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