sas_port.c 8.23 KB
Newer Older
James Bottomley's avatar
James Bottomley committed
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30
/*
 * Serial Attached SCSI (SAS) Port class
 *
 * Copyright (C) 2005 Adaptec, Inc.  All rights reserved.
 * Copyright (C) 2005 Luben Tuikov <luben_tuikov@adaptec.com>
 *
 * This file is licensed under GPLv2.
 *
 * This program is free software; you can redistribute it and/or
 * modify it under the terms of the GNU General Public License as
 * published by the Free Software Foundation; either version 2 of the
 * License, or (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful, but
 * WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 * General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program; if not, write to the Free Software
 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA
 *
 */

#include "sas_internal.h"

#include <scsi/scsi_transport.h>
#include <scsi/scsi_transport_sas.h>
#include "../scsi_sas_internal.h"

31 32 33 34 35 36 37 38 39 40 41
static bool phy_is_wideport_member(struct asd_sas_port *port, struct asd_sas_phy *phy)
{
	struct sas_ha_struct *sas_ha = phy->ha;

	if (memcmp(port->attached_sas_addr, phy->attached_sas_addr,
		   SAS_ADDR_SIZE) != 0 || (sas_ha->strict_wide_ports &&
	     memcmp(port->sas_addr, phy->sas_addr, SAS_ADDR_SIZE) != 0))
		return false;
	return true;
}

James Bottomley's avatar
James Bottomley committed
42 43 44 45 46 47 48 49 50 51 52 53 54 55
/**
 * sas_form_port -- add this phy to a port
 * @phy: the phy of interest
 *
 * This function adds this phy to an existing port, thus creating a wide
 * port, or it creates a port and adds the phy to the port.
 */
static void sas_form_port(struct asd_sas_phy *phy)
{
	int i;
	struct sas_ha_struct *sas_ha = phy->ha;
	struct asd_sas_port *port = phy->port;
	struct sas_internal *si =
		to_sas_internal(sas_ha->core.shost->transportt);
56
	unsigned long flags;
James Bottomley's avatar
James Bottomley committed
57 58

	if (port) {
59
		if (!phy_is_wideport_member(port, phy))
60
			sas_deform_port(phy, 0);
James Bottomley's avatar
James Bottomley committed
61 62
		else {
			SAS_DPRINTK("%s: phy%d belongs to port%d already(%d)!\n",
63
				    __func__, phy->id, phy->port->id,
James Bottomley's avatar
James Bottomley committed
64 65 66 67 68
				    phy->port->num_phys);
			return;
		}
	}

69
	/* see if the phy should be part of a wide port */
70
	spin_lock_irqsave(&sas_ha->phy_port_lock, flags);
James Bottomley's avatar
James Bottomley committed
71 72 73 74
	for (i = 0; i < sas_ha->num_phys; i++) {
		port = sas_ha->sas_port[i];
		spin_lock(&port->phy_list_lock);
		if (*(u64 *) port->sas_addr &&
75
		    phy_is_wideport_member(port, phy) && port->num_phys > 0) {
James Bottomley's avatar
James Bottomley committed
76 77 78 79 80 81 82
			/* wide port */
			SAS_DPRINTK("phy%d matched wide port%d\n", phy->id,
				    port->id);
			break;
		}
		spin_unlock(&port->phy_list_lock);
	}
83 84 85 86 87 88 89 90 91 92 93 94 95 96
	/* The phy does not match any existing port, create a new one */
	if (i == sas_ha->num_phys) {
		for (i = 0; i < sas_ha->num_phys; i++) {
			port = sas_ha->sas_port[i];
			spin_lock(&port->phy_list_lock);
			if (*(u64 *)port->sas_addr == 0
				&& port->num_phys == 0) {
				memcpy(port->sas_addr, phy->sas_addr,
					SAS_ADDR_SIZE);
				break;
			}
			spin_unlock(&port->phy_list_lock);
		}
	}
James Bottomley's avatar
James Bottomley committed
97 98 99

	if (i >= sas_ha->num_phys) {
		printk(KERN_NOTICE "%s: couldn't find a free port, bug?\n",
100
		       __func__);
101
		spin_unlock_irqrestore(&sas_ha->phy_port_lock, flags);
James Bottomley's avatar
James Bottomley committed
102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124
		return;
	}

	/* add the phy to the port */
	list_add_tail(&phy->port_phy_el, &port->phy_list);
	phy->port = port;
	port->num_phys++;
	port->phy_mask |= (1U << phy->id);

	if (!port->phy)
		port->phy = phy->phy;

	if (*(u64 *)port->attached_sas_addr == 0) {
		port->class = phy->class;
		memcpy(port->attached_sas_addr, phy->attached_sas_addr,
		       SAS_ADDR_SIZE);
		port->iproto = phy->iproto;
		port->tproto = phy->tproto;
		port->oob_mode = phy->oob_mode;
		port->linkrate = phy->linkrate;
	} else
		port->linkrate = max(port->linkrate, phy->linkrate);
	spin_unlock(&port->phy_list_lock);
125
	spin_unlock_irqrestore(&sas_ha->phy_port_lock, flags);
James Bottomley's avatar
James Bottomley committed
126 127 128 129 130 131 132 133

	if (!port->port) {
		port->port = sas_port_alloc(phy->phy->dev.parent, port->id);
		BUG_ON(!port->port);
		sas_port_add(port->port);
	}
	sas_port_add_phy(port->port, phy->phy);

134
	SAS_DPRINTK("%s added to %s, phy_mask:0x%x (%16llx)\n",
135
		    dev_name(&phy->phy->dev), dev_name(&port->port->dev),
136 137 138
		    port->phy_mask,
		    SAS_ADDR(port->attached_sas_addr));

James Bottomley's avatar
James Bottomley committed
139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155
	if (port->port_dev)
		port->port_dev->pathways = port->num_phys;

	/* Tell the LLDD about this port formation. */
	if (si->dft->lldd_port_formed)
		si->dft->lldd_port_formed(phy);

	sas_discover_event(phy->port, DISCE_DISCOVER_DOMAIN);
}

/**
 * sas_deform_port -- remove this phy from the port it belongs to
 * @phy: the phy of interest
 *
 * This is called when the physical link to the other phy has been
 * lost (on this phy), in Event thread context. We cannot delay here.
 */
156
void sas_deform_port(struct asd_sas_phy *phy, int gone)
James Bottomley's avatar
James Bottomley committed
157 158 159 160 161
{
	struct sas_ha_struct *sas_ha = phy->ha;
	struct asd_sas_port *port = phy->port;
	struct sas_internal *si =
		to_sas_internal(sas_ha->core.shost->transportt);
162
	struct domain_device *dev;
163
	unsigned long flags;
James Bottomley's avatar
James Bottomley committed
164 165 166 167

	if (!port)
		return;		  /* done by a phy event */

168 169 170
	dev = port->port_dev;
	if (dev)
		dev->pathways--;
James Bottomley's avatar
James Bottomley committed
171 172

	if (port->num_phys == 1) {
173 174
		if (dev && gone)
			dev->gone = 1;
James Bottomley's avatar
James Bottomley committed
175 176 177 178 179 180 181 182 183
		sas_unregister_domain_devices(port);
		sas_port_delete(port->port);
		port->port = NULL;
	} else
		sas_port_delete_phy(port->port, phy->phy);

	if (si->dft->lldd_port_deformed)
		si->dft->lldd_port_deformed(phy);

184
	spin_lock_irqsave(&sas_ha->phy_port_lock, flags);
James Bottomley's avatar
James Bottomley committed
185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202
	spin_lock(&port->phy_list_lock);

	list_del_init(&phy->port_phy_el);
	phy->port = NULL;
	port->num_phys--;
	port->phy_mask &= ~(1U << phy->id);

	if (port->num_phys == 0) {
		INIT_LIST_HEAD(&port->phy_list);
		memset(port->sas_addr, 0, SAS_ADDR_SIZE);
		memset(port->attached_sas_addr, 0, SAS_ADDR_SIZE);
		port->class = 0;
		port->iproto = 0;
		port->tproto = 0;
		port->oob_mode = 0;
		port->phy_mask = 0;
	}
	spin_unlock(&port->phy_list_lock);
203
	spin_unlock_irqrestore(&sas_ha->phy_port_lock, flags);
James Bottomley's avatar
James Bottomley committed
204 205 206 207 208 209

	return;
}

/* ---------- SAS port events ---------- */

David Howells's avatar
David Howells committed
210
void sas_porte_bytes_dmaed(struct work_struct *work)
James Bottomley's avatar
James Bottomley committed
211
{
David Howells's avatar
David Howells committed
212 213 214
	struct asd_sas_event *ev =
		container_of(work, struct asd_sas_event, work);
	struct asd_sas_phy *phy = ev->phy;
James Bottomley's avatar
James Bottomley committed
215 216 217 218 219 220 221

	sas_begin_event(PORTE_BYTES_DMAED, &phy->ha->event_lock,
			&phy->port_events_pending);

	sas_form_port(phy);
}

David Howells's avatar
David Howells committed
222
void sas_porte_broadcast_rcvd(struct work_struct *work)
James Bottomley's avatar
James Bottomley committed
223
{
David Howells's avatar
David Howells committed
224 225 226
	struct asd_sas_event *ev =
		container_of(work, struct asd_sas_event, work);
	struct asd_sas_phy *phy = ev->phy;
James Bottomley's avatar
James Bottomley committed
227 228 229 230 231 232 233 234 235 236 237 238 239 240
	unsigned long flags;
	u32 prim;

	sas_begin_event(PORTE_BROADCAST_RCVD, &phy->ha->event_lock,
			&phy->port_events_pending);

	spin_lock_irqsave(&phy->sas_prim_lock, flags);
	prim = phy->sas_prim;
	spin_unlock_irqrestore(&phy->sas_prim_lock, flags);

	SAS_DPRINTK("broadcast received: %d\n", prim);
	sas_discover_event(phy->port, DISCE_REVALIDATE_DOMAIN);
}

David Howells's avatar
David Howells committed
241
void sas_porte_link_reset_err(struct work_struct *work)
James Bottomley's avatar
James Bottomley committed
242
{
David Howells's avatar
David Howells committed
243 244 245
	struct asd_sas_event *ev =
		container_of(work, struct asd_sas_event, work);
	struct asd_sas_phy *phy = ev->phy;
James Bottomley's avatar
James Bottomley committed
246 247 248 249

	sas_begin_event(PORTE_LINK_RESET_ERR, &phy->ha->event_lock,
			&phy->port_events_pending);

250
	sas_deform_port(phy, 1);
James Bottomley's avatar
James Bottomley committed
251 252
}

David Howells's avatar
David Howells committed
253
void sas_porte_timer_event(struct work_struct *work)
James Bottomley's avatar
James Bottomley committed
254
{
David Howells's avatar
David Howells committed
255 256 257
	struct asd_sas_event *ev =
		container_of(work, struct asd_sas_event, work);
	struct asd_sas_phy *phy = ev->phy;
James Bottomley's avatar
James Bottomley committed
258 259 260 261

	sas_begin_event(PORTE_TIMER_EVENT, &phy->ha->event_lock,
			&phy->port_events_pending);

262
	sas_deform_port(phy, 1);
James Bottomley's avatar
James Bottomley committed
263 264
}

David Howells's avatar
David Howells committed
265
void sas_porte_hard_reset(struct work_struct *work)
James Bottomley's avatar
James Bottomley committed
266
{
David Howells's avatar
David Howells committed
267 268 269
	struct asd_sas_event *ev =
		container_of(work, struct asd_sas_event, work);
	struct asd_sas_phy *phy = ev->phy;
James Bottomley's avatar
James Bottomley committed
270 271 272 273

	sas_begin_event(PORTE_HARD_RESET, &phy->ha->event_lock,
			&phy->port_events_pending);

274
	sas_deform_port(phy, 1);
James Bottomley's avatar
James Bottomley committed
275 276 277 278 279 280 281
}

/* ---------- SAS port registration ---------- */

static void sas_init_port(struct asd_sas_port *port,
			  struct sas_ha_struct *sas_ha, int i)
{
282
	memset(port, 0, sizeof(*port));
James Bottomley's avatar
James Bottomley committed
283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311
	port->id = i;
	INIT_LIST_HEAD(&port->dev_list);
	spin_lock_init(&port->phy_list_lock);
	INIT_LIST_HEAD(&port->phy_list);
	port->ha = sas_ha;

	spin_lock_init(&port->dev_list_lock);
}

int sas_register_ports(struct sas_ha_struct *sas_ha)
{
	int i;

	/* initialize the ports and discovery */
	for (i = 0; i < sas_ha->num_phys; i++) {
		struct asd_sas_port *port = sas_ha->sas_port[i];

		sas_init_port(port, sas_ha, i);
		sas_init_disc(&port->disc, port);
	}
	return 0;
}

void sas_unregister_ports(struct sas_ha_struct *sas_ha)
{
	int i;

	for (i = 0; i < sas_ha->num_phys; i++)
		if (sas_ha->sas_phy[i]->port)
312
			sas_deform_port(sas_ha->sas_phy[i], 0);
James Bottomley's avatar
James Bottomley committed
313 314

}