[SCSI] libsas: add SAS management protocol handler
[linux-2.6/kvm.git] / drivers / scsi / libsas / sas_expander.c
bloba81195354b9cdfff0e701bfe126a0f9df9b326f4
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>
26 #include <linux/blkdev.h>
28 #include "sas_internal.h"
30 #include <scsi/scsi_transport.h>
31 #include <scsi/scsi_transport_sas.h>
32 #include "../scsi_sas_internal.h"
34 static int sas_discover_expander(struct domain_device *dev);
35 static int sas_configure_routing(struct domain_device *dev, u8 *sas_addr);
36 static int sas_configure_phy(struct domain_device *dev, int phy_id,
37 u8 *sas_addr, int include);
38 static int sas_disable_routing(struct domain_device *dev, u8 *sas_addr);
40 #if 0
41 /* FIXME: smp needs to migrate into the sas class */
42 static ssize_t smp_portal_read(struct kobject *, struct bin_attribute *,
43 char *, loff_t, size_t);
44 static ssize_t smp_portal_write(struct kobject *, struct bin_attribute *,
45 char *, loff_t, size_t);
46 #endif
48 /* ---------- SMP task management ---------- */
50 static void smp_task_timedout(unsigned long _task)
52 struct sas_task *task = (void *) _task;
53 unsigned long flags;
55 spin_lock_irqsave(&task->task_state_lock, flags);
56 if (!(task->task_state_flags & SAS_TASK_STATE_DONE))
57 task->task_state_flags |= SAS_TASK_STATE_ABORTED;
58 spin_unlock_irqrestore(&task->task_state_lock, flags);
60 complete(&task->completion);
63 static void smp_task_done(struct sas_task *task)
65 if (!del_timer(&task->timer))
66 return;
67 complete(&task->completion);
70 /* Give it some long enough timeout. In seconds. */
71 #define SMP_TIMEOUT 10
73 static int smp_execute_task(struct domain_device *dev, void *req, int req_size,
74 void *resp, int resp_size)
76 int res, retry;
77 struct sas_task *task = NULL;
78 struct sas_internal *i =
79 to_sas_internal(dev->port->ha->core.shost->transportt);
81 for (retry = 0; retry < 3; retry++) {
82 task = sas_alloc_task(GFP_KERNEL);
83 if (!task)
84 return -ENOMEM;
86 task->dev = dev;
87 task->task_proto = dev->tproto;
88 sg_init_one(&task->smp_task.smp_req, req, req_size);
89 sg_init_one(&task->smp_task.smp_resp, resp, resp_size);
91 task->task_done = smp_task_done;
93 task->timer.data = (unsigned long) task;
94 task->timer.function = smp_task_timedout;
95 task->timer.expires = jiffies + SMP_TIMEOUT*HZ;
96 add_timer(&task->timer);
98 res = i->dft->lldd_execute_task(task, 1, GFP_KERNEL);
100 if (res) {
101 del_timer(&task->timer);
102 SAS_DPRINTK("executing SMP task failed:%d\n", res);
103 goto ex_err;
106 wait_for_completion(&task->completion);
107 res = -ETASK;
108 if ((task->task_state_flags & SAS_TASK_STATE_ABORTED)) {
109 SAS_DPRINTK("smp task timed out or aborted\n");
110 i->dft->lldd_abort_task(task);
111 if (!(task->task_state_flags & SAS_TASK_STATE_DONE)) {
112 SAS_DPRINTK("SMP task aborted and not done\n");
113 goto ex_err;
116 if (task->task_status.resp == SAS_TASK_COMPLETE &&
117 task->task_status.stat == SAM_GOOD) {
118 res = 0;
119 break;
120 } else {
121 SAS_DPRINTK("%s: task to dev %016llx response: 0x%x "
122 "status 0x%x\n", __FUNCTION__,
123 SAS_ADDR(dev->sas_addr),
124 task->task_status.resp,
125 task->task_status.stat);
126 sas_free_task(task);
127 task = NULL;
130 ex_err:
131 BUG_ON(retry == 3 && task != NULL);
132 if (task != NULL) {
133 sas_free_task(task);
135 return res;
138 /* ---------- Allocations ---------- */
140 static inline void *alloc_smp_req(int size)
142 u8 *p = kzalloc(size, GFP_KERNEL);
143 if (p)
144 p[0] = SMP_REQUEST;
145 return p;
148 static inline void *alloc_smp_resp(int size)
150 return kzalloc(size, GFP_KERNEL);
153 /* ---------- Expander configuration ---------- */
155 static void sas_set_ex_phy(struct domain_device *dev, int phy_id,
156 void *disc_resp)
158 struct expander_device *ex = &dev->ex_dev;
159 struct ex_phy *phy = &ex->ex_phy[phy_id];
160 struct smp_resp *resp = disc_resp;
161 struct discover_resp *dr = &resp->disc;
162 struct sas_rphy *rphy = dev->rphy;
163 int rediscover = (phy->phy != NULL);
165 if (!rediscover) {
166 phy->phy = sas_phy_alloc(&rphy->dev, phy_id);
168 /* FIXME: error_handling */
169 BUG_ON(!phy->phy);
172 switch (resp->result) {
173 case SMP_RESP_PHY_VACANT:
174 phy->phy_state = PHY_VACANT;
175 return;
176 default:
177 phy->phy_state = PHY_NOT_PRESENT;
178 return;
179 case SMP_RESP_FUNC_ACC:
180 phy->phy_state = PHY_EMPTY; /* do not know yet */
181 break;
184 phy->phy_id = phy_id;
185 phy->attached_dev_type = dr->attached_dev_type;
186 phy->linkrate = dr->linkrate;
187 phy->attached_sata_host = dr->attached_sata_host;
188 phy->attached_sata_dev = dr->attached_sata_dev;
189 phy->attached_sata_ps = dr->attached_sata_ps;
190 phy->attached_iproto = dr->iproto << 1;
191 phy->attached_tproto = dr->tproto << 1;
192 memcpy(phy->attached_sas_addr, dr->attached_sas_addr, SAS_ADDR_SIZE);
193 phy->attached_phy_id = dr->attached_phy_id;
194 phy->phy_change_count = dr->change_count;
195 phy->routing_attr = dr->routing_attr;
196 phy->virtual = dr->virtual;
197 phy->last_da_index = -1;
199 phy->phy->identify.initiator_port_protocols = phy->attached_iproto;
200 phy->phy->identify.target_port_protocols = phy->attached_tproto;
201 phy->phy->identify.phy_identifier = phy_id;
202 phy->phy->minimum_linkrate_hw = dr->hmin_linkrate;
203 phy->phy->maximum_linkrate_hw = dr->hmax_linkrate;
204 phy->phy->minimum_linkrate = dr->pmin_linkrate;
205 phy->phy->maximum_linkrate = dr->pmax_linkrate;
206 phy->phy->negotiated_linkrate = phy->linkrate;
208 if (!rediscover)
209 sas_phy_add(phy->phy);
211 SAS_DPRINTK("ex %016llx phy%02d:%c attached: %016llx\n",
212 SAS_ADDR(dev->sas_addr), phy->phy_id,
213 phy->routing_attr == TABLE_ROUTING ? 'T' :
214 phy->routing_attr == DIRECT_ROUTING ? 'D' :
215 phy->routing_attr == SUBTRACTIVE_ROUTING ? 'S' : '?',
216 SAS_ADDR(phy->attached_sas_addr));
218 return;
221 #define DISCOVER_REQ_SIZE 16
222 #define DISCOVER_RESP_SIZE 56
224 static int sas_ex_phy_discover_helper(struct domain_device *dev, u8 *disc_req,
225 u8 *disc_resp, int single)
227 int i, res;
229 disc_req[9] = single;
230 for (i = 1 ; i < 3; i++) {
231 struct discover_resp *dr;
233 res = smp_execute_task(dev, disc_req, DISCOVER_REQ_SIZE,
234 disc_resp, DISCOVER_RESP_SIZE);
235 if (res)
236 return res;
237 /* This is detecting a failure to transmit inital
238 * dev to host FIS as described in section G.5 of
239 * sas-2 r 04b */
240 dr = &((struct smp_resp *)disc_resp)->disc;
241 if (!(dr->attached_dev_type == 0 &&
242 dr->attached_sata_dev))
243 break;
244 /* In order to generate the dev to host FIS, we
245 * send a link reset to the expander port */
246 sas_smp_phy_control(dev, single, PHY_FUNC_LINK_RESET, NULL);
247 /* Wait for the reset to trigger the negotiation */
248 msleep(500);
250 sas_set_ex_phy(dev, single, disc_resp);
251 return 0;
254 static int sas_ex_phy_discover(struct domain_device *dev, int single)
256 struct expander_device *ex = &dev->ex_dev;
257 int res = 0;
258 u8 *disc_req;
259 u8 *disc_resp;
261 disc_req = alloc_smp_req(DISCOVER_REQ_SIZE);
262 if (!disc_req)
263 return -ENOMEM;
265 disc_resp = alloc_smp_req(DISCOVER_RESP_SIZE);
266 if (!disc_resp) {
267 kfree(disc_req);
268 return -ENOMEM;
271 disc_req[1] = SMP_DISCOVER;
273 if (0 <= single && single < ex->num_phys) {
274 res = sas_ex_phy_discover_helper(dev, disc_req, disc_resp, single);
275 } else {
276 int i;
278 for (i = 0; i < ex->num_phys; i++) {
279 res = sas_ex_phy_discover_helper(dev, disc_req,
280 disc_resp, i);
281 if (res)
282 goto out_err;
285 out_err:
286 kfree(disc_resp);
287 kfree(disc_req);
288 return res;
291 static int sas_expander_discover(struct domain_device *dev)
293 struct expander_device *ex = &dev->ex_dev;
294 int res = -ENOMEM;
296 ex->ex_phy = kzalloc(sizeof(*ex->ex_phy)*ex->num_phys, GFP_KERNEL);
297 if (!ex->ex_phy)
298 return -ENOMEM;
300 res = sas_ex_phy_discover(dev, -1);
301 if (res)
302 goto out_err;
304 return 0;
305 out_err:
306 kfree(ex->ex_phy);
307 ex->ex_phy = NULL;
308 return res;
311 #define MAX_EXPANDER_PHYS 128
313 static void ex_assign_report_general(struct domain_device *dev,
314 struct smp_resp *resp)
316 struct report_general_resp *rg = &resp->rg;
318 dev->ex_dev.ex_change_count = be16_to_cpu(rg->change_count);
319 dev->ex_dev.max_route_indexes = be16_to_cpu(rg->route_indexes);
320 dev->ex_dev.num_phys = min(rg->num_phys, (u8)MAX_EXPANDER_PHYS);
321 dev->ex_dev.conf_route_table = rg->conf_route_table;
322 dev->ex_dev.configuring = rg->configuring;
323 memcpy(dev->ex_dev.enclosure_logical_id, rg->enclosure_logical_id, 8);
326 #define RG_REQ_SIZE 8
327 #define RG_RESP_SIZE 32
329 static int sas_ex_general(struct domain_device *dev)
331 u8 *rg_req;
332 struct smp_resp *rg_resp;
333 int res;
334 int i;
336 rg_req = alloc_smp_req(RG_REQ_SIZE);
337 if (!rg_req)
338 return -ENOMEM;
340 rg_resp = alloc_smp_resp(RG_RESP_SIZE);
341 if (!rg_resp) {
342 kfree(rg_req);
343 return -ENOMEM;
346 rg_req[1] = SMP_REPORT_GENERAL;
348 for (i = 0; i < 5; i++) {
349 res = smp_execute_task(dev, rg_req, RG_REQ_SIZE, rg_resp,
350 RG_RESP_SIZE);
352 if (res) {
353 SAS_DPRINTK("RG to ex %016llx failed:0x%x\n",
354 SAS_ADDR(dev->sas_addr), res);
355 goto out;
356 } else if (rg_resp->result != SMP_RESP_FUNC_ACC) {
357 SAS_DPRINTK("RG:ex %016llx returned SMP result:0x%x\n",
358 SAS_ADDR(dev->sas_addr), rg_resp->result);
359 res = rg_resp->result;
360 goto out;
363 ex_assign_report_general(dev, rg_resp);
365 if (dev->ex_dev.configuring) {
366 SAS_DPRINTK("RG: ex %llx self-configuring...\n",
367 SAS_ADDR(dev->sas_addr));
368 schedule_timeout_interruptible(5*HZ);
369 } else
370 break;
372 out:
373 kfree(rg_req);
374 kfree(rg_resp);
375 return res;
378 static void ex_assign_manuf_info(struct domain_device *dev, void
379 *_mi_resp)
381 u8 *mi_resp = _mi_resp;
382 struct sas_rphy *rphy = dev->rphy;
383 struct sas_expander_device *edev = rphy_to_expander_device(rphy);
385 memcpy(edev->vendor_id, mi_resp + 12, SAS_EXPANDER_VENDOR_ID_LEN);
386 memcpy(edev->product_id, mi_resp + 20, SAS_EXPANDER_PRODUCT_ID_LEN);
387 memcpy(edev->product_rev, mi_resp + 36,
388 SAS_EXPANDER_PRODUCT_REV_LEN);
390 if (mi_resp[8] & 1) {
391 memcpy(edev->component_vendor_id, mi_resp + 40,
392 SAS_EXPANDER_COMPONENT_VENDOR_ID_LEN);
393 edev->component_id = mi_resp[48] << 8 | mi_resp[49];
394 edev->component_revision_id = mi_resp[50];
398 #define MI_REQ_SIZE 8
399 #define MI_RESP_SIZE 64
401 static int sas_ex_manuf_info(struct domain_device *dev)
403 u8 *mi_req;
404 u8 *mi_resp;
405 int res;
407 mi_req = alloc_smp_req(MI_REQ_SIZE);
408 if (!mi_req)
409 return -ENOMEM;
411 mi_resp = alloc_smp_resp(MI_RESP_SIZE);
412 if (!mi_resp) {
413 kfree(mi_req);
414 return -ENOMEM;
417 mi_req[1] = SMP_REPORT_MANUF_INFO;
419 res = smp_execute_task(dev, mi_req, MI_REQ_SIZE, mi_resp,MI_RESP_SIZE);
420 if (res) {
421 SAS_DPRINTK("MI: ex %016llx failed:0x%x\n",
422 SAS_ADDR(dev->sas_addr), res);
423 goto out;
424 } else if (mi_resp[2] != SMP_RESP_FUNC_ACC) {
425 SAS_DPRINTK("MI ex %016llx returned SMP result:0x%x\n",
426 SAS_ADDR(dev->sas_addr), mi_resp[2]);
427 goto out;
430 ex_assign_manuf_info(dev, mi_resp);
431 out:
432 kfree(mi_req);
433 kfree(mi_resp);
434 return res;
437 #define PC_REQ_SIZE 44
438 #define PC_RESP_SIZE 8
440 int sas_smp_phy_control(struct domain_device *dev, int phy_id,
441 enum phy_func phy_func,
442 struct sas_phy_linkrates *rates)
444 u8 *pc_req;
445 u8 *pc_resp;
446 int res;
448 pc_req = alloc_smp_req(PC_REQ_SIZE);
449 if (!pc_req)
450 return -ENOMEM;
452 pc_resp = alloc_smp_resp(PC_RESP_SIZE);
453 if (!pc_resp) {
454 kfree(pc_req);
455 return -ENOMEM;
458 pc_req[1] = SMP_PHY_CONTROL;
459 pc_req[9] = phy_id;
460 pc_req[10]= phy_func;
461 if (rates) {
462 pc_req[32] = rates->minimum_linkrate << 4;
463 pc_req[33] = rates->maximum_linkrate << 4;
466 res = smp_execute_task(dev, pc_req, PC_REQ_SIZE, pc_resp,PC_RESP_SIZE);
468 kfree(pc_resp);
469 kfree(pc_req);
470 return res;
473 static void sas_ex_disable_phy(struct domain_device *dev, int phy_id)
475 struct expander_device *ex = &dev->ex_dev;
476 struct ex_phy *phy = &ex->ex_phy[phy_id];
478 sas_smp_phy_control(dev, phy_id, PHY_FUNC_DISABLE, NULL);
479 phy->linkrate = SAS_PHY_DISABLED;
482 static void sas_ex_disable_port(struct domain_device *dev, u8 *sas_addr)
484 struct expander_device *ex = &dev->ex_dev;
485 int i;
487 for (i = 0; i < ex->num_phys; i++) {
488 struct ex_phy *phy = &ex->ex_phy[i];
490 if (phy->phy_state == PHY_VACANT ||
491 phy->phy_state == PHY_NOT_PRESENT)
492 continue;
494 if (SAS_ADDR(phy->attached_sas_addr) == SAS_ADDR(sas_addr))
495 sas_ex_disable_phy(dev, i);
499 static int sas_dev_present_in_domain(struct asd_sas_port *port,
500 u8 *sas_addr)
502 struct domain_device *dev;
504 if (SAS_ADDR(port->sas_addr) == SAS_ADDR(sas_addr))
505 return 1;
506 list_for_each_entry(dev, &port->dev_list, dev_list_node) {
507 if (SAS_ADDR(dev->sas_addr) == SAS_ADDR(sas_addr))
508 return 1;
510 return 0;
513 #define RPEL_REQ_SIZE 16
514 #define RPEL_RESP_SIZE 32
515 int sas_smp_get_phy_events(struct sas_phy *phy)
517 int res;
518 struct sas_rphy *rphy = dev_to_rphy(phy->dev.parent);
519 struct domain_device *dev = sas_find_dev_by_rphy(rphy);
520 u8 *req = alloc_smp_req(RPEL_REQ_SIZE);
521 u8 *resp = kzalloc(RPEL_RESP_SIZE, GFP_KERNEL);
523 if (!resp)
524 return -ENOMEM;
526 req[1] = SMP_REPORT_PHY_ERR_LOG;
527 req[9] = phy->number;
529 res = smp_execute_task(dev, req, RPEL_REQ_SIZE,
530 resp, RPEL_RESP_SIZE);
532 if (!res)
533 goto out;
535 phy->invalid_dword_count = scsi_to_u32(&resp[12]);
536 phy->running_disparity_error_count = scsi_to_u32(&resp[16]);
537 phy->loss_of_dword_sync_count = scsi_to_u32(&resp[20]);
538 phy->phy_reset_problem_count = scsi_to_u32(&resp[24]);
540 out:
541 kfree(resp);
542 return res;
546 #define RPS_REQ_SIZE 16
547 #define RPS_RESP_SIZE 60
549 static int sas_get_report_phy_sata(struct domain_device *dev,
550 int phy_id,
551 struct smp_resp *rps_resp)
553 int res;
554 u8 *rps_req = alloc_smp_req(RPS_REQ_SIZE);
555 u8 *resp = (u8 *)rps_resp;
557 if (!rps_req)
558 return -ENOMEM;
560 rps_req[1] = SMP_REPORT_PHY_SATA;
561 rps_req[9] = phy_id;
563 res = smp_execute_task(dev, rps_req, RPS_REQ_SIZE,
564 rps_resp, RPS_RESP_SIZE);
566 /* 0x34 is the FIS type for the D2H fis. There's a potential
567 * standards cockup here. sas-2 explicitly specifies the FIS
568 * should be encoded so that FIS type is in resp[24].
569 * However, some expanders endian reverse this. Undo the
570 * reversal here */
571 if (!res && resp[27] == 0x34 && resp[24] != 0x34) {
572 int i;
574 for (i = 0; i < 5; i++) {
575 int j = 24 + (i*4);
576 u8 a, b;
577 a = resp[j + 0];
578 b = resp[j + 1];
579 resp[j + 0] = resp[j + 3];
580 resp[j + 1] = resp[j + 2];
581 resp[j + 2] = b;
582 resp[j + 3] = a;
586 kfree(rps_req);
587 return res;
590 static void sas_ex_get_linkrate(struct domain_device *parent,
591 struct domain_device *child,
592 struct ex_phy *parent_phy)
594 struct expander_device *parent_ex = &parent->ex_dev;
595 struct sas_port *port;
596 int i;
598 child->pathways = 0;
600 port = parent_phy->port;
602 for (i = 0; i < parent_ex->num_phys; i++) {
603 struct ex_phy *phy = &parent_ex->ex_phy[i];
605 if (phy->phy_state == PHY_VACANT ||
606 phy->phy_state == PHY_NOT_PRESENT)
607 continue;
609 if (SAS_ADDR(phy->attached_sas_addr) ==
610 SAS_ADDR(child->sas_addr)) {
612 child->min_linkrate = min(parent->min_linkrate,
613 phy->linkrate);
614 child->max_linkrate = max(parent->max_linkrate,
615 phy->linkrate);
616 child->pathways++;
617 sas_port_add_phy(port, phy->phy);
620 child->linkrate = min(parent_phy->linkrate, child->max_linkrate);
621 child->pathways = min(child->pathways, parent->pathways);
624 static struct domain_device *sas_ex_discover_end_dev(
625 struct domain_device *parent, int phy_id)
627 struct expander_device *parent_ex = &parent->ex_dev;
628 struct ex_phy *phy = &parent_ex->ex_phy[phy_id];
629 struct domain_device *child = NULL;
630 struct sas_rphy *rphy;
631 int res;
633 if (phy->attached_sata_host || phy->attached_sata_ps)
634 return NULL;
636 child = kzalloc(sizeof(*child), GFP_KERNEL);
637 if (!child)
638 return NULL;
640 child->parent = parent;
641 child->port = parent->port;
642 child->iproto = phy->attached_iproto;
643 memcpy(child->sas_addr, phy->attached_sas_addr, SAS_ADDR_SIZE);
644 sas_hash_addr(child->hashed_sas_addr, child->sas_addr);
645 if (!phy->port) {
646 phy->port = sas_port_alloc(&parent->rphy->dev, phy_id);
647 if (unlikely(!phy->port))
648 goto out_err;
649 if (unlikely(sas_port_add(phy->port) != 0)) {
650 sas_port_free(phy->port);
651 goto out_err;
654 sas_ex_get_linkrate(parent, child, phy);
656 if ((phy->attached_tproto & SAS_PROTO_STP) || phy->attached_sata_dev) {
657 child->dev_type = SATA_DEV;
658 if (phy->attached_tproto & SAS_PROTO_STP)
659 child->tproto = phy->attached_tproto;
660 if (phy->attached_sata_dev)
661 child->tproto |= SATA_DEV;
662 res = sas_get_report_phy_sata(parent, phy_id,
663 &child->sata_dev.rps_resp);
664 if (res) {
665 SAS_DPRINTK("report phy sata to %016llx:0x%x returned "
666 "0x%x\n", SAS_ADDR(parent->sas_addr),
667 phy_id, res);
668 goto out_free;
670 memcpy(child->frame_rcvd, &child->sata_dev.rps_resp.rps.fis,
671 sizeof(struct dev_to_host_fis));
673 rphy = sas_end_device_alloc(phy->port);
674 if (unlikely(!rphy))
675 goto out_free;
677 sas_init_dev(child);
679 child->rphy = rphy;
681 spin_lock_irq(&parent->port->dev_list_lock);
682 list_add_tail(&child->dev_list_node, &parent->port->dev_list);
683 spin_unlock_irq(&parent->port->dev_list_lock);
685 res = sas_discover_sata(child);
686 if (res) {
687 SAS_DPRINTK("sas_discover_sata() for device %16llx at "
688 "%016llx:0x%x returned 0x%x\n",
689 SAS_ADDR(child->sas_addr),
690 SAS_ADDR(parent->sas_addr), phy_id, res);
691 goto out_list_del;
693 } else if (phy->attached_tproto & SAS_PROTO_SSP) {
694 child->dev_type = SAS_END_DEV;
695 rphy = sas_end_device_alloc(phy->port);
696 /* FIXME: error handling */
697 if (unlikely(!rphy))
698 goto out_free;
699 child->tproto = phy->attached_tproto;
700 sas_init_dev(child);
702 child->rphy = rphy;
703 sas_fill_in_rphy(child, rphy);
705 spin_lock_irq(&parent->port->dev_list_lock);
706 list_add_tail(&child->dev_list_node, &parent->port->dev_list);
707 spin_unlock_irq(&parent->port->dev_list_lock);
709 res = sas_discover_end_dev(child);
710 if (res) {
711 SAS_DPRINTK("sas_discover_end_dev() for device %16llx "
712 "at %016llx:0x%x returned 0x%x\n",
713 SAS_ADDR(child->sas_addr),
714 SAS_ADDR(parent->sas_addr), phy_id, res);
715 goto out_list_del;
717 } else {
718 SAS_DPRINTK("target proto 0x%x at %016llx:0x%x not handled\n",
719 phy->attached_tproto, SAS_ADDR(parent->sas_addr),
720 phy_id);
723 list_add_tail(&child->siblings, &parent_ex->children);
724 return child;
726 out_list_del:
727 sas_rphy_free(child->rphy);
728 child->rphy = NULL;
729 list_del(&child->dev_list_node);
730 out_free:
731 sas_port_delete(phy->port);
732 out_err:
733 phy->port = NULL;
734 kfree(child);
735 return NULL;
738 /* See if this phy is part of a wide port */
739 static int sas_ex_join_wide_port(struct domain_device *parent, int phy_id)
741 struct ex_phy *phy = &parent->ex_dev.ex_phy[phy_id];
742 int i;
744 for (i = 0; i < parent->ex_dev.num_phys; i++) {
745 struct ex_phy *ephy = &parent->ex_dev.ex_phy[i];
747 if (ephy == phy)
748 continue;
750 if (!memcmp(phy->attached_sas_addr, ephy->attached_sas_addr,
751 SAS_ADDR_SIZE) && ephy->port) {
752 sas_port_add_phy(ephy->port, phy->phy);
753 phy->phy_state = PHY_DEVICE_DISCOVERED;
754 return 0;
758 return -ENODEV;
761 static struct domain_device *sas_ex_discover_expander(
762 struct domain_device *parent, int phy_id)
764 struct sas_expander_device *parent_ex = rphy_to_expander_device(parent->rphy);
765 struct ex_phy *phy = &parent->ex_dev.ex_phy[phy_id];
766 struct domain_device *child = NULL;
767 struct sas_rphy *rphy;
768 struct sas_expander_device *edev;
769 struct asd_sas_port *port;
770 int res;
772 if (phy->routing_attr == DIRECT_ROUTING) {
773 SAS_DPRINTK("ex %016llx:0x%x:D <--> ex %016llx:0x%x is not "
774 "allowed\n",
775 SAS_ADDR(parent->sas_addr), phy_id,
776 SAS_ADDR(phy->attached_sas_addr),
777 phy->attached_phy_id);
778 return NULL;
780 child = kzalloc(sizeof(*child), GFP_KERNEL);
781 if (!child)
782 return NULL;
784 phy->port = sas_port_alloc(&parent->rphy->dev, phy_id);
785 /* FIXME: better error handling */
786 BUG_ON(sas_port_add(phy->port) != 0);
789 switch (phy->attached_dev_type) {
790 case EDGE_DEV:
791 rphy = sas_expander_alloc(phy->port,
792 SAS_EDGE_EXPANDER_DEVICE);
793 break;
794 case FANOUT_DEV:
795 rphy = sas_expander_alloc(phy->port,
796 SAS_FANOUT_EXPANDER_DEVICE);
797 break;
798 default:
799 rphy = NULL; /* shut gcc up */
800 BUG();
802 port = parent->port;
803 child->rphy = rphy;
804 edev = rphy_to_expander_device(rphy);
805 child->dev_type = phy->attached_dev_type;
806 child->parent = parent;
807 child->port = port;
808 child->iproto = phy->attached_iproto;
809 child->tproto = phy->attached_tproto;
810 memcpy(child->sas_addr, phy->attached_sas_addr, SAS_ADDR_SIZE);
811 sas_hash_addr(child->hashed_sas_addr, child->sas_addr);
812 sas_ex_get_linkrate(parent, child, phy);
813 edev->level = parent_ex->level + 1;
814 parent->port->disc.max_level = max(parent->port->disc.max_level,
815 edev->level);
816 sas_init_dev(child);
817 sas_fill_in_rphy(child, rphy);
818 sas_rphy_add(rphy);
820 spin_lock_irq(&parent->port->dev_list_lock);
821 list_add_tail(&child->dev_list_node, &parent->port->dev_list);
822 spin_unlock_irq(&parent->port->dev_list_lock);
824 res = sas_discover_expander(child);
825 if (res) {
826 kfree(child);
827 return NULL;
829 list_add_tail(&child->siblings, &parent->ex_dev.children);
830 return child;
833 static int sas_ex_discover_dev(struct domain_device *dev, int phy_id)
835 struct expander_device *ex = &dev->ex_dev;
836 struct ex_phy *ex_phy = &ex->ex_phy[phy_id];
837 struct domain_device *child = NULL;
838 int res = 0;
840 /* Phy state */
841 if (ex_phy->linkrate == SAS_SATA_SPINUP_HOLD) {
842 if (!sas_smp_phy_control(dev, phy_id, PHY_FUNC_LINK_RESET, NULL))
843 res = sas_ex_phy_discover(dev, phy_id);
844 if (res)
845 return res;
848 /* Parent and domain coherency */
849 if (!dev->parent && (SAS_ADDR(ex_phy->attached_sas_addr) ==
850 SAS_ADDR(dev->port->sas_addr))) {
851 sas_add_parent_port(dev, phy_id);
852 return 0;
854 if (dev->parent && (SAS_ADDR(ex_phy->attached_sas_addr) ==
855 SAS_ADDR(dev->parent->sas_addr))) {
856 sas_add_parent_port(dev, phy_id);
857 if (ex_phy->routing_attr == TABLE_ROUTING)
858 sas_configure_phy(dev, phy_id, dev->port->sas_addr, 1);
859 return 0;
862 if (sas_dev_present_in_domain(dev->port, ex_phy->attached_sas_addr))
863 sas_ex_disable_port(dev, ex_phy->attached_sas_addr);
865 if (ex_phy->attached_dev_type == NO_DEVICE) {
866 if (ex_phy->routing_attr == DIRECT_ROUTING) {
867 memset(ex_phy->attached_sas_addr, 0, SAS_ADDR_SIZE);
868 sas_configure_routing(dev, ex_phy->attached_sas_addr);
870 return 0;
871 } else if (ex_phy->linkrate == SAS_LINK_RATE_UNKNOWN)
872 return 0;
874 if (ex_phy->attached_dev_type != SAS_END_DEV &&
875 ex_phy->attached_dev_type != FANOUT_DEV &&
876 ex_phy->attached_dev_type != EDGE_DEV) {
877 SAS_DPRINTK("unknown device type(0x%x) attached to ex %016llx "
878 "phy 0x%x\n", ex_phy->attached_dev_type,
879 SAS_ADDR(dev->sas_addr),
880 phy_id);
881 return 0;
884 res = sas_configure_routing(dev, ex_phy->attached_sas_addr);
885 if (res) {
886 SAS_DPRINTK("configure routing for dev %016llx "
887 "reported 0x%x. Forgotten\n",
888 SAS_ADDR(ex_phy->attached_sas_addr), res);
889 sas_disable_routing(dev, ex_phy->attached_sas_addr);
890 return res;
893 res = sas_ex_join_wide_port(dev, phy_id);
894 if (!res) {
895 SAS_DPRINTK("Attaching ex phy%d to wide port %016llx\n",
896 phy_id, SAS_ADDR(ex_phy->attached_sas_addr));
897 return res;
900 switch (ex_phy->attached_dev_type) {
901 case SAS_END_DEV:
902 child = sas_ex_discover_end_dev(dev, phy_id);
903 break;
904 case FANOUT_DEV:
905 if (SAS_ADDR(dev->port->disc.fanout_sas_addr)) {
906 SAS_DPRINTK("second fanout expander %016llx phy 0x%x "
907 "attached to ex %016llx phy 0x%x\n",
908 SAS_ADDR(ex_phy->attached_sas_addr),
909 ex_phy->attached_phy_id,
910 SAS_ADDR(dev->sas_addr),
911 phy_id);
912 sas_ex_disable_phy(dev, phy_id);
913 break;
914 } else
915 memcpy(dev->port->disc.fanout_sas_addr,
916 ex_phy->attached_sas_addr, SAS_ADDR_SIZE);
917 /* fallthrough */
918 case EDGE_DEV:
919 child = sas_ex_discover_expander(dev, phy_id);
920 break;
921 default:
922 break;
925 if (child) {
926 int i;
928 for (i = 0; i < ex->num_phys; i++) {
929 if (ex->ex_phy[i].phy_state == PHY_VACANT ||
930 ex->ex_phy[i].phy_state == PHY_NOT_PRESENT)
931 continue;
933 if (SAS_ADDR(ex->ex_phy[i].attached_sas_addr) ==
934 SAS_ADDR(child->sas_addr))
935 ex->ex_phy[i].phy_state= PHY_DEVICE_DISCOVERED;
939 return res;
942 static int sas_find_sub_addr(struct domain_device *dev, u8 *sub_addr)
944 struct expander_device *ex = &dev->ex_dev;
945 int i;
947 for (i = 0; i < ex->num_phys; i++) {
948 struct ex_phy *phy = &ex->ex_phy[i];
950 if (phy->phy_state == PHY_VACANT ||
951 phy->phy_state == PHY_NOT_PRESENT)
952 continue;
954 if ((phy->attached_dev_type == EDGE_DEV ||
955 phy->attached_dev_type == FANOUT_DEV) &&
956 phy->routing_attr == SUBTRACTIVE_ROUTING) {
958 memcpy(sub_addr, phy->attached_sas_addr,SAS_ADDR_SIZE);
960 return 1;
963 return 0;
966 static int sas_check_level_subtractive_boundary(struct domain_device *dev)
968 struct expander_device *ex = &dev->ex_dev;
969 struct domain_device *child;
970 u8 sub_addr[8] = {0, };
972 list_for_each_entry(child, &ex->children, siblings) {
973 if (child->dev_type != EDGE_DEV &&
974 child->dev_type != FANOUT_DEV)
975 continue;
976 if (sub_addr[0] == 0) {
977 sas_find_sub_addr(child, sub_addr);
978 continue;
979 } else {
980 u8 s2[8];
982 if (sas_find_sub_addr(child, s2) &&
983 (SAS_ADDR(sub_addr) != SAS_ADDR(s2))) {
985 SAS_DPRINTK("ex %016llx->%016llx-?->%016llx "
986 "diverges from subtractive "
987 "boundary %016llx\n",
988 SAS_ADDR(dev->sas_addr),
989 SAS_ADDR(child->sas_addr),
990 SAS_ADDR(s2),
991 SAS_ADDR(sub_addr));
993 sas_ex_disable_port(child, s2);
997 return 0;
1000 * sas_ex_discover_devices -- discover devices attached to this expander
1001 * dev: pointer to the expander domain device
1002 * single: if you want to do a single phy, else set to -1;
1004 * Configure this expander for use with its devices and register the
1005 * devices of this expander.
1007 static int sas_ex_discover_devices(struct domain_device *dev, int single)
1009 struct expander_device *ex = &dev->ex_dev;
1010 int i = 0, end = ex->num_phys;
1011 int res = 0;
1013 if (0 <= single && single < end) {
1014 i = single;
1015 end = i+1;
1018 for ( ; i < end; i++) {
1019 struct ex_phy *ex_phy = &ex->ex_phy[i];
1021 if (ex_phy->phy_state == PHY_VACANT ||
1022 ex_phy->phy_state == PHY_NOT_PRESENT ||
1023 ex_phy->phy_state == PHY_DEVICE_DISCOVERED)
1024 continue;
1026 switch (ex_phy->linkrate) {
1027 case SAS_PHY_DISABLED:
1028 case SAS_PHY_RESET_PROBLEM:
1029 case SAS_SATA_PORT_SELECTOR:
1030 continue;
1031 default:
1032 res = sas_ex_discover_dev(dev, i);
1033 if (res)
1034 break;
1035 continue;
1039 if (!res)
1040 sas_check_level_subtractive_boundary(dev);
1042 return res;
1045 static int sas_check_ex_subtractive_boundary(struct domain_device *dev)
1047 struct expander_device *ex = &dev->ex_dev;
1048 int i;
1049 u8 *sub_sas_addr = NULL;
1051 if (dev->dev_type != EDGE_DEV)
1052 return 0;
1054 for (i = 0; i < ex->num_phys; i++) {
1055 struct ex_phy *phy = &ex->ex_phy[i];
1057 if (phy->phy_state == PHY_VACANT ||
1058 phy->phy_state == PHY_NOT_PRESENT)
1059 continue;
1061 if ((phy->attached_dev_type == FANOUT_DEV ||
1062 phy->attached_dev_type == EDGE_DEV) &&
1063 phy->routing_attr == SUBTRACTIVE_ROUTING) {
1065 if (!sub_sas_addr)
1066 sub_sas_addr = &phy->attached_sas_addr[0];
1067 else if (SAS_ADDR(sub_sas_addr) !=
1068 SAS_ADDR(phy->attached_sas_addr)) {
1070 SAS_DPRINTK("ex %016llx phy 0x%x "
1071 "diverges(%016llx) on subtractive "
1072 "boundary(%016llx). Disabled\n",
1073 SAS_ADDR(dev->sas_addr), i,
1074 SAS_ADDR(phy->attached_sas_addr),
1075 SAS_ADDR(sub_sas_addr));
1076 sas_ex_disable_phy(dev, i);
1080 return 0;
1083 static void sas_print_parent_topology_bug(struct domain_device *child,
1084 struct ex_phy *parent_phy,
1085 struct ex_phy *child_phy)
1087 static const char ra_char[] = {
1088 [DIRECT_ROUTING] = 'D',
1089 [SUBTRACTIVE_ROUTING] = 'S',
1090 [TABLE_ROUTING] = 'T',
1092 static const char *ex_type[] = {
1093 [EDGE_DEV] = "edge",
1094 [FANOUT_DEV] = "fanout",
1096 struct domain_device *parent = child->parent;
1098 sas_printk("%s ex %016llx phy 0x%x <--> %s ex %016llx phy 0x%x "
1099 "has %c:%c routing link!\n",
1101 ex_type[parent->dev_type],
1102 SAS_ADDR(parent->sas_addr),
1103 parent_phy->phy_id,
1105 ex_type[child->dev_type],
1106 SAS_ADDR(child->sas_addr),
1107 child_phy->phy_id,
1109 ra_char[parent_phy->routing_attr],
1110 ra_char[child_phy->routing_attr]);
1113 static int sas_check_eeds(struct domain_device *child,
1114 struct ex_phy *parent_phy,
1115 struct ex_phy *child_phy)
1117 int res = 0;
1118 struct domain_device *parent = child->parent;
1120 if (SAS_ADDR(parent->port->disc.fanout_sas_addr) != 0) {
1121 res = -ENODEV;
1122 SAS_DPRINTK("edge ex %016llx phy S:0x%x <--> edge ex %016llx "
1123 "phy S:0x%x, while there is a fanout ex %016llx\n",
1124 SAS_ADDR(parent->sas_addr),
1125 parent_phy->phy_id,
1126 SAS_ADDR(child->sas_addr),
1127 child_phy->phy_id,
1128 SAS_ADDR(parent->port->disc.fanout_sas_addr));
1129 } else if (SAS_ADDR(parent->port->disc.eeds_a) == 0) {
1130 memcpy(parent->port->disc.eeds_a, parent->sas_addr,
1131 SAS_ADDR_SIZE);
1132 memcpy(parent->port->disc.eeds_b, child->sas_addr,
1133 SAS_ADDR_SIZE);
1134 } else if (((SAS_ADDR(parent->port->disc.eeds_a) ==
1135 SAS_ADDR(parent->sas_addr)) ||
1136 (SAS_ADDR(parent->port->disc.eeds_a) ==
1137 SAS_ADDR(child->sas_addr)))
1139 ((SAS_ADDR(parent->port->disc.eeds_b) ==
1140 SAS_ADDR(parent->sas_addr)) ||
1141 (SAS_ADDR(parent->port->disc.eeds_b) ==
1142 SAS_ADDR(child->sas_addr))))
1144 else {
1145 res = -ENODEV;
1146 SAS_DPRINTK("edge ex %016llx phy 0x%x <--> edge ex %016llx "
1147 "phy 0x%x link forms a third EEDS!\n",
1148 SAS_ADDR(parent->sas_addr),
1149 parent_phy->phy_id,
1150 SAS_ADDR(child->sas_addr),
1151 child_phy->phy_id);
1154 return res;
1157 /* Here we spill over 80 columns. It is intentional.
1159 static int sas_check_parent_topology(struct domain_device *child)
1161 struct expander_device *child_ex = &child->ex_dev;
1162 struct expander_device *parent_ex;
1163 int i;
1164 int res = 0;
1166 if (!child->parent)
1167 return 0;
1169 if (child->parent->dev_type != EDGE_DEV &&
1170 child->parent->dev_type != FANOUT_DEV)
1171 return 0;
1173 parent_ex = &child->parent->ex_dev;
1175 for (i = 0; i < parent_ex->num_phys; i++) {
1176 struct ex_phy *parent_phy = &parent_ex->ex_phy[i];
1177 struct ex_phy *child_phy;
1179 if (parent_phy->phy_state == PHY_VACANT ||
1180 parent_phy->phy_state == PHY_NOT_PRESENT)
1181 continue;
1183 if (SAS_ADDR(parent_phy->attached_sas_addr) != SAS_ADDR(child->sas_addr))
1184 continue;
1186 child_phy = &child_ex->ex_phy[parent_phy->attached_phy_id];
1188 switch (child->parent->dev_type) {
1189 case EDGE_DEV:
1190 if (child->dev_type == FANOUT_DEV) {
1191 if (parent_phy->routing_attr != SUBTRACTIVE_ROUTING ||
1192 child_phy->routing_attr != TABLE_ROUTING) {
1193 sas_print_parent_topology_bug(child, parent_phy, child_phy);
1194 res = -ENODEV;
1196 } else if (parent_phy->routing_attr == SUBTRACTIVE_ROUTING) {
1197 if (child_phy->routing_attr == SUBTRACTIVE_ROUTING) {
1198 res = sas_check_eeds(child, parent_phy, child_phy);
1199 } else if (child_phy->routing_attr != TABLE_ROUTING) {
1200 sas_print_parent_topology_bug(child, parent_phy, child_phy);
1201 res = -ENODEV;
1203 } else if (parent_phy->routing_attr == TABLE_ROUTING &&
1204 child_phy->routing_attr != SUBTRACTIVE_ROUTING) {
1205 sas_print_parent_topology_bug(child, parent_phy, child_phy);
1206 res = -ENODEV;
1208 break;
1209 case FANOUT_DEV:
1210 if (parent_phy->routing_attr != TABLE_ROUTING ||
1211 child_phy->routing_attr != SUBTRACTIVE_ROUTING) {
1212 sas_print_parent_topology_bug(child, parent_phy, child_phy);
1213 res = -ENODEV;
1215 break;
1216 default:
1217 break;
1221 return res;
1224 #define RRI_REQ_SIZE 16
1225 #define RRI_RESP_SIZE 44
1227 static int sas_configure_present(struct domain_device *dev, int phy_id,
1228 u8 *sas_addr, int *index, int *present)
1230 int i, res = 0;
1231 struct expander_device *ex = &dev->ex_dev;
1232 struct ex_phy *phy = &ex->ex_phy[phy_id];
1233 u8 *rri_req;
1234 u8 *rri_resp;
1236 *present = 0;
1237 *index = 0;
1239 rri_req = alloc_smp_req(RRI_REQ_SIZE);
1240 if (!rri_req)
1241 return -ENOMEM;
1243 rri_resp = alloc_smp_resp(RRI_RESP_SIZE);
1244 if (!rri_resp) {
1245 kfree(rri_req);
1246 return -ENOMEM;
1249 rri_req[1] = SMP_REPORT_ROUTE_INFO;
1250 rri_req[9] = phy_id;
1252 for (i = 0; i < ex->max_route_indexes ; i++) {
1253 *(__be16 *)(rri_req+6) = cpu_to_be16(i);
1254 res = smp_execute_task(dev, rri_req, RRI_REQ_SIZE, rri_resp,
1255 RRI_RESP_SIZE);
1256 if (res)
1257 goto out;
1258 res = rri_resp[2];
1259 if (res == SMP_RESP_NO_INDEX) {
1260 SAS_DPRINTK("overflow of indexes: dev %016llx "
1261 "phy 0x%x index 0x%x\n",
1262 SAS_ADDR(dev->sas_addr), phy_id, i);
1263 goto out;
1264 } else if (res != SMP_RESP_FUNC_ACC) {
1265 SAS_DPRINTK("%s: dev %016llx phy 0x%x index 0x%x "
1266 "result 0x%x\n", __FUNCTION__,
1267 SAS_ADDR(dev->sas_addr), phy_id, i, res);
1268 goto out;
1270 if (SAS_ADDR(sas_addr) != 0) {
1271 if (SAS_ADDR(rri_resp+16) == SAS_ADDR(sas_addr)) {
1272 *index = i;
1273 if ((rri_resp[12] & 0x80) == 0x80)
1274 *present = 0;
1275 else
1276 *present = 1;
1277 goto out;
1278 } else if (SAS_ADDR(rri_resp+16) == 0) {
1279 *index = i;
1280 *present = 0;
1281 goto out;
1283 } else if (SAS_ADDR(rri_resp+16) == 0 &&
1284 phy->last_da_index < i) {
1285 phy->last_da_index = i;
1286 *index = i;
1287 *present = 0;
1288 goto out;
1291 res = -1;
1292 out:
1293 kfree(rri_req);
1294 kfree(rri_resp);
1295 return res;
1298 #define CRI_REQ_SIZE 44
1299 #define CRI_RESP_SIZE 8
1301 static int sas_configure_set(struct domain_device *dev, int phy_id,
1302 u8 *sas_addr, int index, int include)
1304 int res;
1305 u8 *cri_req;
1306 u8 *cri_resp;
1308 cri_req = alloc_smp_req(CRI_REQ_SIZE);
1309 if (!cri_req)
1310 return -ENOMEM;
1312 cri_resp = alloc_smp_resp(CRI_RESP_SIZE);
1313 if (!cri_resp) {
1314 kfree(cri_req);
1315 return -ENOMEM;
1318 cri_req[1] = SMP_CONF_ROUTE_INFO;
1319 *(__be16 *)(cri_req+6) = cpu_to_be16(index);
1320 cri_req[9] = phy_id;
1321 if (SAS_ADDR(sas_addr) == 0 || !include)
1322 cri_req[12] |= 0x80;
1323 memcpy(cri_req+16, sas_addr, SAS_ADDR_SIZE);
1325 res = smp_execute_task(dev, cri_req, CRI_REQ_SIZE, cri_resp,
1326 CRI_RESP_SIZE);
1327 if (res)
1328 goto out;
1329 res = cri_resp[2];
1330 if (res == SMP_RESP_NO_INDEX) {
1331 SAS_DPRINTK("overflow of indexes: dev %016llx phy 0x%x "
1332 "index 0x%x\n",
1333 SAS_ADDR(dev->sas_addr), phy_id, index);
1335 out:
1336 kfree(cri_req);
1337 kfree(cri_resp);
1338 return res;
1341 static int sas_configure_phy(struct domain_device *dev, int phy_id,
1342 u8 *sas_addr, int include)
1344 int index;
1345 int present;
1346 int res;
1348 res = sas_configure_present(dev, phy_id, sas_addr, &index, &present);
1349 if (res)
1350 return res;
1351 if (include ^ present)
1352 return sas_configure_set(dev, phy_id, sas_addr, index,include);
1354 return res;
1358 * sas_configure_parent -- configure routing table of parent
1359 * parent: parent expander
1360 * child: child expander
1361 * sas_addr: SAS port identifier of device directly attached to child
1363 static int sas_configure_parent(struct domain_device *parent,
1364 struct domain_device *child,
1365 u8 *sas_addr, int include)
1367 struct expander_device *ex_parent = &parent->ex_dev;
1368 int res = 0;
1369 int i;
1371 if (parent->parent) {
1372 res = sas_configure_parent(parent->parent, parent, sas_addr,
1373 include);
1374 if (res)
1375 return res;
1378 if (ex_parent->conf_route_table == 0) {
1379 SAS_DPRINTK("ex %016llx has self-configuring routing table\n",
1380 SAS_ADDR(parent->sas_addr));
1381 return 0;
1384 for (i = 0; i < ex_parent->num_phys; i++) {
1385 struct ex_phy *phy = &ex_parent->ex_phy[i];
1387 if ((phy->routing_attr == TABLE_ROUTING) &&
1388 (SAS_ADDR(phy->attached_sas_addr) ==
1389 SAS_ADDR(child->sas_addr))) {
1390 res = sas_configure_phy(parent, i, sas_addr, include);
1391 if (res)
1392 return res;
1396 return res;
1400 * sas_configure_routing -- configure routing
1401 * dev: expander device
1402 * sas_addr: port identifier of device directly attached to the expander device
1404 static int sas_configure_routing(struct domain_device *dev, u8 *sas_addr)
1406 if (dev->parent)
1407 return sas_configure_parent(dev->parent, dev, sas_addr, 1);
1408 return 0;
1411 static int sas_disable_routing(struct domain_device *dev, u8 *sas_addr)
1413 if (dev->parent)
1414 return sas_configure_parent(dev->parent, dev, sas_addr, 0);
1415 return 0;
1418 #if 0
1419 #define SMP_BIN_ATTR_NAME "smp_portal"
1421 static void sas_ex_smp_hook(struct domain_device *dev)
1423 struct expander_device *ex_dev = &dev->ex_dev;
1424 struct bin_attribute *bin_attr = &ex_dev->smp_bin_attr;
1426 memset(bin_attr, 0, sizeof(*bin_attr));
1428 bin_attr->attr.name = SMP_BIN_ATTR_NAME;
1429 bin_attr->attr.mode = 0600;
1431 bin_attr->size = 0;
1432 bin_attr->private = NULL;
1433 bin_attr->read = smp_portal_read;
1434 bin_attr->write= smp_portal_write;
1435 bin_attr->mmap = NULL;
1437 ex_dev->smp_portal_pid = -1;
1438 init_MUTEX(&ex_dev->smp_sema);
1440 #endif
1443 * sas_discover_expander -- expander discovery
1444 * @ex: pointer to expander domain device
1446 * See comment in sas_discover_sata().
1448 static int sas_discover_expander(struct domain_device *dev)
1450 int res;
1452 res = sas_notify_lldd_dev_found(dev);
1453 if (res)
1454 return res;
1456 res = sas_ex_general(dev);
1457 if (res)
1458 goto out_err;
1459 res = sas_ex_manuf_info(dev);
1460 if (res)
1461 goto out_err;
1463 res = sas_expander_discover(dev);
1464 if (res) {
1465 SAS_DPRINTK("expander %016llx discovery failed(0x%x)\n",
1466 SAS_ADDR(dev->sas_addr), res);
1467 goto out_err;
1470 sas_check_ex_subtractive_boundary(dev);
1471 res = sas_check_parent_topology(dev);
1472 if (res)
1473 goto out_err;
1474 return 0;
1475 out_err:
1476 sas_notify_lldd_dev_gone(dev);
1477 return res;
1480 static int sas_ex_level_discovery(struct asd_sas_port *port, const int level)
1482 int res = 0;
1483 struct domain_device *dev;
1485 list_for_each_entry(dev, &port->dev_list, dev_list_node) {
1486 if (dev->dev_type == EDGE_DEV ||
1487 dev->dev_type == FANOUT_DEV) {
1488 struct sas_expander_device *ex =
1489 rphy_to_expander_device(dev->rphy);
1491 if (level == ex->level)
1492 res = sas_ex_discover_devices(dev, -1);
1493 else if (level > 0)
1494 res = sas_ex_discover_devices(port->port_dev, -1);
1499 return res;
1502 static int sas_ex_bfs_disc(struct asd_sas_port *port)
1504 int res;
1505 int level;
1507 do {
1508 level = port->disc.max_level;
1509 res = sas_ex_level_discovery(port, level);
1510 mb();
1511 } while (level < port->disc.max_level);
1513 return res;
1516 int sas_discover_root_expander(struct domain_device *dev)
1518 int res;
1519 struct sas_expander_device *ex = rphy_to_expander_device(dev->rphy);
1521 res = sas_rphy_add(dev->rphy);
1522 if (res)
1523 goto out_err;
1525 ex->level = dev->port->disc.max_level; /* 0 */
1526 res = sas_discover_expander(dev);
1527 if (res)
1528 goto out_err2;
1530 sas_ex_bfs_disc(dev->port);
1532 return res;
1534 out_err2:
1535 sas_rphy_remove(dev->rphy);
1536 out_err:
1537 return res;
1540 /* ---------- Domain revalidation ---------- */
1542 static int sas_get_phy_discover(struct domain_device *dev,
1543 int phy_id, struct smp_resp *disc_resp)
1545 int res;
1546 u8 *disc_req;
1548 disc_req = alloc_smp_req(DISCOVER_REQ_SIZE);
1549 if (!disc_req)
1550 return -ENOMEM;
1552 disc_req[1] = SMP_DISCOVER;
1553 disc_req[9] = phy_id;
1555 res = smp_execute_task(dev, disc_req, DISCOVER_REQ_SIZE,
1556 disc_resp, DISCOVER_RESP_SIZE);
1557 if (res)
1558 goto out;
1559 else if (disc_resp->result != SMP_RESP_FUNC_ACC) {
1560 res = disc_resp->result;
1561 goto out;
1563 out:
1564 kfree(disc_req);
1565 return res;
1568 static int sas_get_phy_change_count(struct domain_device *dev,
1569 int phy_id, int *pcc)
1571 int res;
1572 struct smp_resp *disc_resp;
1574 disc_resp = alloc_smp_resp(DISCOVER_RESP_SIZE);
1575 if (!disc_resp)
1576 return -ENOMEM;
1578 res = sas_get_phy_discover(dev, phy_id, disc_resp);
1579 if (!res)
1580 *pcc = disc_resp->disc.change_count;
1582 kfree(disc_resp);
1583 return res;
1586 static int sas_get_phy_attached_sas_addr(struct domain_device *dev,
1587 int phy_id, u8 *attached_sas_addr)
1589 int res;
1590 struct smp_resp *disc_resp;
1591 struct discover_resp *dr;
1593 disc_resp = alloc_smp_resp(DISCOVER_RESP_SIZE);
1594 if (!disc_resp)
1595 return -ENOMEM;
1596 dr = &disc_resp->disc;
1598 res = sas_get_phy_discover(dev, phy_id, disc_resp);
1599 if (!res) {
1600 memcpy(attached_sas_addr,disc_resp->disc.attached_sas_addr,8);
1601 if (dr->attached_dev_type == 0)
1602 memset(attached_sas_addr, 0, 8);
1604 kfree(disc_resp);
1605 return res;
1608 static int sas_find_bcast_phy(struct domain_device *dev, int *phy_id,
1609 int from_phy)
1611 struct expander_device *ex = &dev->ex_dev;
1612 int res = 0;
1613 int i;
1615 for (i = from_phy; i < ex->num_phys; i++) {
1616 int phy_change_count = 0;
1618 res = sas_get_phy_change_count(dev, i, &phy_change_count);
1619 if (res)
1620 goto out;
1621 else if (phy_change_count != ex->ex_phy[i].phy_change_count) {
1622 ex->ex_phy[i].phy_change_count = phy_change_count;
1623 *phy_id = i;
1624 return 0;
1627 out:
1628 return res;
1631 static int sas_get_ex_change_count(struct domain_device *dev, int *ecc)
1633 int res;
1634 u8 *rg_req;
1635 struct smp_resp *rg_resp;
1637 rg_req = alloc_smp_req(RG_REQ_SIZE);
1638 if (!rg_req)
1639 return -ENOMEM;
1641 rg_resp = alloc_smp_resp(RG_RESP_SIZE);
1642 if (!rg_resp) {
1643 kfree(rg_req);
1644 return -ENOMEM;
1647 rg_req[1] = SMP_REPORT_GENERAL;
1649 res = smp_execute_task(dev, rg_req, RG_REQ_SIZE, rg_resp,
1650 RG_RESP_SIZE);
1651 if (res)
1652 goto out;
1653 if (rg_resp->result != SMP_RESP_FUNC_ACC) {
1654 res = rg_resp->result;
1655 goto out;
1658 *ecc = be16_to_cpu(rg_resp->rg.change_count);
1659 out:
1660 kfree(rg_resp);
1661 kfree(rg_req);
1662 return res;
1665 static int sas_find_bcast_dev(struct domain_device *dev,
1666 struct domain_device **src_dev)
1668 struct expander_device *ex = &dev->ex_dev;
1669 int ex_change_count = -1;
1670 int res;
1672 res = sas_get_ex_change_count(dev, &ex_change_count);
1673 if (res)
1674 goto out;
1675 if (ex_change_count != -1 &&
1676 ex_change_count != ex->ex_change_count) {
1677 *src_dev = dev;
1678 ex->ex_change_count = ex_change_count;
1679 } else {
1680 struct domain_device *ch;
1682 list_for_each_entry(ch, &ex->children, siblings) {
1683 if (ch->dev_type == EDGE_DEV ||
1684 ch->dev_type == FANOUT_DEV) {
1685 res = sas_find_bcast_dev(ch, src_dev);
1686 if (src_dev)
1687 return res;
1691 out:
1692 return res;
1695 static void sas_unregister_ex_tree(struct domain_device *dev)
1697 struct expander_device *ex = &dev->ex_dev;
1698 struct domain_device *child, *n;
1700 list_for_each_entry_safe(child, n, &ex->children, siblings) {
1701 if (child->dev_type == EDGE_DEV ||
1702 child->dev_type == FANOUT_DEV)
1703 sas_unregister_ex_tree(child);
1704 else
1705 sas_unregister_dev(child);
1707 sas_unregister_dev(dev);
1710 static void sas_unregister_devs_sas_addr(struct domain_device *parent,
1711 int phy_id)
1713 struct expander_device *ex_dev = &parent->ex_dev;
1714 struct ex_phy *phy = &ex_dev->ex_phy[phy_id];
1715 struct domain_device *child, *n;
1717 list_for_each_entry_safe(child, n, &ex_dev->children, siblings) {
1718 if (SAS_ADDR(child->sas_addr) ==
1719 SAS_ADDR(phy->attached_sas_addr)) {
1720 if (child->dev_type == EDGE_DEV ||
1721 child->dev_type == FANOUT_DEV)
1722 sas_unregister_ex_tree(child);
1723 else
1724 sas_unregister_dev(child);
1725 break;
1728 sas_disable_routing(parent, phy->attached_sas_addr);
1729 memset(phy->attached_sas_addr, 0, SAS_ADDR_SIZE);
1730 sas_port_delete_phy(phy->port, phy->phy);
1731 if (phy->port->num_phys == 0)
1732 sas_port_delete(phy->port);
1733 phy->port = NULL;
1736 static int sas_discover_bfs_by_root_level(struct domain_device *root,
1737 const int level)
1739 struct expander_device *ex_root = &root->ex_dev;
1740 struct domain_device *child;
1741 int res = 0;
1743 list_for_each_entry(child, &ex_root->children, siblings) {
1744 if (child->dev_type == EDGE_DEV ||
1745 child->dev_type == FANOUT_DEV) {
1746 struct sas_expander_device *ex =
1747 rphy_to_expander_device(child->rphy);
1749 if (level > ex->level)
1750 res = sas_discover_bfs_by_root_level(child,
1751 level);
1752 else if (level == ex->level)
1753 res = sas_ex_discover_devices(child, -1);
1756 return res;
1759 static int sas_discover_bfs_by_root(struct domain_device *dev)
1761 int res;
1762 struct sas_expander_device *ex = rphy_to_expander_device(dev->rphy);
1763 int level = ex->level+1;
1765 res = sas_ex_discover_devices(dev, -1);
1766 if (res)
1767 goto out;
1768 do {
1769 res = sas_discover_bfs_by_root_level(dev, level);
1770 mb();
1771 level += 1;
1772 } while (level <= dev->port->disc.max_level);
1773 out:
1774 return res;
1777 static int sas_discover_new(struct domain_device *dev, int phy_id)
1779 struct ex_phy *ex_phy = &dev->ex_dev.ex_phy[phy_id];
1780 struct domain_device *child;
1781 int res;
1783 SAS_DPRINTK("ex %016llx phy%d new device attached\n",
1784 SAS_ADDR(dev->sas_addr), phy_id);
1785 res = sas_ex_phy_discover(dev, phy_id);
1786 if (res)
1787 goto out;
1788 res = sas_ex_discover_devices(dev, phy_id);
1789 if (res)
1790 goto out;
1791 list_for_each_entry(child, &dev->ex_dev.children, siblings) {
1792 if (SAS_ADDR(child->sas_addr) ==
1793 SAS_ADDR(ex_phy->attached_sas_addr)) {
1794 if (child->dev_type == EDGE_DEV ||
1795 child->dev_type == FANOUT_DEV)
1796 res = sas_discover_bfs_by_root(child);
1797 break;
1800 out:
1801 return res;
1804 static int sas_rediscover_dev(struct domain_device *dev, int phy_id)
1806 struct expander_device *ex = &dev->ex_dev;
1807 struct ex_phy *phy = &ex->ex_phy[phy_id];
1808 u8 attached_sas_addr[8];
1809 int res;
1811 res = sas_get_phy_attached_sas_addr(dev, phy_id, attached_sas_addr);
1812 switch (res) {
1813 case SMP_RESP_NO_PHY:
1814 phy->phy_state = PHY_NOT_PRESENT;
1815 sas_unregister_devs_sas_addr(dev, phy_id);
1816 goto out; break;
1817 case SMP_RESP_PHY_VACANT:
1818 phy->phy_state = PHY_VACANT;
1819 sas_unregister_devs_sas_addr(dev, phy_id);
1820 goto out; break;
1821 case SMP_RESP_FUNC_ACC:
1822 break;
1825 if (SAS_ADDR(attached_sas_addr) == 0) {
1826 phy->phy_state = PHY_EMPTY;
1827 sas_unregister_devs_sas_addr(dev, phy_id);
1828 } else if (SAS_ADDR(attached_sas_addr) ==
1829 SAS_ADDR(phy->attached_sas_addr)) {
1830 SAS_DPRINTK("ex %016llx phy 0x%x broadcast flutter\n",
1831 SAS_ADDR(dev->sas_addr), phy_id);
1832 sas_ex_phy_discover(dev, phy_id);
1833 } else
1834 res = sas_discover_new(dev, phy_id);
1835 out:
1836 return res;
1839 static int sas_rediscover(struct domain_device *dev, const int phy_id)
1841 struct expander_device *ex = &dev->ex_dev;
1842 struct ex_phy *changed_phy = &ex->ex_phy[phy_id];
1843 int res = 0;
1844 int i;
1846 SAS_DPRINTK("ex %016llx phy%d originated BROADCAST(CHANGE)\n",
1847 SAS_ADDR(dev->sas_addr), phy_id);
1849 if (SAS_ADDR(changed_phy->attached_sas_addr) != 0) {
1850 for (i = 0; i < ex->num_phys; i++) {
1851 struct ex_phy *phy = &ex->ex_phy[i];
1853 if (i == phy_id)
1854 continue;
1855 if (SAS_ADDR(phy->attached_sas_addr) ==
1856 SAS_ADDR(changed_phy->attached_sas_addr)) {
1857 SAS_DPRINTK("phy%d part of wide port with "
1858 "phy%d\n", phy_id, i);
1859 goto out;
1862 res = sas_rediscover_dev(dev, phy_id);
1863 } else
1864 res = sas_discover_new(dev, phy_id);
1865 out:
1866 return res;
1870 * sas_revalidate_domain -- revalidate the domain
1871 * @port: port to the domain of interest
1873 * NOTE: this process _must_ quit (return) as soon as any connection
1874 * errors are encountered. Connection recovery is done elsewhere.
1875 * Discover process only interrogates devices in order to discover the
1876 * domain.
1878 int sas_ex_revalidate_domain(struct domain_device *port_dev)
1880 int res;
1881 struct domain_device *dev = NULL;
1883 res = sas_find_bcast_dev(port_dev, &dev);
1884 if (res)
1885 goto out;
1886 if (dev) {
1887 struct expander_device *ex = &dev->ex_dev;
1888 int i = 0, phy_id;
1890 do {
1891 phy_id = -1;
1892 res = sas_find_bcast_phy(dev, &phy_id, i);
1893 if (phy_id == -1)
1894 break;
1895 res = sas_rediscover(dev, phy_id);
1896 i = phy_id + 1;
1897 } while (i < ex->num_phys);
1899 out:
1900 return res;
1903 #if 0
1904 /* ---------- SMP portal ---------- */
1906 static ssize_t smp_portal_write(struct kobject *kobj,
1907 struct bin_attribute *bin_attr,
1908 char *buf, loff_t offs, size_t size)
1910 struct domain_device *dev = to_dom_device(kobj);
1911 struct expander_device *ex = &dev->ex_dev;
1913 if (offs != 0)
1914 return -EFBIG;
1915 else if (size == 0)
1916 return 0;
1918 down_interruptible(&ex->smp_sema);
1919 if (ex->smp_req)
1920 kfree(ex->smp_req);
1921 ex->smp_req = kzalloc(size, GFP_USER);
1922 if (!ex->smp_req) {
1923 up(&ex->smp_sema);
1924 return -ENOMEM;
1926 memcpy(ex->smp_req, buf, size);
1927 ex->smp_req_size = size;
1928 ex->smp_portal_pid = current->pid;
1929 up(&ex->smp_sema);
1931 return size;
1934 static ssize_t smp_portal_read(struct kobject *kobj,
1935 struct bin_attribute *bin_attr,
1936 char *buf, loff_t offs, size_t size)
1938 struct domain_device *dev = to_dom_device(kobj);
1939 struct expander_device *ex = &dev->ex_dev;
1940 u8 *smp_resp;
1941 int res = -EINVAL;
1943 /* XXX: sysfs gives us an offset of 0x10 or 0x8 while in fact
1944 * it should be 0.
1947 down_interruptible(&ex->smp_sema);
1948 if (!ex->smp_req || ex->smp_portal_pid != current->pid)
1949 goto out;
1951 res = 0;
1952 if (size == 0)
1953 goto out;
1955 res = -ENOMEM;
1956 smp_resp = alloc_smp_resp(size);
1957 if (!smp_resp)
1958 goto out;
1959 res = smp_execute_task(dev, ex->smp_req, ex->smp_req_size,
1960 smp_resp, size);
1961 if (!res) {
1962 memcpy(buf, smp_resp, size);
1963 res = size;
1966 kfree(smp_resp);
1967 out:
1968 kfree(ex->smp_req);
1969 ex->smp_req = NULL;
1970 ex->smp_req_size = 0;
1971 ex->smp_portal_pid = -1;
1972 up(&ex->smp_sema);
1973 return res;
1975 #endif
1977 int sas_smp_handler(struct Scsi_Host *shost, struct sas_rphy *rphy,
1978 struct request *req)
1980 struct domain_device *dev;
1981 int ret, type = rphy->identify.device_type;
1982 struct request *rsp = req->next_rq;
1984 if (!rsp) {
1985 printk("%s: space for a smp response is missing\n",
1986 __FUNCTION__);
1987 return -EINVAL;
1990 /* seems aic94xx doesn't support */
1991 if (!rphy) {
1992 printk("%s: can we send a smp request to a host?\n",
1993 __FUNCTION__);
1994 return -EINVAL;
1997 if (type != SAS_EDGE_EXPANDER_DEVICE &&
1998 type != SAS_FANOUT_EXPANDER_DEVICE) {
1999 printk("%s: can we send a smp request to a device?\n",
2000 __FUNCTION__);
2001 return -EINVAL;
2004 dev = sas_find_dev_by_rphy(rphy);
2005 if (!dev) {
2006 printk("%s: fail to find a domain_device?\n", __FUNCTION__);
2007 return -EINVAL;
2010 /* do we need to support multiple segments? */
2011 if (req->bio->bi_vcnt > 1 || rsp->bio->bi_vcnt > 1) {
2012 printk("%s: multiple segments req %u %u, rsp %u %u\n",
2013 __FUNCTION__, req->bio->bi_vcnt, req->data_len,
2014 rsp->bio->bi_vcnt, rsp->data_len);
2015 return -EINVAL;
2018 ret = smp_execute_task(dev, bio_data(req->bio), req->data_len,
2019 bio_data(rsp->bio), rsp->data_len);
2021 return ret;