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