2 * Copyright (c) 1997, 1998, 2000 Justin T. Gibbs.
3 * Copyright (c) 1997, 1998, 1999 Kenneth D. Merry.
6 * Redistribution and use in source and binary forms, with or without
7 * modification, are permitted provided that the following conditions
9 * 1. Redistributions of source code must retain the above copyright
10 * notice, this list of conditions, and the following disclaimer,
11 * without modification, immediately at the beginning of the file.
12 * 2. The name of the author may not be used to endorse or promote products
13 * derived from this software without specific prior written permission.
15 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
16 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
17 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
18 * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE FOR
19 * ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
20 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
21 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
22 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
23 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
24 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
28 #include <sys/cdefs.h>
29 __FBSDID("$FreeBSD$");
31 #include <sys/param.h>
32 #include <sys/systm.h>
33 #include <sys/kernel.h>
34 #include <sys/types.h>
36 #include <sys/malloc.h>
37 #include <sys/fcntl.h>
39 #include <sys/errno.h>
40 #include <sys/devicestat.h>
44 #include <cam/cam_ccb.h>
45 #include <cam/cam_periph.h>
46 #include <cam/cam_queue.h>
47 #include <cam/cam_xpt_periph.h>
48 #include <cam/cam_debug.h>
50 #include <cam/scsi/scsi_all.h>
51 #include <cam/scsi/scsi_pass.h>
54 PASS_FLAG_OPEN = 0x01,
55 PASS_FLAG_LOCKED = 0x02,
56 PASS_FLAG_INVALID = 0x04
68 #define ccb_type ppriv_field0
69 #define ccb_bp ppriv_ptr1
76 struct devstat *device_stats;
81 static d_open_t passopen;
82 static d_close_t passclose;
83 static d_ioctl_t passioctl;
85 static periph_init_t passinit;
86 static periph_ctor_t passregister;
87 static periph_oninv_t passoninvalidate;
88 static periph_dtor_t passcleanup;
89 static periph_start_t passstart;
90 static void passasync(void *callback_arg, u_int32_t code,
91 struct cam_path *path, void *arg);
92 static void passdone(struct cam_periph *periph,
94 static int passerror(union ccb *ccb, u_int32_t cam_flags,
95 u_int32_t sense_flags);
96 static int passsendccb(struct cam_periph *periph, union ccb *ccb,
99 static struct periph_driver passdriver =
102 TAILQ_HEAD_INITIALIZER(passdriver.units), /* generation */ 0
105 PERIPHDRIVER_DECLARE(pass, passdriver);
107 static struct cdevsw pass_cdevsw = {
108 .d_version = D_VERSION,
109 .d_flags = D_NEEDGIANT,
111 .d_close = passclose,
112 .d_ioctl = passioctl,
120 struct cam_path *path;
123 * Install a global async callback. This callback will
124 * receive async callbacks like "new device found".
126 status = xpt_create_path(&path, /*periph*/NULL, CAM_XPT_PATH_ID,
127 CAM_TARGET_WILDCARD, CAM_LUN_WILDCARD);
129 if (status == CAM_REQ_CMP) {
130 struct ccb_setasync csa;
132 xpt_setup_ccb(&csa.ccb_h, path, /*priority*/5);
133 csa.ccb_h.func_code = XPT_SASYNC_CB;
134 csa.event_enable = AC_FOUND_DEVICE;
135 csa.callback = passasync;
136 csa.callback_arg = NULL;
137 xpt_action((union ccb *)&csa);
138 status = csa.ccb_h.status;
142 if (status != CAM_REQ_CMP) {
143 printf("pass: Failed to attach master async callback "
144 "due to status 0x%x!\n", status);
150 passoninvalidate(struct cam_periph *periph)
152 struct pass_softc *softc;
153 struct ccb_setasync csa;
155 softc = (struct pass_softc *)periph->softc;
158 * De-register any async callbacks.
160 xpt_setup_ccb(&csa.ccb_h, periph->path,
162 csa.ccb_h.func_code = XPT_SASYNC_CB;
163 csa.event_enable = 0;
164 csa.callback = passasync;
165 csa.callback_arg = periph;
166 xpt_action((union ccb *)&csa);
168 softc->flags |= PASS_FLAG_INVALID;
171 * XXX Return all queued I/O with ENXIO.
172 * XXX Handle any transactions queued to the card
173 * with XPT_ABORT_CCB.
177 xpt_print_path(periph->path);
178 printf("lost device\n");
184 passcleanup(struct cam_periph *periph)
186 struct pass_softc *softc;
188 softc = (struct pass_softc *)periph->softc;
190 devstat_remove_entry(softc->device_stats);
192 destroy_dev(softc->dev);
195 xpt_print_path(periph->path);
196 printf("removing device entry\n");
198 free(softc, M_DEVBUF);
202 passasync(void *callback_arg, u_int32_t code,
203 struct cam_path *path, void *arg)
205 struct cam_periph *periph;
207 periph = (struct cam_periph *)callback_arg;
210 case AC_FOUND_DEVICE:
212 struct ccb_getdev *cgd;
215 cgd = (struct ccb_getdev *)arg;
220 * Allocate a peripheral instance for
221 * this device and start the probe
224 status = cam_periph_alloc(passregister, passoninvalidate,
225 passcleanup, passstart, "pass",
226 CAM_PERIPH_BIO, cgd->ccb_h.path,
227 passasync, AC_FOUND_DEVICE, cgd);
229 if (status != CAM_REQ_CMP
230 && status != CAM_REQ_INPROG) {
231 const struct cam_status_entry *entry;
233 entry = cam_fetch_status_entry(status);
235 printf("passasync: Unable to attach new device "
236 "due to status %#x: %s\n", status, entry ?
237 entry->status_text : "Unknown");
243 cam_periph_async(periph, code, path, arg);
249 passregister(struct cam_periph *periph, void *arg)
251 struct pass_softc *softc;
252 struct ccb_setasync csa;
253 struct ccb_getdev *cgd;
256 cgd = (struct ccb_getdev *)arg;
257 if (periph == NULL) {
258 printf("passregister: periph was NULL!!\n");
259 return(CAM_REQ_CMP_ERR);
263 printf("passregister: no getdev CCB, can't register device\n");
264 return(CAM_REQ_CMP_ERR);
267 softc = (struct pass_softc *)malloc(sizeof(*softc),
271 printf("passregister: Unable to probe new device. "
272 "Unable to allocate softc\n");
273 return(CAM_REQ_CMP_ERR);
276 bzero(softc, sizeof(*softc));
277 softc->state = PASS_STATE_NORMAL;
278 softc->pd_type = SID_TYPE(&cgd->inq_data);
280 periph->softc = softc;
283 * We pass in 0 for a blocksize, since we don't
284 * know what the blocksize of this device is, if
285 * it even has a blocksize.
287 no_tags = (cgd->inq_data.flags & SID_CmdQue) == 0;
288 softc->device_stats = devstat_new_entry("pass",
289 unit2minor(periph->unit_number), 0,
291 | (no_tags ? DEVSTAT_NO_ORDERED_TAGS : 0),
293 DEVSTAT_TYPE_IF_SCSI |
295 DEVSTAT_PRIORITY_PASS);
297 /* Register the device */
298 softc->dev = make_dev(&pass_cdevsw, unit2minor(periph->unit_number),
299 UID_ROOT, GID_OPERATOR, 0600, "%s%d",
300 periph->periph_name, periph->unit_number);
301 softc->dev->si_drv1 = periph;
304 * Add an async callback so that we get
305 * notified if this device goes away.
307 xpt_setup_ccb(&csa.ccb_h, periph->path, /* priority */ 5);
308 csa.ccb_h.func_code = XPT_SASYNC_CB;
309 csa.event_enable = AC_LOST_DEVICE;
310 csa.callback = passasync;
311 csa.callback_arg = periph;
312 xpt_action((union ccb *)&csa);
315 xpt_announce_periph(periph, NULL);
321 passopen(struct cdev *dev, int flags, int fmt, struct thread *td)
323 struct cam_periph *periph;
324 struct pass_softc *softc;
328 error = 0; /* default to no error */
330 periph = (struct cam_periph *)dev->si_drv1;
334 softc = (struct pass_softc *)periph->softc;
337 if (softc->flags & PASS_FLAG_INVALID) {
343 * Don't allow access when we're running at a high securelevel.
345 error = securelevel_gt(td->td_ucred, 1);
352 * Only allow read-write access.
354 if (((flags & FWRITE) == 0) || ((flags & FREAD) == 0)) {
360 * We don't allow nonblocking access.
362 if ((flags & O_NONBLOCK) != 0) {
363 xpt_print_path(periph->path);
364 printf("can't do nonblocking accesss\n");
369 if ((error = cam_periph_lock(periph, PRIBIO | PCATCH)) != 0) {
376 if ((softc->flags & PASS_FLAG_OPEN) == 0) {
377 if (cam_periph_acquire(periph) != CAM_REQ_CMP)
379 softc->flags |= PASS_FLAG_OPEN;
382 cam_periph_unlock(periph);
388 passclose(struct cdev *dev, int flag, int fmt, struct thread *td)
390 struct cam_periph *periph;
391 struct pass_softc *softc;
394 periph = (struct cam_periph *)dev->si_drv1;
398 softc = (struct pass_softc *)periph->softc;
400 if ((error = cam_periph_lock(periph, PRIBIO)) != 0)
403 softc->flags &= ~PASS_FLAG_OPEN;
405 cam_periph_unlock(periph);
406 cam_periph_release(periph);
412 passstart(struct cam_periph *periph, union ccb *start_ccb)
414 struct pass_softc *softc;
417 softc = (struct pass_softc *)periph->softc;
419 switch (softc->state) {
420 case PASS_STATE_NORMAL:
422 start_ccb->ccb_h.ccb_type = PASS_CCB_WAITING;
423 SLIST_INSERT_HEAD(&periph->ccb_list, &start_ccb->ccb_h,
425 periph->immediate_priority = CAM_PRIORITY_NONE;
427 wakeup(&periph->ccb_list);
433 passdone(struct cam_periph *periph, union ccb *done_ccb)
435 struct pass_softc *softc;
436 struct ccb_scsiio *csio;
438 softc = (struct pass_softc *)periph->softc;
439 csio = &done_ccb->csio;
440 switch (csio->ccb_h.ccb_type) {
441 case PASS_CCB_WAITING:
442 /* Caller will release the CCB */
443 wakeup(&done_ccb->ccb_h.cbfcnp);
446 xpt_release_ccb(done_ccb);
450 passioctl(struct cdev *dev, u_long cmd, caddr_t addr, int flag, struct thread *td)
452 struct cam_periph *periph;
453 struct pass_softc *softc;
456 periph = (struct cam_periph *)dev->si_drv1;
460 softc = (struct pass_softc *)periph->softc;
472 inccb = (union ccb *)addr;
475 * Some CCB types, like scan bus and scan lun can only go
476 * through the transport layer device.
478 if (inccb->ccb_h.func_code & XPT_FC_XPT_ONLY) {
479 xpt_print_path(periph->path);
480 printf("CCB function code %#x is restricted to the "
481 "XPT device\n", inccb->ccb_h.func_code);
487 * Non-immediate CCBs need a CCB from the per-device pool
488 * of CCBs, which is scheduled by the transport layer.
489 * Immediate CCBs and user-supplied CCBs should just be
492 if ((inccb->ccb_h.func_code & XPT_FC_QUEUED)
493 && ((inccb->ccb_h.func_code & XPT_FC_USER_CCB) == 0)) {
494 ccb = cam_periph_getccb(periph,
495 inccb->ccb_h.pinfo.priority);
498 ccb = xpt_alloc_ccb();
501 xpt_setup_ccb(&ccb->ccb_h, periph->path,
502 inccb->ccb_h.pinfo.priority);
507 xpt_print_path(periph->path);
508 printf("unable to allocate CCB\n");
513 error = passsendccb(periph, ccb, inccb);
518 xpt_release_ccb(ccb);
523 error = cam_periph_ioctl(periph, cmd, addr, passerror);
531 * Generally, "ccb" should be the CCB supplied by the kernel. "inccb"
532 * should be the CCB that is copied in from the user.
535 passsendccb(struct cam_periph *periph, union ccb *ccb, union ccb *inccb)
537 struct pass_softc *softc;
538 struct cam_periph_map_info mapinfo;
539 int error, need_unmap;
541 softc = (struct pass_softc *)periph->softc;
546 * There are some fields in the CCB header that need to be
547 * preserved, the rest we get from the user.
549 xpt_merge_ccb(ccb, inccb);
552 * There's no way for the user to have a completion
553 * function, so we put our own completion function in here.
555 ccb->ccb_h.cbfcnp = passdone;
558 * We only attempt to map the user memory into kernel space
559 * if they haven't passed in a physical memory pointer,
560 * and if there is actually an I/O operation to perform.
561 * Right now cam_periph_mapmem() only supports SCSI and device
562 * match CCBs. For the SCSI CCBs, we only pass the CCB in if
563 * there's actually data to map. cam_periph_mapmem() will do the
564 * right thing, even if there isn't data to map, but since CCBs
565 * without data are a reasonably common occurance (e.g. test unit
566 * ready), it will save a few cycles if we check for it here.
568 if (((ccb->ccb_h.flags & CAM_DATA_PHYS) == 0)
569 && (((ccb->ccb_h.func_code == XPT_SCSI_IO)
570 && ((ccb->ccb_h.flags & CAM_DIR_MASK) != CAM_DIR_NONE))
571 || (ccb->ccb_h.func_code == XPT_DEV_MATCH))) {
573 bzero(&mapinfo, sizeof(mapinfo));
575 error = cam_periph_mapmem(ccb, &mapinfo);
578 * cam_periph_mapmem returned an error, we can't continue.
579 * Return the error to the user.
585 * We successfully mapped the memory in, so we need to
586 * unmap it when the transaction is done.
592 * If the user wants us to perform any error recovery, then honor
593 * that request. Otherwise, it's up to the user to perform any
596 error = cam_periph_runccb(ccb,
597 (ccb->ccb_h.flags & CAM_PASS_ERR_RECOVER) ?
599 /* cam_flags */ CAM_RETRY_SELTO,
600 /* sense_flags */SF_RETRY_UA,
601 softc->device_stats);
604 cam_periph_unmapmem(ccb, &mapinfo);
606 ccb->ccb_h.cbfcnp = NULL;
607 ccb->ccb_h.periph_priv = inccb->ccb_h.periph_priv;
608 bcopy(ccb, inccb, sizeof(union ccb));
614 passerror(union ccb *ccb, u_int32_t cam_flags, u_int32_t sense_flags)
616 struct cam_periph *periph;
617 struct pass_softc *softc;
619 periph = xpt_path_periph(ccb->ccb_h.path);
620 softc = (struct pass_softc *)periph->softc;
622 return(cam_periph_error(ccb, cam_flags, sense_flags,