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