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