[SCSI] libsas: improve debug statements
[linux-2.6.git] / drivers / scsi / libsas / sas_expander.c
blob4b2ecd35dc5ada1172200eca57e24b260a7c5f33
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>
27 #include <linux/slab.h>
29 #include "sas_internal.h"
31 #include <scsi/sas_ata.h>
32 #include <scsi/scsi_transport.h>
33 #include <scsi/scsi_transport_sas.h>
34 #include "../scsi_sas_internal.h"
36 static int sas_discover_expander(struct domain_device *dev);
37 static int sas_configure_routing(struct domain_device *dev, u8 *sas_addr);
38 static int sas_configure_phy(struct domain_device *dev, int phy_id,
39 u8 *sas_addr, int include);
40 static int sas_disable_routing(struct domain_device *dev, u8 *sas_addr);
42 /* ---------- SMP task management ---------- */
44 static void smp_task_timedout(unsigned long _task)
46 struct sas_task *task = (void *) _task;
47 unsigned long flags;
49 spin_lock_irqsave(&task->task_state_lock, flags);
50 if (!(task->task_state_flags & SAS_TASK_STATE_DONE))
51 task->task_state_flags |= SAS_TASK_STATE_ABORTED;
52 spin_unlock_irqrestore(&task->task_state_lock, flags);
54 complete(&task->completion);
57 static void smp_task_done(struct sas_task *task)
59 if (!del_timer(&task->timer))
60 return;
61 complete(&task->completion);
64 /* Give it some long enough timeout. In seconds. */
65 #define SMP_TIMEOUT 10
67 static int smp_execute_task(struct domain_device *dev, void *req, int req_size,
68 void *resp, int resp_size)
70 int res, retry;
71 struct sas_task *task = NULL;
72 struct sas_internal *i =
73 to_sas_internal(dev->port->ha->core.shost->transportt);
75 mutex_lock(&dev->ex_dev.cmd_mutex);
76 for (retry = 0; retry < 3; retry++) {
77 if (test_bit(SAS_DEV_GONE, &dev->state)) {
78 res = -ECOMM;
79 break;
82 task = sas_alloc_task(GFP_KERNEL);
83 if (!task) {
84 res = -ENOMEM;
85 break;
87 task->dev = dev;
88 task->task_proto = dev->tproto;
89 sg_init_one(&task->smp_task.smp_req, req, req_size);
90 sg_init_one(&task->smp_task.smp_resp, resp, resp_size);
92 task->task_done = smp_task_done;
94 task->timer.data = (unsigned long) task;
95 task->timer.function = smp_task_timedout;
96 task->timer.expires = jiffies + SMP_TIMEOUT*HZ;
97 add_timer(&task->timer);
99 res = i->dft->lldd_execute_task(task, 1, GFP_KERNEL);
101 if (res) {
102 del_timer(&task->timer);
103 SAS_DPRINTK("executing SMP task failed:%d\n", res);
104 break;
107 wait_for_completion(&task->completion);
108 res = -ECOMM;
109 if ((task->task_state_flags & SAS_TASK_STATE_ABORTED)) {
110 SAS_DPRINTK("smp task timed out or aborted\n");
111 i->dft->lldd_abort_task(task);
112 if (!(task->task_state_flags & SAS_TASK_STATE_DONE)) {
113 SAS_DPRINTK("SMP task aborted and not done\n");
114 break;
117 if (task->task_status.resp == SAS_TASK_COMPLETE &&
118 task->task_status.stat == SAM_STAT_GOOD) {
119 res = 0;
120 break;
122 if (task->task_status.resp == SAS_TASK_COMPLETE &&
123 task->task_status.stat == SAS_DATA_UNDERRUN) {
124 /* no error, but return the number of bytes of
125 * underrun */
126 res = task->task_status.residual;
127 break;
129 if (task->task_status.resp == SAS_TASK_COMPLETE &&
130 task->task_status.stat == SAS_DATA_OVERRUN) {
131 res = -EMSGSIZE;
132 break;
134 if (task->task_status.resp == SAS_TASK_UNDELIVERED &&
135 task->task_status.stat == SAS_DEVICE_UNKNOWN)
136 break;
137 else {
138 SAS_DPRINTK("%s: task to dev %016llx response: 0x%x "
139 "status 0x%x\n", __func__,
140 SAS_ADDR(dev->sas_addr),
141 task->task_status.resp,
142 task->task_status.stat);
143 sas_free_task(task);
144 task = NULL;
147 mutex_unlock(&dev->ex_dev.cmd_mutex);
149 BUG_ON(retry == 3 && task != NULL);
150 sas_free_task(task);
151 return res;
154 /* ---------- Allocations ---------- */
156 static inline void *alloc_smp_req(int size)
158 u8 *p = kzalloc(size, GFP_KERNEL);
159 if (p)
160 p[0] = SMP_REQUEST;
161 return p;
164 static inline void *alloc_smp_resp(int size)
166 return kzalloc(size, GFP_KERNEL);
169 static char sas_route_char(struct domain_device *dev, struct ex_phy *phy)
171 switch (phy->routing_attr) {
172 case TABLE_ROUTING:
173 if (dev->ex_dev.t2t_supp)
174 return 'U';
175 else
176 return 'T';
177 case DIRECT_ROUTING:
178 return 'D';
179 case SUBTRACTIVE_ROUTING:
180 return 'S';
181 default:
182 return '?';
186 static void sas_set_ex_phy(struct domain_device *dev, int phy_id,
187 void *disc_resp)
189 struct expander_device *ex = &dev->ex_dev;
190 struct ex_phy *phy = &ex->ex_phy[phy_id];
191 struct smp_resp *resp = disc_resp;
192 struct discover_resp *dr = &resp->disc;
193 struct sas_rphy *rphy = dev->rphy;
194 bool new_phy = !phy->phy;
195 char *type;
197 if (new_phy) {
198 phy->phy = sas_phy_alloc(&rphy->dev, phy_id);
200 /* FIXME: error_handling */
201 BUG_ON(!phy->phy);
204 switch (resp->result) {
205 case SMP_RESP_PHY_VACANT:
206 phy->phy_state = PHY_VACANT;
207 break;
208 default:
209 phy->phy_state = PHY_NOT_PRESENT;
210 break;
211 case SMP_RESP_FUNC_ACC:
212 phy->phy_state = PHY_EMPTY; /* do not know yet */
213 break;
216 phy->phy_id = phy_id;
217 phy->attached_dev_type = dr->attached_dev_type;
218 phy->linkrate = dr->linkrate;
219 phy->attached_sata_host = dr->attached_sata_host;
220 phy->attached_sata_dev = dr->attached_sata_dev;
221 phy->attached_sata_ps = dr->attached_sata_ps;
222 phy->attached_iproto = dr->iproto << 1;
223 phy->attached_tproto = dr->tproto << 1;
224 memcpy(phy->attached_sas_addr, dr->attached_sas_addr, SAS_ADDR_SIZE);
225 phy->attached_phy_id = dr->attached_phy_id;
226 phy->phy_change_count = dr->change_count;
227 phy->routing_attr = dr->routing_attr;
228 phy->virtual = dr->virtual;
229 phy->last_da_index = -1;
231 phy->phy->identify.sas_address = SAS_ADDR(phy->attached_sas_addr);
232 phy->phy->identify.device_type = phy->attached_dev_type;
233 phy->phy->identify.initiator_port_protocols = phy->attached_iproto;
234 phy->phy->identify.target_port_protocols = phy->attached_tproto;
235 phy->phy->identify.phy_identifier = phy_id;
236 phy->phy->minimum_linkrate_hw = dr->hmin_linkrate;
237 phy->phy->maximum_linkrate_hw = dr->hmax_linkrate;
238 phy->phy->minimum_linkrate = dr->pmin_linkrate;
239 phy->phy->maximum_linkrate = dr->pmax_linkrate;
240 phy->phy->negotiated_linkrate = phy->linkrate;
242 if (new_phy)
243 if (sas_phy_add(phy->phy)) {
244 sas_phy_free(phy->phy);
245 return;
248 switch (phy->attached_dev_type) {
249 case NO_DEVICE:
250 type = "no device";
251 break;
252 case SAS_END_DEV:
253 if (phy->attached_iproto) {
254 if (phy->attached_tproto)
255 type = "host+target";
256 else
257 type = "host";
258 } else {
259 if (dr->attached_sata_dev)
260 type = "stp";
261 else
262 type = "ssp";
264 break;
265 case EDGE_DEV:
266 case FANOUT_DEV:
267 type = "smp";
268 break;
269 default:
270 type = "unknown";
273 SAS_DPRINTK("ex %016llx phy%02d:%c:%X attached: %016llx (%s)\n",
274 SAS_ADDR(dev->sas_addr), phy->phy_id,
275 sas_route_char(dev, phy), phy->linkrate,
276 SAS_ADDR(phy->attached_sas_addr), type);
279 /* check if we have an existing attached ata device on this expander phy */
280 struct domain_device *sas_ex_to_ata(struct domain_device *ex_dev, int phy_id)
282 struct ex_phy *ex_phy = &ex_dev->ex_dev.ex_phy[phy_id];
283 struct domain_device *dev;
284 struct sas_rphy *rphy;
286 if (!ex_phy->port)
287 return NULL;
289 rphy = ex_phy->port->rphy;
290 if (!rphy)
291 return NULL;
293 dev = sas_find_dev_by_rphy(rphy);
295 if (dev && dev_is_sata(dev))
296 return dev;
298 return NULL;
301 #define DISCOVER_REQ_SIZE 16
302 #define DISCOVER_RESP_SIZE 56
304 static int sas_ex_phy_discover_helper(struct domain_device *dev, u8 *disc_req,
305 u8 *disc_resp, int single)
307 struct domain_device *ata_dev = sas_ex_to_ata(dev, single);
308 int i, res;
310 disc_req[9] = single;
311 for (i = 1 ; i < 3; i++) {
312 struct discover_resp *dr;
314 res = smp_execute_task(dev, disc_req, DISCOVER_REQ_SIZE,
315 disc_resp, DISCOVER_RESP_SIZE);
316 if (res)
317 return res;
318 dr = &((struct smp_resp *)disc_resp)->disc;
319 if (memcmp(dev->sas_addr, dr->attached_sas_addr,
320 SAS_ADDR_SIZE) == 0) {
321 sas_printk("Found loopback topology, just ignore it!\n");
322 return 0;
325 /* This is detecting a failure to transmit initial
326 * dev to host FIS as described in section J.5 of
327 * sas-2 r16
329 if (!(dr->attached_dev_type == 0 &&
330 dr->attached_sata_dev))
331 break;
333 /* In order to generate the dev to host FIS, we send a
334 * link reset to the expander port. If a device was
335 * previously detected on this port we ask libata to
336 * manage the reset and link recovery.
338 if (ata_dev) {
339 sas_ata_schedule_reset(ata_dev);
340 break;
342 sas_smp_phy_control(dev, single, PHY_FUNC_LINK_RESET, NULL);
343 /* Wait for the reset to trigger the negotiation */
344 msleep(500);
346 sas_set_ex_phy(dev, single, disc_resp);
347 return 0;
350 static int sas_ex_phy_discover(struct domain_device *dev, int single)
352 struct expander_device *ex = &dev->ex_dev;
353 int res = 0;
354 u8 *disc_req;
355 u8 *disc_resp;
357 disc_req = alloc_smp_req(DISCOVER_REQ_SIZE);
358 if (!disc_req)
359 return -ENOMEM;
361 disc_resp = alloc_smp_req(DISCOVER_RESP_SIZE);
362 if (!disc_resp) {
363 kfree(disc_req);
364 return -ENOMEM;
367 disc_req[1] = SMP_DISCOVER;
369 if (0 <= single && single < ex->num_phys) {
370 res = sas_ex_phy_discover_helper(dev, disc_req, disc_resp, single);
371 } else {
372 int i;
374 for (i = 0; i < ex->num_phys; i++) {
375 res = sas_ex_phy_discover_helper(dev, disc_req,
376 disc_resp, i);
377 if (res)
378 goto out_err;
381 out_err:
382 kfree(disc_resp);
383 kfree(disc_req);
384 return res;
387 static int sas_expander_discover(struct domain_device *dev)
389 struct expander_device *ex = &dev->ex_dev;
390 int res = -ENOMEM;
392 ex->ex_phy = kzalloc(sizeof(*ex->ex_phy)*ex->num_phys, GFP_KERNEL);
393 if (!ex->ex_phy)
394 return -ENOMEM;
396 res = sas_ex_phy_discover(dev, -1);
397 if (res)
398 goto out_err;
400 return 0;
401 out_err:
402 kfree(ex->ex_phy);
403 ex->ex_phy = NULL;
404 return res;
407 #define MAX_EXPANDER_PHYS 128
409 static void ex_assign_report_general(struct domain_device *dev,
410 struct smp_resp *resp)
412 struct report_general_resp *rg = &resp->rg;
414 dev->ex_dev.ex_change_count = be16_to_cpu(rg->change_count);
415 dev->ex_dev.max_route_indexes = be16_to_cpu(rg->route_indexes);
416 dev->ex_dev.num_phys = min(rg->num_phys, (u8)MAX_EXPANDER_PHYS);
417 dev->ex_dev.t2t_supp = rg->t2t_supp;
418 dev->ex_dev.conf_route_table = rg->conf_route_table;
419 dev->ex_dev.configuring = rg->configuring;
420 memcpy(dev->ex_dev.enclosure_logical_id, rg->enclosure_logical_id, 8);
423 #define RG_REQ_SIZE 8
424 #define RG_RESP_SIZE 32
426 static int sas_ex_general(struct domain_device *dev)
428 u8 *rg_req;
429 struct smp_resp *rg_resp;
430 int res;
431 int i;
433 rg_req = alloc_smp_req(RG_REQ_SIZE);
434 if (!rg_req)
435 return -ENOMEM;
437 rg_resp = alloc_smp_resp(RG_RESP_SIZE);
438 if (!rg_resp) {
439 kfree(rg_req);
440 return -ENOMEM;
443 rg_req[1] = SMP_REPORT_GENERAL;
445 for (i = 0; i < 5; i++) {
446 res = smp_execute_task(dev, rg_req, RG_REQ_SIZE, rg_resp,
447 RG_RESP_SIZE);
449 if (res) {
450 SAS_DPRINTK("RG to ex %016llx failed:0x%x\n",
451 SAS_ADDR(dev->sas_addr), res);
452 goto out;
453 } else if (rg_resp->result != SMP_RESP_FUNC_ACC) {
454 SAS_DPRINTK("RG:ex %016llx returned SMP result:0x%x\n",
455 SAS_ADDR(dev->sas_addr), rg_resp->result);
456 res = rg_resp->result;
457 goto out;
460 ex_assign_report_general(dev, rg_resp);
462 if (dev->ex_dev.configuring) {
463 SAS_DPRINTK("RG: ex %llx self-configuring...\n",
464 SAS_ADDR(dev->sas_addr));
465 schedule_timeout_interruptible(5*HZ);
466 } else
467 break;
469 out:
470 kfree(rg_req);
471 kfree(rg_resp);
472 return res;
475 static void ex_assign_manuf_info(struct domain_device *dev, void
476 *_mi_resp)
478 u8 *mi_resp = _mi_resp;
479 struct sas_rphy *rphy = dev->rphy;
480 struct sas_expander_device *edev = rphy_to_expander_device(rphy);
482 memcpy(edev->vendor_id, mi_resp + 12, SAS_EXPANDER_VENDOR_ID_LEN);
483 memcpy(edev->product_id, mi_resp + 20, SAS_EXPANDER_PRODUCT_ID_LEN);
484 memcpy(edev->product_rev, mi_resp + 36,
485 SAS_EXPANDER_PRODUCT_REV_LEN);
487 if (mi_resp[8] & 1) {
488 memcpy(edev->component_vendor_id, mi_resp + 40,
489 SAS_EXPANDER_COMPONENT_VENDOR_ID_LEN);
490 edev->component_id = mi_resp[48] << 8 | mi_resp[49];
491 edev->component_revision_id = mi_resp[50];
495 #define MI_REQ_SIZE 8
496 #define MI_RESP_SIZE 64
498 static int sas_ex_manuf_info(struct domain_device *dev)
500 u8 *mi_req;
501 u8 *mi_resp;
502 int res;
504 mi_req = alloc_smp_req(MI_REQ_SIZE);
505 if (!mi_req)
506 return -ENOMEM;
508 mi_resp = alloc_smp_resp(MI_RESP_SIZE);
509 if (!mi_resp) {
510 kfree(mi_req);
511 return -ENOMEM;
514 mi_req[1] = SMP_REPORT_MANUF_INFO;
516 res = smp_execute_task(dev, mi_req, MI_REQ_SIZE, mi_resp,MI_RESP_SIZE);
517 if (res) {
518 SAS_DPRINTK("MI: ex %016llx failed:0x%x\n",
519 SAS_ADDR(dev->sas_addr), res);
520 goto out;
521 } else if (mi_resp[2] != SMP_RESP_FUNC_ACC) {
522 SAS_DPRINTK("MI ex %016llx returned SMP result:0x%x\n",
523 SAS_ADDR(dev->sas_addr), mi_resp[2]);
524 goto out;
527 ex_assign_manuf_info(dev, mi_resp);
528 out:
529 kfree(mi_req);
530 kfree(mi_resp);
531 return res;
534 #define PC_REQ_SIZE 44
535 #define PC_RESP_SIZE 8
537 int sas_smp_phy_control(struct domain_device *dev, int phy_id,
538 enum phy_func phy_func,
539 struct sas_phy_linkrates *rates)
541 u8 *pc_req;
542 u8 *pc_resp;
543 int res;
545 pc_req = alloc_smp_req(PC_REQ_SIZE);
546 if (!pc_req)
547 return -ENOMEM;
549 pc_resp = alloc_smp_resp(PC_RESP_SIZE);
550 if (!pc_resp) {
551 kfree(pc_req);
552 return -ENOMEM;
555 pc_req[1] = SMP_PHY_CONTROL;
556 pc_req[9] = phy_id;
557 pc_req[10]= phy_func;
558 if (rates) {
559 pc_req[32] = rates->minimum_linkrate << 4;
560 pc_req[33] = rates->maximum_linkrate << 4;
563 res = smp_execute_task(dev, pc_req, PC_REQ_SIZE, pc_resp,PC_RESP_SIZE);
565 kfree(pc_resp);
566 kfree(pc_req);
567 return res;
570 static void sas_ex_disable_phy(struct domain_device *dev, int phy_id)
572 struct expander_device *ex = &dev->ex_dev;
573 struct ex_phy *phy = &ex->ex_phy[phy_id];
575 sas_smp_phy_control(dev, phy_id, PHY_FUNC_DISABLE, NULL);
576 phy->linkrate = SAS_PHY_DISABLED;
579 static void sas_ex_disable_port(struct domain_device *dev, u8 *sas_addr)
581 struct expander_device *ex = &dev->ex_dev;
582 int i;
584 for (i = 0; i < ex->num_phys; i++) {
585 struct ex_phy *phy = &ex->ex_phy[i];
587 if (phy->phy_state == PHY_VACANT ||
588 phy->phy_state == PHY_NOT_PRESENT)
589 continue;
591 if (SAS_ADDR(phy->attached_sas_addr) == SAS_ADDR(sas_addr))
592 sas_ex_disable_phy(dev, i);
596 static int sas_dev_present_in_domain(struct asd_sas_port *port,
597 u8 *sas_addr)
599 struct domain_device *dev;
601 if (SAS_ADDR(port->sas_addr) == SAS_ADDR(sas_addr))
602 return 1;
603 list_for_each_entry(dev, &port->dev_list, dev_list_node) {
604 if (SAS_ADDR(dev->sas_addr) == SAS_ADDR(sas_addr))
605 return 1;
607 return 0;
610 #define RPEL_REQ_SIZE 16
611 #define RPEL_RESP_SIZE 32
612 int sas_smp_get_phy_events(struct sas_phy *phy)
614 int res;
615 u8 *req;
616 u8 *resp;
617 struct sas_rphy *rphy = dev_to_rphy(phy->dev.parent);
618 struct domain_device *dev = sas_find_dev_by_rphy(rphy);
620 req = alloc_smp_req(RPEL_REQ_SIZE);
621 if (!req)
622 return -ENOMEM;
624 resp = alloc_smp_resp(RPEL_RESP_SIZE);
625 if (!resp) {
626 kfree(req);
627 return -ENOMEM;
630 req[1] = SMP_REPORT_PHY_ERR_LOG;
631 req[9] = phy->number;
633 res = smp_execute_task(dev, req, RPEL_REQ_SIZE,
634 resp, RPEL_RESP_SIZE);
636 if (!res)
637 goto out;
639 phy->invalid_dword_count = scsi_to_u32(&resp[12]);
640 phy->running_disparity_error_count = scsi_to_u32(&resp[16]);
641 phy->loss_of_dword_sync_count = scsi_to_u32(&resp[20]);
642 phy->phy_reset_problem_count = scsi_to_u32(&resp[24]);
644 out:
645 kfree(resp);
646 return res;
650 #ifdef CONFIG_SCSI_SAS_ATA
652 #define RPS_REQ_SIZE 16
653 #define RPS_RESP_SIZE 60
655 static int sas_get_report_phy_sata(struct domain_device *dev,
656 int phy_id,
657 struct smp_resp *rps_resp)
659 int res;
660 u8 *rps_req = alloc_smp_req(RPS_REQ_SIZE);
661 u8 *resp = (u8 *)rps_resp;
663 if (!rps_req)
664 return -ENOMEM;
666 rps_req[1] = SMP_REPORT_PHY_SATA;
667 rps_req[9] = phy_id;
669 res = smp_execute_task(dev, rps_req, RPS_REQ_SIZE,
670 rps_resp, RPS_RESP_SIZE);
672 /* 0x34 is the FIS type for the D2H fis. There's a potential
673 * standards cockup here. sas-2 explicitly specifies the FIS
674 * should be encoded so that FIS type is in resp[24].
675 * However, some expanders endian reverse this. Undo the
676 * reversal here */
677 if (!res && resp[27] == 0x34 && resp[24] != 0x34) {
678 int i;
680 for (i = 0; i < 5; i++) {
681 int j = 24 + (i*4);
682 u8 a, b;
683 a = resp[j + 0];
684 b = resp[j + 1];
685 resp[j + 0] = resp[j + 3];
686 resp[j + 1] = resp[j + 2];
687 resp[j + 2] = b;
688 resp[j + 3] = a;
692 kfree(rps_req);
693 return res;
695 #endif
697 static void sas_ex_get_linkrate(struct domain_device *parent,
698 struct domain_device *child,
699 struct ex_phy *parent_phy)
701 struct expander_device *parent_ex = &parent->ex_dev;
702 struct sas_port *port;
703 int i;
705 child->pathways = 0;
707 port = parent_phy->port;
709 for (i = 0; i < parent_ex->num_phys; i++) {
710 struct ex_phy *phy = &parent_ex->ex_phy[i];
712 if (phy->phy_state == PHY_VACANT ||
713 phy->phy_state == PHY_NOT_PRESENT)
714 continue;
716 if (SAS_ADDR(phy->attached_sas_addr) ==
717 SAS_ADDR(child->sas_addr)) {
719 child->min_linkrate = min(parent->min_linkrate,
720 phy->linkrate);
721 child->max_linkrate = max(parent->max_linkrate,
722 phy->linkrate);
723 child->pathways++;
724 sas_port_add_phy(port, phy->phy);
727 child->linkrate = min(parent_phy->linkrate, child->max_linkrate);
728 child->pathways = min(child->pathways, parent->pathways);
731 static struct domain_device *sas_ex_discover_end_dev(
732 struct domain_device *parent, int phy_id)
734 struct expander_device *parent_ex = &parent->ex_dev;
735 struct ex_phy *phy = &parent_ex->ex_phy[phy_id];
736 struct domain_device *child = NULL;
737 struct sas_rphy *rphy;
738 int res;
740 if (phy->attached_sata_host || phy->attached_sata_ps)
741 return NULL;
743 child = sas_alloc_device();
744 if (!child)
745 return NULL;
747 kref_get(&parent->kref);
748 child->parent = parent;
749 child->port = parent->port;
750 child->iproto = phy->attached_iproto;
751 memcpy(child->sas_addr, phy->attached_sas_addr, SAS_ADDR_SIZE);
752 sas_hash_addr(child->hashed_sas_addr, child->sas_addr);
753 if (!phy->port) {
754 phy->port = sas_port_alloc(&parent->rphy->dev, phy_id);
755 if (unlikely(!phy->port))
756 goto out_err;
757 if (unlikely(sas_port_add(phy->port) != 0)) {
758 sas_port_free(phy->port);
759 goto out_err;
762 sas_ex_get_linkrate(parent, child, phy);
763 sas_device_set_phy(child, phy->port);
765 #ifdef CONFIG_SCSI_SAS_ATA
766 if ((phy->attached_tproto & SAS_PROTOCOL_STP) || phy->attached_sata_dev) {
767 child->dev_type = SATA_DEV;
768 if (phy->attached_tproto & SAS_PROTOCOL_STP)
769 child->tproto = phy->attached_tproto;
770 if (phy->attached_sata_dev)
771 child->tproto |= SATA_DEV;
772 res = sas_get_report_phy_sata(parent, phy_id,
773 &child->sata_dev.rps_resp);
774 if (res) {
775 SAS_DPRINTK("report phy sata to %016llx:0x%x returned "
776 "0x%x\n", SAS_ADDR(parent->sas_addr),
777 phy_id, res);
778 goto out_free;
780 memcpy(child->frame_rcvd, &child->sata_dev.rps_resp.rps.fis,
781 sizeof(struct dev_to_host_fis));
783 rphy = sas_end_device_alloc(phy->port);
784 if (unlikely(!rphy))
785 goto out_free;
787 sas_init_dev(child);
789 child->rphy = rphy;
791 list_add_tail(&child->disco_list_node, &parent->port->disco_list);
793 res = sas_discover_sata(child);
794 if (res) {
795 SAS_DPRINTK("sas_discover_sata() for device %16llx at "
796 "%016llx:0x%x returned 0x%x\n",
797 SAS_ADDR(child->sas_addr),
798 SAS_ADDR(parent->sas_addr), phy_id, res);
799 goto out_list_del;
801 } else
802 #endif
803 if (phy->attached_tproto & SAS_PROTOCOL_SSP) {
804 child->dev_type = SAS_END_DEV;
805 rphy = sas_end_device_alloc(phy->port);
806 /* FIXME: error handling */
807 if (unlikely(!rphy))
808 goto out_free;
809 child->tproto = phy->attached_tproto;
810 sas_init_dev(child);
812 child->rphy = rphy;
813 sas_fill_in_rphy(child, rphy);
815 spin_lock_irq(&parent->port->dev_list_lock);
816 list_add_tail(&child->dev_list_node, &parent->port->dev_list);
817 spin_unlock_irq(&parent->port->dev_list_lock);
819 res = sas_discover_end_dev(child);
820 if (res) {
821 SAS_DPRINTK("sas_discover_end_dev() for device %16llx "
822 "at %016llx:0x%x returned 0x%x\n",
823 SAS_ADDR(child->sas_addr),
824 SAS_ADDR(parent->sas_addr), phy_id, res);
825 goto out_list_del;
827 } else {
828 SAS_DPRINTK("target proto 0x%x at %016llx:0x%x not handled\n",
829 phy->attached_tproto, SAS_ADDR(parent->sas_addr),
830 phy_id);
831 goto out_free;
834 list_add_tail(&child->siblings, &parent_ex->children);
835 return child;
837 out_list_del:
838 sas_rphy_free(child->rphy);
839 child->rphy = NULL;
841 list_del(&child->disco_list_node);
842 spin_lock_irq(&parent->port->dev_list_lock);
843 list_del(&child->dev_list_node);
844 spin_unlock_irq(&parent->port->dev_list_lock);
845 out_free:
846 sas_port_delete(phy->port);
847 out_err:
848 phy->port = NULL;
849 sas_put_device(child);
850 return NULL;
853 /* See if this phy is part of a wide port */
854 static int sas_ex_join_wide_port(struct domain_device *parent, int phy_id)
856 struct ex_phy *phy = &parent->ex_dev.ex_phy[phy_id];
857 int i;
859 for (i = 0; i < parent->ex_dev.num_phys; i++) {
860 struct ex_phy *ephy = &parent->ex_dev.ex_phy[i];
862 if (ephy == phy)
863 continue;
865 if (!memcmp(phy->attached_sas_addr, ephy->attached_sas_addr,
866 SAS_ADDR_SIZE) && ephy->port) {
867 sas_port_add_phy(ephy->port, phy->phy);
868 phy->port = ephy->port;
869 phy->phy_state = PHY_DEVICE_DISCOVERED;
870 return 0;
874 return -ENODEV;
877 static struct domain_device *sas_ex_discover_expander(
878 struct domain_device *parent, int phy_id)
880 struct sas_expander_device *parent_ex = rphy_to_expander_device(parent->rphy);
881 struct ex_phy *phy = &parent->ex_dev.ex_phy[phy_id];
882 struct domain_device *child = NULL;
883 struct sas_rphy *rphy;
884 struct sas_expander_device *edev;
885 struct asd_sas_port *port;
886 int res;
888 if (phy->routing_attr == DIRECT_ROUTING) {
889 SAS_DPRINTK("ex %016llx:0x%x:D <--> ex %016llx:0x%x is not "
890 "allowed\n",
891 SAS_ADDR(parent->sas_addr), phy_id,
892 SAS_ADDR(phy->attached_sas_addr),
893 phy->attached_phy_id);
894 return NULL;
896 child = sas_alloc_device();
897 if (!child)
898 return NULL;
900 phy->port = sas_port_alloc(&parent->rphy->dev, phy_id);
901 /* FIXME: better error handling */
902 BUG_ON(sas_port_add(phy->port) != 0);
905 switch (phy->attached_dev_type) {
906 case EDGE_DEV:
907 rphy = sas_expander_alloc(phy->port,
908 SAS_EDGE_EXPANDER_DEVICE);
909 break;
910 case FANOUT_DEV:
911 rphy = sas_expander_alloc(phy->port,
912 SAS_FANOUT_EXPANDER_DEVICE);
913 break;
914 default:
915 rphy = NULL; /* shut gcc up */
916 BUG();
918 port = parent->port;
919 child->rphy = rphy;
920 edev = rphy_to_expander_device(rphy);
921 child->dev_type = phy->attached_dev_type;
922 kref_get(&parent->kref);
923 child->parent = parent;
924 child->port = port;
925 child->iproto = phy->attached_iproto;
926 child->tproto = phy->attached_tproto;
927 memcpy(child->sas_addr, phy->attached_sas_addr, SAS_ADDR_SIZE);
928 sas_hash_addr(child->hashed_sas_addr, child->sas_addr);
929 sas_ex_get_linkrate(parent, child, phy);
930 edev->level = parent_ex->level + 1;
931 parent->port->disc.max_level = max(parent->port->disc.max_level,
932 edev->level);
933 sas_init_dev(child);
934 sas_fill_in_rphy(child, rphy);
935 sas_rphy_add(rphy);
937 spin_lock_irq(&parent->port->dev_list_lock);
938 list_add_tail(&child->dev_list_node, &parent->port->dev_list);
939 spin_unlock_irq(&parent->port->dev_list_lock);
941 res = sas_discover_expander(child);
942 if (res) {
943 spin_lock_irq(&parent->port->dev_list_lock);
944 list_del(&child->dev_list_node);
945 spin_unlock_irq(&parent->port->dev_list_lock);
946 sas_put_device(child);
947 return NULL;
949 list_add_tail(&child->siblings, &parent->ex_dev.children);
950 return child;
953 static int sas_ex_discover_dev(struct domain_device *dev, int phy_id)
955 struct expander_device *ex = &dev->ex_dev;
956 struct ex_phy *ex_phy = &ex->ex_phy[phy_id];
957 struct domain_device *child = NULL;
958 int res = 0;
960 /* Phy state */
961 if (ex_phy->linkrate == SAS_SATA_SPINUP_HOLD) {
962 if (!sas_smp_phy_control(dev, phy_id, PHY_FUNC_LINK_RESET, NULL))
963 res = sas_ex_phy_discover(dev, phy_id);
964 if (res)
965 return res;
968 /* Parent and domain coherency */
969 if (!dev->parent && (SAS_ADDR(ex_phy->attached_sas_addr) ==
970 SAS_ADDR(dev->port->sas_addr))) {
971 sas_add_parent_port(dev, phy_id);
972 return 0;
974 if (dev->parent && (SAS_ADDR(ex_phy->attached_sas_addr) ==
975 SAS_ADDR(dev->parent->sas_addr))) {
976 sas_add_parent_port(dev, phy_id);
977 if (ex_phy->routing_attr == TABLE_ROUTING)
978 sas_configure_phy(dev, phy_id, dev->port->sas_addr, 1);
979 return 0;
982 if (sas_dev_present_in_domain(dev->port, ex_phy->attached_sas_addr))
983 sas_ex_disable_port(dev, ex_phy->attached_sas_addr);
985 if (ex_phy->attached_dev_type == NO_DEVICE) {
986 if (ex_phy->routing_attr == DIRECT_ROUTING) {
987 memset(ex_phy->attached_sas_addr, 0, SAS_ADDR_SIZE);
988 sas_configure_routing(dev, ex_phy->attached_sas_addr);
990 return 0;
991 } else if (ex_phy->linkrate == SAS_LINK_RATE_UNKNOWN)
992 return 0;
994 if (ex_phy->attached_dev_type != SAS_END_DEV &&
995 ex_phy->attached_dev_type != FANOUT_DEV &&
996 ex_phy->attached_dev_type != EDGE_DEV) {
997 SAS_DPRINTK("unknown device type(0x%x) attached to ex %016llx "
998 "phy 0x%x\n", ex_phy->attached_dev_type,
999 SAS_ADDR(dev->sas_addr),
1000 phy_id);
1001 return 0;
1004 res = sas_configure_routing(dev, ex_phy->attached_sas_addr);
1005 if (res) {
1006 SAS_DPRINTK("configure routing for dev %016llx "
1007 "reported 0x%x. Forgotten\n",
1008 SAS_ADDR(ex_phy->attached_sas_addr), res);
1009 sas_disable_routing(dev, ex_phy->attached_sas_addr);
1010 return res;
1013 res = sas_ex_join_wide_port(dev, phy_id);
1014 if (!res) {
1015 SAS_DPRINTK("Attaching ex phy%d to wide port %016llx\n",
1016 phy_id, SAS_ADDR(ex_phy->attached_sas_addr));
1017 return res;
1020 switch (ex_phy->attached_dev_type) {
1021 case SAS_END_DEV:
1022 child = sas_ex_discover_end_dev(dev, phy_id);
1023 break;
1024 case FANOUT_DEV:
1025 if (SAS_ADDR(dev->port->disc.fanout_sas_addr)) {
1026 SAS_DPRINTK("second fanout expander %016llx phy 0x%x "
1027 "attached to ex %016llx phy 0x%x\n",
1028 SAS_ADDR(ex_phy->attached_sas_addr),
1029 ex_phy->attached_phy_id,
1030 SAS_ADDR(dev->sas_addr),
1031 phy_id);
1032 sas_ex_disable_phy(dev, phy_id);
1033 break;
1034 } else
1035 memcpy(dev->port->disc.fanout_sas_addr,
1036 ex_phy->attached_sas_addr, SAS_ADDR_SIZE);
1037 /* fallthrough */
1038 case EDGE_DEV:
1039 child = sas_ex_discover_expander(dev, phy_id);
1040 break;
1041 default:
1042 break;
1045 if (child) {
1046 int i;
1048 for (i = 0; i < ex->num_phys; i++) {
1049 if (ex->ex_phy[i].phy_state == PHY_VACANT ||
1050 ex->ex_phy[i].phy_state == PHY_NOT_PRESENT)
1051 continue;
1053 * Due to races, the phy might not get added to the
1054 * wide port, so we add the phy to the wide port here.
1056 if (SAS_ADDR(ex->ex_phy[i].attached_sas_addr) ==
1057 SAS_ADDR(child->sas_addr)) {
1058 ex->ex_phy[i].phy_state= PHY_DEVICE_DISCOVERED;
1059 res = sas_ex_join_wide_port(dev, i);
1060 if (!res)
1061 SAS_DPRINTK("Attaching ex phy%d to wide port %016llx\n",
1062 i, SAS_ADDR(ex->ex_phy[i].attached_sas_addr));
1068 return res;
1071 static int sas_find_sub_addr(struct domain_device *dev, u8 *sub_addr)
1073 struct expander_device *ex = &dev->ex_dev;
1074 int i;
1076 for (i = 0; i < ex->num_phys; i++) {
1077 struct ex_phy *phy = &ex->ex_phy[i];
1079 if (phy->phy_state == PHY_VACANT ||
1080 phy->phy_state == PHY_NOT_PRESENT)
1081 continue;
1083 if ((phy->attached_dev_type == EDGE_DEV ||
1084 phy->attached_dev_type == FANOUT_DEV) &&
1085 phy->routing_attr == SUBTRACTIVE_ROUTING) {
1087 memcpy(sub_addr, phy->attached_sas_addr,SAS_ADDR_SIZE);
1089 return 1;
1092 return 0;
1095 static int sas_check_level_subtractive_boundary(struct domain_device *dev)
1097 struct expander_device *ex = &dev->ex_dev;
1098 struct domain_device *child;
1099 u8 sub_addr[8] = {0, };
1101 list_for_each_entry(child, &ex->children, siblings) {
1102 if (child->dev_type != EDGE_DEV &&
1103 child->dev_type != FANOUT_DEV)
1104 continue;
1105 if (sub_addr[0] == 0) {
1106 sas_find_sub_addr(child, sub_addr);
1107 continue;
1108 } else {
1109 u8 s2[8];
1111 if (sas_find_sub_addr(child, s2) &&
1112 (SAS_ADDR(sub_addr) != SAS_ADDR(s2))) {
1114 SAS_DPRINTK("ex %016llx->%016llx-?->%016llx "
1115 "diverges from subtractive "
1116 "boundary %016llx\n",
1117 SAS_ADDR(dev->sas_addr),
1118 SAS_ADDR(child->sas_addr),
1119 SAS_ADDR(s2),
1120 SAS_ADDR(sub_addr));
1122 sas_ex_disable_port(child, s2);
1126 return 0;
1129 * sas_ex_discover_devices -- discover devices attached to this expander
1130 * dev: pointer to the expander domain device
1131 * single: if you want to do a single phy, else set to -1;
1133 * Configure this expander for use with its devices and register the
1134 * devices of this expander.
1136 static int sas_ex_discover_devices(struct domain_device *dev, int single)
1138 struct expander_device *ex = &dev->ex_dev;
1139 int i = 0, end = ex->num_phys;
1140 int res = 0;
1142 if (0 <= single && single < end) {
1143 i = single;
1144 end = i+1;
1147 for ( ; i < end; i++) {
1148 struct ex_phy *ex_phy = &ex->ex_phy[i];
1150 if (ex_phy->phy_state == PHY_VACANT ||
1151 ex_phy->phy_state == PHY_NOT_PRESENT ||
1152 ex_phy->phy_state == PHY_DEVICE_DISCOVERED)
1153 continue;
1155 switch (ex_phy->linkrate) {
1156 case SAS_PHY_DISABLED:
1157 case SAS_PHY_RESET_PROBLEM:
1158 case SAS_SATA_PORT_SELECTOR:
1159 continue;
1160 default:
1161 res = sas_ex_discover_dev(dev, i);
1162 if (res)
1163 break;
1164 continue;
1168 if (!res)
1169 sas_check_level_subtractive_boundary(dev);
1171 return res;
1174 static int sas_check_ex_subtractive_boundary(struct domain_device *dev)
1176 struct expander_device *ex = &dev->ex_dev;
1177 int i;
1178 u8 *sub_sas_addr = NULL;
1180 if (dev->dev_type != EDGE_DEV)
1181 return 0;
1183 for (i = 0; i < ex->num_phys; i++) {
1184 struct ex_phy *phy = &ex->ex_phy[i];
1186 if (phy->phy_state == PHY_VACANT ||
1187 phy->phy_state == PHY_NOT_PRESENT)
1188 continue;
1190 if ((phy->attached_dev_type == FANOUT_DEV ||
1191 phy->attached_dev_type == EDGE_DEV) &&
1192 phy->routing_attr == SUBTRACTIVE_ROUTING) {
1194 if (!sub_sas_addr)
1195 sub_sas_addr = &phy->attached_sas_addr[0];
1196 else if (SAS_ADDR(sub_sas_addr) !=
1197 SAS_ADDR(phy->attached_sas_addr)) {
1199 SAS_DPRINTK("ex %016llx phy 0x%x "
1200 "diverges(%016llx) on subtractive "
1201 "boundary(%016llx). Disabled\n",
1202 SAS_ADDR(dev->sas_addr), i,
1203 SAS_ADDR(phy->attached_sas_addr),
1204 SAS_ADDR(sub_sas_addr));
1205 sas_ex_disable_phy(dev, i);
1209 return 0;
1212 static void sas_print_parent_topology_bug(struct domain_device *child,
1213 struct ex_phy *parent_phy,
1214 struct ex_phy *child_phy)
1216 static const char *ex_type[] = {
1217 [EDGE_DEV] = "edge",
1218 [FANOUT_DEV] = "fanout",
1220 struct domain_device *parent = child->parent;
1222 sas_printk("%s ex %016llx phy 0x%x <--> %s ex %016llx "
1223 "phy 0x%x has %c:%c routing link!\n",
1225 ex_type[parent->dev_type],
1226 SAS_ADDR(parent->sas_addr),
1227 parent_phy->phy_id,
1229 ex_type[child->dev_type],
1230 SAS_ADDR(child->sas_addr),
1231 child_phy->phy_id,
1233 sas_route_char(parent, parent_phy),
1234 sas_route_char(child, child_phy));
1237 static int sas_check_eeds(struct domain_device *child,
1238 struct ex_phy *parent_phy,
1239 struct ex_phy *child_phy)
1241 int res = 0;
1242 struct domain_device *parent = child->parent;
1244 if (SAS_ADDR(parent->port->disc.fanout_sas_addr) != 0) {
1245 res = -ENODEV;
1246 SAS_DPRINTK("edge ex %016llx phy S:0x%x <--> edge ex %016llx "
1247 "phy S:0x%x, while there is a fanout ex %016llx\n",
1248 SAS_ADDR(parent->sas_addr),
1249 parent_phy->phy_id,
1250 SAS_ADDR(child->sas_addr),
1251 child_phy->phy_id,
1252 SAS_ADDR(parent->port->disc.fanout_sas_addr));
1253 } else if (SAS_ADDR(parent->port->disc.eeds_a) == 0) {
1254 memcpy(parent->port->disc.eeds_a, parent->sas_addr,
1255 SAS_ADDR_SIZE);
1256 memcpy(parent->port->disc.eeds_b, child->sas_addr,
1257 SAS_ADDR_SIZE);
1258 } else if (((SAS_ADDR(parent->port->disc.eeds_a) ==
1259 SAS_ADDR(parent->sas_addr)) ||
1260 (SAS_ADDR(parent->port->disc.eeds_a) ==
1261 SAS_ADDR(child->sas_addr)))
1263 ((SAS_ADDR(parent->port->disc.eeds_b) ==
1264 SAS_ADDR(parent->sas_addr)) ||
1265 (SAS_ADDR(parent->port->disc.eeds_b) ==
1266 SAS_ADDR(child->sas_addr))))
1268 else {
1269 res = -ENODEV;
1270 SAS_DPRINTK("edge ex %016llx phy 0x%x <--> edge ex %016llx "
1271 "phy 0x%x link forms a third EEDS!\n",
1272 SAS_ADDR(parent->sas_addr),
1273 parent_phy->phy_id,
1274 SAS_ADDR(child->sas_addr),
1275 child_phy->phy_id);
1278 return res;
1281 /* Here we spill over 80 columns. It is intentional.
1283 static int sas_check_parent_topology(struct domain_device *child)
1285 struct expander_device *child_ex = &child->ex_dev;
1286 struct expander_device *parent_ex;
1287 int i;
1288 int res = 0;
1290 if (!child->parent)
1291 return 0;
1293 if (child->parent->dev_type != EDGE_DEV &&
1294 child->parent->dev_type != FANOUT_DEV)
1295 return 0;
1297 parent_ex = &child->parent->ex_dev;
1299 for (i = 0; i < parent_ex->num_phys; i++) {
1300 struct ex_phy *parent_phy = &parent_ex->ex_phy[i];
1301 struct ex_phy *child_phy;
1303 if (parent_phy->phy_state == PHY_VACANT ||
1304 parent_phy->phy_state == PHY_NOT_PRESENT)
1305 continue;
1307 if (SAS_ADDR(parent_phy->attached_sas_addr) != SAS_ADDR(child->sas_addr))
1308 continue;
1310 child_phy = &child_ex->ex_phy[parent_phy->attached_phy_id];
1312 switch (child->parent->dev_type) {
1313 case EDGE_DEV:
1314 if (child->dev_type == FANOUT_DEV) {
1315 if (parent_phy->routing_attr != SUBTRACTIVE_ROUTING ||
1316 child_phy->routing_attr != TABLE_ROUTING) {
1317 sas_print_parent_topology_bug(child, parent_phy, child_phy);
1318 res = -ENODEV;
1320 } else if (parent_phy->routing_attr == SUBTRACTIVE_ROUTING) {
1321 if (child_phy->routing_attr == SUBTRACTIVE_ROUTING) {
1322 res = sas_check_eeds(child, parent_phy, child_phy);
1323 } else if (child_phy->routing_attr != TABLE_ROUTING) {
1324 sas_print_parent_topology_bug(child, parent_phy, child_phy);
1325 res = -ENODEV;
1327 } else if (parent_phy->routing_attr == TABLE_ROUTING) {
1328 if (child_phy->routing_attr == SUBTRACTIVE_ROUTING ||
1329 (child_phy->routing_attr == TABLE_ROUTING &&
1330 child_ex->t2t_supp && parent_ex->t2t_supp)) {
1331 /* All good */;
1332 } else {
1333 sas_print_parent_topology_bug(child, parent_phy, child_phy);
1334 res = -ENODEV;
1337 break;
1338 case FANOUT_DEV:
1339 if (parent_phy->routing_attr != TABLE_ROUTING ||
1340 child_phy->routing_attr != SUBTRACTIVE_ROUTING) {
1341 sas_print_parent_topology_bug(child, parent_phy, child_phy);
1342 res = -ENODEV;
1344 break;
1345 default:
1346 break;
1350 return res;
1353 #define RRI_REQ_SIZE 16
1354 #define RRI_RESP_SIZE 44
1356 static int sas_configure_present(struct domain_device *dev, int phy_id,
1357 u8 *sas_addr, int *index, int *present)
1359 int i, res = 0;
1360 struct expander_device *ex = &dev->ex_dev;
1361 struct ex_phy *phy = &ex->ex_phy[phy_id];
1362 u8 *rri_req;
1363 u8 *rri_resp;
1365 *present = 0;
1366 *index = 0;
1368 rri_req = alloc_smp_req(RRI_REQ_SIZE);
1369 if (!rri_req)
1370 return -ENOMEM;
1372 rri_resp = alloc_smp_resp(RRI_RESP_SIZE);
1373 if (!rri_resp) {
1374 kfree(rri_req);
1375 return -ENOMEM;
1378 rri_req[1] = SMP_REPORT_ROUTE_INFO;
1379 rri_req[9] = phy_id;
1381 for (i = 0; i < ex->max_route_indexes ; i++) {
1382 *(__be16 *)(rri_req+6) = cpu_to_be16(i);
1383 res = smp_execute_task(dev, rri_req, RRI_REQ_SIZE, rri_resp,
1384 RRI_RESP_SIZE);
1385 if (res)
1386 goto out;
1387 res = rri_resp[2];
1388 if (res == SMP_RESP_NO_INDEX) {
1389 SAS_DPRINTK("overflow of indexes: dev %016llx "
1390 "phy 0x%x index 0x%x\n",
1391 SAS_ADDR(dev->sas_addr), phy_id, i);
1392 goto out;
1393 } else if (res != SMP_RESP_FUNC_ACC) {
1394 SAS_DPRINTK("%s: dev %016llx phy 0x%x index 0x%x "
1395 "result 0x%x\n", __func__,
1396 SAS_ADDR(dev->sas_addr), phy_id, i, res);
1397 goto out;
1399 if (SAS_ADDR(sas_addr) != 0) {
1400 if (SAS_ADDR(rri_resp+16) == SAS_ADDR(sas_addr)) {
1401 *index = i;
1402 if ((rri_resp[12] & 0x80) == 0x80)
1403 *present = 0;
1404 else
1405 *present = 1;
1406 goto out;
1407 } else if (SAS_ADDR(rri_resp+16) == 0) {
1408 *index = i;
1409 *present = 0;
1410 goto out;
1412 } else if (SAS_ADDR(rri_resp+16) == 0 &&
1413 phy->last_da_index < i) {
1414 phy->last_da_index = i;
1415 *index = i;
1416 *present = 0;
1417 goto out;
1420 res = -1;
1421 out:
1422 kfree(rri_req);
1423 kfree(rri_resp);
1424 return res;
1427 #define CRI_REQ_SIZE 44
1428 #define CRI_RESP_SIZE 8
1430 static int sas_configure_set(struct domain_device *dev, int phy_id,
1431 u8 *sas_addr, int index, int include)
1433 int res;
1434 u8 *cri_req;
1435 u8 *cri_resp;
1437 cri_req = alloc_smp_req(CRI_REQ_SIZE);
1438 if (!cri_req)
1439 return -ENOMEM;
1441 cri_resp = alloc_smp_resp(CRI_RESP_SIZE);
1442 if (!cri_resp) {
1443 kfree(cri_req);
1444 return -ENOMEM;
1447 cri_req[1] = SMP_CONF_ROUTE_INFO;
1448 *(__be16 *)(cri_req+6) = cpu_to_be16(index);
1449 cri_req[9] = phy_id;
1450 if (SAS_ADDR(sas_addr) == 0 || !include)
1451 cri_req[12] |= 0x80;
1452 memcpy(cri_req+16, sas_addr, SAS_ADDR_SIZE);
1454 res = smp_execute_task(dev, cri_req, CRI_REQ_SIZE, cri_resp,
1455 CRI_RESP_SIZE);
1456 if (res)
1457 goto out;
1458 res = cri_resp[2];
1459 if (res == SMP_RESP_NO_INDEX) {
1460 SAS_DPRINTK("overflow of indexes: dev %016llx phy 0x%x "
1461 "index 0x%x\n",
1462 SAS_ADDR(dev->sas_addr), phy_id, index);
1464 out:
1465 kfree(cri_req);
1466 kfree(cri_resp);
1467 return res;
1470 static int sas_configure_phy(struct domain_device *dev, int phy_id,
1471 u8 *sas_addr, int include)
1473 int index;
1474 int present;
1475 int res;
1477 res = sas_configure_present(dev, phy_id, sas_addr, &index, &present);
1478 if (res)
1479 return res;
1480 if (include ^ present)
1481 return sas_configure_set(dev, phy_id, sas_addr, index,include);
1483 return res;
1487 * sas_configure_parent -- configure routing table of parent
1488 * parent: parent expander
1489 * child: child expander
1490 * sas_addr: SAS port identifier of device directly attached to child
1492 static int sas_configure_parent(struct domain_device *parent,
1493 struct domain_device *child,
1494 u8 *sas_addr, int include)
1496 struct expander_device *ex_parent = &parent->ex_dev;
1497 int res = 0;
1498 int i;
1500 if (parent->parent) {
1501 res = sas_configure_parent(parent->parent, parent, sas_addr,
1502 include);
1503 if (res)
1504 return res;
1507 if (ex_parent->conf_route_table == 0) {
1508 SAS_DPRINTK("ex %016llx has self-configuring routing table\n",
1509 SAS_ADDR(parent->sas_addr));
1510 return 0;
1513 for (i = 0; i < ex_parent->num_phys; i++) {
1514 struct ex_phy *phy = &ex_parent->ex_phy[i];
1516 if ((phy->routing_attr == TABLE_ROUTING) &&
1517 (SAS_ADDR(phy->attached_sas_addr) ==
1518 SAS_ADDR(child->sas_addr))) {
1519 res = sas_configure_phy(parent, i, sas_addr, include);
1520 if (res)
1521 return res;
1525 return res;
1529 * sas_configure_routing -- configure routing
1530 * dev: expander device
1531 * sas_addr: port identifier of device directly attached to the expander device
1533 static int sas_configure_routing(struct domain_device *dev, u8 *sas_addr)
1535 if (dev->parent)
1536 return sas_configure_parent(dev->parent, dev, sas_addr, 1);
1537 return 0;
1540 static int sas_disable_routing(struct domain_device *dev, u8 *sas_addr)
1542 if (dev->parent)
1543 return sas_configure_parent(dev->parent, dev, sas_addr, 0);
1544 return 0;
1548 * sas_discover_expander -- expander discovery
1549 * @ex: pointer to expander domain device
1551 * See comment in sas_discover_sata().
1553 static int sas_discover_expander(struct domain_device *dev)
1555 int res;
1557 res = sas_notify_lldd_dev_found(dev);
1558 if (res)
1559 return res;
1561 res = sas_ex_general(dev);
1562 if (res)
1563 goto out_err;
1564 res = sas_ex_manuf_info(dev);
1565 if (res)
1566 goto out_err;
1568 res = sas_expander_discover(dev);
1569 if (res) {
1570 SAS_DPRINTK("expander %016llx discovery failed(0x%x)\n",
1571 SAS_ADDR(dev->sas_addr), res);
1572 goto out_err;
1575 sas_check_ex_subtractive_boundary(dev);
1576 res = sas_check_parent_topology(dev);
1577 if (res)
1578 goto out_err;
1579 return 0;
1580 out_err:
1581 sas_notify_lldd_dev_gone(dev);
1582 return res;
1585 static int sas_ex_level_discovery(struct asd_sas_port *port, const int level)
1587 int res = 0;
1588 struct domain_device *dev;
1590 list_for_each_entry(dev, &port->dev_list, dev_list_node) {
1591 if (dev->dev_type == EDGE_DEV ||
1592 dev->dev_type == FANOUT_DEV) {
1593 struct sas_expander_device *ex =
1594 rphy_to_expander_device(dev->rphy);
1596 if (level == ex->level)
1597 res = sas_ex_discover_devices(dev, -1);
1598 else if (level > 0)
1599 res = sas_ex_discover_devices(port->port_dev, -1);
1604 return res;
1607 static int sas_ex_bfs_disc(struct asd_sas_port *port)
1609 int res;
1610 int level;
1612 do {
1613 level = port->disc.max_level;
1614 res = sas_ex_level_discovery(port, level);
1615 mb();
1616 } while (level < port->disc.max_level);
1618 return res;
1621 int sas_discover_root_expander(struct domain_device *dev)
1623 int res;
1624 struct sas_expander_device *ex = rphy_to_expander_device(dev->rphy);
1626 res = sas_rphy_add(dev->rphy);
1627 if (res)
1628 goto out_err;
1630 ex->level = dev->port->disc.max_level; /* 0 */
1631 res = sas_discover_expander(dev);
1632 if (res)
1633 goto out_err2;
1635 sas_ex_bfs_disc(dev->port);
1637 return res;
1639 out_err2:
1640 sas_rphy_remove(dev->rphy);
1641 out_err:
1642 return res;
1645 /* ---------- Domain revalidation ---------- */
1647 static int sas_get_phy_discover(struct domain_device *dev,
1648 int phy_id, struct smp_resp *disc_resp)
1650 int res;
1651 u8 *disc_req;
1653 disc_req = alloc_smp_req(DISCOVER_REQ_SIZE);
1654 if (!disc_req)
1655 return -ENOMEM;
1657 disc_req[1] = SMP_DISCOVER;
1658 disc_req[9] = phy_id;
1660 res = smp_execute_task(dev, disc_req, DISCOVER_REQ_SIZE,
1661 disc_resp, DISCOVER_RESP_SIZE);
1662 if (res)
1663 goto out;
1664 else if (disc_resp->result != SMP_RESP_FUNC_ACC) {
1665 res = disc_resp->result;
1666 goto out;
1668 out:
1669 kfree(disc_req);
1670 return res;
1673 static int sas_get_phy_change_count(struct domain_device *dev,
1674 int phy_id, int *pcc)
1676 int res;
1677 struct smp_resp *disc_resp;
1679 disc_resp = alloc_smp_resp(DISCOVER_RESP_SIZE);
1680 if (!disc_resp)
1681 return -ENOMEM;
1683 res = sas_get_phy_discover(dev, phy_id, disc_resp);
1684 if (!res)
1685 *pcc = disc_resp->disc.change_count;
1687 kfree(disc_resp);
1688 return res;
1691 int sas_get_phy_attached_sas_addr(struct domain_device *dev, int phy_id,
1692 u8 *attached_sas_addr)
1694 int res;
1695 struct smp_resp *disc_resp;
1696 struct discover_resp *dr;
1698 disc_resp = alloc_smp_resp(DISCOVER_RESP_SIZE);
1699 if (!disc_resp)
1700 return -ENOMEM;
1701 dr = &disc_resp->disc;
1703 res = sas_get_phy_discover(dev, phy_id, disc_resp);
1704 if (!res) {
1705 memcpy(attached_sas_addr,disc_resp->disc.attached_sas_addr,8);
1706 if (dr->attached_dev_type == 0)
1707 memset(attached_sas_addr, 0, 8);
1709 kfree(disc_resp);
1710 return res;
1713 static int sas_find_bcast_phy(struct domain_device *dev, int *phy_id,
1714 int from_phy, bool update)
1716 struct expander_device *ex = &dev->ex_dev;
1717 int res = 0;
1718 int i;
1720 for (i = from_phy; i < ex->num_phys; i++) {
1721 int phy_change_count = 0;
1723 res = sas_get_phy_change_count(dev, i, &phy_change_count);
1724 if (res)
1725 goto out;
1726 else if (phy_change_count != ex->ex_phy[i].phy_change_count) {
1727 if (update)
1728 ex->ex_phy[i].phy_change_count =
1729 phy_change_count;
1730 *phy_id = i;
1731 return 0;
1734 out:
1735 return res;
1738 static int sas_get_ex_change_count(struct domain_device *dev, int *ecc)
1740 int res;
1741 u8 *rg_req;
1742 struct smp_resp *rg_resp;
1744 rg_req = alloc_smp_req(RG_REQ_SIZE);
1745 if (!rg_req)
1746 return -ENOMEM;
1748 rg_resp = alloc_smp_resp(RG_RESP_SIZE);
1749 if (!rg_resp) {
1750 kfree(rg_req);
1751 return -ENOMEM;
1754 rg_req[1] = SMP_REPORT_GENERAL;
1756 res = smp_execute_task(dev, rg_req, RG_REQ_SIZE, rg_resp,
1757 RG_RESP_SIZE);
1758 if (res)
1759 goto out;
1760 if (rg_resp->result != SMP_RESP_FUNC_ACC) {
1761 res = rg_resp->result;
1762 goto out;
1765 *ecc = be16_to_cpu(rg_resp->rg.change_count);
1766 out:
1767 kfree(rg_resp);
1768 kfree(rg_req);
1769 return res;
1772 * sas_find_bcast_dev - find the device issue BROADCAST(CHANGE).
1773 * @dev:domain device to be detect.
1774 * @src_dev: the device which originated BROADCAST(CHANGE).
1776 * Add self-configuration expander suport. Suppose two expander cascading,
1777 * when the first level expander is self-configuring, hotplug the disks in
1778 * second level expander, BROADCAST(CHANGE) will not only be originated
1779 * in the second level expander, but also be originated in the first level
1780 * expander (see SAS protocol SAS 2r-14, 7.11 for detail), it is to say,
1781 * expander changed count in two level expanders will all increment at least
1782 * once, but the phy which chang count has changed is the source device which
1783 * we concerned.
1786 static int sas_find_bcast_dev(struct domain_device *dev,
1787 struct domain_device **src_dev)
1789 struct expander_device *ex = &dev->ex_dev;
1790 int ex_change_count = -1;
1791 int phy_id = -1;
1792 int res;
1793 struct domain_device *ch;
1795 res = sas_get_ex_change_count(dev, &ex_change_count);
1796 if (res)
1797 goto out;
1798 if (ex_change_count != -1 && ex_change_count != ex->ex_change_count) {
1799 /* Just detect if this expander phys phy change count changed,
1800 * in order to determine if this expander originate BROADCAST,
1801 * and do not update phy change count field in our structure.
1803 res = sas_find_bcast_phy(dev, &phy_id, 0, false);
1804 if (phy_id != -1) {
1805 *src_dev = dev;
1806 ex->ex_change_count = ex_change_count;
1807 SAS_DPRINTK("Expander phy change count has changed\n");
1808 return res;
1809 } else
1810 SAS_DPRINTK("Expander phys DID NOT change\n");
1812 list_for_each_entry(ch, &ex->children, siblings) {
1813 if (ch->dev_type == EDGE_DEV || ch->dev_type == FANOUT_DEV) {
1814 res = sas_find_bcast_dev(ch, src_dev);
1815 if (*src_dev)
1816 return res;
1819 out:
1820 return res;
1823 static void sas_unregister_ex_tree(struct asd_sas_port *port, struct domain_device *dev)
1825 struct expander_device *ex = &dev->ex_dev;
1826 struct domain_device *child, *n;
1828 list_for_each_entry_safe(child, n, &ex->children, siblings) {
1829 set_bit(SAS_DEV_GONE, &child->state);
1830 if (child->dev_type == EDGE_DEV ||
1831 child->dev_type == FANOUT_DEV)
1832 sas_unregister_ex_tree(port, child);
1833 else
1834 sas_unregister_dev(port, child);
1836 sas_unregister_dev(port, dev);
1839 static void sas_unregister_devs_sas_addr(struct domain_device *parent,
1840 int phy_id, bool last)
1842 struct expander_device *ex_dev = &parent->ex_dev;
1843 struct ex_phy *phy = &ex_dev->ex_phy[phy_id];
1844 struct domain_device *child, *n, *found = NULL;
1845 if (last) {
1846 list_for_each_entry_safe(child, n,
1847 &ex_dev->children, siblings) {
1848 if (SAS_ADDR(child->sas_addr) ==
1849 SAS_ADDR(phy->attached_sas_addr)) {
1850 set_bit(SAS_DEV_GONE, &child->state);
1851 if (child->dev_type == EDGE_DEV ||
1852 child->dev_type == FANOUT_DEV)
1853 sas_unregister_ex_tree(parent->port, child);
1854 else
1855 sas_unregister_dev(parent->port, child);
1856 found = child;
1857 break;
1860 sas_disable_routing(parent, phy->attached_sas_addr);
1862 memset(phy->attached_sas_addr, 0, SAS_ADDR_SIZE);
1863 if (phy->port) {
1864 sas_port_delete_phy(phy->port, phy->phy);
1865 sas_device_set_phy(found, phy->port);
1866 if (phy->port->num_phys == 0)
1867 sas_port_delete(phy->port);
1868 phy->port = NULL;
1872 static int sas_discover_bfs_by_root_level(struct domain_device *root,
1873 const int level)
1875 struct expander_device *ex_root = &root->ex_dev;
1876 struct domain_device *child;
1877 int res = 0;
1879 list_for_each_entry(child, &ex_root->children, siblings) {
1880 if (child->dev_type == EDGE_DEV ||
1881 child->dev_type == FANOUT_DEV) {
1882 struct sas_expander_device *ex =
1883 rphy_to_expander_device(child->rphy);
1885 if (level > ex->level)
1886 res = sas_discover_bfs_by_root_level(child,
1887 level);
1888 else if (level == ex->level)
1889 res = sas_ex_discover_devices(child, -1);
1892 return res;
1895 static int sas_discover_bfs_by_root(struct domain_device *dev)
1897 int res;
1898 struct sas_expander_device *ex = rphy_to_expander_device(dev->rphy);
1899 int level = ex->level+1;
1901 res = sas_ex_discover_devices(dev, -1);
1902 if (res)
1903 goto out;
1904 do {
1905 res = sas_discover_bfs_by_root_level(dev, level);
1906 mb();
1907 level += 1;
1908 } while (level <= dev->port->disc.max_level);
1909 out:
1910 return res;
1913 static int sas_discover_new(struct domain_device *dev, int phy_id)
1915 struct ex_phy *ex_phy = &dev->ex_dev.ex_phy[phy_id];
1916 struct domain_device *child;
1917 bool found = false;
1918 int res, i;
1920 SAS_DPRINTK("ex %016llx phy%d new device attached\n",
1921 SAS_ADDR(dev->sas_addr), phy_id);
1922 res = sas_ex_phy_discover(dev, phy_id);
1923 if (res)
1924 goto out;
1925 /* to support the wide port inserted */
1926 for (i = 0; i < dev->ex_dev.num_phys; i++) {
1927 struct ex_phy *ex_phy_temp = &dev->ex_dev.ex_phy[i];
1928 if (i == phy_id)
1929 continue;
1930 if (SAS_ADDR(ex_phy_temp->attached_sas_addr) ==
1931 SAS_ADDR(ex_phy->attached_sas_addr)) {
1932 found = true;
1933 break;
1936 if (found) {
1937 sas_ex_join_wide_port(dev, phy_id);
1938 return 0;
1940 res = sas_ex_discover_devices(dev, phy_id);
1941 if (!res)
1942 goto out;
1943 list_for_each_entry(child, &dev->ex_dev.children, siblings) {
1944 if (SAS_ADDR(child->sas_addr) ==
1945 SAS_ADDR(ex_phy->attached_sas_addr)) {
1946 if (child->dev_type == EDGE_DEV ||
1947 child->dev_type == FANOUT_DEV)
1948 res = sas_discover_bfs_by_root(child);
1949 break;
1952 out:
1953 return res;
1956 static int sas_rediscover_dev(struct domain_device *dev, int phy_id, bool last)
1958 struct expander_device *ex = &dev->ex_dev;
1959 struct ex_phy *phy = &ex->ex_phy[phy_id];
1960 u8 attached_sas_addr[8];
1961 int res;
1963 res = sas_get_phy_attached_sas_addr(dev, phy_id, attached_sas_addr);
1964 switch (res) {
1965 case SMP_RESP_NO_PHY:
1966 phy->phy_state = PHY_NOT_PRESENT;
1967 sas_unregister_devs_sas_addr(dev, phy_id, last);
1968 goto out; break;
1969 case SMP_RESP_PHY_VACANT:
1970 phy->phy_state = PHY_VACANT;
1971 sas_unregister_devs_sas_addr(dev, phy_id, last);
1972 goto out; break;
1973 case SMP_RESP_FUNC_ACC:
1974 break;
1977 if (SAS_ADDR(attached_sas_addr) == 0) {
1978 phy->phy_state = PHY_EMPTY;
1979 sas_unregister_devs_sas_addr(dev, phy_id, last);
1980 } else if (SAS_ADDR(attached_sas_addr) ==
1981 SAS_ADDR(phy->attached_sas_addr)) {
1982 SAS_DPRINTK("ex %016llx phy 0x%x broadcast flutter\n",
1983 SAS_ADDR(dev->sas_addr), phy_id);
1984 sas_ex_phy_discover(dev, phy_id);
1985 } else
1986 res = sas_discover_new(dev, phy_id);
1987 out:
1988 return res;
1992 * sas_rediscover - revalidate the domain.
1993 * @dev:domain device to be detect.
1994 * @phy_id: the phy id will be detected.
1996 * NOTE: this process _must_ quit (return) as soon as any connection
1997 * errors are encountered. Connection recovery is done elsewhere.
1998 * Discover process only interrogates devices in order to discover the
1999 * domain.For plugging out, we un-register the device only when it is
2000 * the last phy in the port, for other phys in this port, we just delete it
2001 * from the port.For inserting, we do discovery when it is the
2002 * first phy,for other phys in this port, we add it to the port to
2003 * forming the wide-port.
2005 static int sas_rediscover(struct domain_device *dev, const int phy_id)
2007 struct expander_device *ex = &dev->ex_dev;
2008 struct ex_phy *changed_phy = &ex->ex_phy[phy_id];
2009 int res = 0;
2010 int i;
2011 bool last = true; /* is this the last phy of the port */
2013 SAS_DPRINTK("ex %016llx phy%d originated BROADCAST(CHANGE)\n",
2014 SAS_ADDR(dev->sas_addr), phy_id);
2016 if (SAS_ADDR(changed_phy->attached_sas_addr) != 0) {
2017 for (i = 0; i < ex->num_phys; i++) {
2018 struct ex_phy *phy = &ex->ex_phy[i];
2020 if (i == phy_id)
2021 continue;
2022 if (SAS_ADDR(phy->attached_sas_addr) ==
2023 SAS_ADDR(changed_phy->attached_sas_addr)) {
2024 SAS_DPRINTK("phy%d part of wide port with "
2025 "phy%d\n", phy_id, i);
2026 last = false;
2027 break;
2030 res = sas_rediscover_dev(dev, phy_id, last);
2031 } else
2032 res = sas_discover_new(dev, phy_id);
2033 return res;
2037 * sas_revalidate_domain -- revalidate the domain
2038 * @port: port to the domain of interest
2040 * NOTE: this process _must_ quit (return) as soon as any connection
2041 * errors are encountered. Connection recovery is done elsewhere.
2042 * Discover process only interrogates devices in order to discover the
2043 * domain.
2045 int sas_ex_revalidate_domain(struct domain_device *port_dev)
2047 int res;
2048 struct domain_device *dev = NULL;
2050 res = sas_find_bcast_dev(port_dev, &dev);
2051 if (res)
2052 goto out;
2053 if (dev) {
2054 struct expander_device *ex = &dev->ex_dev;
2055 int i = 0, phy_id;
2057 do {
2058 phy_id = -1;
2059 res = sas_find_bcast_phy(dev, &phy_id, i, true);
2060 if (phy_id == -1)
2061 break;
2062 res = sas_rediscover(dev, phy_id);
2063 i = phy_id + 1;
2064 } while (i < ex->num_phys);
2066 out:
2067 return res;
2070 int sas_smp_handler(struct Scsi_Host *shost, struct sas_rphy *rphy,
2071 struct request *req)
2073 struct domain_device *dev;
2074 int ret, type;
2075 struct request *rsp = req->next_rq;
2077 if (!rsp) {
2078 printk("%s: space for a smp response is missing\n",
2079 __func__);
2080 return -EINVAL;
2083 /* no rphy means no smp target support (ie aic94xx host) */
2084 if (!rphy)
2085 return sas_smp_host_handler(shost, req, rsp);
2087 type = rphy->identify.device_type;
2089 if (type != SAS_EDGE_EXPANDER_DEVICE &&
2090 type != SAS_FANOUT_EXPANDER_DEVICE) {
2091 printk("%s: can we send a smp request to a device?\n",
2092 __func__);
2093 return -EINVAL;
2096 dev = sas_find_dev_by_rphy(rphy);
2097 if (!dev) {
2098 printk("%s: fail to find a domain_device?\n", __func__);
2099 return -EINVAL;
2102 /* do we need to support multiple segments? */
2103 if (req->bio->bi_vcnt > 1 || rsp->bio->bi_vcnt > 1) {
2104 printk("%s: multiple segments req %u %u, rsp %u %u\n",
2105 __func__, req->bio->bi_vcnt, blk_rq_bytes(req),
2106 rsp->bio->bi_vcnt, blk_rq_bytes(rsp));
2107 return -EINVAL;
2110 ret = smp_execute_task(dev, bio_data(req->bio), blk_rq_bytes(req),
2111 bio_data(rsp->bio), blk_rq_bytes(rsp));
2112 if (ret > 0) {
2113 /* positive number is the untransferred residual */
2114 rsp->resid_len = ret;
2115 req->resid_len = 0;
2116 ret = 0;
2117 } else if (ret == 0) {
2118 rsp->resid_len = 0;
2119 req->resid_len = 0;
2122 return ret;