[SCSI] libsas: prevent domain rediscovery competing with ata error handling
[deliverable/linux.git] / drivers / scsi / libsas / sas_expander.c
CommitLineData
2908d778
JB
1/*
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
2908d778 25#include <linux/scatterlist.h>
ba1fc175 26#include <linux/blkdev.h>
5a0e3ad6 27#include <linux/slab.h>
2908d778
JB
28
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
2908d778
JB
41/* ---------- 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{
42961ee8 69 int res, retry;
70 struct sas_task *task = NULL;
2908d778
JB
71 struct sas_internal *i =
72 to_sas_internal(dev->port->ha->core.shost->transportt);
73
42961ee8 74 for (retry = 0; retry < 3; retry++) {
75 task = sas_alloc_task(GFP_KERNEL);
76 if (!task)
77 return -ENOMEM;
2908d778 78
42961ee8 79 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);
2908d778 83
42961ee8 84 task->task_done = smp_task_done;
2908d778 85
42961ee8 86 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);
2908d778 90
42961ee8 91 res = i->dft->lldd_execute_task(task, 1, GFP_KERNEL);
2908d778 92
42961ee8 93 if (res) {
94 del_timer(&task->timer);
95 SAS_DPRINTK("executing SMP task failed:%d\n", res);
2908d778
JB
96 goto ex_err;
97 }
42961ee8 98
99 wait_for_completion(&task->completion);
32e8ae36 100 res = -ECOMM;
42961ee8 101 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 &&
df64d3ca 110 task->task_status.stat == SAM_STAT_GOOD) {
42961ee8 111 res = 0;
112 break;
2d4b63e1
JB
113 } 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;
42961ee8 123 } else {
124 SAS_DPRINTK("%s: task to dev %016llx response: 0x%x "
cadbd4a5 125 "status 0x%x\n", __func__,
42961ee8 126 SAS_ADDR(dev->sas_addr),
127 task->task_status.resp,
128 task->task_status.stat);
129 sas_free_task(task);
130 task = NULL;
131 }
2908d778 132 }
2908d778 133ex_err:
42961ee8 134 BUG_ON(retry == 3 && task != NULL);
135 if (task != NULL) {
136 sas_free_task(task);
137 }
2908d778
JB
138 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;
2bc72c91 178 break;
2908d778
JB
179 default:
180 phy->phy_state = PHY_NOT_PRESENT;
2bc72c91 181 break;
2908d778
JB
182 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
bb041a0e
JW
202 phy->phy->identify.sas_address = SAS_ADDR(phy->attached_sas_addr);
203 phy->phy->identify.device_type = phy->attached_dev_type;
2908d778
JB
204 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;
a01e70e5
JB
207 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;
88edf746 211 phy->phy->negotiated_linkrate = phy->linkrate;
2908d778
JB
212
213 if (!rediscover)
2bc72c91
JW
214 if (sas_phy_add(phy->phy)) {
215 sas_phy_free(phy->phy);
216 return;
217 }
2908d778
JB
218
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
1acce194
JB
232static 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;
25985edc 245 /* This is detecting a failure to transmit initial
1acce194
JB
246 * dev to host FIS as described in section G.5 of
247 * sas-2 r 04b */
248 dr = &((struct smp_resp *)disc_resp)->disc;
183ce896 249 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 }
1acce194
JB
254 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 */
38e2f035 259 sas_smp_phy_control(dev, single, PHY_FUNC_LINK_RESET, NULL);
1acce194
JB
260 /* 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
2908d778
JB
267static 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) {
1acce194 287 res = sas_ex_phy_discover_helper(dev, disc_req, disc_resp, single);
2908d778
JB
288 } else {
289 int i;
290
291 for (i = 0; i < ex->num_phys; i++) {
1acce194
JB
292 res = sas_ex_phy_discover_helper(dev, disc_req,
293 disc_resp, i);
2908d778
JB
294 if (res)
295 goto out_err;
2908d778
JB
296 }
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);
ffaac8f4 334 dev->ex_dev.t2t_supp = rg->t2t_supp;
2908d778
JB
335 dev->ex_dev.conf_route_table = rg->conf_route_table;
336 dev->ex_dev.configuring = rg->configuring;
337 memcpy(dev->ex_dev.enclosure_logical_id, rg->enclosure_logical_id, 8);
338}
339
340#define RG_REQ_SIZE 8
341#define RG_RESP_SIZE 32
342
343static int sas_ex_general(struct domain_device *dev)
344{
345 u8 *rg_req;
346 struct smp_resp *rg_resp;
347 int res;
348 int i;
349
350 rg_req = alloc_smp_req(RG_REQ_SIZE);
351 if (!rg_req)
352 return -ENOMEM;
353
354 rg_resp = alloc_smp_resp(RG_RESP_SIZE);
355 if (!rg_resp) {
356 kfree(rg_req);
357 return -ENOMEM;
358 }
359
360 rg_req[1] = SMP_REPORT_GENERAL;
361
362 for (i = 0; i < 5; i++) {
363 res = smp_execute_task(dev, rg_req, RG_REQ_SIZE, rg_resp,
364 RG_RESP_SIZE);
365
366 if (res) {
367 SAS_DPRINTK("RG to ex %016llx failed:0x%x\n",
368 SAS_ADDR(dev->sas_addr), res);
369 goto out;
370 } else if (rg_resp->result != SMP_RESP_FUNC_ACC) {
371 SAS_DPRINTK("RG:ex %016llx returned SMP result:0x%x\n",
372 SAS_ADDR(dev->sas_addr), rg_resp->result);
373 res = rg_resp->result;
374 goto out;
375 }
376
377 ex_assign_report_general(dev, rg_resp);
378
379 if (dev->ex_dev.configuring) {
380 SAS_DPRINTK("RG: ex %llx self-configuring...\n",
381 SAS_ADDR(dev->sas_addr));
382 schedule_timeout_interruptible(5*HZ);
383 } else
384 break;
385 }
386out:
387 kfree(rg_req);
388 kfree(rg_resp);
389 return res;
390}
391
392static void ex_assign_manuf_info(struct domain_device *dev, void
393 *_mi_resp)
394{
395 u8 *mi_resp = _mi_resp;
396 struct sas_rphy *rphy = dev->rphy;
397 struct sas_expander_device *edev = rphy_to_expander_device(rphy);
398
399 memcpy(edev->vendor_id, mi_resp + 12, SAS_EXPANDER_VENDOR_ID_LEN);
400 memcpy(edev->product_id, mi_resp + 20, SAS_EXPANDER_PRODUCT_ID_LEN);
401 memcpy(edev->product_rev, mi_resp + 36,
402 SAS_EXPANDER_PRODUCT_REV_LEN);
403
404 if (mi_resp[8] & 1) {
405 memcpy(edev->component_vendor_id, mi_resp + 40,
406 SAS_EXPANDER_COMPONENT_VENDOR_ID_LEN);
407 edev->component_id = mi_resp[48] << 8 | mi_resp[49];
408 edev->component_revision_id = mi_resp[50];
409 }
410}
411
412#define MI_REQ_SIZE 8
413#define MI_RESP_SIZE 64
414
415static int sas_ex_manuf_info(struct domain_device *dev)
416{
417 u8 *mi_req;
418 u8 *mi_resp;
419 int res;
420
421 mi_req = alloc_smp_req(MI_REQ_SIZE);
422 if (!mi_req)
423 return -ENOMEM;
424
425 mi_resp = alloc_smp_resp(MI_RESP_SIZE);
426 if (!mi_resp) {
427 kfree(mi_req);
428 return -ENOMEM;
429 }
430
431 mi_req[1] = SMP_REPORT_MANUF_INFO;
432
433 res = smp_execute_task(dev, mi_req, MI_REQ_SIZE, mi_resp,MI_RESP_SIZE);
434 if (res) {
435 SAS_DPRINTK("MI: ex %016llx failed:0x%x\n",
436 SAS_ADDR(dev->sas_addr), res);
437 goto out;
438 } else if (mi_resp[2] != SMP_RESP_FUNC_ACC) {
439 SAS_DPRINTK("MI ex %016llx returned SMP result:0x%x\n",
440 SAS_ADDR(dev->sas_addr), mi_resp[2]);
441 goto out;
442 }
443
444 ex_assign_manuf_info(dev, mi_resp);
445out:
446 kfree(mi_req);
447 kfree(mi_resp);
448 return res;
449}
450
451#define PC_REQ_SIZE 44
452#define PC_RESP_SIZE 8
453
454int sas_smp_phy_control(struct domain_device *dev, int phy_id,
a01e70e5
JB
455 enum phy_func phy_func,
456 struct sas_phy_linkrates *rates)
2908d778
JB
457{
458 u8 *pc_req;
459 u8 *pc_resp;
460 int res;
461
462 pc_req = alloc_smp_req(PC_REQ_SIZE);
463 if (!pc_req)
464 return -ENOMEM;
465
466 pc_resp = alloc_smp_resp(PC_RESP_SIZE);
467 if (!pc_resp) {
468 kfree(pc_req);
469 return -ENOMEM;
470 }
471
472 pc_req[1] = SMP_PHY_CONTROL;
473 pc_req[9] = phy_id;
474 pc_req[10]= phy_func;
a01e70e5
JB
475 if (rates) {
476 pc_req[32] = rates->minimum_linkrate << 4;
477 pc_req[33] = rates->maximum_linkrate << 4;
478 }
2908d778
JB
479
480 res = smp_execute_task(dev, pc_req, PC_REQ_SIZE, pc_resp,PC_RESP_SIZE);
481
482 kfree(pc_resp);
483 kfree(pc_req);
484 return res;
485}
486
487static void sas_ex_disable_phy(struct domain_device *dev, int phy_id)
488{
489 struct expander_device *ex = &dev->ex_dev;
490 struct ex_phy *phy = &ex->ex_phy[phy_id];
491
a01e70e5 492 sas_smp_phy_control(dev, phy_id, PHY_FUNC_DISABLE, NULL);
88edf746 493 phy->linkrate = SAS_PHY_DISABLED;
2908d778
JB
494}
495
496static void sas_ex_disable_port(struct domain_device *dev, u8 *sas_addr)
497{
498 struct expander_device *ex = &dev->ex_dev;
499 int i;
500
501 for (i = 0; i < ex->num_phys; i++) {
502 struct ex_phy *phy = &ex->ex_phy[i];
503
504 if (phy->phy_state == PHY_VACANT ||
505 phy->phy_state == PHY_NOT_PRESENT)
506 continue;
507
508 if (SAS_ADDR(phy->attached_sas_addr) == SAS_ADDR(sas_addr))
509 sas_ex_disable_phy(dev, i);
510 }
511}
512
513static int sas_dev_present_in_domain(struct asd_sas_port *port,
514 u8 *sas_addr)
515{
516 struct domain_device *dev;
517
518 if (SAS_ADDR(port->sas_addr) == SAS_ADDR(sas_addr))
519 return 1;
520 list_for_each_entry(dev, &port->dev_list, dev_list_node) {
521 if (SAS_ADDR(dev->sas_addr) == SAS_ADDR(sas_addr))
522 return 1;
523 }
524 return 0;
525}
526
527#define RPEL_REQ_SIZE 16
528#define RPEL_RESP_SIZE 32
529int sas_smp_get_phy_events(struct sas_phy *phy)
530{
531 int res;
92631fa4
JJ
532 u8 *req;
533 u8 *resp;
2908d778
JB
534 struct sas_rphy *rphy = dev_to_rphy(phy->dev.parent);
535 struct domain_device *dev = sas_find_dev_by_rphy(rphy);
2908d778 536
92631fa4
JJ
537 req = alloc_smp_req(RPEL_REQ_SIZE);
538 if (!req)
2908d778
JB
539 return -ENOMEM;
540
92631fa4
JJ
541 resp = alloc_smp_resp(RPEL_RESP_SIZE);
542 if (!resp) {
543 kfree(req);
544 return -ENOMEM;
545 }
546
2908d778
JB
547 req[1] = SMP_REPORT_PHY_ERR_LOG;
548 req[9] = phy->number;
549
550 res = smp_execute_task(dev, req, RPEL_REQ_SIZE,
551 resp, RPEL_RESP_SIZE);
552
553 if (!res)
554 goto out;
555
556 phy->invalid_dword_count = scsi_to_u32(&resp[12]);
557 phy->running_disparity_error_count = scsi_to_u32(&resp[16]);
558 phy->loss_of_dword_sync_count = scsi_to_u32(&resp[20]);
559 phy->phy_reset_problem_count = scsi_to_u32(&resp[24]);
560
561 out:
562 kfree(resp);
563 return res;
564
565}
566
b9142174
JB
567#ifdef CONFIG_SCSI_SAS_ATA
568
2908d778
JB
569#define RPS_REQ_SIZE 16
570#define RPS_RESP_SIZE 60
571
572static int sas_get_report_phy_sata(struct domain_device *dev,
573 int phy_id,
574 struct smp_resp *rps_resp)
575{
576 int res;
577 u8 *rps_req = alloc_smp_req(RPS_REQ_SIZE);
1acce194 578 u8 *resp = (u8 *)rps_resp;
2908d778
JB
579
580 if (!rps_req)
581 return -ENOMEM;
582
583 rps_req[1] = SMP_REPORT_PHY_SATA;
584 rps_req[9] = phy_id;
585
586 res = smp_execute_task(dev, rps_req, RPS_REQ_SIZE,
587 rps_resp, RPS_RESP_SIZE);
588
1acce194
JB
589 /* 0x34 is the FIS type for the D2H fis. There's a potential
590 * standards cockup here. sas-2 explicitly specifies the FIS
591 * should be encoded so that FIS type is in resp[24].
592 * However, some expanders endian reverse this. Undo the
593 * reversal here */
594 if (!res && resp[27] == 0x34 && resp[24] != 0x34) {
595 int i;
596
597 for (i = 0; i < 5; i++) {
598 int j = 24 + (i*4);
599 u8 a, b;
600 a = resp[j + 0];
601 b = resp[j + 1];
602 resp[j + 0] = resp[j + 3];
603 resp[j + 1] = resp[j + 2];
604 resp[j + 2] = b;
605 resp[j + 3] = a;
606 }
607 }
608
2908d778 609 kfree(rps_req);
1acce194 610 return res;
2908d778 611}
b9142174 612#endif
2908d778
JB
613
614static void sas_ex_get_linkrate(struct domain_device *parent,
615 struct domain_device *child,
616 struct ex_phy *parent_phy)
617{
618 struct expander_device *parent_ex = &parent->ex_dev;
619 struct sas_port *port;
620 int i;
621
622 child->pathways = 0;
623
624 port = parent_phy->port;
625
626 for (i = 0; i < parent_ex->num_phys; i++) {
627 struct ex_phy *phy = &parent_ex->ex_phy[i];
628
629 if (phy->phy_state == PHY_VACANT ||
630 phy->phy_state == PHY_NOT_PRESENT)
631 continue;
632
633 if (SAS_ADDR(phy->attached_sas_addr) ==
634 SAS_ADDR(child->sas_addr)) {
635
636 child->min_linkrate = min(parent->min_linkrate,
637 phy->linkrate);
638 child->max_linkrate = max(parent->max_linkrate,
639 phy->linkrate);
640 child->pathways++;
641 sas_port_add_phy(port, phy->phy);
642 }
643 }
644 child->linkrate = min(parent_phy->linkrate, child->max_linkrate);
645 child->pathways = min(child->pathways, parent->pathways);
646}
647
648static struct domain_device *sas_ex_discover_end_dev(
649 struct domain_device *parent, int phy_id)
650{
651 struct expander_device *parent_ex = &parent->ex_dev;
652 struct ex_phy *phy = &parent_ex->ex_phy[phy_id];
653 struct domain_device *child = NULL;
654 struct sas_rphy *rphy;
655 int res;
656
657 if (phy->attached_sata_host || phy->attached_sata_ps)
658 return NULL;
659
735f7d2f 660 child = sas_alloc_device();
2908d778
JB
661 if (!child)
662 return NULL;
663
735f7d2f 664 kref_get(&parent->kref);
2908d778
JB
665 child->parent = parent;
666 child->port = parent->port;
667 child->iproto = phy->attached_iproto;
668 memcpy(child->sas_addr, phy->attached_sas_addr, SAS_ADDR_SIZE);
669 sas_hash_addr(child->hashed_sas_addr, child->sas_addr);
024879ea
JB
670 if (!phy->port) {
671 phy->port = sas_port_alloc(&parent->rphy->dev, phy_id);
672 if (unlikely(!phy->port))
673 goto out_err;
674 if (unlikely(sas_port_add(phy->port) != 0)) {
675 sas_port_free(phy->port);
676 goto out_err;
677 }
678 }
2908d778
JB
679 sas_ex_get_linkrate(parent, child, phy);
680
b9142174 681#ifdef CONFIG_SCSI_SAS_ATA
5929faf3 682 if ((phy->attached_tproto & SAS_PROTOCOL_STP) || phy->attached_sata_dev) {
2908d778 683 child->dev_type = SATA_DEV;
5929faf3 684 if (phy->attached_tproto & SAS_PROTOCOL_STP)
2908d778
JB
685 child->tproto = phy->attached_tproto;
686 if (phy->attached_sata_dev)
687 child->tproto |= SATA_DEV;
688 res = sas_get_report_phy_sata(parent, phy_id,
689 &child->sata_dev.rps_resp);
690 if (res) {
691 SAS_DPRINTK("report phy sata to %016llx:0x%x returned "
692 "0x%x\n", SAS_ADDR(parent->sas_addr),
693 phy_id, res);
024879ea 694 goto out_free;
2908d778
JB
695 }
696 memcpy(child->frame_rcvd, &child->sata_dev.rps_resp.rps.fis,
697 sizeof(struct dev_to_host_fis));
1acce194
JB
698
699 rphy = sas_end_device_alloc(phy->port);
528fd552
JB
700 if (unlikely(!rphy))
701 goto out_free;
1acce194 702
2908d778 703 sas_init_dev(child);
1acce194
JB
704
705 child->rphy = rphy;
706
87c8331f 707 list_add_tail(&child->disco_list_node, &parent->port->disco_list);
1acce194 708
2908d778
JB
709 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);
1acce194 715 goto out_list_del;
2908d778 716 }
b9142174
JB
717 } else
718#endif
5929faf3 719 if (phy->attached_tproto & SAS_PROTOCOL_SSP) {
2908d778
JB
720 child->dev_type = SAS_END_DEV;
721 rphy = sas_end_device_alloc(phy->port);
722 /* FIXME: error handling */
024879ea
JB
723 if (unlikely(!rphy))
724 goto out_free;
2908d778
JB
725 child->tproto = phy->attached_tproto;
726 sas_init_dev(child);
727
728 child->rphy = rphy;
729 sas_fill_in_rphy(child, rphy);
730
9d720d82 731 spin_lock_irq(&parent->port->dev_list_lock);
2908d778 732 list_add_tail(&child->dev_list_node, &parent->port->dev_list);
9d720d82 733 spin_unlock_irq(&parent->port->dev_list_lock);
2908d778
JB
734
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);
024879ea 741 goto out_list_del;
2908d778
JB
742 }
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);
b9142174 747 goto out_free;
2908d778
JB
748 }
749
750 list_add_tail(&child->siblings, &parent_ex->children);
751 return child;
024879ea
JB
752
753 out_list_del:
6f63caae
DW
754 sas_rphy_free(child->rphy);
755 child->rphy = NULL;
1a34c064 756
87c8331f 757 list_del(&child->disco_list_node);
1a34c064 758 spin_lock_irq(&parent->port->dev_list_lock);
024879ea 759 list_del(&child->dev_list_node);
1a34c064 760 spin_unlock_irq(&parent->port->dev_list_lock);
024879ea
JB
761 out_free:
762 sas_port_delete(phy->port);
763 out_err:
764 phy->port = NULL;
735f7d2f 765 sas_put_device(child);
024879ea 766 return NULL;
2908d778
JB
767}
768
423f7cf4
DW
769/* See if this phy is part of a wide port */
770static int sas_ex_join_wide_port(struct domain_device *parent, int phy_id)
771{
772 struct ex_phy *phy = &parent->ex_dev.ex_phy[phy_id];
773 int i;
774
775 for (i = 0; i < parent->ex_dev.num_phys; i++) {
776 struct ex_phy *ephy = &parent->ex_dev.ex_phy[i];
777
778 if (ephy == phy)
779 continue;
780
781 if (!memcmp(phy->attached_sas_addr, ephy->attached_sas_addr,
782 SAS_ADDR_SIZE) && ephy->port) {
783 sas_port_add_phy(ephy->port, phy->phy);
19252de6 784 phy->port = ephy->port;
423f7cf4
DW
785 phy->phy_state = PHY_DEVICE_DISCOVERED;
786 return 0;
787 }
788 }
789
790 return -ENODEV;
791}
792
2908d778
JB
793static struct domain_device *sas_ex_discover_expander(
794 struct domain_device *parent, int phy_id)
795{
796 struct sas_expander_device *parent_ex = rphy_to_expander_device(parent->rphy);
797 struct ex_phy *phy = &parent->ex_dev.ex_phy[phy_id];
798 struct domain_device *child = NULL;
799 struct sas_rphy *rphy;
800 struct sas_expander_device *edev;
801 struct asd_sas_port *port;
802 int res;
803
804 if (phy->routing_attr == DIRECT_ROUTING) {
805 SAS_DPRINTK("ex %016llx:0x%x:D <--> ex %016llx:0x%x is not "
806 "allowed\n",
807 SAS_ADDR(parent->sas_addr), phy_id,
808 SAS_ADDR(phy->attached_sas_addr),
809 phy->attached_phy_id);
810 return NULL;
811 }
735f7d2f 812 child = sas_alloc_device();
2908d778
JB
813 if (!child)
814 return NULL;
815
816 phy->port = sas_port_alloc(&parent->rphy->dev, phy_id);
817 /* FIXME: better error handling */
818 BUG_ON(sas_port_add(phy->port) != 0);
819
820
821 switch (phy->attached_dev_type) {
822 case EDGE_DEV:
823 rphy = sas_expander_alloc(phy->port,
824 SAS_EDGE_EXPANDER_DEVICE);
825 break;
826 case FANOUT_DEV:
827 rphy = sas_expander_alloc(phy->port,
828 SAS_FANOUT_EXPANDER_DEVICE);
829 break;
830 default:
831 rphy = NULL; /* shut gcc up */
832 BUG();
833 }
834 port = parent->port;
835 child->rphy = rphy;
836 edev = rphy_to_expander_device(rphy);
837 child->dev_type = phy->attached_dev_type;
735f7d2f 838 kref_get(&parent->kref);
2908d778
JB
839 child->parent = parent;
840 child->port = port;
841 child->iproto = phy->attached_iproto;
842 child->tproto = phy->attached_tproto;
843 memcpy(child->sas_addr, phy->attached_sas_addr, SAS_ADDR_SIZE);
844 sas_hash_addr(child->hashed_sas_addr, child->sas_addr);
845 sas_ex_get_linkrate(parent, child, phy);
846 edev->level = parent_ex->level + 1;
847 parent->port->disc.max_level = max(parent->port->disc.max_level,
848 edev->level);
849 sas_init_dev(child);
850 sas_fill_in_rphy(child, rphy);
851 sas_rphy_add(rphy);
852
9d720d82 853 spin_lock_irq(&parent->port->dev_list_lock);
2908d778 854 list_add_tail(&child->dev_list_node, &parent->port->dev_list);
9d720d82 855 spin_unlock_irq(&parent->port->dev_list_lock);
2908d778
JB
856
857 res = sas_discover_expander(child);
858 if (res) {
5911e963
LT
859 spin_lock_irq(&parent->port->dev_list_lock);
860 list_del(&child->dev_list_node);
861 spin_unlock_irq(&parent->port->dev_list_lock);
735f7d2f 862 sas_put_device(child);
2908d778
JB
863 return NULL;
864 }
865 list_add_tail(&child->siblings, &parent->ex_dev.children);
866 return child;
867}
868
869static int sas_ex_discover_dev(struct domain_device *dev, int phy_id)
870{
871 struct expander_device *ex = &dev->ex_dev;
872 struct ex_phy *ex_phy = &ex->ex_phy[phy_id];
873 struct domain_device *child = NULL;
874 int res = 0;
875
876 /* Phy state */
88edf746 877 if (ex_phy->linkrate == SAS_SATA_SPINUP_HOLD) {
a01e70e5 878 if (!sas_smp_phy_control(dev, phy_id, PHY_FUNC_LINK_RESET, NULL))
2908d778
JB
879 res = sas_ex_phy_discover(dev, phy_id);
880 if (res)
881 return res;
882 }
883
884 /* Parent and domain coherency */
885 if (!dev->parent && (SAS_ADDR(ex_phy->attached_sas_addr) ==
886 SAS_ADDR(dev->port->sas_addr))) {
887 sas_add_parent_port(dev, phy_id);
888 return 0;
889 }
890 if (dev->parent && (SAS_ADDR(ex_phy->attached_sas_addr) ==
891 SAS_ADDR(dev->parent->sas_addr))) {
892 sas_add_parent_port(dev, phy_id);
893 if (ex_phy->routing_attr == TABLE_ROUTING)
894 sas_configure_phy(dev, phy_id, dev->port->sas_addr, 1);
895 return 0;
896 }
897
898 if (sas_dev_present_in_domain(dev->port, ex_phy->attached_sas_addr))
899 sas_ex_disable_port(dev, ex_phy->attached_sas_addr);
900
901 if (ex_phy->attached_dev_type == NO_DEVICE) {
902 if (ex_phy->routing_attr == DIRECT_ROUTING) {
903 memset(ex_phy->attached_sas_addr, 0, SAS_ADDR_SIZE);
904 sas_configure_routing(dev, ex_phy->attached_sas_addr);
905 }
906 return 0;
88edf746 907 } else if (ex_phy->linkrate == SAS_LINK_RATE_UNKNOWN)
2908d778
JB
908 return 0;
909
910 if (ex_phy->attached_dev_type != SAS_END_DEV &&
911 ex_phy->attached_dev_type != FANOUT_DEV &&
912 ex_phy->attached_dev_type != EDGE_DEV) {
913 SAS_DPRINTK("unknown device type(0x%x) attached to ex %016llx "
914 "phy 0x%x\n", ex_phy->attached_dev_type,
915 SAS_ADDR(dev->sas_addr),
916 phy_id);
917 return 0;
918 }
919
920 res = sas_configure_routing(dev, ex_phy->attached_sas_addr);
921 if (res) {
922 SAS_DPRINTK("configure routing for dev %016llx "
923 "reported 0x%x. Forgotten\n",
924 SAS_ADDR(ex_phy->attached_sas_addr), res);
925 sas_disable_routing(dev, ex_phy->attached_sas_addr);
926 return res;
927 }
928
423f7cf4
DW
929 res = sas_ex_join_wide_port(dev, phy_id);
930 if (!res) {
931 SAS_DPRINTK("Attaching ex phy%d to wide port %016llx\n",
932 phy_id, SAS_ADDR(ex_phy->attached_sas_addr));
933 return res;
934 }
935
2908d778
JB
936 switch (ex_phy->attached_dev_type) {
937 case SAS_END_DEV:
938 child = sas_ex_discover_end_dev(dev, phy_id);
939 break;
940 case FANOUT_DEV:
941 if (SAS_ADDR(dev->port->disc.fanout_sas_addr)) {
942 SAS_DPRINTK("second fanout expander %016llx phy 0x%x "
943 "attached to ex %016llx phy 0x%x\n",
944 SAS_ADDR(ex_phy->attached_sas_addr),
945 ex_phy->attached_phy_id,
946 SAS_ADDR(dev->sas_addr),
947 phy_id);
948 sas_ex_disable_phy(dev, phy_id);
949 break;
950 } else
951 memcpy(dev->port->disc.fanout_sas_addr,
952 ex_phy->attached_sas_addr, SAS_ADDR_SIZE);
953 /* fallthrough */
954 case EDGE_DEV:
955 child = sas_ex_discover_expander(dev, phy_id);
956 break;
957 default:
958 break;
959 }
960
961 if (child) {
962 int i;
963
964 for (i = 0; i < ex->num_phys; i++) {
965 if (ex->ex_phy[i].phy_state == PHY_VACANT ||
966 ex->ex_phy[i].phy_state == PHY_NOT_PRESENT)
967 continue;
19252de6
TP
968 /*
969 * Due to races, the phy might not get added to the
970 * wide port, so we add the phy to the wide port here.
971 */
2908d778 972 if (SAS_ADDR(ex->ex_phy[i].attached_sas_addr) ==
19252de6 973 SAS_ADDR(child->sas_addr)) {
2908d778 974 ex->ex_phy[i].phy_state= PHY_DEVICE_DISCOVERED;
19252de6
TP
975 res = sas_ex_join_wide_port(dev, i);
976 if (!res)
977 SAS_DPRINTK("Attaching ex phy%d to wide port %016llx\n",
978 i, SAS_ADDR(ex->ex_phy[i].attached_sas_addr));
979
980 }
2908d778
JB
981 }
982 }
983
984 return res;
985}
986
987static int sas_find_sub_addr(struct domain_device *dev, u8 *sub_addr)
988{
989 struct expander_device *ex = &dev->ex_dev;
990 int i;
991
992 for (i = 0; i < ex->num_phys; i++) {
993 struct ex_phy *phy = &ex->ex_phy[i];
994
995 if (phy->phy_state == PHY_VACANT ||
996 phy->phy_state == PHY_NOT_PRESENT)
997 continue;
998
999 if ((phy->attached_dev_type == EDGE_DEV ||
1000 phy->attached_dev_type == FANOUT_DEV) &&
1001 phy->routing_attr == SUBTRACTIVE_ROUTING) {
1002
1003 memcpy(sub_addr, phy->attached_sas_addr,SAS_ADDR_SIZE);
1004
1005 return 1;
1006 }
1007 }
1008 return 0;
1009}
1010
1011static int sas_check_level_subtractive_boundary(struct domain_device *dev)
1012{
1013 struct expander_device *ex = &dev->ex_dev;
1014 struct domain_device *child;
1015 u8 sub_addr[8] = {0, };
1016
1017 list_for_each_entry(child, &ex->children, siblings) {
1018 if (child->dev_type != EDGE_DEV &&
1019 child->dev_type != FANOUT_DEV)
1020 continue;
1021 if (sub_addr[0] == 0) {
1022 sas_find_sub_addr(child, sub_addr);
1023 continue;
1024 } else {
1025 u8 s2[8];
1026
1027 if (sas_find_sub_addr(child, s2) &&
1028 (SAS_ADDR(sub_addr) != SAS_ADDR(s2))) {
1029
1030 SAS_DPRINTK("ex %016llx->%016llx-?->%016llx "
1031 "diverges from subtractive "
1032 "boundary %016llx\n",
1033 SAS_ADDR(dev->sas_addr),
1034 SAS_ADDR(child->sas_addr),
1035 SAS_ADDR(s2),
1036 SAS_ADDR(sub_addr));
1037
1038 sas_ex_disable_port(child, s2);
1039 }
1040 }
1041 }
1042 return 0;
1043}
1044/**
1045 * sas_ex_discover_devices -- discover devices attached to this expander
1046 * dev: pointer to the expander domain device
1047 * single: if you want to do a single phy, else set to -1;
1048 *
1049 * Configure this expander for use with its devices and register the
1050 * devices of this expander.
1051 */
1052static int sas_ex_discover_devices(struct domain_device *dev, int single)
1053{
1054 struct expander_device *ex = &dev->ex_dev;
1055 int i = 0, end = ex->num_phys;
1056 int res = 0;
1057
1058 if (0 <= single && single < end) {
1059 i = single;
1060 end = i+1;
1061 }
1062
1063 for ( ; i < end; i++) {
1064 struct ex_phy *ex_phy = &ex->ex_phy[i];
1065
1066 if (ex_phy->phy_state == PHY_VACANT ||
1067 ex_phy->phy_state == PHY_NOT_PRESENT ||
1068 ex_phy->phy_state == PHY_DEVICE_DISCOVERED)
1069 continue;
1070
1071 switch (ex_phy->linkrate) {
88edf746
JB
1072 case SAS_PHY_DISABLED:
1073 case SAS_PHY_RESET_PROBLEM:
1074 case SAS_SATA_PORT_SELECTOR:
2908d778
JB
1075 continue;
1076 default:
1077 res = sas_ex_discover_dev(dev, i);
1078 if (res)
1079 break;
1080 continue;
1081 }
1082 }
1083
1084 if (!res)
1085 sas_check_level_subtractive_boundary(dev);
1086
1087 return res;
1088}
1089
1090static int sas_check_ex_subtractive_boundary(struct domain_device *dev)
1091{
1092 struct expander_device *ex = &dev->ex_dev;
1093 int i;
1094 u8 *sub_sas_addr = NULL;
1095
1096 if (dev->dev_type != EDGE_DEV)
1097 return 0;
1098
1099 for (i = 0; i < ex->num_phys; i++) {
1100 struct ex_phy *phy = &ex->ex_phy[i];
1101
1102 if (phy->phy_state == PHY_VACANT ||
1103 phy->phy_state == PHY_NOT_PRESENT)
1104 continue;
1105
1106 if ((phy->attached_dev_type == FANOUT_DEV ||
1107 phy->attached_dev_type == EDGE_DEV) &&
1108 phy->routing_attr == SUBTRACTIVE_ROUTING) {
1109
1110 if (!sub_sas_addr)
1111 sub_sas_addr = &phy->attached_sas_addr[0];
1112 else if (SAS_ADDR(sub_sas_addr) !=
1113 SAS_ADDR(phy->attached_sas_addr)) {
1114
1115 SAS_DPRINTK("ex %016llx phy 0x%x "
1116 "diverges(%016llx) on subtractive "
1117 "boundary(%016llx). Disabled\n",
1118 SAS_ADDR(dev->sas_addr), i,
1119 SAS_ADDR(phy->attached_sas_addr),
1120 SAS_ADDR(sub_sas_addr));
1121 sas_ex_disable_phy(dev, i);
1122 }
1123 }
1124 }
1125 return 0;
1126}
1127
1128static void sas_print_parent_topology_bug(struct domain_device *child,
1129 struct ex_phy *parent_phy,
1130 struct ex_phy *child_phy)
1131{
1132 static const char ra_char[] = {
1133 [DIRECT_ROUTING] = 'D',
1134 [SUBTRACTIVE_ROUTING] = 'S',
1135 [TABLE_ROUTING] = 'T',
1136 };
1137 static const char *ex_type[] = {
1138 [EDGE_DEV] = "edge",
1139 [FANOUT_DEV] = "fanout",
1140 };
1141 struct domain_device *parent = child->parent;
1142
ffaac8f4
LT
1143 sas_printk("%s ex %016llx (T2T supp:%d) phy 0x%x <--> %s ex %016llx "
1144 "(T2T supp:%d) phy 0x%x has %c:%c routing link!\n",
2908d778
JB
1145
1146 ex_type[parent->dev_type],
1147 SAS_ADDR(parent->sas_addr),
ffaac8f4 1148 parent->ex_dev.t2t_supp,
2908d778
JB
1149 parent_phy->phy_id,
1150
1151 ex_type[child->dev_type],
1152 SAS_ADDR(child->sas_addr),
ffaac8f4 1153 child->ex_dev.t2t_supp,
2908d778
JB
1154 child_phy->phy_id,
1155
1156 ra_char[parent_phy->routing_attr],
1157 ra_char[child_phy->routing_attr]);
1158}
1159
1160static int sas_check_eeds(struct domain_device *child,
1161 struct ex_phy *parent_phy,
1162 struct ex_phy *child_phy)
1163{
1164 int res = 0;
1165 struct domain_device *parent = child->parent;
1166
1167 if (SAS_ADDR(parent->port->disc.fanout_sas_addr) != 0) {
1168 res = -ENODEV;
1169 SAS_DPRINTK("edge ex %016llx phy S:0x%x <--> edge ex %016llx "
1170 "phy S:0x%x, while there is a fanout ex %016llx\n",
1171 SAS_ADDR(parent->sas_addr),
1172 parent_phy->phy_id,
1173 SAS_ADDR(child->sas_addr),
1174 child_phy->phy_id,
1175 SAS_ADDR(parent->port->disc.fanout_sas_addr));
1176 } else if (SAS_ADDR(parent->port->disc.eeds_a) == 0) {
1177 memcpy(parent->port->disc.eeds_a, parent->sas_addr,
1178 SAS_ADDR_SIZE);
1179 memcpy(parent->port->disc.eeds_b, child->sas_addr,
1180 SAS_ADDR_SIZE);
1181 } else if (((SAS_ADDR(parent->port->disc.eeds_a) ==
1182 SAS_ADDR(parent->sas_addr)) ||
1183 (SAS_ADDR(parent->port->disc.eeds_a) ==
1184 SAS_ADDR(child->sas_addr)))
1185 &&
1186 ((SAS_ADDR(parent->port->disc.eeds_b) ==
1187 SAS_ADDR(parent->sas_addr)) ||
1188 (SAS_ADDR(parent->port->disc.eeds_b) ==
1189 SAS_ADDR(child->sas_addr))))
1190 ;
1191 else {
1192 res = -ENODEV;
1193 SAS_DPRINTK("edge ex %016llx phy 0x%x <--> edge ex %016llx "
1194 "phy 0x%x link forms a third EEDS!\n",
1195 SAS_ADDR(parent->sas_addr),
1196 parent_phy->phy_id,
1197 SAS_ADDR(child->sas_addr),
1198 child_phy->phy_id);
1199 }
1200
1201 return res;
1202}
1203
1204/* Here we spill over 80 columns. It is intentional.
1205 */
1206static int sas_check_parent_topology(struct domain_device *child)
1207{
1208 struct expander_device *child_ex = &child->ex_dev;
1209 struct expander_device *parent_ex;
1210 int i;
1211 int res = 0;
1212
1213 if (!child->parent)
1214 return 0;
1215
1216 if (child->parent->dev_type != EDGE_DEV &&
1217 child->parent->dev_type != FANOUT_DEV)
1218 return 0;
1219
1220 parent_ex = &child->parent->ex_dev;
1221
1222 for (i = 0; i < parent_ex->num_phys; i++) {
1223 struct ex_phy *parent_phy = &parent_ex->ex_phy[i];
1224 struct ex_phy *child_phy;
1225
1226 if (parent_phy->phy_state == PHY_VACANT ||
1227 parent_phy->phy_state == PHY_NOT_PRESENT)
1228 continue;
1229
1230 if (SAS_ADDR(parent_phy->attached_sas_addr) != SAS_ADDR(child->sas_addr))
1231 continue;
1232
1233 child_phy = &child_ex->ex_phy[parent_phy->attached_phy_id];
1234
1235 switch (child->parent->dev_type) {
1236 case EDGE_DEV:
1237 if (child->dev_type == FANOUT_DEV) {
1238 if (parent_phy->routing_attr != SUBTRACTIVE_ROUTING ||
1239 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 == SUBTRACTIVE_ROUTING) {
1244 if (child_phy->routing_attr == SUBTRACTIVE_ROUTING) {
1245 res = sas_check_eeds(child, parent_phy, child_phy);
1246 } else if (child_phy->routing_attr != TABLE_ROUTING) {
1247 sas_print_parent_topology_bug(child, parent_phy, child_phy);
1248 res = -ENODEV;
1249 }
ffaac8f4
LT
1250 } else if (parent_phy->routing_attr == TABLE_ROUTING) {
1251 if (child_phy->routing_attr == SUBTRACTIVE_ROUTING ||
1252 (child_phy->routing_attr == TABLE_ROUTING &&
1253 child_ex->t2t_supp && parent_ex->t2t_supp)) {
1254 /* All good */;
1255 } else {
1256 sas_print_parent_topology_bug(child, parent_phy, child_phy);
1257 res = -ENODEV;
1258 }
2908d778
JB
1259 }
1260 break;
1261 case FANOUT_DEV:
1262 if (parent_phy->routing_attr != TABLE_ROUTING ||
1263 child_phy->routing_attr != SUBTRACTIVE_ROUTING) {
1264 sas_print_parent_topology_bug(child, parent_phy, child_phy);
1265 res = -ENODEV;
1266 }
1267 break;
1268 default:
1269 break;
1270 }
1271 }
1272
1273 return res;
1274}
1275
1276#define RRI_REQ_SIZE 16
1277#define RRI_RESP_SIZE 44
1278
1279static int sas_configure_present(struct domain_device *dev, int phy_id,
1280 u8 *sas_addr, int *index, int *present)
1281{
1282 int i, res = 0;
1283 struct expander_device *ex = &dev->ex_dev;
1284 struct ex_phy *phy = &ex->ex_phy[phy_id];
1285 u8 *rri_req;
1286 u8 *rri_resp;
1287
1288 *present = 0;
1289 *index = 0;
1290
1291 rri_req = alloc_smp_req(RRI_REQ_SIZE);
1292 if (!rri_req)
1293 return -ENOMEM;
1294
1295 rri_resp = alloc_smp_resp(RRI_RESP_SIZE);
1296 if (!rri_resp) {
1297 kfree(rri_req);
1298 return -ENOMEM;
1299 }
1300
1301 rri_req[1] = SMP_REPORT_ROUTE_INFO;
1302 rri_req[9] = phy_id;
1303
1304 for (i = 0; i < ex->max_route_indexes ; i++) {
1305 *(__be16 *)(rri_req+6) = cpu_to_be16(i);
1306 res = smp_execute_task(dev, rri_req, RRI_REQ_SIZE, rri_resp,
1307 RRI_RESP_SIZE);
1308 if (res)
1309 goto out;
1310 res = rri_resp[2];
1311 if (res == SMP_RESP_NO_INDEX) {
1312 SAS_DPRINTK("overflow of indexes: dev %016llx "
1313 "phy 0x%x index 0x%x\n",
1314 SAS_ADDR(dev->sas_addr), phy_id, i);
1315 goto out;
1316 } else if (res != SMP_RESP_FUNC_ACC) {
1317 SAS_DPRINTK("%s: dev %016llx phy 0x%x index 0x%x "
cadbd4a5 1318 "result 0x%x\n", __func__,
2908d778
JB
1319 SAS_ADDR(dev->sas_addr), phy_id, i, res);
1320 goto out;
1321 }
1322 if (SAS_ADDR(sas_addr) != 0) {
1323 if (SAS_ADDR(rri_resp+16) == SAS_ADDR(sas_addr)) {
1324 *index = i;
1325 if ((rri_resp[12] & 0x80) == 0x80)
1326 *present = 0;
1327 else
1328 *present = 1;
1329 goto out;
1330 } else if (SAS_ADDR(rri_resp+16) == 0) {
1331 *index = i;
1332 *present = 0;
1333 goto out;
1334 }
1335 } else if (SAS_ADDR(rri_resp+16) == 0 &&
1336 phy->last_da_index < i) {
1337 phy->last_da_index = i;
1338 *index = i;
1339 *present = 0;
1340 goto out;
1341 }
1342 }
1343 res = -1;
1344out:
1345 kfree(rri_req);
1346 kfree(rri_resp);
1347 return res;
1348}
1349
1350#define CRI_REQ_SIZE 44
1351#define CRI_RESP_SIZE 8
1352
1353static int sas_configure_set(struct domain_device *dev, int phy_id,
1354 u8 *sas_addr, int index, int include)
1355{
1356 int res;
1357 u8 *cri_req;
1358 u8 *cri_resp;
1359
1360 cri_req = alloc_smp_req(CRI_REQ_SIZE);
1361 if (!cri_req)
1362 return -ENOMEM;
1363
1364 cri_resp = alloc_smp_resp(CRI_RESP_SIZE);
1365 if (!cri_resp) {
1366 kfree(cri_req);
1367 return -ENOMEM;
1368 }
1369
1370 cri_req[1] = SMP_CONF_ROUTE_INFO;
1371 *(__be16 *)(cri_req+6) = cpu_to_be16(index);
1372 cri_req[9] = phy_id;
1373 if (SAS_ADDR(sas_addr) == 0 || !include)
1374 cri_req[12] |= 0x80;
1375 memcpy(cri_req+16, sas_addr, SAS_ADDR_SIZE);
1376
1377 res = smp_execute_task(dev, cri_req, CRI_REQ_SIZE, cri_resp,
1378 CRI_RESP_SIZE);
1379 if (res)
1380 goto out;
1381 res = cri_resp[2];
1382 if (res == SMP_RESP_NO_INDEX) {
1383 SAS_DPRINTK("overflow of indexes: dev %016llx phy 0x%x "
1384 "index 0x%x\n",
1385 SAS_ADDR(dev->sas_addr), phy_id, index);
1386 }
1387out:
1388 kfree(cri_req);
1389 kfree(cri_resp);
1390 return res;
1391}
1392
1393static int sas_configure_phy(struct domain_device *dev, int phy_id,
1394 u8 *sas_addr, int include)
1395{
1396 int index;
1397 int present;
1398 int res;
1399
1400 res = sas_configure_present(dev, phy_id, sas_addr, &index, &present);
1401 if (res)
1402 return res;
1403 if (include ^ present)
1404 return sas_configure_set(dev, phy_id, sas_addr, index,include);
1405
1406 return res;
1407}
1408
1409/**
1410 * sas_configure_parent -- configure routing table of parent
1411 * parent: parent expander
1412 * child: child expander
1413 * sas_addr: SAS port identifier of device directly attached to child
1414 */
1415static int sas_configure_parent(struct domain_device *parent,
1416 struct domain_device *child,
1417 u8 *sas_addr, int include)
1418{
1419 struct expander_device *ex_parent = &parent->ex_dev;
1420 int res = 0;
1421 int i;
1422
1423 if (parent->parent) {
1424 res = sas_configure_parent(parent->parent, parent, sas_addr,
1425 include);
1426 if (res)
1427 return res;
1428 }
1429
1430 if (ex_parent->conf_route_table == 0) {
1431 SAS_DPRINTK("ex %016llx has self-configuring routing table\n",
1432 SAS_ADDR(parent->sas_addr));
1433 return 0;
1434 }
1435
1436 for (i = 0; i < ex_parent->num_phys; i++) {
1437 struct ex_phy *phy = &ex_parent->ex_phy[i];
1438
1439 if ((phy->routing_attr == TABLE_ROUTING) &&
1440 (SAS_ADDR(phy->attached_sas_addr) ==
1441 SAS_ADDR(child->sas_addr))) {
1442 res = sas_configure_phy(parent, i, sas_addr, include);
1443 if (res)
1444 return res;
1445 }
1446 }
1447
1448 return res;
1449}
1450
1451/**
1452 * sas_configure_routing -- configure routing
1453 * dev: expander device
1454 * sas_addr: port identifier of device directly attached to the expander device
1455 */
1456static int sas_configure_routing(struct domain_device *dev, u8 *sas_addr)
1457{
1458 if (dev->parent)
1459 return sas_configure_parent(dev->parent, dev, sas_addr, 1);
1460 return 0;
1461}
1462
1463static int sas_disable_routing(struct domain_device *dev, u8 *sas_addr)
1464{
1465 if (dev->parent)
1466 return sas_configure_parent(dev->parent, dev, sas_addr, 0);
1467 return 0;
1468}
1469
2908d778
JB
1470/**
1471 * sas_discover_expander -- expander discovery
1472 * @ex: pointer to expander domain device
1473 *
1474 * See comment in sas_discover_sata().
1475 */
1476static int sas_discover_expander(struct domain_device *dev)
1477{
1478 int res;
1479
1480 res = sas_notify_lldd_dev_found(dev);
1481 if (res)
1482 return res;
1483
1484 res = sas_ex_general(dev);
1485 if (res)
1486 goto out_err;
1487 res = sas_ex_manuf_info(dev);
1488 if (res)
1489 goto out_err;
1490
1491 res = sas_expander_discover(dev);
1492 if (res) {
1493 SAS_DPRINTK("expander %016llx discovery failed(0x%x)\n",
1494 SAS_ADDR(dev->sas_addr), res);
1495 goto out_err;
1496 }
1497
1498 sas_check_ex_subtractive_boundary(dev);
1499 res = sas_check_parent_topology(dev);
1500 if (res)
1501 goto out_err;
1502 return 0;
1503out_err:
1504 sas_notify_lldd_dev_gone(dev);
1505 return res;
1506}
1507
1508static int sas_ex_level_discovery(struct asd_sas_port *port, const int level)
1509{
1510 int res = 0;
1511 struct domain_device *dev;
1512
1513 list_for_each_entry(dev, &port->dev_list, dev_list_node) {
1514 if (dev->dev_type == EDGE_DEV ||
1515 dev->dev_type == FANOUT_DEV) {
1516 struct sas_expander_device *ex =
1517 rphy_to_expander_device(dev->rphy);
1518
1519 if (level == ex->level)
1520 res = sas_ex_discover_devices(dev, -1);
1521 else if (level > 0)
1522 res = sas_ex_discover_devices(port->port_dev, -1);
1523
1524 }
1525 }
1526
1527 return res;
1528}
1529
1530static int sas_ex_bfs_disc(struct asd_sas_port *port)
1531{
1532 int res;
1533 int level;
1534
1535 do {
1536 level = port->disc.max_level;
1537 res = sas_ex_level_discovery(port, level);
1538 mb();
1539 } while (level < port->disc.max_level);
1540
1541 return res;
1542}
1543
1544int sas_discover_root_expander(struct domain_device *dev)
1545{
1546 int res;
1547 struct sas_expander_device *ex = rphy_to_expander_device(dev->rphy);
1548
bf451207
DW
1549 res = sas_rphy_add(dev->rphy);
1550 if (res)
1551 goto out_err;
2908d778
JB
1552
1553 ex->level = dev->port->disc.max_level; /* 0 */
1554 res = sas_discover_expander(dev);
bf451207
DW
1555 if (res)
1556 goto out_err2;
1557
1558 sas_ex_bfs_disc(dev->port);
2908d778
JB
1559
1560 return res;
bf451207
DW
1561
1562out_err2:
6f63caae 1563 sas_rphy_remove(dev->rphy);
bf451207 1564out_err:
bf451207 1565 return res;
2908d778
JB
1566}
1567
1568/* ---------- Domain revalidation ---------- */
1569
1570static int sas_get_phy_discover(struct domain_device *dev,
1571 int phy_id, struct smp_resp *disc_resp)
1572{
1573 int res;
1574 u8 *disc_req;
1575
1576 disc_req = alloc_smp_req(DISCOVER_REQ_SIZE);
1577 if (!disc_req)
1578 return -ENOMEM;
1579
1580 disc_req[1] = SMP_DISCOVER;
1581 disc_req[9] = phy_id;
1582
1583 res = smp_execute_task(dev, disc_req, DISCOVER_REQ_SIZE,
1584 disc_resp, DISCOVER_RESP_SIZE);
1585 if (res)
1586 goto out;
1587 else if (disc_resp->result != SMP_RESP_FUNC_ACC) {
1588 res = disc_resp->result;
1589 goto out;
1590 }
1591out:
1592 kfree(disc_req);
1593 return res;
1594}
1595
1596static int sas_get_phy_change_count(struct domain_device *dev,
1597 int phy_id, int *pcc)
1598{
1599 int res;
1600 struct smp_resp *disc_resp;
1601
1602 disc_resp = alloc_smp_resp(DISCOVER_RESP_SIZE);
1603 if (!disc_resp)
1604 return -ENOMEM;
1605
1606 res = sas_get_phy_discover(dev, phy_id, disc_resp);
1607 if (!res)
1608 *pcc = disc_resp->disc.change_count;
1609
1610 kfree(disc_resp);
1611 return res;
1612}
1613
1614static int sas_get_phy_attached_sas_addr(struct domain_device *dev,
1615 int phy_id, u8 *attached_sas_addr)
1616{
1617 int res;
1618 struct smp_resp *disc_resp;
1619 struct discover_resp *dr;
1620
1621 disc_resp = alloc_smp_resp(DISCOVER_RESP_SIZE);
1622 if (!disc_resp)
1623 return -ENOMEM;
1624 dr = &disc_resp->disc;
1625
1626 res = sas_get_phy_discover(dev, phy_id, disc_resp);
1627 if (!res) {
1628 memcpy(attached_sas_addr,disc_resp->disc.attached_sas_addr,8);
1629 if (dr->attached_dev_type == 0)
1630 memset(attached_sas_addr, 0, 8);
1631 }
1632 kfree(disc_resp);
1633 return res;
1634}
1635
1636static int sas_find_bcast_phy(struct domain_device *dev, int *phy_id,
19252de6 1637 int from_phy, bool update)
2908d778
JB
1638{
1639 struct expander_device *ex = &dev->ex_dev;
1640 int res = 0;
1641 int i;
1642
1643 for (i = from_phy; i < ex->num_phys; i++) {
1644 int phy_change_count = 0;
1645
1646 res = sas_get_phy_change_count(dev, i, &phy_change_count);
1647 if (res)
1648 goto out;
1649 else if (phy_change_count != ex->ex_phy[i].phy_change_count) {
19252de6
TP
1650 if (update)
1651 ex->ex_phy[i].phy_change_count =
1652 phy_change_count;
2908d778
JB
1653 *phy_id = i;
1654 return 0;
1655 }
1656 }
1657out:
1658 return res;
1659}
1660
1661static int sas_get_ex_change_count(struct domain_device *dev, int *ecc)
1662{
1663 int res;
1664 u8 *rg_req;
1665 struct smp_resp *rg_resp;
1666
1667 rg_req = alloc_smp_req(RG_REQ_SIZE);
1668 if (!rg_req)
1669 return -ENOMEM;
1670
1671 rg_resp = alloc_smp_resp(RG_RESP_SIZE);
1672 if (!rg_resp) {
1673 kfree(rg_req);
1674 return -ENOMEM;
1675 }
1676
1677 rg_req[1] = SMP_REPORT_GENERAL;
1678
1679 res = smp_execute_task(dev, rg_req, RG_REQ_SIZE, rg_resp,
1680 RG_RESP_SIZE);
1681 if (res)
1682 goto out;
1683 if (rg_resp->result != SMP_RESP_FUNC_ACC) {
1684 res = rg_resp->result;
1685 goto out;
1686 }
1687
1688 *ecc = be16_to_cpu(rg_resp->rg.change_count);
1689out:
1690 kfree(rg_resp);
1691 kfree(rg_req);
1692 return res;
1693}
19252de6
TP
1694/**
1695 * sas_find_bcast_dev - find the device issue BROADCAST(CHANGE).
1696 * @dev:domain device to be detect.
1697 * @src_dev: the device which originated BROADCAST(CHANGE).
1698 *
1699 * Add self-configuration expander suport. Suppose two expander cascading,
1700 * when the first level expander is self-configuring, hotplug the disks in
1701 * second level expander, BROADCAST(CHANGE) will not only be originated
1702 * in the second level expander, but also be originated in the first level
1703 * expander (see SAS protocol SAS 2r-14, 7.11 for detail), it is to say,
1704 * expander changed count in two level expanders will all increment at least
1705 * once, but the phy which chang count has changed is the source device which
1706 * we concerned.
1707 */
2908d778
JB
1708
1709static int sas_find_bcast_dev(struct domain_device *dev,
1710 struct domain_device **src_dev)
1711{
1712 struct expander_device *ex = &dev->ex_dev;
1713 int ex_change_count = -1;
19252de6 1714 int phy_id = -1;
2908d778 1715 int res;
19252de6 1716 struct domain_device *ch;
2908d778
JB
1717
1718 res = sas_get_ex_change_count(dev, &ex_change_count);
1719 if (res)
1720 goto out;
19252de6
TP
1721 if (ex_change_count != -1 && ex_change_count != ex->ex_change_count) {
1722 /* Just detect if this expander phys phy change count changed,
1723 * in order to determine if this expander originate BROADCAST,
1724 * and do not update phy change count field in our structure.
1725 */
1726 res = sas_find_bcast_phy(dev, &phy_id, 0, false);
1727 if (phy_id != -1) {
1728 *src_dev = dev;
1729 ex->ex_change_count = ex_change_count;
1730 SAS_DPRINTK("Expander phy change count has changed\n");
1731 return res;
1732 } else
1733 SAS_DPRINTK("Expander phys DID NOT change\n");
1734 }
1735 list_for_each_entry(ch, &ex->children, siblings) {
1736 if (ch->dev_type == EDGE_DEV || ch->dev_type == FANOUT_DEV) {
1737 res = sas_find_bcast_dev(ch, src_dev);
24926dad 1738 if (*src_dev)
19252de6 1739 return res;
2908d778
JB
1740 }
1741 }
1742out:
1743 return res;
1744}
1745
1a34c064 1746static void sas_unregister_ex_tree(struct asd_sas_port *port, struct domain_device *dev)
2908d778
JB
1747{
1748 struct expander_device *ex = &dev->ex_dev;
1749 struct domain_device *child, *n;
1750
1751 list_for_each_entry_safe(child, n, &ex->children, siblings) {
e139942d 1752 set_bit(SAS_DEV_GONE, &child->state);
2908d778
JB
1753 if (child->dev_type == EDGE_DEV ||
1754 child->dev_type == FANOUT_DEV)
1a34c064 1755 sas_unregister_ex_tree(port, child);
2908d778 1756 else
1a34c064 1757 sas_unregister_dev(port, child);
2908d778 1758 }
1a34c064 1759 sas_unregister_dev(port, dev);
2908d778
JB
1760}
1761
1762static void sas_unregister_devs_sas_addr(struct domain_device *parent,
19252de6 1763 int phy_id, bool last)
2908d778
JB
1764{
1765 struct expander_device *ex_dev = &parent->ex_dev;
1766 struct ex_phy *phy = &ex_dev->ex_phy[phy_id];
1767 struct domain_device *child, *n;
19252de6
TP
1768 if (last) {
1769 list_for_each_entry_safe(child, n,
1770 &ex_dev->children, siblings) {
1771 if (SAS_ADDR(child->sas_addr) ==
1772 SAS_ADDR(phy->attached_sas_addr)) {
e139942d 1773 set_bit(SAS_DEV_GONE, &child->state);
19252de6
TP
1774 if (child->dev_type == EDGE_DEV ||
1775 child->dev_type == FANOUT_DEV)
1a34c064 1776 sas_unregister_ex_tree(parent->port, child);
19252de6 1777 else
1a34c064 1778 sas_unregister_dev(parent->port, child);
19252de6
TP
1779 break;
1780 }
2908d778 1781 }
e139942d 1782 set_bit(SAS_DEV_GONE, &parent->state);
19252de6 1783 sas_disable_routing(parent, phy->attached_sas_addr);
2908d778 1784 }
2908d778 1785 memset(phy->attached_sas_addr, 0, SAS_ADDR_SIZE);
a73914c3
MS
1786 if (phy->port) {
1787 sas_port_delete_phy(phy->port, phy->phy);
1788 if (phy->port->num_phys == 0)
1789 sas_port_delete(phy->port);
1790 phy->port = NULL;
1791 }
2908d778
JB
1792}
1793
1794static int sas_discover_bfs_by_root_level(struct domain_device *root,
1795 const int level)
1796{
1797 struct expander_device *ex_root = &root->ex_dev;
1798 struct domain_device *child;
1799 int res = 0;
1800
1801 list_for_each_entry(child, &ex_root->children, siblings) {
1802 if (child->dev_type == EDGE_DEV ||
1803 child->dev_type == FANOUT_DEV) {
1804 struct sas_expander_device *ex =
1805 rphy_to_expander_device(child->rphy);
1806
1807 if (level > ex->level)
1808 res = sas_discover_bfs_by_root_level(child,
1809 level);
1810 else if (level == ex->level)
1811 res = sas_ex_discover_devices(child, -1);
1812 }
1813 }
1814 return res;
1815}
1816
1817static int sas_discover_bfs_by_root(struct domain_device *dev)
1818{
1819 int res;
1820 struct sas_expander_device *ex = rphy_to_expander_device(dev->rphy);
1821 int level = ex->level+1;
1822
1823 res = sas_ex_discover_devices(dev, -1);
1824 if (res)
1825 goto out;
1826 do {
1827 res = sas_discover_bfs_by_root_level(dev, level);
1828 mb();
1829 level += 1;
1830 } while (level <= dev->port->disc.max_level);
1831out:
1832 return res;
1833}
1834
1835static int sas_discover_new(struct domain_device *dev, int phy_id)
1836{
1837 struct ex_phy *ex_phy = &dev->ex_dev.ex_phy[phy_id];
1838 struct domain_device *child;
19252de6
TP
1839 bool found = false;
1840 int res, i;
2908d778
JB
1841
1842 SAS_DPRINTK("ex %016llx phy%d new device attached\n",
1843 SAS_ADDR(dev->sas_addr), phy_id);
1844 res = sas_ex_phy_discover(dev, phy_id);
1845 if (res)
1846 goto out;
19252de6
TP
1847 /* to support the wide port inserted */
1848 for (i = 0; i < dev->ex_dev.num_phys; i++) {
1849 struct ex_phy *ex_phy_temp = &dev->ex_dev.ex_phy[i];
1850 if (i == phy_id)
1851 continue;
1852 if (SAS_ADDR(ex_phy_temp->attached_sas_addr) ==
1853 SAS_ADDR(ex_phy->attached_sas_addr)) {
1854 found = true;
1855 break;
1856 }
1857 }
1858 if (found) {
1859 sas_ex_join_wide_port(dev, phy_id);
1860 return 0;
1861 }
2908d778 1862 res = sas_ex_discover_devices(dev, phy_id);
19252de6 1863 if (!res)
2908d778
JB
1864 goto out;
1865 list_for_each_entry(child, &dev->ex_dev.children, siblings) {
1866 if (SAS_ADDR(child->sas_addr) ==
1867 SAS_ADDR(ex_phy->attached_sas_addr)) {
1868 if (child->dev_type == EDGE_DEV ||
1869 child->dev_type == FANOUT_DEV)
1870 res = sas_discover_bfs_by_root(child);
1871 break;
1872 }
1873 }
1874out:
1875 return res;
1876}
1877
19252de6 1878static int sas_rediscover_dev(struct domain_device *dev, int phy_id, bool last)
2908d778
JB
1879{
1880 struct expander_device *ex = &dev->ex_dev;
1881 struct ex_phy *phy = &ex->ex_phy[phy_id];
1882 u8 attached_sas_addr[8];
1883 int res;
1884
1885 res = sas_get_phy_attached_sas_addr(dev, phy_id, attached_sas_addr);
1886 switch (res) {
1887 case SMP_RESP_NO_PHY:
1888 phy->phy_state = PHY_NOT_PRESENT;
19252de6 1889 sas_unregister_devs_sas_addr(dev, phy_id, last);
2908d778
JB
1890 goto out; break;
1891 case SMP_RESP_PHY_VACANT:
1892 phy->phy_state = PHY_VACANT;
19252de6 1893 sas_unregister_devs_sas_addr(dev, phy_id, last);
2908d778
JB
1894 goto out; break;
1895 case SMP_RESP_FUNC_ACC:
1896 break;
1897 }
1898
1899 if (SAS_ADDR(attached_sas_addr) == 0) {
1900 phy->phy_state = PHY_EMPTY;
19252de6 1901 sas_unregister_devs_sas_addr(dev, phy_id, last);
2908d778
JB
1902 } else if (SAS_ADDR(attached_sas_addr) ==
1903 SAS_ADDR(phy->attached_sas_addr)) {
1904 SAS_DPRINTK("ex %016llx phy 0x%x broadcast flutter\n",
1905 SAS_ADDR(dev->sas_addr), phy_id);
a01e70e5 1906 sas_ex_phy_discover(dev, phy_id);
2908d778
JB
1907 } else
1908 res = sas_discover_new(dev, phy_id);
1909out:
1910 return res;
1911}
1912
19252de6
TP
1913/**
1914 * sas_rediscover - revalidate the domain.
1915 * @dev:domain device to be detect.
1916 * @phy_id: the phy id will be detected.
1917 *
1918 * NOTE: this process _must_ quit (return) as soon as any connection
1919 * errors are encountered. Connection recovery is done elsewhere.
1920 * Discover process only interrogates devices in order to discover the
1921 * domain.For plugging out, we un-register the device only when it is
1922 * the last phy in the port, for other phys in this port, we just delete it
1923 * from the port.For inserting, we do discovery when it is the
1924 * first phy,for other phys in this port, we add it to the port to
1925 * forming the wide-port.
1926 */
2908d778
JB
1927static int sas_rediscover(struct domain_device *dev, const int phy_id)
1928{
1929 struct expander_device *ex = &dev->ex_dev;
1930 struct ex_phy *changed_phy = &ex->ex_phy[phy_id];
1931 int res = 0;
1932 int i;
19252de6 1933 bool last = true; /* is this the last phy of the port */
2908d778
JB
1934
1935 SAS_DPRINTK("ex %016llx phy%d originated BROADCAST(CHANGE)\n",
1936 SAS_ADDR(dev->sas_addr), phy_id);
1937
1938 if (SAS_ADDR(changed_phy->attached_sas_addr) != 0) {
1939 for (i = 0; i < ex->num_phys; i++) {
1940 struct ex_phy *phy = &ex->ex_phy[i];
1941
1942 if (i == phy_id)
1943 continue;
1944 if (SAS_ADDR(phy->attached_sas_addr) ==
1945 SAS_ADDR(changed_phy->attached_sas_addr)) {
1946 SAS_DPRINTK("phy%d part of wide port with "
1947 "phy%d\n", phy_id, i);
19252de6
TP
1948 last = false;
1949 break;
2908d778
JB
1950 }
1951 }
19252de6 1952 res = sas_rediscover_dev(dev, phy_id, last);
2908d778
JB
1953 } else
1954 res = sas_discover_new(dev, phy_id);
2908d778
JB
1955 return res;
1956}
1957
1958/**
1959 * sas_revalidate_domain -- revalidate the domain
1960 * @port: port to the domain of interest
1961 *
1962 * NOTE: this process _must_ quit (return) as soon as any connection
1963 * errors are encountered. Connection recovery is done elsewhere.
1964 * Discover process only interrogates devices in order to discover the
1965 * domain.
1966 */
1967int sas_ex_revalidate_domain(struct domain_device *port_dev)
1968{
1969 int res;
1970 struct domain_device *dev = NULL;
1971
1972 res = sas_find_bcast_dev(port_dev, &dev);
1973 if (res)
1974 goto out;
1975 if (dev) {
1976 struct expander_device *ex = &dev->ex_dev;
1977 int i = 0, phy_id;
1978
1979 do {
1980 phy_id = -1;
19252de6 1981 res = sas_find_bcast_phy(dev, &phy_id, i, true);
2908d778
JB
1982 if (phy_id == -1)
1983 break;
1984 res = sas_rediscover(dev, phy_id);
1985 i = phy_id + 1;
1986 } while (i < ex->num_phys);
1987 }
1988out:
1989 return res;
1990}
1991
ba1fc175
FT
1992int sas_smp_handler(struct Scsi_Host *shost, struct sas_rphy *rphy,
1993 struct request *req)
1994{
1995 struct domain_device *dev;
2cd614c8 1996 int ret, type;
ba1fc175
FT
1997 struct request *rsp = req->next_rq;
1998
1999 if (!rsp) {
2000 printk("%s: space for a smp response is missing\n",
cadbd4a5 2001 __func__);
ba1fc175
FT
2002 return -EINVAL;
2003 }
2004
2cd614c8 2005 /* no rphy means no smp target support (ie aic94xx host) */
b98e66fa
JB
2006 if (!rphy)
2007 return sas_smp_host_handler(shost, req, rsp);
2008
2cd614c8 2009 type = rphy->identify.device_type;
ba1fc175
FT
2010
2011 if (type != SAS_EDGE_EXPANDER_DEVICE &&
2012 type != SAS_FANOUT_EXPANDER_DEVICE) {
2013 printk("%s: can we send a smp request to a device?\n",
cadbd4a5 2014 __func__);
ba1fc175
FT
2015 return -EINVAL;
2016 }
2017
2018 dev = sas_find_dev_by_rphy(rphy);
2019 if (!dev) {
cadbd4a5 2020 printk("%s: fail to find a domain_device?\n", __func__);
ba1fc175
FT
2021 return -EINVAL;
2022 }
2023
2024 /* do we need to support multiple segments? */
2025 if (req->bio->bi_vcnt > 1 || rsp->bio->bi_vcnt > 1) {
2026 printk("%s: multiple segments req %u %u, rsp %u %u\n",
b0790410
TH
2027 __func__, req->bio->bi_vcnt, blk_rq_bytes(req),
2028 rsp->bio->bi_vcnt, blk_rq_bytes(rsp));
ba1fc175
FT
2029 return -EINVAL;
2030 }
2031
b0790410
TH
2032 ret = smp_execute_task(dev, bio_data(req->bio), blk_rq_bytes(req),
2033 bio_data(rsp->bio), blk_rq_bytes(rsp));
2d4b63e1
JB
2034 if (ret > 0) {
2035 /* positive number is the untransferred residual */
c3a4d78c 2036 rsp->resid_len = ret;
5f49f631 2037 req->resid_len = 0;
2d4b63e1 2038 ret = 0;
5f49f631
TH
2039 } else if (ret == 0) {
2040 rsp->resid_len = 0;
2041 req->resid_len = 0;
2d4b63e1 2042 }
ba1fc175
FT
2043
2044 return ret;
2045}
This page took 0.584566 seconds and 5 git commands to generate.