xref: /dragonfly/sys/dev/disk/nata/atapi-cam.c (revision 279dd846)
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  * $FreeBSD: src/sys/dev/ata/atapi-cam.c,v 1.44 2006/03/31 08:09:05 sos Exp $
29  */
30 
31 #include "opt_ata.h"
32 #include "opt_scsi.h"
33 
34 #include <sys/param.h>
35 #include <sys/bus.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>
40 #include <sys/nata.h>
41 #include <sys/queue.h>
42 #include <sys/systm.h>
43 
44 #include <sys/thread2.h>
45 #include <sys/mplock2.h>
46 
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>
54 
55 #include "ata-all.h"
56 #include "ata_if.h"
57 
58 /* private data associated with an ATA bus */
59 struct atapi_xpt_softc {
60     struct ata_device   atapi_cam_dev;  /* must be first */
61     device_t            dev;
62     device_t            parent;
63     struct ata_channel  *ata_ch;
64     struct cam_path     *path;
65     struct cam_sim      *sim;
66     int                 flags;
67 #define BUS_REGISTERED          0x01
68 #define RESOURCE_SHORTAGE       0x02
69 #define DETACHING               0x04
70 
71     TAILQ_HEAD(,atapi_hcb) pending_hcbs;
72     struct ata_device   *atadev[2];
73     struct lock		state_lock;
74 };
75 
76 /* hardware command descriptor block */
77 struct atapi_hcb {
78     struct atapi_xpt_softc *softc;
79     int                 unit;
80     int                 bus;
81     int                 target;
82     int                 lun;
83     union ccb           *ccb;
84     int                 flags;
85 #define QUEUED          0x0001
86 #define AUTOSENSE       0x0002
87     char                *dxfer_alloc;
88     TAILQ_ENTRY(atapi_hcb) chain;
89 };
90 
91 enum reinit_reason { ATTACH, RESET };
92 
93 /* Device methods */
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);
99 
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 *);
105 
106 /* Module methods */
107 static int atapi_cam_event_handler(module_t mod, int what, void *arg);
108 
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);
118 
119 static MALLOC_DEFINE(M_ATACAM, "ata_cam", "ATA driver CAM-XPT layer");
120 
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),
127 	DEVMETHOD_END
128 };
129 
130 static driver_t atapi_cam_driver = {
131 	"atapicam",
132 	atapi_cam_methods,
133 	sizeof(struct atapi_xpt_softc)
134 };
135 
136 static devclass_t       atapi_cam_devclass;
137 DRIVER_MODULE(atapicam, ata,
138 	atapi_cam_driver,
139 	atapi_cam_devclass,
140 	atapi_cam_event_handler,
141 	/*arg*/NULL);
142 MODULE_VERSION(atapicam, 1);
143 MODULE_DEPEND(atapicam, ata, 1, 1, 1);
144 MODULE_DEPEND(atapicam, cam, 1, 1, 1);
145 
146 static void
147 atapi_cam_identify(device_t *dev, device_t parent)
148 {
149 	struct atapi_xpt_softc *scp =
150 	    kmalloc(sizeof(struct atapi_xpt_softc), M_ATACAM, M_INTWAIT|M_ZERO);
151 	device_t child;
152 
153 	/* Assume one atapicam instance per parent channel instance. */
154 	child = device_add_child(parent, "atapicam", -1);
155 	if (child == NULL) {
156 		/* XXX TGEN Use device_printf()? */
157 		kprintf("atapi_cam_identify: out of memory, can't add child");
158 		kfree(scp, M_ATACAM);
159 		return;
160 	}
161 	scp->atapi_cam_dev.unit = -1;
162 	scp->atapi_cam_dev.dev = child;
163 	device_quiet(child);
164 	device_set_softc(child, scp);
165 }
166 
167 static int
168 atapi_cam_probe(device_t dev)
169 {
170 	struct ata_device *atadev = device_get_softc (dev);
171 
172 	KASSERT(atadev != NULL, ("expect valid struct ata_device"));
173 	if (atadev->unit < 0) {
174 		device_set_desc(dev, "ATAPI CAM Attachment");
175 		return(0);
176 	} else {
177 		return ENXIO;
178 	}
179 }
180 
181 static int
182 atapi_cam_attach(device_t dev)
183 {
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;
188     int unit, error;
189 
190     scp = (struct atapi_xpt_softc *)device_get_softc(dev);
191     if (scp == NULL) {
192 	device_printf(dev, "Cannot get softc\n");
193 	return ENOMEM;
194     }
195 
196     lockinit(&scp->state_lock, "atapicamattach", 0, 0);
197 
198     scp->dev = dev;
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);
203 
204     if ((devq = cam_simq_alloc(16)) == NULL) {
205 	error = ENOMEM;
206 	goto out;
207     }
208 
209     if ((sim = cam_sim_alloc(atapi_action, atapi_poll, "ata",
210 		 (void *)scp, unit, &sim_mplock, 1, 1, devq)) == NULL) {
211 	error = ENOMEM;
212 	goto out;
213     }
214     scp->sim = sim;
215 
216     if (xpt_bus_register(sim, 0) != CAM_SUCCESS) {
217 	error = EINVAL;
218 	goto out;
219     }
220     scp->flags |= BUS_REGISTERED;
221 
222     if (xpt_create_path(&path, /*periph*/ NULL,
223 		cam_sim_path(sim), CAM_TARGET_WILDCARD,
224 		CAM_LUN_WILDCARD) != CAM_REQ_CMP) {
225 	error = ENOMEM;
226 	goto out;
227     }
228     scp->path = path;
229 
230     CAM_DEBUG(path, CAM_DEBUG_TRACE, ("Registered SIM for ata%d\n", unit));
231 
232     setup_async_cb(scp, AC_LOST_DEVICE);
233     reinit_bus(scp, ATTACH);
234     error = 0;
235 
236 out:
237     if (error != 0)
238 	free_softc(scp);
239 
240     return(error);
241 }
242 
243 static int
244 atapi_cam_detach(device_t dev)
245 {
246     struct atapi_xpt_softc *scp = device_get_softc(dev);
247 
248     get_mplock();
249     xpt_freeze_simq(scp->sim, 1 /*count*/);
250     rel_mplock();
251     lockmgr(&scp->state_lock, LK_EXCLUSIVE);
252     scp->flags |= DETACHING;
253     lockmgr(&scp->state_lock, LK_RELEASE);
254     free_softc(scp);
255     return (0);
256 }
257 
258 static int
259 atapi_cam_reinit(device_t dev) {
260     struct atapi_xpt_softc *scp = device_get_softc(dev);
261 
262     /*
263      * scp might be null if the bus is being reinitialised during
264      * the boot-up sequence, before the ATAPI bus is registered.
265      */
266 
267     if (scp != NULL) {
268 	reinit_bus(scp, RESET);
269     }
270     return 0;
271 }
272 
273 static void
274 reinit_bus(struct atapi_xpt_softc *scp, enum reinit_reason reason) {
275     struct ata_device *old_atadev[2], *atadev;
276     device_t *children;
277     int nchildren, i, dev_changed;
278 
279     if (device_get_children(scp->parent, &children, &nchildren) != 0) {
280 	return;
281     }
282 
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;
288 
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;
299 	}
300     }
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);
305 
306     switch (reason) {
307 	case RESET:
308 	    xpt_async(AC_BUS_RESET, scp->path, NULL);
309 
310 	    if (!dev_changed)
311 		    break;
312 
313 	    /*FALLTHROUGH*/
314 	case ATTACH:
315 	    cam_rescan(scp->sim);
316 	    break;
317     }
318 }
319 
320 static void
321 setup_async_cb(struct atapi_xpt_softc *scp, uint32_t events)
322 {
323     struct ccb_setasync csa;
324 
325     get_mplock();
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);
332     rel_mplock();
333 }
334 
335 static void
336 atapi_action(struct cam_sim *sim, union ccb *ccb)
337 {
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);
344     int len;
345     char *buf;
346 
347     switch (ccb_h->func_code) {
348     case XPT_PATH_INQ: {
349 	struct ccb_pathinq *cpi = &ccb->cpi;
350 	int tid = ccb_h->target_id;
351 
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));
358 	cpi->max_target = 1;
359 	cpi->max_lun = 0;
360 	cpi->async_flags = 0;
361 	cpi->hpath_id = 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;
373 
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;
378 		xpt_done(ccb);
379 		lockmgr(&softc->state_lock, LK_RELEASE);
380 		return;
381 	    }
382 	    switch (softc->atadev[ccb_h->target_id]->mode) {
383 	    case ATA_PIO1:
384 		cpi->base_transfer_speed = 5200;
385 		break;
386 	    case ATA_PIO2:
387 		cpi->base_transfer_speed = 7000;
388 		break;
389 	    case ATA_PIO3:
390 		cpi->base_transfer_speed = 11000;
391 		break;
392 	    case ATA_PIO4:
393 	    case ATA_DMA:
394 	    case ATA_WDMA2:
395 		cpi->base_transfer_speed = 16000;
396 		break;
397 	    case ATA_UDMA2:
398 		cpi->base_transfer_speed = 33000;
399 		break;
400 	    case ATA_UDMA4:
401 		cpi->base_transfer_speed = 66000;
402 		break;
403 	    case ATA_UDMA5:
404 		cpi->base_transfer_speed = 100000;
405 		break;
406 	    case ATA_UDMA6:
407 		cpi->base_transfer_speed = 133000;
408 		break;
409 	    default:
410 		break;
411 	    }
412 	    lockmgr(&softc->state_lock, LK_RELEASE);
413 	}
414 	ccb->ccb_h.status = CAM_REQ_CMP;
415 	xpt_done(ccb);
416 	return;
417     }
418 
419     case XPT_RESET_DEV: {
420 	int tid = ccb_h->target_id;
421 
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;
425 	xpt_done(ccb);
426 	return;
427     }
428 
429     case XPT_RESET_BUS:
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;
433 	xpt_done(ccb);
434 	return;
435 
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;
441 	xpt_done(ccb);
442 	return;
443 
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"));
455 	xpt_done(ccb);
456 	return;
457     }
458 
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);
462 	xpt_done(ccb);
463 	return;
464     }
465 
466     case XPT_SCSI_IO: {
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;
470 
471 	CAM_DEBUG(ccb_h->path, CAM_DEBUG_SUBTRACE, ("XPT_SCSI_IO\n"));
472 
473 	lockmgr(&softc->state_lock, LK_EXCLUSIVE);
474 	if (softc->flags & DETACHING) {
475 	    ccb->ccb_h.status = CAM_REQ_ABORTED;
476 	    xpt_done(ccb);
477 	    lockmgr(&softc->state_lock, LK_RELEASE);
478 	    return;
479 	}
480 
481 	if (softc->atadev[tid] == NULL) {
482 	    ccb->ccb_h.status = CAM_DEV_NOT_THERE;
483 	    xpt_done(ccb);
484 	    lockmgr(&softc->state_lock, LK_RELEASE);
485 	    return;
486 	}
487 
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");
491 	    xpt_done(ccb);
492 	    lockmgr(&softc->state_lock, LK_RELEASE);
493 	    return;
494 	}
495 	if (lid > 0) {
496 	    CAM_DEBUG(ccb_h->path, CAM_DEBUG_SUBTRACE,
497 		      ("SCSI IO received for invalid lun %d\n", lid));
498 	    goto action_invalid;
499 	}
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"));
503 	    goto action_invalid;
504 	}
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");
509 	    goto action_invalid;
510 	}
511 
512 	switch (ccb_h->flags & CAM_DIR_MASK) {
513 	case CAM_DIR_IN:
514 	     request_flags |= ATA_R_READ;
515 	     break;
516 	case CAM_DIR_OUT:
517 	     request_flags |= ATA_R_WRITE;
518 	     break;
519 	case CAM_DIR_NONE:
520 	     /* No flags need to be set */
521 	     break;
522 	default:
523 	     device_printf(softc->dev, "unknown IO operation\n");
524 	     goto action_invalid;
525 	}
526 
527 	if ((hcb = allocate_hcb(softc, unit, bus, ccb)) == NULL) {
528 	    kprintf("cannot allocate ATAPI/CAM hcb\n");
529 	    goto action_oom;
530 	}
531 	if ((request = ata_alloc_request()) == NULL) {
532 	    kprintf("cannot allocate ATAPI/CAM request\n");
533 	    goto action_oom;
534 	}
535 
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);
539 #ifdef CAMDEBUG
540 	if (CAM_DEBUGGED(ccb_h->path, CAM_DEBUG_CDB)) {
541 		char cdb_str[(SCSI_MAX_CDBLEN * 3) + 1];
542 
543 		kprintf("atapi_action: hcb@%p: %s\n", hcb,
544 		       scsi_cdb_string(request->u.atapi.ccb, cdb_str, sizeof(cdb_str)));
545 	}
546 	if (CAM_DEBUGGED(ccb_h->path, CAM_DEBUG_SUBTRACE)) {
547 		request_flags |= ATA_R_DEBUG;
548 	}
549 #endif
550 
551 	len = csio->dxfer_len;
552 	buf = csio->data_ptr;
553 
554 	/* some SCSI commands require special processing */
555 	switch (request->u.atapi.ccb[0]) {
556 	case INQUIRY: {
557 	    /*
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.
563 	     */
564 	    struct scsi_inquiry *inq = (struct scsi_inquiry *) &request->u.atapi.ccb[0];
565 
566 	    if (inq->byte2 == 0 && inq->page_code == 0 &&
567 		inq->length > SHORT_INQUIRY_LENGTH) {
568 		bzero(buf, len);
569 		len = inq->length = SHORT_INQUIRY_LENGTH;
570 	    }
571 	    break;
572 	}
573 	case READ_6:
574 	    /* FALLTHROUGH */
575 
576 	case WRITE_6:
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;
590 	    break;
591 	}
592 
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);
597 	}
598 
599 	/*
600 	 * Don't allow DMA for requests with length not multiple of 16 bytes.
601 	 * Some ATAPI devices don't like it.
602 	 */
603 	if ((softc->atadev[tid]->mode >= ATA_DMA) && len > 0 && !(len & 15))
604 		request_flags |= ATA_R_DMA;
605 
606 	request->dev = softc->atadev[tid]->dev;
607 	request->driver = hcb;
608 	request->data = buf;
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;
615 
616 	/*
617 	 * no retries are to be performed at the ATA level; any retries
618 	 * will be done by CAM .
619 	 */
620 	request->retries = 0;
621 
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);
626 
627 	ata_queue_request(request);
628 	return;
629     }
630 
631     default:
632 	CAM_DEBUG(ccb_h->path, CAM_DEBUG_SUBTRACE,
633 		  ("unsupported function code 0x%02x\n", ccb_h->func_code));
634 	goto action_invalid;
635     }
636 
637     /* NOTREACHED */
638 
639 action_oom:
640     if (request != NULL)
641 	ata_free_request(request);
642     if (hcb != NULL)
643 	free_hcb(hcb);
644     lockmgr(&softc->state_lock, LK_RELEASE);
645     get_mplock();
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);
650     rel_mplock();
651     ccb_h->status = CAM_REQUEUE_REQ;
652     xpt_done(ccb);
653     return;
654 
655 action_invalid:
656     lockmgr(&softc->state_lock, LK_RELEASE);
657     ccb_h->status = CAM_REQ_INVALID;
658     xpt_done(ccb);
659     return;
660 }
661 
662 static void
663 atapi_poll(struct cam_sim *sim)
664 {
665     /* do nothing - we do not actually service any interrupts */
666     kprintf("atapi_poll called!\n");
667 }
668 
669 static void
670 atapi_cb(struct ata_request *request)
671 {
672     struct atapi_xpt_softc *scp;
673     struct atapi_hcb *hcb;
674     struct ccb_scsiio *csio;
675     u_int32_t rc;
676 
677     hcb = (struct atapi_hcb *)request->driver;
678     scp = hcb->softc;
679     csio = &hcb->ccb->csio;
680 
681 #ifdef CAMDEBUG
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);
693     }
694 #endif
695 
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;
700 	}
701     } else if (request->result != 0) {
702 	if ((request->flags & ATA_R_TIMEOUT) != 0) {
703 	    rc = CAM_CMD_TIMEOUT;
704 	} else {
705 	    rc = CAM_SCSI_STATUS_ERROR;
706 	    csio->scsi_status = SCSI_STATUS_CHECK_COND;
707 
708 	    if ((csio->ccb_h.flags & CAM_DIS_AUTOSENSE) == 0) {
709 #if 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,
712 		    0, 0, 0, 0, 0 };
713 
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;
722 
723 		ata_queue_request(request);
724 		return;
725 #else
726 		/*
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.
730 		 */
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;
734 		}
735 	    }
736 #endif
737 	}
738     } else {
739 	rc = CAM_REQ_CMP;
740 	csio->scsi_status = SCSI_STATUS_OK;
741 	if (((csio->ccb_h.flags & CAM_DIR_MASK) == CAM_DIR_IN) &&
742 	    hcb->dxfer_alloc != NULL)
743 	{
744 	    bcopy(hcb->dxfer_alloc, csio->data_ptr, csio->dxfer_len);
745 	}
746     }
747 
748     lockmgr(&scp->state_lock, LK_EXCLUSIVE);
749     free_hcb_and_ccb_done(hcb, rc);
750     lockmgr(&scp->state_lock, LK_RELEASE);
751 
752     ata_free_request(request);
753 }
754 
755 static void
756 free_hcb_and_ccb_done(struct atapi_hcb *hcb, u_int32_t status)
757 {
758     struct atapi_xpt_softc *softc;
759     union ccb *ccb;
760 
761     if (hcb == NULL)
762 	return;
763 
764     softc = hcb->softc;
765     ccb = hcb->ccb;
766 
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;
771     }
772     free_hcb(hcb);
773     ccb->ccb_h.status =
774 	status | (ccb->ccb_h.status & ~(CAM_STATUS_MASK | CAM_SIM_QUEUED));
775     xpt_done(ccb);
776 }
777 
778 static void
779 atapi_async(void *callback_arg, u_int32_t code,
780 	     struct cam_path* path, void *arg)
781 {
782     int targ;
783 
784     crit_enter();
785 
786     switch (code) {
787     case AC_LOST_DEVICE:
788 	targ = xpt_path_target_id(path);
789 	xpt_print_path(path);
790 	if (targ == -1)
791 		kprintf("Lost host adapter\n");
792 	else
793 		kprintf("Lost target %d???\n", targ);
794 	break;
795 
796     default:
797 	break;
798     }
799 
800     crit_exit();
801 }
802 
803 static void
804 cam_rescan_callback(struct cam_periph *periph, union ccb *ccb)
805 {
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));
809 	} else {
810 	    CAM_DEBUG(ccb->ccb_h.path, CAM_DEBUG_TRACE,
811 		      ("Rescan succeeded\n"));
812 	}
813 	xpt_free_path(ccb->ccb_h.path);
814 	kfree(ccb, M_ATACAM);
815 }
816 
817 static void
818 cam_rescan(struct cam_sim *sim)
819 {
820     struct cam_path *path;
821     union ccb *ccb = kmalloc(sizeof(union ccb), M_ATACAM, M_WAITOK | M_ZERO);
822 
823     get_mplock();
824     if (xpt_create_path(&path, xpt_periph, cam_sim_path(sim),
825 			CAM_TARGET_WILDCARD, CAM_LUN_WILDCARD) != CAM_REQ_CMP) {
826 	rel_mplock();
827 	kfree(ccb, M_ATACAM);
828 	return;
829     }
830 
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;
836     xpt_action(ccb);
837     /* scan is in progress now */
838     rel_mplock();
839 }
840 
841 static struct atapi_hcb *
842 allocate_hcb(struct atapi_xpt_softc *softc, int unit, int bus, union ccb *ccb)
843 {
844     struct atapi_hcb *hcb = (struct atapi_hcb *)
845 	kmalloc(sizeof(struct atapi_hcb), M_ATACAM, M_INTWAIT | M_ZERO);
846 
847     hcb->softc = softc;
848     hcb->unit = unit;
849     hcb->bus = bus;
850     hcb->ccb = ccb;
851     return hcb;
852 }
853 
854 static void
855 free_hcb(struct atapi_hcb *hcb)
856 {
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);
862 }
863 
864 static void
865 free_softc(struct atapi_xpt_softc *scp)
866 {
867     struct atapi_hcb *hcb;
868 
869     if (scp != NULL) {
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);
873 	}
874 	lockmgr(&scp->state_lock, LK_RELEASE);
875 	get_mplock();
876 	if (scp->path != NULL) {
877 	    setup_async_cb(scp, 0);
878 	    xpt_free_path(scp->path);
879 	}
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;
883 	}
884 	if (scp->sim != NULL) {
885 	    if ((scp->flags & BUS_REGISTERED) == 0)
886 		cam_sim_free(scp->sim);
887 	    else
888 		kprintf("Can't free %s SIM (still registered)\n",
889 		       cam_sim_name(scp->sim));
890 	}
891 	rel_mplock();
892 	lockuninit(&scp->state_lock);
893     }
894 }
895 
896 static int
897 atapi_cam_event_handler(module_t mod, int what, void *arg) {
898     device_t *devlist;
899     int devcount;
900 
901     switch (what) {
902 	case MOD_UNLOAD:
903 	    if (devclass_get_devices(atapi_cam_devclass, &devlist, &devcount)
904 		  != 0)
905 		return ENXIO;
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);
910 
911 		    device_delete_child(device_get_parent(child),child);
912 		    if (scp != NULL)
913 			kfree(scp, M_ATACAM);
914 		}
915 		kfree(devlist, M_TEMP);
916 	    }
917 	    break;
918 
919 	default:
920 	    break;
921     }
922     return 0;
923 }
924