12908d778SJames Bottomley /* 22908d778SJames Bottomley * Serial Attached SCSI (SAS) Port class 32908d778SJames Bottomley * 42908d778SJames Bottomley * Copyright (C) 2005 Adaptec, Inc. All rights reserved. 52908d778SJames Bottomley * Copyright (C) 2005 Luben Tuikov <luben_tuikov@adaptec.com> 62908d778SJames Bottomley * 72908d778SJames Bottomley * This file is licensed under GPLv2. 82908d778SJames Bottomley * 92908d778SJames Bottomley * This program is free software; you can redistribute it and/or 102908d778SJames Bottomley * modify it under the terms of the GNU General Public License as 112908d778SJames Bottomley * published by the Free Software Foundation; either version 2 of the 122908d778SJames Bottomley * License, or (at your option) any later version. 132908d778SJames Bottomley * 142908d778SJames Bottomley * This program is distributed in the hope that it will be useful, but 152908d778SJames Bottomley * WITHOUT ANY WARRANTY; without even the implied warranty of 162908d778SJames Bottomley * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU 172908d778SJames Bottomley * General Public License for more details. 182908d778SJames Bottomley * 192908d778SJames Bottomley * You should have received a copy of the GNU General Public License 202908d778SJames Bottomley * along with this program; if not, write to the Free Software 212908d778SJames Bottomley * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA 222908d778SJames Bottomley * 232908d778SJames Bottomley */ 242908d778SJames Bottomley 252908d778SJames Bottomley #include "sas_internal.h" 262908d778SJames Bottomley 272908d778SJames Bottomley #include <scsi/scsi_transport.h> 282908d778SJames Bottomley #include <scsi/scsi_transport_sas.h> 292908d778SJames Bottomley #include "../scsi_sas_internal.h" 302908d778SJames Bottomley 3100f0254eSDan Williams static bool phy_is_wideport_member(struct asd_sas_port *port, struct asd_sas_phy *phy) 3200f0254eSDan Williams { 3300f0254eSDan Williams struct sas_ha_struct *sas_ha = phy->ha; 3400f0254eSDan Williams 3500f0254eSDan Williams if (memcmp(port->attached_sas_addr, phy->attached_sas_addr, 3600f0254eSDan Williams SAS_ADDR_SIZE) != 0 || (sas_ha->strict_wide_ports && 3700f0254eSDan Williams memcmp(port->sas_addr, phy->sas_addr, SAS_ADDR_SIZE) != 0)) 3800f0254eSDan Williams return false; 3900f0254eSDan Williams return true; 4000f0254eSDan Williams } 4100f0254eSDan Williams 42303694eeSDan Williams static void sas_resume_port(struct asd_sas_phy *phy) 43303694eeSDan Williams { 44303694eeSDan Williams struct domain_device *dev; 45303694eeSDan Williams struct asd_sas_port *port = phy->port; 46303694eeSDan Williams struct sas_ha_struct *sas_ha = phy->ha; 47303694eeSDan Williams struct sas_internal *si = to_sas_internal(sas_ha->core.shost->transportt); 48303694eeSDan Williams 49303694eeSDan Williams if (si->dft->lldd_port_formed) 50303694eeSDan Williams si->dft->lldd_port_formed(phy); 51303694eeSDan Williams 52303694eeSDan Williams if (port->suspended) 53303694eeSDan Williams port->suspended = 0; 54303694eeSDan Williams else { 55303694eeSDan Williams /* we only need to handle "link returned" actions once */ 56303694eeSDan Williams return; 57303694eeSDan Williams } 58303694eeSDan Williams 59303694eeSDan Williams /* if the port came back: 60303694eeSDan Williams * 1/ presume every device came back 61303694eeSDan Williams * 2/ force the next revalidation to check all expander phys 62303694eeSDan Williams */ 63303694eeSDan Williams list_for_each_entry(dev, &port->dev_list, dev_list_node) { 64303694eeSDan Williams int i, rc; 65303694eeSDan Williams 66303694eeSDan Williams rc = sas_notify_lldd_dev_found(dev); 67303694eeSDan Williams if (rc) { 68303694eeSDan Williams sas_unregister_dev(port, dev); 69303694eeSDan Williams continue; 70303694eeSDan Williams } 71303694eeSDan Williams 72303694eeSDan Williams if (dev->dev_type == EDGE_DEV || dev->dev_type == FANOUT_DEV) { 73303694eeSDan Williams dev->ex_dev.ex_change_count = -1; 74303694eeSDan Williams for (i = 0; i < dev->ex_dev.num_phys; i++) { 75303694eeSDan Williams struct ex_phy *phy = &dev->ex_dev.ex_phy[i]; 76303694eeSDan Williams 77303694eeSDan Williams phy->phy_change_count = -1; 78303694eeSDan Williams } 79303694eeSDan Williams } 80303694eeSDan Williams } 81303694eeSDan Williams 82303694eeSDan Williams sas_discover_event(port, DISCE_RESUME); 83303694eeSDan Williams } 84303694eeSDan Williams 852908d778SJames Bottomley /** 862908d778SJames Bottomley * sas_form_port -- add this phy to a port 872908d778SJames Bottomley * @phy: the phy of interest 882908d778SJames Bottomley * 892908d778SJames Bottomley * This function adds this phy to an existing port, thus creating a wide 902908d778SJames Bottomley * port, or it creates a port and adds the phy to the port. 912908d778SJames Bottomley */ 922908d778SJames Bottomley static void sas_form_port(struct asd_sas_phy *phy) 932908d778SJames Bottomley { 942908d778SJames Bottomley int i; 952908d778SJames Bottomley struct sas_ha_struct *sas_ha = phy->ha; 962908d778SJames Bottomley struct asd_sas_port *port = phy->port; 972908d778SJames Bottomley struct sas_internal *si = 982908d778SJames Bottomley to_sas_internal(sas_ha->core.shost->transportt); 99980fa2f9SDarrick J. Wong unsigned long flags; 1002908d778SJames Bottomley 1012908d778SJames Bottomley if (port) { 10200f0254eSDan Williams if (!phy_is_wideport_member(port, phy)) 10390f1e10dSDan Williams sas_deform_port(phy, 0); 104303694eeSDan Williams else if (phy->suspended) { 105303694eeSDan Williams phy->suspended = 0; 106303694eeSDan Williams sas_resume_port(phy); 107303694eeSDan Williams 108303694eeSDan Williams /* phy came back, try to cancel the timeout */ 109303694eeSDan Williams wake_up(&sas_ha->eh_wait_q); 110303694eeSDan Williams return; 111303694eeSDan Williams } else { 1122908d778SJames Bottomley SAS_DPRINTK("%s: phy%d belongs to port%d already(%d)!\n", 113cadbd4a5SHarvey Harrison __func__, phy->id, phy->port->id, 1142908d778SJames Bottomley phy->port->num_phys); 1152908d778SJames Bottomley return; 1162908d778SJames Bottomley } 1172908d778SJames Bottomley } 1182908d778SJames Bottomley 1195381837fSTom Peng /* see if the phy should be part of a wide port */ 120980fa2f9SDarrick J. Wong spin_lock_irqsave(&sas_ha->phy_port_lock, flags); 1212908d778SJames Bottomley for (i = 0; i < sas_ha->num_phys; i++) { 1222908d778SJames Bottomley port = sas_ha->sas_port[i]; 1232908d778SJames Bottomley spin_lock(&port->phy_list_lock); 1242908d778SJames Bottomley if (*(u64 *) port->sas_addr && 12500f0254eSDan Williams phy_is_wideport_member(port, phy) && port->num_phys > 0) { 1262908d778SJames Bottomley /* wide port */ 1272908d778SJames Bottomley SAS_DPRINTK("phy%d matched wide port%d\n", phy->id, 1282908d778SJames Bottomley port->id); 1292908d778SJames Bottomley break; 1305381837fSTom Peng } 1315381837fSTom Peng spin_unlock(&port->phy_list_lock); 1325381837fSTom Peng } 1335381837fSTom Peng /* The phy does not match any existing port, create a new one */ 1345381837fSTom Peng if (i == sas_ha->num_phys) { 1355381837fSTom Peng for (i = 0; i < sas_ha->num_phys; i++) { 1365381837fSTom Peng port = sas_ha->sas_port[i]; 1375381837fSTom Peng spin_lock(&port->phy_list_lock); 1385381837fSTom Peng if (*(u64 *)port->sas_addr == 0 1395381837fSTom Peng && port->num_phys == 0) { 1405381837fSTom Peng memcpy(port->sas_addr, phy->sas_addr, 1415381837fSTom Peng SAS_ADDR_SIZE); 1422908d778SJames Bottomley break; 1432908d778SJames Bottomley } 1442908d778SJames Bottomley spin_unlock(&port->phy_list_lock); 1452908d778SJames Bottomley } 1465381837fSTom Peng } 1472908d778SJames Bottomley 1482908d778SJames Bottomley if (i >= sas_ha->num_phys) { 1492908d778SJames Bottomley printk(KERN_NOTICE "%s: couldn't find a free port, bug?\n", 150cadbd4a5SHarvey Harrison __func__); 151980fa2f9SDarrick J. Wong spin_unlock_irqrestore(&sas_ha->phy_port_lock, flags); 1522908d778SJames Bottomley return; 1532908d778SJames Bottomley } 1542908d778SJames Bottomley 1552908d778SJames Bottomley /* add the phy to the port */ 1562908d778SJames Bottomley list_add_tail(&phy->port_phy_el, &port->phy_list); 157899fcf40SDan Williams sas_phy_set_target(phy, port->port_dev); 1582908d778SJames Bottomley phy->port = port; 1592908d778SJames Bottomley port->num_phys++; 1602908d778SJames Bottomley port->phy_mask |= (1U << phy->id); 1612908d778SJames Bottomley 1622908d778SJames Bottomley if (*(u64 *)port->attached_sas_addr == 0) { 1632908d778SJames Bottomley port->class = phy->class; 1642908d778SJames Bottomley memcpy(port->attached_sas_addr, phy->attached_sas_addr, 1652908d778SJames Bottomley SAS_ADDR_SIZE); 1662908d778SJames Bottomley port->iproto = phy->iproto; 1672908d778SJames Bottomley port->tproto = phy->tproto; 1682908d778SJames Bottomley port->oob_mode = phy->oob_mode; 1692908d778SJames Bottomley port->linkrate = phy->linkrate; 1702908d778SJames Bottomley } else 1712908d778SJames Bottomley port->linkrate = max(port->linkrate, phy->linkrate); 1722908d778SJames Bottomley spin_unlock(&port->phy_list_lock); 173980fa2f9SDarrick J. Wong spin_unlock_irqrestore(&sas_ha->phy_port_lock, flags); 1742908d778SJames Bottomley 1752908d778SJames Bottomley if (!port->port) { 176b4698d88SDan Williams port->port = sas_port_alloc(phy->phy->dev.parent, port->id); 1772908d778SJames Bottomley BUG_ON(!port->port); 1782908d778SJames Bottomley sas_port_add(port->port); 1792908d778SJames Bottomley } 1802908d778SJames Bottomley sas_port_add_phy(port->port, phy->phy); 1812908d778SJames Bottomley 182a29c0515SJames Bottomley SAS_DPRINTK("%s added to %s, phy_mask:0x%x (%16llx)\n", 18371610f55SKay Sievers dev_name(&phy->phy->dev), dev_name(&port->port->dev), 184a29c0515SJames Bottomley port->phy_mask, 185a29c0515SJames Bottomley SAS_ADDR(port->attached_sas_addr)); 186a29c0515SJames Bottomley 1872908d778SJames Bottomley if (port->port_dev) 1882908d778SJames Bottomley port->port_dev->pathways = port->num_phys; 1892908d778SJames Bottomley 1902908d778SJames Bottomley /* Tell the LLDD about this port formation. */ 1912908d778SJames Bottomley if (si->dft->lldd_port_formed) 1922908d778SJames Bottomley si->dft->lldd_port_formed(phy); 1932908d778SJames Bottomley 1942908d778SJames Bottomley sas_discover_event(phy->port, DISCE_DISCOVER_DOMAIN); 1952908d778SJames Bottomley } 1962908d778SJames Bottomley 1972908d778SJames Bottomley /** 1982908d778SJames Bottomley * sas_deform_port -- remove this phy from the port it belongs to 1992908d778SJames Bottomley * @phy: the phy of interest 2002908d778SJames Bottomley * 2012908d778SJames Bottomley * This is called when the physical link to the other phy has been 2022908d778SJames Bottomley * lost (on this phy), in Event thread context. We cannot delay here. 2032908d778SJames Bottomley */ 20490f1e10dSDan Williams void sas_deform_port(struct asd_sas_phy *phy, int gone) 2052908d778SJames Bottomley { 2062908d778SJames Bottomley struct sas_ha_struct *sas_ha = phy->ha; 2072908d778SJames Bottomley struct asd_sas_port *port = phy->port; 2082908d778SJames Bottomley struct sas_internal *si = 2092908d778SJames Bottomley to_sas_internal(sas_ha->core.shost->transportt); 21090f1e10dSDan Williams struct domain_device *dev; 211980fa2f9SDarrick J. Wong unsigned long flags; 2122908d778SJames Bottomley 2132908d778SJames Bottomley if (!port) 2142908d778SJames Bottomley return; /* done by a phy event */ 2152908d778SJames Bottomley 21690f1e10dSDan Williams dev = port->port_dev; 21790f1e10dSDan Williams if (dev) 21890f1e10dSDan Williams dev->pathways--; 2192908d778SJames Bottomley 2202908d778SJames Bottomley if (port->num_phys == 1) { 2217d05919aSDan Williams sas_unregister_domain_devices(port, gone); 2222908d778SJames Bottomley sas_port_delete(port->port); 2232908d778SJames Bottomley port->port = NULL; 224f41a0c44SDan Williams } else { 2252908d778SJames Bottomley sas_port_delete_phy(port->port, phy->phy); 226f41a0c44SDan Williams sas_device_set_phy(dev, port->port); 227f41a0c44SDan Williams } 2282908d778SJames Bottomley 2292908d778SJames Bottomley if (si->dft->lldd_port_deformed) 2302908d778SJames Bottomley si->dft->lldd_port_deformed(phy); 2312908d778SJames Bottomley 232980fa2f9SDarrick J. Wong spin_lock_irqsave(&sas_ha->phy_port_lock, flags); 2332908d778SJames Bottomley spin_lock(&port->phy_list_lock); 2342908d778SJames Bottomley 2352908d778SJames Bottomley list_del_init(&phy->port_phy_el); 236899fcf40SDan Williams sas_phy_set_target(phy, NULL); 2372908d778SJames Bottomley phy->port = NULL; 2382908d778SJames Bottomley port->num_phys--; 2392908d778SJames Bottomley port->phy_mask &= ~(1U << phy->id); 2402908d778SJames Bottomley 2412908d778SJames Bottomley if (port->num_phys == 0) { 2422908d778SJames Bottomley INIT_LIST_HEAD(&port->phy_list); 2432908d778SJames Bottomley memset(port->sas_addr, 0, SAS_ADDR_SIZE); 2442908d778SJames Bottomley memset(port->attached_sas_addr, 0, SAS_ADDR_SIZE); 2452908d778SJames Bottomley port->class = 0; 2462908d778SJames Bottomley port->iproto = 0; 2472908d778SJames Bottomley port->tproto = 0; 2482908d778SJames Bottomley port->oob_mode = 0; 2492908d778SJames Bottomley port->phy_mask = 0; 2502908d778SJames Bottomley } 2512908d778SJames Bottomley spin_unlock(&port->phy_list_lock); 252980fa2f9SDarrick J. Wong spin_unlock_irqrestore(&sas_ha->phy_port_lock, flags); 2532908d778SJames Bottomley 2542908d778SJames Bottomley return; 2552908d778SJames Bottomley } 2562908d778SJames Bottomley 2572908d778SJames Bottomley /* ---------- SAS port events ---------- */ 2582908d778SJames Bottomley 259c4028958SDavid Howells void sas_porte_bytes_dmaed(struct work_struct *work) 2602908d778SJames Bottomley { 26122b9153fSDan Williams struct asd_sas_event *ev = to_asd_sas_event(work); 262c4028958SDavid Howells struct asd_sas_phy *phy = ev->phy; 2632908d778SJames Bottomley 264b15ebe0bSDan Williams clear_bit(PORTE_BYTES_DMAED, &phy->port_events_pending); 2652908d778SJames Bottomley 2662908d778SJames Bottomley sas_form_port(phy); 2672908d778SJames Bottomley } 2682908d778SJames Bottomley 269c4028958SDavid Howells void sas_porte_broadcast_rcvd(struct work_struct *work) 2702908d778SJames Bottomley { 27122b9153fSDan Williams struct asd_sas_event *ev = to_asd_sas_event(work); 272c4028958SDavid Howells struct asd_sas_phy *phy = ev->phy; 2732908d778SJames Bottomley unsigned long flags; 2742908d778SJames Bottomley u32 prim; 2752908d778SJames Bottomley 276b15ebe0bSDan Williams clear_bit(PORTE_BROADCAST_RCVD, &phy->port_events_pending); 2772908d778SJames Bottomley 2782908d778SJames Bottomley spin_lock_irqsave(&phy->sas_prim_lock, flags); 2792908d778SJames Bottomley prim = phy->sas_prim; 2802908d778SJames Bottomley spin_unlock_irqrestore(&phy->sas_prim_lock, flags); 2812908d778SJames Bottomley 2822908d778SJames Bottomley SAS_DPRINTK("broadcast received: %d\n", prim); 2832908d778SJames Bottomley sas_discover_event(phy->port, DISCE_REVALIDATE_DOMAIN); 2842908d778SJames Bottomley } 2852908d778SJames Bottomley 286c4028958SDavid Howells void sas_porte_link_reset_err(struct work_struct *work) 2872908d778SJames Bottomley { 28822b9153fSDan Williams struct asd_sas_event *ev = to_asd_sas_event(work); 289c4028958SDavid Howells struct asd_sas_phy *phy = ev->phy; 2902908d778SJames Bottomley 291b15ebe0bSDan Williams clear_bit(PORTE_LINK_RESET_ERR, &phy->port_events_pending); 2922908d778SJames Bottomley 29390f1e10dSDan Williams sas_deform_port(phy, 1); 2942908d778SJames Bottomley } 2952908d778SJames Bottomley 296c4028958SDavid Howells void sas_porte_timer_event(struct work_struct *work) 2972908d778SJames Bottomley { 29822b9153fSDan Williams struct asd_sas_event *ev = to_asd_sas_event(work); 299c4028958SDavid Howells struct asd_sas_phy *phy = ev->phy; 3002908d778SJames Bottomley 301b15ebe0bSDan Williams clear_bit(PORTE_TIMER_EVENT, &phy->port_events_pending); 3022908d778SJames Bottomley 30390f1e10dSDan Williams sas_deform_port(phy, 1); 3042908d778SJames Bottomley } 3052908d778SJames Bottomley 306c4028958SDavid Howells void sas_porte_hard_reset(struct work_struct *work) 3072908d778SJames Bottomley { 30822b9153fSDan Williams struct asd_sas_event *ev = to_asd_sas_event(work); 309c4028958SDavid Howells struct asd_sas_phy *phy = ev->phy; 3102908d778SJames Bottomley 311b15ebe0bSDan Williams clear_bit(PORTE_HARD_RESET, &phy->port_events_pending); 3122908d778SJames Bottomley 31390f1e10dSDan Williams sas_deform_port(phy, 1); 3142908d778SJames Bottomley } 3152908d778SJames Bottomley 3162908d778SJames Bottomley /* ---------- SAS port registration ---------- */ 3172908d778SJames Bottomley 3182908d778SJames Bottomley static void sas_init_port(struct asd_sas_port *port, 3192908d778SJames Bottomley struct sas_ha_struct *sas_ha, int i) 3202908d778SJames Bottomley { 321a29c0515SJames Bottomley memset(port, 0, sizeof(*port)); 3222908d778SJames Bottomley port->id = i; 3232908d778SJames Bottomley INIT_LIST_HEAD(&port->dev_list); 32487c8331fSDan Williams INIT_LIST_HEAD(&port->disco_list); 32587c8331fSDan Williams INIT_LIST_HEAD(&port->destroy_list); 3262908d778SJames Bottomley spin_lock_init(&port->phy_list_lock); 3272908d778SJames Bottomley INIT_LIST_HEAD(&port->phy_list); 3282908d778SJames Bottomley port->ha = sas_ha; 3292908d778SJames Bottomley 3302908d778SJames Bottomley spin_lock_init(&port->dev_list_lock); 3312908d778SJames Bottomley } 3322908d778SJames Bottomley 3332908d778SJames Bottomley int sas_register_ports(struct sas_ha_struct *sas_ha) 3342908d778SJames Bottomley { 3352908d778SJames Bottomley int i; 3362908d778SJames Bottomley 3372908d778SJames Bottomley /* initialize the ports and discovery */ 3382908d778SJames Bottomley for (i = 0; i < sas_ha->num_phys; i++) { 3392908d778SJames Bottomley struct asd_sas_port *port = sas_ha->sas_port[i]; 3402908d778SJames Bottomley 3412908d778SJames Bottomley sas_init_port(port, sas_ha, i); 3422908d778SJames Bottomley sas_init_disc(&port->disc, port); 3432908d778SJames Bottomley } 3442908d778SJames Bottomley return 0; 3452908d778SJames Bottomley } 3462908d778SJames Bottomley 3472908d778SJames Bottomley void sas_unregister_ports(struct sas_ha_struct *sas_ha) 3482908d778SJames Bottomley { 3492908d778SJames Bottomley int i; 3502908d778SJames Bottomley 3512908d778SJames Bottomley for (i = 0; i < sas_ha->num_phys; i++) 3522908d778SJames Bottomley if (sas_ha->sas_phy[i]->port) 35390f1e10dSDan Williams sas_deform_port(sas_ha->sas_phy[i], 0); 3542908d778SJames Bottomley 3552908d778SJames Bottomley } 356