2 * Serial Attached SCSI (SAS) Expander discovery and configuration
4 * Copyright (C) 2005 Adaptec, Inc. All rights reserved.
5 * Copyright (C) 2005 Luben Tuikov <luben_tuikov@adaptec.com>
7 * This file is licensed under GPLv2.
9 * This program is free software; you can redistribute it and/or
10 * modify it under the terms of the GNU General Public License as
11 * published by the Free Software Foundation; either version 2 of the
12 * License, or (at your option) any later version.
14 * This program is distributed in the hope that it will be useful, but
15 * WITHOUT ANY WARRANTY; without even the implied warranty of
16 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
17 * General Public License for more details.
19 * You should have received a copy of the GNU General Public License
20 * along with this program; if not, write to the Free Software
21 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
25 #include <linux/scatterlist.h>
26 #include <linux/blkdev.h>
28 #include "sas_internal.h"
30 #include <scsi/scsi_transport.h>
31 #include <scsi/scsi_transport_sas.h>
32 #include "../scsi_sas_internal.h"
34 static int sas_discover_expander(struct domain_device
*dev
);
35 static int sas_configure_routing(struct domain_device
*dev
, u8
*sas_addr
);
36 static int sas_configure_phy(struct domain_device
*dev
, int phy_id
,
37 u8
*sas_addr
, int include
);
38 static int sas_disable_routing(struct domain_device
*dev
, u8
*sas_addr
);
40 /* ---------- SMP task management ---------- */
42 static void smp_task_timedout(unsigned long _task
)
44 struct sas_task
*task
= (void *) _task
;
47 spin_lock_irqsave(&task
->task_state_lock
, flags
);
48 if (!(task
->task_state_flags
& SAS_TASK_STATE_DONE
))
49 task
->task_state_flags
|= SAS_TASK_STATE_ABORTED
;
50 spin_unlock_irqrestore(&task
->task_state_lock
, flags
);
52 complete(&task
->completion
);
55 static void smp_task_done(struct sas_task
*task
)
57 if (!del_timer(&task
->timer
))
59 complete(&task
->completion
);
62 /* Give it some long enough timeout. In seconds. */
63 #define SMP_TIMEOUT 10
65 static int smp_execute_task(struct domain_device
*dev
, void *req
, int req_size
,
66 void *resp
, int resp_size
)
69 struct sas_task
*task
= NULL
;
70 struct sas_internal
*i
=
71 to_sas_internal(dev
->port
->ha
->core
.shost
->transportt
);
73 for (retry
= 0; retry
< 3; retry
++) {
74 task
= sas_alloc_task(GFP_KERNEL
);
79 task
->task_proto
= dev
->tproto
;
80 sg_init_one(&task
->smp_task
.smp_req
, req
, req_size
);
81 sg_init_one(&task
->smp_task
.smp_resp
, resp
, resp_size
);
83 task
->task_done
= smp_task_done
;
85 task
->timer
.data
= (unsigned long) task
;
86 task
->timer
.function
= smp_task_timedout
;
87 task
->timer
.expires
= jiffies
+ SMP_TIMEOUT
*HZ
;
88 add_timer(&task
->timer
);
90 res
= i
->dft
->lldd_execute_task(task
, 1, GFP_KERNEL
);
93 del_timer(&task
->timer
);
94 SAS_DPRINTK("executing SMP task failed:%d\n", res
);
98 wait_for_completion(&task
->completion
);
100 if ((task
->task_state_flags
& SAS_TASK_STATE_ABORTED
)) {
101 SAS_DPRINTK("smp task timed out or aborted\n");
102 i
->dft
->lldd_abort_task(task
);
103 if (!(task
->task_state_flags
& SAS_TASK_STATE_DONE
)) {
104 SAS_DPRINTK("SMP task aborted and not done\n");
108 if (task
->task_status
.resp
== SAS_TASK_COMPLETE
&&
109 task
->task_status
.stat
== SAM_GOOD
) {
112 } if (task
->task_status
.resp
== SAS_TASK_COMPLETE
&&
113 task
->task_status
.stat
== SAS_DATA_UNDERRUN
) {
114 /* no error, but return the number of bytes of
116 res
= task
->task_status
.residual
;
118 } if (task
->task_status
.resp
== SAS_TASK_COMPLETE
&&
119 task
->task_status
.stat
== SAS_DATA_OVERRUN
) {
123 SAS_DPRINTK("%s: task to dev %016llx response: 0x%x "
124 "status 0x%x\n", __func__
,
125 SAS_ADDR(dev
->sas_addr
),
126 task
->task_status
.resp
,
127 task
->task_status
.stat
);
133 BUG_ON(retry
== 3 && task
!= NULL
);
140 /* ---------- Allocations ---------- */
142 static inline void *alloc_smp_req(int size
)
144 u8
*p
= kzalloc(size
, GFP_KERNEL
);
150 static inline void *alloc_smp_resp(int size
)
152 return kzalloc(size
, GFP_KERNEL
);
155 /* ---------- Expander configuration ---------- */
157 static void sas_set_ex_phy(struct domain_device
*dev
, int phy_id
,
160 struct expander_device
*ex
= &dev
->ex_dev
;
161 struct ex_phy
*phy
= &ex
->ex_phy
[phy_id
];
162 struct smp_resp
*resp
= disc_resp
;
163 struct discover_resp
*dr
= &resp
->disc
;
164 struct sas_rphy
*rphy
= dev
->rphy
;
165 int rediscover
= (phy
->phy
!= NULL
);
168 phy
->phy
= sas_phy_alloc(&rphy
->dev
, phy_id
);
170 /* FIXME: error_handling */
174 switch (resp
->result
) {
175 case SMP_RESP_PHY_VACANT
:
176 phy
->phy_state
= PHY_VACANT
;
179 phy
->phy_state
= PHY_NOT_PRESENT
;
181 case SMP_RESP_FUNC_ACC
:
182 phy
->phy_state
= PHY_EMPTY
; /* do not know yet */
186 phy
->phy_id
= phy_id
;
187 phy
->attached_dev_type
= dr
->attached_dev_type
;
188 phy
->linkrate
= dr
->linkrate
;
189 phy
->attached_sata_host
= dr
->attached_sata_host
;
190 phy
->attached_sata_dev
= dr
->attached_sata_dev
;
191 phy
->attached_sata_ps
= dr
->attached_sata_ps
;
192 phy
->attached_iproto
= dr
->iproto
<< 1;
193 phy
->attached_tproto
= dr
->tproto
<< 1;
194 memcpy(phy
->attached_sas_addr
, dr
->attached_sas_addr
, SAS_ADDR_SIZE
);
195 phy
->attached_phy_id
= dr
->attached_phy_id
;
196 phy
->phy_change_count
= dr
->change_count
;
197 phy
->routing_attr
= dr
->routing_attr
;
198 phy
->virtual = dr
->virtual;
199 phy
->last_da_index
= -1;
201 phy
->phy
->identify
.initiator_port_protocols
= phy
->attached_iproto
;
202 phy
->phy
->identify
.target_port_protocols
= phy
->attached_tproto
;
203 phy
->phy
->identify
.phy_identifier
= phy_id
;
204 phy
->phy
->minimum_linkrate_hw
= dr
->hmin_linkrate
;
205 phy
->phy
->maximum_linkrate_hw
= dr
->hmax_linkrate
;
206 phy
->phy
->minimum_linkrate
= dr
->pmin_linkrate
;
207 phy
->phy
->maximum_linkrate
= dr
->pmax_linkrate
;
208 phy
->phy
->negotiated_linkrate
= phy
->linkrate
;
211 sas_phy_add(phy
->phy
);
213 SAS_DPRINTK("ex %016llx phy%02d:%c attached: %016llx\n",
214 SAS_ADDR(dev
->sas_addr
), phy
->phy_id
,
215 phy
->routing_attr
== TABLE_ROUTING
? 'T' :
216 phy
->routing_attr
== DIRECT_ROUTING
? 'D' :
217 phy
->routing_attr
== SUBTRACTIVE_ROUTING
? 'S' : '?',
218 SAS_ADDR(phy
->attached_sas_addr
));
223 #define DISCOVER_REQ_SIZE 16
224 #define DISCOVER_RESP_SIZE 56
226 static int sas_ex_phy_discover_helper(struct domain_device
*dev
, u8
*disc_req
,
227 u8
*disc_resp
, int single
)
231 disc_req
[9] = single
;
232 for (i
= 1 ; i
< 3; i
++) {
233 struct discover_resp
*dr
;
235 res
= smp_execute_task(dev
, disc_req
, DISCOVER_REQ_SIZE
,
236 disc_resp
, DISCOVER_RESP_SIZE
);
239 /* This is detecting a failure to transmit inital
240 * dev to host FIS as described in section G.5 of
242 dr
= &((struct smp_resp
*)disc_resp
)->disc
;
243 if (!(dr
->attached_dev_type
== 0 &&
244 dr
->attached_sata_dev
))
246 /* In order to generate the dev to host FIS, we
247 * send a link reset to the expander port */
248 sas_smp_phy_control(dev
, single
, PHY_FUNC_LINK_RESET
, NULL
);
249 /* Wait for the reset to trigger the negotiation */
252 sas_set_ex_phy(dev
, single
, disc_resp
);
256 static int sas_ex_phy_discover(struct domain_device
*dev
, int single
)
258 struct expander_device
*ex
= &dev
->ex_dev
;
263 disc_req
= alloc_smp_req(DISCOVER_REQ_SIZE
);
267 disc_resp
= alloc_smp_req(DISCOVER_RESP_SIZE
);
273 disc_req
[1] = SMP_DISCOVER
;
275 if (0 <= single
&& single
< ex
->num_phys
) {
276 res
= sas_ex_phy_discover_helper(dev
, disc_req
, disc_resp
, single
);
280 for (i
= 0; i
< ex
->num_phys
; i
++) {
281 res
= sas_ex_phy_discover_helper(dev
, disc_req
,
293 static int sas_expander_discover(struct domain_device
*dev
)
295 struct expander_device
*ex
= &dev
->ex_dev
;
298 ex
->ex_phy
= kzalloc(sizeof(*ex
->ex_phy
)*ex
->num_phys
, GFP_KERNEL
);
302 res
= sas_ex_phy_discover(dev
, -1);
313 #define MAX_EXPANDER_PHYS 128
315 static void ex_assign_report_general(struct domain_device
*dev
,
316 struct smp_resp
*resp
)
318 struct report_general_resp
*rg
= &resp
->rg
;
320 dev
->ex_dev
.ex_change_count
= be16_to_cpu(rg
->change_count
);
321 dev
->ex_dev
.max_route_indexes
= be16_to_cpu(rg
->route_indexes
);
322 dev
->ex_dev
.num_phys
= min(rg
->num_phys
, (u8
)MAX_EXPANDER_PHYS
);
323 dev
->ex_dev
.conf_route_table
= rg
->conf_route_table
;
324 dev
->ex_dev
.configuring
= rg
->configuring
;
325 memcpy(dev
->ex_dev
.enclosure_logical_id
, rg
->enclosure_logical_id
, 8);
328 #define RG_REQ_SIZE 8
329 #define RG_RESP_SIZE 32
331 static int sas_ex_general(struct domain_device
*dev
)
334 struct smp_resp
*rg_resp
;
338 rg_req
= alloc_smp_req(RG_REQ_SIZE
);
342 rg_resp
= alloc_smp_resp(RG_RESP_SIZE
);
348 rg_req
[1] = SMP_REPORT_GENERAL
;
350 for (i
= 0; i
< 5; i
++) {
351 res
= smp_execute_task(dev
, rg_req
, RG_REQ_SIZE
, rg_resp
,
355 SAS_DPRINTK("RG to ex %016llx failed:0x%x\n",
356 SAS_ADDR(dev
->sas_addr
), res
);
358 } else if (rg_resp
->result
!= SMP_RESP_FUNC_ACC
) {
359 SAS_DPRINTK("RG:ex %016llx returned SMP result:0x%x\n",
360 SAS_ADDR(dev
->sas_addr
), rg_resp
->result
);
361 res
= rg_resp
->result
;
365 ex_assign_report_general(dev
, rg_resp
);
367 if (dev
->ex_dev
.configuring
) {
368 SAS_DPRINTK("RG: ex %llx self-configuring...\n",
369 SAS_ADDR(dev
->sas_addr
));
370 schedule_timeout_interruptible(5*HZ
);
380 static void ex_assign_manuf_info(struct domain_device
*dev
, void
383 u8
*mi_resp
= _mi_resp
;
384 struct sas_rphy
*rphy
= dev
->rphy
;
385 struct sas_expander_device
*edev
= rphy_to_expander_device(rphy
);
387 memcpy(edev
->vendor_id
, mi_resp
+ 12, SAS_EXPANDER_VENDOR_ID_LEN
);
388 memcpy(edev
->product_id
, mi_resp
+ 20, SAS_EXPANDER_PRODUCT_ID_LEN
);
389 memcpy(edev
->product_rev
, mi_resp
+ 36,
390 SAS_EXPANDER_PRODUCT_REV_LEN
);
392 if (mi_resp
[8] & 1) {
393 memcpy(edev
->component_vendor_id
, mi_resp
+ 40,
394 SAS_EXPANDER_COMPONENT_VENDOR_ID_LEN
);
395 edev
->component_id
= mi_resp
[48] << 8 | mi_resp
[49];
396 edev
->component_revision_id
= mi_resp
[50];
400 #define MI_REQ_SIZE 8
401 #define MI_RESP_SIZE 64
403 static int sas_ex_manuf_info(struct domain_device
*dev
)
409 mi_req
= alloc_smp_req(MI_REQ_SIZE
);
413 mi_resp
= alloc_smp_resp(MI_RESP_SIZE
);
419 mi_req
[1] = SMP_REPORT_MANUF_INFO
;
421 res
= smp_execute_task(dev
, mi_req
, MI_REQ_SIZE
, mi_resp
,MI_RESP_SIZE
);
423 SAS_DPRINTK("MI: ex %016llx failed:0x%x\n",
424 SAS_ADDR(dev
->sas_addr
), res
);
426 } else if (mi_resp
[2] != SMP_RESP_FUNC_ACC
) {
427 SAS_DPRINTK("MI ex %016llx returned SMP result:0x%x\n",
428 SAS_ADDR(dev
->sas_addr
), mi_resp
[2]);
432 ex_assign_manuf_info(dev
, mi_resp
);
439 #define PC_REQ_SIZE 44
440 #define PC_RESP_SIZE 8
442 int sas_smp_phy_control(struct domain_device
*dev
, int phy_id
,
443 enum phy_func phy_func
,
444 struct sas_phy_linkrates
*rates
)
450 pc_req
= alloc_smp_req(PC_REQ_SIZE
);
454 pc_resp
= alloc_smp_resp(PC_RESP_SIZE
);
460 pc_req
[1] = SMP_PHY_CONTROL
;
462 pc_req
[10]= phy_func
;
464 pc_req
[32] = rates
->minimum_linkrate
<< 4;
465 pc_req
[33] = rates
->maximum_linkrate
<< 4;
468 res
= smp_execute_task(dev
, pc_req
, PC_REQ_SIZE
, pc_resp
,PC_RESP_SIZE
);
475 static void sas_ex_disable_phy(struct domain_device
*dev
, int phy_id
)
477 struct expander_device
*ex
= &dev
->ex_dev
;
478 struct ex_phy
*phy
= &ex
->ex_phy
[phy_id
];
480 sas_smp_phy_control(dev
, phy_id
, PHY_FUNC_DISABLE
, NULL
);
481 phy
->linkrate
= SAS_PHY_DISABLED
;
484 static void sas_ex_disable_port(struct domain_device
*dev
, u8
*sas_addr
)
486 struct expander_device
*ex
= &dev
->ex_dev
;
489 for (i
= 0; i
< ex
->num_phys
; i
++) {
490 struct ex_phy
*phy
= &ex
->ex_phy
[i
];
492 if (phy
->phy_state
== PHY_VACANT
||
493 phy
->phy_state
== PHY_NOT_PRESENT
)
496 if (SAS_ADDR(phy
->attached_sas_addr
) == SAS_ADDR(sas_addr
))
497 sas_ex_disable_phy(dev
, i
);
501 static int sas_dev_present_in_domain(struct asd_sas_port
*port
,
504 struct domain_device
*dev
;
506 if (SAS_ADDR(port
->sas_addr
) == SAS_ADDR(sas_addr
))
508 list_for_each_entry(dev
, &port
->dev_list
, dev_list_node
) {
509 if (SAS_ADDR(dev
->sas_addr
) == SAS_ADDR(sas_addr
))
515 #define RPEL_REQ_SIZE 16
516 #define RPEL_RESP_SIZE 32
517 int sas_smp_get_phy_events(struct sas_phy
*phy
)
522 struct sas_rphy
*rphy
= dev_to_rphy(phy
->dev
.parent
);
523 struct domain_device
*dev
= sas_find_dev_by_rphy(rphy
);
525 req
= alloc_smp_req(RPEL_REQ_SIZE
);
529 resp
= alloc_smp_resp(RPEL_RESP_SIZE
);
535 req
[1] = SMP_REPORT_PHY_ERR_LOG
;
536 req
[9] = phy
->number
;
538 res
= smp_execute_task(dev
, req
, RPEL_REQ_SIZE
,
539 resp
, RPEL_RESP_SIZE
);
544 phy
->invalid_dword_count
= scsi_to_u32(&resp
[12]);
545 phy
->running_disparity_error_count
= scsi_to_u32(&resp
[16]);
546 phy
->loss_of_dword_sync_count
= scsi_to_u32(&resp
[20]);
547 phy
->phy_reset_problem_count
= scsi_to_u32(&resp
[24]);
555 #ifdef CONFIG_SCSI_SAS_ATA
557 #define RPS_REQ_SIZE 16
558 #define RPS_RESP_SIZE 60
560 static int sas_get_report_phy_sata(struct domain_device
*dev
,
562 struct smp_resp
*rps_resp
)
565 u8
*rps_req
= alloc_smp_req(RPS_REQ_SIZE
);
566 u8
*resp
= (u8
*)rps_resp
;
571 rps_req
[1] = SMP_REPORT_PHY_SATA
;
574 res
= smp_execute_task(dev
, rps_req
, RPS_REQ_SIZE
,
575 rps_resp
, RPS_RESP_SIZE
);
577 /* 0x34 is the FIS type for the D2H fis. There's a potential
578 * standards cockup here. sas-2 explicitly specifies the FIS
579 * should be encoded so that FIS type is in resp[24].
580 * However, some expanders endian reverse this. Undo the
582 if (!res
&& resp
[27] == 0x34 && resp
[24] != 0x34) {
585 for (i
= 0; i
< 5; i
++) {
590 resp
[j
+ 0] = resp
[j
+ 3];
591 resp
[j
+ 1] = resp
[j
+ 2];
602 static void sas_ex_get_linkrate(struct domain_device
*parent
,
603 struct domain_device
*child
,
604 struct ex_phy
*parent_phy
)
606 struct expander_device
*parent_ex
= &parent
->ex_dev
;
607 struct sas_port
*port
;
612 port
= parent_phy
->port
;
614 for (i
= 0; i
< parent_ex
->num_phys
; i
++) {
615 struct ex_phy
*phy
= &parent_ex
->ex_phy
[i
];
617 if (phy
->phy_state
== PHY_VACANT
||
618 phy
->phy_state
== PHY_NOT_PRESENT
)
621 if (SAS_ADDR(phy
->attached_sas_addr
) ==
622 SAS_ADDR(child
->sas_addr
)) {
624 child
->min_linkrate
= min(parent
->min_linkrate
,
626 child
->max_linkrate
= max(parent
->max_linkrate
,
629 sas_port_add_phy(port
, phy
->phy
);
632 child
->linkrate
= min(parent_phy
->linkrate
, child
->max_linkrate
);
633 child
->pathways
= min(child
->pathways
, parent
->pathways
);
636 static struct domain_device
*sas_ex_discover_end_dev(
637 struct domain_device
*parent
, int phy_id
)
639 struct expander_device
*parent_ex
= &parent
->ex_dev
;
640 struct ex_phy
*phy
= &parent_ex
->ex_phy
[phy_id
];
641 struct domain_device
*child
= NULL
;
642 struct sas_rphy
*rphy
;
645 if (phy
->attached_sata_host
|| phy
->attached_sata_ps
)
648 child
= kzalloc(sizeof(*child
), GFP_KERNEL
);
652 child
->parent
= parent
;
653 child
->port
= parent
->port
;
654 child
->iproto
= phy
->attached_iproto
;
655 memcpy(child
->sas_addr
, phy
->attached_sas_addr
, SAS_ADDR_SIZE
);
656 sas_hash_addr(child
->hashed_sas_addr
, child
->sas_addr
);
658 phy
->port
= sas_port_alloc(&parent
->rphy
->dev
, phy_id
);
659 if (unlikely(!phy
->port
))
661 if (unlikely(sas_port_add(phy
->port
) != 0)) {
662 sas_port_free(phy
->port
);
666 sas_ex_get_linkrate(parent
, child
, phy
);
668 #ifdef CONFIG_SCSI_SAS_ATA
669 if ((phy
->attached_tproto
& SAS_PROTOCOL_STP
) || phy
->attached_sata_dev
) {
670 child
->dev_type
= SATA_DEV
;
671 if (phy
->attached_tproto
& SAS_PROTOCOL_STP
)
672 child
->tproto
= phy
->attached_tproto
;
673 if (phy
->attached_sata_dev
)
674 child
->tproto
|= SATA_DEV
;
675 res
= sas_get_report_phy_sata(parent
, phy_id
,
676 &child
->sata_dev
.rps_resp
);
678 SAS_DPRINTK("report phy sata to %016llx:0x%x returned "
679 "0x%x\n", SAS_ADDR(parent
->sas_addr
),
683 memcpy(child
->frame_rcvd
, &child
->sata_dev
.rps_resp
.rps
.fis
,
684 sizeof(struct dev_to_host_fis
));
686 rphy
= sas_end_device_alloc(phy
->port
);
694 spin_lock_irq(&parent
->port
->dev_list_lock
);
695 list_add_tail(&child
->dev_list_node
, &parent
->port
->dev_list
);
696 spin_unlock_irq(&parent
->port
->dev_list_lock
);
698 res
= sas_discover_sata(child
);
700 SAS_DPRINTK("sas_discover_sata() for device %16llx at "
701 "%016llx:0x%x returned 0x%x\n",
702 SAS_ADDR(child
->sas_addr
),
703 SAS_ADDR(parent
->sas_addr
), phy_id
, res
);
708 if (phy
->attached_tproto
& SAS_PROTOCOL_SSP
) {
709 child
->dev_type
= SAS_END_DEV
;
710 rphy
= sas_end_device_alloc(phy
->port
);
711 /* FIXME: error handling */
714 child
->tproto
= phy
->attached_tproto
;
718 sas_fill_in_rphy(child
, rphy
);
720 spin_lock_irq(&parent
->port
->dev_list_lock
);
721 list_add_tail(&child
->dev_list_node
, &parent
->port
->dev_list
);
722 spin_unlock_irq(&parent
->port
->dev_list_lock
);
724 res
= sas_discover_end_dev(child
);
726 SAS_DPRINTK("sas_discover_end_dev() for device %16llx "
727 "at %016llx:0x%x returned 0x%x\n",
728 SAS_ADDR(child
->sas_addr
),
729 SAS_ADDR(parent
->sas_addr
), phy_id
, res
);
733 SAS_DPRINTK("target proto 0x%x at %016llx:0x%x not handled\n",
734 phy
->attached_tproto
, SAS_ADDR(parent
->sas_addr
),
739 list_add_tail(&child
->siblings
, &parent_ex
->children
);
743 sas_rphy_free(child
->rphy
);
745 list_del(&child
->dev_list_node
);
747 sas_port_delete(phy
->port
);
754 /* See if this phy is part of a wide port */
755 static int sas_ex_join_wide_port(struct domain_device
*parent
, int phy_id
)
757 struct ex_phy
*phy
= &parent
->ex_dev
.ex_phy
[phy_id
];
760 for (i
= 0; i
< parent
->ex_dev
.num_phys
; i
++) {
761 struct ex_phy
*ephy
= &parent
->ex_dev
.ex_phy
[i
];
766 if (!memcmp(phy
->attached_sas_addr
, ephy
->attached_sas_addr
,
767 SAS_ADDR_SIZE
) && ephy
->port
) {
768 sas_port_add_phy(ephy
->port
, phy
->phy
);
769 phy
->port
= ephy
->port
;
770 phy
->phy_state
= PHY_DEVICE_DISCOVERED
;
778 static struct domain_device
*sas_ex_discover_expander(
779 struct domain_device
*parent
, int phy_id
)
781 struct sas_expander_device
*parent_ex
= rphy_to_expander_device(parent
->rphy
);
782 struct ex_phy
*phy
= &parent
->ex_dev
.ex_phy
[phy_id
];
783 struct domain_device
*child
= NULL
;
784 struct sas_rphy
*rphy
;
785 struct sas_expander_device
*edev
;
786 struct asd_sas_port
*port
;
789 if (phy
->routing_attr
== DIRECT_ROUTING
) {
790 SAS_DPRINTK("ex %016llx:0x%x:D <--> ex %016llx:0x%x is not "
792 SAS_ADDR(parent
->sas_addr
), phy_id
,
793 SAS_ADDR(phy
->attached_sas_addr
),
794 phy
->attached_phy_id
);
797 child
= kzalloc(sizeof(*child
), GFP_KERNEL
);
801 phy
->port
= sas_port_alloc(&parent
->rphy
->dev
, phy_id
);
802 /* FIXME: better error handling */
803 BUG_ON(sas_port_add(phy
->port
) != 0);
806 switch (phy
->attached_dev_type
) {
808 rphy
= sas_expander_alloc(phy
->port
,
809 SAS_EDGE_EXPANDER_DEVICE
);
812 rphy
= sas_expander_alloc(phy
->port
,
813 SAS_FANOUT_EXPANDER_DEVICE
);
816 rphy
= NULL
; /* shut gcc up */
821 edev
= rphy_to_expander_device(rphy
);
822 child
->dev_type
= phy
->attached_dev_type
;
823 child
->parent
= parent
;
825 child
->iproto
= phy
->attached_iproto
;
826 child
->tproto
= phy
->attached_tproto
;
827 memcpy(child
->sas_addr
, phy
->attached_sas_addr
, SAS_ADDR_SIZE
);
828 sas_hash_addr(child
->hashed_sas_addr
, child
->sas_addr
);
829 sas_ex_get_linkrate(parent
, child
, phy
);
830 edev
->level
= parent_ex
->level
+ 1;
831 parent
->port
->disc
.max_level
= max(parent
->port
->disc
.max_level
,
834 sas_fill_in_rphy(child
, rphy
);
837 spin_lock_irq(&parent
->port
->dev_list_lock
);
838 list_add_tail(&child
->dev_list_node
, &parent
->port
->dev_list
);
839 spin_unlock_irq(&parent
->port
->dev_list_lock
);
841 res
= sas_discover_expander(child
);
846 list_add_tail(&child
->siblings
, &parent
->ex_dev
.children
);
850 static int sas_ex_discover_dev(struct domain_device
*dev
, int phy_id
)
852 struct expander_device
*ex
= &dev
->ex_dev
;
853 struct ex_phy
*ex_phy
= &ex
->ex_phy
[phy_id
];
854 struct domain_device
*child
= NULL
;
858 if (ex_phy
->linkrate
== SAS_SATA_SPINUP_HOLD
) {
859 if (!sas_smp_phy_control(dev
, phy_id
, PHY_FUNC_LINK_RESET
, NULL
))
860 res
= sas_ex_phy_discover(dev
, phy_id
);
865 /* Parent and domain coherency */
866 if (!dev
->parent
&& (SAS_ADDR(ex_phy
->attached_sas_addr
) ==
867 SAS_ADDR(dev
->port
->sas_addr
))) {
868 sas_add_parent_port(dev
, phy_id
);
871 if (dev
->parent
&& (SAS_ADDR(ex_phy
->attached_sas_addr
) ==
872 SAS_ADDR(dev
->parent
->sas_addr
))) {
873 sas_add_parent_port(dev
, phy_id
);
874 if (ex_phy
->routing_attr
== TABLE_ROUTING
)
875 sas_configure_phy(dev
, phy_id
, dev
->port
->sas_addr
, 1);
879 if (sas_dev_present_in_domain(dev
->port
, ex_phy
->attached_sas_addr
))
880 sas_ex_disable_port(dev
, ex_phy
->attached_sas_addr
);
882 if (ex_phy
->attached_dev_type
== NO_DEVICE
) {
883 if (ex_phy
->routing_attr
== DIRECT_ROUTING
) {
884 memset(ex_phy
->attached_sas_addr
, 0, SAS_ADDR_SIZE
);
885 sas_configure_routing(dev
, ex_phy
->attached_sas_addr
);
888 } else if (ex_phy
->linkrate
== SAS_LINK_RATE_UNKNOWN
)
891 if (ex_phy
->attached_dev_type
!= SAS_END_DEV
&&
892 ex_phy
->attached_dev_type
!= FANOUT_DEV
&&
893 ex_phy
->attached_dev_type
!= EDGE_DEV
) {
894 SAS_DPRINTK("unknown device type(0x%x) attached to ex %016llx "
895 "phy 0x%x\n", ex_phy
->attached_dev_type
,
896 SAS_ADDR(dev
->sas_addr
),
901 res
= sas_configure_routing(dev
, ex_phy
->attached_sas_addr
);
903 SAS_DPRINTK("configure routing for dev %016llx "
904 "reported 0x%x. Forgotten\n",
905 SAS_ADDR(ex_phy
->attached_sas_addr
), res
);
906 sas_disable_routing(dev
, ex_phy
->attached_sas_addr
);
910 res
= sas_ex_join_wide_port(dev
, phy_id
);
912 SAS_DPRINTK("Attaching ex phy%d to wide port %016llx\n",
913 phy_id
, SAS_ADDR(ex_phy
->attached_sas_addr
));
917 switch (ex_phy
->attached_dev_type
) {
919 child
= sas_ex_discover_end_dev(dev
, phy_id
);
922 if (SAS_ADDR(dev
->port
->disc
.fanout_sas_addr
)) {
923 SAS_DPRINTK("second fanout expander %016llx phy 0x%x "
924 "attached to ex %016llx phy 0x%x\n",
925 SAS_ADDR(ex_phy
->attached_sas_addr
),
926 ex_phy
->attached_phy_id
,
927 SAS_ADDR(dev
->sas_addr
),
929 sas_ex_disable_phy(dev
, phy_id
);
932 memcpy(dev
->port
->disc
.fanout_sas_addr
,
933 ex_phy
->attached_sas_addr
, SAS_ADDR_SIZE
);
936 child
= sas_ex_discover_expander(dev
, phy_id
);
945 for (i
= 0; i
< ex
->num_phys
; i
++) {
946 if (ex
->ex_phy
[i
].phy_state
== PHY_VACANT
||
947 ex
->ex_phy
[i
].phy_state
== PHY_NOT_PRESENT
)
950 * Due to races, the phy might not get added to the
951 * wide port, so we add the phy to the wide port here.
953 if (SAS_ADDR(ex
->ex_phy
[i
].attached_sas_addr
) ==
954 SAS_ADDR(child
->sas_addr
)) {
955 ex
->ex_phy
[i
].phy_state
= PHY_DEVICE_DISCOVERED
;
956 res
= sas_ex_join_wide_port(dev
, i
);
958 SAS_DPRINTK("Attaching ex phy%d to wide port %016llx\n",
959 i
, SAS_ADDR(ex
->ex_phy
[i
].attached_sas_addr
));
969 static int sas_find_sub_addr(struct domain_device
*dev
, u8
*sub_addr
)
971 struct expander_device
*ex
= &dev
->ex_dev
;
974 for (i
= 0; i
< ex
->num_phys
; i
++) {
975 struct ex_phy
*phy
= &ex
->ex_phy
[i
];
977 if (phy
->phy_state
== PHY_VACANT
||
978 phy
->phy_state
== PHY_NOT_PRESENT
)
981 if ((phy
->attached_dev_type
== EDGE_DEV
||
982 phy
->attached_dev_type
== FANOUT_DEV
) &&
983 phy
->routing_attr
== SUBTRACTIVE_ROUTING
) {
985 memcpy(sub_addr
, phy
->attached_sas_addr
,SAS_ADDR_SIZE
);
993 static int sas_check_level_subtractive_boundary(struct domain_device
*dev
)
995 struct expander_device
*ex
= &dev
->ex_dev
;
996 struct domain_device
*child
;
997 u8 sub_addr
[8] = {0, };
999 list_for_each_entry(child
, &ex
->children
, siblings
) {
1000 if (child
->dev_type
!= EDGE_DEV
&&
1001 child
->dev_type
!= FANOUT_DEV
)
1003 if (sub_addr
[0] == 0) {
1004 sas_find_sub_addr(child
, sub_addr
);
1009 if (sas_find_sub_addr(child
, s2
) &&
1010 (SAS_ADDR(sub_addr
) != SAS_ADDR(s2
))) {
1012 SAS_DPRINTK("ex %016llx->%016llx-?->%016llx "
1013 "diverges from subtractive "
1014 "boundary %016llx\n",
1015 SAS_ADDR(dev
->sas_addr
),
1016 SAS_ADDR(child
->sas_addr
),
1018 SAS_ADDR(sub_addr
));
1020 sas_ex_disable_port(child
, s2
);
1027 * sas_ex_discover_devices -- discover devices attached to this expander
1028 * dev: pointer to the expander domain device
1029 * single: if you want to do a single phy, else set to -1;
1031 * Configure this expander for use with its devices and register the
1032 * devices of this expander.
1034 static int sas_ex_discover_devices(struct domain_device
*dev
, int single
)
1036 struct expander_device
*ex
= &dev
->ex_dev
;
1037 int i
= 0, end
= ex
->num_phys
;
1040 if (0 <= single
&& single
< end
) {
1045 for ( ; i
< end
; i
++) {
1046 struct ex_phy
*ex_phy
= &ex
->ex_phy
[i
];
1048 if (ex_phy
->phy_state
== PHY_VACANT
||
1049 ex_phy
->phy_state
== PHY_NOT_PRESENT
||
1050 ex_phy
->phy_state
== PHY_DEVICE_DISCOVERED
)
1053 switch (ex_phy
->linkrate
) {
1054 case SAS_PHY_DISABLED
:
1055 case SAS_PHY_RESET_PROBLEM
:
1056 case SAS_SATA_PORT_SELECTOR
:
1059 res
= sas_ex_discover_dev(dev
, i
);
1067 sas_check_level_subtractive_boundary(dev
);
1072 static int sas_check_ex_subtractive_boundary(struct domain_device
*dev
)
1074 struct expander_device
*ex
= &dev
->ex_dev
;
1076 u8
*sub_sas_addr
= NULL
;
1078 if (dev
->dev_type
!= EDGE_DEV
)
1081 for (i
= 0; i
< ex
->num_phys
; i
++) {
1082 struct ex_phy
*phy
= &ex
->ex_phy
[i
];
1084 if (phy
->phy_state
== PHY_VACANT
||
1085 phy
->phy_state
== PHY_NOT_PRESENT
)
1088 if ((phy
->attached_dev_type
== FANOUT_DEV
||
1089 phy
->attached_dev_type
== EDGE_DEV
) &&
1090 phy
->routing_attr
== SUBTRACTIVE_ROUTING
) {
1093 sub_sas_addr
= &phy
->attached_sas_addr
[0];
1094 else if (SAS_ADDR(sub_sas_addr
) !=
1095 SAS_ADDR(phy
->attached_sas_addr
)) {
1097 SAS_DPRINTK("ex %016llx phy 0x%x "
1098 "diverges(%016llx) on subtractive "
1099 "boundary(%016llx). Disabled\n",
1100 SAS_ADDR(dev
->sas_addr
), i
,
1101 SAS_ADDR(phy
->attached_sas_addr
),
1102 SAS_ADDR(sub_sas_addr
));
1103 sas_ex_disable_phy(dev
, i
);
1110 static void sas_print_parent_topology_bug(struct domain_device
*child
,
1111 struct ex_phy
*parent_phy
,
1112 struct ex_phy
*child_phy
)
1114 static const char ra_char
[] = {
1115 [DIRECT_ROUTING
] = 'D',
1116 [SUBTRACTIVE_ROUTING
] = 'S',
1117 [TABLE_ROUTING
] = 'T',
1119 static const char *ex_type
[] = {
1120 [EDGE_DEV
] = "edge",
1121 [FANOUT_DEV
] = "fanout",
1123 struct domain_device
*parent
= child
->parent
;
1125 sas_printk("%s ex %016llx phy 0x%x <--> %s ex %016llx phy 0x%x "
1126 "has %c:%c routing link!\n",
1128 ex_type
[parent
->dev_type
],
1129 SAS_ADDR(parent
->sas_addr
),
1132 ex_type
[child
->dev_type
],
1133 SAS_ADDR(child
->sas_addr
),
1136 ra_char
[parent_phy
->routing_attr
],
1137 ra_char
[child_phy
->routing_attr
]);
1140 static int sas_check_eeds(struct domain_device
*child
,
1141 struct ex_phy
*parent_phy
,
1142 struct ex_phy
*child_phy
)
1145 struct domain_device
*parent
= child
->parent
;
1147 if (SAS_ADDR(parent
->port
->disc
.fanout_sas_addr
) != 0) {
1149 SAS_DPRINTK("edge ex %016llx phy S:0x%x <--> edge ex %016llx "
1150 "phy S:0x%x, while there is a fanout ex %016llx\n",
1151 SAS_ADDR(parent
->sas_addr
),
1153 SAS_ADDR(child
->sas_addr
),
1155 SAS_ADDR(parent
->port
->disc
.fanout_sas_addr
));
1156 } else if (SAS_ADDR(parent
->port
->disc
.eeds_a
) == 0) {
1157 memcpy(parent
->port
->disc
.eeds_a
, parent
->sas_addr
,
1159 memcpy(parent
->port
->disc
.eeds_b
, child
->sas_addr
,
1161 } else if (((SAS_ADDR(parent
->port
->disc
.eeds_a
) ==
1162 SAS_ADDR(parent
->sas_addr
)) ||
1163 (SAS_ADDR(parent
->port
->disc
.eeds_a
) ==
1164 SAS_ADDR(child
->sas_addr
)))
1166 ((SAS_ADDR(parent
->port
->disc
.eeds_b
) ==
1167 SAS_ADDR(parent
->sas_addr
)) ||
1168 (SAS_ADDR(parent
->port
->disc
.eeds_b
) ==
1169 SAS_ADDR(child
->sas_addr
))))
1173 SAS_DPRINTK("edge ex %016llx phy 0x%x <--> edge ex %016llx "
1174 "phy 0x%x link forms a third EEDS!\n",
1175 SAS_ADDR(parent
->sas_addr
),
1177 SAS_ADDR(child
->sas_addr
),
1184 /* Here we spill over 80 columns. It is intentional.
1186 static int sas_check_parent_topology(struct domain_device
*child
)
1188 struct expander_device
*child_ex
= &child
->ex_dev
;
1189 struct expander_device
*parent_ex
;
1196 if (child
->parent
->dev_type
!= EDGE_DEV
&&
1197 child
->parent
->dev_type
!= FANOUT_DEV
)
1200 parent_ex
= &child
->parent
->ex_dev
;
1202 for (i
= 0; i
< parent_ex
->num_phys
; i
++) {
1203 struct ex_phy
*parent_phy
= &parent_ex
->ex_phy
[i
];
1204 struct ex_phy
*child_phy
;
1206 if (parent_phy
->phy_state
== PHY_VACANT
||
1207 parent_phy
->phy_state
== PHY_NOT_PRESENT
)
1210 if (SAS_ADDR(parent_phy
->attached_sas_addr
) != SAS_ADDR(child
->sas_addr
))
1213 child_phy
= &child_ex
->ex_phy
[parent_phy
->attached_phy_id
];
1215 switch (child
->parent
->dev_type
) {
1217 if (child
->dev_type
== FANOUT_DEV
) {
1218 if (parent_phy
->routing_attr
!= SUBTRACTIVE_ROUTING
||
1219 child_phy
->routing_attr
!= TABLE_ROUTING
) {
1220 sas_print_parent_topology_bug(child
, parent_phy
, child_phy
);
1223 } else if (parent_phy
->routing_attr
== SUBTRACTIVE_ROUTING
) {
1224 if (child_phy
->routing_attr
== SUBTRACTIVE_ROUTING
) {
1225 res
= sas_check_eeds(child
, parent_phy
, child_phy
);
1226 } else if (child_phy
->routing_attr
!= TABLE_ROUTING
) {
1227 sas_print_parent_topology_bug(child
, parent_phy
, child_phy
);
1230 } else if (parent_phy
->routing_attr
== TABLE_ROUTING
&&
1231 child_phy
->routing_attr
!= SUBTRACTIVE_ROUTING
) {
1232 sas_print_parent_topology_bug(child
, parent_phy
, child_phy
);
1237 if (parent_phy
->routing_attr
!= TABLE_ROUTING
||
1238 child_phy
->routing_attr
!= SUBTRACTIVE_ROUTING
) {
1239 sas_print_parent_topology_bug(child
, parent_phy
, child_phy
);
1251 #define RRI_REQ_SIZE 16
1252 #define RRI_RESP_SIZE 44
1254 static int sas_configure_present(struct domain_device
*dev
, int phy_id
,
1255 u8
*sas_addr
, int *index
, int *present
)
1258 struct expander_device
*ex
= &dev
->ex_dev
;
1259 struct ex_phy
*phy
= &ex
->ex_phy
[phy_id
];
1266 rri_req
= alloc_smp_req(RRI_REQ_SIZE
);
1270 rri_resp
= alloc_smp_resp(RRI_RESP_SIZE
);
1276 rri_req
[1] = SMP_REPORT_ROUTE_INFO
;
1277 rri_req
[9] = phy_id
;
1279 for (i
= 0; i
< ex
->max_route_indexes
; i
++) {
1280 *(__be16
*)(rri_req
+6) = cpu_to_be16(i
);
1281 res
= smp_execute_task(dev
, rri_req
, RRI_REQ_SIZE
, rri_resp
,
1286 if (res
== SMP_RESP_NO_INDEX
) {
1287 SAS_DPRINTK("overflow of indexes: dev %016llx "
1288 "phy 0x%x index 0x%x\n",
1289 SAS_ADDR(dev
->sas_addr
), phy_id
, i
);
1291 } else if (res
!= SMP_RESP_FUNC_ACC
) {
1292 SAS_DPRINTK("%s: dev %016llx phy 0x%x index 0x%x "
1293 "result 0x%x\n", __func__
,
1294 SAS_ADDR(dev
->sas_addr
), phy_id
, i
, res
);
1297 if (SAS_ADDR(sas_addr
) != 0) {
1298 if (SAS_ADDR(rri_resp
+16) == SAS_ADDR(sas_addr
)) {
1300 if ((rri_resp
[12] & 0x80) == 0x80)
1305 } else if (SAS_ADDR(rri_resp
+16) == 0) {
1310 } else if (SAS_ADDR(rri_resp
+16) == 0 &&
1311 phy
->last_da_index
< i
) {
1312 phy
->last_da_index
= i
;
1325 #define CRI_REQ_SIZE 44
1326 #define CRI_RESP_SIZE 8
1328 static int sas_configure_set(struct domain_device
*dev
, int phy_id
,
1329 u8
*sas_addr
, int index
, int include
)
1335 cri_req
= alloc_smp_req(CRI_REQ_SIZE
);
1339 cri_resp
= alloc_smp_resp(CRI_RESP_SIZE
);
1345 cri_req
[1] = SMP_CONF_ROUTE_INFO
;
1346 *(__be16
*)(cri_req
+6) = cpu_to_be16(index
);
1347 cri_req
[9] = phy_id
;
1348 if (SAS_ADDR(sas_addr
) == 0 || !include
)
1349 cri_req
[12] |= 0x80;
1350 memcpy(cri_req
+16, sas_addr
, SAS_ADDR_SIZE
);
1352 res
= smp_execute_task(dev
, cri_req
, CRI_REQ_SIZE
, cri_resp
,
1357 if (res
== SMP_RESP_NO_INDEX
) {
1358 SAS_DPRINTK("overflow of indexes: dev %016llx phy 0x%x "
1360 SAS_ADDR(dev
->sas_addr
), phy_id
, index
);
1368 static int sas_configure_phy(struct domain_device
*dev
, int phy_id
,
1369 u8
*sas_addr
, int include
)
1375 res
= sas_configure_present(dev
, phy_id
, sas_addr
, &index
, &present
);
1378 if (include
^ present
)
1379 return sas_configure_set(dev
, phy_id
, sas_addr
, index
,include
);
1385 * sas_configure_parent -- configure routing table of parent
1386 * parent: parent expander
1387 * child: child expander
1388 * sas_addr: SAS port identifier of device directly attached to child
1390 static int sas_configure_parent(struct domain_device
*parent
,
1391 struct domain_device
*child
,
1392 u8
*sas_addr
, int include
)
1394 struct expander_device
*ex_parent
= &parent
->ex_dev
;
1398 if (parent
->parent
) {
1399 res
= sas_configure_parent(parent
->parent
, parent
, sas_addr
,
1405 if (ex_parent
->conf_route_table
== 0) {
1406 SAS_DPRINTK("ex %016llx has self-configuring routing table\n",
1407 SAS_ADDR(parent
->sas_addr
));
1411 for (i
= 0; i
< ex_parent
->num_phys
; i
++) {
1412 struct ex_phy
*phy
= &ex_parent
->ex_phy
[i
];
1414 if ((phy
->routing_attr
== TABLE_ROUTING
) &&
1415 (SAS_ADDR(phy
->attached_sas_addr
) ==
1416 SAS_ADDR(child
->sas_addr
))) {
1417 res
= sas_configure_phy(parent
, i
, sas_addr
, include
);
1427 * sas_configure_routing -- configure routing
1428 * dev: expander device
1429 * sas_addr: port identifier of device directly attached to the expander device
1431 static int sas_configure_routing(struct domain_device
*dev
, u8
*sas_addr
)
1434 return sas_configure_parent(dev
->parent
, dev
, sas_addr
, 1);
1438 static int sas_disable_routing(struct domain_device
*dev
, u8
*sas_addr
)
1441 return sas_configure_parent(dev
->parent
, dev
, sas_addr
, 0);
1446 * sas_discover_expander -- expander discovery
1447 * @ex: pointer to expander domain device
1449 * See comment in sas_discover_sata().
1451 static int sas_discover_expander(struct domain_device
*dev
)
1455 res
= sas_notify_lldd_dev_found(dev
);
1459 res
= sas_ex_general(dev
);
1462 res
= sas_ex_manuf_info(dev
);
1466 res
= sas_expander_discover(dev
);
1468 SAS_DPRINTK("expander %016llx discovery failed(0x%x)\n",
1469 SAS_ADDR(dev
->sas_addr
), res
);
1473 sas_check_ex_subtractive_boundary(dev
);
1474 res
= sas_check_parent_topology(dev
);
1479 sas_notify_lldd_dev_gone(dev
);
1483 static int sas_ex_level_discovery(struct asd_sas_port
*port
, const int level
)
1486 struct domain_device
*dev
;
1488 list_for_each_entry(dev
, &port
->dev_list
, dev_list_node
) {
1489 if (dev
->dev_type
== EDGE_DEV
||
1490 dev
->dev_type
== FANOUT_DEV
) {
1491 struct sas_expander_device
*ex
=
1492 rphy_to_expander_device(dev
->rphy
);
1494 if (level
== ex
->level
)
1495 res
= sas_ex_discover_devices(dev
, -1);
1497 res
= sas_ex_discover_devices(port
->port_dev
, -1);
1505 static int sas_ex_bfs_disc(struct asd_sas_port
*port
)
1511 level
= port
->disc
.max_level
;
1512 res
= sas_ex_level_discovery(port
, level
);
1514 } while (level
< port
->disc
.max_level
);
1519 int sas_discover_root_expander(struct domain_device
*dev
)
1522 struct sas_expander_device
*ex
= rphy_to_expander_device(dev
->rphy
);
1524 res
= sas_rphy_add(dev
->rphy
);
1528 ex
->level
= dev
->port
->disc
.max_level
; /* 0 */
1529 res
= sas_discover_expander(dev
);
1533 sas_ex_bfs_disc(dev
->port
);
1538 sas_rphy_remove(dev
->rphy
);
1543 /* ---------- Domain revalidation ---------- */
1545 static int sas_get_phy_discover(struct domain_device
*dev
,
1546 int phy_id
, struct smp_resp
*disc_resp
)
1551 disc_req
= alloc_smp_req(DISCOVER_REQ_SIZE
);
1555 disc_req
[1] = SMP_DISCOVER
;
1556 disc_req
[9] = phy_id
;
1558 res
= smp_execute_task(dev
, disc_req
, DISCOVER_REQ_SIZE
,
1559 disc_resp
, DISCOVER_RESP_SIZE
);
1562 else if (disc_resp
->result
!= SMP_RESP_FUNC_ACC
) {
1563 res
= disc_resp
->result
;
1571 static int sas_get_phy_change_count(struct domain_device
*dev
,
1572 int phy_id
, int *pcc
)
1575 struct smp_resp
*disc_resp
;
1577 disc_resp
= alloc_smp_resp(DISCOVER_RESP_SIZE
);
1581 res
= sas_get_phy_discover(dev
, phy_id
, disc_resp
);
1583 *pcc
= disc_resp
->disc
.change_count
;
1589 static int sas_get_phy_attached_sas_addr(struct domain_device
*dev
,
1590 int phy_id
, u8
*attached_sas_addr
)
1593 struct smp_resp
*disc_resp
;
1594 struct discover_resp
*dr
;
1596 disc_resp
= alloc_smp_resp(DISCOVER_RESP_SIZE
);
1599 dr
= &disc_resp
->disc
;
1601 res
= sas_get_phy_discover(dev
, phy_id
, disc_resp
);
1603 memcpy(attached_sas_addr
,disc_resp
->disc
.attached_sas_addr
,8);
1604 if (dr
->attached_dev_type
== 0)
1605 memset(attached_sas_addr
, 0, 8);
1611 static int sas_find_bcast_phy(struct domain_device
*dev
, int *phy_id
,
1612 int from_phy
, bool update
)
1614 struct expander_device
*ex
= &dev
->ex_dev
;
1618 for (i
= from_phy
; i
< ex
->num_phys
; i
++) {
1619 int phy_change_count
= 0;
1621 res
= sas_get_phy_change_count(dev
, i
, &phy_change_count
);
1624 else if (phy_change_count
!= ex
->ex_phy
[i
].phy_change_count
) {
1626 ex
->ex_phy
[i
].phy_change_count
=
1636 static int sas_get_ex_change_count(struct domain_device
*dev
, int *ecc
)
1640 struct smp_resp
*rg_resp
;
1642 rg_req
= alloc_smp_req(RG_REQ_SIZE
);
1646 rg_resp
= alloc_smp_resp(RG_RESP_SIZE
);
1652 rg_req
[1] = SMP_REPORT_GENERAL
;
1654 res
= smp_execute_task(dev
, rg_req
, RG_REQ_SIZE
, rg_resp
,
1658 if (rg_resp
->result
!= SMP_RESP_FUNC_ACC
) {
1659 res
= rg_resp
->result
;
1663 *ecc
= be16_to_cpu(rg_resp
->rg
.change_count
);
1670 * sas_find_bcast_dev - find the device issue BROADCAST(CHANGE).
1671 * @dev:domain device to be detect.
1672 * @src_dev: the device which originated BROADCAST(CHANGE).
1674 * Add self-configuration expander suport. Suppose two expander cascading,
1675 * when the first level expander is self-configuring, hotplug the disks in
1676 * second level expander, BROADCAST(CHANGE) will not only be originated
1677 * in the second level expander, but also be originated in the first level
1678 * expander (see SAS protocol SAS 2r-14, 7.11 for detail), it is to say,
1679 * expander changed count in two level expanders will all increment at least
1680 * once, but the phy which chang count has changed is the source device which
1684 static int sas_find_bcast_dev(struct domain_device
*dev
,
1685 struct domain_device
**src_dev
)
1687 struct expander_device
*ex
= &dev
->ex_dev
;
1688 int ex_change_count
= -1;
1691 struct domain_device
*ch
;
1693 res
= sas_get_ex_change_count(dev
, &ex_change_count
);
1696 if (ex_change_count
!= -1 && ex_change_count
!= ex
->ex_change_count
) {
1697 /* Just detect if this expander phys phy change count changed,
1698 * in order to determine if this expander originate BROADCAST,
1699 * and do not update phy change count field in our structure.
1701 res
= sas_find_bcast_phy(dev
, &phy_id
, 0, false);
1704 ex
->ex_change_count
= ex_change_count
;
1705 SAS_DPRINTK("Expander phy change count has changed\n");
1708 SAS_DPRINTK("Expander phys DID NOT change\n");
1710 list_for_each_entry(ch
, &ex
->children
, siblings
) {
1711 if (ch
->dev_type
== EDGE_DEV
|| ch
->dev_type
== FANOUT_DEV
) {
1712 res
= sas_find_bcast_dev(ch
, src_dev
);
1721 static void sas_unregister_ex_tree(struct domain_device
*dev
)
1723 struct expander_device
*ex
= &dev
->ex_dev
;
1724 struct domain_device
*child
, *n
;
1726 list_for_each_entry_safe(child
, n
, &ex
->children
, siblings
) {
1727 if (child
->dev_type
== EDGE_DEV
||
1728 child
->dev_type
== FANOUT_DEV
)
1729 sas_unregister_ex_tree(child
);
1731 sas_unregister_dev(child
);
1733 sas_unregister_dev(dev
);
1736 static void sas_unregister_devs_sas_addr(struct domain_device
*parent
,
1737 int phy_id
, bool last
)
1739 struct expander_device
*ex_dev
= &parent
->ex_dev
;
1740 struct ex_phy
*phy
= &ex_dev
->ex_phy
[phy_id
];
1741 struct domain_device
*child
, *n
;
1743 list_for_each_entry_safe(child
, n
,
1744 &ex_dev
->children
, siblings
) {
1745 if (SAS_ADDR(child
->sas_addr
) ==
1746 SAS_ADDR(phy
->attached_sas_addr
)) {
1747 if (child
->dev_type
== EDGE_DEV
||
1748 child
->dev_type
== FANOUT_DEV
)
1749 sas_unregister_ex_tree(child
);
1751 sas_unregister_dev(child
);
1755 sas_disable_routing(parent
, phy
->attached_sas_addr
);
1757 memset(phy
->attached_sas_addr
, 0, SAS_ADDR_SIZE
);
1758 sas_port_delete_phy(phy
->port
, phy
->phy
);
1759 if (phy
->port
->num_phys
== 0)
1760 sas_port_delete(phy
->port
);
1764 static int sas_discover_bfs_by_root_level(struct domain_device
*root
,
1767 struct expander_device
*ex_root
= &root
->ex_dev
;
1768 struct domain_device
*child
;
1771 list_for_each_entry(child
, &ex_root
->children
, siblings
) {
1772 if (child
->dev_type
== EDGE_DEV
||
1773 child
->dev_type
== FANOUT_DEV
) {
1774 struct sas_expander_device
*ex
=
1775 rphy_to_expander_device(child
->rphy
);
1777 if (level
> ex
->level
)
1778 res
= sas_discover_bfs_by_root_level(child
,
1780 else if (level
== ex
->level
)
1781 res
= sas_ex_discover_devices(child
, -1);
1787 static int sas_discover_bfs_by_root(struct domain_device
*dev
)
1790 struct sas_expander_device
*ex
= rphy_to_expander_device(dev
->rphy
);
1791 int level
= ex
->level
+1;
1793 res
= sas_ex_discover_devices(dev
, -1);
1797 res
= sas_discover_bfs_by_root_level(dev
, level
);
1800 } while (level
<= dev
->port
->disc
.max_level
);
1805 static int sas_discover_new(struct domain_device
*dev
, int phy_id
)
1807 struct ex_phy
*ex_phy
= &dev
->ex_dev
.ex_phy
[phy_id
];
1808 struct domain_device
*child
;
1812 SAS_DPRINTK("ex %016llx phy%d new device attached\n",
1813 SAS_ADDR(dev
->sas_addr
), phy_id
);
1814 res
= sas_ex_phy_discover(dev
, phy_id
);
1817 /* to support the wide port inserted */
1818 for (i
= 0; i
< dev
->ex_dev
.num_phys
; i
++) {
1819 struct ex_phy
*ex_phy_temp
= &dev
->ex_dev
.ex_phy
[i
];
1822 if (SAS_ADDR(ex_phy_temp
->attached_sas_addr
) ==
1823 SAS_ADDR(ex_phy
->attached_sas_addr
)) {
1829 sas_ex_join_wide_port(dev
, phy_id
);
1832 res
= sas_ex_discover_devices(dev
, phy_id
);
1835 list_for_each_entry(child
, &dev
->ex_dev
.children
, siblings
) {
1836 if (SAS_ADDR(child
->sas_addr
) ==
1837 SAS_ADDR(ex_phy
->attached_sas_addr
)) {
1838 if (child
->dev_type
== EDGE_DEV
||
1839 child
->dev_type
== FANOUT_DEV
)
1840 res
= sas_discover_bfs_by_root(child
);
1848 static int sas_rediscover_dev(struct domain_device
*dev
, int phy_id
, bool last
)
1850 struct expander_device
*ex
= &dev
->ex_dev
;
1851 struct ex_phy
*phy
= &ex
->ex_phy
[phy_id
];
1852 u8 attached_sas_addr
[8];
1855 res
= sas_get_phy_attached_sas_addr(dev
, phy_id
, attached_sas_addr
);
1857 case SMP_RESP_NO_PHY
:
1858 phy
->phy_state
= PHY_NOT_PRESENT
;
1859 sas_unregister_devs_sas_addr(dev
, phy_id
, last
);
1861 case SMP_RESP_PHY_VACANT
:
1862 phy
->phy_state
= PHY_VACANT
;
1863 sas_unregister_devs_sas_addr(dev
, phy_id
, last
);
1865 case SMP_RESP_FUNC_ACC
:
1869 if (SAS_ADDR(attached_sas_addr
) == 0) {
1870 phy
->phy_state
= PHY_EMPTY
;
1871 sas_unregister_devs_sas_addr(dev
, phy_id
, last
);
1872 } else if (SAS_ADDR(attached_sas_addr
) ==
1873 SAS_ADDR(phy
->attached_sas_addr
)) {
1874 SAS_DPRINTK("ex %016llx phy 0x%x broadcast flutter\n",
1875 SAS_ADDR(dev
->sas_addr
), phy_id
);
1876 sas_ex_phy_discover(dev
, phy_id
);
1878 res
= sas_discover_new(dev
, phy_id
);
1884 * sas_rediscover - revalidate the domain.
1885 * @dev:domain device to be detect.
1886 * @phy_id: the phy id will be detected.
1888 * NOTE: this process _must_ quit (return) as soon as any connection
1889 * errors are encountered. Connection recovery is done elsewhere.
1890 * Discover process only interrogates devices in order to discover the
1891 * domain.For plugging out, we un-register the device only when it is
1892 * the last phy in the port, for other phys in this port, we just delete it
1893 * from the port.For inserting, we do discovery when it is the
1894 * first phy,for other phys in this port, we add it to the port to
1895 * forming the wide-port.
1897 static int sas_rediscover(struct domain_device
*dev
, const int phy_id
)
1899 struct expander_device
*ex
= &dev
->ex_dev
;
1900 struct ex_phy
*changed_phy
= &ex
->ex_phy
[phy_id
];
1903 bool last
= true; /* is this the last phy of the port */
1905 SAS_DPRINTK("ex %016llx phy%d originated BROADCAST(CHANGE)\n",
1906 SAS_ADDR(dev
->sas_addr
), phy_id
);
1908 if (SAS_ADDR(changed_phy
->attached_sas_addr
) != 0) {
1909 for (i
= 0; i
< ex
->num_phys
; i
++) {
1910 struct ex_phy
*phy
= &ex
->ex_phy
[i
];
1914 if (SAS_ADDR(phy
->attached_sas_addr
) ==
1915 SAS_ADDR(changed_phy
->attached_sas_addr
)) {
1916 SAS_DPRINTK("phy%d part of wide port with "
1917 "phy%d\n", phy_id
, i
);
1922 res
= sas_rediscover_dev(dev
, phy_id
, last
);
1924 res
= sas_discover_new(dev
, phy_id
);
1929 * sas_revalidate_domain -- revalidate the domain
1930 * @port: port to the domain of interest
1932 * NOTE: this process _must_ quit (return) as soon as any connection
1933 * errors are encountered. Connection recovery is done elsewhere.
1934 * Discover process only interrogates devices in order to discover the
1937 int sas_ex_revalidate_domain(struct domain_device
*port_dev
)
1940 struct domain_device
*dev
= NULL
;
1942 res
= sas_find_bcast_dev(port_dev
, &dev
);
1946 struct expander_device
*ex
= &dev
->ex_dev
;
1951 res
= sas_find_bcast_phy(dev
, &phy_id
, i
, true);
1954 res
= sas_rediscover(dev
, phy_id
);
1956 } while (i
< ex
->num_phys
);
1962 int sas_smp_handler(struct Scsi_Host
*shost
, struct sas_rphy
*rphy
,
1963 struct request
*req
)
1965 struct domain_device
*dev
;
1967 struct request
*rsp
= req
->next_rq
;
1970 printk("%s: space for a smp response is missing\n",
1975 /* no rphy means no smp target support (ie aic94xx host) */
1977 return sas_smp_host_handler(shost
, req
, rsp
);
1979 type
= rphy
->identify
.device_type
;
1981 if (type
!= SAS_EDGE_EXPANDER_DEVICE
&&
1982 type
!= SAS_FANOUT_EXPANDER_DEVICE
) {
1983 printk("%s: can we send a smp request to a device?\n",
1988 dev
= sas_find_dev_by_rphy(rphy
);
1990 printk("%s: fail to find a domain_device?\n", __func__
);
1994 /* do we need to support multiple segments? */
1995 if (req
->bio
->bi_vcnt
> 1 || rsp
->bio
->bi_vcnt
> 1) {
1996 printk("%s: multiple segments req %u %u, rsp %u %u\n",
1997 __func__
, req
->bio
->bi_vcnt
, blk_rq_bytes(req
),
1998 rsp
->bio
->bi_vcnt
, blk_rq_bytes(rsp
));
2002 ret
= smp_execute_task(dev
, bio_data(req
->bio
), blk_rq_bytes(req
),
2003 bio_data(rsp
->bio
), blk_rq_bytes(rsp
));
2005 /* positive number is the untransferred residual */
2006 rsp
->resid_len
= ret
;
2009 } else if (ret
== 0) {