blob: 37c3c3f5f35dfedacbf2fe192b615a789ecca904 [file] [log] [blame]
James Bottomley2908d772006-08-29 09:22:51 -05001/*
2 * Serial Attached SCSI (SAS) Expander discovery and configuration
3 *
4 * Copyright (C) 2005 Adaptec, Inc. All rights reserved.
5 * Copyright (C) 2005 Luben Tuikov <luben_tuikov@adaptec.com>
6 *
7 * This file is licensed under GPLv2.
8 *
9 * This program is free software; you can redistribute it and/or
10 * modify it under the terms of the GNU General Public License as
11 * published by the Free Software Foundation; either version 2 of the
12 * License, or (at your option) any later version.
13 *
14 * This program is distributed in the hope that it will be useful, but
15 * WITHOUT ANY WARRANTY; without even the implied warranty of
16 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
17 * General Public License for more details.
18 *
19 * You should have received a copy of the GNU General Public License
20 * along with this program; if not, write to the Free Software
21 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
22 *
23 */
24
James Bottomley2908d772006-08-29 09:22:51 -050025#include <linux/scatterlist.h>
FUJITA Tomonoriba1fc172007-07-09 12:52:08 +090026#include <linux/blkdev.h>
Tejun Heo5a0e3ad2010-03-24 17:04:11 +090027#include <linux/slab.h>
James Bottomley2908d772006-08-29 09:22:51 -050028
29#include "sas_internal.h"
30
Dan Williamsb52df412011-11-30 23:23:33 -080031#include <scsi/sas_ata.h>
James Bottomley2908d772006-08-29 09:22:51 -050032#include <scsi/scsi_transport.h>
33#include <scsi/scsi_transport_sas.h>
34#include "../scsi_sas_internal.h"
35
36static int sas_discover_expander(struct domain_device *dev);
37static int sas_configure_routing(struct domain_device *dev, u8 *sas_addr);
38static int sas_configure_phy(struct domain_device *dev, int phy_id,
39 u8 *sas_addr, int include);
40static int sas_disable_routing(struct domain_device *dev, u8 *sas_addr);
41
James Bottomley2908d772006-08-29 09:22:51 -050042/* ---------- SMP task management ---------- */
43
44static void smp_task_timedout(unsigned long _task)
45{
46 struct sas_task *task = (void *) _task;
47 unsigned long flags;
48
49 spin_lock_irqsave(&task->task_state_lock, flags);
50 if (!(task->task_state_flags & SAS_TASK_STATE_DONE))
51 task->task_state_flags |= SAS_TASK_STATE_ABORTED;
52 spin_unlock_irqrestore(&task->task_state_lock, flags);
53
54 complete(&task->completion);
55}
56
57static void smp_task_done(struct sas_task *task)
58{
59 if (!del_timer(&task->timer))
60 return;
61 complete(&task->completion);
62}
63
64/* Give it some long enough timeout. In seconds. */
65#define SMP_TIMEOUT 10
66
67static int smp_execute_task(struct domain_device *dev, void *req, int req_size,
68 void *resp, int resp_size)
69{
malahal@us.ibm.com42961ee2006-10-04 17:34:03 -070070 int res, retry;
71 struct sas_task *task = NULL;
James Bottomley2908d772006-08-29 09:22:51 -050072 struct sas_internal *i =
73 to_sas_internal(dev->port->ha->core.shost->transportt);
74
Jeff Skirvin89d3cf62011-11-16 09:44:13 +000075 mutex_lock(&dev->ex_dev.cmd_mutex);
malahal@us.ibm.com42961ee2006-10-04 17:34:03 -070076 for (retry = 0; retry < 3; retry++) {
Dan Williams3a9c5562011-12-21 15:19:56 -080077 if (test_bit(SAS_DEV_GONE, &dev->state)) {
78 res = -ECOMM;
79 break;
80 }
81
malahal@us.ibm.com42961ee2006-10-04 17:34:03 -070082 task = sas_alloc_task(GFP_KERNEL);
Jeff Skirvin89d3cf62011-11-16 09:44:13 +000083 if (!task) {
84 res = -ENOMEM;
85 break;
86 }
malahal@us.ibm.com42961ee2006-10-04 17:34:03 -070087 task->dev = dev;
88 task->task_proto = dev->tproto;
89 sg_init_one(&task->smp_task.smp_req, req, req_size);
90 sg_init_one(&task->smp_task.smp_resp, resp, resp_size);
James Bottomley2908d772006-08-29 09:22:51 -050091
malahal@us.ibm.com42961ee2006-10-04 17:34:03 -070092 task->task_done = smp_task_done;
James Bottomley2908d772006-08-29 09:22:51 -050093
malahal@us.ibm.com42961ee2006-10-04 17:34:03 -070094 task->timer.data = (unsigned long) task;
95 task->timer.function = smp_task_timedout;
96 task->timer.expires = jiffies + SMP_TIMEOUT*HZ;
97 add_timer(&task->timer);
James Bottomley2908d772006-08-29 09:22:51 -050098
malahal@us.ibm.com42961ee2006-10-04 17:34:03 -070099 res = i->dft->lldd_execute_task(task, 1, GFP_KERNEL);
James Bottomley2908d772006-08-29 09:22:51 -0500100
malahal@us.ibm.com42961ee2006-10-04 17:34:03 -0700101 if (res) {
102 del_timer(&task->timer);
103 SAS_DPRINTK("executing SMP task failed:%d\n", res);
Jeff Skirvin89d3cf62011-11-16 09:44:13 +0000104 break;
James Bottomley2908d772006-08-29 09:22:51 -0500105 }
malahal@us.ibm.com42961ee2006-10-04 17:34:03 -0700106
107 wait_for_completion(&task->completion);
James Bottomley32e8ae32007-12-30 12:37:31 -0600108 res = -ECOMM;
malahal@us.ibm.com42961ee2006-10-04 17:34:03 -0700109 if ((task->task_state_flags & SAS_TASK_STATE_ABORTED)) {
110 SAS_DPRINTK("smp task timed out or aborted\n");
111 i->dft->lldd_abort_task(task);
112 if (!(task->task_state_flags & SAS_TASK_STATE_DONE)) {
113 SAS_DPRINTK("SMP task aborted and not done\n");
Jeff Skirvin89d3cf62011-11-16 09:44:13 +0000114 break;
malahal@us.ibm.com42961ee2006-10-04 17:34:03 -0700115 }
116 }
117 if (task->task_status.resp == SAS_TASK_COMPLETE &&
James Bottomleydf64d3c2010-07-27 15:51:13 -0500118 task->task_status.stat == SAM_STAT_GOOD) {
malahal@us.ibm.com42961ee2006-10-04 17:34:03 -0700119 res = 0;
120 break;
Jeff Skirvin89d3cf62011-11-16 09:44:13 +0000121 }
122 if (task->task_status.resp == SAS_TASK_COMPLETE &&
123 task->task_status.stat == SAS_DATA_UNDERRUN) {
James Bottomley2d4b63e2007-12-29 11:49:53 -0600124 /* no error, but return the number of bytes of
125 * underrun */
126 res = task->task_status.residual;
127 break;
Jeff Skirvin89d3cf62011-11-16 09:44:13 +0000128 }
129 if (task->task_status.resp == SAS_TASK_COMPLETE &&
130 task->task_status.stat == SAS_DATA_OVERRUN) {
James Bottomley2d4b63e2007-12-29 11:49:53 -0600131 res = -EMSGSIZE;
132 break;
Dan Williams36a39942011-11-17 17:59:54 -0800133 }
134 if (task->task_status.resp == SAS_TASK_UNDELIVERED &&
135 task->task_status.stat == SAS_DEVICE_UNKNOWN)
136 break;
137 else {
malahal@us.ibm.com42961ee2006-10-04 17:34:03 -0700138 SAS_DPRINTK("%s: task to dev %016llx response: 0x%x "
Harvey Harrisoncadbd4a2008-07-03 23:47:27 -0700139 "status 0x%x\n", __func__,
malahal@us.ibm.com42961ee2006-10-04 17:34:03 -0700140 SAS_ADDR(dev->sas_addr),
141 task->task_status.resp,
142 task->task_status.stat);
143 sas_free_task(task);
144 task = NULL;
145 }
James Bottomley2908d772006-08-29 09:22:51 -0500146 }
Jeff Skirvin89d3cf62011-11-16 09:44:13 +0000147 mutex_unlock(&dev->ex_dev.cmd_mutex);
148
malahal@us.ibm.com42961ee2006-10-04 17:34:03 -0700149 BUG_ON(retry == 3 && task != NULL);
Jeff Skirvin89d3cf62011-11-16 09:44:13 +0000150 sas_free_task(task);
James Bottomley2908d772006-08-29 09:22:51 -0500151 return res;
152}
153
154/* ---------- Allocations ---------- */
155
156static inline void *alloc_smp_req(int size)
157{
158 u8 *p = kzalloc(size, GFP_KERNEL);
159 if (p)
160 p[0] = SMP_REQUEST;
161 return p;
162}
163
164static inline void *alloc_smp_resp(int size)
165{
166 return kzalloc(size, GFP_KERNEL);
167}
168
Dan Williamsd214d812012-01-16 11:56:50 -0800169static char sas_route_char(struct domain_device *dev, struct ex_phy *phy)
170{
171 switch (phy->routing_attr) {
172 case TABLE_ROUTING:
173 if (dev->ex_dev.t2t_supp)
174 return 'U';
175 else
176 return 'T';
177 case DIRECT_ROUTING:
178 return 'D';
179 case SUBTRACTIVE_ROUTING:
180 return 'S';
181 default:
182 return '?';
183 }
184}
James Bottomley2908d772006-08-29 09:22:51 -0500185
Dan Williams354cf822012-01-12 17:57:35 -0800186static enum sas_dev_type to_dev_type(struct discover_resp *dr)
James Bottomley2908d772006-08-29 09:22:51 -0500187{
Dan Williams354cf822012-01-12 17:57:35 -0800188 /* This is detecting a failure to transmit initial dev to host
189 * FIS as described in section J.5 of sas-2 r16
190 */
191 if (dr->attached_dev_type == NO_DEVICE && dr->attached_sata_dev &&
192 dr->linkrate >= SAS_LINK_RATE_1_5_GBPS)
193 return SATA_PENDING;
194 else
195 return dr->attached_dev_type;
196}
197
198static void sas_set_ex_phy(struct domain_device *dev, int phy_id, void *rsp)
199{
200 enum sas_dev_type dev_type;
201 enum sas_linkrate linkrate;
202 u8 sas_addr[SAS_ADDR_SIZE];
203 struct smp_resp *resp = rsp;
204 struct discover_resp *dr = &resp->disc;
James Bottomley2908d772006-08-29 09:22:51 -0500205 struct expander_device *ex = &dev->ex_dev;
206 struct ex_phy *phy = &ex->ex_phy[phy_id];
James Bottomley2908d772006-08-29 09:22:51 -0500207 struct sas_rphy *rphy = dev->rphy;
Dan Williamsd214d812012-01-16 11:56:50 -0800208 bool new_phy = !phy->phy;
209 char *type;
James Bottomley2908d772006-08-29 09:22:51 -0500210
Dan Williamsd214d812012-01-16 11:56:50 -0800211 if (new_phy) {
James Bottomley2908d772006-08-29 09:22:51 -0500212 phy->phy = sas_phy_alloc(&rphy->dev, phy_id);
213
214 /* FIXME: error_handling */
215 BUG_ON(!phy->phy);
216 }
217
218 switch (resp->result) {
219 case SMP_RESP_PHY_VACANT:
220 phy->phy_state = PHY_VACANT;
Jack Wang2bc72c92010-10-06 16:05:35 +0800221 break;
James Bottomley2908d772006-08-29 09:22:51 -0500222 default:
223 phy->phy_state = PHY_NOT_PRESENT;
Jack Wang2bc72c92010-10-06 16:05:35 +0800224 break;
James Bottomley2908d772006-08-29 09:22:51 -0500225 case SMP_RESP_FUNC_ACC:
226 phy->phy_state = PHY_EMPTY; /* do not know yet */
227 break;
228 }
229
Dan Williams354cf822012-01-12 17:57:35 -0800230 /* check if anything important changed to squelch debug */
231 dev_type = phy->attached_dev_type;
232 linkrate = phy->linkrate;
233 memcpy(sas_addr, phy->attached_sas_addr, SAS_ADDR_SIZE);
234
235 phy->attached_dev_type = to_dev_type(dr);
James Bottomley2908d772006-08-29 09:22:51 -0500236 phy->phy_id = phy_id;
James Bottomley2908d772006-08-29 09:22:51 -0500237 phy->linkrate = dr->linkrate;
238 phy->attached_sata_host = dr->attached_sata_host;
239 phy->attached_sata_dev = dr->attached_sata_dev;
240 phy->attached_sata_ps = dr->attached_sata_ps;
241 phy->attached_iproto = dr->iproto << 1;
242 phy->attached_tproto = dr->tproto << 1;
243 memcpy(phy->attached_sas_addr, dr->attached_sas_addr, SAS_ADDR_SIZE);
244 phy->attached_phy_id = dr->attached_phy_id;
245 phy->phy_change_count = dr->change_count;
246 phy->routing_attr = dr->routing_attr;
247 phy->virtual = dr->virtual;
248 phy->last_da_index = -1;
249
Jack Wangbb041a02011-09-23 14:32:32 +0800250 phy->phy->identify.sas_address = SAS_ADDR(phy->attached_sas_addr);
Dan Williams354cf822012-01-12 17:57:35 -0800251 phy->phy->identify.device_type = dr->attached_dev_type;
James Bottomley2908d772006-08-29 09:22:51 -0500252 phy->phy->identify.initiator_port_protocols = phy->attached_iproto;
253 phy->phy->identify.target_port_protocols = phy->attached_tproto;
Dan Williams77c309f2012-02-08 23:20:41 -0800254 if (!phy->attached_tproto && dr->attached_sata_dev)
255 phy->phy->identify.target_port_protocols = SAS_PROTOCOL_SATA;
James Bottomley2908d772006-08-29 09:22:51 -0500256 phy->phy->identify.phy_identifier = phy_id;
James Bottomleya01e70e2006-09-06 19:28:07 -0500257 phy->phy->minimum_linkrate_hw = dr->hmin_linkrate;
258 phy->phy->maximum_linkrate_hw = dr->hmax_linkrate;
259 phy->phy->minimum_linkrate = dr->pmin_linkrate;
260 phy->phy->maximum_linkrate = dr->pmax_linkrate;
James Bottomley88edf742006-09-06 17:36:13 -0500261 phy->phy->negotiated_linkrate = phy->linkrate;
James Bottomley2908d772006-08-29 09:22:51 -0500262
Dan Williamsd214d812012-01-16 11:56:50 -0800263 if (new_phy)
Jack Wang2bc72c92010-10-06 16:05:35 +0800264 if (sas_phy_add(phy->phy)) {
265 sas_phy_free(phy->phy);
266 return;
267 }
James Bottomley2908d772006-08-29 09:22:51 -0500268
Dan Williamsd214d812012-01-16 11:56:50 -0800269 switch (phy->attached_dev_type) {
Dan Williams354cf822012-01-12 17:57:35 -0800270 case SATA_PENDING:
271 type = "stp pending";
272 break;
Dan Williamsd214d812012-01-16 11:56:50 -0800273 case NO_DEVICE:
274 type = "no device";
275 break;
276 case SAS_END_DEV:
277 if (phy->attached_iproto) {
278 if (phy->attached_tproto)
279 type = "host+target";
280 else
281 type = "host";
282 } else {
283 if (dr->attached_sata_dev)
284 type = "stp";
285 else
286 type = "ssp";
287 }
288 break;
289 case EDGE_DEV:
290 case FANOUT_DEV:
291 type = "smp";
292 break;
293 default:
294 type = "unknown";
295 }
James Bottomley2908d772006-08-29 09:22:51 -0500296
Dan Williams354cf822012-01-12 17:57:35 -0800297 /* this routine is polled by libata error recovery so filter
298 * unimportant messages
299 */
300 if (new_phy || phy->attached_dev_type != dev_type ||
301 phy->linkrate != linkrate ||
302 SAS_ADDR(phy->attached_sas_addr) != SAS_ADDR(sas_addr))
303 /* pass */;
304 else
305 return;
306
Dan Williamsd214d812012-01-16 11:56:50 -0800307 SAS_DPRINTK("ex %016llx phy%02d:%c:%X attached: %016llx (%s)\n",
308 SAS_ADDR(dev->sas_addr), phy->phy_id,
309 sas_route_char(dev, phy), phy->linkrate,
310 SAS_ADDR(phy->attached_sas_addr), type);
James Bottomley2908d772006-08-29 09:22:51 -0500311}
312
Dan Williamsb52df412011-11-30 23:23:33 -0800313/* check if we have an existing attached ata device on this expander phy */
Dan Williams81c757b2011-12-02 16:07:01 -0800314struct domain_device *sas_ex_to_ata(struct domain_device *ex_dev, int phy_id)
Dan Williamsb52df412011-11-30 23:23:33 -0800315{
316 struct ex_phy *ex_phy = &ex_dev->ex_dev.ex_phy[phy_id];
317 struct domain_device *dev;
318 struct sas_rphy *rphy;
319
320 if (!ex_phy->port)
321 return NULL;
322
323 rphy = ex_phy->port->rphy;
324 if (!rphy)
325 return NULL;
326
327 dev = sas_find_dev_by_rphy(rphy);
328
329 if (dev && dev_is_sata(dev))
330 return dev;
331
332 return NULL;
333}
334
James Bottomley2908d772006-08-29 09:22:51 -0500335#define DISCOVER_REQ_SIZE 16
336#define DISCOVER_RESP_SIZE 56
337
James Bottomley1acce192006-08-22 12:39:19 -0500338static int sas_ex_phy_discover_helper(struct domain_device *dev, u8 *disc_req,
339 u8 *disc_resp, int single)
340{
Dan Williams354cf822012-01-12 17:57:35 -0800341 struct discover_resp *dr;
342 int res;
James Bottomley1acce192006-08-22 12:39:19 -0500343
344 disc_req[9] = single;
James Bottomley1acce192006-08-22 12:39:19 -0500345
Dan Williams354cf822012-01-12 17:57:35 -0800346 res = smp_execute_task(dev, disc_req, DISCOVER_REQ_SIZE,
347 disc_resp, DISCOVER_RESP_SIZE);
348 if (res)
349 return res;
350 dr = &((struct smp_resp *)disc_resp)->disc;
351 if (memcmp(dev->sas_addr, dr->attached_sas_addr, SAS_ADDR_SIZE) == 0) {
352 sas_printk("Found loopback topology, just ignore it!\n");
353 return 0;
James Bottomley1acce192006-08-22 12:39:19 -0500354 }
355 sas_set_ex_phy(dev, single, disc_resp);
356 return 0;
357}
358
Dan Williams354cf822012-01-12 17:57:35 -0800359int sas_ex_phy_discover(struct domain_device *dev, int single)
James Bottomley2908d772006-08-29 09:22:51 -0500360{
361 struct expander_device *ex = &dev->ex_dev;
362 int res = 0;
363 u8 *disc_req;
364 u8 *disc_resp;
365
366 disc_req = alloc_smp_req(DISCOVER_REQ_SIZE);
367 if (!disc_req)
368 return -ENOMEM;
369
370 disc_resp = alloc_smp_req(DISCOVER_RESP_SIZE);
371 if (!disc_resp) {
372 kfree(disc_req);
373 return -ENOMEM;
374 }
375
376 disc_req[1] = SMP_DISCOVER;
377
378 if (0 <= single && single < ex->num_phys) {
James Bottomley1acce192006-08-22 12:39:19 -0500379 res = sas_ex_phy_discover_helper(dev, disc_req, disc_resp, single);
James Bottomley2908d772006-08-29 09:22:51 -0500380 } else {
381 int i;
382
383 for (i = 0; i < ex->num_phys; i++) {
James Bottomley1acce192006-08-22 12:39:19 -0500384 res = sas_ex_phy_discover_helper(dev, disc_req,
385 disc_resp, i);
James Bottomley2908d772006-08-29 09:22:51 -0500386 if (res)
387 goto out_err;
James Bottomley2908d772006-08-29 09:22:51 -0500388 }
389 }
390out_err:
391 kfree(disc_resp);
392 kfree(disc_req);
393 return res;
394}
395
396static int sas_expander_discover(struct domain_device *dev)
397{
398 struct expander_device *ex = &dev->ex_dev;
399 int res = -ENOMEM;
400
401 ex->ex_phy = kzalloc(sizeof(*ex->ex_phy)*ex->num_phys, GFP_KERNEL);
402 if (!ex->ex_phy)
403 return -ENOMEM;
404
405 res = sas_ex_phy_discover(dev, -1);
406 if (res)
407 goto out_err;
408
409 return 0;
410 out_err:
411 kfree(ex->ex_phy);
412 ex->ex_phy = NULL;
413 return res;
414}
415
416#define MAX_EXPANDER_PHYS 128
417
418static void ex_assign_report_general(struct domain_device *dev,
419 struct smp_resp *resp)
420{
421 struct report_general_resp *rg = &resp->rg;
422
423 dev->ex_dev.ex_change_count = be16_to_cpu(rg->change_count);
424 dev->ex_dev.max_route_indexes = be16_to_cpu(rg->route_indexes);
425 dev->ex_dev.num_phys = min(rg->num_phys, (u8)MAX_EXPANDER_PHYS);
Luben Tuikovffaac8f2011-09-22 09:41:36 -0700426 dev->ex_dev.t2t_supp = rg->t2t_supp;
James Bottomley2908d772006-08-29 09:22:51 -0500427 dev->ex_dev.conf_route_table = rg->conf_route_table;
428 dev->ex_dev.configuring = rg->configuring;
429 memcpy(dev->ex_dev.enclosure_logical_id, rg->enclosure_logical_id, 8);
430}
431
432#define RG_REQ_SIZE 8
433#define RG_RESP_SIZE 32
434
435static int sas_ex_general(struct domain_device *dev)
436{
437 u8 *rg_req;
438 struct smp_resp *rg_resp;
439 int res;
440 int i;
441
442 rg_req = alloc_smp_req(RG_REQ_SIZE);
443 if (!rg_req)
444 return -ENOMEM;
445
446 rg_resp = alloc_smp_resp(RG_RESP_SIZE);
447 if (!rg_resp) {
448 kfree(rg_req);
449 return -ENOMEM;
450 }
451
452 rg_req[1] = SMP_REPORT_GENERAL;
453
454 for (i = 0; i < 5; i++) {
455 res = smp_execute_task(dev, rg_req, RG_REQ_SIZE, rg_resp,
456 RG_RESP_SIZE);
457
458 if (res) {
459 SAS_DPRINTK("RG to ex %016llx failed:0x%x\n",
460 SAS_ADDR(dev->sas_addr), res);
461 goto out;
462 } else if (rg_resp->result != SMP_RESP_FUNC_ACC) {
463 SAS_DPRINTK("RG:ex %016llx returned SMP result:0x%x\n",
464 SAS_ADDR(dev->sas_addr), rg_resp->result);
465 res = rg_resp->result;
466 goto out;
467 }
468
469 ex_assign_report_general(dev, rg_resp);
470
471 if (dev->ex_dev.configuring) {
472 SAS_DPRINTK("RG: ex %llx self-configuring...\n",
473 SAS_ADDR(dev->sas_addr));
474 schedule_timeout_interruptible(5*HZ);
475 } else
476 break;
477 }
478out:
479 kfree(rg_req);
480 kfree(rg_resp);
481 return res;
482}
483
484static void ex_assign_manuf_info(struct domain_device *dev, void
485 *_mi_resp)
486{
487 u8 *mi_resp = _mi_resp;
488 struct sas_rphy *rphy = dev->rphy;
489 struct sas_expander_device *edev = rphy_to_expander_device(rphy);
490
491 memcpy(edev->vendor_id, mi_resp + 12, SAS_EXPANDER_VENDOR_ID_LEN);
492 memcpy(edev->product_id, mi_resp + 20, SAS_EXPANDER_PRODUCT_ID_LEN);
493 memcpy(edev->product_rev, mi_resp + 36,
494 SAS_EXPANDER_PRODUCT_REV_LEN);
495
496 if (mi_resp[8] & 1) {
497 memcpy(edev->component_vendor_id, mi_resp + 40,
498 SAS_EXPANDER_COMPONENT_VENDOR_ID_LEN);
499 edev->component_id = mi_resp[48] << 8 | mi_resp[49];
500 edev->component_revision_id = mi_resp[50];
501 }
502}
503
504#define MI_REQ_SIZE 8
505#define MI_RESP_SIZE 64
506
507static int sas_ex_manuf_info(struct domain_device *dev)
508{
509 u8 *mi_req;
510 u8 *mi_resp;
511 int res;
512
513 mi_req = alloc_smp_req(MI_REQ_SIZE);
514 if (!mi_req)
515 return -ENOMEM;
516
517 mi_resp = alloc_smp_resp(MI_RESP_SIZE);
518 if (!mi_resp) {
519 kfree(mi_req);
520 return -ENOMEM;
521 }
522
523 mi_req[1] = SMP_REPORT_MANUF_INFO;
524
525 res = smp_execute_task(dev, mi_req, MI_REQ_SIZE, mi_resp,MI_RESP_SIZE);
526 if (res) {
527 SAS_DPRINTK("MI: ex %016llx failed:0x%x\n",
528 SAS_ADDR(dev->sas_addr), res);
529 goto out;
530 } else if (mi_resp[2] != SMP_RESP_FUNC_ACC) {
531 SAS_DPRINTK("MI ex %016llx returned SMP result:0x%x\n",
532 SAS_ADDR(dev->sas_addr), mi_resp[2]);
533 goto out;
534 }
535
536 ex_assign_manuf_info(dev, mi_resp);
537out:
538 kfree(mi_req);
539 kfree(mi_resp);
540 return res;
541}
542
543#define PC_REQ_SIZE 44
544#define PC_RESP_SIZE 8
545
546int sas_smp_phy_control(struct domain_device *dev, int phy_id,
James Bottomleya01e70e2006-09-06 19:28:07 -0500547 enum phy_func phy_func,
548 struct sas_phy_linkrates *rates)
James Bottomley2908d772006-08-29 09:22:51 -0500549{
550 u8 *pc_req;
551 u8 *pc_resp;
552 int res;
553
554 pc_req = alloc_smp_req(PC_REQ_SIZE);
555 if (!pc_req)
556 return -ENOMEM;
557
558 pc_resp = alloc_smp_resp(PC_RESP_SIZE);
559 if (!pc_resp) {
560 kfree(pc_req);
561 return -ENOMEM;
562 }
563
564 pc_req[1] = SMP_PHY_CONTROL;
565 pc_req[9] = phy_id;
566 pc_req[10]= phy_func;
James Bottomleya01e70e2006-09-06 19:28:07 -0500567 if (rates) {
568 pc_req[32] = rates->minimum_linkrate << 4;
569 pc_req[33] = rates->maximum_linkrate << 4;
570 }
James Bottomley2908d772006-08-29 09:22:51 -0500571
572 res = smp_execute_task(dev, pc_req, PC_REQ_SIZE, pc_resp,PC_RESP_SIZE);
573
574 kfree(pc_resp);
575 kfree(pc_req);
576 return res;
577}
578
579static void sas_ex_disable_phy(struct domain_device *dev, int phy_id)
580{
581 struct expander_device *ex = &dev->ex_dev;
582 struct ex_phy *phy = &ex->ex_phy[phy_id];
583
James Bottomleya01e70e2006-09-06 19:28:07 -0500584 sas_smp_phy_control(dev, phy_id, PHY_FUNC_DISABLE, NULL);
James Bottomley88edf742006-09-06 17:36:13 -0500585 phy->linkrate = SAS_PHY_DISABLED;
James Bottomley2908d772006-08-29 09:22:51 -0500586}
587
588static void sas_ex_disable_port(struct domain_device *dev, u8 *sas_addr)
589{
590 struct expander_device *ex = &dev->ex_dev;
591 int i;
592
593 for (i = 0; i < ex->num_phys; i++) {
594 struct ex_phy *phy = &ex->ex_phy[i];
595
596 if (phy->phy_state == PHY_VACANT ||
597 phy->phy_state == PHY_NOT_PRESENT)
598 continue;
599
600 if (SAS_ADDR(phy->attached_sas_addr) == SAS_ADDR(sas_addr))
601 sas_ex_disable_phy(dev, i);
602 }
603}
604
605static int sas_dev_present_in_domain(struct asd_sas_port *port,
606 u8 *sas_addr)
607{
608 struct domain_device *dev;
609
610 if (SAS_ADDR(port->sas_addr) == SAS_ADDR(sas_addr))
611 return 1;
612 list_for_each_entry(dev, &port->dev_list, dev_list_node) {
613 if (SAS_ADDR(dev->sas_addr) == SAS_ADDR(sas_addr))
614 return 1;
615 }
616 return 0;
617}
618
619#define RPEL_REQ_SIZE 16
620#define RPEL_RESP_SIZE 32
621int sas_smp_get_phy_events(struct sas_phy *phy)
622{
623 int res;
Jesper Juhl92631fa2007-07-28 01:13:33 +0200624 u8 *req;
625 u8 *resp;
James Bottomley2908d772006-08-29 09:22:51 -0500626 struct sas_rphy *rphy = dev_to_rphy(phy->dev.parent);
627 struct domain_device *dev = sas_find_dev_by_rphy(rphy);
James Bottomley2908d772006-08-29 09:22:51 -0500628
Jesper Juhl92631fa2007-07-28 01:13:33 +0200629 req = alloc_smp_req(RPEL_REQ_SIZE);
630 if (!req)
James Bottomley2908d772006-08-29 09:22:51 -0500631 return -ENOMEM;
632
Jesper Juhl92631fa2007-07-28 01:13:33 +0200633 resp = alloc_smp_resp(RPEL_RESP_SIZE);
634 if (!resp) {
635 kfree(req);
636 return -ENOMEM;
637 }
638
James Bottomley2908d772006-08-29 09:22:51 -0500639 req[1] = SMP_REPORT_PHY_ERR_LOG;
640 req[9] = phy->number;
641
642 res = smp_execute_task(dev, req, RPEL_REQ_SIZE,
643 resp, RPEL_RESP_SIZE);
644
645 if (!res)
646 goto out;
647
648 phy->invalid_dword_count = scsi_to_u32(&resp[12]);
649 phy->running_disparity_error_count = scsi_to_u32(&resp[16]);
650 phy->loss_of_dword_sync_count = scsi_to_u32(&resp[20]);
651 phy->phy_reset_problem_count = scsi_to_u32(&resp[24]);
652
653 out:
654 kfree(resp);
655 return res;
656
657}
658
James Bottomleyb9142172007-07-22 13:15:55 -0500659#ifdef CONFIG_SCSI_SAS_ATA
660
James Bottomley2908d772006-08-29 09:22:51 -0500661#define RPS_REQ_SIZE 16
662#define RPS_RESP_SIZE 60
663
Dan Williams354cf822012-01-12 17:57:35 -0800664int sas_get_report_phy_sata(struct domain_device *dev, int phy_id,
665 struct smp_resp *rps_resp)
James Bottomley2908d772006-08-29 09:22:51 -0500666{
667 int res;
668 u8 *rps_req = alloc_smp_req(RPS_REQ_SIZE);
James Bottomley1acce192006-08-22 12:39:19 -0500669 u8 *resp = (u8 *)rps_resp;
James Bottomley2908d772006-08-29 09:22:51 -0500670
671 if (!rps_req)
672 return -ENOMEM;
673
674 rps_req[1] = SMP_REPORT_PHY_SATA;
675 rps_req[9] = phy_id;
676
677 res = smp_execute_task(dev, rps_req, RPS_REQ_SIZE,
678 rps_resp, RPS_RESP_SIZE);
679
James Bottomley1acce192006-08-22 12:39:19 -0500680 /* 0x34 is the FIS type for the D2H fis. There's a potential
681 * standards cockup here. sas-2 explicitly specifies the FIS
682 * should be encoded so that FIS type is in resp[24].
683 * However, some expanders endian reverse this. Undo the
684 * reversal here */
685 if (!res && resp[27] == 0x34 && resp[24] != 0x34) {
686 int i;
687
688 for (i = 0; i < 5; i++) {
689 int j = 24 + (i*4);
690 u8 a, b;
691 a = resp[j + 0];
692 b = resp[j + 1];
693 resp[j + 0] = resp[j + 3];
694 resp[j + 1] = resp[j + 2];
695 resp[j + 2] = b;
696 resp[j + 3] = a;
697 }
698 }
699
James Bottomley2908d772006-08-29 09:22:51 -0500700 kfree(rps_req);
James Bottomley1acce192006-08-22 12:39:19 -0500701 return res;
James Bottomley2908d772006-08-29 09:22:51 -0500702}
James Bottomleyb9142172007-07-22 13:15:55 -0500703#endif
James Bottomley2908d772006-08-29 09:22:51 -0500704
705static void sas_ex_get_linkrate(struct domain_device *parent,
706 struct domain_device *child,
707 struct ex_phy *parent_phy)
708{
709 struct expander_device *parent_ex = &parent->ex_dev;
710 struct sas_port *port;
711 int i;
712
713 child->pathways = 0;
714
715 port = parent_phy->port;
716
717 for (i = 0; i < parent_ex->num_phys; i++) {
718 struct ex_phy *phy = &parent_ex->ex_phy[i];
719
720 if (phy->phy_state == PHY_VACANT ||
721 phy->phy_state == PHY_NOT_PRESENT)
722 continue;
723
724 if (SAS_ADDR(phy->attached_sas_addr) ==
725 SAS_ADDR(child->sas_addr)) {
726
727 child->min_linkrate = min(parent->min_linkrate,
728 phy->linkrate);
729 child->max_linkrate = max(parent->max_linkrate,
730 phy->linkrate);
731 child->pathways++;
732 sas_port_add_phy(port, phy->phy);
733 }
734 }
735 child->linkrate = min(parent_phy->linkrate, child->max_linkrate);
736 child->pathways = min(child->pathways, parent->pathways);
737}
738
739static struct domain_device *sas_ex_discover_end_dev(
740 struct domain_device *parent, int phy_id)
741{
742 struct expander_device *parent_ex = &parent->ex_dev;
743 struct ex_phy *phy = &parent_ex->ex_phy[phy_id];
744 struct domain_device *child = NULL;
745 struct sas_rphy *rphy;
746 int res;
747
748 if (phy->attached_sata_host || phy->attached_sata_ps)
749 return NULL;
750
Dan Williams735f7d22011-11-17 17:59:47 -0800751 child = sas_alloc_device();
James Bottomley2908d772006-08-29 09:22:51 -0500752 if (!child)
753 return NULL;
754
Dan Williams735f7d22011-11-17 17:59:47 -0800755 kref_get(&parent->kref);
James Bottomley2908d772006-08-29 09:22:51 -0500756 child->parent = parent;
757 child->port = parent->port;
758 child->iproto = phy->attached_iproto;
759 memcpy(child->sas_addr, phy->attached_sas_addr, SAS_ADDR_SIZE);
760 sas_hash_addr(child->hashed_sas_addr, child->sas_addr);
James Bottomley024879e2006-11-15 18:03:07 -0600761 if (!phy->port) {
762 phy->port = sas_port_alloc(&parent->rphy->dev, phy_id);
763 if (unlikely(!phy->port))
764 goto out_err;
765 if (unlikely(sas_port_add(phy->port) != 0)) {
766 sas_port_free(phy->port);
767 goto out_err;
768 }
769 }
James Bottomley2908d772006-08-29 09:22:51 -0500770 sas_ex_get_linkrate(parent, child, phy);
Dan Williamsf41a0c42011-12-21 21:33:17 -0800771 sas_device_set_phy(child, phy->port);
James Bottomley2908d772006-08-29 09:22:51 -0500772
James Bottomleyb9142172007-07-22 13:15:55 -0500773#ifdef CONFIG_SCSI_SAS_ATA
Darrick J. Wong5929faf2007-11-05 11:51:17 -0800774 if ((phy->attached_tproto & SAS_PROTOCOL_STP) || phy->attached_sata_dev) {
Dan Williams354cf822012-01-12 17:57:35 -0800775 res = sas_get_ata_info(child, phy);
776 if (res)
James Bottomley024879e2006-11-15 18:03:07 -0600777 goto out_free;
James Bottomley1acce192006-08-22 12:39:19 -0500778
779 rphy = sas_end_device_alloc(phy->port);
James Bottomley528fd552006-10-16 10:57:05 -0500780 if (unlikely(!rphy))
781 goto out_free;
James Bottomley1acce192006-08-22 12:39:19 -0500782
James Bottomley2908d772006-08-29 09:22:51 -0500783 sas_init_dev(child);
James Bottomley1acce192006-08-22 12:39:19 -0500784
785 child->rphy = rphy;
Dan Williams94876692012-03-20 10:53:24 -0700786 get_device(&rphy->dev);
James Bottomley1acce192006-08-22 12:39:19 -0500787
Dan Williams87c83312011-11-17 17:59:51 -0800788 list_add_tail(&child->disco_list_node, &parent->port->disco_list);
James Bottomley1acce192006-08-22 12:39:19 -0500789
James Bottomley2908d772006-08-29 09:22:51 -0500790 res = sas_discover_sata(child);
791 if (res) {
792 SAS_DPRINTK("sas_discover_sata() for device %16llx at "
793 "%016llx:0x%x returned 0x%x\n",
794 SAS_ADDR(child->sas_addr),
795 SAS_ADDR(parent->sas_addr), phy_id, res);
James Bottomley1acce192006-08-22 12:39:19 -0500796 goto out_list_del;
James Bottomley2908d772006-08-29 09:22:51 -0500797 }
James Bottomleyb9142172007-07-22 13:15:55 -0500798 } else
799#endif
Darrick J. Wong5929faf2007-11-05 11:51:17 -0800800 if (phy->attached_tproto & SAS_PROTOCOL_SSP) {
James Bottomley2908d772006-08-29 09:22:51 -0500801 child->dev_type = SAS_END_DEV;
802 rphy = sas_end_device_alloc(phy->port);
803 /* FIXME: error handling */
James Bottomley024879e2006-11-15 18:03:07 -0600804 if (unlikely(!rphy))
805 goto out_free;
James Bottomley2908d772006-08-29 09:22:51 -0500806 child->tproto = phy->attached_tproto;
807 sas_init_dev(child);
808
809 child->rphy = rphy;
Dan Williams94876692012-03-20 10:53:24 -0700810 get_device(&rphy->dev);
James Bottomley2908d772006-08-29 09:22:51 -0500811 sas_fill_in_rphy(child, rphy);
812
Dan Williams92625f92012-01-18 20:14:01 -0800813 list_add_tail(&child->disco_list_node, &parent->port->disco_list);
James Bottomley2908d772006-08-29 09:22:51 -0500814
815 res = sas_discover_end_dev(child);
816 if (res) {
817 SAS_DPRINTK("sas_discover_end_dev() for device %16llx "
818 "at %016llx:0x%x returned 0x%x\n",
819 SAS_ADDR(child->sas_addr),
820 SAS_ADDR(parent->sas_addr), phy_id, res);
James Bottomley024879e2006-11-15 18:03:07 -0600821 goto out_list_del;
James Bottomley2908d772006-08-29 09:22:51 -0500822 }
823 } else {
824 SAS_DPRINTK("target proto 0x%x at %016llx:0x%x not handled\n",
825 phy->attached_tproto, SAS_ADDR(parent->sas_addr),
826 phy_id);
James Bottomleyb9142172007-07-22 13:15:55 -0500827 goto out_free;
James Bottomley2908d772006-08-29 09:22:51 -0500828 }
829
830 list_add_tail(&child->siblings, &parent_ex->children);
831 return child;
James Bottomley024879e2006-11-15 18:03:07 -0600832
833 out_list_del:
Darrick J. Wong6f63caa2007-01-26 14:08:43 -0800834 sas_rphy_free(child->rphy);
Dan Williams87c83312011-11-17 17:59:51 -0800835 list_del(&child->disco_list_node);
Dan Williams1a34c062011-09-21 22:05:34 -0700836 spin_lock_irq(&parent->port->dev_list_lock);
James Bottomley024879e2006-11-15 18:03:07 -0600837 list_del(&child->dev_list_node);
Dan Williams1a34c062011-09-21 22:05:34 -0700838 spin_unlock_irq(&parent->port->dev_list_lock);
James Bottomley024879e2006-11-15 18:03:07 -0600839 out_free:
840 sas_port_delete(phy->port);
841 out_err:
842 phy->port = NULL;
Dan Williams735f7d22011-11-17 17:59:47 -0800843 sas_put_device(child);
James Bottomley024879e2006-11-15 18:03:07 -0600844 return NULL;
James Bottomley2908d772006-08-29 09:22:51 -0500845}
846
Darrick J. Wong423f7cf2007-01-30 12:07:27 -0800847/* See if this phy is part of a wide port */
848static int sas_ex_join_wide_port(struct domain_device *parent, int phy_id)
849{
850 struct ex_phy *phy = &parent->ex_dev.ex_phy[phy_id];
851 int i;
852
853 for (i = 0; i < parent->ex_dev.num_phys; i++) {
854 struct ex_phy *ephy = &parent->ex_dev.ex_phy[i];
855
856 if (ephy == phy)
857 continue;
858
859 if (!memcmp(phy->attached_sas_addr, ephy->attached_sas_addr,
860 SAS_ADDR_SIZE) && ephy->port) {
861 sas_port_add_phy(ephy->port, phy->phy);
Tom Peng19252de2009-07-17 16:02:04 +0800862 phy->port = ephy->port;
Darrick J. Wong423f7cf2007-01-30 12:07:27 -0800863 phy->phy_state = PHY_DEVICE_DISCOVERED;
864 return 0;
865 }
866 }
867
868 return -ENODEV;
869}
870
James Bottomley2908d772006-08-29 09:22:51 -0500871static struct domain_device *sas_ex_discover_expander(
872 struct domain_device *parent, int phy_id)
873{
874 struct sas_expander_device *parent_ex = rphy_to_expander_device(parent->rphy);
875 struct ex_phy *phy = &parent->ex_dev.ex_phy[phy_id];
876 struct domain_device *child = NULL;
877 struct sas_rphy *rphy;
878 struct sas_expander_device *edev;
879 struct asd_sas_port *port;
880 int res;
881
882 if (phy->routing_attr == DIRECT_ROUTING) {
883 SAS_DPRINTK("ex %016llx:0x%x:D <--> ex %016llx:0x%x is not "
884 "allowed\n",
885 SAS_ADDR(parent->sas_addr), phy_id,
886 SAS_ADDR(phy->attached_sas_addr),
887 phy->attached_phy_id);
888 return NULL;
889 }
Dan Williams735f7d22011-11-17 17:59:47 -0800890 child = sas_alloc_device();
James Bottomley2908d772006-08-29 09:22:51 -0500891 if (!child)
892 return NULL;
893
894 phy->port = sas_port_alloc(&parent->rphy->dev, phy_id);
895 /* FIXME: better error handling */
896 BUG_ON(sas_port_add(phy->port) != 0);
897
898
899 switch (phy->attached_dev_type) {
900 case EDGE_DEV:
901 rphy = sas_expander_alloc(phy->port,
902 SAS_EDGE_EXPANDER_DEVICE);
903 break;
904 case FANOUT_DEV:
905 rphy = sas_expander_alloc(phy->port,
906 SAS_FANOUT_EXPANDER_DEVICE);
907 break;
908 default:
909 rphy = NULL; /* shut gcc up */
910 BUG();
911 }
912 port = parent->port;
913 child->rphy = rphy;
Dan Williams94876692012-03-20 10:53:24 -0700914 get_device(&rphy->dev);
James Bottomley2908d772006-08-29 09:22:51 -0500915 edev = rphy_to_expander_device(rphy);
916 child->dev_type = phy->attached_dev_type;
Dan Williams735f7d22011-11-17 17:59:47 -0800917 kref_get(&parent->kref);
James Bottomley2908d772006-08-29 09:22:51 -0500918 child->parent = parent;
919 child->port = port;
920 child->iproto = phy->attached_iproto;
921 child->tproto = phy->attached_tproto;
922 memcpy(child->sas_addr, phy->attached_sas_addr, SAS_ADDR_SIZE);
923 sas_hash_addr(child->hashed_sas_addr, child->sas_addr);
924 sas_ex_get_linkrate(parent, child, phy);
925 edev->level = parent_ex->level + 1;
926 parent->port->disc.max_level = max(parent->port->disc.max_level,
927 edev->level);
928 sas_init_dev(child);
929 sas_fill_in_rphy(child, rphy);
930 sas_rphy_add(rphy);
931
James Bottomley9d720d82007-07-16 13:15:51 -0500932 spin_lock_irq(&parent->port->dev_list_lock);
James Bottomley2908d772006-08-29 09:22:51 -0500933 list_add_tail(&child->dev_list_node, &parent->port->dev_list);
James Bottomley9d720d82007-07-16 13:15:51 -0500934 spin_unlock_irq(&parent->port->dev_list_lock);
James Bottomley2908d772006-08-29 09:22:51 -0500935
936 res = sas_discover_expander(child);
937 if (res) {
Dan Williams94876692012-03-20 10:53:24 -0700938 sas_rphy_delete(rphy);
Luben Tuikov5911e962011-07-26 23:10:48 -0700939 spin_lock_irq(&parent->port->dev_list_lock);
940 list_del(&child->dev_list_node);
941 spin_unlock_irq(&parent->port->dev_list_lock);
Dan Williams735f7d22011-11-17 17:59:47 -0800942 sas_put_device(child);
James Bottomley2908d772006-08-29 09:22:51 -0500943 return NULL;
944 }
945 list_add_tail(&child->siblings, &parent->ex_dev.children);
946 return child;
947}
948
949static int sas_ex_discover_dev(struct domain_device *dev, int phy_id)
950{
951 struct expander_device *ex = &dev->ex_dev;
952 struct ex_phy *ex_phy = &ex->ex_phy[phy_id];
953 struct domain_device *child = NULL;
954 int res = 0;
955
956 /* Phy state */
James Bottomley88edf742006-09-06 17:36:13 -0500957 if (ex_phy->linkrate == SAS_SATA_SPINUP_HOLD) {
James Bottomleya01e70e2006-09-06 19:28:07 -0500958 if (!sas_smp_phy_control(dev, phy_id, PHY_FUNC_LINK_RESET, NULL))
James Bottomley2908d772006-08-29 09:22:51 -0500959 res = sas_ex_phy_discover(dev, phy_id);
960 if (res)
961 return res;
962 }
963
964 /* Parent and domain coherency */
965 if (!dev->parent && (SAS_ADDR(ex_phy->attached_sas_addr) ==
966 SAS_ADDR(dev->port->sas_addr))) {
967 sas_add_parent_port(dev, phy_id);
968 return 0;
969 }
970 if (dev->parent && (SAS_ADDR(ex_phy->attached_sas_addr) ==
971 SAS_ADDR(dev->parent->sas_addr))) {
972 sas_add_parent_port(dev, phy_id);
973 if (ex_phy->routing_attr == TABLE_ROUTING)
974 sas_configure_phy(dev, phy_id, dev->port->sas_addr, 1);
975 return 0;
976 }
977
978 if (sas_dev_present_in_domain(dev->port, ex_phy->attached_sas_addr))
979 sas_ex_disable_port(dev, ex_phy->attached_sas_addr);
980
981 if (ex_phy->attached_dev_type == NO_DEVICE) {
982 if (ex_phy->routing_attr == DIRECT_ROUTING) {
983 memset(ex_phy->attached_sas_addr, 0, SAS_ADDR_SIZE);
984 sas_configure_routing(dev, ex_phy->attached_sas_addr);
985 }
986 return 0;
James Bottomley88edf742006-09-06 17:36:13 -0500987 } else if (ex_phy->linkrate == SAS_LINK_RATE_UNKNOWN)
James Bottomley2908d772006-08-29 09:22:51 -0500988 return 0;
989
990 if (ex_phy->attached_dev_type != SAS_END_DEV &&
991 ex_phy->attached_dev_type != FANOUT_DEV &&
Dan Williams354cf822012-01-12 17:57:35 -0800992 ex_phy->attached_dev_type != EDGE_DEV &&
993 ex_phy->attached_dev_type != SATA_PENDING) {
James Bottomley2908d772006-08-29 09:22:51 -0500994 SAS_DPRINTK("unknown device type(0x%x) attached to ex %016llx "
995 "phy 0x%x\n", ex_phy->attached_dev_type,
996 SAS_ADDR(dev->sas_addr),
997 phy_id);
998 return 0;
999 }
1000
1001 res = sas_configure_routing(dev, ex_phy->attached_sas_addr);
1002 if (res) {
1003 SAS_DPRINTK("configure routing for dev %016llx "
1004 "reported 0x%x. Forgotten\n",
1005 SAS_ADDR(ex_phy->attached_sas_addr), res);
1006 sas_disable_routing(dev, ex_phy->attached_sas_addr);
1007 return res;
1008 }
1009
Darrick J. Wong423f7cf2007-01-30 12:07:27 -08001010 res = sas_ex_join_wide_port(dev, phy_id);
1011 if (!res) {
1012 SAS_DPRINTK("Attaching ex phy%d to wide port %016llx\n",
1013 phy_id, SAS_ADDR(ex_phy->attached_sas_addr));
1014 return res;
1015 }
1016
James Bottomley2908d772006-08-29 09:22:51 -05001017 switch (ex_phy->attached_dev_type) {
1018 case SAS_END_DEV:
Dan Williams354cf822012-01-12 17:57:35 -08001019 case SATA_PENDING:
James Bottomley2908d772006-08-29 09:22:51 -05001020 child = sas_ex_discover_end_dev(dev, phy_id);
1021 break;
1022 case FANOUT_DEV:
1023 if (SAS_ADDR(dev->port->disc.fanout_sas_addr)) {
1024 SAS_DPRINTK("second fanout expander %016llx phy 0x%x "
1025 "attached to ex %016llx phy 0x%x\n",
1026 SAS_ADDR(ex_phy->attached_sas_addr),
1027 ex_phy->attached_phy_id,
1028 SAS_ADDR(dev->sas_addr),
1029 phy_id);
1030 sas_ex_disable_phy(dev, phy_id);
1031 break;
1032 } else
1033 memcpy(dev->port->disc.fanout_sas_addr,
1034 ex_phy->attached_sas_addr, SAS_ADDR_SIZE);
1035 /* fallthrough */
1036 case EDGE_DEV:
1037 child = sas_ex_discover_expander(dev, phy_id);
1038 break;
1039 default:
1040 break;
1041 }
1042
1043 if (child) {
1044 int i;
1045
1046 for (i = 0; i < ex->num_phys; i++) {
1047 if (ex->ex_phy[i].phy_state == PHY_VACANT ||
1048 ex->ex_phy[i].phy_state == PHY_NOT_PRESENT)
1049 continue;
Tom Peng19252de2009-07-17 16:02:04 +08001050 /*
1051 * Due to races, the phy might not get added to the
1052 * wide port, so we add the phy to the wide port here.
1053 */
James Bottomley2908d772006-08-29 09:22:51 -05001054 if (SAS_ADDR(ex->ex_phy[i].attached_sas_addr) ==
Tom Peng19252de2009-07-17 16:02:04 +08001055 SAS_ADDR(child->sas_addr)) {
James Bottomley2908d772006-08-29 09:22:51 -05001056 ex->ex_phy[i].phy_state= PHY_DEVICE_DISCOVERED;
Tom Peng19252de2009-07-17 16:02:04 +08001057 res = sas_ex_join_wide_port(dev, i);
1058 if (!res)
1059 SAS_DPRINTK("Attaching ex phy%d to wide port %016llx\n",
1060 i, SAS_ADDR(ex->ex_phy[i].attached_sas_addr));
1061
1062 }
James Bottomley2908d772006-08-29 09:22:51 -05001063 }
1064 }
1065
1066 return res;
1067}
1068
1069static int sas_find_sub_addr(struct domain_device *dev, u8 *sub_addr)
1070{
1071 struct expander_device *ex = &dev->ex_dev;
1072 int i;
1073
1074 for (i = 0; i < ex->num_phys; i++) {
1075 struct ex_phy *phy = &ex->ex_phy[i];
1076
1077 if (phy->phy_state == PHY_VACANT ||
1078 phy->phy_state == PHY_NOT_PRESENT)
1079 continue;
1080
1081 if ((phy->attached_dev_type == EDGE_DEV ||
1082 phy->attached_dev_type == FANOUT_DEV) &&
1083 phy->routing_attr == SUBTRACTIVE_ROUTING) {
1084
1085 memcpy(sub_addr, phy->attached_sas_addr,SAS_ADDR_SIZE);
1086
1087 return 1;
1088 }
1089 }
1090 return 0;
1091}
1092
1093static int sas_check_level_subtractive_boundary(struct domain_device *dev)
1094{
1095 struct expander_device *ex = &dev->ex_dev;
1096 struct domain_device *child;
1097 u8 sub_addr[8] = {0, };
1098
1099 list_for_each_entry(child, &ex->children, siblings) {
1100 if (child->dev_type != EDGE_DEV &&
1101 child->dev_type != FANOUT_DEV)
1102 continue;
1103 if (sub_addr[0] == 0) {
1104 sas_find_sub_addr(child, sub_addr);
1105 continue;
1106 } else {
1107 u8 s2[8];
1108
1109 if (sas_find_sub_addr(child, s2) &&
1110 (SAS_ADDR(sub_addr) != SAS_ADDR(s2))) {
1111
1112 SAS_DPRINTK("ex %016llx->%016llx-?->%016llx "
1113 "diverges from subtractive "
1114 "boundary %016llx\n",
1115 SAS_ADDR(dev->sas_addr),
1116 SAS_ADDR(child->sas_addr),
1117 SAS_ADDR(s2),
1118 SAS_ADDR(sub_addr));
1119
1120 sas_ex_disable_port(child, s2);
1121 }
1122 }
1123 }
1124 return 0;
1125}
1126/**
1127 * sas_ex_discover_devices -- discover devices attached to this expander
1128 * dev: pointer to the expander domain device
1129 * single: if you want to do a single phy, else set to -1;
1130 *
1131 * Configure this expander for use with its devices and register the
1132 * devices of this expander.
1133 */
1134static int sas_ex_discover_devices(struct domain_device *dev, int single)
1135{
1136 struct expander_device *ex = &dev->ex_dev;
1137 int i = 0, end = ex->num_phys;
1138 int res = 0;
1139
1140 if (0 <= single && single < end) {
1141 i = single;
1142 end = i+1;
1143 }
1144
1145 for ( ; i < end; i++) {
1146 struct ex_phy *ex_phy = &ex->ex_phy[i];
1147
1148 if (ex_phy->phy_state == PHY_VACANT ||
1149 ex_phy->phy_state == PHY_NOT_PRESENT ||
1150 ex_phy->phy_state == PHY_DEVICE_DISCOVERED)
1151 continue;
1152
1153 switch (ex_phy->linkrate) {
James Bottomley88edf742006-09-06 17:36:13 -05001154 case SAS_PHY_DISABLED:
1155 case SAS_PHY_RESET_PROBLEM:
1156 case SAS_SATA_PORT_SELECTOR:
James Bottomley2908d772006-08-29 09:22:51 -05001157 continue;
1158 default:
1159 res = sas_ex_discover_dev(dev, i);
1160 if (res)
1161 break;
1162 continue;
1163 }
1164 }
1165
1166 if (!res)
1167 sas_check_level_subtractive_boundary(dev);
1168
1169 return res;
1170}
1171
1172static int sas_check_ex_subtractive_boundary(struct domain_device *dev)
1173{
1174 struct expander_device *ex = &dev->ex_dev;
1175 int i;
1176 u8 *sub_sas_addr = NULL;
1177
1178 if (dev->dev_type != EDGE_DEV)
1179 return 0;
1180
1181 for (i = 0; i < ex->num_phys; i++) {
1182 struct ex_phy *phy = &ex->ex_phy[i];
1183
1184 if (phy->phy_state == PHY_VACANT ||
1185 phy->phy_state == PHY_NOT_PRESENT)
1186 continue;
1187
1188 if ((phy->attached_dev_type == FANOUT_DEV ||
1189 phy->attached_dev_type == EDGE_DEV) &&
1190 phy->routing_attr == SUBTRACTIVE_ROUTING) {
1191
1192 if (!sub_sas_addr)
1193 sub_sas_addr = &phy->attached_sas_addr[0];
1194 else if (SAS_ADDR(sub_sas_addr) !=
1195 SAS_ADDR(phy->attached_sas_addr)) {
1196
1197 SAS_DPRINTK("ex %016llx phy 0x%x "
1198 "diverges(%016llx) on subtractive "
1199 "boundary(%016llx). Disabled\n",
1200 SAS_ADDR(dev->sas_addr), i,
1201 SAS_ADDR(phy->attached_sas_addr),
1202 SAS_ADDR(sub_sas_addr));
1203 sas_ex_disable_phy(dev, i);
1204 }
1205 }
1206 }
1207 return 0;
1208}
1209
1210static void sas_print_parent_topology_bug(struct domain_device *child,
1211 struct ex_phy *parent_phy,
1212 struct ex_phy *child_phy)
1213{
James Bottomley2908d772006-08-29 09:22:51 -05001214 static const char *ex_type[] = {
1215 [EDGE_DEV] = "edge",
1216 [FANOUT_DEV] = "fanout",
1217 };
1218 struct domain_device *parent = child->parent;
1219
Dan Williamsd214d812012-01-16 11:56:50 -08001220 sas_printk("%s ex %016llx phy 0x%x <--> %s ex %016llx "
1221 "phy 0x%x has %c:%c routing link!\n",
James Bottomley2908d772006-08-29 09:22:51 -05001222
1223 ex_type[parent->dev_type],
1224 SAS_ADDR(parent->sas_addr),
1225 parent_phy->phy_id,
1226
1227 ex_type[child->dev_type],
1228 SAS_ADDR(child->sas_addr),
1229 child_phy->phy_id,
1230
Dan Williamsd214d812012-01-16 11:56:50 -08001231 sas_route_char(parent, parent_phy),
1232 sas_route_char(child, child_phy));
James Bottomley2908d772006-08-29 09:22:51 -05001233}
1234
1235static int sas_check_eeds(struct domain_device *child,
1236 struct ex_phy *parent_phy,
1237 struct ex_phy *child_phy)
1238{
1239 int res = 0;
1240 struct domain_device *parent = child->parent;
1241
1242 if (SAS_ADDR(parent->port->disc.fanout_sas_addr) != 0) {
1243 res = -ENODEV;
1244 SAS_DPRINTK("edge ex %016llx phy S:0x%x <--> edge ex %016llx "
1245 "phy S:0x%x, while there is a fanout ex %016llx\n",
1246 SAS_ADDR(parent->sas_addr),
1247 parent_phy->phy_id,
1248 SAS_ADDR(child->sas_addr),
1249 child_phy->phy_id,
1250 SAS_ADDR(parent->port->disc.fanout_sas_addr));
1251 } else if (SAS_ADDR(parent->port->disc.eeds_a) == 0) {
1252 memcpy(parent->port->disc.eeds_a, parent->sas_addr,
1253 SAS_ADDR_SIZE);
1254 memcpy(parent->port->disc.eeds_b, child->sas_addr,
1255 SAS_ADDR_SIZE);
1256 } else if (((SAS_ADDR(parent->port->disc.eeds_a) ==
1257 SAS_ADDR(parent->sas_addr)) ||
1258 (SAS_ADDR(parent->port->disc.eeds_a) ==
1259 SAS_ADDR(child->sas_addr)))
1260 &&
1261 ((SAS_ADDR(parent->port->disc.eeds_b) ==
1262 SAS_ADDR(parent->sas_addr)) ||
1263 (SAS_ADDR(parent->port->disc.eeds_b) ==
1264 SAS_ADDR(child->sas_addr))))
1265 ;
1266 else {
1267 res = -ENODEV;
1268 SAS_DPRINTK("edge ex %016llx phy 0x%x <--> edge ex %016llx "
1269 "phy 0x%x link forms a third EEDS!\n",
1270 SAS_ADDR(parent->sas_addr),
1271 parent_phy->phy_id,
1272 SAS_ADDR(child->sas_addr),
1273 child_phy->phy_id);
1274 }
1275
1276 return res;
1277}
1278
1279/* Here we spill over 80 columns. It is intentional.
1280 */
1281static int sas_check_parent_topology(struct domain_device *child)
1282{
1283 struct expander_device *child_ex = &child->ex_dev;
1284 struct expander_device *parent_ex;
1285 int i;
1286 int res = 0;
1287
1288 if (!child->parent)
1289 return 0;
1290
1291 if (child->parent->dev_type != EDGE_DEV &&
1292 child->parent->dev_type != FANOUT_DEV)
1293 return 0;
1294
1295 parent_ex = &child->parent->ex_dev;
1296
1297 for (i = 0; i < parent_ex->num_phys; i++) {
1298 struct ex_phy *parent_phy = &parent_ex->ex_phy[i];
1299 struct ex_phy *child_phy;
1300
1301 if (parent_phy->phy_state == PHY_VACANT ||
1302 parent_phy->phy_state == PHY_NOT_PRESENT)
1303 continue;
1304
1305 if (SAS_ADDR(parent_phy->attached_sas_addr) != SAS_ADDR(child->sas_addr))
1306 continue;
1307
1308 child_phy = &child_ex->ex_phy[parent_phy->attached_phy_id];
1309
1310 switch (child->parent->dev_type) {
1311 case EDGE_DEV:
1312 if (child->dev_type == FANOUT_DEV) {
1313 if (parent_phy->routing_attr != SUBTRACTIVE_ROUTING ||
1314 child_phy->routing_attr != TABLE_ROUTING) {
1315 sas_print_parent_topology_bug(child, parent_phy, child_phy);
1316 res = -ENODEV;
1317 }
1318 } else if (parent_phy->routing_attr == SUBTRACTIVE_ROUTING) {
1319 if (child_phy->routing_attr == SUBTRACTIVE_ROUTING) {
1320 res = sas_check_eeds(child, parent_phy, child_phy);
1321 } else if (child_phy->routing_attr != TABLE_ROUTING) {
1322 sas_print_parent_topology_bug(child, parent_phy, child_phy);
1323 res = -ENODEV;
1324 }
Luben Tuikovffaac8f2011-09-22 09:41:36 -07001325 } else if (parent_phy->routing_attr == TABLE_ROUTING) {
1326 if (child_phy->routing_attr == SUBTRACTIVE_ROUTING ||
1327 (child_phy->routing_attr == TABLE_ROUTING &&
1328 child_ex->t2t_supp && parent_ex->t2t_supp)) {
1329 /* All good */;
1330 } else {
1331 sas_print_parent_topology_bug(child, parent_phy, child_phy);
1332 res = -ENODEV;
1333 }
James Bottomley2908d772006-08-29 09:22:51 -05001334 }
1335 break;
1336 case FANOUT_DEV:
1337 if (parent_phy->routing_attr != TABLE_ROUTING ||
1338 child_phy->routing_attr != SUBTRACTIVE_ROUTING) {
1339 sas_print_parent_topology_bug(child, parent_phy, child_phy);
1340 res = -ENODEV;
1341 }
1342 break;
1343 default:
1344 break;
1345 }
1346 }
1347
1348 return res;
1349}
1350
1351#define RRI_REQ_SIZE 16
1352#define RRI_RESP_SIZE 44
1353
1354static int sas_configure_present(struct domain_device *dev, int phy_id,
1355 u8 *sas_addr, int *index, int *present)
1356{
1357 int i, res = 0;
1358 struct expander_device *ex = &dev->ex_dev;
1359 struct ex_phy *phy = &ex->ex_phy[phy_id];
1360 u8 *rri_req;
1361 u8 *rri_resp;
1362
1363 *present = 0;
1364 *index = 0;
1365
1366 rri_req = alloc_smp_req(RRI_REQ_SIZE);
1367 if (!rri_req)
1368 return -ENOMEM;
1369
1370 rri_resp = alloc_smp_resp(RRI_RESP_SIZE);
1371 if (!rri_resp) {
1372 kfree(rri_req);
1373 return -ENOMEM;
1374 }
1375
1376 rri_req[1] = SMP_REPORT_ROUTE_INFO;
1377 rri_req[9] = phy_id;
1378
1379 for (i = 0; i < ex->max_route_indexes ; i++) {
1380 *(__be16 *)(rri_req+6) = cpu_to_be16(i);
1381 res = smp_execute_task(dev, rri_req, RRI_REQ_SIZE, rri_resp,
1382 RRI_RESP_SIZE);
1383 if (res)
1384 goto out;
1385 res = rri_resp[2];
1386 if (res == SMP_RESP_NO_INDEX) {
1387 SAS_DPRINTK("overflow of indexes: dev %016llx "
1388 "phy 0x%x index 0x%x\n",
1389 SAS_ADDR(dev->sas_addr), phy_id, i);
1390 goto out;
1391 } else if (res != SMP_RESP_FUNC_ACC) {
1392 SAS_DPRINTK("%s: dev %016llx phy 0x%x index 0x%x "
Harvey Harrisoncadbd4a2008-07-03 23:47:27 -07001393 "result 0x%x\n", __func__,
James Bottomley2908d772006-08-29 09:22:51 -05001394 SAS_ADDR(dev->sas_addr), phy_id, i, res);
1395 goto out;
1396 }
1397 if (SAS_ADDR(sas_addr) != 0) {
1398 if (SAS_ADDR(rri_resp+16) == SAS_ADDR(sas_addr)) {
1399 *index = i;
1400 if ((rri_resp[12] & 0x80) == 0x80)
1401 *present = 0;
1402 else
1403 *present = 1;
1404 goto out;
1405 } else if (SAS_ADDR(rri_resp+16) == 0) {
1406 *index = i;
1407 *present = 0;
1408 goto out;
1409 }
1410 } else if (SAS_ADDR(rri_resp+16) == 0 &&
1411 phy->last_da_index < i) {
1412 phy->last_da_index = i;
1413 *index = i;
1414 *present = 0;
1415 goto out;
1416 }
1417 }
1418 res = -1;
1419out:
1420 kfree(rri_req);
1421 kfree(rri_resp);
1422 return res;
1423}
1424
1425#define CRI_REQ_SIZE 44
1426#define CRI_RESP_SIZE 8
1427
1428static int sas_configure_set(struct domain_device *dev, int phy_id,
1429 u8 *sas_addr, int index, int include)
1430{
1431 int res;
1432 u8 *cri_req;
1433 u8 *cri_resp;
1434
1435 cri_req = alloc_smp_req(CRI_REQ_SIZE);
1436 if (!cri_req)
1437 return -ENOMEM;
1438
1439 cri_resp = alloc_smp_resp(CRI_RESP_SIZE);
1440 if (!cri_resp) {
1441 kfree(cri_req);
1442 return -ENOMEM;
1443 }
1444
1445 cri_req[1] = SMP_CONF_ROUTE_INFO;
1446 *(__be16 *)(cri_req+6) = cpu_to_be16(index);
1447 cri_req[9] = phy_id;
1448 if (SAS_ADDR(sas_addr) == 0 || !include)
1449 cri_req[12] |= 0x80;
1450 memcpy(cri_req+16, sas_addr, SAS_ADDR_SIZE);
1451
1452 res = smp_execute_task(dev, cri_req, CRI_REQ_SIZE, cri_resp,
1453 CRI_RESP_SIZE);
1454 if (res)
1455 goto out;
1456 res = cri_resp[2];
1457 if (res == SMP_RESP_NO_INDEX) {
1458 SAS_DPRINTK("overflow of indexes: dev %016llx phy 0x%x "
1459 "index 0x%x\n",
1460 SAS_ADDR(dev->sas_addr), phy_id, index);
1461 }
1462out:
1463 kfree(cri_req);
1464 kfree(cri_resp);
1465 return res;
1466}
1467
1468static int sas_configure_phy(struct domain_device *dev, int phy_id,
1469 u8 *sas_addr, int include)
1470{
1471 int index;
1472 int present;
1473 int res;
1474
1475 res = sas_configure_present(dev, phy_id, sas_addr, &index, &present);
1476 if (res)
1477 return res;
1478 if (include ^ present)
1479 return sas_configure_set(dev, phy_id, sas_addr, index,include);
1480
1481 return res;
1482}
1483
1484/**
1485 * sas_configure_parent -- configure routing table of parent
1486 * parent: parent expander
1487 * child: child expander
1488 * sas_addr: SAS port identifier of device directly attached to child
1489 */
1490static int sas_configure_parent(struct domain_device *parent,
1491 struct domain_device *child,
1492 u8 *sas_addr, int include)
1493{
1494 struct expander_device *ex_parent = &parent->ex_dev;
1495 int res = 0;
1496 int i;
1497
1498 if (parent->parent) {
1499 res = sas_configure_parent(parent->parent, parent, sas_addr,
1500 include);
1501 if (res)
1502 return res;
1503 }
1504
1505 if (ex_parent->conf_route_table == 0) {
1506 SAS_DPRINTK("ex %016llx has self-configuring routing table\n",
1507 SAS_ADDR(parent->sas_addr));
1508 return 0;
1509 }
1510
1511 for (i = 0; i < ex_parent->num_phys; i++) {
1512 struct ex_phy *phy = &ex_parent->ex_phy[i];
1513
1514 if ((phy->routing_attr == TABLE_ROUTING) &&
1515 (SAS_ADDR(phy->attached_sas_addr) ==
1516 SAS_ADDR(child->sas_addr))) {
1517 res = sas_configure_phy(parent, i, sas_addr, include);
1518 if (res)
1519 return res;
1520 }
1521 }
1522
1523 return res;
1524}
1525
1526/**
1527 * sas_configure_routing -- configure routing
1528 * dev: expander device
1529 * sas_addr: port identifier of device directly attached to the expander device
1530 */
1531static int sas_configure_routing(struct domain_device *dev, u8 *sas_addr)
1532{
1533 if (dev->parent)
1534 return sas_configure_parent(dev->parent, dev, sas_addr, 1);
1535 return 0;
1536}
1537
1538static int sas_disable_routing(struct domain_device *dev, u8 *sas_addr)
1539{
1540 if (dev->parent)
1541 return sas_configure_parent(dev->parent, dev, sas_addr, 0);
1542 return 0;
1543}
1544
James Bottomley2908d772006-08-29 09:22:51 -05001545/**
1546 * sas_discover_expander -- expander discovery
1547 * @ex: pointer to expander domain device
1548 *
1549 * See comment in sas_discover_sata().
1550 */
1551static int sas_discover_expander(struct domain_device *dev)
1552{
1553 int res;
1554
1555 res = sas_notify_lldd_dev_found(dev);
1556 if (res)
1557 return res;
1558
1559 res = sas_ex_general(dev);
1560 if (res)
1561 goto out_err;
1562 res = sas_ex_manuf_info(dev);
1563 if (res)
1564 goto out_err;
1565
1566 res = sas_expander_discover(dev);
1567 if (res) {
1568 SAS_DPRINTK("expander %016llx discovery failed(0x%x)\n",
1569 SAS_ADDR(dev->sas_addr), res);
1570 goto out_err;
1571 }
1572
1573 sas_check_ex_subtractive_boundary(dev);
1574 res = sas_check_parent_topology(dev);
1575 if (res)
1576 goto out_err;
1577 return 0;
1578out_err:
1579 sas_notify_lldd_dev_gone(dev);
1580 return res;
1581}
1582
1583static int sas_ex_level_discovery(struct asd_sas_port *port, const int level)
1584{
1585 int res = 0;
1586 struct domain_device *dev;
1587
1588 list_for_each_entry(dev, &port->dev_list, dev_list_node) {
1589 if (dev->dev_type == EDGE_DEV ||
1590 dev->dev_type == FANOUT_DEV) {
1591 struct sas_expander_device *ex =
1592 rphy_to_expander_device(dev->rphy);
1593
1594 if (level == ex->level)
1595 res = sas_ex_discover_devices(dev, -1);
1596 else if (level > 0)
1597 res = sas_ex_discover_devices(port->port_dev, -1);
1598
1599 }
1600 }
1601
1602 return res;
1603}
1604
1605static int sas_ex_bfs_disc(struct asd_sas_port *port)
1606{
1607 int res;
1608 int level;
1609
1610 do {
1611 level = port->disc.max_level;
1612 res = sas_ex_level_discovery(port, level);
1613 mb();
1614 } while (level < port->disc.max_level);
1615
1616 return res;
1617}
1618
1619int sas_discover_root_expander(struct domain_device *dev)
1620{
1621 int res;
1622 struct sas_expander_device *ex = rphy_to_expander_device(dev->rphy);
1623
Darrick J. Wongbf451202007-01-11 14:14:52 -08001624 res = sas_rphy_add(dev->rphy);
1625 if (res)
1626 goto out_err;
James Bottomley2908d772006-08-29 09:22:51 -05001627
1628 ex->level = dev->port->disc.max_level; /* 0 */
1629 res = sas_discover_expander(dev);
Darrick J. Wongbf451202007-01-11 14:14:52 -08001630 if (res)
1631 goto out_err2;
James Bottomley2908d772006-08-29 09:22:51 -05001632
Darrick J. Wongbf451202007-01-11 14:14:52 -08001633 sas_ex_bfs_disc(dev->port);
1634
1635 return res;
1636
1637out_err2:
Darrick J. Wong6f63caa2007-01-26 14:08:43 -08001638 sas_rphy_remove(dev->rphy);
Darrick J. Wongbf451202007-01-11 14:14:52 -08001639out_err:
James Bottomley2908d772006-08-29 09:22:51 -05001640 return res;
1641}
1642
1643/* ---------- Domain revalidation ---------- */
1644
1645static int sas_get_phy_discover(struct domain_device *dev,
1646 int phy_id, struct smp_resp *disc_resp)
1647{
1648 int res;
1649 u8 *disc_req;
1650
1651 disc_req = alloc_smp_req(DISCOVER_REQ_SIZE);
1652 if (!disc_req)
1653 return -ENOMEM;
1654
1655 disc_req[1] = SMP_DISCOVER;
1656 disc_req[9] = phy_id;
1657
1658 res = smp_execute_task(dev, disc_req, DISCOVER_REQ_SIZE,
1659 disc_resp, DISCOVER_RESP_SIZE);
1660 if (res)
1661 goto out;
1662 else if (disc_resp->result != SMP_RESP_FUNC_ACC) {
1663 res = disc_resp->result;
1664 goto out;
1665 }
1666out:
1667 kfree(disc_req);
1668 return res;
1669}
1670
1671static int sas_get_phy_change_count(struct domain_device *dev,
1672 int phy_id, int *pcc)
1673{
1674 int res;
1675 struct smp_resp *disc_resp;
1676
1677 disc_resp = alloc_smp_resp(DISCOVER_RESP_SIZE);
1678 if (!disc_resp)
1679 return -ENOMEM;
1680
1681 res = sas_get_phy_discover(dev, phy_id, disc_resp);
1682 if (!res)
1683 *pcc = disc_resp->disc.change_count;
1684
1685 kfree(disc_resp);
1686 return res;
1687}
1688
Dan Williams354cf822012-01-12 17:57:35 -08001689static int sas_get_phy_attached_dev(struct domain_device *dev, int phy_id,
1690 u8 *sas_addr, enum sas_dev_type *type)
James Bottomley2908d772006-08-29 09:22:51 -05001691{
1692 int res;
1693 struct smp_resp *disc_resp;
1694 struct discover_resp *dr;
1695
1696 disc_resp = alloc_smp_resp(DISCOVER_RESP_SIZE);
1697 if (!disc_resp)
1698 return -ENOMEM;
1699 dr = &disc_resp->disc;
1700
1701 res = sas_get_phy_discover(dev, phy_id, disc_resp);
Dan Williams354cf822012-01-12 17:57:35 -08001702 if (res == 0) {
1703 memcpy(sas_addr, disc_resp->disc.attached_sas_addr, 8);
1704 *type = to_dev_type(dr);
1705 if (*type == 0)
1706 memset(sas_addr, 0, 8);
James Bottomley2908d772006-08-29 09:22:51 -05001707 }
1708 kfree(disc_resp);
1709 return res;
1710}
1711
1712static int sas_find_bcast_phy(struct domain_device *dev, int *phy_id,
Tom Peng19252de2009-07-17 16:02:04 +08001713 int from_phy, bool update)
James Bottomley2908d772006-08-29 09:22:51 -05001714{
1715 struct expander_device *ex = &dev->ex_dev;
1716 int res = 0;
1717 int i;
1718
1719 for (i = from_phy; i < ex->num_phys; i++) {
1720 int phy_change_count = 0;
1721
1722 res = sas_get_phy_change_count(dev, i, &phy_change_count);
Thomas Jackson16994902012-02-17 18:33:10 -08001723 switch (res) {
1724 case SMP_RESP_PHY_VACANT:
1725 case SMP_RESP_NO_PHY:
1726 continue;
1727 case SMP_RESP_FUNC_ACC:
1728 break;
1729 default:
1730 return res;
1731 }
1732
1733 if (phy_change_count != ex->ex_phy[i].phy_change_count) {
Tom Peng19252de2009-07-17 16:02:04 +08001734 if (update)
1735 ex->ex_phy[i].phy_change_count =
1736 phy_change_count;
James Bottomley2908d772006-08-29 09:22:51 -05001737 *phy_id = i;
1738 return 0;
1739 }
1740 }
Thomas Jackson16994902012-02-17 18:33:10 -08001741 return 0;
James Bottomley2908d772006-08-29 09:22:51 -05001742}
1743
1744static int sas_get_ex_change_count(struct domain_device *dev, int *ecc)
1745{
1746 int res;
1747 u8 *rg_req;
1748 struct smp_resp *rg_resp;
1749
1750 rg_req = alloc_smp_req(RG_REQ_SIZE);
1751 if (!rg_req)
1752 return -ENOMEM;
1753
1754 rg_resp = alloc_smp_resp(RG_RESP_SIZE);
1755 if (!rg_resp) {
1756 kfree(rg_req);
1757 return -ENOMEM;
1758 }
1759
1760 rg_req[1] = SMP_REPORT_GENERAL;
1761
1762 res = smp_execute_task(dev, rg_req, RG_REQ_SIZE, rg_resp,
1763 RG_RESP_SIZE);
1764 if (res)
1765 goto out;
1766 if (rg_resp->result != SMP_RESP_FUNC_ACC) {
1767 res = rg_resp->result;
1768 goto out;
1769 }
1770
1771 *ecc = be16_to_cpu(rg_resp->rg.change_count);
1772out:
1773 kfree(rg_resp);
1774 kfree(rg_req);
1775 return res;
1776}
Tom Peng19252de2009-07-17 16:02:04 +08001777/**
1778 * sas_find_bcast_dev - find the device issue BROADCAST(CHANGE).
1779 * @dev:domain device to be detect.
1780 * @src_dev: the device which originated BROADCAST(CHANGE).
1781 *
1782 * Add self-configuration expander suport. Suppose two expander cascading,
1783 * when the first level expander is self-configuring, hotplug the disks in
1784 * second level expander, BROADCAST(CHANGE) will not only be originated
1785 * in the second level expander, but also be originated in the first level
1786 * expander (see SAS protocol SAS 2r-14, 7.11 for detail), it is to say,
1787 * expander changed count in two level expanders will all increment at least
1788 * once, but the phy which chang count has changed is the source device which
1789 * we concerned.
1790 */
James Bottomley2908d772006-08-29 09:22:51 -05001791
1792static int sas_find_bcast_dev(struct domain_device *dev,
1793 struct domain_device **src_dev)
1794{
1795 struct expander_device *ex = &dev->ex_dev;
1796 int ex_change_count = -1;
Tom Peng19252de2009-07-17 16:02:04 +08001797 int phy_id = -1;
James Bottomley2908d772006-08-29 09:22:51 -05001798 int res;
Tom Peng19252de2009-07-17 16:02:04 +08001799 struct domain_device *ch;
James Bottomley2908d772006-08-29 09:22:51 -05001800
1801 res = sas_get_ex_change_count(dev, &ex_change_count);
1802 if (res)
1803 goto out;
Tom Peng19252de2009-07-17 16:02:04 +08001804 if (ex_change_count != -1 && ex_change_count != ex->ex_change_count) {
1805 /* Just detect if this expander phys phy change count changed,
1806 * in order to determine if this expander originate BROADCAST,
1807 * and do not update phy change count field in our structure.
1808 */
1809 res = sas_find_bcast_phy(dev, &phy_id, 0, false);
1810 if (phy_id != -1) {
1811 *src_dev = dev;
1812 ex->ex_change_count = ex_change_count;
1813 SAS_DPRINTK("Expander phy change count has changed\n");
1814 return res;
1815 } else
1816 SAS_DPRINTK("Expander phys DID NOT change\n");
1817 }
1818 list_for_each_entry(ch, &ex->children, siblings) {
1819 if (ch->dev_type == EDGE_DEV || ch->dev_type == FANOUT_DEV) {
1820 res = sas_find_bcast_dev(ch, src_dev);
Mark Salyzyn24926da2011-09-01 06:11:17 -07001821 if (*src_dev)
Tom Peng19252de2009-07-17 16:02:04 +08001822 return res;
James Bottomley2908d772006-08-29 09:22:51 -05001823 }
1824 }
1825out:
1826 return res;
1827}
1828
Dan Williams1a34c062011-09-21 22:05:34 -07001829static void sas_unregister_ex_tree(struct asd_sas_port *port, struct domain_device *dev)
James Bottomley2908d772006-08-29 09:22:51 -05001830{
1831 struct expander_device *ex = &dev->ex_dev;
1832 struct domain_device *child, *n;
1833
1834 list_for_each_entry_safe(child, n, &ex->children, siblings) {
Dan Williamse1399422012-01-07 08:52:39 +00001835 set_bit(SAS_DEV_GONE, &child->state);
James Bottomley2908d772006-08-29 09:22:51 -05001836 if (child->dev_type == EDGE_DEV ||
1837 child->dev_type == FANOUT_DEV)
Dan Williams1a34c062011-09-21 22:05:34 -07001838 sas_unregister_ex_tree(port, child);
James Bottomley2908d772006-08-29 09:22:51 -05001839 else
Dan Williams1a34c062011-09-21 22:05:34 -07001840 sas_unregister_dev(port, child);
James Bottomley2908d772006-08-29 09:22:51 -05001841 }
Dan Williams1a34c062011-09-21 22:05:34 -07001842 sas_unregister_dev(port, dev);
James Bottomley2908d772006-08-29 09:22:51 -05001843}
1844
1845static void sas_unregister_devs_sas_addr(struct domain_device *parent,
Tom Peng19252de2009-07-17 16:02:04 +08001846 int phy_id, bool last)
James Bottomley2908d772006-08-29 09:22:51 -05001847{
1848 struct expander_device *ex_dev = &parent->ex_dev;
1849 struct ex_phy *phy = &ex_dev->ex_phy[phy_id];
Dan Williamsf41a0c42011-12-21 21:33:17 -08001850 struct domain_device *child, *n, *found = NULL;
Tom Peng19252de2009-07-17 16:02:04 +08001851 if (last) {
1852 list_for_each_entry_safe(child, n,
1853 &ex_dev->children, siblings) {
1854 if (SAS_ADDR(child->sas_addr) ==
1855 SAS_ADDR(phy->attached_sas_addr)) {
Dan Williamse1399422012-01-07 08:52:39 +00001856 set_bit(SAS_DEV_GONE, &child->state);
Tom Peng19252de2009-07-17 16:02:04 +08001857 if (child->dev_type == EDGE_DEV ||
1858 child->dev_type == FANOUT_DEV)
Dan Williams1a34c062011-09-21 22:05:34 -07001859 sas_unregister_ex_tree(parent->port, child);
Tom Peng19252de2009-07-17 16:02:04 +08001860 else
Dan Williams1a34c062011-09-21 22:05:34 -07001861 sas_unregister_dev(parent->port, child);
Dan Williamsf41a0c42011-12-21 21:33:17 -08001862 found = child;
Tom Peng19252de2009-07-17 16:02:04 +08001863 break;
1864 }
James Bottomley2908d772006-08-29 09:22:51 -05001865 }
Tom Peng19252de2009-07-17 16:02:04 +08001866 sas_disable_routing(parent, phy->attached_sas_addr);
James Bottomley2908d772006-08-29 09:22:51 -05001867 }
James Bottomley2908d772006-08-29 09:22:51 -05001868 memset(phy->attached_sas_addr, 0, SAS_ADDR_SIZE);
Mark Salyzyna73914c2011-09-22 08:32:23 -07001869 if (phy->port) {
1870 sas_port_delete_phy(phy->port, phy->phy);
Dan Williamsf41a0c42011-12-21 21:33:17 -08001871 sas_device_set_phy(found, phy->port);
Mark Salyzyna73914c2011-09-22 08:32:23 -07001872 if (phy->port->num_phys == 0)
1873 sas_port_delete(phy->port);
1874 phy->port = NULL;
1875 }
James Bottomley2908d772006-08-29 09:22:51 -05001876}
1877
1878static int sas_discover_bfs_by_root_level(struct domain_device *root,
1879 const int level)
1880{
1881 struct expander_device *ex_root = &root->ex_dev;
1882 struct domain_device *child;
1883 int res = 0;
1884
1885 list_for_each_entry(child, &ex_root->children, siblings) {
1886 if (child->dev_type == EDGE_DEV ||
1887 child->dev_type == FANOUT_DEV) {
1888 struct sas_expander_device *ex =
1889 rphy_to_expander_device(child->rphy);
1890
1891 if (level > ex->level)
1892 res = sas_discover_bfs_by_root_level(child,
1893 level);
1894 else if (level == ex->level)
1895 res = sas_ex_discover_devices(child, -1);
1896 }
1897 }
1898 return res;
1899}
1900
1901static int sas_discover_bfs_by_root(struct domain_device *dev)
1902{
1903 int res;
1904 struct sas_expander_device *ex = rphy_to_expander_device(dev->rphy);
1905 int level = ex->level+1;
1906
1907 res = sas_ex_discover_devices(dev, -1);
1908 if (res)
1909 goto out;
1910 do {
1911 res = sas_discover_bfs_by_root_level(dev, level);
1912 mb();
1913 level += 1;
1914 } while (level <= dev->port->disc.max_level);
1915out:
1916 return res;
1917}
1918
1919static int sas_discover_new(struct domain_device *dev, int phy_id)
1920{
1921 struct ex_phy *ex_phy = &dev->ex_dev.ex_phy[phy_id];
1922 struct domain_device *child;
Tom Peng19252de2009-07-17 16:02:04 +08001923 bool found = false;
1924 int res, i;
James Bottomley2908d772006-08-29 09:22:51 -05001925
1926 SAS_DPRINTK("ex %016llx phy%d new device attached\n",
1927 SAS_ADDR(dev->sas_addr), phy_id);
1928 res = sas_ex_phy_discover(dev, phy_id);
1929 if (res)
1930 goto out;
Tom Peng19252de2009-07-17 16:02:04 +08001931 /* to support the wide port inserted */
1932 for (i = 0; i < dev->ex_dev.num_phys; i++) {
1933 struct ex_phy *ex_phy_temp = &dev->ex_dev.ex_phy[i];
1934 if (i == phy_id)
1935 continue;
1936 if (SAS_ADDR(ex_phy_temp->attached_sas_addr) ==
1937 SAS_ADDR(ex_phy->attached_sas_addr)) {
1938 found = true;
1939 break;
1940 }
1941 }
1942 if (found) {
1943 sas_ex_join_wide_port(dev, phy_id);
1944 return 0;
1945 }
James Bottomley2908d772006-08-29 09:22:51 -05001946 res = sas_ex_discover_devices(dev, phy_id);
Tom Peng19252de2009-07-17 16:02:04 +08001947 if (!res)
James Bottomley2908d772006-08-29 09:22:51 -05001948 goto out;
1949 list_for_each_entry(child, &dev->ex_dev.children, siblings) {
1950 if (SAS_ADDR(child->sas_addr) ==
1951 SAS_ADDR(ex_phy->attached_sas_addr)) {
1952 if (child->dev_type == EDGE_DEV ||
1953 child->dev_type == FANOUT_DEV)
1954 res = sas_discover_bfs_by_root(child);
1955 break;
1956 }
1957 }
1958out:
1959 return res;
1960}
1961
Dan Williams354cf822012-01-12 17:57:35 -08001962static bool dev_type_flutter(enum sas_dev_type new, enum sas_dev_type old)
1963{
1964 if (old == new)
1965 return true;
1966
1967 /* treat device directed resets as flutter, if we went
1968 * SAS_END_DEV to SATA_PENDING the link needs recovery
1969 */
1970 if ((old == SATA_PENDING && new == SAS_END_DEV) ||
1971 (old == SAS_END_DEV && new == SATA_PENDING))
1972 return true;
1973
1974 return false;
1975}
1976
Tom Peng19252de2009-07-17 16:02:04 +08001977static int sas_rediscover_dev(struct domain_device *dev, int phy_id, bool last)
James Bottomley2908d772006-08-29 09:22:51 -05001978{
1979 struct expander_device *ex = &dev->ex_dev;
1980 struct ex_phy *phy = &ex->ex_phy[phy_id];
Dan Williams354cf822012-01-12 17:57:35 -08001981 enum sas_dev_type type = NO_DEVICE;
1982 u8 sas_addr[8];
James Bottomley2908d772006-08-29 09:22:51 -05001983 int res;
1984
Dan Williams354cf822012-01-12 17:57:35 -08001985 res = sas_get_phy_attached_dev(dev, phy_id, sas_addr, &type);
James Bottomley2908d772006-08-29 09:22:51 -05001986 switch (res) {
1987 case SMP_RESP_NO_PHY:
1988 phy->phy_state = PHY_NOT_PRESENT;
Tom Peng19252de2009-07-17 16:02:04 +08001989 sas_unregister_devs_sas_addr(dev, phy_id, last);
Dan Williams354cf822012-01-12 17:57:35 -08001990 return res;
James Bottomley2908d772006-08-29 09:22:51 -05001991 case SMP_RESP_PHY_VACANT:
1992 phy->phy_state = PHY_VACANT;
Tom Peng19252de2009-07-17 16:02:04 +08001993 sas_unregister_devs_sas_addr(dev, phy_id, last);
Dan Williams354cf822012-01-12 17:57:35 -08001994 return res;
James Bottomley2908d772006-08-29 09:22:51 -05001995 case SMP_RESP_FUNC_ACC:
1996 break;
1997 }
1998
Dan Williams354cf822012-01-12 17:57:35 -08001999 if (SAS_ADDR(sas_addr) == 0) {
James Bottomley2908d772006-08-29 09:22:51 -05002000 phy->phy_state = PHY_EMPTY;
Tom Peng19252de2009-07-17 16:02:04 +08002001 sas_unregister_devs_sas_addr(dev, phy_id, last);
Dan Williams354cf822012-01-12 17:57:35 -08002002 return res;
2003 } else if (SAS_ADDR(sas_addr) == SAS_ADDR(phy->attached_sas_addr) &&
2004 dev_type_flutter(type, phy->attached_dev_type)) {
2005 struct domain_device *ata_dev = sas_ex_to_ata(dev, phy_id);
2006 char *action = "";
2007
James Bottomleya01e70e2006-09-06 19:28:07 -05002008 sas_ex_phy_discover(dev, phy_id);
Dan Williams354cf822012-01-12 17:57:35 -08002009
2010 if (ata_dev && phy->attached_dev_type == SATA_PENDING)
2011 action = ", needs recovery";
2012 SAS_DPRINTK("ex %016llx phy 0x%x broadcast flutter%s\n",
2013 SAS_ADDR(dev->sas_addr), phy_id, action);
2014 return res;
2015 }
2016
Dan Williamsc666aae2012-01-19 18:43:08 -08002017 /* delete the old link */
2018 if (SAS_ADDR(phy->attached_sas_addr) &&
2019 SAS_ADDR(sas_addr) != SAS_ADDR(phy->attached_sas_addr)) {
2020 SAS_DPRINTK("ex %016llx phy 0x%x replace %016llx\n",
2021 SAS_ADDR(dev->sas_addr), phy_id,
2022 SAS_ADDR(phy->attached_sas_addr));
2023 sas_unregister_devs_sas_addr(dev, phy_id, last);
2024 }
2025
Dan Williams354cf822012-01-12 17:57:35 -08002026 return sas_discover_new(dev, phy_id);
James Bottomley2908d772006-08-29 09:22:51 -05002027}
2028
Tom Peng19252de2009-07-17 16:02:04 +08002029/**
2030 * sas_rediscover - revalidate the domain.
2031 * @dev:domain device to be detect.
2032 * @phy_id: the phy id will be detected.
2033 *
2034 * NOTE: this process _must_ quit (return) as soon as any connection
2035 * errors are encountered. Connection recovery is done elsewhere.
2036 * Discover process only interrogates devices in order to discover the
2037 * domain.For plugging out, we un-register the device only when it is
2038 * the last phy in the port, for other phys in this port, we just delete it
2039 * from the port.For inserting, we do discovery when it is the
2040 * first phy,for other phys in this port, we add it to the port to
2041 * forming the wide-port.
2042 */
James Bottomley2908d772006-08-29 09:22:51 -05002043static int sas_rediscover(struct domain_device *dev, const int phy_id)
2044{
2045 struct expander_device *ex = &dev->ex_dev;
2046 struct ex_phy *changed_phy = &ex->ex_phy[phy_id];
2047 int res = 0;
2048 int i;
Tom Peng19252de2009-07-17 16:02:04 +08002049 bool last = true; /* is this the last phy of the port */
James Bottomley2908d772006-08-29 09:22:51 -05002050
2051 SAS_DPRINTK("ex %016llx phy%d originated BROADCAST(CHANGE)\n",
2052 SAS_ADDR(dev->sas_addr), phy_id);
2053
2054 if (SAS_ADDR(changed_phy->attached_sas_addr) != 0) {
2055 for (i = 0; i < ex->num_phys; i++) {
2056 struct ex_phy *phy = &ex->ex_phy[i];
2057
2058 if (i == phy_id)
2059 continue;
2060 if (SAS_ADDR(phy->attached_sas_addr) ==
2061 SAS_ADDR(changed_phy->attached_sas_addr)) {
2062 SAS_DPRINTK("phy%d part of wide port with "
2063 "phy%d\n", phy_id, i);
Tom Peng19252de2009-07-17 16:02:04 +08002064 last = false;
2065 break;
James Bottomley2908d772006-08-29 09:22:51 -05002066 }
2067 }
Tom Peng19252de2009-07-17 16:02:04 +08002068 res = sas_rediscover_dev(dev, phy_id, last);
James Bottomley2908d772006-08-29 09:22:51 -05002069 } else
2070 res = sas_discover_new(dev, phy_id);
James Bottomley2908d772006-08-29 09:22:51 -05002071 return res;
2072}
2073
2074/**
2075 * sas_revalidate_domain -- revalidate the domain
2076 * @port: port to the domain of interest
2077 *
2078 * NOTE: this process _must_ quit (return) as soon as any connection
2079 * errors are encountered. Connection recovery is done elsewhere.
2080 * Discover process only interrogates devices in order to discover the
2081 * domain.
2082 */
2083int sas_ex_revalidate_domain(struct domain_device *port_dev)
2084{
2085 int res;
2086 struct domain_device *dev = NULL;
2087
2088 res = sas_find_bcast_dev(port_dev, &dev);
2089 if (res)
2090 goto out;
2091 if (dev) {
2092 struct expander_device *ex = &dev->ex_dev;
2093 int i = 0, phy_id;
2094
2095 do {
2096 phy_id = -1;
Tom Peng19252de2009-07-17 16:02:04 +08002097 res = sas_find_bcast_phy(dev, &phy_id, i, true);
James Bottomley2908d772006-08-29 09:22:51 -05002098 if (phy_id == -1)
2099 break;
2100 res = sas_rediscover(dev, phy_id);
2101 i = phy_id + 1;
2102 } while (i < ex->num_phys);
2103 }
2104out:
2105 return res;
2106}
2107
FUJITA Tomonoriba1fc172007-07-09 12:52:08 +09002108int sas_smp_handler(struct Scsi_Host *shost, struct sas_rphy *rphy,
2109 struct request *req)
2110{
2111 struct domain_device *dev;
Darrick J. Wong2cd614c2007-07-24 09:33:49 -07002112 int ret, type;
FUJITA Tomonoriba1fc172007-07-09 12:52:08 +09002113 struct request *rsp = req->next_rq;
2114
2115 if (!rsp) {
2116 printk("%s: space for a smp response is missing\n",
Harvey Harrisoncadbd4a2008-07-03 23:47:27 -07002117 __func__);
FUJITA Tomonoriba1fc172007-07-09 12:52:08 +09002118 return -EINVAL;
2119 }
2120
Darrick J. Wong2cd614c2007-07-24 09:33:49 -07002121 /* no rphy means no smp target support (ie aic94xx host) */
James Bottomleyb98e66f2007-12-28 16:35:17 -06002122 if (!rphy)
2123 return sas_smp_host_handler(shost, req, rsp);
2124
Darrick J. Wong2cd614c2007-07-24 09:33:49 -07002125 type = rphy->identify.device_type;
FUJITA Tomonoriba1fc172007-07-09 12:52:08 +09002126
2127 if (type != SAS_EDGE_EXPANDER_DEVICE &&
2128 type != SAS_FANOUT_EXPANDER_DEVICE) {
2129 printk("%s: can we send a smp request to a device?\n",
Harvey Harrisoncadbd4a2008-07-03 23:47:27 -07002130 __func__);
FUJITA Tomonoriba1fc172007-07-09 12:52:08 +09002131 return -EINVAL;
2132 }
2133
2134 dev = sas_find_dev_by_rphy(rphy);
2135 if (!dev) {
Harvey Harrisoncadbd4a2008-07-03 23:47:27 -07002136 printk("%s: fail to find a domain_device?\n", __func__);
FUJITA Tomonoriba1fc172007-07-09 12:52:08 +09002137 return -EINVAL;
2138 }
2139
2140 /* do we need to support multiple segments? */
2141 if (req->bio->bi_vcnt > 1 || rsp->bio->bi_vcnt > 1) {
2142 printk("%s: multiple segments req %u %u, rsp %u %u\n",
Tejun Heob0790412009-05-07 22:24:42 +09002143 __func__, req->bio->bi_vcnt, blk_rq_bytes(req),
2144 rsp->bio->bi_vcnt, blk_rq_bytes(rsp));
FUJITA Tomonoriba1fc172007-07-09 12:52:08 +09002145 return -EINVAL;
2146 }
2147
Tejun Heob0790412009-05-07 22:24:42 +09002148 ret = smp_execute_task(dev, bio_data(req->bio), blk_rq_bytes(req),
2149 bio_data(rsp->bio), blk_rq_bytes(rsp));
James Bottomley2d4b63e2007-12-29 11:49:53 -06002150 if (ret > 0) {
2151 /* positive number is the untransferred residual */
Tejun Heoc3a4d782009-05-07 22:24:37 +09002152 rsp->resid_len = ret;
Tejun Heo5f49f632009-05-19 18:33:05 +09002153 req->resid_len = 0;
James Bottomley2d4b63e2007-12-29 11:49:53 -06002154 ret = 0;
Tejun Heo5f49f632009-05-19 18:33:05 +09002155 } else if (ret == 0) {
2156 rsp->resid_len = 0;
2157 req->resid_len = 0;
James Bottomley2d4b63e2007-12-29 11:49:53 -06002158 }
FUJITA Tomonoriba1fc172007-07-09 12:52:08 +09002159
2160 return ret;
2161}