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