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