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>
27 #include "sas_internal.h"
29 #include <scsi/scsi_transport.h>
30 #include <scsi/scsi_transport_sas.h>
31 #include "../scsi_sas_internal.h"
33 static int sas_discover_expander(struct domain_device
*dev
);
34 static int sas_configure_routing(struct domain_device
*dev
, u8
*sas_addr
);
35 static int sas_configure_phy(struct domain_device
*dev
, int phy_id
,
36 u8
*sas_addr
, int include
);
37 static int sas_disable_routing(struct domain_device
*dev
, u8
*sas_addr
);
40 /* FIXME: smp needs to migrate into the sas class */
41 static ssize_t
smp_portal_read(struct kobject
*, char *, loff_t
, size_t);
42 static ssize_t
smp_portal_write(struct kobject
*, char *, loff_t
, size_t);
45 /* ---------- SMP task management ---------- */
47 static void smp_task_timedout(unsigned long _task
)
49 struct sas_task
*task
= (void *) _task
;
52 spin_lock_irqsave(&task
->task_state_lock
, flags
);
53 if (!(task
->task_state_flags
& SAS_TASK_STATE_DONE
))
54 task
->task_state_flags
|= SAS_TASK_STATE_ABORTED
;
55 spin_unlock_irqrestore(&task
->task_state_lock
, flags
);
57 complete(&task
->completion
);
60 static void smp_task_done(struct sas_task
*task
)
62 if (!del_timer(&task
->timer
))
64 complete(&task
->completion
);
67 /* Give it some long enough timeout. In seconds. */
68 #define SMP_TIMEOUT 10
70 static int smp_execute_task(struct domain_device
*dev
, void *req
, int req_size
,
71 void *resp
, int resp_size
)
74 struct sas_task
*task
= NULL
;
75 struct sas_internal
*i
=
76 to_sas_internal(dev
->port
->ha
->core
.shost
->transportt
);
78 for (retry
= 0; retry
< 3; retry
++) {
79 task
= sas_alloc_task(GFP_KERNEL
);
84 task
->task_proto
= dev
->tproto
;
85 sg_init_one(&task
->smp_task
.smp_req
, req
, req_size
);
86 sg_init_one(&task
->smp_task
.smp_resp
, resp
, resp_size
);
88 task
->task_done
= smp_task_done
;
90 task
->timer
.data
= (unsigned long) task
;
91 task
->timer
.function
= smp_task_timedout
;
92 task
->timer
.expires
= jiffies
+ SMP_TIMEOUT
*HZ
;
93 add_timer(&task
->timer
);
95 res
= i
->dft
->lldd_execute_task(task
, 1, GFP_KERNEL
);
98 del_timer(&task
->timer
);
99 SAS_DPRINTK("executing SMP task failed:%d\n", res
);
103 wait_for_completion(&task
->completion
);
105 if ((task
->task_state_flags
& SAS_TASK_STATE_ABORTED
)) {
106 SAS_DPRINTK("smp task timed out or aborted\n");
107 i
->dft
->lldd_abort_task(task
);
108 if (!(task
->task_state_flags
& SAS_TASK_STATE_DONE
)) {
109 SAS_DPRINTK("SMP task aborted and not done\n");
113 if (task
->task_status
.resp
== SAS_TASK_COMPLETE
&&
114 task
->task_status
.stat
== SAM_GOOD
) {
118 SAS_DPRINTK("%s: task to dev %016llx response: 0x%x "
119 "status 0x%x\n", __FUNCTION__
,
120 SAS_ADDR(dev
->sas_addr
),
121 task
->task_status
.resp
,
122 task
->task_status
.stat
);
128 BUG_ON(retry
== 3 && task
!= NULL
);
135 /* ---------- Allocations ---------- */
137 static inline void *alloc_smp_req(int size
)
139 u8
*p
= kzalloc(size
, GFP_KERNEL
);
145 static inline void *alloc_smp_resp(int size
)
147 return kzalloc(size
, GFP_KERNEL
);
150 /* ---------- Expander configuration ---------- */
152 static void sas_set_ex_phy(struct domain_device
*dev
, int phy_id
,
155 struct expander_device
*ex
= &dev
->ex_dev
;
156 struct ex_phy
*phy
= &ex
->ex_phy
[phy_id
];
157 struct smp_resp
*resp
= disc_resp
;
158 struct discover_resp
*dr
= &resp
->disc
;
159 struct sas_rphy
*rphy
= dev
->rphy
;
160 int rediscover
= (phy
->phy
!= NULL
);
163 phy
->phy
= sas_phy_alloc(&rphy
->dev
, phy_id
);
165 /* FIXME: error_handling */
169 switch (resp
->result
) {
170 case SMP_RESP_PHY_VACANT
:
171 phy
->phy_state
= PHY_VACANT
;
174 phy
->phy_state
= PHY_NOT_PRESENT
;
176 case SMP_RESP_FUNC_ACC
:
177 phy
->phy_state
= PHY_EMPTY
; /* do not know yet */
181 phy
->phy_id
= phy_id
;
182 phy
->attached_dev_type
= dr
->attached_dev_type
;
183 phy
->linkrate
= dr
->linkrate
;
184 phy
->attached_sata_host
= dr
->attached_sata_host
;
185 phy
->attached_sata_dev
= dr
->attached_sata_dev
;
186 phy
->attached_sata_ps
= dr
->attached_sata_ps
;
187 phy
->attached_iproto
= dr
->iproto
<< 1;
188 phy
->attached_tproto
= dr
->tproto
<< 1;
189 memcpy(phy
->attached_sas_addr
, dr
->attached_sas_addr
, SAS_ADDR_SIZE
);
190 phy
->attached_phy_id
= dr
->attached_phy_id
;
191 phy
->phy_change_count
= dr
->change_count
;
192 phy
->routing_attr
= dr
->routing_attr
;
193 phy
->virtual = dr
->virtual;
194 phy
->last_da_index
= -1;
196 phy
->phy
->identify
.initiator_port_protocols
= phy
->attached_iproto
;
197 phy
->phy
->identify
.target_port_protocols
= phy
->attached_tproto
;
198 phy
->phy
->identify
.phy_identifier
= phy_id
;
199 phy
->phy
->minimum_linkrate_hw
= dr
->hmin_linkrate
;
200 phy
->phy
->maximum_linkrate_hw
= dr
->hmax_linkrate
;
201 phy
->phy
->minimum_linkrate
= dr
->pmin_linkrate
;
202 phy
->phy
->maximum_linkrate
= dr
->pmax_linkrate
;
203 phy
->phy
->negotiated_linkrate
= phy
->linkrate
;
206 sas_phy_add(phy
->phy
);
208 SAS_DPRINTK("ex %016llx phy%02d:%c attached: %016llx\n",
209 SAS_ADDR(dev
->sas_addr
), phy
->phy_id
,
210 phy
->routing_attr
== TABLE_ROUTING
? 'T' :
211 phy
->routing_attr
== DIRECT_ROUTING
? 'D' :
212 phy
->routing_attr
== SUBTRACTIVE_ROUTING
? 'S' : '?',
213 SAS_ADDR(phy
->attached_sas_addr
));
218 #define DISCOVER_REQ_SIZE 16
219 #define DISCOVER_RESP_SIZE 56
221 static int sas_ex_phy_discover(struct domain_device
*dev
, int single
)
223 struct expander_device
*ex
= &dev
->ex_dev
;
228 disc_req
= alloc_smp_req(DISCOVER_REQ_SIZE
);
232 disc_resp
= alloc_smp_req(DISCOVER_RESP_SIZE
);
238 disc_req
[1] = SMP_DISCOVER
;
240 if (0 <= single
&& single
< ex
->num_phys
) {
241 disc_req
[9] = single
;
242 res
= smp_execute_task(dev
, disc_req
, DISCOVER_REQ_SIZE
,
243 disc_resp
, DISCOVER_RESP_SIZE
);
246 sas_set_ex_phy(dev
, single
, disc_resp
);
250 for (i
= 0; i
< ex
->num_phys
; i
++) {
252 res
= smp_execute_task(dev
, disc_req
,
253 DISCOVER_REQ_SIZE
, disc_resp
,
257 sas_set_ex_phy(dev
, i
, disc_resp
);
266 static int sas_expander_discover(struct domain_device
*dev
)
268 struct expander_device
*ex
= &dev
->ex_dev
;
271 ex
->ex_phy
= kzalloc(sizeof(*ex
->ex_phy
)*ex
->num_phys
, GFP_KERNEL
);
275 res
= sas_ex_phy_discover(dev
, -1);
286 #define MAX_EXPANDER_PHYS 128
288 static void ex_assign_report_general(struct domain_device
*dev
,
289 struct smp_resp
*resp
)
291 struct report_general_resp
*rg
= &resp
->rg
;
293 dev
->ex_dev
.ex_change_count
= be16_to_cpu(rg
->change_count
);
294 dev
->ex_dev
.max_route_indexes
= be16_to_cpu(rg
->route_indexes
);
295 dev
->ex_dev
.num_phys
= min(rg
->num_phys
, (u8
)MAX_EXPANDER_PHYS
);
296 dev
->ex_dev
.conf_route_table
= rg
->conf_route_table
;
297 dev
->ex_dev
.configuring
= rg
->configuring
;
298 memcpy(dev
->ex_dev
.enclosure_logical_id
, rg
->enclosure_logical_id
, 8);
301 #define RG_REQ_SIZE 8
302 #define RG_RESP_SIZE 32
304 static int sas_ex_general(struct domain_device
*dev
)
307 struct smp_resp
*rg_resp
;
311 rg_req
= alloc_smp_req(RG_REQ_SIZE
);
315 rg_resp
= alloc_smp_resp(RG_RESP_SIZE
);
321 rg_req
[1] = SMP_REPORT_GENERAL
;
323 for (i
= 0; i
< 5; i
++) {
324 res
= smp_execute_task(dev
, rg_req
, RG_REQ_SIZE
, rg_resp
,
328 SAS_DPRINTK("RG to ex %016llx failed:0x%x\n",
329 SAS_ADDR(dev
->sas_addr
), res
);
331 } else if (rg_resp
->result
!= SMP_RESP_FUNC_ACC
) {
332 SAS_DPRINTK("RG:ex %016llx returned SMP result:0x%x\n",
333 SAS_ADDR(dev
->sas_addr
), rg_resp
->result
);
334 res
= rg_resp
->result
;
338 ex_assign_report_general(dev
, rg_resp
);
340 if (dev
->ex_dev
.configuring
) {
341 SAS_DPRINTK("RG: ex %llx self-configuring...\n",
342 SAS_ADDR(dev
->sas_addr
));
343 schedule_timeout_interruptible(5*HZ
);
353 static void ex_assign_manuf_info(struct domain_device
*dev
, void
356 u8
*mi_resp
= _mi_resp
;
357 struct sas_rphy
*rphy
= dev
->rphy
;
358 struct sas_expander_device
*edev
= rphy_to_expander_device(rphy
);
360 memcpy(edev
->vendor_id
, mi_resp
+ 12, SAS_EXPANDER_VENDOR_ID_LEN
);
361 memcpy(edev
->product_id
, mi_resp
+ 20, SAS_EXPANDER_PRODUCT_ID_LEN
);
362 memcpy(edev
->product_rev
, mi_resp
+ 36,
363 SAS_EXPANDER_PRODUCT_REV_LEN
);
365 if (mi_resp
[8] & 1) {
366 memcpy(edev
->component_vendor_id
, mi_resp
+ 40,
367 SAS_EXPANDER_COMPONENT_VENDOR_ID_LEN
);
368 edev
->component_id
= mi_resp
[48] << 8 | mi_resp
[49];
369 edev
->component_revision_id
= mi_resp
[50];
373 #define MI_REQ_SIZE 8
374 #define MI_RESP_SIZE 64
376 static int sas_ex_manuf_info(struct domain_device
*dev
)
382 mi_req
= alloc_smp_req(MI_REQ_SIZE
);
386 mi_resp
= alloc_smp_resp(MI_RESP_SIZE
);
392 mi_req
[1] = SMP_REPORT_MANUF_INFO
;
394 res
= smp_execute_task(dev
, mi_req
, MI_REQ_SIZE
, mi_resp
,MI_RESP_SIZE
);
396 SAS_DPRINTK("MI: ex %016llx failed:0x%x\n",
397 SAS_ADDR(dev
->sas_addr
), res
);
399 } else if (mi_resp
[2] != SMP_RESP_FUNC_ACC
) {
400 SAS_DPRINTK("MI ex %016llx returned SMP result:0x%x\n",
401 SAS_ADDR(dev
->sas_addr
), mi_resp
[2]);
405 ex_assign_manuf_info(dev
, mi_resp
);
412 #define PC_REQ_SIZE 44
413 #define PC_RESP_SIZE 8
415 int sas_smp_phy_control(struct domain_device
*dev
, int phy_id
,
416 enum phy_func phy_func
,
417 struct sas_phy_linkrates
*rates
)
423 pc_req
= alloc_smp_req(PC_REQ_SIZE
);
427 pc_resp
= alloc_smp_resp(PC_RESP_SIZE
);
433 pc_req
[1] = SMP_PHY_CONTROL
;
435 pc_req
[10]= phy_func
;
437 pc_req
[32] = rates
->minimum_linkrate
<< 4;
438 pc_req
[33] = rates
->maximum_linkrate
<< 4;
441 res
= smp_execute_task(dev
, pc_req
, PC_REQ_SIZE
, pc_resp
,PC_RESP_SIZE
);
448 static void sas_ex_disable_phy(struct domain_device
*dev
, int phy_id
)
450 struct expander_device
*ex
= &dev
->ex_dev
;
451 struct ex_phy
*phy
= &ex
->ex_phy
[phy_id
];
453 sas_smp_phy_control(dev
, phy_id
, PHY_FUNC_DISABLE
, NULL
);
454 phy
->linkrate
= SAS_PHY_DISABLED
;
457 static void sas_ex_disable_port(struct domain_device
*dev
, u8
*sas_addr
)
459 struct expander_device
*ex
= &dev
->ex_dev
;
462 for (i
= 0; i
< ex
->num_phys
; i
++) {
463 struct ex_phy
*phy
= &ex
->ex_phy
[i
];
465 if (phy
->phy_state
== PHY_VACANT
||
466 phy
->phy_state
== PHY_NOT_PRESENT
)
469 if (SAS_ADDR(phy
->attached_sas_addr
) == SAS_ADDR(sas_addr
))
470 sas_ex_disable_phy(dev
, i
);
474 static int sas_dev_present_in_domain(struct asd_sas_port
*port
,
477 struct domain_device
*dev
;
479 if (SAS_ADDR(port
->sas_addr
) == SAS_ADDR(sas_addr
))
481 list_for_each_entry(dev
, &port
->dev_list
, dev_list_node
) {
482 if (SAS_ADDR(dev
->sas_addr
) == SAS_ADDR(sas_addr
))
488 #define RPEL_REQ_SIZE 16
489 #define RPEL_RESP_SIZE 32
490 int sas_smp_get_phy_events(struct sas_phy
*phy
)
493 struct sas_rphy
*rphy
= dev_to_rphy(phy
->dev
.parent
);
494 struct domain_device
*dev
= sas_find_dev_by_rphy(rphy
);
495 u8
*req
= alloc_smp_req(RPEL_REQ_SIZE
);
496 u8
*resp
= kzalloc(RPEL_RESP_SIZE
, GFP_KERNEL
);
501 req
[1] = SMP_REPORT_PHY_ERR_LOG
;
502 req
[9] = phy
->number
;
504 res
= smp_execute_task(dev
, req
, RPEL_REQ_SIZE
,
505 resp
, RPEL_RESP_SIZE
);
510 phy
->invalid_dword_count
= scsi_to_u32(&resp
[12]);
511 phy
->running_disparity_error_count
= scsi_to_u32(&resp
[16]);
512 phy
->loss_of_dword_sync_count
= scsi_to_u32(&resp
[20]);
513 phy
->phy_reset_problem_count
= scsi_to_u32(&resp
[24]);
521 #define RPS_REQ_SIZE 16
522 #define RPS_RESP_SIZE 60
524 static int sas_get_report_phy_sata(struct domain_device
*dev
,
526 struct smp_resp
*rps_resp
)
529 u8
*rps_req
= alloc_smp_req(RPS_REQ_SIZE
);
534 rps_req
[1] = SMP_REPORT_PHY_SATA
;
537 res
= smp_execute_task(dev
, rps_req
, RPS_REQ_SIZE
,
538 rps_resp
, RPS_RESP_SIZE
);
544 static void sas_ex_get_linkrate(struct domain_device
*parent
,
545 struct domain_device
*child
,
546 struct ex_phy
*parent_phy
)
548 struct expander_device
*parent_ex
= &parent
->ex_dev
;
549 struct sas_port
*port
;
554 port
= parent_phy
->port
;
556 for (i
= 0; i
< parent_ex
->num_phys
; i
++) {
557 struct ex_phy
*phy
= &parent_ex
->ex_phy
[i
];
559 if (phy
->phy_state
== PHY_VACANT
||
560 phy
->phy_state
== PHY_NOT_PRESENT
)
563 if (SAS_ADDR(phy
->attached_sas_addr
) ==
564 SAS_ADDR(child
->sas_addr
)) {
566 child
->min_linkrate
= min(parent
->min_linkrate
,
568 child
->max_linkrate
= max(parent
->max_linkrate
,
571 sas_port_add_phy(port
, phy
->phy
);
574 child
->linkrate
= min(parent_phy
->linkrate
, child
->max_linkrate
);
575 child
->pathways
= min(child
->pathways
, parent
->pathways
);
578 static struct domain_device
*sas_ex_discover_end_dev(
579 struct domain_device
*parent
, int phy_id
)
581 struct expander_device
*parent_ex
= &parent
->ex_dev
;
582 struct ex_phy
*phy
= &parent_ex
->ex_phy
[phy_id
];
583 struct domain_device
*child
= NULL
;
584 struct sas_rphy
*rphy
;
587 if (phy
->attached_sata_host
|| phy
->attached_sata_ps
)
590 child
= kzalloc(sizeof(*child
), GFP_KERNEL
);
594 child
->parent
= parent
;
595 child
->port
= parent
->port
;
596 child
->iproto
= phy
->attached_iproto
;
597 memcpy(child
->sas_addr
, phy
->attached_sas_addr
, SAS_ADDR_SIZE
);
598 sas_hash_addr(child
->hashed_sas_addr
, child
->sas_addr
);
600 phy
->port
= sas_port_alloc(&parent
->rphy
->dev
, phy_id
);
601 if (unlikely(!phy
->port
))
603 if (unlikely(sas_port_add(phy
->port
) != 0)) {
604 sas_port_free(phy
->port
);
608 sas_ex_get_linkrate(parent
, child
, phy
);
610 if ((phy
->attached_tproto
& SAS_PROTO_STP
) || phy
->attached_sata_dev
) {
611 child
->dev_type
= SATA_DEV
;
612 if (phy
->attached_tproto
& SAS_PROTO_STP
)
613 child
->tproto
= phy
->attached_tproto
;
614 if (phy
->attached_sata_dev
)
615 child
->tproto
|= SATA_DEV
;
616 res
= sas_get_report_phy_sata(parent
, phy_id
,
617 &child
->sata_dev
.rps_resp
);
619 SAS_DPRINTK("report phy sata to %016llx:0x%x returned "
620 "0x%x\n", SAS_ADDR(parent
->sas_addr
),
624 memcpy(child
->frame_rcvd
, &child
->sata_dev
.rps_resp
.rps
.fis
,
625 sizeof(struct dev_to_host_fis
));
627 res
= sas_discover_sata(child
);
629 SAS_DPRINTK("sas_discover_sata() for device %16llx at "
630 "%016llx:0x%x returned 0x%x\n",
631 SAS_ADDR(child
->sas_addr
),
632 SAS_ADDR(parent
->sas_addr
), phy_id
, res
);
635 } else if (phy
->attached_tproto
& SAS_PROTO_SSP
) {
636 child
->dev_type
= SAS_END_DEV
;
637 rphy
= sas_end_device_alloc(phy
->port
);
638 /* FIXME: error handling */
641 child
->tproto
= phy
->attached_tproto
;
645 sas_fill_in_rphy(child
, rphy
);
647 spin_lock(&parent
->port
->dev_list_lock
);
648 list_add_tail(&child
->dev_list_node
, &parent
->port
->dev_list
);
649 spin_unlock(&parent
->port
->dev_list_lock
);
651 res
= sas_discover_end_dev(child
);
653 SAS_DPRINTK("sas_discover_end_dev() for device %16llx "
654 "at %016llx:0x%x returned 0x%x\n",
655 SAS_ADDR(child
->sas_addr
),
656 SAS_ADDR(parent
->sas_addr
), phy_id
, res
);
660 SAS_DPRINTK("target proto 0x%x at %016llx:0x%x not handled\n",
661 phy
->attached_tproto
, SAS_ADDR(parent
->sas_addr
),
665 list_add_tail(&child
->siblings
, &parent_ex
->children
);
669 sas_rphy_free(child
->rphy
);
671 list_del(&child
->dev_list_node
);
673 sas_port_delete(phy
->port
);
680 /* See if this phy is part of a wide port */
681 static int sas_ex_join_wide_port(struct domain_device
*parent
, int phy_id
)
683 struct ex_phy
*phy
= &parent
->ex_dev
.ex_phy
[phy_id
];
686 for (i
= 0; i
< parent
->ex_dev
.num_phys
; i
++) {
687 struct ex_phy
*ephy
= &parent
->ex_dev
.ex_phy
[i
];
692 if (!memcmp(phy
->attached_sas_addr
, ephy
->attached_sas_addr
,
693 SAS_ADDR_SIZE
) && ephy
->port
) {
694 sas_port_add_phy(ephy
->port
, phy
->phy
);
695 phy
->phy_state
= PHY_DEVICE_DISCOVERED
;
703 static struct domain_device
*sas_ex_discover_expander(
704 struct domain_device
*parent
, int phy_id
)
706 struct sas_expander_device
*parent_ex
= rphy_to_expander_device(parent
->rphy
);
707 struct ex_phy
*phy
= &parent
->ex_dev
.ex_phy
[phy_id
];
708 struct domain_device
*child
= NULL
;
709 struct sas_rphy
*rphy
;
710 struct sas_expander_device
*edev
;
711 struct asd_sas_port
*port
;
714 if (phy
->routing_attr
== DIRECT_ROUTING
) {
715 SAS_DPRINTK("ex %016llx:0x%x:D <--> ex %016llx:0x%x is not "
717 SAS_ADDR(parent
->sas_addr
), phy_id
,
718 SAS_ADDR(phy
->attached_sas_addr
),
719 phy
->attached_phy_id
);
722 child
= kzalloc(sizeof(*child
), GFP_KERNEL
);
726 phy
->port
= sas_port_alloc(&parent
->rphy
->dev
, phy_id
);
727 /* FIXME: better error handling */
728 BUG_ON(sas_port_add(phy
->port
) != 0);
731 switch (phy
->attached_dev_type
) {
733 rphy
= sas_expander_alloc(phy
->port
,
734 SAS_EDGE_EXPANDER_DEVICE
);
737 rphy
= sas_expander_alloc(phy
->port
,
738 SAS_FANOUT_EXPANDER_DEVICE
);
741 rphy
= NULL
; /* shut gcc up */
746 edev
= rphy_to_expander_device(rphy
);
747 child
->dev_type
= phy
->attached_dev_type
;
748 child
->parent
= parent
;
750 child
->iproto
= phy
->attached_iproto
;
751 child
->tproto
= phy
->attached_tproto
;
752 memcpy(child
->sas_addr
, phy
->attached_sas_addr
, SAS_ADDR_SIZE
);
753 sas_hash_addr(child
->hashed_sas_addr
, child
->sas_addr
);
754 sas_ex_get_linkrate(parent
, child
, phy
);
755 edev
->level
= parent_ex
->level
+ 1;
756 parent
->port
->disc
.max_level
= max(parent
->port
->disc
.max_level
,
759 sas_fill_in_rphy(child
, rphy
);
762 spin_lock(&parent
->port
->dev_list_lock
);
763 list_add_tail(&child
->dev_list_node
, &parent
->port
->dev_list
);
764 spin_unlock(&parent
->port
->dev_list_lock
);
766 res
= sas_discover_expander(child
);
771 list_add_tail(&child
->siblings
, &parent
->ex_dev
.children
);
775 static int sas_ex_discover_dev(struct domain_device
*dev
, int phy_id
)
777 struct expander_device
*ex
= &dev
->ex_dev
;
778 struct ex_phy
*ex_phy
= &ex
->ex_phy
[phy_id
];
779 struct domain_device
*child
= NULL
;
783 if (ex_phy
->linkrate
== SAS_SATA_SPINUP_HOLD
) {
784 if (!sas_smp_phy_control(dev
, phy_id
, PHY_FUNC_LINK_RESET
, NULL
))
785 res
= sas_ex_phy_discover(dev
, phy_id
);
790 /* Parent and domain coherency */
791 if (!dev
->parent
&& (SAS_ADDR(ex_phy
->attached_sas_addr
) ==
792 SAS_ADDR(dev
->port
->sas_addr
))) {
793 sas_add_parent_port(dev
, phy_id
);
796 if (dev
->parent
&& (SAS_ADDR(ex_phy
->attached_sas_addr
) ==
797 SAS_ADDR(dev
->parent
->sas_addr
))) {
798 sas_add_parent_port(dev
, phy_id
);
799 if (ex_phy
->routing_attr
== TABLE_ROUTING
)
800 sas_configure_phy(dev
, phy_id
, dev
->port
->sas_addr
, 1);
804 if (sas_dev_present_in_domain(dev
->port
, ex_phy
->attached_sas_addr
))
805 sas_ex_disable_port(dev
, ex_phy
->attached_sas_addr
);
807 if (ex_phy
->attached_dev_type
== NO_DEVICE
) {
808 if (ex_phy
->routing_attr
== DIRECT_ROUTING
) {
809 memset(ex_phy
->attached_sas_addr
, 0, SAS_ADDR_SIZE
);
810 sas_configure_routing(dev
, ex_phy
->attached_sas_addr
);
813 } else if (ex_phy
->linkrate
== SAS_LINK_RATE_UNKNOWN
)
816 if (ex_phy
->attached_dev_type
!= SAS_END_DEV
&&
817 ex_phy
->attached_dev_type
!= FANOUT_DEV
&&
818 ex_phy
->attached_dev_type
!= EDGE_DEV
) {
819 SAS_DPRINTK("unknown device type(0x%x) attached to ex %016llx "
820 "phy 0x%x\n", ex_phy
->attached_dev_type
,
821 SAS_ADDR(dev
->sas_addr
),
826 res
= sas_configure_routing(dev
, ex_phy
->attached_sas_addr
);
828 SAS_DPRINTK("configure routing for dev %016llx "
829 "reported 0x%x. Forgotten\n",
830 SAS_ADDR(ex_phy
->attached_sas_addr
), res
);
831 sas_disable_routing(dev
, ex_phy
->attached_sas_addr
);
835 res
= sas_ex_join_wide_port(dev
, phy_id
);
837 SAS_DPRINTK("Attaching ex phy%d to wide port %016llx\n",
838 phy_id
, SAS_ADDR(ex_phy
->attached_sas_addr
));
842 switch (ex_phy
->attached_dev_type
) {
844 child
= sas_ex_discover_end_dev(dev
, phy_id
);
847 if (SAS_ADDR(dev
->port
->disc
.fanout_sas_addr
)) {
848 SAS_DPRINTK("second fanout expander %016llx phy 0x%x "
849 "attached to ex %016llx phy 0x%x\n",
850 SAS_ADDR(ex_phy
->attached_sas_addr
),
851 ex_phy
->attached_phy_id
,
852 SAS_ADDR(dev
->sas_addr
),
854 sas_ex_disable_phy(dev
, phy_id
);
857 memcpy(dev
->port
->disc
.fanout_sas_addr
,
858 ex_phy
->attached_sas_addr
, SAS_ADDR_SIZE
);
861 child
= sas_ex_discover_expander(dev
, phy_id
);
870 for (i
= 0; i
< ex
->num_phys
; i
++) {
871 if (ex
->ex_phy
[i
].phy_state
== PHY_VACANT
||
872 ex
->ex_phy
[i
].phy_state
== PHY_NOT_PRESENT
)
875 if (SAS_ADDR(ex
->ex_phy
[i
].attached_sas_addr
) ==
876 SAS_ADDR(child
->sas_addr
))
877 ex
->ex_phy
[i
].phy_state
= PHY_DEVICE_DISCOVERED
;
884 static int sas_find_sub_addr(struct domain_device
*dev
, u8
*sub_addr
)
886 struct expander_device
*ex
= &dev
->ex_dev
;
889 for (i
= 0; i
< ex
->num_phys
; i
++) {
890 struct ex_phy
*phy
= &ex
->ex_phy
[i
];
892 if (phy
->phy_state
== PHY_VACANT
||
893 phy
->phy_state
== PHY_NOT_PRESENT
)
896 if ((phy
->attached_dev_type
== EDGE_DEV
||
897 phy
->attached_dev_type
== FANOUT_DEV
) &&
898 phy
->routing_attr
== SUBTRACTIVE_ROUTING
) {
900 memcpy(sub_addr
, phy
->attached_sas_addr
,SAS_ADDR_SIZE
);
908 static int sas_check_level_subtractive_boundary(struct domain_device
*dev
)
910 struct expander_device
*ex
= &dev
->ex_dev
;
911 struct domain_device
*child
;
912 u8 sub_addr
[8] = {0, };
914 list_for_each_entry(child
, &ex
->children
, siblings
) {
915 if (child
->dev_type
!= EDGE_DEV
&&
916 child
->dev_type
!= FANOUT_DEV
)
918 if (sub_addr
[0] == 0) {
919 sas_find_sub_addr(child
, sub_addr
);
924 if (sas_find_sub_addr(child
, s2
) &&
925 (SAS_ADDR(sub_addr
) != SAS_ADDR(s2
))) {
927 SAS_DPRINTK("ex %016llx->%016llx-?->%016llx "
928 "diverges from subtractive "
929 "boundary %016llx\n",
930 SAS_ADDR(dev
->sas_addr
),
931 SAS_ADDR(child
->sas_addr
),
935 sas_ex_disable_port(child
, s2
);
942 * sas_ex_discover_devices -- discover devices attached to this expander
943 * dev: pointer to the expander domain device
944 * single: if you want to do a single phy, else set to -1;
946 * Configure this expander for use with its devices and register the
947 * devices of this expander.
949 static int sas_ex_discover_devices(struct domain_device
*dev
, int single
)
951 struct expander_device
*ex
= &dev
->ex_dev
;
952 int i
= 0, end
= ex
->num_phys
;
955 if (0 <= single
&& single
< end
) {
960 for ( ; i
< end
; i
++) {
961 struct ex_phy
*ex_phy
= &ex
->ex_phy
[i
];
963 if (ex_phy
->phy_state
== PHY_VACANT
||
964 ex_phy
->phy_state
== PHY_NOT_PRESENT
||
965 ex_phy
->phy_state
== PHY_DEVICE_DISCOVERED
)
968 switch (ex_phy
->linkrate
) {
969 case SAS_PHY_DISABLED
:
970 case SAS_PHY_RESET_PROBLEM
:
971 case SAS_SATA_PORT_SELECTOR
:
974 res
= sas_ex_discover_dev(dev
, i
);
982 sas_check_level_subtractive_boundary(dev
);
987 static int sas_check_ex_subtractive_boundary(struct domain_device
*dev
)
989 struct expander_device
*ex
= &dev
->ex_dev
;
991 u8
*sub_sas_addr
= NULL
;
993 if (dev
->dev_type
!= EDGE_DEV
)
996 for (i
= 0; i
< ex
->num_phys
; i
++) {
997 struct ex_phy
*phy
= &ex
->ex_phy
[i
];
999 if (phy
->phy_state
== PHY_VACANT
||
1000 phy
->phy_state
== PHY_NOT_PRESENT
)
1003 if ((phy
->attached_dev_type
== FANOUT_DEV
||
1004 phy
->attached_dev_type
== EDGE_DEV
) &&
1005 phy
->routing_attr
== SUBTRACTIVE_ROUTING
) {
1008 sub_sas_addr
= &phy
->attached_sas_addr
[0];
1009 else if (SAS_ADDR(sub_sas_addr
) !=
1010 SAS_ADDR(phy
->attached_sas_addr
)) {
1012 SAS_DPRINTK("ex %016llx phy 0x%x "
1013 "diverges(%016llx) on subtractive "
1014 "boundary(%016llx). Disabled\n",
1015 SAS_ADDR(dev
->sas_addr
), i
,
1016 SAS_ADDR(phy
->attached_sas_addr
),
1017 SAS_ADDR(sub_sas_addr
));
1018 sas_ex_disable_phy(dev
, i
);
1025 static void sas_print_parent_topology_bug(struct domain_device
*child
,
1026 struct ex_phy
*parent_phy
,
1027 struct ex_phy
*child_phy
)
1029 static const char ra_char
[] = {
1030 [DIRECT_ROUTING
] = 'D',
1031 [SUBTRACTIVE_ROUTING
] = 'S',
1032 [TABLE_ROUTING
] = 'T',
1034 static const char *ex_type
[] = {
1035 [EDGE_DEV
] = "edge",
1036 [FANOUT_DEV
] = "fanout",
1038 struct domain_device
*parent
= child
->parent
;
1040 sas_printk("%s ex %016llx phy 0x%x <--> %s ex %016llx phy 0x%x "
1041 "has %c:%c routing link!\n",
1043 ex_type
[parent
->dev_type
],
1044 SAS_ADDR(parent
->sas_addr
),
1047 ex_type
[child
->dev_type
],
1048 SAS_ADDR(child
->sas_addr
),
1051 ra_char
[parent_phy
->routing_attr
],
1052 ra_char
[child_phy
->routing_attr
]);
1055 static int sas_check_eeds(struct domain_device
*child
,
1056 struct ex_phy
*parent_phy
,
1057 struct ex_phy
*child_phy
)
1060 struct domain_device
*parent
= child
->parent
;
1062 if (SAS_ADDR(parent
->port
->disc
.fanout_sas_addr
) != 0) {
1064 SAS_DPRINTK("edge ex %016llx phy S:0x%x <--> edge ex %016llx "
1065 "phy S:0x%x, while there is a fanout ex %016llx\n",
1066 SAS_ADDR(parent
->sas_addr
),
1068 SAS_ADDR(child
->sas_addr
),
1070 SAS_ADDR(parent
->port
->disc
.fanout_sas_addr
));
1071 } else if (SAS_ADDR(parent
->port
->disc
.eeds_a
) == 0) {
1072 memcpy(parent
->port
->disc
.eeds_a
, parent
->sas_addr
,
1074 memcpy(parent
->port
->disc
.eeds_b
, child
->sas_addr
,
1076 } else if (((SAS_ADDR(parent
->port
->disc
.eeds_a
) ==
1077 SAS_ADDR(parent
->sas_addr
)) ||
1078 (SAS_ADDR(parent
->port
->disc
.eeds_a
) ==
1079 SAS_ADDR(child
->sas_addr
)))
1081 ((SAS_ADDR(parent
->port
->disc
.eeds_b
) ==
1082 SAS_ADDR(parent
->sas_addr
)) ||
1083 (SAS_ADDR(parent
->port
->disc
.eeds_b
) ==
1084 SAS_ADDR(child
->sas_addr
))))
1088 SAS_DPRINTK("edge ex %016llx phy 0x%x <--> edge ex %016llx "
1089 "phy 0x%x link forms a third EEDS!\n",
1090 SAS_ADDR(parent
->sas_addr
),
1092 SAS_ADDR(child
->sas_addr
),
1099 /* Here we spill over 80 columns. It is intentional.
1101 static int sas_check_parent_topology(struct domain_device
*child
)
1103 struct expander_device
*child_ex
= &child
->ex_dev
;
1104 struct expander_device
*parent_ex
;
1111 if (child
->parent
->dev_type
!= EDGE_DEV
&&
1112 child
->parent
->dev_type
!= FANOUT_DEV
)
1115 parent_ex
= &child
->parent
->ex_dev
;
1117 for (i
= 0; i
< parent_ex
->num_phys
; i
++) {
1118 struct ex_phy
*parent_phy
= &parent_ex
->ex_phy
[i
];
1119 struct ex_phy
*child_phy
;
1121 if (parent_phy
->phy_state
== PHY_VACANT
||
1122 parent_phy
->phy_state
== PHY_NOT_PRESENT
)
1125 if (SAS_ADDR(parent_phy
->attached_sas_addr
) != SAS_ADDR(child
->sas_addr
))
1128 child_phy
= &child_ex
->ex_phy
[parent_phy
->attached_phy_id
];
1130 switch (child
->parent
->dev_type
) {
1132 if (child
->dev_type
== FANOUT_DEV
) {
1133 if (parent_phy
->routing_attr
!= SUBTRACTIVE_ROUTING
||
1134 child_phy
->routing_attr
!= TABLE_ROUTING
) {
1135 sas_print_parent_topology_bug(child
, parent_phy
, child_phy
);
1138 } else if (parent_phy
->routing_attr
== SUBTRACTIVE_ROUTING
) {
1139 if (child_phy
->routing_attr
== SUBTRACTIVE_ROUTING
) {
1140 res
= sas_check_eeds(child
, parent_phy
, child_phy
);
1141 } else if (child_phy
->routing_attr
!= TABLE_ROUTING
) {
1142 sas_print_parent_topology_bug(child
, parent_phy
, child_phy
);
1145 } else if (parent_phy
->routing_attr
== TABLE_ROUTING
&&
1146 child_phy
->routing_attr
!= SUBTRACTIVE_ROUTING
) {
1147 sas_print_parent_topology_bug(child
, parent_phy
, child_phy
);
1152 if (parent_phy
->routing_attr
!= TABLE_ROUTING
||
1153 child_phy
->routing_attr
!= SUBTRACTIVE_ROUTING
) {
1154 sas_print_parent_topology_bug(child
, parent_phy
, child_phy
);
1166 #define RRI_REQ_SIZE 16
1167 #define RRI_RESP_SIZE 44
1169 static int sas_configure_present(struct domain_device
*dev
, int phy_id
,
1170 u8
*sas_addr
, int *index
, int *present
)
1173 struct expander_device
*ex
= &dev
->ex_dev
;
1174 struct ex_phy
*phy
= &ex
->ex_phy
[phy_id
];
1181 rri_req
= alloc_smp_req(RRI_REQ_SIZE
);
1185 rri_resp
= alloc_smp_resp(RRI_RESP_SIZE
);
1191 rri_req
[1] = SMP_REPORT_ROUTE_INFO
;
1192 rri_req
[9] = phy_id
;
1194 for (i
= 0; i
< ex
->max_route_indexes
; i
++) {
1195 *(__be16
*)(rri_req
+6) = cpu_to_be16(i
);
1196 res
= smp_execute_task(dev
, rri_req
, RRI_REQ_SIZE
, rri_resp
,
1201 if (res
== SMP_RESP_NO_INDEX
) {
1202 SAS_DPRINTK("overflow of indexes: dev %016llx "
1203 "phy 0x%x index 0x%x\n",
1204 SAS_ADDR(dev
->sas_addr
), phy_id
, i
);
1206 } else if (res
!= SMP_RESP_FUNC_ACC
) {
1207 SAS_DPRINTK("%s: dev %016llx phy 0x%x index 0x%x "
1208 "result 0x%x\n", __FUNCTION__
,
1209 SAS_ADDR(dev
->sas_addr
), phy_id
, i
, res
);
1212 if (SAS_ADDR(sas_addr
) != 0) {
1213 if (SAS_ADDR(rri_resp
+16) == SAS_ADDR(sas_addr
)) {
1215 if ((rri_resp
[12] & 0x80) == 0x80)
1220 } else if (SAS_ADDR(rri_resp
+16) == 0) {
1225 } else if (SAS_ADDR(rri_resp
+16) == 0 &&
1226 phy
->last_da_index
< i
) {
1227 phy
->last_da_index
= i
;
1240 #define CRI_REQ_SIZE 44
1241 #define CRI_RESP_SIZE 8
1243 static int sas_configure_set(struct domain_device
*dev
, int phy_id
,
1244 u8
*sas_addr
, int index
, int include
)
1250 cri_req
= alloc_smp_req(CRI_REQ_SIZE
);
1254 cri_resp
= alloc_smp_resp(CRI_RESP_SIZE
);
1260 cri_req
[1] = SMP_CONF_ROUTE_INFO
;
1261 *(__be16
*)(cri_req
+6) = cpu_to_be16(index
);
1262 cri_req
[9] = phy_id
;
1263 if (SAS_ADDR(sas_addr
) == 0 || !include
)
1264 cri_req
[12] |= 0x80;
1265 memcpy(cri_req
+16, sas_addr
, SAS_ADDR_SIZE
);
1267 res
= smp_execute_task(dev
, cri_req
, CRI_REQ_SIZE
, cri_resp
,
1272 if (res
== SMP_RESP_NO_INDEX
) {
1273 SAS_DPRINTK("overflow of indexes: dev %016llx phy 0x%x "
1275 SAS_ADDR(dev
->sas_addr
), phy_id
, index
);
1283 static int sas_configure_phy(struct domain_device
*dev
, int phy_id
,
1284 u8
*sas_addr
, int include
)
1290 res
= sas_configure_present(dev
, phy_id
, sas_addr
, &index
, &present
);
1293 if (include
^ present
)
1294 return sas_configure_set(dev
, phy_id
, sas_addr
, index
,include
);
1300 * sas_configure_parent -- configure routing table of parent
1301 * parent: parent expander
1302 * child: child expander
1303 * sas_addr: SAS port identifier of device directly attached to child
1305 static int sas_configure_parent(struct domain_device
*parent
,
1306 struct domain_device
*child
,
1307 u8
*sas_addr
, int include
)
1309 struct expander_device
*ex_parent
= &parent
->ex_dev
;
1313 if (parent
->parent
) {
1314 res
= sas_configure_parent(parent
->parent
, parent
, sas_addr
,
1320 if (ex_parent
->conf_route_table
== 0) {
1321 SAS_DPRINTK("ex %016llx has self-configuring routing table\n",
1322 SAS_ADDR(parent
->sas_addr
));
1326 for (i
= 0; i
< ex_parent
->num_phys
; i
++) {
1327 struct ex_phy
*phy
= &ex_parent
->ex_phy
[i
];
1329 if ((phy
->routing_attr
== TABLE_ROUTING
) &&
1330 (SAS_ADDR(phy
->attached_sas_addr
) ==
1331 SAS_ADDR(child
->sas_addr
))) {
1332 res
= sas_configure_phy(parent
, i
, sas_addr
, include
);
1342 * sas_configure_routing -- configure routing
1343 * dev: expander device
1344 * sas_addr: port identifier of device directly attached to the expander device
1346 static int sas_configure_routing(struct domain_device
*dev
, u8
*sas_addr
)
1349 return sas_configure_parent(dev
->parent
, dev
, sas_addr
, 1);
1353 static int sas_disable_routing(struct domain_device
*dev
, u8
*sas_addr
)
1356 return sas_configure_parent(dev
->parent
, dev
, sas_addr
, 0);
1361 #define SMP_BIN_ATTR_NAME "smp_portal"
1363 static void sas_ex_smp_hook(struct domain_device
*dev
)
1365 struct expander_device
*ex_dev
= &dev
->ex_dev
;
1366 struct bin_attribute
*bin_attr
= &ex_dev
->smp_bin_attr
;
1368 memset(bin_attr
, 0, sizeof(*bin_attr
));
1370 bin_attr
->attr
.name
= SMP_BIN_ATTR_NAME
;
1371 bin_attr
->attr
.owner
= THIS_MODULE
;
1372 bin_attr
->attr
.mode
= 0600;
1375 bin_attr
->private = NULL
;
1376 bin_attr
->read
= smp_portal_read
;
1377 bin_attr
->write
= smp_portal_write
;
1378 bin_attr
->mmap
= NULL
;
1380 ex_dev
->smp_portal_pid
= -1;
1381 init_MUTEX(&ex_dev
->smp_sema
);
1386 * sas_discover_expander -- expander discovery
1387 * @ex: pointer to expander domain device
1389 * See comment in sas_discover_sata().
1391 static int sas_discover_expander(struct domain_device
*dev
)
1395 res
= sas_notify_lldd_dev_found(dev
);
1399 res
= sas_ex_general(dev
);
1402 res
= sas_ex_manuf_info(dev
);
1406 res
= sas_expander_discover(dev
);
1408 SAS_DPRINTK("expander %016llx discovery failed(0x%x)\n",
1409 SAS_ADDR(dev
->sas_addr
), res
);
1413 sas_check_ex_subtractive_boundary(dev
);
1414 res
= sas_check_parent_topology(dev
);
1419 sas_notify_lldd_dev_gone(dev
);
1423 static int sas_ex_level_discovery(struct asd_sas_port
*port
, const int level
)
1426 struct domain_device
*dev
;
1428 list_for_each_entry(dev
, &port
->dev_list
, dev_list_node
) {
1429 if (dev
->dev_type
== EDGE_DEV
||
1430 dev
->dev_type
== FANOUT_DEV
) {
1431 struct sas_expander_device
*ex
=
1432 rphy_to_expander_device(dev
->rphy
);
1434 if (level
== ex
->level
)
1435 res
= sas_ex_discover_devices(dev
, -1);
1437 res
= sas_ex_discover_devices(port
->port_dev
, -1);
1445 static int sas_ex_bfs_disc(struct asd_sas_port
*port
)
1451 level
= port
->disc
.max_level
;
1452 res
= sas_ex_level_discovery(port
, level
);
1454 } while (level
< port
->disc
.max_level
);
1459 int sas_discover_root_expander(struct domain_device
*dev
)
1462 struct sas_expander_device
*ex
= rphy_to_expander_device(dev
->rphy
);
1464 res
= sas_rphy_add(dev
->rphy
);
1468 ex
->level
= dev
->port
->disc
.max_level
; /* 0 */
1469 res
= sas_discover_expander(dev
);
1473 sas_ex_bfs_disc(dev
->port
);
1478 sas_rphy_remove(dev
->rphy
);
1483 /* ---------- Domain revalidation ---------- */
1485 static int sas_get_phy_discover(struct domain_device
*dev
,
1486 int phy_id
, struct smp_resp
*disc_resp
)
1491 disc_req
= alloc_smp_req(DISCOVER_REQ_SIZE
);
1495 disc_req
[1] = SMP_DISCOVER
;
1496 disc_req
[9] = phy_id
;
1498 res
= smp_execute_task(dev
, disc_req
, DISCOVER_REQ_SIZE
,
1499 disc_resp
, DISCOVER_RESP_SIZE
);
1502 else if (disc_resp
->result
!= SMP_RESP_FUNC_ACC
) {
1503 res
= disc_resp
->result
;
1511 static int sas_get_phy_change_count(struct domain_device
*dev
,
1512 int phy_id
, int *pcc
)
1515 struct smp_resp
*disc_resp
;
1517 disc_resp
= alloc_smp_resp(DISCOVER_RESP_SIZE
);
1521 res
= sas_get_phy_discover(dev
, phy_id
, disc_resp
);
1523 *pcc
= disc_resp
->disc
.change_count
;
1529 static int sas_get_phy_attached_sas_addr(struct domain_device
*dev
,
1530 int phy_id
, u8
*attached_sas_addr
)
1533 struct smp_resp
*disc_resp
;
1534 struct discover_resp
*dr
;
1536 disc_resp
= alloc_smp_resp(DISCOVER_RESP_SIZE
);
1539 dr
= &disc_resp
->disc
;
1541 res
= sas_get_phy_discover(dev
, phy_id
, disc_resp
);
1543 memcpy(attached_sas_addr
,disc_resp
->disc
.attached_sas_addr
,8);
1544 if (dr
->attached_dev_type
== 0)
1545 memset(attached_sas_addr
, 0, 8);
1551 static int sas_find_bcast_phy(struct domain_device
*dev
, int *phy_id
,
1554 struct expander_device
*ex
= &dev
->ex_dev
;
1558 for (i
= from_phy
; i
< ex
->num_phys
; i
++) {
1559 int phy_change_count
= 0;
1561 res
= sas_get_phy_change_count(dev
, i
, &phy_change_count
);
1564 else if (phy_change_count
!= ex
->ex_phy
[i
].phy_change_count
) {
1565 ex
->ex_phy
[i
].phy_change_count
= phy_change_count
;
1574 static int sas_get_ex_change_count(struct domain_device
*dev
, int *ecc
)
1578 struct smp_resp
*rg_resp
;
1580 rg_req
= alloc_smp_req(RG_REQ_SIZE
);
1584 rg_resp
= alloc_smp_resp(RG_RESP_SIZE
);
1590 rg_req
[1] = SMP_REPORT_GENERAL
;
1592 res
= smp_execute_task(dev
, rg_req
, RG_REQ_SIZE
, rg_resp
,
1596 if (rg_resp
->result
!= SMP_RESP_FUNC_ACC
) {
1597 res
= rg_resp
->result
;
1601 *ecc
= be16_to_cpu(rg_resp
->rg
.change_count
);
1608 static int sas_find_bcast_dev(struct domain_device
*dev
,
1609 struct domain_device
**src_dev
)
1611 struct expander_device
*ex
= &dev
->ex_dev
;
1612 int ex_change_count
= -1;
1615 res
= sas_get_ex_change_count(dev
, &ex_change_count
);
1618 if (ex_change_count
!= -1 &&
1619 ex_change_count
!= ex
->ex_change_count
) {
1621 ex
->ex_change_count
= ex_change_count
;
1623 struct domain_device
*ch
;
1625 list_for_each_entry(ch
, &ex
->children
, siblings
) {
1626 if (ch
->dev_type
== EDGE_DEV
||
1627 ch
->dev_type
== FANOUT_DEV
) {
1628 res
= sas_find_bcast_dev(ch
, src_dev
);
1638 static void sas_unregister_ex_tree(struct domain_device
*dev
)
1640 struct expander_device
*ex
= &dev
->ex_dev
;
1641 struct domain_device
*child
, *n
;
1643 list_for_each_entry_safe(child
, n
, &ex
->children
, siblings
) {
1644 if (child
->dev_type
== EDGE_DEV
||
1645 child
->dev_type
== FANOUT_DEV
)
1646 sas_unregister_ex_tree(child
);
1648 sas_unregister_dev(child
);
1650 sas_unregister_dev(dev
);
1653 static void sas_unregister_devs_sas_addr(struct domain_device
*parent
,
1656 struct expander_device
*ex_dev
= &parent
->ex_dev
;
1657 struct ex_phy
*phy
= &ex_dev
->ex_phy
[phy_id
];
1658 struct domain_device
*child
, *n
;
1660 list_for_each_entry_safe(child
, n
, &ex_dev
->children
, siblings
) {
1661 if (SAS_ADDR(child
->sas_addr
) ==
1662 SAS_ADDR(phy
->attached_sas_addr
)) {
1663 if (child
->dev_type
== EDGE_DEV
||
1664 child
->dev_type
== FANOUT_DEV
)
1665 sas_unregister_ex_tree(child
);
1667 sas_unregister_dev(child
);
1671 sas_disable_routing(parent
, phy
->attached_sas_addr
);
1672 memset(phy
->attached_sas_addr
, 0, SAS_ADDR_SIZE
);
1673 sas_port_delete_phy(phy
->port
, phy
->phy
);
1674 if (phy
->port
->num_phys
== 0)
1675 sas_port_delete(phy
->port
);
1679 static int sas_discover_bfs_by_root_level(struct domain_device
*root
,
1682 struct expander_device
*ex_root
= &root
->ex_dev
;
1683 struct domain_device
*child
;
1686 list_for_each_entry(child
, &ex_root
->children
, siblings
) {
1687 if (child
->dev_type
== EDGE_DEV
||
1688 child
->dev_type
== FANOUT_DEV
) {
1689 struct sas_expander_device
*ex
=
1690 rphy_to_expander_device(child
->rphy
);
1692 if (level
> ex
->level
)
1693 res
= sas_discover_bfs_by_root_level(child
,
1695 else if (level
== ex
->level
)
1696 res
= sas_ex_discover_devices(child
, -1);
1702 static int sas_discover_bfs_by_root(struct domain_device
*dev
)
1705 struct sas_expander_device
*ex
= rphy_to_expander_device(dev
->rphy
);
1706 int level
= ex
->level
+1;
1708 res
= sas_ex_discover_devices(dev
, -1);
1712 res
= sas_discover_bfs_by_root_level(dev
, level
);
1715 } while (level
<= dev
->port
->disc
.max_level
);
1720 static int sas_discover_new(struct domain_device
*dev
, int phy_id
)
1722 struct ex_phy
*ex_phy
= &dev
->ex_dev
.ex_phy
[phy_id
];
1723 struct domain_device
*child
;
1726 SAS_DPRINTK("ex %016llx phy%d new device attached\n",
1727 SAS_ADDR(dev
->sas_addr
), phy_id
);
1728 res
= sas_ex_phy_discover(dev
, phy_id
);
1731 res
= sas_ex_discover_devices(dev
, phy_id
);
1734 list_for_each_entry(child
, &dev
->ex_dev
.children
, siblings
) {
1735 if (SAS_ADDR(child
->sas_addr
) ==
1736 SAS_ADDR(ex_phy
->attached_sas_addr
)) {
1737 if (child
->dev_type
== EDGE_DEV
||
1738 child
->dev_type
== FANOUT_DEV
)
1739 res
= sas_discover_bfs_by_root(child
);
1747 static int sas_rediscover_dev(struct domain_device
*dev
, int phy_id
)
1749 struct expander_device
*ex
= &dev
->ex_dev
;
1750 struct ex_phy
*phy
= &ex
->ex_phy
[phy_id
];
1751 u8 attached_sas_addr
[8];
1754 res
= sas_get_phy_attached_sas_addr(dev
, phy_id
, attached_sas_addr
);
1756 case SMP_RESP_NO_PHY
:
1757 phy
->phy_state
= PHY_NOT_PRESENT
;
1758 sas_unregister_devs_sas_addr(dev
, phy_id
);
1760 case SMP_RESP_PHY_VACANT
:
1761 phy
->phy_state
= PHY_VACANT
;
1762 sas_unregister_devs_sas_addr(dev
, phy_id
);
1764 case SMP_RESP_FUNC_ACC
:
1768 if (SAS_ADDR(attached_sas_addr
) == 0) {
1769 phy
->phy_state
= PHY_EMPTY
;
1770 sas_unregister_devs_sas_addr(dev
, phy_id
);
1771 } else if (SAS_ADDR(attached_sas_addr
) ==
1772 SAS_ADDR(phy
->attached_sas_addr
)) {
1773 SAS_DPRINTK("ex %016llx phy 0x%x broadcast flutter\n",
1774 SAS_ADDR(dev
->sas_addr
), phy_id
);
1775 sas_ex_phy_discover(dev
, phy_id
);
1777 res
= sas_discover_new(dev
, phy_id
);
1782 static int sas_rediscover(struct domain_device
*dev
, const int phy_id
)
1784 struct expander_device
*ex
= &dev
->ex_dev
;
1785 struct ex_phy
*changed_phy
= &ex
->ex_phy
[phy_id
];
1789 SAS_DPRINTK("ex %016llx phy%d originated BROADCAST(CHANGE)\n",
1790 SAS_ADDR(dev
->sas_addr
), phy_id
);
1792 if (SAS_ADDR(changed_phy
->attached_sas_addr
) != 0) {
1793 for (i
= 0; i
< ex
->num_phys
; i
++) {
1794 struct ex_phy
*phy
= &ex
->ex_phy
[i
];
1798 if (SAS_ADDR(phy
->attached_sas_addr
) ==
1799 SAS_ADDR(changed_phy
->attached_sas_addr
)) {
1800 SAS_DPRINTK("phy%d part of wide port with "
1801 "phy%d\n", phy_id
, i
);
1805 res
= sas_rediscover_dev(dev
, phy_id
);
1807 res
= sas_discover_new(dev
, phy_id
);
1813 * sas_revalidate_domain -- revalidate the domain
1814 * @port: port to the domain of interest
1816 * NOTE: this process _must_ quit (return) as soon as any connection
1817 * errors are encountered. Connection recovery is done elsewhere.
1818 * Discover process only interrogates devices in order to discover the
1821 int sas_ex_revalidate_domain(struct domain_device
*port_dev
)
1824 struct domain_device
*dev
= NULL
;
1826 res
= sas_find_bcast_dev(port_dev
, &dev
);
1830 struct expander_device
*ex
= &dev
->ex_dev
;
1835 res
= sas_find_bcast_phy(dev
, &phy_id
, i
);
1838 res
= sas_rediscover(dev
, phy_id
);
1840 } while (i
< ex
->num_phys
);
1847 /* ---------- SMP portal ---------- */
1849 static ssize_t
smp_portal_write(struct kobject
*kobj
, char *buf
, loff_t offs
,
1852 struct domain_device
*dev
= to_dom_device(kobj
);
1853 struct expander_device
*ex
= &dev
->ex_dev
;
1860 down_interruptible(&ex
->smp_sema
);
1863 ex
->smp_req
= kzalloc(size
, GFP_USER
);
1868 memcpy(ex
->smp_req
, buf
, size
);
1869 ex
->smp_req_size
= size
;
1870 ex
->smp_portal_pid
= current
->pid
;
1876 static ssize_t
smp_portal_read(struct kobject
*kobj
, char *buf
, loff_t offs
,
1879 struct domain_device
*dev
= to_dom_device(kobj
);
1880 struct expander_device
*ex
= &dev
->ex_dev
;
1884 /* XXX: sysfs gives us an offset of 0x10 or 0x8 while in fact
1888 down_interruptible(&ex
->smp_sema
);
1889 if (!ex
->smp_req
|| ex
->smp_portal_pid
!= current
->pid
)
1897 smp_resp
= alloc_smp_resp(size
);
1900 res
= smp_execute_task(dev
, ex
->smp_req
, ex
->smp_req_size
,
1903 memcpy(buf
, smp_resp
, size
);
1911 ex
->smp_req_size
= 0;
1912 ex
->smp_portal_pid
= -1;