port_config.c 26.1 KB
Newer Older
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 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55
/*
 * This file is provided under a dual BSD/GPLv2 license.  When using or
 * redistributing this file, you may do so under either license.
 *
 * GPL LICENSE SUMMARY
 *
 * Copyright(c) 2008 - 2011 Intel Corporation. All rights reserved.
 *
 * This program is free software; you can redistribute it and/or modify
 * it under the terms of version 2 of the GNU General Public License as
 * published by the Free Software Foundation.
 *
 * 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.
 * The full GNU General Public License is included in this distribution
 * in the file called LICENSE.GPL.
 *
 * BSD LICENSE
 *
 * Copyright(c) 2008 - 2011 Intel Corporation. All rights reserved.
 * All rights reserved.
 *
 * Redistribution and use in source and binary forms, with or without
 * modification, are permitted provided that the following conditions
 * are met:
 *
 *   * Redistributions of source code must retain the above copyright
 *     notice, this list of conditions and the following disclaimer.
 *   * Redistributions in binary form must reproduce the above copyright
 *     notice, this list of conditions and the following disclaimer in
 *     the documentation and/or other materials provided with the
 *     distribution.
 *   * Neither the name of Intel Corporation nor the names of its
 *     contributors may be used to endorse or promote products derived
 *     from this software without specific prior written permission.
 *
 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
 * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
 * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
 * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
 */

56
#include "host.h"
57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110

#define SCIC_SDS_MPC_RECONFIGURATION_TIMEOUT    (10)
#define SCIC_SDS_APC_RECONFIGURATION_TIMEOUT    (10)
#define SCIC_SDS_APC_WAIT_LINK_UP_NOTIFICATION  (100)

enum SCIC_SDS_APC_ACTIVITY {
	SCIC_SDS_APC_SKIP_PHY,
	SCIC_SDS_APC_ADD_PHY,
	SCIC_SDS_APC_START_TIMER,

	SCIC_SDS_APC_ACTIVITY_MAX
};

/*
 * ******************************************************************************
 * General port configuration agent routines
 * ****************************************************************************** */

/**
 *
 * @address_one: A SAS Address to be compared.
 * @address_two: A SAS Address to be compared.
 *
 * Compare the two SAS Address and if SAS Address One is greater than SAS
 * Address Two then return > 0 else if SAS Address One is less than SAS Address
 * Two return < 0 Otherwise they are the same return 0 A signed value of x > 0
 * > y where x is returned for Address One > Address Two y is returned for
 * Address One < Address Two 0 is returned ofr Address One = Address Two
 */
static s32 sci_sas_address_compare(
	struct sci_sas_address address_one,
	struct sci_sas_address address_two)
{
	if (address_one.high > address_two.high) {
		return 1;
	} else if (address_one.high < address_two.high) {
		return -1;
	} else if (address_one.low > address_two.low) {
		return 1;
	} else if (address_one.low < address_two.low) {
		return -1;
	}

	/* The two SAS Address must be identical */
	return 0;
}

/**
 *
 * @controller: The controller object used for the port search.
 * @phy: The phy object to match.
 *
 * This routine will find a matching port for the phy.  This means that the
 * port and phy both have the same broadcast sas address and same received sas
111
 * address. The port address or the NULL if there is no matching
112
 * port. port address if the port can be found to match the phy.
113
 * NULL if there is no matching port for the phy.
114
 */
115
static struct isci_port *scic_sds_port_configuration_agent_find_port(
116
	struct scic_sds_controller *scic,
117
	struct isci_phy *iphy)
118
{
119
	u8 i;
120 121 122 123 124 125 126 127
	struct sci_sas_address port_sas_address;
	struct sci_sas_address port_attached_device_address;
	struct sci_sas_address phy_sas_address;
	struct sci_sas_address phy_attached_device_address;

	/*
	 * Since this phy can be a member of a wide port check to see if one or
	 * more phys match the sent and received SAS address as this phy in which
128 129
	 * case it should participate in the same port.
	 */
130 131
	scic_sds_phy_get_sas_address(iphy, &phy_sas_address);
	scic_sds_phy_get_attached_sas_address(iphy, &phy_attached_device_address);
132

133
	for (i = 0; i < scic->logical_port_entries; i++) {
D
Dan Williams 已提交
134
		struct isci_host *ihost = scic_to_ihost(scic);
135
		struct isci_port *iport = &ihost->ports[i];
136

137 138
		scic_sds_port_get_sas_address(iport, &port_sas_address);
		scic_sds_port_get_attached_sas_address(iport, &port_attached_device_address);
139

D
Dan Williams 已提交
140 141
		if (sci_sas_address_compare(port_sas_address, phy_sas_address) == 0 &&
		    sci_sas_address_compare(port_attached_device_address, phy_attached_device_address) == 0)
142
			return iport;
143 144
	}

145
	return NULL;
146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163
}

/**
 *
 * @controller: This is the controller object that contains the port agent
 * @port_agent: This is the port configruation agent for the controller.
 *
 * This routine will validate the port configuration is correct for the SCU
 * hardware.  The SCU hardware allows for port configurations as follows. LP0
 * -> (PE0), (PE0, PE1), (PE0, PE1, PE2, PE3) LP1 -> (PE1) LP2 -> (PE2), (PE2,
 * PE3) LP3 -> (PE3) enum sci_status SCI_SUCCESS the port configuration is valid for
 * this port configuration agent. SCI_FAILURE_UNSUPPORTED_PORT_CONFIGURATION
 * the port configuration is not valid for this port configuration agent.
 */
static enum sci_status scic_sds_port_configuration_agent_validate_ports(
	struct scic_sds_controller *controller,
	struct scic_sds_port_configuration_agent *port_agent)
{
D
Dan Williams 已提交
164
	struct isci_host *ihost = scic_to_ihost(controller);
165 166 167 168 169 170
	struct sci_sas_address first_address;
	struct sci_sas_address second_address;

	/*
	 * Sanity check the max ranges for all the phys the max index
	 * is always equal to the port range index */
D
Dan Williams 已提交
171 172 173 174
	if (port_agent->phy_valid_port_range[0].max_index != 0 ||
	    port_agent->phy_valid_port_range[1].max_index != 1 ||
	    port_agent->phy_valid_port_range[2].max_index != 2 ||
	    port_agent->phy_valid_port_range[3].max_index != 3)
175 176 177 178 179
		return SCI_FAILURE_UNSUPPORTED_PORT_CONFIGURATION;

	/*
	 * This is a request to configure a single x4 port or at least attempt
	 * to make all the phys into a single port */
D
Dan Williams 已提交
180 181 182 183
	if (port_agent->phy_valid_port_range[0].min_index == 0 &&
	    port_agent->phy_valid_port_range[1].min_index == 0 &&
	    port_agent->phy_valid_port_range[2].min_index == 0 &&
	    port_agent->phy_valid_port_range[3].min_index == 0)
184 185 186 187 188 189 190 191 192 193 194 195 196 197 198
		return SCI_SUCCESS;

	/*
	 * This is a degenerate case where phy 1 and phy 2 are assigned
	 * to the same port this is explicitly disallowed by the hardware
	 * unless they are part of the same x4 port and this condition was
	 * already checked above. */
	if (port_agent->phy_valid_port_range[2].min_index == 1) {
		return SCI_FAILURE_UNSUPPORTED_PORT_CONFIGURATION;
	}

	/*
	 * PE0 and PE3 can never have the same SAS Address unless they
	 * are part of the same x4 wide port and we have already checked
	 * for this condition. */
199 200
	scic_sds_phy_get_sas_address(&ihost->phys[0], &first_address);
	scic_sds_phy_get_sas_address(&ihost->phys[3], &second_address);
201 202 203 204 205 206 207 208 209

	if (sci_sas_address_compare(first_address, second_address) == 0) {
		return SCI_FAILURE_UNSUPPORTED_PORT_CONFIGURATION;
	}

	/*
	 * PE0 and PE1 are configured into a 2x1 ports make sure that the
	 * SAS Address for PE0 and PE2 are different since they can not be
	 * part of the same port. */
D
Dan Williams 已提交
210 211
	if (port_agent->phy_valid_port_range[0].min_index == 0 &&
	    port_agent->phy_valid_port_range[1].min_index == 1) {
212 213
		scic_sds_phy_get_sas_address(&ihost->phys[0], &first_address);
		scic_sds_phy_get_sas_address(&ihost->phys[2], &second_address);
214 215 216 217 218 219 220 221 222 223

		if (sci_sas_address_compare(first_address, second_address) == 0) {
			return SCI_FAILURE_UNSUPPORTED_PORT_CONFIGURATION;
		}
	}

	/*
	 * PE2 and PE3 are configured into a 2x1 ports make sure that the
	 * SAS Address for PE1 and PE3 are different since they can not be
	 * part of the same port. */
D
Dan Williams 已提交
224 225
	if (port_agent->phy_valid_port_range[2].min_index == 2 &&
	    port_agent->phy_valid_port_range[3].min_index == 3) {
226 227
		scic_sds_phy_get_sas_address(&ihost->phys[1], &first_address);
		scic_sds_phy_get_sas_address(&ihost->phys[3], &second_address);
228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251

		if (sci_sas_address_compare(first_address, second_address) == 0) {
			return SCI_FAILURE_UNSUPPORTED_PORT_CONFIGURATION;
		}
	}

	return SCI_SUCCESS;
}

/*
 * ******************************************************************************
 * Manual port configuration agent routines
 * ****************************************************************************** */

/**
 *
 *
 * This routine will verify that all of the phys in the same port are using the
 * same SAS address.
 */
static enum sci_status scic_sds_mpc_agent_validate_phy_configuration(
	struct scic_sds_controller *controller,
	struct scic_sds_port_configuration_agent *port_agent)
{
D
Dan Williams 已提交
252
	struct isci_host *ihost = scic_to_ihost(controller);
253 254 255 256 257 258 259 260 261 262 263 264 265 266
	u32 phy_mask;
	u32 assigned_phy_mask;
	struct sci_sas_address sas_address;
	struct sci_sas_address phy_assigned_address;
	u8 port_index;
	u8 phy_index;

	assigned_phy_mask = 0;
	sas_address.high = 0;
	sas_address.low = 0;

	for (port_index = 0; port_index < SCI_MAX_PORTS; port_index++) {
		phy_mask = controller->oem_parameters.sds1.ports[port_index].phy_mask;

D
Dan Williams 已提交
267 268 269 270 271 272 273 274
		if (!phy_mask)
			continue;
		/*
		 * Make sure that one or more of the phys were not already assinged to
		 * a different port. */
		if ((phy_mask & ~assigned_phy_mask) == 0) {
			return SCI_FAILURE_UNSUPPORTED_PORT_CONFIGURATION;
		}
275

D
Dan Williams 已提交
276 277 278 279
		/* Find the starting phy index for this round through the loop */
		for (phy_index = 0; phy_index < SCI_MAX_PHYS; phy_index++) {
			if ((phy_mask & (1 << phy_index)) == 0)
				continue;
280
			scic_sds_phy_get_sas_address(&ihost->phys[phy_index],
D
Dan Williams 已提交
281
						     &sas_address);
282

D
Dan Williams 已提交
283 284 285 286 287 288
			/*
			 * The phy_index can be used as the starting point for the
			 * port range since the hardware starts all logical ports
			 * the same as the PE index. */
			port_agent->phy_valid_port_range[phy_index].min_index = port_index;
			port_agent->phy_valid_port_range[phy_index].max_index = phy_index;
289

D
Dan Williams 已提交
290 291
			if (phy_index != port_index) {
				return SCI_FAILURE_UNSUPPORTED_PORT_CONFIGURATION;
292 293
			}

D
Dan Williams 已提交
294 295
			break;
		}
296

D
Dan Williams 已提交
297 298 299 300 301 302 303 304
		/*
		 * See how many additional phys are being added to this logical port.
		 * Note: We have not moved the current phy_index so we will actually
		 *       compare the startting phy with itself.
		 *       This is expected and required to add the phy to the port. */
		while (phy_index < SCI_MAX_PHYS) {
			if ((phy_mask & (1 << phy_index)) == 0)
				continue;
305
			scic_sds_phy_get_sas_address(&ihost->phys[phy_index],
D
Dan Williams 已提交
306 307 308 309 310 311 312 313
						     &phy_assigned_address);

			if (sci_sas_address_compare(sas_address, phy_assigned_address) != 0) {
				/*
				 * The phy mask specified that this phy is part of the same port
				 * as the starting phy and it is not so fail this configuration */
				return SCI_FAILURE_UNSUPPORTED_PORT_CONFIGURATION;
			}
314

D
Dan Williams 已提交
315 316
			port_agent->phy_valid_port_range[phy_index].min_index = port_index;
			port_agent->phy_valid_port_range[phy_index].max_index = phy_index;
317

318
			scic_sds_port_add_phy(&ihost->ports[port_index],
319
					      &ihost->phys[phy_index]);
320

D
Dan Williams 已提交
321
			assigned_phy_mask |= (1 << phy_index);
322
		}
D
Dan Williams 已提交
323 324

		phy_index++;
325 326 327 328 329
	}

	return scic_sds_port_configuration_agent_validate_ports(controller, port_agent);
}

330
static void mpc_agent_timeout(unsigned long data)
331 332
{
	u8 index;
333 334 335 336 337
	struct sci_timer *tmr = (struct sci_timer *)data;
	struct scic_sds_port_configuration_agent *port_agent;
	struct scic_sds_controller *scic;
	struct isci_host *ihost;
	unsigned long flags;
338 339
	u16 configure_phy_mask;

340 341 342 343 344 345 346 347 348
	port_agent = container_of(tmr, typeof(*port_agent), timer);
	scic = container_of(port_agent, typeof(*scic), port_agent);
	ihost = scic_to_ihost(scic);

	spin_lock_irqsave(&ihost->scic_lock, flags);

	if (tmr->cancel)
		goto done;

349 350 351 352 353 354
	port_agent->timer_pending = false;

	/* Find the mask of phys that are reported read but as yet unconfigured into a port */
	configure_phy_mask = ~port_agent->phy_configured_mask & port_agent->phy_ready_mask;

	for (index = 0; index < SCI_MAX_PHYS; index++) {
355
		struct isci_phy *iphy = &ihost->phys[index];
D
Dan Williams 已提交
356

357
		if (configure_phy_mask & (1 << index)) {
D
Dan Williams 已提交
358
			port_agent->link_up_handler(scic, port_agent,
359 360
						    phy_get_non_dummy_port(iphy),
						    iphy);
361 362
		}
	}
363 364 365

done:
	spin_unlock_irqrestore(&ihost->scic_lock, flags);
366 367
}

368 369
static void scic_sds_mpc_agent_link_up(struct scic_sds_controller *controller,
				       struct scic_sds_port_configuration_agent *port_agent,
370
				       struct isci_port *iport,
371
				       struct isci_phy *iphy)
372
{
373 374 375
	/* If the port is NULL then the phy was not assigned to a port.
	 * This is because the phy was not given the same SAS Address as
	 * the other PHYs in the port.
376
	 */
377 378
	if (!iport)
		return;
379

380 381 382 383
	port_agent->phy_ready_mask |= (1 << scic_sds_phy_get_index(iphy));
	scic_sds_port_link_up(iport, iphy);
	if ((iport->active_phy_mask & (1 << scic_sds_phy_get_index(iphy))))
		port_agent->phy_configured_mask |= (1 << scic_sds_phy_get_index(iphy));
384 385 386 387 388 389 390
}

/**
 *
 * @controller: This is the controller object that receives the link down
 *    notification.
 * @port: This is the port object associated with the phy.  If the is no
391
 *    associated port this is an NULL.  The port is an invalid
392 393 394 395 396
 *    handle only if the phy was never port of this port.  This happens when
 *    the phy is not broadcasting the same SAS address as the other phys in the
 *    assigned port.
 * @phy: This is the phy object which has gone link down.
 *
397
 * This function handles the manual port configuration link down notifications.
398 399 400 401 402 403
 * Since all ports and phys are associated at initialization time we just turn
 * around and notifiy the port object of the link down event.  If this PHY is
 * not associated with a port there is no action taken. Is it possible to get a
 * link down notification from a phy that has no assocoated port?
 */
static void scic_sds_mpc_agent_link_down(
404
	struct scic_sds_controller *scic,
405
	struct scic_sds_port_configuration_agent *port_agent,
406
	struct isci_port *iport,
407
	struct isci_phy *iphy)
408
{
409
	if (iport != NULL) {
410
		/*
411 412 413 414 415 416 417
		 * If we can form a new port from the remainder of the phys
		 * then we want to start the timer to allow the SCI User to
		 * cleanup old devices and rediscover the port before
		 * rebuilding the port with the phys that remain in the ready
		 * state.
		 */
		port_agent->phy_ready_mask &=
418
			~(1 << scic_sds_phy_get_index(iphy));
419
		port_agent->phy_configured_mask &=
420
			~(1 << scic_sds_phy_get_index(iphy));
421 422

		/*
423 424 425 426 427 428 429 430
		 * Check to see if there are more phys waiting to be
		 * configured into a port. If there are allow the SCI User
		 * to tear down this port, if necessary, and then reconstruct
		 * the port after the timeout.
		 */
		if ((port_agent->phy_configured_mask == 0x0000) &&
		    (port_agent->phy_ready_mask != 0x0000) &&
		    !port_agent->timer_pending) {
431 432
			port_agent->timer_pending = true;

433 434
			sci_mod_timer(&port_agent->timer,
				      SCIC_SDS_MPC_RECONFIGURATION_TIMEOUT);
435 436
		}

437
		scic_sds_port_link_down(iport, iphy);
438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459
	}
}

/*
 * ******************************************************************************
 * Automatic port configuration agent routines
 * ****************************************************************************** */

/**
 *
 *
 * This routine will verify that the phys are assigned a valid SAS address for
 * automatic port configuration mode.
 */
static enum sci_status scic_sds_apc_agent_validate_phy_configuration(
	struct scic_sds_controller *controller,
	struct scic_sds_port_configuration_agent *port_agent)
{
	u8 phy_index;
	u8 port_index;
	struct sci_sas_address sas_address;
	struct sci_sas_address phy_assigned_address;
D
Dan Williams 已提交
460
	struct isci_host *ihost = scic_to_ihost(controller);
461 462 463 464 465 466 467

	phy_index = 0;

	while (phy_index < SCI_MAX_PHYS) {
		port_index = phy_index;

		/* Get the assigned SAS Address for the first PHY on the controller. */
468
		scic_sds_phy_get_sas_address(&ihost->phys[phy_index],
D
Dan Williams 已提交
469
					    &sas_address);
470 471

		while (++phy_index < SCI_MAX_PHYS) {
472
			scic_sds_phy_get_sas_address(&ihost->phys[phy_index],
D
Dan Williams 已提交
473
						     &phy_assigned_address);
474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489

			/* Verify each of the SAS address are all the same for every PHY */
			if (sci_sas_address_compare(sas_address, phy_assigned_address) == 0) {
				port_agent->phy_valid_port_range[phy_index].min_index = port_index;
				port_agent->phy_valid_port_range[phy_index].max_index = phy_index;
			} else {
				port_agent->phy_valid_port_range[phy_index].min_index = phy_index;
				port_agent->phy_valid_port_range[phy_index].max_index = phy_index;
				break;
			}
		}
	}

	return scic_sds_port_configuration_agent_validate_ports(controller, port_agent);
}

490 491 492 493
static void scic_sds_apc_agent_configure_ports(struct scic_sds_controller *controller,
					       struct scic_sds_port_configuration_agent *port_agent,
					       struct isci_phy *iphy,
					       bool start_timer)
494 495 496
{
	u8 port_index;
	enum sci_status status;
497
	struct isci_port *iport;
498
	enum SCIC_SDS_APC_ACTIVITY apc_activity = SCIC_SDS_APC_SKIP_PHY;
D
Dan Williams 已提交
499
	struct isci_host *ihost = scic_to_ihost(controller);
500

501
	iport = scic_sds_port_configuration_agent_find_port(controller, iphy);
502

503 504
	if (iport) {
		if (scic_sds_port_is_valid_phy_assignment(iport, iphy->phy_index))
505 506 507 508 509 510 511 512 513 514
			apc_activity = SCIC_SDS_APC_ADD_PHY;
		else
			apc_activity = SCIC_SDS_APC_SKIP_PHY;
	} else {
		/*
		 * There is no matching Port for this PHY so lets search through the
		 * Ports and see if we can add the PHY to its own port or maybe start
		 * the timer and wait to see if a wider port can be made.
		 *
		 * Note the break when we reach the condition of the port id == phy id */
515 516 517
		for (port_index = port_agent->phy_valid_port_range[iphy->phy_index].min_index;
		     port_index <= port_agent->phy_valid_port_range[iphy->phy_index].max_index;
		     port_index++) {
518

519
			iport = &ihost->ports[port_index];
520 521

			/* First we must make sure that this PHY can be added to this Port. */
522
			if (scic_sds_port_is_valid_phy_assignment(iport, iphy->phy_index)) {
523 524 525 526
				/*
				 * Port contains a PHY with a greater PHY ID than the current
				 * PHY that has gone link up.  This phy can not be part of any
				 * port so skip it and move on. */
527
				if (iport->active_phy_mask > (1 << iphy->phy_index)) {
528 529 530 531 532 533 534 535
					apc_activity = SCIC_SDS_APC_SKIP_PHY;
					break;
				}

				/*
				 * We have reached the end of our Port list and have not found
				 * any reason why we should not either add the PHY to the port
				 * or wait for more phys to become active. */
536
				if (iport->physical_port_index == iphy->phy_index) {
537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552
					/*
					 * The Port either has no active PHYs.
					 * Consider that if the port had any active PHYs we would have
					 * or active PHYs with
					 * a lower PHY Id than this PHY. */
					if (apc_activity != SCIC_SDS_APC_START_TIMER) {
						apc_activity = SCIC_SDS_APC_ADD_PHY;
					}

					break;
				}

				/*
				 * The current Port has no active PHYs and this PHY could be part
				 * of this Port.  Since we dont know as yet setup to start the
				 * timer and see if there is a better configuration. */
553
				if (iport->active_phy_mask == 0) {
554 555
					apc_activity = SCIC_SDS_APC_START_TIMER;
				}
556
			} else if (iport->active_phy_mask != 0) {
557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581
				/*
				 * The Port has an active phy and the current Phy can not
				 * participate in this port so skip the PHY and see if
				 * there is a better configuration. */
				apc_activity = SCIC_SDS_APC_SKIP_PHY;
			}
		}
	}

	/*
	 * Check to see if the start timer operations should instead map to an
	 * add phy operation.  This is caused because we have been waiting to
	 * add a phy to a port but could not becuase the automatic port
	 * configuration engine had a choice of possible ports for the phy.
	 * Since we have gone through a timeout we are going to restrict the
	 * choice to the smallest possible port. */
	if (
		(start_timer == false)
		&& (apc_activity == SCIC_SDS_APC_START_TIMER)
		) {
		apc_activity = SCIC_SDS_APC_ADD_PHY;
	}

	switch (apc_activity) {
	case SCIC_SDS_APC_ADD_PHY:
582
		status = scic_sds_port_add_phy(iport, iphy);
583 584

		if (status == SCI_SUCCESS) {
585
			port_agent->phy_configured_mask |= (1 << iphy->phy_index);
586 587 588 589
		}
		break;

	case SCIC_SDS_APC_START_TIMER:
590 591 592 593 594 595 596 597 598 599 600
		/*
		 * This can occur for either a link down event, or a link
		 * up event where we cannot yet tell the port to which a
		 * phy belongs.
		 */
		if (port_agent->timer_pending)
			sci_del_timer(&port_agent->timer);

		port_agent->timer_pending = true;
		sci_mod_timer(&port_agent->timer,
			      SCIC_SDS_APC_WAIT_LINK_UP_NOTIFICATION);
601 602 603 604 605 606 607 608 609 610
		break;

	case SCIC_SDS_APC_SKIP_PHY:
	default:
		/* do nothing the PHY can not be made part of a port at this time. */
		break;
	}
}

/**
611 612
 * scic_sds_apc_agent_link_up - handle apc link up events
 * @scic: This is the controller object that receives the link up
613
 *    notification.
614
 * @sci_port: This is the port object associated with the phy.  If the is no
615
 *    associated port this is an NULL.
616
 * @sci_phy: This is the phy object which has gone link up.
617 618 619 620 621
 *
 * This method handles the automatic port configuration for link up
 * notifications. Is it possible to get a link down notification from a phy
 * that has no assocoated port?
 */
622 623
static void scic_sds_apc_agent_link_up(struct scic_sds_controller *scic,
				       struct scic_sds_port_configuration_agent *port_agent,
624
				       struct isci_port *iport,
625
				       struct isci_phy *iphy)
626
{
627
	u8 phy_index  = iphy->phy_index;
628

629
	if (!iport) {
630 631
		/* the phy is not the part of this port */
		port_agent->phy_ready_mask |= 1 << phy_index;
632
		scic_sds_apc_agent_configure_ports(scic, port_agent, iphy, true);
633 634
	} else {
		/* the phy is already the part of the port */
635
		u32 port_state = iport->sm.current_state_id;
636 637 638 639 640

		/* if the PORT'S state is resetting then the link up is from
		 * port hard reset in this case, we need to tell the port
		 * that link up is recieved
		 */
E
Edmund Nadolski 已提交
641
		BUG_ON(port_state != SCI_PORT_RESETTING);
642
		port_agent->phy_ready_mask |= 1 << phy_index;
643
		scic_sds_port_link_up(iport, iphy);
644
	}
645 646 647 648 649 650
}

/**
 *
 * @controller: This is the controller object that receives the link down
 *    notification.
651
 * @iport: This is the port object associated with the phy.  If the is no
652
 *    associated port this is an NULL.
653
 * @iphy: This is the phy object which has gone link down.
654 655 656 657 658 659 660 661 662
 *
 * This method handles the automatic port configuration link down
 * notifications. not associated with a port there is no action taken. Is it
 * possible to get a link down notification from a phy that has no assocoated
 * port?
 */
static void scic_sds_apc_agent_link_down(
	struct scic_sds_controller *controller,
	struct scic_sds_port_configuration_agent *port_agent,
663
	struct isci_port *iport,
664
	struct isci_phy *iphy)
665
{
666
	port_agent->phy_ready_mask &= ~(1 << scic_sds_phy_get_index(iphy));
667

668 669 670 671
	if (!iport)
		return;
	if (port_agent->phy_configured_mask & (1 << iphy->phy_index)) {
		enum sci_status status;
672

673
		status = scic_sds_port_remove_phy(iport, iphy);
674

675 676
		if (status == SCI_SUCCESS)
			port_agent->phy_configured_mask &= ~(1 << iphy->phy_index);
677 678 679
	}
}

D
Dan Williams 已提交
680
/* configure the phys into ports when the timer fires */
681
static void apc_agent_timeout(unsigned long data)
682 683
{
	u32 index;
684
	struct sci_timer *tmr = (struct sci_timer *)data;
685
	struct scic_sds_port_configuration_agent *port_agent;
686 687 688
	struct scic_sds_controller *scic;
	struct isci_host *ihost;
	unsigned long flags;
689 690
	u16 configure_phy_mask;

691 692 693 694 695 696 697 698
	port_agent = container_of(tmr, typeof(*port_agent), timer);
	scic = container_of(port_agent, typeof(*scic), port_agent);
	ihost = scic_to_ihost(scic);

	spin_lock_irqsave(&ihost->scic_lock, flags);

	if (tmr->cancel)
		goto done;
699 700 701 702 703

	port_agent->timer_pending = false;

	configure_phy_mask = ~port_agent->phy_configured_mask & port_agent->phy_ready_mask;

D
Dan Williams 已提交
704 705 706 707 708 709 710 711
	if (!configure_phy_mask)
		return;

	for (index = 0; index < SCI_MAX_PHYS; index++) {
		if ((configure_phy_mask & (1 << index)) == 0)
			continue;

		scic_sds_apc_agent_configure_ports(scic, port_agent,
712
						   &ihost->phys[index], false);
713
	}
714 715 716

done:
	spin_unlock_irqrestore(&ihost->scic_lock, flags);
717 718 719 720 721 722 723 724 725 726 727 728 729 730 731 732 733 734 735 736 737 738 739 740 741 742 743 744 745 746 747 748 749 750
}

/*
 * ******************************************************************************
 * Public port configuration agent routines
 * ****************************************************************************** */

/**
 *
 *
 * This method will construct the port configuration agent for operation. This
 * call is universal for both manual port configuration and automatic port
 * configuration modes.
 */
void scic_sds_port_configuration_agent_construct(
	struct scic_sds_port_configuration_agent *port_agent)
{
	u32 index;

	port_agent->phy_configured_mask = 0x00;
	port_agent->phy_ready_mask = 0x00;

	port_agent->link_up_handler = NULL;
	port_agent->link_down_handler = NULL;

	port_agent->timer_pending = false;

	for (index = 0; index < SCI_MAX_PORTS; index++) {
		port_agent->phy_valid_port_range[index].min_index = 0;
		port_agent->phy_valid_port_range[index].max_index = 0;
	}
}

enum sci_status scic_sds_port_configuration_agent_initialize(
751
	struct scic_sds_controller *scic,
752 753
	struct scic_sds_port_configuration_agent *port_agent)
{
754
	enum sci_status status;
D
Dave Jiang 已提交
755
	enum scic_port_configuration_mode mode;
756

757
	mode = scic->oem_parameters.sds1.controller.mode_type;
758 759

	if (mode == SCIC_PORT_MANUAL_CONFIGURATION_MODE) {
760 761
		status = scic_sds_mpc_agent_validate_phy_configuration(
				scic, port_agent);
762 763 764 765

		port_agent->link_up_handler = scic_sds_mpc_agent_link_up;
		port_agent->link_down_handler = scic_sds_mpc_agent_link_down;

766
		sci_init_timer(&port_agent->timer, mpc_agent_timeout);
767
	} else {
768 769
		status = scic_sds_apc_agent_validate_phy_configuration(
				scic, port_agent);
770 771 772 773

		port_agent->link_up_handler = scic_sds_apc_agent_link_up;
		port_agent->link_down_handler = scic_sds_apc_agent_link_down;

774
		sci_init_timer(&port_agent->timer, apc_agent_timeout);
775 776 777 778
	}

	return status;
}