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
);
41 /* FIXME: smp needs to migrate into the sas class */
42 static ssize_t
smp_portal_read(struct kobject
*, struct bin_attribute
*,
43 char *, loff_t
, size_t);
44 static ssize_t
smp_portal_write(struct kobject
*, struct bin_attribute
*,
45 char *, loff_t
, size_t);
48 /* ---------- SMP task management ---------- */
50 static void smp_task_timedout(unsigned long _task
)
52 struct sas_task
*task
= (void *) _task
;
55 spin_lock_irqsave(&task
->task_state_lock
, flags
);
56 if (!(task
->task_state_flags
& SAS_TASK_STATE_DONE
))
57 task
->task_state_flags
|= SAS_TASK_STATE_ABORTED
;
58 spin_unlock_irqrestore(&task
->task_state_lock
, flags
);
60 complete(&task
->completion
);
63 static void smp_task_done(struct sas_task
*task
)
65 if (!del_timer(&task
->timer
))
67 complete(&task
->completion
);
70 /* Give it some long enough timeout. In seconds. */
71 #define SMP_TIMEOUT 10
73 static int smp_execute_task(struct domain_device
*dev
, void *req
, int req_size
,
74 void *resp
, int resp_size
)
77 struct sas_task
*task
= NULL
;
78 struct sas_internal
*i
=
79 to_sas_internal(dev
->port
->ha
->core
.shost
->transportt
);
81 for (retry
= 0; retry
< 3; retry
++) {
82 task
= sas_alloc_task(GFP_KERNEL
);
87 task
->task_proto
= dev
->tproto
;
88 sg_init_one(&task
->smp_task
.smp_req
, req
, req_size
);
89 sg_init_one(&task
->smp_task
.smp_resp
, resp
, resp_size
);
91 task
->task_done
= smp_task_done
;
93 task
->timer
.data
= (unsigned long) task
;
94 task
->timer
.function
= smp_task_timedout
;
95 task
->timer
.expires
= jiffies
+ SMP_TIMEOUT
*HZ
;
96 add_timer(&task
->timer
);
98 res
= i
->dft
->lldd_execute_task(task
, 1, GFP_KERNEL
);
101 del_timer(&task
->timer
);
102 SAS_DPRINTK("executing SMP task failed:%d\n", res
);
106 wait_for_completion(&task
->completion
);
108 if ((task
->task_state_flags
& SAS_TASK_STATE_ABORTED
)) {
109 SAS_DPRINTK("smp task timed out or aborted\n");
110 i
->dft
->lldd_abort_task(task
);
111 if (!(task
->task_state_flags
& SAS_TASK_STATE_DONE
)) {
112 SAS_DPRINTK("SMP task aborted and not done\n");
116 if (task
->task_status
.resp
== SAS_TASK_COMPLETE
&&
117 task
->task_status
.stat
== SAM_GOOD
) {
121 SAS_DPRINTK("%s: task to dev %016llx response: 0x%x "
122 "status 0x%x\n", __FUNCTION__
,
123 SAS_ADDR(dev
->sas_addr
),
124 task
->task_status
.resp
,
125 task
->task_status
.stat
);
131 BUG_ON(retry
== 3 && task
!= NULL
);
138 /* ---------- Allocations ---------- */
140 static inline void *alloc_smp_req(int size
)
142 u8
*p
= kzalloc(size
, GFP_KERNEL
);
148 static inline void *alloc_smp_resp(int size
)
150 return kzalloc(size
, GFP_KERNEL
);
153 /* ---------- Expander configuration ---------- */
155 static void sas_set_ex_phy(struct domain_device
*dev
, int phy_id
,
158 struct expander_device
*ex
= &dev
->ex_dev
;
159 struct ex_phy
*phy
= &ex
->ex_phy
[phy_id
];
160 struct smp_resp
*resp
= disc_resp
;
161 struct discover_resp
*dr
= &resp
->disc
;
162 struct sas_rphy
*rphy
= dev
->rphy
;
163 int rediscover
= (phy
->phy
!= NULL
);
166 phy
->phy
= sas_phy_alloc(&rphy
->dev
, phy_id
);
168 /* FIXME: error_handling */
172 switch (resp
->result
) {
173 case SMP_RESP_PHY_VACANT
:
174 phy
->phy_state
= PHY_VACANT
;
177 phy
->phy_state
= PHY_NOT_PRESENT
;
179 case SMP_RESP_FUNC_ACC
:
180 phy
->phy_state
= PHY_EMPTY
; /* do not know yet */
184 phy
->phy_id
= phy_id
;
185 phy
->attached_dev_type
= dr
->attached_dev_type
;
186 phy
->linkrate
= dr
->linkrate
;
187 phy
->attached_sata_host
= dr
->attached_sata_host
;
188 phy
->attached_sata_dev
= dr
->attached_sata_dev
;
189 phy
->attached_sata_ps
= dr
->attached_sata_ps
;
190 phy
->attached_iproto
= dr
->iproto
<< 1;
191 phy
->attached_tproto
= dr
->tproto
<< 1;
192 memcpy(phy
->attached_sas_addr
, dr
->attached_sas_addr
, SAS_ADDR_SIZE
);
193 phy
->attached_phy_id
= dr
->attached_phy_id
;
194 phy
->phy_change_count
= dr
->change_count
;
195 phy
->routing_attr
= dr
->routing_attr
;
196 phy
->virtual = dr
->virtual;
197 phy
->last_da_index
= -1;
199 phy
->phy
->identify
.initiator_port_protocols
= phy
->attached_iproto
;
200 phy
->phy
->identify
.target_port_protocols
= phy
->attached_tproto
;
201 phy
->phy
->identify
.phy_identifier
= phy_id
;
202 phy
->phy
->minimum_linkrate_hw
= dr
->hmin_linkrate
;
203 phy
->phy
->maximum_linkrate_hw
= dr
->hmax_linkrate
;
204 phy
->phy
->minimum_linkrate
= dr
->pmin_linkrate
;
205 phy
->phy
->maximum_linkrate
= dr
->pmax_linkrate
;
206 phy
->phy
->negotiated_linkrate
= phy
->linkrate
;
209 sas_phy_add(phy
->phy
);
211 SAS_DPRINTK("ex %016llx phy%02d:%c attached: %016llx\n",
212 SAS_ADDR(dev
->sas_addr
), phy
->phy_id
,
213 phy
->routing_attr
== TABLE_ROUTING
? 'T' :
214 phy
->routing_attr
== DIRECT_ROUTING
? 'D' :
215 phy
->routing_attr
== SUBTRACTIVE_ROUTING
? 'S' : '?',
216 SAS_ADDR(phy
->attached_sas_addr
));
221 #define DISCOVER_REQ_SIZE 16
222 #define DISCOVER_RESP_SIZE 56
224 static int sas_ex_phy_discover_helper(struct domain_device
*dev
, u8
*disc_req
,
225 u8
*disc_resp
, int single
)
229 disc_req
[9] = single
;
230 for (i
= 1 ; i
< 3; i
++) {
231 struct discover_resp
*dr
;
233 res
= smp_execute_task(dev
, disc_req
, DISCOVER_REQ_SIZE
,
234 disc_resp
, DISCOVER_RESP_SIZE
);
237 /* This is detecting a failure to transmit inital
238 * dev to host FIS as described in section G.5 of
240 dr
= &((struct smp_resp
*)disc_resp
)->disc
;
241 if (!(dr
->attached_dev_type
== 0 &&
242 dr
->attached_sata_dev
))
244 /* In order to generate the dev to host FIS, we
245 * send a link reset to the expander port */
246 sas_smp_phy_control(dev
, single
, PHY_FUNC_LINK_RESET
, NULL
);
247 /* Wait for the reset to trigger the negotiation */
250 sas_set_ex_phy(dev
, single
, disc_resp
);
254 static int sas_ex_phy_discover(struct domain_device
*dev
, int single
)
256 struct expander_device
*ex
= &dev
->ex_dev
;
261 disc_req
= alloc_smp_req(DISCOVER_REQ_SIZE
);
265 disc_resp
= alloc_smp_req(DISCOVER_RESP_SIZE
);
271 disc_req
[1] = SMP_DISCOVER
;
273 if (0 <= single
&& single
< ex
->num_phys
) {
274 res
= sas_ex_phy_discover_helper(dev
, disc_req
, disc_resp
, single
);
278 for (i
= 0; i
< ex
->num_phys
; i
++) {
279 res
= sas_ex_phy_discover_helper(dev
, disc_req
,
291 static int sas_expander_discover(struct domain_device
*dev
)
293 struct expander_device
*ex
= &dev
->ex_dev
;
296 ex
->ex_phy
= kzalloc(sizeof(*ex
->ex_phy
)*ex
->num_phys
, GFP_KERNEL
);
300 res
= sas_ex_phy_discover(dev
, -1);
311 #define MAX_EXPANDER_PHYS 128
313 static void ex_assign_report_general(struct domain_device
*dev
,
314 struct smp_resp
*resp
)
316 struct report_general_resp
*rg
= &resp
->rg
;
318 dev
->ex_dev
.ex_change_count
= be16_to_cpu(rg
->change_count
);
319 dev
->ex_dev
.max_route_indexes
= be16_to_cpu(rg
->route_indexes
);
320 dev
->ex_dev
.num_phys
= min(rg
->num_phys
, (u8
)MAX_EXPANDER_PHYS
);
321 dev
->ex_dev
.conf_route_table
= rg
->conf_route_table
;
322 dev
->ex_dev
.configuring
= rg
->configuring
;
323 memcpy(dev
->ex_dev
.enclosure_logical_id
, rg
->enclosure_logical_id
, 8);
326 #define RG_REQ_SIZE 8
327 #define RG_RESP_SIZE 32
329 static int sas_ex_general(struct domain_device
*dev
)
332 struct smp_resp
*rg_resp
;
336 rg_req
= alloc_smp_req(RG_REQ_SIZE
);
340 rg_resp
= alloc_smp_resp(RG_RESP_SIZE
);
346 rg_req
[1] = SMP_REPORT_GENERAL
;
348 for (i
= 0; i
< 5; i
++) {
349 res
= smp_execute_task(dev
, rg_req
, RG_REQ_SIZE
, rg_resp
,
353 SAS_DPRINTK("RG to ex %016llx failed:0x%x\n",
354 SAS_ADDR(dev
->sas_addr
), res
);
356 } else if (rg_resp
->result
!= SMP_RESP_FUNC_ACC
) {
357 SAS_DPRINTK("RG:ex %016llx returned SMP result:0x%x\n",
358 SAS_ADDR(dev
->sas_addr
), rg_resp
->result
);
359 res
= rg_resp
->result
;
363 ex_assign_report_general(dev
, rg_resp
);
365 if (dev
->ex_dev
.configuring
) {
366 SAS_DPRINTK("RG: ex %llx self-configuring...\n",
367 SAS_ADDR(dev
->sas_addr
));
368 schedule_timeout_interruptible(5*HZ
);
378 static void ex_assign_manuf_info(struct domain_device
*dev
, void
381 u8
*mi_resp
= _mi_resp
;
382 struct sas_rphy
*rphy
= dev
->rphy
;
383 struct sas_expander_device
*edev
= rphy_to_expander_device(rphy
);
385 memcpy(edev
->vendor_id
, mi_resp
+ 12, SAS_EXPANDER_VENDOR_ID_LEN
);
386 memcpy(edev
->product_id
, mi_resp
+ 20, SAS_EXPANDER_PRODUCT_ID_LEN
);
387 memcpy(edev
->product_rev
, mi_resp
+ 36,
388 SAS_EXPANDER_PRODUCT_REV_LEN
);
390 if (mi_resp
[8] & 1) {
391 memcpy(edev
->component_vendor_id
, mi_resp
+ 40,
392 SAS_EXPANDER_COMPONENT_VENDOR_ID_LEN
);
393 edev
->component_id
= mi_resp
[48] << 8 | mi_resp
[49];
394 edev
->component_revision_id
= mi_resp
[50];
398 #define MI_REQ_SIZE 8
399 #define MI_RESP_SIZE 64
401 static int sas_ex_manuf_info(struct domain_device
*dev
)
407 mi_req
= alloc_smp_req(MI_REQ_SIZE
);
411 mi_resp
= alloc_smp_resp(MI_RESP_SIZE
);
417 mi_req
[1] = SMP_REPORT_MANUF_INFO
;
419 res
= smp_execute_task(dev
, mi_req
, MI_REQ_SIZE
, mi_resp
,MI_RESP_SIZE
);
421 SAS_DPRINTK("MI: ex %016llx failed:0x%x\n",
422 SAS_ADDR(dev
->sas_addr
), res
);
424 } else if (mi_resp
[2] != SMP_RESP_FUNC_ACC
) {
425 SAS_DPRINTK("MI ex %016llx returned SMP result:0x%x\n",
426 SAS_ADDR(dev
->sas_addr
), mi_resp
[2]);
430 ex_assign_manuf_info(dev
, mi_resp
);
437 #define PC_REQ_SIZE 44
438 #define PC_RESP_SIZE 8
440 int sas_smp_phy_control(struct domain_device
*dev
, int phy_id
,
441 enum phy_func phy_func
,
442 struct sas_phy_linkrates
*rates
)
448 pc_req
= alloc_smp_req(PC_REQ_SIZE
);
452 pc_resp
= alloc_smp_resp(PC_RESP_SIZE
);
458 pc_req
[1] = SMP_PHY_CONTROL
;
460 pc_req
[10]= phy_func
;
462 pc_req
[32] = rates
->minimum_linkrate
<< 4;
463 pc_req
[33] = rates
->maximum_linkrate
<< 4;
466 res
= smp_execute_task(dev
, pc_req
, PC_REQ_SIZE
, pc_resp
,PC_RESP_SIZE
);
473 static void sas_ex_disable_phy(struct domain_device
*dev
, int phy_id
)
475 struct expander_device
*ex
= &dev
->ex_dev
;
476 struct ex_phy
*phy
= &ex
->ex_phy
[phy_id
];
478 sas_smp_phy_control(dev
, phy_id
, PHY_FUNC_DISABLE
, NULL
);
479 phy
->linkrate
= SAS_PHY_DISABLED
;
482 static void sas_ex_disable_port(struct domain_device
*dev
, u8
*sas_addr
)
484 struct expander_device
*ex
= &dev
->ex_dev
;
487 for (i
= 0; i
< ex
->num_phys
; i
++) {
488 struct ex_phy
*phy
= &ex
->ex_phy
[i
];
490 if (phy
->phy_state
== PHY_VACANT
||
491 phy
->phy_state
== PHY_NOT_PRESENT
)
494 if (SAS_ADDR(phy
->attached_sas_addr
) == SAS_ADDR(sas_addr
))
495 sas_ex_disable_phy(dev
, i
);
499 static int sas_dev_present_in_domain(struct asd_sas_port
*port
,
502 struct domain_device
*dev
;
504 if (SAS_ADDR(port
->sas_addr
) == SAS_ADDR(sas_addr
))
506 list_for_each_entry(dev
, &port
->dev_list
, dev_list_node
) {
507 if (SAS_ADDR(dev
->sas_addr
) == SAS_ADDR(sas_addr
))
513 #define RPEL_REQ_SIZE 16
514 #define RPEL_RESP_SIZE 32
515 int sas_smp_get_phy_events(struct sas_phy
*phy
)
518 struct sas_rphy
*rphy
= dev_to_rphy(phy
->dev
.parent
);
519 struct domain_device
*dev
= sas_find_dev_by_rphy(rphy
);
520 u8
*req
= alloc_smp_req(RPEL_REQ_SIZE
);
521 u8
*resp
= kzalloc(RPEL_RESP_SIZE
, GFP_KERNEL
);
526 req
[1] = SMP_REPORT_PHY_ERR_LOG
;
527 req
[9] = phy
->number
;
529 res
= smp_execute_task(dev
, req
, RPEL_REQ_SIZE
,
530 resp
, RPEL_RESP_SIZE
);
535 phy
->invalid_dword_count
= scsi_to_u32(&resp
[12]);
536 phy
->running_disparity_error_count
= scsi_to_u32(&resp
[16]);
537 phy
->loss_of_dword_sync_count
= scsi_to_u32(&resp
[20]);
538 phy
->phy_reset_problem_count
= scsi_to_u32(&resp
[24]);
546 #define RPS_REQ_SIZE 16
547 #define RPS_RESP_SIZE 60
549 static int sas_get_report_phy_sata(struct domain_device
*dev
,
551 struct smp_resp
*rps_resp
)
554 u8
*rps_req
= alloc_smp_req(RPS_REQ_SIZE
);
555 u8
*resp
= (u8
*)rps_resp
;
560 rps_req
[1] = SMP_REPORT_PHY_SATA
;
563 res
= smp_execute_task(dev
, rps_req
, RPS_REQ_SIZE
,
564 rps_resp
, RPS_RESP_SIZE
);
566 /* 0x34 is the FIS type for the D2H fis. There's a potential
567 * standards cockup here. sas-2 explicitly specifies the FIS
568 * should be encoded so that FIS type is in resp[24].
569 * However, some expanders endian reverse this. Undo the
571 if (!res
&& resp
[27] == 0x34 && resp
[24] != 0x34) {
574 for (i
= 0; i
< 5; i
++) {
579 resp
[j
+ 0] = resp
[j
+ 3];
580 resp
[j
+ 1] = resp
[j
+ 2];
590 static void sas_ex_get_linkrate(struct domain_device
*parent
,
591 struct domain_device
*child
,
592 struct ex_phy
*parent_phy
)
594 struct expander_device
*parent_ex
= &parent
->ex_dev
;
595 struct sas_port
*port
;
600 port
= parent_phy
->port
;
602 for (i
= 0; i
< parent_ex
->num_phys
; i
++) {
603 struct ex_phy
*phy
= &parent_ex
->ex_phy
[i
];
605 if (phy
->phy_state
== PHY_VACANT
||
606 phy
->phy_state
== PHY_NOT_PRESENT
)
609 if (SAS_ADDR(phy
->attached_sas_addr
) ==
610 SAS_ADDR(child
->sas_addr
)) {
612 child
->min_linkrate
= min(parent
->min_linkrate
,
614 child
->max_linkrate
= max(parent
->max_linkrate
,
617 sas_port_add_phy(port
, phy
->phy
);
620 child
->linkrate
= min(parent_phy
->linkrate
, child
->max_linkrate
);
621 child
->pathways
= min(child
->pathways
, parent
->pathways
);
624 static struct domain_device
*sas_ex_discover_end_dev(
625 struct domain_device
*parent
, int phy_id
)
627 struct expander_device
*parent_ex
= &parent
->ex_dev
;
628 struct ex_phy
*phy
= &parent_ex
->ex_phy
[phy_id
];
629 struct domain_device
*child
= NULL
;
630 struct sas_rphy
*rphy
;
633 if (phy
->attached_sata_host
|| phy
->attached_sata_ps
)
636 child
= kzalloc(sizeof(*child
), GFP_KERNEL
);
640 child
->parent
= parent
;
641 child
->port
= parent
->port
;
642 child
->iproto
= phy
->attached_iproto
;
643 memcpy(child
->sas_addr
, phy
->attached_sas_addr
, SAS_ADDR_SIZE
);
644 sas_hash_addr(child
->hashed_sas_addr
, child
->sas_addr
);
646 phy
->port
= sas_port_alloc(&parent
->rphy
->dev
, phy_id
);
647 if (unlikely(!phy
->port
))
649 if (unlikely(sas_port_add(phy
->port
) != 0)) {
650 sas_port_free(phy
->port
);
654 sas_ex_get_linkrate(parent
, child
, phy
);
656 if ((phy
->attached_tproto
& SAS_PROTO_STP
) || phy
->attached_sata_dev
) {
657 child
->dev_type
= SATA_DEV
;
658 if (phy
->attached_tproto
& SAS_PROTO_STP
)
659 child
->tproto
= phy
->attached_tproto
;
660 if (phy
->attached_sata_dev
)
661 child
->tproto
|= SATA_DEV
;
662 res
= sas_get_report_phy_sata(parent
, phy_id
,
663 &child
->sata_dev
.rps_resp
);
665 SAS_DPRINTK("report phy sata to %016llx:0x%x returned "
666 "0x%x\n", SAS_ADDR(parent
->sas_addr
),
670 memcpy(child
->frame_rcvd
, &child
->sata_dev
.rps_resp
.rps
.fis
,
671 sizeof(struct dev_to_host_fis
));
673 rphy
= sas_end_device_alloc(phy
->port
);
681 spin_lock_irq(&parent
->port
->dev_list_lock
);
682 list_add_tail(&child
->dev_list_node
, &parent
->port
->dev_list
);
683 spin_unlock_irq(&parent
->port
->dev_list_lock
);
685 res
= sas_discover_sata(child
);
687 SAS_DPRINTK("sas_discover_sata() for device %16llx at "
688 "%016llx:0x%x returned 0x%x\n",
689 SAS_ADDR(child
->sas_addr
),
690 SAS_ADDR(parent
->sas_addr
), phy_id
, res
);
693 } else if (phy
->attached_tproto
& SAS_PROTO_SSP
) {
694 child
->dev_type
= SAS_END_DEV
;
695 rphy
= sas_end_device_alloc(phy
->port
);
696 /* FIXME: error handling */
699 child
->tproto
= phy
->attached_tproto
;
703 sas_fill_in_rphy(child
, rphy
);
705 spin_lock_irq(&parent
->port
->dev_list_lock
);
706 list_add_tail(&child
->dev_list_node
, &parent
->port
->dev_list
);
707 spin_unlock_irq(&parent
->port
->dev_list_lock
);
709 res
= sas_discover_end_dev(child
);
711 SAS_DPRINTK("sas_discover_end_dev() for device %16llx "
712 "at %016llx:0x%x returned 0x%x\n",
713 SAS_ADDR(child
->sas_addr
),
714 SAS_ADDR(parent
->sas_addr
), phy_id
, res
);
718 SAS_DPRINTK("target proto 0x%x at %016llx:0x%x not handled\n",
719 phy
->attached_tproto
, SAS_ADDR(parent
->sas_addr
),
723 list_add_tail(&child
->siblings
, &parent_ex
->children
);
727 sas_rphy_free(child
->rphy
);
729 list_del(&child
->dev_list_node
);
731 sas_port_delete(phy
->port
);
738 /* See if this phy is part of a wide port */
739 static int sas_ex_join_wide_port(struct domain_device
*parent
, int phy_id
)
741 struct ex_phy
*phy
= &parent
->ex_dev
.ex_phy
[phy_id
];
744 for (i
= 0; i
< parent
->ex_dev
.num_phys
; i
++) {
745 struct ex_phy
*ephy
= &parent
->ex_dev
.ex_phy
[i
];
750 if (!memcmp(phy
->attached_sas_addr
, ephy
->attached_sas_addr
,
751 SAS_ADDR_SIZE
) && ephy
->port
) {
752 sas_port_add_phy(ephy
->port
, phy
->phy
);
753 phy
->phy_state
= PHY_DEVICE_DISCOVERED
;
761 static struct domain_device
*sas_ex_discover_expander(
762 struct domain_device
*parent
, int phy_id
)
764 struct sas_expander_device
*parent_ex
= rphy_to_expander_device(parent
->rphy
);
765 struct ex_phy
*phy
= &parent
->ex_dev
.ex_phy
[phy_id
];
766 struct domain_device
*child
= NULL
;
767 struct sas_rphy
*rphy
;
768 struct sas_expander_device
*edev
;
769 struct asd_sas_port
*port
;
772 if (phy
->routing_attr
== DIRECT_ROUTING
) {
773 SAS_DPRINTK("ex %016llx:0x%x:D <--> ex %016llx:0x%x is not "
775 SAS_ADDR(parent
->sas_addr
), phy_id
,
776 SAS_ADDR(phy
->attached_sas_addr
),
777 phy
->attached_phy_id
);
780 child
= kzalloc(sizeof(*child
), GFP_KERNEL
);
784 phy
->port
= sas_port_alloc(&parent
->rphy
->dev
, phy_id
);
785 /* FIXME: better error handling */
786 BUG_ON(sas_port_add(phy
->port
) != 0);
789 switch (phy
->attached_dev_type
) {
791 rphy
= sas_expander_alloc(phy
->port
,
792 SAS_EDGE_EXPANDER_DEVICE
);
795 rphy
= sas_expander_alloc(phy
->port
,
796 SAS_FANOUT_EXPANDER_DEVICE
);
799 rphy
= NULL
; /* shut gcc up */
804 edev
= rphy_to_expander_device(rphy
);
805 child
->dev_type
= phy
->attached_dev_type
;
806 child
->parent
= parent
;
808 child
->iproto
= phy
->attached_iproto
;
809 child
->tproto
= phy
->attached_tproto
;
810 memcpy(child
->sas_addr
, phy
->attached_sas_addr
, SAS_ADDR_SIZE
);
811 sas_hash_addr(child
->hashed_sas_addr
, child
->sas_addr
);
812 sas_ex_get_linkrate(parent
, child
, phy
);
813 edev
->level
= parent_ex
->level
+ 1;
814 parent
->port
->disc
.max_level
= max(parent
->port
->disc
.max_level
,
817 sas_fill_in_rphy(child
, rphy
);
820 spin_lock_irq(&parent
->port
->dev_list_lock
);
821 list_add_tail(&child
->dev_list_node
, &parent
->port
->dev_list
);
822 spin_unlock_irq(&parent
->port
->dev_list_lock
);
824 res
= sas_discover_expander(child
);
829 list_add_tail(&child
->siblings
, &parent
->ex_dev
.children
);
833 static int sas_ex_discover_dev(struct domain_device
*dev
, int phy_id
)
835 struct expander_device
*ex
= &dev
->ex_dev
;
836 struct ex_phy
*ex_phy
= &ex
->ex_phy
[phy_id
];
837 struct domain_device
*child
= NULL
;
841 if (ex_phy
->linkrate
== SAS_SATA_SPINUP_HOLD
) {
842 if (!sas_smp_phy_control(dev
, phy_id
, PHY_FUNC_LINK_RESET
, NULL
))
843 res
= sas_ex_phy_discover(dev
, phy_id
);
848 /* Parent and domain coherency */
849 if (!dev
->parent
&& (SAS_ADDR(ex_phy
->attached_sas_addr
) ==
850 SAS_ADDR(dev
->port
->sas_addr
))) {
851 sas_add_parent_port(dev
, phy_id
);
854 if (dev
->parent
&& (SAS_ADDR(ex_phy
->attached_sas_addr
) ==
855 SAS_ADDR(dev
->parent
->sas_addr
))) {
856 sas_add_parent_port(dev
, phy_id
);
857 if (ex_phy
->routing_attr
== TABLE_ROUTING
)
858 sas_configure_phy(dev
, phy_id
, dev
->port
->sas_addr
, 1);
862 if (sas_dev_present_in_domain(dev
->port
, ex_phy
->attached_sas_addr
))
863 sas_ex_disable_port(dev
, ex_phy
->attached_sas_addr
);
865 if (ex_phy
->attached_dev_type
== NO_DEVICE
) {
866 if (ex_phy
->routing_attr
== DIRECT_ROUTING
) {
867 memset(ex_phy
->attached_sas_addr
, 0, SAS_ADDR_SIZE
);
868 sas_configure_routing(dev
, ex_phy
->attached_sas_addr
);
871 } else if (ex_phy
->linkrate
== SAS_LINK_RATE_UNKNOWN
)
874 if (ex_phy
->attached_dev_type
!= SAS_END_DEV
&&
875 ex_phy
->attached_dev_type
!= FANOUT_DEV
&&
876 ex_phy
->attached_dev_type
!= EDGE_DEV
) {
877 SAS_DPRINTK("unknown device type(0x%x) attached to ex %016llx "
878 "phy 0x%x\n", ex_phy
->attached_dev_type
,
879 SAS_ADDR(dev
->sas_addr
),
884 res
= sas_configure_routing(dev
, ex_phy
->attached_sas_addr
);
886 SAS_DPRINTK("configure routing for dev %016llx "
887 "reported 0x%x. Forgotten\n",
888 SAS_ADDR(ex_phy
->attached_sas_addr
), res
);
889 sas_disable_routing(dev
, ex_phy
->attached_sas_addr
);
893 res
= sas_ex_join_wide_port(dev
, phy_id
);
895 SAS_DPRINTK("Attaching ex phy%d to wide port %016llx\n",
896 phy_id
, SAS_ADDR(ex_phy
->attached_sas_addr
));
900 switch (ex_phy
->attached_dev_type
) {
902 child
= sas_ex_discover_end_dev(dev
, phy_id
);
905 if (SAS_ADDR(dev
->port
->disc
.fanout_sas_addr
)) {
906 SAS_DPRINTK("second fanout expander %016llx phy 0x%x "
907 "attached to ex %016llx phy 0x%x\n",
908 SAS_ADDR(ex_phy
->attached_sas_addr
),
909 ex_phy
->attached_phy_id
,
910 SAS_ADDR(dev
->sas_addr
),
912 sas_ex_disable_phy(dev
, phy_id
);
915 memcpy(dev
->port
->disc
.fanout_sas_addr
,
916 ex_phy
->attached_sas_addr
, SAS_ADDR_SIZE
);
919 child
= sas_ex_discover_expander(dev
, phy_id
);
928 for (i
= 0; i
< ex
->num_phys
; i
++) {
929 if (ex
->ex_phy
[i
].phy_state
== PHY_VACANT
||
930 ex
->ex_phy
[i
].phy_state
== PHY_NOT_PRESENT
)
933 if (SAS_ADDR(ex
->ex_phy
[i
].attached_sas_addr
) ==
934 SAS_ADDR(child
->sas_addr
))
935 ex
->ex_phy
[i
].phy_state
= PHY_DEVICE_DISCOVERED
;
942 static int sas_find_sub_addr(struct domain_device
*dev
, u8
*sub_addr
)
944 struct expander_device
*ex
= &dev
->ex_dev
;
947 for (i
= 0; i
< ex
->num_phys
; i
++) {
948 struct ex_phy
*phy
= &ex
->ex_phy
[i
];
950 if (phy
->phy_state
== PHY_VACANT
||
951 phy
->phy_state
== PHY_NOT_PRESENT
)
954 if ((phy
->attached_dev_type
== EDGE_DEV
||
955 phy
->attached_dev_type
== FANOUT_DEV
) &&
956 phy
->routing_attr
== SUBTRACTIVE_ROUTING
) {
958 memcpy(sub_addr
, phy
->attached_sas_addr
,SAS_ADDR_SIZE
);
966 static int sas_check_level_subtractive_boundary(struct domain_device
*dev
)
968 struct expander_device
*ex
= &dev
->ex_dev
;
969 struct domain_device
*child
;
970 u8 sub_addr
[8] = {0, };
972 list_for_each_entry(child
, &ex
->children
, siblings
) {
973 if (child
->dev_type
!= EDGE_DEV
&&
974 child
->dev_type
!= FANOUT_DEV
)
976 if (sub_addr
[0] == 0) {
977 sas_find_sub_addr(child
, sub_addr
);
982 if (sas_find_sub_addr(child
, s2
) &&
983 (SAS_ADDR(sub_addr
) != SAS_ADDR(s2
))) {
985 SAS_DPRINTK("ex %016llx->%016llx-?->%016llx "
986 "diverges from subtractive "
987 "boundary %016llx\n",
988 SAS_ADDR(dev
->sas_addr
),
989 SAS_ADDR(child
->sas_addr
),
993 sas_ex_disable_port(child
, s2
);
1000 * sas_ex_discover_devices -- discover devices attached to this expander
1001 * dev: pointer to the expander domain device
1002 * single: if you want to do a single phy, else set to -1;
1004 * Configure this expander for use with its devices and register the
1005 * devices of this expander.
1007 static int sas_ex_discover_devices(struct domain_device
*dev
, int single
)
1009 struct expander_device
*ex
= &dev
->ex_dev
;
1010 int i
= 0, end
= ex
->num_phys
;
1013 if (0 <= single
&& single
< end
) {
1018 for ( ; i
< end
; i
++) {
1019 struct ex_phy
*ex_phy
= &ex
->ex_phy
[i
];
1021 if (ex_phy
->phy_state
== PHY_VACANT
||
1022 ex_phy
->phy_state
== PHY_NOT_PRESENT
||
1023 ex_phy
->phy_state
== PHY_DEVICE_DISCOVERED
)
1026 switch (ex_phy
->linkrate
) {
1027 case SAS_PHY_DISABLED
:
1028 case SAS_PHY_RESET_PROBLEM
:
1029 case SAS_SATA_PORT_SELECTOR
:
1032 res
= sas_ex_discover_dev(dev
, i
);
1040 sas_check_level_subtractive_boundary(dev
);
1045 static int sas_check_ex_subtractive_boundary(struct domain_device
*dev
)
1047 struct expander_device
*ex
= &dev
->ex_dev
;
1049 u8
*sub_sas_addr
= NULL
;
1051 if (dev
->dev_type
!= EDGE_DEV
)
1054 for (i
= 0; i
< ex
->num_phys
; i
++) {
1055 struct ex_phy
*phy
= &ex
->ex_phy
[i
];
1057 if (phy
->phy_state
== PHY_VACANT
||
1058 phy
->phy_state
== PHY_NOT_PRESENT
)
1061 if ((phy
->attached_dev_type
== FANOUT_DEV
||
1062 phy
->attached_dev_type
== EDGE_DEV
) &&
1063 phy
->routing_attr
== SUBTRACTIVE_ROUTING
) {
1066 sub_sas_addr
= &phy
->attached_sas_addr
[0];
1067 else if (SAS_ADDR(sub_sas_addr
) !=
1068 SAS_ADDR(phy
->attached_sas_addr
)) {
1070 SAS_DPRINTK("ex %016llx phy 0x%x "
1071 "diverges(%016llx) on subtractive "
1072 "boundary(%016llx). Disabled\n",
1073 SAS_ADDR(dev
->sas_addr
), i
,
1074 SAS_ADDR(phy
->attached_sas_addr
),
1075 SAS_ADDR(sub_sas_addr
));
1076 sas_ex_disable_phy(dev
, i
);
1083 static void sas_print_parent_topology_bug(struct domain_device
*child
,
1084 struct ex_phy
*parent_phy
,
1085 struct ex_phy
*child_phy
)
1087 static const char ra_char
[] = {
1088 [DIRECT_ROUTING
] = 'D',
1089 [SUBTRACTIVE_ROUTING
] = 'S',
1090 [TABLE_ROUTING
] = 'T',
1092 static const char *ex_type
[] = {
1093 [EDGE_DEV
] = "edge",
1094 [FANOUT_DEV
] = "fanout",
1096 struct domain_device
*parent
= child
->parent
;
1098 sas_printk("%s ex %016llx phy 0x%x <--> %s ex %016llx phy 0x%x "
1099 "has %c:%c routing link!\n",
1101 ex_type
[parent
->dev_type
],
1102 SAS_ADDR(parent
->sas_addr
),
1105 ex_type
[child
->dev_type
],
1106 SAS_ADDR(child
->sas_addr
),
1109 ra_char
[parent_phy
->routing_attr
],
1110 ra_char
[child_phy
->routing_attr
]);
1113 static int sas_check_eeds(struct domain_device
*child
,
1114 struct ex_phy
*parent_phy
,
1115 struct ex_phy
*child_phy
)
1118 struct domain_device
*parent
= child
->parent
;
1120 if (SAS_ADDR(parent
->port
->disc
.fanout_sas_addr
) != 0) {
1122 SAS_DPRINTK("edge ex %016llx phy S:0x%x <--> edge ex %016llx "
1123 "phy S:0x%x, while there is a fanout ex %016llx\n",
1124 SAS_ADDR(parent
->sas_addr
),
1126 SAS_ADDR(child
->sas_addr
),
1128 SAS_ADDR(parent
->port
->disc
.fanout_sas_addr
));
1129 } else if (SAS_ADDR(parent
->port
->disc
.eeds_a
) == 0) {
1130 memcpy(parent
->port
->disc
.eeds_a
, parent
->sas_addr
,
1132 memcpy(parent
->port
->disc
.eeds_b
, child
->sas_addr
,
1134 } else if (((SAS_ADDR(parent
->port
->disc
.eeds_a
) ==
1135 SAS_ADDR(parent
->sas_addr
)) ||
1136 (SAS_ADDR(parent
->port
->disc
.eeds_a
) ==
1137 SAS_ADDR(child
->sas_addr
)))
1139 ((SAS_ADDR(parent
->port
->disc
.eeds_b
) ==
1140 SAS_ADDR(parent
->sas_addr
)) ||
1141 (SAS_ADDR(parent
->port
->disc
.eeds_b
) ==
1142 SAS_ADDR(child
->sas_addr
))))
1146 SAS_DPRINTK("edge ex %016llx phy 0x%x <--> edge ex %016llx "
1147 "phy 0x%x link forms a third EEDS!\n",
1148 SAS_ADDR(parent
->sas_addr
),
1150 SAS_ADDR(child
->sas_addr
),
1157 /* Here we spill over 80 columns. It is intentional.
1159 static int sas_check_parent_topology(struct domain_device
*child
)
1161 struct expander_device
*child_ex
= &child
->ex_dev
;
1162 struct expander_device
*parent_ex
;
1169 if (child
->parent
->dev_type
!= EDGE_DEV
&&
1170 child
->parent
->dev_type
!= FANOUT_DEV
)
1173 parent_ex
= &child
->parent
->ex_dev
;
1175 for (i
= 0; i
< parent_ex
->num_phys
; i
++) {
1176 struct ex_phy
*parent_phy
= &parent_ex
->ex_phy
[i
];
1177 struct ex_phy
*child_phy
;
1179 if (parent_phy
->phy_state
== PHY_VACANT
||
1180 parent_phy
->phy_state
== PHY_NOT_PRESENT
)
1183 if (SAS_ADDR(parent_phy
->attached_sas_addr
) != SAS_ADDR(child
->sas_addr
))
1186 child_phy
= &child_ex
->ex_phy
[parent_phy
->attached_phy_id
];
1188 switch (child
->parent
->dev_type
) {
1190 if (child
->dev_type
== FANOUT_DEV
) {
1191 if (parent_phy
->routing_attr
!= SUBTRACTIVE_ROUTING
||
1192 child_phy
->routing_attr
!= TABLE_ROUTING
) {
1193 sas_print_parent_topology_bug(child
, parent_phy
, child_phy
);
1196 } else if (parent_phy
->routing_attr
== SUBTRACTIVE_ROUTING
) {
1197 if (child_phy
->routing_attr
== SUBTRACTIVE_ROUTING
) {
1198 res
= sas_check_eeds(child
, parent_phy
, child_phy
);
1199 } else if (child_phy
->routing_attr
!= TABLE_ROUTING
) {
1200 sas_print_parent_topology_bug(child
, parent_phy
, child_phy
);
1203 } else if (parent_phy
->routing_attr
== TABLE_ROUTING
&&
1204 child_phy
->routing_attr
!= SUBTRACTIVE_ROUTING
) {
1205 sas_print_parent_topology_bug(child
, parent_phy
, child_phy
);
1210 if (parent_phy
->routing_attr
!= TABLE_ROUTING
||
1211 child_phy
->routing_attr
!= SUBTRACTIVE_ROUTING
) {
1212 sas_print_parent_topology_bug(child
, parent_phy
, child_phy
);
1224 #define RRI_REQ_SIZE 16
1225 #define RRI_RESP_SIZE 44
1227 static int sas_configure_present(struct domain_device
*dev
, int phy_id
,
1228 u8
*sas_addr
, int *index
, int *present
)
1231 struct expander_device
*ex
= &dev
->ex_dev
;
1232 struct ex_phy
*phy
= &ex
->ex_phy
[phy_id
];
1239 rri_req
= alloc_smp_req(RRI_REQ_SIZE
);
1243 rri_resp
= alloc_smp_resp(RRI_RESP_SIZE
);
1249 rri_req
[1] = SMP_REPORT_ROUTE_INFO
;
1250 rri_req
[9] = phy_id
;
1252 for (i
= 0; i
< ex
->max_route_indexes
; i
++) {
1253 *(__be16
*)(rri_req
+6) = cpu_to_be16(i
);
1254 res
= smp_execute_task(dev
, rri_req
, RRI_REQ_SIZE
, rri_resp
,
1259 if (res
== SMP_RESP_NO_INDEX
) {
1260 SAS_DPRINTK("overflow of indexes: dev %016llx "
1261 "phy 0x%x index 0x%x\n",
1262 SAS_ADDR(dev
->sas_addr
), phy_id
, i
);
1264 } else if (res
!= SMP_RESP_FUNC_ACC
) {
1265 SAS_DPRINTK("%s: dev %016llx phy 0x%x index 0x%x "
1266 "result 0x%x\n", __FUNCTION__
,
1267 SAS_ADDR(dev
->sas_addr
), phy_id
, i
, res
);
1270 if (SAS_ADDR(sas_addr
) != 0) {
1271 if (SAS_ADDR(rri_resp
+16) == SAS_ADDR(sas_addr
)) {
1273 if ((rri_resp
[12] & 0x80) == 0x80)
1278 } else if (SAS_ADDR(rri_resp
+16) == 0) {
1283 } else if (SAS_ADDR(rri_resp
+16) == 0 &&
1284 phy
->last_da_index
< i
) {
1285 phy
->last_da_index
= i
;
1298 #define CRI_REQ_SIZE 44
1299 #define CRI_RESP_SIZE 8
1301 static int sas_configure_set(struct domain_device
*dev
, int phy_id
,
1302 u8
*sas_addr
, int index
, int include
)
1308 cri_req
= alloc_smp_req(CRI_REQ_SIZE
);
1312 cri_resp
= alloc_smp_resp(CRI_RESP_SIZE
);
1318 cri_req
[1] = SMP_CONF_ROUTE_INFO
;
1319 *(__be16
*)(cri_req
+6) = cpu_to_be16(index
);
1320 cri_req
[9] = phy_id
;
1321 if (SAS_ADDR(sas_addr
) == 0 || !include
)
1322 cri_req
[12] |= 0x80;
1323 memcpy(cri_req
+16, sas_addr
, SAS_ADDR_SIZE
);
1325 res
= smp_execute_task(dev
, cri_req
, CRI_REQ_SIZE
, cri_resp
,
1330 if (res
== SMP_RESP_NO_INDEX
) {
1331 SAS_DPRINTK("overflow of indexes: dev %016llx phy 0x%x "
1333 SAS_ADDR(dev
->sas_addr
), phy_id
, index
);
1341 static int sas_configure_phy(struct domain_device
*dev
, int phy_id
,
1342 u8
*sas_addr
, int include
)
1348 res
= sas_configure_present(dev
, phy_id
, sas_addr
, &index
, &present
);
1351 if (include
^ present
)
1352 return sas_configure_set(dev
, phy_id
, sas_addr
, index
,include
);
1358 * sas_configure_parent -- configure routing table of parent
1359 * parent: parent expander
1360 * child: child expander
1361 * sas_addr: SAS port identifier of device directly attached to child
1363 static int sas_configure_parent(struct domain_device
*parent
,
1364 struct domain_device
*child
,
1365 u8
*sas_addr
, int include
)
1367 struct expander_device
*ex_parent
= &parent
->ex_dev
;
1371 if (parent
->parent
) {
1372 res
= sas_configure_parent(parent
->parent
, parent
, sas_addr
,
1378 if (ex_parent
->conf_route_table
== 0) {
1379 SAS_DPRINTK("ex %016llx has self-configuring routing table\n",
1380 SAS_ADDR(parent
->sas_addr
));
1384 for (i
= 0; i
< ex_parent
->num_phys
; i
++) {
1385 struct ex_phy
*phy
= &ex_parent
->ex_phy
[i
];
1387 if ((phy
->routing_attr
== TABLE_ROUTING
) &&
1388 (SAS_ADDR(phy
->attached_sas_addr
) ==
1389 SAS_ADDR(child
->sas_addr
))) {
1390 res
= sas_configure_phy(parent
, i
, sas_addr
, include
);
1400 * sas_configure_routing -- configure routing
1401 * dev: expander device
1402 * sas_addr: port identifier of device directly attached to the expander device
1404 static int sas_configure_routing(struct domain_device
*dev
, u8
*sas_addr
)
1407 return sas_configure_parent(dev
->parent
, dev
, sas_addr
, 1);
1411 static int sas_disable_routing(struct domain_device
*dev
, u8
*sas_addr
)
1414 return sas_configure_parent(dev
->parent
, dev
, sas_addr
, 0);
1419 #define SMP_BIN_ATTR_NAME "smp_portal"
1421 static void sas_ex_smp_hook(struct domain_device
*dev
)
1423 struct expander_device
*ex_dev
= &dev
->ex_dev
;
1424 struct bin_attribute
*bin_attr
= &ex_dev
->smp_bin_attr
;
1426 memset(bin_attr
, 0, sizeof(*bin_attr
));
1428 bin_attr
->attr
.name
= SMP_BIN_ATTR_NAME
;
1429 bin_attr
->attr
.mode
= 0600;
1432 bin_attr
->private = NULL
;
1433 bin_attr
->read
= smp_portal_read
;
1434 bin_attr
->write
= smp_portal_write
;
1435 bin_attr
->mmap
= NULL
;
1437 ex_dev
->smp_portal_pid
= -1;
1438 init_MUTEX(&ex_dev
->smp_sema
);
1443 * sas_discover_expander -- expander discovery
1444 * @ex: pointer to expander domain device
1446 * See comment in sas_discover_sata().
1448 static int sas_discover_expander(struct domain_device
*dev
)
1452 res
= sas_notify_lldd_dev_found(dev
);
1456 res
= sas_ex_general(dev
);
1459 res
= sas_ex_manuf_info(dev
);
1463 res
= sas_expander_discover(dev
);
1465 SAS_DPRINTK("expander %016llx discovery failed(0x%x)\n",
1466 SAS_ADDR(dev
->sas_addr
), res
);
1470 sas_check_ex_subtractive_boundary(dev
);
1471 res
= sas_check_parent_topology(dev
);
1476 sas_notify_lldd_dev_gone(dev
);
1480 static int sas_ex_level_discovery(struct asd_sas_port
*port
, const int level
)
1483 struct domain_device
*dev
;
1485 list_for_each_entry(dev
, &port
->dev_list
, dev_list_node
) {
1486 if (dev
->dev_type
== EDGE_DEV
||
1487 dev
->dev_type
== FANOUT_DEV
) {
1488 struct sas_expander_device
*ex
=
1489 rphy_to_expander_device(dev
->rphy
);
1491 if (level
== ex
->level
)
1492 res
= sas_ex_discover_devices(dev
, -1);
1494 res
= sas_ex_discover_devices(port
->port_dev
, -1);
1502 static int sas_ex_bfs_disc(struct asd_sas_port
*port
)
1508 level
= port
->disc
.max_level
;
1509 res
= sas_ex_level_discovery(port
, level
);
1511 } while (level
< port
->disc
.max_level
);
1516 int sas_discover_root_expander(struct domain_device
*dev
)
1519 struct sas_expander_device
*ex
= rphy_to_expander_device(dev
->rphy
);
1521 res
= sas_rphy_add(dev
->rphy
);
1525 ex
->level
= dev
->port
->disc
.max_level
; /* 0 */
1526 res
= sas_discover_expander(dev
);
1530 sas_ex_bfs_disc(dev
->port
);
1535 sas_rphy_remove(dev
->rphy
);
1540 /* ---------- Domain revalidation ---------- */
1542 static int sas_get_phy_discover(struct domain_device
*dev
,
1543 int phy_id
, struct smp_resp
*disc_resp
)
1548 disc_req
= alloc_smp_req(DISCOVER_REQ_SIZE
);
1552 disc_req
[1] = SMP_DISCOVER
;
1553 disc_req
[9] = phy_id
;
1555 res
= smp_execute_task(dev
, disc_req
, DISCOVER_REQ_SIZE
,
1556 disc_resp
, DISCOVER_RESP_SIZE
);
1559 else if (disc_resp
->result
!= SMP_RESP_FUNC_ACC
) {
1560 res
= disc_resp
->result
;
1568 static int sas_get_phy_change_count(struct domain_device
*dev
,
1569 int phy_id
, int *pcc
)
1572 struct smp_resp
*disc_resp
;
1574 disc_resp
= alloc_smp_resp(DISCOVER_RESP_SIZE
);
1578 res
= sas_get_phy_discover(dev
, phy_id
, disc_resp
);
1580 *pcc
= disc_resp
->disc
.change_count
;
1586 static int sas_get_phy_attached_sas_addr(struct domain_device
*dev
,
1587 int phy_id
, u8
*attached_sas_addr
)
1590 struct smp_resp
*disc_resp
;
1591 struct discover_resp
*dr
;
1593 disc_resp
= alloc_smp_resp(DISCOVER_RESP_SIZE
);
1596 dr
= &disc_resp
->disc
;
1598 res
= sas_get_phy_discover(dev
, phy_id
, disc_resp
);
1600 memcpy(attached_sas_addr
,disc_resp
->disc
.attached_sas_addr
,8);
1601 if (dr
->attached_dev_type
== 0)
1602 memset(attached_sas_addr
, 0, 8);
1608 static int sas_find_bcast_phy(struct domain_device
*dev
, int *phy_id
,
1611 struct expander_device
*ex
= &dev
->ex_dev
;
1615 for (i
= from_phy
; i
< ex
->num_phys
; i
++) {
1616 int phy_change_count
= 0;
1618 res
= sas_get_phy_change_count(dev
, i
, &phy_change_count
);
1621 else if (phy_change_count
!= ex
->ex_phy
[i
].phy_change_count
) {
1622 ex
->ex_phy
[i
].phy_change_count
= phy_change_count
;
1631 static int sas_get_ex_change_count(struct domain_device
*dev
, int *ecc
)
1635 struct smp_resp
*rg_resp
;
1637 rg_req
= alloc_smp_req(RG_REQ_SIZE
);
1641 rg_resp
= alloc_smp_resp(RG_RESP_SIZE
);
1647 rg_req
[1] = SMP_REPORT_GENERAL
;
1649 res
= smp_execute_task(dev
, rg_req
, RG_REQ_SIZE
, rg_resp
,
1653 if (rg_resp
->result
!= SMP_RESP_FUNC_ACC
) {
1654 res
= rg_resp
->result
;
1658 *ecc
= be16_to_cpu(rg_resp
->rg
.change_count
);
1665 static int sas_find_bcast_dev(struct domain_device
*dev
,
1666 struct domain_device
**src_dev
)
1668 struct expander_device
*ex
= &dev
->ex_dev
;
1669 int ex_change_count
= -1;
1672 res
= sas_get_ex_change_count(dev
, &ex_change_count
);
1675 if (ex_change_count
!= -1 &&
1676 ex_change_count
!= ex
->ex_change_count
) {
1678 ex
->ex_change_count
= ex_change_count
;
1680 struct domain_device
*ch
;
1682 list_for_each_entry(ch
, &ex
->children
, siblings
) {
1683 if (ch
->dev_type
== EDGE_DEV
||
1684 ch
->dev_type
== FANOUT_DEV
) {
1685 res
= sas_find_bcast_dev(ch
, src_dev
);
1695 static void sas_unregister_ex_tree(struct domain_device
*dev
)
1697 struct expander_device
*ex
= &dev
->ex_dev
;
1698 struct domain_device
*child
, *n
;
1700 list_for_each_entry_safe(child
, n
, &ex
->children
, siblings
) {
1701 if (child
->dev_type
== EDGE_DEV
||
1702 child
->dev_type
== FANOUT_DEV
)
1703 sas_unregister_ex_tree(child
);
1705 sas_unregister_dev(child
);
1707 sas_unregister_dev(dev
);
1710 static void sas_unregister_devs_sas_addr(struct domain_device
*parent
,
1713 struct expander_device
*ex_dev
= &parent
->ex_dev
;
1714 struct ex_phy
*phy
= &ex_dev
->ex_phy
[phy_id
];
1715 struct domain_device
*child
, *n
;
1717 list_for_each_entry_safe(child
, n
, &ex_dev
->children
, siblings
) {
1718 if (SAS_ADDR(child
->sas_addr
) ==
1719 SAS_ADDR(phy
->attached_sas_addr
)) {
1720 if (child
->dev_type
== EDGE_DEV
||
1721 child
->dev_type
== FANOUT_DEV
)
1722 sas_unregister_ex_tree(child
);
1724 sas_unregister_dev(child
);
1728 sas_disable_routing(parent
, phy
->attached_sas_addr
);
1729 memset(phy
->attached_sas_addr
, 0, SAS_ADDR_SIZE
);
1730 sas_port_delete_phy(phy
->port
, phy
->phy
);
1731 if (phy
->port
->num_phys
== 0)
1732 sas_port_delete(phy
->port
);
1736 static int sas_discover_bfs_by_root_level(struct domain_device
*root
,
1739 struct expander_device
*ex_root
= &root
->ex_dev
;
1740 struct domain_device
*child
;
1743 list_for_each_entry(child
, &ex_root
->children
, siblings
) {
1744 if (child
->dev_type
== EDGE_DEV
||
1745 child
->dev_type
== FANOUT_DEV
) {
1746 struct sas_expander_device
*ex
=
1747 rphy_to_expander_device(child
->rphy
);
1749 if (level
> ex
->level
)
1750 res
= sas_discover_bfs_by_root_level(child
,
1752 else if (level
== ex
->level
)
1753 res
= sas_ex_discover_devices(child
, -1);
1759 static int sas_discover_bfs_by_root(struct domain_device
*dev
)
1762 struct sas_expander_device
*ex
= rphy_to_expander_device(dev
->rphy
);
1763 int level
= ex
->level
+1;
1765 res
= sas_ex_discover_devices(dev
, -1);
1769 res
= sas_discover_bfs_by_root_level(dev
, level
);
1772 } while (level
<= dev
->port
->disc
.max_level
);
1777 static int sas_discover_new(struct domain_device
*dev
, int phy_id
)
1779 struct ex_phy
*ex_phy
= &dev
->ex_dev
.ex_phy
[phy_id
];
1780 struct domain_device
*child
;
1783 SAS_DPRINTK("ex %016llx phy%d new device attached\n",
1784 SAS_ADDR(dev
->sas_addr
), phy_id
);
1785 res
= sas_ex_phy_discover(dev
, phy_id
);
1788 res
= sas_ex_discover_devices(dev
, phy_id
);
1791 list_for_each_entry(child
, &dev
->ex_dev
.children
, siblings
) {
1792 if (SAS_ADDR(child
->sas_addr
) ==
1793 SAS_ADDR(ex_phy
->attached_sas_addr
)) {
1794 if (child
->dev_type
== EDGE_DEV
||
1795 child
->dev_type
== FANOUT_DEV
)
1796 res
= sas_discover_bfs_by_root(child
);
1804 static int sas_rediscover_dev(struct domain_device
*dev
, int phy_id
)
1806 struct expander_device
*ex
= &dev
->ex_dev
;
1807 struct ex_phy
*phy
= &ex
->ex_phy
[phy_id
];
1808 u8 attached_sas_addr
[8];
1811 res
= sas_get_phy_attached_sas_addr(dev
, phy_id
, attached_sas_addr
);
1813 case SMP_RESP_NO_PHY
:
1814 phy
->phy_state
= PHY_NOT_PRESENT
;
1815 sas_unregister_devs_sas_addr(dev
, phy_id
);
1817 case SMP_RESP_PHY_VACANT
:
1818 phy
->phy_state
= PHY_VACANT
;
1819 sas_unregister_devs_sas_addr(dev
, phy_id
);
1821 case SMP_RESP_FUNC_ACC
:
1825 if (SAS_ADDR(attached_sas_addr
) == 0) {
1826 phy
->phy_state
= PHY_EMPTY
;
1827 sas_unregister_devs_sas_addr(dev
, phy_id
);
1828 } else if (SAS_ADDR(attached_sas_addr
) ==
1829 SAS_ADDR(phy
->attached_sas_addr
)) {
1830 SAS_DPRINTK("ex %016llx phy 0x%x broadcast flutter\n",
1831 SAS_ADDR(dev
->sas_addr
), phy_id
);
1832 sas_ex_phy_discover(dev
, phy_id
);
1834 res
= sas_discover_new(dev
, phy_id
);
1839 static int sas_rediscover(struct domain_device
*dev
, const int phy_id
)
1841 struct expander_device
*ex
= &dev
->ex_dev
;
1842 struct ex_phy
*changed_phy
= &ex
->ex_phy
[phy_id
];
1846 SAS_DPRINTK("ex %016llx phy%d originated BROADCAST(CHANGE)\n",
1847 SAS_ADDR(dev
->sas_addr
), phy_id
);
1849 if (SAS_ADDR(changed_phy
->attached_sas_addr
) != 0) {
1850 for (i
= 0; i
< ex
->num_phys
; i
++) {
1851 struct ex_phy
*phy
= &ex
->ex_phy
[i
];
1855 if (SAS_ADDR(phy
->attached_sas_addr
) ==
1856 SAS_ADDR(changed_phy
->attached_sas_addr
)) {
1857 SAS_DPRINTK("phy%d part of wide port with "
1858 "phy%d\n", phy_id
, i
);
1862 res
= sas_rediscover_dev(dev
, phy_id
);
1864 res
= sas_discover_new(dev
, phy_id
);
1870 * sas_revalidate_domain -- revalidate the domain
1871 * @port: port to the domain of interest
1873 * NOTE: this process _must_ quit (return) as soon as any connection
1874 * errors are encountered. Connection recovery is done elsewhere.
1875 * Discover process only interrogates devices in order to discover the
1878 int sas_ex_revalidate_domain(struct domain_device
*port_dev
)
1881 struct domain_device
*dev
= NULL
;
1883 res
= sas_find_bcast_dev(port_dev
, &dev
);
1887 struct expander_device
*ex
= &dev
->ex_dev
;
1892 res
= sas_find_bcast_phy(dev
, &phy_id
, i
);
1895 res
= sas_rediscover(dev
, phy_id
);
1897 } while (i
< ex
->num_phys
);
1904 /* ---------- SMP portal ---------- */
1906 static ssize_t
smp_portal_write(struct kobject
*kobj
,
1907 struct bin_attribute
*bin_attr
,
1908 char *buf
, loff_t offs
, size_t size
)
1910 struct domain_device
*dev
= to_dom_device(kobj
);
1911 struct expander_device
*ex
= &dev
->ex_dev
;
1918 down_interruptible(&ex
->smp_sema
);
1921 ex
->smp_req
= kzalloc(size
, GFP_USER
);
1926 memcpy(ex
->smp_req
, buf
, size
);
1927 ex
->smp_req_size
= size
;
1928 ex
->smp_portal_pid
= current
->pid
;
1934 static ssize_t
smp_portal_read(struct kobject
*kobj
,
1935 struct bin_attribute
*bin_attr
,
1936 char *buf
, loff_t offs
, size_t size
)
1938 struct domain_device
*dev
= to_dom_device(kobj
);
1939 struct expander_device
*ex
= &dev
->ex_dev
;
1943 /* XXX: sysfs gives us an offset of 0x10 or 0x8 while in fact
1947 down_interruptible(&ex
->smp_sema
);
1948 if (!ex
->smp_req
|| ex
->smp_portal_pid
!= current
->pid
)
1956 smp_resp
= alloc_smp_resp(size
);
1959 res
= smp_execute_task(dev
, ex
->smp_req
, ex
->smp_req_size
,
1962 memcpy(buf
, smp_resp
, size
);
1970 ex
->smp_req_size
= 0;
1971 ex
->smp_portal_pid
= -1;
1977 int sas_smp_handler(struct Scsi_Host
*shost
, struct sas_rphy
*rphy
,
1978 struct request
*req
)
1980 struct domain_device
*dev
;
1981 int ret
, type
= rphy
->identify
.device_type
;
1982 struct request
*rsp
= req
->next_rq
;
1985 printk("%s: space for a smp response is missing\n",
1990 /* seems aic94xx doesn't support */
1992 printk("%s: can we send a smp request to a host?\n",
1997 if (type
!= SAS_EDGE_EXPANDER_DEVICE
&&
1998 type
!= SAS_FANOUT_EXPANDER_DEVICE
) {
1999 printk("%s: can we send a smp request to a device?\n",
2004 dev
= sas_find_dev_by_rphy(rphy
);
2006 printk("%s: fail to find a domain_device?\n", __FUNCTION__
);
2010 /* do we need to support multiple segments? */
2011 if (req
->bio
->bi_vcnt
> 1 || rsp
->bio
->bi_vcnt
> 1) {
2012 printk("%s: multiple segments req %u %u, rsp %u %u\n",
2013 __FUNCTION__
, req
->bio
->bi_vcnt
, req
->data_len
,
2014 rsp
->bio
->bi_vcnt
, rsp
->data_len
);
2018 ret
= smp_execute_task(dev
, bio_data(req
->bio
), req
->data_len
,
2019 bio_data(rsp
->bio
), rsp
->data_len
);