blob: c1f91b1c27c309301cf67a045e2564e7dd55d997 [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;
Dan Williams0f3fce52012-03-20 13:24:29 -0700205 struct sas_ha_struct *ha = dev->port->ha;
James Bottomley2908d772006-08-29 09:22:51 -0500206 struct expander_device *ex = &dev->ex_dev;
207 struct ex_phy *phy = &ex->ex_phy[phy_id];
James Bottomley2908d772006-08-29 09:22:51 -0500208 struct sas_rphy *rphy = dev->rphy;
Dan Williamsd214d812012-01-16 11:56:50 -0800209 bool new_phy = !phy->phy;
210 char *type;
James Bottomley2908d772006-08-29 09:22:51 -0500211
Dan Williamsd214d812012-01-16 11:56:50 -0800212 if (new_phy) {
Dan Williams0f3fce52012-03-20 13:24:29 -0700213 if (WARN_ON_ONCE(test_bit(SAS_HA_ATA_EH_ACTIVE, &ha->state)))
214 return;
James Bottomley2908d772006-08-29 09:22:51 -0500215 phy->phy = sas_phy_alloc(&rphy->dev, phy_id);
216
217 /* FIXME: error_handling */
218 BUG_ON(!phy->phy);
219 }
220
221 switch (resp->result) {
222 case SMP_RESP_PHY_VACANT:
223 phy->phy_state = PHY_VACANT;
Jack Wang2bc72c92010-10-06 16:05:35 +0800224 break;
James Bottomley2908d772006-08-29 09:22:51 -0500225 default:
226 phy->phy_state = PHY_NOT_PRESENT;
Jack Wang2bc72c92010-10-06 16:05:35 +0800227 break;
James Bottomley2908d772006-08-29 09:22:51 -0500228 case SMP_RESP_FUNC_ACC:
229 phy->phy_state = PHY_EMPTY; /* do not know yet */
230 break;
231 }
232
Dan Williams354cf822012-01-12 17:57:35 -0800233 /* check if anything important changed to squelch debug */
234 dev_type = phy->attached_dev_type;
235 linkrate = phy->linkrate;
236 memcpy(sas_addr, phy->attached_sas_addr, SAS_ADDR_SIZE);
237
238 phy->attached_dev_type = to_dev_type(dr);
Dan Williams0f3fce52012-03-20 13:24:29 -0700239 if (test_bit(SAS_HA_ATA_EH_ACTIVE, &ha->state))
240 goto out;
James Bottomley2908d772006-08-29 09:22:51 -0500241 phy->phy_id = phy_id;
James Bottomley2908d772006-08-29 09:22:51 -0500242 phy->linkrate = dr->linkrate;
243 phy->attached_sata_host = dr->attached_sata_host;
244 phy->attached_sata_dev = dr->attached_sata_dev;
245 phy->attached_sata_ps = dr->attached_sata_ps;
246 phy->attached_iproto = dr->iproto << 1;
247 phy->attached_tproto = dr->tproto << 1;
248 memcpy(phy->attached_sas_addr, dr->attached_sas_addr, SAS_ADDR_SIZE);
249 phy->attached_phy_id = dr->attached_phy_id;
250 phy->phy_change_count = dr->change_count;
251 phy->routing_attr = dr->routing_attr;
252 phy->virtual = dr->virtual;
253 phy->last_da_index = -1;
254
Jack Wangbb041a02011-09-23 14:32:32 +0800255 phy->phy->identify.sas_address = SAS_ADDR(phy->attached_sas_addr);
Dan Williams354cf822012-01-12 17:57:35 -0800256 phy->phy->identify.device_type = dr->attached_dev_type;
James Bottomley2908d772006-08-29 09:22:51 -0500257 phy->phy->identify.initiator_port_protocols = phy->attached_iproto;
258 phy->phy->identify.target_port_protocols = phy->attached_tproto;
Dan Williams77c309f2012-02-08 23:20:41 -0800259 if (!phy->attached_tproto && dr->attached_sata_dev)
260 phy->phy->identify.target_port_protocols = SAS_PROTOCOL_SATA;
James Bottomley2908d772006-08-29 09:22:51 -0500261 phy->phy->identify.phy_identifier = phy_id;
James Bottomleya01e70e2006-09-06 19:28:07 -0500262 phy->phy->minimum_linkrate_hw = dr->hmin_linkrate;
263 phy->phy->maximum_linkrate_hw = dr->hmax_linkrate;
264 phy->phy->minimum_linkrate = dr->pmin_linkrate;
265 phy->phy->maximum_linkrate = dr->pmax_linkrate;
James Bottomley88edf742006-09-06 17:36:13 -0500266 phy->phy->negotiated_linkrate = phy->linkrate;
James Bottomley2908d772006-08-29 09:22:51 -0500267
Dan Williamsd214d812012-01-16 11:56:50 -0800268 if (new_phy)
Jack Wang2bc72c92010-10-06 16:05:35 +0800269 if (sas_phy_add(phy->phy)) {
270 sas_phy_free(phy->phy);
271 return;
272 }
James Bottomley2908d772006-08-29 09:22:51 -0500273
Dan Williams0f3fce52012-03-20 13:24:29 -0700274 out:
Dan Williamsd214d812012-01-16 11:56:50 -0800275 switch (phy->attached_dev_type) {
Dan Williams354cf822012-01-12 17:57:35 -0800276 case SATA_PENDING:
277 type = "stp pending";
278 break;
Dan Williamsd214d812012-01-16 11:56:50 -0800279 case NO_DEVICE:
280 type = "no device";
281 break;
282 case SAS_END_DEV:
283 if (phy->attached_iproto) {
284 if (phy->attached_tproto)
285 type = "host+target";
286 else
287 type = "host";
288 } else {
289 if (dr->attached_sata_dev)
290 type = "stp";
291 else
292 type = "ssp";
293 }
294 break;
295 case EDGE_DEV:
296 case FANOUT_DEV:
297 type = "smp";
298 break;
299 default:
300 type = "unknown";
301 }
James Bottomley2908d772006-08-29 09:22:51 -0500302
Dan Williams354cf822012-01-12 17:57:35 -0800303 /* this routine is polled by libata error recovery so filter
304 * unimportant messages
305 */
306 if (new_phy || phy->attached_dev_type != dev_type ||
307 phy->linkrate != linkrate ||
308 SAS_ADDR(phy->attached_sas_addr) != SAS_ADDR(sas_addr))
309 /* pass */;
310 else
311 return;
312
Dan Williams0f3fce52012-03-20 13:24:29 -0700313 /* if the attached device type changed and ata_eh is active,
314 * make sure we run revalidation when eh completes (see:
315 * sas_enable_revalidation)
316 */
317 if (test_bit(SAS_HA_ATA_EH_ACTIVE, &ha->state))
318 set_bit(DISCE_REVALIDATE_DOMAIN, &dev->port->disc.pending);
319
320 SAS_DPRINTK("%sex %016llx phy%02d:%c:%X attached: %016llx (%s)\n",
321 test_bit(SAS_HA_ATA_EH_ACTIVE, &ha->state) ? "ata: " : "",
Dan Williamsd214d812012-01-16 11:56:50 -0800322 SAS_ADDR(dev->sas_addr), phy->phy_id,
323 sas_route_char(dev, phy), phy->linkrate,
324 SAS_ADDR(phy->attached_sas_addr), type);
James Bottomley2908d772006-08-29 09:22:51 -0500325}
326
Dan Williamsb52df412011-11-30 23:23:33 -0800327/* check if we have an existing attached ata device on this expander phy */
Dan Williams81c757b2011-12-02 16:07:01 -0800328struct domain_device *sas_ex_to_ata(struct domain_device *ex_dev, int phy_id)
Dan Williamsb52df412011-11-30 23:23:33 -0800329{
330 struct ex_phy *ex_phy = &ex_dev->ex_dev.ex_phy[phy_id];
331 struct domain_device *dev;
332 struct sas_rphy *rphy;
333
334 if (!ex_phy->port)
335 return NULL;
336
337 rphy = ex_phy->port->rphy;
338 if (!rphy)
339 return NULL;
340
341 dev = sas_find_dev_by_rphy(rphy);
342
343 if (dev && dev_is_sata(dev))
344 return dev;
345
346 return NULL;
347}
348
James Bottomley2908d772006-08-29 09:22:51 -0500349#define DISCOVER_REQ_SIZE 16
350#define DISCOVER_RESP_SIZE 56
351
James Bottomley1acce192006-08-22 12:39:19 -0500352static int sas_ex_phy_discover_helper(struct domain_device *dev, u8 *disc_req,
353 u8 *disc_resp, int single)
354{
Dan Williams354cf822012-01-12 17:57:35 -0800355 struct discover_resp *dr;
356 int res;
James Bottomley1acce192006-08-22 12:39:19 -0500357
358 disc_req[9] = single;
James Bottomley1acce192006-08-22 12:39:19 -0500359
Dan Williams354cf822012-01-12 17:57:35 -0800360 res = smp_execute_task(dev, disc_req, DISCOVER_REQ_SIZE,
361 disc_resp, DISCOVER_RESP_SIZE);
362 if (res)
363 return res;
364 dr = &((struct smp_resp *)disc_resp)->disc;
365 if (memcmp(dev->sas_addr, dr->attached_sas_addr, SAS_ADDR_SIZE) == 0) {
366 sas_printk("Found loopback topology, just ignore it!\n");
367 return 0;
James Bottomley1acce192006-08-22 12:39:19 -0500368 }
369 sas_set_ex_phy(dev, single, disc_resp);
370 return 0;
371}
372
Dan Williams354cf822012-01-12 17:57:35 -0800373int sas_ex_phy_discover(struct domain_device *dev, int single)
James Bottomley2908d772006-08-29 09:22:51 -0500374{
375 struct expander_device *ex = &dev->ex_dev;
376 int res = 0;
377 u8 *disc_req;
378 u8 *disc_resp;
379
380 disc_req = alloc_smp_req(DISCOVER_REQ_SIZE);
381 if (!disc_req)
382 return -ENOMEM;
383
384 disc_resp = alloc_smp_req(DISCOVER_RESP_SIZE);
385 if (!disc_resp) {
386 kfree(disc_req);
387 return -ENOMEM;
388 }
389
390 disc_req[1] = SMP_DISCOVER;
391
392 if (0 <= single && single < ex->num_phys) {
James Bottomley1acce192006-08-22 12:39:19 -0500393 res = sas_ex_phy_discover_helper(dev, disc_req, disc_resp, single);
James Bottomley2908d772006-08-29 09:22:51 -0500394 } else {
395 int i;
396
397 for (i = 0; i < ex->num_phys; i++) {
James Bottomley1acce192006-08-22 12:39:19 -0500398 res = sas_ex_phy_discover_helper(dev, disc_req,
399 disc_resp, i);
James Bottomley2908d772006-08-29 09:22:51 -0500400 if (res)
401 goto out_err;
James Bottomley2908d772006-08-29 09:22:51 -0500402 }
403 }
404out_err:
405 kfree(disc_resp);
406 kfree(disc_req);
407 return res;
408}
409
410static int sas_expander_discover(struct domain_device *dev)
411{
412 struct expander_device *ex = &dev->ex_dev;
413 int res = -ENOMEM;
414
415 ex->ex_phy = kzalloc(sizeof(*ex->ex_phy)*ex->num_phys, GFP_KERNEL);
416 if (!ex->ex_phy)
417 return -ENOMEM;
418
419 res = sas_ex_phy_discover(dev, -1);
420 if (res)
421 goto out_err;
422
423 return 0;
424 out_err:
425 kfree(ex->ex_phy);
426 ex->ex_phy = NULL;
427 return res;
428}
429
430#define MAX_EXPANDER_PHYS 128
431
432static void ex_assign_report_general(struct domain_device *dev,
433 struct smp_resp *resp)
434{
435 struct report_general_resp *rg = &resp->rg;
436
437 dev->ex_dev.ex_change_count = be16_to_cpu(rg->change_count);
438 dev->ex_dev.max_route_indexes = be16_to_cpu(rg->route_indexes);
439 dev->ex_dev.num_phys = min(rg->num_phys, (u8)MAX_EXPANDER_PHYS);
Luben Tuikovffaac8f2011-09-22 09:41:36 -0700440 dev->ex_dev.t2t_supp = rg->t2t_supp;
James Bottomley2908d772006-08-29 09:22:51 -0500441 dev->ex_dev.conf_route_table = rg->conf_route_table;
442 dev->ex_dev.configuring = rg->configuring;
443 memcpy(dev->ex_dev.enclosure_logical_id, rg->enclosure_logical_id, 8);
444}
445
446#define RG_REQ_SIZE 8
447#define RG_RESP_SIZE 32
448
449static int sas_ex_general(struct domain_device *dev)
450{
451 u8 *rg_req;
452 struct smp_resp *rg_resp;
453 int res;
454 int i;
455
456 rg_req = alloc_smp_req(RG_REQ_SIZE);
457 if (!rg_req)
458 return -ENOMEM;
459
460 rg_resp = alloc_smp_resp(RG_RESP_SIZE);
461 if (!rg_resp) {
462 kfree(rg_req);
463 return -ENOMEM;
464 }
465
466 rg_req[1] = SMP_REPORT_GENERAL;
467
468 for (i = 0; i < 5; i++) {
469 res = smp_execute_task(dev, rg_req, RG_REQ_SIZE, rg_resp,
470 RG_RESP_SIZE);
471
472 if (res) {
473 SAS_DPRINTK("RG to ex %016llx failed:0x%x\n",
474 SAS_ADDR(dev->sas_addr), res);
475 goto out;
476 } else if (rg_resp->result != SMP_RESP_FUNC_ACC) {
477 SAS_DPRINTK("RG:ex %016llx returned SMP result:0x%x\n",
478 SAS_ADDR(dev->sas_addr), rg_resp->result);
479 res = rg_resp->result;
480 goto out;
481 }
482
483 ex_assign_report_general(dev, rg_resp);
484
485 if (dev->ex_dev.configuring) {
486 SAS_DPRINTK("RG: ex %llx self-configuring...\n",
487 SAS_ADDR(dev->sas_addr));
488 schedule_timeout_interruptible(5*HZ);
489 } else
490 break;
491 }
492out:
493 kfree(rg_req);
494 kfree(rg_resp);
495 return res;
496}
497
498static void ex_assign_manuf_info(struct domain_device *dev, void
499 *_mi_resp)
500{
501 u8 *mi_resp = _mi_resp;
502 struct sas_rphy *rphy = dev->rphy;
503 struct sas_expander_device *edev = rphy_to_expander_device(rphy);
504
505 memcpy(edev->vendor_id, mi_resp + 12, SAS_EXPANDER_VENDOR_ID_LEN);
506 memcpy(edev->product_id, mi_resp + 20, SAS_EXPANDER_PRODUCT_ID_LEN);
507 memcpy(edev->product_rev, mi_resp + 36,
508 SAS_EXPANDER_PRODUCT_REV_LEN);
509
510 if (mi_resp[8] & 1) {
511 memcpy(edev->component_vendor_id, mi_resp + 40,
512 SAS_EXPANDER_COMPONENT_VENDOR_ID_LEN);
513 edev->component_id = mi_resp[48] << 8 | mi_resp[49];
514 edev->component_revision_id = mi_resp[50];
515 }
516}
517
518#define MI_REQ_SIZE 8
519#define MI_RESP_SIZE 64
520
521static int sas_ex_manuf_info(struct domain_device *dev)
522{
523 u8 *mi_req;
524 u8 *mi_resp;
525 int res;
526
527 mi_req = alloc_smp_req(MI_REQ_SIZE);
528 if (!mi_req)
529 return -ENOMEM;
530
531 mi_resp = alloc_smp_resp(MI_RESP_SIZE);
532 if (!mi_resp) {
533 kfree(mi_req);
534 return -ENOMEM;
535 }
536
537 mi_req[1] = SMP_REPORT_MANUF_INFO;
538
539 res = smp_execute_task(dev, mi_req, MI_REQ_SIZE, mi_resp,MI_RESP_SIZE);
540 if (res) {
541 SAS_DPRINTK("MI: ex %016llx failed:0x%x\n",
542 SAS_ADDR(dev->sas_addr), res);
543 goto out;
544 } else if (mi_resp[2] != SMP_RESP_FUNC_ACC) {
545 SAS_DPRINTK("MI ex %016llx returned SMP result:0x%x\n",
546 SAS_ADDR(dev->sas_addr), mi_resp[2]);
547 goto out;
548 }
549
550 ex_assign_manuf_info(dev, mi_resp);
551out:
552 kfree(mi_req);
553 kfree(mi_resp);
554 return res;
555}
556
557#define PC_REQ_SIZE 44
558#define PC_RESP_SIZE 8
559
560int sas_smp_phy_control(struct domain_device *dev, int phy_id,
James Bottomleya01e70e2006-09-06 19:28:07 -0500561 enum phy_func phy_func,
562 struct sas_phy_linkrates *rates)
James Bottomley2908d772006-08-29 09:22:51 -0500563{
564 u8 *pc_req;
565 u8 *pc_resp;
566 int res;
567
568 pc_req = alloc_smp_req(PC_REQ_SIZE);
569 if (!pc_req)
570 return -ENOMEM;
571
572 pc_resp = alloc_smp_resp(PC_RESP_SIZE);
573 if (!pc_resp) {
574 kfree(pc_req);
575 return -ENOMEM;
576 }
577
578 pc_req[1] = SMP_PHY_CONTROL;
579 pc_req[9] = phy_id;
580 pc_req[10]= phy_func;
James Bottomleya01e70e2006-09-06 19:28:07 -0500581 if (rates) {
582 pc_req[32] = rates->minimum_linkrate << 4;
583 pc_req[33] = rates->maximum_linkrate << 4;
584 }
James Bottomley2908d772006-08-29 09:22:51 -0500585
586 res = smp_execute_task(dev, pc_req, PC_REQ_SIZE, pc_resp,PC_RESP_SIZE);
587
588 kfree(pc_resp);
589 kfree(pc_req);
590 return res;
591}
592
593static void sas_ex_disable_phy(struct domain_device *dev, int phy_id)
594{
595 struct expander_device *ex = &dev->ex_dev;
596 struct ex_phy *phy = &ex->ex_phy[phy_id];
597
James Bottomleya01e70e2006-09-06 19:28:07 -0500598 sas_smp_phy_control(dev, phy_id, PHY_FUNC_DISABLE, NULL);
James Bottomley88edf742006-09-06 17:36:13 -0500599 phy->linkrate = SAS_PHY_DISABLED;
James Bottomley2908d772006-08-29 09:22:51 -0500600}
601
602static void sas_ex_disable_port(struct domain_device *dev, u8 *sas_addr)
603{
604 struct expander_device *ex = &dev->ex_dev;
605 int i;
606
607 for (i = 0; i < ex->num_phys; i++) {
608 struct ex_phy *phy = &ex->ex_phy[i];
609
610 if (phy->phy_state == PHY_VACANT ||
611 phy->phy_state == PHY_NOT_PRESENT)
612 continue;
613
614 if (SAS_ADDR(phy->attached_sas_addr) == SAS_ADDR(sas_addr))
615 sas_ex_disable_phy(dev, i);
616 }
617}
618
619static int sas_dev_present_in_domain(struct asd_sas_port *port,
620 u8 *sas_addr)
621{
622 struct domain_device *dev;
623
624 if (SAS_ADDR(port->sas_addr) == SAS_ADDR(sas_addr))
625 return 1;
626 list_for_each_entry(dev, &port->dev_list, dev_list_node) {
627 if (SAS_ADDR(dev->sas_addr) == SAS_ADDR(sas_addr))
628 return 1;
629 }
630 return 0;
631}
632
633#define RPEL_REQ_SIZE 16
634#define RPEL_RESP_SIZE 32
635int sas_smp_get_phy_events(struct sas_phy *phy)
636{
637 int res;
Jesper Juhl92631fa2007-07-28 01:13:33 +0200638 u8 *req;
639 u8 *resp;
James Bottomley2908d772006-08-29 09:22:51 -0500640 struct sas_rphy *rphy = dev_to_rphy(phy->dev.parent);
641 struct domain_device *dev = sas_find_dev_by_rphy(rphy);
James Bottomley2908d772006-08-29 09:22:51 -0500642
Jesper Juhl92631fa2007-07-28 01:13:33 +0200643 req = alloc_smp_req(RPEL_REQ_SIZE);
644 if (!req)
James Bottomley2908d772006-08-29 09:22:51 -0500645 return -ENOMEM;
646
Jesper Juhl92631fa2007-07-28 01:13:33 +0200647 resp = alloc_smp_resp(RPEL_RESP_SIZE);
648 if (!resp) {
649 kfree(req);
650 return -ENOMEM;
651 }
652
James Bottomley2908d772006-08-29 09:22:51 -0500653 req[1] = SMP_REPORT_PHY_ERR_LOG;
654 req[9] = phy->number;
655
656 res = smp_execute_task(dev, req, RPEL_REQ_SIZE,
657 resp, RPEL_RESP_SIZE);
658
659 if (!res)
660 goto out;
661
662 phy->invalid_dword_count = scsi_to_u32(&resp[12]);
663 phy->running_disparity_error_count = scsi_to_u32(&resp[16]);
664 phy->loss_of_dword_sync_count = scsi_to_u32(&resp[20]);
665 phy->phy_reset_problem_count = scsi_to_u32(&resp[24]);
666
667 out:
668 kfree(resp);
669 return res;
670
671}
672
James Bottomleyb9142172007-07-22 13:15:55 -0500673#ifdef CONFIG_SCSI_SAS_ATA
674
James Bottomley2908d772006-08-29 09:22:51 -0500675#define RPS_REQ_SIZE 16
676#define RPS_RESP_SIZE 60
677
Dan Williams354cf822012-01-12 17:57:35 -0800678int sas_get_report_phy_sata(struct domain_device *dev, int phy_id,
679 struct smp_resp *rps_resp)
James Bottomley2908d772006-08-29 09:22:51 -0500680{
681 int res;
682 u8 *rps_req = alloc_smp_req(RPS_REQ_SIZE);
James Bottomley1acce192006-08-22 12:39:19 -0500683 u8 *resp = (u8 *)rps_resp;
James Bottomley2908d772006-08-29 09:22:51 -0500684
685 if (!rps_req)
686 return -ENOMEM;
687
688 rps_req[1] = SMP_REPORT_PHY_SATA;
689 rps_req[9] = phy_id;
690
691 res = smp_execute_task(dev, rps_req, RPS_REQ_SIZE,
692 rps_resp, RPS_RESP_SIZE);
693
James Bottomley1acce192006-08-22 12:39:19 -0500694 /* 0x34 is the FIS type for the D2H fis. There's a potential
695 * standards cockup here. sas-2 explicitly specifies the FIS
696 * should be encoded so that FIS type is in resp[24].
697 * However, some expanders endian reverse this. Undo the
698 * reversal here */
699 if (!res && resp[27] == 0x34 && resp[24] != 0x34) {
700 int i;
701
702 for (i = 0; i < 5; i++) {
703 int j = 24 + (i*4);
704 u8 a, b;
705 a = resp[j + 0];
706 b = resp[j + 1];
707 resp[j + 0] = resp[j + 3];
708 resp[j + 1] = resp[j + 2];
709 resp[j + 2] = b;
710 resp[j + 3] = a;
711 }
712 }
713
James Bottomley2908d772006-08-29 09:22:51 -0500714 kfree(rps_req);
James Bottomley1acce192006-08-22 12:39:19 -0500715 return res;
James Bottomley2908d772006-08-29 09:22:51 -0500716}
James Bottomleyb9142172007-07-22 13:15:55 -0500717#endif
James Bottomley2908d772006-08-29 09:22:51 -0500718
719static void sas_ex_get_linkrate(struct domain_device *parent,
720 struct domain_device *child,
721 struct ex_phy *parent_phy)
722{
723 struct expander_device *parent_ex = &parent->ex_dev;
724 struct sas_port *port;
725 int i;
726
727 child->pathways = 0;
728
729 port = parent_phy->port;
730
731 for (i = 0; i < parent_ex->num_phys; i++) {
732 struct ex_phy *phy = &parent_ex->ex_phy[i];
733
734 if (phy->phy_state == PHY_VACANT ||
735 phy->phy_state == PHY_NOT_PRESENT)
736 continue;
737
738 if (SAS_ADDR(phy->attached_sas_addr) ==
739 SAS_ADDR(child->sas_addr)) {
740
741 child->min_linkrate = min(parent->min_linkrate,
742 phy->linkrate);
743 child->max_linkrate = max(parent->max_linkrate,
744 phy->linkrate);
745 child->pathways++;
746 sas_port_add_phy(port, phy->phy);
747 }
748 }
749 child->linkrate = min(parent_phy->linkrate, child->max_linkrate);
750 child->pathways = min(child->pathways, parent->pathways);
751}
752
753static struct domain_device *sas_ex_discover_end_dev(
754 struct domain_device *parent, int phy_id)
755{
756 struct expander_device *parent_ex = &parent->ex_dev;
757 struct ex_phy *phy = &parent_ex->ex_phy[phy_id];
758 struct domain_device *child = NULL;
759 struct sas_rphy *rphy;
760 int res;
761
762 if (phy->attached_sata_host || phy->attached_sata_ps)
763 return NULL;
764
Dan Williams735f7d22011-11-17 17:59:47 -0800765 child = sas_alloc_device();
James Bottomley2908d772006-08-29 09:22:51 -0500766 if (!child)
767 return NULL;
768
Dan Williams735f7d22011-11-17 17:59:47 -0800769 kref_get(&parent->kref);
James Bottomley2908d772006-08-29 09:22:51 -0500770 child->parent = parent;
771 child->port = parent->port;
772 child->iproto = phy->attached_iproto;
773 memcpy(child->sas_addr, phy->attached_sas_addr, SAS_ADDR_SIZE);
774 sas_hash_addr(child->hashed_sas_addr, child->sas_addr);
James Bottomley024879e2006-11-15 18:03:07 -0600775 if (!phy->port) {
776 phy->port = sas_port_alloc(&parent->rphy->dev, phy_id);
777 if (unlikely(!phy->port))
778 goto out_err;
779 if (unlikely(sas_port_add(phy->port) != 0)) {
780 sas_port_free(phy->port);
781 goto out_err;
782 }
783 }
James Bottomley2908d772006-08-29 09:22:51 -0500784 sas_ex_get_linkrate(parent, child, phy);
Dan Williamsf41a0c42011-12-21 21:33:17 -0800785 sas_device_set_phy(child, phy->port);
James Bottomley2908d772006-08-29 09:22:51 -0500786
James Bottomleyb9142172007-07-22 13:15:55 -0500787#ifdef CONFIG_SCSI_SAS_ATA
Darrick J. Wong5929faf2007-11-05 11:51:17 -0800788 if ((phy->attached_tproto & SAS_PROTOCOL_STP) || phy->attached_sata_dev) {
Dan Williams354cf822012-01-12 17:57:35 -0800789 res = sas_get_ata_info(child, phy);
790 if (res)
James Bottomley024879e2006-11-15 18:03:07 -0600791 goto out_free;
James Bottomley1acce192006-08-22 12:39:19 -0500792
793 rphy = sas_end_device_alloc(phy->port);
James Bottomley528fd552006-10-16 10:57:05 -0500794 if (unlikely(!rphy))
795 goto out_free;
James Bottomley1acce192006-08-22 12:39:19 -0500796
James Bottomley2908d772006-08-29 09:22:51 -0500797 sas_init_dev(child);
James Bottomley1acce192006-08-22 12:39:19 -0500798
799 child->rphy = rphy;
Dan Williams94876692012-03-20 10:53:24 -0700800 get_device(&rphy->dev);
James Bottomley1acce192006-08-22 12:39:19 -0500801
Dan Williams87c83312011-11-17 17:59:51 -0800802 list_add_tail(&child->disco_list_node, &parent->port->disco_list);
James Bottomley1acce192006-08-22 12:39:19 -0500803
James Bottomley2908d772006-08-29 09:22:51 -0500804 res = sas_discover_sata(child);
805 if (res) {
806 SAS_DPRINTK("sas_discover_sata() for device %16llx at "
807 "%016llx:0x%x returned 0x%x\n",
808 SAS_ADDR(child->sas_addr),
809 SAS_ADDR(parent->sas_addr), phy_id, res);
James Bottomley1acce192006-08-22 12:39:19 -0500810 goto out_list_del;
James Bottomley2908d772006-08-29 09:22:51 -0500811 }
James Bottomleyb9142172007-07-22 13:15:55 -0500812 } else
813#endif
Darrick J. Wong5929faf2007-11-05 11:51:17 -0800814 if (phy->attached_tproto & SAS_PROTOCOL_SSP) {
James Bottomley2908d772006-08-29 09:22:51 -0500815 child->dev_type = SAS_END_DEV;
816 rphy = sas_end_device_alloc(phy->port);
817 /* FIXME: error handling */
James Bottomley024879e2006-11-15 18:03:07 -0600818 if (unlikely(!rphy))
819 goto out_free;
James Bottomley2908d772006-08-29 09:22:51 -0500820 child->tproto = phy->attached_tproto;
821 sas_init_dev(child);
822
823 child->rphy = rphy;
Dan Williams94876692012-03-20 10:53:24 -0700824 get_device(&rphy->dev);
James Bottomley2908d772006-08-29 09:22:51 -0500825 sas_fill_in_rphy(child, rphy);
826
Dan Williams92625f92012-01-18 20:14:01 -0800827 list_add_tail(&child->disco_list_node, &parent->port->disco_list);
James Bottomley2908d772006-08-29 09:22:51 -0500828
829 res = sas_discover_end_dev(child);
830 if (res) {
831 SAS_DPRINTK("sas_discover_end_dev() for device %16llx "
832 "at %016llx:0x%x returned 0x%x\n",
833 SAS_ADDR(child->sas_addr),
834 SAS_ADDR(parent->sas_addr), phy_id, res);
James Bottomley024879e2006-11-15 18:03:07 -0600835 goto out_list_del;
James Bottomley2908d772006-08-29 09:22:51 -0500836 }
837 } else {
838 SAS_DPRINTK("target proto 0x%x at %016llx:0x%x not handled\n",
839 phy->attached_tproto, SAS_ADDR(parent->sas_addr),
840 phy_id);
James Bottomleyb9142172007-07-22 13:15:55 -0500841 goto out_free;
James Bottomley2908d772006-08-29 09:22:51 -0500842 }
843
844 list_add_tail(&child->siblings, &parent_ex->children);
845 return child;
James Bottomley024879e2006-11-15 18:03:07 -0600846
847 out_list_del:
Darrick J. Wong6f63caa2007-01-26 14:08:43 -0800848 sas_rphy_free(child->rphy);
Dan Williams87c83312011-11-17 17:59:51 -0800849 list_del(&child->disco_list_node);
Dan Williams1a34c062011-09-21 22:05:34 -0700850 spin_lock_irq(&parent->port->dev_list_lock);
James Bottomley024879e2006-11-15 18:03:07 -0600851 list_del(&child->dev_list_node);
Dan Williams1a34c062011-09-21 22:05:34 -0700852 spin_unlock_irq(&parent->port->dev_list_lock);
James Bottomley024879e2006-11-15 18:03:07 -0600853 out_free:
854 sas_port_delete(phy->port);
855 out_err:
856 phy->port = NULL;
Dan Williams735f7d22011-11-17 17:59:47 -0800857 sas_put_device(child);
James Bottomley024879e2006-11-15 18:03:07 -0600858 return NULL;
James Bottomley2908d772006-08-29 09:22:51 -0500859}
860
Darrick J. Wong423f7cf2007-01-30 12:07:27 -0800861/* See if this phy is part of a wide port */
862static int sas_ex_join_wide_port(struct domain_device *parent, int phy_id)
863{
864 struct ex_phy *phy = &parent->ex_dev.ex_phy[phy_id];
865 int i;
866
867 for (i = 0; i < parent->ex_dev.num_phys; i++) {
868 struct ex_phy *ephy = &parent->ex_dev.ex_phy[i];
869
870 if (ephy == phy)
871 continue;
872
873 if (!memcmp(phy->attached_sas_addr, ephy->attached_sas_addr,
874 SAS_ADDR_SIZE) && ephy->port) {
875 sas_port_add_phy(ephy->port, phy->phy);
Tom Peng19252de2009-07-17 16:02:04 +0800876 phy->port = ephy->port;
Darrick J. Wong423f7cf2007-01-30 12:07:27 -0800877 phy->phy_state = PHY_DEVICE_DISCOVERED;
878 return 0;
879 }
880 }
881
882 return -ENODEV;
883}
884
James Bottomley2908d772006-08-29 09:22:51 -0500885static struct domain_device *sas_ex_discover_expander(
886 struct domain_device *parent, int phy_id)
887{
888 struct sas_expander_device *parent_ex = rphy_to_expander_device(parent->rphy);
889 struct ex_phy *phy = &parent->ex_dev.ex_phy[phy_id];
890 struct domain_device *child = NULL;
891 struct sas_rphy *rphy;
892 struct sas_expander_device *edev;
893 struct asd_sas_port *port;
894 int res;
895
896 if (phy->routing_attr == DIRECT_ROUTING) {
897 SAS_DPRINTK("ex %016llx:0x%x:D <--> ex %016llx:0x%x is not "
898 "allowed\n",
899 SAS_ADDR(parent->sas_addr), phy_id,
900 SAS_ADDR(phy->attached_sas_addr),
901 phy->attached_phy_id);
902 return NULL;
903 }
Dan Williams735f7d22011-11-17 17:59:47 -0800904 child = sas_alloc_device();
James Bottomley2908d772006-08-29 09:22:51 -0500905 if (!child)
906 return NULL;
907
908 phy->port = sas_port_alloc(&parent->rphy->dev, phy_id);
909 /* FIXME: better error handling */
910 BUG_ON(sas_port_add(phy->port) != 0);
911
912
913 switch (phy->attached_dev_type) {
914 case EDGE_DEV:
915 rphy = sas_expander_alloc(phy->port,
916 SAS_EDGE_EXPANDER_DEVICE);
917 break;
918 case FANOUT_DEV:
919 rphy = sas_expander_alloc(phy->port,
920 SAS_FANOUT_EXPANDER_DEVICE);
921 break;
922 default:
923 rphy = NULL; /* shut gcc up */
924 BUG();
925 }
926 port = parent->port;
927 child->rphy = rphy;
Dan Williams94876692012-03-20 10:53:24 -0700928 get_device(&rphy->dev);
James Bottomley2908d772006-08-29 09:22:51 -0500929 edev = rphy_to_expander_device(rphy);
930 child->dev_type = phy->attached_dev_type;
Dan Williams735f7d22011-11-17 17:59:47 -0800931 kref_get(&parent->kref);
James Bottomley2908d772006-08-29 09:22:51 -0500932 child->parent = parent;
933 child->port = port;
934 child->iproto = phy->attached_iproto;
935 child->tproto = phy->attached_tproto;
936 memcpy(child->sas_addr, phy->attached_sas_addr, SAS_ADDR_SIZE);
937 sas_hash_addr(child->hashed_sas_addr, child->sas_addr);
938 sas_ex_get_linkrate(parent, child, phy);
939 edev->level = parent_ex->level + 1;
940 parent->port->disc.max_level = max(parent->port->disc.max_level,
941 edev->level);
942 sas_init_dev(child);
943 sas_fill_in_rphy(child, rphy);
944 sas_rphy_add(rphy);
945
James Bottomley9d720d82007-07-16 13:15:51 -0500946 spin_lock_irq(&parent->port->dev_list_lock);
James Bottomley2908d772006-08-29 09:22:51 -0500947 list_add_tail(&child->dev_list_node, &parent->port->dev_list);
James Bottomley9d720d82007-07-16 13:15:51 -0500948 spin_unlock_irq(&parent->port->dev_list_lock);
James Bottomley2908d772006-08-29 09:22:51 -0500949
950 res = sas_discover_expander(child);
951 if (res) {
Dan Williams94876692012-03-20 10:53:24 -0700952 sas_rphy_delete(rphy);
Luben Tuikov5911e962011-07-26 23:10:48 -0700953 spin_lock_irq(&parent->port->dev_list_lock);
954 list_del(&child->dev_list_node);
955 spin_unlock_irq(&parent->port->dev_list_lock);
Dan Williams735f7d22011-11-17 17:59:47 -0800956 sas_put_device(child);
James Bottomley2908d772006-08-29 09:22:51 -0500957 return NULL;
958 }
959 list_add_tail(&child->siblings, &parent->ex_dev.children);
960 return child;
961}
962
963static int sas_ex_discover_dev(struct domain_device *dev, int phy_id)
964{
965 struct expander_device *ex = &dev->ex_dev;
966 struct ex_phy *ex_phy = &ex->ex_phy[phy_id];
967 struct domain_device *child = NULL;
968 int res = 0;
969
970 /* Phy state */
James Bottomley88edf742006-09-06 17:36:13 -0500971 if (ex_phy->linkrate == SAS_SATA_SPINUP_HOLD) {
James Bottomleya01e70e2006-09-06 19:28:07 -0500972 if (!sas_smp_phy_control(dev, phy_id, PHY_FUNC_LINK_RESET, NULL))
James Bottomley2908d772006-08-29 09:22:51 -0500973 res = sas_ex_phy_discover(dev, phy_id);
974 if (res)
975 return res;
976 }
977
978 /* Parent and domain coherency */
979 if (!dev->parent && (SAS_ADDR(ex_phy->attached_sas_addr) ==
980 SAS_ADDR(dev->port->sas_addr))) {
981 sas_add_parent_port(dev, phy_id);
982 return 0;
983 }
984 if (dev->parent && (SAS_ADDR(ex_phy->attached_sas_addr) ==
985 SAS_ADDR(dev->parent->sas_addr))) {
986 sas_add_parent_port(dev, phy_id);
987 if (ex_phy->routing_attr == TABLE_ROUTING)
988 sas_configure_phy(dev, phy_id, dev->port->sas_addr, 1);
989 return 0;
990 }
991
992 if (sas_dev_present_in_domain(dev->port, ex_phy->attached_sas_addr))
993 sas_ex_disable_port(dev, ex_phy->attached_sas_addr);
994
995 if (ex_phy->attached_dev_type == NO_DEVICE) {
996 if (ex_phy->routing_attr == DIRECT_ROUTING) {
997 memset(ex_phy->attached_sas_addr, 0, SAS_ADDR_SIZE);
998 sas_configure_routing(dev, ex_phy->attached_sas_addr);
999 }
1000 return 0;
James Bottomley88edf742006-09-06 17:36:13 -05001001 } else if (ex_phy->linkrate == SAS_LINK_RATE_UNKNOWN)
James Bottomley2908d772006-08-29 09:22:51 -05001002 return 0;
1003
1004 if (ex_phy->attached_dev_type != SAS_END_DEV &&
1005 ex_phy->attached_dev_type != FANOUT_DEV &&
Dan Williams354cf822012-01-12 17:57:35 -08001006 ex_phy->attached_dev_type != EDGE_DEV &&
1007 ex_phy->attached_dev_type != SATA_PENDING) {
James Bottomley2908d772006-08-29 09:22:51 -05001008 SAS_DPRINTK("unknown device type(0x%x) attached to ex %016llx "
1009 "phy 0x%x\n", ex_phy->attached_dev_type,
1010 SAS_ADDR(dev->sas_addr),
1011 phy_id);
1012 return 0;
1013 }
1014
1015 res = sas_configure_routing(dev, ex_phy->attached_sas_addr);
1016 if (res) {
1017 SAS_DPRINTK("configure routing for dev %016llx "
1018 "reported 0x%x. Forgotten\n",
1019 SAS_ADDR(ex_phy->attached_sas_addr), res);
1020 sas_disable_routing(dev, ex_phy->attached_sas_addr);
1021 return res;
1022 }
1023
Darrick J. Wong423f7cf2007-01-30 12:07:27 -08001024 res = sas_ex_join_wide_port(dev, phy_id);
1025 if (!res) {
1026 SAS_DPRINTK("Attaching ex phy%d to wide port %016llx\n",
1027 phy_id, SAS_ADDR(ex_phy->attached_sas_addr));
1028 return res;
1029 }
1030
James Bottomley2908d772006-08-29 09:22:51 -05001031 switch (ex_phy->attached_dev_type) {
1032 case SAS_END_DEV:
Dan Williams354cf822012-01-12 17:57:35 -08001033 case SATA_PENDING:
James Bottomley2908d772006-08-29 09:22:51 -05001034 child = sas_ex_discover_end_dev(dev, phy_id);
1035 break;
1036 case FANOUT_DEV:
1037 if (SAS_ADDR(dev->port->disc.fanout_sas_addr)) {
1038 SAS_DPRINTK("second fanout expander %016llx phy 0x%x "
1039 "attached to ex %016llx phy 0x%x\n",
1040 SAS_ADDR(ex_phy->attached_sas_addr),
1041 ex_phy->attached_phy_id,
1042 SAS_ADDR(dev->sas_addr),
1043 phy_id);
1044 sas_ex_disable_phy(dev, phy_id);
1045 break;
1046 } else
1047 memcpy(dev->port->disc.fanout_sas_addr,
1048 ex_phy->attached_sas_addr, SAS_ADDR_SIZE);
1049 /* fallthrough */
1050 case EDGE_DEV:
1051 child = sas_ex_discover_expander(dev, phy_id);
1052 break;
1053 default:
1054 break;
1055 }
1056
1057 if (child) {
1058 int i;
1059
1060 for (i = 0; i < ex->num_phys; i++) {
1061 if (ex->ex_phy[i].phy_state == PHY_VACANT ||
1062 ex->ex_phy[i].phy_state == PHY_NOT_PRESENT)
1063 continue;
Tom Peng19252de2009-07-17 16:02:04 +08001064 /*
1065 * Due to races, the phy might not get added to the
1066 * wide port, so we add the phy to the wide port here.
1067 */
James Bottomley2908d772006-08-29 09:22:51 -05001068 if (SAS_ADDR(ex->ex_phy[i].attached_sas_addr) ==
Tom Peng19252de2009-07-17 16:02:04 +08001069 SAS_ADDR(child->sas_addr)) {
James Bottomley2908d772006-08-29 09:22:51 -05001070 ex->ex_phy[i].phy_state= PHY_DEVICE_DISCOVERED;
Tom Peng19252de2009-07-17 16:02:04 +08001071 res = sas_ex_join_wide_port(dev, i);
1072 if (!res)
1073 SAS_DPRINTK("Attaching ex phy%d to wide port %016llx\n",
1074 i, SAS_ADDR(ex->ex_phy[i].attached_sas_addr));
1075
1076 }
James Bottomley2908d772006-08-29 09:22:51 -05001077 }
1078 }
1079
1080 return res;
1081}
1082
1083static int sas_find_sub_addr(struct domain_device *dev, u8 *sub_addr)
1084{
1085 struct expander_device *ex = &dev->ex_dev;
1086 int i;
1087
1088 for (i = 0; i < ex->num_phys; i++) {
1089 struct ex_phy *phy = &ex->ex_phy[i];
1090
1091 if (phy->phy_state == PHY_VACANT ||
1092 phy->phy_state == PHY_NOT_PRESENT)
1093 continue;
1094
1095 if ((phy->attached_dev_type == EDGE_DEV ||
1096 phy->attached_dev_type == FANOUT_DEV) &&
1097 phy->routing_attr == SUBTRACTIVE_ROUTING) {
1098
1099 memcpy(sub_addr, phy->attached_sas_addr,SAS_ADDR_SIZE);
1100
1101 return 1;
1102 }
1103 }
1104 return 0;
1105}
1106
1107static int sas_check_level_subtractive_boundary(struct domain_device *dev)
1108{
1109 struct expander_device *ex = &dev->ex_dev;
1110 struct domain_device *child;
1111 u8 sub_addr[8] = {0, };
1112
1113 list_for_each_entry(child, &ex->children, siblings) {
1114 if (child->dev_type != EDGE_DEV &&
1115 child->dev_type != FANOUT_DEV)
1116 continue;
1117 if (sub_addr[0] == 0) {
1118 sas_find_sub_addr(child, sub_addr);
1119 continue;
1120 } else {
1121 u8 s2[8];
1122
1123 if (sas_find_sub_addr(child, s2) &&
1124 (SAS_ADDR(sub_addr) != SAS_ADDR(s2))) {
1125
1126 SAS_DPRINTK("ex %016llx->%016llx-?->%016llx "
1127 "diverges from subtractive "
1128 "boundary %016llx\n",
1129 SAS_ADDR(dev->sas_addr),
1130 SAS_ADDR(child->sas_addr),
1131 SAS_ADDR(s2),
1132 SAS_ADDR(sub_addr));
1133
1134 sas_ex_disable_port(child, s2);
1135 }
1136 }
1137 }
1138 return 0;
1139}
1140/**
1141 * sas_ex_discover_devices -- discover devices attached to this expander
1142 * dev: pointer to the expander domain device
1143 * single: if you want to do a single phy, else set to -1;
1144 *
1145 * Configure this expander for use with its devices and register the
1146 * devices of this expander.
1147 */
1148static int sas_ex_discover_devices(struct domain_device *dev, int single)
1149{
1150 struct expander_device *ex = &dev->ex_dev;
1151 int i = 0, end = ex->num_phys;
1152 int res = 0;
1153
1154 if (0 <= single && single < end) {
1155 i = single;
1156 end = i+1;
1157 }
1158
1159 for ( ; i < end; i++) {
1160 struct ex_phy *ex_phy = &ex->ex_phy[i];
1161
1162 if (ex_phy->phy_state == PHY_VACANT ||
1163 ex_phy->phy_state == PHY_NOT_PRESENT ||
1164 ex_phy->phy_state == PHY_DEVICE_DISCOVERED)
1165 continue;
1166
1167 switch (ex_phy->linkrate) {
James Bottomley88edf742006-09-06 17:36:13 -05001168 case SAS_PHY_DISABLED:
1169 case SAS_PHY_RESET_PROBLEM:
1170 case SAS_SATA_PORT_SELECTOR:
James Bottomley2908d772006-08-29 09:22:51 -05001171 continue;
1172 default:
1173 res = sas_ex_discover_dev(dev, i);
1174 if (res)
1175 break;
1176 continue;
1177 }
1178 }
1179
1180 if (!res)
1181 sas_check_level_subtractive_boundary(dev);
1182
1183 return res;
1184}
1185
1186static int sas_check_ex_subtractive_boundary(struct domain_device *dev)
1187{
1188 struct expander_device *ex = &dev->ex_dev;
1189 int i;
1190 u8 *sub_sas_addr = NULL;
1191
1192 if (dev->dev_type != EDGE_DEV)
1193 return 0;
1194
1195 for (i = 0; i < ex->num_phys; i++) {
1196 struct ex_phy *phy = &ex->ex_phy[i];
1197
1198 if (phy->phy_state == PHY_VACANT ||
1199 phy->phy_state == PHY_NOT_PRESENT)
1200 continue;
1201
1202 if ((phy->attached_dev_type == FANOUT_DEV ||
1203 phy->attached_dev_type == EDGE_DEV) &&
1204 phy->routing_attr == SUBTRACTIVE_ROUTING) {
1205
1206 if (!sub_sas_addr)
1207 sub_sas_addr = &phy->attached_sas_addr[0];
1208 else if (SAS_ADDR(sub_sas_addr) !=
1209 SAS_ADDR(phy->attached_sas_addr)) {
1210
1211 SAS_DPRINTK("ex %016llx phy 0x%x "
1212 "diverges(%016llx) on subtractive "
1213 "boundary(%016llx). Disabled\n",
1214 SAS_ADDR(dev->sas_addr), i,
1215 SAS_ADDR(phy->attached_sas_addr),
1216 SAS_ADDR(sub_sas_addr));
1217 sas_ex_disable_phy(dev, i);
1218 }
1219 }
1220 }
1221 return 0;
1222}
1223
1224static void sas_print_parent_topology_bug(struct domain_device *child,
1225 struct ex_phy *parent_phy,
1226 struct ex_phy *child_phy)
1227{
James Bottomley2908d772006-08-29 09:22:51 -05001228 static const char *ex_type[] = {
1229 [EDGE_DEV] = "edge",
1230 [FANOUT_DEV] = "fanout",
1231 };
1232 struct domain_device *parent = child->parent;
1233
Dan Williamsd214d812012-01-16 11:56:50 -08001234 sas_printk("%s ex %016llx phy 0x%x <--> %s ex %016llx "
1235 "phy 0x%x has %c:%c routing link!\n",
James Bottomley2908d772006-08-29 09:22:51 -05001236
1237 ex_type[parent->dev_type],
1238 SAS_ADDR(parent->sas_addr),
1239 parent_phy->phy_id,
1240
1241 ex_type[child->dev_type],
1242 SAS_ADDR(child->sas_addr),
1243 child_phy->phy_id,
1244
Dan Williamsd214d812012-01-16 11:56:50 -08001245 sas_route_char(parent, parent_phy),
1246 sas_route_char(child, child_phy));
James Bottomley2908d772006-08-29 09:22:51 -05001247}
1248
1249static int sas_check_eeds(struct domain_device *child,
1250 struct ex_phy *parent_phy,
1251 struct ex_phy *child_phy)
1252{
1253 int res = 0;
1254 struct domain_device *parent = child->parent;
1255
1256 if (SAS_ADDR(parent->port->disc.fanout_sas_addr) != 0) {
1257 res = -ENODEV;
1258 SAS_DPRINTK("edge ex %016llx phy S:0x%x <--> edge ex %016llx "
1259 "phy S:0x%x, while there is a fanout ex %016llx\n",
1260 SAS_ADDR(parent->sas_addr),
1261 parent_phy->phy_id,
1262 SAS_ADDR(child->sas_addr),
1263 child_phy->phy_id,
1264 SAS_ADDR(parent->port->disc.fanout_sas_addr));
1265 } else if (SAS_ADDR(parent->port->disc.eeds_a) == 0) {
1266 memcpy(parent->port->disc.eeds_a, parent->sas_addr,
1267 SAS_ADDR_SIZE);
1268 memcpy(parent->port->disc.eeds_b, child->sas_addr,
1269 SAS_ADDR_SIZE);
1270 } else if (((SAS_ADDR(parent->port->disc.eeds_a) ==
1271 SAS_ADDR(parent->sas_addr)) ||
1272 (SAS_ADDR(parent->port->disc.eeds_a) ==
1273 SAS_ADDR(child->sas_addr)))
1274 &&
1275 ((SAS_ADDR(parent->port->disc.eeds_b) ==
1276 SAS_ADDR(parent->sas_addr)) ||
1277 (SAS_ADDR(parent->port->disc.eeds_b) ==
1278 SAS_ADDR(child->sas_addr))))
1279 ;
1280 else {
1281 res = -ENODEV;
1282 SAS_DPRINTK("edge ex %016llx phy 0x%x <--> edge ex %016llx "
1283 "phy 0x%x link forms a third EEDS!\n",
1284 SAS_ADDR(parent->sas_addr),
1285 parent_phy->phy_id,
1286 SAS_ADDR(child->sas_addr),
1287 child_phy->phy_id);
1288 }
1289
1290 return res;
1291}
1292
1293/* Here we spill over 80 columns. It is intentional.
1294 */
1295static int sas_check_parent_topology(struct domain_device *child)
1296{
1297 struct expander_device *child_ex = &child->ex_dev;
1298 struct expander_device *parent_ex;
1299 int i;
1300 int res = 0;
1301
1302 if (!child->parent)
1303 return 0;
1304
1305 if (child->parent->dev_type != EDGE_DEV &&
1306 child->parent->dev_type != FANOUT_DEV)
1307 return 0;
1308
1309 parent_ex = &child->parent->ex_dev;
1310
1311 for (i = 0; i < parent_ex->num_phys; i++) {
1312 struct ex_phy *parent_phy = &parent_ex->ex_phy[i];
1313 struct ex_phy *child_phy;
1314
1315 if (parent_phy->phy_state == PHY_VACANT ||
1316 parent_phy->phy_state == PHY_NOT_PRESENT)
1317 continue;
1318
1319 if (SAS_ADDR(parent_phy->attached_sas_addr) != SAS_ADDR(child->sas_addr))
1320 continue;
1321
1322 child_phy = &child_ex->ex_phy[parent_phy->attached_phy_id];
1323
1324 switch (child->parent->dev_type) {
1325 case EDGE_DEV:
1326 if (child->dev_type == FANOUT_DEV) {
1327 if (parent_phy->routing_attr != SUBTRACTIVE_ROUTING ||
1328 child_phy->routing_attr != TABLE_ROUTING) {
1329 sas_print_parent_topology_bug(child, parent_phy, child_phy);
1330 res = -ENODEV;
1331 }
1332 } else if (parent_phy->routing_attr == SUBTRACTIVE_ROUTING) {
1333 if (child_phy->routing_attr == SUBTRACTIVE_ROUTING) {
1334 res = sas_check_eeds(child, parent_phy, child_phy);
1335 } else if (child_phy->routing_attr != TABLE_ROUTING) {
1336 sas_print_parent_topology_bug(child, parent_phy, child_phy);
1337 res = -ENODEV;
1338 }
Luben Tuikovffaac8f2011-09-22 09:41:36 -07001339 } else if (parent_phy->routing_attr == TABLE_ROUTING) {
1340 if (child_phy->routing_attr == SUBTRACTIVE_ROUTING ||
1341 (child_phy->routing_attr == TABLE_ROUTING &&
1342 child_ex->t2t_supp && parent_ex->t2t_supp)) {
1343 /* All good */;
1344 } else {
1345 sas_print_parent_topology_bug(child, parent_phy, child_phy);
1346 res = -ENODEV;
1347 }
James Bottomley2908d772006-08-29 09:22:51 -05001348 }
1349 break;
1350 case FANOUT_DEV:
1351 if (parent_phy->routing_attr != TABLE_ROUTING ||
1352 child_phy->routing_attr != SUBTRACTIVE_ROUTING) {
1353 sas_print_parent_topology_bug(child, parent_phy, child_phy);
1354 res = -ENODEV;
1355 }
1356 break;
1357 default:
1358 break;
1359 }
1360 }
1361
1362 return res;
1363}
1364
1365#define RRI_REQ_SIZE 16
1366#define RRI_RESP_SIZE 44
1367
1368static int sas_configure_present(struct domain_device *dev, int phy_id,
1369 u8 *sas_addr, int *index, int *present)
1370{
1371 int i, res = 0;
1372 struct expander_device *ex = &dev->ex_dev;
1373 struct ex_phy *phy = &ex->ex_phy[phy_id];
1374 u8 *rri_req;
1375 u8 *rri_resp;
1376
1377 *present = 0;
1378 *index = 0;
1379
1380 rri_req = alloc_smp_req(RRI_REQ_SIZE);
1381 if (!rri_req)
1382 return -ENOMEM;
1383
1384 rri_resp = alloc_smp_resp(RRI_RESP_SIZE);
1385 if (!rri_resp) {
1386 kfree(rri_req);
1387 return -ENOMEM;
1388 }
1389
1390 rri_req[1] = SMP_REPORT_ROUTE_INFO;
1391 rri_req[9] = phy_id;
1392
1393 for (i = 0; i < ex->max_route_indexes ; i++) {
1394 *(__be16 *)(rri_req+6) = cpu_to_be16(i);
1395 res = smp_execute_task(dev, rri_req, RRI_REQ_SIZE, rri_resp,
1396 RRI_RESP_SIZE);
1397 if (res)
1398 goto out;
1399 res = rri_resp[2];
1400 if (res == SMP_RESP_NO_INDEX) {
1401 SAS_DPRINTK("overflow of indexes: dev %016llx "
1402 "phy 0x%x index 0x%x\n",
1403 SAS_ADDR(dev->sas_addr), phy_id, i);
1404 goto out;
1405 } else if (res != SMP_RESP_FUNC_ACC) {
1406 SAS_DPRINTK("%s: dev %016llx phy 0x%x index 0x%x "
Harvey Harrisoncadbd4a2008-07-03 23:47:27 -07001407 "result 0x%x\n", __func__,
James Bottomley2908d772006-08-29 09:22:51 -05001408 SAS_ADDR(dev->sas_addr), phy_id, i, res);
1409 goto out;
1410 }
1411 if (SAS_ADDR(sas_addr) != 0) {
1412 if (SAS_ADDR(rri_resp+16) == SAS_ADDR(sas_addr)) {
1413 *index = i;
1414 if ((rri_resp[12] & 0x80) == 0x80)
1415 *present = 0;
1416 else
1417 *present = 1;
1418 goto out;
1419 } else if (SAS_ADDR(rri_resp+16) == 0) {
1420 *index = i;
1421 *present = 0;
1422 goto out;
1423 }
1424 } else if (SAS_ADDR(rri_resp+16) == 0 &&
1425 phy->last_da_index < i) {
1426 phy->last_da_index = i;
1427 *index = i;
1428 *present = 0;
1429 goto out;
1430 }
1431 }
1432 res = -1;
1433out:
1434 kfree(rri_req);
1435 kfree(rri_resp);
1436 return res;
1437}
1438
1439#define CRI_REQ_SIZE 44
1440#define CRI_RESP_SIZE 8
1441
1442static int sas_configure_set(struct domain_device *dev, int phy_id,
1443 u8 *sas_addr, int index, int include)
1444{
1445 int res;
1446 u8 *cri_req;
1447 u8 *cri_resp;
1448
1449 cri_req = alloc_smp_req(CRI_REQ_SIZE);
1450 if (!cri_req)
1451 return -ENOMEM;
1452
1453 cri_resp = alloc_smp_resp(CRI_RESP_SIZE);
1454 if (!cri_resp) {
1455 kfree(cri_req);
1456 return -ENOMEM;
1457 }
1458
1459 cri_req[1] = SMP_CONF_ROUTE_INFO;
1460 *(__be16 *)(cri_req+6) = cpu_to_be16(index);
1461 cri_req[9] = phy_id;
1462 if (SAS_ADDR(sas_addr) == 0 || !include)
1463 cri_req[12] |= 0x80;
1464 memcpy(cri_req+16, sas_addr, SAS_ADDR_SIZE);
1465
1466 res = smp_execute_task(dev, cri_req, CRI_REQ_SIZE, cri_resp,
1467 CRI_RESP_SIZE);
1468 if (res)
1469 goto out;
1470 res = cri_resp[2];
1471 if (res == SMP_RESP_NO_INDEX) {
1472 SAS_DPRINTK("overflow of indexes: dev %016llx phy 0x%x "
1473 "index 0x%x\n",
1474 SAS_ADDR(dev->sas_addr), phy_id, index);
1475 }
1476out:
1477 kfree(cri_req);
1478 kfree(cri_resp);
1479 return res;
1480}
1481
1482static int sas_configure_phy(struct domain_device *dev, int phy_id,
1483 u8 *sas_addr, int include)
1484{
1485 int index;
1486 int present;
1487 int res;
1488
1489 res = sas_configure_present(dev, phy_id, sas_addr, &index, &present);
1490 if (res)
1491 return res;
1492 if (include ^ present)
1493 return sas_configure_set(dev, phy_id, sas_addr, index,include);
1494
1495 return res;
1496}
1497
1498/**
1499 * sas_configure_parent -- configure routing table of parent
1500 * parent: parent expander
1501 * child: child expander
1502 * sas_addr: SAS port identifier of device directly attached to child
1503 */
1504static int sas_configure_parent(struct domain_device *parent,
1505 struct domain_device *child,
1506 u8 *sas_addr, int include)
1507{
1508 struct expander_device *ex_parent = &parent->ex_dev;
1509 int res = 0;
1510 int i;
1511
1512 if (parent->parent) {
1513 res = sas_configure_parent(parent->parent, parent, sas_addr,
1514 include);
1515 if (res)
1516 return res;
1517 }
1518
1519 if (ex_parent->conf_route_table == 0) {
1520 SAS_DPRINTK("ex %016llx has self-configuring routing table\n",
1521 SAS_ADDR(parent->sas_addr));
1522 return 0;
1523 }
1524
1525 for (i = 0; i < ex_parent->num_phys; i++) {
1526 struct ex_phy *phy = &ex_parent->ex_phy[i];
1527
1528 if ((phy->routing_attr == TABLE_ROUTING) &&
1529 (SAS_ADDR(phy->attached_sas_addr) ==
1530 SAS_ADDR(child->sas_addr))) {
1531 res = sas_configure_phy(parent, i, sas_addr, include);
1532 if (res)
1533 return res;
1534 }
1535 }
1536
1537 return res;
1538}
1539
1540/**
1541 * sas_configure_routing -- configure routing
1542 * dev: expander device
1543 * sas_addr: port identifier of device directly attached to the expander device
1544 */
1545static int sas_configure_routing(struct domain_device *dev, u8 *sas_addr)
1546{
1547 if (dev->parent)
1548 return sas_configure_parent(dev->parent, dev, sas_addr, 1);
1549 return 0;
1550}
1551
1552static int sas_disable_routing(struct domain_device *dev, u8 *sas_addr)
1553{
1554 if (dev->parent)
1555 return sas_configure_parent(dev->parent, dev, sas_addr, 0);
1556 return 0;
1557}
1558
James Bottomley2908d772006-08-29 09:22:51 -05001559/**
1560 * sas_discover_expander -- expander discovery
1561 * @ex: pointer to expander domain device
1562 *
1563 * See comment in sas_discover_sata().
1564 */
1565static int sas_discover_expander(struct domain_device *dev)
1566{
1567 int res;
1568
1569 res = sas_notify_lldd_dev_found(dev);
1570 if (res)
1571 return res;
1572
1573 res = sas_ex_general(dev);
1574 if (res)
1575 goto out_err;
1576 res = sas_ex_manuf_info(dev);
1577 if (res)
1578 goto out_err;
1579
1580 res = sas_expander_discover(dev);
1581 if (res) {
1582 SAS_DPRINTK("expander %016llx discovery failed(0x%x)\n",
1583 SAS_ADDR(dev->sas_addr), res);
1584 goto out_err;
1585 }
1586
1587 sas_check_ex_subtractive_boundary(dev);
1588 res = sas_check_parent_topology(dev);
1589 if (res)
1590 goto out_err;
1591 return 0;
1592out_err:
1593 sas_notify_lldd_dev_gone(dev);
1594 return res;
1595}
1596
1597static int sas_ex_level_discovery(struct asd_sas_port *port, const int level)
1598{
1599 int res = 0;
1600 struct domain_device *dev;
1601
1602 list_for_each_entry(dev, &port->dev_list, dev_list_node) {
1603 if (dev->dev_type == EDGE_DEV ||
1604 dev->dev_type == FANOUT_DEV) {
1605 struct sas_expander_device *ex =
1606 rphy_to_expander_device(dev->rphy);
1607
1608 if (level == ex->level)
1609 res = sas_ex_discover_devices(dev, -1);
1610 else if (level > 0)
1611 res = sas_ex_discover_devices(port->port_dev, -1);
1612
1613 }
1614 }
1615
1616 return res;
1617}
1618
1619static int sas_ex_bfs_disc(struct asd_sas_port *port)
1620{
1621 int res;
1622 int level;
1623
1624 do {
1625 level = port->disc.max_level;
1626 res = sas_ex_level_discovery(port, level);
1627 mb();
1628 } while (level < port->disc.max_level);
1629
1630 return res;
1631}
1632
1633int sas_discover_root_expander(struct domain_device *dev)
1634{
1635 int res;
1636 struct sas_expander_device *ex = rphy_to_expander_device(dev->rphy);
1637
Darrick J. Wongbf451202007-01-11 14:14:52 -08001638 res = sas_rphy_add(dev->rphy);
1639 if (res)
1640 goto out_err;
James Bottomley2908d772006-08-29 09:22:51 -05001641
1642 ex->level = dev->port->disc.max_level; /* 0 */
1643 res = sas_discover_expander(dev);
Darrick J. Wongbf451202007-01-11 14:14:52 -08001644 if (res)
1645 goto out_err2;
James Bottomley2908d772006-08-29 09:22:51 -05001646
Darrick J. Wongbf451202007-01-11 14:14:52 -08001647 sas_ex_bfs_disc(dev->port);
1648
1649 return res;
1650
1651out_err2:
Darrick J. Wong6f63caa2007-01-26 14:08:43 -08001652 sas_rphy_remove(dev->rphy);
Darrick J. Wongbf451202007-01-11 14:14:52 -08001653out_err:
James Bottomley2908d772006-08-29 09:22:51 -05001654 return res;
1655}
1656
1657/* ---------- Domain revalidation ---------- */
1658
1659static int sas_get_phy_discover(struct domain_device *dev,
1660 int phy_id, struct smp_resp *disc_resp)
1661{
1662 int res;
1663 u8 *disc_req;
1664
1665 disc_req = alloc_smp_req(DISCOVER_REQ_SIZE);
1666 if (!disc_req)
1667 return -ENOMEM;
1668
1669 disc_req[1] = SMP_DISCOVER;
1670 disc_req[9] = phy_id;
1671
1672 res = smp_execute_task(dev, disc_req, DISCOVER_REQ_SIZE,
1673 disc_resp, DISCOVER_RESP_SIZE);
1674 if (res)
1675 goto out;
1676 else if (disc_resp->result != SMP_RESP_FUNC_ACC) {
1677 res = disc_resp->result;
1678 goto out;
1679 }
1680out:
1681 kfree(disc_req);
1682 return res;
1683}
1684
1685static int sas_get_phy_change_count(struct domain_device *dev,
1686 int phy_id, int *pcc)
1687{
1688 int res;
1689 struct smp_resp *disc_resp;
1690
1691 disc_resp = alloc_smp_resp(DISCOVER_RESP_SIZE);
1692 if (!disc_resp)
1693 return -ENOMEM;
1694
1695 res = sas_get_phy_discover(dev, phy_id, disc_resp);
1696 if (!res)
1697 *pcc = disc_resp->disc.change_count;
1698
1699 kfree(disc_resp);
1700 return res;
1701}
1702
Dan Williams354cf822012-01-12 17:57:35 -08001703static int sas_get_phy_attached_dev(struct domain_device *dev, int phy_id,
1704 u8 *sas_addr, enum sas_dev_type *type)
James Bottomley2908d772006-08-29 09:22:51 -05001705{
1706 int res;
1707 struct smp_resp *disc_resp;
1708 struct discover_resp *dr;
1709
1710 disc_resp = alloc_smp_resp(DISCOVER_RESP_SIZE);
1711 if (!disc_resp)
1712 return -ENOMEM;
1713 dr = &disc_resp->disc;
1714
1715 res = sas_get_phy_discover(dev, phy_id, disc_resp);
Dan Williams354cf822012-01-12 17:57:35 -08001716 if (res == 0) {
1717 memcpy(sas_addr, disc_resp->disc.attached_sas_addr, 8);
1718 *type = to_dev_type(dr);
1719 if (*type == 0)
1720 memset(sas_addr, 0, 8);
James Bottomley2908d772006-08-29 09:22:51 -05001721 }
1722 kfree(disc_resp);
1723 return res;
1724}
1725
1726static int sas_find_bcast_phy(struct domain_device *dev, int *phy_id,
Tom Peng19252de2009-07-17 16:02:04 +08001727 int from_phy, bool update)
James Bottomley2908d772006-08-29 09:22:51 -05001728{
1729 struct expander_device *ex = &dev->ex_dev;
1730 int res = 0;
1731 int i;
1732
1733 for (i = from_phy; i < ex->num_phys; i++) {
1734 int phy_change_count = 0;
1735
1736 res = sas_get_phy_change_count(dev, i, &phy_change_count);
Thomas Jackson16994902012-02-17 18:33:10 -08001737 switch (res) {
1738 case SMP_RESP_PHY_VACANT:
1739 case SMP_RESP_NO_PHY:
1740 continue;
1741 case SMP_RESP_FUNC_ACC:
1742 break;
1743 default:
1744 return res;
1745 }
1746
1747 if (phy_change_count != ex->ex_phy[i].phy_change_count) {
Tom Peng19252de2009-07-17 16:02:04 +08001748 if (update)
1749 ex->ex_phy[i].phy_change_count =
1750 phy_change_count;
James Bottomley2908d772006-08-29 09:22:51 -05001751 *phy_id = i;
1752 return 0;
1753 }
1754 }
Thomas Jackson16994902012-02-17 18:33:10 -08001755 return 0;
James Bottomley2908d772006-08-29 09:22:51 -05001756}
1757
1758static int sas_get_ex_change_count(struct domain_device *dev, int *ecc)
1759{
1760 int res;
1761 u8 *rg_req;
1762 struct smp_resp *rg_resp;
1763
1764 rg_req = alloc_smp_req(RG_REQ_SIZE);
1765 if (!rg_req)
1766 return -ENOMEM;
1767
1768 rg_resp = alloc_smp_resp(RG_RESP_SIZE);
1769 if (!rg_resp) {
1770 kfree(rg_req);
1771 return -ENOMEM;
1772 }
1773
1774 rg_req[1] = SMP_REPORT_GENERAL;
1775
1776 res = smp_execute_task(dev, rg_req, RG_REQ_SIZE, rg_resp,
1777 RG_RESP_SIZE);
1778 if (res)
1779 goto out;
1780 if (rg_resp->result != SMP_RESP_FUNC_ACC) {
1781 res = rg_resp->result;
1782 goto out;
1783 }
1784
1785 *ecc = be16_to_cpu(rg_resp->rg.change_count);
1786out:
1787 kfree(rg_resp);
1788 kfree(rg_req);
1789 return res;
1790}
Tom Peng19252de2009-07-17 16:02:04 +08001791/**
1792 * sas_find_bcast_dev - find the device issue BROADCAST(CHANGE).
1793 * @dev:domain device to be detect.
1794 * @src_dev: the device which originated BROADCAST(CHANGE).
1795 *
1796 * Add self-configuration expander suport. Suppose two expander cascading,
1797 * when the first level expander is self-configuring, hotplug the disks in
1798 * second level expander, BROADCAST(CHANGE) will not only be originated
1799 * in the second level expander, but also be originated in the first level
1800 * expander (see SAS protocol SAS 2r-14, 7.11 for detail), it is to say,
1801 * expander changed count in two level expanders will all increment at least
1802 * once, but the phy which chang count has changed is the source device which
1803 * we concerned.
1804 */
James Bottomley2908d772006-08-29 09:22:51 -05001805
1806static int sas_find_bcast_dev(struct domain_device *dev,
1807 struct domain_device **src_dev)
1808{
1809 struct expander_device *ex = &dev->ex_dev;
1810 int ex_change_count = -1;
Tom Peng19252de2009-07-17 16:02:04 +08001811 int phy_id = -1;
James Bottomley2908d772006-08-29 09:22:51 -05001812 int res;
Tom Peng19252de2009-07-17 16:02:04 +08001813 struct domain_device *ch;
James Bottomley2908d772006-08-29 09:22:51 -05001814
1815 res = sas_get_ex_change_count(dev, &ex_change_count);
1816 if (res)
1817 goto out;
Tom Peng19252de2009-07-17 16:02:04 +08001818 if (ex_change_count != -1 && ex_change_count != ex->ex_change_count) {
1819 /* Just detect if this expander phys phy change count changed,
1820 * in order to determine if this expander originate BROADCAST,
1821 * and do not update phy change count field in our structure.
1822 */
1823 res = sas_find_bcast_phy(dev, &phy_id, 0, false);
1824 if (phy_id != -1) {
1825 *src_dev = dev;
1826 ex->ex_change_count = ex_change_count;
1827 SAS_DPRINTK("Expander phy change count has changed\n");
1828 return res;
1829 } else
1830 SAS_DPRINTK("Expander phys DID NOT change\n");
1831 }
1832 list_for_each_entry(ch, &ex->children, siblings) {
1833 if (ch->dev_type == EDGE_DEV || ch->dev_type == FANOUT_DEV) {
1834 res = sas_find_bcast_dev(ch, src_dev);
Mark Salyzyn24926da2011-09-01 06:11:17 -07001835 if (*src_dev)
Tom Peng19252de2009-07-17 16:02:04 +08001836 return res;
James Bottomley2908d772006-08-29 09:22:51 -05001837 }
1838 }
1839out:
1840 return res;
1841}
1842
Dan Williams1a34c062011-09-21 22:05:34 -07001843static void sas_unregister_ex_tree(struct asd_sas_port *port, struct domain_device *dev)
James Bottomley2908d772006-08-29 09:22:51 -05001844{
1845 struct expander_device *ex = &dev->ex_dev;
1846 struct domain_device *child, *n;
1847
1848 list_for_each_entry_safe(child, n, &ex->children, siblings) {
Dan Williamse1399422012-01-07 08:52:39 +00001849 set_bit(SAS_DEV_GONE, &child->state);
James Bottomley2908d772006-08-29 09:22:51 -05001850 if (child->dev_type == EDGE_DEV ||
1851 child->dev_type == FANOUT_DEV)
Dan Williams1a34c062011-09-21 22:05:34 -07001852 sas_unregister_ex_tree(port, child);
James Bottomley2908d772006-08-29 09:22:51 -05001853 else
Dan Williams1a34c062011-09-21 22:05:34 -07001854 sas_unregister_dev(port, child);
James Bottomley2908d772006-08-29 09:22:51 -05001855 }
Dan Williams1a34c062011-09-21 22:05:34 -07001856 sas_unregister_dev(port, dev);
James Bottomley2908d772006-08-29 09:22:51 -05001857}
1858
1859static void sas_unregister_devs_sas_addr(struct domain_device *parent,
Tom Peng19252de2009-07-17 16:02:04 +08001860 int phy_id, bool last)
James Bottomley2908d772006-08-29 09:22:51 -05001861{
1862 struct expander_device *ex_dev = &parent->ex_dev;
1863 struct ex_phy *phy = &ex_dev->ex_phy[phy_id];
Dan Williamsf41a0c42011-12-21 21:33:17 -08001864 struct domain_device *child, *n, *found = NULL;
Tom Peng19252de2009-07-17 16:02:04 +08001865 if (last) {
1866 list_for_each_entry_safe(child, n,
1867 &ex_dev->children, siblings) {
1868 if (SAS_ADDR(child->sas_addr) ==
1869 SAS_ADDR(phy->attached_sas_addr)) {
Dan Williamse1399422012-01-07 08:52:39 +00001870 set_bit(SAS_DEV_GONE, &child->state);
Tom Peng19252de2009-07-17 16:02:04 +08001871 if (child->dev_type == EDGE_DEV ||
1872 child->dev_type == FANOUT_DEV)
Dan Williams1a34c062011-09-21 22:05:34 -07001873 sas_unregister_ex_tree(parent->port, child);
Tom Peng19252de2009-07-17 16:02:04 +08001874 else
Dan Williams1a34c062011-09-21 22:05:34 -07001875 sas_unregister_dev(parent->port, child);
Dan Williamsf41a0c42011-12-21 21:33:17 -08001876 found = child;
Tom Peng19252de2009-07-17 16:02:04 +08001877 break;
1878 }
James Bottomley2908d772006-08-29 09:22:51 -05001879 }
Tom Peng19252de2009-07-17 16:02:04 +08001880 sas_disable_routing(parent, phy->attached_sas_addr);
James Bottomley2908d772006-08-29 09:22:51 -05001881 }
James Bottomley2908d772006-08-29 09:22:51 -05001882 memset(phy->attached_sas_addr, 0, SAS_ADDR_SIZE);
Mark Salyzyna73914c2011-09-22 08:32:23 -07001883 if (phy->port) {
1884 sas_port_delete_phy(phy->port, phy->phy);
Dan Williamsf41a0c42011-12-21 21:33:17 -08001885 sas_device_set_phy(found, phy->port);
Mark Salyzyna73914c2011-09-22 08:32:23 -07001886 if (phy->port->num_phys == 0)
1887 sas_port_delete(phy->port);
1888 phy->port = NULL;
1889 }
James Bottomley2908d772006-08-29 09:22:51 -05001890}
1891
1892static int sas_discover_bfs_by_root_level(struct domain_device *root,
1893 const int level)
1894{
1895 struct expander_device *ex_root = &root->ex_dev;
1896 struct domain_device *child;
1897 int res = 0;
1898
1899 list_for_each_entry(child, &ex_root->children, siblings) {
1900 if (child->dev_type == EDGE_DEV ||
1901 child->dev_type == FANOUT_DEV) {
1902 struct sas_expander_device *ex =
1903 rphy_to_expander_device(child->rphy);
1904
1905 if (level > ex->level)
1906 res = sas_discover_bfs_by_root_level(child,
1907 level);
1908 else if (level == ex->level)
1909 res = sas_ex_discover_devices(child, -1);
1910 }
1911 }
1912 return res;
1913}
1914
1915static int sas_discover_bfs_by_root(struct domain_device *dev)
1916{
1917 int res;
1918 struct sas_expander_device *ex = rphy_to_expander_device(dev->rphy);
1919 int level = ex->level+1;
1920
1921 res = sas_ex_discover_devices(dev, -1);
1922 if (res)
1923 goto out;
1924 do {
1925 res = sas_discover_bfs_by_root_level(dev, level);
1926 mb();
1927 level += 1;
1928 } while (level <= dev->port->disc.max_level);
1929out:
1930 return res;
1931}
1932
1933static int sas_discover_new(struct domain_device *dev, int phy_id)
1934{
1935 struct ex_phy *ex_phy = &dev->ex_dev.ex_phy[phy_id];
1936 struct domain_device *child;
Tom Peng19252de2009-07-17 16:02:04 +08001937 bool found = false;
1938 int res, i;
James Bottomley2908d772006-08-29 09:22:51 -05001939
1940 SAS_DPRINTK("ex %016llx phy%d new device attached\n",
1941 SAS_ADDR(dev->sas_addr), phy_id);
1942 res = sas_ex_phy_discover(dev, phy_id);
1943 if (res)
1944 goto out;
Tom Peng19252de2009-07-17 16:02:04 +08001945 /* to support the wide port inserted */
1946 for (i = 0; i < dev->ex_dev.num_phys; i++) {
1947 struct ex_phy *ex_phy_temp = &dev->ex_dev.ex_phy[i];
1948 if (i == phy_id)
1949 continue;
1950 if (SAS_ADDR(ex_phy_temp->attached_sas_addr) ==
1951 SAS_ADDR(ex_phy->attached_sas_addr)) {
1952 found = true;
1953 break;
1954 }
1955 }
1956 if (found) {
1957 sas_ex_join_wide_port(dev, phy_id);
1958 return 0;
1959 }
James Bottomley2908d772006-08-29 09:22:51 -05001960 res = sas_ex_discover_devices(dev, phy_id);
Tom Peng19252de2009-07-17 16:02:04 +08001961 if (!res)
James Bottomley2908d772006-08-29 09:22:51 -05001962 goto out;
1963 list_for_each_entry(child, &dev->ex_dev.children, siblings) {
1964 if (SAS_ADDR(child->sas_addr) ==
1965 SAS_ADDR(ex_phy->attached_sas_addr)) {
1966 if (child->dev_type == EDGE_DEV ||
1967 child->dev_type == FANOUT_DEV)
1968 res = sas_discover_bfs_by_root(child);
1969 break;
1970 }
1971 }
1972out:
1973 return res;
1974}
1975
Dan Williams354cf822012-01-12 17:57:35 -08001976static bool dev_type_flutter(enum sas_dev_type new, enum sas_dev_type old)
1977{
1978 if (old == new)
1979 return true;
1980
1981 /* treat device directed resets as flutter, if we went
1982 * SAS_END_DEV to SATA_PENDING the link needs recovery
1983 */
1984 if ((old == SATA_PENDING && new == SAS_END_DEV) ||
1985 (old == SAS_END_DEV && new == SATA_PENDING))
1986 return true;
1987
1988 return false;
1989}
1990
Tom Peng19252de2009-07-17 16:02:04 +08001991static int sas_rediscover_dev(struct domain_device *dev, int phy_id, bool last)
James Bottomley2908d772006-08-29 09:22:51 -05001992{
1993 struct expander_device *ex = &dev->ex_dev;
1994 struct ex_phy *phy = &ex->ex_phy[phy_id];
Dan Williams354cf822012-01-12 17:57:35 -08001995 enum sas_dev_type type = NO_DEVICE;
1996 u8 sas_addr[8];
James Bottomley2908d772006-08-29 09:22:51 -05001997 int res;
1998
Dan Williams354cf822012-01-12 17:57:35 -08001999 res = sas_get_phy_attached_dev(dev, phy_id, sas_addr, &type);
James Bottomley2908d772006-08-29 09:22:51 -05002000 switch (res) {
2001 case SMP_RESP_NO_PHY:
2002 phy->phy_state = PHY_NOT_PRESENT;
Tom Peng19252de2009-07-17 16:02:04 +08002003 sas_unregister_devs_sas_addr(dev, phy_id, last);
Dan Williams354cf822012-01-12 17:57:35 -08002004 return res;
James Bottomley2908d772006-08-29 09:22:51 -05002005 case SMP_RESP_PHY_VACANT:
2006 phy->phy_state = PHY_VACANT;
Tom Peng19252de2009-07-17 16:02:04 +08002007 sas_unregister_devs_sas_addr(dev, phy_id, last);
Dan Williams354cf822012-01-12 17:57:35 -08002008 return res;
James Bottomley2908d772006-08-29 09:22:51 -05002009 case SMP_RESP_FUNC_ACC:
2010 break;
2011 }
2012
Dan Williams354cf822012-01-12 17:57:35 -08002013 if (SAS_ADDR(sas_addr) == 0) {
James Bottomley2908d772006-08-29 09:22:51 -05002014 phy->phy_state = PHY_EMPTY;
Tom Peng19252de2009-07-17 16:02:04 +08002015 sas_unregister_devs_sas_addr(dev, phy_id, last);
Dan Williams354cf822012-01-12 17:57:35 -08002016 return res;
2017 } else if (SAS_ADDR(sas_addr) == SAS_ADDR(phy->attached_sas_addr) &&
2018 dev_type_flutter(type, phy->attached_dev_type)) {
2019 struct domain_device *ata_dev = sas_ex_to_ata(dev, phy_id);
2020 char *action = "";
2021
James Bottomleya01e70e2006-09-06 19:28:07 -05002022 sas_ex_phy_discover(dev, phy_id);
Dan Williams354cf822012-01-12 17:57:35 -08002023
2024 if (ata_dev && phy->attached_dev_type == SATA_PENDING)
2025 action = ", needs recovery";
2026 SAS_DPRINTK("ex %016llx phy 0x%x broadcast flutter%s\n",
2027 SAS_ADDR(dev->sas_addr), phy_id, action);
2028 return res;
2029 }
2030
Dan Williamsc666aae2012-01-19 18:43:08 -08002031 /* delete the old link */
2032 if (SAS_ADDR(phy->attached_sas_addr) &&
2033 SAS_ADDR(sas_addr) != SAS_ADDR(phy->attached_sas_addr)) {
2034 SAS_DPRINTK("ex %016llx phy 0x%x replace %016llx\n",
2035 SAS_ADDR(dev->sas_addr), phy_id,
2036 SAS_ADDR(phy->attached_sas_addr));
2037 sas_unregister_devs_sas_addr(dev, phy_id, last);
2038 }
2039
Dan Williams354cf822012-01-12 17:57:35 -08002040 return sas_discover_new(dev, phy_id);
James Bottomley2908d772006-08-29 09:22:51 -05002041}
2042
Tom Peng19252de2009-07-17 16:02:04 +08002043/**
2044 * sas_rediscover - revalidate the domain.
2045 * @dev:domain device to be detect.
2046 * @phy_id: the phy id will be detected.
2047 *
2048 * NOTE: this process _must_ quit (return) as soon as any connection
2049 * errors are encountered. Connection recovery is done elsewhere.
2050 * Discover process only interrogates devices in order to discover the
2051 * domain.For plugging out, we un-register the device only when it is
2052 * the last phy in the port, for other phys in this port, we just delete it
2053 * from the port.For inserting, we do discovery when it is the
2054 * first phy,for other phys in this port, we add it to the port to
2055 * forming the wide-port.
2056 */
James Bottomley2908d772006-08-29 09:22:51 -05002057static int sas_rediscover(struct domain_device *dev, const int phy_id)
2058{
2059 struct expander_device *ex = &dev->ex_dev;
2060 struct ex_phy *changed_phy = &ex->ex_phy[phy_id];
2061 int res = 0;
2062 int i;
Tom Peng19252de2009-07-17 16:02:04 +08002063 bool last = true; /* is this the last phy of the port */
James Bottomley2908d772006-08-29 09:22:51 -05002064
2065 SAS_DPRINTK("ex %016llx phy%d originated BROADCAST(CHANGE)\n",
2066 SAS_ADDR(dev->sas_addr), phy_id);
2067
2068 if (SAS_ADDR(changed_phy->attached_sas_addr) != 0) {
2069 for (i = 0; i < ex->num_phys; i++) {
2070 struct ex_phy *phy = &ex->ex_phy[i];
2071
2072 if (i == phy_id)
2073 continue;
2074 if (SAS_ADDR(phy->attached_sas_addr) ==
2075 SAS_ADDR(changed_phy->attached_sas_addr)) {
2076 SAS_DPRINTK("phy%d part of wide port with "
2077 "phy%d\n", phy_id, i);
Tom Peng19252de2009-07-17 16:02:04 +08002078 last = false;
2079 break;
James Bottomley2908d772006-08-29 09:22:51 -05002080 }
2081 }
Tom Peng19252de2009-07-17 16:02:04 +08002082 res = sas_rediscover_dev(dev, phy_id, last);
James Bottomley2908d772006-08-29 09:22:51 -05002083 } else
2084 res = sas_discover_new(dev, phy_id);
James Bottomley2908d772006-08-29 09:22:51 -05002085 return res;
2086}
2087
2088/**
2089 * sas_revalidate_domain -- revalidate the domain
2090 * @port: port to the domain of interest
2091 *
2092 * NOTE: this process _must_ quit (return) as soon as any connection
2093 * errors are encountered. Connection recovery is done elsewhere.
2094 * Discover process only interrogates devices in order to discover the
2095 * domain.
2096 */
2097int sas_ex_revalidate_domain(struct domain_device *port_dev)
2098{
2099 int res;
2100 struct domain_device *dev = NULL;
2101
2102 res = sas_find_bcast_dev(port_dev, &dev);
2103 if (res)
2104 goto out;
2105 if (dev) {
2106 struct expander_device *ex = &dev->ex_dev;
2107 int i = 0, phy_id;
2108
2109 do {
2110 phy_id = -1;
Tom Peng19252de2009-07-17 16:02:04 +08002111 res = sas_find_bcast_phy(dev, &phy_id, i, true);
James Bottomley2908d772006-08-29 09:22:51 -05002112 if (phy_id == -1)
2113 break;
2114 res = sas_rediscover(dev, phy_id);
2115 i = phy_id + 1;
2116 } while (i < ex->num_phys);
2117 }
2118out:
2119 return res;
2120}
2121
FUJITA Tomonoriba1fc172007-07-09 12:52:08 +09002122int sas_smp_handler(struct Scsi_Host *shost, struct sas_rphy *rphy,
2123 struct request *req)
2124{
2125 struct domain_device *dev;
Darrick J. Wong2cd614c2007-07-24 09:33:49 -07002126 int ret, type;
FUJITA Tomonoriba1fc172007-07-09 12:52:08 +09002127 struct request *rsp = req->next_rq;
2128
2129 if (!rsp) {
2130 printk("%s: space for a smp response is missing\n",
Harvey Harrisoncadbd4a2008-07-03 23:47:27 -07002131 __func__);
FUJITA Tomonoriba1fc172007-07-09 12:52:08 +09002132 return -EINVAL;
2133 }
2134
Darrick J. Wong2cd614c2007-07-24 09:33:49 -07002135 /* no rphy means no smp target support (ie aic94xx host) */
James Bottomleyb98e66f2007-12-28 16:35:17 -06002136 if (!rphy)
2137 return sas_smp_host_handler(shost, req, rsp);
2138
Darrick J. Wong2cd614c2007-07-24 09:33:49 -07002139 type = rphy->identify.device_type;
FUJITA Tomonoriba1fc172007-07-09 12:52:08 +09002140
2141 if (type != SAS_EDGE_EXPANDER_DEVICE &&
2142 type != SAS_FANOUT_EXPANDER_DEVICE) {
2143 printk("%s: can we send a smp request to a device?\n",
Harvey Harrisoncadbd4a2008-07-03 23:47:27 -07002144 __func__);
FUJITA Tomonoriba1fc172007-07-09 12:52:08 +09002145 return -EINVAL;
2146 }
2147
2148 dev = sas_find_dev_by_rphy(rphy);
2149 if (!dev) {
Harvey Harrisoncadbd4a2008-07-03 23:47:27 -07002150 printk("%s: fail to find a domain_device?\n", __func__);
FUJITA Tomonoriba1fc172007-07-09 12:52:08 +09002151 return -EINVAL;
2152 }
2153
2154 /* do we need to support multiple segments? */
2155 if (req->bio->bi_vcnt > 1 || rsp->bio->bi_vcnt > 1) {
2156 printk("%s: multiple segments req %u %u, rsp %u %u\n",
Tejun Heob0790412009-05-07 22:24:42 +09002157 __func__, req->bio->bi_vcnt, blk_rq_bytes(req),
2158 rsp->bio->bi_vcnt, blk_rq_bytes(rsp));
FUJITA Tomonoriba1fc172007-07-09 12:52:08 +09002159 return -EINVAL;
2160 }
2161
Tejun Heob0790412009-05-07 22:24:42 +09002162 ret = smp_execute_task(dev, bio_data(req->bio), blk_rq_bytes(req),
2163 bio_data(rsp->bio), blk_rq_bytes(rsp));
James Bottomley2d4b63e2007-12-29 11:49:53 -06002164 if (ret > 0) {
2165 /* positive number is the untransferred residual */
Tejun Heoc3a4d782009-05-07 22:24:37 +09002166 rsp->resid_len = ret;
Tejun Heo5f49f632009-05-19 18:33:05 +09002167 req->resid_len = 0;
James Bottomley2d4b63e2007-12-29 11:49:53 -06002168 ret = 0;
Tejun Heo5f49f632009-05-19 18:33:05 +09002169 } else if (ret == 0) {
2170 rsp->resid_len = 0;
2171 req->resid_len = 0;
James Bottomley2d4b63e2007-12-29 11:49:53 -06002172 }
FUJITA Tomonoriba1fc172007-07-09 12:52:08 +09002173
2174 return ret;
2175}