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 $
29 * $DragonFly: src/sys/dev/disk/nata/atapi-cam.c,v 1.10 2008/05/18 20:30:22 pavalos Exp $
35 #include <sys/param.h>
37 #include <sys/libkern.h>
38 #include <sys/lock.h> /* for {get,rel}_mplock() */
39 #include <sys/malloc.h>
40 #include <sys/module.h>
42 #include <sys/spinlock.h>
43 #include <sys/spinlock2.h>
44 #include <sys/queue.h>
45 #include <sys/systm.h>
46 #include <sys/thread2.h>
48 #include <bus/cam/cam.h>
49 #include <bus/cam/cam_ccb.h>
50 #include <bus/cam/cam_periph.h>
51 #include <bus/cam/cam_sim.h>
52 #include <bus/cam/cam_xpt_sim.h>
53 #include <bus/cam/cam_debug.h>
54 #include <bus/cam/scsi/scsi_all.h>
59 /* private data associated with an ATA bus */
60 struct atapi_xpt_softc
{
61 struct ata_device atapi_cam_dev
; /* must be first */
64 struct ata_channel
*ata_ch
;
65 struct cam_path
*path
;
68 #define BUS_REGISTERED 0x01
69 #define RESOURCE_SHORTAGE 0x02
70 #define DETACHING 0x04
72 TAILQ_HEAD(,atapi_hcb
) pending_hcbs
;
73 struct ata_device
*atadev
[2];
74 struct spinlock state_lock
;
77 /* hardware command descriptor block */
79 struct atapi_xpt_softc
*softc
;
87 #define AUTOSENSE 0x0002
89 TAILQ_ENTRY(atapi_hcb
) chain
;
92 enum reinit_reason
{ ATTACH
, RESET
};
95 static void atapi_cam_identify(device_t
*dev
, device_t parent
);
96 static int atapi_cam_probe(device_t dev
);
97 static int atapi_cam_attach(device_t dev
);
98 static int atapi_cam_detach(device_t dev
);
99 static int atapi_cam_reinit(device_t dev
);
101 /* CAM XPT methods */
102 static void atapi_action(struct cam_sim
*, union ccb
*);
103 static void atapi_poll(struct cam_sim
*);
104 static void atapi_async(void *, u_int32_t
, struct cam_path
*, void *);
105 static void atapi_cb(struct ata_request
*);
108 static int atapi_cam_event_handler(module_t mod
, int what
, void *arg
);
110 /* internal functions */
111 static void reinit_bus(struct atapi_xpt_softc
*scp
, enum reinit_reason reason
);
112 static void setup_async_cb(struct atapi_xpt_softc
*, uint32_t);
113 static void cam_rescan_callback(struct cam_periph
*, union ccb
*);
114 static void cam_rescan(struct cam_sim
*);
115 static void free_hcb_and_ccb_done(struct atapi_hcb
*, u_int32_t
);
116 static struct atapi_hcb
*allocate_hcb(struct atapi_xpt_softc
*, int, int, union ccb
*);
117 static void free_hcb(struct atapi_hcb
*hcb
);
118 static void free_softc(struct atapi_xpt_softc
*scp
);
120 static MALLOC_DEFINE(M_ATACAM
, "ata_cam", "ATA driver CAM-XPT layer");
122 static device_method_t atapi_cam_methods
[] = {
123 DEVMETHOD(device_identify
, atapi_cam_identify
),
124 DEVMETHOD(device_probe
, atapi_cam_probe
),
125 DEVMETHOD(device_attach
, atapi_cam_attach
),
126 DEVMETHOD(device_detach
, atapi_cam_detach
),
127 DEVMETHOD(ata_reinit
, atapi_cam_reinit
),
131 static driver_t atapi_cam_driver
= {
134 sizeof(struct atapi_xpt_softc
)
137 static devclass_t atapi_cam_devclass
;
138 DRIVER_MODULE(atapicam
, ata
,
141 atapi_cam_event_handler
,
143 MODULE_VERSION(atapicam
, 1);
144 MODULE_DEPEND(atapicam
, ata
, 1, 1, 1);
145 MODULE_DEPEND(atapicam
, cam
, 1, 1, 1);
148 atapi_cam_identify(device_t
*dev
, device_t parent
)
150 struct atapi_xpt_softc
*scp
=
151 kmalloc(sizeof(struct atapi_xpt_softc
), M_ATACAM
, M_INTWAIT
|M_ZERO
);
154 /* Assume one atapicam instance per parent channel instance. */
155 child
= device_add_child(parent
, "atapicam", -1);
157 /* XXX TGEN Use device_printf()? */
158 kprintf("atapi_cam_identify: out of memory, can't add child");
159 kfree(scp
, M_ATACAM
);
162 scp
->atapi_cam_dev
.unit
= -1;
163 scp
->atapi_cam_dev
.dev
= child
;
165 device_set_softc(child
, scp
);
169 atapi_cam_probe(device_t dev
)
171 struct ata_device
*atadev
= device_get_softc (dev
);
173 KASSERT(atadev
!= NULL
, ("expect valid struct ata_device"));
174 if (atadev
->unit
< 0) {
175 device_set_desc(dev
, "ATAPI CAM Attachment");
183 atapi_cam_attach(device_t dev
)
185 struct atapi_xpt_softc
*scp
= NULL
;
186 struct cam_devq
*devq
= NULL
;
187 struct cam_sim
*sim
= NULL
;
188 struct cam_path
*path
= NULL
;
191 scp
= (struct atapi_xpt_softc
*)device_get_softc(dev
);
193 device_printf(dev
, "Cannot get softc\n");
197 spin_init(&scp
->state_lock
);
200 scp
->parent
= device_get_parent(dev
);
201 scp
->ata_ch
= device_get_softc(scp
->parent
);
202 TAILQ_INIT(&scp
->pending_hcbs
);
203 unit
= device_get_unit(dev
);
205 if ((devq
= cam_simq_alloc(16)) == NULL
) {
210 if ((sim
= cam_sim_alloc(atapi_action
, atapi_poll
, "ata",
211 (void *)scp
, unit
, &sim_mplock
, 1, 1, devq
)) == NULL
) {
217 if (xpt_bus_register(sim
, 0) != CAM_SUCCESS
) {
221 scp
->flags
|= BUS_REGISTERED
;
223 if (xpt_create_path(&path
, /*periph*/ NULL
,
224 cam_sim_path(sim
), CAM_TARGET_WILDCARD
,
225 CAM_LUN_WILDCARD
) != CAM_REQ_CMP
) {
231 CAM_DEBUG(path
, CAM_DEBUG_TRACE
, ("Registered SIM for ata%d\n", unit
));
233 setup_async_cb(scp
, AC_LOST_DEVICE
);
234 reinit_bus(scp
, ATTACH
);
245 atapi_cam_detach(device_t dev
)
247 struct atapi_xpt_softc
*scp
= device_get_softc(dev
);
250 xpt_freeze_simq(scp
->sim
, 1 /*count*/);
252 spin_lock_wr(&scp
->state_lock
);
253 scp
->flags
|= DETACHING
;
254 spin_unlock_wr(&scp
->state_lock
);
260 atapi_cam_reinit(device_t dev
) {
261 struct atapi_xpt_softc
*scp
= device_get_softc(dev
);
264 * scp might be null if the bus is being reinitialised during
265 * the boot-up sequence, before the ATAPI bus is registered.
269 reinit_bus(scp
, RESET
);
275 reinit_bus(struct atapi_xpt_softc
*scp
, enum reinit_reason reason
) {
276 struct ata_device
*atadev
;
280 if (device_get_children(scp
->parent
, &children
, &nchildren
) != 0) {
284 spin_lock_wr(&scp
->state_lock
);
285 scp
->atadev
[0] = NULL
;
286 scp
->atadev
[1] = NULL
;
288 for (i
= 0; i
< nchildren
; i
++) {
289 /* XXX Does the child need to actually be attached yet? */
290 if (children
[i
] != NULL
) {
291 atadev
= device_get_softc(children
[i
]);
292 if ((atadev
->unit
== ATA_MASTER
) &&
293 (scp
->ata_ch
->devices
& ATA_ATAPI_MASTER
) != 0)
294 scp
->atadev
[0] = atadev
;
295 if ((atadev
->unit
== ATA_SLAVE
) &&
296 (scp
->ata_ch
->devices
& ATA_ATAPI_SLAVE
) != 0)
297 scp
->atadev
[1] = atadev
;
300 spin_unlock_wr(&scp
->state_lock
);
301 kfree(children
, M_TEMP
);
305 xpt_async(AC_BUS_RESET
, scp
->path
, NULL
);
308 cam_rescan(scp
->sim
);
314 setup_async_cb(struct atapi_xpt_softc
*scp
, uint32_t events
)
316 struct ccb_setasync csa
;
319 xpt_setup_ccb(&csa
.ccb_h
, scp
->path
, /*priority*/ 5);
320 csa
.ccb_h
.func_code
= XPT_SASYNC_CB
;
321 csa
.event_enable
= events
;
322 csa
.callback
= &atapi_async
;
323 csa
.callback_arg
= scp
->sim
;
324 xpt_action((union ccb
*) &csa
);
329 atapi_action(struct cam_sim
*sim
, union ccb
*ccb
)
331 struct atapi_xpt_softc
*softc
= (struct atapi_xpt_softc
*)cam_sim_softc(sim
);
332 struct ccb_hdr
*ccb_h
= &ccb
->ccb_h
;
333 struct atapi_hcb
*hcb
= NULL
;
334 struct ata_request
*request
= NULL
;
335 int unit
= cam_sim_unit(sim
);
336 int bus
= cam_sim_bus(sim
);
340 switch (ccb_h
->func_code
) {
342 struct ccb_pathinq
*cpi
= &ccb
->cpi
;
343 int tid
= ccb_h
->target_id
;
345 cpi
->version_num
= 1;
346 cpi
->hba_inquiry
= 0;
347 cpi
->target_sprt
= 0;
348 cpi
->hba_misc
= PIM_NO_6_BYTE
;
349 cpi
->hba_eng_cnt
= 0;
350 bzero(cpi
->vuhba_flags
, sizeof(cpi
->vuhba_flags
));
353 cpi
->async_flags
= 0;
355 cpi
->initiator_id
= 7;
356 strncpy(cpi
->sim_vid
, "FreeBSD", sizeof(cpi
->sim_vid
));
357 strncpy(cpi
->hba_vid
, "ATAPI", sizeof(cpi
->hba_vid
));
358 strncpy(cpi
->dev_name
, cam_sim_name(sim
), sizeof cpi
->dev_name
);
359 cpi
->unit_number
= cam_sim_unit(sim
);
360 cpi
->bus_id
= cam_sim_bus(sim
);
361 cpi
->base_transfer_speed
= 3300;
362 cpi
->transport
= XPORT_ATA
;
363 cpi
->transport_version
= 2;
364 cpi
->protocol
= PROTO_SCSI
;
365 cpi
->protocol_version
= SCSI_REV_2
;
367 if (softc
->ata_ch
&& tid
!= CAM_TARGET_WILDCARD
) {
368 spin_lock_wr(&softc
->state_lock
);
369 if (softc
->atadev
[tid
] == NULL
) {
370 ccb
->ccb_h
.status
= CAM_DEV_NOT_THERE
;
372 spin_unlock_wr(&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 spin_unlock_wr(&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
;
439 cts
->protocol
= PROTO_SCSI
;
440 cts
->protocol_version
= SCSI_REV_2
;
441 cts
->transport
= XPORT_ATA
;
442 cts
->transport_version
= XPORT_VERSION_UNSPECIFIED
;
443 cts
->proto_specific
.valid
= 0;
444 cts
->xport_specific
.valid
= 0;
445 /* nothing more to do */
446 ccb
->ccb_h
.status
= CAM_REQ_CMP
;
447 CAM_DEBUG(ccb
->ccb_h
.path
, CAM_DEBUG_SUBTRACE
, ("GET_TRAN_SETTINGS\n"));
452 case XPT_CALC_GEOMETRY
: {
453 CAM_DEBUG(ccb
->ccb_h
.path
, CAM_DEBUG_SUBTRACE
, ("CALC_GEOMETRY\n"));
454 cam_calc_geometry(&ccb
->ccg
, /*extended*/1);
460 struct ccb_scsiio
*csio
= &ccb
->csio
;
461 int tid
= ccb_h
->target_id
, lid
= ccb_h
->target_lun
;
462 int request_flags
= ATA_R_QUIET
| ATA_R_ATAPI
;
464 CAM_DEBUG(ccb_h
->path
, CAM_DEBUG_SUBTRACE
, ("XPT_SCSI_IO\n"));
466 spin_lock_wr(&softc
->state_lock
);
467 if (softc
->flags
& DETACHING
) {
468 ccb
->ccb_h
.status
= CAM_REQ_ABORTED
;
470 spin_unlock_wr(&softc
->state_lock
);
474 if (softc
->atadev
[tid
] == NULL
) {
475 ccb
->ccb_h
.status
= CAM_DEV_NOT_THERE
;
477 spin_unlock_wr(&softc
->state_lock
);
481 /* check that this request was not aborted already */
482 if ((ccb_h
->status
& CAM_STATUS_MASK
) != CAM_REQ_INPROG
) {
483 kprintf("XPT_SCSI_IO received but already in progress?\n");
485 spin_unlock_wr(&softc
->state_lock
);
489 CAM_DEBUG(ccb_h
->path
, CAM_DEBUG_SUBTRACE
,
490 ("SCSI IO received for invalid lun %d\n", lid
));
493 if (csio
->cdb_len
> sizeof request
->u
.atapi
.ccb
) {
494 CAM_DEBUG(ccb_h
->path
, CAM_DEBUG_SUBTRACE
,
495 ("CAM CCB too long for ATAPI"));
498 if ((ccb_h
->flags
& CAM_SCATTER_VALID
)) {
499 /* scatter-gather not supported */
500 xpt_print_path(ccb_h
->path
);
501 kprintf("ATAPI/CAM does not support scatter-gather yet!\n");
505 switch (ccb_h
->flags
& CAM_DIR_MASK
) {
507 request_flags
|= ATA_R_READ
|ATA_R_DMA
;
510 request_flags
|= ATA_R_WRITE
|ATA_R_DMA
;
513 /* No flags need to be set */
516 device_printf(softc
->dev
, "unknown IO operation\n");
519 if (softc
->atadev
[tid
]->mode
< ATA_DMA
)
520 request_flags
&= ~ATA_R_DMA
;
522 if ((hcb
= allocate_hcb(softc
, unit
, bus
, ccb
)) == NULL
) {
523 kprintf("cannot allocate ATAPI/CAM hcb\n");
526 if ((request
= ata_alloc_request()) == NULL
) {
527 kprintf("cannot allocate ATAPI/CAM request\n");
531 bcopy((ccb_h
->flags
& CAM_CDB_POINTER
) ?
532 csio
->cdb_io
.cdb_ptr
: csio
->cdb_io
.cdb_bytes
,
533 request
->u
.atapi
.ccb
, csio
->cdb_len
);
535 if (CAM_DEBUGGED(ccb_h
->path
, CAM_DEBUG_CDB
)) {
536 char cdb_str
[(SCSI_MAX_CDBLEN
* 3) + 1];
538 kprintf("atapi_action: hcb@%p: %s\n", hcb
,
539 scsi_cdb_string(request
->u
.atapi
.ccb
, cdb_str
, sizeof(cdb_str
)));
543 len
= csio
->dxfer_len
;
544 buf
= csio
->data_ptr
;
546 /* some SCSI commands require special processing */
547 switch (request
->u
.atapi
.ccb
[0]) {
550 * many ATAPI devices seem to report more than
551 * SHORT_INQUIRY_LENGTH bytes of available INQUIRY
552 * information, but respond with some incorrect condition
553 * when actually asked for it, so we are going to pretend
554 * that only SHORT_INQUIRY_LENGTH are expected, anyway.
556 struct scsi_inquiry
*inq
= (struct scsi_inquiry
*) &request
->u
.atapi
.ccb
[0];
558 if (inq
->byte2
== 0 && inq
->page_code
== 0 &&
559 inq
->length
> SHORT_INQUIRY_LENGTH
) {
561 len
= inq
->length
= SHORT_INQUIRY_LENGTH
;
569 CAM_DEBUG(ccb_h
->path
, CAM_DEBUG_SUBTRACE
,
570 ("Translating %s into _10 equivalent\n",
571 (request
->u
.atapi
.ccb
[0] == READ_6
) ? "READ_6" : "WRITE_6"));
572 request
->u
.atapi
.ccb
[0] |= 0x20;
573 request
->u
.atapi
.ccb
[9] = request
->u
.atapi
.ccb
[5];
574 request
->u
.atapi
.ccb
[8] = request
->u
.atapi
.ccb
[4];
575 request
->u
.atapi
.ccb
[7] = 0;
576 request
->u
.atapi
.ccb
[6] = 0;
577 request
->u
.atapi
.ccb
[5] = request
->u
.atapi
.ccb
[3];
578 request
->u
.atapi
.ccb
[4] = request
->u
.atapi
.ccb
[2];
579 request
->u
.atapi
.ccb
[3] = request
->u
.atapi
.ccb
[1] & 0x1f;
580 request
->u
.atapi
.ccb
[2] = 0;
581 request
->u
.atapi
.ccb
[1] = 0;
585 if ((ccb_h
->flags
& CAM_DIR_MASK
) == CAM_DIR_IN
&& (len
& 1)) {
586 /* ATA always transfers an even number of bytes */
587 buf
= hcb
->dxfer_alloc
=
588 kmalloc(++len
, M_ATACAM
, M_INTWAIT
| M_ZERO
);
590 request
->dev
= softc
->atadev
[tid
]->dev
;
591 request
->driver
= hcb
;
593 request
->bytecount
= len
;
594 request
->transfersize
= min(request
->bytecount
,
595 min(softc
->ata_ch
->dma
->max_iosize
, 65534));
596 request
->timeout
= ccb_h
->timeout
/ 1000; /* XXX lost granularity */
597 request
->retries
= 2;
598 request
->callback
= &atapi_cb
;
599 request
->flags
= request_flags
;
601 TAILQ_INSERT_TAIL(&softc
->pending_hcbs
, hcb
, chain
);
602 hcb
->flags
|= QUEUED
;
603 ccb_h
->status
|= CAM_SIM_QUEUED
;
604 spin_unlock_wr(&softc
->state_lock
);
606 ata_queue_request(request
);
611 CAM_DEBUG(ccb_h
->path
, CAM_DEBUG_SUBTRACE
,
612 ("unsupported function code 0x%02x\n", ccb_h
->func_code
));
620 ata_free_request(request
);
623 spin_unlock_wr(&softc
->state_lock
);
625 xpt_print_path(ccb_h
->path
);
626 kprintf("out of memory, freezing queue.\n");
627 softc
->flags
|= RESOURCE_SHORTAGE
;
628 xpt_freeze_simq(sim
, /*count*/ 1);
630 ccb_h
->status
= CAM_REQUEUE_REQ
;
635 spin_unlock_wr(&softc
->state_lock
);
636 ccb_h
->status
= CAM_REQ_INVALID
;
642 atapi_poll(struct cam_sim
*sim
)
644 /* do nothing - we do not actually service any interrupts */
645 kprintf("atapi_poll called!\n");
649 atapi_cb(struct ata_request
*request
)
651 struct atapi_xpt_softc
*scp
;
652 struct atapi_hcb
*hcb
;
653 struct ccb_scsiio
*csio
;
656 hcb
= (struct atapi_hcb
*)request
->driver
;
658 csio
= &hcb
->ccb
->csio
;
661 # define err (request->u.atapi.sense.key)
662 if (CAM_DEBUGGED(csio
->ccb_h
.path
, CAM_DEBUG_CDB
)) {
663 kprintf("atapi_cb: hcb@%p error = %02x: (sk = %02x%s%s%s)\n",
665 (err
& 4) ? " ABRT" : "",
666 (err
& 2) ? " EOM" : "",
667 (err
& 1) ? " ILI" : "");
668 kprintf("dev %s: cmd %02x status %02x result %02x\n",
669 device_get_nameunit(request
->dev
), request
->u
.atapi
.ccb
[0],
670 request
->status
, request
->result
);
674 if ((hcb
->flags
& AUTOSENSE
) != 0) {
675 rc
= CAM_SCSI_STATUS_ERROR
;
676 if (request
->result
== 0) {
677 csio
->ccb_h
.status
|= CAM_AUTOSNS_VALID
;
679 } else if (request
->result
!= 0) {
680 rc
= CAM_SCSI_STATUS_ERROR
;
681 csio
->scsi_status
= SCSI_STATUS_CHECK_COND
;
683 if ((csio
->ccb_h
.flags
& CAM_DIS_AUTOSENSE
) == 0) {
685 static const int8_t ccb
[16] = { ATAPI_REQUEST_SENSE
, 0, 0, 0,
686 sizeof(struct atapi_sense
), 0, 0, 0, 0, 0, 0,
689 bcopy (ccb
, request
->u
.atapi
.ccb
, sizeof ccb
);
690 request
->data
= (caddr_t
)&csio
->sense_data
;
691 request
->bytecount
= sizeof(struct atapi_sense
);
692 request
->transfersize
= min(request
->bytecount
, 65534);
693 request
->timeout
= csio
->ccb_h
.timeout
/ 1000;
694 request
->retries
= 2;
695 request
->flags
= ATA_R_QUIET
|ATA_R_ATAPI
|ATA_R_IMMEDIATE
;
696 hcb
->flags
|= AUTOSENSE
;
698 ata_queue_request(request
);
701 /* The ATA driver has already requested sense for us. */
702 if (request
->error
== 0) {
703 /* The ATA autosense suceeded. */
704 bcopy (&request
->u
.atapi
.sense
, &csio
->sense_data
, sizeof(struct atapi_sense
));
705 csio
->ccb_h
.status
|= CAM_AUTOSNS_VALID
;
711 csio
->scsi_status
= SCSI_STATUS_OK
;
712 if (((csio
->ccb_h
.flags
& CAM_DIR_MASK
) == CAM_DIR_IN
) &&
713 hcb
->dxfer_alloc
!= NULL
)
715 bcopy(hcb
->dxfer_alloc
, csio
->data_ptr
, csio
->dxfer_len
);
719 spin_lock_wr(&scp
->state_lock
);
720 free_hcb_and_ccb_done(hcb
, rc
);
721 spin_unlock_wr(&scp
->state_lock
);
723 ata_free_request(request
);
727 free_hcb_and_ccb_done(struct atapi_hcb
*hcb
, u_int32_t status
)
729 struct atapi_xpt_softc
*softc
;
738 /* we're about to free a hcb, so the shortage has ended */
739 if (softc
->flags
& RESOURCE_SHORTAGE
) {
740 softc
->flags
&= ~RESOURCE_SHORTAGE
;
741 status
|= CAM_RELEASE_SIMQ
;
745 status
| (ccb
->ccb_h
.status
& ~(CAM_STATUS_MASK
| CAM_SIM_QUEUED
));
750 atapi_async(void *callback_arg
, u_int32_t code
,
751 struct cam_path
* path
, void *arg
)
753 struct atapi_xpt_softc
*softc
;
759 sim
= (struct cam_sim
*) callback_arg
;
760 softc
= (struct atapi_xpt_softc
*) cam_sim_softc(sim
);
763 targ
= xpt_path_target_id(path
);
764 xpt_print_path(path
);
766 kprintf("Lost host adapter\n");
768 kprintf("Lost target %d???\n", targ
);
779 cam_rescan_callback(struct cam_periph
*periph
, union ccb
*ccb
)
781 if (ccb
->ccb_h
.status
!= CAM_REQ_CMP
) {
782 CAM_DEBUG(ccb
->ccb_h
.path
, CAM_DEBUG_TRACE
,
783 ("Rescan failed, 0x%04x\n", ccb
->ccb_h
.status
));
785 CAM_DEBUG(ccb
->ccb_h
.path
, CAM_DEBUG_TRACE
,
786 ("Rescan succeeded\n"));
788 xpt_free_path(ccb
->ccb_h
.path
);
789 kfree(ccb
, M_ATACAM
);
793 cam_rescan(struct cam_sim
*sim
)
795 struct cam_path
*path
;
796 union ccb
*ccb
= kmalloc(sizeof(union ccb
), M_ATACAM
, M_WAITOK
| M_ZERO
);
799 if (xpt_create_path(&path
, xpt_periph
, cam_sim_path(sim
),
800 CAM_TARGET_WILDCARD
, CAM_LUN_WILDCARD
) != CAM_REQ_CMP
) {
802 kfree(ccb
, M_ATACAM
);
806 CAM_DEBUG(ccb
->ccb_h
.path
, CAM_DEBUG_TRACE
, ("Rescanning ATAPI bus.\n"));
807 xpt_setup_ccb(&ccb
->ccb_h
, path
, 5/*priority (low)*/);
808 ccb
->ccb_h
.func_code
= XPT_SCAN_BUS
;
809 ccb
->ccb_h
.cbfcnp
= cam_rescan_callback
;
810 ccb
->crcn
.flags
= CAM_FLAG_NONE
;
812 /* scan is in progress now */
816 static struct atapi_hcb
*
817 allocate_hcb(struct atapi_xpt_softc
*softc
, int unit
, int bus
, union ccb
*ccb
)
819 struct atapi_hcb
*hcb
= (struct atapi_hcb
*)
820 kmalloc(sizeof(struct atapi_hcb
), M_ATACAM
, M_INTWAIT
| M_ZERO
);
830 free_hcb(struct atapi_hcb
*hcb
)
832 if ((hcb
->flags
& QUEUED
) != 0)
833 TAILQ_REMOVE(&hcb
->softc
->pending_hcbs
, hcb
, chain
);
834 if (hcb
->dxfer_alloc
!= NULL
)
835 kfree(hcb
->dxfer_alloc
, M_ATACAM
);
836 kfree(hcb
, M_ATACAM
);
840 free_softc(struct atapi_xpt_softc
*scp
)
842 struct atapi_hcb
*hcb
;
845 spin_lock_wr(&scp
->state_lock
);
846 TAILQ_FOREACH(hcb
, &scp
->pending_hcbs
, chain
) {
847 free_hcb_and_ccb_done(hcb
, CAM_UNREC_HBA_ERROR
);
849 spin_unlock_wr(&scp
->state_lock
);
851 if (scp
->path
!= NULL
) {
852 setup_async_cb(scp
, 0);
853 xpt_free_path(scp
->path
);
855 if ((scp
->flags
& BUS_REGISTERED
) != 0) {
856 if (xpt_bus_deregister(cam_sim_path(scp
->sim
)) == CAM_REQ_CMP
)
857 scp
->flags
&= ~BUS_REGISTERED
;
859 if (scp
->sim
!= NULL
) {
860 if ((scp
->flags
& BUS_REGISTERED
) == 0)
861 cam_sim_free(scp
->sim
);
863 kprintf("Can't free %s SIM (still registered)\n",
864 cam_sim_name(scp
->sim
));
867 spin_uninit(&scp
->state_lock
);
872 atapi_cam_event_handler(module_t mod
, int what
, void *arg
) {
878 if (devclass_get_devices(atapi_cam_devclass
, &devlist
, &devcount
)
881 if (devlist
!= NULL
) {
882 while (devlist
!= NULL
&& devcount
> 0) {
883 device_t child
= devlist
[--devcount
];
884 struct atapi_xpt_softc
*scp
= device_get_softc(child
);
886 device_delete_child(device_get_parent(child
),child
);
888 kfree(scp
, M_ATACAM
);
890 kfree(devlist
, M_TEMP
);