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