Commit | Line | Data |
---|---|---|
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 |
31 | static 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 |
42 | static 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); | |
69 | continue; | |
70 | } | |
71 | ||
aa9f8328 | 72 | if (dev->dev_type == SAS_EDGE_EXPANDER_DEVICE || dev->dev_type == SAS_FANOUT_EXPANDER_DEVICE) { |
303694ee DW |
73 | dev->ex_dev.ex_change_count = -1; |
74 | for (i = 0; i < dev->ex_dev.num_phys; i++) { | |
75 | struct ex_phy *phy = &dev->ex_dev.ex_phy[i]; | |
76 | ||
77 | phy->phy_change_count = -1; | |
78 | } | |
79 | } | |
80 | } | |
81 | ||
82 | sas_discover_event(port, DISCE_RESUME); | |
83 | } | |
84 | ||
2908d778 JB |
85 | /** |
86 | * sas_form_port -- add this phy to a port | |
87 | * @phy: the phy of interest | |
88 | * | |
89 | * This function adds this phy to an existing port, thus creating a wide | |
90 | * port, or it creates a port and adds the phy to the port. | |
91 | */ | |
92 | static void sas_form_port(struct asd_sas_phy *phy) | |
93 | { | |
94 | int i; | |
95 | struct sas_ha_struct *sas_ha = phy->ha; | |
96 | struct asd_sas_port *port = phy->port; | |
97 | struct sas_internal *si = | |
98 | to_sas_internal(sas_ha->core.shost->transportt); | |
980fa2f9 | 99 | unsigned long flags; |
2908d778 JB |
100 | |
101 | if (port) { | |
00f0254e | 102 | if (!phy_is_wideport_member(port, phy)) |
90f1e10d | 103 | sas_deform_port(phy, 0); |
303694ee DW |
104 | else if (phy->suspended) { |
105 | phy->suspended = 0; | |
106 | sas_resume_port(phy); | |
107 | ||
108 | /* phy came back, try to cancel the timeout */ | |
109 | wake_up(&sas_ha->eh_wait_q); | |
110 | return; | |
111 | } else { | |
2908d778 | 112 | SAS_DPRINTK("%s: phy%d belongs to port%d already(%d)!\n", |
cadbd4a5 | 113 | __func__, phy->id, phy->port->id, |
2908d778 JB |
114 | phy->port->num_phys); |
115 | return; | |
116 | } | |
117 | } | |
118 | ||
5381837f | 119 | /* see if the phy should be part of a wide port */ |
980fa2f9 | 120 | spin_lock_irqsave(&sas_ha->phy_port_lock, flags); |
2908d778 JB |
121 | for (i = 0; i < sas_ha->num_phys; i++) { |
122 | port = sas_ha->sas_port[i]; | |
123 | spin_lock(&port->phy_list_lock); | |
124 | if (*(u64 *) port->sas_addr && | |
00f0254e | 125 | phy_is_wideport_member(port, phy) && port->num_phys > 0) { |
2908d778 JB |
126 | /* wide port */ |
127 | SAS_DPRINTK("phy%d matched wide port%d\n", phy->id, | |
128 | port->id); | |
129 | break; | |
2908d778 JB |
130 | } |
131 | spin_unlock(&port->phy_list_lock); | |
132 | } | |
5381837f TP |
133 | /* The phy does not match any existing port, create a new one */ |
134 | if (i == sas_ha->num_phys) { | |
135 | for (i = 0; i < sas_ha->num_phys; i++) { | |
136 | port = sas_ha->sas_port[i]; | |
137 | spin_lock(&port->phy_list_lock); | |
138 | if (*(u64 *)port->sas_addr == 0 | |
139 | && port->num_phys == 0) { | |
140 | memcpy(port->sas_addr, phy->sas_addr, | |
141 | SAS_ADDR_SIZE); | |
142 | break; | |
143 | } | |
144 | spin_unlock(&port->phy_list_lock); | |
145 | } | |
146 | } | |
2908d778 JB |
147 | |
148 | if (i >= sas_ha->num_phys) { | |
149 | printk(KERN_NOTICE "%s: couldn't find a free port, bug?\n", | |
cadbd4a5 | 150 | __func__); |
980fa2f9 | 151 | spin_unlock_irqrestore(&sas_ha->phy_port_lock, flags); |
2908d778 JB |
152 | return; |
153 | } | |
154 | ||
155 | /* add the phy to the port */ | |
156 | list_add_tail(&phy->port_phy_el, &port->phy_list); | |
899fcf40 | 157 | sas_phy_set_target(phy, port->port_dev); |
2908d778 JB |
158 | phy->port = port; |
159 | port->num_phys++; | |
160 | port->phy_mask |= (1U << phy->id); | |
161 | ||
2908d778 JB |
162 | if (*(u64 *)port->attached_sas_addr == 0) { |
163 | port->class = phy->class; | |
164 | memcpy(port->attached_sas_addr, phy->attached_sas_addr, | |
165 | SAS_ADDR_SIZE); | |
166 | port->iproto = phy->iproto; | |
167 | port->tproto = phy->tproto; | |
168 | port->oob_mode = phy->oob_mode; | |
169 | port->linkrate = phy->linkrate; | |
170 | } else | |
171 | port->linkrate = max(port->linkrate, phy->linkrate); | |
172 | spin_unlock(&port->phy_list_lock); | |
980fa2f9 | 173 | spin_unlock_irqrestore(&sas_ha->phy_port_lock, flags); |
2908d778 JB |
174 | |
175 | if (!port->port) { | |
b4698d88 | 176 | port->port = sas_port_alloc(phy->phy->dev.parent, port->id); |
2908d778 JB |
177 | BUG_ON(!port->port); |
178 | sas_port_add(port->port); | |
179 | } | |
180 | sas_port_add_phy(port->port, phy->phy); | |
181 | ||
a29c0515 | 182 | SAS_DPRINTK("%s added to %s, phy_mask:0x%x (%16llx)\n", |
71610f55 | 183 | dev_name(&phy->phy->dev), dev_name(&port->port->dev), |
a29c0515 JB |
184 | port->phy_mask, |
185 | SAS_ADDR(port->attached_sas_addr)); | |
186 | ||
2908d778 JB |
187 | if (port->port_dev) |
188 | port->port_dev->pathways = port->num_phys; | |
189 | ||
190 | /* Tell the LLDD about this port formation. */ | |
191 | if (si->dft->lldd_port_formed) | |
192 | si->dft->lldd_port_formed(phy); | |
193 | ||
194 | sas_discover_event(phy->port, DISCE_DISCOVER_DOMAIN); | |
195 | } | |
196 | ||
197 | /** | |
198 | * sas_deform_port -- remove this phy from the port it belongs to | |
199 | * @phy: the phy of interest | |
200 | * | |
201 | * This is called when the physical link to the other phy has been | |
202 | * lost (on this phy), in Event thread context. We cannot delay here. | |
203 | */ | |
90f1e10d | 204 | void sas_deform_port(struct asd_sas_phy *phy, int gone) |
2908d778 JB |
205 | { |
206 | struct sas_ha_struct *sas_ha = phy->ha; | |
207 | struct asd_sas_port *port = phy->port; | |
208 | struct sas_internal *si = | |
209 | to_sas_internal(sas_ha->core.shost->transportt); | |
90f1e10d | 210 | struct domain_device *dev; |
980fa2f9 | 211 | unsigned long flags; |
2908d778 JB |
212 | |
213 | if (!port) | |
214 | return; /* done by a phy event */ | |
215 | ||
90f1e10d DW |
216 | dev = port->port_dev; |
217 | if (dev) | |
218 | dev->pathways--; | |
2908d778 JB |
219 | |
220 | if (port->num_phys == 1) { | |
7d05919a | 221 | sas_unregister_domain_devices(port, gone); |
2908d778 JB |
222 | sas_port_delete(port->port); |
223 | port->port = NULL; | |
f41a0c44 | 224 | } else { |
2908d778 | 225 | sas_port_delete_phy(port->port, phy->phy); |
f41a0c44 DW |
226 | sas_device_set_phy(dev, port->port); |
227 | } | |
2908d778 | 228 | |
2908d778 JB |
229 | if (si->dft->lldd_port_deformed) |
230 | si->dft->lldd_port_deformed(phy); | |
231 | ||
980fa2f9 | 232 | spin_lock_irqsave(&sas_ha->phy_port_lock, flags); |
2908d778 JB |
233 | spin_lock(&port->phy_list_lock); |
234 | ||
235 | list_del_init(&phy->port_phy_el); | |
899fcf40 | 236 | sas_phy_set_target(phy, NULL); |
2908d778 JB |
237 | phy->port = NULL; |
238 | port->num_phys--; | |
239 | port->phy_mask &= ~(1U << phy->id); | |
240 | ||
241 | if (port->num_phys == 0) { | |
242 | INIT_LIST_HEAD(&port->phy_list); | |
243 | memset(port->sas_addr, 0, SAS_ADDR_SIZE); | |
244 | memset(port->attached_sas_addr, 0, SAS_ADDR_SIZE); | |
245 | port->class = 0; | |
246 | port->iproto = 0; | |
247 | port->tproto = 0; | |
248 | port->oob_mode = 0; | |
249 | port->phy_mask = 0; | |
250 | } | |
251 | spin_unlock(&port->phy_list_lock); | |
980fa2f9 | 252 | spin_unlock_irqrestore(&sas_ha->phy_port_lock, flags); |
2908d778 JB |
253 | |
254 | return; | |
255 | } | |
256 | ||
257 | /* ---------- SAS port events ---------- */ | |
258 | ||
c4028958 | 259 | void sas_porte_bytes_dmaed(struct work_struct *work) |
2908d778 | 260 | { |
22b9153f | 261 | struct asd_sas_event *ev = to_asd_sas_event(work); |
c4028958 | 262 | struct asd_sas_phy *phy = ev->phy; |
2908d778 | 263 | |
b15ebe0b | 264 | clear_bit(PORTE_BYTES_DMAED, &phy->port_events_pending); |
2908d778 JB |
265 | |
266 | sas_form_port(phy); | |
267 | } | |
268 | ||
c4028958 | 269 | void sas_porte_broadcast_rcvd(struct work_struct *work) |
2908d778 | 270 | { |
22b9153f | 271 | struct asd_sas_event *ev = to_asd_sas_event(work); |
c4028958 | 272 | struct asd_sas_phy *phy = ev->phy; |
2908d778 JB |
273 | unsigned long flags; |
274 | u32 prim; | |
2908d778 | 275 | |
b15ebe0b | 276 | clear_bit(PORTE_BROADCAST_RCVD, &phy->port_events_pending); |
2908d778 JB |
277 | |
278 | spin_lock_irqsave(&phy->sas_prim_lock, flags); | |
279 | prim = phy->sas_prim; | |
280 | spin_unlock_irqrestore(&phy->sas_prim_lock, flags); | |
281 | ||
282 | SAS_DPRINTK("broadcast received: %d\n", prim); | |
283 | sas_discover_event(phy->port, DISCE_REVALIDATE_DOMAIN); | |
284 | } | |
285 | ||
c4028958 | 286 | void sas_porte_link_reset_err(struct work_struct *work) |
2908d778 | 287 | { |
22b9153f | 288 | struct asd_sas_event *ev = to_asd_sas_event(work); |
c4028958 | 289 | struct asd_sas_phy *phy = ev->phy; |
2908d778 | 290 | |
b15ebe0b | 291 | clear_bit(PORTE_LINK_RESET_ERR, &phy->port_events_pending); |
2908d778 | 292 | |
90f1e10d | 293 | sas_deform_port(phy, 1); |
2908d778 JB |
294 | } |
295 | ||
c4028958 | 296 | void sas_porte_timer_event(struct work_struct *work) |
2908d778 | 297 | { |
22b9153f | 298 | struct asd_sas_event *ev = to_asd_sas_event(work); |
c4028958 | 299 | struct asd_sas_phy *phy = ev->phy; |
2908d778 | 300 | |
b15ebe0b | 301 | clear_bit(PORTE_TIMER_EVENT, &phy->port_events_pending); |
2908d778 | 302 | |
90f1e10d | 303 | sas_deform_port(phy, 1); |
2908d778 JB |
304 | } |
305 | ||
c4028958 | 306 | void sas_porte_hard_reset(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 | |
b15ebe0b | 311 | clear_bit(PORTE_HARD_RESET, &phy->port_events_pending); |
2908d778 | 312 | |
90f1e10d | 313 | sas_deform_port(phy, 1); |
2908d778 JB |
314 | } |
315 | ||
316 | /* ---------- SAS port registration ---------- */ | |
317 | ||
318 | static void sas_init_port(struct asd_sas_port *port, | |
319 | struct sas_ha_struct *sas_ha, int i) | |
320 | { | |
a29c0515 | 321 | memset(port, 0, sizeof(*port)); |
2908d778 JB |
322 | port->id = i; |
323 | INIT_LIST_HEAD(&port->dev_list); | |
87c8331f DW |
324 | INIT_LIST_HEAD(&port->disco_list); |
325 | INIT_LIST_HEAD(&port->destroy_list); | |
2908d778 JB |
326 | spin_lock_init(&port->phy_list_lock); |
327 | INIT_LIST_HEAD(&port->phy_list); | |
2908d778 JB |
328 | port->ha = sas_ha; |
329 | ||
330 | spin_lock_init(&port->dev_list_lock); | |
331 | } | |
332 | ||
333 | int sas_register_ports(struct sas_ha_struct *sas_ha) | |
334 | { | |
335 | int i; | |
336 | ||
337 | /* initialize the ports and discovery */ | |
338 | for (i = 0; i < sas_ha->num_phys; i++) { | |
339 | struct asd_sas_port *port = sas_ha->sas_port[i]; | |
340 | ||
341 | sas_init_port(port, sas_ha, i); | |
342 | sas_init_disc(&port->disc, port); | |
343 | } | |
344 | return 0; | |
345 | } | |
346 | ||
347 | void sas_unregister_ports(struct sas_ha_struct *sas_ha) | |
348 | { | |
349 | int i; | |
350 | ||
351 | for (i = 0; i < sas_ha->num_phys; i++) | |
352 | if (sas_ha->sas_phy[i]->port) | |
90f1e10d | 353 | sas_deform_port(sas_ha->sas_phy[i], 0); |
2908d778 JB |
354 | |
355 | } |