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