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