xref: /freebsd/sys/dev/ciss/ciss.c (revision ec5d9810da1ea3a571007353f4e028e80dc5683d)
1 /*-
2  * Copyright (c) 2001 Michael Smith
3  * Copyright (c) 2004 Paul Saab
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  * 2. Redistributions in binary form must reproduce the above copyright
12  *    notice, this list of conditions and the following disclaimer in the
13  *    documentation and/or other materials provided with the distribution.
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
19  * FOR 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$
28  */
29 
30 /*
31  * Common Interface for SCSI-3 Support driver.
32  *
33  * CISS claims to provide a common interface between a generic SCSI
34  * transport and an intelligent host adapter.
35  *
36  * This driver supports CISS as defined in the document "CISS Command
37  * Interface for SCSI-3 Support Open Specification", Version 1.04,
38  * Valence Number 1, dated 20001127, produced by Compaq Computer
39  * Corporation.  This document appears to be a hastily and somewhat
40  * arbitrarlily cut-down version of a larger (and probably even more
41  * chaotic and inconsistent) Compaq internal document.  Various
42  * details were also gleaned from Compaq's "cciss" driver for Linux.
43  *
44  * We provide a shim layer between the CISS interface and CAM,
45  * offloading most of the queueing and being-a-disk chores onto CAM.
46  * Entry to the driver is via the PCI bus attachment (ciss_probe,
47  * ciss_attach, etc) and via the CAM interface (ciss_cam_action,
48  * ciss_cam_poll).  The Compaq CISS adapters are, however, poor SCSI
49  * citizens and we have to fake up some responses to get reasonable
50  * behaviour out of them.  In addition, the CISS command set is by no
51  * means adequate to support the functionality of a RAID controller,
52  * and thus the supported Compaq adapters utilise portions of the
53  * control protocol from earlier Compaq adapter families.
54  *
55  * Note that we only support the "simple" transport layer over PCI.
56  * This interface (ab)uses the I2O register set (specifically the post
57  * queues) to exchange commands with the adapter.  Other interfaces
58  * are available, but we aren't supposed to know about them, and it is
59  * dubious whether they would provide major performance improvements
60  * except under extreme load.
61  *
62  * Currently the only supported CISS adapters are the Compaq Smart
63  * Array 5* series (5300, 5i, 532).  Even with only three adapters,
64  * Compaq still manage to have interface variations.
65  *
66  *
67  * Thanks must go to Fred Harris and Darryl DeVinney at Compaq, as
68  * well as Paul Saab at Yahoo! for their assistance in making this
69  * driver happen.
70  *
71  * More thanks must go to John Cagle at HP for the countless hours
72  * spent making this driver "work" with the MSA* series storage
73  * enclosures.  Without his help (and nagging), this driver could not
74  * be used with these enclosures.
75  */
76 
77 #include <sys/param.h>
78 #include <sys/systm.h>
79 #include <sys/malloc.h>
80 #include <sys/kernel.h>
81 #include <sys/bus.h>
82 #include <sys/conf.h>
83 #include <sys/stat.h>
84 #include <sys/kthread.h>
85 #include <sys/queue.h>
86 #include <sys/sysctl.h>
87 
88 #include <cam/cam.h>
89 #include <cam/cam_ccb.h>
90 #include <cam/cam_periph.h>
91 #include <cam/cam_sim.h>
92 #include <cam/cam_xpt_sim.h>
93 #include <cam/scsi/scsi_all.h>
94 #include <cam/scsi/scsi_message.h>
95 
96 #include <machine/bus.h>
97 #include <machine/endian.h>
98 #include <machine/resource.h>
99 #include <sys/rman.h>
100 
101 #include <dev/pci/pcireg.h>
102 #include <dev/pci/pcivar.h>
103 
104 #include <dev/ciss/cissreg.h>
105 #include <dev/ciss/cissio.h>
106 #include <dev/ciss/cissvar.h>
107 
108 static MALLOC_DEFINE(CISS_MALLOC_CLASS, "ciss_data",
109     "ciss internal data buffers");
110 
111 /* pci interface */
112 static int	ciss_lookup(device_t dev);
113 static int	ciss_probe(device_t dev);
114 static int	ciss_attach(device_t dev);
115 static int	ciss_detach(device_t dev);
116 static int	ciss_shutdown(device_t dev);
117 
118 /* (de)initialisation functions, control wrappers */
119 static int	ciss_init_pci(struct ciss_softc *sc);
120 static int	ciss_setup_msix(struct ciss_softc *sc);
121 static int	ciss_init_perf(struct ciss_softc *sc);
122 static int	ciss_wait_adapter(struct ciss_softc *sc);
123 static int	ciss_flush_adapter(struct ciss_softc *sc);
124 static int	ciss_init_requests(struct ciss_softc *sc);
125 static void	ciss_command_map_helper(void *arg, bus_dma_segment_t *segs,
126 					int nseg, int error);
127 static int	ciss_identify_adapter(struct ciss_softc *sc);
128 static int	ciss_init_logical(struct ciss_softc *sc);
129 static int	ciss_init_physical(struct ciss_softc *sc);
130 static int	ciss_filter_physical(struct ciss_softc *sc, struct ciss_lun_report *cll);
131 static int	ciss_identify_logical(struct ciss_softc *sc, struct ciss_ldrive *ld);
132 static int	ciss_get_ldrive_status(struct ciss_softc *sc,  struct ciss_ldrive *ld);
133 static int	ciss_update_config(struct ciss_softc *sc);
134 static int	ciss_accept_media(struct ciss_softc *sc, struct ciss_ldrive *ld);
135 static void	ciss_init_sysctl(struct ciss_softc *sc);
136 static void	ciss_soft_reset(struct ciss_softc *sc);
137 static void	ciss_free(struct ciss_softc *sc);
138 static void	ciss_spawn_notify_thread(struct ciss_softc *sc);
139 static void	ciss_kill_notify_thread(struct ciss_softc *sc);
140 
141 /* request submission/completion */
142 static int	ciss_start(struct ciss_request *cr);
143 static void	ciss_done(struct ciss_softc *sc, cr_qhead_t *qh);
144 static void	ciss_perf_done(struct ciss_softc *sc, cr_qhead_t *qh);
145 static void	ciss_intr(void *arg);
146 static void	ciss_perf_intr(void *arg);
147 static void	ciss_perf_msi_intr(void *arg);
148 static void	ciss_complete(struct ciss_softc *sc, cr_qhead_t *qh);
149 static int	_ciss_report_request(struct ciss_request *cr, int *command_status, int *scsi_status, const char *func);
150 static int	ciss_synch_request(struct ciss_request *cr, int timeout);
151 static int	ciss_poll_request(struct ciss_request *cr, int timeout);
152 static int	ciss_wait_request(struct ciss_request *cr, int timeout);
153 #if 0
154 static int	ciss_abort_request(struct ciss_request *cr);
155 #endif
156 
157 /* request queueing */
158 static int	ciss_get_request(struct ciss_softc *sc, struct ciss_request **crp);
159 static void	ciss_preen_command(struct ciss_request *cr);
160 static void 	ciss_release_request(struct ciss_request *cr);
161 
162 /* request helpers */
163 static int	ciss_get_bmic_request(struct ciss_softc *sc, struct ciss_request **crp,
164 				      int opcode, void **bufp, size_t bufsize);
165 static int	ciss_user_command(struct ciss_softc *sc, IOCTL_Command_struct *ioc);
166 
167 /* DMA map/unmap */
168 static int	ciss_map_request(struct ciss_request *cr);
169 static void	ciss_request_map_helper(void *arg, bus_dma_segment_t *segs,
170 					int nseg, int error);
171 static void	ciss_unmap_request(struct ciss_request *cr);
172 
173 /* CAM interface */
174 static int	ciss_cam_init(struct ciss_softc *sc);
175 static void	ciss_cam_rescan_target(struct ciss_softc *sc,
176 				       int bus, int target);
177 static void	ciss_cam_action(struct cam_sim *sim, union ccb *ccb);
178 static int	ciss_cam_action_io(struct cam_sim *sim, struct ccb_scsiio *csio);
179 static int	ciss_cam_emulate(struct ciss_softc *sc, struct ccb_scsiio *csio);
180 static void	ciss_cam_poll(struct cam_sim *sim);
181 static void	ciss_cam_complete(struct ciss_request *cr);
182 static void	ciss_cam_complete_fixup(struct ciss_softc *sc, struct ccb_scsiio *csio);
183 static struct cam_periph *ciss_find_periph(struct ciss_softc *sc,
184 					   int bus, int target);
185 static int	ciss_name_device(struct ciss_softc *sc, int bus, int target);
186 
187 /* periodic status monitoring */
188 static void	ciss_periodic(void *arg);
189 static void	ciss_nop_complete(struct ciss_request *cr);
190 static void	ciss_disable_adapter(struct ciss_softc *sc);
191 static void	ciss_notify_event(struct ciss_softc *sc);
192 static void	ciss_notify_complete(struct ciss_request *cr);
193 static int	ciss_notify_abort(struct ciss_softc *sc);
194 static int	ciss_notify_abort_bmic(struct ciss_softc *sc);
195 static void	ciss_notify_hotplug(struct ciss_softc *sc, struct ciss_notify *cn);
196 static void	ciss_notify_logical(struct ciss_softc *sc, struct ciss_notify *cn);
197 static void	ciss_notify_physical(struct ciss_softc *sc, struct ciss_notify *cn);
198 
199 /* debugging output */
200 static void	ciss_print_request(struct ciss_request *cr);
201 static void	ciss_print_ldrive(struct ciss_softc *sc, struct ciss_ldrive *ld);
202 static const char *ciss_name_ldrive_status(int status);
203 static int	ciss_decode_ldrive_status(int status);
204 static const char *ciss_name_ldrive_org(int org);
205 static const char *ciss_name_command_status(int status);
206 
207 /*
208  * PCI bus interface.
209  */
210 static device_method_t ciss_methods[] = {
211     /* Device interface */
212     DEVMETHOD(device_probe,	ciss_probe),
213     DEVMETHOD(device_attach,	ciss_attach),
214     DEVMETHOD(device_detach,	ciss_detach),
215     DEVMETHOD(device_shutdown,	ciss_shutdown),
216     { 0, 0 }
217 };
218 
219 static driver_t ciss_pci_driver = {
220     "ciss",
221     ciss_methods,
222     sizeof(struct ciss_softc)
223 };
224 
225 static devclass_t	ciss_devclass;
226 DRIVER_MODULE(ciss, pci, ciss_pci_driver, ciss_devclass, 0, 0);
227 MODULE_DEPEND(ciss, cam, 1, 1, 1);
228 MODULE_DEPEND(ciss, pci, 1, 1, 1);
229 
230 /*
231  * Control device interface.
232  */
233 static d_open_t		ciss_open;
234 static d_close_t	ciss_close;
235 static d_ioctl_t	ciss_ioctl;
236 
237 static struct cdevsw ciss_cdevsw = {
238 	.d_version =	D_VERSION,
239 	.d_flags =	0,
240 	.d_open =	ciss_open,
241 	.d_close =	ciss_close,
242 	.d_ioctl =	ciss_ioctl,
243 	.d_name =	"ciss",
244 };
245 
246 /*
247  * This tunable can be set at boot time and controls whether physical devices
248  * that are marked hidden by the firmware should be exposed anyways.
249  */
250 static unsigned int ciss_expose_hidden_physical = 0;
251 TUNABLE_INT("hw.ciss.expose_hidden_physical", &ciss_expose_hidden_physical);
252 
253 static unsigned int ciss_nop_message_heartbeat = 0;
254 TUNABLE_INT("hw.ciss.nop_message_heartbeat", &ciss_nop_message_heartbeat);
255 
256 /*
257  * This tunable can force a particular transport to be used:
258  * <= 0 : use default
259  *    1 : force simple
260  *    2 : force performant
261  */
262 static int ciss_force_transport = 0;
263 TUNABLE_INT("hw.ciss.force_transport", &ciss_force_transport);
264 
265 /*
266  * This tunable can force a particular interrupt delivery method to be used:
267  * <= 0 : use default
268  *    1 : force INTx
269  *    2 : force MSIX
270  */
271 static int ciss_force_interrupt = 0;
272 TUNABLE_INT("hw.ciss.force_interrupt", &ciss_force_interrupt);
273 
274 /************************************************************************
275  * CISS adapters amazingly don't have a defined programming interface
276  * value.  (One could say some very despairing things about PCI and
277  * people just not getting the general idea.)  So we are forced to
278  * stick with matching against subvendor/subdevice, and thus have to
279  * be updated for every new CISS adapter that appears.
280  */
281 #define CISS_BOARD_UNKNWON	0
282 #define CISS_BOARD_SA5		1
283 #define CISS_BOARD_SA5B		2
284 #define CISS_BOARD_NOMSI	(1<<4)
285 #define CISS_BOARD_SIMPLE       (1<<5)
286 
287 static struct
288 {
289     u_int16_t	subvendor;
290     u_int16_t	subdevice;
291     int		flags;
292     char	*desc;
293 } ciss_vendor_data[] = {
294     { 0x0e11, 0x4070, CISS_BOARD_SA5|CISS_BOARD_NOMSI|CISS_BOARD_SIMPLE,
295                                                         "Compaq Smart Array 5300" },
296     { 0x0e11, 0x4080, CISS_BOARD_SA5B|CISS_BOARD_NOMSI,	"Compaq Smart Array 5i" },
297     { 0x0e11, 0x4082, CISS_BOARD_SA5B|CISS_BOARD_NOMSI,	"Compaq Smart Array 532" },
298     { 0x0e11, 0x4083, CISS_BOARD_SA5B|CISS_BOARD_NOMSI,	"HP Smart Array 5312" },
299     { 0x0e11, 0x4091, CISS_BOARD_SA5,	"HP Smart Array 6i" },
300     { 0x0e11, 0x409A, CISS_BOARD_SA5,	"HP Smart Array 641" },
301     { 0x0e11, 0x409B, CISS_BOARD_SA5,	"HP Smart Array 642" },
302     { 0x0e11, 0x409C, CISS_BOARD_SA5,	"HP Smart Array 6400" },
303     { 0x0e11, 0x409D, CISS_BOARD_SA5,	"HP Smart Array 6400 EM" },
304     { 0x103C, 0x3211, CISS_BOARD_SA5,	"HP Smart Array E200i" },
305     { 0x103C, 0x3212, CISS_BOARD_SA5,	"HP Smart Array E200" },
306     { 0x103C, 0x3213, CISS_BOARD_SA5,	"HP Smart Array E200i" },
307     { 0x103C, 0x3214, CISS_BOARD_SA5,	"HP Smart Array E200i" },
308     { 0x103C, 0x3215, CISS_BOARD_SA5,	"HP Smart Array E200i" },
309     { 0x103C, 0x3220, CISS_BOARD_SA5,	"HP Smart Array" },
310     { 0x103C, 0x3222, CISS_BOARD_SA5,	"HP Smart Array" },
311     { 0x103C, 0x3223, CISS_BOARD_SA5,	"HP Smart Array P800" },
312     { 0x103C, 0x3225, CISS_BOARD_SA5,	"HP Smart Array P600" },
313     { 0x103C, 0x3230, CISS_BOARD_SA5,	"HP Smart Array" },
314     { 0x103C, 0x3231, CISS_BOARD_SA5,	"HP Smart Array" },
315     { 0x103C, 0x3232, CISS_BOARD_SA5,	"HP Smart Array" },
316     { 0x103C, 0x3233, CISS_BOARD_SA5,	"HP Smart Array" },
317     { 0x103C, 0x3234, CISS_BOARD_SA5,	"HP Smart Array P400" },
318     { 0x103C, 0x3235, CISS_BOARD_SA5,	"HP Smart Array P400i" },
319     { 0x103C, 0x3236, CISS_BOARD_SA5,	"HP Smart Array" },
320     { 0x103C, 0x3237, CISS_BOARD_SA5,	"HP Smart Array E500" },
321     { 0x103C, 0x3238, CISS_BOARD_SA5,	"HP Smart Array" },
322     { 0x103C, 0x3239, CISS_BOARD_SA5,	"HP Smart Array" },
323     { 0x103C, 0x323A, CISS_BOARD_SA5,	"HP Smart Array" },
324     { 0x103C, 0x323B, CISS_BOARD_SA5,	"HP Smart Array" },
325     { 0x103C, 0x323C, CISS_BOARD_SA5,	"HP Smart Array" },
326     { 0x103C, 0x323D, CISS_BOARD_SA5,	"HP Smart Array P700m" },
327     { 0x103C, 0x3241, CISS_BOARD_SA5,	"HP Smart Array P212" },
328     { 0x103C, 0x3243, CISS_BOARD_SA5,	"HP Smart Array P410" },
329     { 0x103C, 0x3245, CISS_BOARD_SA5,	"HP Smart Array P410i" },
330     { 0x103C, 0x3247, CISS_BOARD_SA5,	"HP Smart Array P411" },
331     { 0x103C, 0x3249, CISS_BOARD_SA5,	"HP Smart Array P812" },
332     { 0x103C, 0x324A, CISS_BOARD_SA5,	"HP Smart Array P712m" },
333     { 0x103C, 0x324B, CISS_BOARD_SA5,	"HP Smart Array" },
334     { 0x103C, 0x3350, CISS_BOARD_SA5,   "HP Smart Array P222" },
335     { 0x103C, 0x3351, CISS_BOARD_SA5,   "HP Smart Array P420" },
336     { 0x103C, 0x3352, CISS_BOARD_SA5,   "HP Smart Array P421" },
337     { 0x103C, 0x3353, CISS_BOARD_SA5,   "HP Smart Array P822" },
338     { 0x103C, 0x3354, CISS_BOARD_SA5,   "HP Smart Array P420i" },
339     { 0x103C, 0x3355, CISS_BOARD_SA5,   "HP Smart Array P220i" },
340     { 0x103C, 0x3356, CISS_BOARD_SA5,   "HP Smart Array P721m" },
341     { 0x103C, 0x1920, CISS_BOARD_SA5,   "HP Smart Array P430i" },
342     { 0x103C, 0x1921, CISS_BOARD_SA5,   "HP Smart Array P830i" },
343     { 0x103C, 0x1922, CISS_BOARD_SA5,   "HP Smart Array P430" },
344     { 0x103C, 0x1923, CISS_BOARD_SA5,   "HP Smart Array P431" },
345     { 0x103C, 0x1924, CISS_BOARD_SA5,   "HP Smart Array P830" },
346     { 0x103C, 0x1926, CISS_BOARD_SA5,   "HP Smart Array P731m" },
347     { 0x103C, 0x1928, CISS_BOARD_SA5,   "HP Smart Array P230i" },
348     { 0x103C, 0x1929, CISS_BOARD_SA5,   "HP Smart Array P530" },
349     { 0x103C, 0x192A, CISS_BOARD_SA5,   "HP Smart Array P531" },
350     { 0, 0, 0, NULL }
351 };
352 
353 /************************************************************************
354  * Find a match for the device in our list of known adapters.
355  */
356 static int
357 ciss_lookup(device_t dev)
358 {
359     int 	i;
360 
361     for (i = 0; ciss_vendor_data[i].desc != NULL; i++)
362 	if ((pci_get_subvendor(dev) == ciss_vendor_data[i].subvendor) &&
363 	    (pci_get_subdevice(dev) == ciss_vendor_data[i].subdevice)) {
364 	    return(i);
365 	}
366     return(-1);
367 }
368 
369 /************************************************************************
370  * Match a known CISS adapter.
371  */
372 static int
373 ciss_probe(device_t dev)
374 {
375     int		i;
376 
377     i = ciss_lookup(dev);
378     if (i != -1) {
379 	device_set_desc(dev, ciss_vendor_data[i].desc);
380 	return(BUS_PROBE_DEFAULT);
381     }
382     return(ENOENT);
383 }
384 
385 /************************************************************************
386  * Attach the driver to this adapter.
387  */
388 static int
389 ciss_attach(device_t dev)
390 {
391     struct ciss_softc	*sc;
392     int			error;
393 
394     debug_called(1);
395 
396 #ifdef CISS_DEBUG
397     /* print structure/union sizes */
398     debug_struct(ciss_command);
399     debug_struct(ciss_header);
400     debug_union(ciss_device_address);
401     debug_struct(ciss_cdb);
402     debug_struct(ciss_report_cdb);
403     debug_struct(ciss_notify_cdb);
404     debug_struct(ciss_notify);
405     debug_struct(ciss_message_cdb);
406     debug_struct(ciss_error_info_pointer);
407     debug_struct(ciss_error_info);
408     debug_struct(ciss_sg_entry);
409     debug_struct(ciss_config_table);
410     debug_struct(ciss_bmic_cdb);
411     debug_struct(ciss_bmic_id_ldrive);
412     debug_struct(ciss_bmic_id_lstatus);
413     debug_struct(ciss_bmic_id_table);
414     debug_struct(ciss_bmic_id_pdrive);
415     debug_struct(ciss_bmic_blink_pdrive);
416     debug_struct(ciss_bmic_flush_cache);
417     debug_const(CISS_MAX_REQUESTS);
418     debug_const(CISS_MAX_LOGICAL);
419     debug_const(CISS_INTERRUPT_COALESCE_DELAY);
420     debug_const(CISS_INTERRUPT_COALESCE_COUNT);
421     debug_const(CISS_COMMAND_ALLOC_SIZE);
422     debug_const(CISS_COMMAND_SG_LENGTH);
423 
424     debug_type(cciss_pci_info_struct);
425     debug_type(cciss_coalint_struct);
426     debug_type(cciss_coalint_struct);
427     debug_type(NodeName_type);
428     debug_type(NodeName_type);
429     debug_type(Heartbeat_type);
430     debug_type(BusTypes_type);
431     debug_type(FirmwareVer_type);
432     debug_type(DriverVer_type);
433     debug_type(IOCTL_Command_struct);
434 #endif
435 
436     sc = device_get_softc(dev);
437     sc->ciss_dev = dev;
438     mtx_init(&sc->ciss_mtx, "cissmtx", NULL, MTX_DEF);
439     callout_init_mtx(&sc->ciss_periodic, &sc->ciss_mtx, 0);
440 
441     /*
442      * Do PCI-specific init.
443      */
444     if ((error = ciss_init_pci(sc)) != 0)
445 	goto out;
446 
447     /*
448      * Initialise driver queues.
449      */
450     ciss_initq_free(sc);
451     ciss_initq_notify(sc);
452 
453     /*
454      * Initalize device sysctls.
455      */
456     ciss_init_sysctl(sc);
457 
458     /*
459      * Initialise command/request pool.
460      */
461     if ((error = ciss_init_requests(sc)) != 0)
462 	goto out;
463 
464     /*
465      * Get adapter information.
466      */
467     if ((error = ciss_identify_adapter(sc)) != 0)
468 	goto out;
469 
470     /*
471      * Find all the physical devices.
472      */
473     if ((error = ciss_init_physical(sc)) != 0)
474 	goto out;
475 
476     /*
477      * Build our private table of logical devices.
478      */
479     if ((error = ciss_init_logical(sc)) != 0)
480 	goto out;
481 
482     /*
483      * Enable interrupts so that the CAM scan can complete.
484      */
485     CISS_TL_SIMPLE_ENABLE_INTERRUPTS(sc);
486 
487     /*
488      * Initialise the CAM interface.
489      */
490     if ((error = ciss_cam_init(sc)) != 0)
491 	goto out;
492 
493     /*
494      * Start the heartbeat routine and event chain.
495      */
496     ciss_periodic(sc);
497 
498    /*
499      * Create the control device.
500      */
501     sc->ciss_dev_t = make_dev(&ciss_cdevsw, device_get_unit(sc->ciss_dev),
502 			      UID_ROOT, GID_OPERATOR, S_IRUSR | S_IWUSR,
503 			      "ciss%d", device_get_unit(sc->ciss_dev));
504     sc->ciss_dev_t->si_drv1 = sc;
505 
506     /*
507      * The adapter is running; synchronous commands can now sleep
508      * waiting for an interrupt to signal completion.
509      */
510     sc->ciss_flags |= CISS_FLAG_RUNNING;
511 
512     ciss_spawn_notify_thread(sc);
513 
514     error = 0;
515  out:
516     if (error != 0) {
517 	/* ciss_free() expects the mutex to be held */
518 	mtx_lock(&sc->ciss_mtx);
519 	ciss_free(sc);
520     }
521     return(error);
522 }
523 
524 /************************************************************************
525  * Detach the driver from this adapter.
526  */
527 static int
528 ciss_detach(device_t dev)
529 {
530     struct ciss_softc	*sc = device_get_softc(dev);
531 
532     debug_called(1);
533 
534     mtx_lock(&sc->ciss_mtx);
535     if (sc->ciss_flags & CISS_FLAG_CONTROL_OPEN) {
536 	mtx_unlock(&sc->ciss_mtx);
537 	return (EBUSY);
538     }
539 
540     /* flush adapter cache */
541     ciss_flush_adapter(sc);
542 
543     /* release all resources.  The mutex is released and freed here too. */
544     ciss_free(sc);
545 
546     return(0);
547 }
548 
549 /************************************************************************
550  * Prepare adapter for system shutdown.
551  */
552 static int
553 ciss_shutdown(device_t dev)
554 {
555     struct ciss_softc	*sc = device_get_softc(dev);
556 
557     debug_called(1);
558 
559     mtx_lock(&sc->ciss_mtx);
560     /* flush adapter cache */
561     ciss_flush_adapter(sc);
562 
563     if (sc->ciss_soft_reset)
564 	ciss_soft_reset(sc);
565     mtx_unlock(&sc->ciss_mtx);
566 
567     return(0);
568 }
569 
570 static void
571 ciss_init_sysctl(struct ciss_softc *sc)
572 {
573 
574     SYSCTL_ADD_INT(device_get_sysctl_ctx(sc->ciss_dev),
575 	SYSCTL_CHILDREN(device_get_sysctl_tree(sc->ciss_dev)),
576 	OID_AUTO, "soft_reset", CTLFLAG_RW, &sc->ciss_soft_reset, 0, "");
577 }
578 
579 /************************************************************************
580  * Perform PCI-specific attachment actions.
581  */
582 static int
583 ciss_init_pci(struct ciss_softc *sc)
584 {
585     uintptr_t		cbase, csize, cofs;
586     uint32_t		method, supported_methods;
587     int			error, sqmask, i;
588     void		*intr;
589 
590     debug_called(1);
591 
592     /*
593      * Work out adapter type.
594      */
595     i = ciss_lookup(sc->ciss_dev);
596     if (i < 0) {
597 	ciss_printf(sc, "unknown adapter type\n");
598 	return (ENXIO);
599     }
600 
601     if (ciss_vendor_data[i].flags & CISS_BOARD_SA5) {
602 	sqmask = CISS_TL_SIMPLE_INTR_OPQ_SA5;
603     } else if (ciss_vendor_data[i].flags & CISS_BOARD_SA5B) {
604 	sqmask = CISS_TL_SIMPLE_INTR_OPQ_SA5B;
605     } else {
606 	/*
607 	 * XXX Big hammer, masks/unmasks all possible interrupts.  This should
608 	 * work on all hardware variants.  Need to add code to handle the
609 	 * "controller crashed" interupt bit that this unmasks.
610 	 */
611 	sqmask = ~0;
612     }
613 
614     /*
615      * Allocate register window first (we need this to find the config
616      * struct).
617      */
618     error = ENXIO;
619     sc->ciss_regs_rid = CISS_TL_SIMPLE_BAR_REGS;
620     if ((sc->ciss_regs_resource =
621 	 bus_alloc_resource_any(sc->ciss_dev, SYS_RES_MEMORY,
622 				&sc->ciss_regs_rid, RF_ACTIVE)) == NULL) {
623 	ciss_printf(sc, "can't allocate register window\n");
624 	return(ENXIO);
625     }
626     sc->ciss_regs_bhandle = rman_get_bushandle(sc->ciss_regs_resource);
627     sc->ciss_regs_btag = rman_get_bustag(sc->ciss_regs_resource);
628 
629     /*
630      * Find the BAR holding the config structure.  If it's not the one
631      * we already mapped for registers, map it too.
632      */
633     sc->ciss_cfg_rid = CISS_TL_SIMPLE_READ(sc, CISS_TL_SIMPLE_CFG_BAR) & 0xffff;
634     if (sc->ciss_cfg_rid != sc->ciss_regs_rid) {
635 	if ((sc->ciss_cfg_resource =
636 	     bus_alloc_resource_any(sc->ciss_dev, SYS_RES_MEMORY,
637 				    &sc->ciss_cfg_rid, RF_ACTIVE)) == NULL) {
638 	    ciss_printf(sc, "can't allocate config window\n");
639 	    return(ENXIO);
640 	}
641 	cbase = (uintptr_t)rman_get_virtual(sc->ciss_cfg_resource);
642 	csize = rman_get_end(sc->ciss_cfg_resource) -
643 	    rman_get_start(sc->ciss_cfg_resource) + 1;
644     } else {
645 	cbase = (uintptr_t)rman_get_virtual(sc->ciss_regs_resource);
646 	csize = rman_get_end(sc->ciss_regs_resource) -
647 	    rman_get_start(sc->ciss_regs_resource) + 1;
648     }
649     cofs = CISS_TL_SIMPLE_READ(sc, CISS_TL_SIMPLE_CFG_OFF);
650 
651     /*
652      * Use the base/size/offset values we just calculated to
653      * sanity-check the config structure.  If it's OK, point to it.
654      */
655     if ((cofs + sizeof(struct ciss_config_table)) > csize) {
656 	ciss_printf(sc, "config table outside window\n");
657 	return(ENXIO);
658     }
659     sc->ciss_cfg = (struct ciss_config_table *)(cbase + cofs);
660     debug(1, "config struct at %p", sc->ciss_cfg);
661 
662     /*
663      * Calculate the number of request structures/commands we are
664      * going to provide for this adapter.
665      */
666     sc->ciss_max_requests = min(CISS_MAX_REQUESTS, sc->ciss_cfg->max_outstanding_commands);
667 
668     /*
669      * Validate the config structure.  If we supported other transport
670      * methods, we could select amongst them at this point in time.
671      */
672     if (strncmp(sc->ciss_cfg->signature, "CISS", 4)) {
673 	ciss_printf(sc, "config signature mismatch (got '%c%c%c%c')\n",
674 		    sc->ciss_cfg->signature[0], sc->ciss_cfg->signature[1],
675 		    sc->ciss_cfg->signature[2], sc->ciss_cfg->signature[3]);
676 	return(ENXIO);
677     }
678 
679     /*
680      * Select the mode of operation, prefer Performant.
681      */
682     if (!(sc->ciss_cfg->supported_methods &
683 	(CISS_TRANSPORT_METHOD_SIMPLE | CISS_TRANSPORT_METHOD_PERF))) {
684 	ciss_printf(sc, "No supported transport layers: 0x%x\n",
685 	    sc->ciss_cfg->supported_methods);
686     }
687 
688     switch (ciss_force_transport) {
689     case 1:
690 	supported_methods = CISS_TRANSPORT_METHOD_SIMPLE;
691 	break;
692     case 2:
693 	supported_methods = CISS_TRANSPORT_METHOD_PERF;
694 	break;
695     default:
696         /*
697          * Override the capabilities of the BOARD and specify SIMPLE
698          * MODE
699          */
700         if (ciss_vendor_data[i].flags & CISS_BOARD_SIMPLE)
701                 supported_methods = CISS_TRANSPORT_METHOD_SIMPLE;
702         else
703                 supported_methods = sc->ciss_cfg->supported_methods;
704         break;
705     }
706 
707 setup:
708     if ((supported_methods & CISS_TRANSPORT_METHOD_PERF) != 0) {
709 	method = CISS_TRANSPORT_METHOD_PERF;
710 	sc->ciss_perf = (struct ciss_perf_config *)(cbase + cofs +
711 	    sc->ciss_cfg->transport_offset);
712 	if (ciss_init_perf(sc)) {
713 	    supported_methods &= ~method;
714 	    goto setup;
715 	}
716     } else if (supported_methods & CISS_TRANSPORT_METHOD_SIMPLE) {
717 	method = CISS_TRANSPORT_METHOD_SIMPLE;
718     } else {
719 	ciss_printf(sc, "No supported transport methods: 0x%x\n",
720 	    sc->ciss_cfg->supported_methods);
721 	return(ENXIO);
722     }
723 
724     /*
725      * Tell it we're using the low 4GB of RAM.  Set the default interrupt
726      * coalescing options.
727      */
728     sc->ciss_cfg->requested_method = method;
729     sc->ciss_cfg->command_physlimit = 0;
730     sc->ciss_cfg->interrupt_coalesce_delay = CISS_INTERRUPT_COALESCE_DELAY;
731     sc->ciss_cfg->interrupt_coalesce_count = CISS_INTERRUPT_COALESCE_COUNT;
732 
733 #ifdef __i386__
734     sc->ciss_cfg->host_driver |= CISS_DRIVER_SCSI_PREFETCH;
735 #endif
736 
737     if (ciss_update_config(sc)) {
738 	ciss_printf(sc, "adapter refuses to accept config update (IDBR 0x%x)\n",
739 		    CISS_TL_SIMPLE_READ(sc, CISS_TL_SIMPLE_IDBR));
740 	return(ENXIO);
741     }
742     if ((sc->ciss_cfg->active_method & method) == 0) {
743 	supported_methods &= ~method;
744 	if (supported_methods == 0) {
745 	    ciss_printf(sc, "adapter refuses to go into available transports "
746 		"mode (0x%x, 0x%x)\n", supported_methods,
747 		sc->ciss_cfg->active_method);
748 	    return(ENXIO);
749 	} else
750 	    goto setup;
751     }
752 
753     /*
754      * Wait for the adapter to come ready.
755      */
756     if ((error = ciss_wait_adapter(sc)) != 0)
757 	return(error);
758 
759     /* Prepare to possibly use MSIX and/or PERFORMANT interrupts.  Normal
760      * interrupts have a rid of 0, this will be overridden if MSIX is used.
761      */
762     sc->ciss_irq_rid[0] = 0;
763     if (method == CISS_TRANSPORT_METHOD_PERF) {
764 	ciss_printf(sc, "PERFORMANT Transport\n");
765 	if ((ciss_force_interrupt != 1) && (ciss_setup_msix(sc) == 0)) {
766 	    intr = ciss_perf_msi_intr;
767 	} else {
768 	    intr = ciss_perf_intr;
769 	}
770 	/* XXX The docs say that the 0x01 bit is only for SAS controllers.
771 	 * Unfortunately, there is no good way to know if this is a SAS
772 	 * controller.  Hopefully enabling this bit universally will work OK.
773 	 * It seems to work fine for SA6i controllers.
774 	 */
775 	sc->ciss_interrupt_mask = CISS_TL_PERF_INTR_OPQ | CISS_TL_PERF_INTR_MSI;
776 
777     } else {
778 	ciss_printf(sc, "SIMPLE Transport\n");
779 	/* MSIX doesn't seem to work in SIMPLE mode, only enable if it forced */
780 	if (ciss_force_interrupt == 2)
781 	    /* If this fails, we automatically revert to INTx */
782 	    ciss_setup_msix(sc);
783 	sc->ciss_perf = NULL;
784 	intr = ciss_intr;
785 	sc->ciss_interrupt_mask = sqmask;
786     }
787 
788     /*
789      * Turn off interrupts before we go routing anything.
790      */
791     CISS_TL_SIMPLE_DISABLE_INTERRUPTS(sc);
792 
793     /*
794      * Allocate and set up our interrupt.
795      */
796     if ((sc->ciss_irq_resource =
797 	 bus_alloc_resource_any(sc->ciss_dev, SYS_RES_IRQ, &sc->ciss_irq_rid[0],
798 				RF_ACTIVE | RF_SHAREABLE)) == NULL) {
799 	ciss_printf(sc, "can't allocate interrupt\n");
800 	return(ENXIO);
801     }
802 
803     if (bus_setup_intr(sc->ciss_dev, sc->ciss_irq_resource,
804 		       INTR_TYPE_CAM|INTR_MPSAFE, NULL, intr, sc,
805 		       &sc->ciss_intr)) {
806 	ciss_printf(sc, "can't set up interrupt\n");
807 	return(ENXIO);
808     }
809 
810     /*
811      * Allocate the parent bus DMA tag appropriate for our PCI
812      * interface.
813      *
814      * Note that "simple" adapters can only address within a 32-bit
815      * span.
816      */
817     if (bus_dma_tag_create(bus_get_dma_tag(sc->ciss_dev),/* PCI parent */
818 			   1, 0, 			/* alignment, boundary */
819 			   BUS_SPACE_MAXADDR,		/* lowaddr */
820 			   BUS_SPACE_MAXADDR, 		/* highaddr */
821 			   NULL, NULL, 			/* filter, filterarg */
822 			   BUS_SPACE_MAXSIZE_32BIT,	/* maxsize */
823 			   CISS_MAX_SG_ELEMENTS,	/* nsegments */
824 			   BUS_SPACE_MAXSIZE_32BIT,	/* maxsegsize */
825 			   0,				/* flags */
826 			   NULL, NULL,			/* lockfunc, lockarg */
827 			   &sc->ciss_parent_dmat)) {
828 	ciss_printf(sc, "can't allocate parent DMA tag\n");
829 	return(ENOMEM);
830     }
831 
832     /*
833      * Create DMA tag for mapping buffers into adapter-addressable
834      * space.
835      */
836     if (bus_dma_tag_create(sc->ciss_parent_dmat, 	/* parent */
837 			   1, 0, 			/* alignment, boundary */
838 			   BUS_SPACE_MAXADDR,		/* lowaddr */
839 			   BUS_SPACE_MAXADDR, 		/* highaddr */
840 			   NULL, NULL, 			/* filter, filterarg */
841 			   MAXBSIZE, CISS_MAX_SG_ELEMENTS,	/* maxsize, nsegments */
842 			   BUS_SPACE_MAXSIZE_32BIT,	/* maxsegsize */
843 			   BUS_DMA_ALLOCNOW,		/* flags */
844 			   busdma_lock_mutex, &sc->ciss_mtx,	/* lockfunc, lockarg */
845 			   &sc->ciss_buffer_dmat)) {
846 	ciss_printf(sc, "can't allocate buffer DMA tag\n");
847 	return(ENOMEM);
848     }
849     return(0);
850 }
851 
852 /************************************************************************
853  * Setup MSI/MSIX operation (Performant only)
854  * Four interrupts are available, but we only use 1 right now.  If MSI-X
855  * isn't avaialble, try using MSI instead.
856  */
857 static int
858 ciss_setup_msix(struct ciss_softc *sc)
859 {
860     int val, i;
861 
862     /* Weed out devices that don't actually support MSI */
863     i = ciss_lookup(sc->ciss_dev);
864     if (ciss_vendor_data[i].flags & CISS_BOARD_NOMSI)
865 	return (EINVAL);
866 
867     /*
868      * Only need to use the minimum number of MSI vectors, as the driver
869      * doesn't support directed MSIX interrupts.
870      */
871     val = pci_msix_count(sc->ciss_dev);
872     if (val < CISS_MSI_COUNT) {
873 	val = pci_msi_count(sc->ciss_dev);
874 	device_printf(sc->ciss_dev, "got %d MSI messages]\n", val);
875 	if (val < CISS_MSI_COUNT)
876 	    return (EINVAL);
877     }
878     val = MIN(val, CISS_MSI_COUNT);
879     if (pci_alloc_msix(sc->ciss_dev, &val) != 0) {
880 	if (pci_alloc_msi(sc->ciss_dev, &val) != 0)
881 	    return (EINVAL);
882     }
883 
884     sc->ciss_msi = val;
885     if (bootverbose)
886 	ciss_printf(sc, "Using %d MSIX interrupt%s\n", val,
887 	    (val != 1) ? "s" : "");
888 
889     for (i = 0; i < val; i++)
890 	sc->ciss_irq_rid[i] = i + 1;
891 
892     return (0);
893 
894 }
895 
896 /************************************************************************
897  * Setup the Performant structures.
898  */
899 static int
900 ciss_init_perf(struct ciss_softc *sc)
901 {
902     struct ciss_perf_config *pc = sc->ciss_perf;
903     int reply_size;
904 
905     /*
906      * Create the DMA tag for the reply queue.
907      */
908     reply_size = sizeof(uint64_t) * sc->ciss_max_requests;
909     if (bus_dma_tag_create(sc->ciss_parent_dmat,	/* parent */
910 			   1, 0, 			/* alignment, boundary */
911 			   BUS_SPACE_MAXADDR_32BIT,	/* lowaddr */
912 			   BUS_SPACE_MAXADDR, 		/* highaddr */
913 			   NULL, NULL, 			/* filter, filterarg */
914 			   reply_size, 1,		/* maxsize, nsegments */
915 			   BUS_SPACE_MAXSIZE_32BIT,	/* maxsegsize */
916 			   0,				/* flags */
917 			   NULL, NULL,			/* lockfunc, lockarg */
918 			   &sc->ciss_reply_dmat)) {
919 	ciss_printf(sc, "can't allocate reply DMA tag\n");
920 	return(ENOMEM);
921     }
922     /*
923      * Allocate memory and make it available for DMA.
924      */
925     if (bus_dmamem_alloc(sc->ciss_reply_dmat, (void **)&sc->ciss_reply,
926 			 BUS_DMA_NOWAIT, &sc->ciss_reply_map)) {
927 	ciss_printf(sc, "can't allocate reply memory\n");
928 	return(ENOMEM);
929     }
930     bus_dmamap_load(sc->ciss_reply_dmat, sc->ciss_reply_map, sc->ciss_reply,
931 		    reply_size, ciss_command_map_helper, &sc->ciss_reply_phys, 0);
932     bzero(sc->ciss_reply, reply_size);
933 
934     sc->ciss_cycle = 0x1;
935     sc->ciss_rqidx = 0;
936 
937     /*
938      * Preload the fetch table with common command sizes.  This allows the
939      * hardware to not waste bus cycles for typical i/o commands, but also not
940      * tax the driver to be too exact in choosing sizes.  The table is optimized
941      * for page-aligned i/o's, but since most i/o comes from the various pagers,
942      * it's a reasonable assumption to make.
943      */
944     pc->fetch_count[CISS_SG_FETCH_NONE] = (sizeof(struct ciss_command) + 15) / 16;
945     pc->fetch_count[CISS_SG_FETCH_1] =
946 	(sizeof(struct ciss_command) + sizeof(struct ciss_sg_entry) * 1 + 15) / 16;
947     pc->fetch_count[CISS_SG_FETCH_2] =
948 	(sizeof(struct ciss_command) + sizeof(struct ciss_sg_entry) * 2 + 15) / 16;
949     pc->fetch_count[CISS_SG_FETCH_4] =
950 	(sizeof(struct ciss_command) + sizeof(struct ciss_sg_entry) * 4 + 15) / 16;
951     pc->fetch_count[CISS_SG_FETCH_8] =
952 	(sizeof(struct ciss_command) + sizeof(struct ciss_sg_entry) * 8 + 15) / 16;
953     pc->fetch_count[CISS_SG_FETCH_16] =
954 	(sizeof(struct ciss_command) + sizeof(struct ciss_sg_entry) * 16 + 15) / 16;
955     pc->fetch_count[CISS_SG_FETCH_32] =
956 	(sizeof(struct ciss_command) + sizeof(struct ciss_sg_entry) * 32 + 15) / 16;
957     pc->fetch_count[CISS_SG_FETCH_MAX] = (CISS_COMMAND_ALLOC_SIZE + 15) / 16;
958 
959     pc->rq_size = sc->ciss_max_requests; /* XXX less than the card supports? */
960     pc->rq_count = 1;	/* XXX Hardcode for a single queue */
961     pc->rq_bank_hi = 0;
962     pc->rq_bank_lo = 0;
963     pc->rq[0].rq_addr_hi = 0x0;
964     pc->rq[0].rq_addr_lo = sc->ciss_reply_phys;
965 
966     return(0);
967 }
968 
969 /************************************************************************
970  * Wait for the adapter to come ready.
971  */
972 static int
973 ciss_wait_adapter(struct ciss_softc *sc)
974 {
975     int		i;
976 
977     debug_called(1);
978 
979     /*
980      * Wait for the adapter to come ready.
981      */
982     if (!(sc->ciss_cfg->active_method & CISS_TRANSPORT_METHOD_READY)) {
983 	ciss_printf(sc, "waiting for adapter to come ready...\n");
984 	for (i = 0; !(sc->ciss_cfg->active_method & CISS_TRANSPORT_METHOD_READY); i++) {
985 	    DELAY(1000000);	/* one second */
986 	    if (i > 30) {
987 		ciss_printf(sc, "timed out waiting for adapter to come ready\n");
988 		return(EIO);
989 	    }
990 	}
991     }
992     return(0);
993 }
994 
995 /************************************************************************
996  * Flush the adapter cache.
997  */
998 static int
999 ciss_flush_adapter(struct ciss_softc *sc)
1000 {
1001     struct ciss_request			*cr;
1002     struct ciss_bmic_flush_cache	*cbfc;
1003     int					error, command_status;
1004 
1005     debug_called(1);
1006 
1007     cr = NULL;
1008     cbfc = NULL;
1009 
1010     /*
1011      * Build a BMIC request to flush the cache.  We don't disable
1012      * it, as we may be going to do more I/O (eg. we are emulating
1013      * the Synchronise Cache command).
1014      */
1015     if ((cbfc = malloc(sizeof(*cbfc), CISS_MALLOC_CLASS, M_NOWAIT | M_ZERO)) == NULL) {
1016 	error = ENOMEM;
1017 	goto out;
1018     }
1019     if ((error = ciss_get_bmic_request(sc, &cr, CISS_BMIC_FLUSH_CACHE,
1020 				       (void **)&cbfc, sizeof(*cbfc))) != 0)
1021 	goto out;
1022 
1023     /*
1024      * Submit the request and wait for it to complete.
1025      */
1026     if ((error = ciss_synch_request(cr, 60 * 1000)) != 0) {
1027 	ciss_printf(sc, "error sending BMIC FLUSH_CACHE command (%d)\n", error);
1028 	goto out;
1029     }
1030 
1031     /*
1032      * Check response.
1033      */
1034     ciss_report_request(cr, &command_status, NULL);
1035     switch(command_status) {
1036     case CISS_CMD_STATUS_SUCCESS:
1037 	break;
1038     default:
1039 	ciss_printf(sc, "error flushing cache (%s)\n",
1040 		    ciss_name_command_status(command_status));
1041 	error = EIO;
1042 	goto out;
1043     }
1044 
1045 out:
1046     if (cbfc != NULL)
1047 	free(cbfc, CISS_MALLOC_CLASS);
1048     if (cr != NULL)
1049 	ciss_release_request(cr);
1050     return(error);
1051 }
1052 
1053 static void
1054 ciss_soft_reset(struct ciss_softc *sc)
1055 {
1056     struct ciss_request		*cr = NULL;
1057     struct ciss_command		*cc;
1058     int				i, error = 0;
1059 
1060     for (i = 0; i < sc->ciss_max_logical_bus; i++) {
1061 	/* only reset proxy controllers */
1062 	if (sc->ciss_controllers[i].physical.bus == 0)
1063 	    continue;
1064 
1065 	if ((error = ciss_get_request(sc, &cr)) != 0)
1066 	    break;
1067 
1068 	if ((error = ciss_get_bmic_request(sc, &cr, CISS_BMIC_SOFT_RESET,
1069 					   NULL, 0)) != 0)
1070 	    break;
1071 
1072 	cc = cr->cr_cc;
1073 	cc->header.address = sc->ciss_controllers[i];
1074 
1075 	if ((error = ciss_synch_request(cr, 60 * 1000)) != 0)
1076 	    break;
1077 
1078 	ciss_release_request(cr);
1079     }
1080 
1081     if (error)
1082 	ciss_printf(sc, "error resetting controller (%d)\n", error);
1083 
1084     if (cr != NULL)
1085 	ciss_release_request(cr);
1086 }
1087 
1088 /************************************************************************
1089  * Allocate memory for the adapter command structures, initialise
1090  * the request structures.
1091  *
1092  * Note that the entire set of commands are allocated in a single
1093  * contiguous slab.
1094  */
1095 static int
1096 ciss_init_requests(struct ciss_softc *sc)
1097 {
1098     struct ciss_request	*cr;
1099     int			i;
1100 
1101     debug_called(1);
1102 
1103     if (bootverbose)
1104 	ciss_printf(sc, "using %d of %d available commands\n",
1105 		    sc->ciss_max_requests, sc->ciss_cfg->max_outstanding_commands);
1106 
1107     /*
1108      * Create the DMA tag for commands.
1109      */
1110     if (bus_dma_tag_create(sc->ciss_parent_dmat,	/* parent */
1111 			   32, 0, 			/* alignment, boundary */
1112 			   BUS_SPACE_MAXADDR_32BIT,	/* lowaddr */
1113 			   BUS_SPACE_MAXADDR, 		/* highaddr */
1114 			   NULL, NULL, 			/* filter, filterarg */
1115 			   CISS_COMMAND_ALLOC_SIZE *
1116 			   sc->ciss_max_requests, 1,	/* maxsize, nsegments */
1117 			   BUS_SPACE_MAXSIZE_32BIT,	/* maxsegsize */
1118 			   0,				/* flags */
1119 			   NULL, NULL,			/* lockfunc, lockarg */
1120 			   &sc->ciss_command_dmat)) {
1121 	ciss_printf(sc, "can't allocate command DMA tag\n");
1122 	return(ENOMEM);
1123     }
1124     /*
1125      * Allocate memory and make it available for DMA.
1126      */
1127     if (bus_dmamem_alloc(sc->ciss_command_dmat, (void **)&sc->ciss_command,
1128 			 BUS_DMA_NOWAIT, &sc->ciss_command_map)) {
1129 	ciss_printf(sc, "can't allocate command memory\n");
1130 	return(ENOMEM);
1131     }
1132     bus_dmamap_load(sc->ciss_command_dmat, sc->ciss_command_map,sc->ciss_command,
1133 		    CISS_COMMAND_ALLOC_SIZE * sc->ciss_max_requests,
1134 		    ciss_command_map_helper, &sc->ciss_command_phys, 0);
1135     bzero(sc->ciss_command, CISS_COMMAND_ALLOC_SIZE * sc->ciss_max_requests);
1136 
1137     /*
1138      * Set up the request and command structures, push requests onto
1139      * the free queue.
1140      */
1141     for (i = 1; i < sc->ciss_max_requests; i++) {
1142 	cr = &sc->ciss_request[i];
1143 	cr->cr_sc = sc;
1144 	cr->cr_tag = i;
1145 	cr->cr_cc = (struct ciss_command *)((uintptr_t)sc->ciss_command +
1146 	    CISS_COMMAND_ALLOC_SIZE * i);
1147 	cr->cr_ccphys = sc->ciss_command_phys + CISS_COMMAND_ALLOC_SIZE * i;
1148 	bus_dmamap_create(sc->ciss_buffer_dmat, 0, &cr->cr_datamap);
1149 	ciss_enqueue_free(cr);
1150     }
1151     return(0);
1152 }
1153 
1154 static void
1155 ciss_command_map_helper(void *arg, bus_dma_segment_t *segs, int nseg, int error)
1156 {
1157     uint32_t *addr;
1158 
1159     addr = arg;
1160     *addr = segs[0].ds_addr;
1161 }
1162 
1163 /************************************************************************
1164  * Identify the adapter, print some information about it.
1165  */
1166 static int
1167 ciss_identify_adapter(struct ciss_softc *sc)
1168 {
1169     struct ciss_request	*cr;
1170     int			error, command_status;
1171 
1172     debug_called(1);
1173 
1174     cr = NULL;
1175 
1176     /*
1177      * Get a request, allocate storage for the adapter data.
1178      */
1179     if ((error = ciss_get_bmic_request(sc, &cr, CISS_BMIC_ID_CTLR,
1180 				       (void **)&sc->ciss_id,
1181 				       sizeof(*sc->ciss_id))) != 0)
1182 	goto out;
1183 
1184     /*
1185      * Submit the request and wait for it to complete.
1186      */
1187     if ((error = ciss_synch_request(cr, 60 * 1000)) != 0) {
1188 	ciss_printf(sc, "error sending BMIC ID_CTLR command (%d)\n", error);
1189 	goto out;
1190     }
1191 
1192     /*
1193      * Check response.
1194      */
1195     ciss_report_request(cr, &command_status, NULL);
1196     switch(command_status) {
1197     case CISS_CMD_STATUS_SUCCESS:		/* buffer right size */
1198 	break;
1199     case CISS_CMD_STATUS_DATA_UNDERRUN:
1200     case CISS_CMD_STATUS_DATA_OVERRUN:
1201 	ciss_printf(sc, "data over/underrun reading adapter information\n");
1202     default:
1203 	ciss_printf(sc, "error reading adapter information (%s)\n",
1204 		    ciss_name_command_status(command_status));
1205 	error = EIO;
1206 	goto out;
1207     }
1208 
1209     /* sanity-check reply */
1210     if (!sc->ciss_id->big_map_supported) {
1211 	ciss_printf(sc, "adapter does not support BIG_MAP\n");
1212 	error = ENXIO;
1213 	goto out;
1214     }
1215 
1216 #if 0
1217     /* XXX later revisions may not need this */
1218     sc->ciss_flags |= CISS_FLAG_FAKE_SYNCH;
1219 #endif
1220 
1221     /* XXX only really required for old 5300 adapters? */
1222     sc->ciss_flags |= CISS_FLAG_BMIC_ABORT;
1223 
1224     /*
1225      * Earlier controller specs do not contain these config
1226      * entries, so assume that a 0 means its old and assign
1227      * these values to the defaults that were established
1228      * when this driver was developed for them
1229      */
1230     if (sc->ciss_cfg->max_logical_supported == 0)
1231         sc->ciss_cfg->max_logical_supported = CISS_MAX_LOGICAL;
1232     if (sc->ciss_cfg->max_physical_supported == 0)
1233 	sc->ciss_cfg->max_physical_supported = CISS_MAX_PHYSICAL;
1234     /* print information */
1235     if (bootverbose) {
1236 	ciss_printf(sc, "  %d logical drive%s configured\n",
1237 		    sc->ciss_id->configured_logical_drives,
1238 		    (sc->ciss_id->configured_logical_drives == 1) ? "" : "s");
1239 	ciss_printf(sc, "  firmware %4.4s\n", sc->ciss_id->running_firmware_revision);
1240 	ciss_printf(sc, "  %d SCSI channels\n", sc->ciss_id->scsi_bus_count);
1241 
1242 	ciss_printf(sc, "  signature '%.4s'\n", sc->ciss_cfg->signature);
1243 	ciss_printf(sc, "  valence %d\n", sc->ciss_cfg->valence);
1244 	ciss_printf(sc, "  supported I/O methods 0x%b\n",
1245 		    sc->ciss_cfg->supported_methods,
1246 		    "\20\1READY\2simple\3performant\4MEMQ\n");
1247 	ciss_printf(sc, "  active I/O method 0x%b\n",
1248 		    sc->ciss_cfg->active_method, "\20\2simple\3performant\4MEMQ\n");
1249 	ciss_printf(sc, "  4G page base 0x%08x\n",
1250 		    sc->ciss_cfg->command_physlimit);
1251 	ciss_printf(sc, "  interrupt coalesce delay %dus\n",
1252 		    sc->ciss_cfg->interrupt_coalesce_delay);
1253 	ciss_printf(sc, "  interrupt coalesce count %d\n",
1254 		    sc->ciss_cfg->interrupt_coalesce_count);
1255 	ciss_printf(sc, "  max outstanding commands %d\n",
1256 		    sc->ciss_cfg->max_outstanding_commands);
1257 	ciss_printf(sc, "  bus types 0x%b\n", sc->ciss_cfg->bus_types,
1258 		    "\20\1ultra2\2ultra3\10fibre1\11fibre2\n");
1259 	ciss_printf(sc, "  server name '%.16s'\n", sc->ciss_cfg->server_name);
1260 	ciss_printf(sc, "  heartbeat 0x%x\n", sc->ciss_cfg->heartbeat);
1261     	ciss_printf(sc, "  max logical logical volumes: %d\n", sc->ciss_cfg->max_logical_supported);
1262     	ciss_printf(sc, "  max physical disks supported: %d\n", sc->ciss_cfg->max_physical_supported);
1263     	ciss_printf(sc, "  max physical disks per logical volume: %d\n", sc->ciss_cfg->max_physical_per_logical);
1264     }
1265 
1266 out:
1267     if (error) {
1268 	if (sc->ciss_id != NULL) {
1269 	    free(sc->ciss_id, CISS_MALLOC_CLASS);
1270 	    sc->ciss_id = NULL;
1271 	}
1272     }
1273     if (cr != NULL)
1274 	ciss_release_request(cr);
1275     return(error);
1276 }
1277 
1278 /************************************************************************
1279  * Helper routine for generating a list of logical and physical luns.
1280  */
1281 static struct ciss_lun_report *
1282 ciss_report_luns(struct ciss_softc *sc, int opcode, int nunits)
1283 {
1284     struct ciss_request		*cr;
1285     struct ciss_command		*cc;
1286     struct ciss_report_cdb	*crc;
1287     struct ciss_lun_report	*cll;
1288     int				command_status;
1289     int				report_size;
1290     int				error = 0;
1291 
1292     debug_called(1);
1293 
1294     cr = NULL;
1295     cll = NULL;
1296 
1297     /*
1298      * Get a request, allocate storage for the address list.
1299      */
1300     if ((error = ciss_get_request(sc, &cr)) != 0)
1301 	goto out;
1302     report_size = sizeof(*cll) + nunits * sizeof(union ciss_device_address);
1303     if ((cll = malloc(report_size, CISS_MALLOC_CLASS, M_NOWAIT | M_ZERO)) == NULL) {
1304 	ciss_printf(sc, "can't allocate memory for lun report\n");
1305 	error = ENOMEM;
1306 	goto out;
1307     }
1308 
1309     /*
1310      * Build the Report Logical/Physical LUNs command.
1311      */
1312     cc = cr->cr_cc;
1313     cr->cr_data = cll;
1314     cr->cr_length = report_size;
1315     cr->cr_flags = CISS_REQ_DATAIN;
1316 
1317     cc->header.address.physical.mode = CISS_HDR_ADDRESS_MODE_PERIPHERAL;
1318     cc->header.address.physical.bus = 0;
1319     cc->header.address.physical.target = 0;
1320     cc->cdb.cdb_length = sizeof(*crc);
1321     cc->cdb.type = CISS_CDB_TYPE_COMMAND;
1322     cc->cdb.attribute = CISS_CDB_ATTRIBUTE_SIMPLE;
1323     cc->cdb.direction = CISS_CDB_DIRECTION_READ;
1324     cc->cdb.timeout = 30;	/* XXX better suggestions? */
1325 
1326     crc = (struct ciss_report_cdb *)&(cc->cdb.cdb[0]);
1327     bzero(crc, sizeof(*crc));
1328     crc->opcode = opcode;
1329     crc->length = htonl(report_size);			/* big-endian field */
1330     cll->list_size = htonl(report_size - sizeof(*cll));	/* big-endian field */
1331 
1332     /*
1333      * Submit the request and wait for it to complete.  (timeout
1334      * here should be much greater than above)
1335      */
1336     if ((error = ciss_synch_request(cr, 60 * 1000)) != 0) {
1337 	ciss_printf(sc, "error sending %d LUN command (%d)\n", opcode, error);
1338 	goto out;
1339     }
1340 
1341     /*
1342      * Check response.  Note that data over/underrun is OK.
1343      */
1344     ciss_report_request(cr, &command_status, NULL);
1345     switch(command_status) {
1346     case CISS_CMD_STATUS_SUCCESS:	/* buffer right size */
1347     case CISS_CMD_STATUS_DATA_UNDERRUN:	/* buffer too large, not bad */
1348 	break;
1349     case CISS_CMD_STATUS_DATA_OVERRUN:
1350 	ciss_printf(sc, "WARNING: more units than driver limit (%d)\n",
1351 		    sc->ciss_cfg->max_logical_supported);
1352 	break;
1353     default:
1354 	ciss_printf(sc, "error detecting logical drive configuration (%s)\n",
1355 		    ciss_name_command_status(command_status));
1356 	error = EIO;
1357 	goto out;
1358     }
1359     ciss_release_request(cr);
1360     cr = NULL;
1361 
1362 out:
1363     if (cr != NULL)
1364 	ciss_release_request(cr);
1365     if (error && cll != NULL) {
1366 	free(cll, CISS_MALLOC_CLASS);
1367 	cll = NULL;
1368     }
1369     return(cll);
1370 }
1371 
1372 /************************************************************************
1373  * Find logical drives on the adapter.
1374  */
1375 static int
1376 ciss_init_logical(struct ciss_softc *sc)
1377 {
1378     struct ciss_lun_report	*cll;
1379     int				error = 0, i, j;
1380     int				ndrives;
1381 
1382     debug_called(1);
1383 
1384     cll = ciss_report_luns(sc, CISS_OPCODE_REPORT_LOGICAL_LUNS,
1385 			   sc->ciss_cfg->max_logical_supported);
1386     if (cll == NULL) {
1387 	error = ENXIO;
1388 	goto out;
1389     }
1390 
1391     /* sanity-check reply */
1392     ndrives = (ntohl(cll->list_size) / sizeof(union ciss_device_address));
1393     if ((ndrives < 0) || (ndrives > sc->ciss_cfg->max_logical_supported)) {
1394 	ciss_printf(sc, "adapter claims to report absurd number of logical drives (%d > %d)\n",
1395 	    	ndrives, sc->ciss_cfg->max_logical_supported);
1396 	error = ENXIO;
1397 	goto out;
1398     }
1399 
1400     /*
1401      * Save logical drive information.
1402      */
1403     if (bootverbose) {
1404 	ciss_printf(sc, "%d logical drive%s\n",
1405 	    ndrives, (ndrives > 1 || ndrives == 0) ? "s" : "");
1406     }
1407 
1408     sc->ciss_logical =
1409 	malloc(sc->ciss_max_logical_bus * sizeof(struct ciss_ldrive *),
1410 	       CISS_MALLOC_CLASS, M_NOWAIT | M_ZERO);
1411     if (sc->ciss_logical == NULL) {
1412 	error = ENXIO;
1413 	goto out;
1414     }
1415 
1416     for (i = 0; i <= sc->ciss_max_logical_bus; i++) {
1417 	sc->ciss_logical[i] =
1418 	    malloc(sc->ciss_cfg->max_logical_supported *
1419 		   sizeof(struct ciss_ldrive),
1420 		   CISS_MALLOC_CLASS, M_NOWAIT | M_ZERO);
1421 	if (sc->ciss_logical[i] == NULL) {
1422 	    error = ENXIO;
1423 	    goto out;
1424 	}
1425 
1426 	for (j = 0; j < sc->ciss_cfg->max_logical_supported; j++)
1427 	    sc->ciss_logical[i][j].cl_status = CISS_LD_NONEXISTENT;
1428     }
1429 
1430 
1431     for (i = 0; i < sc->ciss_cfg->max_logical_supported; i++) {
1432 	if (i < ndrives) {
1433 	    struct ciss_ldrive	*ld;
1434 	    int			bus, target;
1435 
1436 	    bus		= CISS_LUN_TO_BUS(cll->lun[i].logical.lun);
1437 	    target	= CISS_LUN_TO_TARGET(cll->lun[i].logical.lun);
1438 	    ld		= &sc->ciss_logical[bus][target];
1439 
1440 	    ld->cl_address	= cll->lun[i];
1441 	    ld->cl_controller	= &sc->ciss_controllers[bus];
1442 	    if (ciss_identify_logical(sc, ld) != 0)
1443 		continue;
1444 	    /*
1445 	     * If the drive has had media exchanged, we should bring it online.
1446 	     */
1447 	    if (ld->cl_lstatus->media_exchanged)
1448 		ciss_accept_media(sc, ld);
1449 
1450 	}
1451     }
1452 
1453  out:
1454     if (cll != NULL)
1455 	free(cll, CISS_MALLOC_CLASS);
1456     return(error);
1457 }
1458 
1459 static int
1460 ciss_init_physical(struct ciss_softc *sc)
1461 {
1462     struct ciss_lun_report	*cll;
1463     int				error = 0, i;
1464     int				nphys;
1465     int				bus, target;
1466 
1467     debug_called(1);
1468 
1469     bus = 0;
1470     target = 0;
1471 
1472     cll = ciss_report_luns(sc, CISS_OPCODE_REPORT_PHYSICAL_LUNS,
1473 			   sc->ciss_cfg->max_physical_supported);
1474     if (cll == NULL) {
1475 	error = ENXIO;
1476 	goto out;
1477     }
1478 
1479     nphys = (ntohl(cll->list_size) / sizeof(union ciss_device_address));
1480 
1481     if (bootverbose) {
1482 	ciss_printf(sc, "%d physical device%s\n",
1483 	    nphys, (nphys > 1 || nphys == 0) ? "s" : "");
1484     }
1485 
1486     /*
1487      * Figure out the bus mapping.
1488      * Logical buses include both the local logical bus for local arrays and
1489      * proxy buses for remote arrays.  Physical buses are numbered by the
1490      * controller and represent physical buses that hold physical devices.
1491      * We shift these bus numbers so that everything fits into a single flat
1492      * numbering space for CAM.  Logical buses occupy the first 32 CAM bus
1493      * numbers, and the physical bus numbers are shifted to be above that.
1494      * This results in the various driver arrays being indexed as follows:
1495      *
1496      * ciss_controllers[] - indexed by logical bus
1497      * ciss_cam_sim[]     - indexed by both logical and physical, with physical
1498      *                      being shifted by 32.
1499      * ciss_logical[][]   - indexed by logical bus
1500      * ciss_physical[][]  - indexed by physical bus
1501      *
1502      * XXX This is getting more and more hackish.  CISS really doesn't play
1503      *     well with a standard SCSI model; devices are addressed via magic
1504      *     cookies, not via b/t/l addresses.  Since there is no way to store
1505      *     the cookie in the CAM device object, we have to keep these lookup
1506      *     tables handy so that the devices can be found quickly at the cost
1507      *     of wasting memory and having a convoluted lookup scheme.  This
1508      *     driver should probably be converted to block interface.
1509      */
1510     /*
1511      * If the L2 and L3 SCSI addresses are 0, this signifies a proxy
1512      * controller. A proxy controller is another physical controller
1513      * behind the primary PCI controller. We need to know about this
1514      * so that BMIC commands can be properly targeted.  There can be
1515      * proxy controllers attached to a single PCI controller, so
1516      * find the highest numbered one so the array can be properly
1517      * sized.
1518      */
1519     sc->ciss_max_logical_bus = 1;
1520     for (i = 0; i < nphys; i++) {
1521 	if (cll->lun[i].physical.extra_address == 0) {
1522 	    bus = cll->lun[i].physical.bus;
1523 	    sc->ciss_max_logical_bus = max(sc->ciss_max_logical_bus, bus) + 1;
1524 	} else {
1525 	    bus = CISS_EXTRA_BUS2(cll->lun[i].physical.extra_address);
1526 	    sc->ciss_max_physical_bus = max(sc->ciss_max_physical_bus, bus);
1527 	}
1528     }
1529 
1530     sc->ciss_controllers =
1531 	malloc(sc->ciss_max_logical_bus * sizeof (union ciss_device_address),
1532 	       CISS_MALLOC_CLASS, M_NOWAIT | M_ZERO);
1533 
1534     if (sc->ciss_controllers == NULL) {
1535 	ciss_printf(sc, "Could not allocate memory for controller map\n");
1536 	error = ENOMEM;
1537 	goto out;
1538     }
1539 
1540     /* setup a map of controller addresses */
1541     for (i = 0; i < nphys; i++) {
1542 	if (cll->lun[i].physical.extra_address == 0) {
1543 	    sc->ciss_controllers[cll->lun[i].physical.bus] = cll->lun[i];
1544 	}
1545     }
1546 
1547     sc->ciss_physical =
1548 	malloc(sc->ciss_max_physical_bus * sizeof(struct ciss_pdrive *),
1549 	       CISS_MALLOC_CLASS, M_NOWAIT | M_ZERO);
1550     if (sc->ciss_physical == NULL) {
1551 	ciss_printf(sc, "Could not allocate memory for physical device map\n");
1552 	error = ENOMEM;
1553 	goto out;
1554     }
1555 
1556     for (i = 0; i < sc->ciss_max_physical_bus; i++) {
1557 	sc->ciss_physical[i] =
1558 	    malloc(sizeof(struct ciss_pdrive) * CISS_MAX_PHYSTGT,
1559 		   CISS_MALLOC_CLASS, M_NOWAIT | M_ZERO);
1560 	if (sc->ciss_physical[i] == NULL) {
1561 	    ciss_printf(sc, "Could not allocate memory for target map\n");
1562 	    error = ENOMEM;
1563 	    goto out;
1564 	}
1565     }
1566 
1567     ciss_filter_physical(sc, cll);
1568 
1569 out:
1570     if (cll != NULL)
1571 	free(cll, CISS_MALLOC_CLASS);
1572 
1573     return(error);
1574 }
1575 
1576 static int
1577 ciss_filter_physical(struct ciss_softc *sc, struct ciss_lun_report *cll)
1578 {
1579     u_int32_t ea;
1580     int i, nphys;
1581     int	bus, target;
1582 
1583     nphys = (ntohl(cll->list_size) / sizeof(union ciss_device_address));
1584     for (i = 0; i < nphys; i++) {
1585 	if (cll->lun[i].physical.extra_address == 0)
1586 	    continue;
1587 
1588 	/*
1589 	 * Filter out devices that we don't want.  Level 3 LUNs could
1590 	 * probably be supported, but the docs don't give enough of a
1591 	 * hint to know how.
1592 	 *
1593 	 * The mode field of the physical address is likely set to have
1594 	 * hard disks masked out.  Honor it unless the user has overridden
1595 	 * us with the tunable.  We also munge the inquiry data for these
1596 	 * disks so that they only show up as passthrough devices.  Keeping
1597 	 * them visible in this fashion is useful for doing things like
1598 	 * flashing firmware.
1599 	 */
1600 	ea = cll->lun[i].physical.extra_address;
1601 	if ((CISS_EXTRA_BUS3(ea) != 0) || (CISS_EXTRA_TARGET3(ea) != 0) ||
1602 	    (CISS_EXTRA_MODE2(ea) == 0x3))
1603 	    continue;
1604 	if ((ciss_expose_hidden_physical == 0) &&
1605 	   (cll->lun[i].physical.mode == CISS_HDR_ADDRESS_MODE_MASK_PERIPHERAL))
1606 	    continue;
1607 
1608 	/*
1609 	 * Note: CISS firmware numbers physical busses starting at '1', not
1610 	 *       '0'.  This numbering is internal to the firmware and is only
1611 	 *       used as a hint here.
1612 	 */
1613 	bus = CISS_EXTRA_BUS2(ea) - 1;
1614 	target = CISS_EXTRA_TARGET2(ea);
1615 	sc->ciss_physical[bus][target].cp_address = cll->lun[i];
1616 	sc->ciss_physical[bus][target].cp_online = 1;
1617     }
1618 
1619     return (0);
1620 }
1621 
1622 static int
1623 ciss_inquiry_logical(struct ciss_softc *sc, struct ciss_ldrive *ld)
1624 {
1625     struct ciss_request			*cr;
1626     struct ciss_command			*cc;
1627     struct scsi_inquiry			*inq;
1628     int					error;
1629     int					command_status;
1630 
1631     cr = NULL;
1632 
1633     bzero(&ld->cl_geometry, sizeof(ld->cl_geometry));
1634 
1635     if ((error = ciss_get_request(sc, &cr)) != 0)
1636 	goto out;
1637 
1638     cc = cr->cr_cc;
1639     cr->cr_data = &ld->cl_geometry;
1640     cr->cr_length = sizeof(ld->cl_geometry);
1641     cr->cr_flags = CISS_REQ_DATAIN;
1642 
1643     cc->header.address = ld->cl_address;
1644     cc->cdb.cdb_length = 6;
1645     cc->cdb.type = CISS_CDB_TYPE_COMMAND;
1646     cc->cdb.attribute = CISS_CDB_ATTRIBUTE_SIMPLE;
1647     cc->cdb.direction = CISS_CDB_DIRECTION_READ;
1648     cc->cdb.timeout = 30;
1649 
1650     inq = (struct scsi_inquiry *)&(cc->cdb.cdb[0]);
1651     inq->opcode = INQUIRY;
1652     inq->byte2 = SI_EVPD;
1653     inq->page_code = CISS_VPD_LOGICAL_DRIVE_GEOMETRY;
1654     scsi_ulto2b(sizeof(ld->cl_geometry), inq->length);
1655 
1656     if ((error = ciss_synch_request(cr, 60 * 1000)) != 0) {
1657 	ciss_printf(sc, "error getting geometry (%d)\n", error);
1658 	goto out;
1659     }
1660 
1661     ciss_report_request(cr, &command_status, NULL);
1662     switch(command_status) {
1663     case CISS_CMD_STATUS_SUCCESS:
1664     case CISS_CMD_STATUS_DATA_UNDERRUN:
1665 	break;
1666     case CISS_CMD_STATUS_DATA_OVERRUN:
1667 	ciss_printf(sc, "WARNING: Data overrun\n");
1668 	break;
1669     default:
1670 	ciss_printf(sc, "Error detecting logical drive geometry (%s)\n",
1671 		    ciss_name_command_status(command_status));
1672 	break;
1673     }
1674 
1675 out:
1676     if (cr != NULL)
1677 	ciss_release_request(cr);
1678     return(error);
1679 }
1680 /************************************************************************
1681  * Identify a logical drive, initialise state related to it.
1682  */
1683 static int
1684 ciss_identify_logical(struct ciss_softc *sc, struct ciss_ldrive *ld)
1685 {
1686     struct ciss_request		*cr;
1687     struct ciss_command		*cc;
1688     struct ciss_bmic_cdb	*cbc;
1689     int				error, command_status;
1690 
1691     debug_called(1);
1692 
1693     cr = NULL;
1694 
1695     /*
1696      * Build a BMIC request to fetch the drive ID.
1697      */
1698     if ((error = ciss_get_bmic_request(sc, &cr, CISS_BMIC_ID_LDRIVE,
1699 				       (void **)&ld->cl_ldrive,
1700 				       sizeof(*ld->cl_ldrive))) != 0)
1701 	goto out;
1702     cc = cr->cr_cc;
1703     cc->header.address = *ld->cl_controller;	/* target controller */
1704     cbc = (struct ciss_bmic_cdb *)&(cc->cdb.cdb[0]);
1705     cbc->log_drive = CISS_LUN_TO_TARGET(ld->cl_address.logical.lun);
1706 
1707     /*
1708      * Submit the request and wait for it to complete.
1709      */
1710     if ((error = ciss_synch_request(cr, 60 * 1000)) != 0) {
1711 	ciss_printf(sc, "error sending BMIC LDRIVE command (%d)\n", error);
1712 	goto out;
1713     }
1714 
1715     /*
1716      * Check response.
1717      */
1718     ciss_report_request(cr, &command_status, NULL);
1719     switch(command_status) {
1720     case CISS_CMD_STATUS_SUCCESS:		/* buffer right size */
1721 	break;
1722     case CISS_CMD_STATUS_DATA_UNDERRUN:
1723     case CISS_CMD_STATUS_DATA_OVERRUN:
1724 	ciss_printf(sc, "data over/underrun reading logical drive ID\n");
1725     default:
1726 	ciss_printf(sc, "error reading logical drive ID (%s)\n",
1727 		    ciss_name_command_status(command_status));
1728 	error = EIO;
1729 	goto out;
1730     }
1731     ciss_release_request(cr);
1732     cr = NULL;
1733 
1734     /*
1735      * Build a CISS BMIC command to get the logical drive status.
1736      */
1737     if ((error = ciss_get_ldrive_status(sc, ld)) != 0)
1738 	goto out;
1739 
1740     /*
1741      * Get the logical drive geometry.
1742      */
1743     if ((error = ciss_inquiry_logical(sc, ld)) != 0)
1744 	goto out;
1745 
1746     /*
1747      * Print the drive's basic characteristics.
1748      */
1749     if (bootverbose) {
1750 	ciss_printf(sc, "logical drive (b%dt%d): %s, %dMB ",
1751 		    CISS_LUN_TO_BUS(ld->cl_address.logical.lun),
1752 		    CISS_LUN_TO_TARGET(ld->cl_address.logical.lun),
1753 		    ciss_name_ldrive_org(ld->cl_ldrive->fault_tolerance),
1754 		    ((ld->cl_ldrive->blocks_available / (1024 * 1024)) *
1755 		     ld->cl_ldrive->block_size));
1756 
1757 	ciss_print_ldrive(sc, ld);
1758     }
1759 out:
1760     if (error != 0) {
1761 	/* make the drive not-exist */
1762 	ld->cl_status = CISS_LD_NONEXISTENT;
1763 	if (ld->cl_ldrive != NULL) {
1764 	    free(ld->cl_ldrive, CISS_MALLOC_CLASS);
1765 	    ld->cl_ldrive = NULL;
1766 	}
1767 	if (ld->cl_lstatus != NULL) {
1768 	    free(ld->cl_lstatus, CISS_MALLOC_CLASS);
1769 	    ld->cl_lstatus = NULL;
1770 	}
1771     }
1772     if (cr != NULL)
1773 	ciss_release_request(cr);
1774 
1775     return(error);
1776 }
1777 
1778 /************************************************************************
1779  * Get status for a logical drive.
1780  *
1781  * XXX should we also do this in response to Test Unit Ready?
1782  */
1783 static int
1784 ciss_get_ldrive_status(struct ciss_softc *sc,  struct ciss_ldrive *ld)
1785 {
1786     struct ciss_request		*cr;
1787     struct ciss_command		*cc;
1788     struct ciss_bmic_cdb	*cbc;
1789     int				error, command_status;
1790 
1791     /*
1792      * Build a CISS BMIC command to get the logical drive status.
1793      */
1794     if ((error = ciss_get_bmic_request(sc, &cr, CISS_BMIC_ID_LSTATUS,
1795 				       (void **)&ld->cl_lstatus,
1796 				       sizeof(*ld->cl_lstatus))) != 0)
1797 	goto out;
1798     cc = cr->cr_cc;
1799     cc->header.address = *ld->cl_controller;	/* target controller */
1800     cbc = (struct ciss_bmic_cdb *)&(cc->cdb.cdb[0]);
1801     cbc->log_drive = CISS_LUN_TO_TARGET(ld->cl_address.logical.lun);
1802 
1803     /*
1804      * Submit the request and wait for it to complete.
1805      */
1806     if ((error = ciss_synch_request(cr, 60 * 1000)) != 0) {
1807 	ciss_printf(sc, "error sending BMIC LSTATUS command (%d)\n", error);
1808 	goto out;
1809     }
1810 
1811     /*
1812      * Check response.
1813      */
1814     ciss_report_request(cr, &command_status, NULL);
1815     switch(command_status) {
1816     case CISS_CMD_STATUS_SUCCESS:		/* buffer right size */
1817 	break;
1818     case CISS_CMD_STATUS_DATA_UNDERRUN:
1819     case CISS_CMD_STATUS_DATA_OVERRUN:
1820 	ciss_printf(sc, "data over/underrun reading logical drive status\n");
1821     default:
1822 	ciss_printf(sc, "error reading logical drive status (%s)\n",
1823 		    ciss_name_command_status(command_status));
1824 	error = EIO;
1825 	goto out;
1826     }
1827 
1828     /*
1829      * Set the drive's summary status based on the returned status.
1830      *
1831      * XXX testing shows that a failed JBOD drive comes back at next
1832      * boot in "queued for expansion" mode.  WTF?
1833      */
1834     ld->cl_status = ciss_decode_ldrive_status(ld->cl_lstatus->status);
1835 
1836 out:
1837     if (cr != NULL)
1838 	ciss_release_request(cr);
1839     return(error);
1840 }
1841 
1842 /************************************************************************
1843  * Notify the adapter of a config update.
1844  */
1845 static int
1846 ciss_update_config(struct ciss_softc *sc)
1847 {
1848     int		i;
1849 
1850     debug_called(1);
1851 
1852     CISS_TL_SIMPLE_WRITE(sc, CISS_TL_SIMPLE_IDBR, CISS_TL_SIMPLE_IDBR_CFG_TABLE);
1853     for (i = 0; i < 1000; i++) {
1854 	if (!(CISS_TL_SIMPLE_READ(sc, CISS_TL_SIMPLE_IDBR) &
1855 	      CISS_TL_SIMPLE_IDBR_CFG_TABLE)) {
1856 	    return(0);
1857 	}
1858 	DELAY(1000);
1859     }
1860     return(1);
1861 }
1862 
1863 /************************************************************************
1864  * Accept new media into a logical drive.
1865  *
1866  * XXX The drive has previously been offline; it would be good if we
1867  *     could make sure it's not open right now.
1868  */
1869 static int
1870 ciss_accept_media(struct ciss_softc *sc, struct ciss_ldrive *ld)
1871 {
1872     struct ciss_request		*cr;
1873     struct ciss_command		*cc;
1874     struct ciss_bmic_cdb	*cbc;
1875     int				command_status;
1876     int				error = 0, ldrive;
1877 
1878     ldrive = CISS_LUN_TO_TARGET(ld->cl_address.logical.lun);
1879 
1880     debug(0, "bringing logical drive %d back online", ldrive);
1881 
1882     /*
1883      * Build a CISS BMIC command to bring the drive back online.
1884      */
1885     if ((error = ciss_get_bmic_request(sc, &cr, CISS_BMIC_ACCEPT_MEDIA,
1886 				       NULL, 0)) != 0)
1887 	goto out;
1888     cc = cr->cr_cc;
1889     cc->header.address = *ld->cl_controller;	/* target controller */
1890     cbc = (struct ciss_bmic_cdb *)&(cc->cdb.cdb[0]);
1891     cbc->log_drive = ldrive;
1892 
1893     /*
1894      * Submit the request and wait for it to complete.
1895      */
1896     if ((error = ciss_synch_request(cr, 60 * 1000)) != 0) {
1897 	ciss_printf(sc, "error sending BMIC ACCEPT MEDIA command (%d)\n", error);
1898 	goto out;
1899     }
1900 
1901     /*
1902      * Check response.
1903      */
1904     ciss_report_request(cr, &command_status, NULL);
1905     switch(command_status) {
1906     case CISS_CMD_STATUS_SUCCESS:		/* all OK */
1907 	/* we should get a logical drive status changed event here */
1908 	break;
1909     default:
1910 	ciss_printf(cr->cr_sc, "error accepting media into failed logical drive (%s)\n",
1911 		    ciss_name_command_status(command_status));
1912 	break;
1913     }
1914 
1915 out:
1916     if (cr != NULL)
1917 	ciss_release_request(cr);
1918     return(error);
1919 }
1920 
1921 /************************************************************************
1922  * Release adapter resources.
1923  */
1924 static void
1925 ciss_free(struct ciss_softc *sc)
1926 {
1927     struct ciss_request *cr;
1928     int			i, j;
1929 
1930     debug_called(1);
1931 
1932     /* we're going away */
1933     sc->ciss_flags |= CISS_FLAG_ABORTING;
1934 
1935     /* terminate the periodic heartbeat routine */
1936     callout_stop(&sc->ciss_periodic);
1937 
1938     /* cancel the Event Notify chain */
1939     ciss_notify_abort(sc);
1940 
1941     ciss_kill_notify_thread(sc);
1942 
1943     /* disconnect from CAM */
1944     if (sc->ciss_cam_sim) {
1945 	for (i = 0; i < sc->ciss_max_logical_bus; i++) {
1946 	    if (sc->ciss_cam_sim[i]) {
1947 		xpt_bus_deregister(cam_sim_path(sc->ciss_cam_sim[i]));
1948 		cam_sim_free(sc->ciss_cam_sim[i], 0);
1949 	    }
1950 	}
1951 	for (i = CISS_PHYSICAL_BASE; i < sc->ciss_max_physical_bus +
1952 	     CISS_PHYSICAL_BASE; i++) {
1953 	    if (sc->ciss_cam_sim[i]) {
1954 		xpt_bus_deregister(cam_sim_path(sc->ciss_cam_sim[i]));
1955 		cam_sim_free(sc->ciss_cam_sim[i], 0);
1956 	    }
1957 	}
1958 	free(sc->ciss_cam_sim, CISS_MALLOC_CLASS);
1959     }
1960     if (sc->ciss_cam_devq)
1961 	cam_simq_free(sc->ciss_cam_devq);
1962 
1963     /* remove the control device */
1964     mtx_unlock(&sc->ciss_mtx);
1965     if (sc->ciss_dev_t != NULL)
1966 	destroy_dev(sc->ciss_dev_t);
1967 
1968     /* Final cleanup of the callout. */
1969     callout_drain(&sc->ciss_periodic);
1970     mtx_destroy(&sc->ciss_mtx);
1971 
1972     /* free the controller data */
1973     if (sc->ciss_id != NULL)
1974 	free(sc->ciss_id, CISS_MALLOC_CLASS);
1975 
1976     /* release I/O resources */
1977     if (sc->ciss_regs_resource != NULL)
1978 	bus_release_resource(sc->ciss_dev, SYS_RES_MEMORY,
1979 			     sc->ciss_regs_rid, sc->ciss_regs_resource);
1980     if (sc->ciss_cfg_resource != NULL)
1981 	bus_release_resource(sc->ciss_dev, SYS_RES_MEMORY,
1982 			     sc->ciss_cfg_rid, sc->ciss_cfg_resource);
1983     if (sc->ciss_intr != NULL)
1984 	bus_teardown_intr(sc->ciss_dev, sc->ciss_irq_resource, sc->ciss_intr);
1985     if (sc->ciss_irq_resource != NULL)
1986 	bus_release_resource(sc->ciss_dev, SYS_RES_IRQ,
1987 			     sc->ciss_irq_rid[0], sc->ciss_irq_resource);
1988     if (sc->ciss_msi)
1989 	pci_release_msi(sc->ciss_dev);
1990 
1991     while ((cr = ciss_dequeue_free(sc)) != NULL)
1992 	bus_dmamap_destroy(sc->ciss_buffer_dmat, cr->cr_datamap);
1993     if (sc->ciss_buffer_dmat)
1994 	bus_dma_tag_destroy(sc->ciss_buffer_dmat);
1995 
1996     /* destroy command memory and DMA tag */
1997     if (sc->ciss_command != NULL) {
1998 	bus_dmamap_unload(sc->ciss_command_dmat, sc->ciss_command_map);
1999 	bus_dmamem_free(sc->ciss_command_dmat, sc->ciss_command, sc->ciss_command_map);
2000     }
2001     if (sc->ciss_command_dmat)
2002 	bus_dma_tag_destroy(sc->ciss_command_dmat);
2003 
2004     if (sc->ciss_reply) {
2005 	bus_dmamap_unload(sc->ciss_reply_dmat, sc->ciss_reply_map);
2006 	bus_dmamem_free(sc->ciss_reply_dmat, sc->ciss_reply, sc->ciss_reply_map);
2007     }
2008     if (sc->ciss_reply_dmat)
2009 	bus_dma_tag_destroy(sc->ciss_reply_dmat);
2010 
2011     /* destroy DMA tags */
2012     if (sc->ciss_parent_dmat)
2013 	bus_dma_tag_destroy(sc->ciss_parent_dmat);
2014     if (sc->ciss_logical) {
2015 	for (i = 0; i <= sc->ciss_max_logical_bus; i++) {
2016 	    for (j = 0; j < sc->ciss_cfg->max_logical_supported; j++) {
2017 		if (sc->ciss_logical[i][j].cl_ldrive)
2018 		    free(sc->ciss_logical[i][j].cl_ldrive, CISS_MALLOC_CLASS);
2019 		if (sc->ciss_logical[i][j].cl_lstatus)
2020 		    free(sc->ciss_logical[i][j].cl_lstatus, CISS_MALLOC_CLASS);
2021 	    }
2022 	    free(sc->ciss_logical[i], CISS_MALLOC_CLASS);
2023 	}
2024 	free(sc->ciss_logical, CISS_MALLOC_CLASS);
2025     }
2026 
2027     if (sc->ciss_physical) {
2028 	for (i = 0; i < sc->ciss_max_physical_bus; i++)
2029 	    free(sc->ciss_physical[i], CISS_MALLOC_CLASS);
2030 	free(sc->ciss_physical, CISS_MALLOC_CLASS);
2031     }
2032 
2033     if (sc->ciss_controllers)
2034 	free(sc->ciss_controllers, CISS_MALLOC_CLASS);
2035 
2036 }
2037 
2038 /************************************************************************
2039  * Give a command to the adapter.
2040  *
2041  * Note that this uses the simple transport layer directly.  If we
2042  * want to add support for other layers, we'll need a switch of some
2043  * sort.
2044  *
2045  * Note that the simple transport layer has no way of refusing a
2046  * command; we only have as many request structures as the adapter
2047  * supports commands, so we don't have to check (this presumes that
2048  * the adapter can handle commands as fast as we throw them at it).
2049  */
2050 static int
2051 ciss_start(struct ciss_request *cr)
2052 {
2053     struct ciss_command	*cc;	/* XXX debugging only */
2054     int			error;
2055 
2056     cc = cr->cr_cc;
2057     debug(2, "post command %d tag %d ", cr->cr_tag, cc->header.host_tag);
2058 
2059     /*
2060      * Map the request's data.
2061      */
2062     if ((error = ciss_map_request(cr)))
2063 	return(error);
2064 
2065 #if 0
2066     ciss_print_request(cr);
2067 #endif
2068 
2069     return(0);
2070 }
2071 
2072 /************************************************************************
2073  * Fetch completed request(s) from the adapter, queue them for
2074  * completion handling.
2075  *
2076  * Note that this uses the simple transport layer directly.  If we
2077  * want to add support for other layers, we'll need a switch of some
2078  * sort.
2079  *
2080  * Note that the simple transport mechanism does not require any
2081  * reentrancy protection; the OPQ read is atomic.  If there is a
2082  * chance of a race with something else that might move the request
2083  * off the busy list, then we will have to lock against that
2084  * (eg. timeouts, etc.)
2085  */
2086 static void
2087 ciss_done(struct ciss_softc *sc, cr_qhead_t *qh)
2088 {
2089     struct ciss_request	*cr;
2090     struct ciss_command	*cc;
2091     u_int32_t		tag, index;
2092 
2093     debug_called(3);
2094 
2095     /*
2096      * Loop quickly taking requests from the adapter and moving them
2097      * to the completed queue.
2098      */
2099     for (;;) {
2100 
2101 	tag = CISS_TL_SIMPLE_FETCH_CMD(sc);
2102 	if (tag == CISS_TL_SIMPLE_OPQ_EMPTY)
2103 	    break;
2104 	index = tag >> 2;
2105 	debug(2, "completed command %d%s", index,
2106 	      (tag & CISS_HDR_HOST_TAG_ERROR) ? " with error" : "");
2107 	if (index >= sc->ciss_max_requests) {
2108 	    ciss_printf(sc, "completed invalid request %d (0x%x)\n", index, tag);
2109 	    continue;
2110 	}
2111 	cr = &(sc->ciss_request[index]);
2112 	cc = cr->cr_cc;
2113 	cc->header.host_tag = tag;	/* not updated by adapter */
2114 	ciss_enqueue_complete(cr, qh);
2115     }
2116 
2117 }
2118 
2119 static void
2120 ciss_perf_done(struct ciss_softc *sc, cr_qhead_t *qh)
2121 {
2122     struct ciss_request	*cr;
2123     struct ciss_command	*cc;
2124     u_int32_t		tag, index;
2125 
2126     debug_called(3);
2127 
2128     /*
2129      * Loop quickly taking requests from the adapter and moving them
2130      * to the completed queue.
2131      */
2132     for (;;) {
2133 	tag = sc->ciss_reply[sc->ciss_rqidx];
2134 	if ((tag & CISS_CYCLE_MASK) != sc->ciss_cycle)
2135 	    break;
2136 	index = tag >> 2;
2137 	debug(2, "completed command %d%s\n", index,
2138 	      (tag & CISS_HDR_HOST_TAG_ERROR) ? " with error" : "");
2139 	if (index < sc->ciss_max_requests) {
2140 	    cr = &(sc->ciss_request[index]);
2141 	    cc = cr->cr_cc;
2142 	    cc->header.host_tag = tag;	/* not updated by adapter */
2143 	    ciss_enqueue_complete(cr, qh);
2144 	} else {
2145 	    ciss_printf(sc, "completed invalid request %d (0x%x)\n", index, tag);
2146 	}
2147 	if (++sc->ciss_rqidx == sc->ciss_max_requests) {
2148 	    sc->ciss_rqidx = 0;
2149 	    sc->ciss_cycle ^= 1;
2150 	}
2151     }
2152 
2153 }
2154 
2155 /************************************************************************
2156  * Take an interrupt from the adapter.
2157  */
2158 static void
2159 ciss_intr(void *arg)
2160 {
2161     cr_qhead_t qh;
2162     struct ciss_softc	*sc = (struct ciss_softc *)arg;
2163 
2164     /*
2165      * The only interrupt we recognise indicates that there are
2166      * entries in the outbound post queue.
2167      */
2168     STAILQ_INIT(&qh);
2169     ciss_done(sc, &qh);
2170     mtx_lock(&sc->ciss_mtx);
2171     ciss_complete(sc, &qh);
2172     mtx_unlock(&sc->ciss_mtx);
2173 }
2174 
2175 static void
2176 ciss_perf_intr(void *arg)
2177 {
2178     struct ciss_softc	*sc = (struct ciss_softc *)arg;
2179 
2180     /* Clear the interrupt and flush the bridges.  Docs say that the flush
2181      * needs to be done twice, which doesn't seem right.
2182      */
2183     CISS_TL_PERF_CLEAR_INT(sc);
2184     CISS_TL_PERF_FLUSH_INT(sc);
2185 
2186     ciss_perf_msi_intr(sc);
2187 }
2188 
2189 static void
2190 ciss_perf_msi_intr(void *arg)
2191 {
2192     cr_qhead_t qh;
2193     struct ciss_softc	*sc = (struct ciss_softc *)arg;
2194 
2195     STAILQ_INIT(&qh);
2196     ciss_perf_done(sc, &qh);
2197     mtx_lock(&sc->ciss_mtx);
2198     ciss_complete(sc, &qh);
2199     mtx_unlock(&sc->ciss_mtx);
2200 }
2201 
2202 
2203 /************************************************************************
2204  * Process completed requests.
2205  *
2206  * Requests can be completed in three fashions:
2207  *
2208  * - by invoking a callback function (cr_complete is non-null)
2209  * - by waking up a sleeper (cr_flags has CISS_REQ_SLEEP set)
2210  * - by clearing the CISS_REQ_POLL flag in interrupt/timeout context
2211  */
2212 static void
2213 ciss_complete(struct ciss_softc *sc, cr_qhead_t *qh)
2214 {
2215     struct ciss_request	*cr;
2216 
2217     debug_called(2);
2218 
2219     /*
2220      * Loop taking requests off the completed queue and performing
2221      * completion processing on them.
2222      */
2223     for (;;) {
2224 	if ((cr = ciss_dequeue_complete(sc, qh)) == NULL)
2225 	    break;
2226 	ciss_unmap_request(cr);
2227 
2228 	if ((cr->cr_flags & CISS_REQ_BUSY) == 0)
2229 	    ciss_printf(sc, "WARNING: completing non-busy request\n");
2230 	cr->cr_flags &= ~CISS_REQ_BUSY;
2231 
2232 	/*
2233 	 * If the request has a callback, invoke it.
2234 	 */
2235 	if (cr->cr_complete != NULL) {
2236 	    cr->cr_complete(cr);
2237 	    continue;
2238 	}
2239 
2240 	/*
2241 	 * If someone is sleeping on this request, wake them up.
2242 	 */
2243 	if (cr->cr_flags & CISS_REQ_SLEEP) {
2244 	    cr->cr_flags &= ~CISS_REQ_SLEEP;
2245 	    wakeup(cr);
2246 	    continue;
2247 	}
2248 
2249 	/*
2250 	 * If someone is polling this request for completion, signal.
2251 	 */
2252 	if (cr->cr_flags & CISS_REQ_POLL) {
2253 	    cr->cr_flags &= ~CISS_REQ_POLL;
2254 	    continue;
2255 	}
2256 
2257 	/*
2258 	 * Give up and throw the request back on the free queue.  This
2259 	 * should never happen; resources will probably be lost.
2260 	 */
2261 	ciss_printf(sc, "WARNING: completed command with no submitter\n");
2262 	ciss_enqueue_free(cr);
2263     }
2264 }
2265 
2266 /************************************************************************
2267  * Report on the completion status of a request, and pass back SCSI
2268  * and command status values.
2269  */
2270 static int
2271 _ciss_report_request(struct ciss_request *cr, int *command_status, int *scsi_status, const char *func)
2272 {
2273     struct ciss_command		*cc;
2274     struct ciss_error_info	*ce;
2275 
2276     debug_called(2);
2277 
2278     cc = cr->cr_cc;
2279     ce = (struct ciss_error_info *)&(cc->sg[0]);
2280 
2281     /*
2282      * We don't consider data under/overrun an error for the Report
2283      * Logical/Physical LUNs commands.
2284      */
2285     if ((cc->header.host_tag & CISS_HDR_HOST_TAG_ERROR) &&
2286 	((ce->command_status == CISS_CMD_STATUS_DATA_OVERRUN) ||
2287 	 (ce->command_status == CISS_CMD_STATUS_DATA_UNDERRUN)) &&
2288 	((cc->cdb.cdb[0] == CISS_OPCODE_REPORT_LOGICAL_LUNS) ||
2289 	 (cc->cdb.cdb[0] == CISS_OPCODE_REPORT_PHYSICAL_LUNS) ||
2290 	 (cc->cdb.cdb[0] == INQUIRY))) {
2291 	cc->header.host_tag &= ~CISS_HDR_HOST_TAG_ERROR;
2292 	debug(2, "ignoring irrelevant under/overrun error");
2293     }
2294 
2295     /*
2296      * Check the command's error bit, if clear, there's no status and
2297      * everything is OK.
2298      */
2299     if (!(cc->header.host_tag & CISS_HDR_HOST_TAG_ERROR)) {
2300 	if (scsi_status != NULL)
2301 	    *scsi_status = SCSI_STATUS_OK;
2302 	if (command_status != NULL)
2303 	    *command_status = CISS_CMD_STATUS_SUCCESS;
2304 	return(0);
2305     } else {
2306 	if (command_status != NULL)
2307 	    *command_status = ce->command_status;
2308 	if (scsi_status != NULL) {
2309 	    if (ce->command_status == CISS_CMD_STATUS_TARGET_STATUS) {
2310 		*scsi_status = ce->scsi_status;
2311 	    } else {
2312 		*scsi_status = -1;
2313 	    }
2314 	}
2315 	if (bootverbose)
2316 	    ciss_printf(cr->cr_sc, "command status 0x%x (%s) scsi status 0x%x\n",
2317 			ce->command_status, ciss_name_command_status(ce->command_status),
2318 			ce->scsi_status);
2319 	if (ce->command_status == CISS_CMD_STATUS_INVALID_COMMAND) {
2320 	    ciss_printf(cr->cr_sc, "invalid command, offense size %d at %d, value 0x%x, function %s\n",
2321 			ce->additional_error_info.invalid_command.offense_size,
2322 			ce->additional_error_info.invalid_command.offense_offset,
2323 			ce->additional_error_info.invalid_command.offense_value,
2324 			func);
2325 	}
2326     }
2327 #if 0
2328     ciss_print_request(cr);
2329 #endif
2330     return(1);
2331 }
2332 
2333 /************************************************************************
2334  * Issue a request and don't return until it's completed.
2335  *
2336  * Depending on adapter status, we may poll or sleep waiting for
2337  * completion.
2338  */
2339 static int
2340 ciss_synch_request(struct ciss_request *cr, int timeout)
2341 {
2342     if (cr->cr_sc->ciss_flags & CISS_FLAG_RUNNING) {
2343 	return(ciss_wait_request(cr, timeout));
2344     } else {
2345 	return(ciss_poll_request(cr, timeout));
2346     }
2347 }
2348 
2349 /************************************************************************
2350  * Issue a request and poll for completion.
2351  *
2352  * Timeout in milliseconds.
2353  */
2354 static int
2355 ciss_poll_request(struct ciss_request *cr, int timeout)
2356 {
2357     cr_qhead_t qh;
2358     struct ciss_softc *sc;
2359     int		error;
2360 
2361     debug_called(2);
2362 
2363     STAILQ_INIT(&qh);
2364     sc = cr->cr_sc;
2365     cr->cr_flags |= CISS_REQ_POLL;
2366     if ((error = ciss_start(cr)) != 0)
2367 	return(error);
2368 
2369     do {
2370 	if (sc->ciss_perf)
2371 	    ciss_perf_done(sc, &qh);
2372 	else
2373 	    ciss_done(sc, &qh);
2374 	ciss_complete(sc, &qh);
2375 	if (!(cr->cr_flags & CISS_REQ_POLL))
2376 	    return(0);
2377 	DELAY(1000);
2378     } while (timeout-- >= 0);
2379     return(EWOULDBLOCK);
2380 }
2381 
2382 /************************************************************************
2383  * Issue a request and sleep waiting for completion.
2384  *
2385  * Timeout in milliseconds.  Note that a spurious wakeup will reset
2386  * the timeout.
2387  */
2388 static int
2389 ciss_wait_request(struct ciss_request *cr, int timeout)
2390 {
2391     int		error;
2392 
2393     debug_called(2);
2394 
2395     cr->cr_flags |= CISS_REQ_SLEEP;
2396     if ((error = ciss_start(cr)) != 0)
2397 	return(error);
2398 
2399     while ((cr->cr_flags & CISS_REQ_SLEEP) && (error != EWOULDBLOCK)) {
2400 	error = msleep(cr, &cr->cr_sc->ciss_mtx, PRIBIO, "cissREQ", (timeout * hz) / 1000);
2401     }
2402     return(error);
2403 }
2404 
2405 #if 0
2406 /************************************************************************
2407  * Abort a request.  Note that a potential exists here to race the
2408  * request being completed; the caller must deal with this.
2409  */
2410 static int
2411 ciss_abort_request(struct ciss_request *ar)
2412 {
2413     struct ciss_request		*cr;
2414     struct ciss_command		*cc;
2415     struct ciss_message_cdb	*cmc;
2416     int				error;
2417 
2418     debug_called(1);
2419 
2420     /* get a request */
2421     if ((error = ciss_get_request(ar->cr_sc, &cr)) != 0)
2422 	return(error);
2423 
2424     /* build the abort command */
2425     cc = cr->cr_cc;
2426     cc->header.address.mode.mode = CISS_HDR_ADDRESS_MODE_PERIPHERAL;	/* addressing? */
2427     cc->header.address.physical.target = 0;
2428     cc->header.address.physical.bus = 0;
2429     cc->cdb.cdb_length = sizeof(*cmc);
2430     cc->cdb.type = CISS_CDB_TYPE_MESSAGE;
2431     cc->cdb.attribute = CISS_CDB_ATTRIBUTE_SIMPLE;
2432     cc->cdb.direction = CISS_CDB_DIRECTION_NONE;
2433     cc->cdb.timeout = 30;
2434 
2435     cmc = (struct ciss_message_cdb *)&(cc->cdb.cdb[0]);
2436     cmc->opcode = CISS_OPCODE_MESSAGE_ABORT;
2437     cmc->type = CISS_MESSAGE_ABORT_TASK;
2438     cmc->abort_tag = ar->cr_tag;	/* endianness?? */
2439 
2440     /*
2441      * Send the request and wait for a response.  If we believe we
2442      * aborted the request OK, clear the flag that indicates it's
2443      * running.
2444      */
2445     error = ciss_synch_request(cr, 35 * 1000);
2446     if (!error)
2447 	error = ciss_report_request(cr, NULL, NULL);
2448     ciss_release_request(cr);
2449 
2450     return(error);
2451 }
2452 #endif
2453 
2454 
2455 /************************************************************************
2456  * Fetch and initialise a request
2457  */
2458 static int
2459 ciss_get_request(struct ciss_softc *sc, struct ciss_request **crp)
2460 {
2461     struct ciss_request *cr;
2462 
2463     debug_called(2);
2464 
2465     /*
2466      * Get a request and clean it up.
2467      */
2468     if ((cr = ciss_dequeue_free(sc)) == NULL)
2469 	return(ENOMEM);
2470 
2471     cr->cr_data = NULL;
2472     cr->cr_flags = 0;
2473     cr->cr_complete = NULL;
2474     cr->cr_private = NULL;
2475     cr->cr_sg_tag = CISS_SG_MAX;	/* Backstop to prevent accidents */
2476 
2477     ciss_preen_command(cr);
2478     *crp = cr;
2479     return(0);
2480 }
2481 
2482 static void
2483 ciss_preen_command(struct ciss_request *cr)
2484 {
2485     struct ciss_command	*cc;
2486     u_int32_t		cmdphys;
2487 
2488     /*
2489      * Clean up the command structure.
2490      *
2491      * Note that we set up the error_info structure here, since the
2492      * length can be overwritten by any command.
2493      */
2494     cc = cr->cr_cc;
2495     cc->header.sg_in_list = 0;		/* kinda inefficient this way */
2496     cc->header.sg_total = 0;
2497     cc->header.host_tag = cr->cr_tag << 2;
2498     cc->header.host_tag_zeroes = 0;
2499     bzero(&(cc->sg[0]), CISS_COMMAND_ALLOC_SIZE - sizeof(struct ciss_command));
2500     cmdphys = cr->cr_ccphys;
2501     cc->error_info.error_info_address = cmdphys + sizeof(struct ciss_command);
2502     cc->error_info.error_info_length = CISS_COMMAND_ALLOC_SIZE - sizeof(struct ciss_command);
2503 }
2504 
2505 /************************************************************************
2506  * Release a request to the free list.
2507  */
2508 static void
2509 ciss_release_request(struct ciss_request *cr)
2510 {
2511     struct ciss_softc	*sc;
2512 
2513     debug_called(2);
2514 
2515     sc = cr->cr_sc;
2516 
2517     /* release the request to the free queue */
2518     ciss_requeue_free(cr);
2519 }
2520 
2521 /************************************************************************
2522  * Allocate a request that will be used to send a BMIC command.  Do some
2523  * of the common setup here to avoid duplicating it everywhere else.
2524  */
2525 static int
2526 ciss_get_bmic_request(struct ciss_softc *sc, struct ciss_request **crp,
2527 		      int opcode, void **bufp, size_t bufsize)
2528 {
2529     struct ciss_request		*cr;
2530     struct ciss_command		*cc;
2531     struct ciss_bmic_cdb	*cbc;
2532     void			*buf;
2533     int				error;
2534     int				dataout;
2535 
2536     debug_called(2);
2537 
2538     cr = NULL;
2539     buf = NULL;
2540 
2541     /*
2542      * Get a request.
2543      */
2544     if ((error = ciss_get_request(sc, &cr)) != 0)
2545 	goto out;
2546 
2547     /*
2548      * Allocate data storage if requested, determine the data direction.
2549      */
2550     dataout = 0;
2551     if ((bufsize > 0) && (bufp != NULL)) {
2552 	if (*bufp == NULL) {
2553 	    if ((buf = malloc(bufsize, CISS_MALLOC_CLASS, M_NOWAIT | M_ZERO)) == NULL) {
2554 		error = ENOMEM;
2555 		goto out;
2556 	    }
2557 	} else {
2558 	    buf = *bufp;
2559 	    dataout = 1;	/* we are given a buffer, so we are writing */
2560 	}
2561     }
2562 
2563     /*
2564      * Build a CISS BMIC command to get the logical drive ID.
2565      */
2566     cr->cr_data = buf;
2567     cr->cr_length = bufsize;
2568     if (!dataout)
2569 	cr->cr_flags = CISS_REQ_DATAIN;
2570 
2571     cc = cr->cr_cc;
2572     cc->header.address.physical.mode = CISS_HDR_ADDRESS_MODE_PERIPHERAL;
2573     cc->header.address.physical.bus = 0;
2574     cc->header.address.physical.target = 0;
2575     cc->cdb.cdb_length = sizeof(*cbc);
2576     cc->cdb.type = CISS_CDB_TYPE_COMMAND;
2577     cc->cdb.attribute = CISS_CDB_ATTRIBUTE_SIMPLE;
2578     cc->cdb.direction = dataout ? CISS_CDB_DIRECTION_WRITE : CISS_CDB_DIRECTION_READ;
2579     cc->cdb.timeout = 0;
2580 
2581     cbc = (struct ciss_bmic_cdb *)&(cc->cdb.cdb[0]);
2582     bzero(cbc, sizeof(*cbc));
2583     cbc->opcode = dataout ? CISS_ARRAY_CONTROLLER_WRITE : CISS_ARRAY_CONTROLLER_READ;
2584     cbc->bmic_opcode = opcode;
2585     cbc->size = htons((u_int16_t)bufsize);
2586 
2587 out:
2588     if (error) {
2589 	if (cr != NULL)
2590 	    ciss_release_request(cr);
2591     } else {
2592 	*crp = cr;
2593 	if ((bufp != NULL) && (*bufp == NULL) && (buf != NULL))
2594 	    *bufp = buf;
2595     }
2596     return(error);
2597 }
2598 
2599 /************************************************************************
2600  * Handle a command passed in from userspace.
2601  */
2602 static int
2603 ciss_user_command(struct ciss_softc *sc, IOCTL_Command_struct *ioc)
2604 {
2605     struct ciss_request		*cr;
2606     struct ciss_command		*cc;
2607     struct ciss_error_info	*ce;
2608     int				error = 0;
2609 
2610     debug_called(1);
2611 
2612     cr = NULL;
2613 
2614     /*
2615      * Get a request.
2616      */
2617     while (ciss_get_request(sc, &cr) != 0)
2618 	msleep(sc, &sc->ciss_mtx, PPAUSE, "cissREQ", hz);
2619     cc = cr->cr_cc;
2620 
2621     /*
2622      * Allocate an in-kernel databuffer if required, copy in user data.
2623      */
2624     mtx_unlock(&sc->ciss_mtx);
2625     cr->cr_length = ioc->buf_size;
2626     if (ioc->buf_size > 0) {
2627 	if ((cr->cr_data = malloc(ioc->buf_size, CISS_MALLOC_CLASS, M_NOWAIT)) == NULL) {
2628 	    error = ENOMEM;
2629 	    goto out_unlocked;
2630 	}
2631 	if ((error = copyin(ioc->buf, cr->cr_data, ioc->buf_size))) {
2632 	    debug(0, "copyin: bad data buffer %p/%d", ioc->buf, ioc->buf_size);
2633 	    goto out_unlocked;
2634 	}
2635     }
2636 
2637     /*
2638      * Build the request based on the user command.
2639      */
2640     bcopy(&ioc->LUN_info, &cc->header.address, sizeof(cc->header.address));
2641     bcopy(&ioc->Request, &cc->cdb, sizeof(cc->cdb));
2642 
2643     /* XXX anything else to populate here? */
2644     mtx_lock(&sc->ciss_mtx);
2645 
2646     /*
2647      * Run the command.
2648      */
2649     if ((error = ciss_synch_request(cr, 60 * 1000))) {
2650 	debug(0, "request failed - %d", error);
2651 	goto out;
2652     }
2653 
2654     /*
2655      * Check to see if the command succeeded.
2656      */
2657     ce = (struct ciss_error_info *)&(cc->sg[0]);
2658     if ((cc->header.host_tag & CISS_HDR_HOST_TAG_ERROR) == 0)
2659 	bzero(ce, sizeof(*ce));
2660 
2661     /*
2662      * Copy the results back to the user.
2663      */
2664     bcopy(ce, &ioc->error_info, sizeof(*ce));
2665     mtx_unlock(&sc->ciss_mtx);
2666     if ((ioc->buf_size > 0) &&
2667 	(error = copyout(cr->cr_data, ioc->buf, ioc->buf_size))) {
2668 	debug(0, "copyout: bad data buffer %p/%d", ioc->buf, ioc->buf_size);
2669 	goto out_unlocked;
2670     }
2671 
2672     /* done OK */
2673     error = 0;
2674 
2675 out_unlocked:
2676     mtx_lock(&sc->ciss_mtx);
2677 
2678 out:
2679     if ((cr != NULL) && (cr->cr_data != NULL))
2680 	free(cr->cr_data, CISS_MALLOC_CLASS);
2681     if (cr != NULL)
2682 	ciss_release_request(cr);
2683     return(error);
2684 }
2685 
2686 /************************************************************************
2687  * Map a request into bus-visible space, initialise the scatter/gather
2688  * list.
2689  */
2690 static int
2691 ciss_map_request(struct ciss_request *cr)
2692 {
2693     struct ciss_softc	*sc;
2694     int			error = 0;
2695 
2696     debug_called(2);
2697 
2698     sc = cr->cr_sc;
2699 
2700     /* check that mapping is necessary */
2701     if (cr->cr_flags & CISS_REQ_MAPPED)
2702 	return(0);
2703 
2704     cr->cr_flags |= CISS_REQ_MAPPED;
2705 
2706     bus_dmamap_sync(sc->ciss_command_dmat, sc->ciss_command_map,
2707 		    BUS_DMASYNC_PREWRITE);
2708 
2709     if (cr->cr_data != NULL) {
2710 	if (cr->cr_flags & CISS_REQ_CCB)
2711 		error = bus_dmamap_load_ccb(sc->ciss_buffer_dmat,
2712 					cr->cr_datamap, cr->cr_data,
2713 					ciss_request_map_helper, cr, 0);
2714 	else
2715 		error = bus_dmamap_load(sc->ciss_buffer_dmat, cr->cr_datamap,
2716 					cr->cr_data, cr->cr_length,
2717 					ciss_request_map_helper, cr, 0);
2718 	if (error != 0)
2719 	    return (error);
2720     } else {
2721 	/*
2722 	 * Post the command to the adapter.
2723 	 */
2724 	cr->cr_sg_tag = CISS_SG_NONE;
2725 	cr->cr_flags |= CISS_REQ_BUSY;
2726 	if (sc->ciss_perf)
2727 	    CISS_TL_PERF_POST_CMD(sc, cr);
2728 	else
2729 	    CISS_TL_SIMPLE_POST_CMD(sc, cr->cr_ccphys);
2730     }
2731 
2732     return(0);
2733 }
2734 
2735 static void
2736 ciss_request_map_helper(void *arg, bus_dma_segment_t *segs, int nseg, int error)
2737 {
2738     struct ciss_command	*cc;
2739     struct ciss_request *cr;
2740     struct ciss_softc	*sc;
2741     int			i;
2742 
2743     debug_called(2);
2744 
2745     cr = (struct ciss_request *)arg;
2746     sc = cr->cr_sc;
2747     cc = cr->cr_cc;
2748 
2749     for (i = 0; i < nseg; i++) {
2750 	cc->sg[i].address = segs[i].ds_addr;
2751 	cc->sg[i].length = segs[i].ds_len;
2752 	cc->sg[i].extension = 0;
2753     }
2754     /* we leave the s/g table entirely within the command */
2755     cc->header.sg_in_list = nseg;
2756     cc->header.sg_total = nseg;
2757 
2758     if (cr->cr_flags & CISS_REQ_DATAIN)
2759 	bus_dmamap_sync(sc->ciss_buffer_dmat, cr->cr_datamap, BUS_DMASYNC_PREREAD);
2760     if (cr->cr_flags & CISS_REQ_DATAOUT)
2761 	bus_dmamap_sync(sc->ciss_buffer_dmat, cr->cr_datamap, BUS_DMASYNC_PREWRITE);
2762 
2763     if (nseg == 0)
2764 	cr->cr_sg_tag = CISS_SG_NONE;
2765     else if (nseg == 1)
2766 	cr->cr_sg_tag = CISS_SG_1;
2767     else if (nseg == 2)
2768 	cr->cr_sg_tag = CISS_SG_2;
2769     else if (nseg <= 4)
2770 	cr->cr_sg_tag = CISS_SG_4;
2771     else if (nseg <= 8)
2772 	cr->cr_sg_tag = CISS_SG_8;
2773     else if (nseg <= 16)
2774 	cr->cr_sg_tag = CISS_SG_16;
2775     else if (nseg <= 32)
2776 	cr->cr_sg_tag = CISS_SG_32;
2777     else
2778 	cr->cr_sg_tag = CISS_SG_MAX;
2779 
2780     /*
2781      * Post the command to the adapter.
2782      */
2783     cr->cr_flags |= CISS_REQ_BUSY;
2784     if (sc->ciss_perf)
2785 	CISS_TL_PERF_POST_CMD(sc, cr);
2786     else
2787 	CISS_TL_SIMPLE_POST_CMD(sc, cr->cr_ccphys);
2788 }
2789 
2790 /************************************************************************
2791  * Unmap a request from bus-visible space.
2792  */
2793 static void
2794 ciss_unmap_request(struct ciss_request *cr)
2795 {
2796     struct ciss_softc	*sc;
2797 
2798     debug_called(2);
2799 
2800     sc = cr->cr_sc;
2801 
2802     /* check that unmapping is necessary */
2803     if ((cr->cr_flags & CISS_REQ_MAPPED) == 0)
2804 	return;
2805 
2806     bus_dmamap_sync(sc->ciss_command_dmat, sc->ciss_command_map,
2807 		    BUS_DMASYNC_POSTWRITE);
2808 
2809     if (cr->cr_data == NULL)
2810 	goto out;
2811 
2812     if (cr->cr_flags & CISS_REQ_DATAIN)
2813 	bus_dmamap_sync(sc->ciss_buffer_dmat, cr->cr_datamap, BUS_DMASYNC_POSTREAD);
2814     if (cr->cr_flags & CISS_REQ_DATAOUT)
2815 	bus_dmamap_sync(sc->ciss_buffer_dmat, cr->cr_datamap, BUS_DMASYNC_POSTWRITE);
2816 
2817     bus_dmamap_unload(sc->ciss_buffer_dmat, cr->cr_datamap);
2818 out:
2819     cr->cr_flags &= ~CISS_REQ_MAPPED;
2820 }
2821 
2822 /************************************************************************
2823  * Attach the driver to CAM.
2824  *
2825  * We put all the logical drives on a single SCSI bus.
2826  */
2827 static int
2828 ciss_cam_init(struct ciss_softc *sc)
2829 {
2830     int			i, maxbus;
2831 
2832     debug_called(1);
2833 
2834     /*
2835      * Allocate a devq.  We can reuse this for the masked physical
2836      * devices if we decide to export these as well.
2837      */
2838     if ((sc->ciss_cam_devq = cam_simq_alloc(sc->ciss_max_requests - 2)) == NULL) {
2839 	ciss_printf(sc, "can't allocate CAM SIM queue\n");
2840 	return(ENOMEM);
2841     }
2842 
2843     /*
2844      * Create a SIM.
2845      *
2846      * This naturally wastes a bit of memory.  The alternative is to allocate
2847      * and register each bus as it is found, and then track them on a linked
2848      * list.  Unfortunately, the driver has a few places where it needs to
2849      * look up the SIM based solely on bus number, and it's unclear whether
2850      * a list traversal would work for these situations.
2851      */
2852     maxbus = max(sc->ciss_max_logical_bus, sc->ciss_max_physical_bus +
2853 		 CISS_PHYSICAL_BASE);
2854     sc->ciss_cam_sim = malloc(maxbus * sizeof(struct cam_sim*),
2855 			      CISS_MALLOC_CLASS, M_NOWAIT | M_ZERO);
2856     if (sc->ciss_cam_sim == NULL) {
2857 	ciss_printf(sc, "can't allocate memory for controller SIM\n");
2858 	return(ENOMEM);
2859     }
2860 
2861     for (i = 0; i < sc->ciss_max_logical_bus; i++) {
2862 	if ((sc->ciss_cam_sim[i] = cam_sim_alloc(ciss_cam_action, ciss_cam_poll,
2863 						 "ciss", sc,
2864 						 device_get_unit(sc->ciss_dev),
2865 						 &sc->ciss_mtx,
2866 						 2,
2867 						 sc->ciss_max_requests - 2,
2868 						 sc->ciss_cam_devq)) == NULL) {
2869 	    ciss_printf(sc, "can't allocate CAM SIM for controller %d\n", i);
2870 	    return(ENOMEM);
2871 	}
2872 
2873 	/*
2874 	 * Register bus with this SIM.
2875 	 */
2876 	mtx_lock(&sc->ciss_mtx);
2877 	if (i == 0 || sc->ciss_controllers[i].physical.bus != 0) {
2878 	    if (xpt_bus_register(sc->ciss_cam_sim[i], sc->ciss_dev, i) != 0) {
2879 		ciss_printf(sc, "can't register SCSI bus %d\n", i);
2880 		mtx_unlock(&sc->ciss_mtx);
2881 		return (ENXIO);
2882 	    }
2883 	}
2884 	mtx_unlock(&sc->ciss_mtx);
2885     }
2886 
2887     for (i = CISS_PHYSICAL_BASE; i < sc->ciss_max_physical_bus +
2888 	 CISS_PHYSICAL_BASE; i++) {
2889 	if ((sc->ciss_cam_sim[i] = cam_sim_alloc(ciss_cam_action, ciss_cam_poll,
2890 						 "ciss", sc,
2891 						 device_get_unit(sc->ciss_dev),
2892 						 &sc->ciss_mtx, 1,
2893 						 sc->ciss_max_requests - 2,
2894 						 sc->ciss_cam_devq)) == NULL) {
2895 	    ciss_printf(sc, "can't allocate CAM SIM for controller %d\n", i);
2896 	    return (ENOMEM);
2897 	}
2898 
2899 	mtx_lock(&sc->ciss_mtx);
2900 	if (xpt_bus_register(sc->ciss_cam_sim[i], sc->ciss_dev, i) != 0) {
2901 	    ciss_printf(sc, "can't register SCSI bus %d\n", i);
2902 	    mtx_unlock(&sc->ciss_mtx);
2903 	    return (ENXIO);
2904 	}
2905 	mtx_unlock(&sc->ciss_mtx);
2906     }
2907 
2908     return(0);
2909 }
2910 
2911 /************************************************************************
2912  * Initiate a rescan of the 'logical devices' SIM
2913  */
2914 static void
2915 ciss_cam_rescan_target(struct ciss_softc *sc, int bus, int target)
2916 {
2917     union ccb		*ccb;
2918 
2919     debug_called(1);
2920 
2921     if ((ccb = xpt_alloc_ccb_nowait()) == NULL) {
2922 	ciss_printf(sc, "rescan failed (can't allocate CCB)\n");
2923 	return;
2924     }
2925 
2926     if (xpt_create_path(&ccb->ccb_h.path, NULL,
2927 	    cam_sim_path(sc->ciss_cam_sim[bus]),
2928 	    target, CAM_LUN_WILDCARD) != CAM_REQ_CMP) {
2929 	ciss_printf(sc, "rescan failed (can't create path)\n");
2930 	xpt_free_ccb(ccb);
2931 	return;
2932     }
2933     xpt_rescan(ccb);
2934     /* scan is now in progress */
2935 }
2936 
2937 /************************************************************************
2938  * Handle requests coming from CAM
2939  */
2940 static void
2941 ciss_cam_action(struct cam_sim *sim, union ccb *ccb)
2942 {
2943     struct ciss_softc	*sc;
2944     struct ccb_scsiio	*csio;
2945     int			bus, target;
2946     int			physical;
2947 
2948     sc = cam_sim_softc(sim);
2949     bus = cam_sim_bus(sim);
2950     csio = (struct ccb_scsiio *)&ccb->csio;
2951     target = csio->ccb_h.target_id;
2952     physical = CISS_IS_PHYSICAL(bus);
2953 
2954     switch (ccb->ccb_h.func_code) {
2955 
2956 	/* perform SCSI I/O */
2957     case XPT_SCSI_IO:
2958 	if (!ciss_cam_action_io(sim, csio))
2959 	    return;
2960 	break;
2961 
2962 	/* perform geometry calculations */
2963     case XPT_CALC_GEOMETRY:
2964     {
2965 	struct ccb_calc_geometry	*ccg = &ccb->ccg;
2966 	struct ciss_ldrive		*ld;
2967 
2968 	debug(1, "XPT_CALC_GEOMETRY %d:%d:%d", cam_sim_bus(sim), ccb->ccb_h.target_id, ccb->ccb_h.target_lun);
2969 
2970 	ld = NULL;
2971 	if (!physical)
2972 	    ld = &sc->ciss_logical[bus][target];
2973 
2974 	/*
2975 	 * Use the cached geometry settings unless the fault tolerance
2976 	 * is invalid.
2977 	 */
2978 	if (physical || ld->cl_geometry.fault_tolerance == 0xFF) {
2979 	    u_int32_t			secs_per_cylinder;
2980 
2981 	    ccg->heads = 255;
2982 	    ccg->secs_per_track = 32;
2983 	    secs_per_cylinder = ccg->heads * ccg->secs_per_track;
2984 	    ccg->cylinders = ccg->volume_size / secs_per_cylinder;
2985 	} else {
2986 	    ccg->heads = ld->cl_geometry.heads;
2987 	    ccg->secs_per_track = ld->cl_geometry.sectors;
2988 	    ccg->cylinders = ntohs(ld->cl_geometry.cylinders);
2989 	}
2990 	ccb->ccb_h.status = CAM_REQ_CMP;
2991         break;
2992     }
2993 
2994 	/* handle path attribute inquiry */
2995     case XPT_PATH_INQ:
2996     {
2997 	struct ccb_pathinq	*cpi = &ccb->cpi;
2998 	int			sg_length;
2999 
3000 	debug(1, "XPT_PATH_INQ %d:%d:%d", cam_sim_bus(sim), ccb->ccb_h.target_id, ccb->ccb_h.target_lun);
3001 
3002 	cpi->version_num = 1;
3003 	cpi->hba_inquiry = PI_TAG_ABLE;	/* XXX is this correct? */
3004 	cpi->target_sprt = 0;
3005 	cpi->hba_misc = 0;
3006 	cpi->max_target = sc->ciss_cfg->max_logical_supported;
3007 	cpi->max_lun = 0;		/* 'logical drive' channel only */
3008 	cpi->initiator_id = sc->ciss_cfg->max_logical_supported;
3009 	strncpy(cpi->sim_vid, "FreeBSD", SIM_IDLEN);
3010         strncpy(cpi->hba_vid, "msmith@freebsd.org", HBA_IDLEN);
3011         strncpy(cpi->dev_name, cam_sim_name(sim), DEV_IDLEN);
3012         cpi->unit_number = cam_sim_unit(sim);
3013         cpi->bus_id = cam_sim_bus(sim);
3014 	cpi->base_transfer_speed = 132 * 1024;	/* XXX what to set this to? */
3015 	cpi->transport = XPORT_SPI;
3016 	cpi->transport_version = 2;
3017 	cpi->protocol = PROTO_SCSI;
3018 	cpi->protocol_version = SCSI_REV_2;
3019 	if (sc->ciss_cfg->max_sg_length == 0) {
3020 		sg_length = 17;
3021 	} else {
3022 	/* XXX Fix for ZMR cards that advertise max_sg_length == 32
3023 	 * Confusing bit here. max_sg_length is usually a power of 2. We always
3024 	 * need to subtract 1 to account for partial pages. Then we need to
3025 	 * align on a valid PAGE_SIZE so we round down to the nearest power of 2.
3026 	 * Add 1 so we can then subtract it out in the assignment to maxio.
3027 	 * The reason for all these shenanigans is to create a maxio value that
3028 	 * creates IO operations to volumes that yield consistent operations
3029 	 * with good performance.
3030 	 */
3031 		sg_length = sc->ciss_cfg->max_sg_length - 1;
3032 		sg_length = (1 << (fls(sg_length) - 1)) + 1;
3033 	}
3034 	cpi->maxio = (min(CISS_MAX_SG_ELEMENTS, sg_length) - 1) * PAGE_SIZE;
3035 	ccb->ccb_h.status = CAM_REQ_CMP;
3036 	break;
3037     }
3038 
3039     case XPT_GET_TRAN_SETTINGS:
3040     {
3041 	struct ccb_trans_settings	*cts = &ccb->cts;
3042 	int				bus, target;
3043 	struct ccb_trans_settings_spi *spi = &cts->xport_specific.spi;
3044 	struct ccb_trans_settings_scsi *scsi = &cts->proto_specific.scsi;
3045 
3046 	bus = cam_sim_bus(sim);
3047 	target = cts->ccb_h.target_id;
3048 
3049 	debug(1, "XPT_GET_TRAN_SETTINGS %d:%d", bus, target);
3050 	/* disconnect always OK */
3051 	cts->protocol = PROTO_SCSI;
3052 	cts->protocol_version = SCSI_REV_2;
3053 	cts->transport = XPORT_SPI;
3054 	cts->transport_version = 2;
3055 
3056 	spi->valid = CTS_SPI_VALID_DISC;
3057 	spi->flags = CTS_SPI_FLAGS_DISC_ENB;
3058 
3059 	scsi->valid = CTS_SCSI_VALID_TQ;
3060 	scsi->flags = CTS_SCSI_FLAGS_TAG_ENB;
3061 
3062 	cts->ccb_h.status = CAM_REQ_CMP;
3063 	break;
3064     }
3065 
3066     default:		/* we can't do this */
3067 	debug(1, "unspported func_code = 0x%x", ccb->ccb_h.func_code);
3068 	ccb->ccb_h.status = CAM_REQ_INVALID;
3069 	break;
3070     }
3071 
3072     xpt_done(ccb);
3073 }
3074 
3075 /************************************************************************
3076  * Handle a CAM SCSI I/O request.
3077  */
3078 static int
3079 ciss_cam_action_io(struct cam_sim *sim, struct ccb_scsiio *csio)
3080 {
3081     struct ciss_softc	*sc;
3082     int			bus, target;
3083     struct ciss_request	*cr;
3084     struct ciss_command	*cc;
3085     int			error;
3086 
3087     sc = cam_sim_softc(sim);
3088     bus = cam_sim_bus(sim);
3089     target = csio->ccb_h.target_id;
3090 
3091     debug(2, "XPT_SCSI_IO %d:%d:%d", bus, target, csio->ccb_h.target_lun);
3092 
3093     /* check that the CDB pointer is not to a physical address */
3094     if ((csio->ccb_h.flags & CAM_CDB_POINTER) && (csio->ccb_h.flags & CAM_CDB_PHYS)) {
3095 	debug(3, "  CDB pointer is to physical address");
3096 	csio->ccb_h.status = CAM_REQ_CMP_ERR;
3097     }
3098 
3099     /* abandon aborted ccbs or those that have failed validation */
3100     if ((csio->ccb_h.status & CAM_STATUS_MASK) != CAM_REQ_INPROG) {
3101 	debug(3, "abandoning CCB due to abort/validation failure");
3102 	return(EINVAL);
3103     }
3104 
3105     /* handle emulation of some SCSI commands ourself */
3106     if (ciss_cam_emulate(sc, csio))
3107 	return(0);
3108 
3109     /*
3110      * Get a request to manage this command.  If we can't, return the
3111      * ccb, freeze the queue and flag so that we unfreeze it when a
3112      * request completes.
3113      */
3114     if ((error = ciss_get_request(sc, &cr)) != 0) {
3115 	xpt_freeze_simq(sim, 1);
3116 	sc->ciss_flags |= CISS_FLAG_BUSY;
3117 	csio->ccb_h.status |= CAM_REQUEUE_REQ;
3118 	return(error);
3119     }
3120 
3121     /*
3122      * Build the command.
3123      */
3124     cc = cr->cr_cc;
3125     cr->cr_data = csio;
3126     cr->cr_length = csio->dxfer_len;
3127     cr->cr_complete = ciss_cam_complete;
3128     cr->cr_private = csio;
3129 
3130     /*
3131      * Target the right logical volume.
3132      */
3133     if (CISS_IS_PHYSICAL(bus))
3134 	cc->header.address =
3135 	    sc->ciss_physical[CISS_CAM_TO_PBUS(bus)][target].cp_address;
3136     else
3137 	cc->header.address =
3138 	    sc->ciss_logical[bus][target].cl_address;
3139     cc->cdb.cdb_length = csio->cdb_len;
3140     cc->cdb.type = CISS_CDB_TYPE_COMMAND;
3141     cc->cdb.attribute = CISS_CDB_ATTRIBUTE_SIMPLE;	/* XXX ordered tags? */
3142     if ((csio->ccb_h.flags & CAM_DIR_MASK) == CAM_DIR_OUT) {
3143 	cr->cr_flags = CISS_REQ_DATAOUT | CISS_REQ_CCB;
3144 	cc->cdb.direction = CISS_CDB_DIRECTION_WRITE;
3145     } else if ((csio->ccb_h.flags & CAM_DIR_MASK) == CAM_DIR_IN) {
3146 	cr->cr_flags = CISS_REQ_DATAIN | CISS_REQ_CCB;
3147 	cc->cdb.direction = CISS_CDB_DIRECTION_READ;
3148     } else {
3149 	cr->cr_data = NULL;
3150 	cr->cr_flags = 0;
3151 	cc->cdb.direction = CISS_CDB_DIRECTION_NONE;
3152     }
3153     cc->cdb.timeout = (csio->ccb_h.timeout / 1000) + 1;
3154     if (csio->ccb_h.flags & CAM_CDB_POINTER) {
3155 	bcopy(csio->cdb_io.cdb_ptr, &cc->cdb.cdb[0], csio->cdb_len);
3156     } else {
3157 	bcopy(csio->cdb_io.cdb_bytes, &cc->cdb.cdb[0], csio->cdb_len);
3158     }
3159 
3160     /*
3161      * Submit the request to the adapter.
3162      *
3163      * Note that this may fail if we're unable to map the request (and
3164      * if we ever learn a transport layer other than simple, may fail
3165      * if the adapter rejects the command).
3166      */
3167     if ((error = ciss_start(cr)) != 0) {
3168 	xpt_freeze_simq(sim, 1);
3169 	csio->ccb_h.status |= CAM_RELEASE_SIMQ;
3170 	if (error == EINPROGRESS) {
3171 	    error = 0;
3172 	} else {
3173 	    csio->ccb_h.status |= CAM_REQUEUE_REQ;
3174 	    ciss_release_request(cr);
3175 	}
3176 	return(error);
3177     }
3178 
3179     return(0);
3180 }
3181 
3182 /************************************************************************
3183  * Emulate SCSI commands the adapter doesn't handle as we might like.
3184  */
3185 static int
3186 ciss_cam_emulate(struct ciss_softc *sc, struct ccb_scsiio *csio)
3187 {
3188     int		bus, target;
3189     u_int8_t	opcode;
3190 
3191     target = csio->ccb_h.target_id;
3192     bus = cam_sim_bus(xpt_path_sim(csio->ccb_h.path));
3193     opcode = (csio->ccb_h.flags & CAM_CDB_POINTER) ?
3194 	*(u_int8_t *)csio->cdb_io.cdb_ptr : csio->cdb_io.cdb_bytes[0];
3195 
3196     if (CISS_IS_PHYSICAL(bus)) {
3197 	if (sc->ciss_physical[CISS_CAM_TO_PBUS(bus)][target].cp_online != 1) {
3198 	    csio->ccb_h.status |= CAM_SEL_TIMEOUT;
3199 	    xpt_done((union ccb *)csio);
3200 	    return(1);
3201 	} else
3202 	    return(0);
3203     }
3204 
3205     /*
3206      * Handle requests for volumes that don't exist or are not online.
3207      * A selection timeout is slightly better than an illegal request.
3208      * Other errors might be better.
3209      */
3210     if (sc->ciss_logical[bus][target].cl_status != CISS_LD_ONLINE) {
3211 	csio->ccb_h.status |= CAM_SEL_TIMEOUT;
3212 	xpt_done((union ccb *)csio);
3213 	return(1);
3214     }
3215 
3216     /* if we have to fake Synchronise Cache */
3217     if (sc->ciss_flags & CISS_FLAG_FAKE_SYNCH) {
3218 	/*
3219 	 * If this is a Synchronise Cache command, typically issued when
3220 	 * a device is closed, flush the adapter and complete now.
3221 	 */
3222 	if (((csio->ccb_h.flags & CAM_CDB_POINTER) ?
3223 	     *(u_int8_t *)csio->cdb_io.cdb_ptr : csio->cdb_io.cdb_bytes[0]) == SYNCHRONIZE_CACHE) {
3224 	    ciss_flush_adapter(sc);
3225 	    csio->ccb_h.status |= CAM_REQ_CMP;
3226 	    xpt_done((union ccb *)csio);
3227 	    return(1);
3228 	}
3229     }
3230 
3231     /*
3232      * A CISS target can only ever have one lun per target. REPORT_LUNS requires
3233      * at least one LUN field to be pre created for us, so snag it and fill in
3234      * the least significant byte indicating 1 LUN here.  Emulate the command
3235      * return to shut up warning on console of a CDB error.  swb
3236      */
3237     if (opcode == REPORT_LUNS && csio->dxfer_len > 0) {
3238        csio->data_ptr[3] = 8;
3239        csio->ccb_h.status |= CAM_REQ_CMP;
3240        xpt_done((union ccb *)csio);
3241        return(1);
3242     }
3243 
3244     return(0);
3245 }
3246 
3247 /************************************************************************
3248  * Check for possibly-completed commands.
3249  */
3250 static void
3251 ciss_cam_poll(struct cam_sim *sim)
3252 {
3253     cr_qhead_t qh;
3254     struct ciss_softc	*sc = cam_sim_softc(sim);
3255 
3256     debug_called(2);
3257 
3258     STAILQ_INIT(&qh);
3259     if (sc->ciss_perf)
3260 	ciss_perf_done(sc, &qh);
3261     else
3262 	ciss_done(sc, &qh);
3263     ciss_complete(sc, &qh);
3264 }
3265 
3266 /************************************************************************
3267  * Handle completion of a command - pass results back through the CCB
3268  */
3269 static void
3270 ciss_cam_complete(struct ciss_request *cr)
3271 {
3272     struct ciss_softc		*sc;
3273     struct ciss_command		*cc;
3274     struct ciss_error_info	*ce;
3275     struct ccb_scsiio		*csio;
3276     int				scsi_status;
3277     int				command_status;
3278 
3279     debug_called(2);
3280 
3281     sc = cr->cr_sc;
3282     cc = cr->cr_cc;
3283     ce = (struct ciss_error_info *)&(cc->sg[0]);
3284     csio = (struct ccb_scsiio *)cr->cr_private;
3285 
3286     /*
3287      * Extract status values from request.
3288      */
3289     ciss_report_request(cr, &command_status, &scsi_status);
3290     csio->scsi_status = scsi_status;
3291 
3292     /*
3293      * Handle specific SCSI status values.
3294      */
3295     switch(scsi_status) {
3296 	/* no status due to adapter error */
3297     case -1:
3298 	debug(0, "adapter error");
3299 	csio->ccb_h.status |= CAM_REQ_CMP_ERR;
3300 	break;
3301 
3302 	/* no status due to command completed OK */
3303     case SCSI_STATUS_OK:		/* CISS_SCSI_STATUS_GOOD */
3304 	debug(2, "SCSI_STATUS_OK");
3305 	csio->ccb_h.status |= CAM_REQ_CMP;
3306 	break;
3307 
3308 	/* check condition, sense data included */
3309     case SCSI_STATUS_CHECK_COND:	/* CISS_SCSI_STATUS_CHECK_CONDITION */
3310 	debug(0, "SCSI_STATUS_CHECK_COND  sense size %d  resid %d\n",
3311 	      ce->sense_length, ce->residual_count);
3312 	bzero(&csio->sense_data, SSD_FULL_SIZE);
3313 	bcopy(&ce->sense_info[0], &csio->sense_data, ce->sense_length);
3314 	if (csio->sense_len > ce->sense_length)
3315 		csio->sense_resid = csio->sense_len - ce->sense_length;
3316 	else
3317 		csio->sense_resid = 0;
3318 	csio->resid = ce->residual_count;
3319 	csio->ccb_h.status |= CAM_SCSI_STATUS_ERROR | CAM_AUTOSNS_VALID;
3320 #ifdef CISS_DEBUG
3321 	{
3322 	    struct scsi_sense_data	*sns = (struct scsi_sense_data *)&ce->sense_info[0];
3323 	    debug(0, "sense key %x", scsi_get_sense_key(sns, csio->sense_len -
3324 		  csio->sense_resid, /*show_errors*/ 1));
3325 	}
3326 #endif
3327 	break;
3328 
3329     case SCSI_STATUS_BUSY:		/* CISS_SCSI_STATUS_BUSY */
3330 	debug(0, "SCSI_STATUS_BUSY");
3331 	csio->ccb_h.status |= CAM_SCSI_BUSY;
3332 	break;
3333 
3334     default:
3335 	debug(0, "unknown status 0x%x", csio->scsi_status);
3336 	csio->ccb_h.status |= CAM_REQ_CMP_ERR;
3337 	break;
3338     }
3339 
3340     /* handle post-command fixup */
3341     ciss_cam_complete_fixup(sc, csio);
3342 
3343     ciss_release_request(cr);
3344     if (sc->ciss_flags & CISS_FLAG_BUSY) {
3345 	sc->ciss_flags &= ~CISS_FLAG_BUSY;
3346 	if (csio->ccb_h.status & CAM_RELEASE_SIMQ)
3347 	    xpt_release_simq(xpt_path_sim(csio->ccb_h.path), 0);
3348 	else
3349 	    csio->ccb_h.status |= CAM_RELEASE_SIMQ;
3350     }
3351     xpt_done((union ccb *)csio);
3352 }
3353 
3354 /********************************************************************************
3355  * Fix up the result of some commands here.
3356  */
3357 static void
3358 ciss_cam_complete_fixup(struct ciss_softc *sc, struct ccb_scsiio *csio)
3359 {
3360     struct scsi_inquiry_data	*inq;
3361     struct ciss_ldrive		*cl;
3362     uint8_t			*cdb;
3363     int				bus, target;
3364 
3365     cdb = (csio->ccb_h.flags & CAM_CDB_POINTER) ?
3366 	 (uint8_t *)csio->cdb_io.cdb_ptr : csio->cdb_io.cdb_bytes;
3367     if (cdb[0] == INQUIRY &&
3368 	(cdb[1] & SI_EVPD) == 0 &&
3369 	(csio->ccb_h.flags & CAM_DIR_MASK) == CAM_DIR_IN &&
3370 	csio->dxfer_len >= SHORT_INQUIRY_LENGTH) {
3371 
3372 	inq = (struct scsi_inquiry_data *)csio->data_ptr;
3373 	target = csio->ccb_h.target_id;
3374 	bus = cam_sim_bus(xpt_path_sim(csio->ccb_h.path));
3375 
3376 	/*
3377 	 * Don't let hard drives be seen by the DA driver.  They will still be
3378 	 * attached by the PASS driver.
3379 	 */
3380 	if (CISS_IS_PHYSICAL(bus)) {
3381 	    if (SID_TYPE(inq) == T_DIRECT)
3382 		inq->device = (inq->device & 0xe0) | T_NODEVICE;
3383 	    return;
3384 	}
3385 
3386 	cl = &sc->ciss_logical[bus][target];
3387 
3388 	padstr(inq->vendor, "HP",
3389 	       SID_VENDOR_SIZE);
3390 	padstr(inq->product,
3391 	       ciss_name_ldrive_org(cl->cl_ldrive->fault_tolerance),
3392 	       SID_PRODUCT_SIZE);
3393 	padstr(inq->revision,
3394 	       ciss_name_ldrive_status(cl->cl_lstatus->status),
3395 	       SID_REVISION_SIZE);
3396     }
3397 }
3398 
3399 
3400 /********************************************************************************
3401  * Find a peripheral attached at (target)
3402  */
3403 static struct cam_periph *
3404 ciss_find_periph(struct ciss_softc *sc, int bus, int target)
3405 {
3406     struct cam_periph	*periph;
3407     struct cam_path	*path;
3408     int			status;
3409 
3410     status = xpt_create_path(&path, NULL, cam_sim_path(sc->ciss_cam_sim[bus]),
3411 			     target, 0);
3412     if (status == CAM_REQ_CMP) {
3413 	periph = cam_periph_find(path, NULL);
3414 	xpt_free_path(path);
3415     } else {
3416 	periph = NULL;
3417     }
3418     return(periph);
3419 }
3420 
3421 /********************************************************************************
3422  * Name the device at (target)
3423  *
3424  * XXX is this strictly correct?
3425  */
3426 static int
3427 ciss_name_device(struct ciss_softc *sc, int bus, int target)
3428 {
3429     struct cam_periph	*periph;
3430 
3431     if (CISS_IS_PHYSICAL(bus))
3432 	return (0);
3433     if ((periph = ciss_find_periph(sc, bus, target)) != NULL) {
3434 	sprintf(sc->ciss_logical[bus][target].cl_name, "%s%d",
3435 		periph->periph_name, periph->unit_number);
3436 	return(0);
3437     }
3438     sc->ciss_logical[bus][target].cl_name[0] = 0;
3439     return(ENOENT);
3440 }
3441 
3442 /************************************************************************
3443  * Periodic status monitoring.
3444  */
3445 static void
3446 ciss_periodic(void *arg)
3447 {
3448     struct ciss_softc	*sc;
3449     struct ciss_request	*cr = NULL;
3450     struct ciss_command	*cc = NULL;
3451     int			error = 0;
3452 
3453     debug_called(1);
3454 
3455     sc = (struct ciss_softc *)arg;
3456 
3457     /*
3458      * Check the adapter heartbeat.
3459      */
3460     if (sc->ciss_cfg->heartbeat == sc->ciss_heartbeat) {
3461 	sc->ciss_heart_attack++;
3462 	debug(0, "adapter heart attack in progress 0x%x/%d",
3463 	      sc->ciss_heartbeat, sc->ciss_heart_attack);
3464 	if (sc->ciss_heart_attack == 3) {
3465 	    ciss_printf(sc, "ADAPTER HEARTBEAT FAILED\n");
3466 	    ciss_disable_adapter(sc);
3467 	    return;
3468 	}
3469     } else {
3470 	sc->ciss_heartbeat = sc->ciss_cfg->heartbeat;
3471 	sc->ciss_heart_attack = 0;
3472 	debug(3, "new heartbeat 0x%x", sc->ciss_heartbeat);
3473     }
3474 
3475     /*
3476      * Send the NOP message and wait for a response.
3477      */
3478     if (ciss_nop_message_heartbeat != 0 && (error = ciss_get_request(sc, &cr)) == 0) {
3479 	cc = cr->cr_cc;
3480 	cr->cr_complete = ciss_nop_complete;
3481 	cc->cdb.cdb_length = 1;
3482 	cc->cdb.type = CISS_CDB_TYPE_MESSAGE;
3483 	cc->cdb.attribute = CISS_CDB_ATTRIBUTE_SIMPLE;
3484 	cc->cdb.direction = CISS_CDB_DIRECTION_WRITE;
3485 	cc->cdb.timeout = 0;
3486 	cc->cdb.cdb[0] = CISS_OPCODE_MESSAGE_NOP;
3487 
3488 	if ((error = ciss_start(cr)) != 0) {
3489 	    ciss_printf(sc, "SENDING NOP MESSAGE FAILED\n");
3490 	}
3491     }
3492 
3493     /*
3494      * If the notify event request has died for some reason, or has
3495      * not started yet, restart it.
3496      */
3497     if (!(sc->ciss_flags & CISS_FLAG_NOTIFY_OK)) {
3498 	debug(0, "(re)starting Event Notify chain");
3499 	ciss_notify_event(sc);
3500     }
3501 
3502     /*
3503      * Reschedule.
3504      */
3505     callout_reset(&sc->ciss_periodic, CISS_HEARTBEAT_RATE * hz, ciss_periodic, sc);
3506 }
3507 
3508 static void
3509 ciss_nop_complete(struct ciss_request *cr)
3510 {
3511     struct ciss_softc		*sc;
3512     static int			first_time = 1;
3513 
3514     sc = cr->cr_sc;
3515     if (ciss_report_request(cr, NULL, NULL) != 0) {
3516 	if (first_time == 1) {
3517 	    first_time = 0;
3518 	    ciss_printf(sc, "SENDING NOP MESSAGE FAILED (not logging anymore)\n");
3519 	}
3520     }
3521 
3522     ciss_release_request(cr);
3523 }
3524 
3525 /************************************************************************
3526  * Disable the adapter.
3527  *
3528  * The all requests in completed queue is failed with hardware error.
3529  * This will cause failover in a multipath configuration.
3530  */
3531 static void
3532 ciss_disable_adapter(struct ciss_softc *sc)
3533 {
3534     cr_qhead_t			qh;
3535     struct ciss_request		*cr;
3536     struct ciss_command		*cc;
3537     struct ciss_error_info	*ce;
3538     int				i;
3539 
3540     CISS_TL_SIMPLE_DISABLE_INTERRUPTS(sc);
3541     pci_disable_busmaster(sc->ciss_dev);
3542     sc->ciss_flags &= ~CISS_FLAG_RUNNING;
3543 
3544     for (i = 1; i < sc->ciss_max_requests; i++) {
3545 	cr = &sc->ciss_request[i];
3546 	if ((cr->cr_flags & CISS_REQ_BUSY) == 0)
3547 	    continue;
3548 
3549 	cc = cr->cr_cc;
3550 	ce = (struct ciss_error_info *)&(cc->sg[0]);
3551 	ce->command_status = CISS_CMD_STATUS_HARDWARE_ERROR;
3552 	ciss_enqueue_complete(cr, &qh);
3553     }
3554 
3555     for (;;) {
3556 	if ((cr = ciss_dequeue_complete(sc, &qh)) == NULL)
3557 	    break;
3558 
3559 	/*
3560 	 * If the request has a callback, invoke it.
3561 	 */
3562 	if (cr->cr_complete != NULL) {
3563 	    cr->cr_complete(cr);
3564 	    continue;
3565 	}
3566 
3567 	/*
3568 	 * If someone is sleeping on this request, wake them up.
3569 	 */
3570 	if (cr->cr_flags & CISS_REQ_SLEEP) {
3571 	    cr->cr_flags &= ~CISS_REQ_SLEEP;
3572 	    wakeup(cr);
3573 	    continue;
3574 	}
3575     }
3576 }
3577 
3578 /************************************************************************
3579  * Request a notification response from the adapter.
3580  *
3581  * If (cr) is NULL, this is the first request of the adapter, so
3582  * reset the adapter's message pointer and start with the oldest
3583  * message available.
3584  */
3585 static void
3586 ciss_notify_event(struct ciss_softc *sc)
3587 {
3588     struct ciss_request		*cr;
3589     struct ciss_command		*cc;
3590     struct ciss_notify_cdb	*cnc;
3591     int				error;
3592 
3593     debug_called(1);
3594 
3595     cr = sc->ciss_periodic_notify;
3596 
3597     /* get a request if we don't already have one */
3598     if (cr == NULL) {
3599 	if ((error = ciss_get_request(sc, &cr)) != 0) {
3600 	    debug(0, "can't get notify event request");
3601 	    goto out;
3602 	}
3603 	sc->ciss_periodic_notify = cr;
3604 	cr->cr_complete = ciss_notify_complete;
3605 	debug(1, "acquired request %d", cr->cr_tag);
3606     }
3607 
3608     /*
3609      * Get a databuffer if we don't already have one, note that the
3610      * adapter command wants a larger buffer than the actual
3611      * structure.
3612      */
3613     if (cr->cr_data == NULL) {
3614 	if ((cr->cr_data = malloc(CISS_NOTIFY_DATA_SIZE, CISS_MALLOC_CLASS, M_NOWAIT)) == NULL) {
3615 	    debug(0, "can't get notify event request buffer");
3616 	    error = ENOMEM;
3617 	    goto out;
3618 	}
3619 	cr->cr_length = CISS_NOTIFY_DATA_SIZE;
3620     }
3621 
3622     /* re-setup the request's command (since we never release it) XXX overkill*/
3623     ciss_preen_command(cr);
3624 
3625     /* (re)build the notify event command */
3626     cc = cr->cr_cc;
3627     cc->header.address.physical.mode = CISS_HDR_ADDRESS_MODE_PERIPHERAL;
3628     cc->header.address.physical.bus = 0;
3629     cc->header.address.physical.target = 0;
3630 
3631     cc->cdb.cdb_length = sizeof(*cnc);
3632     cc->cdb.type = CISS_CDB_TYPE_COMMAND;
3633     cc->cdb.attribute = CISS_CDB_ATTRIBUTE_SIMPLE;
3634     cc->cdb.direction = CISS_CDB_DIRECTION_READ;
3635     cc->cdb.timeout = 0;	/* no timeout, we hope */
3636 
3637     cnc = (struct ciss_notify_cdb *)&(cc->cdb.cdb[0]);
3638     bzero(cr->cr_data, CISS_NOTIFY_DATA_SIZE);
3639     cnc->opcode = CISS_OPCODE_READ;
3640     cnc->command = CISS_COMMAND_NOTIFY_ON_EVENT;
3641     cnc->timeout = 0;		/* no timeout, we hope */
3642     cnc->synchronous = 0;
3643     cnc->ordered = 0;
3644     cnc->seek_to_oldest = 0;
3645     if ((sc->ciss_flags & CISS_FLAG_RUNNING) == 0)
3646 	cnc->new_only = 1;
3647     else
3648 	cnc->new_only = 0;
3649     cnc->length = htonl(CISS_NOTIFY_DATA_SIZE);
3650 
3651     /* submit the request */
3652     error = ciss_start(cr);
3653 
3654  out:
3655     if (error) {
3656 	if (cr != NULL) {
3657 	    if (cr->cr_data != NULL)
3658 		free(cr->cr_data, CISS_MALLOC_CLASS);
3659 	    ciss_release_request(cr);
3660 	}
3661 	sc->ciss_periodic_notify = NULL;
3662 	debug(0, "can't submit notify event request");
3663 	sc->ciss_flags &= ~CISS_FLAG_NOTIFY_OK;
3664     } else {
3665 	debug(1, "notify event submitted");
3666 	sc->ciss_flags |= CISS_FLAG_NOTIFY_OK;
3667     }
3668 }
3669 
3670 static void
3671 ciss_notify_complete(struct ciss_request *cr)
3672 {
3673     struct ciss_command	*cc;
3674     struct ciss_notify	*cn;
3675     struct ciss_softc	*sc;
3676     int			scsi_status;
3677     int			command_status;
3678     debug_called(1);
3679 
3680     cc = cr->cr_cc;
3681     cn = (struct ciss_notify *)cr->cr_data;
3682     sc = cr->cr_sc;
3683 
3684     /*
3685      * Report request results, decode status.
3686      */
3687     ciss_report_request(cr, &command_status, &scsi_status);
3688 
3689     /*
3690      * Abort the chain on a fatal error.
3691      *
3692      * XXX which of these are actually errors?
3693      */
3694     if ((command_status != CISS_CMD_STATUS_SUCCESS) &&
3695 	(command_status != CISS_CMD_STATUS_TARGET_STATUS) &&
3696 	(command_status != CISS_CMD_STATUS_TIMEOUT)) {	/* XXX timeout? */
3697 	ciss_printf(sc, "fatal error in Notify Event request (%s)\n",
3698 		    ciss_name_command_status(command_status));
3699 	ciss_release_request(cr);
3700 	sc->ciss_flags &= ~CISS_FLAG_NOTIFY_OK;
3701 	return;
3702     }
3703 
3704     /*
3705      * If the adapter gave us a text message, print it.
3706      */
3707     if (cn->message[0] != 0)
3708 	ciss_printf(sc, "*** %.80s\n", cn->message);
3709 
3710     debug(0, "notify event class %d subclass %d detail %d",
3711 		cn->class, cn->subclass, cn->detail);
3712 
3713     /*
3714      * If the response indicates that the notifier has been aborted,
3715      * release the notifier command.
3716      */
3717     if ((cn->class == CISS_NOTIFY_NOTIFIER) &&
3718 	(cn->subclass == CISS_NOTIFY_NOTIFIER_STATUS) &&
3719 	(cn->detail == 1)) {
3720 	debug(0, "notifier exiting");
3721 	sc->ciss_flags &= ~CISS_FLAG_NOTIFY_OK;
3722 	ciss_release_request(cr);
3723 	sc->ciss_periodic_notify = NULL;
3724 	wakeup(&sc->ciss_periodic_notify);
3725     } else {
3726 	/* Handle notify events in a kernel thread */
3727 	ciss_enqueue_notify(cr);
3728 	sc->ciss_periodic_notify = NULL;
3729 	wakeup(&sc->ciss_periodic_notify);
3730 	wakeup(&sc->ciss_notify);
3731     }
3732     /*
3733      * Send a new notify event command, if we're not aborting.
3734      */
3735     if (!(sc->ciss_flags & CISS_FLAG_ABORTING)) {
3736 	ciss_notify_event(sc);
3737     }
3738 }
3739 
3740 /************************************************************************
3741  * Abort the Notify Event chain.
3742  *
3743  * Note that we can't just abort the command in progress; we have to
3744  * explicitly issue an Abort Notify Event command in order for the
3745  * adapter to clean up correctly.
3746  *
3747  * If we are called with CISS_FLAG_ABORTING set in the adapter softc,
3748  * the chain will not restart itself.
3749  */
3750 static int
3751 ciss_notify_abort(struct ciss_softc *sc)
3752 {
3753     struct ciss_request		*cr;
3754     struct ciss_command		*cc;
3755     struct ciss_notify_cdb	*cnc;
3756     int				error, command_status, scsi_status;
3757 
3758     debug_called(1);
3759 
3760     cr = NULL;
3761     error = 0;
3762 
3763     /* verify that there's an outstanding command */
3764     if (!(sc->ciss_flags & CISS_FLAG_NOTIFY_OK))
3765 	goto out;
3766 
3767     /* get a command to issue the abort with */
3768     if ((error = ciss_get_request(sc, &cr)))
3769 	goto out;
3770 
3771     /* get a buffer for the result */
3772     if ((cr->cr_data = malloc(CISS_NOTIFY_DATA_SIZE, CISS_MALLOC_CLASS, M_NOWAIT)) == NULL) {
3773 	debug(0, "can't get notify event request buffer");
3774 	error = ENOMEM;
3775 	goto out;
3776     }
3777     cr->cr_length = CISS_NOTIFY_DATA_SIZE;
3778 
3779     /* build the CDB */
3780     cc = cr->cr_cc;
3781     cc->header.address.physical.mode = CISS_HDR_ADDRESS_MODE_PERIPHERAL;
3782     cc->header.address.physical.bus = 0;
3783     cc->header.address.physical.target = 0;
3784     cc->cdb.cdb_length = sizeof(*cnc);
3785     cc->cdb.type = CISS_CDB_TYPE_COMMAND;
3786     cc->cdb.attribute = CISS_CDB_ATTRIBUTE_SIMPLE;
3787     cc->cdb.direction = CISS_CDB_DIRECTION_READ;
3788     cc->cdb.timeout = 0;	/* no timeout, we hope */
3789 
3790     cnc = (struct ciss_notify_cdb *)&(cc->cdb.cdb[0]);
3791     bzero(cnc, sizeof(*cnc));
3792     cnc->opcode = CISS_OPCODE_WRITE;
3793     cnc->command = CISS_COMMAND_ABORT_NOTIFY;
3794     cnc->length = htonl(CISS_NOTIFY_DATA_SIZE);
3795 
3796     ciss_print_request(cr);
3797 
3798     /*
3799      * Submit the request and wait for it to complete.
3800      */
3801     if ((error = ciss_synch_request(cr, 60 * 1000)) != 0) {
3802 	ciss_printf(sc, "Abort Notify Event command failed (%d)\n", error);
3803 	goto out;
3804     }
3805 
3806     /*
3807      * Check response.
3808      */
3809     ciss_report_request(cr, &command_status, &scsi_status);
3810     switch(command_status) {
3811     case CISS_CMD_STATUS_SUCCESS:
3812 	break;
3813     case CISS_CMD_STATUS_INVALID_COMMAND:
3814 	/*
3815 	 * Some older adapters don't support the CISS version of this
3816 	 * command.  Fall back to using the BMIC version.
3817 	 */
3818 	error = ciss_notify_abort_bmic(sc);
3819 	if (error != 0)
3820 	    goto out;
3821 	break;
3822 
3823     case CISS_CMD_STATUS_TARGET_STATUS:
3824 	/*
3825 	 * This can happen if the adapter thinks there wasn't an outstanding
3826 	 * Notify Event command but we did.  We clean up here.
3827 	 */
3828 	if (scsi_status == CISS_SCSI_STATUS_CHECK_CONDITION) {
3829 	    if (sc->ciss_periodic_notify != NULL)
3830 		ciss_release_request(sc->ciss_periodic_notify);
3831 	    error = 0;
3832 	    goto out;
3833 	}
3834 	/* FALLTHROUGH */
3835 
3836     default:
3837 	ciss_printf(sc, "Abort Notify Event command failed (%s)\n",
3838 		    ciss_name_command_status(command_status));
3839 	error = EIO;
3840 	goto out;
3841     }
3842 
3843     /*
3844      * Sleep waiting for the notifier command to complete.  Note
3845      * that if it doesn't, we may end up in a bad situation, since
3846      * the adapter may deliver it later.  Also note that the adapter
3847      * requires the Notify Event command to be cancelled in order to
3848      * maintain internal bookkeeping.
3849      */
3850     while (sc->ciss_periodic_notify != NULL) {
3851 	error = msleep(&sc->ciss_periodic_notify, &sc->ciss_mtx, PRIBIO, "cissNEA", hz * 5);
3852 	if (error == EWOULDBLOCK) {
3853 	    ciss_printf(sc, "Notify Event command failed to abort, adapter may wedge.\n");
3854 	    break;
3855 	}
3856     }
3857 
3858  out:
3859     /* release the cancel request */
3860     if (cr != NULL) {
3861 	if (cr->cr_data != NULL)
3862 	    free(cr->cr_data, CISS_MALLOC_CLASS);
3863 	ciss_release_request(cr);
3864     }
3865     if (error == 0)
3866 	sc->ciss_flags &= ~CISS_FLAG_NOTIFY_OK;
3867     return(error);
3868 }
3869 
3870 /************************************************************************
3871  * Abort the Notify Event chain using a BMIC command.
3872  */
3873 static int
3874 ciss_notify_abort_bmic(struct ciss_softc *sc)
3875 {
3876     struct ciss_request			*cr;
3877     int					error, command_status;
3878 
3879     debug_called(1);
3880 
3881     cr = NULL;
3882     error = 0;
3883 
3884     /* verify that there's an outstanding command */
3885     if (!(sc->ciss_flags & CISS_FLAG_NOTIFY_OK))
3886 	goto out;
3887 
3888     /*
3889      * Build a BMIC command to cancel the Notify on Event command.
3890      *
3891      * Note that we are sending a CISS opcode here.  Odd.
3892      */
3893     if ((error = ciss_get_bmic_request(sc, &cr, CISS_COMMAND_ABORT_NOTIFY,
3894 				       NULL, 0)) != 0)
3895 	goto out;
3896 
3897     /*
3898      * Submit the request and wait for it to complete.
3899      */
3900     if ((error = ciss_synch_request(cr, 60 * 1000)) != 0) {
3901 	ciss_printf(sc, "error sending BMIC Cancel Notify on Event command (%d)\n", error);
3902 	goto out;
3903     }
3904 
3905     /*
3906      * Check response.
3907      */
3908     ciss_report_request(cr, &command_status, NULL);
3909     switch(command_status) {
3910     case CISS_CMD_STATUS_SUCCESS:
3911 	break;
3912     default:
3913 	ciss_printf(sc, "error cancelling Notify on Event (%s)\n",
3914 		    ciss_name_command_status(command_status));
3915 	error = EIO;
3916 	goto out;
3917     }
3918 
3919 out:
3920     if (cr != NULL)
3921 	ciss_release_request(cr);
3922     return(error);
3923 }
3924 
3925 /************************************************************************
3926  * Handle rescanning all the logical volumes when a notify event
3927  * causes the drives to come online or offline.
3928  */
3929 static void
3930 ciss_notify_rescan_logical(struct ciss_softc *sc)
3931 {
3932     struct ciss_lun_report      *cll;
3933     struct ciss_ldrive		*ld;
3934     int                         i, j, ndrives;
3935 
3936     /*
3937      * We must rescan all logical volumes to get the right logical
3938      * drive address.
3939      */
3940     cll = ciss_report_luns(sc, CISS_OPCODE_REPORT_LOGICAL_LUNS,
3941                            sc->ciss_cfg->max_logical_supported);
3942     if (cll == NULL)
3943         return;
3944 
3945     ndrives = (ntohl(cll->list_size) / sizeof(union ciss_device_address));
3946 
3947     /*
3948      * Delete any of the drives which were destroyed by the
3949      * firmware.
3950      */
3951     for (i = 0; i < sc->ciss_max_logical_bus; i++) {
3952 	for (j = 0; j < sc->ciss_cfg->max_logical_supported; j++) {
3953 	    ld = &sc->ciss_logical[i][j];
3954 
3955 	    if (ld->cl_update == 0)
3956 		continue;
3957 
3958 	    if (ld->cl_status != CISS_LD_ONLINE) {
3959 		ciss_cam_rescan_target(sc, i, j);
3960 		ld->cl_update = 0;
3961 		if (ld->cl_ldrive)
3962 		    free(ld->cl_ldrive, CISS_MALLOC_CLASS);
3963 		if (ld->cl_lstatus)
3964 		    free(ld->cl_lstatus, CISS_MALLOC_CLASS);
3965 
3966 		ld->cl_ldrive = NULL;
3967 		ld->cl_lstatus = NULL;
3968 	    }
3969 	}
3970     }
3971 
3972     /*
3973      * Scan for new drives.
3974      */
3975     for (i = 0; i < ndrives; i++) {
3976 	int	bus, target;
3977 
3978 	bus 	= CISS_LUN_TO_BUS(cll->lun[i].logical.lun);
3979 	target	= CISS_LUN_TO_TARGET(cll->lun[i].logical.lun);
3980 	ld	= &sc->ciss_logical[bus][target];
3981 
3982 	if (ld->cl_update == 0)
3983 		continue;
3984 
3985 	ld->cl_update		= 0;
3986 	ld->cl_address		= cll->lun[i];
3987 	ld->cl_controller	= &sc->ciss_controllers[bus];
3988 	if (ciss_identify_logical(sc, ld) == 0) {
3989 	    ciss_cam_rescan_target(sc, bus, target);
3990 	}
3991     }
3992     free(cll, CISS_MALLOC_CLASS);
3993 }
3994 
3995 /************************************************************************
3996  * Handle a notify event relating to the status of a logical drive.
3997  *
3998  * XXX need to be able to defer some of these to properly handle
3999  *     calling the "ID Physical drive" command, unless the 'extended'
4000  *     drive IDs are always in BIG_MAP format.
4001  */
4002 static void
4003 ciss_notify_logical(struct ciss_softc *sc, struct ciss_notify *cn)
4004 {
4005     struct ciss_ldrive	*ld;
4006     int			bus, target;
4007     int			rescan_ld;
4008 
4009     debug_called(2);
4010 
4011     bus		= cn->device.physical.bus;
4012     target	= cn->data.logical_status.logical_drive;
4013     ld		= &sc->ciss_logical[bus][target];
4014 
4015     switch (cn->subclass) {
4016     case CISS_NOTIFY_LOGICAL_STATUS:
4017 	switch (cn->detail) {
4018 	case 0:
4019 	    ciss_name_device(sc, bus, target);
4020 	    ciss_printf(sc, "logical drive %d (%s) changed status %s->%s, spare status 0x%b\n",
4021 			cn->data.logical_status.logical_drive, ld->cl_name,
4022 			ciss_name_ldrive_status(cn->data.logical_status.previous_state),
4023 			ciss_name_ldrive_status(cn->data.logical_status.new_state),
4024 			cn->data.logical_status.spare_state,
4025 			"\20\1configured\2rebuilding\3failed\4in use\5available\n");
4026 
4027 	    /*
4028 	     * Update our idea of the drive's status.
4029 	     */
4030 	    ld->cl_status = ciss_decode_ldrive_status(cn->data.logical_status.new_state);
4031 	    if (ld->cl_lstatus != NULL)
4032 		ld->cl_lstatus->status = cn->data.logical_status.new_state;
4033 
4034 	    /*
4035 	     * Have CAM rescan the drive if its status has changed.
4036 	     */
4037             rescan_ld = (cn->data.logical_status.previous_state !=
4038                          cn->data.logical_status.new_state) ? 1 : 0;
4039 	    if (rescan_ld) {
4040 		ld->cl_update = 1;
4041 		ciss_notify_rescan_logical(sc);
4042 	    }
4043 
4044 	    break;
4045 
4046 	case 1:	/* logical drive has recognised new media, needs Accept Media Exchange */
4047 	    ciss_name_device(sc, bus, target);
4048 	    ciss_printf(sc, "logical drive %d (%s) media exchanged, ready to go online\n",
4049 			cn->data.logical_status.logical_drive, ld->cl_name);
4050 	    ciss_accept_media(sc, ld);
4051 
4052 	    ld->cl_update = 1;
4053 	    ld->cl_status = ciss_decode_ldrive_status(cn->data.logical_status.new_state);
4054 	    ciss_notify_rescan_logical(sc);
4055 	    break;
4056 
4057 	case 2:
4058 	case 3:
4059 	    ciss_printf(sc, "rebuild of logical drive %d (%s) failed due to %s error\n",
4060 			cn->data.rebuild_aborted.logical_drive,
4061 			ld->cl_name,
4062 			(cn->detail == 2) ? "read" : "write");
4063 	    break;
4064 	}
4065 	break;
4066 
4067     case CISS_NOTIFY_LOGICAL_ERROR:
4068 	if (cn->detail == 0) {
4069 	    ciss_printf(sc, "FATAL I/O ERROR on logical drive %d (%s), SCSI port %d ID %d\n",
4070 			cn->data.io_error.logical_drive,
4071 			ld->cl_name,
4072 			cn->data.io_error.failure_bus,
4073 			cn->data.io_error.failure_drive);
4074 	    /* XXX should we take the drive down at this point, or will we be told? */
4075 	}
4076 	break;
4077 
4078     case CISS_NOTIFY_LOGICAL_SURFACE:
4079 	if (cn->detail == 0)
4080 	    ciss_printf(sc, "logical drive %d (%s) completed consistency initialisation\n",
4081 			cn->data.consistency_completed.logical_drive,
4082 			ld->cl_name);
4083 	break;
4084     }
4085 }
4086 
4087 /************************************************************************
4088  * Handle a notify event relating to the status of a physical drive.
4089  */
4090 static void
4091 ciss_notify_physical(struct ciss_softc *sc, struct ciss_notify *cn)
4092 {
4093 }
4094 
4095 /************************************************************************
4096  * Handle a notify event relating to the status of a physical drive.
4097  */
4098 static void
4099 ciss_notify_hotplug(struct ciss_softc *sc, struct ciss_notify *cn)
4100 {
4101     struct ciss_lun_report *cll = NULL;
4102     int bus, target;
4103 
4104     switch (cn->subclass) {
4105     case CISS_NOTIFY_HOTPLUG_PHYSICAL:
4106     case CISS_NOTIFY_HOTPLUG_NONDISK:
4107 	bus = CISS_BIG_MAP_BUS(sc, cn->data.drive.big_physical_drive_number);
4108 	target =
4109 	    CISS_BIG_MAP_TARGET(sc, cn->data.drive.big_physical_drive_number);
4110 
4111 	if (cn->detail == 0) {
4112 	    /*
4113 	     * Mark the device offline so that it'll start producing selection
4114 	     * timeouts to the upper layer.
4115 	     */
4116 	    if ((bus >= 0) && (target >= 0))
4117 		sc->ciss_physical[bus][target].cp_online = 0;
4118 	} else {
4119 	    /*
4120 	     * Rescan the physical lun list for new items
4121 	     */
4122 	    cll = ciss_report_luns(sc, CISS_OPCODE_REPORT_PHYSICAL_LUNS,
4123 				   sc->ciss_cfg->max_physical_supported);
4124 	    if (cll == NULL) {
4125 		ciss_printf(sc, "Warning, cannot get physical lun list\n");
4126 		break;
4127 	    }
4128 	    ciss_filter_physical(sc, cll);
4129 	}
4130 	break;
4131 
4132     default:
4133 	ciss_printf(sc, "Unknown hotplug event %d\n", cn->subclass);
4134 	return;
4135     }
4136 
4137     if (cll != NULL)
4138 	free(cll, CISS_MALLOC_CLASS);
4139 }
4140 
4141 /************************************************************************
4142  * Handle deferred processing of notify events.  Notify events may need
4143  * sleep which is unsafe during an interrupt.
4144  */
4145 static void
4146 ciss_notify_thread(void *arg)
4147 {
4148     struct ciss_softc		*sc;
4149     struct ciss_request		*cr;
4150     struct ciss_notify		*cn;
4151 
4152     sc = (struct ciss_softc *)arg;
4153 #if __FreeBSD_version >= 500000
4154     mtx_lock(&sc->ciss_mtx);
4155 #endif
4156 
4157     for (;;) {
4158 	if (STAILQ_EMPTY(&sc->ciss_notify) != 0 &&
4159 	    (sc->ciss_flags & CISS_FLAG_THREAD_SHUT) == 0) {
4160 	    msleep(&sc->ciss_notify, &sc->ciss_mtx, PUSER, "idle", 0);
4161 	}
4162 
4163 	if (sc->ciss_flags & CISS_FLAG_THREAD_SHUT)
4164 	    break;
4165 
4166 	cr = ciss_dequeue_notify(sc);
4167 
4168 	if (cr == NULL)
4169 		panic("cr null");
4170 	cn = (struct ciss_notify *)cr->cr_data;
4171 
4172 	switch (cn->class) {
4173 	case CISS_NOTIFY_HOTPLUG:
4174 	    ciss_notify_hotplug(sc, cn);
4175 	    break;
4176 	case CISS_NOTIFY_LOGICAL:
4177 	    ciss_notify_logical(sc, cn);
4178 	    break;
4179 	case CISS_NOTIFY_PHYSICAL:
4180 	    ciss_notify_physical(sc, cn);
4181 	    break;
4182 	}
4183 
4184 	ciss_release_request(cr);
4185 
4186     }
4187     sc->ciss_notify_thread = NULL;
4188     wakeup(&sc->ciss_notify_thread);
4189 
4190 #if __FreeBSD_version >= 500000
4191     mtx_unlock(&sc->ciss_mtx);
4192 #endif
4193     kproc_exit(0);
4194 }
4195 
4196 /************************************************************************
4197  * Start the notification kernel thread.
4198  */
4199 static void
4200 ciss_spawn_notify_thread(struct ciss_softc *sc)
4201 {
4202 
4203 #if __FreeBSD_version > 500005
4204     if (kproc_create((void(*)(void *))ciss_notify_thread, sc,
4205 		       &sc->ciss_notify_thread, 0, 0, "ciss_notify%d",
4206 		       device_get_unit(sc->ciss_dev)))
4207 #else
4208     if (kproc_create((void(*)(void *))ciss_notify_thread, sc,
4209 		       &sc->ciss_notify_thread, "ciss_notify%d",
4210 		       device_get_unit(sc->ciss_dev)))
4211 #endif
4212 	panic("Could not create notify thread\n");
4213 }
4214 
4215 /************************************************************************
4216  * Kill the notification kernel thread.
4217  */
4218 static void
4219 ciss_kill_notify_thread(struct ciss_softc *sc)
4220 {
4221 
4222     if (sc->ciss_notify_thread == NULL)
4223 	return;
4224 
4225     sc->ciss_flags |= CISS_FLAG_THREAD_SHUT;
4226     wakeup(&sc->ciss_notify);
4227     msleep(&sc->ciss_notify_thread, &sc->ciss_mtx, PUSER, "thtrm", 0);
4228 }
4229 
4230 /************************************************************************
4231  * Print a request.
4232  */
4233 static void
4234 ciss_print_request(struct ciss_request *cr)
4235 {
4236     struct ciss_softc	*sc;
4237     struct ciss_command	*cc;
4238     int			i;
4239 
4240     sc = cr->cr_sc;
4241     cc = cr->cr_cc;
4242 
4243     ciss_printf(sc, "REQUEST @ %p\n", cr);
4244     ciss_printf(sc, "  data %p/%d  tag %d  flags %b\n",
4245 	      cr->cr_data, cr->cr_length, cr->cr_tag, cr->cr_flags,
4246 	      "\20\1mapped\2sleep\3poll\4dataout\5datain\n");
4247     ciss_printf(sc, "  sg list/total %d/%d  host tag 0x%x\n",
4248 		cc->header.sg_in_list, cc->header.sg_total, cc->header.host_tag);
4249     switch(cc->header.address.mode.mode) {
4250     case CISS_HDR_ADDRESS_MODE_PERIPHERAL:
4251     case CISS_HDR_ADDRESS_MODE_MASK_PERIPHERAL:
4252 	ciss_printf(sc, "  physical bus %d target %d\n",
4253 		    cc->header.address.physical.bus, cc->header.address.physical.target);
4254 	break;
4255     case CISS_HDR_ADDRESS_MODE_LOGICAL:
4256 	ciss_printf(sc, "  logical unit %d\n", cc->header.address.logical.lun);
4257 	break;
4258     }
4259     ciss_printf(sc, "  %s cdb length %d type %s attribute %s\n",
4260 		(cc->cdb.direction == CISS_CDB_DIRECTION_NONE) ? "no-I/O" :
4261 		(cc->cdb.direction == CISS_CDB_DIRECTION_READ) ? "READ" :
4262 		(cc->cdb.direction == CISS_CDB_DIRECTION_WRITE) ? "WRITE" : "??",
4263 		cc->cdb.cdb_length,
4264 		(cc->cdb.type == CISS_CDB_TYPE_COMMAND) ? "command" :
4265 		(cc->cdb.type == CISS_CDB_TYPE_MESSAGE) ? "message" : "??",
4266 		(cc->cdb.attribute == CISS_CDB_ATTRIBUTE_UNTAGGED) ? "untagged" :
4267 		(cc->cdb.attribute == CISS_CDB_ATTRIBUTE_SIMPLE) ? "simple" :
4268 		(cc->cdb.attribute == CISS_CDB_ATTRIBUTE_HEAD_OF_QUEUE) ? "head-of-queue" :
4269 		(cc->cdb.attribute == CISS_CDB_ATTRIBUTE_ORDERED) ? "ordered" :
4270 		(cc->cdb.attribute == CISS_CDB_ATTRIBUTE_AUTO_CONTINGENT) ? "auto-contingent" : "??");
4271     ciss_printf(sc, "  %*D\n", cc->cdb.cdb_length, &cc->cdb.cdb[0], " ");
4272 
4273     if (cc->header.host_tag & CISS_HDR_HOST_TAG_ERROR) {
4274 	/* XXX print error info */
4275     } else {
4276 	/* since we don't use chained s/g, don't support it here */
4277 	for (i = 0; i < cc->header.sg_in_list; i++) {
4278 	    if ((i % 4) == 0)
4279 		ciss_printf(sc, "   ");
4280 	    printf("0x%08x/%d ", (u_int32_t)cc->sg[i].address, cc->sg[i].length);
4281 	    if ((((i + 1) % 4) == 0) || (i == (cc->header.sg_in_list - 1)))
4282 		printf("\n");
4283 	}
4284     }
4285 }
4286 
4287 /************************************************************************
4288  * Print information about the status of a logical drive.
4289  */
4290 static void
4291 ciss_print_ldrive(struct ciss_softc *sc, struct ciss_ldrive *ld)
4292 {
4293     int		bus, target, i;
4294 
4295     if (ld->cl_lstatus == NULL) {
4296 	printf("does not exist\n");
4297 	return;
4298     }
4299 
4300     /* print drive status */
4301     switch(ld->cl_lstatus->status) {
4302     case CISS_LSTATUS_OK:
4303 	printf("online\n");
4304 	break;
4305     case CISS_LSTATUS_INTERIM_RECOVERY:
4306 	printf("in interim recovery mode\n");
4307 	break;
4308     case CISS_LSTATUS_READY_RECOVERY:
4309 	printf("ready to begin recovery\n");
4310 	break;
4311     case CISS_LSTATUS_RECOVERING:
4312 	bus = CISS_BIG_MAP_BUS(sc, ld->cl_lstatus->drive_rebuilding);
4313 	target = CISS_BIG_MAP_BUS(sc, ld->cl_lstatus->drive_rebuilding);
4314 	printf("being recovered, working on physical drive %d.%d, %u blocks remaining\n",
4315 	       bus, target, ld->cl_lstatus->blocks_to_recover);
4316 	break;
4317     case CISS_LSTATUS_EXPANDING:
4318 	printf("being expanded, %u blocks remaining\n",
4319 	       ld->cl_lstatus->blocks_to_recover);
4320 	break;
4321     case CISS_LSTATUS_QUEUED_FOR_EXPANSION:
4322 	printf("queued for expansion\n");
4323 	break;
4324     case CISS_LSTATUS_FAILED:
4325 	printf("queued for expansion\n");
4326 	break;
4327     case CISS_LSTATUS_WRONG_PDRIVE:
4328 	printf("wrong physical drive inserted\n");
4329 	break;
4330     case CISS_LSTATUS_MISSING_PDRIVE:
4331 	printf("missing a needed physical drive\n");
4332 	break;
4333     case CISS_LSTATUS_BECOMING_READY:
4334 	printf("becoming ready\n");
4335 	break;
4336     }
4337 
4338     /* print failed physical drives */
4339     for (i = 0; i < CISS_BIG_MAP_ENTRIES / 8; i++) {
4340 	bus = CISS_BIG_MAP_BUS(sc, ld->cl_lstatus->drive_failure_map[i]);
4341 	target = CISS_BIG_MAP_TARGET(sc, ld->cl_lstatus->drive_failure_map[i]);
4342 	if (bus == -1)
4343 	    continue;
4344 	ciss_printf(sc, "physical drive %d:%d (%x) failed\n", bus, target,
4345 		    ld->cl_lstatus->drive_failure_map[i]);
4346     }
4347 }
4348 
4349 #ifdef CISS_DEBUG
4350 #include "opt_ddb.h"
4351 #ifdef DDB
4352 #include <ddb/ddb.h>
4353 /************************************************************************
4354  * Print information about the controller/driver.
4355  */
4356 static void
4357 ciss_print_adapter(struct ciss_softc *sc)
4358 {
4359     int		i, j;
4360 
4361     ciss_printf(sc, "ADAPTER:\n");
4362     for (i = 0; i < CISSQ_COUNT; i++) {
4363 	ciss_printf(sc, "%s     %d/%d\n",
4364 	    i == 0 ? "free" :
4365 	    i == 1 ? "busy" : "complete",
4366 	    sc->ciss_qstat[i].q_length,
4367 	    sc->ciss_qstat[i].q_max);
4368     }
4369     ciss_printf(sc, "max_requests %d\n", sc->ciss_max_requests);
4370     ciss_printf(sc, "flags %b\n", sc->ciss_flags,
4371 	"\20\1notify_ok\2control_open\3aborting\4running\21fake_synch\22bmic_abort\n");
4372 
4373     for (i = 0; i < sc->ciss_max_logical_bus; i++) {
4374 	for (j = 0; j < sc->ciss_cfg->max_logical_supported; j++) {
4375 	    ciss_printf(sc, "LOGICAL DRIVE %d:  ", i);
4376 	    ciss_print_ldrive(sc, &sc->ciss_logical[i][j]);
4377 	}
4378     }
4379 
4380     /* XXX Should physical drives be printed out here? */
4381 
4382     for (i = 1; i < sc->ciss_max_requests; i++)
4383 	ciss_print_request(sc->ciss_request + i);
4384 }
4385 
4386 /* DDB hook */
4387 DB_COMMAND(ciss_prt, db_ciss_prt)
4388 {
4389     struct ciss_softc	*sc;
4390     devclass_t dc;
4391     int maxciss, i;
4392 
4393     dc = devclass_find("ciss");
4394     if ( dc == NULL ) {
4395         printf("%s: can't find devclass!\n", __func__);
4396         return;
4397     }
4398     maxciss = devclass_get_maxunit(dc);
4399     for (i = 0; i < maxciss; i++) {
4400         sc = devclass_get_softc(dc, i);
4401 	ciss_print_adapter(sc);
4402     }
4403 }
4404 #endif
4405 #endif
4406 
4407 /************************************************************************
4408  * Return a name for a logical drive status value.
4409  */
4410 static const char *
4411 ciss_name_ldrive_status(int status)
4412 {
4413     switch (status) {
4414     case CISS_LSTATUS_OK:
4415 	return("OK");
4416     case CISS_LSTATUS_FAILED:
4417 	return("failed");
4418     case CISS_LSTATUS_NOT_CONFIGURED:
4419 	return("not configured");
4420     case CISS_LSTATUS_INTERIM_RECOVERY:
4421 	return("interim recovery");
4422     case CISS_LSTATUS_READY_RECOVERY:
4423 	return("ready for recovery");
4424     case CISS_LSTATUS_RECOVERING:
4425 	return("recovering");
4426     case CISS_LSTATUS_WRONG_PDRIVE:
4427 	return("wrong physical drive inserted");
4428     case CISS_LSTATUS_MISSING_PDRIVE:
4429 	return("missing physical drive");
4430     case CISS_LSTATUS_EXPANDING:
4431 	return("expanding");
4432     case CISS_LSTATUS_BECOMING_READY:
4433 	return("becoming ready");
4434     case CISS_LSTATUS_QUEUED_FOR_EXPANSION:
4435 	return("queued for expansion");
4436     }
4437     return("unknown status");
4438 }
4439 
4440 /************************************************************************
4441  * Return an online/offline/nonexistent value for a logical drive
4442  * status value.
4443  */
4444 static int
4445 ciss_decode_ldrive_status(int status)
4446 {
4447     switch(status) {
4448     case CISS_LSTATUS_NOT_CONFIGURED:
4449 	return(CISS_LD_NONEXISTENT);
4450 
4451     case CISS_LSTATUS_OK:
4452     case CISS_LSTATUS_INTERIM_RECOVERY:
4453     case CISS_LSTATUS_READY_RECOVERY:
4454     case CISS_LSTATUS_RECOVERING:
4455     case CISS_LSTATUS_EXPANDING:
4456     case CISS_LSTATUS_QUEUED_FOR_EXPANSION:
4457 	return(CISS_LD_ONLINE);
4458 
4459     case CISS_LSTATUS_FAILED:
4460     case CISS_LSTATUS_WRONG_PDRIVE:
4461     case CISS_LSTATUS_MISSING_PDRIVE:
4462     case CISS_LSTATUS_BECOMING_READY:
4463     default:
4464 	return(CISS_LD_OFFLINE);
4465     }
4466 }
4467 
4468 
4469 /************************************************************************
4470  * Return a name for a logical drive's organisation.
4471  */
4472 static const char *
4473 ciss_name_ldrive_org(int org)
4474 {
4475     switch(org) {
4476     case CISS_LDRIVE_RAID0:
4477 	return("RAID 0");
4478     case CISS_LDRIVE_RAID1:
4479 	return("RAID 1(1+0)");
4480     case CISS_LDRIVE_RAID4:
4481 	return("RAID 4");
4482     case CISS_LDRIVE_RAID5:
4483 	return("RAID 5");
4484     case CISS_LDRIVE_RAID51:
4485 	return("RAID 5+1");
4486     case CISS_LDRIVE_RAIDADG:
4487 	return("RAID ADG");
4488     }
4489     return("unkown");
4490 }
4491 
4492 /************************************************************************
4493  * Return a name for a command status value.
4494  */
4495 static const char *
4496 ciss_name_command_status(int status)
4497 {
4498     switch(status) {
4499     case CISS_CMD_STATUS_SUCCESS:
4500 	return("success");
4501     case CISS_CMD_STATUS_TARGET_STATUS:
4502 	return("target status");
4503     case CISS_CMD_STATUS_DATA_UNDERRUN:
4504 	return("data underrun");
4505     case CISS_CMD_STATUS_DATA_OVERRUN:
4506 	return("data overrun");
4507     case CISS_CMD_STATUS_INVALID_COMMAND:
4508 	return("invalid command");
4509     case CISS_CMD_STATUS_PROTOCOL_ERROR:
4510 	return("protocol error");
4511     case CISS_CMD_STATUS_HARDWARE_ERROR:
4512 	return("hardware error");
4513     case CISS_CMD_STATUS_CONNECTION_LOST:
4514 	return("connection lost");
4515     case CISS_CMD_STATUS_ABORTED:
4516 	return("aborted");
4517     case CISS_CMD_STATUS_ABORT_FAILED:
4518 	return("abort failed");
4519     case CISS_CMD_STATUS_UNSOLICITED_ABORT:
4520 	return("unsolicited abort");
4521     case CISS_CMD_STATUS_TIMEOUT:
4522 	return("timeout");
4523     case CISS_CMD_STATUS_UNABORTABLE:
4524 	return("unabortable");
4525     }
4526     return("unknown status");
4527 }
4528 
4529 /************************************************************************
4530  * Handle an open on the control device.
4531  */
4532 static int
4533 ciss_open(struct cdev *dev, int flags, int fmt, struct thread *p)
4534 {
4535     struct ciss_softc	*sc;
4536 
4537     debug_called(1);
4538 
4539     sc = (struct ciss_softc *)dev->si_drv1;
4540 
4541     /* we might want to veto if someone already has us open */
4542 
4543     mtx_lock(&sc->ciss_mtx);
4544     sc->ciss_flags |= CISS_FLAG_CONTROL_OPEN;
4545     mtx_unlock(&sc->ciss_mtx);
4546     return(0);
4547 }
4548 
4549 /************************************************************************
4550  * Handle the last close on the control device.
4551  */
4552 static int
4553 ciss_close(struct cdev *dev, int flags, int fmt, struct thread *p)
4554 {
4555     struct ciss_softc	*sc;
4556 
4557     debug_called(1);
4558 
4559     sc = (struct ciss_softc *)dev->si_drv1;
4560 
4561     mtx_lock(&sc->ciss_mtx);
4562     sc->ciss_flags &= ~CISS_FLAG_CONTROL_OPEN;
4563     mtx_unlock(&sc->ciss_mtx);
4564     return (0);
4565 }
4566 
4567 /********************************************************************************
4568  * Handle adapter-specific control operations.
4569  *
4570  * Note that the API here is compatible with the Linux driver, in order to
4571  * simplify the porting of Compaq's userland tools.
4572  */
4573 static int
4574 ciss_ioctl(struct cdev *dev, u_long cmd, caddr_t addr, int32_t flag, struct thread *p)
4575 {
4576     struct ciss_softc		*sc;
4577     IOCTL_Command_struct	*ioc	= (IOCTL_Command_struct *)addr;
4578 #ifdef __amd64__
4579     IOCTL_Command_struct32	*ioc32	= (IOCTL_Command_struct32 *)addr;
4580     IOCTL_Command_struct	ioc_swab;
4581 #endif
4582     int				error;
4583 
4584     debug_called(1);
4585 
4586     sc = (struct ciss_softc *)dev->si_drv1;
4587     error = 0;
4588     mtx_lock(&sc->ciss_mtx);
4589 
4590     switch(cmd) {
4591     case CCISS_GETQSTATS:
4592     {
4593 	union ciss_statrequest *cr = (union ciss_statrequest *)addr;
4594 
4595 	switch (cr->cs_item) {
4596 	case CISSQ_FREE:
4597 	case CISSQ_NOTIFY:
4598 	    bcopy(&sc->ciss_qstat[cr->cs_item], &cr->cs_qstat,
4599 		sizeof(struct ciss_qstat));
4600 	    break;
4601 	default:
4602 	    error = ENOIOCTL;
4603 	    break;
4604 	}
4605 
4606 	break;
4607     }
4608 
4609     case CCISS_GETPCIINFO:
4610     {
4611 	cciss_pci_info_struct	*pis = (cciss_pci_info_struct *)addr;
4612 
4613 	pis->bus = pci_get_bus(sc->ciss_dev);
4614 	pis->dev_fn = pci_get_slot(sc->ciss_dev);
4615         pis->board_id = (pci_get_subvendor(sc->ciss_dev) << 16) |
4616                 pci_get_subdevice(sc->ciss_dev);
4617 
4618 	break;
4619     }
4620 
4621     case CCISS_GETINTINFO:
4622     {
4623 	cciss_coalint_struct	*cis = (cciss_coalint_struct *)addr;
4624 
4625 	cis->delay = sc->ciss_cfg->interrupt_coalesce_delay;
4626 	cis->count = sc->ciss_cfg->interrupt_coalesce_count;
4627 
4628 	break;
4629     }
4630 
4631     case CCISS_SETINTINFO:
4632     {
4633 	cciss_coalint_struct	*cis = (cciss_coalint_struct *)addr;
4634 
4635 	if ((cis->delay == 0) && (cis->count == 0)) {
4636 	    error = EINVAL;
4637 	    break;
4638 	}
4639 
4640 	/*
4641 	 * XXX apparently this is only safe if the controller is idle,
4642 	 *     we should suspend it before doing this.
4643 	 */
4644 	sc->ciss_cfg->interrupt_coalesce_delay = cis->delay;
4645 	sc->ciss_cfg->interrupt_coalesce_count = cis->count;
4646 
4647 	if (ciss_update_config(sc))
4648 	    error = EIO;
4649 
4650 	/* XXX resume the controller here */
4651 	break;
4652     }
4653 
4654     case CCISS_GETNODENAME:
4655 	bcopy(sc->ciss_cfg->server_name, (NodeName_type *)addr,
4656 	      sizeof(NodeName_type));
4657 	break;
4658 
4659     case CCISS_SETNODENAME:
4660 	bcopy((NodeName_type *)addr, sc->ciss_cfg->server_name,
4661 	      sizeof(NodeName_type));
4662 	if (ciss_update_config(sc))
4663 	    error = EIO;
4664 	break;
4665 
4666     case CCISS_GETHEARTBEAT:
4667 	*(Heartbeat_type *)addr = sc->ciss_cfg->heartbeat;
4668 	break;
4669 
4670     case CCISS_GETBUSTYPES:
4671 	*(BusTypes_type *)addr = sc->ciss_cfg->bus_types;
4672 	break;
4673 
4674     case CCISS_GETFIRMVER:
4675 	bcopy(sc->ciss_id->running_firmware_revision, (FirmwareVer_type *)addr,
4676 	      sizeof(FirmwareVer_type));
4677 	break;
4678 
4679     case CCISS_GETDRIVERVER:
4680 	*(DriverVer_type *)addr = CISS_DRIVER_VERSION;
4681 	break;
4682 
4683     case CCISS_REVALIDVOLS:
4684 	/*
4685 	 * This is a bit ugly; to do it "right" we really need
4686 	 * to find any disks that have changed, kick CAM off them,
4687 	 * then rescan only these disks.  It'd be nice if they
4688 	 * a) told us which disk(s) they were going to play with,
4689 	 * and b) which ones had arrived. 8(
4690 	 */
4691 	break;
4692 
4693 #ifdef __amd64__
4694     case CCISS_PASSTHRU32:
4695 	ioc_swab.LUN_info	= ioc32->LUN_info;
4696 	ioc_swab.Request	= ioc32->Request;
4697 	ioc_swab.error_info	= ioc32->error_info;
4698 	ioc_swab.buf_size	= ioc32->buf_size;
4699 	ioc_swab.buf		= (u_int8_t *)(uintptr_t)ioc32->buf;
4700 	ioc			= &ioc_swab;
4701 	/* FALLTHROUGH */
4702 #endif
4703 
4704     case CCISS_PASSTHRU:
4705 	error = ciss_user_command(sc, ioc);
4706 	break;
4707 
4708     default:
4709 	debug(0, "unknown ioctl 0x%lx", cmd);
4710 
4711 	debug(1, "CCISS_GETPCIINFO:   0x%lx", CCISS_GETPCIINFO);
4712 	debug(1, "CCISS_GETINTINFO:   0x%lx", CCISS_GETINTINFO);
4713 	debug(1, "CCISS_SETINTINFO:   0x%lx", CCISS_SETINTINFO);
4714 	debug(1, "CCISS_GETNODENAME:  0x%lx", CCISS_GETNODENAME);
4715 	debug(1, "CCISS_SETNODENAME:  0x%lx", CCISS_SETNODENAME);
4716 	debug(1, "CCISS_GETHEARTBEAT: 0x%lx", CCISS_GETHEARTBEAT);
4717 	debug(1, "CCISS_GETBUSTYPES:  0x%lx", CCISS_GETBUSTYPES);
4718 	debug(1, "CCISS_GETFIRMVER:   0x%lx", CCISS_GETFIRMVER);
4719 	debug(1, "CCISS_GETDRIVERVER: 0x%lx", CCISS_GETDRIVERVER);
4720 	debug(1, "CCISS_REVALIDVOLS:  0x%lx", CCISS_REVALIDVOLS);
4721 	debug(1, "CCISS_PASSTHRU:     0x%lx", CCISS_PASSTHRU);
4722 
4723 	error = ENOIOCTL;
4724 	break;
4725     }
4726 
4727     mtx_unlock(&sc->ciss_mtx);
4728     return(error);
4729 }
4730