port_config.c 27 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808
  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 (100)
  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 scic_sds_port *scic_sds_port_configuration_agent_find_port(
  108. struct scic_sds_controller *scic,
  109. struct scic_sds_phy *phy)
  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. scic_sds_phy_get_sas_address(phy, &phy_sas_address);
  122. scic_sds_phy_get_attached_sas_address(phy, &phy_attached_device_address);
  123. for (i = 0; i < scic->logical_port_entries; i++) {
  124. struct isci_host *ihost = scic_to_ihost(scic);
  125. struct scic_sds_port *sci_port = &ihost->ports[i].sci;
  126. scic_sds_port_get_sas_address(sci_port, &port_sas_address);
  127. scic_sds_port_get_attached_sas_address(sci_port, &port_attached_device_address);
  128. if (sci_sas_address_compare(port_sas_address, phy_sas_address) == 0 &&
  129. sci_sas_address_compare(port_attached_device_address, phy_attached_device_address) == 0)
  130. return sci_port;
  131. }
  132. return NULL;
  133. }
  134. /**
  135. *
  136. * @controller: This is the controller object that contains the port agent
  137. * @port_agent: This is the port configruation agent for the controller.
  138. *
  139. * This routine will validate the port configuration is correct for the SCU
  140. * hardware. The SCU hardware allows for port configurations as follows. LP0
  141. * -> (PE0), (PE0, PE1), (PE0, PE1, PE2, PE3) LP1 -> (PE1) LP2 -> (PE2), (PE2,
  142. * PE3) LP3 -> (PE3) enum sci_status SCI_SUCCESS the port configuration is valid for
  143. * this port configuration agent. SCI_FAILURE_UNSUPPORTED_PORT_CONFIGURATION
  144. * the port configuration is not valid for this port configuration agent.
  145. */
  146. static enum sci_status scic_sds_port_configuration_agent_validate_ports(
  147. struct scic_sds_controller *controller,
  148. struct scic_sds_port_configuration_agent *port_agent)
  149. {
  150. struct isci_host *ihost = scic_to_ihost(controller);
  151. struct sci_sas_address first_address;
  152. struct sci_sas_address second_address;
  153. /*
  154. * Sanity check the max ranges for all the phys the max index
  155. * is always equal to the port range index */
  156. if (port_agent->phy_valid_port_range[0].max_index != 0 ||
  157. port_agent->phy_valid_port_range[1].max_index != 1 ||
  158. port_agent->phy_valid_port_range[2].max_index != 2 ||
  159. port_agent->phy_valid_port_range[3].max_index != 3)
  160. return SCI_FAILURE_UNSUPPORTED_PORT_CONFIGURATION;
  161. /*
  162. * This is a request to configure a single x4 port or at least attempt
  163. * to make all the phys into a single port */
  164. if (port_agent->phy_valid_port_range[0].min_index == 0 &&
  165. port_agent->phy_valid_port_range[1].min_index == 0 &&
  166. port_agent->phy_valid_port_range[2].min_index == 0 &&
  167. port_agent->phy_valid_port_range[3].min_index == 0)
  168. return SCI_SUCCESS;
  169. /*
  170. * This is a degenerate case where phy 1 and phy 2 are assigned
  171. * to the same port this is explicitly disallowed by the hardware
  172. * unless they are part of the same x4 port and this condition was
  173. * already checked above. */
  174. if (port_agent->phy_valid_port_range[2].min_index == 1) {
  175. return SCI_FAILURE_UNSUPPORTED_PORT_CONFIGURATION;
  176. }
  177. /*
  178. * PE0 and PE3 can never have the same SAS Address unless they
  179. * are part of the same x4 wide port and we have already checked
  180. * for this condition. */
  181. scic_sds_phy_get_sas_address(&ihost->phys[0].sci, &first_address);
  182. scic_sds_phy_get_sas_address(&ihost->phys[3].sci, &second_address);
  183. if (sci_sas_address_compare(first_address, second_address) == 0) {
  184. return SCI_FAILURE_UNSUPPORTED_PORT_CONFIGURATION;
  185. }
  186. /*
  187. * PE0 and PE1 are configured into a 2x1 ports make sure that the
  188. * SAS Address for PE0 and PE2 are different since they can not be
  189. * part of the same port. */
  190. if (port_agent->phy_valid_port_range[0].min_index == 0 &&
  191. port_agent->phy_valid_port_range[1].min_index == 1) {
  192. scic_sds_phy_get_sas_address(&ihost->phys[0].sci, &first_address);
  193. scic_sds_phy_get_sas_address(&ihost->phys[2].sci, &second_address);
  194. if (sci_sas_address_compare(first_address, second_address) == 0) {
  195. return SCI_FAILURE_UNSUPPORTED_PORT_CONFIGURATION;
  196. }
  197. }
  198. /*
  199. * PE2 and PE3 are configured into a 2x1 ports make sure that the
  200. * SAS Address for PE1 and PE3 are different since they can not be
  201. * part of the same port. */
  202. if (port_agent->phy_valid_port_range[2].min_index == 2 &&
  203. port_agent->phy_valid_port_range[3].min_index == 3) {
  204. scic_sds_phy_get_sas_address(&ihost->phys[1].sci, &first_address);
  205. scic_sds_phy_get_sas_address(&ihost->phys[3].sci, &second_address);
  206. if (sci_sas_address_compare(first_address, second_address) == 0) {
  207. return SCI_FAILURE_UNSUPPORTED_PORT_CONFIGURATION;
  208. }
  209. }
  210. return SCI_SUCCESS;
  211. }
  212. /*
  213. * ******************************************************************************
  214. * Manual port configuration agent routines
  215. * ****************************************************************************** */
  216. /**
  217. *
  218. *
  219. * This routine will verify that all of the phys in the same port are using the
  220. * same SAS address.
  221. */
  222. static enum sci_status scic_sds_mpc_agent_validate_phy_configuration(
  223. struct scic_sds_controller *controller,
  224. struct scic_sds_port_configuration_agent *port_agent)
  225. {
  226. struct isci_host *ihost = scic_to_ihost(controller);
  227. u32 phy_mask;
  228. u32 assigned_phy_mask;
  229. struct sci_sas_address sas_address;
  230. struct sci_sas_address phy_assigned_address;
  231. u8 port_index;
  232. u8 phy_index;
  233. assigned_phy_mask = 0;
  234. sas_address.high = 0;
  235. sas_address.low = 0;
  236. for (port_index = 0; port_index < SCI_MAX_PORTS; port_index++) {
  237. phy_mask = controller->oem_parameters.sds1.ports[port_index].phy_mask;
  238. if (!phy_mask)
  239. continue;
  240. /*
  241. * Make sure that one or more of the phys were not already assinged to
  242. * a different port. */
  243. if ((phy_mask & ~assigned_phy_mask) == 0) {
  244. return SCI_FAILURE_UNSUPPORTED_PORT_CONFIGURATION;
  245. }
  246. /* Find the starting phy index for this round through the loop */
  247. for (phy_index = 0; phy_index < SCI_MAX_PHYS; phy_index++) {
  248. if ((phy_mask & (1 << phy_index)) == 0)
  249. continue;
  250. scic_sds_phy_get_sas_address(&ihost->phys[phy_index].sci,
  251. &sas_address);
  252. /*
  253. * The phy_index can be used as the starting point for the
  254. * port range since the hardware starts all logical ports
  255. * the same as the PE index. */
  256. port_agent->phy_valid_port_range[phy_index].min_index = port_index;
  257. port_agent->phy_valid_port_range[phy_index].max_index = phy_index;
  258. if (phy_index != port_index) {
  259. return SCI_FAILURE_UNSUPPORTED_PORT_CONFIGURATION;
  260. }
  261. break;
  262. }
  263. /*
  264. * See how many additional phys are being added to this logical port.
  265. * Note: We have not moved the current phy_index so we will actually
  266. * compare the startting phy with itself.
  267. * This is expected and required to add the phy to the port. */
  268. while (phy_index < SCI_MAX_PHYS) {
  269. if ((phy_mask & (1 << phy_index)) == 0)
  270. continue;
  271. scic_sds_phy_get_sas_address(&ihost->phys[phy_index].sci,
  272. &phy_assigned_address);
  273. if (sci_sas_address_compare(sas_address, phy_assigned_address) != 0) {
  274. /*
  275. * The phy mask specified that this phy is part of the same port
  276. * as the starting phy and it is not so fail this configuration */
  277. return SCI_FAILURE_UNSUPPORTED_PORT_CONFIGURATION;
  278. }
  279. port_agent->phy_valid_port_range[phy_index].min_index = port_index;
  280. port_agent->phy_valid_port_range[phy_index].max_index = phy_index;
  281. scic_sds_port_add_phy(&ihost->ports[port_index].sci,
  282. &ihost->phys[phy_index].sci);
  283. assigned_phy_mask |= (1 << phy_index);
  284. }
  285. phy_index++;
  286. }
  287. return scic_sds_port_configuration_agent_validate_ports(controller, port_agent);
  288. }
  289. static void mpc_agent_timeout(unsigned long data)
  290. {
  291. u8 index;
  292. struct sci_timer *tmr = (struct sci_timer *)data;
  293. struct scic_sds_port_configuration_agent *port_agent;
  294. struct scic_sds_controller *scic;
  295. struct isci_host *ihost;
  296. unsigned long flags;
  297. u16 configure_phy_mask;
  298. port_agent = container_of(tmr, typeof(*port_agent), timer);
  299. scic = container_of(port_agent, typeof(*scic), port_agent);
  300. ihost = scic_to_ihost(scic);
  301. spin_lock_irqsave(&ihost->scic_lock, flags);
  302. if (tmr->cancel)
  303. goto done;
  304. port_agent->timer_pending = false;
  305. /* Find the mask of phys that are reported read but as yet unconfigured into a port */
  306. configure_phy_mask = ~port_agent->phy_configured_mask & port_agent->phy_ready_mask;
  307. for (index = 0; index < SCI_MAX_PHYS; index++) {
  308. struct scic_sds_phy *sci_phy = &ihost->phys[index].sci;
  309. if (configure_phy_mask & (1 << index)) {
  310. port_agent->link_up_handler(scic, port_agent,
  311. phy_get_non_dummy_port(sci_phy),
  312. sci_phy);
  313. }
  314. }
  315. done:
  316. spin_unlock_irqrestore(&ihost->scic_lock, flags);
  317. }
  318. /**
  319. *
  320. * @controller: This is the controller object that receives the link up
  321. * notification.
  322. * @port: This is the port object associated with the phy. If the is no
  323. * associated port this is an NULL.
  324. * @phy: This is the phy object which has gone ready.
  325. *
  326. * This method handles the manual port configuration link up notifications.
  327. * Since all ports and phys are associate at initialization time we just turn
  328. * around and notifiy the port object that there is a link up. If this PHY is
  329. * not associated with a port there is no action taken. Is it possible to get a
  330. * link up notification from a phy that has no assocoated port?
  331. */
  332. static void scic_sds_mpc_agent_link_up(
  333. struct scic_sds_controller *controller,
  334. struct scic_sds_port_configuration_agent *port_agent,
  335. struct scic_sds_port *port,
  336. struct scic_sds_phy *phy)
  337. {
  338. /*
  339. * If the port has an invalid handle then the phy was not assigned to
  340. * a port. This is because the phy was not given the same SAS Address
  341. * as the other PHYs in the port. */
  342. if (port != NULL) {
  343. port_agent->phy_ready_mask |= (1 << scic_sds_phy_get_index(phy));
  344. scic_sds_port_link_up(port, phy);
  345. if ((port->active_phy_mask & (1 << scic_sds_phy_get_index(phy))) != 0) {
  346. port_agent->phy_configured_mask |= (1 << scic_sds_phy_get_index(phy));
  347. }
  348. }
  349. }
  350. /**
  351. *
  352. * @controller: This is the controller object that receives the link down
  353. * notification.
  354. * @port: This is the port object associated with the phy. If the is no
  355. * associated port this is an NULL. The port is an invalid
  356. * handle only if the phy was never port of this port. This happens when
  357. * the phy is not broadcasting the same SAS address as the other phys in the
  358. * assigned port.
  359. * @phy: This is the phy object which has gone link down.
  360. *
  361. * This function handles the manual port configuration link down notifications.
  362. * Since all ports and phys are associated at initialization time we just turn
  363. * around and notifiy the port object of the link down event. If this PHY is
  364. * not associated with a port there is no action taken. Is it possible to get a
  365. * link down notification from a phy that has no assocoated port?
  366. */
  367. static void scic_sds_mpc_agent_link_down(
  368. struct scic_sds_controller *scic,
  369. struct scic_sds_port_configuration_agent *port_agent,
  370. struct scic_sds_port *sci_port,
  371. struct scic_sds_phy *sci_phy)
  372. {
  373. if (sci_port != NULL) {
  374. /*
  375. * If we can form a new port from the remainder of the phys
  376. * then we want to start the timer to allow the SCI User to
  377. * cleanup old devices and rediscover the port before
  378. * rebuilding the port with the phys that remain in the ready
  379. * state.
  380. */
  381. port_agent->phy_ready_mask &=
  382. ~(1 << scic_sds_phy_get_index(sci_phy));
  383. port_agent->phy_configured_mask &=
  384. ~(1 << scic_sds_phy_get_index(sci_phy));
  385. /*
  386. * Check to see if there are more phys waiting to be
  387. * configured into a port. If there are allow the SCI User
  388. * to tear down this port, if necessary, and then reconstruct
  389. * the port after the timeout.
  390. */
  391. if ((port_agent->phy_configured_mask == 0x0000) &&
  392. (port_agent->phy_ready_mask != 0x0000) &&
  393. !port_agent->timer_pending) {
  394. port_agent->timer_pending = true;
  395. sci_mod_timer(&port_agent->timer,
  396. SCIC_SDS_MPC_RECONFIGURATION_TIMEOUT);
  397. }
  398. scic_sds_port_link_down(sci_port, sci_phy);
  399. }
  400. }
  401. /*
  402. * ******************************************************************************
  403. * Automatic port configuration agent routines
  404. * ****************************************************************************** */
  405. /**
  406. *
  407. *
  408. * This routine will verify that the phys are assigned a valid SAS address for
  409. * automatic port configuration mode.
  410. */
  411. static enum sci_status scic_sds_apc_agent_validate_phy_configuration(
  412. struct scic_sds_controller *controller,
  413. struct scic_sds_port_configuration_agent *port_agent)
  414. {
  415. u8 phy_index;
  416. u8 port_index;
  417. struct sci_sas_address sas_address;
  418. struct sci_sas_address phy_assigned_address;
  419. struct isci_host *ihost = scic_to_ihost(controller);
  420. phy_index = 0;
  421. while (phy_index < SCI_MAX_PHYS) {
  422. port_index = phy_index;
  423. /* Get the assigned SAS Address for the first PHY on the controller. */
  424. scic_sds_phy_get_sas_address(&ihost->phys[phy_index].sci,
  425. &sas_address);
  426. while (++phy_index < SCI_MAX_PHYS) {
  427. scic_sds_phy_get_sas_address(&ihost->phys[phy_index].sci,
  428. &phy_assigned_address);
  429. /* Verify each of the SAS address are all the same for every PHY */
  430. if (sci_sas_address_compare(sas_address, phy_assigned_address) == 0) {
  431. port_agent->phy_valid_port_range[phy_index].min_index = port_index;
  432. port_agent->phy_valid_port_range[phy_index].max_index = phy_index;
  433. } else {
  434. port_agent->phy_valid_port_range[phy_index].min_index = phy_index;
  435. port_agent->phy_valid_port_range[phy_index].max_index = phy_index;
  436. break;
  437. }
  438. }
  439. }
  440. return scic_sds_port_configuration_agent_validate_ports(controller, port_agent);
  441. }
  442. /**
  443. *
  444. * @controller: This is the controller object that receives the link up
  445. * notification.
  446. * @phy: This is the phy object which has gone link up.
  447. *
  448. * This method handles the automatic port configuration for link up
  449. * notifications.
  450. */
  451. static void scic_sds_apc_agent_configure_ports(
  452. struct scic_sds_controller *controller,
  453. struct scic_sds_port_configuration_agent *port_agent,
  454. struct scic_sds_phy *phy,
  455. bool start_timer)
  456. {
  457. u8 port_index;
  458. enum sci_status status;
  459. struct scic_sds_port *port;
  460. enum SCIC_SDS_APC_ACTIVITY apc_activity = SCIC_SDS_APC_SKIP_PHY;
  461. struct isci_host *ihost = scic_to_ihost(controller);
  462. port = scic_sds_port_configuration_agent_find_port(controller, phy);
  463. if (port != NULL) {
  464. if (scic_sds_port_is_valid_phy_assignment(port, phy->phy_index))
  465. apc_activity = SCIC_SDS_APC_ADD_PHY;
  466. else
  467. apc_activity = SCIC_SDS_APC_SKIP_PHY;
  468. } else {
  469. /*
  470. * There is no matching Port for this PHY so lets search through the
  471. * Ports and see if we can add the PHY to its own port or maybe start
  472. * the timer and wait to see if a wider port can be made.
  473. *
  474. * Note the break when we reach the condition of the port id == phy id */
  475. for (
  476. port_index = port_agent->phy_valid_port_range[phy->phy_index].min_index;
  477. port_index <= port_agent->phy_valid_port_range[phy->phy_index].max_index;
  478. port_index++
  479. ) {
  480. port = &ihost->ports[port_index].sci;
  481. /* First we must make sure that this PHY can be added to this Port. */
  482. if (scic_sds_port_is_valid_phy_assignment(port, phy->phy_index)) {
  483. /*
  484. * Port contains a PHY with a greater PHY ID than the current
  485. * PHY that has gone link up. This phy can not be part of any
  486. * port so skip it and move on. */
  487. if (port->active_phy_mask > (1 << phy->phy_index)) {
  488. apc_activity = SCIC_SDS_APC_SKIP_PHY;
  489. break;
  490. }
  491. /*
  492. * We have reached the end of our Port list and have not found
  493. * any reason why we should not either add the PHY to the port
  494. * or wait for more phys to become active. */
  495. if (port->physical_port_index == phy->phy_index) {
  496. /*
  497. * The Port either has no active PHYs.
  498. * Consider that if the port had any active PHYs we would have
  499. * or active PHYs with
  500. * a lower PHY Id than this PHY. */
  501. if (apc_activity != SCIC_SDS_APC_START_TIMER) {
  502. apc_activity = SCIC_SDS_APC_ADD_PHY;
  503. }
  504. break;
  505. }
  506. /*
  507. * The current Port has no active PHYs and this PHY could be part
  508. * of this Port. Since we dont know as yet setup to start the
  509. * timer and see if there is a better configuration. */
  510. if (port->active_phy_mask == 0) {
  511. apc_activity = SCIC_SDS_APC_START_TIMER;
  512. }
  513. } else if (port->active_phy_mask != 0) {
  514. /*
  515. * The Port has an active phy and the current Phy can not
  516. * participate in this port so skip the PHY and see if
  517. * there is a better configuration. */
  518. apc_activity = SCIC_SDS_APC_SKIP_PHY;
  519. }
  520. }
  521. }
  522. /*
  523. * Check to see if the start timer operations should instead map to an
  524. * add phy operation. This is caused because we have been waiting to
  525. * add a phy to a port but could not becuase the automatic port
  526. * configuration engine had a choice of possible ports for the phy.
  527. * Since we have gone through a timeout we are going to restrict the
  528. * choice to the smallest possible port. */
  529. if (
  530. (start_timer == false)
  531. && (apc_activity == SCIC_SDS_APC_START_TIMER)
  532. ) {
  533. apc_activity = SCIC_SDS_APC_ADD_PHY;
  534. }
  535. switch (apc_activity) {
  536. case SCIC_SDS_APC_ADD_PHY:
  537. status = scic_sds_port_add_phy(port, phy);
  538. if (status == SCI_SUCCESS) {
  539. port_agent->phy_configured_mask |= (1 << phy->phy_index);
  540. }
  541. break;
  542. case SCIC_SDS_APC_START_TIMER:
  543. /*
  544. * This can occur for either a link down event, or a link
  545. * up event where we cannot yet tell the port to which a
  546. * phy belongs.
  547. */
  548. if (port_agent->timer_pending)
  549. sci_del_timer(&port_agent->timer);
  550. port_agent->timer_pending = true;
  551. sci_mod_timer(&port_agent->timer,
  552. SCIC_SDS_APC_WAIT_LINK_UP_NOTIFICATION);
  553. break;
  554. case SCIC_SDS_APC_SKIP_PHY:
  555. default:
  556. /* do nothing the PHY can not be made part of a port at this time. */
  557. break;
  558. }
  559. }
  560. /**
  561. * scic_sds_apc_agent_link_up - handle apc link up events
  562. * @scic: This is the controller object that receives the link up
  563. * notification.
  564. * @sci_port: This is the port object associated with the phy. If the is no
  565. * associated port this is an NULL.
  566. * @sci_phy: This is the phy object which has gone link up.
  567. *
  568. * This method handles the automatic port configuration for link up
  569. * notifications. Is it possible to get a link down notification from a phy
  570. * that has no assocoated port?
  571. */
  572. static void scic_sds_apc_agent_link_up(struct scic_sds_controller *scic,
  573. struct scic_sds_port_configuration_agent *port_agent,
  574. struct scic_sds_port *sci_port,
  575. struct scic_sds_phy *sci_phy)
  576. {
  577. u8 phy_index = sci_phy->phy_index;
  578. if (!sci_port) {
  579. /* the phy is not the part of this port */
  580. port_agent->phy_ready_mask |= 1 << phy_index;
  581. scic_sds_apc_agent_configure_ports(scic, port_agent, sci_phy, true);
  582. } else {
  583. /* the phy is already the part of the port */
  584. u32 port_state = sci_port->sm.current_state_id;
  585. /* if the PORT'S state is resetting then the link up is from
  586. * port hard reset in this case, we need to tell the port
  587. * that link up is recieved
  588. */
  589. BUG_ON(port_state != SCI_PORT_RESETTING);
  590. port_agent->phy_ready_mask |= 1 << phy_index;
  591. scic_sds_port_link_up(sci_port, sci_phy);
  592. }
  593. }
  594. /**
  595. *
  596. * @controller: This is the controller object that receives the link down
  597. * notification.
  598. * @port: This is the port object associated with the phy. If the is no
  599. * associated port this is an NULL.
  600. * @phy: This is the phy object which has gone link down.
  601. *
  602. * This method handles the automatic port configuration link down
  603. * notifications. not associated with a port there is no action taken. Is it
  604. * possible to get a link down notification from a phy that has no assocoated
  605. * port?
  606. */
  607. static void scic_sds_apc_agent_link_down(
  608. struct scic_sds_controller *controller,
  609. struct scic_sds_port_configuration_agent *port_agent,
  610. struct scic_sds_port *port,
  611. struct scic_sds_phy *phy)
  612. {
  613. port_agent->phy_ready_mask &= ~(1 << scic_sds_phy_get_index(phy));
  614. if (port != NULL) {
  615. if (port_agent->phy_configured_mask & (1 << phy->phy_index)) {
  616. enum sci_status status;
  617. status = scic_sds_port_remove_phy(port, phy);
  618. if (status == SCI_SUCCESS) {
  619. port_agent->phy_configured_mask &= ~(1 << phy->phy_index);
  620. }
  621. }
  622. }
  623. }
  624. /* configure the phys into ports when the timer fires */
  625. static void apc_agent_timeout(unsigned long data)
  626. {
  627. u32 index;
  628. struct sci_timer *tmr = (struct sci_timer *)data;
  629. struct scic_sds_port_configuration_agent *port_agent;
  630. struct scic_sds_controller *scic;
  631. struct isci_host *ihost;
  632. unsigned long flags;
  633. u16 configure_phy_mask;
  634. port_agent = container_of(tmr, typeof(*port_agent), timer);
  635. scic = container_of(port_agent, typeof(*scic), port_agent);
  636. ihost = scic_to_ihost(scic);
  637. spin_lock_irqsave(&ihost->scic_lock, flags);
  638. if (tmr->cancel)
  639. goto done;
  640. port_agent->timer_pending = false;
  641. configure_phy_mask = ~port_agent->phy_configured_mask & port_agent->phy_ready_mask;
  642. if (!configure_phy_mask)
  643. return;
  644. for (index = 0; index < SCI_MAX_PHYS; index++) {
  645. if ((configure_phy_mask & (1 << index)) == 0)
  646. continue;
  647. scic_sds_apc_agent_configure_ports(scic, port_agent,
  648. &ihost->phys[index].sci, false);
  649. }
  650. done:
  651. spin_unlock_irqrestore(&ihost->scic_lock, flags);
  652. }
  653. /*
  654. * ******************************************************************************
  655. * Public port configuration agent routines
  656. * ****************************************************************************** */
  657. /**
  658. *
  659. *
  660. * This method will construct the port configuration agent for operation. This
  661. * call is universal for both manual port configuration and automatic port
  662. * configuration modes.
  663. */
  664. void scic_sds_port_configuration_agent_construct(
  665. struct scic_sds_port_configuration_agent *port_agent)
  666. {
  667. u32 index;
  668. port_agent->phy_configured_mask = 0x00;
  669. port_agent->phy_ready_mask = 0x00;
  670. port_agent->link_up_handler = NULL;
  671. port_agent->link_down_handler = NULL;
  672. port_agent->timer_pending = false;
  673. for (index = 0; index < SCI_MAX_PORTS; index++) {
  674. port_agent->phy_valid_port_range[index].min_index = 0;
  675. port_agent->phy_valid_port_range[index].max_index = 0;
  676. }
  677. }
  678. enum sci_status scic_sds_port_configuration_agent_initialize(
  679. struct scic_sds_controller *scic,
  680. struct scic_sds_port_configuration_agent *port_agent)
  681. {
  682. enum sci_status status;
  683. enum scic_port_configuration_mode mode;
  684. mode = scic->oem_parameters.sds1.controller.mode_type;
  685. if (mode == SCIC_PORT_MANUAL_CONFIGURATION_MODE) {
  686. status = scic_sds_mpc_agent_validate_phy_configuration(
  687. scic, port_agent);
  688. port_agent->link_up_handler = scic_sds_mpc_agent_link_up;
  689. port_agent->link_down_handler = scic_sds_mpc_agent_link_down;
  690. sci_init_timer(&port_agent->timer, mpc_agent_timeout);
  691. } else {
  692. status = scic_sds_apc_agent_validate_phy_configuration(
  693. scic, port_agent);
  694. port_agent->link_up_handler = scic_sds_apc_agent_link_up;
  695. port_agent->link_down_handler = scic_sds_apc_agent_link_down;
  696. sci_init_timer(&port_agent->timer, apc_agent_timeout);
  697. }
  698. return status;
  699. }