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,
24 struct sas_rphy *rphy;
26 if (phy_id >= sas_ha->num_phys) {
27 resp_data[2] = SMP_RESP_NO_PHY;
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,
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)
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,
56 struct sas_rphy *rphy;
57 struct dev_to_host_fis *fis;
60 if (phy_id >= sas_ha->num_phys) {
61 resp_data[2] = SMP_RESP_NO_PHY;
65 resp_data[2] = SMP_RESP_PHY_NO_SATA;
67 if (!sas_ha->sas_phy[phy_id]->port)
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)
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,
81 /* check to see if we have a valid d2h fis */
82 if (fis->fis_type != 0x34)
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];
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;
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:
119 resp_data[2] = SMP_RESP_PHY_UNK_OP;
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;
129 resp_data[2] = SMP_RESP_FUNC_ACC;
132 int sas_smp_host_handler(struct Scsi_Host *shost, struct request *req,
135 u8 *req_data = NULL, *resp_data = NULL, *buf;
136 struct sas_ha_struct *sas_ha = SHOST_TO_SAS_HA(shost);
139 /* eight is the minimum size for request and response frames */
140 if (blk_rq_bytes(req) < 8 || blk_rq_bytes(rsp) < 8)
143 if (bio_offset(req->bio) + blk_rq_bytes(req) > PAGE_SIZE ||
144 bio_offset(rsp->bio) + blk_rq_bytes(rsp) > PAGE_SIZE) {
145 shost_printk(KERN_ERR, shost,
146 "SMP request/response frame crosses page boundary");
150 req_data = kzalloc(blk_rq_bytes(req), GFP_KERNEL);
152 /* make sure frame can always be built ... we copy
153 * back only the requested length */
154 resp_data = kzalloc(max(blk_rq_bytes(rsp), 128U), GFP_KERNEL);
156 if (!req_data || !resp_data) {
162 buf = kmap_atomic(bio_page(req->bio), KM_USER0) + bio_offset(req->bio);
163 memcpy(req_data, buf, blk_rq_bytes(req));
164 kunmap_atomic(buf - bio_offset(req->bio), KM_USER0);
167 if (req_data[0] != SMP_REQUEST)
170 /* always succeeds ... even if we can't process the request
171 * the result is in the response frame */
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 switch (req_data[1]) {
180 case SMP_REPORT_GENERAL:
182 rsp->resid_len -= 32;
183 resp_data[2] = SMP_RESP_FUNC_ACC;
184 resp_data[9] = sas_ha->num_phys;
187 case SMP_REPORT_MANUF_INFO:
189 rsp->resid_len -= 64;
190 resp_data[2] = SMP_RESP_FUNC_ACC;
191 memcpy(resp_data + 12, shost->hostt->name,
192 SAS_EXPANDER_VENDOR_ID_LEN);
193 memcpy(resp_data + 20, "libsas virt phy",
194 SAS_EXPANDER_PRODUCT_ID_LEN);
197 case SMP_READ_GPIO_REG:
198 /* FIXME: need GPIO support in the transport class */
202 req->resid_len -= 16;
203 if ((int)req->resid_len < 0) {
208 rsp->resid_len -= 56;
209 sas_host_smp_discover(sas_ha, resp_data, req_data[9]);
212 case SMP_REPORT_PHY_ERR_LOG:
213 /* FIXME: could implement this with additional
214 * libsas callbacks providing the HW supports it */
217 case SMP_REPORT_PHY_SATA:
218 req->resid_len -= 16;
219 if ((int)req->resid_len < 0) {
224 rsp->resid_len -= 60;
225 sas_report_phy_sata(sas_ha, resp_data, req_data[9]);
228 case SMP_REPORT_ROUTE_INFO:
229 /* Can't implement; hosts have no routes */
232 case SMP_WRITE_GPIO_REG:
233 /* FIXME: need GPIO support in the transport class */
236 case SMP_CONF_ROUTE_INFO:
237 /* Can't implement; hosts have no routes */
240 case SMP_PHY_CONTROL:
241 req->resid_len -= 44;
242 if ((int)req->resid_len < 0) {
248 sas_phy_control(sas_ha, req_data[9], req_data[10],
249 req_data[32] >> 4, req_data[33] >> 4,
253 case SMP_PHY_TEST_FUNCTION:
254 /* FIXME: should this be implemented? */
258 /* probably a 2.0 function */
263 buf = kmap_atomic(bio_page(rsp->bio), KM_USER0) + bio_offset(rsp->bio);
264 memcpy(buf, resp_data, blk_rq_bytes(rsp));
265 flush_kernel_dcache_page(bio_page(rsp->bio));
266 kunmap_atomic(buf - bio_offset(rsp->bio), KM_USER0);