blob: a47c7a75327b4316d0c4bd5138247e43f4979e9c [file] [log] [blame]
James Bottomley2908d772006-08-29 09:22:51 -05001/*
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
Dan Williams00f02542010-10-01 13:55:52 -070031static bool phy_is_wideport_member(struct asd_sas_port *port, struct asd_sas_phy *phy)
32{
33 struct sas_ha_struct *sas_ha = phy->ha;
34
35 if (memcmp(port->attached_sas_addr, phy->attached_sas_addr,
36 SAS_ADDR_SIZE) != 0 || (sas_ha->strict_wide_ports &&
37 memcmp(port->sas_addr, phy->sas_addr, SAS_ADDR_SIZE) != 0))
38 return false;
39 return true;
40}
41
James Bottomley2908d772006-08-29 09:22:51 -050042/**
43 * sas_form_port -- add this phy to a port
44 * @phy: the phy of interest
45 *
46 * This function adds this phy to an existing port, thus creating a wide
47 * port, or it creates a port and adds the phy to the port.
48 */
49static void sas_form_port(struct asd_sas_phy *phy)
50{
51 int i;
52 struct sas_ha_struct *sas_ha = phy->ha;
53 struct asd_sas_port *port = phy->port;
54 struct sas_internal *si =
55 to_sas_internal(sas_ha->core.shost->transportt);
Darrick J. Wong980fa2f2007-01-11 14:15:40 -080056 unsigned long flags;
James Bottomley2908d772006-08-29 09:22:51 -050057
58 if (port) {
Dan Williams00f02542010-10-01 13:55:52 -070059 if (!phy_is_wideport_member(port, phy))
Dan Williams90f1e102011-05-24 13:17:53 -070060 sas_deform_port(phy, 0);
James Bottomley2908d772006-08-29 09:22:51 -050061 else {
62 SAS_DPRINTK("%s: phy%d belongs to port%d already(%d)!\n",
Harvey Harrisoncadbd4a2008-07-03 23:47:27 -070063 __func__, phy->id, phy->port->id,
James Bottomley2908d772006-08-29 09:22:51 -050064 phy->port->num_phys);
65 return;
66 }
67 }
68
Tom Peng53818372009-07-01 20:37:26 +080069 /* see if the phy should be part of a wide port */
Darrick J. Wong980fa2f2007-01-11 14:15:40 -080070 spin_lock_irqsave(&sas_ha->phy_port_lock, flags);
James Bottomley2908d772006-08-29 09:22:51 -050071 for (i = 0; i < sas_ha->num_phys; i++) {
72 port = sas_ha->sas_port[i];
73 spin_lock(&port->phy_list_lock);
74 if (*(u64 *) port->sas_addr &&
Dan Williams00f02542010-10-01 13:55:52 -070075 phy_is_wideport_member(port, phy) && port->num_phys > 0) {
James Bottomley2908d772006-08-29 09:22:51 -050076 /* wide port */
77 SAS_DPRINTK("phy%d matched wide port%d\n", phy->id,
78 port->id);
79 break;
James Bottomley2908d772006-08-29 09:22:51 -050080 }
81 spin_unlock(&port->phy_list_lock);
82 }
Tom Peng53818372009-07-01 20:37:26 +080083 /* The phy does not match any existing port, create a new one */
84 if (i == sas_ha->num_phys) {
85 for (i = 0; i < sas_ha->num_phys; i++) {
86 port = sas_ha->sas_port[i];
87 spin_lock(&port->phy_list_lock);
88 if (*(u64 *)port->sas_addr == 0
89 && port->num_phys == 0) {
90 memcpy(port->sas_addr, phy->sas_addr,
91 SAS_ADDR_SIZE);
92 break;
93 }
94 spin_unlock(&port->phy_list_lock);
95 }
96 }
James Bottomley2908d772006-08-29 09:22:51 -050097
98 if (i >= sas_ha->num_phys) {
99 printk(KERN_NOTICE "%s: couldn't find a free port, bug?\n",
Harvey Harrisoncadbd4a2008-07-03 23:47:27 -0700100 __func__);
Darrick J. Wong980fa2f2007-01-11 14:15:40 -0800101 spin_unlock_irqrestore(&sas_ha->phy_port_lock, flags);
James Bottomley2908d772006-08-29 09:22:51 -0500102 return;
103 }
104
105 /* add the phy to the port */
106 list_add_tail(&phy->port_phy_el, &port->phy_list);
107 phy->port = port;
108 port->num_phys++;
109 port->phy_mask |= (1U << phy->id);
110
111 if (!port->phy)
112 port->phy = phy->phy;
113
James Bottomley2908d772006-08-29 09:22:51 -0500114 if (*(u64 *)port->attached_sas_addr == 0) {
115 port->class = phy->class;
116 memcpy(port->attached_sas_addr, phy->attached_sas_addr,
117 SAS_ADDR_SIZE);
118 port->iproto = phy->iproto;
119 port->tproto = phy->tproto;
120 port->oob_mode = phy->oob_mode;
121 port->linkrate = phy->linkrate;
122 } else
123 port->linkrate = max(port->linkrate, phy->linkrate);
124 spin_unlock(&port->phy_list_lock);
Darrick J. Wong980fa2f2007-01-11 14:15:40 -0800125 spin_unlock_irqrestore(&sas_ha->phy_port_lock, flags);
James Bottomley2908d772006-08-29 09:22:51 -0500126
127 if (!port->port) {
128 port->port = sas_port_alloc(phy->phy->dev.parent, port->id);
129 BUG_ON(!port->port);
130 sas_port_add(port->port);
131 }
132 sas_port_add_phy(port->port, phy->phy);
133
James Bottomleya29c0512008-02-23 23:38:44 -0600134 SAS_DPRINTK("%s added to %s, phy_mask:0x%x (%16llx)\n",
Kay Sievers71610f52008-12-03 22:41:36 +0100135 dev_name(&phy->phy->dev), dev_name(&port->port->dev),
James Bottomleya29c0512008-02-23 23:38:44 -0600136 port->phy_mask,
137 SAS_ADDR(port->attached_sas_addr));
138
James Bottomley2908d772006-08-29 09:22:51 -0500139 if (port->port_dev)
140 port->port_dev->pathways = port->num_phys;
141
142 /* Tell the LLDD about this port formation. */
143 if (si->dft->lldd_port_formed)
144 si->dft->lldd_port_formed(phy);
145
146 sas_discover_event(phy->port, DISCE_DISCOVER_DOMAIN);
147}
148
149/**
150 * sas_deform_port -- remove this phy from the port it belongs to
151 * @phy: the phy of interest
152 *
153 * This is called when the physical link to the other phy has been
154 * lost (on this phy), in Event thread context. We cannot delay here.
155 */
Dan Williams90f1e102011-05-24 13:17:53 -0700156void sas_deform_port(struct asd_sas_phy *phy, int gone)
James Bottomley2908d772006-08-29 09:22:51 -0500157{
158 struct sas_ha_struct *sas_ha = phy->ha;
159 struct asd_sas_port *port = phy->port;
160 struct sas_internal *si =
161 to_sas_internal(sas_ha->core.shost->transportt);
Dan Williams90f1e102011-05-24 13:17:53 -0700162 struct domain_device *dev;
Darrick J. Wong980fa2f2007-01-11 14:15:40 -0800163 unsigned long flags;
James Bottomley2908d772006-08-29 09:22:51 -0500164
165 if (!port)
166 return; /* done by a phy event */
167
Dan Williams90f1e102011-05-24 13:17:53 -0700168 dev = port->port_dev;
169 if (dev)
170 dev->pathways--;
James Bottomley2908d772006-08-29 09:22:51 -0500171
172 if (port->num_phys == 1) {
Dan Williams90f1e102011-05-24 13:17:53 -0700173 if (dev && gone)
174 dev->gone = 1;
James Bottomley2908d772006-08-29 09:22:51 -0500175 sas_unregister_domain_devices(port);
176 sas_port_delete(port->port);
177 port->port = NULL;
178 } else
179 sas_port_delete_phy(port->port, phy->phy);
180
James Bottomley2908d772006-08-29 09:22:51 -0500181 if (si->dft->lldd_port_deformed)
182 si->dft->lldd_port_deformed(phy);
183
Darrick J. Wong980fa2f2007-01-11 14:15:40 -0800184 spin_lock_irqsave(&sas_ha->phy_port_lock, flags);
James Bottomley2908d772006-08-29 09:22:51 -0500185 spin_lock(&port->phy_list_lock);
186
187 list_del_init(&phy->port_phy_el);
188 phy->port = NULL;
189 port->num_phys--;
190 port->phy_mask &= ~(1U << phy->id);
191
192 if (port->num_phys == 0) {
193 INIT_LIST_HEAD(&port->phy_list);
194 memset(port->sas_addr, 0, SAS_ADDR_SIZE);
195 memset(port->attached_sas_addr, 0, SAS_ADDR_SIZE);
196 port->class = 0;
197 port->iproto = 0;
198 port->tproto = 0;
199 port->oob_mode = 0;
200 port->phy_mask = 0;
201 }
202 spin_unlock(&port->phy_list_lock);
Darrick J. Wong980fa2f2007-01-11 14:15:40 -0800203 spin_unlock_irqrestore(&sas_ha->phy_port_lock, flags);
James Bottomley2908d772006-08-29 09:22:51 -0500204
205 return;
206}
207
208/* ---------- SAS port events ---------- */
209
David Howellsc4028952006-11-22 14:57:56 +0000210void sas_porte_bytes_dmaed(struct work_struct *work)
James Bottomley2908d772006-08-29 09:22:51 -0500211{
David Howellsc4028952006-11-22 14:57:56 +0000212 struct asd_sas_event *ev =
213 container_of(work, struct asd_sas_event, work);
214 struct asd_sas_phy *phy = ev->phy;
James Bottomley2908d772006-08-29 09:22:51 -0500215
Dan Williamsb15ebe02011-11-17 17:59:49 -0800216 clear_bit(PORTE_BYTES_DMAED, &phy->port_events_pending);
James Bottomley2908d772006-08-29 09:22:51 -0500217
218 sas_form_port(phy);
219}
220
David Howellsc4028952006-11-22 14:57:56 +0000221void sas_porte_broadcast_rcvd(struct work_struct *work)
James Bottomley2908d772006-08-29 09:22:51 -0500222{
David Howellsc4028952006-11-22 14:57:56 +0000223 struct asd_sas_event *ev =
224 container_of(work, struct asd_sas_event, work);
225 struct asd_sas_phy *phy = ev->phy;
James Bottomley2908d772006-08-29 09:22:51 -0500226 unsigned long flags;
227 u32 prim;
James Bottomley2908d772006-08-29 09:22:51 -0500228
Dan Williamsb15ebe02011-11-17 17:59:49 -0800229 clear_bit(PORTE_BROADCAST_RCVD, &phy->port_events_pending);
James Bottomley2908d772006-08-29 09:22:51 -0500230
231 spin_lock_irqsave(&phy->sas_prim_lock, flags);
232 prim = phy->sas_prim;
233 spin_unlock_irqrestore(&phy->sas_prim_lock, flags);
234
235 SAS_DPRINTK("broadcast received: %d\n", prim);
236 sas_discover_event(phy->port, DISCE_REVALIDATE_DOMAIN);
237}
238
David Howellsc4028952006-11-22 14:57:56 +0000239void sas_porte_link_reset_err(struct work_struct *work)
James Bottomley2908d772006-08-29 09:22:51 -0500240{
David Howellsc4028952006-11-22 14:57:56 +0000241 struct asd_sas_event *ev =
242 container_of(work, struct asd_sas_event, work);
243 struct asd_sas_phy *phy = ev->phy;
James Bottomley2908d772006-08-29 09:22:51 -0500244
Dan Williamsb15ebe02011-11-17 17:59:49 -0800245 clear_bit(PORTE_LINK_RESET_ERR, &phy->port_events_pending);
James Bottomley2908d772006-08-29 09:22:51 -0500246
Dan Williams90f1e102011-05-24 13:17:53 -0700247 sas_deform_port(phy, 1);
James Bottomley2908d772006-08-29 09:22:51 -0500248}
249
David Howellsc4028952006-11-22 14:57:56 +0000250void sas_porte_timer_event(struct work_struct *work)
James Bottomley2908d772006-08-29 09:22:51 -0500251{
David Howellsc4028952006-11-22 14:57:56 +0000252 struct asd_sas_event *ev =
253 container_of(work, struct asd_sas_event, work);
254 struct asd_sas_phy *phy = ev->phy;
James Bottomley2908d772006-08-29 09:22:51 -0500255
Dan Williamsb15ebe02011-11-17 17:59:49 -0800256 clear_bit(PORTE_TIMER_EVENT, &phy->port_events_pending);
James Bottomley2908d772006-08-29 09:22:51 -0500257
Dan Williams90f1e102011-05-24 13:17:53 -0700258 sas_deform_port(phy, 1);
James Bottomley2908d772006-08-29 09:22:51 -0500259}
260
David Howellsc4028952006-11-22 14:57:56 +0000261void sas_porte_hard_reset(struct work_struct *work)
James Bottomley2908d772006-08-29 09:22:51 -0500262{
David Howellsc4028952006-11-22 14:57:56 +0000263 struct asd_sas_event *ev =
264 container_of(work, struct asd_sas_event, work);
265 struct asd_sas_phy *phy = ev->phy;
James Bottomley2908d772006-08-29 09:22:51 -0500266
Dan Williamsb15ebe02011-11-17 17:59:49 -0800267 clear_bit(PORTE_HARD_RESET, &phy->port_events_pending);
James Bottomley2908d772006-08-29 09:22:51 -0500268
Dan Williams90f1e102011-05-24 13:17:53 -0700269 sas_deform_port(phy, 1);
James Bottomley2908d772006-08-29 09:22:51 -0500270}
271
272/* ---------- SAS port registration ---------- */
273
274static void sas_init_port(struct asd_sas_port *port,
275 struct sas_ha_struct *sas_ha, int i)
276{
James Bottomleya29c0512008-02-23 23:38:44 -0600277 memset(port, 0, sizeof(*port));
James Bottomley2908d772006-08-29 09:22:51 -0500278 port->id = i;
279 INIT_LIST_HEAD(&port->dev_list);
280 spin_lock_init(&port->phy_list_lock);
281 INIT_LIST_HEAD(&port->phy_list);
James Bottomley2908d772006-08-29 09:22:51 -0500282 port->ha = sas_ha;
283
284 spin_lock_init(&port->dev_list_lock);
285}
286
287int sas_register_ports(struct sas_ha_struct *sas_ha)
288{
289 int i;
290
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];
294
295 sas_init_port(port, sas_ha, i);
296 sas_init_disc(&port->disc, port);
297 }
298 return 0;
299}
300
301void sas_unregister_ports(struct sas_ha_struct *sas_ha)
302{
303 int i;
304
305 for (i = 0; i < sas_ha->num_phys; i++)
306 if (sas_ha->sas_phy[i]->port)
Dan Williams90f1e102011-05-24 13:17:53 -0700307 sas_deform_port(sas_ha->sas_phy[i], 0);
James Bottomley2908d772006-08-29 09:22:51 -0500308
309}