FreeBSD/Linux Kernel Cross Reference
sys/cam/scsi/scsi_sg.c
1 /*-
2 * Copyright (c) 2007 Scott Long
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. The name of the author may not be used to endorse or promote products
12 * derived from this software without specific prior written permission.
13 *
14 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
15 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
16 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
17 * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE FOR
18 * ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
19 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
20 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
21 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
22 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
23 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
24 * SUCH DAMAGE.
25 */
26
27 /*
28 * scsi_sg peripheral driver. This driver is meant to implement the Linux
29 * SG passthrough interface for SCSI.
30 */
31
32 #include <sys/cdefs.h>
33 __FBSDID("$FreeBSD$");
34
35 #include <sys/param.h>
36 #include <sys/systm.h>
37 #include <sys/kernel.h>
38 #include <sys/types.h>
39 #include <sys/bio.h>
40 #include <sys/malloc.h>
41 #include <sys/fcntl.h>
42 #include <sys/ioccom.h>
43 #include <sys/conf.h>
44 #include <sys/errno.h>
45 #include <sys/devicestat.h>
46 #include <sys/proc.h>
47 #include <sys/uio.h>
48
49 #include <cam/cam.h>
50 #include <cam/cam_ccb.h>
51 #include <cam/cam_periph.h>
52 #include <cam/cam_queue.h>
53 #include <cam/cam_xpt_periph.h>
54 #include <cam/cam_debug.h>
55 #include <cam/cam_sim.h>
56
57 #include <cam/scsi/scsi_all.h>
58 #include <cam/scsi/scsi_message.h>
59 #include <cam/scsi/scsi_sg.h>
60
61 #include <compat/linux/linux_ioctl.h>
62
63 typedef enum {
64 SG_FLAG_OPEN = 0x01,
65 SG_FLAG_LOCKED = 0x02,
66 SG_FLAG_INVALID = 0x04
67 } sg_flags;
68
69 typedef enum {
70 SG_STATE_NORMAL
71 } sg_state;
72
73 typedef enum {
74 SG_RDWR_FREE,
75 SG_RDWR_INPROG,
76 SG_RDWR_DONE
77 } sg_rdwr_state;
78
79 typedef enum {
80 SG_CCB_RDWR_IO,
81 SG_CCB_WAITING
82 } sg_ccb_types;
83
84 #define ccb_type ppriv_field0
85 #define ccb_rdwr ppriv_ptr1
86
87 struct sg_rdwr {
88 TAILQ_ENTRY(sg_rdwr) rdwr_link;
89 int tag;
90 int state;
91 int buf_len;
92 char *buf;
93 union ccb *ccb;
94 union {
95 struct sg_header hdr;
96 struct sg_io_hdr io_hdr;
97 } hdr;
98 };
99
100 struct sg_softc {
101 sg_state state;
102 sg_flags flags;
103 struct devstat *device_stats;
104 TAILQ_HEAD(, sg_rdwr) rdwr_done;
105 struct cdev *dev;
106 int sg_timeout;
107 int sg_user_timeout;
108 uint8_t pd_type;
109 union ccb saved_ccb;
110 };
111
112 static d_open_t sgopen;
113 static d_close_t sgclose;
114 static d_ioctl_t sgioctl;
115 static d_write_t sgwrite;
116 static d_read_t sgread;
117
118 static periph_init_t sginit;
119 static periph_ctor_t sgregister;
120 static periph_oninv_t sgoninvalidate;
121 static periph_dtor_t sgcleanup;
122 static periph_start_t sgstart;
123 static void sgasync(void *callback_arg, uint32_t code,
124 struct cam_path *path, void *arg);
125 static void sgdone(struct cam_periph *periph, union ccb *done_ccb);
126 static int sgsendccb(struct cam_periph *periph, union ccb *ccb);
127 static int sgsendrdwr(struct cam_periph *periph, union ccb *ccb);
128 static int sgerror(union ccb *ccb, uint32_t cam_flags,
129 uint32_t sense_flags);
130 static void sg_scsiio_status(struct ccb_scsiio *csio,
131 u_short *hoststat, u_short *drvstat);
132
133 static int scsi_group_len(u_char cmd);
134
135 static struct periph_driver sgdriver =
136 {
137 sginit, "sg",
138 TAILQ_HEAD_INITIALIZER(sgdriver.units), /* gen */ 0
139 };
140 PERIPHDRIVER_DECLARE(sg, sgdriver);
141
142 static struct cdevsw sg_cdevsw = {
143 .d_version = D_VERSION,
144 .d_flags = D_NEEDGIANT,
145 .d_open = sgopen,
146 .d_close = sgclose,
147 .d_ioctl = sgioctl,
148 .d_write = sgwrite,
149 .d_read = sgread,
150 .d_name = "sg",
151 };
152
153 static int sg_version = 30125;
154
155 static void
156 sginit(void)
157 {
158 cam_status status;
159 struct cam_path *path;
160
161 /*
162 * Install a global async callback. This callback will receive aync
163 * callbacks like "new device found".
164 */
165 status = xpt_create_path(&path, /*periph*/NULL, CAM_XPT_PATH_ID,
166 CAM_TARGET_WILDCARD, CAM_LUN_WILDCARD);
167
168 if (status == CAM_REQ_CMP) {
169 struct ccb_setasync csa;
170
171 xpt_setup_ccb(&csa.ccb_h, path, /*priority*/5);
172 csa.ccb_h.func_code = XPT_SASYNC_CB;
173 csa.event_enable = AC_FOUND_DEVICE;
174 csa.callback = sgasync;
175 csa.callback_arg = NULL;
176 xpt_action((union ccb *)&csa);
177 status = csa.ccb_h.status;
178 xpt_free_path(path);
179 }
180
181 if (status != CAM_REQ_CMP) {
182 printf("sg: Failed to attach master async callbac "
183 "due to status 0x%x!\n", status);
184 }
185 }
186
187 static void
188 sgoninvalidate(struct cam_periph *periph)
189 {
190 struct sg_softc *softc;
191 struct ccb_setasync csa;
192
193 softc = (struct sg_softc *)periph->softc;
194
195 /*
196 * Deregister any async callbacks.
197 */
198 xpt_setup_ccb(&csa.ccb_h, periph->path, /*priority*/5);
199 csa.ccb_h.func_code = XPT_SASYNC_CB;
200 csa.event_enable = 0;
201 csa.callback = sgasync;
202 csa.callback_arg = periph;
203 xpt_action((union ccb *)&csa);
204
205 softc->flags |= SG_FLAG_INVALID;
206
207 /*
208 * XXX Return all queued I/O with ENXIO.
209 * XXX Handle any transactions queued to the card
210 * with XPT_ABORT_CCB.
211 */
212
213 if (bootverbose) {
214 xpt_print(periph->path, "lost device\n");
215 }
216 }
217
218 static void
219 sgcleanup(struct cam_periph *periph)
220 {
221 struct sg_softc *softc;
222
223 softc = (struct sg_softc *)periph->softc;
224 devstat_remove_entry(softc->device_stats);
225 destroy_dev(softc->dev);
226 if (bootverbose) {
227 xpt_print(periph->path, "removing device entry\n");
228 }
229 free(softc, M_DEVBUF);
230 }
231
232 static void
233 sgasync(void *callback_arg, uint32_t code, struct cam_path *path, void *arg)
234 {
235 struct cam_periph *periph;
236
237 periph = (struct cam_periph *)callback_arg;
238
239 switch (code) {
240 case AC_FOUND_DEVICE:
241 {
242 struct ccb_getdev *cgd;
243 cam_status status;
244
245 cgd = (struct ccb_getdev *)arg;
246 if (cgd == NULL)
247 break;
248
249 /*
250 * Allocate a peripheral instance for this device and
251 * start the probe process.
252 */
253 status = cam_periph_alloc(sgregister, sgoninvalidate,
254 sgcleanup, sgstart, "sg",
255 CAM_PERIPH_BIO, cgd->ccb_h.path,
256 sgasync, AC_FOUND_DEVICE, cgd);
257 if ((status != CAM_REQ_CMP) && (status != CAM_REQ_INPROG)) {
258 const struct cam_status_entry *entry;
259
260 entry = cam_fetch_status_entry(status);
261 printf("sgasync: Unable to attach new device "
262 "due to status %#x: %s\n", status, entry ?
263 entry->status_text : "Unknown");
264 }
265 break;
266 }
267 default:
268 cam_periph_async(periph, code, path, arg);
269 break;
270 }
271 }
272
273 static cam_status
274 sgregister(struct cam_periph *periph, void *arg)
275 {
276 struct sg_softc *softc;
277 struct ccb_setasync csa;
278 struct ccb_getdev *cgd;
279 int no_tags;
280
281 cgd = (struct ccb_getdev *)arg;
282 if (periph == NULL) {
283 printf("sgregister: periph was NULL!!\n");
284 return (CAM_REQ_CMP_ERR);
285 }
286
287 if (cgd == NULL) {
288 printf("sgregister: no getdev CCB, can't register device\n");
289 return (CAM_REQ_CMP_ERR);
290 }
291
292 softc = malloc(sizeof(*softc), M_DEVBUF, M_ZERO | M_NOWAIT);
293 if (softc == NULL) {
294 printf("sgregister: Unable to allocate softc\n");
295 return (CAM_REQ_CMP_ERR);
296 }
297
298 softc->state = SG_STATE_NORMAL;
299 softc->pd_type = SID_TYPE(&cgd->inq_data);
300 softc->sg_timeout = SG_DEFAULT_TIMEOUT / SG_DEFAULT_HZ * hz;
301 softc->sg_user_timeout = SG_DEFAULT_TIMEOUT;
302 TAILQ_INIT(&softc->rdwr_done);
303 periph->softc = softc;
304
305 /*
306 * We pass in 0 for all blocksize, since we don't know what the
307 * blocksize of the device is, if it even has a blocksize.
308 */
309 no_tags = (cgd->inq_data.flags & SID_CmdQue) == 0;
310 softc->device_stats = devstat_new_entry("sg",
311 unit2minor(periph->unit_number), 0,
312 DEVSTAT_NO_BLOCKSIZE
313 | (no_tags ? DEVSTAT_NO_ORDERED_TAGS : 0),
314 softc->pd_type |
315 DEVSTAT_TYPE_IF_SCSI |
316 DEVSTAT_TYPE_PASS,
317 DEVSTAT_PRIORITY_PASS);
318
319 /* Register the device */
320 softc->dev = make_dev(&sg_cdevsw, unit2minor(periph->unit_number),
321 UID_ROOT, GID_OPERATOR, 0600, "%s%d",
322 periph->periph_name, periph->unit_number);
323 (void)make_dev_alias(softc->dev, "sg%c", 'a' + periph->unit_number);
324 softc->dev->si_drv1 = periph;
325
326 /*
327 * Add as async callback so that we get
328 * notified if this device goes away.
329 */
330 xpt_setup_ccb(&csa.ccb_h, periph->path, /*priority*/5);
331 csa.ccb_h.func_code = XPT_SASYNC_CB;
332 csa.event_enable = AC_LOST_DEVICE;
333 csa.callback = sgasync;
334 csa.callback_arg = periph;
335 xpt_action((union ccb *)&csa);
336
337 if (bootverbose)
338 xpt_announce_periph(periph, NULL);
339
340 return (CAM_REQ_CMP);
341 }
342
343 static void
344 sgstart(struct cam_periph *periph, union ccb *start_ccb)
345 {
346 struct sg_softc *softc;
347
348 softc = (struct sg_softc *)periph->softc;
349
350 switch (softc->state) {
351 case SG_STATE_NORMAL:
352 start_ccb->ccb_h.ccb_type = SG_CCB_WAITING;
353 SLIST_INSERT_HEAD(&periph->ccb_list, &start_ccb->ccb_h,
354 periph_links.sle);
355 periph->immediate_priority = CAM_PRIORITY_NONE;
356 wakeup(&periph->ccb_list);
357 break;
358 }
359 }
360
361 static void
362 sgdone(struct cam_periph *periph, union ccb *done_ccb)
363 {
364 struct sg_softc *softc;
365 struct ccb_scsiio *csio;
366
367 softc = (struct sg_softc *)periph->softc;
368 csio = &done_ccb->csio;
369 switch (csio->ccb_h.ccb_type) {
370 case SG_CCB_WAITING:
371 /* Caller will release the CCB */
372 wakeup(&done_ccb->ccb_h.cbfcnp);
373 return;
374 case SG_CCB_RDWR_IO:
375 {
376 struct sg_rdwr *rdwr;
377 int state;
378
379 devstat_end_transaction(softc->device_stats,
380 csio->dxfer_len,
381 csio->tag_action & 0xf,
382 ((csio->ccb_h.flags & CAM_DIR_MASK) ==
383 CAM_DIR_NONE) ? DEVSTAT_NO_DATA :
384 (csio->ccb_h.flags & CAM_DIR_OUT) ?
385 DEVSTAT_WRITE : DEVSTAT_READ,
386 NULL, NULL);
387
388 rdwr = done_ccb->ccb_h.ccb_rdwr;
389 state = rdwr->state;
390 rdwr->state = SG_RDWR_DONE;
391 wakeup(rdwr);
392 break;
393 }
394 default:
395 panic("unknown sg CCB type");
396 }
397 }
398
399 static int
400 sgopen(struct cdev *dev, int flags, int fmt, struct thread *td)
401 {
402 struct cam_periph *periph;
403 struct sg_softc *softc;
404 int error = 0;
405
406 periph = (struct cam_periph *)dev->si_drv1;
407 if (periph == NULL)
408 return (ENXIO);
409
410 softc = (struct sg_softc *)periph->softc;
411 if (softc->flags & SG_FLAG_INVALID)
412 return (ENXIO);
413
414 /*
415 * Don't allow access when we're running at a high securelevel.
416 */
417 error = securelevel_gt(td->td_ucred, 1);
418 if (error)
419 return (error);
420
421 if ((error = cam_periph_lock(periph, PRIBIO | PCATCH)) != 0)
422 return (error);
423
424 if ((softc->flags & SG_FLAG_OPEN) == 0) {
425 if (cam_periph_acquire(periph) != CAM_REQ_CMP)
426 return (ENXIO);
427 softc->flags |= SG_FLAG_OPEN;
428 }
429
430 cam_periph_unlock(periph);
431
432 return (error);
433 }
434
435 static int
436 sgclose(struct cdev *dev, int flag, int fmt, struct thread *td)
437 {
438 struct cam_periph *periph;
439 struct sg_softc *softc;
440 int error;
441
442 periph = (struct cam_periph *)dev->si_drv1;
443 if (periph == NULL)
444 return (ENXIO);
445
446 softc = (struct sg_softc *)periph->softc;
447
448 if ((error = cam_periph_lock(periph, PRIBIO)) != 0)
449 return (error);
450
451 softc->flags &= ~SG_FLAG_OPEN;
452
453 cam_periph_unlock(periph);
454 cam_periph_release(periph);
455
456 return (0);
457 }
458
459 static int
460 sgioctl(struct cdev *dev, u_long cmd, caddr_t arg, int flag, struct thread *td)
461 {
462 union ccb *ccb;
463 struct ccb_scsiio *csio;
464 struct cam_periph *periph;
465 struct sg_softc *softc;
466 struct sg_io_hdr req;
467 int dir, error;
468
469 periph = (struct cam_periph *)dev->si_drv1;
470 if (periph == NULL)
471 return (ENXIO);
472
473 softc = (struct sg_softc *)periph->softc;
474 error = 0;
475
476 switch (cmd) {
477 case LINUX_SCSI_GET_BUS_NUMBER: {
478 int busno;
479
480 busno = xpt_path_path_id(periph->path);
481 error = copyout(&busno, arg, sizeof(busno));
482 break;
483 }
484 case LINUX_SCSI_GET_IDLUN: {
485 struct scsi_idlun idlun;
486 struct cam_sim *sim;
487
488 idlun.dev_id = xpt_path_target_id(periph->path);
489 sim = xpt_path_sim(periph->path);
490 idlun.host_unique_id = sim->unit_number;
491 error = copyout(&idlun, arg, sizeof(idlun));
492 break;
493 }
494 case SG_GET_VERSION_NUM:
495 case LINUX_SG_GET_VERSION_NUM:
496 error = copyout(&sg_version, arg, sizeof(sg_version));
497 break;
498 case SG_SET_TIMEOUT:
499 case LINUX_SG_SET_TIMEOUT: {
500 u_int user_timeout;
501
502 error = copyin(arg, &user_timeout, sizeof(u_int));
503 if (error == 0) {
504 softc->sg_user_timeout = user_timeout;
505 softc->sg_timeout = user_timeout / SG_DEFAULT_HZ * hz;
506 }
507 break;
508 }
509 case SG_GET_TIMEOUT:
510 case LINUX_SG_GET_TIMEOUT:
511 /*
512 * The value is returned directly to the syscall.
513 */
514 td->td_retval[0] = softc->sg_user_timeout;
515 error = 0;
516 break;
517 case SG_IO:
518 case LINUX_SG_IO:
519 error = copyin(arg, &req, sizeof(req));
520 if (error)
521 break;
522
523 if (req.cmd_len > IOCDBLEN) {
524 error = EINVAL;
525 break;
526 }
527
528 if (req.iovec_count != 0) {
529 error = EOPNOTSUPP;
530 break;
531 }
532
533 ccb = cam_periph_getccb(periph, /*priority*/5);
534 csio = &ccb->csio;
535
536 error = copyin(req.cmdp, &csio->cdb_io.cdb_bytes,
537 req.cmd_len);
538 if (error) {
539 xpt_release_ccb(ccb);
540 break;
541 }
542
543 switch(req.dxfer_direction) {
544 case SG_DXFER_TO_DEV:
545 dir = CAM_DIR_OUT;
546 break;
547 case SG_DXFER_FROM_DEV:
548 dir = CAM_DIR_IN;
549 break;
550 case SG_DXFER_TO_FROM_DEV:
551 dir = CAM_DIR_IN | CAM_DIR_OUT;
552 break;
553 case SG_DXFER_NONE:
554 default:
555 dir = CAM_DIR_NONE;
556 break;
557 }
558
559 cam_fill_csio(csio,
560 /*retries*/1,
561 sgdone,
562 dir|CAM_DEV_QFRZDIS,
563 MSG_SIMPLE_Q_TAG,
564 req.dxferp,
565 req.dxfer_len,
566 req.mx_sb_len,
567 req.cmd_len,
568 req.timeout);
569
570 error = sgsendccb(periph, ccb);
571 if (error) {
572 req.host_status = DID_ERROR;
573 req.driver_status = DRIVER_INVALID;
574 xpt_release_ccb(ccb);
575 break;
576 }
577
578 req.status = csio->scsi_status;
579 req.masked_status = (csio->scsi_status >> 1) & 0x7f;
580 sg_scsiio_status(csio, &req.host_status, &req.driver_status);
581 req.resid = csio->resid;
582 req.duration = csio->ccb_h.timeout;
583 req.info = 0;
584
585 error = copyout(&req, arg, sizeof(req));
586 if ((error == 0) && (csio->ccb_h.status & CAM_AUTOSNS_VALID)
587 && (req.sbp != NULL)) {
588 req.sb_len_wr = req.mx_sb_len - csio->sense_resid;
589 error = copyout(&csio->sense_data, req.sbp,
590 req.sb_len_wr);
591 }
592
593 xpt_release_ccb(ccb);
594 break;
595
596 case SG_GET_RESERVED_SIZE:
597 case LINUX_SG_GET_RESERVED_SIZE: {
598 int size = 32768;
599
600 error = copyout(&size, arg, sizeof(size));
601 break;
602 }
603
604 case SG_GET_SCSI_ID:
605 case LINUX_SG_GET_SCSI_ID:
606 {
607 struct sg_scsi_id id;
608
609 id.host_no = 0; /* XXX */
610 id.channel = xpt_path_path_id(periph->path);
611 id.scsi_id = xpt_path_target_id(periph->path);
612 id.lun = xpt_path_lun_id(periph->path);
613 id.scsi_type = softc->pd_type;
614 id.h_cmd_per_lun = 1;
615 id.d_queue_depth = 1;
616 id.unused[0] = 0;
617 id.unused[1] = 0;
618
619 error = copyout(&id, arg, sizeof(id));
620 break;
621 }
622
623 case SG_EMULATED_HOST:
624 case SG_SET_TRANSFORM:
625 case SG_GET_TRANSFORM:
626 case SG_GET_NUM_WAITING:
627 case SG_SCSI_RESET:
628 case SG_GET_REQUEST_TABLE:
629 case SG_SET_KEEP_ORPHAN:
630 case SG_GET_KEEP_ORPHAN:
631 case SG_GET_ACCESS_COUNT:
632 case SG_SET_FORCE_LOW_DMA:
633 case SG_GET_LOW_DMA:
634 case SG_GET_SG_TABLESIZE:
635 case SG_SET_FORCE_PACK_ID:
636 case SG_GET_PACK_ID:
637 case SG_SET_RESERVED_SIZE:
638 case SG_GET_COMMAND_Q:
639 case SG_SET_COMMAND_Q:
640 case SG_SET_DEBUG:
641 case SG_NEXT_CMD_LEN:
642 case LINUX_SG_EMULATED_HOST:
643 case LINUX_SG_SET_TRANSFORM:
644 case LINUX_SG_GET_TRANSFORM:
645 case LINUX_SG_GET_NUM_WAITING:
646 case LINUX_SG_SCSI_RESET:
647 case LINUX_SG_GET_REQUEST_TABLE:
648 case LINUX_SG_SET_KEEP_ORPHAN:
649 case LINUX_SG_GET_KEEP_ORPHAN:
650 case LINUX_SG_GET_ACCESS_COUNT:
651 case LINUX_SG_SET_FORCE_LOW_DMA:
652 case LINUX_SG_GET_LOW_DMA:
653 case LINUX_SG_GET_SG_TABLESIZE:
654 case LINUX_SG_SET_FORCE_PACK_ID:
655 case LINUX_SG_GET_PACK_ID:
656 case LINUX_SG_SET_RESERVED_SIZE:
657 case LINUX_SG_GET_COMMAND_Q:
658 case LINUX_SG_SET_COMMAND_Q:
659 case LINUX_SG_SET_DEBUG:
660 case LINUX_SG_NEXT_CMD_LEN:
661 default:
662 #ifdef CAMDEBUG
663 printf("sgioctl: rejecting cmd 0x%lx\n", cmd);
664 #endif
665 error = ENODEV;
666 break;
667 }
668
669 return (error);
670 }
671
672 static int
673 sgwrite(struct cdev *dev, struct uio *uio, int ioflag)
674 {
675 union ccb *ccb;
676 struct cam_periph *periph;
677 struct ccb_scsiio *csio;
678 struct sg_softc *sc;
679 struct sg_header *hdr;
680 struct sg_rdwr *rdwr;
681 u_char cdb_cmd;
682 char *buf;
683 int error = 0, cdb_len, buf_len, dir;
684
685 periph = dev->si_drv1;
686 sc = periph->softc;
687 rdwr = malloc(sizeof(*rdwr), M_DEVBUF, M_WAITOK | M_ZERO);
688 hdr = &rdwr->hdr.hdr;
689
690 /* Copy in the header block and sanity check it */
691 if (uio->uio_resid < sizeof(*hdr)) {
692 error = EINVAL;
693 goto out_hdr;
694 }
695 error = uiomove(hdr, sizeof(*hdr), uio);
696 if (error)
697 goto out_hdr;
698
699 ccb = xpt_alloc_ccb();
700 if (ccb == NULL) {
701 error = ENOMEM;
702 goto out_hdr;
703 }
704 xpt_setup_ccb(&ccb->ccb_h, periph->path, /*priority*/5);
705 csio = &ccb->csio;
706
707 /*
708 * Copy in the CDB block. The designers of the interface didn't
709 * bother to provide a size for this in the header, so we have to
710 * figure it out ourselves.
711 */
712 if (uio->uio_resid < 1)
713 goto out_ccb;
714 error = uiomove(&cdb_cmd, 1, uio);
715 if (error)
716 goto out_ccb;
717 if (hdr->twelve_byte)
718 cdb_len = 12;
719 else
720 cdb_len = scsi_group_len(cdb_cmd);
721 /*
722 * We've already read the first byte of the CDB and advanced the uio
723 * pointer. Just read the rest.
724 */
725 csio->cdb_io.cdb_bytes[0] = cdb_cmd;
726 error = uiomove(&csio->cdb_io.cdb_bytes[1], cdb_len - 1, uio);
727 if (error)
728 goto out_ccb;
729
730 /*
731 * Now set up the data block. Again, the designers didn't bother
732 * to make this reliable.
733 */
734 buf_len = uio->uio_resid;
735 if (buf_len != 0) {
736 buf = malloc(buf_len, M_DEVBUF, M_WAITOK | M_ZERO);
737 error = uiomove(buf, buf_len, uio);
738 if (error)
739 goto out_buf;
740 dir = CAM_DIR_OUT;
741 } else if (hdr->reply_len != 0) {
742 buf = malloc(hdr->reply_len, M_DEVBUF, M_WAITOK | M_ZERO);
743 buf_len = hdr->reply_len;
744 dir = CAM_DIR_IN;
745 } else {
746 buf = NULL;
747 buf_len = 0;
748 dir = CAM_DIR_NONE;
749 }
750
751 cam_fill_csio(csio,
752 /*retries*/1,
753 sgdone,
754 dir|CAM_DEV_QFRZDIS,
755 MSG_SIMPLE_Q_TAG,
756 buf,
757 buf_len,
758 SG_MAX_SENSE,
759 cdb_len,
760 sc->sg_timeout);
761
762 /*
763 * Send off the command and hope that it works. This path does not
764 * go through sgstart because the I/O is supposed to be asynchronous.
765 */
766 rdwr->buf = buf;
767 rdwr->buf_len = buf_len;
768 rdwr->tag = hdr->pack_id;
769 rdwr->ccb = ccb;
770 rdwr->state = SG_RDWR_INPROG;
771 ccb->ccb_h.ccb_rdwr = rdwr;
772 ccb->ccb_h.ccb_type = SG_CCB_RDWR_IO;
773 TAILQ_INSERT_TAIL(&sc->rdwr_done, rdwr, rdwr_link);
774 return (sgsendrdwr(periph, ccb));
775
776 out_buf:
777 free(buf, M_DEVBUF);
778 out_ccb:
779 xpt_free_ccb(ccb);
780 out_hdr:
781 free(rdwr, M_DEVBUF);
782 return (error);
783 }
784
785 static int
786 sgread(struct cdev *dev, struct uio *uio, int ioflag)
787 {
788 struct ccb_scsiio *csio;
789 struct cam_periph *periph;
790 struct sg_softc *sc;
791 struct sg_header *hdr;
792 struct sg_rdwr *rdwr;
793 u_short hstat, dstat;
794 int error, pack_len, reply_len, pack_id;
795
796 periph = dev->si_drv1;
797 sc = periph->softc;
798
799 /* XXX The pack len field needs to be updated and written out instead
800 * of discarded. Not sure how to do that.
801 */
802 uio->uio_rw = UIO_WRITE;
803 if ((error = uiomove(&pack_len, 4, uio)) != 0)
804 return (error);
805 if ((error = uiomove(&reply_len, 4, uio)) != 0)
806 return (error);
807 if ((error = uiomove(&pack_id, 4, uio)) != 0)
808 return (error);
809 uio->uio_rw = UIO_READ;
810
811 search:
812 TAILQ_FOREACH(rdwr, &sc->rdwr_done, rdwr_link) {
813 if (rdwr->tag == pack_id)
814 break;
815 }
816 if ((rdwr == NULL) || (rdwr->state != SG_RDWR_DONE)) {
817 if (tsleep(rdwr, PCATCH, "sgread", 0) == ERESTART)
818 return (EAGAIN);
819 goto search;
820 }
821 TAILQ_REMOVE(&sc->rdwr_done, rdwr, rdwr_link);
822
823 hdr = &rdwr->hdr.hdr;
824 csio = &rdwr->ccb->csio;
825 sg_scsiio_status(csio, &hstat, &dstat);
826 hdr->host_status = hstat;
827 hdr->driver_status = dstat;
828 hdr->target_status = csio->scsi_status >> 1;
829
830 switch (hstat) {
831 case DID_OK:
832 case DID_PASSTHROUGH:
833 case DID_SOFT_ERROR:
834 hdr->result = 0;
835 break;
836 case DID_NO_CONNECT:
837 case DID_BUS_BUSY:
838 case DID_TIME_OUT:
839 hdr->result = EBUSY;
840 break;
841 case DID_BAD_TARGET:
842 case DID_ABORT:
843 case DID_PARITY:
844 case DID_RESET:
845 case DID_BAD_INTR:
846 case DID_ERROR:
847 default:
848 hdr->result = EIO;
849 break;
850 }
851
852 if (dstat == DRIVER_SENSE) {
853 bcopy(&csio->sense_data, hdr->sense_buffer,
854 min(csio->sense_len, SG_MAX_SENSE));
855 #ifdef CAMDEBUG
856 scsi_sense_print(csio);
857 #endif
858 }
859
860 error = uiomove(&hdr->result, sizeof(*hdr) -
861 offsetof(struct sg_header, result), uio);
862 if ((error == 0) && (hdr->result == 0))
863 error = uiomove(rdwr->buf, rdwr->buf_len, uio);
864
865 xpt_free_ccb(rdwr->ccb);
866 free(rdwr->buf, M_DEVBUF);
867 free(rdwr, M_DEVBUF);
868 return (error);
869 }
870
871 static int
872 sgsendccb(struct cam_periph *periph, union ccb *ccb)
873 {
874 struct sg_softc *softc;
875 struct cam_periph_map_info mapinfo;
876 int error, need_unmap = 0;
877
878 softc = periph->softc;
879 if (((ccb->ccb_h.flags & CAM_DIR_MASK) != CAM_DIR_NONE)
880 && (ccb->csio.data_ptr != NULL)) {
881 bzero(&mapinfo, sizeof(mapinfo));
882 error = cam_periph_mapmem(ccb, &mapinfo);
883 if (error)
884 return (error);
885 need_unmap = 1;
886 }
887
888 error = cam_periph_runccb(ccb,
889 sgerror,
890 CAM_RETRY_SELTO,
891 SF_RETRY_UA,
892 softc->device_stats);
893
894 if (need_unmap)
895 cam_periph_unmapmem(ccb, &mapinfo);
896
897 return (error);
898 }
899
900 static int
901 sgsendrdwr(struct cam_periph *periph, union ccb *ccb)
902 {
903 struct sg_softc *softc;
904
905 softc = periph->softc;
906 devstat_start_transaction(softc->device_stats, NULL);
907 xpt_action(ccb);
908 return (0);
909 }
910
911 static int
912 sgerror(union ccb *ccb, uint32_t cam_flags, uint32_t sense_flags)
913 {
914 struct cam_periph *periph;
915 struct sg_softc *softc;
916
917 periph = xpt_path_periph(ccb->ccb_h.path);
918 softc = (struct sg_softc *)periph->softc;
919
920 return (cam_periph_error(ccb, cam_flags, sense_flags,
921 &softc->saved_ccb));
922 }
923
924 static void
925 sg_scsiio_status(struct ccb_scsiio *csio, u_short *hoststat, u_short *drvstat)
926 {
927 int status;
928
929 status = csio->ccb_h.status;
930
931 switch (status & CAM_STATUS_MASK) {
932 case CAM_REQ_CMP:
933 *hoststat = DID_OK;
934 *drvstat = 0;
935 break;
936 case CAM_REQ_CMP_ERR:
937 *hoststat = DID_ERROR;
938 *drvstat = 0;
939 break;
940 case CAM_REQ_ABORTED:
941 *hoststat = DID_ABORT;
942 *drvstat = 0;
943 break;
944 case CAM_REQ_INVALID:
945 *hoststat = DID_ERROR;
946 *drvstat = DRIVER_INVALID;
947 break;
948 case CAM_DEV_NOT_THERE:
949 *hoststat = DID_BAD_TARGET;
950 *drvstat = 0;
951 case CAM_SEL_TIMEOUT:
952 *hoststat = DID_NO_CONNECT;
953 *drvstat = 0;
954 break;
955 case CAM_CMD_TIMEOUT:
956 *hoststat = DID_TIME_OUT;
957 *drvstat = 0;
958 break;
959 case CAM_SCSI_STATUS_ERROR:
960 *hoststat = DID_ERROR;
961 *drvstat = 0;
962 case CAM_SCSI_BUS_RESET:
963 *hoststat = DID_RESET;
964 *drvstat = 0;
965 break;
966 case CAM_UNCOR_PARITY:
967 *hoststat = DID_PARITY;
968 *drvstat = 0;
969 break;
970 case CAM_SCSI_BUSY:
971 *hoststat = DID_BUS_BUSY;
972 *drvstat = 0;
973 default:
974 *hoststat = DID_ERROR;
975 *drvstat = DRIVER_ERROR;
976 }
977
978 if (status & CAM_AUTOSNS_VALID)
979 *drvstat = DRIVER_SENSE;
980 }
981
982 static int
983 scsi_group_len(u_char cmd)
984 {
985 int len[] = {6, 10, 10, 12, 12, 12, 10, 10};
986 int group;
987
988 group = (cmd >> 5) & 0x7;
989 return (len[group]);
990 }
991
Cache object: c6c130bbb5e6b44ce653a3a78c274631
|