]> git.proxmox.com Git - mirror_ubuntu-zesty-kernel.git/blob - drivers/scsi/libsas/sas_port.c
WorkStruct: make allyesconfig
[mirror_ubuntu-zesty-kernel.git] / drivers / scsi / libsas / sas_port.c
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
31 /**
32 * sas_form_port -- add this phy to a port
33 * @phy: the phy of interest
34 *
35 * This function adds this phy to an existing port, thus creating a wide
36 * port, or it creates a port and adds the phy to the port.
37 */
38 static void sas_form_port(struct asd_sas_phy *phy)
39 {
40 int i;
41 struct sas_ha_struct *sas_ha = phy->ha;
42 struct asd_sas_port *port = phy->port;
43 struct sas_internal *si =
44 to_sas_internal(sas_ha->core.shost->transportt);
45
46 if (port) {
47 if (memcmp(port->attached_sas_addr, phy->attached_sas_addr,
48 SAS_ADDR_SIZE) == 0)
49 sas_deform_port(phy);
50 else {
51 SAS_DPRINTK("%s: phy%d belongs to port%d already(%d)!\n",
52 __FUNCTION__, phy->id, phy->port->id,
53 phy->port->num_phys);
54 return;
55 }
56 }
57
58 /* find a port */
59 spin_lock(&sas_ha->phy_port_lock);
60 for (i = 0; i < sas_ha->num_phys; i++) {
61 port = sas_ha->sas_port[i];
62 spin_lock(&port->phy_list_lock);
63 if (*(u64 *) port->sas_addr &&
64 memcmp(port->attached_sas_addr,
65 phy->attached_sas_addr, SAS_ADDR_SIZE) == 0 &&
66 port->num_phys > 0) {
67 /* wide port */
68 SAS_DPRINTK("phy%d matched wide port%d\n", phy->id,
69 port->id);
70 break;
71 } else if (*(u64 *) port->sas_addr == 0 && port->num_phys==0) {
72 memcpy(port->sas_addr, phy->sas_addr, SAS_ADDR_SIZE);
73 break;
74 }
75 spin_unlock(&port->phy_list_lock);
76 }
77
78 if (i >= sas_ha->num_phys) {
79 printk(KERN_NOTICE "%s: couldn't find a free port, bug?\n",
80 __FUNCTION__);
81 spin_unlock(&sas_ha->phy_port_lock);
82 return;
83 }
84
85 /* add the phy to the port */
86 list_add_tail(&phy->port_phy_el, &port->phy_list);
87 phy->port = port;
88 port->num_phys++;
89 port->phy_mask |= (1U << phy->id);
90
91 if (!port->phy)
92 port->phy = phy->phy;
93
94 SAS_DPRINTK("phy%d added to port%d, phy_mask:0x%x\n", phy->id,
95 port->id, port->phy_mask);
96
97 if (*(u64 *)port->attached_sas_addr == 0) {
98 port->class = phy->class;
99 memcpy(port->attached_sas_addr, phy->attached_sas_addr,
100 SAS_ADDR_SIZE);
101 port->iproto = phy->iproto;
102 port->tproto = phy->tproto;
103 port->oob_mode = phy->oob_mode;
104 port->linkrate = phy->linkrate;
105 } else
106 port->linkrate = max(port->linkrate, phy->linkrate);
107 spin_unlock(&port->phy_list_lock);
108 spin_unlock(&sas_ha->phy_port_lock);
109
110 if (!port->port) {
111 port->port = sas_port_alloc(phy->phy->dev.parent, port->id);
112 BUG_ON(!port->port);
113 sas_port_add(port->port);
114 }
115 sas_port_add_phy(port->port, phy->phy);
116
117 if (port->port_dev)
118 port->port_dev->pathways = port->num_phys;
119
120 /* Tell the LLDD about this port formation. */
121 if (si->dft->lldd_port_formed)
122 si->dft->lldd_port_formed(phy);
123
124 sas_discover_event(phy->port, DISCE_DISCOVER_DOMAIN);
125 }
126
127 /**
128 * sas_deform_port -- remove this phy from the port it belongs to
129 * @phy: the phy of interest
130 *
131 * This is called when the physical link to the other phy has been
132 * lost (on this phy), in Event thread context. We cannot delay here.
133 */
134 void sas_deform_port(struct asd_sas_phy *phy)
135 {
136 struct sas_ha_struct *sas_ha = phy->ha;
137 struct asd_sas_port *port = phy->port;
138 struct sas_internal *si =
139 to_sas_internal(sas_ha->core.shost->transportt);
140
141 if (!port)
142 return; /* done by a phy event */
143
144 if (port->port_dev)
145 port->port_dev->pathways--;
146
147 if (port->num_phys == 1) {
148 sas_unregister_domain_devices(port);
149 sas_port_delete(port->port);
150 port->port = NULL;
151 } else
152 sas_port_delete_phy(port->port, phy->phy);
153
154
155 if (si->dft->lldd_port_deformed)
156 si->dft->lldd_port_deformed(phy);
157
158 spin_lock(&sas_ha->phy_port_lock);
159 spin_lock(&port->phy_list_lock);
160
161 list_del_init(&phy->port_phy_el);
162 phy->port = NULL;
163 port->num_phys--;
164 port->phy_mask &= ~(1U << phy->id);
165
166 if (port->num_phys == 0) {
167 INIT_LIST_HEAD(&port->phy_list);
168 memset(port->sas_addr, 0, SAS_ADDR_SIZE);
169 memset(port->attached_sas_addr, 0, SAS_ADDR_SIZE);
170 port->class = 0;
171 port->iproto = 0;
172 port->tproto = 0;
173 port->oob_mode = 0;
174 port->phy_mask = 0;
175 }
176 spin_unlock(&port->phy_list_lock);
177 spin_unlock(&sas_ha->phy_port_lock);
178
179 return;
180 }
181
182 /* ---------- SAS port events ---------- */
183
184 void sas_porte_bytes_dmaed(struct work_struct *work)
185 {
186 struct asd_sas_event *ev =
187 container_of(work, struct asd_sas_event, work);
188 struct asd_sas_phy *phy = ev->phy;
189
190 sas_begin_event(PORTE_BYTES_DMAED, &phy->ha->event_lock,
191 &phy->port_events_pending);
192
193 sas_form_port(phy);
194 }
195
196 void sas_porte_broadcast_rcvd(struct work_struct *work)
197 {
198 struct asd_sas_event *ev =
199 container_of(work, struct asd_sas_event, work);
200 struct asd_sas_phy *phy = ev->phy;
201 unsigned long flags;
202 u32 prim;
203
204 sas_begin_event(PORTE_BROADCAST_RCVD, &phy->ha->event_lock,
205 &phy->port_events_pending);
206
207 spin_lock_irqsave(&phy->sas_prim_lock, flags);
208 prim = phy->sas_prim;
209 spin_unlock_irqrestore(&phy->sas_prim_lock, flags);
210
211 SAS_DPRINTK("broadcast received: %d\n", prim);
212 sas_discover_event(phy->port, DISCE_REVALIDATE_DOMAIN);
213 }
214
215 void sas_porte_link_reset_err(struct work_struct *work)
216 {
217 struct asd_sas_event *ev =
218 container_of(work, struct asd_sas_event, work);
219 struct asd_sas_phy *phy = ev->phy;
220
221 sas_begin_event(PORTE_LINK_RESET_ERR, &phy->ha->event_lock,
222 &phy->port_events_pending);
223
224 sas_deform_port(phy);
225 }
226
227 void sas_porte_timer_event(struct work_struct *work)
228 {
229 struct asd_sas_event *ev =
230 container_of(work, struct asd_sas_event, work);
231 struct asd_sas_phy *phy = ev->phy;
232
233 sas_begin_event(PORTE_TIMER_EVENT, &phy->ha->event_lock,
234 &phy->port_events_pending);
235
236 sas_deform_port(phy);
237 }
238
239 void sas_porte_hard_reset(struct work_struct *work)
240 {
241 struct asd_sas_event *ev =
242 container_of(work, struct asd_sas_event, work);
243 struct asd_sas_phy *phy = ev->phy;
244
245 sas_begin_event(PORTE_HARD_RESET, &phy->ha->event_lock,
246 &phy->port_events_pending);
247
248 sas_deform_port(phy);
249 }
250
251 /* ---------- SAS port registration ---------- */
252
253 static void sas_init_port(struct asd_sas_port *port,
254 struct sas_ha_struct *sas_ha, int i)
255 {
256 port->id = i;
257 INIT_LIST_HEAD(&port->dev_list);
258 spin_lock_init(&port->phy_list_lock);
259 INIT_LIST_HEAD(&port->phy_list);
260 port->num_phys = 0;
261 port->phy_mask = 0;
262 port->ha = sas_ha;
263
264 spin_lock_init(&port->dev_list_lock);
265 }
266
267 int sas_register_ports(struct sas_ha_struct *sas_ha)
268 {
269 int i;
270
271 /* initialize the ports and discovery */
272 for (i = 0; i < sas_ha->num_phys; i++) {
273 struct asd_sas_port *port = sas_ha->sas_port[i];
274
275 sas_init_port(port, sas_ha, i);
276 sas_init_disc(&port->disc, port);
277 }
278 return 0;
279 }
280
281 void sas_unregister_ports(struct sas_ha_struct *sas_ha)
282 {
283 int i;
284
285 for (i = 0; i < sas_ha->num_phys; i++)
286 if (sas_ha->sas_phy[i]->port)
287 sas_deform_port(sas_ha->sas_phy[i]);
288
289 }