Merge mulgrave-w:git/scsi-misc-2.6
[pandora-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(void *data)
185 {
186         struct asd_sas_phy *phy = data;
187
188         sas_begin_event(PORTE_BYTES_DMAED, &phy->ha->event_lock,
189                         &phy->port_events_pending);
190
191         sas_form_port(phy);
192 }
193
194 void sas_porte_broadcast_rcvd(void *data)
195 {
196         unsigned long flags;
197         u32 prim;
198         struct asd_sas_phy *phy = data;
199
200         sas_begin_event(PORTE_BROADCAST_RCVD, &phy->ha->event_lock,
201                         &phy->port_events_pending);
202
203         spin_lock_irqsave(&phy->sas_prim_lock, flags);
204         prim = phy->sas_prim;
205         spin_unlock_irqrestore(&phy->sas_prim_lock, flags);
206
207         SAS_DPRINTK("broadcast received: %d\n", prim);
208         sas_discover_event(phy->port, DISCE_REVALIDATE_DOMAIN);
209 }
210
211 void sas_porte_link_reset_err(void *data)
212 {
213         struct asd_sas_phy *phy = data;
214
215         sas_begin_event(PORTE_LINK_RESET_ERR, &phy->ha->event_lock,
216                         &phy->port_events_pending);
217
218         sas_deform_port(phy);
219 }
220
221 void sas_porte_timer_event(void *data)
222 {
223         struct asd_sas_phy *phy = data;
224
225         sas_begin_event(PORTE_TIMER_EVENT, &phy->ha->event_lock,
226                         &phy->port_events_pending);
227
228         sas_deform_port(phy);
229 }
230
231 void sas_porte_hard_reset(void *data)
232 {
233         struct asd_sas_phy *phy = data;
234
235         sas_begin_event(PORTE_HARD_RESET, &phy->ha->event_lock,
236                         &phy->port_events_pending);
237
238         sas_deform_port(phy);
239 }
240
241 /* ---------- SAS port registration ---------- */
242
243 static void sas_init_port(struct asd_sas_port *port,
244                           struct sas_ha_struct *sas_ha, int i)
245 {
246         port->id = i;
247         INIT_LIST_HEAD(&port->dev_list);
248         spin_lock_init(&port->phy_list_lock);
249         INIT_LIST_HEAD(&port->phy_list);
250         port->num_phys = 0;
251         port->phy_mask = 0;
252         port->ha = sas_ha;
253
254         spin_lock_init(&port->dev_list_lock);
255 }
256
257 int sas_register_ports(struct sas_ha_struct *sas_ha)
258 {
259         int i;
260
261         /* initialize the ports and discovery */
262         for (i = 0; i < sas_ha->num_phys; i++) {
263                 struct asd_sas_port *port = sas_ha->sas_port[i];
264
265                 sas_init_port(port, sas_ha, i);
266                 sas_init_disc(&port->disc, port);
267         }
268         return 0;
269 }
270
271 void sas_unregister_ports(struct sas_ha_struct *sas_ha)
272 {
273         int i;
274
275         for (i = 0; i < sas_ha->num_phys; i++)
276                 if (sas_ha->sas_phy[i]->port)
277                         sas_deform_port(sas_ha->sas_phy[i]);
278
279 }