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