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 $
30 #include <sys/param.h>
31 #include <sys/systm.h>
32 #include <sys/kernel.h>
33 #include <sys/types.h>
35 #include <sys/malloc.h>
36 #include <sys/fcntl.h>
39 #include <sys/errno.h>
40 #include <sys/devicestat.h>
42 #include <sys/thread2.h>
45 #include "../cam_ccb.h"
46 #include "../cam_extend.h"
47 #include "../cam_periph.h"
48 #include "../cam_queue.h"
49 #include "../cam_xpt_periph.h"
50 #include "../cam_debug.h"
51 #include "../cam_sim.h"
54 #include "scsi_message.h"
56 #include "scsi_pass.h"
59 PASS_FLAG_OPEN = 0x01,
60 PASS_FLAG_LOCKED = 0x02,
61 PASS_FLAG_INVALID = 0x04
73 #define ccb_type ppriv_field0
74 #define ccb_bio ppriv_ptr1
81 struct devstat device_stats;
84 static d_open_t passopen;
85 static d_close_t passclose;
86 static d_ioctl_t passioctl;
88 static periph_init_t passinit;
89 static periph_ctor_t passregister;
90 static periph_oninv_t passoninvalidate;
91 static periph_dtor_t passcleanup;
92 static periph_start_t passstart;
93 static void passasync(void *callback_arg, u_int32_t code,
94 struct cam_path *path, void *arg);
95 static void passdone(struct cam_periph *periph,
97 static int passerror(union ccb *ccb, u_int32_t cam_flags,
98 u_int32_t sense_flags);
99 static int passsendccb(struct cam_periph *periph, union ccb *ccb,
102 static struct periph_driver passdriver =
105 TAILQ_HEAD_INITIALIZER(passdriver.units), /* generation */ 0
108 PERIPHDRIVER_DECLARE(pass, passdriver);
110 static struct dev_ops pass_ops = {
113 .d_close = passclose,
116 .d_ioctl = passioctl,
117 .d_strategy = nostrategy,
120 static struct extend_array *passperiphs;
128 * Create our extend array for storing the devices we attach to.
130 passperiphs = cam_extend_new();
131 if (passperiphs == NULL) {
132 kprintf("passm: Failed to alloc extend array!\n");
137 * Install a global async callback. This callback will
138 * receive async callbacks like "new device found".
140 status = xpt_register_async(AC_FOUND_DEVICE, passasync, NULL, NULL);
142 if (status != CAM_REQ_CMP) {
143 kprintf("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;
154 softc = (struct pass_softc *)periph->softc;
157 * De-register any async callbacks.
159 xpt_register_async(0, passasync, periph, periph->path);
161 softc->flags |= PASS_FLAG_INVALID;
164 xpt_print(periph->path, "lost device\n");
170 passcleanup(struct cam_periph *periph)
172 struct pass_softc *softc;
174 softc = (struct pass_softc *)periph->softc;
176 devstat_remove_entry(&softc->device_stats);
178 cam_extend_release(passperiphs, periph->unit_number);
181 xpt_print(periph->path, "removing device entry\n");
183 dev_ops_remove_minor(&pass_ops, periph->unit_number);
184 kfree(softc, M_DEVBUF);
188 passasync(void *callback_arg, u_int32_t code,
189 struct cam_path *path, void *arg)
191 struct cam_periph *periph;
193 periph = (struct cam_periph *)callback_arg;
196 case AC_FOUND_DEVICE:
198 struct ccb_getdev *cgd;
201 cgd = (struct ccb_getdev *)arg;
206 * Don't complain if a valid peripheral is already attached.
208 periph = cam_periph_find(cgd->ccb_h.path, "pass");
209 if (periph && (periph->flags & CAM_PERIPH_INVALID) == 0)
213 * Allocate a peripheral instance for
214 * this device and start the probe
217 status = cam_periph_alloc(passregister, passoninvalidate,
218 passcleanup, passstart, "pass",
219 CAM_PERIPH_BIO, cgd->ccb_h.path,
220 passasync, AC_FOUND_DEVICE, cgd);
222 if (status != CAM_REQ_CMP && status != CAM_REQ_INPROG) {
223 const struct cam_status_entry *entry;
225 entry = cam_fetch_status_entry(status);
227 kprintf("passasync: Unable to attach new device "
228 "due to status %#x: %s\n", status, entry ?
229 entry->status_text : "Unknown");
235 cam_periph_async(periph, code, path, arg);
241 passregister(struct cam_periph *periph, void *arg)
243 struct pass_softc *softc;
244 struct ccb_getdev *cgd;
247 cgd = (struct ccb_getdev *)arg;
248 if (periph == NULL) {
249 kprintf("passregister: periph was NULL!!\n");
250 return(CAM_REQ_CMP_ERR);
254 kprintf("passregister: no getdev CCB, can't register device\n");
255 return(CAM_REQ_CMP_ERR);
258 softc = kmalloc(sizeof(*softc), M_DEVBUF, M_INTWAIT | M_ZERO);
259 softc->state = PASS_STATE_NORMAL;
260 softc->pd_type = SID_TYPE(&cgd->inq_data);
262 periph->softc = softc;
263 cam_extend_set(passperiphs, periph->unit_number, periph);
266 * We pass in 0 for a blocksize, since we don't
267 * know what the blocksize of this device is, if
268 * it even has a blocksize.
270 CAM_SIM_UNLOCK(periph->sim);
271 no_tags = (cgd->inq_data.flags & SID_CmdQue) == 0;
272 devstat_add_entry(&softc->device_stats, "pass", periph->unit_number, 0,
274 | (no_tags ? DEVSTAT_NO_ORDERED_TAGS : 0),
276 DEVSTAT_TYPE_IF_SCSI |
278 DEVSTAT_PRIORITY_PASS);
280 /* Register the device */
281 make_dev(&pass_ops, periph->unit_number, UID_ROOT,
282 GID_OPERATOR, 0600, "%s%d", periph->periph_name,
283 periph->unit_number);
284 CAM_SIM_LOCK(periph->sim);
286 * Add an async callback so that we get
287 * notified if this device goes away.
289 xpt_register_async(AC_LOST_DEVICE, passasync, periph, periph->path);
292 xpt_announce_periph(periph, NULL);
298 passopen(struct dev_open_args *ap)
300 cdev_t dev = ap->a_head.a_dev;
301 struct cam_periph *periph;
302 struct pass_softc *softc;
305 error = 0; /* default to no error */
307 /* unit = dkunit(dev); */
308 /* XXX KDM fix this */
309 unit = minor(dev) & 0xff;
311 periph = cam_extend_get(passperiphs, unit);
313 if (cam_periph_acquire(periph) != CAM_REQ_CMP)
316 cam_periph_lock(periph);
318 softc = (struct pass_softc *)periph->softc;
320 if (softc->flags & PASS_FLAG_INVALID) {
321 cam_periph_unlock(periph);
322 cam_periph_release(periph);
327 * Don't allow access when we're running at a high securelevel.
329 if (securelevel > 1) {
330 cam_periph_unlock(periph);
331 cam_periph_release(periph);
336 * Only allow read-write access.
338 if (((ap->a_oflags & FWRITE) == 0) || ((ap->a_oflags & FREAD) == 0)) {
339 cam_periph_unlock(periph);
340 cam_periph_release(periph);
345 * We don't allow nonblocking access.
347 if ((ap->a_oflags & O_NONBLOCK) != 0) {
348 xpt_print(periph->path, "can't do nonblocking access\n");
349 cam_periph_unlock(periph);
350 cam_periph_release(periph);
354 if ((softc->flags & PASS_FLAG_OPEN) == 0) {
355 softc->flags |= PASS_FLAG_OPEN;
357 /* Device closes aren't symmertical, so fix up the refcount */
358 cam_periph_release(periph);
361 cam_periph_unlock(periph);
367 passclose(struct dev_close_args *ap)
369 cdev_t dev = ap->a_head.a_dev;
370 struct cam_periph *periph;
371 struct pass_softc *softc;
374 /* unit = dkunit(dev); */
375 /* XXX KDM fix this */
376 unit = minor(dev) & 0xff;
378 periph = cam_extend_get(passperiphs, unit);
382 cam_periph_lock(periph);
384 softc = (struct pass_softc *)periph->softc;
385 softc->flags &= ~PASS_FLAG_OPEN;
387 cam_periph_unlock(periph);
388 cam_periph_release(periph);
394 passstart(struct cam_periph *periph, union ccb *start_ccb)
396 struct pass_softc *softc;
398 softc = (struct pass_softc *)periph->softc;
400 switch (softc->state) {
401 case PASS_STATE_NORMAL:
402 start_ccb->ccb_h.ccb_type = PASS_CCB_WAITING;
403 SLIST_INSERT_HEAD(&periph->ccb_list, &start_ccb->ccb_h,
405 periph->immediate_priority = CAM_PRIORITY_NONE;
406 wakeup(&periph->ccb_list);
412 passdone(struct cam_periph *periph, union ccb *done_ccb)
414 struct ccb_scsiio *csio;
416 csio = &done_ccb->csio;
417 switch (csio->ccb_h.ccb_type) {
418 case PASS_CCB_WAITING:
419 /* Caller will release the CCB */
420 wakeup(&done_ccb->ccb_h.cbfcnp);
423 xpt_release_ccb(done_ccb);
427 passioctl(struct dev_ioctl_args *ap)
429 cdev_t dev = ap->a_head.a_dev;
430 caddr_t addr = ap->a_data;
431 struct cam_periph *periph;
436 /* unit = dkunit(dev); */
437 /* XXX KDM fix this */
438 unit = minor(dev) & 0xff;
440 periph = cam_extend_get(passperiphs, unit);
445 cam_periph_lock(periph);
457 inccb = (union ccb *)addr;
460 * Some CCB types, like scan bus and scan lun can only go
461 * through the transport layer device.
463 if (inccb->ccb_h.func_code & XPT_FC_XPT_ONLY) {
464 xpt_print(periph->path, "CCB function code %#x is "
465 "restricted to the XPT device\n",
466 inccb->ccb_h.func_code);
472 * Non-immediate CCBs need a CCB from the per-device pool
473 * of CCBs, which is scheduled by the transport layer.
474 * Immediate CCBs and user-supplied CCBs should just be
477 if ((inccb->ccb_h.func_code & XPT_FC_QUEUED)
478 && ((inccb->ccb_h.func_code & XPT_FC_USER_CCB) == 0)) {
479 ccb = cam_periph_getccb(periph,
480 inccb->ccb_h.pinfo.priority);
483 ccb = xpt_alloc_ccb();
486 xpt_setup_ccb(&ccb->ccb_h, periph->path,
487 inccb->ccb_h.pinfo.priority);
492 xpt_print(periph->path, "unable to allocate CCB\n");
497 error = passsendccb(periph, ccb, inccb);
502 xpt_release_ccb(ccb);
507 error = cam_periph_ioctl(periph, ap->a_cmd, addr, passerror);
511 cam_periph_unlock(periph);
516 * Generally, "ccb" should be the CCB supplied by the kernel. "inccb"
517 * should be the CCB that is copied in from the user.
520 passsendccb(struct cam_periph *periph, union ccb *ccb, union ccb *inccb)
522 struct pass_softc *softc;
523 struct cam_periph_map_info mapinfo;
524 int error, need_unmap;
526 softc = (struct pass_softc *)periph->softc;
531 * There are some fields in the CCB header that need to be
532 * preserved, the rest we get from the user.
534 xpt_merge_ccb(ccb, inccb);
537 * There's no way for the user to have a completion
538 * function, so we put our own completion function in here.
540 ccb->ccb_h.cbfcnp = passdone;
543 * We only attempt to map the user memory into kernel space
544 * if they haven't passed in a physical memory pointer,
545 * and if there is actually an I/O operation to perform.
546 * Right now cam_periph_mapmem() only supports SCSI and device
547 * match CCBs. For the SCSI CCBs, we only pass the CCB in if
548 * there's actually data to map. cam_periph_mapmem() will do the
549 * right thing, even if there isn't data to map, but since CCBs
550 * without data are a reasonably common occurance (e.g. test unit
551 * ready), it will save a few cycles if we check for it here.
553 if (((ccb->ccb_h.flags & CAM_DATA_PHYS) == 0)
554 && (((ccb->ccb_h.func_code == XPT_SCSI_IO)
555 && ((ccb->ccb_h.flags & CAM_DIR_MASK) != CAM_DIR_NONE))
556 || (ccb->ccb_h.func_code == XPT_DEV_MATCH))) {
558 bzero(&mapinfo, sizeof(mapinfo));
561 * cam_periph_mapmem calls into proc and vm functions that can
562 * sleep as well as trigger I/O, so we can't hold the lock.
563 * Dropping it here is reasonably safe.
565 cam_periph_unlock(periph);
566 error = cam_periph_mapmem(ccb, &mapinfo);
567 cam_periph_lock(periph);
570 * cam_periph_mapmem returned an error, we can't continue.
571 * Return the error to the user.
577 * We successfully mapped the memory in, so we need to
578 * unmap it when the transaction is done.
584 * If the user wants us to perform any error recovery, then honor
585 * that request. Otherwise, it's up to the user to perform any
588 error = cam_periph_runccb(ccb,
589 (ccb->ccb_h.flags & CAM_PASS_ERR_RECOVER) ?
591 /* cam_flags */ CAM_RETRY_SELTO,
592 /* sense_flags */SF_RETRY_UA,
593 &softc->device_stats);
596 cam_periph_unmapmem(ccb, &mapinfo);
598 ccb->ccb_h.cbfcnp = NULL;
599 ccb->ccb_h.periph_priv = inccb->ccb_h.periph_priv;
600 bcopy(ccb, inccb, sizeof(union ccb));
606 passerror(union ccb *ccb, u_int32_t cam_flags, u_int32_t sense_flags)
608 struct cam_periph *periph;
609 struct pass_softc *softc;
611 periph = xpt_path_periph(ccb->ccb_h.path);
612 softc = (struct pass_softc *)periph->softc;
614 return(cam_periph_error(ccb, cam_flags, sense_flags,