blob: 37cbe4d3bb9ccda64361e789fff8e3e1ba87499b [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
31#include <scsi/scsi_transport.h>
32#include <scsi/scsi_transport_sas.h>
33#include "../scsi_sas_internal.h"
34
35static int sas_discover_expander(struct domain_device *dev);
36static int sas_configure_routing(struct domain_device *dev, u8 *sas_addr);
37static int sas_configure_phy(struct domain_device *dev, int phy_id,
38 u8 *sas_addr, int include);
39static int sas_disable_routing(struct domain_device *dev, u8 *sas_addr);
40
James Bottomley2908d772006-08-29 09:22:51 -050041/* ---------- SMP task management ---------- */
42
43static void smp_task_timedout(unsigned long _task)
44{
45 struct sas_task *task = (void *) _task;
46 unsigned long flags;
47
48 spin_lock_irqsave(&task->task_state_lock, flags);
49 if (!(task->task_state_flags & SAS_TASK_STATE_DONE))
50 task->task_state_flags |= SAS_TASK_STATE_ABORTED;
51 spin_unlock_irqrestore(&task->task_state_lock, flags);
52
53 complete(&task->completion);
54}
55
56static void smp_task_done(struct sas_task *task)
57{
58 if (!del_timer(&task->timer))
59 return;
60 complete(&task->completion);
61}
62
63/* Give it some long enough timeout. In seconds. */
64#define SMP_TIMEOUT 10
65
66static int smp_execute_task(struct domain_device *dev, void *req, int req_size,
67 void *resp, int resp_size)
68{
malahal@us.ibm.com42961ee2006-10-04 17:34:03 -070069 int res, retry;
70 struct sas_task *task = NULL;
James Bottomley2908d772006-08-29 09:22:51 -050071 struct sas_internal *i =
72 to_sas_internal(dev->port->ha->core.shost->transportt);
73
malahal@us.ibm.com42961ee2006-10-04 17:34:03 -070074 for (retry = 0; retry < 3; retry++) {
75 task = sas_alloc_task(GFP_KERNEL);
76 if (!task)
77 return -ENOMEM;
James Bottomley2908d772006-08-29 09:22:51 -050078
malahal@us.ibm.com42961ee2006-10-04 17:34:03 -070079 task->dev = dev;
80 task->task_proto = dev->tproto;
81 sg_init_one(&task->smp_task.smp_req, req, req_size);
82 sg_init_one(&task->smp_task.smp_resp, resp, resp_size);
James Bottomley2908d772006-08-29 09:22:51 -050083
malahal@us.ibm.com42961ee2006-10-04 17:34:03 -070084 task->task_done = smp_task_done;
James Bottomley2908d772006-08-29 09:22:51 -050085
malahal@us.ibm.com42961ee2006-10-04 17:34:03 -070086 task->timer.data = (unsigned long) task;
87 task->timer.function = smp_task_timedout;
88 task->timer.expires = jiffies + SMP_TIMEOUT*HZ;
89 add_timer(&task->timer);
James Bottomley2908d772006-08-29 09:22:51 -050090
malahal@us.ibm.com42961ee2006-10-04 17:34:03 -070091 res = i->dft->lldd_execute_task(task, 1, GFP_KERNEL);
James Bottomley2908d772006-08-29 09:22:51 -050092
malahal@us.ibm.com42961ee2006-10-04 17:34:03 -070093 if (res) {
94 del_timer(&task->timer);
95 SAS_DPRINTK("executing SMP task failed:%d\n", res);
James Bottomley2908d772006-08-29 09:22:51 -050096 goto ex_err;
97 }
malahal@us.ibm.com42961ee2006-10-04 17:34:03 -070098
99 wait_for_completion(&task->completion);
James Bottomley32e8ae32007-12-30 12:37:31 -0600100 res = -ECOMM;
malahal@us.ibm.com42961ee2006-10-04 17:34:03 -0700101 if ((task->task_state_flags & SAS_TASK_STATE_ABORTED)) {
102 SAS_DPRINTK("smp task timed out or aborted\n");
103 i->dft->lldd_abort_task(task);
104 if (!(task->task_state_flags & SAS_TASK_STATE_DONE)) {
105 SAS_DPRINTK("SMP task aborted and not done\n");
106 goto ex_err;
107 }
108 }
109 if (task->task_status.resp == SAS_TASK_COMPLETE &&
James Bottomleydf64d3c2010-07-27 15:51:13 -0500110 task->task_status.stat == SAM_STAT_GOOD) {
malahal@us.ibm.com42961ee2006-10-04 17:34:03 -0700111 res = 0;
112 break;
James Bottomley2d4b63e2007-12-29 11:49:53 -0600113 } if (task->task_status.resp == SAS_TASK_COMPLETE &&
114 task->task_status.stat == SAS_DATA_UNDERRUN) {
115 /* no error, but return the number of bytes of
116 * underrun */
117 res = task->task_status.residual;
118 break;
119 } if (task->task_status.resp == SAS_TASK_COMPLETE &&
120 task->task_status.stat == SAS_DATA_OVERRUN) {
121 res = -EMSGSIZE;
122 break;
malahal@us.ibm.com42961ee2006-10-04 17:34:03 -0700123 } else {
124 SAS_DPRINTK("%s: task to dev %016llx response: 0x%x "
Harvey Harrisoncadbd4a2008-07-03 23:47:27 -0700125 "status 0x%x\n", __func__,
malahal@us.ibm.com42961ee2006-10-04 17:34:03 -0700126 SAS_ADDR(dev->sas_addr),
127 task->task_status.resp,
128 task->task_status.stat);
129 sas_free_task(task);
130 task = NULL;
131 }
James Bottomley2908d772006-08-29 09:22:51 -0500132 }
James Bottomley2908d772006-08-29 09:22:51 -0500133ex_err:
malahal@us.ibm.com42961ee2006-10-04 17:34:03 -0700134 BUG_ON(retry == 3 && task != NULL);
135 if (task != NULL) {
136 sas_free_task(task);
137 }
James Bottomley2908d772006-08-29 09:22:51 -0500138 return res;
139}
140
141/* ---------- Allocations ---------- */
142
143static inline void *alloc_smp_req(int size)
144{
145 u8 *p = kzalloc(size, GFP_KERNEL);
146 if (p)
147 p[0] = SMP_REQUEST;
148 return p;
149}
150
151static inline void *alloc_smp_resp(int size)
152{
153 return kzalloc(size, GFP_KERNEL);
154}
155
156/* ---------- Expander configuration ---------- */
157
158static void sas_set_ex_phy(struct domain_device *dev, int phy_id,
159 void *disc_resp)
160{
161 struct expander_device *ex = &dev->ex_dev;
162 struct ex_phy *phy = &ex->ex_phy[phy_id];
163 struct smp_resp *resp = disc_resp;
164 struct discover_resp *dr = &resp->disc;
165 struct sas_rphy *rphy = dev->rphy;
166 int rediscover = (phy->phy != NULL);
167
168 if (!rediscover) {
169 phy->phy = sas_phy_alloc(&rphy->dev, phy_id);
170
171 /* FIXME: error_handling */
172 BUG_ON(!phy->phy);
173 }
174
175 switch (resp->result) {
176 case SMP_RESP_PHY_VACANT:
177 phy->phy_state = PHY_VACANT;
Jack Wang2bc72c92010-10-06 16:05:35 +0800178 break;
James Bottomley2908d772006-08-29 09:22:51 -0500179 default:
180 phy->phy_state = PHY_NOT_PRESENT;
Jack Wang2bc72c92010-10-06 16:05:35 +0800181 break;
James Bottomley2908d772006-08-29 09:22:51 -0500182 case SMP_RESP_FUNC_ACC:
183 phy->phy_state = PHY_EMPTY; /* do not know yet */
184 break;
185 }
186
187 phy->phy_id = phy_id;
188 phy->attached_dev_type = dr->attached_dev_type;
189 phy->linkrate = dr->linkrate;
190 phy->attached_sata_host = dr->attached_sata_host;
191 phy->attached_sata_dev = dr->attached_sata_dev;
192 phy->attached_sata_ps = dr->attached_sata_ps;
193 phy->attached_iproto = dr->iproto << 1;
194 phy->attached_tproto = dr->tproto << 1;
195 memcpy(phy->attached_sas_addr, dr->attached_sas_addr, SAS_ADDR_SIZE);
196 phy->attached_phy_id = dr->attached_phy_id;
197 phy->phy_change_count = dr->change_count;
198 phy->routing_attr = dr->routing_attr;
199 phy->virtual = dr->virtual;
200 phy->last_da_index = -1;
201
Jack Wang31a471a2011-09-23 14:32:32 +0800202 phy->phy->identify.sas_address = SAS_ADDR(phy->attached_sas_addr);
203 phy->phy->identify.device_type = phy->attached_dev_type;
James Bottomley2908d772006-08-29 09:22:51 -0500204 phy->phy->identify.initiator_port_protocols = phy->attached_iproto;
205 phy->phy->identify.target_port_protocols = phy->attached_tproto;
206 phy->phy->identify.phy_identifier = phy_id;
James Bottomleya01e70e2006-09-06 19:28:07 -0500207 phy->phy->minimum_linkrate_hw = dr->hmin_linkrate;
208 phy->phy->maximum_linkrate_hw = dr->hmax_linkrate;
209 phy->phy->minimum_linkrate = dr->pmin_linkrate;
210 phy->phy->maximum_linkrate = dr->pmax_linkrate;
James Bottomley88edf742006-09-06 17:36:13 -0500211 phy->phy->negotiated_linkrate = phy->linkrate;
James Bottomley2908d772006-08-29 09:22:51 -0500212
213 if (!rediscover)
Jack Wang2bc72c92010-10-06 16:05:35 +0800214 if (sas_phy_add(phy->phy)) {
215 sas_phy_free(phy->phy);
216 return;
217 }
James Bottomley2908d772006-08-29 09:22:51 -0500218
219 SAS_DPRINTK("ex %016llx phy%02d:%c attached: %016llx\n",
220 SAS_ADDR(dev->sas_addr), phy->phy_id,
221 phy->routing_attr == TABLE_ROUTING ? 'T' :
222 phy->routing_attr == DIRECT_ROUTING ? 'D' :
223 phy->routing_attr == SUBTRACTIVE_ROUTING ? 'S' : '?',
224 SAS_ADDR(phy->attached_sas_addr));
225
226 return;
227}
228
229#define DISCOVER_REQ_SIZE 16
230#define DISCOVER_RESP_SIZE 56
231
James Bottomley1acce192006-08-22 12:39:19 -0500232static int sas_ex_phy_discover_helper(struct domain_device *dev, u8 *disc_req,
233 u8 *disc_resp, int single)
234{
235 int i, res;
236
237 disc_req[9] = single;
238 for (i = 1 ; i < 3; i++) {
239 struct discover_resp *dr;
240
241 res = smp_execute_task(dev, disc_req, DISCOVER_REQ_SIZE,
242 disc_resp, DISCOVER_RESP_SIZE);
243 if (res)
244 return res;
Lucas De Marchi25985ed2011-03-30 22:57:33 -0300245 /* This is detecting a failure to transmit initial
James Bottomley1acce192006-08-22 12:39:19 -0500246 * dev to host FIS as described in section G.5 of
247 * sas-2 r 04b */
248 dr = &((struct smp_resp *)disc_resp)->disc;
jack_wang183ce892011-02-19 18:20:53 +0800249 if (memcmp(dev->sas_addr, dr->attached_sas_addr,
250 SAS_ADDR_SIZE) == 0) {
251 sas_printk("Found loopback topology, just ignore it!\n");
252 return 0;
253 }
James Bottomley1acce192006-08-22 12:39:19 -0500254 if (!(dr->attached_dev_type == 0 &&
255 dr->attached_sata_dev))
256 break;
257 /* In order to generate the dev to host FIS, we
258 * send a link reset to the expander port */
James Bottomley38e2f032006-09-07 15:52:09 -0500259 sas_smp_phy_control(dev, single, PHY_FUNC_LINK_RESET, NULL);
James Bottomley1acce192006-08-22 12:39:19 -0500260 /* Wait for the reset to trigger the negotiation */
261 msleep(500);
262 }
263 sas_set_ex_phy(dev, single, disc_resp);
264 return 0;
265}
266
James Bottomley2908d772006-08-29 09:22:51 -0500267static int sas_ex_phy_discover(struct domain_device *dev, int single)
268{
269 struct expander_device *ex = &dev->ex_dev;
270 int res = 0;
271 u8 *disc_req;
272 u8 *disc_resp;
273
274 disc_req = alloc_smp_req(DISCOVER_REQ_SIZE);
275 if (!disc_req)
276 return -ENOMEM;
277
278 disc_resp = alloc_smp_req(DISCOVER_RESP_SIZE);
279 if (!disc_resp) {
280 kfree(disc_req);
281 return -ENOMEM;
282 }
283
284 disc_req[1] = SMP_DISCOVER;
285
286 if (0 <= single && single < ex->num_phys) {
James Bottomley1acce192006-08-22 12:39:19 -0500287 res = sas_ex_phy_discover_helper(dev, disc_req, disc_resp, single);
James Bottomley2908d772006-08-29 09:22:51 -0500288 } else {
289 int i;
290
291 for (i = 0; i < ex->num_phys; i++) {
James Bottomley1acce192006-08-22 12:39:19 -0500292 res = sas_ex_phy_discover_helper(dev, disc_req,
293 disc_resp, i);
James Bottomley2908d772006-08-29 09:22:51 -0500294 if (res)
295 goto out_err;
James Bottomley2908d772006-08-29 09:22:51 -0500296 }
297 }
298out_err:
299 kfree(disc_resp);
300 kfree(disc_req);
301 return res;
302}
303
304static int sas_expander_discover(struct domain_device *dev)
305{
306 struct expander_device *ex = &dev->ex_dev;
307 int res = -ENOMEM;
308
309 ex->ex_phy = kzalloc(sizeof(*ex->ex_phy)*ex->num_phys, GFP_KERNEL);
310 if (!ex->ex_phy)
311 return -ENOMEM;
312
313 res = sas_ex_phy_discover(dev, -1);
314 if (res)
315 goto out_err;
316
317 return 0;
318 out_err:
319 kfree(ex->ex_phy);
320 ex->ex_phy = NULL;
321 return res;
322}
323
324#define MAX_EXPANDER_PHYS 128
325
326static void ex_assign_report_general(struct domain_device *dev,
327 struct smp_resp *resp)
328{
329 struct report_general_resp *rg = &resp->rg;
330
331 dev->ex_dev.ex_change_count = be16_to_cpu(rg->change_count);
332 dev->ex_dev.max_route_indexes = be16_to_cpu(rg->route_indexes);
333 dev->ex_dev.num_phys = min(rg->num_phys, (u8)MAX_EXPANDER_PHYS);
334 dev->ex_dev.conf_route_table = rg->conf_route_table;
335 dev->ex_dev.configuring = rg->configuring;
336 memcpy(dev->ex_dev.enclosure_logical_id, rg->enclosure_logical_id, 8);
337}
338
339#define RG_REQ_SIZE 8
340#define RG_RESP_SIZE 32
341
342static int sas_ex_general(struct domain_device *dev)
343{
344 u8 *rg_req;
345 struct smp_resp *rg_resp;
346 int res;
347 int i;
348
349 rg_req = alloc_smp_req(RG_REQ_SIZE);
350 if (!rg_req)
351 return -ENOMEM;
352
353 rg_resp = alloc_smp_resp(RG_RESP_SIZE);
354 if (!rg_resp) {
355 kfree(rg_req);
356 return -ENOMEM;
357 }
358
359 rg_req[1] = SMP_REPORT_GENERAL;
360
361 for (i = 0; i < 5; i++) {
362 res = smp_execute_task(dev, rg_req, RG_REQ_SIZE, rg_resp,
363 RG_RESP_SIZE);
364
365 if (res) {
366 SAS_DPRINTK("RG to ex %016llx failed:0x%x\n",
367 SAS_ADDR(dev->sas_addr), res);
368 goto out;
369 } else if (rg_resp->result != SMP_RESP_FUNC_ACC) {
370 SAS_DPRINTK("RG:ex %016llx returned SMP result:0x%x\n",
371 SAS_ADDR(dev->sas_addr), rg_resp->result);
372 res = rg_resp->result;
373 goto out;
374 }
375
376 ex_assign_report_general(dev, rg_resp);
377
378 if (dev->ex_dev.configuring) {
379 SAS_DPRINTK("RG: ex %llx self-configuring...\n",
380 SAS_ADDR(dev->sas_addr));
381 schedule_timeout_interruptible(5*HZ);
382 } else
383 break;
384 }
385out:
386 kfree(rg_req);
387 kfree(rg_resp);
388 return res;
389}
390
391static void ex_assign_manuf_info(struct domain_device *dev, void
392 *_mi_resp)
393{
394 u8 *mi_resp = _mi_resp;
395 struct sas_rphy *rphy = dev->rphy;
396 struct sas_expander_device *edev = rphy_to_expander_device(rphy);
397
398 memcpy(edev->vendor_id, mi_resp + 12, SAS_EXPANDER_VENDOR_ID_LEN);
399 memcpy(edev->product_id, mi_resp + 20, SAS_EXPANDER_PRODUCT_ID_LEN);
400 memcpy(edev->product_rev, mi_resp + 36,
401 SAS_EXPANDER_PRODUCT_REV_LEN);
402
403 if (mi_resp[8] & 1) {
404 memcpy(edev->component_vendor_id, mi_resp + 40,
405 SAS_EXPANDER_COMPONENT_VENDOR_ID_LEN);
406 edev->component_id = mi_resp[48] << 8 | mi_resp[49];
407 edev->component_revision_id = mi_resp[50];
408 }
409}
410
411#define MI_REQ_SIZE 8
412#define MI_RESP_SIZE 64
413
414static int sas_ex_manuf_info(struct domain_device *dev)
415{
416 u8 *mi_req;
417 u8 *mi_resp;
418 int res;
419
420 mi_req = alloc_smp_req(MI_REQ_SIZE);
421 if (!mi_req)
422 return -ENOMEM;
423
424 mi_resp = alloc_smp_resp(MI_RESP_SIZE);
425 if (!mi_resp) {
426 kfree(mi_req);
427 return -ENOMEM;
428 }
429
430 mi_req[1] = SMP_REPORT_MANUF_INFO;
431
432 res = smp_execute_task(dev, mi_req, MI_REQ_SIZE, mi_resp,MI_RESP_SIZE);
433 if (res) {
434 SAS_DPRINTK("MI: ex %016llx failed:0x%x\n",
435 SAS_ADDR(dev->sas_addr), res);
436 goto out;
437 } else if (mi_resp[2] != SMP_RESP_FUNC_ACC) {
438 SAS_DPRINTK("MI ex %016llx returned SMP result:0x%x\n",
439 SAS_ADDR(dev->sas_addr), mi_resp[2]);
440 goto out;
441 }
442
443 ex_assign_manuf_info(dev, mi_resp);
444out:
445 kfree(mi_req);
446 kfree(mi_resp);
447 return res;
448}
449
450#define PC_REQ_SIZE 44
451#define PC_RESP_SIZE 8
452
453int sas_smp_phy_control(struct domain_device *dev, int phy_id,
James Bottomleya01e70e2006-09-06 19:28:07 -0500454 enum phy_func phy_func,
455 struct sas_phy_linkrates *rates)
James Bottomley2908d772006-08-29 09:22:51 -0500456{
457 u8 *pc_req;
458 u8 *pc_resp;
459 int res;
460
461 pc_req = alloc_smp_req(PC_REQ_SIZE);
462 if (!pc_req)
463 return -ENOMEM;
464
465 pc_resp = alloc_smp_resp(PC_RESP_SIZE);
466 if (!pc_resp) {
467 kfree(pc_req);
468 return -ENOMEM;
469 }
470
471 pc_req[1] = SMP_PHY_CONTROL;
472 pc_req[9] = phy_id;
473 pc_req[10]= phy_func;
James Bottomleya01e70e2006-09-06 19:28:07 -0500474 if (rates) {
475 pc_req[32] = rates->minimum_linkrate << 4;
476 pc_req[33] = rates->maximum_linkrate << 4;
477 }
James Bottomley2908d772006-08-29 09:22:51 -0500478
479 res = smp_execute_task(dev, pc_req, PC_REQ_SIZE, pc_resp,PC_RESP_SIZE);
480
481 kfree(pc_resp);
482 kfree(pc_req);
483 return res;
484}
485
486static void sas_ex_disable_phy(struct domain_device *dev, int phy_id)
487{
488 struct expander_device *ex = &dev->ex_dev;
489 struct ex_phy *phy = &ex->ex_phy[phy_id];
490
James Bottomleya01e70e2006-09-06 19:28:07 -0500491 sas_smp_phy_control(dev, phy_id, PHY_FUNC_DISABLE, NULL);
James Bottomley88edf742006-09-06 17:36:13 -0500492 phy->linkrate = SAS_PHY_DISABLED;
James Bottomley2908d772006-08-29 09:22:51 -0500493}
494
495static void sas_ex_disable_port(struct domain_device *dev, u8 *sas_addr)
496{
497 struct expander_device *ex = &dev->ex_dev;
498 int i;
499
500 for (i = 0; i < ex->num_phys; i++) {
501 struct ex_phy *phy = &ex->ex_phy[i];
502
503 if (phy->phy_state == PHY_VACANT ||
504 phy->phy_state == PHY_NOT_PRESENT)
505 continue;
506
507 if (SAS_ADDR(phy->attached_sas_addr) == SAS_ADDR(sas_addr))
508 sas_ex_disable_phy(dev, i);
509 }
510}
511
512static int sas_dev_present_in_domain(struct asd_sas_port *port,
513 u8 *sas_addr)
514{
515 struct domain_device *dev;
516
517 if (SAS_ADDR(port->sas_addr) == SAS_ADDR(sas_addr))
518 return 1;
519 list_for_each_entry(dev, &port->dev_list, dev_list_node) {
520 if (SAS_ADDR(dev->sas_addr) == SAS_ADDR(sas_addr))
521 return 1;
522 }
523 return 0;
524}
525
526#define RPEL_REQ_SIZE 16
527#define RPEL_RESP_SIZE 32
528int sas_smp_get_phy_events(struct sas_phy *phy)
529{
530 int res;
Jesper Juhl92631fa2007-07-28 01:13:33 +0200531 u8 *req;
532 u8 *resp;
James Bottomley2908d772006-08-29 09:22:51 -0500533 struct sas_rphy *rphy = dev_to_rphy(phy->dev.parent);
534 struct domain_device *dev = sas_find_dev_by_rphy(rphy);
James Bottomley2908d772006-08-29 09:22:51 -0500535
Jesper Juhl92631fa2007-07-28 01:13:33 +0200536 req = alloc_smp_req(RPEL_REQ_SIZE);
537 if (!req)
James Bottomley2908d772006-08-29 09:22:51 -0500538 return -ENOMEM;
539
Jesper Juhl92631fa2007-07-28 01:13:33 +0200540 resp = alloc_smp_resp(RPEL_RESP_SIZE);
541 if (!resp) {
542 kfree(req);
543 return -ENOMEM;
544 }
545
James Bottomley2908d772006-08-29 09:22:51 -0500546 req[1] = SMP_REPORT_PHY_ERR_LOG;
547 req[9] = phy->number;
548
549 res = smp_execute_task(dev, req, RPEL_REQ_SIZE,
550 resp, RPEL_RESP_SIZE);
551
552 if (!res)
553 goto out;
554
555 phy->invalid_dword_count = scsi_to_u32(&resp[12]);
556 phy->running_disparity_error_count = scsi_to_u32(&resp[16]);
557 phy->loss_of_dword_sync_count = scsi_to_u32(&resp[20]);
558 phy->phy_reset_problem_count = scsi_to_u32(&resp[24]);
559
560 out:
561 kfree(resp);
562 return res;
563
564}
565
James Bottomleyb9142172007-07-22 13:15:55 -0500566#ifdef CONFIG_SCSI_SAS_ATA
567
James Bottomley2908d772006-08-29 09:22:51 -0500568#define RPS_REQ_SIZE 16
569#define RPS_RESP_SIZE 60
570
571static int sas_get_report_phy_sata(struct domain_device *dev,
572 int phy_id,
573 struct smp_resp *rps_resp)
574{
575 int res;
576 u8 *rps_req = alloc_smp_req(RPS_REQ_SIZE);
James Bottomley1acce192006-08-22 12:39:19 -0500577 u8 *resp = (u8 *)rps_resp;
James Bottomley2908d772006-08-29 09:22:51 -0500578
579 if (!rps_req)
580 return -ENOMEM;
581
582 rps_req[1] = SMP_REPORT_PHY_SATA;
583 rps_req[9] = phy_id;
584
585 res = smp_execute_task(dev, rps_req, RPS_REQ_SIZE,
586 rps_resp, RPS_RESP_SIZE);
587
James Bottomley1acce192006-08-22 12:39:19 -0500588 /* 0x34 is the FIS type for the D2H fis. There's a potential
589 * standards cockup here. sas-2 explicitly specifies the FIS
590 * should be encoded so that FIS type is in resp[24].
591 * However, some expanders endian reverse this. Undo the
592 * reversal here */
593 if (!res && resp[27] == 0x34 && resp[24] != 0x34) {
594 int i;
595
596 for (i = 0; i < 5; i++) {
597 int j = 24 + (i*4);
598 u8 a, b;
599 a = resp[j + 0];
600 b = resp[j + 1];
601 resp[j + 0] = resp[j + 3];
602 resp[j + 1] = resp[j + 2];
603 resp[j + 2] = b;
604 resp[j + 3] = a;
605 }
606 }
607
James Bottomley2908d772006-08-29 09:22:51 -0500608 kfree(rps_req);
James Bottomley1acce192006-08-22 12:39:19 -0500609 return res;
James Bottomley2908d772006-08-29 09:22:51 -0500610}
James Bottomleyb9142172007-07-22 13:15:55 -0500611#endif
James Bottomley2908d772006-08-29 09:22:51 -0500612
613static void sas_ex_get_linkrate(struct domain_device *parent,
614 struct domain_device *child,
615 struct ex_phy *parent_phy)
616{
617 struct expander_device *parent_ex = &parent->ex_dev;
618 struct sas_port *port;
619 int i;
620
621 child->pathways = 0;
622
623 port = parent_phy->port;
624
625 for (i = 0; i < parent_ex->num_phys; i++) {
626 struct ex_phy *phy = &parent_ex->ex_phy[i];
627
628 if (phy->phy_state == PHY_VACANT ||
629 phy->phy_state == PHY_NOT_PRESENT)
630 continue;
631
632 if (SAS_ADDR(phy->attached_sas_addr) ==
633 SAS_ADDR(child->sas_addr)) {
634
635 child->min_linkrate = min(parent->min_linkrate,
636 phy->linkrate);
637 child->max_linkrate = max(parent->max_linkrate,
638 phy->linkrate);
639 child->pathways++;
640 sas_port_add_phy(port, phy->phy);
641 }
642 }
643 child->linkrate = min(parent_phy->linkrate, child->max_linkrate);
644 child->pathways = min(child->pathways, parent->pathways);
645}
646
647static struct domain_device *sas_ex_discover_end_dev(
648 struct domain_device *parent, int phy_id)
649{
650 struct expander_device *parent_ex = &parent->ex_dev;
651 struct ex_phy *phy = &parent_ex->ex_phy[phy_id];
652 struct domain_device *child = NULL;
653 struct sas_rphy *rphy;
654 int res;
655
656 if (phy->attached_sata_host || phy->attached_sata_ps)
657 return NULL;
658
659 child = kzalloc(sizeof(*child), GFP_KERNEL);
660 if (!child)
661 return NULL;
662
663 child->parent = parent;
664 child->port = parent->port;
665 child->iproto = phy->attached_iproto;
666 memcpy(child->sas_addr, phy->attached_sas_addr, SAS_ADDR_SIZE);
667 sas_hash_addr(child->hashed_sas_addr, child->sas_addr);
James Bottomley024879e2006-11-15 18:03:07 -0600668 if (!phy->port) {
669 phy->port = sas_port_alloc(&parent->rphy->dev, phy_id);
670 if (unlikely(!phy->port))
671 goto out_err;
672 if (unlikely(sas_port_add(phy->port) != 0)) {
673 sas_port_free(phy->port);
674 goto out_err;
675 }
676 }
James Bottomley2908d772006-08-29 09:22:51 -0500677 sas_ex_get_linkrate(parent, child, phy);
678
James Bottomleyb9142172007-07-22 13:15:55 -0500679#ifdef CONFIG_SCSI_SAS_ATA
Darrick J. Wong5929faf2007-11-05 11:51:17 -0800680 if ((phy->attached_tproto & SAS_PROTOCOL_STP) || phy->attached_sata_dev) {
James Bottomley2908d772006-08-29 09:22:51 -0500681 child->dev_type = SATA_DEV;
Darrick J. Wong5929faf2007-11-05 11:51:17 -0800682 if (phy->attached_tproto & SAS_PROTOCOL_STP)
James Bottomley2908d772006-08-29 09:22:51 -0500683 child->tproto = phy->attached_tproto;
684 if (phy->attached_sata_dev)
685 child->tproto |= SATA_DEV;
686 res = sas_get_report_phy_sata(parent, phy_id,
687 &child->sata_dev.rps_resp);
688 if (res) {
689 SAS_DPRINTK("report phy sata to %016llx:0x%x returned "
690 "0x%x\n", SAS_ADDR(parent->sas_addr),
691 phy_id, res);
James Bottomley024879e2006-11-15 18:03:07 -0600692 goto out_free;
James Bottomley2908d772006-08-29 09:22:51 -0500693 }
694 memcpy(child->frame_rcvd, &child->sata_dev.rps_resp.rps.fis,
695 sizeof(struct dev_to_host_fis));
James Bottomley1acce192006-08-22 12:39:19 -0500696
697 rphy = sas_end_device_alloc(phy->port);
James Bottomley528fd552006-10-16 10:57:05 -0500698 if (unlikely(!rphy))
699 goto out_free;
James Bottomley1acce192006-08-22 12:39:19 -0500700
James Bottomley2908d772006-08-29 09:22:51 -0500701 sas_init_dev(child);
James Bottomley1acce192006-08-22 12:39:19 -0500702
703 child->rphy = rphy;
704
James Bottomley9d720d82007-07-16 13:15:51 -0500705 spin_lock_irq(&parent->port->dev_list_lock);
James Bottomley1acce192006-08-22 12:39:19 -0500706 list_add_tail(&child->dev_list_node, &parent->port->dev_list);
James Bottomley9d720d82007-07-16 13:15:51 -0500707 spin_unlock_irq(&parent->port->dev_list_lock);
James Bottomley1acce192006-08-22 12:39:19 -0500708
James Bottomley2908d772006-08-29 09:22:51 -0500709 res = sas_discover_sata(child);
710 if (res) {
711 SAS_DPRINTK("sas_discover_sata() for device %16llx at "
712 "%016llx:0x%x returned 0x%x\n",
713 SAS_ADDR(child->sas_addr),
714 SAS_ADDR(parent->sas_addr), phy_id, res);
James Bottomley1acce192006-08-22 12:39:19 -0500715 goto out_list_del;
James Bottomley2908d772006-08-29 09:22:51 -0500716 }
James Bottomleyb9142172007-07-22 13:15:55 -0500717 } else
718#endif
Darrick J. Wong5929faf2007-11-05 11:51:17 -0800719 if (phy->attached_tproto & SAS_PROTOCOL_SSP) {
James Bottomley2908d772006-08-29 09:22:51 -0500720 child->dev_type = SAS_END_DEV;
721 rphy = sas_end_device_alloc(phy->port);
722 /* FIXME: error handling */
James Bottomley024879e2006-11-15 18:03:07 -0600723 if (unlikely(!rphy))
724 goto out_free;
James Bottomley2908d772006-08-29 09:22:51 -0500725 child->tproto = phy->attached_tproto;
726 sas_init_dev(child);
727
728 child->rphy = rphy;
729 sas_fill_in_rphy(child, rphy);
730
James Bottomley9d720d82007-07-16 13:15:51 -0500731 spin_lock_irq(&parent->port->dev_list_lock);
James Bottomley2908d772006-08-29 09:22:51 -0500732 list_add_tail(&child->dev_list_node, &parent->port->dev_list);
James Bottomley9d720d82007-07-16 13:15:51 -0500733 spin_unlock_irq(&parent->port->dev_list_lock);
James Bottomley2908d772006-08-29 09:22:51 -0500734
735 res = sas_discover_end_dev(child);
736 if (res) {
737 SAS_DPRINTK("sas_discover_end_dev() for device %16llx "
738 "at %016llx:0x%x returned 0x%x\n",
739 SAS_ADDR(child->sas_addr),
740 SAS_ADDR(parent->sas_addr), phy_id, res);
James Bottomley024879e2006-11-15 18:03:07 -0600741 goto out_list_del;
James Bottomley2908d772006-08-29 09:22:51 -0500742 }
743 } else {
744 SAS_DPRINTK("target proto 0x%x at %016llx:0x%x not handled\n",
745 phy->attached_tproto, SAS_ADDR(parent->sas_addr),
746 phy_id);
James Bottomleyb9142172007-07-22 13:15:55 -0500747 goto out_free;
James Bottomley2908d772006-08-29 09:22:51 -0500748 }
749
750 list_add_tail(&child->siblings, &parent_ex->children);
751 return child;
James Bottomley024879e2006-11-15 18:03:07 -0600752
753 out_list_del:
Darrick J. Wong6f63caa2007-01-26 14:08:43 -0800754 sas_rphy_free(child->rphy);
755 child->rphy = NULL;
James Bottomley024879e2006-11-15 18:03:07 -0600756 list_del(&child->dev_list_node);
James Bottomley024879e2006-11-15 18:03:07 -0600757 out_free:
758 sas_port_delete(phy->port);
759 out_err:
760 phy->port = NULL;
761 kfree(child);
762 return NULL;
James Bottomley2908d772006-08-29 09:22:51 -0500763}
764
Darrick J. Wong423f7cf2007-01-30 12:07:27 -0800765/* See if this phy is part of a wide port */
766static int sas_ex_join_wide_port(struct domain_device *parent, int phy_id)
767{
768 struct ex_phy *phy = &parent->ex_dev.ex_phy[phy_id];
769 int i;
770
771 for (i = 0; i < parent->ex_dev.num_phys; i++) {
772 struct ex_phy *ephy = &parent->ex_dev.ex_phy[i];
773
774 if (ephy == phy)
775 continue;
776
777 if (!memcmp(phy->attached_sas_addr, ephy->attached_sas_addr,
778 SAS_ADDR_SIZE) && ephy->port) {
779 sas_port_add_phy(ephy->port, phy->phy);
Tom Peng19252de2009-07-17 16:02:04 +0800780 phy->port = ephy->port;
Darrick J. Wong423f7cf2007-01-30 12:07:27 -0800781 phy->phy_state = PHY_DEVICE_DISCOVERED;
782 return 0;
783 }
784 }
785
786 return -ENODEV;
787}
788
James Bottomley2908d772006-08-29 09:22:51 -0500789static struct domain_device *sas_ex_discover_expander(
790 struct domain_device *parent, int phy_id)
791{
792 struct sas_expander_device *parent_ex = rphy_to_expander_device(parent->rphy);
793 struct ex_phy *phy = &parent->ex_dev.ex_phy[phy_id];
794 struct domain_device *child = NULL;
795 struct sas_rphy *rphy;
796 struct sas_expander_device *edev;
797 struct asd_sas_port *port;
798 int res;
799
800 if (phy->routing_attr == DIRECT_ROUTING) {
801 SAS_DPRINTK("ex %016llx:0x%x:D <--> ex %016llx:0x%x is not "
802 "allowed\n",
803 SAS_ADDR(parent->sas_addr), phy_id,
804 SAS_ADDR(phy->attached_sas_addr),
805 phy->attached_phy_id);
806 return NULL;
807 }
808 child = kzalloc(sizeof(*child), GFP_KERNEL);
809 if (!child)
810 return NULL;
811
812 phy->port = sas_port_alloc(&parent->rphy->dev, phy_id);
813 /* FIXME: better error handling */
814 BUG_ON(sas_port_add(phy->port) != 0);
815
816
817 switch (phy->attached_dev_type) {
818 case EDGE_DEV:
819 rphy = sas_expander_alloc(phy->port,
820 SAS_EDGE_EXPANDER_DEVICE);
821 break;
822 case FANOUT_DEV:
823 rphy = sas_expander_alloc(phy->port,
824 SAS_FANOUT_EXPANDER_DEVICE);
825 break;
826 default:
827 rphy = NULL; /* shut gcc up */
828 BUG();
829 }
830 port = parent->port;
831 child->rphy = rphy;
832 edev = rphy_to_expander_device(rphy);
833 child->dev_type = phy->attached_dev_type;
834 child->parent = parent;
835 child->port = port;
836 child->iproto = phy->attached_iproto;
837 child->tproto = phy->attached_tproto;
838 memcpy(child->sas_addr, phy->attached_sas_addr, SAS_ADDR_SIZE);
839 sas_hash_addr(child->hashed_sas_addr, child->sas_addr);
840 sas_ex_get_linkrate(parent, child, phy);
841 edev->level = parent_ex->level + 1;
842 parent->port->disc.max_level = max(parent->port->disc.max_level,
843 edev->level);
844 sas_init_dev(child);
845 sas_fill_in_rphy(child, rphy);
846 sas_rphy_add(rphy);
847
James Bottomley9d720d82007-07-16 13:15:51 -0500848 spin_lock_irq(&parent->port->dev_list_lock);
James Bottomley2908d772006-08-29 09:22:51 -0500849 list_add_tail(&child->dev_list_node, &parent->port->dev_list);
James Bottomley9d720d82007-07-16 13:15:51 -0500850 spin_unlock_irq(&parent->port->dev_list_lock);
James Bottomley2908d772006-08-29 09:22:51 -0500851
852 res = sas_discover_expander(child);
853 if (res) {
Luben Tuikovec16ea52011-07-26 23:10:48 -0700854 spin_lock_irq(&parent->port->dev_list_lock);
855 list_del(&child->dev_list_node);
856 spin_unlock_irq(&parent->port->dev_list_lock);
James Bottomley2908d772006-08-29 09:22:51 -0500857 kfree(child);
858 return NULL;
859 }
860 list_add_tail(&child->siblings, &parent->ex_dev.children);
861 return child;
862}
863
864static int sas_ex_discover_dev(struct domain_device *dev, int phy_id)
865{
866 struct expander_device *ex = &dev->ex_dev;
867 struct ex_phy *ex_phy = &ex->ex_phy[phy_id];
868 struct domain_device *child = NULL;
869 int res = 0;
870
871 /* Phy state */
James Bottomley88edf742006-09-06 17:36:13 -0500872 if (ex_phy->linkrate == SAS_SATA_SPINUP_HOLD) {
James Bottomleya01e70e2006-09-06 19:28:07 -0500873 if (!sas_smp_phy_control(dev, phy_id, PHY_FUNC_LINK_RESET, NULL))
James Bottomley2908d772006-08-29 09:22:51 -0500874 res = sas_ex_phy_discover(dev, phy_id);
875 if (res)
876 return res;
877 }
878
879 /* Parent and domain coherency */
880 if (!dev->parent && (SAS_ADDR(ex_phy->attached_sas_addr) ==
881 SAS_ADDR(dev->port->sas_addr))) {
882 sas_add_parent_port(dev, phy_id);
883 return 0;
884 }
885 if (dev->parent && (SAS_ADDR(ex_phy->attached_sas_addr) ==
886 SAS_ADDR(dev->parent->sas_addr))) {
887 sas_add_parent_port(dev, phy_id);
888 if (ex_phy->routing_attr == TABLE_ROUTING)
889 sas_configure_phy(dev, phy_id, dev->port->sas_addr, 1);
890 return 0;
891 }
892
893 if (sas_dev_present_in_domain(dev->port, ex_phy->attached_sas_addr))
894 sas_ex_disable_port(dev, ex_phy->attached_sas_addr);
895
896 if (ex_phy->attached_dev_type == NO_DEVICE) {
897 if (ex_phy->routing_attr == DIRECT_ROUTING) {
898 memset(ex_phy->attached_sas_addr, 0, SAS_ADDR_SIZE);
899 sas_configure_routing(dev, ex_phy->attached_sas_addr);
900 }
901 return 0;
James Bottomley88edf742006-09-06 17:36:13 -0500902 } else if (ex_phy->linkrate == SAS_LINK_RATE_UNKNOWN)
James Bottomley2908d772006-08-29 09:22:51 -0500903 return 0;
904
905 if (ex_phy->attached_dev_type != SAS_END_DEV &&
906 ex_phy->attached_dev_type != FANOUT_DEV &&
907 ex_phy->attached_dev_type != EDGE_DEV) {
908 SAS_DPRINTK("unknown device type(0x%x) attached to ex %016llx "
909 "phy 0x%x\n", ex_phy->attached_dev_type,
910 SAS_ADDR(dev->sas_addr),
911 phy_id);
912 return 0;
913 }
914
915 res = sas_configure_routing(dev, ex_phy->attached_sas_addr);
916 if (res) {
917 SAS_DPRINTK("configure routing for dev %016llx "
918 "reported 0x%x. Forgotten\n",
919 SAS_ADDR(ex_phy->attached_sas_addr), res);
920 sas_disable_routing(dev, ex_phy->attached_sas_addr);
921 return res;
922 }
923
Darrick J. Wong423f7cf2007-01-30 12:07:27 -0800924 res = sas_ex_join_wide_port(dev, phy_id);
925 if (!res) {
926 SAS_DPRINTK("Attaching ex phy%d to wide port %016llx\n",
927 phy_id, SAS_ADDR(ex_phy->attached_sas_addr));
928 return res;
929 }
930
James Bottomley2908d772006-08-29 09:22:51 -0500931 switch (ex_phy->attached_dev_type) {
932 case SAS_END_DEV:
933 child = sas_ex_discover_end_dev(dev, phy_id);
934 break;
935 case FANOUT_DEV:
936 if (SAS_ADDR(dev->port->disc.fanout_sas_addr)) {
937 SAS_DPRINTK("second fanout expander %016llx phy 0x%x "
938 "attached to ex %016llx phy 0x%x\n",
939 SAS_ADDR(ex_phy->attached_sas_addr),
940 ex_phy->attached_phy_id,
941 SAS_ADDR(dev->sas_addr),
942 phy_id);
943 sas_ex_disable_phy(dev, phy_id);
944 break;
945 } else
946 memcpy(dev->port->disc.fanout_sas_addr,
947 ex_phy->attached_sas_addr, SAS_ADDR_SIZE);
948 /* fallthrough */
949 case EDGE_DEV:
950 child = sas_ex_discover_expander(dev, phy_id);
951 break;
952 default:
953 break;
954 }
955
956 if (child) {
957 int i;
958
959 for (i = 0; i < ex->num_phys; i++) {
960 if (ex->ex_phy[i].phy_state == PHY_VACANT ||
961 ex->ex_phy[i].phy_state == PHY_NOT_PRESENT)
962 continue;
Tom Peng19252de2009-07-17 16:02:04 +0800963 /*
964 * Due to races, the phy might not get added to the
965 * wide port, so we add the phy to the wide port here.
966 */
James Bottomley2908d772006-08-29 09:22:51 -0500967 if (SAS_ADDR(ex->ex_phy[i].attached_sas_addr) ==
Tom Peng19252de2009-07-17 16:02:04 +0800968 SAS_ADDR(child->sas_addr)) {
James Bottomley2908d772006-08-29 09:22:51 -0500969 ex->ex_phy[i].phy_state= PHY_DEVICE_DISCOVERED;
Tom Peng19252de2009-07-17 16:02:04 +0800970 res = sas_ex_join_wide_port(dev, i);
971 if (!res)
972 SAS_DPRINTK("Attaching ex phy%d to wide port %016llx\n",
973 i, SAS_ADDR(ex->ex_phy[i].attached_sas_addr));
974
975 }
James Bottomley2908d772006-08-29 09:22:51 -0500976 }
977 }
978
979 return res;
980}
981
982static int sas_find_sub_addr(struct domain_device *dev, u8 *sub_addr)
983{
984 struct expander_device *ex = &dev->ex_dev;
985 int i;
986
987 for (i = 0; i < ex->num_phys; i++) {
988 struct ex_phy *phy = &ex->ex_phy[i];
989
990 if (phy->phy_state == PHY_VACANT ||
991 phy->phy_state == PHY_NOT_PRESENT)
992 continue;
993
994 if ((phy->attached_dev_type == EDGE_DEV ||
995 phy->attached_dev_type == FANOUT_DEV) &&
996 phy->routing_attr == SUBTRACTIVE_ROUTING) {
997
998 memcpy(sub_addr, phy->attached_sas_addr,SAS_ADDR_SIZE);
999
1000 return 1;
1001 }
1002 }
1003 return 0;
1004}
1005
1006static int sas_check_level_subtractive_boundary(struct domain_device *dev)
1007{
1008 struct expander_device *ex = &dev->ex_dev;
1009 struct domain_device *child;
1010 u8 sub_addr[8] = {0, };
1011
1012 list_for_each_entry(child, &ex->children, siblings) {
1013 if (child->dev_type != EDGE_DEV &&
1014 child->dev_type != FANOUT_DEV)
1015 continue;
1016 if (sub_addr[0] == 0) {
1017 sas_find_sub_addr(child, sub_addr);
1018 continue;
1019 } else {
1020 u8 s2[8];
1021
1022 if (sas_find_sub_addr(child, s2) &&
1023 (SAS_ADDR(sub_addr) != SAS_ADDR(s2))) {
1024
1025 SAS_DPRINTK("ex %016llx->%016llx-?->%016llx "
1026 "diverges from subtractive "
1027 "boundary %016llx\n",
1028 SAS_ADDR(dev->sas_addr),
1029 SAS_ADDR(child->sas_addr),
1030 SAS_ADDR(s2),
1031 SAS_ADDR(sub_addr));
1032
1033 sas_ex_disable_port(child, s2);
1034 }
1035 }
1036 }
1037 return 0;
1038}
1039/**
1040 * sas_ex_discover_devices -- discover devices attached to this expander
1041 * dev: pointer to the expander domain device
1042 * single: if you want to do a single phy, else set to -1;
1043 *
1044 * Configure this expander for use with its devices and register the
1045 * devices of this expander.
1046 */
1047static int sas_ex_discover_devices(struct domain_device *dev, int single)
1048{
1049 struct expander_device *ex = &dev->ex_dev;
1050 int i = 0, end = ex->num_phys;
1051 int res = 0;
1052
1053 if (0 <= single && single < end) {
1054 i = single;
1055 end = i+1;
1056 }
1057
1058 for ( ; i < end; i++) {
1059 struct ex_phy *ex_phy = &ex->ex_phy[i];
1060
1061 if (ex_phy->phy_state == PHY_VACANT ||
1062 ex_phy->phy_state == PHY_NOT_PRESENT ||
1063 ex_phy->phy_state == PHY_DEVICE_DISCOVERED)
1064 continue;
1065
1066 switch (ex_phy->linkrate) {
James Bottomley88edf742006-09-06 17:36:13 -05001067 case SAS_PHY_DISABLED:
1068 case SAS_PHY_RESET_PROBLEM:
1069 case SAS_SATA_PORT_SELECTOR:
James Bottomley2908d772006-08-29 09:22:51 -05001070 continue;
1071 default:
1072 res = sas_ex_discover_dev(dev, i);
1073 if (res)
1074 break;
1075 continue;
1076 }
1077 }
1078
1079 if (!res)
1080 sas_check_level_subtractive_boundary(dev);
1081
1082 return res;
1083}
1084
1085static int sas_check_ex_subtractive_boundary(struct domain_device *dev)
1086{
1087 struct expander_device *ex = &dev->ex_dev;
1088 int i;
1089 u8 *sub_sas_addr = NULL;
1090
1091 if (dev->dev_type != EDGE_DEV)
1092 return 0;
1093
1094 for (i = 0; i < ex->num_phys; i++) {
1095 struct ex_phy *phy = &ex->ex_phy[i];
1096
1097 if (phy->phy_state == PHY_VACANT ||
1098 phy->phy_state == PHY_NOT_PRESENT)
1099 continue;
1100
1101 if ((phy->attached_dev_type == FANOUT_DEV ||
1102 phy->attached_dev_type == EDGE_DEV) &&
1103 phy->routing_attr == SUBTRACTIVE_ROUTING) {
1104
1105 if (!sub_sas_addr)
1106 sub_sas_addr = &phy->attached_sas_addr[0];
1107 else if (SAS_ADDR(sub_sas_addr) !=
1108 SAS_ADDR(phy->attached_sas_addr)) {
1109
1110 SAS_DPRINTK("ex %016llx phy 0x%x "
1111 "diverges(%016llx) on subtractive "
1112 "boundary(%016llx). Disabled\n",
1113 SAS_ADDR(dev->sas_addr), i,
1114 SAS_ADDR(phy->attached_sas_addr),
1115 SAS_ADDR(sub_sas_addr));
1116 sas_ex_disable_phy(dev, i);
1117 }
1118 }
1119 }
1120 return 0;
1121}
1122
1123static void sas_print_parent_topology_bug(struct domain_device *child,
1124 struct ex_phy *parent_phy,
1125 struct ex_phy *child_phy)
1126{
1127 static const char ra_char[] = {
1128 [DIRECT_ROUTING] = 'D',
1129 [SUBTRACTIVE_ROUTING] = 'S',
1130 [TABLE_ROUTING] = 'T',
1131 };
1132 static const char *ex_type[] = {
1133 [EDGE_DEV] = "edge",
1134 [FANOUT_DEV] = "fanout",
1135 };
1136 struct domain_device *parent = child->parent;
1137
1138 sas_printk("%s ex %016llx phy 0x%x <--> %s ex %016llx phy 0x%x "
1139 "has %c:%c routing link!\n",
1140
1141 ex_type[parent->dev_type],
1142 SAS_ADDR(parent->sas_addr),
1143 parent_phy->phy_id,
1144
1145 ex_type[child->dev_type],
1146 SAS_ADDR(child->sas_addr),
1147 child_phy->phy_id,
1148
1149 ra_char[parent_phy->routing_attr],
1150 ra_char[child_phy->routing_attr]);
1151}
1152
1153static int sas_check_eeds(struct domain_device *child,
1154 struct ex_phy *parent_phy,
1155 struct ex_phy *child_phy)
1156{
1157 int res = 0;
1158 struct domain_device *parent = child->parent;
1159
1160 if (SAS_ADDR(parent->port->disc.fanout_sas_addr) != 0) {
1161 res = -ENODEV;
1162 SAS_DPRINTK("edge ex %016llx phy S:0x%x <--> edge ex %016llx "
1163 "phy S:0x%x, while there is a fanout ex %016llx\n",
1164 SAS_ADDR(parent->sas_addr),
1165 parent_phy->phy_id,
1166 SAS_ADDR(child->sas_addr),
1167 child_phy->phy_id,
1168 SAS_ADDR(parent->port->disc.fanout_sas_addr));
1169 } else if (SAS_ADDR(parent->port->disc.eeds_a) == 0) {
1170 memcpy(parent->port->disc.eeds_a, parent->sas_addr,
1171 SAS_ADDR_SIZE);
1172 memcpy(parent->port->disc.eeds_b, child->sas_addr,
1173 SAS_ADDR_SIZE);
1174 } else if (((SAS_ADDR(parent->port->disc.eeds_a) ==
1175 SAS_ADDR(parent->sas_addr)) ||
1176 (SAS_ADDR(parent->port->disc.eeds_a) ==
1177 SAS_ADDR(child->sas_addr)))
1178 &&
1179 ((SAS_ADDR(parent->port->disc.eeds_b) ==
1180 SAS_ADDR(parent->sas_addr)) ||
1181 (SAS_ADDR(parent->port->disc.eeds_b) ==
1182 SAS_ADDR(child->sas_addr))))
1183 ;
1184 else {
1185 res = -ENODEV;
1186 SAS_DPRINTK("edge ex %016llx phy 0x%x <--> edge ex %016llx "
1187 "phy 0x%x link forms a third EEDS!\n",
1188 SAS_ADDR(parent->sas_addr),
1189 parent_phy->phy_id,
1190 SAS_ADDR(child->sas_addr),
1191 child_phy->phy_id);
1192 }
1193
1194 return res;
1195}
1196
1197/* Here we spill over 80 columns. It is intentional.
1198 */
1199static int sas_check_parent_topology(struct domain_device *child)
1200{
1201 struct expander_device *child_ex = &child->ex_dev;
1202 struct expander_device *parent_ex;
1203 int i;
1204 int res = 0;
1205
1206 if (!child->parent)
1207 return 0;
1208
1209 if (child->parent->dev_type != EDGE_DEV &&
1210 child->parent->dev_type != FANOUT_DEV)
1211 return 0;
1212
1213 parent_ex = &child->parent->ex_dev;
1214
1215 for (i = 0; i < parent_ex->num_phys; i++) {
1216 struct ex_phy *parent_phy = &parent_ex->ex_phy[i];
1217 struct ex_phy *child_phy;
1218
1219 if (parent_phy->phy_state == PHY_VACANT ||
1220 parent_phy->phy_state == PHY_NOT_PRESENT)
1221 continue;
1222
1223 if (SAS_ADDR(parent_phy->attached_sas_addr) != SAS_ADDR(child->sas_addr))
1224 continue;
1225
1226 child_phy = &child_ex->ex_phy[parent_phy->attached_phy_id];
1227
1228 switch (child->parent->dev_type) {
1229 case EDGE_DEV:
1230 if (child->dev_type == FANOUT_DEV) {
1231 if (parent_phy->routing_attr != SUBTRACTIVE_ROUTING ||
1232 child_phy->routing_attr != TABLE_ROUTING) {
1233 sas_print_parent_topology_bug(child, parent_phy, child_phy);
1234 res = -ENODEV;
1235 }
1236 } else if (parent_phy->routing_attr == SUBTRACTIVE_ROUTING) {
1237 if (child_phy->routing_attr == SUBTRACTIVE_ROUTING) {
1238 res = sas_check_eeds(child, parent_phy, child_phy);
1239 } else if (child_phy->routing_attr != TABLE_ROUTING) {
1240 sas_print_parent_topology_bug(child, parent_phy, child_phy);
1241 res = -ENODEV;
1242 }
1243 } else if (parent_phy->routing_attr == TABLE_ROUTING &&
1244 child_phy->routing_attr != SUBTRACTIVE_ROUTING) {
1245 sas_print_parent_topology_bug(child, parent_phy, child_phy);
1246 res = -ENODEV;
1247 }
1248 break;
1249 case FANOUT_DEV:
1250 if (parent_phy->routing_attr != TABLE_ROUTING ||
1251 child_phy->routing_attr != SUBTRACTIVE_ROUTING) {
1252 sas_print_parent_topology_bug(child, parent_phy, child_phy);
1253 res = -ENODEV;
1254 }
1255 break;
1256 default:
1257 break;
1258 }
1259 }
1260
1261 return res;
1262}
1263
1264#define RRI_REQ_SIZE 16
1265#define RRI_RESP_SIZE 44
1266
1267static int sas_configure_present(struct domain_device *dev, int phy_id,
1268 u8 *sas_addr, int *index, int *present)
1269{
1270 int i, res = 0;
1271 struct expander_device *ex = &dev->ex_dev;
1272 struct ex_phy *phy = &ex->ex_phy[phy_id];
1273 u8 *rri_req;
1274 u8 *rri_resp;
1275
1276 *present = 0;
1277 *index = 0;
1278
1279 rri_req = alloc_smp_req(RRI_REQ_SIZE);
1280 if (!rri_req)
1281 return -ENOMEM;
1282
1283 rri_resp = alloc_smp_resp(RRI_RESP_SIZE);
1284 if (!rri_resp) {
1285 kfree(rri_req);
1286 return -ENOMEM;
1287 }
1288
1289 rri_req[1] = SMP_REPORT_ROUTE_INFO;
1290 rri_req[9] = phy_id;
1291
1292 for (i = 0; i < ex->max_route_indexes ; i++) {
1293 *(__be16 *)(rri_req+6) = cpu_to_be16(i);
1294 res = smp_execute_task(dev, rri_req, RRI_REQ_SIZE, rri_resp,
1295 RRI_RESP_SIZE);
1296 if (res)
1297 goto out;
1298 res = rri_resp[2];
1299 if (res == SMP_RESP_NO_INDEX) {
1300 SAS_DPRINTK("overflow of indexes: dev %016llx "
1301 "phy 0x%x index 0x%x\n",
1302 SAS_ADDR(dev->sas_addr), phy_id, i);
1303 goto out;
1304 } else if (res != SMP_RESP_FUNC_ACC) {
1305 SAS_DPRINTK("%s: dev %016llx phy 0x%x index 0x%x "
Harvey Harrisoncadbd4a2008-07-03 23:47:27 -07001306 "result 0x%x\n", __func__,
James Bottomley2908d772006-08-29 09:22:51 -05001307 SAS_ADDR(dev->sas_addr), phy_id, i, res);
1308 goto out;
1309 }
1310 if (SAS_ADDR(sas_addr) != 0) {
1311 if (SAS_ADDR(rri_resp+16) == SAS_ADDR(sas_addr)) {
1312 *index = i;
1313 if ((rri_resp[12] & 0x80) == 0x80)
1314 *present = 0;
1315 else
1316 *present = 1;
1317 goto out;
1318 } else if (SAS_ADDR(rri_resp+16) == 0) {
1319 *index = i;
1320 *present = 0;
1321 goto out;
1322 }
1323 } else if (SAS_ADDR(rri_resp+16) == 0 &&
1324 phy->last_da_index < i) {
1325 phy->last_da_index = i;
1326 *index = i;
1327 *present = 0;
1328 goto out;
1329 }
1330 }
1331 res = -1;
1332out:
1333 kfree(rri_req);
1334 kfree(rri_resp);
1335 return res;
1336}
1337
1338#define CRI_REQ_SIZE 44
1339#define CRI_RESP_SIZE 8
1340
1341static int sas_configure_set(struct domain_device *dev, int phy_id,
1342 u8 *sas_addr, int index, int include)
1343{
1344 int res;
1345 u8 *cri_req;
1346 u8 *cri_resp;
1347
1348 cri_req = alloc_smp_req(CRI_REQ_SIZE);
1349 if (!cri_req)
1350 return -ENOMEM;
1351
1352 cri_resp = alloc_smp_resp(CRI_RESP_SIZE);
1353 if (!cri_resp) {
1354 kfree(cri_req);
1355 return -ENOMEM;
1356 }
1357
1358 cri_req[1] = SMP_CONF_ROUTE_INFO;
1359 *(__be16 *)(cri_req+6) = cpu_to_be16(index);
1360 cri_req[9] = phy_id;
1361 if (SAS_ADDR(sas_addr) == 0 || !include)
1362 cri_req[12] |= 0x80;
1363 memcpy(cri_req+16, sas_addr, SAS_ADDR_SIZE);
1364
1365 res = smp_execute_task(dev, cri_req, CRI_REQ_SIZE, cri_resp,
1366 CRI_RESP_SIZE);
1367 if (res)
1368 goto out;
1369 res = cri_resp[2];
1370 if (res == SMP_RESP_NO_INDEX) {
1371 SAS_DPRINTK("overflow of indexes: dev %016llx phy 0x%x "
1372 "index 0x%x\n",
1373 SAS_ADDR(dev->sas_addr), phy_id, index);
1374 }
1375out:
1376 kfree(cri_req);
1377 kfree(cri_resp);
1378 return res;
1379}
1380
1381static int sas_configure_phy(struct domain_device *dev, int phy_id,
1382 u8 *sas_addr, int include)
1383{
1384 int index;
1385 int present;
1386 int res;
1387
1388 res = sas_configure_present(dev, phy_id, sas_addr, &index, &present);
1389 if (res)
1390 return res;
1391 if (include ^ present)
1392 return sas_configure_set(dev, phy_id, sas_addr, index,include);
1393
1394 return res;
1395}
1396
1397/**
1398 * sas_configure_parent -- configure routing table of parent
1399 * parent: parent expander
1400 * child: child expander
1401 * sas_addr: SAS port identifier of device directly attached to child
1402 */
1403static int sas_configure_parent(struct domain_device *parent,
1404 struct domain_device *child,
1405 u8 *sas_addr, int include)
1406{
1407 struct expander_device *ex_parent = &parent->ex_dev;
1408 int res = 0;
1409 int i;
1410
1411 if (parent->parent) {
1412 res = sas_configure_parent(parent->parent, parent, sas_addr,
1413 include);
1414 if (res)
1415 return res;
1416 }
1417
1418 if (ex_parent->conf_route_table == 0) {
1419 SAS_DPRINTK("ex %016llx has self-configuring routing table\n",
1420 SAS_ADDR(parent->sas_addr));
1421 return 0;
1422 }
1423
1424 for (i = 0; i < ex_parent->num_phys; i++) {
1425 struct ex_phy *phy = &ex_parent->ex_phy[i];
1426
1427 if ((phy->routing_attr == TABLE_ROUTING) &&
1428 (SAS_ADDR(phy->attached_sas_addr) ==
1429 SAS_ADDR(child->sas_addr))) {
1430 res = sas_configure_phy(parent, i, sas_addr, include);
1431 if (res)
1432 return res;
1433 }
1434 }
1435
1436 return res;
1437}
1438
1439/**
1440 * sas_configure_routing -- configure routing
1441 * dev: expander device
1442 * sas_addr: port identifier of device directly attached to the expander device
1443 */
1444static int sas_configure_routing(struct domain_device *dev, u8 *sas_addr)
1445{
1446 if (dev->parent)
1447 return sas_configure_parent(dev->parent, dev, sas_addr, 1);
1448 return 0;
1449}
1450
1451static int sas_disable_routing(struct domain_device *dev, u8 *sas_addr)
1452{
1453 if (dev->parent)
1454 return sas_configure_parent(dev->parent, dev, sas_addr, 0);
1455 return 0;
1456}
1457
James Bottomley2908d772006-08-29 09:22:51 -05001458/**
1459 * sas_discover_expander -- expander discovery
1460 * @ex: pointer to expander domain device
1461 *
1462 * See comment in sas_discover_sata().
1463 */
1464static int sas_discover_expander(struct domain_device *dev)
1465{
1466 int res;
1467
1468 res = sas_notify_lldd_dev_found(dev);
1469 if (res)
1470 return res;
1471
1472 res = sas_ex_general(dev);
1473 if (res)
1474 goto out_err;
1475 res = sas_ex_manuf_info(dev);
1476 if (res)
1477 goto out_err;
1478
1479 res = sas_expander_discover(dev);
1480 if (res) {
1481 SAS_DPRINTK("expander %016llx discovery failed(0x%x)\n",
1482 SAS_ADDR(dev->sas_addr), res);
1483 goto out_err;
1484 }
1485
1486 sas_check_ex_subtractive_boundary(dev);
1487 res = sas_check_parent_topology(dev);
1488 if (res)
1489 goto out_err;
1490 return 0;
1491out_err:
1492 sas_notify_lldd_dev_gone(dev);
1493 return res;
1494}
1495
1496static int sas_ex_level_discovery(struct asd_sas_port *port, const int level)
1497{
1498 int res = 0;
1499 struct domain_device *dev;
1500
1501 list_for_each_entry(dev, &port->dev_list, dev_list_node) {
1502 if (dev->dev_type == EDGE_DEV ||
1503 dev->dev_type == FANOUT_DEV) {
1504 struct sas_expander_device *ex =
1505 rphy_to_expander_device(dev->rphy);
1506
1507 if (level == ex->level)
1508 res = sas_ex_discover_devices(dev, -1);
1509 else if (level > 0)
1510 res = sas_ex_discover_devices(port->port_dev, -1);
1511
1512 }
1513 }
1514
1515 return res;
1516}
1517
1518static int sas_ex_bfs_disc(struct asd_sas_port *port)
1519{
1520 int res;
1521 int level;
1522
1523 do {
1524 level = port->disc.max_level;
1525 res = sas_ex_level_discovery(port, level);
1526 mb();
1527 } while (level < port->disc.max_level);
1528
1529 return res;
1530}
1531
1532int sas_discover_root_expander(struct domain_device *dev)
1533{
1534 int res;
1535 struct sas_expander_device *ex = rphy_to_expander_device(dev->rphy);
1536
Darrick J. Wongbf451202007-01-11 14:14:52 -08001537 res = sas_rphy_add(dev->rphy);
1538 if (res)
1539 goto out_err;
James Bottomley2908d772006-08-29 09:22:51 -05001540
1541 ex->level = dev->port->disc.max_level; /* 0 */
1542 res = sas_discover_expander(dev);
Darrick J. Wongbf451202007-01-11 14:14:52 -08001543 if (res)
1544 goto out_err2;
James Bottomley2908d772006-08-29 09:22:51 -05001545
Darrick J. Wongbf451202007-01-11 14:14:52 -08001546 sas_ex_bfs_disc(dev->port);
1547
1548 return res;
1549
1550out_err2:
Darrick J. Wong6f63caa2007-01-26 14:08:43 -08001551 sas_rphy_remove(dev->rphy);
Darrick J. Wongbf451202007-01-11 14:14:52 -08001552out_err:
James Bottomley2908d772006-08-29 09:22:51 -05001553 return res;
1554}
1555
1556/* ---------- Domain revalidation ---------- */
1557
1558static int sas_get_phy_discover(struct domain_device *dev,
1559 int phy_id, struct smp_resp *disc_resp)
1560{
1561 int res;
1562 u8 *disc_req;
1563
1564 disc_req = alloc_smp_req(DISCOVER_REQ_SIZE);
1565 if (!disc_req)
1566 return -ENOMEM;
1567
1568 disc_req[1] = SMP_DISCOVER;
1569 disc_req[9] = phy_id;
1570
1571 res = smp_execute_task(dev, disc_req, DISCOVER_REQ_SIZE,
1572 disc_resp, DISCOVER_RESP_SIZE);
1573 if (res)
1574 goto out;
1575 else if (disc_resp->result != SMP_RESP_FUNC_ACC) {
1576 res = disc_resp->result;
1577 goto out;
1578 }
1579out:
1580 kfree(disc_req);
1581 return res;
1582}
1583
1584static int sas_get_phy_change_count(struct domain_device *dev,
1585 int phy_id, int *pcc)
1586{
1587 int res;
1588 struct smp_resp *disc_resp;
1589
1590 disc_resp = alloc_smp_resp(DISCOVER_RESP_SIZE);
1591 if (!disc_resp)
1592 return -ENOMEM;
1593
1594 res = sas_get_phy_discover(dev, phy_id, disc_resp);
1595 if (!res)
1596 *pcc = disc_resp->disc.change_count;
1597
1598 kfree(disc_resp);
1599 return res;
1600}
1601
1602static int sas_get_phy_attached_sas_addr(struct domain_device *dev,
1603 int phy_id, u8 *attached_sas_addr)
1604{
1605 int res;
1606 struct smp_resp *disc_resp;
1607 struct discover_resp *dr;
1608
1609 disc_resp = alloc_smp_resp(DISCOVER_RESP_SIZE);
1610 if (!disc_resp)
1611 return -ENOMEM;
1612 dr = &disc_resp->disc;
1613
1614 res = sas_get_phy_discover(dev, phy_id, disc_resp);
1615 if (!res) {
1616 memcpy(attached_sas_addr,disc_resp->disc.attached_sas_addr,8);
1617 if (dr->attached_dev_type == 0)
1618 memset(attached_sas_addr, 0, 8);
1619 }
1620 kfree(disc_resp);
1621 return res;
1622}
1623
1624static int sas_find_bcast_phy(struct domain_device *dev, int *phy_id,
Tom Peng19252de2009-07-17 16:02:04 +08001625 int from_phy, bool update)
James Bottomley2908d772006-08-29 09:22:51 -05001626{
1627 struct expander_device *ex = &dev->ex_dev;
1628 int res = 0;
1629 int i;
1630
1631 for (i = from_phy; i < ex->num_phys; i++) {
1632 int phy_change_count = 0;
1633
1634 res = sas_get_phy_change_count(dev, i, &phy_change_count);
1635 if (res)
1636 goto out;
1637 else if (phy_change_count != ex->ex_phy[i].phy_change_count) {
Tom Peng19252de2009-07-17 16:02:04 +08001638 if (update)
1639 ex->ex_phy[i].phy_change_count =
1640 phy_change_count;
James Bottomley2908d772006-08-29 09:22:51 -05001641 *phy_id = i;
1642 return 0;
1643 }
1644 }
1645out:
1646 return res;
1647}
1648
1649static int sas_get_ex_change_count(struct domain_device *dev, int *ecc)
1650{
1651 int res;
1652 u8 *rg_req;
1653 struct smp_resp *rg_resp;
1654
1655 rg_req = alloc_smp_req(RG_REQ_SIZE);
1656 if (!rg_req)
1657 return -ENOMEM;
1658
1659 rg_resp = alloc_smp_resp(RG_RESP_SIZE);
1660 if (!rg_resp) {
1661 kfree(rg_req);
1662 return -ENOMEM;
1663 }
1664
1665 rg_req[1] = SMP_REPORT_GENERAL;
1666
1667 res = smp_execute_task(dev, rg_req, RG_REQ_SIZE, rg_resp,
1668 RG_RESP_SIZE);
1669 if (res)
1670 goto out;
1671 if (rg_resp->result != SMP_RESP_FUNC_ACC) {
1672 res = rg_resp->result;
1673 goto out;
1674 }
1675
1676 *ecc = be16_to_cpu(rg_resp->rg.change_count);
1677out:
1678 kfree(rg_resp);
1679 kfree(rg_req);
1680 return res;
1681}
Tom Peng19252de2009-07-17 16:02:04 +08001682/**
1683 * sas_find_bcast_dev - find the device issue BROADCAST(CHANGE).
1684 * @dev:domain device to be detect.
1685 * @src_dev: the device which originated BROADCAST(CHANGE).
1686 *
1687 * Add self-configuration expander suport. Suppose two expander cascading,
1688 * when the first level expander is self-configuring, hotplug the disks in
1689 * second level expander, BROADCAST(CHANGE) will not only be originated
1690 * in the second level expander, but also be originated in the first level
1691 * expander (see SAS protocol SAS 2r-14, 7.11 for detail), it is to say,
1692 * expander changed count in two level expanders will all increment at least
1693 * once, but the phy which chang count has changed is the source device which
1694 * we concerned.
1695 */
James Bottomley2908d772006-08-29 09:22:51 -05001696
1697static int sas_find_bcast_dev(struct domain_device *dev,
1698 struct domain_device **src_dev)
1699{
1700 struct expander_device *ex = &dev->ex_dev;
1701 int ex_change_count = -1;
Tom Peng19252de2009-07-17 16:02:04 +08001702 int phy_id = -1;
James Bottomley2908d772006-08-29 09:22:51 -05001703 int res;
Tom Peng19252de2009-07-17 16:02:04 +08001704 struct domain_device *ch;
James Bottomley2908d772006-08-29 09:22:51 -05001705
1706 res = sas_get_ex_change_count(dev, &ex_change_count);
1707 if (res)
1708 goto out;
Tom Peng19252de2009-07-17 16:02:04 +08001709 if (ex_change_count != -1 && ex_change_count != ex->ex_change_count) {
1710 /* Just detect if this expander phys phy change count changed,
1711 * in order to determine if this expander originate BROADCAST,
1712 * and do not update phy change count field in our structure.
1713 */
1714 res = sas_find_bcast_phy(dev, &phy_id, 0, false);
1715 if (phy_id != -1) {
1716 *src_dev = dev;
1717 ex->ex_change_count = ex_change_count;
1718 SAS_DPRINTK("Expander phy change count has changed\n");
1719 return res;
1720 } else
1721 SAS_DPRINTK("Expander phys DID NOT change\n");
1722 }
1723 list_for_each_entry(ch, &ex->children, siblings) {
1724 if (ch->dev_type == EDGE_DEV || ch->dev_type == FANOUT_DEV) {
1725 res = sas_find_bcast_dev(ch, src_dev);
Mark Salyzync4c672c2011-09-01 06:11:17 -07001726 if (*src_dev)
Tom Peng19252de2009-07-17 16:02:04 +08001727 return res;
James Bottomley2908d772006-08-29 09:22:51 -05001728 }
1729 }
1730out:
1731 return res;
1732}
1733
1734static void sas_unregister_ex_tree(struct domain_device *dev)
1735{
1736 struct expander_device *ex = &dev->ex_dev;
1737 struct domain_device *child, *n;
1738
1739 list_for_each_entry_safe(child, n, &ex->children, siblings) {
Darrick J. Wong56dd2c02010-10-01 13:55:47 -07001740 child->gone = 1;
James Bottomley2908d772006-08-29 09:22:51 -05001741 if (child->dev_type == EDGE_DEV ||
1742 child->dev_type == FANOUT_DEV)
1743 sas_unregister_ex_tree(child);
1744 else
1745 sas_unregister_dev(child);
1746 }
1747 sas_unregister_dev(dev);
1748}
1749
1750static void sas_unregister_devs_sas_addr(struct domain_device *parent,
Tom Peng19252de2009-07-17 16:02:04 +08001751 int phy_id, bool last)
James Bottomley2908d772006-08-29 09:22:51 -05001752{
1753 struct expander_device *ex_dev = &parent->ex_dev;
1754 struct ex_phy *phy = &ex_dev->ex_phy[phy_id];
1755 struct domain_device *child, *n;
Tom Peng19252de2009-07-17 16:02:04 +08001756 if (last) {
1757 list_for_each_entry_safe(child, n,
1758 &ex_dev->children, siblings) {
1759 if (SAS_ADDR(child->sas_addr) ==
1760 SAS_ADDR(phy->attached_sas_addr)) {
Darrick J. Wong56dd2c02010-10-01 13:55:47 -07001761 child->gone = 1;
Tom Peng19252de2009-07-17 16:02:04 +08001762 if (child->dev_type == EDGE_DEV ||
1763 child->dev_type == FANOUT_DEV)
1764 sas_unregister_ex_tree(child);
1765 else
1766 sas_unregister_dev(child);
1767 break;
1768 }
James Bottomley2908d772006-08-29 09:22:51 -05001769 }
Darrick J. Wong56dd2c02010-10-01 13:55:47 -07001770 parent->gone = 1;
Tom Peng19252de2009-07-17 16:02:04 +08001771 sas_disable_routing(parent, phy->attached_sas_addr);
James Bottomley2908d772006-08-29 09:22:51 -05001772 }
James Bottomley2908d772006-08-29 09:22:51 -05001773 memset(phy->attached_sas_addr, 0, SAS_ADDR_SIZE);
Mark Salyzyn57fb87f2011-09-22 08:32:23 -07001774 if (phy->port) {
1775 sas_port_delete_phy(phy->port, phy->phy);
1776 if (phy->port->num_phys == 0)
1777 sas_port_delete(phy->port);
1778 phy->port = NULL;
1779 }
James Bottomley2908d772006-08-29 09:22:51 -05001780}
1781
1782static int sas_discover_bfs_by_root_level(struct domain_device *root,
1783 const int level)
1784{
1785 struct expander_device *ex_root = &root->ex_dev;
1786 struct domain_device *child;
1787 int res = 0;
1788
1789 list_for_each_entry(child, &ex_root->children, siblings) {
1790 if (child->dev_type == EDGE_DEV ||
1791 child->dev_type == FANOUT_DEV) {
1792 struct sas_expander_device *ex =
1793 rphy_to_expander_device(child->rphy);
1794
1795 if (level > ex->level)
1796 res = sas_discover_bfs_by_root_level(child,
1797 level);
1798 else if (level == ex->level)
1799 res = sas_ex_discover_devices(child, -1);
1800 }
1801 }
1802 return res;
1803}
1804
1805static int sas_discover_bfs_by_root(struct domain_device *dev)
1806{
1807 int res;
1808 struct sas_expander_device *ex = rphy_to_expander_device(dev->rphy);
1809 int level = ex->level+1;
1810
1811 res = sas_ex_discover_devices(dev, -1);
1812 if (res)
1813 goto out;
1814 do {
1815 res = sas_discover_bfs_by_root_level(dev, level);
1816 mb();
1817 level += 1;
1818 } while (level <= dev->port->disc.max_level);
1819out:
1820 return res;
1821}
1822
1823static int sas_discover_new(struct domain_device *dev, int phy_id)
1824{
1825 struct ex_phy *ex_phy = &dev->ex_dev.ex_phy[phy_id];
1826 struct domain_device *child;
Tom Peng19252de2009-07-17 16:02:04 +08001827 bool found = false;
1828 int res, i;
James Bottomley2908d772006-08-29 09:22:51 -05001829
1830 SAS_DPRINTK("ex %016llx phy%d new device attached\n",
1831 SAS_ADDR(dev->sas_addr), phy_id);
1832 res = sas_ex_phy_discover(dev, phy_id);
1833 if (res)
1834 goto out;
Tom Peng19252de2009-07-17 16:02:04 +08001835 /* to support the wide port inserted */
1836 for (i = 0; i < dev->ex_dev.num_phys; i++) {
1837 struct ex_phy *ex_phy_temp = &dev->ex_dev.ex_phy[i];
1838 if (i == phy_id)
1839 continue;
1840 if (SAS_ADDR(ex_phy_temp->attached_sas_addr) ==
1841 SAS_ADDR(ex_phy->attached_sas_addr)) {
1842 found = true;
1843 break;
1844 }
1845 }
1846 if (found) {
1847 sas_ex_join_wide_port(dev, phy_id);
1848 return 0;
1849 }
James Bottomley2908d772006-08-29 09:22:51 -05001850 res = sas_ex_discover_devices(dev, phy_id);
Tom Peng19252de2009-07-17 16:02:04 +08001851 if (!res)
James Bottomley2908d772006-08-29 09:22:51 -05001852 goto out;
1853 list_for_each_entry(child, &dev->ex_dev.children, siblings) {
1854 if (SAS_ADDR(child->sas_addr) ==
1855 SAS_ADDR(ex_phy->attached_sas_addr)) {
1856 if (child->dev_type == EDGE_DEV ||
1857 child->dev_type == FANOUT_DEV)
1858 res = sas_discover_bfs_by_root(child);
1859 break;
1860 }
1861 }
1862out:
1863 return res;
1864}
1865
Tom Peng19252de2009-07-17 16:02:04 +08001866static int sas_rediscover_dev(struct domain_device *dev, int phy_id, bool last)
James Bottomley2908d772006-08-29 09:22:51 -05001867{
1868 struct expander_device *ex = &dev->ex_dev;
1869 struct ex_phy *phy = &ex->ex_phy[phy_id];
1870 u8 attached_sas_addr[8];
1871 int res;
1872
1873 res = sas_get_phy_attached_sas_addr(dev, phy_id, attached_sas_addr);
1874 switch (res) {
1875 case SMP_RESP_NO_PHY:
1876 phy->phy_state = PHY_NOT_PRESENT;
Tom Peng19252de2009-07-17 16:02:04 +08001877 sas_unregister_devs_sas_addr(dev, phy_id, last);
James Bottomley2908d772006-08-29 09:22:51 -05001878 goto out; break;
1879 case SMP_RESP_PHY_VACANT:
1880 phy->phy_state = PHY_VACANT;
Tom Peng19252de2009-07-17 16:02:04 +08001881 sas_unregister_devs_sas_addr(dev, phy_id, last);
James Bottomley2908d772006-08-29 09:22:51 -05001882 goto out; break;
1883 case SMP_RESP_FUNC_ACC:
1884 break;
1885 }
1886
1887 if (SAS_ADDR(attached_sas_addr) == 0) {
1888 phy->phy_state = PHY_EMPTY;
Tom Peng19252de2009-07-17 16:02:04 +08001889 sas_unregister_devs_sas_addr(dev, phy_id, last);
James Bottomley2908d772006-08-29 09:22:51 -05001890 } else if (SAS_ADDR(attached_sas_addr) ==
1891 SAS_ADDR(phy->attached_sas_addr)) {
1892 SAS_DPRINTK("ex %016llx phy 0x%x broadcast flutter\n",
1893 SAS_ADDR(dev->sas_addr), phy_id);
James Bottomleya01e70e2006-09-06 19:28:07 -05001894 sas_ex_phy_discover(dev, phy_id);
James Bottomley2908d772006-08-29 09:22:51 -05001895 } else
1896 res = sas_discover_new(dev, phy_id);
1897out:
1898 return res;
1899}
1900
Tom Peng19252de2009-07-17 16:02:04 +08001901/**
1902 * sas_rediscover - revalidate the domain.
1903 * @dev:domain device to be detect.
1904 * @phy_id: the phy id will be detected.
1905 *
1906 * NOTE: this process _must_ quit (return) as soon as any connection
1907 * errors are encountered. Connection recovery is done elsewhere.
1908 * Discover process only interrogates devices in order to discover the
1909 * domain.For plugging out, we un-register the device only when it is
1910 * the last phy in the port, for other phys in this port, we just delete it
1911 * from the port.For inserting, we do discovery when it is the
1912 * first phy,for other phys in this port, we add it to the port to
1913 * forming the wide-port.
1914 */
James Bottomley2908d772006-08-29 09:22:51 -05001915static int sas_rediscover(struct domain_device *dev, const int phy_id)
1916{
1917 struct expander_device *ex = &dev->ex_dev;
1918 struct ex_phy *changed_phy = &ex->ex_phy[phy_id];
1919 int res = 0;
1920 int i;
Tom Peng19252de2009-07-17 16:02:04 +08001921 bool last = true; /* is this the last phy of the port */
James Bottomley2908d772006-08-29 09:22:51 -05001922
1923 SAS_DPRINTK("ex %016llx phy%d originated BROADCAST(CHANGE)\n",
1924 SAS_ADDR(dev->sas_addr), phy_id);
1925
1926 if (SAS_ADDR(changed_phy->attached_sas_addr) != 0) {
1927 for (i = 0; i < ex->num_phys; i++) {
1928 struct ex_phy *phy = &ex->ex_phy[i];
1929
1930 if (i == phy_id)
1931 continue;
1932 if (SAS_ADDR(phy->attached_sas_addr) ==
1933 SAS_ADDR(changed_phy->attached_sas_addr)) {
1934 SAS_DPRINTK("phy%d part of wide port with "
1935 "phy%d\n", phy_id, i);
Tom Peng19252de2009-07-17 16:02:04 +08001936 last = false;
1937 break;
James Bottomley2908d772006-08-29 09:22:51 -05001938 }
1939 }
Tom Peng19252de2009-07-17 16:02:04 +08001940 res = sas_rediscover_dev(dev, phy_id, last);
James Bottomley2908d772006-08-29 09:22:51 -05001941 } else
1942 res = sas_discover_new(dev, phy_id);
James Bottomley2908d772006-08-29 09:22:51 -05001943 return res;
1944}
1945
1946/**
1947 * sas_revalidate_domain -- revalidate the domain
1948 * @port: port to the domain of interest
1949 *
1950 * NOTE: this process _must_ quit (return) as soon as any connection
1951 * errors are encountered. Connection recovery is done elsewhere.
1952 * Discover process only interrogates devices in order to discover the
1953 * domain.
1954 */
1955int sas_ex_revalidate_domain(struct domain_device *port_dev)
1956{
1957 int res;
1958 struct domain_device *dev = NULL;
1959
1960 res = sas_find_bcast_dev(port_dev, &dev);
1961 if (res)
1962 goto out;
1963 if (dev) {
1964 struct expander_device *ex = &dev->ex_dev;
1965 int i = 0, phy_id;
1966
1967 do {
1968 phy_id = -1;
Tom Peng19252de2009-07-17 16:02:04 +08001969 res = sas_find_bcast_phy(dev, &phy_id, i, true);
James Bottomley2908d772006-08-29 09:22:51 -05001970 if (phy_id == -1)
1971 break;
1972 res = sas_rediscover(dev, phy_id);
1973 i = phy_id + 1;
1974 } while (i < ex->num_phys);
1975 }
1976out:
1977 return res;
1978}
1979
FUJITA Tomonoriba1fc172007-07-09 12:52:08 +09001980int sas_smp_handler(struct Scsi_Host *shost, struct sas_rphy *rphy,
1981 struct request *req)
1982{
1983 struct domain_device *dev;
Darrick J. Wong2cd614c2007-07-24 09:33:49 -07001984 int ret, type;
FUJITA Tomonoriba1fc172007-07-09 12:52:08 +09001985 struct request *rsp = req->next_rq;
1986
1987 if (!rsp) {
1988 printk("%s: space for a smp response is missing\n",
Harvey Harrisoncadbd4a2008-07-03 23:47:27 -07001989 __func__);
FUJITA Tomonoriba1fc172007-07-09 12:52:08 +09001990 return -EINVAL;
1991 }
1992
Darrick J. Wong2cd614c2007-07-24 09:33:49 -07001993 /* no rphy means no smp target support (ie aic94xx host) */
James Bottomleyb98e66f2007-12-28 16:35:17 -06001994 if (!rphy)
1995 return sas_smp_host_handler(shost, req, rsp);
1996
Darrick J. Wong2cd614c2007-07-24 09:33:49 -07001997 type = rphy->identify.device_type;
FUJITA Tomonoriba1fc172007-07-09 12:52:08 +09001998
1999 if (type != SAS_EDGE_EXPANDER_DEVICE &&
2000 type != SAS_FANOUT_EXPANDER_DEVICE) {
2001 printk("%s: can we send a smp request to a device?\n",
Harvey Harrisoncadbd4a2008-07-03 23:47:27 -07002002 __func__);
FUJITA Tomonoriba1fc172007-07-09 12:52:08 +09002003 return -EINVAL;
2004 }
2005
2006 dev = sas_find_dev_by_rphy(rphy);
2007 if (!dev) {
Harvey Harrisoncadbd4a2008-07-03 23:47:27 -07002008 printk("%s: fail to find a domain_device?\n", __func__);
FUJITA Tomonoriba1fc172007-07-09 12:52:08 +09002009 return -EINVAL;
2010 }
2011
2012 /* do we need to support multiple segments? */
2013 if (req->bio->bi_vcnt > 1 || rsp->bio->bi_vcnt > 1) {
2014 printk("%s: multiple segments req %u %u, rsp %u %u\n",
Tejun Heob0790412009-05-07 22:24:42 +09002015 __func__, req->bio->bi_vcnt, blk_rq_bytes(req),
2016 rsp->bio->bi_vcnt, blk_rq_bytes(rsp));
FUJITA Tomonoriba1fc172007-07-09 12:52:08 +09002017 return -EINVAL;
2018 }
2019
Tejun Heob0790412009-05-07 22:24:42 +09002020 ret = smp_execute_task(dev, bio_data(req->bio), blk_rq_bytes(req),
2021 bio_data(rsp->bio), blk_rq_bytes(rsp));
James Bottomley2d4b63e2007-12-29 11:49:53 -06002022 if (ret > 0) {
2023 /* positive number is the untransferred residual */
Tejun Heoc3a4d782009-05-07 22:24:37 +09002024 rsp->resid_len = ret;
Tejun Heo5f49f632009-05-19 18:33:05 +09002025 req->resid_len = 0;
James Bottomley2d4b63e2007-12-29 11:49:53 -06002026 ret = 0;
Tejun Heo5f49f632009-05-19 18:33:05 +09002027 } else if (ret == 0) {
2028 rsp->resid_len = 0;
2029 req->resid_len = 0;
James Bottomley2d4b63e2007-12-29 11:49:53 -06002030 }
FUJITA Tomonoriba1fc172007-07-09 12:52:08 +09002031
2032 return ret;
2033}