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