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
;
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
))
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
)
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
)) {
82 task
= sas_alloc_task(GFP_KERNEL
);
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
);
102 del_timer(&task
->timer
);
103 SAS_DPRINTK("executing SMP task failed:%d\n", res
);
107 wait_for_completion(&task
->completion
);
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");
117 if (task
->task_status
.resp
== SAS_TASK_COMPLETE
&&
118 task
->task_status
.stat
== SAM_STAT_GOOD
) {
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
126 res
= task
->task_status
.residual
;
129 if (task
->task_status
.resp
== SAS_TASK_COMPLETE
&&
130 task
->task_status
.stat
== SAS_DATA_OVERRUN
) {
134 if (task
->task_status
.resp
== SAS_TASK_UNDELIVERED
&&
135 task
->task_status
.stat
== SAS_DEVICE_UNKNOWN
)
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
);
147 mutex_unlock(&dev
->ex_dev
.cmd_mutex
);
149 BUG_ON(retry
== 3 && task
!= NULL
);
154 /* ---------- Allocations ---------- */
156 static inline void *alloc_smp_req(int size
)
158 u8
*p
= kzalloc(size
, GFP_KERNEL
);
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
) {
173 if (dev
->ex_dev
.t2t_supp
)
179 case SUBTRACTIVE_ROUTING
:
186 static void sas_set_ex_phy(struct domain_device
*dev
, int phy_id
,
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
;
198 phy
->phy
= sas_phy_alloc(&rphy
->dev
, phy_id
);
200 /* FIXME: error_handling */
204 switch (resp
->result
) {
205 case SMP_RESP_PHY_VACANT
:
206 phy
->phy_state
= PHY_VACANT
;
209 phy
->phy_state
= PHY_NOT_PRESENT
;
211 case SMP_RESP_FUNC_ACC
:
212 phy
->phy_state
= PHY_EMPTY
; /* do not know yet */
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
;
243 if (sas_phy_add(phy
->phy
)) {
244 sas_phy_free(phy
->phy
);
248 switch (phy
->attached_dev_type
) {
253 if (phy
->attached_iproto
) {
254 if (phy
->attached_tproto
)
255 type
= "host+target";
259 if (dr
->attached_sata_dev
)
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
;
289 rphy
= ex_phy
->port
->rphy
;
293 dev
= sas_find_dev_by_rphy(rphy
);
295 if (dev
&& dev_is_sata(dev
))
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
);
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
);
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");
325 /* This is detecting a failure to transmit initial
326 * dev to host FIS as described in section J.5 of
329 if (!(dr
->attached_dev_type
== 0 &&
330 dr
->attached_sata_dev
))
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.
339 sas_ata_schedule_reset(ata_dev
);
342 sas_smp_phy_control(dev
, single
, PHY_FUNC_LINK_RESET
, NULL
);
343 /* Wait for the reset to trigger the negotiation */
346 sas_set_ex_phy(dev
, single
, disc_resp
);
350 static int sas_ex_phy_discover(struct domain_device
*dev
, int single
)
352 struct expander_device
*ex
= &dev
->ex_dev
;
357 disc_req
= alloc_smp_req(DISCOVER_REQ_SIZE
);
361 disc_resp
= alloc_smp_req(DISCOVER_RESP_SIZE
);
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
);
374 for (i
= 0; i
< ex
->num_phys
; i
++) {
375 res
= sas_ex_phy_discover_helper(dev
, disc_req
,
387 static int sas_expander_discover(struct domain_device
*dev
)
389 struct expander_device
*ex
= &dev
->ex_dev
;
392 ex
->ex_phy
= kzalloc(sizeof(*ex
->ex_phy
)*ex
->num_phys
, GFP_KERNEL
);
396 res
= sas_ex_phy_discover(dev
, -1);
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
)
429 struct smp_resp
*rg_resp
;
433 rg_req
= alloc_smp_req(RG_REQ_SIZE
);
437 rg_resp
= alloc_smp_resp(RG_RESP_SIZE
);
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
,
450 SAS_DPRINTK("RG to ex %016llx failed:0x%x\n",
451 SAS_ADDR(dev
->sas_addr
), res
);
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
;
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
);
475 static void ex_assign_manuf_info(struct domain_device
*dev
, void
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
)
504 mi_req
= alloc_smp_req(MI_REQ_SIZE
);
508 mi_resp
= alloc_smp_resp(MI_RESP_SIZE
);
514 mi_req
[1] = SMP_REPORT_MANUF_INFO
;
516 res
= smp_execute_task(dev
, mi_req
, MI_REQ_SIZE
, mi_resp
,MI_RESP_SIZE
);
518 SAS_DPRINTK("MI: ex %016llx failed:0x%x\n",
519 SAS_ADDR(dev
->sas_addr
), res
);
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]);
527 ex_assign_manuf_info(dev
, mi_resp
);
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
)
545 pc_req
= alloc_smp_req(PC_REQ_SIZE
);
549 pc_resp
= alloc_smp_resp(PC_RESP_SIZE
);
555 pc_req
[1] = SMP_PHY_CONTROL
;
557 pc_req
[10]= phy_func
;
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
);
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
;
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
)
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
,
599 struct domain_device
*dev
;
601 if (SAS_ADDR(port
->sas_addr
) == SAS_ADDR(sas_addr
))
603 list_for_each_entry(dev
, &port
->dev_list
, dev_list_node
) {
604 if (SAS_ADDR(dev
->sas_addr
) == SAS_ADDR(sas_addr
))
610 #define RPEL_REQ_SIZE 16
611 #define RPEL_RESP_SIZE 32
612 int sas_smp_get_phy_events(struct sas_phy
*phy
)
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
);
624 resp
= alloc_smp_resp(RPEL_RESP_SIZE
);
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
);
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]);
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
,
657 struct smp_resp
*rps_resp
)
660 u8
*rps_req
= alloc_smp_req(RPS_REQ_SIZE
);
661 u8
*resp
= (u8
*)rps_resp
;
666 rps_req
[1] = SMP_REPORT_PHY_SATA
;
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
677 if (!res
&& resp
[27] == 0x34 && resp
[24] != 0x34) {
680 for (i
= 0; i
< 5; i
++) {
685 resp
[j
+ 0] = resp
[j
+ 3];
686 resp
[j
+ 1] = resp
[j
+ 2];
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
;
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
)
716 if (SAS_ADDR(phy
->attached_sas_addr
) ==
717 SAS_ADDR(child
->sas_addr
)) {
719 child
->min_linkrate
= min(parent
->min_linkrate
,
721 child
->max_linkrate
= max(parent
->max_linkrate
,
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
;
740 if (phy
->attached_sata_host
|| phy
->attached_sata_ps
)
743 child
= sas_alloc_device();
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
);
754 phy
->port
= sas_port_alloc(&parent
->rphy
->dev
, phy_id
);
755 if (unlikely(!phy
->port
))
757 if (unlikely(sas_port_add(phy
->port
) != 0)) {
758 sas_port_free(phy
->port
);
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
);
775 SAS_DPRINTK("report phy sata to %016llx:0x%x returned "
776 "0x%x\n", SAS_ADDR(parent
->sas_addr
),
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
);
791 list_add_tail(&child
->disco_list_node
, &parent
->port
->disco_list
);
793 res
= sas_discover_sata(child
);
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
);
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 */
809 child
->tproto
= phy
->attached_tproto
;
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
);
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
);
828 SAS_DPRINTK("target proto 0x%x at %016llx:0x%x not handled\n",
829 phy
->attached_tproto
, SAS_ADDR(parent
->sas_addr
),
834 list_add_tail(&child
->siblings
, &parent_ex
->children
);
838 sas_rphy_free(child
->rphy
);
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
);
846 sas_port_delete(phy
->port
);
849 sas_put_device(child
);
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
];
859 for (i
= 0; i
< parent
->ex_dev
.num_phys
; i
++) {
860 struct ex_phy
*ephy
= &parent
->ex_dev
.ex_phy
[i
];
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
;
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
;
888 if (phy
->routing_attr
== DIRECT_ROUTING
) {
889 SAS_DPRINTK("ex %016llx:0x%x:D <--> ex %016llx:0x%x is not "
891 SAS_ADDR(parent
->sas_addr
), phy_id
,
892 SAS_ADDR(phy
->attached_sas_addr
),
893 phy
->attached_phy_id
);
896 child
= sas_alloc_device();
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
) {
907 rphy
= sas_expander_alloc(phy
->port
,
908 SAS_EDGE_EXPANDER_DEVICE
);
911 rphy
= sas_expander_alloc(phy
->port
,
912 SAS_FANOUT_EXPANDER_DEVICE
);
915 rphy
= NULL
; /* shut gcc up */
920 edev
= rphy_to_expander_device(rphy
);
921 child
->dev_type
= phy
->attached_dev_type
;
922 kref_get(&parent
->kref
);
923 child
->parent
= parent
;
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
,
934 sas_fill_in_rphy(child
, 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
);
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
);
949 list_add_tail(&child
->siblings
, &parent
->ex_dev
.children
);
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
;
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
);
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
);
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);
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
);
991 } else if (ex_phy
->linkrate
== SAS_LINK_RATE_UNKNOWN
)
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
),
1004 res
= sas_configure_routing(dev
, ex_phy
->attached_sas_addr
);
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
);
1013 res
= sas_ex_join_wide_port(dev
, phy_id
);
1015 SAS_DPRINTK("Attaching ex phy%d to wide port %016llx\n",
1016 phy_id
, SAS_ADDR(ex_phy
->attached_sas_addr
));
1020 switch (ex_phy
->attached_dev_type
) {
1022 child
= sas_ex_discover_end_dev(dev
, phy_id
);
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
),
1032 sas_ex_disable_phy(dev
, phy_id
);
1035 memcpy(dev
->port
->disc
.fanout_sas_addr
,
1036 ex_phy
->attached_sas_addr
, SAS_ADDR_SIZE
);
1039 child
= sas_ex_discover_expander(dev
, phy_id
);
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
)
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
);
1061 SAS_DPRINTK("Attaching ex phy%d to wide port %016llx\n",
1062 i
, SAS_ADDR(ex
->ex_phy
[i
].attached_sas_addr
));
1071 static int sas_find_sub_addr(struct domain_device
*dev
, u8
*sub_addr
)
1073 struct expander_device
*ex
= &dev
->ex_dev
;
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
)
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
);
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
)
1105 if (sub_addr
[0] == 0) {
1106 sas_find_sub_addr(child
, sub_addr
);
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
),
1120 SAS_ADDR(sub_addr
));
1122 sas_ex_disable_port(child
, s2
);
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
;
1142 if (0 <= single
&& single
< end
) {
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
)
1155 switch (ex_phy
->linkrate
) {
1156 case SAS_PHY_DISABLED
:
1157 case SAS_PHY_RESET_PROBLEM
:
1158 case SAS_SATA_PORT_SELECTOR
:
1161 res
= sas_ex_discover_dev(dev
, i
);
1169 sas_check_level_subtractive_boundary(dev
);
1174 static int sas_check_ex_subtractive_boundary(struct domain_device
*dev
)
1176 struct expander_device
*ex
= &dev
->ex_dev
;
1178 u8
*sub_sas_addr
= NULL
;
1180 if (dev
->dev_type
!= EDGE_DEV
)
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
)
1190 if ((phy
->attached_dev_type
== FANOUT_DEV
||
1191 phy
->attached_dev_type
== EDGE_DEV
) &&
1192 phy
->routing_attr
== SUBTRACTIVE_ROUTING
) {
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
);
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
),
1229 ex_type
[child
->dev_type
],
1230 SAS_ADDR(child
->sas_addr
),
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
)
1242 struct domain_device
*parent
= child
->parent
;
1244 if (SAS_ADDR(parent
->port
->disc
.fanout_sas_addr
) != 0) {
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
),
1250 SAS_ADDR(child
->sas_addr
),
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
,
1256 memcpy(parent
->port
->disc
.eeds_b
, child
->sas_addr
,
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
))))
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
),
1274 SAS_ADDR(child
->sas_addr
),
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
;
1293 if (child
->parent
->dev_type
!= EDGE_DEV
&&
1294 child
->parent
->dev_type
!= FANOUT_DEV
)
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
)
1307 if (SAS_ADDR(parent_phy
->attached_sas_addr
) != SAS_ADDR(child
->sas_addr
))
1310 child_phy
= &child_ex
->ex_phy
[parent_phy
->attached_phy_id
];
1312 switch (child
->parent
->dev_type
) {
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
);
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
);
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
)) {
1333 sas_print_parent_topology_bug(child
, parent_phy
, child_phy
);
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
);
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
)
1360 struct expander_device
*ex
= &dev
->ex_dev
;
1361 struct ex_phy
*phy
= &ex
->ex_phy
[phy_id
];
1368 rri_req
= alloc_smp_req(RRI_REQ_SIZE
);
1372 rri_resp
= alloc_smp_resp(RRI_RESP_SIZE
);
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
,
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
);
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
);
1399 if (SAS_ADDR(sas_addr
) != 0) {
1400 if (SAS_ADDR(rri_resp
+16) == SAS_ADDR(sas_addr
)) {
1402 if ((rri_resp
[12] & 0x80) == 0x80)
1407 } else if (SAS_ADDR(rri_resp
+16) == 0) {
1412 } else if (SAS_ADDR(rri_resp
+16) == 0 &&
1413 phy
->last_da_index
< i
) {
1414 phy
->last_da_index
= i
;
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
)
1437 cri_req
= alloc_smp_req(CRI_REQ_SIZE
);
1441 cri_resp
= alloc_smp_resp(CRI_RESP_SIZE
);
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
,
1459 if (res
== SMP_RESP_NO_INDEX
) {
1460 SAS_DPRINTK("overflow of indexes: dev %016llx phy 0x%x "
1462 SAS_ADDR(dev
->sas_addr
), phy_id
, index
);
1470 static int sas_configure_phy(struct domain_device
*dev
, int phy_id
,
1471 u8
*sas_addr
, int include
)
1477 res
= sas_configure_present(dev
, phy_id
, sas_addr
, &index
, &present
);
1480 if (include
^ present
)
1481 return sas_configure_set(dev
, phy_id
, sas_addr
, index
,include
);
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
;
1500 if (parent
->parent
) {
1501 res
= sas_configure_parent(parent
->parent
, parent
, sas_addr
,
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
));
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
);
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
)
1536 return sas_configure_parent(dev
->parent
, dev
, sas_addr
, 1);
1540 static int sas_disable_routing(struct domain_device
*dev
, u8
*sas_addr
)
1543 return sas_configure_parent(dev
->parent
, dev
, sas_addr
, 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
)
1557 res
= sas_notify_lldd_dev_found(dev
);
1561 res
= sas_ex_general(dev
);
1564 res
= sas_ex_manuf_info(dev
);
1568 res
= sas_expander_discover(dev
);
1570 SAS_DPRINTK("expander %016llx discovery failed(0x%x)\n",
1571 SAS_ADDR(dev
->sas_addr
), res
);
1575 sas_check_ex_subtractive_boundary(dev
);
1576 res
= sas_check_parent_topology(dev
);
1581 sas_notify_lldd_dev_gone(dev
);
1585 static int sas_ex_level_discovery(struct asd_sas_port
*port
, const int level
)
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);
1599 res
= sas_ex_discover_devices(port
->port_dev
, -1);
1607 static int sas_ex_bfs_disc(struct asd_sas_port
*port
)
1613 level
= port
->disc
.max_level
;
1614 res
= sas_ex_level_discovery(port
, level
);
1616 } while (level
< port
->disc
.max_level
);
1621 int sas_discover_root_expander(struct domain_device
*dev
)
1624 struct sas_expander_device
*ex
= rphy_to_expander_device(dev
->rphy
);
1626 res
= sas_rphy_add(dev
->rphy
);
1630 ex
->level
= dev
->port
->disc
.max_level
; /* 0 */
1631 res
= sas_discover_expander(dev
);
1635 sas_ex_bfs_disc(dev
->port
);
1640 sas_rphy_remove(dev
->rphy
);
1645 /* ---------- Domain revalidation ---------- */
1647 static int sas_get_phy_discover(struct domain_device
*dev
,
1648 int phy_id
, struct smp_resp
*disc_resp
)
1653 disc_req
= alloc_smp_req(DISCOVER_REQ_SIZE
);
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
);
1664 else if (disc_resp
->result
!= SMP_RESP_FUNC_ACC
) {
1665 res
= disc_resp
->result
;
1673 static int sas_get_phy_change_count(struct domain_device
*dev
,
1674 int phy_id
, int *pcc
)
1677 struct smp_resp
*disc_resp
;
1679 disc_resp
= alloc_smp_resp(DISCOVER_RESP_SIZE
);
1683 res
= sas_get_phy_discover(dev
, phy_id
, disc_resp
);
1685 *pcc
= disc_resp
->disc
.change_count
;
1691 int sas_get_phy_attached_sas_addr(struct domain_device
*dev
, int phy_id
,
1692 u8
*attached_sas_addr
)
1695 struct smp_resp
*disc_resp
;
1696 struct discover_resp
*dr
;
1698 disc_resp
= alloc_smp_resp(DISCOVER_RESP_SIZE
);
1701 dr
= &disc_resp
->disc
;
1703 res
= sas_get_phy_discover(dev
, phy_id
, disc_resp
);
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);
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
;
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
);
1726 else if (phy_change_count
!= ex
->ex_phy
[i
].phy_change_count
) {
1728 ex
->ex_phy
[i
].phy_change_count
=
1738 static int sas_get_ex_change_count(struct domain_device
*dev
, int *ecc
)
1742 struct smp_resp
*rg_resp
;
1744 rg_req
= alloc_smp_req(RG_REQ_SIZE
);
1748 rg_resp
= alloc_smp_resp(RG_RESP_SIZE
);
1754 rg_req
[1] = SMP_REPORT_GENERAL
;
1756 res
= smp_execute_task(dev
, rg_req
, RG_REQ_SIZE
, rg_resp
,
1760 if (rg_resp
->result
!= SMP_RESP_FUNC_ACC
) {
1761 res
= rg_resp
->result
;
1765 *ecc
= be16_to_cpu(rg_resp
->rg
.change_count
);
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
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;
1793 struct domain_device
*ch
;
1795 res
= sas_get_ex_change_count(dev
, &ex_change_count
);
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);
1806 ex
->ex_change_count
= ex_change_count
;
1807 SAS_DPRINTK("Expander phy change count has changed\n");
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
);
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
);
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
;
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
);
1855 sas_unregister_dev(parent
->port
, child
);
1860 sas_disable_routing(parent
, phy
->attached_sas_addr
);
1862 memset(phy
->attached_sas_addr
, 0, SAS_ADDR_SIZE
);
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
);
1872 static int sas_discover_bfs_by_root_level(struct domain_device
*root
,
1875 struct expander_device
*ex_root
= &root
->ex_dev
;
1876 struct domain_device
*child
;
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
,
1888 else if (level
== ex
->level
)
1889 res
= sas_ex_discover_devices(child
, -1);
1895 static int sas_discover_bfs_by_root(struct domain_device
*dev
)
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);
1905 res
= sas_discover_bfs_by_root_level(dev
, level
);
1908 } while (level
<= dev
->port
->disc
.max_level
);
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
;
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
);
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
];
1930 if (SAS_ADDR(ex_phy_temp
->attached_sas_addr
) ==
1931 SAS_ADDR(ex_phy
->attached_sas_addr
)) {
1937 sas_ex_join_wide_port(dev
, phy_id
);
1940 res
= sas_ex_discover_devices(dev
, phy_id
);
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
);
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];
1963 res
= sas_get_phy_attached_sas_addr(dev
, phy_id
, attached_sas_addr
);
1965 case SMP_RESP_NO_PHY
:
1966 phy
->phy_state
= PHY_NOT_PRESENT
;
1967 sas_unregister_devs_sas_addr(dev
, phy_id
, last
);
1969 case SMP_RESP_PHY_VACANT
:
1970 phy
->phy_state
= PHY_VACANT
;
1971 sas_unregister_devs_sas_addr(dev
, phy_id
, last
);
1973 case SMP_RESP_FUNC_ACC
:
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
);
1986 res
= sas_discover_new(dev
, phy_id
);
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
];
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
];
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
);
2030 res
= sas_rediscover_dev(dev
, phy_id
, last
);
2032 res
= sas_discover_new(dev
, phy_id
);
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
2045 int sas_ex_revalidate_domain(struct domain_device
*port_dev
)
2048 struct domain_device
*dev
= NULL
;
2050 res
= sas_find_bcast_dev(port_dev
, &dev
);
2054 struct expander_device
*ex
= &dev
->ex_dev
;
2059 res
= sas_find_bcast_phy(dev
, &phy_id
, i
, true);
2062 res
= sas_rediscover(dev
, phy_id
);
2064 } while (i
< ex
->num_phys
);
2070 int sas_smp_handler(struct Scsi_Host
*shost
, struct sas_rphy
*rphy
,
2071 struct request
*req
)
2073 struct domain_device
*dev
;
2075 struct request
*rsp
= req
->next_rq
;
2078 printk("%s: space for a smp response is missing\n",
2083 /* no rphy means no smp target support (ie aic94xx host) */
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",
2096 dev
= sas_find_dev_by_rphy(rphy
);
2098 printk("%s: fail to find a domain_device?\n", __func__
);
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
));
2110 ret
= smp_execute_task(dev
, bio_data(req
->bio
), blk_rq_bytes(req
),
2111 bio_data(rsp
->bio
), blk_rq_bytes(rsp
));
2113 /* positive number is the untransferred residual */
2114 rsp
->resid_len
= ret
;
2117 } else if (ret
== 0) {