scsi_pass.c revision 195534
1235783Skib/*-
2235783Skib * Copyright (c) 1997, 1998, 2000 Justin T. Gibbs.
3235783Skib * Copyright (c) 1997, 1998, 1999 Kenneth D. Merry.
4235783Skib * All rights reserved.
5235783Skib *
6235783Skib * Redistribution and use in source and binary forms, with or without
7235783Skib * modification, are permitted provided that the following conditions
8235783Skib * are met:
9235783Skib * 1. Redistributions of source code must retain the above copyright
10235783Skib *    notice, this list of conditions, and the following disclaimer,
11235783Skib *    without modification, immediately at the beginning of the file.
12235783Skib * 2. The name of the author may not be used to endorse or promote products
13235783Skib *    derived from this software without specific prior written permission.
14235783Skib *
15235783Skib * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
16235783Skib * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
17235783Skib * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
18235783Skib * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE FOR
19235783Skib * ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
20235783Skib * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
21235783Skib * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
22235783Skib * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
23235783Skib * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
24235783Skib * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
25235783Skib * SUCH DAMAGE.
26235783Skib */
27235783Skib
28235783Skib#include <sys/cdefs.h>
29235783Skib__FBSDID("$FreeBSD: head/sys/cam/scsi/scsi_pass.c 195534 2009-07-10 08:18:08Z scottl $");
30235783Skib
31235783Skib#include <sys/param.h>
32235783Skib#include <sys/systm.h>
33235783Skib#include <sys/kernel.h>
34235783Skib#include <sys/types.h>
35235783Skib#include <sys/bio.h>
36235783Skib#include <sys/malloc.h>
37235783Skib#include <sys/fcntl.h>
38235783Skib#include <sys/conf.h>
39235783Skib#include <sys/errno.h>
40235783Skib#include <sys/devicestat.h>
41235783Skib#include <sys/proc.h>
42235783Skib
43235783Skib#include <cam/cam.h>
44235783Skib#include <cam/cam_ccb.h>
45235783Skib#include <cam/cam_periph.h>
46235783Skib#include <cam/cam_queue.h>
47235783Skib#include <cam/cam_xpt_periph.h>
48235783Skib#include <cam/cam_debug.h>
49235783Skib#include <cam/cam_sim.h>
50235783Skib
51235783Skib#include <cam/scsi/scsi_all.h>
52235783Skib#include <cam/scsi/scsi_pass.h>
53235783Skib
54235783Skibtypedef enum {
55235783Skib	PASS_FLAG_OPEN			= 0x01,
56235783Skib	PASS_FLAG_LOCKED		= 0x02,
57235783Skib	PASS_FLAG_INVALID		= 0x04
58235783Skib} pass_flags;
59235783Skib
60235783Skibtypedef enum {
61235783Skib	PASS_STATE_NORMAL
62235783Skib} pass_state;
63235783Skib
64235783Skibtypedef enum {
65235783Skib	PASS_CCB_BUFFER_IO,
66235783Skib	PASS_CCB_WAITING
67235783Skib} pass_ccb_types;
68235783Skib
69235783Skib#define ccb_type	ppriv_field0
70235783Skib#define ccb_bp		ppriv_ptr1
71235783Skib
72235783Skibstruct pass_softc {
73235783Skib	pass_state		state;
74235783Skib	pass_flags		flags;
75235783Skib	u_int8_t		pd_type;
76235783Skib	union ccb		saved_ccb;
77235783Skib	struct devstat		*device_stats;
78235783Skib	struct cdev *dev;
79235783Skib};
80235783Skib
81235783Skib
82235783Skibstatic	d_open_t	passopen;
83235783Skibstatic	d_close_t	passclose;
84235783Skibstatic	d_ioctl_t	passioctl;
85235783Skib
86235783Skibstatic	periph_init_t	passinit;
87235783Skibstatic	periph_ctor_t	passregister;
88235783Skibstatic	periph_oninv_t	passoninvalidate;
89235783Skibstatic	periph_dtor_t	passcleanup;
90235783Skibstatic	periph_start_t	passstart;
91235783Skibstatic	void		passasync(void *callback_arg, u_int32_t code,
92235783Skib				  struct cam_path *path, void *arg);
93235783Skibstatic	void		passdone(struct cam_periph *periph,
94235783Skib				 union ccb *done_ccb);
95235783Skibstatic	int		passerror(union ccb *ccb, u_int32_t cam_flags,
96235783Skib				  u_int32_t sense_flags);
97235783Skibstatic 	int		passsendccb(struct cam_periph *periph, union ccb *ccb,
98235783Skib				    union ccb *inccb);
99235783Skib
100235783Skibstatic struct periph_driver passdriver =
101235783Skib{
102235783Skib	passinit, "pass",
103235783Skib	TAILQ_HEAD_INITIALIZER(passdriver.units), /* generation */ 0
104235783Skib};
105235783Skib
106235783SkibPERIPHDRIVER_DECLARE(pass, passdriver);
107235783Skib
108235783Skibstatic struct cdevsw pass_cdevsw = {
109235783Skib	.d_version =	D_VERSION,
110235783Skib	.d_flags =	0,
111235783Skib	.d_open =	passopen,
112235783Skib	.d_close =	passclose,
113235783Skib	.d_ioctl =	passioctl,
114235783Skib	.d_name =	"pass",
115235783Skib};
116235783Skib
117235783Skibstatic void
118235783Skibpassinit(void)
119235783Skib{
120235783Skib	cam_status status;
121235783Skib
122235783Skib	/*
123235783Skib	 * Install a global async callback.  This callback will
124235783Skib	 * receive async callbacks like "new device found".
125235783Skib	 */
126235783Skib	status = xpt_register_async(AC_FOUND_DEVICE, passasync, NULL, NULL);
127235783Skib
128235783Skib	if (status != CAM_REQ_CMP) {
129235783Skib		printf("pass: Failed to attach master async callback "
130235783Skib		       "due to status 0x%x!\n", status);
131235783Skib	}
132235783Skib
133235783Skib}
134235783Skib
135235783Skibstatic void
136235783Skibpassoninvalidate(struct cam_periph *periph)
137235783Skib{
138235783Skib	struct pass_softc *softc;
139235783Skib
140235783Skib	softc = (struct pass_softc *)periph->softc;
141235783Skib
142235783Skib	/*
143235783Skib	 * De-register any async callbacks.
144235783Skib	 */
145235783Skib	xpt_register_async(0, passasync, periph, periph->path);
146235783Skib
147235783Skib	softc->flags |= PASS_FLAG_INVALID;
148235783Skib
149235783Skib	/*
150235783Skib	 * XXX Return all queued I/O with ENXIO.
151235783Skib	 * XXX Handle any transactions queued to the card
152235783Skib	 *     with XPT_ABORT_CCB.
153235783Skib	 */
154235783Skib
155235783Skib	if (bootverbose) {
156235783Skib		xpt_print(periph->path, "lost device\n");
157235783Skib	}
158235783Skib
159235783Skib}
160235783Skib
161235783Skibstatic void
162235783Skibpasscleanup(struct cam_periph *periph)
163235783Skib{
164235783Skib	struct pass_softc *softc;
165235783Skib
166235783Skib	softc = (struct pass_softc *)periph->softc;
167235783Skib
168235783Skib	if (bootverbose)
169235783Skib		xpt_print(periph->path, "removing device entry\n");
170235783Skib	devstat_remove_entry(softc->device_stats);
171235783Skib	cam_periph_unlock(periph);
172235783Skib	destroy_dev(softc->dev);
173235783Skib	cam_periph_lock(periph);
174235783Skib	free(softc, M_DEVBUF);
175235783Skib}
176235783Skib
177235783Skibstatic void
178235783Skibpassasync(void *callback_arg, u_int32_t code,
179235783Skib	  struct cam_path *path, void *arg)
180235783Skib{
181235783Skib	struct cam_periph *periph;
182235783Skib
183235783Skib	periph = (struct cam_periph *)callback_arg;
184235783Skib
185255037Sjkim	switch (code) {
186235783Skib	case AC_FOUND_DEVICE:
187235783Skib	{
188235783Skib		struct ccb_getdev *cgd;
189235783Skib		cam_status status;
190235783Skib
191235783Skib		cgd = (struct ccb_getdev *)arg;
192235783Skib		if (cgd == NULL)
193255037Sjkim			break;
194235783Skib
195235783Skib		/*
196235783Skib		 * Allocate a peripheral instance for
197235783Skib		 * this device and start the probe
198235783Skib		 * process.
199235783Skib		 */
200235783Skib		status = cam_periph_alloc(passregister, passoninvalidate,
201235783Skib					  passcleanup, passstart, "pass",
202235783Skib					  CAM_PERIPH_BIO, cgd->ccb_h.path,
203235783Skib					  passasync, AC_FOUND_DEVICE, cgd);
204235783Skib
205235783Skib		if (status != CAM_REQ_CMP
206235783Skib		 && status != CAM_REQ_INPROG) {
207235783Skib			const struct cam_status_entry *entry;
208235783Skib
209235783Skib			entry = cam_fetch_status_entry(status);
210235783Skib
211255037Sjkim			printf("passasync: Unable to attach new device "
212235783Skib			       "due to status %#x: %s\n", status, entry ?
213235783Skib			       entry->status_text : "Unknown");
214235783Skib		}
215235783Skib
216235783Skib		break;
217235783Skib	}
218235783Skib	default:
219235783Skib		cam_periph_async(periph, code, path, arg);
220235783Skib		break;
221235783Skib	}
222235783Skib}
223235783Skib
224235783Skibstatic cam_status
225235783Skibpassregister(struct cam_periph *periph, void *arg)
226235783Skib{
227235783Skib	struct pass_softc *softc;
228235783Skib	struct ccb_getdev *cgd;
229235783Skib	int    no_tags;
230235783Skib
231235783Skib	cgd = (struct ccb_getdev *)arg;
232235783Skib	if (periph == NULL) {
233235783Skib		printf("passregister: periph was NULL!!\n");
234235783Skib		return(CAM_REQ_CMP_ERR);
235235783Skib	}
236235783Skib
237235783Skib	if (cgd == NULL) {
238235783Skib		printf("passregister: no getdev CCB, can't register device\n");
239235783Skib		return(CAM_REQ_CMP_ERR);
240235783Skib	}
241235783Skib
242235783Skib	softc = (struct pass_softc *)malloc(sizeof(*softc),
243235783Skib					    M_DEVBUF, M_NOWAIT);
244235783Skib
245235783Skib	if (softc == NULL) {
246235783Skib		printf("passregister: Unable to probe new device. "
247235783Skib		       "Unable to allocate softc\n");
248235783Skib		return(CAM_REQ_CMP_ERR);
249235783Skib	}
250235783Skib
251235783Skib	bzero(softc, sizeof(*softc));
252235783Skib	softc->state = PASS_STATE_NORMAL;
253235783Skib	softc->pd_type = SID_TYPE(&cgd->inq_data);
254235783Skib
255235783Skib	periph->softc = softc;
256235783Skib
257235783Skib	/*
258235783Skib	 * We pass in 0 for a blocksize, since we don't
259235783Skib	 * know what the blocksize of this device is, if
260235783Skib	 * it even has a blocksize.
261235783Skib	 */
262235783Skib	mtx_unlock(periph->sim->mtx);
263235783Skib	no_tags = (cgd->inq_data.flags & SID_CmdQue) == 0;
264235783Skib	softc->device_stats = devstat_new_entry("pass",
265235783Skib			  periph->unit_number, 0,
266235783Skib			  DEVSTAT_NO_BLOCKSIZE
267235783Skib			  | (no_tags ? DEVSTAT_NO_ORDERED_TAGS : 0),
268235783Skib			  softc->pd_type |
269235783Skib			  DEVSTAT_TYPE_IF_SCSI |
270235783Skib			  DEVSTAT_TYPE_PASS,
271235783Skib			  DEVSTAT_PRIORITY_PASS);
272235783Skib
273235783Skib	/* Register the device */
274235783Skib	softc->dev = make_dev(&pass_cdevsw, periph->unit_number,
275235783Skib			      UID_ROOT, GID_OPERATOR, 0600, "%s%d",
276235783Skib			      periph->periph_name, periph->unit_number);
277235783Skib	mtx_lock(periph->sim->mtx);
278235783Skib	softc->dev->si_drv1 = periph;
279235783Skib
280235783Skib	/*
281235783Skib	 * Add an async callback so that we get
282235783Skib	 * notified if this device goes away.
283235783Skib	 */
284235783Skib	xpt_register_async(AC_LOST_DEVICE, passasync, periph, periph->path);
285235783Skib
286235783Skib	if (bootverbose)
287235783Skib		xpt_announce_periph(periph, NULL);
288235783Skib
289235783Skib	return(CAM_REQ_CMP);
290235783Skib}
291235783Skib
292235783Skibstatic int
293235783Skibpassopen(struct cdev *dev, int flags, int fmt, struct thread *td)
294235783Skib{
295235783Skib	struct cam_periph *periph;
296235783Skib	struct pass_softc *softc;
297235783Skib	int error;
298235783Skib
299235783Skib	periph = (struct cam_periph *)dev->si_drv1;
300235783Skib	if (cam_periph_acquire(periph) != CAM_REQ_CMP)
301235783Skib		return (ENXIO);
302235783Skib
303235783Skib	cam_periph_lock(periph);
304235783Skib
305235783Skib	softc = (struct pass_softc *)periph->softc;
306235783Skib
307235783Skib	if (softc->flags & PASS_FLAG_INVALID) {
308235783Skib		cam_periph_unlock(periph);
309235783Skib		cam_periph_release(periph);
310235783Skib		return(ENXIO);
311235783Skib	}
312235783Skib
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
354static int
355passclose(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
375static void
376passstart(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
393static void
394passdone(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
410static int
411passioctl(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 */
496static int
497passsendccb(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
583static int
584passerror(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