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