Change the kernel dev_t, representing a pointer to a specinfo structure,
[dragonfly.git] / sys / bus / cam / scsi / scsi_pass.c
CommitLineData
984263bc
MD
1/*
2 * Copyright (c) 1997, 1998 Justin T. Gibbs.
3 * Copyright (c) 1997, 1998, 1999 Kenneth D. Merry.
4 * All rights reserved.
5 *
6 * Redistribution and use in source and binary forms, with or without
7 * modification, are permitted provided that the following conditions
8 * are met:
9 * 1. Redistributions of source code must retain the above copyright
10 * notice, this list of conditions, and the following disclaimer,
11 * without modification, immediately at the beginning of the file.
12 * 2. The name of the author may not be used to endorse or promote products
13 * derived from this software without specific prior written permission.
14 *
15 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
16 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
17 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
18 * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE FOR
19 * ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
20 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
21 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
22 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
23 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
24 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
25 * SUCH DAMAGE.
26 *
27 * $FreeBSD: src/sys/cam/scsi/scsi_pass.c,v 1.19 2000/01/17 06:27:37 mjacob Exp $
b13267a5 28 * $DragonFly: src/sys/bus/cam/scsi/scsi_pass.c,v 1.18 2006/09/10 01:26:32 dillon Exp $
984263bc
MD
29 */
30
31#include <sys/param.h>
32#include <sys/systm.h>
33#include <sys/kernel.h>
34#include <sys/types.h>
35#include <sys/buf.h>
36#include <sys/malloc.h>
37#include <sys/fcntl.h>
38#include <sys/stat.h>
39#include <sys/conf.h>
40#include <sys/buf.h>
41#include <sys/proc.h>
42#include <sys/errno.h>
43#include <sys/devicestat.h>
3020e3be
MD
44#include <sys/proc.h>
45#include <sys/buf2.h>
4e01b467 46#include <sys/thread2.h>
984263bc 47
1f2de5d4
MD
48#include "../cam.h"
49#include "../cam_ccb.h"
50#include "../cam_extend.h"
51#include "../cam_periph.h"
52#include "../cam_xpt_periph.h"
53#include "../cam_debug.h"
984263bc 54
1f2de5d4
MD
55#include "scsi_all.h"
56#include "scsi_message.h"
57#include "scsi_da.h"
58#include "scsi_pass.h"
984263bc
MD
59
60typedef enum {
61 PASS_FLAG_OPEN = 0x01,
62 PASS_FLAG_LOCKED = 0x02,
63 PASS_FLAG_INVALID = 0x04
64} pass_flags;
65
66typedef enum {
67 PASS_STATE_NORMAL
68} pass_state;
69
70typedef enum {
71 PASS_CCB_BUFFER_IO,
72 PASS_CCB_WAITING
73} pass_ccb_types;
74
75#define ccb_type ppriv_field0
81b5c339 76#define ccb_bio ppriv_ptr1
984263bc
MD
77
78struct pass_softc {
79 pass_state state;
80 pass_flags flags;
81 u_int8_t pd_type;
81b5c339 82 struct bio_queue_head bio_queue;
984263bc
MD
83 union ccb saved_ccb;
84 struct devstat device_stats;
984263bc
MD
85};
86
984263bc
MD
87#define PASS_CDEV_MAJOR 31
88
89static d_open_t passopen;
90static d_close_t passclose;
91static d_ioctl_t passioctl;
92static d_strategy_t passstrategy;
93
94static periph_init_t passinit;
95static periph_ctor_t passregister;
96static periph_oninv_t passoninvalidate;
97static periph_dtor_t passcleanup;
98static periph_start_t passstart;
99static void passasync(void *callback_arg, u_int32_t code,
100 struct cam_path *path, void *arg);
101static void passdone(struct cam_periph *periph,
102 union ccb *done_ccb);
103static int passerror(union ccb *ccb, u_int32_t cam_flags,
104 u_int32_t sense_flags);
105static int passsendccb(struct cam_periph *periph, union ccb *ccb,
106 union ccb *inccb);
107
108static struct periph_driver passdriver =
109{
110 passinit, "pass",
111 TAILQ_HEAD_INITIALIZER(passdriver.units), /* generation */ 0
112};
113
114DATA_SET(periphdriver_set, passdriver);
115
fef8985e
MD
116static struct dev_ops pass_ops = {
117 { "pass", PASS_CDEV_MAJOR, 0 },
118 .d_open = passopen,
119 .d_close = passclose,
120 .d_read = physread,
121 .d_write = physwrite,
122 .d_ioctl = passioctl,
123 .d_strategy = passstrategy,
984263bc
MD
124};
125
126static struct extend_array *passperiphs;
127
128static void
129passinit(void)
130{
131 cam_status status;
132 struct cam_path *path;
133
134 /*
135 * Create our extend array for storing the devices we attach to.
136 */
137 passperiphs = cam_extend_new();
138 if (passperiphs == NULL) {
139 printf("passm: Failed to alloc extend array!\n");
140 return;
141 }
142
143 /*
144 * Install a global async callback. This callback will
145 * receive async callbacks like "new device found".
146 */
147 status = xpt_create_path(&path, /*periph*/NULL, CAM_XPT_PATH_ID,
148 CAM_TARGET_WILDCARD, CAM_LUN_WILDCARD);
149
150 if (status == CAM_REQ_CMP) {
151 struct ccb_setasync csa;
152
153 xpt_setup_ccb(&csa.ccb_h, path, /*priority*/5);
154 csa.ccb_h.func_code = XPT_SASYNC_CB;
155 csa.event_enable = AC_FOUND_DEVICE;
156 csa.callback = passasync;
157 csa.callback_arg = NULL;
158 xpt_action((union ccb *)&csa);
159 status = csa.ccb_h.status;
160 xpt_free_path(path);
161 }
162
163 if (status != CAM_REQ_CMP) {
164 printf("pass: Failed to attach master async callback "
165 "due to status 0x%x!\n", status);
166 }
167
168}
169
170static void
171passoninvalidate(struct cam_periph *periph)
172{
984263bc
MD
173 struct pass_softc *softc;
174 struct buf *q_bp;
81b5c339 175 struct bio *q_bio;
984263bc
MD
176 struct ccb_setasync csa;
177
178 softc = (struct pass_softc *)periph->softc;
179
180 /*
181 * De-register any async callbacks.
182 */
183 xpt_setup_ccb(&csa.ccb_h, periph->path,
184 /* priority */ 5);
185 csa.ccb_h.func_code = XPT_SASYNC_CB;
186 csa.event_enable = 0;
187 csa.callback = passasync;
188 csa.callback_arg = periph;
189 xpt_action((union ccb *)&csa);
190
191 softc->flags |= PASS_FLAG_INVALID;
192
193 /*
4e01b467 194 * We need to be in a critical section here to keep the buffer
984263bc
MD
195 * queue from being modified while we traverse it.
196 */
4e01b467 197 crit_enter();
984263bc
MD
198
199 /*
200 * Return all queued I/O with ENXIO.
201 * XXX Handle any transactions queued to the card
202 * with XPT_ABORT_CCB.
203 */
81b5c339
MD
204 while ((q_bio = bioq_first(&softc->bio_queue)) != NULL){
205 bioq_remove(&softc->bio_queue, q_bio);
206 q_bp = q_bio->bio_buf;
984263bc
MD
207 q_bp->b_resid = q_bp->b_bcount;
208 q_bp->b_error = ENXIO;
209 q_bp->b_flags |= B_ERROR;
81b5c339 210 biodone(q_bio);
984263bc 211 }
4e01b467 212 crit_exit();
984263bc
MD
213
214 if (bootverbose) {
215 xpt_print_path(periph->path);
216 printf("lost device\n");
217 }
218
219}
220
221static void
222passcleanup(struct cam_periph *periph)
223{
224 struct pass_softc *softc;
225
226 softc = (struct pass_softc *)periph->softc;
227
228 devstat_remove_entry(&softc->device_stats);
229
984263bc
MD
230 cam_extend_release(passperiphs, periph->unit_number);
231
232 if (bootverbose) {
233 xpt_print_path(periph->path);
234 printf("removing device entry\n");
235 }
fef8985e 236 dev_ops_remove(&pass_ops, -1, periph->unit_number);
efda3bd0 237 kfree(softc, M_DEVBUF);
984263bc
MD
238}
239
240static void
241passasync(void *callback_arg, u_int32_t code,
242 struct cam_path *path, void *arg)
243{
244 struct cam_periph *periph;
245
246 periph = (struct cam_periph *)callback_arg;
247
248 switch (code) {
249 case AC_FOUND_DEVICE:
250 {
251 struct ccb_getdev *cgd;
252 cam_status status;
253
254 cgd = (struct ccb_getdev *)arg;
255
256 /*
257 * Allocate a peripheral instance for
258 * this device and start the probe
259 * process.
260 */
261 status = cam_periph_alloc(passregister, passoninvalidate,
262 passcleanup, passstart, "pass",
263 CAM_PERIPH_BIO, cgd->ccb_h.path,
264 passasync, AC_FOUND_DEVICE, cgd);
265
266 if (status != CAM_REQ_CMP
267 && status != CAM_REQ_INPROG)
268 printf("passasync: Unable to attach new device "
269 "due to status 0x%x\n", status);
270
271 break;
272 }
273 default:
274 cam_periph_async(periph, code, path, arg);
275 break;
276 }
277}
278
279static cam_status
280passregister(struct cam_periph *periph, void *arg)
281{
282 struct pass_softc *softc;
283 struct ccb_setasync csa;
284 struct ccb_getdev *cgd;
285
286 cgd = (struct ccb_getdev *)arg;
287 if (periph == NULL) {
288 printf("passregister: periph was NULL!!\n");
289 return(CAM_REQ_CMP_ERR);
290 }
291
292 if (cgd == NULL) {
293 printf("passregister: no getdev CCB, can't register device\n");
294 return(CAM_REQ_CMP_ERR);
295 }
296
efda3bd0 297 softc = kmalloc(sizeof(*softc), M_DEVBUF, M_INTWAIT | M_ZERO);
984263bc
MD
298 softc->state = PASS_STATE_NORMAL;
299 softc->pd_type = SID_TYPE(&cgd->inq_data);
81b5c339 300 bioq_init(&softc->bio_queue);
984263bc
MD
301
302 periph->softc = softc;
303
304 cam_extend_set(passperiphs, periph->unit_number, periph);
305 /*
306 * We pass in 0 for a blocksize, since we don't
307 * know what the blocksize of this device is, if
308 * it even has a blocksize.
309 */
310 devstat_add_entry(&softc->device_stats, "pass", periph->unit_number,
311 0, DEVSTAT_NO_BLOCKSIZE | DEVSTAT_NO_ORDERED_TAGS,
312 softc->pd_type |
313 DEVSTAT_TYPE_IF_SCSI |
314 DEVSTAT_TYPE_PASS,
315 DEVSTAT_PRIORITY_PASS);
316
317 /* Register the device */
fef8985e
MD
318 dev_ops_add(&pass_ops, -1, periph->unit_number);
319 make_dev(&pass_ops, periph->unit_number, UID_ROOT,
e4c9c0c8
MD
320 GID_OPERATOR, 0600, "%s%d", periph->periph_name,
321 periph->unit_number);
984263bc
MD
322
323 /*
324 * Add an async callback so that we get
325 * notified if this device goes away.
326 */
327 xpt_setup_ccb(&csa.ccb_h, periph->path, /* priority */ 5);
328 csa.ccb_h.func_code = XPT_SASYNC_CB;
329 csa.event_enable = AC_LOST_DEVICE;
330 csa.callback = passasync;
331 csa.callback_arg = periph;
332 xpt_action((union ccb *)&csa);
333
334 if (bootverbose)
335 xpt_announce_periph(periph, NULL);
336
337 return(CAM_REQ_CMP);
338}
339
340static int
fef8985e 341passopen(struct dev_open_args *ap)
984263bc 342{
b13267a5 343 cdev_t dev = ap->a_head.a_dev;
984263bc
MD
344 struct cam_periph *periph;
345 struct pass_softc *softc;
346 int unit, error;
984263bc
MD
347
348 error = 0; /* default to no error */
349
350 /* unit = dkunit(dev); */
351 /* XXX KDM fix this */
352 unit = minor(dev) & 0xff;
353
354 periph = cam_extend_get(passperiphs, unit);
355
356 if (periph == NULL)
357 return (ENXIO);
358
359 softc = (struct pass_softc *)periph->softc;
360
4e01b467 361 crit_enter();
984263bc 362 if (softc->flags & PASS_FLAG_INVALID) {
4e01b467 363 crit_exit();
984263bc
MD
364 return(ENXIO);
365 }
366
367 /*
368 * Don't allow access when we're running at a high securelvel.
369 */
370 if (securelevel > 1) {
4e01b467 371 crit_exit();
984263bc
MD
372 return(EPERM);
373 }
374
375 /*
376 * Only allow read-write access.
377 */
fef8985e 378 if (((ap->a_oflags & FWRITE) == 0) || ((ap->a_oflags & FREAD) == 0)) {
4e01b467 379 crit_exit();
984263bc
MD
380 return(EPERM);
381 }
382
383 /*
384 * We don't allow nonblocking access.
385 */
fef8985e 386 if ((ap->a_oflags & O_NONBLOCK) != 0) {
984263bc
MD
387 xpt_print_path(periph->path);
388 printf("can't do nonblocking accesss\n");
4e01b467 389 crit_exit();
984263bc
MD
390 return(EINVAL);
391 }
392
377d4740 393 if ((error = cam_periph_lock(periph, PCATCH)) != 0) {
4e01b467 394 crit_exit();
984263bc
MD
395 return (error);
396 }
397
4e01b467 398 crit_exit();
984263bc
MD
399
400 if ((softc->flags & PASS_FLAG_OPEN) == 0) {
401 if (cam_periph_acquire(periph) != CAM_REQ_CMP)
402 return(ENXIO);
403 softc->flags |= PASS_FLAG_OPEN;
404 }
405
406 cam_periph_unlock(periph);
407
408 return (error);
409}
410
411static int
fef8985e 412passclose(struct dev_close_args *ap)
984263bc 413{
b13267a5 414 cdev_t dev = ap->a_head.a_dev;
984263bc
MD
415 struct cam_periph *periph;
416 struct pass_softc *softc;
417 int unit, error;
418
419 /* unit = dkunit(dev); */
420 /* XXX KDM fix this */
421 unit = minor(dev) & 0xff;
422
423 periph = cam_extend_get(passperiphs, unit);
424 if (periph == NULL)
425 return (ENXIO);
426
427 softc = (struct pass_softc *)periph->softc;
428
377d4740 429 if ((error = cam_periph_lock(periph, 0)) != 0)
984263bc
MD
430 return (error);
431
432 softc->flags &= ~PASS_FLAG_OPEN;
433
434 cam_periph_unlock(periph);
435 cam_periph_release(periph);
436
437 return (0);
438}
439
440/*
441 * Actually translate the requested transfer into one the physical driver
442 * can understand. The transfer is described by a buf and will include
443 * only one physical transfer.
444 */
fef8985e
MD
445static int
446passstrategy(struct dev_strategy_args *ap)
984263bc 447{
b13267a5 448 cdev_t dev = ap->a_head.a_dev;
fef8985e 449 struct bio *bio = ap->a_bio;
81b5c339 450 struct buf *bp = bio->bio_buf;
984263bc
MD
451 struct cam_periph *periph;
452 struct pass_softc *softc;
453 u_int unit;
984263bc
MD
454
455 /*
456 * The read/write interface for the passthrough driver doesn't
457 * really work right now. So, we just pass back EINVAL to tell the
458 * user to go away.
459 */
460 bp->b_error = EINVAL;
461 goto bad;
462
81b5c339 463 /* unit = dkunit(dev); */
984263bc 464 /* XXX KDM fix this */
81b5c339 465 unit = minor(dev) & 0xff;
984263bc
MD
466
467 periph = cam_extend_get(passperiphs, unit);
468 if (periph == NULL) {
469 bp->b_error = ENXIO;
470 goto bad;
471 }
472 softc = (struct pass_softc *)periph->softc;
473
474 /*
475 * Odd number of bytes or negative offset
476 */
477 /* valid request? */
54078292 478 if (bio->bio_offset < 0) {
984263bc
MD
479 bp->b_error = EINVAL;
480 goto bad;
481 }
482
483 /*
484 * Mask interrupts so that the pack cannot be invalidated until
485 * after we are in the queue. Otherwise, we might not properly
486 * clean up one of the buffers.
487 */
4e01b467 488 crit_enter();
81b5c339 489 bioq_insert_tail(&softc->bio_queue, bio);
4e01b467 490 crit_exit();
984263bc
MD
491
492 /*
493 * Schedule ourselves for performing the work.
494 */
495 xpt_schedule(periph, /* XXX priority */1);
496
fef8985e 497 return(0);
984263bc
MD
498bad:
499 bp->b_flags |= B_ERROR;
500
501 /*
502 * Correctly set the buf to indicate a completed xfer
503 */
504 bp->b_resid = bp->b_bcount;
81b5c339 505 biodone(bio);
fef8985e 506 return(0);
984263bc
MD
507}
508
509static void
510passstart(struct cam_periph *periph, union ccb *start_ccb)
511{
512 struct pass_softc *softc;
984263bc
MD
513
514 softc = (struct pass_softc *)periph->softc;
515
516 switch (softc->state) {
517 case PASS_STATE_NORMAL:
518 {
519 struct buf *bp;
81b5c339 520 struct bio *bio;
984263bc 521
4e01b467 522 crit_enter();
81b5c339 523 bio = bioq_first(&softc->bio_queue);
984263bc
MD
524 if (periph->immediate_priority <= periph->pinfo.priority) {
525 start_ccb->ccb_h.ccb_type = PASS_CCB_WAITING;
526 SLIST_INSERT_HEAD(&periph->ccb_list, &start_ccb->ccb_h,
527 periph_links.sle);
528 periph->immediate_priority = CAM_PRIORITY_NONE;
4e01b467 529 crit_exit();
984263bc 530 wakeup(&periph->ccb_list);
81b5c339 531 } else if (bio == NULL) {
4e01b467 532 crit_exit();
984263bc
MD
533 xpt_release_ccb(start_ccb);
534 } else {
535
81b5c339
MD
536 bioq_remove(&softc->bio_queue, bio);
537 bp = bio->bio_buf;
984263bc
MD
538
539 devstat_start_transaction(&softc->device_stats);
540
541 /*
542 * XXX JGibbs -
543 * Interpret the contents of the bp as a CCB
544 * and pass it to a routine shared by our ioctl
545 * code and passtart.
546 * For now, just biodone it with EIO so we don't
547 * hang.
548 */
549 bp->b_error = EIO;
550 bp->b_flags |= B_ERROR;
551 bp->b_resid = bp->b_bcount;
81b5c339
MD
552 biodone(bio);
553 bio = bioq_first(&softc->bio_queue);
4e01b467 554 crit_exit();
984263bc
MD
555
556 xpt_action(start_ccb);
557
558 }
81b5c339 559 if (bio != NULL) {
984263bc
MD
560 /* Have more work to do, so ensure we stay scheduled */
561 xpt_schedule(periph, /* XXX priority */1);
562 }
563 break;
564 }
565 }
566}
567static void
568passdone(struct cam_periph *periph, union ccb *done_ccb)
569{
570 struct pass_softc *softc;
571 struct ccb_scsiio *csio;
572
573 softc = (struct pass_softc *)periph->softc;
574 csio = &done_ccb->csio;
575 switch (csio->ccb_h.ccb_type) {
576 case PASS_CCB_BUFFER_IO:
577 {
81b5c339 578 struct bio *bio;
984263bc
MD
579 struct buf *bp;
580 cam_status status;
581 u_int8_t scsi_status;
582 devstat_trans_flags ds_flags;
583
584 status = done_ccb->ccb_h.status;
585 scsi_status = done_ccb->csio.scsi_status;
81b5c339
MD
586 bio = (struct bio *)done_ccb->ccb_h.ccb_bio;
587 bp = bio->bio_buf;
588
984263bc
MD
589 /* XXX handle errors */
590 if (!(((status & CAM_STATUS_MASK) == CAM_REQ_CMP)
591 && (scsi_status == SCSI_STATUS_OK))) {
592 int error;
593
594 if ((error = passerror(done_ccb, 0, 0)) == ERESTART) {
595 /*
596 * A retry was scheuled, so
597 * just return.
598 */
599 return;
600 }
601
602 /*
603 * XXX unfreeze the queue after we complete
604 * the abort process
605 */
606 bp->b_error = error;
607 bp->b_flags |= B_ERROR;
608 }
609
610 if ((done_ccb->ccb_h.flags & CAM_DIR_MASK) == CAM_DIR_IN)
611 ds_flags = DEVSTAT_READ;
612 else if ((done_ccb->ccb_h.flags & CAM_DIR_MASK) == CAM_DIR_OUT)
613 ds_flags = DEVSTAT_WRITE;
614 else
615 ds_flags = DEVSTAT_NO_DATA;
616
617 devstat_end_transaction_buf(&softc->device_stats, bp);
81b5c339 618 biodone(bio);
984263bc
MD
619 break;
620 }
621 case PASS_CCB_WAITING:
622 {
623 /* Caller will release the CCB */
624 wakeup(&done_ccb->ccb_h.cbfcnp);
625 return;
626 }
627 }
628 xpt_release_ccb(done_ccb);
629}
630
631static int
fef8985e 632passioctl(struct dev_ioctl_args *ap)
984263bc 633{
b13267a5 634 cdev_t dev = ap->a_head.a_dev;
fef8985e 635 caddr_t addr = ap->a_data;
984263bc
MD
636 struct cam_periph *periph;
637 struct pass_softc *softc;
638 u_int8_t unit;
639 int error;
640
641
642 /* unit = dkunit(dev); */
643 /* XXX KDM fix this */
644 unit = minor(dev) & 0xff;
645
646 periph = cam_extend_get(passperiphs, unit);
647
648 if (periph == NULL)
649 return(ENXIO);
650
651 softc = (struct pass_softc *)periph->softc;
652
653 error = 0;
654
fef8985e 655 switch (ap->a_cmd) {
984263bc
MD
656
657 case CAMIOCOMMAND:
658 {
659 union ccb *inccb;
660 union ccb *ccb;
661 int ccb_malloced;
662
663 inccb = (union ccb *)addr;
664
665 /*
666 * Some CCB types, like scan bus and scan lun can only go
667 * through the transport layer device.
668 */
669 if (inccb->ccb_h.func_code & XPT_FC_XPT_ONLY) {
670 xpt_print_path(periph->path);
671 printf("CCB function code %#x is restricted to the "
672 "XPT device\n", inccb->ccb_h.func_code);
673 error = ENODEV;
674 break;
675 }
676
677 /*
678 * Non-immediate CCBs need a CCB from the per-device pool
679 * of CCBs, which is scheduled by the transport layer.
680 * Immediate CCBs and user-supplied CCBs should just be
681 * malloced.
682 */
683 if ((inccb->ccb_h.func_code & XPT_FC_QUEUED)
684 && ((inccb->ccb_h.func_code & XPT_FC_USER_CCB) == 0)) {
685 ccb = cam_periph_getccb(periph,
686 inccb->ccb_h.pinfo.priority);
687 ccb_malloced = 0;
688 } else {
689 ccb = xpt_alloc_ccb();
690
691 if (ccb != NULL)
692 xpt_setup_ccb(&ccb->ccb_h, periph->path,
693 inccb->ccb_h.pinfo.priority);
694 ccb_malloced = 1;
695 }
696
697 if (ccb == NULL) {
698 xpt_print_path(periph->path);
699 printf("unable to allocate CCB\n");
700 error = ENOMEM;
701 break;
702 }
703
704 error = passsendccb(periph, ccb, inccb);
705
706 if (ccb_malloced)
707 xpt_free_ccb(ccb);
708 else
709 xpt_release_ccb(ccb);
710
711 break;
712 }
713 default:
fef8985e 714 error = cam_periph_ioctl(periph, ap->a_cmd, addr, passerror);
984263bc
MD
715 break;
716 }
717
718 return(error);
719}
720
721/*
722 * Generally, "ccb" should be the CCB supplied by the kernel. "inccb"
723 * should be the CCB that is copied in from the user.
724 */
725static int
726passsendccb(struct cam_periph *periph, union ccb *ccb, union ccb *inccb)
727{
728 struct pass_softc *softc;
729 struct cam_periph_map_info mapinfo;
730 int error, need_unmap;
731
732 softc = (struct pass_softc *)periph->softc;
733
734 need_unmap = 0;
735
736 /*
737 * There are some fields in the CCB header that need to be
738 * preserved, the rest we get from the user.
739 */
740 xpt_merge_ccb(ccb, inccb);
741
742 /*
743 * There's no way for the user to have a completion
744 * function, so we put our own completion function in here.
745 */
746 ccb->ccb_h.cbfcnp = passdone;
747
748 /*
749 * We only attempt to map the user memory into kernel space
750 * if they haven't passed in a physical memory pointer,
751 * and if there is actually an I/O operation to perform.
752 * Right now cam_periph_mapmem() only supports SCSI and device
753 * match CCBs. For the SCSI CCBs, we only pass the CCB in if
754 * there's actually data to map. cam_periph_mapmem() will do the
755 * right thing, even if there isn't data to map, but since CCBs
756 * without data are a reasonably common occurance (e.g. test unit
757 * ready), it will save a few cycles if we check for it here.
758 */
759 if (((ccb->ccb_h.flags & CAM_DATA_PHYS) == 0)
760 && (((ccb->ccb_h.func_code == XPT_SCSI_IO)
761 && ((ccb->ccb_h.flags & CAM_DIR_MASK) != CAM_DIR_NONE))
762 || (ccb->ccb_h.func_code == XPT_DEV_MATCH))) {
763
764 bzero(&mapinfo, sizeof(mapinfo));
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 return(error);
774
775 /*
776 * We successfully mapped the memory in, so we need to
777 * unmap it when the transaction is done.
778 */
779 need_unmap = 1;
780 }
781
782 /*
783 * If the user wants us to perform any error recovery, then honor
784 * that request. Otherwise, it's up to the user to perform any
785 * error recovery.
786 */
787 error = cam_periph_runccb(ccb,
788 (ccb->ccb_h.flags & CAM_PASS_ERR_RECOVER) ?
789 passerror : NULL,
790 /* cam_flags */ 0,
791 /* sense_flags */SF_RETRY_UA | SF_RETRY_SELTO,
792 &softc->device_stats);
793
794 if (need_unmap != 0)
795 cam_periph_unmapmem(ccb, &mapinfo);
796
797 ccb->ccb_h.cbfcnp = NULL;
798 ccb->ccb_h.periph_priv = inccb->ccb_h.periph_priv;
799 bcopy(ccb, inccb, sizeof(union ccb));
800
801 return(error);
802}
803
804static int
805passerror(union ccb *ccb, u_int32_t cam_flags, u_int32_t sense_flags)
806{
807 struct cam_periph *periph;
808 struct pass_softc *softc;
809
810 periph = xpt_path_periph(ccb->ccb_h.path);
811 softc = (struct pass_softc *)periph->softc;
812
813 return(cam_periph_error(ccb, cam_flags, sense_flags,
814 &softc->saved_ccb));
815}