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