RT-AC66 3.0.0.4.374.130 core
[tomato.git] / release / src-rt-6.x / linux / linux-2.6 / drivers / scsi / libsas / sas_expander.c
blobe34442e405e8a8aa96e05f0f195b20958a9a2ddb
1 /*
2 * Serial Attached SCSI (SAS) Expander discovery and configuration
4 * Copyright (C) 2005 Adaptec, Inc. All rights reserved.
5 * Copyright (C) 2005 Luben Tuikov <luben_tuikov@adaptec.com>
7 * This file is licensed under GPLv2.
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.
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.
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
25 #include <linux/scatterlist.h>
27 #include "sas_internal.h"
29 #include <scsi/scsi_transport.h>
30 #include <scsi/scsi_transport_sas.h>
31 #include "../scsi_sas_internal.h"
33 static int sas_discover_expander(struct domain_device *dev);
34 static int sas_configure_routing(struct domain_device *dev, u8 *sas_addr);
35 static int sas_configure_phy(struct domain_device *dev, int phy_id,
36 u8 *sas_addr, int include);
37 static int sas_disable_routing(struct domain_device *dev, u8 *sas_addr);
39 #if 0
40 /* FIXME: smp needs to migrate into the sas class */
41 static ssize_t smp_portal_read(struct kobject *, char *, loff_t, size_t);
42 static ssize_t smp_portal_write(struct kobject *, char *, loff_t, size_t);
43 #endif
45 /* ---------- SMP task management ---------- */
47 static void smp_task_timedout(unsigned long _task)
49 struct sas_task *task = (void *) _task;
50 unsigned long flags;
52 spin_lock_irqsave(&task->task_state_lock, flags);
53 if (!(task->task_state_flags & SAS_TASK_STATE_DONE))
54 task->task_state_flags |= SAS_TASK_STATE_ABORTED;
55 spin_unlock_irqrestore(&task->task_state_lock, flags);
57 complete(&task->completion);
60 static void smp_task_done(struct sas_task *task)
62 if (!del_timer(&task->timer))
63 return;
64 complete(&task->completion);
67 /* Give it some long enough timeout. In seconds. */
68 #define SMP_TIMEOUT 10
70 static int smp_execute_task(struct domain_device *dev, void *req, int req_size,
71 void *resp, int resp_size)
73 int res, retry;
74 struct sas_task *task = NULL;
75 struct sas_internal *i =
76 to_sas_internal(dev->port->ha->core.shost->transportt);
78 for (retry = 0; retry < 3; retry++) {
79 task = sas_alloc_task(GFP_KERNEL);
80 if (!task)
81 return -ENOMEM;
83 task->dev = dev;
84 task->task_proto = dev->tproto;
85 sg_init_one(&task->smp_task.smp_req, req, req_size);
86 sg_init_one(&task->smp_task.smp_resp, resp, resp_size);
88 task->task_done = smp_task_done;
90 task->timer.data = (unsigned long) task;
91 task->timer.function = smp_task_timedout;
92 task->timer.expires = jiffies + SMP_TIMEOUT*HZ;
93 add_timer(&task->timer);
95 res = i->dft->lldd_execute_task(task, 1, GFP_KERNEL);
97 if (res) {
98 del_timer(&task->timer);
99 SAS_DPRINTK("executing SMP task failed:%d\n", res);
100 goto ex_err;
103 wait_for_completion(&task->completion);
104 res = -ETASK;
105 if ((task->task_state_flags & SAS_TASK_STATE_ABORTED)) {
106 SAS_DPRINTK("smp task timed out or aborted\n");
107 i->dft->lldd_abort_task(task);
108 if (!(task->task_state_flags & SAS_TASK_STATE_DONE)) {
109 SAS_DPRINTK("SMP task aborted and not done\n");
110 goto ex_err;
113 if (task->task_status.resp == SAS_TASK_COMPLETE &&
114 task->task_status.stat == SAM_GOOD) {
115 res = 0;
116 break;
117 } else {
118 SAS_DPRINTK("%s: task to dev %016llx response: 0x%x "
119 "status 0x%x\n", __FUNCTION__,
120 SAS_ADDR(dev->sas_addr),
121 task->task_status.resp,
122 task->task_status.stat);
123 sas_free_task(task);
124 task = NULL;
127 ex_err:
128 BUG_ON(retry == 3 && task != NULL);
129 if (task != NULL) {
130 sas_free_task(task);
132 return res;
135 /* ---------- Allocations ---------- */
137 static inline void *alloc_smp_req(int size)
139 u8 *p = kzalloc(size, GFP_KERNEL);
140 if (p)
141 p[0] = SMP_REQUEST;
142 return p;
145 static inline void *alloc_smp_resp(int size)
147 return kzalloc(size, GFP_KERNEL);
150 /* ---------- Expander configuration ---------- */
152 static void sas_set_ex_phy(struct domain_device *dev, int phy_id,
153 void *disc_resp)
155 struct expander_device *ex = &dev->ex_dev;
156 struct ex_phy *phy = &ex->ex_phy[phy_id];
157 struct smp_resp *resp = disc_resp;
158 struct discover_resp *dr = &resp->disc;
159 struct sas_rphy *rphy = dev->rphy;
160 int rediscover = (phy->phy != NULL);
162 if (!rediscover) {
163 phy->phy = sas_phy_alloc(&rphy->dev, phy_id);
165 /* FIXME: error_handling */
166 BUG_ON(!phy->phy);
169 switch (resp->result) {
170 case SMP_RESP_PHY_VACANT:
171 phy->phy_state = PHY_VACANT;
172 return;
173 default:
174 phy->phy_state = PHY_NOT_PRESENT;
175 return;
176 case SMP_RESP_FUNC_ACC:
177 phy->phy_state = PHY_EMPTY; /* do not know yet */
178 break;
181 phy->phy_id = phy_id;
182 phy->attached_dev_type = dr->attached_dev_type;
183 phy->linkrate = dr->linkrate;
184 phy->attached_sata_host = dr->attached_sata_host;
185 phy->attached_sata_dev = dr->attached_sata_dev;
186 phy->attached_sata_ps = dr->attached_sata_ps;
187 phy->attached_iproto = dr->iproto << 1;
188 phy->attached_tproto = dr->tproto << 1;
189 memcpy(phy->attached_sas_addr, dr->attached_sas_addr, SAS_ADDR_SIZE);
190 phy->attached_phy_id = dr->attached_phy_id;
191 phy->phy_change_count = dr->change_count;
192 phy->routing_attr = dr->routing_attr;
193 phy->virtual = dr->virtual;
194 phy->last_da_index = -1;
196 phy->phy->identify.initiator_port_protocols = phy->attached_iproto;
197 phy->phy->identify.target_port_protocols = phy->attached_tproto;
198 phy->phy->identify.phy_identifier = phy_id;
199 phy->phy->minimum_linkrate_hw = dr->hmin_linkrate;
200 phy->phy->maximum_linkrate_hw = dr->hmax_linkrate;
201 phy->phy->minimum_linkrate = dr->pmin_linkrate;
202 phy->phy->maximum_linkrate = dr->pmax_linkrate;
203 phy->phy->negotiated_linkrate = phy->linkrate;
205 if (!rediscover)
206 sas_phy_add(phy->phy);
208 SAS_DPRINTK("ex %016llx phy%02d:%c attached: %016llx\n",
209 SAS_ADDR(dev->sas_addr), phy->phy_id,
210 phy->routing_attr == TABLE_ROUTING ? 'T' :
211 phy->routing_attr == DIRECT_ROUTING ? 'D' :
212 phy->routing_attr == SUBTRACTIVE_ROUTING ? 'S' : '?',
213 SAS_ADDR(phy->attached_sas_addr));
215 return;
218 #define DISCOVER_REQ_SIZE 16
219 #define DISCOVER_RESP_SIZE 56
221 static int sas_ex_phy_discover(struct domain_device *dev, int single)
223 struct expander_device *ex = &dev->ex_dev;
224 int res = 0;
225 u8 *disc_req;
226 u8 *disc_resp;
228 disc_req = alloc_smp_req(DISCOVER_REQ_SIZE);
229 if (!disc_req)
230 return -ENOMEM;
232 disc_resp = alloc_smp_req(DISCOVER_RESP_SIZE);
233 if (!disc_resp) {
234 kfree(disc_req);
235 return -ENOMEM;
238 disc_req[1] = SMP_DISCOVER;
240 if (0 <= single && single < ex->num_phys) {
241 disc_req[9] = single;
242 res = smp_execute_task(dev, disc_req, DISCOVER_REQ_SIZE,
243 disc_resp, DISCOVER_RESP_SIZE);
244 if (res)
245 goto out_err;
246 sas_set_ex_phy(dev, single, disc_resp);
247 } else {
248 int i;
250 for (i = 0; i < ex->num_phys; i++) {
251 disc_req[9] = i;
252 res = smp_execute_task(dev, disc_req,
253 DISCOVER_REQ_SIZE, disc_resp,
254 DISCOVER_RESP_SIZE);
255 if (res)
256 goto out_err;
257 sas_set_ex_phy(dev, i, disc_resp);
260 out_err:
261 kfree(disc_resp);
262 kfree(disc_req);
263 return res;
266 static int sas_expander_discover(struct domain_device *dev)
268 struct expander_device *ex = &dev->ex_dev;
269 int res = -ENOMEM;
271 ex->ex_phy = kzalloc(sizeof(*ex->ex_phy)*ex->num_phys, GFP_KERNEL);
272 if (!ex->ex_phy)
273 return -ENOMEM;
275 res = sas_ex_phy_discover(dev, -1);
276 if (res)
277 goto out_err;
279 return 0;
280 out_err:
281 kfree(ex->ex_phy);
282 ex->ex_phy = NULL;
283 return res;
286 #define MAX_EXPANDER_PHYS 128
288 static void ex_assign_report_general(struct domain_device *dev,
289 struct smp_resp *resp)
291 struct report_general_resp *rg = &resp->rg;
293 dev->ex_dev.ex_change_count = be16_to_cpu(rg->change_count);
294 dev->ex_dev.max_route_indexes = be16_to_cpu(rg->route_indexes);
295 dev->ex_dev.num_phys = min(rg->num_phys, (u8)MAX_EXPANDER_PHYS);
296 dev->ex_dev.conf_route_table = rg->conf_route_table;
297 dev->ex_dev.configuring = rg->configuring;
298 memcpy(dev->ex_dev.enclosure_logical_id, rg->enclosure_logical_id, 8);
301 #define RG_REQ_SIZE 8
302 #define RG_RESP_SIZE 32
304 static int sas_ex_general(struct domain_device *dev)
306 u8 *rg_req;
307 struct smp_resp *rg_resp;
308 int res;
309 int i;
311 rg_req = alloc_smp_req(RG_REQ_SIZE);
312 if (!rg_req)
313 return -ENOMEM;
315 rg_resp = alloc_smp_resp(RG_RESP_SIZE);
316 if (!rg_resp) {
317 kfree(rg_req);
318 return -ENOMEM;
321 rg_req[1] = SMP_REPORT_GENERAL;
323 for (i = 0; i < 5; i++) {
324 res = smp_execute_task(dev, rg_req, RG_REQ_SIZE, rg_resp,
325 RG_RESP_SIZE);
327 if (res) {
328 SAS_DPRINTK("RG to ex %016llx failed:0x%x\n",
329 SAS_ADDR(dev->sas_addr), res);
330 goto out;
331 } else if (rg_resp->result != SMP_RESP_FUNC_ACC) {
332 SAS_DPRINTK("RG:ex %016llx returned SMP result:0x%x\n",
333 SAS_ADDR(dev->sas_addr), rg_resp->result);
334 res = rg_resp->result;
335 goto out;
338 ex_assign_report_general(dev, rg_resp);
340 if (dev->ex_dev.configuring) {
341 SAS_DPRINTK("RG: ex %llx self-configuring...\n",
342 SAS_ADDR(dev->sas_addr));
343 schedule_timeout_interruptible(5*HZ);
344 } else
345 break;
347 out:
348 kfree(rg_req);
349 kfree(rg_resp);
350 return res;
353 static void ex_assign_manuf_info(struct domain_device *dev, void
354 *_mi_resp)
356 u8 *mi_resp = _mi_resp;
357 struct sas_rphy *rphy = dev->rphy;
358 struct sas_expander_device *edev = rphy_to_expander_device(rphy);
360 memcpy(edev->vendor_id, mi_resp + 12, SAS_EXPANDER_VENDOR_ID_LEN);
361 memcpy(edev->product_id, mi_resp + 20, SAS_EXPANDER_PRODUCT_ID_LEN);
362 memcpy(edev->product_rev, mi_resp + 36,
363 SAS_EXPANDER_PRODUCT_REV_LEN);
365 if (mi_resp[8] & 1) {
366 memcpy(edev->component_vendor_id, mi_resp + 40,
367 SAS_EXPANDER_COMPONENT_VENDOR_ID_LEN);
368 edev->component_id = mi_resp[48] << 8 | mi_resp[49];
369 edev->component_revision_id = mi_resp[50];
373 #define MI_REQ_SIZE 8
374 #define MI_RESP_SIZE 64
376 static int sas_ex_manuf_info(struct domain_device *dev)
378 u8 *mi_req;
379 u8 *mi_resp;
380 int res;
382 mi_req = alloc_smp_req(MI_REQ_SIZE);
383 if (!mi_req)
384 return -ENOMEM;
386 mi_resp = alloc_smp_resp(MI_RESP_SIZE);
387 if (!mi_resp) {
388 kfree(mi_req);
389 return -ENOMEM;
392 mi_req[1] = SMP_REPORT_MANUF_INFO;
394 res = smp_execute_task(dev, mi_req, MI_REQ_SIZE, mi_resp,MI_RESP_SIZE);
395 if (res) {
396 SAS_DPRINTK("MI: ex %016llx failed:0x%x\n",
397 SAS_ADDR(dev->sas_addr), res);
398 goto out;
399 } else if (mi_resp[2] != SMP_RESP_FUNC_ACC) {
400 SAS_DPRINTK("MI ex %016llx returned SMP result:0x%x\n",
401 SAS_ADDR(dev->sas_addr), mi_resp[2]);
402 goto out;
405 ex_assign_manuf_info(dev, mi_resp);
406 out:
407 kfree(mi_req);
408 kfree(mi_resp);
409 return res;
412 #define PC_REQ_SIZE 44
413 #define PC_RESP_SIZE 8
415 int sas_smp_phy_control(struct domain_device *dev, int phy_id,
416 enum phy_func phy_func,
417 struct sas_phy_linkrates *rates)
419 u8 *pc_req;
420 u8 *pc_resp;
421 int res;
423 pc_req = alloc_smp_req(PC_REQ_SIZE);
424 if (!pc_req)
425 return -ENOMEM;
427 pc_resp = alloc_smp_resp(PC_RESP_SIZE);
428 if (!pc_resp) {
429 kfree(pc_req);
430 return -ENOMEM;
433 pc_req[1] = SMP_PHY_CONTROL;
434 pc_req[9] = phy_id;
435 pc_req[10]= phy_func;
436 if (rates) {
437 pc_req[32] = rates->minimum_linkrate << 4;
438 pc_req[33] = rates->maximum_linkrate << 4;
441 res = smp_execute_task(dev, pc_req, PC_REQ_SIZE, pc_resp,PC_RESP_SIZE);
443 kfree(pc_resp);
444 kfree(pc_req);
445 return res;
448 static void sas_ex_disable_phy(struct domain_device *dev, int phy_id)
450 struct expander_device *ex = &dev->ex_dev;
451 struct ex_phy *phy = &ex->ex_phy[phy_id];
453 sas_smp_phy_control(dev, phy_id, PHY_FUNC_DISABLE, NULL);
454 phy->linkrate = SAS_PHY_DISABLED;
457 static void sas_ex_disable_port(struct domain_device *dev, u8 *sas_addr)
459 struct expander_device *ex = &dev->ex_dev;
460 int i;
462 for (i = 0; i < ex->num_phys; i++) {
463 struct ex_phy *phy = &ex->ex_phy[i];
465 if (phy->phy_state == PHY_VACANT ||
466 phy->phy_state == PHY_NOT_PRESENT)
467 continue;
469 if (SAS_ADDR(phy->attached_sas_addr) == SAS_ADDR(sas_addr))
470 sas_ex_disable_phy(dev, i);
474 static int sas_dev_present_in_domain(struct asd_sas_port *port,
475 u8 *sas_addr)
477 struct domain_device *dev;
479 if (SAS_ADDR(port->sas_addr) == SAS_ADDR(sas_addr))
480 return 1;
481 list_for_each_entry(dev, &port->dev_list, dev_list_node) {
482 if (SAS_ADDR(dev->sas_addr) == SAS_ADDR(sas_addr))
483 return 1;
485 return 0;
488 #define RPEL_REQ_SIZE 16
489 #define RPEL_RESP_SIZE 32
490 int sas_smp_get_phy_events(struct sas_phy *phy)
492 int res;
493 struct sas_rphy *rphy = dev_to_rphy(phy->dev.parent);
494 struct domain_device *dev = sas_find_dev_by_rphy(rphy);
495 u8 *req = alloc_smp_req(RPEL_REQ_SIZE);
496 u8 *resp = kzalloc(RPEL_RESP_SIZE, GFP_KERNEL);
498 if (!resp)
499 return -ENOMEM;
501 req[1] = SMP_REPORT_PHY_ERR_LOG;
502 req[9] = phy->number;
504 res = smp_execute_task(dev, req, RPEL_REQ_SIZE,
505 resp, RPEL_RESP_SIZE);
507 if (!res)
508 goto out;
510 phy->invalid_dword_count = scsi_to_u32(&resp[12]);
511 phy->running_disparity_error_count = scsi_to_u32(&resp[16]);
512 phy->loss_of_dword_sync_count = scsi_to_u32(&resp[20]);
513 phy->phy_reset_problem_count = scsi_to_u32(&resp[24]);
515 out:
516 kfree(resp);
517 return res;
521 #define RPS_REQ_SIZE 16
522 #define RPS_RESP_SIZE 60
524 static int sas_get_report_phy_sata(struct domain_device *dev,
525 int phy_id,
526 struct smp_resp *rps_resp)
528 int res;
529 u8 *rps_req = alloc_smp_req(RPS_REQ_SIZE);
531 if (!rps_req)
532 return -ENOMEM;
534 rps_req[1] = SMP_REPORT_PHY_SATA;
535 rps_req[9] = phy_id;
537 res = smp_execute_task(dev, rps_req, RPS_REQ_SIZE,
538 rps_resp, RPS_RESP_SIZE);
540 kfree(rps_req);
541 return 0;
544 static void sas_ex_get_linkrate(struct domain_device *parent,
545 struct domain_device *child,
546 struct ex_phy *parent_phy)
548 struct expander_device *parent_ex = &parent->ex_dev;
549 struct sas_port *port;
550 int i;
552 child->pathways = 0;
554 port = parent_phy->port;
556 for (i = 0; i < parent_ex->num_phys; i++) {
557 struct ex_phy *phy = &parent_ex->ex_phy[i];
559 if (phy->phy_state == PHY_VACANT ||
560 phy->phy_state == PHY_NOT_PRESENT)
561 continue;
563 if (SAS_ADDR(phy->attached_sas_addr) ==
564 SAS_ADDR(child->sas_addr)) {
566 child->min_linkrate = min(parent->min_linkrate,
567 phy->linkrate);
568 child->max_linkrate = max(parent->max_linkrate,
569 phy->linkrate);
570 child->pathways++;
571 sas_port_add_phy(port, phy->phy);
574 child->linkrate = min(parent_phy->linkrate, child->max_linkrate);
575 child->pathways = min(child->pathways, parent->pathways);
578 static struct domain_device *sas_ex_discover_end_dev(
579 struct domain_device *parent, int phy_id)
581 struct expander_device *parent_ex = &parent->ex_dev;
582 struct ex_phy *phy = &parent_ex->ex_phy[phy_id];
583 struct domain_device *child = NULL;
584 struct sas_rphy *rphy;
585 int res;
587 if (phy->attached_sata_host || phy->attached_sata_ps)
588 return NULL;
590 child = kzalloc(sizeof(*child), GFP_KERNEL);
591 if (!child)
592 return NULL;
594 child->parent = parent;
595 child->port = parent->port;
596 child->iproto = phy->attached_iproto;
597 memcpy(child->sas_addr, phy->attached_sas_addr, SAS_ADDR_SIZE);
598 sas_hash_addr(child->hashed_sas_addr, child->sas_addr);
599 if (!phy->port) {
600 phy->port = sas_port_alloc(&parent->rphy->dev, phy_id);
601 if (unlikely(!phy->port))
602 goto out_err;
603 if (unlikely(sas_port_add(phy->port) != 0)) {
604 sas_port_free(phy->port);
605 goto out_err;
608 sas_ex_get_linkrate(parent, child, phy);
610 if ((phy->attached_tproto & SAS_PROTO_STP) || phy->attached_sata_dev) {
611 child->dev_type = SATA_DEV;
612 if (phy->attached_tproto & SAS_PROTO_STP)
613 child->tproto = phy->attached_tproto;
614 if (phy->attached_sata_dev)
615 child->tproto |= SATA_DEV;
616 res = sas_get_report_phy_sata(parent, phy_id,
617 &child->sata_dev.rps_resp);
618 if (res) {
619 SAS_DPRINTK("report phy sata to %016llx:0x%x returned "
620 "0x%x\n", SAS_ADDR(parent->sas_addr),
621 phy_id, res);
622 goto out_free;
624 memcpy(child->frame_rcvd, &child->sata_dev.rps_resp.rps.fis,
625 sizeof(struct dev_to_host_fis));
626 sas_init_dev(child);
627 res = sas_discover_sata(child);
628 if (res) {
629 SAS_DPRINTK("sas_discover_sata() for device %16llx at "
630 "%016llx:0x%x returned 0x%x\n",
631 SAS_ADDR(child->sas_addr),
632 SAS_ADDR(parent->sas_addr), phy_id, res);
633 goto out_free;
635 } else if (phy->attached_tproto & SAS_PROTO_SSP) {
636 child->dev_type = SAS_END_DEV;
637 rphy = sas_end_device_alloc(phy->port);
638 /* FIXME: error handling */
639 if (unlikely(!rphy))
640 goto out_free;
641 child->tproto = phy->attached_tproto;
642 sas_init_dev(child);
644 child->rphy = rphy;
645 sas_fill_in_rphy(child, rphy);
647 spin_lock(&parent->port->dev_list_lock);
648 list_add_tail(&child->dev_list_node, &parent->port->dev_list);
649 spin_unlock(&parent->port->dev_list_lock);
651 res = sas_discover_end_dev(child);
652 if (res) {
653 SAS_DPRINTK("sas_discover_end_dev() for device %16llx "
654 "at %016llx:0x%x returned 0x%x\n",
655 SAS_ADDR(child->sas_addr),
656 SAS_ADDR(parent->sas_addr), phy_id, res);
657 goto out_list_del;
659 } else {
660 SAS_DPRINTK("target proto 0x%x at %016llx:0x%x not handled\n",
661 phy->attached_tproto, SAS_ADDR(parent->sas_addr),
662 phy_id);
665 list_add_tail(&child->siblings, &parent_ex->children);
666 return child;
668 out_list_del:
669 sas_rphy_free(child->rphy);
670 child->rphy = NULL;
671 list_del(&child->dev_list_node);
672 out_free:
673 sas_port_delete(phy->port);
674 out_err:
675 phy->port = NULL;
676 kfree(child);
677 return NULL;
680 /* See if this phy is part of a wide port */
681 static int sas_ex_join_wide_port(struct domain_device *parent, int phy_id)
683 struct ex_phy *phy = &parent->ex_dev.ex_phy[phy_id];
684 int i;
686 for (i = 0; i < parent->ex_dev.num_phys; i++) {
687 struct ex_phy *ephy = &parent->ex_dev.ex_phy[i];
689 if (ephy == phy)
690 continue;
692 if (!memcmp(phy->attached_sas_addr, ephy->attached_sas_addr,
693 SAS_ADDR_SIZE) && ephy->port) {
694 sas_port_add_phy(ephy->port, phy->phy);
695 phy->phy_state = PHY_DEVICE_DISCOVERED;
696 return 0;
700 return -ENODEV;
703 static struct domain_device *sas_ex_discover_expander(
704 struct domain_device *parent, int phy_id)
706 struct sas_expander_device *parent_ex = rphy_to_expander_device(parent->rphy);
707 struct ex_phy *phy = &parent->ex_dev.ex_phy[phy_id];
708 struct domain_device *child = NULL;
709 struct sas_rphy *rphy;
710 struct sas_expander_device *edev;
711 struct asd_sas_port *port;
712 int res;
714 if (phy->routing_attr == DIRECT_ROUTING) {
715 SAS_DPRINTK("ex %016llx:0x%x:D <--> ex %016llx:0x%x is not "
716 "allowed\n",
717 SAS_ADDR(parent->sas_addr), phy_id,
718 SAS_ADDR(phy->attached_sas_addr),
719 phy->attached_phy_id);
720 return NULL;
722 child = kzalloc(sizeof(*child), GFP_KERNEL);
723 if (!child)
724 return NULL;
726 phy->port = sas_port_alloc(&parent->rphy->dev, phy_id);
727 /* FIXME: better error handling */
728 BUG_ON(sas_port_add(phy->port) != 0);
731 switch (phy->attached_dev_type) {
732 case EDGE_DEV:
733 rphy = sas_expander_alloc(phy->port,
734 SAS_EDGE_EXPANDER_DEVICE);
735 break;
736 case FANOUT_DEV:
737 rphy = sas_expander_alloc(phy->port,
738 SAS_FANOUT_EXPANDER_DEVICE);
739 break;
740 default:
741 rphy = NULL; /* shut gcc up */
742 BUG();
744 port = parent->port;
745 child->rphy = rphy;
746 edev = rphy_to_expander_device(rphy);
747 child->dev_type = phy->attached_dev_type;
748 child->parent = parent;
749 child->port = port;
750 child->iproto = phy->attached_iproto;
751 child->tproto = phy->attached_tproto;
752 memcpy(child->sas_addr, phy->attached_sas_addr, SAS_ADDR_SIZE);
753 sas_hash_addr(child->hashed_sas_addr, child->sas_addr);
754 sas_ex_get_linkrate(parent, child, phy);
755 edev->level = parent_ex->level + 1;
756 parent->port->disc.max_level = max(parent->port->disc.max_level,
757 edev->level);
758 sas_init_dev(child);
759 sas_fill_in_rphy(child, rphy);
760 sas_rphy_add(rphy);
762 spin_lock(&parent->port->dev_list_lock);
763 list_add_tail(&child->dev_list_node, &parent->port->dev_list);
764 spin_unlock(&parent->port->dev_list_lock);
766 res = sas_discover_expander(child);
767 if (res) {
768 kfree(child);
769 return NULL;
771 list_add_tail(&child->siblings, &parent->ex_dev.children);
772 return child;
775 static int sas_ex_discover_dev(struct domain_device *dev, int phy_id)
777 struct expander_device *ex = &dev->ex_dev;
778 struct ex_phy *ex_phy = &ex->ex_phy[phy_id];
779 struct domain_device *child = NULL;
780 int res = 0;
782 /* Phy state */
783 if (ex_phy->linkrate == SAS_SATA_SPINUP_HOLD) {
784 if (!sas_smp_phy_control(dev, phy_id, PHY_FUNC_LINK_RESET, NULL))
785 res = sas_ex_phy_discover(dev, phy_id);
786 if (res)
787 return res;
790 /* Parent and domain coherency */
791 if (!dev->parent && (SAS_ADDR(ex_phy->attached_sas_addr) ==
792 SAS_ADDR(dev->port->sas_addr))) {
793 sas_add_parent_port(dev, phy_id);
794 return 0;
796 if (dev->parent && (SAS_ADDR(ex_phy->attached_sas_addr) ==
797 SAS_ADDR(dev->parent->sas_addr))) {
798 sas_add_parent_port(dev, phy_id);
799 if (ex_phy->routing_attr == TABLE_ROUTING)
800 sas_configure_phy(dev, phy_id, dev->port->sas_addr, 1);
801 return 0;
804 if (sas_dev_present_in_domain(dev->port, ex_phy->attached_sas_addr))
805 sas_ex_disable_port(dev, ex_phy->attached_sas_addr);
807 if (ex_phy->attached_dev_type == NO_DEVICE) {
808 if (ex_phy->routing_attr == DIRECT_ROUTING) {
809 memset(ex_phy->attached_sas_addr, 0, SAS_ADDR_SIZE);
810 sas_configure_routing(dev, ex_phy->attached_sas_addr);
812 return 0;
813 } else if (ex_phy->linkrate == SAS_LINK_RATE_UNKNOWN)
814 return 0;
816 if (ex_phy->attached_dev_type != SAS_END_DEV &&
817 ex_phy->attached_dev_type != FANOUT_DEV &&
818 ex_phy->attached_dev_type != EDGE_DEV) {
819 SAS_DPRINTK("unknown device type(0x%x) attached to ex %016llx "
820 "phy 0x%x\n", ex_phy->attached_dev_type,
821 SAS_ADDR(dev->sas_addr),
822 phy_id);
823 return 0;
826 res = sas_configure_routing(dev, ex_phy->attached_sas_addr);
827 if (res) {
828 SAS_DPRINTK("configure routing for dev %016llx "
829 "reported 0x%x. Forgotten\n",
830 SAS_ADDR(ex_phy->attached_sas_addr), res);
831 sas_disable_routing(dev, ex_phy->attached_sas_addr);
832 return res;
835 res = sas_ex_join_wide_port(dev, phy_id);
836 if (!res) {
837 SAS_DPRINTK("Attaching ex phy%d to wide port %016llx\n",
838 phy_id, SAS_ADDR(ex_phy->attached_sas_addr));
839 return res;
842 switch (ex_phy->attached_dev_type) {
843 case SAS_END_DEV:
844 child = sas_ex_discover_end_dev(dev, phy_id);
845 break;
846 case FANOUT_DEV:
847 if (SAS_ADDR(dev->port->disc.fanout_sas_addr)) {
848 SAS_DPRINTK("second fanout expander %016llx phy 0x%x "
849 "attached to ex %016llx phy 0x%x\n",
850 SAS_ADDR(ex_phy->attached_sas_addr),
851 ex_phy->attached_phy_id,
852 SAS_ADDR(dev->sas_addr),
853 phy_id);
854 sas_ex_disable_phy(dev, phy_id);
855 break;
856 } else
857 memcpy(dev->port->disc.fanout_sas_addr,
858 ex_phy->attached_sas_addr, SAS_ADDR_SIZE);
859 /* fallthrough */
860 case EDGE_DEV:
861 child = sas_ex_discover_expander(dev, phy_id);
862 break;
863 default:
864 break;
867 if (child) {
868 int i;
870 for (i = 0; i < ex->num_phys; i++) {
871 if (ex->ex_phy[i].phy_state == PHY_VACANT ||
872 ex->ex_phy[i].phy_state == PHY_NOT_PRESENT)
873 continue;
875 if (SAS_ADDR(ex->ex_phy[i].attached_sas_addr) ==
876 SAS_ADDR(child->sas_addr))
877 ex->ex_phy[i].phy_state= PHY_DEVICE_DISCOVERED;
881 return res;
884 static int sas_find_sub_addr(struct domain_device *dev, u8 *sub_addr)
886 struct expander_device *ex = &dev->ex_dev;
887 int i;
889 for (i = 0; i < ex->num_phys; i++) {
890 struct ex_phy *phy = &ex->ex_phy[i];
892 if (phy->phy_state == PHY_VACANT ||
893 phy->phy_state == PHY_NOT_PRESENT)
894 continue;
896 if ((phy->attached_dev_type == EDGE_DEV ||
897 phy->attached_dev_type == FANOUT_DEV) &&
898 phy->routing_attr == SUBTRACTIVE_ROUTING) {
900 memcpy(sub_addr, phy->attached_sas_addr,SAS_ADDR_SIZE);
902 return 1;
905 return 0;
908 static int sas_check_level_subtractive_boundary(struct domain_device *dev)
910 struct expander_device *ex = &dev->ex_dev;
911 struct domain_device *child;
912 u8 sub_addr[8] = {0, };
914 list_for_each_entry(child, &ex->children, siblings) {
915 if (child->dev_type != EDGE_DEV &&
916 child->dev_type != FANOUT_DEV)
917 continue;
918 if (sub_addr[0] == 0) {
919 sas_find_sub_addr(child, sub_addr);
920 continue;
921 } else {
922 u8 s2[8];
924 if (sas_find_sub_addr(child, s2) &&
925 (SAS_ADDR(sub_addr) != SAS_ADDR(s2))) {
927 SAS_DPRINTK("ex %016llx->%016llx-?->%016llx "
928 "diverges from subtractive "
929 "boundary %016llx\n",
930 SAS_ADDR(dev->sas_addr),
931 SAS_ADDR(child->sas_addr),
932 SAS_ADDR(s2),
933 SAS_ADDR(sub_addr));
935 sas_ex_disable_port(child, s2);
939 return 0;
942 * sas_ex_discover_devices -- discover devices attached to this expander
943 * dev: pointer to the expander domain device
944 * single: if you want to do a single phy, else set to -1;
946 * Configure this expander for use with its devices and register the
947 * devices of this expander.
949 static int sas_ex_discover_devices(struct domain_device *dev, int single)
951 struct expander_device *ex = &dev->ex_dev;
952 int i = 0, end = ex->num_phys;
953 int res = 0;
955 if (0 <= single && single < end) {
956 i = single;
957 end = i+1;
960 for ( ; i < end; i++) {
961 struct ex_phy *ex_phy = &ex->ex_phy[i];
963 if (ex_phy->phy_state == PHY_VACANT ||
964 ex_phy->phy_state == PHY_NOT_PRESENT ||
965 ex_phy->phy_state == PHY_DEVICE_DISCOVERED)
966 continue;
968 switch (ex_phy->linkrate) {
969 case SAS_PHY_DISABLED:
970 case SAS_PHY_RESET_PROBLEM:
971 case SAS_SATA_PORT_SELECTOR:
972 continue;
973 default:
974 res = sas_ex_discover_dev(dev, i);
975 if (res)
976 break;
977 continue;
981 if (!res)
982 sas_check_level_subtractive_boundary(dev);
984 return res;
987 static int sas_check_ex_subtractive_boundary(struct domain_device *dev)
989 struct expander_device *ex = &dev->ex_dev;
990 int i;
991 u8 *sub_sas_addr = NULL;
993 if (dev->dev_type != EDGE_DEV)
994 return 0;
996 for (i = 0; i < ex->num_phys; i++) {
997 struct ex_phy *phy = &ex->ex_phy[i];
999 if (phy->phy_state == PHY_VACANT ||
1000 phy->phy_state == PHY_NOT_PRESENT)
1001 continue;
1003 if ((phy->attached_dev_type == FANOUT_DEV ||
1004 phy->attached_dev_type == EDGE_DEV) &&
1005 phy->routing_attr == SUBTRACTIVE_ROUTING) {
1007 if (!sub_sas_addr)
1008 sub_sas_addr = &phy->attached_sas_addr[0];
1009 else if (SAS_ADDR(sub_sas_addr) !=
1010 SAS_ADDR(phy->attached_sas_addr)) {
1012 SAS_DPRINTK("ex %016llx phy 0x%x "
1013 "diverges(%016llx) on subtractive "
1014 "boundary(%016llx). Disabled\n",
1015 SAS_ADDR(dev->sas_addr), i,
1016 SAS_ADDR(phy->attached_sas_addr),
1017 SAS_ADDR(sub_sas_addr));
1018 sas_ex_disable_phy(dev, i);
1022 return 0;
1025 static void sas_print_parent_topology_bug(struct domain_device *child,
1026 struct ex_phy *parent_phy,
1027 struct ex_phy *child_phy)
1029 static const char ra_char[] = {
1030 [DIRECT_ROUTING] = 'D',
1031 [SUBTRACTIVE_ROUTING] = 'S',
1032 [TABLE_ROUTING] = 'T',
1034 static const char *ex_type[] = {
1035 [EDGE_DEV] = "edge",
1036 [FANOUT_DEV] = "fanout",
1038 struct domain_device *parent = child->parent;
1040 sas_printk("%s ex %016llx phy 0x%x <--> %s ex %016llx phy 0x%x "
1041 "has %c:%c routing link!\n",
1043 ex_type[parent->dev_type],
1044 SAS_ADDR(parent->sas_addr),
1045 parent_phy->phy_id,
1047 ex_type[child->dev_type],
1048 SAS_ADDR(child->sas_addr),
1049 child_phy->phy_id,
1051 ra_char[parent_phy->routing_attr],
1052 ra_char[child_phy->routing_attr]);
1055 static int sas_check_eeds(struct domain_device *child,
1056 struct ex_phy *parent_phy,
1057 struct ex_phy *child_phy)
1059 int res = 0;
1060 struct domain_device *parent = child->parent;
1062 if (SAS_ADDR(parent->port->disc.fanout_sas_addr) != 0) {
1063 res = -ENODEV;
1064 SAS_DPRINTK("edge ex %016llx phy S:0x%x <--> edge ex %016llx "
1065 "phy S:0x%x, while there is a fanout ex %016llx\n",
1066 SAS_ADDR(parent->sas_addr),
1067 parent_phy->phy_id,
1068 SAS_ADDR(child->sas_addr),
1069 child_phy->phy_id,
1070 SAS_ADDR(parent->port->disc.fanout_sas_addr));
1071 } else if (SAS_ADDR(parent->port->disc.eeds_a) == 0) {
1072 memcpy(parent->port->disc.eeds_a, parent->sas_addr,
1073 SAS_ADDR_SIZE);
1074 memcpy(parent->port->disc.eeds_b, child->sas_addr,
1075 SAS_ADDR_SIZE);
1076 } else if (((SAS_ADDR(parent->port->disc.eeds_a) ==
1077 SAS_ADDR(parent->sas_addr)) ||
1078 (SAS_ADDR(parent->port->disc.eeds_a) ==
1079 SAS_ADDR(child->sas_addr)))
1081 ((SAS_ADDR(parent->port->disc.eeds_b) ==
1082 SAS_ADDR(parent->sas_addr)) ||
1083 (SAS_ADDR(parent->port->disc.eeds_b) ==
1084 SAS_ADDR(child->sas_addr))))
1086 else {
1087 res = -ENODEV;
1088 SAS_DPRINTK("edge ex %016llx phy 0x%x <--> edge ex %016llx "
1089 "phy 0x%x link forms a third EEDS!\n",
1090 SAS_ADDR(parent->sas_addr),
1091 parent_phy->phy_id,
1092 SAS_ADDR(child->sas_addr),
1093 child_phy->phy_id);
1096 return res;
1099 /* Here we spill over 80 columns. It is intentional.
1101 static int sas_check_parent_topology(struct domain_device *child)
1103 struct expander_device *child_ex = &child->ex_dev;
1104 struct expander_device *parent_ex;
1105 int i;
1106 int res = 0;
1108 if (!child->parent)
1109 return 0;
1111 if (child->parent->dev_type != EDGE_DEV &&
1112 child->parent->dev_type != FANOUT_DEV)
1113 return 0;
1115 parent_ex = &child->parent->ex_dev;
1117 for (i = 0; i < parent_ex->num_phys; i++) {
1118 struct ex_phy *parent_phy = &parent_ex->ex_phy[i];
1119 struct ex_phy *child_phy;
1121 if (parent_phy->phy_state == PHY_VACANT ||
1122 parent_phy->phy_state == PHY_NOT_PRESENT)
1123 continue;
1125 if (SAS_ADDR(parent_phy->attached_sas_addr) != SAS_ADDR(child->sas_addr))
1126 continue;
1128 child_phy = &child_ex->ex_phy[parent_phy->attached_phy_id];
1130 switch (child->parent->dev_type) {
1131 case EDGE_DEV:
1132 if (child->dev_type == FANOUT_DEV) {
1133 if (parent_phy->routing_attr != SUBTRACTIVE_ROUTING ||
1134 child_phy->routing_attr != TABLE_ROUTING) {
1135 sas_print_parent_topology_bug(child, parent_phy, child_phy);
1136 res = -ENODEV;
1138 } else if (parent_phy->routing_attr == SUBTRACTIVE_ROUTING) {
1139 if (child_phy->routing_attr == SUBTRACTIVE_ROUTING) {
1140 res = sas_check_eeds(child, parent_phy, child_phy);
1141 } else if (child_phy->routing_attr != TABLE_ROUTING) {
1142 sas_print_parent_topology_bug(child, parent_phy, child_phy);
1143 res = -ENODEV;
1145 } else if (parent_phy->routing_attr == TABLE_ROUTING &&
1146 child_phy->routing_attr != SUBTRACTIVE_ROUTING) {
1147 sas_print_parent_topology_bug(child, parent_phy, child_phy);
1148 res = -ENODEV;
1150 break;
1151 case FANOUT_DEV:
1152 if (parent_phy->routing_attr != TABLE_ROUTING ||
1153 child_phy->routing_attr != SUBTRACTIVE_ROUTING) {
1154 sas_print_parent_topology_bug(child, parent_phy, child_phy);
1155 res = -ENODEV;
1157 break;
1158 default:
1159 break;
1163 return res;
1166 #define RRI_REQ_SIZE 16
1167 #define RRI_RESP_SIZE 44
1169 static int sas_configure_present(struct domain_device *dev, int phy_id,
1170 u8 *sas_addr, int *index, int *present)
1172 int i, res = 0;
1173 struct expander_device *ex = &dev->ex_dev;
1174 struct ex_phy *phy = &ex->ex_phy[phy_id];
1175 u8 *rri_req;
1176 u8 *rri_resp;
1178 *present = 0;
1179 *index = 0;
1181 rri_req = alloc_smp_req(RRI_REQ_SIZE);
1182 if (!rri_req)
1183 return -ENOMEM;
1185 rri_resp = alloc_smp_resp(RRI_RESP_SIZE);
1186 if (!rri_resp) {
1187 kfree(rri_req);
1188 return -ENOMEM;
1191 rri_req[1] = SMP_REPORT_ROUTE_INFO;
1192 rri_req[9] = phy_id;
1194 for (i = 0; i < ex->max_route_indexes ; i++) {
1195 *(__be16 *)(rri_req+6) = cpu_to_be16(i);
1196 res = smp_execute_task(dev, rri_req, RRI_REQ_SIZE, rri_resp,
1197 RRI_RESP_SIZE);
1198 if (res)
1199 goto out;
1200 res = rri_resp[2];
1201 if (res == SMP_RESP_NO_INDEX) {
1202 SAS_DPRINTK("overflow of indexes: dev %016llx "
1203 "phy 0x%x index 0x%x\n",
1204 SAS_ADDR(dev->sas_addr), phy_id, i);
1205 goto out;
1206 } else if (res != SMP_RESP_FUNC_ACC) {
1207 SAS_DPRINTK("%s: dev %016llx phy 0x%x index 0x%x "
1208 "result 0x%x\n", __FUNCTION__,
1209 SAS_ADDR(dev->sas_addr), phy_id, i, res);
1210 goto out;
1212 if (SAS_ADDR(sas_addr) != 0) {
1213 if (SAS_ADDR(rri_resp+16) == SAS_ADDR(sas_addr)) {
1214 *index = i;
1215 if ((rri_resp[12] & 0x80) == 0x80)
1216 *present = 0;
1217 else
1218 *present = 1;
1219 goto out;
1220 } else if (SAS_ADDR(rri_resp+16) == 0) {
1221 *index = i;
1222 *present = 0;
1223 goto out;
1225 } else if (SAS_ADDR(rri_resp+16) == 0 &&
1226 phy->last_da_index < i) {
1227 phy->last_da_index = i;
1228 *index = i;
1229 *present = 0;
1230 goto out;
1233 res = -1;
1234 out:
1235 kfree(rri_req);
1236 kfree(rri_resp);
1237 return res;
1240 #define CRI_REQ_SIZE 44
1241 #define CRI_RESP_SIZE 8
1243 static int sas_configure_set(struct domain_device *dev, int phy_id,
1244 u8 *sas_addr, int index, int include)
1246 int res;
1247 u8 *cri_req;
1248 u8 *cri_resp;
1250 cri_req = alloc_smp_req(CRI_REQ_SIZE);
1251 if (!cri_req)
1252 return -ENOMEM;
1254 cri_resp = alloc_smp_resp(CRI_RESP_SIZE);
1255 if (!cri_resp) {
1256 kfree(cri_req);
1257 return -ENOMEM;
1260 cri_req[1] = SMP_CONF_ROUTE_INFO;
1261 *(__be16 *)(cri_req+6) = cpu_to_be16(index);
1262 cri_req[9] = phy_id;
1263 if (SAS_ADDR(sas_addr) == 0 || !include)
1264 cri_req[12] |= 0x80;
1265 memcpy(cri_req+16, sas_addr, SAS_ADDR_SIZE);
1267 res = smp_execute_task(dev, cri_req, CRI_REQ_SIZE, cri_resp,
1268 CRI_RESP_SIZE);
1269 if (res)
1270 goto out;
1271 res = cri_resp[2];
1272 if (res == SMP_RESP_NO_INDEX) {
1273 SAS_DPRINTK("overflow of indexes: dev %016llx phy 0x%x "
1274 "index 0x%x\n",
1275 SAS_ADDR(dev->sas_addr), phy_id, index);
1277 out:
1278 kfree(cri_req);
1279 kfree(cri_resp);
1280 return res;
1283 static int sas_configure_phy(struct domain_device *dev, int phy_id,
1284 u8 *sas_addr, int include)
1286 int index;
1287 int present;
1288 int res;
1290 res = sas_configure_present(dev, phy_id, sas_addr, &index, &present);
1291 if (res)
1292 return res;
1293 if (include ^ present)
1294 return sas_configure_set(dev, phy_id, sas_addr, index,include);
1296 return res;
1300 * sas_configure_parent -- configure routing table of parent
1301 * parent: parent expander
1302 * child: child expander
1303 * sas_addr: SAS port identifier of device directly attached to child
1305 static int sas_configure_parent(struct domain_device *parent,
1306 struct domain_device *child,
1307 u8 *sas_addr, int include)
1309 struct expander_device *ex_parent = &parent->ex_dev;
1310 int res = 0;
1311 int i;
1313 if (parent->parent) {
1314 res = sas_configure_parent(parent->parent, parent, sas_addr,
1315 include);
1316 if (res)
1317 return res;
1320 if (ex_parent->conf_route_table == 0) {
1321 SAS_DPRINTK("ex %016llx has self-configuring routing table\n",
1322 SAS_ADDR(parent->sas_addr));
1323 return 0;
1326 for (i = 0; i < ex_parent->num_phys; i++) {
1327 struct ex_phy *phy = &ex_parent->ex_phy[i];
1329 if ((phy->routing_attr == TABLE_ROUTING) &&
1330 (SAS_ADDR(phy->attached_sas_addr) ==
1331 SAS_ADDR(child->sas_addr))) {
1332 res = sas_configure_phy(parent, i, sas_addr, include);
1333 if (res)
1334 return res;
1338 return res;
1342 * sas_configure_routing -- configure routing
1343 * dev: expander device
1344 * sas_addr: port identifier of device directly attached to the expander device
1346 static int sas_configure_routing(struct domain_device *dev, u8 *sas_addr)
1348 if (dev->parent)
1349 return sas_configure_parent(dev->parent, dev, sas_addr, 1);
1350 return 0;
1353 static int sas_disable_routing(struct domain_device *dev, u8 *sas_addr)
1355 if (dev->parent)
1356 return sas_configure_parent(dev->parent, dev, sas_addr, 0);
1357 return 0;
1360 #if 0
1361 #define SMP_BIN_ATTR_NAME "smp_portal"
1363 static void sas_ex_smp_hook(struct domain_device *dev)
1365 struct expander_device *ex_dev = &dev->ex_dev;
1366 struct bin_attribute *bin_attr = &ex_dev->smp_bin_attr;
1368 memset(bin_attr, 0, sizeof(*bin_attr));
1370 bin_attr->attr.name = SMP_BIN_ATTR_NAME;
1371 bin_attr->attr.owner = THIS_MODULE;
1372 bin_attr->attr.mode = 0600;
1374 bin_attr->size = 0;
1375 bin_attr->private = NULL;
1376 bin_attr->read = smp_portal_read;
1377 bin_attr->write= smp_portal_write;
1378 bin_attr->mmap = NULL;
1380 ex_dev->smp_portal_pid = -1;
1381 init_MUTEX(&ex_dev->smp_sema);
1383 #endif
1386 * sas_discover_expander -- expander discovery
1387 * @ex: pointer to expander domain device
1389 * See comment in sas_discover_sata().
1391 static int sas_discover_expander(struct domain_device *dev)
1393 int res;
1395 res = sas_notify_lldd_dev_found(dev);
1396 if (res)
1397 return res;
1399 res = sas_ex_general(dev);
1400 if (res)
1401 goto out_err;
1402 res = sas_ex_manuf_info(dev);
1403 if (res)
1404 goto out_err;
1406 res = sas_expander_discover(dev);
1407 if (res) {
1408 SAS_DPRINTK("expander %016llx discovery failed(0x%x)\n",
1409 SAS_ADDR(dev->sas_addr), res);
1410 goto out_err;
1413 sas_check_ex_subtractive_boundary(dev);
1414 res = sas_check_parent_topology(dev);
1415 if (res)
1416 goto out_err;
1417 return 0;
1418 out_err:
1419 sas_notify_lldd_dev_gone(dev);
1420 return res;
1423 static int sas_ex_level_discovery(struct asd_sas_port *port, const int level)
1425 int res = 0;
1426 struct domain_device *dev;
1428 list_for_each_entry(dev, &port->dev_list, dev_list_node) {
1429 if (dev->dev_type == EDGE_DEV ||
1430 dev->dev_type == FANOUT_DEV) {
1431 struct sas_expander_device *ex =
1432 rphy_to_expander_device(dev->rphy);
1434 if (level == ex->level)
1435 res = sas_ex_discover_devices(dev, -1);
1436 else if (level > 0)
1437 res = sas_ex_discover_devices(port->port_dev, -1);
1442 return res;
1445 static int sas_ex_bfs_disc(struct asd_sas_port *port)
1447 int res;
1448 int level;
1450 do {
1451 level = port->disc.max_level;
1452 res = sas_ex_level_discovery(port, level);
1453 mb();
1454 } while (level < port->disc.max_level);
1456 return res;
1459 int sas_discover_root_expander(struct domain_device *dev)
1461 int res;
1462 struct sas_expander_device *ex = rphy_to_expander_device(dev->rphy);
1464 res = sas_rphy_add(dev->rphy);
1465 if (res)
1466 goto out_err;
1468 ex->level = dev->port->disc.max_level; /* 0 */
1469 res = sas_discover_expander(dev);
1470 if (res)
1471 goto out_err2;
1473 sas_ex_bfs_disc(dev->port);
1475 return res;
1477 out_err2:
1478 sas_rphy_remove(dev->rphy);
1479 out_err:
1480 return res;
1483 /* ---------- Domain revalidation ---------- */
1485 static int sas_get_phy_discover(struct domain_device *dev,
1486 int phy_id, struct smp_resp *disc_resp)
1488 int res;
1489 u8 *disc_req;
1491 disc_req = alloc_smp_req(DISCOVER_REQ_SIZE);
1492 if (!disc_req)
1493 return -ENOMEM;
1495 disc_req[1] = SMP_DISCOVER;
1496 disc_req[9] = phy_id;
1498 res = smp_execute_task(dev, disc_req, DISCOVER_REQ_SIZE,
1499 disc_resp, DISCOVER_RESP_SIZE);
1500 if (res)
1501 goto out;
1502 else if (disc_resp->result != SMP_RESP_FUNC_ACC) {
1503 res = disc_resp->result;
1504 goto out;
1506 out:
1507 kfree(disc_req);
1508 return res;
1511 static int sas_get_phy_change_count(struct domain_device *dev,
1512 int phy_id, int *pcc)
1514 int res;
1515 struct smp_resp *disc_resp;
1517 disc_resp = alloc_smp_resp(DISCOVER_RESP_SIZE);
1518 if (!disc_resp)
1519 return -ENOMEM;
1521 res = sas_get_phy_discover(dev, phy_id, disc_resp);
1522 if (!res)
1523 *pcc = disc_resp->disc.change_count;
1525 kfree(disc_resp);
1526 return res;
1529 static int sas_get_phy_attached_sas_addr(struct domain_device *dev,
1530 int phy_id, u8 *attached_sas_addr)
1532 int res;
1533 struct smp_resp *disc_resp;
1534 struct discover_resp *dr;
1536 disc_resp = alloc_smp_resp(DISCOVER_RESP_SIZE);
1537 if (!disc_resp)
1538 return -ENOMEM;
1539 dr = &disc_resp->disc;
1541 res = sas_get_phy_discover(dev, phy_id, disc_resp);
1542 if (!res) {
1543 memcpy(attached_sas_addr,disc_resp->disc.attached_sas_addr,8);
1544 if (dr->attached_dev_type == 0)
1545 memset(attached_sas_addr, 0, 8);
1547 kfree(disc_resp);
1548 return res;
1551 static int sas_find_bcast_phy(struct domain_device *dev, int *phy_id,
1552 int from_phy)
1554 struct expander_device *ex = &dev->ex_dev;
1555 int res = 0;
1556 int i;
1558 for (i = from_phy; i < ex->num_phys; i++) {
1559 int phy_change_count = 0;
1561 res = sas_get_phy_change_count(dev, i, &phy_change_count);
1562 if (res)
1563 goto out;
1564 else if (phy_change_count != ex->ex_phy[i].phy_change_count) {
1565 ex->ex_phy[i].phy_change_count = phy_change_count;
1566 *phy_id = i;
1567 return 0;
1570 out:
1571 return res;
1574 static int sas_get_ex_change_count(struct domain_device *dev, int *ecc)
1576 int res;
1577 u8 *rg_req;
1578 struct smp_resp *rg_resp;
1580 rg_req = alloc_smp_req(RG_REQ_SIZE);
1581 if (!rg_req)
1582 return -ENOMEM;
1584 rg_resp = alloc_smp_resp(RG_RESP_SIZE);
1585 if (!rg_resp) {
1586 kfree(rg_req);
1587 return -ENOMEM;
1590 rg_req[1] = SMP_REPORT_GENERAL;
1592 res = smp_execute_task(dev, rg_req, RG_REQ_SIZE, rg_resp,
1593 RG_RESP_SIZE);
1594 if (res)
1595 goto out;
1596 if (rg_resp->result != SMP_RESP_FUNC_ACC) {
1597 res = rg_resp->result;
1598 goto out;
1601 *ecc = be16_to_cpu(rg_resp->rg.change_count);
1602 out:
1603 kfree(rg_resp);
1604 kfree(rg_req);
1605 return res;
1608 static int sas_find_bcast_dev(struct domain_device *dev,
1609 struct domain_device **src_dev)
1611 struct expander_device *ex = &dev->ex_dev;
1612 int ex_change_count = -1;
1613 int res;
1615 res = sas_get_ex_change_count(dev, &ex_change_count);
1616 if (res)
1617 goto out;
1618 if (ex_change_count != -1 &&
1619 ex_change_count != ex->ex_change_count) {
1620 *src_dev = dev;
1621 ex->ex_change_count = ex_change_count;
1622 } else {
1623 struct domain_device *ch;
1625 list_for_each_entry(ch, &ex->children, siblings) {
1626 if (ch->dev_type == EDGE_DEV ||
1627 ch->dev_type == FANOUT_DEV) {
1628 res = sas_find_bcast_dev(ch, src_dev);
1629 if (src_dev)
1630 return res;
1634 out:
1635 return res;
1638 static void sas_unregister_ex_tree(struct domain_device *dev)
1640 struct expander_device *ex = &dev->ex_dev;
1641 struct domain_device *child, *n;
1643 list_for_each_entry_safe(child, n, &ex->children, siblings) {
1644 if (child->dev_type == EDGE_DEV ||
1645 child->dev_type == FANOUT_DEV)
1646 sas_unregister_ex_tree(child);
1647 else
1648 sas_unregister_dev(child);
1650 sas_unregister_dev(dev);
1653 static void sas_unregister_devs_sas_addr(struct domain_device *parent,
1654 int phy_id)
1656 struct expander_device *ex_dev = &parent->ex_dev;
1657 struct ex_phy *phy = &ex_dev->ex_phy[phy_id];
1658 struct domain_device *child, *n;
1660 list_for_each_entry_safe(child, n, &ex_dev->children, siblings) {
1661 if (SAS_ADDR(child->sas_addr) ==
1662 SAS_ADDR(phy->attached_sas_addr)) {
1663 if (child->dev_type == EDGE_DEV ||
1664 child->dev_type == FANOUT_DEV)
1665 sas_unregister_ex_tree(child);
1666 else
1667 sas_unregister_dev(child);
1668 break;
1671 sas_disable_routing(parent, phy->attached_sas_addr);
1672 memset(phy->attached_sas_addr, 0, SAS_ADDR_SIZE);
1673 sas_port_delete_phy(phy->port, phy->phy);
1674 if (phy->port->num_phys == 0)
1675 sas_port_delete(phy->port);
1676 phy->port = NULL;
1679 static int sas_discover_bfs_by_root_level(struct domain_device *root,
1680 const int level)
1682 struct expander_device *ex_root = &root->ex_dev;
1683 struct domain_device *child;
1684 int res = 0;
1686 list_for_each_entry(child, &ex_root->children, siblings) {
1687 if (child->dev_type == EDGE_DEV ||
1688 child->dev_type == FANOUT_DEV) {
1689 struct sas_expander_device *ex =
1690 rphy_to_expander_device(child->rphy);
1692 if (level > ex->level)
1693 res = sas_discover_bfs_by_root_level(child,
1694 level);
1695 else if (level == ex->level)
1696 res = sas_ex_discover_devices(child, -1);
1699 return res;
1702 static int sas_discover_bfs_by_root(struct domain_device *dev)
1704 int res;
1705 struct sas_expander_device *ex = rphy_to_expander_device(dev->rphy);
1706 int level = ex->level+1;
1708 res = sas_ex_discover_devices(dev, -1);
1709 if (res)
1710 goto out;
1711 do {
1712 res = sas_discover_bfs_by_root_level(dev, level);
1713 mb();
1714 level += 1;
1715 } while (level <= dev->port->disc.max_level);
1716 out:
1717 return res;
1720 static int sas_discover_new(struct domain_device *dev, int phy_id)
1722 struct ex_phy *ex_phy = &dev->ex_dev.ex_phy[phy_id];
1723 struct domain_device *child;
1724 int res;
1726 SAS_DPRINTK("ex %016llx phy%d new device attached\n",
1727 SAS_ADDR(dev->sas_addr), phy_id);
1728 res = sas_ex_phy_discover(dev, phy_id);
1729 if (res)
1730 goto out;
1731 res = sas_ex_discover_devices(dev, phy_id);
1732 if (res)
1733 goto out;
1734 list_for_each_entry(child, &dev->ex_dev.children, siblings) {
1735 if (SAS_ADDR(child->sas_addr) ==
1736 SAS_ADDR(ex_phy->attached_sas_addr)) {
1737 if (child->dev_type == EDGE_DEV ||
1738 child->dev_type == FANOUT_DEV)
1739 res = sas_discover_bfs_by_root(child);
1740 break;
1743 out:
1744 return res;
1747 static int sas_rediscover_dev(struct domain_device *dev, int phy_id)
1749 struct expander_device *ex = &dev->ex_dev;
1750 struct ex_phy *phy = &ex->ex_phy[phy_id];
1751 u8 attached_sas_addr[8];
1752 int res;
1754 res = sas_get_phy_attached_sas_addr(dev, phy_id, attached_sas_addr);
1755 switch (res) {
1756 case SMP_RESP_NO_PHY:
1757 phy->phy_state = PHY_NOT_PRESENT;
1758 sas_unregister_devs_sas_addr(dev, phy_id);
1759 goto out; break;
1760 case SMP_RESP_PHY_VACANT:
1761 phy->phy_state = PHY_VACANT;
1762 sas_unregister_devs_sas_addr(dev, phy_id);
1763 goto out; break;
1764 case SMP_RESP_FUNC_ACC:
1765 break;
1768 if (SAS_ADDR(attached_sas_addr) == 0) {
1769 phy->phy_state = PHY_EMPTY;
1770 sas_unregister_devs_sas_addr(dev, phy_id);
1771 } else if (SAS_ADDR(attached_sas_addr) ==
1772 SAS_ADDR(phy->attached_sas_addr)) {
1773 SAS_DPRINTK("ex %016llx phy 0x%x broadcast flutter\n",
1774 SAS_ADDR(dev->sas_addr), phy_id);
1775 sas_ex_phy_discover(dev, phy_id);
1776 } else
1777 res = sas_discover_new(dev, phy_id);
1778 out:
1779 return res;
1782 static int sas_rediscover(struct domain_device *dev, const int phy_id)
1784 struct expander_device *ex = &dev->ex_dev;
1785 struct ex_phy *changed_phy = &ex->ex_phy[phy_id];
1786 int res = 0;
1787 int i;
1789 SAS_DPRINTK("ex %016llx phy%d originated BROADCAST(CHANGE)\n",
1790 SAS_ADDR(dev->sas_addr), phy_id);
1792 if (SAS_ADDR(changed_phy->attached_sas_addr) != 0) {
1793 for (i = 0; i < ex->num_phys; i++) {
1794 struct ex_phy *phy = &ex->ex_phy[i];
1796 if (i == phy_id)
1797 continue;
1798 if (SAS_ADDR(phy->attached_sas_addr) ==
1799 SAS_ADDR(changed_phy->attached_sas_addr)) {
1800 SAS_DPRINTK("phy%d part of wide port with "
1801 "phy%d\n", phy_id, i);
1802 goto out;
1805 res = sas_rediscover_dev(dev, phy_id);
1806 } else
1807 res = sas_discover_new(dev, phy_id);
1808 out:
1809 return res;
1813 * sas_revalidate_domain -- revalidate the domain
1814 * @port: port to the domain of interest
1816 * NOTE: this process _must_ quit (return) as soon as any connection
1817 * errors are encountered. Connection recovery is done elsewhere.
1818 * Discover process only interrogates devices in order to discover the
1819 * domain.
1821 int sas_ex_revalidate_domain(struct domain_device *port_dev)
1823 int res;
1824 struct domain_device *dev = NULL;
1826 res = sas_find_bcast_dev(port_dev, &dev);
1827 if (res)
1828 goto out;
1829 if (dev) {
1830 struct expander_device *ex = &dev->ex_dev;
1831 int i = 0, phy_id;
1833 do {
1834 phy_id = -1;
1835 res = sas_find_bcast_phy(dev, &phy_id, i);
1836 if (phy_id == -1)
1837 break;
1838 res = sas_rediscover(dev, phy_id);
1839 i = phy_id + 1;
1840 } while (i < ex->num_phys);
1842 out:
1843 return res;
1846 #if 0
1847 /* ---------- SMP portal ---------- */
1849 static ssize_t smp_portal_write(struct kobject *kobj, char *buf, loff_t offs,
1850 size_t size)
1852 struct domain_device *dev = to_dom_device(kobj);
1853 struct expander_device *ex = &dev->ex_dev;
1855 if (offs != 0)
1856 return -EFBIG;
1857 else if (size == 0)
1858 return 0;
1860 down_interruptible(&ex->smp_sema);
1861 if (ex->smp_req)
1862 kfree(ex->smp_req);
1863 ex->smp_req = kzalloc(size, GFP_USER);
1864 if (!ex->smp_req) {
1865 up(&ex->smp_sema);
1866 return -ENOMEM;
1868 memcpy(ex->smp_req, buf, size);
1869 ex->smp_req_size = size;
1870 ex->smp_portal_pid = current->pid;
1871 up(&ex->smp_sema);
1873 return size;
1876 static ssize_t smp_portal_read(struct kobject *kobj, char *buf, loff_t offs,
1877 size_t size)
1879 struct domain_device *dev = to_dom_device(kobj);
1880 struct expander_device *ex = &dev->ex_dev;
1881 u8 *smp_resp;
1882 int res = -EINVAL;
1884 /* XXX: sysfs gives us an offset of 0x10 or 0x8 while in fact
1885 * it should be 0.
1888 down_interruptible(&ex->smp_sema);
1889 if (!ex->smp_req || ex->smp_portal_pid != current->pid)
1890 goto out;
1892 res = 0;
1893 if (size == 0)
1894 goto out;
1896 res = -ENOMEM;
1897 smp_resp = alloc_smp_resp(size);
1898 if (!smp_resp)
1899 goto out;
1900 res = smp_execute_task(dev, ex->smp_req, ex->smp_req_size,
1901 smp_resp, size);
1902 if (!res) {
1903 memcpy(buf, smp_resp, size);
1904 res = size;
1907 kfree(smp_resp);
1908 out:
1909 kfree(ex->smp_req);
1910 ex->smp_req = NULL;
1911 ex->smp_req_size = 0;
1912 ex->smp_portal_pid = -1;
1913 up(&ex->smp_sema);
1914 return res;
1916 #endif