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
27 * $FreeBSD: src/sys/cam/scsi/scsi_pass.c,v 1.19 2000/01/17 06:27:37 mjacob Exp $
28 * $DragonFly: src/sys/bus/cam/scsi/scsi_pass.c,v 1.27 2008/05/18 20:30:20 pavalos Exp $
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>
40 #include <sys/errno.h>
41 #include <sys/devicestat.h>
43 #include <sys/thread2.h>
46 #include "../cam_ccb.h"
47 #include "../cam_extend.h"
48 #include "../cam_periph.h"
49 #include "../cam_queue.h"
50 #include "../cam_xpt_periph.h"
51 #include "../cam_debug.h"
52 #include "../cam_sim.h"
55 #include "scsi_message.h"
57 #include "scsi_pass.h"
60 PASS_FLAG_OPEN = 0x01,
61 PASS_FLAG_LOCKED = 0x02,
62 PASS_FLAG_INVALID = 0x04
74 #define ccb_type ppriv_field0
75 #define ccb_bio ppriv_ptr1
82 struct devstat device_stats;
85 #define PASS_CDEV_MAJOR 31
87 static d_open_t passopen;
88 static d_close_t passclose;
89 static d_ioctl_t passioctl;
91 static periph_init_t passinit;
92 static periph_ctor_t passregister;
93 static periph_oninv_t passoninvalidate;
94 static periph_dtor_t passcleanup;
95 static periph_start_t passstart;
96 static void passasync(void *callback_arg, u_int32_t code,
97 struct cam_path *path, void *arg);
98 static void passdone(struct cam_periph *periph,
100 static int passerror(union ccb *ccb, u_int32_t cam_flags,
101 u_int32_t sense_flags);
102 static int passsendccb(struct cam_periph *periph, union ccb *ccb,
105 static struct periph_driver passdriver =
108 TAILQ_HEAD_INITIALIZER(passdriver.units), /* generation */ 0
111 PERIPHDRIVER_DECLARE(pass, passdriver);
113 static struct dev_ops pass_ops = {
114 { "pass", PASS_CDEV_MAJOR, 0 },
116 .d_close = passclose,
119 .d_ioctl = passioctl,
120 .d_strategy = nostrategy,
123 static struct extend_array *passperiphs;
131 * Create our extend array for storing the devices we attach to.
133 passperiphs = cam_extend_new();
134 if (passperiphs == NULL) {
135 kprintf("passm: Failed to alloc extend array!\n");
140 * Install a global async callback. This callback will
141 * receive async callbacks like "new device found".
143 status = xpt_register_async(AC_FOUND_DEVICE, passasync, NULL, NULL);
145 if (status != CAM_REQ_CMP) {
146 kprintf("pass: Failed to attach master async callback "
147 "due to status 0x%x!\n", status);
153 passoninvalidate(struct cam_periph *periph)
155 struct pass_softc *softc;
157 softc = (struct pass_softc *)periph->softc;
160 * De-register any async callbacks.
162 xpt_register_async(0, passasync, periph, periph->path);
164 softc->flags |= PASS_FLAG_INVALID;
167 xpt_print(periph->path, "lost device\n");
173 passcleanup(struct cam_periph *periph)
175 struct pass_softc *softc;
177 softc = (struct pass_softc *)periph->softc;
179 devstat_remove_entry(&softc->device_stats);
181 cam_extend_release(passperiphs, periph->unit_number);
184 xpt_print(periph->path, "removing device entry\n");
186 dev_ops_remove(&pass_ops, -1, periph->unit_number);
187 kfree(softc, M_DEVBUF);
191 passasync(void *callback_arg, u_int32_t code,
192 struct cam_path *path, void *arg)
194 struct cam_periph *periph;
197 periph = (struct cam_periph *)callback_arg;
200 case AC_FOUND_DEVICE:
202 struct ccb_getdev *cgd;
205 cgd = (struct ccb_getdev *)arg;
210 * Don't complain if a valid peripheral is already attached.
212 periph = cam_periph_find(cgd->ccb_h.path, "pass");
213 if (periph && (periph->flags & CAM_PERIPH_INVALID) == 0)
217 * Allocate a peripheral instance for
218 * this device and start the probe
221 sim = xpt_path_sim(cgd->ccb_h.path);
222 status = cam_periph_alloc(passregister, passoninvalidate,
223 passcleanup, passstart, "pass",
224 CAM_PERIPH_BIO, cgd->ccb_h.path,
225 passasync, AC_FOUND_DEVICE, cgd);
227 if (status != CAM_REQ_CMP && status != CAM_REQ_INPROG) {
228 const struct cam_status_entry *entry;
230 entry = cam_fetch_status_entry(status);
232 kprintf("passasync: Unable to attach new device "
233 "due to status %#x: %s\n", status, entry ?
234 entry->status_text : "Unknown");
240 cam_periph_async(periph, code, path, arg);
246 passregister(struct cam_periph *periph, void *arg)
248 struct pass_softc *softc;
249 struct ccb_getdev *cgd;
252 cgd = (struct ccb_getdev *)arg;
253 if (periph == NULL) {
254 kprintf("passregister: periph was NULL!!\n");
255 return(CAM_REQ_CMP_ERR);
259 kprintf("passregister: no getdev CCB, can't register device\n");
260 return(CAM_REQ_CMP_ERR);
263 softc = kmalloc(sizeof(*softc), M_DEVBUF, M_INTWAIT | M_ZERO);
264 softc->state = PASS_STATE_NORMAL;
265 softc->pd_type = SID_TYPE(&cgd->inq_data);
267 periph->softc = softc;
268 cam_extend_set(passperiphs, periph->unit_number, periph);
271 * We pass in 0 for a blocksize, since we don't
272 * know what the blocksize of this device is, if
273 * it even has a blocksize.
275 CAM_SIM_UNLOCK(periph->sim);
276 no_tags = (cgd->inq_data.flags & SID_CmdQue) == 0;
277 devstat_add_entry(&softc->device_stats, "pass", periph->unit_number, 0,
279 | (no_tags ? DEVSTAT_NO_ORDERED_TAGS : 0),
281 DEVSTAT_TYPE_IF_SCSI |
283 DEVSTAT_PRIORITY_PASS);
285 /* Register the device */
286 dev_ops_add(&pass_ops, -1, periph->unit_number);
287 make_dev(&pass_ops, periph->unit_number, UID_ROOT,
288 GID_OPERATOR, 0600, "%s%d", periph->periph_name,
289 periph->unit_number);
290 CAM_SIM_LOCK(periph->sim);
292 * Add an async callback so that we get
293 * notified if this device goes away.
295 xpt_register_async(AC_LOST_DEVICE, passasync, periph, periph->path);
298 xpt_announce_periph(periph, NULL);
304 passopen(struct dev_open_args *ap)
306 cdev_t dev = ap->a_head.a_dev;
307 struct cam_periph *periph;
308 struct pass_softc *softc;
311 error = 0; /* default to no error */
313 /* unit = dkunit(dev); */
314 /* XXX KDM fix this */
315 unit = minor(dev) & 0xff;
317 periph = cam_extend_get(passperiphs, unit);
319 if (cam_periph_acquire(periph) != CAM_REQ_CMP)
322 cam_periph_lock(periph);
324 softc = (struct pass_softc *)periph->softc;
326 if (softc->flags & PASS_FLAG_INVALID) {
327 cam_periph_unlock(periph);
328 cam_periph_release(periph);
333 * Don't allow access when we're running at a high securelevel.
335 if (securelevel > 1) {
336 cam_periph_unlock(periph);
337 cam_periph_release(periph);
342 * Only allow read-write access.
344 if (((ap->a_oflags & FWRITE) == 0) || ((ap->a_oflags & FREAD) == 0)) {
345 cam_periph_unlock(periph);
346 cam_periph_release(periph);
351 * We don't allow nonblocking access.
353 if ((ap->a_oflags & O_NONBLOCK) != 0) {
354 xpt_print(periph->path, "can't do nonblocking access\n");
355 cam_periph_unlock(periph);
356 cam_periph_release(periph);
360 if ((softc->flags & PASS_FLAG_OPEN) == 0) {
361 softc->flags |= PASS_FLAG_OPEN;
363 /* Device closes aren't symmertical, so fix up the refcount */
364 cam_periph_release(periph);
367 cam_periph_unlock(periph);
373 passclose(struct dev_close_args *ap)
375 cdev_t dev = ap->a_head.a_dev;
376 struct cam_periph *periph;
377 struct pass_softc *softc;
380 /* unit = dkunit(dev); */
381 /* XXX KDM fix this */
382 unit = minor(dev) & 0xff;
384 periph = cam_extend_get(passperiphs, unit);
388 cam_periph_lock(periph);
390 softc = (struct pass_softc *)periph->softc;
391 softc->flags &= ~PASS_FLAG_OPEN;
393 cam_periph_unlock(periph);
394 cam_periph_release(periph);
400 passstart(struct cam_periph *periph, union ccb *start_ccb)
402 struct pass_softc *softc;
404 softc = (struct pass_softc *)periph->softc;
406 switch (softc->state) {
407 case PASS_STATE_NORMAL:
408 start_ccb->ccb_h.ccb_type = PASS_CCB_WAITING;
409 SLIST_INSERT_HEAD(&periph->ccb_list, &start_ccb->ccb_h,
411 periph->immediate_priority = CAM_PRIORITY_NONE;
412 wakeup(&periph->ccb_list);
418 passdone(struct cam_periph *periph, union ccb *done_ccb)
420 struct pass_softc *softc;
421 struct ccb_scsiio *csio;
423 softc = (struct pass_softc *)periph->softc;
424 csio = &done_ccb->csio;
425 switch (csio->ccb_h.ccb_type) {
426 case PASS_CCB_WAITING:
427 /* Caller will release the CCB */
428 wakeup(&done_ccb->ccb_h.cbfcnp);
431 xpt_release_ccb(done_ccb);
435 passioctl(struct dev_ioctl_args *ap)
437 cdev_t dev = ap->a_head.a_dev;
438 caddr_t addr = ap->a_data;
439 struct cam_periph *periph;
440 struct pass_softc *softc;
445 /* unit = dkunit(dev); */
446 /* XXX KDM fix this */
447 unit = minor(dev) & 0xff;
449 periph = cam_extend_get(passperiphs, unit);
454 cam_periph_lock(periph);
455 softc = (struct pass_softc *)periph->softc;
467 inccb = (union ccb *)addr;
470 * Some CCB types, like scan bus and scan lun can only go
471 * through the transport layer device.
473 if (inccb->ccb_h.func_code & XPT_FC_XPT_ONLY) {
474 xpt_print(periph->path, "CCB function code %#x is "
475 "restricted to the XPT device\n",
476 inccb->ccb_h.func_code);
482 * Non-immediate CCBs need a CCB from the per-device pool
483 * of CCBs, which is scheduled by the transport layer.
484 * Immediate CCBs and user-supplied CCBs should just be
487 if ((inccb->ccb_h.func_code & XPT_FC_QUEUED)
488 && ((inccb->ccb_h.func_code & XPT_FC_USER_CCB) == 0)) {
489 ccb = cam_periph_getccb(periph,
490 inccb->ccb_h.pinfo.priority);
493 ccb = xpt_alloc_ccb();
496 xpt_setup_ccb(&ccb->ccb_h, periph->path,
497 inccb->ccb_h.pinfo.priority);
502 xpt_print(periph->path, "unable to allocate CCB\n");
507 error = passsendccb(periph, ccb, inccb);
512 xpt_release_ccb(ccb);
517 error = cam_periph_ioctl(periph, ap->a_cmd, addr, passerror);
521 cam_periph_unlock(periph);
526 * Generally, "ccb" should be the CCB supplied by the kernel. "inccb"
527 * should be the CCB that is copied in from the user.
530 passsendccb(struct cam_periph *periph, union ccb *ccb, union ccb *inccb)
532 struct pass_softc *softc;
533 struct cam_periph_map_info mapinfo;
534 int error, need_unmap;
536 softc = (struct pass_softc *)periph->softc;
541 * There are some fields in the CCB header that need to be
542 * preserved, the rest we get from the user.
544 xpt_merge_ccb(ccb, inccb);
547 * There's no way for the user to have a completion
548 * function, so we put our own completion function in here.
550 ccb->ccb_h.cbfcnp = passdone;
553 * We only attempt to map the user memory into kernel space
554 * if they haven't passed in a physical memory pointer,
555 * and if there is actually an I/O operation to perform.
556 * Right now cam_periph_mapmem() only supports SCSI and device
557 * match CCBs. For the SCSI CCBs, we only pass the CCB in if
558 * there's actually data to map. cam_periph_mapmem() will do the
559 * right thing, even if there isn't data to map, but since CCBs
560 * without data are a reasonably common occurance (e.g. test unit
561 * ready), it will save a few cycles if we check for it here.
563 if (((ccb->ccb_h.flags & CAM_DATA_PHYS) == 0)
564 && (((ccb->ccb_h.func_code == XPT_SCSI_IO)
565 && ((ccb->ccb_h.flags & CAM_DIR_MASK) != CAM_DIR_NONE))
566 || (ccb->ccb_h.func_code == XPT_DEV_MATCH))) {
568 bzero(&mapinfo, sizeof(mapinfo));
571 * cam_periph_mapmem calls into proc and vm functions that can
572 * sleep as well as trigger I/O, so we can't hold the lock.
573 * Dropping it here is reasonably safe.
575 cam_periph_unlock(periph);
576 error = cam_periph_mapmem(ccb, &mapinfo);
577 cam_periph_lock(periph);
580 * cam_periph_mapmem returned an error, we can't continue.
581 * Return the error to the user.
587 * We successfully mapped the memory in, so we need to
588 * unmap it when the transaction is done.
594 * If the user wants us to perform any error recovery, then honor
595 * that request. Otherwise, it's up to the user to perform any
598 error = cam_periph_runccb(ccb,
599 (ccb->ccb_h.flags & CAM_PASS_ERR_RECOVER) ?
601 /* cam_flags */ CAM_RETRY_SELTO,
602 /* sense_flags */SF_RETRY_UA,
603 &softc->device_stats);
606 cam_periph_unmapmem(ccb, &mapinfo);
608 ccb->ccb_h.cbfcnp = NULL;
609 ccb->ccb_h.periph_priv = inccb->ccb_h.periph_priv;
610 bcopy(ccb, inccb, sizeof(union ccb));
616 passerror(union ccb *ccb, u_int32_t cam_flags, u_int32_t sense_flags)
618 struct cam_periph *periph;
619 struct pass_softc *softc;
621 periph = xpt_path_periph(ccb->ccb_h.path);
622 softc = (struct pass_softc *)periph->softc;
624 return(cam_periph_error(ccb, cam_flags, sense_flags,