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