treewide: Replace GPLv2 boilerplate/reference with SPDX - rule 335
[linux-2.6-block.git] / drivers / scsi / libsas / sas_port.c
CommitLineData
2908d778
JB
1/*
2 * Serial Attached SCSI (SAS) Port class
3 *
4 * Copyright (C) 2005 Adaptec, Inc. All rights reserved.
5 * Copyright (C) 2005 Luben Tuikov <luben_tuikov@adaptec.com>
6 *
7 * This file is licensed under GPLv2.
8 *
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.
13 *
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.
18 *
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
22 *
23 */
24
25#include "sas_internal.h"
26
27#include <scsi/scsi_transport.h>
28#include <scsi/scsi_transport_sas.h>
29#include "../scsi_sas_internal.h"
30
00f0254e
DW
31static bool phy_is_wideport_member(struct asd_sas_port *port, struct asd_sas_phy *phy)
32{
33 struct sas_ha_struct *sas_ha = phy->ha;
34
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))
38 return false;
39 return true;
40}
41
303694ee
DW
42static void sas_resume_port(struct asd_sas_phy *phy)
43{
44 struct domain_device *dev;
45 struct asd_sas_port *port = phy->port;
46 struct sas_ha_struct *sas_ha = phy->ha;
47 struct sas_internal *si = to_sas_internal(sas_ha->core.shost->transportt);
48
49 if (si->dft->lldd_port_formed)
50 si->dft->lldd_port_formed(phy);
51
52 if (port->suspended)
53 port->suspended = 0;
54 else {
55 /* we only need to handle "link returned" actions once */
56 return;
57 }
58
59 /* if the port came back:
60 * 1/ presume every device came back
61 * 2/ force the next revalidation to check all expander phys
62 */
63 list_for_each_entry(dev, &port->dev_list, dev_list_node) {
64 int i, rc;
65
66 rc = sas_notify_lldd_dev_found(dev);
67 if (rc) {
68 sas_unregister_dev(port, dev);
0558f33c 69 sas_destruct_devices(port);
303694ee
DW
70 continue;
71 }
72
aa9f8328 73 if (dev->dev_type == SAS_EDGE_EXPANDER_DEVICE || dev->dev_type == SAS_FANOUT_EXPANDER_DEVICE) {
303694ee
DW
74 dev->ex_dev.ex_change_count = -1;
75 for (i = 0; i < dev->ex_dev.num_phys; i++) {
76 struct ex_phy *phy = &dev->ex_dev.ex_phy[i];
77
78 phy->phy_change_count = -1;
79 }
80 }
81 }
82
83 sas_discover_event(port, DISCE_RESUME);
84}
85
2908d778 86/**
121246ae 87 * sas_form_port - add this phy to a port
2908d778
JB
88 * @phy: the phy of interest
89 *
90 * This function adds this phy to an existing port, thus creating a wide
91 * port, or it creates a port and adds the phy to the port.
92 */
93static void sas_form_port(struct asd_sas_phy *phy)
94{
95 int i;
96 struct sas_ha_struct *sas_ha = phy->ha;
97 struct asd_sas_port *port = phy->port;
085f104a 98 struct domain_device *port_dev;
2908d778
JB
99 struct sas_internal *si =
100 to_sas_internal(sas_ha->core.shost->transportt);
980fa2f9 101 unsigned long flags;
2908d778
JB
102
103 if (port) {
00f0254e 104 if (!phy_is_wideport_member(port, phy))
90f1e10d 105 sas_deform_port(phy, 0);
303694ee
DW
106 else if (phy->suspended) {
107 phy->suspended = 0;
108 sas_resume_port(phy);
109
110 /* phy came back, try to cancel the timeout */
111 wake_up(&sas_ha->eh_wait_q);
112 return;
113 } else {
15ba7806
JG
114 pr_info("%s: phy%d belongs to port%d already(%d)!\n",
115 __func__, phy->id, phy->port->id,
116 phy->port->num_phys);
2908d778
JB
117 return;
118 }
119 }
120
5381837f 121 /* see if the phy should be part of a wide port */
980fa2f9 122 spin_lock_irqsave(&sas_ha->phy_port_lock, flags);
2908d778
JB
123 for (i = 0; i < sas_ha->num_phys; i++) {
124 port = sas_ha->sas_port[i];
125 spin_lock(&port->phy_list_lock);
126 if (*(u64 *) port->sas_addr &&
00f0254e 127 phy_is_wideport_member(port, phy) && port->num_phys > 0) {
2908d778 128 /* wide port */
15ba7806
JG
129 pr_debug("phy%d matched wide port%d\n", phy->id,
130 port->id);
2908d778 131 break;
2908d778
JB
132 }
133 spin_unlock(&port->phy_list_lock);
134 }
5381837f
TP
135 /* The phy does not match any existing port, create a new one */
136 if (i == sas_ha->num_phys) {
137 for (i = 0; i < sas_ha->num_phys; i++) {
138 port = sas_ha->sas_port[i];
139 spin_lock(&port->phy_list_lock);
140 if (*(u64 *)port->sas_addr == 0
141 && port->num_phys == 0) {
142 memcpy(port->sas_addr, phy->sas_addr,
143 SAS_ADDR_SIZE);
144 break;
145 }
146 spin_unlock(&port->phy_list_lock);
147 }
148 }
2908d778
JB
149
150 if (i >= sas_ha->num_phys) {
15ba7806 151 pr_err("%s: couldn't find a free port, bug?\n", __func__);
980fa2f9 152 spin_unlock_irqrestore(&sas_ha->phy_port_lock, flags);
2908d778
JB
153 return;
154 }
155
156 /* add the phy to the port */
085f104a 157 port_dev = port->port_dev;
2908d778 158 list_add_tail(&phy->port_phy_el, &port->phy_list);
085f104a 159 sas_phy_set_target(phy, port_dev);
2908d778
JB
160 phy->port = port;
161 port->num_phys++;
162 port->phy_mask |= (1U << phy->id);
163
2908d778
JB
164 if (*(u64 *)port->attached_sas_addr == 0) {
165 port->class = phy->class;
166 memcpy(port->attached_sas_addr, phy->attached_sas_addr,
167 SAS_ADDR_SIZE);
168 port->iproto = phy->iproto;
169 port->tproto = phy->tproto;
170 port->oob_mode = phy->oob_mode;
171 port->linkrate = phy->linkrate;
172 } else
173 port->linkrate = max(port->linkrate, phy->linkrate);
174 spin_unlock(&port->phy_list_lock);
980fa2f9 175 spin_unlock_irqrestore(&sas_ha->phy_port_lock, flags);
2908d778
JB
176
177 if (!port->port) {
b4698d88 178 port->port = sas_port_alloc(phy->phy->dev.parent, port->id);
2908d778
JB
179 BUG_ON(!port->port);
180 sas_port_add(port->port);
181 }
182 sas_port_add_phy(port->port, phy->phy);
183
15ba7806
JG
184 pr_debug("%s added to %s, phy_mask:0x%x (%16llx)\n",
185 dev_name(&phy->phy->dev), dev_name(&port->port->dev),
186 port->phy_mask,
187 SAS_ADDR(port->attached_sas_addr));
a29c0515 188
085f104a
JG
189 if (port_dev)
190 port_dev->pathways = port->num_phys;
2908d778
JB
191
192 /* Tell the LLDD about this port formation. */
193 if (si->dft->lldd_port_formed)
194 si->dft->lldd_port_formed(phy);
195
196 sas_discover_event(phy->port, DISCE_DISCOVER_DOMAIN);
085f104a
JG
197 /* Only insert a revalidate event after initial discovery */
198 if (port_dev && sas_dev_type_is_expander(port_dev->dev_type)) {
199 struct expander_device *ex_dev = &port_dev->ex_dev;
200
201 ex_dev->ex_change_count = -1;
202 sas_discover_event(port, DISCE_REVALIDATE_DOMAIN);
203 }
517e5153 204 flush_workqueue(sas_ha->disco_q);
2908d778
JB
205}
206
207/**
121246ae 208 * sas_deform_port - remove this phy from the port it belongs to
2908d778 209 * @phy: the phy of interest
121246ae 210 * @gone: whether or not the PHY is gone
2908d778
JB
211 *
212 * This is called when the physical link to the other phy has been
213 * lost (on this phy), in Event thread context. We cannot delay here.
214 */
90f1e10d 215void sas_deform_port(struct asd_sas_phy *phy, int gone)
2908d778
JB
216{
217 struct sas_ha_struct *sas_ha = phy->ha;
218 struct asd_sas_port *port = phy->port;
219 struct sas_internal *si =
220 to_sas_internal(sas_ha->core.shost->transportt);
90f1e10d 221 struct domain_device *dev;
980fa2f9 222 unsigned long flags;
2908d778
JB
223
224 if (!port)
225 return; /* done by a phy event */
226
90f1e10d
DW
227 dev = port->port_dev;
228 if (dev)
229 dev->pathways--;
2908d778
JB
230
231 if (port->num_phys == 1) {
7d05919a 232 sas_unregister_domain_devices(port, gone);
0558f33c 233 sas_destruct_devices(port);
2908d778
JB
234 sas_port_delete(port->port);
235 port->port = NULL;
f41a0c44 236 } else {
2908d778 237 sas_port_delete_phy(port->port, phy->phy);
f41a0c44
DW
238 sas_device_set_phy(dev, port->port);
239 }
2908d778 240
2908d778
JB
241 if (si->dft->lldd_port_deformed)
242 si->dft->lldd_port_deformed(phy);
243
980fa2f9 244 spin_lock_irqsave(&sas_ha->phy_port_lock, flags);
2908d778
JB
245 spin_lock(&port->phy_list_lock);
246
247 list_del_init(&phy->port_phy_el);
899fcf40 248 sas_phy_set_target(phy, NULL);
2908d778
JB
249 phy->port = NULL;
250 port->num_phys--;
251 port->phy_mask &= ~(1U << phy->id);
252
253 if (port->num_phys == 0) {
254 INIT_LIST_HEAD(&port->phy_list);
255 memset(port->sas_addr, 0, SAS_ADDR_SIZE);
256 memset(port->attached_sas_addr, 0, SAS_ADDR_SIZE);
257 port->class = 0;
258 port->iproto = 0;
259 port->tproto = 0;
260 port->oob_mode = 0;
261 port->phy_mask = 0;
262 }
263 spin_unlock(&port->phy_list_lock);
980fa2f9 264 spin_unlock_irqrestore(&sas_ha->phy_port_lock, flags);
2908d778 265
085f104a
JG
266 /* Only insert revalidate event if the port still has members */
267 if (port->port && dev && sas_dev_type_is_expander(dev->dev_type)) {
268 struct expander_device *ex_dev = &dev->ex_dev;
269
270 ex_dev->ex_change_count = -1;
271 sas_discover_event(port, DISCE_REVALIDATE_DOMAIN);
272 }
273 flush_workqueue(sas_ha->disco_q);
274
2908d778
JB
275 return;
276}
277
278/* ---------- SAS port events ---------- */
279
c4028958 280void sas_porte_bytes_dmaed(struct work_struct *work)
2908d778 281{
22b9153f 282 struct asd_sas_event *ev = to_asd_sas_event(work);
c4028958 283 struct asd_sas_phy *phy = ev->phy;
2908d778 284
2908d778
JB
285 sas_form_port(phy);
286}
287
c4028958 288void sas_porte_broadcast_rcvd(struct work_struct *work)
2908d778 289{
22b9153f 290 struct asd_sas_event *ev = to_asd_sas_event(work);
c4028958 291 struct asd_sas_phy *phy = ev->phy;
2908d778
JB
292 unsigned long flags;
293 u32 prim;
2908d778 294
2908d778
JB
295 spin_lock_irqsave(&phy->sas_prim_lock, flags);
296 prim = phy->sas_prim;
297 spin_unlock_irqrestore(&phy->sas_prim_lock, flags);
298
15ba7806 299 pr_debug("broadcast received: %d\n", prim);
2908d778 300 sas_discover_event(phy->port, DISCE_REVALIDATE_DOMAIN);
517e5153
JY
301
302 if (phy->port)
303 flush_workqueue(phy->port->ha->disco_q);
2908d778
JB
304}
305
c4028958 306void sas_porte_link_reset_err(struct work_struct *work)
2908d778 307{
22b9153f 308 struct asd_sas_event *ev = to_asd_sas_event(work);
c4028958 309 struct asd_sas_phy *phy = ev->phy;
2908d778 310
90f1e10d 311 sas_deform_port(phy, 1);
2908d778
JB
312}
313
c4028958 314void sas_porte_timer_event(struct work_struct *work)
2908d778 315{
22b9153f 316 struct asd_sas_event *ev = to_asd_sas_event(work);
c4028958 317 struct asd_sas_phy *phy = ev->phy;
2908d778 318
90f1e10d 319 sas_deform_port(phy, 1);
2908d778
JB
320}
321
c4028958 322void sas_porte_hard_reset(struct work_struct *work)
2908d778 323{
22b9153f 324 struct asd_sas_event *ev = to_asd_sas_event(work);
c4028958 325 struct asd_sas_phy *phy = ev->phy;
2908d778 326
90f1e10d 327 sas_deform_port(phy, 1);
2908d778
JB
328}
329
330/* ---------- SAS port registration ---------- */
331
332static void sas_init_port(struct asd_sas_port *port,
333 struct sas_ha_struct *sas_ha, int i)
334{
a29c0515 335 memset(port, 0, sizeof(*port));
2908d778
JB
336 port->id = i;
337 INIT_LIST_HEAD(&port->dev_list);
87c8331f
DW
338 INIT_LIST_HEAD(&port->disco_list);
339 INIT_LIST_HEAD(&port->destroy_list);
0558f33c 340 INIT_LIST_HEAD(&port->sas_port_del_list);
2908d778
JB
341 spin_lock_init(&port->phy_list_lock);
342 INIT_LIST_HEAD(&port->phy_list);
2908d778
JB
343 port->ha = sas_ha;
344
345 spin_lock_init(&port->dev_list_lock);
346}
347
348int sas_register_ports(struct sas_ha_struct *sas_ha)
349{
350 int i;
351
352 /* initialize the ports and discovery */
353 for (i = 0; i < sas_ha->num_phys; i++) {
354 struct asd_sas_port *port = sas_ha->sas_port[i];
355
356 sas_init_port(port, sas_ha, i);
357 sas_init_disc(&port->disc, port);
358 }
359 return 0;
360}
361
362void sas_unregister_ports(struct sas_ha_struct *sas_ha)
363{
364 int i;
365
366 for (i = 0; i < sas_ha->num_phys; i++)
367 if (sas_ha->sas_phy[i]->port)
90f1e10d 368 sas_deform_port(sas_ha->sas_phy[i], 0);
2908d778
JB
369
370}
1c393b97
JY
371
372const work_func_t sas_port_event_fns[PORT_NUM_EVENTS] = {
373 [PORTE_BYTES_DMAED] = sas_porte_bytes_dmaed,
374 [PORTE_BROADCAST_RCVD] = sas_porte_broadcast_rcvd,
375 [PORTE_LINK_RESET_ERR] = sas_porte_link_reset_err,
376 [PORTE_TIMER_EVENT] = sas_porte_timer_event,
377 [PORTE_HARD_RESET] = sas_porte_hard_reset,
378};