1b2441318SGreg Kroah-Hartman /* SPDX-License-Identifier: GPL-2.0 */
2c7ebbbceSChristoph Hellwig #ifndef SCSI_TRANSPORT_SAS_H
3c7ebbbceSChristoph Hellwig #define SCSI_TRANSPORT_SAS_H
4c7ebbbceSChristoph Hellwig
5c7ebbbceSChristoph Hellwig #include <linux/transport_class.h>
6c7ebbbceSChristoph Hellwig #include <linux/types.h>
765c92b09SJames Bottomley #include <linux/mutex.h>
85929faf3SDarrick J. Wong #include <scsi/sas.h>
9651a0136SChristoph Hellwig #include <linux/bsg-lib.h>
10c7ebbbceSChristoph Hellwig
11c7ebbbceSChristoph Hellwig struct scsi_transport_template;
12c7ebbbceSChristoph Hellwig struct sas_rphy;
137aa68e80SFUJITA Tomonori struct request;
14c7ebbbceSChristoph Hellwig
153b91d09cSJames Bottomley #if !IS_ENABLED(CONFIG_SCSI_SAS_ATTRS)
scsi_is_sas_rphy(const struct device * sdev)16c1a23f6dSJohannes Thumshirn static inline int scsi_is_sas_rphy(const struct device *sdev)
17c1a23f6dSJohannes Thumshirn {
18c1a23f6dSJohannes Thumshirn return 0;
19c1a23f6dSJohannes Thumshirn }
203b91d09cSJames Bottomley #else
21c1a23f6dSJohannes Thumshirn extern int scsi_is_sas_rphy(const struct device *);
223b91d09cSJames Bottomley #endif
233b91d09cSJames Bottomley
sas_protocol_ata(enum sas_protocol proto)240f05df8bSJames Bottomley static inline int sas_protocol_ata(enum sas_protocol proto)
250f05df8bSJames Bottomley {
260f05df8bSJames Bottomley return ((proto & SAS_PROTOCOL_SATA) ||
270f05df8bSJames Bottomley (proto & SAS_PROTOCOL_STP))? 1 : 0;
280f05df8bSJames Bottomley }
290f05df8bSJames Bottomley
30c7ebbbceSChristoph Hellwig enum sas_linkrate {
3188edf746SJames Bottomley /* These Values are defined in the SAS standard */
3288edf746SJames Bottomley SAS_LINK_RATE_UNKNOWN = 0,
3388edf746SJames Bottomley SAS_PHY_DISABLED = 1,
3488edf746SJames Bottomley SAS_PHY_RESET_PROBLEM = 2,
3588edf746SJames Bottomley SAS_SATA_SPINUP_HOLD = 3,
3688edf746SJames Bottomley SAS_SATA_PORT_SELECTOR = 4,
3788edf746SJames Bottomley SAS_PHY_RESET_IN_PROGRESS = 5,
3888edf746SJames Bottomley SAS_LINK_RATE_1_5_GBPS = 8,
3988edf746SJames Bottomley SAS_LINK_RATE_G1 = SAS_LINK_RATE_1_5_GBPS,
4088edf746SJames Bottomley SAS_LINK_RATE_3_0_GBPS = 9,
4188edf746SJames Bottomley SAS_LINK_RATE_G2 = SAS_LINK_RATE_3_0_GBPS,
4288edf746SJames Bottomley SAS_LINK_RATE_6_0_GBPS = 10,
43d84fd392SSreekanth Reddy SAS_LINK_RATE_12_0_GBPS = 11,
443d8fa78eSSreekanth Reddy SAS_LINK_RATE_22_5_GBPS = 12,
4588edf746SJames Bottomley /* These are virtual to the transport class and may never
4688edf746SJames Bottomley * be signalled normally since the standard defined field
4788edf746SJames Bottomley * is only 4 bits */
4888edf746SJames Bottomley SAS_LINK_RATE_FAILED = 0x10,
4988edf746SJames Bottomley SAS_PHY_VIRTUAL = 0x11,
50c7ebbbceSChristoph Hellwig };
51c7ebbbceSChristoph Hellwig
52c7ebbbceSChristoph Hellwig struct sas_identify {
53c7ebbbceSChristoph Hellwig enum sas_device_type device_type;
54c7ebbbceSChristoph Hellwig enum sas_protocol initiator_port_protocols;
55c7ebbbceSChristoph Hellwig enum sas_protocol target_port_protocols;
56c7ebbbceSChristoph Hellwig u64 sas_address;
57c7ebbbceSChristoph Hellwig u8 phy_identifier;
58c7ebbbceSChristoph Hellwig };
59c7ebbbceSChristoph Hellwig
60c7ebbbceSChristoph Hellwig struct sas_phy {
61c7ebbbceSChristoph Hellwig struct device dev;
62c7ebbbceSChristoph Hellwig int number;
63acbf167dSDarrick J. Wong int enabled;
64c3ee74c4SChristoph Hellwig
65c3ee74c4SChristoph Hellwig /* phy identification */
66c7ebbbceSChristoph Hellwig struct sas_identify identify;
67c3ee74c4SChristoph Hellwig
68c3ee74c4SChristoph Hellwig /* phy attributes */
69c7ebbbceSChristoph Hellwig enum sas_linkrate negotiated_linkrate;
70c7ebbbceSChristoph Hellwig enum sas_linkrate minimum_linkrate_hw;
71c7ebbbceSChristoph Hellwig enum sas_linkrate minimum_linkrate;
72c7ebbbceSChristoph Hellwig enum sas_linkrate maximum_linkrate_hw;
73c7ebbbceSChristoph Hellwig enum sas_linkrate maximum_linkrate;
74c3ee74c4SChristoph Hellwig
75c3ee74c4SChristoph Hellwig /* link error statistics */
76c3ee74c4SChristoph Hellwig u32 invalid_dword_count;
77c3ee74c4SChristoph Hellwig u32 running_disparity_error_count;
78c3ee74c4SChristoph Hellwig u32 loss_of_dword_sync_count;
79c3ee74c4SChristoph Hellwig u32 phy_reset_problem_count;
80c3ee74c4SChristoph Hellwig
8165c92b09SJames Bottomley /* for the list of phys belonging to a port */
8265c92b09SJames Bottomley struct list_head port_siblings;
83dea22214SDarrick J. Wong
840b3e09daSDan Williams /* available to the lldd */
850b3e09daSDan Williams void *hostdata;
86c7ebbbceSChristoph Hellwig };
87c7ebbbceSChristoph Hellwig
88c7ebbbceSChristoph Hellwig #define dev_to_phy(d) \
89c7ebbbceSChristoph Hellwig container_of((d), struct sas_phy, dev)
90ee959b00STony Jones #define transport_class_to_phy(dev) \
91ee959b00STony Jones dev_to_phy((dev)->parent)
92c7ebbbceSChristoph Hellwig #define phy_to_shost(phy) \
93c7ebbbceSChristoph Hellwig dev_to_shost((phy)->dev.parent)
94c7ebbbceSChristoph Hellwig
95b6aff669SJames Bottomley struct request_queue;
96c7ebbbceSChristoph Hellwig struct sas_rphy {
97c7ebbbceSChristoph Hellwig struct device dev;
98c7ebbbceSChristoph Hellwig struct sas_identify identify;
99c7ebbbceSChristoph Hellwig struct list_head list;
100b6aff669SJames Bottomley struct request_queue *q;
101c7ebbbceSChristoph Hellwig u32 scsi_target_id;
102c7ebbbceSChristoph Hellwig };
103c7ebbbceSChristoph Hellwig
104c7ebbbceSChristoph Hellwig #define dev_to_rphy(d) \
105c7ebbbceSChristoph Hellwig container_of((d), struct sas_rphy, dev)
106ee959b00STony Jones #define transport_class_to_rphy(dev) \
107ee959b00STony Jones dev_to_rphy((dev)->parent)
108c7ebbbceSChristoph Hellwig #define rphy_to_shost(rphy) \
109c7ebbbceSChristoph Hellwig dev_to_shost((rphy)->dev.parent)
11042ab0360SJames Bottomley #define target_to_rphy(targ) \
11142ab0360SJames Bottomley dev_to_rphy((targ)->dev.parent)
11242ab0360SJames Bottomley
11342ab0360SJames Bottomley struct sas_end_device {
11442ab0360SJames Bottomley struct sas_rphy rphy;
11542ab0360SJames Bottomley /* flags */
11642ab0360SJames Bottomley unsigned ready_led_meaning:1;
1170f88009dSJames Bottomley unsigned tlr_supported:1;
1180f88009dSJames Bottomley unsigned tlr_enabled:1;
11942ab0360SJames Bottomley /* parameters */
12042ab0360SJames Bottomley u16 I_T_nexus_loss_timeout;
12142ab0360SJames Bottomley u16 initiator_response_timeout;
12242ab0360SJames Bottomley };
12342ab0360SJames Bottomley #define rphy_to_end_device(r) \
12442ab0360SJames Bottomley container_of((r), struct sas_end_device, rphy)
125c7ebbbceSChristoph Hellwig
12679cb1819SJames Bottomley struct sas_expander_device {
12779cb1819SJames Bottomley int level;
128c9fefeb2SJames Bottomley int next_port_id;
12979cb1819SJames Bottomley
13079cb1819SJames Bottomley #define SAS_EXPANDER_VENDOR_ID_LEN 8
13179cb1819SJames Bottomley char vendor_id[SAS_EXPANDER_VENDOR_ID_LEN+1];
13279cb1819SJames Bottomley #define SAS_EXPANDER_PRODUCT_ID_LEN 16
13379cb1819SJames Bottomley char product_id[SAS_EXPANDER_PRODUCT_ID_LEN+1];
13479cb1819SJames Bottomley #define SAS_EXPANDER_PRODUCT_REV_LEN 4
13579cb1819SJames Bottomley char product_rev[SAS_EXPANDER_PRODUCT_REV_LEN+1];
13679cb1819SJames Bottomley #define SAS_EXPANDER_COMPONENT_VENDOR_ID_LEN 8
13779cb1819SJames Bottomley char component_vendor_id[SAS_EXPANDER_COMPONENT_VENDOR_ID_LEN+1];
13879cb1819SJames Bottomley u16 component_id;
13979cb1819SJames Bottomley u8 component_revision_id;
14079cb1819SJames Bottomley
14179cb1819SJames Bottomley struct sas_rphy rphy;
14279cb1819SJames Bottomley
14379cb1819SJames Bottomley };
14479cb1819SJames Bottomley #define rphy_to_expander_device(r) \
14579cb1819SJames Bottomley container_of((r), struct sas_expander_device, rphy)
146c3ee74c4SChristoph Hellwig
14765c92b09SJames Bottomley struct sas_port {
14865c92b09SJames Bottomley struct device dev;
14965c92b09SJames Bottomley
150c9fefeb2SJames Bottomley int port_identifier;
15165c92b09SJames Bottomley int num_phys;
152a0e1b6efSJames Bottomley /* port flags */
153a0e1b6efSJames Bottomley unsigned int is_backlink:1;
15465c92b09SJames Bottomley
15565c92b09SJames Bottomley /* the other end of the link */
15665c92b09SJames Bottomley struct sas_rphy *rphy;
15765c92b09SJames Bottomley
15865c92b09SJames Bottomley struct mutex phy_list_mutex;
15965c92b09SJames Bottomley struct list_head phy_list;
1600558f33cSJason Yan struct list_head del_list; /* libsas only */
16165c92b09SJames Bottomley };
16265c92b09SJames Bottomley
16365c92b09SJames Bottomley #define dev_to_sas_port(d) \
16465c92b09SJames Bottomley container_of((d), struct sas_port, dev)
165ee959b00STony Jones #define transport_class_to_sas_port(dev) \
166ee959b00STony Jones dev_to_sas_port((dev)->parent)
16765c92b09SJames Bottomley
168d24e1eebSJames Bottomley struct sas_phy_linkrates {
169d24e1eebSJames Bottomley enum sas_linkrate maximum_linkrate;
170d24e1eebSJames Bottomley enum sas_linkrate minimum_linkrate;
171d24e1eebSJames Bottomley };
172d24e1eebSJames Bottomley
173c3ee74c4SChristoph Hellwig /* The functions by which the transport class and the driver communicate */
174c3ee74c4SChristoph Hellwig struct sas_function_template {
175c3ee74c4SChristoph Hellwig int (*get_linkerrors)(struct sas_phy *);
176a0125641SChristoph Hellwig int (*get_enclosure_identifier)(struct sas_rphy *, u64 *);
177a0125641SChristoph Hellwig int (*get_bay_identifier)(struct sas_rphy *);
17807ba3a95SChristoph Hellwig int (*phy_reset)(struct sas_phy *, int);
179acbf167dSDarrick J. Wong int (*phy_enable)(struct sas_phy *, int);
1800b3e09daSDan Williams int (*phy_setup)(struct sas_phy *);
1810b3e09daSDan Williams void (*phy_release)(struct sas_phy *);
182d24e1eebSJames Bottomley int (*set_phy_speed)(struct sas_phy *, struct sas_phy_linkrates *);
183651a0136SChristoph Hellwig void (*smp_handler)(struct bsg_job *, struct Scsi_Host *,
184651a0136SChristoph Hellwig struct sas_rphy *);
185c3ee74c4SChristoph Hellwig };
186c3ee74c4SChristoph Hellwig
187c3ee74c4SChristoph Hellwig
18865c92b09SJames Bottomley void sas_remove_children(struct device *);
189c7ebbbceSChristoph Hellwig extern void sas_remove_host(struct Scsi_Host *);
190c7ebbbceSChristoph Hellwig
191c7ebbbceSChristoph Hellwig extern struct sas_phy *sas_phy_alloc(struct device *, int);
192c7ebbbceSChristoph Hellwig extern void sas_phy_free(struct sas_phy *);
193c7ebbbceSChristoph Hellwig extern int sas_phy_add(struct sas_phy *);
194c7ebbbceSChristoph Hellwig extern void sas_phy_delete(struct sas_phy *);
195c7ebbbceSChristoph Hellwig extern int scsi_is_sas_phy(const struct device *);
196c7ebbbceSChristoph Hellwig
197bcf508c1SJames Bottomley u64 sas_get_address(struct scsi_device *);
1980f88009dSJames Bottomley unsigned int sas_tlr_supported(struct scsi_device *);
1990f88009dSJames Bottomley unsigned int sas_is_tlr_enabled(struct scsi_device *);
2000f88009dSJames Bottomley void sas_disable_tlr(struct scsi_device *);
2010f88009dSJames Bottomley void sas_enable_tlr(struct scsi_device *);
2020f88009dSJames Bottomley
203*da097dccSDamien Le Moal bool sas_ata_ncq_prio_supported(struct scsi_device *sdev);
204*da097dccSDamien Le Moal
20565c92b09SJames Bottomley extern struct sas_rphy *sas_end_device_alloc(struct sas_port *);
20665c92b09SJames Bottomley extern struct sas_rphy *sas_expander_alloc(struct sas_port *, enum sas_device_type);
207c7ebbbceSChristoph Hellwig void sas_rphy_free(struct sas_rphy *);
208c7ebbbceSChristoph Hellwig extern int sas_rphy_add(struct sas_rphy *);
2096f63caaeSDarrick J. Wong extern void sas_rphy_remove(struct sas_rphy *);
210c7ebbbceSChristoph Hellwig extern void sas_rphy_delete(struct sas_rphy *);
21187c8331fSDan Williams extern void sas_rphy_unlink(struct sas_rphy *);
212c7ebbbceSChristoph Hellwig
21365c92b09SJames Bottomley struct sas_port *sas_port_alloc(struct device *, int);
214c9fefeb2SJames Bottomley struct sas_port *sas_port_alloc_num(struct device *);
21565c92b09SJames Bottomley int sas_port_add(struct sas_port *);
21665c92b09SJames Bottomley void sas_port_free(struct sas_port *);
21765c92b09SJames Bottomley void sas_port_delete(struct sas_port *);
21865c92b09SJames Bottomley void sas_port_add_phy(struct sas_port *, struct sas_phy *);
21965c92b09SJames Bottomley void sas_port_delete_phy(struct sas_port *, struct sas_phy *);
220a0e1b6efSJames Bottomley void sas_port_mark_backlink(struct sas_port *);
22165c92b09SJames Bottomley int scsi_is_sas_port(const struct device *);
222f41a0c44SDan Williams struct sas_phy *sas_port_get_phy(struct sas_port *port);
sas_port_put_phy(struct sas_phy * phy)223f41a0c44SDan Williams static inline void sas_port_put_phy(struct sas_phy *phy)
224f41a0c44SDan Williams {
225f41a0c44SDan Williams if (phy)
226f41a0c44SDan Williams put_device(&phy->dev);
227f41a0c44SDan Williams }
22865c92b09SJames Bottomley
229c7ebbbceSChristoph Hellwig extern struct scsi_transport_template *
230c7ebbbceSChristoph Hellwig sas_attach_transport(struct sas_function_template *);
231c7ebbbceSChristoph Hellwig extern void sas_release_transport(struct scsi_transport_template *);
23242ab0360SJames Bottomley int sas_read_port_mode_page(struct scsi_device *);
233c7ebbbceSChristoph Hellwig
23479cb1819SJames Bottomley static inline int
scsi_is_sas_expander_device(struct device * dev)23579cb1819SJames Bottomley scsi_is_sas_expander_device(struct device *dev)
23679cb1819SJames Bottomley {
23779cb1819SJames Bottomley struct sas_rphy *rphy;
23879cb1819SJames Bottomley if (!scsi_is_sas_rphy(dev))
23979cb1819SJames Bottomley return 0;
24079cb1819SJames Bottomley rphy = dev_to_rphy(dev);
24179cb1819SJames Bottomley return rphy->identify.device_type == SAS_FANOUT_EXPANDER_DEVICE ||
24279cb1819SJames Bottomley rphy->identify.device_type == SAS_EDGE_EXPANDER_DEVICE;
24379cb1819SJames Bottomley }
24479cb1819SJames Bottomley
245f4ad7b58SJames Bottomley #define scsi_is_sas_phy_local(phy) scsi_is_host_device((phy)->dev.parent)
246f4ad7b58SJames Bottomley
247c7ebbbceSChristoph Hellwig #endif /* SCSI_TRANSPORT_SAS_H */
248