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