port_config.c 27 KB

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