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