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