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