Linux Audio

Check our new training course

Loading...
v3.1
 
  1/*
  2 * Serial Attached SCSI (SAS) Expander discovery and configuration
  3 *
  4 * Copyright (C) 2007 James E.J. Bottomley
  5 *		<James.Bottomley@HansenPartnership.com>
  6 *
  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.
 10 */
 11#include <linux/scatterlist.h>
 12#include <linux/blkdev.h>
 13#include <linux/slab.h>
 
 14
 15#include "sas_internal.h"
 16
 17#include <scsi/scsi_transport.h>
 18#include <scsi/scsi_transport_sas.h>
 19#include "../scsi_sas_internal.h"
 20
 21static void sas_host_smp_discover(struct sas_ha_struct *sas_ha, u8 *resp_data,
 22				  u8 phy_id)
 23{
 24	struct sas_phy *phy;
 25	struct sas_rphy *rphy;
 26
 27	if (phy_id >= sas_ha->num_phys) {
 28		resp_data[2] = SMP_RESP_NO_PHY;
 29		return;
 30	}
 31	resp_data[2] = SMP_RESP_FUNC_ACC;
 32
 33	phy = sas_ha->sas_phy[phy_id]->phy;
 34	resp_data[9] = phy_id;
 35	resp_data[13] = phy->negotiated_linkrate;
 36	memcpy(resp_data + 16, sas_ha->sas_addr, SAS_ADDR_SIZE);
 37	memcpy(resp_data + 24, sas_ha->sas_phy[phy_id]->attached_sas_addr,
 38	       SAS_ADDR_SIZE);
 39	resp_data[40] = (phy->minimum_linkrate << 4) |
 40		phy->minimum_linkrate_hw;
 41	resp_data[41] = (phy->maximum_linkrate << 4) |
 42		phy->maximum_linkrate_hw;
 43
 44	if (!sas_ha->sas_phy[phy_id]->port ||
 45	    !sas_ha->sas_phy[phy_id]->port->port_dev)
 46		return;
 47
 48	rphy = sas_ha->sas_phy[phy_id]->port->port_dev->rphy;
 49	resp_data[12] = rphy->identify.device_type << 4;
 50	resp_data[14] = rphy->identify.initiator_port_protocols;
 51	resp_data[15] = rphy->identify.target_port_protocols;
 52}
 53
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 54static void sas_report_phy_sata(struct sas_ha_struct *sas_ha, u8 *resp_data,
 55				u8 phy_id)
 56{
 57	struct sas_rphy *rphy;
 58	struct dev_to_host_fis *fis;
 59	int i;
 60
 61	if (phy_id >= sas_ha->num_phys) {
 62		resp_data[2] = SMP_RESP_NO_PHY;
 63		return;
 64	}
 65
 66	resp_data[2] = SMP_RESP_PHY_NO_SATA;
 67
 68	if (!sas_ha->sas_phy[phy_id]->port)
 69		return;
 70
 71	rphy = sas_ha->sas_phy[phy_id]->port->port_dev->rphy;
 72	fis = (struct dev_to_host_fis *)
 73		sas_ha->sas_phy[phy_id]->port->port_dev->frame_rcvd;
 74	if (rphy->identify.target_port_protocols != SAS_PROTOCOL_SATA)
 75		return;
 76
 77	resp_data[2] = SMP_RESP_FUNC_ACC;
 78	resp_data[9] = phy_id;
 79	memcpy(resp_data + 16, sas_ha->sas_phy[phy_id]->attached_sas_addr,
 80	       SAS_ADDR_SIZE);
 81
 82	/* check to see if we have a valid d2h fis */
 83	if (fis->fis_type != 0x34)
 84		return;
 85
 86	/* the d2h fis is required by the standard to be in LE format */
 87	for (i = 0; i < 20; i += 4) {
 88		u8 *dst = resp_data + 24 + i, *src =
 89			&sas_ha->sas_phy[phy_id]->port->port_dev->frame_rcvd[i];
 90		dst[0] = src[3];
 91		dst[1] = src[2];
 92		dst[2] = src[1];
 93		dst[3] = src[0];
 94	}
 95}
 96
 97static void sas_phy_control(struct sas_ha_struct *sas_ha, u8 phy_id,
 98			    u8 phy_op, enum sas_linkrate min,
 99			    enum sas_linkrate max, u8 *resp_data)
100{
101	struct sas_internal *i =
102		to_sas_internal(sas_ha->core.shost->transportt);
103	struct sas_phy_linkrates rates;
 
104
105	if (phy_id >= sas_ha->num_phys) {
106		resp_data[2] = SMP_RESP_NO_PHY;
107		return;
108	}
 
 
109	switch (phy_op) {
110	case PHY_FUNC_NOP:
111	case PHY_FUNC_LINK_RESET:
112	case PHY_FUNC_HARD_RESET:
113	case PHY_FUNC_DISABLE:
114	case PHY_FUNC_CLEAR_ERROR_LOG:
115	case PHY_FUNC_CLEAR_AFFIL:
116	case PHY_FUNC_TX_SATA_PS_SIGNAL:
117		break;
118
119	default:
120		resp_data[2] = SMP_RESP_PHY_UNK_OP;
121		return;
122	}
123
124	rates.minimum_linkrate = min;
125	rates.maximum_linkrate = max;
126
127	if (i->dft->lldd_control_phy(sas_ha->sas_phy[phy_id], phy_op, &rates))
 
 
 
 
 
 
128		resp_data[2] = SMP_RESP_FUNC_FAILED;
129	else
130		resp_data[2] = SMP_RESP_FUNC_ACC;
131}
132
133int sas_smp_host_handler(struct Scsi_Host *shost, struct request *req,
134			 struct request *rsp)
135{
136	u8 *req_data = NULL, *resp_data = NULL, *buf;
137	struct sas_ha_struct *sas_ha = SHOST_TO_SAS_HA(shost);
 
 
138	int error = -EINVAL;
139
140	/* eight is the minimum size for request and response frames */
141	if (blk_rq_bytes(req) < 8 || blk_rq_bytes(rsp) < 8)
 
142		goto out;
143
144	if (bio_offset(req->bio) + blk_rq_bytes(req) > PAGE_SIZE ||
145	    bio_offset(rsp->bio) + blk_rq_bytes(rsp) > PAGE_SIZE) {
146		shost_printk(KERN_ERR, shost,
147			"SMP request/response frame crosses page boundary");
148		goto out;
149	}
150
151	req_data = kzalloc(blk_rq_bytes(req), GFP_KERNEL);
152
153	/* make sure frame can always be built ... we copy
154	 * back only the requested length */
155	resp_data = kzalloc(max(blk_rq_bytes(rsp), 128U), GFP_KERNEL);
156
157	if (!req_data || !resp_data) {
158		error = -ENOMEM;
159		goto out;
160	}
161
162	local_irq_disable();
163	buf = kmap_atomic(bio_page(req->bio), KM_USER0) + bio_offset(req->bio);
164	memcpy(req_data, buf, blk_rq_bytes(req));
165	kunmap_atomic(buf - bio_offset(req->bio), KM_USER0);
166	local_irq_enable();
167
 
168	if (req_data[0] != SMP_REQUEST)
169		goto out;
170
171	/* always succeeds ... even if we can't process the request
172	 * the result is in the response frame */
173	error = 0;
174
175	/* set up default don't know response */
176	resp_data[0] = SMP_RESPONSE;
177	resp_data[1] = req_data[1];
178	resp_data[2] = SMP_RESP_FUNC_UNK;
179
180	switch (req_data[1]) {
181	case SMP_REPORT_GENERAL:
182		req->resid_len -= 8;
183		rsp->resid_len -= 32;
184		resp_data[2] = SMP_RESP_FUNC_ACC;
185		resp_data[9] = sas_ha->num_phys;
 
186		break;
187
188	case SMP_REPORT_MANUF_INFO:
189		req->resid_len -= 8;
190		rsp->resid_len -= 64;
191		resp_data[2] = SMP_RESP_FUNC_ACC;
192		memcpy(resp_data + 12, shost->hostt->name,
193		       SAS_EXPANDER_VENDOR_ID_LEN);
194		memcpy(resp_data + 20, "libsas virt phy",
195		       SAS_EXPANDER_PRODUCT_ID_LEN);
 
196		break;
197
198	case SMP_READ_GPIO_REG:
199		/* FIXME: need GPIO support in the transport class */
200		break;
201
202	case SMP_DISCOVER:
203		req->resid_len -= 16;
204		if ((int)req->resid_len < 0) {
205			req->resid_len = 0;
206			error = -EINVAL;
207			goto out;
208		}
209		rsp->resid_len -= 56;
210		sas_host_smp_discover(sas_ha, resp_data, req_data[9]);
 
211		break;
212
213	case SMP_REPORT_PHY_ERR_LOG:
214		/* FIXME: could implement this with additional
215		 * libsas callbacks providing the HW supports it */
216		break;
217
218	case SMP_REPORT_PHY_SATA:
219		req->resid_len -= 16;
220		if ((int)req->resid_len < 0) {
221			req->resid_len = 0;
222			error = -EINVAL;
223			goto out;
224		}
225		rsp->resid_len -= 60;
226		sas_report_phy_sata(sas_ha, resp_data, req_data[9]);
 
227		break;
228
229	case SMP_REPORT_ROUTE_INFO:
230		/* Can't implement; hosts have no routes */
231		break;
232
233	case SMP_WRITE_GPIO_REG:
234		/* FIXME: need GPIO support in the transport class */
 
 
 
 
 
 
 
 
 
 
 
 
235		break;
 
236
237	case SMP_CONF_ROUTE_INFO:
238		/* Can't implement; hosts have no routes */
239		break;
240
241	case SMP_PHY_CONTROL:
242		req->resid_len -= 44;
243		if ((int)req->resid_len < 0) {
244			req->resid_len = 0;
245			error = -EINVAL;
246			goto out;
247		}
248		rsp->resid_len -= 8;
249		sas_phy_control(sas_ha, req_data[9], req_data[10],
250				req_data[32] >> 4, req_data[33] >> 4,
251				resp_data);
 
252		break;
253
254	case SMP_PHY_TEST_FUNCTION:
255		/* FIXME: should this be implemented? */
256		break;
257
258	default:
259		/* probably a 2.0 function */
260		break;
261	}
262
263	local_irq_disable();
264	buf = kmap_atomic(bio_page(rsp->bio), KM_USER0) + bio_offset(rsp->bio);
265	memcpy(buf, resp_data, blk_rq_bytes(rsp));
266	flush_kernel_dcache_page(bio_page(rsp->bio));
267	kunmap_atomic(buf - bio_offset(rsp->bio), KM_USER0);
268	local_irq_enable();
269
270 out:
271	kfree(req_data);
272	kfree(resp_data);
273	return error;
 
 
 
274}
v6.13.7
  1// SPDX-License-Identifier: GPL-2.0-only
  2/*
  3 * Serial Attached SCSI (SAS) Expander discovery and configuration
  4 *
  5 * Copyright (C) 2007 James E.J. Bottomley
  6 *		<James.Bottomley@HansenPartnership.com>
 
 
 
 
  7 */
  8#include <linux/scatterlist.h>
  9#include <linux/blkdev.h>
 10#include <linux/slab.h>
 11#include <linux/export.h>
 12
 13#include "sas_internal.h"
 14
 15#include <scsi/scsi_transport.h>
 16#include <scsi/scsi_transport_sas.h>
 17#include "scsi_sas_internal.h"
 18
 19static void sas_host_smp_discover(struct sas_ha_struct *sas_ha, u8 *resp_data,
 20				  u8 phy_id)
 21{
 22	struct sas_phy *phy;
 23	struct sas_rphy *rphy;
 24
 25	if (phy_id >= sas_ha->num_phys) {
 26		resp_data[2] = SMP_RESP_NO_PHY;
 27		return;
 28	}
 29	resp_data[2] = SMP_RESP_FUNC_ACC;
 30
 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,
 36	       SAS_ADDR_SIZE);
 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;
 41
 42	if (!sas_ha->sas_phy[phy_id]->port ||
 43	    !sas_ha->sas_phy[phy_id]->port->port_dev)
 44		return;
 45
 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;
 50}
 51
 52/**
 53 * to_sas_gpio_gp_bit - given the gpio frame data find the byte/bit position of 'od'
 54 * @od: od bit to find
 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
 59 *
 60 * returns NULL if 'od' is not in 'data'
 61 *
 62 * From SFF-8485 v0.7:
 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).
 65 *
 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)."
 68 *
 69 * The general-purpose (raw-bitstream) RX registers have the same layout
 70 * although 'od' is renamed 'id' for 'input data'.
 71 *
 72 * SFF-8489 defines the behavior of the LEDs in response to the 'od' values.
 73 */
 74static u8 *to_sas_gpio_gp_bit(unsigned int od, u8 *data, u8 index, u8 count, u8 *bit)
 75{
 76	unsigned int reg;
 77	u8 byte;
 78
 79	/* gp registers start at index 1 */
 80	if (index == 0)
 81		return NULL;
 82
 83	index--; /* make index 0-based */
 84	if (od < index * 32)
 85		return NULL;
 86
 87	od -= index * 32;
 88	reg = od >> 5;
 89
 90	if (reg >= count)
 91		return NULL;
 92
 93	od &= (1 << 5) - 1;
 94	byte = 3 - (od >> 3);
 95	*bit = od & ((1 << 3) - 1);
 96
 97	return &data[reg * 4 + byte];
 98}
 99
100int try_test_sas_gpio_gp_bit(unsigned int od, u8 *data, u8 index, u8 count)
101{
102	u8 *byte;
103	u8 bit;
104
105	byte = to_sas_gpio_gp_bit(od, data, index, count, &bit);
106	if (!byte)
107		return -1;
108
109	return (*byte >> bit) & 1;
110}
111EXPORT_SYMBOL(try_test_sas_gpio_gp_bit);
112
113static 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,
115				   u8 *req_data)
116{
117	struct sas_internal *i = to_sas_internal(sas_ha->shost->transportt);
118	int written;
119
120	if (i->dft->lldd_write_gpio == NULL) {
121		resp_data[2] = SMP_RESP_FUNC_UNK;
122		return 0;
123	}
124
125	written = i->dft->lldd_write_gpio(sas_ha, reg_type, reg_index,
126					  reg_count, req_data);
127
128	if (written < 0) {
129		resp_data[2] = SMP_RESP_FUNC_FAILED;
130		written = 0;
131	} else
132		resp_data[2] = SMP_RESP_FUNC_ACC;
133
134	return written;
135}
136
137static void sas_report_phy_sata(struct sas_ha_struct *sas_ha, u8 *resp_data,
138				u8 phy_id)
139{
140	struct sas_rphy *rphy;
141	struct dev_to_host_fis *fis;
142	int i;
143
144	if (phy_id >= sas_ha->num_phys) {
145		resp_data[2] = SMP_RESP_NO_PHY;
146		return;
147	}
148
149	resp_data[2] = SMP_RESP_PHY_NO_SATA;
150
151	if (!sas_ha->sas_phy[phy_id]->port)
152		return;
153
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)
158		return;
159
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,
163	       SAS_ADDR_SIZE);
164
165	/* check to see if we have a valid d2h fis */
166	if (fis->fis_type != 0x34)
167		return;
168
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];
173		dst[0] = src[3];
174		dst[1] = src[2];
175		dst[2] = src[1];
176		dst[3] = src[0];
177	}
178}
179
180static 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)
183{
184	struct sas_internal *i =
185		to_sas_internal(sas_ha->shost->transportt);
186	struct sas_phy_linkrates rates;
187	struct asd_sas_phy *asd_phy;
188
189	if (phy_id >= sas_ha->num_phys) {
190		resp_data[2] = SMP_RESP_NO_PHY;
191		return;
192	}
193
194	asd_phy = sas_ha->sas_phy[phy_id];
195	switch (phy_op) {
196	case PHY_FUNC_NOP:
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:
203		break;
204
205	default:
206		resp_data[2] = SMP_RESP_PHY_UNK_OP;
207		return;
208	}
209
210	rates.minimum_linkrate = min;
211	rates.maximum_linkrate = max;
212
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;
216		return;
217	}
218
219	if (i->dft->lldd_control_phy(asd_phy, phy_op, &rates))
220		resp_data[2] = SMP_RESP_FUNC_FAILED;
221	else
222		resp_data[2] = SMP_RESP_FUNC_ACC;
223}
224
225void sas_smp_host_handler(struct bsg_job *job, struct Scsi_Host *shost)
 
226{
 
227	struct sas_ha_struct *sas_ha = SHOST_TO_SAS_HA(shost);
228	u8 *req_data, *resp_data;
229	unsigned int reslen = 0;
230	int error = -EINVAL;
231
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)
235		goto out;
236
237	error = -ENOMEM;
238	req_data = kzalloc(job->request_payload.payload_len, GFP_KERNEL);
239	if (!req_data)
 
240		goto out;
241	sg_copy_to_buffer(job->request_payload.sg_list,
242			  job->request_payload.sg_cnt, req_data,
243			  job->request_payload.payload_len);
244
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),
248			GFP_KERNEL);
249	if (!resp_data)
250		goto out_free_req;
 
 
 
 
 
 
 
 
251
252	error = -EINVAL;
253	if (req_data[0] != SMP_REQUEST)
254		goto out_free_resp;
 
 
 
 
255
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;
260
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;
265		reslen = 32;
266		break;
267
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);
274		reslen = 64;
275		break;
276
277	case SMP_READ_GPIO_REG:
278		/* FIXME: need GPIO support in the transport class */
279		break;
280
281	case SMP_DISCOVER:
282		if (job->request_payload.payload_len < 16)
283			goto out_free_resp;
 
 
 
 
 
284		sas_host_smp_discover(sas_ha, resp_data, req_data[9]);
285		reslen = 56;
286		break;
287
288	case SMP_REPORT_PHY_ERR_LOG:
289		/* FIXME: could implement this with additional
290		 * libsas callbacks providing the HW supports it */
291		break;
292
293	case SMP_REPORT_PHY_SATA:
294		if (job->request_payload.payload_len < 16)
295			goto out_free_resp;
 
 
 
 
 
296		sas_report_phy_sata(sas_ha, resp_data, req_data[9]);
297		reslen = 60;
298		break;
299
300	case SMP_REPORT_ROUTE_INFO:
301		/* Can't implement; hosts have no routes */
302		break;
303
304	case SMP_WRITE_GPIO_REG: {
305		/* SFF-8485 v0.7 */
306		const int base_frame_size = 11;
307		int to_write = req_data[4];
308
309		if (job->request_payload.payload_len <
310				base_frame_size + to_write * 4) {
311			resp_data[2] = SMP_RESP_INV_FRM_LEN;
312			break;
313		}
314
315		to_write = sas_host_smp_write_gpio(sas_ha, resp_data, req_data[2],
316						   req_data[3], to_write, &req_data[8]);
317		reslen = 8;
318		break;
319	}
320
321	case SMP_CONF_ROUTE_INFO:
322		/* Can't implement; hosts have no routes */
323		break;
324
325	case SMP_PHY_CONTROL:
326		if (job->request_payload.payload_len < 44)
327			goto out_free_resp;
 
 
 
 
 
328		sas_phy_control(sas_ha, req_data[9], req_data[10],
329				req_data[32] >> 4, req_data[33] >> 4,
330				resp_data);
331		reslen = 8;
332		break;
333
334	case SMP_PHY_TEST_FUNCTION:
335		/* FIXME: should this be implemented? */
336		break;
337
338	default:
339		/* probably a 2.0 function */
340		break;
341	}
342
343	sg_copy_from_buffer(job->reply_payload.sg_list,
344			    job->reply_payload.sg_cnt, resp_data,
345			    job->reply_payload.payload_len);
 
 
 
346
347	error = 0;
348out_free_resp:
349	kfree(resp_data);
350out_free_req:
351	kfree(req_data);
352out:
353	bsg_job_done(job, error, reslen);
354}