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