xref: /freebsd/sys/cam/scsi/scsi_pass.c (revision ee2ea5ceafed78a5bd9810beb9e3ca927180c226)
1 /*
2  * Copyright (c) 1997, 1998, 2000 Justin T. Gibbs.
3  * Copyright (c) 1997, 1998, 1999 Kenneth D. Merry.
4  * All rights reserved.
5  *
6  * Redistribution and use in source and binary forms, with or without
7  * modification, are permitted provided that the following conditions
8  * are met:
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.
14  *
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
25  * SUCH DAMAGE.
26  *
27  * $FreeBSD$
28  */
29 
30 #include <sys/param.h>
31 #include <sys/systm.h>
32 #include <sys/kernel.h>
33 #include <sys/types.h>
34 #include <sys/bio.h>
35 #include <sys/malloc.h>
36 #include <sys/fcntl.h>
37 #include <sys/conf.h>
38 #include <sys/errno.h>
39 #include <sys/devicestat.h>
40 #include <sys/proc.h>
41 
42 #include <cam/cam.h>
43 #include <cam/cam_ccb.h>
44 #include <cam/cam_extend.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>
49 
50 #include <cam/scsi/scsi_all.h>
51 #include <cam/scsi/scsi_pass.h>
52 
53 typedef enum {
54 	PASS_FLAG_OPEN			= 0x01,
55 	PASS_FLAG_LOCKED		= 0x02,
56 	PASS_FLAG_INVALID		= 0x04
57 } pass_flags;
58 
59 typedef enum {
60 	PASS_STATE_NORMAL
61 } pass_state;
62 
63 typedef enum {
64 	PASS_CCB_BUFFER_IO,
65 	PASS_CCB_WAITING
66 } pass_ccb_types;
67 
68 #define ccb_type	ppriv_field0
69 #define ccb_bp		ppriv_ptr1
70 
71 struct pass_softc {
72 	pass_state		state;
73 	pass_flags		flags;
74 	u_int8_t		pd_type;
75 	union ccb		saved_ccb;
76 	struct devstat		device_stats;
77 	dev_t			dev;
78 };
79 
80 #ifndef MIN
81 #define MIN(x,y) ((x<y) ? x : y)
82 #endif
83 
84 #define PASS_CDEV_MAJOR 31
85 
86 static	d_open_t	passopen;
87 static	d_close_t	passclose;
88 static	d_ioctl_t	passioctl;
89 
90 static	periph_init_t	passinit;
91 static	periph_ctor_t	passregister;
92 static	periph_oninv_t	passoninvalidate;
93 static	periph_dtor_t	passcleanup;
94 static	periph_start_t	passstart;
95 static	void		passasync(void *callback_arg, u_int32_t code,
96 				  struct cam_path *path, void *arg);
97 static	void		passdone(struct cam_periph *periph,
98 				 union ccb *done_ccb);
99 static	int		passerror(union ccb *ccb, u_int32_t cam_flags,
100 				  u_int32_t sense_flags);
101 static 	int		passsendccb(struct cam_periph *periph, union ccb *ccb,
102 				    union ccb *inccb);
103 
104 static struct periph_driver passdriver =
105 {
106 	passinit, "pass",
107 	TAILQ_HEAD_INITIALIZER(passdriver.units), /* generation */ 0
108 };
109 
110 PERIPHDRIVER_DECLARE(pass, passdriver);
111 
112 static struct cdevsw pass_cdevsw = {
113 	/* open */	passopen,
114 	/* close */	passclose,
115 	/* read */	noread,
116 	/* write */	nowrite,
117 	/* ioctl */	passioctl,
118 	/* poll */	nopoll,
119 	/* mmap */	nommap,
120 	/* strategy */	nostrategy,
121 	/* name */	"pass",
122 	/* maj */	PASS_CDEV_MAJOR,
123 	/* dump */	nodump,
124 	/* psize */	nopsize,
125 	/* flags */	0,
126 };
127 
128 static struct extend_array *passperiphs;
129 
130 static void
131 passinit(void)
132 {
133 	cam_status status;
134 	struct cam_path *path;
135 
136 	/*
137 	 * Create our extend array for storing the devices we attach to.
138 	 */
139 	passperiphs = cam_extend_new();
140 	if (passperiphs == NULL) {
141 		printf("passm: Failed to alloc extend array!\n");
142 		return;
143 	}
144 
145 	/*
146 	 * Install a global async callback.  This callback will
147 	 * receive async callbacks like "new device found".
148 	 */
149 	status = xpt_create_path(&path, /*periph*/NULL, CAM_XPT_PATH_ID,
150 				 CAM_TARGET_WILDCARD, CAM_LUN_WILDCARD);
151 
152 	if (status == CAM_REQ_CMP) {
153 		struct ccb_setasync csa;
154 
155                 xpt_setup_ccb(&csa.ccb_h, path, /*priority*/5);
156                 csa.ccb_h.func_code = XPT_SASYNC_CB;
157                 csa.event_enable = AC_FOUND_DEVICE;
158                 csa.callback = passasync;
159                 csa.callback_arg = NULL;
160                 xpt_action((union ccb *)&csa);
161 		status = csa.ccb_h.status;
162                 xpt_free_path(path);
163         }
164 
165 	if (status != CAM_REQ_CMP) {
166 		printf("pass: Failed to attach master async callback "
167 		       "due to status 0x%x!\n", status);
168 	}
169 
170 }
171 
172 static void
173 passoninvalidate(struct cam_periph *periph)
174 {
175 	struct pass_softc *softc;
176 	struct ccb_setasync csa;
177 
178 	softc = (struct pass_softc *)periph->softc;
179 
180 	/*
181 	 * De-register any async callbacks.
182 	 */
183 	xpt_setup_ccb(&csa.ccb_h, periph->path,
184 		      /* priority */ 5);
185 	csa.ccb_h.func_code = XPT_SASYNC_CB;
186 	csa.event_enable = 0;
187 	csa.callback = passasync;
188 	csa.callback_arg = periph;
189 	xpt_action((union ccb *)&csa);
190 
191 	softc->flags |= PASS_FLAG_INVALID;
192 
193 	/*
194 	 * XXX Return all queued I/O with ENXIO.
195 	 * XXX Handle any transactions queued to the card
196 	 *     with XPT_ABORT_CCB.
197 	 */
198 
199 	if (bootverbose) {
200 		xpt_print_path(periph->path);
201 		printf("lost device\n");
202 	}
203 
204 }
205 
206 static void
207 passcleanup(struct cam_periph *periph)
208 {
209 	struct pass_softc *softc;
210 
211 	softc = (struct pass_softc *)periph->softc;
212 
213 	devstat_remove_entry(&softc->device_stats);
214 
215 	destroy_dev(softc->dev);
216 
217 	cam_extend_release(passperiphs, periph->unit_number);
218 
219 	if (bootverbose) {
220 		xpt_print_path(periph->path);
221 		printf("removing device entry\n");
222 	}
223 	free(softc, M_DEVBUF);
224 }
225 
226 static void
227 passasync(void *callback_arg, u_int32_t code,
228 	  struct cam_path *path, void *arg)
229 {
230 	struct cam_periph *periph;
231 
232 	periph = (struct cam_periph *)callback_arg;
233 
234 	switch (code) {
235 	case AC_FOUND_DEVICE:
236 	{
237 		struct ccb_getdev *cgd;
238 		cam_status status;
239 
240 		cgd = (struct ccb_getdev *)arg;
241 		if (cgd == NULL)
242 			break;
243 
244 		/*
245 		 * Allocate a peripheral instance for
246 		 * this device and start the probe
247 		 * process.
248 		 */
249 		status = cam_periph_alloc(passregister, passoninvalidate,
250 					  passcleanup, passstart, "pass",
251 					  CAM_PERIPH_BIO, cgd->ccb_h.path,
252 					  passasync, AC_FOUND_DEVICE, cgd);
253 
254 		if (status != CAM_REQ_CMP
255 		 && status != CAM_REQ_INPROG) {
256 			const struct cam_status_entry *entry;
257 
258 			entry = cam_fetch_status_entry(status);
259 
260 			printf("passasync: Unable to attach new device "
261 			       "due to status %#x: %s\n", status, entry ?
262 			       entry->status_text : "Unknown");
263 		}
264 
265 		break;
266 	}
267 	default:
268 		cam_periph_async(periph, code, path, arg);
269 		break;
270 	}
271 }
272 
273 static cam_status
274 passregister(struct cam_periph *periph, void *arg)
275 {
276 	struct pass_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("passregister: periph was NULL!!\n");
284 		return(CAM_REQ_CMP_ERR);
285 	}
286 
287 	if (cgd == NULL) {
288 		printf("passregister: no getdev CCB, can't register device\n");
289 		return(CAM_REQ_CMP_ERR);
290 	}
291 
292 	softc = (struct pass_softc *)malloc(sizeof(*softc),
293 					    M_DEVBUF, M_NOWAIT);
294 
295 	if (softc == NULL) {
296 		printf("passregister: Unable to probe new device. "
297 		       "Unable to allocate softc\n");
298 		return(CAM_REQ_CMP_ERR);
299 	}
300 
301 	bzero(softc, sizeof(*softc));
302 	softc->state = PASS_STATE_NORMAL;
303 	softc->pd_type = SID_TYPE(&cgd->inq_data);
304 
305 	periph->softc = softc;
306 	cam_extend_set(passperiphs, periph->unit_number, periph);
307 
308 	/*
309 	 * We pass in 0 for a blocksize, since we don't
310 	 * know what the blocksize of this device is, if
311 	 * it even has a blocksize.
312 	 */
313 	no_tags = (cgd->inq_data.flags & SID_CmdQue) == 0;
314 	devstat_add_entry(&softc->device_stats, "pass", periph->unit_number, 0,
315 			  DEVSTAT_NO_BLOCKSIZE
316 			  | (no_tags ? DEVSTAT_NO_ORDERED_TAGS : 0),
317 			  softc->pd_type |
318 			  DEVSTAT_TYPE_IF_SCSI |
319 			  DEVSTAT_TYPE_PASS,
320 			  DEVSTAT_PRIORITY_PASS);
321 
322 	/* Register the device */
323 	softc->dev = make_dev(&pass_cdevsw, periph->unit_number, UID_ROOT,
324 			      GID_OPERATOR, 0600, "%s%d", periph->periph_name,
325 			      periph->unit_number);
326 
327 	/*
328 	 * Add an async callback so that we get
329 	 * notified if this device goes away.
330 	 */
331 	xpt_setup_ccb(&csa.ccb_h, periph->path, /* priority */ 5);
332 	csa.ccb_h.func_code = XPT_SASYNC_CB;
333 	csa.event_enable = AC_LOST_DEVICE;
334 	csa.callback = passasync;
335 	csa.callback_arg = periph;
336 	xpt_action((union ccb *)&csa);
337 
338 	if (bootverbose)
339 		xpt_announce_periph(periph, NULL);
340 
341 	return(CAM_REQ_CMP);
342 }
343 
344 static int
345 passopen(dev_t dev, int flags, int fmt, struct thread *td)
346 {
347 	struct cam_periph *periph;
348 	struct pass_softc *softc;
349 	int unit, error;
350 	int s;
351 
352 	error = 0; /* default to no error */
353 
354 	/* unit = dkunit(dev); */
355 	/* XXX KDM fix this */
356 	unit = minor(dev) & 0xff;
357 
358 	periph = cam_extend_get(passperiphs, unit);
359 
360 	if (periph == NULL)
361 		return (ENXIO);
362 
363 	softc = (struct pass_softc *)periph->softc;
364 
365 	s = splsoftcam();
366 	if (softc->flags & PASS_FLAG_INVALID) {
367 		splx(s);
368 		return(ENXIO);
369 	}
370 
371 	/*
372 	 * Don't allow access when we're running at a high securelevel.
373 	 */
374 	error = securelevel_gt(td->td_ucred, 1);
375 	if (error) {
376 		splx(s);
377 		return(error);
378 	}
379 
380 	/*
381 	 * Only allow read-write access.
382 	 */
383 	if (((flags & FWRITE) == 0) || ((flags & FREAD) == 0)) {
384 		splx(s);
385 		return(EPERM);
386 	}
387 
388 	/*
389 	 * We don't allow nonblocking access.
390 	 */
391 	if ((flags & O_NONBLOCK) != 0) {
392 		xpt_print_path(periph->path);
393 		printf("can't do nonblocking accesss\n");
394 		splx(s);
395 		return(EINVAL);
396 	}
397 
398 	if ((error = cam_periph_lock(periph, PRIBIO | PCATCH)) != 0) {
399 		splx(s);
400 		return (error);
401 	}
402 
403 	splx(s);
404 
405 	if ((softc->flags & PASS_FLAG_OPEN) == 0) {
406 		if (cam_periph_acquire(periph) != CAM_REQ_CMP)
407 			return(ENXIO);
408 		softc->flags |= PASS_FLAG_OPEN;
409 	}
410 
411 	cam_periph_unlock(periph);
412 
413 	return (error);
414 }
415 
416 static int
417 passclose(dev_t dev, int flag, int fmt, struct thread *td)
418 {
419 	struct 	cam_periph *periph;
420 	struct	pass_softc *softc;
421 	int	unit, error;
422 
423 	/* unit = dkunit(dev); */
424 	/* XXX KDM fix this */
425 	unit = minor(dev) & 0xff;
426 
427 	periph = cam_extend_get(passperiphs, unit);
428 	if (periph == NULL)
429 		return (ENXIO);
430 
431 	softc = (struct pass_softc *)periph->softc;
432 
433 	if ((error = cam_periph_lock(periph, PRIBIO)) != 0)
434 		return (error);
435 
436 	softc->flags &= ~PASS_FLAG_OPEN;
437 
438 	cam_periph_unlock(periph);
439 	cam_periph_release(periph);
440 
441 	return (0);
442 }
443 
444 static void
445 passstart(struct cam_periph *periph, union ccb *start_ccb)
446 {
447 	struct pass_softc *softc;
448 	int s;
449 
450 	softc = (struct pass_softc *)periph->softc;
451 
452 	switch (softc->state) {
453 	case PASS_STATE_NORMAL:
454 		s = splbio();
455 		start_ccb->ccb_h.ccb_type = PASS_CCB_WAITING;
456 		SLIST_INSERT_HEAD(&periph->ccb_list, &start_ccb->ccb_h,
457 				  periph_links.sle);
458 		periph->immediate_priority = CAM_PRIORITY_NONE;
459 		splx(s);
460 		wakeup(&periph->ccb_list);
461 		break;
462 	}
463 }
464 
465 static void
466 passdone(struct cam_periph *periph, union ccb *done_ccb)
467 {
468 	struct pass_softc *softc;
469 	struct ccb_scsiio *csio;
470 
471 	softc = (struct pass_softc *)periph->softc;
472 	csio = &done_ccb->csio;
473 	switch (csio->ccb_h.ccb_type) {
474 	case PASS_CCB_WAITING:
475 		/* Caller will release the CCB */
476 		wakeup(&done_ccb->ccb_h.cbfcnp);
477 		return;
478 	}
479 	xpt_release_ccb(done_ccb);
480 }
481 
482 static int
483 passioctl(dev_t dev, u_long cmd, caddr_t addr, int flag, struct thread *td)
484 {
485 	struct 	cam_periph *periph;
486 	struct	pass_softc *softc;
487 	u_int8_t unit;
488 	int      error;
489 
490 
491 	/* unit = dkunit(dev); */
492 	/* XXX KDM fix this */
493 	unit = minor(dev) & 0xff;
494 
495 	periph = cam_extend_get(passperiphs, unit);
496 
497 	if (periph == NULL)
498 		return(ENXIO);
499 
500 	softc = (struct pass_softc *)periph->softc;
501 
502 	error = 0;
503 
504 	switch (cmd) {
505 
506 	case CAMIOCOMMAND:
507 	{
508 		union ccb *inccb;
509 		union ccb *ccb;
510 		int ccb_malloced;
511 
512 		inccb = (union ccb *)addr;
513 
514 		/*
515 		 * Some CCB types, like scan bus and scan lun can only go
516 		 * through the transport layer device.
517 		 */
518 		if (inccb->ccb_h.func_code & XPT_FC_XPT_ONLY) {
519 			xpt_print_path(periph->path);
520 			printf("CCB function code %#x is restricted to the "
521 			       "XPT device\n", inccb->ccb_h.func_code);
522 			error = ENODEV;
523 			break;
524 		}
525 
526 		/*
527 		 * Non-immediate CCBs need a CCB from the per-device pool
528 		 * of CCBs, which is scheduled by the transport layer.
529 		 * Immediate CCBs and user-supplied CCBs should just be
530 		 * malloced.
531 		 */
532 		if ((inccb->ccb_h.func_code & XPT_FC_QUEUED)
533 		 && ((inccb->ccb_h.func_code & XPT_FC_USER_CCB) == 0)) {
534 			ccb = cam_periph_getccb(periph,
535 						inccb->ccb_h.pinfo.priority);
536 			ccb_malloced = 0;
537 		} else {
538 			ccb = xpt_alloc_ccb();
539 
540 			if (ccb != NULL)
541 				xpt_setup_ccb(&ccb->ccb_h, periph->path,
542 					      inccb->ccb_h.pinfo.priority);
543 			ccb_malloced = 1;
544 		}
545 
546 		if (ccb == NULL) {
547 			xpt_print_path(periph->path);
548 			printf("unable to allocate CCB\n");
549 			error = ENOMEM;
550 			break;
551 		}
552 
553 		error = passsendccb(periph, ccb, inccb);
554 
555 		if (ccb_malloced)
556 			xpt_free_ccb(ccb);
557 		else
558 			xpt_release_ccb(ccb);
559 
560 		break;
561 	}
562 	default:
563 		error = cam_periph_ioctl(periph, cmd, addr, passerror);
564 		break;
565 	}
566 
567 	return(error);
568 }
569 
570 /*
571  * Generally, "ccb" should be the CCB supplied by the kernel.  "inccb"
572  * should be the CCB that is copied in from the user.
573  */
574 static int
575 passsendccb(struct cam_periph *periph, union ccb *ccb, union ccb *inccb)
576 {
577 	struct pass_softc *softc;
578 	struct cam_periph_map_info mapinfo;
579 	int error, need_unmap;
580 
581 	softc = (struct pass_softc *)periph->softc;
582 
583 	need_unmap = 0;
584 
585 	/*
586 	 * There are some fields in the CCB header that need to be
587 	 * preserved, the rest we get from the user.
588 	 */
589 	xpt_merge_ccb(ccb, inccb);
590 
591 	/*
592 	 * There's no way for the user to have a completion
593 	 * function, so we put our own completion function in here.
594 	 */
595 	ccb->ccb_h.cbfcnp = passdone;
596 
597 	/*
598 	 * We only attempt to map the user memory into kernel space
599 	 * if they haven't passed in a physical memory pointer,
600 	 * and if there is actually an I/O operation to perform.
601 	 * Right now cam_periph_mapmem() only supports SCSI and device
602 	 * match CCBs.  For the SCSI CCBs, we only pass the CCB in if
603 	 * there's actually data to map.  cam_periph_mapmem() will do the
604 	 * right thing, even if there isn't data to map, but since CCBs
605 	 * without data are a reasonably common occurance (e.g. test unit
606 	 * ready), it will save a few cycles if we check for it here.
607 	 */
608 	if (((ccb->ccb_h.flags & CAM_DATA_PHYS) == 0)
609 	 && (((ccb->ccb_h.func_code == XPT_SCSI_IO)
610 	    && ((ccb->ccb_h.flags & CAM_DIR_MASK) != CAM_DIR_NONE))
611 	  || (ccb->ccb_h.func_code == XPT_DEV_MATCH))) {
612 
613 		bzero(&mapinfo, sizeof(mapinfo));
614 
615 		error = cam_periph_mapmem(ccb, &mapinfo);
616 
617 		/*
618 		 * cam_periph_mapmem returned an error, we can't continue.
619 		 * Return the error to the user.
620 		 */
621 		if (error)
622 			return(error);
623 
624 		/*
625 		 * We successfully mapped the memory in, so we need to
626 		 * unmap it when the transaction is done.
627 		 */
628 		need_unmap = 1;
629 	}
630 
631 	/*
632 	 * If the user wants us to perform any error recovery, then honor
633 	 * that request.  Otherwise, it's up to the user to perform any
634 	 * error recovery.
635 	 */
636 	error = cam_periph_runccb(ccb,
637 				  (ccb->ccb_h.flags & CAM_PASS_ERR_RECOVER) ?
638 				  passerror : NULL,
639 				  /* cam_flags */ CAM_RETRY_SELTO,
640 				  /* sense_flags */SF_RETRY_UA,
641 				  &softc->device_stats);
642 
643 	if (need_unmap != 0)
644 		cam_periph_unmapmem(ccb, &mapinfo);
645 
646 	ccb->ccb_h.cbfcnp = NULL;
647 	ccb->ccb_h.periph_priv = inccb->ccb_h.periph_priv;
648 	bcopy(ccb, inccb, sizeof(union ccb));
649 
650 	return(error);
651 }
652 
653 static int
654 passerror(union ccb *ccb, u_int32_t cam_flags, u_int32_t sense_flags)
655 {
656 	struct cam_periph *periph;
657 	struct pass_softc *softc;
658 
659 	periph = xpt_path_periph(ccb->ccb_h.path);
660 	softc = (struct pass_softc *)periph->softc;
661 
662 	return(cam_periph_error(ccb, cam_flags, sense_flags,
663 				 &softc->saved_ccb));
664 }
665