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