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