[ source navigation ] [ diff markup ] [ identifier search ] [ freetext search ] [ file search ] [ list types ] [ track identifier ]

FreeBSD/Linux Kernel Cross Reference
sys/cam/scsi/scsi_pass.c

Version: -  FREEBSD  -  FREEBSD7  -  FREEBSD71  -  FREEBSD70  -  FREEBSD6  -  FREEBSD64  -  FREEBSD63  -  FREEBSD62  -  FREEBSD61  -  FREEBSD60  -  FREEBSD5  -  FREEBSD55  -  FREEBSD54  -  FREEBSD53  -  FREEBSD52  -  FREEBSD51  -  FREEBSD50  -  FREEBSD4  -  FREEBSD3  -  FREEBSD22  -  linux-2.6  -  linux-2.4.22  -  MK83  -  MK84  -  PLAN9  -  DFBSD  -  NETBSD  -  NETBSD5  -  NETBSD4  -  NETBSD3  -  NETBSD20  -  OPENBSD  -  xnu-517  -  xnu-792  -  xnu-792.6.70  -  xnu-1228  -  OPENSOLARIS  -  minix-3-1-1  -  TRUSTEDBSD-SEBSD  -  FREEBSD-LIBC  -  FREEBSD7-LIBC  -  FREEBSD6-LIBC  -  GLIBC27 
SearchContext: -  none  -  excerpts  -  bigexcerpts 

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

Cache object: 01a032cb212852aaa36da273fc8a30ea


[ source navigation ] [ diff markup ] [ identifier search ] [ freetext search ] [ file search ] [ list types ] [ track identifier ]


This page is part of the FreeBSD/Linux Linux Kernel Cross-Reference, and was automatically generated using a modified version of the LXR engine.