Home | History | Annotate | Line # | Download | only in pci
mpii.c revision 1.13
      1  1.13    bouyer /* $NetBSD: mpii.c,v 1.13 2018/11/24 18:11:22 bouyer Exp $ */
      2   1.1    bouyer /*	OpenBSD: mpii.c,v 1.51 2012/04/11 13:29:14 naddy Exp 	*/
      3   1.1    bouyer /*
      4   1.1    bouyer  * Copyright (c) 2010 Mike Belopuhov <mkb (at) crypt.org.ru>
      5   1.1    bouyer  * Copyright (c) 2009 James Giannoules
      6   1.1    bouyer  * Copyright (c) 2005 - 2010 David Gwynne <dlg (at) openbsd.org>
      7   1.1    bouyer  * Copyright (c) 2005 - 2010 Marco Peereboom <marco (at) openbsd.org>
      8   1.1    bouyer  *
      9   1.1    bouyer  * Permission to use, copy, modify, and distribute this software for any
     10   1.1    bouyer  * purpose with or without fee is hereby granted, provided that the above
     11   1.1    bouyer  * copyright notice and this permission notice appear in all copies.
     12   1.1    bouyer  *
     13   1.1    bouyer  * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
     14   1.1    bouyer  * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
     15   1.1    bouyer  * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
     16   1.1    bouyer  * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
     17   1.1    bouyer  * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
     18   1.1    bouyer  * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
     19   1.1    bouyer  * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
     20   1.1    bouyer  */
     21   1.1    bouyer 
     22   1.1    bouyer #include <sys/cdefs.h>
     23  1.13    bouyer __KERNEL_RCSID(0, "$NetBSD: mpii.c,v 1.13 2018/11/24 18:11:22 bouyer Exp $");
     24   1.1    bouyer 
     25   1.1    bouyer #include "bio.h"
     26   1.1    bouyer 
     27   1.1    bouyer #include <sys/param.h>
     28   1.1    bouyer #include <sys/systm.h>
     29   1.1    bouyer #include <sys/buf.h>
     30   1.1    bouyer #include <sys/device.h>
     31   1.1    bouyer #include <sys/ioctl.h>
     32   1.1    bouyer #include <sys/malloc.h>
     33   1.1    bouyer #include <sys/kernel.h>
     34   1.1    bouyer #include <sys/mutex.h>
     35   1.1    bouyer #include <sys/condvar.h>
     36   1.1    bouyer #include <sys/dkio.h>
     37   1.1    bouyer #include <sys/tree.h>
     38   1.1    bouyer 
     39   1.1    bouyer #include <dev/pci/pcireg.h>
     40   1.1    bouyer #include <dev/pci/pcivar.h>
     41   1.1    bouyer #include <dev/pci/pcidevs.h>
     42   1.1    bouyer 
     43   1.1    bouyer #include <dev/scsipi/scsipi_all.h>
     44   1.1    bouyer #include <dev/scsipi/scsi_all.h>
     45   1.1    bouyer #include <dev/scsipi/scsiconf.h>
     46   1.1    bouyer 
     47  1.13    bouyer #include <dev/pci/mpiireg.h>
     48  1.13    bouyer 
     49  1.13    bouyer #if NBIO > 0
     50   1.1    bouyer #include <dev/biovar.h>
     51   1.1    bouyer #include <dev/sysmon/sysmonvar.h>
     52   1.1    bouyer #include <sys/envsys.h>
     53  1.13    bouyer #endif
     54   1.1    bouyer 
     55   1.1    bouyer /* #define MPII_DEBUG */
     56   1.1    bouyer #ifdef MPII_DEBUG
     57   1.1    bouyer #define DPRINTF(x...)		do { if (mpii_debug) printf(x); } while(0)
     58   1.1    bouyer #define DNPRINTF(n,x...)	do { if (mpii_debug & (n)) printf(x); } while(0)
     59   1.1    bouyer #define	MPII_D_CMD		(0x0001)
     60   1.1    bouyer #define	MPII_D_INTR		(0x0002)
     61   1.1    bouyer #define	MPII_D_MISC		(0x0004)
     62   1.1    bouyer #define	MPII_D_DMA		(0x0008)
     63   1.1    bouyer #define	MPII_D_IOCTL		(0x0010)
     64   1.1    bouyer #define	MPII_D_RW		(0x0020)
     65   1.1    bouyer #define	MPII_D_MEM		(0x0040)
     66   1.1    bouyer #define	MPII_D_CCB		(0x0080)
     67   1.1    bouyer #define	MPII_D_PPR		(0x0100)
     68   1.1    bouyer #define	MPII_D_RAID		(0x0200)
     69   1.1    bouyer #define	MPII_D_EVT		(0x0400)
     70   1.1    bouyer #define MPII_D_CFG		(0x0800)
     71   1.1    bouyer #define MPII_D_MAP		(0x1000)
     72   1.1    bouyer #if 0
     73   1.1    bouyer u_int32_t  mpii_debug = 0
     74   1.1    bouyer 		| MPII_D_CMD
     75   1.1    bouyer 		| MPII_D_INTR
     76   1.1    bouyer 		| MPII_D_MISC
     77   1.1    bouyer 		| MPII_D_DMA
     78   1.1    bouyer 		| MPII_D_IOCTL
     79   1.1    bouyer 		| MPII_D_RW
     80   1.1    bouyer 		| MPII_D_MEM
     81   1.1    bouyer 		| MPII_D_CCB
     82   1.1    bouyer 		| MPII_D_PPR
     83   1.1    bouyer 		| MPII_D_RAID
     84   1.1    bouyer 		| MPII_D_EVT
     85   1.1    bouyer 		| MPII_D_CFG
     86   1.1    bouyer 		| MPII_D_MAP
     87   1.1    bouyer 	;
     88   1.1    bouyer #endif
     89   1.1    bouyer u_int32_t  mpii_debug = MPII_D_MISC;
     90   1.1    bouyer #else
     91   1.1    bouyer #define DPRINTF(x...)
     92   1.1    bouyer #define DNPRINTF(n,x...)
     93   1.1    bouyer #endif
     94   1.1    bouyer 
     95   1.1    bouyer #define MPII_REQUEST_SIZE	(512)
     96   1.1    bouyer #define MPII_REPLY_SIZE		(128)
     97   1.1    bouyer #define MPII_REPLY_COUNT	PAGE_SIZE / MPII_REPLY_SIZE
     98   1.1    bouyer 
     99   1.1    bouyer /*
    100   1.1    bouyer  * this is the max number of sge's we can stuff in a request frame:
    101   1.1    bouyer  * sizeof(scsi_io) + sizeof(sense) + sizeof(sge) * 32 = MPII_REQUEST_SIZE
    102   1.1    bouyer  */
    103   1.1    bouyer #define MPII_MAX_SGL			(32)
    104   1.1    bouyer 
    105   1.1    bouyer #define MPII_MAX_REQUEST_CREDIT		(128)
    106   1.1    bouyer 
    107   1.1    bouyer #define MPII_MAXFER MAXPHYS /* XXX bogus */
    108   1.1    bouyer 
    109   1.1    bouyer struct mpii_dmamem {
    110   1.1    bouyer 	bus_dmamap_t		mdm_map;
    111   1.1    bouyer 	bus_dma_segment_t	mdm_seg;
    112   1.1    bouyer 	size_t			mdm_size;
    113   1.1    bouyer 	void 			*mdm_kva;
    114   1.1    bouyer };
    115   1.1    bouyer #define MPII_DMA_MAP(_mdm)	(_mdm)->mdm_map
    116   1.1    bouyer #define MPII_DMA_DVA(_mdm)	(_mdm)->mdm_map->dm_segs[0].ds_addr
    117   1.1    bouyer #define MPII_DMA_KVA(_mdm)	(void *)(_mdm)->mdm_kva
    118   1.1    bouyer 
    119   1.1    bouyer struct mpii_ccb_bundle {
    120   1.1    bouyer 	struct mpii_msg_scsi_io	mcb_io; /* sgl must follow */
    121   1.1    bouyer 	struct mpii_sge		mcb_sgl[MPII_MAX_SGL];
    122   1.1    bouyer 	struct scsi_sense_data	mcb_sense;
    123   1.1    bouyer } __packed;
    124   1.1    bouyer 
    125   1.1    bouyer struct mpii_softc;
    126   1.1    bouyer 
    127   1.1    bouyer struct mpii_rcb {
    128   1.1    bouyer 	union {
    129   1.1    bouyer 		struct work	rcb_wk; /* has to be first in struct */
    130   1.1    bouyer 		SIMPLEQ_ENTRY(mpii_rcb)	rcb_link;
    131   1.1    bouyer 	} u;
    132   1.1    bouyer 	void			*rcb_reply;
    133   1.1    bouyer 	u_int32_t		rcb_reply_dva;
    134   1.1    bouyer };
    135   1.1    bouyer 
    136   1.1    bouyer SIMPLEQ_HEAD(mpii_rcb_list, mpii_rcb);
    137   1.1    bouyer 
    138   1.1    bouyer struct mpii_device {
    139   1.1    bouyer 	int			flags;
    140   1.1    bouyer #define MPII_DF_ATTACH		(0x0001)
    141   1.1    bouyer #define MPII_DF_DETACH		(0x0002)
    142   1.1    bouyer #define MPII_DF_HIDDEN		(0x0004)
    143   1.1    bouyer #define MPII_DF_UNUSED		(0x0008)
    144   1.1    bouyer #define MPII_DF_VOLUME		(0x0010)
    145   1.1    bouyer #define MPII_DF_VOLUME_DISK	(0x0020)
    146   1.1    bouyer #define MPII_DF_HOT_SPARE	(0x0040)
    147   1.1    bouyer 	short			slot;
    148   1.1    bouyer 	short			percent;
    149   1.1    bouyer 	u_int16_t		dev_handle;
    150   1.1    bouyer 	u_int16_t		enclosure;
    151   1.1    bouyer 	u_int16_t		expander;
    152   1.1    bouyer 	u_int8_t		phy_num;
    153   1.1    bouyer 	u_int8_t		physical_port;
    154   1.1    bouyer };
    155   1.1    bouyer 
    156   1.1    bouyer struct mpii_ccb {
    157   1.1    bouyer 	union {
    158   1.1    bouyer 		struct work	ccb_wk; /* has to be first in struct */
    159   1.1    bouyer 		SIMPLEQ_ENTRY(mpii_ccb)	ccb_link;
    160   1.1    bouyer 	} u;
    161   1.1    bouyer 	struct mpii_softc	*ccb_sc;
    162   1.1    bouyer 	int			ccb_smid;
    163   1.1    bouyer 
    164   1.1    bouyer 	void *			ccb_cookie;
    165   1.1    bouyer 	bus_dmamap_t		ccb_dmamap;
    166   1.1    bouyer 
    167   1.1    bouyer 	bus_addr_t		ccb_offset;
    168   1.1    bouyer 	void			*ccb_cmd;
    169   1.1    bouyer 	bus_addr_t		ccb_cmd_dva;
    170   1.1    bouyer 	u_int16_t		ccb_dev_handle;
    171   1.1    bouyer 
    172   1.1    bouyer 	volatile enum {
    173   1.1    bouyer 		MPII_CCB_FREE,
    174   1.1    bouyer 		MPII_CCB_READY,
    175   1.1    bouyer 		MPII_CCB_QUEUED,
    176   1.1    bouyer 		MPII_CCB_TIMEOUT
    177   1.1    bouyer 	}			ccb_state;
    178   1.1    bouyer 
    179   1.1    bouyer 	void			(*ccb_done)(struct mpii_ccb *);
    180   1.1    bouyer 	struct mpii_rcb		*ccb_rcb;
    181   1.1    bouyer 
    182   1.1    bouyer };
    183   1.1    bouyer 
    184   1.1    bouyer struct mpii_ccb_wait {
    185   1.1    bouyer 	kmutex_t	mpii_ccbw_mtx;
    186   1.1    bouyer 	kcondvar_t	mpii_ccbw_cv;
    187   1.1    bouyer };
    188   1.1    bouyer 
    189   1.1    bouyer SIMPLEQ_HEAD(mpii_ccb_list, mpii_ccb);
    190   1.1    bouyer 
    191   1.1    bouyer struct mpii_softc {
    192   1.1    bouyer 	device_t		sc_dev;
    193   1.1    bouyer 
    194   1.1    bouyer 	pci_chipset_tag_t	sc_pc;
    195   1.1    bouyer 	pcitag_t		sc_tag;
    196   1.1    bouyer 
    197   1.1    bouyer 	void			*sc_ih;
    198   1.1    bouyer 
    199   1.1    bouyer 	int			sc_flags;
    200   1.1    bouyer #define MPII_F_RAID		(1<<1)
    201   1.1    bouyer 
    202   1.1    bouyer 	struct scsipi_adapter	sc_adapt;
    203   1.1    bouyer 	struct scsipi_channel	sc_chan;
    204   1.1    bouyer 	device_t		sc_child; /* our scsibus */
    205   1.1    bouyer 
    206   1.1    bouyer 	struct mpii_device	**sc_devs;
    207   1.1    bouyer 
    208   1.1    bouyer 	bus_space_tag_t		sc_iot;
    209   1.1    bouyer 	bus_space_handle_t	sc_ioh;
    210   1.1    bouyer 	bus_size_t		sc_ios;
    211   1.1    bouyer 	bus_dma_tag_t		sc_dmat;
    212   1.1    bouyer 
    213   1.1    bouyer 	kmutex_t		sc_req_mtx;
    214   1.1    bouyer 	kmutex_t		sc_rep_mtx;
    215   1.1    bouyer 
    216   1.1    bouyer 	u_int8_t		sc_porttype;
    217   1.1    bouyer 	int			sc_request_depth;
    218   1.1    bouyer 	int			sc_num_reply_frames;
    219   1.1    bouyer 	int			sc_reply_free_qdepth;
    220   1.1    bouyer 	int			sc_reply_post_qdepth;
    221   1.1    bouyer 	int			sc_maxchdepth;
    222   1.1    bouyer 	int			sc_first_sgl_len;
    223   1.1    bouyer 	int			sc_chain_len;
    224   1.1    bouyer 	int			sc_max_sgl_len;
    225   1.1    bouyer 
    226   1.1    bouyer 	u_int8_t		sc_ioc_event_replay;
    227   1.1    bouyer 	u_int16_t		sc_max_enclosures;
    228   1.1    bouyer 	u_int16_t		sc_max_expanders;
    229   1.1    bouyer 	u_int8_t		sc_max_volumes;
    230   1.1    bouyer 	u_int16_t		sc_max_devices;
    231   1.1    bouyer 	u_int16_t		sc_max_dpm_entries;
    232   1.1    bouyer 	u_int16_t		sc_vd_count;
    233   1.1    bouyer 	u_int16_t		sc_vd_id_low;
    234   1.1    bouyer 	u_int16_t		sc_pd_id_start;
    235   1.1    bouyer 	u_int8_t		sc_num_channels;
    236   1.1    bouyer 	int			sc_ioc_number;
    237   1.1    bouyer 	u_int8_t		sc_vf_id;
    238   1.1    bouyer 	u_int8_t		sc_num_ports;
    239   1.1    bouyer 
    240   1.1    bouyer 	struct mpii_ccb		*sc_ccbs;
    241   1.1    bouyer 	struct mpii_ccb_list	sc_ccb_free;
    242   1.1    bouyer 	kmutex_t		sc_ccb_free_mtx;
    243   1.1    bouyer 	kcondvar_t		sc_ccb_free_cv;
    244   1.1    bouyer 
    245   1.1    bouyer 	kmutex_t		sc_ccb_mtx;
    246   1.1    bouyer 				/*
    247   1.1    bouyer 				 * this protects the ccb state and list entry
    248   1.1    bouyer 				 * between mpii_scsi_cmd and scsidone.
    249   1.1    bouyer 				 */
    250   1.1    bouyer 
    251   1.1    bouyer 	struct workqueue	*sc_ssb_tmowk;
    252   1.1    bouyer 
    253   1.1    bouyer 	struct mpii_dmamem	*sc_requests;
    254   1.1    bouyer 
    255   1.1    bouyer 	struct mpii_dmamem	*sc_replies;
    256   1.1    bouyer 	struct mpii_rcb		*sc_rcbs;
    257   1.1    bouyer 
    258   1.1    bouyer 	struct mpii_dmamem	*sc_reply_postq;
    259   1.1    bouyer 	struct mpii_reply_descr	*sc_reply_postq_kva;
    260   1.1    bouyer 	int			sc_reply_post_host_index;
    261   1.1    bouyer 
    262   1.1    bouyer 	struct mpii_dmamem	*sc_reply_freeq;
    263   1.1    bouyer 	int			sc_reply_free_host_index;
    264   1.1    bouyer 
    265   1.1    bouyer 	struct workqueue	*sc_ssb_evt_ackwk;
    266   1.1    bouyer 
    267   1.1    bouyer 	struct sysmon_envsys	*sc_sme;
    268   1.1    bouyer 	envsys_data_t		*sc_sensors;
    269   1.1    bouyer };
    270   1.1    bouyer 
    271   1.1    bouyer static int	mpii_match(device_t, cfdata_t, void *);
    272   1.1    bouyer static void	mpii_attach(device_t, device_t, void *);
    273   1.1    bouyer static int	mpii_detach(device_t, int);
    274   1.1    bouyer static void	mpii_childdetached(device_t, device_t);
    275   1.1    bouyer static int	mpii_rescan(device_t, const char *, const int *);
    276   1.1    bouyer 
    277   1.1    bouyer static int	mpii_intr(void *);
    278   1.1    bouyer 
    279   1.1    bouyer CFATTACH_DECL3_NEW(mpii, sizeof(struct mpii_softc),
    280   1.1    bouyer     mpii_match, mpii_attach, mpii_detach, NULL, mpii_rescan,
    281   1.1    bouyer     mpii_childdetached, DVF_DETACH_SHUTDOWN);
    282   1.1    bouyer 
    283   1.1    bouyer #define PREAD(s, r)	pci_conf_read((s)->sc_pc, (s)->sc_tag, (r))
    284   1.1    bouyer #define PWRITE(s, r, v)	pci_conf_write((s)->sc_pc, (s)->sc_tag, (r), (v))
    285   1.1    bouyer 
    286   1.1    bouyer static void	mpii_scsipi_request(struct scsipi_channel *,
    287   1.1    bouyer 		    scsipi_adapter_req_t, void *);
    288   1.1    bouyer static void	mpii_scsi_cmd_done(struct mpii_ccb *);
    289   1.1    bouyer static void	mpii_minphys(struct buf *bp);
    290   1.1    bouyer 
    291   1.1    bouyer static struct mpii_dmamem *mpii_dmamem_alloc(struct mpii_softc *, size_t);
    292   1.1    bouyer static void	mpii_dmamem_free(struct mpii_softc *, struct mpii_dmamem *);
    293   1.1    bouyer static int	mpii_alloc_ccbs(struct mpii_softc *);
    294   1.1    bouyer static struct mpii_ccb *mpii_get_ccb(struct mpii_softc *, int);
    295   1.1    bouyer #define MPII_NOSLEEP 0x0001
    296   1.1    bouyer static void	mpii_put_ccb(struct mpii_softc *, struct mpii_ccb *);
    297   1.1    bouyer static int	mpii_alloc_replies(struct mpii_softc *);
    298   1.1    bouyer static int	mpii_alloc_queues(struct mpii_softc *);
    299   1.1    bouyer static void	mpii_push_reply(struct mpii_softc *, struct mpii_rcb *);
    300   1.1    bouyer static void	mpii_push_replies(struct mpii_softc *);
    301   1.1    bouyer 
    302   1.1    bouyer static void	mpii_scsi_cmd_tmo(void *);
    303   1.1    bouyer static void	mpii_scsi_cmd_tmo_handler(struct work *, void *);
    304   1.1    bouyer static void	mpii_scsi_cmd_tmo_done(struct mpii_ccb *);
    305   1.1    bouyer 
    306   1.1    bouyer static int	mpii_alloc_dev(struct mpii_softc *);
    307   1.1    bouyer static int	mpii_insert_dev(struct mpii_softc *, struct mpii_device *);
    308   1.1    bouyer static int	mpii_remove_dev(struct mpii_softc *, struct mpii_device *);
    309   1.1    bouyer static struct mpii_device *mpii_find_dev(struct mpii_softc *, u_int16_t);
    310   1.1    bouyer 
    311   1.1    bouyer static void	mpii_start(struct mpii_softc *, struct mpii_ccb *);
    312   1.1    bouyer static int	mpii_poll(struct mpii_softc *, struct mpii_ccb *);
    313   1.1    bouyer static void	mpii_poll_done(struct mpii_ccb *);
    314   1.1    bouyer static struct mpii_rcb *mpii_reply(struct mpii_softc *,
    315   1.1    bouyer 			struct mpii_reply_descr *);
    316   1.1    bouyer 
    317   1.1    bouyer static void	mpii_wait(struct mpii_softc *, struct mpii_ccb *);
    318   1.1    bouyer static void	mpii_wait_done(struct mpii_ccb *);
    319   1.1    bouyer 
    320   1.1    bouyer static void	mpii_init_queues(struct mpii_softc *);
    321   1.1    bouyer 
    322   1.1    bouyer static int	mpii_load_xs(struct mpii_ccb *);
    323   1.1    bouyer 
    324   1.1    bouyer static u_int32_t mpii_read(struct mpii_softc *, bus_size_t);
    325   1.1    bouyer static void	mpii_write(struct mpii_softc *, bus_size_t, u_int32_t);
    326   1.1    bouyer static int	mpii_wait_eq(struct mpii_softc *, bus_size_t, u_int32_t,
    327   1.1    bouyer 		    u_int32_t);
    328   1.1    bouyer static int	mpii_wait_ne(struct mpii_softc *, bus_size_t, u_int32_t,
    329   1.1    bouyer 		    u_int32_t);
    330   1.1    bouyer 
    331   1.1    bouyer static int	mpii_init(struct mpii_softc *);
    332   1.1    bouyer static int	mpii_reset_soft(struct mpii_softc *);
    333   1.1    bouyer static int	mpii_reset_hard(struct mpii_softc *);
    334   1.1    bouyer 
    335   1.1    bouyer static int	mpii_handshake_send(struct mpii_softc *, void *, size_t);
    336   1.1    bouyer static int	mpii_handshake_recv_dword(struct mpii_softc *,
    337   1.1    bouyer 		    u_int32_t *);
    338   1.1    bouyer static int	mpii_handshake_recv(struct mpii_softc *, void *, size_t);
    339   1.1    bouyer 
    340   1.1    bouyer static void	mpii_empty_done(struct mpii_ccb *);
    341   1.1    bouyer 
    342   1.1    bouyer static int	mpii_iocinit(struct mpii_softc *);
    343   1.1    bouyer static int	mpii_iocfacts(struct mpii_softc *);
    344   1.1    bouyer static int	mpii_portfacts(struct mpii_softc *);
    345   1.1    bouyer static int	mpii_portenable(struct mpii_softc *);
    346   1.1    bouyer static int	mpii_cfg_coalescing(struct mpii_softc *);
    347   1.1    bouyer 
    348   1.1    bouyer static int	mpii_eventnotify(struct mpii_softc *);
    349   1.1    bouyer static void	mpii_eventnotify_done(struct mpii_ccb *);
    350   1.1    bouyer static void	mpii_eventack(struct work *, void *);
    351   1.1    bouyer static void	mpii_eventack_done(struct mpii_ccb *);
    352   1.1    bouyer static void	mpii_event_process(struct mpii_softc *, struct mpii_rcb *);
    353   1.1    bouyer static void	mpii_event_sas(struct mpii_softc *,
    354   1.1    bouyer 		    struct mpii_msg_event_reply *);
    355   1.1    bouyer static void	mpii_event_raid(struct mpii_softc *,
    356   1.1    bouyer 		    struct mpii_msg_event_reply *);
    357   1.1    bouyer static void	mpii_event_defer(void *, void *);
    358   1.1    bouyer 
    359   1.1    bouyer static void	mpii_sas_remove_device(struct mpii_softc *, u_int16_t);
    360   1.1    bouyer 
    361   1.1    bouyer static int	mpii_req_cfg_header(struct mpii_softc *, u_int8_t,
    362   1.1    bouyer 		    u_int8_t, u_int32_t, int, void *);
    363   1.1    bouyer static int	mpii_req_cfg_page(struct mpii_softc *, u_int32_t, int,
    364   1.1    bouyer 		    void *, int, void *, size_t);
    365   1.1    bouyer 
    366   1.1    bouyer static int	mpii_get_ioc_pg8(struct mpii_softc *);
    367   1.1    bouyer 
    368   1.1    bouyer #if 0
    369   1.1    bouyer static int	mpii_ioctl_cache(struct scsi_link *, u_long, struct dk_cache *);
    370   1.1    bouyer #endif
    371   1.1    bouyer static int	mpii_cache_enable(struct mpii_softc *, struct mpii_device *);
    372   1.1    bouyer 
    373   1.1    bouyer #if NBIO > 0
    374   1.1    bouyer static int	mpii_ioctl(device_t, u_long, void *);
    375   1.1    bouyer static int	mpii_ioctl_inq(struct mpii_softc *, struct bioc_inq *);
    376   1.1    bouyer static int	mpii_ioctl_vol(struct mpii_softc *, struct bioc_vol *);
    377   1.1    bouyer static int	mpii_ioctl_disk(struct mpii_softc *, struct bioc_disk *);
    378   1.1    bouyer static int	mpii_bio_hs(struct mpii_softc *, struct bioc_disk *, int,
    379   1.1    bouyer 		    int, int *);
    380   1.1    bouyer static int	mpii_bio_disk(struct mpii_softc *, struct bioc_disk *,
    381   1.1    bouyer 		    u_int8_t);
    382   1.1    bouyer static struct mpii_device *mpii_find_vol(struct mpii_softc *, int);
    383   1.1    bouyer static int	mpii_bio_volstate(struct mpii_softc *, struct bioc_vol *);
    384   1.1    bouyer static int	mpii_create_sensors(struct mpii_softc *);
    385   1.1    bouyer static int	mpii_destroy_sensors(struct mpii_softc *);
    386   1.1    bouyer static void	mpii_refresh_sensors(struct sysmon_envsys *, envsys_data_t *);
    387   1.1    bouyer #endif /* NBIO > 0 */
    388   1.1    bouyer 
    389   1.1    bouyer #define DEVNAME(_s)		(device_xname((_s)->sc_dev))
    390   1.1    bouyer 
    391   1.1    bouyer #define dwordsof(s)		(sizeof(s) / sizeof(u_int32_t))
    392   1.1    bouyer #define dwordn(p, n)		(((u_int32_t *)(p))[(n)])
    393   1.1    bouyer 
    394   1.1    bouyer #define mpii_read_db(s)		mpii_read((s), MPII_DOORBELL)
    395   1.1    bouyer #define mpii_write_db(s, v)	mpii_write((s), MPII_DOORBELL, (v))
    396   1.1    bouyer #define mpii_read_intr(s)	mpii_read((s), MPII_INTR_STATUS)
    397   1.1    bouyer #define mpii_write_intr(s, v)	mpii_write((s), MPII_INTR_STATUS, (v))
    398   1.1    bouyer #define mpii_reply_waiting(s)	((mpii_read_intr((s)) & MPII_INTR_STATUS_REPLY)\
    399   1.1    bouyer 				    == MPII_INTR_STATUS_REPLY)
    400   1.1    bouyer 
    401   1.1    bouyer #define mpii_read_reply_free(s)		mpii_read((s), \
    402   1.1    bouyer 						MPII_REPLY_FREE_HOST_INDEX)
    403   1.1    bouyer #define mpii_write_reply_free(s, v)	mpii_write((s), \
    404   1.1    bouyer 						MPII_REPLY_FREE_HOST_INDEX, (v))
    405   1.1    bouyer #define mpii_read_reply_post(s)		mpii_read((s), \
    406   1.1    bouyer 						MPII_REPLY_POST_HOST_INDEX)
    407   1.1    bouyer #define mpii_write_reply_post(s, v)	mpii_write((s), \
    408   1.1    bouyer 						MPII_REPLY_POST_HOST_INDEX, (v))
    409   1.1    bouyer 
    410   1.1    bouyer #define mpii_wait_db_int(s)	mpii_wait_ne((s), MPII_INTR_STATUS, \
    411   1.1    bouyer 				    MPII_INTR_STATUS_IOC2SYSDB, 0)
    412   1.1    bouyer #define mpii_wait_db_ack(s)	mpii_wait_eq((s), MPII_INTR_STATUS, \
    413   1.1    bouyer 				    MPII_INTR_STATUS_SYS2IOCDB, 0)
    414   1.1    bouyer 
    415   1.1    bouyer #define MPII_PG_EXTENDED	(1<<0)
    416   1.1    bouyer #define MPII_PG_POLL		(1<<1)
    417   1.1    bouyer #define MPII_PG_FMT		"\020" "\002POLL" "\001EXTENDED"
    418   1.1    bouyer 
    419   1.1    bouyer #define mpii_cfg_header(_s, _t, _n, _a, _h) \
    420   1.1    bouyer 	mpii_req_cfg_header((_s), (_t), (_n), (_a), \
    421   1.1    bouyer 	    MPII_PG_POLL, (_h))
    422   1.1    bouyer #define mpii_ecfg_header(_s, _t, _n, _a, _h) \
    423   1.1    bouyer 	mpii_req_cfg_header((_s), (_t), (_n), (_a), \
    424   1.1    bouyer 	    MPII_PG_POLL|MPII_PG_EXTENDED, (_h))
    425   1.1    bouyer 
    426   1.1    bouyer #define mpii_cfg_page(_s, _a, _h, _r, _p, _l) \
    427   1.1    bouyer 	mpii_req_cfg_page((_s), (_a), MPII_PG_POLL, \
    428   1.1    bouyer 	    (_h), (_r), (_p), (_l))
    429   1.1    bouyer #define mpii_ecfg_page(_s, _a, _h, _r, _p, _l) \
    430   1.1    bouyer 	mpii_req_cfg_page((_s), (_a), MPII_PG_POLL|MPII_PG_EXTENDED, \
    431   1.1    bouyer 	    (_h), (_r), (_p), (_l))
    432   1.1    bouyer 
    433   1.1    bouyer 
    434   1.1    bouyer static const struct mpii_pci_product {
    435   1.1    bouyer 	pci_vendor_id_t         mpii_vendor;
    436   1.1    bouyer 	pci_product_id_t        mpii_product;
    437   1.1    bouyer } mpii_devices[] = {
    438   1.1    bouyer 	{ PCI_VENDOR_SYMBIOS,	PCI_PRODUCT_SYMBIOS_SAS2004 },
    439   1.1    bouyer 	{ PCI_VENDOR_SYMBIOS,	PCI_PRODUCT_SYMBIOS_SAS2008 },
    440   1.1    bouyer 	{ PCI_VENDOR_SYMBIOS,	PCI_PRODUCT_SYMBIOS_SAS2108_3 },
    441   1.1    bouyer 	{ PCI_VENDOR_SYMBIOS,	PCI_PRODUCT_SYMBIOS_SAS2108_4 },
    442   1.1    bouyer 	{ PCI_VENDOR_SYMBIOS,	PCI_PRODUCT_SYMBIOS_SAS2108_5 },
    443   1.1    bouyer 	{ PCI_VENDOR_SYMBIOS,	PCI_PRODUCT_SYMBIOS_SAS2116_1 },
    444   1.1    bouyer 	{ PCI_VENDOR_SYMBIOS,	PCI_PRODUCT_SYMBIOS_SAS2116_2 },
    445   1.1    bouyer 	{ PCI_VENDOR_SYMBIOS,	PCI_PRODUCT_SYMBIOS_SAS2208_1 },
    446   1.1    bouyer 	{ PCI_VENDOR_SYMBIOS,	PCI_PRODUCT_SYMBIOS_SAS2208_2 },
    447   1.1    bouyer 	{ PCI_VENDOR_SYMBIOS,	PCI_PRODUCT_SYMBIOS_SAS2208_3 },
    448   1.1    bouyer 	{ PCI_VENDOR_SYMBIOS,	PCI_PRODUCT_SYMBIOS_SAS2208_4 },
    449   1.1    bouyer 	{ PCI_VENDOR_SYMBIOS,	PCI_PRODUCT_SYMBIOS_SAS2208_5 },
    450   1.1    bouyer 	{ PCI_VENDOR_SYMBIOS,	PCI_PRODUCT_SYMBIOS_SAS2208_6 },
    451   1.1    bouyer 	{ PCI_VENDOR_SYMBIOS,	PCI_PRODUCT_SYMBIOS_SAS2308_1 },
    452   1.1    bouyer 	{ PCI_VENDOR_SYMBIOS,	PCI_PRODUCT_SYMBIOS_SAS2308_2 },
    453   1.1    bouyer 	{ PCI_VENDOR_SYMBIOS,	PCI_PRODUCT_SYMBIOS_SAS2308_3 },
    454   1.1    bouyer 	{ 0,	0 }
    455   1.1    bouyer };
    456   1.1    bouyer 
    457   1.1    bouyer static int
    458   1.1    bouyer mpii_match(device_t parent, cfdata_t match, void *aux)
    459   1.1    bouyer {
    460   1.1    bouyer 	struct pci_attach_args *pa = aux;
    461   1.1    bouyer 	const struct mpii_pci_product *mpii;
    462   1.1    bouyer 
    463   1.1    bouyer 	for (mpii = mpii_devices; mpii->mpii_vendor != 0; mpii++) {
    464   1.1    bouyer 		if (PCI_VENDOR(pa->pa_id) == mpii->mpii_vendor &&
    465   1.1    bouyer 		    PCI_PRODUCT(pa->pa_id) == mpii->mpii_product)
    466   1.1    bouyer 			return (1);
    467   1.1    bouyer 	}
    468   1.1    bouyer 	return (0);
    469   1.1    bouyer }
    470   1.1    bouyer 
    471   1.1    bouyer static void
    472   1.1    bouyer mpii_attach(device_t parent, device_t self, void *aux)
    473   1.1    bouyer {
    474   1.1    bouyer 	struct mpii_softc		*sc = device_private(self);
    475   1.1    bouyer 	struct pci_attach_args		*pa = aux;
    476   1.1    bouyer 	pcireg_t			memtype;
    477   1.1    bouyer 	int				r;
    478   1.1    bouyer 	pci_intr_handle_t		ih;
    479   1.1    bouyer 	const char			*intrstr;
    480   1.1    bouyer 	struct mpii_ccb			*ccb;
    481   1.1    bouyer 	struct scsipi_adapter *adapt = &sc->sc_adapt;
    482   1.1    bouyer 	struct scsipi_channel *chan = &sc->sc_chan;
    483   1.1    bouyer 	char wkname[15];
    484   1.5  christos 	char intrbuf[PCI_INTRSTR_LEN];
    485   1.1    bouyer 
    486   1.1    bouyer 	pci_aprint_devinfo(pa, NULL);
    487   1.1    bouyer 
    488   1.1    bouyer 	sc->sc_pc = pa->pa_pc;
    489   1.1    bouyer 	sc->sc_tag = pa->pa_tag;
    490   1.1    bouyer 	sc->sc_dmat = pa->pa_dmat;
    491   1.1    bouyer 	sc->sc_dev = self;
    492   1.1    bouyer 
    493   1.1    bouyer 	mutex_init(&sc->sc_req_mtx, MUTEX_DEFAULT, IPL_BIO);
    494   1.1    bouyer 	mutex_init(&sc->sc_rep_mtx, MUTEX_DEFAULT, IPL_BIO);
    495   1.1    bouyer 	mutex_init(&sc->sc_ccb_free_mtx, MUTEX_DEFAULT, IPL_BIO);
    496   1.1    bouyer 	cv_init(&sc->sc_ccb_free_cv, "mpii_ccbs");
    497   1.1    bouyer 	mutex_init(&sc->sc_ccb_mtx, MUTEX_DEFAULT, IPL_BIO);
    498   1.1    bouyer 
    499   1.1    bouyer 	snprintf(wkname, sizeof(wkname), "%s_tmo", DEVNAME(sc));
    500   1.1    bouyer 	if (workqueue_create(&sc->sc_ssb_tmowk, wkname,
    501   1.1    bouyer 	    mpii_scsi_cmd_tmo_handler, sc, PRI_NONE, IPL_BIO, WQ_MPSAFE) != 0) {
    502   1.1    bouyer 		aprint_error_dev(self, "can't create %s workqueue\n", wkname);
    503   1.1    bouyer 		return;
    504   1.1    bouyer 	}
    505   1.1    bouyer 
    506   1.1    bouyer 	snprintf(wkname, sizeof(wkname), "%s_evt", DEVNAME(sc));
    507   1.1    bouyer 	if (workqueue_create(&sc->sc_ssb_evt_ackwk, wkname,
    508   1.1    bouyer 	    mpii_eventack, sc, PRI_NONE, IPL_BIO, WQ_MPSAFE) != 0) {
    509   1.1    bouyer 		aprint_error_dev(self, "can't create %s workqueue\n", wkname);
    510   1.1    bouyer 		return;
    511   1.1    bouyer 	}
    512   1.1    bouyer 
    513   1.1    bouyer 	/* find the appropriate memory base */
    514   1.1    bouyer 	for (r = PCI_MAPREG_START; r < PCI_MAPREG_END; r += sizeof(memtype)) {
    515   1.1    bouyer 		memtype = pci_mapreg_type(sc->sc_pc, sc->sc_tag, r);
    516   1.1    bouyer 		if ((memtype & PCI_MAPREG_TYPE_MASK) == PCI_MAPREG_TYPE_MEM)
    517   1.1    bouyer 			break;
    518   1.1    bouyer 	}
    519   1.1    bouyer 	if (r >= PCI_MAPREG_END) {
    520   1.1    bouyer 		aprint_error_dev(self,
    521   1.1    bouyer 		    "unable to locate system interface registers\n");
    522   1.1    bouyer 		return;
    523   1.1    bouyer 	}
    524   1.1    bouyer 
    525   1.1    bouyer 	if (pci_mapreg_map(pa, r, memtype, 0, &sc->sc_iot, &sc->sc_ioh,
    526   1.1    bouyer 	    NULL, &sc->sc_ios) != 0) {
    527   1.1    bouyer 		aprint_error_dev(self,
    528   1.1    bouyer 		    "unable to map system interface registers\n");
    529   1.1    bouyer 		return;
    530   1.1    bouyer 	}
    531   1.1    bouyer 
    532   1.1    bouyer 	/* disable the expansion rom */
    533   1.1    bouyer 	PWRITE(sc, PCI_MAPREG_ROM,
    534   1.1    bouyer 	    PREAD(sc, PCI_MAPREG_ROM) & ~PCI_MAPREG_ROM_ENABLE);
    535   1.1    bouyer 
    536   1.1    bouyer 	/* disable interrupts */
    537   1.1    bouyer 	mpii_write(sc, MPII_INTR_MASK,
    538   1.1    bouyer 	    MPII_INTR_MASK_RESET | MPII_INTR_MASK_REPLY |
    539   1.1    bouyer 	    MPII_INTR_MASK_DOORBELL);
    540   1.1    bouyer 
    541   1.1    bouyer 	/* hook up the interrupt */
    542   1.1    bouyer 	if (pci_intr_map(pa, &ih) != 0) {
    543   1.1    bouyer 		aprint_error_dev(self, "unable to map interrupt\n");
    544   1.1    bouyer 		goto unmap;
    545   1.1    bouyer 	}
    546   1.5  christos 	intrstr = pci_intr_string(pa->pa_pc, ih, intrbuf, sizeof(intrbuf));
    547   1.1    bouyer 
    548   1.1    bouyer 	if (mpii_init(sc) != 0) {
    549   1.1    bouyer 		aprint_error_dev(self, "unable to initialize ioc\n");
    550   1.1    bouyer 		goto unmap;
    551   1.1    bouyer 	}
    552   1.1    bouyer 
    553   1.1    bouyer 	if (mpii_iocfacts(sc) != 0) {
    554   1.1    bouyer 		aprint_error_dev(self, "unable to get iocfacts\n");
    555   1.1    bouyer 		goto unmap;
    556   1.1    bouyer 	}
    557   1.1    bouyer 
    558   1.1    bouyer 	if (mpii_alloc_ccbs(sc) != 0) {
    559   1.1    bouyer 		/* error already printed */
    560   1.1    bouyer 		goto unmap;
    561   1.1    bouyer 	}
    562   1.1    bouyer 
    563   1.1    bouyer 	if (mpii_alloc_replies(sc) != 0) {
    564   1.1    bouyer 		aprint_error_dev(self, "unable to allocated reply space\n");
    565   1.1    bouyer 		goto free_ccbs;
    566   1.1    bouyer 	}
    567   1.1    bouyer 
    568   1.1    bouyer 	if (mpii_alloc_queues(sc) != 0) {
    569   1.1    bouyer 		aprint_error_dev(self, "unable to allocate reply queues\n");
    570   1.1    bouyer 		goto free_replies;
    571   1.1    bouyer 	}
    572   1.1    bouyer 
    573   1.1    bouyer 	if (mpii_iocinit(sc) != 0) {
    574   1.1    bouyer 		aprint_error_dev(self, "unable to send iocinit\n");
    575   1.1    bouyer 		goto free_queues;
    576   1.1    bouyer 	}
    577   1.1    bouyer 
    578   1.1    bouyer 	if (mpii_wait_eq(sc, MPII_DOORBELL, MPII_DOORBELL_STATE,
    579   1.1    bouyer 	    MPII_DOORBELL_STATE_OPER) != 0) {
    580   1.1    bouyer 		aprint_error_dev(self, "state: 0x%08x\n",
    581   1.1    bouyer 			mpii_read_db(sc) & MPII_DOORBELL_STATE);
    582   1.1    bouyer 		aprint_error_dev(self, "operational state timeout\n");
    583   1.1    bouyer 		goto free_queues;
    584   1.1    bouyer 	}
    585   1.1    bouyer 
    586   1.1    bouyer 	mpii_push_replies(sc);
    587   1.1    bouyer 	mpii_init_queues(sc);
    588   1.1    bouyer 
    589   1.1    bouyer 	if (mpii_portfacts(sc) != 0) {
    590   1.1    bouyer 		aprint_error_dev(self, "unable to get portfacts\n");
    591   1.1    bouyer 		goto free_queues;
    592   1.1    bouyer 	}
    593   1.1    bouyer 
    594   1.1    bouyer 	if (mpii_get_ioc_pg8(sc) != 0) {
    595   1.1    bouyer 		aprint_error_dev(self, "unable to get ioc page 8\n");
    596   1.1    bouyer 		goto free_queues;
    597   1.1    bouyer 	}
    598   1.1    bouyer 
    599   1.1    bouyer 	if (mpii_cfg_coalescing(sc) != 0) {
    600   1.1    bouyer 		aprint_error_dev(self, "unable to configure coalescing\n");
    601   1.1    bouyer 		goto free_queues;
    602   1.1    bouyer 	}
    603   1.1    bouyer 
    604   1.1    bouyer 	/* XXX bail on unsupported porttype? */
    605   1.1    bouyer 	if ((sc->sc_porttype == MPII_PORTFACTS_PORTTYPE_SAS_PHYSICAL) ||
    606   1.1    bouyer 	    (sc->sc_porttype == MPII_PORTFACTS_PORTTYPE_SAS_VIRTUAL)) {
    607   1.1    bouyer 		if (mpii_eventnotify(sc) != 0) {
    608   1.1    bouyer 			aprint_error_dev(self, "unable to enable events\n");
    609   1.1    bouyer 			goto free_queues;
    610   1.1    bouyer 		}
    611   1.1    bouyer 	}
    612   1.1    bouyer 
    613   1.1    bouyer 	if (mpii_alloc_dev(sc) != 0) {
    614   1.1    bouyer 		aprint_error_dev(self,
    615   1.1    bouyer 		    "unable to allocate memory for mpii_device\n");
    616   1.1    bouyer 		goto free_queues;
    617   1.1    bouyer 	}
    618   1.1    bouyer 
    619   1.1    bouyer 	if (mpii_portenable(sc) != 0) {
    620   1.1    bouyer 		aprint_error_dev(self, "unable to enable port\n");
    621   1.1    bouyer 		goto free_dev;
    622   1.1    bouyer 	}
    623   1.1    bouyer 
    624   1.1    bouyer 	sc->sc_ih = pci_intr_establish(sc->sc_pc, ih, IPL_BIO,
    625   1.1    bouyer 	    mpii_intr, sc);
    626   1.1    bouyer 	if (sc->sc_ih == NULL) {
    627   1.1    bouyer 		aprint_error_dev(self, "can't establish interrupt");
    628   1.1    bouyer 		if (intrstr)
    629   1.1    bouyer 			aprint_error(" at %s", intrstr);
    630   1.1    bouyer 		aprint_error("\n");
    631   1.1    bouyer 		goto free_dev;
    632   1.1    bouyer 	}
    633   1.1    bouyer 
    634   1.1    bouyer 	memset(adapt, 0, sizeof(*adapt));
    635   1.1    bouyer 	adapt->adapt_dev = sc->sc_dev;
    636   1.1    bouyer 	adapt->adapt_nchannels = 1;
    637   1.1    bouyer 	adapt->adapt_openings = sc->sc_request_depth - 1;
    638   1.1    bouyer 	adapt->adapt_max_periph = adapt->adapt_openings;
    639   1.1    bouyer 	adapt->adapt_request = mpii_scsipi_request;
    640   1.1    bouyer 	adapt->adapt_minphys = mpii_minphys;
    641   1.1    bouyer 
    642   1.1    bouyer 	memset(chan, 0, sizeof(*chan));
    643   1.1    bouyer 	chan->chan_adapter = adapt;
    644   1.1    bouyer 	chan->chan_bustype = &scsi_sas_bustype;
    645   1.1    bouyer 	chan->chan_channel = 0;
    646   1.1    bouyer 	chan->chan_flags = 0;
    647   1.2    kardel 	chan->chan_nluns = 8;
    648   1.1    bouyer 	chan->chan_ntargets = sc->sc_max_devices;
    649   1.1    bouyer 	chan->chan_id = -1;
    650   1.1    bouyer 
    651   1.1    bouyer 	mpii_rescan(self, "scsi", NULL);
    652   1.1    bouyer 
    653   1.1    bouyer 	/* enable interrupts */
    654   1.1    bouyer 	mpii_write(sc, MPII_INTR_MASK, MPII_INTR_MASK_DOORBELL
    655   1.1    bouyer 	    | MPII_INTR_MASK_RESET);
    656   1.1    bouyer 
    657   1.1    bouyer #if NBIO > 0
    658   1.1    bouyer 	if (ISSET(sc->sc_flags, MPII_F_RAID)) {
    659   1.1    bouyer 		if (bio_register(sc->sc_dev, mpii_ioctl) != 0)
    660   1.1    bouyer 			panic("%s: controller registration failed",
    661   1.1    bouyer 			    DEVNAME(sc));
    662   1.1    bouyer 
    663   1.1    bouyer 		if (mpii_create_sensors(sc) != 0)
    664   1.1    bouyer 			aprint_error_dev(self, "unable to create sensors\n");
    665   1.1    bouyer 	}
    666   1.1    bouyer #endif
    667   1.1    bouyer 
    668   1.1    bouyer 	return;
    669   1.1    bouyer 
    670   1.1    bouyer free_dev:
    671   1.1    bouyer 	if (sc->sc_devs)
    672   1.1    bouyer 		free(sc->sc_devs, M_DEVBUF);
    673   1.1    bouyer 
    674   1.1    bouyer free_queues:
    675   1.1    bouyer 	bus_dmamap_sync(sc->sc_dmat, MPII_DMA_MAP(sc->sc_reply_freeq),
    676   1.1    bouyer      	    0, sc->sc_reply_free_qdepth * 4, BUS_DMASYNC_POSTREAD);
    677   1.1    bouyer 	mpii_dmamem_free(sc, sc->sc_reply_freeq);
    678   1.1    bouyer 
    679   1.1    bouyer 	bus_dmamap_sync(sc->sc_dmat, MPII_DMA_MAP(sc->sc_reply_postq),
    680   1.1    bouyer 	    0, sc->sc_reply_post_qdepth * 8, BUS_DMASYNC_POSTREAD);
    681   1.1    bouyer 	mpii_dmamem_free(sc, sc->sc_reply_postq);
    682   1.1    bouyer 
    683   1.1    bouyer free_replies:
    684   1.1    bouyer 	bus_dmamap_sync(sc->sc_dmat, MPII_DMA_MAP(sc->sc_replies),
    685   1.1    bouyer 		0, PAGE_SIZE, BUS_DMASYNC_POSTREAD);
    686   1.1    bouyer 	mpii_dmamem_free(sc, sc->sc_replies);
    687   1.1    bouyer 
    688   1.1    bouyer free_ccbs:
    689   1.1    bouyer 	while ((ccb = mpii_get_ccb(sc, MPII_NOSLEEP)) != NULL)
    690   1.1    bouyer 		bus_dmamap_destroy(sc->sc_dmat, ccb->ccb_dmamap);
    691   1.1    bouyer 	mpii_dmamem_free(sc, sc->sc_requests);
    692   1.1    bouyer 	free(sc->sc_ccbs, M_DEVBUF);
    693   1.1    bouyer 
    694   1.1    bouyer unmap:
    695   1.1    bouyer 	bus_space_unmap(sc->sc_iot, sc->sc_ioh, sc->sc_ios);
    696   1.1    bouyer 	sc->sc_ios = 0;
    697   1.1    bouyer }
    698   1.1    bouyer 
    699   1.1    bouyer static int
    700   1.1    bouyer mpii_detach(device_t self, int flags)
    701   1.1    bouyer {
    702   1.1    bouyer 	struct mpii_softc		*sc = device_private(self);
    703   1.1    bouyer 	int error;
    704   1.1    bouyer 	struct mpii_ccb *ccb;
    705   1.1    bouyer 
    706   1.1    bouyer 	if ((error = config_detach_children(sc->sc_dev, flags)) != 0)
    707   1.1    bouyer 		return error;
    708   1.1    bouyer 
    709   1.1    bouyer #if NBIO > 0
    710   1.1    bouyer 	mpii_destroy_sensors(sc);
    711   1.1    bouyer 	bio_unregister(sc->sc_dev);
    712   1.1    bouyer #endif /* NBIO > 0 */
    713   1.1    bouyer 
    714   1.1    bouyer 	if (sc->sc_ih != NULL) {
    715   1.1    bouyer 		if (sc->sc_devs)
    716   1.1    bouyer 			free(sc->sc_devs, M_DEVBUF);
    717   1.1    bouyer 
    718   1.1    bouyer 		bus_dmamap_sync(sc->sc_dmat, MPII_DMA_MAP(sc->sc_reply_freeq),
    719   1.1    bouyer 		    0, sc->sc_reply_free_qdepth * 4, BUS_DMASYNC_POSTREAD);
    720   1.1    bouyer 		mpii_dmamem_free(sc, sc->sc_reply_freeq);
    721   1.1    bouyer 
    722   1.1    bouyer 		bus_dmamap_sync(sc->sc_dmat, MPII_DMA_MAP(sc->sc_reply_postq),
    723   1.1    bouyer 		    0, sc->sc_reply_post_qdepth * 8, BUS_DMASYNC_POSTREAD);
    724   1.1    bouyer 		mpii_dmamem_free(sc, sc->sc_reply_postq);
    725   1.1    bouyer 
    726   1.1    bouyer 		bus_dmamap_sync(sc->sc_dmat, MPII_DMA_MAP(sc->sc_replies),
    727   1.1    bouyer 			0, PAGE_SIZE, BUS_DMASYNC_POSTREAD);
    728   1.1    bouyer 		mpii_dmamem_free(sc, sc->sc_replies);
    729   1.1    bouyer 
    730   1.1    bouyer 		while ((ccb = mpii_get_ccb(sc, MPII_NOSLEEP)) != NULL)
    731   1.1    bouyer 			bus_dmamap_destroy(sc->sc_dmat, ccb->ccb_dmamap);
    732   1.1    bouyer 		mpii_dmamem_free(sc, sc->sc_requests);
    733   1.1    bouyer 		free(sc->sc_ccbs, M_DEVBUF);
    734   1.1    bouyer 
    735   1.1    bouyer 		pci_intr_disestablish(sc->sc_pc, sc->sc_ih);
    736   1.1    bouyer 		sc->sc_ih = NULL;
    737   1.1    bouyer 	}
    738   1.1    bouyer 	if (sc->sc_ios != 0) {
    739   1.1    bouyer 		bus_space_unmap(sc->sc_iot, sc->sc_ioh, sc->sc_ios);
    740   1.1    bouyer 		sc->sc_ios = 0;
    741   1.1    bouyer 	}
    742   1.1    bouyer 
    743   1.1    bouyer 	return (0);
    744   1.1    bouyer }
    745   1.1    bouyer 
    746   1.1    bouyer static int
    747   1.1    bouyer mpii_rescan(device_t self, const char *ifattr, const int *locators)
    748   1.1    bouyer {
    749   1.1    bouyer 	struct mpii_softc *sc = device_private(self);
    750   1.1    bouyer 
    751   1.1    bouyer 	if (sc->sc_child != NULL)
    752   1.1    bouyer 		return 0;
    753   1.1    bouyer 
    754   1.1    bouyer 	sc->sc_child = config_found_sm_loc(self, ifattr, locators, &sc->sc_chan,
    755   1.1    bouyer 	    scsiprint, NULL);
    756   1.1    bouyer 
    757   1.1    bouyer 	return 0;
    758   1.1    bouyer }
    759   1.1    bouyer 
    760   1.1    bouyer static void
    761   1.1    bouyer mpii_childdetached(device_t self, device_t child)
    762   1.1    bouyer {
    763   1.1    bouyer         struct mpii_softc *sc = device_private(self);
    764   1.1    bouyer 
    765   1.1    bouyer         KASSERT(self == sc->sc_dev);
    766   1.1    bouyer         KASSERT(child == sc->sc_child);
    767   1.1    bouyer 
    768   1.1    bouyer         if (child == sc->sc_child)
    769   1.1    bouyer                 sc->sc_child = NULL;
    770   1.1    bouyer }
    771   1.1    bouyer 
    772   1.1    bouyer static int
    773   1.1    bouyer mpii_intr(void *arg)
    774   1.1    bouyer {
    775   1.1    bouyer 	struct mpii_rcb_list		evts = SIMPLEQ_HEAD_INITIALIZER(evts);
    776   1.1    bouyer 	struct mpii_ccb_list		ccbs = SIMPLEQ_HEAD_INITIALIZER(ccbs);
    777   1.1    bouyer 	struct mpii_softc		*sc = arg;
    778   1.1    bouyer 	struct mpii_reply_descr		*postq = sc->sc_reply_postq_kva, *rdp;
    779   1.1    bouyer 	struct mpii_ccb			*ccb;
    780   1.1    bouyer 	struct mpii_rcb			*rcb;
    781   1.1    bouyer 	int				smid;
    782   1.1    bouyer 	int				rv = 0;
    783   1.1    bouyer 
    784   1.1    bouyer 	mutex_enter(&sc->sc_rep_mtx);
    785   1.1    bouyer 	bus_dmamap_sync(sc->sc_dmat,
    786   1.1    bouyer 	    MPII_DMA_MAP(sc->sc_reply_postq),
    787   1.1    bouyer 	    0, 8 * sc->sc_reply_post_qdepth,
    788   1.1    bouyer 	    BUS_DMASYNC_POSTREAD | BUS_DMASYNC_POSTWRITE);
    789   1.1    bouyer 
    790   1.1    bouyer 	for (;;) {
    791   1.1    bouyer 		rdp = &postq[sc->sc_reply_post_host_index];
    792   1.1    bouyer 		if ((rdp->reply_flags & MPII_REPLY_DESCR_TYPE_MASK) ==
    793   1.1    bouyer 		    MPII_REPLY_DESCR_UNUSED)
    794   1.1    bouyer 			break;
    795   1.1    bouyer 		if (rdp->data == 0xffffffff) {
    796   1.1    bouyer 			/*
    797   1.1    bouyer 			 * ioc is still writing to the reply post queue
    798   1.1    bouyer 			 * race condition - bail!
    799   1.1    bouyer 			 */
    800   1.1    bouyer 			break;
    801   1.1    bouyer 		}
    802   1.1    bouyer 
    803   1.1    bouyer 		smid = le16toh(rdp->smid);
    804   1.1    bouyer 		rcb = mpii_reply(sc, rdp);
    805   1.1    bouyer 
    806   1.1    bouyer 		if (smid) {
    807   1.1    bouyer 			ccb = &sc->sc_ccbs[smid - 1];
    808   1.1    bouyer 			ccb->ccb_state = MPII_CCB_READY;
    809   1.1    bouyer 			ccb->ccb_rcb = rcb;
    810   1.1    bouyer 			SIMPLEQ_INSERT_TAIL(&ccbs, ccb, u.ccb_link);
    811   1.1    bouyer 		} else
    812   1.1    bouyer 			SIMPLEQ_INSERT_TAIL(&evts, rcb, u.rcb_link);
    813   1.1    bouyer 
    814   1.1    bouyer 		sc->sc_reply_post_host_index++;
    815   1.1    bouyer 		sc->sc_reply_post_host_index %= sc->sc_reply_post_qdepth;
    816   1.1    bouyer 		rv = 1;
    817   1.1    bouyer 	}
    818   1.1    bouyer 
    819   1.1    bouyer 	bus_dmamap_sync(sc->sc_dmat,
    820   1.1    bouyer 	    MPII_DMA_MAP(sc->sc_reply_postq),
    821   1.1    bouyer 	    0, 8 * sc->sc_reply_post_qdepth,
    822   1.1    bouyer 	    BUS_DMASYNC_PREREAD | BUS_DMASYNC_PREWRITE);
    823   1.1    bouyer 
    824   1.1    bouyer 	if (rv)
    825   1.1    bouyer 		mpii_write_reply_post(sc, sc->sc_reply_post_host_index);
    826   1.1    bouyer 
    827   1.1    bouyer 	mutex_exit(&sc->sc_rep_mtx);
    828   1.1    bouyer 
    829   1.1    bouyer 	if (rv == 0)
    830   1.1    bouyer 		return (0);
    831   1.1    bouyer 
    832   1.1    bouyer 	while ((ccb = SIMPLEQ_FIRST(&ccbs)) != NULL) {
    833   1.1    bouyer 		SIMPLEQ_REMOVE_HEAD(&ccbs, u.ccb_link);
    834   1.1    bouyer 		ccb->ccb_done(ccb);
    835   1.1    bouyer 	}
    836   1.1    bouyer 	while ((rcb = SIMPLEQ_FIRST(&evts)) != NULL) {
    837   1.1    bouyer 		SIMPLEQ_REMOVE_HEAD(&evts, u.rcb_link);
    838   1.1    bouyer 		mpii_event_process(sc, rcb);
    839   1.1    bouyer 	}
    840   1.1    bouyer 
    841   1.1    bouyer 	return (1);
    842   1.1    bouyer }
    843   1.1    bouyer 
    844   1.1    bouyer static int
    845   1.1    bouyer mpii_load_xs(struct mpii_ccb *ccb)
    846   1.1    bouyer {
    847   1.1    bouyer 	struct mpii_softc	*sc = ccb->ccb_sc;
    848   1.1    bouyer 	struct scsipi_xfer	*xs = ccb->ccb_cookie;
    849   1.1    bouyer 	struct mpii_ccb_bundle	*mcb = ccb->ccb_cmd;
    850   1.1    bouyer 	struct mpii_msg_scsi_io	*io = &mcb->mcb_io;
    851   1.1    bouyer 	struct mpii_sge		*sge = NULL, *nsge = &mcb->mcb_sgl[0];
    852   1.1    bouyer 	struct mpii_sge		*ce = NULL, *nce = NULL;
    853   1.1    bouyer 	u_int64_t		ce_dva;
    854   1.1    bouyer 	bus_dmamap_t		dmap = ccb->ccb_dmamap;
    855   1.1    bouyer 	u_int32_t		addr, flags;
    856   1.1    bouyer 	int			i, error;
    857   1.1    bouyer 
    858   1.1    bouyer 	/* zero length transfer still requires an SGE */
    859   1.1    bouyer 	if (xs->datalen == 0) {
    860   1.1    bouyer 		nsge->sg_hdr = htole32(MPII_SGE_FL_TYPE_SIMPLE |
    861   1.1    bouyer 		    MPII_SGE_FL_LAST | MPII_SGE_FL_EOB | MPII_SGE_FL_EOL);
    862   1.1    bouyer 		return (0);
    863   1.1    bouyer 	}
    864   1.1    bouyer 
    865   1.1    bouyer 	error = bus_dmamap_load(sc->sc_dmat, dmap,
    866   1.1    bouyer 	    xs->data, xs->datalen, NULL, (xs->xs_control & XS_CTL_NOSLEEP) ?
    867   1.1    bouyer 	      BUS_DMA_NOWAIT : BUS_DMA_WAITOK);
    868   1.1    bouyer 	if (error) {
    869   1.1    bouyer 		aprint_error_dev(sc->sc_dev, "error %d loading dmamap\n",
    870   1.1    bouyer 		    error);
    871   1.1    bouyer 		return (1);
    872   1.1    bouyer 	}
    873   1.1    bouyer 
    874   1.1    bouyer 	/* safe default staring flags */
    875   1.1    bouyer 	flags = MPII_SGE_FL_TYPE_SIMPLE | MPII_SGE_FL_SIZE_64;
    876   1.1    bouyer 	/* if data out */
    877   1.1    bouyer 	if (xs->xs_control & XS_CTL_DATA_OUT)
    878   1.1    bouyer 		flags |= MPII_SGE_FL_DIR_OUT;
    879   1.1    bouyer 
    880   1.1    bouyer 	/* we will have to exceed the SGEs we can cram into the request frame */
    881   1.1    bouyer 	if (dmap->dm_nsegs > sc->sc_first_sgl_len) {
    882   1.1    bouyer 		ce = &mcb->mcb_sgl[sc->sc_first_sgl_len - 1];
    883   1.1    bouyer 		io->chain_offset = ((u_int8_t *)ce - (u_int8_t *)io) / 4;
    884   1.1    bouyer 	}
    885   1.1    bouyer 
    886   1.1    bouyer 	for (i = 0; i < dmap->dm_nsegs; i++) {
    887   1.1    bouyer 		if (nsge == ce) {
    888   1.1    bouyer 			nsge++;
    889   1.1    bouyer 			sge->sg_hdr |= htole32(MPII_SGE_FL_LAST);
    890   1.1    bouyer 
    891   1.1    bouyer 			DNPRINTF(MPII_D_DMA, "%s:   - 0x%08x 0x%08x 0x%08x\n",
    892   1.1    bouyer 			    DEVNAME(sc), sge->sg_hdr,
    893   1.1    bouyer 			    sge->sg_hi_addr, sge->sg_lo_addr);
    894   1.1    bouyer 
    895   1.1    bouyer 			if ((dmap->dm_nsegs - i) > sc->sc_chain_len) {
    896   1.1    bouyer 				nce = &nsge[sc->sc_chain_len - 1];
    897   1.1    bouyer 				addr = ((u_int8_t *)nce - (u_int8_t *)nsge) / 4;
    898   1.1    bouyer 				addr = addr << 16 |
    899   1.1    bouyer 				    sizeof(struct mpii_sge) * sc->sc_chain_len;
    900   1.1    bouyer 			} else {
    901   1.1    bouyer 				nce = NULL;
    902   1.1    bouyer 				addr = sizeof(struct mpii_sge) *
    903   1.1    bouyer 				    (dmap->dm_nsegs - i);
    904   1.1    bouyer 			}
    905   1.1    bouyer 
    906   1.1    bouyer 			ce->sg_hdr = htole32(MPII_SGE_FL_TYPE_CHAIN |
    907   1.1    bouyer 			    MPII_SGE_FL_SIZE_64 | addr);
    908   1.1    bouyer 
    909   1.1    bouyer 			ce_dva = ccb->ccb_cmd_dva +
    910   1.1    bouyer 			    ((u_int8_t *)nsge - (u_int8_t *)mcb);
    911   1.1    bouyer 
    912   1.1    bouyer 			addr = (u_int32_t)(ce_dva >> 32);
    913   1.1    bouyer 			ce->sg_hi_addr = htole32(addr);
    914   1.1    bouyer 			addr = (u_int32_t)ce_dva;
    915   1.1    bouyer 			ce->sg_lo_addr = htole32(addr);
    916   1.1    bouyer 
    917   1.1    bouyer 			DNPRINTF(MPII_D_DMA, "%s:  ce: 0x%08x 0x%08x 0x%08x\n",
    918   1.1    bouyer 			    DEVNAME(sc), ce->sg_hdr, ce->sg_hi_addr,
    919   1.1    bouyer 			    ce->sg_lo_addr);
    920   1.1    bouyer 
    921   1.1    bouyer 			ce = nce;
    922   1.1    bouyer 		}
    923   1.1    bouyer 
    924   1.1    bouyer 		DNPRINTF(MPII_D_DMA, "%s:  %d: %" PRId64 " 0x%016" PRIx64 "\n",
    925   1.1    bouyer 		    DEVNAME(sc), i, (int64_t)dmap->dm_segs[i].ds_len,
    926   1.1    bouyer 		    (u_int64_t)dmap->dm_segs[i].ds_addr);
    927   1.1    bouyer 
    928   1.1    bouyer 		sge = nsge;
    929   1.1    bouyer 
    930   1.1    bouyer 		sge->sg_hdr = htole32(flags | dmap->dm_segs[i].ds_len);
    931   1.1    bouyer 		addr = (u_int32_t)((u_int64_t)dmap->dm_segs[i].ds_addr >> 32);
    932   1.1    bouyer 		sge->sg_hi_addr = htole32(addr);
    933   1.1    bouyer 		addr = (u_int32_t)dmap->dm_segs[i].ds_addr;
    934   1.1    bouyer 		sge->sg_lo_addr = htole32(addr);
    935   1.1    bouyer 
    936   1.1    bouyer 		DNPRINTF(MPII_D_DMA, "%s:  %d: 0x%08x 0x%08x 0x%08x\n",
    937   1.1    bouyer 		    DEVNAME(sc), i, sge->sg_hdr, sge->sg_hi_addr,
    938   1.1    bouyer 		    sge->sg_lo_addr);
    939   1.1    bouyer 
    940   1.1    bouyer 		nsge = sge + 1;
    941   1.1    bouyer 	}
    942   1.1    bouyer 
    943   1.1    bouyer 	/* terminate list */
    944   1.1    bouyer 	sge->sg_hdr |= htole32(MPII_SGE_FL_LAST | MPII_SGE_FL_EOB |
    945   1.1    bouyer 	    MPII_SGE_FL_EOL);
    946   1.1    bouyer 
    947   1.1    bouyer 	bus_dmamap_sync(sc->sc_dmat, dmap, 0, dmap->dm_mapsize,
    948   1.1    bouyer 	    (xs->xs_control & XS_CTL_DATA_IN) ? BUS_DMASYNC_PREREAD :
    949   1.1    bouyer 	    BUS_DMASYNC_PREWRITE);
    950   1.1    bouyer 
    951   1.1    bouyer 	return (0);
    952   1.1    bouyer }
    953   1.1    bouyer 
    954   1.1    bouyer static u_int32_t
    955   1.1    bouyer mpii_read(struct mpii_softc *sc, bus_size_t r)
    956   1.1    bouyer {
    957   1.1    bouyer 	u_int32_t			rv;
    958   1.1    bouyer 
    959   1.1    bouyer 	bus_space_barrier(sc->sc_iot, sc->sc_ioh, r, 4,
    960   1.1    bouyer 	    BUS_SPACE_BARRIER_READ);
    961   1.1    bouyer 	rv = bus_space_read_4(sc->sc_iot, sc->sc_ioh, r);
    962   1.1    bouyer 
    963   1.1    bouyer 	DNPRINTF(MPII_D_RW, "%s: mpii_read %#" PRIx64 " %#x\n", DEVNAME(sc),
    964   1.1    bouyer 	    (uint64_t)r, rv);
    965   1.1    bouyer 
    966   1.1    bouyer 	return (rv);
    967   1.1    bouyer }
    968   1.1    bouyer 
    969   1.1    bouyer static void
    970   1.1    bouyer mpii_write(struct mpii_softc *sc, bus_size_t r, u_int32_t v)
    971   1.1    bouyer {
    972   1.1    bouyer 	DNPRINTF(MPII_D_RW, "%s: mpii_write %#" PRIx64 " %#x\n", DEVNAME(sc),
    973   1.1    bouyer 	    (uint64_t)r, v);
    974   1.1    bouyer 
    975   1.1    bouyer 	bus_space_write_4(sc->sc_iot, sc->sc_ioh, r, v);
    976   1.1    bouyer 	bus_space_barrier(sc->sc_iot, sc->sc_ioh, r, 4,
    977   1.1    bouyer 	    BUS_SPACE_BARRIER_WRITE);
    978   1.1    bouyer }
    979   1.1    bouyer 
    980   1.1    bouyer 
    981   1.1    bouyer static int
    982   1.1    bouyer mpii_wait_eq(struct mpii_softc *sc, bus_size_t r, u_int32_t mask,
    983   1.1    bouyer     u_int32_t target)
    984   1.1    bouyer {
    985   1.1    bouyer 	int			i;
    986   1.1    bouyer 
    987   1.1    bouyer 	DNPRINTF(MPII_D_RW, "%s: mpii_wait_eq %#" PRIx64 " %#x %#x\n",
    988   1.1    bouyer 	    DEVNAME(sc), (uint64_t)r, mask, target);
    989   1.1    bouyer 
    990   1.1    bouyer 	for (i = 0; i < 15000; i++) {
    991   1.1    bouyer 		if ((mpii_read(sc, r) & mask) == target)
    992   1.1    bouyer 			return (0);
    993   1.1    bouyer 		delay(1000);
    994   1.1    bouyer 	}
    995   1.1    bouyer 
    996   1.1    bouyer 	return (1);
    997   1.1    bouyer }
    998   1.1    bouyer 
    999   1.1    bouyer static int
   1000   1.1    bouyer mpii_wait_ne(struct mpii_softc *sc, bus_size_t r, u_int32_t mask,
   1001   1.1    bouyer     u_int32_t target)
   1002   1.1    bouyer {
   1003   1.1    bouyer 	int			i;
   1004   1.1    bouyer 
   1005   1.1    bouyer 	DNPRINTF(MPII_D_RW, "%s: mpii_wait_ne %#" PRIx64 " %#x %#x\n",
   1006   1.1    bouyer 	    DEVNAME(sc), (uint64_t)r, mask, target);
   1007   1.1    bouyer 
   1008   1.1    bouyer 	for (i = 0; i < 15000; i++) {
   1009   1.1    bouyer 		if ((mpii_read(sc, r) & mask) != target)
   1010   1.1    bouyer 			return (0);
   1011   1.1    bouyer 		delay(1000);
   1012   1.1    bouyer 	}
   1013   1.1    bouyer 
   1014   1.1    bouyer 	return (1);
   1015   1.1    bouyer }
   1016   1.1    bouyer 
   1017   1.1    bouyer 
   1018   1.1    bouyer static int
   1019   1.1    bouyer mpii_init(struct mpii_softc *sc)
   1020   1.1    bouyer {
   1021   1.1    bouyer 	u_int32_t		db;
   1022   1.1    bouyer 	int			i;
   1023   1.1    bouyer 
   1024   1.1    bouyer 	/* spin until the ioc leaves the reset state */
   1025   1.1    bouyer 	if (mpii_wait_ne(sc, MPII_DOORBELL, MPII_DOORBELL_STATE,
   1026   1.1    bouyer 	    MPII_DOORBELL_STATE_RESET) != 0) {
   1027   1.1    bouyer 		DNPRINTF(MPII_D_MISC, "%s: mpii_init timeout waiting to leave "
   1028   1.1    bouyer 		    "reset state\n", DEVNAME(sc));
   1029   1.1    bouyer 		return (1);
   1030   1.1    bouyer 	}
   1031   1.1    bouyer 
   1032   1.1    bouyer 	/* check current ownership */
   1033   1.1    bouyer 	db = mpii_read_db(sc);
   1034   1.1    bouyer 	if ((db & MPII_DOORBELL_WHOINIT) == MPII_DOORBELL_WHOINIT_PCIPEER) {
   1035   1.1    bouyer 		DNPRINTF(MPII_D_MISC, "%s: mpii_init initialised by pci peer\n",
   1036   1.1    bouyer 		    DEVNAME(sc));
   1037   1.1    bouyer 		return (0);
   1038   1.1    bouyer 	}
   1039   1.1    bouyer 
   1040   1.1    bouyer 	for (i = 0; i < 5; i++) {
   1041   1.1    bouyer 		switch (db & MPII_DOORBELL_STATE) {
   1042   1.1    bouyer 		case MPII_DOORBELL_STATE_READY:
   1043   1.1    bouyer 			DNPRINTF(MPII_D_MISC, "%s: mpii_init ioc is ready\n",
   1044   1.1    bouyer 			    DEVNAME(sc));
   1045   1.1    bouyer 			return (0);
   1046   1.1    bouyer 
   1047   1.1    bouyer 		case MPII_DOORBELL_STATE_OPER:
   1048   1.1    bouyer 			DNPRINTF(MPII_D_MISC, "%s: mpii_init ioc is oper\n",
   1049   1.1    bouyer 			    DEVNAME(sc));
   1050   1.1    bouyer 			if (sc->sc_ioc_event_replay)
   1051   1.1    bouyer 				mpii_reset_soft(sc);
   1052   1.1    bouyer 			else
   1053   1.1    bouyer 				mpii_reset_hard(sc);
   1054   1.1    bouyer 			break;
   1055   1.1    bouyer 
   1056   1.1    bouyer 		case MPII_DOORBELL_STATE_FAULT:
   1057   1.1    bouyer 			DNPRINTF(MPII_D_MISC, "%s: mpii_init ioc is being "
   1058   1.1    bouyer 			    "reset hard\n" , DEVNAME(sc));
   1059   1.1    bouyer 			mpii_reset_hard(sc);
   1060   1.1    bouyer 			break;
   1061   1.1    bouyer 
   1062   1.1    bouyer 		case MPII_DOORBELL_STATE_RESET:
   1063   1.1    bouyer 			DNPRINTF(MPII_D_MISC, "%s: mpii_init waiting to come "
   1064   1.1    bouyer 			    "out of reset\n", DEVNAME(sc));
   1065   1.1    bouyer 			if (mpii_wait_ne(sc, MPII_DOORBELL, MPII_DOORBELL_STATE,
   1066   1.1    bouyer 			    MPII_DOORBELL_STATE_RESET) != 0)
   1067   1.1    bouyer 				return (1);
   1068   1.1    bouyer 			break;
   1069   1.1    bouyer 		}
   1070   1.1    bouyer 		db = mpii_read_db(sc);
   1071   1.1    bouyer 	}
   1072   1.1    bouyer 
   1073   1.1    bouyer 	return (1);
   1074   1.1    bouyer }
   1075   1.1    bouyer 
   1076   1.1    bouyer static int
   1077   1.1    bouyer mpii_reset_soft(struct mpii_softc *sc)
   1078   1.1    bouyer {
   1079   1.1    bouyer 	DNPRINTF(MPII_D_MISC, "%s: mpii_reset_soft\n", DEVNAME(sc));
   1080   1.1    bouyer 
   1081   1.1    bouyer 	if (mpii_read_db(sc) & MPII_DOORBELL_INUSE) {
   1082   1.1    bouyer 		return (1);
   1083   1.1    bouyer 	}
   1084   1.1    bouyer 
   1085   1.1    bouyer 	mpii_write_db(sc,
   1086   1.1    bouyer 	    MPII_DOORBELL_FUNCTION(MPII_FUNCTION_IOC_MESSAGE_UNIT_RESET));
   1087   1.1    bouyer 
   1088   1.1    bouyer 	/* XXX LSI waits 15 sec */
   1089   1.1    bouyer 	if (mpii_wait_db_ack(sc) != 0)
   1090   1.1    bouyer 		return (1);
   1091   1.1    bouyer 
   1092   1.1    bouyer 	/* XXX LSI waits 15 sec */
   1093   1.1    bouyer 	if (mpii_wait_eq(sc, MPII_DOORBELL, MPII_DOORBELL_STATE,
   1094   1.1    bouyer 	    MPII_DOORBELL_STATE_READY) != 0)
   1095   1.1    bouyer 		return (1);
   1096   1.1    bouyer 
   1097   1.1    bouyer 	/* XXX wait for Sys2IOCDB bit to clear in HIS?? */
   1098   1.1    bouyer 
   1099   1.1    bouyer 	return (0);
   1100   1.1    bouyer }
   1101   1.1    bouyer 
   1102   1.1    bouyer static int
   1103   1.1    bouyer mpii_reset_hard(struct mpii_softc *sc)
   1104   1.1    bouyer {
   1105   1.1    bouyer 	u_int16_t		i;
   1106   1.1    bouyer 
   1107   1.1    bouyer 	DNPRINTF(MPII_D_MISC, "%s: mpii_reset_hard\n", DEVNAME(sc));
   1108   1.1    bouyer 
   1109   1.1    bouyer 	mpii_write_intr(sc, 0);
   1110   1.1    bouyer 
   1111   1.1    bouyer 	/* enable diagnostic register */
   1112   1.1    bouyer 	mpii_write(sc, MPII_WRITESEQ, MPII_WRITESEQ_FLUSH);
   1113   1.1    bouyer 	mpii_write(sc, MPII_WRITESEQ, MPII_WRITESEQ_1);
   1114   1.1    bouyer 	mpii_write(sc, MPII_WRITESEQ, MPII_WRITESEQ_2);
   1115   1.1    bouyer 	mpii_write(sc, MPII_WRITESEQ, MPII_WRITESEQ_3);
   1116   1.1    bouyer 	mpii_write(sc, MPII_WRITESEQ, MPII_WRITESEQ_4);
   1117   1.1    bouyer 	mpii_write(sc, MPII_WRITESEQ, MPII_WRITESEQ_5);
   1118   1.1    bouyer 	mpii_write(sc, MPII_WRITESEQ, MPII_WRITESEQ_6);
   1119   1.1    bouyer 
   1120   1.1    bouyer 	delay(100);
   1121   1.1    bouyer 
   1122   1.1    bouyer 	if ((mpii_read(sc, MPII_HOSTDIAG) & MPII_HOSTDIAG_DWRE) == 0) {
   1123   1.1    bouyer 		DNPRINTF(MPII_D_MISC, "%s: mpii_reset_hard failure to enable "
   1124   1.1    bouyer 		    "diagnostic read/write\n", DEVNAME(sc));
   1125   1.1    bouyer 		return(1);
   1126   1.1    bouyer 	}
   1127   1.1    bouyer 
   1128   1.1    bouyer 	/* reset ioc */
   1129   1.1    bouyer 	mpii_write(sc, MPII_HOSTDIAG, MPII_HOSTDIAG_RESET_ADAPTER);
   1130   1.1    bouyer 
   1131   1.1    bouyer 	/* 240 milliseconds */
   1132   1.1    bouyer 	delay(240000);
   1133   1.1    bouyer 
   1134   1.1    bouyer 
   1135   1.1    bouyer 	/* XXX this whole function should be more robust */
   1136   1.1    bouyer 
   1137   1.1    bouyer 	/* XXX  read the host diagnostic reg until reset adapter bit clears ? */
   1138   1.1    bouyer 	for (i = 0; i < 30000; i++) {
   1139   1.1    bouyer 		if ((mpii_read(sc, MPII_HOSTDIAG) &
   1140   1.1    bouyer 		    MPII_HOSTDIAG_RESET_ADAPTER) == 0)
   1141   1.1    bouyer 			break;
   1142   1.1    bouyer 		delay(10000);
   1143   1.1    bouyer 	}
   1144   1.1    bouyer 
   1145   1.1    bouyer 	/* disable diagnostic register */
   1146   1.1    bouyer 	mpii_write(sc, MPII_WRITESEQ, 0xff);
   1147   1.1    bouyer 
   1148   1.1    bouyer 	/* XXX what else? */
   1149   1.1    bouyer 
   1150   1.1    bouyer 	DNPRINTF(MPII_D_MISC, "%s: done with mpii_reset_hard\n", DEVNAME(sc));
   1151   1.1    bouyer 
   1152   1.1    bouyer 	return(0);
   1153   1.1    bouyer }
   1154   1.1    bouyer 
   1155   1.1    bouyer static int
   1156   1.1    bouyer mpii_handshake_send(struct mpii_softc *sc, void *buf, size_t dwords)
   1157   1.1    bouyer {
   1158   1.1    bouyer 	u_int32_t		*query = buf;
   1159   1.1    bouyer 	int			i;
   1160   1.1    bouyer 
   1161   1.1    bouyer 	/* make sure the doorbell is not in use. */
   1162   1.1    bouyer 	if (mpii_read_db(sc) & MPII_DOORBELL_INUSE)
   1163   1.1    bouyer 		return (1);
   1164   1.1    bouyer 
   1165   1.1    bouyer 	/* clear pending doorbell interrupts */
   1166   1.1    bouyer 	if (mpii_read_intr(sc) & MPII_INTR_STATUS_IOC2SYSDB)
   1167   1.1    bouyer 		mpii_write_intr(sc, 0);
   1168   1.1    bouyer 
   1169   1.1    bouyer 	/*
   1170   1.1    bouyer 	 * first write the doorbell with the handshake function and the
   1171   1.1    bouyer 	 * dword count.
   1172   1.1    bouyer 	 */
   1173   1.1    bouyer 	mpii_write_db(sc, MPII_DOORBELL_FUNCTION(MPII_FUNCTION_HANDSHAKE) |
   1174   1.1    bouyer 	    MPII_DOORBELL_DWORDS(dwords));
   1175   1.1    bouyer 
   1176   1.1    bouyer 	/*
   1177   1.1    bouyer 	 * the doorbell used bit will be set because a doorbell function has
   1178   1.1    bouyer 	 * started. wait for the interrupt and then ack it.
   1179   1.1    bouyer 	 */
   1180   1.1    bouyer 	if (mpii_wait_db_int(sc) != 0)
   1181   1.1    bouyer 		return (1);
   1182   1.1    bouyer 	mpii_write_intr(sc, 0);
   1183   1.1    bouyer 
   1184   1.1    bouyer 	/* poll for the acknowledgement. */
   1185   1.1    bouyer 	if (mpii_wait_db_ack(sc) != 0)
   1186   1.1    bouyer 		return (1);
   1187   1.1    bouyer 
   1188   1.1    bouyer 	/* write the query through the doorbell. */
   1189   1.1    bouyer 	for (i = 0; i < dwords; i++) {
   1190   1.1    bouyer 		mpii_write_db(sc, htole32(query[i]));
   1191   1.1    bouyer 		if (mpii_wait_db_ack(sc) != 0)
   1192   1.1    bouyer 			return (1);
   1193   1.1    bouyer 	}
   1194   1.1    bouyer 
   1195   1.1    bouyer 	return (0);
   1196   1.1    bouyer }
   1197   1.1    bouyer 
   1198   1.1    bouyer static int
   1199   1.1    bouyer mpii_handshake_recv_dword(struct mpii_softc *sc, u_int32_t *dword)
   1200   1.1    bouyer {
   1201   1.1    bouyer 	u_int16_t		*words = (u_int16_t *)dword;
   1202   1.1    bouyer 	int			i;
   1203   1.1    bouyer 
   1204   1.1    bouyer 	for (i = 0; i < 2; i++) {
   1205   1.1    bouyer 		if (mpii_wait_db_int(sc) != 0)
   1206   1.1    bouyer 			return (1);
   1207   1.1    bouyer 		words[i] = le16toh(mpii_read_db(sc) & MPII_DOORBELL_DATA_MASK);
   1208   1.1    bouyer 		mpii_write_intr(sc, 0);
   1209   1.1    bouyer 	}
   1210   1.1    bouyer 
   1211   1.1    bouyer 	return (0);
   1212   1.1    bouyer }
   1213   1.1    bouyer 
   1214   1.1    bouyer static int
   1215   1.1    bouyer mpii_handshake_recv(struct mpii_softc *sc, void *buf, size_t dwords)
   1216   1.1    bouyer {
   1217   1.1    bouyer 	struct mpii_msg_reply	*reply = buf;
   1218   1.1    bouyer 	u_int32_t		*dbuf = buf, dummy;
   1219   1.1    bouyer 	int			i;
   1220   1.1    bouyer 
   1221   1.1    bouyer 	/* get the first dword so we can read the length out of the header. */
   1222   1.1    bouyer 	if (mpii_handshake_recv_dword(sc, &dbuf[0]) != 0)
   1223   1.1    bouyer 		return (1);
   1224   1.1    bouyer 
   1225   1.1    bouyer 	DNPRINTF(MPII_D_CMD, "%s: mpii_handshake_recv dwords: %zd reply: %d\n",
   1226   1.1    bouyer 	    DEVNAME(sc), dwords, reply->msg_length);
   1227   1.1    bouyer 
   1228   1.1    bouyer 	/*
   1229   1.1    bouyer 	 * the total length, in dwords, is in the message length field of the
   1230   1.1    bouyer 	 * reply header.
   1231   1.1    bouyer 	 */
   1232   1.1    bouyer 	for (i = 1; i < MIN(dwords, reply->msg_length); i++) {
   1233   1.1    bouyer 		if (mpii_handshake_recv_dword(sc, &dbuf[i]) != 0)
   1234   1.1    bouyer 			return (1);
   1235   1.1    bouyer 	}
   1236   1.1    bouyer 
   1237   1.1    bouyer 	/* if there's extra stuff to come off the ioc, discard it */
   1238   1.1    bouyer 	while (i++ < reply->msg_length) {
   1239   1.1    bouyer 		if (mpii_handshake_recv_dword(sc, &dummy) != 0)
   1240   1.1    bouyer 			return (1);
   1241   1.1    bouyer 		DNPRINTF(MPII_D_CMD, "%s: mpii_handshake_recv dummy read: "
   1242   1.1    bouyer 		    "0x%08x\n", DEVNAME(sc), dummy);
   1243   1.1    bouyer 	}
   1244   1.1    bouyer 
   1245   1.1    bouyer 	/* wait for the doorbell used bit to be reset and clear the intr */
   1246   1.1    bouyer 	if (mpii_wait_db_int(sc) != 0)
   1247   1.1    bouyer 		return (1);
   1248   1.1    bouyer 
   1249   1.1    bouyer 	if (mpii_wait_eq(sc, MPII_DOORBELL, MPII_DOORBELL_INUSE, 0) != 0)
   1250   1.1    bouyer 		return (1);
   1251   1.1    bouyer 
   1252   1.1    bouyer 	mpii_write_intr(sc, 0);
   1253   1.1    bouyer 
   1254   1.1    bouyer 	return (0);
   1255   1.1    bouyer }
   1256   1.1    bouyer 
   1257   1.1    bouyer static void
   1258   1.1    bouyer mpii_empty_done(struct mpii_ccb *ccb)
   1259   1.1    bouyer {
   1260   1.1    bouyer 	/* nothing to do */
   1261   1.1    bouyer }
   1262   1.1    bouyer 
   1263   1.1    bouyer static int
   1264   1.1    bouyer mpii_iocfacts(struct mpii_softc *sc)
   1265   1.1    bouyer {
   1266   1.1    bouyer 	struct mpii_msg_iocfacts_request	ifq;
   1267   1.1    bouyer 	struct mpii_msg_iocfacts_reply		ifp;
   1268   1.1    bouyer 
   1269   1.1    bouyer 	DNPRINTF(MPII_D_MISC, "%s: mpii_iocfacts\n", DEVNAME(sc));
   1270   1.1    bouyer 
   1271   1.1    bouyer 	bzero(&ifq, sizeof(ifq));
   1272   1.1    bouyer 	bzero(&ifp, sizeof(ifp));
   1273   1.1    bouyer 
   1274   1.1    bouyer 	ifq.function = MPII_FUNCTION_IOC_FACTS;
   1275   1.1    bouyer 
   1276   1.1    bouyer 	if (mpii_handshake_send(sc, &ifq, dwordsof(ifq)) != 0) {
   1277   1.1    bouyer 		DNPRINTF(MPII_D_MISC, "%s: mpii_iocfacts send failed\n",
   1278   1.1    bouyer 		    DEVNAME(sc));
   1279   1.1    bouyer 		return (1);
   1280   1.1    bouyer 	}
   1281   1.1    bouyer 
   1282   1.1    bouyer 	if (mpii_handshake_recv(sc, &ifp, dwordsof(ifp)) != 0) {
   1283   1.1    bouyer 		DNPRINTF(MPII_D_MISC, "%s: mpii_iocfacts recv failed\n",
   1284   1.1    bouyer 		    DEVNAME(sc));
   1285   1.1    bouyer 		return (1);
   1286   1.1    bouyer 	}
   1287   1.1    bouyer 
   1288   1.1    bouyer 	DNPRINTF(MPII_D_MISC, "%s:  func: 0x%02x length: %d msgver: %d.%d\n",
   1289   1.1    bouyer 	    DEVNAME(sc), ifp.function, ifp.msg_length,
   1290   1.1    bouyer 	    ifp.msg_version_maj, ifp.msg_version_min);
   1291   1.1    bouyer 	DNPRINTF(MPII_D_MISC, "%s:  msgflags: 0x%02x iocnumber: 0x%02x "
   1292   1.1    bouyer 	    "headerver: %d.%d\n", DEVNAME(sc), ifp.msg_flags,
   1293   1.1    bouyer 	    ifp.ioc_number, ifp.header_version_unit,
   1294   1.1    bouyer 	    ifp.header_version_dev);
   1295   1.1    bouyer 	DNPRINTF(MPII_D_MISC, "%s:  vp_id: 0x%02x vf_id: 0x%02x\n", DEVNAME(sc),
   1296   1.1    bouyer 	    ifp.vp_id, ifp.vf_id);
   1297   1.1    bouyer 	DNPRINTF(MPII_D_MISC, "%s:  iocstatus: 0x%04x ioexceptions: 0x%04x\n",
   1298   1.1    bouyer 	    DEVNAME(sc), le16toh(ifp.ioc_status),
   1299   1.1    bouyer 	    le16toh(ifp.ioc_exceptions));
   1300   1.1    bouyer 	DNPRINTF(MPII_D_MISC, "%s:  iocloginfo: 0x%08x\n", DEVNAME(sc),
   1301   1.1    bouyer 	    le32toh(ifp.ioc_loginfo));
   1302   1.1    bouyer 	DNPRINTF(MPII_D_MISC, "%s:  numberofports: 0x%02x whoinit: 0x%02x "
   1303   1.1    bouyer 	    "maxchaindepth: %d\n", DEVNAME(sc), ifp.number_of_ports,
   1304   1.1    bouyer 	    ifp.whoinit, ifp.max_chain_depth);
   1305   1.1    bouyer 	DNPRINTF(MPII_D_MISC, "%s:  productid: 0x%04x requestcredit: 0x%04x\n",
   1306   1.1    bouyer 	    DEVNAME(sc), le16toh(ifp.product_id), le16toh(ifp.request_credit));
   1307   1.1    bouyer 	DNPRINTF(MPII_D_MISC, "%s:  ioc_capabilities: 0x%08x\n", DEVNAME(sc),
   1308   1.1    bouyer 	    le32toh(ifp.ioc_capabilities));
   1309   1.1    bouyer 	DNPRINTF(MPII_D_MISC, "%s:  fw_version: %d.%d fw_version_unit: 0x%02x "
   1310   1.1    bouyer 	    "fw_version_dev: 0x%02x\n", DEVNAME(sc),
   1311   1.1    bouyer 	    ifp.fw_version_maj, ifp.fw_version_min,
   1312   1.1    bouyer 	    ifp.fw_version_unit, ifp.fw_version_dev);
   1313   1.1    bouyer 	DNPRINTF(MPII_D_MISC, "%s:  iocrequestframesize: 0x%04x\n",
   1314   1.1    bouyer 	    DEVNAME(sc), le16toh(ifp.ioc_request_frame_size));
   1315   1.1    bouyer 	DNPRINTF(MPII_D_MISC, "%s:  maxtargets: 0x%04x "
   1316   1.1    bouyer 	    "maxinitiators: 0x%04x\n", DEVNAME(sc),
   1317   1.1    bouyer 	    le16toh(ifp.max_targets), le16toh(ifp.max_initiators));
   1318   1.1    bouyer 	DNPRINTF(MPII_D_MISC, "%s:  maxenclosures: 0x%04x "
   1319   1.1    bouyer 	    "maxsasexpanders: 0x%04x\n", DEVNAME(sc),
   1320   1.1    bouyer 	    le16toh(ifp.max_enclosures), le16toh(ifp.max_sas_expanders));
   1321   1.1    bouyer 	DNPRINTF(MPII_D_MISC, "%s:  highprioritycredit: 0x%04x "
   1322   1.1    bouyer 	    "protocolflags: 0x%02x\n", DEVNAME(sc),
   1323   1.1    bouyer 	    le16toh(ifp.high_priority_credit), le16toh(ifp.protocol_flags));
   1324   1.1    bouyer 	DNPRINTF(MPII_D_MISC, "%s:  maxvolumes: 0x%02x replyframesize: 0x%02x "
   1325   1.1    bouyer 	    "mrdpqd: 0x%04x\n", DEVNAME(sc), ifp.max_volumes,
   1326   1.1    bouyer 	    ifp.reply_frame_size,
   1327   1.1    bouyer 	    le16toh(ifp.max_reply_descriptor_post_queue_depth));
   1328   1.1    bouyer 	DNPRINTF(MPII_D_MISC, "%s:  maxpersistententries: 0x%04x "
   1329   1.1    bouyer 	    "maxdevhandle: 0x%02x\n", DEVNAME(sc),
   1330   1.1    bouyer 	    le16toh(ifp.max_persistent_entries), le16toh(ifp.max_dev_handle));
   1331   1.1    bouyer 
   1332   1.1    bouyer 	sc->sc_maxchdepth = ifp.max_chain_depth;
   1333   1.1    bouyer 	sc->sc_ioc_number = ifp.ioc_number;
   1334   1.1    bouyer 	sc->sc_vf_id = ifp.vf_id;
   1335   1.1    bouyer 
   1336   1.1    bouyer 	sc->sc_num_ports = ifp.number_of_ports;
   1337   1.1    bouyer 	sc->sc_ioc_event_replay = (le32toh(ifp.ioc_capabilities) &
   1338   1.1    bouyer 	    MPII_IOCFACTS_CAPABILITY_EVENT_REPLAY) ? 1 : 0;
   1339   1.1    bouyer 	sc->sc_max_enclosures = le16toh(ifp.max_enclosures);
   1340   1.1    bouyer 	sc->sc_max_expanders = le16toh(ifp.max_sas_expanders);
   1341   1.1    bouyer 	sc->sc_max_volumes = ifp.max_volumes;
   1342   1.1    bouyer 	sc->sc_max_devices = ifp.max_volumes + le16toh(ifp.max_targets);
   1343   1.1    bouyer 	sc->sc_num_channels = 1;
   1344   1.1    bouyer 
   1345   1.1    bouyer 	if (ISSET(le32toh(ifp.ioc_capabilities),
   1346   1.1    bouyer 	    MPII_IOCFACTS_CAPABILITY_INTEGRATED_RAID))
   1347   1.1    bouyer 		SET(sc->sc_flags, MPII_F_RAID);
   1348   1.1    bouyer 
   1349   1.1    bouyer 	sc->sc_request_depth = MIN(le16toh(ifp.request_credit),
   1350   1.1    bouyer 	    MPII_MAX_REQUEST_CREDIT);
   1351   1.1    bouyer 
   1352   1.1    bouyer 	/* should not be multiple of 16 */
   1353   1.1    bouyer 	sc->sc_num_reply_frames = sc->sc_request_depth + 32;
   1354   1.1    bouyer 	if (!(sc->sc_num_reply_frames % 16))
   1355   1.1    bouyer 		sc->sc_num_reply_frames--;
   1356   1.1    bouyer 
   1357   1.1    bouyer 	/* must be multiple of 16 */
   1358   1.1    bouyer 	sc->sc_reply_free_qdepth = sc->sc_num_reply_frames +
   1359   1.1    bouyer 	    (16 - (sc->sc_num_reply_frames % 16));
   1360   1.1    bouyer 	sc->sc_reply_post_qdepth = ((sc->sc_request_depth +
   1361   1.1    bouyer 	    sc->sc_num_reply_frames + 1 + 15) / 16) * 16;
   1362   1.1    bouyer 
   1363   1.1    bouyer 	if (sc->sc_reply_post_qdepth >
   1364   1.1    bouyer 	    ifp.max_reply_descriptor_post_queue_depth)
   1365   1.1    bouyer 		sc->sc_reply_post_qdepth =
   1366   1.1    bouyer 		    ifp.max_reply_descriptor_post_queue_depth;
   1367   1.1    bouyer 
   1368   1.1    bouyer 	DNPRINTF(MPII_D_MISC, "%s: sc_request_depth: %d "
   1369   1.1    bouyer 	    "sc_num_reply_frames: %d sc_reply_free_qdepth: %d "
   1370   1.1    bouyer 	    "sc_reply_post_qdepth: %d\n", DEVNAME(sc), sc->sc_request_depth,
   1371   1.1    bouyer 	    sc->sc_num_reply_frames, sc->sc_reply_free_qdepth,
   1372   1.1    bouyer 	    sc->sc_reply_post_qdepth);
   1373   1.1    bouyer 
   1374   1.1    bouyer 	/*
   1375   1.1    bouyer 	 * you can fit sg elements on the end of the io cmd if they fit in the
   1376   1.1    bouyer 	 * request frame size.
   1377   1.1    bouyer 	 */
   1378   1.1    bouyer 
   1379   1.1    bouyer 	sc->sc_first_sgl_len = ((le16toh(ifp.ioc_request_frame_size) * 4) -
   1380   1.1    bouyer 	    sizeof(struct mpii_msg_scsi_io)) / sizeof(struct mpii_sge);
   1381   1.1    bouyer 	DNPRINTF(MPII_D_MISC, "%s:   first sgl len: %d\n", DEVNAME(sc),
   1382   1.1    bouyer 	    sc->sc_first_sgl_len);
   1383   1.1    bouyer 
   1384   1.1    bouyer 	sc->sc_chain_len = (le16toh(ifp.ioc_request_frame_size) * 4) /
   1385   1.1    bouyer 	    sizeof(struct mpii_sge);
   1386   1.1    bouyer 	DNPRINTF(MPII_D_MISC, "%s:   chain len: %d\n", DEVNAME(sc),
   1387   1.1    bouyer 	    sc->sc_chain_len);
   1388   1.1    bouyer 
   1389   1.1    bouyer 	/* the sgl tailing the io cmd loses an entry to the chain element. */
   1390   1.1    bouyer 	sc->sc_max_sgl_len = MPII_MAX_SGL - 1;
   1391   1.1    bouyer 	/* the sgl chains lose an entry for each chain element */
   1392   1.1    bouyer 	sc->sc_max_sgl_len -= (MPII_MAX_SGL - sc->sc_first_sgl_len) /
   1393   1.1    bouyer 	    sc->sc_chain_len;
   1394   1.1    bouyer 	DNPRINTF(MPII_D_MISC, "%s:   max sgl len: %d\n", DEVNAME(sc),
   1395   1.1    bouyer 	    sc->sc_max_sgl_len);
   1396   1.1    bouyer 
   1397   1.1    bouyer 	/* XXX we're ignoring the max chain depth */
   1398   1.1    bouyer 
   1399   1.1    bouyer 	return(0);
   1400   1.1    bouyer 
   1401   1.1    bouyer }
   1402   1.1    bouyer 
   1403   1.1    bouyer static int
   1404   1.1    bouyer mpii_iocinit(struct mpii_softc *sc)
   1405   1.1    bouyer {
   1406   1.1    bouyer 	struct mpii_msg_iocinit_request		iiq;
   1407   1.1    bouyer 	struct mpii_msg_iocinit_reply		iip;
   1408   1.1    bouyer 	u_int32_t				hi_addr;
   1409   1.1    bouyer 
   1410   1.1    bouyer 	DNPRINTF(MPII_D_MISC, "%s: mpii_iocinit\n", DEVNAME(sc));
   1411   1.1    bouyer 
   1412   1.1    bouyer 	bzero(&iiq, sizeof(iiq));
   1413   1.1    bouyer 	bzero(&iip, sizeof(iip));
   1414   1.1    bouyer 
   1415   1.1    bouyer 	iiq.function = MPII_FUNCTION_IOC_INIT;
   1416   1.1    bouyer 	iiq.whoinit = MPII_WHOINIT_HOST_DRIVER;
   1417   1.1    bouyer 
   1418   1.1    bouyer 	/* XXX JPG do something about vf_id */
   1419   1.1    bouyer 	iiq.vf_id = 0;
   1420   1.1    bouyer 
   1421   1.1    bouyer 	iiq.msg_version_maj = 0x02;
   1422   1.1    bouyer 	iiq.msg_version_min = 0x00;
   1423   1.1    bouyer 
   1424   1.1    bouyer 	/* XXX JPG ensure compliance with some level and hard-code? */
   1425   1.1    bouyer 	iiq.hdr_version_unit = 0x00;
   1426   1.1    bouyer 	iiq.hdr_version_dev = 0x00;
   1427   1.1    bouyer 
   1428   1.1    bouyer 	iiq.system_request_frame_size = htole16(MPII_REQUEST_SIZE / 4);
   1429   1.1    bouyer 
   1430   1.1    bouyer 	iiq.reply_descriptor_post_queue_depth =
   1431   1.1    bouyer 	    htole16(sc->sc_reply_post_qdepth);
   1432   1.1    bouyer 
   1433   1.1    bouyer 	iiq.reply_free_queue_depth = htole16(sc->sc_reply_free_qdepth);
   1434   1.1    bouyer 
   1435   1.1    bouyer 	hi_addr = (u_int32_t)((u_int64_t)MPII_DMA_DVA(sc->sc_requests) >> 32);
   1436   1.1    bouyer 	iiq.sense_buffer_address_high = htole32(hi_addr);
   1437   1.1    bouyer 
   1438   1.1    bouyer 	hi_addr = (u_int32_t)
   1439   1.1    bouyer 	    ((u_int64_t)MPII_DMA_DVA(sc->sc_replies) >> 32);
   1440   1.1    bouyer 	iiq.system_reply_address_high = htole32(hi_addr);
   1441   1.1    bouyer 
   1442   1.1    bouyer 	iiq.system_request_frame_base_address =
   1443   1.1    bouyer 	    (u_int64_t)MPII_DMA_DVA(sc->sc_requests);
   1444   1.1    bouyer 
   1445   1.1    bouyer 	iiq.reply_descriptor_post_queue_address =
   1446   1.1    bouyer 	    (u_int64_t)MPII_DMA_DVA(sc->sc_reply_postq);
   1447   1.1    bouyer 
   1448   1.1    bouyer 	iiq.reply_free_queue_address =
   1449   1.1    bouyer 	    (u_int64_t)MPII_DMA_DVA(sc->sc_reply_freeq);
   1450   1.1    bouyer 
   1451   1.1    bouyer 	if (mpii_handshake_send(sc, &iiq, dwordsof(iiq)) != 0) {
   1452   1.1    bouyer 		DNPRINTF(MPII_D_MISC, "%s: mpii_iocinit send failed\n",
   1453   1.1    bouyer 		    DEVNAME(sc));
   1454   1.1    bouyer 		return (1);
   1455   1.1    bouyer 	}
   1456   1.1    bouyer 
   1457   1.1    bouyer 	if (mpii_handshake_recv(sc, &iip, dwordsof(iip)) != 0) {
   1458   1.1    bouyer 		DNPRINTF(MPII_D_MISC, "%s: mpii_iocinit recv failed\n",
   1459   1.1    bouyer 		    DEVNAME(sc));
   1460   1.1    bouyer 		return (1);
   1461   1.1    bouyer 	}
   1462   1.1    bouyer 
   1463   1.1    bouyer 	DNPRINTF(MPII_D_MISC, "%s:  function: 0x%02x msg_length: %d "
   1464   1.1    bouyer 	    "whoinit: 0x%02x\n", DEVNAME(sc), iip.function,
   1465   1.1    bouyer 	    iip.msg_length, iip.whoinit);
   1466   1.1    bouyer 	DNPRINTF(MPII_D_MISC, "%s:  msg_flags: 0x%02x\n", DEVNAME(sc),
   1467   1.1    bouyer 	    iip.msg_flags);
   1468   1.1    bouyer 	DNPRINTF(MPII_D_MISC, "%s:  vf_id: 0x%02x vp_id: 0x%02x\n", DEVNAME(sc),
   1469   1.1    bouyer 	    iip.vf_id, iip.vp_id);
   1470   1.1    bouyer 	DNPRINTF(MPII_D_MISC, "%s:  ioc_status: 0x%04x\n", DEVNAME(sc),
   1471   1.1    bouyer 	    le16toh(iip.ioc_status));
   1472   1.1    bouyer 	DNPRINTF(MPII_D_MISC, "%s:  ioc_loginfo: 0x%08x\n", DEVNAME(sc),
   1473   1.1    bouyer 	    le32toh(iip.ioc_loginfo));
   1474   1.1    bouyer 
   1475   1.1    bouyer 	if ((iip.ioc_status != MPII_IOCSTATUS_SUCCESS) || (iip.ioc_loginfo))
   1476   1.1    bouyer 		return (1);
   1477   1.1    bouyer 
   1478   1.1    bouyer 	return (0);
   1479   1.1    bouyer }
   1480   1.1    bouyer 
   1481   1.1    bouyer static void
   1482   1.1    bouyer mpii_push_reply(struct mpii_softc *sc, struct mpii_rcb *rcb)
   1483   1.1    bouyer {
   1484   1.1    bouyer 	u_int32_t		*rfp;
   1485   1.1    bouyer 
   1486   1.1    bouyer 	if (rcb == NULL)
   1487   1.1    bouyer 		return;
   1488   1.1    bouyer 
   1489   1.1    bouyer 	rfp = MPII_DMA_KVA(sc->sc_reply_freeq);
   1490   1.1    bouyer 	rfp[sc->sc_reply_free_host_index] = rcb->rcb_reply_dva;
   1491   1.1    bouyer 
   1492   1.1    bouyer 	sc->sc_reply_free_host_index = (sc->sc_reply_free_host_index + 1) %
   1493   1.1    bouyer 	    sc->sc_reply_free_qdepth;
   1494   1.1    bouyer 
   1495   1.1    bouyer 	mpii_write_reply_free(sc, sc->sc_reply_free_host_index);
   1496   1.1    bouyer }
   1497   1.1    bouyer 
   1498   1.1    bouyer static int
   1499   1.1    bouyer mpii_portfacts(struct mpii_softc *sc)
   1500   1.1    bouyer {
   1501   1.1    bouyer 	struct mpii_msg_portfacts_request	*pfq;
   1502   1.1    bouyer 	struct mpii_msg_portfacts_reply		*pfp;
   1503   1.1    bouyer 	struct mpii_ccb				*ccb;
   1504   1.1    bouyer 	int					rv = 1;
   1505   1.1    bouyer 
   1506   1.1    bouyer 	DNPRINTF(MPII_D_MISC, "%s: mpii_portfacts\n", DEVNAME(sc));
   1507   1.1    bouyer 
   1508   1.1    bouyer 	ccb = mpii_get_ccb(sc, 0);
   1509   1.1    bouyer 	if (ccb == NULL) {
   1510   1.1    bouyer 		DNPRINTF(MPII_D_MISC, "%s: mpii_portfacts mpii_get_ccb fail\n",
   1511   1.1    bouyer 		    DEVNAME(sc));
   1512   1.1    bouyer 		return (rv);
   1513   1.1    bouyer 	}
   1514   1.1    bouyer 
   1515   1.1    bouyer 	ccb->ccb_done = mpii_empty_done;
   1516   1.1    bouyer 	pfq = ccb->ccb_cmd;
   1517   1.1    bouyer 
   1518   1.1    bouyer 	bzero(pfq, sizeof(*pfq));
   1519   1.1    bouyer 
   1520   1.1    bouyer 	pfq->function = MPII_FUNCTION_PORT_FACTS;
   1521   1.1    bouyer 	pfq->chain_offset = 0;
   1522   1.1    bouyer 	pfq->msg_flags = 0;
   1523   1.1    bouyer 	pfq->port_number = 0;
   1524   1.1    bouyer 	pfq->vp_id = 0;
   1525   1.1    bouyer 	pfq->vf_id = 0;
   1526   1.1    bouyer 
   1527   1.1    bouyer 	if (mpii_poll(sc, ccb) != 0) {
   1528   1.1    bouyer 		DNPRINTF(MPII_D_MISC, "%s: mpii_portfacts poll\n",
   1529   1.1    bouyer 		    DEVNAME(sc));
   1530   1.1    bouyer 		goto err;
   1531   1.1    bouyer 	}
   1532   1.1    bouyer 
   1533   1.1    bouyer 	if (ccb->ccb_rcb == NULL) {
   1534   1.1    bouyer 		DNPRINTF(MPII_D_MISC, "%s: empty portfacts reply\n",
   1535   1.1    bouyer 		    DEVNAME(sc));
   1536   1.1    bouyer 		goto err;
   1537   1.1    bouyer 	}
   1538   1.1    bouyer 
   1539   1.1    bouyer 	pfp = ccb->ccb_rcb->rcb_reply;
   1540   1.1    bouyer 	DNPRINTF(MPII_D_MISC, "%s   pfp: %p\n", DEVNAME(sc), pfp);
   1541   1.1    bouyer 
   1542   1.1    bouyer 	DNPRINTF(MPII_D_MISC, "%s:  function: 0x%02x msg_length: %d\n",
   1543   1.1    bouyer 	    DEVNAME(sc), pfp->function, pfp->msg_length);
   1544   1.1    bouyer 	DNPRINTF(MPII_D_MISC, "%s:  msg_flags: 0x%02x port_number: %d\n",
   1545   1.1    bouyer 	    DEVNAME(sc), pfp->msg_flags, pfp->port_number);
   1546   1.1    bouyer 	DNPRINTF(MPII_D_MISC, "%s:  vf_id: 0x%02x vp_id: 0x%02x\n",
   1547   1.1    bouyer 	    DEVNAME(sc), pfp->vf_id, pfp->vp_id);
   1548   1.1    bouyer 	DNPRINTF(MPII_D_MISC, "%s:  ioc_status: 0x%04x\n", DEVNAME(sc),
   1549   1.1    bouyer 	    le16toh(pfp->ioc_status));
   1550   1.1    bouyer 	DNPRINTF(MPII_D_MISC, "%s:  ioc_loginfo: 0x%08x\n", DEVNAME(sc),
   1551   1.1    bouyer 	    le32toh(pfp->ioc_loginfo));
   1552   1.1    bouyer 	DNPRINTF(MPII_D_MISC, "%s:  port_type: 0x%02x\n", DEVNAME(sc),
   1553   1.1    bouyer 	    pfp->port_type);
   1554   1.1    bouyer 	DNPRINTF(MPII_D_MISC, "%s:  max_posted_cmd_buffers: %d\n", DEVNAME(sc),
   1555   1.1    bouyer 	    le16toh(pfp->max_posted_cmd_buffers));
   1556   1.1    bouyer 
   1557   1.1    bouyer 	sc->sc_porttype = pfp->port_type;
   1558   1.1    bouyer 
   1559   1.1    bouyer 	mpii_push_reply(sc, ccb->ccb_rcb);
   1560   1.1    bouyer 	rv = 0;
   1561   1.1    bouyer err:
   1562   1.1    bouyer 	mpii_put_ccb(sc, ccb);
   1563   1.1    bouyer 
   1564   1.1    bouyer 	return (rv);
   1565   1.1    bouyer }
   1566   1.1    bouyer 
   1567   1.1    bouyer static void
   1568   1.1    bouyer mpii_eventack(struct work *wk, void *cookie)
   1569   1.1    bouyer {
   1570   1.1    bouyer 	struct mpii_softc			*sc = cookie;
   1571   1.1    bouyer 	struct mpii_ccb				*ccb;
   1572   1.1    bouyer 	struct mpii_rcb				*rcb = (void *)wk;
   1573   1.1    bouyer 	struct mpii_msg_event_reply		*enp;
   1574   1.1    bouyer 	struct mpii_msg_eventack_request	*eaq;
   1575   1.1    bouyer 
   1576   1.1    bouyer 	ccb = mpii_get_ccb(sc, 0);
   1577   1.1    bouyer 
   1578   1.1    bouyer 	enp = (struct mpii_msg_event_reply *)rcb->rcb_reply;
   1579   1.1    bouyer 
   1580   1.1    bouyer 	ccb->ccb_done = mpii_eventack_done;
   1581   1.1    bouyer 	eaq = ccb->ccb_cmd;
   1582   1.1    bouyer 
   1583   1.1    bouyer 	eaq->function = MPII_FUNCTION_EVENT_ACK;
   1584   1.1    bouyer 
   1585   1.1    bouyer 	eaq->event = enp->event;
   1586   1.1    bouyer 	eaq->event_context = enp->event_context;
   1587   1.1    bouyer 
   1588   1.1    bouyer 	mpii_push_reply(sc, rcb);
   1589   1.1    bouyer 
   1590   1.1    bouyer 	mpii_start(sc, ccb);
   1591   1.1    bouyer 
   1592   1.1    bouyer }
   1593   1.1    bouyer 
   1594   1.1    bouyer static void
   1595   1.1    bouyer mpii_eventack_done(struct mpii_ccb *ccb)
   1596   1.1    bouyer {
   1597   1.1    bouyer 	struct mpii_softc			*sc = ccb->ccb_sc;
   1598   1.1    bouyer 
   1599   1.1    bouyer 	DNPRINTF(MPII_D_EVT, "%s: event ack done\n", DEVNAME(sc));
   1600   1.1    bouyer 
   1601   1.1    bouyer 	mpii_push_reply(sc, ccb->ccb_rcb);
   1602   1.1    bouyer 	mpii_put_ccb(sc, ccb);
   1603   1.1    bouyer }
   1604   1.1    bouyer 
   1605   1.1    bouyer static int
   1606   1.1    bouyer mpii_portenable(struct mpii_softc *sc)
   1607   1.1    bouyer {
   1608   1.1    bouyer 	struct mpii_msg_portenable_request	*peq;
   1609   1.1    bouyer 	struct mpii_ccb				*ccb;
   1610   1.1    bouyer 
   1611   1.1    bouyer 	DNPRINTF(MPII_D_MISC, "%s: mpii_portenable\n", DEVNAME(sc));
   1612   1.1    bouyer 
   1613   1.1    bouyer 	ccb = mpii_get_ccb(sc, 0);
   1614   1.1    bouyer 	if (ccb == NULL) {
   1615   1.1    bouyer 		DNPRINTF(MPII_D_MISC, "%s: mpii_portenable ccb_get\n",
   1616   1.1    bouyer 		    DEVNAME(sc));
   1617   1.1    bouyer 		return (1);
   1618   1.1    bouyer 	}
   1619   1.1    bouyer 
   1620   1.1    bouyer 	ccb->ccb_done = mpii_empty_done;
   1621   1.1    bouyer 	peq = ccb->ccb_cmd;
   1622   1.1    bouyer 
   1623   1.1    bouyer 	peq->function = MPII_FUNCTION_PORT_ENABLE;
   1624   1.1    bouyer 	peq->vf_id = sc->sc_vf_id;
   1625   1.1    bouyer 
   1626   1.1    bouyer 	if (mpii_poll(sc, ccb) != 0) {
   1627   1.1    bouyer 		DNPRINTF(MPII_D_MISC, "%s: mpii_portenable poll\n",
   1628   1.1    bouyer 		    DEVNAME(sc));
   1629   1.1    bouyer 		return (1);
   1630   1.1    bouyer 	}
   1631   1.1    bouyer 
   1632   1.1    bouyer 	if (ccb->ccb_rcb == NULL) {
   1633   1.1    bouyer 		DNPRINTF(MPII_D_MISC, "%s: empty portenable reply\n",
   1634   1.1    bouyer 		    DEVNAME(sc));
   1635   1.1    bouyer 		return (1);
   1636   1.1    bouyer 	}
   1637   1.1    bouyer 
   1638   1.1    bouyer 	mpii_push_reply(sc, ccb->ccb_rcb);
   1639   1.1    bouyer 	mpii_put_ccb(sc, ccb);
   1640   1.1    bouyer 
   1641   1.1    bouyer 	return (0);
   1642   1.1    bouyer }
   1643   1.1    bouyer 
   1644   1.1    bouyer static int
   1645   1.1    bouyer mpii_cfg_coalescing(struct mpii_softc *sc)
   1646   1.1    bouyer {
   1647   1.1    bouyer 	struct mpii_cfg_hdr		hdr;
   1648   1.1    bouyer 	struct mpii_cfg_ioc_pg1		pg;
   1649   1.1    bouyer 
   1650   1.1    bouyer 	if (mpii_cfg_header(sc, MPII_CONFIG_REQ_PAGE_TYPE_IOC, 1, 0,
   1651   1.1    bouyer 	    &hdr) != 0) {
   1652   1.1    bouyer 		DNPRINTF(MPII_D_MISC, "%s: unable to fetch IOC page 1 "
   1653   1.1    bouyer 		    "header\n", DEVNAME(sc));
   1654   1.1    bouyer 		return (1);
   1655   1.1    bouyer 	}
   1656   1.1    bouyer 
   1657   1.1    bouyer 	if (mpii_cfg_page(sc, 0, &hdr, 1, &pg, sizeof(pg)) != 0) {
   1658   1.1    bouyer 		DNPRINTF(MPII_D_MISC, "%s: unable to fetch IOC page 1\n"
   1659   1.1    bouyer 		    "page 1\n", DEVNAME(sc));
   1660   1.1    bouyer 		return (1);
   1661   1.1    bouyer 	}
   1662   1.1    bouyer 
   1663   1.1    bouyer 	DNPRINTF(MPII_D_MISC, "%s: IOC page 1\n", DEVNAME(sc));
   1664   1.1    bouyer 	DNPRINTF(MPII_D_MISC, "%s:  flags: 0x08%x\n", DEVNAME(sc),
   1665   1.1    bouyer 	    le32toh(pg.flags));
   1666   1.1    bouyer 	DNPRINTF(MPII_D_MISC, "%s:  coalescing_timeout: %d\n", DEVNAME(sc),
   1667   1.1    bouyer 	    le32toh(pg.coalescing_timeout));
   1668   1.1    bouyer 	DNPRINTF(MPII_D_MISC, "%s:  coalescing_depth: %d pci_slot_num: %d\n",
   1669   1.1    bouyer 	    DEVNAME(sc), pg.coalescing_timeout, pg.pci_slot_num);
   1670   1.1    bouyer 
   1671   1.1    bouyer 	if (!ISSET(le32toh(pg.flags), MPII_CFG_IOC_1_REPLY_COALESCING))
   1672   1.1    bouyer 		return (0);
   1673   1.1    bouyer 
   1674   1.1    bouyer 	CLR(pg.flags, htole32(MPII_CFG_IOC_1_REPLY_COALESCING));
   1675   1.1    bouyer 	if (mpii_cfg_page(sc, 0, &hdr, 0, &pg, sizeof(pg)) != 0) {
   1676   1.1    bouyer 		DNPRINTF(MPII_D_MISC, "%s: unable to clear coalescing\n",
   1677   1.1    bouyer 		    DEVNAME(sc));
   1678   1.1    bouyer 		return (1);
   1679   1.1    bouyer 	}
   1680   1.1    bouyer 
   1681   1.1    bouyer 	return (0);
   1682   1.1    bouyer }
   1683   1.1    bouyer 
   1684   1.1    bouyer #define MPII_EVENT_MASKALL(enq)		do {			\
   1685   1.1    bouyer 		enq->event_masks[0] = 0xffffffff;		\
   1686   1.1    bouyer 		enq->event_masks[1] = 0xffffffff;		\
   1687   1.1    bouyer 		enq->event_masks[2] = 0xffffffff;		\
   1688   1.1    bouyer 		enq->event_masks[3] = 0xffffffff;		\
   1689   1.1    bouyer 	} while (0)
   1690   1.1    bouyer 
   1691   1.1    bouyer #define MPII_EVENT_UNMASK(enq, evt)	do {			\
   1692   1.1    bouyer 		enq->event_masks[evt / 32] &=			\
   1693   1.1    bouyer 		    htole32(~(1 << (evt % 32)));		\
   1694   1.1    bouyer 	} while (0)
   1695   1.1    bouyer 
   1696   1.1    bouyer static int
   1697   1.1    bouyer mpii_eventnotify(struct mpii_softc *sc)
   1698   1.1    bouyer {
   1699   1.1    bouyer 	struct mpii_msg_event_request		*enq;
   1700   1.1    bouyer 	struct mpii_ccb				*ccb;
   1701   1.1    bouyer 
   1702   1.1    bouyer 	ccb = mpii_get_ccb(sc, 0);
   1703   1.1    bouyer 	if (ccb == NULL) {
   1704   1.1    bouyer 		DNPRINTF(MPII_D_MISC, "%s: mpii_eventnotify ccb_get\n",
   1705   1.1    bouyer 		    DEVNAME(sc));
   1706   1.1    bouyer 		return (1);
   1707   1.1    bouyer 	}
   1708   1.1    bouyer 
   1709   1.1    bouyer 	ccb->ccb_done = mpii_eventnotify_done;
   1710   1.1    bouyer 	enq = ccb->ccb_cmd;
   1711   1.1    bouyer 
   1712   1.1    bouyer 	enq->function = MPII_FUNCTION_EVENT_NOTIFICATION;
   1713   1.1    bouyer 
   1714   1.1    bouyer 	/*
   1715   1.1    bouyer 	 * Enable reporting of the following events:
   1716   1.1    bouyer 	 *
   1717   1.1    bouyer 	 * MPII_EVENT_SAS_DISCOVERY
   1718   1.1    bouyer 	 * MPII_EVENT_SAS_TOPOLOGY_CHANGE_LIST
   1719   1.1    bouyer 	 * MPII_EVENT_SAS_DEVICE_STATUS_CHANGE
   1720   1.1    bouyer 	 * MPII_EVENT_SAS_ENCL_DEVICE_STATUS_CHANGE
   1721   1.1    bouyer 	 * MPII_EVENT_IR_CONFIGURATION_CHANGE_LIST
   1722   1.1    bouyer 	 * MPII_EVENT_IR_VOLUME
   1723   1.1    bouyer 	 * MPII_EVENT_IR_PHYSICAL_DISK
   1724   1.1    bouyer 	 * MPII_EVENT_IR_OPERATION_STATUS
   1725   1.1    bouyer 	 */
   1726   1.1    bouyer 
   1727   1.1    bouyer 	MPII_EVENT_MASKALL(enq);
   1728   1.1    bouyer 	MPII_EVENT_UNMASK(enq, MPII_EVENT_SAS_DISCOVERY);
   1729   1.1    bouyer 	MPII_EVENT_UNMASK(enq, MPII_EVENT_SAS_TOPOLOGY_CHANGE_LIST);
   1730   1.1    bouyer 	MPII_EVENT_UNMASK(enq, MPII_EVENT_SAS_DEVICE_STATUS_CHANGE);
   1731   1.1    bouyer 	MPII_EVENT_UNMASK(enq, MPII_EVENT_SAS_ENCL_DEVICE_STATUS_CHANGE);
   1732   1.1    bouyer 	MPII_EVENT_UNMASK(enq, MPII_EVENT_IR_CONFIGURATION_CHANGE_LIST);
   1733   1.1    bouyer 	MPII_EVENT_UNMASK(enq, MPII_EVENT_IR_VOLUME);
   1734   1.1    bouyer 	MPII_EVENT_UNMASK(enq, MPII_EVENT_IR_PHYSICAL_DISK);
   1735   1.1    bouyer 	MPII_EVENT_UNMASK(enq, MPII_EVENT_IR_OPERATION_STATUS);
   1736   1.1    bouyer 
   1737   1.1    bouyer 	mpii_start(sc, ccb);
   1738   1.1    bouyer 
   1739   1.1    bouyer 	return (0);
   1740   1.1    bouyer }
   1741   1.1    bouyer 
   1742   1.1    bouyer static void
   1743   1.1    bouyer mpii_eventnotify_done(struct mpii_ccb *ccb)
   1744   1.1    bouyer {
   1745   1.1    bouyer 	struct mpii_softc			*sc = ccb->ccb_sc;
   1746   1.1    bouyer 	struct mpii_rcb				*rcb = ccb->ccb_rcb;
   1747   1.1    bouyer 
   1748   1.1    bouyer 	DNPRINTF(MPII_D_EVT, "%s: mpii_eventnotify_done\n", DEVNAME(sc));
   1749   1.1    bouyer 
   1750   1.1    bouyer 	mpii_put_ccb(sc, ccb);
   1751   1.1    bouyer 	mpii_event_process(sc, rcb);
   1752   1.1    bouyer }
   1753   1.1    bouyer 
   1754   1.1    bouyer static void
   1755   1.1    bouyer mpii_event_raid(struct mpii_softc *sc, struct mpii_msg_event_reply *enp)
   1756   1.1    bouyer {
   1757   1.1    bouyer 	struct mpii_evt_ir_cfg_change_list	*ccl;
   1758   1.1    bouyer 	struct mpii_evt_ir_cfg_element		*ce;
   1759   1.1    bouyer 	struct mpii_device			*dev;
   1760   1.1    bouyer 	u_int16_t				type;
   1761   1.1    bouyer 	int					i;
   1762   1.1    bouyer 
   1763   1.1    bouyer 	ccl = (struct mpii_evt_ir_cfg_change_list *)(enp + 1);
   1764   1.1    bouyer 
   1765   1.1    bouyer 	if (ccl->num_elements == 0)
   1766   1.1    bouyer 		return;
   1767   1.1    bouyer 	if (ISSET(le32toh(ccl->flags), MPII_EVT_IR_CFG_CHANGE_LIST_FOREIGN))
   1768   1.1    bouyer 		/* bail on foreign configurations */
   1769   1.1    bouyer 		return;
   1770   1.1    bouyer 
   1771   1.1    bouyer 	ce = (struct mpii_evt_ir_cfg_element *)(ccl + 1);
   1772   1.1    bouyer 
   1773   1.1    bouyer 	for (i = 0; i < ccl->num_elements; i++, ce++) {
   1774   1.1    bouyer 		type = (le16toh(ce->element_flags) &
   1775   1.1    bouyer 		    MPII_EVT_IR_CFG_ELEMENT_TYPE_MASK);
   1776   1.1    bouyer 
   1777   1.1    bouyer 		switch (type) {
   1778   1.1    bouyer 		case MPII_EVT_IR_CFG_ELEMENT_TYPE_VOLUME:
   1779   1.1    bouyer 			switch (ce->reason_code) {
   1780   1.1    bouyer 			case MPII_EVT_IR_CFG_ELEMENT_RC_ADDED:
   1781   1.1    bouyer 			case MPII_EVT_IR_CFG_ELEMENT_RC_VOLUME_CREATED:
   1782   1.1    bouyer 				if (mpii_find_dev(sc,
   1783   1.1    bouyer 				    le16toh(ce->vol_dev_handle))) {
   1784   1.9  christos 					aprint_error_dev(sc->sc_dev,
   1785   1.9  christos 					    "device %#x is already "
   1786   1.9  christos 					    "configured\n",
   1787   1.1    bouyer 					    le16toh(ce->vol_dev_handle));
   1788   1.1    bouyer 					break;
   1789   1.1    bouyer 				}
   1790   1.1    bouyer 				dev = malloc(sizeof(*dev), M_DEVBUF,
   1791   1.1    bouyer 				    M_NOWAIT | M_ZERO);
   1792   1.1    bouyer 				if (!dev) {
   1793   1.9  christos 					aprint_error_dev(sc->sc_dev,
   1794   1.9  christos 					    "can't allocate device structure\n");
   1795   1.1    bouyer 					break;
   1796   1.1    bouyer 				}
   1797   1.1    bouyer 				SET(dev->flags, MPII_DF_VOLUME);
   1798   1.1    bouyer 				dev->slot = sc->sc_vd_id_low;
   1799   1.1    bouyer 				dev->dev_handle = le16toh(ce->vol_dev_handle);
   1800   1.1    bouyer 				if (mpii_insert_dev(sc, dev)) {
   1801   1.9  christos 					aprint_error_dev(sc->sc_dev,
   1802   1.9  christos 					    "can't insert device structure\n");
   1803   1.9  christos 					free(dev, M_DEVBUF);
   1804   1.9  christos 					break;
   1805   1.9  christos 				}
   1806   1.9  christos 				if (mpii_cache_enable(sc, dev)) {
   1807   1.9  christos 					aprint_error_dev(sc->sc_dev,
   1808   1.9  christos 					    "can't enable device cache\n");
   1809   1.1    bouyer 					free(dev, M_DEVBUF);
   1810   1.1    bouyer 					break;
   1811   1.1    bouyer 				}
   1812   1.1    bouyer 				sc->sc_vd_count++;
   1813   1.1    bouyer 				break;
   1814   1.1    bouyer 			case MPII_EVT_IR_CFG_ELEMENT_RC_REMOVED:
   1815   1.1    bouyer 			case MPII_EVT_IR_CFG_ELEMENT_RC_VOLUME_DELETED:
   1816   1.1    bouyer 				if (!(dev = mpii_find_dev(sc,
   1817   1.1    bouyer 				    le16toh(ce->vol_dev_handle))))
   1818   1.1    bouyer 					break;
   1819   1.1    bouyer 				mpii_remove_dev(sc, dev);
   1820   1.1    bouyer 				sc->sc_vd_count--;
   1821   1.1    bouyer 				break;
   1822   1.1    bouyer 			}
   1823   1.1    bouyer 			break;
   1824   1.1    bouyer 		case MPII_EVT_IR_CFG_ELEMENT_TYPE_VOLUME_DISK:
   1825   1.1    bouyer 			if (ce->reason_code ==
   1826   1.1    bouyer 			    MPII_EVT_IR_CFG_ELEMENT_RC_PD_CREATED ||
   1827   1.1    bouyer 			    ce->reason_code ==
   1828   1.1    bouyer 			    MPII_EVT_IR_CFG_ELEMENT_RC_HIDE) {
   1829   1.1    bouyer 				/* there should be an underlying sas drive */
   1830   1.1    bouyer 				if (!(dev = mpii_find_dev(sc,
   1831   1.1    bouyer 				    le16toh(ce->phys_disk_dev_handle))))
   1832   1.1    bouyer 					break;
   1833   1.1    bouyer 				/* promoted from a hot spare? */
   1834   1.1    bouyer 				CLR(dev->flags, MPII_DF_HOT_SPARE);
   1835   1.1    bouyer 				SET(dev->flags, MPII_DF_VOLUME_DISK |
   1836   1.1    bouyer 				    MPII_DF_HIDDEN);
   1837   1.1    bouyer 			}
   1838   1.1    bouyer 			break;
   1839   1.1    bouyer 		case MPII_EVT_IR_CFG_ELEMENT_TYPE_HOT_SPARE:
   1840   1.1    bouyer 			if (ce->reason_code ==
   1841   1.1    bouyer 			    MPII_EVT_IR_CFG_ELEMENT_RC_HIDE) {
   1842   1.1    bouyer 				/* there should be an underlying sas drive */
   1843   1.1    bouyer 				if (!(dev = mpii_find_dev(sc,
   1844   1.1    bouyer 				    le16toh(ce->phys_disk_dev_handle))))
   1845   1.1    bouyer 					break;
   1846   1.1    bouyer 				SET(dev->flags, MPII_DF_HOT_SPARE |
   1847   1.1    bouyer 				    MPII_DF_HIDDEN);
   1848   1.1    bouyer 			}
   1849   1.1    bouyer 			break;
   1850   1.1    bouyer 		}
   1851   1.1    bouyer 	}
   1852   1.1    bouyer }
   1853   1.1    bouyer 
   1854   1.1    bouyer static void
   1855   1.1    bouyer mpii_event_sas(struct mpii_softc *sc, struct mpii_msg_event_reply *enp)
   1856   1.1    bouyer {
   1857   1.1    bouyer 	struct mpii_evt_sas_tcl		*tcl;
   1858   1.1    bouyer 	struct mpii_evt_phy_entry	*pe;
   1859   1.1    bouyer 	struct mpii_device		*dev;
   1860   1.1    bouyer 	int				i;
   1861   1.1    bouyer 
   1862   1.1    bouyer 	tcl = (struct mpii_evt_sas_tcl *)(enp + 1);
   1863   1.1    bouyer 
   1864   1.1    bouyer 	if (tcl->num_entries == 0)
   1865   1.1    bouyer 		return;
   1866   1.1    bouyer 
   1867   1.1    bouyer 	pe = (struct mpii_evt_phy_entry *)(tcl + 1);
   1868   1.1    bouyer 
   1869   1.1    bouyer 	for (i = 0; i < tcl->num_entries; i++, pe++) {
   1870   1.1    bouyer 		switch (pe->phy_status & MPII_EVENT_SAS_TOPO_PS_RC_MASK) {
   1871   1.1    bouyer 		case MPII_EVENT_SAS_TOPO_PS_RC_ADDED:
   1872   1.1    bouyer 			if (mpii_find_dev(sc, le16toh(pe->dev_handle))) {
   1873   1.9  christos 				aprint_error_dev(sc->sc_dev,
   1874   1.9  christos 				    "device %#x is already configured\n",
   1875   1.1    bouyer 				    le16toh(pe->dev_handle));
   1876   1.1    bouyer 				break;
   1877   1.1    bouyer 			}
   1878   1.1    bouyer 			dev = malloc(sizeof(*dev), M_DEVBUF, M_NOWAIT | M_ZERO);
   1879   1.1    bouyer 			if (!dev) {
   1880   1.9  christos 				aprint_error_dev(sc->sc_dev, "can't allocate "
   1881   1.9  christos 				    "device structure\n");
   1882   1.1    bouyer 				break;
   1883   1.1    bouyer 			}
   1884   1.1    bouyer 			dev->slot = sc->sc_pd_id_start + tcl->start_phy_num + i;
   1885   1.1    bouyer 			dev->dev_handle = le16toh(pe->dev_handle);
   1886   1.1    bouyer 			dev->phy_num = tcl->start_phy_num + i;
   1887   1.1    bouyer 			if (tcl->enclosure_handle)
   1888   1.1    bouyer 				dev->physical_port = tcl->physical_port;
   1889   1.1    bouyer 			dev->enclosure = le16toh(tcl->enclosure_handle);
   1890   1.1    bouyer 			dev->expander = le16toh(tcl->expander_handle);
   1891   1.1    bouyer 			if (mpii_insert_dev(sc, dev)) {
   1892   1.9  christos 				aprint_error_dev(sc->sc_dev, "can't insert "
   1893   1.9  christos 				    "device structure\n");
   1894   1.1    bouyer 				free(dev, M_DEVBUF);
   1895   1.1    bouyer 				break;
   1896   1.1    bouyer 			}
   1897   1.1    bouyer 			break;
   1898   1.1    bouyer 		case MPII_EVENT_SAS_TOPO_PS_RC_MISSING:
   1899   1.1    bouyer 			if (!(dev = mpii_find_dev(sc,
   1900   1.1    bouyer 			    le16toh(pe->dev_handle))))
   1901   1.1    bouyer 				break;
   1902   1.1    bouyer 			mpii_remove_dev(sc, dev);
   1903   1.1    bouyer #if 0
   1904   1.1    bouyer 			if (sc->sc_scsibus) {
   1905   1.1    bouyer 				SET(dev->flags, MPII_DF_DETACH);
   1906   1.1    bouyer 				scsi_activate(sc->sc_scsibus, dev->slot, -1,
   1907   1.1    bouyer 				    DVACT_DEACTIVATE);
   1908   1.1    bouyer 				if (scsi_task(mpii_event_defer, sc,
   1909   1.1    bouyer 				    dev, 0) != 0)
   1910   1.9  christos 					aprint_error_dev(sc->sc_dev,
   1911   1.9  christos 					    "unable to run device "
   1912   1.9  christos 					    "detachment routine\n");
   1913   1.1    bouyer 			}
   1914   1.1    bouyer #else
   1915   1.1    bouyer 			mpii_event_defer(sc, dev);
   1916   1.1    bouyer #endif /* XXX */
   1917   1.1    bouyer 			break;
   1918   1.1    bouyer 		}
   1919   1.1    bouyer 	}
   1920   1.1    bouyer }
   1921   1.1    bouyer 
   1922   1.1    bouyer static void
   1923   1.1    bouyer mpii_event_process(struct mpii_softc *sc, struct mpii_rcb *rcb)
   1924   1.1    bouyer {
   1925   1.1    bouyer 	struct mpii_msg_event_reply		*enp;
   1926   1.1    bouyer 
   1927   1.1    bouyer 	enp = (struct mpii_msg_event_reply *)rcb->rcb_reply;
   1928   1.1    bouyer 
   1929   1.1    bouyer 	DNPRINTF(MPII_D_EVT, "%s: mpii_event_process: %#x\n", DEVNAME(sc),
   1930   1.1    bouyer 	    le32toh(enp->event));
   1931   1.1    bouyer 
   1932   1.1    bouyer 	switch (le32toh(enp->event)) {
   1933   1.1    bouyer 	case MPII_EVENT_EVENT_CHANGE:
   1934   1.1    bouyer 		/* should be properly ignored */
   1935   1.1    bouyer 		break;
   1936   1.1    bouyer 	case MPII_EVENT_SAS_DISCOVERY: {
   1937   1.1    bouyer 		struct mpii_evt_sas_discovery	*esd =
   1938   1.1    bouyer 		    (struct mpii_evt_sas_discovery *)(enp + 1);
   1939   1.1    bouyer 
   1940   1.1    bouyer 		if (esd->reason_code ==
   1941   1.1    bouyer 		    MPII_EVENT_SAS_DISC_REASON_CODE_COMPLETED &&
   1942   1.1    bouyer 		    esd->discovery_status != 0)
   1943   1.1    bouyer 			printf("%s: sas discovery completed with status %#x\n",
   1944   1.1    bouyer 			    DEVNAME(sc), esd->discovery_status);
   1945   1.1    bouyer 		}
   1946   1.1    bouyer 		break;
   1947   1.1    bouyer 	case MPII_EVENT_SAS_TOPOLOGY_CHANGE_LIST:
   1948   1.1    bouyer 		mpii_event_sas(sc, enp);
   1949   1.1    bouyer 		break;
   1950   1.1    bouyer 	case MPII_EVENT_SAS_DEVICE_STATUS_CHANGE:
   1951   1.1    bouyer 		break;
   1952   1.1    bouyer 	case MPII_EVENT_SAS_ENCL_DEVICE_STATUS_CHANGE:
   1953   1.1    bouyer 		break;
   1954   1.1    bouyer 	case MPII_EVENT_IR_VOLUME: {
   1955   1.1    bouyer 		struct mpii_evt_ir_volume	*evd =
   1956   1.1    bouyer 		    (struct mpii_evt_ir_volume *)(enp + 1);
   1957   1.1    bouyer 		struct mpii_device		*dev;
   1958   1.1    bouyer #if NBIO > 0
   1959   1.1    bouyer 		const char *vol_states[] = {
   1960   1.1    bouyer 			BIOC_SVINVALID_S,
   1961   1.1    bouyer 			BIOC_SVOFFLINE_S,
   1962   1.1    bouyer 			BIOC_SVBUILDING_S,
   1963   1.1    bouyer 			BIOC_SVONLINE_S,
   1964   1.1    bouyer 			BIOC_SVDEGRADED_S,
   1965   1.1    bouyer 			BIOC_SVONLINE_S,
   1966   1.1    bouyer 		};
   1967   1.1    bouyer #endif
   1968   1.1    bouyer 
   1969   1.1    bouyer 		if (cold)
   1970   1.1    bouyer 			break;
   1971   1.1    bouyer 		if (!(dev = mpii_find_dev(sc, le16toh(evd->vol_dev_handle))))
   1972   1.1    bouyer 			break;
   1973   1.1    bouyer #if NBIO > 0
   1974   1.1    bouyer 		if (evd->reason_code == MPII_EVENT_IR_VOL_RC_STATE_CHANGED)
   1975   1.1    bouyer 			printf("%s: volume %d state changed from %s to %s\n",
   1976   1.1    bouyer 			    DEVNAME(sc), dev->slot - sc->sc_vd_id_low,
   1977   1.1    bouyer 			    vol_states[evd->prev_value],
   1978   1.1    bouyer 			    vol_states[evd->new_value]);
   1979   1.1    bouyer #endif
   1980   1.1    bouyer 		if (evd->reason_code == MPII_EVENT_IR_VOL_RC_STATUS_CHANGED &&
   1981   1.1    bouyer 		    ISSET(evd->new_value, MPII_CFG_RAID_VOL_0_STATUS_RESYNC) &&
   1982   1.1    bouyer 		    !ISSET(evd->prev_value, MPII_CFG_RAID_VOL_0_STATUS_RESYNC))
   1983   1.1    bouyer 			printf("%s: started resync on a volume %d\n",
   1984   1.1    bouyer 			    DEVNAME(sc), dev->slot - sc->sc_vd_id_low);
   1985   1.1    bouyer 		}
   1986   1.1    bouyer 		break;
   1987   1.1    bouyer 	case MPII_EVENT_IR_PHYSICAL_DISK:
   1988   1.1    bouyer 		break;
   1989   1.1    bouyer 	case MPII_EVENT_IR_CONFIGURATION_CHANGE_LIST:
   1990   1.1    bouyer 		mpii_event_raid(sc, enp);
   1991   1.1    bouyer 		break;
   1992   1.1    bouyer 	case MPII_EVENT_IR_OPERATION_STATUS: {
   1993   1.1    bouyer 		struct mpii_evt_ir_status	*evs =
   1994   1.1    bouyer 		    (struct mpii_evt_ir_status *)(enp + 1);
   1995   1.1    bouyer 		struct mpii_device		*dev;
   1996   1.1    bouyer 
   1997   1.1    bouyer 		if (!(dev = mpii_find_dev(sc, le16toh(evs->vol_dev_handle))))
   1998   1.1    bouyer 			break;
   1999   1.1    bouyer 		if (evs->operation == MPII_EVENT_IR_RAIDOP_RESYNC)
   2000   1.1    bouyer 			dev->percent = evs->percent;
   2001   1.1    bouyer 		break;
   2002   1.1    bouyer 		}
   2003   1.1    bouyer 	default:
   2004   1.1    bouyer 		DNPRINTF(MPII_D_EVT, "%s:  unhandled event 0x%02x\n",
   2005   1.1    bouyer 		    DEVNAME(sc), le32toh(enp->event));
   2006   1.1    bouyer 	}
   2007   1.1    bouyer 
   2008   1.1    bouyer 	if (enp->ack_required)
   2009   1.1    bouyer 		workqueue_enqueue(sc->sc_ssb_evt_ackwk, &rcb->u.rcb_wk, NULL);
   2010   1.1    bouyer 	else
   2011   1.1    bouyer 		mpii_push_reply(sc, rcb);
   2012   1.1    bouyer }
   2013   1.1    bouyer 
   2014   1.1    bouyer static void
   2015   1.1    bouyer mpii_event_defer(void *xsc, void *arg)
   2016   1.1    bouyer {
   2017   1.1    bouyer 	struct mpii_softc	*sc = xsc;
   2018   1.1    bouyer 	struct mpii_device	*dev = arg;
   2019   1.1    bouyer 
   2020   1.1    bouyer 	if (ISSET(dev->flags, MPII_DF_DETACH)) {
   2021   1.1    bouyer 		mpii_sas_remove_device(sc, dev->dev_handle);
   2022   1.1    bouyer #if 0
   2023   1.1    bouyer 		if (!ISSET(dev->flags, MPII_DF_HIDDEN)) {
   2024   1.1    bouyer 			scsi_detach_target(sc->sc_scsibus, dev->slot,
   2025   1.1    bouyer 			    DETACH_FORCE);
   2026   1.1    bouyer 		}
   2027   1.1    bouyer #endif /* XXX */
   2028   1.1    bouyer 		free(dev, M_DEVBUF);
   2029   1.1    bouyer 
   2030   1.1    bouyer 	} else if (ISSET(dev->flags, MPII_DF_ATTACH)) {
   2031   1.1    bouyer 		CLR(dev->flags, MPII_DF_ATTACH);
   2032   1.1    bouyer #if 0
   2033   1.1    bouyer 		if (!ISSET(dev->flags, MPII_DF_HIDDEN))
   2034   1.1    bouyer 			scsi_probe_target(sc->sc_scsibus, dev->slot);
   2035   1.1    bouyer #endif /* XXX */
   2036   1.1    bouyer 	}
   2037   1.1    bouyer }
   2038   1.1    bouyer 
   2039   1.1    bouyer static void
   2040   1.1    bouyer mpii_sas_remove_device(struct mpii_softc *sc, u_int16_t handle)
   2041   1.1    bouyer {
   2042   1.1    bouyer  	struct mpii_msg_scsi_task_request	*stq;
   2043   1.1    bouyer 	struct mpii_msg_sas_oper_request	*soq;
   2044   1.1    bouyer 	struct mpii_ccb				*ccb;
   2045   1.1    bouyer 
   2046   1.1    bouyer 	ccb = mpii_get_ccb(sc, 0);
   2047   1.1    bouyer 	if (ccb == NULL)
   2048   1.1    bouyer 		return;
   2049   1.1    bouyer 
   2050   1.1    bouyer 	stq = ccb->ccb_cmd;
   2051   1.1    bouyer 	stq->function = MPII_FUNCTION_SCSI_TASK_MGMT;
   2052   1.1    bouyer 	stq->task_type = MPII_SCSI_TASK_TARGET_RESET;
   2053   1.1    bouyer 	stq->dev_handle = htole16(handle);
   2054   1.1    bouyer 
   2055   1.1    bouyer 	ccb->ccb_done = mpii_empty_done;
   2056   1.1    bouyer 	mpii_wait(sc, ccb);
   2057   1.1    bouyer 
   2058   1.1    bouyer 	if (ccb->ccb_rcb != NULL)
   2059   1.1    bouyer 		mpii_push_reply(sc, ccb->ccb_rcb);
   2060   1.1    bouyer 
   2061   1.1    bouyer 	/* reuse a ccb */
   2062   1.1    bouyer 	ccb->ccb_state = MPII_CCB_READY;
   2063   1.1    bouyer 	ccb->ccb_rcb = NULL;
   2064   1.1    bouyer 
   2065   1.1    bouyer 	soq = ccb->ccb_cmd;
   2066   1.1    bouyer 	bzero(soq, sizeof(*soq));
   2067   1.1    bouyer 	soq->function = MPII_FUNCTION_SAS_IO_UNIT_CONTROL;
   2068   1.1    bouyer 	soq->operation = MPII_SAS_OP_REMOVE_DEVICE;
   2069   1.1    bouyer 	soq->dev_handle = htole16(handle);
   2070   1.1    bouyer 
   2071   1.1    bouyer 	ccb->ccb_done = mpii_empty_done;
   2072   1.1    bouyer 	mpii_wait(sc, ccb);
   2073   1.1    bouyer 	if (ccb->ccb_rcb != NULL)
   2074   1.1    bouyer 		mpii_push_reply(sc, ccb->ccb_rcb);
   2075   1.1    bouyer }
   2076   1.1    bouyer 
   2077   1.1    bouyer static int
   2078   1.1    bouyer mpii_get_ioc_pg8(struct mpii_softc *sc)
   2079   1.1    bouyer {
   2080   1.1    bouyer 	struct mpii_cfg_hdr	hdr;
   2081   1.1    bouyer 	struct mpii_cfg_ioc_pg8	*page;
   2082   1.1    bouyer 	size_t			pagelen;
   2083   1.1    bouyer 	u_int16_t		flags;
   2084   1.1    bouyer 	int			pad = 0, rv = 0;
   2085   1.1    bouyer 
   2086   1.1    bouyer 	DNPRINTF(MPII_D_RAID, "%s: mpii_get_ioc_pg8\n", DEVNAME(sc));
   2087   1.1    bouyer 
   2088   1.1    bouyer 	if (mpii_cfg_header(sc, MPII_CONFIG_REQ_PAGE_TYPE_IOC, 8, 0,
   2089   1.1    bouyer 	    &hdr) != 0) {
   2090   1.1    bouyer 		DNPRINTF(MPII_D_CFG, "%s: mpii_get_ioc_pg8 unable to fetch "
   2091   1.1    bouyer 		    "header for IOC page 8\n", DEVNAME(sc));
   2092   1.1    bouyer 		return (1);
   2093   1.1    bouyer 	}
   2094   1.1    bouyer 
   2095   1.1    bouyer 	pagelen = hdr.page_length * 4; /* dwords to bytes */
   2096   1.1    bouyer 
   2097   1.1    bouyer 	page = malloc(pagelen, M_TEMP, M_NOWAIT);
   2098   1.1    bouyer 	if (page == NULL) {
   2099   1.1    bouyer 		DNPRINTF(MPII_D_CFG, "%s: mpii_get_ioc_pg8 unable to allocate "
   2100   1.1    bouyer 		    "space for ioc config page 8\n", DEVNAME(sc));
   2101   1.1    bouyer 		return (1);
   2102   1.1    bouyer 	}
   2103   1.1    bouyer 
   2104   1.1    bouyer 	if (mpii_cfg_page(sc, 0, &hdr, 1, page, pagelen) != 0) {
   2105   1.1    bouyer 		DNPRINTF(MPII_D_CFG, "%s: mpii_get_raid unable to fetch IOC "
   2106   1.1    bouyer 		    "page 8\n", DEVNAME(sc));
   2107   1.1    bouyer 		rv = 1;
   2108   1.1    bouyer 		goto out;
   2109   1.1    bouyer 	}
   2110   1.1    bouyer 
   2111   1.1    bouyer 	DNPRINTF(MPII_D_CFG, "%s:  numdevsperenclosure: 0x%02x\n", DEVNAME(sc),
   2112   1.1    bouyer 	    page->num_devs_per_enclosure);
   2113   1.1    bouyer 	DNPRINTF(MPII_D_CFG, "%s:  maxpersistententries: 0x%04x "
   2114   1.1    bouyer 	    "maxnumphysicalmappedids: 0x%04x\n", DEVNAME(sc),
   2115   1.1    bouyer 	    le16toh(page->max_persistent_entries),
   2116   1.1    bouyer 	    le16toh(page->max_num_physical_mapped_ids));
   2117   1.1    bouyer 	DNPRINTF(MPII_D_CFG, "%s:  flags: 0x%04x\n", DEVNAME(sc),
   2118   1.1    bouyer 	    le16toh(page->flags));
   2119   1.1    bouyer 	DNPRINTF(MPII_D_CFG, "%s:  irvolumemappingflags: 0x%04x\n",
   2120   1.1    bouyer 	    DEVNAME(sc), le16toh(page->ir_volume_mapping_flags));
   2121   1.1    bouyer 
   2122   1.1    bouyer 	if (page->flags & MPII_IOC_PG8_FLAGS_RESERVED_TARGETID_0)
   2123   1.1    bouyer 		pad = 1;
   2124   1.1    bouyer 
   2125   1.1    bouyer 	flags = page->ir_volume_mapping_flags &
   2126   1.1    bouyer 	    MPII_IOC_PG8_IRFLAGS_VOLUME_MAPPING_MODE_MASK;
   2127   1.1    bouyer 	if (ISSET(sc->sc_flags, MPII_F_RAID)) {
   2128   1.1    bouyer 		if (flags == MPII_IOC_PG8_IRFLAGS_LOW_VOLUME_MAPPING) {
   2129   1.1    bouyer 			sc->sc_vd_id_low += pad;
   2130   1.1    bouyer 			pad = sc->sc_max_volumes; /* for sc_pd_id_start */
   2131   1.1    bouyer 		} else
   2132   1.1    bouyer 			sc->sc_vd_id_low = sc->sc_max_devices -
   2133   1.1    bouyer 			    sc->sc_max_volumes;
   2134   1.1    bouyer 	}
   2135   1.1    bouyer 
   2136   1.1    bouyer 	sc->sc_pd_id_start += pad;
   2137   1.1    bouyer 
   2138   1.1    bouyer 	DNPRINTF(MPII_D_MAP, "%s: mpii_get_ioc_pg8 mapping: sc_pd_id_start: %d "
   2139   1.1    bouyer 	    "sc_vd_id_low: %d sc_max_volumes: %d\n", DEVNAME(sc),
   2140   1.1    bouyer 	    sc->sc_pd_id_start, sc->sc_vd_id_low, sc->sc_max_volumes);
   2141   1.1    bouyer 
   2142   1.1    bouyer out:
   2143   1.1    bouyer 	free(page, M_TEMP);
   2144   1.1    bouyer 
   2145   1.1    bouyer 	return(rv);
   2146   1.1    bouyer }
   2147   1.1    bouyer 
   2148   1.1    bouyer static int
   2149   1.1    bouyer mpii_req_cfg_header(struct mpii_softc *sc, u_int8_t type, u_int8_t number,
   2150   1.1    bouyer     u_int32_t address, int flags, void *p)
   2151   1.1    bouyer {
   2152   1.1    bouyer 	struct mpii_msg_config_request		*cq;
   2153   1.1    bouyer 	struct mpii_msg_config_reply		*cp;
   2154   1.1    bouyer 	struct mpii_cfg_hdr	*hdr = p;
   2155   1.1    bouyer 	struct mpii_ccb		*ccb;
   2156   1.1    bouyer 	struct mpii_ecfg_hdr	*ehdr = p;
   2157   1.1    bouyer 	int			etype = 0;
   2158   1.1    bouyer 	int			rv = 0;
   2159   1.1    bouyer 
   2160   1.1    bouyer 	DNPRINTF(MPII_D_MISC, "%s: mpii_req_cfg_header type: %#x number: %x "
   2161   1.1    bouyer 	    "address: 0x%08x flags: 0x%x\n", DEVNAME(sc), type, number,
   2162   1.1    bouyer 	    address, flags);
   2163   1.1    bouyer 
   2164   1.1    bouyer 	ccb = mpii_get_ccb(sc, ISSET(flags, MPII_PG_POLL) ? MPII_NOSLEEP : 0);
   2165   1.1    bouyer 	if (ccb == NULL) {
   2166   1.1    bouyer 		DNPRINTF(MPII_D_MISC, "%s: mpii_cfg_header ccb_get\n",
   2167   1.1    bouyer 		    DEVNAME(sc));
   2168   1.1    bouyer 		return (1);
   2169   1.1    bouyer 	}
   2170   1.1    bouyer 
   2171   1.1    bouyer 	if (ISSET(flags, MPII_PG_EXTENDED)) {
   2172   1.1    bouyer 		etype = type;
   2173   1.1    bouyer 		type = MPII_CONFIG_REQ_PAGE_TYPE_EXTENDED;
   2174   1.1    bouyer 	}
   2175   1.1    bouyer 
   2176   1.1    bouyer 	cq = ccb->ccb_cmd;
   2177   1.1    bouyer 
   2178   1.1    bouyer 	cq->function = MPII_FUNCTION_CONFIG;
   2179   1.1    bouyer 
   2180   1.1    bouyer 	cq->action = MPII_CONFIG_REQ_ACTION_PAGE_HEADER;
   2181   1.1    bouyer 
   2182   1.1    bouyer 	cq->config_header.page_number = number;
   2183   1.1    bouyer 	cq->config_header.page_type = type;
   2184   1.1    bouyer 	cq->ext_page_type = etype;
   2185   1.1    bouyer 	cq->page_address = htole32(address);
   2186   1.1    bouyer 	cq->page_buffer.sg_hdr = htole32(MPII_SGE_FL_TYPE_SIMPLE |
   2187   1.1    bouyer 	    MPII_SGE_FL_LAST | MPII_SGE_FL_EOB | MPII_SGE_FL_EOL);
   2188   1.1    bouyer 
   2189   1.1    bouyer 	ccb->ccb_done = mpii_empty_done;
   2190   1.1    bouyer 	if (ISSET(flags, MPII_PG_POLL)) {
   2191   1.1    bouyer 		if (mpii_poll(sc, ccb) != 0) {
   2192   1.1    bouyer 			DNPRINTF(MPII_D_MISC, "%s: mpii_cfg_header poll\n",
   2193   1.1    bouyer 			    DEVNAME(sc));
   2194   1.1    bouyer 			return (1);
   2195   1.1    bouyer 		}
   2196   1.1    bouyer 	} else
   2197   1.1    bouyer 		mpii_wait(sc, ccb);
   2198   1.1    bouyer 
   2199   1.1    bouyer 	if (ccb->ccb_rcb == NULL) {
   2200   1.1    bouyer 		mpii_put_ccb(sc, ccb);
   2201   1.1    bouyer 		return (1);
   2202   1.1    bouyer 	}
   2203   1.1    bouyer 	cp = ccb->ccb_rcb->rcb_reply;
   2204   1.1    bouyer 
   2205   1.1    bouyer 	DNPRINTF(MPII_D_MISC, "%s:  action: 0x%02x sgl_flags: 0x%02x "
   2206   1.1    bouyer 	    "msg_length: %d function: 0x%02x\n", DEVNAME(sc), cp->action,
   2207   1.1    bouyer 	    cp->sgl_flags, cp->msg_length, cp->function);
   2208   1.1    bouyer 	DNPRINTF(MPII_D_MISC, "%s:  ext_page_length: %d ext_page_type: 0x%02x "
   2209   1.1    bouyer 	    "msg_flags: 0x%02x\n", DEVNAME(sc),
   2210   1.1    bouyer 	    le16toh(cp->ext_page_length), cp->ext_page_type,
   2211   1.1    bouyer 	    cp->msg_flags);
   2212   1.1    bouyer 	DNPRINTF(MPII_D_MISC, "%s:  vp_id: 0x%02x vf_id: 0x%02x\n", DEVNAME(sc),
   2213   1.1    bouyer 	    cp->vp_id, cp->vf_id);
   2214   1.1    bouyer 	DNPRINTF(MPII_D_MISC, "%s:  ioc_status: 0x%04x\n", DEVNAME(sc),
   2215   1.1    bouyer 	    le16toh(cp->ioc_status));
   2216   1.1    bouyer 	DNPRINTF(MPII_D_MISC, "%s:  ioc_loginfo: 0x%08x\n", DEVNAME(sc),
   2217   1.1    bouyer 	    le32toh(cp->ioc_loginfo));
   2218   1.1    bouyer 	DNPRINTF(MPII_D_MISC, "%s:  page_version: 0x%02x page_length: %d "
   2219   1.1    bouyer 	    "page_number: 0x%02x page_type: 0x%02x\n", DEVNAME(sc),
   2220   1.1    bouyer 	    cp->config_header.page_version,
   2221   1.1    bouyer 	    cp->config_header.page_length,
   2222   1.1    bouyer 	    cp->config_header.page_number,
   2223   1.1    bouyer 	    cp->config_header.page_type);
   2224   1.1    bouyer 
   2225   1.1    bouyer 	if (le16toh(cp->ioc_status) != MPII_IOCSTATUS_SUCCESS)
   2226   1.1    bouyer 		rv = 1;
   2227   1.1    bouyer 	else if (ISSET(flags, MPII_PG_EXTENDED)) {
   2228   1.1    bouyer 		bzero(ehdr, sizeof(*ehdr));
   2229   1.1    bouyer 		ehdr->page_version = cp->config_header.page_version;
   2230   1.1    bouyer 		ehdr->page_number = cp->config_header.page_number;
   2231   1.1    bouyer 		ehdr->page_type = cp->config_header.page_type;
   2232   1.1    bouyer 		ehdr->ext_page_length = cp->ext_page_length;
   2233   1.1    bouyer 		ehdr->ext_page_type = cp->ext_page_type;
   2234   1.1    bouyer 	} else
   2235   1.1    bouyer 		*hdr = cp->config_header;
   2236   1.1    bouyer 
   2237   1.1    bouyer 	mpii_push_reply(sc, ccb->ccb_rcb);
   2238   1.1    bouyer 	mpii_put_ccb(sc, ccb);
   2239   1.1    bouyer 
   2240   1.1    bouyer 	return (rv);
   2241   1.1    bouyer }
   2242   1.1    bouyer 
   2243   1.1    bouyer static int
   2244   1.1    bouyer mpii_req_cfg_page(struct mpii_softc *sc, u_int32_t address, int flags,
   2245   1.1    bouyer     void *p, int read, void *page, size_t len)
   2246   1.1    bouyer {
   2247   1.1    bouyer 	struct mpii_msg_config_request		*cq;
   2248   1.1    bouyer 	struct mpii_msg_config_reply		*cp;
   2249   1.1    bouyer 	struct mpii_cfg_hdr	*hdr = p;
   2250   1.1    bouyer 	struct mpii_ccb		*ccb;
   2251   1.1    bouyer 	struct mpii_ecfg_hdr	*ehdr = p;
   2252   1.1    bouyer 	u_int64_t		dva;
   2253   1.1    bouyer 	char			*kva;
   2254   1.1    bouyer 	int			page_length;
   2255   1.1    bouyer 	int			rv = 0;
   2256   1.1    bouyer 
   2257   1.1    bouyer 	DNPRINTF(MPII_D_MISC, "%s: mpii_cfg_page address: %d read: %d "
   2258   1.1    bouyer 	    "type: %x\n", DEVNAME(sc), address, read, hdr->page_type);
   2259   1.1    bouyer 
   2260   1.1    bouyer 	page_length = ISSET(flags, MPII_PG_EXTENDED) ?
   2261   1.1    bouyer 	    le16toh(ehdr->ext_page_length) : hdr->page_length;
   2262   1.1    bouyer 
   2263   1.1    bouyer 	if (len > MPII_REQUEST_SIZE - sizeof(struct mpii_msg_config_request) ||
   2264   1.1    bouyer     	    len < page_length * 4)
   2265   1.1    bouyer 		return (1);
   2266   1.1    bouyer 
   2267   1.1    bouyer 	ccb = mpii_get_ccb(sc,
   2268   1.1    bouyer 	    ISSET(flags, MPII_PG_POLL) ? MPII_NOSLEEP : 0);
   2269   1.1    bouyer 	if (ccb == NULL) {
   2270   1.1    bouyer 		DNPRINTF(MPII_D_MISC, "%s: mpii_cfg_page ccb_get\n",
   2271   1.1    bouyer 		    DEVNAME(sc));
   2272   1.1    bouyer 		return (1);
   2273   1.1    bouyer 	}
   2274   1.1    bouyer 
   2275   1.1    bouyer 	cq = ccb->ccb_cmd;
   2276   1.1    bouyer 
   2277   1.1    bouyer 	cq->function = MPII_FUNCTION_CONFIG;
   2278   1.1    bouyer 
   2279   1.1    bouyer 	cq->action = (read ? MPII_CONFIG_REQ_ACTION_PAGE_READ_CURRENT :
   2280   1.1    bouyer 	    MPII_CONFIG_REQ_ACTION_PAGE_WRITE_CURRENT);
   2281   1.1    bouyer 
   2282   1.1    bouyer 	if (ISSET(flags, MPII_PG_EXTENDED)) {
   2283   1.1    bouyer 		cq->config_header.page_version = ehdr->page_version;
   2284   1.1    bouyer 		cq->config_header.page_number = ehdr->page_number;
   2285   1.1    bouyer 		cq->config_header.page_type = ehdr->page_type;
   2286   1.1    bouyer 		cq->ext_page_len = ehdr->ext_page_length;
   2287   1.1    bouyer 		cq->ext_page_type = ehdr->ext_page_type;
   2288   1.1    bouyer 	} else
   2289   1.1    bouyer 		cq->config_header = *hdr;
   2290   1.1    bouyer 	cq->config_header.page_type &= MPII_CONFIG_REQ_PAGE_TYPE_MASK;
   2291   1.1    bouyer 	cq->page_address = htole32(address);
   2292   1.1    bouyer 	cq->page_buffer.sg_hdr = htole32(MPII_SGE_FL_TYPE_SIMPLE |
   2293   1.1    bouyer 	    MPII_SGE_FL_LAST | MPII_SGE_FL_EOB | MPII_SGE_FL_EOL |
   2294   1.1    bouyer 	    MPII_SGE_FL_SIZE_64 | (page_length * 4) |
   2295   1.1    bouyer 	    (read ? MPII_SGE_FL_DIR_IN : MPII_SGE_FL_DIR_OUT));
   2296   1.1    bouyer 
   2297   1.1    bouyer 	/* bounce the page via the request space to avoid more bus_dma games */
   2298   1.1    bouyer 	dva = ccb->ccb_cmd_dva + sizeof(struct mpii_msg_config_request);
   2299   1.1    bouyer 
   2300   1.1    bouyer 	cq->page_buffer.sg_hi_addr = htole32((u_int32_t)(dva >> 32));
   2301   1.1    bouyer 	cq->page_buffer.sg_lo_addr = htole32((u_int32_t)dva);
   2302   1.1    bouyer 
   2303   1.1    bouyer 	kva = ccb->ccb_cmd;
   2304   1.1    bouyer 	kva += sizeof(struct mpii_msg_config_request);
   2305   1.1    bouyer 
   2306   1.1    bouyer 	if (!read)
   2307   1.1    bouyer 		bcopy(page, kva, len);
   2308   1.1    bouyer 
   2309   1.1    bouyer 	ccb->ccb_done = mpii_empty_done;
   2310   1.1    bouyer 	if (ISSET(flags, MPII_PG_POLL)) {
   2311   1.1    bouyer 		if (mpii_poll(sc, ccb) != 0) {
   2312   1.1    bouyer 			DNPRINTF(MPII_D_MISC, "%s: mpii_cfg_header poll\n",
   2313   1.1    bouyer 			    DEVNAME(sc));
   2314   1.1    bouyer 			return (1);
   2315   1.1    bouyer 		}
   2316   1.1    bouyer 	} else
   2317   1.1    bouyer 		mpii_wait(sc, ccb);
   2318   1.1    bouyer 
   2319   1.1    bouyer 	if (ccb->ccb_rcb == NULL) {
   2320   1.1    bouyer 		mpii_put_ccb(sc, ccb);
   2321   1.1    bouyer 		return (1);
   2322   1.1    bouyer 	}
   2323   1.1    bouyer 	cp = ccb->ccb_rcb->rcb_reply;
   2324   1.1    bouyer 
   2325   1.1    bouyer 	DNPRINTF(MPII_D_MISC, "%s:  action: 0x%02x "
   2326   1.1    bouyer 	    "msg_length: %d function: 0x%02x\n", DEVNAME(sc), cp->action,
   2327   1.1    bouyer 	    cp->msg_length, cp->function);
   2328   1.1    bouyer 	DNPRINTF(MPII_D_MISC, "%s:  ext_page_length: %d ext_page_type: 0x%02x "
   2329   1.1    bouyer 	    "msg_flags: 0x%02x\n", DEVNAME(sc),
   2330   1.1    bouyer 	    le16toh(cp->ext_page_length), cp->ext_page_type,
   2331   1.1    bouyer 	    cp->msg_flags);
   2332   1.1    bouyer 	DNPRINTF(MPII_D_MISC, "%s:  vp_id: 0x%02x vf_id: 0x%02x\n", DEVNAME(sc),
   2333   1.1    bouyer 	    cp->vp_id, cp->vf_id);
   2334   1.1    bouyer 	DNPRINTF(MPII_D_MISC, "%s:  ioc_status: 0x%04x\n", DEVNAME(sc),
   2335   1.1    bouyer 	    le16toh(cp->ioc_status));
   2336   1.1    bouyer 	DNPRINTF(MPII_D_MISC, "%s:  ioc_loginfo: 0x%08x\n", DEVNAME(sc),
   2337   1.1    bouyer 	    le32toh(cp->ioc_loginfo));
   2338   1.1    bouyer 	DNPRINTF(MPII_D_MISC, "%s:  page_version: 0x%02x page_length: %d "
   2339   1.1    bouyer 	    "page_number: 0x%02x page_type: 0x%02x\n", DEVNAME(sc),
   2340   1.1    bouyer 	    cp->config_header.page_version,
   2341   1.1    bouyer 	    cp->config_header.page_length,
   2342   1.1    bouyer 	    cp->config_header.page_number,
   2343   1.1    bouyer 	    cp->config_header.page_type);
   2344   1.1    bouyer 
   2345   1.1    bouyer 	if (le16toh(cp->ioc_status) != MPII_IOCSTATUS_SUCCESS)
   2346   1.1    bouyer 		rv = 1;
   2347   1.1    bouyer 	else if (read)
   2348   1.1    bouyer 		bcopy(kva, page, len);
   2349   1.1    bouyer 
   2350   1.1    bouyer 	mpii_push_reply(sc, ccb->ccb_rcb);
   2351   1.1    bouyer 	mpii_put_ccb(sc, ccb);
   2352   1.1    bouyer 
   2353   1.1    bouyer 	return (rv);
   2354   1.1    bouyer }
   2355   1.1    bouyer 
   2356   1.1    bouyer static struct mpii_rcb *
   2357   1.1    bouyer mpii_reply(struct mpii_softc *sc, struct mpii_reply_descr *rdp)
   2358   1.1    bouyer {
   2359   1.1    bouyer 	struct mpii_rcb		*rcb = NULL;
   2360   1.1    bouyer 	u_int32_t		rfid;
   2361   1.1    bouyer 
   2362   1.1    bouyer 	DNPRINTF(MPII_D_INTR, "%s: mpii_reply\n", DEVNAME(sc));
   2363   1.1    bouyer 
   2364   1.1    bouyer 	if ((rdp->reply_flags & MPII_REPLY_DESCR_TYPE_MASK) ==
   2365   1.1    bouyer 	    MPII_REPLY_DESCR_ADDRESS_REPLY) {
   2366   1.1    bouyer 		rfid = (le32toh(rdp->frame_addr) -
   2367   1.1    bouyer 		    (u_int32_t)MPII_DMA_DVA(sc->sc_replies)) / MPII_REPLY_SIZE;
   2368   1.1    bouyer 
   2369   1.1    bouyer 		bus_dmamap_sync(sc->sc_dmat,
   2370   1.1    bouyer 		    MPII_DMA_MAP(sc->sc_replies), MPII_REPLY_SIZE * rfid,
   2371   1.1    bouyer 		    MPII_REPLY_SIZE, BUS_DMASYNC_POSTREAD);
   2372   1.1    bouyer 
   2373   1.1    bouyer 		rcb = &sc->sc_rcbs[rfid];
   2374   1.1    bouyer 	}
   2375   1.1    bouyer 
   2376   1.1    bouyer 	memset(rdp, 0xff, sizeof(*rdp));
   2377   1.1    bouyer 
   2378   1.1    bouyer 	bus_dmamap_sync(sc->sc_dmat, MPII_DMA_MAP(sc->sc_reply_postq),
   2379   1.1    bouyer 	    8 * sc->sc_reply_post_host_index, 8,
   2380   1.1    bouyer 	    BUS_DMASYNC_POSTREAD | BUS_DMASYNC_POSTWRITE);
   2381   1.1    bouyer 
   2382   1.1    bouyer 	return (rcb);
   2383   1.1    bouyer }
   2384   1.1    bouyer 
   2385   1.1    bouyer static struct mpii_dmamem *
   2386   1.1    bouyer mpii_dmamem_alloc(struct mpii_softc *sc, size_t size)
   2387   1.1    bouyer {
   2388   1.1    bouyer 	struct mpii_dmamem	*mdm;
   2389   1.1    bouyer 	int			nsegs;
   2390   1.1    bouyer 
   2391   1.1    bouyer 	mdm = malloc(sizeof(*mdm), M_DEVBUF, M_NOWAIT | M_ZERO);
   2392   1.1    bouyer 	if (mdm == NULL)
   2393  1.11      maya 		return (NULL);
   2394   1.1    bouyer 
   2395   1.1    bouyer 	mdm->mdm_size = size;
   2396   1.1    bouyer 
   2397   1.1    bouyer 	if (bus_dmamap_create(sc->sc_dmat, size, 1, size, 0,
   2398   1.1    bouyer 	    BUS_DMA_NOWAIT | BUS_DMA_ALLOCNOW, &mdm->mdm_map) != 0)
   2399   1.1    bouyer 		goto mdmfree;
   2400   1.1    bouyer 
   2401   1.1    bouyer 	if (bus_dmamem_alloc(sc->sc_dmat, size, PAGE_SIZE, 0, &mdm->mdm_seg,
   2402   1.1    bouyer 	    1, &nsegs, BUS_DMA_NOWAIT) != 0) goto destroy;
   2403   1.1    bouyer 
   2404   1.1    bouyer 	if (bus_dmamem_map(sc->sc_dmat, &mdm->mdm_seg, nsegs, size,
   2405   1.1    bouyer 	    &mdm->mdm_kva, BUS_DMA_NOWAIT) != 0)
   2406   1.1    bouyer 		goto free;
   2407   1.1    bouyer 
   2408   1.1    bouyer 	if (bus_dmamap_load(sc->sc_dmat, mdm->mdm_map, mdm->mdm_kva, size,
   2409   1.1    bouyer 	    NULL, BUS_DMA_NOWAIT) != 0)
   2410   1.1    bouyer 		goto unmap;
   2411   1.1    bouyer 
   2412   1.1    bouyer 	DNPRINTF(MPII_D_MEM,
   2413   1.1    bouyer 	    "  kva: %p  dva: 0x%" PRIx64 "  map: %p  size: %" PRId64 "\n",
   2414   1.1    bouyer 	    mdm->mdm_kva, (uint64_t)mdm->mdm_map->dm_segs[0].ds_addr,
   2415   1.1    bouyer 	    mdm->mdm_map, (uint64_t)size);
   2416   1.1    bouyer 
   2417   1.1    bouyer 	bzero(mdm->mdm_kva, size);
   2418   1.1    bouyer 
   2419   1.1    bouyer 	return (mdm);
   2420   1.1    bouyer 
   2421   1.1    bouyer unmap:
   2422   1.1    bouyer 	bus_dmamem_unmap(sc->sc_dmat, mdm->mdm_kva, size);
   2423   1.1    bouyer free:
   2424   1.1    bouyer 	bus_dmamem_free(sc->sc_dmat, &mdm->mdm_seg, 1);
   2425   1.1    bouyer destroy:
   2426   1.1    bouyer 	bus_dmamap_destroy(sc->sc_dmat, mdm->mdm_map);
   2427   1.1    bouyer mdmfree:
   2428   1.1    bouyer 	free(mdm, M_DEVBUF);
   2429   1.1    bouyer 
   2430   1.1    bouyer 	return (NULL);
   2431   1.1    bouyer }
   2432   1.1    bouyer 
   2433   1.1    bouyer static void
   2434   1.1    bouyer mpii_dmamem_free(struct mpii_softc *sc, struct mpii_dmamem *mdm)
   2435   1.1    bouyer {
   2436   1.1    bouyer 	DNPRINTF(MPII_D_MEM, "%s: mpii_dmamem_free %p\n", DEVNAME(sc), mdm);
   2437   1.1    bouyer 
   2438   1.1    bouyer 	bus_dmamap_unload(sc->sc_dmat, mdm->mdm_map);
   2439   1.1    bouyer 	bus_dmamem_unmap(sc->sc_dmat, mdm->mdm_kva, mdm->mdm_size);
   2440   1.1    bouyer 	bus_dmamem_free(sc->sc_dmat, &mdm->mdm_seg, 1);
   2441   1.1    bouyer 	bus_dmamap_destroy(sc->sc_dmat, mdm->mdm_map);
   2442   1.1    bouyer 	free(mdm, M_DEVBUF);
   2443   1.1    bouyer }
   2444   1.1    bouyer 
   2445   1.1    bouyer static int
   2446   1.1    bouyer mpii_alloc_dev(struct mpii_softc *sc)
   2447   1.1    bouyer {
   2448   1.1    bouyer 	sc->sc_devs = malloc(sc->sc_max_devices *
   2449   1.1    bouyer 	    sizeof(struct mpii_device *), M_DEVBUF, M_NOWAIT | M_ZERO);
   2450   1.1    bouyer 	if (sc->sc_devs == NULL)
   2451   1.1    bouyer 		return (1);
   2452   1.1    bouyer 	return (0);
   2453   1.1    bouyer }
   2454   1.1    bouyer 
   2455   1.1    bouyer static int
   2456   1.1    bouyer mpii_insert_dev(struct mpii_softc *sc, struct mpii_device *dev)
   2457   1.1    bouyer {
   2458  1.10      maya 
   2459  1.10      maya 	if (!dev || dev->slot < 0)
   2460  1.10      maya 		return (1);
   2461  1.10      maya 
   2462   1.1    bouyer 	int slot = dev->slot; 	/* initial hint */
   2463   1.1    bouyer 
   2464   1.1    bouyer 	while (slot < sc->sc_max_devices && sc->sc_devs[slot] != NULL)
   2465   1.1    bouyer 		slot++;
   2466   1.1    bouyer 	if (slot >= sc->sc_max_devices)
   2467   1.1    bouyer 		return (1);
   2468   1.1    bouyer 	dev->slot = slot;
   2469   1.1    bouyer 	sc->sc_devs[slot] = dev;
   2470   1.1    bouyer 	return (0);
   2471   1.1    bouyer }
   2472   1.1    bouyer 
   2473   1.1    bouyer static int
   2474   1.1    bouyer mpii_remove_dev(struct mpii_softc *sc, struct mpii_device *dev)
   2475   1.1    bouyer {
   2476   1.1    bouyer 	int			i;
   2477   1.1    bouyer 
   2478   1.1    bouyer 	if (!dev)
   2479   1.1    bouyer 		return (1);
   2480   1.1    bouyer 	for (i = 0; i < sc->sc_max_devices;  i++)
   2481   1.1    bouyer 		if (sc->sc_devs[i] &&
   2482   1.1    bouyer 		    sc->sc_devs[i]->dev_handle == dev->dev_handle) {
   2483   1.1    bouyer 			sc->sc_devs[i] = NULL;
   2484   1.1    bouyer 			return (0);
   2485   1.1    bouyer 		}
   2486   1.1    bouyer 	return (1);
   2487   1.1    bouyer }
   2488   1.1    bouyer 
   2489   1.1    bouyer static struct mpii_device *
   2490   1.1    bouyer mpii_find_dev(struct mpii_softc *sc, u_int16_t handle)
   2491   1.1    bouyer {
   2492   1.1    bouyer 	int			i;
   2493   1.1    bouyer 
   2494   1.1    bouyer 	for (i = 0; i < sc->sc_max_devices;  i++)
   2495   1.1    bouyer 		if (sc->sc_devs[i] && sc->sc_devs[i]->dev_handle == handle)
   2496   1.1    bouyer 			return (sc->sc_devs[i]);
   2497   1.1    bouyer 	return (NULL);
   2498   1.1    bouyer }
   2499   1.1    bouyer 
   2500   1.1    bouyer static int
   2501   1.1    bouyer mpii_alloc_ccbs(struct mpii_softc *sc)
   2502   1.1    bouyer {
   2503   1.1    bouyer 	struct mpii_ccb		*ccb;
   2504   1.1    bouyer 	u_int8_t		*cmd;
   2505   1.1    bouyer 	int			i;
   2506   1.1    bouyer 
   2507   1.1    bouyer 	SIMPLEQ_INIT(&sc->sc_ccb_free);
   2508   1.1    bouyer 
   2509   1.1    bouyer 	sc->sc_ccbs = malloc(sizeof(*ccb) * (sc->sc_request_depth-1),
   2510   1.1    bouyer 	    M_DEVBUF, M_NOWAIT | M_ZERO);
   2511   1.1    bouyer 	if (sc->sc_ccbs == NULL) {
   2512   1.1    bouyer 		printf("%s: unable to allocate ccbs\n", DEVNAME(sc));
   2513   1.1    bouyer 		return (1);
   2514   1.1    bouyer 	}
   2515   1.1    bouyer 
   2516   1.1    bouyer 	sc->sc_requests = mpii_dmamem_alloc(sc,
   2517   1.1    bouyer 	    MPII_REQUEST_SIZE * sc->sc_request_depth);
   2518   1.1    bouyer 	if (sc->sc_requests == NULL) {
   2519   1.1    bouyer 		printf("%s: unable to allocate ccb dmamem\n", DEVNAME(sc));
   2520   1.1    bouyer 		goto free_ccbs;
   2521   1.1    bouyer 	}
   2522   1.1    bouyer 	cmd = MPII_DMA_KVA(sc->sc_requests);
   2523   1.1    bouyer 	bzero(cmd, MPII_REQUEST_SIZE * sc->sc_request_depth);
   2524   1.1    bouyer 
   2525   1.1    bouyer 	/*
   2526   1.1    bouyer 	 * we have sc->sc_request_depth system request message
   2527   1.1    bouyer 	 * frames, but smid zero cannot be used. so we then
   2528   1.1    bouyer 	 * have (sc->sc_request_depth - 1) number of ccbs
   2529   1.1    bouyer 	 */
   2530   1.1    bouyer 	for (i = 1; i < sc->sc_request_depth; i++) {
   2531   1.1    bouyer 		ccb = &sc->sc_ccbs[i - 1];
   2532   1.1    bouyer 
   2533   1.1    bouyer 		if (bus_dmamap_create(sc->sc_dmat, MAXPHYS,
   2534   1.1    bouyer 		    sc->sc_max_sgl_len, MAXPHYS, 0,
   2535   1.1    bouyer 		    BUS_DMA_NOWAIT | BUS_DMA_ALLOCNOW,
   2536   1.1    bouyer 		    &ccb->ccb_dmamap) != 0) {
   2537   1.1    bouyer 			printf("%s: unable to create dma map\n", DEVNAME(sc));
   2538   1.1    bouyer 			goto free_maps;
   2539   1.1    bouyer 		}
   2540   1.1    bouyer 
   2541   1.1    bouyer 		ccb->ccb_sc = sc;
   2542   1.1    bouyer 		ccb->ccb_smid = i;
   2543   1.1    bouyer 		ccb->ccb_offset = MPII_REQUEST_SIZE * i;
   2544   1.1    bouyer 
   2545   1.1    bouyer 		ccb->ccb_cmd = &cmd[ccb->ccb_offset];
   2546   1.1    bouyer 		ccb->ccb_cmd_dva = (u_int32_t)MPII_DMA_DVA(sc->sc_requests) +
   2547   1.1    bouyer 		    ccb->ccb_offset;
   2548   1.1    bouyer 
   2549   1.1    bouyer 		DNPRINTF(MPII_D_CCB, "%s: mpii_alloc_ccbs(%d) ccb: %p map: %p "
   2550   1.1    bouyer 		    "sc: %p smid: %#x offs: %#" PRIx64 " cmd: %#" PRIx64 " dva: %#" PRIx64 "\n",
   2551   1.1    bouyer 		    DEVNAME(sc), i, ccb, ccb->ccb_dmamap, ccb->ccb_sc,
   2552   1.1    bouyer 		    ccb->ccb_smid, (uint64_t)ccb->ccb_offset,
   2553   1.1    bouyer 		    (uint64_t)ccb->ccb_cmd, (uint64_t)ccb->ccb_cmd_dva);
   2554   1.1    bouyer 
   2555   1.1    bouyer 		mpii_put_ccb(sc, ccb);
   2556   1.1    bouyer 	}
   2557   1.1    bouyer 
   2558   1.1    bouyer 	return (0);
   2559   1.1    bouyer 
   2560   1.1    bouyer free_maps:
   2561   1.1    bouyer 	while ((ccb = mpii_get_ccb(sc, MPII_NOSLEEP)) != NULL)
   2562   1.1    bouyer 		bus_dmamap_destroy(sc->sc_dmat, ccb->ccb_dmamap);
   2563   1.1    bouyer 
   2564   1.1    bouyer 	mpii_dmamem_free(sc, sc->sc_requests);
   2565   1.1    bouyer free_ccbs:
   2566   1.1    bouyer 	free(sc->sc_ccbs, M_DEVBUF);
   2567   1.1    bouyer 
   2568   1.1    bouyer 	return (1);
   2569   1.1    bouyer }
   2570   1.1    bouyer 
   2571   1.1    bouyer static void
   2572   1.1    bouyer mpii_put_ccb(struct mpii_softc *sc, struct mpii_ccb *ccb)
   2573   1.1    bouyer {
   2574   1.1    bouyer 	KASSERT(ccb->ccb_sc == sc);
   2575   1.1    bouyer 	DNPRINTF(MPII_D_CCB, "%s: mpii_put_ccb %p\n", DEVNAME(sc), ccb);
   2576   1.1    bouyer 
   2577   1.1    bouyer 	ccb->ccb_state = MPII_CCB_FREE;
   2578   1.1    bouyer 	ccb->ccb_cookie = NULL;
   2579   1.1    bouyer 	ccb->ccb_done = NULL;
   2580   1.1    bouyer 	ccb->ccb_rcb = NULL;
   2581   1.1    bouyer 	bzero(ccb->ccb_cmd, MPII_REQUEST_SIZE);
   2582   1.1    bouyer 
   2583   1.1    bouyer 	mutex_enter(&sc->sc_ccb_free_mtx);
   2584   1.1    bouyer 	SIMPLEQ_INSERT_HEAD(&sc->sc_ccb_free, ccb, u.ccb_link);
   2585   1.1    bouyer 	cv_signal(&sc->sc_ccb_free_cv);
   2586   1.1    bouyer 	mutex_exit(&sc->sc_ccb_free_mtx);
   2587   1.1    bouyer }
   2588   1.1    bouyer 
   2589   1.1    bouyer static struct mpii_ccb *
   2590   1.1    bouyer mpii_get_ccb(struct mpii_softc *sc, int flags)
   2591   1.1    bouyer {
   2592   1.1    bouyer 	struct mpii_ccb		*ccb;
   2593   1.1    bouyer 
   2594   1.1    bouyer 	mutex_enter(&sc->sc_ccb_free_mtx);
   2595   1.1    bouyer 	while ((ccb = SIMPLEQ_FIRST(&sc->sc_ccb_free)) == NULL) {
   2596   1.1    bouyer 		if (flags & MPII_NOSLEEP)
   2597   1.1    bouyer 			break;
   2598   1.1    bouyer 		cv_wait(&sc->sc_ccb_free_cv, &sc->sc_ccb_free_mtx);
   2599   1.1    bouyer 	}
   2600   1.1    bouyer 
   2601   1.1    bouyer 	if (ccb != NULL) {
   2602   1.1    bouyer 		SIMPLEQ_REMOVE_HEAD(&sc->sc_ccb_free, u.ccb_link);
   2603   1.1    bouyer 		ccb->ccb_state = MPII_CCB_READY;
   2604   1.1    bouyer 		KASSERT(ccb->ccb_sc == sc);
   2605   1.1    bouyer 	}
   2606   1.1    bouyer 	mutex_exit(&sc->sc_ccb_free_mtx);
   2607   1.1    bouyer 
   2608   1.1    bouyer 	DNPRINTF(MPII_D_CCB, "%s: mpii_get_ccb %p\n", DEVNAME(sc), ccb);
   2609   1.1    bouyer 
   2610   1.1    bouyer 	return (ccb);
   2611   1.1    bouyer }
   2612   1.1    bouyer 
   2613   1.1    bouyer static int
   2614   1.1    bouyer mpii_alloc_replies(struct mpii_softc *sc)
   2615   1.1    bouyer {
   2616   1.1    bouyer 	DNPRINTF(MPII_D_MISC, "%s: mpii_alloc_replies\n", DEVNAME(sc));
   2617   1.1    bouyer 
   2618   1.1    bouyer 	sc->sc_rcbs = malloc(sc->sc_num_reply_frames * sizeof(struct mpii_rcb),
   2619   1.1    bouyer 	    M_DEVBUF, M_NOWAIT);
   2620   1.1    bouyer 	if (sc->sc_rcbs == NULL)
   2621   1.1    bouyer 		return (1);
   2622   1.1    bouyer 
   2623   1.1    bouyer 	sc->sc_replies = mpii_dmamem_alloc(sc, MPII_REPLY_SIZE *
   2624   1.1    bouyer 	    sc->sc_num_reply_frames);
   2625   1.1    bouyer 	if (sc->sc_replies == NULL) {
   2626   1.1    bouyer 		free(sc->sc_rcbs, M_DEVBUF);
   2627   1.1    bouyer 		return (1);
   2628   1.1    bouyer 	}
   2629   1.1    bouyer 
   2630   1.1    bouyer 	return (0);
   2631   1.1    bouyer }
   2632   1.1    bouyer 
   2633   1.1    bouyer static void
   2634   1.1    bouyer mpii_push_replies(struct mpii_softc *sc)
   2635   1.1    bouyer {
   2636   1.1    bouyer 	struct mpii_rcb		*rcb;
   2637   1.1    bouyer 	char			*kva = MPII_DMA_KVA(sc->sc_replies);
   2638   1.1    bouyer 	int			i;
   2639   1.1    bouyer 
   2640   1.1    bouyer 	bus_dmamap_sync(sc->sc_dmat, MPII_DMA_MAP(sc->sc_replies),
   2641   1.1    bouyer 	    0, MPII_REPLY_SIZE * sc->sc_num_reply_frames, BUS_DMASYNC_PREREAD);
   2642   1.1    bouyer 
   2643   1.1    bouyer 	for (i = 0; i < sc->sc_num_reply_frames; i++) {
   2644   1.1    bouyer 		rcb = &sc->sc_rcbs[i];
   2645   1.1    bouyer 
   2646   1.1    bouyer 		rcb->rcb_reply = kva + MPII_REPLY_SIZE * i;
   2647   1.1    bouyer 		rcb->rcb_reply_dva = (u_int32_t)MPII_DMA_DVA(sc->sc_replies) +
   2648   1.1    bouyer 		    MPII_REPLY_SIZE * i;
   2649   1.1    bouyer 		mpii_push_reply(sc, rcb);
   2650   1.1    bouyer 	}
   2651   1.1    bouyer }
   2652   1.1    bouyer 
   2653   1.1    bouyer static void
   2654   1.1    bouyer mpii_start(struct mpii_softc *sc, struct mpii_ccb *ccb)
   2655   1.1    bouyer {
   2656   1.1    bouyer 	struct mpii_request_header	*rhp;
   2657   1.1    bouyer 	struct mpii_request_descr	descr;
   2658   1.1    bouyer 	u_int32_t			*rdp = (u_int32_t *)&descr;
   2659   1.1    bouyer 
   2660   1.1    bouyer 	DNPRINTF(MPII_D_RW, "%s: mpii_start %#" PRIx64 "\n", DEVNAME(sc),
   2661   1.1    bouyer 	    (uint64_t)ccb->ccb_cmd_dva);
   2662   1.1    bouyer 
   2663   1.1    bouyer 	rhp = ccb->ccb_cmd;
   2664   1.1    bouyer 
   2665   1.1    bouyer 	bzero(&descr, sizeof(descr));
   2666   1.1    bouyer 
   2667   1.1    bouyer 	switch (rhp->function) {
   2668   1.1    bouyer 	case MPII_FUNCTION_SCSI_IO_REQUEST:
   2669   1.1    bouyer 		descr.request_flags = MPII_REQ_DESCR_SCSI_IO;
   2670   1.1    bouyer 		descr.dev_handle = htole16(ccb->ccb_dev_handle);
   2671   1.1    bouyer 		break;
   2672   1.1    bouyer 	case MPII_FUNCTION_SCSI_TASK_MGMT:
   2673   1.1    bouyer 		descr.request_flags = MPII_REQ_DESCR_HIGH_PRIORITY;
   2674   1.1    bouyer 		break;
   2675   1.1    bouyer 	default:
   2676   1.1    bouyer 		descr.request_flags = MPII_REQ_DESCR_DEFAULT;
   2677   1.1    bouyer 	}
   2678   1.1    bouyer 
   2679   1.1    bouyer 	descr.vf_id = sc->sc_vf_id;
   2680   1.1    bouyer 	descr.smid = htole16(ccb->ccb_smid);
   2681   1.1    bouyer 
   2682   1.1    bouyer 	bus_dmamap_sync(sc->sc_dmat, MPII_DMA_MAP(sc->sc_requests),
   2683   1.1    bouyer 	    ccb->ccb_offset, MPII_REQUEST_SIZE,
   2684   1.1    bouyer 	    BUS_DMASYNC_PREREAD | BUS_DMASYNC_PREWRITE);
   2685   1.1    bouyer 
   2686   1.1    bouyer 	ccb->ccb_state = MPII_CCB_QUEUED;
   2687   1.1    bouyer 
   2688   1.1    bouyer 	DNPRINTF(MPII_D_RW, "%s:   MPII_REQ_DESCR_POST_LOW (0x%08x) write "
   2689   1.1    bouyer 	    "0x%08x\n", DEVNAME(sc), MPII_REQ_DESCR_POST_LOW, *rdp);
   2690   1.1    bouyer 
   2691   1.1    bouyer 	DNPRINTF(MPII_D_RW, "%s:   MPII_REQ_DESCR_POST_HIGH (0x%08x) write "
   2692   1.1    bouyer 	    "0x%08x\n", DEVNAME(sc), MPII_REQ_DESCR_POST_HIGH, *(rdp+1));
   2693   1.1    bouyer 
   2694   1.1    bouyer 	mutex_enter(&sc->sc_req_mtx);
   2695   1.1    bouyer 	mpii_write(sc, MPII_REQ_DESCR_POST_LOW, htole32(*rdp));
   2696   1.1    bouyer 	mpii_write(sc, MPII_REQ_DESCR_POST_HIGH, htole32(*(rdp+1)));
   2697   1.1    bouyer 	mutex_exit(&sc->sc_req_mtx);
   2698   1.1    bouyer }
   2699   1.1    bouyer 
   2700   1.1    bouyer static int
   2701   1.1    bouyer mpii_poll(struct mpii_softc *sc, struct mpii_ccb *ccb)
   2702   1.1    bouyer {
   2703   1.1    bouyer 	void				(*done)(struct mpii_ccb *);
   2704   1.1    bouyer 	void				*cookie;
   2705   1.1    bouyer 	int				rv = 1;
   2706   1.1    bouyer 
   2707   1.1    bouyer 	DNPRINTF(MPII_D_INTR, "%s: mpii_complete\n", DEVNAME(sc));
   2708   1.1    bouyer 
   2709   1.1    bouyer 	done = ccb->ccb_done;
   2710   1.1    bouyer 	cookie = ccb->ccb_cookie;
   2711   1.1    bouyer 
   2712   1.1    bouyer 	ccb->ccb_done = mpii_poll_done;
   2713   1.1    bouyer 	ccb->ccb_cookie = &rv;
   2714   1.1    bouyer 
   2715   1.1    bouyer 	mpii_start(sc, ccb);
   2716   1.1    bouyer 
   2717   1.1    bouyer 	while (rv == 1) {
   2718   1.1    bouyer 		/* avoid excessive polling */
   2719   1.1    bouyer 		if (mpii_reply_waiting(sc))
   2720   1.1    bouyer 			mpii_intr(sc);
   2721   1.1    bouyer 		else
   2722   1.1    bouyer 			delay(10);
   2723   1.1    bouyer 	}
   2724   1.1    bouyer 
   2725   1.1    bouyer 	ccb->ccb_cookie = cookie;
   2726   1.1    bouyer 	done(ccb);
   2727   1.1    bouyer 
   2728   1.1    bouyer 	return (0);
   2729   1.1    bouyer }
   2730   1.1    bouyer 
   2731   1.1    bouyer static void
   2732   1.1    bouyer mpii_poll_done(struct mpii_ccb *ccb)
   2733   1.1    bouyer {
   2734   1.1    bouyer 	int				*rv = ccb->ccb_cookie;
   2735   1.1    bouyer 
   2736   1.1    bouyer 	*rv = 0;
   2737   1.1    bouyer }
   2738   1.1    bouyer 
   2739   1.1    bouyer static int
   2740   1.1    bouyer mpii_alloc_queues(struct mpii_softc *sc)
   2741   1.1    bouyer {
   2742   1.1    bouyer 	u_int32_t		*kva;
   2743   1.1    bouyer 	u_int64_t		*kva64;
   2744   1.1    bouyer 	int			i;
   2745   1.1    bouyer 
   2746   1.1    bouyer 	DNPRINTF(MPII_D_MISC, "%s: mpii_alloc_queues\n", DEVNAME(sc));
   2747   1.1    bouyer 
   2748   1.1    bouyer 	sc->sc_reply_freeq = mpii_dmamem_alloc(sc,
   2749   1.1    bouyer 	    sc->sc_reply_free_qdepth * 4);
   2750   1.1    bouyer 	if (sc->sc_reply_freeq == NULL)
   2751   1.1    bouyer 		return (1);
   2752   1.1    bouyer 
   2753   1.1    bouyer 	kva = MPII_DMA_KVA(sc->sc_reply_freeq);
   2754   1.1    bouyer 	for (i = 0; i < sc->sc_num_reply_frames; i++) {
   2755   1.1    bouyer 		kva[i] = (u_int32_t)MPII_DMA_DVA(sc->sc_replies) +
   2756   1.1    bouyer 		    MPII_REPLY_SIZE * i;
   2757   1.1    bouyer 
   2758   1.1    bouyer 		DNPRINTF(MPII_D_MISC, "%s:   %d:  %p = 0x%08x\n",
   2759   1.1    bouyer 		    DEVNAME(sc), i,
   2760   1.1    bouyer 		    &kva[i], (u_int)MPII_DMA_DVA(sc->sc_replies) +
   2761   1.1    bouyer 		    MPII_REPLY_SIZE * i);
   2762   1.1    bouyer 	}
   2763   1.1    bouyer 
   2764   1.1    bouyer 	sc->sc_reply_postq =
   2765   1.1    bouyer 	    mpii_dmamem_alloc(sc, sc->sc_reply_post_qdepth * 8);
   2766   1.1    bouyer 	if (sc->sc_reply_postq == NULL)
   2767   1.1    bouyer 		goto free_reply_freeq;
   2768   1.1    bouyer 	sc->sc_reply_postq_kva = MPII_DMA_KVA(sc->sc_reply_postq);
   2769   1.1    bouyer 
   2770   1.1    bouyer 	DNPRINTF(MPII_D_MISC, "%s:  populating reply post descriptor queue\n",
   2771   1.1    bouyer 	    DEVNAME(sc));
   2772   1.1    bouyer 	kva64 = (u_int64_t *)MPII_DMA_KVA(sc->sc_reply_postq);
   2773   1.1    bouyer 	for (i = 0; i < sc->sc_reply_post_qdepth; i++) {
   2774   1.1    bouyer 		kva64[i] = 0xffffffffffffffffllu;
   2775   1.1    bouyer 		DNPRINTF(MPII_D_MISC, "%s:    %d:  %p = 0x%" PRIx64 "\n",
   2776   1.1    bouyer 		    DEVNAME(sc), i, &kva64[i], kva64[i]);
   2777   1.1    bouyer 	}
   2778   1.1    bouyer 
   2779   1.1    bouyer 	return (0);
   2780   1.1    bouyer 
   2781   1.1    bouyer free_reply_freeq:
   2782   1.1    bouyer 
   2783   1.1    bouyer 	mpii_dmamem_free(sc, sc->sc_reply_freeq);
   2784   1.1    bouyer 	return (1);
   2785   1.1    bouyer }
   2786   1.1    bouyer 
   2787   1.1    bouyer static void
   2788   1.1    bouyer mpii_init_queues(struct mpii_softc *sc)
   2789   1.1    bouyer {
   2790   1.1    bouyer 	DNPRINTF(MPII_D_MISC, "%s:  mpii_init_queues\n", DEVNAME(sc));
   2791   1.1    bouyer 
   2792   1.1    bouyer 	sc->sc_reply_free_host_index = sc->sc_reply_free_qdepth - 1;
   2793   1.1    bouyer 	sc->sc_reply_post_host_index = 0;
   2794   1.1    bouyer 	mpii_write_reply_free(sc, sc->sc_reply_free_host_index);
   2795   1.1    bouyer 	mpii_write_reply_post(sc, sc->sc_reply_post_host_index);
   2796   1.1    bouyer }
   2797   1.1    bouyer 
   2798   1.1    bouyer static void
   2799   1.1    bouyer mpii_wait(struct mpii_softc *sc, struct mpii_ccb *ccb)
   2800   1.1    bouyer {
   2801   1.1    bouyer 	struct mpii_ccb_wait	mpii_ccb_wait;
   2802   1.1    bouyer 	void			(*done)(struct mpii_ccb *);
   2803   1.1    bouyer 	void			*cookie;
   2804   1.1    bouyer 
   2805   1.1    bouyer 	done = ccb->ccb_done;
   2806   1.1    bouyer 	cookie = ccb->ccb_cookie;
   2807   1.1    bouyer 
   2808   1.1    bouyer 	ccb->ccb_done = mpii_wait_done;
   2809   1.1    bouyer 	ccb->ccb_cookie = &mpii_ccb_wait;
   2810   1.1    bouyer 
   2811   1.1    bouyer 	mutex_init(&mpii_ccb_wait.mpii_ccbw_mtx, MUTEX_DEFAULT, IPL_BIO);
   2812   1.1    bouyer 	cv_init(&mpii_ccb_wait.mpii_ccbw_cv, "mpii_wait");
   2813   1.1    bouyer 
   2814   1.1    bouyer 	/* XXX this will wait forever for the ccb to complete */
   2815   1.1    bouyer 
   2816   1.1    bouyer 	mpii_start(sc, ccb);
   2817   1.1    bouyer 
   2818   1.1    bouyer 	mutex_enter(&mpii_ccb_wait.mpii_ccbw_mtx);
   2819   1.1    bouyer 	while (ccb->ccb_cookie != NULL) {
   2820   1.1    bouyer 		cv_wait(&mpii_ccb_wait.mpii_ccbw_cv,
   2821   1.1    bouyer 		    &mpii_ccb_wait.mpii_ccbw_mtx);
   2822   1.1    bouyer 	}
   2823   1.1    bouyer 	mutex_exit(&mpii_ccb_wait.mpii_ccbw_mtx);
   2824   1.1    bouyer 	mutex_destroy(&mpii_ccb_wait.mpii_ccbw_mtx);
   2825   1.1    bouyer 	cv_destroy(&mpii_ccb_wait.mpii_ccbw_cv);
   2826   1.1    bouyer 
   2827   1.1    bouyer 	ccb->ccb_cookie = cookie;
   2828   1.1    bouyer 	done(ccb);
   2829   1.1    bouyer }
   2830   1.1    bouyer 
   2831   1.1    bouyer static void
   2832   1.1    bouyer mpii_wait_done(struct mpii_ccb *ccb)
   2833   1.1    bouyer {
   2834   1.1    bouyer 	struct mpii_ccb_wait	*mpii_ccb_waitp = ccb->ccb_cookie;
   2835   1.1    bouyer 
   2836   1.1    bouyer 	mutex_enter(&mpii_ccb_waitp->mpii_ccbw_mtx);
   2837   1.1    bouyer 	ccb->ccb_cookie = NULL;
   2838   1.1    bouyer 	cv_signal(&mpii_ccb_waitp->mpii_ccbw_cv);
   2839   1.1    bouyer 	mutex_exit(&mpii_ccb_waitp->mpii_ccbw_mtx);
   2840   1.1    bouyer }
   2841   1.1    bouyer 
   2842   1.1    bouyer static void
   2843   1.1    bouyer mpii_minphys(struct buf *bp)
   2844   1.1    bouyer {
   2845   1.1    bouyer 	DNPRINTF(MPII_D_MISC, "mpii_minphys: %d\n", bp->b_bcount);
   2846   1.1    bouyer 
   2847   1.1    bouyer 	/* XXX currently using MPII_MAXFER = MAXPHYS */
   2848   1.1    bouyer 	if (bp->b_bcount > MPII_MAXFER) {
   2849   1.1    bouyer 		bp->b_bcount = MPII_MAXFER;
   2850   1.1    bouyer 		minphys(bp);
   2851   1.1    bouyer 	}
   2852   1.1    bouyer }
   2853   1.1    bouyer 
   2854   1.1    bouyer static void
   2855   1.1    bouyer mpii_scsipi_request(struct scsipi_channel *chan, scsipi_adapter_req_t req,
   2856   1.1    bouyer     void *arg)
   2857   1.1    bouyer {
   2858   1.1    bouyer 	struct scsipi_periph	*periph;
   2859   1.1    bouyer 	struct scsipi_xfer	*xs;
   2860   1.1    bouyer 	struct scsipi_adapter	*adapt = chan->chan_adapter;
   2861   1.1    bouyer 	struct mpii_softc	*sc = device_private(adapt->adapt_dev);
   2862   1.1    bouyer 	struct mpii_ccb		*ccb;
   2863   1.1    bouyer 	struct mpii_ccb_bundle	*mcb;
   2864   1.1    bouyer 	struct mpii_msg_scsi_io	*io;
   2865   1.1    bouyer 	struct mpii_device	*dev;
   2866   1.1    bouyer 	int			target;
   2867   1.1    bouyer 	int timeout;
   2868   1.1    bouyer 
   2869   1.1    bouyer 	DNPRINTF(MPII_D_CMD, "%s: mpii_scsipi_request\n", DEVNAME(sc));
   2870   1.1    bouyer 	switch (req) {
   2871   1.1    bouyer 	case ADAPTER_REQ_GROW_RESOURCES:
   2872   1.1    bouyer 		/* Not supported. */
   2873   1.1    bouyer 		return;
   2874   1.1    bouyer 	case ADAPTER_REQ_SET_XFER_MODE:
   2875   1.1    bouyer 	{
   2876   1.1    bouyer 		struct scsipi_xfer_mode *xm = arg;
   2877   1.1    bouyer 		xm->xm_mode = PERIPH_CAP_TQING;
   2878   1.1    bouyer 		xm->xm_period = 0;
   2879   1.1    bouyer 		xm->xm_offset = 0;
   2880   1.1    bouyer 		scsipi_async_event(&sc->sc_chan, ASYNC_EVENT_XFER_MODE, xm);
   2881   1.1    bouyer 		return;
   2882   1.1    bouyer 	}
   2883   1.1    bouyer 	case ADAPTER_REQ_RUN_XFER:
   2884   1.1    bouyer 		break;
   2885   1.1    bouyer 	}
   2886   1.1    bouyer 
   2887   1.1    bouyer 	xs = arg;
   2888   1.1    bouyer 	periph = xs->xs_periph;
   2889   1.1    bouyer 	target = periph->periph_target;
   2890   1.1    bouyer 
   2891   1.1    bouyer 	if (xs->cmdlen > MPII_CDB_LEN) {
   2892   1.1    bouyer 		DNPRINTF(MPII_D_CMD, "%s: CBD too big %d\n",
   2893   1.1    bouyer 		    DEVNAME(sc), xs->cmdlen);
   2894   1.1    bouyer 		bzero(&xs->sense, sizeof(xs->sense));
   2895   1.1    bouyer 		xs->sense.scsi_sense.response_code =
   2896   1.1    bouyer 		    SSD_RCODE_VALID | SSD_RCODE_CURRENT;
   2897   1.1    bouyer 		xs->sense.scsi_sense.flags = SKEY_ILLEGAL_REQUEST;
   2898   1.1    bouyer 		xs->sense.scsi_sense.asc = 0x20;
   2899   1.1    bouyer 		xs->error = XS_SENSE;
   2900   1.1    bouyer 		scsipi_done(xs);
   2901   1.1    bouyer 		return;
   2902   1.1    bouyer 	}
   2903   1.1    bouyer 
   2904   1.1    bouyer 	if ((dev = sc->sc_devs[target]) == NULL) {
   2905   1.1    bouyer 		/* device no longer exists */
   2906   1.1    bouyer 		xs->error = XS_SELTIMEOUT;
   2907   1.1    bouyer 		scsipi_done(xs);
   2908   1.1    bouyer 		return;
   2909   1.1    bouyer 	}
   2910   1.1    bouyer 
   2911   1.1    bouyer 	ccb = mpii_get_ccb(sc, MPII_NOSLEEP);
   2912   1.1    bouyer 	if (ccb == NULL) {
   2913   1.1    bouyer 		xs->error = XS_RESOURCE_SHORTAGE;
   2914   1.1    bouyer 		scsipi_done(xs);
   2915   1.1    bouyer 		return;
   2916   1.1    bouyer 	}
   2917   1.1    bouyer 
   2918   1.1    bouyer 	DNPRINTF(MPII_D_CMD, "%s: ccb_smid: %d xs->xs_control: 0x%x\n",
   2919   1.1    bouyer 	    DEVNAME(sc), ccb->ccb_smid, xs->xs_control);
   2920   1.1    bouyer 
   2921   1.1    bouyer 	ccb->ccb_cookie = xs;
   2922   1.1    bouyer 	ccb->ccb_done = mpii_scsi_cmd_done;
   2923   1.1    bouyer 	ccb->ccb_dev_handle = dev->dev_handle;
   2924   1.1    bouyer 
   2925   1.1    bouyer 	mcb = ccb->ccb_cmd;
   2926   1.1    bouyer 	io = &mcb->mcb_io;
   2927   1.1    bouyer 
   2928   1.1    bouyer 	io->function = MPII_FUNCTION_SCSI_IO_REQUEST;
   2929   1.1    bouyer 	io->sense_buffer_length = sizeof(xs->sense);
   2930   1.1    bouyer 	io->sgl_offset0 = 24; /* XXX fix this */
   2931   1.1    bouyer 	io->io_flags = htole16(xs->cmdlen);
   2932   1.1    bouyer 	io->dev_handle = htole16(ccb->ccb_dev_handle);
   2933   1.1    bouyer 	io->lun[0] = htobe16(periph->periph_lun);
   2934   1.1    bouyer 
   2935   1.1    bouyer 	switch (xs->xs_control & (XS_CTL_DATA_IN | XS_CTL_DATA_OUT)) {
   2936   1.1    bouyer 	case XS_CTL_DATA_IN:
   2937   1.1    bouyer 		io->direction = MPII_SCSIIO_DIR_READ;
   2938   1.1    bouyer 		break;
   2939   1.1    bouyer 	case XS_CTL_DATA_OUT:
   2940   1.1    bouyer 		io->direction = MPII_SCSIIO_DIR_WRITE;
   2941   1.1    bouyer 		break;
   2942   1.1    bouyer 	default:
   2943   1.1    bouyer 		io->direction = MPII_SCSIIO_DIR_NONE;
   2944   1.1    bouyer 	}
   2945   1.1    bouyer 
   2946   1.1    bouyer 	io->tagging = MPII_SCSIIO_ATTR_SIMPLE_Q;
   2947   1.1    bouyer 
   2948   1.1    bouyer 	memcpy(io->cdb, xs->cmd, xs->cmdlen);
   2949   1.1    bouyer 
   2950   1.1    bouyer 	io->data_length = htole32(xs->datalen);
   2951   1.1    bouyer 
   2952   1.1    bouyer 	io->sense_buffer_low_address = htole32(ccb->ccb_cmd_dva +
   2953   1.1    bouyer 	    ((u_int8_t *)&mcb->mcb_sense - (u_int8_t *)mcb));
   2954   1.1    bouyer 
   2955   1.1    bouyer 	if (mpii_load_xs(ccb) != 0) {
   2956   1.1    bouyer 		xs->error = XS_DRIVER_STUFFUP;
   2957   1.1    bouyer 		mpii_put_ccb(sc, ccb);
   2958   1.1    bouyer 		scsipi_done(xs);
   2959   1.1    bouyer 		return;
   2960   1.1    bouyer 	}
   2961   1.1    bouyer 
   2962   1.1    bouyer 	DNPRINTF(MPII_D_CMD, "%s:  sizeof(mpii_msg_scsi_io): %ld "
   2963   1.1    bouyer 	    "sizeof(mpii_ccb_bundle): %ld sge offset: 0x%02lx\n",
   2964   1.1    bouyer 	    DEVNAME(sc), sizeof(struct mpii_msg_scsi_io),
   2965   1.1    bouyer 	    sizeof(struct mpii_ccb_bundle),
   2966   1.1    bouyer 	    (u_int8_t *)&mcb->mcb_sgl[0] - (u_int8_t *)mcb);
   2967   1.1    bouyer 
   2968   1.1    bouyer 	DNPRINTF(MPII_D_CMD, "%s   sgl[0]: 0x%04x 0%04x 0x%04x\n",
   2969   1.1    bouyer 	    DEVNAME(sc), mcb->mcb_sgl[0].sg_hdr, mcb->mcb_sgl[0].sg_lo_addr,
   2970   1.1    bouyer 	    mcb->mcb_sgl[0].sg_hi_addr);
   2971   1.1    bouyer 
   2972   1.1    bouyer 	DNPRINTF(MPII_D_CMD, "%s:  Offset0: 0x%02x\n", DEVNAME(sc),
   2973   1.1    bouyer 	    io->sgl_offset0);
   2974   1.1    bouyer 
   2975   1.1    bouyer 	if (xs->xs_control & XS_CTL_POLL) {
   2976   1.1    bouyer 		if (mpii_poll(sc, ccb) != 0) {
   2977   1.1    bouyer 			xs->error = XS_DRIVER_STUFFUP;
   2978   1.1    bouyer 			mpii_put_ccb(sc, ccb);
   2979   1.1    bouyer 			scsipi_done(xs);
   2980   1.1    bouyer 		}
   2981   1.1    bouyer 		return;
   2982   1.1    bouyer 	}
   2983   1.1    bouyer 	timeout = mstohz(xs->timeout);
   2984   1.1    bouyer 	if (timeout == 0)
   2985   1.1    bouyer 		timeout = 1;
   2986   1.1    bouyer 	callout_reset(&xs->xs_callout, timeout, mpii_scsi_cmd_tmo, ccb);
   2987   1.1    bouyer 
   2988   1.1    bouyer 	DNPRINTF(MPII_D_CMD, "%s:    mpii_scsipi_request(): opcode: %02x "
   2989   1.1    bouyer 	    "datalen: %d\n", DEVNAME(sc), xs->cmd->opcode, xs->datalen);
   2990   1.1    bouyer 
   2991   1.1    bouyer 	mpii_start(sc, ccb);
   2992   1.1    bouyer }
   2993   1.1    bouyer 
   2994   1.1    bouyer static void
   2995   1.1    bouyer mpii_scsi_cmd_tmo(void *xccb)
   2996   1.1    bouyer {
   2997   1.1    bouyer 	struct mpii_ccb		*ccb = xccb;
   2998   1.1    bouyer 	struct mpii_softc	*sc = ccb->ccb_sc;
   2999   1.1    bouyer 
   3000   1.1    bouyer 	printf("%s: mpii_scsi_cmd_tmo\n", DEVNAME(sc));
   3001   1.1    bouyer 
   3002   1.1    bouyer 	mutex_enter(&sc->sc_ccb_mtx);
   3003   1.1    bouyer 	if (ccb->ccb_state == MPII_CCB_QUEUED) {
   3004   1.1    bouyer 		ccb->ccb_state = MPII_CCB_TIMEOUT;
   3005   1.1    bouyer 		workqueue_enqueue(sc->sc_ssb_tmowk, &ccb->u.ccb_wk, NULL);
   3006   1.1    bouyer 	}
   3007   1.1    bouyer 	mutex_exit(&sc->sc_ccb_mtx);
   3008   1.1    bouyer }
   3009   1.1    bouyer 
   3010   1.1    bouyer static void
   3011   1.1    bouyer mpii_scsi_cmd_tmo_handler(struct work *wk, void *cookie)
   3012   1.1    bouyer {
   3013   1.1    bouyer 	struct mpii_softc			*sc = cookie;
   3014   1.1    bouyer 	struct mpii_ccb				*tccb;
   3015   1.1    bouyer 	struct mpii_ccb				*ccb;
   3016   1.1    bouyer 	struct mpii_msg_scsi_task_request	*stq;
   3017   1.1    bouyer 
   3018   1.1    bouyer 	ccb = (void *)wk;
   3019   1.1    bouyer 	tccb = mpii_get_ccb(sc, 0);
   3020   1.1    bouyer 
   3021   1.1    bouyer 	mutex_enter(&sc->sc_ccb_mtx);
   3022   1.1    bouyer 	if (ccb->ccb_state != MPII_CCB_TIMEOUT) {
   3023   1.1    bouyer 		mpii_put_ccb(sc, tccb);
   3024   1.1    bouyer 	}
   3025   1.1    bouyer 	/* should remove any other ccbs for the same dev handle */
   3026   1.1    bouyer 	mutex_exit(&sc->sc_ccb_mtx);
   3027   1.1    bouyer 
   3028   1.1    bouyer 	stq = tccb->ccb_cmd;
   3029   1.1    bouyer 	stq->function = MPII_FUNCTION_SCSI_TASK_MGMT;
   3030   1.1    bouyer 	stq->task_type = MPII_SCSI_TASK_TARGET_RESET;
   3031   1.1    bouyer 	stq->dev_handle = htole16(ccb->ccb_dev_handle);
   3032   1.1    bouyer 
   3033   1.1    bouyer 	tccb->ccb_done = mpii_scsi_cmd_tmo_done;
   3034   1.1    bouyer 	mpii_start(sc, tccb);
   3035   1.1    bouyer }
   3036   1.1    bouyer 
   3037   1.1    bouyer static void
   3038   1.1    bouyer mpii_scsi_cmd_tmo_done(struct mpii_ccb *tccb)
   3039   1.1    bouyer {
   3040   1.1    bouyer         mpii_put_ccb(tccb->ccb_sc, tccb);
   3041   1.1    bouyer }
   3042   1.1    bouyer 
   3043   1.3    kardel static u_int8_t
   3044   1.3    kardel map_scsi_status(u_int8_t mpii_scsi_status)
   3045   1.3    kardel {
   3046   1.3    kardel 	u_int8_t scsi_status;
   3047   1.3    kardel 
   3048   1.3    kardel 	switch (mpii_scsi_status)
   3049   1.3    kardel 	{
   3050   1.3    kardel 	case MPII_SCSIIO_ERR_STATUS_SUCCESS:
   3051   1.3    kardel 		scsi_status = SCSI_OK;
   3052   1.3    kardel 		break;
   3053   1.3    kardel 
   3054   1.3    kardel 	case MPII_SCSIIO_ERR_STATUS_CHECK_COND:
   3055   1.3    kardel 		scsi_status = SCSI_CHECK;
   3056   1.3    kardel 		break;
   3057   1.3    kardel 
   3058   1.3    kardel 	case MPII_SCSIIO_ERR_STATUS_BUSY:
   3059   1.3    kardel 		scsi_status = SCSI_BUSY;
   3060   1.3    kardel 		break;
   3061   1.3    kardel 
   3062   1.3    kardel 	case MPII_SCSIIO_ERR_STATUS_INTERMEDIATE:
   3063   1.3    kardel 		scsi_status = SCSI_INTERM;
   3064   1.3    kardel 		break;
   3065   1.3    kardel 
   3066   1.3    kardel 	case MPII_SCSIIO_ERR_STATUS_INTERMEDIATE_CONDMET:
   3067   1.3    kardel 		scsi_status = SCSI_INTERM;
   3068   1.3    kardel 		break;
   3069   1.3    kardel 
   3070   1.3    kardel 	case MPII_SCSIIO_ERR_STATUS_RESERVATION_CONFLICT:
   3071   1.3    kardel 		scsi_status = SCSI_RESV_CONFLICT;
   3072   1.3    kardel 		break;
   3073   1.3    kardel 
   3074   1.3    kardel 	case MPII_SCSIIO_ERR_STATUS_CMD_TERM:
   3075   1.3    kardel 	case MPII_SCSIIO_ERR_STATUS_TASK_ABORTED:
   3076   1.3    kardel 		scsi_status = SCSI_TERMINATED;
   3077   1.3    kardel 		break;
   3078   1.3    kardel 
   3079   1.3    kardel 	case MPII_SCSIIO_ERR_STATUS_TASK_SET_FULL:
   3080   1.3    kardel 		scsi_status = SCSI_QUEUE_FULL;
   3081   1.3    kardel 		break;
   3082   1.3    kardel 
   3083   1.3    kardel 	case MPII_SCSIIO_ERR_STATUS_ACA_ACTIVE:
   3084   1.3    kardel 		scsi_status = SCSI_ACA_ACTIVE;
   3085   1.3    kardel 		break;
   3086   1.3    kardel 
   3087   1.3    kardel 	default:
   3088   1.3    kardel 		/* XXX: for the lack of anything better and other than OK */
   3089   1.3    kardel 		scsi_status = 0xFF;
   3090   1.3    kardel 		break;
   3091   1.3    kardel 	}
   3092   1.3    kardel 
   3093   1.3    kardel 	return scsi_status;
   3094   1.3    kardel }
   3095   1.1    bouyer 
   3096   1.1    bouyer static void
   3097   1.1    bouyer mpii_scsi_cmd_done(struct mpii_ccb *ccb)
   3098   1.1    bouyer {
   3099   1.1    bouyer 	struct mpii_msg_scsi_io_error	*sie;
   3100   1.1    bouyer 	struct mpii_softc	*sc = ccb->ccb_sc;
   3101   1.1    bouyer 	struct scsipi_xfer	*xs = ccb->ccb_cookie;
   3102   1.1    bouyer 	struct mpii_ccb_bundle	*mcb = ccb->ccb_cmd;
   3103   1.1    bouyer 	bus_dmamap_t		dmap = ccb->ccb_dmamap;
   3104   1.1    bouyer 	bool			timeout = 0;
   3105   1.1    bouyer 
   3106   1.1    bouyer 	callout_stop(&xs->xs_callout);
   3107   1.1    bouyer 	mutex_enter(&sc->sc_ccb_mtx);
   3108   1.1    bouyer 	if (ccb->ccb_state == MPII_CCB_TIMEOUT)
   3109   1.1    bouyer 		timeout = 1;
   3110   1.1    bouyer 	ccb->ccb_state = MPII_CCB_READY;
   3111   1.1    bouyer 	mutex_exit(&sc->sc_ccb_mtx);
   3112   1.1    bouyer 
   3113   1.1    bouyer 	if (xs->datalen != 0) {
   3114   1.1    bouyer 		bus_dmamap_sync(sc->sc_dmat, dmap, 0, dmap->dm_mapsize,
   3115   1.1    bouyer 		    (xs->xs_control & XS_CTL_DATA_IN) ? BUS_DMASYNC_POSTREAD :
   3116   1.1    bouyer 		    BUS_DMASYNC_POSTWRITE);
   3117   1.1    bouyer 
   3118   1.1    bouyer 		bus_dmamap_unload(sc->sc_dmat, dmap);
   3119   1.1    bouyer 	}
   3120   1.1    bouyer 
   3121   1.1    bouyer 	xs->error = XS_NOERROR;
   3122   1.1    bouyer 	xs->resid = 0;
   3123   1.1    bouyer 
   3124   1.1    bouyer 	if (ccb->ccb_rcb == NULL) {
   3125   1.1    bouyer 		/* no scsi error, we're ok so drop out early */
   3126   1.1    bouyer 		xs->status = SCSI_OK;
   3127   1.1    bouyer 		mpii_put_ccb(sc, ccb);
   3128   1.1    bouyer 		scsipi_done(xs);
   3129   1.1    bouyer 		return;
   3130   1.1    bouyer 	}
   3131   1.1    bouyer 
   3132   1.1    bouyer 	sie = ccb->ccb_rcb->rcb_reply;
   3133   1.1    bouyer 
   3134   1.1    bouyer 	DNPRINTF(MPII_D_CMD, "%s: mpii_scsi_cmd_done xs cmd: 0x%02x len: %d "
   3135   1.1    bouyer 	    "xs_control 0x%x\n", DEVNAME(sc), xs->cmd->opcode, xs->datalen,
   3136   1.1    bouyer 	    xs->xs_control);
   3137   1.1    bouyer 	DNPRINTF(MPII_D_CMD, "%s:  dev_handle: %d msg_length: %d "
   3138   1.1    bouyer 	    "function: 0x%02x\n", DEVNAME(sc), le16toh(sie->dev_handle),
   3139   1.1    bouyer 	    sie->msg_length, sie->function);
   3140   1.1    bouyer 	DNPRINTF(MPII_D_CMD, "%s:  vp_id: 0x%02x vf_id: 0x%02x\n", DEVNAME(sc),
   3141   1.1    bouyer 	    sie->vp_id, sie->vf_id);
   3142   1.1    bouyer 	DNPRINTF(MPII_D_CMD, "%s:  scsi_status: 0x%02x scsi_state: 0x%02x "
   3143   1.1    bouyer 	    "ioc_status: 0x%04x\n", DEVNAME(sc), sie->scsi_status,
   3144   1.1    bouyer 	    sie->scsi_state, le16toh(sie->ioc_status));
   3145   1.1    bouyer 	DNPRINTF(MPII_D_CMD, "%s:  ioc_loginfo: 0x%08x\n", DEVNAME(sc),
   3146   1.1    bouyer 	    le32toh(sie->ioc_loginfo));
   3147   1.1    bouyer 	DNPRINTF(MPII_D_CMD, "%s:  transfer_count: %d\n", DEVNAME(sc),
   3148   1.1    bouyer 	    le32toh(sie->transfer_count));
   3149   1.1    bouyer 	DNPRINTF(MPII_D_CMD, "%s:  sense_count: %d\n", DEVNAME(sc),
   3150   1.1    bouyer 	    le32toh(sie->sense_count));
   3151   1.1    bouyer 	DNPRINTF(MPII_D_CMD, "%s:  response_info: 0x%08x\n", DEVNAME(sc),
   3152   1.1    bouyer 	    le32toh(sie->response_info));
   3153   1.1    bouyer 	DNPRINTF(MPII_D_CMD, "%s:  task_tag: 0x%04x\n", DEVNAME(sc),
   3154   1.1    bouyer 	    le16toh(sie->task_tag));
   3155   1.1    bouyer 	DNPRINTF(MPII_D_CMD, "%s:  bidirectional_transfer_count: 0x%08x\n",
   3156   1.1    bouyer 	    DEVNAME(sc), le32toh(sie->bidirectional_transfer_count));
   3157   1.1    bouyer 
   3158   1.3    kardel 	xs->status = map_scsi_status(sie->scsi_status);
   3159   1.3    kardel 
   3160   1.1    bouyer 	switch (le16toh(sie->ioc_status) & MPII_IOCSTATUS_MASK) {
   3161   1.1    bouyer 	case MPII_IOCSTATUS_SCSI_DATA_UNDERRUN:
   3162   1.3    kardel 		switch (sie->scsi_status) {
   3163   1.3    kardel 		case MPII_SCSIIO_ERR_STATUS_CHECK_COND:
   3164   1.3    kardel 			xs->error = XS_SENSE;
   3165   1.3    kardel 			/*FALLTHROUGH*/
   3166   1.3    kardel 		case MPII_SCSIIO_ERR_STATUS_SUCCESS:
   3167   1.1    bouyer 			xs->resid = xs->datalen - le32toh(sie->transfer_count);
   3168   1.1    bouyer 			break;
   3169   1.3    kardel 
   3170   1.1    bouyer 		default:
   3171   1.1    bouyer 			xs->error = XS_DRIVER_STUFFUP;
   3172   1.1    bouyer 			break;
   3173   1.1    bouyer 		}
   3174   1.1    bouyer 		break;
   3175   1.3    kardel 
   3176   1.1    bouyer 	case MPII_IOCSTATUS_SUCCESS:
   3177   1.1    bouyer 	case MPII_IOCSTATUS_SCSI_RECOVERED_ERROR:
   3178   1.3    kardel 		switch (sie->scsi_status) {
   3179   1.3    kardel 		case MPII_SCSIIO_ERR_STATUS_SUCCESS:
   3180   1.3    kardel 			/*
   3181   1.3    kardel 			 * xs->resid = 0; - already set above
   3182   1.3    kardel 			 *
   3183   1.3    kardel 			 * XXX: check whether UNDERUN strategy
   3184   1.3    kardel 			 * would be appropriate here too.
   3185   1.3    kardel 			 * that would allow joining these cases.
   3186   1.3    kardel 			 */
   3187   1.1    bouyer 			break;
   3188   1.1    bouyer 
   3189   1.3    kardel 		case MPII_SCSIIO_ERR_STATUS_CHECK_COND:
   3190   1.1    bouyer 			xs->error = XS_SENSE;
   3191   1.1    bouyer 			break;
   3192   1.3    kardel 
   3193   1.3    kardel 		case MPII_SCSIIO_ERR_STATUS_BUSY:
   3194   1.3    kardel 		case MPII_SCSIIO_ERR_STATUS_TASK_SET_FULL:
   3195   1.1    bouyer 			xs->error = XS_BUSY;
   3196   1.1    bouyer 			break;
   3197   1.1    bouyer 
   3198   1.1    bouyer 		default:
   3199   1.1    bouyer 			xs->error = XS_DRIVER_STUFFUP;
   3200   1.1    bouyer 		}
   3201   1.1    bouyer 		break;
   3202   1.1    bouyer 
   3203   1.1    bouyer 	case MPII_IOCSTATUS_BUSY:
   3204   1.1    bouyer 	case MPII_IOCSTATUS_INSUFFICIENT_RESOURCES:
   3205   1.1    bouyer 		xs->error = XS_BUSY;
   3206   1.1    bouyer 		break;
   3207   1.1    bouyer 
   3208   1.1    bouyer 	case MPII_IOCSTATUS_SCSI_IOC_TERMINATED:
   3209   1.1    bouyer 	case MPII_IOCSTATUS_SCSI_TASK_TERMINATED:
   3210   1.1    bouyer 		xs->error = timeout ? XS_TIMEOUT : XS_RESET;
   3211   1.1    bouyer 		break;
   3212   1.1    bouyer 
   3213   1.1    bouyer 	case MPII_IOCSTATUS_SCSI_INVALID_DEVHANDLE:
   3214   1.1    bouyer 	case MPII_IOCSTATUS_SCSI_DEVICE_NOT_THERE:
   3215   1.1    bouyer 		xs->error = XS_SELTIMEOUT;
   3216   1.1    bouyer 		break;
   3217   1.1    bouyer 
   3218   1.1    bouyer 	default:
   3219   1.1    bouyer 		xs->error = XS_DRIVER_STUFFUP;
   3220   1.1    bouyer 		break;
   3221   1.1    bouyer 	}
   3222   1.1    bouyer 
   3223   1.1    bouyer 	if (sie->scsi_state & MPII_SCSIIO_ERR_STATE_AUTOSENSE_VALID)
   3224   1.1    bouyer 		memcpy(&xs->sense, &mcb->mcb_sense, sizeof(xs->sense));
   3225   1.1    bouyer 
   3226   1.1    bouyer 	DNPRINTF(MPII_D_CMD, "%s:  xs err: %d status: %#x\n", DEVNAME(sc),
   3227   1.1    bouyer 	    xs->error, xs->status);
   3228   1.1    bouyer 
   3229   1.1    bouyer 	mpii_push_reply(sc, ccb->ccb_rcb);
   3230   1.1    bouyer 	mpii_put_ccb(sc, ccb);
   3231   1.1    bouyer 	scsipi_done(xs);
   3232   1.1    bouyer }
   3233   1.1    bouyer 
   3234   1.1    bouyer #if 0
   3235   1.1    bouyer static int
   3236   1.1    bouyer mpii_ioctl_cache(struct scsi_link *link, u_long cmd, struct dk_cache *dc)
   3237   1.1    bouyer {
   3238   1.1    bouyer 	struct mpii_softc *sc = (struct mpii_softc *)link->adapter_softc;
   3239   1.1    bouyer 	struct mpii_device *dev = sc->sc_devs[link->target];
   3240   1.1    bouyer 	struct mpii_cfg_raid_vol_pg0 *vpg;
   3241   1.1    bouyer 	struct mpii_msg_raid_action_request *req;
   3242   1.1    bouyer  	struct mpii_msg_raid_action_reply *rep;
   3243   1.1    bouyer 	struct mpii_cfg_hdr hdr;
   3244   1.1    bouyer 	struct mpii_ccb	*ccb;
   3245   1.1    bouyer 	u_int32_t addr = MPII_CFG_RAID_VOL_ADDR_HANDLE | dev->dev_handle;
   3246   1.1    bouyer 	size_t pagelen;
   3247   1.1    bouyer 	int rv = 0;
   3248   1.1    bouyer 	int enabled;
   3249   1.1    bouyer 
   3250   1.1    bouyer 	if (mpii_req_cfg_header(sc, MPII_CONFIG_REQ_PAGE_TYPE_RAID_VOL, 0,
   3251   1.1    bouyer 	    addr, MPII_PG_POLL, &hdr) != 0)
   3252   1.1    bouyer 		return (EINVAL);
   3253   1.1    bouyer 
   3254   1.1    bouyer 	pagelen = hdr.page_length * 4;
   3255  1.12  jdolecek 	vpg = malloc(pagelen, M_TEMP, M_WAITOK | M_ZERO);
   3256   1.1    bouyer 	if (vpg == NULL)
   3257   1.1    bouyer 		return (ENOMEM);
   3258   1.1    bouyer 
   3259   1.1    bouyer 	if (mpii_req_cfg_page(sc, addr, MPII_PG_POLL, &hdr, 1,
   3260   1.1    bouyer 	    vpg, pagelen) != 0) {
   3261   1.1    bouyer 		rv = EINVAL;
   3262   1.1    bouyer 		goto done;
   3263   1.1    bouyer 	}
   3264   1.1    bouyer 
   3265   1.1    bouyer 	enabled = ((le16toh(vpg->volume_settings) &
   3266   1.1    bouyer 	    MPII_CFG_RAID_VOL_0_SETTINGS_CACHE_MASK) ==
   3267   1.1    bouyer 	    MPII_CFG_RAID_VOL_0_SETTINGS_CACHE_ENABLED) ? 1 : 0;
   3268   1.1    bouyer 
   3269   1.1    bouyer 	if (cmd == DIOCGCACHE) {
   3270   1.1    bouyer 		dc->wrcache = enabled;
   3271   1.1    bouyer 		dc->rdcache = 0;
   3272   1.1    bouyer 		goto done;
   3273   1.1    bouyer 	} /* else DIOCSCACHE */
   3274   1.1    bouyer 
   3275   1.1    bouyer 	if (dc->rdcache) {
   3276   1.1    bouyer 		rv = EOPNOTSUPP;
   3277   1.1    bouyer 		goto done;
   3278   1.1    bouyer 	}
   3279   1.1    bouyer 
   3280   1.1    bouyer 	if (((dc->wrcache) ? 1 : 0) == enabled)
   3281   1.1    bouyer 		goto done;
   3282   1.1    bouyer 
   3283   1.1    bouyer 	ccb = mpii_get_ccb(sc, MPII_NOSLEEP);
   3284   1.1    bouyer 	if (ccb == NULL) {
   3285   1.1    bouyer 		rv = ENOMEM;
   3286   1.1    bouyer 		goto done;
   3287   1.1    bouyer 	}
   3288   1.1    bouyer 
   3289   1.1    bouyer 	ccb->ccb_done = mpii_empty_done;
   3290   1.1    bouyer 
   3291   1.1    bouyer 	req = ccb->ccb_cmd;
   3292   1.1    bouyer 	bzero(req, sizeof(*req));
   3293   1.1    bouyer 	req->function = MPII_FUNCTION_RAID_ACTION;
   3294   1.1    bouyer 	req->action = MPII_RAID_ACTION_CHANGE_VOL_WRITE_CACHE;
   3295   1.1    bouyer 	req->vol_dev_handle = htole16(dev->dev_handle);
   3296   1.1    bouyer 	req->action_data = htole32(dc->wrcache ?
   3297   1.1    bouyer 	    MPII_RAID_VOL_WRITE_CACHE_ENABLE :
   3298   1.1    bouyer 	    MPII_RAID_VOL_WRITE_CACHE_DISABLE);
   3299   1.1    bouyer 
   3300   1.1    bouyer 	if (mpii_poll(sc, ccb) != 0) {
   3301   1.1    bouyer 		rv = EIO;
   3302   1.1    bouyer 		goto done;
   3303   1.1    bouyer 	}
   3304   1.1    bouyer 
   3305   1.1    bouyer 	if (ccb->ccb_rcb != NULL) {
   3306   1.1    bouyer 		rep = ccb->ccb_rcb->rcb_reply;
   3307   1.1    bouyer 		if ((rep->ioc_status != MPII_IOCSTATUS_SUCCESS) ||
   3308   1.1    bouyer 		    ((rep->action_data[0] &
   3309   1.1    bouyer 		     MPII_RAID_VOL_WRITE_CACHE_MASK) !=
   3310   1.1    bouyer 		    (dc->wrcache ? MPII_RAID_VOL_WRITE_CACHE_ENABLE :
   3311   1.1    bouyer 		     MPII_RAID_VOL_WRITE_CACHE_DISABLE)))
   3312   1.1    bouyer 			rv = EINVAL;
   3313   1.1    bouyer 		mpii_push_reply(sc, ccb->ccb_rcb);
   3314   1.1    bouyer 	}
   3315   1.1    bouyer 
   3316   1.1    bouyer 	mpii_put_ccb(sc, ccb);
   3317   1.1    bouyer 
   3318   1.1    bouyer done:
   3319   1.1    bouyer 	free(vpg, M_TEMP);
   3320   1.1    bouyer 	return (rv);
   3321   1.1    bouyer }
   3322   1.1    bouyer #endif
   3323   1.1    bouyer static int
   3324   1.1    bouyer mpii_cache_enable(struct mpii_softc *sc, struct mpii_device *dev)
   3325   1.1    bouyer {
   3326   1.1    bouyer 	struct mpii_cfg_raid_vol_pg0 *vpg;
   3327   1.1    bouyer 	struct mpii_msg_raid_action_request *req;
   3328   1.1    bouyer  	struct mpii_msg_raid_action_reply *rep;
   3329   1.1    bouyer 	struct mpii_cfg_hdr hdr;
   3330   1.1    bouyer 	struct mpii_ccb	*ccb;
   3331   1.1    bouyer 	u_int32_t addr = MPII_CFG_RAID_VOL_ADDR_HANDLE | dev->dev_handle;
   3332   1.1    bouyer 	size_t pagelen;
   3333   1.1    bouyer 	int rv = 0;
   3334   1.1    bouyer 	int enabled;
   3335   1.1    bouyer 
   3336   1.1    bouyer 	if (mpii_req_cfg_header(sc, MPII_CONFIG_REQ_PAGE_TYPE_RAID_VOL, 0,
   3337   1.1    bouyer 	    addr, MPII_PG_POLL, &hdr) != 0)
   3338   1.1    bouyer 		return (EINVAL);
   3339   1.1    bouyer 
   3340   1.1    bouyer 	pagelen = hdr.page_length * 4;
   3341   1.9  christos 	vpg = malloc(pagelen, M_TEMP, M_NOWAIT | M_ZERO);
   3342   1.1    bouyer 	if (vpg == NULL)
   3343   1.1    bouyer 		return (ENOMEM);
   3344   1.1    bouyer 
   3345   1.1    bouyer 	if (mpii_req_cfg_page(sc, addr, MPII_PG_POLL, &hdr, 1,
   3346   1.1    bouyer 	    vpg, pagelen) != 0) {
   3347   1.1    bouyer 		rv = EINVAL;
   3348   1.1    bouyer 		goto done;
   3349   1.1    bouyer 		free(vpg, M_TEMP);
   3350   1.1    bouyer 		return (EINVAL);
   3351   1.1    bouyer 	}
   3352   1.1    bouyer 
   3353   1.1    bouyer 	enabled = ((le16toh(vpg->volume_settings) &
   3354   1.1    bouyer 	    MPII_CFG_RAID_VOL_0_SETTINGS_CACHE_MASK) ==
   3355   1.1    bouyer 	    MPII_CFG_RAID_VOL_0_SETTINGS_CACHE_ENABLED) ? 1 : 0;
   3356   1.1    bouyer 	aprint_normal_dev(sc->sc_dev, "target %d cache %s", dev->slot,
   3357   1.1    bouyer 	    enabled ? "enabled" : "disabled, enabling");
   3358   1.1    bouyer 	aprint_normal("\n");
   3359   1.1    bouyer 
   3360   1.1    bouyer 	if (enabled == 0)
   3361   1.1    bouyer 		goto done;
   3362   1.1    bouyer 
   3363   1.1    bouyer 	ccb = mpii_get_ccb(sc, MPII_NOSLEEP);
   3364   1.1    bouyer 	if (ccb == NULL) {
   3365   1.1    bouyer 		rv = ENOMEM;
   3366   1.1    bouyer 		goto done;
   3367   1.1    bouyer 	}
   3368   1.1    bouyer 
   3369   1.1    bouyer 	ccb->ccb_done = mpii_empty_done;
   3370   1.1    bouyer 
   3371   1.1    bouyer 	req = ccb->ccb_cmd;
   3372   1.1    bouyer 	bzero(req, sizeof(*req));
   3373   1.1    bouyer 	req->function = MPII_FUNCTION_RAID_ACTION;
   3374   1.1    bouyer 	req->action = MPII_RAID_ACTION_CHANGE_VOL_WRITE_CACHE;
   3375   1.1    bouyer 	req->vol_dev_handle = htole16(dev->dev_handle);
   3376   1.1    bouyer 	req->action_data = htole32(
   3377   1.1    bouyer 	    MPII_RAID_VOL_WRITE_CACHE_ENABLE);
   3378   1.1    bouyer 
   3379   1.1    bouyer 	if (mpii_poll(sc, ccb) != 0) {
   3380   1.1    bouyer 		rv = EIO;
   3381   1.1    bouyer 		goto done;
   3382   1.1    bouyer 	}
   3383   1.1    bouyer 
   3384   1.1    bouyer 	if (ccb->ccb_rcb != NULL) {
   3385   1.1    bouyer 		rep = ccb->ccb_rcb->rcb_reply;
   3386   1.1    bouyer 		if ((rep->ioc_status != MPII_IOCSTATUS_SUCCESS) ||
   3387   1.1    bouyer 		    ((rep->action_data[0] &
   3388   1.1    bouyer 		     MPII_RAID_VOL_WRITE_CACHE_MASK) !=
   3389   1.1    bouyer 		     MPII_RAID_VOL_WRITE_CACHE_ENABLE))
   3390   1.1    bouyer 			rv = EINVAL;
   3391   1.1    bouyer 		mpii_push_reply(sc, ccb->ccb_rcb);
   3392   1.1    bouyer 	}
   3393   1.1    bouyer 
   3394   1.1    bouyer 	mpii_put_ccb(sc, ccb);
   3395   1.1    bouyer 
   3396   1.1    bouyer done:
   3397   1.1    bouyer 	free(vpg, M_TEMP);
   3398   1.1    bouyer 	if (rv) {
   3399   1.1    bouyer 		aprint_error_dev(sc->sc_dev,
   3400   1.1    bouyer 		    "enabling cache on target %d failed (%d)\n",
   3401   1.1    bouyer 		    dev->slot, rv);
   3402   1.1    bouyer 	}
   3403   1.1    bouyer 	return (rv);
   3404   1.1    bouyer }
   3405   1.1    bouyer 
   3406   1.1    bouyer #if NBIO > 0
   3407   1.1    bouyer static int
   3408   1.1    bouyer mpii_ioctl(device_t dev, u_long cmd, void *addr)
   3409   1.1    bouyer {
   3410   1.1    bouyer 	struct mpii_softc	*sc = device_private(dev);
   3411   1.1    bouyer 	int			s, error = 0;
   3412   1.1    bouyer 
   3413   1.1    bouyer 	DNPRINTF(MPII_D_IOCTL, "%s: mpii_ioctl ", DEVNAME(sc));
   3414   1.1    bouyer 	KERNEL_LOCK(1, curlwp);
   3415   1.1    bouyer 	s = splbio();
   3416   1.1    bouyer 
   3417   1.1    bouyer 	switch (cmd) {
   3418   1.1    bouyer 	case BIOCINQ:
   3419   1.1    bouyer 		DNPRINTF(MPII_D_IOCTL, "inq\n");
   3420   1.1    bouyer 		error = mpii_ioctl_inq(sc, (struct bioc_inq *)addr);
   3421   1.1    bouyer 		break;
   3422   1.1    bouyer 	case BIOCVOL:
   3423   1.1    bouyer 		DNPRINTF(MPII_D_IOCTL, "vol\n");
   3424   1.1    bouyer 		error = mpii_ioctl_vol(sc, (struct bioc_vol *)addr);
   3425   1.1    bouyer 		break;
   3426   1.1    bouyer 	case BIOCDISK:
   3427   1.1    bouyer 		DNPRINTF(MPII_D_IOCTL, "disk\n");
   3428   1.1    bouyer 		error = mpii_ioctl_disk(sc, (struct bioc_disk *)addr);
   3429   1.1    bouyer 		break;
   3430   1.1    bouyer 	default:
   3431   1.1    bouyer 		DNPRINTF(MPII_D_IOCTL, " invalid ioctl\n");
   3432   1.1    bouyer 		error = EINVAL;
   3433   1.1    bouyer 	}
   3434   1.1    bouyer 
   3435   1.1    bouyer 	splx(s);
   3436   1.1    bouyer 	KERNEL_UNLOCK_ONE(curlwp);
   3437   1.1    bouyer 	return (error);
   3438   1.1    bouyer }
   3439   1.1    bouyer 
   3440   1.1    bouyer static int
   3441   1.1    bouyer mpii_ioctl_inq(struct mpii_softc *sc, struct bioc_inq *bi)
   3442   1.1    bouyer {
   3443   1.1    bouyer 	int			i;
   3444   1.1    bouyer 
   3445   1.1    bouyer 	DNPRINTF(MPII_D_IOCTL, "%s: mpii_ioctl_inq\n", DEVNAME(sc));
   3446   1.1    bouyer 
   3447   1.1    bouyer 	strlcpy(bi->bi_dev, DEVNAME(sc), sizeof(bi->bi_dev));
   3448   1.1    bouyer 	for (i = 0; i < sc->sc_max_devices; i++)
   3449   1.1    bouyer 		if (sc->sc_devs[i] &&
   3450   1.1    bouyer 		    ISSET(sc->sc_devs[i]->flags, MPII_DF_VOLUME))
   3451   1.1    bouyer 			bi->bi_novol++;
   3452   1.1    bouyer 	return (0);
   3453   1.1    bouyer }
   3454   1.1    bouyer 
   3455   1.1    bouyer static int
   3456   1.1    bouyer mpii_ioctl_vol(struct mpii_softc *sc, struct bioc_vol *bv)
   3457   1.1    bouyer {
   3458   1.1    bouyer 	struct mpii_cfg_raid_vol_pg0	*vpg;
   3459   1.1    bouyer 	struct mpii_cfg_hdr		hdr;
   3460   1.1    bouyer 	struct mpii_device		*dev;
   3461   1.1    bouyer 	struct scsipi_periph 		*periph;
   3462   1.1    bouyer 	size_t				pagelen;
   3463   1.1    bouyer 	u_int16_t			volh;
   3464   1.1    bouyer 	int				rv, hcnt = 0;
   3465   1.1    bouyer 
   3466   1.1    bouyer 	DNPRINTF(MPII_D_IOCTL, "%s: mpii_ioctl_vol %d\n",
   3467   1.1    bouyer 	    DEVNAME(sc), bv->bv_volid);
   3468   1.1    bouyer 
   3469   1.1    bouyer 	if ((dev = mpii_find_vol(sc, bv->bv_volid)) == NULL)
   3470   1.1    bouyer 		return (ENODEV);
   3471   1.1    bouyer 	volh = dev->dev_handle;
   3472   1.1    bouyer 
   3473   1.1    bouyer 	if (mpii_req_cfg_header(sc, MPII_CONFIG_REQ_PAGE_TYPE_RAID_VOL, 0,
   3474   1.1    bouyer 	    MPII_CFG_RAID_VOL_ADDR_HANDLE | volh, 0, &hdr) != 0) {
   3475   1.1    bouyer 		printf("%s: unable to fetch header for raid volume page 0\n",
   3476   1.1    bouyer 		    DEVNAME(sc));
   3477   1.1    bouyer 		return (EINVAL);
   3478   1.1    bouyer 	}
   3479   1.1    bouyer 
   3480   1.1    bouyer 	pagelen = hdr.page_length * 4;
   3481  1.12  jdolecek 	vpg = malloc(pagelen, M_TEMP, M_WAITOK | M_ZERO);
   3482   1.1    bouyer 	if (vpg == NULL) {
   3483   1.1    bouyer 		printf("%s: unable to allocate space for raid "
   3484   1.1    bouyer 		    "volume page 0\n", DEVNAME(sc));
   3485   1.1    bouyer 		return (ENOMEM);
   3486   1.1    bouyer 	}
   3487   1.1    bouyer 
   3488   1.1    bouyer 	if (mpii_req_cfg_page(sc, MPII_CFG_RAID_VOL_ADDR_HANDLE | volh, 0,
   3489   1.1    bouyer 	    &hdr, 1, vpg, pagelen) != 0) {
   3490   1.1    bouyer 		printf("%s: unable to fetch raid volume page 0\n",
   3491   1.1    bouyer 		    DEVNAME(sc));
   3492   1.1    bouyer 		free(vpg, M_TEMP);
   3493   1.1    bouyer 		return (EINVAL);
   3494   1.1    bouyer 	}
   3495   1.1    bouyer 
   3496   1.1    bouyer 	switch (vpg->volume_state) {
   3497   1.1    bouyer 	case MPII_CFG_RAID_VOL_0_STATE_ONLINE:
   3498   1.1    bouyer 	case MPII_CFG_RAID_VOL_0_STATE_OPTIMAL:
   3499   1.1    bouyer 		bv->bv_status = BIOC_SVONLINE;
   3500   1.1    bouyer 		break;
   3501   1.1    bouyer 	case MPII_CFG_RAID_VOL_0_STATE_DEGRADED:
   3502   1.1    bouyer 		if (ISSET(le32toh(vpg->volume_status),
   3503   1.1    bouyer 		    MPII_CFG_RAID_VOL_0_STATUS_RESYNC)) {
   3504   1.1    bouyer 			bv->bv_status = BIOC_SVREBUILD;
   3505   1.1    bouyer 			bv->bv_percent = dev->percent;
   3506   1.1    bouyer 		} else
   3507   1.1    bouyer 			bv->bv_status = BIOC_SVDEGRADED;
   3508   1.1    bouyer 		break;
   3509   1.1    bouyer 	case MPII_CFG_RAID_VOL_0_STATE_FAILED:
   3510   1.1    bouyer 		bv->bv_status = BIOC_SVOFFLINE;
   3511   1.1    bouyer 		break;
   3512   1.1    bouyer 	case MPII_CFG_RAID_VOL_0_STATE_INITIALIZING:
   3513   1.1    bouyer 		bv->bv_status = BIOC_SVBUILDING;
   3514   1.1    bouyer 		break;
   3515   1.1    bouyer 	case MPII_CFG_RAID_VOL_0_STATE_MISSING:
   3516   1.1    bouyer 	default:
   3517   1.1    bouyer 		bv->bv_status = BIOC_SVINVALID;
   3518   1.1    bouyer 		break;
   3519   1.1    bouyer 	}
   3520   1.1    bouyer 
   3521   1.1    bouyer 	switch (vpg->volume_type) {
   3522   1.1    bouyer 	case MPII_CFG_RAID_VOL_0_TYPE_RAID0:
   3523   1.1    bouyer 		bv->bv_level = 0;
   3524   1.1    bouyer 		break;
   3525   1.1    bouyer 	case MPII_CFG_RAID_VOL_0_TYPE_RAID1:
   3526   1.1    bouyer 		bv->bv_level = 1;
   3527   1.1    bouyer 		break;
   3528   1.1    bouyer 	case MPII_CFG_RAID_VOL_0_TYPE_RAID1E:
   3529   1.1    bouyer 	case MPII_CFG_RAID_VOL_0_TYPE_RAID10:
   3530   1.1    bouyer 		bv->bv_level = 10;
   3531   1.1    bouyer 		break;
   3532   1.1    bouyer 	default:
   3533   1.1    bouyer 		bv->bv_level = -1;
   3534   1.1    bouyer 	}
   3535   1.1    bouyer 
   3536   1.1    bouyer 	if ((rv = mpii_bio_hs(sc, NULL, 0, vpg->hot_spare_pool, &hcnt)) != 0) {
   3537   1.1    bouyer 		free(vpg, M_TEMP);
   3538   1.1    bouyer 		return (rv);
   3539   1.1    bouyer 	}
   3540   1.1    bouyer 
   3541   1.1    bouyer 	bv->bv_nodisk = vpg->num_phys_disks + hcnt;
   3542   1.1    bouyer 
   3543   1.1    bouyer 	bv->bv_size = le64toh(vpg->max_lba) * le16toh(vpg->block_size);
   3544   1.1    bouyer 
   3545   1.1    bouyer 	periph = scsipi_lookup_periph(&sc->sc_chan, dev->slot, 0);
   3546   1.1    bouyer 	if (periph != NULL) {
   3547   1.1    bouyer 		if (periph->periph_dev == NULL) {
   3548   1.1    bouyer 			snprintf(bv->bv_dev, sizeof(bv->bv_dev), "%s:%d",
   3549   1.1    bouyer 			    DEVNAME(sc), dev->slot);
   3550   1.1    bouyer 		} else {
   3551   1.1    bouyer 			strlcpy(bv->bv_dev, device_xname(periph->periph_dev),
   3552   1.1    bouyer 			    sizeof(bv->bv_dev));
   3553   1.1    bouyer 		}
   3554   1.1    bouyer 	}
   3555   1.1    bouyer 
   3556   1.1    bouyer 	free(vpg, M_TEMP);
   3557   1.1    bouyer 	return (0);
   3558   1.1    bouyer }
   3559   1.1    bouyer 
   3560   1.1    bouyer static int
   3561   1.1    bouyer mpii_ioctl_disk(struct mpii_softc *sc, struct bioc_disk *bd)
   3562   1.1    bouyer {
   3563   1.1    bouyer 	struct mpii_cfg_raid_vol_pg0		*vpg;
   3564   1.1    bouyer 	struct mpii_cfg_raid_vol_pg0_physdisk	*pd;
   3565   1.1    bouyer 	struct mpii_cfg_hdr			hdr;
   3566   1.1    bouyer 	struct mpii_device			*dev;
   3567   1.1    bouyer 	size_t					pagelen;
   3568   1.1    bouyer 	u_int16_t				volh;
   3569   1.1    bouyer 	u_int8_t				dn;
   3570   1.1    bouyer 
   3571   1.1    bouyer 	DNPRINTF(MPII_D_IOCTL, "%s: mpii_ioctl_disk %d/%d\n",
   3572   1.1    bouyer 	    DEVNAME(sc), bd->bd_volid, bd->bd_diskid);
   3573   1.1    bouyer 
   3574   1.1    bouyer 	if ((dev = mpii_find_vol(sc, bd->bd_volid)) == NULL)
   3575   1.1    bouyer 		return (ENODEV);
   3576   1.1    bouyer 	volh = dev->dev_handle;
   3577   1.1    bouyer 
   3578   1.1    bouyer 	if (mpii_req_cfg_header(sc, MPII_CONFIG_REQ_PAGE_TYPE_RAID_VOL, 0,
   3579   1.1    bouyer 	    MPII_CFG_RAID_VOL_ADDR_HANDLE | volh, 0, &hdr) != 0) {
   3580   1.1    bouyer 		printf("%s: unable to fetch header for raid volume page 0\n",
   3581   1.1    bouyer 		    DEVNAME(sc));
   3582   1.1    bouyer 		return (EINVAL);
   3583   1.1    bouyer 	}
   3584   1.1    bouyer 
   3585   1.1    bouyer 	pagelen = hdr.page_length * 4;
   3586  1.12  jdolecek 	vpg = malloc(pagelen, M_TEMP, M_WAITOK | M_ZERO);
   3587   1.1    bouyer 	if (vpg == NULL) {
   3588   1.1    bouyer 		printf("%s: unable to allocate space for raid "
   3589   1.1    bouyer 		    "volume page 0\n", DEVNAME(sc));
   3590   1.1    bouyer 		return (ENOMEM);
   3591   1.1    bouyer 	}
   3592   1.1    bouyer 
   3593   1.1    bouyer 	if (mpii_req_cfg_page(sc, MPII_CFG_RAID_VOL_ADDR_HANDLE | volh, 0,
   3594   1.1    bouyer 	    &hdr, 1, vpg, pagelen) != 0) {
   3595   1.1    bouyer 		printf("%s: unable to fetch raid volume page 0\n",
   3596   1.1    bouyer 		    DEVNAME(sc));
   3597   1.1    bouyer 		free(vpg, M_TEMP);
   3598   1.1    bouyer 		return (EINVAL);
   3599   1.1    bouyer 	}
   3600   1.1    bouyer 
   3601   1.1    bouyer 	if (bd->bd_diskid >= vpg->num_phys_disks) {
   3602   1.1    bouyer 		int		nvdsk = vpg->num_phys_disks;
   3603   1.1    bouyer 		int		hsmap = vpg->hot_spare_pool;
   3604   1.1    bouyer 
   3605   1.1    bouyer 		free(vpg, M_TEMP);
   3606   1.1    bouyer 		return (mpii_bio_hs(sc, bd, nvdsk, hsmap, NULL));
   3607   1.1    bouyer 	}
   3608   1.1    bouyer 
   3609   1.1    bouyer 	pd = (struct mpii_cfg_raid_vol_pg0_physdisk *)(vpg + 1) +
   3610   1.1    bouyer 	    bd->bd_diskid;
   3611   1.1    bouyer 	dn = pd->phys_disk_num;
   3612   1.1    bouyer 
   3613   1.1    bouyer 	free(vpg, M_TEMP);
   3614   1.1    bouyer 	return (mpii_bio_disk(sc, bd, dn));
   3615   1.1    bouyer }
   3616   1.1    bouyer 
   3617   1.1    bouyer static int
   3618   1.1    bouyer mpii_bio_hs(struct mpii_softc *sc, struct bioc_disk *bd, int nvdsk,
   3619   1.1    bouyer      int hsmap, int *hscnt)
   3620   1.1    bouyer {
   3621   1.1    bouyer 	struct mpii_cfg_raid_config_pg0	*cpg;
   3622   1.1    bouyer 	struct mpii_raid_config_element	*el;
   3623   1.1    bouyer 	struct mpii_ecfg_hdr		ehdr;
   3624   1.1    bouyer 	size_t				pagelen;
   3625   1.1    bouyer 	int				i, nhs = 0;
   3626   1.1    bouyer 
   3627   1.1    bouyer 	if (bd) {
   3628   1.1    bouyer 		DNPRINTF(MPII_D_IOCTL, "%s: mpii_bio_hs %d\n", DEVNAME(sc),
   3629   1.1    bouyer 		    bd->bd_diskid - nvdsk);
   3630   1.1    bouyer 	} else {
   3631   1.1    bouyer 		DNPRINTF(MPII_D_IOCTL, "%s: mpii_bio_hs\n", DEVNAME(sc));
   3632   1.1    bouyer 	}
   3633   1.1    bouyer 
   3634   1.1    bouyer 	if (mpii_req_cfg_header(sc, MPII_CONFIG_REQ_PAGE_TYPE_RAID_CONFIG,
   3635   1.1    bouyer 	    0, MPII_CFG_RAID_CONFIG_ACTIVE_CONFIG, MPII_PG_EXTENDED,
   3636   1.1    bouyer 	    &ehdr) != 0) {
   3637   1.1    bouyer 		printf("%s: unable to fetch header for raid config page 0\n",
   3638   1.1    bouyer 		    DEVNAME(sc));
   3639   1.1    bouyer 		return (EINVAL);
   3640   1.1    bouyer 	}
   3641   1.1    bouyer 
   3642   1.1    bouyer 	pagelen = le16toh(ehdr.ext_page_length) * 4;
   3643  1.12  jdolecek 	cpg = malloc(pagelen, M_TEMP, M_WAITOK | M_ZERO);
   3644   1.1    bouyer 	if (cpg == NULL) {
   3645   1.1    bouyer 		printf("%s: unable to allocate space for raid config page 0\n",
   3646   1.1    bouyer 		    DEVNAME(sc));
   3647   1.1    bouyer 		return (ENOMEM);
   3648   1.1    bouyer 	}
   3649   1.1    bouyer 
   3650   1.1    bouyer 	if (mpii_req_cfg_page(sc, MPII_CFG_RAID_CONFIG_ACTIVE_CONFIG,
   3651   1.1    bouyer 	    MPII_PG_EXTENDED, &ehdr, 1, cpg, pagelen) != 0) {
   3652   1.1    bouyer 		printf("%s: unable to fetch raid config page 0\n",
   3653   1.1    bouyer 		    DEVNAME(sc));
   3654   1.1    bouyer 		free(cpg, M_TEMP);
   3655   1.1    bouyer 		return (EINVAL);
   3656   1.1    bouyer 	}
   3657   1.1    bouyer 
   3658   1.1    bouyer 	el = (struct mpii_raid_config_element *)(cpg + 1);
   3659   1.1    bouyer 	for (i = 0; i < cpg->num_elements; i++, el++) {
   3660   1.1    bouyer 		if (ISSET(le16toh(el->element_flags),
   3661   1.1    bouyer 		    MPII_RAID_CONFIG_ELEMENT_FLAG_HSP_PHYS_DISK) &&
   3662   1.1    bouyer 		    el->hot_spare_pool == hsmap) {
   3663   1.1    bouyer 			/*
   3664   1.1    bouyer 			 * diskid comparison is based on the idea that all
   3665   1.1    bouyer 			 * disks are counted by the bio(4) in sequence, thus
   3666   1.1    bouyer 			 * substracting the number of disks in the volume
   3667   1.1    bouyer 			 * from the diskid yields us a "relative" hotspare
   3668   1.1    bouyer 			 * number, which is good enough for us.
   3669   1.1    bouyer 			 */
   3670   1.1    bouyer 			if (bd != NULL && bd->bd_diskid == nhs + nvdsk) {
   3671   1.1    bouyer 				u_int8_t dn = el->phys_disk_num;
   3672   1.1    bouyer 
   3673   1.1    bouyer 				free(cpg, M_TEMP);
   3674   1.1    bouyer 				return (mpii_bio_disk(sc, bd, dn));
   3675   1.1    bouyer 			}
   3676   1.1    bouyer 			nhs++;
   3677   1.1    bouyer 		}
   3678   1.1    bouyer 	}
   3679   1.1    bouyer 
   3680   1.1    bouyer 	if (hscnt)
   3681   1.1    bouyer 		*hscnt = nhs;
   3682   1.1    bouyer 
   3683   1.1    bouyer 	free(cpg, M_TEMP);
   3684   1.1    bouyer 	return (0);
   3685   1.1    bouyer }
   3686   1.1    bouyer 
   3687   1.1    bouyer static int
   3688   1.1    bouyer mpii_bio_disk(struct mpii_softc *sc, struct bioc_disk *bd, u_int8_t dn)
   3689   1.1    bouyer {
   3690   1.1    bouyer 	struct mpii_cfg_raid_physdisk_pg0	*ppg;
   3691   1.1    bouyer 	struct mpii_cfg_hdr			hdr;
   3692   1.1    bouyer 	struct mpii_device			*dev;
   3693   1.1    bouyer 	int					len;
   3694   1.1    bouyer 
   3695   1.1    bouyer 	DNPRINTF(MPII_D_IOCTL, "%s: mpii_bio_disk %d\n", DEVNAME(sc),
   3696   1.1    bouyer 	    bd->bd_diskid);
   3697   1.1    bouyer 
   3698  1.12  jdolecek 	ppg = malloc(sizeof(*ppg), M_TEMP, M_WAITOK | M_ZERO);
   3699   1.1    bouyer 	if (ppg == NULL) {
   3700   1.1    bouyer 		printf("%s: unable to allocate space for raid physical disk "
   3701   1.1    bouyer 		    "page 0\n", DEVNAME(sc));
   3702   1.1    bouyer 		return (ENOMEM);
   3703   1.1    bouyer 	}
   3704   1.1    bouyer 
   3705   1.1    bouyer 	hdr.page_version = 0;
   3706   1.1    bouyer 	hdr.page_length = sizeof(*ppg) / 4;
   3707   1.1    bouyer 	hdr.page_number = 0;
   3708   1.1    bouyer 	hdr.page_type = MPII_CONFIG_REQ_PAGE_TYPE_RAID_PD;
   3709   1.1    bouyer 
   3710   1.1    bouyer 	if (mpii_req_cfg_page(sc, MPII_CFG_RAID_PHYS_DISK_ADDR_NUMBER | dn, 0,
   3711   1.1    bouyer 	    &hdr, 1, ppg, sizeof(*ppg)) != 0) {
   3712   1.1    bouyer 		printf("%s: unable to fetch raid drive page 0\n",
   3713   1.1    bouyer 		    DEVNAME(sc));
   3714   1.1    bouyer 		free(ppg, M_TEMP);
   3715   1.1    bouyer 		return (EINVAL);
   3716   1.1    bouyer 	}
   3717   1.1    bouyer 
   3718   1.1    bouyer 	bd->bd_target = ppg->phys_disk_num;
   3719   1.1    bouyer 
   3720   1.1    bouyer 	if ((dev = mpii_find_dev(sc, le16toh(ppg->dev_handle))) == NULL) {
   3721   1.1    bouyer 		bd->bd_status = BIOC_SDINVALID;
   3722   1.1    bouyer 		free(ppg, M_TEMP);
   3723   1.1    bouyer 		return (0);
   3724   1.1    bouyer 	}
   3725   1.1    bouyer 
   3726   1.1    bouyer 	switch (ppg->phys_disk_state) {
   3727   1.1    bouyer 	case MPII_CFG_RAID_PHYDISK_0_STATE_ONLINE:
   3728   1.1    bouyer 	case MPII_CFG_RAID_PHYDISK_0_STATE_OPTIMAL:
   3729   1.1    bouyer 		bd->bd_status = BIOC_SDONLINE;
   3730   1.1    bouyer 		break;
   3731   1.1    bouyer 	case MPII_CFG_RAID_PHYDISK_0_STATE_OFFLINE:
   3732   1.1    bouyer 		if (ppg->offline_reason ==
   3733   1.1    bouyer 		    MPII_CFG_RAID_PHYDISK_0_OFFLINE_FAILED ||
   3734   1.1    bouyer 		    ppg->offline_reason ==
   3735   1.1    bouyer 		    MPII_CFG_RAID_PHYDISK_0_OFFLINE_FAILEDREQ)
   3736   1.1    bouyer 			bd->bd_status = BIOC_SDFAILED;
   3737   1.1    bouyer 		else
   3738   1.1    bouyer 			bd->bd_status = BIOC_SDOFFLINE;
   3739   1.1    bouyer 		break;
   3740   1.1    bouyer 	case MPII_CFG_RAID_PHYDISK_0_STATE_DEGRADED:
   3741   1.1    bouyer 		bd->bd_status = BIOC_SDFAILED;
   3742   1.1    bouyer 		break;
   3743   1.1    bouyer 	case MPII_CFG_RAID_PHYDISK_0_STATE_REBUILDING:
   3744   1.1    bouyer 		bd->bd_status = BIOC_SDREBUILD;
   3745   1.1    bouyer 		break;
   3746   1.1    bouyer 	case MPII_CFG_RAID_PHYDISK_0_STATE_HOTSPARE:
   3747   1.1    bouyer 		bd->bd_status = BIOC_SDHOTSPARE;
   3748   1.1    bouyer 		break;
   3749   1.1    bouyer 	case MPII_CFG_RAID_PHYDISK_0_STATE_NOTCONFIGURED:
   3750   1.1    bouyer 		bd->bd_status = BIOC_SDUNUSED;
   3751   1.1    bouyer 		break;
   3752   1.1    bouyer 	case MPII_CFG_RAID_PHYDISK_0_STATE_NOTCOMPATIBLE:
   3753   1.1    bouyer 	default:
   3754   1.1    bouyer 		bd->bd_status = BIOC_SDINVALID;
   3755   1.1    bouyer 		break;
   3756   1.1    bouyer 	}
   3757   1.1    bouyer 
   3758   1.1    bouyer 	bd->bd_size = le64toh(ppg->dev_max_lba) * le16toh(ppg->block_size);
   3759   1.1    bouyer 
   3760   1.8  christos 	strnvisx(bd->bd_vendor, sizeof(bd->bd_vendor),
   3761   1.8  christos 	    ppg->vendor_id, sizeof(ppg->vendor_id),
   3762   1.8  christos 	    VIS_TRIM|VIS_SAFE|VIS_OCTAL);
   3763   1.1    bouyer 	len = strlen(bd->bd_vendor);
   3764   1.1    bouyer 	bd->bd_vendor[len] = ' ';
   3765   1.8  christos 	strnvisx(&bd->bd_vendor[len + 1], sizeof(ppg->vendor_id) - len - 1,
   3766   1.8  christos 	    ppg->product_id, sizeof(ppg->product_id),
   3767   1.8  christos 	    VIS_TRIM|VIS_SAFE|VIS_OCTAL);
   3768   1.8  christos 	strnvisx(bd->bd_serial, sizeof(bd->bd_serial),
   3769   1.8  christos 	    ppg->serial, sizeof(ppg->serial), VIS_TRIM|VIS_SAFE|VIS_OCTAL);
   3770   1.1    bouyer 
   3771   1.1    bouyer 	free(ppg, M_TEMP);
   3772   1.1    bouyer 	return (0);
   3773   1.1    bouyer }
   3774   1.1    bouyer 
   3775   1.1    bouyer static struct mpii_device *
   3776   1.1    bouyer mpii_find_vol(struct mpii_softc *sc, int volid)
   3777   1.1    bouyer {
   3778   1.1    bouyer 	struct mpii_device	*dev = NULL;
   3779   1.1    bouyer 
   3780   1.1    bouyer 	if (sc->sc_vd_id_low + volid >= sc->sc_max_devices)
   3781   1.1    bouyer 		return (NULL);
   3782   1.1    bouyer 	dev = sc->sc_devs[sc->sc_vd_id_low + volid];
   3783   1.1    bouyer 	if (dev && ISSET(dev->flags, MPII_DF_VOLUME))
   3784   1.1    bouyer 		return (dev);
   3785   1.1    bouyer 	return (NULL);
   3786   1.1    bouyer }
   3787   1.1    bouyer 
   3788   1.1    bouyer /*
   3789   1.1    bouyer  * Non-sleeping lightweight version of the mpii_ioctl_vol
   3790   1.1    bouyer  */
   3791   1.1    bouyer static int
   3792   1.1    bouyer mpii_bio_volstate(struct mpii_softc *sc, struct bioc_vol *bv)
   3793   1.1    bouyer {
   3794   1.1    bouyer 	struct mpii_cfg_raid_vol_pg0	*vpg;
   3795   1.1    bouyer 	struct mpii_cfg_hdr		hdr;
   3796   1.1    bouyer 	struct mpii_device		*dev = NULL;
   3797   1.1    bouyer 	size_t				pagelen;
   3798   1.1    bouyer 	u_int16_t			volh;
   3799   1.1    bouyer 
   3800   1.1    bouyer 	if ((dev = mpii_find_vol(sc, bv->bv_volid)) == NULL)
   3801   1.1    bouyer 		return (ENODEV);
   3802   1.1    bouyer 	volh = dev->dev_handle;
   3803   1.1    bouyer 
   3804   1.1    bouyer 	if (mpii_cfg_header(sc, MPII_CONFIG_REQ_PAGE_TYPE_RAID_VOL, 0,
   3805   1.1    bouyer 	    MPII_CFG_RAID_VOL_ADDR_HANDLE | volh, &hdr) != 0) {
   3806   1.1    bouyer 		DNPRINTF(MPII_D_MISC, "%s: unable to fetch header for raid "
   3807   1.1    bouyer 		    "volume page 0\n", DEVNAME(sc));
   3808   1.1    bouyer 		return (EINVAL);
   3809   1.1    bouyer 	}
   3810   1.1    bouyer 
   3811   1.1    bouyer 	pagelen = hdr.page_length * 4;
   3812   1.1    bouyer 	vpg = malloc(pagelen, M_TEMP, M_NOWAIT | M_ZERO);
   3813   1.1    bouyer 	if (vpg == NULL) {
   3814   1.1    bouyer 		DNPRINTF(MPII_D_MISC, "%s: unable to allocate space for raid "
   3815   1.1    bouyer 		    "volume page 0\n", DEVNAME(sc));
   3816   1.1    bouyer 		return (ENOMEM);
   3817   1.1    bouyer 	}
   3818   1.1    bouyer 
   3819   1.1    bouyer 	if (mpii_cfg_page(sc, MPII_CFG_RAID_VOL_ADDR_HANDLE | volh,
   3820   1.1    bouyer 	    &hdr, 1, vpg, pagelen) != 0) {
   3821   1.1    bouyer 		DNPRINTF(MPII_D_MISC, "%s: unable to fetch raid volume "
   3822   1.1    bouyer 		    "page 0\n", DEVNAME(sc));
   3823   1.1    bouyer 		free(vpg, M_TEMP);
   3824   1.1    bouyer 		return (EINVAL);
   3825   1.1    bouyer 	}
   3826   1.1    bouyer 
   3827   1.1    bouyer 	switch (vpg->volume_state) {
   3828   1.1    bouyer 	case MPII_CFG_RAID_VOL_0_STATE_ONLINE:
   3829   1.1    bouyer 	case MPII_CFG_RAID_VOL_0_STATE_OPTIMAL:
   3830   1.1    bouyer 		bv->bv_status = BIOC_SVONLINE;
   3831   1.1    bouyer 		break;
   3832   1.1    bouyer 	case MPII_CFG_RAID_VOL_0_STATE_DEGRADED:
   3833   1.1    bouyer 		if (ISSET(le32toh(vpg->volume_status),
   3834   1.1    bouyer 		    MPII_CFG_RAID_VOL_0_STATUS_RESYNC))
   3835   1.1    bouyer 			bv->bv_status = BIOC_SVREBUILD;
   3836   1.1    bouyer 		else
   3837   1.1    bouyer 			bv->bv_status = BIOC_SVDEGRADED;
   3838   1.1    bouyer 		break;
   3839   1.1    bouyer 	case MPII_CFG_RAID_VOL_0_STATE_FAILED:
   3840   1.1    bouyer 		bv->bv_status = BIOC_SVOFFLINE;
   3841   1.1    bouyer 		break;
   3842   1.1    bouyer 	case MPII_CFG_RAID_VOL_0_STATE_INITIALIZING:
   3843   1.1    bouyer 		bv->bv_status = BIOC_SVBUILDING;
   3844   1.1    bouyer 		break;
   3845   1.1    bouyer 	case MPII_CFG_RAID_VOL_0_STATE_MISSING:
   3846   1.1    bouyer 	default:
   3847   1.1    bouyer 		bv->bv_status = BIOC_SVINVALID;
   3848   1.1    bouyer 		break;
   3849   1.1    bouyer 	}
   3850   1.1    bouyer 
   3851   1.1    bouyer 	free(vpg, M_TEMP);
   3852   1.1    bouyer 	return (0);
   3853   1.1    bouyer }
   3854   1.1    bouyer 
   3855   1.1    bouyer static int
   3856   1.1    bouyer mpii_create_sensors(struct mpii_softc *sc)
   3857   1.1    bouyer {
   3858   1.1    bouyer 	int			i, rv;
   3859   1.1    bouyer 
   3860   1.1    bouyer 	sc->sc_sme = sysmon_envsys_create();
   3861   1.1    bouyer 	sc->sc_sensors = malloc(sizeof(envsys_data_t) * sc->sc_vd_count,
   3862   1.1    bouyer 	    M_DEVBUF, M_NOWAIT | M_ZERO);
   3863   1.1    bouyer 	if (sc->sc_sensors == NULL) {
   3864   1.1    bouyer 		aprint_error_dev(sc->sc_dev,
   3865   1.1    bouyer 		    "can't allocate envsys_data_t\n");
   3866   1.1    bouyer 		return (1);
   3867   1.1    bouyer 	}
   3868   1.1    bouyer 
   3869   1.1    bouyer 	for (i = 0; i < sc->sc_vd_count; i++) {
   3870   1.1    bouyer 		sc->sc_sensors[i].units = ENVSYS_DRIVE;
   3871   1.1    bouyer 		sc->sc_sensors[i].state = ENVSYS_SINVALID;
   3872   1.1    bouyer 		sc->sc_sensors[i].value_cur = ENVSYS_DRIVE_EMPTY;
   3873   1.1    bouyer 		/* Enable monitoring for drive state changes */
   3874   1.1    bouyer 		sc->sc_sensors[i].flags |= ENVSYS_FMONSTCHANGED;
   3875   1.1    bouyer 
   3876   1.1    bouyer 		/* logical drives */
   3877   1.1    bouyer 		snprintf(sc->sc_sensors[i].desc,
   3878   1.1    bouyer 		    sizeof(sc->sc_sensors[i].desc), "%s:%d",
   3879   1.1    bouyer 		    DEVNAME(sc), i);
   3880   1.1    bouyer 		if ((rv = sysmon_envsys_sensor_attach(sc->sc_sme,
   3881   1.1    bouyer 		    &sc->sc_sensors[i])) != 0) {
   3882   1.1    bouyer 			aprint_error_dev(sc->sc_dev,
   3883   1.1    bouyer 			    "unable to attach sensor (rv = %d)\n", rv);
   3884   1.1    bouyer 			goto out;
   3885   1.1    bouyer 		}
   3886   1.1    bouyer 	}
   3887   1.1    bouyer 	sc->sc_sme->sme_name =  DEVNAME(sc);
   3888   1.1    bouyer 	sc->sc_sme->sme_cookie = sc;
   3889   1.1    bouyer 	sc->sc_sme->sme_refresh = mpii_refresh_sensors;
   3890   1.1    bouyer 
   3891   1.1    bouyer 	rv = sysmon_envsys_register(sc->sc_sme);
   3892   1.1    bouyer 
   3893   1.1    bouyer 	if (rv != 0) {
   3894   1.1    bouyer 		aprint_error_dev(sc->sc_dev,
   3895   1.1    bouyer 		    "unable to register with sysmon (rv = %d)\n", rv);
   3896   1.1    bouyer 		goto out;
   3897   1.1    bouyer 	}
   3898   1.1    bouyer 	return 0;
   3899   1.1    bouyer 
   3900   1.1    bouyer out:
   3901   1.1    bouyer 	free(sc->sc_sensors, M_DEVBUF);
   3902   1.1    bouyer 	sysmon_envsys_destroy(sc->sc_sme);
   3903   1.1    bouyer 	sc->sc_sme = NULL;
   3904   1.1    bouyer 	return EINVAL;
   3905   1.1    bouyer }
   3906   1.1    bouyer 
   3907   1.1    bouyer static int
   3908   1.1    bouyer mpii_destroy_sensors(struct mpii_softc *sc)
   3909   1.1    bouyer {
   3910   1.1    bouyer 	if (sc->sc_sme == NULL)
   3911   1.1    bouyer 		return 0;
   3912   1.1    bouyer 	sysmon_envsys_unregister(sc->sc_sme);
   3913   1.1    bouyer 	sc->sc_sme = NULL;
   3914   1.1    bouyer 	free(sc->sc_sensors, M_DEVBUF);
   3915   1.1    bouyer 	return 0;
   3916   1.1    bouyer }
   3917   1.1    bouyer 
   3918   1.1    bouyer static void
   3919   1.1    bouyer mpii_refresh_sensors(struct sysmon_envsys *sme, envsys_data_t *edata)
   3920   1.1    bouyer {
   3921   1.1    bouyer 	struct mpii_softc	*sc = sc = sme->sme_cookie;
   3922   1.1    bouyer 	struct bioc_vol		bv;
   3923   1.1    bouyer 	int			s, error;
   3924   1.1    bouyer 
   3925   1.1    bouyer 	bzero(&bv, sizeof(bv));
   3926   1.1    bouyer 	bv.bv_volid = edata->sensor;
   3927   1.1    bouyer 	KERNEL_LOCK(1, curlwp);
   3928   1.1    bouyer 	s = splbio();
   3929   1.1    bouyer 	error = mpii_bio_volstate(sc, &bv);
   3930   1.1    bouyer 	splx(s);
   3931   1.1    bouyer 	KERNEL_UNLOCK_ONE(curlwp);
   3932   1.1    bouyer 	if (error)
   3933   1.6  christos 		bv.bv_status = BIOC_SVINVALID;
   3934   1.6  christos 	bio_vol_to_envsys(edata, &bv);
   3935   1.1    bouyer }
   3936   1.1    bouyer #endif /* NBIO > 0 */
   3937