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