xref: /freebsd/sys/cam/scsi/scsi_pass.c (revision b3aaa0cc21c63d388230c7ef2a80abd631ff20d5)
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 	struct cam_sim *sim;
183 
184 	periph = (struct cam_periph *)callback_arg;
185 
186 	switch (code) {
187 	case AC_FOUND_DEVICE:
188 	{
189 		struct ccb_getdev *cgd;
190 		cam_status status;
191 
192 		cgd = (struct ccb_getdev *)arg;
193 		if (cgd == NULL)
194 			break;
195 
196 		/*
197 		 * Allocate a peripheral instance for
198 		 * this device and start the probe
199 		 * process.
200 		 */
201 		sim = xpt_path_sim(cgd->ccb_h.path);
202 		status = cam_periph_alloc(passregister, passoninvalidate,
203 					  passcleanup, passstart, "pass",
204 					  CAM_PERIPH_BIO, cgd->ccb_h.path,
205 					  passasync, AC_FOUND_DEVICE, cgd);
206 
207 		if (status != CAM_REQ_CMP
208 		 && status != CAM_REQ_INPROG) {
209 			const struct cam_status_entry *entry;
210 
211 			entry = cam_fetch_status_entry(status);
212 
213 			printf("passasync: Unable to attach new device "
214 			       "due to status %#x: %s\n", status, entry ?
215 			       entry->status_text : "Unknown");
216 		}
217 
218 		break;
219 	}
220 	default:
221 		cam_periph_async(periph, code, path, arg);
222 		break;
223 	}
224 }
225 
226 static cam_status
227 passregister(struct cam_periph *periph, void *arg)
228 {
229 	struct pass_softc *softc;
230 	struct ccb_getdev *cgd;
231 	int    no_tags;
232 
233 	cgd = (struct ccb_getdev *)arg;
234 	if (periph == NULL) {
235 		printf("passregister: periph was NULL!!\n");
236 		return(CAM_REQ_CMP_ERR);
237 	}
238 
239 	if (cgd == NULL) {
240 		printf("passregister: no getdev CCB, can't register device\n");
241 		return(CAM_REQ_CMP_ERR);
242 	}
243 
244 	softc = (struct pass_softc *)malloc(sizeof(*softc),
245 					    M_DEVBUF, M_NOWAIT);
246 
247 	if (softc == NULL) {
248 		printf("passregister: Unable to probe new device. "
249 		       "Unable to allocate softc\n");
250 		return(CAM_REQ_CMP_ERR);
251 	}
252 
253 	bzero(softc, sizeof(*softc));
254 	softc->state = PASS_STATE_NORMAL;
255 	softc->pd_type = SID_TYPE(&cgd->inq_data);
256 
257 	periph->softc = softc;
258 
259 	/*
260 	 * We pass in 0 for a blocksize, since we don't
261 	 * know what the blocksize of this device is, if
262 	 * it even has a blocksize.
263 	 */
264 	mtx_unlock(periph->sim->mtx);
265 	no_tags = (cgd->inq_data.flags & SID_CmdQue) == 0;
266 	softc->device_stats = devstat_new_entry("pass",
267 			  periph->unit_number, 0,
268 			  DEVSTAT_NO_BLOCKSIZE
269 			  | (no_tags ? DEVSTAT_NO_ORDERED_TAGS : 0),
270 			  softc->pd_type |
271 			  DEVSTAT_TYPE_IF_SCSI |
272 			  DEVSTAT_TYPE_PASS,
273 			  DEVSTAT_PRIORITY_PASS);
274 
275 	/* Register the device */
276 	softc->dev = make_dev(&pass_cdevsw, periph->unit_number,
277 			      UID_ROOT, GID_OPERATOR, 0600, "%s%d",
278 			      periph->periph_name, periph->unit_number);
279 	mtx_lock(periph->sim->mtx);
280 	softc->dev->si_drv1 = periph;
281 
282 	/*
283 	 * Add an async callback so that we get
284 	 * notified if this device goes away.
285 	 */
286 	xpt_register_async(AC_LOST_DEVICE, passasync, periph, periph->path);
287 
288 	if (bootverbose)
289 		xpt_announce_periph(periph, NULL);
290 
291 	return(CAM_REQ_CMP);
292 }
293 
294 static int
295 passopen(struct cdev *dev, int flags, int fmt, struct thread *td)
296 {
297 	struct cam_periph *periph;
298 	struct pass_softc *softc;
299 	int error;
300 
301 	periph = (struct cam_periph *)dev->si_drv1;
302 	if (cam_periph_acquire(periph) != CAM_REQ_CMP)
303 		return (ENXIO);
304 
305 	cam_periph_lock(periph);
306 
307 	softc = (struct pass_softc *)periph->softc;
308 
309 	if (softc->flags & PASS_FLAG_INVALID) {
310 		cam_periph_unlock(periph);
311 		cam_periph_release(periph);
312 		return(ENXIO);
313 	}
314 
315 	/*
316 	 * Don't allow access when we're running at a high securelevel.
317 	 */
318 	error = securelevel_gt(td->td_ucred, 1);
319 	if (error) {
320 		cam_periph_unlock(periph);
321 		cam_periph_release(periph);
322 		return(error);
323 	}
324 
325 	/*
326 	 * Only allow read-write access.
327 	 */
328 	if (((flags & FWRITE) == 0) || ((flags & FREAD) == 0)) {
329 		cam_periph_unlock(periph);
330 		cam_periph_release(periph);
331 		return(EPERM);
332 	}
333 
334 	/*
335 	 * We don't allow nonblocking access.
336 	 */
337 	if ((flags & O_NONBLOCK) != 0) {
338 		xpt_print(periph->path, "can't do nonblocking access\n");
339 		cam_periph_unlock(periph);
340 		cam_periph_release(periph);
341 		return(EINVAL);
342 	}
343 
344 	if ((softc->flags & PASS_FLAG_OPEN) == 0) {
345 		softc->flags |= PASS_FLAG_OPEN;
346 		cam_periph_unlock(periph);
347 	} else {
348 		/* Device closes aren't symmertical, so fix up the refcount */
349 		cam_periph_unlock(periph);
350 		cam_periph_release(periph);
351 	}
352 
353 	return (error);
354 }
355 
356 static int
357 passclose(struct cdev *dev, int flag, int fmt, struct thread *td)
358 {
359 	struct 	cam_periph *periph;
360 	struct	pass_softc *softc;
361 
362 	periph = (struct cam_periph *)dev->si_drv1;
363 	if (periph == NULL)
364 		return (ENXIO);
365 
366 	cam_periph_lock(periph);
367 
368 	softc = (struct pass_softc *)periph->softc;
369 	softc->flags &= ~PASS_FLAG_OPEN;
370 
371 	cam_periph_unlock(periph);
372 	cam_periph_release(periph);
373 
374 	return (0);
375 }
376 
377 static void
378 passstart(struct cam_periph *periph, union ccb *start_ccb)
379 {
380 	struct pass_softc *softc;
381 
382 	softc = (struct pass_softc *)periph->softc;
383 
384 	switch (softc->state) {
385 	case PASS_STATE_NORMAL:
386 		start_ccb->ccb_h.ccb_type = PASS_CCB_WAITING;
387 		SLIST_INSERT_HEAD(&periph->ccb_list, &start_ccb->ccb_h,
388 				  periph_links.sle);
389 		periph->immediate_priority = CAM_PRIORITY_NONE;
390 		wakeup(&periph->ccb_list);
391 		break;
392 	}
393 }
394 
395 static void
396 passdone(struct cam_periph *periph, union ccb *done_ccb)
397 {
398 	struct pass_softc *softc;
399 	struct ccb_scsiio *csio;
400 
401 	softc = (struct pass_softc *)periph->softc;
402 	csio = &done_ccb->csio;
403 	switch (csio->ccb_h.ccb_type) {
404 	case PASS_CCB_WAITING:
405 		/* Caller will release the CCB */
406 		wakeup(&done_ccb->ccb_h.cbfcnp);
407 		return;
408 	}
409 	xpt_release_ccb(done_ccb);
410 }
411 
412 static int
413 passioctl(struct cdev *dev, u_long cmd, caddr_t addr, int flag, struct thread *td)
414 {
415 	struct	cam_periph *periph;
416 	struct	pass_softc *softc;
417 	int	error;
418 
419 	periph = (struct cam_periph *)dev->si_drv1;
420 	if (periph == NULL)
421 		return(ENXIO);
422 
423 	cam_periph_lock(periph);
424 	softc = (struct pass_softc *)periph->softc;
425 
426 	error = 0;
427 
428 	switch (cmd) {
429 
430 	case CAMIOCOMMAND:
431 	{
432 		union ccb *inccb;
433 		union ccb *ccb;
434 		int ccb_malloced;
435 
436 		inccb = (union ccb *)addr;
437 
438 		/*
439 		 * Some CCB types, like scan bus and scan lun can only go
440 		 * through the transport layer device.
441 		 */
442 		if (inccb->ccb_h.func_code & XPT_FC_XPT_ONLY) {
443 			xpt_print(periph->path, "CCB function code %#x is "
444 			    "restricted to the XPT device\n",
445 			    inccb->ccb_h.func_code);
446 			error = ENODEV;
447 			break;
448 		}
449 
450 		/*
451 		 * Non-immediate CCBs need a CCB from the per-device pool
452 		 * of CCBs, which is scheduled by the transport layer.
453 		 * Immediate CCBs and user-supplied CCBs should just be
454 		 * malloced.
455 		 */
456 		if ((inccb->ccb_h.func_code & XPT_FC_QUEUED)
457 		 && ((inccb->ccb_h.func_code & XPT_FC_USER_CCB) == 0)) {
458 			ccb = cam_periph_getccb(periph,
459 						inccb->ccb_h.pinfo.priority);
460 			ccb_malloced = 0;
461 		} else {
462 			ccb = xpt_alloc_ccb_nowait();
463 
464 			if (ccb != NULL)
465 				xpt_setup_ccb(&ccb->ccb_h, periph->path,
466 					      inccb->ccb_h.pinfo.priority);
467 			ccb_malloced = 1;
468 		}
469 
470 		if (ccb == NULL) {
471 			xpt_print(periph->path, "unable to allocate CCB\n");
472 			error = ENOMEM;
473 			break;
474 		}
475 
476 		error = passsendccb(periph, ccb, inccb);
477 
478 		if (ccb_malloced)
479 			xpt_free_ccb(ccb);
480 		else
481 			xpt_release_ccb(ccb);
482 
483 		break;
484 	}
485 	default:
486 		error = cam_periph_ioctl(periph, cmd, addr, passerror);
487 		break;
488 	}
489 
490 	cam_periph_unlock(periph);
491 	return(error);
492 }
493 
494 /*
495  * Generally, "ccb" should be the CCB supplied by the kernel.  "inccb"
496  * should be the CCB that is copied in from the user.
497  */
498 static int
499 passsendccb(struct cam_periph *periph, union ccb *ccb, union ccb *inccb)
500 {
501 	struct pass_softc *softc;
502 	struct cam_periph_map_info mapinfo;
503 	int error, need_unmap;
504 
505 	softc = (struct pass_softc *)periph->softc;
506 
507 	need_unmap = 0;
508 
509 	/*
510 	 * There are some fields in the CCB header that need to be
511 	 * preserved, the rest we get from the user.
512 	 */
513 	xpt_merge_ccb(ccb, inccb);
514 
515 	/*
516 	 * There's no way for the user to have a completion
517 	 * function, so we put our own completion function in here.
518 	 */
519 	ccb->ccb_h.cbfcnp = passdone;
520 
521 	/*
522 	 * We only attempt to map the user memory into kernel space
523 	 * if they haven't passed in a physical memory pointer,
524 	 * and if there is actually an I/O operation to perform.
525 	 * Right now cam_periph_mapmem() only supports SCSI and device
526 	 * match CCBs.  For the SCSI CCBs, we only pass the CCB in if
527 	 * there's actually data to map.  cam_periph_mapmem() will do the
528 	 * right thing, even if there isn't data to map, but since CCBs
529 	 * without data are a reasonably common occurance (e.g. test unit
530 	 * ready), it will save a few cycles if we check for it here.
531 	 */
532 	if (((ccb->ccb_h.flags & CAM_DATA_PHYS) == 0)
533 	 && (((ccb->ccb_h.func_code == XPT_SCSI_IO)
534 	    && ((ccb->ccb_h.flags & CAM_DIR_MASK) != CAM_DIR_NONE))
535 	  || (ccb->ccb_h.func_code == XPT_DEV_MATCH))) {
536 
537 		bzero(&mapinfo, sizeof(mapinfo));
538 
539 		/*
540 		 * cam_periph_mapmem calls into proc and vm functions that can
541 		 * sleep as well as trigger I/O, so we can't hold the lock.
542 		 * Dropping it here is reasonably safe.
543 		 */
544 		cam_periph_unlock(periph);
545 		error = cam_periph_mapmem(ccb, &mapinfo);
546 		cam_periph_lock(periph);
547 
548 		/*
549 		 * cam_periph_mapmem returned an error, we can't continue.
550 		 * Return the error to the user.
551 		 */
552 		if (error)
553 			return(error);
554 
555 		/*
556 		 * We successfully mapped the memory in, so we need to
557 		 * unmap it when the transaction is done.
558 		 */
559 		need_unmap = 1;
560 	}
561 
562 	/*
563 	 * If the user wants us to perform any error recovery, then honor
564 	 * that request.  Otherwise, it's up to the user to perform any
565 	 * error recovery.
566 	 */
567 	error = cam_periph_runccb(ccb,
568 				  (ccb->ccb_h.flags & CAM_PASS_ERR_RECOVER) ?
569 				  passerror : NULL,
570 				  /* cam_flags */ CAM_RETRY_SELTO,
571 				  /* sense_flags */SF_RETRY_UA,
572 				  softc->device_stats);
573 
574 	if (need_unmap != 0)
575 		cam_periph_unmapmem(ccb, &mapinfo);
576 
577 	ccb->ccb_h.cbfcnp = NULL;
578 	ccb->ccb_h.periph_priv = inccb->ccb_h.periph_priv;
579 	bcopy(ccb, inccb, sizeof(union ccb));
580 
581 	return(error);
582 }
583 
584 static int
585 passerror(union ccb *ccb, u_int32_t cam_flags, u_int32_t sense_flags)
586 {
587 	struct cam_periph *periph;
588 	struct pass_softc *softc;
589 
590 	periph = xpt_path_periph(ccb->ccb_h.path);
591 	softc = (struct pass_softc *)periph->softc;
592 
593 	return(cam_periph_error(ccb, cam_flags, sense_flags,
594 				 &softc->saved_ccb));
595 }
596