2 * Serial Attached SCSI (SAS) Port class
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 "sas_internal.h"
27 #include <scsi/scsi_transport.h>
28 #include <scsi/scsi_transport_sas.h>
29 #include "../scsi_sas_internal.h"
31 static bool phy_is_wideport_member(struct asd_sas_port
*port
, struct asd_sas_phy
*phy
)
33 struct sas_ha_struct
*sas_ha
= phy
->ha
;
35 if (memcmp(port
->attached_sas_addr
, phy
->attached_sas_addr
,
36 SAS_ADDR_SIZE
) != 0 || (sas_ha
->strict_wide_ports
&&
37 memcmp(port
->sas_addr
, phy
->sas_addr
, SAS_ADDR_SIZE
) != 0))
43 * sas_form_port -- add this phy to a port
44 * @phy: the phy of interest
46 * This function adds this phy to an existing port, thus creating a wide
47 * port, or it creates a port and adds the phy to the port.
49 static void sas_form_port(struct asd_sas_phy
*phy
)
52 struct sas_ha_struct
*sas_ha
= phy
->ha
;
53 struct asd_sas_port
*port
= phy
->port
;
54 struct sas_internal
*si
=
55 to_sas_internal(sas_ha
->core
.shost
->transportt
);
59 if (!phy_is_wideport_member(port
, phy
))
60 sas_deform_port(phy
, 0);
62 SAS_DPRINTK("%s: phy%d belongs to port%d already(%d)!\n",
63 __func__
, phy
->id
, phy
->port
->id
,
69 /* see if the phy should be part of a wide port */
70 spin_lock_irqsave(&sas_ha
->phy_port_lock
, flags
);
71 for (i
= 0; i
< sas_ha
->num_phys
; i
++) {
72 port
= sas_ha
->sas_port
[i
];
73 spin_lock(&port
->phy_list_lock
);
74 if (*(u64
*) port
->sas_addr
&&
75 phy_is_wideport_member(port
, phy
) && port
->num_phys
> 0) {
77 SAS_DPRINTK("phy%d matched wide port%d\n", phy
->id
,
81 spin_unlock(&port
->phy_list_lock
);
83 /* The phy does not match any existing port, create a new one */
84 if (i
== sas_ha
->num_phys
) {
85 for (i
= 0; i
< sas_ha
->num_phys
; i
++) {
86 port
= sas_ha
->sas_port
[i
];
87 spin_lock(&port
->phy_list_lock
);
88 if (*(u64
*)port
->sas_addr
== 0
89 && port
->num_phys
== 0) {
90 memcpy(port
->sas_addr
, phy
->sas_addr
,
94 spin_unlock(&port
->phy_list_lock
);
98 if (i
>= sas_ha
->num_phys
) {
99 printk(KERN_NOTICE
"%s: couldn't find a free port, bug?\n",
101 spin_unlock_irqrestore(&sas_ha
->phy_port_lock
, flags
);
105 /* add the phy to the port */
106 list_add_tail(&phy
->port_phy_el
, &port
->phy_list
);
109 port
->phy_mask
|= (1U << phy
->id
);
111 if (*(u64
*)port
->attached_sas_addr
== 0) {
112 port
->class = phy
->class;
113 memcpy(port
->attached_sas_addr
, phy
->attached_sas_addr
,
115 port
->iproto
= phy
->iproto
;
116 port
->tproto
= phy
->tproto
;
117 port
->oob_mode
= phy
->oob_mode
;
118 port
->linkrate
= phy
->linkrate
;
120 port
->linkrate
= max(port
->linkrate
, phy
->linkrate
);
121 spin_unlock(&port
->phy_list_lock
);
122 spin_unlock_irqrestore(&sas_ha
->phy_port_lock
, flags
);
125 port
->port
= sas_port_alloc(phy
->phy
->dev
.parent
, port
->id
);
127 sas_port_add(port
->port
);
129 sas_port_add_phy(port
->port
, phy
->phy
);
131 SAS_DPRINTK("%s added to %s, phy_mask:0x%x (%16llx)\n",
132 dev_name(&phy
->phy
->dev
), dev_name(&port
->port
->dev
),
134 SAS_ADDR(port
->attached_sas_addr
));
137 port
->port_dev
->pathways
= port
->num_phys
;
139 /* Tell the LLDD about this port formation. */
140 if (si
->dft
->lldd_port_formed
)
141 si
->dft
->lldd_port_formed(phy
);
143 sas_discover_event(phy
->port
, DISCE_DISCOVER_DOMAIN
);
147 * sas_deform_port -- remove this phy from the port it belongs to
148 * @phy: the phy of interest
150 * This is called when the physical link to the other phy has been
151 * lost (on this phy), in Event thread context. We cannot delay here.
153 void sas_deform_port(struct asd_sas_phy
*phy
, int gone
)
155 struct sas_ha_struct
*sas_ha
= phy
->ha
;
156 struct asd_sas_port
*port
= phy
->port
;
157 struct sas_internal
*si
=
158 to_sas_internal(sas_ha
->core
.shost
->transportt
);
159 struct domain_device
*dev
;
163 return; /* done by a phy event */
165 dev
= port
->port_dev
;
169 if (port
->num_phys
== 1) {
170 sas_unregister_domain_devices(port
, gone
);
171 sas_port_delete(port
->port
);
174 sas_port_delete_phy(port
->port
, phy
->phy
);
175 sas_device_set_phy(dev
, port
->port
);
178 if (si
->dft
->lldd_port_deformed
)
179 si
->dft
->lldd_port_deformed(phy
);
181 spin_lock_irqsave(&sas_ha
->phy_port_lock
, flags
);
182 spin_lock(&port
->phy_list_lock
);
184 list_del_init(&phy
->port_phy_el
);
187 port
->phy_mask
&= ~(1U << phy
->id
);
189 if (port
->num_phys
== 0) {
190 INIT_LIST_HEAD(&port
->phy_list
);
191 memset(port
->sas_addr
, 0, SAS_ADDR_SIZE
);
192 memset(port
->attached_sas_addr
, 0, SAS_ADDR_SIZE
);
199 spin_unlock(&port
->phy_list_lock
);
200 spin_unlock_irqrestore(&sas_ha
->phy_port_lock
, flags
);
205 /* ---------- SAS port events ---------- */
207 void sas_porte_bytes_dmaed(struct work_struct
*work
)
209 struct asd_sas_event
*ev
=
210 container_of(work
, struct asd_sas_event
, work
);
211 struct asd_sas_phy
*phy
= ev
->phy
;
213 clear_bit(PORTE_BYTES_DMAED
, &phy
->port_events_pending
);
218 void sas_porte_broadcast_rcvd(struct work_struct
*work
)
220 struct asd_sas_event
*ev
=
221 container_of(work
, struct asd_sas_event
, work
);
222 struct asd_sas_phy
*phy
= ev
->phy
;
226 clear_bit(PORTE_BROADCAST_RCVD
, &phy
->port_events_pending
);
228 spin_lock_irqsave(&phy
->sas_prim_lock
, flags
);
229 prim
= phy
->sas_prim
;
230 spin_unlock_irqrestore(&phy
->sas_prim_lock
, flags
);
232 SAS_DPRINTK("broadcast received: %d\n", prim
);
233 sas_discover_event(phy
->port
, DISCE_REVALIDATE_DOMAIN
);
236 void sas_porte_link_reset_err(struct work_struct
*work
)
238 struct asd_sas_event
*ev
=
239 container_of(work
, struct asd_sas_event
, work
);
240 struct asd_sas_phy
*phy
= ev
->phy
;
242 clear_bit(PORTE_LINK_RESET_ERR
, &phy
->port_events_pending
);
244 sas_deform_port(phy
, 1);
247 void sas_porte_timer_event(struct work_struct
*work
)
249 struct asd_sas_event
*ev
=
250 container_of(work
, struct asd_sas_event
, work
);
251 struct asd_sas_phy
*phy
= ev
->phy
;
253 clear_bit(PORTE_TIMER_EVENT
, &phy
->port_events_pending
);
255 sas_deform_port(phy
, 1);
258 void sas_porte_hard_reset(struct work_struct
*work
)
260 struct asd_sas_event
*ev
=
261 container_of(work
, struct asd_sas_event
, work
);
262 struct asd_sas_phy
*phy
= ev
->phy
;
264 clear_bit(PORTE_HARD_RESET
, &phy
->port_events_pending
);
266 sas_deform_port(phy
, 1);
269 /* ---------- SAS port registration ---------- */
271 static void sas_init_port(struct asd_sas_port
*port
,
272 struct sas_ha_struct
*sas_ha
, int i
)
274 memset(port
, 0, sizeof(*port
));
276 INIT_LIST_HEAD(&port
->dev_list
);
277 INIT_LIST_HEAD(&port
->disco_list
);
278 INIT_LIST_HEAD(&port
->destroy_list
);
279 spin_lock_init(&port
->phy_list_lock
);
280 INIT_LIST_HEAD(&port
->phy_list
);
283 spin_lock_init(&port
->dev_list_lock
);
286 int sas_register_ports(struct sas_ha_struct
*sas_ha
)
290 /* initialize the ports and discovery */
291 for (i
= 0; i
< sas_ha
->num_phys
; i
++) {
292 struct asd_sas_port
*port
= sas_ha
->sas_port
[i
];
294 sas_init_port(port
, sas_ha
, i
);
295 sas_init_disc(&port
->disc
, port
);
300 void sas_unregister_ports(struct sas_ha_struct
*sas_ha
)
304 for (i
= 0; i
< sas_ha
->num_phys
; i
++)
305 if (sas_ha
->sas_phy
[i
]->port
)
306 sas_deform_port(sas_ha
->sas_phy
[i
], 0);