1/*-
2 * Copyright (c) 2011-2014 LSI Corp.
3 * All rights reserved.
4 *
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions
7 * are met:
8 * 1. Redistributions of source code must retain the above copyright
9 *    notice, this list of conditions and the following disclaimer.
10 * 2. Redistributions in binary form must reproduce the above copyright
11 *    notice, this list of conditions and the following disclaimer in the
12 *    documentation and/or other materials provided with the distribution.
13 *
14 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
15 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
16 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
17 * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
18 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
19 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
20 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
21 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
22 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
23 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
24 * SUCH DAMAGE.
25 *
26 * LSI MPT-Fusion Host Adapter FreeBSD
27 *
28 * $FreeBSD$
29 */
30
31struct mpr_fw_event_work;
32
33struct mprsas_lun {
34	SLIST_ENTRY(mprsas_lun) lun_link;
35	lun_id_t	lun_id;
36	uint8_t		eedp_formatted;
37	uint32_t	eedp_block_size;
38	uint8_t		stop_at_shutdown;
39};
40
41struct mprsas_target {
42	uint16_t	handle;
43	uint8_t		linkrate;
44	uint8_t		encl_level_valid;
45	uint8_t		encl_level;
46	char		connector_name[4];
47	uint64_t	devname;
48	uint32_t	devinfo;
49	uint16_t	encl_handle;
50	uint16_t	encl_slot;
51	uint8_t		flags;
52#define MPRSAS_TARGET_INABORT	(1 << 0)
53#define MPRSAS_TARGET_INRESET	(1 << 1)
54#define MPRSAS_TARGET_INDIAGRESET (1 << 2)
55#define MPRSAS_TARGET_INREMOVAL	(1 << 3)
56#define MPR_TARGET_FLAGS_RAID_COMPONENT (1 << 4)
57#define MPR_TARGET_FLAGS_VOLUME         (1 << 5)
58#define MPRSAS_TARGET_INRECOVERY (MPRSAS_TARGET_INABORT | \
59    MPRSAS_TARGET_INRESET | MPRSAS_TARGET_INCHIPRESET)
60
61#define MPRSAS_TARGET_ADD       (1 << 29)
62#define MPRSAS_TARGET_REMOVE    (1 << 30)
63	uint16_t	tid;
64	SLIST_HEAD(, mprsas_lun) luns;
65	TAILQ_HEAD(, mpr_command) commands;
66	struct mpr_command *tm;
67	TAILQ_HEAD(, mpr_command) timedout_commands;
68	uint16_t        exp_dev_handle;
69	uint16_t        phy_num;
70	uint64_t	sasaddr;
71	uint16_t	parent_handle;
72	uint64_t	parent_sasaddr;
73	uint32_t	parent_devinfo;
74	struct sysctl_ctx_list sysctl_ctx;
75	struct sysctl_oid *sysctl_tree;
76	TAILQ_ENTRY(mprsas_target) sysctl_link;
77	uint64_t        issued;
78	uint64_t        completed;
79	unsigned int    outstanding;
80	unsigned int    timeouts;
81	unsigned int    aborts;
82	unsigned int    logical_unit_resets;
83	unsigned int    target_resets;
84	uint8_t		scsi_req_desc_type;
85};
86
87struct mprsas_softc {
88	struct mpr_softc	*sc;
89	u_int			flags;
90#define MPRSAS_IN_DISCOVERY	(1 << 0)
91#define MPRSAS_IN_STARTUP	(1 << 1)
92#define MPRSAS_DISCOVERY_TIMEOUT_PENDING	(1 << 2)
93#define MPRSAS_QUEUE_FROZEN	(1 << 3)
94#define	MPRSAS_SHUTDOWN		(1 << 4)
95#define	MPRSAS_SCANTHREAD	(1 << 5)
96	u_int			maxtargets;
97	struct mprsas_target	*targets;
98	struct cam_devq		*devq;
99	struct cam_sim		*sim;
100	struct cam_path		*path;
101	struct intr_config_hook	sas_ich;
102	struct callout		discovery_callout;
103	struct mpr_event_handle	*mprsas_eh;
104
105	u_int                   startup_refcount;
106	u_int                   tm_count;
107	struct proc             *sysctl_proc;
108
109	struct taskqueue	*ev_tq;
110	struct task		ev_task;
111	TAILQ_HEAD(, mpr_fw_event_work)	ev_queue;
112};
113
114MALLOC_DECLARE(M_MPRSAS);
115
116/*
117 * Abstracted so that the driver can be backwards and forwards compatible
118 * with future versions of CAM that will provide this functionality.
119 */
120#define MPR_SET_LUN(lun, ccblun)	\
121	mprsas_set_lun(lun, ccblun)
122
123static __inline int
124mprsas_set_lun(uint8_t *lun, u_int ccblun)
125{
126	uint64_t *newlun;
127
128	newlun = (uint64_t *)lun;
129	*newlun = 0;
130	if (ccblun <= 0xff) {
131		/* Peripheral device address method, LUN is 0 to 255 */
132		lun[1] = ccblun;
133	} else if (ccblun <= 0x3fff) {
134		/* Flat space address method, LUN is <= 16383 */
135		scsi_ulto2b(ccblun, lun);
136		lun[0] |= 0x40;
137	} else if (ccblun <= 0xffffff) {
138		/* Extended flat space address method, LUN is <= 16777215 */
139		scsi_ulto3b(ccblun, &lun[1]);
140		/* Extended Flat space address method */
141		lun[0] = 0xc0;
142		/* Length = 1, i.e. LUN is 3 bytes long */
143		lun[0] |= 0x10;
144		/* Extended Address Method */
145		lun[0] |= 0x02;
146	} else {
147		return (EINVAL);
148	}
149
150	return (0);
151}
152
153#define MPR_SET_SINGLE_LUN(req, lun)	\
154do {					\
155	bzero((req)->LUN, 8);		\
156	(req)->LUN[1] = lun;		\
157} while(0)
158
159void mprsas_rescan_target(struct mpr_softc *sc, struct mprsas_target *targ);
160void mprsas_discovery_end(struct mprsas_softc *sassc);
161void mprsas_startup_increment(struct mprsas_softc *sassc);
162void mprsas_startup_decrement(struct mprsas_softc *sassc);
163void mprsas_release_simq_reinit(struct mprsas_softc *sassc);
164
165struct mpr_command * mprsas_alloc_tm(struct mpr_softc *sc);
166void mprsas_free_tm(struct mpr_softc *sc, struct mpr_command *tm);
167void mprsas_firmware_event_work(void *arg, int pending);
168int mprsas_check_id(struct mprsas_softc *sassc, int id);
169