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