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$");
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(device_t *dev, device_t parent);
95 static int atapi_cam_probe(device_t dev);
96 static int atapi_cam_attach(device_t dev);
97 static int atapi_cam_detach(device_t dev);
98 static int atapi_cam_reinit(device_t dev);
99
100 /* CAM XPT methods */
101 static void atapi_action(struct cam_sim *, union ccb *);
102 static void atapi_poll(struct cam_sim *);
103 static void atapi_async(void *, u_int32_t, struct cam_path *, void *);
104 static void atapi_cb(struct ata_request *);
105
106 /* Module methods */
107 static int atapi_cam_event_handler(module_t mod, int what, void *arg);
108
109 /* internal functions */
110 static void reinit_bus(struct atapi_xpt_softc *scp, enum reinit_reason reason);
111 static void setup_async_cb(struct atapi_xpt_softc *, uint32_t);
112 static void cam_rescan_callback(struct cam_periph *, union ccb *);
113 static void cam_rescan(struct cam_sim *);
114 static void free_hcb_and_ccb_done(struct atapi_hcb *, u_int32_t);
115 static struct atapi_hcb *allocate_hcb(struct atapi_xpt_softc *, int, int, union ccb *);
116 static void free_hcb(struct atapi_hcb *hcb);
117 static void free_softc(struct atapi_xpt_softc *scp);
118
119 static MALLOC_DEFINE(M_ATACAM, "ata_cam", "ATA driver CAM-XPT layer");
120
121 static device_method_t atapi_cam_methods[] = {
122 DEVMETHOD(device_identify, atapi_cam_identify),
123 DEVMETHOD(device_probe, atapi_cam_probe),
124 DEVMETHOD(device_attach, atapi_cam_attach),
125 DEVMETHOD(device_detach, atapi_cam_detach),
126 DEVMETHOD(ata_reinit, atapi_cam_reinit),
127 {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(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
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_ATA;
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 default:
418 break;
419 }
420 }
421 ccb->ccb_h.status = CAM_REQ_CMP;
422 xpt_done(ccb);
423 return;
424 }
425
426 case XPT_RESET_DEV: {
427 int tid = ccb_h->target_id;
428
429 CAM_DEBUG(ccb->ccb_h.path, CAM_DEBUG_SUBTRACE, ("dev reset\n"));
430 mtx_unlock(&softc->state_lock);
431 ata_controlcmd(softc->atadev[tid]->dev, ATA_DEVICE_RESET, 0, 0, 0);
432 mtx_lock(&softc->state_lock);
433 ccb->ccb_h.status = CAM_REQ_CMP;
434 xpt_done(ccb);
435 return;
436 }
437
438 case XPT_RESET_BUS:
439 CAM_DEBUG(ccb->ccb_h.path, CAM_DEBUG_SUBTRACE, ("bus reset\n"));
440 mtx_unlock(&softc->state_lock);
441 ata_reinit(softc->parent);
442 mtx_lock(&softc->state_lock);
443 ccb->ccb_h.status = CAM_REQ_CMP;
444 xpt_done(ccb);
445 return;
446
447 case XPT_SET_TRAN_SETTINGS:
448 /* ignore these, we're not doing SCSI here */
449 CAM_DEBUG(ccb->ccb_h.path, CAM_DEBUG_SUBTRACE,
450 ("SET_TRAN_SETTINGS not supported\n"));
451 ccb->ccb_h.status = CAM_FUNC_NOTAVAIL;
452 xpt_done(ccb);
453 return;
454
455 case XPT_GET_TRAN_SETTINGS: {
456 struct ccb_trans_settings *cts = &ccb->cts;
457 cts->protocol = PROTO_SCSI;
458 cts->protocol_version = SCSI_REV_2;
459 cts->transport = XPORT_ATA;
460 cts->transport_version = XPORT_VERSION_UNSPECIFIED;
461 cts->proto_specific.valid = 0;
462 cts->xport_specific.valid = 0;
463 /* nothing more to do */
464 ccb->ccb_h.status = CAM_REQ_CMP;
465 CAM_DEBUG(ccb->ccb_h.path, CAM_DEBUG_SUBTRACE, ("GET_TRAN_SETTINGS\n"));
466 xpt_done(ccb);
467 return;
468 }
469
470 case XPT_CALC_GEOMETRY: {
471 CAM_DEBUG(ccb->ccb_h.path, CAM_DEBUG_SUBTRACE, ("CALC_GEOMETRY\n"));
472 cam_calc_geometry(&ccb->ccg, /*extended*/1);
473 xpt_done(ccb);
474 return;
475 }
476
477 case XPT_SCSI_IO: {
478 struct ccb_scsiio *csio = &ccb->csio;
479 int tid = ccb_h->target_id, lid = ccb_h->target_lun;
480 int request_flags = ATA_R_ATAPI;
481
482 CAM_DEBUG(ccb_h->path, CAM_DEBUG_SUBTRACE, ("XPT_SCSI_IO\n"));
483
484 if (softc->flags & DETACHING) {
485 ccb->ccb_h.status = CAM_REQ_ABORTED;
486 xpt_done(ccb);
487 return;
488 }
489
490 if (softc->atadev[tid] == NULL) {
491 ccb->ccb_h.status = CAM_DEV_NOT_THERE;
492 xpt_done(ccb);
493 return;
494 }
495
496 /* check that this request was not aborted already */
497 if ((ccb_h->status & CAM_STATUS_MASK) != CAM_REQ_INPROG) {
498 printf("XPT_SCSI_IO received but already in progress?\n");
499 xpt_done(ccb);
500 return;
501 }
502 if (lid > 0) {
503 CAM_DEBUG(ccb_h->path, CAM_DEBUG_SUBTRACE,
504 ("SCSI IO received for invalid lun %d\n", lid));
505 goto action_invalid;
506 }
507 if (csio->cdb_len > sizeof request->u.atapi.ccb) {
508 CAM_DEBUG(ccb_h->path, CAM_DEBUG_SUBTRACE,
509 ("CAM CCB too long for ATAPI"));
510 goto action_invalid;
511 }
512 if ((ccb_h->flags & CAM_SCATTER_VALID)) {
513 /* scatter-gather not supported */
514 xpt_print_path(ccb_h->path);
515 printf("ATAPI/CAM does not support scatter-gather yet!\n");
516 goto action_invalid;
517 }
518
519 switch (ccb_h->flags & CAM_DIR_MASK) {
520 case CAM_DIR_IN:
521 request_flags |= ATA_R_READ;
522 break;
523 case CAM_DIR_OUT:
524 request_flags |= ATA_R_WRITE;
525 break;
526 case CAM_DIR_NONE:
527 /* No flags need to be set */
528 break;
529 default:
530 device_printf(softc->dev, "unknown IO operation\n");
531 goto action_invalid;
532 }
533
534 if ((hcb = allocate_hcb(softc, unit, bus, ccb)) == NULL) {
535 printf("cannot allocate ATAPI/CAM hcb\n");
536 goto action_oom;
537 }
538 if ((request = ata_alloc_request()) == NULL) {
539 printf("cannot allocate ATAPI/CAM request\n");
540 goto action_oom;
541 }
542
543 bcopy((ccb_h->flags & CAM_CDB_POINTER) ?
544 csio->cdb_io.cdb_ptr : csio->cdb_io.cdb_bytes,
545 request->u.atapi.ccb, csio->cdb_len);
546 #ifdef CAMDEBUG
547 if (CAM_DEBUGGED(ccb_h->path, CAM_DEBUG_CDB)) {
548 char cdb_str[(SCSI_MAX_CDBLEN * 3) + 1];
549
550 printf("atapi_action: hcb@%p: %s\n", hcb,
551 scsi_cdb_string(request->u.atapi.ccb, cdb_str, sizeof(cdb_str)));
552 }
553 if (CAM_DEBUGGED(ccb_h->path, CAM_DEBUG_SUBTRACE)) {
554 request_flags |= ATA_R_DEBUG;
555 }
556 #endif
557
558 len = csio->dxfer_len;
559 buf = csio->data_ptr;
560
561 /* some SCSI commands require special processing */
562 switch (request->u.atapi.ccb[0]) {
563 case INQUIRY: {
564 /*
565 * many ATAPI devices seem to report more than
566 * SHORT_INQUIRY_LENGTH bytes of available INQUIRY
567 * information, but respond with some incorrect condition
568 * when actually asked for it, so we are going to pretend
569 * that only SHORT_INQUIRY_LENGTH are expected, anyway.
570 */
571 struct scsi_inquiry *inq = (struct scsi_inquiry *) &request->u.atapi.ccb[0];
572
573 if (inq->byte2 == 0 && inq->page_code == 0 &&
574 inq->length > SHORT_INQUIRY_LENGTH) {
575 bzero(buf, len);
576 len = inq->length = SHORT_INQUIRY_LENGTH;
577 }
578 break;
579 }
580 case READ_6:
581 /* FALLTHROUGH */
582
583 case WRITE_6:
584 CAM_DEBUG(ccb_h->path, CAM_DEBUG_SUBTRACE,
585 ("Translating %s into _10 equivalent\n",
586 (request->u.atapi.ccb[0] == READ_6) ? "READ_6" : "WRITE_6"));
587 request->u.atapi.ccb[0] |= 0x20;
588 request->u.atapi.ccb[9] = request->u.atapi.ccb[5];
589 request->u.atapi.ccb[8] = request->u.atapi.ccb[4];
590 request->u.atapi.ccb[7] = 0;
591 request->u.atapi.ccb[6] = 0;
592 request->u.atapi.ccb[5] = request->u.atapi.ccb[3];
593 request->u.atapi.ccb[4] = request->u.atapi.ccb[2];
594 request->u.atapi.ccb[3] = request->u.atapi.ccb[1] & 0x1f;
595 request->u.atapi.ccb[2] = 0;
596 request->u.atapi.ccb[1] = 0;
597 /* FALLTHROUGH */
598
599 case READ_10:
600 /* FALLTHROUGH */
601 case WRITE_10:
602 /* FALLTHROUGH */
603 case READ_12:
604 /* FALLTHROUGH */
605 case WRITE_12:
606 /*
607 * Enable DMA (if target supports it) for READ and WRITE commands
608 * only, as some combinations of drive, controller and chipset do
609 * not behave correctly when DMA is enabled for other commands.
610 */
611 if (softc->atadev[tid]->mode >= ATA_DMA)
612 request_flags |= ATA_R_DMA;
613 break;
614
615 }
616
617 if ((ccb_h->flags & CAM_DIR_MASK) == CAM_DIR_IN && (len & 1)) {
618 /* ATA always transfers an even number of bytes */
619 if ((buf = hcb->dxfer_alloc
620 = malloc(++len, M_ATACAM, M_NOWAIT | M_ZERO)) == NULL) {
621 printf("cannot allocate ATAPI/CAM buffer\n");
622 goto action_oom;
623 }
624 }
625 request->dev = softc->atadev[tid]->dev;
626 request->driver = hcb;
627 request->data = buf;
628 request->bytecount = len;
629 request->transfersize = min(request->bytecount, 65534);
630 request->timeout = ccb_h->timeout / 1000; /* XXX lost granularity */
631 request->callback = &atapi_cb;
632 request->flags = request_flags;
633
634 /*
635 * no retries are to be performed at the ATA level; any retries
636 * will be done by CAM.
637 */
638 request->retries = 0;
639
640 TAILQ_INSERT_TAIL(&softc->pending_hcbs, hcb, chain);
641 hcb->flags |= QUEUED;
642 ccb_h->status |= CAM_SIM_QUEUED;
643 mtx_unlock(&softc->state_lock);
644
645 ata_queue_request(request);
646 mtx_lock(&softc->state_lock);
647 return;
648 }
649
650 default:
651 CAM_DEBUG(ccb_h->path, CAM_DEBUG_SUBTRACE,
652 ("unsupported function code 0x%02x\n", ccb_h->func_code));
653 goto action_invalid;
654 }
655
656 /* NOTREACHED */
657
658 action_oom:
659 if (request != NULL)
660 ata_free_request(request);
661 if (hcb != NULL)
662 free_hcb(hcb);
663 xpt_print_path(ccb_h->path);
664 printf("out of memory, freezing queue.\n");
665 softc->flags |= RESOURCE_SHORTAGE;
666 xpt_freeze_simq(sim, /*count*/ 1);
667 ccb_h->status = CAM_REQUEUE_REQ;
668 xpt_done(ccb);
669 mtx_unlock(&softc->state_lock);
670 return;
671
672 action_invalid:
673 ccb_h->status = CAM_REQ_INVALID;
674 xpt_done(ccb);
675 mtx_unlock(&softc->state_lock);
676 return;
677 }
678
679 static void
680 atapi_poll(struct cam_sim *sim)
681 {
682 /* do nothing - we do not actually service any interrupts */
683 printf("atapi_poll called!\n");
684 }
685
686 static void
687 atapi_cb(struct ata_request *request)
688 {
689 struct atapi_xpt_softc *scp;
690 struct atapi_hcb *hcb;
691 struct ccb_scsiio *csio;
692 u_int32_t rc;
693
694 hcb = (struct atapi_hcb *)request->driver;
695 scp = hcb->softc;
696 csio = &hcb->ccb->csio;
697
698 #ifdef CAMDEBUG
699 # define err (request->u.atapi.sense.key)
700 if (CAM_DEBUGGED(csio->ccb_h.path, CAM_DEBUG_CDB)) {
701 printf("atapi_cb: hcb@%p sense = %02x: sk = %01x%s%s%s\n",
702 hcb, err, err & 0x0f,
703 (err & 0x80) ? ", Filemark" : "",
704 (err & 0x40) ? ", EOM" : "",
705 (err & 0x20) ? ", ILI" : "");
706 device_printf(request->dev,
707 "cmd %s status %02x result %02x error %02x\n",
708 ata_cmd2str(request),
709 request->status, request->result, request->error);
710 }
711 #endif
712
713 if ((hcb->flags & AUTOSENSE) != 0) {
714 rc = CAM_SCSI_STATUS_ERROR;
715 if (request->result == 0) {
716 csio->ccb_h.status |= CAM_AUTOSNS_VALID;
717 }
718 } else if (request->result != 0) {
719 if ((request->flags & ATA_R_TIMEOUT) != 0) {
720 rc = CAM_CMD_TIMEOUT;
721 } else {
722 rc = CAM_SCSI_STATUS_ERROR;
723 csio->scsi_status = SCSI_STATUS_CHECK_COND;
724
725 if ((csio->ccb_h.flags & CAM_DIS_AUTOSENSE) == 0) {
726 #if 0
727 static const int8_t ccb[16] = { ATAPI_REQUEST_SENSE, 0, 0, 0,
728 sizeof(struct atapi_sense), 0, 0, 0, 0, 0, 0,
729 0, 0, 0, 0, 0 };
730
731 bcopy (ccb, request->u.atapi.ccb, sizeof ccb);
732 request->data = (caddr_t)&csio->sense_data;
733 request->bytecount = sizeof(struct atapi_sense);
734 request->transfersize = min(request->bytecount, 65534);
735 request->timeout = csio->ccb_h.timeout / 1000;
736 request->retries = 2;
737 request->flags = ATA_R_QUIET|ATA_R_ATAPI|ATA_R_IMMEDIATE;
738 hcb->flags |= AUTOSENSE;
739
740 ata_queue_request(request);
741 return;
742 #else
743 /*
744 * Use auto-sense data from the ATA layer, if it has
745 * issued a REQUEST SENSE automatically and that operation
746 * returned without error.
747 */
748 if (request->u.atapi.sense.key != 0 && request->error == 0) {
749 bcopy (&request->u.atapi.sense, &csio->sense_data, sizeof(struct atapi_sense));
750 csio->ccb_h.status |= CAM_AUTOSNS_VALID;
751 }
752 }
753 #endif
754 }
755 } else {
756 rc = CAM_REQ_CMP;
757 csio->scsi_status = SCSI_STATUS_OK;
758 if (((csio->ccb_h.flags & CAM_DIR_MASK) == CAM_DIR_IN) &&
759 hcb->dxfer_alloc != NULL)
760 {
761 bcopy(hcb->dxfer_alloc, csio->data_ptr, csio->dxfer_len);
762 }
763 }
764
765 mtx_lock(&scp->state_lock);
766 free_hcb_and_ccb_done(hcb, rc);
767 mtx_unlock(&scp->state_lock);
768
769 ata_free_request(request);
770 }
771
772 static void
773 free_hcb_and_ccb_done(struct atapi_hcb *hcb, u_int32_t status)
774 {
775 struct atapi_xpt_softc *softc;
776 union ccb *ccb;
777
778 if (hcb == NULL)
779 return;
780
781 softc = hcb->softc;
782 ccb = hcb->ccb;
783
784 /* we're about to free a hcb, so the shortage has ended */
785 if (softc->flags & RESOURCE_SHORTAGE) {
786 softc->flags &= ~RESOURCE_SHORTAGE;
787 status |= CAM_RELEASE_SIMQ;
788 }
789 free_hcb(hcb);
790 ccb->ccb_h.status =
791 status | (ccb->ccb_h.status & ~(CAM_STATUS_MASK | CAM_SIM_QUEUED));
792 xpt_done(ccb);
793 }
794
795 static void
796 atapi_async(void *callback_arg, u_int32_t code,
797 struct cam_path* path, void *arg)
798 {
799 struct atapi_xpt_softc *softc;
800 struct cam_sim *sim;
801 int targ;
802
803 GIANT_REQUIRED;
804
805 sim = (struct cam_sim *) callback_arg;
806 softc = (struct atapi_xpt_softc *) cam_sim_softc(sim);
807 switch (code) {
808 case AC_LOST_DEVICE:
809 targ = xpt_path_target_id(path);
810 xpt_print_path(path);
811 if (targ == -1)
812 printf("Lost host adapter\n");
813 else
814 printf("Lost target %d???\n", targ);
815 break;
816
817 default:
818 break;
819 }
820 }
821
822 static void
823 cam_rescan_callback(struct cam_periph *periph, union ccb *ccb)
824 {
825 if (ccb->ccb_h.status != CAM_REQ_CMP) {
826 CAM_DEBUG(ccb->ccb_h.path, CAM_DEBUG_TRACE,
827 ("Rescan failed, 0x%04x\n", ccb->ccb_h.status));
828 } else {
829 CAM_DEBUG(ccb->ccb_h.path, CAM_DEBUG_TRACE,
830 ("Rescan succeeded\n"));
831 }
832 xpt_free_path(ccb->ccb_h.path);
833 xpt_free_ccb(ccb);
834 }
835
836 static void
837 cam_rescan(struct cam_sim *sim)
838 {
839 struct cam_path *path;
840 union ccb *ccb;
841
842 ccb = xpt_alloc_ccb_nowait();
843 if (ccb == NULL)
844 return;
845
846 if (xpt_create_path(&path, xpt_periph, cam_sim_path(sim),
847 CAM_TARGET_WILDCARD, CAM_LUN_WILDCARD) != CAM_REQ_CMP) {
848 xpt_free_ccb(ccb);
849 return;
850 }
851
852 CAM_DEBUG(path, CAM_DEBUG_TRACE, ("Rescanning ATAPI bus.\n"));
853 xpt_setup_ccb(&ccb->ccb_h, path, 5/*priority (low)*/);
854 ccb->ccb_h.func_code = XPT_SCAN_BUS;
855 ccb->ccb_h.cbfcnp = cam_rescan_callback;
856 ccb->crcn.flags = CAM_FLAG_NONE;
857 xpt_action(ccb);
858 /* scan is in progress now */
859 }
860
861 static struct atapi_hcb *
862 allocate_hcb(struct atapi_xpt_softc *softc, int unit, int bus, union ccb *ccb)
863 {
864 struct atapi_hcb *hcb = (struct atapi_hcb *)
865 malloc(sizeof(struct atapi_hcb), M_ATACAM, M_NOWAIT | M_ZERO);
866
867 if (hcb != NULL) {
868 hcb->softc = softc;
869 hcb->unit = unit;
870 hcb->bus = bus;
871 hcb->ccb = ccb;
872 }
873 return hcb;
874 }
875
876 static void
877 free_hcb(struct atapi_hcb *hcb)
878 {
879 if ((hcb->flags & QUEUED) != 0)
880 TAILQ_REMOVE(&hcb->softc->pending_hcbs, hcb, chain);
881 if (hcb->dxfer_alloc != NULL)
882 free(hcb->dxfer_alloc, M_ATACAM);
883 free(hcb, M_ATACAM);
884 }
885
886 static void
887 free_softc(struct atapi_xpt_softc *scp)
888 {
889 struct atapi_hcb *hcb;
890
891 if (scp != NULL) {
892 mtx_lock(&scp->state_lock);
893 TAILQ_FOREACH(hcb, &scp->pending_hcbs, chain) {
894 free_hcb_and_ccb_done(hcb, CAM_UNREC_HBA_ERROR);
895 }
896 if (scp->path != NULL) {
897 setup_async_cb(scp, 0);
898 xpt_free_path(scp->path);
899 }
900 if ((scp->flags & BUS_REGISTERED) != 0) {
901 if (xpt_bus_deregister(cam_sim_path(scp->sim)) == CAM_REQ_CMP)
902 scp->flags &= ~BUS_REGISTERED;
903 }
904 if (scp->sim != NULL) {
905 if ((scp->flags & BUS_REGISTERED) == 0)
906 cam_sim_free(scp->sim, /*free_devq*/TRUE);
907 else
908 printf("Can't free %s SIM (still registered)\n",
909 cam_sim_name(scp->sim));
910 }
911 mtx_destroy(&scp->state_lock);
912 }
913 }
914
915 static int
916 atapi_cam_event_handler(module_t mod, int what, void *arg) {
917 device_t *devlist;
918 int devcount;
919
920 switch (what) {
921 case MOD_UNLOAD:
922 if (devclass_get_devices(atapi_cam_devclass, &devlist, &devcount)
923 != 0)
924 return ENXIO;
925 if (devlist != NULL) {
926 while (devlist != NULL && devcount > 0) {
927 device_t child = devlist[--devcount];
928 struct atapi_xpt_softc *scp = device_get_softc(child);
929
930 device_delete_child(device_get_parent(child),child);
931 if (scp != NULL)
932 free(scp, M_ATACAM);
933 }
934 free(devlist, M_TEMP);
935 }
936 break;
937
938 default:
939 break;
940 }
941 return 0;
942 }
Cache object: ad3c32039646bd90f7b239f4c8ef2b90
|