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