Merge git://git.kernel.org/pub/scm/linux/kernel/git/torvalds/linux-2.6
[wrt350n-kernel.git] / drivers / scsi / libsas / sas_port.c
blob82f5b38014167bbd3c45b00e1e3435a21286460f
1 /*
2 * Serial Attached SCSI (SAS) Port class
4 * Copyright (C) 2005 Adaptec, Inc. All rights reserved.
5 * Copyright (C) 2005 Luben Tuikov <luben_tuikov@adaptec.com>
7 * This file is licensed under GPLv2.
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.
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.
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
25 #include "sas_internal.h"
27 #include <scsi/scsi_transport.h>
28 #include <scsi/scsi_transport_sas.h>
29 #include "../scsi_sas_internal.h"
31 /**
32 * sas_form_port -- add this phy to a port
33 * @phy: the phy of interest
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.
38 static void sas_form_port(struct asd_sas_phy *phy)
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;
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 __FUNCTION__, phy->id, phy->port->id,
54 phy->port->num_phys);
55 return;
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;
76 spin_unlock(&port->phy_list_lock);
79 if (i >= sas_ha->num_phys) {
80 printk(KERN_NOTICE "%s: couldn't find a free port, bug?\n",
81 __FUNCTION__);
82 spin_unlock_irqrestore(&sas_ha->phy_port_lock, flags);
83 return;
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);
92 if (!port->phy)
93 port->phy = phy->phy;
95 <<<<<<< HEAD:drivers/scsi/libsas/sas_port.c
96 SAS_DPRINTK("phy%d added to port%d, phy_mask:0x%x\n", phy->id,
97 port->id, port->phy_mask);
99 =======
100 >>>>>>> 264e3e889d86e552b4191d69bb60f4f3b383135a:drivers/scsi/libsas/sas_port.c
101 if (*(u64 *)port->attached_sas_addr == 0) {
102 port->class = phy->class;
103 memcpy(port->attached_sas_addr, phy->attached_sas_addr,
104 SAS_ADDR_SIZE);
105 port->iproto = phy->iproto;
106 port->tproto = phy->tproto;
107 port->oob_mode = phy->oob_mode;
108 port->linkrate = phy->linkrate;
109 } else
110 port->linkrate = max(port->linkrate, phy->linkrate);
111 spin_unlock(&port->phy_list_lock);
112 spin_unlock_irqrestore(&sas_ha->phy_port_lock, flags);
114 if (!port->port) {
115 port->port = sas_port_alloc(phy->phy->dev.parent, port->id);
116 BUG_ON(!port->port);
117 sas_port_add(port->port);
119 sas_port_add_phy(port->port, phy->phy);
121 <<<<<<< HEAD:drivers/scsi/libsas/sas_port.c
122 =======
123 SAS_DPRINTK("%s added to %s, phy_mask:0x%x (%16llx)\n",
124 phy->phy->dev.bus_id,port->port->dev.bus_id,
125 port->phy_mask,
126 SAS_ADDR(port->attached_sas_addr));
128 >>>>>>> 264e3e889d86e552b4191d69bb60f4f3b383135a:drivers/scsi/libsas/sas_port.c
129 if (port->port_dev)
130 port->port_dev->pathways = port->num_phys;
132 /* Tell the LLDD about this port formation. */
133 if (si->dft->lldd_port_formed)
134 si->dft->lldd_port_formed(phy);
136 sas_discover_event(phy->port, DISCE_DISCOVER_DOMAIN);
140 * sas_deform_port -- remove this phy from the port it belongs to
141 * @phy: the phy of interest
143 * This is called when the physical link to the other phy has been
144 * lost (on this phy), in Event thread context. We cannot delay here.
146 void sas_deform_port(struct asd_sas_phy *phy)
148 struct sas_ha_struct *sas_ha = phy->ha;
149 struct asd_sas_port *port = phy->port;
150 struct sas_internal *si =
151 to_sas_internal(sas_ha->core.shost->transportt);
152 unsigned long flags;
154 if (!port)
155 return; /* done by a phy event */
157 if (port->port_dev)
158 port->port_dev->pathways--;
160 if (port->num_phys == 1) {
161 sas_unregister_domain_devices(port);
162 sas_port_delete(port->port);
163 port->port = NULL;
164 } else
165 sas_port_delete_phy(port->port, phy->phy);
168 if (si->dft->lldd_port_deformed)
169 si->dft->lldd_port_deformed(phy);
171 spin_lock_irqsave(&sas_ha->phy_port_lock, flags);
172 spin_lock(&port->phy_list_lock);
174 list_del_init(&phy->port_phy_el);
175 phy->port = NULL;
176 port->num_phys--;
177 port->phy_mask &= ~(1U << phy->id);
179 if (port->num_phys == 0) {
180 INIT_LIST_HEAD(&port->phy_list);
181 memset(port->sas_addr, 0, SAS_ADDR_SIZE);
182 memset(port->attached_sas_addr, 0, SAS_ADDR_SIZE);
183 port->class = 0;
184 port->iproto = 0;
185 port->tproto = 0;
186 port->oob_mode = 0;
187 port->phy_mask = 0;
189 spin_unlock(&port->phy_list_lock);
190 spin_unlock_irqrestore(&sas_ha->phy_port_lock, flags);
192 return;
195 /* ---------- SAS port events ---------- */
197 void sas_porte_bytes_dmaed(struct work_struct *work)
199 struct asd_sas_event *ev =
200 container_of(work, struct asd_sas_event, work);
201 struct asd_sas_phy *phy = ev->phy;
203 sas_begin_event(PORTE_BYTES_DMAED, &phy->ha->event_lock,
204 &phy->port_events_pending);
206 sas_form_port(phy);
209 void sas_porte_broadcast_rcvd(struct work_struct *work)
211 struct asd_sas_event *ev =
212 container_of(work, struct asd_sas_event, work);
213 struct asd_sas_phy *phy = ev->phy;
214 unsigned long flags;
215 u32 prim;
217 sas_begin_event(PORTE_BROADCAST_RCVD, &phy->ha->event_lock,
218 &phy->port_events_pending);
220 spin_lock_irqsave(&phy->sas_prim_lock, flags);
221 prim = phy->sas_prim;
222 spin_unlock_irqrestore(&phy->sas_prim_lock, flags);
224 SAS_DPRINTK("broadcast received: %d\n", prim);
225 sas_discover_event(phy->port, DISCE_REVALIDATE_DOMAIN);
228 void sas_porte_link_reset_err(struct work_struct *work)
230 struct asd_sas_event *ev =
231 container_of(work, struct asd_sas_event, work);
232 struct asd_sas_phy *phy = ev->phy;
234 sas_begin_event(PORTE_LINK_RESET_ERR, &phy->ha->event_lock,
235 &phy->port_events_pending);
237 sas_deform_port(phy);
240 void sas_porte_timer_event(struct work_struct *work)
242 struct asd_sas_event *ev =
243 container_of(work, struct asd_sas_event, work);
244 struct asd_sas_phy *phy = ev->phy;
246 sas_begin_event(PORTE_TIMER_EVENT, &phy->ha->event_lock,
247 &phy->port_events_pending);
249 sas_deform_port(phy);
252 void sas_porte_hard_reset(struct work_struct *work)
254 struct asd_sas_event *ev =
255 container_of(work, struct asd_sas_event, work);
256 struct asd_sas_phy *phy = ev->phy;
258 sas_begin_event(PORTE_HARD_RESET, &phy->ha->event_lock,
259 &phy->port_events_pending);
261 sas_deform_port(phy);
264 /* ---------- SAS port registration ---------- */
266 static void sas_init_port(struct asd_sas_port *port,
267 struct sas_ha_struct *sas_ha, int i)
269 <<<<<<< HEAD:drivers/scsi/libsas/sas_port.c
270 =======
271 memset(port, 0, sizeof(*port));
272 >>>>>>> 264e3e889d86e552b4191d69bb60f4f3b383135a:drivers/scsi/libsas/sas_port.c
273 port->id = i;
274 INIT_LIST_HEAD(&port->dev_list);
275 spin_lock_init(&port->phy_list_lock);
276 INIT_LIST_HEAD(&port->phy_list);
277 <<<<<<< HEAD:drivers/scsi/libsas/sas_port.c
278 port->num_phys = 0;
279 port->phy_mask = 0;
280 =======
281 >>>>>>> 264e3e889d86e552b4191d69bb60f4f3b383135a:drivers/scsi/libsas/sas_port.c
282 port->ha = sas_ha;
284 spin_lock_init(&port->dev_list_lock);
287 int sas_register_ports(struct sas_ha_struct *sas_ha)
289 int i;
291 /* initialize the ports and discovery */
292 for (i = 0; i < sas_ha->num_phys; i++) {
293 struct asd_sas_port *port = sas_ha->sas_port[i];
295 sas_init_port(port, sas_ha, i);
296 sas_init_disc(&port->disc, port);
298 return 0;
301 void sas_unregister_ports(struct sas_ha_struct *sas_ha)
303 int i;
305 for (i = 0; i < sas_ha->num_phys; i++)
306 if (sas_ha->sas_phy[i]->port)
307 sas_deform_port(sas_ha->sas_phy[i]);