xref: /freebsd/sys/dev/ciss/ciss.c (revision 6780ab54325a71e7e70112b11657973edde8655e)
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 static int
1015 ciss_inquiry_logical(struct ciss_softc *sc, struct ciss_ldrive *ld)
1016 {
1017     struct ciss_request			*cr;
1018     struct ciss_command			*cc;
1019     struct scsi_inquiry			*inq;
1020     int					error;
1021     int					command_status;
1022     int					lun;
1023 
1024     cr = NULL;
1025     lun = ld->cl_address.logical.lun;
1026 
1027     bzero(&ld->cl_geometry, sizeof(ld->cl_geometry));
1028 
1029     if ((error = ciss_get_request(sc, &cr)) != 0)
1030 	goto out;
1031 
1032     cc = CISS_FIND_COMMAND(cr);
1033     cr->cr_data = &ld->cl_geometry;
1034     cr->cr_length = sizeof(ld->cl_geometry);
1035     cr->cr_flags = CISS_REQ_DATAIN;
1036 
1037     cc->header.address.logical.mode = CISS_HDR_ADDRESS_MODE_LOGICAL;
1038     cc->header.address.logical.lun  = lun;
1039     cc->cdb.cdb_length = 6;
1040     cc->cdb.type = CISS_CDB_TYPE_COMMAND;
1041     cc->cdb.attribute = CISS_CDB_ATTRIBUTE_SIMPLE;
1042     cc->cdb.direction = CISS_CDB_DIRECTION_READ;
1043     cc->cdb.timeout = 30;
1044 
1045     inq = (struct scsi_inquiry *)&(cc->cdb.cdb[0]);
1046     inq->opcode = INQUIRY;
1047     inq->byte2 = SI_EVPD;
1048     inq->page_code = CISS_VPD_LOGICAL_DRIVE_GEOMETRY;
1049     inq->length = sizeof(ld->cl_geometry);
1050 
1051     if ((error = ciss_synch_request(cr, 60 * 1000)) != 0) {
1052 	ciss_printf(sc, "error getting geometry (%d)\n", error);
1053 	goto out;
1054     }
1055 
1056     ciss_report_request(cr, &command_status, NULL);
1057     switch(command_status) {
1058     case CISS_CMD_STATUS_SUCCESS:
1059     case CISS_CMD_STATUS_DATA_UNDERRUN:
1060 	break;
1061     case CISS_CMD_STATUS_DATA_OVERRUN:
1062 	ciss_printf(sc, "WARNING: Data overrun\n");
1063 	break;
1064     default:
1065 	ciss_printf(sc, "Error detecting logical drive geometry (%s)\n",
1066 		    ciss_name_command_status(command_status));
1067 	break;
1068     }
1069 
1070 out:
1071     if (cr != NULL)
1072 	ciss_release_request(cr);
1073     return(error);
1074 }
1075 /************************************************************************
1076  * Identify a logical drive, initialise state related to it.
1077  */
1078 static int
1079 ciss_identify_logical(struct ciss_softc *sc, struct ciss_ldrive *ld)
1080 {
1081     struct ciss_request		*cr;
1082     struct ciss_command		*cc;
1083     struct ciss_bmic_cdb	*cbc;
1084     int				error, command_status;
1085 
1086     debug_called(1);
1087 
1088     cr = NULL;
1089 
1090     /*
1091      * Build a BMIC request to fetch the drive ID.
1092      */
1093     if ((error = ciss_get_bmic_request(sc, &cr, CISS_BMIC_ID_LDRIVE,
1094 				       (void **)&ld->cl_ldrive,
1095 				       sizeof(*ld->cl_ldrive))) != 0)
1096 	goto out;
1097     cc = CISS_FIND_COMMAND(cr);
1098     cbc = (struct ciss_bmic_cdb *)&(cc->cdb.cdb[0]);
1099     cbc->log_drive = ld->cl_address.logical.lun;
1100 
1101     /*
1102      * Submit the request and wait for it to complete.
1103      */
1104     if ((error = ciss_synch_request(cr, 60 * 1000)) != 0) {
1105 	ciss_printf(sc, "error sending BMIC LDRIVE command (%d)\n", error);
1106 	goto out;
1107     }
1108 
1109     /*
1110      * Check response.
1111      */
1112     ciss_report_request(cr, &command_status, NULL);
1113     switch(command_status) {
1114     case CISS_CMD_STATUS_SUCCESS:		/* buffer right size */
1115 	break;
1116     case CISS_CMD_STATUS_DATA_UNDERRUN:
1117     case CISS_CMD_STATUS_DATA_OVERRUN:
1118 	ciss_printf(sc, "data over/underrun reading logical drive ID\n");
1119     default:
1120 	ciss_printf(sc, "error reading logical drive ID (%s)\n",
1121 		    ciss_name_command_status(command_status));
1122 	error = EIO;
1123 	goto out;
1124     }
1125     ciss_release_request(cr);
1126     cr = NULL;
1127 
1128     /*
1129      * Build a CISS BMIC command to get the logical drive status.
1130      */
1131     if ((error = ciss_get_ldrive_status(sc, ld)) != 0)
1132 	goto out;
1133 
1134     /*
1135      * Get the logical drive geometry.
1136      */
1137     if ((error = ciss_inquiry_logical(sc, ld)) != 0)
1138 	goto out;
1139 
1140     /*
1141      * Print the drive's basic characteristics.
1142      */
1143     if (1/*bootverbose*/) {
1144 	ciss_printf(sc, "logical drive %d: %s, %dMB ",
1145 		    cbc->log_drive, ciss_name_ldrive_org(ld->cl_ldrive->fault_tolerance),
1146 		    ((ld->cl_ldrive->blocks_available / (1024 * 1024)) *
1147 		     ld->cl_ldrive->block_size));
1148 
1149 	ciss_print_ldrive(sc, ld);
1150     }
1151 out:
1152     if (error != 0) {
1153 	/* make the drive not-exist */
1154 	ld->cl_status = CISS_LD_NONEXISTENT;
1155 	if (ld->cl_ldrive != NULL) {
1156 	    free(ld->cl_ldrive, CISS_MALLOC_CLASS);
1157 	    ld->cl_ldrive = NULL;
1158 	}
1159 	if (ld->cl_lstatus != NULL) {
1160 	    free(ld->cl_lstatus, CISS_MALLOC_CLASS);
1161 	    ld->cl_lstatus = NULL;
1162 	}
1163     }
1164     if (cr != NULL)
1165 	ciss_release_request(cr);
1166 
1167     return(error);
1168 }
1169 
1170 /************************************************************************
1171  * Get status for a logical drive.
1172  *
1173  * XXX should we also do this in response to Test Unit Ready?
1174  */
1175 static int
1176 ciss_get_ldrive_status(struct ciss_softc *sc,  struct ciss_ldrive *ld)
1177 {
1178     struct ciss_request		*cr;
1179     struct ciss_command		*cc;
1180     struct ciss_bmic_cdb	*cbc;
1181     int				error, command_status;
1182 
1183     /*
1184      * Build a CISS BMIC command to get the logical drive status.
1185      */
1186     if ((error = ciss_get_bmic_request(sc, &cr, CISS_BMIC_ID_LSTATUS,
1187 				       (void **)&ld->cl_lstatus,
1188 				       sizeof(*ld->cl_lstatus))) != 0)
1189 	goto out;
1190     cc = CISS_FIND_COMMAND(cr);
1191     cbc = (struct ciss_bmic_cdb *)&(cc->cdb.cdb[0]);
1192     cbc->log_drive = ld->cl_address.logical.lun;
1193 
1194     /*
1195      * Submit the request and wait for it to complete.
1196      */
1197     if ((error = ciss_synch_request(cr, 60 * 1000)) != 0) {
1198 	ciss_printf(sc, "error sending BMIC LSTATUS command (%d)\n", error);
1199 	goto out;
1200     }
1201 
1202     /*
1203      * Check response.
1204      */
1205     ciss_report_request(cr, &command_status, NULL);
1206     switch(command_status) {
1207     case CISS_CMD_STATUS_SUCCESS:		/* buffer right size */
1208 	break;
1209     case CISS_CMD_STATUS_DATA_UNDERRUN:
1210     case CISS_CMD_STATUS_DATA_OVERRUN:
1211 	ciss_printf(sc, "data over/underrun reading logical drive status\n");
1212     default:
1213 	ciss_printf(sc, "error reading logical drive status (%s)\n",
1214 		    ciss_name_command_status(command_status));
1215 	error = EIO;
1216 	goto out;
1217     }
1218 
1219     /*
1220      * Set the drive's summary status based on the returned status.
1221      *
1222      * XXX testing shows that a failed JBOD drive comes back at next
1223      * boot in "queued for expansion" mode.  WTF?
1224      */
1225     ld->cl_status = ciss_decode_ldrive_status(ld->cl_lstatus->status);
1226 
1227 out:
1228     if (cr != NULL)
1229 	ciss_release_request(cr);
1230     return(error);
1231 }
1232 
1233 /************************************************************************
1234  * Notify the adapter of a config update.
1235  */
1236 static int
1237 ciss_update_config(struct ciss_softc *sc)
1238 {
1239     int		i;
1240 
1241     debug_called(1);
1242 
1243     CISS_TL_SIMPLE_WRITE(sc, CISS_TL_SIMPLE_IDBR, CISS_TL_SIMPLE_IDBR_CFG_TABLE);
1244     for (i = 0; i < 1000; i++) {
1245 	if (!(CISS_TL_SIMPLE_READ(sc, CISS_TL_SIMPLE_IDBR) &
1246 	      CISS_TL_SIMPLE_IDBR_CFG_TABLE)) {
1247 	    return(0);
1248 	}
1249 	DELAY(1000);
1250     }
1251     return(1);
1252 }
1253 
1254 /************************************************************************
1255  * Accept new media into a logical drive.
1256  *
1257  * XXX The drive has previously been offline; it would be good if we
1258  *     could make sure it's not open right now.
1259  */
1260 static int
1261 ciss_accept_media(struct ciss_softc *sc, int ldrive, int async)
1262 {
1263     struct ciss_request		*cr;
1264     struct ciss_command		*cc;
1265     struct ciss_bmic_cdb	*cbc;
1266     int				error;
1267 
1268     debug(0, "bringing logical drive %d back online %ssynchronously",
1269 	  ldrive, async ? "a" : "");
1270 
1271     /*
1272      * Build a CISS BMIC command to bring the drive back online.
1273      */
1274     if ((error = ciss_get_bmic_request(sc, &cr, CISS_BMIC_ACCEPT_MEDIA,
1275 				       NULL, 0)) != 0)
1276 	goto out;
1277     cc = CISS_FIND_COMMAND(cr);
1278     cbc = (struct ciss_bmic_cdb *)&(cc->cdb.cdb[0]);
1279     cbc->log_drive = ldrive;
1280 
1281     /*
1282      * Dispatch the request asynchronously if we can't sleep waiting
1283      * for it to complete.
1284      */
1285     if (async) {
1286 	cr->cr_complete = ciss_accept_media_complete;
1287 	if ((error = ciss_start(cr)) != 0)
1288 	    goto out;
1289 	return(0);
1290     } else {
1291 	/*
1292 	 * Submit the request and wait for it to complete.
1293 	 */
1294 	if ((error = ciss_synch_request(cr, 60 * 1000)) != 0) {
1295 	    ciss_printf(sc, "error sending BMIC LSTATUS command (%d)\n", error);
1296 	    goto out;
1297 	}
1298     }
1299 
1300     /*
1301      * Call the completion callback manually.
1302      */
1303     ciss_accept_media_complete(cr);
1304     return(0);
1305 
1306 out:
1307     if (cr != NULL)
1308 	ciss_release_request(cr);
1309     return(error);
1310 }
1311 
1312 static void
1313 ciss_accept_media_complete(struct ciss_request *cr)
1314 {
1315     int				command_status;
1316 
1317     /*
1318      * Check response.
1319      */
1320     ciss_report_request(cr, &command_status, NULL);
1321     switch(command_status) {
1322     case CISS_CMD_STATUS_SUCCESS:		/* all OK */
1323 	/* we should get a logical drive status changed event here */
1324 	break;
1325     default:
1326 	ciss_printf(cr->cr_sc, "error accepting media into failed logical drive (%s)\n",
1327 		    ciss_name_command_status(command_status));
1328 	break;
1329     }
1330     ciss_release_request(cr);
1331 }
1332 
1333 /************************************************************************
1334  * Release adapter resources.
1335  */
1336 static void
1337 ciss_free(struct ciss_softc *sc)
1338 {
1339     debug_called(1);
1340 
1341     /* we're going away */
1342     sc->ciss_flags |= CISS_FLAG_ABORTING;
1343 
1344     /* terminate the periodic heartbeat routine */
1345     untimeout(ciss_periodic, sc, sc->ciss_periodic);
1346 
1347     /* cancel the Event Notify chain */
1348     ciss_notify_abort(sc);
1349 
1350     /* free the controller data */
1351     if (sc->ciss_id != NULL)
1352 	free(sc->ciss_id, CISS_MALLOC_CLASS);
1353 
1354     /* release I/O resources */
1355     if (sc->ciss_regs_resource != NULL)
1356 	bus_release_resource(sc->ciss_dev, SYS_RES_MEMORY,
1357 			     sc->ciss_regs_rid, sc->ciss_regs_resource);
1358     if (sc->ciss_cfg_resource != NULL)
1359 	bus_release_resource(sc->ciss_dev, SYS_RES_MEMORY,
1360 			     sc->ciss_cfg_rid, sc->ciss_cfg_resource);
1361     if (sc->ciss_intr != NULL)
1362 	bus_teardown_intr(sc->ciss_dev, sc->ciss_irq_resource, sc->ciss_intr);
1363     if (sc->ciss_irq_resource != NULL)
1364 	bus_release_resource(sc->ciss_dev, SYS_RES_IRQ,
1365 			     sc->ciss_irq_rid, sc->ciss_irq_resource);
1366 
1367     /* destroy DMA tags */
1368     if (sc->ciss_parent_dmat)
1369 	bus_dma_tag_destroy(sc->ciss_parent_dmat);
1370     if (sc->ciss_buffer_dmat)
1371 	bus_dma_tag_destroy(sc->ciss_buffer_dmat);
1372 
1373     /* destroy command memory and DMA tag */
1374     if (sc->ciss_command != NULL) {
1375 	bus_dmamap_unload(sc->ciss_command_dmat, sc->ciss_command_map);
1376 	bus_dmamem_free(sc->ciss_command_dmat, sc->ciss_command, sc->ciss_command_map);
1377     }
1378     if (sc->ciss_buffer_dmat)
1379 	bus_dma_tag_destroy(sc->ciss_command_dmat);
1380 
1381     /* disconnect from CAM */
1382     if (sc->ciss_cam_sim) {
1383 	xpt_bus_deregister(cam_sim_path(sc->ciss_cam_sim));
1384 	cam_sim_free(sc->ciss_cam_sim, 0);
1385     }
1386     if (sc->ciss_cam_devq)
1387 	cam_simq_free(sc->ciss_cam_devq);
1388     /* XXX what about ciss_cam_path? */
1389 }
1390 
1391 /************************************************************************
1392  * Give a command to the adapter.
1393  *
1394  * Note that this uses the simple transport layer directly.  If we
1395  * want to add support for other layers, we'll need a switch of some
1396  * sort.
1397  *
1398  * Note that the simple transport layer has no way of refusing a
1399  * command; we only have as many request structures as the adapter
1400  * supports commands, so we don't have to check (this presumes that
1401  * the adapter can handle commands as fast as we throw them at it).
1402  */
1403 static int
1404 ciss_start(struct ciss_request *cr)
1405 {
1406     struct ciss_command	*cc;	/* XXX debugging only */
1407     int			error;
1408 
1409     cc = CISS_FIND_COMMAND(cr);
1410     debug(2, "post command %d tag %d ", cr->cr_tag, cc->header.host_tag);
1411 
1412     /*
1413      * Map the request's data.
1414      */
1415     if ((error = ciss_map_request(cr)))
1416 	return(error);
1417 
1418 #if 0
1419     ciss_print_request(cr);
1420 #endif
1421 
1422     /*
1423      * Post the command to the adapter.
1424      */
1425     ciss_enqueue_busy(cr);
1426     CISS_TL_SIMPLE_POST_CMD(cr->cr_sc, CISS_FIND_COMMANDPHYS(cr));
1427 
1428     return(0);
1429 }
1430 
1431 /************************************************************************
1432  * Fetch completed request(s) from the adapter, queue them for
1433  * completion handling.
1434  *
1435  * Note that this uses the simple transport layer directly.  If we
1436  * want to add support for other layers, we'll need a switch of some
1437  * sort.
1438  *
1439  * Note that the simple transport mechanism does not require any
1440  * reentrancy protection; the OPQ read is atomic.  If there is a
1441  * chance of a race with something else that might move the request
1442  * off the busy list, then we will have to lock against that
1443  * (eg. timeouts, etc.)
1444  */
1445 static void
1446 ciss_done(struct ciss_softc *sc)
1447 {
1448     struct ciss_request	*cr;
1449     struct ciss_command	*cc;
1450     u_int32_t		tag, index;
1451     int			complete;
1452 
1453     debug_called(3);
1454 
1455     /*
1456      * Loop quickly taking requests from the adapter and moving them
1457      * from the busy queue to the completed queue.
1458      */
1459     complete = 0;
1460     for (;;) {
1461 
1462 	/* see if the OPQ contains anything */
1463 	if (!CISS_TL_SIMPLE_OPQ_INTERRUPT(sc))
1464 	    break;
1465 
1466 	tag = CISS_TL_SIMPLE_FETCH_CMD(sc);
1467 	if (tag == CISS_TL_SIMPLE_OPQ_EMPTY)
1468 	    break;
1469 	index = tag >> 2;
1470 	debug(2, "completed command %d%s", index,
1471 	      (tag & CISS_HDR_HOST_TAG_ERROR) ? " with error" : "");
1472 	if (index >= sc->ciss_max_requests) {
1473 	    ciss_printf(sc, "completed invalid request %d (0x%x)\n", index, tag);
1474 	    continue;
1475 	}
1476 	cr = &(sc->ciss_request[index]);
1477 	cc = CISS_FIND_COMMAND(cr);
1478 	cc->header.host_tag = tag;	/* not updated by adapter */
1479 	if (ciss_remove_busy(cr)) {
1480 	    /* assume this is garbage out of the adapter */
1481 	    ciss_printf(sc, "completed nonbusy request %d\n", index);
1482 	} else {
1483 	    ciss_enqueue_complete(cr);
1484 	}
1485 	complete = 1;
1486     }
1487 
1488     /*
1489      * Invoke completion processing.  If we can defer this out of
1490      * interrupt context, that'd be good.
1491      */
1492     if (complete)
1493 	ciss_complete(sc);
1494 }
1495 
1496 /************************************************************************
1497  * Take an interrupt from the adapter.
1498  */
1499 static void
1500 ciss_intr(void *arg)
1501 {
1502     struct ciss_softc	*sc = (struct ciss_softc *)arg;
1503 
1504     /*
1505      * The only interrupt we recognise indicates that there are
1506      * entries in the outbound post queue.
1507      */
1508     ciss_done(sc);
1509 }
1510 
1511 /************************************************************************
1512  * Process completed requests.
1513  *
1514  * Requests can be completed in three fashions:
1515  *
1516  * - by invoking a callback function (cr_complete is non-null)
1517  * - by waking up a sleeper (cr_flags has CISS_REQ_SLEEP set)
1518  * - by clearing the CISS_REQ_POLL flag in interrupt/timeout context
1519  */
1520 static void
1521 ciss_complete(struct ciss_softc *sc)
1522 {
1523     struct ciss_request	*cr;
1524 
1525     debug_called(2);
1526 
1527     /*
1528      * Loop taking requests off the completed queue and performing
1529      * completion processing on them.
1530      */
1531     for (;;) {
1532 	if ((cr = ciss_dequeue_complete(sc)) == NULL)
1533 	    break;
1534 	ciss_unmap_request(cr);
1535 
1536 	/*
1537 	 * If the request has a callback, invoke it.
1538 	 */
1539 	if (cr->cr_complete != NULL) {
1540 	    cr->cr_complete(cr);
1541 	    continue;
1542 	}
1543 
1544 	/*
1545 	 * If someone is sleeping on this request, wake them up.
1546 	 */
1547 	if (cr->cr_flags & CISS_REQ_SLEEP) {
1548 	    cr->cr_flags &= ~CISS_REQ_SLEEP;
1549 	    wakeup(cr);
1550 	    continue;
1551 	}
1552 
1553 	/*
1554 	 * If someone is polling this request for completion, signal.
1555 	 */
1556 	if (cr->cr_flags & CISS_REQ_POLL) {
1557 	    cr->cr_flags &= ~CISS_REQ_POLL;
1558 	    continue;
1559 	}
1560 
1561 	/*
1562 	 * Give up and throw the request back on the free queue.  This
1563 	 * should never happen; resources will probably be lost.
1564 	 */
1565 	ciss_printf(sc, "WARNING: completed command with no submitter\n");
1566 	ciss_enqueue_free(cr);
1567     }
1568 }
1569 
1570 /************************************************************************
1571  * Report on the completion status of a request, and pass back SCSI
1572  * and command status values.
1573  */
1574 static int
1575 ciss_report_request(struct ciss_request *cr, int *command_status, int *scsi_status)
1576 {
1577     struct ciss_command		*cc;
1578     struct ciss_error_info	*ce;
1579 
1580     debug_called(2);
1581 
1582     cc = CISS_FIND_COMMAND(cr);
1583     ce = (struct ciss_error_info *)&(cc->sg[0]);
1584 
1585     /*
1586      * We don't consider data under/overrun an error for the Report
1587      * Logical/Physical LUNs commands.
1588      */
1589     if ((cc->header.host_tag & CISS_HDR_HOST_TAG_ERROR) &&
1590 	((cc->cdb.cdb[0] == CISS_OPCODE_REPORT_LOGICAL_LUNS) ||
1591 	 (cc->cdb.cdb[0] == CISS_OPCODE_REPORT_PHYSICAL_LUNS))) {
1592 	cc->header.host_tag &= ~CISS_HDR_HOST_TAG_ERROR;
1593 	debug(2, "ignoring irrelevant under/overrun error");
1594     }
1595 
1596     /*
1597      * Check the command's error bit, if clear, there's no status and
1598      * everything is OK.
1599      */
1600     if (!(cc->header.host_tag & CISS_HDR_HOST_TAG_ERROR)) {
1601 	if (scsi_status != NULL)
1602 	    *scsi_status = SCSI_STATUS_OK;
1603 	if (command_status != NULL)
1604 	    *command_status = CISS_CMD_STATUS_SUCCESS;
1605 	return(0);
1606     } else {
1607 	if (command_status != NULL)
1608 	    *command_status = ce->command_status;
1609 	if (scsi_status != NULL) {
1610 	    if (ce->command_status == CISS_CMD_STATUS_TARGET_STATUS) {
1611 		*scsi_status = ce->scsi_status;
1612 	    } else {
1613 		*scsi_status = -1;
1614 	    }
1615 	}
1616 	if (bootverbose)
1617 	    ciss_printf(cr->cr_sc, "command status 0x%x (%s) scsi status 0x%x\n",
1618 			ce->command_status, ciss_name_command_status(ce->command_status),
1619 			ce->scsi_status);
1620 	if (ce->command_status == CISS_CMD_STATUS_INVALID_COMMAND) {
1621 	    ciss_printf(cr->cr_sc, "invalid command, offense size %d at %d, value 0x%x\n",
1622 			ce->additional_error_info.invalid_command.offense_size,
1623 			ce->additional_error_info.invalid_command.offense_offset,
1624 			ce->additional_error_info.invalid_command.offense_value);
1625 	}
1626     }
1627     return(1);
1628 }
1629 
1630 /************************************************************************
1631  * Issue a request and don't return until it's completed.
1632  *
1633  * Depending on adapter status, we may poll or sleep waiting for
1634  * completion.
1635  */
1636 static int
1637 ciss_synch_request(struct ciss_request *cr, int timeout)
1638 {
1639     if (cr->cr_sc->ciss_flags & CISS_FLAG_RUNNING) {
1640 	return(ciss_wait_request(cr, timeout));
1641     } else {
1642 	return(ciss_poll_request(cr, timeout));
1643     }
1644 }
1645 
1646 /************************************************************************
1647  * Issue a request and poll for completion.
1648  *
1649  * Timeout in milliseconds.
1650  */
1651 static int
1652 ciss_poll_request(struct ciss_request *cr, int timeout)
1653 {
1654     int		error;
1655 
1656     debug_called(2);
1657 
1658     cr->cr_flags |= CISS_REQ_POLL;
1659     if ((error = ciss_start(cr)) != 0)
1660 	return(error);
1661 
1662     do {
1663 	ciss_done(cr->cr_sc);
1664 	if (!(cr->cr_flags & CISS_REQ_POLL))
1665 	    return(0);
1666 	DELAY(1000);
1667     } while (timeout-- >= 0);
1668     return(EWOULDBLOCK);
1669 }
1670 
1671 /************************************************************************
1672  * Issue a request and sleep waiting for completion.
1673  *
1674  * Timeout in milliseconds.  Note that a spurious wakeup will reset
1675  * the timeout.
1676  */
1677 static int
1678 ciss_wait_request(struct ciss_request *cr, int timeout)
1679 {
1680     int		s, error;
1681 
1682     debug_called(2);
1683 
1684     cr->cr_flags |= CISS_REQ_SLEEP;
1685     if ((error = ciss_start(cr)) != 0)
1686 	return(error);
1687 
1688     s = splcam();
1689     while (cr->cr_flags & CISS_REQ_SLEEP) {
1690 	error = tsleep(cr, PCATCH, "cissREQ", (timeout * hz) / 1000);
1691 	/*
1692 	 * On wakeup or interruption due to restartable activity, go
1693 	 * back and check to see if we're done.
1694 	 */
1695 	if ((error == 0) || (error == ERESTART)) {
1696 	    error = 0;
1697 	    continue;
1698 	}
1699 	/*
1700 	 * Timeout, interrupted system call, etc.
1701 	 */
1702 	break;
1703     }
1704     splx(s);
1705     return(error);
1706 }
1707 
1708 #if 0
1709 /************************************************************************
1710  * Abort a request.  Note that a potential exists here to race the
1711  * request being completed; the caller must deal with this.
1712  */
1713 static int
1714 ciss_abort_request(struct ciss_request *ar)
1715 {
1716     struct ciss_request		*cr;
1717     struct ciss_command		*cc;
1718     struct ciss_message_cdb	*cmc;
1719     int				error;
1720 
1721     debug_called(1);
1722 
1723     /* get a request */
1724     if ((error = ciss_get_request(ar->cr_sc, &cr)) != 0)
1725 	return(error);
1726 
1727     /* build the abort command */
1728     cc = CISS_FIND_COMMAND(cr);
1729     cc->header.address.mode.mode = CISS_HDR_ADDRESS_MODE_PERIPHERAL;	/* addressing? */
1730     cc->header.address.physical.target = 0;
1731     cc->header.address.physical.bus = 0;
1732     cc->cdb.cdb_length = sizeof(*cmc);
1733     cc->cdb.type = CISS_CDB_TYPE_MESSAGE;
1734     cc->cdb.attribute = CISS_CDB_ATTRIBUTE_SIMPLE;
1735     cc->cdb.direction = CISS_CDB_DIRECTION_NONE;
1736     cc->cdb.timeout = 30;
1737 
1738     cmc = (struct ciss_message_cdb *)&(cc->cdb.cdb[0]);
1739     cmc->opcode = CISS_OPCODE_MESSAGE_ABORT;
1740     cmc->type = CISS_MESSAGE_ABORT_TASK;
1741     cmc->abort_tag = ar->cr_tag;	/* endianness?? */
1742 
1743     /*
1744      * Send the request and wait for a response.  If we believe we
1745      * aborted the request OK, clear the flag that indicates it's
1746      * running.
1747      */
1748     error = ciss_synch_request(cr, 35 * 1000);
1749     if (!error)
1750 	error = ciss_report_request(cr, NULL, NULL);
1751     ciss_release_request(cr);
1752 
1753     return(error);
1754 }
1755 #endif
1756 
1757 
1758 /************************************************************************
1759  * Fetch and initialise a request
1760  */
1761 static int
1762 ciss_get_request(struct ciss_softc *sc, struct ciss_request **crp)
1763 {
1764     struct ciss_request *cr;
1765 
1766     debug_called(2);
1767 
1768     /*
1769      * Get a request and clean it up.
1770      */
1771     if ((cr = ciss_dequeue_free(sc)) == NULL)
1772 	return(ENOMEM);
1773 
1774     cr->cr_data = NULL;
1775     cr->cr_flags = 0;
1776     cr->cr_complete = NULL;
1777 
1778     ciss_preen_command(cr);
1779     *crp = cr;
1780     return(0);
1781 }
1782 
1783 static void
1784 ciss_preen_command(struct ciss_request *cr)
1785 {
1786     struct ciss_command	*cc;
1787     u_int32_t		cmdphys;
1788 
1789     /*
1790      * Clean up the command structure.
1791      *
1792      * Note that we set up the error_info structure here, since the
1793      * length can be overwritten by any command.
1794      */
1795     cc = CISS_FIND_COMMAND(cr);
1796     cc->header.sg_in_list = 0;		/* kinda inefficient this way */
1797     cc->header.sg_total = 0;
1798     cc->header.host_tag = cr->cr_tag << 2;
1799     cc->header.host_tag_zeroes = 0;
1800     cmdphys = CISS_FIND_COMMANDPHYS(cr);
1801     cc->error_info.error_info_address = cmdphys + sizeof(struct ciss_command);
1802     cc->error_info.error_info_length = CISS_COMMAND_ALLOC_SIZE - sizeof(struct ciss_command);
1803 
1804 }
1805 
1806 /************************************************************************
1807  * Release a request to the free list.
1808  */
1809 static void
1810 ciss_release_request(struct ciss_request *cr)
1811 {
1812     struct ciss_softc	*sc;
1813 
1814     debug_called(2);
1815 
1816     sc = cr->cr_sc;
1817 
1818     /* release the request to the free queue */
1819     ciss_requeue_free(cr);
1820 }
1821 
1822 /************************************************************************
1823  * Allocate a request that will be used to send a BMIC command.  Do some
1824  * of the common setup here to avoid duplicating it everywhere else.
1825  */
1826 static int
1827 ciss_get_bmic_request(struct ciss_softc *sc, struct ciss_request **crp,
1828 		      int opcode, void **bufp, size_t bufsize)
1829 {
1830     struct ciss_request		*cr;
1831     struct ciss_command		*cc;
1832     struct ciss_bmic_cdb	*cbc;
1833     void			*buf;
1834     int				error;
1835     int				dataout;
1836 
1837     debug_called(2);
1838 
1839     cr = NULL;
1840     buf = NULL;
1841 
1842     /*
1843      * Get a request.
1844      */
1845     if ((error = ciss_get_request(sc, &cr)) != 0)
1846 	goto out;
1847 
1848     /*
1849      * Allocate data storage if requested, determine the data direction.
1850      */
1851     dataout = 0;
1852     if ((bufsize > 0) && (bufp != NULL)) {
1853 	if (*bufp == NULL) {
1854 	    if ((buf = malloc(bufsize, CISS_MALLOC_CLASS, M_NOWAIT | M_ZERO)) == NULL) {
1855 		error = ENOMEM;
1856 		goto out;
1857 	    }
1858 	} else {
1859 	    buf = *bufp;
1860 	    dataout = 1;	/* we are given a buffer, so we are writing */
1861 	}
1862     }
1863 
1864     /*
1865      * Build a CISS BMIC command to get the logical drive ID.
1866      */
1867     cr->cr_data = buf;
1868     cr->cr_length = bufsize;
1869     if (!dataout)
1870 	cr->cr_flags = CISS_REQ_DATAIN;
1871 
1872     cc = CISS_FIND_COMMAND(cr);
1873     cc->header.address.physical.mode = CISS_HDR_ADDRESS_MODE_PERIPHERAL;
1874     cc->header.address.physical.bus = 0;
1875     cc->header.address.physical.target = 0;
1876     cc->cdb.cdb_length = sizeof(*cbc);
1877     cc->cdb.type = CISS_CDB_TYPE_COMMAND;
1878     cc->cdb.attribute = CISS_CDB_ATTRIBUTE_SIMPLE;
1879     cc->cdb.direction = dataout ? CISS_CDB_DIRECTION_WRITE : CISS_CDB_DIRECTION_READ;
1880     cc->cdb.timeout = 0;
1881 
1882     cbc = (struct ciss_bmic_cdb *)&(cc->cdb.cdb[0]);
1883     bzero(cbc, sizeof(*cbc));
1884     cbc->opcode = dataout ? CISS_ARRAY_CONTROLLER_WRITE : CISS_ARRAY_CONTROLLER_READ;
1885     cbc->bmic_opcode = opcode;
1886     cbc->size = htons((u_int16_t)bufsize);
1887 
1888 out:
1889     if (error) {
1890 	if (cr != NULL)
1891 	    ciss_release_request(cr);
1892 	if ((bufp != NULL) && (*bufp == NULL) && (buf != NULL))
1893 	    free(buf, CISS_MALLOC_CLASS);
1894     } else {
1895 	*crp = cr;
1896 	if ((bufp != NULL) && (*bufp == NULL) && (buf != NULL))
1897 	    *bufp = buf;
1898     }
1899     return(error);
1900 }
1901 
1902 /************************************************************************
1903  * Handle a command passed in from userspace.
1904  */
1905 static int
1906 ciss_user_command(struct ciss_softc *sc, IOCTL_Command_struct *ioc)
1907 {
1908     struct ciss_request		*cr;
1909     struct ciss_command		*cc;
1910     struct ciss_error_info	*ce;
1911     int				error;
1912 
1913     debug_called(1);
1914 
1915     cr = NULL;
1916 
1917     /*
1918      * Get a request.
1919      */
1920     if ((error = ciss_get_request(sc, &cr)) != 0)
1921 	goto out;
1922     cc = CISS_FIND_COMMAND(cr);
1923 
1924     /*
1925      * Allocate an in-kernel databuffer if required, copy in user data.
1926      */
1927     cr->cr_length = ioc->buf_size;
1928     if (ioc->buf_size > 0) {
1929 	if ((cr->cr_data = malloc(ioc->buf_size, CISS_MALLOC_CLASS, 0)) == NULL) {
1930 	    error = ENOMEM;
1931 	    goto out;
1932 	}
1933 	if ((error = copyin(ioc->buf, cr->cr_data, ioc->buf_size))) {
1934 	    debug(0, "copyin: bad data buffer %p/%d", ioc->buf, ioc->buf_size);
1935 	    goto out;
1936 	}
1937     }
1938 
1939     /*
1940      * Build the request based on the user command.
1941      */
1942     bcopy(&ioc->LUN_info, &cc->header.address, sizeof(cc->header.address));
1943     bcopy(&ioc->Request, &cc->cdb, sizeof(cc->cdb));
1944 
1945     /* XXX anything else to populate here? */
1946 
1947     /*
1948      * Run the command.
1949      */
1950     if ((error = ciss_synch_request(cr, 60 * 1000))) {
1951 	debug(0, "request failed - %d", error);
1952 	goto out;
1953     }
1954 
1955     /*
1956      * Copy the results back to the user.
1957      */
1958     ce = (struct ciss_error_info *)&(cc->sg[0]);
1959     bcopy(ce, &ioc->error_info, sizeof(*ce));
1960     if ((ioc->buf_size > 0) &&
1961 	(error = copyout(cr->cr_data, ioc->buf, ioc->buf_size))) {
1962 	debug(0, "copyout: bad data buffer %p/%d", ioc->buf, ioc->buf_size);
1963 	goto out;
1964     }
1965 
1966     /* done OK */
1967     error = 0;
1968 
1969 out:
1970     if ((cr != NULL) && (cr->cr_data != NULL))
1971 	free(cr->cr_data, CISS_MALLOC_CLASS);
1972     if (cr != NULL)
1973 	ciss_release_request(cr);
1974     return(error);
1975 }
1976 
1977 /************************************************************************
1978  * Map a request into bus-visible space, initialise the scatter/gather
1979  * list.
1980  */
1981 static int
1982 ciss_map_request(struct ciss_request *cr)
1983 {
1984     struct ciss_softc	*sc;
1985 
1986     debug_called(2);
1987 
1988     sc = cr->cr_sc;
1989 
1990     /* check that mapping is necessary */
1991     if ((cr->cr_flags & CISS_REQ_MAPPED) || (cr->cr_data == NULL))
1992 	return(0);
1993 
1994     bus_dmamap_load(sc->ciss_buffer_dmat, cr->cr_datamap, cr->cr_data, cr->cr_length,
1995 		    ciss_request_map_helper, CISS_FIND_COMMAND(cr), 0);
1996 
1997     if (cr->cr_flags & CISS_REQ_DATAIN)
1998 	bus_dmamap_sync(sc->ciss_buffer_dmat, cr->cr_datamap, BUS_DMASYNC_PREREAD);
1999     if (cr->cr_flags & CISS_REQ_DATAOUT)
2000 	bus_dmamap_sync(sc->ciss_buffer_dmat, cr->cr_datamap, BUS_DMASYNC_PREWRITE);
2001 
2002     cr->cr_flags |= CISS_REQ_MAPPED;
2003     return(0);
2004 }
2005 
2006 static void
2007 ciss_request_map_helper(void *arg, bus_dma_segment_t *segs, int nseg, int error)
2008 {
2009     struct ciss_command	*cc;
2010     int			i;
2011 
2012     debug_called(2);
2013 
2014     cc = (struct ciss_command *)arg;
2015     for (i = 0; i < nseg; i++) {
2016 	cc->sg[i].address = segs[i].ds_addr;
2017 	cc->sg[i].length = segs[i].ds_len;
2018 	cc->sg[i].extension = 0;
2019     }
2020     /* we leave the s/g table entirely within the command */
2021     cc->header.sg_in_list = nseg;
2022     cc->header.sg_total = nseg;
2023 }
2024 
2025 /************************************************************************
2026  * Unmap a request from bus-visible space.
2027  */
2028 static void
2029 ciss_unmap_request(struct ciss_request *cr)
2030 {
2031     struct ciss_softc	*sc;
2032 
2033     debug_called(2);
2034 
2035     sc = cr->cr_sc;
2036 
2037     /* check that unmapping is necessary */
2038     if (!(cr->cr_flags & CISS_REQ_MAPPED) || (cr->cr_data == NULL))
2039 	return;
2040 
2041     if (cr->cr_flags & CISS_REQ_DATAIN)
2042 	bus_dmamap_sync(sc->ciss_buffer_dmat, cr->cr_datamap, BUS_DMASYNC_POSTREAD);
2043     if (cr->cr_flags & CISS_REQ_DATAOUT)
2044 	bus_dmamap_sync(sc->ciss_buffer_dmat, cr->cr_datamap, BUS_DMASYNC_POSTWRITE);
2045 
2046     bus_dmamap_unload(sc->ciss_buffer_dmat, cr->cr_datamap);
2047     cr->cr_flags &= ~CISS_REQ_MAPPED;
2048 }
2049 
2050 /************************************************************************
2051  * Attach the driver to CAM.
2052  *
2053  * We put all the logical drives on a single SCSI bus.
2054  */
2055 static int
2056 ciss_cam_init(struct ciss_softc *sc)
2057 {
2058 
2059     debug_called(1);
2060 
2061     /*
2062      * Allocate a devq.  We can reuse this for the masked physical
2063      * devices if we decide to export these as well.
2064      */
2065     if ((sc->ciss_cam_devq = cam_simq_alloc(sc->ciss_max_requests)) == NULL) {
2066 	ciss_printf(sc, "can't allocate CAM SIM queue\n");
2067 	return(ENOMEM);
2068     }
2069 
2070     /*
2071      * Create a SIM.
2072      */
2073     if ((sc->ciss_cam_sim = cam_sim_alloc(ciss_cam_action, ciss_cam_poll, "ciss", sc,
2074 					  device_get_unit(sc->ciss_dev),
2075 					  sc->ciss_max_requests - 2,
2076 					  1,
2077 					  sc->ciss_cam_devq)) == NULL) {
2078 	ciss_printf(sc, "can't allocate CAM SIM\n");
2079 	return(ENOMEM);
2080     }
2081 
2082     /*
2083      * Register bus 0 (the 'logical drives' bus) with this SIM.
2084      */
2085     if (xpt_bus_register(sc->ciss_cam_sim, 0) != 0) {
2086 	ciss_printf(sc, "can't register SCSI bus 0\n");
2087 	return(ENXIO);
2088     }
2089 
2090     /*
2091      * Initiate a rescan of the bus.
2092      */
2093     ciss_cam_rescan_all(sc);
2094 
2095     return(0);
2096 }
2097 
2098 /************************************************************************
2099  * Initiate a rescan of the 'logical devices' SIM
2100  */
2101 static void
2102 ciss_cam_rescan_target(struct ciss_softc *sc, int target)
2103 {
2104     union ccb	*ccb;
2105 
2106     debug_called(1);
2107 
2108     if ((ccb = malloc(sizeof(union ccb), M_TEMP, M_ZERO)) == NULL) {
2109 	ciss_printf(sc, "rescan failed (can't allocate CCB)\n");
2110 	return;
2111     }
2112 
2113     if (xpt_create_path(&sc->ciss_cam_path, xpt_periph, cam_sim_path(sc->ciss_cam_sim), target, 0)
2114 	!= CAM_REQ_CMP) {
2115 	ciss_printf(sc, "rescan failed (can't create path)\n");
2116 	return;
2117     }
2118 
2119     xpt_setup_ccb(&ccb->ccb_h, sc->ciss_cam_path, 5/*priority (low)*/);
2120     ccb->ccb_h.func_code = XPT_SCAN_BUS;
2121     ccb->ccb_h.cbfcnp = ciss_cam_rescan_callback;
2122     ccb->crcn.flags = CAM_FLAG_NONE;
2123     xpt_action(ccb);
2124 
2125     /* scan is now in progress */
2126 }
2127 
2128 static void
2129 ciss_cam_rescan_all(struct ciss_softc *sc)
2130 {
2131     return(ciss_cam_rescan_target(sc, 0));
2132 }
2133 
2134 static void
2135 ciss_cam_rescan_callback(struct cam_periph *periph, union ccb *ccb)
2136 {
2137     xpt_free_path(ccb->ccb_h.path);
2138     free(ccb, M_TEMP);
2139 }
2140 
2141 /************************************************************************
2142  * Handle requests coming from CAM
2143  */
2144 static void
2145 ciss_cam_action(struct cam_sim *sim, union ccb *ccb)
2146 {
2147     struct ciss_softc	*sc;
2148     struct ccb_scsiio	*csio;
2149     int			target;
2150 
2151     sc = cam_sim_softc(sim);
2152     csio = (struct ccb_scsiio *)&ccb->csio;
2153     target = csio->ccb_h.target_id;
2154 
2155     switch (ccb->ccb_h.func_code) {
2156 
2157 	/* perform SCSI I/O */
2158     case XPT_SCSI_IO:
2159 	if (!ciss_cam_action_io(sim, csio))
2160 	    return;
2161 	break;
2162 
2163 	/* perform geometry calculations */
2164     case XPT_CALC_GEOMETRY:
2165     {
2166 	struct ccb_calc_geometry	*ccg = &ccb->ccg;
2167 	struct ciss_ldrive		*ld = &sc->ciss_logical[target];
2168 
2169 	debug(1, "XPT_CALC_GEOMETRY %d:%d:%d", cam_sim_bus(sim), ccb->ccb_h.target_id, ccb->ccb_h.target_lun);
2170 
2171 	/*
2172 	 * Use the cached geometry settings unless the fault tolerance
2173 	 * is invalid.
2174 	 */
2175 	if (ld->cl_geometry.fault_tolerance == 0xFF) {
2176 	    u_int32_t			secs_per_cylinder;
2177 
2178 	    ccg->heads = 255;
2179 	    ccg->secs_per_track = 32;
2180 	    secs_per_cylinder = ccg->heads * ccg->secs_per_track;
2181 	    ccg->cylinders = ccg->volume_size / secs_per_cylinder;
2182 	} else {
2183 	    ccg->heads = ld->cl_geometry.heads;
2184 	    ccg->secs_per_track = ld->cl_geometry.sectors;
2185 	    ccg->cylinders = ntohs(ld->cl_geometry.cylinders);
2186 	}
2187 	ccb->ccb_h.status = CAM_REQ_CMP;
2188         break;
2189     }
2190 
2191 	/* handle path attribute inquiry */
2192     case XPT_PATH_INQ:
2193     {
2194 	struct ccb_pathinq	*cpi = &ccb->cpi;
2195 
2196 	debug(1, "XPT_PATH_INQ %d:%d:%d", cam_sim_bus(sim), ccb->ccb_h.target_id, ccb->ccb_h.target_lun);
2197 
2198 	cpi->version_num = 1;
2199 	cpi->hba_inquiry = PI_TAG_ABLE;	/* XXX is this correct? */
2200 	cpi->target_sprt = 0;
2201 	cpi->hba_misc = 0;
2202 	cpi->max_target = CISS_MAX_LOGICAL;
2203 	cpi->max_lun = 0;		/* 'logical drive' channel only */
2204 	cpi->initiator_id = CISS_MAX_LOGICAL;
2205 	strncpy(cpi->sim_vid, "FreeBSD", SIM_IDLEN);
2206         strncpy(cpi->hba_vid, "msmith@freebsd.org", HBA_IDLEN);
2207         strncpy(cpi->dev_name, cam_sim_name(sim), DEV_IDLEN);
2208         cpi->unit_number = cam_sim_unit(sim);
2209         cpi->bus_id = cam_sim_bus(sim);
2210 	cpi->base_transfer_speed = 132 * 1024;	/* XXX what to set this to? */
2211 	ccb->ccb_h.status = CAM_REQ_CMP;
2212 	break;
2213     }
2214 
2215     case XPT_GET_TRAN_SETTINGS:
2216     {
2217 	struct ccb_trans_settings	*cts = &ccb->cts;
2218 	int				bus, target;
2219 
2220 	bus = cam_sim_bus(sim);
2221 	target = cts->ccb_h.target_id;
2222 
2223 	debug(1, "XPT_GET_TRAN_SETTINGS %d:%d", bus, target);
2224 	cts->valid = 0;
2225 
2226 	/* disconnect always OK */
2227 	cts->flags |= CCB_TRANS_DISC_ENB;
2228 	cts->valid |= CCB_TRANS_DISC_VALID;
2229 
2230 	cts->ccb_h.status = CAM_REQ_CMP;
2231 	break;
2232     }
2233 
2234     default:		/* we can't do this */
2235 	debug(1, "unspported func_code = 0x%x", ccb->ccb_h.func_code);
2236 	ccb->ccb_h.status = CAM_REQ_INVALID;
2237 	break;
2238     }
2239 
2240     xpt_done(ccb);
2241 }
2242 
2243 /************************************************************************
2244  * Handle a CAM SCSI I/O request.
2245  */
2246 static int
2247 ciss_cam_action_io(struct cam_sim *sim, struct ccb_scsiio *csio)
2248 {
2249     struct ciss_softc	*sc;
2250     int			bus, target;
2251     struct ciss_request	*cr;
2252     struct ciss_command	*cc;
2253     int			error;
2254 
2255     sc = cam_sim_softc(sim);
2256     bus = cam_sim_bus(sim);
2257     target = csio->ccb_h.target_id;
2258 
2259     debug(2, "XPT_SCSI_IO %d:%d:%d", bus, target, csio->ccb_h.target_lun);
2260 
2261     /* check for I/O attempt to nonexistent device */
2262     if ((bus != 0) ||
2263 	(target > CISS_MAX_LOGICAL) ||
2264 	(sc->ciss_logical[target].cl_status == CISS_LD_NONEXISTENT)) {
2265 	debug(3, "  device does not exist");
2266 	csio->ccb_h.status = CAM_REQ_CMP_ERR;
2267     }
2268 
2269     /* firmware does not support commands > 10 bytes */
2270     if (csio->cdb_len > 12/*CISS_CDB_BUFFER_SIZE*/) {
2271 	debug(3, "  command too large (%d > %d)", csio->cdb_len, CISS_CDB_BUFFER_SIZE);
2272 	csio->ccb_h.status = CAM_REQ_CMP_ERR;
2273     }
2274 
2275     /* check that the CDB pointer is not to a physical address */
2276     if ((csio->ccb_h.flags & CAM_CDB_POINTER) && (csio->ccb_h.flags & CAM_CDB_PHYS)) {
2277 	debug(3, "  CDB pointer is to physical address");
2278 	csio->ccb_h.status = CAM_REQ_CMP_ERR;
2279     }
2280 
2281     /* if there is data transfer, it must be to/from a virtual address */
2282     if ((csio->ccb_h.flags & CAM_DIR_MASK) != CAM_DIR_NONE) {
2283 	if (csio->ccb_h.flags & CAM_DATA_PHYS) {		/* we can't map it */
2284 	    debug(3, "  data pointer is to physical address");
2285 	    csio->ccb_h.status = CAM_REQ_CMP_ERR;
2286 	}
2287 	if (csio->ccb_h.flags & CAM_SCATTER_VALID) {	/* we want to do the s/g setup */
2288 	    debug(3, "  data has premature s/g setup");
2289 	    csio->ccb_h.status = CAM_REQ_CMP_ERR;
2290 	}
2291     }
2292 
2293     /* abandon aborted ccbs or those that have failed validation */
2294     if ((csio->ccb_h.status & CAM_STATUS_MASK) != CAM_REQ_INPROG) {
2295 	debug(3, "abandoning CCB due to abort/validation failure");
2296 	return(EINVAL);
2297     }
2298 
2299     /* handle emulation of some SCSI commands ourself */
2300     if (ciss_cam_emulate(sc, csio))
2301 	return(0);
2302 
2303     /*
2304      * Get a request to manage this command.  If we can't, return the
2305      * ccb, freeze the queue and flag so that we unfreeze it when a
2306      * request completes.
2307      */
2308     if ((error = ciss_get_request(sc, &cr)) != 0) {
2309 	xpt_freeze_simq(sc->ciss_cam_sim, 1);
2310 	csio->ccb_h.status |= CAM_REQUEUE_REQ;
2311 	return(error);
2312     }
2313 
2314     /*
2315      * Build the command.
2316      */
2317     cc = CISS_FIND_COMMAND(cr);
2318     cr->cr_data = csio->data_ptr;
2319     cr->cr_length = csio->dxfer_len;
2320     cr->cr_complete = ciss_cam_complete;
2321     cr->cr_private = csio;
2322 
2323     cc->header.address.logical.mode = CISS_HDR_ADDRESS_MODE_LOGICAL;
2324     cc->header.address.logical.lun = target;
2325     cc->cdb.cdb_length = csio->cdb_len;
2326     cc->cdb.type = CISS_CDB_TYPE_COMMAND;
2327     cc->cdb.attribute = CISS_CDB_ATTRIBUTE_SIMPLE;	/* XXX ordered tags? */
2328     if ((csio->ccb_h.flags & CAM_DIR_MASK) == CAM_DIR_OUT) {
2329 	cr->cr_flags = CISS_REQ_DATAOUT;
2330 	cc->cdb.direction = CISS_CDB_DIRECTION_WRITE;
2331     } else if ((csio->ccb_h.flags & CAM_DIR_MASK) == CAM_DIR_IN) {
2332 	cr->cr_flags = CISS_REQ_DATAIN;
2333 	cc->cdb.direction = CISS_CDB_DIRECTION_READ;
2334     } else {
2335 	cr->cr_flags = 0;
2336 	cc->cdb.direction = CISS_CDB_DIRECTION_NONE;
2337     }
2338     cc->cdb.timeout = (csio->ccb_h.timeout / 1000) + 1;
2339     if (csio->ccb_h.flags & CAM_CDB_POINTER) {
2340 	bcopy(csio->cdb_io.cdb_ptr, &cc->cdb.cdb[0], csio->cdb_len);
2341     } else {
2342 	bcopy(csio->cdb_io.cdb_bytes, &cc->cdb.cdb[0], csio->cdb_len);
2343     }
2344 
2345     /*
2346      * Submit the request to the adapter.
2347      *
2348      * Note that this may fail if we're unable to map the request (and
2349      * if we ever learn a transport layer other than simple, may fail
2350      * if the adapter rejects the command).
2351      */
2352     if ((error = ciss_start(cr)) != 0) {
2353 	xpt_freeze_simq(sc->ciss_cam_sim, 1);
2354 	csio->ccb_h.status |= CAM_REQUEUE_REQ;
2355 	ciss_release_request(cr);
2356 	return(error);
2357     }
2358 
2359     return(0);
2360 }
2361 
2362 /************************************************************************
2363  * Emulate SCSI commands the adapter doesn't handle as we might like.
2364  */
2365 static int
2366 ciss_cam_emulate(struct ciss_softc *sc, struct ccb_scsiio *csio)
2367 {
2368     int		target;
2369     u_int8_t	opcode;
2370 
2371 
2372     target = csio->ccb_h.target_id;
2373     opcode = (csio->ccb_h.flags & CAM_CDB_POINTER) ?
2374 	*(u_int8_t *)csio->cdb_io.cdb_ptr : csio->cdb_io.cdb_bytes[0];
2375 
2376     /*
2377      * Handle requests for volumes that don't exist.  A selection timeout
2378      * is slightly better than an illegal request.  Other errors might be
2379      * better.
2380      */
2381     if (sc->ciss_logical[target].cl_status == CISS_LD_NONEXISTENT) {
2382 	csio->ccb_h.status = CAM_SEL_TIMEOUT;
2383 	xpt_done((union ccb *)csio);
2384 	return(1);
2385     }
2386 
2387     /*
2388      * Handle requests for volumes that exist but are offline.
2389      *
2390      * I/O operations should fail, everything else should work.
2391      */
2392     if (sc->ciss_logical[target].cl_status == CISS_LD_OFFLINE) {
2393 	switch(opcode) {
2394 	case READ_6:
2395 	case READ_10:
2396 	case READ_12:
2397 	case WRITE_6:
2398 	case WRITE_10:
2399 	case WRITE_12:
2400 	    csio->ccb_h.status = CAM_SEL_TIMEOUT;
2401 	    xpt_done((union ccb *)csio);
2402 	    return(1);
2403 	}
2404     }
2405 
2406 
2407     /* if we have to fake Synchronise Cache */
2408     if (sc->ciss_flags & CISS_FLAG_FAKE_SYNCH) {
2409 
2410 	/*
2411 	 * If this is a Synchronise Cache command, typically issued when
2412 	 * a device is closed, flush the adapter and complete now.
2413 	 */
2414 	if (((csio->ccb_h.flags & CAM_CDB_POINTER) ?
2415 	     *(u_int8_t *)csio->cdb_io.cdb_ptr : csio->cdb_io.cdb_bytes[0]) == SYNCHRONIZE_CACHE) {
2416 	    ciss_flush_adapter(sc);
2417 	    csio->ccb_h.status = CAM_REQ_CMP;
2418 	    xpt_done((union ccb *)csio);
2419 	    return(1);
2420 	}
2421     }
2422 
2423     return(0);
2424 }
2425 
2426 /************************************************************************
2427  * Check for possibly-completed commands.
2428  */
2429 static void
2430 ciss_cam_poll(struct cam_sim *sim)
2431 {
2432     struct ciss_softc	*sc = cam_sim_softc(sim);
2433 
2434     debug_called(2);
2435 
2436     ciss_done(sc);
2437 }
2438 
2439 /************************************************************************
2440  * Handle completion of a command - pass results back through the CCB
2441  */
2442 static void
2443 ciss_cam_complete(struct ciss_request *cr)
2444 {
2445     struct ciss_softc		*sc;
2446     struct ciss_command		*cc;
2447     struct ciss_error_info	*ce;
2448     struct ccb_scsiio		*csio;
2449     int				scsi_status;
2450     int				command_status;
2451 
2452     debug_called(2);
2453 
2454     sc = cr->cr_sc;
2455     cc = CISS_FIND_COMMAND(cr);
2456     ce = (struct ciss_error_info *)&(cc->sg[0]);
2457     csio = (struct ccb_scsiio *)cr->cr_private;
2458 
2459     /*
2460      * Extract status values from request.
2461      */
2462     ciss_report_request(cr, &command_status, &scsi_status);
2463     csio->scsi_status = scsi_status;
2464 
2465     /*
2466      * Handle specific SCSI status values.
2467      */
2468     switch(scsi_status) {
2469 	/* no status due to adapter error */
2470     case -1:
2471 	debug(0, "adapter error");
2472 	csio->ccb_h.status = CAM_REQ_CMP_ERR;
2473 	break;
2474 
2475 	/* no status due to command completed OK */
2476     case SCSI_STATUS_OK:		/* CISS_SCSI_STATUS_GOOD */
2477 	debug(2, "SCSI_STATUS_OK");
2478 	csio->ccb_h.status = CAM_REQ_CMP;
2479 	break;
2480 
2481 	/* check condition, sense data included */
2482     case SCSI_STATUS_CHECK_COND:	/* CISS_SCSI_STATUS_CHECK_CONDITION */
2483 	debug(0, "SCSI_STATUS_CHECK_COND  sense size %d  resid %d",
2484 	      ce->sense_length, ce->residual_count);
2485 	bzero(&csio->sense_data, SSD_FULL_SIZE);
2486 	bcopy(&ce->sense_info[0], &csio->sense_data, ce->sense_length);
2487 	csio->sense_len = ce->sense_length;
2488 	csio->resid = ce->residual_count;
2489 	csio->ccb_h.status = CAM_SCSI_STATUS_ERROR | CAM_AUTOSNS_VALID;
2490 #ifdef CISS_DEBUG
2491 	{
2492 	    struct scsi_sense_data	*sns = (struct scsi_sense_data *)&ce->sense_info[0];
2493 	    debug(0, "sense key %x", sns->flags & SSD_KEY);
2494 	}
2495 #endif
2496 	break;
2497 
2498     case SCSI_STATUS_BUSY:		/* CISS_SCSI_STATUS_BUSY */
2499 	debug(0, "SCSI_STATUS_BUSY");
2500 	csio->ccb_h.status = CAM_SCSI_BUSY;
2501 	break;
2502 
2503     default:
2504 	debug(0, "unknown status 0x%x", csio->scsi_status);
2505 	csio->ccb_h.status = CAM_REQ_CMP_ERR;
2506 	break;
2507     }
2508 
2509     /* handle post-command fixup */
2510     ciss_cam_complete_fixup(sc, csio);
2511 
2512     /* tell CAM we're ready for more commands */
2513     csio->ccb_h.status |= CAM_RELEASE_SIMQ;
2514 
2515     xpt_done((union ccb *)csio);
2516     ciss_release_request(cr);
2517 }
2518 
2519 /********************************************************************************
2520  * Fix up the result of some commands here.
2521  */
2522 static void
2523 ciss_cam_complete_fixup(struct ciss_softc *sc, struct ccb_scsiio *csio)
2524 {
2525     struct scsi_inquiry_data	*inq;
2526     struct ciss_ldrive		*cl;
2527     int				target;
2528 
2529     if (((csio->ccb_h.flags & CAM_CDB_POINTER) ?
2530 	 *(u_int8_t *)csio->cdb_io.cdb_ptr : csio->cdb_io.cdb_bytes[0]) == INQUIRY) {
2531 
2532 	inq = (struct scsi_inquiry_data *)csio->data_ptr;
2533 	target = csio->ccb_h.target_id;
2534 	cl = &sc->ciss_logical[target];
2535 
2536 	padstr(inq->vendor, "COMPAQ", 8);
2537 	padstr(inq->product, ciss_name_ldrive_org(cl->cl_ldrive->fault_tolerance), 8);
2538 	padstr(inq->revision, ciss_name_ldrive_status(cl->cl_lstatus->status), 16);
2539     }
2540 }
2541 
2542 
2543 /********************************************************************************
2544  * Find a peripheral attached at (target)
2545  */
2546 static struct cam_periph *
2547 ciss_find_periph(struct ciss_softc *sc, int target)
2548 {
2549     struct cam_periph	*periph;
2550     struct cam_path	*path;
2551     int			status;
2552 
2553     status = xpt_create_path(&path, NULL, cam_sim_path(sc->ciss_cam_sim), target, 0);
2554     if (status == CAM_REQ_CMP) {
2555 	periph = cam_periph_find(path, NULL);
2556 	xpt_free_path(path);
2557     } else {
2558 	periph = NULL;
2559     }
2560     return(periph);
2561 }
2562 
2563 /********************************************************************************
2564  * Name the device at (target)
2565  *
2566  * XXX is this strictly correct?
2567  */
2568 static int
2569 ciss_name_device(struct ciss_softc *sc, int target)
2570 {
2571     struct cam_periph	*periph;
2572 
2573     if ((periph = ciss_find_periph(sc, target)) != NULL) {
2574 	sprintf(sc->ciss_logical[target].cl_name, "%s%d", periph->periph_name, periph->unit_number);
2575 	return(0);
2576     }
2577     sc->ciss_logical[target].cl_name[0] = 0;
2578     return(ENOENT);
2579 }
2580 
2581 /************************************************************************
2582  * Periodic status monitoring.
2583  */
2584 static void
2585 ciss_periodic(void *arg)
2586 {
2587     struct ciss_softc	*sc;
2588 
2589     debug_called(1);
2590 
2591     sc = (struct ciss_softc *)arg;
2592 
2593     /*
2594      * Check the adapter heartbeat.
2595      */
2596     if (sc->ciss_cfg->heartbeat == sc->ciss_heartbeat) {
2597 	sc->ciss_heart_attack++;
2598 	debug(0, "adapter heart attack in progress 0x%x/%d",
2599 	      sc->ciss_heartbeat, sc->ciss_heart_attack);
2600 	if (sc->ciss_heart_attack == 3) {
2601 	    ciss_printf(sc, "ADAPTER HEARTBEAT FAILED\n");
2602 	    /* XXX should reset adapter here */
2603 	}
2604     } else {
2605 	sc->ciss_heartbeat = sc->ciss_cfg->heartbeat;
2606 	sc->ciss_heart_attack = 0;
2607 	debug(3, "new heartbeat 0x%x", sc->ciss_heartbeat);
2608     }
2609 
2610     /*
2611      * If the notify event request has died for some reason, or has
2612      * not started yet, restart it.
2613      */
2614     if (!(sc->ciss_flags & CISS_FLAG_NOTIFY_OK)) {
2615 	debug(0, "(re)starting Event Notify chain");
2616 	ciss_notify_event(sc);
2617     }
2618 
2619     /*
2620      * Reschedule.
2621      */
2622     if (!(sc->ciss_flags & CISS_FLAG_ABORTING))
2623 	sc->ciss_periodic = timeout(ciss_periodic, sc, CISS_HEARTBEAT_RATE * hz);
2624 }
2625 
2626 /************************************************************************
2627  * Request a notification response from the adapter.
2628  *
2629  * If (cr) is NULL, this is the first request of the adapter, so
2630  * reset the adapter's message pointer and start with the oldest
2631  * message available.
2632  */
2633 static void
2634 ciss_notify_event(struct ciss_softc *sc)
2635 {
2636     struct ciss_request		*cr;
2637     struct ciss_command		*cc;
2638     struct ciss_notify_cdb	*cnc;
2639     int				error;
2640 
2641     debug_called(1);
2642 
2643     cr = sc->ciss_periodic_notify;
2644 
2645     /* get a request if we don't already have one */
2646     if (cr == NULL) {
2647 	if ((error = ciss_get_request(sc, &cr)) != 0) {
2648 	    debug(0, "can't get notify event request");
2649 	    goto out;
2650 	}
2651 	sc->ciss_periodic_notify = cr;
2652 	cr->cr_complete = ciss_notify_complete;
2653 	debug(1, "acquired request %d", cr->cr_tag);
2654     }
2655 
2656     /*
2657      * Get a databuffer if we don't already have one, note that the
2658      * adapter command wants a larger buffer than the actual
2659      * structure.
2660      */
2661     if (cr->cr_data == NULL) {
2662 	if ((cr->cr_data = malloc(CISS_NOTIFY_DATA_SIZE, CISS_MALLOC_CLASS, M_NOWAIT)) == NULL) {
2663 	    debug(0, "can't get notify event request buffer");
2664 	    error = ENOMEM;
2665 	    goto out;
2666 	}
2667 	cr->cr_length = CISS_NOTIFY_DATA_SIZE;
2668     }
2669 
2670     /* re-setup the request's command (since we never release it) XXX overkill*/
2671     ciss_preen_command(cr);
2672 
2673     /* (re)build the notify event command */
2674     cc = CISS_FIND_COMMAND(cr);
2675     cc->header.address.physical.mode = CISS_HDR_ADDRESS_MODE_PERIPHERAL;
2676     cc->header.address.physical.bus = 0;
2677     cc->header.address.physical.target = 0;
2678 
2679     cc->cdb.cdb_length = sizeof(*cnc);
2680     cc->cdb.type = CISS_CDB_TYPE_COMMAND;
2681     cc->cdb.attribute = CISS_CDB_ATTRIBUTE_SIMPLE;
2682     cc->cdb.direction = CISS_CDB_DIRECTION_READ;
2683     cc->cdb.timeout = 0;	/* no timeout, we hope */
2684 
2685     cnc = (struct ciss_notify_cdb *)&(cc->cdb.cdb[0]);
2686     bzero(cr->cr_data, CISS_NOTIFY_DATA_SIZE);
2687     cnc->opcode = CISS_OPCODE_READ;
2688     cnc->command = CISS_COMMAND_NOTIFY_ON_EVENT;
2689     cnc->timeout = 0;		/* no timeout, we hope */
2690     cnc->synchronous = 0;
2691     cnc->ordered = 0;
2692     cnc->seek_to_oldest = 0;
2693     cnc->new_only = 0;
2694     cnc->length = htonl(CISS_NOTIFY_DATA_SIZE);
2695 
2696     /* submit the request */
2697     error = ciss_start(cr);
2698 
2699  out:
2700     if (error) {
2701 	if (cr != NULL) {
2702 	    if (cr->cr_data != NULL)
2703 		free(cr->cr_data, CISS_MALLOC_CLASS);
2704 	    ciss_release_request(cr);
2705 	}
2706 	sc->ciss_periodic_notify = NULL;
2707 	debug(0, "can't submit notify event request");
2708 	sc->ciss_flags &= ~CISS_FLAG_NOTIFY_OK;
2709     } else {
2710 	debug(1, "notify event submitted");
2711 	sc->ciss_flags |= CISS_FLAG_NOTIFY_OK;
2712     }
2713 }
2714 
2715 static void
2716 ciss_notify_complete(struct ciss_request *cr)
2717 {
2718     struct ciss_command	*cc;
2719     struct ciss_notify	*cn;
2720     struct ciss_softc	*sc;
2721     int			scsi_status;
2722     int			command_status;
2723 
2724     debug_called(1);
2725 
2726     cc = CISS_FIND_COMMAND(cr);
2727     cn = (struct ciss_notify *)cr->cr_data;
2728     sc = cr->cr_sc;
2729 
2730     /*
2731      * Report request results, decode status.
2732      */
2733     ciss_report_request(cr, &command_status, &scsi_status);
2734 
2735     /*
2736      * Abort the chain on a fatal error.
2737      *
2738      * XXX which of these are actually errors?
2739      */
2740     if ((command_status != CISS_CMD_STATUS_SUCCESS) &&
2741 	(command_status != CISS_CMD_STATUS_TARGET_STATUS) &&
2742 	(command_status != CISS_CMD_STATUS_TIMEOUT)) {	/* XXX timeout? */
2743 	ciss_printf(sc, "fatal error in Notify Event request (%s)\n",
2744 		    ciss_name_command_status(command_status));
2745 	ciss_release_request(cr);
2746 	sc->ciss_flags &= ~CISS_FLAG_NOTIFY_OK;
2747 	return;
2748     }
2749 
2750     /*
2751      * If the adapter gave us a text message, print it.
2752      */
2753     if (cn->message[0] != 0)
2754 	ciss_printf(sc, "*** %.80s\n", cn->message);
2755 
2756     debug(0, "notify event class %d subclass %d detail %d",
2757 		cn->class, cn->subclass, cn->detail);
2758 
2759     /*
2760      * If there's room, save the event for a user-level tool.
2761      */
2762     if (((sc->ciss_notify_head + 1) % CISS_MAX_EVENTS) != sc->ciss_notify_tail) {
2763 	sc->ciss_notify[sc->ciss_notify_head] = *cn;
2764 	sc->ciss_notify_head = (sc->ciss_notify_head + 1) % CISS_MAX_EVENTS;
2765     }
2766 
2767     /*
2768      * Some events are directly of interest to us.
2769      */
2770     switch (cn->class) {
2771     case CISS_NOTIFY_LOGICAL:
2772 	ciss_notify_logical(sc, cn);
2773 	break;
2774     case CISS_NOTIFY_PHYSICAL:
2775 	ciss_notify_physical(sc, cn);
2776 	break;
2777     }
2778 
2779     /*
2780      * If the response indicates that the notifier has been aborted,
2781      * release the notifier command.
2782      */
2783     if ((cn->class == CISS_NOTIFY_NOTIFIER) &&
2784 	(cn->subclass == CISS_NOTIFY_NOTIFIER_STATUS) &&
2785 	(cn->detail == 1)) {
2786 	debug(0, "notifier exiting");
2787 	sc->ciss_flags &= ~CISS_FLAG_NOTIFY_OK;
2788 	ciss_release_request(cr);
2789 	sc->ciss_periodic_notify = NULL;
2790 	wakeup(&sc->ciss_periodic_notify);
2791     }
2792 
2793     /*
2794      * Send a new notify event command, if we're not aborting.
2795      */
2796     if (!(sc->ciss_flags & CISS_FLAG_ABORTING)) {
2797 	ciss_notify_event(sc);
2798     }
2799 }
2800 
2801 /************************************************************************
2802  * Abort the Notify Event chain.
2803  *
2804  * Note that we can't just abort the command in progress; we have to
2805  * explicitly issue an Abort Notify Event command in order for the
2806  * adapter to clean up correctly.
2807  *
2808  * If we are called with CISS_FLAG_ABORTING set in the adapter softc,
2809  * the chain will not restart itself.
2810  */
2811 static int
2812 ciss_notify_abort(struct ciss_softc *sc)
2813 {
2814     struct ciss_request		*cr;
2815     struct ciss_command		*cc;
2816     struct ciss_notify_cdb	*cnc;
2817     int				error, s, command_status, scsi_status;
2818 
2819     debug_called(1);
2820 
2821     cr = NULL;
2822     error = 0;
2823 
2824     /* verify that there's an outstanding command */
2825     if (!(sc->ciss_flags & CISS_FLAG_NOTIFY_OK))
2826 	goto out;
2827 
2828     /* get a command to issue the abort with */
2829     if ((error = ciss_get_request(sc, &cr)))
2830 	goto out;
2831 
2832     /* get a buffer for the result */
2833     if ((cr->cr_data = malloc(CISS_NOTIFY_DATA_SIZE, CISS_MALLOC_CLASS, M_NOWAIT)) == NULL) {
2834 	debug(0, "can't get notify event request buffer");
2835 	error = ENOMEM;
2836 	goto out;
2837     }
2838     cr->cr_length = CISS_NOTIFY_DATA_SIZE;
2839 
2840     /* build the CDB */
2841     cc = CISS_FIND_COMMAND(cr);
2842     cc->header.address.physical.mode = CISS_HDR_ADDRESS_MODE_PERIPHERAL;
2843     cc->header.address.physical.bus = 0;
2844     cc->header.address.physical.target = 0;
2845     cc->cdb.cdb_length = sizeof(*cnc);
2846     cc->cdb.type = CISS_CDB_TYPE_COMMAND;
2847     cc->cdb.attribute = CISS_CDB_ATTRIBUTE_SIMPLE;
2848     cc->cdb.direction = CISS_CDB_DIRECTION_READ;
2849     cc->cdb.timeout = 0;	/* no timeout, we hope */
2850 
2851     cnc = (struct ciss_notify_cdb *)&(cc->cdb.cdb[0]);
2852     bzero(cnc, sizeof(*cnc));
2853     cnc->opcode = CISS_OPCODE_WRITE;
2854     cnc->command = CISS_COMMAND_ABORT_NOTIFY;
2855     cnc->length = htonl(CISS_NOTIFY_DATA_SIZE);
2856 
2857     ciss_print_request(cr);
2858 
2859     /*
2860      * Submit the request and wait for it to complete.
2861      */
2862     if ((error = ciss_synch_request(cr, 60 * 1000)) != 0) {
2863 	ciss_printf(sc, "Abort Notify Event command failed (%d)\n", error);
2864 	goto out;
2865     }
2866 
2867     /*
2868      * Check response.
2869      */
2870     ciss_report_request(cr, &command_status, &scsi_status);
2871     switch(command_status) {
2872     case CISS_CMD_STATUS_SUCCESS:
2873 	break;
2874     case CISS_CMD_STATUS_INVALID_COMMAND:
2875 	/*
2876 	 * Some older adapters don't support the CISS version of this
2877 	 * command.  Fall back to using the BMIC version.
2878 	 */
2879 	error = ciss_notify_abort_bmic(sc);
2880 	if (error != 0)
2881 	    goto out;
2882 	break;
2883 
2884     case CISS_CMD_STATUS_TARGET_STATUS:
2885 	/*
2886 	 * This can happen if the adapter thinks there wasn't an outstanding
2887 	 * Notify Event command but we did.  We clean up here.
2888 	 */
2889 	if (scsi_status == CISS_SCSI_STATUS_CHECK_CONDITION) {
2890 	    if (sc->ciss_periodic_notify != NULL)
2891 		ciss_release_request(sc->ciss_periodic_notify);
2892 	    error = 0;
2893 	    goto out;
2894 	}
2895 	/* FALLTHROUGH */
2896 
2897     default:
2898 	ciss_printf(sc, "Abort Notify Event command failed (%s)\n",
2899 		    ciss_name_command_status(command_status));
2900 	error = EIO;
2901 	goto out;
2902     }
2903 
2904     /*
2905      * Sleep waiting for the notifier command to complete.  Note
2906      * that if it doesn't, we may end up in a bad situation, since
2907      * the adapter may deliver it later.  Also note that the adapter
2908      * requires the Notify Event command to be cancelled in order to
2909      * maintain internal bookkeeping.
2910      */
2911     s = splcam();
2912     while (sc->ciss_periodic_notify != NULL) {
2913 	error = tsleep(&sc->ciss_periodic_notify, 0, "cissNEA", hz * 5);
2914 	if (error == EWOULDBLOCK) {
2915 	    ciss_printf(sc, "Notify Event command failed to abort, adapter may wedge.\n");
2916 	    break;
2917 	}
2918     }
2919     splx(s);
2920 
2921  out:
2922     /* release the cancel request */
2923     if (cr != NULL) {
2924 	if (cr->cr_data != NULL)
2925 	    free(cr->cr_data, CISS_MALLOC_CLASS);
2926 	ciss_release_request(cr);
2927     }
2928     if (error == 0)
2929 	sc->ciss_flags &= ~CISS_FLAG_NOTIFY_OK;
2930     return(error);
2931 }
2932 
2933 /************************************************************************
2934  * Abort the Notify Event chain using a BMIC command.
2935  */
2936 static int
2937 ciss_notify_abort_bmic(struct ciss_softc *sc)
2938 {
2939     struct ciss_request			*cr;
2940     int					error, command_status;
2941 
2942     debug_called(1);
2943 
2944     cr = NULL;
2945     error = 0;
2946 
2947     /* verify that there's an outstanding command */
2948     if (!(sc->ciss_flags & CISS_FLAG_NOTIFY_OK))
2949 	goto out;
2950 
2951     /*
2952      * Build a BMIC command to cancel the Notify on Event command.
2953      *
2954      * Note that we are sending a CISS opcode here.  Odd.
2955      */
2956     if ((error = ciss_get_bmic_request(sc, &cr, CISS_COMMAND_ABORT_NOTIFY,
2957 				       NULL, 0)) != 0)
2958 	goto out;
2959 
2960     /*
2961      * Submit the request and wait for it to complete.
2962      */
2963     if ((error = ciss_synch_request(cr, 60 * 1000)) != 0) {
2964 	ciss_printf(sc, "error sending BMIC Cancel Notify on Event command (%d)\n", error);
2965 	goto out;
2966     }
2967 
2968     /*
2969      * Check response.
2970      */
2971     ciss_report_request(cr, &command_status, NULL);
2972     switch(command_status) {
2973     case CISS_CMD_STATUS_SUCCESS:
2974 	break;
2975     default:
2976 	ciss_printf(sc, "error cancelling Notify on Event (%s)\n",
2977 		    ciss_name_command_status(command_status));
2978 	error = EIO;
2979 	goto out;
2980     }
2981 
2982 out:
2983     if (cr != NULL)
2984 	ciss_release_request(cr);
2985     return(error);
2986 }
2987 
2988 /************************************************************************
2989  * Handle a notify event relating to the status of a logical drive.
2990  *
2991  * XXX need to be able to defer some of these to properly handle
2992  *     calling the "ID Physical drive" command, unless the 'extended'
2993  *     drive IDs are always in BIG_MAP format.
2994  */
2995 static void
2996 ciss_notify_logical(struct ciss_softc *sc, struct ciss_notify *cn)
2997 {
2998     struct ciss_ldrive	*ld;
2999     int			ostatus;
3000 
3001     debug_called(2);
3002 
3003     ld = &sc->ciss_logical[cn->data.logical_status.logical_drive];
3004 
3005     switch (cn->subclass) {
3006     case CISS_NOTIFY_LOGICAL_STATUS:
3007 	switch (cn->detail) {
3008 	case 0:
3009 	    ciss_name_device(sc, cn->data.logical_status.logical_drive);
3010 	    ciss_printf(sc, "logical drive %d (%s) changed status %s->%s, spare status 0x%b\n",
3011 			cn->data.logical_status.logical_drive, ld->cl_name,
3012 			ciss_name_ldrive_status(cn->data.logical_status.previous_state),
3013 			ciss_name_ldrive_status(cn->data.logical_status.new_state),
3014 			cn->data.logical_status.spare_state,
3015 			"\20\1configured\2rebuilding\3failed\4in use\5available\n");
3016 
3017 	    /*
3018 	     * Update our idea of the drive's status.
3019 	     */
3020 	    ostatus = ciss_decode_ldrive_status(cn->data.logical_status.previous_state);
3021 	    ld->cl_status = ciss_decode_ldrive_status(cn->data.logical_status.new_state);
3022 	    if (ld->cl_lstatus != NULL)
3023 		ld->cl_lstatus->status = cn->data.logical_status.new_state;
3024 
3025 #if 0
3026 	    /*
3027 	     * Have CAM rescan the drive if its status has changed.
3028 	     */
3029 	    if (ostatus != ld->cl_status)
3030 		ciss_cam_rescan_target(sc, cn->data.logical_status.logical_drive);
3031 #endif
3032 
3033 	    break;
3034 
3035 	case 1:	/* logical drive has recognised new media, needs Accept Media Exchange */
3036 	    ciss_name_device(sc, cn->data.logical_status.logical_drive);
3037 	    ciss_printf(sc, "logical drive %d (%s) media exchanged, ready to go online\n",
3038 			cn->data.logical_status.logical_drive, ld->cl_name);
3039 	    ciss_accept_media(sc, cn->data.logical_status.logical_drive, 1);
3040 	    break;
3041 
3042 	case 2:
3043 	case 3:
3044 	    ciss_printf(sc, "rebuild of logical drive %d (%s) failed due to %s error\n",
3045 			cn->data.rebuild_aborted.logical_drive,
3046 			sc->ciss_logical[cn->data.rebuild_aborted.logical_drive].cl_name,
3047 			(cn->detail == 2) ? "read" : "write");
3048 	    break;
3049 	}
3050 	break;
3051 
3052     case CISS_NOTIFY_LOGICAL_ERROR:
3053 	if (cn->detail == 0) {
3054 	    ciss_printf(sc, "FATAL I/O ERROR on logical drive %d (%s), SCSI port %d ID %d\n",
3055 			cn->data.io_error.logical_drive,
3056 			sc->ciss_logical[cn->data.io_error.logical_drive].cl_name,
3057 			cn->data.io_error.failure_bus,
3058 			cn->data.io_error.failure_drive);
3059 	    /* XXX should we take the drive down at this point, or will we be told? */
3060 	}
3061 	break;
3062 
3063     case CISS_NOTIFY_LOGICAL_SURFACE:
3064 	if (cn->detail == 0)
3065 	    ciss_printf(sc, "logical drive %d (%s) completed consistency initialisation\n",
3066 			cn->data.consistency_completed.logical_drive,
3067 			sc->ciss_logical[cn->data.consistency_completed.logical_drive].cl_name);
3068 	break;
3069     }
3070 }
3071 
3072 /************************************************************************
3073  * Handle a notify event relating to the status of a physical drive.
3074  */
3075 static void
3076 ciss_notify_physical(struct ciss_softc *sc, struct ciss_notify *cn)
3077 {
3078 
3079 }
3080 
3081 /************************************************************************
3082  * Print a request.
3083  */
3084 static void
3085 ciss_print_request(struct ciss_request *cr)
3086 {
3087     struct ciss_softc	*sc;
3088     struct ciss_command	*cc;
3089     int			i;
3090 
3091     sc = cr->cr_sc;
3092     cc = CISS_FIND_COMMAND(cr);
3093 
3094     ciss_printf(sc, "REQUEST @ %p\n", cr);
3095     ciss_printf(sc, "  data %p/%d  tag %d  flags %b\n",
3096 	      cr->cr_data, cr->cr_length, cr->cr_tag, cr->cr_flags,
3097 	      "\20\1mapped\2sleep\3poll\4dataout\5datain\n");
3098     ciss_printf(sc, "  sg list/total %d/%d  host tag 0x%x\n",
3099 		cc->header.sg_in_list, cc->header.sg_total, cc->header.host_tag);
3100     switch(cc->header.address.mode.mode) {
3101     case CISS_HDR_ADDRESS_MODE_PERIPHERAL:
3102     case CISS_HDR_ADDRESS_MODE_MASK_PERIPHERAL:
3103 	ciss_printf(sc, "  physical bus %d target %d\n",
3104 		    cc->header.address.physical.bus, cc->header.address.physical.target);
3105 	break;
3106     case CISS_HDR_ADDRESS_MODE_LOGICAL:
3107 	ciss_printf(sc, "  logical unit %d\n", cc->header.address.logical.lun);
3108 	break;
3109     }
3110     ciss_printf(sc, "  %s cdb length %d type %s attribute %s\n",
3111 		(cc->cdb.direction == CISS_CDB_DIRECTION_NONE) ? "no-I/O" :
3112 		(cc->cdb.direction == CISS_CDB_DIRECTION_READ) ? "READ" :
3113 		(cc->cdb.direction == CISS_CDB_DIRECTION_WRITE) ? "WRITE" : "??",
3114 		cc->cdb.cdb_length,
3115 		(cc->cdb.type == CISS_CDB_TYPE_COMMAND) ? "command" :
3116 		(cc->cdb.type == CISS_CDB_TYPE_MESSAGE) ? "message" : "??",
3117 		(cc->cdb.attribute == CISS_CDB_ATTRIBUTE_UNTAGGED) ? "untagged" :
3118 		(cc->cdb.attribute == CISS_CDB_ATTRIBUTE_SIMPLE) ? "simple" :
3119 		(cc->cdb.attribute == CISS_CDB_ATTRIBUTE_HEAD_OF_QUEUE) ? "head-of-queue" :
3120 		(cc->cdb.attribute == CISS_CDB_ATTRIBUTE_ORDERED) ? "ordered" :
3121 		(cc->cdb.attribute == CISS_CDB_ATTRIBUTE_AUTO_CONTINGENT) ? "auto-contingent" : "??");
3122     ciss_printf(sc, "  %*D\n", cc->cdb.cdb_length, &cc->cdb.cdb[0], " ");
3123 
3124     if (cc->header.host_tag & CISS_HDR_HOST_TAG_ERROR) {
3125 	/* XXX print error info */
3126     } else {
3127 	/* since we don't use chained s/g, don't support it here */
3128 	for (i = 0; i < cc->header.sg_in_list; i++) {
3129 	    if ((i % 4) == 0)
3130 		ciss_printf(sc, "   ");
3131 	    printf("0x%08x/%d ", (u_int32_t)cc->sg[i].address, cc->sg[i].length);
3132 	    if ((((i + 1) % 4) == 0) || (i == (cc->header.sg_in_list - 1)))
3133 		printf("\n");
3134 	}
3135     }
3136 }
3137 
3138 /************************************************************************
3139  * Print information about the status of a logical drive.
3140  */
3141 static void
3142 ciss_print_ldrive(struct ciss_softc *sc, struct ciss_ldrive *ld)
3143 {
3144     int		bus, target, i;
3145 
3146     if (ld->cl_lstatus == NULL) {
3147 	printf("does not exist\n");
3148 	return;
3149     }
3150 
3151     /* print drive status */
3152     switch(ld->cl_lstatus->status) {
3153     case CISS_LSTATUS_OK:
3154 	printf("online\n");
3155 	break;
3156     case CISS_LSTATUS_INTERIM_RECOVERY:
3157 	printf("in interim recovery mode\n");
3158 	break;
3159     case CISS_LSTATUS_READY_RECOVERY:
3160 	printf("ready to begin recovery\n");
3161 	break;
3162     case CISS_LSTATUS_RECOVERING:
3163 	bus = CISS_BIG_MAP_BUS(sc, ld->cl_lstatus->drive_rebuilding);
3164 	target = CISS_BIG_MAP_BUS(sc, ld->cl_lstatus->drive_rebuilding);
3165 	printf("being recovered, working on physical drive %d.%d, %u blocks remaining\n",
3166 	       bus, target, ld->cl_lstatus->blocks_to_recover);
3167 	break;
3168     case CISS_LSTATUS_EXPANDING:
3169 	printf("being expanded, %u blocks remaining\n",
3170 	       ld->cl_lstatus->blocks_to_recover);
3171 	break;
3172     case CISS_LSTATUS_QUEUED_FOR_EXPANSION:
3173 	printf("queued for expansion\n");
3174 	break;
3175     case CISS_LSTATUS_FAILED:
3176 	printf("queued for expansion\n");
3177 	break;
3178     case CISS_LSTATUS_WRONG_PDRIVE:
3179 	printf("wrong physical drive inserted\n");
3180 	break;
3181     case CISS_LSTATUS_MISSING_PDRIVE:
3182 	printf("missing a needed physical drive\n");
3183 	break;
3184     case CISS_LSTATUS_BECOMING_READY:
3185 	printf("becoming ready\n");
3186 	break;
3187     }
3188 
3189     /* print failed physical drives */
3190     for (i = 0; i < CISS_BIG_MAP_ENTRIES / 8; i++) {
3191 	bus = CISS_BIG_MAP_BUS(sc, ld->cl_lstatus->drive_failure_map[i]);
3192 	target = CISS_BIG_MAP_TARGET(sc, ld->cl_lstatus->drive_failure_map[i]);
3193 	if (bus == -1)
3194 	    continue;
3195 	ciss_printf(sc, "physical drive %d:%d (%x) failed\n", bus, target,
3196 		    ld->cl_lstatus->drive_failure_map[i]);
3197     }
3198 }
3199 
3200 #ifdef CISS_DEBUG
3201 /************************************************************************
3202  * Print information about the controller/driver.
3203  */
3204 static void
3205 ciss_print_adapter(struct ciss_softc *sc)
3206 {
3207     int		i;
3208 
3209     ciss_printf(sc, "ADAPTER:\n");
3210     for (i = 0; i < CISSQ_COUNT; i++) {
3211 	ciss_printf(sc, "%s     %d/%d\n",
3212 	    i == 0 ? "free" :
3213 	    i == 1 ? "busy" : "complete",
3214 	    sc->ciss_qstat[i].q_length,
3215 	    sc->ciss_qstat[i].q_max);
3216     }
3217     ciss_printf(sc, "max_requests %d\n", sc->ciss_max_requests);
3218     ciss_printf(sc, "notify_head/tail %d/%d\n",
3219 	sc->ciss_notify_head, sc->ciss_notify_tail);
3220     ciss_printf(sc, "flags %b\n", sc->ciss_flags,
3221 	"\20\1notify_ok\2control_open\3aborting\4running\21fake_synch\22bmic_abort\n");
3222 
3223     for (i = 0; i < CISS_MAX_LOGICAL; i++) {
3224 	ciss_printf(sc, "LOGICAL DRIVE %d:  ", i);
3225 	ciss_print_ldrive(sc, sc->ciss_logical + i);
3226     }
3227 
3228     for (i = 1; i < sc->ciss_max_requests; i++)
3229 	ciss_print_request(sc->ciss_request + i);
3230 
3231 }
3232 
3233 /* DDB hook */
3234 static void
3235 ciss_print0(void)
3236 {
3237     struct ciss_softc	*sc;
3238 
3239     sc = devclass_get_softc(devclass_find("ciss"), 0);
3240     if (sc == NULL) {
3241 	printf("no ciss controllers\n");
3242     } else {
3243 	ciss_print_adapter(sc);
3244     }
3245 }
3246 #endif
3247 
3248 /************************************************************************
3249  * Return a name for a logical drive status value.
3250  */
3251 static const char *
3252 ciss_name_ldrive_status(int status)
3253 {
3254     switch (status) {
3255     case CISS_LSTATUS_OK:
3256 	return("OK");
3257     case CISS_LSTATUS_FAILED:
3258 	return("failed");
3259     case CISS_LSTATUS_NOT_CONFIGURED:
3260 	return("not configured");
3261     case CISS_LSTATUS_INTERIM_RECOVERY:
3262 	return("interim recovery");
3263     case CISS_LSTATUS_READY_RECOVERY:
3264 	return("ready for recovery");
3265     case CISS_LSTATUS_RECOVERING:
3266 	return("recovering");
3267     case CISS_LSTATUS_WRONG_PDRIVE:
3268 	return("wrong physical drive inserted");
3269     case CISS_LSTATUS_MISSING_PDRIVE:
3270 	return("missing physical drive");
3271     case CISS_LSTATUS_EXPANDING:
3272 	return("expanding");
3273     case CISS_LSTATUS_BECOMING_READY:
3274 	return("becoming ready");
3275     case CISS_LSTATUS_QUEUED_FOR_EXPANSION:
3276 	return("queued for expansion");
3277     }
3278     return("unknown status");
3279 }
3280 
3281 /************************************************************************
3282  * Return an online/offline/nonexistent value for a logical drive
3283  * status value.
3284  */
3285 static int
3286 ciss_decode_ldrive_status(int status)
3287 {
3288     switch(status) {
3289     case CISS_LSTATUS_NOT_CONFIGURED:
3290 	return(CISS_LD_NONEXISTENT);
3291 
3292     case CISS_LSTATUS_OK:
3293     case CISS_LSTATUS_INTERIM_RECOVERY:
3294     case CISS_LSTATUS_READY_RECOVERY:
3295     case CISS_LSTATUS_RECOVERING:
3296     case CISS_LSTATUS_EXPANDING:
3297     case CISS_LSTATUS_QUEUED_FOR_EXPANSION:
3298 	return(CISS_LD_ONLINE);
3299 
3300     case CISS_LSTATUS_FAILED:
3301     case CISS_LSTATUS_WRONG_PDRIVE:
3302     case CISS_LSTATUS_MISSING_PDRIVE:
3303     case CISS_LSTATUS_BECOMING_READY:
3304     default:
3305 	return(CISS_LD_OFFLINE);
3306     }
3307 }
3308 
3309 
3310 /************************************************************************
3311  * Return a name for a logical drive's organisation.
3312  */
3313 static const char *
3314 ciss_name_ldrive_org(int org)
3315 {
3316     switch(org) {
3317     case CISS_LDRIVE_RAID0:
3318 	return("RAID 0");
3319     case CISS_LDRIVE_RAID1:
3320 	return("RAID 1");
3321     case CISS_LDRIVE_RAID4:
3322 	return("RAID 4");
3323     case CISS_LDRIVE_RAID5:
3324 	return("RAID 5");
3325     }
3326     return("unkown");
3327 }
3328 
3329 /************************************************************************
3330  * Return a name for a command status value.
3331  */
3332 static const char *
3333 ciss_name_command_status(int status)
3334 {
3335     switch(status) {
3336     case CISS_CMD_STATUS_SUCCESS:
3337 	return("success");
3338     case CISS_CMD_STATUS_TARGET_STATUS:
3339 	return("target status");
3340     case CISS_CMD_STATUS_DATA_UNDERRUN:
3341 	return("data underrun");
3342     case CISS_CMD_STATUS_DATA_OVERRUN:
3343 	return("data overrun");
3344     case CISS_CMD_STATUS_INVALID_COMMAND:
3345 	return("invalid command");
3346     case CISS_CMD_STATUS_PROTOCOL_ERROR:
3347 	return("protocol error");
3348     case CISS_CMD_STATUS_HARDWARE_ERROR:
3349 	return("hardware error");
3350     case CISS_CMD_STATUS_CONNECTION_LOST:
3351 	return("connection lost");
3352     case CISS_CMD_STATUS_ABORTED:
3353 	return("aborted");
3354     case CISS_CMD_STATUS_ABORT_FAILED:
3355 	return("abort failed");
3356     case CISS_CMD_STATUS_UNSOLICITED_ABORT:
3357 	return("unsolicited abort");
3358     case CISS_CMD_STATUS_TIMEOUT:
3359 	return("timeout");
3360     case CISS_CMD_STATUS_UNABORTABLE:
3361 	return("unabortable");
3362     }
3363     return("unknown status");
3364 }
3365 
3366 /************************************************************************
3367  * Handle an open on the control device.
3368  */
3369 static int
3370 ciss_open(dev_t dev, int flags, int fmt, d_thread_t *p)
3371 {
3372     struct ciss_softc	*sc;
3373 
3374     debug_called(1);
3375 
3376     sc = (struct ciss_softc *)dev->si_drv1;
3377 
3378     /* we might want to veto if someone already has us open */
3379 
3380     sc->ciss_flags |= CISS_FLAG_CONTROL_OPEN;
3381     return(0);
3382 }
3383 
3384 /************************************************************************
3385  * Handle the last close on the control device.
3386  */
3387 static int
3388 ciss_close(dev_t dev, int flags, int fmt, d_thread_t *p)
3389 {
3390     struct ciss_softc	*sc;
3391 
3392     debug_called(1);
3393 
3394     sc = (struct ciss_softc *)dev->si_drv1;
3395 
3396     sc->ciss_flags &= ~CISS_FLAG_CONTROL_OPEN;
3397     return (0);
3398 }
3399 
3400 /********************************************************************************
3401  * Handle adapter-specific control operations.
3402  *
3403  * Note that the API here is compatible with the Linux driver, in order to
3404  * simplify the porting of Compaq's userland tools.
3405  */
3406 static int
3407 ciss_ioctl(dev_t dev, u_long cmd, caddr_t addr, int32_t flag, d_thread_t *p)
3408 {
3409     struct ciss_softc		*sc;
3410     int				error;
3411 
3412     debug_called(1);
3413 
3414     sc = (struct ciss_softc *)dev->si_drv1;
3415     error = 0;
3416 
3417     switch(cmd) {
3418     case CCISS_GETPCIINFO:
3419     {
3420 	cciss_pci_info_struct	*pis = (cciss_pci_info_struct *)addr;
3421 
3422 	pis->bus = pci_get_bus(sc->ciss_dev);
3423 	pis->dev_fn = pci_get_slot(sc->ciss_dev);
3424 	pis->board_id = pci_get_devid(sc->ciss_dev);
3425 
3426 	break;
3427     }
3428 
3429     case CCISS_GETINTINFO:
3430     {
3431 	cciss_coalint_struct	*cis = (cciss_coalint_struct *)addr;
3432 
3433 	cis->delay = sc->ciss_cfg->interrupt_coalesce_delay;
3434 	cis->count = sc->ciss_cfg->interrupt_coalesce_count;
3435 
3436 	break;
3437     }
3438 
3439     case CCISS_SETINTINFO:
3440     {
3441 	cciss_coalint_struct	*cis = (cciss_coalint_struct *)addr;
3442 
3443 	if ((cis->delay == 0) && (cis->count == 0)) {
3444 	    error = EINVAL;
3445 	    break;
3446 	}
3447 
3448 	/*
3449 	 * XXX apparently this is only safe if the controller is idle,
3450 	 *     we should suspend it before doing this.
3451 	 */
3452 	sc->ciss_cfg->interrupt_coalesce_delay = cis->delay;
3453 	sc->ciss_cfg->interrupt_coalesce_count = cis->count;
3454 
3455 	if (ciss_update_config(sc))
3456 	    error = EIO;
3457 
3458 	/* XXX resume the controller here */
3459 	break;
3460     }
3461 
3462     case CCISS_GETNODENAME:
3463 	bcopy(sc->ciss_cfg->server_name, (NodeName_type *)addr,
3464 	      sizeof(NodeName_type));
3465 	break;
3466 
3467     case CCISS_SETNODENAME:
3468 	bcopy((NodeName_type *)addr, sc->ciss_cfg->server_name,
3469 	      sizeof(NodeName_type));
3470 	if (ciss_update_config(sc))
3471 	    error = EIO;
3472 	break;
3473 
3474     case CCISS_GETHEARTBEAT:
3475 	*(Heartbeat_type *)addr = sc->ciss_cfg->heartbeat;
3476 	break;
3477 
3478     case CCISS_GETBUSTYPES:
3479 	*(BusTypes_type *)addr = sc->ciss_cfg->bus_types;
3480 	break;
3481 
3482     case CCISS_GETFIRMVER:
3483 	bcopy(sc->ciss_id->running_firmware_revision, (FirmwareVer_type *)addr,
3484 	      sizeof(FirmwareVer_type));
3485 	break;
3486 
3487     case CCISS_GETDRIVERVER:
3488 	*(DriverVer_type *)addr = CISS_DRIVER_VERSION;
3489 	break;
3490 
3491     case CCISS_REVALIDVOLS:
3492 	/*
3493 	 * This is a bit ugly; to do it "right" we really need
3494 	 * to find any disks that have changed, kick CAM off them,
3495 	 * then rescan only these disks.  It'd be nice if they
3496 	 * a) told us which disk(s) they were going to play with,
3497 	 * and b) which ones had arrived. 8(
3498 	 */
3499 	break;
3500 
3501     case CCISS_PASSTHRU:
3502 	error = ciss_user_command(sc, (IOCTL_Command_struct *)addr);
3503 	break;
3504 
3505     default:
3506 	debug(0, "unknown ioctl 0x%lx", cmd);
3507 
3508 	debug(1, "CCISS_GETPCIINFO:   0x%lx", CCISS_GETPCIINFO);
3509 	debug(1, "CCISS_GETINTINFO:   0x%lx", CCISS_GETINTINFO);
3510 	debug(1, "CCISS_SETINTINFO:   0x%lx", CCISS_SETINTINFO);
3511 	debug(1, "CCISS_GETNODENAME:  0x%lx", CCISS_GETNODENAME);
3512 	debug(1, "CCISS_SETNODENAME:  0x%lx", CCISS_SETNODENAME);
3513 	debug(1, "CCISS_GETHEARTBEAT: 0x%lx", CCISS_GETHEARTBEAT);
3514 	debug(1, "CCISS_GETBUSTYPES:  0x%lx", CCISS_GETBUSTYPES);
3515 	debug(1, "CCISS_GETFIRMVER:   0x%lx", CCISS_GETFIRMVER);
3516 	debug(1, "CCISS_GETDRIVERVER: 0x%lx", CCISS_GETDRIVERVER);
3517 	debug(1, "CCISS_REVALIDVOLS:  0x%lx", CCISS_REVALIDVOLS);
3518 	debug(1, "CCISS_PASSTHRU:     0x%lx", CCISS_PASSTHRU);
3519 
3520 	error = ENOIOCTL;
3521 	break;
3522     }
3523 
3524     return(error);
3525 }
3526