ciss.c revision 265987
1/*-
2 * Copyright (c) 2001 Michael Smith
3 * Copyright (c) 2004 Paul Saab
4 * All rights reserved.
5 *
6 * Redistribution and use in source and binary forms, with or without
7 * modification, are permitted provided that the following conditions
8 * are met:
9 * 1. Redistributions of source code must retain the above copyright
10 *    notice, this list of conditions and the following disclaimer.
11 * 2. Redistributions in binary form must reproduce the above copyright
12 *    notice, this list of conditions and the following disclaimer in the
13 *    documentation and/or other materials provided with the distribution.
14 *
15 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
16 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
17 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
18 * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
19 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
20 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
21 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
22 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
23 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
24 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
25 * SUCH DAMAGE.
26 *
27 *	$FreeBSD: releng/10.0/sys/dev/ciss/ciss.c 265987 2014-05-13 23:22:28Z delphij $
28 */
29
30/*
31 * Common Interface for SCSI-3 Support driver.
32 *
33 * CISS claims to provide a common interface between a generic SCSI
34 * transport and an intelligent host adapter.
35 *
36 * This driver supports CISS as defined in the document "CISS Command
37 * Interface for SCSI-3 Support Open Specification", Version 1.04,
38 * Valence Number 1, dated 20001127, produced by Compaq Computer
39 * Corporation.  This document appears to be a hastily and somewhat
40 * arbitrarlily cut-down version of a larger (and probably even more
41 * chaotic and inconsistent) Compaq internal document.  Various
42 * details were also gleaned from Compaq's "cciss" driver for Linux.
43 *
44 * We provide a shim layer between the CISS interface and CAM,
45 * offloading most of the queueing and being-a-disk chores onto CAM.
46 * Entry to the driver is via the PCI bus attachment (ciss_probe,
47 * ciss_attach, etc) and via the CAM interface (ciss_cam_action,
48 * ciss_cam_poll).  The Compaq CISS adapters are, however, poor SCSI
49 * citizens and we have to fake up some responses to get reasonable
50 * behaviour out of them.  In addition, the CISS command set is by no
51 * means adequate to support the functionality of a RAID controller,
52 * and thus the supported Compaq adapters utilise portions of the
53 * control protocol from earlier Compaq adapter families.
54 *
55 * Note that we only support the "simple" transport layer over PCI.
56 * This interface (ab)uses the I2O register set (specifically the post
57 * queues) to exchange commands with the adapter.  Other interfaces
58 * are available, but we aren't supposed to know about them, and it is
59 * dubious whether they would provide major performance improvements
60 * except under extreme load.
61 *
62 * Currently the only supported CISS adapters are the Compaq Smart
63 * Array 5* series (5300, 5i, 532).  Even with only three adapters,
64 * Compaq still manage to have interface variations.
65 *
66 *
67 * Thanks must go to Fred Harris and Darryl DeVinney at Compaq, as
68 * well as Paul Saab at Yahoo! for their assistance in making this
69 * driver happen.
70 *
71 * More thanks must go to John Cagle at HP for the countless hours
72 * spent making this driver "work" with the MSA* series storage
73 * enclosures.  Without his help (and nagging), this driver could not
74 * be used with these enclosures.
75 */
76
77#include <sys/param.h>
78#include <sys/systm.h>
79#include <sys/malloc.h>
80#include <sys/kernel.h>
81#include <sys/bus.h>
82#include <sys/conf.h>
83#include <sys/stat.h>
84#include <sys/kthread.h>
85#include <sys/queue.h>
86#include <sys/sysctl.h>
87
88#include <cam/cam.h>
89#include <cam/cam_ccb.h>
90#include <cam/cam_periph.h>
91#include <cam/cam_sim.h>
92#include <cam/cam_xpt_sim.h>
93#include <cam/scsi/scsi_all.h>
94#include <cam/scsi/scsi_message.h>
95
96#include <machine/bus.h>
97#include <machine/endian.h>
98#include <machine/resource.h>
99#include <sys/rman.h>
100
101#include <dev/pci/pcireg.h>
102#include <dev/pci/pcivar.h>
103
104#include <dev/ciss/cissreg.h>
105#include <dev/ciss/cissio.h>
106#include <dev/ciss/cissvar.h>
107
108static MALLOC_DEFINE(CISS_MALLOC_CLASS, "ciss_data",
109    "ciss internal data buffers");
110
111/* pci interface */
112static int	ciss_lookup(device_t dev);
113static int	ciss_probe(device_t dev);
114static int	ciss_attach(device_t dev);
115static int	ciss_detach(device_t dev);
116static int	ciss_shutdown(device_t dev);
117
118/* (de)initialisation functions, control wrappers */
119static int	ciss_init_pci(struct ciss_softc *sc);
120static int	ciss_setup_msix(struct ciss_softc *sc);
121static int	ciss_init_perf(struct ciss_softc *sc);
122static int	ciss_wait_adapter(struct ciss_softc *sc);
123static int	ciss_flush_adapter(struct ciss_softc *sc);
124static int	ciss_init_requests(struct ciss_softc *sc);
125static void	ciss_command_map_helper(void *arg, bus_dma_segment_t *segs,
126					int nseg, int error);
127static int	ciss_identify_adapter(struct ciss_softc *sc);
128static int	ciss_init_logical(struct ciss_softc *sc);
129static int	ciss_init_physical(struct ciss_softc *sc);
130static int	ciss_filter_physical(struct ciss_softc *sc, struct ciss_lun_report *cll);
131static int	ciss_identify_logical(struct ciss_softc *sc, struct ciss_ldrive *ld);
132static int	ciss_get_ldrive_status(struct ciss_softc *sc,  struct ciss_ldrive *ld);
133static int	ciss_update_config(struct ciss_softc *sc);
134static int	ciss_accept_media(struct ciss_softc *sc, struct ciss_ldrive *ld);
135static void	ciss_init_sysctl(struct ciss_softc *sc);
136static void	ciss_soft_reset(struct ciss_softc *sc);
137static void	ciss_free(struct ciss_softc *sc);
138static void	ciss_spawn_notify_thread(struct ciss_softc *sc);
139static void	ciss_kill_notify_thread(struct ciss_softc *sc);
140
141/* request submission/completion */
142static int	ciss_start(struct ciss_request *cr);
143static void	ciss_done(struct ciss_softc *sc, cr_qhead_t *qh);
144static void	ciss_perf_done(struct ciss_softc *sc, cr_qhead_t *qh);
145static void	ciss_intr(void *arg);
146static void	ciss_perf_intr(void *arg);
147static void	ciss_perf_msi_intr(void *arg);
148static void	ciss_complete(struct ciss_softc *sc, cr_qhead_t *qh);
149static int	_ciss_report_request(struct ciss_request *cr, int *command_status, int *scsi_status, const char *func);
150static int	ciss_synch_request(struct ciss_request *cr, int timeout);
151static int	ciss_poll_request(struct ciss_request *cr, int timeout);
152static int	ciss_wait_request(struct ciss_request *cr, int timeout);
153#if 0
154static int	ciss_abort_request(struct ciss_request *cr);
155#endif
156
157/* request queueing */
158static int	ciss_get_request(struct ciss_softc *sc, struct ciss_request **crp);
159static void	ciss_preen_command(struct ciss_request *cr);
160static void 	ciss_release_request(struct ciss_request *cr);
161
162/* request helpers */
163static int	ciss_get_bmic_request(struct ciss_softc *sc, struct ciss_request **crp,
164				      int opcode, void **bufp, size_t bufsize);
165static int	ciss_user_command(struct ciss_softc *sc, IOCTL_Command_struct *ioc);
166
167/* DMA map/unmap */
168static int	ciss_map_request(struct ciss_request *cr);
169static void	ciss_request_map_helper(void *arg, bus_dma_segment_t *segs,
170					int nseg, int error);
171static void	ciss_unmap_request(struct ciss_request *cr);
172
173/* CAM interface */
174static int	ciss_cam_init(struct ciss_softc *sc);
175static void	ciss_cam_rescan_target(struct ciss_softc *sc,
176				       int bus, int target);
177static void	ciss_cam_action(struct cam_sim *sim, union ccb *ccb);
178static int	ciss_cam_action_io(struct cam_sim *sim, struct ccb_scsiio *csio);
179static int	ciss_cam_emulate(struct ciss_softc *sc, struct ccb_scsiio *csio);
180static void	ciss_cam_poll(struct cam_sim *sim);
181static void	ciss_cam_complete(struct ciss_request *cr);
182static void	ciss_cam_complete_fixup(struct ciss_softc *sc, struct ccb_scsiio *csio);
183static int	ciss_name_device(struct ciss_softc *sc, int bus, int target);
184
185/* periodic status monitoring */
186static void	ciss_periodic(void *arg);
187static void	ciss_nop_complete(struct ciss_request *cr);
188static void	ciss_disable_adapter(struct ciss_softc *sc);
189static void	ciss_notify_event(struct ciss_softc *sc);
190static void	ciss_notify_complete(struct ciss_request *cr);
191static int	ciss_notify_abort(struct ciss_softc *sc);
192static int	ciss_notify_abort_bmic(struct ciss_softc *sc);
193static void	ciss_notify_hotplug(struct ciss_softc *sc, struct ciss_notify *cn);
194static void	ciss_notify_logical(struct ciss_softc *sc, struct ciss_notify *cn);
195static void	ciss_notify_physical(struct ciss_softc *sc, struct ciss_notify *cn);
196
197/* debugging output */
198static void	ciss_print_request(struct ciss_request *cr);
199static void	ciss_print_ldrive(struct ciss_softc *sc, struct ciss_ldrive *ld);
200static const char *ciss_name_ldrive_status(int status);
201static int	ciss_decode_ldrive_status(int status);
202static const char *ciss_name_ldrive_org(int org);
203static const char *ciss_name_command_status(int status);
204
205/*
206 * PCI bus interface.
207 */
208static device_method_t ciss_methods[] = {
209    /* Device interface */
210    DEVMETHOD(device_probe,	ciss_probe),
211    DEVMETHOD(device_attach,	ciss_attach),
212    DEVMETHOD(device_detach,	ciss_detach),
213    DEVMETHOD(device_shutdown,	ciss_shutdown),
214    { 0, 0 }
215};
216
217static driver_t ciss_pci_driver = {
218    "ciss",
219    ciss_methods,
220    sizeof(struct ciss_softc)
221};
222
223static devclass_t	ciss_devclass;
224DRIVER_MODULE(ciss, pci, ciss_pci_driver, ciss_devclass, 0, 0);
225MODULE_DEPEND(ciss, cam, 1, 1, 1);
226MODULE_DEPEND(ciss, pci, 1, 1, 1);
227
228/*
229 * Control device interface.
230 */
231static d_open_t		ciss_open;
232static d_close_t	ciss_close;
233static d_ioctl_t	ciss_ioctl;
234
235static struct cdevsw ciss_cdevsw = {
236	.d_version =	D_VERSION,
237	.d_flags =	0,
238	.d_open =	ciss_open,
239	.d_close =	ciss_close,
240	.d_ioctl =	ciss_ioctl,
241	.d_name =	"ciss",
242};
243
244/*
245 * This tunable can be set at boot time and controls whether physical devices
246 * that are marked hidden by the firmware should be exposed anyways.
247 */
248static unsigned int ciss_expose_hidden_physical = 0;
249TUNABLE_INT("hw.ciss.expose_hidden_physical", &ciss_expose_hidden_physical);
250
251static unsigned int ciss_nop_message_heartbeat = 0;
252TUNABLE_INT("hw.ciss.nop_message_heartbeat", &ciss_nop_message_heartbeat);
253
254/*
255 * This tunable can force a particular transport to be used:
256 * <= 0 : use default
257 *    1 : force simple
258 *    2 : force performant
259 */
260static int ciss_force_transport = 0;
261TUNABLE_INT("hw.ciss.force_transport", &ciss_force_transport);
262
263/*
264 * This tunable can force a particular interrupt delivery method to be used:
265 * <= 0 : use default
266 *    1 : force INTx
267 *    2 : force MSIX
268 */
269static int ciss_force_interrupt = 0;
270TUNABLE_INT("hw.ciss.force_interrupt", &ciss_force_interrupt);
271
272/************************************************************************
273 * CISS adapters amazingly don't have a defined programming interface
274 * value.  (One could say some very despairing things about PCI and
275 * people just not getting the general idea.)  So we are forced to
276 * stick with matching against subvendor/subdevice, and thus have to
277 * be updated for every new CISS adapter that appears.
278 */
279#define CISS_BOARD_UNKNWON	0
280#define CISS_BOARD_SA5		1
281#define CISS_BOARD_SA5B		2
282#define CISS_BOARD_NOMSI	(1<<4)
283#define CISS_BOARD_SIMPLE       (1<<5)
284
285static struct
286{
287    u_int16_t	subvendor;
288    u_int16_t	subdevice;
289    int		flags;
290    char	*desc;
291} ciss_vendor_data[] = {
292    { 0x0e11, 0x4070, CISS_BOARD_SA5|CISS_BOARD_NOMSI|CISS_BOARD_SIMPLE,
293                                                        "Compaq Smart Array 5300" },
294    { 0x0e11, 0x4080, CISS_BOARD_SA5B|CISS_BOARD_NOMSI,	"Compaq Smart Array 5i" },
295    { 0x0e11, 0x4082, CISS_BOARD_SA5B|CISS_BOARD_NOMSI,	"Compaq Smart Array 532" },
296    { 0x0e11, 0x4083, CISS_BOARD_SA5B|CISS_BOARD_NOMSI,	"HP Smart Array 5312" },
297    { 0x0e11, 0x4091, CISS_BOARD_SA5,	"HP Smart Array 6i" },
298    { 0x0e11, 0x409A, CISS_BOARD_SA5,	"HP Smart Array 641" },
299    { 0x0e11, 0x409B, CISS_BOARD_SA5,	"HP Smart Array 642" },
300    { 0x0e11, 0x409C, CISS_BOARD_SA5,	"HP Smart Array 6400" },
301    { 0x0e11, 0x409D, CISS_BOARD_SA5,	"HP Smart Array 6400 EM" },
302    { 0x103C, 0x3211, CISS_BOARD_SA5,	"HP Smart Array E200i" },
303    { 0x103C, 0x3212, CISS_BOARD_SA5,	"HP Smart Array E200" },
304    { 0x103C, 0x3213, CISS_BOARD_SA5,	"HP Smart Array E200i" },
305    { 0x103C, 0x3214, CISS_BOARD_SA5,	"HP Smart Array E200i" },
306    { 0x103C, 0x3215, CISS_BOARD_SA5,	"HP Smart Array E200i" },
307    { 0x103C, 0x3220, CISS_BOARD_SA5,	"HP Smart Array" },
308    { 0x103C, 0x3222, CISS_BOARD_SA5,	"HP Smart Array" },
309    { 0x103C, 0x3223, CISS_BOARD_SA5,	"HP Smart Array P800" },
310    { 0x103C, 0x3225, CISS_BOARD_SA5,	"HP Smart Array P600" },
311    { 0x103C, 0x3230, CISS_BOARD_SA5,	"HP Smart Array" },
312    { 0x103C, 0x3231, CISS_BOARD_SA5,	"HP Smart Array" },
313    { 0x103C, 0x3232, CISS_BOARD_SA5,	"HP Smart Array" },
314    { 0x103C, 0x3233, CISS_BOARD_SA5,	"HP Smart Array" },
315    { 0x103C, 0x3234, CISS_BOARD_SA5,	"HP Smart Array P400" },
316    { 0x103C, 0x3235, CISS_BOARD_SA5,	"HP Smart Array P400i" },
317    { 0x103C, 0x3236, CISS_BOARD_SA5,	"HP Smart Array" },
318    { 0x103C, 0x3237, CISS_BOARD_SA5,	"HP Smart Array E500" },
319    { 0x103C, 0x3238, CISS_BOARD_SA5,	"HP Smart Array" },
320    { 0x103C, 0x3239, CISS_BOARD_SA5,	"HP Smart Array" },
321    { 0x103C, 0x323A, CISS_BOARD_SA5,	"HP Smart Array" },
322    { 0x103C, 0x323B, CISS_BOARD_SA5,	"HP Smart Array" },
323    { 0x103C, 0x323C, CISS_BOARD_SA5,	"HP Smart Array" },
324    { 0x103C, 0x323D, CISS_BOARD_SA5,	"HP Smart Array P700m" },
325    { 0x103C, 0x3241, CISS_BOARD_SA5,	"HP Smart Array P212" },
326    { 0x103C, 0x3243, CISS_BOARD_SA5,	"HP Smart Array P410" },
327    { 0x103C, 0x3245, CISS_BOARD_SA5,	"HP Smart Array P410i" },
328    { 0x103C, 0x3247, CISS_BOARD_SA5,	"HP Smart Array P411" },
329    { 0x103C, 0x3249, CISS_BOARD_SA5,	"HP Smart Array P812" },
330    { 0x103C, 0x324A, CISS_BOARD_SA5,	"HP Smart Array P712m" },
331    { 0x103C, 0x324B, CISS_BOARD_SA5,	"HP Smart Array" },
332    { 0x103C, 0x3350, CISS_BOARD_SA5,   "HP Smart Array P222" },
333    { 0x103C, 0x3351, CISS_BOARD_SA5,   "HP Smart Array P420" },
334    { 0x103C, 0x3352, CISS_BOARD_SA5,   "HP Smart Array P421" },
335    { 0x103C, 0x3353, CISS_BOARD_SA5,   "HP Smart Array P822" },
336    { 0x103C, 0x3354, CISS_BOARD_SA5,   "HP Smart Array P420i" },
337    { 0x103C, 0x3355, CISS_BOARD_SA5,   "HP Smart Array P220i" },
338    { 0x103C, 0x3356, CISS_BOARD_SA5,   "HP Smart Array P721m" },
339    { 0x103C, 0x1920, CISS_BOARD_SA5,   "HP Smart Array P430i" },
340    { 0x103C, 0x1921, CISS_BOARD_SA5,   "HP Smart Array P830i" },
341    { 0x103C, 0x1922, CISS_BOARD_SA5,   "HP Smart Array P430" },
342    { 0x103C, 0x1923, CISS_BOARD_SA5,   "HP Smart Array P431" },
343    { 0x103C, 0x1924, CISS_BOARD_SA5,   "HP Smart Array P830" },
344    { 0x103C, 0x1926, CISS_BOARD_SA5,   "HP Smart Array P731m" },
345    { 0x103C, 0x1928, CISS_BOARD_SA5,   "HP Smart Array P230i" },
346    { 0x103C, 0x1929, CISS_BOARD_SA5,   "HP Smart Array P530" },
347    { 0x103C, 0x192A, CISS_BOARD_SA5,   "HP Smart Array P531" },
348    { 0, 0, 0, NULL }
349};
350
351/************************************************************************
352 * Find a match for the device in our list of known adapters.
353 */
354static int
355ciss_lookup(device_t dev)
356{
357    int 	i;
358
359    for (i = 0; ciss_vendor_data[i].desc != NULL; i++)
360	if ((pci_get_subvendor(dev) == ciss_vendor_data[i].subvendor) &&
361	    (pci_get_subdevice(dev) == ciss_vendor_data[i].subdevice)) {
362	    return(i);
363	}
364    return(-1);
365}
366
367/************************************************************************
368 * Match a known CISS adapter.
369 */
370static int
371ciss_probe(device_t dev)
372{
373    int		i;
374
375    i = ciss_lookup(dev);
376    if (i != -1) {
377	device_set_desc(dev, ciss_vendor_data[i].desc);
378	return(BUS_PROBE_DEFAULT);
379    }
380    return(ENOENT);
381}
382
383/************************************************************************
384 * Attach the driver to this adapter.
385 */
386static int
387ciss_attach(device_t dev)
388{
389    struct ciss_softc	*sc;
390    int			error;
391
392    debug_called(1);
393
394#ifdef CISS_DEBUG
395    /* print structure/union sizes */
396    debug_struct(ciss_command);
397    debug_struct(ciss_header);
398    debug_union(ciss_device_address);
399    debug_struct(ciss_cdb);
400    debug_struct(ciss_report_cdb);
401    debug_struct(ciss_notify_cdb);
402    debug_struct(ciss_notify);
403    debug_struct(ciss_message_cdb);
404    debug_struct(ciss_error_info_pointer);
405    debug_struct(ciss_error_info);
406    debug_struct(ciss_sg_entry);
407    debug_struct(ciss_config_table);
408    debug_struct(ciss_bmic_cdb);
409    debug_struct(ciss_bmic_id_ldrive);
410    debug_struct(ciss_bmic_id_lstatus);
411    debug_struct(ciss_bmic_id_table);
412    debug_struct(ciss_bmic_id_pdrive);
413    debug_struct(ciss_bmic_blink_pdrive);
414    debug_struct(ciss_bmic_flush_cache);
415    debug_const(CISS_MAX_REQUESTS);
416    debug_const(CISS_MAX_LOGICAL);
417    debug_const(CISS_INTERRUPT_COALESCE_DELAY);
418    debug_const(CISS_INTERRUPT_COALESCE_COUNT);
419    debug_const(CISS_COMMAND_ALLOC_SIZE);
420    debug_const(CISS_COMMAND_SG_LENGTH);
421
422    debug_type(cciss_pci_info_struct);
423    debug_type(cciss_coalint_struct);
424    debug_type(cciss_coalint_struct);
425    debug_type(NodeName_type);
426    debug_type(NodeName_type);
427    debug_type(Heartbeat_type);
428    debug_type(BusTypes_type);
429    debug_type(FirmwareVer_type);
430    debug_type(DriverVer_type);
431    debug_type(IOCTL_Command_struct);
432#endif
433
434    sc = device_get_softc(dev);
435    sc->ciss_dev = dev;
436    mtx_init(&sc->ciss_mtx, "cissmtx", NULL, MTX_DEF);
437    callout_init_mtx(&sc->ciss_periodic, &sc->ciss_mtx, 0);
438
439    /*
440     * Do PCI-specific init.
441     */
442    if ((error = ciss_init_pci(sc)) != 0)
443	goto out;
444
445    /*
446     * Initialise driver queues.
447     */
448    ciss_initq_free(sc);
449    ciss_initq_notify(sc);
450
451    /*
452     * Initalize device sysctls.
453     */
454    ciss_init_sysctl(sc);
455
456    /*
457     * Initialise command/request pool.
458     */
459    if ((error = ciss_init_requests(sc)) != 0)
460	goto out;
461
462    /*
463     * Get adapter information.
464     */
465    if ((error = ciss_identify_adapter(sc)) != 0)
466	goto out;
467
468    /*
469     * Find all the physical devices.
470     */
471    if ((error = ciss_init_physical(sc)) != 0)
472	goto out;
473
474    /*
475     * Build our private table of logical devices.
476     */
477    if ((error = ciss_init_logical(sc)) != 0)
478	goto out;
479
480    /*
481     * Enable interrupts so that the CAM scan can complete.
482     */
483    CISS_TL_SIMPLE_ENABLE_INTERRUPTS(sc);
484
485    /*
486     * Initialise the CAM interface.
487     */
488    if ((error = ciss_cam_init(sc)) != 0)
489	goto out;
490
491    /*
492     * Start the heartbeat routine and event chain.
493     */
494    ciss_periodic(sc);
495
496   /*
497     * Create the control device.
498     */
499    sc->ciss_dev_t = make_dev(&ciss_cdevsw, device_get_unit(sc->ciss_dev),
500			      UID_ROOT, GID_OPERATOR, S_IRUSR | S_IWUSR,
501			      "ciss%d", device_get_unit(sc->ciss_dev));
502    sc->ciss_dev_t->si_drv1 = sc;
503
504    /*
505     * The adapter is running; synchronous commands can now sleep
506     * waiting for an interrupt to signal completion.
507     */
508    sc->ciss_flags |= CISS_FLAG_RUNNING;
509
510    ciss_spawn_notify_thread(sc);
511
512    error = 0;
513 out:
514    if (error != 0) {
515	/* ciss_free() expects the mutex to be held */
516	mtx_lock(&sc->ciss_mtx);
517	ciss_free(sc);
518    }
519    return(error);
520}
521
522/************************************************************************
523 * Detach the driver from this adapter.
524 */
525static int
526ciss_detach(device_t dev)
527{
528    struct ciss_softc	*sc = device_get_softc(dev);
529
530    debug_called(1);
531
532    mtx_lock(&sc->ciss_mtx);
533    if (sc->ciss_flags & CISS_FLAG_CONTROL_OPEN) {
534	mtx_unlock(&sc->ciss_mtx);
535	return (EBUSY);
536    }
537
538    /* flush adapter cache */
539    ciss_flush_adapter(sc);
540
541    /* release all resources.  The mutex is released and freed here too. */
542    ciss_free(sc);
543
544    return(0);
545}
546
547/************************************************************************
548 * Prepare adapter for system shutdown.
549 */
550static int
551ciss_shutdown(device_t dev)
552{
553    struct ciss_softc	*sc = device_get_softc(dev);
554
555    debug_called(1);
556
557    mtx_lock(&sc->ciss_mtx);
558    /* flush adapter cache */
559    ciss_flush_adapter(sc);
560
561    if (sc->ciss_soft_reset)
562	ciss_soft_reset(sc);
563    mtx_unlock(&sc->ciss_mtx);
564
565    return(0);
566}
567
568static void
569ciss_init_sysctl(struct ciss_softc *sc)
570{
571
572    SYSCTL_ADD_INT(device_get_sysctl_ctx(sc->ciss_dev),
573	SYSCTL_CHILDREN(device_get_sysctl_tree(sc->ciss_dev)),
574	OID_AUTO, "soft_reset", CTLFLAG_RW, &sc->ciss_soft_reset, 0, "");
575}
576
577/************************************************************************
578 * Perform PCI-specific attachment actions.
579 */
580static int
581ciss_init_pci(struct ciss_softc *sc)
582{
583    uintptr_t		cbase, csize, cofs;
584    uint32_t		method, supported_methods;
585    int			error, sqmask, i;
586    void		*intr;
587
588    debug_called(1);
589
590    /*
591     * Work out adapter type.
592     */
593    i = ciss_lookup(sc->ciss_dev);
594    if (i < 0) {
595	ciss_printf(sc, "unknown adapter type\n");
596	return (ENXIO);
597    }
598
599    if (ciss_vendor_data[i].flags & CISS_BOARD_SA5) {
600	sqmask = CISS_TL_SIMPLE_INTR_OPQ_SA5;
601    } else if (ciss_vendor_data[i].flags & CISS_BOARD_SA5B) {
602	sqmask = CISS_TL_SIMPLE_INTR_OPQ_SA5B;
603    } else {
604	/*
605	 * XXX Big hammer, masks/unmasks all possible interrupts.  This should
606	 * work on all hardware variants.  Need to add code to handle the
607	 * "controller crashed" interupt bit that this unmasks.
608	 */
609	sqmask = ~0;
610    }
611
612    /*
613     * Allocate register window first (we need this to find the config
614     * struct).
615     */
616    error = ENXIO;
617    sc->ciss_regs_rid = CISS_TL_SIMPLE_BAR_REGS;
618    if ((sc->ciss_regs_resource =
619	 bus_alloc_resource_any(sc->ciss_dev, SYS_RES_MEMORY,
620				&sc->ciss_regs_rid, RF_ACTIVE)) == NULL) {
621	ciss_printf(sc, "can't allocate register window\n");
622	return(ENXIO);
623    }
624    sc->ciss_regs_bhandle = rman_get_bushandle(sc->ciss_regs_resource);
625    sc->ciss_regs_btag = rman_get_bustag(sc->ciss_regs_resource);
626
627    /*
628     * Find the BAR holding the config structure.  If it's not the one
629     * we already mapped for registers, map it too.
630     */
631    sc->ciss_cfg_rid = CISS_TL_SIMPLE_READ(sc, CISS_TL_SIMPLE_CFG_BAR) & 0xffff;
632    if (sc->ciss_cfg_rid != sc->ciss_regs_rid) {
633	if ((sc->ciss_cfg_resource =
634	     bus_alloc_resource_any(sc->ciss_dev, SYS_RES_MEMORY,
635				    &sc->ciss_cfg_rid, RF_ACTIVE)) == NULL) {
636	    ciss_printf(sc, "can't allocate config window\n");
637	    return(ENXIO);
638	}
639	cbase = (uintptr_t)rman_get_virtual(sc->ciss_cfg_resource);
640	csize = rman_get_end(sc->ciss_cfg_resource) -
641	    rman_get_start(sc->ciss_cfg_resource) + 1;
642    } else {
643	cbase = (uintptr_t)rman_get_virtual(sc->ciss_regs_resource);
644	csize = rman_get_end(sc->ciss_regs_resource) -
645	    rman_get_start(sc->ciss_regs_resource) + 1;
646    }
647    cofs = CISS_TL_SIMPLE_READ(sc, CISS_TL_SIMPLE_CFG_OFF);
648
649    /*
650     * Use the base/size/offset values we just calculated to
651     * sanity-check the config structure.  If it's OK, point to it.
652     */
653    if ((cofs + sizeof(struct ciss_config_table)) > csize) {
654	ciss_printf(sc, "config table outside window\n");
655	return(ENXIO);
656    }
657    sc->ciss_cfg = (struct ciss_config_table *)(cbase + cofs);
658    debug(1, "config struct at %p", sc->ciss_cfg);
659
660    /*
661     * Calculate the number of request structures/commands we are
662     * going to provide for this adapter.
663     */
664    sc->ciss_max_requests = min(CISS_MAX_REQUESTS, sc->ciss_cfg->max_outstanding_commands);
665
666    /*
667     * Validate the config structure.  If we supported other transport
668     * methods, we could select amongst them at this point in time.
669     */
670    if (strncmp(sc->ciss_cfg->signature, "CISS", 4)) {
671	ciss_printf(sc, "config signature mismatch (got '%c%c%c%c')\n",
672		    sc->ciss_cfg->signature[0], sc->ciss_cfg->signature[1],
673		    sc->ciss_cfg->signature[2], sc->ciss_cfg->signature[3]);
674	return(ENXIO);
675    }
676
677    /*
678     * Select the mode of operation, prefer Performant.
679     */
680    if (!(sc->ciss_cfg->supported_methods &
681	(CISS_TRANSPORT_METHOD_SIMPLE | CISS_TRANSPORT_METHOD_PERF))) {
682	ciss_printf(sc, "No supported transport layers: 0x%x\n",
683	    sc->ciss_cfg->supported_methods);
684    }
685
686    switch (ciss_force_transport) {
687    case 1:
688	supported_methods = CISS_TRANSPORT_METHOD_SIMPLE;
689	break;
690    case 2:
691	supported_methods = CISS_TRANSPORT_METHOD_PERF;
692	break;
693    default:
694        /*
695         * Override the capabilities of the BOARD and specify SIMPLE
696         * MODE
697         */
698        if (ciss_vendor_data[i].flags & CISS_BOARD_SIMPLE)
699                supported_methods = CISS_TRANSPORT_METHOD_SIMPLE;
700        else
701                supported_methods = sc->ciss_cfg->supported_methods;
702        break;
703    }
704
705setup:
706    if ((supported_methods & CISS_TRANSPORT_METHOD_PERF) != 0) {
707	method = CISS_TRANSPORT_METHOD_PERF;
708	sc->ciss_perf = (struct ciss_perf_config *)(cbase + cofs +
709	    sc->ciss_cfg->transport_offset);
710	if (ciss_init_perf(sc)) {
711	    supported_methods &= ~method;
712	    goto setup;
713	}
714    } else if (supported_methods & CISS_TRANSPORT_METHOD_SIMPLE) {
715	method = CISS_TRANSPORT_METHOD_SIMPLE;
716    } else {
717	ciss_printf(sc, "No supported transport methods: 0x%x\n",
718	    sc->ciss_cfg->supported_methods);
719	return(ENXIO);
720    }
721
722    /*
723     * Tell it we're using the low 4GB of RAM.  Set the default interrupt
724     * coalescing options.
725     */
726    sc->ciss_cfg->requested_method = method;
727    sc->ciss_cfg->command_physlimit = 0;
728    sc->ciss_cfg->interrupt_coalesce_delay = CISS_INTERRUPT_COALESCE_DELAY;
729    sc->ciss_cfg->interrupt_coalesce_count = CISS_INTERRUPT_COALESCE_COUNT;
730
731#ifdef __i386__
732    sc->ciss_cfg->host_driver |= CISS_DRIVER_SCSI_PREFETCH;
733#endif
734
735    if (ciss_update_config(sc)) {
736	ciss_printf(sc, "adapter refuses to accept config update (IDBR 0x%x)\n",
737		    CISS_TL_SIMPLE_READ(sc, CISS_TL_SIMPLE_IDBR));
738	return(ENXIO);
739    }
740    if ((sc->ciss_cfg->active_method & method) == 0) {
741	supported_methods &= ~method;
742	if (supported_methods == 0) {
743	    ciss_printf(sc, "adapter refuses to go into available transports "
744		"mode (0x%x, 0x%x)\n", supported_methods,
745		sc->ciss_cfg->active_method);
746	    return(ENXIO);
747	} else
748	    goto setup;
749    }
750
751    /*
752     * Wait for the adapter to come ready.
753     */
754    if ((error = ciss_wait_adapter(sc)) != 0)
755	return(error);
756
757    /* Prepare to possibly use MSIX and/or PERFORMANT interrupts.  Normal
758     * interrupts have a rid of 0, this will be overridden if MSIX is used.
759     */
760    sc->ciss_irq_rid[0] = 0;
761    if (method == CISS_TRANSPORT_METHOD_PERF) {
762	ciss_printf(sc, "PERFORMANT Transport\n");
763	if ((ciss_force_interrupt != 1) && (ciss_setup_msix(sc) == 0)) {
764	    intr = ciss_perf_msi_intr;
765	} else {
766	    intr = ciss_perf_intr;
767	}
768	/* XXX The docs say that the 0x01 bit is only for SAS controllers.
769	 * Unfortunately, there is no good way to know if this is a SAS
770	 * controller.  Hopefully enabling this bit universally will work OK.
771	 * It seems to work fine for SA6i controllers.
772	 */
773	sc->ciss_interrupt_mask = CISS_TL_PERF_INTR_OPQ | CISS_TL_PERF_INTR_MSI;
774
775    } else {
776	ciss_printf(sc, "SIMPLE Transport\n");
777	/* MSIX doesn't seem to work in SIMPLE mode, only enable if it forced */
778	if (ciss_force_interrupt == 2)
779	    /* If this fails, we automatically revert to INTx */
780	    ciss_setup_msix(sc);
781	sc->ciss_perf = NULL;
782	intr = ciss_intr;
783	sc->ciss_interrupt_mask = sqmask;
784    }
785
786    /*
787     * Turn off interrupts before we go routing anything.
788     */
789    CISS_TL_SIMPLE_DISABLE_INTERRUPTS(sc);
790
791    /*
792     * Allocate and set up our interrupt.
793     */
794    if ((sc->ciss_irq_resource =
795	 bus_alloc_resource_any(sc->ciss_dev, SYS_RES_IRQ, &sc->ciss_irq_rid[0],
796				RF_ACTIVE | RF_SHAREABLE)) == NULL) {
797	ciss_printf(sc, "can't allocate interrupt\n");
798	return(ENXIO);
799    }
800
801    if (bus_setup_intr(sc->ciss_dev, sc->ciss_irq_resource,
802		       INTR_TYPE_CAM|INTR_MPSAFE, NULL, intr, sc,
803		       &sc->ciss_intr)) {
804	ciss_printf(sc, "can't set up interrupt\n");
805	return(ENXIO);
806    }
807
808    /*
809     * Allocate the parent bus DMA tag appropriate for our PCI
810     * interface.
811     *
812     * Note that "simple" adapters can only address within a 32-bit
813     * span.
814     */
815    if (bus_dma_tag_create(bus_get_dma_tag(sc->ciss_dev),/* PCI parent */
816			   1, 0, 			/* alignment, boundary */
817			   BUS_SPACE_MAXADDR,		/* lowaddr */
818			   BUS_SPACE_MAXADDR, 		/* highaddr */
819			   NULL, NULL, 			/* filter, filterarg */
820			   BUS_SPACE_MAXSIZE_32BIT,	/* maxsize */
821			   CISS_MAX_SG_ELEMENTS,	/* nsegments */
822			   BUS_SPACE_MAXSIZE_32BIT,	/* maxsegsize */
823			   0,				/* flags */
824			   NULL, NULL,			/* lockfunc, lockarg */
825			   &sc->ciss_parent_dmat)) {
826	ciss_printf(sc, "can't allocate parent DMA tag\n");
827	return(ENOMEM);
828    }
829
830    /*
831     * Create DMA tag for mapping buffers into adapter-addressable
832     * space.
833     */
834    if (bus_dma_tag_create(sc->ciss_parent_dmat, 	/* parent */
835			   1, 0, 			/* alignment, boundary */
836			   BUS_SPACE_MAXADDR,		/* lowaddr */
837			   BUS_SPACE_MAXADDR, 		/* highaddr */
838			   NULL, NULL, 			/* filter, filterarg */
839			   MAXBSIZE, CISS_MAX_SG_ELEMENTS,	/* maxsize, nsegments */
840			   BUS_SPACE_MAXSIZE_32BIT,	/* maxsegsize */
841			   BUS_DMA_ALLOCNOW,		/* flags */
842			   busdma_lock_mutex, &sc->ciss_mtx,	/* lockfunc, lockarg */
843			   &sc->ciss_buffer_dmat)) {
844	ciss_printf(sc, "can't allocate buffer DMA tag\n");
845	return(ENOMEM);
846    }
847    return(0);
848}
849
850/************************************************************************
851 * Setup MSI/MSIX operation (Performant only)
852 * Four interrupts are available, but we only use 1 right now.  If MSI-X
853 * isn't avaialble, try using MSI instead.
854 */
855static int
856ciss_setup_msix(struct ciss_softc *sc)
857{
858    int val, i;
859
860    /* Weed out devices that don't actually support MSI */
861    i = ciss_lookup(sc->ciss_dev);
862    if (ciss_vendor_data[i].flags & CISS_BOARD_NOMSI)
863	return (EINVAL);
864
865    /*
866     * Only need to use the minimum number of MSI vectors, as the driver
867     * doesn't support directed MSIX interrupts.
868     */
869    val = pci_msix_count(sc->ciss_dev);
870    if (val < CISS_MSI_COUNT) {
871	val = pci_msi_count(sc->ciss_dev);
872	device_printf(sc->ciss_dev, "got %d MSI messages]\n", val);
873	if (val < CISS_MSI_COUNT)
874	    return (EINVAL);
875    }
876    val = MIN(val, CISS_MSI_COUNT);
877    if (pci_alloc_msix(sc->ciss_dev, &val) != 0) {
878	if (pci_alloc_msi(sc->ciss_dev, &val) != 0)
879	    return (EINVAL);
880    }
881
882    sc->ciss_msi = val;
883    if (bootverbose)
884	ciss_printf(sc, "Using %d MSIX interrupt%s\n", val,
885	    (val != 1) ? "s" : "");
886
887    for (i = 0; i < val; i++)
888	sc->ciss_irq_rid[i] = i + 1;
889
890    return (0);
891
892}
893
894/************************************************************************
895 * Setup the Performant structures.
896 */
897static int
898ciss_init_perf(struct ciss_softc *sc)
899{
900    struct ciss_perf_config *pc = sc->ciss_perf;
901    int reply_size;
902
903    /*
904     * Create the DMA tag for the reply queue.
905     */
906    reply_size = sizeof(uint64_t) * sc->ciss_max_requests;
907    if (bus_dma_tag_create(sc->ciss_parent_dmat,	/* parent */
908			   1, 0, 			/* alignment, boundary */
909			   BUS_SPACE_MAXADDR_32BIT,	/* lowaddr */
910			   BUS_SPACE_MAXADDR, 		/* highaddr */
911			   NULL, NULL, 			/* filter, filterarg */
912			   reply_size, 1,		/* maxsize, nsegments */
913			   BUS_SPACE_MAXSIZE_32BIT,	/* maxsegsize */
914			   0,				/* flags */
915			   NULL, NULL,			/* lockfunc, lockarg */
916			   &sc->ciss_reply_dmat)) {
917	ciss_printf(sc, "can't allocate reply DMA tag\n");
918	return(ENOMEM);
919    }
920    /*
921     * Allocate memory and make it available for DMA.
922     */
923    if (bus_dmamem_alloc(sc->ciss_reply_dmat, (void **)&sc->ciss_reply,
924			 BUS_DMA_NOWAIT, &sc->ciss_reply_map)) {
925	ciss_printf(sc, "can't allocate reply memory\n");
926	return(ENOMEM);
927    }
928    bus_dmamap_load(sc->ciss_reply_dmat, sc->ciss_reply_map, sc->ciss_reply,
929		    reply_size, ciss_command_map_helper, &sc->ciss_reply_phys, 0);
930    bzero(sc->ciss_reply, reply_size);
931
932    sc->ciss_cycle = 0x1;
933    sc->ciss_rqidx = 0;
934
935    /*
936     * Preload the fetch table with common command sizes.  This allows the
937     * hardware to not waste bus cycles for typical i/o commands, but also not
938     * tax the driver to be too exact in choosing sizes.  The table is optimized
939     * for page-aligned i/o's, but since most i/o comes from the various pagers,
940     * it's a reasonable assumption to make.
941     */
942    pc->fetch_count[CISS_SG_FETCH_NONE] = (sizeof(struct ciss_command) + 15) / 16;
943    pc->fetch_count[CISS_SG_FETCH_1] =
944	(sizeof(struct ciss_command) + sizeof(struct ciss_sg_entry) * 1 + 15) / 16;
945    pc->fetch_count[CISS_SG_FETCH_2] =
946	(sizeof(struct ciss_command) + sizeof(struct ciss_sg_entry) * 2 + 15) / 16;
947    pc->fetch_count[CISS_SG_FETCH_4] =
948	(sizeof(struct ciss_command) + sizeof(struct ciss_sg_entry) * 4 + 15) / 16;
949    pc->fetch_count[CISS_SG_FETCH_8] =
950	(sizeof(struct ciss_command) + sizeof(struct ciss_sg_entry) * 8 + 15) / 16;
951    pc->fetch_count[CISS_SG_FETCH_16] =
952	(sizeof(struct ciss_command) + sizeof(struct ciss_sg_entry) * 16 + 15) / 16;
953    pc->fetch_count[CISS_SG_FETCH_32] =
954	(sizeof(struct ciss_command) + sizeof(struct ciss_sg_entry) * 32 + 15) / 16;
955    pc->fetch_count[CISS_SG_FETCH_MAX] = (CISS_COMMAND_ALLOC_SIZE + 15) / 16;
956
957    pc->rq_size = sc->ciss_max_requests; /* XXX less than the card supports? */
958    pc->rq_count = 1;	/* XXX Hardcode for a single queue */
959    pc->rq_bank_hi = 0;
960    pc->rq_bank_lo = 0;
961    pc->rq[0].rq_addr_hi = 0x0;
962    pc->rq[0].rq_addr_lo = sc->ciss_reply_phys;
963
964    return(0);
965}
966
967/************************************************************************
968 * Wait for the adapter to come ready.
969 */
970static int
971ciss_wait_adapter(struct ciss_softc *sc)
972{
973    int		i;
974
975    debug_called(1);
976
977    /*
978     * Wait for the adapter to come ready.
979     */
980    if (!(sc->ciss_cfg->active_method & CISS_TRANSPORT_METHOD_READY)) {
981	ciss_printf(sc, "waiting for adapter to come ready...\n");
982	for (i = 0; !(sc->ciss_cfg->active_method & CISS_TRANSPORT_METHOD_READY); i++) {
983	    DELAY(1000000);	/* one second */
984	    if (i > 30) {
985		ciss_printf(sc, "timed out waiting for adapter to come ready\n");
986		return(EIO);
987	    }
988	}
989    }
990    return(0);
991}
992
993/************************************************************************
994 * Flush the adapter cache.
995 */
996static int
997ciss_flush_adapter(struct ciss_softc *sc)
998{
999    struct ciss_request			*cr;
1000    struct ciss_bmic_flush_cache	*cbfc;
1001    int					error, command_status;
1002
1003    debug_called(1);
1004
1005    cr = NULL;
1006    cbfc = NULL;
1007
1008    /*
1009     * Build a BMIC request to flush the cache.  We don't disable
1010     * it, as we may be going to do more I/O (eg. we are emulating
1011     * the Synchronise Cache command).
1012     */
1013    if ((cbfc = malloc(sizeof(*cbfc), CISS_MALLOC_CLASS, M_NOWAIT | M_ZERO)) == NULL) {
1014	error = ENOMEM;
1015	goto out;
1016    }
1017    if ((error = ciss_get_bmic_request(sc, &cr, CISS_BMIC_FLUSH_CACHE,
1018				       (void **)&cbfc, sizeof(*cbfc))) != 0)
1019	goto out;
1020
1021    /*
1022     * Submit the request and wait for it to complete.
1023     */
1024    if ((error = ciss_synch_request(cr, 60 * 1000)) != 0) {
1025	ciss_printf(sc, "error sending BMIC FLUSH_CACHE command (%d)\n", error);
1026	goto out;
1027    }
1028
1029    /*
1030     * Check response.
1031     */
1032    ciss_report_request(cr, &command_status, NULL);
1033    switch(command_status) {
1034    case CISS_CMD_STATUS_SUCCESS:
1035	break;
1036    default:
1037	ciss_printf(sc, "error flushing cache (%s)\n",
1038		    ciss_name_command_status(command_status));
1039	error = EIO;
1040	goto out;
1041    }
1042
1043out:
1044    if (cbfc != NULL)
1045	free(cbfc, CISS_MALLOC_CLASS);
1046    if (cr != NULL)
1047	ciss_release_request(cr);
1048    return(error);
1049}
1050
1051static void
1052ciss_soft_reset(struct ciss_softc *sc)
1053{
1054    struct ciss_request		*cr = NULL;
1055    struct ciss_command		*cc;
1056    int				i, error = 0;
1057
1058    for (i = 0; i < sc->ciss_max_logical_bus; i++) {
1059	/* only reset proxy controllers */
1060	if (sc->ciss_controllers[i].physical.bus == 0)
1061	    continue;
1062
1063	if ((error = ciss_get_request(sc, &cr)) != 0)
1064	    break;
1065
1066	if ((error = ciss_get_bmic_request(sc, &cr, CISS_BMIC_SOFT_RESET,
1067					   NULL, 0)) != 0)
1068	    break;
1069
1070	cc = cr->cr_cc;
1071	cc->header.address = sc->ciss_controllers[i];
1072
1073	if ((error = ciss_synch_request(cr, 60 * 1000)) != 0)
1074	    break;
1075
1076	ciss_release_request(cr);
1077    }
1078
1079    if (error)
1080	ciss_printf(sc, "error resetting controller (%d)\n", error);
1081
1082    if (cr != NULL)
1083	ciss_release_request(cr);
1084}
1085
1086/************************************************************************
1087 * Allocate memory for the adapter command structures, initialise
1088 * the request structures.
1089 *
1090 * Note that the entire set of commands are allocated in a single
1091 * contiguous slab.
1092 */
1093static int
1094ciss_init_requests(struct ciss_softc *sc)
1095{
1096    struct ciss_request	*cr;
1097    int			i;
1098
1099    debug_called(1);
1100
1101    if (bootverbose)
1102	ciss_printf(sc, "using %d of %d available commands\n",
1103		    sc->ciss_max_requests, sc->ciss_cfg->max_outstanding_commands);
1104
1105    /*
1106     * Create the DMA tag for commands.
1107     */
1108    if (bus_dma_tag_create(sc->ciss_parent_dmat,	/* parent */
1109			   32, 0, 			/* alignment, boundary */
1110			   BUS_SPACE_MAXADDR_32BIT,	/* lowaddr */
1111			   BUS_SPACE_MAXADDR, 		/* highaddr */
1112			   NULL, NULL, 			/* filter, filterarg */
1113			   CISS_COMMAND_ALLOC_SIZE *
1114			   sc->ciss_max_requests, 1,	/* maxsize, nsegments */
1115			   BUS_SPACE_MAXSIZE_32BIT,	/* maxsegsize */
1116			   0,				/* flags */
1117			   NULL, NULL,			/* lockfunc, lockarg */
1118			   &sc->ciss_command_dmat)) {
1119	ciss_printf(sc, "can't allocate command DMA tag\n");
1120	return(ENOMEM);
1121    }
1122    /*
1123     * Allocate memory and make it available for DMA.
1124     */
1125    if (bus_dmamem_alloc(sc->ciss_command_dmat, (void **)&sc->ciss_command,
1126			 BUS_DMA_NOWAIT, &sc->ciss_command_map)) {
1127	ciss_printf(sc, "can't allocate command memory\n");
1128	return(ENOMEM);
1129    }
1130    bus_dmamap_load(sc->ciss_command_dmat, sc->ciss_command_map,sc->ciss_command,
1131		    CISS_COMMAND_ALLOC_SIZE * sc->ciss_max_requests,
1132		    ciss_command_map_helper, &sc->ciss_command_phys, 0);
1133    bzero(sc->ciss_command, CISS_COMMAND_ALLOC_SIZE * sc->ciss_max_requests);
1134
1135    /*
1136     * Set up the request and command structures, push requests onto
1137     * the free queue.
1138     */
1139    for (i = 1; i < sc->ciss_max_requests; i++) {
1140	cr = &sc->ciss_request[i];
1141	cr->cr_sc = sc;
1142	cr->cr_tag = i;
1143	cr->cr_cc = (struct ciss_command *)((uintptr_t)sc->ciss_command +
1144	    CISS_COMMAND_ALLOC_SIZE * i);
1145	cr->cr_ccphys = sc->ciss_command_phys + CISS_COMMAND_ALLOC_SIZE * i;
1146	bus_dmamap_create(sc->ciss_buffer_dmat, 0, &cr->cr_datamap);
1147	ciss_enqueue_free(cr);
1148    }
1149    return(0);
1150}
1151
1152static void
1153ciss_command_map_helper(void *arg, bus_dma_segment_t *segs, int nseg, int error)
1154{
1155    uint32_t *addr;
1156
1157    addr = arg;
1158    *addr = segs[0].ds_addr;
1159}
1160
1161/************************************************************************
1162 * Identify the adapter, print some information about it.
1163 */
1164static int
1165ciss_identify_adapter(struct ciss_softc *sc)
1166{
1167    struct ciss_request	*cr;
1168    int			error, command_status;
1169
1170    debug_called(1);
1171
1172    cr = NULL;
1173
1174    /*
1175     * Get a request, allocate storage for the adapter data.
1176     */
1177    if ((error = ciss_get_bmic_request(sc, &cr, CISS_BMIC_ID_CTLR,
1178				       (void **)&sc->ciss_id,
1179				       sizeof(*sc->ciss_id))) != 0)
1180	goto out;
1181
1182    /*
1183     * Submit the request and wait for it to complete.
1184     */
1185    if ((error = ciss_synch_request(cr, 60 * 1000)) != 0) {
1186	ciss_printf(sc, "error sending BMIC ID_CTLR command (%d)\n", error);
1187	goto out;
1188    }
1189
1190    /*
1191     * Check response.
1192     */
1193    ciss_report_request(cr, &command_status, NULL);
1194    switch(command_status) {
1195    case CISS_CMD_STATUS_SUCCESS:		/* buffer right size */
1196	break;
1197    case CISS_CMD_STATUS_DATA_UNDERRUN:
1198    case CISS_CMD_STATUS_DATA_OVERRUN:
1199	ciss_printf(sc, "data over/underrun reading adapter information\n");
1200    default:
1201	ciss_printf(sc, "error reading adapter information (%s)\n",
1202		    ciss_name_command_status(command_status));
1203	error = EIO;
1204	goto out;
1205    }
1206
1207    /* sanity-check reply */
1208    if (!sc->ciss_id->big_map_supported) {
1209	ciss_printf(sc, "adapter does not support BIG_MAP\n");
1210	error = ENXIO;
1211	goto out;
1212    }
1213
1214#if 0
1215    /* XXX later revisions may not need this */
1216    sc->ciss_flags |= CISS_FLAG_FAKE_SYNCH;
1217#endif
1218
1219    /* XXX only really required for old 5300 adapters? */
1220    sc->ciss_flags |= CISS_FLAG_BMIC_ABORT;
1221
1222    /*
1223     * Earlier controller specs do not contain these config
1224     * entries, so assume that a 0 means its old and assign
1225     * these values to the defaults that were established
1226     * when this driver was developed for them
1227     */
1228    if (sc->ciss_cfg->max_logical_supported == 0)
1229        sc->ciss_cfg->max_logical_supported = CISS_MAX_LOGICAL;
1230    if (sc->ciss_cfg->max_physical_supported == 0)
1231	sc->ciss_cfg->max_physical_supported = CISS_MAX_PHYSICAL;
1232    /* print information */
1233    if (bootverbose) {
1234	ciss_printf(sc, "  %d logical drive%s configured\n",
1235		    sc->ciss_id->configured_logical_drives,
1236		    (sc->ciss_id->configured_logical_drives == 1) ? "" : "s");
1237	ciss_printf(sc, "  firmware %4.4s\n", sc->ciss_id->running_firmware_revision);
1238	ciss_printf(sc, "  %d SCSI channels\n", sc->ciss_id->scsi_bus_count);
1239
1240	ciss_printf(sc, "  signature '%.4s'\n", sc->ciss_cfg->signature);
1241	ciss_printf(sc, "  valence %d\n", sc->ciss_cfg->valence);
1242	ciss_printf(sc, "  supported I/O methods 0x%b\n",
1243		    sc->ciss_cfg->supported_methods,
1244		    "\20\1READY\2simple\3performant\4MEMQ\n");
1245	ciss_printf(sc, "  active I/O method 0x%b\n",
1246		    sc->ciss_cfg->active_method, "\20\2simple\3performant\4MEMQ\n");
1247	ciss_printf(sc, "  4G page base 0x%08x\n",
1248		    sc->ciss_cfg->command_physlimit);
1249	ciss_printf(sc, "  interrupt coalesce delay %dus\n",
1250		    sc->ciss_cfg->interrupt_coalesce_delay);
1251	ciss_printf(sc, "  interrupt coalesce count %d\n",
1252		    sc->ciss_cfg->interrupt_coalesce_count);
1253	ciss_printf(sc, "  max outstanding commands %d\n",
1254		    sc->ciss_cfg->max_outstanding_commands);
1255	ciss_printf(sc, "  bus types 0x%b\n", sc->ciss_cfg->bus_types,
1256		    "\20\1ultra2\2ultra3\10fibre1\11fibre2\n");
1257	ciss_printf(sc, "  server name '%.16s'\n", sc->ciss_cfg->server_name);
1258	ciss_printf(sc, "  heartbeat 0x%x\n", sc->ciss_cfg->heartbeat);
1259    	ciss_printf(sc, "  max logical logical volumes: %d\n", sc->ciss_cfg->max_logical_supported);
1260    	ciss_printf(sc, "  max physical disks supported: %d\n", sc->ciss_cfg->max_physical_supported);
1261    	ciss_printf(sc, "  max physical disks per logical volume: %d\n", sc->ciss_cfg->max_physical_per_logical);
1262    }
1263
1264out:
1265    if (error) {
1266	if (sc->ciss_id != NULL) {
1267	    free(sc->ciss_id, CISS_MALLOC_CLASS);
1268	    sc->ciss_id = NULL;
1269	}
1270    }
1271    if (cr != NULL)
1272	ciss_release_request(cr);
1273    return(error);
1274}
1275
1276/************************************************************************
1277 * Helper routine for generating a list of logical and physical luns.
1278 */
1279static struct ciss_lun_report *
1280ciss_report_luns(struct ciss_softc *sc, int opcode, int nunits)
1281{
1282    struct ciss_request		*cr;
1283    struct ciss_command		*cc;
1284    struct ciss_report_cdb	*crc;
1285    struct ciss_lun_report	*cll;
1286    int				command_status;
1287    int				report_size;
1288    int				error = 0;
1289
1290    debug_called(1);
1291
1292    cr = NULL;
1293    cll = NULL;
1294
1295    /*
1296     * Get a request, allocate storage for the address list.
1297     */
1298    if ((error = ciss_get_request(sc, &cr)) != 0)
1299	goto out;
1300    report_size = sizeof(*cll) + nunits * sizeof(union ciss_device_address);
1301    if ((cll = malloc(report_size, CISS_MALLOC_CLASS, M_NOWAIT | M_ZERO)) == NULL) {
1302	ciss_printf(sc, "can't allocate memory for lun report\n");
1303	error = ENOMEM;
1304	goto out;
1305    }
1306
1307    /*
1308     * Build the Report Logical/Physical LUNs command.
1309     */
1310    cc = cr->cr_cc;
1311    cr->cr_data = cll;
1312    cr->cr_length = report_size;
1313    cr->cr_flags = CISS_REQ_DATAIN;
1314
1315    cc->header.address.physical.mode = CISS_HDR_ADDRESS_MODE_PERIPHERAL;
1316    cc->header.address.physical.bus = 0;
1317    cc->header.address.physical.target = 0;
1318    cc->cdb.cdb_length = sizeof(*crc);
1319    cc->cdb.type = CISS_CDB_TYPE_COMMAND;
1320    cc->cdb.attribute = CISS_CDB_ATTRIBUTE_SIMPLE;
1321    cc->cdb.direction = CISS_CDB_DIRECTION_READ;
1322    cc->cdb.timeout = 30;	/* XXX better suggestions? */
1323
1324    crc = (struct ciss_report_cdb *)&(cc->cdb.cdb[0]);
1325    bzero(crc, sizeof(*crc));
1326    crc->opcode = opcode;
1327    crc->length = htonl(report_size);			/* big-endian field */
1328    cll->list_size = htonl(report_size - sizeof(*cll));	/* big-endian field */
1329
1330    /*
1331     * Submit the request and wait for it to complete.  (timeout
1332     * here should be much greater than above)
1333     */
1334    if ((error = ciss_synch_request(cr, 60 * 1000)) != 0) {
1335	ciss_printf(sc, "error sending %d LUN command (%d)\n", opcode, error);
1336	goto out;
1337    }
1338
1339    /*
1340     * Check response.  Note that data over/underrun is OK.
1341     */
1342    ciss_report_request(cr, &command_status, NULL);
1343    switch(command_status) {
1344    case CISS_CMD_STATUS_SUCCESS:	/* buffer right size */
1345    case CISS_CMD_STATUS_DATA_UNDERRUN:	/* buffer too large, not bad */
1346	break;
1347    case CISS_CMD_STATUS_DATA_OVERRUN:
1348	ciss_printf(sc, "WARNING: more units than driver limit (%d)\n",
1349		    sc->ciss_cfg->max_logical_supported);
1350	break;
1351    default:
1352	ciss_printf(sc, "error detecting logical drive configuration (%s)\n",
1353		    ciss_name_command_status(command_status));
1354	error = EIO;
1355	goto out;
1356    }
1357    ciss_release_request(cr);
1358    cr = NULL;
1359
1360out:
1361    if (cr != NULL)
1362	ciss_release_request(cr);
1363    if (error && cll != NULL) {
1364	free(cll, CISS_MALLOC_CLASS);
1365	cll = NULL;
1366    }
1367    return(cll);
1368}
1369
1370/************************************************************************
1371 * Find logical drives on the adapter.
1372 */
1373static int
1374ciss_init_logical(struct ciss_softc *sc)
1375{
1376    struct ciss_lun_report	*cll;
1377    int				error = 0, i, j;
1378    int				ndrives;
1379
1380    debug_called(1);
1381
1382    cll = ciss_report_luns(sc, CISS_OPCODE_REPORT_LOGICAL_LUNS,
1383			   sc->ciss_cfg->max_logical_supported);
1384    if (cll == NULL) {
1385	error = ENXIO;
1386	goto out;
1387    }
1388
1389    /* sanity-check reply */
1390    ndrives = (ntohl(cll->list_size) / sizeof(union ciss_device_address));
1391    if ((ndrives < 0) || (ndrives > sc->ciss_cfg->max_logical_supported)) {
1392	ciss_printf(sc, "adapter claims to report absurd number of logical drives (%d > %d)\n",
1393	    	ndrives, sc->ciss_cfg->max_logical_supported);
1394	error = ENXIO;
1395	goto out;
1396    }
1397
1398    /*
1399     * Save logical drive information.
1400     */
1401    if (bootverbose) {
1402	ciss_printf(sc, "%d logical drive%s\n",
1403	    ndrives, (ndrives > 1 || ndrives == 0) ? "s" : "");
1404    }
1405
1406    sc->ciss_logical =
1407	malloc(sc->ciss_max_logical_bus * sizeof(struct ciss_ldrive *),
1408	       CISS_MALLOC_CLASS, M_NOWAIT | M_ZERO);
1409    if (sc->ciss_logical == NULL) {
1410	error = ENXIO;
1411	goto out;
1412    }
1413
1414    for (i = 0; i <= sc->ciss_max_logical_bus; i++) {
1415	sc->ciss_logical[i] =
1416	    malloc(sc->ciss_cfg->max_logical_supported *
1417		   sizeof(struct ciss_ldrive),
1418		   CISS_MALLOC_CLASS, M_NOWAIT | M_ZERO);
1419	if (sc->ciss_logical[i] == NULL) {
1420	    error = ENXIO;
1421	    goto out;
1422	}
1423
1424	for (j = 0; j < sc->ciss_cfg->max_logical_supported; j++)
1425	    sc->ciss_logical[i][j].cl_status = CISS_LD_NONEXISTENT;
1426    }
1427
1428
1429    for (i = 0; i < sc->ciss_cfg->max_logical_supported; i++) {
1430	if (i < ndrives) {
1431	    struct ciss_ldrive	*ld;
1432	    int			bus, target;
1433
1434	    bus		= CISS_LUN_TO_BUS(cll->lun[i].logical.lun);
1435	    target	= CISS_LUN_TO_TARGET(cll->lun[i].logical.lun);
1436	    ld		= &sc->ciss_logical[bus][target];
1437
1438	    ld->cl_address	= cll->lun[i];
1439	    ld->cl_controller	= &sc->ciss_controllers[bus];
1440	    if (ciss_identify_logical(sc, ld) != 0)
1441		continue;
1442	    /*
1443	     * If the drive has had media exchanged, we should bring it online.
1444	     */
1445	    if (ld->cl_lstatus->media_exchanged)
1446		ciss_accept_media(sc, ld);
1447
1448	}
1449    }
1450
1451 out:
1452    if (cll != NULL)
1453	free(cll, CISS_MALLOC_CLASS);
1454    return(error);
1455}
1456
1457static int
1458ciss_init_physical(struct ciss_softc *sc)
1459{
1460    struct ciss_lun_report	*cll;
1461    int				error = 0, i;
1462    int				nphys;
1463    int				bus, target;
1464
1465    debug_called(1);
1466
1467    bus = 0;
1468    target = 0;
1469
1470    cll = ciss_report_luns(sc, CISS_OPCODE_REPORT_PHYSICAL_LUNS,
1471			   sc->ciss_cfg->max_physical_supported);
1472    if (cll == NULL) {
1473	error = ENXIO;
1474	goto out;
1475    }
1476
1477    nphys = (ntohl(cll->list_size) / sizeof(union ciss_device_address));
1478
1479    if (bootverbose) {
1480	ciss_printf(sc, "%d physical device%s\n",
1481	    nphys, (nphys > 1 || nphys == 0) ? "s" : "");
1482    }
1483
1484    /*
1485     * Figure out the bus mapping.
1486     * Logical buses include both the local logical bus for local arrays and
1487     * proxy buses for remote arrays.  Physical buses are numbered by the
1488     * controller and represent physical buses that hold physical devices.
1489     * We shift these bus numbers so that everything fits into a single flat
1490     * numbering space for CAM.  Logical buses occupy the first 32 CAM bus
1491     * numbers, and the physical bus numbers are shifted to be above that.
1492     * This results in the various driver arrays being indexed as follows:
1493     *
1494     * ciss_controllers[] - indexed by logical bus
1495     * ciss_cam_sim[]     - indexed by both logical and physical, with physical
1496     *                      being shifted by 32.
1497     * ciss_logical[][]   - indexed by logical bus
1498     * ciss_physical[][]  - indexed by physical bus
1499     *
1500     * XXX This is getting more and more hackish.  CISS really doesn't play
1501     *     well with a standard SCSI model; devices are addressed via magic
1502     *     cookies, not via b/t/l addresses.  Since there is no way to store
1503     *     the cookie in the CAM device object, we have to keep these lookup
1504     *     tables handy so that the devices can be found quickly at the cost
1505     *     of wasting memory and having a convoluted lookup scheme.  This
1506     *     driver should probably be converted to block interface.
1507     */
1508    /*
1509     * If the L2 and L3 SCSI addresses are 0, this signifies a proxy
1510     * controller. A proxy controller is another physical controller
1511     * behind the primary PCI controller. We need to know about this
1512     * so that BMIC commands can be properly targeted.  There can be
1513     * proxy controllers attached to a single PCI controller, so
1514     * find the highest numbered one so the array can be properly
1515     * sized.
1516     */
1517    sc->ciss_max_logical_bus = 1;
1518    for (i = 0; i < nphys; i++) {
1519	if (cll->lun[i].physical.extra_address == 0) {
1520	    bus = cll->lun[i].physical.bus;
1521	    sc->ciss_max_logical_bus = max(sc->ciss_max_logical_bus, bus) + 1;
1522	} else {
1523	    bus = CISS_EXTRA_BUS2(cll->lun[i].physical.extra_address);
1524	    sc->ciss_max_physical_bus = max(sc->ciss_max_physical_bus, bus);
1525	}
1526    }
1527
1528    sc->ciss_controllers =
1529	malloc(sc->ciss_max_logical_bus * sizeof (union ciss_device_address),
1530	       CISS_MALLOC_CLASS, M_NOWAIT | M_ZERO);
1531
1532    if (sc->ciss_controllers == NULL) {
1533	ciss_printf(sc, "Could not allocate memory for controller map\n");
1534	error = ENOMEM;
1535	goto out;
1536    }
1537
1538    /* setup a map of controller addresses */
1539    for (i = 0; i < nphys; i++) {
1540	if (cll->lun[i].physical.extra_address == 0) {
1541	    sc->ciss_controllers[cll->lun[i].physical.bus] = cll->lun[i];
1542	}
1543    }
1544
1545    sc->ciss_physical =
1546	malloc(sc->ciss_max_physical_bus * sizeof(struct ciss_pdrive *),
1547	       CISS_MALLOC_CLASS, M_NOWAIT | M_ZERO);
1548    if (sc->ciss_physical == NULL) {
1549	ciss_printf(sc, "Could not allocate memory for physical device map\n");
1550	error = ENOMEM;
1551	goto out;
1552    }
1553
1554    for (i = 0; i < sc->ciss_max_physical_bus; i++) {
1555	sc->ciss_physical[i] =
1556	    malloc(sizeof(struct ciss_pdrive) * CISS_MAX_PHYSTGT,
1557		   CISS_MALLOC_CLASS, M_NOWAIT | M_ZERO);
1558	if (sc->ciss_physical[i] == NULL) {
1559	    ciss_printf(sc, "Could not allocate memory for target map\n");
1560	    error = ENOMEM;
1561	    goto out;
1562	}
1563    }
1564
1565    ciss_filter_physical(sc, cll);
1566
1567out:
1568    if (cll != NULL)
1569	free(cll, CISS_MALLOC_CLASS);
1570
1571    return(error);
1572}
1573
1574static int
1575ciss_filter_physical(struct ciss_softc *sc, struct ciss_lun_report *cll)
1576{
1577    u_int32_t ea;
1578    int i, nphys;
1579    int	bus, target;
1580
1581    nphys = (ntohl(cll->list_size) / sizeof(union ciss_device_address));
1582    for (i = 0; i < nphys; i++) {
1583	if (cll->lun[i].physical.extra_address == 0)
1584	    continue;
1585
1586	/*
1587	 * Filter out devices that we don't want.  Level 3 LUNs could
1588	 * probably be supported, but the docs don't give enough of a
1589	 * hint to know how.
1590	 *
1591	 * The mode field of the physical address is likely set to have
1592	 * hard disks masked out.  Honor it unless the user has overridden
1593	 * us with the tunable.  We also munge the inquiry data for these
1594	 * disks so that they only show up as passthrough devices.  Keeping
1595	 * them visible in this fashion is useful for doing things like
1596	 * flashing firmware.
1597	 */
1598	ea = cll->lun[i].physical.extra_address;
1599	if ((CISS_EXTRA_BUS3(ea) != 0) || (CISS_EXTRA_TARGET3(ea) != 0) ||
1600	    (CISS_EXTRA_MODE2(ea) == 0x3))
1601	    continue;
1602	if ((ciss_expose_hidden_physical == 0) &&
1603	   (cll->lun[i].physical.mode == CISS_HDR_ADDRESS_MODE_MASK_PERIPHERAL))
1604	    continue;
1605
1606	/*
1607	 * Note: CISS firmware numbers physical busses starting at '1', not
1608	 *       '0'.  This numbering is internal to the firmware and is only
1609	 *       used as a hint here.
1610	 */
1611	bus = CISS_EXTRA_BUS2(ea) - 1;
1612	target = CISS_EXTRA_TARGET2(ea);
1613	sc->ciss_physical[bus][target].cp_address = cll->lun[i];
1614	sc->ciss_physical[bus][target].cp_online = 1;
1615    }
1616
1617    return (0);
1618}
1619
1620static int
1621ciss_inquiry_logical(struct ciss_softc *sc, struct ciss_ldrive *ld)
1622{
1623    struct ciss_request			*cr;
1624    struct ciss_command			*cc;
1625    struct scsi_inquiry			*inq;
1626    int					error;
1627    int					command_status;
1628
1629    cr = NULL;
1630
1631    bzero(&ld->cl_geometry, sizeof(ld->cl_geometry));
1632
1633    if ((error = ciss_get_request(sc, &cr)) != 0)
1634	goto out;
1635
1636    cc = cr->cr_cc;
1637    cr->cr_data = &ld->cl_geometry;
1638    cr->cr_length = sizeof(ld->cl_geometry);
1639    cr->cr_flags = CISS_REQ_DATAIN;
1640
1641    cc->header.address = ld->cl_address;
1642    cc->cdb.cdb_length = 6;
1643    cc->cdb.type = CISS_CDB_TYPE_COMMAND;
1644    cc->cdb.attribute = CISS_CDB_ATTRIBUTE_SIMPLE;
1645    cc->cdb.direction = CISS_CDB_DIRECTION_READ;
1646    cc->cdb.timeout = 30;
1647
1648    inq = (struct scsi_inquiry *)&(cc->cdb.cdb[0]);
1649    inq->opcode = INQUIRY;
1650    inq->byte2 = SI_EVPD;
1651    inq->page_code = CISS_VPD_LOGICAL_DRIVE_GEOMETRY;
1652    scsi_ulto2b(sizeof(ld->cl_geometry), inq->length);
1653
1654    if ((error = ciss_synch_request(cr, 60 * 1000)) != 0) {
1655	ciss_printf(sc, "error getting geometry (%d)\n", error);
1656	goto out;
1657    }
1658
1659    ciss_report_request(cr, &command_status, NULL);
1660    switch(command_status) {
1661    case CISS_CMD_STATUS_SUCCESS:
1662    case CISS_CMD_STATUS_DATA_UNDERRUN:
1663	break;
1664    case CISS_CMD_STATUS_DATA_OVERRUN:
1665	ciss_printf(sc, "WARNING: Data overrun\n");
1666	break;
1667    default:
1668	ciss_printf(sc, "Error detecting logical drive geometry (%s)\n",
1669		    ciss_name_command_status(command_status));
1670	break;
1671    }
1672
1673out:
1674    if (cr != NULL)
1675	ciss_release_request(cr);
1676    return(error);
1677}
1678/************************************************************************
1679 * Identify a logical drive, initialise state related to it.
1680 */
1681static int
1682ciss_identify_logical(struct ciss_softc *sc, struct ciss_ldrive *ld)
1683{
1684    struct ciss_request		*cr;
1685    struct ciss_command		*cc;
1686    struct ciss_bmic_cdb	*cbc;
1687    int				error, command_status;
1688
1689    debug_called(1);
1690
1691    cr = NULL;
1692
1693    /*
1694     * Build a BMIC request to fetch the drive ID.
1695     */
1696    if ((error = ciss_get_bmic_request(sc, &cr, CISS_BMIC_ID_LDRIVE,
1697				       (void **)&ld->cl_ldrive,
1698				       sizeof(*ld->cl_ldrive))) != 0)
1699	goto out;
1700    cc = cr->cr_cc;
1701    cc->header.address = *ld->cl_controller;	/* target controller */
1702    cbc = (struct ciss_bmic_cdb *)&(cc->cdb.cdb[0]);
1703    cbc->log_drive = CISS_LUN_TO_TARGET(ld->cl_address.logical.lun);
1704
1705    /*
1706     * Submit the request and wait for it to complete.
1707     */
1708    if ((error = ciss_synch_request(cr, 60 * 1000)) != 0) {
1709	ciss_printf(sc, "error sending BMIC LDRIVE command (%d)\n", error);
1710	goto out;
1711    }
1712
1713    /*
1714     * Check response.
1715     */
1716    ciss_report_request(cr, &command_status, NULL);
1717    switch(command_status) {
1718    case CISS_CMD_STATUS_SUCCESS:		/* buffer right size */
1719	break;
1720    case CISS_CMD_STATUS_DATA_UNDERRUN:
1721    case CISS_CMD_STATUS_DATA_OVERRUN:
1722	ciss_printf(sc, "data over/underrun reading logical drive ID\n");
1723    default:
1724	ciss_printf(sc, "error reading logical drive ID (%s)\n",
1725		    ciss_name_command_status(command_status));
1726	error = EIO;
1727	goto out;
1728    }
1729    ciss_release_request(cr);
1730    cr = NULL;
1731
1732    /*
1733     * Build a CISS BMIC command to get the logical drive status.
1734     */
1735    if ((error = ciss_get_ldrive_status(sc, ld)) != 0)
1736	goto out;
1737
1738    /*
1739     * Get the logical drive geometry.
1740     */
1741    if ((error = ciss_inquiry_logical(sc, ld)) != 0)
1742	goto out;
1743
1744    /*
1745     * Print the drive's basic characteristics.
1746     */
1747    if (bootverbose) {
1748	ciss_printf(sc, "logical drive (b%dt%d): %s, %dMB ",
1749		    CISS_LUN_TO_BUS(ld->cl_address.logical.lun),
1750		    CISS_LUN_TO_TARGET(ld->cl_address.logical.lun),
1751		    ciss_name_ldrive_org(ld->cl_ldrive->fault_tolerance),
1752		    ((ld->cl_ldrive->blocks_available / (1024 * 1024)) *
1753		     ld->cl_ldrive->block_size));
1754
1755	ciss_print_ldrive(sc, ld);
1756    }
1757out:
1758    if (error != 0) {
1759	/* make the drive not-exist */
1760	ld->cl_status = CISS_LD_NONEXISTENT;
1761	if (ld->cl_ldrive != NULL) {
1762	    free(ld->cl_ldrive, CISS_MALLOC_CLASS);
1763	    ld->cl_ldrive = NULL;
1764	}
1765	if (ld->cl_lstatus != NULL) {
1766	    free(ld->cl_lstatus, CISS_MALLOC_CLASS);
1767	    ld->cl_lstatus = NULL;
1768	}
1769    }
1770    if (cr != NULL)
1771	ciss_release_request(cr);
1772
1773    return(error);
1774}
1775
1776/************************************************************************
1777 * Get status for a logical drive.
1778 *
1779 * XXX should we also do this in response to Test Unit Ready?
1780 */
1781static int
1782ciss_get_ldrive_status(struct ciss_softc *sc,  struct ciss_ldrive *ld)
1783{
1784    struct ciss_request		*cr;
1785    struct ciss_command		*cc;
1786    struct ciss_bmic_cdb	*cbc;
1787    int				error, command_status;
1788
1789    /*
1790     * Build a CISS BMIC command to get the logical drive status.
1791     */
1792    if ((error = ciss_get_bmic_request(sc, &cr, CISS_BMIC_ID_LSTATUS,
1793				       (void **)&ld->cl_lstatus,
1794				       sizeof(*ld->cl_lstatus))) != 0)
1795	goto out;
1796    cc = cr->cr_cc;
1797    cc->header.address = *ld->cl_controller;	/* target controller */
1798    cbc = (struct ciss_bmic_cdb *)&(cc->cdb.cdb[0]);
1799    cbc->log_drive = CISS_LUN_TO_TARGET(ld->cl_address.logical.lun);
1800
1801    /*
1802     * Submit the request and wait for it to complete.
1803     */
1804    if ((error = ciss_synch_request(cr, 60 * 1000)) != 0) {
1805	ciss_printf(sc, "error sending BMIC LSTATUS command (%d)\n", error);
1806	goto out;
1807    }
1808
1809    /*
1810     * Check response.
1811     */
1812    ciss_report_request(cr, &command_status, NULL);
1813    switch(command_status) {
1814    case CISS_CMD_STATUS_SUCCESS:		/* buffer right size */
1815	break;
1816    case CISS_CMD_STATUS_DATA_UNDERRUN:
1817    case CISS_CMD_STATUS_DATA_OVERRUN:
1818	ciss_printf(sc, "data over/underrun reading logical drive status\n");
1819    default:
1820	ciss_printf(sc, "error reading logical drive status (%s)\n",
1821		    ciss_name_command_status(command_status));
1822	error = EIO;
1823	goto out;
1824    }
1825
1826    /*
1827     * Set the drive's summary status based on the returned status.
1828     *
1829     * XXX testing shows that a failed JBOD drive comes back at next
1830     * boot in "queued for expansion" mode.  WTF?
1831     */
1832    ld->cl_status = ciss_decode_ldrive_status(ld->cl_lstatus->status);
1833
1834out:
1835    if (cr != NULL)
1836	ciss_release_request(cr);
1837    return(error);
1838}
1839
1840/************************************************************************
1841 * Notify the adapter of a config update.
1842 */
1843static int
1844ciss_update_config(struct ciss_softc *sc)
1845{
1846    int		i;
1847
1848    debug_called(1);
1849
1850    CISS_TL_SIMPLE_WRITE(sc, CISS_TL_SIMPLE_IDBR, CISS_TL_SIMPLE_IDBR_CFG_TABLE);
1851    for (i = 0; i < 1000; i++) {
1852	if (!(CISS_TL_SIMPLE_READ(sc, CISS_TL_SIMPLE_IDBR) &
1853	      CISS_TL_SIMPLE_IDBR_CFG_TABLE)) {
1854	    return(0);
1855	}
1856	DELAY(1000);
1857    }
1858    return(1);
1859}
1860
1861/************************************************************************
1862 * Accept new media into a logical drive.
1863 *
1864 * XXX The drive has previously been offline; it would be good if we
1865 *     could make sure it's not open right now.
1866 */
1867static int
1868ciss_accept_media(struct ciss_softc *sc, struct ciss_ldrive *ld)
1869{
1870    struct ciss_request		*cr;
1871    struct ciss_command		*cc;
1872    struct ciss_bmic_cdb	*cbc;
1873    int				command_status;
1874    int				error = 0, ldrive;
1875
1876    ldrive = CISS_LUN_TO_TARGET(ld->cl_address.logical.lun);
1877
1878    debug(0, "bringing logical drive %d back online", ldrive);
1879
1880    /*
1881     * Build a CISS BMIC command to bring the drive back online.
1882     */
1883    if ((error = ciss_get_bmic_request(sc, &cr, CISS_BMIC_ACCEPT_MEDIA,
1884				       NULL, 0)) != 0)
1885	goto out;
1886    cc = cr->cr_cc;
1887    cc->header.address = *ld->cl_controller;	/* target controller */
1888    cbc = (struct ciss_bmic_cdb *)&(cc->cdb.cdb[0]);
1889    cbc->log_drive = ldrive;
1890
1891    /*
1892     * Submit the request and wait for it to complete.
1893     */
1894    if ((error = ciss_synch_request(cr, 60 * 1000)) != 0) {
1895	ciss_printf(sc, "error sending BMIC ACCEPT MEDIA command (%d)\n", error);
1896	goto out;
1897    }
1898
1899    /*
1900     * Check response.
1901     */
1902    ciss_report_request(cr, &command_status, NULL);
1903    switch(command_status) {
1904    case CISS_CMD_STATUS_SUCCESS:		/* all OK */
1905	/* we should get a logical drive status changed event here */
1906	break;
1907    default:
1908	ciss_printf(cr->cr_sc, "error accepting media into failed logical drive (%s)\n",
1909		    ciss_name_command_status(command_status));
1910	break;
1911    }
1912
1913out:
1914    if (cr != NULL)
1915	ciss_release_request(cr);
1916    return(error);
1917}
1918
1919/************************************************************************
1920 * Release adapter resources.
1921 */
1922static void
1923ciss_free(struct ciss_softc *sc)
1924{
1925    struct ciss_request *cr;
1926    int			i, j;
1927
1928    debug_called(1);
1929
1930    /* we're going away */
1931    sc->ciss_flags |= CISS_FLAG_ABORTING;
1932
1933    /* terminate the periodic heartbeat routine */
1934    callout_stop(&sc->ciss_periodic);
1935
1936    /* cancel the Event Notify chain */
1937    ciss_notify_abort(sc);
1938
1939    ciss_kill_notify_thread(sc);
1940
1941    /* disconnect from CAM */
1942    if (sc->ciss_cam_sim) {
1943	for (i = 0; i < sc->ciss_max_logical_bus; i++) {
1944	    if (sc->ciss_cam_sim[i]) {
1945		xpt_bus_deregister(cam_sim_path(sc->ciss_cam_sim[i]));
1946		cam_sim_free(sc->ciss_cam_sim[i], 0);
1947	    }
1948	}
1949	for (i = CISS_PHYSICAL_BASE; i < sc->ciss_max_physical_bus +
1950	     CISS_PHYSICAL_BASE; i++) {
1951	    if (sc->ciss_cam_sim[i]) {
1952		xpt_bus_deregister(cam_sim_path(sc->ciss_cam_sim[i]));
1953		cam_sim_free(sc->ciss_cam_sim[i], 0);
1954	    }
1955	}
1956	free(sc->ciss_cam_sim, CISS_MALLOC_CLASS);
1957    }
1958    if (sc->ciss_cam_devq)
1959	cam_simq_free(sc->ciss_cam_devq);
1960
1961    /* remove the control device */
1962    mtx_unlock(&sc->ciss_mtx);
1963    if (sc->ciss_dev_t != NULL)
1964	destroy_dev(sc->ciss_dev_t);
1965
1966    /* Final cleanup of the callout. */
1967    callout_drain(&sc->ciss_periodic);
1968    mtx_destroy(&sc->ciss_mtx);
1969
1970    /* free the controller data */
1971    if (sc->ciss_id != NULL)
1972	free(sc->ciss_id, CISS_MALLOC_CLASS);
1973
1974    /* release I/O resources */
1975    if (sc->ciss_regs_resource != NULL)
1976	bus_release_resource(sc->ciss_dev, SYS_RES_MEMORY,
1977			     sc->ciss_regs_rid, sc->ciss_regs_resource);
1978    if (sc->ciss_cfg_resource != NULL)
1979	bus_release_resource(sc->ciss_dev, SYS_RES_MEMORY,
1980			     sc->ciss_cfg_rid, sc->ciss_cfg_resource);
1981    if (sc->ciss_intr != NULL)
1982	bus_teardown_intr(sc->ciss_dev, sc->ciss_irq_resource, sc->ciss_intr);
1983    if (sc->ciss_irq_resource != NULL)
1984	bus_release_resource(sc->ciss_dev, SYS_RES_IRQ,
1985			     sc->ciss_irq_rid[0], sc->ciss_irq_resource);
1986    if (sc->ciss_msi)
1987	pci_release_msi(sc->ciss_dev);
1988
1989    while ((cr = ciss_dequeue_free(sc)) != NULL)
1990	bus_dmamap_destroy(sc->ciss_buffer_dmat, cr->cr_datamap);
1991    if (sc->ciss_buffer_dmat)
1992	bus_dma_tag_destroy(sc->ciss_buffer_dmat);
1993
1994    /* destroy command memory and DMA tag */
1995    if (sc->ciss_command != NULL) {
1996	bus_dmamap_unload(sc->ciss_command_dmat, sc->ciss_command_map);
1997	bus_dmamem_free(sc->ciss_command_dmat, sc->ciss_command, sc->ciss_command_map);
1998    }
1999    if (sc->ciss_command_dmat)
2000	bus_dma_tag_destroy(sc->ciss_command_dmat);
2001
2002    if (sc->ciss_reply) {
2003	bus_dmamap_unload(sc->ciss_reply_dmat, sc->ciss_reply_map);
2004	bus_dmamem_free(sc->ciss_reply_dmat, sc->ciss_reply, sc->ciss_reply_map);
2005    }
2006    if (sc->ciss_reply_dmat)
2007	bus_dma_tag_destroy(sc->ciss_reply_dmat);
2008
2009    /* destroy DMA tags */
2010    if (sc->ciss_parent_dmat)
2011	bus_dma_tag_destroy(sc->ciss_parent_dmat);
2012    if (sc->ciss_logical) {
2013	for (i = 0; i <= sc->ciss_max_logical_bus; i++) {
2014	    for (j = 0; j < sc->ciss_cfg->max_logical_supported; j++) {
2015		if (sc->ciss_logical[i][j].cl_ldrive)
2016		    free(sc->ciss_logical[i][j].cl_ldrive, CISS_MALLOC_CLASS);
2017		if (sc->ciss_logical[i][j].cl_lstatus)
2018		    free(sc->ciss_logical[i][j].cl_lstatus, CISS_MALLOC_CLASS);
2019	    }
2020	    free(sc->ciss_logical[i], CISS_MALLOC_CLASS);
2021	}
2022	free(sc->ciss_logical, CISS_MALLOC_CLASS);
2023    }
2024
2025    if (sc->ciss_physical) {
2026	for (i = 0; i < sc->ciss_max_physical_bus; i++)
2027	    free(sc->ciss_physical[i], CISS_MALLOC_CLASS);
2028	free(sc->ciss_physical, CISS_MALLOC_CLASS);
2029    }
2030
2031    if (sc->ciss_controllers)
2032	free(sc->ciss_controllers, CISS_MALLOC_CLASS);
2033
2034}
2035
2036/************************************************************************
2037 * Give a command to the adapter.
2038 *
2039 * Note that this uses the simple transport layer directly.  If we
2040 * want to add support for other layers, we'll need a switch of some
2041 * sort.
2042 *
2043 * Note that the simple transport layer has no way of refusing a
2044 * command; we only have as many request structures as the adapter
2045 * supports commands, so we don't have to check (this presumes that
2046 * the adapter can handle commands as fast as we throw them at it).
2047 */
2048static int
2049ciss_start(struct ciss_request *cr)
2050{
2051    struct ciss_command	*cc;	/* XXX debugging only */
2052    int			error;
2053
2054    cc = cr->cr_cc;
2055    debug(2, "post command %d tag %d ", cr->cr_tag, cc->header.host_tag);
2056
2057    /*
2058     * Map the request's data.
2059     */
2060    if ((error = ciss_map_request(cr)))
2061	return(error);
2062
2063#if 0
2064    ciss_print_request(cr);
2065#endif
2066
2067    return(0);
2068}
2069
2070/************************************************************************
2071 * Fetch completed request(s) from the adapter, queue them for
2072 * completion handling.
2073 *
2074 * Note that this uses the simple transport layer directly.  If we
2075 * want to add support for other layers, we'll need a switch of some
2076 * sort.
2077 *
2078 * Note that the simple transport mechanism does not require any
2079 * reentrancy protection; the OPQ read is atomic.  If there is a
2080 * chance of a race with something else that might move the request
2081 * off the busy list, then we will have to lock against that
2082 * (eg. timeouts, etc.)
2083 */
2084static void
2085ciss_done(struct ciss_softc *sc, cr_qhead_t *qh)
2086{
2087    struct ciss_request	*cr;
2088    struct ciss_command	*cc;
2089    u_int32_t		tag, index;
2090
2091    debug_called(3);
2092
2093    /*
2094     * Loop quickly taking requests from the adapter and moving them
2095     * to the completed queue.
2096     */
2097    for (;;) {
2098
2099	tag = CISS_TL_SIMPLE_FETCH_CMD(sc);
2100	if (tag == CISS_TL_SIMPLE_OPQ_EMPTY)
2101	    break;
2102	index = tag >> 2;
2103	debug(2, "completed command %d%s", index,
2104	      (tag & CISS_HDR_HOST_TAG_ERROR) ? " with error" : "");
2105	if (index >= sc->ciss_max_requests) {
2106	    ciss_printf(sc, "completed invalid request %d (0x%x)\n", index, tag);
2107	    continue;
2108	}
2109	cr = &(sc->ciss_request[index]);
2110	cc = cr->cr_cc;
2111	cc->header.host_tag = tag;	/* not updated by adapter */
2112	ciss_enqueue_complete(cr, qh);
2113    }
2114
2115}
2116
2117static void
2118ciss_perf_done(struct ciss_softc *sc, cr_qhead_t *qh)
2119{
2120    struct ciss_request	*cr;
2121    struct ciss_command	*cc;
2122    u_int32_t		tag, index;
2123
2124    debug_called(3);
2125
2126    /*
2127     * Loop quickly taking requests from the adapter and moving them
2128     * to the completed queue.
2129     */
2130    for (;;) {
2131	tag = sc->ciss_reply[sc->ciss_rqidx];
2132	if ((tag & CISS_CYCLE_MASK) != sc->ciss_cycle)
2133	    break;
2134	index = tag >> 2;
2135	debug(2, "completed command %d%s\n", index,
2136	      (tag & CISS_HDR_HOST_TAG_ERROR) ? " with error" : "");
2137	if (index < sc->ciss_max_requests) {
2138	    cr = &(sc->ciss_request[index]);
2139	    cc = cr->cr_cc;
2140	    cc->header.host_tag = tag;	/* not updated by adapter */
2141	    ciss_enqueue_complete(cr, qh);
2142	} else {
2143	    ciss_printf(sc, "completed invalid request %d (0x%x)\n", index, tag);
2144	}
2145	if (++sc->ciss_rqidx == sc->ciss_max_requests) {
2146	    sc->ciss_rqidx = 0;
2147	    sc->ciss_cycle ^= 1;
2148	}
2149    }
2150
2151}
2152
2153/************************************************************************
2154 * Take an interrupt from the adapter.
2155 */
2156static void
2157ciss_intr(void *arg)
2158{
2159    cr_qhead_t qh;
2160    struct ciss_softc	*sc = (struct ciss_softc *)arg;
2161
2162    /*
2163     * The only interrupt we recognise indicates that there are
2164     * entries in the outbound post queue.
2165     */
2166    STAILQ_INIT(&qh);
2167    ciss_done(sc, &qh);
2168    mtx_lock(&sc->ciss_mtx);
2169    ciss_complete(sc, &qh);
2170    mtx_unlock(&sc->ciss_mtx);
2171}
2172
2173static void
2174ciss_perf_intr(void *arg)
2175{
2176    struct ciss_softc	*sc = (struct ciss_softc *)arg;
2177
2178    /* Clear the interrupt and flush the bridges.  Docs say that the flush
2179     * needs to be done twice, which doesn't seem right.
2180     */
2181    CISS_TL_PERF_CLEAR_INT(sc);
2182    CISS_TL_PERF_FLUSH_INT(sc);
2183
2184    ciss_perf_msi_intr(sc);
2185}
2186
2187static void
2188ciss_perf_msi_intr(void *arg)
2189{
2190    cr_qhead_t qh;
2191    struct ciss_softc	*sc = (struct ciss_softc *)arg;
2192
2193    STAILQ_INIT(&qh);
2194    ciss_perf_done(sc, &qh);
2195    mtx_lock(&sc->ciss_mtx);
2196    ciss_complete(sc, &qh);
2197    mtx_unlock(&sc->ciss_mtx);
2198}
2199
2200
2201/************************************************************************
2202 * Process completed requests.
2203 *
2204 * Requests can be completed in three fashions:
2205 *
2206 * - by invoking a callback function (cr_complete is non-null)
2207 * - by waking up a sleeper (cr_flags has CISS_REQ_SLEEP set)
2208 * - by clearing the CISS_REQ_POLL flag in interrupt/timeout context
2209 */
2210static void
2211ciss_complete(struct ciss_softc *sc, cr_qhead_t *qh)
2212{
2213    struct ciss_request	*cr;
2214
2215    debug_called(2);
2216
2217    /*
2218     * Loop taking requests off the completed queue and performing
2219     * completion processing on them.
2220     */
2221    for (;;) {
2222	if ((cr = ciss_dequeue_complete(sc, qh)) == NULL)
2223	    break;
2224	ciss_unmap_request(cr);
2225
2226	if ((cr->cr_flags & CISS_REQ_BUSY) == 0)
2227	    ciss_printf(sc, "WARNING: completing non-busy request\n");
2228	cr->cr_flags &= ~CISS_REQ_BUSY;
2229
2230	/*
2231	 * If the request has a callback, invoke it.
2232	 */
2233	if (cr->cr_complete != NULL) {
2234	    cr->cr_complete(cr);
2235	    continue;
2236	}
2237
2238	/*
2239	 * If someone is sleeping on this request, wake them up.
2240	 */
2241	if (cr->cr_flags & CISS_REQ_SLEEP) {
2242	    cr->cr_flags &= ~CISS_REQ_SLEEP;
2243	    wakeup(cr);
2244	    continue;
2245	}
2246
2247	/*
2248	 * If someone is polling this request for completion, signal.
2249	 */
2250	if (cr->cr_flags & CISS_REQ_POLL) {
2251	    cr->cr_flags &= ~CISS_REQ_POLL;
2252	    continue;
2253	}
2254
2255	/*
2256	 * Give up and throw the request back on the free queue.  This
2257	 * should never happen; resources will probably be lost.
2258	 */
2259	ciss_printf(sc, "WARNING: completed command with no submitter\n");
2260	ciss_enqueue_free(cr);
2261    }
2262}
2263
2264/************************************************************************
2265 * Report on the completion status of a request, and pass back SCSI
2266 * and command status values.
2267 */
2268static int
2269_ciss_report_request(struct ciss_request *cr, int *command_status, int *scsi_status, const char *func)
2270{
2271    struct ciss_command		*cc;
2272    struct ciss_error_info	*ce;
2273
2274    debug_called(2);
2275
2276    cc = cr->cr_cc;
2277    ce = (struct ciss_error_info *)&(cc->sg[0]);
2278
2279    /*
2280     * We don't consider data under/overrun an error for the Report
2281     * Logical/Physical LUNs commands.
2282     */
2283    if ((cc->header.host_tag & CISS_HDR_HOST_TAG_ERROR) &&
2284	((ce->command_status == CISS_CMD_STATUS_DATA_OVERRUN) ||
2285	 (ce->command_status == CISS_CMD_STATUS_DATA_UNDERRUN)) &&
2286	((cc->cdb.cdb[0] == CISS_OPCODE_REPORT_LOGICAL_LUNS) ||
2287	 (cc->cdb.cdb[0] == CISS_OPCODE_REPORT_PHYSICAL_LUNS) ||
2288	 (cc->cdb.cdb[0] == INQUIRY))) {
2289	cc->header.host_tag &= ~CISS_HDR_HOST_TAG_ERROR;
2290	debug(2, "ignoring irrelevant under/overrun error");
2291    }
2292
2293    /*
2294     * Check the command's error bit, if clear, there's no status and
2295     * everything is OK.
2296     */
2297    if (!(cc->header.host_tag & CISS_HDR_HOST_TAG_ERROR)) {
2298	if (scsi_status != NULL)
2299	    *scsi_status = SCSI_STATUS_OK;
2300	if (command_status != NULL)
2301	    *command_status = CISS_CMD_STATUS_SUCCESS;
2302	return(0);
2303    } else {
2304	if (command_status != NULL)
2305	    *command_status = ce->command_status;
2306	if (scsi_status != NULL) {
2307	    if (ce->command_status == CISS_CMD_STATUS_TARGET_STATUS) {
2308		*scsi_status = ce->scsi_status;
2309	    } else {
2310		*scsi_status = -1;
2311	    }
2312	}
2313	if (bootverbose)
2314	    ciss_printf(cr->cr_sc, "command status 0x%x (%s) scsi status 0x%x\n",
2315			ce->command_status, ciss_name_command_status(ce->command_status),
2316			ce->scsi_status);
2317	if (ce->command_status == CISS_CMD_STATUS_INVALID_COMMAND) {
2318	    ciss_printf(cr->cr_sc, "invalid command, offense size %d at %d, value 0x%x, function %s\n",
2319			ce->additional_error_info.invalid_command.offense_size,
2320			ce->additional_error_info.invalid_command.offense_offset,
2321			ce->additional_error_info.invalid_command.offense_value,
2322			func);
2323	}
2324    }
2325#if 0
2326    ciss_print_request(cr);
2327#endif
2328    return(1);
2329}
2330
2331/************************************************************************
2332 * Issue a request and don't return until it's completed.
2333 *
2334 * Depending on adapter status, we may poll or sleep waiting for
2335 * completion.
2336 */
2337static int
2338ciss_synch_request(struct ciss_request *cr, int timeout)
2339{
2340    if (cr->cr_sc->ciss_flags & CISS_FLAG_RUNNING) {
2341	return(ciss_wait_request(cr, timeout));
2342    } else {
2343	return(ciss_poll_request(cr, timeout));
2344    }
2345}
2346
2347/************************************************************************
2348 * Issue a request and poll for completion.
2349 *
2350 * Timeout in milliseconds.
2351 */
2352static int
2353ciss_poll_request(struct ciss_request *cr, int timeout)
2354{
2355    cr_qhead_t qh;
2356    struct ciss_softc *sc;
2357    int		error;
2358
2359    debug_called(2);
2360
2361    STAILQ_INIT(&qh);
2362    sc = cr->cr_sc;
2363    cr->cr_flags |= CISS_REQ_POLL;
2364    if ((error = ciss_start(cr)) != 0)
2365	return(error);
2366
2367    do {
2368	if (sc->ciss_perf)
2369	    ciss_perf_done(sc, &qh);
2370	else
2371	    ciss_done(sc, &qh);
2372	ciss_complete(sc, &qh);
2373	if (!(cr->cr_flags & CISS_REQ_POLL))
2374	    return(0);
2375	DELAY(1000);
2376    } while (timeout-- >= 0);
2377    return(EWOULDBLOCK);
2378}
2379
2380/************************************************************************
2381 * Issue a request and sleep waiting for completion.
2382 *
2383 * Timeout in milliseconds.  Note that a spurious wakeup will reset
2384 * the timeout.
2385 */
2386static int
2387ciss_wait_request(struct ciss_request *cr, int timeout)
2388{
2389    int		error;
2390
2391    debug_called(2);
2392
2393    cr->cr_flags |= CISS_REQ_SLEEP;
2394    if ((error = ciss_start(cr)) != 0)
2395	return(error);
2396
2397    while ((cr->cr_flags & CISS_REQ_SLEEP) && (error != EWOULDBLOCK)) {
2398	error = msleep(cr, &cr->cr_sc->ciss_mtx, PRIBIO, "cissREQ", (timeout * hz) / 1000);
2399    }
2400    return(error);
2401}
2402
2403#if 0
2404/************************************************************************
2405 * Abort a request.  Note that a potential exists here to race the
2406 * request being completed; the caller must deal with this.
2407 */
2408static int
2409ciss_abort_request(struct ciss_request *ar)
2410{
2411    struct ciss_request		*cr;
2412    struct ciss_command		*cc;
2413    struct ciss_message_cdb	*cmc;
2414    int				error;
2415
2416    debug_called(1);
2417
2418    /* get a request */
2419    if ((error = ciss_get_request(ar->cr_sc, &cr)) != 0)
2420	return(error);
2421
2422    /* build the abort command */
2423    cc = cr->cr_cc;
2424    cc->header.address.mode.mode = CISS_HDR_ADDRESS_MODE_PERIPHERAL;	/* addressing? */
2425    cc->header.address.physical.target = 0;
2426    cc->header.address.physical.bus = 0;
2427    cc->cdb.cdb_length = sizeof(*cmc);
2428    cc->cdb.type = CISS_CDB_TYPE_MESSAGE;
2429    cc->cdb.attribute = CISS_CDB_ATTRIBUTE_SIMPLE;
2430    cc->cdb.direction = CISS_CDB_DIRECTION_NONE;
2431    cc->cdb.timeout = 30;
2432
2433    cmc = (struct ciss_message_cdb *)&(cc->cdb.cdb[0]);
2434    cmc->opcode = CISS_OPCODE_MESSAGE_ABORT;
2435    cmc->type = CISS_MESSAGE_ABORT_TASK;
2436    cmc->abort_tag = ar->cr_tag;	/* endianness?? */
2437
2438    /*
2439     * Send the request and wait for a response.  If we believe we
2440     * aborted the request OK, clear the flag that indicates it's
2441     * running.
2442     */
2443    error = ciss_synch_request(cr, 35 * 1000);
2444    if (!error)
2445	error = ciss_report_request(cr, NULL, NULL);
2446    ciss_release_request(cr);
2447
2448    return(error);
2449}
2450#endif
2451
2452
2453/************************************************************************
2454 * Fetch and initialise a request
2455 */
2456static int
2457ciss_get_request(struct ciss_softc *sc, struct ciss_request **crp)
2458{
2459    struct ciss_request *cr;
2460
2461    debug_called(2);
2462
2463    /*
2464     * Get a request and clean it up.
2465     */
2466    if ((cr = ciss_dequeue_free(sc)) == NULL)
2467	return(ENOMEM);
2468
2469    cr->cr_data = NULL;
2470    cr->cr_flags = 0;
2471    cr->cr_complete = NULL;
2472    cr->cr_private = NULL;
2473    cr->cr_sg_tag = CISS_SG_MAX;	/* Backstop to prevent accidents */
2474
2475    ciss_preen_command(cr);
2476    *crp = cr;
2477    return(0);
2478}
2479
2480static void
2481ciss_preen_command(struct ciss_request *cr)
2482{
2483    struct ciss_command	*cc;
2484    u_int32_t		cmdphys;
2485
2486    /*
2487     * Clean up the command structure.
2488     *
2489     * Note that we set up the error_info structure here, since the
2490     * length can be overwritten by any command.
2491     */
2492    cc = cr->cr_cc;
2493    cc->header.sg_in_list = 0;		/* kinda inefficient this way */
2494    cc->header.sg_total = 0;
2495    cc->header.host_tag = cr->cr_tag << 2;
2496    cc->header.host_tag_zeroes = 0;
2497    bzero(&(cc->sg[0]), CISS_COMMAND_ALLOC_SIZE - sizeof(struct ciss_command));
2498    cmdphys = cr->cr_ccphys;
2499    cc->error_info.error_info_address = cmdphys + sizeof(struct ciss_command);
2500    cc->error_info.error_info_length = CISS_COMMAND_ALLOC_SIZE - sizeof(struct ciss_command);
2501}
2502
2503/************************************************************************
2504 * Release a request to the free list.
2505 */
2506static void
2507ciss_release_request(struct ciss_request *cr)
2508{
2509    struct ciss_softc	*sc;
2510
2511    debug_called(2);
2512
2513    sc = cr->cr_sc;
2514
2515    /* release the request to the free queue */
2516    ciss_requeue_free(cr);
2517}
2518
2519/************************************************************************
2520 * Allocate a request that will be used to send a BMIC command.  Do some
2521 * of the common setup here to avoid duplicating it everywhere else.
2522 */
2523static int
2524ciss_get_bmic_request(struct ciss_softc *sc, struct ciss_request **crp,
2525		      int opcode, void **bufp, size_t bufsize)
2526{
2527    struct ciss_request		*cr;
2528    struct ciss_command		*cc;
2529    struct ciss_bmic_cdb	*cbc;
2530    void			*buf;
2531    int				error;
2532    int				dataout;
2533
2534    debug_called(2);
2535
2536    cr = NULL;
2537    buf = NULL;
2538
2539    /*
2540     * Get a request.
2541     */
2542    if ((error = ciss_get_request(sc, &cr)) != 0)
2543	goto out;
2544
2545    /*
2546     * Allocate data storage if requested, determine the data direction.
2547     */
2548    dataout = 0;
2549    if ((bufsize > 0) && (bufp != NULL)) {
2550	if (*bufp == NULL) {
2551	    if ((buf = malloc(bufsize, CISS_MALLOC_CLASS, M_NOWAIT | M_ZERO)) == NULL) {
2552		error = ENOMEM;
2553		goto out;
2554	    }
2555	} else {
2556	    buf = *bufp;
2557	    dataout = 1;	/* we are given a buffer, so we are writing */
2558	}
2559    }
2560
2561    /*
2562     * Build a CISS BMIC command to get the logical drive ID.
2563     */
2564    cr->cr_data = buf;
2565    cr->cr_length = bufsize;
2566    if (!dataout)
2567	cr->cr_flags = CISS_REQ_DATAIN;
2568
2569    cc = cr->cr_cc;
2570    cc->header.address.physical.mode = CISS_HDR_ADDRESS_MODE_PERIPHERAL;
2571    cc->header.address.physical.bus = 0;
2572    cc->header.address.physical.target = 0;
2573    cc->cdb.cdb_length = sizeof(*cbc);
2574    cc->cdb.type = CISS_CDB_TYPE_COMMAND;
2575    cc->cdb.attribute = CISS_CDB_ATTRIBUTE_SIMPLE;
2576    cc->cdb.direction = dataout ? CISS_CDB_DIRECTION_WRITE : CISS_CDB_DIRECTION_READ;
2577    cc->cdb.timeout = 0;
2578
2579    cbc = (struct ciss_bmic_cdb *)&(cc->cdb.cdb[0]);
2580    bzero(cbc, sizeof(*cbc));
2581    cbc->opcode = dataout ? CISS_ARRAY_CONTROLLER_WRITE : CISS_ARRAY_CONTROLLER_READ;
2582    cbc->bmic_opcode = opcode;
2583    cbc->size = htons((u_int16_t)bufsize);
2584
2585out:
2586    if (error) {
2587	if (cr != NULL)
2588	    ciss_release_request(cr);
2589    } else {
2590	*crp = cr;
2591	if ((bufp != NULL) && (*bufp == NULL) && (buf != NULL))
2592	    *bufp = buf;
2593    }
2594    return(error);
2595}
2596
2597/************************************************************************
2598 * Handle a command passed in from userspace.
2599 */
2600static int
2601ciss_user_command(struct ciss_softc *sc, IOCTL_Command_struct *ioc)
2602{
2603    struct ciss_request		*cr;
2604    struct ciss_command		*cc;
2605    struct ciss_error_info	*ce;
2606    int				error = 0;
2607
2608    debug_called(1);
2609
2610    cr = NULL;
2611
2612    /*
2613     * Get a request.
2614     */
2615    while (ciss_get_request(sc, &cr) != 0)
2616	msleep(sc, &sc->ciss_mtx, PPAUSE, "cissREQ", hz);
2617    cc = cr->cr_cc;
2618
2619    /*
2620     * Allocate an in-kernel databuffer if required, copy in user data.
2621     */
2622    mtx_unlock(&sc->ciss_mtx);
2623    cr->cr_length = ioc->buf_size;
2624    if (ioc->buf_size > 0) {
2625	if ((cr->cr_data = malloc(ioc->buf_size, CISS_MALLOC_CLASS, M_NOWAIT)) == NULL) {
2626	    error = ENOMEM;
2627	    goto out_unlocked;
2628	}
2629	if ((error = copyin(ioc->buf, cr->cr_data, ioc->buf_size))) {
2630	    debug(0, "copyin: bad data buffer %p/%d", ioc->buf, ioc->buf_size);
2631	    goto out_unlocked;
2632	}
2633    }
2634
2635    /*
2636     * Build the request based on the user command.
2637     */
2638    bcopy(&ioc->LUN_info, &cc->header.address, sizeof(cc->header.address));
2639    bcopy(&ioc->Request, &cc->cdb, sizeof(cc->cdb));
2640
2641    /* XXX anything else to populate here? */
2642    mtx_lock(&sc->ciss_mtx);
2643
2644    /*
2645     * Run the command.
2646     */
2647    if ((error = ciss_synch_request(cr, 60 * 1000))) {
2648	debug(0, "request failed - %d", error);
2649	goto out;
2650    }
2651
2652    /*
2653     * Check to see if the command succeeded.
2654     */
2655    ce = (struct ciss_error_info *)&(cc->sg[0]);
2656    if ((cc->header.host_tag & CISS_HDR_HOST_TAG_ERROR) == 0)
2657	bzero(ce, sizeof(*ce));
2658
2659    /*
2660     * Copy the results back to the user.
2661     */
2662    bcopy(ce, &ioc->error_info, sizeof(*ce));
2663    mtx_unlock(&sc->ciss_mtx);
2664    if ((ioc->buf_size > 0) &&
2665	(error = copyout(cr->cr_data, ioc->buf, ioc->buf_size))) {
2666	debug(0, "copyout: bad data buffer %p/%d", ioc->buf, ioc->buf_size);
2667	goto out_unlocked;
2668    }
2669
2670    /* done OK */
2671    error = 0;
2672
2673out_unlocked:
2674    mtx_lock(&sc->ciss_mtx);
2675
2676out:
2677    if ((cr != NULL) && (cr->cr_data != NULL))
2678	free(cr->cr_data, CISS_MALLOC_CLASS);
2679    if (cr != NULL)
2680	ciss_release_request(cr);
2681    return(error);
2682}
2683
2684/************************************************************************
2685 * Map a request into bus-visible space, initialise the scatter/gather
2686 * list.
2687 */
2688static int
2689ciss_map_request(struct ciss_request *cr)
2690{
2691    struct ciss_softc	*sc;
2692    int			error = 0;
2693
2694    debug_called(2);
2695
2696    sc = cr->cr_sc;
2697
2698    /* check that mapping is necessary */
2699    if (cr->cr_flags & CISS_REQ_MAPPED)
2700	return(0);
2701
2702    cr->cr_flags |= CISS_REQ_MAPPED;
2703
2704    bus_dmamap_sync(sc->ciss_command_dmat, sc->ciss_command_map,
2705		    BUS_DMASYNC_PREWRITE);
2706
2707    if (cr->cr_data != NULL) {
2708	if (cr->cr_flags & CISS_REQ_CCB)
2709		error = bus_dmamap_load_ccb(sc->ciss_buffer_dmat,
2710					cr->cr_datamap, cr->cr_data,
2711					ciss_request_map_helper, cr, 0);
2712	else
2713		error = bus_dmamap_load(sc->ciss_buffer_dmat, cr->cr_datamap,
2714					cr->cr_data, cr->cr_length,
2715					ciss_request_map_helper, cr, 0);
2716	if (error != 0)
2717	    return (error);
2718    } else {
2719	/*
2720	 * Post the command to the adapter.
2721	 */
2722	cr->cr_sg_tag = CISS_SG_NONE;
2723	cr->cr_flags |= CISS_REQ_BUSY;
2724	if (sc->ciss_perf)
2725	    CISS_TL_PERF_POST_CMD(sc, cr);
2726	else
2727	    CISS_TL_SIMPLE_POST_CMD(sc, cr->cr_ccphys);
2728    }
2729
2730    return(0);
2731}
2732
2733static void
2734ciss_request_map_helper(void *arg, bus_dma_segment_t *segs, int nseg, int error)
2735{
2736    struct ciss_command	*cc;
2737    struct ciss_request *cr;
2738    struct ciss_softc	*sc;
2739    int			i;
2740
2741    debug_called(2);
2742
2743    cr = (struct ciss_request *)arg;
2744    sc = cr->cr_sc;
2745    cc = cr->cr_cc;
2746
2747    for (i = 0; i < nseg; i++) {
2748	cc->sg[i].address = segs[i].ds_addr;
2749	cc->sg[i].length = segs[i].ds_len;
2750	cc->sg[i].extension = 0;
2751    }
2752    /* we leave the s/g table entirely within the command */
2753    cc->header.sg_in_list = nseg;
2754    cc->header.sg_total = nseg;
2755
2756    if (cr->cr_flags & CISS_REQ_DATAIN)
2757	bus_dmamap_sync(sc->ciss_buffer_dmat, cr->cr_datamap, BUS_DMASYNC_PREREAD);
2758    if (cr->cr_flags & CISS_REQ_DATAOUT)
2759	bus_dmamap_sync(sc->ciss_buffer_dmat, cr->cr_datamap, BUS_DMASYNC_PREWRITE);
2760
2761    if (nseg == 0)
2762	cr->cr_sg_tag = CISS_SG_NONE;
2763    else if (nseg == 1)
2764	cr->cr_sg_tag = CISS_SG_1;
2765    else if (nseg == 2)
2766	cr->cr_sg_tag = CISS_SG_2;
2767    else if (nseg <= 4)
2768	cr->cr_sg_tag = CISS_SG_4;
2769    else if (nseg <= 8)
2770	cr->cr_sg_tag = CISS_SG_8;
2771    else if (nseg <= 16)
2772	cr->cr_sg_tag = CISS_SG_16;
2773    else if (nseg <= 32)
2774	cr->cr_sg_tag = CISS_SG_32;
2775    else
2776	cr->cr_sg_tag = CISS_SG_MAX;
2777
2778    /*
2779     * Post the command to the adapter.
2780     */
2781    cr->cr_flags |= CISS_REQ_BUSY;
2782    if (sc->ciss_perf)
2783	CISS_TL_PERF_POST_CMD(sc, cr);
2784    else
2785	CISS_TL_SIMPLE_POST_CMD(sc, cr->cr_ccphys);
2786}
2787
2788/************************************************************************
2789 * Unmap a request from bus-visible space.
2790 */
2791static void
2792ciss_unmap_request(struct ciss_request *cr)
2793{
2794    struct ciss_softc	*sc;
2795
2796    debug_called(2);
2797
2798    sc = cr->cr_sc;
2799
2800    /* check that unmapping is necessary */
2801    if ((cr->cr_flags & CISS_REQ_MAPPED) == 0)
2802	return;
2803
2804    bus_dmamap_sync(sc->ciss_command_dmat, sc->ciss_command_map,
2805		    BUS_DMASYNC_POSTWRITE);
2806
2807    if (cr->cr_data == NULL)
2808	goto out;
2809
2810    if (cr->cr_flags & CISS_REQ_DATAIN)
2811	bus_dmamap_sync(sc->ciss_buffer_dmat, cr->cr_datamap, BUS_DMASYNC_POSTREAD);
2812    if (cr->cr_flags & CISS_REQ_DATAOUT)
2813	bus_dmamap_sync(sc->ciss_buffer_dmat, cr->cr_datamap, BUS_DMASYNC_POSTWRITE);
2814
2815    bus_dmamap_unload(sc->ciss_buffer_dmat, cr->cr_datamap);
2816out:
2817    cr->cr_flags &= ~CISS_REQ_MAPPED;
2818}
2819
2820/************************************************************************
2821 * Attach the driver to CAM.
2822 *
2823 * We put all the logical drives on a single SCSI bus.
2824 */
2825static int
2826ciss_cam_init(struct ciss_softc *sc)
2827{
2828    int			i, maxbus;
2829
2830    debug_called(1);
2831
2832    /*
2833     * Allocate a devq.  We can reuse this for the masked physical
2834     * devices if we decide to export these as well.
2835     */
2836    if ((sc->ciss_cam_devq = cam_simq_alloc(sc->ciss_max_requests - 2)) == NULL) {
2837	ciss_printf(sc, "can't allocate CAM SIM queue\n");
2838	return(ENOMEM);
2839    }
2840
2841    /*
2842     * Create a SIM.
2843     *
2844     * This naturally wastes a bit of memory.  The alternative is to allocate
2845     * and register each bus as it is found, and then track them on a linked
2846     * list.  Unfortunately, the driver has a few places where it needs to
2847     * look up the SIM based solely on bus number, and it's unclear whether
2848     * a list traversal would work for these situations.
2849     */
2850    maxbus = max(sc->ciss_max_logical_bus, sc->ciss_max_physical_bus +
2851		 CISS_PHYSICAL_BASE);
2852    sc->ciss_cam_sim = malloc(maxbus * sizeof(struct cam_sim*),
2853			      CISS_MALLOC_CLASS, M_NOWAIT | M_ZERO);
2854    if (sc->ciss_cam_sim == NULL) {
2855	ciss_printf(sc, "can't allocate memory for controller SIM\n");
2856	return(ENOMEM);
2857    }
2858
2859    for (i = 0; i < sc->ciss_max_logical_bus; i++) {
2860	if ((sc->ciss_cam_sim[i] = cam_sim_alloc(ciss_cam_action, ciss_cam_poll,
2861						 "ciss", sc,
2862						 device_get_unit(sc->ciss_dev),
2863						 &sc->ciss_mtx,
2864						 2,
2865						 sc->ciss_max_requests - 2,
2866						 sc->ciss_cam_devq)) == NULL) {
2867	    ciss_printf(sc, "can't allocate CAM SIM for controller %d\n", i);
2868	    return(ENOMEM);
2869	}
2870
2871	/*
2872	 * Register bus with this SIM.
2873	 */
2874	mtx_lock(&sc->ciss_mtx);
2875	if (i == 0 || sc->ciss_controllers[i].physical.bus != 0) {
2876	    if (xpt_bus_register(sc->ciss_cam_sim[i], sc->ciss_dev, i) != 0) {
2877		ciss_printf(sc, "can't register SCSI bus %d\n", i);
2878		mtx_unlock(&sc->ciss_mtx);
2879		return (ENXIO);
2880	    }
2881	}
2882	mtx_unlock(&sc->ciss_mtx);
2883    }
2884
2885    for (i = CISS_PHYSICAL_BASE; i < sc->ciss_max_physical_bus +
2886	 CISS_PHYSICAL_BASE; i++) {
2887	if ((sc->ciss_cam_sim[i] = cam_sim_alloc(ciss_cam_action, ciss_cam_poll,
2888						 "ciss", sc,
2889						 device_get_unit(sc->ciss_dev),
2890						 &sc->ciss_mtx, 1,
2891						 sc->ciss_max_requests - 2,
2892						 sc->ciss_cam_devq)) == NULL) {
2893	    ciss_printf(sc, "can't allocate CAM SIM for controller %d\n", i);
2894	    return (ENOMEM);
2895	}
2896
2897	mtx_lock(&sc->ciss_mtx);
2898	if (xpt_bus_register(sc->ciss_cam_sim[i], sc->ciss_dev, i) != 0) {
2899	    ciss_printf(sc, "can't register SCSI bus %d\n", i);
2900	    mtx_unlock(&sc->ciss_mtx);
2901	    return (ENXIO);
2902	}
2903	mtx_unlock(&sc->ciss_mtx);
2904    }
2905
2906    return(0);
2907}
2908
2909/************************************************************************
2910 * Initiate a rescan of the 'logical devices' SIM
2911 */
2912static void
2913ciss_cam_rescan_target(struct ciss_softc *sc, int bus, int target)
2914{
2915    union ccb		*ccb;
2916
2917    debug_called(1);
2918
2919    if ((ccb = xpt_alloc_ccb_nowait()) == NULL) {
2920	ciss_printf(sc, "rescan failed (can't allocate CCB)\n");
2921	return;
2922    }
2923
2924    if (xpt_create_path(&ccb->ccb_h.path, NULL,
2925	    cam_sim_path(sc->ciss_cam_sim[bus]),
2926	    target, CAM_LUN_WILDCARD) != CAM_REQ_CMP) {
2927	ciss_printf(sc, "rescan failed (can't create path)\n");
2928	xpt_free_ccb(ccb);
2929	return;
2930    }
2931    xpt_rescan(ccb);
2932    /* scan is now in progress */
2933}
2934
2935/************************************************************************
2936 * Handle requests coming from CAM
2937 */
2938static void
2939ciss_cam_action(struct cam_sim *sim, union ccb *ccb)
2940{
2941    struct ciss_softc	*sc;
2942    struct ccb_scsiio	*csio;
2943    int			bus, target;
2944    int			physical;
2945
2946    sc = cam_sim_softc(sim);
2947    bus = cam_sim_bus(sim);
2948    csio = (struct ccb_scsiio *)&ccb->csio;
2949    target = csio->ccb_h.target_id;
2950    physical = CISS_IS_PHYSICAL(bus);
2951
2952    switch (ccb->ccb_h.func_code) {
2953
2954	/* perform SCSI I/O */
2955    case XPT_SCSI_IO:
2956	if (!ciss_cam_action_io(sim, csio))
2957	    return;
2958	break;
2959
2960	/* perform geometry calculations */
2961    case XPT_CALC_GEOMETRY:
2962    {
2963	struct ccb_calc_geometry	*ccg = &ccb->ccg;
2964	struct ciss_ldrive		*ld;
2965
2966	debug(1, "XPT_CALC_GEOMETRY %d:%d:%d", cam_sim_bus(sim), ccb->ccb_h.target_id, ccb->ccb_h.target_lun);
2967
2968	ld = NULL;
2969	if (!physical)
2970	    ld = &sc->ciss_logical[bus][target];
2971
2972	/*
2973	 * Use the cached geometry settings unless the fault tolerance
2974	 * is invalid.
2975	 */
2976	if (physical || ld->cl_geometry.fault_tolerance == 0xFF) {
2977	    u_int32_t			secs_per_cylinder;
2978
2979	    ccg->heads = 255;
2980	    ccg->secs_per_track = 32;
2981	    secs_per_cylinder = ccg->heads * ccg->secs_per_track;
2982	    ccg->cylinders = ccg->volume_size / secs_per_cylinder;
2983	} else {
2984	    ccg->heads = ld->cl_geometry.heads;
2985	    ccg->secs_per_track = ld->cl_geometry.sectors;
2986	    ccg->cylinders = ntohs(ld->cl_geometry.cylinders);
2987	}
2988	ccb->ccb_h.status = CAM_REQ_CMP;
2989        break;
2990    }
2991
2992	/* handle path attribute inquiry */
2993    case XPT_PATH_INQ:
2994    {
2995	struct ccb_pathinq	*cpi = &ccb->cpi;
2996	int			sg_length;
2997
2998	debug(1, "XPT_PATH_INQ %d:%d:%d", cam_sim_bus(sim), ccb->ccb_h.target_id, ccb->ccb_h.target_lun);
2999
3000	cpi->version_num = 1;
3001	cpi->hba_inquiry = PI_TAG_ABLE;	/* XXX is this correct? */
3002	cpi->target_sprt = 0;
3003	cpi->hba_misc = 0;
3004	cpi->max_target = sc->ciss_cfg->max_logical_supported;
3005	cpi->max_lun = 0;		/* 'logical drive' channel only */
3006	cpi->initiator_id = sc->ciss_cfg->max_logical_supported;
3007	strncpy(cpi->sim_vid, "FreeBSD", SIM_IDLEN);
3008        strncpy(cpi->hba_vid, "msmith@freebsd.org", HBA_IDLEN);
3009        strncpy(cpi->dev_name, cam_sim_name(sim), DEV_IDLEN);
3010        cpi->unit_number = cam_sim_unit(sim);
3011        cpi->bus_id = cam_sim_bus(sim);
3012	cpi->base_transfer_speed = 132 * 1024;	/* XXX what to set this to? */
3013	cpi->transport = XPORT_SPI;
3014	cpi->transport_version = 2;
3015	cpi->protocol = PROTO_SCSI;
3016	cpi->protocol_version = SCSI_REV_2;
3017	if (sc->ciss_cfg->max_sg_length == 0) {
3018		sg_length = 17;
3019	} else {
3020	/* XXX Fix for ZMR cards that advertise max_sg_length == 32
3021	 * Confusing bit here. max_sg_length is usually a power of 2. We always
3022	 * need to subtract 1 to account for partial pages. Then we need to
3023	 * align on a valid PAGE_SIZE so we round down to the nearest power of 2.
3024	 * Add 1 so we can then subtract it out in the assignment to maxio.
3025	 * The reason for all these shenanigans is to create a maxio value that
3026	 * creates IO operations to volumes that yield consistent operations
3027	 * with good performance.
3028	 */
3029		sg_length = sc->ciss_cfg->max_sg_length - 1;
3030		sg_length = (1 << (fls(sg_length) - 1)) + 1;
3031	}
3032	cpi->maxio = (min(CISS_MAX_SG_ELEMENTS, sg_length) - 1) * PAGE_SIZE;
3033	ccb->ccb_h.status = CAM_REQ_CMP;
3034	break;
3035    }
3036
3037    case XPT_GET_TRAN_SETTINGS:
3038    {
3039	struct ccb_trans_settings	*cts = &ccb->cts;
3040	int				bus, target;
3041	struct ccb_trans_settings_spi *spi = &cts->xport_specific.spi;
3042	struct ccb_trans_settings_scsi *scsi = &cts->proto_specific.scsi;
3043
3044	bus = cam_sim_bus(sim);
3045	target = cts->ccb_h.target_id;
3046
3047	debug(1, "XPT_GET_TRAN_SETTINGS %d:%d", bus, target);
3048	/* disconnect always OK */
3049	cts->protocol = PROTO_SCSI;
3050	cts->protocol_version = SCSI_REV_2;
3051	cts->transport = XPORT_SPI;
3052	cts->transport_version = 2;
3053
3054	spi->valid = CTS_SPI_VALID_DISC;
3055	spi->flags = CTS_SPI_FLAGS_DISC_ENB;
3056
3057	scsi->valid = CTS_SCSI_VALID_TQ;
3058	scsi->flags = CTS_SCSI_FLAGS_TAG_ENB;
3059
3060	cts->ccb_h.status = CAM_REQ_CMP;
3061	break;
3062    }
3063
3064    default:		/* we can't do this */
3065	debug(1, "unspported func_code = 0x%x", ccb->ccb_h.func_code);
3066	ccb->ccb_h.status = CAM_REQ_INVALID;
3067	break;
3068    }
3069
3070    xpt_done(ccb);
3071}
3072
3073/************************************************************************
3074 * Handle a CAM SCSI I/O request.
3075 */
3076static int
3077ciss_cam_action_io(struct cam_sim *sim, struct ccb_scsiio *csio)
3078{
3079    struct ciss_softc	*sc;
3080    int			bus, target;
3081    struct ciss_request	*cr;
3082    struct ciss_command	*cc;
3083    int			error;
3084
3085    sc = cam_sim_softc(sim);
3086    bus = cam_sim_bus(sim);
3087    target = csio->ccb_h.target_id;
3088
3089    debug(2, "XPT_SCSI_IO %d:%d:%d", bus, target, csio->ccb_h.target_lun);
3090
3091    /* check that the CDB pointer is not to a physical address */
3092    if ((csio->ccb_h.flags & CAM_CDB_POINTER) && (csio->ccb_h.flags & CAM_CDB_PHYS)) {
3093	debug(3, "  CDB pointer is to physical address");
3094	csio->ccb_h.status = CAM_REQ_CMP_ERR;
3095    }
3096
3097    /* abandon aborted ccbs or those that have failed validation */
3098    if ((csio->ccb_h.status & CAM_STATUS_MASK) != CAM_REQ_INPROG) {
3099	debug(3, "abandoning CCB due to abort/validation failure");
3100	return(EINVAL);
3101    }
3102
3103    /* handle emulation of some SCSI commands ourself */
3104    if (ciss_cam_emulate(sc, csio))
3105	return(0);
3106
3107    /*
3108     * Get a request to manage this command.  If we can't, return the
3109     * ccb, freeze the queue and flag so that we unfreeze it when a
3110     * request completes.
3111     */
3112    if ((error = ciss_get_request(sc, &cr)) != 0) {
3113	xpt_freeze_simq(sim, 1);
3114	sc->ciss_flags |= CISS_FLAG_BUSY;
3115	csio->ccb_h.status |= CAM_REQUEUE_REQ;
3116	return(error);
3117    }
3118
3119    /*
3120     * Build the command.
3121     */
3122    cc = cr->cr_cc;
3123    cr->cr_data = csio;
3124    cr->cr_length = csio->dxfer_len;
3125    cr->cr_complete = ciss_cam_complete;
3126    cr->cr_private = csio;
3127
3128    /*
3129     * Target the right logical volume.
3130     */
3131    if (CISS_IS_PHYSICAL(bus))
3132	cc->header.address =
3133	    sc->ciss_physical[CISS_CAM_TO_PBUS(bus)][target].cp_address;
3134    else
3135	cc->header.address =
3136	    sc->ciss_logical[bus][target].cl_address;
3137    cc->cdb.cdb_length = csio->cdb_len;
3138    cc->cdb.type = CISS_CDB_TYPE_COMMAND;
3139    cc->cdb.attribute = CISS_CDB_ATTRIBUTE_SIMPLE;	/* XXX ordered tags? */
3140    if ((csio->ccb_h.flags & CAM_DIR_MASK) == CAM_DIR_OUT) {
3141	cr->cr_flags = CISS_REQ_DATAOUT | CISS_REQ_CCB;
3142	cc->cdb.direction = CISS_CDB_DIRECTION_WRITE;
3143    } else if ((csio->ccb_h.flags & CAM_DIR_MASK) == CAM_DIR_IN) {
3144	cr->cr_flags = CISS_REQ_DATAIN | CISS_REQ_CCB;
3145	cc->cdb.direction = CISS_CDB_DIRECTION_READ;
3146    } else {
3147	cr->cr_data = NULL;
3148	cr->cr_flags = 0;
3149	cc->cdb.direction = CISS_CDB_DIRECTION_NONE;
3150    }
3151    cc->cdb.timeout = (csio->ccb_h.timeout / 1000) + 1;
3152    if (csio->ccb_h.flags & CAM_CDB_POINTER) {
3153	bcopy(csio->cdb_io.cdb_ptr, &cc->cdb.cdb[0], csio->cdb_len);
3154    } else {
3155	bcopy(csio->cdb_io.cdb_bytes, &cc->cdb.cdb[0], csio->cdb_len);
3156    }
3157
3158    /*
3159     * Submit the request to the adapter.
3160     *
3161     * Note that this may fail if we're unable to map the request (and
3162     * if we ever learn a transport layer other than simple, may fail
3163     * if the adapter rejects the command).
3164     */
3165    if ((error = ciss_start(cr)) != 0) {
3166	xpt_freeze_simq(sim, 1);
3167	csio->ccb_h.status |= CAM_RELEASE_SIMQ;
3168	if (error == EINPROGRESS) {
3169	    error = 0;
3170	} else {
3171	    csio->ccb_h.status |= CAM_REQUEUE_REQ;
3172	    ciss_release_request(cr);
3173	}
3174	return(error);
3175    }
3176
3177    return(0);
3178}
3179
3180/************************************************************************
3181 * Emulate SCSI commands the adapter doesn't handle as we might like.
3182 */
3183static int
3184ciss_cam_emulate(struct ciss_softc *sc, struct ccb_scsiio *csio)
3185{
3186    int		bus, target;
3187    u_int8_t	opcode;
3188
3189    target = csio->ccb_h.target_id;
3190    bus = cam_sim_bus(xpt_path_sim(csio->ccb_h.path));
3191    opcode = (csio->ccb_h.flags & CAM_CDB_POINTER) ?
3192	*(u_int8_t *)csio->cdb_io.cdb_ptr : csio->cdb_io.cdb_bytes[0];
3193
3194    if (CISS_IS_PHYSICAL(bus)) {
3195	if (sc->ciss_physical[CISS_CAM_TO_PBUS(bus)][target].cp_online != 1) {
3196	    csio->ccb_h.status |= CAM_SEL_TIMEOUT;
3197	    xpt_done((union ccb *)csio);
3198	    return(1);
3199	} else
3200	    return(0);
3201    }
3202
3203    /*
3204     * Handle requests for volumes that don't exist or are not online.
3205     * A selection timeout is slightly better than an illegal request.
3206     * Other errors might be better.
3207     */
3208    if (sc->ciss_logical[bus][target].cl_status != CISS_LD_ONLINE) {
3209	csio->ccb_h.status |= CAM_SEL_TIMEOUT;
3210	xpt_done((union ccb *)csio);
3211	return(1);
3212    }
3213
3214    /* if we have to fake Synchronise Cache */
3215    if (sc->ciss_flags & CISS_FLAG_FAKE_SYNCH) {
3216	/*
3217	 * If this is a Synchronise Cache command, typically issued when
3218	 * a device is closed, flush the adapter and complete now.
3219	 */
3220	if (((csio->ccb_h.flags & CAM_CDB_POINTER) ?
3221	     *(u_int8_t *)csio->cdb_io.cdb_ptr : csio->cdb_io.cdb_bytes[0]) == SYNCHRONIZE_CACHE) {
3222	    ciss_flush_adapter(sc);
3223	    csio->ccb_h.status |= CAM_REQ_CMP;
3224	    xpt_done((union ccb *)csio);
3225	    return(1);
3226	}
3227    }
3228
3229    /*
3230     * A CISS target can only ever have one lun per target. REPORT_LUNS requires
3231     * at least one LUN field to be pre created for us, so snag it and fill in
3232     * the least significant byte indicating 1 LUN here.  Emulate the command
3233     * return to shut up warning on console of a CDB error.  swb
3234     */
3235    if (opcode == REPORT_LUNS && csio->dxfer_len > 0) {
3236       csio->data_ptr[3] = 8;
3237       csio->ccb_h.status |= CAM_REQ_CMP;
3238       xpt_done((union ccb *)csio);
3239       return(1);
3240    }
3241
3242    return(0);
3243}
3244
3245/************************************************************************
3246 * Check for possibly-completed commands.
3247 */
3248static void
3249ciss_cam_poll(struct cam_sim *sim)
3250{
3251    cr_qhead_t qh;
3252    struct ciss_softc	*sc = cam_sim_softc(sim);
3253
3254    debug_called(2);
3255
3256    STAILQ_INIT(&qh);
3257    if (sc->ciss_perf)
3258	ciss_perf_done(sc, &qh);
3259    else
3260	ciss_done(sc, &qh);
3261    ciss_complete(sc, &qh);
3262}
3263
3264/************************************************************************
3265 * Handle completion of a command - pass results back through the CCB
3266 */
3267static void
3268ciss_cam_complete(struct ciss_request *cr)
3269{
3270    struct ciss_softc		*sc;
3271    struct ciss_command		*cc;
3272    struct ciss_error_info	*ce;
3273    struct ccb_scsiio		*csio;
3274    int				scsi_status;
3275    int				command_status;
3276
3277    debug_called(2);
3278
3279    sc = cr->cr_sc;
3280    cc = cr->cr_cc;
3281    ce = (struct ciss_error_info *)&(cc->sg[0]);
3282    csio = (struct ccb_scsiio *)cr->cr_private;
3283
3284    /*
3285     * Extract status values from request.
3286     */
3287    ciss_report_request(cr, &command_status, &scsi_status);
3288    csio->scsi_status = scsi_status;
3289
3290    /*
3291     * Handle specific SCSI status values.
3292     */
3293    switch(scsi_status) {
3294	/* no status due to adapter error */
3295    case -1:
3296	debug(0, "adapter error");
3297	csio->ccb_h.status |= CAM_REQ_CMP_ERR;
3298	break;
3299
3300	/* no status due to command completed OK */
3301    case SCSI_STATUS_OK:		/* CISS_SCSI_STATUS_GOOD */
3302	debug(2, "SCSI_STATUS_OK");
3303	csio->ccb_h.status |= CAM_REQ_CMP;
3304	break;
3305
3306	/* check condition, sense data included */
3307    case SCSI_STATUS_CHECK_COND:	/* CISS_SCSI_STATUS_CHECK_CONDITION */
3308	debug(0, "SCSI_STATUS_CHECK_COND  sense size %d  resid %d\n",
3309	      ce->sense_length, ce->residual_count);
3310	bzero(&csio->sense_data, SSD_FULL_SIZE);
3311	bcopy(&ce->sense_info[0], &csio->sense_data, ce->sense_length);
3312	if (csio->sense_len > ce->sense_length)
3313		csio->sense_resid = csio->sense_len - ce->sense_length;
3314	else
3315		csio->sense_resid = 0;
3316	csio->resid = ce->residual_count;
3317	csio->ccb_h.status |= CAM_SCSI_STATUS_ERROR | CAM_AUTOSNS_VALID;
3318#ifdef CISS_DEBUG
3319	{
3320	    struct scsi_sense_data	*sns = (struct scsi_sense_data *)&ce->sense_info[0];
3321	    debug(0, "sense key %x", scsi_get_sense_key(sns, csio->sense_len -
3322		  csio->sense_resid, /*show_errors*/ 1));
3323	}
3324#endif
3325	break;
3326
3327    case SCSI_STATUS_BUSY:		/* CISS_SCSI_STATUS_BUSY */
3328	debug(0, "SCSI_STATUS_BUSY");
3329	csio->ccb_h.status |= CAM_SCSI_BUSY;
3330	break;
3331
3332    default:
3333	debug(0, "unknown status 0x%x", csio->scsi_status);
3334	csio->ccb_h.status |= CAM_REQ_CMP_ERR;
3335	break;
3336    }
3337
3338    /* handle post-command fixup */
3339    ciss_cam_complete_fixup(sc, csio);
3340
3341    ciss_release_request(cr);
3342    if (sc->ciss_flags & CISS_FLAG_BUSY) {
3343	sc->ciss_flags &= ~CISS_FLAG_BUSY;
3344	if (csio->ccb_h.status & CAM_RELEASE_SIMQ)
3345	    xpt_release_simq(xpt_path_sim(csio->ccb_h.path), 0);
3346	else
3347	    csio->ccb_h.status |= CAM_RELEASE_SIMQ;
3348    }
3349    xpt_done((union ccb *)csio);
3350}
3351
3352/********************************************************************************
3353 * Fix up the result of some commands here.
3354 */
3355static void
3356ciss_cam_complete_fixup(struct ciss_softc *sc, struct ccb_scsiio *csio)
3357{
3358    struct scsi_inquiry_data	*inq;
3359    struct ciss_ldrive		*cl;
3360    uint8_t			*cdb;
3361    int				bus, target;
3362
3363    cdb = (csio->ccb_h.flags & CAM_CDB_POINTER) ?
3364	 (uint8_t *)csio->cdb_io.cdb_ptr : csio->cdb_io.cdb_bytes;
3365    if (cdb[0] == INQUIRY &&
3366	(cdb[1] & SI_EVPD) == 0 &&
3367	(csio->ccb_h.flags & CAM_DIR_MASK) == CAM_DIR_IN &&
3368	csio->dxfer_len >= SHORT_INQUIRY_LENGTH) {
3369
3370	inq = (struct scsi_inquiry_data *)csio->data_ptr;
3371	target = csio->ccb_h.target_id;
3372	bus = cam_sim_bus(xpt_path_sim(csio->ccb_h.path));
3373
3374	/*
3375	 * Don't let hard drives be seen by the DA driver.  They will still be
3376	 * attached by the PASS driver.
3377	 */
3378	if (CISS_IS_PHYSICAL(bus)) {
3379	    if (SID_TYPE(inq) == T_DIRECT)
3380		inq->device = (inq->device & 0xe0) | T_NODEVICE;
3381	    return;
3382	}
3383
3384	cl = &sc->ciss_logical[bus][target];
3385
3386	padstr(inq->vendor, "HP",
3387	       SID_VENDOR_SIZE);
3388	padstr(inq->product,
3389	       ciss_name_ldrive_org(cl->cl_ldrive->fault_tolerance),
3390	       SID_PRODUCT_SIZE);
3391	padstr(inq->revision,
3392	       ciss_name_ldrive_status(cl->cl_lstatus->status),
3393	       SID_REVISION_SIZE);
3394    }
3395}
3396
3397
3398/********************************************************************************
3399 * Name the device at (target)
3400 *
3401 * XXX is this strictly correct?
3402 */
3403static int
3404ciss_name_device(struct ciss_softc *sc, int bus, int target)
3405{
3406    struct cam_periph	*periph;
3407    struct cam_path	*path;
3408    int			status;
3409
3410    if (CISS_IS_PHYSICAL(bus))
3411	return (0);
3412
3413    status = xpt_create_path(&path, NULL, cam_sim_path(sc->ciss_cam_sim[bus]),
3414			     target, 0);
3415
3416    if (status == CAM_REQ_CMP) {
3417    	mtx_lock(&sc->ciss_mtx);
3418	periph = cam_periph_find(path, NULL);
3419	sprintf(sc->ciss_logical[bus][target].cl_name, "%s%d",
3420		periph->periph_name, periph->unit_number);
3421    	mtx_unlock(&sc->ciss_mtx);
3422	xpt_free_path(path);
3423	return(0);
3424    }
3425    sc->ciss_logical[bus][target].cl_name[0] = 0;
3426    return(ENOENT);
3427}
3428
3429/************************************************************************
3430 * Periodic status monitoring.
3431 */
3432static void
3433ciss_periodic(void *arg)
3434{
3435    struct ciss_softc	*sc;
3436    struct ciss_request	*cr = NULL;
3437    struct ciss_command	*cc = NULL;
3438    int			error = 0;
3439
3440    debug_called(1);
3441
3442    sc = (struct ciss_softc *)arg;
3443
3444    /*
3445     * Check the adapter heartbeat.
3446     */
3447    if (sc->ciss_cfg->heartbeat == sc->ciss_heartbeat) {
3448	sc->ciss_heart_attack++;
3449	debug(0, "adapter heart attack in progress 0x%x/%d",
3450	      sc->ciss_heartbeat, sc->ciss_heart_attack);
3451	if (sc->ciss_heart_attack == 3) {
3452	    ciss_printf(sc, "ADAPTER HEARTBEAT FAILED\n");
3453	    ciss_disable_adapter(sc);
3454	    return;
3455	}
3456    } else {
3457	sc->ciss_heartbeat = sc->ciss_cfg->heartbeat;
3458	sc->ciss_heart_attack = 0;
3459	debug(3, "new heartbeat 0x%x", sc->ciss_heartbeat);
3460    }
3461
3462    /*
3463     * Send the NOP message and wait for a response.
3464     */
3465    if (ciss_nop_message_heartbeat != 0 && (error = ciss_get_request(sc, &cr)) == 0) {
3466	cc = cr->cr_cc;
3467	cr->cr_complete = ciss_nop_complete;
3468	cc->cdb.cdb_length = 1;
3469	cc->cdb.type = CISS_CDB_TYPE_MESSAGE;
3470	cc->cdb.attribute = CISS_CDB_ATTRIBUTE_SIMPLE;
3471	cc->cdb.direction = CISS_CDB_DIRECTION_WRITE;
3472	cc->cdb.timeout = 0;
3473	cc->cdb.cdb[0] = CISS_OPCODE_MESSAGE_NOP;
3474
3475	if ((error = ciss_start(cr)) != 0) {
3476	    ciss_printf(sc, "SENDING NOP MESSAGE FAILED\n");
3477	}
3478    }
3479
3480    /*
3481     * If the notify event request has died for some reason, or has
3482     * not started yet, restart it.
3483     */
3484    if (!(sc->ciss_flags & CISS_FLAG_NOTIFY_OK)) {
3485	debug(0, "(re)starting Event Notify chain");
3486	ciss_notify_event(sc);
3487    }
3488
3489    /*
3490     * Reschedule.
3491     */
3492    callout_reset(&sc->ciss_periodic, CISS_HEARTBEAT_RATE * hz, ciss_periodic, sc);
3493}
3494
3495static void
3496ciss_nop_complete(struct ciss_request *cr)
3497{
3498    struct ciss_softc		*sc;
3499    static int			first_time = 1;
3500
3501    sc = cr->cr_sc;
3502    if (ciss_report_request(cr, NULL, NULL) != 0) {
3503	if (first_time == 1) {
3504	    first_time = 0;
3505	    ciss_printf(sc, "SENDING NOP MESSAGE FAILED (not logging anymore)\n");
3506	}
3507    }
3508
3509    ciss_release_request(cr);
3510}
3511
3512/************************************************************************
3513 * Disable the adapter.
3514 *
3515 * The all requests in completed queue is failed with hardware error.
3516 * This will cause failover in a multipath configuration.
3517 */
3518static void
3519ciss_disable_adapter(struct ciss_softc *sc)
3520{
3521    cr_qhead_t			qh;
3522    struct ciss_request		*cr;
3523    struct ciss_command		*cc;
3524    struct ciss_error_info	*ce;
3525    int				i;
3526
3527    CISS_TL_SIMPLE_DISABLE_INTERRUPTS(sc);
3528    pci_disable_busmaster(sc->ciss_dev);
3529    sc->ciss_flags &= ~CISS_FLAG_RUNNING;
3530
3531    for (i = 1; i < sc->ciss_max_requests; i++) {
3532	cr = &sc->ciss_request[i];
3533	if ((cr->cr_flags & CISS_REQ_BUSY) == 0)
3534	    continue;
3535
3536	cc = cr->cr_cc;
3537	ce = (struct ciss_error_info *)&(cc->sg[0]);
3538	ce->command_status = CISS_CMD_STATUS_HARDWARE_ERROR;
3539	ciss_enqueue_complete(cr, &qh);
3540    }
3541
3542    for (;;) {
3543	if ((cr = ciss_dequeue_complete(sc, &qh)) == NULL)
3544	    break;
3545
3546	/*
3547	 * If the request has a callback, invoke it.
3548	 */
3549	if (cr->cr_complete != NULL) {
3550	    cr->cr_complete(cr);
3551	    continue;
3552	}
3553
3554	/*
3555	 * If someone is sleeping on this request, wake them up.
3556	 */
3557	if (cr->cr_flags & CISS_REQ_SLEEP) {
3558	    cr->cr_flags &= ~CISS_REQ_SLEEP;
3559	    wakeup(cr);
3560	    continue;
3561	}
3562    }
3563}
3564
3565/************************************************************************
3566 * Request a notification response from the adapter.
3567 *
3568 * If (cr) is NULL, this is the first request of the adapter, so
3569 * reset the adapter's message pointer and start with the oldest
3570 * message available.
3571 */
3572static void
3573ciss_notify_event(struct ciss_softc *sc)
3574{
3575    struct ciss_request		*cr;
3576    struct ciss_command		*cc;
3577    struct ciss_notify_cdb	*cnc;
3578    int				error;
3579
3580    debug_called(1);
3581
3582    cr = sc->ciss_periodic_notify;
3583
3584    /* get a request if we don't already have one */
3585    if (cr == NULL) {
3586	if ((error = ciss_get_request(sc, &cr)) != 0) {
3587	    debug(0, "can't get notify event request");
3588	    goto out;
3589	}
3590	sc->ciss_periodic_notify = cr;
3591	cr->cr_complete = ciss_notify_complete;
3592	debug(1, "acquired request %d", cr->cr_tag);
3593    }
3594
3595    /*
3596     * Get a databuffer if we don't already have one, note that the
3597     * adapter command wants a larger buffer than the actual
3598     * structure.
3599     */
3600    if (cr->cr_data == NULL) {
3601	if ((cr->cr_data = malloc(CISS_NOTIFY_DATA_SIZE, CISS_MALLOC_CLASS, M_NOWAIT)) == NULL) {
3602	    debug(0, "can't get notify event request buffer");
3603	    error = ENOMEM;
3604	    goto out;
3605	}
3606	cr->cr_length = CISS_NOTIFY_DATA_SIZE;
3607    }
3608
3609    /* re-setup the request's command (since we never release it) XXX overkill*/
3610    ciss_preen_command(cr);
3611
3612    /* (re)build the notify event command */
3613    cc = cr->cr_cc;
3614    cc->header.address.physical.mode = CISS_HDR_ADDRESS_MODE_PERIPHERAL;
3615    cc->header.address.physical.bus = 0;
3616    cc->header.address.physical.target = 0;
3617
3618    cc->cdb.cdb_length = sizeof(*cnc);
3619    cc->cdb.type = CISS_CDB_TYPE_COMMAND;
3620    cc->cdb.attribute = CISS_CDB_ATTRIBUTE_SIMPLE;
3621    cc->cdb.direction = CISS_CDB_DIRECTION_READ;
3622    cc->cdb.timeout = 0;	/* no timeout, we hope */
3623
3624    cnc = (struct ciss_notify_cdb *)&(cc->cdb.cdb[0]);
3625    bzero(cr->cr_data, CISS_NOTIFY_DATA_SIZE);
3626    cnc->opcode = CISS_OPCODE_READ;
3627    cnc->command = CISS_COMMAND_NOTIFY_ON_EVENT;
3628    cnc->timeout = 0;		/* no timeout, we hope */
3629    cnc->synchronous = 0;
3630    cnc->ordered = 0;
3631    cnc->seek_to_oldest = 0;
3632    if ((sc->ciss_flags & CISS_FLAG_RUNNING) == 0)
3633	cnc->new_only = 1;
3634    else
3635	cnc->new_only = 0;
3636    cnc->length = htonl(CISS_NOTIFY_DATA_SIZE);
3637
3638    /* submit the request */
3639    error = ciss_start(cr);
3640
3641 out:
3642    if (error) {
3643	if (cr != NULL) {
3644	    if (cr->cr_data != NULL)
3645		free(cr->cr_data, CISS_MALLOC_CLASS);
3646	    ciss_release_request(cr);
3647	}
3648	sc->ciss_periodic_notify = NULL;
3649	debug(0, "can't submit notify event request");
3650	sc->ciss_flags &= ~CISS_FLAG_NOTIFY_OK;
3651    } else {
3652	debug(1, "notify event submitted");
3653	sc->ciss_flags |= CISS_FLAG_NOTIFY_OK;
3654    }
3655}
3656
3657static void
3658ciss_notify_complete(struct ciss_request *cr)
3659{
3660    struct ciss_command	*cc;
3661    struct ciss_notify	*cn;
3662    struct ciss_softc	*sc;
3663    int			scsi_status;
3664    int			command_status;
3665    debug_called(1);
3666
3667    cc = cr->cr_cc;
3668    cn = (struct ciss_notify *)cr->cr_data;
3669    sc = cr->cr_sc;
3670
3671    /*
3672     * Report request results, decode status.
3673     */
3674    ciss_report_request(cr, &command_status, &scsi_status);
3675
3676    /*
3677     * Abort the chain on a fatal error.
3678     *
3679     * XXX which of these are actually errors?
3680     */
3681    if ((command_status != CISS_CMD_STATUS_SUCCESS) &&
3682	(command_status != CISS_CMD_STATUS_TARGET_STATUS) &&
3683	(command_status != CISS_CMD_STATUS_TIMEOUT)) {	/* XXX timeout? */
3684	ciss_printf(sc, "fatal error in Notify Event request (%s)\n",
3685		    ciss_name_command_status(command_status));
3686	ciss_release_request(cr);
3687	sc->ciss_flags &= ~CISS_FLAG_NOTIFY_OK;
3688	return;
3689    }
3690
3691    /*
3692     * If the adapter gave us a text message, print it.
3693     */
3694    if (cn->message[0] != 0)
3695	ciss_printf(sc, "*** %.80s\n", cn->message);
3696
3697    debug(0, "notify event class %d subclass %d detail %d",
3698		cn->class, cn->subclass, cn->detail);
3699
3700    /*
3701     * If the response indicates that the notifier has been aborted,
3702     * release the notifier command.
3703     */
3704    if ((cn->class == CISS_NOTIFY_NOTIFIER) &&
3705	(cn->subclass == CISS_NOTIFY_NOTIFIER_STATUS) &&
3706	(cn->detail == 1)) {
3707	debug(0, "notifier exiting");
3708	sc->ciss_flags &= ~CISS_FLAG_NOTIFY_OK;
3709	ciss_release_request(cr);
3710	sc->ciss_periodic_notify = NULL;
3711	wakeup(&sc->ciss_periodic_notify);
3712    } else {
3713	/* Handle notify events in a kernel thread */
3714	ciss_enqueue_notify(cr);
3715	sc->ciss_periodic_notify = NULL;
3716	wakeup(&sc->ciss_periodic_notify);
3717	wakeup(&sc->ciss_notify);
3718    }
3719    /*
3720     * Send a new notify event command, if we're not aborting.
3721     */
3722    if (!(sc->ciss_flags & CISS_FLAG_ABORTING)) {
3723	ciss_notify_event(sc);
3724    }
3725}
3726
3727/************************************************************************
3728 * Abort the Notify Event chain.
3729 *
3730 * Note that we can't just abort the command in progress; we have to
3731 * explicitly issue an Abort Notify Event command in order for the
3732 * adapter to clean up correctly.
3733 *
3734 * If we are called with CISS_FLAG_ABORTING set in the adapter softc,
3735 * the chain will not restart itself.
3736 */
3737static int
3738ciss_notify_abort(struct ciss_softc *sc)
3739{
3740    struct ciss_request		*cr;
3741    struct ciss_command		*cc;
3742    struct ciss_notify_cdb	*cnc;
3743    int				error, command_status, scsi_status;
3744
3745    debug_called(1);
3746
3747    cr = NULL;
3748    error = 0;
3749
3750    /* verify that there's an outstanding command */
3751    if (!(sc->ciss_flags & CISS_FLAG_NOTIFY_OK))
3752	goto out;
3753
3754    /* get a command to issue the abort with */
3755    if ((error = ciss_get_request(sc, &cr)))
3756	goto out;
3757
3758    /* get a buffer for the result */
3759    if ((cr->cr_data = malloc(CISS_NOTIFY_DATA_SIZE, CISS_MALLOC_CLASS, M_NOWAIT)) == NULL) {
3760	debug(0, "can't get notify event request buffer");
3761	error = ENOMEM;
3762	goto out;
3763    }
3764    cr->cr_length = CISS_NOTIFY_DATA_SIZE;
3765
3766    /* build the CDB */
3767    cc = cr->cr_cc;
3768    cc->header.address.physical.mode = CISS_HDR_ADDRESS_MODE_PERIPHERAL;
3769    cc->header.address.physical.bus = 0;
3770    cc->header.address.physical.target = 0;
3771    cc->cdb.cdb_length = sizeof(*cnc);
3772    cc->cdb.type = CISS_CDB_TYPE_COMMAND;
3773    cc->cdb.attribute = CISS_CDB_ATTRIBUTE_SIMPLE;
3774    cc->cdb.direction = CISS_CDB_DIRECTION_READ;
3775    cc->cdb.timeout = 0;	/* no timeout, we hope */
3776
3777    cnc = (struct ciss_notify_cdb *)&(cc->cdb.cdb[0]);
3778    bzero(cnc, sizeof(*cnc));
3779    cnc->opcode = CISS_OPCODE_WRITE;
3780    cnc->command = CISS_COMMAND_ABORT_NOTIFY;
3781    cnc->length = htonl(CISS_NOTIFY_DATA_SIZE);
3782
3783    ciss_print_request(cr);
3784
3785    /*
3786     * Submit the request and wait for it to complete.
3787     */
3788    if ((error = ciss_synch_request(cr, 60 * 1000)) != 0) {
3789	ciss_printf(sc, "Abort Notify Event command failed (%d)\n", error);
3790	goto out;
3791    }
3792
3793    /*
3794     * Check response.
3795     */
3796    ciss_report_request(cr, &command_status, &scsi_status);
3797    switch(command_status) {
3798    case CISS_CMD_STATUS_SUCCESS:
3799	break;
3800    case CISS_CMD_STATUS_INVALID_COMMAND:
3801	/*
3802	 * Some older adapters don't support the CISS version of this
3803	 * command.  Fall back to using the BMIC version.
3804	 */
3805	error = ciss_notify_abort_bmic(sc);
3806	if (error != 0)
3807	    goto out;
3808	break;
3809
3810    case CISS_CMD_STATUS_TARGET_STATUS:
3811	/*
3812	 * This can happen if the adapter thinks there wasn't an outstanding
3813	 * Notify Event command but we did.  We clean up here.
3814	 */
3815	if (scsi_status == CISS_SCSI_STATUS_CHECK_CONDITION) {
3816	    if (sc->ciss_periodic_notify != NULL)
3817		ciss_release_request(sc->ciss_periodic_notify);
3818	    error = 0;
3819	    goto out;
3820	}
3821	/* FALLTHROUGH */
3822
3823    default:
3824	ciss_printf(sc, "Abort Notify Event command failed (%s)\n",
3825		    ciss_name_command_status(command_status));
3826	error = EIO;
3827	goto out;
3828    }
3829
3830    /*
3831     * Sleep waiting for the notifier command to complete.  Note
3832     * that if it doesn't, we may end up in a bad situation, since
3833     * the adapter may deliver it later.  Also note that the adapter
3834     * requires the Notify Event command to be cancelled in order to
3835     * maintain internal bookkeeping.
3836     */
3837    while (sc->ciss_periodic_notify != NULL) {
3838	error = msleep(&sc->ciss_periodic_notify, &sc->ciss_mtx, PRIBIO, "cissNEA", hz * 5);
3839	if (error == EWOULDBLOCK) {
3840	    ciss_printf(sc, "Notify Event command failed to abort, adapter may wedge.\n");
3841	    break;
3842	}
3843    }
3844
3845 out:
3846    /* release the cancel request */
3847    if (cr != NULL) {
3848	if (cr->cr_data != NULL)
3849	    free(cr->cr_data, CISS_MALLOC_CLASS);
3850	ciss_release_request(cr);
3851    }
3852    if (error == 0)
3853	sc->ciss_flags &= ~CISS_FLAG_NOTIFY_OK;
3854    return(error);
3855}
3856
3857/************************************************************************
3858 * Abort the Notify Event chain using a BMIC command.
3859 */
3860static int
3861ciss_notify_abort_bmic(struct ciss_softc *sc)
3862{
3863    struct ciss_request			*cr;
3864    int					error, command_status;
3865
3866    debug_called(1);
3867
3868    cr = NULL;
3869    error = 0;
3870
3871    /* verify that there's an outstanding command */
3872    if (!(sc->ciss_flags & CISS_FLAG_NOTIFY_OK))
3873	goto out;
3874
3875    /*
3876     * Build a BMIC command to cancel the Notify on Event command.
3877     *
3878     * Note that we are sending a CISS opcode here.  Odd.
3879     */
3880    if ((error = ciss_get_bmic_request(sc, &cr, CISS_COMMAND_ABORT_NOTIFY,
3881				       NULL, 0)) != 0)
3882	goto out;
3883
3884    /*
3885     * Submit the request and wait for it to complete.
3886     */
3887    if ((error = ciss_synch_request(cr, 60 * 1000)) != 0) {
3888	ciss_printf(sc, "error sending BMIC Cancel Notify on Event command (%d)\n", error);
3889	goto out;
3890    }
3891
3892    /*
3893     * Check response.
3894     */
3895    ciss_report_request(cr, &command_status, NULL);
3896    switch(command_status) {
3897    case CISS_CMD_STATUS_SUCCESS:
3898	break;
3899    default:
3900	ciss_printf(sc, "error cancelling Notify on Event (%s)\n",
3901		    ciss_name_command_status(command_status));
3902	error = EIO;
3903	goto out;
3904    }
3905
3906out:
3907    if (cr != NULL)
3908	ciss_release_request(cr);
3909    return(error);
3910}
3911
3912/************************************************************************
3913 * Handle rescanning all the logical volumes when a notify event
3914 * causes the drives to come online or offline.
3915 */
3916static void
3917ciss_notify_rescan_logical(struct ciss_softc *sc)
3918{
3919    struct ciss_lun_report      *cll;
3920    struct ciss_ldrive		*ld;
3921    int                         i, j, ndrives;
3922
3923    /*
3924     * We must rescan all logical volumes to get the right logical
3925     * drive address.
3926     */
3927    cll = ciss_report_luns(sc, CISS_OPCODE_REPORT_LOGICAL_LUNS,
3928                           sc->ciss_cfg->max_logical_supported);
3929    if (cll == NULL)
3930        return;
3931
3932    ndrives = (ntohl(cll->list_size) / sizeof(union ciss_device_address));
3933
3934    /*
3935     * Delete any of the drives which were destroyed by the
3936     * firmware.
3937     */
3938    for (i = 0; i < sc->ciss_max_logical_bus; i++) {
3939	for (j = 0; j < sc->ciss_cfg->max_logical_supported; j++) {
3940	    ld = &sc->ciss_logical[i][j];
3941
3942	    if (ld->cl_update == 0)
3943		continue;
3944
3945	    if (ld->cl_status != CISS_LD_ONLINE) {
3946		ciss_cam_rescan_target(sc, i, j);
3947		ld->cl_update = 0;
3948		if (ld->cl_ldrive)
3949		    free(ld->cl_ldrive, CISS_MALLOC_CLASS);
3950		if (ld->cl_lstatus)
3951		    free(ld->cl_lstatus, CISS_MALLOC_CLASS);
3952
3953		ld->cl_ldrive = NULL;
3954		ld->cl_lstatus = NULL;
3955	    }
3956	}
3957    }
3958
3959    /*
3960     * Scan for new drives.
3961     */
3962    for (i = 0; i < ndrives; i++) {
3963	int	bus, target;
3964
3965	bus 	= CISS_LUN_TO_BUS(cll->lun[i].logical.lun);
3966	target	= CISS_LUN_TO_TARGET(cll->lun[i].logical.lun);
3967	ld	= &sc->ciss_logical[bus][target];
3968
3969	if (ld->cl_update == 0)
3970		continue;
3971
3972	ld->cl_update		= 0;
3973	ld->cl_address		= cll->lun[i];
3974	ld->cl_controller	= &sc->ciss_controllers[bus];
3975	if (ciss_identify_logical(sc, ld) == 0) {
3976	    ciss_cam_rescan_target(sc, bus, target);
3977	}
3978    }
3979    free(cll, CISS_MALLOC_CLASS);
3980}
3981
3982/************************************************************************
3983 * Handle a notify event relating to the status of a logical drive.
3984 *
3985 * XXX need to be able to defer some of these to properly handle
3986 *     calling the "ID Physical drive" command, unless the 'extended'
3987 *     drive IDs are always in BIG_MAP format.
3988 */
3989static void
3990ciss_notify_logical(struct ciss_softc *sc, struct ciss_notify *cn)
3991{
3992    struct ciss_ldrive	*ld;
3993    int			bus, target;
3994    int			rescan_ld;
3995
3996    debug_called(2);
3997
3998    bus		= cn->device.physical.bus;
3999    target	= cn->data.logical_status.logical_drive;
4000    ld		= &sc->ciss_logical[bus][target];
4001
4002    switch (cn->subclass) {
4003    case CISS_NOTIFY_LOGICAL_STATUS:
4004	switch (cn->detail) {
4005	case 0:
4006	    ciss_name_device(sc, bus, target);
4007	    ciss_printf(sc, "logical drive %d (%s) changed status %s->%s, spare status 0x%b\n",
4008			cn->data.logical_status.logical_drive, ld->cl_name,
4009			ciss_name_ldrive_status(cn->data.logical_status.previous_state),
4010			ciss_name_ldrive_status(cn->data.logical_status.new_state),
4011			cn->data.logical_status.spare_state,
4012			"\20\1configured\2rebuilding\3failed\4in use\5available\n");
4013
4014	    /*
4015	     * Update our idea of the drive's status.
4016	     */
4017	    ld->cl_status = ciss_decode_ldrive_status(cn->data.logical_status.new_state);
4018	    if (ld->cl_lstatus != NULL)
4019		ld->cl_lstatus->status = cn->data.logical_status.new_state;
4020
4021	    /*
4022	     * Have CAM rescan the drive if its status has changed.
4023	     */
4024            rescan_ld = (cn->data.logical_status.previous_state !=
4025                         cn->data.logical_status.new_state) ? 1 : 0;
4026	    if (rescan_ld) {
4027		ld->cl_update = 1;
4028		ciss_notify_rescan_logical(sc);
4029	    }
4030
4031	    break;
4032
4033	case 1:	/* logical drive has recognised new media, needs Accept Media Exchange */
4034	    ciss_name_device(sc, bus, target);
4035	    ciss_printf(sc, "logical drive %d (%s) media exchanged, ready to go online\n",
4036			cn->data.logical_status.logical_drive, ld->cl_name);
4037	    ciss_accept_media(sc, ld);
4038
4039	    ld->cl_update = 1;
4040	    ld->cl_status = ciss_decode_ldrive_status(cn->data.logical_status.new_state);
4041	    ciss_notify_rescan_logical(sc);
4042	    break;
4043
4044	case 2:
4045	case 3:
4046	    ciss_printf(sc, "rebuild of logical drive %d (%s) failed due to %s error\n",
4047			cn->data.rebuild_aborted.logical_drive,
4048			ld->cl_name,
4049			(cn->detail == 2) ? "read" : "write");
4050	    break;
4051	}
4052	break;
4053
4054    case CISS_NOTIFY_LOGICAL_ERROR:
4055	if (cn->detail == 0) {
4056	    ciss_printf(sc, "FATAL I/O ERROR on logical drive %d (%s), SCSI port %d ID %d\n",
4057			cn->data.io_error.logical_drive,
4058			ld->cl_name,
4059			cn->data.io_error.failure_bus,
4060			cn->data.io_error.failure_drive);
4061	    /* XXX should we take the drive down at this point, or will we be told? */
4062	}
4063	break;
4064
4065    case CISS_NOTIFY_LOGICAL_SURFACE:
4066	if (cn->detail == 0)
4067	    ciss_printf(sc, "logical drive %d (%s) completed consistency initialisation\n",
4068			cn->data.consistency_completed.logical_drive,
4069			ld->cl_name);
4070	break;
4071    }
4072}
4073
4074/************************************************************************
4075 * Handle a notify event relating to the status of a physical drive.
4076 */
4077static void
4078ciss_notify_physical(struct ciss_softc *sc, struct ciss_notify *cn)
4079{
4080}
4081
4082/************************************************************************
4083 * Handle a notify event relating to the status of a physical drive.
4084 */
4085static void
4086ciss_notify_hotplug(struct ciss_softc *sc, struct ciss_notify *cn)
4087{
4088    struct ciss_lun_report *cll = NULL;
4089    int bus, target;
4090
4091    switch (cn->subclass) {
4092    case CISS_NOTIFY_HOTPLUG_PHYSICAL:
4093    case CISS_NOTIFY_HOTPLUG_NONDISK:
4094	bus = CISS_BIG_MAP_BUS(sc, cn->data.drive.big_physical_drive_number);
4095	target =
4096	    CISS_BIG_MAP_TARGET(sc, cn->data.drive.big_physical_drive_number);
4097
4098	if (cn->detail == 0) {
4099	    /*
4100	     * Mark the device offline so that it'll start producing selection
4101	     * timeouts to the upper layer.
4102	     */
4103	    if ((bus >= 0) && (target >= 0))
4104		sc->ciss_physical[bus][target].cp_online = 0;
4105	} else {
4106	    /*
4107	     * Rescan the physical lun list for new items
4108	     */
4109	    cll = ciss_report_luns(sc, CISS_OPCODE_REPORT_PHYSICAL_LUNS,
4110				   sc->ciss_cfg->max_physical_supported);
4111	    if (cll == NULL) {
4112		ciss_printf(sc, "Warning, cannot get physical lun list\n");
4113		break;
4114	    }
4115	    ciss_filter_physical(sc, cll);
4116	}
4117	break;
4118
4119    default:
4120	ciss_printf(sc, "Unknown hotplug event %d\n", cn->subclass);
4121	return;
4122    }
4123
4124    if (cll != NULL)
4125	free(cll, CISS_MALLOC_CLASS);
4126}
4127
4128/************************************************************************
4129 * Handle deferred processing of notify events.  Notify events may need
4130 * sleep which is unsafe during an interrupt.
4131 */
4132static void
4133ciss_notify_thread(void *arg)
4134{
4135    struct ciss_softc		*sc;
4136    struct ciss_request		*cr;
4137    struct ciss_notify		*cn;
4138
4139    sc = (struct ciss_softc *)arg;
4140#if __FreeBSD_version >= 500000
4141    mtx_lock(&sc->ciss_mtx);
4142#endif
4143
4144    for (;;) {
4145	if (STAILQ_EMPTY(&sc->ciss_notify) != 0 &&
4146	    (sc->ciss_flags & CISS_FLAG_THREAD_SHUT) == 0) {
4147	    msleep(&sc->ciss_notify, &sc->ciss_mtx, PUSER, "idle", 0);
4148	}
4149
4150	if (sc->ciss_flags & CISS_FLAG_THREAD_SHUT)
4151	    break;
4152
4153	cr = ciss_dequeue_notify(sc);
4154
4155	if (cr == NULL)
4156		panic("cr null");
4157	cn = (struct ciss_notify *)cr->cr_data;
4158
4159	switch (cn->class) {
4160	case CISS_NOTIFY_HOTPLUG:
4161	    ciss_notify_hotplug(sc, cn);
4162	    break;
4163	case CISS_NOTIFY_LOGICAL:
4164	    ciss_notify_logical(sc, cn);
4165	    break;
4166	case CISS_NOTIFY_PHYSICAL:
4167	    ciss_notify_physical(sc, cn);
4168	    break;
4169	}
4170
4171	ciss_release_request(cr);
4172
4173    }
4174    sc->ciss_notify_thread = NULL;
4175    wakeup(&sc->ciss_notify_thread);
4176
4177#if __FreeBSD_version >= 500000
4178    mtx_unlock(&sc->ciss_mtx);
4179#endif
4180    kproc_exit(0);
4181}
4182
4183/************************************************************************
4184 * Start the notification kernel thread.
4185 */
4186static void
4187ciss_spawn_notify_thread(struct ciss_softc *sc)
4188{
4189
4190#if __FreeBSD_version > 500005
4191    if (kproc_create((void(*)(void *))ciss_notify_thread, sc,
4192		       &sc->ciss_notify_thread, 0, 0, "ciss_notify%d",
4193		       device_get_unit(sc->ciss_dev)))
4194#else
4195    if (kproc_create((void(*)(void *))ciss_notify_thread, sc,
4196		       &sc->ciss_notify_thread, "ciss_notify%d",
4197		       device_get_unit(sc->ciss_dev)))
4198#endif
4199	panic("Could not create notify thread\n");
4200}
4201
4202/************************************************************************
4203 * Kill the notification kernel thread.
4204 */
4205static void
4206ciss_kill_notify_thread(struct ciss_softc *sc)
4207{
4208
4209    if (sc->ciss_notify_thread == NULL)
4210	return;
4211
4212    sc->ciss_flags |= CISS_FLAG_THREAD_SHUT;
4213    wakeup(&sc->ciss_notify);
4214    msleep(&sc->ciss_notify_thread, &sc->ciss_mtx, PUSER, "thtrm", 0);
4215}
4216
4217/************************************************************************
4218 * Print a request.
4219 */
4220static void
4221ciss_print_request(struct ciss_request *cr)
4222{
4223    struct ciss_softc	*sc;
4224    struct ciss_command	*cc;
4225    int			i;
4226
4227    sc = cr->cr_sc;
4228    cc = cr->cr_cc;
4229
4230    ciss_printf(sc, "REQUEST @ %p\n", cr);
4231    ciss_printf(sc, "  data %p/%d  tag %d  flags %b\n",
4232	      cr->cr_data, cr->cr_length, cr->cr_tag, cr->cr_flags,
4233	      "\20\1mapped\2sleep\3poll\4dataout\5datain\n");
4234    ciss_printf(sc, "  sg list/total %d/%d  host tag 0x%x\n",
4235		cc->header.sg_in_list, cc->header.sg_total, cc->header.host_tag);
4236    switch(cc->header.address.mode.mode) {
4237    case CISS_HDR_ADDRESS_MODE_PERIPHERAL:
4238    case CISS_HDR_ADDRESS_MODE_MASK_PERIPHERAL:
4239	ciss_printf(sc, "  physical bus %d target %d\n",
4240		    cc->header.address.physical.bus, cc->header.address.physical.target);
4241	break;
4242    case CISS_HDR_ADDRESS_MODE_LOGICAL:
4243	ciss_printf(sc, "  logical unit %d\n", cc->header.address.logical.lun);
4244	break;
4245    }
4246    ciss_printf(sc, "  %s cdb length %d type %s attribute %s\n",
4247		(cc->cdb.direction == CISS_CDB_DIRECTION_NONE) ? "no-I/O" :
4248		(cc->cdb.direction == CISS_CDB_DIRECTION_READ) ? "READ" :
4249		(cc->cdb.direction == CISS_CDB_DIRECTION_WRITE) ? "WRITE" : "??",
4250		cc->cdb.cdb_length,
4251		(cc->cdb.type == CISS_CDB_TYPE_COMMAND) ? "command" :
4252		(cc->cdb.type == CISS_CDB_TYPE_MESSAGE) ? "message" : "??",
4253		(cc->cdb.attribute == CISS_CDB_ATTRIBUTE_UNTAGGED) ? "untagged" :
4254		(cc->cdb.attribute == CISS_CDB_ATTRIBUTE_SIMPLE) ? "simple" :
4255		(cc->cdb.attribute == CISS_CDB_ATTRIBUTE_HEAD_OF_QUEUE) ? "head-of-queue" :
4256		(cc->cdb.attribute == CISS_CDB_ATTRIBUTE_ORDERED) ? "ordered" :
4257		(cc->cdb.attribute == CISS_CDB_ATTRIBUTE_AUTO_CONTINGENT) ? "auto-contingent" : "??");
4258    ciss_printf(sc, "  %*D\n", cc->cdb.cdb_length, &cc->cdb.cdb[0], " ");
4259
4260    if (cc->header.host_tag & CISS_HDR_HOST_TAG_ERROR) {
4261	/* XXX print error info */
4262    } else {
4263	/* since we don't use chained s/g, don't support it here */
4264	for (i = 0; i < cc->header.sg_in_list; i++) {
4265	    if ((i % 4) == 0)
4266		ciss_printf(sc, "   ");
4267	    printf("0x%08x/%d ", (u_int32_t)cc->sg[i].address, cc->sg[i].length);
4268	    if ((((i + 1) % 4) == 0) || (i == (cc->header.sg_in_list - 1)))
4269		printf("\n");
4270	}
4271    }
4272}
4273
4274/************************************************************************
4275 * Print information about the status of a logical drive.
4276 */
4277static void
4278ciss_print_ldrive(struct ciss_softc *sc, struct ciss_ldrive *ld)
4279{
4280    int		bus, target, i;
4281
4282    if (ld->cl_lstatus == NULL) {
4283	printf("does not exist\n");
4284	return;
4285    }
4286
4287    /* print drive status */
4288    switch(ld->cl_lstatus->status) {
4289    case CISS_LSTATUS_OK:
4290	printf("online\n");
4291	break;
4292    case CISS_LSTATUS_INTERIM_RECOVERY:
4293	printf("in interim recovery mode\n");
4294	break;
4295    case CISS_LSTATUS_READY_RECOVERY:
4296	printf("ready to begin recovery\n");
4297	break;
4298    case CISS_LSTATUS_RECOVERING:
4299	bus = CISS_BIG_MAP_BUS(sc, ld->cl_lstatus->drive_rebuilding);
4300	target = CISS_BIG_MAP_BUS(sc, ld->cl_lstatus->drive_rebuilding);
4301	printf("being recovered, working on physical drive %d.%d, %u blocks remaining\n",
4302	       bus, target, ld->cl_lstatus->blocks_to_recover);
4303	break;
4304    case CISS_LSTATUS_EXPANDING:
4305	printf("being expanded, %u blocks remaining\n",
4306	       ld->cl_lstatus->blocks_to_recover);
4307	break;
4308    case CISS_LSTATUS_QUEUED_FOR_EXPANSION:
4309	printf("queued for expansion\n");
4310	break;
4311    case CISS_LSTATUS_FAILED:
4312	printf("queued for expansion\n");
4313	break;
4314    case CISS_LSTATUS_WRONG_PDRIVE:
4315	printf("wrong physical drive inserted\n");
4316	break;
4317    case CISS_LSTATUS_MISSING_PDRIVE:
4318	printf("missing a needed physical drive\n");
4319	break;
4320    case CISS_LSTATUS_BECOMING_READY:
4321	printf("becoming ready\n");
4322	break;
4323    }
4324
4325    /* print failed physical drives */
4326    for (i = 0; i < CISS_BIG_MAP_ENTRIES / 8; i++) {
4327	bus = CISS_BIG_MAP_BUS(sc, ld->cl_lstatus->drive_failure_map[i]);
4328	target = CISS_BIG_MAP_TARGET(sc, ld->cl_lstatus->drive_failure_map[i]);
4329	if (bus == -1)
4330	    continue;
4331	ciss_printf(sc, "physical drive %d:%d (%x) failed\n", bus, target,
4332		    ld->cl_lstatus->drive_failure_map[i]);
4333    }
4334}
4335
4336#ifdef CISS_DEBUG
4337#include "opt_ddb.h"
4338#ifdef DDB
4339#include <ddb/ddb.h>
4340/************************************************************************
4341 * Print information about the controller/driver.
4342 */
4343static void
4344ciss_print_adapter(struct ciss_softc *sc)
4345{
4346    int		i, j;
4347
4348    ciss_printf(sc, "ADAPTER:\n");
4349    for (i = 0; i < CISSQ_COUNT; i++) {
4350	ciss_printf(sc, "%s     %d/%d\n",
4351	    i == 0 ? "free" :
4352	    i == 1 ? "busy" : "complete",
4353	    sc->ciss_qstat[i].q_length,
4354	    sc->ciss_qstat[i].q_max);
4355    }
4356    ciss_printf(sc, "max_requests %d\n", sc->ciss_max_requests);
4357    ciss_printf(sc, "flags %b\n", sc->ciss_flags,
4358	"\20\1notify_ok\2control_open\3aborting\4running\21fake_synch\22bmic_abort\n");
4359
4360    for (i = 0; i < sc->ciss_max_logical_bus; i++) {
4361	for (j = 0; j < sc->ciss_cfg->max_logical_supported; j++) {
4362	    ciss_printf(sc, "LOGICAL DRIVE %d:  ", i);
4363	    ciss_print_ldrive(sc, &sc->ciss_logical[i][j]);
4364	}
4365    }
4366
4367    /* XXX Should physical drives be printed out here? */
4368
4369    for (i = 1; i < sc->ciss_max_requests; i++)
4370	ciss_print_request(sc->ciss_request + i);
4371}
4372
4373/* DDB hook */
4374DB_COMMAND(ciss_prt, db_ciss_prt)
4375{
4376    struct ciss_softc	*sc;
4377    devclass_t dc;
4378    int maxciss, i;
4379
4380    dc = devclass_find("ciss");
4381    if ( dc == NULL ) {
4382        printf("%s: can't find devclass!\n", __func__);
4383        return;
4384    }
4385    maxciss = devclass_get_maxunit(dc);
4386    for (i = 0; i < maxciss; i++) {
4387        sc = devclass_get_softc(dc, i);
4388	ciss_print_adapter(sc);
4389    }
4390}
4391#endif
4392#endif
4393
4394/************************************************************************
4395 * Return a name for a logical drive status value.
4396 */
4397static const char *
4398ciss_name_ldrive_status(int status)
4399{
4400    switch (status) {
4401    case CISS_LSTATUS_OK:
4402	return("OK");
4403    case CISS_LSTATUS_FAILED:
4404	return("failed");
4405    case CISS_LSTATUS_NOT_CONFIGURED:
4406	return("not configured");
4407    case CISS_LSTATUS_INTERIM_RECOVERY:
4408	return("interim recovery");
4409    case CISS_LSTATUS_READY_RECOVERY:
4410	return("ready for recovery");
4411    case CISS_LSTATUS_RECOVERING:
4412	return("recovering");
4413    case CISS_LSTATUS_WRONG_PDRIVE:
4414	return("wrong physical drive inserted");
4415    case CISS_LSTATUS_MISSING_PDRIVE:
4416	return("missing physical drive");
4417    case CISS_LSTATUS_EXPANDING:
4418	return("expanding");
4419    case CISS_LSTATUS_BECOMING_READY:
4420	return("becoming ready");
4421    case CISS_LSTATUS_QUEUED_FOR_EXPANSION:
4422	return("queued for expansion");
4423    }
4424    return("unknown status");
4425}
4426
4427/************************************************************************
4428 * Return an online/offline/nonexistent value for a logical drive
4429 * status value.
4430 */
4431static int
4432ciss_decode_ldrive_status(int status)
4433{
4434    switch(status) {
4435    case CISS_LSTATUS_NOT_CONFIGURED:
4436	return(CISS_LD_NONEXISTENT);
4437
4438    case CISS_LSTATUS_OK:
4439    case CISS_LSTATUS_INTERIM_RECOVERY:
4440    case CISS_LSTATUS_READY_RECOVERY:
4441    case CISS_LSTATUS_RECOVERING:
4442    case CISS_LSTATUS_EXPANDING:
4443    case CISS_LSTATUS_QUEUED_FOR_EXPANSION:
4444	return(CISS_LD_ONLINE);
4445
4446    case CISS_LSTATUS_FAILED:
4447    case CISS_LSTATUS_WRONG_PDRIVE:
4448    case CISS_LSTATUS_MISSING_PDRIVE:
4449    case CISS_LSTATUS_BECOMING_READY:
4450    default:
4451	return(CISS_LD_OFFLINE);
4452    }
4453}
4454
4455
4456/************************************************************************
4457 * Return a name for a logical drive's organisation.
4458 */
4459static const char *
4460ciss_name_ldrive_org(int org)
4461{
4462    switch(org) {
4463    case CISS_LDRIVE_RAID0:
4464	return("RAID 0");
4465    case CISS_LDRIVE_RAID1:
4466	return("RAID 1(1+0)");
4467    case CISS_LDRIVE_RAID4:
4468	return("RAID 4");
4469    case CISS_LDRIVE_RAID5:
4470	return("RAID 5");
4471    case CISS_LDRIVE_RAID51:
4472	return("RAID 5+1");
4473    case CISS_LDRIVE_RAIDADG:
4474	return("RAID ADG");
4475    }
4476    return("unkown");
4477}
4478
4479/************************************************************************
4480 * Return a name for a command status value.
4481 */
4482static const char *
4483ciss_name_command_status(int status)
4484{
4485    switch(status) {
4486    case CISS_CMD_STATUS_SUCCESS:
4487	return("success");
4488    case CISS_CMD_STATUS_TARGET_STATUS:
4489	return("target status");
4490    case CISS_CMD_STATUS_DATA_UNDERRUN:
4491	return("data underrun");
4492    case CISS_CMD_STATUS_DATA_OVERRUN:
4493	return("data overrun");
4494    case CISS_CMD_STATUS_INVALID_COMMAND:
4495	return("invalid command");
4496    case CISS_CMD_STATUS_PROTOCOL_ERROR:
4497	return("protocol error");
4498    case CISS_CMD_STATUS_HARDWARE_ERROR:
4499	return("hardware error");
4500    case CISS_CMD_STATUS_CONNECTION_LOST:
4501	return("connection lost");
4502    case CISS_CMD_STATUS_ABORTED:
4503	return("aborted");
4504    case CISS_CMD_STATUS_ABORT_FAILED:
4505	return("abort failed");
4506    case CISS_CMD_STATUS_UNSOLICITED_ABORT:
4507	return("unsolicited abort");
4508    case CISS_CMD_STATUS_TIMEOUT:
4509	return("timeout");
4510    case CISS_CMD_STATUS_UNABORTABLE:
4511	return("unabortable");
4512    }
4513    return("unknown status");
4514}
4515
4516/************************************************************************
4517 * Handle an open on the control device.
4518 */
4519static int
4520ciss_open(struct cdev *dev, int flags, int fmt, struct thread *p)
4521{
4522    struct ciss_softc	*sc;
4523
4524    debug_called(1);
4525
4526    sc = (struct ciss_softc *)dev->si_drv1;
4527
4528    /* we might want to veto if someone already has us open */
4529
4530    mtx_lock(&sc->ciss_mtx);
4531    sc->ciss_flags |= CISS_FLAG_CONTROL_OPEN;
4532    mtx_unlock(&sc->ciss_mtx);
4533    return(0);
4534}
4535
4536/************************************************************************
4537 * Handle the last close on the control device.
4538 */
4539static int
4540ciss_close(struct cdev *dev, int flags, int fmt, struct thread *p)
4541{
4542    struct ciss_softc	*sc;
4543
4544    debug_called(1);
4545
4546    sc = (struct ciss_softc *)dev->si_drv1;
4547
4548    mtx_lock(&sc->ciss_mtx);
4549    sc->ciss_flags &= ~CISS_FLAG_CONTROL_OPEN;
4550    mtx_unlock(&sc->ciss_mtx);
4551    return (0);
4552}
4553
4554/********************************************************************************
4555 * Handle adapter-specific control operations.
4556 *
4557 * Note that the API here is compatible with the Linux driver, in order to
4558 * simplify the porting of Compaq's userland tools.
4559 */
4560static int
4561ciss_ioctl(struct cdev *dev, u_long cmd, caddr_t addr, int32_t flag, struct thread *p)
4562{
4563    struct ciss_softc		*sc;
4564    IOCTL_Command_struct	*ioc	= (IOCTL_Command_struct *)addr;
4565#ifdef __amd64__
4566    IOCTL_Command_struct32	*ioc32	= (IOCTL_Command_struct32 *)addr;
4567    IOCTL_Command_struct	ioc_swab;
4568#endif
4569    int				error;
4570
4571    debug_called(1);
4572
4573    sc = (struct ciss_softc *)dev->si_drv1;
4574    error = 0;
4575    mtx_lock(&sc->ciss_mtx);
4576
4577    switch(cmd) {
4578    case CCISS_GETQSTATS:
4579    {
4580	union ciss_statrequest *cr = (union ciss_statrequest *)addr;
4581
4582	switch (cr->cs_item) {
4583	case CISSQ_FREE:
4584	case CISSQ_NOTIFY:
4585	    bcopy(&sc->ciss_qstat[cr->cs_item], &cr->cs_qstat,
4586		sizeof(struct ciss_qstat));
4587	    break;
4588	default:
4589	    error = ENOIOCTL;
4590	    break;
4591	}
4592
4593	break;
4594    }
4595
4596    case CCISS_GETPCIINFO:
4597    {
4598	cciss_pci_info_struct	*pis = (cciss_pci_info_struct *)addr;
4599
4600	pis->bus = pci_get_bus(sc->ciss_dev);
4601	pis->dev_fn = pci_get_slot(sc->ciss_dev);
4602        pis->board_id = (pci_get_subvendor(sc->ciss_dev) << 16) |
4603                pci_get_subdevice(sc->ciss_dev);
4604
4605	break;
4606    }
4607
4608    case CCISS_GETINTINFO:
4609    {
4610	cciss_coalint_struct	*cis = (cciss_coalint_struct *)addr;
4611
4612	cis->delay = sc->ciss_cfg->interrupt_coalesce_delay;
4613	cis->count = sc->ciss_cfg->interrupt_coalesce_count;
4614
4615	break;
4616    }
4617
4618    case CCISS_SETINTINFO:
4619    {
4620	cciss_coalint_struct	*cis = (cciss_coalint_struct *)addr;
4621
4622	if ((cis->delay == 0) && (cis->count == 0)) {
4623	    error = EINVAL;
4624	    break;
4625	}
4626
4627	/*
4628	 * XXX apparently this is only safe if the controller is idle,
4629	 *     we should suspend it before doing this.
4630	 */
4631	sc->ciss_cfg->interrupt_coalesce_delay = cis->delay;
4632	sc->ciss_cfg->interrupt_coalesce_count = cis->count;
4633
4634	if (ciss_update_config(sc))
4635	    error = EIO;
4636
4637	/* XXX resume the controller here */
4638	break;
4639    }
4640
4641    case CCISS_GETNODENAME:
4642	bcopy(sc->ciss_cfg->server_name, (NodeName_type *)addr,
4643	      sizeof(NodeName_type));
4644	break;
4645
4646    case CCISS_SETNODENAME:
4647	bcopy((NodeName_type *)addr, sc->ciss_cfg->server_name,
4648	      sizeof(NodeName_type));
4649	if (ciss_update_config(sc))
4650	    error = EIO;
4651	break;
4652
4653    case CCISS_GETHEARTBEAT:
4654	*(Heartbeat_type *)addr = sc->ciss_cfg->heartbeat;
4655	break;
4656
4657    case CCISS_GETBUSTYPES:
4658	*(BusTypes_type *)addr = sc->ciss_cfg->bus_types;
4659	break;
4660
4661    case CCISS_GETFIRMVER:
4662	bcopy(sc->ciss_id->running_firmware_revision, (FirmwareVer_type *)addr,
4663	      sizeof(FirmwareVer_type));
4664	break;
4665
4666    case CCISS_GETDRIVERVER:
4667	*(DriverVer_type *)addr = CISS_DRIVER_VERSION;
4668	break;
4669
4670    case CCISS_REVALIDVOLS:
4671	/*
4672	 * This is a bit ugly; to do it "right" we really need
4673	 * to find any disks that have changed, kick CAM off them,
4674	 * then rescan only these disks.  It'd be nice if they
4675	 * a) told us which disk(s) they were going to play with,
4676	 * and b) which ones had arrived. 8(
4677	 */
4678	break;
4679
4680#ifdef __amd64__
4681    case CCISS_PASSTHRU32:
4682	ioc_swab.LUN_info	= ioc32->LUN_info;
4683	ioc_swab.Request	= ioc32->Request;
4684	ioc_swab.error_info	= ioc32->error_info;
4685	ioc_swab.buf_size	= ioc32->buf_size;
4686	ioc_swab.buf		= (u_int8_t *)(uintptr_t)ioc32->buf;
4687	ioc			= &ioc_swab;
4688	/* FALLTHROUGH */
4689#endif
4690
4691    case CCISS_PASSTHRU:
4692	error = ciss_user_command(sc, ioc);
4693	break;
4694
4695    default:
4696	debug(0, "unknown ioctl 0x%lx", cmd);
4697
4698	debug(1, "CCISS_GETPCIINFO:   0x%lx", CCISS_GETPCIINFO);
4699	debug(1, "CCISS_GETINTINFO:   0x%lx", CCISS_GETINTINFO);
4700	debug(1, "CCISS_SETINTINFO:   0x%lx", CCISS_SETINTINFO);
4701	debug(1, "CCISS_GETNODENAME:  0x%lx", CCISS_GETNODENAME);
4702	debug(1, "CCISS_SETNODENAME:  0x%lx", CCISS_SETNODENAME);
4703	debug(1, "CCISS_GETHEARTBEAT: 0x%lx", CCISS_GETHEARTBEAT);
4704	debug(1, "CCISS_GETBUSTYPES:  0x%lx", CCISS_GETBUSTYPES);
4705	debug(1, "CCISS_GETFIRMVER:   0x%lx", CCISS_GETFIRMVER);
4706	debug(1, "CCISS_GETDRIVERVER: 0x%lx", CCISS_GETDRIVERVER);
4707	debug(1, "CCISS_REVALIDVOLS:  0x%lx", CCISS_REVALIDVOLS);
4708	debug(1, "CCISS_PASSTHRU:     0x%lx", CCISS_PASSTHRU);
4709
4710	error = ENOIOCTL;
4711	break;
4712    }
4713
4714    mtx_unlock(&sc->ciss_mtx);
4715    return(error);
4716}
4717