Merge rev. 1.7 of FreeBSD's src/sys/dev/twa/twa_freebsd.c.
[dragonfly.git] / sys / dev / raid / twa / twa_freebsd.c
1 /*-
2  * Copyright (c) 2003-04 3ware, Inc.
3  * Copyright (c) 2000 Michael Smith
4  * Copyright (c) 2000 BSDi
5  * All rights reserved.
6  *
7  * Redistribution and use in source and binary forms, with or without
8  * modification, are permitted provided that the following conditions
9  * are met:
10  * 1. Redistributions of source code must retain the above copyright
11  *    notice, this list of conditions and the following disclaimer.
12  * 2. Redistributions in binary form must reproduce the above copyright
13  *    notice, this list of conditions and the following disclaimer in the
14  *    documentation and/or other materials provided with the distribution.
15  *
16  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
17  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
18  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
19  * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
20  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
21  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
22  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
23  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
24  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
25  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
26  * SUCH DAMAGE.
27  *
28  *      $FreeBSD$
29  * $DragonFly: src/sys/dev/raid/twa/twa_freebsd.c,v 1.4 2004/06/25 03:37:03 hmp Exp $
30  */
31
32 /*
33  * 3ware driver for 9000 series storage controllers.
34  *
35  * Author: Vinod Kashyap
36  */
37
38
39 #include "twa_includes.h"
40
41 static void     twa_setup_data_dmamap(void *arg, bus_dma_segment_t *segs,
42                                                 int nsegments, int error);
43 static void     twa_setup_request_dmamap(void *arg, bus_dma_segment_t *segs,
44                                                 int nsegments, int error);
45
46 MALLOC_DEFINE(TWA_MALLOC_CLASS, "twa commands", "twa commands");
47
48
49 static  d_open_t                twa_open;
50 static  d_close_t               twa_close;
51 static  d_ioctl_t               twa_ioctl_wrapper;
52
53 static struct cdevsw twa_cdevsw = {
54         "twa",
55         TWA_CDEV_MAJOR,
56         0,
57         NULL,
58         0,
59         twa_open,
60         twa_close,
61         noread,
62         nowrite,
63         twa_ioctl_wrapper,
64         nopoll,
65         nommap,
66         nostrategy,
67         nodump,
68         nopsize,
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 int
88 twa_open(dev_t dev, int flags, int fmt, d_thread_t *proc)
89 {
90         int                     unit = minor(dev);
91         struct twa_softc        *sc = devclass_get_softc(twa_devclass, unit);
92
93         sc->twa_state |= TWA_STATE_OPEN;
94         return(0);
95 }
96
97
98
99 /*
100  * Function name:       twa_close
101  * Description:         Called when the controller is closed.
102  *                      Simply marks the controller as not open.
103  *
104  * Input:               dev     -- control device corresponding to the ctlr
105  *                      flags   -- mode of corresponding open
106  *                      fmt     -- device type (character/block etc.)
107  *                      proc    -- current process
108  * Output:              None
109  * Return value:        0       -- success
110  *                      non-zero-- failure
111  */
112 static int
113 twa_close(dev_t dev, int flags, int fmt, d_thread_t *proc)
114 {
115         int                     unit = minor(dev);
116         struct twa_softc        *sc = devclass_get_softc(twa_devclass, unit);
117
118         sc->twa_state &= ~TWA_STATE_OPEN;
119         return(0);
120 }
121
122
123
124 /*
125  * Function name:       twa_ioctl_wrapper
126  * Description:         Called when an ioctl is posted to the controller.
127  *                      Simply calls the ioctl handler.
128  *
129  * Input:               dev     -- control device corresponding to the ctlr
130  *                      cmd     -- ioctl cmd
131  *                      buf     -- ptr to buffer in kernel memory, which is
132  *                                 a copy of the input buffer in user-space
133  *                      flags   -- mode of corresponding open
134  *                      proc    -- current process
135  * Output:              buf     -- ptr to buffer in kernel memory, which will
136  *                                 be copied to the output buffer in user-space
137  * Return value:        0       -- success
138  *                      non-zero-- failure
139  */
140 static int
141 twa_ioctl_wrapper(dev_t dev, u_long cmd, caddr_t buf,
142                                         int flags, d_thread_t *proc)
143 {
144         struct twa_softc        *sc = (struct twa_softc *)(dev->si_drv1);
145
146         return(twa_ioctl(sc, cmd, buf));
147 }
148
149
150
151 static int      twa_probe (device_t dev);
152 static int      twa_attach (device_t dev);
153 static void     twa_free (struct twa_softc *sc);
154 static int      twa_detach (device_t dev);
155 static int      twa_shutdown (device_t dev);
156 static int      twa_suspend (device_t dev);
157 static int      twa_resume (device_t dev);
158 static void     twa_pci_intr(void *arg);
159 static void     twa_intrhook (void *arg);
160
161 static device_method_t  twa_methods[] = {
162         /* Device interface */
163         DEVMETHOD(device_probe,         twa_probe),
164         DEVMETHOD(device_attach,        twa_attach),
165         DEVMETHOD(device_detach,        twa_detach),
166         DEVMETHOD(device_shutdown,      twa_shutdown),
167         DEVMETHOD(device_suspend,       twa_suspend),
168         DEVMETHOD(device_resume,        twa_resume),
169
170         DEVMETHOD(bus_print_child,      bus_generic_print_child),
171         DEVMETHOD(bus_driver_added,     bus_generic_driver_added),
172         {0, 0}
173 };
174
175 static driver_t twa_pci_driver = {
176         "twa",
177         twa_methods,
178         sizeof(struct twa_softc)
179 };
180
181 DRIVER_MODULE(twa, pci, twa_pci_driver, twa_devclass, 0, 0);
182
183
184
185 /*
186  * Function name:       twa_probe
187  * Description:         Called at driver load time.  Claims 9000 ctlrs.
188  *
189  * Input:               dev     -- bus device corresponding to the ctlr
190  * Output:              None
191  * Return value:        <= 0    -- success
192  *                      > 0     -- failure
193  */
194 static int
195 twa_probe(device_t dev)
196 {
197         static u_int8_t first_ctlr = 1;
198
199         twa_dbg_print(3, "entered");
200
201         if ((pci_get_vendor(dev) == TWA_VENDOR_ID) &&
202                         (pci_get_device(dev) == TWA_DEVICE_ID_9K)) {
203                 device_set_desc(dev, TWA_DEVICE_NAME);
204                 /* Print the driver version only once. */
205                 if (first_ctlr) {
206                         printf("3ware device driver for 9000 series storage controllers, version: %s\n",
207                                         TWA_DRIVER_VERSION_STRING);
208                         first_ctlr = 0;
209                 }
210                 return(0);
211         }
212         return(ENXIO);
213 }
214
215
216
217 /*
218  * Function name:       twa_attach
219  * Description:         Allocates pci resources; updates sc; adds a node to the
220  *                      sysctl tree to expose the driver version; makes calls
221  *                      to initialize ctlr, and to attach to CAM.
222  *
223  * Input:               dev     -- bus device corresponding to the ctlr
224  * Output:              None
225  * Return value:        0       -- success
226  *                      non-zero-- failure
227  */
228 static int
229 twa_attach(device_t dev)
230 {
231         struct twa_softc        *sc = device_get_softc(dev);
232         u_int32_t               command;
233         int                     res_id;
234         int                     error;
235         dev_t                   xdev;
236
237         twa_dbg_dprint_enter(3, sc);
238
239         /* Initialize the softc structure. */
240         sc->twa_bus_dev = dev;
241
242         sysctl_ctx_init(&sc->twa_sysctl_ctx);
243         sc->twa_sysctl_tree = SYSCTL_ADD_NODE(&sc->twa_sysctl_ctx,
244                                 SYSCTL_STATIC_CHILDREN(_hw), OID_AUTO,
245                                 device_get_nameunit(dev), CTLFLAG_RD, 0, "");
246         if (sc->twa_sysctl_tree == NULL) {
247                 twa_printf(sc, "Cannot add sysctl tree node.\n");
248                 return(ENXIO);
249         }
250         SYSCTL_ADD_STRING(&sc->twa_sysctl_ctx, SYSCTL_CHILDREN(sc->twa_sysctl_tree),
251                                 OID_AUTO, "driver_version", CTLFLAG_RD,
252                                 TWA_DRIVER_VERSION_STRING, 0, "TWA driver version");
253
254         /* Make sure we are going to be able to talk to this board. */
255         command = pci_read_config(dev, PCIR_COMMAND, 2);
256         if ((command & PCIM_CMD_PORTEN) == 0) {
257                 twa_printf(sc, "Register window not available.\n");
258                 return(ENXIO);
259         }
260         
261         /* Force the busmaster enable bit on, in case the BIOS forgot. */
262         command |= PCIM_CMD_BUSMASTEREN;
263         pci_write_config(dev, PCIR_COMMAND, command, 2);
264
265         /* Allocate the PCI register window. */
266         res_id = TWA_IO_CONFIG_REG;
267         if ((sc->twa_io_res = bus_alloc_resource(dev, SYS_RES_IOPORT, &res_id,
268                                         0, ~0, 1, RF_ACTIVE)) == NULL) {
269                 twa_printf(sc, "can't allocate register window.\n");
270                 twa_free(sc);
271                 return(ENXIO);
272         }
273         sc->twa_bus_tag = rman_get_bustag(sc->twa_io_res);
274         sc->twa_bus_handle = rman_get_bushandle(sc->twa_io_res);
275
276         /* Allocate and connect our interrupt. */
277         res_id = 0;
278         if ((sc->twa_irq_res = bus_alloc_resource(sc->twa_bus_dev, SYS_RES_IRQ,
279                                         &res_id, 0, ~0, 1,
280                                         RF_SHAREABLE | RF_ACTIVE)) == NULL) {
281                 twa_printf(sc, "Can't allocate interrupt.\n");
282                 twa_free(sc);
283                 return(ENXIO);
284         }
285         if (bus_setup_intr(sc->twa_bus_dev, sc->twa_irq_res, INTR_TYPE_CAM,
286                                 twa_pci_intr, sc, &sc->twa_intr_handle)) {
287                 twa_printf(sc, "Can't set up interrupt.\n");
288                 twa_free(sc);
289                 return(ENXIO);
290         }
291
292         /* Initialize the driver for this controller. */
293         if ((error = twa_setup(sc))) {
294                 twa_free(sc);
295                 return(error);
296         }
297
298         /* Print some information about the controller and configuration. */
299         twa_describe_controller(sc);
300
301         /* Create the control device. */
302         cdevsw_add(&twa_cdevsw, -1, device_get_unit(sc->twa_bus_dev));
303         xdev = make_dev(&twa_cdevsw, device_get_unit(sc->twa_bus_dev),
304                         UID_ROOT, GID_OPERATOR, S_IRUSR | S_IWUSR,
305                         "twa%d", device_get_unit(sc->twa_bus_dev));
306         xdev->si_drv1 = sc;
307
308         /*
309          * Schedule ourselves to bring the controller up once interrupts are
310          * available.  This isn't strictly necessary, since we disable
311          * interrupts while probing the controller, but it is more in keeping
312          * with common practice for other disk devices.
313          */
314         sc->twa_ich.ich_func = twa_intrhook;
315         sc->twa_ich.ich_arg = sc;
316         if (config_intrhook_establish(&sc->twa_ich) != 0) {
317                 twa_printf(sc, "Can't establish configuration hook.\n");
318                 twa_free(sc);
319                 return(ENXIO);
320         }
321
322         if ((error = twa_cam_setup(sc))) {
323                 twa_free(sc);
324                 return(error);
325         }
326         return(0);
327 }
328
329
330
331 /*
332  * Function name:       twa_free
333  * Description:         Performs clean-up at the time of going down.
334  *
335  * Input:               sc      -- ptr to per ctlr structure
336  * Output:              None
337  * Return value:        None
338  */
339 static void
340 twa_free(struct twa_softc *sc)
341 {
342         struct twa_request      *tr;
343
344         twa_dbg_dprint_enter(3, sc);
345
346         /* Detach from CAM */
347         twa_cam_detach(sc);
348
349         /* Destroy dma handles. */
350
351         bus_dmamap_unload(sc->twa_dma_tag, sc->twa_cmd_map); 
352         while ((tr = twa_dequeue_free(sc)) != NULL)
353                 bus_dmamap_destroy(sc->twa_dma_tag, tr->tr_dma_map);
354
355         /* Free all memory allocated so far. */
356         if (sc->twa_req_buf)
357                 free(sc->twa_req_buf, TWA_MALLOC_CLASS);
358         if (sc->twa_cmd_pkt_buf)
359                 bus_dmamem_free(sc->twa_dma_tag, sc->twa_cmd_pkt_buf,
360                                         sc->twa_cmd_map);
361         if (sc->twa_aen_queue[0])
362                 free (sc->twa_aen_queue[0], M_DEVBUF);
363
364         /* Destroy the data-transfer DMA tag. */
365         if (sc->twa_dma_tag)
366                 bus_dma_tag_destroy(sc->twa_dma_tag);
367
368         /* Disconnect the interrupt handler. */
369         if (sc->twa_intr_handle)
370                 bus_teardown_intr(sc->twa_bus_dev, sc->twa_irq_res,
371                                         sc->twa_intr_handle);
372         if (sc->twa_irq_res != NULL)
373                 bus_release_resource(sc->twa_bus_dev, SYS_RES_IRQ,
374                                         0, sc->twa_irq_res);
375
376         /* Release the register window mapping. */
377         if (sc->twa_io_res != NULL)
378                 bus_release_resource(sc->twa_bus_dev, SYS_RES_IOPORT,
379                                         TWA_IO_CONFIG_REG, sc->twa_io_res);
380
381         cdevsw_remove(&twa_cdevsw, -1, device_get_unit(sc->twa_bus_dev));
382
383         sysctl_ctx_free(&sc->twa_sysctl_ctx);
384 }
385
386
387
388 /*
389  * Function name:       twa_detach
390  * Description:         Called when the controller is being detached from
391  *                      the pci bus.
392  *
393  * Input:               dev     -- bus device corresponding to the ctlr
394  * Output:              None
395  * Return value:        0       -- success
396  *                      non-zero-- failure
397  */
398 static int
399 twa_detach(device_t dev)
400 {
401         struct twa_softc        *sc = device_get_softc(dev);
402         int                     s;
403         int                     error;
404
405         twa_dbg_dprint_enter(3, sc);
406
407         error = EBUSY;
408         s = splcam();
409         if (sc->twa_state & TWA_STATE_OPEN)
410                 goto out;
411
412         /* Shut the controller down. */
413         if ((error = twa_shutdown(dev)))
414                 goto out;
415
416         /* Free all resources associated with this controller. */
417         twa_free(sc);
418         error = 0;
419
420 out:
421         splx(s);
422         return(error);
423 }
424
425
426
427 /*
428  * Function name:       twa_shutdown
429  * Description:         Called at unload/shutdown time.  Lets the controller
430  *                      know that we are going down.
431  *
432  * Input:               dev     -- bus device corresponding to the ctlr
433  * Output:              None
434  * Return value:        0       -- success
435  *                      non-zero-- failure
436  */
437 static int
438 twa_shutdown(device_t dev)
439 {
440         struct twa_softc        *sc = device_get_softc(dev);
441         int                     s;
442         int                     error = 0;
443
444         twa_dbg_dprint_enter(3, sc);
445
446         s = splcam();
447
448         /* Disconnect from the controller. */
449         error = twa_deinit_ctlr(sc);
450
451         splx(s);
452         return(error);
453 }
454
455
456
457 /*
458  * Function name:       twa_suspend
459  * Description:         Called to suspend I/O before hot-swapping PCI ctlrs.
460  *                      Doesn't do much as of now.
461  *
462  * Input:               dev     -- bus device corresponding to the ctlr
463  * Output:              None
464  * Return value:        0       -- success
465  *                      non-zero-- failure
466  */
467 static int
468 twa_suspend(device_t dev)
469 {
470         struct twa_softc        *sc = device_get_softc(dev);
471         int                     s;
472
473         twa_dbg_dprint_enter(3, sc);
474
475         s = splcam();
476         sc->twa_state |= TWA_STATE_SUSPEND;
477     
478         twa_disable_interrupts(sc);
479         splx(s);
480
481         return(1);
482 }
483
484
485
486 /*
487  * Function name:       twa_resume
488  * Description:         Called to resume I/O after hot-swapping PCI ctlrs.
489  *                      Doesn't do much as of now.
490  *
491  * Input:               dev     -- bus device corresponding to the ctlr
492  * Output:              None
493  * Return value:        0       -- success
494  *                      non-zero-- failure
495  */
496 static int
497 twa_resume(device_t dev)
498 {
499         struct twa_softc        *sc = device_get_softc(dev);
500
501         twa_dbg_dprint_enter(3, sc);
502
503         sc->twa_state &= ~TWA_STATE_SUSPEND;
504         twa_enable_interrupts(sc);
505
506         return(1);
507 }
508
509
510
511 /*
512  * Function name:       twa_pci_intr
513  * Description:         Interrupt handler.  Wrapper for twa_interrupt.
514  *
515  * Input:               arg     -- ptr to per ctlr structure
516  * Output:              None
517  * Return value:        None
518  */
519 static void
520 twa_pci_intr(void *arg)
521 {
522         struct twa_softc        *sc = (struct twa_softc *)arg;
523
524         twa_interrupt(sc);
525 }
526
527
528
529 /*
530  * Function name:       twa_intrhook
531  * Description:         Callback for us to enable interrupts.
532  *
533  * Input:               arg     -- ptr to per ctlr structure
534  * Output:              None
535  * Return value:        None
536  */
537 static void
538 twa_intrhook(void *arg)
539 {
540         struct twa_softc        *sc = (struct twa_softc *)arg;
541
542         twa_dbg_dprint(4, sc, "twa_intrhook Entered");
543
544         /* Pull ourselves off the intrhook chain. */
545         config_intrhook_disestablish(&sc->twa_ich);
546
547         /* Enable interrupts. */
548         twa_enable_interrupts(sc);
549 }
550
551
552
553 /*
554  * Function name:       twa_write_pci_config
555  * Description:         Writes to the PCI config space.
556  *
557  * Input:               sc      -- ptr to per ctlr structure
558  *                      value   -- value to be written
559  *                      size    -- # of bytes to be written
560  * Output:              None
561  * Return value:        None
562  */
563 void
564 twa_write_pci_config(struct twa_softc *sc, u_int32_t value, int size)
565 {
566         pci_write_config(sc->twa_bus_dev, PCIR_STATUS, value, size);
567 }
568
569
570
571 /*
572  * Function name:       twa_alloc_req_pkts
573  * Description:         Allocates memory for, and initializes request pkts,
574  *                      and queues them in the free queue.
575  *
576  * Input:               sc      -- ptr to per ctlr structure
577  *                      num_reqs-- # of request pkts to allocate and initialize.
578  * Output:              None
579  * Return value:        0       -- success
580  *                      non-zero-- failure
581  */
582 int
583 twa_alloc_req_pkts(struct twa_softc *sc, int num_reqs)
584 {
585         struct twa_request      *tr;
586         int                     i;
587
588         sc->twa_req_buf = malloc(num_reqs * sizeof(struct twa_request),
589                                         TWA_MALLOC_CLASS, M_INTWAIT);
590
591         /* Allocate the bus DMA tag appropriate for PCI. */
592         if (bus_dma_tag_create(NULL,                    /* parent */
593                                 TWA_ALIGNMENT,          /* alignment */
594                                 0,                      /* boundary */
595                                 BUS_SPACE_MAXADDR,      /* lowaddr */
596                                 BUS_SPACE_MAXADDR,      /* highaddr */
597                                 NULL, NULL,             /* filter, filterarg */
598                                 TWA_Q_LENGTH *
599                                 (sizeof(struct twa_command_packet)),/* maxsize */
600                                 TWA_MAX_SG_ELEMENTS,    /* nsegments */
601                                 BUS_SPACE_MAXSIZE_32BIT,/* maxsegsize */
602                                 BUS_DMA_ALLOCNOW,       /* flags */
603                                 &sc->twa_dma_tag        /* tag */)) {
604                 twa_printf(sc, "Can't allocate DMA tag.\n");
605                 return(ENOMEM);
606         }
607
608         /* Allocate memory for cmd pkts. */
609         if (bus_dmamem_alloc(sc->twa_dma_tag,
610                                 (void *)(&(sc->twa_cmd_pkt_buf)),
611                                 BUS_DMA_WAITOK, &(sc->twa_cmd_map)))
612                 return(ENOMEM);
613
614         bus_dmamap_load(sc->twa_dma_tag, sc->twa_cmd_map,
615                                 sc->twa_cmd_pkt_buf,
616                                 num_reqs * sizeof(struct twa_command_packet),
617                                 twa_setup_request_dmamap, sc, 0);
618         bzero(sc->twa_req_buf, num_reqs * sizeof(struct twa_request));
619         bzero(sc->twa_cmd_pkt_buf,
620                         num_reqs * sizeof(struct twa_command_packet));
621
622         for (i = 0; i < num_reqs; i++) {
623                 tr = &(sc->twa_req_buf[i]);
624                 tr->tr_command = &(sc->twa_cmd_pkt_buf[i]);
625                 tr->tr_cmd_phys = sc->twa_cmd_pkt_phys +
626                                         (i * sizeof(struct twa_command_packet));
627                 tr->tr_request_id = i;
628                 tr->tr_sc = sc;
629                 sc->twa_lookup[i] = tr;
630
631                 /*
632                  * Create a map for data buffers.  maxsize (256 * 1024) used in
633                  * bus_dma_tag_create above should suffice the bounce page needs
634                  * for data buffers, since the max I/O size we support is 128KB.
635                  * If we supported I/O's bigger than 256KB, we would have to
636                  * create a second dma_tag, with the appropriate maxsize.
637                  */
638                 if (bus_dmamap_create(sc->twa_dma_tag, 0,
639                                                 &tr->tr_dma_map))
640                         return(ENOMEM);
641
642                 /* Insert request into the free queue. */
643                 twa_release_request(tr);
644         }
645         return(0);
646 }
647
648
649
650 /*
651  * Function name:       twa_fillin_sgl
652  * Description:         Fills in the scatter/gather list.
653  *
654  * Input:               sgl     -- ptr to sg list
655  *                      segs    -- ptr to fill the sg list from
656  *                      nsegments--# of segments
657  * Output:              None
658  * Return value:        None
659  */
660 static void
661 twa_fillin_sgl(struct twa_sg *sgl, bus_dma_segment_t *segs, int nsegments)
662 {
663         int     i;
664
665         for (i = 0; i < nsegments; i++) {
666                 sgl[i].address = segs[i].ds_addr;
667                 sgl[i].length = segs[i].ds_len;
668         }
669 }
670
671
672
673 /*
674  * Function name:       twa_setup_data_dmamap
675  * Description:         Callback of bus_dmamap_load for the buffer associated
676  *                      with data.  Updates the cmd pkt (size/sgl_entries
677  *                      fields, as applicable) to reflect the number of sg
678  *                      elements.
679  *
680  * Input:               arg     -- ptr to request pkt
681  *                      segs    -- ptr to a list of segment descriptors
682  *                      nsegments--# of segments
683  *                      error   -- 0 if no errors encountered before callback,
684  *                                 non-zero if errors were encountered
685  * Output:              None
686  * Return value:        None
687  */
688 static void
689 twa_setup_data_dmamap(void *arg, bus_dma_segment_t *segs,
690                                         int nsegments, int error)
691 {
692         struct twa_request              *tr = (struct twa_request *)arg;
693         struct twa_command_packet       *cmdpkt = tr->tr_command;
694         struct twa_command_9k           *cmd9k;
695         union twa_command_7k            *cmd7k;
696         u_int8_t                        sgl_offset;
697
698         twa_dbg_dprint_enter(10, tr->tr_sc);
699
700         if ((tr->tr_flags & TWA_CMD_IN_PROGRESS) &&
701                         (tr->tr_cmd_pkt_type & TWA_CMD_PKT_TYPE_EXTERNAL))
702                 twa_allow_new_requests(tr->tr_sc, (void *)(tr->tr_private));
703
704         if (error == EFBIG) {
705                 tr->tr_error = error;
706                 goto out;
707         }
708
709         if (tr->tr_cmd_pkt_type & TWA_CMD_PKT_TYPE_9K) {
710                 cmd9k = &(cmdpkt->command.cmd_pkt_9k);
711                 twa_fillin_sgl(&(cmd9k->sg_list[0]), segs, nsegments);
712                 cmd9k->sgl_entries += nsegments - 1;
713         } else {
714                 /* It's a 7000 command packet. */
715                 cmd7k = &(cmdpkt->command.cmd_pkt_7k);
716                 if ((sgl_offset = cmdpkt->command.cmd_pkt_7k.generic.sgl_offset))
717                         twa_fillin_sgl((struct twa_sg *)
718                                         (((u_int32_t *)cmd7k) + sgl_offset),
719                                         segs, nsegments);
720                 /* Modify the size field, based on sg address size. */
721                 cmd7k->generic.size += 
722                                 ((TWA_64BIT_ADDRESSES ? 3 : 2) * nsegments);
723         }
724
725         if (tr->tr_flags & TWA_CMD_DATA_IN)
726                 bus_dmamap_sync(tr->tr_sc->twa_dma_tag, tr->tr_dma_map,
727                                                         BUS_DMASYNC_PREREAD);
728         if (tr->tr_flags & TWA_CMD_DATA_OUT) {
729                 /* 
730                  * If we're using an alignment buffer, and we're
731                  * writing data, copy the real data out.
732                  */
733                 if (tr->tr_flags & TWA_CMD_DATA_COPY_NEEDED)
734                         bcopy(tr->tr_real_data, tr->tr_data, tr->tr_real_length);
735                 bus_dmamap_sync(tr->tr_sc->twa_dma_tag, tr->tr_dma_map,
736                                                 BUS_DMASYNC_PREWRITE);
737         }
738         error = twa_submit_io(tr);
739
740 out:
741         if (error) {
742                 twa_unmap_request(tr);
743                 /*
744                  * If the caller had been returned EINPROGRESS, and he has
745                  * registered a callback for handling completion, the callback
746                  * will never get called because we were unable to submit the
747                  * request.  So, free up the request right here.
748                  */
749                 if ((tr->tr_flags & TWA_CMD_IN_PROGRESS) && (tr->tr_callback))
750                         twa_release_request(tr);
751         }
752 }
753
754
755
756 /*
757  * Function name:       twa_setup_request_dmamap
758  * Description:         Callback of bus_dmamap_load for the buffer associated
759  *                      with a cmd pkt.
760  *
761  * Input:               arg     -- ptr to request pkt
762  *                      segs    -- ptr to a list of segment descriptors
763  *                      nsegments--# of segments
764  *                      error   -- 0 if no errors encountered before callback,
765  *                                 non-zero if errors were encountered
766  * Output:              None
767  * Return value:        None
768  */
769 static void
770 twa_setup_request_dmamap(void *arg, bus_dma_segment_t *segs,
771                                                 int nsegments, int error)
772 {
773         struct twa_softc        *sc = (struct twa_softc *)arg;
774
775         twa_dbg_dprint_enter(10, sc);
776
777         sc->twa_cmd_pkt_phys = segs[0].ds_addr;
778 }
779
780
781
782 /*
783  * Function name:       twa_map_request
784  * Description:         Maps a cmd pkt and data associated with it, into
785  *                      DMA'able memory.
786  *
787  * Input:               tr      -- ptr to request pkt
788  * Output:              None
789  * Return value:        0       -- success
790  *                      non-zero-- failure
791  */
792 int
793 twa_map_request(struct twa_request *tr)
794 {
795         struct twa_softc        *sc = tr->tr_sc;
796         int                     error = 0;
797
798         twa_dbg_dprint_enter(10, sc);
799
800         /* If the command involves data, map that too. */
801         if (tr->tr_data != NULL) {
802                 /*
803                  * It's sufficient for the data pointer to be 4-byte aligned
804                  * to work with 9000.  However, if 4-byte aligned addresses
805                  * are passed to bus_dmamap_load, we can get back sg elements
806                  * that are not 512-byte multiples in size.  So, we will let
807                  * only those buffers that are 512-byte aligned to pass
808                  * through, and bounce the rest, so as to make sure that we
809                  * always get back sg elements that are 512-byte multiples
810                  * in size.
811                  */
812                 if (((vm_offset_t)tr->tr_data % 512) || (tr->tr_length % 512)) {
813                         tr->tr_flags |= TWA_CMD_DATA_COPY_NEEDED;
814                         tr->tr_real_data = tr->tr_data; /* save original data pointer */
815                         tr->tr_real_length = tr->tr_length; /* save original data length */
816                         tr->tr_length = (tr->tr_length + 511) & ~511;
817                         tr->tr_data = malloc(tr->tr_length, TWA_MALLOC_CLASS, M_INTWAIT);
818                 }
819         
820                 /*
821                  * Map the data buffer into bus space and build the s/g list.
822                  */
823                 if ((error = bus_dmamap_load(sc->twa_dma_tag, tr->tr_dma_map,
824                                         tr->tr_data, tr->tr_length, 
825                                         twa_setup_data_dmamap, tr,
826                                         BUS_DMA_WAITOK))) {
827                         if (error == EINPROGRESS) {
828                                 tr->tr_flags |= TWA_CMD_IN_PROGRESS;
829                                 if (tr->tr_cmd_pkt_type & TWA_CMD_PKT_TYPE_EXTERNAL)
830                                         twa_disallow_new_requests(sc);
831                                 error = 0;
832                         } else {
833                                 /* Free alignment buffer if it was used. */
834                                 if (tr->tr_flags & TWA_CMD_DATA_COPY_NEEDED) {
835                                         free(tr->tr_data, TWA_MALLOC_CLASS);
836                                         tr->tr_data = tr->tr_real_data; /* restore 'real' data pointer */
837                                         tr->tr_length = tr->tr_real_length;/* restore 'real' data length */
838                                 }
839                         }
840                 } else
841                         error = tr->tr_error;
842
843         } else
844                 if ((error = twa_submit_io(tr)))
845                         twa_unmap_request(tr);
846
847         return(error);
848 }
849
850
851
852 /*
853  * Function name:       twa_unmap_request
854  * Description:         Undoes the mapping done by twa_map_request.
855  *
856  * Input:               tr      -- ptr to request pkt
857  * Output:              None
858  * Return value:        None
859  */
860 void
861 twa_unmap_request(struct twa_request *tr)
862 {
863         struct twa_softc        *sc = tr->tr_sc;
864         u_int8_t                cmd_status;
865
866         twa_dbg_dprint_enter(10, sc);
867
868         /* If the command involved data, unmap that too. */
869         if (tr->tr_data != NULL) {
870                 if (tr->tr_cmd_pkt_type & TWA_CMD_PKT_TYPE_9K)
871                         cmd_status = tr->tr_command->command.cmd_pkt_9k.status;
872                 else
873                         cmd_status = tr->tr_command->command.cmd_pkt_7k.generic.status;
874
875                 if (tr->tr_flags & TWA_CMD_DATA_IN) {
876                         bus_dmamap_sync(sc->twa_dma_tag,
877                                         tr->tr_dma_map, BUS_DMASYNC_POSTREAD);
878
879                         /* 
880                          * If we are using a bounce buffer, and we are reading
881                          * data, copy the real data in.
882                          */
883                         if (tr->tr_flags & TWA_CMD_DATA_COPY_NEEDED)
884                                 if (cmd_status == 0)
885                                         bcopy(tr->tr_data, tr->tr_real_data,
886                                                         tr->tr_real_length);
887                 }
888                 if (tr->tr_flags & TWA_CMD_DATA_OUT)
889                         bus_dmamap_sync(sc->twa_dma_tag, tr->tr_dma_map,
890                                                         BUS_DMASYNC_POSTWRITE);
891
892                 bus_dmamap_unload(sc->twa_dma_tag, tr->tr_dma_map); 
893         }
894
895         /* Free alignment buffer if it was used. */
896         if (tr->tr_flags & TWA_CMD_DATA_COPY_NEEDED) {
897                 free(tr->tr_data, TWA_MALLOC_CLASS);
898                 tr->tr_data = tr->tr_real_data; /* restore 'real' data pointer */
899                 tr->tr_length = tr->tr_real_length;/* restore 'real' data length */
900         }
901 }
902
903
904
905 #ifdef TWA_DEBUG
906 void    twa_report(void);
907 void    twa_reset_stats(void);
908 void    twa_print_request(struct twa_request *tr, int req_type);
909
910
911
912 /*
913  * Function name:       twa_report
914  * Description:         For being called from ddb.  Prints controller stats,
915  *                      and requests, if any, that are in the wrong queue.
916  *
917  * Input:               None
918  * Output:              None
919  * Return value:        None
920  */
921 void
922 twa_report(void)
923 {
924         struct twa_softc        *sc;
925         struct twa_request      *tr;
926         int                     s;
927         int                     i;
928
929         s = splcam();
930         for (i = 0; (sc = devclass_get_softc(twa_devclass, i)) != NULL; i++) {
931                 twa_print_controller(sc);
932                 TAILQ_FOREACH(tr, &sc->twa_busy, tr_link)
933                         twa_print_request(tr, TWA_CMD_BUSY);
934                 TAILQ_FOREACH(tr, &sc->twa_complete, tr_link)
935                         twa_print_request(tr, TWA_CMD_COMPLETE);
936         }
937         splx(s);
938 }
939
940
941
942 /*
943  * Function name:       twa_reset_stats
944  * Description:         For being called from ddb.
945  *                      Resets some controller stats.
946  *
947  * Input:               None
948  * Output:              None
949  * Return value:        None
950  */
951 void
952 twa_reset_stats(void)
953 {
954         struct twa_softc        *sc;
955         int                     s;
956         int                     i;
957
958         s = splcam();
959         for (i = 0; (sc = devclass_get_softc(twa_devclass, i)) != NULL; i++) {
960                 sc->twa_qstats[TWAQ_FREE].q_max = 0;
961                 sc->twa_qstats[TWAQ_BUSY].q_max = 0;
962                 sc->twa_qstats[TWAQ_PENDING].q_max = 0;
963                 sc->twa_qstats[TWAQ_COMPLETE].q_max = 0;
964         }
965         splx(s);
966 }
967
968
969
970 /*
971  * Function name:       twa_print_request
972  * Description:         Prints a given request if it's in the wrong queue.
973  *
974  * Input:               tr      -- ptr to request pkt
975  *                      req_type-- expected status of the given request
976  * Output:              None
977  * Return value:        None
978  */
979 void
980 twa_print_request(struct twa_request *tr, int req_type)
981 {
982         struct twa_softc                *sc = tr->tr_sc;
983         struct twa_command_packet       *cmdpkt = tr->tr_command;
984         struct twa_command_9k           *cmd9k;
985         union twa_command_7k            *cmd7k;
986         u_int8_t                        *cdb;
987         int                             cmd_phys_addr;
988
989         if (tr->tr_status != req_type) {
990                 twa_printf(sc, "Invalid %s request %p in queue! req_type = %x, queue_type = %x\n",
991                         (tr->tr_cmd_pkt_type & TWA_CMD_PKT_TYPE_INTERNAL) ? "INTERNAL" : "EXTERNAL",
992                         tr, tr->tr_status, req_type);
993
994                 if (tr->tr_cmd_pkt_type & TWA_CMD_PKT_TYPE_9K) {
995                         cmd9k = &(cmdpkt->command.cmd_pkt_9k);
996                         cmd_phys_addr = cmd9k->sg_list[0].address;
997                         twa_printf(sc, "9K cmd = %x %x %x %x %x %x %x %x %x\n",
998                                         cmd9k->command.opcode,
999                                         cmd9k->command.reserved,
1000                                         cmd9k->unit,
1001                                         cmd9k->request_id,
1002                                         cmd9k->status,
1003                                         cmd9k->sgl_offset,
1004                                         cmd9k->sgl_entries,
1005                                         cmd_phys_addr,
1006                                         cmd9k->sg_list[0].length);
1007                         cdb = (u_int8_t *)(cmdpkt->command.cmd_pkt_9k.cdb);
1008                         twa_printf(sc, "cdb = %x %x %x %x %x %x %x %x %x %x %x %x %x %x %x %x\n",
1009                                 cdb[0], cdb[1], cdb[2], cdb[3], cdb[4], cdb[5], cdb[6], cdb[7],
1010                                 cdb[8], cdb[9], cdb[10], cdb[11], cdb[12], cdb[13], cdb[14], cdb[15]);
1011                 } else {
1012                         cmd7k = &(cmdpkt->command.cmd_pkt_7k);
1013                         twa_printf(sc, "7K cmd = %x %x %x %x %x %x %x %x %x\n",
1014                                         cmd7k->generic.opcode,
1015                                         cmd7k->generic.sgl_offset,
1016                                         cmd7k->generic.size,
1017                                         cmd7k->generic.request_id,
1018                                         cmd7k->generic.unit,
1019                                         cmd7k->generic.host_id,
1020                                         cmd7k->generic.status,
1021                                         cmd7k->generic.flags,
1022                                         cmd7k->generic.count);
1023                 }
1024
1025                 cmd_phys_addr = (int)(tr->tr_cmd_phys);
1026                 twa_printf(sc, "cmdphys=0x%x data=%p length=0x%x\n",
1027                                 cmd_phys_addr, tr->tr_data, tr->tr_length);
1028                 twa_printf(sc, "req_id=0x%x flags=0x%x callback=%p private=%p\n",
1029                                         tr->tr_request_id, tr->tr_flags,
1030                                         tr->tr_callback, tr->tr_private);
1031         }
1032 }
1033 #endif