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