2 * Serial Attached SCSI (SAS) Port class
4 * Copyright (C) 2005 Adaptec, Inc. All rights reserved.
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 static bool phy_is_wideport_member(struct asd_sas_port *port, struct asd_sas_phy *phy)
33 struct sas_ha_struct *sas_ha = phy->ha;
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))
42 static void sas_resume_port(struct asd_sas_phy *phy)
44 struct domain_device *dev;
45 struct asd_sas_port *port = phy->port;
46 struct sas_ha_struct *sas_ha = phy->ha;
47 struct sas_internal *si = to_sas_internal(sas_ha->core.shost->transportt);
49 if (si->dft->lldd_port_formed)
50 si->dft->lldd_port_formed(phy);
55 /* we only need to handle "link returned" actions once */
59 /* if the port came back:
60 * 1/ presume every device came back
61 * 2/ force the next revalidation to check all expander phys
63 list_for_each_entry(dev, &port->dev_list, dev_list_node) {
66 rc = sas_notify_lldd_dev_found(dev);
68 sas_unregister_dev(port, dev);
69 sas_destruct_devices(port);
73 if (dev->dev_type == SAS_EDGE_EXPANDER_DEVICE || dev->dev_type == SAS_FANOUT_EXPANDER_DEVICE) {
74 dev->ex_dev.ex_change_count = -1;
75 for (i = 0; i < dev->ex_dev.num_phys; i++) {
76 struct ex_phy *phy = &dev->ex_dev.ex_phy[i];
78 phy->phy_change_count = -1;
83 sas_discover_event(port, DISCE_RESUME);
87 * sas_form_port - add this phy to a port
88 * @phy: the phy of interest
90 * This function adds this phy to an existing port, thus creating a wide
91 * port, or it creates a port and adds the phy to the port.
93 static void sas_form_port(struct asd_sas_phy *phy)
96 struct sas_ha_struct *sas_ha = phy->ha;
97 struct asd_sas_port *port = phy->port;
98 struct sas_internal *si =
99 to_sas_internal(sas_ha->core.shost->transportt);
103 if (!phy_is_wideport_member(port, phy))
104 sas_deform_port(phy, 0);
105 else if (phy->suspended) {
107 sas_resume_port(phy);
109 /* phy came back, try to cancel the timeout */
110 wake_up(&sas_ha->eh_wait_q);
113 pr_info("%s: phy%d belongs to port%d already(%d)!\n",
114 __func__, phy->id, phy->port->id,
115 phy->port->num_phys);
120 /* see if the phy should be part of a wide port */
121 spin_lock_irqsave(&sas_ha->phy_port_lock, flags);
122 for (i = 0; i < sas_ha->num_phys; i++) {
123 port = sas_ha->sas_port[i];
124 spin_lock(&port->phy_list_lock);
125 if (*(u64 *) port->sas_addr &&
126 phy_is_wideport_member(port, phy) && port->num_phys > 0) {
128 pr_debug("phy%d matched wide port%d\n", phy->id,
132 spin_unlock(&port->phy_list_lock);
134 /* The phy does not match any existing port, create a new one */
135 if (i == sas_ha->num_phys) {
136 for (i = 0; i < sas_ha->num_phys; i++) {
137 port = sas_ha->sas_port[i];
138 spin_lock(&port->phy_list_lock);
139 if (*(u64 *)port->sas_addr == 0
140 && port->num_phys == 0) {
141 memcpy(port->sas_addr, phy->sas_addr,
145 spin_unlock(&port->phy_list_lock);
149 if (i >= sas_ha->num_phys) {
150 pr_err("%s: couldn't find a free port, bug?\n", __func__);
151 spin_unlock_irqrestore(&sas_ha->phy_port_lock, flags);
155 /* add the phy to the port */
156 list_add_tail(&phy->port_phy_el, &port->phy_list);
157 sas_phy_set_target(phy, port->port_dev);
160 port->phy_mask |= (1U << phy->id);
162 if (*(u64 *)port->attached_sas_addr == 0) {
163 port->class = phy->class;
164 memcpy(port->attached_sas_addr, phy->attached_sas_addr,
166 port->iproto = phy->iproto;
167 port->tproto = phy->tproto;
168 port->oob_mode = phy->oob_mode;
169 port->linkrate = phy->linkrate;
171 port->linkrate = max(port->linkrate, phy->linkrate);
172 spin_unlock(&port->phy_list_lock);
173 spin_unlock_irqrestore(&sas_ha->phy_port_lock, flags);
176 port->port = sas_port_alloc(phy->phy->dev.parent, port->id);
178 sas_port_add(port->port);
180 sas_port_add_phy(port->port, phy->phy);
182 pr_debug("%s added to %s, phy_mask:0x%x (%16llx)\n",
183 dev_name(&phy->phy->dev), dev_name(&port->port->dev),
185 SAS_ADDR(port->attached_sas_addr));
188 port->port_dev->pathways = port->num_phys;
190 /* Tell the LLDD about this port formation. */
191 if (si->dft->lldd_port_formed)
192 si->dft->lldd_port_formed(phy);
194 sas_discover_event(phy->port, DISCE_DISCOVER_DOMAIN);
195 flush_workqueue(sas_ha->disco_q);
199 * sas_deform_port - remove this phy from the port it belongs to
200 * @phy: the phy of interest
201 * @gone: whether or not the PHY is gone
203 * This is called when the physical link to the other phy has been
204 * lost (on this phy), in Event thread context. We cannot delay here.
206 void sas_deform_port(struct asd_sas_phy *phy, int gone)
208 struct sas_ha_struct *sas_ha = phy->ha;
209 struct asd_sas_port *port = phy->port;
210 struct sas_internal *si =
211 to_sas_internal(sas_ha->core.shost->transportt);
212 struct domain_device *dev;
216 return; /* done by a phy event */
218 dev = port->port_dev;
222 if (port->num_phys == 1) {
223 sas_unregister_domain_devices(port, gone);
224 sas_destruct_devices(port);
225 sas_port_delete(port->port);
228 sas_port_delete_phy(port->port, phy->phy);
229 sas_device_set_phy(dev, port->port);
232 if (si->dft->lldd_port_deformed)
233 si->dft->lldd_port_deformed(phy);
235 spin_lock_irqsave(&sas_ha->phy_port_lock, flags);
236 spin_lock(&port->phy_list_lock);
238 list_del_init(&phy->port_phy_el);
239 sas_phy_set_target(phy, NULL);
242 port->phy_mask &= ~(1U << phy->id);
244 if (port->num_phys == 0) {
245 INIT_LIST_HEAD(&port->phy_list);
246 memset(port->sas_addr, 0, SAS_ADDR_SIZE);
247 memset(port->attached_sas_addr, 0, SAS_ADDR_SIZE);
254 spin_unlock(&port->phy_list_lock);
255 spin_unlock_irqrestore(&sas_ha->phy_port_lock, flags);
260 /* ---------- SAS port events ---------- */
262 void sas_porte_bytes_dmaed(struct work_struct *work)
264 struct asd_sas_event *ev = to_asd_sas_event(work);
265 struct asd_sas_phy *phy = ev->phy;
270 void sas_porte_broadcast_rcvd(struct work_struct *work)
272 struct asd_sas_event *ev = to_asd_sas_event(work);
273 struct asd_sas_phy *phy = ev->phy;
277 spin_lock_irqsave(&phy->sas_prim_lock, flags);
278 prim = phy->sas_prim;
279 spin_unlock_irqrestore(&phy->sas_prim_lock, flags);
281 pr_debug("broadcast received: %d\n", prim);
282 sas_discover_event(phy->port, DISCE_REVALIDATE_DOMAIN);
285 flush_workqueue(phy->port->ha->disco_q);
288 void sas_porte_link_reset_err(struct work_struct *work)
290 struct asd_sas_event *ev = to_asd_sas_event(work);
291 struct asd_sas_phy *phy = ev->phy;
293 sas_deform_port(phy, 1);
296 void sas_porte_timer_event(struct work_struct *work)
298 struct asd_sas_event *ev = to_asd_sas_event(work);
299 struct asd_sas_phy *phy = ev->phy;
301 sas_deform_port(phy, 1);
304 void sas_porte_hard_reset(struct work_struct *work)
306 struct asd_sas_event *ev = to_asd_sas_event(work);
307 struct asd_sas_phy *phy = ev->phy;
309 sas_deform_port(phy, 1);
312 /* ---------- SAS port registration ---------- */
314 static void sas_init_port(struct asd_sas_port *port,
315 struct sas_ha_struct *sas_ha, int i)
317 memset(port, 0, sizeof(*port));
319 INIT_LIST_HEAD(&port->dev_list);
320 INIT_LIST_HEAD(&port->disco_list);
321 INIT_LIST_HEAD(&port->destroy_list);
322 INIT_LIST_HEAD(&port->sas_port_del_list);
323 spin_lock_init(&port->phy_list_lock);
324 INIT_LIST_HEAD(&port->phy_list);
327 spin_lock_init(&port->dev_list_lock);
330 int sas_register_ports(struct sas_ha_struct *sas_ha)
334 /* initialize the ports and discovery */
335 for (i = 0; i < sas_ha->num_phys; i++) {
336 struct asd_sas_port *port = sas_ha->sas_port[i];
338 sas_init_port(port, sas_ha, i);
339 sas_init_disc(&port->disc, port);
344 void sas_unregister_ports(struct sas_ha_struct *sas_ha)
348 for (i = 0; i < sas_ha->num_phys; i++)
349 if (sas_ha->sas_phy[i]->port)
350 sas_deform_port(sas_ha->sas_phy[i], 0);
354 const work_func_t sas_port_event_fns[PORT_NUM_EVENTS] = {
355 [PORTE_BYTES_DMAED] = sas_porte_bytes_dmaed,
356 [PORTE_BROADCAST_RCVD] = sas_porte_broadcast_rcvd,
357 [PORTE_LINK_RESET_ERR] = sas_porte_link_reset_err,
358 [PORTE_TIMER_EVENT] = sas_porte_timer_event,
359 [PORTE_HARD_RESET] = sas_porte_hard_reset,