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