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