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: src/sys/dev/ata/atapi-cam.c,v 1.35 2004/06/17 07:29:56 green Exp $");
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/bus.h>
46 #include <cam/cam_ccb.h>
47 #include <cam/cam_periph.h>
48 #include <cam/cam_sim.h>
49 #include <cam/cam_xpt_sim.h>
50 #include <cam/cam_debug.h>
51 #include <cam/scsi/scsi_all.h>
53 #include <dev/ata/ata-all.h>
55 /* hardware command descriptor block */
57 struct atapi_xpt_softc *softc;
65 #define AUTOSENSE 0x0002
67 TAILQ_ENTRY(atapi_hcb) chain;
70 /* private data associated with an ATA bus */
71 struct atapi_xpt_softc {
72 struct ata_channel *ata_ch;
73 struct cam_path *path;
76 #define BUS_REGISTERED 0x01
77 #define RESOURCE_SHORTAGE 0x02
79 TAILQ_HEAD(,atapi_hcb) pending_hcbs;
80 LIST_ENTRY(atapi_xpt_softc) chain;
83 enum reinit_reason { BOOT_ATTACH, ATTACH, RESET };
85 static struct mtx atapicam_softc_mtx;
86 static LIST_HEAD(,atapi_xpt_softc) all_buses = LIST_HEAD_INITIALIZER(all_buses);
89 static void atapi_action(struct cam_sim *, union ccb *);
90 static void atapi_poll(struct cam_sim *);
91 static void atapi_async(void *, u_int32_t, struct cam_path *, void *);
92 static void atapi_async1(void *, u_int32_t, struct cam_path *, void *);
93 static void atapi_cb(struct ata_request *);
95 /* internal functions */
96 static void reinit_bus(struct atapi_xpt_softc *scp, enum reinit_reason reason);
97 static void setup_dev(struct atapi_xpt_softc *, struct ata_device *);
98 static void setup_async_cb(struct atapi_xpt_softc *, uint32_t);
99 static void cam_rescan_callback(struct cam_periph *, union ccb *);
100 static void cam_rescan(struct cam_sim *);
101 static void free_hcb_and_ccb_done(struct atapi_hcb *, u_int32_t);
102 static struct atapi_hcb *allocate_hcb(struct atapi_xpt_softc *, int, int, union ccb *);
103 static void free_hcb(struct atapi_hcb *hcb);
104 static void free_softc(struct atapi_xpt_softc *scp);
105 static struct atapi_xpt_softc *get_softc(struct ata_channel *ata_ch);
106 static struct ata_device *get_ata_device(struct atapi_xpt_softc *scp, int id);
108 static MALLOC_DEFINE(M_ATACAM, "ATA CAM transport", "ATA driver CAM-XPT layer");
111 atapi_cam_attach_bus(struct ata_channel *ata_ch)
113 struct atapi_xpt_softc *scp = NULL;
114 struct cam_devq *devq = NULL;
115 struct cam_sim *sim = NULL;
116 struct cam_path *path = NULL;
121 if (mtx_initialized(&atapicam_softc_mtx) == 0)
122 mtx_init(&atapicam_softc_mtx, "ATAPI/CAM softc mutex", NULL, MTX_DEF);
124 mtx_lock(&atapicam_softc_mtx);
126 LIST_FOREACH(scp, &all_buses, chain) {
127 if (scp->ata_ch == ata_ch)
130 mtx_unlock(&atapicam_softc_mtx);
135 if ((scp = malloc(sizeof(struct atapi_xpt_softc),
136 M_ATACAM, M_NOWAIT | M_ZERO)) == NULL)
139 scp->ata_ch = ata_ch;
140 TAILQ_INIT(&scp->pending_hcbs);
141 LIST_INSERT_HEAD(&all_buses, scp, chain);
142 unit = device_get_unit(ata_ch->dev);
144 if ((devq = cam_simq_alloc(16)) == NULL)
147 if ((sim = cam_sim_alloc(atapi_action, atapi_poll, "ata",
148 (void *)scp, unit, 1, 1, devq)) == NULL) {
154 if (xpt_bus_register(sim, 0) != CAM_SUCCESS) {
157 scp->flags |= BUS_REGISTERED;
159 if (xpt_create_path(&path, /*periph*/ NULL,
160 cam_sim_path(sim), CAM_TARGET_WILDCARD,
161 CAM_LUN_WILDCARD) != CAM_REQ_CMP) {
166 CAM_DEBUG(path, CAM_DEBUG_TRACE, ("Registered SIM for ata%d\n", unit));
168 setup_async_cb(scp, AC_LOST_DEVICE);
169 reinit_bus(scp, cold ? BOOT_ATTACH : ATTACH);
177 atapi_cam_detach_bus(struct ata_channel *ata_ch)
179 struct atapi_xpt_softc *scp = get_softc(ata_ch);
187 atapi_cam_reinit_bus(struct ata_channel *ata_ch) {
188 struct atapi_xpt_softc *scp = get_softc(ata_ch);
191 * scp might be null if the bus is being reinitialised during
192 * the boot-up sequence, before the ATAPI bus is registered.
197 reinit_bus(scp, RESET);
203 reinit_bus(struct atapi_xpt_softc *scp, enum reinit_reason reason) {
207 if (scp->ata_ch->devices & ATA_ATAPI_MASTER)
208 setup_dev(scp, &scp->ata_ch->device[MASTER]);
209 if (scp->ata_ch->devices & ATA_ATAPI_SLAVE)
210 setup_dev(scp, &scp->ata_ch->device[SLAVE]);
216 xpt_async(AC_BUS_RESET, scp->path, NULL);
219 cam_rescan(scp->sim);
225 setup_dev(struct atapi_xpt_softc *scp, struct ata_device *atp)
227 if (atp->softc == NULL) {
228 ata_set_name(atp, "atapicam",
229 2 * device_get_unit(atp->channel->dev) +
230 (atp->unit == ATA_MASTER) ? 0 : 1);
231 atp->softc = (void *)scp;
236 setup_async_cb(struct atapi_xpt_softc *scp, uint32_t events)
238 struct ccb_setasync csa;
242 xpt_setup_ccb(&csa.ccb_h, scp->path, /*priority*/ 5);
243 csa.ccb_h.func_code = XPT_SASYNC_CB;
244 csa.event_enable = events;
245 csa.callback = &atapi_async;
246 csa.callback_arg = scp->sim;
247 xpt_action((union ccb *) &csa);
251 atapi_action(struct cam_sim *sim, union ccb *ccb)
253 struct atapi_xpt_softc *softc = (struct atapi_xpt_softc*)cam_sim_softc(sim);
254 struct ccb_hdr *ccb_h = &ccb->ccb_h;
255 struct atapi_hcb *hcb = NULL;
256 struct ata_request *request = NULL;
257 int unit = cam_sim_unit(sim);
258 int bus = cam_sim_bus(sim);
262 switch (ccb_h->func_code) {
264 struct ccb_pathinq *cpi = &ccb->cpi;
266 cpi->version_num = 1;
267 cpi->hba_inquiry = 0;
268 cpi->target_sprt = 0;
269 cpi->hba_misc = PIM_NO_6_BYTE;
270 cpi->hba_eng_cnt = 0;
271 bzero(cpi->vuhba_flags, sizeof(cpi->vuhba_flags));
274 cpi->async_flags = 0;
276 cpi->initiator_id = 7;
277 strncpy(cpi->sim_vid, "FreeBSD", sizeof(cpi->sim_vid));
278 strncpy(cpi->hba_vid, "ATAPI", sizeof(cpi->hba_vid));
279 strncpy(cpi->dev_name, cam_sim_name(sim), sizeof cpi->dev_name);
280 cpi->unit_number = cam_sim_unit(sim);
281 cpi->bus_id = cam_sim_bus(sim);
282 cpi->base_transfer_speed = 3300;
284 if (softc->ata_ch && ccb_h->target_id != CAM_TARGET_WILDCARD) {
285 switch (softc->ata_ch->device[ccb_h->target_id].mode) {
287 cpi->base_transfer_speed = 5200;
290 cpi->base_transfer_speed = 7000;
293 cpi->base_transfer_speed = 11000;
298 cpi->base_transfer_speed = 16000;
301 cpi->base_transfer_speed = 33000;
304 cpi->base_transfer_speed = 66000;
307 cpi->base_transfer_speed = 100000;
310 cpi->base_transfer_speed = 133000;
316 ccb->ccb_h.status = CAM_REQ_CMP;
321 case XPT_RESET_DEV: {
322 int tid = ccb_h->target_id;
323 struct ata_device *dev = get_ata_device(softc, tid);
325 CAM_DEBUG(ccb->ccb_h.path, CAM_DEBUG_SUBTRACE, ("dev reset\n"));
326 ata_controlcmd(dev, ATA_ATAPI_RESET, 0, 0, 0);
327 ccb->ccb_h.status = CAM_REQ_CMP;
333 CAM_DEBUG(ccb->ccb_h.path, CAM_DEBUG_SUBTRACE, ("bus reset\n"));
334 ata_reinit(softc->ata_ch);
335 ccb->ccb_h.status = CAM_REQ_CMP;
339 case XPT_SET_TRAN_SETTINGS:
340 /* ignore these, we're not doing SCSI here */
341 CAM_DEBUG(ccb->ccb_h.path, CAM_DEBUG_SUBTRACE,
342 ("SET_TRAN_SETTINGS not supported\n"));
343 ccb->ccb_h.status = CAM_FUNC_NOTAVAIL;
347 case XPT_GET_TRAN_SETTINGS: {
348 struct ccb_trans_settings *cts = &ccb->cts;
351 * XXX The default CAM transport code is very SCSI-specific and
352 * doesn't understand IDE speeds very well. Be silent about it
353 * here and let it default to what is set in XPT_PATH_INQ
355 CAM_DEBUG(ccb->ccb_h.path, CAM_DEBUG_SUBTRACE, ("GET_TRAN_SETTINGS\n"));
356 cts->valid = (CCB_TRANS_DISC_VALID | CCB_TRANS_TQ_VALID);
357 cts->flags &= ~(CCB_TRANS_DISC_ENB | CCB_TRANS_TAG_ENB);
358 ccb->ccb_h.status = CAM_REQ_CMP;
363 case XPT_CALC_GEOMETRY: {
364 CAM_DEBUG(ccb->ccb_h.path, CAM_DEBUG_SUBTRACE, ("CALC_GEOMETRY\n"));
365 cam_calc_geometry(&ccb->ccg, /*extended*/1);
371 struct ccb_scsiio *csio = &ccb->csio;
372 int tid = ccb_h->target_id, lid = ccb_h->target_lun;
373 struct ata_device *dev = get_ata_device(softc, tid);
374 int request_flags = ATA_R_QUIET | ATA_R_ATAPI;
376 CAM_DEBUG(ccb_h->path, CAM_DEBUG_SUBTRACE, ("XPT_SCSI_IO\n"));
378 /* check that this request was not aborted already */
379 if ((ccb_h->status & CAM_STATUS_MASK) != CAM_REQ_INPROG) {
380 printf("XPT_SCSI_IO received but already in progress?\n");
385 CAM_DEBUG(ccb_h->path, CAM_DEBUG_SUBTRACE,
386 ("SCSI IO received for invalid device\n"));
390 CAM_DEBUG(ccb_h->path, CAM_DEBUG_SUBTRACE,
391 ("SCSI IO received for invalid lun %d\n", lid));
394 if (csio->cdb_len > sizeof request->u.atapi.ccb) {
395 CAM_DEBUG(ccb_h->path, CAM_DEBUG_SUBTRACE,
396 ("CAM CCB too long for ATAPI"));
399 if ((ccb_h->flags & CAM_SCATTER_VALID)) {
400 /* scatter-gather not supported */
401 xpt_print_path(ccb_h->path);
402 printf("ATAPI/CAM does not support scatter-gather yet!\n");
406 switch (ccb_h->flags & CAM_DIR_MASK) {
408 request_flags |= ATA_R_READ|ATA_R_DMA;
411 request_flags |= ATA_R_WRITE|ATA_R_DMA;
414 /* No flags need to be set */
417 ata_prtdev(dev, "unknown IO operation\n");
420 if (dev->mode < ATA_DMA)
421 request_flags &= ~ATA_R_DMA;
423 if ((hcb = allocate_hcb(softc, unit, bus, ccb)) == NULL) {
424 printf("cannot allocate ATAPI/CAM hcb\n");
427 if ((request = ata_alloc_request()) == NULL) {
428 printf("cannot allocate ATAPI/CAM request\n");
432 bcopy((ccb_h->flags & CAM_CDB_POINTER) ?
433 csio->cdb_io.cdb_ptr : csio->cdb_io.cdb_bytes,
434 request->u.atapi.ccb, csio->cdb_len);
436 if (CAM_DEBUGGED(ccb_h->path, CAM_DEBUG_CDB)) {
437 char cdb_str[(SCSI_MAX_CDBLEN * 3) + 1];
439 printf("atapi_action: hcb@%p: %s\n", hcb,
440 scsi_cdb_string(request->u.atapi.ccb, cdb_str, sizeof(cdb_str)));
444 len = csio->dxfer_len;
445 buf = csio->data_ptr;
447 /* some SCSI commands require special processing */
448 switch (request->u.atapi.ccb[0]) {
451 * many ATAPI devices seem to report more than
452 * SHORT_INQUIRY_LENGTH bytes of available INQUIRY
453 * information, but respond with some incorrect condition
454 * when actually asked for it, so we are going to pretend
455 * that only SHORT_INQUIRY_LENGTH are expected, anyway.
457 struct scsi_inquiry *inq = (struct scsi_inquiry *) &request->u.atapi.ccb[0];
459 if (inq->byte2 == 0 && inq->page_code == 0 &&
460 inq->length > SHORT_INQUIRY_LENGTH) {
462 len = inq->length = SHORT_INQUIRY_LENGTH;
470 CAM_DEBUG(ccb_h->path, CAM_DEBUG_SUBTRACE,
471 ("Translating %s into _10 equivalent\n",
472 (request->u.atapi.ccb[0] == READ_6) ? "READ_6" : "WRITE_6"));
473 request->u.atapi.ccb[0] |= 0x20;
474 request->u.atapi.ccb[9] = request->u.atapi.ccb[5];
475 request->u.atapi.ccb[8] = request->u.atapi.ccb[4];
476 request->u.atapi.ccb[7] = 0;
477 request->u.atapi.ccb[6] = 0;
478 request->u.atapi.ccb[5] = request->u.atapi.ccb[3];
479 request->u.atapi.ccb[4] = request->u.atapi.ccb[2];
480 request->u.atapi.ccb[3] = request->u.atapi.ccb[1] & 0x1f;
481 request->u.atapi.ccb[2] = 0;
482 request->u.atapi.ccb[1] = 0;
486 if ((ccb_h->flags & CAM_DIR_MASK) == CAM_DIR_IN && (len & 1)) {
487 /* ATA always transfers an even number of bytes */
488 if ((buf = hcb->dxfer_alloc
489 = malloc(++len, M_ATACAM, M_NOWAIT | M_ZERO)) == NULL) {
490 printf("cannot allocate ATAPI/CAM buffer\n");
494 request->device = dev;
495 request->driver = hcb;
497 request->bytecount = len;
498 request->transfersize = min(request->bytecount, 65534);
499 request->timeout = ccb_h->timeout / 1000; /* XXX lost granularity */
500 request->retries = 2;
501 request->callback = &atapi_cb;
502 request->flags = request_flags;
504 TAILQ_INSERT_TAIL(&softc->pending_hcbs, hcb, chain);
505 hcb->flags |= QUEUED;
506 ccb_h->status |= CAM_SIM_QUEUED;
508 ata_queue_request(request);
513 CAM_DEBUG(ccb_h->path, CAM_DEBUG_SUBTRACE,
514 ("unsupported function code 0x%02x\n", ccb_h->func_code));
522 ata_free_request(request);
525 xpt_print_path(ccb_h->path);
526 printf("out of memory, freezing queue.\n");
527 softc->flags |= RESOURCE_SHORTAGE;
528 xpt_freeze_simq(sim, /*count*/ 1);
529 ccb_h->status = CAM_REQUEUE_REQ;
534 ccb_h->status = CAM_REQ_INVALID;
540 atapi_poll(struct cam_sim *sim)
542 /* do nothing - we do not actually service any interrupts */
543 printf("atapi_poll called!\n");
547 atapi_cb(struct ata_request *request)
549 struct atapi_hcb *hcb;
550 struct ccb_scsiio *csio;
555 hcb = (struct atapi_hcb *)request->driver;
556 csio = &hcb->ccb->csio;
559 # define err (request->u.atapi.sense_key)
560 if (CAM_DEBUGGED(csio->ccb_h.path, CAM_DEBUG_CDB)) {
561 printf("atapi_cb: hcb@%p error = %02x: (sk = %02x%s%s%s)\n",
563 (err & 4) ? " ABRT" : "",
564 (err & 2) ? " EOM" : "",
565 (err & 1) ? " ILI" : "");
566 printf("dev %s: cmd %02x status %02x result %02x\n",
567 request->device->name, request->u.atapi.ccb[0],
568 request->status, request->result);
572 if ((hcb->flags & AUTOSENSE) != 0) {
573 rc = CAM_SCSI_STATUS_ERROR;
574 if (request->result == 0) {
575 csio->ccb_h.status |= CAM_AUTOSNS_VALID;
577 } else if (request->result != 0) {
578 rc = CAM_SCSI_STATUS_ERROR;
579 csio->scsi_status = SCSI_STATUS_CHECK_COND;
581 if ((csio->ccb_h.flags & CAM_DIS_AUTOSENSE) == 0) {
583 static const int8_t ccb[16] = { ATAPI_REQUEST_SENSE, 0, 0, 0,
584 sizeof(struct atapi_sense), 0, 0, 0, 0, 0, 0,
587 bcopy (ccb, request->u.atapi.ccb, sizeof ccb);
588 request->data = (caddr_t)&csio->sense_data;
589 request->bytecount = sizeof(struct atapi_sense);
590 request->transfersize = min(request->bytecount, 65534);
591 request->timeout = csio->ccb_h.timeout / 1000;
592 request->retries = 2;
593 request->flags = ATA_R_QUIET|ATA_R_ATAPI|ATA_R_IMMEDIATE;
594 hcb->flags |= AUTOSENSE;
597 ata_queue_request(request);
600 /* The ATA driver has already requested sense for us. */
601 if (request->error == 0) {
602 /* The ATA autosense suceeded. */
603 bcopy (&request->u.atapi.sense_data, &csio->sense_data, sizeof(struct atapi_sense));
604 csio->ccb_h.status |= CAM_AUTOSNS_VALID;
610 csio->scsi_status = SCSI_STATUS_OK;
611 if (((csio->ccb_h.flags & CAM_DIR_MASK) == CAM_DIR_IN) &&
612 hcb->dxfer_alloc != NULL)
614 bcopy(hcb->dxfer_alloc, csio->data_ptr, csio->dxfer_len);
618 free_hcb_and_ccb_done(hcb, rc);
621 ata_free_request(request);
625 free_hcb_and_ccb_done(struct atapi_hcb *hcb, u_int32_t status)
627 struct atapi_xpt_softc *softc = hcb->softc;
628 union ccb *ccb = hcb->ccb;
633 /* we're about to free a hcb, so the shortage has ended */
634 if (softc->flags & RESOURCE_SHORTAGE) {
635 softc->flags &= ~RESOURCE_SHORTAGE;
636 status |= CAM_RELEASE_SIMQ;
641 status | (ccb->ccb_h.status & ~(CAM_STATUS_MASK | CAM_SIM_QUEUED));
646 atapi_async(void *callback_arg, u_int32_t code,
647 struct cam_path *path, void *arg)
650 atapi_async1(callback_arg, code, path, arg);
655 atapi_async1(void *callback_arg, u_int32_t code,
656 struct cam_path* path, void *arg)
658 struct atapi_xpt_softc *softc;
664 sim = (struct cam_sim *) callback_arg;
665 softc = (struct atapi_xpt_softc *) cam_sim_softc(sim);
668 targ = xpt_path_target_id(path);
669 xpt_print_path(path);
671 printf("Lost host adapter\n");
673 printf("Lost target %d???\n", targ);
682 cam_rescan_callback(struct cam_periph *periph, union ccb *ccb)
684 if (ccb->ccb_h.status != CAM_REQ_CMP) {
685 CAM_DEBUG(ccb->ccb_h.path, CAM_DEBUG_TRACE,
686 ("Rescan failed, 0x%04x\n", ccb->ccb_h.status));
688 CAM_DEBUG(ccb->ccb_h.path, CAM_DEBUG_TRACE,
689 ("Rescan succeeded\n"));
691 xpt_free_path(ccb->ccb_h.path);
696 cam_rescan(struct cam_sim *sim)
698 struct cam_path *path;
699 union ccb *ccb = malloc(sizeof(union ccb), M_ATACAM, M_WAITOK | M_ZERO);
701 if (xpt_create_path(&path, xpt_periph, cam_sim_path(sim),
702 CAM_TARGET_WILDCARD, CAM_LUN_WILDCARD) != CAM_REQ_CMP) {
707 CAM_DEBUG(ccb->ccb_h.path, CAM_DEBUG_TRACE, ("Rescanning ATAPI bus.\n"));
708 xpt_setup_ccb(&ccb->ccb_h, path, 5/*priority (low)*/);
709 ccb->ccb_h.func_code = XPT_SCAN_BUS;
710 ccb->ccb_h.cbfcnp = cam_rescan_callback;
711 ccb->crcn.flags = CAM_FLAG_NONE;
713 /* scan is in progress now */
716 static struct atapi_hcb *
717 allocate_hcb(struct atapi_xpt_softc *softc, int unit, int bus, union ccb *ccb)
719 struct atapi_hcb *hcb = (struct atapi_hcb *)
720 malloc(sizeof(struct atapi_hcb), M_ATACAM, M_NOWAIT | M_ZERO);
732 free_hcb(struct atapi_hcb *hcb)
734 if ((hcb->flags & QUEUED) != 0)
735 TAILQ_REMOVE(&hcb->softc->pending_hcbs, hcb, chain);
736 if (hcb->dxfer_alloc != NULL)
737 free(hcb->dxfer_alloc, M_ATACAM);
742 free_softc(struct atapi_xpt_softc *scp)
744 struct atapi_hcb *hcb;
749 TAILQ_FOREACH(hcb, &scp->pending_hcbs, chain) {
750 free_hcb_and_ccb_done(hcb, CAM_UNREC_HBA_ERROR);
752 if (scp->path != NULL) {
753 setup_async_cb(scp, 0);
754 xpt_free_path(scp->path);
756 if ((scp->flags & BUS_REGISTERED) != 0) {
757 if (xpt_bus_deregister(cam_sim_path(scp->sim)) == CAM_REQ_CMP)
758 scp->flags &= ~BUS_REGISTERED;
760 if (scp->sim != NULL) {
761 if ((scp->flags & BUS_REGISTERED) == 0)
762 cam_sim_free(scp->sim, /*free_devq*/TRUE);
764 printf("Can't free %s SIM (still registered)\n",
765 cam_sim_name(scp->sim));
767 LIST_REMOVE(scp, chain);
772 static struct atapi_xpt_softc *
773 get_softc(struct ata_channel *ata_ch) {
774 struct atapi_xpt_softc *scp = NULL;
776 mtx_lock(&atapicam_softc_mtx);
777 LIST_FOREACH(scp, &all_buses, chain) {
778 if (scp->ata_ch == ata_ch)
781 mtx_unlock(&atapicam_softc_mtx);
785 static struct ata_device *
786 get_ata_device(struct atapi_xpt_softc *scp, int id)
788 int role = ATA_ATAPI_MASTER;
792 role = ATA_ATAPI_SLAVE;
796 if (scp->ata_ch->devices & role)
797 return &scp->ata_ch->device[id];