init from v2.6.32.60
[mach-moxart.git] / drivers / scsi / libsas / sas_expander.c
blob1bdfde16c98771af82adf8e7878e9b4ec3c1b089
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 /* ---------- SMP task management ---------- */
42 static void smp_task_timedout(unsigned long _task)
44 struct sas_task *task = (void *) _task;
45 unsigned long flags;
47 spin_lock_irqsave(&task->task_state_lock, flags);
48 if (!(task->task_state_flags & SAS_TASK_STATE_DONE))
49 task->task_state_flags |= SAS_TASK_STATE_ABORTED;
50 spin_unlock_irqrestore(&task->task_state_lock, flags);
52 complete(&task->completion);
55 static void smp_task_done(struct sas_task *task)
57 if (!del_timer(&task->timer))
58 return;
59 complete(&task->completion);
62 /* Give it some long enough timeout. In seconds. */
63 #define SMP_TIMEOUT 10
65 static int smp_execute_task(struct domain_device *dev, void *req, int req_size,
66 void *resp, int resp_size)
68 int res, retry;
69 struct sas_task *task = NULL;
70 struct sas_internal *i =
71 to_sas_internal(dev->port->ha->core.shost->transportt);
73 for (retry = 0; retry < 3; retry++) {
74 task = sas_alloc_task(GFP_KERNEL);
75 if (!task)
76 return -ENOMEM;
78 task->dev = dev;
79 task->task_proto = dev->tproto;
80 sg_init_one(&task->smp_task.smp_req, req, req_size);
81 sg_init_one(&task->smp_task.smp_resp, resp, resp_size);
83 task->task_done = smp_task_done;
85 task->timer.data = (unsigned long) task;
86 task->timer.function = smp_task_timedout;
87 task->timer.expires = jiffies + SMP_TIMEOUT*HZ;
88 add_timer(&task->timer);
90 res = i->dft->lldd_execute_task(task, 1, GFP_KERNEL);
92 if (res) {
93 del_timer(&task->timer);
94 SAS_DPRINTK("executing SMP task failed:%d\n", res);
95 goto ex_err;
98 wait_for_completion(&task->completion);
99 res = -ECOMM;
100 if ((task->task_state_flags & SAS_TASK_STATE_ABORTED)) {
101 SAS_DPRINTK("smp task timed out or aborted\n");
102 i->dft->lldd_abort_task(task);
103 if (!(task->task_state_flags & SAS_TASK_STATE_DONE)) {
104 SAS_DPRINTK("SMP task aborted and not done\n");
105 goto ex_err;
108 if (task->task_status.resp == SAS_TASK_COMPLETE &&
109 task->task_status.stat == SAM_GOOD) {
110 res = 0;
111 break;
112 } if (task->task_status.resp == SAS_TASK_COMPLETE &&
113 task->task_status.stat == SAS_DATA_UNDERRUN) {
114 /* no error, but return the number of bytes of
115 * underrun */
116 res = task->task_status.residual;
117 break;
118 } if (task->task_status.resp == SAS_TASK_COMPLETE &&
119 task->task_status.stat == SAS_DATA_OVERRUN) {
120 res = -EMSGSIZE;
121 break;
122 } else {
123 SAS_DPRINTK("%s: task to dev %016llx response: 0x%x "
124 "status 0x%x\n", __func__,
125 SAS_ADDR(dev->sas_addr),
126 task->task_status.resp,
127 task->task_status.stat);
128 sas_free_task(task);
129 task = NULL;
132 ex_err:
133 BUG_ON(retry == 3 && task != NULL);
134 if (task != NULL) {
135 sas_free_task(task);
137 return res;
140 /* ---------- Allocations ---------- */
142 static inline void *alloc_smp_req(int size)
144 u8 *p = kzalloc(size, GFP_KERNEL);
145 if (p)
146 p[0] = SMP_REQUEST;
147 return p;
150 static inline void *alloc_smp_resp(int size)
152 return kzalloc(size, GFP_KERNEL);
155 /* ---------- Expander configuration ---------- */
157 static void sas_set_ex_phy(struct domain_device *dev, int phy_id,
158 void *disc_resp)
160 struct expander_device *ex = &dev->ex_dev;
161 struct ex_phy *phy = &ex->ex_phy[phy_id];
162 struct smp_resp *resp = disc_resp;
163 struct discover_resp *dr = &resp->disc;
164 struct sas_rphy *rphy = dev->rphy;
165 int rediscover = (phy->phy != NULL);
167 if (!rediscover) {
168 phy->phy = sas_phy_alloc(&rphy->dev, phy_id);
170 /* FIXME: error_handling */
171 BUG_ON(!phy->phy);
174 switch (resp->result) {
175 case SMP_RESP_PHY_VACANT:
176 phy->phy_state = PHY_VACANT;
177 return;
178 default:
179 phy->phy_state = PHY_NOT_PRESENT;
180 return;
181 case SMP_RESP_FUNC_ACC:
182 phy->phy_state = PHY_EMPTY; /* do not know yet */
183 break;
186 phy->phy_id = phy_id;
187 phy->attached_dev_type = dr->attached_dev_type;
188 phy->linkrate = dr->linkrate;
189 phy->attached_sata_host = dr->attached_sata_host;
190 phy->attached_sata_dev = dr->attached_sata_dev;
191 phy->attached_sata_ps = dr->attached_sata_ps;
192 phy->attached_iproto = dr->iproto << 1;
193 phy->attached_tproto = dr->tproto << 1;
194 memcpy(phy->attached_sas_addr, dr->attached_sas_addr, SAS_ADDR_SIZE);
195 phy->attached_phy_id = dr->attached_phy_id;
196 phy->phy_change_count = dr->change_count;
197 phy->routing_attr = dr->routing_attr;
198 phy->virtual = dr->virtual;
199 phy->last_da_index = -1;
201 phy->phy->identify.sas_address = SAS_ADDR(phy->attached_sas_addr);
202 phy->phy->identify.device_type = phy->attached_dev_type;
203 phy->phy->identify.initiator_port_protocols = phy->attached_iproto;
204 phy->phy->identify.target_port_protocols = phy->attached_tproto;
205 phy->phy->identify.phy_identifier = phy_id;
206 phy->phy->minimum_linkrate_hw = dr->hmin_linkrate;
207 phy->phy->maximum_linkrate_hw = dr->hmax_linkrate;
208 phy->phy->minimum_linkrate = dr->pmin_linkrate;
209 phy->phy->maximum_linkrate = dr->pmax_linkrate;
210 phy->phy->negotiated_linkrate = phy->linkrate;
212 if (!rediscover)
213 sas_phy_add(phy->phy);
215 SAS_DPRINTK("ex %016llx phy%02d:%c attached: %016llx\n",
216 SAS_ADDR(dev->sas_addr), phy->phy_id,
217 phy->routing_attr == TABLE_ROUTING ? 'T' :
218 phy->routing_attr == DIRECT_ROUTING ? 'D' :
219 phy->routing_attr == SUBTRACTIVE_ROUTING ? 'S' : '?',
220 SAS_ADDR(phy->attached_sas_addr));
222 return;
225 #define DISCOVER_REQ_SIZE 16
226 #define DISCOVER_RESP_SIZE 56
228 static int sas_ex_phy_discover_helper(struct domain_device *dev, u8 *disc_req,
229 u8 *disc_resp, int single)
231 int i, res;
233 disc_req[9] = single;
234 for (i = 1 ; i < 3; i++) {
235 struct discover_resp *dr;
237 res = smp_execute_task(dev, disc_req, DISCOVER_REQ_SIZE,
238 disc_resp, DISCOVER_RESP_SIZE);
239 if (res)
240 return res;
241 /* This is detecting a failure to transmit inital
242 * dev to host FIS as described in section G.5 of
243 * sas-2 r 04b */
244 dr = &((struct smp_resp *)disc_resp)->disc;
245 if (!(dr->attached_dev_type == 0 &&
246 dr->attached_sata_dev))
247 break;
248 /* In order to generate the dev to host FIS, we
249 * send a link reset to the expander port */
250 sas_smp_phy_control(dev, single, PHY_FUNC_LINK_RESET, NULL);
251 /* Wait for the reset to trigger the negotiation */
252 msleep(500);
254 sas_set_ex_phy(dev, single, disc_resp);
255 return 0;
258 static int sas_ex_phy_discover(struct domain_device *dev, int single)
260 struct expander_device *ex = &dev->ex_dev;
261 int res = 0;
262 u8 *disc_req;
263 u8 *disc_resp;
265 disc_req = alloc_smp_req(DISCOVER_REQ_SIZE);
266 if (!disc_req)
267 return -ENOMEM;
269 disc_resp = alloc_smp_req(DISCOVER_RESP_SIZE);
270 if (!disc_resp) {
271 kfree(disc_req);
272 return -ENOMEM;
275 disc_req[1] = SMP_DISCOVER;
277 if (0 <= single && single < ex->num_phys) {
278 res = sas_ex_phy_discover_helper(dev, disc_req, disc_resp, single);
279 } else {
280 int i;
282 for (i = 0; i < ex->num_phys; i++) {
283 res = sas_ex_phy_discover_helper(dev, disc_req,
284 disc_resp, i);
285 if (res)
286 goto out_err;
289 out_err:
290 kfree(disc_resp);
291 kfree(disc_req);
292 return res;
295 static int sas_expander_discover(struct domain_device *dev)
297 struct expander_device *ex = &dev->ex_dev;
298 int res = -ENOMEM;
300 ex->ex_phy = kzalloc(sizeof(*ex->ex_phy)*ex->num_phys, GFP_KERNEL);
301 if (!ex->ex_phy)
302 return -ENOMEM;
304 res = sas_ex_phy_discover(dev, -1);
305 if (res)
306 goto out_err;
308 return 0;
309 out_err:
310 kfree(ex->ex_phy);
311 ex->ex_phy = NULL;
312 return res;
315 #define MAX_EXPANDER_PHYS 128
317 static void ex_assign_report_general(struct domain_device *dev,
318 struct smp_resp *resp)
320 struct report_general_resp *rg = &resp->rg;
322 dev->ex_dev.ex_change_count = be16_to_cpu(rg->change_count);
323 dev->ex_dev.max_route_indexes = be16_to_cpu(rg->route_indexes);
324 dev->ex_dev.num_phys = min(rg->num_phys, (u8)MAX_EXPANDER_PHYS);
325 dev->ex_dev.conf_route_table = rg->conf_route_table;
326 dev->ex_dev.configuring = rg->configuring;
327 memcpy(dev->ex_dev.enclosure_logical_id, rg->enclosure_logical_id, 8);
330 #define RG_REQ_SIZE 8
331 #define RG_RESP_SIZE 32
333 static int sas_ex_general(struct domain_device *dev)
335 u8 *rg_req;
336 struct smp_resp *rg_resp;
337 int res;
338 int i;
340 rg_req = alloc_smp_req(RG_REQ_SIZE);
341 if (!rg_req)
342 return -ENOMEM;
344 rg_resp = alloc_smp_resp(RG_RESP_SIZE);
345 if (!rg_resp) {
346 kfree(rg_req);
347 return -ENOMEM;
350 rg_req[1] = SMP_REPORT_GENERAL;
352 for (i = 0; i < 5; i++) {
353 res = smp_execute_task(dev, rg_req, RG_REQ_SIZE, rg_resp,
354 RG_RESP_SIZE);
356 if (res) {
357 SAS_DPRINTK("RG to ex %016llx failed:0x%x\n",
358 SAS_ADDR(dev->sas_addr), res);
359 goto out;
360 } else if (rg_resp->result != SMP_RESP_FUNC_ACC) {
361 SAS_DPRINTK("RG:ex %016llx returned SMP result:0x%x\n",
362 SAS_ADDR(dev->sas_addr), rg_resp->result);
363 res = rg_resp->result;
364 goto out;
367 ex_assign_report_general(dev, rg_resp);
369 if (dev->ex_dev.configuring) {
370 SAS_DPRINTK("RG: ex %llx self-configuring...\n",
371 SAS_ADDR(dev->sas_addr));
372 schedule_timeout_interruptible(5*HZ);
373 } else
374 break;
376 out:
377 kfree(rg_req);
378 kfree(rg_resp);
379 return res;
382 static void ex_assign_manuf_info(struct domain_device *dev, void
383 *_mi_resp)
385 u8 *mi_resp = _mi_resp;
386 struct sas_rphy *rphy = dev->rphy;
387 struct sas_expander_device *edev = rphy_to_expander_device(rphy);
389 memcpy(edev->vendor_id, mi_resp + 12, SAS_EXPANDER_VENDOR_ID_LEN);
390 memcpy(edev->product_id, mi_resp + 20, SAS_EXPANDER_PRODUCT_ID_LEN);
391 memcpy(edev->product_rev, mi_resp + 36,
392 SAS_EXPANDER_PRODUCT_REV_LEN);
394 if (mi_resp[8] & 1) {
395 memcpy(edev->component_vendor_id, mi_resp + 40,
396 SAS_EXPANDER_COMPONENT_VENDOR_ID_LEN);
397 edev->component_id = mi_resp[48] << 8 | mi_resp[49];
398 edev->component_revision_id = mi_resp[50];
402 #define MI_REQ_SIZE 8
403 #define MI_RESP_SIZE 64
405 static int sas_ex_manuf_info(struct domain_device *dev)
407 u8 *mi_req;
408 u8 *mi_resp;
409 int res;
411 mi_req = alloc_smp_req(MI_REQ_SIZE);
412 if (!mi_req)
413 return -ENOMEM;
415 mi_resp = alloc_smp_resp(MI_RESP_SIZE);
416 if (!mi_resp) {
417 kfree(mi_req);
418 return -ENOMEM;
421 mi_req[1] = SMP_REPORT_MANUF_INFO;
423 res = smp_execute_task(dev, mi_req, MI_REQ_SIZE, mi_resp,MI_RESP_SIZE);
424 if (res) {
425 SAS_DPRINTK("MI: ex %016llx failed:0x%x\n",
426 SAS_ADDR(dev->sas_addr), res);
427 goto out;
428 } else if (mi_resp[2] != SMP_RESP_FUNC_ACC) {
429 SAS_DPRINTK("MI ex %016llx returned SMP result:0x%x\n",
430 SAS_ADDR(dev->sas_addr), mi_resp[2]);
431 goto out;
434 ex_assign_manuf_info(dev, mi_resp);
435 out:
436 kfree(mi_req);
437 kfree(mi_resp);
438 return res;
441 #define PC_REQ_SIZE 44
442 #define PC_RESP_SIZE 8
444 int sas_smp_phy_control(struct domain_device *dev, int phy_id,
445 enum phy_func phy_func,
446 struct sas_phy_linkrates *rates)
448 u8 *pc_req;
449 u8 *pc_resp;
450 int res;
452 pc_req = alloc_smp_req(PC_REQ_SIZE);
453 if (!pc_req)
454 return -ENOMEM;
456 pc_resp = alloc_smp_resp(PC_RESP_SIZE);
457 if (!pc_resp) {
458 kfree(pc_req);
459 return -ENOMEM;
462 pc_req[1] = SMP_PHY_CONTROL;
463 pc_req[9] = phy_id;
464 pc_req[10]= phy_func;
465 if (rates) {
466 pc_req[32] = rates->minimum_linkrate << 4;
467 pc_req[33] = rates->maximum_linkrate << 4;
470 res = smp_execute_task(dev, pc_req, PC_REQ_SIZE, pc_resp,PC_RESP_SIZE);
472 kfree(pc_resp);
473 kfree(pc_req);
474 return res;
477 static void sas_ex_disable_phy(struct domain_device *dev, int phy_id)
479 struct expander_device *ex = &dev->ex_dev;
480 struct ex_phy *phy = &ex->ex_phy[phy_id];
482 sas_smp_phy_control(dev, phy_id, PHY_FUNC_DISABLE, NULL);
483 phy->linkrate = SAS_PHY_DISABLED;
486 static void sas_ex_disable_port(struct domain_device *dev, u8 *sas_addr)
488 struct expander_device *ex = &dev->ex_dev;
489 int i;
491 for (i = 0; i < ex->num_phys; i++) {
492 struct ex_phy *phy = &ex->ex_phy[i];
494 if (phy->phy_state == PHY_VACANT ||
495 phy->phy_state == PHY_NOT_PRESENT)
496 continue;
498 if (SAS_ADDR(phy->attached_sas_addr) == SAS_ADDR(sas_addr))
499 sas_ex_disable_phy(dev, i);
503 static int sas_dev_present_in_domain(struct asd_sas_port *port,
504 u8 *sas_addr)
506 struct domain_device *dev;
508 if (SAS_ADDR(port->sas_addr) == SAS_ADDR(sas_addr))
509 return 1;
510 list_for_each_entry(dev, &port->dev_list, dev_list_node) {
511 if (SAS_ADDR(dev->sas_addr) == SAS_ADDR(sas_addr))
512 return 1;
514 return 0;
517 #define RPEL_REQ_SIZE 16
518 #define RPEL_RESP_SIZE 32
519 int sas_smp_get_phy_events(struct sas_phy *phy)
521 int res;
522 u8 *req;
523 u8 *resp;
524 struct sas_rphy *rphy = dev_to_rphy(phy->dev.parent);
525 struct domain_device *dev = sas_find_dev_by_rphy(rphy);
527 req = alloc_smp_req(RPEL_REQ_SIZE);
528 if (!req)
529 return -ENOMEM;
531 resp = alloc_smp_resp(RPEL_RESP_SIZE);
532 if (!resp) {
533 kfree(req);
534 return -ENOMEM;
537 req[1] = SMP_REPORT_PHY_ERR_LOG;
538 req[9] = phy->number;
540 res = smp_execute_task(dev, req, RPEL_REQ_SIZE,
541 resp, RPEL_RESP_SIZE);
543 if (!res)
544 goto out;
546 phy->invalid_dword_count = scsi_to_u32(&resp[12]);
547 phy->running_disparity_error_count = scsi_to_u32(&resp[16]);
548 phy->loss_of_dword_sync_count = scsi_to_u32(&resp[20]);
549 phy->phy_reset_problem_count = scsi_to_u32(&resp[24]);
551 out:
552 kfree(resp);
553 return res;
557 #ifdef CONFIG_SCSI_SAS_ATA
559 #define RPS_REQ_SIZE 16
560 #define RPS_RESP_SIZE 60
562 static int sas_get_report_phy_sata(struct domain_device *dev,
563 int phy_id,
564 struct smp_resp *rps_resp)
566 int res;
567 u8 *rps_req = alloc_smp_req(RPS_REQ_SIZE);
568 u8 *resp = (u8 *)rps_resp;
570 if (!rps_req)
571 return -ENOMEM;
573 rps_req[1] = SMP_REPORT_PHY_SATA;
574 rps_req[9] = phy_id;
576 res = smp_execute_task(dev, rps_req, RPS_REQ_SIZE,
577 rps_resp, RPS_RESP_SIZE);
579 /* 0x34 is the FIS type for the D2H fis. There's a potential
580 * standards cockup here. sas-2 explicitly specifies the FIS
581 * should be encoded so that FIS type is in resp[24].
582 * However, some expanders endian reverse this. Undo the
583 * reversal here */
584 if (!res && resp[27] == 0x34 && resp[24] != 0x34) {
585 int i;
587 for (i = 0; i < 5; i++) {
588 int j = 24 + (i*4);
589 u8 a, b;
590 a = resp[j + 0];
591 b = resp[j + 1];
592 resp[j + 0] = resp[j + 3];
593 resp[j + 1] = resp[j + 2];
594 resp[j + 2] = b;
595 resp[j + 3] = a;
599 kfree(rps_req);
600 return res;
602 #endif
604 static void sas_ex_get_linkrate(struct domain_device *parent,
605 struct domain_device *child,
606 struct ex_phy *parent_phy)
608 struct expander_device *parent_ex = &parent->ex_dev;
609 struct sas_port *port;
610 int i;
612 child->pathways = 0;
614 port = parent_phy->port;
616 for (i = 0; i < parent_ex->num_phys; i++) {
617 struct ex_phy *phy = &parent_ex->ex_phy[i];
619 if (phy->phy_state == PHY_VACANT ||
620 phy->phy_state == PHY_NOT_PRESENT)
621 continue;
623 if (SAS_ADDR(phy->attached_sas_addr) ==
624 SAS_ADDR(child->sas_addr)) {
626 child->min_linkrate = min(parent->min_linkrate,
627 phy->linkrate);
628 child->max_linkrate = max(parent->max_linkrate,
629 phy->linkrate);
630 child->pathways++;
631 sas_port_add_phy(port, phy->phy);
634 child->linkrate = min(parent_phy->linkrate, child->max_linkrate);
635 child->pathways = min(child->pathways, parent->pathways);
638 static struct domain_device *sas_ex_discover_end_dev(
639 struct domain_device *parent, int phy_id)
641 struct expander_device *parent_ex = &parent->ex_dev;
642 struct ex_phy *phy = &parent_ex->ex_phy[phy_id];
643 struct domain_device *child = NULL;
644 struct sas_rphy *rphy;
645 int res;
647 if (phy->attached_sata_host || phy->attached_sata_ps)
648 return NULL;
650 child = kzalloc(sizeof(*child), GFP_KERNEL);
651 if (!child)
652 return NULL;
654 child->parent = parent;
655 child->port = parent->port;
656 child->iproto = phy->attached_iproto;
657 memcpy(child->sas_addr, phy->attached_sas_addr, SAS_ADDR_SIZE);
658 sas_hash_addr(child->hashed_sas_addr, child->sas_addr);
659 if (!phy->port) {
660 phy->port = sas_port_alloc(&parent->rphy->dev, phy_id);
661 if (unlikely(!phy->port))
662 goto out_err;
663 if (unlikely(sas_port_add(phy->port) != 0)) {
664 sas_port_free(phy->port);
665 goto out_err;
668 sas_ex_get_linkrate(parent, child, phy);
670 #ifdef CONFIG_SCSI_SAS_ATA
671 if ((phy->attached_tproto & SAS_PROTOCOL_STP) || phy->attached_sata_dev) {
672 child->dev_type = SATA_DEV;
673 if (phy->attached_tproto & SAS_PROTOCOL_STP)
674 child->tproto = phy->attached_tproto;
675 if (phy->attached_sata_dev)
676 child->tproto |= SATA_DEV;
677 res = sas_get_report_phy_sata(parent, phy_id,
678 &child->sata_dev.rps_resp);
679 if (res) {
680 SAS_DPRINTK("report phy sata to %016llx:0x%x returned "
681 "0x%x\n", SAS_ADDR(parent->sas_addr),
682 phy_id, res);
683 goto out_free;
685 memcpy(child->frame_rcvd, &child->sata_dev.rps_resp.rps.fis,
686 sizeof(struct dev_to_host_fis));
688 rphy = sas_end_device_alloc(phy->port);
689 if (unlikely(!rphy))
690 goto out_free;
692 sas_init_dev(child);
694 child->rphy = rphy;
696 spin_lock_irq(&parent->port->dev_list_lock);
697 list_add_tail(&child->dev_list_node, &parent->port->dev_list);
698 spin_unlock_irq(&parent->port->dev_list_lock);
700 res = sas_discover_sata(child);
701 if (res) {
702 SAS_DPRINTK("sas_discover_sata() for device %16llx at "
703 "%016llx:0x%x returned 0x%x\n",
704 SAS_ADDR(child->sas_addr),
705 SAS_ADDR(parent->sas_addr), phy_id, res);
706 goto out_list_del;
708 } else
709 #endif
710 if (phy->attached_tproto & SAS_PROTOCOL_SSP) {
711 child->dev_type = SAS_END_DEV;
712 rphy = sas_end_device_alloc(phy->port);
713 /* FIXME: error handling */
714 if (unlikely(!rphy))
715 goto out_free;
716 child->tproto = phy->attached_tproto;
717 sas_init_dev(child);
719 child->rphy = rphy;
720 sas_fill_in_rphy(child, rphy);
722 spin_lock_irq(&parent->port->dev_list_lock);
723 list_add_tail(&child->dev_list_node, &parent->port->dev_list);
724 spin_unlock_irq(&parent->port->dev_list_lock);
726 res = sas_discover_end_dev(child);
727 if (res) {
728 SAS_DPRINTK("sas_discover_end_dev() for device %16llx "
729 "at %016llx:0x%x returned 0x%x\n",
730 SAS_ADDR(child->sas_addr),
731 SAS_ADDR(parent->sas_addr), phy_id, res);
732 goto out_list_del;
734 } else {
735 SAS_DPRINTK("target proto 0x%x at %016llx:0x%x not handled\n",
736 phy->attached_tproto, SAS_ADDR(parent->sas_addr),
737 phy_id);
738 goto out_free;
741 list_add_tail(&child->siblings, &parent_ex->children);
742 return child;
744 out_list_del:
745 sas_rphy_free(child->rphy);
746 child->rphy = NULL;
747 list_del(&child->dev_list_node);
748 out_free:
749 sas_port_delete(phy->port);
750 out_err:
751 phy->port = NULL;
752 kfree(child);
753 return NULL;
756 /* See if this phy is part of a wide port */
757 static bool sas_ex_join_wide_port(struct domain_device *parent, int phy_id)
759 struct ex_phy *phy = &parent->ex_dev.ex_phy[phy_id];
760 int i;
762 for (i = 0; i < parent->ex_dev.num_phys; i++) {
763 struct ex_phy *ephy = &parent->ex_dev.ex_phy[i];
765 if (ephy == phy)
766 continue;
768 if (!memcmp(phy->attached_sas_addr, ephy->attached_sas_addr,
769 SAS_ADDR_SIZE) && ephy->port) {
770 sas_port_add_phy(ephy->port, phy->phy);
771 phy->port = ephy->port;
772 phy->phy_state = PHY_DEVICE_DISCOVERED;
773 return true;
777 return false;
780 static struct domain_device *sas_ex_discover_expander(
781 struct domain_device *parent, int phy_id)
783 struct sas_expander_device *parent_ex = rphy_to_expander_device(parent->rphy);
784 struct ex_phy *phy = &parent->ex_dev.ex_phy[phy_id];
785 struct domain_device *child = NULL;
786 struct sas_rphy *rphy;
787 struct sas_expander_device *edev;
788 struct asd_sas_port *port;
789 int res;
791 if (phy->routing_attr == DIRECT_ROUTING) {
792 SAS_DPRINTK("ex %016llx:0x%x:D <--> ex %016llx:0x%x is not "
793 "allowed\n",
794 SAS_ADDR(parent->sas_addr), phy_id,
795 SAS_ADDR(phy->attached_sas_addr),
796 phy->attached_phy_id);
797 return NULL;
799 child = kzalloc(sizeof(*child), GFP_KERNEL);
800 if (!child)
801 return NULL;
803 phy->port = sas_port_alloc(&parent->rphy->dev, phy_id);
804 /* FIXME: better error handling */
805 BUG_ON(sas_port_add(phy->port) != 0);
808 switch (phy->attached_dev_type) {
809 case EDGE_DEV:
810 rphy = sas_expander_alloc(phy->port,
811 SAS_EDGE_EXPANDER_DEVICE);
812 break;
813 case FANOUT_DEV:
814 rphy = sas_expander_alloc(phy->port,
815 SAS_FANOUT_EXPANDER_DEVICE);
816 break;
817 default:
818 rphy = NULL; /* shut gcc up */
819 BUG();
821 port = parent->port;
822 child->rphy = rphy;
823 edev = rphy_to_expander_device(rphy);
824 child->dev_type = phy->attached_dev_type;
825 child->parent = parent;
826 child->port = port;
827 child->iproto = phy->attached_iproto;
828 child->tproto = phy->attached_tproto;
829 memcpy(child->sas_addr, phy->attached_sas_addr, SAS_ADDR_SIZE);
830 sas_hash_addr(child->hashed_sas_addr, child->sas_addr);
831 sas_ex_get_linkrate(parent, child, phy);
832 edev->level = parent_ex->level + 1;
833 parent->port->disc.max_level = max(parent->port->disc.max_level,
834 edev->level);
835 sas_init_dev(child);
836 sas_fill_in_rphy(child, rphy);
837 sas_rphy_add(rphy);
839 spin_lock_irq(&parent->port->dev_list_lock);
840 list_add_tail(&child->dev_list_node, &parent->port->dev_list);
841 spin_unlock_irq(&parent->port->dev_list_lock);
843 res = sas_discover_expander(child);
844 if (res) {
845 spin_lock_irq(&parent->port->dev_list_lock);
846 list_del(&child->dev_list_node);
847 spin_unlock_irq(&parent->port->dev_list_lock);
848 kfree(child);
849 return NULL;
851 list_add_tail(&child->siblings, &parent->ex_dev.children);
852 return child;
855 static int sas_ex_discover_dev(struct domain_device *dev, int phy_id)
857 struct expander_device *ex = &dev->ex_dev;
858 struct ex_phy *ex_phy = &ex->ex_phy[phy_id];
859 struct domain_device *child = NULL;
860 int res = 0;
862 /* Phy state */
863 if (ex_phy->linkrate == SAS_SATA_SPINUP_HOLD) {
864 if (!sas_smp_phy_control(dev, phy_id, PHY_FUNC_LINK_RESET, NULL))
865 res = sas_ex_phy_discover(dev, phy_id);
866 if (res)
867 return res;
870 /* Parent and domain coherency */
871 if (!dev->parent && (SAS_ADDR(ex_phy->attached_sas_addr) ==
872 SAS_ADDR(dev->port->sas_addr))) {
873 sas_add_parent_port(dev, phy_id);
874 return 0;
876 if (dev->parent && (SAS_ADDR(ex_phy->attached_sas_addr) ==
877 SAS_ADDR(dev->parent->sas_addr))) {
878 sas_add_parent_port(dev, phy_id);
879 if (ex_phy->routing_attr == TABLE_ROUTING)
880 sas_configure_phy(dev, phy_id, dev->port->sas_addr, 1);
881 return 0;
884 if (sas_dev_present_in_domain(dev->port, ex_phy->attached_sas_addr))
885 sas_ex_disable_port(dev, ex_phy->attached_sas_addr);
887 if (ex_phy->attached_dev_type == NO_DEVICE) {
888 if (ex_phy->routing_attr == DIRECT_ROUTING) {
889 memset(ex_phy->attached_sas_addr, 0, SAS_ADDR_SIZE);
890 sas_configure_routing(dev, ex_phy->attached_sas_addr);
892 return 0;
893 } else if (ex_phy->linkrate == SAS_LINK_RATE_UNKNOWN)
894 return 0;
896 if (ex_phy->attached_dev_type != SAS_END_DEV &&
897 ex_phy->attached_dev_type != FANOUT_DEV &&
898 ex_phy->attached_dev_type != EDGE_DEV) {
899 SAS_DPRINTK("unknown device type(0x%x) attached to ex %016llx "
900 "phy 0x%x\n", ex_phy->attached_dev_type,
901 SAS_ADDR(dev->sas_addr),
902 phy_id);
903 return 0;
906 res = sas_configure_routing(dev, ex_phy->attached_sas_addr);
907 if (res) {
908 SAS_DPRINTK("configure routing for dev %016llx "
909 "reported 0x%x. Forgotten\n",
910 SAS_ADDR(ex_phy->attached_sas_addr), res);
911 sas_disable_routing(dev, ex_phy->attached_sas_addr);
912 return res;
915 if (sas_ex_join_wide_port(dev, phy_id)) {
916 SAS_DPRINTK("Attaching ex phy%d to wide port %016llx\n",
917 phy_id, SAS_ADDR(ex_phy->attached_sas_addr));
918 return res;
921 switch (ex_phy->attached_dev_type) {
922 case SAS_END_DEV:
923 child = sas_ex_discover_end_dev(dev, phy_id);
924 break;
925 case FANOUT_DEV:
926 if (SAS_ADDR(dev->port->disc.fanout_sas_addr)) {
927 SAS_DPRINTK("second fanout expander %016llx phy 0x%x "
928 "attached to ex %016llx phy 0x%x\n",
929 SAS_ADDR(ex_phy->attached_sas_addr),
930 ex_phy->attached_phy_id,
931 SAS_ADDR(dev->sas_addr),
932 phy_id);
933 sas_ex_disable_phy(dev, phy_id);
934 break;
935 } else
936 memcpy(dev->port->disc.fanout_sas_addr,
937 ex_phy->attached_sas_addr, SAS_ADDR_SIZE);
938 /* fallthrough */
939 case EDGE_DEV:
940 child = sas_ex_discover_expander(dev, phy_id);
941 break;
942 default:
943 break;
946 if (child) {
947 int i;
949 for (i = 0; i < ex->num_phys; i++) {
950 if (ex->ex_phy[i].phy_state == PHY_VACANT ||
951 ex->ex_phy[i].phy_state == PHY_NOT_PRESENT)
952 continue;
954 * Due to races, the phy might not get added to the
955 * wide port, so we add the phy to the wide port here.
957 if (SAS_ADDR(ex->ex_phy[i].attached_sas_addr) ==
958 SAS_ADDR(child->sas_addr)) {
959 ex->ex_phy[i].phy_state= PHY_DEVICE_DISCOVERED;
960 if (sas_ex_join_wide_port(dev, i))
961 SAS_DPRINTK("Attaching ex phy%d to wide port %016llx\n",
962 i, SAS_ADDR(ex->ex_phy[i].attached_sas_addr));
968 return res;
971 static int sas_find_sub_addr(struct domain_device *dev, u8 *sub_addr)
973 struct expander_device *ex = &dev->ex_dev;
974 int i;
976 for (i = 0; i < ex->num_phys; i++) {
977 struct ex_phy *phy = &ex->ex_phy[i];
979 if (phy->phy_state == PHY_VACANT ||
980 phy->phy_state == PHY_NOT_PRESENT)
981 continue;
983 if ((phy->attached_dev_type == EDGE_DEV ||
984 phy->attached_dev_type == FANOUT_DEV) &&
985 phy->routing_attr == SUBTRACTIVE_ROUTING) {
987 memcpy(sub_addr, phy->attached_sas_addr,SAS_ADDR_SIZE);
989 return 1;
992 return 0;
995 static int sas_check_level_subtractive_boundary(struct domain_device *dev)
997 struct expander_device *ex = &dev->ex_dev;
998 struct domain_device *child;
999 u8 sub_addr[8] = {0, };
1001 list_for_each_entry(child, &ex->children, siblings) {
1002 if (child->dev_type != EDGE_DEV &&
1003 child->dev_type != FANOUT_DEV)
1004 continue;
1005 if (sub_addr[0] == 0) {
1006 sas_find_sub_addr(child, sub_addr);
1007 continue;
1008 } else {
1009 u8 s2[8];
1011 if (sas_find_sub_addr(child, s2) &&
1012 (SAS_ADDR(sub_addr) != SAS_ADDR(s2))) {
1014 SAS_DPRINTK("ex %016llx->%016llx-?->%016llx "
1015 "diverges from subtractive "
1016 "boundary %016llx\n",
1017 SAS_ADDR(dev->sas_addr),
1018 SAS_ADDR(child->sas_addr),
1019 SAS_ADDR(s2),
1020 SAS_ADDR(sub_addr));
1022 sas_ex_disable_port(child, s2);
1026 return 0;
1029 * sas_ex_discover_devices -- discover devices attached to this expander
1030 * dev: pointer to the expander domain device
1031 * single: if you want to do a single phy, else set to -1;
1033 * Configure this expander for use with its devices and register the
1034 * devices of this expander.
1036 static int sas_ex_discover_devices(struct domain_device *dev, int single)
1038 struct expander_device *ex = &dev->ex_dev;
1039 int i = 0, end = ex->num_phys;
1040 int res = 0;
1042 if (0 <= single && single < end) {
1043 i = single;
1044 end = i+1;
1047 for ( ; i < end; i++) {
1048 struct ex_phy *ex_phy = &ex->ex_phy[i];
1050 if (ex_phy->phy_state == PHY_VACANT ||
1051 ex_phy->phy_state == PHY_NOT_PRESENT ||
1052 ex_phy->phy_state == PHY_DEVICE_DISCOVERED)
1053 continue;
1055 switch (ex_phy->linkrate) {
1056 case SAS_PHY_DISABLED:
1057 case SAS_PHY_RESET_PROBLEM:
1058 case SAS_SATA_PORT_SELECTOR:
1059 continue;
1060 default:
1061 res = sas_ex_discover_dev(dev, i);
1062 if (res)
1063 break;
1064 continue;
1068 if (!res)
1069 sas_check_level_subtractive_boundary(dev);
1071 return res;
1074 static int sas_check_ex_subtractive_boundary(struct domain_device *dev)
1076 struct expander_device *ex = &dev->ex_dev;
1077 int i;
1078 u8 *sub_sas_addr = NULL;
1080 if (dev->dev_type != EDGE_DEV)
1081 return 0;
1083 for (i = 0; i < ex->num_phys; i++) {
1084 struct ex_phy *phy = &ex->ex_phy[i];
1086 if (phy->phy_state == PHY_VACANT ||
1087 phy->phy_state == PHY_NOT_PRESENT)
1088 continue;
1090 if ((phy->attached_dev_type == FANOUT_DEV ||
1091 phy->attached_dev_type == EDGE_DEV) &&
1092 phy->routing_attr == SUBTRACTIVE_ROUTING) {
1094 if (!sub_sas_addr)
1095 sub_sas_addr = &phy->attached_sas_addr[0];
1096 else if (SAS_ADDR(sub_sas_addr) !=
1097 SAS_ADDR(phy->attached_sas_addr)) {
1099 SAS_DPRINTK("ex %016llx phy 0x%x "
1100 "diverges(%016llx) on subtractive "
1101 "boundary(%016llx). Disabled\n",
1102 SAS_ADDR(dev->sas_addr), i,
1103 SAS_ADDR(phy->attached_sas_addr),
1104 SAS_ADDR(sub_sas_addr));
1105 sas_ex_disable_phy(dev, i);
1109 return 0;
1112 static void sas_print_parent_topology_bug(struct domain_device *child,
1113 struct ex_phy *parent_phy,
1114 struct ex_phy *child_phy)
1116 static const char ra_char[] = {
1117 [DIRECT_ROUTING] = 'D',
1118 [SUBTRACTIVE_ROUTING] = 'S',
1119 [TABLE_ROUTING] = 'T',
1121 static const char *ex_type[] = {
1122 [EDGE_DEV] = "edge",
1123 [FANOUT_DEV] = "fanout",
1125 struct domain_device *parent = child->parent;
1127 sas_printk("%s ex %016llx phy 0x%x <--> %s ex %016llx phy 0x%x "
1128 "has %c:%c routing link!\n",
1130 ex_type[parent->dev_type],
1131 SAS_ADDR(parent->sas_addr),
1132 parent_phy->phy_id,
1134 ex_type[child->dev_type],
1135 SAS_ADDR(child->sas_addr),
1136 child_phy->phy_id,
1138 ra_char[parent_phy->routing_attr],
1139 ra_char[child_phy->routing_attr]);
1142 static int sas_check_eeds(struct domain_device *child,
1143 struct ex_phy *parent_phy,
1144 struct ex_phy *child_phy)
1146 int res = 0;
1147 struct domain_device *parent = child->parent;
1149 if (SAS_ADDR(parent->port->disc.fanout_sas_addr) != 0) {
1150 res = -ENODEV;
1151 SAS_DPRINTK("edge ex %016llx phy S:0x%x <--> edge ex %016llx "
1152 "phy S:0x%x, while there is a fanout ex %016llx\n",
1153 SAS_ADDR(parent->sas_addr),
1154 parent_phy->phy_id,
1155 SAS_ADDR(child->sas_addr),
1156 child_phy->phy_id,
1157 SAS_ADDR(parent->port->disc.fanout_sas_addr));
1158 } else if (SAS_ADDR(parent->port->disc.eeds_a) == 0) {
1159 memcpy(parent->port->disc.eeds_a, parent->sas_addr,
1160 SAS_ADDR_SIZE);
1161 memcpy(parent->port->disc.eeds_b, child->sas_addr,
1162 SAS_ADDR_SIZE);
1163 } else if (((SAS_ADDR(parent->port->disc.eeds_a) ==
1164 SAS_ADDR(parent->sas_addr)) ||
1165 (SAS_ADDR(parent->port->disc.eeds_a) ==
1166 SAS_ADDR(child->sas_addr)))
1168 ((SAS_ADDR(parent->port->disc.eeds_b) ==
1169 SAS_ADDR(parent->sas_addr)) ||
1170 (SAS_ADDR(parent->port->disc.eeds_b) ==
1171 SAS_ADDR(child->sas_addr))))
1173 else {
1174 res = -ENODEV;
1175 SAS_DPRINTK("edge ex %016llx phy 0x%x <--> edge ex %016llx "
1176 "phy 0x%x link forms a third EEDS!\n",
1177 SAS_ADDR(parent->sas_addr),
1178 parent_phy->phy_id,
1179 SAS_ADDR(child->sas_addr),
1180 child_phy->phy_id);
1183 return res;
1186 /* Here we spill over 80 columns. It is intentional.
1188 static int sas_check_parent_topology(struct domain_device *child)
1190 struct expander_device *child_ex = &child->ex_dev;
1191 struct expander_device *parent_ex;
1192 int i;
1193 int res = 0;
1195 if (!child->parent)
1196 return 0;
1198 if (child->parent->dev_type != EDGE_DEV &&
1199 child->parent->dev_type != FANOUT_DEV)
1200 return 0;
1202 parent_ex = &child->parent->ex_dev;
1204 for (i = 0; i < parent_ex->num_phys; i++) {
1205 struct ex_phy *parent_phy = &parent_ex->ex_phy[i];
1206 struct ex_phy *child_phy;
1208 if (parent_phy->phy_state == PHY_VACANT ||
1209 parent_phy->phy_state == PHY_NOT_PRESENT)
1210 continue;
1212 if (SAS_ADDR(parent_phy->attached_sas_addr) != SAS_ADDR(child->sas_addr))
1213 continue;
1215 child_phy = &child_ex->ex_phy[parent_phy->attached_phy_id];
1217 switch (child->parent->dev_type) {
1218 case EDGE_DEV:
1219 if (child->dev_type == FANOUT_DEV) {
1220 if (parent_phy->routing_attr != SUBTRACTIVE_ROUTING ||
1221 child_phy->routing_attr != TABLE_ROUTING) {
1222 sas_print_parent_topology_bug(child, parent_phy, child_phy);
1223 res = -ENODEV;
1225 } else if (parent_phy->routing_attr == SUBTRACTIVE_ROUTING) {
1226 if (child_phy->routing_attr == SUBTRACTIVE_ROUTING) {
1227 res = sas_check_eeds(child, parent_phy, child_phy);
1228 } else if (child_phy->routing_attr != TABLE_ROUTING) {
1229 sas_print_parent_topology_bug(child, parent_phy, child_phy);
1230 res = -ENODEV;
1232 } else if (parent_phy->routing_attr == TABLE_ROUTING &&
1233 child_phy->routing_attr != SUBTRACTIVE_ROUTING) {
1234 sas_print_parent_topology_bug(child, parent_phy, child_phy);
1235 res = -ENODEV;
1237 break;
1238 case FANOUT_DEV:
1239 if (parent_phy->routing_attr != TABLE_ROUTING ||
1240 child_phy->routing_attr != SUBTRACTIVE_ROUTING) {
1241 sas_print_parent_topology_bug(child, parent_phy, child_phy);
1242 res = -ENODEV;
1244 break;
1245 default:
1246 break;
1250 return res;
1253 #define RRI_REQ_SIZE 16
1254 #define RRI_RESP_SIZE 44
1256 static int sas_configure_present(struct domain_device *dev, int phy_id,
1257 u8 *sas_addr, int *index, int *present)
1259 int i, res = 0;
1260 struct expander_device *ex = &dev->ex_dev;
1261 struct ex_phy *phy = &ex->ex_phy[phy_id];
1262 u8 *rri_req;
1263 u8 *rri_resp;
1265 *present = 0;
1266 *index = 0;
1268 rri_req = alloc_smp_req(RRI_REQ_SIZE);
1269 if (!rri_req)
1270 return -ENOMEM;
1272 rri_resp = alloc_smp_resp(RRI_RESP_SIZE);
1273 if (!rri_resp) {
1274 kfree(rri_req);
1275 return -ENOMEM;
1278 rri_req[1] = SMP_REPORT_ROUTE_INFO;
1279 rri_req[9] = phy_id;
1281 for (i = 0; i < ex->max_route_indexes ; i++) {
1282 *(__be16 *)(rri_req+6) = cpu_to_be16(i);
1283 res = smp_execute_task(dev, rri_req, RRI_REQ_SIZE, rri_resp,
1284 RRI_RESP_SIZE);
1285 if (res)
1286 goto out;
1287 res = rri_resp[2];
1288 if (res == SMP_RESP_NO_INDEX) {
1289 SAS_DPRINTK("overflow of indexes: dev %016llx "
1290 "phy 0x%x index 0x%x\n",
1291 SAS_ADDR(dev->sas_addr), phy_id, i);
1292 goto out;
1293 } else if (res != SMP_RESP_FUNC_ACC) {
1294 SAS_DPRINTK("%s: dev %016llx phy 0x%x index 0x%x "
1295 "result 0x%x\n", __func__,
1296 SAS_ADDR(dev->sas_addr), phy_id, i, res);
1297 goto out;
1299 if (SAS_ADDR(sas_addr) != 0) {
1300 if (SAS_ADDR(rri_resp+16) == SAS_ADDR(sas_addr)) {
1301 *index = i;
1302 if ((rri_resp[12] & 0x80) == 0x80)
1303 *present = 0;
1304 else
1305 *present = 1;
1306 goto out;
1307 } else if (SAS_ADDR(rri_resp+16) == 0) {
1308 *index = i;
1309 *present = 0;
1310 goto out;
1312 } else if (SAS_ADDR(rri_resp+16) == 0 &&
1313 phy->last_da_index < i) {
1314 phy->last_da_index = i;
1315 *index = i;
1316 *present = 0;
1317 goto out;
1320 res = -1;
1321 out:
1322 kfree(rri_req);
1323 kfree(rri_resp);
1324 return res;
1327 #define CRI_REQ_SIZE 44
1328 #define CRI_RESP_SIZE 8
1330 static int sas_configure_set(struct domain_device *dev, int phy_id,
1331 u8 *sas_addr, int index, int include)
1333 int res;
1334 u8 *cri_req;
1335 u8 *cri_resp;
1337 cri_req = alloc_smp_req(CRI_REQ_SIZE);
1338 if (!cri_req)
1339 return -ENOMEM;
1341 cri_resp = alloc_smp_resp(CRI_RESP_SIZE);
1342 if (!cri_resp) {
1343 kfree(cri_req);
1344 return -ENOMEM;
1347 cri_req[1] = SMP_CONF_ROUTE_INFO;
1348 *(__be16 *)(cri_req+6) = cpu_to_be16(index);
1349 cri_req[9] = phy_id;
1350 if (SAS_ADDR(sas_addr) == 0 || !include)
1351 cri_req[12] |= 0x80;
1352 memcpy(cri_req+16, sas_addr, SAS_ADDR_SIZE);
1354 res = smp_execute_task(dev, cri_req, CRI_REQ_SIZE, cri_resp,
1355 CRI_RESP_SIZE);
1356 if (res)
1357 goto out;
1358 res = cri_resp[2];
1359 if (res == SMP_RESP_NO_INDEX) {
1360 SAS_DPRINTK("overflow of indexes: dev %016llx phy 0x%x "
1361 "index 0x%x\n",
1362 SAS_ADDR(dev->sas_addr), phy_id, index);
1364 out:
1365 kfree(cri_req);
1366 kfree(cri_resp);
1367 return res;
1370 static int sas_configure_phy(struct domain_device *dev, int phy_id,
1371 u8 *sas_addr, int include)
1373 int index;
1374 int present;
1375 int res;
1377 res = sas_configure_present(dev, phy_id, sas_addr, &index, &present);
1378 if (res)
1379 return res;
1380 if (include ^ present)
1381 return sas_configure_set(dev, phy_id, sas_addr, index,include);
1383 return res;
1387 * sas_configure_parent -- configure routing table of parent
1388 * parent: parent expander
1389 * child: child expander
1390 * sas_addr: SAS port identifier of device directly attached to child
1392 static int sas_configure_parent(struct domain_device *parent,
1393 struct domain_device *child,
1394 u8 *sas_addr, int include)
1396 struct expander_device *ex_parent = &parent->ex_dev;
1397 int res = 0;
1398 int i;
1400 if (parent->parent) {
1401 res = sas_configure_parent(parent->parent, parent, sas_addr,
1402 include);
1403 if (res)
1404 return res;
1407 if (ex_parent->conf_route_table == 0) {
1408 SAS_DPRINTK("ex %016llx has self-configuring routing table\n",
1409 SAS_ADDR(parent->sas_addr));
1410 return 0;
1413 for (i = 0; i < ex_parent->num_phys; i++) {
1414 struct ex_phy *phy = &ex_parent->ex_phy[i];
1416 if ((phy->routing_attr == TABLE_ROUTING) &&
1417 (SAS_ADDR(phy->attached_sas_addr) ==
1418 SAS_ADDR(child->sas_addr))) {
1419 res = sas_configure_phy(parent, i, sas_addr, include);
1420 if (res)
1421 return res;
1425 return res;
1429 * sas_configure_routing -- configure routing
1430 * dev: expander device
1431 * sas_addr: port identifier of device directly attached to the expander device
1433 static int sas_configure_routing(struct domain_device *dev, u8 *sas_addr)
1435 if (dev->parent)
1436 return sas_configure_parent(dev->parent, dev, sas_addr, 1);
1437 return 0;
1440 static int sas_disable_routing(struct domain_device *dev, u8 *sas_addr)
1442 if (dev->parent)
1443 return sas_configure_parent(dev->parent, dev, sas_addr, 0);
1444 return 0;
1448 * sas_discover_expander -- expander discovery
1449 * @ex: pointer to expander domain device
1451 * See comment in sas_discover_sata().
1453 static int sas_discover_expander(struct domain_device *dev)
1455 int res;
1457 res = sas_notify_lldd_dev_found(dev);
1458 if (res)
1459 return res;
1461 res = sas_ex_general(dev);
1462 if (res)
1463 goto out_err;
1464 res = sas_ex_manuf_info(dev);
1465 if (res)
1466 goto out_err;
1468 res = sas_expander_discover(dev);
1469 if (res) {
1470 SAS_DPRINTK("expander %016llx discovery failed(0x%x)\n",
1471 SAS_ADDR(dev->sas_addr), res);
1472 goto out_err;
1475 sas_check_ex_subtractive_boundary(dev);
1476 res = sas_check_parent_topology(dev);
1477 if (res)
1478 goto out_err;
1479 return 0;
1480 out_err:
1481 sas_notify_lldd_dev_gone(dev);
1482 return res;
1485 static int sas_ex_level_discovery(struct asd_sas_port *port, const int level)
1487 int res = 0;
1488 struct domain_device *dev;
1490 list_for_each_entry(dev, &port->dev_list, dev_list_node) {
1491 if (dev->dev_type == EDGE_DEV ||
1492 dev->dev_type == FANOUT_DEV) {
1493 struct sas_expander_device *ex =
1494 rphy_to_expander_device(dev->rphy);
1496 if (level == ex->level)
1497 res = sas_ex_discover_devices(dev, -1);
1498 else if (level > 0)
1499 res = sas_ex_discover_devices(port->port_dev, -1);
1504 return res;
1507 static int sas_ex_bfs_disc(struct asd_sas_port *port)
1509 int res;
1510 int level;
1512 do {
1513 level = port->disc.max_level;
1514 res = sas_ex_level_discovery(port, level);
1515 mb();
1516 } while (level < port->disc.max_level);
1518 return res;
1521 int sas_discover_root_expander(struct domain_device *dev)
1523 int res;
1524 struct sas_expander_device *ex = rphy_to_expander_device(dev->rphy);
1526 res = sas_rphy_add(dev->rphy);
1527 if (res)
1528 goto out_err;
1530 ex->level = dev->port->disc.max_level; /* 0 */
1531 res = sas_discover_expander(dev);
1532 if (res)
1533 goto out_err2;
1535 sas_ex_bfs_disc(dev->port);
1537 return res;
1539 out_err2:
1540 sas_rphy_remove(dev->rphy);
1541 out_err:
1542 return res;
1545 /* ---------- Domain revalidation ---------- */
1547 static int sas_get_phy_discover(struct domain_device *dev,
1548 int phy_id, struct smp_resp *disc_resp)
1550 int res;
1551 u8 *disc_req;
1553 disc_req = alloc_smp_req(DISCOVER_REQ_SIZE);
1554 if (!disc_req)
1555 return -ENOMEM;
1557 disc_req[1] = SMP_DISCOVER;
1558 disc_req[9] = phy_id;
1560 res = smp_execute_task(dev, disc_req, DISCOVER_REQ_SIZE,
1561 disc_resp, DISCOVER_RESP_SIZE);
1562 if (res)
1563 goto out;
1564 else if (disc_resp->result != SMP_RESP_FUNC_ACC) {
1565 res = disc_resp->result;
1566 goto out;
1568 out:
1569 kfree(disc_req);
1570 return res;
1573 static int sas_get_phy_change_count(struct domain_device *dev,
1574 int phy_id, int *pcc)
1576 int res;
1577 struct smp_resp *disc_resp;
1579 disc_resp = alloc_smp_resp(DISCOVER_RESP_SIZE);
1580 if (!disc_resp)
1581 return -ENOMEM;
1583 res = sas_get_phy_discover(dev, phy_id, disc_resp);
1584 if (!res)
1585 *pcc = disc_resp->disc.change_count;
1587 kfree(disc_resp);
1588 return res;
1591 static int sas_get_phy_attached_sas_addr(struct domain_device *dev,
1592 int phy_id, u8 *attached_sas_addr)
1594 int res;
1595 struct smp_resp *disc_resp;
1596 struct discover_resp *dr;
1598 disc_resp = alloc_smp_resp(DISCOVER_RESP_SIZE);
1599 if (!disc_resp)
1600 return -ENOMEM;
1601 dr = &disc_resp->disc;
1603 res = sas_get_phy_discover(dev, phy_id, disc_resp);
1604 if (!res) {
1605 memcpy(attached_sas_addr,disc_resp->disc.attached_sas_addr,8);
1606 if (dr->attached_dev_type == 0)
1607 memset(attached_sas_addr, 0, 8);
1609 kfree(disc_resp);
1610 return res;
1613 static int sas_find_bcast_phy(struct domain_device *dev, int *phy_id,
1614 int from_phy, bool update)
1616 struct expander_device *ex = &dev->ex_dev;
1617 int res = 0;
1618 int i;
1620 for (i = from_phy; i < ex->num_phys; i++) {
1621 int phy_change_count = 0;
1623 res = sas_get_phy_change_count(dev, i, &phy_change_count);
1624 if (res)
1625 goto out;
1626 else if (phy_change_count != ex->ex_phy[i].phy_change_count) {
1627 if (update)
1628 ex->ex_phy[i].phy_change_count =
1629 phy_change_count;
1630 *phy_id = i;
1631 return 0;
1634 out:
1635 return res;
1638 static int sas_get_ex_change_count(struct domain_device *dev, int *ecc)
1640 int res;
1641 u8 *rg_req;
1642 struct smp_resp *rg_resp;
1644 rg_req = alloc_smp_req(RG_REQ_SIZE);
1645 if (!rg_req)
1646 return -ENOMEM;
1648 rg_resp = alloc_smp_resp(RG_RESP_SIZE);
1649 if (!rg_resp) {
1650 kfree(rg_req);
1651 return -ENOMEM;
1654 rg_req[1] = SMP_REPORT_GENERAL;
1656 res = smp_execute_task(dev, rg_req, RG_REQ_SIZE, rg_resp,
1657 RG_RESP_SIZE);
1658 if (res)
1659 goto out;
1660 if (rg_resp->result != SMP_RESP_FUNC_ACC) {
1661 res = rg_resp->result;
1662 goto out;
1665 *ecc = be16_to_cpu(rg_resp->rg.change_count);
1666 out:
1667 kfree(rg_resp);
1668 kfree(rg_req);
1669 return res;
1672 * sas_find_bcast_dev - find the device issue BROADCAST(CHANGE).
1673 * @dev:domain device to be detect.
1674 * @src_dev: the device which originated BROADCAST(CHANGE).
1676 * Add self-configuration expander suport. Suppose two expander cascading,
1677 * when the first level expander is self-configuring, hotplug the disks in
1678 * second level expander, BROADCAST(CHANGE) will not only be originated
1679 * in the second level expander, but also be originated in the first level
1680 * expander (see SAS protocol SAS 2r-14, 7.11 for detail), it is to say,
1681 * expander changed count in two level expanders will all increment at least
1682 * once, but the phy which chang count has changed is the source device which
1683 * we concerned.
1686 static int sas_find_bcast_dev(struct domain_device *dev,
1687 struct domain_device **src_dev)
1689 struct expander_device *ex = &dev->ex_dev;
1690 int ex_change_count = -1;
1691 int phy_id = -1;
1692 int res;
1693 struct domain_device *ch;
1695 res = sas_get_ex_change_count(dev, &ex_change_count);
1696 if (res)
1697 goto out;
1698 if (ex_change_count != -1 && ex_change_count != ex->ex_change_count) {
1699 /* Just detect if this expander phys phy change count changed,
1700 * in order to determine if this expander originate BROADCAST,
1701 * and do not update phy change count field in our structure.
1703 res = sas_find_bcast_phy(dev, &phy_id, 0, false);
1704 if (phy_id != -1) {
1705 *src_dev = dev;
1706 ex->ex_change_count = ex_change_count;
1707 SAS_DPRINTK("Expander phy change count has changed\n");
1708 return res;
1709 } else
1710 SAS_DPRINTK("Expander phys DID NOT change\n");
1712 list_for_each_entry(ch, &ex->children, siblings) {
1713 if (ch->dev_type == EDGE_DEV || ch->dev_type == FANOUT_DEV) {
1714 res = sas_find_bcast_dev(ch, src_dev);
1715 if (*src_dev)
1716 return res;
1719 out:
1720 return res;
1723 static void sas_unregister_ex_tree(struct domain_device *dev)
1725 struct expander_device *ex = &dev->ex_dev;
1726 struct domain_device *child, *n;
1728 list_for_each_entry_safe(child, n, &ex->children, siblings) {
1729 if (child->dev_type == EDGE_DEV ||
1730 child->dev_type == FANOUT_DEV)
1731 sas_unregister_ex_tree(child);
1732 else
1733 sas_unregister_dev(child);
1735 sas_unregister_dev(dev);
1738 static void sas_unregister_devs_sas_addr(struct domain_device *parent,
1739 int phy_id, bool last)
1741 struct expander_device *ex_dev = &parent->ex_dev;
1742 struct ex_phy *phy = &ex_dev->ex_phy[phy_id];
1743 struct domain_device *child, *n;
1744 if (last) {
1745 list_for_each_entry_safe(child, n,
1746 &ex_dev->children, siblings) {
1747 if (SAS_ADDR(child->sas_addr) ==
1748 SAS_ADDR(phy->attached_sas_addr)) {
1749 if (child->dev_type == EDGE_DEV ||
1750 child->dev_type == FANOUT_DEV)
1751 sas_unregister_ex_tree(child);
1752 else
1753 sas_unregister_dev(child);
1754 break;
1757 sas_disable_routing(parent, phy->attached_sas_addr);
1759 memset(phy->attached_sas_addr, 0, SAS_ADDR_SIZE);
1760 if (phy->port) {
1761 sas_port_delete_phy(phy->port, phy->phy);
1762 if (phy->port->num_phys == 0)
1763 sas_port_delete(phy->port);
1764 phy->port = NULL;
1768 static int sas_discover_bfs_by_root_level(struct domain_device *root,
1769 const int level)
1771 struct expander_device *ex_root = &root->ex_dev;
1772 struct domain_device *child;
1773 int res = 0;
1775 list_for_each_entry(child, &ex_root->children, siblings) {
1776 if (child->dev_type == EDGE_DEV ||
1777 child->dev_type == FANOUT_DEV) {
1778 struct sas_expander_device *ex =
1779 rphy_to_expander_device(child->rphy);
1781 if (level > ex->level)
1782 res = sas_discover_bfs_by_root_level(child,
1783 level);
1784 else if (level == ex->level)
1785 res = sas_ex_discover_devices(child, -1);
1788 return res;
1791 static int sas_discover_bfs_by_root(struct domain_device *dev)
1793 int res;
1794 struct sas_expander_device *ex = rphy_to_expander_device(dev->rphy);
1795 int level = ex->level+1;
1797 res = sas_ex_discover_devices(dev, -1);
1798 if (res)
1799 goto out;
1800 do {
1801 res = sas_discover_bfs_by_root_level(dev, level);
1802 mb();
1803 level += 1;
1804 } while (level <= dev->port->disc.max_level);
1805 out:
1806 return res;
1809 static int sas_discover_new(struct domain_device *dev, int phy_id)
1811 struct ex_phy *ex_phy = &dev->ex_dev.ex_phy[phy_id];
1812 struct domain_device *child;
1813 int res;
1815 SAS_DPRINTK("ex %016llx phy%d new device attached\n",
1816 SAS_ADDR(dev->sas_addr), phy_id);
1817 res = sas_ex_phy_discover(dev, phy_id);
1818 if (res)
1819 return res;
1821 if (sas_ex_join_wide_port(dev, phy_id))
1822 return 0;
1824 res = sas_ex_discover_devices(dev, phy_id);
1825 if (res)
1826 return res;
1827 list_for_each_entry(child, &dev->ex_dev.children, siblings) {
1828 if (SAS_ADDR(child->sas_addr) ==
1829 SAS_ADDR(ex_phy->attached_sas_addr)) {
1830 if (child->dev_type == EDGE_DEV ||
1831 child->dev_type == FANOUT_DEV)
1832 res = sas_discover_bfs_by_root(child);
1833 break;
1836 return res;
1839 static int sas_rediscover_dev(struct domain_device *dev, int phy_id, bool last)
1841 struct expander_device *ex = &dev->ex_dev;
1842 struct ex_phy *phy = &ex->ex_phy[phy_id];
1843 u8 attached_sas_addr[8];
1844 int res;
1846 res = sas_get_phy_attached_sas_addr(dev, phy_id, attached_sas_addr);
1847 switch (res) {
1848 case SMP_RESP_NO_PHY:
1849 phy->phy_state = PHY_NOT_PRESENT;
1850 sas_unregister_devs_sas_addr(dev, phy_id, last);
1851 goto out; break;
1852 case SMP_RESP_PHY_VACANT:
1853 phy->phy_state = PHY_VACANT;
1854 sas_unregister_devs_sas_addr(dev, phy_id, last);
1855 goto out; break;
1856 case SMP_RESP_FUNC_ACC:
1857 break;
1860 if (SAS_ADDR(attached_sas_addr) == 0) {
1861 phy->phy_state = PHY_EMPTY;
1862 sas_unregister_devs_sas_addr(dev, phy_id, last);
1863 } else if (SAS_ADDR(attached_sas_addr) ==
1864 SAS_ADDR(phy->attached_sas_addr)) {
1865 SAS_DPRINTK("ex %016llx phy 0x%x broadcast flutter\n",
1866 SAS_ADDR(dev->sas_addr), phy_id);
1867 sas_ex_phy_discover(dev, phy_id);
1868 } else
1869 res = sas_discover_new(dev, phy_id);
1870 out:
1871 return res;
1875 * sas_rediscover - revalidate the domain.
1876 * @dev:domain device to be detect.
1877 * @phy_id: the phy id will be detected.
1879 * NOTE: this process _must_ quit (return) as soon as any connection
1880 * errors are encountered. Connection recovery is done elsewhere.
1881 * Discover process only interrogates devices in order to discover the
1882 * domain.For plugging out, we un-register the device only when it is
1883 * the last phy in the port, for other phys in this port, we just delete it
1884 * from the port.For inserting, we do discovery when it is the
1885 * first phy,for other phys in this port, we add it to the port to
1886 * forming the wide-port.
1888 static int sas_rediscover(struct domain_device *dev, const int phy_id)
1890 struct expander_device *ex = &dev->ex_dev;
1891 struct ex_phy *changed_phy = &ex->ex_phy[phy_id];
1892 int res = 0;
1893 int i;
1894 bool last = true; /* is this the last phy of the port */
1896 SAS_DPRINTK("ex %016llx phy%d originated BROADCAST(CHANGE)\n",
1897 SAS_ADDR(dev->sas_addr), phy_id);
1899 if (SAS_ADDR(changed_phy->attached_sas_addr) != 0) {
1900 for (i = 0; i < ex->num_phys; i++) {
1901 struct ex_phy *phy = &ex->ex_phy[i];
1903 if (i == phy_id)
1904 continue;
1905 if (SAS_ADDR(phy->attached_sas_addr) ==
1906 SAS_ADDR(changed_phy->attached_sas_addr)) {
1907 SAS_DPRINTK("phy%d part of wide port with "
1908 "phy%d\n", phy_id, i);
1909 last = false;
1910 break;
1913 res = sas_rediscover_dev(dev, phy_id, last);
1914 } else
1915 res = sas_discover_new(dev, phy_id);
1916 return res;
1920 * sas_revalidate_domain -- revalidate the domain
1921 * @port: port to the domain of interest
1923 * NOTE: this process _must_ quit (return) as soon as any connection
1924 * errors are encountered. Connection recovery is done elsewhere.
1925 * Discover process only interrogates devices in order to discover the
1926 * domain.
1928 int sas_ex_revalidate_domain(struct domain_device *port_dev)
1930 int res;
1931 struct domain_device *dev = NULL;
1933 res = sas_find_bcast_dev(port_dev, &dev);
1934 while (res == 0 && dev) {
1935 struct expander_device *ex = &dev->ex_dev;
1936 int i = 0, phy_id;
1938 do {
1939 phy_id = -1;
1940 res = sas_find_bcast_phy(dev, &phy_id, i, true);
1941 if (phy_id == -1)
1942 break;
1943 res = sas_rediscover(dev, phy_id);
1944 i = phy_id + 1;
1945 } while (i < ex->num_phys);
1947 dev = NULL;
1948 res = sas_find_bcast_dev(port_dev, &dev);
1950 return res;
1953 int sas_smp_handler(struct Scsi_Host *shost, struct sas_rphy *rphy,
1954 struct request *req)
1956 struct domain_device *dev;
1957 int ret, type;
1958 struct request *rsp = req->next_rq;
1960 if (!rsp) {
1961 printk("%s: space for a smp response is missing\n",
1962 __func__);
1963 return -EINVAL;
1966 /* no rphy means no smp target support (ie aic94xx host) */
1967 if (!rphy)
1968 return sas_smp_host_handler(shost, req, rsp);
1970 type = rphy->identify.device_type;
1972 if (type != SAS_EDGE_EXPANDER_DEVICE &&
1973 type != SAS_FANOUT_EXPANDER_DEVICE) {
1974 printk("%s: can we send a smp request to a device?\n",
1975 __func__);
1976 return -EINVAL;
1979 dev = sas_find_dev_by_rphy(rphy);
1980 if (!dev) {
1981 printk("%s: fail to find a domain_device?\n", __func__);
1982 return -EINVAL;
1985 /* do we need to support multiple segments? */
1986 if (req->bio->bi_vcnt > 1 || rsp->bio->bi_vcnt > 1) {
1987 printk("%s: multiple segments req %u %u, rsp %u %u\n",
1988 __func__, req->bio->bi_vcnt, blk_rq_bytes(req),
1989 rsp->bio->bi_vcnt, blk_rq_bytes(rsp));
1990 return -EINVAL;
1993 ret = smp_execute_task(dev, bio_data(req->bio), blk_rq_bytes(req),
1994 bio_data(rsp->bio), blk_rq_bytes(rsp));
1995 if (ret > 0) {
1996 /* positive number is the untransferred residual */
1997 rsp->resid_len = ret;
1998 req->resid_len = 0;
1999 ret = 0;
2000 } else if (ret == 0) {
2001 rsp->resid_len = 0;
2002 req->resid_len = 0;
2005 return ret;