xref: /freebsd/sys/cam/scsi/scsi_target.c (revision f856af0466c076beef4ea9b15d088e1119a945b8)
1 /*-
2  * Generic SCSI Target Kernel Mode Driver
3  *
4  * Copyright (c) 2002 Nate Lawson.
5  * Copyright (c) 1998, 1999, 2001, 2002 Justin T. Gibbs.
6  * All rights reserved.
7  *
8  * Redistribution and use in source and binary forms, with or without
9  * modification, are permitted provided that the following conditions
10  * are met:
11  * 1. Redistributions of source code must retain the above copyright
12  *    notice, this list of conditions, and the following disclaimer,
13  *    without modification, immediately at the beginning of the file.
14  * 2. The name of the author may not be used to endorse or promote products
15  *    derived from this software without specific prior written permission.
16  *
17  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
18  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
19  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
20  * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE FOR
21  * ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
22  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
23  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
24  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
25  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
26  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
27  * SUCH DAMAGE.
28  */
29 
30 #include <sys/cdefs.h>
31 __FBSDID("$FreeBSD$");
32 
33 
34 #include <sys/param.h>
35 #include <sys/systm.h>
36 #include <sys/kernel.h>
37 #include <sys/conf.h>
38 #include <sys/malloc.h>
39 #include <sys/poll.h>
40 #include <sys/vnode.h>
41 #include <sys/lock.h>
42 #include <sys/mutex.h>
43 #include <sys/devicestat.h>
44 #include <sys/proc.h>
45 
46 #include <cam/cam.h>
47 #include <cam/cam_ccb.h>
48 #include <cam/cam_periph.h>
49 #include <cam/cam_xpt_periph.h>
50 #include <cam/scsi/scsi_targetio.h>
51 
52 /* Transaction information attached to each CCB sent by the user */
53 struct targ_cmd_descr {
54 	struct cam_periph_map_info  mapinfo;
55 	TAILQ_ENTRY(targ_cmd_descr) tqe;
56 	union ccb *user_ccb;
57 	int	   priority;
58 	int	   func_code;
59 };
60 
61 /* Offset into the private CCB area for storing our descriptor */
62 #define targ_descr	periph_priv.entries[1].ptr
63 
64 TAILQ_HEAD(descr_queue, targ_cmd_descr);
65 
66 typedef enum {
67 	TARG_STATE_RESV		= 0x00, /* Invalid state */
68 	TARG_STATE_OPENED	= 0x01, /* Device opened, softc initialized */
69 	TARG_STATE_LUN_ENABLED	= 0x02  /* Device enabled for a path */
70 } targ_state;
71 
72 /* Per-instance device software context */
73 struct targ_softc {
74 	/* CCBs (CTIOs, ATIOs, INOTs) pending on the controller */
75 	struct ccb_queue	 pending_ccb_queue;
76 
77 	/* Command descriptors awaiting CTIO resources from the XPT */
78 	struct descr_queue	 work_queue;
79 
80 	/* Command descriptors that have been aborted back to the user. */
81 	struct descr_queue	 abort_queue;
82 
83 	/*
84 	 * Queue of CCBs that have been copied out to userland, but our
85 	 * userland daemon has not yet seen.
86 	 */
87 	struct ccb_queue	 user_ccb_queue;
88 
89 	struct cam_periph	*periph;
90 	struct cam_path		*path;
91 	targ_state		 state;
92 	struct selinfo		 read_select;
93 	struct devstat		 device_stats;
94 };
95 
96 static d_open_t		targopen;
97 static d_close_t	targclose;
98 static d_read_t		targread;
99 static d_write_t	targwrite;
100 static d_ioctl_t	targioctl;
101 static d_poll_t		targpoll;
102 static d_kqfilter_t	targkqfilter;
103 static void		targreadfiltdetach(struct knote *kn);
104 static int		targreadfilt(struct knote *kn, long hint);
105 static struct filterops targread_filtops =
106 	{ 1, NULL, targreadfiltdetach, targreadfilt };
107 
108 static struct cdevsw targ_cdevsw = {
109 	.d_version =	D_VERSION,
110 	.d_flags =	D_NEEDGIANT,
111 	.d_open =	targopen,
112 	.d_close =	targclose,
113 	.d_read =	targread,
114 	.d_write =	targwrite,
115 	.d_ioctl =	targioctl,
116 	.d_poll =	targpoll,
117 	.d_name =	"targ",
118 	.d_kqfilter =	targkqfilter
119 };
120 
121 static cam_status	targendislun(struct cam_path *path, int enable,
122 				     int grp6_len, int grp7_len);
123 static cam_status	targenable(struct targ_softc *softc,
124 				   struct cam_path *path,
125 				   int grp6_len, int grp7_len);
126 static cam_status	targdisable(struct targ_softc *softc);
127 static periph_ctor_t    targctor;
128 static periph_dtor_t    targdtor;
129 static periph_start_t   targstart;
130 static int		targusermerge(struct targ_softc *softc,
131 				      struct targ_cmd_descr *descr,
132 				      union ccb *ccb);
133 static int		targsendccb(struct targ_softc *softc, union ccb *ccb,
134 				    struct targ_cmd_descr *descr);
135 static void		targdone(struct cam_periph *periph,
136 				 union  ccb *done_ccb);
137 static int		targreturnccb(struct targ_softc *softc,
138 				      union  ccb *ccb);
139 static union ccb *	targgetccb(struct targ_softc *softc, xpt_opcode type,
140 				   int priority);
141 static void		targfreeccb(struct targ_softc *softc, union ccb *ccb);
142 static struct targ_cmd_descr *
143 			targgetdescr(struct targ_softc *softc);
144 static periph_init_t	targinit;
145 static void		targclone(void *arg, struct ucred *cred, char *name,
146 				  int namelen, struct cdev **dev);
147 static void		targasync(void *callback_arg, u_int32_t code,
148 				  struct cam_path *path, void *arg);
149 static void		abort_all_pending(struct targ_softc *softc);
150 static void		notify_user(struct targ_softc *softc);
151 static int		targcamstatus(cam_status status);
152 static size_t		targccblen(xpt_opcode func_code);
153 
154 static struct periph_driver targdriver =
155 {
156 	targinit, "targ",
157 	TAILQ_HEAD_INITIALIZER(targdriver.units), /* generation */ 0
158 };
159 PERIPHDRIVER_DECLARE(targ, targdriver);
160 
161 static MALLOC_DEFINE(M_TARG, "TARG", "TARG data");
162 
163 /* Create softc and initialize it. Only one proc can open each targ device. */
164 static int
165 targopen(struct cdev *dev, int flags, int fmt, struct thread *td)
166 {
167 	struct targ_softc *softc;
168 
169 	if (dev->si_drv1 != 0) {
170 		return (EBUSY);
171 	}
172 
173 	/* Mark device busy before any potentially blocking operations */
174 	dev->si_drv1 = (void *)~0;
175 
176 	/* Create the targ device, allocate its softc, initialize it */
177 	if ((dev->si_flags & SI_NAMED) == 0) {
178 		make_dev(&targ_cdevsw, minor(dev), UID_ROOT, GID_WHEEL, 0600,
179 			 "targ%d", dev2unit(dev));
180 	}
181 	MALLOC(softc, struct targ_softc *, sizeof(*softc), M_TARG,
182 	       M_WAITOK | M_ZERO);
183 	dev->si_drv1 = softc;
184 	softc->state = TARG_STATE_OPENED;
185 	softc->periph = NULL;
186 	softc->path = NULL;
187 
188 	TAILQ_INIT(&softc->pending_ccb_queue);
189 	TAILQ_INIT(&softc->work_queue);
190 	TAILQ_INIT(&softc->abort_queue);
191 	TAILQ_INIT(&softc->user_ccb_queue);
192 	knlist_init(&softc->read_select.si_note, NULL, NULL, NULL, NULL);
193 
194 	return (0);
195 }
196 
197 /* Disable LUN if enabled and teardown softc */
198 static int
199 targclose(struct cdev *dev, int flag, int fmt, struct thread *td)
200 {
201 	struct targ_softc     *softc;
202 	int    error;
203 
204 	softc = (struct targ_softc *)dev->si_drv1;
205 	error = targdisable(softc);
206 	if (error == CAM_REQ_CMP) {
207 		dev->si_drv1 = 0;
208 		if (softc->periph != NULL) {
209 			cam_periph_invalidate(softc->periph);
210 			softc->periph = NULL;
211 		}
212 		destroy_dev(dev);
213 		FREE(softc, M_TARG);
214 	}
215 	return (error);
216 }
217 
218 /* Enable/disable LUNs, set debugging level */
219 static int
220 targioctl(struct cdev *dev, u_long cmd, caddr_t addr, int flag, struct thread *td)
221 {
222 	struct targ_softc *softc;
223 	cam_status	   status;
224 
225 	softc = (struct targ_softc *)dev->si_drv1;
226 
227 	switch (cmd) {
228 	case TARGIOCENABLE:
229 	{
230 		struct ioc_enable_lun	*new_lun;
231 		struct cam_path		*path;
232 
233 		new_lun = (struct ioc_enable_lun *)addr;
234 		status = xpt_create_path(&path, /*periph*/NULL,
235 					 new_lun->path_id,
236 					 new_lun->target_id,
237 					 new_lun->lun_id);
238 		if (status != CAM_REQ_CMP) {
239 			printf("Couldn't create path, status %#x\n", status);
240 			break;
241 		}
242 		status = targenable(softc, path, new_lun->grp6_len,
243 				    new_lun->grp7_len);
244 		xpt_free_path(path);
245 		break;
246 	}
247 	case TARGIOCDISABLE:
248 		status = targdisable(softc);
249 		break;
250 	case TARGIOCDEBUG:
251 	{
252 #ifdef	CAMDEBUG
253 		struct ccb_debug cdbg;
254 
255 		bzero(&cdbg, sizeof cdbg);
256 		if (*((int *)addr) != 0)
257 			cdbg.flags = CAM_DEBUG_PERIPH;
258 		else
259 			cdbg.flags = CAM_DEBUG_NONE;
260 		xpt_setup_ccb(&cdbg.ccb_h, softc->path, /*priority*/0);
261 		cdbg.ccb_h.func_code = XPT_DEBUG;
262 		cdbg.ccb_h.cbfcnp = targdone;
263 
264 		/* If no periph available, disallow debugging changes */
265 		if ((softc->state & TARG_STATE_LUN_ENABLED) == 0) {
266 			status = CAM_DEV_NOT_THERE;
267 			break;
268 		}
269 		xpt_action((union ccb *)&cdbg);
270 		status = cdbg.ccb_h.status & CAM_STATUS_MASK;
271 #else
272 		status = CAM_FUNC_NOTAVAIL;
273 #endif
274 		break;
275 	}
276 	default:
277 		status = CAM_PROVIDE_FAIL;
278 		break;
279 	}
280 
281 	return (targcamstatus(status));
282 }
283 
284 /* Writes are always ready, reads wait for user_ccb_queue or abort_queue */
285 static int
286 targpoll(struct cdev *dev, int poll_events, struct thread *td)
287 {
288 	struct targ_softc *softc;
289 	int	revents;
290 
291 	softc = (struct targ_softc *)dev->si_drv1;
292 
293 	/* Poll for write() is always ok. */
294 	revents = poll_events & (POLLOUT | POLLWRNORM);
295 	if ((poll_events & (POLLIN | POLLRDNORM)) != 0) {
296 		/* Poll for read() depends on user and abort queues. */
297 		if (!TAILQ_EMPTY(&softc->user_ccb_queue) ||
298 		    !TAILQ_EMPTY(&softc->abort_queue)) {
299 			revents |= poll_events & (POLLIN | POLLRDNORM);
300 		}
301 		/* Only sleep if the user didn't poll for write. */
302 		if (revents == 0)
303 			selrecord(td, &softc->read_select);
304 	}
305 
306 	return (revents);
307 }
308 
309 static int
310 targkqfilter(struct cdev *dev, struct knote *kn)
311 {
312 	struct  targ_softc *softc;
313 
314 	softc = (struct targ_softc *)dev->si_drv1;
315 	kn->kn_hook = (caddr_t)softc;
316 	kn->kn_fop = &targread_filtops;
317 	knlist_add(&softc->read_select.si_note, kn, 0);
318 	return (0);
319 }
320 
321 static void
322 targreadfiltdetach(struct knote *kn)
323 {
324 	struct  targ_softc *softc;
325 
326 	softc = (struct targ_softc *)kn->kn_hook;
327 	knlist_remove(&softc->read_select.si_note, kn, 0);
328 }
329 
330 /* Notify the user's kqueue when the user queue or abort queue gets a CCB */
331 static int
332 targreadfilt(struct knote *kn, long hint)
333 {
334 	struct targ_softc *softc;
335 	int	retval;
336 
337 	softc = (struct targ_softc *)kn->kn_hook;
338 	retval = !TAILQ_EMPTY(&softc->user_ccb_queue) ||
339 		 !TAILQ_EMPTY(&softc->abort_queue);
340 	return (retval);
341 }
342 
343 /* Send the HBA the enable/disable message */
344 static cam_status
345 targendislun(struct cam_path *path, int enable, int grp6_len, int grp7_len)
346 {
347 	struct ccb_en_lun en_ccb;
348 	cam_status	  status;
349 
350 	/* Tell the lun to begin answering selects */
351 	xpt_setup_ccb(&en_ccb.ccb_h, path, /*priority*/1);
352 	en_ccb.ccb_h.func_code = XPT_EN_LUN;
353 	/* Don't need support for any vendor specific commands */
354 	en_ccb.grp6_len = grp6_len;
355 	en_ccb.grp7_len = grp7_len;
356 	en_ccb.enable = enable ? 1 : 0;
357 	xpt_action((union ccb *)&en_ccb);
358 	status = en_ccb.ccb_h.status & CAM_STATUS_MASK;
359 	if (status != CAM_REQ_CMP) {
360 		xpt_print(path, "%sable lun CCB rejected, status %#x\n",
361 		    enable ? "en" : "dis", status);
362 	}
363 	return (status);
364 }
365 
366 /* Enable target mode on a LUN, given its path */
367 static cam_status
368 targenable(struct targ_softc *softc, struct cam_path *path, int grp6_len,
369 	   int grp7_len)
370 {
371 	struct cam_periph *periph;
372 	struct ccb_pathinq cpi;
373 	cam_status	   status;
374 
375 	if ((softc->state & TARG_STATE_LUN_ENABLED) != 0)
376 		return (CAM_LUN_ALRDY_ENA);
377 
378 	/* Make sure SIM supports target mode */
379 	xpt_setup_ccb(&cpi.ccb_h, path, /*priority*/1);
380 	cpi.ccb_h.func_code = XPT_PATH_INQ;
381 	xpt_action((union ccb *)&cpi);
382 	status = cpi.ccb_h.status & CAM_STATUS_MASK;
383 	if (status != CAM_REQ_CMP) {
384 		printf("pathinq failed, status %#x\n", status);
385 		goto enable_fail;
386 	}
387 	if ((cpi.target_sprt & PIT_PROCESSOR) == 0) {
388 		printf("controller does not support target mode\n");
389 		status = CAM_FUNC_NOTAVAIL;
390 		goto enable_fail;
391 	}
392 
393 	/* Destroy any periph on our path if it is disabled */
394 	periph = cam_periph_find(path, "targ");
395 	if (periph != NULL) {
396 		struct targ_softc *del_softc;
397 
398 		del_softc = (struct targ_softc *)periph->softc;
399 		if ((del_softc->state & TARG_STATE_LUN_ENABLED) == 0) {
400 			cam_periph_invalidate(del_softc->periph);
401 			del_softc->periph = NULL;
402 		} else {
403 			printf("Requested path still in use by targ%d\n",
404 			       periph->unit_number);
405 			status = CAM_LUN_ALRDY_ENA;
406 			goto enable_fail;
407 		}
408 	}
409 
410 	/* Create a periph instance attached to this path */
411 	status = cam_periph_alloc(targctor, NULL, targdtor, targstart,
412 			"targ", CAM_PERIPH_BIO, path, targasync, 0, softc);
413 	if (status != CAM_REQ_CMP) {
414 		printf("cam_periph_alloc failed, status %#x\n", status);
415 		goto enable_fail;
416 	}
417 
418 	/* Ensure that the periph now exists. */
419 	if (cam_periph_find(path, "targ") == NULL) {
420 		panic("targenable: succeeded but no periph?");
421 		/* NOTREACHED */
422 	}
423 
424 	/* Send the enable lun message */
425 	status = targendislun(path, /*enable*/1, grp6_len, grp7_len);
426 	if (status != CAM_REQ_CMP) {
427 		printf("enable lun failed, status %#x\n", status);
428 		goto enable_fail;
429 	}
430 	softc->state |= TARG_STATE_LUN_ENABLED;
431 
432 enable_fail:
433 	return (status);
434 }
435 
436 /* Disable this softc's target instance if enabled */
437 static cam_status
438 targdisable(struct targ_softc *softc)
439 {
440 	cam_status status;
441 
442 	if ((softc->state & TARG_STATE_LUN_ENABLED) == 0)
443 		return (CAM_REQ_CMP);
444 
445 	CAM_DEBUG(softc->path, CAM_DEBUG_PERIPH, ("targdisable\n"));
446 
447 	/* Abort any ccbs pending on the controller */
448 	abort_all_pending(softc);
449 
450 	/* Disable this lun */
451 	status = targendislun(softc->path, /*enable*/0,
452 			      /*grp6_len*/0, /*grp7_len*/0);
453 	if (status == CAM_REQ_CMP)
454 		softc->state &= ~TARG_STATE_LUN_ENABLED;
455 	else
456 		printf("Disable lun failed, status %#x\n", status);
457 
458 	return (status);
459 }
460 
461 /* Initialize a periph (called from cam_periph_alloc) */
462 static cam_status
463 targctor(struct cam_periph *periph, void *arg)
464 {
465 	struct targ_softc *softc;
466 
467 	/* Store pointer to softc for periph-driven routines */
468 	softc = (struct targ_softc *)arg;
469 	periph->softc = softc;
470 	softc->periph = periph;
471 	softc->path = periph->path;
472 	return (CAM_REQ_CMP);
473 }
474 
475 static void
476 targdtor(struct cam_periph *periph)
477 {
478 	struct targ_softc     *softc;
479 	struct ccb_hdr	      *ccb_h;
480 	struct targ_cmd_descr *descr;
481 
482 	softc = (struct targ_softc *)periph->softc;
483 
484 	/*
485 	 * targdisable() aborts CCBs back to the user and leaves them
486 	 * on user_ccb_queue and abort_queue in case the user is still
487 	 * interested in them.  We free them now.
488 	 */
489 	while ((ccb_h = TAILQ_FIRST(&softc->user_ccb_queue)) != NULL) {
490 		TAILQ_REMOVE(&softc->user_ccb_queue, ccb_h, periph_links.tqe);
491 		targfreeccb(softc, (union ccb *)ccb_h);
492 	}
493 	while ((descr = TAILQ_FIRST(&softc->abort_queue)) != NULL) {
494 		TAILQ_REMOVE(&softc->abort_queue, descr, tqe);
495 		FREE(descr, M_TARG);
496 	}
497 
498 	softc->periph = NULL;
499 	softc->path = NULL;
500 	periph->softc = NULL;
501 }
502 
503 /* Receive CCBs from user mode proc and send them to the HBA */
504 static int
505 targwrite(struct cdev *dev, struct uio *uio, int ioflag)
506 {
507 	union ccb *user_ccb;
508 	struct targ_softc *softc;
509 	struct targ_cmd_descr *descr;
510 	int write_len, error;
511 	int func_code, priority;
512 
513 	softc = (struct targ_softc *)dev->si_drv1;
514 	write_len = error = 0;
515 	CAM_DEBUG(softc->path, CAM_DEBUG_PERIPH,
516 		  ("write - uio_resid %d\n", uio->uio_resid));
517 	while (uio->uio_resid >= sizeof(user_ccb) && error == 0) {
518 		union ccb *ccb;
519 
520 		error = uiomove((caddr_t)&user_ccb, sizeof(user_ccb), uio);
521 		if (error != 0) {
522 			CAM_DEBUG(softc->path, CAM_DEBUG_PERIPH,
523 				  ("write - uiomove failed (%d)\n", error));
524 			break;
525 		}
526 		priority = fuword32(&user_ccb->ccb_h.pinfo.priority);
527 		if (priority == -1) {
528 			error = EINVAL;
529 			break;
530 		}
531 		func_code = fuword32(&user_ccb->ccb_h.func_code);
532 		switch (func_code) {
533 		case XPT_ACCEPT_TARGET_IO:
534 		case XPT_IMMED_NOTIFY:
535 			ccb = targgetccb(softc, func_code, priority);
536 			descr = (struct targ_cmd_descr *)ccb->ccb_h.targ_descr;
537 			descr->user_ccb = user_ccb;
538 			descr->func_code = func_code;
539 			CAM_DEBUG(softc->path, CAM_DEBUG_PERIPH,
540 				  ("Sent ATIO/INOT (%p)\n", user_ccb));
541 			xpt_action(ccb);
542 			TAILQ_INSERT_TAIL(&softc->pending_ccb_queue,
543 					  &ccb->ccb_h,
544 					  periph_links.tqe);
545 			break;
546 		default:
547 			if ((func_code & XPT_FC_QUEUED) != 0) {
548 				CAM_DEBUG(softc->path, CAM_DEBUG_PERIPH,
549 					  ("Sending queued ccb %#x (%p)\n",
550 					  func_code, user_ccb));
551 				descr = targgetdescr(softc);
552 				descr->user_ccb = user_ccb;
553 				descr->priority = priority;
554 				descr->func_code = func_code;
555 				TAILQ_INSERT_TAIL(&softc->work_queue,
556 						  descr, tqe);
557 				xpt_schedule(softc->periph, priority);
558 			} else {
559 				CAM_DEBUG(softc->path, CAM_DEBUG_PERIPH,
560 					  ("Sending inline ccb %#x (%p)\n",
561 					  func_code, user_ccb));
562 				ccb = targgetccb(softc, func_code, priority);
563 				descr = (struct targ_cmd_descr *)
564 					 ccb->ccb_h.targ_descr;
565 				descr->user_ccb = user_ccb;
566 				descr->priority = priority;
567 				descr->func_code = func_code;
568 				if (targusermerge(softc, descr, ccb) != EFAULT)
569 					targsendccb(softc, ccb, descr);
570 				targreturnccb(softc, ccb);
571 			}
572 			break;
573 		}
574 		write_len += sizeof(user_ccb);
575 	}
576 
577 	/*
578 	 * If we've successfully taken in some amount of
579 	 * data, return success for that data first.  If
580 	 * an error is persistent, it will be reported
581 	 * on the next write.
582 	 */
583 	if (error != 0 && write_len == 0)
584 		return (error);
585 	if (write_len == 0 && uio->uio_resid != 0)
586 		return (ENOSPC);
587 	return (0);
588 }
589 
590 /* Process requests (descrs) via the periph-supplied CCBs */
591 static void
592 targstart(struct cam_periph *periph, union ccb *start_ccb)
593 {
594 	struct targ_softc *softc;
595 	struct targ_cmd_descr *descr, *next_descr;
596 	int error;
597 
598 	softc = (struct targ_softc *)periph->softc;
599 	CAM_DEBUG(softc->path, CAM_DEBUG_PERIPH, ("targstart %p\n", start_ccb));
600 
601 	descr = TAILQ_FIRST(&softc->work_queue);
602 	if (descr == NULL) {
603 		xpt_release_ccb(start_ccb);
604 	} else {
605 		TAILQ_REMOVE(&softc->work_queue, descr, tqe);
606 		next_descr = TAILQ_FIRST(&softc->work_queue);
607 
608 		/* Initiate a transaction using the descr and supplied CCB */
609 		error = targusermerge(softc, descr, start_ccb);
610 		if (error == 0)
611 			error = targsendccb(softc, start_ccb, descr);
612 		if (error != 0) {
613 			xpt_print(periph->path,
614 			    "targsendccb failed, err %d\n", error);
615 			xpt_release_ccb(start_ccb);
616 			suword(&descr->user_ccb->ccb_h.status,
617 			       CAM_REQ_CMP_ERR);
618 			TAILQ_INSERT_TAIL(&softc->abort_queue, descr, tqe);
619 			notify_user(softc);
620 		}
621 
622 		/* If we have more work to do, stay scheduled */
623 		if (next_descr != NULL)
624 			xpt_schedule(periph, next_descr->priority);
625 	}
626 }
627 
628 static int
629 targusermerge(struct targ_softc *softc, struct targ_cmd_descr *descr,
630 	      union ccb *ccb)
631 {
632 	struct ccb_hdr *u_ccbh, *k_ccbh;
633 	size_t ccb_len;
634 	int error;
635 
636 	u_ccbh = &descr->user_ccb->ccb_h;
637 	k_ccbh = &ccb->ccb_h;
638 
639 	/*
640 	 * There are some fields in the CCB header that need to be
641 	 * preserved, the rest we get from the user ccb. (See xpt_merge_ccb)
642 	 */
643 	xpt_setup_ccb(k_ccbh, softc->path, descr->priority);
644 	k_ccbh->retry_count = fuword32(&u_ccbh->retry_count);
645 	k_ccbh->func_code = descr->func_code;
646 	k_ccbh->flags = fuword32(&u_ccbh->flags);
647 	k_ccbh->timeout = fuword32(&u_ccbh->timeout);
648 	ccb_len = targccblen(k_ccbh->func_code) - sizeof(struct ccb_hdr);
649 	error = copyin(u_ccbh + 1, k_ccbh + 1, ccb_len);
650 	if (error != 0) {
651 		k_ccbh->status = CAM_REQ_CMP_ERR;
652 		return (error);
653 	}
654 
655 	/* Translate usermode abort_ccb pointer to its kernel counterpart */
656 	if (k_ccbh->func_code == XPT_ABORT) {
657 		struct ccb_abort *cab;
658 		struct ccb_hdr *ccb_h;
659 
660 		cab = (struct ccb_abort *)ccb;
661 		TAILQ_FOREACH(ccb_h, &softc->pending_ccb_queue,
662 		    periph_links.tqe) {
663 			struct targ_cmd_descr *ab_descr;
664 
665 			ab_descr = (struct targ_cmd_descr *)ccb_h->targ_descr;
666 			if (ab_descr->user_ccb == cab->abort_ccb) {
667 				CAM_DEBUG(softc->path, CAM_DEBUG_PERIPH,
668 					  ("Changing abort for %p to %p\n",
669 					  cab->abort_ccb, ccb_h));
670 				cab->abort_ccb = (union ccb *)ccb_h;
671 				break;
672 			}
673 		}
674 		/* CCB not found, set appropriate status */
675 		if (ccb_h == NULL) {
676 			k_ccbh->status = CAM_PATH_INVALID;
677 			error = ESRCH;
678 		}
679 	}
680 
681 	return (error);
682 }
683 
684 /* Build and send a kernel CCB formed from descr->user_ccb */
685 static int
686 targsendccb(struct targ_softc *softc, union ccb *ccb,
687 	    struct targ_cmd_descr *descr)
688 {
689 	struct cam_periph_map_info *mapinfo;
690 	struct ccb_hdr *ccb_h;
691 	int error;
692 
693 	ccb_h = &ccb->ccb_h;
694 	mapinfo = &descr->mapinfo;
695 	mapinfo->num_bufs_used = 0;
696 
697 	/*
698 	 * There's no way for the user to have a completion
699 	 * function, so we put our own completion function in here.
700 	 * We also stash in a reference to our descriptor so targreturnccb()
701 	 * can find our mapping info.
702 	 */
703 	ccb_h->cbfcnp = targdone;
704 	ccb_h->targ_descr = descr;
705 
706 	/*
707 	 * We only attempt to map the user memory into kernel space
708 	 * if they haven't passed in a physical memory pointer,
709 	 * and if there is actually an I/O operation to perform.
710 	 * Right now cam_periph_mapmem() only supports SCSI and device
711 	 * match CCBs.  For the SCSI CCBs, we only pass the CCB in if
712 	 * there's actually data to map.  cam_periph_mapmem() will do the
713 	 * right thing, even if there isn't data to map, but since CCBs
714 	 * without data are a reasonably common occurance (e.g. test unit
715 	 * ready), it will save a few cycles if we check for it here.
716 	 */
717 	if (((ccb_h->flags & CAM_DATA_PHYS) == 0)
718 	 && (((ccb_h->func_code == XPT_CONT_TARGET_IO)
719 	    && ((ccb_h->flags & CAM_DIR_MASK) != CAM_DIR_NONE))
720 	  || (ccb_h->func_code == XPT_DEV_MATCH))) {
721 
722 		error = cam_periph_mapmem(ccb, mapinfo);
723 
724 		/*
725 		 * cam_periph_mapmem returned an error, we can't continue.
726 		 * Return the error to the user.
727 		 */
728 		if (error) {
729 			ccb_h->status = CAM_REQ_CMP_ERR;
730 			mapinfo->num_bufs_used = 0;
731 			return (error);
732 		}
733 	}
734 
735 	/*
736 	 * Once queued on the pending CCB list, this CCB will be protected
737 	 * by our error recovery handler.
738 	 */
739 	CAM_DEBUG(softc->path, CAM_DEBUG_PERIPH, ("sendccb %p\n", ccb));
740 	if (XPT_FC_IS_QUEUED(ccb)) {
741 		TAILQ_INSERT_TAIL(&softc->pending_ccb_queue, ccb_h,
742 				  periph_links.tqe);
743 	}
744 	xpt_action(ccb);
745 
746 	return (0);
747 }
748 
749 /* Completion routine for CCBs (called at splsoftcam) */
750 static void
751 targdone(struct cam_periph *periph, union ccb *done_ccb)
752 {
753 	struct targ_softc *softc;
754 	cam_status status;
755 
756 	CAM_DEBUG(periph->path, CAM_DEBUG_PERIPH, ("targdone %p\n", done_ccb));
757 	softc = (struct targ_softc *)periph->softc;
758 	TAILQ_REMOVE(&softc->pending_ccb_queue, &done_ccb->ccb_h,
759 		     periph_links.tqe);
760 	status = done_ccb->ccb_h.status & CAM_STATUS_MASK;
761 
762 	/* If we're no longer enabled, throw away CCB */
763 	if ((softc->state & TARG_STATE_LUN_ENABLED) == 0) {
764 		targfreeccb(softc, done_ccb);
765 		return;
766 	}
767 	/* abort_all_pending() waits for pending queue to be empty */
768 	if (TAILQ_EMPTY(&softc->pending_ccb_queue))
769 		wakeup(&softc->pending_ccb_queue);
770 
771 	switch (done_ccb->ccb_h.func_code) {
772 	/* All FC_*_QUEUED CCBs go back to userland */
773 	case XPT_IMMED_NOTIFY:
774 	case XPT_ACCEPT_TARGET_IO:
775 	case XPT_CONT_TARGET_IO:
776 		TAILQ_INSERT_TAIL(&softc->user_ccb_queue, &done_ccb->ccb_h,
777 				  periph_links.tqe);
778 		notify_user(softc);
779 		break;
780 	default:
781 		panic("targdone: impossible xpt opcode %#x",
782 		      done_ccb->ccb_h.func_code);
783 		/* NOTREACHED */
784 	}
785 }
786 
787 /* Return CCBs to the user from the user queue and abort queue */
788 static int
789 targread(struct cdev *dev, struct uio *uio, int ioflag)
790 {
791 	struct descr_queue	*abort_queue;
792 	struct targ_cmd_descr	*user_descr;
793 	struct targ_softc	*softc;
794 	struct ccb_queue  *user_queue;
795 	struct ccb_hdr	  *ccb_h;
796 	union  ccb	  *user_ccb;
797 	int		   read_len, error;
798 
799 	mtx_lock(&Giant);
800 
801 	error = 0;
802 	read_len = 0;
803 	softc = (struct targ_softc *)dev->si_drv1;
804 	user_queue = &softc->user_ccb_queue;
805 	abort_queue = &softc->abort_queue;
806 	CAM_DEBUG(softc->path, CAM_DEBUG_PERIPH, ("targread\n"));
807 
808 	/* If no data is available, wait or return immediately */
809 	ccb_h = TAILQ_FIRST(user_queue);
810 	user_descr = TAILQ_FIRST(abort_queue);
811 	while (ccb_h == NULL && user_descr == NULL) {
812 		if ((ioflag & IO_NDELAY) == 0) {
813 			error = msleep(user_queue, NULL,
814 				       PRIBIO | PCATCH, "targrd", 0);
815 			ccb_h = TAILQ_FIRST(user_queue);
816 			user_descr = TAILQ_FIRST(abort_queue);
817 			if (error != 0) {
818 				if (error == ERESTART) {
819 					continue;
820 				} else {
821 					goto read_fail;
822 				}
823 			}
824 		} else {
825 			mtx_unlock(&Giant);
826 			return (EAGAIN);
827 		}
828 	}
829 
830 	/* Data is available so fill the user's buffer */
831 	while (ccb_h != NULL) {
832 		struct targ_cmd_descr *descr;
833 
834 		if (uio->uio_resid < sizeof(user_ccb))
835 			break;
836 		TAILQ_REMOVE(user_queue, ccb_h, periph_links.tqe);
837 		descr = (struct targ_cmd_descr *)ccb_h->targ_descr;
838 		user_ccb = descr->user_ccb;
839 		CAM_DEBUG(softc->path, CAM_DEBUG_PERIPH,
840 			  ("targread ccb %p (%p)\n", ccb_h, user_ccb));
841 		error = targreturnccb(softc, (union ccb *)ccb_h);
842 		if (error != 0)
843 			goto read_fail;
844 		error = uiomove((caddr_t)&user_ccb, sizeof(user_ccb), uio);
845 		if (error != 0)
846 			goto read_fail;
847 		read_len += sizeof(user_ccb);
848 
849 		ccb_h = TAILQ_FIRST(user_queue);
850 	}
851 
852 	/* Flush out any aborted descriptors */
853 	while (user_descr != NULL) {
854 		if (uio->uio_resid < sizeof(user_ccb))
855 			break;
856 		TAILQ_REMOVE(abort_queue, user_descr, tqe);
857 		user_ccb = user_descr->user_ccb;
858 		CAM_DEBUG(softc->path, CAM_DEBUG_PERIPH,
859 			  ("targread aborted descr %p (%p)\n",
860 			  user_descr, user_ccb));
861 		suword(&user_ccb->ccb_h.status, CAM_REQ_ABORTED);
862 		error = uiomove((caddr_t)&user_ccb, sizeof(user_ccb), uio);
863 		if (error != 0)
864 			goto read_fail;
865 		read_len += sizeof(user_ccb);
866 
867 		user_descr = TAILQ_FIRST(abort_queue);
868 	}
869 
870 	/*
871 	 * If we've successfully read some amount of data, don't report an
872 	 * error.  If the error is persistent, it will be reported on the
873 	 * next read().
874 	 */
875 	if (read_len == 0 && uio->uio_resid != 0)
876 		error = ENOSPC;
877 
878 read_fail:
879 	mtx_unlock(&Giant);
880 	return (error);
881 }
882 
883 /* Copy completed ccb back to the user */
884 static int
885 targreturnccb(struct targ_softc *softc, union ccb *ccb)
886 {
887 	struct targ_cmd_descr *descr;
888 	struct ccb_hdr *u_ccbh;
889 	size_t ccb_len;
890 	int error;
891 
892 	CAM_DEBUG(softc->path, CAM_DEBUG_PERIPH, ("targreturnccb %p\n", ccb));
893 	descr = (struct targ_cmd_descr *)ccb->ccb_h.targ_descr;
894 	u_ccbh = &descr->user_ccb->ccb_h;
895 
896 	/* Copy out the central portion of the ccb_hdr */
897 	copyout(&ccb->ccb_h.retry_count, &u_ccbh->retry_count,
898 		offsetof(struct ccb_hdr, periph_priv) -
899 		offsetof(struct ccb_hdr, retry_count));
900 
901 	/* Copy out the rest of the ccb (after the ccb_hdr) */
902 	ccb_len = targccblen(ccb->ccb_h.func_code) - sizeof(struct ccb_hdr);
903 	if (descr->mapinfo.num_bufs_used != 0)
904 		cam_periph_unmapmem(ccb, &descr->mapinfo);
905 	error = copyout(&ccb->ccb_h + 1, u_ccbh + 1, ccb_len);
906 	if (error != 0) {
907 		xpt_print(softc->path,
908 		    "targreturnccb - CCB copyout failed (%d)\n", error);
909 	}
910 	/* Free CCB or send back to devq. */
911 	targfreeccb(softc, ccb);
912 
913 	return (error);
914 }
915 
916 static union ccb *
917 targgetccb(struct targ_softc *softc, xpt_opcode type, int priority)
918 {
919 	union ccb *ccb;
920 	int ccb_len;
921 
922 	ccb_len = targccblen(type);
923 	MALLOC(ccb, union ccb *, ccb_len, M_TARG, M_WAITOK);
924 	CAM_DEBUG(softc->path, CAM_DEBUG_PERIPH, ("getccb %p\n", ccb));
925 
926 	xpt_setup_ccb(&ccb->ccb_h, softc->path, priority);
927 	ccb->ccb_h.func_code = type;
928 	ccb->ccb_h.cbfcnp = targdone;
929 	ccb->ccb_h.targ_descr = targgetdescr(softc);
930 	return (ccb);
931 }
932 
933 static void
934 targfreeccb(struct targ_softc *softc, union ccb *ccb)
935 {
936 	CAM_DEBUG_PRINT(CAM_DEBUG_PERIPH, ("targfreeccb descr %p and\n",
937 			ccb->ccb_h.targ_descr));
938 	FREE(ccb->ccb_h.targ_descr, M_TARG);
939 
940 	switch (ccb->ccb_h.func_code) {
941 	case XPT_ACCEPT_TARGET_IO:
942 	case XPT_IMMED_NOTIFY:
943 		CAM_DEBUG_PRINT(CAM_DEBUG_PERIPH, ("freeing ccb %p\n", ccb));
944 		FREE(ccb, M_TARG);
945 		break;
946 	default:
947 		/* Send back CCB if we got it from the periph */
948 		if (XPT_FC_IS_QUEUED(ccb)) {
949 			CAM_DEBUG_PRINT(CAM_DEBUG_PERIPH,
950 					("returning queued ccb %p\n", ccb));
951 			xpt_release_ccb(ccb);
952 		} else {
953 			CAM_DEBUG_PRINT(CAM_DEBUG_PERIPH,
954 					("freeing ccb %p\n", ccb));
955 			FREE(ccb, M_TARG);
956 		}
957 		break;
958 	}
959 }
960 
961 static struct targ_cmd_descr *
962 targgetdescr(struct targ_softc *softc)
963 {
964 	struct targ_cmd_descr *descr;
965 
966 	MALLOC(descr, struct targ_cmd_descr *, sizeof(*descr), M_TARG,
967 	       M_WAITOK);
968 	descr->mapinfo.num_bufs_used = 0;
969 	return (descr);
970 }
971 
972 static void
973 targinit(void)
974 {
975 	EVENTHANDLER_REGISTER(dev_clone, targclone, 0, 1000);
976 }
977 
978 static void
979 targclone(void *arg, struct ucred *cred, char *name, int namelen,
980     struct cdev **dev)
981 {
982 	int u;
983 
984 	if (*dev != NULL)
985 		return;
986 	if (dev_stdclone(name, NULL, "targ", &u) != 1)
987 		return;
988 	*dev = make_dev(&targ_cdevsw, unit2minor(u), UID_ROOT, GID_WHEEL,
989 			0600, "targ%d", u);
990 	dev_ref(*dev);
991 	(*dev)->si_flags |= SI_CHEAPCLONE;
992 }
993 
994 static void
995 targasync(void *callback_arg, u_int32_t code, struct cam_path *path, void *arg)
996 {
997 	/* All events are handled in usermode by INOTs */
998 	panic("targasync() called, should be an INOT instead");
999 }
1000 
1001 /* Cancel all pending requests and CCBs awaiting work. */
1002 static void
1003 abort_all_pending(struct targ_softc *softc)
1004 {
1005 	struct targ_cmd_descr   *descr;
1006 	struct ccb_abort	 cab;
1007 	struct ccb_hdr		*ccb_h;
1008 
1009 	CAM_DEBUG(softc->path, CAM_DEBUG_PERIPH, ("abort_all_pending\n"));
1010 
1011 	/* First abort the descriptors awaiting resources */
1012 	while ((descr = TAILQ_FIRST(&softc->work_queue)) != NULL) {
1013 		CAM_DEBUG(softc->path, CAM_DEBUG_PERIPH,
1014 			  ("Aborting descr from workq %p\n", descr));
1015 		TAILQ_REMOVE(&softc->work_queue, descr, tqe);
1016 		TAILQ_INSERT_TAIL(&softc->abort_queue, descr, tqe);
1017 	}
1018 
1019 	/*
1020 	 * Then abort all pending CCBs.
1021 	 * targdone() will return the aborted CCB via user_ccb_queue
1022 	 */
1023 	xpt_setup_ccb(&cab.ccb_h, softc->path, /*priority*/0);
1024 	cab.ccb_h.func_code = XPT_ABORT;
1025 	cab.ccb_h.status = CAM_REQ_CMP_ERR;
1026 	TAILQ_FOREACH(ccb_h, &softc->pending_ccb_queue, periph_links.tqe) {
1027 		CAM_DEBUG(softc->path, CAM_DEBUG_PERIPH,
1028 			  ("Aborting pending CCB %p\n", ccb_h));
1029 		cab.abort_ccb = (union ccb *)ccb_h;
1030 		xpt_action((union ccb *)&cab);
1031 		if (cab.ccb_h.status != CAM_REQ_CMP) {
1032 			xpt_print(cab.ccb_h.path,
1033 			    "Unable to abort CCB, status %#x\n",
1034 			    cab.ccb_h.status);
1035 		}
1036 	}
1037 
1038 	/* If we aborted at least one pending CCB ok, wait for it. */
1039 	if (cab.ccb_h.status == CAM_REQ_CMP) {
1040 		msleep(&softc->pending_ccb_queue, NULL,
1041 		       PRIBIO | PCATCH, "tgabrt", 0);
1042 	}
1043 
1044 	/* If we aborted anything from the work queue, wakeup user. */
1045 	if (!TAILQ_EMPTY(&softc->user_ccb_queue)
1046 	 || !TAILQ_EMPTY(&softc->abort_queue))
1047 		notify_user(softc);
1048 }
1049 
1050 /* Notify the user that data is ready */
1051 static void
1052 notify_user(struct targ_softc *softc)
1053 {
1054 	/*
1055 	 * Notify users sleeping via poll(), kqueue(), and
1056 	 * blocking read().
1057 	 */
1058 	selwakeuppri(&softc->read_select, PRIBIO);
1059 	KNOTE_UNLOCKED(&softc->read_select.si_note, 0);
1060 	wakeup(&softc->user_ccb_queue);
1061 }
1062 
1063 /* Convert CAM status to errno values */
1064 static int
1065 targcamstatus(cam_status status)
1066 {
1067 	switch (status & CAM_STATUS_MASK) {
1068 	case CAM_REQ_CMP:	/* CCB request completed without error */
1069 		return (0);
1070 	case CAM_REQ_INPROG:	/* CCB request is in progress */
1071 		return (EINPROGRESS);
1072 	case CAM_REQ_CMP_ERR:	/* CCB request completed with an error */
1073 		return (EIO);
1074 	case CAM_PROVIDE_FAIL:	/* Unable to provide requested capability */
1075 		return (ENOTTY);
1076 	case CAM_FUNC_NOTAVAIL:	/* The requested function is not available */
1077 		return (ENOTSUP);
1078 	case CAM_LUN_ALRDY_ENA:	/* LUN is already enabled for target mode */
1079 		return (EADDRINUSE);
1080 	case CAM_PATH_INVALID:	/* Supplied Path ID is invalid */
1081 	case CAM_DEV_NOT_THERE:	/* SCSI Device Not Installed/there */
1082 		return (ENOENT);
1083 	case CAM_REQ_ABORTED:	/* CCB request aborted by the host */
1084 		return (ECANCELED);
1085 	case CAM_CMD_TIMEOUT:	/* Command timeout */
1086 		return (ETIMEDOUT);
1087 	case CAM_REQUEUE_REQ:	/* Requeue to preserve transaction ordering */
1088 		return (EAGAIN);
1089 	case CAM_REQ_INVALID:	/* CCB request was invalid */
1090 		return (EINVAL);
1091 	case CAM_RESRC_UNAVAIL:	/* Resource Unavailable */
1092 		return (ENOMEM);
1093 	case CAM_BUSY:		/* CAM subsytem is busy */
1094 	case CAM_UA_ABORT:	/* Unable to abort CCB request */
1095 		return (EBUSY);
1096 	default:
1097 		return (ENXIO);
1098 	}
1099 }
1100 
1101 static size_t
1102 targccblen(xpt_opcode func_code)
1103 {
1104 	int len;
1105 
1106 	/* Codes we expect to see as a target */
1107 	switch (func_code) {
1108 	case XPT_CONT_TARGET_IO:
1109 	case XPT_SCSI_IO:
1110 		len = sizeof(struct ccb_scsiio);
1111 		break;
1112 	case XPT_ACCEPT_TARGET_IO:
1113 		len = sizeof(struct ccb_accept_tio);
1114 		break;
1115 	case XPT_IMMED_NOTIFY:
1116 		len = sizeof(struct ccb_immed_notify);
1117 		break;
1118 	case XPT_REL_SIMQ:
1119 		len = sizeof(struct ccb_relsim);
1120 		break;
1121 	case XPT_PATH_INQ:
1122 		len = sizeof(struct ccb_pathinq);
1123 		break;
1124 	case XPT_DEBUG:
1125 		len = sizeof(struct ccb_debug);
1126 		break;
1127 	case XPT_ABORT:
1128 		len = sizeof(struct ccb_abort);
1129 		break;
1130 	case XPT_EN_LUN:
1131 		len = sizeof(struct ccb_en_lun);
1132 		break;
1133 	default:
1134 		len = sizeof(union ccb);
1135 		break;
1136 	}
1137 
1138 	return (len);
1139 }
1140