blob: 75247a176c6bbda6fa5290d1d308a1373f15a09c [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
James Bottomley2908d772006-08-29 09:22:51 -0500793 sas_init_dev(child);
Dan Williamsb2024452012-03-21 21:09:07 -0700794 res = sas_ata_init(child);
795 if (res)
796 goto out_free;
797 rphy = sas_end_device_alloc(phy->port);
798 if (!rphy)
799 goto out_free;
James Bottomley1acce192006-08-22 12:39:19 -0500800
801 child->rphy = rphy;
Dan Williams94876692012-03-20 10:53:24 -0700802 get_device(&rphy->dev);
James Bottomley1acce192006-08-22 12:39:19 -0500803
Dan Williams87c83312011-11-17 17:59:51 -0800804 list_add_tail(&child->disco_list_node, &parent->port->disco_list);
James Bottomley1acce192006-08-22 12:39:19 -0500805
James Bottomley2908d772006-08-29 09:22:51 -0500806 res = sas_discover_sata(child);
807 if (res) {
808 SAS_DPRINTK("sas_discover_sata() for device %16llx at "
809 "%016llx:0x%x returned 0x%x\n",
810 SAS_ADDR(child->sas_addr),
811 SAS_ADDR(parent->sas_addr), phy_id, res);
James Bottomley1acce192006-08-22 12:39:19 -0500812 goto out_list_del;
James Bottomley2908d772006-08-29 09:22:51 -0500813 }
James Bottomleyb9142172007-07-22 13:15:55 -0500814 } else
815#endif
Darrick J. Wong5929faf2007-11-05 11:51:17 -0800816 if (phy->attached_tproto & SAS_PROTOCOL_SSP) {
James Bottomley2908d772006-08-29 09:22:51 -0500817 child->dev_type = SAS_END_DEV;
818 rphy = sas_end_device_alloc(phy->port);
819 /* FIXME: error handling */
James Bottomley024879e2006-11-15 18:03:07 -0600820 if (unlikely(!rphy))
821 goto out_free;
James Bottomley2908d772006-08-29 09:22:51 -0500822 child->tproto = phy->attached_tproto;
823 sas_init_dev(child);
824
825 child->rphy = rphy;
Dan Williams94876692012-03-20 10:53:24 -0700826 get_device(&rphy->dev);
James Bottomley2908d772006-08-29 09:22:51 -0500827 sas_fill_in_rphy(child, rphy);
828
Dan Williams92625f92012-01-18 20:14:01 -0800829 list_add_tail(&child->disco_list_node, &parent->port->disco_list);
James Bottomley2908d772006-08-29 09:22:51 -0500830
831 res = sas_discover_end_dev(child);
832 if (res) {
833 SAS_DPRINTK("sas_discover_end_dev() for device %16llx "
834 "at %016llx:0x%x returned 0x%x\n",
835 SAS_ADDR(child->sas_addr),
836 SAS_ADDR(parent->sas_addr), phy_id, res);
James Bottomley024879e2006-11-15 18:03:07 -0600837 goto out_list_del;
James Bottomley2908d772006-08-29 09:22:51 -0500838 }
839 } else {
840 SAS_DPRINTK("target proto 0x%x at %016llx:0x%x not handled\n",
841 phy->attached_tproto, SAS_ADDR(parent->sas_addr),
842 phy_id);
James Bottomleyb9142172007-07-22 13:15:55 -0500843 goto out_free;
James Bottomley2908d772006-08-29 09:22:51 -0500844 }
845
846 list_add_tail(&child->siblings, &parent_ex->children);
847 return child;
James Bottomley024879e2006-11-15 18:03:07 -0600848
849 out_list_del:
Darrick J. Wong6f63caa2007-01-26 14:08:43 -0800850 sas_rphy_free(child->rphy);
Dan Williams87c83312011-11-17 17:59:51 -0800851 list_del(&child->disco_list_node);
Dan Williams1a34c062011-09-21 22:05:34 -0700852 spin_lock_irq(&parent->port->dev_list_lock);
James Bottomley024879e2006-11-15 18:03:07 -0600853 list_del(&child->dev_list_node);
Dan Williams1a34c062011-09-21 22:05:34 -0700854 spin_unlock_irq(&parent->port->dev_list_lock);
James Bottomley024879e2006-11-15 18:03:07 -0600855 out_free:
856 sas_port_delete(phy->port);
857 out_err:
858 phy->port = NULL;
Dan Williams735f7d22011-11-17 17:59:47 -0800859 sas_put_device(child);
James Bottomley024879e2006-11-15 18:03:07 -0600860 return NULL;
James Bottomley2908d772006-08-29 09:22:51 -0500861}
862
Darrick J. Wong423f7cf2007-01-30 12:07:27 -0800863/* See if this phy is part of a wide port */
864static int sas_ex_join_wide_port(struct domain_device *parent, int phy_id)
865{
866 struct ex_phy *phy = &parent->ex_dev.ex_phy[phy_id];
867 int i;
868
869 for (i = 0; i < parent->ex_dev.num_phys; i++) {
870 struct ex_phy *ephy = &parent->ex_dev.ex_phy[i];
871
872 if (ephy == phy)
873 continue;
874
875 if (!memcmp(phy->attached_sas_addr, ephy->attached_sas_addr,
876 SAS_ADDR_SIZE) && ephy->port) {
877 sas_port_add_phy(ephy->port, phy->phy);
Tom Peng19252de2009-07-17 16:02:04 +0800878 phy->port = ephy->port;
Darrick J. Wong423f7cf2007-01-30 12:07:27 -0800879 phy->phy_state = PHY_DEVICE_DISCOVERED;
880 return 0;
881 }
882 }
883
884 return -ENODEV;
885}
886
James Bottomley2908d772006-08-29 09:22:51 -0500887static struct domain_device *sas_ex_discover_expander(
888 struct domain_device *parent, int phy_id)
889{
890 struct sas_expander_device *parent_ex = rphy_to_expander_device(parent->rphy);
891 struct ex_phy *phy = &parent->ex_dev.ex_phy[phy_id];
892 struct domain_device *child = NULL;
893 struct sas_rphy *rphy;
894 struct sas_expander_device *edev;
895 struct asd_sas_port *port;
896 int res;
897
898 if (phy->routing_attr == DIRECT_ROUTING) {
899 SAS_DPRINTK("ex %016llx:0x%x:D <--> ex %016llx:0x%x is not "
900 "allowed\n",
901 SAS_ADDR(parent->sas_addr), phy_id,
902 SAS_ADDR(phy->attached_sas_addr),
903 phy->attached_phy_id);
904 return NULL;
905 }
Dan Williams735f7d22011-11-17 17:59:47 -0800906 child = sas_alloc_device();
James Bottomley2908d772006-08-29 09:22:51 -0500907 if (!child)
908 return NULL;
909
910 phy->port = sas_port_alloc(&parent->rphy->dev, phy_id);
911 /* FIXME: better error handling */
912 BUG_ON(sas_port_add(phy->port) != 0);
913
914
915 switch (phy->attached_dev_type) {
916 case EDGE_DEV:
917 rphy = sas_expander_alloc(phy->port,
918 SAS_EDGE_EXPANDER_DEVICE);
919 break;
920 case FANOUT_DEV:
921 rphy = sas_expander_alloc(phy->port,
922 SAS_FANOUT_EXPANDER_DEVICE);
923 break;
924 default:
925 rphy = NULL; /* shut gcc up */
926 BUG();
927 }
928 port = parent->port;
929 child->rphy = rphy;
Dan Williams94876692012-03-20 10:53:24 -0700930 get_device(&rphy->dev);
James Bottomley2908d772006-08-29 09:22:51 -0500931 edev = rphy_to_expander_device(rphy);
932 child->dev_type = phy->attached_dev_type;
Dan Williams735f7d22011-11-17 17:59:47 -0800933 kref_get(&parent->kref);
James Bottomley2908d772006-08-29 09:22:51 -0500934 child->parent = parent;
935 child->port = port;
936 child->iproto = phy->attached_iproto;
937 child->tproto = phy->attached_tproto;
938 memcpy(child->sas_addr, phy->attached_sas_addr, SAS_ADDR_SIZE);
939 sas_hash_addr(child->hashed_sas_addr, child->sas_addr);
940 sas_ex_get_linkrate(parent, child, phy);
941 edev->level = parent_ex->level + 1;
942 parent->port->disc.max_level = max(parent->port->disc.max_level,
943 edev->level);
944 sas_init_dev(child);
945 sas_fill_in_rphy(child, rphy);
946 sas_rphy_add(rphy);
947
James Bottomley9d720d82007-07-16 13:15:51 -0500948 spin_lock_irq(&parent->port->dev_list_lock);
James Bottomley2908d772006-08-29 09:22:51 -0500949 list_add_tail(&child->dev_list_node, &parent->port->dev_list);
James Bottomley9d720d82007-07-16 13:15:51 -0500950 spin_unlock_irq(&parent->port->dev_list_lock);
James Bottomley2908d772006-08-29 09:22:51 -0500951
952 res = sas_discover_expander(child);
953 if (res) {
Dan Williams94876692012-03-20 10:53:24 -0700954 sas_rphy_delete(rphy);
Luben Tuikov5911e962011-07-26 23:10:48 -0700955 spin_lock_irq(&parent->port->dev_list_lock);
956 list_del(&child->dev_list_node);
957 spin_unlock_irq(&parent->port->dev_list_lock);
Dan Williams735f7d22011-11-17 17:59:47 -0800958 sas_put_device(child);
James Bottomley2908d772006-08-29 09:22:51 -0500959 return NULL;
960 }
961 list_add_tail(&child->siblings, &parent->ex_dev.children);
962 return child;
963}
964
965static int sas_ex_discover_dev(struct domain_device *dev, int phy_id)
966{
967 struct expander_device *ex = &dev->ex_dev;
968 struct ex_phy *ex_phy = &ex->ex_phy[phy_id];
969 struct domain_device *child = NULL;
970 int res = 0;
971
972 /* Phy state */
James Bottomley88edf742006-09-06 17:36:13 -0500973 if (ex_phy->linkrate == SAS_SATA_SPINUP_HOLD) {
James Bottomleya01e70e2006-09-06 19:28:07 -0500974 if (!sas_smp_phy_control(dev, phy_id, PHY_FUNC_LINK_RESET, NULL))
James Bottomley2908d772006-08-29 09:22:51 -0500975 res = sas_ex_phy_discover(dev, phy_id);
976 if (res)
977 return res;
978 }
979
980 /* Parent and domain coherency */
981 if (!dev->parent && (SAS_ADDR(ex_phy->attached_sas_addr) ==
982 SAS_ADDR(dev->port->sas_addr))) {
983 sas_add_parent_port(dev, phy_id);
984 return 0;
985 }
986 if (dev->parent && (SAS_ADDR(ex_phy->attached_sas_addr) ==
987 SAS_ADDR(dev->parent->sas_addr))) {
988 sas_add_parent_port(dev, phy_id);
989 if (ex_phy->routing_attr == TABLE_ROUTING)
990 sas_configure_phy(dev, phy_id, dev->port->sas_addr, 1);
991 return 0;
992 }
993
994 if (sas_dev_present_in_domain(dev->port, ex_phy->attached_sas_addr))
995 sas_ex_disable_port(dev, ex_phy->attached_sas_addr);
996
997 if (ex_phy->attached_dev_type == NO_DEVICE) {
998 if (ex_phy->routing_attr == DIRECT_ROUTING) {
999 memset(ex_phy->attached_sas_addr, 0, SAS_ADDR_SIZE);
1000 sas_configure_routing(dev, ex_phy->attached_sas_addr);
1001 }
1002 return 0;
James Bottomley88edf742006-09-06 17:36:13 -05001003 } else if (ex_phy->linkrate == SAS_LINK_RATE_UNKNOWN)
James Bottomley2908d772006-08-29 09:22:51 -05001004 return 0;
1005
1006 if (ex_phy->attached_dev_type != SAS_END_DEV &&
1007 ex_phy->attached_dev_type != FANOUT_DEV &&
Dan Williams354cf822012-01-12 17:57:35 -08001008 ex_phy->attached_dev_type != EDGE_DEV &&
1009 ex_phy->attached_dev_type != SATA_PENDING) {
James Bottomley2908d772006-08-29 09:22:51 -05001010 SAS_DPRINTK("unknown device type(0x%x) attached to ex %016llx "
1011 "phy 0x%x\n", ex_phy->attached_dev_type,
1012 SAS_ADDR(dev->sas_addr),
1013 phy_id);
1014 return 0;
1015 }
1016
1017 res = sas_configure_routing(dev, ex_phy->attached_sas_addr);
1018 if (res) {
1019 SAS_DPRINTK("configure routing for dev %016llx "
1020 "reported 0x%x. Forgotten\n",
1021 SAS_ADDR(ex_phy->attached_sas_addr), res);
1022 sas_disable_routing(dev, ex_phy->attached_sas_addr);
1023 return res;
1024 }
1025
Darrick J. Wong423f7cf2007-01-30 12:07:27 -08001026 res = sas_ex_join_wide_port(dev, phy_id);
1027 if (!res) {
1028 SAS_DPRINTK("Attaching ex phy%d to wide port %016llx\n",
1029 phy_id, SAS_ADDR(ex_phy->attached_sas_addr));
1030 return res;
1031 }
1032
James Bottomley2908d772006-08-29 09:22:51 -05001033 switch (ex_phy->attached_dev_type) {
1034 case SAS_END_DEV:
Dan Williams354cf822012-01-12 17:57:35 -08001035 case SATA_PENDING:
James Bottomley2908d772006-08-29 09:22:51 -05001036 child = sas_ex_discover_end_dev(dev, phy_id);
1037 break;
1038 case FANOUT_DEV:
1039 if (SAS_ADDR(dev->port->disc.fanout_sas_addr)) {
1040 SAS_DPRINTK("second fanout expander %016llx phy 0x%x "
1041 "attached to ex %016llx phy 0x%x\n",
1042 SAS_ADDR(ex_phy->attached_sas_addr),
1043 ex_phy->attached_phy_id,
1044 SAS_ADDR(dev->sas_addr),
1045 phy_id);
1046 sas_ex_disable_phy(dev, phy_id);
1047 break;
1048 } else
1049 memcpy(dev->port->disc.fanout_sas_addr,
1050 ex_phy->attached_sas_addr, SAS_ADDR_SIZE);
1051 /* fallthrough */
1052 case EDGE_DEV:
1053 child = sas_ex_discover_expander(dev, phy_id);
1054 break;
1055 default:
1056 break;
1057 }
1058
1059 if (child) {
1060 int i;
1061
1062 for (i = 0; i < ex->num_phys; i++) {
1063 if (ex->ex_phy[i].phy_state == PHY_VACANT ||
1064 ex->ex_phy[i].phy_state == PHY_NOT_PRESENT)
1065 continue;
Tom Peng19252de2009-07-17 16:02:04 +08001066 /*
1067 * Due to races, the phy might not get added to the
1068 * wide port, so we add the phy to the wide port here.
1069 */
James Bottomley2908d772006-08-29 09:22:51 -05001070 if (SAS_ADDR(ex->ex_phy[i].attached_sas_addr) ==
Tom Peng19252de2009-07-17 16:02:04 +08001071 SAS_ADDR(child->sas_addr)) {
James Bottomley2908d772006-08-29 09:22:51 -05001072 ex->ex_phy[i].phy_state= PHY_DEVICE_DISCOVERED;
Tom Peng19252de2009-07-17 16:02:04 +08001073 res = sas_ex_join_wide_port(dev, i);
1074 if (!res)
1075 SAS_DPRINTK("Attaching ex phy%d to wide port %016llx\n",
1076 i, SAS_ADDR(ex->ex_phy[i].attached_sas_addr));
1077
1078 }
James Bottomley2908d772006-08-29 09:22:51 -05001079 }
1080 }
1081
1082 return res;
1083}
1084
1085static int sas_find_sub_addr(struct domain_device *dev, u8 *sub_addr)
1086{
1087 struct expander_device *ex = &dev->ex_dev;
1088 int i;
1089
1090 for (i = 0; i < ex->num_phys; i++) {
1091 struct ex_phy *phy = &ex->ex_phy[i];
1092
1093 if (phy->phy_state == PHY_VACANT ||
1094 phy->phy_state == PHY_NOT_PRESENT)
1095 continue;
1096
1097 if ((phy->attached_dev_type == EDGE_DEV ||
1098 phy->attached_dev_type == FANOUT_DEV) &&
1099 phy->routing_attr == SUBTRACTIVE_ROUTING) {
1100
1101 memcpy(sub_addr, phy->attached_sas_addr,SAS_ADDR_SIZE);
1102
1103 return 1;
1104 }
1105 }
1106 return 0;
1107}
1108
1109static int sas_check_level_subtractive_boundary(struct domain_device *dev)
1110{
1111 struct expander_device *ex = &dev->ex_dev;
1112 struct domain_device *child;
1113 u8 sub_addr[8] = {0, };
1114
1115 list_for_each_entry(child, &ex->children, siblings) {
1116 if (child->dev_type != EDGE_DEV &&
1117 child->dev_type != FANOUT_DEV)
1118 continue;
1119 if (sub_addr[0] == 0) {
1120 sas_find_sub_addr(child, sub_addr);
1121 continue;
1122 } else {
1123 u8 s2[8];
1124
1125 if (sas_find_sub_addr(child, s2) &&
1126 (SAS_ADDR(sub_addr) != SAS_ADDR(s2))) {
1127
1128 SAS_DPRINTK("ex %016llx->%016llx-?->%016llx "
1129 "diverges from subtractive "
1130 "boundary %016llx\n",
1131 SAS_ADDR(dev->sas_addr),
1132 SAS_ADDR(child->sas_addr),
1133 SAS_ADDR(s2),
1134 SAS_ADDR(sub_addr));
1135
1136 sas_ex_disable_port(child, s2);
1137 }
1138 }
1139 }
1140 return 0;
1141}
1142/**
1143 * sas_ex_discover_devices -- discover devices attached to this expander
1144 * dev: pointer to the expander domain device
1145 * single: if you want to do a single phy, else set to -1;
1146 *
1147 * Configure this expander for use with its devices and register the
1148 * devices of this expander.
1149 */
1150static int sas_ex_discover_devices(struct domain_device *dev, int single)
1151{
1152 struct expander_device *ex = &dev->ex_dev;
1153 int i = 0, end = ex->num_phys;
1154 int res = 0;
1155
1156 if (0 <= single && single < end) {
1157 i = single;
1158 end = i+1;
1159 }
1160
1161 for ( ; i < end; i++) {
1162 struct ex_phy *ex_phy = &ex->ex_phy[i];
1163
1164 if (ex_phy->phy_state == PHY_VACANT ||
1165 ex_phy->phy_state == PHY_NOT_PRESENT ||
1166 ex_phy->phy_state == PHY_DEVICE_DISCOVERED)
1167 continue;
1168
1169 switch (ex_phy->linkrate) {
James Bottomley88edf742006-09-06 17:36:13 -05001170 case SAS_PHY_DISABLED:
1171 case SAS_PHY_RESET_PROBLEM:
1172 case SAS_SATA_PORT_SELECTOR:
James Bottomley2908d772006-08-29 09:22:51 -05001173 continue;
1174 default:
1175 res = sas_ex_discover_dev(dev, i);
1176 if (res)
1177 break;
1178 continue;
1179 }
1180 }
1181
1182 if (!res)
1183 sas_check_level_subtractive_boundary(dev);
1184
1185 return res;
1186}
1187
1188static int sas_check_ex_subtractive_boundary(struct domain_device *dev)
1189{
1190 struct expander_device *ex = &dev->ex_dev;
1191 int i;
1192 u8 *sub_sas_addr = NULL;
1193
1194 if (dev->dev_type != EDGE_DEV)
1195 return 0;
1196
1197 for (i = 0; i < ex->num_phys; i++) {
1198 struct ex_phy *phy = &ex->ex_phy[i];
1199
1200 if (phy->phy_state == PHY_VACANT ||
1201 phy->phy_state == PHY_NOT_PRESENT)
1202 continue;
1203
1204 if ((phy->attached_dev_type == FANOUT_DEV ||
1205 phy->attached_dev_type == EDGE_DEV) &&
1206 phy->routing_attr == SUBTRACTIVE_ROUTING) {
1207
1208 if (!sub_sas_addr)
1209 sub_sas_addr = &phy->attached_sas_addr[0];
1210 else if (SAS_ADDR(sub_sas_addr) !=
1211 SAS_ADDR(phy->attached_sas_addr)) {
1212
1213 SAS_DPRINTK("ex %016llx phy 0x%x "
1214 "diverges(%016llx) on subtractive "
1215 "boundary(%016llx). Disabled\n",
1216 SAS_ADDR(dev->sas_addr), i,
1217 SAS_ADDR(phy->attached_sas_addr),
1218 SAS_ADDR(sub_sas_addr));
1219 sas_ex_disable_phy(dev, i);
1220 }
1221 }
1222 }
1223 return 0;
1224}
1225
1226static void sas_print_parent_topology_bug(struct domain_device *child,
1227 struct ex_phy *parent_phy,
1228 struct ex_phy *child_phy)
1229{
James Bottomley2908d772006-08-29 09:22:51 -05001230 static const char *ex_type[] = {
1231 [EDGE_DEV] = "edge",
1232 [FANOUT_DEV] = "fanout",
1233 };
1234 struct domain_device *parent = child->parent;
1235
Dan Williamsd214d812012-01-16 11:56:50 -08001236 sas_printk("%s ex %016llx phy 0x%x <--> %s ex %016llx "
1237 "phy 0x%x has %c:%c routing link!\n",
James Bottomley2908d772006-08-29 09:22:51 -05001238
1239 ex_type[parent->dev_type],
1240 SAS_ADDR(parent->sas_addr),
1241 parent_phy->phy_id,
1242
1243 ex_type[child->dev_type],
1244 SAS_ADDR(child->sas_addr),
1245 child_phy->phy_id,
1246
Dan Williamsd214d812012-01-16 11:56:50 -08001247 sas_route_char(parent, parent_phy),
1248 sas_route_char(child, child_phy));
James Bottomley2908d772006-08-29 09:22:51 -05001249}
1250
1251static int sas_check_eeds(struct domain_device *child,
1252 struct ex_phy *parent_phy,
1253 struct ex_phy *child_phy)
1254{
1255 int res = 0;
1256 struct domain_device *parent = child->parent;
1257
1258 if (SAS_ADDR(parent->port->disc.fanout_sas_addr) != 0) {
1259 res = -ENODEV;
1260 SAS_DPRINTK("edge ex %016llx phy S:0x%x <--> edge ex %016llx "
1261 "phy S:0x%x, while there is a fanout ex %016llx\n",
1262 SAS_ADDR(parent->sas_addr),
1263 parent_phy->phy_id,
1264 SAS_ADDR(child->sas_addr),
1265 child_phy->phy_id,
1266 SAS_ADDR(parent->port->disc.fanout_sas_addr));
1267 } else if (SAS_ADDR(parent->port->disc.eeds_a) == 0) {
1268 memcpy(parent->port->disc.eeds_a, parent->sas_addr,
1269 SAS_ADDR_SIZE);
1270 memcpy(parent->port->disc.eeds_b, child->sas_addr,
1271 SAS_ADDR_SIZE);
1272 } else if (((SAS_ADDR(parent->port->disc.eeds_a) ==
1273 SAS_ADDR(parent->sas_addr)) ||
1274 (SAS_ADDR(parent->port->disc.eeds_a) ==
1275 SAS_ADDR(child->sas_addr)))
1276 &&
1277 ((SAS_ADDR(parent->port->disc.eeds_b) ==
1278 SAS_ADDR(parent->sas_addr)) ||
1279 (SAS_ADDR(parent->port->disc.eeds_b) ==
1280 SAS_ADDR(child->sas_addr))))
1281 ;
1282 else {
1283 res = -ENODEV;
1284 SAS_DPRINTK("edge ex %016llx phy 0x%x <--> edge ex %016llx "
1285 "phy 0x%x link forms a third EEDS!\n",
1286 SAS_ADDR(parent->sas_addr),
1287 parent_phy->phy_id,
1288 SAS_ADDR(child->sas_addr),
1289 child_phy->phy_id);
1290 }
1291
1292 return res;
1293}
1294
1295/* Here we spill over 80 columns. It is intentional.
1296 */
1297static int sas_check_parent_topology(struct domain_device *child)
1298{
1299 struct expander_device *child_ex = &child->ex_dev;
1300 struct expander_device *parent_ex;
1301 int i;
1302 int res = 0;
1303
1304 if (!child->parent)
1305 return 0;
1306
1307 if (child->parent->dev_type != EDGE_DEV &&
1308 child->parent->dev_type != FANOUT_DEV)
1309 return 0;
1310
1311 parent_ex = &child->parent->ex_dev;
1312
1313 for (i = 0; i < parent_ex->num_phys; i++) {
1314 struct ex_phy *parent_phy = &parent_ex->ex_phy[i];
1315 struct ex_phy *child_phy;
1316
1317 if (parent_phy->phy_state == PHY_VACANT ||
1318 parent_phy->phy_state == PHY_NOT_PRESENT)
1319 continue;
1320
1321 if (SAS_ADDR(parent_phy->attached_sas_addr) != SAS_ADDR(child->sas_addr))
1322 continue;
1323
1324 child_phy = &child_ex->ex_phy[parent_phy->attached_phy_id];
1325
1326 switch (child->parent->dev_type) {
1327 case EDGE_DEV:
1328 if (child->dev_type == FANOUT_DEV) {
1329 if (parent_phy->routing_attr != SUBTRACTIVE_ROUTING ||
1330 child_phy->routing_attr != TABLE_ROUTING) {
1331 sas_print_parent_topology_bug(child, parent_phy, child_phy);
1332 res = -ENODEV;
1333 }
1334 } else if (parent_phy->routing_attr == SUBTRACTIVE_ROUTING) {
1335 if (child_phy->routing_attr == SUBTRACTIVE_ROUTING) {
1336 res = sas_check_eeds(child, parent_phy, child_phy);
1337 } else if (child_phy->routing_attr != TABLE_ROUTING) {
1338 sas_print_parent_topology_bug(child, parent_phy, child_phy);
1339 res = -ENODEV;
1340 }
Luben Tuikovffaac8f2011-09-22 09:41:36 -07001341 } else if (parent_phy->routing_attr == TABLE_ROUTING) {
1342 if (child_phy->routing_attr == SUBTRACTIVE_ROUTING ||
1343 (child_phy->routing_attr == TABLE_ROUTING &&
1344 child_ex->t2t_supp && parent_ex->t2t_supp)) {
1345 /* All good */;
1346 } else {
1347 sas_print_parent_topology_bug(child, parent_phy, child_phy);
1348 res = -ENODEV;
1349 }
James Bottomley2908d772006-08-29 09:22:51 -05001350 }
1351 break;
1352 case FANOUT_DEV:
1353 if (parent_phy->routing_attr != TABLE_ROUTING ||
1354 child_phy->routing_attr != SUBTRACTIVE_ROUTING) {
1355 sas_print_parent_topology_bug(child, parent_phy, child_phy);
1356 res = -ENODEV;
1357 }
1358 break;
1359 default:
1360 break;
1361 }
1362 }
1363
1364 return res;
1365}
1366
1367#define RRI_REQ_SIZE 16
1368#define RRI_RESP_SIZE 44
1369
1370static int sas_configure_present(struct domain_device *dev, int phy_id,
1371 u8 *sas_addr, int *index, int *present)
1372{
1373 int i, res = 0;
1374 struct expander_device *ex = &dev->ex_dev;
1375 struct ex_phy *phy = &ex->ex_phy[phy_id];
1376 u8 *rri_req;
1377 u8 *rri_resp;
1378
1379 *present = 0;
1380 *index = 0;
1381
1382 rri_req = alloc_smp_req(RRI_REQ_SIZE);
1383 if (!rri_req)
1384 return -ENOMEM;
1385
1386 rri_resp = alloc_smp_resp(RRI_RESP_SIZE);
1387 if (!rri_resp) {
1388 kfree(rri_req);
1389 return -ENOMEM;
1390 }
1391
1392 rri_req[1] = SMP_REPORT_ROUTE_INFO;
1393 rri_req[9] = phy_id;
1394
1395 for (i = 0; i < ex->max_route_indexes ; i++) {
1396 *(__be16 *)(rri_req+6) = cpu_to_be16(i);
1397 res = smp_execute_task(dev, rri_req, RRI_REQ_SIZE, rri_resp,
1398 RRI_RESP_SIZE);
1399 if (res)
1400 goto out;
1401 res = rri_resp[2];
1402 if (res == SMP_RESP_NO_INDEX) {
1403 SAS_DPRINTK("overflow of indexes: dev %016llx "
1404 "phy 0x%x index 0x%x\n",
1405 SAS_ADDR(dev->sas_addr), phy_id, i);
1406 goto out;
1407 } else if (res != SMP_RESP_FUNC_ACC) {
1408 SAS_DPRINTK("%s: dev %016llx phy 0x%x index 0x%x "
Harvey Harrisoncadbd4a2008-07-03 23:47:27 -07001409 "result 0x%x\n", __func__,
James Bottomley2908d772006-08-29 09:22:51 -05001410 SAS_ADDR(dev->sas_addr), phy_id, i, res);
1411 goto out;
1412 }
1413 if (SAS_ADDR(sas_addr) != 0) {
1414 if (SAS_ADDR(rri_resp+16) == SAS_ADDR(sas_addr)) {
1415 *index = i;
1416 if ((rri_resp[12] & 0x80) == 0x80)
1417 *present = 0;
1418 else
1419 *present = 1;
1420 goto out;
1421 } else if (SAS_ADDR(rri_resp+16) == 0) {
1422 *index = i;
1423 *present = 0;
1424 goto out;
1425 }
1426 } else if (SAS_ADDR(rri_resp+16) == 0 &&
1427 phy->last_da_index < i) {
1428 phy->last_da_index = i;
1429 *index = i;
1430 *present = 0;
1431 goto out;
1432 }
1433 }
1434 res = -1;
1435out:
1436 kfree(rri_req);
1437 kfree(rri_resp);
1438 return res;
1439}
1440
1441#define CRI_REQ_SIZE 44
1442#define CRI_RESP_SIZE 8
1443
1444static int sas_configure_set(struct domain_device *dev, int phy_id,
1445 u8 *sas_addr, int index, int include)
1446{
1447 int res;
1448 u8 *cri_req;
1449 u8 *cri_resp;
1450
1451 cri_req = alloc_smp_req(CRI_REQ_SIZE);
1452 if (!cri_req)
1453 return -ENOMEM;
1454
1455 cri_resp = alloc_smp_resp(CRI_RESP_SIZE);
1456 if (!cri_resp) {
1457 kfree(cri_req);
1458 return -ENOMEM;
1459 }
1460
1461 cri_req[1] = SMP_CONF_ROUTE_INFO;
1462 *(__be16 *)(cri_req+6) = cpu_to_be16(index);
1463 cri_req[9] = phy_id;
1464 if (SAS_ADDR(sas_addr) == 0 || !include)
1465 cri_req[12] |= 0x80;
1466 memcpy(cri_req+16, sas_addr, SAS_ADDR_SIZE);
1467
1468 res = smp_execute_task(dev, cri_req, CRI_REQ_SIZE, cri_resp,
1469 CRI_RESP_SIZE);
1470 if (res)
1471 goto out;
1472 res = cri_resp[2];
1473 if (res == SMP_RESP_NO_INDEX) {
1474 SAS_DPRINTK("overflow of indexes: dev %016llx phy 0x%x "
1475 "index 0x%x\n",
1476 SAS_ADDR(dev->sas_addr), phy_id, index);
1477 }
1478out:
1479 kfree(cri_req);
1480 kfree(cri_resp);
1481 return res;
1482}
1483
1484static int sas_configure_phy(struct domain_device *dev, int phy_id,
1485 u8 *sas_addr, int include)
1486{
1487 int index;
1488 int present;
1489 int res;
1490
1491 res = sas_configure_present(dev, phy_id, sas_addr, &index, &present);
1492 if (res)
1493 return res;
1494 if (include ^ present)
1495 return sas_configure_set(dev, phy_id, sas_addr, index,include);
1496
1497 return res;
1498}
1499
1500/**
1501 * sas_configure_parent -- configure routing table of parent
1502 * parent: parent expander
1503 * child: child expander
1504 * sas_addr: SAS port identifier of device directly attached to child
1505 */
1506static int sas_configure_parent(struct domain_device *parent,
1507 struct domain_device *child,
1508 u8 *sas_addr, int include)
1509{
1510 struct expander_device *ex_parent = &parent->ex_dev;
1511 int res = 0;
1512 int i;
1513
1514 if (parent->parent) {
1515 res = sas_configure_parent(parent->parent, parent, sas_addr,
1516 include);
1517 if (res)
1518 return res;
1519 }
1520
1521 if (ex_parent->conf_route_table == 0) {
1522 SAS_DPRINTK("ex %016llx has self-configuring routing table\n",
1523 SAS_ADDR(parent->sas_addr));
1524 return 0;
1525 }
1526
1527 for (i = 0; i < ex_parent->num_phys; i++) {
1528 struct ex_phy *phy = &ex_parent->ex_phy[i];
1529
1530 if ((phy->routing_attr == TABLE_ROUTING) &&
1531 (SAS_ADDR(phy->attached_sas_addr) ==
1532 SAS_ADDR(child->sas_addr))) {
1533 res = sas_configure_phy(parent, i, sas_addr, include);
1534 if (res)
1535 return res;
1536 }
1537 }
1538
1539 return res;
1540}
1541
1542/**
1543 * sas_configure_routing -- configure routing
1544 * dev: expander device
1545 * sas_addr: port identifier of device directly attached to the expander device
1546 */
1547static int sas_configure_routing(struct domain_device *dev, u8 *sas_addr)
1548{
1549 if (dev->parent)
1550 return sas_configure_parent(dev->parent, dev, sas_addr, 1);
1551 return 0;
1552}
1553
1554static int sas_disable_routing(struct domain_device *dev, u8 *sas_addr)
1555{
1556 if (dev->parent)
1557 return sas_configure_parent(dev->parent, dev, sas_addr, 0);
1558 return 0;
1559}
1560
James Bottomley2908d772006-08-29 09:22:51 -05001561/**
1562 * sas_discover_expander -- expander discovery
1563 * @ex: pointer to expander domain device
1564 *
1565 * See comment in sas_discover_sata().
1566 */
1567static int sas_discover_expander(struct domain_device *dev)
1568{
1569 int res;
1570
1571 res = sas_notify_lldd_dev_found(dev);
1572 if (res)
1573 return res;
1574
1575 res = sas_ex_general(dev);
1576 if (res)
1577 goto out_err;
1578 res = sas_ex_manuf_info(dev);
1579 if (res)
1580 goto out_err;
1581
1582 res = sas_expander_discover(dev);
1583 if (res) {
1584 SAS_DPRINTK("expander %016llx discovery failed(0x%x)\n",
1585 SAS_ADDR(dev->sas_addr), res);
1586 goto out_err;
1587 }
1588
1589 sas_check_ex_subtractive_boundary(dev);
1590 res = sas_check_parent_topology(dev);
1591 if (res)
1592 goto out_err;
1593 return 0;
1594out_err:
1595 sas_notify_lldd_dev_gone(dev);
1596 return res;
1597}
1598
1599static int sas_ex_level_discovery(struct asd_sas_port *port, const int level)
1600{
1601 int res = 0;
1602 struct domain_device *dev;
1603
1604 list_for_each_entry(dev, &port->dev_list, dev_list_node) {
1605 if (dev->dev_type == EDGE_DEV ||
1606 dev->dev_type == FANOUT_DEV) {
1607 struct sas_expander_device *ex =
1608 rphy_to_expander_device(dev->rphy);
1609
1610 if (level == ex->level)
1611 res = sas_ex_discover_devices(dev, -1);
1612 else if (level > 0)
1613 res = sas_ex_discover_devices(port->port_dev, -1);
1614
1615 }
1616 }
1617
1618 return res;
1619}
1620
1621static int sas_ex_bfs_disc(struct asd_sas_port *port)
1622{
1623 int res;
1624 int level;
1625
1626 do {
1627 level = port->disc.max_level;
1628 res = sas_ex_level_discovery(port, level);
1629 mb();
1630 } while (level < port->disc.max_level);
1631
1632 return res;
1633}
1634
1635int sas_discover_root_expander(struct domain_device *dev)
1636{
1637 int res;
1638 struct sas_expander_device *ex = rphy_to_expander_device(dev->rphy);
1639
Darrick J. Wongbf451202007-01-11 14:14:52 -08001640 res = sas_rphy_add(dev->rphy);
1641 if (res)
1642 goto out_err;
James Bottomley2908d772006-08-29 09:22:51 -05001643
1644 ex->level = dev->port->disc.max_level; /* 0 */
1645 res = sas_discover_expander(dev);
Darrick J. Wongbf451202007-01-11 14:14:52 -08001646 if (res)
1647 goto out_err2;
James Bottomley2908d772006-08-29 09:22:51 -05001648
Darrick J. Wongbf451202007-01-11 14:14:52 -08001649 sas_ex_bfs_disc(dev->port);
1650
1651 return res;
1652
1653out_err2:
Darrick J. Wong6f63caa2007-01-26 14:08:43 -08001654 sas_rphy_remove(dev->rphy);
Darrick J. Wongbf451202007-01-11 14:14:52 -08001655out_err:
James Bottomley2908d772006-08-29 09:22:51 -05001656 return res;
1657}
1658
1659/* ---------- Domain revalidation ---------- */
1660
1661static int sas_get_phy_discover(struct domain_device *dev,
1662 int phy_id, struct smp_resp *disc_resp)
1663{
1664 int res;
1665 u8 *disc_req;
1666
1667 disc_req = alloc_smp_req(DISCOVER_REQ_SIZE);
1668 if (!disc_req)
1669 return -ENOMEM;
1670
1671 disc_req[1] = SMP_DISCOVER;
1672 disc_req[9] = phy_id;
1673
1674 res = smp_execute_task(dev, disc_req, DISCOVER_REQ_SIZE,
1675 disc_resp, DISCOVER_RESP_SIZE);
1676 if (res)
1677 goto out;
1678 else if (disc_resp->result != SMP_RESP_FUNC_ACC) {
1679 res = disc_resp->result;
1680 goto out;
1681 }
1682out:
1683 kfree(disc_req);
1684 return res;
1685}
1686
1687static int sas_get_phy_change_count(struct domain_device *dev,
1688 int phy_id, int *pcc)
1689{
1690 int res;
1691 struct smp_resp *disc_resp;
1692
1693 disc_resp = alloc_smp_resp(DISCOVER_RESP_SIZE);
1694 if (!disc_resp)
1695 return -ENOMEM;
1696
1697 res = sas_get_phy_discover(dev, phy_id, disc_resp);
1698 if (!res)
1699 *pcc = disc_resp->disc.change_count;
1700
1701 kfree(disc_resp);
1702 return res;
1703}
1704
Dan Williams354cf822012-01-12 17:57:35 -08001705static int sas_get_phy_attached_dev(struct domain_device *dev, int phy_id,
1706 u8 *sas_addr, enum sas_dev_type *type)
James Bottomley2908d772006-08-29 09:22:51 -05001707{
1708 int res;
1709 struct smp_resp *disc_resp;
1710 struct discover_resp *dr;
1711
1712 disc_resp = alloc_smp_resp(DISCOVER_RESP_SIZE);
1713 if (!disc_resp)
1714 return -ENOMEM;
1715 dr = &disc_resp->disc;
1716
1717 res = sas_get_phy_discover(dev, phy_id, disc_resp);
Dan Williams354cf822012-01-12 17:57:35 -08001718 if (res == 0) {
1719 memcpy(sas_addr, disc_resp->disc.attached_sas_addr, 8);
1720 *type = to_dev_type(dr);
1721 if (*type == 0)
1722 memset(sas_addr, 0, 8);
James Bottomley2908d772006-08-29 09:22:51 -05001723 }
1724 kfree(disc_resp);
1725 return res;
1726}
1727
1728static int sas_find_bcast_phy(struct domain_device *dev, int *phy_id,
Tom Peng19252de2009-07-17 16:02:04 +08001729 int from_phy, bool update)
James Bottomley2908d772006-08-29 09:22:51 -05001730{
1731 struct expander_device *ex = &dev->ex_dev;
1732 int res = 0;
1733 int i;
1734
1735 for (i = from_phy; i < ex->num_phys; i++) {
1736 int phy_change_count = 0;
1737
1738 res = sas_get_phy_change_count(dev, i, &phy_change_count);
Thomas Jackson16994902012-02-17 18:33:10 -08001739 switch (res) {
1740 case SMP_RESP_PHY_VACANT:
1741 case SMP_RESP_NO_PHY:
1742 continue;
1743 case SMP_RESP_FUNC_ACC:
1744 break;
1745 default:
1746 return res;
1747 }
1748
1749 if (phy_change_count != ex->ex_phy[i].phy_change_count) {
Tom Peng19252de2009-07-17 16:02:04 +08001750 if (update)
1751 ex->ex_phy[i].phy_change_count =
1752 phy_change_count;
James Bottomley2908d772006-08-29 09:22:51 -05001753 *phy_id = i;
1754 return 0;
1755 }
1756 }
Thomas Jackson16994902012-02-17 18:33:10 -08001757 return 0;
James Bottomley2908d772006-08-29 09:22:51 -05001758}
1759
1760static int sas_get_ex_change_count(struct domain_device *dev, int *ecc)
1761{
1762 int res;
1763 u8 *rg_req;
1764 struct smp_resp *rg_resp;
1765
1766 rg_req = alloc_smp_req(RG_REQ_SIZE);
1767 if (!rg_req)
1768 return -ENOMEM;
1769
1770 rg_resp = alloc_smp_resp(RG_RESP_SIZE);
1771 if (!rg_resp) {
1772 kfree(rg_req);
1773 return -ENOMEM;
1774 }
1775
1776 rg_req[1] = SMP_REPORT_GENERAL;
1777
1778 res = smp_execute_task(dev, rg_req, RG_REQ_SIZE, rg_resp,
1779 RG_RESP_SIZE);
1780 if (res)
1781 goto out;
1782 if (rg_resp->result != SMP_RESP_FUNC_ACC) {
1783 res = rg_resp->result;
1784 goto out;
1785 }
1786
1787 *ecc = be16_to_cpu(rg_resp->rg.change_count);
1788out:
1789 kfree(rg_resp);
1790 kfree(rg_req);
1791 return res;
1792}
Tom Peng19252de2009-07-17 16:02:04 +08001793/**
1794 * sas_find_bcast_dev - find the device issue BROADCAST(CHANGE).
1795 * @dev:domain device to be detect.
1796 * @src_dev: the device which originated BROADCAST(CHANGE).
1797 *
1798 * Add self-configuration expander suport. Suppose two expander cascading,
1799 * when the first level expander is self-configuring, hotplug the disks in
1800 * second level expander, BROADCAST(CHANGE) will not only be originated
1801 * in the second level expander, but also be originated in the first level
1802 * expander (see SAS protocol SAS 2r-14, 7.11 for detail), it is to say,
1803 * expander changed count in two level expanders will all increment at least
1804 * once, but the phy which chang count has changed is the source device which
1805 * we concerned.
1806 */
James Bottomley2908d772006-08-29 09:22:51 -05001807
1808static int sas_find_bcast_dev(struct domain_device *dev,
1809 struct domain_device **src_dev)
1810{
1811 struct expander_device *ex = &dev->ex_dev;
1812 int ex_change_count = -1;
Tom Peng19252de2009-07-17 16:02:04 +08001813 int phy_id = -1;
James Bottomley2908d772006-08-29 09:22:51 -05001814 int res;
Tom Peng19252de2009-07-17 16:02:04 +08001815 struct domain_device *ch;
James Bottomley2908d772006-08-29 09:22:51 -05001816
1817 res = sas_get_ex_change_count(dev, &ex_change_count);
1818 if (res)
1819 goto out;
Tom Peng19252de2009-07-17 16:02:04 +08001820 if (ex_change_count != -1 && ex_change_count != ex->ex_change_count) {
1821 /* Just detect if this expander phys phy change count changed,
1822 * in order to determine if this expander originate BROADCAST,
1823 * and do not update phy change count field in our structure.
1824 */
1825 res = sas_find_bcast_phy(dev, &phy_id, 0, false);
1826 if (phy_id != -1) {
1827 *src_dev = dev;
1828 ex->ex_change_count = ex_change_count;
1829 SAS_DPRINTK("Expander phy change count has changed\n");
1830 return res;
1831 } else
1832 SAS_DPRINTK("Expander phys DID NOT change\n");
1833 }
1834 list_for_each_entry(ch, &ex->children, siblings) {
1835 if (ch->dev_type == EDGE_DEV || ch->dev_type == FANOUT_DEV) {
1836 res = sas_find_bcast_dev(ch, src_dev);
Mark Salyzyn24926da2011-09-01 06:11:17 -07001837 if (*src_dev)
Tom Peng19252de2009-07-17 16:02:04 +08001838 return res;
James Bottomley2908d772006-08-29 09:22:51 -05001839 }
1840 }
1841out:
1842 return res;
1843}
1844
Dan Williams1a34c062011-09-21 22:05:34 -07001845static void sas_unregister_ex_tree(struct asd_sas_port *port, struct domain_device *dev)
James Bottomley2908d772006-08-29 09:22:51 -05001846{
1847 struct expander_device *ex = &dev->ex_dev;
1848 struct domain_device *child, *n;
1849
1850 list_for_each_entry_safe(child, n, &ex->children, siblings) {
Dan Williamse1399422012-01-07 08:52:39 +00001851 set_bit(SAS_DEV_GONE, &child->state);
James Bottomley2908d772006-08-29 09:22:51 -05001852 if (child->dev_type == EDGE_DEV ||
1853 child->dev_type == FANOUT_DEV)
Dan Williams1a34c062011-09-21 22:05:34 -07001854 sas_unregister_ex_tree(port, child);
James Bottomley2908d772006-08-29 09:22:51 -05001855 else
Dan Williams1a34c062011-09-21 22:05:34 -07001856 sas_unregister_dev(port, child);
James Bottomley2908d772006-08-29 09:22:51 -05001857 }
Dan Williams1a34c062011-09-21 22:05:34 -07001858 sas_unregister_dev(port, dev);
James Bottomley2908d772006-08-29 09:22:51 -05001859}
1860
1861static void sas_unregister_devs_sas_addr(struct domain_device *parent,
Tom Peng19252de2009-07-17 16:02:04 +08001862 int phy_id, bool last)
James Bottomley2908d772006-08-29 09:22:51 -05001863{
1864 struct expander_device *ex_dev = &parent->ex_dev;
1865 struct ex_phy *phy = &ex_dev->ex_phy[phy_id];
Dan Williamsf41a0c42011-12-21 21:33:17 -08001866 struct domain_device *child, *n, *found = NULL;
Tom Peng19252de2009-07-17 16:02:04 +08001867 if (last) {
1868 list_for_each_entry_safe(child, n,
1869 &ex_dev->children, siblings) {
1870 if (SAS_ADDR(child->sas_addr) ==
1871 SAS_ADDR(phy->attached_sas_addr)) {
Dan Williamse1399422012-01-07 08:52:39 +00001872 set_bit(SAS_DEV_GONE, &child->state);
Tom Peng19252de2009-07-17 16:02:04 +08001873 if (child->dev_type == EDGE_DEV ||
1874 child->dev_type == FANOUT_DEV)
Dan Williams1a34c062011-09-21 22:05:34 -07001875 sas_unregister_ex_tree(parent->port, child);
Tom Peng19252de2009-07-17 16:02:04 +08001876 else
Dan Williams1a34c062011-09-21 22:05:34 -07001877 sas_unregister_dev(parent->port, child);
Dan Williamsf41a0c42011-12-21 21:33:17 -08001878 found = child;
Tom Peng19252de2009-07-17 16:02:04 +08001879 break;
1880 }
James Bottomley2908d772006-08-29 09:22:51 -05001881 }
Tom Peng19252de2009-07-17 16:02:04 +08001882 sas_disable_routing(parent, phy->attached_sas_addr);
James Bottomley2908d772006-08-29 09:22:51 -05001883 }
James Bottomley2908d772006-08-29 09:22:51 -05001884 memset(phy->attached_sas_addr, 0, SAS_ADDR_SIZE);
Mark Salyzyna73914c2011-09-22 08:32:23 -07001885 if (phy->port) {
1886 sas_port_delete_phy(phy->port, phy->phy);
Dan Williamsf41a0c42011-12-21 21:33:17 -08001887 sas_device_set_phy(found, phy->port);
Mark Salyzyna73914c2011-09-22 08:32:23 -07001888 if (phy->port->num_phys == 0)
1889 sas_port_delete(phy->port);
1890 phy->port = NULL;
1891 }
James Bottomley2908d772006-08-29 09:22:51 -05001892}
1893
1894static int sas_discover_bfs_by_root_level(struct domain_device *root,
1895 const int level)
1896{
1897 struct expander_device *ex_root = &root->ex_dev;
1898 struct domain_device *child;
1899 int res = 0;
1900
1901 list_for_each_entry(child, &ex_root->children, siblings) {
1902 if (child->dev_type == EDGE_DEV ||
1903 child->dev_type == FANOUT_DEV) {
1904 struct sas_expander_device *ex =
1905 rphy_to_expander_device(child->rphy);
1906
1907 if (level > ex->level)
1908 res = sas_discover_bfs_by_root_level(child,
1909 level);
1910 else if (level == ex->level)
1911 res = sas_ex_discover_devices(child, -1);
1912 }
1913 }
1914 return res;
1915}
1916
1917static int sas_discover_bfs_by_root(struct domain_device *dev)
1918{
1919 int res;
1920 struct sas_expander_device *ex = rphy_to_expander_device(dev->rphy);
1921 int level = ex->level+1;
1922
1923 res = sas_ex_discover_devices(dev, -1);
1924 if (res)
1925 goto out;
1926 do {
1927 res = sas_discover_bfs_by_root_level(dev, level);
1928 mb();
1929 level += 1;
1930 } while (level <= dev->port->disc.max_level);
1931out:
1932 return res;
1933}
1934
1935static int sas_discover_new(struct domain_device *dev, int phy_id)
1936{
1937 struct ex_phy *ex_phy = &dev->ex_dev.ex_phy[phy_id];
1938 struct domain_device *child;
Tom Peng19252de2009-07-17 16:02:04 +08001939 bool found = false;
1940 int res, i;
James Bottomley2908d772006-08-29 09:22:51 -05001941
1942 SAS_DPRINTK("ex %016llx phy%d new device attached\n",
1943 SAS_ADDR(dev->sas_addr), phy_id);
1944 res = sas_ex_phy_discover(dev, phy_id);
1945 if (res)
1946 goto out;
Tom Peng19252de2009-07-17 16:02:04 +08001947 /* to support the wide port inserted */
1948 for (i = 0; i < dev->ex_dev.num_phys; i++) {
1949 struct ex_phy *ex_phy_temp = &dev->ex_dev.ex_phy[i];
1950 if (i == phy_id)
1951 continue;
1952 if (SAS_ADDR(ex_phy_temp->attached_sas_addr) ==
1953 SAS_ADDR(ex_phy->attached_sas_addr)) {
1954 found = true;
1955 break;
1956 }
1957 }
1958 if (found) {
1959 sas_ex_join_wide_port(dev, phy_id);
1960 return 0;
1961 }
James Bottomley2908d772006-08-29 09:22:51 -05001962 res = sas_ex_discover_devices(dev, phy_id);
Tom Peng19252de2009-07-17 16:02:04 +08001963 if (!res)
James Bottomley2908d772006-08-29 09:22:51 -05001964 goto out;
1965 list_for_each_entry(child, &dev->ex_dev.children, siblings) {
1966 if (SAS_ADDR(child->sas_addr) ==
1967 SAS_ADDR(ex_phy->attached_sas_addr)) {
1968 if (child->dev_type == EDGE_DEV ||
1969 child->dev_type == FANOUT_DEV)
1970 res = sas_discover_bfs_by_root(child);
1971 break;
1972 }
1973 }
1974out:
1975 return res;
1976}
1977
Dan Williams354cf822012-01-12 17:57:35 -08001978static bool dev_type_flutter(enum sas_dev_type new, enum sas_dev_type old)
1979{
1980 if (old == new)
1981 return true;
1982
1983 /* treat device directed resets as flutter, if we went
1984 * SAS_END_DEV to SATA_PENDING the link needs recovery
1985 */
1986 if ((old == SATA_PENDING && new == SAS_END_DEV) ||
1987 (old == SAS_END_DEV && new == SATA_PENDING))
1988 return true;
1989
1990 return false;
1991}
1992
Tom Peng19252de2009-07-17 16:02:04 +08001993static int sas_rediscover_dev(struct domain_device *dev, int phy_id, bool last)
James Bottomley2908d772006-08-29 09:22:51 -05001994{
1995 struct expander_device *ex = &dev->ex_dev;
1996 struct ex_phy *phy = &ex->ex_phy[phy_id];
Dan Williams354cf822012-01-12 17:57:35 -08001997 enum sas_dev_type type = NO_DEVICE;
1998 u8 sas_addr[8];
James Bottomley2908d772006-08-29 09:22:51 -05001999 int res;
2000
Dan Williams354cf822012-01-12 17:57:35 -08002001 res = sas_get_phy_attached_dev(dev, phy_id, sas_addr, &type);
James Bottomley2908d772006-08-29 09:22:51 -05002002 switch (res) {
2003 case SMP_RESP_NO_PHY:
2004 phy->phy_state = PHY_NOT_PRESENT;
Tom Peng19252de2009-07-17 16:02:04 +08002005 sas_unregister_devs_sas_addr(dev, phy_id, last);
Dan Williams354cf822012-01-12 17:57:35 -08002006 return res;
James Bottomley2908d772006-08-29 09:22:51 -05002007 case SMP_RESP_PHY_VACANT:
2008 phy->phy_state = PHY_VACANT;
Tom Peng19252de2009-07-17 16:02:04 +08002009 sas_unregister_devs_sas_addr(dev, phy_id, last);
Dan Williams354cf822012-01-12 17:57:35 -08002010 return res;
James Bottomley2908d772006-08-29 09:22:51 -05002011 case SMP_RESP_FUNC_ACC:
2012 break;
2013 }
2014
Dan Williams354cf822012-01-12 17:57:35 -08002015 if (SAS_ADDR(sas_addr) == 0) {
James Bottomley2908d772006-08-29 09:22:51 -05002016 phy->phy_state = PHY_EMPTY;
Tom Peng19252de2009-07-17 16:02:04 +08002017 sas_unregister_devs_sas_addr(dev, phy_id, last);
Dan Williams354cf822012-01-12 17:57:35 -08002018 return res;
2019 } else if (SAS_ADDR(sas_addr) == SAS_ADDR(phy->attached_sas_addr) &&
2020 dev_type_flutter(type, phy->attached_dev_type)) {
2021 struct domain_device *ata_dev = sas_ex_to_ata(dev, phy_id);
2022 char *action = "";
2023
James Bottomleya01e70e2006-09-06 19:28:07 -05002024 sas_ex_phy_discover(dev, phy_id);
Dan Williams354cf822012-01-12 17:57:35 -08002025
2026 if (ata_dev && phy->attached_dev_type == SATA_PENDING)
2027 action = ", needs recovery";
2028 SAS_DPRINTK("ex %016llx phy 0x%x broadcast flutter%s\n",
2029 SAS_ADDR(dev->sas_addr), phy_id, action);
2030 return res;
2031 }
2032
Dan Williamsc666aae2012-01-19 18:43:08 -08002033 /* delete the old link */
2034 if (SAS_ADDR(phy->attached_sas_addr) &&
2035 SAS_ADDR(sas_addr) != SAS_ADDR(phy->attached_sas_addr)) {
2036 SAS_DPRINTK("ex %016llx phy 0x%x replace %016llx\n",
2037 SAS_ADDR(dev->sas_addr), phy_id,
2038 SAS_ADDR(phy->attached_sas_addr));
2039 sas_unregister_devs_sas_addr(dev, phy_id, last);
2040 }
2041
Dan Williams354cf822012-01-12 17:57:35 -08002042 return sas_discover_new(dev, phy_id);
James Bottomley2908d772006-08-29 09:22:51 -05002043}
2044
Tom Peng19252de2009-07-17 16:02:04 +08002045/**
2046 * sas_rediscover - revalidate the domain.
2047 * @dev:domain device to be detect.
2048 * @phy_id: the phy id will be detected.
2049 *
2050 * NOTE: this process _must_ quit (return) as soon as any connection
2051 * errors are encountered. Connection recovery is done elsewhere.
2052 * Discover process only interrogates devices in order to discover the
2053 * domain.For plugging out, we un-register the device only when it is
2054 * the last phy in the port, for other phys in this port, we just delete it
2055 * from the port.For inserting, we do discovery when it is the
2056 * first phy,for other phys in this port, we add it to the port to
2057 * forming the wide-port.
2058 */
James Bottomley2908d772006-08-29 09:22:51 -05002059static int sas_rediscover(struct domain_device *dev, const int phy_id)
2060{
2061 struct expander_device *ex = &dev->ex_dev;
2062 struct ex_phy *changed_phy = &ex->ex_phy[phy_id];
2063 int res = 0;
2064 int i;
Tom Peng19252de2009-07-17 16:02:04 +08002065 bool last = true; /* is this the last phy of the port */
James Bottomley2908d772006-08-29 09:22:51 -05002066
2067 SAS_DPRINTK("ex %016llx phy%d originated BROADCAST(CHANGE)\n",
2068 SAS_ADDR(dev->sas_addr), phy_id);
2069
2070 if (SAS_ADDR(changed_phy->attached_sas_addr) != 0) {
2071 for (i = 0; i < ex->num_phys; i++) {
2072 struct ex_phy *phy = &ex->ex_phy[i];
2073
2074 if (i == phy_id)
2075 continue;
2076 if (SAS_ADDR(phy->attached_sas_addr) ==
2077 SAS_ADDR(changed_phy->attached_sas_addr)) {
2078 SAS_DPRINTK("phy%d part of wide port with "
2079 "phy%d\n", phy_id, i);
Tom Peng19252de2009-07-17 16:02:04 +08002080 last = false;
2081 break;
James Bottomley2908d772006-08-29 09:22:51 -05002082 }
2083 }
Tom Peng19252de2009-07-17 16:02:04 +08002084 res = sas_rediscover_dev(dev, phy_id, last);
James Bottomley2908d772006-08-29 09:22:51 -05002085 } else
2086 res = sas_discover_new(dev, phy_id);
James Bottomley2908d772006-08-29 09:22:51 -05002087 return res;
2088}
2089
2090/**
2091 * sas_revalidate_domain -- revalidate the domain
2092 * @port: port to the domain of interest
2093 *
2094 * NOTE: this process _must_ quit (return) as soon as any connection
2095 * errors are encountered. Connection recovery is done elsewhere.
2096 * Discover process only interrogates devices in order to discover the
2097 * domain.
2098 */
2099int sas_ex_revalidate_domain(struct domain_device *port_dev)
2100{
2101 int res;
2102 struct domain_device *dev = NULL;
2103
2104 res = sas_find_bcast_dev(port_dev, &dev);
2105 if (res)
2106 goto out;
2107 if (dev) {
2108 struct expander_device *ex = &dev->ex_dev;
2109 int i = 0, phy_id;
2110
2111 do {
2112 phy_id = -1;
Tom Peng19252de2009-07-17 16:02:04 +08002113 res = sas_find_bcast_phy(dev, &phy_id, i, true);
James Bottomley2908d772006-08-29 09:22:51 -05002114 if (phy_id == -1)
2115 break;
2116 res = sas_rediscover(dev, phy_id);
2117 i = phy_id + 1;
2118 } while (i < ex->num_phys);
2119 }
2120out:
2121 return res;
2122}
2123
FUJITA Tomonoriba1fc172007-07-09 12:52:08 +09002124int sas_smp_handler(struct Scsi_Host *shost, struct sas_rphy *rphy,
2125 struct request *req)
2126{
2127 struct domain_device *dev;
Darrick J. Wong2cd614c2007-07-24 09:33:49 -07002128 int ret, type;
FUJITA Tomonoriba1fc172007-07-09 12:52:08 +09002129 struct request *rsp = req->next_rq;
2130
2131 if (!rsp) {
2132 printk("%s: space for a smp response is missing\n",
Harvey Harrisoncadbd4a2008-07-03 23:47:27 -07002133 __func__);
FUJITA Tomonoriba1fc172007-07-09 12:52:08 +09002134 return -EINVAL;
2135 }
2136
Darrick J. Wong2cd614c2007-07-24 09:33:49 -07002137 /* no rphy means no smp target support (ie aic94xx host) */
James Bottomleyb98e66f2007-12-28 16:35:17 -06002138 if (!rphy)
2139 return sas_smp_host_handler(shost, req, rsp);
2140
Darrick J. Wong2cd614c2007-07-24 09:33:49 -07002141 type = rphy->identify.device_type;
FUJITA Tomonoriba1fc172007-07-09 12:52:08 +09002142
2143 if (type != SAS_EDGE_EXPANDER_DEVICE &&
2144 type != SAS_FANOUT_EXPANDER_DEVICE) {
2145 printk("%s: can we send a smp request to a device?\n",
Harvey Harrisoncadbd4a2008-07-03 23:47:27 -07002146 __func__);
FUJITA Tomonoriba1fc172007-07-09 12:52:08 +09002147 return -EINVAL;
2148 }
2149
2150 dev = sas_find_dev_by_rphy(rphy);
2151 if (!dev) {
Harvey Harrisoncadbd4a2008-07-03 23:47:27 -07002152 printk("%s: fail to find a domain_device?\n", __func__);
FUJITA Tomonoriba1fc172007-07-09 12:52:08 +09002153 return -EINVAL;
2154 }
2155
2156 /* do we need to support multiple segments? */
2157 if (req->bio->bi_vcnt > 1 || rsp->bio->bi_vcnt > 1) {
2158 printk("%s: multiple segments req %u %u, rsp %u %u\n",
Tejun Heob0790412009-05-07 22:24:42 +09002159 __func__, req->bio->bi_vcnt, blk_rq_bytes(req),
2160 rsp->bio->bi_vcnt, blk_rq_bytes(rsp));
FUJITA Tomonoriba1fc172007-07-09 12:52:08 +09002161 return -EINVAL;
2162 }
2163
Tejun Heob0790412009-05-07 22:24:42 +09002164 ret = smp_execute_task(dev, bio_data(req->bio), blk_rq_bytes(req),
2165 bio_data(rsp->bio), blk_rq_bytes(rsp));
James Bottomley2d4b63e2007-12-29 11:49:53 -06002166 if (ret > 0) {
2167 /* positive number is the untransferred residual */
Tejun Heoc3a4d782009-05-07 22:24:37 +09002168 rsp->resid_len = ret;
Tejun Heo5f49f632009-05-19 18:33:05 +09002169 req->resid_len = 0;
James Bottomley2d4b63e2007-12-29 11:49:53 -06002170 ret = 0;
Tejun Heo5f49f632009-05-19 18:33:05 +09002171 } else if (ret == 0) {
2172 rsp->resid_len = 0;
2173 req->resid_len = 0;
James Bottomley2d4b63e2007-12-29 11:49:53 -06002174 }
FUJITA Tomonoriba1fc172007-07-09 12:52:08 +09002175
2176 return ret;
2177}