18c2ecf20Sopenharmony_ci/* SPDX-License-Identifier: GPL-2.0 */
28c2ecf20Sopenharmony_ci#ifndef SCSI_TRANSPORT_SAS_H
38c2ecf20Sopenharmony_ci#define SCSI_TRANSPORT_SAS_H
48c2ecf20Sopenharmony_ci
58c2ecf20Sopenharmony_ci#include <linux/transport_class.h>
68c2ecf20Sopenharmony_ci#include <linux/types.h>
78c2ecf20Sopenharmony_ci#include <linux/mutex.h>
88c2ecf20Sopenharmony_ci#include <scsi/sas.h>
98c2ecf20Sopenharmony_ci#include <linux/bsg-lib.h>
108c2ecf20Sopenharmony_ci
118c2ecf20Sopenharmony_cistruct scsi_transport_template;
128c2ecf20Sopenharmony_cistruct sas_rphy;
138c2ecf20Sopenharmony_cistruct request;
148c2ecf20Sopenharmony_ci
158c2ecf20Sopenharmony_ci#if !IS_ENABLED(CONFIG_SCSI_SAS_ATTRS)
168c2ecf20Sopenharmony_cistatic inline int scsi_is_sas_rphy(const struct device *sdev)
178c2ecf20Sopenharmony_ci{
188c2ecf20Sopenharmony_ci	return 0;
198c2ecf20Sopenharmony_ci}
208c2ecf20Sopenharmony_ci#else
218c2ecf20Sopenharmony_ciextern int scsi_is_sas_rphy(const struct device *);
228c2ecf20Sopenharmony_ci#endif
238c2ecf20Sopenharmony_ci
248c2ecf20Sopenharmony_cistatic inline int sas_protocol_ata(enum sas_protocol proto)
258c2ecf20Sopenharmony_ci{
268c2ecf20Sopenharmony_ci	return ((proto & SAS_PROTOCOL_SATA) ||
278c2ecf20Sopenharmony_ci		(proto & SAS_PROTOCOL_STP))? 1 : 0;
288c2ecf20Sopenharmony_ci}
298c2ecf20Sopenharmony_ci
308c2ecf20Sopenharmony_cienum sas_linkrate {
318c2ecf20Sopenharmony_ci	/* These Values are defined in the SAS standard */
328c2ecf20Sopenharmony_ci	SAS_LINK_RATE_UNKNOWN = 0,
338c2ecf20Sopenharmony_ci	SAS_PHY_DISABLED = 1,
348c2ecf20Sopenharmony_ci	SAS_PHY_RESET_PROBLEM = 2,
358c2ecf20Sopenharmony_ci	SAS_SATA_SPINUP_HOLD = 3,
368c2ecf20Sopenharmony_ci	SAS_SATA_PORT_SELECTOR = 4,
378c2ecf20Sopenharmony_ci	SAS_PHY_RESET_IN_PROGRESS = 5,
388c2ecf20Sopenharmony_ci	SAS_LINK_RATE_1_5_GBPS = 8,
398c2ecf20Sopenharmony_ci	SAS_LINK_RATE_G1 = SAS_LINK_RATE_1_5_GBPS,
408c2ecf20Sopenharmony_ci	SAS_LINK_RATE_3_0_GBPS = 9,
418c2ecf20Sopenharmony_ci	SAS_LINK_RATE_G2 = SAS_LINK_RATE_3_0_GBPS,
428c2ecf20Sopenharmony_ci	SAS_LINK_RATE_6_0_GBPS = 10,
438c2ecf20Sopenharmony_ci	SAS_LINK_RATE_12_0_GBPS = 11,
448c2ecf20Sopenharmony_ci	/* These are virtual to the transport class and may never
458c2ecf20Sopenharmony_ci	 * be signalled normally since the standard defined field
468c2ecf20Sopenharmony_ci	 * is only 4 bits */
478c2ecf20Sopenharmony_ci	SAS_LINK_RATE_FAILED = 0x10,
488c2ecf20Sopenharmony_ci	SAS_PHY_VIRTUAL = 0x11,
498c2ecf20Sopenharmony_ci};
508c2ecf20Sopenharmony_ci
518c2ecf20Sopenharmony_cistruct sas_identify {
528c2ecf20Sopenharmony_ci	enum sas_device_type	device_type;
538c2ecf20Sopenharmony_ci	enum sas_protocol	initiator_port_protocols;
548c2ecf20Sopenharmony_ci	enum sas_protocol	target_port_protocols;
558c2ecf20Sopenharmony_ci	u64			sas_address;
568c2ecf20Sopenharmony_ci	u8			phy_identifier;
578c2ecf20Sopenharmony_ci};
588c2ecf20Sopenharmony_ci
598c2ecf20Sopenharmony_cistruct sas_phy {
608c2ecf20Sopenharmony_ci	struct device		dev;
618c2ecf20Sopenharmony_ci	int			number;
628c2ecf20Sopenharmony_ci	int			enabled;
638c2ecf20Sopenharmony_ci
648c2ecf20Sopenharmony_ci	/* phy identification */
658c2ecf20Sopenharmony_ci	struct sas_identify	identify;
668c2ecf20Sopenharmony_ci
678c2ecf20Sopenharmony_ci	/* phy attributes */
688c2ecf20Sopenharmony_ci	enum sas_linkrate	negotiated_linkrate;
698c2ecf20Sopenharmony_ci	enum sas_linkrate	minimum_linkrate_hw;
708c2ecf20Sopenharmony_ci	enum sas_linkrate	minimum_linkrate;
718c2ecf20Sopenharmony_ci	enum sas_linkrate	maximum_linkrate_hw;
728c2ecf20Sopenharmony_ci	enum sas_linkrate	maximum_linkrate;
738c2ecf20Sopenharmony_ci
748c2ecf20Sopenharmony_ci	/* link error statistics */
758c2ecf20Sopenharmony_ci	u32			invalid_dword_count;
768c2ecf20Sopenharmony_ci	u32			running_disparity_error_count;
778c2ecf20Sopenharmony_ci	u32			loss_of_dword_sync_count;
788c2ecf20Sopenharmony_ci	u32			phy_reset_problem_count;
798c2ecf20Sopenharmony_ci
808c2ecf20Sopenharmony_ci	/* for the list of phys belonging to a port */
818c2ecf20Sopenharmony_ci	struct list_head	port_siblings;
828c2ecf20Sopenharmony_ci
838c2ecf20Sopenharmony_ci	/* available to the lldd */
848c2ecf20Sopenharmony_ci	void			*hostdata;
858c2ecf20Sopenharmony_ci};
868c2ecf20Sopenharmony_ci
878c2ecf20Sopenharmony_ci#define dev_to_phy(d) \
888c2ecf20Sopenharmony_ci	container_of((d), struct sas_phy, dev)
898c2ecf20Sopenharmony_ci#define transport_class_to_phy(dev) \
908c2ecf20Sopenharmony_ci	dev_to_phy((dev)->parent)
918c2ecf20Sopenharmony_ci#define phy_to_shost(phy) \
928c2ecf20Sopenharmony_ci	dev_to_shost((phy)->dev.parent)
938c2ecf20Sopenharmony_ci
948c2ecf20Sopenharmony_cistruct request_queue;
958c2ecf20Sopenharmony_cistruct sas_rphy {
968c2ecf20Sopenharmony_ci	struct device		dev;
978c2ecf20Sopenharmony_ci	struct sas_identify	identify;
988c2ecf20Sopenharmony_ci	struct list_head	list;
998c2ecf20Sopenharmony_ci	struct request_queue	*q;
1008c2ecf20Sopenharmony_ci	u32			scsi_target_id;
1018c2ecf20Sopenharmony_ci};
1028c2ecf20Sopenharmony_ci
1038c2ecf20Sopenharmony_ci#define dev_to_rphy(d) \
1048c2ecf20Sopenharmony_ci	container_of((d), struct sas_rphy, dev)
1058c2ecf20Sopenharmony_ci#define transport_class_to_rphy(dev) \
1068c2ecf20Sopenharmony_ci	dev_to_rphy((dev)->parent)
1078c2ecf20Sopenharmony_ci#define rphy_to_shost(rphy) \
1088c2ecf20Sopenharmony_ci	dev_to_shost((rphy)->dev.parent)
1098c2ecf20Sopenharmony_ci#define target_to_rphy(targ) \
1108c2ecf20Sopenharmony_ci	dev_to_rphy((targ)->dev.parent)
1118c2ecf20Sopenharmony_ci
1128c2ecf20Sopenharmony_cistruct sas_end_device {
1138c2ecf20Sopenharmony_ci	struct sas_rphy		rphy;
1148c2ecf20Sopenharmony_ci	/* flags */
1158c2ecf20Sopenharmony_ci	unsigned		ready_led_meaning:1;
1168c2ecf20Sopenharmony_ci	unsigned		tlr_supported:1;
1178c2ecf20Sopenharmony_ci	unsigned		tlr_enabled:1;
1188c2ecf20Sopenharmony_ci	/* parameters */
1198c2ecf20Sopenharmony_ci	u16			I_T_nexus_loss_timeout;
1208c2ecf20Sopenharmony_ci	u16			initiator_response_timeout;
1218c2ecf20Sopenharmony_ci};
1228c2ecf20Sopenharmony_ci#define rphy_to_end_device(r) \
1238c2ecf20Sopenharmony_ci	container_of((r), struct sas_end_device, rphy)
1248c2ecf20Sopenharmony_ci
1258c2ecf20Sopenharmony_cistruct sas_expander_device {
1268c2ecf20Sopenharmony_ci	int    level;
1278c2ecf20Sopenharmony_ci	int    next_port_id;
1288c2ecf20Sopenharmony_ci
1298c2ecf20Sopenharmony_ci	#define SAS_EXPANDER_VENDOR_ID_LEN	8
1308c2ecf20Sopenharmony_ci	char   vendor_id[SAS_EXPANDER_VENDOR_ID_LEN+1];
1318c2ecf20Sopenharmony_ci	#define SAS_EXPANDER_PRODUCT_ID_LEN	16
1328c2ecf20Sopenharmony_ci	char   product_id[SAS_EXPANDER_PRODUCT_ID_LEN+1];
1338c2ecf20Sopenharmony_ci	#define SAS_EXPANDER_PRODUCT_REV_LEN	4
1348c2ecf20Sopenharmony_ci	char   product_rev[SAS_EXPANDER_PRODUCT_REV_LEN+1];
1358c2ecf20Sopenharmony_ci	#define SAS_EXPANDER_COMPONENT_VENDOR_ID_LEN	8
1368c2ecf20Sopenharmony_ci	char   component_vendor_id[SAS_EXPANDER_COMPONENT_VENDOR_ID_LEN+1];
1378c2ecf20Sopenharmony_ci	u16    component_id;
1388c2ecf20Sopenharmony_ci	u8     component_revision_id;
1398c2ecf20Sopenharmony_ci
1408c2ecf20Sopenharmony_ci	struct sas_rphy		rphy;
1418c2ecf20Sopenharmony_ci
1428c2ecf20Sopenharmony_ci};
1438c2ecf20Sopenharmony_ci#define rphy_to_expander_device(r) \
1448c2ecf20Sopenharmony_ci	container_of((r), struct sas_expander_device, rphy)
1458c2ecf20Sopenharmony_ci
1468c2ecf20Sopenharmony_cistruct sas_port {
1478c2ecf20Sopenharmony_ci	struct device		dev;
1488c2ecf20Sopenharmony_ci
1498c2ecf20Sopenharmony_ci	int			port_identifier;
1508c2ecf20Sopenharmony_ci	int			num_phys;
1518c2ecf20Sopenharmony_ci	/* port flags */
1528c2ecf20Sopenharmony_ci	unsigned int		is_backlink:1;
1538c2ecf20Sopenharmony_ci
1548c2ecf20Sopenharmony_ci	/* the other end of the link */
1558c2ecf20Sopenharmony_ci	struct sas_rphy		*rphy;
1568c2ecf20Sopenharmony_ci
1578c2ecf20Sopenharmony_ci	struct mutex		phy_list_mutex;
1588c2ecf20Sopenharmony_ci	struct list_head	phy_list;
1598c2ecf20Sopenharmony_ci	struct list_head	del_list; /* libsas only */
1608c2ecf20Sopenharmony_ci};
1618c2ecf20Sopenharmony_ci
1628c2ecf20Sopenharmony_ci#define dev_to_sas_port(d) \
1638c2ecf20Sopenharmony_ci	container_of((d), struct sas_port, dev)
1648c2ecf20Sopenharmony_ci#define transport_class_to_sas_port(dev) \
1658c2ecf20Sopenharmony_ci	dev_to_sas_port((dev)->parent)
1668c2ecf20Sopenharmony_ci
1678c2ecf20Sopenharmony_cistruct sas_phy_linkrates {
1688c2ecf20Sopenharmony_ci	enum sas_linkrate maximum_linkrate;
1698c2ecf20Sopenharmony_ci	enum sas_linkrate minimum_linkrate;
1708c2ecf20Sopenharmony_ci};
1718c2ecf20Sopenharmony_ci
1728c2ecf20Sopenharmony_ci/* The functions by which the transport class and the driver communicate */
1738c2ecf20Sopenharmony_cistruct sas_function_template {
1748c2ecf20Sopenharmony_ci	int (*get_linkerrors)(struct sas_phy *);
1758c2ecf20Sopenharmony_ci	int (*get_enclosure_identifier)(struct sas_rphy *, u64 *);
1768c2ecf20Sopenharmony_ci	int (*get_bay_identifier)(struct sas_rphy *);
1778c2ecf20Sopenharmony_ci	int (*phy_reset)(struct sas_phy *, int);
1788c2ecf20Sopenharmony_ci	int (*phy_enable)(struct sas_phy *, int);
1798c2ecf20Sopenharmony_ci	int (*phy_setup)(struct sas_phy *);
1808c2ecf20Sopenharmony_ci	void (*phy_release)(struct sas_phy *);
1818c2ecf20Sopenharmony_ci	int (*set_phy_speed)(struct sas_phy *, struct sas_phy_linkrates *);
1828c2ecf20Sopenharmony_ci	void (*smp_handler)(struct bsg_job *, struct Scsi_Host *,
1838c2ecf20Sopenharmony_ci			struct sas_rphy *);
1848c2ecf20Sopenharmony_ci};
1858c2ecf20Sopenharmony_ci
1868c2ecf20Sopenharmony_ci
1878c2ecf20Sopenharmony_civoid sas_remove_children(struct device *);
1888c2ecf20Sopenharmony_ciextern void sas_remove_host(struct Scsi_Host *);
1898c2ecf20Sopenharmony_ci
1908c2ecf20Sopenharmony_ciextern struct sas_phy *sas_phy_alloc(struct device *, int);
1918c2ecf20Sopenharmony_ciextern void sas_phy_free(struct sas_phy *);
1928c2ecf20Sopenharmony_ciextern int sas_phy_add(struct sas_phy *);
1938c2ecf20Sopenharmony_ciextern void sas_phy_delete(struct sas_phy *);
1948c2ecf20Sopenharmony_ciextern int scsi_is_sas_phy(const struct device *);
1958c2ecf20Sopenharmony_ci
1968c2ecf20Sopenharmony_ciu64 sas_get_address(struct scsi_device *);
1978c2ecf20Sopenharmony_ciunsigned int sas_tlr_supported(struct scsi_device *);
1988c2ecf20Sopenharmony_ciunsigned int sas_is_tlr_enabled(struct scsi_device *);
1998c2ecf20Sopenharmony_civoid sas_disable_tlr(struct scsi_device *);
2008c2ecf20Sopenharmony_civoid sas_enable_tlr(struct scsi_device *);
2018c2ecf20Sopenharmony_ci
2028c2ecf20Sopenharmony_ciextern struct sas_rphy *sas_end_device_alloc(struct sas_port *);
2038c2ecf20Sopenharmony_ciextern struct sas_rphy *sas_expander_alloc(struct sas_port *, enum sas_device_type);
2048c2ecf20Sopenharmony_civoid sas_rphy_free(struct sas_rphy *);
2058c2ecf20Sopenharmony_ciextern int sas_rphy_add(struct sas_rphy *);
2068c2ecf20Sopenharmony_ciextern void sas_rphy_remove(struct sas_rphy *);
2078c2ecf20Sopenharmony_ciextern void sas_rphy_delete(struct sas_rphy *);
2088c2ecf20Sopenharmony_ciextern void sas_rphy_unlink(struct sas_rphy *);
2098c2ecf20Sopenharmony_ci
2108c2ecf20Sopenharmony_cistruct sas_port *sas_port_alloc(struct device *, int);
2118c2ecf20Sopenharmony_cistruct sas_port *sas_port_alloc_num(struct device *);
2128c2ecf20Sopenharmony_ciint sas_port_add(struct sas_port *);
2138c2ecf20Sopenharmony_civoid sas_port_free(struct sas_port *);
2148c2ecf20Sopenharmony_civoid sas_port_delete(struct sas_port *);
2158c2ecf20Sopenharmony_civoid sas_port_add_phy(struct sas_port *, struct sas_phy *);
2168c2ecf20Sopenharmony_civoid sas_port_delete_phy(struct sas_port *, struct sas_phy *);
2178c2ecf20Sopenharmony_civoid sas_port_mark_backlink(struct sas_port *);
2188c2ecf20Sopenharmony_ciint scsi_is_sas_port(const struct device *);
2198c2ecf20Sopenharmony_cistruct sas_phy *sas_port_get_phy(struct sas_port *port);
2208c2ecf20Sopenharmony_cistatic inline void sas_port_put_phy(struct sas_phy *phy)
2218c2ecf20Sopenharmony_ci{
2228c2ecf20Sopenharmony_ci	if (phy)
2238c2ecf20Sopenharmony_ci		put_device(&phy->dev);
2248c2ecf20Sopenharmony_ci}
2258c2ecf20Sopenharmony_ci
2268c2ecf20Sopenharmony_ciextern struct scsi_transport_template *
2278c2ecf20Sopenharmony_cisas_attach_transport(struct sas_function_template *);
2288c2ecf20Sopenharmony_ciextern void sas_release_transport(struct scsi_transport_template *);
2298c2ecf20Sopenharmony_ciint sas_read_port_mode_page(struct scsi_device *);
2308c2ecf20Sopenharmony_ci
2318c2ecf20Sopenharmony_cistatic inline int
2328c2ecf20Sopenharmony_ciscsi_is_sas_expander_device(struct device *dev)
2338c2ecf20Sopenharmony_ci{
2348c2ecf20Sopenharmony_ci	struct sas_rphy *rphy;
2358c2ecf20Sopenharmony_ci	if (!scsi_is_sas_rphy(dev))
2368c2ecf20Sopenharmony_ci		return 0;
2378c2ecf20Sopenharmony_ci	rphy = dev_to_rphy(dev);
2388c2ecf20Sopenharmony_ci	return rphy->identify.device_type == SAS_FANOUT_EXPANDER_DEVICE ||
2398c2ecf20Sopenharmony_ci		rphy->identify.device_type == SAS_EDGE_EXPANDER_DEVICE;
2408c2ecf20Sopenharmony_ci}
2418c2ecf20Sopenharmony_ci
2428c2ecf20Sopenharmony_ci#define scsi_is_sas_phy_local(phy)	scsi_is_host_device((phy)->dev.parent)
2438c2ecf20Sopenharmony_ci
2448c2ecf20Sopenharmony_ci#endif /* SCSI_TRANSPORT_SAS_H */
245