Linux 4.18.10
[linux/fpc-iii.git] / drivers / scsi / libsas / sas_host_smp.c
blob9ead93df3a6e21621b00d067a0b5aef06eb8c5d8
1 /*
2 * Serial Attached SCSI (SAS) Expander discovery and configuration
4 * Copyright (C) 2007 James E.J. Bottomley
5 * <James.Bottomley@HansenPartnership.com>
7 * This program is free software; you can redistribute it and/or
8 * modify it under the terms of the GNU General Public License as
9 * published by the Free Software Foundation; version 2 only.
11 #include <linux/scatterlist.h>
12 #include <linux/blkdev.h>
13 #include <linux/slab.h>
14 #include <linux/export.h>
16 #include "sas_internal.h"
18 #include <scsi/scsi_transport.h>
19 #include <scsi/scsi_transport_sas.h>
20 #include "../scsi_sas_internal.h"
22 static void sas_host_smp_discover(struct sas_ha_struct *sas_ha, u8 *resp_data,
23 u8 phy_id)
25 struct sas_phy *phy;
26 struct sas_rphy *rphy;
28 if (phy_id >= sas_ha->num_phys) {
29 resp_data[2] = SMP_RESP_NO_PHY;
30 return;
32 resp_data[2] = SMP_RESP_FUNC_ACC;
34 phy = sas_ha->sas_phy[phy_id]->phy;
35 resp_data[9] = phy_id;
36 resp_data[13] = phy->negotiated_linkrate;
37 memcpy(resp_data + 16, sas_ha->sas_addr, SAS_ADDR_SIZE);
38 memcpy(resp_data + 24, sas_ha->sas_phy[phy_id]->attached_sas_addr,
39 SAS_ADDR_SIZE);
40 resp_data[40] = (phy->minimum_linkrate << 4) |
41 phy->minimum_linkrate_hw;
42 resp_data[41] = (phy->maximum_linkrate << 4) |
43 phy->maximum_linkrate_hw;
45 if (!sas_ha->sas_phy[phy_id]->port ||
46 !sas_ha->sas_phy[phy_id]->port->port_dev)
47 return;
49 rphy = sas_ha->sas_phy[phy_id]->port->port_dev->rphy;
50 resp_data[12] = rphy->identify.device_type << 4;
51 resp_data[14] = rphy->identify.initiator_port_protocols;
52 resp_data[15] = rphy->identify.target_port_protocols;
55 /**
56 * to_sas_gpio_gp_bit - given the gpio frame data find the byte/bit position of 'od'
57 * @od: od bit to find
58 * @data: incoming bitstream (from frame)
59 * @index: requested data register index (from frame)
60 * @count: total number of registers in the bitstream (from frame)
61 * @bit: bit position of 'od' in the returned byte
63 * returns NULL if 'od' is not in 'data'
65 * From SFF-8485 v0.7:
66 * "In GPIO_TX[1], bit 0 of byte 3 contains the first bit (i.e., OD0.0)
67 * and bit 7 of byte 0 contains the 32nd bit (i.e., OD10.1).
69 * In GPIO_TX[2], bit 0 of byte 3 contains the 33rd bit (i.e., OD10.2)
70 * and bit 7 of byte 0 contains the 64th bit (i.e., OD21.0)."
72 * The general-purpose (raw-bitstream) RX registers have the same layout
73 * although 'od' is renamed 'id' for 'input data'.
75 * SFF-8489 defines the behavior of the LEDs in response to the 'od' values.
77 static u8 *to_sas_gpio_gp_bit(unsigned int od, u8 *data, u8 index, u8 count, u8 *bit)
79 unsigned int reg;
80 u8 byte;
82 /* gp registers start at index 1 */
83 if (index == 0)
84 return NULL;
86 index--; /* make index 0-based */
87 if (od < index * 32)
88 return NULL;
90 od -= index * 32;
91 reg = od >> 5;
93 if (reg >= count)
94 return NULL;
96 od &= (1 << 5) - 1;
97 byte = 3 - (od >> 3);
98 *bit = od & ((1 << 3) - 1);
100 return &data[reg * 4 + byte];
103 int try_test_sas_gpio_gp_bit(unsigned int od, u8 *data, u8 index, u8 count)
105 u8 *byte;
106 u8 bit;
108 byte = to_sas_gpio_gp_bit(od, data, index, count, &bit);
109 if (!byte)
110 return -1;
112 return (*byte >> bit) & 1;
114 EXPORT_SYMBOL(try_test_sas_gpio_gp_bit);
116 static int sas_host_smp_write_gpio(struct sas_ha_struct *sas_ha, u8 *resp_data,
117 u8 reg_type, u8 reg_index, u8 reg_count,
118 u8 *req_data)
120 struct sas_internal *i = to_sas_internal(sas_ha->core.shost->transportt);
121 int written;
123 if (i->dft->lldd_write_gpio == NULL) {
124 resp_data[2] = SMP_RESP_FUNC_UNK;
125 return 0;
128 written = i->dft->lldd_write_gpio(sas_ha, reg_type, reg_index,
129 reg_count, req_data);
131 if (written < 0) {
132 resp_data[2] = SMP_RESP_FUNC_FAILED;
133 written = 0;
134 } else
135 resp_data[2] = SMP_RESP_FUNC_ACC;
137 return written;
140 static void sas_report_phy_sata(struct sas_ha_struct *sas_ha, u8 *resp_data,
141 u8 phy_id)
143 struct sas_rphy *rphy;
144 struct dev_to_host_fis *fis;
145 int i;
147 if (phy_id >= sas_ha->num_phys) {
148 resp_data[2] = SMP_RESP_NO_PHY;
149 return;
152 resp_data[2] = SMP_RESP_PHY_NO_SATA;
154 if (!sas_ha->sas_phy[phy_id]->port)
155 return;
157 rphy = sas_ha->sas_phy[phy_id]->port->port_dev->rphy;
158 fis = (struct dev_to_host_fis *)
159 sas_ha->sas_phy[phy_id]->port->port_dev->frame_rcvd;
160 if (rphy->identify.target_port_protocols != SAS_PROTOCOL_SATA)
161 return;
163 resp_data[2] = SMP_RESP_FUNC_ACC;
164 resp_data[9] = phy_id;
165 memcpy(resp_data + 16, sas_ha->sas_phy[phy_id]->attached_sas_addr,
166 SAS_ADDR_SIZE);
168 /* check to see if we have a valid d2h fis */
169 if (fis->fis_type != 0x34)
170 return;
172 /* the d2h fis is required by the standard to be in LE format */
173 for (i = 0; i < 20; i += 4) {
174 u8 *dst = resp_data + 24 + i, *src =
175 &sas_ha->sas_phy[phy_id]->port->port_dev->frame_rcvd[i];
176 dst[0] = src[3];
177 dst[1] = src[2];
178 dst[2] = src[1];
179 dst[3] = src[0];
183 static void sas_phy_control(struct sas_ha_struct *sas_ha, u8 phy_id,
184 u8 phy_op, enum sas_linkrate min,
185 enum sas_linkrate max, u8 *resp_data)
187 struct sas_internal *i =
188 to_sas_internal(sas_ha->core.shost->transportt);
189 struct sas_phy_linkrates rates;
190 struct asd_sas_phy *asd_phy;
192 if (phy_id >= sas_ha->num_phys) {
193 resp_data[2] = SMP_RESP_NO_PHY;
194 return;
197 asd_phy = sas_ha->sas_phy[phy_id];
198 switch (phy_op) {
199 case PHY_FUNC_NOP:
200 case PHY_FUNC_LINK_RESET:
201 case PHY_FUNC_HARD_RESET:
202 case PHY_FUNC_DISABLE:
203 case PHY_FUNC_CLEAR_ERROR_LOG:
204 case PHY_FUNC_CLEAR_AFFIL:
205 case PHY_FUNC_TX_SATA_PS_SIGNAL:
206 break;
208 default:
209 resp_data[2] = SMP_RESP_PHY_UNK_OP;
210 return;
213 rates.minimum_linkrate = min;
214 rates.maximum_linkrate = max;
216 /* filter reset requests through libata eh */
217 if (phy_op == PHY_FUNC_LINK_RESET && sas_try_ata_reset(asd_phy) == 0) {
218 resp_data[2] = SMP_RESP_FUNC_ACC;
219 return;
222 if (i->dft->lldd_control_phy(asd_phy, phy_op, &rates))
223 resp_data[2] = SMP_RESP_FUNC_FAILED;
224 else
225 resp_data[2] = SMP_RESP_FUNC_ACC;
228 void sas_smp_host_handler(struct bsg_job *job, struct Scsi_Host *shost)
230 struct sas_ha_struct *sas_ha = SHOST_TO_SAS_HA(shost);
231 u8 *req_data, *resp_data;
232 unsigned int reslen = 0;
233 int error = -EINVAL;
235 /* eight is the minimum size for request and response frames */
236 if (job->request_payload.payload_len < 8 ||
237 job->reply_payload.payload_len < 8)
238 goto out;
240 error = -ENOMEM;
241 req_data = kzalloc(job->request_payload.payload_len, GFP_KERNEL);
242 if (!req_data)
243 goto out;
244 sg_copy_to_buffer(job->request_payload.sg_list,
245 job->request_payload.sg_cnt, req_data,
246 job->request_payload.payload_len);
248 /* make sure frame can always be built ... we copy
249 * back only the requested length */
250 resp_data = kzalloc(max(job->reply_payload.payload_len, 128U),
251 GFP_KERNEL);
252 if (!resp_data)
253 goto out_free_req;
255 error = -EINVAL;
256 if (req_data[0] != SMP_REQUEST)
257 goto out_free_resp;
259 /* set up default don't know response */
260 resp_data[0] = SMP_RESPONSE;
261 resp_data[1] = req_data[1];
262 resp_data[2] = SMP_RESP_FUNC_UNK;
264 switch (req_data[1]) {
265 case SMP_REPORT_GENERAL:
266 resp_data[2] = SMP_RESP_FUNC_ACC;
267 resp_data[9] = sas_ha->num_phys;
268 reslen = 32;
269 break;
271 case SMP_REPORT_MANUF_INFO:
272 resp_data[2] = SMP_RESP_FUNC_ACC;
273 memcpy(resp_data + 12, shost->hostt->name,
274 SAS_EXPANDER_VENDOR_ID_LEN);
275 memcpy(resp_data + 20, "libsas virt phy",
276 SAS_EXPANDER_PRODUCT_ID_LEN);
277 reslen = 64;
278 break;
280 case SMP_READ_GPIO_REG:
281 /* FIXME: need GPIO support in the transport class */
282 break;
284 case SMP_DISCOVER:
285 if (job->request_payload.payload_len < 16)
286 goto out_free_resp;
287 sas_host_smp_discover(sas_ha, resp_data, req_data[9]);
288 reslen = 56;
289 break;
291 case SMP_REPORT_PHY_ERR_LOG:
292 /* FIXME: could implement this with additional
293 * libsas callbacks providing the HW supports it */
294 break;
296 case SMP_REPORT_PHY_SATA:
297 if (job->request_payload.payload_len < 16)
298 goto out_free_resp;
299 sas_report_phy_sata(sas_ha, resp_data, req_data[9]);
300 reslen = 60;
301 break;
303 case SMP_REPORT_ROUTE_INFO:
304 /* Can't implement; hosts have no routes */
305 break;
307 case SMP_WRITE_GPIO_REG: {
308 /* SFF-8485 v0.7 */
309 const int base_frame_size = 11;
310 int to_write = req_data[4];
312 if (job->request_payload.payload_len <
313 base_frame_size + to_write * 4) {
314 resp_data[2] = SMP_RESP_INV_FRM_LEN;
315 break;
318 to_write = sas_host_smp_write_gpio(sas_ha, resp_data, req_data[2],
319 req_data[3], to_write, &req_data[8]);
320 reslen = 8;
321 break;
324 case SMP_CONF_ROUTE_INFO:
325 /* Can't implement; hosts have no routes */
326 break;
328 case SMP_PHY_CONTROL:
329 if (job->request_payload.payload_len < 44)
330 goto out_free_resp;
331 sas_phy_control(sas_ha, req_data[9], req_data[10],
332 req_data[32] >> 4, req_data[33] >> 4,
333 resp_data);
334 reslen = 8;
335 break;
337 case SMP_PHY_TEST_FUNCTION:
338 /* FIXME: should this be implemented? */
339 break;
341 default:
342 /* probably a 2.0 function */
343 break;
346 sg_copy_from_buffer(job->reply_payload.sg_list,
347 job->reply_payload.sg_cnt, resp_data,
348 job->reply_payload.payload_len);
350 error = 0;
351 out_free_resp:
352 kfree(resp_data);
353 out_free_req:
354 kfree(req_data);
355 out:
356 bsg_job_done(job, error, reslen);