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