block: add rq->resid_len
[linux-2.6/verdex.git] / drivers / scsi / libsas / sas_host_smp.c
blob89952edd0be37477dc4dda13334079511440cd8a
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>
14 #include "sas_internal.h"
16 #include <scsi/scsi_transport.h>
17 #include <scsi/scsi_transport_sas.h>
18 #include "../scsi_sas_internal.h"
20 static void sas_host_smp_discover(struct sas_ha_struct *sas_ha, u8 *resp_data,
21 u8 phy_id)
23 struct sas_phy *phy;
24 struct sas_rphy *rphy;
26 if (phy_id >= sas_ha->num_phys) {
27 resp_data[2] = SMP_RESP_NO_PHY;
28 return;
30 resp_data[2] = SMP_RESP_FUNC_ACC;
32 phy = sas_ha->sas_phy[phy_id]->phy;
33 resp_data[9] = phy_id;
34 resp_data[13] = phy->negotiated_linkrate;
35 memcpy(resp_data + 16, sas_ha->sas_addr, SAS_ADDR_SIZE);
36 memcpy(resp_data + 24, sas_ha->sas_phy[phy_id]->attached_sas_addr,
37 SAS_ADDR_SIZE);
38 resp_data[40] = (phy->minimum_linkrate << 4) |
39 phy->minimum_linkrate_hw;
40 resp_data[41] = (phy->maximum_linkrate << 4) |
41 phy->maximum_linkrate_hw;
43 if (!sas_ha->sas_phy[phy_id]->port ||
44 !sas_ha->sas_phy[phy_id]->port->port_dev)
45 return;
47 rphy = sas_ha->sas_phy[phy_id]->port->port_dev->rphy;
48 resp_data[12] = rphy->identify.device_type << 4;
49 resp_data[14] = rphy->identify.initiator_port_protocols;
50 resp_data[15] = rphy->identify.target_port_protocols;
53 static void sas_report_phy_sata(struct sas_ha_struct *sas_ha, u8 *resp_data,
54 u8 phy_id)
56 struct sas_rphy *rphy;
57 struct dev_to_host_fis *fis;
58 int i;
60 if (phy_id >= sas_ha->num_phys) {
61 resp_data[2] = SMP_RESP_NO_PHY;
62 return;
65 resp_data[2] = SMP_RESP_PHY_NO_SATA;
67 if (!sas_ha->sas_phy[phy_id]->port)
68 return;
70 rphy = sas_ha->sas_phy[phy_id]->port->port_dev->rphy;
71 fis = (struct dev_to_host_fis *)
72 sas_ha->sas_phy[phy_id]->port->port_dev->frame_rcvd;
73 if (rphy->identify.target_port_protocols != SAS_PROTOCOL_SATA)
74 return;
76 resp_data[2] = SMP_RESP_FUNC_ACC;
77 resp_data[9] = phy_id;
78 memcpy(resp_data + 16, sas_ha->sas_phy[phy_id]->attached_sas_addr,
79 SAS_ADDR_SIZE);
81 /* check to see if we have a valid d2h fis */
82 if (fis->fis_type != 0x34)
83 return;
85 /* the d2h fis is required by the standard to be in LE format */
86 for (i = 0; i < 20; i += 4) {
87 u8 *dst = resp_data + 24 + i, *src =
88 &sas_ha->sas_phy[phy_id]->port->port_dev->frame_rcvd[i];
89 dst[0] = src[3];
90 dst[1] = src[2];
91 dst[2] = src[1];
92 dst[3] = src[0];
96 static void sas_phy_control(struct sas_ha_struct *sas_ha, u8 phy_id,
97 u8 phy_op, enum sas_linkrate min,
98 enum sas_linkrate max, u8 *resp_data)
100 struct sas_internal *i =
101 to_sas_internal(sas_ha->core.shost->transportt);
102 struct sas_phy_linkrates rates;
104 if (phy_id >= sas_ha->num_phys) {
105 resp_data[2] = SMP_RESP_NO_PHY;
106 return;
108 switch (phy_op) {
109 case PHY_FUNC_NOP:
110 case PHY_FUNC_LINK_RESET:
111 case PHY_FUNC_HARD_RESET:
112 case PHY_FUNC_DISABLE:
113 case PHY_FUNC_CLEAR_ERROR_LOG:
114 case PHY_FUNC_CLEAR_AFFIL:
115 case PHY_FUNC_TX_SATA_PS_SIGNAL:
116 break;
118 default:
119 resp_data[2] = SMP_RESP_PHY_UNK_OP;
120 return;
123 rates.minimum_linkrate = min;
124 rates.maximum_linkrate = max;
126 if (i->dft->lldd_control_phy(sas_ha->sas_phy[phy_id], phy_op, &rates))
127 resp_data[2] = SMP_RESP_FUNC_FAILED;
128 else
129 resp_data[2] = SMP_RESP_FUNC_ACC;
132 int sas_smp_host_handler(struct Scsi_Host *shost, struct request *req,
133 struct request *rsp)
135 u8 *req_data = NULL, *resp_data = NULL, *buf;
136 struct sas_ha_struct *sas_ha = SHOST_TO_SAS_HA(shost);
137 int error = -EINVAL;
139 /* eight is the minimum size for request and response frames */
140 if (req->data_len < 8 || rsp->data_len < 8)
141 goto out;
143 if (bio_offset(req->bio) + req->data_len > PAGE_SIZE ||
144 bio_offset(rsp->bio) + rsp->data_len > PAGE_SIZE) {
145 shost_printk(KERN_ERR, shost,
146 "SMP request/response frame crosses page boundary");
147 goto out;
150 req_data = kzalloc(req->data_len, GFP_KERNEL);
152 /* make sure frame can always be built ... we copy
153 * back only the requested length */
154 resp_data = kzalloc(max(rsp->data_len, 128U), GFP_KERNEL);
156 if (!req_data || !resp_data) {
157 error = -ENOMEM;
158 goto out;
161 local_irq_disable();
162 buf = kmap_atomic(bio_page(req->bio), KM_USER0) + bio_offset(req->bio);
163 memcpy(req_data, buf, req->data_len);
164 kunmap_atomic(buf - bio_offset(req->bio), KM_USER0);
165 local_irq_enable();
167 if (req_data[0] != SMP_REQUEST)
168 goto out;
170 /* always succeeds ... even if we can't process the request
171 * the result is in the response frame */
172 error = 0;
174 /* set up default don't know response */
175 resp_data[0] = SMP_RESPONSE;
176 resp_data[1] = req_data[1];
177 resp_data[2] = SMP_RESP_FUNC_UNK;
179 req->resid_len = req->data_len;
180 rsp->resid_len = rsp->data_len;
182 switch (req_data[1]) {
183 case SMP_REPORT_GENERAL:
184 req->resid_len -= 8;
185 rsp->resid_len -= 32;
186 resp_data[2] = SMP_RESP_FUNC_ACC;
187 resp_data[9] = sas_ha->num_phys;
188 break;
190 case SMP_REPORT_MANUF_INFO:
191 req->resid_len -= 8;
192 rsp->resid_len -= 64;
193 resp_data[2] = SMP_RESP_FUNC_ACC;
194 memcpy(resp_data + 12, shost->hostt->name,
195 SAS_EXPANDER_VENDOR_ID_LEN);
196 memcpy(resp_data + 20, "libsas virt phy",
197 SAS_EXPANDER_PRODUCT_ID_LEN);
198 break;
200 case SMP_READ_GPIO_REG:
201 /* FIXME: need GPIO support in the transport class */
202 break;
204 case SMP_DISCOVER:
205 req->resid_len -= 16;
206 if ((int)req->resid_len < 0) {
207 req->resid_len = 0;
208 error = -EINVAL;
209 goto out;
211 rsp->resid_len -= 56;
212 sas_host_smp_discover(sas_ha, resp_data, req_data[9]);
213 break;
215 case SMP_REPORT_PHY_ERR_LOG:
216 /* FIXME: could implement this with additional
217 * libsas callbacks providing the HW supports it */
218 break;
220 case SMP_REPORT_PHY_SATA:
221 req->resid_len -= 16;
222 if ((int)req->resid_len < 0) {
223 req->resid_len = 0;
224 error = -EINVAL;
225 goto out;
227 rsp->resid_len -= 60;
228 sas_report_phy_sata(sas_ha, resp_data, req_data[9]);
229 break;
231 case SMP_REPORT_ROUTE_INFO:
232 /* Can't implement; hosts have no routes */
233 break;
235 case SMP_WRITE_GPIO_REG:
236 /* FIXME: need GPIO support in the transport class */
237 break;
239 case SMP_CONF_ROUTE_INFO:
240 /* Can't implement; hosts have no routes */
241 break;
243 case SMP_PHY_CONTROL:
244 req->resid_len -= 44;
245 if ((int)req->resid_len < 0) {
246 req->resid_len = 0;
247 error = -EINVAL;
248 goto out;
250 rsp->resid_len -= 8;
251 sas_phy_control(sas_ha, req_data[9], req_data[10],
252 req_data[32] >> 4, req_data[33] >> 4,
253 resp_data);
254 break;
256 case SMP_PHY_TEST_FUNCTION:
257 /* FIXME: should this be implemented? */
258 break;
260 default:
261 /* probably a 2.0 function */
262 break;
265 local_irq_disable();
266 buf = kmap_atomic(bio_page(rsp->bio), KM_USER0) + bio_offset(rsp->bio);
267 memcpy(buf, resp_data, rsp->data_len);
268 flush_kernel_dcache_page(bio_page(rsp->bio));
269 kunmap_atomic(buf - bio_offset(rsp->bio), KM_USER0);
270 local_irq_enable();
272 out:
273 kfree(req_data);
274 kfree(resp_data);
275 return error;