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