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