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.
28 * $FreeBSD: src/sys/dev/ata/atapi-cam.c,v 1.44 2006/03/31 08:09:05 sos Exp $
34 #include <sys/param.h>
36 #include <sys/libkern.h>
37 #include <sys/lock.h> /* for {get,rel}_mplock() */
38 #include <sys/malloc.h>
39 #include <sys/module.h>
41 #include <sys/queue.h>
42 #include <sys/systm.h>
44 #include <sys/thread2.h>
45 #include <sys/mplock2.h>
47 #include <bus/cam/cam.h>
48 #include <bus/cam/cam_ccb.h>
49 #include <bus/cam/cam_periph.h>
50 #include <bus/cam/cam_sim.h>
51 #include <bus/cam/cam_xpt_sim.h>
52 #include <bus/cam/cam_debug.h>
53 #include <bus/cam/scsi/scsi_all.h>
58 /* private data associated with an ATA bus */
59 struct atapi_xpt_softc
{
60 struct ata_device atapi_cam_dev
; /* must be first */
63 struct ata_channel
*ata_ch
;
64 struct cam_path
*path
;
67 #define BUS_REGISTERED 0x01
68 #define RESOURCE_SHORTAGE 0x02
69 #define DETACHING 0x04
71 TAILQ_HEAD(,atapi_hcb
) pending_hcbs
;
72 struct ata_device
*atadev
[2];
73 struct lock state_lock
;
76 /* hardware command descriptor block */
78 struct atapi_xpt_softc
*softc
;
86 #define AUTOSENSE 0x0002
88 TAILQ_ENTRY(atapi_hcb
) chain
;
91 enum reinit_reason
{ ATTACH
, RESET
};
94 static void atapi_cam_identify(device_t
*dev
, device_t parent
);
95 static int atapi_cam_probe(device_t dev
);
96 static int atapi_cam_attach(device_t dev
);
97 static int atapi_cam_detach(device_t dev
);
98 static int atapi_cam_reinit(device_t dev
);
100 /* CAM XPT methods */
101 static void atapi_action(struct cam_sim
*, union ccb
*);
102 static void atapi_poll(struct cam_sim
*);
103 static void atapi_async(void *, u_int32_t
, struct cam_path
*, void *);
104 static void atapi_cb(struct ata_request
*);
107 static int atapi_cam_event_handler(module_t mod
, int what
, void *arg
);
109 /* internal functions */
110 static void reinit_bus(struct atapi_xpt_softc
*scp
, enum reinit_reason reason
);
111 static void setup_async_cb(struct atapi_xpt_softc
*, uint32_t);
112 static void cam_rescan_callback(struct cam_periph
*, union ccb
*);
113 static void cam_rescan(struct cam_sim
*);
114 static void free_hcb_and_ccb_done(struct atapi_hcb
*, u_int32_t
);
115 static struct atapi_hcb
*allocate_hcb(struct atapi_xpt_softc
*, int, int, union ccb
*);
116 static void free_hcb(struct atapi_hcb
*hcb
);
117 static void free_softc(struct atapi_xpt_softc
*scp
);
119 static MALLOC_DEFINE(M_ATACAM
, "ata_cam", "ATA driver CAM-XPT layer");
121 static device_method_t atapi_cam_methods
[] = {
122 DEVMETHOD(device_identify
, atapi_cam_identify
),
123 DEVMETHOD(device_probe
, atapi_cam_probe
),
124 DEVMETHOD(device_attach
, atapi_cam_attach
),
125 DEVMETHOD(device_detach
, atapi_cam_detach
),
126 DEVMETHOD(ata_reinit
, atapi_cam_reinit
),
130 static driver_t atapi_cam_driver
= {
133 sizeof(struct atapi_xpt_softc
)
136 static devclass_t atapi_cam_devclass
;
137 DRIVER_MODULE(atapicam
, ata
,
140 atapi_cam_event_handler
,
142 MODULE_VERSION(atapicam
, 1);
143 MODULE_DEPEND(atapicam
, ata
, 1, 1, 1);
144 MODULE_DEPEND(atapicam
, cam
, 1, 1, 1);
147 atapi_cam_identify(device_t
*dev
, device_t parent
)
149 struct atapi_xpt_softc
*scp
=
150 kmalloc(sizeof(struct atapi_xpt_softc
), M_ATACAM
, M_INTWAIT
|M_ZERO
);
153 /* Assume one atapicam instance per parent channel instance. */
154 child
= device_add_child(parent
, "atapicam", -1);
156 /* XXX TGEN Use device_printf()? */
157 kprintf("atapi_cam_identify: out of memory, can't add child");
158 kfree(scp
, M_ATACAM
);
161 scp
->atapi_cam_dev
.unit
= -1;
162 scp
->atapi_cam_dev
.dev
= child
;
164 device_set_softc(child
, scp
);
168 atapi_cam_probe(device_t dev
)
170 struct ata_device
*atadev
= device_get_softc (dev
);
172 KASSERT(atadev
!= NULL
, ("expect valid struct ata_device"));
173 if (atadev
->unit
< 0) {
174 device_set_desc(dev
, "ATAPI CAM Attachment");
182 atapi_cam_attach(device_t dev
)
184 struct atapi_xpt_softc
*scp
= NULL
;
185 struct cam_devq
*devq
= NULL
;
186 struct cam_sim
*sim
= NULL
;
187 struct cam_path
*path
= NULL
;
190 scp
= (struct atapi_xpt_softc
*)device_get_softc(dev
);
192 device_printf(dev
, "Cannot get softc\n");
196 lockinit(&scp
->state_lock
, "atapicamattach", 0, 0);
199 scp
->parent
= device_get_parent(dev
);
200 scp
->ata_ch
= device_get_softc(scp
->parent
);
201 TAILQ_INIT(&scp
->pending_hcbs
);
202 unit
= device_get_unit(dev
);
204 if ((devq
= cam_simq_alloc(16)) == NULL
) {
209 if ((sim
= cam_sim_alloc(atapi_action
, atapi_poll
, "ata",
210 (void *)scp
, unit
, &sim_mplock
, 1, 1, devq
)) == NULL
) {
216 if (xpt_bus_register(sim
, 0) != CAM_SUCCESS
) {
220 scp
->flags
|= BUS_REGISTERED
;
222 if (xpt_create_path(&path
, /*periph*/ NULL
,
223 cam_sim_path(sim
), CAM_TARGET_WILDCARD
,
224 CAM_LUN_WILDCARD
) != CAM_REQ_CMP
) {
230 CAM_DEBUG(path
, CAM_DEBUG_TRACE
, ("Registered SIM for ata%d\n", unit
));
232 setup_async_cb(scp
, AC_LOST_DEVICE
);
233 reinit_bus(scp
, ATTACH
);
244 atapi_cam_detach(device_t dev
)
246 struct atapi_xpt_softc
*scp
= device_get_softc(dev
);
249 xpt_freeze_simq(scp
->sim
, 1 /*count*/);
251 lockmgr(&scp
->state_lock
, LK_EXCLUSIVE
);
252 scp
->flags
|= DETACHING
;
253 lockmgr(&scp
->state_lock
, LK_RELEASE
);
259 atapi_cam_reinit(device_t dev
) {
260 struct atapi_xpt_softc
*scp
= device_get_softc(dev
);
263 * scp might be null if the bus is being reinitialised during
264 * the boot-up sequence, before the ATAPI bus is registered.
268 reinit_bus(scp
, RESET
);
274 reinit_bus(struct atapi_xpt_softc
*scp
, enum reinit_reason reason
) {
275 struct ata_device
*old_atadev
[2], *atadev
;
277 int nchildren
, i
, dev_changed
;
279 if (device_get_children(scp
->parent
, &children
, &nchildren
) != 0) {
283 lockmgr(&scp
->state_lock
, LK_EXCLUSIVE
);
284 old_atadev
[0] = scp
->atadev
[0];
285 old_atadev
[1] = scp
->atadev
[1];
286 scp
->atadev
[0] = NULL
;
287 scp
->atadev
[1] = NULL
;
289 for (i
= 0; i
< nchildren
; i
++) {
290 /* XXX Does the child need to actually be attached yet? */
291 if (children
[i
] != NULL
) {
292 atadev
= device_get_softc(children
[i
]);
293 if ((atadev
->unit
== ATA_MASTER
) &&
294 (scp
->ata_ch
->devices
& ATA_ATAPI_MASTER
) != 0)
295 scp
->atadev
[0] = atadev
;
296 if ((atadev
->unit
== ATA_SLAVE
) &&
297 (scp
->ata_ch
->devices
& ATA_ATAPI_SLAVE
) != 0)
298 scp
->atadev
[1] = atadev
;
301 dev_changed
= (old_atadev
[0] != scp
->atadev
[0])
302 || (old_atadev
[1] != scp
->atadev
[1]);
303 lockmgr(&scp
->state_lock
, LK_RELEASE
);
304 kfree(children
, M_TEMP
);
308 xpt_async(AC_BUS_RESET
, scp
->path
, NULL
);
315 cam_rescan(scp
->sim
);
321 setup_async_cb(struct atapi_xpt_softc
*scp
, uint32_t events
)
323 struct ccb_setasync csa
;
326 xpt_setup_ccb(&csa
.ccb_h
, scp
->path
, /*priority*/ 5);
327 csa
.ccb_h
.func_code
= XPT_SASYNC_CB
;
328 csa
.event_enable
= events
;
329 csa
.callback
= &atapi_async
;
330 csa
.callback_arg
= scp
->sim
;
331 xpt_action((union ccb
*) &csa
);
336 atapi_action(struct cam_sim
*sim
, union ccb
*ccb
)
338 struct atapi_xpt_softc
*softc
= (struct atapi_xpt_softc
*)cam_sim_softc(sim
);
339 struct ccb_hdr
*ccb_h
= &ccb
->ccb_h
;
340 struct atapi_hcb
*hcb
= NULL
;
341 struct ata_request
*request
= NULL
;
342 int unit
= cam_sim_unit(sim
);
343 int bus
= cam_sim_bus(sim
);
347 switch (ccb_h
->func_code
) {
349 struct ccb_pathinq
*cpi
= &ccb
->cpi
;
350 int tid
= ccb_h
->target_id
;
352 cpi
->version_num
= 1;
353 cpi
->hba_inquiry
= 0;
354 cpi
->target_sprt
= 0;
355 cpi
->hba_misc
= PIM_NO_6_BYTE
;
356 cpi
->hba_eng_cnt
= 0;
357 bzero(cpi
->vuhba_flags
, sizeof(cpi
->vuhba_flags
));
360 cpi
->async_flags
= 0;
362 cpi
->initiator_id
= 7;
363 strncpy(cpi
->sim_vid
, "FreeBSD", sizeof(cpi
->sim_vid
));
364 strncpy(cpi
->hba_vid
, "ATAPI", sizeof(cpi
->hba_vid
));
365 strncpy(cpi
->dev_name
, cam_sim_name(sim
), sizeof cpi
->dev_name
);
366 cpi
->unit_number
= cam_sim_unit(sim
);
367 cpi
->bus_id
= cam_sim_bus(sim
);
368 cpi
->base_transfer_speed
= 3300;
369 cpi
->transport
= XPORT_ATA
;
370 cpi
->transport_version
= 2;
371 cpi
->protocol
= PROTO_SCSI
;
372 cpi
->protocol_version
= SCSI_REV_2
;
374 if (softc
->ata_ch
&& tid
!= CAM_TARGET_WILDCARD
) {
375 lockmgr(&softc
->state_lock
, LK_EXCLUSIVE
);
376 if (softc
->atadev
[tid
] == NULL
) {
377 ccb
->ccb_h
.status
= CAM_DEV_NOT_THERE
;
379 lockmgr(&softc
->state_lock
, LK_RELEASE
);
382 switch (softc
->atadev
[ccb_h
->target_id
]->mode
) {
384 cpi
->base_transfer_speed
= 5200;
387 cpi
->base_transfer_speed
= 7000;
390 cpi
->base_transfer_speed
= 11000;
395 cpi
->base_transfer_speed
= 16000;
398 cpi
->base_transfer_speed
= 33000;
401 cpi
->base_transfer_speed
= 66000;
404 cpi
->base_transfer_speed
= 100000;
407 cpi
->base_transfer_speed
= 133000;
412 lockmgr(&softc
->state_lock
, LK_RELEASE
);
414 ccb
->ccb_h
.status
= CAM_REQ_CMP
;
419 case XPT_RESET_DEV
: {
420 int tid
= ccb_h
->target_id
;
422 CAM_DEBUG(ccb
->ccb_h
.path
, CAM_DEBUG_SUBTRACE
, ("dev reset\n"));
423 ata_controlcmd(softc
->atadev
[tid
]->dev
, ATA_DEVICE_RESET
, 0, 0, 0);
424 ccb
->ccb_h
.status
= CAM_REQ_CMP
;
430 CAM_DEBUG(ccb
->ccb_h
.path
, CAM_DEBUG_SUBTRACE
, ("bus reset\n"));
431 ata_reinit(softc
->parent
);
432 ccb
->ccb_h
.status
= CAM_REQ_CMP
;
436 case XPT_SET_TRAN_SETTINGS
:
437 /* ignore these, we're not doing SCSI here */
438 CAM_DEBUG(ccb
->ccb_h
.path
, CAM_DEBUG_SUBTRACE
,
439 ("SET_TRAN_SETTINGS not supported\n"));
440 ccb
->ccb_h
.status
= CAM_FUNC_NOTAVAIL
;
444 case XPT_GET_TRAN_SETTINGS
: {
445 struct ccb_trans_settings
*cts
= &ccb
->cts
;
446 cts
->protocol
= PROTO_SCSI
;
447 cts
->protocol_version
= SCSI_REV_2
;
448 cts
->transport
= XPORT_ATA
;
449 cts
->transport_version
= XPORT_VERSION_UNSPECIFIED
;
450 cts
->proto_specific
.valid
= 0;
451 cts
->xport_specific
.valid
= 0;
452 /* nothing more to do */
453 ccb
->ccb_h
.status
= CAM_REQ_CMP
;
454 CAM_DEBUG(ccb
->ccb_h
.path
, CAM_DEBUG_SUBTRACE
, ("GET_TRAN_SETTINGS\n"));
459 case XPT_CALC_GEOMETRY
: {
460 CAM_DEBUG(ccb
->ccb_h
.path
, CAM_DEBUG_SUBTRACE
, ("CALC_GEOMETRY\n"));
461 cam_calc_geometry(&ccb
->ccg
, /*extended*/1);
467 struct ccb_scsiio
*csio
= &ccb
->csio
;
468 int tid
= ccb_h
->target_id
, lid
= ccb_h
->target_lun
;
469 int request_flags
= ATA_R_QUIET
| ATA_R_ATAPI
;
471 CAM_DEBUG(ccb_h
->path
, CAM_DEBUG_SUBTRACE
, ("XPT_SCSI_IO\n"));
473 lockmgr(&softc
->state_lock
, LK_EXCLUSIVE
);
474 if (softc
->flags
& DETACHING
) {
475 ccb
->ccb_h
.status
= CAM_REQ_ABORTED
;
477 lockmgr(&softc
->state_lock
, LK_RELEASE
);
481 if (softc
->atadev
[tid
] == NULL
) {
482 ccb
->ccb_h
.status
= CAM_DEV_NOT_THERE
;
484 lockmgr(&softc
->state_lock
, LK_RELEASE
);
488 /* check that this request was not aborted already */
489 if ((ccb_h
->status
& CAM_STATUS_MASK
) != CAM_REQ_INPROG
) {
490 kprintf("XPT_SCSI_IO received but already in progress?\n");
492 lockmgr(&softc
->state_lock
, LK_RELEASE
);
496 CAM_DEBUG(ccb_h
->path
, CAM_DEBUG_SUBTRACE
,
497 ("SCSI IO received for invalid lun %d\n", lid
));
500 if (csio
->cdb_len
> sizeof request
->u
.atapi
.ccb
) {
501 CAM_DEBUG(ccb_h
->path
, CAM_DEBUG_SUBTRACE
,
502 ("CAM CCB too long for ATAPI"));
505 if ((ccb_h
->flags
& CAM_SCATTER_VALID
)) {
506 /* scatter-gather not supported */
507 xpt_print_path(ccb_h
->path
);
508 kprintf("ATAPI/CAM does not support scatter-gather yet!\n");
512 switch (ccb_h
->flags
& CAM_DIR_MASK
) {
514 request_flags
|= ATA_R_READ
;
517 request_flags
|= ATA_R_WRITE
;
520 /* No flags need to be set */
523 device_printf(softc
->dev
, "unknown IO operation\n");
527 if ((hcb
= allocate_hcb(softc
, unit
, bus
, ccb
)) == NULL
) {
528 kprintf("cannot allocate ATAPI/CAM hcb\n");
531 if ((request
= ata_alloc_request()) == NULL
) {
532 kprintf("cannot allocate ATAPI/CAM request\n");
536 bcopy((ccb_h
->flags
& CAM_CDB_POINTER
) ?
537 csio
->cdb_io
.cdb_ptr
: csio
->cdb_io
.cdb_bytes
,
538 request
->u
.atapi
.ccb
, csio
->cdb_len
);
540 if (CAM_DEBUGGED(ccb_h
->path
, CAM_DEBUG_CDB
)) {
541 char cdb_str
[(SCSI_MAX_CDBLEN
* 3) + 1];
543 kprintf("atapi_action: hcb@%p: %s\n", hcb
,
544 scsi_cdb_string(request
->u
.atapi
.ccb
, cdb_str
, sizeof(cdb_str
)));
546 if (CAM_DEBUGGED(ccb_h
->path
, CAM_DEBUG_SUBTRACE
)) {
547 request_flags
|= ATA_R_DEBUG
;
551 len
= csio
->dxfer_len
;
552 buf
= csio
->data_ptr
;
554 /* some SCSI commands require special processing */
555 switch (request
->u
.atapi
.ccb
[0]) {
558 * many ATAPI devices seem to report more than
559 * SHORT_INQUIRY_LENGTH bytes of available INQUIRY
560 * information, but respond with some incorrect condition
561 * when actually asked for it, so we are going to pretend
562 * that only SHORT_INQUIRY_LENGTH are expected, anyway.
564 struct scsi_inquiry
*inq
= (struct scsi_inquiry
*) &request
->u
.atapi
.ccb
[0];
566 if (inq
->byte2
== 0 && inq
->page_code
== 0 &&
567 inq
->length
> SHORT_INQUIRY_LENGTH
) {
569 len
= inq
->length
= SHORT_INQUIRY_LENGTH
;
577 CAM_DEBUG(ccb_h
->path
, CAM_DEBUG_SUBTRACE
,
578 ("Translating %s into _10 equivalent\n",
579 (request
->u
.atapi
.ccb
[0] == READ_6
) ? "READ_6" : "WRITE_6"));
580 request
->u
.atapi
.ccb
[0] |= 0x20;
581 request
->u
.atapi
.ccb
[9] = request
->u
.atapi
.ccb
[5];
582 request
->u
.atapi
.ccb
[8] = request
->u
.atapi
.ccb
[4];
583 request
->u
.atapi
.ccb
[7] = 0;
584 request
->u
.atapi
.ccb
[6] = 0;
585 request
->u
.atapi
.ccb
[5] = request
->u
.atapi
.ccb
[3];
586 request
->u
.atapi
.ccb
[4] = request
->u
.atapi
.ccb
[2];
587 request
->u
.atapi
.ccb
[3] = request
->u
.atapi
.ccb
[1] & 0x1f;
588 request
->u
.atapi
.ccb
[2] = 0;
589 request
->u
.atapi
.ccb
[1] = 0;
593 if ((ccb_h
->flags
& CAM_DIR_MASK
) == CAM_DIR_IN
&& (len
& 1)) {
594 /* ATA always transfers an even number of bytes */
595 buf
= hcb
->dxfer_alloc
=
596 kmalloc(++len
, M_ATACAM
, M_INTWAIT
| M_ZERO
);
600 * Don't allow DMA for requests with length not multiple of 16 bytes.
601 * Some ATAPI devices don't like it.
603 if ((softc
->atadev
[tid
]->mode
>= ATA_DMA
) && len
> 0 && !(len
& 15))
604 request_flags
|= ATA_R_DMA
;
606 request
->dev
= softc
->atadev
[tid
]->dev
;
607 request
->driver
= hcb
;
609 request
->bytecount
= len
;
610 request
->transfersize
= min(request
->bytecount
,
611 min(softc
->ata_ch
->dma
->max_iosize
, 65534));
612 request
->timeout
= ccb_h
->timeout
/ 1000; /* XXX lost granularity */
613 request
->callback
= &atapi_cb
;
614 request
->flags
= request_flags
;
617 * no retries are to be performed at the ATA level; any retries
618 * will be done by CAM .
620 request
->retries
= 0;
622 TAILQ_INSERT_TAIL(&softc
->pending_hcbs
, hcb
, chain
);
623 hcb
->flags
|= QUEUED
;
624 ccb_h
->status
|= CAM_SIM_QUEUED
;
625 lockmgr(&softc
->state_lock
, LK_RELEASE
);
627 ata_queue_request(request
);
632 CAM_DEBUG(ccb_h
->path
, CAM_DEBUG_SUBTRACE
,
633 ("unsupported function code 0x%02x\n", ccb_h
->func_code
));
641 ata_free_request(request
);
644 lockmgr(&softc
->state_lock
, LK_RELEASE
);
646 xpt_print_path(ccb_h
->path
);
647 kprintf("out of memory, freezing queue.\n");
648 softc
->flags
|= RESOURCE_SHORTAGE
;
649 xpt_freeze_simq(sim
, /*count*/ 1);
651 ccb_h
->status
= CAM_REQUEUE_REQ
;
656 lockmgr(&softc
->state_lock
, LK_RELEASE
);
657 ccb_h
->status
= CAM_REQ_INVALID
;
663 atapi_poll(struct cam_sim
*sim
)
665 /* do nothing - we do not actually service any interrupts */
666 kprintf("atapi_poll called!\n");
670 atapi_cb(struct ata_request
*request
)
672 struct atapi_xpt_softc
*scp
;
673 struct atapi_hcb
*hcb
;
674 struct ccb_scsiio
*csio
;
677 hcb
= (struct atapi_hcb
*)request
->driver
;
679 csio
= &hcb
->ccb
->csio
;
682 # define err (request->u.atapi.sense.key)
683 if (CAM_DEBUGGED(csio
->ccb_h
.path
, CAM_DEBUG_CDB
)) {
684 kprintf("atapi_cb: hcb@%p sense = %02x: sk = %01x%s%s%s\n",
685 hcb
, err
, err
& 0x0f,
686 (err
& 0x80) ? ", Filemark" : "",
687 (err
& 0x40) ? ", EOM" : "",
688 (err
& 0x20) ? ", ILI" : "");
689 device_printf(request
->dev
,
690 "cmd %s status %02x result %02x error %02x\n",
691 ata_cmd2str(request
),
692 request
->status
, request
->result
, request
->error
);
696 if ((hcb
->flags
& AUTOSENSE
) != 0) {
697 rc
= CAM_SCSI_STATUS_ERROR
;
698 if (request
->result
== 0) {
699 csio
->ccb_h
.status
|= CAM_AUTOSNS_VALID
;
701 } else if (request
->result
!= 0) {
702 if ((request
->flags
& ATA_R_TIMEOUT
) != 0) {
703 rc
= CAM_CMD_TIMEOUT
;
705 rc
= CAM_SCSI_STATUS_ERROR
;
706 csio
->scsi_status
= SCSI_STATUS_CHECK_COND
;
708 if ((csio
->ccb_h
.flags
& CAM_DIS_AUTOSENSE
) == 0) {
710 static const int8_t ccb
[16] = { ATAPI_REQUEST_SENSE
, 0, 0, 0,
711 sizeof(struct atapi_sense
), 0, 0, 0, 0, 0, 0,
714 bcopy (ccb
, request
->u
.atapi
.ccb
, sizeof ccb
);
715 request
->data
= (caddr_t
)&csio
->sense_data
;
716 request
->bytecount
= sizeof(struct atapi_sense
);
717 request
->transfersize
= min(request
->bytecount
, 65534);
718 request
->timeout
= csio
->ccb_h
.timeout
/ 1000;
719 request
->retries
= 2;
720 request
->flags
= ATA_R_QUIET
|ATA_R_ATAPI
|ATA_R_IMMEDIATE
;
721 hcb
->flags
|= AUTOSENSE
;
723 ata_queue_request(request
);
727 * Use auto-sense data from the ATA layer, if it has
728 * issued a REQUEST SENSE automatically and that operation
729 * returned without error.
731 if (request
->u
.atapi
.sense
.key
!= 0 && request
->error
== 0) {
732 bcopy (&request
->u
.atapi
.sense
, &csio
->sense_data
, sizeof(struct atapi_sense
));
733 csio
->ccb_h
.status
|= CAM_AUTOSNS_VALID
;
740 csio
->scsi_status
= SCSI_STATUS_OK
;
741 if (((csio
->ccb_h
.flags
& CAM_DIR_MASK
) == CAM_DIR_IN
) &&
742 hcb
->dxfer_alloc
!= NULL
)
744 bcopy(hcb
->dxfer_alloc
, csio
->data_ptr
, csio
->dxfer_len
);
748 lockmgr(&scp
->state_lock
, LK_EXCLUSIVE
);
749 free_hcb_and_ccb_done(hcb
, rc
);
750 lockmgr(&scp
->state_lock
, LK_RELEASE
);
752 ata_free_request(request
);
756 free_hcb_and_ccb_done(struct atapi_hcb
*hcb
, u_int32_t status
)
758 struct atapi_xpt_softc
*softc
;
767 /* we're about to free a hcb, so the shortage has ended */
768 if (softc
->flags
& RESOURCE_SHORTAGE
) {
769 softc
->flags
&= ~RESOURCE_SHORTAGE
;
770 status
|= CAM_RELEASE_SIMQ
;
774 status
| (ccb
->ccb_h
.status
& ~(CAM_STATUS_MASK
| CAM_SIM_QUEUED
));
779 atapi_async(void *callback_arg
, u_int32_t code
,
780 struct cam_path
* path
, void *arg
)
788 targ
= xpt_path_target_id(path
);
789 xpt_print_path(path
);
791 kprintf("Lost host adapter\n");
793 kprintf("Lost target %d???\n", targ
);
804 cam_rescan_callback(struct cam_periph
*periph
, union ccb
*ccb
)
806 if (ccb
->ccb_h
.status
!= CAM_REQ_CMP
) {
807 CAM_DEBUG(ccb
->ccb_h
.path
, CAM_DEBUG_TRACE
,
808 ("Rescan failed, 0x%04x\n", ccb
->ccb_h
.status
));
810 CAM_DEBUG(ccb
->ccb_h
.path
, CAM_DEBUG_TRACE
,
811 ("Rescan succeeded\n"));
813 xpt_free_path(ccb
->ccb_h
.path
);
814 kfree(ccb
, M_ATACAM
);
818 cam_rescan(struct cam_sim
*sim
)
820 struct cam_path
*path
;
821 union ccb
*ccb
= kmalloc(sizeof(union ccb
), M_ATACAM
, M_WAITOK
| M_ZERO
);
824 if (xpt_create_path(&path
, xpt_periph
, cam_sim_path(sim
),
825 CAM_TARGET_WILDCARD
, CAM_LUN_WILDCARD
) != CAM_REQ_CMP
) {
827 kfree(ccb
, M_ATACAM
);
831 CAM_DEBUG(path
, CAM_DEBUG_TRACE
, ("Rescanning ATAPI bus.\n"));
832 xpt_setup_ccb(&ccb
->ccb_h
, path
, 5/*priority (low)*/);
833 ccb
->ccb_h
.func_code
= XPT_SCAN_BUS
;
834 ccb
->ccb_h
.cbfcnp
= cam_rescan_callback
;
835 ccb
->crcn
.flags
= CAM_FLAG_NONE
;
837 /* scan is in progress now */
841 static struct atapi_hcb
*
842 allocate_hcb(struct atapi_xpt_softc
*softc
, int unit
, int bus
, union ccb
*ccb
)
844 struct atapi_hcb
*hcb
= (struct atapi_hcb
*)
845 kmalloc(sizeof(struct atapi_hcb
), M_ATACAM
, M_INTWAIT
| M_ZERO
);
855 free_hcb(struct atapi_hcb
*hcb
)
857 if ((hcb
->flags
& QUEUED
) != 0)
858 TAILQ_REMOVE(&hcb
->softc
->pending_hcbs
, hcb
, chain
);
859 if (hcb
->dxfer_alloc
!= NULL
)
860 kfree(hcb
->dxfer_alloc
, M_ATACAM
);
861 kfree(hcb
, M_ATACAM
);
865 free_softc(struct atapi_xpt_softc
*scp
)
867 struct atapi_hcb
*hcb
;
870 lockmgr(&scp
->state_lock
, LK_EXCLUSIVE
);
871 TAILQ_FOREACH(hcb
, &scp
->pending_hcbs
, chain
) {
872 free_hcb_and_ccb_done(hcb
, CAM_UNREC_HBA_ERROR
);
874 lockmgr(&scp
->state_lock
, LK_RELEASE
);
876 if (scp
->path
!= NULL
) {
877 setup_async_cb(scp
, 0);
878 xpt_free_path(scp
->path
);
880 if ((scp
->flags
& BUS_REGISTERED
) != 0) {
881 if (xpt_bus_deregister(cam_sim_path(scp
->sim
)) == CAM_REQ_CMP
)
882 scp
->flags
&= ~BUS_REGISTERED
;
884 if (scp
->sim
!= NULL
) {
885 if ((scp
->flags
& BUS_REGISTERED
) == 0)
886 cam_sim_free(scp
->sim
);
888 kprintf("Can't free %s SIM (still registered)\n",
889 cam_sim_name(scp
->sim
));
892 lockuninit(&scp
->state_lock
);
897 atapi_cam_event_handler(module_t mod
, int what
, void *arg
) {
903 if (devclass_get_devices(atapi_cam_devclass
, &devlist
, &devcount
)
906 if (devlist
!= NULL
) {
907 while (devlist
!= NULL
&& devcount
> 0) {
908 device_t child
= devlist
[--devcount
];
909 struct atapi_xpt_softc
*scp
= device_get_softc(child
);
911 device_delete_child(device_get_parent(child
),child
);
913 kfree(scp
, M_ATACAM
);
915 kfree(devlist
, M_TEMP
);