port_config.c 25 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761
  1. /*
  2. * This file is provided under a dual BSD/GPLv2 license. When using or
  3. * redistributing this file, you may do so under either license.
  4. *
  5. * GPL LICENSE SUMMARY
  6. *
  7. * Copyright(c) 2008 - 2011 Intel Corporation. All rights reserved.
  8. *
  9. * This program is free software; you can redistribute it and/or modify
  10. * it under the terms of version 2 of the GNU General Public License as
  11. * published by the Free Software Foundation.
  12. *
  13. * This program is distributed in the hope that it will be useful, but
  14. * WITHOUT ANY WARRANTY; without even the implied warranty of
  15. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  16. * General Public License for more details.
  17. *
  18. * You should have received a copy of the GNU General Public License
  19. * along with this program; if not, write to the Free Software
  20. * Foundation, Inc., 51 Franklin St - Fifth Floor, Boston, MA 02110-1301 USA.
  21. * The full GNU General Public License is included in this distribution
  22. * in the file called LICENSE.GPL.
  23. *
  24. * BSD LICENSE
  25. *
  26. * Copyright(c) 2008 - 2011 Intel Corporation. All rights reserved.
  27. * All rights reserved.
  28. *
  29. * Redistribution and use in source and binary forms, with or without
  30. * modification, are permitted provided that the following conditions
  31. * are met:
  32. *
  33. * * Redistributions of source code must retain the above copyright
  34. * notice, this list of conditions and the following disclaimer.
  35. * * Redistributions in binary form must reproduce the above copyright
  36. * notice, this list of conditions and the following disclaimer in
  37. * the documentation and/or other materials provided with the
  38. * distribution.
  39. * * Neither the name of Intel Corporation nor the names of its
  40. * contributors may be used to endorse or promote products derived
  41. * from this software without specific prior written permission.
  42. *
  43. * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
  44. * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
  45. * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
  46. * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
  47. * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
  48. * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
  49. * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
  50. * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
  51. * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
  52. * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
  53. * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  54. */
  55. #include "host.h"
  56. #define SCIC_SDS_MPC_RECONFIGURATION_TIMEOUT (10)
  57. #define SCIC_SDS_APC_RECONFIGURATION_TIMEOUT (10)
  58. #define SCIC_SDS_APC_WAIT_LINK_UP_NOTIFICATION (1000)
  59. enum SCIC_SDS_APC_ACTIVITY {
  60. SCIC_SDS_APC_SKIP_PHY,
  61. SCIC_SDS_APC_ADD_PHY,
  62. SCIC_SDS_APC_START_TIMER,
  63. SCIC_SDS_APC_ACTIVITY_MAX
  64. };
  65. /*
  66. * ******************************************************************************
  67. * General port configuration agent routines
  68. * ****************************************************************************** */
  69. /**
  70. *
  71. * @address_one: A SAS Address to be compared.
  72. * @address_two: A SAS Address to be compared.
  73. *
  74. * Compare the two SAS Address and if SAS Address One is greater than SAS
  75. * Address Two then return > 0 else if SAS Address One is less than SAS Address
  76. * Two return < 0 Otherwise they are the same return 0 A signed value of x > 0
  77. * > y where x is returned for Address One > Address Two y is returned for
  78. * Address One < Address Two 0 is returned ofr Address One = Address Two
  79. */
  80. static s32 sci_sas_address_compare(
  81. struct sci_sas_address address_one,
  82. struct sci_sas_address address_two)
  83. {
  84. if (address_one.high > address_two.high) {
  85. return 1;
  86. } else if (address_one.high < address_two.high) {
  87. return -1;
  88. } else if (address_one.low > address_two.low) {
  89. return 1;
  90. } else if (address_one.low < address_two.low) {
  91. return -1;
  92. }
  93. /* The two SAS Address must be identical */
  94. return 0;
  95. }
  96. /**
  97. *
  98. * @controller: The controller object used for the port search.
  99. * @phy: The phy object to match.
  100. *
  101. * This routine will find a matching port for the phy. This means that the
  102. * port and phy both have the same broadcast sas address and same received sas
  103. * address. The port address or the NULL if there is no matching
  104. * port. port address if the port can be found to match the phy.
  105. * NULL if there is no matching port for the phy.
  106. */
  107. static struct isci_port *sci_port_configuration_agent_find_port(
  108. struct isci_host *ihost,
  109. struct isci_phy *iphy)
  110. {
  111. u8 i;
  112. struct sci_sas_address port_sas_address;
  113. struct sci_sas_address port_attached_device_address;
  114. struct sci_sas_address phy_sas_address;
  115. struct sci_sas_address phy_attached_device_address;
  116. /*
  117. * Since this phy can be a member of a wide port check to see if one or
  118. * more phys match the sent and received SAS address as this phy in which
  119. * case it should participate in the same port.
  120. */
  121. sci_phy_get_sas_address(iphy, &phy_sas_address);
  122. sci_phy_get_attached_sas_address(iphy, &phy_attached_device_address);
  123. for (i = 0; i < ihost->logical_port_entries; i++) {
  124. struct isci_port *iport = &ihost->ports[i];
  125. sci_port_get_sas_address(iport, &port_sas_address);
  126. sci_port_get_attached_sas_address(iport, &port_attached_device_address);
  127. if (sci_sas_address_compare(port_sas_address, phy_sas_address) == 0 &&
  128. sci_sas_address_compare(port_attached_device_address, phy_attached_device_address) == 0)
  129. return iport;
  130. }
  131. return NULL;
  132. }
  133. /**
  134. *
  135. * @controller: This is the controller object that contains the port agent
  136. * @port_agent: This is the port configruation agent for the controller.
  137. *
  138. * This routine will validate the port configuration is correct for the SCU
  139. * hardware. The SCU hardware allows for port configurations as follows. LP0
  140. * -> (PE0), (PE0, PE1), (PE0, PE1, PE2, PE3) LP1 -> (PE1) LP2 -> (PE2), (PE2,
  141. * PE3) LP3 -> (PE3) enum sci_status SCI_SUCCESS the port configuration is valid for
  142. * this port configuration agent. SCI_FAILURE_UNSUPPORTED_PORT_CONFIGURATION
  143. * the port configuration is not valid for this port configuration agent.
  144. */
  145. static enum sci_status sci_port_configuration_agent_validate_ports(
  146. struct isci_host *ihost,
  147. struct sci_port_configuration_agent *port_agent)
  148. {
  149. struct sci_sas_address first_address;
  150. struct sci_sas_address second_address;
  151. /*
  152. * Sanity check the max ranges for all the phys the max index
  153. * is always equal to the port range index */
  154. if (port_agent->phy_valid_port_range[0].max_index != 0 ||
  155. port_agent->phy_valid_port_range[1].max_index != 1 ||
  156. port_agent->phy_valid_port_range[2].max_index != 2 ||
  157. port_agent->phy_valid_port_range[3].max_index != 3)
  158. return SCI_FAILURE_UNSUPPORTED_PORT_CONFIGURATION;
  159. /*
  160. * This is a request to configure a single x4 port or at least attempt
  161. * to make all the phys into a single port */
  162. if (port_agent->phy_valid_port_range[0].min_index == 0 &&
  163. port_agent->phy_valid_port_range[1].min_index == 0 &&
  164. port_agent->phy_valid_port_range[2].min_index == 0 &&
  165. port_agent->phy_valid_port_range[3].min_index == 0)
  166. return SCI_SUCCESS;
  167. /*
  168. * This is a degenerate case where phy 1 and phy 2 are assigned
  169. * to the same port this is explicitly disallowed by the hardware
  170. * unless they are part of the same x4 port and this condition was
  171. * already checked above. */
  172. if (port_agent->phy_valid_port_range[2].min_index == 1) {
  173. return SCI_FAILURE_UNSUPPORTED_PORT_CONFIGURATION;
  174. }
  175. /*
  176. * PE0 and PE3 can never have the same SAS Address unless they
  177. * are part of the same x4 wide port and we have already checked
  178. * for this condition. */
  179. sci_phy_get_sas_address(&ihost->phys[0], &first_address);
  180. sci_phy_get_sas_address(&ihost->phys[3], &second_address);
  181. if (sci_sas_address_compare(first_address, second_address) == 0) {
  182. return SCI_FAILURE_UNSUPPORTED_PORT_CONFIGURATION;
  183. }
  184. /*
  185. * PE0 and PE1 are configured into a 2x1 ports make sure that the
  186. * SAS Address for PE0 and PE2 are different since they can not be
  187. * part of the same port. */
  188. if (port_agent->phy_valid_port_range[0].min_index == 0 &&
  189. port_agent->phy_valid_port_range[1].min_index == 1) {
  190. sci_phy_get_sas_address(&ihost->phys[0], &first_address);
  191. sci_phy_get_sas_address(&ihost->phys[2], &second_address);
  192. if (sci_sas_address_compare(first_address, second_address) == 0) {
  193. return SCI_FAILURE_UNSUPPORTED_PORT_CONFIGURATION;
  194. }
  195. }
  196. /*
  197. * PE2 and PE3 are configured into a 2x1 ports make sure that the
  198. * SAS Address for PE1 and PE3 are different since they can not be
  199. * part of the same port. */
  200. if (port_agent->phy_valid_port_range[2].min_index == 2 &&
  201. port_agent->phy_valid_port_range[3].min_index == 3) {
  202. sci_phy_get_sas_address(&ihost->phys[1], &first_address);
  203. sci_phy_get_sas_address(&ihost->phys[3], &second_address);
  204. if (sci_sas_address_compare(first_address, second_address) == 0) {
  205. return SCI_FAILURE_UNSUPPORTED_PORT_CONFIGURATION;
  206. }
  207. }
  208. return SCI_SUCCESS;
  209. }
  210. /*
  211. * ******************************************************************************
  212. * Manual port configuration agent routines
  213. * ****************************************************************************** */
  214. /* verify all of the phys in the same port are using the same SAS address */
  215. static enum sci_status
  216. sci_mpc_agent_validate_phy_configuration(struct isci_host *ihost,
  217. struct sci_port_configuration_agent *port_agent)
  218. {
  219. u32 phy_mask;
  220. u32 assigned_phy_mask;
  221. struct sci_sas_address sas_address;
  222. struct sci_sas_address phy_assigned_address;
  223. u8 port_index;
  224. u8 phy_index;
  225. assigned_phy_mask = 0;
  226. sas_address.high = 0;
  227. sas_address.low = 0;
  228. for (port_index = 0; port_index < SCI_MAX_PORTS; port_index++) {
  229. phy_mask = ihost->oem_parameters.ports[port_index].phy_mask;
  230. if (!phy_mask)
  231. continue;
  232. /*
  233. * Make sure that one or more of the phys were not already assinged to
  234. * a different port. */
  235. if ((phy_mask & ~assigned_phy_mask) == 0) {
  236. return SCI_FAILURE_UNSUPPORTED_PORT_CONFIGURATION;
  237. }
  238. /* Find the starting phy index for this round through the loop */
  239. for (phy_index = 0; phy_index < SCI_MAX_PHYS; phy_index++) {
  240. if ((phy_mask & (1 << phy_index)) == 0)
  241. continue;
  242. sci_phy_get_sas_address(&ihost->phys[phy_index],
  243. &sas_address);
  244. /*
  245. * The phy_index can be used as the starting point for the
  246. * port range since the hardware starts all logical ports
  247. * the same as the PE index. */
  248. port_agent->phy_valid_port_range[phy_index].min_index = port_index;
  249. port_agent->phy_valid_port_range[phy_index].max_index = phy_index;
  250. if (phy_index != port_index) {
  251. return SCI_FAILURE_UNSUPPORTED_PORT_CONFIGURATION;
  252. }
  253. break;
  254. }
  255. /*
  256. * See how many additional phys are being added to this logical port.
  257. * Note: We have not moved the current phy_index so we will actually
  258. * compare the startting phy with itself.
  259. * This is expected and required to add the phy to the port. */
  260. while (phy_index < SCI_MAX_PHYS) {
  261. if ((phy_mask & (1 << phy_index)) == 0)
  262. continue;
  263. sci_phy_get_sas_address(&ihost->phys[phy_index],
  264. &phy_assigned_address);
  265. if (sci_sas_address_compare(sas_address, phy_assigned_address) != 0) {
  266. /*
  267. * The phy mask specified that this phy is part of the same port
  268. * as the starting phy and it is not so fail this configuration */
  269. return SCI_FAILURE_UNSUPPORTED_PORT_CONFIGURATION;
  270. }
  271. port_agent->phy_valid_port_range[phy_index].min_index = port_index;
  272. port_agent->phy_valid_port_range[phy_index].max_index = phy_index;
  273. sci_port_add_phy(&ihost->ports[port_index],
  274. &ihost->phys[phy_index]);
  275. assigned_phy_mask |= (1 << phy_index);
  276. phy_index++;
  277. }
  278. }
  279. return sci_port_configuration_agent_validate_ports(ihost, port_agent);
  280. }
  281. static void mpc_agent_timeout(unsigned long data)
  282. {
  283. u8 index;
  284. struct sci_timer *tmr = (struct sci_timer *)data;
  285. struct sci_port_configuration_agent *port_agent;
  286. struct isci_host *ihost;
  287. unsigned long flags;
  288. u16 configure_phy_mask;
  289. port_agent = container_of(tmr, typeof(*port_agent), timer);
  290. ihost = container_of(port_agent, typeof(*ihost), port_agent);
  291. spin_lock_irqsave(&ihost->scic_lock, flags);
  292. if (tmr->cancel)
  293. goto done;
  294. port_agent->timer_pending = false;
  295. /* Find the mask of phys that are reported read but as yet unconfigured into a port */
  296. configure_phy_mask = ~port_agent->phy_configured_mask & port_agent->phy_ready_mask;
  297. for (index = 0; index < SCI_MAX_PHYS; index++) {
  298. struct isci_phy *iphy = &ihost->phys[index];
  299. if (configure_phy_mask & (1 << index)) {
  300. port_agent->link_up_handler(ihost, port_agent,
  301. phy_get_non_dummy_port(iphy),
  302. iphy);
  303. }
  304. }
  305. done:
  306. spin_unlock_irqrestore(&ihost->scic_lock, flags);
  307. }
  308. static void sci_mpc_agent_link_up(struct isci_host *ihost,
  309. struct sci_port_configuration_agent *port_agent,
  310. struct isci_port *iport,
  311. struct isci_phy *iphy)
  312. {
  313. /* If the port is NULL then the phy was not assigned to a port.
  314. * This is because the phy was not given the same SAS Address as
  315. * the other PHYs in the port.
  316. */
  317. if (!iport)
  318. return;
  319. port_agent->phy_ready_mask |= (1 << iphy->phy_index);
  320. sci_port_link_up(iport, iphy);
  321. if ((iport->active_phy_mask & (1 << iphy->phy_index)))
  322. port_agent->phy_configured_mask |= (1 << iphy->phy_index);
  323. }
  324. /**
  325. *
  326. * @controller: This is the controller object that receives the link down
  327. * notification.
  328. * @port: This is the port object associated with the phy. If the is no
  329. * associated port this is an NULL. The port is an invalid
  330. * handle only if the phy was never port of this port. This happens when
  331. * the phy is not broadcasting the same SAS address as the other phys in the
  332. * assigned port.
  333. * @phy: This is the phy object which has gone link down.
  334. *
  335. * This function handles the manual port configuration link down notifications.
  336. * Since all ports and phys are associated at initialization time we just turn
  337. * around and notifiy the port object of the link down event. If this PHY is
  338. * not associated with a port there is no action taken. Is it possible to get a
  339. * link down notification from a phy that has no assocoated port?
  340. */
  341. static void sci_mpc_agent_link_down(
  342. struct isci_host *ihost,
  343. struct sci_port_configuration_agent *port_agent,
  344. struct isci_port *iport,
  345. struct isci_phy *iphy)
  346. {
  347. if (iport != NULL) {
  348. /*
  349. * If we can form a new port from the remainder of the phys
  350. * then we want to start the timer to allow the SCI User to
  351. * cleanup old devices and rediscover the port before
  352. * rebuilding the port with the phys that remain in the ready
  353. * state.
  354. */
  355. port_agent->phy_ready_mask &= ~(1 << iphy->phy_index);
  356. port_agent->phy_configured_mask &= ~(1 << iphy->phy_index);
  357. /*
  358. * Check to see if there are more phys waiting to be
  359. * configured into a port. If there are allow the SCI User
  360. * to tear down this port, if necessary, and then reconstruct
  361. * the port after the timeout.
  362. */
  363. if ((port_agent->phy_configured_mask == 0x0000) &&
  364. (port_agent->phy_ready_mask != 0x0000) &&
  365. !port_agent->timer_pending) {
  366. port_agent->timer_pending = true;
  367. sci_mod_timer(&port_agent->timer,
  368. SCIC_SDS_MPC_RECONFIGURATION_TIMEOUT);
  369. }
  370. sci_port_link_down(iport, iphy);
  371. }
  372. }
  373. /* verify phys are assigned a valid SAS address for automatic port
  374. * configuration mode.
  375. */
  376. static enum sci_status
  377. sci_apc_agent_validate_phy_configuration(struct isci_host *ihost,
  378. struct sci_port_configuration_agent *port_agent)
  379. {
  380. u8 phy_index;
  381. u8 port_index;
  382. struct sci_sas_address sas_address;
  383. struct sci_sas_address phy_assigned_address;
  384. phy_index = 0;
  385. while (phy_index < SCI_MAX_PHYS) {
  386. port_index = phy_index;
  387. /* Get the assigned SAS Address for the first PHY on the controller. */
  388. sci_phy_get_sas_address(&ihost->phys[phy_index],
  389. &sas_address);
  390. while (++phy_index < SCI_MAX_PHYS) {
  391. sci_phy_get_sas_address(&ihost->phys[phy_index],
  392. &phy_assigned_address);
  393. /* Verify each of the SAS address are all the same for every PHY */
  394. if (sci_sas_address_compare(sas_address, phy_assigned_address) == 0) {
  395. port_agent->phy_valid_port_range[phy_index].min_index = port_index;
  396. port_agent->phy_valid_port_range[phy_index].max_index = phy_index;
  397. } else {
  398. port_agent->phy_valid_port_range[phy_index].min_index = phy_index;
  399. port_agent->phy_valid_port_range[phy_index].max_index = phy_index;
  400. break;
  401. }
  402. }
  403. }
  404. return sci_port_configuration_agent_validate_ports(ihost, port_agent);
  405. }
  406. /*
  407. * This routine will restart the automatic port configuration timeout
  408. * timer for the next time period. This could be caused by either a link
  409. * down event or a link up event where we can not yet tell to which a phy
  410. * belongs.
  411. */
  412. static void sci_apc_agent_start_timer(struct sci_port_configuration_agent *port_agent,
  413. u32 timeout)
  414. {
  415. port_agent->timer_pending = true;
  416. sci_mod_timer(&port_agent->timer, timeout);
  417. }
  418. static void sci_apc_agent_configure_ports(struct isci_host *ihost,
  419. struct sci_port_configuration_agent *port_agent,
  420. struct isci_phy *iphy,
  421. bool start_timer)
  422. {
  423. u8 port_index;
  424. enum sci_status status;
  425. struct isci_port *iport;
  426. enum SCIC_SDS_APC_ACTIVITY apc_activity = SCIC_SDS_APC_SKIP_PHY;
  427. iport = sci_port_configuration_agent_find_port(ihost, iphy);
  428. if (iport) {
  429. if (sci_port_is_valid_phy_assignment(iport, iphy->phy_index))
  430. apc_activity = SCIC_SDS_APC_ADD_PHY;
  431. else
  432. apc_activity = SCIC_SDS_APC_SKIP_PHY;
  433. } else {
  434. /*
  435. * There is no matching Port for this PHY so lets search through the
  436. * Ports and see if we can add the PHY to its own port or maybe start
  437. * the timer and wait to see if a wider port can be made.
  438. *
  439. * Note the break when we reach the condition of the port id == phy id */
  440. for (port_index = port_agent->phy_valid_port_range[iphy->phy_index].min_index;
  441. port_index <= port_agent->phy_valid_port_range[iphy->phy_index].max_index;
  442. port_index++) {
  443. iport = &ihost->ports[port_index];
  444. /* First we must make sure that this PHY can be added to this Port. */
  445. if (sci_port_is_valid_phy_assignment(iport, iphy->phy_index)) {
  446. /*
  447. * Port contains a PHY with a greater PHY ID than the current
  448. * PHY that has gone link up. This phy can not be part of any
  449. * port so skip it and move on. */
  450. if (iport->active_phy_mask > (1 << iphy->phy_index)) {
  451. apc_activity = SCIC_SDS_APC_SKIP_PHY;
  452. break;
  453. }
  454. /*
  455. * We have reached the end of our Port list and have not found
  456. * any reason why we should not either add the PHY to the port
  457. * or wait for more phys to become active. */
  458. if (iport->physical_port_index == iphy->phy_index) {
  459. /*
  460. * The Port either has no active PHYs.
  461. * Consider that if the port had any active PHYs we would have
  462. * or active PHYs with
  463. * a lower PHY Id than this PHY. */
  464. if (apc_activity != SCIC_SDS_APC_START_TIMER) {
  465. apc_activity = SCIC_SDS_APC_ADD_PHY;
  466. }
  467. break;
  468. }
  469. /*
  470. * The current Port has no active PHYs and this PHY could be part
  471. * of this Port. Since we dont know as yet setup to start the
  472. * timer and see if there is a better configuration. */
  473. if (iport->active_phy_mask == 0) {
  474. apc_activity = SCIC_SDS_APC_START_TIMER;
  475. }
  476. } else if (iport->active_phy_mask != 0) {
  477. /*
  478. * The Port has an active phy and the current Phy can not
  479. * participate in this port so skip the PHY and see if
  480. * there is a better configuration. */
  481. apc_activity = SCIC_SDS_APC_SKIP_PHY;
  482. }
  483. }
  484. }
  485. /*
  486. * Check to see if the start timer operations should instead map to an
  487. * add phy operation. This is caused because we have been waiting to
  488. * add a phy to a port but could not becuase the automatic port
  489. * configuration engine had a choice of possible ports for the phy.
  490. * Since we have gone through a timeout we are going to restrict the
  491. * choice to the smallest possible port. */
  492. if (
  493. (start_timer == false)
  494. && (apc_activity == SCIC_SDS_APC_START_TIMER)
  495. ) {
  496. apc_activity = SCIC_SDS_APC_ADD_PHY;
  497. }
  498. switch (apc_activity) {
  499. case SCIC_SDS_APC_ADD_PHY:
  500. status = sci_port_add_phy(iport, iphy);
  501. if (status == SCI_SUCCESS) {
  502. port_agent->phy_configured_mask |= (1 << iphy->phy_index);
  503. }
  504. break;
  505. case SCIC_SDS_APC_START_TIMER:
  506. sci_apc_agent_start_timer(port_agent,
  507. SCIC_SDS_APC_WAIT_LINK_UP_NOTIFICATION);
  508. break;
  509. case SCIC_SDS_APC_SKIP_PHY:
  510. default:
  511. /* do nothing the PHY can not be made part of a port at this time. */
  512. break;
  513. }
  514. }
  515. /**
  516. * sci_apc_agent_link_up - handle apc link up events
  517. * @scic: This is the controller object that receives the link up
  518. * notification.
  519. * @sci_port: This is the port object associated with the phy. If the is no
  520. * associated port this is an NULL.
  521. * @sci_phy: This is the phy object which has gone link up.
  522. *
  523. * This method handles the automatic port configuration for link up
  524. * notifications. Is it possible to get a link down notification from a phy
  525. * that has no assocoated port?
  526. */
  527. static void sci_apc_agent_link_up(struct isci_host *ihost,
  528. struct sci_port_configuration_agent *port_agent,
  529. struct isci_port *iport,
  530. struct isci_phy *iphy)
  531. {
  532. u8 phy_index = iphy->phy_index;
  533. if (!iport) {
  534. /* the phy is not the part of this port */
  535. port_agent->phy_ready_mask |= 1 << phy_index;
  536. sci_apc_agent_start_timer(port_agent,
  537. SCIC_SDS_APC_WAIT_LINK_UP_NOTIFICATION);
  538. } else {
  539. /* the phy is already the part of the port */
  540. port_agent->phy_ready_mask |= 1 << phy_index;
  541. sci_port_link_up(iport, iphy);
  542. }
  543. }
  544. /**
  545. *
  546. * @controller: This is the controller object that receives the link down
  547. * notification.
  548. * @iport: This is the port object associated with the phy. If the is no
  549. * associated port this is an NULL.
  550. * @iphy: This is the phy object which has gone link down.
  551. *
  552. * This method handles the automatic port configuration link down
  553. * notifications. not associated with a port there is no action taken. Is it
  554. * possible to get a link down notification from a phy that has no assocoated
  555. * port?
  556. */
  557. static void sci_apc_agent_link_down(
  558. struct isci_host *ihost,
  559. struct sci_port_configuration_agent *port_agent,
  560. struct isci_port *iport,
  561. struct isci_phy *iphy)
  562. {
  563. port_agent->phy_ready_mask &= ~(1 << iphy->phy_index);
  564. if (!iport)
  565. return;
  566. if (port_agent->phy_configured_mask & (1 << iphy->phy_index)) {
  567. enum sci_status status;
  568. status = sci_port_remove_phy(iport, iphy);
  569. if (status == SCI_SUCCESS)
  570. port_agent->phy_configured_mask &= ~(1 << iphy->phy_index);
  571. }
  572. }
  573. /* configure the phys into ports when the timer fires */
  574. static void apc_agent_timeout(unsigned long data)
  575. {
  576. u32 index;
  577. struct sci_timer *tmr = (struct sci_timer *)data;
  578. struct sci_port_configuration_agent *port_agent;
  579. struct isci_host *ihost;
  580. unsigned long flags;
  581. u16 configure_phy_mask;
  582. port_agent = container_of(tmr, typeof(*port_agent), timer);
  583. ihost = container_of(port_agent, typeof(*ihost), port_agent);
  584. spin_lock_irqsave(&ihost->scic_lock, flags);
  585. if (tmr->cancel)
  586. goto done;
  587. port_agent->timer_pending = false;
  588. configure_phy_mask = ~port_agent->phy_configured_mask & port_agent->phy_ready_mask;
  589. if (!configure_phy_mask)
  590. goto done;
  591. for (index = 0; index < SCI_MAX_PHYS; index++) {
  592. if ((configure_phy_mask & (1 << index)) == 0)
  593. continue;
  594. sci_apc_agent_configure_ports(ihost, port_agent,
  595. &ihost->phys[index], false);
  596. }
  597. if (is_controller_start_complete(ihost))
  598. sci_controller_transition_to_ready(ihost, SCI_SUCCESS);
  599. done:
  600. spin_unlock_irqrestore(&ihost->scic_lock, flags);
  601. }
  602. /*
  603. * ******************************************************************************
  604. * Public port configuration agent routines
  605. * ****************************************************************************** */
  606. /**
  607. *
  608. *
  609. * This method will construct the port configuration agent for operation. This
  610. * call is universal for both manual port configuration and automatic port
  611. * configuration modes.
  612. */
  613. void sci_port_configuration_agent_construct(
  614. struct sci_port_configuration_agent *port_agent)
  615. {
  616. u32 index;
  617. port_agent->phy_configured_mask = 0x00;
  618. port_agent->phy_ready_mask = 0x00;
  619. port_agent->link_up_handler = NULL;
  620. port_agent->link_down_handler = NULL;
  621. port_agent->timer_pending = false;
  622. for (index = 0; index < SCI_MAX_PORTS; index++) {
  623. port_agent->phy_valid_port_range[index].min_index = 0;
  624. port_agent->phy_valid_port_range[index].max_index = 0;
  625. }
  626. }
  627. bool is_port_config_apc(struct isci_host *ihost)
  628. {
  629. return ihost->port_agent.link_up_handler == sci_apc_agent_link_up;
  630. }
  631. enum sci_status sci_port_configuration_agent_initialize(
  632. struct isci_host *ihost,
  633. struct sci_port_configuration_agent *port_agent)
  634. {
  635. enum sci_status status;
  636. enum sci_port_configuration_mode mode;
  637. mode = ihost->oem_parameters.controller.mode_type;
  638. if (mode == SCIC_PORT_MANUAL_CONFIGURATION_MODE) {
  639. status = sci_mpc_agent_validate_phy_configuration(
  640. ihost, port_agent);
  641. port_agent->link_up_handler = sci_mpc_agent_link_up;
  642. port_agent->link_down_handler = sci_mpc_agent_link_down;
  643. sci_init_timer(&port_agent->timer, mpc_agent_timeout);
  644. } else {
  645. status = sci_apc_agent_validate_phy_configuration(
  646. ihost, port_agent);
  647. port_agent->link_up_handler = sci_apc_agent_link_up;
  648. port_agent->link_down_handler = sci_apc_agent_link_down;
  649. sci_init_timer(&port_agent->timer, apc_agent_timeout);
  650. }
  651. return status;
  652. }