blob: 295941e41433d30bf821d26293f8fba6b91ffef8 [file] [log] [blame]
b.liue9582032025-04-17 19:18:16 +08001// SPDX-License-Identifier: GPL-2.0
2/*
3 * Serial Attached SCSI (SAS) Expander discovery and configuration
4 *
5 * Copyright (C) 2005 Adaptec, Inc. All rights reserved.
6 * Copyright (C) 2005 Luben Tuikov <luben_tuikov@adaptec.com>
7 *
8 * This file is licensed under GPLv2.
9 */
10
11#include <linux/scatterlist.h>
12#include <linux/blkdev.h>
13#include <linux/slab.h>
14#include <asm/unaligned.h>
15
16#include "sas_internal.h"
17
18#include <scsi/sas_ata.h>
19#include <scsi/scsi_transport.h>
20#include <scsi/scsi_transport_sas.h>
21#include "../scsi_sas_internal.h"
22
23static int sas_discover_expander(struct domain_device *dev);
24static int sas_configure_routing(struct domain_device *dev, u8 *sas_addr);
25static int sas_configure_phy(struct domain_device *dev, int phy_id,
26 u8 *sas_addr, int include);
27static int sas_disable_routing(struct domain_device *dev, u8 *sas_addr);
28
29/* ---------- SMP task management ---------- */
30
31static void smp_task_timedout(struct timer_list *t)
32{
33 struct sas_task_slow *slow = from_timer(slow, t, timer);
34 struct sas_task *task = slow->task;
35 unsigned long flags;
36
37 spin_lock_irqsave(&task->task_state_lock, flags);
38 if (!(task->task_state_flags & SAS_TASK_STATE_DONE)) {
39 task->task_state_flags |= SAS_TASK_STATE_ABORTED;
40 complete(&task->slow_task->completion);
41 }
42 spin_unlock_irqrestore(&task->task_state_lock, flags);
43}
44
45static void smp_task_done(struct sas_task *task)
46{
47 del_timer(&task->slow_task->timer);
48 complete(&task->slow_task->completion);
49}
50
51/* Give it some long enough timeout. In seconds. */
52#define SMP_TIMEOUT 10
53
54static int smp_execute_task_sg(struct domain_device *dev,
55 struct scatterlist *req, struct scatterlist *resp)
56{
57 int res, retry;
58 struct sas_task *task = NULL;
59 struct sas_internal *i =
60 to_sas_internal(dev->port->ha->core.shost->transportt);
61
62 mutex_lock(&dev->ex_dev.cmd_mutex);
63 for (retry = 0; retry < 3; retry++) {
64 if (test_bit(SAS_DEV_GONE, &dev->state)) {
65 res = -ECOMM;
66 break;
67 }
68
69 task = sas_alloc_slow_task(GFP_KERNEL);
70 if (!task) {
71 res = -ENOMEM;
72 break;
73 }
74 task->dev = dev;
75 task->task_proto = dev->tproto;
76 task->smp_task.smp_req = *req;
77 task->smp_task.smp_resp = *resp;
78
79 task->task_done = smp_task_done;
80
81 task->slow_task->timer.function = smp_task_timedout;
82 task->slow_task->timer.expires = jiffies + SMP_TIMEOUT*HZ;
83 add_timer(&task->slow_task->timer);
84
85 res = i->dft->lldd_execute_task(task, GFP_KERNEL);
86
87 if (res) {
88 del_timer_sync(&task->slow_task->timer);
89 pr_notice("executing SMP task failed:%d\n", res);
90 break;
91 }
92
93 wait_for_completion(&task->slow_task->completion);
94 res = -ECOMM;
95 if ((task->task_state_flags & SAS_TASK_STATE_ABORTED)) {
96 pr_notice("smp task timed out or aborted\n");
97 i->dft->lldd_abort_task(task);
98 if (!(task->task_state_flags & SAS_TASK_STATE_DONE)) {
99 pr_notice("SMP task aborted and not done\n");
100 break;
101 }
102 }
103 if (task->task_status.resp == SAS_TASK_COMPLETE &&
104 task->task_status.stat == SAM_STAT_GOOD) {
105 res = 0;
106 break;
107 }
108 if (task->task_status.resp == SAS_TASK_COMPLETE &&
109 task->task_status.stat == SAS_DATA_UNDERRUN) {
110 /* no error, but return the number of bytes of
111 * underrun */
112 res = task->task_status.residual;
113 break;
114 }
115 if (task->task_status.resp == SAS_TASK_COMPLETE &&
116 task->task_status.stat == SAS_DATA_OVERRUN) {
117 res = -EMSGSIZE;
118 break;
119 }
120 if (task->task_status.resp == SAS_TASK_UNDELIVERED &&
121 task->task_status.stat == SAS_DEVICE_UNKNOWN)
122 break;
123 else {
124 pr_notice("%s: task to dev %016llx response: 0x%x status 0x%x\n",
125 __func__,
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 }
132 }
133 mutex_unlock(&dev->ex_dev.cmd_mutex);
134
135 BUG_ON(retry == 3 && task != NULL);
136 sas_free_task(task);
137 return res;
138}
139
140static int smp_execute_task(struct domain_device *dev, void *req, int req_size,
141 void *resp, int resp_size)
142{
143 struct scatterlist req_sg;
144 struct scatterlist resp_sg;
145
146 sg_init_one(&req_sg, req, req_size);
147 sg_init_one(&resp_sg, resp, resp_size);
148 return smp_execute_task_sg(dev, &req_sg, &resp_sg);
149}
150
151/* ---------- Allocations ---------- */
152
153static inline void *alloc_smp_req(int size)
154{
155 u8 *p = kzalloc(size, GFP_KERNEL);
156 if (p)
157 p[0] = SMP_REQUEST;
158 return p;
159}
160
161static inline void *alloc_smp_resp(int size)
162{
163 return kzalloc(size, GFP_KERNEL);
164}
165
166static char sas_route_char(struct domain_device *dev, struct ex_phy *phy)
167{
168 switch (phy->routing_attr) {
169 case TABLE_ROUTING:
170 if (dev->ex_dev.t2t_supp)
171 return 'U';
172 else
173 return 'T';
174 case DIRECT_ROUTING:
175 return 'D';
176 case SUBTRACTIVE_ROUTING:
177 return 'S';
178 default:
179 return '?';
180 }
181}
182
183static enum sas_device_type to_dev_type(struct discover_resp *dr)
184{
185 /* This is detecting a failure to transmit initial dev to host
186 * FIS as described in section J.5 of sas-2 r16
187 */
188 if (dr->attached_dev_type == SAS_PHY_UNUSED && dr->attached_sata_dev &&
189 dr->linkrate >= SAS_LINK_RATE_1_5_GBPS)
190 return SAS_SATA_PENDING;
191 else
192 return dr->attached_dev_type;
193}
194
195static void sas_set_ex_phy(struct domain_device *dev, int phy_id, void *rsp)
196{
197 enum sas_device_type dev_type;
198 enum sas_linkrate linkrate;
199 u8 sas_addr[SAS_ADDR_SIZE];
200 struct smp_resp *resp = rsp;
201 struct discover_resp *dr = &resp->disc;
202 struct sas_ha_struct *ha = dev->port->ha;
203 struct expander_device *ex = &dev->ex_dev;
204 struct ex_phy *phy = &ex->ex_phy[phy_id];
205 struct sas_rphy *rphy = dev->rphy;
206 bool new_phy = !phy->phy;
207 char *type;
208
209 if (new_phy) {
210 if (WARN_ON_ONCE(test_bit(SAS_HA_ATA_EH_ACTIVE, &ha->state)))
211 return;
212 phy->phy = sas_phy_alloc(&rphy->dev, phy_id);
213
214 /* FIXME: error_handling */
215 BUG_ON(!phy->phy);
216 }
217
218 switch (resp->result) {
219 case SMP_RESP_PHY_VACANT:
220 phy->phy_state = PHY_VACANT;
221 break;
222 default:
223 phy->phy_state = PHY_NOT_PRESENT;
224 break;
225 case SMP_RESP_FUNC_ACC:
226 phy->phy_state = PHY_EMPTY; /* do not know yet */
227 break;
228 }
229
230 /* check if anything important changed to squelch debug */
231 dev_type = phy->attached_dev_type;
232 linkrate = phy->linkrate;
233 memcpy(sas_addr, phy->attached_sas_addr, SAS_ADDR_SIZE);
234
235 /* Handle vacant phy - rest of dr data is not valid so skip it */
236 if (phy->phy_state == PHY_VACANT) {
237 memset(phy->attached_sas_addr, 0, SAS_ADDR_SIZE);
238 phy->attached_dev_type = SAS_PHY_UNUSED;
239 if (!test_bit(SAS_HA_ATA_EH_ACTIVE, &ha->state)) {
240 phy->phy_id = phy_id;
241 goto skip;
242 } else
243 goto out;
244 }
245
246 phy->attached_dev_type = to_dev_type(dr);
247 if (test_bit(SAS_HA_ATA_EH_ACTIVE, &ha->state))
248 goto out;
249 phy->phy_id = phy_id;
250 phy->linkrate = dr->linkrate;
251 phy->attached_sata_host = dr->attached_sata_host;
252 phy->attached_sata_dev = dr->attached_sata_dev;
253 phy->attached_sata_ps = dr->attached_sata_ps;
254 phy->attached_iproto = dr->iproto << 1;
255 phy->attached_tproto = dr->tproto << 1;
256 /* help some expanders that fail to zero sas_address in the 'no
257 * device' case
258 */
259 if (phy->attached_dev_type == SAS_PHY_UNUSED)
260 memset(phy->attached_sas_addr, 0, SAS_ADDR_SIZE);
261 else
262 memcpy(phy->attached_sas_addr, dr->attached_sas_addr, SAS_ADDR_SIZE);
263 phy->attached_phy_id = dr->attached_phy_id;
264 phy->phy_change_count = dr->change_count;
265 phy->routing_attr = dr->routing_attr;
266 phy->virtual = dr->virtual;
267 phy->last_da_index = -1;
268
269 phy->phy->identify.sas_address = SAS_ADDR(phy->attached_sas_addr);
270 phy->phy->identify.device_type = dr->attached_dev_type;
271 phy->phy->identify.initiator_port_protocols = phy->attached_iproto;
272 phy->phy->identify.target_port_protocols = phy->attached_tproto;
273 if (!phy->attached_tproto && dr->attached_sata_dev)
274 phy->phy->identify.target_port_protocols = SAS_PROTOCOL_SATA;
275 phy->phy->identify.phy_identifier = phy_id;
276 phy->phy->minimum_linkrate_hw = dr->hmin_linkrate;
277 phy->phy->maximum_linkrate_hw = dr->hmax_linkrate;
278 phy->phy->minimum_linkrate = dr->pmin_linkrate;
279 phy->phy->maximum_linkrate = dr->pmax_linkrate;
280 phy->phy->negotiated_linkrate = phy->linkrate;
281 phy->phy->enabled = (phy->linkrate != SAS_PHY_DISABLED);
282
283 skip:
284 if (new_phy)
285 if (sas_phy_add(phy->phy)) {
286 sas_phy_free(phy->phy);
287 return;
288 }
289
290 out:
291 switch (phy->attached_dev_type) {
292 case SAS_SATA_PENDING:
293 type = "stp pending";
294 break;
295 case SAS_PHY_UNUSED:
296 type = "no device";
297 break;
298 case SAS_END_DEVICE:
299 if (phy->attached_iproto) {
300 if (phy->attached_tproto)
301 type = "host+target";
302 else
303 type = "host";
304 } else {
305 if (dr->attached_sata_dev)
306 type = "stp";
307 else
308 type = "ssp";
309 }
310 break;
311 case SAS_EDGE_EXPANDER_DEVICE:
312 case SAS_FANOUT_EXPANDER_DEVICE:
313 type = "smp";
314 break;
315 default:
316 type = "unknown";
317 }
318
319 /* this routine is polled by libata error recovery so filter
320 * unimportant messages
321 */
322 if (new_phy || phy->attached_dev_type != dev_type ||
323 phy->linkrate != linkrate ||
324 SAS_ADDR(phy->attached_sas_addr) != SAS_ADDR(sas_addr))
325 /* pass */;
326 else
327 return;
328
329 /* if the attached device type changed and ata_eh is active,
330 * make sure we run revalidation when eh completes (see:
331 * sas_enable_revalidation)
332 */
333 if (test_bit(SAS_HA_ATA_EH_ACTIVE, &ha->state))
334 set_bit(DISCE_REVALIDATE_DOMAIN, &dev->port->disc.pending);
335
336 pr_debug("%sex %016llx phy%02d:%c:%X attached: %016llx (%s)\n",
337 test_bit(SAS_HA_ATA_EH_ACTIVE, &ha->state) ? "ata: " : "",
338 SAS_ADDR(dev->sas_addr), phy->phy_id,
339 sas_route_char(dev, phy), phy->linkrate,
340 SAS_ADDR(phy->attached_sas_addr), type);
341}
342
343/* check if we have an existing attached ata device on this expander phy */
344struct domain_device *sas_ex_to_ata(struct domain_device *ex_dev, int phy_id)
345{
346 struct ex_phy *ex_phy = &ex_dev->ex_dev.ex_phy[phy_id];
347 struct domain_device *dev;
348 struct sas_rphy *rphy;
349
350 if (!ex_phy->port)
351 return NULL;
352
353 rphy = ex_phy->port->rphy;
354 if (!rphy)
355 return NULL;
356
357 dev = sas_find_dev_by_rphy(rphy);
358
359 if (dev && dev_is_sata(dev))
360 return dev;
361
362 return NULL;
363}
364
365#define DISCOVER_REQ_SIZE 16
366#define DISCOVER_RESP_SIZE 56
367
368static int sas_ex_phy_discover_helper(struct domain_device *dev, u8 *disc_req,
369 u8 *disc_resp, int single)
370{
371 struct discover_resp *dr;
372 int res;
373
374 disc_req[9] = single;
375
376 res = smp_execute_task(dev, disc_req, DISCOVER_REQ_SIZE,
377 disc_resp, DISCOVER_RESP_SIZE);
378 if (res)
379 return res;
380 dr = &((struct smp_resp *)disc_resp)->disc;
381 if (memcmp(dev->sas_addr, dr->attached_sas_addr, SAS_ADDR_SIZE) == 0) {
382 pr_notice("Found loopback topology, just ignore it!\n");
383 return 0;
384 }
385 sas_set_ex_phy(dev, single, disc_resp);
386 return 0;
387}
388
389int sas_ex_phy_discover(struct domain_device *dev, int single)
390{
391 struct expander_device *ex = &dev->ex_dev;
392 int res = 0;
393 u8 *disc_req;
394 u8 *disc_resp;
395
396 disc_req = alloc_smp_req(DISCOVER_REQ_SIZE);
397 if (!disc_req)
398 return -ENOMEM;
399
400 disc_resp = alloc_smp_resp(DISCOVER_RESP_SIZE);
401 if (!disc_resp) {
402 kfree(disc_req);
403 return -ENOMEM;
404 }
405
406 disc_req[1] = SMP_DISCOVER;
407
408 if (0 <= single && single < ex->num_phys) {
409 res = sas_ex_phy_discover_helper(dev, disc_req, disc_resp, single);
410 } else {
411 int i;
412
413 for (i = 0; i < ex->num_phys; i++) {
414 res = sas_ex_phy_discover_helper(dev, disc_req,
415 disc_resp, i);
416 if (res)
417 goto out_err;
418 }
419 }
420out_err:
421 kfree(disc_resp);
422 kfree(disc_req);
423 return res;
424}
425
426static int sas_expander_discover(struct domain_device *dev)
427{
428 struct expander_device *ex = &dev->ex_dev;
429 int res = -ENOMEM;
430
431 ex->ex_phy = kcalloc(ex->num_phys, sizeof(*ex->ex_phy), GFP_KERNEL);
432 if (!ex->ex_phy)
433 return -ENOMEM;
434
435 res = sas_ex_phy_discover(dev, -1);
436 if (res)
437 goto out_err;
438
439 return 0;
440 out_err:
441 kfree(ex->ex_phy);
442 ex->ex_phy = NULL;
443 return res;
444}
445
446#define MAX_EXPANDER_PHYS 128
447
448static void ex_assign_report_general(struct domain_device *dev,
449 struct smp_resp *resp)
450{
451 struct report_general_resp *rg = &resp->rg;
452
453 dev->ex_dev.ex_change_count = be16_to_cpu(rg->change_count);
454 dev->ex_dev.max_route_indexes = be16_to_cpu(rg->route_indexes);
455 dev->ex_dev.num_phys = min(rg->num_phys, (u8)MAX_EXPANDER_PHYS);
456 dev->ex_dev.t2t_supp = rg->t2t_supp;
457 dev->ex_dev.conf_route_table = rg->conf_route_table;
458 dev->ex_dev.configuring = rg->configuring;
459 memcpy(dev->ex_dev.enclosure_logical_id, rg->enclosure_logical_id, 8);
460}
461
462#define RG_REQ_SIZE 8
463#define RG_RESP_SIZE 32
464
465static int sas_ex_general(struct domain_device *dev)
466{
467 u8 *rg_req;
468 struct smp_resp *rg_resp;
469 int res;
470 int i;
471
472 rg_req = alloc_smp_req(RG_REQ_SIZE);
473 if (!rg_req)
474 return -ENOMEM;
475
476 rg_resp = alloc_smp_resp(RG_RESP_SIZE);
477 if (!rg_resp) {
478 kfree(rg_req);
479 return -ENOMEM;
480 }
481
482 rg_req[1] = SMP_REPORT_GENERAL;
483
484 for (i = 0; i < 5; i++) {
485 res = smp_execute_task(dev, rg_req, RG_REQ_SIZE, rg_resp,
486 RG_RESP_SIZE);
487
488 if (res) {
489 pr_notice("RG to ex %016llx failed:0x%x\n",
490 SAS_ADDR(dev->sas_addr), res);
491 goto out;
492 } else if (rg_resp->result != SMP_RESP_FUNC_ACC) {
493 pr_debug("RG:ex %016llx returned SMP result:0x%x\n",
494 SAS_ADDR(dev->sas_addr), rg_resp->result);
495 res = rg_resp->result;
496 goto out;
497 }
498
499 ex_assign_report_general(dev, rg_resp);
500
501 if (dev->ex_dev.configuring) {
502 pr_debug("RG: ex %llx self-configuring...\n",
503 SAS_ADDR(dev->sas_addr));
504 schedule_timeout_interruptible(5*HZ);
505 } else
506 break;
507 }
508out:
509 kfree(rg_req);
510 kfree(rg_resp);
511 return res;
512}
513
514static void ex_assign_manuf_info(struct domain_device *dev, void
515 *_mi_resp)
516{
517 u8 *mi_resp = _mi_resp;
518 struct sas_rphy *rphy = dev->rphy;
519 struct sas_expander_device *edev = rphy_to_expander_device(rphy);
520
521 memcpy(edev->vendor_id, mi_resp + 12, SAS_EXPANDER_VENDOR_ID_LEN);
522 memcpy(edev->product_id, mi_resp + 20, SAS_EXPANDER_PRODUCT_ID_LEN);
523 memcpy(edev->product_rev, mi_resp + 36,
524 SAS_EXPANDER_PRODUCT_REV_LEN);
525
526 if (mi_resp[8] & 1) {
527 memcpy(edev->component_vendor_id, mi_resp + 40,
528 SAS_EXPANDER_COMPONENT_VENDOR_ID_LEN);
529 edev->component_id = mi_resp[48] << 8 | mi_resp[49];
530 edev->component_revision_id = mi_resp[50];
531 }
532}
533
534#define MI_REQ_SIZE 8
535#define MI_RESP_SIZE 64
536
537static int sas_ex_manuf_info(struct domain_device *dev)
538{
539 u8 *mi_req;
540 u8 *mi_resp;
541 int res;
542
543 mi_req = alloc_smp_req(MI_REQ_SIZE);
544 if (!mi_req)
545 return -ENOMEM;
546
547 mi_resp = alloc_smp_resp(MI_RESP_SIZE);
548 if (!mi_resp) {
549 kfree(mi_req);
550 return -ENOMEM;
551 }
552
553 mi_req[1] = SMP_REPORT_MANUF_INFO;
554
555 res = smp_execute_task(dev, mi_req, MI_REQ_SIZE, mi_resp,MI_RESP_SIZE);
556 if (res) {
557 pr_notice("MI: ex %016llx failed:0x%x\n",
558 SAS_ADDR(dev->sas_addr), res);
559 goto out;
560 } else if (mi_resp[2] != SMP_RESP_FUNC_ACC) {
561 pr_debug("MI ex %016llx returned SMP result:0x%x\n",
562 SAS_ADDR(dev->sas_addr), mi_resp[2]);
563 goto out;
564 }
565
566 ex_assign_manuf_info(dev, mi_resp);
567out:
568 kfree(mi_req);
569 kfree(mi_resp);
570 return res;
571}
572
573#define PC_REQ_SIZE 44
574#define PC_RESP_SIZE 8
575
576int sas_smp_phy_control(struct domain_device *dev, int phy_id,
577 enum phy_func phy_func,
578 struct sas_phy_linkrates *rates)
579{
580 u8 *pc_req;
581 u8 *pc_resp;
582 int res;
583
584 pc_req = alloc_smp_req(PC_REQ_SIZE);
585 if (!pc_req)
586 return -ENOMEM;
587
588 pc_resp = alloc_smp_resp(PC_RESP_SIZE);
589 if (!pc_resp) {
590 kfree(pc_req);
591 return -ENOMEM;
592 }
593
594 pc_req[1] = SMP_PHY_CONTROL;
595 pc_req[9] = phy_id;
596 pc_req[10]= phy_func;
597 if (rates) {
598 pc_req[32] = rates->minimum_linkrate << 4;
599 pc_req[33] = rates->maximum_linkrate << 4;
600 }
601
602 res = smp_execute_task(dev, pc_req, PC_REQ_SIZE, pc_resp,PC_RESP_SIZE);
603 if (res) {
604 pr_err("ex %016llx phy%02d PHY control failed: %d\n",
605 SAS_ADDR(dev->sas_addr), phy_id, res);
606 } else if (pc_resp[2] != SMP_RESP_FUNC_ACC) {
607 pr_err("ex %016llx phy%02d PHY control failed: function result 0x%x\n",
608 SAS_ADDR(dev->sas_addr), phy_id, pc_resp[2]);
609 res = pc_resp[2];
610 }
611 kfree(pc_resp);
612 kfree(pc_req);
613 return res;
614}
615
616static void sas_ex_disable_phy(struct domain_device *dev, int phy_id)
617{
618 struct expander_device *ex = &dev->ex_dev;
619 struct ex_phy *phy = &ex->ex_phy[phy_id];
620
621 sas_smp_phy_control(dev, phy_id, PHY_FUNC_DISABLE, NULL);
622 phy->linkrate = SAS_PHY_DISABLED;
623}
624
625static void sas_ex_disable_port(struct domain_device *dev, u8 *sas_addr)
626{
627 struct expander_device *ex = &dev->ex_dev;
628 int i;
629
630 for (i = 0; i < ex->num_phys; i++) {
631 struct ex_phy *phy = &ex->ex_phy[i];
632
633 if (phy->phy_state == PHY_VACANT ||
634 phy->phy_state == PHY_NOT_PRESENT)
635 continue;
636
637 if (SAS_ADDR(phy->attached_sas_addr) == SAS_ADDR(sas_addr))
638 sas_ex_disable_phy(dev, i);
639 }
640}
641
642static int sas_dev_present_in_domain(struct asd_sas_port *port,
643 u8 *sas_addr)
644{
645 struct domain_device *dev;
646
647 if (SAS_ADDR(port->sas_addr) == SAS_ADDR(sas_addr))
648 return 1;
649 list_for_each_entry(dev, &port->dev_list, dev_list_node) {
650 if (SAS_ADDR(dev->sas_addr) == SAS_ADDR(sas_addr))
651 return 1;
652 }
653 return 0;
654}
655
656#define RPEL_REQ_SIZE 16
657#define RPEL_RESP_SIZE 32
658int sas_smp_get_phy_events(struct sas_phy *phy)
659{
660 int res;
661 u8 *req;
662 u8 *resp;
663 struct sas_rphy *rphy = dev_to_rphy(phy->dev.parent);
664 struct domain_device *dev = sas_find_dev_by_rphy(rphy);
665
666 req = alloc_smp_req(RPEL_REQ_SIZE);
667 if (!req)
668 return -ENOMEM;
669
670 resp = alloc_smp_resp(RPEL_RESP_SIZE);
671 if (!resp) {
672 kfree(req);
673 return -ENOMEM;
674 }
675
676 req[1] = SMP_REPORT_PHY_ERR_LOG;
677 req[9] = phy->number;
678
679 res = smp_execute_task(dev, req, RPEL_REQ_SIZE,
680 resp, RPEL_RESP_SIZE);
681
682 if (res)
683 goto out;
684
685 phy->invalid_dword_count = get_unaligned_be32(&resp[12]);
686 phy->running_disparity_error_count = get_unaligned_be32(&resp[16]);
687 phy->loss_of_dword_sync_count = get_unaligned_be32(&resp[20]);
688 phy->phy_reset_problem_count = get_unaligned_be32(&resp[24]);
689
690 out:
691 kfree(req);
692 kfree(resp);
693 return res;
694
695}
696
697#ifdef CONFIG_SCSI_SAS_ATA
698
699#define RPS_REQ_SIZE 16
700#define RPS_RESP_SIZE 60
701
702int sas_get_report_phy_sata(struct domain_device *dev, int phy_id,
703 struct smp_resp *rps_resp)
704{
705 int res;
706 u8 *rps_req = alloc_smp_req(RPS_REQ_SIZE);
707 u8 *resp = (u8 *)rps_resp;
708
709 if (!rps_req)
710 return -ENOMEM;
711
712 rps_req[1] = SMP_REPORT_PHY_SATA;
713 rps_req[9] = phy_id;
714
715 res = smp_execute_task(dev, rps_req, RPS_REQ_SIZE,
716 rps_resp, RPS_RESP_SIZE);
717
718 /* 0x34 is the FIS type for the D2H fis. There's a potential
719 * standards cockup here. sas-2 explicitly specifies the FIS
720 * should be encoded so that FIS type is in resp[24].
721 * However, some expanders endian reverse this. Undo the
722 * reversal here */
723 if (!res && resp[27] == 0x34 && resp[24] != 0x34) {
724 int i;
725
726 for (i = 0; i < 5; i++) {
727 int j = 24 + (i*4);
728 u8 a, b;
729 a = resp[j + 0];
730 b = resp[j + 1];
731 resp[j + 0] = resp[j + 3];
732 resp[j + 1] = resp[j + 2];
733 resp[j + 2] = b;
734 resp[j + 3] = a;
735 }
736 }
737
738 kfree(rps_req);
739 return res;
740}
741#endif
742
743static void sas_ex_get_linkrate(struct domain_device *parent,
744 struct domain_device *child,
745 struct ex_phy *parent_phy)
746{
747 struct expander_device *parent_ex = &parent->ex_dev;
748 struct sas_port *port;
749 int i;
750
751 child->pathways = 0;
752
753 port = parent_phy->port;
754
755 for (i = 0; i < parent_ex->num_phys; i++) {
756 struct ex_phy *phy = &parent_ex->ex_phy[i];
757
758 if (phy->phy_state == PHY_VACANT ||
759 phy->phy_state == PHY_NOT_PRESENT)
760 continue;
761
762 if (SAS_ADDR(phy->attached_sas_addr) ==
763 SAS_ADDR(child->sas_addr)) {
764
765 child->min_linkrate = min(parent->min_linkrate,
766 phy->linkrate);
767 child->max_linkrate = max(parent->max_linkrate,
768 phy->linkrate);
769 child->pathways++;
770 sas_port_add_phy(port, phy->phy);
771 }
772 }
773 child->linkrate = min(parent_phy->linkrate, child->max_linkrate);
774 child->pathways = min(child->pathways, parent->pathways);
775}
776
777static struct domain_device *sas_ex_discover_end_dev(
778 struct domain_device *parent, int phy_id)
779{
780 struct expander_device *parent_ex = &parent->ex_dev;
781 struct ex_phy *phy = &parent_ex->ex_phy[phy_id];
782 struct domain_device *child = NULL;
783 struct sas_rphy *rphy;
784 int res;
785
786 if (phy->attached_sata_host || phy->attached_sata_ps)
787 return NULL;
788
789 child = sas_alloc_device();
790 if (!child)
791 return NULL;
792
793 kref_get(&parent->kref);
794 child->parent = parent;
795 child->port = parent->port;
796 child->iproto = phy->attached_iproto;
797 memcpy(child->sas_addr, phy->attached_sas_addr, SAS_ADDR_SIZE);
798 sas_hash_addr(child->hashed_sas_addr, child->sas_addr);
799 if (!phy->port) {
800 phy->port = sas_port_alloc(&parent->rphy->dev, phy_id);
801 if (unlikely(!phy->port))
802 goto out_err;
803 if (unlikely(sas_port_add(phy->port) != 0)) {
804 sas_port_free(phy->port);
805 goto out_err;
806 }
807 }
808 sas_ex_get_linkrate(parent, child, phy);
809 sas_device_set_phy(child, phy->port);
810
811#ifdef CONFIG_SCSI_SAS_ATA
812 if ((phy->attached_tproto & SAS_PROTOCOL_STP) || phy->attached_sata_dev) {
813 if (child->linkrate > parent->min_linkrate) {
814 struct sas_phy *cphy = child->phy;
815 enum sas_linkrate min_prate = cphy->minimum_linkrate,
816 parent_min_lrate = parent->min_linkrate,
817 min_linkrate = (min_prate > parent_min_lrate) ?
818 parent_min_lrate : 0;
819 struct sas_phy_linkrates rates = {
820 .maximum_linkrate = parent->min_linkrate,
821 .minimum_linkrate = min_linkrate,
822 };
823 int ret;
824
825 pr_notice("ex %016llx phy%02d SATA device linkrate > min pathway connection rate, attempting to lower device linkrate\n",
826 SAS_ADDR(child->sas_addr), phy_id);
827 ret = sas_smp_phy_control(parent, phy_id,
828 PHY_FUNC_LINK_RESET, &rates);
829 if (ret) {
830 pr_err("ex %016llx phy%02d SATA device could not set linkrate (%d)\n",
831 SAS_ADDR(child->sas_addr), phy_id, ret);
832 goto out_free;
833 }
834 pr_notice("ex %016llx phy%02d SATA device set linkrate successfully\n",
835 SAS_ADDR(child->sas_addr), phy_id);
836 child->linkrate = child->min_linkrate;
837 }
838 res = sas_get_ata_info(child, phy);
839 if (res)
840 goto out_free;
841
842 sas_init_dev(child);
843 res = sas_ata_init(child);
844 if (res)
845 goto out_free;
846 rphy = sas_end_device_alloc(phy->port);
847 if (!rphy)
848 goto out_free;
849 rphy->identify.phy_identifier = phy_id;
850
851 child->rphy = rphy;
852 get_device(&rphy->dev);
853
854 list_add_tail(&child->disco_list_node, &parent->port->disco_list);
855
856 res = sas_discover_sata(child);
857 if (res) {
858 pr_notice("sas_discover_sata() for device %16llx at %016llx:%02d returned 0x%x\n",
859 SAS_ADDR(child->sas_addr),
860 SAS_ADDR(parent->sas_addr), phy_id, res);
861 goto out_list_del;
862 }
863 } else
864#endif
865 if (phy->attached_tproto & SAS_PROTOCOL_SSP) {
866 child->dev_type = SAS_END_DEVICE;
867 rphy = sas_end_device_alloc(phy->port);
868 /* FIXME: error handling */
869 if (unlikely(!rphy))
870 goto out_free;
871 child->tproto = phy->attached_tproto;
872 sas_init_dev(child);
873
874 child->rphy = rphy;
875 get_device(&rphy->dev);
876 rphy->identify.phy_identifier = phy_id;
877 sas_fill_in_rphy(child, rphy);
878
879 list_add_tail(&child->disco_list_node, &parent->port->disco_list);
880
881 res = sas_discover_end_dev(child);
882 if (res) {
883 pr_notice("sas_discover_end_dev() for device %16llx at %016llx:%02d returned 0x%x\n",
884 SAS_ADDR(child->sas_addr),
885 SAS_ADDR(parent->sas_addr), phy_id, res);
886 goto out_list_del;
887 }
888 } else {
889 pr_notice("target proto 0x%x at %016llx:0x%x not handled\n",
890 phy->attached_tproto, SAS_ADDR(parent->sas_addr),
891 phy_id);
892 goto out_free;
893 }
894
895 list_add_tail(&child->siblings, &parent_ex->children);
896 return child;
897
898 out_list_del:
899 sas_rphy_free(child->rphy);
900 list_del(&child->disco_list_node);
901 spin_lock_irq(&parent->port->dev_list_lock);
902 list_del(&child->dev_list_node);
903 spin_unlock_irq(&parent->port->dev_list_lock);
904 out_free:
905 sas_port_delete(phy->port);
906 out_err:
907 phy->port = NULL;
908 sas_put_device(child);
909 return NULL;
910}
911
912/* See if this phy is part of a wide port */
913static bool sas_ex_join_wide_port(struct domain_device *parent, int phy_id)
914{
915 struct ex_phy *phy = &parent->ex_dev.ex_phy[phy_id];
916 int i;
917
918 for (i = 0; i < parent->ex_dev.num_phys; i++) {
919 struct ex_phy *ephy = &parent->ex_dev.ex_phy[i];
920
921 if (ephy == phy)
922 continue;
923
924 if (!memcmp(phy->attached_sas_addr, ephy->attached_sas_addr,
925 SAS_ADDR_SIZE) && ephy->port) {
926 sas_port_add_phy(ephy->port, phy->phy);
927 phy->port = ephy->port;
928 phy->phy_state = PHY_DEVICE_DISCOVERED;
929 return true;
930 }
931 }
932
933 return false;
934}
935
936static struct domain_device *sas_ex_discover_expander(
937 struct domain_device *parent, int phy_id)
938{
939 struct sas_expander_device *parent_ex = rphy_to_expander_device(parent->rphy);
940 struct ex_phy *phy = &parent->ex_dev.ex_phy[phy_id];
941 struct domain_device *child = NULL;
942 struct sas_rphy *rphy;
943 struct sas_expander_device *edev;
944 struct asd_sas_port *port;
945 int res;
946
947 if (phy->routing_attr == DIRECT_ROUTING) {
948 pr_warn("ex %016llx:%02d:D <--> ex %016llx:0x%x is not allowed\n",
949 SAS_ADDR(parent->sas_addr), phy_id,
950 SAS_ADDR(phy->attached_sas_addr),
951 phy->attached_phy_id);
952 return NULL;
953 }
954 child = sas_alloc_device();
955 if (!child)
956 return NULL;
957
958 phy->port = sas_port_alloc(&parent->rphy->dev, phy_id);
959 /* FIXME: better error handling */
960 BUG_ON(sas_port_add(phy->port) != 0);
961
962
963 switch (phy->attached_dev_type) {
964 case SAS_EDGE_EXPANDER_DEVICE:
965 rphy = sas_expander_alloc(phy->port,
966 SAS_EDGE_EXPANDER_DEVICE);
967 break;
968 case SAS_FANOUT_EXPANDER_DEVICE:
969 rphy = sas_expander_alloc(phy->port,
970 SAS_FANOUT_EXPANDER_DEVICE);
971 break;
972 default:
973 rphy = NULL; /* shut gcc up */
974 BUG();
975 }
976 port = parent->port;
977 child->rphy = rphy;
978 get_device(&rphy->dev);
979 edev = rphy_to_expander_device(rphy);
980 child->dev_type = phy->attached_dev_type;
981 kref_get(&parent->kref);
982 child->parent = parent;
983 child->port = port;
984 child->iproto = phy->attached_iproto;
985 child->tproto = phy->attached_tproto;
986 memcpy(child->sas_addr, phy->attached_sas_addr, SAS_ADDR_SIZE);
987 sas_hash_addr(child->hashed_sas_addr, child->sas_addr);
988 sas_ex_get_linkrate(parent, child, phy);
989 edev->level = parent_ex->level + 1;
990 parent->port->disc.max_level = max(parent->port->disc.max_level,
991 edev->level);
992 sas_init_dev(child);
993 sas_fill_in_rphy(child, rphy);
994 sas_rphy_add(rphy);
995
996 spin_lock_irq(&parent->port->dev_list_lock);
997 list_add_tail(&child->dev_list_node, &parent->port->dev_list);
998 spin_unlock_irq(&parent->port->dev_list_lock);
999
1000 res = sas_discover_expander(child);
1001 if (res) {
1002 sas_rphy_delete(rphy);
1003 spin_lock_irq(&parent->port->dev_list_lock);
1004 list_del(&child->dev_list_node);
1005 spin_unlock_irq(&parent->port->dev_list_lock);
1006 sas_put_device(child);
1007 sas_port_delete(phy->port);
1008 phy->port = NULL;
1009 return NULL;
1010 }
1011 list_add_tail(&child->siblings, &parent->ex_dev.children);
1012 return child;
1013}
1014
1015static int sas_ex_discover_dev(struct domain_device *dev, int phy_id)
1016{
1017 struct expander_device *ex = &dev->ex_dev;
1018 struct ex_phy *ex_phy = &ex->ex_phy[phy_id];
1019 struct domain_device *child = NULL;
1020 int res = 0;
1021
1022 /* Phy state */
1023 if (ex_phy->linkrate == SAS_SATA_SPINUP_HOLD) {
1024 if (!sas_smp_phy_control(dev, phy_id, PHY_FUNC_LINK_RESET, NULL))
1025 res = sas_ex_phy_discover(dev, phy_id);
1026 if (res)
1027 return res;
1028 }
1029
1030 /* Parent and domain coherency */
1031 if (!dev->parent && (SAS_ADDR(ex_phy->attached_sas_addr) ==
1032 SAS_ADDR(dev->port->sas_addr))) {
1033 sas_add_parent_port(dev, phy_id);
1034 return 0;
1035 }
1036 if (dev->parent && (SAS_ADDR(ex_phy->attached_sas_addr) ==
1037 SAS_ADDR(dev->parent->sas_addr))) {
1038 sas_add_parent_port(dev, phy_id);
1039 if (ex_phy->routing_attr == TABLE_ROUTING)
1040 sas_configure_phy(dev, phy_id, dev->port->sas_addr, 1);
1041 return 0;
1042 }
1043
1044 if (sas_dev_present_in_domain(dev->port, ex_phy->attached_sas_addr))
1045 sas_ex_disable_port(dev, ex_phy->attached_sas_addr);
1046
1047 if (ex_phy->attached_dev_type == SAS_PHY_UNUSED) {
1048 if (ex_phy->routing_attr == DIRECT_ROUTING) {
1049 memset(ex_phy->attached_sas_addr, 0, SAS_ADDR_SIZE);
1050 sas_configure_routing(dev, ex_phy->attached_sas_addr);
1051 }
1052 return 0;
1053 } else if (ex_phy->linkrate == SAS_LINK_RATE_UNKNOWN)
1054 return 0;
1055
1056 if (ex_phy->attached_dev_type != SAS_END_DEVICE &&
1057 ex_phy->attached_dev_type != SAS_FANOUT_EXPANDER_DEVICE &&
1058 ex_phy->attached_dev_type != SAS_EDGE_EXPANDER_DEVICE &&
1059 ex_phy->attached_dev_type != SAS_SATA_PENDING) {
1060 pr_warn("unknown device type(0x%x) attached to ex %016llx phy%02d\n",
1061 ex_phy->attached_dev_type,
1062 SAS_ADDR(dev->sas_addr),
1063 phy_id);
1064 return 0;
1065 }
1066
1067 res = sas_configure_routing(dev, ex_phy->attached_sas_addr);
1068 if (res) {
1069 pr_notice("configure routing for dev %016llx reported 0x%x. Forgotten\n",
1070 SAS_ADDR(ex_phy->attached_sas_addr), res);
1071 sas_disable_routing(dev, ex_phy->attached_sas_addr);
1072 return res;
1073 }
1074
1075 if (sas_ex_join_wide_port(dev, phy_id)) {
1076 pr_debug("Attaching ex phy%02d to wide port %016llx\n",
1077 phy_id, SAS_ADDR(ex_phy->attached_sas_addr));
1078 return res;
1079 }
1080
1081 switch (ex_phy->attached_dev_type) {
1082 case SAS_END_DEVICE:
1083 case SAS_SATA_PENDING:
1084 child = sas_ex_discover_end_dev(dev, phy_id);
1085 break;
1086 case SAS_FANOUT_EXPANDER_DEVICE:
1087 if (SAS_ADDR(dev->port->disc.fanout_sas_addr)) {
1088 pr_debug("second fanout expander %016llx phy%02d attached to ex %016llx phy%02d\n",
1089 SAS_ADDR(ex_phy->attached_sas_addr),
1090 ex_phy->attached_phy_id,
1091 SAS_ADDR(dev->sas_addr),
1092 phy_id);
1093 sas_ex_disable_phy(dev, phy_id);
1094 return res;
1095 } else
1096 memcpy(dev->port->disc.fanout_sas_addr,
1097 ex_phy->attached_sas_addr, SAS_ADDR_SIZE);
1098 /* fallthrough */
1099 case SAS_EDGE_EXPANDER_DEVICE:
1100 child = sas_ex_discover_expander(dev, phy_id);
1101 break;
1102 default:
1103 break;
1104 }
1105
1106 if (!child)
1107 pr_notice("ex %016llx phy%02d failed to discover\n",
1108 SAS_ADDR(dev->sas_addr), phy_id);
1109 return res;
1110}
1111
1112static int sas_find_sub_addr(struct domain_device *dev, u8 *sub_addr)
1113{
1114 struct expander_device *ex = &dev->ex_dev;
1115 int i;
1116
1117 for (i = 0; i < ex->num_phys; i++) {
1118 struct ex_phy *phy = &ex->ex_phy[i];
1119
1120 if (phy->phy_state == PHY_VACANT ||
1121 phy->phy_state == PHY_NOT_PRESENT)
1122 continue;
1123
1124 if (dev_is_expander(phy->attached_dev_type) &&
1125 phy->routing_attr == SUBTRACTIVE_ROUTING) {
1126
1127 memcpy(sub_addr, phy->attached_sas_addr, SAS_ADDR_SIZE);
1128
1129 return 1;
1130 }
1131 }
1132 return 0;
1133}
1134
1135static int sas_check_level_subtractive_boundary(struct domain_device *dev)
1136{
1137 struct expander_device *ex = &dev->ex_dev;
1138 struct domain_device *child;
1139 u8 sub_addr[SAS_ADDR_SIZE] = {0, };
1140
1141 list_for_each_entry(child, &ex->children, siblings) {
1142 if (!dev_is_expander(child->dev_type))
1143 continue;
1144 if (sub_addr[0] == 0) {
1145 sas_find_sub_addr(child, sub_addr);
1146 continue;
1147 } else {
1148 u8 s2[SAS_ADDR_SIZE];
1149
1150 if (sas_find_sub_addr(child, s2) &&
1151 (SAS_ADDR(sub_addr) != SAS_ADDR(s2))) {
1152
1153 pr_notice("ex %016llx->%016llx-?->%016llx diverges from subtractive boundary %016llx\n",
1154 SAS_ADDR(dev->sas_addr),
1155 SAS_ADDR(child->sas_addr),
1156 SAS_ADDR(s2),
1157 SAS_ADDR(sub_addr));
1158
1159 sas_ex_disable_port(child, s2);
1160 }
1161 }
1162 }
1163 return 0;
1164}
1165/**
1166 * sas_ex_discover_devices - discover devices attached to this expander
1167 * @dev: pointer to the expander domain device
1168 * @single: if you want to do a single phy, else set to -1;
1169 *
1170 * Configure this expander for use with its devices and register the
1171 * devices of this expander.
1172 */
1173static int sas_ex_discover_devices(struct domain_device *dev, int single)
1174{
1175 struct expander_device *ex = &dev->ex_dev;
1176 int i = 0, end = ex->num_phys;
1177 int res = 0;
1178
1179 if (0 <= single && single < end) {
1180 i = single;
1181 end = i+1;
1182 }
1183
1184 for ( ; i < end; i++) {
1185 struct ex_phy *ex_phy = &ex->ex_phy[i];
1186
1187 if (ex_phy->phy_state == PHY_VACANT ||
1188 ex_phy->phy_state == PHY_NOT_PRESENT ||
1189 ex_phy->phy_state == PHY_DEVICE_DISCOVERED)
1190 continue;
1191
1192 switch (ex_phy->linkrate) {
1193 case SAS_PHY_DISABLED:
1194 case SAS_PHY_RESET_PROBLEM:
1195 case SAS_SATA_PORT_SELECTOR:
1196 continue;
1197 default:
1198 res = sas_ex_discover_dev(dev, i);
1199 if (res)
1200 break;
1201 continue;
1202 }
1203 }
1204
1205 if (!res)
1206 sas_check_level_subtractive_boundary(dev);
1207
1208 return res;
1209}
1210
1211static int sas_check_ex_subtractive_boundary(struct domain_device *dev)
1212{
1213 struct expander_device *ex = &dev->ex_dev;
1214 int i;
1215 u8 *sub_sas_addr = NULL;
1216
1217 if (dev->dev_type != SAS_EDGE_EXPANDER_DEVICE)
1218 return 0;
1219
1220 for (i = 0; i < ex->num_phys; i++) {
1221 struct ex_phy *phy = &ex->ex_phy[i];
1222
1223 if (phy->phy_state == PHY_VACANT ||
1224 phy->phy_state == PHY_NOT_PRESENT)
1225 continue;
1226
1227 if (dev_is_expander(phy->attached_dev_type) &&
1228 phy->routing_attr == SUBTRACTIVE_ROUTING) {
1229
1230 if (!sub_sas_addr)
1231 sub_sas_addr = &phy->attached_sas_addr[0];
1232 else if (SAS_ADDR(sub_sas_addr) !=
1233 SAS_ADDR(phy->attached_sas_addr)) {
1234
1235 pr_notice("ex %016llx phy%02d diverges(%016llx) on subtractive boundary(%016llx). Disabled\n",
1236 SAS_ADDR(dev->sas_addr), i,
1237 SAS_ADDR(phy->attached_sas_addr),
1238 SAS_ADDR(sub_sas_addr));
1239 sas_ex_disable_phy(dev, i);
1240 }
1241 }
1242 }
1243 return 0;
1244}
1245
1246static void sas_print_parent_topology_bug(struct domain_device *child,
1247 struct ex_phy *parent_phy,
1248 struct ex_phy *child_phy)
1249{
1250 static const char *ex_type[] = {
1251 [SAS_EDGE_EXPANDER_DEVICE] = "edge",
1252 [SAS_FANOUT_EXPANDER_DEVICE] = "fanout",
1253 };
1254 struct domain_device *parent = child->parent;
1255
1256 pr_notice("%s ex %016llx phy%02d <--> %s ex %016llx phy%02d has %c:%c routing link!\n",
1257 ex_type[parent->dev_type],
1258 SAS_ADDR(parent->sas_addr),
1259 parent_phy->phy_id,
1260
1261 ex_type[child->dev_type],
1262 SAS_ADDR(child->sas_addr),
1263 child_phy->phy_id,
1264
1265 sas_route_char(parent, parent_phy),
1266 sas_route_char(child, child_phy));
1267}
1268
1269static int sas_check_eeds(struct domain_device *child,
1270 struct ex_phy *parent_phy,
1271 struct ex_phy *child_phy)
1272{
1273 int res = 0;
1274 struct domain_device *parent = child->parent;
1275
1276 if (SAS_ADDR(parent->port->disc.fanout_sas_addr) != 0) {
1277 res = -ENODEV;
1278 pr_warn("edge ex %016llx phy S:%02d <--> edge ex %016llx phy S:%02d, while there is a fanout ex %016llx\n",
1279 SAS_ADDR(parent->sas_addr),
1280 parent_phy->phy_id,
1281 SAS_ADDR(child->sas_addr),
1282 child_phy->phy_id,
1283 SAS_ADDR(parent->port->disc.fanout_sas_addr));
1284 } else if (SAS_ADDR(parent->port->disc.eeds_a) == 0) {
1285 memcpy(parent->port->disc.eeds_a, parent->sas_addr,
1286 SAS_ADDR_SIZE);
1287 memcpy(parent->port->disc.eeds_b, child->sas_addr,
1288 SAS_ADDR_SIZE);
1289 } else if (((SAS_ADDR(parent->port->disc.eeds_a) ==
1290 SAS_ADDR(parent->sas_addr)) ||
1291 (SAS_ADDR(parent->port->disc.eeds_a) ==
1292 SAS_ADDR(child->sas_addr)))
1293 &&
1294 ((SAS_ADDR(parent->port->disc.eeds_b) ==
1295 SAS_ADDR(parent->sas_addr)) ||
1296 (SAS_ADDR(parent->port->disc.eeds_b) ==
1297 SAS_ADDR(child->sas_addr))))
1298 ;
1299 else {
1300 res = -ENODEV;
1301 pr_warn("edge ex %016llx phy%02d <--> edge ex %016llx phy%02d link forms a third EEDS!\n",
1302 SAS_ADDR(parent->sas_addr),
1303 parent_phy->phy_id,
1304 SAS_ADDR(child->sas_addr),
1305 child_phy->phy_id);
1306 }
1307
1308 return res;
1309}
1310
1311/* Here we spill over 80 columns. It is intentional.
1312 */
1313static int sas_check_parent_topology(struct domain_device *child)
1314{
1315 struct expander_device *child_ex = &child->ex_dev;
1316 struct expander_device *parent_ex;
1317 int i;
1318 int res = 0;
1319
1320 if (!child->parent)
1321 return 0;
1322
1323 if (!dev_is_expander(child->parent->dev_type))
1324 return 0;
1325
1326 parent_ex = &child->parent->ex_dev;
1327
1328 for (i = 0; i < parent_ex->num_phys; i++) {
1329 struct ex_phy *parent_phy = &parent_ex->ex_phy[i];
1330 struct ex_phy *child_phy;
1331
1332 if (parent_phy->phy_state == PHY_VACANT ||
1333 parent_phy->phy_state == PHY_NOT_PRESENT)
1334 continue;
1335
1336 if (SAS_ADDR(parent_phy->attached_sas_addr) != SAS_ADDR(child->sas_addr))
1337 continue;
1338
1339 child_phy = &child_ex->ex_phy[parent_phy->attached_phy_id];
1340
1341 switch (child->parent->dev_type) {
1342 case SAS_EDGE_EXPANDER_DEVICE:
1343 if (child->dev_type == SAS_FANOUT_EXPANDER_DEVICE) {
1344 if (parent_phy->routing_attr != SUBTRACTIVE_ROUTING ||
1345 child_phy->routing_attr != TABLE_ROUTING) {
1346 sas_print_parent_topology_bug(child, parent_phy, child_phy);
1347 res = -ENODEV;
1348 }
1349 } else if (parent_phy->routing_attr == SUBTRACTIVE_ROUTING) {
1350 if (child_phy->routing_attr == SUBTRACTIVE_ROUTING) {
1351 res = sas_check_eeds(child, parent_phy, child_phy);
1352 } else if (child_phy->routing_attr != TABLE_ROUTING) {
1353 sas_print_parent_topology_bug(child, parent_phy, child_phy);
1354 res = -ENODEV;
1355 }
1356 } else if (parent_phy->routing_attr == TABLE_ROUTING) {
1357 if (child_phy->routing_attr == SUBTRACTIVE_ROUTING ||
1358 (child_phy->routing_attr == TABLE_ROUTING &&
1359 child_ex->t2t_supp && parent_ex->t2t_supp)) {
1360 /* All good */;
1361 } else {
1362 sas_print_parent_topology_bug(child, parent_phy, child_phy);
1363 res = -ENODEV;
1364 }
1365 }
1366 break;
1367 case SAS_FANOUT_EXPANDER_DEVICE:
1368 if (parent_phy->routing_attr != TABLE_ROUTING ||
1369 child_phy->routing_attr != SUBTRACTIVE_ROUTING) {
1370 sas_print_parent_topology_bug(child, parent_phy, child_phy);
1371 res = -ENODEV;
1372 }
1373 break;
1374 default:
1375 break;
1376 }
1377 }
1378
1379 return res;
1380}
1381
1382#define RRI_REQ_SIZE 16
1383#define RRI_RESP_SIZE 44
1384
1385static int sas_configure_present(struct domain_device *dev, int phy_id,
1386 u8 *sas_addr, int *index, int *present)
1387{
1388 int i, res = 0;
1389 struct expander_device *ex = &dev->ex_dev;
1390 struct ex_phy *phy = &ex->ex_phy[phy_id];
1391 u8 *rri_req;
1392 u8 *rri_resp;
1393
1394 *present = 0;
1395 *index = 0;
1396
1397 rri_req = alloc_smp_req(RRI_REQ_SIZE);
1398 if (!rri_req)
1399 return -ENOMEM;
1400
1401 rri_resp = alloc_smp_resp(RRI_RESP_SIZE);
1402 if (!rri_resp) {
1403 kfree(rri_req);
1404 return -ENOMEM;
1405 }
1406
1407 rri_req[1] = SMP_REPORT_ROUTE_INFO;
1408 rri_req[9] = phy_id;
1409
1410 for (i = 0; i < ex->max_route_indexes ; i++) {
1411 *(__be16 *)(rri_req+6) = cpu_to_be16(i);
1412 res = smp_execute_task(dev, rri_req, RRI_REQ_SIZE, rri_resp,
1413 RRI_RESP_SIZE);
1414 if (res)
1415 goto out;
1416 res = rri_resp[2];
1417 if (res == SMP_RESP_NO_INDEX) {
1418 pr_warn("overflow of indexes: dev %016llx phy%02d index 0x%x\n",
1419 SAS_ADDR(dev->sas_addr), phy_id, i);
1420 goto out;
1421 } else if (res != SMP_RESP_FUNC_ACC) {
1422 pr_notice("%s: dev %016llx phy%02d index 0x%x result 0x%x\n",
1423 __func__, SAS_ADDR(dev->sas_addr), phy_id,
1424 i, res);
1425 goto out;
1426 }
1427 if (SAS_ADDR(sas_addr) != 0) {
1428 if (SAS_ADDR(rri_resp+16) == SAS_ADDR(sas_addr)) {
1429 *index = i;
1430 if ((rri_resp[12] & 0x80) == 0x80)
1431 *present = 0;
1432 else
1433 *present = 1;
1434 goto out;
1435 } else if (SAS_ADDR(rri_resp+16) == 0) {
1436 *index = i;
1437 *present = 0;
1438 goto out;
1439 }
1440 } else if (SAS_ADDR(rri_resp+16) == 0 &&
1441 phy->last_da_index < i) {
1442 phy->last_da_index = i;
1443 *index = i;
1444 *present = 0;
1445 goto out;
1446 }
1447 }
1448 res = -1;
1449out:
1450 kfree(rri_req);
1451 kfree(rri_resp);
1452 return res;
1453}
1454
1455#define CRI_REQ_SIZE 44
1456#define CRI_RESP_SIZE 8
1457
1458static int sas_configure_set(struct domain_device *dev, int phy_id,
1459 u8 *sas_addr, int index, int include)
1460{
1461 int res;
1462 u8 *cri_req;
1463 u8 *cri_resp;
1464
1465 cri_req = alloc_smp_req(CRI_REQ_SIZE);
1466 if (!cri_req)
1467 return -ENOMEM;
1468
1469 cri_resp = alloc_smp_resp(CRI_RESP_SIZE);
1470 if (!cri_resp) {
1471 kfree(cri_req);
1472 return -ENOMEM;
1473 }
1474
1475 cri_req[1] = SMP_CONF_ROUTE_INFO;
1476 *(__be16 *)(cri_req+6) = cpu_to_be16(index);
1477 cri_req[9] = phy_id;
1478 if (SAS_ADDR(sas_addr) == 0 || !include)
1479 cri_req[12] |= 0x80;
1480 memcpy(cri_req+16, sas_addr, SAS_ADDR_SIZE);
1481
1482 res = smp_execute_task(dev, cri_req, CRI_REQ_SIZE, cri_resp,
1483 CRI_RESP_SIZE);
1484 if (res)
1485 goto out;
1486 res = cri_resp[2];
1487 if (res == SMP_RESP_NO_INDEX) {
1488 pr_warn("overflow of indexes: dev %016llx phy%02d index 0x%x\n",
1489 SAS_ADDR(dev->sas_addr), phy_id, index);
1490 }
1491out:
1492 kfree(cri_req);
1493 kfree(cri_resp);
1494 return res;
1495}
1496
1497static int sas_configure_phy(struct domain_device *dev, int phy_id,
1498 u8 *sas_addr, int include)
1499{
1500 int index;
1501 int present;
1502 int res;
1503
1504 res = sas_configure_present(dev, phy_id, sas_addr, &index, &present);
1505 if (res)
1506 return res;
1507 if (include ^ present)
1508 return sas_configure_set(dev, phy_id, sas_addr, index,include);
1509
1510 return res;
1511}
1512
1513/**
1514 * sas_configure_parent - configure routing table of parent
1515 * @parent: parent expander
1516 * @child: child expander
1517 * @sas_addr: SAS port identifier of device directly attached to child
1518 * @include: whether or not to include @child in the expander routing table
1519 */
1520static int sas_configure_parent(struct domain_device *parent,
1521 struct domain_device *child,
1522 u8 *sas_addr, int include)
1523{
1524 struct expander_device *ex_parent = &parent->ex_dev;
1525 int res = 0;
1526 int i;
1527
1528 if (parent->parent) {
1529 res = sas_configure_parent(parent->parent, parent, sas_addr,
1530 include);
1531 if (res)
1532 return res;
1533 }
1534
1535 if (ex_parent->conf_route_table == 0) {
1536 pr_debug("ex %016llx has self-configuring routing table\n",
1537 SAS_ADDR(parent->sas_addr));
1538 return 0;
1539 }
1540
1541 for (i = 0; i < ex_parent->num_phys; i++) {
1542 struct ex_phy *phy = &ex_parent->ex_phy[i];
1543
1544 if ((phy->routing_attr == TABLE_ROUTING) &&
1545 (SAS_ADDR(phy->attached_sas_addr) ==
1546 SAS_ADDR(child->sas_addr))) {
1547 res = sas_configure_phy(parent, i, sas_addr, include);
1548 if (res)
1549 return res;
1550 }
1551 }
1552
1553 return res;
1554}
1555
1556/**
1557 * sas_configure_routing - configure routing
1558 * @dev: expander device
1559 * @sas_addr: port identifier of device directly attached to the expander device
1560 */
1561static int sas_configure_routing(struct domain_device *dev, u8 *sas_addr)
1562{
1563 if (dev->parent)
1564 return sas_configure_parent(dev->parent, dev, sas_addr, 1);
1565 return 0;
1566}
1567
1568static int sas_disable_routing(struct domain_device *dev, u8 *sas_addr)
1569{
1570 if (dev->parent)
1571 return sas_configure_parent(dev->parent, dev, sas_addr, 0);
1572 return 0;
1573}
1574
1575/**
1576 * sas_discover_expander - expander discovery
1577 * @dev: pointer to expander domain device
1578 *
1579 * See comment in sas_discover_sata().
1580 */
1581static int sas_discover_expander(struct domain_device *dev)
1582{
1583 int res;
1584
1585 res = sas_notify_lldd_dev_found(dev);
1586 if (res)
1587 return res;
1588
1589 res = sas_ex_general(dev);
1590 if (res)
1591 goto out_err;
1592 res = sas_ex_manuf_info(dev);
1593 if (res)
1594 goto out_err;
1595
1596 res = sas_expander_discover(dev);
1597 if (res) {
1598 pr_warn("expander %016llx discovery failed(0x%x)\n",
1599 SAS_ADDR(dev->sas_addr), res);
1600 goto out_err;
1601 }
1602
1603 sas_check_ex_subtractive_boundary(dev);
1604 res = sas_check_parent_topology(dev);
1605 if (res)
1606 goto out_err;
1607 return 0;
1608out_err:
1609 sas_notify_lldd_dev_gone(dev);
1610 return res;
1611}
1612
1613static int sas_ex_level_discovery(struct asd_sas_port *port, const int level)
1614{
1615 int res = 0;
1616 struct domain_device *dev;
1617
1618 list_for_each_entry(dev, &port->dev_list, dev_list_node) {
1619 if (dev_is_expander(dev->dev_type)) {
1620 struct sas_expander_device *ex =
1621 rphy_to_expander_device(dev->rphy);
1622
1623 if (level == ex->level)
1624 res = sas_ex_discover_devices(dev, -1);
1625 else if (level > 0)
1626 res = sas_ex_discover_devices(port->port_dev, -1);
1627
1628 }
1629 }
1630
1631 return res;
1632}
1633
1634static int sas_ex_bfs_disc(struct asd_sas_port *port)
1635{
1636 int res;
1637 int level;
1638
1639 do {
1640 level = port->disc.max_level;
1641 res = sas_ex_level_discovery(port, level);
1642 mb();
1643 } while (level < port->disc.max_level);
1644
1645 return res;
1646}
1647
1648int sas_discover_root_expander(struct domain_device *dev)
1649{
1650 int res;
1651 struct sas_expander_device *ex = rphy_to_expander_device(dev->rphy);
1652
1653 res = sas_rphy_add(dev->rphy);
1654 if (res)
1655 goto out_err;
1656
1657 ex->level = dev->port->disc.max_level; /* 0 */
1658 res = sas_discover_expander(dev);
1659 if (res)
1660 goto out_err2;
1661
1662 sas_ex_bfs_disc(dev->port);
1663
1664 return res;
1665
1666out_err2:
1667 sas_rphy_remove(dev->rphy);
1668out_err:
1669 return res;
1670}
1671
1672/* ---------- Domain revalidation ---------- */
1673
1674static int sas_get_phy_discover(struct domain_device *dev,
1675 int phy_id, struct smp_resp *disc_resp)
1676{
1677 int res;
1678 u8 *disc_req;
1679
1680 disc_req = alloc_smp_req(DISCOVER_REQ_SIZE);
1681 if (!disc_req)
1682 return -ENOMEM;
1683
1684 disc_req[1] = SMP_DISCOVER;
1685 disc_req[9] = phy_id;
1686
1687 res = smp_execute_task(dev, disc_req, DISCOVER_REQ_SIZE,
1688 disc_resp, DISCOVER_RESP_SIZE);
1689 if (res)
1690 goto out;
1691 else if (disc_resp->result != SMP_RESP_FUNC_ACC) {
1692 res = disc_resp->result;
1693 goto out;
1694 }
1695out:
1696 kfree(disc_req);
1697 return res;
1698}
1699
1700static int sas_get_phy_change_count(struct domain_device *dev,
1701 int phy_id, int *pcc)
1702{
1703 int res;
1704 struct smp_resp *disc_resp;
1705
1706 disc_resp = alloc_smp_resp(DISCOVER_RESP_SIZE);
1707 if (!disc_resp)
1708 return -ENOMEM;
1709
1710 res = sas_get_phy_discover(dev, phy_id, disc_resp);
1711 if (!res)
1712 *pcc = disc_resp->disc.change_count;
1713
1714 kfree(disc_resp);
1715 return res;
1716}
1717
1718static int sas_get_phy_attached_dev(struct domain_device *dev, int phy_id,
1719 u8 *sas_addr, enum sas_device_type *type)
1720{
1721 int res;
1722 struct smp_resp *disc_resp;
1723 struct discover_resp *dr;
1724
1725 disc_resp = alloc_smp_resp(DISCOVER_RESP_SIZE);
1726 if (!disc_resp)
1727 return -ENOMEM;
1728 dr = &disc_resp->disc;
1729
1730 res = sas_get_phy_discover(dev, phy_id, disc_resp);
1731 if (res == 0) {
1732 memcpy(sas_addr, disc_resp->disc.attached_sas_addr,
1733 SAS_ADDR_SIZE);
1734 *type = to_dev_type(dr);
1735 if (*type == 0)
1736 memset(sas_addr, 0, SAS_ADDR_SIZE);
1737 }
1738 kfree(disc_resp);
1739 return res;
1740}
1741
1742static int sas_find_bcast_phy(struct domain_device *dev, int *phy_id,
1743 int from_phy, bool update)
1744{
1745 struct expander_device *ex = &dev->ex_dev;
1746 int res = 0;
1747 int i;
1748
1749 for (i = from_phy; i < ex->num_phys; i++) {
1750 int phy_change_count = 0;
1751
1752 res = sas_get_phy_change_count(dev, i, &phy_change_count);
1753 switch (res) {
1754 case SMP_RESP_PHY_VACANT:
1755 case SMP_RESP_NO_PHY:
1756 continue;
1757 case SMP_RESP_FUNC_ACC:
1758 break;
1759 default:
1760 return res;
1761 }
1762
1763 if (phy_change_count != ex->ex_phy[i].phy_change_count) {
1764 if (update)
1765 ex->ex_phy[i].phy_change_count =
1766 phy_change_count;
1767 *phy_id = i;
1768 return 0;
1769 }
1770 }
1771 return 0;
1772}
1773
1774static int sas_get_ex_change_count(struct domain_device *dev, int *ecc)
1775{
1776 int res;
1777 u8 *rg_req;
1778 struct smp_resp *rg_resp;
1779
1780 rg_req = alloc_smp_req(RG_REQ_SIZE);
1781 if (!rg_req)
1782 return -ENOMEM;
1783
1784 rg_resp = alloc_smp_resp(RG_RESP_SIZE);
1785 if (!rg_resp) {
1786 kfree(rg_req);
1787 return -ENOMEM;
1788 }
1789
1790 rg_req[1] = SMP_REPORT_GENERAL;
1791
1792 res = smp_execute_task(dev, rg_req, RG_REQ_SIZE, rg_resp,
1793 RG_RESP_SIZE);
1794 if (res)
1795 goto out;
1796 if (rg_resp->result != SMP_RESP_FUNC_ACC) {
1797 res = rg_resp->result;
1798 goto out;
1799 }
1800
1801 *ecc = be16_to_cpu(rg_resp->rg.change_count);
1802out:
1803 kfree(rg_resp);
1804 kfree(rg_req);
1805 return res;
1806}
1807/**
1808 * sas_find_bcast_dev - find the device issue BROADCAST(CHANGE).
1809 * @dev:domain device to be detect.
1810 * @src_dev: the device which originated BROADCAST(CHANGE).
1811 *
1812 * Add self-configuration expander support. Suppose two expander cascading,
1813 * when the first level expander is self-configuring, hotplug the disks in
1814 * second level expander, BROADCAST(CHANGE) will not only be originated
1815 * in the second level expander, but also be originated in the first level
1816 * expander (see SAS protocol SAS 2r-14, 7.11 for detail), it is to say,
1817 * expander changed count in two level expanders will all increment at least
1818 * once, but the phy which chang count has changed is the source device which
1819 * we concerned.
1820 */
1821
1822static int sas_find_bcast_dev(struct domain_device *dev,
1823 struct domain_device **src_dev)
1824{
1825 struct expander_device *ex = &dev->ex_dev;
1826 int ex_change_count = -1;
1827 int phy_id = -1;
1828 int res;
1829 struct domain_device *ch;
1830
1831 res = sas_get_ex_change_count(dev, &ex_change_count);
1832 if (res)
1833 goto out;
1834 if (ex_change_count != -1 && ex_change_count != ex->ex_change_count) {
1835 /* Just detect if this expander phys phy change count changed,
1836 * in order to determine if this expander originate BROADCAST,
1837 * and do not update phy change count field in our structure.
1838 */
1839 res = sas_find_bcast_phy(dev, &phy_id, 0, false);
1840 if (phy_id != -1) {
1841 *src_dev = dev;
1842 ex->ex_change_count = ex_change_count;
1843 pr_info("ex %016llx phy%02d change count has changed\n",
1844 SAS_ADDR(dev->sas_addr), phy_id);
1845 return res;
1846 } else
1847 pr_info("ex %016llx phys DID NOT change\n",
1848 SAS_ADDR(dev->sas_addr));
1849 }
1850 list_for_each_entry(ch, &ex->children, siblings) {
1851 if (dev_is_expander(ch->dev_type)) {
1852 res = sas_find_bcast_dev(ch, src_dev);
1853 if (*src_dev)
1854 return res;
1855 }
1856 }
1857out:
1858 return res;
1859}
1860
1861static void sas_unregister_ex_tree(struct asd_sas_port *port, struct domain_device *dev)
1862{
1863 struct expander_device *ex = &dev->ex_dev;
1864 struct domain_device *child, *n;
1865
1866 list_for_each_entry_safe(child, n, &ex->children, siblings) {
1867 set_bit(SAS_DEV_GONE, &child->state);
1868 if (dev_is_expander(child->dev_type))
1869 sas_unregister_ex_tree(port, child);
1870 else
1871 sas_unregister_dev(port, child);
1872 }
1873 sas_unregister_dev(port, dev);
1874}
1875
1876static void sas_unregister_devs_sas_addr(struct domain_device *parent,
1877 int phy_id, bool last)
1878{
1879 struct expander_device *ex_dev = &parent->ex_dev;
1880 struct ex_phy *phy = &ex_dev->ex_phy[phy_id];
1881 struct domain_device *child, *n, *found = NULL;
1882 if (last) {
1883 list_for_each_entry_safe(child, n,
1884 &ex_dev->children, siblings) {
1885 if (SAS_ADDR(child->sas_addr) ==
1886 SAS_ADDR(phy->attached_sas_addr)) {
1887 set_bit(SAS_DEV_GONE, &child->state);
1888 if (dev_is_expander(child->dev_type))
1889 sas_unregister_ex_tree(parent->port, child);
1890 else
1891 sas_unregister_dev(parent->port, child);
1892 found = child;
1893 break;
1894 }
1895 }
1896 sas_disable_routing(parent, phy->attached_sas_addr);
1897 }
1898 memset(phy->attached_sas_addr, 0, SAS_ADDR_SIZE);
1899 if (phy->port) {
1900 sas_port_delete_phy(phy->port, phy->phy);
1901 sas_device_set_phy(found, phy->port);
1902 if (phy->port->num_phys == 0)
1903 list_add_tail(&phy->port->del_list,
1904 &parent->port->sas_port_del_list);
1905 phy->port = NULL;
1906 }
1907}
1908
1909static int sas_discover_bfs_by_root_level(struct domain_device *root,
1910 const int level)
1911{
1912 struct expander_device *ex_root = &root->ex_dev;
1913 struct domain_device *child;
1914 int res = 0;
1915
1916 list_for_each_entry(child, &ex_root->children, siblings) {
1917 if (dev_is_expander(child->dev_type)) {
1918 struct sas_expander_device *ex =
1919 rphy_to_expander_device(child->rphy);
1920
1921 if (level > ex->level)
1922 res = sas_discover_bfs_by_root_level(child,
1923 level);
1924 else if (level == ex->level)
1925 res = sas_ex_discover_devices(child, -1);
1926 }
1927 }
1928 return res;
1929}
1930
1931static int sas_discover_bfs_by_root(struct domain_device *dev)
1932{
1933 int res;
1934 struct sas_expander_device *ex = rphy_to_expander_device(dev->rphy);
1935 int level = ex->level+1;
1936
1937 res = sas_ex_discover_devices(dev, -1);
1938 if (res)
1939 goto out;
1940 do {
1941 res = sas_discover_bfs_by_root_level(dev, level);
1942 mb();
1943 level += 1;
1944 } while (level <= dev->port->disc.max_level);
1945out:
1946 return res;
1947}
1948
1949static int sas_discover_new(struct domain_device *dev, int phy_id)
1950{
1951 struct ex_phy *ex_phy = &dev->ex_dev.ex_phy[phy_id];
1952 struct domain_device *child;
1953 int res;
1954
1955 pr_debug("ex %016llx phy%02d new device attached\n",
1956 SAS_ADDR(dev->sas_addr), phy_id);
1957 res = sas_ex_phy_discover(dev, phy_id);
1958 if (res)
1959 return res;
1960
1961 if (sas_ex_join_wide_port(dev, phy_id))
1962 return 0;
1963
1964 res = sas_ex_discover_devices(dev, phy_id);
1965 if (res)
1966 return res;
1967 list_for_each_entry(child, &dev->ex_dev.children, siblings) {
1968 if (SAS_ADDR(child->sas_addr) ==
1969 SAS_ADDR(ex_phy->attached_sas_addr)) {
1970 if (dev_is_expander(child->dev_type))
1971 res = sas_discover_bfs_by_root(child);
1972 break;
1973 }
1974 }
1975 return res;
1976}
1977
1978static bool dev_type_flutter(enum sas_device_type new, enum sas_device_type old)
1979{
1980 if (old == new)
1981 return true;
1982
1983 /* treat device directed resets as flutter, if we went
1984 * SAS_END_DEVICE to SAS_SATA_PENDING the link needs recovery
1985 */
1986 if ((old == SAS_SATA_PENDING && new == SAS_END_DEVICE) ||
1987 (old == SAS_END_DEVICE && new == SAS_SATA_PENDING))
1988 return true;
1989
1990 return false;
1991}
1992
1993static int sas_rediscover_dev(struct domain_device *dev, int phy_id,
1994 bool last, int sibling)
1995{
1996 struct expander_device *ex = &dev->ex_dev;
1997 struct ex_phy *phy = &ex->ex_phy[phy_id];
1998 enum sas_device_type type = SAS_PHY_UNUSED;
1999 u8 sas_addr[SAS_ADDR_SIZE];
2000 char msg[80] = "";
2001 int res;
2002
2003 if (!last)
2004 sprintf(msg, ", part of a wide port with phy%02d", sibling);
2005
2006 pr_debug("ex %016llx rediscovering phy%02d%s\n",
2007 SAS_ADDR(dev->sas_addr), phy_id, msg);
2008
2009 memset(sas_addr, 0, SAS_ADDR_SIZE);
2010 res = sas_get_phy_attached_dev(dev, phy_id, sas_addr, &type);
2011 switch (res) {
2012 case SMP_RESP_NO_PHY:
2013 phy->phy_state = PHY_NOT_PRESENT;
2014 sas_unregister_devs_sas_addr(dev, phy_id, last);
2015 return res;
2016 case SMP_RESP_PHY_VACANT:
2017 phy->phy_state = PHY_VACANT;
2018 sas_unregister_devs_sas_addr(dev, phy_id, last);
2019 return res;
2020 case SMP_RESP_FUNC_ACC:
2021 break;
2022 case -ECOMM:
2023 break;
2024 default:
2025 return res;
2026 }
2027
2028 if ((SAS_ADDR(sas_addr) == 0) || (res == -ECOMM)) {
2029 phy->phy_state = PHY_EMPTY;
2030 sas_unregister_devs_sas_addr(dev, phy_id, last);
2031 /*
2032 * Even though the PHY is empty, for convenience we discover
2033 * the PHY to update the PHY info, like negotiated linkrate.
2034 */
2035 sas_ex_phy_discover(dev, phy_id);
2036 return res;
2037 } else if (SAS_ADDR(sas_addr) == SAS_ADDR(phy->attached_sas_addr) &&
2038 dev_type_flutter(type, phy->attached_dev_type)) {
2039 struct domain_device *ata_dev = sas_ex_to_ata(dev, phy_id);
2040 char *action = "";
2041
2042 sas_ex_phy_discover(dev, phy_id);
2043
2044 if (ata_dev && phy->attached_dev_type == SAS_SATA_PENDING)
2045 action = ", needs recovery";
2046 pr_debug("ex %016llx phy%02d broadcast flutter%s\n",
2047 SAS_ADDR(dev->sas_addr), phy_id, action);
2048 return res;
2049 }
2050
2051 /* we always have to delete the old device when we went here */
2052 pr_info("ex %016llx phy%02d replace %016llx\n",
2053 SAS_ADDR(dev->sas_addr), phy_id,
2054 SAS_ADDR(phy->attached_sas_addr));
2055 sas_unregister_devs_sas_addr(dev, phy_id, last);
2056
2057 return sas_discover_new(dev, phy_id);
2058}
2059
2060/**
2061 * sas_rediscover - revalidate the domain.
2062 * @dev:domain device to be detect.
2063 * @phy_id: the phy id will be detected.
2064 *
2065 * NOTE: this process _must_ quit (return) as soon as any connection
2066 * errors are encountered. Connection recovery is done elsewhere.
2067 * Discover process only interrogates devices in order to discover the
2068 * domain.For plugging out, we un-register the device only when it is
2069 * the last phy in the port, for other phys in this port, we just delete it
2070 * from the port.For inserting, we do discovery when it is the
2071 * first phy,for other phys in this port, we add it to the port to
2072 * forming the wide-port.
2073 */
2074static int sas_rediscover(struct domain_device *dev, const int phy_id)
2075{
2076 struct expander_device *ex = &dev->ex_dev;
2077 struct ex_phy *changed_phy = &ex->ex_phy[phy_id];
2078 int res = 0;
2079 int i;
2080 bool last = true; /* is this the last phy of the port */
2081
2082 pr_debug("ex %016llx phy%02d originated BROADCAST(CHANGE)\n",
2083 SAS_ADDR(dev->sas_addr), phy_id);
2084
2085 if (SAS_ADDR(changed_phy->attached_sas_addr) != 0) {
2086 for (i = 0; i < ex->num_phys; i++) {
2087 struct ex_phy *phy = &ex->ex_phy[i];
2088
2089 if (i == phy_id)
2090 continue;
2091 if (SAS_ADDR(phy->attached_sas_addr) ==
2092 SAS_ADDR(changed_phy->attached_sas_addr)) {
2093 last = false;
2094 break;
2095 }
2096 }
2097 res = sas_rediscover_dev(dev, phy_id, last, i);
2098 } else
2099 res = sas_discover_new(dev, phy_id);
2100 return res;
2101}
2102
2103/**
2104 * sas_ex_revalidate_domain - revalidate the domain
2105 * @port_dev: port domain device.
2106 *
2107 * NOTE: this process _must_ quit (return) as soon as any connection
2108 * errors are encountered. Connection recovery is done elsewhere.
2109 * Discover process only interrogates devices in order to discover the
2110 * domain.
2111 */
2112int sas_ex_revalidate_domain(struct domain_device *port_dev)
2113{
2114 int res;
2115 struct domain_device *dev = NULL;
2116
2117 res = sas_find_bcast_dev(port_dev, &dev);
2118 if (res == 0 && dev) {
2119 struct expander_device *ex = &dev->ex_dev;
2120 int i = 0, phy_id;
2121
2122 do {
2123 phy_id = -1;
2124 res = sas_find_bcast_phy(dev, &phy_id, i, true);
2125 if (phy_id == -1)
2126 break;
2127 res = sas_rediscover(dev, phy_id);
2128 i = phy_id + 1;
2129 } while (i < ex->num_phys);
2130 }
2131 return res;
2132}
2133
2134void sas_smp_handler(struct bsg_job *job, struct Scsi_Host *shost,
2135 struct sas_rphy *rphy)
2136{
2137 struct domain_device *dev;
2138 unsigned int rcvlen = 0;
2139 int ret = -EINVAL;
2140
2141 /* no rphy means no smp target support (ie aic94xx host) */
2142 if (!rphy)
2143 return sas_smp_host_handler(job, shost);
2144
2145 switch (rphy->identify.device_type) {
2146 case SAS_EDGE_EXPANDER_DEVICE:
2147 case SAS_FANOUT_EXPANDER_DEVICE:
2148 break;
2149 default:
2150 pr_err("%s: can we send a smp request to a device?\n",
2151 __func__);
2152 goto out;
2153 }
2154
2155 dev = sas_find_dev_by_rphy(rphy);
2156 if (!dev) {
2157 pr_err("%s: fail to find a domain_device?\n", __func__);
2158 goto out;
2159 }
2160
2161 /* do we need to support multiple segments? */
2162 if (job->request_payload.sg_cnt > 1 ||
2163 job->reply_payload.sg_cnt > 1) {
2164 pr_info("%s: multiple segments req %u, rsp %u\n",
2165 __func__, job->request_payload.payload_len,
2166 job->reply_payload.payload_len);
2167 goto out;
2168 }
2169
2170 ret = smp_execute_task_sg(dev, job->request_payload.sg_list,
2171 job->reply_payload.sg_list);
2172 if (ret >= 0) {
2173 /* bsg_job_done() requires the length received */
2174 rcvlen = job->reply_payload.payload_len - ret;
2175 ret = 0;
2176 }
2177
2178out:
2179 bsg_job_done(job, ret, rcvlen);
2180}