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