[ source navigation ] [ diff markup ] [ identifier search ] [ freetext search ] [ file search ]

FreeBSD/Linux Kernel Cross Reference
sys/cam/cam_xpt.c

Version: -  FREEBSD  -  FREEBSD7  -  FREEBSD70  -  FREEBSD6  -  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  -  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  * Implementation of the Common Access Method Transport (XPT) layer.
  3  *
  4  * Copyright (c) 1997, 1998, 1999 Justin T. Gibbs.
  5  * Copyright (c) 1997, 1998, 1999 Kenneth D. Merry.
  6  * All rights reserved.
  7  *
  8  * Redistribution and use in source and binary forms, with or without
  9  * modification, are permitted provided that the following conditions
 10  * are met:
 11  * 1. Redistributions of source code must retain the above copyright
 12  *    notice, this list of conditions, and the following disclaimer,
 13  *    without modification, immediately at the beginning of the file.
 14  * 2. The name of the author may not be used to endorse or promote products
 15  *    derived from this software without specific prior written permission.
 16  *
 17  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
 18  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
 19  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
 20  * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE FOR
 21  * ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
 22  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
 23  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
 24  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
 25  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
 26  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
 27  * SUCH DAMAGE.
 28  */
 29 
 30 #include <sys/cdefs.h>
 31 __FBSDID("$FreeBSD: src/sys/cam/cam_xpt.c,v 1.198 2008/08/01 15:13:56 emaste Exp $");
 32 
 33 #include <sys/param.h>
 34 #include <sys/bus.h>
 35 #include <sys/systm.h>
 36 #include <sys/types.h>
 37 #include <sys/malloc.h>
 38 #include <sys/kernel.h>
 39 #include <sys/time.h>
 40 #include <sys/conf.h>
 41 #include <sys/fcntl.h>
 42 #include <sys/md5.h>
 43 #include <sys/interrupt.h>
 44 #include <sys/sbuf.h>
 45 #include <sys/taskqueue.h>
 46 
 47 #include <sys/lock.h>
 48 #include <sys/mutex.h>
 49 #include <sys/sysctl.h>
 50 #include <sys/kthread.h>
 51 
 52 #ifdef PC98
 53 #include <pc98/pc98/pc98_machdep.h>     /* geometry translation */
 54 #endif
 55 
 56 #include <cam/cam.h>
 57 #include <cam/cam_ccb.h>
 58 #include <cam/cam_periph.h>
 59 #include <cam/cam_sim.h>
 60 #include <cam/cam_xpt.h>
 61 #include <cam/cam_xpt_sim.h>
 62 #include <cam/cam_xpt_periph.h>
 63 #include <cam/cam_debug.h>
 64 
 65 #include <cam/scsi/scsi_all.h>
 66 #include <cam/scsi/scsi_message.h>
 67 #include <cam/scsi/scsi_pass.h>
 68 #include <machine/stdarg.h>     /* for xpt_print below */
 69 #include "opt_cam.h"
 70 
 71 /* Datastructures internal to the xpt layer */
 72 MALLOC_DEFINE(M_CAMXPT, "CAM XPT", "CAM XPT buffers");
 73 
 74 /* Object for defering XPT actions to a taskqueue */
 75 struct xpt_task {
 76         struct task     task;
 77         void            *data1;
 78         uintptr_t       data2;
 79 };
 80 
 81 /*
 82  * Definition of an async handler callback block.  These are used to add
 83  * SIMs and peripherals to the async callback lists.
 84  */
 85 struct async_node {
 86         SLIST_ENTRY(async_node) links;
 87         u_int32_t       event_enable;   /* Async Event enables */
 88         void            (*callback)(void *arg, u_int32_t code,
 89                                     struct cam_path *path, void *args);
 90         void            *callback_arg;
 91 };
 92 
 93 SLIST_HEAD(async_list, async_node);
 94 SLIST_HEAD(periph_list, cam_periph);
 95 
 96 /*
 97  * This is the maximum number of high powered commands (e.g. start unit)
 98  * that can be outstanding at a particular time.
 99  */
100 #ifndef CAM_MAX_HIGHPOWER
101 #define CAM_MAX_HIGHPOWER  4
102 #endif
103 
104 /*
105  * Structure for queueing a device in a run queue.
106  * There is one run queue for allocating new ccbs,
107  * and another for sending ccbs to the controller.
108  */
109 struct cam_ed_qinfo {
110         cam_pinfo pinfo;
111         struct    cam_ed *device;
112 };
113 
114 /*
115  * The CAM EDT (Existing Device Table) contains the device information for
116  * all devices for all busses in the system.  The table contains a
117  * cam_ed structure for each device on the bus.
118  */
119 struct cam_ed {
120         TAILQ_ENTRY(cam_ed) links;
121         struct  cam_ed_qinfo alloc_ccb_entry;
122         struct  cam_ed_qinfo send_ccb_entry;
123         struct  cam_et   *target;
124         struct  cam_sim  *sim;
125         lun_id_t         lun_id;
126         struct  camq drvq;              /*
127                                          * Queue of type drivers wanting to do
128                                          * work on this device.
129                                          */
130         struct  cam_ccbq ccbq;          /* Queue of pending ccbs */
131         struct  async_list asyncs;      /* Async callback info for this B/T/L */
132         struct  periph_list periphs;    /* All attached devices */
133         u_int   generation;             /* Generation number */
134         struct  cam_periph *owner;      /* Peripheral driver's ownership tag */
135         struct  xpt_quirk_entry *quirk; /* Oddities about this device */
136                                         /* Storage for the inquiry data */
137         cam_proto        protocol;
138         u_int            protocol_version;
139         cam_xport        transport;
140         u_int            transport_version;
141         struct           scsi_inquiry_data inq_data;
142         u_int8_t         inq_flags;     /*
143                                          * Current settings for inquiry flags.
144                                          * This allows us to override settings
145                                          * like disconnection and tagged
146                                          * queuing for a device.
147                                          */
148         u_int8_t         queue_flags;   /* Queue flags from the control page */
149         u_int8_t         serial_num_len;
150         u_int8_t        *serial_num;
151         u_int32_t        qfrozen_cnt;
152         u_int32_t        flags;
153 #define CAM_DEV_UNCONFIGURED            0x01
154 #define CAM_DEV_REL_TIMEOUT_PENDING     0x02
155 #define CAM_DEV_REL_ON_COMPLETE         0x04
156 #define CAM_DEV_REL_ON_QUEUE_EMPTY      0x08
157 #define CAM_DEV_RESIZE_QUEUE_NEEDED     0x10
158 #define CAM_DEV_TAG_AFTER_COUNT         0x20
159 #define CAM_DEV_INQUIRY_DATA_VALID      0x40
160 #define CAM_DEV_IN_DV                   0x80
161 #define CAM_DEV_DV_HIT_BOTTOM           0x100
162         u_int32_t        tag_delay_count;
163 #define CAM_TAG_DELAY_COUNT             5
164         u_int32_t        tag_saved_openings;
165         u_int32_t        refcount;
166         struct callout   callout;
167 };
168 
169 /*
170  * Each target is represented by an ET (Existing Target).  These
171  * entries are created when a target is successfully probed with an
172  * identify, and removed when a device fails to respond after a number
173  * of retries, or a bus rescan finds the device missing.
174  */
175 struct cam_et {
176         TAILQ_HEAD(, cam_ed) ed_entries;
177         TAILQ_ENTRY(cam_et) links;
178         struct  cam_eb  *bus;
179         target_id_t     target_id;
180         u_int32_t       refcount;
181         u_int           generation;
182         struct          timeval last_reset;
183 };
184 
185 /*
186  * Each bus is represented by an EB (Existing Bus).  These entries
187  * are created by calls to xpt_bus_register and deleted by calls to
188  * xpt_bus_deregister.
189  */
190 struct cam_eb {
191         TAILQ_HEAD(, cam_et) et_entries;
192         TAILQ_ENTRY(cam_eb)  links;
193         path_id_t            path_id;
194         struct cam_sim       *sim;
195         struct timeval       last_reset;
196         u_int32_t            flags;
197 #define CAM_EB_RUNQ_SCHEDULED   0x01
198         u_int32_t            refcount;
199         u_int                generation;
200         device_t             parent_dev;
201 };
202 
203 struct cam_path {
204         struct cam_periph *periph;
205         struct cam_eb     *bus;
206         struct cam_et     *target;
207         struct cam_ed     *device;
208 };
209 
210 struct xpt_quirk_entry {
211         struct scsi_inquiry_pattern inq_pat;
212         u_int8_t quirks;
213 #define CAM_QUIRK_NOLUNS        0x01
214 #define CAM_QUIRK_NOSERIAL      0x02
215 #define CAM_QUIRK_HILUNS        0x04
216 #define CAM_QUIRK_NOHILUNS      0x08
217         u_int mintags;
218         u_int maxtags;
219 };
220 
221 static int cam_srch_hi = 0;
222 TUNABLE_INT("kern.cam.cam_srch_hi", &cam_srch_hi);
223 static int sysctl_cam_search_luns(SYSCTL_HANDLER_ARGS);
224 SYSCTL_PROC(_kern_cam, OID_AUTO, cam_srch_hi, CTLTYPE_INT|CTLFLAG_RW, 0, 0,
225     sysctl_cam_search_luns, "I",
226     "allow search above LUN 7 for SCSI3 and greater devices");
227 
228 #define CAM_SCSI2_MAXLUN        8
229 /*
230  * If we're not quirked to search <= the first 8 luns
231  * and we are either quirked to search above lun 8,
232  * or we're > SCSI-2 and we've enabled hilun searching,
233  * or we're > SCSI-2 and the last lun was a success,
234  * we can look for luns above lun 8.
235  */
236 #define CAN_SRCH_HI_SPARSE(dv)                          \
237   (((dv->quirk->quirks & CAM_QUIRK_NOHILUNS) == 0)      \
238   && ((dv->quirk->quirks & CAM_QUIRK_HILUNS)            \
239   || (SID_ANSI_REV(&dv->inq_data) > SCSI_REV_2 && cam_srch_hi)))
240 
241 #define CAN_SRCH_HI_DENSE(dv)                           \
242   (((dv->quirk->quirks & CAM_QUIRK_NOHILUNS) == 0)      \
243   && ((dv->quirk->quirks & CAM_QUIRK_HILUNS)            \
244   || (SID_ANSI_REV(&dv->inq_data) > SCSI_REV_2)))
245 
246 typedef enum {
247         XPT_FLAG_OPEN           = 0x01
248 } xpt_flags;
249 
250 struct xpt_softc {
251         xpt_flags               flags;
252         u_int32_t               xpt_generation;
253 
254         /* number of high powered commands that can go through right now */
255         STAILQ_HEAD(highpowerlist, ccb_hdr)     highpowerq;
256         int                     num_highpower;
257 
258         /* queue for handling async rescan requests. */
259         TAILQ_HEAD(, ccb_hdr) ccb_scanq;
260 
261         /* Registered busses */
262         TAILQ_HEAD(,cam_eb)     xpt_busses;
263         u_int                   bus_generation;
264 
265         struct intr_config_hook *xpt_config_hook;
266 
267         struct mtx              xpt_topo_lock;
268         struct mtx              xpt_lock;
269 };
270 
271 static const char quantum[] = "QUANTUM";
272 static const char sony[] = "SONY";
273 static const char west_digital[] = "WDIGTL";
274 static const char samsung[] = "SAMSUNG";
275 static const char seagate[] = "SEAGATE";
276 static const char microp[] = "MICROP";
277 
278 static struct xpt_quirk_entry xpt_quirk_table[] =
279 {
280         {
281                 /* Reports QUEUE FULL for temporary resource shortages */
282                 { T_DIRECT, SIP_MEDIA_FIXED, quantum, "XP39100*", "*" },
283                 /*quirks*/0, /*mintags*/24, /*maxtags*/32
284         },
285         {
286                 /* Reports QUEUE FULL for temporary resource shortages */
287                 { T_DIRECT, SIP_MEDIA_FIXED, quantum, "XP34550*", "*" },
288                 /*quirks*/0, /*mintags*/24, /*maxtags*/32
289         },
290         {
291                 /* Reports QUEUE FULL for temporary resource shortages */
292                 { T_DIRECT, SIP_MEDIA_FIXED, quantum, "XP32275*", "*" },
293                 /*quirks*/0, /*mintags*/24, /*maxtags*/32
294         },
295         {
296                 /* Broken tagged queuing drive */
297                 { T_DIRECT, SIP_MEDIA_FIXED, microp, "4421-07*", "*" },
298                 /*quirks*/0, /*mintags*/0, /*maxtags*/
299         },
300         {
301                 /* Broken tagged queuing drive */
302                 { T_DIRECT, SIP_MEDIA_FIXED, "HP", "C372*", "*" },
303                 /*quirks*/0, /*mintags*/0, /*maxtags*/
304         },
305         {
306                 /* Broken tagged queuing drive */
307                 { T_DIRECT, SIP_MEDIA_FIXED, microp, "3391*", "x43h" },
308                 /*quirks*/0, /*mintags*/0, /*maxtags*/
309         },
310         {
311                 /*
312                  * Unfortunately, the Quantum Atlas III has the same
313                  * problem as the Atlas II drives above.
314                  * Reported by: "Johan Granlund" <johan@granlund.nu>
315                  *
316                  * For future reference, the drive with the problem was:
317                  * QUANTUM QM39100TD-SW N1B0
318                  *
319                  * It's possible that Quantum will fix the problem in later
320                  * firmware revisions.  If that happens, the quirk entry
321                  * will need to be made specific to the firmware revisions
322                  * with the problem.
323                  *
324                  */
325                 /* Reports QUEUE FULL for temporary resource shortages */
326                 { T_DIRECT, SIP_MEDIA_FIXED, quantum, "QM39100*", "*" },
327                 /*quirks*/0, /*mintags*/24, /*maxtags*/32
328         },
329         {
330                 /*
331                  * 18 Gig Atlas III, same problem as the 9G version.
332                  * Reported by: Andre Albsmeier
333                  *              <andre.albsmeier@mchp.siemens.de>
334                  *
335                  * For future reference, the drive with the problem was:
336                  * QUANTUM QM318000TD-S N491
337                  */
338                 /* Reports QUEUE FULL for temporary resource shortages */
339                 { T_DIRECT, SIP_MEDIA_FIXED, quantum, "QM318000*", "*" },
340                 /*quirks*/0, /*mintags*/24, /*maxtags*/32
341         },
342         {
343                 /*
344                  * Broken tagged queuing drive
345                  * Reported by: Bret Ford <bford@uop.cs.uop.edu>
346                  *         and: Martin Renters <martin@tdc.on.ca>
347                  */
348                 { T_DIRECT, SIP_MEDIA_FIXED, seagate, "ST410800*", "71*" },
349                 /*quirks*/0, /*mintags*/0, /*maxtags*/
350         },
351                 /*
352                  * The Seagate Medalist Pro drives have very poor write
353                  * performance with anything more than 2 tags.
354                  *
355                  * Reported by:  Paul van der Zwan <paulz@trantor.xs4all.nl>
356                  * Drive:  <SEAGATE ST36530N 1444>
357                  *
358                  * Reported by:  Jeremy Lea <reg@shale.csir.co.za>
359                  * Drive:  <SEAGATE ST34520W 1281>
360                  *
361                  * No one has actually reported that the 9G version
362                  * (ST39140*) of the Medalist Pro has the same problem, but
363                  * we're assuming that it does because the 4G and 6.5G
364                  * versions of the drive are broken.
365                  */
366         {
367                 { T_DIRECT, SIP_MEDIA_FIXED, seagate, "ST34520*", "*"},
368                 /*quirks*/0, /*mintags*/2, /*maxtags*/2
369         },
370         {
371                 { T_DIRECT, SIP_MEDIA_FIXED, seagate, "ST36530*", "*"},
372                 /*quirks*/0, /*mintags*/2, /*maxtags*/2
373         },
374         {
375                 { T_DIRECT, SIP_MEDIA_FIXED, seagate, "ST39140*", "*"},
376                 /*quirks*/0, /*mintags*/2, /*maxtags*/2
377         },
378         {
379                 /*
380                  * Slow when tagged queueing is enabled.  Write performance
381                  * steadily drops off with more and more concurrent
382                  * transactions.  Best sequential write performance with
383                  * tagged queueing turned off and write caching turned on.
384                  *
385                  * PR:  kern/10398
386                  * Submitted by:  Hideaki Okada <hokada@isl.melco.co.jp>
387                  * Drive:  DCAS-34330 w/ "S65A" firmware.
388                  *
389                  * The drive with the problem had the "S65A" firmware
390                  * revision, and has also been reported (by Stephen J.
391                  * Roznowski <sjr@home.net>) for a drive with the "S61A"
392                  * firmware revision.
393                  *
394                  * Although no one has reported problems with the 2 gig
395                  * version of the DCAS drive, the assumption is that it
396                  * has the same problems as the 4 gig version.  Therefore
397                  * this quirk entries disables tagged queueing for all
398                  * DCAS drives.
399                  */
400                 { T_DIRECT, SIP_MEDIA_FIXED, "IBM", "DCAS*", "*" },
401                 /*quirks*/0, /*mintags*/0, /*maxtags*/
402         },
403         {
404                 /* Broken tagged queuing drive */
405                 { T_DIRECT, SIP_MEDIA_REMOVABLE, "iomega", "jaz*", "*" },
406                 /*quirks*/0, /*mintags*/0, /*maxtags*/
407         },
408         {
409                 /* Broken tagged queuing drive */
410                 { T_DIRECT, SIP_MEDIA_FIXED, "CONNER", "CFP2107*", "*" },
411                 /*quirks*/0, /*mintags*/0, /*maxtags*/
412         },
413         {
414                 /* This does not support other than LUN 0 */
415                 { T_DIRECT, SIP_MEDIA_FIXED, "VMware*", "*", "*" },
416                 CAM_QUIRK_NOLUNS, /*mintags*/2, /*maxtags*/255
417         },
418         {
419                 /*
420                  * Broken tagged queuing drive.
421                  * Submitted by:
422                  * NAKAJI Hiroyuki <nakaji@zeisei.dpri.kyoto-u.ac.jp>
423                  * in PR kern/9535
424                  */
425                 { T_DIRECT, SIP_MEDIA_FIXED, samsung, "WN34324U*", "*" },
426                 /*quirks*/0, /*mintags*/0, /*maxtags*/
427         },
428         {
429                 /*
430                  * Slow when tagged queueing is enabled. (1.5MB/sec versus
431                  * 8MB/sec.)
432                  * Submitted by: Andrew Gallatin <gallatin@cs.duke.edu>
433                  * Best performance with these drives is achieved with
434                  * tagged queueing turned off, and write caching turned on.
435                  */
436                 { T_DIRECT, SIP_MEDIA_FIXED, west_digital, "WDE*", "*" },
437                 /*quirks*/0, /*mintags*/0, /*maxtags*/
438         },
439         {
440                 /*
441                  * Slow when tagged queueing is enabled. (1.5MB/sec versus
442                  * 8MB/sec.)
443                  * Submitted by: Andrew Gallatin <gallatin@cs.duke.edu>
444                  * Best performance with these drives is achieved with
445                  * tagged queueing turned off, and write caching turned on.
446                  */
447                 { T_DIRECT, SIP_MEDIA_FIXED, west_digital, "ENTERPRISE", "*" },
448                 /*quirks*/0, /*mintags*/0, /*maxtags*/
449         },
450         {
451                 /*
452                  * Doesn't handle queue full condition correctly,
453                  * so we need to limit maxtags to what the device
454                  * can handle instead of determining this automatically.
455                  */
456                 { T_DIRECT, SIP_MEDIA_FIXED, samsung, "WN321010S*", "*" },
457                 /*quirks*/0, /*mintags*/2, /*maxtags*/32
458         },
459         {
460                 /* Really only one LUN */
461                 { T_ENCLOSURE, SIP_MEDIA_FIXED, "SUN", "SENA", "*" },
462                 CAM_QUIRK_NOLUNS, /*mintags*/0, /*maxtags*/
463         },
464         {
465                 /* I can't believe we need a quirk for DPT volumes. */
466                 { T_ANY, SIP_MEDIA_FIXED|SIP_MEDIA_REMOVABLE, "DPT", "*", "*" },
467                 CAM_QUIRK_NOLUNS,
468                 /*mintags*/0, /*maxtags*/255
469         },
470         {
471                 /*
472                  * Many Sony CDROM drives don't like multi-LUN probing.
473                  */
474                 { T_CDROM, SIP_MEDIA_REMOVABLE, sony, "CD-ROM CDU*", "*" },
475                 CAM_QUIRK_NOLUNS, /*mintags*/0, /*maxtags*/
476         },
477         {
478                 /*
479                  * This drive doesn't like multiple LUN probing.
480                  * Submitted by:  Parag Patel <parag@cgt.com>
481                  */
482                 { T_WORM, SIP_MEDIA_REMOVABLE, sony, "CD-R   CDU9*", "*" },
483                 CAM_QUIRK_NOLUNS, /*mintags*/0, /*maxtags*/
484         },
485         {
486                 { T_WORM, SIP_MEDIA_REMOVABLE, "YAMAHA", "CDR100*", "*" },
487                 CAM_QUIRK_NOLUNS, /*mintags*/0, /*maxtags*/
488         },
489         {
490                 /*
491                  * The 8200 doesn't like multi-lun probing, and probably
492                  * don't like serial number requests either.
493                  */
494                 {
495                         T_SEQUENTIAL, SIP_MEDIA_REMOVABLE, "EXABYTE",
496                         "EXB-8200*", "*"
497                 },
498                 CAM_QUIRK_NOLUNS, /*mintags*/0, /*maxtags*/
499         },
500         {
501                 /*
502                  * Let's try the same as above, but for a drive that says
503                  * it's an IPL-6860 but is actually an EXB 8200.
504                  */
505                 {
506                         T_SEQUENTIAL, SIP_MEDIA_REMOVABLE, "EXABYTE",
507                         "IPL-6860*", "*"
508                 },
509                 CAM_QUIRK_NOLUNS, /*mintags*/0, /*maxtags*/
510         },
511         {
512                 /*
513                  * These Hitachi drives don't like multi-lun probing.
514                  * The PR submitter has a DK319H, but says that the Linux
515                  * kernel has a similar work-around for the DK312 and DK314,
516                  * so all DK31* drives are quirked here.
517                  * PR:            misc/18793
518                  * Submitted by:  Paul Haddad <paul@pth.com>
519                  */
520                 { T_DIRECT, SIP_MEDIA_FIXED, "HITACHI", "DK31*", "*" },
521                 CAM_QUIRK_NOLUNS, /*mintags*/2, /*maxtags*/255
522         },
523         {
524                 /*
525                  * The Hitachi CJ series with J8A8 firmware apparantly has
526                  * problems with tagged commands.
527                  * PR: 23536
528                  * Reported by: amagai@nue.org
529                  */
530                 { T_DIRECT, SIP_MEDIA_FIXED, "HITACHI", "DK32CJ*", "J8A8" },
531                 CAM_QUIRK_NOLUNS, /*mintags*/0, /*maxtags*/
532         },
533         {
534                 /*
535                  * These are the large storage arrays.
536                  * Submitted by:  William Carrel <william.carrel@infospace.com>
537                  */
538                 { T_DIRECT, SIP_MEDIA_FIXED, "HITACHI", "OPEN*", "*" },
539                 CAM_QUIRK_HILUNS, 2, 1024
540         },
541         {
542                 /*
543                  * This old revision of the TDC3600 is also SCSI-1, and
544                  * hangs upon serial number probing.
545                  */
546                 {
547                         T_SEQUENTIAL, SIP_MEDIA_REMOVABLE, "TANDBERG",
548                         " TDC 3600", "U07:"
549                 },
550                 CAM_QUIRK_NOSERIAL, /*mintags*/0, /*maxtags*/
551         },
552         {
553                 /*
554                  * Would repond to all LUNs if asked for.
555                  */
556                 {
557                         T_SEQUENTIAL, SIP_MEDIA_REMOVABLE, "CALIPER",
558                         "CP150", "*"
559                 },
560                 CAM_QUIRK_NOLUNS, /*mintags*/0, /*maxtags*/
561         },
562         {
563                 /*
564                  * Would repond to all LUNs if asked for.
565                  */
566                 {
567                         T_SEQUENTIAL, SIP_MEDIA_REMOVABLE, "KENNEDY",
568                         "96X2*", "*"
569                 },
570                 CAM_QUIRK_NOLUNS, /*mintags*/0, /*maxtags*/
571         },
572         {
573                 /* Submitted by: Matthew Dodd <winter@jurai.net> */
574                 { T_PROCESSOR, SIP_MEDIA_FIXED, "Cabletrn", "EA41*", "*" },
575                 CAM_QUIRK_NOLUNS, /*mintags*/0, /*maxtags*/
576         },
577         {
578                 /* Submitted by: Matthew Dodd <winter@jurai.net> */
579                 { T_PROCESSOR, SIP_MEDIA_FIXED, "CABLETRN", "EA41*", "*" },
580                 CAM_QUIRK_NOLUNS, /*mintags*/0, /*maxtags*/
581         },
582         {
583                 /* TeraSolutions special settings for TRC-22 RAID */
584                 { T_DIRECT, SIP_MEDIA_FIXED, "TERASOLU", "TRC-22", "*" },
585                   /*quirks*/0, /*mintags*/55, /*maxtags*/255
586         },
587         {
588                 /* Veritas Storage Appliance */
589                 { T_DIRECT, SIP_MEDIA_FIXED, "VERITAS", "*", "*" },
590                   CAM_QUIRK_HILUNS, /*mintags*/2, /*maxtags*/1024
591         },
592         {
593                 /*
594                  * Would respond to all LUNs.  Device type and removable
595                  * flag are jumper-selectable.
596                  */
597                 { T_ANY, SIP_MEDIA_REMOVABLE|SIP_MEDIA_FIXED, "MaxOptix",
598                   "Tahiti 1", "*"
599                 },
600                 CAM_QUIRK_NOLUNS, /*mintags*/0, /*maxtags*/
601         },
602         {
603                 /* EasyRAID E5A aka. areca ARC-6010 */
604                 { T_DIRECT, SIP_MEDIA_FIXED, "easyRAID", "*", "*" },
605                   CAM_QUIRK_NOHILUNS, /*mintags*/2, /*maxtags*/255
606         },
607         {
608                 { T_ENCLOSURE, SIP_MEDIA_FIXED, "DP", "BACKPLANE", "*" },
609                 CAM_QUIRK_NOLUNS, /*mintags*/0, /*maxtags*/
610         },
611         {
612                 /* Default tagged queuing parameters for all devices */
613                 {
614                   T_ANY, SIP_MEDIA_REMOVABLE|SIP_MEDIA_FIXED,
615                   /*vendor*/"*", /*product*/"*", /*revision*/"*"
616                 },
617                 /*quirks*/0, /*mintags*/2, /*maxtags*/255
618         },
619 };
620 
621 static const int xpt_quirk_table_size =
622         sizeof(xpt_quirk_table) / sizeof(*xpt_quirk_table);
623 
624 typedef enum {
625         DM_RET_COPY             = 0x01,
626         DM_RET_FLAG_MASK        = 0x0f,
627         DM_RET_NONE             = 0x00,
628         DM_RET_STOP             = 0x10,
629         DM_RET_DESCEND          = 0x20,
630         DM_RET_ERROR            = 0x30,
631         DM_RET_ACTION_MASK      = 0xf0
632 } dev_match_ret;
633 
634 typedef enum {
635         XPT_DEPTH_BUS,
636         XPT_DEPTH_TARGET,
637         XPT_DEPTH_DEVICE,
638         XPT_DEPTH_PERIPH
639 } xpt_traverse_depth;
640 
641 struct xpt_traverse_config {
642         xpt_traverse_depth      depth;
643         void                    *tr_func;
644         void                    *tr_arg;
645 };
646 
647 typedef int     xpt_busfunc_t (struct cam_eb *bus, void *arg);
648 typedef int     xpt_targetfunc_t (struct cam_et *target, void *arg);
649 typedef int     xpt_devicefunc_t (struct cam_ed *device, void *arg);
650 typedef int     xpt_periphfunc_t (struct cam_periph *periph, void *arg);
651 typedef int     xpt_pdrvfunc_t (struct periph_driver **pdrv, void *arg);
652 
653 /* Transport layer configuration information */
654 static struct xpt_softc xsoftc;
655 
656 /* Queues for our software interrupt handler */
657 typedef TAILQ_HEAD(cam_isrq, ccb_hdr) cam_isrq_t;
658 typedef TAILQ_HEAD(cam_simq, cam_sim) cam_simq_t;
659 static cam_simq_t cam_simq;
660 static struct mtx cam_simq_lock;
661 
662 /* Pointers to software interrupt handlers */
663 static void *cambio_ih;
664 
665 struct cam_periph *xpt_periph;
666 
667 static periph_init_t xpt_periph_init;
668 
669 static periph_init_t probe_periph_init;
670 
671 static struct periph_driver xpt_driver =
672 {
673         xpt_periph_init, "xpt",
674         TAILQ_HEAD_INITIALIZER(xpt_driver.units)
675 };
676 
677 static struct periph_driver probe_driver =
678 {
679         probe_periph_init, "probe",
680         TAILQ_HEAD_INITIALIZER(probe_driver.units)
681 };
682 
683 PERIPHDRIVER_DECLARE(xpt, xpt_driver);
684 PERIPHDRIVER_DECLARE(probe, probe_driver);
685 
686 
687 static d_open_t xptopen;
688 static d_close_t xptclose;
689 static d_ioctl_t xptioctl;
690 
691 static struct cdevsw xpt_cdevsw = {
692         .d_version =    D_VERSION,
693         .d_flags =      0,
694         .d_open =       xptopen,
695         .d_close =      xptclose,
696         .d_ioctl =      xptioctl,
697         .d_name =       "xpt",
698 };
699 
700 
701 static void dead_sim_action(struct cam_sim *sim, union ccb *ccb);
702 static void dead_sim_poll(struct cam_sim *sim);
703 
704 /* Dummy SIM that is used when the real one has gone. */
705 static struct cam_sim cam_dead_sim = {
706         .sim_action =   dead_sim_action,
707         .sim_poll =     dead_sim_poll,
708         .sim_name =     "dead_sim",
709 };
710 
711 #define SIM_DEAD(sim)   ((sim) == &cam_dead_sim)
712 
713 
714 /* Storage for debugging datastructures */
715 #ifdef  CAMDEBUG
716 struct cam_path *cam_dpath;
717 u_int32_t cam_dflags;
718 u_int32_t cam_debug_delay;
719 #endif
720 
721 #if defined(CAM_DEBUG_FLAGS) && !defined(CAMDEBUG)
722 #error "You must have options CAMDEBUG to use options CAM_DEBUG_FLAGS"
723 #endif
724 
725 /*
726  * In order to enable the CAM_DEBUG_* options, the user must have CAMDEBUG
727  * enabled.  Also, the user must have either none, or all of CAM_DEBUG_BUS,
728  * CAM_DEBUG_TARGET, and CAM_DEBUG_LUN specified.
729  */
730 #if defined(CAM_DEBUG_BUS) || defined(CAM_DEBUG_TARGET) \
731     || defined(CAM_DEBUG_LUN)
732 #ifdef CAMDEBUG
733 #if !defined(CAM_DEBUG_BUS) || !defined(CAM_DEBUG_TARGET) \
734     || !defined(CAM_DEBUG_LUN)
735 #error "You must define all or none of CAM_DEBUG_BUS, CAM_DEBUG_TARGET \
736         and CAM_DEBUG_LUN"
737 #endif /* !CAM_DEBUG_BUS || !CAM_DEBUG_TARGET || !CAM_DEBUG_LUN */
738 #else /* !CAMDEBUG */
739 #error "You must use options CAMDEBUG if you use the CAM_DEBUG_* options"
740 #endif /* CAMDEBUG */
741 #endif /* CAM_DEBUG_BUS || CAM_DEBUG_TARGET || CAM_DEBUG_LUN */
742 
743 /* Our boot-time initialization hook */
744 static int cam_module_event_handler(module_t, int /*modeventtype_t*/, void *);
745 
746 static moduledata_t cam_moduledata = {
747         "cam",
748         cam_module_event_handler,
749         NULL
750 };
751 
752 static int      xpt_init(void *);
753 
754 DECLARE_MODULE(cam, cam_moduledata, SI_SUB_CONFIGURE, SI_ORDER_SECOND);
755 MODULE_VERSION(cam, 1);
756 
757 
758 static cam_status       xpt_compile_path(struct cam_path *new_path,
759                                          struct cam_periph *perph,
760                                          path_id_t path_id,
761                                          target_id_t target_id,
762                                          lun_id_t lun_id);
763 
764 static void             xpt_release_path(struct cam_path *path);
765 
766 static void             xpt_async_bcast(struct async_list *async_head,
767                                         u_int32_t async_code,
768                                         struct cam_path *path,
769                                         void *async_arg);
770 static void             xpt_dev_async(u_int32_t async_code,
771                                       struct cam_eb *bus,
772                                       struct cam_et *target,
773                                       struct cam_ed *device,
774                                       void *async_arg);
775 static path_id_t xptnextfreepathid(void);
776 static path_id_t xptpathid(const char *sim_name, int sim_unit, int sim_bus);
777 static union ccb *xpt_get_ccb(struct cam_ed *device);
778 static int       xpt_schedule_dev(struct camq *queue, cam_pinfo *dev_pinfo,
779                                   u_int32_t new_priority);
780 static void      xpt_run_dev_allocq(struct cam_eb *bus);
781 static void      xpt_run_dev_sendq(struct cam_eb *bus);
782 static timeout_t xpt_release_devq_timeout;
783 static void      xpt_release_simq_timeout(void *arg) __unused;
784 static void      xpt_release_bus(struct cam_eb *bus);
785 static void      xpt_release_devq_device(struct cam_ed *dev, u_int count,
786                                          int run_queue);
787 static struct cam_et*
788                  xpt_alloc_target(struct cam_eb *bus, target_id_t target_id);
789 static void      xpt_release_target(struct cam_eb *bus, struct cam_et *target);
790 static struct cam_ed*
791                  xpt_alloc_device(struct cam_eb *bus, struct cam_et *target,
792                                   lun_id_t lun_id);
793 static void      xpt_release_device(struct cam_eb *bus, struct cam_et *target,
794                                     struct cam_ed *device);
795 static u_int32_t xpt_dev_ccbq_resize(struct cam_path *path, int newopenings);
796 static struct cam_eb*
797                  xpt_find_bus(path_id_t path_id);
798 static struct cam_et*
799                  xpt_find_target(struct cam_eb *bus, target_id_t target_id);
800 static struct cam_ed*
801                  xpt_find_device(struct cam_et *target, lun_id_t lun_id);
802 static void      xpt_scan_bus(struct cam_periph *periph, union ccb *ccb);
803 static void      xpt_scan_lun(struct cam_periph *periph,
804                               struct cam_path *path, cam_flags flags,
805                               union ccb *ccb);
806 static void      xptscandone(struct cam_periph *periph, union ccb *done_ccb);
807 static xpt_busfunc_t    xptconfigbuscountfunc;
808 static xpt_busfunc_t    xptconfigfunc;
809 static void      xpt_config(void *arg);
810 static xpt_devicefunc_t xptpassannouncefunc;
811 static void      xpt_finishconfig(struct cam_periph *periph, union ccb *ccb);
812 static void      xptaction(struct cam_sim *sim, union ccb *work_ccb);
813 static void      xptpoll(struct cam_sim *sim);
814 static void      camisr(void *);
815 static void      camisr_runqueue(void *);
816 static dev_match_ret    xptbusmatch(struct dev_match_pattern *patterns,
817                                     u_int num_patterns, struct cam_eb *bus);
818 static dev_match_ret    xptdevicematch(struct dev_match_pattern *patterns,
819                                        u_int num_patterns,
820                                        struct cam_ed *device);
821 static dev_match_ret    xptperiphmatch(struct dev_match_pattern *patterns,
822                                        u_int num_patterns,
823                                        struct cam_periph *periph);
824 static xpt_busfunc_t    xptedtbusfunc;
825 static xpt_targetfunc_t xptedttargetfunc;
826 static xpt_devicefunc_t xptedtdevicefunc;
827 static xpt_periphfunc_t xptedtperiphfunc;
828 static xpt_pdrvfunc_t   xptplistpdrvfunc;
829 static xpt_periphfunc_t xptplistperiphfunc;
830 static int              xptedtmatch(struct ccb_dev_match *cdm);
831 static int              xptperiphlistmatch(struct ccb_dev_match *cdm);
832 static int              xptbustraverse(struct cam_eb *start_bus,
833                                        xpt_busfunc_t *tr_func, void *arg);
834 static int              xpttargettraverse(struct cam_eb *bus,
835                                           struct cam_et *start_target,
836                                           xpt_targetfunc_t *tr_func, void *arg);
837 static int              xptdevicetraverse(struct cam_et *target,
838                                           struct cam_ed *start_device,
839                                           xpt_devicefunc_t *tr_func, void *arg);
840 static int              xptperiphtraverse(struct cam_ed *device,
841                                           struct cam_periph *start_periph,
842                                           xpt_periphfunc_t *tr_func, void *arg);
843 static int              xptpdrvtraverse(struct periph_driver **start_pdrv,
844                                         xpt_pdrvfunc_t *tr_func, void *arg);
845 static int              xptpdperiphtraverse(struct periph_driver **pdrv,
846                                             struct cam_periph *start_periph,
847                                             xpt_periphfunc_t *tr_func,
848                                             void *arg);
849 static xpt_busfunc_t    xptdefbusfunc;
850 static xpt_targetfunc_t xptdeftargetfunc;
851 static xpt_devicefunc_t xptdefdevicefunc;
852 static xpt_periphfunc_t xptdefperiphfunc;
853 static int              xpt_for_all_busses(xpt_busfunc_t *tr_func, void *arg);
854 static int              xpt_for_all_devices(xpt_devicefunc_t *tr_func,
855                                             void *arg);
856 static xpt_devicefunc_t xptsetasyncfunc;
857 static xpt_busfunc_t    xptsetasyncbusfunc;
858 static cam_status       xptregister(struct cam_periph *periph,
859                                     void *arg);
860 static cam_status       proberegister(struct cam_periph *periph,
861                                       void *arg);
862 static void      probeschedule(struct cam_periph *probe_periph);
863 static void      probestart(struct cam_periph *periph, union ccb *start_ccb);
864 static void      proberequestdefaultnegotiation(struct cam_periph *periph);
865 static int       proberequestbackoff(struct cam_periph *periph,
866                                      struct cam_ed *device);
867 static void      probedone(struct cam_periph *periph, union ccb *done_ccb);
868 static void      probecleanup(struct cam_periph *periph);
869 static void      xpt_find_quirk(struct cam_ed *device);
870 static void      xpt_devise_transport(struct cam_path *path);
871 static void      xpt_set_transfer_settings(struct ccb_trans_settings *cts,
872                                            struct cam_ed *device,
873                                            int async_update);
874 static void      xpt_toggle_tags(struct cam_path *path);
875 static void      xpt_start_tags(struct cam_path *path);
876 static __inline int xpt_schedule_dev_allocq(struct cam_eb *bus,
877                                             struct cam_ed *dev);
878 static __inline int xpt_schedule_dev_sendq(struct cam_eb *bus,
879                                            struct cam_ed *dev);
880 static __inline int periph_is_queued(struct cam_periph *periph);
881 static __inline int device_is_alloc_queued(struct cam_ed *device);
882 static __inline int device_is_send_queued(struct cam_ed *device);
883 static __inline int dev_allocq_is_runnable(struct cam_devq *devq);
884 
885 static __inline int
886 xpt_schedule_dev_allocq(struct cam_eb *bus, struct cam_ed *dev)
887 {
888         int retval;
889 
890         if (dev->ccbq.devq_openings > 0) {
891                 if ((dev->flags & CAM_DEV_RESIZE_QUEUE_NEEDED) != 0) {
892                         cam_ccbq_resize(&dev->ccbq,
893                                         dev->ccbq.dev_openings
894                                         + dev->ccbq.dev_active);
895                         dev->flags &= ~CAM_DEV_RESIZE_QUEUE_NEEDED;
896                 }
897                 /*
898                  * The priority of a device waiting for CCB resources
899                  * is that of the the highest priority peripheral driver
900                  * enqueued.
901                  */
902                 retval = xpt_schedule_dev(&bus->sim->devq->alloc_queue,
903                                           &dev->alloc_ccb_entry.pinfo,
904                                           CAMQ_GET_HEAD(&dev->drvq)->priority);
905         } else {
906                 retval = 0;
907         }
908 
909         return (retval);
910 }
911 
912 static __inline int
913 xpt_schedule_dev_sendq(struct cam_eb *bus, struct cam_ed *dev)
914 {
915         int     retval;
916 
917         if (dev->ccbq.dev_openings > 0) {
918                 /*
919                  * The priority of a device waiting for controller
920                  * resources is that of the the highest priority CCB
921                  * enqueued.
922                  */
923                 retval =
924                     xpt_schedule_dev(&bus->