1 /* SPDX-License-Identifier: GPL-2.0 */
2 #ifndef SCSI_TRANSPORT_SAS_H
3 #define SCSI_TRANSPORT_SAS_H
4 
5 #include <linux/transport_class.h>
6 #include <linux/types.h>
7 #include <linux/mutex.h>
8 #include <scsi/sas.h>
9 #include <linux/bsg-lib.h>
10 
11 struct scsi_transport_template;
12 struct sas_rphy;
13 struct request;
14 
15 #if !IS_ENABLED(CONFIG_SCSI_SAS_ATTRS)
16 static inline int scsi_is_sas_rphy(const struct device *sdev)
17 {
18 	return 0;
19 }
20 #else
21 extern int scsi_is_sas_rphy(const struct device *);
22 #endif
23 
24 static inline int sas_protocol_ata(enum sas_protocol proto)
25 {
26 	return ((proto & SAS_PROTOCOL_SATA) ||
27 		(proto & SAS_PROTOCOL_STP))? 1 : 0;
28 }
29 
30 enum sas_linkrate {
31 	/* These Values are defined in the SAS standard */
32 	SAS_LINK_RATE_UNKNOWN = 0,
33 	SAS_PHY_DISABLED = 1,
34 	SAS_PHY_RESET_PROBLEM = 2,
35 	SAS_SATA_SPINUP_HOLD = 3,
36 	SAS_SATA_PORT_SELECTOR = 4,
37 	SAS_PHY_RESET_IN_PROGRESS = 5,
38 	SAS_LINK_RATE_1_5_GBPS = 8,
39 	SAS_LINK_RATE_G1 = SAS_LINK_RATE_1_5_GBPS,
40 	SAS_LINK_RATE_3_0_GBPS = 9,
41 	SAS_LINK_RATE_G2 = SAS_LINK_RATE_3_0_GBPS,
42 	SAS_LINK_RATE_6_0_GBPS = 10,
43 	SAS_LINK_RATE_12_0_GBPS = 11,
44 	/* These are virtual to the transport class and may never
45 	 * be signalled normally since the standard defined field
46 	 * is only 4 bits */
47 	SAS_LINK_RATE_FAILED = 0x10,
48 	SAS_PHY_VIRTUAL = 0x11,
49 };
50 
51 struct sas_identify {
52 	enum sas_device_type	device_type;
53 	enum sas_protocol	initiator_port_protocols;
54 	enum sas_protocol	target_port_protocols;
55 	u64			sas_address;
56 	u8			phy_identifier;
57 };
58 
59 struct sas_phy {
60 	struct device		dev;
61 	int			number;
62 	int			enabled;
63 
64 	/* phy identification */
65 	struct sas_identify	identify;
66 
67 	/* phy attributes */
68 	enum sas_linkrate	negotiated_linkrate;
69 	enum sas_linkrate	minimum_linkrate_hw;
70 	enum sas_linkrate	minimum_linkrate;
71 	enum sas_linkrate	maximum_linkrate_hw;
72 	enum sas_linkrate	maximum_linkrate;
73 
74 	/* link error statistics */
75 	u32			invalid_dword_count;
76 	u32			running_disparity_error_count;
77 	u32			loss_of_dword_sync_count;
78 	u32			phy_reset_problem_count;
79 
80 	/* for the list of phys belonging to a port */
81 	struct list_head	port_siblings;
82 
83 	/* available to the lldd */
84 	void			*hostdata;
85 };
86 
87 #define dev_to_phy(d) \
88 	container_of((d), struct sas_phy, dev)
89 #define transport_class_to_phy(dev) \
90 	dev_to_phy((dev)->parent)
91 #define phy_to_shost(phy) \
92 	dev_to_shost((phy)->dev.parent)
93 
94 struct request_queue;
95 struct sas_rphy {
96 	struct device		dev;
97 	struct sas_identify	identify;
98 	struct list_head	list;
99 	struct request_queue	*q;
100 	u32			scsi_target_id;
101 };
102 
103 #define dev_to_rphy(d) \
104 	container_of((d), struct sas_rphy, dev)
105 #define transport_class_to_rphy(dev) \
106 	dev_to_rphy((dev)->parent)
107 #define rphy_to_shost(rphy) \
108 	dev_to_shost((rphy)->dev.parent)
109 #define target_to_rphy(targ) \
110 	dev_to_rphy((targ)->dev.parent)
111 
112 struct sas_end_device {
113 	struct sas_rphy		rphy;
114 	/* flags */
115 	unsigned		ready_led_meaning:1;
116 	unsigned		tlr_supported:1;
117 	unsigned		tlr_enabled:1;
118 	/* parameters */
119 	u16			I_T_nexus_loss_timeout;
120 	u16			initiator_response_timeout;
121 };
122 #define rphy_to_end_device(r) \
123 	container_of((r), struct sas_end_device, rphy)
124 
125 struct sas_expander_device {
126 	int    level;
127 	int    next_port_id;
128 
129 	#define SAS_EXPANDER_VENDOR_ID_LEN	8
130 	char   vendor_id[SAS_EXPANDER_VENDOR_ID_LEN+1];
131 	#define SAS_EXPANDER_PRODUCT_ID_LEN	16
132 	char   product_id[SAS_EXPANDER_PRODUCT_ID_LEN+1];
133 	#define SAS_EXPANDER_PRODUCT_REV_LEN	4
134 	char   product_rev[SAS_EXPANDER_PRODUCT_REV_LEN+1];
135 	#define SAS_EXPANDER_COMPONENT_VENDOR_ID_LEN	8
136 	char   component_vendor_id[SAS_EXPANDER_COMPONENT_VENDOR_ID_LEN+1];
137 	u16    component_id;
138 	u8     component_revision_id;
139 
140 	struct sas_rphy		rphy;
141 
142 };
143 #define rphy_to_expander_device(r) \
144 	container_of((r), struct sas_expander_device, rphy)
145 
146 struct sas_port {
147 	struct device		dev;
148 
149 	int			port_identifier;
150 	int			num_phys;
151 	/* port flags */
152 	unsigned int		is_backlink:1;
153 
154 	/* the other end of the link */
155 	struct sas_rphy		*rphy;
156 
157 	struct mutex		phy_list_mutex;
158 	struct list_head	phy_list;
159 	struct list_head	del_list; /* libsas only */
160 };
161 
162 #define dev_to_sas_port(d) \
163 	container_of((d), struct sas_port, dev)
164 #define transport_class_to_sas_port(dev) \
165 	dev_to_sas_port((dev)->parent)
166 
167 struct sas_phy_linkrates {
168 	enum sas_linkrate maximum_linkrate;
169 	enum sas_linkrate minimum_linkrate;
170 };
171 
172 /* The functions by which the transport class and the driver communicate */
173 struct sas_function_template {
174 	int (*get_linkerrors)(struct sas_phy *);
175 	int (*get_enclosure_identifier)(struct sas_rphy *, u64 *);
176 	int (*get_bay_identifier)(struct sas_rphy *);
177 	int (*phy_reset)(struct sas_phy *, int);
178 	int (*phy_enable)(struct sas_phy *, int);
179 	int (*phy_setup)(struct sas_phy *);
180 	void (*phy_release)(struct sas_phy *);
181 	int (*set_phy_speed)(struct sas_phy *, struct sas_phy_linkrates *);
182 	void (*smp_handler)(struct bsg_job *, struct Scsi_Host *,
183 			struct sas_rphy *);
184 };
185 
186 
187 void sas_remove_children(struct device *);
188 extern void sas_remove_host(struct Scsi_Host *);
189 
190 extern struct sas_phy *sas_phy_alloc(struct device *, int);
191 extern void sas_phy_free(struct sas_phy *);
192 extern int sas_phy_add(struct sas_phy *);
193 extern void sas_phy_delete(struct sas_phy *);
194 extern int scsi_is_sas_phy(const struct device *);
195 
196 u64 sas_get_address(struct scsi_device *);
197 unsigned int sas_tlr_supported(struct scsi_device *);
198 unsigned int sas_is_tlr_enabled(struct scsi_device *);
199 void sas_disable_tlr(struct scsi_device *);
200 void sas_enable_tlr(struct scsi_device *);
201 
202 extern struct sas_rphy *sas_end_device_alloc(struct sas_port *);
203 extern struct sas_rphy *sas_expander_alloc(struct sas_port *, enum sas_device_type);
204 void sas_rphy_free(struct sas_rphy *);
205 extern int sas_rphy_add(struct sas_rphy *);
206 extern void sas_rphy_remove(struct sas_rphy *);
207 extern void sas_rphy_delete(struct sas_rphy *);
208 extern void sas_rphy_unlink(struct sas_rphy *);
209 
210 struct sas_port *sas_port_alloc(struct device *, int);
211 struct sas_port *sas_port_alloc_num(struct device *);
212 int sas_port_add(struct sas_port *);
213 void sas_port_free(struct sas_port *);
214 void sas_port_delete(struct sas_port *);
215 void sas_port_add_phy(struct sas_port *, struct sas_phy *);
216 void sas_port_delete_phy(struct sas_port *, struct sas_phy *);
217 void sas_port_mark_backlink(struct sas_port *);
218 int scsi_is_sas_port(const struct device *);
219 struct sas_phy *sas_port_get_phy(struct sas_port *port);
220 static inline void sas_port_put_phy(struct sas_phy *phy)
221 {
222 	if (phy)
223 		put_device(&phy->dev);
224 }
225 
226 extern struct scsi_transport_template *
227 sas_attach_transport(struct sas_function_template *);
228 extern void sas_release_transport(struct scsi_transport_template *);
229 int sas_read_port_mode_page(struct scsi_device *);
230 
231 static inline int
232 scsi_is_sas_expander_device(struct device *dev)
233 {
234 	struct sas_rphy *rphy;
235 	if (!scsi_is_sas_rphy(dev))
236 		return 0;
237 	rphy = dev_to_rphy(dev);
238 	return rphy->identify.device_type == SAS_FANOUT_EXPANDER_DEVICE ||
239 		rphy->identify.device_type == SAS_EDGE_EXPANDER_DEVICE;
240 }
241 
242 #define scsi_is_sas_phy_local(phy)	scsi_is_host_device((phy)->dev.parent)
243 
244 #endif /* SCSI_TRANSPORT_SAS_H */
245