aboutsummaryrefslogtreecommitdiffstats
path: root/drivers/scsi/libsas/sas_expander.c
diff options
context:
space:
mode:
Diffstat (limited to 'drivers/scsi/libsas/sas_expander.c')
-rw-r--r--drivers/scsi/libsas/sas_expander.c1862
1 files changed, 1862 insertions, 0 deletions
diff --git a/drivers/scsi/libsas/sas_expander.c b/drivers/scsi/libsas/sas_expander.c
new file mode 100644
index 000000000000..b653a263f76a
--- /dev/null
+++ b/drivers/scsi/libsas/sas_expander.c
@@ -0,0 +1,1862 @@
1/*
2 * Serial Attached SCSI (SAS) Expander discovery and configuration
3 *
4 * Copyright (C) 2005 Adaptec, Inc. All rights reserved.
5 * Copyright (C) 2005 Luben Tuikov <luben_tuikov@adaptec.com>
6 *
7 * This file is licensed under GPLv2.
8 *
9 * This program is free software; you can redistribute it and/or
10 * modify it under the terms of the GNU General Public License as
11 * published by the Free Software Foundation; either version 2 of the
12 * License, or (at your option) any later version.
13 *
14 * This program is distributed in the hope that it will be useful, but
15 * WITHOUT ANY WARRANTY; without even the implied warranty of
16 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
17 * General Public License for more details.
18 *
19 * You should have received a copy of the GNU General Public License
20 * along with this program; if not, write to the Free Software
21 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
22 *
23 */
24
25#include <linux/pci.h>
26#include <linux/scatterlist.h>
27
28#include "sas_internal.h"
29
30#include <scsi/scsi_transport.h>
31#include <scsi/scsi_transport_sas.h>
32#include "../scsi_sas_internal.h"
33
34static int sas_discover_expander(struct domain_device *dev);
35static int sas_configure_routing(struct domain_device *dev, u8 *sas_addr);
36static int sas_configure_phy(struct domain_device *dev, int phy_id,
37 u8 *sas_addr, int include);
38static int sas_disable_routing(struct domain_device *dev, u8 *sas_addr);
39
40#if 0
41/* FIXME: smp needs to migrate into the sas class */
42static ssize_t smp_portal_read(struct kobject *, char *, loff_t, size_t);
43static ssize_t smp_portal_write(struct kobject *, char *, loff_t, size_t);
44#endif
45
46/* ---------- SMP task management ---------- */
47
48static void smp_task_timedout(unsigned long _task)
49{
50 struct sas_task *task = (void *) _task;
51 unsigned long flags;
52
53 spin_lock_irqsave(&task->task_state_lock, flags);
54 if (!(task->task_state_flags & SAS_TASK_STATE_DONE))
55 task->task_state_flags |= SAS_TASK_STATE_ABORTED;
56 spin_unlock_irqrestore(&task->task_state_lock, flags);
57
58 complete(&task->completion);
59}
60
61static void smp_task_done(struct sas_task *task)
62{
63 if (!del_timer(&task->timer))
64 return;
65 complete(&task->completion);
66}
67
68/* Give it some long enough timeout. In seconds. */
69#define SMP_TIMEOUT 10
70
71static int smp_execute_task(struct domain_device *dev, void *req, int req_size,
72 void *resp, int resp_size)
73{
74 int res;
75 struct sas_task *task = sas_alloc_task(GFP_KERNEL);
76 struct sas_internal *i =
77 to_sas_internal(dev->port->ha->core.shost->transportt);
78
79 if (!task)
80 return -ENOMEM;
81
82 task->dev = dev;
83 task->task_proto = dev->tproto;
84 sg_init_one(&task->smp_task.smp_req, req, req_size);
85 sg_init_one(&task->smp_task.smp_resp, resp, resp_size);
86
87 task->task_done = smp_task_done;
88
89 task->timer.data = (unsigned long) task;
90 task->timer.function = smp_task_timedout;
91 task->timer.expires = jiffies + SMP_TIMEOUT*HZ;
92 add_timer(&task->timer);
93
94 res = i->dft->lldd_execute_task(task, 1, GFP_KERNEL);
95
96 if (res) {
97 del_timer(&task->timer);
98 SAS_DPRINTK("executing SMP task failed:%d\n", res);
99 goto ex_err;
100 }
101
102 wait_for_completion(&task->completion);
103 res = -ETASK;
104 if ((task->task_state_flags & SAS_TASK_STATE_ABORTED)) {
105 SAS_DPRINTK("smp task timed out or aborted\n");
106 i->dft->lldd_abort_task(task);
107 if (!(task->task_state_flags & SAS_TASK_STATE_DONE)) {
108 SAS_DPRINTK("SMP task aborted and not done\n");
109 goto ex_err;
110 }
111 }
112 if (task->task_status.resp == SAS_TASK_COMPLETE &&
113 task->task_status.stat == SAM_GOOD)
114 res = 0;
115 else
116 SAS_DPRINTK("%s: task to dev %016llx response: 0x%x "
117 "status 0x%x\n", __FUNCTION__,
118 SAS_ADDR(dev->sas_addr),
119 task->task_status.resp,
120 task->task_status.stat);
121ex_err:
122 sas_free_task(task);
123 return res;
124}
125
126/* ---------- Allocations ---------- */
127
128static inline void *alloc_smp_req(int size)
129{
130 u8 *p = kzalloc(size, GFP_KERNEL);
131 if (p)
132 p[0] = SMP_REQUEST;
133 return p;
134}
135
136static inline void *alloc_smp_resp(int size)
137{
138 return kzalloc(size, GFP_KERNEL);
139}
140
141/* ---------- Expander configuration ---------- */
142
143static void sas_set_ex_phy(struct domain_device *dev, int phy_id,
144 void *disc_resp)
145{
146 struct expander_device *ex = &dev->ex_dev;
147 struct ex_phy *phy = &ex->ex_phy[phy_id];
148 struct smp_resp *resp = disc_resp;
149 struct discover_resp *dr = &resp->disc;
150 struct sas_rphy *rphy = dev->rphy;
151 int rediscover = (phy->phy != NULL);
152
153 if (!rediscover) {
154 phy->phy = sas_phy_alloc(&rphy->dev, phy_id);
155
156 /* FIXME: error_handling */
157 BUG_ON(!phy->phy);
158 }
159
160 switch (resp->result) {
161 case SMP_RESP_PHY_VACANT:
162 phy->phy_state = PHY_VACANT;
163 return;
164 default:
165 phy->phy_state = PHY_NOT_PRESENT;
166 return;
167 case SMP_RESP_FUNC_ACC:
168 phy->phy_state = PHY_EMPTY; /* do not know yet */
169 break;
170 }
171
172 phy->phy_id = phy_id;
173 phy->attached_dev_type = dr->attached_dev_type;
174 phy->linkrate = dr->linkrate;
175 phy->attached_sata_host = dr->attached_sata_host;
176 phy->attached_sata_dev = dr->attached_sata_dev;
177 phy->attached_sata_ps = dr->attached_sata_ps;
178 phy->attached_iproto = dr->iproto << 1;
179 phy->attached_tproto = dr->tproto << 1;
180 memcpy(phy->attached_sas_addr, dr->attached_sas_addr, SAS_ADDR_SIZE);
181 phy->attached_phy_id = dr->attached_phy_id;
182 phy->phy_change_count = dr->change_count;
183 phy->routing_attr = dr->routing_attr;
184 phy->virtual = dr->virtual;
185 phy->last_da_index = -1;
186
187 phy->phy->identify.initiator_port_protocols = phy->attached_iproto;
188 phy->phy->identify.target_port_protocols = phy->attached_tproto;
189 phy->phy->identify.phy_identifier = phy_id;
190 phy->phy->minimum_linkrate_hw = SAS_LINK_RATE_1_5_GBPS;
191 phy->phy->maximum_linkrate_hw = SAS_LINK_RATE_3_0_GBPS;
192 phy->phy->minimum_linkrate = SAS_LINK_RATE_1_5_GBPS;
193 phy->phy->maximum_linkrate = SAS_LINK_RATE_3_0_GBPS;
194 switch (phy->linkrate) {
195 case PHY_LINKRATE_1_5:
196 phy->phy->negotiated_linkrate = SAS_LINK_RATE_1_5_GBPS;
197 break;
198 case PHY_LINKRATE_3:
199 phy->phy->negotiated_linkrate = SAS_LINK_RATE_3_0_GBPS;
200 break;
201 case PHY_LINKRATE_6:
202 phy->phy->negotiated_linkrate = SAS_LINK_RATE_6_0_GBPS;
203 break;
204 default:
205 phy->phy->negotiated_linkrate = SAS_LINK_RATE_UNKNOWN;
206 break;
207 }
208
209 if (!rediscover)
210 sas_phy_add(phy->phy);
211
212 SAS_DPRINTK("ex %016llx phy%02d:%c attached: %016llx\n",
213 SAS_ADDR(dev->sas_addr), phy->phy_id,
214 phy->routing_attr == TABLE_ROUTING ? 'T' :
215 phy->routing_attr == DIRECT_ROUTING ? 'D' :
216 phy->routing_attr == SUBTRACTIVE_ROUTING ? 'S' : '?',
217 SAS_ADDR(phy->attached_sas_addr));
218
219 return;
220}
221
222#define DISCOVER_REQ_SIZE 16
223#define DISCOVER_RESP_SIZE 56
224
225static int sas_ex_phy_discover(struct domain_device *dev, int single)
226{
227 struct expander_device *ex = &dev->ex_dev;
228 int res = 0;
229 u8 *disc_req;
230 u8 *disc_resp;
231
232 disc_req = alloc_smp_req(DISCOVER_REQ_SIZE);
233 if (!disc_req)
234 return -ENOMEM;
235
236 disc_resp = alloc_smp_req(DISCOVER_RESP_SIZE);
237 if (!disc_resp) {
238 kfree(disc_req);
239 return -ENOMEM;
240 }
241
242 disc_req[1] = SMP_DISCOVER;
243
244 if (0 <= single && single < ex->num_phys) {
245 disc_req[9] = single;
246 res = smp_execute_task(dev, disc_req, DISCOVER_REQ_SIZE,
247 disc_resp, DISCOVER_RESP_SIZE);
248 if (res)
249 goto out_err;
250 sas_set_ex_phy(dev, single, disc_resp);
251 } else {
252 int i;
253
254 for (i = 0; i < ex->num_phys; i++) {
255 disc_req[9] = i;
256 res = smp_execute_task(dev, disc_req,
257 DISCOVER_REQ_SIZE, disc_resp,
258 DISCOVER_RESP_SIZE);
259 if (res)
260 goto out_err;
261 sas_set_ex_phy(dev, i, disc_resp);
262 }
263 }
264out_err:
265 kfree(disc_resp);
266 kfree(disc_req);
267 return res;
268}
269
270static int sas_expander_discover(struct domain_device *dev)
271{
272 struct expander_device *ex = &dev->ex_dev;
273 int res = -ENOMEM;
274
275 ex->ex_phy = kzalloc(sizeof(*ex->ex_phy)*ex->num_phys, GFP_KERNEL);
276 if (!ex->ex_phy)
277 return -ENOMEM;
278
279 res = sas_ex_phy_discover(dev, -1);
280 if (res)
281 goto out_err;
282
283 return 0;
284 out_err:
285 kfree(ex->ex_phy);
286 ex->ex_phy = NULL;
287 return res;
288}
289
290#define MAX_EXPANDER_PHYS 128
291
292static void ex_assign_report_general(struct domain_device *dev,
293 struct smp_resp *resp)
294{
295 struct report_general_resp *rg = &resp->rg;
296
297 dev->ex_dev.ex_change_count = be16_to_cpu(rg->change_count);
298 dev->ex_dev.max_route_indexes = be16_to_cpu(rg->route_indexes);
299 dev->ex_dev.num_phys = min(rg->num_phys, (u8)MAX_EXPANDER_PHYS);
300 dev->ex_dev.conf_route_table = rg->conf_route_table;
301 dev->ex_dev.configuring = rg->configuring;
302 memcpy(dev->ex_dev.enclosure_logical_id, rg->enclosure_logical_id, 8);
303}
304
305#define RG_REQ_SIZE 8
306#define RG_RESP_SIZE 32
307
308static int sas_ex_general(struct domain_device *dev)
309{
310 u8 *rg_req;
311 struct smp_resp *rg_resp;
312 int res;
313 int i;
314
315 rg_req = alloc_smp_req(RG_REQ_SIZE);
316 if (!rg_req)
317 return -ENOMEM;
318
319 rg_resp = alloc_smp_resp(RG_RESP_SIZE);
320 if (!rg_resp) {
321 kfree(rg_req);
322 return -ENOMEM;
323 }
324
325 rg_req[1] = SMP_REPORT_GENERAL;
326
327 for (i = 0; i < 5; i++) {
328 res = smp_execute_task(dev, rg_req, RG_REQ_SIZE, rg_resp,
329 RG_RESP_SIZE);
330
331 if (res) {
332 SAS_DPRINTK("RG to ex %016llx failed:0x%x\n",
333 SAS_ADDR(dev->sas_addr), res);
334 goto out;
335 } else if (rg_resp->result != SMP_RESP_FUNC_ACC) {
336 SAS_DPRINTK("RG:ex %016llx returned SMP result:0x%x\n",
337 SAS_ADDR(dev->sas_addr), rg_resp->result);
338 res = rg_resp->result;
339 goto out;
340 }
341
342 ex_assign_report_general(dev, rg_resp);
343
344 if (dev->ex_dev.configuring) {
345 SAS_DPRINTK("RG: ex %llx self-configuring...\n",
346 SAS_ADDR(dev->sas_addr));
347 schedule_timeout_interruptible(5*HZ);
348 } else
349 break;
350 }
351out:
352 kfree(rg_req);
353 kfree(rg_resp);
354 return res;
355}
356
357static void ex_assign_manuf_info(struct domain_device *dev, void
358 *_mi_resp)
359{
360 u8 *mi_resp = _mi_resp;
361 struct sas_rphy *rphy = dev->rphy;
362 struct sas_expander_device *edev = rphy_to_expander_device(rphy);
363
364 memcpy(edev->vendor_id, mi_resp + 12, SAS_EXPANDER_VENDOR_ID_LEN);
365 memcpy(edev->product_id, mi_resp + 20, SAS_EXPANDER_PRODUCT_ID_LEN);
366 memcpy(edev->product_rev, mi_resp + 36,
367 SAS_EXPANDER_PRODUCT_REV_LEN);
368
369 if (mi_resp[8] & 1) {
370 memcpy(edev->component_vendor_id, mi_resp + 40,
371 SAS_EXPANDER_COMPONENT_VENDOR_ID_LEN);
372 edev->component_id = mi_resp[48] << 8 | mi_resp[49];
373 edev->component_revision_id = mi_resp[50];
374 }
375}
376
377#define MI_REQ_SIZE 8
378#define MI_RESP_SIZE 64
379
380static int sas_ex_manuf_info(struct domain_device *dev)
381{
382 u8 *mi_req;
383 u8 *mi_resp;
384 int res;
385
386 mi_req = alloc_smp_req(MI_REQ_SIZE);
387 if (!mi_req)
388 return -ENOMEM;
389
390 mi_resp = alloc_smp_resp(MI_RESP_SIZE);
391 if (!mi_resp) {
392 kfree(mi_req);
393 return -ENOMEM;
394 }
395
396 mi_req[1] = SMP_REPORT_MANUF_INFO;
397
398 res = smp_execute_task(dev, mi_req, MI_REQ_SIZE, mi_resp,MI_RESP_SIZE);
399 if (res) {
400 SAS_DPRINTK("MI: ex %016llx failed:0x%x\n",
401 SAS_ADDR(dev->sas_addr), res);
402 goto out;
403 } else if (mi_resp[2] != SMP_RESP_FUNC_ACC) {
404 SAS_DPRINTK("MI ex %016llx returned SMP result:0x%x\n",
405 SAS_ADDR(dev->sas_addr), mi_resp[2]);
406 goto out;
407 }
408
409 ex_assign_manuf_info(dev, mi_resp);
410out:
411 kfree(mi_req);
412 kfree(mi_resp);
413 return res;
414}
415
416#define PC_REQ_SIZE 44
417#define PC_RESP_SIZE 8
418
419int sas_smp_phy_control(struct domain_device *dev, int phy_id,
420 enum phy_func phy_func)
421{
422 u8 *pc_req;
423 u8 *pc_resp;
424 int res;
425
426 pc_req = alloc_smp_req(PC_REQ_SIZE);
427 if (!pc_req)
428 return -ENOMEM;
429
430 pc_resp = alloc_smp_resp(PC_RESP_SIZE);
431 if (!pc_resp) {
432 kfree(pc_req);
433 return -ENOMEM;
434 }
435
436 pc_req[1] = SMP_PHY_CONTROL;
437 pc_req[9] = phy_id;
438 pc_req[10]= phy_func;
439
440 res = smp_execute_task(dev, pc_req, PC_REQ_SIZE, pc_resp,PC_RESP_SIZE);
441
442 kfree(pc_resp);
443 kfree(pc_req);
444 return res;
445}
446
447static void sas_ex_disable_phy(struct domain_device *dev, int phy_id)
448{
449 struct expander_device *ex = &dev->ex_dev;
450 struct ex_phy *phy = &ex->ex_phy[phy_id];
451
452 sas_smp_phy_control(dev, phy_id, PHY_FUNC_DISABLE);
453 phy->linkrate = PHY_DISABLED;
454}
455
456static void sas_ex_disable_port(struct domain_device *dev, u8 *sas_addr)
457{
458 struct expander_device *ex = &dev->ex_dev;
459 int i;
460
461 for (i = 0; i < ex->num_phys; i++) {
462 struct ex_phy *phy = &ex->ex_phy[i];
463
464 if (phy->phy_state == PHY_VACANT ||
465 phy->phy_state == PHY_NOT_PRESENT)
466 continue;
467
468 if (SAS_ADDR(phy->attached_sas_addr) == SAS_ADDR(sas_addr))
469 sas_ex_disable_phy(dev, i);
470 }
471}
472
473static int sas_dev_present_in_domain(struct asd_sas_port *port,
474 u8 *sas_addr)
475{
476 struct domain_device *dev;
477
478 if (SAS_ADDR(port->sas_addr) == SAS_ADDR(sas_addr))
479 return 1;
480 list_for_each_entry(dev, &port->dev_list, dev_list_node) {
481 if (SAS_ADDR(dev->sas_addr) == SAS_ADDR(sas_addr))
482 return 1;
483 }
484 return 0;
485}
486
487#define RPEL_REQ_SIZE 16
488#define RPEL_RESP_SIZE 32
489int sas_smp_get_phy_events(struct sas_phy *phy)
490{
491 int res;
492 struct sas_rphy *rphy = dev_to_rphy(phy->dev.parent);
493 struct domain_device *dev = sas_find_dev_by_rphy(rphy);
494 u8 *req = alloc_smp_req(RPEL_REQ_SIZE);
495 u8 *resp = kzalloc(RPEL_RESP_SIZE, GFP_KERNEL);
496
497 if (!resp)
498 return -ENOMEM;
499
500 req[1] = SMP_REPORT_PHY_ERR_LOG;
501 req[9] = phy->number;
502
503 res = smp_execute_task(dev, req, RPEL_REQ_SIZE,
504 resp, RPEL_RESP_SIZE);
505
506 if (!res)
507 goto out;
508
509 phy->invalid_dword_count = scsi_to_u32(&resp[12]);
510 phy->running_disparity_error_count = scsi_to_u32(&resp[16]);
511 phy->loss_of_dword_sync_count = scsi_to_u32(&resp[20]);
512 phy->phy_reset_problem_count = scsi_to_u32(&resp[24]);
513
514 out:
515 kfree(resp);
516 return res;
517
518}
519
520#define RPS_REQ_SIZE 16
521#define RPS_RESP_SIZE 60
522
523static int sas_get_report_phy_sata(struct domain_device *dev,
524 int phy_id,
525 struct smp_resp *rps_resp)
526{
527 int res;
528 u8 *rps_req = alloc_smp_req(RPS_REQ_SIZE);
529
530 if (!rps_req)
531 return -ENOMEM;
532
533 rps_req[1] = SMP_REPORT_PHY_SATA;
534 rps_req[9] = phy_id;
535
536 res = smp_execute_task(dev, rps_req, RPS_REQ_SIZE,
537 rps_resp, RPS_RESP_SIZE);
538
539 kfree(rps_req);
540 return 0;
541}
542
543static void sas_ex_get_linkrate(struct domain_device *parent,
544 struct domain_device *child,
545 struct ex_phy *parent_phy)
546{
547 struct expander_device *parent_ex = &parent->ex_dev;
548 struct sas_port *port;
549 int i;
550
551 child->pathways = 0;
552
553 port = parent_phy->port;
554
555 for (i = 0; i < parent_ex->num_phys; i++) {
556 struct ex_phy *phy = &parent_ex->ex_phy[i];
557
558 if (phy->phy_state == PHY_VACANT ||
559 phy->phy_state == PHY_NOT_PRESENT)
560 continue;
561
562 if (SAS_ADDR(phy->attached_sas_addr) ==
563 SAS_ADDR(child->sas_addr)) {
564
565 child->min_linkrate = min(parent->min_linkrate,
566 phy->linkrate);
567 child->max_linkrate = max(parent->max_linkrate,
568 phy->linkrate);
569 child->pathways++;
570 sas_port_add_phy(port, phy->phy);
571 }
572 }
573 child->linkrate = min(parent_phy->linkrate, child->max_linkrate);
574 child->pathways = min(child->pathways, parent->pathways);
575}
576
577static struct domain_device *sas_ex_discover_end_dev(
578 struct domain_device *parent, int phy_id)
579{
580 struct expander_device *parent_ex = &parent->ex_dev;
581 struct ex_phy *phy = &parent_ex->ex_phy[phy_id];
582 struct domain_device *child = NULL;
583 struct sas_rphy *rphy;
584 int res;
585
586 if (phy->attached_sata_host || phy->attached_sata_ps)
587 return NULL;
588
589 child = kzalloc(sizeof(*child), GFP_KERNEL);
590 if (!child)
591 return NULL;
592
593 child->parent = parent;
594 child->port = parent->port;
595 child->iproto = phy->attached_iproto;
596 memcpy(child->sas_addr, phy->attached_sas_addr, SAS_ADDR_SIZE);
597 sas_hash_addr(child->hashed_sas_addr, child->sas_addr);
598 phy->port = sas_port_alloc(&parent->rphy->dev, phy_id);
599 BUG_ON(!phy->port);
600 /* FIXME: better error handling*/
601 BUG_ON(sas_port_add(phy->port) != 0);
602 sas_ex_get_linkrate(parent, child, phy);
603
604 if ((phy->attached_tproto & SAS_PROTO_STP) || phy->attached_sata_dev) {
605 child->dev_type = SATA_DEV;
606 if (phy->attached_tproto & SAS_PROTO_STP)
607 child->tproto = phy->attached_tproto;
608 if (phy->attached_sata_dev)
609 child->tproto |= SATA_DEV;
610 res = sas_get_report_phy_sata(parent, phy_id,
611 &child->sata_dev.rps_resp);
612 if (res) {
613 SAS_DPRINTK("report phy sata to %016llx:0x%x returned "
614 "0x%x\n", SAS_ADDR(parent->sas_addr),
615 phy_id, res);
616 kfree(child);
617 return NULL;
618 }
619 memcpy(child->frame_rcvd, &child->sata_dev.rps_resp.rps.fis,
620 sizeof(struct dev_to_host_fis));
621 sas_init_dev(child);
622 res = sas_discover_sata(child);
623 if (res) {
624 SAS_DPRINTK("sas_discover_sata() for device %16llx at "
625 "%016llx:0x%x returned 0x%x\n",
626 SAS_ADDR(child->sas_addr),
627 SAS_ADDR(parent->sas_addr), phy_id, res);
628 kfree(child);
629 return NULL;
630 }
631 } else if (phy->attached_tproto & SAS_PROTO_SSP) {
632 child->dev_type = SAS_END_DEV;
633 rphy = sas_end_device_alloc(phy->port);
634 /* FIXME: error handling */
635 BUG_ON(!rphy);
636 child->tproto = phy->attached_tproto;
637 sas_init_dev(child);
638
639 child->rphy = rphy;
640 sas_fill_in_rphy(child, rphy);
641
642 spin_lock(&parent->port->dev_list_lock);
643 list_add_tail(&child->dev_list_node, &parent->port->dev_list);
644 spin_unlock(&parent->port->dev_list_lock);
645
646 res = sas_discover_end_dev(child);
647 if (res) {
648 SAS_DPRINTK("sas_discover_end_dev() for device %16llx "
649 "at %016llx:0x%x returned 0x%x\n",
650 SAS_ADDR(child->sas_addr),
651 SAS_ADDR(parent->sas_addr), phy_id, res);
652 /* FIXME: this kfrees list elements without removing them */
653 //kfree(child);
654 return NULL;
655 }
656 } else {
657 SAS_DPRINTK("target proto 0x%x at %016llx:0x%x not handled\n",
658 phy->attached_tproto, SAS_ADDR(parent->sas_addr),
659 phy_id);
660 }
661
662 list_add_tail(&child->siblings, &parent_ex->children);
663 return child;
664}
665
666static struct domain_device *sas_ex_discover_expander(
667 struct domain_device *parent, int phy_id)
668{
669 struct sas_expander_device *parent_ex = rphy_to_expander_device(parent->rphy);
670 struct ex_phy *phy = &parent->ex_dev.ex_phy[phy_id];
671 struct domain_device *child = NULL;
672 struct sas_rphy *rphy;
673 struct sas_expander_device *edev;
674 struct asd_sas_port *port;
675 int res;
676
677 if (phy->routing_attr == DIRECT_ROUTING) {
678 SAS_DPRINTK("ex %016llx:0x%x:D <--> ex %016llx:0x%x is not "
679 "allowed\n",
680 SAS_ADDR(parent->sas_addr), phy_id,
681 SAS_ADDR(phy->attached_sas_addr),
682 phy->attached_phy_id);
683 return NULL;
684 }
685 child = kzalloc(sizeof(*child), GFP_KERNEL);
686 if (!child)
687 return NULL;
688
689 phy->port = sas_port_alloc(&parent->rphy->dev, phy_id);
690 /* FIXME: better error handling */
691 BUG_ON(sas_port_add(phy->port) != 0);
692
693
694 switch (phy->attached_dev_type) {
695 case EDGE_DEV:
696 rphy = sas_expander_alloc(phy->port,
697 SAS_EDGE_EXPANDER_DEVICE);
698 break;
699 case FANOUT_DEV:
700 rphy = sas_expander_alloc(phy->port,
701 SAS_FANOUT_EXPANDER_DEVICE);
702 break;
703 default:
704 rphy = NULL; /* shut gcc up */
705 BUG();
706 }
707 port = parent->port;
708 child->rphy = rphy;
709 edev = rphy_to_expander_device(rphy);
710 child->dev_type = phy->attached_dev_type;
711 child->parent = parent;
712 child->port = port;
713 child->iproto = phy->attached_iproto;
714 child->tproto = phy->attached_tproto;
715 memcpy(child->sas_addr, phy->attached_sas_addr, SAS_ADDR_SIZE);
716 sas_hash_addr(child->hashed_sas_addr, child->sas_addr);
717 sas_ex_get_linkrate(parent, child, phy);
718 edev->level = parent_ex->level + 1;
719 parent->port->disc.max_level = max(parent->port->disc.max_level,
720 edev->level);
721 sas_init_dev(child);
722 sas_fill_in_rphy(child, rphy);
723 sas_rphy_add(rphy);
724
725 spin_lock(&parent->port->dev_list_lock);
726 list_add_tail(&child->dev_list_node, &parent->port->dev_list);
727 spin_unlock(&parent->port->dev_list_lock);
728
729 res = sas_discover_expander(child);
730 if (res) {
731 kfree(child);
732 return NULL;
733 }
734 list_add_tail(&child->siblings, &parent->ex_dev.children);
735 return child;
736}
737
738static int sas_ex_discover_dev(struct domain_device *dev, int phy_id)
739{
740 struct expander_device *ex = &dev->ex_dev;
741 struct ex_phy *ex_phy = &ex->ex_phy[phy_id];
742 struct domain_device *child = NULL;
743 int res = 0;
744
745 /* Phy state */
746 if (ex_phy->linkrate == PHY_SPINUP_HOLD) {
747 if (!sas_smp_phy_control(dev, phy_id, PHY_FUNC_LINK_RESET))
748 res = sas_ex_phy_discover(dev, phy_id);
749 if (res)
750 return res;
751 }
752
753 /* Parent and domain coherency */
754 if (!dev->parent && (SAS_ADDR(ex_phy->attached_sas_addr) ==
755 SAS_ADDR(dev->port->sas_addr))) {
756 sas_add_parent_port(dev, phy_id);
757 return 0;
758 }
759 if (dev->parent && (SAS_ADDR(ex_phy->attached_sas_addr) ==
760 SAS_ADDR(dev->parent->sas_addr))) {
761 sas_add_parent_port(dev, phy_id);
762 if (ex_phy->routing_attr == TABLE_ROUTING)
763 sas_configure_phy(dev, phy_id, dev->port->sas_addr, 1);
764 return 0;
765 }
766
767 if (sas_dev_present_in_domain(dev->port, ex_phy->attached_sas_addr))
768 sas_ex_disable_port(dev, ex_phy->attached_sas_addr);
769
770 if (ex_phy->attached_dev_type == NO_DEVICE) {
771 if (ex_phy->routing_attr == DIRECT_ROUTING) {
772 memset(ex_phy->attached_sas_addr, 0, SAS_ADDR_SIZE);
773 sas_configure_routing(dev, ex_phy->attached_sas_addr);
774 }
775 return 0;
776 } else if (ex_phy->linkrate == PHY_LINKRATE_UNKNOWN)
777 return 0;
778
779 if (ex_phy->attached_dev_type != SAS_END_DEV &&
780 ex_phy->attached_dev_type != FANOUT_DEV &&
781 ex_phy->attached_dev_type != EDGE_DEV) {
782 SAS_DPRINTK("unknown device type(0x%x) attached to ex %016llx "
783 "phy 0x%x\n", ex_phy->attached_dev_type,
784 SAS_ADDR(dev->sas_addr),
785 phy_id);
786 return 0;
787 }
788
789 res = sas_configure_routing(dev, ex_phy->attached_sas_addr);
790 if (res) {
791 SAS_DPRINTK("configure routing for dev %016llx "
792 "reported 0x%x. Forgotten\n",
793 SAS_ADDR(ex_phy->attached_sas_addr), res);
794 sas_disable_routing(dev, ex_phy->attached_sas_addr);
795 return res;
796 }
797
798 switch (ex_phy->attached_dev_type) {
799 case SAS_END_DEV:
800 child = sas_ex_discover_end_dev(dev, phy_id);
801 break;
802 case FANOUT_DEV:
803 if (SAS_ADDR(dev->port->disc.fanout_sas_addr)) {
804 SAS_DPRINTK("second fanout expander %016llx phy 0x%x "
805 "attached to ex %016llx phy 0x%x\n",
806 SAS_ADDR(ex_phy->attached_sas_addr),
807 ex_phy->attached_phy_id,
808 SAS_ADDR(dev->sas_addr),
809 phy_id);
810 sas_ex_disable_phy(dev, phy_id);
811 break;
812 } else
813 memcpy(dev->port->disc.fanout_sas_addr,
814 ex_phy->attached_sas_addr, SAS_ADDR_SIZE);
815 /* fallthrough */
816 case EDGE_DEV:
817 child = sas_ex_discover_expander(dev, phy_id);
818 break;
819 default:
820 break;
821 }
822
823 if (child) {
824 int i;
825
826 for (i = 0; i < ex->num_phys; i++) {
827 if (ex->ex_phy[i].phy_state == PHY_VACANT ||
828 ex->ex_phy[i].phy_state == PHY_NOT_PRESENT)
829 continue;
830
831 if (SAS_ADDR(ex->ex_phy[i].attached_sas_addr) ==
832 SAS_ADDR(child->sas_addr))
833 ex->ex_phy[i].phy_state= PHY_DEVICE_DISCOVERED;
834 }
835 }
836
837 return res;
838}
839
840static int sas_find_sub_addr(struct domain_device *dev, u8 *sub_addr)
841{
842 struct expander_device *ex = &dev->ex_dev;
843 int i;
844
845 for (i = 0; i < ex->num_phys; i++) {
846 struct ex_phy *phy = &ex->ex_phy[i];
847
848 if (phy->phy_state == PHY_VACANT ||
849 phy->phy_state == PHY_NOT_PRESENT)
850 continue;
851
852 if ((phy->attached_dev_type == EDGE_DEV ||
853 phy->attached_dev_type == FANOUT_DEV) &&
854 phy->routing_attr == SUBTRACTIVE_ROUTING) {
855
856 memcpy(sub_addr, phy->attached_sas_addr,SAS_ADDR_SIZE);
857
858 return 1;
859 }
860 }
861 return 0;
862}
863
864static int sas_check_level_subtractive_boundary(struct domain_device *dev)
865{
866 struct expander_device *ex = &dev->ex_dev;
867 struct domain_device *child;
868 u8 sub_addr[8] = {0, };
869
870 list_for_each_entry(child, &ex->children, siblings) {
871 if (child->dev_type != EDGE_DEV &&
872 child->dev_type != FANOUT_DEV)
873 continue;
874 if (sub_addr[0] == 0) {
875 sas_find_sub_addr(child, sub_addr);
876 continue;
877 } else {
878 u8 s2[8];
879
880 if (sas_find_sub_addr(child, s2) &&
881 (SAS_ADDR(sub_addr) != SAS_ADDR(s2))) {
882
883 SAS_DPRINTK("ex %016llx->%016llx-?->%016llx "
884 "diverges from subtractive "
885 "boundary %016llx\n",
886 SAS_ADDR(dev->sas_addr),
887 SAS_ADDR(child->sas_addr),
888 SAS_ADDR(s2),
889 SAS_ADDR(sub_addr));
890
891 sas_ex_disable_port(child, s2);
892 }
893 }
894 }
895 return 0;
896}
897/**
898 * sas_ex_discover_devices -- discover devices attached to this expander
899 * dev: pointer to the expander domain device
900 * single: if you want to do a single phy, else set to -1;
901 *
902 * Configure this expander for use with its devices and register the
903 * devices of this expander.
904 */
905static int sas_ex_discover_devices(struct domain_device *dev, int single)
906{
907 struct expander_device *ex = &dev->ex_dev;
908 int i = 0, end = ex->num_phys;
909 int res = 0;
910
911 if (0 <= single && single < end) {
912 i = single;
913 end = i+1;
914 }
915
916 for ( ; i < end; i++) {
917 struct ex_phy *ex_phy = &ex->ex_phy[i];
918
919 if (ex_phy->phy_state == PHY_VACANT ||
920 ex_phy->phy_state == PHY_NOT_PRESENT ||
921 ex_phy->phy_state == PHY_DEVICE_DISCOVERED)
922 continue;
923
924 switch (ex_phy->linkrate) {
925 case PHY_DISABLED:
926 case PHY_RESET_PROBLEM:
927 case PHY_PORT_SELECTOR:
928 continue;
929 default:
930 res = sas_ex_discover_dev(dev, i);
931 if (res)
932 break;
933 continue;
934 }
935 }
936
937 if (!res)
938 sas_check_level_subtractive_boundary(dev);
939
940 return res;
941}
942
943static int sas_check_ex_subtractive_boundary(struct domain_device *dev)
944{
945 struct expander_device *ex = &dev->ex_dev;
946 int i;
947 u8 *sub_sas_addr = NULL;
948
949 if (dev->dev_type != EDGE_DEV)
950 return 0;
951
952 for (i = 0; i < ex->num_phys; i++) {
953 struct ex_phy *phy = &ex->ex_phy[i];
954
955 if (phy->phy_state == PHY_VACANT ||
956 phy->phy_state == PHY_NOT_PRESENT)
957 continue;
958
959 if ((phy->attached_dev_type == FANOUT_DEV ||
960 phy->attached_dev_type == EDGE_DEV) &&
961 phy->routing_attr == SUBTRACTIVE_ROUTING) {
962
963 if (!sub_sas_addr)
964 sub_sas_addr = &phy->attached_sas_addr[0];
965 else if (SAS_ADDR(sub_sas_addr) !=
966 SAS_ADDR(phy->attached_sas_addr)) {
967
968 SAS_DPRINTK("ex %016llx phy 0x%x "
969 "diverges(%016llx) on subtractive "
970 "boundary(%016llx). Disabled\n",
971 SAS_ADDR(dev->sas_addr), i,
972 SAS_ADDR(phy->attached_sas_addr),
973 SAS_ADDR(sub_sas_addr));
974 sas_ex_disable_phy(dev, i);
975 }
976 }
977 }
978 return 0;
979}
980
981static void sas_print_parent_topology_bug(struct domain_device *child,
982 struct ex_phy *parent_phy,
983 struct ex_phy *child_phy)
984{
985 static const char ra_char[] = {
986 [DIRECT_ROUTING] = 'D',
987 [SUBTRACTIVE_ROUTING] = 'S',
988 [TABLE_ROUTING] = 'T',
989 };
990 static const char *ex_type[] = {
991 [EDGE_DEV] = "edge",
992 [FANOUT_DEV] = "fanout",
993 };
994 struct domain_device *parent = child->parent;
995
996 sas_printk("%s ex %016llx phy 0x%x <--> %s ex %016llx phy 0x%x "
997 "has %c:%c routing link!\n",
998
999 ex_type[parent->dev_type],
1000 SAS_ADDR(parent->sas_addr),
1001 parent_phy->phy_id,
1002
1003 ex_type[child->dev_type],
1004 SAS_ADDR(child->sas_addr),
1005 child_phy->phy_id,
1006
1007 ra_char[parent_phy->routing_attr],
1008 ra_char[child_phy->routing_attr]);
1009}
1010
1011static int sas_check_eeds(struct domain_device *child,
1012 struct ex_phy *parent_phy,
1013 struct ex_phy *child_phy)
1014{
1015 int res = 0;
1016 struct domain_device *parent = child->parent;
1017
1018 if (SAS_ADDR(parent->port->disc.fanout_sas_addr) != 0) {
1019 res = -ENODEV;
1020 SAS_DPRINTK("edge ex %016llx phy S:0x%x <--> edge ex %016llx "
1021 "phy S:0x%x, while there is a fanout ex %016llx\n",
1022 SAS_ADDR(parent->sas_addr),
1023 parent_phy->phy_id,
1024 SAS_ADDR(child->sas_addr),
1025 child_phy->phy_id,
1026 SAS_ADDR(parent->port->disc.fanout_sas_addr));
1027 } else if (SAS_ADDR(parent->port->disc.eeds_a) == 0) {
1028 memcpy(parent->port->disc.eeds_a, parent->sas_addr,
1029 SAS_ADDR_SIZE);
1030 memcpy(parent->port->disc.eeds_b, child->sas_addr,
1031 SAS_ADDR_SIZE);
1032 } else if (((SAS_ADDR(parent->port->disc.eeds_a) ==
1033 SAS_ADDR(parent->sas_addr)) ||
1034 (SAS_ADDR(parent->port->disc.eeds_a) ==
1035 SAS_ADDR(child->sas_addr)))
1036 &&
1037 ((SAS_ADDR(parent->port->disc.eeds_b) ==
1038 SAS_ADDR(parent->sas_addr)) ||
1039 (SAS_ADDR(parent->port->disc.eeds_b) ==
1040 SAS_ADDR(child->sas_addr))))
1041 ;
1042 else {
1043 res = -ENODEV;
1044 SAS_DPRINTK("edge ex %016llx phy 0x%x <--> edge ex %016llx "
1045 "phy 0x%x link forms a third EEDS!\n",
1046 SAS_ADDR(parent->sas_addr),
1047 parent_phy->phy_id,
1048 SAS_ADDR(child->sas_addr),
1049 child_phy->phy_id);
1050 }
1051
1052 return res;
1053}
1054
1055/* Here we spill over 80 columns. It is intentional.
1056 */
1057static int sas_check_parent_topology(struct domain_device *child)
1058{
1059 struct expander_device *child_ex = &child->ex_dev;
1060 struct expander_device *parent_ex;
1061 int i;
1062 int res = 0;
1063
1064 if (!child->parent)
1065 return 0;
1066
1067 if (child->parent->dev_type != EDGE_DEV &&
1068 child->parent->dev_type != FANOUT_DEV)
1069 return 0;
1070
1071 parent_ex = &child->parent->ex_dev;
1072
1073 for (i = 0; i < parent_ex->num_phys; i++) {
1074 struct ex_phy *parent_phy = &parent_ex->ex_phy[i];
1075 struct ex_phy *child_phy;
1076
1077 if (parent_phy->phy_state == PHY_VACANT ||
1078 parent_phy->phy_state == PHY_NOT_PRESENT)
1079 continue;
1080
1081 if (SAS_ADDR(parent_phy->attached_sas_addr) != SAS_ADDR(child->sas_addr))
1082 continue;
1083
1084 child_phy = &child_ex->ex_phy[parent_phy->attached_phy_id];
1085
1086 switch (child->parent->dev_type) {
1087 case EDGE_DEV:
1088 if (child->dev_type == FANOUT_DEV) {
1089 if (parent_phy->routing_attr != SUBTRACTIVE_ROUTING ||
1090 child_phy->routing_attr != TABLE_ROUTING) {
1091 sas_print_parent_topology_bug(child, parent_phy, child_phy);
1092 res = -ENODEV;
1093 }
1094 } else if (parent_phy->routing_attr == SUBTRACTIVE_ROUTING) {
1095 if (child_phy->routing_attr == SUBTRACTIVE_ROUTING) {
1096 res = sas_check_eeds(child, parent_phy, child_phy);
1097 } else if (child_phy->routing_attr != TABLE_ROUTING) {
1098 sas_print_parent_topology_bug(child, parent_phy, child_phy);
1099 res = -ENODEV;
1100 }
1101 } else if (parent_phy->routing_attr == TABLE_ROUTING &&
1102 child_phy->routing_attr != SUBTRACTIVE_ROUTING) {
1103 sas_print_parent_topology_bug(child, parent_phy, child_phy);
1104 res = -ENODEV;
1105 }
1106 break;
1107 case FANOUT_DEV:
1108 if (parent_phy->routing_attr != TABLE_ROUTING ||
1109 child_phy->routing_attr != SUBTRACTIVE_ROUTING) {
1110 sas_print_parent_topology_bug(child, parent_phy, child_phy);
1111 res = -ENODEV;
1112 }
1113 break;
1114 default:
1115 break;
1116 }
1117 }
1118
1119 return res;
1120}
1121
1122#define RRI_REQ_SIZE 16
1123#define RRI_RESP_SIZE 44
1124
1125static int sas_configure_present(struct domain_device *dev, int phy_id,
1126 u8 *sas_addr, int *index, int *present)
1127{
1128 int i, res = 0;
1129 struct expander_device *ex = &dev->ex_dev;
1130 struct ex_phy *phy = &ex->ex_phy[phy_id];
1131 u8 *rri_req;
1132 u8 *rri_resp;
1133
1134 *present = 0;
1135 *index = 0;
1136
1137 rri_req = alloc_smp_req(RRI_REQ_SIZE);
1138 if (!rri_req)
1139 return -ENOMEM;
1140
1141 rri_resp = alloc_smp_resp(RRI_RESP_SIZE);
1142 if (!rri_resp) {
1143 kfree(rri_req);
1144 return -ENOMEM;
1145 }
1146
1147 rri_req[1] = SMP_REPORT_ROUTE_INFO;
1148 rri_req[9] = phy_id;
1149
1150 for (i = 0; i < ex->max_route_indexes ; i++) {
1151 *(__be16 *)(rri_req+6) = cpu_to_be16(i);
1152 res = smp_execute_task(dev, rri_req, RRI_REQ_SIZE, rri_resp,
1153 RRI_RESP_SIZE);
1154 if (res)
1155 goto out;
1156 res = rri_resp[2];
1157 if (res == SMP_RESP_NO_INDEX) {
1158 SAS_DPRINTK("overflow of indexes: dev %016llx "
1159 "phy 0x%x index 0x%x\n",
1160 SAS_ADDR(dev->sas_addr), phy_id, i);
1161 goto out;
1162 } else if (res != SMP_RESP_FUNC_ACC) {
1163 SAS_DPRINTK("%s: dev %016llx phy 0x%x index 0x%x "
1164 "result 0x%x\n", __FUNCTION__,
1165 SAS_ADDR(dev->sas_addr), phy_id, i, res);
1166 goto out;
1167 }
1168 if (SAS_ADDR(sas_addr) != 0) {
1169 if (SAS_ADDR(rri_resp+16) == SAS_ADDR(sas_addr)) {
1170 *index = i;
1171 if ((rri_resp[12] & 0x80) == 0x80)
1172 *present = 0;
1173 else
1174 *present = 1;
1175 goto out;
1176 } else if (SAS_ADDR(rri_resp+16) == 0) {
1177 *index = i;
1178 *present = 0;
1179 goto out;
1180 }
1181 } else if (SAS_ADDR(rri_resp+16) == 0 &&
1182 phy->last_da_index < i) {
1183 phy->last_da_index = i;
1184 *index = i;
1185 *present = 0;
1186 goto out;
1187 }
1188 }
1189 res = -1;
1190out:
1191 kfree(rri_req);
1192 kfree(rri_resp);
1193 return res;
1194}
1195
1196#define CRI_REQ_SIZE 44
1197#define CRI_RESP_SIZE 8
1198
1199static int sas_configure_set(struct domain_device *dev, int phy_id,
1200 u8 *sas_addr, int index, int include)
1201{
1202 int res;
1203 u8 *cri_req;
1204 u8 *cri_resp;
1205
1206 cri_req = alloc_smp_req(CRI_REQ_SIZE);
1207 if (!cri_req)
1208 return -ENOMEM;
1209
1210 cri_resp = alloc_smp_resp(CRI_RESP_SIZE);
1211 if (!cri_resp) {
1212 kfree(cri_req);
1213 return -ENOMEM;
1214 }
1215
1216 cri_req[1] = SMP_CONF_ROUTE_INFO;
1217 *(__be16 *)(cri_req+6) = cpu_to_be16(index);
1218 cri_req[9] = phy_id;
1219 if (SAS_ADDR(sas_addr) == 0 || !include)
1220 cri_req[12] |= 0x80;
1221 memcpy(cri_req+16, sas_addr, SAS_ADDR_SIZE);
1222
1223 res = smp_execute_task(dev, cri_req, CRI_REQ_SIZE, cri_resp,
1224 CRI_RESP_SIZE);
1225 if (res)
1226 goto out;
1227 res = cri_resp[2];
1228 if (res == SMP_RESP_NO_INDEX) {
1229 SAS_DPRINTK("overflow of indexes: dev %016llx phy 0x%x "
1230 "index 0x%x\n",
1231 SAS_ADDR(dev->sas_addr), phy_id, index);
1232 }
1233out:
1234 kfree(cri_req);
1235 kfree(cri_resp);
1236 return res;
1237}
1238
1239static int sas_configure_phy(struct domain_device *dev, int phy_id,
1240 u8 *sas_addr, int include)
1241{
1242 int index;
1243 int present;
1244 int res;
1245
1246 res = sas_configure_present(dev, phy_id, sas_addr, &index, &present);
1247 if (res)
1248 return res;
1249 if (include ^ present)
1250 return sas_configure_set(dev, phy_id, sas_addr, index,include);
1251
1252 return res;
1253}
1254
1255/**
1256 * sas_configure_parent -- configure routing table of parent
1257 * parent: parent expander
1258 * child: child expander
1259 * sas_addr: SAS port identifier of device directly attached to child
1260 */
1261static int sas_configure_parent(struct domain_device *parent,
1262 struct domain_device *child,
1263 u8 *sas_addr, int include)
1264{
1265 struct expander_device *ex_parent = &parent->ex_dev;
1266 int res = 0;
1267 int i;
1268
1269 if (parent->parent) {
1270 res = sas_configure_parent(parent->parent, parent, sas_addr,
1271 include);
1272 if (res)
1273 return res;
1274 }
1275
1276 if (ex_parent->conf_route_table == 0) {
1277 SAS_DPRINTK("ex %016llx has self-configuring routing table\n",
1278 SAS_ADDR(parent->sas_addr));
1279 return 0;
1280 }
1281
1282 for (i = 0; i < ex_parent->num_phys; i++) {
1283 struct ex_phy *phy = &ex_parent->ex_phy[i];
1284
1285 if ((phy->routing_attr == TABLE_ROUTING) &&
1286 (SAS_ADDR(phy->attached_sas_addr) ==
1287 SAS_ADDR(child->sas_addr))) {
1288 res = sas_configure_phy(parent, i, sas_addr, include);
1289 if (res)
1290 return res;
1291 }
1292 }
1293
1294 return res;
1295}
1296
1297/**
1298 * sas_configure_routing -- configure routing
1299 * dev: expander device
1300 * sas_addr: port identifier of device directly attached to the expander device
1301 */
1302static int sas_configure_routing(struct domain_device *dev, u8 *sas_addr)
1303{
1304 if (dev->parent)
1305 return sas_configure_parent(dev->parent, dev, sas_addr, 1);
1306 return 0;
1307}
1308
1309static int sas_disable_routing(struct domain_device *dev, u8 *sas_addr)
1310{
1311 if (dev->parent)
1312 return sas_configure_parent(dev->parent, dev, sas_addr, 0);
1313 return 0;
1314}
1315
1316#if 0
1317#define SMP_BIN_ATTR_NAME "smp_portal"
1318
1319static void sas_ex_smp_hook(struct domain_device *dev)
1320{
1321 struct expander_device *ex_dev = &dev->ex_dev;
1322 struct bin_attribute *bin_attr = &ex_dev->smp_bin_attr;
1323
1324 memset(bin_attr, 0, sizeof(*bin_attr));
1325
1326 bin_attr->attr.name = SMP_BIN_ATTR_NAME;
1327 bin_attr->attr.owner = THIS_MODULE;
1328 bin_attr->attr.mode = 0600;
1329
1330 bin_attr->size = 0;
1331 bin_attr->private = NULL;
1332 bin_attr->read = smp_portal_read;
1333 bin_attr->write= smp_portal_write;
1334 bin_attr->mmap = NULL;
1335
1336 ex_dev->smp_portal_pid = -1;
1337 init_MUTEX(&ex_dev->smp_sema);
1338}
1339#endif
1340
1341/**
1342 * sas_discover_expander -- expander discovery
1343 * @ex: pointer to expander domain device
1344 *
1345 * See comment in sas_discover_sata().
1346 */
1347static int sas_discover_expander(struct domain_device *dev)
1348{
1349 int res;
1350
1351 res = sas_notify_lldd_dev_found(dev);
1352 if (res)
1353 return res;
1354
1355 res = sas_ex_general(dev);
1356 if (res)
1357 goto out_err;
1358 res = sas_ex_manuf_info(dev);
1359 if (res)
1360 goto out_err;
1361
1362 res = sas_expander_discover(dev);
1363 if (res) {
1364 SAS_DPRINTK("expander %016llx discovery failed(0x%x)\n",
1365 SAS_ADDR(dev->sas_addr), res);
1366 goto out_err;
1367 }
1368
1369 sas_check_ex_subtractive_boundary(dev);
1370 res = sas_check_parent_topology(dev);
1371 if (res)
1372 goto out_err;
1373 return 0;
1374out_err:
1375 sas_notify_lldd_dev_gone(dev);
1376 return res;
1377}
1378
1379static int sas_ex_level_discovery(struct asd_sas_port *port, const int level)
1380{
1381 int res = 0;
1382 struct domain_device *dev;
1383
1384 list_for_each_entry(dev, &port->dev_list, dev_list_node) {
1385 if (dev->dev_type == EDGE_DEV ||
1386 dev->dev_type == FANOUT_DEV) {
1387 struct sas_expander_device *ex =
1388 rphy_to_expander_device(dev->rphy);
1389
1390 if (level == ex->level)
1391 res = sas_ex_discover_devices(dev, -1);
1392 else if (level > 0)
1393 res = sas_ex_discover_devices(port->port_dev, -1);
1394
1395 }
1396 }
1397
1398 return res;
1399}
1400
1401static int sas_ex_bfs_disc(struct asd_sas_port *port)
1402{
1403 int res;
1404 int level;
1405
1406 do {
1407 level = port->disc.max_level;
1408 res = sas_ex_level_discovery(port, level);
1409 mb();
1410 } while (level < port->disc.max_level);
1411
1412 return res;
1413}
1414
1415int sas_discover_root_expander(struct domain_device *dev)
1416{
1417 int res;
1418 struct sas_expander_device *ex = rphy_to_expander_device(dev->rphy);
1419
1420 sas_rphy_add(dev->rphy);
1421
1422 ex->level = dev->port->disc.max_level; /* 0 */
1423 res = sas_discover_expander(dev);
1424 if (!res)
1425 sas_ex_bfs_disc(dev->port);
1426
1427 return res;
1428}
1429
1430/* ---------- Domain revalidation ---------- */
1431
1432static int sas_get_phy_discover(struct domain_device *dev,
1433 int phy_id, struct smp_resp *disc_resp)
1434{
1435 int res;
1436 u8 *disc_req;
1437
1438 disc_req = alloc_smp_req(DISCOVER_REQ_SIZE);
1439 if (!disc_req)
1440 return -ENOMEM;
1441
1442 disc_req[1] = SMP_DISCOVER;
1443 disc_req[9] = phy_id;
1444
1445 res = smp_execute_task(dev, disc_req, DISCOVER_REQ_SIZE,
1446 disc_resp, DISCOVER_RESP_SIZE);
1447 if (res)
1448 goto out;
1449 else if (disc_resp->result != SMP_RESP_FUNC_ACC) {
1450 res = disc_resp->result;
1451 goto out;
1452 }
1453out:
1454 kfree(disc_req);
1455 return res;
1456}
1457
1458static int sas_get_phy_change_count(struct domain_device *dev,
1459 int phy_id, int *pcc)
1460{
1461 int res;
1462 struct smp_resp *disc_resp;
1463
1464 disc_resp = alloc_smp_resp(DISCOVER_RESP_SIZE);
1465 if (!disc_resp)
1466 return -ENOMEM;
1467
1468 res = sas_get_phy_discover(dev, phy_id, disc_resp);
1469 if (!res)
1470 *pcc = disc_resp->disc.change_count;
1471
1472 kfree(disc_resp);
1473 return res;
1474}
1475
1476static int sas_get_phy_attached_sas_addr(struct domain_device *dev,
1477 int phy_id, u8 *attached_sas_addr)
1478{
1479 int res;
1480 struct smp_resp *disc_resp;
1481 struct discover_resp *dr;
1482
1483 disc_resp = alloc_smp_resp(DISCOVER_RESP_SIZE);
1484 if (!disc_resp)
1485 return -ENOMEM;
1486 dr = &disc_resp->disc;
1487
1488 res = sas_get_phy_discover(dev, phy_id, disc_resp);
1489 if (!res) {
1490 memcpy(attached_sas_addr,disc_resp->disc.attached_sas_addr,8);
1491 if (dr->attached_dev_type == 0)
1492 memset(attached_sas_addr, 0, 8);
1493 }
1494 kfree(disc_resp);
1495 return res;
1496}
1497
1498static int sas_find_bcast_phy(struct domain_device *dev, int *phy_id,
1499 int from_phy)
1500{
1501 struct expander_device *ex = &dev->ex_dev;
1502 int res = 0;
1503 int i;
1504
1505 for (i = from_phy; i < ex->num_phys; i++) {
1506 int phy_change_count = 0;
1507
1508 res = sas_get_phy_change_count(dev, i, &phy_change_count);
1509 if (res)
1510 goto out;
1511 else if (phy_change_count != ex->ex_phy[i].phy_change_count) {
1512 ex->ex_phy[i].phy_change_count = phy_change_count;
1513 *phy_id = i;
1514 return 0;
1515 }
1516 }
1517out:
1518 return res;
1519}
1520
1521static int sas_get_ex_change_count(struct domain_device *dev, int *ecc)
1522{
1523 int res;
1524 u8 *rg_req;
1525 struct smp_resp *rg_resp;
1526
1527 rg_req = alloc_smp_req(RG_REQ_SIZE);
1528 if (!rg_req)
1529 return -ENOMEM;
1530
1531 rg_resp = alloc_smp_resp(RG_RESP_SIZE);
1532 if (!rg_resp) {
1533 kfree(rg_req);
1534 return -ENOMEM;
1535 }
1536
1537 rg_req[1] = SMP_REPORT_GENERAL;
1538
1539 res = smp_execute_task(dev, rg_req, RG_REQ_SIZE, rg_resp,
1540 RG_RESP_SIZE);
1541 if (res)
1542 goto out;
1543 if (rg_resp->result != SMP_RESP_FUNC_ACC) {
1544 res = rg_resp->result;
1545 goto out;
1546 }
1547
1548 *ecc = be16_to_cpu(rg_resp->rg.change_count);
1549out:
1550 kfree(rg_resp);
1551 kfree(rg_req);
1552 return res;
1553}
1554
1555static int sas_find_bcast_dev(struct domain_device *dev,
1556 struct domain_device **src_dev)
1557{
1558 struct expander_device *ex = &dev->ex_dev;
1559 int ex_change_count = -1;
1560 int res;
1561
1562 res = sas_get_ex_change_count(dev, &ex_change_count);
1563 if (res)
1564 goto out;
1565 if (ex_change_count != -1 &&
1566 ex_change_count != ex->ex_change_count) {
1567 *src_dev = dev;
1568 ex->ex_change_count = ex_change_count;
1569 } else {
1570 struct domain_device *ch;
1571
1572 list_for_each_entry(ch, &ex->children, siblings) {
1573 if (ch->dev_type == EDGE_DEV ||
1574 ch->dev_type == FANOUT_DEV) {
1575 res = sas_find_bcast_dev(ch, src_dev);
1576 if (src_dev)
1577 return res;
1578 }
1579 }
1580 }
1581out:
1582 return res;
1583}
1584
1585static void sas_unregister_ex_tree(struct domain_device *dev)
1586{
1587 struct expander_device *ex = &dev->ex_dev;
1588 struct domain_device *child, *n;
1589
1590 list_for_each_entry_safe(child, n, &ex->children, siblings) {
1591 if (child->dev_type == EDGE_DEV ||
1592 child->dev_type == FANOUT_DEV)
1593 sas_unregister_ex_tree(child);
1594 else
1595 sas_unregister_dev(child);
1596 }
1597 sas_unregister_dev(dev);
1598}
1599
1600static void sas_unregister_devs_sas_addr(struct domain_device *parent,
1601 int phy_id)
1602{
1603 struct expander_device *ex_dev = &parent->ex_dev;
1604 struct ex_phy *phy = &ex_dev->ex_phy[phy_id];
1605 struct domain_device *child, *n;
1606
1607 list_for_each_entry_safe(child, n, &ex_dev->children, siblings) {
1608 if (SAS_ADDR(child->sas_addr) ==
1609 SAS_ADDR(phy->attached_sas_addr)) {
1610 if (child->dev_type == EDGE_DEV ||
1611 child->dev_type == FANOUT_DEV)
1612 sas_unregister_ex_tree(child);
1613 else
1614 sas_unregister_dev(child);
1615 break;
1616 }
1617 }
1618 sas_disable_routing(parent, phy->attached_sas_addr);
1619 memset(phy->attached_sas_addr, 0, SAS_ADDR_SIZE);
1620 sas_port_delete_phy(phy->port, phy->phy);
1621 if (phy->port->num_phys == 0)
1622 sas_port_delete(phy->port);
1623 phy->port = NULL;
1624}
1625
1626static int sas_discover_bfs_by_root_level(struct domain_device *root,
1627 const int level)
1628{
1629 struct expander_device *ex_root = &root->ex_dev;
1630 struct domain_device *child;
1631 int res = 0;
1632
1633 list_for_each_entry(child, &ex_root->children, siblings) {
1634 if (child->dev_type == EDGE_DEV ||
1635 child->dev_type == FANOUT_DEV) {
1636 struct sas_expander_device *ex =
1637 rphy_to_expander_device(child->rphy);
1638
1639 if (level > ex->level)
1640 res = sas_discover_bfs_by_root_level(child,
1641 level);
1642 else if (level == ex->level)
1643 res = sas_ex_discover_devices(child, -1);
1644 }
1645 }
1646 return res;
1647}
1648
1649static int sas_discover_bfs_by_root(struct domain_device *dev)
1650{
1651 int res;
1652 struct sas_expander_device *ex = rphy_to_expander_device(dev->rphy);
1653 int level = ex->level+1;
1654
1655 res = sas_ex_discover_devices(dev, -1);
1656 if (res)
1657 goto out;
1658 do {
1659 res = sas_discover_bfs_by_root_level(dev, level);
1660 mb();
1661 level += 1;
1662 } while (level <= dev->port->disc.max_level);
1663out:
1664 return res;
1665}
1666
1667static int sas_discover_new(struct domain_device *dev, int phy_id)
1668{
1669 struct ex_phy *ex_phy = &dev->ex_dev.ex_phy[phy_id];
1670 struct domain_device *child;
1671 int res;
1672
1673 SAS_DPRINTK("ex %016llx phy%d new device attached\n",
1674 SAS_ADDR(dev->sas_addr), phy_id);
1675 res = sas_ex_phy_discover(dev, phy_id);
1676 if (res)
1677 goto out;
1678 res = sas_ex_discover_devices(dev, phy_id);
1679 if (res)
1680 goto out;
1681 list_for_each_entry(child, &dev->ex_dev.children, siblings) {
1682 if (SAS_ADDR(child->sas_addr) ==
1683 SAS_ADDR(ex_phy->attached_sas_addr)) {
1684 if (child->dev_type == EDGE_DEV ||
1685 child->dev_type == FANOUT_DEV)
1686 res = sas_discover_bfs_by_root(child);
1687 break;
1688 }
1689 }
1690out:
1691 return res;
1692}
1693
1694static int sas_rediscover_dev(struct domain_device *dev, int phy_id)
1695{
1696 struct expander_device *ex = &dev->ex_dev;
1697 struct ex_phy *phy = &ex->ex_phy[phy_id];
1698 u8 attached_sas_addr[8];
1699 int res;
1700
1701 res = sas_get_phy_attached_sas_addr(dev, phy_id, attached_sas_addr);
1702 switch (res) {
1703 case SMP_RESP_NO_PHY:
1704 phy->phy_state = PHY_NOT_PRESENT;
1705 sas_unregister_devs_sas_addr(dev, phy_id);
1706 goto out; break;
1707 case SMP_RESP_PHY_VACANT:
1708 phy->phy_state = PHY_VACANT;
1709 sas_unregister_devs_sas_addr(dev, phy_id);
1710 goto out; break;
1711 case SMP_RESP_FUNC_ACC:
1712 break;
1713 }
1714
1715 if (SAS_ADDR(attached_sas_addr) == 0) {
1716 phy->phy_state = PHY_EMPTY;
1717 sas_unregister_devs_sas_addr(dev, phy_id);
1718 } else if (SAS_ADDR(attached_sas_addr) ==
1719 SAS_ADDR(phy->attached_sas_addr)) {
1720 SAS_DPRINTK("ex %016llx phy 0x%x broadcast flutter\n",
1721 SAS_ADDR(dev->sas_addr), phy_id);
1722 } else
1723 res = sas_discover_new(dev, phy_id);
1724out:
1725 return res;
1726}
1727
1728static int sas_rediscover(struct domain_device *dev, const int phy_id)
1729{
1730 struct expander_device *ex = &dev->ex_dev;
1731 struct ex_phy *changed_phy = &ex->ex_phy[phy_id];
1732 int res = 0;
1733 int i;
1734
1735 SAS_DPRINTK("ex %016llx phy%d originated BROADCAST(CHANGE)\n",
1736 SAS_ADDR(dev->sas_addr), phy_id);
1737
1738 if (SAS_ADDR(changed_phy->attached_sas_addr) != 0) {
1739 for (i = 0; i < ex->num_phys; i++) {
1740 struct ex_phy *phy = &ex->ex_phy[i];
1741
1742 if (i == phy_id)
1743 continue;
1744 if (SAS_ADDR(phy->attached_sas_addr) ==
1745 SAS_ADDR(changed_phy->attached_sas_addr)) {
1746 SAS_DPRINTK("phy%d part of wide port with "
1747 "phy%d\n", phy_id, i);
1748 goto out;
1749 }
1750 }
1751 res = sas_rediscover_dev(dev, phy_id);
1752 } else
1753 res = sas_discover_new(dev, phy_id);
1754out:
1755 return res;
1756}
1757
1758/**
1759 * sas_revalidate_domain -- revalidate the domain
1760 * @port: port to the domain of interest
1761 *
1762 * NOTE: this process _must_ quit (return) as soon as any connection
1763 * errors are encountered. Connection recovery is done elsewhere.
1764 * Discover process only interrogates devices in order to discover the
1765 * domain.
1766 */
1767int sas_ex_revalidate_domain(struct domain_device *port_dev)
1768{
1769 int res;
1770 struct domain_device *dev = NULL;
1771
1772 res = sas_find_bcast_dev(port_dev, &dev);
1773 if (res)
1774 goto out;
1775 if (dev) {
1776 struct expander_device *ex = &dev->ex_dev;
1777 int i = 0, phy_id;
1778
1779 do {
1780 phy_id = -1;
1781 res = sas_find_bcast_phy(dev, &phy_id, i);
1782 if (phy_id == -1)
1783 break;
1784 res = sas_rediscover(dev, phy_id);
1785 i = phy_id + 1;
1786 } while (i < ex->num_phys);
1787 }
1788out:
1789 return res;
1790}
1791
1792#if 0
1793/* ---------- SMP portal ---------- */
1794
1795static ssize_t smp_portal_write(struct kobject *kobj, char *buf, loff_t offs,
1796 size_t size)
1797{
1798 struct domain_device *dev = to_dom_device(kobj);
1799 struct expander_device *ex = &dev->ex_dev;
1800
1801 if (offs != 0)
1802 return -EFBIG;
1803 else if (size == 0)
1804 return 0;
1805
1806 down_interruptible(&ex->smp_sema);
1807 if (ex->smp_req)
1808 kfree(ex->smp_req);
1809 ex->smp_req = kzalloc(size, GFP_USER);
1810 if (!ex->smp_req) {
1811 up(&ex->smp_sema);
1812 return -ENOMEM;
1813 }
1814 memcpy(ex->smp_req, buf, size);
1815 ex->smp_req_size = size;
1816 ex->smp_portal_pid = current->pid;
1817 up(&ex->smp_sema);
1818
1819 return size;
1820}
1821
1822static ssize_t smp_portal_read(struct kobject *kobj, char *buf, loff_t offs,
1823 size_t size)
1824{
1825 struct domain_device *dev = to_dom_device(kobj);
1826 struct expander_device *ex = &dev->ex_dev;
1827 u8 *smp_resp;
1828 int res = -EINVAL;
1829
1830 /* XXX: sysfs gives us an offset of 0x10 or 0x8 while in fact
1831 * it should be 0.
1832 */
1833
1834 down_interruptible(&ex->smp_sema);
1835 if (!ex->smp_req || ex->smp_portal_pid != current->pid)
1836 goto out;
1837
1838 res = 0;
1839 if (size == 0)
1840 goto out;
1841
1842 res = -ENOMEM;
1843 smp_resp = alloc_smp_resp(size);
1844 if (!smp_resp)
1845 goto out;
1846 res = smp_execute_task(dev, ex->smp_req, ex->smp_req_size,
1847 smp_resp, size);
1848 if (!res) {
1849 memcpy(buf, smp_resp, size);
1850 res = size;
1851 }
1852
1853 kfree(smp_resp);
1854out:
1855 kfree(ex->smp_req);
1856 ex->smp_req = NULL;
1857 ex->smp_req_size = 0;
1858 ex->smp_portal_pid = -1;
1859 up(&ex->smp_sema);
1860 return res;
1861}
1862#endif