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"
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)
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);
47 if (memcmp(port->attached_sas_addr, phy->attached_sas_addr,
51 SAS_DPRINTK("%s: phy%d belongs to port%d already(%d)!\n",
52 __FUNCTION__, phy->id, phy->port->id,
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 &&
68 SAS_DPRINTK("phy%d matched wide port%d\n", phy->id,
71 } else if (*(u64 *) port->sas_addr == 0 && port->num_phys==0) {
72 memcpy(port->sas_addr, phy->sas_addr, SAS_ADDR_SIZE);
75 spin_unlock(&port->phy_list_lock);
78 if (i >= sas_ha->num_phys) {
79 printk(KERN_NOTICE "%s: couldn't find a free port, bug?\n",
81 spin_unlock(&sas_ha->phy_port_lock);
85 /* add the phy to the port */
86 list_add_tail(&phy->port_phy_el, &port->phy_list);
89 port->phy_mask |= (1U << phy->id);
94 SAS_DPRINTK("phy%d added to port%d, phy_mask:0x%x\n", phy->id,
95 port->id, port->phy_mask);
97 if (*(u64 *)port->attached_sas_addr == 0) {
98 port->class = phy->class;
99 memcpy(port->attached_sas_addr, phy->attached_sas_addr,
101 port->iproto = phy->iproto;
102 port->tproto = phy->tproto;
103 port->oob_mode = phy->oob_mode;
104 port->linkrate = phy->linkrate;
106 port->linkrate = max(port->linkrate, phy->linkrate);
107 spin_unlock(&port->phy_list_lock);
108 spin_unlock(&sas_ha->phy_port_lock);
111 port->port = sas_port_alloc(phy->phy->dev.parent, port->id);
113 sas_port_add(port->port);
115 sas_port_add_phy(port->port, phy->phy);
118 port->port_dev->pathways = port->num_phys;
120 /* Tell the LLDD about this port formation. */
121 if (si->dft->lldd_port_formed)
122 si->dft->lldd_port_formed(phy);
124 sas_discover_event(phy->port, DISCE_DISCOVER_DOMAIN);
128 * sas_deform_port -- remove this phy from the port it belongs to
129 * @phy: the phy of interest
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.
134 void sas_deform_port(struct asd_sas_phy *phy)
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);
142 return; /* done by a phy event */
145 port->port_dev->pathways--;
147 if (port->num_phys == 1) {
148 sas_unregister_domain_devices(port);
149 sas_port_delete(port->port);
152 sas_port_delete_phy(port->port, phy->phy);
155 if (si->dft->lldd_port_deformed)
156 si->dft->lldd_port_deformed(phy);
158 spin_lock(&sas_ha->phy_port_lock);
159 spin_lock(&port->phy_list_lock);
161 list_del_init(&phy->port_phy_el);
164 port->phy_mask &= ~(1U << phy->id);
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);
176 spin_unlock(&port->phy_list_lock);
177 spin_unlock(&sas_ha->phy_port_lock);
182 /* ---------- SAS port events ---------- */
184 void sas_porte_bytes_dmaed(struct work_struct *work)
186 struct asd_sas_event *ev =
187 container_of(work, struct asd_sas_event, work);
188 struct asd_sas_phy *phy = ev->phy;
190 sas_begin_event(PORTE_BYTES_DMAED, &phy->ha->event_lock,
191 &phy->port_events_pending);
196 void sas_porte_broadcast_rcvd(struct work_struct *work)
198 struct asd_sas_event *ev =
199 container_of(work, struct asd_sas_event, work);
200 struct asd_sas_phy *phy = ev->phy;
204 sas_begin_event(PORTE_BROADCAST_RCVD, &phy->ha->event_lock,
205 &phy->port_events_pending);
207 spin_lock_irqsave(&phy->sas_prim_lock, flags);
208 prim = phy->sas_prim;
209 spin_unlock_irqrestore(&phy->sas_prim_lock, flags);
211 SAS_DPRINTK("broadcast received: %d\n", prim);
212 sas_discover_event(phy->port, DISCE_REVALIDATE_DOMAIN);
215 void sas_porte_link_reset_err(struct work_struct *work)
217 struct asd_sas_event *ev =
218 container_of(work, struct asd_sas_event, work);
219 struct asd_sas_phy *phy = ev->phy;
221 sas_begin_event(PORTE_LINK_RESET_ERR, &phy->ha->event_lock,
222 &phy->port_events_pending);
224 sas_deform_port(phy);
227 void sas_porte_timer_event(struct work_struct *work)
229 struct asd_sas_event *ev =
230 container_of(work, struct asd_sas_event, work);
231 struct asd_sas_phy *phy = ev->phy;
233 sas_begin_event(PORTE_TIMER_EVENT, &phy->ha->event_lock,
234 &phy->port_events_pending);
236 sas_deform_port(phy);
239 void sas_porte_hard_reset(struct work_struct *work)
241 struct asd_sas_event *ev =
242 container_of(work, struct asd_sas_event, work);
243 struct asd_sas_phy *phy = ev->phy;
245 sas_begin_event(PORTE_HARD_RESET, &phy->ha->event_lock,
246 &phy->port_events_pending);
248 sas_deform_port(phy);
251 /* ---------- SAS port registration ---------- */
253 static void sas_init_port(struct asd_sas_port *port,
254 struct sas_ha_struct *sas_ha, int i)
257 INIT_LIST_HEAD(&port->dev_list);
258 spin_lock_init(&port->phy_list_lock);
259 INIT_LIST_HEAD(&port->phy_list);
264 spin_lock_init(&port->dev_list_lock);
267 int sas_register_ports(struct sas_ha_struct *sas_ha)
271 /* initialize the ports and discovery */
272 for (i = 0; i < sas_ha->num_phys; i++) {
273 struct asd_sas_port *port = sas_ha->sas_port[i];
275 sas_init_port(port, sas_ha, i);
276 sas_init_disc(&port->disc, port);
281 void sas_unregister_ports(struct sas_ha_struct *sas_ha)
285 for (i = 0; i < sas_ha->num_phys; i++)
286 if (sas_ha->sas_phy[i]->port)
287 sas_deform_port(sas_ha->sas_phy[i]);