1 // SPDX-License-Identifier: GPL-2.0-only
3 * Serial Attached SCSI (SAS) Expander discovery and configuration
5 * Copyright (C) 2007 James E.J. Bottomley
6 * <James.Bottomley@HansenPartnership.com>
8 #include <linux/scatterlist.h>
9 #include <linux/blkdev.h>
10 #include <linux/slab.h>
11 #include <linux/export.h>
13 #include "sas_internal.h"
15 #include <scsi/scsi_transport.h>
16 #include <scsi/scsi_transport_sas.h>
17 #include "../scsi_sas_internal.h"
19 static void sas_host_smp_discover(struct sas_ha_struct
*sas_ha
, u8
*resp_data
,
23 struct sas_rphy
*rphy
;
25 if (phy_id
>= sas_ha
->num_phys
) {
26 resp_data
[2] = SMP_RESP_NO_PHY
;
29 resp_data
[2] = SMP_RESP_FUNC_ACC
;
31 phy
= sas_ha
->sas_phy
[phy_id
]->phy
;
32 resp_data
[9] = phy_id
;
33 resp_data
[13] = phy
->negotiated_linkrate
;
34 memcpy(resp_data
+ 16, sas_ha
->sas_addr
, SAS_ADDR_SIZE
);
35 memcpy(resp_data
+ 24, sas_ha
->sas_phy
[phy_id
]->attached_sas_addr
,
37 resp_data
[40] = (phy
->minimum_linkrate
<< 4) |
38 phy
->minimum_linkrate_hw
;
39 resp_data
[41] = (phy
->maximum_linkrate
<< 4) |
40 phy
->maximum_linkrate_hw
;
42 if (!sas_ha
->sas_phy
[phy_id
]->port
||
43 !sas_ha
->sas_phy
[phy_id
]->port
->port_dev
)
46 rphy
= sas_ha
->sas_phy
[phy_id
]->port
->port_dev
->rphy
;
47 resp_data
[12] = rphy
->identify
.device_type
<< 4;
48 resp_data
[14] = rphy
->identify
.initiator_port_protocols
;
49 resp_data
[15] = rphy
->identify
.target_port_protocols
;
53 * to_sas_gpio_gp_bit - given the gpio frame data find the byte/bit position of 'od'
55 * @data: incoming bitstream (from frame)
56 * @index: requested data register index (from frame)
57 * @count: total number of registers in the bitstream (from frame)
58 * @bit: bit position of 'od' in the returned byte
60 * returns NULL if 'od' is not in 'data'
63 * "In GPIO_TX[1], bit 0 of byte 3 contains the first bit (i.e., OD0.0)
64 * and bit 7 of byte 0 contains the 32nd bit (i.e., OD10.1).
66 * In GPIO_TX[2], bit 0 of byte 3 contains the 33rd bit (i.e., OD10.2)
67 * and bit 7 of byte 0 contains the 64th bit (i.e., OD21.0)."
69 * The general-purpose (raw-bitstream) RX registers have the same layout
70 * although 'od' is renamed 'id' for 'input data'.
72 * SFF-8489 defines the behavior of the LEDs in response to the 'od' values.
74 static u8
*to_sas_gpio_gp_bit(unsigned int od
, u8
*data
, u8 index
, u8 count
, u8
*bit
)
79 /* gp registers start at index 1 */
83 index
--; /* make index 0-based */
95 *bit
= od
& ((1 << 3) - 1);
97 return &data
[reg
* 4 + byte
];
100 int try_test_sas_gpio_gp_bit(unsigned int od
, u8
*data
, u8 index
, u8 count
)
105 byte
= to_sas_gpio_gp_bit(od
, data
, index
, count
, &bit
);
109 return (*byte
>> bit
) & 1;
111 EXPORT_SYMBOL(try_test_sas_gpio_gp_bit
);
113 static int sas_host_smp_write_gpio(struct sas_ha_struct
*sas_ha
, u8
*resp_data
,
114 u8 reg_type
, u8 reg_index
, u8 reg_count
,
117 struct sas_internal
*i
= to_sas_internal(sas_ha
->core
.shost
->transportt
);
120 if (i
->dft
->lldd_write_gpio
== NULL
) {
121 resp_data
[2] = SMP_RESP_FUNC_UNK
;
125 written
= i
->dft
->lldd_write_gpio(sas_ha
, reg_type
, reg_index
,
126 reg_count
, req_data
);
129 resp_data
[2] = SMP_RESP_FUNC_FAILED
;
132 resp_data
[2] = SMP_RESP_FUNC_ACC
;
137 static void sas_report_phy_sata(struct sas_ha_struct
*sas_ha
, u8
*resp_data
,
140 struct sas_rphy
*rphy
;
141 struct dev_to_host_fis
*fis
;
144 if (phy_id
>= sas_ha
->num_phys
) {
145 resp_data
[2] = SMP_RESP_NO_PHY
;
149 resp_data
[2] = SMP_RESP_PHY_NO_SATA
;
151 if (!sas_ha
->sas_phy
[phy_id
]->port
)
154 rphy
= sas_ha
->sas_phy
[phy_id
]->port
->port_dev
->rphy
;
155 fis
= (struct dev_to_host_fis
*)
156 sas_ha
->sas_phy
[phy_id
]->port
->port_dev
->frame_rcvd
;
157 if (rphy
->identify
.target_port_protocols
!= SAS_PROTOCOL_SATA
)
160 resp_data
[2] = SMP_RESP_FUNC_ACC
;
161 resp_data
[9] = phy_id
;
162 memcpy(resp_data
+ 16, sas_ha
->sas_phy
[phy_id
]->attached_sas_addr
,
165 /* check to see if we have a valid d2h fis */
166 if (fis
->fis_type
!= 0x34)
169 /* the d2h fis is required by the standard to be in LE format */
170 for (i
= 0; i
< 20; i
+= 4) {
171 u8
*dst
= resp_data
+ 24 + i
, *src
=
172 &sas_ha
->sas_phy
[phy_id
]->port
->port_dev
->frame_rcvd
[i
];
180 static void sas_phy_control(struct sas_ha_struct
*sas_ha
, u8 phy_id
,
181 u8 phy_op
, enum sas_linkrate min
,
182 enum sas_linkrate max
, u8
*resp_data
)
184 struct sas_internal
*i
=
185 to_sas_internal(sas_ha
->core
.shost
->transportt
);
186 struct sas_phy_linkrates rates
;
187 struct asd_sas_phy
*asd_phy
;
189 if (phy_id
>= sas_ha
->num_phys
) {
190 resp_data
[2] = SMP_RESP_NO_PHY
;
194 asd_phy
= sas_ha
->sas_phy
[phy_id
];
197 case PHY_FUNC_LINK_RESET
:
198 case PHY_FUNC_HARD_RESET
:
199 case PHY_FUNC_DISABLE
:
200 case PHY_FUNC_CLEAR_ERROR_LOG
:
201 case PHY_FUNC_CLEAR_AFFIL
:
202 case PHY_FUNC_TX_SATA_PS_SIGNAL
:
206 resp_data
[2] = SMP_RESP_PHY_UNK_OP
;
210 rates
.minimum_linkrate
= min
;
211 rates
.maximum_linkrate
= max
;
213 /* filter reset requests through libata eh */
214 if (phy_op
== PHY_FUNC_LINK_RESET
&& sas_try_ata_reset(asd_phy
) == 0) {
215 resp_data
[2] = SMP_RESP_FUNC_ACC
;
219 if (i
->dft
->lldd_control_phy(asd_phy
, phy_op
, &rates
))
220 resp_data
[2] = SMP_RESP_FUNC_FAILED
;
222 resp_data
[2] = SMP_RESP_FUNC_ACC
;
225 void sas_smp_host_handler(struct bsg_job
*job
, struct Scsi_Host
*shost
)
227 struct sas_ha_struct
*sas_ha
= SHOST_TO_SAS_HA(shost
);
228 u8
*req_data
, *resp_data
;
229 unsigned int reslen
= 0;
232 /* eight is the minimum size for request and response frames */
233 if (job
->request_payload
.payload_len
< 8 ||
234 job
->reply_payload
.payload_len
< 8)
238 req_data
= kzalloc(job
->request_payload
.payload_len
, GFP_KERNEL
);
241 sg_copy_to_buffer(job
->request_payload
.sg_list
,
242 job
->request_payload
.sg_cnt
, req_data
,
243 job
->request_payload
.payload_len
);
245 /* make sure frame can always be built ... we copy
246 * back only the requested length */
247 resp_data
= kzalloc(max(job
->reply_payload
.payload_len
, 128U),
253 if (req_data
[0] != SMP_REQUEST
)
256 /* set up default don't know response */
257 resp_data
[0] = SMP_RESPONSE
;
258 resp_data
[1] = req_data
[1];
259 resp_data
[2] = SMP_RESP_FUNC_UNK
;
261 switch (req_data
[1]) {
262 case SMP_REPORT_GENERAL
:
263 resp_data
[2] = SMP_RESP_FUNC_ACC
;
264 resp_data
[9] = sas_ha
->num_phys
;
268 case SMP_REPORT_MANUF_INFO
:
269 resp_data
[2] = SMP_RESP_FUNC_ACC
;
270 memcpy(resp_data
+ 12, shost
->hostt
->name
,
271 SAS_EXPANDER_VENDOR_ID_LEN
);
272 memcpy(resp_data
+ 20, "libsas virt phy",
273 SAS_EXPANDER_PRODUCT_ID_LEN
);
277 case SMP_READ_GPIO_REG
:
278 /* FIXME: need GPIO support in the transport class */
282 if (job
->request_payload
.payload_len
< 16)
284 sas_host_smp_discover(sas_ha
, resp_data
, req_data
[9]);
288 case SMP_REPORT_PHY_ERR_LOG
:
289 /* FIXME: could implement this with additional
290 * libsas callbacks providing the HW supports it */
293 case SMP_REPORT_PHY_SATA
:
294 if (job
->request_payload
.payload_len
< 16)
296 sas_report_phy_sata(sas_ha
, resp_data
, req_data
[9]);
300 case SMP_REPORT_ROUTE_INFO
:
301 /* Can't implement; hosts have no routes */
304 case SMP_WRITE_GPIO_REG
: {
306 const int base_frame_size
= 11;
307 int to_write
= req_data
[4];
309 if (job
->request_payload
.payload_len
<
310 base_frame_size
+ to_write
* 4) {
311 resp_data
[2] = SMP_RESP_INV_FRM_LEN
;
315 to_write
= sas_host_smp_write_gpio(sas_ha
, resp_data
, req_data
[2],
316 req_data
[3], to_write
, &req_data
[8]);
321 case SMP_CONF_ROUTE_INFO
:
322 /* Can't implement; hosts have no routes */
325 case SMP_PHY_CONTROL
:
326 if (job
->request_payload
.payload_len
< 44)
328 sas_phy_control(sas_ha
, req_data
[9], req_data
[10],
329 req_data
[32] >> 4, req_data
[33] >> 4,
334 case SMP_PHY_TEST_FUNCTION
:
335 /* FIXME: should this be implemented? */
339 /* probably a 2.0 function */
343 sg_copy_from_buffer(job
->reply_payload
.sg_list
,
344 job
->reply_payload
.sg_cnt
, resp_data
,
345 job
->reply_payload
.payload_len
);
353 bsg_job_done(job
, error
, reslen
);