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