2 * Copyright (c) 2001-2003 Thomas Quinot <thomas@cuivre.fr.eu.org>
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions
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.
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.
29 #include <sys/cdefs.h>
30 __FBSDID("$FreeBSD$");
32 #include <sys/param.h>
33 #include <sys/systm.h>
35 #include <sys/kernel.h>
36 #include <sys/malloc.h>
38 #include <sys/taskqueue.h>
40 #include <sys/mutex.h>
43 #include <machine/resource.h>
44 #include <machine/bus.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>
54 #include <dev/ata/ata-all.h>
57 /* private data associated with an ATA bus */
58 struct atapi_xpt_softc {
59 struct ata_device atapi_cam_dev; /* must be first */
62 struct ata_channel *ata_ch;
63 struct cam_path *path;
66 #define BUS_REGISTERED 0x01
67 #define RESOURCE_SHORTAGE 0x02
68 #define DETACHING 0x04
70 TAILQ_HEAD(,atapi_hcb) pending_hcbs;
71 struct ata_device *atadev[2];
72 struct mtx state_lock;
75 /* hardware command descriptor block */
77 struct atapi_xpt_softc *softc;
85 #define AUTOSENSE 0x0002
87 TAILQ_ENTRY(atapi_hcb) chain;
90 enum reinit_reason { BOOT_ATTACH, ATTACH, RESET };
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);
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 *);
106 static int atapi_cam_event_handler(module_t mod, int what, void *arg);
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);
118 static MALLOC_DEFINE(M_ATACAM, "ata_cam", "ATA driver CAM-XPT layer");
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),
129 static driver_t atapi_cam_driver = {
132 sizeof(struct atapi_xpt_softc)
135 static devclass_t atapi_cam_devclass;
136 DRIVER_MODULE(atapicam, ata,
139 atapi_cam_event_handler,
141 MODULE_VERSION(atapicam, 1);
142 MODULE_DEPEND(atapicam, ata, 1, 1, 1);
143 MODULE_DEPEND(atapicam, cam, 1, 1, 1);
146 atapi_cam_identify(device_t *dev, device_t parent)
148 struct atapi_xpt_softc *scp =
149 malloc (sizeof (struct atapi_xpt_softc), M_ATACAM, M_NOWAIT|M_ZERO);
153 printf ("atapi_cam_identify: out of memory");
157 /* Assume one atapicam instance per parent channel instance. */
158 child = device_add_child(parent, "atapicam", -1);
160 printf ("atapi_cam_identify: out of memory, can't add child");
161 free (scp, M_ATACAM);
164 scp->atapi_cam_dev.unit = -1;
165 scp->atapi_cam_dev.dev = child;
167 device_set_softc(child, scp);
171 atapi_cam_probe(device_t dev)
173 struct ata_device *atadev = device_get_softc (dev);
175 KASSERT(atadev != NULL, ("expect valid struct ata_device"));
176 if (atadev->unit < 0) {
177 device_set_desc(dev, "ATAPI CAM Attachment");
185 atapi_cam_attach(device_t dev)
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;
193 scp = (struct atapi_xpt_softc *)device_get_softc(dev);
195 device_printf(dev, "Cannot get softc\n");
199 mtx_init(&scp->state_lock, "ATAPICAM lock", NULL, MTX_DEF);
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);
207 if ((devq = cam_simq_alloc(16)) == NULL) {
212 if ((sim = cam_sim_alloc(atapi_action, atapi_poll, "ata",
213 (void *)scp, unit, 1, 1, devq)) == NULL) {
219 if (xpt_bus_register(sim, 0) != CAM_SUCCESS) {
223 scp->flags |= BUS_REGISTERED;
225 if (xpt_create_path(&path, /*periph*/ NULL,
226 cam_sim_path(sim), CAM_TARGET_WILDCARD,
227 CAM_LUN_WILDCARD) != CAM_REQ_CMP) {
233 CAM_DEBUG(path, CAM_DEBUG_TRACE, ("Registered SIM for ata%d\n", unit));
235 setup_async_cb(scp, AC_LOST_DEVICE);
236 reinit_bus(scp, cold ? BOOT_ATTACH : ATTACH);
247 atapi_cam_detach(device_t dev)
249 struct atapi_xpt_softc *scp = device_get_softc(dev);
252 xpt_freeze_simq(scp->sim, 1 /*count*/);
254 mtx_lock(&scp->state_lock);
255 scp->flags |= DETACHING;
256 mtx_unlock(&scp->state_lock);
262 atapi_cam_reinit(device_t dev) {
263 struct atapi_xpt_softc *scp = device_get_softc(dev);
266 * scp might be null if the bus is being reinitialised during
267 * the boot-up sequence, before the ATAPI bus is registered.
271 reinit_bus(scp, RESET);
277 reinit_bus(struct atapi_xpt_softc *scp, enum reinit_reason reason) {
278 struct ata_device *atadev;
282 if (device_get_children(scp->parent, &children, &nchildren) != 0) {
286 mtx_lock(&scp->state_lock);
287 scp->atadev[0] = NULL;
288 scp->atadev[1] = NULL;
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;
302 mtx_unlock(&scp->state_lock);
303 free(children, M_TEMP);
309 xpt_async(AC_BUS_RESET, scp->path, NULL);
312 cam_rescan(scp->sim);
318 setup_async_cb(struct atapi_xpt_softc *scp, uint32_t events)
320 struct ccb_setasync csa;
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);
333 atapi_action(struct cam_sim *sim, union ccb *ccb)
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);
344 switch (ccb_h->func_code) {
346 struct ccb_pathinq *cpi = &ccb->cpi;
347 int tid = ccb_h->target_id;
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));
357 cpi->async_flags = 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;
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;
372 mtx_unlock(&softc->state_lock);
375 switch (softc->atadev[ccb_h->target_id]->mode) {
377 cpi->base_transfer_speed = 5200;
380 cpi->base_transfer_speed = 7000;
383 cpi->base_transfer_speed = 11000;
388 cpi->base_transfer_speed = 16000;
391 cpi->base_transfer_speed = 33000;
394 cpi->base_transfer_speed = 66000;
397 cpi->base_transfer_speed = 100000;
400 cpi->base_transfer_speed = 133000;
405 mtx_unlock(&softc->state_lock);
407 ccb->ccb_h.status = CAM_REQ_CMP;
412 case XPT_RESET_DEV: {
413 int tid = ccb_h->target_id;
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;
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;
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;
437 case XPT_GET_TRAN_SETTINGS: {
438 struct ccb_trans_settings *cts = &ccb->cts;
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
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;
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);
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;
465 CAM_DEBUG(ccb_h->path, CAM_DEBUG_SUBTRACE, ("XPT_SCSI_IO\n"));
467 mtx_lock(&softc->state_lock);
468 if (softc->flags & DETACHING) {
469 ccb->ccb_h.status = CAM_REQ_ABORTED;
471 mtx_unlock(&softc->state_lock);
475 if (softc->atadev[tid] == NULL) {
476 ccb->ccb_h.status = CAM_DEV_NOT_THERE;
478 mtx_unlock(&softc->state_lock);
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");
486 mtx_unlock(&softc->state_lock);
490 CAM_DEBUG(ccb_h->path, CAM_DEBUG_SUBTRACE,
491 ("SCSI IO received for invalid lun %d\n", lid));
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"));
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");
506 switch (ccb_h->flags & CAM_DIR_MASK) {
508 request_flags |= ATA_R_READ|ATA_R_DMA;
511 request_flags |= ATA_R_WRITE|ATA_R_DMA;
514 /* No flags need to be set */
517 device_printf(softc->dev, "unknown IO operation\n");
520 if (softc->atadev[tid]->mode < ATA_DMA)
521 request_flags &= ~ATA_R_DMA;
523 if ((hcb = allocate_hcb(softc, unit, bus, ccb)) == NULL) {
524 printf("cannot allocate ATAPI/CAM hcb\n");
527 if ((request = ata_alloc_request()) == NULL) {
528 printf("cannot allocate ATAPI/CAM request\n");
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);
536 if (CAM_DEBUGGED(ccb_h->path, CAM_DEBUG_CDB)) {
537 char cdb_str[(SCSI_MAX_CDBLEN * 3) + 1];
539 printf("atapi_action: hcb@%p: %s\n", hcb,
540 scsi_cdb_string(request->u.atapi.ccb, cdb_str, sizeof(cdb_str)));
544 len = csio->dxfer_len;
545 buf = csio->data_ptr;
547 /* some SCSI commands require special processing */
548 switch (request->u.atapi.ccb[0]) {
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.
557 struct scsi_inquiry *inq = (struct scsi_inquiry *) &request->u.atapi.ccb[0];
559 if (inq->byte2 == 0 && inq->page_code == 0 &&
560 inq->length > SHORT_INQUIRY_LENGTH) {
562 len = inq->length = SHORT_INQUIRY_LENGTH;
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;
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");
594 request->dev = softc->atadev[tid]->dev;
595 request->driver = hcb;
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;
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);
609 ata_queue_request(request);
614 CAM_DEBUG(ccb_h->path, CAM_DEBUG_SUBTRACE,
615 ("unsupported function code 0x%02x\n", ccb_h->func_code));
623 ata_free_request(request);
626 mtx_unlock(&softc->state_lock);
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);
633 ccb_h->status = CAM_REQUEUE_REQ;
638 mtx_unlock(&softc->state_lock);
639 ccb_h->status = CAM_REQ_INVALID;
645 atapi_poll(struct cam_sim *sim)
647 /* do nothing - we do not actually service any interrupts */
648 printf("atapi_poll called!\n");
652 atapi_cb(struct ata_request *request)
654 struct atapi_xpt_softc *scp;
655 struct atapi_hcb *hcb;
656 struct ccb_scsiio *csio;
659 hcb = (struct atapi_hcb *)request->driver;
661 csio = &hcb->ccb->csio;
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",
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);
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;
682 } else if (request->result != 0) {
683 rc = CAM_SCSI_STATUS_ERROR;
684 csio->scsi_status = SCSI_STATUS_CHECK_COND;
686 if ((csio->ccb_h.flags & CAM_DIS_AUTOSENSE) == 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,
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;
701 ata_queue_request(request);
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_data, &csio->sense_data, sizeof(struct atapi_sense));
708 csio->ccb_h.status |= CAM_AUTOSNS_VALID;
714 csio->scsi_status = SCSI_STATUS_OK;
715 if (((csio->ccb_h.flags & CAM_DIR_MASK) == CAM_DIR_IN) &&
716 hcb->dxfer_alloc != NULL)
718 bcopy(hcb->dxfer_alloc, csio->data_ptr, csio->dxfer_len);
722 mtx_lock(&scp->state_lock);
723 free_hcb_and_ccb_done(hcb, rc);
724 mtx_unlock(&scp->state_lock);
726 ata_free_request(request);
730 free_hcb_and_ccb_done(struct atapi_hcb *hcb, u_int32_t status)
732 struct atapi_xpt_softc *softc;
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;
748 status | (ccb->ccb_h.status & ~(CAM_STATUS_MASK | CAM_SIM_QUEUED));
753 atapi_async(void *callback_arg, u_int32_t code,
754 struct cam_path* path, void *arg)
756 struct atapi_xpt_softc *softc;
762 sim = (struct cam_sim *) callback_arg;
763 softc = (struct atapi_xpt_softc *) cam_sim_softc(sim);
766 targ = xpt_path_target_id(path);
767 xpt_print_path(path);
769 printf("Lost host adapter\n");
771 printf("Lost target %d???\n", targ);
780 cam_rescan_callback(struct cam_periph *periph, union ccb *ccb)
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));
786 CAM_DEBUG(ccb->ccb_h.path, CAM_DEBUG_TRACE,
787 ("Rescan succeeded\n"));
789 xpt_free_path(ccb->ccb_h.path);
794 cam_rescan(struct cam_sim *sim)
796 struct cam_path *path;
797 union ccb *ccb = malloc(sizeof(union ccb), M_ATACAM, M_WAITOK | M_ZERO);
800 if (xpt_create_path(&path, xpt_periph, cam_sim_path(sim),
801 CAM_TARGET_WILDCARD, CAM_LUN_WILDCARD) != CAM_REQ_CMP) {
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;
813 /* scan is in progress now */
817 static struct atapi_hcb *
818 allocate_hcb(struct atapi_xpt_softc *softc, int unit, int bus, union ccb *ccb)
820 struct atapi_hcb *hcb = (struct atapi_hcb *)
821 malloc(sizeof(struct atapi_hcb), M_ATACAM, M_NOWAIT | M_ZERO);
833 free_hcb(struct atapi_hcb *hcb)
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);
843 free_softc(struct atapi_xpt_softc *scp)
845 struct atapi_hcb *hcb;
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);
852 mtx_unlock(&scp->state_lock);
854 if (scp->path != NULL) {
855 setup_async_cb(scp, 0);
856 xpt_free_path(scp->path);
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;
862 if (scp->sim != NULL) {
863 if ((scp->flags & BUS_REGISTERED) == 0)
864 cam_sim_free(scp->sim, /*free_devq*/TRUE);
866 printf("Can't free %s SIM (still registered)\n",
867 cam_sim_name(scp->sim));
870 mtx_destroy(&scp->state_lock);
875 atapi_cam_event_handler(module_t mod, int what, void *arg) {
881 if (devclass_get_devices(atapi_cam_devclass, &devlist, &devcount)
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);
889 device_delete_child(device_get_parent(child),child);
893 free(devlist, M_TEMP);