xref: /dflybsd-src/sys/dev/disk/nata/atapi-cam.c (revision 6ea70f7669242fe9fd042834f455f06a4adfaadf)
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  * $DragonFly: src/sys/dev/disk/nata/atapi-cam.c,v 1.4 2006/12/22 23:26:16 swildner Exp $
30  */
31 
32 #include "opt_ata.h"
33 #include "opt_scsi.h"
34 
35 #include <sys/param.h>
36 #include <sys/bus.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>
41 #include <sys/nata.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>
47 
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>
55 
56 #include "ata-all.h"
57 #include "ata_if.h"
58 
59 /* private data associated with an ATA bus */
60 struct atapi_xpt_softc {
61     struct ata_device   atapi_cam_dev;  /* must be first */
62     device_t            dev;
63     device_t            parent;
64     struct ata_channel  *ata_ch;
65     struct cam_path     *path;
66     struct cam_sim      *sim;
67     int                 flags;
68 #define BUS_REGISTERED          0x01
69 #define RESOURCE_SHORTAGE       0x02
70 #define DETACHING               0x04
71 
72     TAILQ_HEAD(,atapi_hcb) pending_hcbs;
73     struct ata_device   *atadev[2];
74     struct spinlock     state_lock;
75 };
76 
77 /* hardware command descriptor block */
78 struct atapi_hcb {
79     struct atapi_xpt_softc *softc;
80     int                 unit;
81     int                 bus;
82     int                 target;
83     int                 lun;
84     union ccb           *ccb;
85     int                 flags;
86 #define QUEUED          0x0001
87 #define AUTOSENSE       0x0002
88     char                *dxfer_alloc;
89     TAILQ_ENTRY(atapi_hcb) chain;
90 };
91 
92 enum reinit_reason { BOOT_ATTACH, ATTACH, RESET };
93 
94 /* Device methods */
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);
100 
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 *);
106 
107 /* Module methods */
108 static int atapi_cam_event_handler(module_t mod, int what, void *arg);
109 
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);
119 
120 static MALLOC_DEFINE(M_ATACAM, "ata_cam", "ATA driver CAM-XPT layer");
121 
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),
128 	{0, 0}
129 };
130 
131 static driver_t atapi_cam_driver = {
132 	"atapicam",
133 	atapi_cam_methods,
134 	sizeof(struct atapi_xpt_softc)
135 };
136 
137 static devclass_t       atapi_cam_devclass;
138 DRIVER_MODULE(atapicam, ata,
139 	atapi_cam_driver,
140 	atapi_cam_devclass,
141 	atapi_cam_event_handler,
142 	/*arg*/NULL);
143 MODULE_VERSION(atapicam, 1);
144 MODULE_DEPEND(atapicam, ata, 1, 1, 1);
145 MODULE_DEPEND(atapicam, cam, 1, 1, 1);
146 
147 static void
148 atapi_cam_identify(device_t *dev, device_t parent)
149 {
150 	struct atapi_xpt_softc *scp =
151 	    kmalloc(sizeof(struct atapi_xpt_softc), M_ATACAM, M_NOWAIT|M_ZERO);
152 	device_t child;
153 
154 	if (scp == NULL) {
155 		/* XXX TGEN Use device_printf()? */
156 		kprintf("atapi_cam_identify: out of memory");
157 		return;
158 	}
159 
160 	/* Assume one atapicam instance per parent channel instance. */
161 	child = device_add_child(parent, "atapicam", -1);
162 	if (child == NULL) {
163 		/* XXX TGEN Use device_printf()? */
164 		kprintf("atapi_cam_identify: out of memory, can't add child");
165 		kfree(scp, M_ATACAM);
166 		return;
167 	}
168 	scp->atapi_cam_dev.unit = -1;
169 	scp->atapi_cam_dev.dev = child;
170 	device_quiet(child);
171 	device_set_softc(child, scp);
172 }
173 
174 static int
175 atapi_cam_probe(device_t dev)
176 {
177 	struct ata_device *atadev = device_get_softc (dev);
178 
179 	KASSERT(atadev != NULL, ("expect valid struct ata_device"));
180 	if (atadev->unit < 0) {
181 		device_set_desc(dev, "ATAPI CAM Attachment");
182 		return(0);
183 	} else {
184 		return ENXIO;
185 	}
186 }
187 
188 static int
189 atapi_cam_attach(device_t dev)
190 {
191     struct atapi_xpt_softc *scp = NULL;
192     struct cam_devq *devq = NULL;
193     struct cam_sim *sim = NULL;
194     struct cam_path *path = NULL;
195     int unit, error;
196 
197     scp = (struct atapi_xpt_softc *)device_get_softc(dev);
198     if (scp == NULL) {
199 	device_printf(dev, "Cannot get softc\n");
200 	return ENOMEM;
201     }
202 
203     spin_init(&scp->state_lock);
204 
205     scp->dev = dev;
206     scp->parent = device_get_parent(dev);
207     scp->ata_ch = device_get_softc(scp->parent);
208     TAILQ_INIT(&scp->pending_hcbs);
209     unit = device_get_unit(dev);
210 
211     if ((devq = cam_simq_alloc(16)) == NULL) {
212 	error = ENOMEM;
213 	goto out;
214     }
215 
216     if ((sim = cam_sim_alloc(atapi_action, atapi_poll, "ata",
217 		 (void *)scp, unit, 1, 1, devq)) == NULL) {
218 	error = ENOMEM;
219 	goto out;
220     }
221     scp->sim = sim;
222 
223     if (xpt_bus_register(sim, 0) != CAM_SUCCESS) {
224 	error = EINVAL;
225 	goto out;
226     }
227     scp->flags |= BUS_REGISTERED;
228 
229     if (xpt_create_path(&path, /*periph*/ NULL,
230 		cam_sim_path(sim), CAM_TARGET_WILDCARD,
231 		CAM_LUN_WILDCARD) != CAM_REQ_CMP) {
232 	error = ENOMEM;
233 	goto out;
234     }
235     scp->path = path;
236 
237     CAM_DEBUG(path, CAM_DEBUG_TRACE, ("Registered SIM for ata%d\n", unit));
238 
239     setup_async_cb(scp, AC_LOST_DEVICE);
240     reinit_bus(scp, cold ? BOOT_ATTACH : ATTACH);
241     error = 0;
242 
243 out:
244     if (error != 0)
245 	free_softc(scp);
246 
247     return(error);
248 }
249 
250 static int
251 atapi_cam_detach(device_t dev)
252 {
253     struct atapi_xpt_softc *scp = device_get_softc(dev);
254 
255     get_mplock();
256     xpt_freeze_simq(scp->sim, 1 /*count*/);
257     rel_mplock();
258     spin_lock_wr(&scp->state_lock);
259     scp->flags |= DETACHING;
260     spin_unlock_wr(&scp->state_lock);
261     free_softc(scp);
262     return (0);
263 }
264 
265 static int
266 atapi_cam_reinit(device_t dev) {
267     struct atapi_xpt_softc *scp = device_get_softc(dev);
268 
269     /*
270      * scp might be null if the bus is being reinitialised during
271      * the boot-up sequence, before the ATAPI bus is registered.
272      */
273 
274     if (scp != NULL) {
275 	reinit_bus(scp, RESET);
276     }
277     return 0;
278 }
279 
280 static void
281 reinit_bus(struct atapi_xpt_softc *scp, enum reinit_reason reason) {
282     struct ata_device *atadev;
283     device_t *children;
284     int nchildren, i;
285 
286     if (device_get_children(scp->parent, &children, &nchildren) != 0) {
287 	return;
288     }
289 
290     spin_lock_wr(&scp->state_lock);
291     scp->atadev[0] = NULL;
292     scp->atadev[1] = NULL;
293 
294     for (i = 0; i < nchildren; i++) {
295 	/* XXX Does the child need to actually be attached yet? */
296 	if (children[i] != NULL) {
297 	    atadev = device_get_softc(children[i]);
298 	    if ((atadev->unit == ATA_MASTER) &&
299 		(scp->ata_ch->devices & ATA_ATAPI_MASTER) != 0)
300 		scp->atadev[0] = atadev;
301 	    if ((atadev->unit == ATA_SLAVE) &&
302 		(scp->ata_ch->devices & ATA_ATAPI_SLAVE) != 0)
303 		scp->atadev[1] = atadev;
304 	}
305     }
306     spin_unlock_wr(&scp->state_lock);
307     kfree(children, M_TEMP);
308 
309     switch (reason) {
310 	case BOOT_ATTACH:
311 	    break;
312 	case RESET:
313 	    xpt_async(AC_BUS_RESET, scp->path, NULL);
314 	    /*FALLTHROUGH*/
315 	case ATTACH:
316 	    cam_rescan(scp->sim);
317 	    break;
318     }
319 }
320 
321 static void
322 setup_async_cb(struct atapi_xpt_softc *scp, uint32_t events)
323 {
324     struct ccb_setasync csa;
325 
326     get_mplock();
327     xpt_setup_ccb(&csa.ccb_h, scp->path, /*priority*/ 5);
328     csa.ccb_h.func_code = XPT_SASYNC_CB;
329     csa.event_enable = events;
330     csa.callback = &atapi_async;
331     csa.callback_arg = scp->sim;
332     xpt_action((union ccb *) &csa);
333     rel_mplock();
334 }
335 
336 static void
337 atapi_action(struct cam_sim *sim, union ccb *ccb)
338 {
339     struct atapi_xpt_softc *softc = (struct atapi_xpt_softc*)cam_sim_softc(sim);
340     struct ccb_hdr *ccb_h = &ccb->ccb_h;
341     struct atapi_hcb *hcb = NULL;
342     struct ata_request *request = NULL;
343     int unit = cam_sim_unit(sim);
344     int bus = cam_sim_bus(sim);
345     int len;
346     char *buf;
347 
348     switch (ccb_h->func_code) {
349     case XPT_PATH_INQ: {
350 	struct ccb_pathinq *cpi = &ccb->cpi;
351 	int tid = ccb_h->target_id;
352 
353 	cpi->version_num = 1;
354 	cpi->hba_inquiry = 0;
355 	cpi->target_sprt = 0;
356 	cpi->hba_misc = PIM_NO_6_BYTE;
357 	cpi->hba_eng_cnt = 0;
358 	bzero(cpi->vuhba_flags, sizeof(cpi->vuhba_flags));
359 	cpi->max_target = 1;
360 	cpi->max_lun = 0;
361 	cpi->async_flags = 0;
362 	cpi->hpath_id = 0;
363 	cpi->initiator_id = 7;
364 	strncpy(cpi->sim_vid, "FreeBSD", sizeof(cpi->sim_vid));
365 	strncpy(cpi->hba_vid, "ATAPI", sizeof(cpi->hba_vid));
366 	strncpy(cpi->dev_name, cam_sim_name(sim), sizeof cpi->dev_name);
367 	cpi->unit_number = cam_sim_unit(sim);
368 	cpi->bus_id = cam_sim_bus(sim);
369 	cpi->base_transfer_speed = 3300;
370 
371 	if (softc->ata_ch && tid != CAM_TARGET_WILDCARD) {
372 	    spin_lock_wr(&softc->state_lock);
373 	    if (softc->atadev[tid] == NULL) {
374 		ccb->ccb_h.status = CAM_DEV_NOT_THERE;
375 		xpt_done(ccb);
376 		spin_unlock_wr(&softc->state_lock);
377 		return;
378 	    }
379 	    switch (softc->atadev[ccb_h->target_id]->mode) {
380 	    case ATA_PIO1:
381 		cpi->base_transfer_speed = 5200;
382 		break;
383 	    case ATA_PIO2:
384 		cpi->base_transfer_speed = 7000;
385 		break;
386 	    case ATA_PIO3:
387 		cpi->base_transfer_speed = 11000;
388 		break;
389 	    case ATA_PIO4:
390 	    case ATA_DMA:
391 	    case ATA_WDMA2:
392 		cpi->base_transfer_speed = 16000;
393 		break;
394 	    case ATA_UDMA2:
395 		cpi->base_transfer_speed = 33000;
396 		break;
397 	    case ATA_UDMA4:
398 		cpi->base_transfer_speed = 66000;
399 		break;
400 	    case ATA_UDMA5:
401 		cpi->base_transfer_speed = 100000;
402 		break;
403 	    case ATA_UDMA6:
404 		cpi->base_transfer_speed = 133000;
405 		break;
406 	    default:
407 		break;
408 	    }
409 	    spin_unlock_wr(&softc->state_lock);
410 	}
411 	ccb->ccb_h.status = CAM_REQ_CMP;
412 	xpt_done(ccb);
413 	return;
414     }
415 
416     case XPT_RESET_DEV: {
417 	int tid = ccb_h->target_id;
418 
419 	CAM_DEBUG(ccb->ccb_h.path, CAM_DEBUG_SUBTRACE, ("dev reset\n"));
420 	ata_controlcmd(softc->atadev[tid]->dev, ATA_DEVICE_RESET, 0, 0, 0);
421 	ccb->ccb_h.status = CAM_REQ_CMP;
422 	xpt_done(ccb);
423 	return;
424     }
425 
426     case XPT_RESET_BUS:
427 	CAM_DEBUG(ccb->ccb_h.path, CAM_DEBUG_SUBTRACE, ("bus reset\n"));
428 	ata_reinit(softc->parent);
429 	ccb->ccb_h.status = CAM_REQ_CMP;
430 	xpt_done(ccb);
431 	return;
432 
433     case XPT_SET_TRAN_SETTINGS:
434 	/* ignore these, we're not doing SCSI here */
435 	CAM_DEBUG(ccb->ccb_h.path, CAM_DEBUG_SUBTRACE,
436 		  ("SET_TRAN_SETTINGS not supported\n"));
437 	ccb->ccb_h.status = CAM_FUNC_NOTAVAIL;
438 	xpt_done(ccb);
439 	return;
440 
441     case XPT_GET_TRAN_SETTINGS: {
442 	struct ccb_trans_settings *cts = &ccb->cts;
443 
444 	/*
445 	 * XXX The default CAM transport code is very SCSI-specific and
446 	 * doesn't understand IDE speeds very well. Be silent about it
447 	 * here and let it default to what is set in XPT_PATH_INQ
448 	 */
449 	CAM_DEBUG(ccb->ccb_h.path, CAM_DEBUG_SUBTRACE, ("GET_TRAN_SETTINGS\n"));
450 	cts->valid = (CCB_TRANS_DISC_VALID | CCB_TRANS_TQ_VALID);
451 	cts->flags &= ~(CCB_TRANS_DISC_ENB | CCB_TRANS_TAG_ENB);
452 	ccb->ccb_h.status = CAM_REQ_CMP;
453 	xpt_done(ccb);
454 	return;
455     }
456 
457     case XPT_CALC_GEOMETRY: {
458 	CAM_DEBUG(ccb->ccb_h.path, CAM_DEBUG_SUBTRACE, ("CALC_GEOMETRY\n"));
459 	cam_calc_geometry(&ccb->ccg, /*extended*/1);
460 	xpt_done(ccb);
461 	return;
462     }
463 
464     case XPT_SCSI_IO: {
465 	struct ccb_scsiio *csio = &ccb->csio;
466 	int tid = ccb_h->target_id, lid = ccb_h->target_lun;
467 	int request_flags = ATA_R_QUIET | ATA_R_ATAPI;
468 
469 	CAM_DEBUG(ccb_h->path, CAM_DEBUG_SUBTRACE, ("XPT_SCSI_IO\n"));
470 
471 	spin_lock_wr(&softc->state_lock);
472 	if (softc->flags & DETACHING) {
473 	    ccb->ccb_h.status = CAM_REQ_ABORTED;
474 	    xpt_done(ccb);
475 	    spin_unlock_wr(&softc->state_lock);
476 	    return;
477 	}
478 
479 	if (softc->atadev[tid] == NULL) {
480 	    ccb->ccb_h.status = CAM_DEV_NOT_THERE;
481 	    xpt_done(ccb);
482 	    spin_unlock_wr(&softc->state_lock);
483 	    return;
484 	}
485 
486 	/* check that this request was not aborted already */
487 	if ((ccb_h->status & CAM_STATUS_MASK) != CAM_REQ_INPROG) {
488 	    kprintf("XPT_SCSI_IO received but already in progress?\n");
489 	    xpt_done(ccb);
490 	    spin_unlock_wr(&softc->state_lock);
491 	    return;
492 	}
493 	if (lid > 0) {
494 	    CAM_DEBUG(ccb_h->path, CAM_DEBUG_SUBTRACE,
495 		      ("SCSI IO received for invalid lun %d\n", lid));
496 	    goto action_invalid;
497 	}
498 	if (csio->cdb_len > sizeof request->u.atapi.ccb) {
499 	    CAM_DEBUG(ccb_h->path, CAM_DEBUG_SUBTRACE,
500 		("CAM CCB too long for ATAPI"));
501 	    goto action_invalid;
502 	}
503 	if ((ccb_h->flags & CAM_SCATTER_VALID)) {
504 	    /* scatter-gather not supported */
505 	    xpt_print_path(ccb_h->path);
506 	    kprintf("ATAPI/CAM does not support scatter-gather yet!\n");
507 	    goto action_invalid;
508 	}
509 
510 	switch (ccb_h->flags & CAM_DIR_MASK) {
511 	case CAM_DIR_IN:
512 	     request_flags |= ATA_R_READ|ATA_R_DMA;
513 	     break;
514 	case CAM_DIR_OUT:
515 	     request_flags |= ATA_R_WRITE|ATA_R_DMA;
516 	     break;
517 	case CAM_DIR_NONE:
518 	     /* No flags need to be set */
519 	     break;
520 	default:
521 	     device_printf(softc->dev, "unknown IO operation\n");
522 	     goto action_invalid;
523 	}
524 	if (softc->atadev[tid]->mode < ATA_DMA)
525 	    request_flags &= ~ATA_R_DMA;
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 #endif
547 
548 	len = csio->dxfer_len;
549 	buf = csio->data_ptr;
550 
551 	/* some SCSI commands require special processing */
552 	switch (request->u.atapi.ccb[0]) {
553 	case INQUIRY: {
554 	    /*
555 	     * many ATAPI devices seem to report more than
556 	     * SHORT_INQUIRY_LENGTH bytes of available INQUIRY
557 	     * information, but respond with some incorrect condition
558 	     * when actually asked for it, so we are going to pretend
559 	     * that only SHORT_INQUIRY_LENGTH are expected, anyway.
560 	     */
561 	    struct scsi_inquiry *inq = (struct scsi_inquiry *) &request->u.atapi.ccb[0];
562 
563 	    if (inq->byte2 == 0 && inq->page_code == 0 &&
564 		inq->length > SHORT_INQUIRY_LENGTH) {
565 		bzero(buf, len);
566 		len = inq->length = SHORT_INQUIRY_LENGTH;
567 	    }
568 	    break;
569 	}
570 	case READ_6:
571 	    /* FALLTHROUGH */
572 
573 	case WRITE_6:
574 	    CAM_DEBUG(ccb_h->path, CAM_DEBUG_SUBTRACE,
575 		      ("Translating %s into _10 equivalent\n",
576 		      (request->u.atapi.ccb[0] == READ_6) ? "READ_6" : "WRITE_6"));
577 	    request->u.atapi.ccb[0] |= 0x20;
578 	    request->u.atapi.ccb[9] = request->u.atapi.ccb[5];
579 	    request->u.atapi.ccb[8] = request->u.atapi.ccb[4];
580 	    request->u.atapi.ccb[7] = 0;
581 	    request->u.atapi.ccb[6] = 0;
582 	    request->u.atapi.ccb[5] = request->u.atapi.ccb[3];
583 	    request->u.atapi.ccb[4] = request->u.atapi.ccb[2];
584 	    request->u.atapi.ccb[3] = request->u.atapi.ccb[1] & 0x1f;
585 	    request->u.atapi.ccb[2] = 0;
586 	    request->u.atapi.ccb[1] = 0;
587 	    break;
588 	}
589 
590 	if ((ccb_h->flags & CAM_DIR_MASK) == CAM_DIR_IN && (len & 1)) {
591 	    /* ATA always transfers an even number of bytes */
592 	    if ((buf = hcb->dxfer_alloc
593 		 = kmalloc(++len, M_ATACAM, M_NOWAIT | M_ZERO)) == NULL) {
594 		kprintf("cannot allocate ATAPI/CAM buffer\n");
595 		goto action_oom;
596 	    }
597 	}
598 	request->dev = softc->atadev[tid]->dev;
599 	request->driver = hcb;
600 	request->data = buf;
601 	request->bytecount = len;
602 	request->transfersize = min(request->bytecount, 65534);
603 	request->timeout = ccb_h->timeout / 1000; /* XXX lost granularity */
604 	request->retries = 2;
605 	request->callback = &atapi_cb;
606 	request->flags = request_flags;
607 
608 	TAILQ_INSERT_TAIL(&softc->pending_hcbs, hcb, chain);
609 	hcb->flags |= QUEUED;
610 	ccb_h->status |= CAM_SIM_QUEUED;
611 	spin_unlock_wr(&softc->state_lock);
612 
613 	ata_queue_request(request);
614 	return;
615     }
616 
617     default:
618 	CAM_DEBUG(ccb_h->path, CAM_DEBUG_SUBTRACE,
619 		  ("unsupported function code 0x%02x\n", ccb_h->func_code));
620 	goto action_invalid;
621     }
622 
623     /* NOTREACHED */
624 
625 action_oom:
626     if (request != NULL)
627 	ata_free_request(request);
628     if (hcb != NULL)
629 	free_hcb(hcb);
630     spin_unlock_wr(&softc->state_lock);
631     get_mplock();
632     xpt_print_path(ccb_h->path);
633     kprintf("out of memory, freezing queue.\n");
634     softc->flags |= RESOURCE_SHORTAGE;
635     xpt_freeze_simq(sim, /*count*/ 1);
636     rel_mplock();
637     ccb_h->status = CAM_REQUEUE_REQ;
638     xpt_done(ccb);
639     return;
640 
641 action_invalid:
642     spin_unlock_wr(&softc->state_lock);
643     ccb_h->status = CAM_REQ_INVALID;
644     xpt_done(ccb);
645     return;
646 }
647 
648 static void
649 atapi_poll(struct cam_sim *sim)
650 {
651     /* do nothing - we do not actually service any interrupts */
652     kprintf("atapi_poll called!\n");
653 }
654 
655 static void
656 atapi_cb(struct ata_request *request)
657 {
658     struct atapi_xpt_softc *scp;
659     struct atapi_hcb *hcb;
660     struct ccb_scsiio *csio;
661     u_int32_t rc;
662 
663     hcb = (struct atapi_hcb *)request->driver;
664     scp = hcb->softc;
665     csio = &hcb->ccb->csio;
666 
667 #ifdef CAMDEBUG
668 # define err (request->u.atapi.sense.key)
669     if (CAM_DEBUGGED(csio->ccb_h.path, CAM_DEBUG_CDB)) {
670 	kprintf("atapi_cb: hcb@%p error = %02x: (sk = %02x%s%s%s)\n",
671 	       hcb, err, err >> 4,
672 	       (err & 4) ? " ABRT" : "",
673 	       (err & 2) ? " EOM" : "",
674 	       (err & 1) ? " ILI" : "");
675 	kprintf("dev %s: cmd %02x status %02x result %02x\n",
676 	    device_get_nameunit(request->dev), request->u.atapi.ccb[0],
677 	    request->status, request->result);
678     }
679 #endif
680 
681     if ((hcb->flags & AUTOSENSE) != 0) {
682 	rc = CAM_SCSI_STATUS_ERROR;
683 	if (request->result == 0) {
684 	    csio->ccb_h.status |= CAM_AUTOSNS_VALID;
685 	}
686     } else if (request->result != 0) {
687 	rc = CAM_SCSI_STATUS_ERROR;
688 	csio->scsi_status = SCSI_STATUS_CHECK_COND;
689 
690 	if ((csio->ccb_h.flags & CAM_DIS_AUTOSENSE) == 0) {
691 #if 0
692 	    static const int8_t ccb[16] = { ATAPI_REQUEST_SENSE, 0, 0, 0,
693 		sizeof(struct atapi_sense), 0, 0, 0, 0, 0, 0,
694 		0, 0, 0, 0, 0 };
695 
696 	    bcopy (ccb, request->u.atapi.ccb, sizeof ccb);
697 	    request->data = (caddr_t)&csio->sense_data;
698 	    request->bytecount = sizeof(struct atapi_sense);
699 	    request->transfersize = min(request->bytecount, 65534);
700 	    request->timeout = csio->ccb_h.timeout / 1000;
701 	    request->retries = 2;
702 	    request->flags = ATA_R_QUIET|ATA_R_ATAPI|ATA_R_IMMEDIATE;
703 	    hcb->flags |= AUTOSENSE;
704 
705 	    ata_queue_request(request);
706 	    return;
707 #else
708 	    /* The ATA driver has already requested sense for us. */
709 	    if (request->error == 0) {
710 		/* The ATA autosense suceeded. */
711 		bcopy (&request->u.atapi.sense, &csio->sense_data, sizeof(struct atapi_sense));
712 		csio->ccb_h.status |= CAM_AUTOSNS_VALID;
713 	    }
714 #endif
715 	}
716     } else {
717 	rc = CAM_REQ_CMP;
718 	csio->scsi_status = SCSI_STATUS_OK;
719 	if (((csio->ccb_h.flags & CAM_DIR_MASK) == CAM_DIR_IN) &&
720 	    hcb->dxfer_alloc != NULL)
721 	{
722 	    bcopy(hcb->dxfer_alloc, csio->data_ptr, csio->dxfer_len);
723 	}
724     }
725 
726     spin_lock_wr(&scp->state_lock);
727     free_hcb_and_ccb_done(hcb, rc);
728     spin_unlock_wr(&scp->state_lock);
729 
730     ata_free_request(request);
731 }
732 
733 static void
734 free_hcb_and_ccb_done(struct atapi_hcb *hcb, u_int32_t status)
735 {
736     struct atapi_xpt_softc *softc;
737     union ccb *ccb;
738 
739     if (hcb == NULL)
740 	return;
741 
742     softc = hcb->softc;
743     ccb = hcb->ccb;
744 
745     /* we're about to free a hcb, so the shortage has ended */
746     if (softc->flags & RESOURCE_SHORTAGE) {
747 	softc->flags &= ~RESOURCE_SHORTAGE;
748 	status |= CAM_RELEASE_SIMQ;
749     }
750     free_hcb(hcb);
751     ccb->ccb_h.status =
752 	status | (ccb->ccb_h.status & ~(CAM_STATUS_MASK | CAM_SIM_QUEUED));
753     xpt_done(ccb);
754 }
755 
756 static void
757 atapi_async(void *callback_arg, u_int32_t code,
758 	     struct cam_path* path, void *arg)
759 {
760     struct atapi_xpt_softc *softc;
761     struct cam_sim *sim;
762     int targ;
763 
764     crit_enter();
765 
766     sim = (struct cam_sim *) callback_arg;
767     softc = (struct atapi_xpt_softc *) cam_sim_softc(sim);
768     switch (code) {
769     case AC_LOST_DEVICE:
770 	targ = xpt_path_target_id(path);
771 	xpt_print_path(path);
772 	if (targ == -1)
773 		kprintf("Lost host adapter\n");
774 	else
775 		kprintf("Lost target %d???\n", targ);
776 	break;
777 
778     default:
779 	break;
780     }
781 
782     crit_exit();
783 }
784 
785 static void
786 cam_rescan_callback(struct cam_periph *periph, union ccb *ccb)
787 {
788 	if (ccb->ccb_h.status != CAM_REQ_CMP) {
789 	    CAM_DEBUG(ccb->ccb_h.path, CAM_DEBUG_TRACE,
790 		      ("Rescan failed, 0x%04x\n", ccb->ccb_h.status));
791 	} else {
792 	    CAM_DEBUG(ccb->ccb_h.path, CAM_DEBUG_TRACE,
793 		      ("Rescan succeeded\n"));
794 	}
795 	xpt_free_path(ccb->ccb_h.path);
796 	kfree(ccb, M_ATACAM);
797 }
798 
799 static void
800 cam_rescan(struct cam_sim *sim)
801 {
802     struct cam_path *path;
803     union ccb *ccb = kmalloc(sizeof(union ccb), M_ATACAM, M_WAITOK | M_ZERO);
804 
805     get_mplock();
806     if (xpt_create_path(&path, xpt_periph, cam_sim_path(sim),
807 			CAM_TARGET_WILDCARD, CAM_LUN_WILDCARD) != CAM_REQ_CMP) {
808 	rel_mplock();
809 	kfree(ccb, M_ATACAM);
810 	return;
811     }
812 
813     CAM_DEBUG(ccb->ccb_h.path, CAM_DEBUG_TRACE, ("Rescanning ATAPI bus.\n"));
814     xpt_setup_ccb(&ccb->ccb_h, path, 5/*priority (low)*/);
815     ccb->ccb_h.func_code = XPT_SCAN_BUS;
816     ccb->ccb_h.cbfcnp = cam_rescan_callback;
817     ccb->crcn.flags = CAM_FLAG_NONE;
818     xpt_action(ccb);
819     /* scan is in progress now */
820     rel_mplock();
821 }
822 
823 static struct atapi_hcb *
824 allocate_hcb(struct atapi_xpt_softc *softc, int unit, int bus, union ccb *ccb)
825 {
826     struct atapi_hcb *hcb = (struct atapi_hcb *)
827     kmalloc(sizeof(struct atapi_hcb), M_ATACAM, M_NOWAIT | M_ZERO);
828 
829     if (hcb != NULL) {
830 	hcb->softc = softc;
831 	hcb->unit = unit;
832 	hcb->bus = bus;
833 	hcb->ccb = ccb;
834     }
835     return hcb;
836 }
837 
838 static void
839 free_hcb(struct atapi_hcb *hcb)
840 {
841     if ((hcb->flags & QUEUED) != 0)
842 	TAILQ_REMOVE(&hcb->softc->pending_hcbs, hcb, chain);
843     if (hcb->dxfer_alloc != NULL)
844 	kfree(hcb->dxfer_alloc, M_ATACAM);
845     kfree(hcb, M_ATACAM);
846 }
847 
848 static void
849 free_softc(struct atapi_xpt_softc *scp)
850 {
851     struct atapi_hcb *hcb;
852 
853     if (scp != NULL) {
854 	spin_lock_wr(&scp->state_lock);
855 	TAILQ_FOREACH(hcb, &scp->pending_hcbs, chain) {
856 	    free_hcb_and_ccb_done(hcb, CAM_UNREC_HBA_ERROR);
857 	}
858 	spin_unlock_wr(&scp->state_lock);
859 	get_mplock();
860 	if (scp->path != NULL) {
861 	    setup_async_cb(scp, 0);
862 	    xpt_free_path(scp->path);
863 	}
864 	if ((scp->flags & BUS_REGISTERED) != 0) {
865 	    if (xpt_bus_deregister(cam_sim_path(scp->sim)) == CAM_REQ_CMP)
866 		scp->flags &= ~BUS_REGISTERED;
867 	}
868 	if (scp->sim != NULL) {
869 	    if ((scp->flags & BUS_REGISTERED) == 0)
870 		cam_sim_free(scp->sim);
871 	    else
872 		kprintf("Can't free %s SIM (still registered)\n",
873 		       cam_sim_name(scp->sim));
874 	}
875 	rel_mplock();
876 	spin_uninit(&scp->state_lock);
877     }
878 }
879 
880 static int
881 atapi_cam_event_handler(module_t mod, int what, void *arg) {
882     device_t *devlist;
883     int devcount;
884 
885     switch (what) {
886 	case MOD_UNLOAD:
887 	    if (devclass_get_devices(atapi_cam_devclass, &devlist, &devcount)
888 		  != 0)
889 		return ENXIO;
890 	    if (devlist != NULL) {
891 		while (devlist != NULL && devcount > 0) {
892 		    device_t child = devlist[--devcount];
893 		    struct atapi_xpt_softc *scp = device_get_softc(child);
894 
895 		    device_delete_child(device_get_parent(child),child);
896 		    if (scp != NULL)
897 			kfree(scp, M_ATACAM);
898 		}
899 		kfree(devlist, M_TEMP);
900 	    }
901 	    break;
902 
903 	default:
904 	    break;
905     }
906     return 0;
907 }
908