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