kernel/spinlock: Add a description to struct spinlock.
[dragonfly.git] / sys / dev / disk / nata / atapi-cam.c
1 /*-
2  * Copyright (c) 2001-2003 Thomas Quinot <thomas@cuivre.fr.eu.org>
3  * All rights reserved.
4  *
5  * Redistribution and use in source and binary forms, with or without
6  * modification, are permitted provided that the following conditions
7  * are met:
8  * 1. Redistributions of source code must retain the above copyright
9  *    notice, this list of conditions and the following disclaimer,
10  *    without modification, immediately at the beginning of the file.
11  * 2. Redistributions in binary form must reproduce the above copyright
12  *    notice, this list of conditions and the following disclaimer in the
13  *    documentation and/or other materials provided with the distribution.
14  * 3. The name of the author may not be used to endorse or promote products
15  *    derived from this software without specific prior written permission.
16  *
17  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
18  * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
19  * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
20  * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
21  * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
22  * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
23  * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
24  * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
25  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
26  * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
27  *
28  * $FreeBSD: src/sys/dev/ata/atapi-cam.c,v 1.44 2006/03/31 08:09:05 sos Exp $
29  */
30
31 #include "opt_ata.h"
32 #include "opt_scsi.h"
33
34 #include <sys/param.h>
35 #include <sys/bus.h>
36 #include <sys/libkern.h>
37 #include <sys/lock.h>           /* for {get,rel}_mplock() */
38 #include <sys/malloc.h>
39 #include <sys/module.h>
40 #include <sys/nata.h>
41 #include <sys/spinlock.h>
42 #include <sys/queue.h>
43 #include <sys/systm.h>
44
45 #include <sys/thread2.h>
46 #include <sys/spinlock2.h>
47 #include <sys/mplock2.h>
48
49 #include <bus/cam/cam.h>
50 #include <bus/cam/cam_ccb.h>
51 #include <bus/cam/cam_periph.h>
52 #include <bus/cam/cam_sim.h>
53 #include <bus/cam/cam_xpt_sim.h>
54 #include <bus/cam/cam_debug.h>
55 #include <bus/cam/scsi/scsi_all.h>
56
57 #include "ata-all.h"
58 #include "ata_if.h"
59
60 /* private data associated with an ATA bus */
61 struct atapi_xpt_softc {
62     struct ata_device   atapi_cam_dev;  /* must be first */
63     device_t            dev;
64     device_t            parent;
65     struct ata_channel  *ata_ch;
66     struct cam_path     *path;
67     struct cam_sim      *sim;
68     int                 flags;
69 #define BUS_REGISTERED          0x01
70 #define RESOURCE_SHORTAGE       0x02
71 #define DETACHING               0x04
72
73     TAILQ_HEAD(,atapi_hcb) pending_hcbs;
74     struct ata_device   *atadev[2];
75     struct spinlock     state_lock;
76 };
77
78 /* hardware command descriptor block */
79 struct atapi_hcb {
80     struct atapi_xpt_softc *softc;
81     int                 unit;
82     int                 bus;
83     int                 target;
84     int                 lun;
85     union ccb           *ccb;
86     int                 flags;
87 #define QUEUED          0x0001
88 #define AUTOSENSE       0x0002
89     char                *dxfer_alloc;
90     TAILQ_ENTRY(atapi_hcb) chain;
91 };
92
93 enum reinit_reason { ATTACH, RESET };
94
95 /* Device methods */
96 static void atapi_cam_identify(device_t *dev, device_t parent);
97 static int atapi_cam_probe(device_t dev);
98 static int atapi_cam_attach(device_t dev);
99 static int atapi_cam_detach(device_t dev);
100 static int atapi_cam_reinit(device_t dev);
101
102 /* CAM XPT methods */
103 static void atapi_action(struct cam_sim *, union ccb *);
104 static void atapi_poll(struct cam_sim *);
105 static void atapi_async(void *, u_int32_t, struct cam_path *, void *);
106 static void atapi_cb(struct ata_request *);
107
108 /* Module methods */
109 static int atapi_cam_event_handler(module_t mod, int what, void *arg);
110
111 /* internal functions */
112 static void reinit_bus(struct atapi_xpt_softc *scp, enum reinit_reason reason);
113 static void setup_async_cb(struct atapi_xpt_softc *, uint32_t);
114 static void cam_rescan_callback(struct cam_periph *, union ccb *);
115 static void cam_rescan(struct cam_sim *);
116 static void free_hcb_and_ccb_done(struct atapi_hcb *, u_int32_t);
117 static struct atapi_hcb *allocate_hcb(struct atapi_xpt_softc *, int, int, union ccb *);
118 static void free_hcb(struct atapi_hcb *hcb);
119 static void free_softc(struct atapi_xpt_softc *scp);
120
121 static MALLOC_DEFINE(M_ATACAM, "ata_cam", "ATA driver CAM-XPT layer");
122
123 static device_method_t atapi_cam_methods[] = {
124         DEVMETHOD(device_identify,      atapi_cam_identify),
125         DEVMETHOD(device_probe,         atapi_cam_probe),
126         DEVMETHOD(device_attach,        atapi_cam_attach),
127         DEVMETHOD(device_detach,        atapi_cam_detach),
128         DEVMETHOD(ata_reinit,           atapi_cam_reinit),
129         DEVMETHOD_END
130 };
131
132 static driver_t atapi_cam_driver = {
133         "atapicam",
134         atapi_cam_methods,
135         sizeof(struct atapi_xpt_softc)
136 };
137
138 static devclass_t       atapi_cam_devclass;
139 DRIVER_MODULE(atapicam, ata,
140         atapi_cam_driver,
141         atapi_cam_devclass,
142         atapi_cam_event_handler,
143         /*arg*/NULL);
144 MODULE_VERSION(atapicam, 1);
145 MODULE_DEPEND(atapicam, ata, 1, 1, 1);
146 MODULE_DEPEND(atapicam, cam, 1, 1, 1);
147
148 static void
149 atapi_cam_identify(device_t *dev, device_t parent)
150 {
151         struct atapi_xpt_softc *scp =
152             kmalloc(sizeof(struct atapi_xpt_softc), M_ATACAM, M_INTWAIT|M_ZERO);
153         device_t child;
154
155         /* Assume one atapicam instance per parent channel instance. */
156         child = device_add_child(parent, "atapicam", -1);
157         if (child == NULL) {
158                 /* XXX TGEN Use device_printf()? */
159                 kprintf("atapi_cam_identify: out of memory, can't add child");
160                 kfree(scp, M_ATACAM);
161                 return;
162         }
163         scp->atapi_cam_dev.unit = -1;
164         scp->atapi_cam_dev.dev = child;
165         device_quiet(child);
166         device_set_softc(child, scp);
167 }
168
169 static int
170 atapi_cam_probe(device_t dev)
171 {
172         struct ata_device *atadev = device_get_softc (dev);
173
174         KASSERT(atadev != NULL, ("expect valid struct ata_device"));
175         if (atadev->unit < 0) {
176                 device_set_desc(dev, "ATAPI CAM Attachment");
177                 return(0);
178         } else {
179                 return ENXIO;
180         }
181 }
182
183 static int
184 atapi_cam_attach(device_t dev)
185 {
186     struct atapi_xpt_softc *scp = NULL;
187     struct cam_devq *devq = NULL;
188     struct cam_sim *sim = NULL;
189     struct cam_path *path = NULL;
190     int unit, error;
191
192     scp = (struct atapi_xpt_softc *)device_get_softc(dev);
193     if (scp == NULL) {
194         device_printf(dev, "Cannot get softc\n");
195         return ENOMEM;
196     }
197
198     spin_init(&scp->state_lock, "atapicamattach");
199
200     scp->dev = dev;
201     scp->parent = device_get_parent(dev);
202     scp->ata_ch = device_get_softc(scp->parent);
203     TAILQ_INIT(&scp->pending_hcbs);
204     unit = device_get_unit(dev);
205
206     if ((devq = cam_simq_alloc(16)) == NULL) {
207         error = ENOMEM;
208         goto out;
209     }
210
211     if ((sim = cam_sim_alloc(atapi_action, atapi_poll, "ata",
212                  (void *)scp, unit, &sim_mplock, 1, 1, devq)) == NULL) {
213         error = ENOMEM;
214         goto out;
215     }
216     scp->sim = sim;
217
218     if (xpt_bus_register(sim, 0) != CAM_SUCCESS) {
219         error = EINVAL;
220         goto out;
221     }
222     scp->flags |= BUS_REGISTERED;
223
224     if (xpt_create_path(&path, /*periph*/ NULL,
225                 cam_sim_path(sim), CAM_TARGET_WILDCARD,
226                 CAM_LUN_WILDCARD) != CAM_REQ_CMP) {
227         error = ENOMEM;
228         goto out;
229     }
230     scp->path = path;
231
232     CAM_DEBUG(path, CAM_DEBUG_TRACE, ("Registered SIM for ata%d\n", unit));
233
234     setup_async_cb(scp, AC_LOST_DEVICE);
235     reinit_bus(scp, ATTACH);
236     error = 0;
237
238 out:
239     if (error != 0)
240         free_softc(scp);
241
242     return(error);
243 }
244
245 static int
246 atapi_cam_detach(device_t dev)
247 {
248     struct atapi_xpt_softc *scp = device_get_softc(dev);
249
250     get_mplock();
251     xpt_freeze_simq(scp->sim, 1 /*count*/);
252     rel_mplock();
253     spin_lock(&scp->state_lock);
254     scp->flags |= DETACHING;
255     spin_unlock(&scp->state_lock);
256     free_softc(scp);
257     return (0);
258 }
259
260 static int
261 atapi_cam_reinit(device_t dev) {
262     struct atapi_xpt_softc *scp = device_get_softc(dev);
263
264     /*
265      * scp might be null if the bus is being reinitialised during
266      * the boot-up sequence, before the ATAPI bus is registered.
267      */
268
269     if (scp != NULL) {
270         reinit_bus(scp, RESET);
271     }
272     return 0;
273 }
274
275 static void
276 reinit_bus(struct atapi_xpt_softc *scp, enum reinit_reason reason) {
277     struct ata_device *old_atadev[2], *atadev;
278     device_t *children;
279     int nchildren, i, dev_changed;
280
281     if (device_get_children(scp->parent, &children, &nchildren) != 0) {
282         return;
283     }
284
285     spin_lock(&scp->state_lock);
286     old_atadev[0] = scp->atadev[0];
287     old_atadev[1] = scp->atadev[1];
288     scp->atadev[0] = NULL;
289     scp->atadev[1] = NULL;
290
291     for (i = 0; i < nchildren; i++) {
292         /* XXX Does the child need to actually be attached yet? */
293         if (children[i] != NULL) {
294             atadev = device_get_softc(children[i]);
295             if ((atadev->unit == ATA_MASTER) &&
296                 (scp->ata_ch->devices & ATA_ATAPI_MASTER) != 0)
297                 scp->atadev[0] = atadev;
298             if ((atadev->unit == ATA_SLAVE) &&
299                 (scp->ata_ch->devices & ATA_ATAPI_SLAVE) != 0)
300                 scp->atadev[1] = atadev;
301         }
302     }
303     dev_changed = (old_atadev[0] != scp->atadev[0])
304             || (old_atadev[1] != scp->atadev[1]);
305     spin_unlock(&scp->state_lock);
306     kfree(children, M_TEMP);
307
308     switch (reason) {
309         case RESET:
310             xpt_async(AC_BUS_RESET, scp->path, NULL);
311
312             if (!dev_changed)
313                     break;
314
315             /*FALLTHROUGH*/
316         case ATTACH:
317             cam_rescan(scp->sim);
318             break;
319     }
320 }
321
322 static void
323 setup_async_cb(struct atapi_xpt_softc *scp, uint32_t events)
324 {
325     struct ccb_setasync csa;
326
327     get_mplock();
328     xpt_setup_ccb(&csa.ccb_h, scp->path, /*priority*/ 5);
329     csa.ccb_h.func_code = XPT_SASYNC_CB;
330     csa.event_enable = events;
331     csa.callback = &atapi_async;
332     csa.callback_arg = scp->sim;
333     xpt_action((union ccb *) &csa);
334     rel_mplock();
335 }
336
337 static void
338 atapi_action(struct cam_sim *sim, union ccb *ccb)
339 {
340     struct atapi_xpt_softc *softc = (struct atapi_xpt_softc*)cam_sim_softc(sim);
341     struct ccb_hdr *ccb_h = &ccb->ccb_h;
342     struct atapi_hcb *hcb = NULL;
343     struct ata_request *request = NULL;
344     int unit = cam_sim_unit(sim);
345     int bus = cam_sim_bus(sim);
346     int len;
347     char *buf;
348
349     switch (ccb_h->func_code) {
350     case XPT_PATH_INQ: {
351         struct ccb_pathinq *cpi = &ccb->cpi;
352         int tid = ccb_h->target_id;
353
354         cpi->version_num = 1;
355         cpi->hba_inquiry = 0;
356         cpi->target_sprt = 0;
357         cpi->hba_misc = PIM_NO_6_BYTE;
358         cpi->hba_eng_cnt = 0;
359         bzero(cpi->vuhba_flags, sizeof(cpi->vuhba_flags));
360         cpi->max_target = 1;
361         cpi->max_lun = 0;
362         cpi->async_flags = 0;
363         cpi->hpath_id = 0;
364         cpi->initiator_id = 7;
365         strncpy(cpi->sim_vid, "FreeBSD", sizeof(cpi->sim_vid));
366         strncpy(cpi->hba_vid, "ATAPI", sizeof(cpi->hba_vid));
367         strncpy(cpi->dev_name, cam_sim_name(sim), sizeof cpi->dev_name);
368         cpi->unit_number = cam_sim_unit(sim);
369         cpi->bus_id = cam_sim_bus(sim);
370         cpi->base_transfer_speed = 3300;
371         cpi->transport = XPORT_ATA;
372         cpi->transport_version = 2;
373         cpi->protocol = PROTO_SCSI;
374         cpi->protocol_version = SCSI_REV_2;
375
376         if (softc->ata_ch && tid != CAM_TARGET_WILDCARD) {
377             spin_lock(&softc->state_lock);
378             if (softc->atadev[tid] == NULL) {
379                 ccb->ccb_h.status = CAM_DEV_NOT_THERE;
380                 xpt_done(ccb);
381                 spin_unlock(&softc->state_lock);
382                 return;
383             }
384             switch (softc->atadev[ccb_h->target_id]->mode) {
385             case ATA_PIO1:
386                 cpi->base_transfer_speed = 5200;
387                 break;
388             case ATA_PIO2:
389                 cpi->base_transfer_speed = 7000;
390                 break;
391             case ATA_PIO3:
392                 cpi->base_transfer_speed = 11000;
393                 break;
394             case ATA_PIO4:
395             case ATA_DMA:
396             case ATA_WDMA2:
397                 cpi->base_transfer_speed = 16000;
398                 break;
399             case ATA_UDMA2:
400                 cpi->base_transfer_speed = 33000;
401                 break;
402             case ATA_UDMA4:
403                 cpi->base_transfer_speed = 66000;
404                 break;
405             case ATA_UDMA5:
406                 cpi->base_transfer_speed = 100000;
407                 break;
408             case ATA_UDMA6:
409                 cpi->base_transfer_speed = 133000;
410                 break;
411             default:
412                 break;
413             }
414             spin_unlock(&softc->state_lock);
415         }
416         ccb->ccb_h.status = CAM_REQ_CMP;
417         xpt_done(ccb);
418         return;
419     }
420
421     case XPT_RESET_DEV: {
422         int tid = ccb_h->target_id;
423
424         CAM_DEBUG(ccb->ccb_h.path, CAM_DEBUG_SUBTRACE, ("dev reset\n"));
425         ata_controlcmd(softc->atadev[tid]->dev, ATA_DEVICE_RESET, 0, 0, 0);
426         ccb->ccb_h.status = CAM_REQ_CMP;
427         xpt_done(ccb);
428         return;
429     }
430
431     case XPT_RESET_BUS:
432         CAM_DEBUG(ccb->ccb_h.path, CAM_DEBUG_SUBTRACE, ("bus reset\n"));
433         ata_reinit(softc->parent);
434         ccb->ccb_h.status = CAM_REQ_CMP;
435         xpt_done(ccb);
436         return;
437
438     case XPT_SET_TRAN_SETTINGS:
439         /* ignore these, we're not doing SCSI here */
440         CAM_DEBUG(ccb->ccb_h.path, CAM_DEBUG_SUBTRACE,
441                   ("SET_TRAN_SETTINGS not supported\n"));
442         ccb->ccb_h.status = CAM_FUNC_NOTAVAIL;
443         xpt_done(ccb);
444         return;
445
446     case XPT_GET_TRAN_SETTINGS: {
447         struct ccb_trans_settings *cts = &ccb->cts;
448         cts->protocol = PROTO_SCSI;
449         cts->protocol_version = SCSI_REV_2;
450         cts->transport = XPORT_ATA;
451         cts->transport_version = XPORT_VERSION_UNSPECIFIED;
452         cts->proto_specific.valid = 0;
453         cts->xport_specific.valid = 0;
454         /* nothing more to do */
455         ccb->ccb_h.status = CAM_REQ_CMP;
456         CAM_DEBUG(ccb->ccb_h.path, CAM_DEBUG_SUBTRACE, ("GET_TRAN_SETTINGS\n"));
457         xpt_done(ccb);
458         return;
459     }
460
461     case XPT_CALC_GEOMETRY: {
462         CAM_DEBUG(ccb->ccb_h.path, CAM_DEBUG_SUBTRACE, ("CALC_GEOMETRY\n"));
463         cam_calc_geometry(&ccb->ccg, /*extended*/1);
464         xpt_done(ccb);
465         return;
466     }
467
468     case XPT_SCSI_IO: {
469         struct ccb_scsiio *csio = &ccb->csio;
470         int tid = ccb_h->target_id, lid = ccb_h->target_lun;
471         int request_flags = ATA_R_QUIET | ATA_R_ATAPI;
472
473         CAM_DEBUG(ccb_h->path, CAM_DEBUG_SUBTRACE, ("XPT_SCSI_IO\n"));
474
475         spin_lock(&softc->state_lock);
476         if (softc->flags & DETACHING) {
477             ccb->ccb_h.status = CAM_REQ_ABORTED;
478             xpt_done(ccb);
479             spin_unlock(&softc->state_lock);
480             return;
481         }
482
483         if (softc->atadev[tid] == NULL) {
484             ccb->ccb_h.status = CAM_DEV_NOT_THERE;
485             xpt_done(ccb);
486             spin_unlock(&softc->state_lock);
487             return;
488         }
489
490         /* check that this request was not aborted already */
491         if ((ccb_h->status & CAM_STATUS_MASK) != CAM_REQ_INPROG) {
492             kprintf("XPT_SCSI_IO received but already in progress?\n");
493             xpt_done(ccb);
494             spin_unlock(&softc->state_lock);
495             return;
496         }
497         if (lid > 0) {
498             CAM_DEBUG(ccb_h->path, CAM_DEBUG_SUBTRACE,
499                       ("SCSI IO received for invalid lun %d\n", lid));
500             goto action_invalid;
501         }
502         if (csio->cdb_len > sizeof request->u.atapi.ccb) {
503             CAM_DEBUG(ccb_h->path, CAM_DEBUG_SUBTRACE,
504                 ("CAM CCB too long for ATAPI"));
505             goto action_invalid;
506         }
507         if ((ccb_h->flags & CAM_SCATTER_VALID)) {
508             /* scatter-gather not supported */
509             xpt_print_path(ccb_h->path);
510             kprintf("ATAPI/CAM does not support scatter-gather yet!\n");
511             goto action_invalid;
512         }
513
514         switch (ccb_h->flags & CAM_DIR_MASK) {
515         case CAM_DIR_IN:
516              request_flags |= ATA_R_READ;
517              break;
518         case CAM_DIR_OUT:
519              request_flags |= ATA_R_WRITE;
520              break;
521         case CAM_DIR_NONE:
522              /* No flags need to be set */
523              break;
524         default:
525              device_printf(softc->dev, "unknown IO operation\n");
526              goto action_invalid;
527         }
528
529         if ((hcb = allocate_hcb(softc, unit, bus, ccb)) == NULL) {
530             kprintf("cannot allocate ATAPI/CAM hcb\n");
531             goto action_oom;
532         }
533         if ((request = ata_alloc_request()) == NULL) {
534             kprintf("cannot allocate ATAPI/CAM request\n");
535             goto action_oom;
536         }
537
538         bcopy((ccb_h->flags & CAM_CDB_POINTER) ?
539               csio->cdb_io.cdb_ptr : csio->cdb_io.cdb_bytes,
540               request->u.atapi.ccb, csio->cdb_len);
541 #ifdef CAMDEBUG
542         if (CAM_DEBUGGED(ccb_h->path, CAM_DEBUG_CDB)) {
543                 char cdb_str[(SCSI_MAX_CDBLEN * 3) + 1];
544
545                 kprintf("atapi_action: hcb@%p: %s\n", hcb,
546                        scsi_cdb_string(request->u.atapi.ccb, cdb_str, sizeof(cdb_str)));
547         }
548         if (CAM_DEBUGGED(ccb_h->path, CAM_DEBUG_SUBTRACE)) {
549                 request_flags |= ATA_R_DEBUG;
550         }
551 #endif
552
553         len = csio->dxfer_len;
554         buf = csio->data_ptr;
555
556         /* some SCSI commands require special processing */
557         switch (request->u.atapi.ccb[0]) {
558         case INQUIRY: {
559             /*
560              * many ATAPI devices seem to report more than
561              * SHORT_INQUIRY_LENGTH bytes of available INQUIRY
562              * information, but respond with some incorrect condition
563              * when actually asked for it, so we are going to pretend
564              * that only SHORT_INQUIRY_LENGTH are expected, anyway.
565              */
566             struct scsi_inquiry *inq = (struct scsi_inquiry *) &request->u.atapi.ccb[0];
567
568             if (inq->byte2 == 0 && inq->page_code == 0 &&
569                 inq->length > SHORT_INQUIRY_LENGTH) {
570                 bzero(buf, len);
571                 len = inq->length = SHORT_INQUIRY_LENGTH;
572             }
573             break;
574         }
575         case READ_6:
576             /* FALLTHROUGH */
577
578         case WRITE_6:
579             CAM_DEBUG(ccb_h->path, CAM_DEBUG_SUBTRACE,
580                       ("Translating %s into _10 equivalent\n",
581                       (request->u.atapi.ccb[0] == READ_6) ? "READ_6" : "WRITE_6"));
582             request->u.atapi.ccb[0] |= 0x20;
583             request->u.atapi.ccb[9] = request->u.atapi.ccb[5];
584             request->u.atapi.ccb[8] = request->u.atapi.ccb[4];
585             request->u.atapi.ccb[7] = 0;
586             request->u.atapi.ccb[6] = 0;
587             request->u.atapi.ccb[5] = request->u.atapi.ccb[3];
588             request->u.atapi.ccb[4] = request->u.atapi.ccb[2];
589             request->u.atapi.ccb[3] = request->u.atapi.ccb[1] & 0x1f;
590             request->u.atapi.ccb[2] = 0;
591             request->u.atapi.ccb[1] = 0;
592             break;
593         }
594
595         if ((ccb_h->flags & CAM_DIR_MASK) == CAM_DIR_IN && (len & 1)) {
596             /* ATA always transfers an even number of bytes */
597             buf = hcb->dxfer_alloc =
598                 kmalloc(++len, M_ATACAM, M_INTWAIT | M_ZERO);
599         }
600
601         /*
602          * Don't allow DMA for requests with length not multiple of 16 bytes.
603          * Some ATAPI devices don't like it.
604          */
605         if ((softc->atadev[tid]->mode >= ATA_DMA) && len > 0 && !(len & 15))
606                 request_flags |= ATA_R_DMA;
607
608         request->dev = softc->atadev[tid]->dev;
609         request->driver = hcb;
610         request->data = buf;
611         request->bytecount = len;
612         request->transfersize = min(request->bytecount,
613                                 min(softc->ata_ch->dma->max_iosize, 65534));
614         request->timeout = ccb_h->timeout / 1000; /* XXX lost granularity */
615         request->callback = &atapi_cb;
616         request->flags = request_flags;
617
618         /*
619          * no retries are to be performed at the ATA level; any retries
620          * will be done by CAM .
621          */
622         request->retries = 0;
623
624         TAILQ_INSERT_TAIL(&softc->pending_hcbs, hcb, chain);
625         hcb->flags |= QUEUED;
626         ccb_h->status |= CAM_SIM_QUEUED;
627         spin_unlock(&softc->state_lock);
628
629         ata_queue_request(request);
630         return;
631     }
632
633     default:
634         CAM_DEBUG(ccb_h->path, CAM_DEBUG_SUBTRACE,
635                   ("unsupported function code 0x%02x\n", ccb_h->func_code));
636         goto action_invalid;
637     }
638
639     /* NOTREACHED */
640
641 action_oom:
642     if (request != NULL)
643         ata_free_request(request);
644     if (hcb != NULL)
645         free_hcb(hcb);
646     spin_unlock(&softc->state_lock);
647     get_mplock();
648     xpt_print_path(ccb_h->path);
649     kprintf("out of memory, freezing queue.\n");
650     softc->flags |= RESOURCE_SHORTAGE;
651     xpt_freeze_simq(sim, /*count*/ 1);
652     rel_mplock();
653     ccb_h->status = CAM_REQUEUE_REQ;
654     xpt_done(ccb);
655     return;
656
657 action_invalid:
658     spin_unlock(&softc->state_lock);
659     ccb_h->status = CAM_REQ_INVALID;
660     xpt_done(ccb);
661     return;
662 }
663
664 static void
665 atapi_poll(struct cam_sim *sim)
666 {
667     /* do nothing - we do not actually service any interrupts */
668     kprintf("atapi_poll called!\n");
669 }
670
671 static void
672 atapi_cb(struct ata_request *request)
673 {
674     struct atapi_xpt_softc *scp;
675     struct atapi_hcb *hcb;
676     struct ccb_scsiio *csio;
677     u_int32_t rc;
678
679     hcb = (struct atapi_hcb *)request->driver;
680     scp = hcb->softc;
681     csio = &hcb->ccb->csio;
682
683 #ifdef CAMDEBUG
684 # define err (request->u.atapi.sense.key)
685     if (CAM_DEBUGGED(csio->ccb_h.path, CAM_DEBUG_CDB)) {
686         kprintf("atapi_cb: hcb@%p sense = %02x: sk = %01x%s%s%s\n",
687                 hcb, err, err & 0x0f,
688                 (err & 0x80) ? ", Filemark" : "",
689                 (err & 0x40) ? ", EOM" : "",
690                 (err & 0x20) ? ", ILI" : "");
691         device_printf(request->dev,
692             "cmd %s status %02x result %02x error %02x\n",
693             ata_cmd2str(request),
694             request->status, request->result, request->error);
695     }
696 #endif
697
698     if ((hcb->flags & AUTOSENSE) != 0) {
699         rc = CAM_SCSI_STATUS_ERROR;
700         if (request->result == 0) {
701             csio->ccb_h.status |= CAM_AUTOSNS_VALID;
702         }
703     } else if (request->result != 0) {
704         if ((request->flags & ATA_R_TIMEOUT) != 0) {
705             rc = CAM_CMD_TIMEOUT;
706         } else {
707             rc = CAM_SCSI_STATUS_ERROR;
708             csio->scsi_status = SCSI_STATUS_CHECK_COND;
709
710             if ((csio->ccb_h.flags & CAM_DIS_AUTOSENSE) == 0) {
711 #if 0
712                 static const int8_t ccb[16] = { ATAPI_REQUEST_SENSE, 0, 0, 0,
713                     sizeof(struct atapi_sense), 0, 0, 0, 0, 0, 0,
714                     0, 0, 0, 0, 0 };
715
716                 bcopy (ccb, request->u.atapi.ccb, sizeof ccb);
717                 request->data = (caddr_t)&csio->sense_data;
718                 request->bytecount = sizeof(struct atapi_sense);
719                 request->transfersize = min(request->bytecount, 65534);
720                 request->timeout = csio->ccb_h.timeout / 1000;
721                 request->retries = 2;
722                 request->flags = ATA_R_QUIET|ATA_R_ATAPI|ATA_R_IMMEDIATE;
723                 hcb->flags |= AUTOSENSE;
724
725                 ata_queue_request(request);
726                 return;
727 #else
728                 /*
729                  * Use auto-sense data from the ATA layer, if it has
730                  * issued a REQUEST SENSE automatically and that operation
731                  * returned without error.
732                  */
733                 if (request->u.atapi.sense.key != 0 && request->error == 0) {
734                     bcopy (&request->u.atapi.sense, &csio->sense_data, sizeof(struct atapi_sense));
735                     csio->ccb_h.status |= CAM_AUTOSNS_VALID;
736                 }
737             }
738 #endif
739         }
740     } else {
741         rc = CAM_REQ_CMP;
742         csio->scsi_status = SCSI_STATUS_OK;
743         if (((csio->ccb_h.flags & CAM_DIR_MASK) == CAM_DIR_IN) &&
744             hcb->dxfer_alloc != NULL)
745         {
746             bcopy(hcb->dxfer_alloc, csio->data_ptr, csio->dxfer_len);
747         }
748     }
749
750     spin_lock(&scp->state_lock);
751     free_hcb_and_ccb_done(hcb, rc);
752     spin_unlock(&scp->state_lock);
753
754     ata_free_request(request);
755 }
756
757 static void
758 free_hcb_and_ccb_done(struct atapi_hcb *hcb, u_int32_t status)
759 {
760     struct atapi_xpt_softc *softc;
761     union ccb *ccb;
762
763     if (hcb == NULL)
764         return;
765
766     softc = hcb->softc;
767     ccb = hcb->ccb;
768
769     /* we're about to free a hcb, so the shortage has ended */
770     if (softc->flags & RESOURCE_SHORTAGE) {
771         softc->flags &= ~RESOURCE_SHORTAGE;
772         status |= CAM_RELEASE_SIMQ;
773     }
774     free_hcb(hcb);
775     ccb->ccb_h.status =
776         status | (ccb->ccb_h.status & ~(CAM_STATUS_MASK | CAM_SIM_QUEUED));
777     xpt_done(ccb);
778 }
779
780 static void
781 atapi_async(void *callback_arg, u_int32_t code,
782              struct cam_path* path, void *arg)
783 {
784     int targ;
785
786     crit_enter();
787
788     switch (code) {
789     case AC_LOST_DEVICE:
790         targ = xpt_path_target_id(path);
791         xpt_print_path(path);
792         if (targ == -1)
793                 kprintf("Lost host adapter\n");
794         else
795                 kprintf("Lost target %d???\n", targ);
796         break;
797
798     default:
799         break;
800     }
801
802     crit_exit();
803 }
804
805 static void
806 cam_rescan_callback(struct cam_periph *periph, union ccb *ccb)
807 {
808         if (ccb->ccb_h.status != CAM_REQ_CMP) {
809             CAM_DEBUG(ccb->ccb_h.path, CAM_DEBUG_TRACE,
810                       ("Rescan failed, 0x%04x\n", ccb->ccb_h.status));
811         } else {
812             CAM_DEBUG(ccb->ccb_h.path, CAM_DEBUG_TRACE,
813                       ("Rescan succeeded\n"));
814         }
815         xpt_free_path(ccb->ccb_h.path);
816         kfree(ccb, M_ATACAM);
817 }
818
819 static void
820 cam_rescan(struct cam_sim *sim)
821 {
822     struct cam_path *path;
823     union ccb *ccb = kmalloc(sizeof(union ccb), M_ATACAM, M_WAITOK | M_ZERO);
824
825     get_mplock();
826     if (xpt_create_path(&path, xpt_periph, cam_sim_path(sim),
827                         CAM_TARGET_WILDCARD, CAM_LUN_WILDCARD) != CAM_REQ_CMP) {
828         rel_mplock();
829         kfree(ccb, M_ATACAM);
830         return;
831     }
832
833     CAM_DEBUG(path, CAM_DEBUG_TRACE, ("Rescanning ATAPI bus.\n"));
834     xpt_setup_ccb(&ccb->ccb_h, path, 5/*priority (low)*/);
835     ccb->ccb_h.func_code = XPT_SCAN_BUS;
836     ccb->ccb_h.cbfcnp = cam_rescan_callback;
837     ccb->crcn.flags = CAM_FLAG_NONE;
838     xpt_action(ccb);
839     /* scan is in progress now */
840     rel_mplock();
841 }
842
843 static struct atapi_hcb *
844 allocate_hcb(struct atapi_xpt_softc *softc, int unit, int bus, union ccb *ccb)
845 {
846     struct atapi_hcb *hcb = (struct atapi_hcb *)
847         kmalloc(sizeof(struct atapi_hcb), M_ATACAM, M_INTWAIT | M_ZERO);
848
849     hcb->softc = softc;
850     hcb->unit = unit;
851     hcb->bus = bus;
852     hcb->ccb = ccb;
853     return hcb;
854 }
855
856 static void
857 free_hcb(struct atapi_hcb *hcb)
858 {
859     if ((hcb->flags & QUEUED) != 0)
860         TAILQ_REMOVE(&hcb->softc->pending_hcbs, hcb, chain);
861     if (hcb->dxfer_alloc != NULL)
862         kfree(hcb->dxfer_alloc, M_ATACAM);
863     kfree(hcb, M_ATACAM);
864 }
865
866 static void
867 free_softc(struct atapi_xpt_softc *scp)
868 {
869     struct atapi_hcb *hcb;
870
871     if (scp != NULL) {
872         spin_lock(&scp->state_lock);
873         TAILQ_FOREACH(hcb, &scp->pending_hcbs, chain) {
874             free_hcb_and_ccb_done(hcb, CAM_UNREC_HBA_ERROR);
875         }
876         spin_unlock(&scp->state_lock);
877         get_mplock();
878         if (scp->path != NULL) {
879             setup_async_cb(scp, 0);
880             xpt_free_path(scp->path);
881         }
882         if ((scp->flags & BUS_REGISTERED) != 0) {
883             if (xpt_bus_deregister(cam_sim_path(scp->sim)) == CAM_REQ_CMP)
884                 scp->flags &= ~BUS_REGISTERED;
885         }
886         if (scp->sim != NULL) {
887             if ((scp->flags & BUS_REGISTERED) == 0)
888                 cam_sim_free(scp->sim);
889             else
890                 kprintf("Can't free %s SIM (still registered)\n",
891                        cam_sim_name(scp->sim));
892         }
893         rel_mplock();
894         spin_uninit(&scp->state_lock);
895     }
896 }
897
898 static int
899 atapi_cam_event_handler(module_t mod, int what, void *arg) {
900     device_t *devlist;
901     int devcount;
902
903     switch (what) {
904         case MOD_UNLOAD:
905             if (devclass_get_devices(atapi_cam_devclass, &devlist, &devcount)
906                   != 0)
907                 return ENXIO;
908             if (devlist != NULL) {
909                 while (devlist != NULL && devcount > 0) {
910                     device_t child = devlist[--devcount];
911                     struct atapi_xpt_softc *scp = device_get_softc(child);
912
913                     device_delete_child(device_get_parent(child),child);
914                     if (scp != NULL)
915                         kfree(scp, M_ATACAM);
916                 }
917                 kfree(devlist, M_TEMP);
918             }
919             break;
920
921         default:
922             break;
923     }
924     return 0;
925 }