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