James Bottomley | 2908d77 | 2006-08-29 09:22:51 -0500 | [diff] [blame] | 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 | |
Dan Williams | 00f0254 | 2010-10-01 13:55:52 -0700 | [diff] [blame] | 31 | static 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 Bottomley | 2908d77 | 2006-08-29 09:22:51 -0500 | [diff] [blame] | 42 | /** |
| 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 | */ |
| 49 | static 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. Wong | 980fa2f | 2007-01-11 14:15:40 -0800 | [diff] [blame] | 56 | unsigned long flags; |
James Bottomley | 2908d77 | 2006-08-29 09:22:51 -0500 | [diff] [blame] | 57 | |
| 58 | if (port) { |
Dan Williams | 00f0254 | 2010-10-01 13:55:52 -0700 | [diff] [blame] | 59 | if (!phy_is_wideport_member(port, phy)) |
Dan Williams | 90f1e10 | 2011-05-24 13:17:53 -0700 | [diff] [blame] | 60 | sas_deform_port(phy, 0); |
James Bottomley | 2908d77 | 2006-08-29 09:22:51 -0500 | [diff] [blame] | 61 | else { |
| 62 | SAS_DPRINTK("%s: phy%d belongs to port%d already(%d)!\n", |
Harvey Harrison | cadbd4a | 2008-07-03 23:47:27 -0700 | [diff] [blame] | 63 | __func__, phy->id, phy->port->id, |
James Bottomley | 2908d77 | 2006-08-29 09:22:51 -0500 | [diff] [blame] | 64 | phy->port->num_phys); |
| 65 | return; |
| 66 | } |
| 67 | } |
| 68 | |
Tom Peng | 5381837 | 2009-07-01 20:37:26 +0800 | [diff] [blame] | 69 | /* see if the phy should be part of a wide port */ |
Darrick J. Wong | 980fa2f | 2007-01-11 14:15:40 -0800 | [diff] [blame] | 70 | spin_lock_irqsave(&sas_ha->phy_port_lock, flags); |
James Bottomley | 2908d77 | 2006-08-29 09:22:51 -0500 | [diff] [blame] | 71 | 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 Williams | 00f0254 | 2010-10-01 13:55:52 -0700 | [diff] [blame] | 75 | phy_is_wideport_member(port, phy) && port->num_phys > 0) { |
James Bottomley | 2908d77 | 2006-08-29 09:22:51 -0500 | [diff] [blame] | 76 | /* wide port */ |
| 77 | SAS_DPRINTK("phy%d matched wide port%d\n", phy->id, |
| 78 | port->id); |
| 79 | break; |
James Bottomley | 2908d77 | 2006-08-29 09:22:51 -0500 | [diff] [blame] | 80 | } |
| 81 | spin_unlock(&port->phy_list_lock); |
| 82 | } |
Tom Peng | 5381837 | 2009-07-01 20:37:26 +0800 | [diff] [blame] | 83 | /* 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 Bottomley | 2908d77 | 2006-08-29 09:22:51 -0500 | [diff] [blame] | 97 | |
| 98 | if (i >= sas_ha->num_phys) { |
| 99 | printk(KERN_NOTICE "%s: couldn't find a free port, bug?\n", |
Harvey Harrison | cadbd4a | 2008-07-03 23:47:27 -0700 | [diff] [blame] | 100 | __func__); |
Darrick J. Wong | 980fa2f | 2007-01-11 14:15:40 -0800 | [diff] [blame] | 101 | spin_unlock_irqrestore(&sas_ha->phy_port_lock, flags); |
James Bottomley | 2908d77 | 2006-08-29 09:22:51 -0500 | [diff] [blame] | 102 | return; |
| 103 | } |
| 104 | |
| 105 | /* add the phy to the port */ |
| 106 | list_add_tail(&phy->port_phy_el, &port->phy_list); |
Dan Williams | 899fcf4 | 2012-01-28 17:24:40 -0800 | [diff] [blame] | 107 | sas_phy_set_target(phy, port->port_dev); |
James Bottomley | 2908d77 | 2006-08-29 09:22:51 -0500 | [diff] [blame] | 108 | phy->port = port; |
| 109 | port->num_phys++; |
| 110 | port->phy_mask |= (1U << phy->id); |
| 111 | |
James Bottomley | 2908d77 | 2006-08-29 09:22:51 -0500 | [diff] [blame] | 112 | if (*(u64 *)port->attached_sas_addr == 0) { |
| 113 | port->class = phy->class; |
| 114 | memcpy(port->attached_sas_addr, phy->attached_sas_addr, |
| 115 | SAS_ADDR_SIZE); |
| 116 | port->iproto = phy->iproto; |
| 117 | port->tproto = phy->tproto; |
| 118 | port->oob_mode = phy->oob_mode; |
| 119 | port->linkrate = phy->linkrate; |
| 120 | } else |
| 121 | port->linkrate = max(port->linkrate, phy->linkrate); |
| 122 | spin_unlock(&port->phy_list_lock); |
Darrick J. Wong | 980fa2f | 2007-01-11 14:15:40 -0800 | [diff] [blame] | 123 | spin_unlock_irqrestore(&sas_ha->phy_port_lock, flags); |
James Bottomley | 2908d77 | 2006-08-29 09:22:51 -0500 | [diff] [blame] | 124 | |
| 125 | if (!port->port) { |
Dan Williams | b4698d88 | 2012-04-19 23:48:12 -0700 | [diff] [blame] | 126 | port->port = sas_port_alloc(phy->phy->dev.parent, port->id); |
James Bottomley | 2908d77 | 2006-08-29 09:22:51 -0500 | [diff] [blame] | 127 | BUG_ON(!port->port); |
| 128 | sas_port_add(port->port); |
| 129 | } |
| 130 | sas_port_add_phy(port->port, phy->phy); |
| 131 | |
James Bottomley | a29c051 | 2008-02-23 23:38:44 -0600 | [diff] [blame] | 132 | SAS_DPRINTK("%s added to %s, phy_mask:0x%x (%16llx)\n", |
Kay Sievers | 71610f5 | 2008-12-03 22:41:36 +0100 | [diff] [blame] | 133 | dev_name(&phy->phy->dev), dev_name(&port->port->dev), |
James Bottomley | a29c051 | 2008-02-23 23:38:44 -0600 | [diff] [blame] | 134 | port->phy_mask, |
| 135 | SAS_ADDR(port->attached_sas_addr)); |
| 136 | |
James Bottomley | 2908d77 | 2006-08-29 09:22:51 -0500 | [diff] [blame] | 137 | if (port->port_dev) |
| 138 | port->port_dev->pathways = port->num_phys; |
| 139 | |
| 140 | /* Tell the LLDD about this port formation. */ |
| 141 | if (si->dft->lldd_port_formed) |
| 142 | si->dft->lldd_port_formed(phy); |
| 143 | |
| 144 | sas_discover_event(phy->port, DISCE_DISCOVER_DOMAIN); |
| 145 | } |
| 146 | |
| 147 | /** |
| 148 | * sas_deform_port -- remove this phy from the port it belongs to |
| 149 | * @phy: the phy of interest |
| 150 | * |
| 151 | * This is called when the physical link to the other phy has been |
| 152 | * lost (on this phy), in Event thread context. We cannot delay here. |
| 153 | */ |
Dan Williams | 90f1e10 | 2011-05-24 13:17:53 -0700 | [diff] [blame] | 154 | void sas_deform_port(struct asd_sas_phy *phy, int gone) |
James Bottomley | 2908d77 | 2006-08-29 09:22:51 -0500 | [diff] [blame] | 155 | { |
| 156 | struct sas_ha_struct *sas_ha = phy->ha; |
| 157 | struct asd_sas_port *port = phy->port; |
| 158 | struct sas_internal *si = |
| 159 | to_sas_internal(sas_ha->core.shost->transportt); |
Dan Williams | 90f1e10 | 2011-05-24 13:17:53 -0700 | [diff] [blame] | 160 | struct domain_device *dev; |
Darrick J. Wong | 980fa2f | 2007-01-11 14:15:40 -0800 | [diff] [blame] | 161 | unsigned long flags; |
James Bottomley | 2908d77 | 2006-08-29 09:22:51 -0500 | [diff] [blame] | 162 | |
| 163 | if (!port) |
| 164 | return; /* done by a phy event */ |
| 165 | |
Dan Williams | 90f1e10 | 2011-05-24 13:17:53 -0700 | [diff] [blame] | 166 | dev = port->port_dev; |
| 167 | if (dev) |
| 168 | dev->pathways--; |
James Bottomley | 2908d77 | 2006-08-29 09:22:51 -0500 | [diff] [blame] | 169 | |
| 170 | if (port->num_phys == 1) { |
Dan Williams | 7d05919 | 2012-01-10 14:39:13 -0800 | [diff] [blame] | 171 | sas_unregister_domain_devices(port, gone); |
James Bottomley | 2908d77 | 2006-08-29 09:22:51 -0500 | [diff] [blame] | 172 | sas_port_delete(port->port); |
| 173 | port->port = NULL; |
Dan Williams | f41a0c4 | 2011-12-21 21:33:17 -0800 | [diff] [blame] | 174 | } else { |
James Bottomley | 2908d77 | 2006-08-29 09:22:51 -0500 | [diff] [blame] | 175 | sas_port_delete_phy(port->port, phy->phy); |
Dan Williams | f41a0c4 | 2011-12-21 21:33:17 -0800 | [diff] [blame] | 176 | sas_device_set_phy(dev, port->port); |
| 177 | } |
James Bottomley | 2908d77 | 2006-08-29 09:22:51 -0500 | [diff] [blame] | 178 | |
James Bottomley | 2908d77 | 2006-08-29 09:22:51 -0500 | [diff] [blame] | 179 | if (si->dft->lldd_port_deformed) |
| 180 | si->dft->lldd_port_deformed(phy); |
| 181 | |
Darrick J. Wong | 980fa2f | 2007-01-11 14:15:40 -0800 | [diff] [blame] | 182 | spin_lock_irqsave(&sas_ha->phy_port_lock, flags); |
James Bottomley | 2908d77 | 2006-08-29 09:22:51 -0500 | [diff] [blame] | 183 | spin_lock(&port->phy_list_lock); |
| 184 | |
| 185 | list_del_init(&phy->port_phy_el); |
Dan Williams | 899fcf4 | 2012-01-28 17:24:40 -0800 | [diff] [blame] | 186 | sas_phy_set_target(phy, NULL); |
James Bottomley | 2908d77 | 2006-08-29 09:22:51 -0500 | [diff] [blame] | 187 | phy->port = NULL; |
| 188 | port->num_phys--; |
| 189 | port->phy_mask &= ~(1U << phy->id); |
| 190 | |
| 191 | if (port->num_phys == 0) { |
| 192 | INIT_LIST_HEAD(&port->phy_list); |
| 193 | memset(port->sas_addr, 0, SAS_ADDR_SIZE); |
| 194 | memset(port->attached_sas_addr, 0, SAS_ADDR_SIZE); |
| 195 | port->class = 0; |
| 196 | port->iproto = 0; |
| 197 | port->tproto = 0; |
| 198 | port->oob_mode = 0; |
| 199 | port->phy_mask = 0; |
| 200 | } |
| 201 | spin_unlock(&port->phy_list_lock); |
Darrick J. Wong | 980fa2f | 2007-01-11 14:15:40 -0800 | [diff] [blame] | 202 | spin_unlock_irqrestore(&sas_ha->phy_port_lock, flags); |
James Bottomley | 2908d77 | 2006-08-29 09:22:51 -0500 | [diff] [blame] | 203 | |
| 204 | return; |
| 205 | } |
| 206 | |
| 207 | /* ---------- SAS port events ---------- */ |
| 208 | |
David Howells | c402895 | 2006-11-22 14:57:56 +0000 | [diff] [blame] | 209 | void sas_porte_bytes_dmaed(struct work_struct *work) |
James Bottomley | 2908d77 | 2006-08-29 09:22:51 -0500 | [diff] [blame] | 210 | { |
Dan Williams | 22b9153 | 2012-03-09 11:00:06 -0800 | [diff] [blame] | 211 | struct asd_sas_event *ev = to_asd_sas_event(work); |
David Howells | c402895 | 2006-11-22 14:57:56 +0000 | [diff] [blame] | 212 | struct asd_sas_phy *phy = ev->phy; |
James Bottomley | 2908d77 | 2006-08-29 09:22:51 -0500 | [diff] [blame] | 213 | |
Dan Williams | b15ebe0 | 2011-11-17 17:59:49 -0800 | [diff] [blame] | 214 | clear_bit(PORTE_BYTES_DMAED, &phy->port_events_pending); |
James Bottomley | 2908d77 | 2006-08-29 09:22:51 -0500 | [diff] [blame] | 215 | |
| 216 | sas_form_port(phy); |
| 217 | } |
| 218 | |
David Howells | c402895 | 2006-11-22 14:57:56 +0000 | [diff] [blame] | 219 | void sas_porte_broadcast_rcvd(struct work_struct *work) |
James Bottomley | 2908d77 | 2006-08-29 09:22:51 -0500 | [diff] [blame] | 220 | { |
Dan Williams | 22b9153 | 2012-03-09 11:00:06 -0800 | [diff] [blame] | 221 | struct asd_sas_event *ev = to_asd_sas_event(work); |
David Howells | c402895 | 2006-11-22 14:57:56 +0000 | [diff] [blame] | 222 | struct asd_sas_phy *phy = ev->phy; |
James Bottomley | 2908d77 | 2006-08-29 09:22:51 -0500 | [diff] [blame] | 223 | unsigned long flags; |
| 224 | u32 prim; |
James Bottomley | 2908d77 | 2006-08-29 09:22:51 -0500 | [diff] [blame] | 225 | |
Dan Williams | b15ebe0 | 2011-11-17 17:59:49 -0800 | [diff] [blame] | 226 | clear_bit(PORTE_BROADCAST_RCVD, &phy->port_events_pending); |
James Bottomley | 2908d77 | 2006-08-29 09:22:51 -0500 | [diff] [blame] | 227 | |
| 228 | spin_lock_irqsave(&phy->sas_prim_lock, flags); |
| 229 | prim = phy->sas_prim; |
| 230 | spin_unlock_irqrestore(&phy->sas_prim_lock, flags); |
| 231 | |
| 232 | SAS_DPRINTK("broadcast received: %d\n", prim); |
| 233 | sas_discover_event(phy->port, DISCE_REVALIDATE_DOMAIN); |
| 234 | } |
| 235 | |
David Howells | c402895 | 2006-11-22 14:57:56 +0000 | [diff] [blame] | 236 | void sas_porte_link_reset_err(struct work_struct *work) |
James Bottomley | 2908d77 | 2006-08-29 09:22:51 -0500 | [diff] [blame] | 237 | { |
Dan Williams | 22b9153 | 2012-03-09 11:00:06 -0800 | [diff] [blame] | 238 | struct asd_sas_event *ev = to_asd_sas_event(work); |
David Howells | c402895 | 2006-11-22 14:57:56 +0000 | [diff] [blame] | 239 | struct asd_sas_phy *phy = ev->phy; |
James Bottomley | 2908d77 | 2006-08-29 09:22:51 -0500 | [diff] [blame] | 240 | |
Dan Williams | b15ebe0 | 2011-11-17 17:59:49 -0800 | [diff] [blame] | 241 | clear_bit(PORTE_LINK_RESET_ERR, &phy->port_events_pending); |
James Bottomley | 2908d77 | 2006-08-29 09:22:51 -0500 | [diff] [blame] | 242 | |
Dan Williams | 90f1e10 | 2011-05-24 13:17:53 -0700 | [diff] [blame] | 243 | sas_deform_port(phy, 1); |
James Bottomley | 2908d77 | 2006-08-29 09:22:51 -0500 | [diff] [blame] | 244 | } |
| 245 | |
David Howells | c402895 | 2006-11-22 14:57:56 +0000 | [diff] [blame] | 246 | void sas_porte_timer_event(struct work_struct *work) |
James Bottomley | 2908d77 | 2006-08-29 09:22:51 -0500 | [diff] [blame] | 247 | { |
Dan Williams | 22b9153 | 2012-03-09 11:00:06 -0800 | [diff] [blame] | 248 | struct asd_sas_event *ev = to_asd_sas_event(work); |
David Howells | c402895 | 2006-11-22 14:57:56 +0000 | [diff] [blame] | 249 | struct asd_sas_phy *phy = ev->phy; |
James Bottomley | 2908d77 | 2006-08-29 09:22:51 -0500 | [diff] [blame] | 250 | |
Dan Williams | b15ebe0 | 2011-11-17 17:59:49 -0800 | [diff] [blame] | 251 | clear_bit(PORTE_TIMER_EVENT, &phy->port_events_pending); |
James Bottomley | 2908d77 | 2006-08-29 09:22:51 -0500 | [diff] [blame] | 252 | |
Dan Williams | 90f1e10 | 2011-05-24 13:17:53 -0700 | [diff] [blame] | 253 | sas_deform_port(phy, 1); |
James Bottomley | 2908d77 | 2006-08-29 09:22:51 -0500 | [diff] [blame] | 254 | } |
| 255 | |
David Howells | c402895 | 2006-11-22 14:57:56 +0000 | [diff] [blame] | 256 | void sas_porte_hard_reset(struct work_struct *work) |
James Bottomley | 2908d77 | 2006-08-29 09:22:51 -0500 | [diff] [blame] | 257 | { |
Dan Williams | 22b9153 | 2012-03-09 11:00:06 -0800 | [diff] [blame] | 258 | struct asd_sas_event *ev = to_asd_sas_event(work); |
David Howells | c402895 | 2006-11-22 14:57:56 +0000 | [diff] [blame] | 259 | struct asd_sas_phy *phy = ev->phy; |
James Bottomley | 2908d77 | 2006-08-29 09:22:51 -0500 | [diff] [blame] | 260 | |
Dan Williams | b15ebe0 | 2011-11-17 17:59:49 -0800 | [diff] [blame] | 261 | clear_bit(PORTE_HARD_RESET, &phy->port_events_pending); |
James Bottomley | 2908d77 | 2006-08-29 09:22:51 -0500 | [diff] [blame] | 262 | |
Dan Williams | 90f1e10 | 2011-05-24 13:17:53 -0700 | [diff] [blame] | 263 | sas_deform_port(phy, 1); |
James Bottomley | 2908d77 | 2006-08-29 09:22:51 -0500 | [diff] [blame] | 264 | } |
| 265 | |
| 266 | /* ---------- SAS port registration ---------- */ |
| 267 | |
| 268 | static void sas_init_port(struct asd_sas_port *port, |
| 269 | struct sas_ha_struct *sas_ha, int i) |
| 270 | { |
James Bottomley | a29c051 | 2008-02-23 23:38:44 -0600 | [diff] [blame] | 271 | memset(port, 0, sizeof(*port)); |
James Bottomley | 2908d77 | 2006-08-29 09:22:51 -0500 | [diff] [blame] | 272 | port->id = i; |
| 273 | INIT_LIST_HEAD(&port->dev_list); |
Dan Williams | 87c8331 | 2011-11-17 17:59:51 -0800 | [diff] [blame] | 274 | INIT_LIST_HEAD(&port->disco_list); |
| 275 | INIT_LIST_HEAD(&port->destroy_list); |
James Bottomley | 2908d77 | 2006-08-29 09:22:51 -0500 | [diff] [blame] | 276 | spin_lock_init(&port->phy_list_lock); |
| 277 | INIT_LIST_HEAD(&port->phy_list); |
James Bottomley | 2908d77 | 2006-08-29 09:22:51 -0500 | [diff] [blame] | 278 | port->ha = sas_ha; |
| 279 | |
| 280 | spin_lock_init(&port->dev_list_lock); |
| 281 | } |
| 282 | |
| 283 | int sas_register_ports(struct sas_ha_struct *sas_ha) |
| 284 | { |
| 285 | int i; |
| 286 | |
| 287 | /* initialize the ports and discovery */ |
| 288 | for (i = 0; i < sas_ha->num_phys; i++) { |
| 289 | struct asd_sas_port *port = sas_ha->sas_port[i]; |
| 290 | |
| 291 | sas_init_port(port, sas_ha, i); |
| 292 | sas_init_disc(&port->disc, port); |
| 293 | } |
| 294 | return 0; |
| 295 | } |
| 296 | |
| 297 | void sas_unregister_ports(struct sas_ha_struct *sas_ha) |
| 298 | { |
| 299 | int i; |
| 300 | |
| 301 | for (i = 0; i < sas_ha->num_phys; i++) |
| 302 | if (sas_ha->sas_phy[i]->port) |
Dan Williams | 90f1e10 | 2011-05-24 13:17:53 -0700 | [diff] [blame] | 303 | sas_deform_port(sas_ha->sas_phy[i], 0); |
James Bottomley | 2908d77 | 2006-08-29 09:22:51 -0500 | [diff] [blame] | 304 | |
| 305 | } |