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