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