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 del_timer_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 (!child->parent)
1317 return 0;
1318
1319 if (!dev_is_expander(child->parent->dev_type))
1320 return 0;
1321
1322 parent_ex = &child->parent->ex_dev;
1323
1324 for (i = 0; i < parent_ex->num_phys; i++) {
1325 struct ex_phy *parent_phy = &parent_ex->ex_phy[i];
1326
1327 if (parent_phy->phy_state == PHY_VACANT ||
1328 parent_phy->phy_state == PHY_NOT_PRESENT)
1329 continue;
1330
1331 if (!sas_phy_match_dev_addr(child, parent_phy))
1332 continue;
1333
1334 switch (child->parent->dev_type) {
1335 case SAS_EDGE_EXPANDER_DEVICE:
1336 if (sas_check_edge_expander_topo(child, parent_phy))
1337 res = -ENODEV;
1338 break;
1339 case SAS_FANOUT_EXPANDER_DEVICE:
1340 if (sas_check_fanout_expander_topo(child, parent_phy))
1341 res = -ENODEV;
1342 break;
1343 default:
1344 break;
1345 }
1346 }
1347
1348 return res;
1349 }
1350
1351 #define RRI_REQ_SIZE 16
1352 #define RRI_RESP_SIZE 44
1353
sas_configure_present(struct domain_device * dev,int phy_id,u8 * sas_addr,int * index,int * present)1354 static int sas_configure_present(struct domain_device *dev, int phy_id,
1355 u8 *sas_addr, int *index, int *present)
1356 {
1357 int i, res = 0;
1358 struct expander_device *ex = &dev->ex_dev;
1359 struct ex_phy *phy = &ex->ex_phy[phy_id];
1360 u8 *rri_req;
1361 u8 *rri_resp;
1362
1363 *present = 0;
1364 *index = 0;
1365
1366 rri_req = alloc_smp_req(RRI_REQ_SIZE);
1367 if (!rri_req)
1368 return -ENOMEM;
1369
1370 rri_resp = alloc_smp_resp(RRI_RESP_SIZE);
1371 if (!rri_resp) {
1372 kfree(rri_req);
1373 return -ENOMEM;
1374 }
1375
1376 rri_req[1] = SMP_REPORT_ROUTE_INFO;
1377 rri_req[9] = phy_id;
1378
1379 for (i = 0; i < ex->max_route_indexes ; i++) {
1380 *(__be16 *)(rri_req+6) = cpu_to_be16(i);
1381 res = smp_execute_task(dev, rri_req, RRI_REQ_SIZE, rri_resp,
1382 RRI_RESP_SIZE);
1383 if (res)
1384 goto out;
1385 res = rri_resp[2];
1386 if (res == SMP_RESP_NO_INDEX) {
1387 pr_warn("overflow of indexes: dev %016llx phy%02d index 0x%x\n",
1388 SAS_ADDR(dev->sas_addr), phy_id, i);
1389 goto out;
1390 } else if (res != SMP_RESP_FUNC_ACC) {
1391 pr_notice("%s: dev %016llx phy%02d index 0x%x result 0x%x\n",
1392 __func__, SAS_ADDR(dev->sas_addr), phy_id,
1393 i, res);
1394 goto out;
1395 }
1396 if (SAS_ADDR(sas_addr) != 0) {
1397 if (SAS_ADDR(rri_resp+16) == SAS_ADDR(sas_addr)) {
1398 *index = i;
1399 if ((rri_resp[12] & 0x80) == 0x80)
1400 *present = 0;
1401 else
1402 *present = 1;
1403 goto out;
1404 } else if (SAS_ADDR(rri_resp+16) == 0) {
1405 *index = i;
1406 *present = 0;
1407 goto out;
1408 }
1409 } else if (SAS_ADDR(rri_resp+16) == 0 &&
1410 phy->last_da_index < i) {
1411 phy->last_da_index = i;
1412 *index = i;
1413 *present = 0;
1414 goto out;
1415 }
1416 }
1417 res = -1;
1418 out:
1419 kfree(rri_req);
1420 kfree(rri_resp);
1421 return res;
1422 }
1423
1424 #define CRI_REQ_SIZE 44
1425 #define CRI_RESP_SIZE 8
1426
sas_configure_set(struct domain_device * dev,int phy_id,u8 * sas_addr,int index,int include)1427 static int sas_configure_set(struct domain_device *dev, int phy_id,
1428 u8 *sas_addr, int index, int include)
1429 {
1430 int res;
1431 u8 *cri_req;
1432 u8 *cri_resp;
1433
1434 cri_req = alloc_smp_req(CRI_REQ_SIZE);
1435 if (!cri_req)
1436 return -ENOMEM;
1437
1438 cri_resp = alloc_smp_resp(CRI_RESP_SIZE);
1439 if (!cri_resp) {
1440 kfree(cri_req);
1441 return -ENOMEM;
1442 }
1443
1444 cri_req[1] = SMP_CONF_ROUTE_INFO;
1445 *(__be16 *)(cri_req+6) = cpu_to_be16(index);
1446 cri_req[9] = phy_id;
1447 if (SAS_ADDR(sas_addr) == 0 || !include)
1448 cri_req[12] |= 0x80;
1449 memcpy(cri_req+16, sas_addr, SAS_ADDR_SIZE);
1450
1451 res = smp_execute_task(dev, cri_req, CRI_REQ_SIZE, cri_resp,
1452 CRI_RESP_SIZE);
1453 if (res)
1454 goto out;
1455 res = cri_resp[2];
1456 if (res == SMP_RESP_NO_INDEX) {
1457 pr_warn("overflow of indexes: dev %016llx phy%02d index 0x%x\n",
1458 SAS_ADDR(dev->sas_addr), phy_id, index);
1459 }
1460 out:
1461 kfree(cri_req);
1462 kfree(cri_resp);
1463 return res;
1464 }
1465
sas_configure_phy(struct domain_device * dev,int phy_id,u8 * sas_addr,int include)1466 static int sas_configure_phy(struct domain_device *dev, int phy_id,
1467 u8 *sas_addr, int include)
1468 {
1469 int index;
1470 int present;
1471 int res;
1472
1473 res = sas_configure_present(dev, phy_id, sas_addr, &index, &present);
1474 if (res)
1475 return res;
1476 if (include ^ present)
1477 return sas_configure_set(dev, phy_id, sas_addr, index,
1478 include);
1479
1480 return res;
1481 }
1482
1483 /**
1484 * sas_configure_parent - configure routing table of parent
1485 * @parent: parent expander
1486 * @child: child expander
1487 * @sas_addr: SAS port identifier of device directly attached to child
1488 * @include: whether or not to include @child in the expander routing table
1489 */
sas_configure_parent(struct domain_device * parent,struct domain_device * child,u8 * sas_addr,int include)1490 static int sas_configure_parent(struct domain_device *parent,
1491 struct domain_device *child,
1492 u8 *sas_addr, int include)
1493 {
1494 struct expander_device *ex_parent = &parent->ex_dev;
1495 int res = 0;
1496 int i;
1497
1498 if (parent->parent) {
1499 res = sas_configure_parent(parent->parent, parent, sas_addr,
1500 include);
1501 if (res)
1502 return res;
1503 }
1504
1505 if (ex_parent->conf_route_table == 0) {
1506 pr_debug("ex %016llx has self-configuring routing table\n",
1507 SAS_ADDR(parent->sas_addr));
1508 return 0;
1509 }
1510
1511 for (i = 0; i < ex_parent->num_phys; i++) {
1512 struct ex_phy *phy = &ex_parent->ex_phy[i];
1513
1514 if ((phy->routing_attr == TABLE_ROUTING) &&
1515 sas_phy_match_dev_addr(child, phy)) {
1516 res = sas_configure_phy(parent, i, sas_addr, include);
1517 if (res)
1518 return res;
1519 }
1520 }
1521
1522 return res;
1523 }
1524
1525 /**
1526 * sas_configure_routing - configure routing
1527 * @dev: expander device
1528 * @sas_addr: port identifier of device directly attached to the expander device
1529 */
sas_configure_routing(struct domain_device * dev,u8 * sas_addr)1530 static int sas_configure_routing(struct domain_device *dev, u8 *sas_addr)
1531 {
1532 if (dev->parent)
1533 return sas_configure_parent(dev->parent, dev, sas_addr, 1);
1534 return 0;
1535 }
1536
sas_disable_routing(struct domain_device * dev,u8 * sas_addr)1537 static int sas_disable_routing(struct domain_device *dev, u8 *sas_addr)
1538 {
1539 if (dev->parent)
1540 return sas_configure_parent(dev->parent, dev, sas_addr, 0);
1541 return 0;
1542 }
1543
1544 /**
1545 * sas_discover_expander - expander discovery
1546 * @dev: pointer to expander domain device
1547 *
1548 * See comment in sas_discover_sata().
1549 */
sas_discover_expander(struct domain_device * dev)1550 static int sas_discover_expander(struct domain_device *dev)
1551 {
1552 int res;
1553
1554 res = sas_notify_lldd_dev_found(dev);
1555 if (res)
1556 return res;
1557
1558 res = sas_ex_general(dev);
1559 if (res)
1560 goto out_err;
1561 res = sas_ex_manuf_info(dev);
1562 if (res)
1563 goto out_err;
1564
1565 res = sas_expander_discover(dev);
1566 if (res) {
1567 pr_warn("expander %016llx discovery failed(0x%x)\n",
1568 SAS_ADDR(dev->sas_addr), res);
1569 goto out_err;
1570 }
1571
1572 sas_check_ex_subtractive_boundary(dev);
1573 res = sas_check_parent_topology(dev);
1574 if (res)
1575 goto out_err;
1576 return 0;
1577 out_err:
1578 sas_notify_lldd_dev_gone(dev);
1579 return res;
1580 }
1581
sas_ex_level_discovery(struct asd_sas_port * port,const int level)1582 static int sas_ex_level_discovery(struct asd_sas_port *port, const int level)
1583 {
1584 int res = 0;
1585 struct domain_device *dev;
1586
1587 list_for_each_entry(dev, &port->dev_list, dev_list_node) {
1588 if (dev_is_expander(dev->dev_type)) {
1589 struct sas_expander_device *ex =
1590 rphy_to_expander_device(dev->rphy);
1591
1592 if (level == ex->level)
1593 res = sas_ex_discover_devices(dev, -1);
1594 else if (level > 0)
1595 res = sas_ex_discover_devices(port->port_dev, -1);
1596
1597 }
1598 }
1599
1600 return res;
1601 }
1602
sas_ex_bfs_disc(struct asd_sas_port * port)1603 static int sas_ex_bfs_disc(struct asd_sas_port *port)
1604 {
1605 int res;
1606 int level;
1607
1608 do {
1609 level = port->disc.max_level;
1610 res = sas_ex_level_discovery(port, level);
1611 mb();
1612 } while (level < port->disc.max_level);
1613
1614 return res;
1615 }
1616
sas_discover_root_expander(struct domain_device * dev)1617 int sas_discover_root_expander(struct domain_device *dev)
1618 {
1619 int res;
1620 struct sas_expander_device *ex = rphy_to_expander_device(dev->rphy);
1621
1622 res = sas_rphy_add(dev->rphy);
1623 if (res)
1624 goto out_err;
1625
1626 ex->level = dev->port->disc.max_level; /* 0 */
1627 res = sas_discover_expander(dev);
1628 if (res)
1629 goto out_err2;
1630
1631 sas_ex_bfs_disc(dev->port);
1632
1633 return res;
1634
1635 out_err2:
1636 sas_rphy_remove(dev->rphy);
1637 out_err:
1638 return res;
1639 }
1640
1641 /* ---------- Domain revalidation ---------- */
1642
sas_get_sas_addr_and_dev_type(struct smp_disc_resp * disc_resp,u8 * sas_addr,enum sas_device_type * type)1643 static void sas_get_sas_addr_and_dev_type(struct smp_disc_resp *disc_resp,
1644 u8 *sas_addr,
1645 enum sas_device_type *type)
1646 {
1647 memcpy(sas_addr, disc_resp->disc.attached_sas_addr, SAS_ADDR_SIZE);
1648 *type = to_dev_type(&disc_resp->disc);
1649 if (*type == SAS_PHY_UNUSED)
1650 memset(sas_addr, 0, SAS_ADDR_SIZE);
1651 }
1652
sas_get_phy_discover(struct domain_device * dev,int phy_id,struct smp_disc_resp * disc_resp)1653 static int sas_get_phy_discover(struct domain_device *dev,
1654 int phy_id, struct smp_disc_resp *disc_resp)
1655 {
1656 int res;
1657 u8 *disc_req;
1658
1659 disc_req = alloc_smp_req(DISCOVER_REQ_SIZE);
1660 if (!disc_req)
1661 return -ENOMEM;
1662
1663 disc_req[1] = SMP_DISCOVER;
1664 disc_req[9] = phy_id;
1665
1666 res = smp_execute_task(dev, disc_req, DISCOVER_REQ_SIZE,
1667 disc_resp, DISCOVER_RESP_SIZE);
1668 if (res)
1669 goto out;
1670 if (disc_resp->result != SMP_RESP_FUNC_ACC)
1671 res = disc_resp->result;
1672 out:
1673 kfree(disc_req);
1674 return res;
1675 }
1676
sas_get_phy_change_count(struct domain_device * dev,int phy_id,int * pcc)1677 static int sas_get_phy_change_count(struct domain_device *dev,
1678 int phy_id, int *pcc)
1679 {
1680 int res;
1681 struct smp_disc_resp *disc_resp;
1682
1683 disc_resp = alloc_smp_resp(DISCOVER_RESP_SIZE);
1684 if (!disc_resp)
1685 return -ENOMEM;
1686
1687 res = sas_get_phy_discover(dev, phy_id, disc_resp);
1688 if (!res)
1689 *pcc = disc_resp->disc.change_count;
1690
1691 kfree(disc_resp);
1692 return res;
1693 }
1694
sas_get_phy_attached_dev(struct domain_device * dev,int phy_id,u8 * sas_addr,enum sas_device_type * type)1695 int sas_get_phy_attached_dev(struct domain_device *dev, int phy_id,
1696 u8 *sas_addr, enum sas_device_type *type)
1697 {
1698 int res;
1699 struct smp_disc_resp *disc_resp;
1700
1701 disc_resp = alloc_smp_resp(DISCOVER_RESP_SIZE);
1702 if (!disc_resp)
1703 return -ENOMEM;
1704
1705 res = sas_get_phy_discover(dev, phy_id, disc_resp);
1706 if (res == 0)
1707 sas_get_sas_addr_and_dev_type(disc_resp, sas_addr, type);
1708 kfree(disc_resp);
1709 return res;
1710 }
1711
sas_find_bcast_phy(struct domain_device * dev,int * phy_id,int from_phy,bool update)1712 static int sas_find_bcast_phy(struct domain_device *dev, int *phy_id,
1713 int from_phy, bool update)
1714 {
1715 struct expander_device *ex = &dev->ex_dev;
1716 int res = 0;
1717 int i;
1718
1719 for (i = from_phy; i < ex->num_phys; i++) {
1720 int phy_change_count = 0;
1721
1722 res = sas_get_phy_change_count(dev, i, &phy_change_count);
1723 switch (res) {
1724 case SMP_RESP_PHY_VACANT:
1725 case SMP_RESP_NO_PHY:
1726 continue;
1727 case SMP_RESP_FUNC_ACC:
1728 break;
1729 default:
1730 return res;
1731 }
1732
1733 if (phy_change_count != ex->ex_phy[i].phy_change_count) {
1734 if (update)
1735 ex->ex_phy[i].phy_change_count =
1736 phy_change_count;
1737 *phy_id = i;
1738 return 0;
1739 }
1740 }
1741 return 0;
1742 }
1743
sas_get_ex_change_count(struct domain_device * dev,int * ecc)1744 static int sas_get_ex_change_count(struct domain_device *dev, int *ecc)
1745 {
1746 int res;
1747 u8 *rg_req;
1748 struct smp_rg_resp *rg_resp;
1749
1750 rg_req = alloc_smp_req(RG_REQ_SIZE);
1751 if (!rg_req)
1752 return -ENOMEM;
1753
1754 rg_resp = alloc_smp_resp(RG_RESP_SIZE);
1755 if (!rg_resp) {
1756 kfree(rg_req);
1757 return -ENOMEM;
1758 }
1759
1760 rg_req[1] = SMP_REPORT_GENERAL;
1761
1762 res = smp_execute_task(dev, rg_req, RG_REQ_SIZE, rg_resp,
1763 RG_RESP_SIZE);
1764 if (res)
1765 goto out;
1766 if (rg_resp->result != SMP_RESP_FUNC_ACC) {
1767 res = rg_resp->result;
1768 goto out;
1769 }
1770
1771 *ecc = be16_to_cpu(rg_resp->rg.change_count);
1772 out:
1773 kfree(rg_resp);
1774 kfree(rg_req);
1775 return res;
1776 }
1777 /**
1778 * sas_find_bcast_dev - find the device issue BROADCAST(CHANGE).
1779 * @dev:domain device to be detect.
1780 * @src_dev: the device which originated BROADCAST(CHANGE).
1781 *
1782 * Add self-configuration expander support. Suppose two expander cascading,
1783 * when the first level expander is self-configuring, hotplug the disks in
1784 * second level expander, BROADCAST(CHANGE) will not only be originated
1785 * in the second level expander, but also be originated in the first level
1786 * expander (see SAS protocol SAS 2r-14, 7.11 for detail), it is to say,
1787 * expander changed count in two level expanders will all increment at least
1788 * once, but the phy which chang count has changed is the source device which
1789 * we concerned.
1790 */
1791
sas_find_bcast_dev(struct domain_device * dev,struct domain_device ** src_dev)1792 static int sas_find_bcast_dev(struct domain_device *dev,
1793 struct domain_device **src_dev)
1794 {
1795 struct expander_device *ex = &dev->ex_dev;
1796 int ex_change_count = -1;
1797 int phy_id = -1;
1798 int res;
1799 struct domain_device *ch;
1800
1801 res = sas_get_ex_change_count(dev, &ex_change_count);
1802 if (res)
1803 goto out;
1804 if (ex_change_count != -1 && ex_change_count != ex->ex_change_count) {
1805 /* Just detect if this expander phys phy change count changed,
1806 * in order to determine if this expander originate BROADCAST,
1807 * and do not update phy change count field in our structure.
1808 */
1809 res = sas_find_bcast_phy(dev, &phy_id, 0, false);
1810 if (phy_id != -1) {
1811 *src_dev = dev;
1812 ex->ex_change_count = ex_change_count;
1813 pr_info("ex %016llx phy%02d change count has changed\n",
1814 SAS_ADDR(dev->sas_addr), phy_id);
1815 return res;
1816 } else
1817 pr_info("ex %016llx phys DID NOT change\n",
1818 SAS_ADDR(dev->sas_addr));
1819 }
1820 list_for_each_entry(ch, &ex->children, siblings) {
1821 if (dev_is_expander(ch->dev_type)) {
1822 res = sas_find_bcast_dev(ch, src_dev);
1823 if (*src_dev)
1824 return res;
1825 }
1826 }
1827 out:
1828 return res;
1829 }
1830
sas_unregister_ex_tree(struct asd_sas_port * port,struct domain_device * dev)1831 static void sas_unregister_ex_tree(struct asd_sas_port *port, struct domain_device *dev)
1832 {
1833 struct expander_device *ex = &dev->ex_dev;
1834 struct domain_device *child, *n;
1835
1836 list_for_each_entry_safe(child, n, &ex->children, siblings) {
1837 set_bit(SAS_DEV_GONE, &child->state);
1838 if (dev_is_expander(child->dev_type))
1839 sas_unregister_ex_tree(port, child);
1840 else
1841 sas_unregister_dev(port, child);
1842 }
1843 sas_unregister_dev(port, dev);
1844 }
1845
sas_unregister_devs_sas_addr(struct domain_device * parent,int phy_id,bool last)1846 static void sas_unregister_devs_sas_addr(struct domain_device *parent,
1847 int phy_id, bool last)
1848 {
1849 struct expander_device *ex_dev = &parent->ex_dev;
1850 struct ex_phy *phy = &ex_dev->ex_phy[phy_id];
1851 struct domain_device *child, *n, *found = NULL;
1852 if (last) {
1853 list_for_each_entry_safe(child, n,
1854 &ex_dev->children, siblings) {
1855 if (sas_phy_match_dev_addr(child, phy)) {
1856 set_bit(SAS_DEV_GONE, &child->state);
1857 if (dev_is_expander(child->dev_type))
1858 sas_unregister_ex_tree(parent->port, child);
1859 else
1860 sas_unregister_dev(parent->port, child);
1861 found = child;
1862 break;
1863 }
1864 }
1865 sas_disable_routing(parent, phy->attached_sas_addr);
1866 }
1867 memset(phy->attached_sas_addr, 0, SAS_ADDR_SIZE);
1868 if (phy->port) {
1869 sas_port_delete_phy(phy->port, phy->phy);
1870 sas_device_set_phy(found, phy->port);
1871 if (phy->port->num_phys == 0) {
1872 list_add_tail(&phy->port->del_list,
1873 &parent->port->sas_port_del_list);
1874 if (ex_dev->parent_port == phy->port)
1875 ex_dev->parent_port = NULL;
1876 }
1877 phy->port = NULL;
1878 }
1879 }
1880
sas_discover_bfs_by_root_level(struct domain_device * root,const int level)1881 static int sas_discover_bfs_by_root_level(struct domain_device *root,
1882 const int level)
1883 {
1884 struct expander_device *ex_root = &root->ex_dev;
1885 struct domain_device *child;
1886 int res = 0;
1887
1888 list_for_each_entry(child, &ex_root->children, siblings) {
1889 if (dev_is_expander(child->dev_type)) {
1890 struct sas_expander_device *ex =
1891 rphy_to_expander_device(child->rphy);
1892
1893 if (level > ex->level)
1894 res = sas_discover_bfs_by_root_level(child,
1895 level);
1896 else if (level == ex->level)
1897 res = sas_ex_discover_devices(child, -1);
1898 }
1899 }
1900 return res;
1901 }
1902
sas_discover_bfs_by_root(struct domain_device * dev)1903 static int sas_discover_bfs_by_root(struct domain_device *dev)
1904 {
1905 int res;
1906 struct sas_expander_device *ex = rphy_to_expander_device(dev->rphy);
1907 int level = ex->level+1;
1908
1909 res = sas_ex_discover_devices(dev, -1);
1910 if (res)
1911 goto out;
1912 do {
1913 res = sas_discover_bfs_by_root_level(dev, level);
1914 mb();
1915 level += 1;
1916 } while (level <= dev->port->disc.max_level);
1917 out:
1918 return res;
1919 }
1920
sas_discover_new(struct domain_device * dev,int phy_id)1921 static int sas_discover_new(struct domain_device *dev, int phy_id)
1922 {
1923 struct ex_phy *ex_phy = &dev->ex_dev.ex_phy[phy_id];
1924 struct domain_device *child;
1925 int res;
1926
1927 pr_debug("ex %016llx phy%02d new device attached\n",
1928 SAS_ADDR(dev->sas_addr), phy_id);
1929 res = sas_ex_phy_discover(dev, phy_id);
1930 if (res)
1931 return res;
1932
1933 if (sas_ex_join_wide_port(dev, phy_id))
1934 return 0;
1935
1936 res = sas_ex_discover_devices(dev, phy_id);
1937 if (res)
1938 return res;
1939 list_for_each_entry(child, &dev->ex_dev.children, siblings) {
1940 if (sas_phy_match_dev_addr(child, ex_phy)) {
1941 if (dev_is_expander(child->dev_type))
1942 res = sas_discover_bfs_by_root(child);
1943 break;
1944 }
1945 }
1946 return res;
1947 }
1948
dev_type_flutter(enum sas_device_type new,enum sas_device_type old)1949 static bool dev_type_flutter(enum sas_device_type new, enum sas_device_type old)
1950 {
1951 if (old == new)
1952 return true;
1953
1954 /* treat device directed resets as flutter, if we went
1955 * SAS_END_DEVICE to SAS_SATA_PENDING the link needs recovery
1956 */
1957 if ((old == SAS_SATA_PENDING && new == SAS_END_DEVICE) ||
1958 (old == SAS_END_DEVICE && new == SAS_SATA_PENDING))
1959 return true;
1960
1961 return false;
1962 }
1963
sas_rediscover_dev(struct domain_device * dev,int phy_id,bool last,int sibling)1964 static int sas_rediscover_dev(struct domain_device *dev, int phy_id,
1965 bool last, int sibling)
1966 {
1967 struct expander_device *ex = &dev->ex_dev;
1968 struct ex_phy *phy = &ex->ex_phy[phy_id];
1969 enum sas_device_type type = SAS_PHY_UNUSED;
1970 struct smp_disc_resp *disc_resp;
1971 u8 sas_addr[SAS_ADDR_SIZE];
1972 char msg[80] = "";
1973 int res;
1974
1975 if (!last)
1976 sprintf(msg, ", part of a wide port with phy%02d", sibling);
1977
1978 pr_debug("ex %016llx rediscovering phy%02d%s\n",
1979 SAS_ADDR(dev->sas_addr), phy_id, msg);
1980
1981 memset(sas_addr, 0, SAS_ADDR_SIZE);
1982 disc_resp = alloc_smp_resp(DISCOVER_RESP_SIZE);
1983 if (!disc_resp)
1984 return -ENOMEM;
1985
1986 res = sas_get_phy_discover(dev, phy_id, disc_resp);
1987 switch (res) {
1988 case SMP_RESP_NO_PHY:
1989 phy->phy_state = PHY_NOT_PRESENT;
1990 sas_unregister_devs_sas_addr(dev, phy_id, last);
1991 goto out_free_resp;
1992 case SMP_RESP_PHY_VACANT:
1993 phy->phy_state = PHY_VACANT;
1994 sas_unregister_devs_sas_addr(dev, phy_id, last);
1995 goto out_free_resp;
1996 case SMP_RESP_FUNC_ACC:
1997 break;
1998 case -ECOMM:
1999 break;
2000 default:
2001 goto out_free_resp;
2002 }
2003
2004 if (res == 0)
2005 sas_get_sas_addr_and_dev_type(disc_resp, sas_addr, &type);
2006
2007 if ((SAS_ADDR(sas_addr) == 0) || (res == -ECOMM)) {
2008 phy->phy_state = PHY_EMPTY;
2009 sas_unregister_devs_sas_addr(dev, phy_id, last);
2010 /*
2011 * Even though the PHY is empty, for convenience we update
2012 * the PHY info, like negotiated linkrate.
2013 */
2014 if (res == 0)
2015 sas_set_ex_phy(dev, phy_id, disc_resp);
2016 goto out_free_resp;
2017 } else if (SAS_ADDR(sas_addr) == SAS_ADDR(phy->attached_sas_addr) &&
2018 dev_type_flutter(type, phy->attached_dev_type)) {
2019 struct domain_device *ata_dev = sas_ex_to_ata(dev, phy_id);
2020 char *action = "";
2021
2022 sas_ex_phy_discover(dev, phy_id);
2023
2024 if (ata_dev && phy->attached_dev_type == SAS_SATA_PENDING)
2025 action = ", needs recovery";
2026 pr_debug("ex %016llx phy%02d broadcast flutter%s\n",
2027 SAS_ADDR(dev->sas_addr), phy_id, action);
2028 goto out_free_resp;
2029 }
2030
2031 /* we always have to delete the old device when we went here */
2032 pr_info("ex %016llx phy%02d replace %016llx\n",
2033 SAS_ADDR(dev->sas_addr), phy_id,
2034 SAS_ADDR(phy->attached_sas_addr));
2035 sas_unregister_devs_sas_addr(dev, phy_id, last);
2036
2037 res = sas_discover_new(dev, phy_id);
2038 out_free_resp:
2039 kfree(disc_resp);
2040 return res;
2041 }
2042
2043 /**
2044 * sas_rediscover - revalidate the domain.
2045 * @dev:domain device to be detect.
2046 * @phy_id: the phy id will be detected.
2047 *
2048 * NOTE: this process _must_ quit (return) as soon as any connection
2049 * errors are encountered. Connection recovery is done elsewhere.
2050 * Discover process only interrogates devices in order to discover the
2051 * domain.For plugging out, we un-register the device only when it is
2052 * the last phy in the port, for other phys in this port, we just delete it
2053 * from the port.For inserting, we do discovery when it is the
2054 * first phy,for other phys in this port, we add it to the port to
2055 * forming the wide-port.
2056 */
sas_rediscover(struct domain_device * dev,const int phy_id)2057 static int sas_rediscover(struct domain_device *dev, const int phy_id)
2058 {
2059 struct expander_device *ex = &dev->ex_dev;
2060 struct ex_phy *changed_phy = &ex->ex_phy[phy_id];
2061 int res = 0;
2062 int i;
2063 bool last = true; /* is this the last phy of the port */
2064
2065 pr_debug("ex %016llx phy%02d originated BROADCAST(CHANGE)\n",
2066 SAS_ADDR(dev->sas_addr), phy_id);
2067
2068 if (SAS_ADDR(changed_phy->attached_sas_addr) != 0) {
2069 for (i = 0; i < ex->num_phys; i++) {
2070 struct ex_phy *phy = &ex->ex_phy[i];
2071
2072 if (i == phy_id)
2073 continue;
2074 if (sas_phy_addr_match(phy, changed_phy)) {
2075 last = false;
2076 break;
2077 }
2078 }
2079 res = sas_rediscover_dev(dev, phy_id, last, i);
2080 } else
2081 res = sas_discover_new(dev, phy_id);
2082 return res;
2083 }
2084
2085 /**
2086 * sas_ex_revalidate_domain - revalidate the domain
2087 * @port_dev: port domain device.
2088 *
2089 * NOTE: this process _must_ quit (return) as soon as any connection
2090 * errors are encountered. Connection recovery is done elsewhere.
2091 * Discover process only interrogates devices in order to discover the
2092 * domain.
2093 */
sas_ex_revalidate_domain(struct domain_device * port_dev)2094 int sas_ex_revalidate_domain(struct domain_device *port_dev)
2095 {
2096 int res;
2097 struct domain_device *dev = NULL;
2098
2099 res = sas_find_bcast_dev(port_dev, &dev);
2100 if (res == 0 && dev) {
2101 struct expander_device *ex = &dev->ex_dev;
2102 int i = 0, phy_id;
2103
2104 do {
2105 phy_id = -1;
2106 res = sas_find_bcast_phy(dev, &phy_id, i, true);
2107 if (phy_id == -1)
2108 break;
2109 res = sas_rediscover(dev, phy_id);
2110 i = phy_id + 1;
2111 } while (i < ex->num_phys);
2112 }
2113 return res;
2114 }
2115
sas_find_attached_phy_id(struct expander_device * ex_dev,struct domain_device * dev)2116 int sas_find_attached_phy_id(struct expander_device *ex_dev,
2117 struct domain_device *dev)
2118 {
2119 struct ex_phy *phy;
2120 int phy_id;
2121
2122 for (phy_id = 0; phy_id < ex_dev->num_phys; phy_id++) {
2123 phy = &ex_dev->ex_phy[phy_id];
2124 if (sas_phy_match_dev_addr(dev, phy))
2125 return phy_id;
2126 }
2127
2128 return -ENODEV;
2129 }
2130 EXPORT_SYMBOL_GPL(sas_find_attached_phy_id);
2131
sas_smp_handler(struct bsg_job * job,struct Scsi_Host * shost,struct sas_rphy * rphy)2132 void sas_smp_handler(struct bsg_job *job, struct Scsi_Host *shost,
2133 struct sas_rphy *rphy)
2134 {
2135 struct domain_device *dev;
2136 unsigned int rcvlen = 0;
2137 int ret = -EINVAL;
2138
2139 /* no rphy means no smp target support (ie aic94xx host) */
2140 if (!rphy)
2141 return sas_smp_host_handler(job, shost);
2142
2143 switch (rphy->identify.device_type) {
2144 case SAS_EDGE_EXPANDER_DEVICE:
2145 case SAS_FANOUT_EXPANDER_DEVICE:
2146 break;
2147 default:
2148 pr_err("%s: can we send a smp request to a device?\n",
2149 __func__);
2150 goto out;
2151 }
2152
2153 dev = sas_find_dev_by_rphy(rphy);
2154 if (!dev) {
2155 pr_err("%s: fail to find a domain_device?\n", __func__);
2156 goto out;
2157 }
2158
2159 /* do we need to support multiple segments? */
2160 if (job->request_payload.sg_cnt > 1 ||
2161 job->reply_payload.sg_cnt > 1) {
2162 pr_info("%s: multiple segments req %u, rsp %u\n",
2163 __func__, job->request_payload.payload_len,
2164 job->reply_payload.payload_len);
2165 goto out;
2166 }
2167
2168 ret = smp_execute_task_sg(dev, job->request_payload.sg_list,
2169 job->reply_payload.sg_list);
2170 if (ret >= 0) {
2171 /* bsg_job_done() requires the length received */
2172 rcvlen = job->reply_payload.payload_len - ret;
2173 ret = 0;
2174 }
2175
2176 out:
2177 bsg_job_done(job, ret, rcvlen);
2178 }
2179