Deleted Added
full compact
1/*
2 * Copyright (c) 2004-07 Applied Micro Circuits Corporation.
3 * Copyright (c) 2004-05 Vinod Kashyap.
4 * Copyright (c) 2000 Michael Smith
5 * Copyright (c) 2000 BSDi
6 * All rights reserved.
7 *
8 * Redistribution and use in source and binary forms, with or without
9 * modification, are permitted provided that the following conditions
10 * are met:
11 * 1. Redistributions of source code must retain the above copyright
12 * notice, this list of conditions and the following disclaimer.
13 * 2. Redistributions in binary form must reproduce the above copyright
14 * notice, this list of conditions and the following disclaimer in the
15 * documentation and/or other materials provided with the distribution.
16 *
17 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
18 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
19 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
20 * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
21 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
22 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
23 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
24 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
25 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
26 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
27 * SUCH DAMAGE.
28 *
29 * $FreeBSD: head/sys/dev/twa/tw_osl_freebsd.c 208969 2010-06-09 21:40:38Z delphij $
29 * $FreeBSD: head/sys/dev/twa/tw_osl_freebsd.c 212008 2010-08-30 19:15:04Z delphij $
30 */
31
32/*
33 * AMCC'S 3ware driver for 9000 series storage controllers.
34 *
35 * Author: Vinod Kashyap
36 * Modifications by: Adam Radford
37 * Modifications by: Manjunath Ranganathaiah
38 */
39
40
41/*
42 * FreeBSD specific functions not related to CAM, and other
43 * miscellaneous functions.
44 */
45
46
47#include <dev/twa/tw_osl_includes.h>
48#include <dev/twa/tw_cl_fwif.h>
49#include <dev/twa/tw_cl_ioctl.h>
50#include <dev/twa/tw_osl_ioctl.h>
51
52#ifdef TW_OSL_DEBUG
53TW_INT32 TW_DEBUG_LEVEL_FOR_OSL = TW_OSL_DEBUG;
54TW_INT32 TW_OSL_DEBUG_LEVEL_FOR_CL = TW_OSL_DEBUG;
55#endif /* TW_OSL_DEBUG */
56
57MALLOC_DEFINE(TW_OSLI_MALLOC_CLASS, "twa_commands", "twa commands");
58
59
60static d_open_t twa_open;
61static d_close_t twa_close;
62static d_ioctl_t twa_ioctl;
63
64static struct cdevsw twa_cdevsw = {
65 .d_version = D_VERSION,
66 .d_open = twa_open,
67 .d_close = twa_close,
68 .d_ioctl = twa_ioctl,
69 .d_name = "twa",
70};
71
72static devclass_t twa_devclass;
73
74
75/*
76 * Function name: twa_open
77 * Description: Called when the controller is opened.
78 * Simply marks the controller as open.
79 *
80 * Input: dev -- control device corresponding to the ctlr
81 * flags -- mode of open
82 * fmt -- device type (character/block etc.)
83 * proc -- current process
84 * Output: None
85 * Return value: 0 -- success
86 * non-zero-- failure
87 */
88static TW_INT32
89twa_open(struct cdev *dev, TW_INT32 flags, TW_INT32 fmt, struct thread *proc)
90{
91 struct twa_softc *sc = (struct twa_softc *)(dev->si_drv1);
92
93 tw_osli_dbg_dprintf(5, sc, "entered");
94 sc->open = TW_CL_TRUE;
95 return(0);
96}
97
98
99
100/*
101 * Function name: twa_close
102 * Description: Called when the controller is closed.
103 * Simply marks the controller as not open.
104 *
105 * Input: dev -- control device corresponding to the ctlr
106 * flags -- mode of corresponding open
107 * fmt -- device type (character/block etc.)
108 * proc -- current process
109 * Output: None
110 * Return value: 0 -- success
111 * non-zero-- failure
112 */
113static TW_INT32
114twa_close(struct cdev *dev, TW_INT32 flags, TW_INT32 fmt, struct thread *proc)
115{
116 struct twa_softc *sc = (struct twa_softc *)(dev->si_drv1);
117
118 tw_osli_dbg_dprintf(5, sc, "entered");
119 sc->open = TW_CL_FALSE;
120 return(0);
121}
122
123
124
125/*
126 * Function name: twa_ioctl
127 * Description: Called when an ioctl is posted to the controller.
128 * Handles any OS Layer specific cmds, passes the rest
129 * on to the Common Layer.
130 *
131 * Input: dev -- control device corresponding to the ctlr
132 * cmd -- ioctl cmd
133 * buf -- ptr to buffer in kernel memory, which is
134 * a copy of the input buffer in user-space
135 * flags -- mode of corresponding open
136 * proc -- current process
137 * Output: buf -- ptr to buffer in kernel memory, which will
138 * be copied to the output buffer in user-space
139 * Return value: 0 -- success
140 * non-zero-- failure
141 */
142static TW_INT32
143twa_ioctl(struct cdev *dev, u_long cmd, caddr_t buf, TW_INT32 flags, struct thread *proc)
144{
145 struct twa_softc *sc = (struct twa_softc *)(dev->si_drv1);
146 TW_INT32 error;
147
148 tw_osli_dbg_dprintf(5, sc, "entered");
149
150 switch (cmd) {
151 case TW_OSL_IOCTL_FIRMWARE_PASS_THROUGH:
152 tw_osli_dbg_dprintf(6, sc, "ioctl: fw_passthru");
153 error = tw_osli_fw_passthru(sc, (TW_INT8 *)buf);
154 break;
155
156 case TW_OSL_IOCTL_SCAN_BUS:
157 /* Request CAM for a bus scan. */
158 tw_osli_dbg_dprintf(6, sc, "ioctl: scan bus");
159 error = tw_osli_request_bus_scan(sc);
160 break;
161
162 default:
163 tw_osli_dbg_dprintf(6, sc, "ioctl: 0x%lx", cmd);
164 error = tw_cl_ioctl(&sc->ctlr_handle, cmd, buf);
165 break;
166 }
167 return(error);
168}
169
170
171
172static TW_INT32 twa_probe(device_t dev);
173static TW_INT32 twa_attach(device_t dev);
174static TW_INT32 twa_detach(device_t dev);
175static TW_INT32 twa_shutdown(device_t dev);
176static TW_VOID twa_busdma_lock(TW_VOID *lock_arg, bus_dma_lock_op_t op);
177static TW_VOID twa_pci_intr(TW_VOID *arg);
178static TW_VOID twa_watchdog(TW_VOID *arg);
179int twa_setup_intr(struct twa_softc *sc);
180int twa_teardown_intr(struct twa_softc *sc);
181
182static TW_INT32 tw_osli_alloc_mem(struct twa_softc *sc);
183static TW_VOID tw_osli_free_resources(struct twa_softc *sc);
184
185static TW_VOID twa_map_load_data_callback(TW_VOID *arg,
186 bus_dma_segment_t *segs, TW_INT32 nsegments, TW_INT32 error);
187static TW_VOID twa_map_load_callback(TW_VOID *arg,
188 bus_dma_segment_t *segs, TW_INT32 nsegments, TW_INT32 error);
189
190
191static device_method_t twa_methods[] = {
192 /* Device interface */
193 DEVMETHOD(device_probe, twa_probe),
194 DEVMETHOD(device_attach, twa_attach),
195 DEVMETHOD(device_detach, twa_detach),
196 DEVMETHOD(device_shutdown, twa_shutdown),
197
198 DEVMETHOD(bus_print_child, bus_generic_print_child),
199 DEVMETHOD(bus_driver_added, bus_generic_driver_added),
200 {0, 0}
201};
202
203static driver_t twa_pci_driver = {
204 "twa",
205 twa_methods,
206 sizeof(struct twa_softc)
207};
208
209DRIVER_MODULE(twa, pci, twa_pci_driver, twa_devclass, 0, 0);
210MODULE_DEPEND(twa, cam, 1, 1, 1);
211MODULE_DEPEND(twa, pci, 1, 1, 1);
212
213
214/*
215 * Function name: twa_probe
216 * Description: Called at driver load time. Claims 9000 ctlrs.
217 *
218 * Input: dev -- bus device corresponding to the ctlr
219 * Output: None
220 * Return value: <= 0 -- success
221 * > 0 -- failure
222 */
223static TW_INT32
224twa_probe(device_t dev)
225{
226 static TW_UINT8 first_ctlr = 1;
227
228 tw_osli_dbg_printf(3, "entered");
229
230 if (tw_cl_ctlr_supported(pci_get_vendor(dev), pci_get_device(dev))) {
231 device_set_desc(dev, TW_OSLI_DEVICE_NAME);
232 /* Print the driver version only once. */
233 if (first_ctlr) {
234 printf("3ware device driver for 9000 series storage "
235 "controllers, version: %s\n",
236 TW_OSL_DRIVER_VERSION_STRING);
237 first_ctlr = 0;
238 }
239 return(0);
240 }
241 return(ENXIO);
242}
243
244int twa_setup_intr(struct twa_softc *sc)
245{
246 int error = 0;
247
248 if (!(sc->intr_handle) && (sc->irq_res)) {
249 error = bus_setup_intr(sc->bus_dev, sc->irq_res,
250 INTR_TYPE_CAM | INTR_MPSAFE,
251 NULL, twa_pci_intr,
252 sc, &sc->intr_handle);
253 }
254 return( error );
255}
256
257
258int twa_teardown_intr(struct twa_softc *sc)
259{
260 int error = 0;
261
262 if ((sc->intr_handle) && (sc->irq_res)) {
263 error = bus_teardown_intr(sc->bus_dev,
264 sc->irq_res, sc->intr_handle);
265 sc->intr_handle = NULL;
266 }
267 return( error );
268}
269
270
271
272/*
273 * Function name: twa_attach
274 * Description: Allocates pci resources; updates sc; adds a node to the
275 * sysctl tree to expose the driver version; makes calls
276 * (to the Common Layer) to initialize ctlr, and to
277 * attach to CAM.
278 *
279 * Input: dev -- bus device corresponding to the ctlr
280 * Output: None
281 * Return value: 0 -- success
282 * non-zero-- failure
283 */
284static TW_INT32
285twa_attach(device_t dev)
286{
287 struct twa_softc *sc = device_get_softc(dev);
288 TW_UINT32 command;
289 TW_INT32 bar_num;
290 TW_INT32 bar0_offset;
291 TW_INT32 bar_size;
292 TW_INT32 error;
293
294 tw_osli_dbg_dprintf(3, sc, "entered");
295
296 sc->ctlr_handle.osl_ctlr_ctxt = sc;
297
298 /* Initialize the softc structure. */
299 sc->bus_dev = dev;
300 sc->device_id = pci_get_device(dev);
301
302 /* Initialize the mutexes right here. */
303 sc->io_lock = &(sc->io_lock_handle);
304 mtx_init(sc->io_lock, "tw_osl_io_lock", NULL, MTX_SPIN);
305 sc->q_lock = &(sc->q_lock_handle);
306 mtx_init(sc->q_lock, "tw_osl_q_lock", NULL, MTX_SPIN);
307 sc->sim_lock = &(sc->sim_lock_handle);
308 mtx_init(sc->sim_lock, "tw_osl_sim_lock", NULL, MTX_DEF | MTX_RECURSE);
309
310 sysctl_ctx_init(&sc->sysctl_ctxt);
311 sc->sysctl_tree = SYSCTL_ADD_NODE(&sc->sysctl_ctxt,
312 SYSCTL_STATIC_CHILDREN(_hw), OID_AUTO,
313 device_get_nameunit(dev), CTLFLAG_RD, 0, "");
314 if (sc->sysctl_tree == NULL) {
315 tw_osli_printf(sc, "error = %d",
316 TW_CL_SEVERITY_ERROR_STRING,
317 TW_CL_MESSAGE_SOURCE_FREEBSD_DRIVER,
318 0x2000,
319 "Cannot add sysctl tree node",
320 ENXIO);
321 return(ENXIO);
322 }
323 SYSCTL_ADD_STRING(&sc->sysctl_ctxt, SYSCTL_CHILDREN(sc->sysctl_tree),
324 OID_AUTO, "driver_version", CTLFLAG_RD,
325 TW_OSL_DRIVER_VERSION_STRING, 0, "TWA driver version");
326
327 /* Make sure we are going to be able to talk to this board. */
328 command = pci_read_config(dev, PCIR_COMMAND, 2);
329 if ((command & PCIM_CMD_PORTEN) == 0) {
330 tw_osli_printf(sc, "error = %d",
331 TW_CL_SEVERITY_ERROR_STRING,
332 TW_CL_MESSAGE_SOURCE_FREEBSD_DRIVER,
333 0x2001,
334 "Register window not available",
335 ENXIO);
336 tw_osli_free_resources(sc);
337 return(ENXIO);
338 }
339
340 /* Force the busmaster enable bit on, in case the BIOS forgot. */
341 command |= PCIM_CMD_BUSMASTEREN;
342 pci_write_config(dev, PCIR_COMMAND, command, 2);
343
344 /* Allocate the PCI register window. */
345 if ((error = tw_cl_get_pci_bar_info(sc->device_id, TW_CL_BAR_TYPE_MEM,
346 &bar_num, &bar0_offset, &bar_size))) {
347 tw_osli_printf(sc, "error = %d",
348 TW_CL_SEVERITY_ERROR_STRING,
349 TW_CL_MESSAGE_SOURCE_FREEBSD_DRIVER,
350 0x201F,
351 "Can't get PCI BAR info",
352 error);
353 tw_osli_free_resources(sc);
354 return(error);
355 }
356 sc->reg_res_id = PCIR_BARS + bar0_offset;
357 if ((sc->reg_res = bus_alloc_resource(dev, SYS_RES_MEMORY,
358 &(sc->reg_res_id), 0, ~0, 1, RF_ACTIVE))
359 == NULL) {
360 tw_osli_printf(sc, "error = %d",
361 TW_CL_SEVERITY_ERROR_STRING,
362 TW_CL_MESSAGE_SOURCE_FREEBSD_DRIVER,
363 0x2002,
364 "Can't allocate register window",
365 ENXIO);
366 tw_osli_free_resources(sc);
367 return(ENXIO);
368 }
369 sc->bus_tag = rman_get_bustag(sc->reg_res);
370 sc->bus_handle = rman_get_bushandle(sc->reg_res);
371
372 /* Allocate and register our interrupt. */
373 sc->irq_res_id = 0;
374 if ((sc->irq_res = bus_alloc_resource(sc->bus_dev, SYS_RES_IRQ,
375 &(sc->irq_res_id), 0, ~0, 1,
376 RF_SHAREABLE | RF_ACTIVE)) == NULL) {
377 tw_osli_printf(sc, "error = %d",
378 TW_CL_SEVERITY_ERROR_STRING,
379 TW_CL_MESSAGE_SOURCE_FREEBSD_DRIVER,
380 0x2003,
381 "Can't allocate interrupt",
382 ENXIO);
383 tw_osli_free_resources(sc);
384 return(ENXIO);
385 }
357 if ((error = bus_setup_intr(sc->bus_dev, sc->irq_res,
358 INTR_TYPE_CAM | INTR_MPSAFE,
359 NULL, twa_pci_intr,
360 sc, &sc->intr_handle))) {
386 if ((error = twa_setup_intr(sc))) {
387 tw_osli_printf(sc, "error = %d",
388 TW_CL_SEVERITY_ERROR_STRING,
389 TW_CL_MESSAGE_SOURCE_FREEBSD_DRIVER,
390 0x2004,
391 "Can't set up interrupt",
392 error);
393 tw_osli_free_resources(sc);
394 return(error);
395 }
396
397 if ((error = tw_osli_alloc_mem(sc))) {
398 tw_osli_printf(sc, "error = %d",
399 TW_CL_SEVERITY_ERROR_STRING,
400 TW_CL_MESSAGE_SOURCE_FREEBSD_DRIVER,
401 0x2005,
402 "Memory allocation failure",
403 error);
404 tw_osli_free_resources(sc);
405 return(error);
406 }
407
408 /* Initialize the Common Layer for this controller. */
409 if ((error = tw_cl_init_ctlr(&sc->ctlr_handle, sc->flags, sc->device_id,
410 TW_OSLI_MAX_NUM_REQUESTS, TW_OSLI_MAX_NUM_AENS,
411 sc->non_dma_mem, sc->dma_mem,
412 sc->dma_mem_phys
413 ))) {
414 tw_osli_printf(sc, "error = %d",
415 TW_CL_SEVERITY_ERROR_STRING,
416 TW_CL_MESSAGE_SOURCE_FREEBSD_DRIVER,
417 0x2006,
418 "Failed to initialize Common Layer/controller",
419 error);
420 tw_osli_free_resources(sc);
421 return(error);
422 }
423
424 /* Create the control device. */
425 sc->ctrl_dev = make_dev(&twa_cdevsw, device_get_unit(sc->bus_dev),
426 UID_ROOT, GID_OPERATOR, S_IRUSR | S_IWUSR,
427 "twa%d", device_get_unit(sc->bus_dev));
428 sc->ctrl_dev->si_drv1 = sc;
429
430 if ((error = tw_osli_cam_attach(sc))) {
431 tw_osli_free_resources(sc);
432 tw_osli_printf(sc, "error = %d",
433 TW_CL_SEVERITY_ERROR_STRING,
434 TW_CL_MESSAGE_SOURCE_FREEBSD_DRIVER,
435 0x2007,
436 "Failed to initialize CAM",
437 error);
438 return(error);
439 }
440
441 sc->watchdog_index = 0;
442 callout_init(&(sc->watchdog_callout[0]), CALLOUT_MPSAFE);
443 callout_init(&(sc->watchdog_callout[1]), CALLOUT_MPSAFE);
444 callout_reset(&(sc->watchdog_callout[0]), 5*hz, twa_watchdog, &sc->ctlr_handle);
445
446 return(0);
447}
448
449
450static TW_VOID
451twa_watchdog(TW_VOID *arg)
452{
453 struct tw_cl_ctlr_handle *ctlr_handle =
454 (struct tw_cl_ctlr_handle *)arg;
455 struct twa_softc *sc = ctlr_handle->osl_ctlr_ctxt;
456 int i;
457 int i_need_a_reset = 0;
458 int driver_is_active = 0;
459 int my_watchdog_was_pending = 1234;
460 TW_UINT64 current_time;
461 struct tw_osli_req_context *my_req;
462
463
464//==============================================================================
465 current_time = (TW_UINT64) (tw_osl_get_local_time());
466
467 for (i = 0; i < TW_OSLI_MAX_NUM_REQUESTS; i++) {
468 my_req = &(sc->req_ctx_buf[i]);
469
470 if ((my_req->state == TW_OSLI_REQ_STATE_BUSY) &&
471 (my_req->deadline) &&
472 (my_req->deadline < current_time)) {
473 tw_cl_set_reset_needed(ctlr_handle);
474#ifdef TW_OSL_DEBUG
475 device_printf((sc)->bus_dev, "Request %d timed out! d = %p, c = %p\n", i, (void*)my_req->deadline, (void*)current_time);
476#else /* TW_OSL_DEBUG */
477 device_printf((sc)->bus_dev, "Request %d timed out!\n", i);
478#endif /* TW_OSL_DEBUG */
479 break;
480 }
481 }
482//==============================================================================
483
484 i_need_a_reset = tw_cl_is_reset_needed(ctlr_handle);
485
486 i = (int) ((sc->watchdog_index++) & 1);
487
488 driver_is_active = tw_cl_is_active(ctlr_handle);
489
490 if (i_need_a_reset) {
491#ifdef TW_OSL_DEBUG
492 device_printf((sc)->bus_dev, "Watchdog rescheduled in 70 seconds\n");
493#endif /* TW_OSL_DEBUG */
494 my_watchdog_was_pending =
495 callout_reset(&(sc->watchdog_callout[i]), 70*hz, twa_watchdog, &sc->ctlr_handle);
496 tw_cl_reset_ctlr(ctlr_handle);
497#ifdef TW_OSL_DEBUG
498 device_printf((sc)->bus_dev, "Watchdog reset completed!\n");
499#endif /* TW_OSL_DEBUG */
500 } else if (driver_is_active) {
501 my_watchdog_was_pending =
502 callout_reset(&(sc->watchdog_callout[i]), 5*hz, twa_watchdog, &sc->ctlr_handle);
503 }
504#ifdef TW_OSL_DEBUG
505 if (i_need_a_reset || my_watchdog_was_pending)
506 device_printf((sc)->bus_dev, "i_need_a_reset = %d, "
507 "driver_is_active = %d, my_watchdog_was_pending = %d\n",
508 i_need_a_reset, driver_is_active, my_watchdog_was_pending);
509#endif /* TW_OSL_DEBUG */
510}
511
512
513/*
514 * Function name: tw_osli_alloc_mem
515 * Description: Allocates memory needed both by CL and OSL.
516 *
517 * Input: sc -- OSL internal controller context
518 * Output: None
519 * Return value: 0 -- success
520 * non-zero-- failure
521 */
522static TW_INT32
523tw_osli_alloc_mem(struct twa_softc *sc)
524{
525 struct tw_osli_req_context *req;
526 TW_UINT32 max_sg_elements;
527 TW_UINT32 non_dma_mem_size;
528 TW_UINT32 dma_mem_size;
529 TW_INT32 error;
530 TW_INT32 i;
531
532 tw_osli_dbg_dprintf(3, sc, "entered");
533
534 sc->flags |= (sizeof(bus_addr_t) == 8) ? TW_CL_64BIT_ADDRESSES : 0;
535 sc->flags |= (sizeof(bus_size_t) == 8) ? TW_CL_64BIT_SG_LENGTH : 0;
536
537 max_sg_elements = (sizeof(bus_addr_t) == 8) ?
538 TW_CL_MAX_64BIT_SG_ELEMENTS : TW_CL_MAX_32BIT_SG_ELEMENTS;
539
540 if ((error = tw_cl_get_mem_requirements(&sc->ctlr_handle, sc->flags,
541 sc->device_id, TW_OSLI_MAX_NUM_REQUESTS, TW_OSLI_MAX_NUM_AENS,
542 &(sc->alignment), &(sc->sg_size_factor),
543 &non_dma_mem_size, &dma_mem_size
544 ))) {
545 tw_osli_printf(sc, "error = %d",
546 TW_CL_SEVERITY_ERROR_STRING,
547 TW_CL_MESSAGE_SOURCE_FREEBSD_DRIVER,
548 0x2008,
549 "Can't get Common Layer's memory requirements",
550 error);
551 return(error);
552 }
553
554 if ((sc->non_dma_mem = malloc(non_dma_mem_size, TW_OSLI_MALLOC_CLASS,
555 M_WAITOK)) == NULL) {
556 tw_osli_printf(sc, "error = %d",
557 TW_CL_SEVERITY_ERROR_STRING,
558 TW_CL_MESSAGE_SOURCE_FREEBSD_DRIVER,
559 0x2009,
560 "Can't allocate non-dma memory",
561 ENOMEM);
562 return(ENOMEM);
563 }
564
565 /* Create the parent dma tag. */
566 if (bus_dma_tag_create(NULL, /* parent */
567 sc->alignment, /* alignment */
568 TW_OSLI_DMA_BOUNDARY, /* boundary */
569 BUS_SPACE_MAXADDR, /* lowaddr */
570 BUS_SPACE_MAXADDR, /* highaddr */
571 NULL, NULL, /* filter, filterarg */
572 TW_CL_MAX_IO_SIZE, /* maxsize */
573 max_sg_elements, /* nsegments */
574 TW_CL_MAX_IO_SIZE, /* maxsegsize */
575 0, /* flags */
576 NULL, /* lockfunc */
577 NULL, /* lockfuncarg */
578 &sc->parent_tag /* tag */)) {
579 tw_osli_printf(sc, "error = %d",
580 TW_CL_SEVERITY_ERROR_STRING,
581 TW_CL_MESSAGE_SOURCE_FREEBSD_DRIVER,
582 0x200A,
583 "Can't allocate parent DMA tag",
584 ENOMEM);
585 return(ENOMEM);
586 }
587
588 /* Create a dma tag for Common Layer's DMA'able memory (dma_mem). */
589 if (bus_dma_tag_create(sc->parent_tag, /* parent */
590 sc->alignment, /* alignment */
591 0, /* boundary */
592 BUS_SPACE_MAXADDR, /* lowaddr */
593 BUS_SPACE_MAXADDR, /* highaddr */
594 NULL, NULL, /* filter, filterarg */
595 dma_mem_size, /* maxsize */
596 1, /* nsegments */
597 BUS_SPACE_MAXSIZE, /* maxsegsize */
598 0, /* flags */
599 NULL, /* lockfunc */
600 NULL, /* lockfuncarg */
601 &sc->cmd_tag /* tag */)) {
602 tw_osli_printf(sc, "error = %d",
603 TW_CL_SEVERITY_ERROR_STRING,
604 TW_CL_MESSAGE_SOURCE_FREEBSD_DRIVER,
605 0x200B,
606 "Can't allocate DMA tag for Common Layer's "
607 "DMA'able memory",
608 ENOMEM);
609 return(ENOMEM);
610 }
611
612 if (bus_dmamem_alloc(sc->cmd_tag, &sc->dma_mem,
613 BUS_DMA_NOWAIT, &sc->cmd_map)) {
614 /* Try a second time. */
615 if (bus_dmamem_alloc(sc->cmd_tag, &sc->dma_mem,
616 BUS_DMA_NOWAIT, &sc->cmd_map)) {
617 tw_osli_printf(sc, "error = %d",
618 TW_CL_SEVERITY_ERROR_STRING,
619 TW_CL_MESSAGE_SOURCE_FREEBSD_DRIVER,
620 0x200C,
621 "Can't allocate DMA'able memory for the"
622 "Common Layer",
623 ENOMEM);
624 return(ENOMEM);
625 }
626 }
627
628 bus_dmamap_load(sc->cmd_tag, sc->cmd_map, sc->dma_mem,
629 dma_mem_size, twa_map_load_callback,
630 &sc->dma_mem_phys, 0);
631
632 /*
633 * Create a dma tag for data buffers; size will be the maximum
634 * possible I/O size (128kB).
635 */
636 if (bus_dma_tag_create(sc->parent_tag, /* parent */
637 sc->alignment, /* alignment */
638 0, /* boundary */
639 BUS_SPACE_MAXADDR, /* lowaddr */
640 BUS_SPACE_MAXADDR, /* highaddr */
641 NULL, NULL, /* filter, filterarg */
642 TW_CL_MAX_IO_SIZE, /* maxsize */
643 max_sg_elements, /* nsegments */
644 TW_CL_MAX_IO_SIZE, /* maxsegsize */
645 BUS_DMA_ALLOCNOW, /* flags */
646 twa_busdma_lock, /* lockfunc */
647 sc->io_lock, /* lockfuncarg */
648 &sc->dma_tag /* tag */)) {
649 tw_osli_printf(sc, "error = %d",
650 TW_CL_SEVERITY_ERROR_STRING,
651 TW_CL_MESSAGE_SOURCE_FREEBSD_DRIVER,
652 0x200F,
653 "Can't allocate DMA tag for data buffers",
654 ENOMEM);
655 return(ENOMEM);
656 }
657
658 /*
659 * Create a dma tag for ioctl data buffers; size will be the maximum
660 * possible I/O size (128kB).
661 */
662 if (bus_dma_tag_create(sc->parent_tag, /* parent */
663 sc->alignment, /* alignment */
664 0, /* boundary */
665 BUS_SPACE_MAXADDR, /* lowaddr */
666 BUS_SPACE_MAXADDR, /* highaddr */
667 NULL, NULL, /* filter, filterarg */
668 TW_CL_MAX_IO_SIZE, /* maxsize */
669 max_sg_elements, /* nsegments */
670 TW_CL_MAX_IO_SIZE, /* maxsegsize */
671 BUS_DMA_ALLOCNOW, /* flags */
672 twa_busdma_lock, /* lockfunc */
673 sc->io_lock, /* lockfuncarg */
674 &sc->ioctl_tag /* tag */)) {
675 tw_osli_printf(sc, "error = %d",
676 TW_CL_SEVERITY_ERROR_STRING,
677 TW_CL_MESSAGE_SOURCE_FREEBSD_DRIVER,
678 0x2010,
679 "Can't allocate DMA tag for ioctl data buffers",
680 ENOMEM);
681 return(ENOMEM);
682 }
683
684 /* Create just one map for all ioctl request data buffers. */
685 if (bus_dmamap_create(sc->ioctl_tag, 0, &sc->ioctl_map)) {
686 tw_osli_printf(sc, "error = %d",
687 TW_CL_SEVERITY_ERROR_STRING,
688 TW_CL_MESSAGE_SOURCE_FREEBSD_DRIVER,
689 0x2011,
690 "Can't create ioctl map",
691 ENOMEM);
692 return(ENOMEM);
693 }
694
695
696 /* Initialize request queues. */
697 tw_osli_req_q_init(sc, TW_OSLI_FREE_Q);
698 tw_osli_req_q_init(sc, TW_OSLI_BUSY_Q);
699
700 if ((sc->req_ctx_buf = (struct tw_osli_req_context *)
701 malloc((sizeof(struct tw_osli_req_context) *
702 TW_OSLI_MAX_NUM_REQUESTS),
703 TW_OSLI_MALLOC_CLASS, M_WAITOK)) == NULL) {
704 tw_osli_printf(sc, "error = %d",
705 TW_CL_SEVERITY_ERROR_STRING,
706 TW_CL_MESSAGE_SOURCE_FREEBSD_DRIVER,
707 0x2012,
708 "Failed to allocate request packets",
709 ENOMEM);
710 return(ENOMEM);
711 }
712 bzero(sc->req_ctx_buf,
713 sizeof(struct tw_osli_req_context) * TW_OSLI_MAX_NUM_REQUESTS);
714
715 for (i = 0; i < TW_OSLI_MAX_NUM_REQUESTS; i++) {
716 req = &(sc->req_ctx_buf[i]);
717 req->ctlr = sc;
718 if (bus_dmamap_create(sc->dma_tag, 0, &req->dma_map)) {
719 tw_osli_printf(sc, "request # = %d, error = %d",
720 TW_CL_SEVERITY_ERROR_STRING,
721 TW_CL_MESSAGE_SOURCE_FREEBSD_DRIVER,
722 0x2013,
723 "Can't create dma map",
724 i, ENOMEM);
725 return(ENOMEM);
726 }
727
728 /* Initialize the ioctl wakeup/ timeout mutex */
729 req->ioctl_wake_timeout_lock = &(req->ioctl_wake_timeout_lock_handle);
730 mtx_init(req->ioctl_wake_timeout_lock, "tw_ioctl_wake_timeout_lock", NULL, MTX_DEF);
731
732 /* Insert request into the free queue. */
733 tw_osli_req_q_insert_tail(req, TW_OSLI_FREE_Q);
734 }
735
736 return(0);
737}
738
739
740
741/*
742 * Function name: tw_osli_free_resources
743 * Description: Performs clean-up at the time of going down.
744 *
745 * Input: sc -- ptr to OSL internal ctlr context
746 * Output: None
747 * Return value: None
748 */
749static TW_VOID
750tw_osli_free_resources(struct twa_softc *sc)
751{
752 struct tw_osli_req_context *req;
753 TW_INT32 error = 0;
754
755 tw_osli_dbg_dprintf(3, sc, "entered");
756
757 /* Detach from CAM */
758 tw_osli_cam_detach(sc);
759
760 if (sc->req_ctx_buf)
761 while ((req = tw_osli_req_q_remove_head(sc, TW_OSLI_FREE_Q)) !=
762 NULL) {
763 mtx_destroy(req->ioctl_wake_timeout_lock);
764
765 if ((error = bus_dmamap_destroy(sc->dma_tag,
766 req->dma_map)))
767 tw_osli_dbg_dprintf(1, sc,
768 "dmamap_destroy(dma) returned %d",
769 error);
770 }
771
772 if ((sc->ioctl_tag) && (sc->ioctl_map))
773 if ((error = bus_dmamap_destroy(sc->ioctl_tag, sc->ioctl_map)))
774 tw_osli_dbg_dprintf(1, sc,
775 "dmamap_destroy(ioctl) returned %d", error);
776
777 /* Free all memory allocated so far. */
778 if (sc->req_ctx_buf)
779 free(sc->req_ctx_buf, TW_OSLI_MALLOC_CLASS);
780
781 if (sc->non_dma_mem)
782 free(sc->non_dma_mem, TW_OSLI_MALLOC_CLASS);
783
784 if (sc->dma_mem) {
785 bus_dmamap_unload(sc->cmd_tag, sc->cmd_map);
786 bus_dmamem_free(sc->cmd_tag, sc->dma_mem,
787 sc->cmd_map);
788 }
789 if (sc->cmd_tag)
790 if ((error = bus_dma_tag_destroy(sc->cmd_tag)))
791 tw_osli_dbg_dprintf(1, sc,
792 "dma_tag_destroy(cmd) returned %d", error);
793
794 if (sc->dma_tag)
795 if ((error = bus_dma_tag_destroy(sc->dma_tag)))
796 tw_osli_dbg_dprintf(1, sc,
797 "dma_tag_destroy(dma) returned %d", error);
798
799 if (sc->ioctl_tag)
800 if ((error = bus_dma_tag_destroy(sc->ioctl_tag)))
801 tw_osli_dbg_dprintf(1, sc,
802 "dma_tag_destroy(ioctl) returned %d", error);
803
804 if (sc->parent_tag)
805 if ((error = bus_dma_tag_destroy(sc->parent_tag)))
806 tw_osli_dbg_dprintf(1, sc,
807 "dma_tag_destroy(parent) returned %d", error);
808
809
810 /* Disconnect the interrupt handler. */
718 if (sc->intr_handle)
719 if ((error = bus_teardown_intr(sc->bus_dev,
720 sc->irq_res, sc->intr_handle)))
811 if ((error = twa_teardown_intr(sc)))
812 tw_osli_dbg_dprintf(1, sc,
813 "teardown_intr returned %d", error);
814
815 if (sc->irq_res != NULL)
816 if ((error = bus_release_resource(sc->bus_dev,
817 SYS_RES_IRQ, sc->irq_res_id, sc->irq_res)))
818 tw_osli_dbg_dprintf(1, sc,
819 "release_resource(irq) returned %d", error);
820
821
822 /* Release the register window mapping. */
823 if (sc->reg_res != NULL)
824 if ((error = bus_release_resource(sc->bus_dev,
825 SYS_RES_MEMORY, sc->reg_res_id, sc->reg_res)))
826 tw_osli_dbg_dprintf(1, sc,
827 "release_resource(io) returned %d", error);
828
829
830 /* Destroy the control device. */
831 if (sc->ctrl_dev != (struct cdev *)NULL)
832 destroy_dev(sc->ctrl_dev);
833
834 if ((error = sysctl_ctx_free(&sc->sysctl_ctxt)))
835 tw_osli_dbg_dprintf(1, sc,
836 "sysctl_ctx_free returned %d", error);
837
838}
839
840
841
842/*
843 * Function name: twa_detach
844 * Description: Called when the controller is being detached from
845 * the pci bus.
846 *
847 * Input: dev -- bus device corresponding to the ctlr
848 * Output: None
849 * Return value: 0 -- success
850 * non-zero-- failure
851 */
852static TW_INT32
853twa_detach(device_t dev)
854{
855 struct twa_softc *sc = device_get_softc(dev);
856 TW_INT32 error;
857
858 tw_osli_dbg_dprintf(3, sc, "entered");
859
860 error = EBUSY;
861 if (sc->open) {
862 tw_osli_printf(sc, "error = %d",
863 TW_CL_SEVERITY_ERROR_STRING,
864 TW_CL_MESSAGE_SOURCE_FREEBSD_DRIVER,
865 0x2014,
866 "Device open",
867 error);
868 goto out;
869 }
870
871 /* Shut the controller down. */
872 if ((error = twa_shutdown(dev)))
873 goto out;
874
875 /* Free all resources associated with this controller. */
876 tw_osli_free_resources(sc);
877 error = 0;
878
879out:
880 return(error);
881}
882
883
884
885/*
886 * Function name: twa_shutdown
887 * Description: Called at unload/shutdown time. Lets the controller
888 * know that we are going down.
889 *
890 * Input: dev -- bus device corresponding to the ctlr
891 * Output: None
892 * Return value: 0 -- success
893 * non-zero-- failure
894 */
895static TW_INT32
896twa_shutdown(device_t dev)
897{
898 struct twa_softc *sc = device_get_softc(dev);
899 TW_INT32 error = 0;
900
901 tw_osli_dbg_dprintf(3, sc, "entered");
902
903 /* Disconnect interrupts. */
904 error = twa_teardown_intr(sc);
905
906 /* Disconnect from the controller. */
907 if ((error = tw_cl_shutdown_ctlr(&(sc->ctlr_handle), 0))) {
908 tw_osli_printf(sc, "error = %d",
909 TW_CL_SEVERITY_ERROR_STRING,
910 TW_CL_MESSAGE_SOURCE_FREEBSD_DRIVER,
911 0x2015,
912 "Failed to shutdown Common Layer/controller",
913 error);
914 }
915 return(error);
916}
917
918
919
920/*
921 * Function name: twa_busdma_lock
922 * Description: Function to provide synchronization during busdma_swi.
923 *
924 * Input: lock_arg -- lock mutex sent as argument
925 * op -- operation (lock/unlock) expected of the function
926 * Output: None
927 * Return value: None
928 */
929TW_VOID
930twa_busdma_lock(TW_VOID *lock_arg, bus_dma_lock_op_t op)
931{
932 struct mtx *lock;
933
934 lock = (struct mtx *)lock_arg;
935 switch (op) {
936 case BUS_DMA_LOCK:
937 mtx_lock_spin(lock);
938 break;
939
940 case BUS_DMA_UNLOCK:
941 mtx_unlock_spin(lock);
942 break;
943
944 default:
945 panic("Unknown operation 0x%x for twa_busdma_lock!", op);
946 }
947}
948
949
950/*
951 * Function name: twa_pci_intr
952 * Description: Interrupt handler. Wrapper for twa_interrupt.
953 *
954 * Input: arg -- ptr to OSL internal ctlr context
955 * Output: None
956 * Return value: None
957 */
958static TW_VOID
959twa_pci_intr(TW_VOID *arg)
960{
961 struct twa_softc *sc = (struct twa_softc *)arg;
962
963 tw_osli_dbg_dprintf(10, sc, "entered");
964 tw_cl_interrupt(&(sc->ctlr_handle));
965}
966
967
968/*
969 * Function name: tw_osli_fw_passthru
970 * Description: Builds a fw passthru cmd pkt, and submits it to CL.
971 *
972 * Input: sc -- ptr to OSL internal ctlr context
973 * buf -- ptr to ioctl pkt understood by CL
974 * Output: None
975 * Return value: 0 -- success
976 * non-zero-- failure
977 */
978TW_INT32
979tw_osli_fw_passthru(struct twa_softc *sc, TW_INT8 *buf)
980{
981 struct tw_osli_req_context *req;
982 struct tw_osli_ioctl_no_data_buf *user_buf =
983 (struct tw_osli_ioctl_no_data_buf *)buf;
984 TW_TIME end_time;
985 TW_UINT32 timeout = 60;
986 TW_UINT32 data_buf_size_adjusted;
987 struct tw_cl_req_packet *req_pkt;
988 struct tw_cl_passthru_req_packet *pt_req;
989 TW_INT32 error;
990
991 tw_osli_dbg_dprintf(5, sc, "ioctl: passthru");
992
993 if ((req = tw_osli_get_request(sc)) == NULL)
994 return(EBUSY);
995
996 req->req_handle.osl_req_ctxt = req;
997 req->orig_req = buf;
998 req->flags |= TW_OSLI_REQ_FLAGS_PASSTHRU;
999
1000 req_pkt = &(req->req_pkt);
1001 req_pkt->status = 0;
1002 req_pkt->tw_osl_callback = tw_osl_complete_passthru;
1003 /* Let the Common Layer retry the request on cmd queue full. */
1004 req_pkt->flags |= TW_CL_REQ_RETRY_ON_BUSY;
1005
1006 pt_req = &(req_pkt->gen_req_pkt.pt_req);
1007 /*
1008 * Make sure that the data buffer sent to firmware is a
1009 * 512 byte multiple in size.
1010 */
1011 data_buf_size_adjusted =
1012 (user_buf->driver_pkt.buffer_length +
1013 (sc->sg_size_factor - 1)) & ~(sc->sg_size_factor - 1);
1014 if ((req->length = data_buf_size_adjusted)) {
1015 if ((req->data = malloc(data_buf_size_adjusted,
1016 TW_OSLI_MALLOC_CLASS, M_WAITOK)) == NULL) {
1017 error = ENOMEM;
1018 tw_osli_printf(sc, "error = %d",
1019 TW_CL_SEVERITY_ERROR_STRING,
1020 TW_CL_MESSAGE_SOURCE_FREEBSD_DRIVER,
1021 0x2016,
1022 "Could not alloc mem for "
1023 "fw_passthru data_buf",
1024 error);
1025 goto fw_passthru_err;
1026 }
1027 /* Copy the payload. */
1028 if ((error = copyin((TW_VOID *)(user_buf->pdata),
1029 req->data,
1030 user_buf->driver_pkt.buffer_length)) != 0) {
1031 tw_osli_printf(sc, "error = %d",
1032 TW_CL_SEVERITY_ERROR_STRING,
1033 TW_CL_MESSAGE_SOURCE_FREEBSD_DRIVER,
1034 0x2017,
1035 "Could not copyin fw_passthru data_buf",
1036 error);
1037 goto fw_passthru_err;
1038 }
1039 pt_req->sgl_entries = 1; /* will be updated during mapping */
1040 req->flags |= (TW_OSLI_REQ_FLAGS_DATA_IN |
1041 TW_OSLI_REQ_FLAGS_DATA_OUT);
1042 } else
1043 pt_req->sgl_entries = 0; /* no payload */
1044
1045 pt_req->cmd_pkt = (TW_VOID *)(&(user_buf->cmd_pkt));
1046 pt_req->cmd_pkt_length = sizeof(struct tw_cl_command_packet);
1047
1048 if ((error = tw_osli_map_request(req)))
1049 goto fw_passthru_err;
1050
1051 end_time = tw_osl_get_local_time() + timeout;
1052 while (req->state != TW_OSLI_REQ_STATE_COMPLETE) {
1053 mtx_lock(req->ioctl_wake_timeout_lock);
1054 req->flags |= TW_OSLI_REQ_FLAGS_SLEEPING;
1055
1056 error = mtx_sleep(req, req->ioctl_wake_timeout_lock, 0,
1057 "twa_passthru", timeout*hz);
1058 mtx_unlock(req->ioctl_wake_timeout_lock);
1059
1060 if (!(req->flags & TW_OSLI_REQ_FLAGS_SLEEPING))
1061 error = 0;
1062 req->flags &= ~TW_OSLI_REQ_FLAGS_SLEEPING;
1063
1064 if (! error) {
1065 if (((error = req->error_code)) ||
1066 ((error = (req->state !=
1067 TW_OSLI_REQ_STATE_COMPLETE))) ||
1068 ((error = req_pkt->status)))
1069 goto fw_passthru_err;
1070 break;
1071 }
1072
1073 if (req_pkt->status) {
1074 error = req_pkt->status;
1075 goto fw_passthru_err;
1076 }
1077
1078 if (error == EWOULDBLOCK) {
1079 /* Time out! */
1080 if ((!(req->error_code)) &&
1081 (req->state == TW_OSLI_REQ_STATE_COMPLETE) &&
1082 (!(req_pkt->status)) ) {
1083#ifdef TW_OSL_DEBUG
1084 tw_osli_printf(sc, "request = %p",
1085 TW_CL_SEVERITY_ERROR_STRING,
1086 TW_CL_MESSAGE_SOURCE_FREEBSD_DRIVER,
1087 0x7777,
1088 "FALSE Passthru timeout!",
1089 req);
1090#endif /* TW_OSL_DEBUG */
1091 error = 0; /* False error */
1092 break;
1093 }
1000 tw_osli_printf(sc, "request = %p",
1001 TW_CL_SEVERITY_ERROR_STRING,
1002 TW_CL_MESSAGE_SOURCE_FREEBSD_DRIVER,
1003 0x2018,
1004 "Passthru request timed out!",
1005 req);
1094 if (!(tw_cl_is_reset_needed(&(req->ctlr->ctlr_handle)))) {
1095#ifdef TW_OSL_DEBUG
1096 tw_osli_printf(sc, "request = %p",
1097 TW_CL_SEVERITY_ERROR_STRING,
1098 TW_CL_MESSAGE_SOURCE_FREEBSD_DRIVER,
1099 0x2018,
1100 "Passthru request timed out!",
1101 req);
1102#else /* TW_OSL_DEBUG */
1103 device_printf((sc)->bus_dev, "Passthru request timed out!\n");
1104#endif /* TW_OSL_DEBUG */
1105 tw_cl_reset_ctlr(&(req->ctlr->ctlr_handle));
1106 }
1107
1108 error = 0;
1109 end_time = tw_osl_get_local_time() + timeout;
1110 continue;
1111 /*
1007 * Should I check here if the timeout happened
1008 * because of yet another reset, and not do a
1009 * second reset?
1010 */
1011 tw_cl_reset_ctlr(&sc->ctlr_handle);
1012 /*
1112 * Don't touch req after a reset. It (and any
1014 * associated data) will already have been
1113 * associated data) will be
1114 * unmapped by the callback.
1115 */
1017 user_buf->driver_pkt.os_status = error;
1018 error = ETIMEDOUT;
1019 goto fw_passthru_err;
1116 }
1117 /*
1118 * Either the request got completed, or we were woken up by a
1119 * signal. Calculate the new timeout, in case it was the latter.
1120 */
1121 timeout = (end_time - tw_osl_get_local_time());
1026 }
1122 } /* End of while loop */
1123
1124 /* If there was a payload, copy it back. */
1125 if ((!error) && (req->length))
1126 if ((error = copyout(req->data, user_buf->pdata,
1127 user_buf->driver_pkt.buffer_length)))
1128 tw_osli_printf(sc, "error = %d",
1129 TW_CL_SEVERITY_ERROR_STRING,
1130 TW_CL_MESSAGE_SOURCE_FREEBSD_DRIVER,
1131 0x2019,
1132 "Could not copyout fw_passthru data_buf",
1133 error);
1134
1135fw_passthru_err:
1040 /*
1041 * Print the failure message. For some reason, on certain OS versions,
1042 * printing this error message during reset hangs the display (although
1043 * the rest of the system is running fine. So, don't print it if the
1044 * failure was due to a reset.
1045 */
1046 if ((error) && (error != TW_CL_ERR_REQ_BUS_RESET))
1047 tw_osli_printf(sc, "error = %d",
1048 TW_CL_SEVERITY_ERROR_STRING,
1049 TW_CL_MESSAGE_SOURCE_FREEBSD_DRIVER,
1050 0x201A,
1051 "Firmware passthru failed!",
1052 error);
1136
1137 if (req_pkt->status == TW_CL_ERR_REQ_BUS_RESET)
1138 error = EBUSY;
1139
1140 user_buf->driver_pkt.os_status = error;
1141 /* Free resources. */
1142 if (req->data)
1143 free(req->data, TW_OSLI_MALLOC_CLASS);
1144 tw_osli_req_q_insert_tail(req, TW_OSLI_FREE_Q);
1145 return(error);
1146}
1147
1148
1149
1150/*
1151 * Function name: tw_osl_complete_passthru
1152 * Description: Called to complete passthru requests.
1153 *
1154 * Input: req_handle -- ptr to request handle
1155 * Output: None
1156 * Return value: None
1157 */
1158TW_VOID
1159tw_osl_complete_passthru(struct tw_cl_req_handle *req_handle)
1160{
1161 struct tw_osli_req_context *req = req_handle->osl_req_ctxt;
1162 struct tw_cl_req_packet *req_pkt =
1163 (struct tw_cl_req_packet *)(&req->req_pkt);
1164 struct twa_softc *sc = req->ctlr;
1165
1166 tw_osli_dbg_dprintf(5, sc, "entered");
1167
1168 if (req->state != TW_OSLI_REQ_STATE_BUSY) {
1169 tw_osli_printf(sc, "request = %p, status = %d",
1170 TW_CL_SEVERITY_ERROR_STRING,
1171 TW_CL_MESSAGE_SOURCE_FREEBSD_DRIVER,
1172 0x201B,
1173 "Unposted command completed!!",
1174 req, req->state);
1175 }
1176
1177 /*
1178 * Remove request from the busy queue. Just mark it complete.
1179 * There's no need to move it into the complete queue as we are
1180 * going to be done with it right now.
1181 */
1182 req->state = TW_OSLI_REQ_STATE_COMPLETE;
1183 tw_osli_req_q_remove_item(req, TW_OSLI_BUSY_Q);
1184
1185 tw_osli_unmap_request(req);
1186
1187 /*
1188 * Don't do a wake up if there was an error even before the request
1189 * was sent down to the Common Layer, and we hadn't gotten an
1190 * EINPROGRESS. The request originator will then be returned an
1191 * error, and he can do the clean-up.
1192 */
1193 if ((req->error_code) && (!(req->flags & TW_OSLI_REQ_FLAGS_IN_PROGRESS)))
1194 return;
1195
1196 if (req->flags & TW_OSLI_REQ_FLAGS_PASSTHRU) {
1197 if (req->flags & TW_OSLI_REQ_FLAGS_SLEEPING) {
1198 /* Wake up the sleeping command originator. */
1199 tw_osli_dbg_dprintf(5, sc,
1200 "Waking up originator of request %p", req);
1201 req->flags &= ~TW_OSLI_REQ_FLAGS_SLEEPING;
1202 wakeup_one(req);
1203 } else {
1204 /*
1205 * If the request completed even before mtx_sleep
1206 * was called, simply return.
1207 */
1208 if (req->flags & TW_OSLI_REQ_FLAGS_MAPPED)
1209 return;
1210
1211 if (req_pkt->status == TW_CL_ERR_REQ_BUS_RESET)
1212 return;
1213
1214 tw_osli_printf(sc, "request = %p",
1215 TW_CL_SEVERITY_ERROR_STRING,
1216 TW_CL_MESSAGE_SOURCE_FREEBSD_DRIVER,
1217 0x201C,
1218 "Passthru callback called, "
1219 "and caller not sleeping",
1220 req);
1221 }
1222 } else {
1223 tw_osli_printf(sc, "request = %p",
1224 TW_CL_SEVERITY_ERROR_STRING,
1225 TW_CL_MESSAGE_SOURCE_FREEBSD_DRIVER,
1226 0x201D,
1227 "Passthru callback called for non-passthru request",
1228 req);
1229 }
1230}
1231
1232
1233
1234/*
1235 * Function name: tw_osli_get_request
1236 * Description: Gets a request pkt from the free queue.
1237 *
1238 * Input: sc -- ptr to OSL internal ctlr context
1239 * Output: None
1240 * Return value: ptr to request pkt -- success
1241 * NULL -- failure
1242 */
1243struct tw_osli_req_context *
1244tw_osli_get_request(struct twa_softc *sc)
1245{
1246 struct tw_osli_req_context *req;
1247
1248 tw_osli_dbg_dprintf(4, sc, "entered");
1249
1250 /* Get a free request packet. */
1251 req = tw_osli_req_q_remove_head(sc, TW_OSLI_FREE_Q);
1252
1253 /* Initialize some fields to their defaults. */
1254 if (req) {
1255 req->req_handle.osl_req_ctxt = NULL;
1256 req->req_handle.cl_req_ctxt = NULL;
1257 req->req_handle.is_io = 0;
1258 req->data = NULL;
1259 req->length = 0;
1260 req->deadline = 0;
1261 req->real_data = NULL;
1262 req->real_length = 0;
1263 req->state = TW_OSLI_REQ_STATE_INIT;/* req being initialized */
1264 req->flags = 0;
1265 req->error_code = 0;
1266 req->orig_req = NULL;
1267
1268 bzero(&(req->req_pkt), sizeof(struct tw_cl_req_packet));
1269
1270 }
1271 return(req);
1272}
1273
1274
1275
1276/*
1277 * Function name: twa_map_load_data_callback
1278 * Description: Callback of bus_dmamap_load for the buffer associated
1279 * with data. Updates the cmd pkt (size/sgl_entries
1280 * fields, as applicable) to reflect the number of sg
1281 * elements.
1282 *
1283 * Input: arg -- ptr to OSL internal request context
1284 * segs -- ptr to a list of segment descriptors
1285 * nsegments--# of segments
1286 * error -- 0 if no errors encountered before callback,
1287 * non-zero if errors were encountered
1288 * Output: None
1289 * Return value: None
1290 */
1291static TW_VOID
1292twa_map_load_data_callback(TW_VOID *arg, bus_dma_segment_t *segs,
1293 TW_INT32 nsegments, TW_INT32 error)
1294{
1295 struct tw_osli_req_context *req =
1296 (struct tw_osli_req_context *)arg;
1297 struct twa_softc *sc = req->ctlr;
1298 struct tw_cl_req_packet *req_pkt = &(req->req_pkt);
1299
1300 tw_osli_dbg_dprintf(10, sc, "entered");
1301
1302 if (error == EINVAL) {
1303 req->error_code = error;
1304 return;
1305 }
1306
1307 /* Mark the request as currently being processed. */
1308 req->state = TW_OSLI_REQ_STATE_BUSY;
1309 /* Move the request into the busy queue. */
1310 tw_osli_req_q_insert_tail(req, TW_OSLI_BUSY_Q);
1311
1312 req->flags |= TW_OSLI_REQ_FLAGS_MAPPED;
1313
1314 if (error == EFBIG) {
1315 req->error_code = error;
1316 goto out;
1317 }
1318
1319 if (req->flags & TW_OSLI_REQ_FLAGS_PASSTHRU) {
1320 struct tw_cl_passthru_req_packet *pt_req;
1321
1322 if (req->flags & TW_OSLI_REQ_FLAGS_DATA_IN)
1323 bus_dmamap_sync(sc->ioctl_tag, sc->ioctl_map,
1324 BUS_DMASYNC_PREREAD);
1325
1326 if (req->flags & TW_OSLI_REQ_FLAGS_DATA_OUT) {
1327 /*
1328 * If we're using an alignment buffer, and we're
1329 * writing data, copy the real data out.
1330 */
1331 if (req->flags & TW_OSLI_REQ_FLAGS_DATA_COPY_NEEDED)
1332 bcopy(req->real_data, req->data, req->real_length);
1333 bus_dmamap_sync(sc->ioctl_tag, sc->ioctl_map,
1334 BUS_DMASYNC_PREWRITE);
1335 }
1336
1337 pt_req = &(req_pkt->gen_req_pkt.pt_req);
1338 pt_req->sg_list = (TW_UINT8 *)segs;
1339 pt_req->sgl_entries += (nsegments - 1);
1340 error = tw_cl_fw_passthru(&(sc->ctlr_handle), req_pkt,
1341 &(req->req_handle));
1342 } else {
1343 struct tw_cl_scsi_req_packet *scsi_req;
1344
1345 if (req->flags & TW_OSLI_REQ_FLAGS_DATA_IN)
1346 bus_dmamap_sync(sc->dma_tag, req->dma_map,
1347 BUS_DMASYNC_PREREAD);
1348
1349 if (req->flags & TW_OSLI_REQ_FLAGS_DATA_OUT) {
1350 /*
1351 * If we're using an alignment buffer, and we're
1352 * writing data, copy the real data out.
1353 */
1354 if (req->flags & TW_OSLI_REQ_FLAGS_DATA_COPY_NEEDED)
1355 bcopy(req->real_data, req->data, req->real_length);
1356 bus_dmamap_sync(sc->dma_tag, req->dma_map,
1357 BUS_DMASYNC_PREWRITE);
1358 }
1359
1360 scsi_req = &(req_pkt->gen_req_pkt.scsi_req);
1361 scsi_req->sg_list = (TW_UINT8 *)segs;
1362 scsi_req->sgl_entries += (nsegments - 1);
1363 error = tw_cl_start_io(&(sc->ctlr_handle), req_pkt,
1364 &(req->req_handle));
1365 }
1366
1367out:
1368 if (error) {
1369 req->error_code = error;
1370 req_pkt->tw_osl_callback(&(req->req_handle));
1371 /*
1372 * If the caller had been returned EINPROGRESS, and he has
1373 * registered a callback for handling completion, the callback
1374 * will never get called because we were unable to submit the
1375 * request. So, free up the request right here.
1376 */
1377 if (req->flags & TW_OSLI_REQ_FLAGS_IN_PROGRESS)
1378 tw_osli_req_q_insert_tail(req, TW_OSLI_FREE_Q);
1379 }
1380}
1381
1382
1383
1384/*
1385 * Function name: twa_map_load_callback
1386 * Description: Callback of bus_dmamap_load for the buffer associated
1387 * with a cmd pkt.
1388 *
1389 * Input: arg -- ptr to variable to hold phys addr
1390 * segs -- ptr to a list of segment descriptors
1391 * nsegments--# of segments
1392 * error -- 0 if no errors encountered before callback,
1393 * non-zero if errors were encountered
1394 * Output: None
1395 * Return value: None
1396 */
1397static TW_VOID
1398twa_map_load_callback(TW_VOID *arg, bus_dma_segment_t *segs,
1399 TW_INT32 nsegments, TW_INT32 error)
1400{
1401 *((bus_addr_t *)arg) = segs[0].ds_addr;
1402}
1403
1404
1405
1406/*
1407 * Function name: tw_osli_map_request
1408 * Description: Maps a cmd pkt and data associated with it, into
1409 * DMA'able memory.
1410 *
1411 * Input: req -- ptr to request pkt
1412 * Output: None
1413 * Return value: 0 -- success
1414 * non-zero-- failure
1415 */
1416TW_INT32
1417tw_osli_map_request(struct tw_osli_req_context *req)
1418{
1419 struct twa_softc *sc = req->ctlr;
1420 TW_INT32 error = 0;
1421
1422 tw_osli_dbg_dprintf(10, sc, "entered");
1423
1424 /* If the command involves data, map that too. */
1425 if (req->data != NULL) {
1426 /*
1427 * It's sufficient for the data pointer to be 4-byte aligned
1428 * to work with 9000. However, if 4-byte aligned addresses
1429 * are passed to bus_dmamap_load, we can get back sg elements
1430 * that are not 512-byte multiples in size. So, we will let
1431 * only those buffers that are 512-byte aligned to pass
1432 * through, and bounce the rest, so as to make sure that we
1433 * always get back sg elements that are 512-byte multiples
1434 * in size.
1435 */
1436 if (((vm_offset_t)req->data % sc->sg_size_factor) ||
1437 (req->length % sc->sg_size_factor)) {
1438 req->flags |= TW_OSLI_REQ_FLAGS_DATA_COPY_NEEDED;
1439 /* Save original data pointer and length. */
1440 req->real_data = req->data;
1441 req->real_length = req->length;
1442 req->length = (req->length +
1443 (sc->sg_size_factor - 1)) &
1444 ~(sc->sg_size_factor - 1);
1445 req->data = malloc(req->length, TW_OSLI_MALLOC_CLASS,
1446 M_NOWAIT);
1447 if (req->data == NULL) {
1448 tw_osli_printf(sc, "error = %d",
1449 TW_CL_SEVERITY_ERROR_STRING,
1450 TW_CL_MESSAGE_SOURCE_FREEBSD_DRIVER,
1451 0x201E,
1452 "Failed to allocate memory "
1453 "for bounce buffer",
1454 ENOMEM);
1455 /* Restore original data pointer and length. */
1456 req->data = req->real_data;
1457 req->length = req->real_length;
1458 return(ENOMEM);
1459 }
1460 }
1461
1462 /*
1463 * Map the data buffer into bus space and build the SG list.
1464 */
1465 if (req->flags & TW_OSLI_REQ_FLAGS_PASSTHRU) {
1466 /* Lock against multiple simultaneous ioctl calls. */
1467 mtx_lock_spin(sc->io_lock);
1468 error = bus_dmamap_load(sc->ioctl_tag, sc->ioctl_map,
1469 req->data, req->length,
1470 twa_map_load_data_callback, req,
1471 BUS_DMA_WAITOK);
1472 mtx_unlock_spin(sc->io_lock);
1473 } else {
1474 /*
1475 * There's only one CAM I/O thread running at a time.
1476 * So, there's no need to hold the io_lock.
1477 */
1478 error = bus_dmamap_load(sc->dma_tag, req->dma_map,
1479 req->data, req->length,
1480 twa_map_load_data_callback, req,
1481 BUS_DMA_WAITOK);
1482 }
1483
1484 if (!error)
1485 error = req->error_code;
1486 else {
1487 if (error == EINPROGRESS) {
1488 /*
1489 * Specifying sc->io_lock as the lockfuncarg
1490 * in ...tag_create should protect the access
1491 * of ...FLAGS_MAPPED from the callback.
1492 */
1493 mtx_lock_spin(sc->io_lock);
1494 if (!(req->flags & TW_OSLI_REQ_FLAGS_MAPPED))
1495 req->flags |= TW_OSLI_REQ_FLAGS_IN_PROGRESS;
1496 tw_osli_disallow_new_requests(sc, &(req->req_handle));
1497 mtx_unlock_spin(sc->io_lock);
1498 error = 0;
1499 } else {
1500 tw_osli_printf(sc, "error = %d",
1501 TW_CL_SEVERITY_ERROR_STRING,
1502 TW_CL_MESSAGE_SOURCE_FREEBSD_DRIVER,
1503 0x9999,
1504 "Failed to map DMA memory "
1505 "for I/O request",
1506 error);
1507 req->flags |= TW_OSLI_REQ_FLAGS_FAILED;
1508 /* Free alignment buffer if it was used. */
1509 if (req->flags &
1510 TW_OSLI_REQ_FLAGS_DATA_COPY_NEEDED) {
1511 free(req->data, TW_OSLI_MALLOC_CLASS);
1512 /*
1513 * Restore original data pointer
1514 * and length.
1515 */
1516 req->data = req->real_data;
1517 req->length = req->real_length;
1518 }
1519 }
1520 }
1521
1522 } else {
1523 /* Mark the request as currently being processed. */
1524 req->state = TW_OSLI_REQ_STATE_BUSY;
1525 /* Move the request into the busy queue. */
1526 tw_osli_req_q_insert_tail(req, TW_OSLI_BUSY_Q);
1527 if (req->flags & TW_OSLI_REQ_FLAGS_PASSTHRU)
1528 error = tw_cl_fw_passthru(&sc->ctlr_handle,
1529 &(req->req_pkt), &(req->req_handle));
1530 else
1531 error = tw_cl_start_io(&sc->ctlr_handle,
1532 &(req->req_pkt), &(req->req_handle));
1533 if (error) {
1534 req->error_code = error;
1535 req->req_pkt.tw_osl_callback(&(req->req_handle));
1536 }
1537 }
1538 return(error);
1539}
1540
1541
1542
1543/*
1544 * Function name: tw_osli_unmap_request
1545 * Description: Undoes the mapping done by tw_osli_map_request.
1546 *
1547 * Input: req -- ptr to request pkt
1548 * Output: None
1549 * Return value: None
1550 */
1551TW_VOID
1552tw_osli_unmap_request(struct tw_osli_req_context *req)
1553{
1554 struct twa_softc *sc = req->ctlr;
1555
1556 tw_osli_dbg_dprintf(10, sc, "entered");
1557
1558 /* If the command involved data, unmap that too. */
1559 if (req->data != NULL) {
1560 if (req->flags & TW_OSLI_REQ_FLAGS_PASSTHRU) {
1561 /* Lock against multiple simultaneous ioctl calls. */
1562 mtx_lock_spin(sc->io_lock);
1563
1564 if (req->flags & TW_OSLI_REQ_FLAGS_DATA_IN) {
1565 bus_dmamap_sync(sc->ioctl_tag,
1566 sc->ioctl_map, BUS_DMASYNC_POSTREAD);
1567
1568 /*
1569 * If we are using a bounce buffer, and we are
1570 * reading data, copy the real data in.
1571 */
1572 if (req->flags & TW_OSLI_REQ_FLAGS_DATA_COPY_NEEDED)
1573 bcopy(req->data, req->real_data,
1574 req->real_length);
1575 }
1576
1577 if (req->flags & TW_OSLI_REQ_FLAGS_DATA_OUT)
1578 bus_dmamap_sync(sc->ioctl_tag, sc->ioctl_map,
1579 BUS_DMASYNC_POSTWRITE);
1580
1581 bus_dmamap_unload(sc->ioctl_tag, sc->ioctl_map);
1582
1583 mtx_unlock_spin(sc->io_lock);
1584 } else {
1585 if (req->flags & TW_OSLI_REQ_FLAGS_DATA_IN) {
1586 bus_dmamap_sync(sc->dma_tag,
1587 req->dma_map, BUS_DMASYNC_POSTREAD);
1588
1589 /*
1590 * If we are using a bounce buffer, and we are
1591 * reading data, copy the real data in.
1592 */
1593 if (req->flags & TW_OSLI_REQ_FLAGS_DATA_COPY_NEEDED)
1594 bcopy(req->data, req->real_data,
1595 req->real_length);
1596 }
1597 if (req->flags & TW_OSLI_REQ_FLAGS_DATA_OUT)
1598 bus_dmamap_sync(sc->dma_tag, req->dma_map,
1599 BUS_DMASYNC_POSTWRITE);
1600
1601 bus_dmamap_unload(sc->dma_tag, req->dma_map);
1602 }
1603 }
1604
1605 /* Free alignment buffer if it was used. */
1606 if (req->flags & TW_OSLI_REQ_FLAGS_DATA_COPY_NEEDED) {
1607 free(req->data, TW_OSLI_MALLOC_CLASS);
1608 /* Restore original data pointer and length. */
1609 req->data = req->real_data;
1610 req->length = req->real_length;
1611 }
1612}
1613
1614
1615
1616#ifdef TW_OSL_DEBUG
1617
1618TW_VOID twa_report_stats(TW_VOID);
1619TW_VOID twa_reset_stats(TW_VOID);
1620TW_VOID tw_osli_print_ctlr_stats(struct twa_softc *sc);
1621TW_VOID twa_print_req_info(struct tw_osli_req_context *req);
1622
1623
1624/*
1625 * Function name: twa_report_stats
1626 * Description: For being called from ddb. Calls functions that print
1627 * OSL and CL internal stats for the controller.
1628 *
1629 * Input: None
1630 * Output: None
1631 * Return value: None
1632 */
1633TW_VOID
1634twa_report_stats(TW_VOID)
1635{
1636 struct twa_softc *sc;
1637 TW_INT32 i;
1638
1639 for (i = 0; (sc = devclass_get_softc(twa_devclass, i)) != NULL; i++) {
1640 tw_osli_print_ctlr_stats(sc);
1641 tw_cl_print_ctlr_stats(&sc->ctlr_handle);
1642 }
1643}
1644
1645
1646
1647/*
1648 * Function name: tw_osli_print_ctlr_stats
1649 * Description: For being called from ddb. Prints OSL controller stats
1650 *
1651 * Input: sc -- ptr to OSL internal controller context
1652 * Output: None
1653 * Return value: None
1654 */
1655TW_VOID
1656tw_osli_print_ctlr_stats(struct twa_softc *sc)
1657{
1658 twa_printf(sc, "osl_ctlr_ctxt = %p\n", sc);
1659 twa_printf(sc, "OSLq type current max\n");
1660 twa_printf(sc, "free %04d %04d\n",
1661 sc->q_stats[TW_OSLI_FREE_Q].cur_len,
1662 sc->q_stats[TW_OSLI_FREE_Q].max_len);
1663 twa_printf(sc, "busy %04d %04d\n",
1664 sc->q_stats[TW_OSLI_BUSY_Q].cur_len,
1665 sc->q_stats[TW_OSLI_BUSY_Q].max_len);
1666}
1667
1668
1669
1670/*
1671 * Function name: twa_print_req_info
1672 * Description: For being called from ddb. Calls functions that print
1673 * OSL and CL internal details for the request.
1674 *
1675 * Input: req -- ptr to OSL internal request context
1676 * Output: None
1677 * Return value: None
1678 */
1679TW_VOID
1680twa_print_req_info(struct tw_osli_req_context *req)
1681{
1682 struct twa_softc *sc = req->ctlr;
1683
1684 twa_printf(sc, "OSL details for request:\n");
1685 twa_printf(sc, "osl_req_ctxt = %p, cl_req_ctxt = %p\n"
1686 "data = %p, length = 0x%x, real_data = %p, real_length = 0x%x\n"
1687 "state = 0x%x, flags = 0x%x, error = 0x%x, orig_req = %p\n"
1688 "next_req = %p, prev_req = %p, dma_map = %p\n",
1689 req->req_handle.osl_req_ctxt, req->req_handle.cl_req_ctxt,
1690 req->data, req->length, req->real_data, req->real_length,
1691 req->state, req->flags, req->error_code, req->orig_req,
1692 req->link.next, req->link.prev, req->dma_map);
1693 tw_cl_print_req_info(&(req->req_handle));
1694}
1695
1696
1697
1698/*
1699 * Function name: twa_reset_stats
1700 * Description: For being called from ddb.
1701 * Resets some OSL controller stats.
1702 *
1703 * Input: None
1704 * Output: None
1705 * Return value: None
1706 */
1707TW_VOID
1708twa_reset_stats(TW_VOID)
1709{
1710 struct twa_softc *sc;
1711 TW_INT32 i;
1712
1713 for (i = 0; (sc = devclass_get_softc(twa_devclass, i)) != NULL; i++) {
1714 sc->q_stats[TW_OSLI_FREE_Q].max_len = 0;
1715 sc->q_stats[TW_OSLI_BUSY_Q].max_len = 0;
1716 tw_cl_reset_stats(&sc->ctlr_handle);
1717 }
1718}
1719
1720#endif /* TW_OSL_DEBUG */