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