PageRenderTime 69ms CodeModel.GetById 25ms RepoModel.GetById 0ms app.codeStats 1ms

/drivers/scsi/isci/port.c

https://bitbucket.org/slukk/jb-tsm-kernel-4.2
C | 1757 lines | 1075 code | 281 blank | 401 comment | 213 complexity | 4232b02de98ea474c7c9f38d074b0bef MD5 | raw file
Possible License(s): GPL-2.0, LGPL-2.0, AGPL-1.0
  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 "isci.h"
  56. #include "port.h"
  57. #include "request.h"
  58. #define SCIC_SDS_PORT_HARD_RESET_TIMEOUT (1000)
  59. #define SCU_DUMMY_INDEX (0xFFFF)
  60. static void isci_port_change_state(struct isci_port *iport, enum isci_status status)
  61. {
  62. unsigned long flags;
  63. dev_dbg(&iport->isci_host->pdev->dev,
  64. "%s: iport = %p, state = 0x%x\n",
  65. __func__, iport, status);
  66. /* XXX pointless lock */
  67. spin_lock_irqsave(&iport->state_lock, flags);
  68. iport->status = status;
  69. spin_unlock_irqrestore(&iport->state_lock, flags);
  70. }
  71. static void sci_port_get_protocols(struct isci_port *iport, struct sci_phy_proto *proto)
  72. {
  73. u8 index;
  74. proto->all = 0;
  75. for (index = 0; index < SCI_MAX_PHYS; index++) {
  76. struct isci_phy *iphy = iport->phy_table[index];
  77. if (!iphy)
  78. continue;
  79. sci_phy_get_protocols(iphy, proto);
  80. }
  81. }
  82. static u32 sci_port_get_phys(struct isci_port *iport)
  83. {
  84. u32 index;
  85. u32 mask;
  86. mask = 0;
  87. for (index = 0; index < SCI_MAX_PHYS; index++)
  88. if (iport->phy_table[index])
  89. mask |= (1 << index);
  90. return mask;
  91. }
  92. /**
  93. * sci_port_get_properties() - This method simply returns the properties
  94. * regarding the port, such as: physical index, protocols, sas address, etc.
  95. * @port: this parameter specifies the port for which to retrieve the physical
  96. * index.
  97. * @properties: This parameter specifies the properties structure into which to
  98. * copy the requested information.
  99. *
  100. * Indicate if the user specified a valid port. SCI_SUCCESS This value is
  101. * returned if the specified port was valid. SCI_FAILURE_INVALID_PORT This
  102. * value is returned if the specified port is not valid. When this value is
  103. * returned, no data is copied to the properties output parameter.
  104. */
  105. static enum sci_status sci_port_get_properties(struct isci_port *iport,
  106. struct sci_port_properties *prop)
  107. {
  108. if (!iport || iport->logical_port_index == SCIC_SDS_DUMMY_PORT)
  109. return SCI_FAILURE_INVALID_PORT;
  110. prop->index = iport->logical_port_index;
  111. prop->phy_mask = sci_port_get_phys(iport);
  112. sci_port_get_sas_address(iport, &prop->local.sas_address);
  113. sci_port_get_protocols(iport, &prop->local.protocols);
  114. sci_port_get_attached_sas_address(iport, &prop->remote.sas_address);
  115. return SCI_SUCCESS;
  116. }
  117. static void sci_port_bcn_enable(struct isci_port *iport)
  118. {
  119. struct isci_phy *iphy;
  120. u32 val;
  121. int i;
  122. for (i = 0; i < ARRAY_SIZE(iport->phy_table); i++) {
  123. iphy = iport->phy_table[i];
  124. if (!iphy)
  125. continue;
  126. val = readl(&iphy->link_layer_registers->link_layer_control);
  127. /* clear the bit by writing 1. */
  128. writel(val, &iphy->link_layer_registers->link_layer_control);
  129. }
  130. }
  131. /* called under sci_lock to stabilize phy:port associations */
  132. void isci_port_bcn_enable(struct isci_host *ihost, struct isci_port *iport)
  133. {
  134. int i;
  135. clear_bit(IPORT_BCN_BLOCKED, &iport->flags);
  136. wake_up(&ihost->eventq);
  137. if (!test_and_clear_bit(IPORT_BCN_PENDING, &iport->flags))
  138. return;
  139. for (i = 0; i < ARRAY_SIZE(iport->phy_table); i++) {
  140. struct isci_phy *iphy = iport->phy_table[i];
  141. if (!iphy)
  142. continue;
  143. ihost->sas_ha.notify_port_event(&iphy->sas_phy,
  144. PORTE_BROADCAST_RCVD);
  145. break;
  146. }
  147. }
  148. static void isci_port_bc_change_received(struct isci_host *ihost,
  149. struct isci_port *iport,
  150. struct isci_phy *iphy)
  151. {
  152. if (iport && test_bit(IPORT_BCN_BLOCKED, &iport->flags)) {
  153. dev_dbg(&ihost->pdev->dev,
  154. "%s: disabled BCN; isci_phy = %p, sas_phy = %p\n",
  155. __func__, iphy, &iphy->sas_phy);
  156. set_bit(IPORT_BCN_PENDING, &iport->flags);
  157. atomic_inc(&iport->event);
  158. wake_up(&ihost->eventq);
  159. } else {
  160. dev_dbg(&ihost->pdev->dev,
  161. "%s: isci_phy = %p, sas_phy = %p\n",
  162. __func__, iphy, &iphy->sas_phy);
  163. ihost->sas_ha.notify_port_event(&iphy->sas_phy,
  164. PORTE_BROADCAST_RCVD);
  165. }
  166. sci_port_bcn_enable(iport);
  167. }
  168. static void isci_port_link_up(struct isci_host *isci_host,
  169. struct isci_port *iport,
  170. struct isci_phy *iphy)
  171. {
  172. unsigned long flags;
  173. struct sci_port_properties properties;
  174. unsigned long success = true;
  175. BUG_ON(iphy->isci_port != NULL);
  176. iphy->isci_port = iport;
  177. dev_dbg(&isci_host->pdev->dev,
  178. "%s: isci_port = %p\n",
  179. __func__, iport);
  180. spin_lock_irqsave(&iphy->sas_phy.frame_rcvd_lock, flags);
  181. isci_port_change_state(iphy->isci_port, isci_starting);
  182. sci_port_get_properties(iport, &properties);
  183. if (iphy->protocol == SCIC_SDS_PHY_PROTOCOL_SATA) {
  184. u64 attached_sas_address;
  185. iphy->sas_phy.oob_mode = SATA_OOB_MODE;
  186. iphy->sas_phy.frame_rcvd_size = sizeof(struct dev_to_host_fis);
  187. /*
  188. * For direct-attached SATA devices, the SCI core will
  189. * automagically assign a SAS address to the end device
  190. * for the purpose of creating a port. This SAS address
  191. * will not be the same as assigned to the PHY and needs
  192. * to be obtained from struct sci_port_properties properties.
  193. */
  194. attached_sas_address = properties.remote.sas_address.high;
  195. attached_sas_address <<= 32;
  196. attached_sas_address |= properties.remote.sas_address.low;
  197. swab64s(&attached_sas_address);
  198. memcpy(&iphy->sas_phy.attached_sas_addr,
  199. &attached_sas_address, sizeof(attached_sas_address));
  200. } else if (iphy->protocol == SCIC_SDS_PHY_PROTOCOL_SAS) {
  201. iphy->sas_phy.oob_mode = SAS_OOB_MODE;
  202. iphy->sas_phy.frame_rcvd_size = sizeof(struct sas_identify_frame);
  203. /* Copy the attached SAS address from the IAF */
  204. memcpy(iphy->sas_phy.attached_sas_addr,
  205. iphy->frame_rcvd.iaf.sas_addr, SAS_ADDR_SIZE);
  206. } else {
  207. dev_err(&isci_host->pdev->dev, "%s: unkown target\n", __func__);
  208. success = false;
  209. }
  210. iphy->sas_phy.phy->negotiated_linkrate = sci_phy_linkrate(iphy);
  211. spin_unlock_irqrestore(&iphy->sas_phy.frame_rcvd_lock, flags);
  212. /* Notify libsas that we have an address frame, if indeed
  213. * we've found an SSP, SMP, or STP target */
  214. if (success)
  215. isci_host->sas_ha.notify_port_event(&iphy->sas_phy,
  216. PORTE_BYTES_DMAED);
  217. }
  218. /**
  219. * isci_port_link_down() - This function is called by the sci core when a link
  220. * becomes inactive.
  221. * @isci_host: This parameter specifies the isci host object.
  222. * @phy: This parameter specifies the isci phy with the active link.
  223. * @port: This parameter specifies the isci port with the active link.
  224. *
  225. */
  226. static void isci_port_link_down(struct isci_host *isci_host,
  227. struct isci_phy *isci_phy,
  228. struct isci_port *isci_port)
  229. {
  230. struct isci_remote_device *isci_device;
  231. dev_dbg(&isci_host->pdev->dev,
  232. "%s: isci_port = %p\n", __func__, isci_port);
  233. if (isci_port) {
  234. /* check to see if this is the last phy on this port. */
  235. if (isci_phy->sas_phy.port &&
  236. isci_phy->sas_phy.port->num_phys == 1) {
  237. atomic_inc(&isci_port->event);
  238. isci_port_bcn_enable(isci_host, isci_port);
  239. /* change the state for all devices on this port. The
  240. * next task sent to this device will be returned as
  241. * SAS_TASK_UNDELIVERED, and the scsi mid layer will
  242. * remove the target
  243. */
  244. list_for_each_entry(isci_device,
  245. &isci_port->remote_dev_list,
  246. node) {
  247. dev_dbg(&isci_host->pdev->dev,
  248. "%s: isci_device = %p\n",
  249. __func__, isci_device);
  250. set_bit(IDEV_GONE, &isci_device->flags);
  251. }
  252. }
  253. isci_port_change_state(isci_port, isci_stopping);
  254. }
  255. /* Notify libsas of the borken link, this will trigger calls to our
  256. * isci_port_deformed and isci_dev_gone functions.
  257. */
  258. sas_phy_disconnected(&isci_phy->sas_phy);
  259. isci_host->sas_ha.notify_phy_event(&isci_phy->sas_phy,
  260. PHYE_LOSS_OF_SIGNAL);
  261. isci_phy->isci_port = NULL;
  262. dev_dbg(&isci_host->pdev->dev,
  263. "%s: isci_port = %p - Done\n", __func__, isci_port);
  264. }
  265. /**
  266. * isci_port_ready() - This function is called by the sci core when a link
  267. * becomes ready.
  268. * @isci_host: This parameter specifies the isci host object.
  269. * @port: This parameter specifies the sci port with the active link.
  270. *
  271. */
  272. static void isci_port_ready(struct isci_host *isci_host, struct isci_port *isci_port)
  273. {
  274. dev_dbg(&isci_host->pdev->dev,
  275. "%s: isci_port = %p\n", __func__, isci_port);
  276. complete_all(&isci_port->start_complete);
  277. isci_port_change_state(isci_port, isci_ready);
  278. return;
  279. }
  280. /**
  281. * isci_port_not_ready() - This function is called by the sci core when a link
  282. * is not ready. All remote devices on this link will be removed if they are
  283. * in the stopping state.
  284. * @isci_host: This parameter specifies the isci host object.
  285. * @port: This parameter specifies the sci port with the active link.
  286. *
  287. */
  288. static void isci_port_not_ready(struct isci_host *isci_host, struct isci_port *isci_port)
  289. {
  290. dev_dbg(&isci_host->pdev->dev,
  291. "%s: isci_port = %p\n", __func__, isci_port);
  292. }
  293. static void isci_port_stop_complete(struct isci_host *ihost,
  294. struct isci_port *iport,
  295. enum sci_status completion_status)
  296. {
  297. dev_dbg(&ihost->pdev->dev, "Port stop complete\n");
  298. }
  299. /**
  300. * isci_port_hard_reset_complete() - This function is called by the sci core
  301. * when the hard reset complete notification has been received.
  302. * @port: This parameter specifies the sci port with the active link.
  303. * @completion_status: This parameter specifies the core status for the reset
  304. * process.
  305. *
  306. */
  307. static void isci_port_hard_reset_complete(struct isci_port *isci_port,
  308. enum sci_status completion_status)
  309. {
  310. dev_dbg(&isci_port->isci_host->pdev->dev,
  311. "%s: isci_port = %p, completion_status=%x\n",
  312. __func__, isci_port, completion_status);
  313. /* Save the status of the hard reset from the port. */
  314. isci_port->hard_reset_status = completion_status;
  315. complete_all(&isci_port->hard_reset_complete);
  316. }
  317. /* This method will return a true value if the specified phy can be assigned to
  318. * this port The following is a list of phys for each port that are allowed: -
  319. * Port 0 - 3 2 1 0 - Port 1 - 1 - Port 2 - 3 2 - Port 3 - 3 This method
  320. * doesn't preclude all configurations. It merely ensures that a phy is part
  321. * of the allowable set of phy identifiers for that port. For example, one
  322. * could assign phy 3 to port 0 and no other phys. Please refer to
  323. * sci_port_is_phy_mask_valid() for information regarding whether the
  324. * phy_mask for a port can be supported. bool true if this is a valid phy
  325. * assignment for the port false if this is not a valid phy assignment for the
  326. * port
  327. */
  328. bool sci_port_is_valid_phy_assignment(struct isci_port *iport, u32 phy_index)
  329. {
  330. struct isci_host *ihost = iport->owning_controller;
  331. struct sci_user_parameters *user = &ihost->user_parameters;
  332. /* Initialize to invalid value. */
  333. u32 existing_phy_index = SCI_MAX_PHYS;
  334. u32 index;
  335. if ((iport->physical_port_index == 1) && (phy_index != 1))
  336. return false;
  337. if (iport->physical_port_index == 3 && phy_index != 3)
  338. return false;
  339. if (iport->physical_port_index == 2 &&
  340. (phy_index == 0 || phy_index == 1))
  341. return false;
  342. for (index = 0; index < SCI_MAX_PHYS; index++)
  343. if (iport->phy_table[index] && index != phy_index)
  344. existing_phy_index = index;
  345. /* Ensure that all of the phys in the port are capable of
  346. * operating at the same maximum link rate.
  347. */
  348. if (existing_phy_index < SCI_MAX_PHYS &&
  349. user->phys[phy_index].max_speed_generation !=
  350. user->phys[existing_phy_index].max_speed_generation)
  351. return false;
  352. return true;
  353. }
  354. /**
  355. *
  356. * @sci_port: This is the port object for which to determine if the phy mask
  357. * can be supported.
  358. *
  359. * This method will return a true value if the port's phy mask can be supported
  360. * by the SCU. The following is a list of valid PHY mask configurations for
  361. * each port: - Port 0 - [[3 2] 1] 0 - Port 1 - [1] - Port 2 - [[3] 2]
  362. * - Port 3 - [3] This method returns a boolean indication specifying if the
  363. * phy mask can be supported. true if this is a valid phy assignment for the
  364. * port false if this is not a valid phy assignment for the port
  365. */
  366. static bool sci_port_is_phy_mask_valid(
  367. struct isci_port *iport,
  368. u32 phy_mask)
  369. {
  370. if (iport->physical_port_index == 0) {
  371. if (((phy_mask & 0x0F) == 0x0F)
  372. || ((phy_mask & 0x03) == 0x03)
  373. || ((phy_mask & 0x01) == 0x01)
  374. || (phy_mask == 0))
  375. return true;
  376. } else if (iport->physical_port_index == 1) {
  377. if (((phy_mask & 0x02) == 0x02)
  378. || (phy_mask == 0))
  379. return true;
  380. } else if (iport->physical_port_index == 2) {
  381. if (((phy_mask & 0x0C) == 0x0C)
  382. || ((phy_mask & 0x04) == 0x04)
  383. || (phy_mask == 0))
  384. return true;
  385. } else if (iport->physical_port_index == 3) {
  386. if (((phy_mask & 0x08) == 0x08)
  387. || (phy_mask == 0))
  388. return true;
  389. }
  390. return false;
  391. }
  392. /*
  393. * This method retrieves a currently active (i.e. connected) phy contained in
  394. * the port. Currently, the lowest order phy that is connected is returned.
  395. * This method returns a pointer to a SCIS_SDS_PHY object. NULL This value is
  396. * returned if there are no currently active (i.e. connected to a remote end
  397. * point) phys contained in the port. All other values specify a struct sci_phy
  398. * object that is active in the port.
  399. */
  400. static struct isci_phy *sci_port_get_a_connected_phy(struct isci_port *iport)
  401. {
  402. u32 index;
  403. struct isci_phy *iphy;
  404. for (index = 0; index < SCI_MAX_PHYS; index++) {
  405. /* Ensure that the phy is both part of the port and currently
  406. * connected to the remote end-point.
  407. */
  408. iphy = iport->phy_table[index];
  409. if (iphy && sci_port_active_phy(iport, iphy))
  410. return iphy;
  411. }
  412. return NULL;
  413. }
  414. static enum sci_status sci_port_set_phy(struct isci_port *iport, struct isci_phy *iphy)
  415. {
  416. /* Check to see if we can add this phy to a port
  417. * that means that the phy is not part of a port and that the port does
  418. * not already have a phy assinged to the phy index.
  419. */
  420. if (!iport->phy_table[iphy->phy_index] &&
  421. !phy_get_non_dummy_port(iphy) &&
  422. sci_port_is_valid_phy_assignment(iport, iphy->phy_index)) {
  423. /* Phy is being added in the stopped state so we are in MPC mode
  424. * make logical port index = physical port index
  425. */
  426. iport->logical_port_index = iport->physical_port_index;
  427. iport->phy_table[iphy->phy_index] = iphy;
  428. sci_phy_set_port(iphy, iport);
  429. return SCI_SUCCESS;
  430. }
  431. return SCI_FAILURE;
  432. }
  433. static enum sci_status sci_port_clear_phy(struct isci_port *iport, struct isci_phy *iphy)
  434. {
  435. /* Make sure that this phy is part of this port */
  436. if (iport->phy_table[iphy->phy_index] == iphy &&
  437. phy_get_non_dummy_port(iphy) == iport) {
  438. struct isci_host *ihost = iport->owning_controller;
  439. /* Yep it is assigned to this port so remove it */
  440. sci_phy_set_port(iphy, &ihost->ports[SCI_MAX_PORTS]);
  441. iport->phy_table[iphy->phy_index] = NULL;
  442. return SCI_SUCCESS;
  443. }
  444. return SCI_FAILURE;
  445. }
  446. void sci_port_get_sas_address(struct isci_port *iport, struct sci_sas_address *sas)
  447. {
  448. u32 index;
  449. sas->high = 0;
  450. sas->low = 0;
  451. for (index = 0; index < SCI_MAX_PHYS; index++)
  452. if (iport->phy_table[index])
  453. sci_phy_get_sas_address(iport->phy_table[index], sas);
  454. }
  455. void sci_port_get_attached_sas_address(struct isci_port *iport, struct sci_sas_address *sas)
  456. {
  457. struct isci_phy *iphy;
  458. /*
  459. * Ensure that the phy is both part of the port and currently
  460. * connected to the remote end-point.
  461. */
  462. iphy = sci_port_get_a_connected_phy(iport);
  463. if (iphy) {
  464. if (iphy->protocol != SCIC_SDS_PHY_PROTOCOL_SATA) {
  465. sci_phy_get_attached_sas_address(iphy, sas);
  466. } else {
  467. sci_phy_get_sas_address(iphy, sas);
  468. sas->low += iphy->phy_index;
  469. }
  470. } else {
  471. sas->high = 0;
  472. sas->low = 0;
  473. }
  474. }
  475. /**
  476. * sci_port_construct_dummy_rnc() - create dummy rnc for si workaround
  477. *
  478. * @sci_port: logical port on which we need to create the remote node context
  479. * @rni: remote node index for this remote node context.
  480. *
  481. * This routine will construct a dummy remote node context data structure
  482. * This structure will be posted to the hardware to work around a scheduler
  483. * error in the hardware.
  484. */
  485. static void sci_port_construct_dummy_rnc(struct isci_port *iport, u16 rni)
  486. {
  487. union scu_remote_node_context *rnc;
  488. rnc = &iport->owning_controller->remote_node_context_table[rni];
  489. memset(rnc, 0, sizeof(union scu_remote_node_context));
  490. rnc->ssp.remote_sas_address_hi = 0;
  491. rnc->ssp.remote_sas_address_lo = 0;
  492. rnc->ssp.remote_node_index = rni;
  493. rnc->ssp.remote_node_port_width = 1;
  494. rnc->ssp.logical_port_index = iport->physical_port_index;
  495. rnc->ssp.nexus_loss_timer_enable = false;
  496. rnc->ssp.check_bit = false;
  497. rnc->ssp.is_valid = true;
  498. rnc->ssp.is_remote_node_context = true;
  499. rnc->ssp.function_number = 0;
  500. rnc->ssp.arbitration_wait_time = 0;
  501. }
  502. /*
  503. * construct a dummy task context data structure. This
  504. * structure will be posted to the hardwre to work around a scheduler error
  505. * in the hardware.
  506. */
  507. static void sci_port_construct_dummy_task(struct isci_port *iport, u16 tag)
  508. {
  509. struct isci_host *ihost = iport->owning_controller;
  510. struct scu_task_context *task_context;
  511. task_context = &ihost->task_context_table[ISCI_TAG_TCI(tag)];
  512. memset(task_context, 0, sizeof(struct scu_task_context));
  513. task_context->initiator_request = 1;
  514. task_context->connection_rate = 1;
  515. task_context->logical_port_index = iport->physical_port_index;
  516. task_context->protocol_type = SCU_TASK_CONTEXT_PROTOCOL_SSP;
  517. task_context->task_index = ISCI_TAG_TCI(tag);
  518. task_context->valid = SCU_TASK_CONTEXT_VALID;
  519. task_context->context_type = SCU_TASK_CONTEXT_TYPE;
  520. task_context->remote_node_index = iport->reserved_rni;
  521. task_context->do_not_dma_ssp_good_response = 1;
  522. task_context->task_phase = 0x01;
  523. }
  524. static void sci_port_destroy_dummy_resources(struct isci_port *iport)
  525. {
  526. struct isci_host *ihost = iport->owning_controller;
  527. if (iport->reserved_tag != SCI_CONTROLLER_INVALID_IO_TAG)
  528. isci_free_tag(ihost, iport->reserved_tag);
  529. if (iport->reserved_rni != SCU_DUMMY_INDEX)
  530. sci_remote_node_table_release_remote_node_index(&ihost->available_remote_nodes,
  531. 1, iport->reserved_rni);
  532. iport->reserved_rni = SCU_DUMMY_INDEX;
  533. iport->reserved_tag = SCI_CONTROLLER_INVALID_IO_TAG;
  534. }
  535. void sci_port_setup_transports(struct isci_port *iport, u32 device_id)
  536. {
  537. u8 index;
  538. for (index = 0; index < SCI_MAX_PHYS; index++) {
  539. if (iport->active_phy_mask & (1 << index))
  540. sci_phy_setup_transport(iport->phy_table[index], device_id);
  541. }
  542. }
  543. static void sci_port_activate_phy(struct isci_port *iport, struct isci_phy *iphy,
  544. bool do_notify_user)
  545. {
  546. struct isci_host *ihost = iport->owning_controller;
  547. if (iphy->protocol != SCIC_SDS_PHY_PROTOCOL_SATA)
  548. sci_phy_resume(iphy);
  549. iport->active_phy_mask |= 1 << iphy->phy_index;
  550. sci_controller_clear_invalid_phy(ihost, iphy);
  551. if (do_notify_user == true)
  552. isci_port_link_up(ihost, iport, iphy);
  553. }
  554. void sci_port_deactivate_phy(struct isci_port *iport, struct isci_phy *iphy,
  555. bool do_notify_user)
  556. {
  557. struct isci_host *ihost = iport->owning_controller;
  558. iport->active_phy_mask &= ~(1 << iphy->phy_index);
  559. iphy->max_negotiated_speed = SAS_LINK_RATE_UNKNOWN;
  560. /* Re-assign the phy back to the LP as if it were a narrow port */
  561. writel(iphy->phy_index,
  562. &iport->port_pe_configuration_register[iphy->phy_index]);
  563. if (do_notify_user == true)
  564. isci_port_link_down(ihost, iphy, iport);
  565. }
  566. static void sci_port_invalid_link_up(struct isci_port *iport, struct isci_phy *iphy)
  567. {
  568. struct isci_host *ihost = iport->owning_controller;
  569. /*
  570. * Check to see if we have alreay reported this link as bad and if
  571. * not go ahead and tell the SCI_USER that we have discovered an
  572. * invalid link.
  573. */
  574. if ((ihost->invalid_phy_mask & (1 << iphy->phy_index)) == 0) {
  575. ihost->invalid_phy_mask |= 1 << iphy->phy_index;
  576. dev_warn(&ihost->pdev->dev, "Invalid link up!\n");
  577. }
  578. }
  579. static bool is_port_ready_state(enum sci_port_states state)
  580. {
  581. switch (state) {
  582. case SCI_PORT_READY:
  583. case SCI_PORT_SUB_WAITING:
  584. case SCI_PORT_SUB_OPERATIONAL:
  585. case SCI_PORT_SUB_CONFIGURING:
  586. return true;
  587. default:
  588. return false;
  589. }
  590. }
  591. /* flag dummy rnc hanling when exiting a ready state */
  592. static void port_state_machine_change(struct isci_port *iport,
  593. enum sci_port_states state)
  594. {
  595. struct sci_base_state_machine *sm = &iport->sm;
  596. enum sci_port_states old_state = sm->current_state_id;
  597. if (is_port_ready_state(old_state) && !is_port_ready_state(state))
  598. iport->ready_exit = true;
  599. sci_change_state(sm, state);
  600. iport->ready_exit = false;
  601. }
  602. /**
  603. * sci_port_general_link_up_handler - phy can be assigned to port?
  604. * @sci_port: sci_port object for which has a phy that has gone link up.
  605. * @sci_phy: This is the struct isci_phy object that has gone link up.
  606. * @do_notify_user: This parameter specifies whether to inform the user (via
  607. * sci_port_link_up()) as to the fact that a new phy as become ready.
  608. *
  609. * Determine if this phy can be assigned to this
  610. * port . If the phy is not a valid PHY for
  611. * this port then the function will notify the user. A PHY can only be
  612. * part of a port if it's attached SAS ADDRESS is the same as all other PHYs in
  613. * the same port. none
  614. */
  615. static void sci_port_general_link_up_handler(struct isci_port *iport,
  616. struct isci_phy *iphy,
  617. bool do_notify_user)
  618. {
  619. struct sci_sas_address port_sas_address;
  620. struct sci_sas_address phy_sas_address;
  621. sci_port_get_attached_sas_address(iport, &port_sas_address);
  622. sci_phy_get_attached_sas_address(iphy, &phy_sas_address);
  623. /* If the SAS address of the new phy matches the SAS address of
  624. * other phys in the port OR this is the first phy in the port,
  625. * then activate the phy and allow it to be used for operations
  626. * in this port.
  627. */
  628. if ((phy_sas_address.high == port_sas_address.high &&
  629. phy_sas_address.low == port_sas_address.low) ||
  630. iport->active_phy_mask == 0) {
  631. struct sci_base_state_machine *sm = &iport->sm;
  632. sci_port_activate_phy(iport, iphy, do_notify_user);
  633. if (sm->current_state_id == SCI_PORT_RESETTING)
  634. port_state_machine_change(iport, SCI_PORT_READY);
  635. } else
  636. sci_port_invalid_link_up(iport, iphy);
  637. }
  638. /**
  639. * This method returns false if the port only has a single phy object assigned.
  640. * If there are no phys or more than one phy then the method will return
  641. * true.
  642. * @sci_port: The port for which the wide port condition is to be checked.
  643. *
  644. * bool true Is returned if this is a wide ported port. false Is returned if
  645. * this is a narrow port.
  646. */
  647. static bool sci_port_is_wide(struct isci_port *iport)
  648. {
  649. u32 index;
  650. u32 phy_count = 0;
  651. for (index = 0; index < SCI_MAX_PHYS; index++) {
  652. if (iport->phy_table[index] != NULL) {
  653. phy_count++;
  654. }
  655. }
  656. return phy_count != 1;
  657. }
  658. /**
  659. * This method is called by the PHY object when the link is detected. if the
  660. * port wants the PHY to continue on to the link up state then the port
  661. * layer must return true. If the port object returns false the phy object
  662. * must halt its attempt to go link up.
  663. * @sci_port: The port associated with the phy object.
  664. * @sci_phy: The phy object that is trying to go link up.
  665. *
  666. * true if the phy object can continue to the link up condition. true Is
  667. * returned if this phy can continue to the ready state. false Is returned if
  668. * can not continue on to the ready state. This notification is in place for
  669. * wide ports and direct attached phys. Since there are no wide ported SATA
  670. * devices this could become an invalid port configuration.
  671. */
  672. bool sci_port_link_detected(
  673. struct isci_port *iport,
  674. struct isci_phy *iphy)
  675. {
  676. if ((iport->logical_port_index != SCIC_SDS_DUMMY_PORT) &&
  677. (iphy->protocol == SCIC_SDS_PHY_PROTOCOL_SATA) &&
  678. sci_port_is_wide(iport)) {
  679. sci_port_invalid_link_up(iport, iphy);
  680. return false;
  681. }
  682. return true;
  683. }
  684. static void port_timeout(unsigned long data)
  685. {
  686. struct sci_timer *tmr = (struct sci_timer *)data;
  687. struct isci_port *iport = container_of(tmr, typeof(*iport), timer);
  688. struct isci_host *ihost = iport->owning_controller;
  689. unsigned long flags;
  690. u32 current_state;
  691. spin_lock_irqsave(&ihost->scic_lock, flags);
  692. if (tmr->cancel)
  693. goto done;
  694. current_state = iport->sm.current_state_id;
  695. if (current_state == SCI_PORT_RESETTING) {
  696. /* if the port is still in the resetting state then the timeout
  697. * fired before the reset completed.
  698. */
  699. port_state_machine_change(iport, SCI_PORT_FAILED);
  700. } else if (current_state == SCI_PORT_STOPPED) {
  701. /* if the port is stopped then the start request failed In this
  702. * case stay in the stopped state.
  703. */
  704. dev_err(sciport_to_dev(iport),
  705. "%s: SCIC Port 0x%p failed to stop before tiemout.\n",
  706. __func__,
  707. iport);
  708. } else if (current_state == SCI_PORT_STOPPING) {
  709. /* if the port is still stopping then the stop has not completed */
  710. isci_port_stop_complete(iport->owning_controller,
  711. iport,
  712. SCI_FAILURE_TIMEOUT);
  713. } else {
  714. /* The port is in the ready state and we have a timer
  715. * reporting a timeout this should not happen.
  716. */
  717. dev_err(sciport_to_dev(iport),
  718. "%s: SCIC Port 0x%p is processing a timeout operation "
  719. "in state %d.\n", __func__, iport, current_state);
  720. }
  721. done:
  722. spin_unlock_irqrestore(&ihost->scic_lock, flags);
  723. }
  724. /* --------------------------------------------------------------------------- */
  725. /**
  726. * This function updates the hardwares VIIT entry for this port.
  727. *
  728. *
  729. */
  730. static void sci_port_update_viit_entry(struct isci_port *iport)
  731. {
  732. struct sci_sas_address sas_address;
  733. sci_port_get_sas_address(iport, &sas_address);
  734. writel(sas_address.high,
  735. &iport->viit_registers->initiator_sas_address_hi);
  736. writel(sas_address.low,
  737. &iport->viit_registers->initiator_sas_address_lo);
  738. /* This value get cleared just in case its not already cleared */
  739. writel(0, &iport->viit_registers->reserved);
  740. /* We are required to update the status register last */
  741. writel(SCU_VIIT_ENTRY_ID_VIIT |
  742. SCU_VIIT_IPPT_INITIATOR |
  743. ((1 << iport->physical_port_index) << SCU_VIIT_ENTRY_LPVIE_SHIFT) |
  744. SCU_VIIT_STATUS_ALL_VALID,
  745. &iport->viit_registers->status);
  746. }
  747. enum sas_linkrate sci_port_get_max_allowed_speed(struct isci_port *iport)
  748. {
  749. u16 index;
  750. struct isci_phy *iphy;
  751. enum sas_linkrate max_allowed_speed = SAS_LINK_RATE_6_0_GBPS;
  752. /*
  753. * Loop through all of the phys in this port and find the phy with the
  754. * lowest maximum link rate. */
  755. for (index = 0; index < SCI_MAX_PHYS; index++) {
  756. iphy = iport->phy_table[index];
  757. if (iphy && sci_port_active_phy(iport, iphy) &&
  758. iphy->max_negotiated_speed < max_allowed_speed)
  759. max_allowed_speed = iphy->max_negotiated_speed;
  760. }
  761. return max_allowed_speed;
  762. }
  763. static void sci_port_suspend_port_task_scheduler(struct isci_port *iport)
  764. {
  765. u32 pts_control_value;
  766. pts_control_value = readl(&iport->port_task_scheduler_registers->control);
  767. pts_control_value |= SCU_PTSxCR_GEN_BIT(SUSPEND);
  768. writel(pts_control_value, &iport->port_task_scheduler_registers->control);
  769. }
  770. /**
  771. * sci_port_post_dummy_request() - post dummy/workaround request
  772. * @sci_port: port to post task
  773. *
  774. * Prevent the hardware scheduler from posting new requests to the front
  775. * of the scheduler queue causing a starvation problem for currently
  776. * ongoing requests.
  777. *
  778. */
  779. static void sci_port_post_dummy_request(struct isci_port *iport)
  780. {
  781. struct isci_host *ihost = iport->owning_controller;
  782. u16 tag = iport->reserved_tag;
  783. struct scu_task_context *tc;
  784. u32 command;
  785. tc = &ihost->task_context_table[ISCI_TAG_TCI(tag)];
  786. tc->abort = 0;
  787. command = SCU_CONTEXT_COMMAND_REQUEST_TYPE_POST_TC |
  788. iport->physical_port_index << SCU_CONTEXT_COMMAND_LOGICAL_PORT_SHIFT |
  789. ISCI_TAG_TCI(tag);
  790. sci_controller_post_request(ihost, command);
  791. }
  792. /**
  793. * This routine will abort the dummy request. This will alow the hardware to
  794. * power down parts of the silicon to save power.
  795. *
  796. * @sci_port: The port on which the task must be aborted.
  797. *
  798. */
  799. static void sci_port_abort_dummy_request(struct isci_port *iport)
  800. {
  801. struct isci_host *ihost = iport->owning_controller;
  802. u16 tag = iport->reserved_tag;
  803. struct scu_task_context *tc;
  804. u32 command;
  805. tc = &ihost->task_context_table[ISCI_TAG_TCI(tag)];
  806. tc->abort = 1;
  807. command = SCU_CONTEXT_COMMAND_REQUEST_POST_TC_ABORT |
  808. iport->physical_port_index << SCU_CONTEXT_COMMAND_LOGICAL_PORT_SHIFT |
  809. ISCI_TAG_TCI(tag);
  810. sci_controller_post_request(ihost, command);
  811. }
  812. /**
  813. *
  814. * @sci_port: This is the struct isci_port object to resume.
  815. *
  816. * This method will resume the port task scheduler for this port object. none
  817. */
  818. static void
  819. sci_port_resume_port_task_scheduler(struct isci_port *iport)
  820. {
  821. u32 pts_control_value;
  822. pts_control_value = readl(&iport->port_task_scheduler_registers->control);
  823. pts_control_value &= ~SCU_PTSxCR_GEN_BIT(SUSPEND);
  824. writel(pts_control_value, &iport->port_task_scheduler_registers->control);
  825. }
  826. static void sci_port_ready_substate_waiting_enter(struct sci_base_state_machine *sm)
  827. {
  828. struct isci_port *iport = container_of(sm, typeof(*iport), sm);
  829. sci_port_suspend_port_task_scheduler(iport);
  830. iport->not_ready_reason = SCIC_PORT_NOT_READY_NO_ACTIVE_PHYS;
  831. if (iport->active_phy_mask != 0) {
  832. /* At least one of the phys on the port is ready */
  833. port_state_machine_change(iport,
  834. SCI_PORT_SUB_OPERATIONAL);
  835. }
  836. }
  837. static void sci_port_ready_substate_operational_enter(struct sci_base_state_machine *sm)
  838. {
  839. u32 index;
  840. struct isci_port *iport = container_of(sm, typeof(*iport), sm);
  841. struct isci_host *ihost = iport->owning_controller;
  842. isci_port_ready(ihost, iport);
  843. for (index = 0; index < SCI_MAX_PHYS; index++) {
  844. if (iport->phy_table[index]) {
  845. writel(iport->physical_port_index,
  846. &iport->port_pe_configuration_register[
  847. iport->phy_table[index]->phy_index]);
  848. }
  849. }
  850. sci_port_update_viit_entry(iport);
  851. sci_port_resume_port_task_scheduler(iport);
  852. /*
  853. * Post the dummy task for the port so the hardware can schedule
  854. * io correctly
  855. */
  856. sci_port_post_dummy_request(iport);
  857. }
  858. static void sci_port_invalidate_dummy_remote_node(struct isci_port *iport)
  859. {
  860. struct isci_host *ihost = iport->owning_controller;
  861. u8 phys_index = iport->physical_port_index;
  862. union scu_remote_node_context *rnc;
  863. u16 rni = iport->reserved_rni;
  864. u32 command;
  865. rnc = &ihost->remote_node_context_table[rni];
  866. rnc->ssp.is_valid = false;
  867. /* ensure the preceding tc abort request has reached the
  868. * controller and give it ample time to act before posting the rnc
  869. * invalidate
  870. */
  871. readl(&ihost->smu_registers->interrupt_status); /* flush */
  872. udelay(10);
  873. command = SCU_CONTEXT_COMMAND_POST_RNC_INVALIDATE |
  874. phys_index << SCU_CONTEXT_COMMAND_LOGICAL_PORT_SHIFT | rni;
  875. sci_controller_post_request(ihost, command);
  876. }
  877. /**
  878. *
  879. * @object: This is the object which is cast to a struct isci_port object.
  880. *
  881. * This method will perform the actions required by the struct isci_port on
  882. * exiting the SCI_PORT_SUB_OPERATIONAL. This function reports
  883. * the port not ready and suspends the port task scheduler. none
  884. */
  885. static void sci_port_ready_substate_operational_exit(struct sci_base_state_machine *sm)
  886. {
  887. struct isci_port *iport = container_of(sm, typeof(*iport), sm);
  888. struct isci_host *ihost = iport->owning_controller;
  889. /*
  890. * Kill the dummy task for this port if it has not yet posted
  891. * the hardware will treat this as a NOP and just return abort
  892. * complete.
  893. */
  894. sci_port_abort_dummy_request(iport);
  895. isci_port_not_ready(ihost, iport);
  896. if (iport->ready_exit)
  897. sci_port_invalidate_dummy_remote_node(iport);
  898. }
  899. static void sci_port_ready_substate_configuring_enter(struct sci_base_state_machine *sm)
  900. {
  901. struct isci_port *iport = container_of(sm, typeof(*iport), sm);
  902. struct isci_host *ihost = iport->owning_controller;
  903. if (iport->active_phy_mask == 0) {
  904. isci_port_not_ready(ihost, iport);
  905. port_state_machine_change(iport,
  906. SCI_PORT_SUB_WAITING);
  907. } else if (iport->started_request_count == 0)
  908. port_state_machine_change(iport,
  909. SCI_PORT_SUB_OPERATIONAL);
  910. }
  911. static void sci_port_ready_substate_configuring_exit(struct sci_base_state_machine *sm)
  912. {
  913. struct isci_port *iport = container_of(sm, typeof(*iport), sm);
  914. sci_port_suspend_port_task_scheduler(iport);
  915. if (iport->ready_exit)
  916. sci_port_invalidate_dummy_remote_node(iport);
  917. }
  918. enum sci_status sci_port_start(struct isci_port *iport)
  919. {
  920. struct isci_host *ihost = iport->owning_controller;
  921. enum sci_status status = SCI_SUCCESS;
  922. enum sci_port_states state;
  923. u32 phy_mask;
  924. state = iport->sm.current_state_id;
  925. if (state != SCI_PORT_STOPPED) {
  926. dev_warn(sciport_to_dev(iport),
  927. "%s: in wrong state: %d\n", __func__, state);
  928. return SCI_FAILURE_INVALID_STATE;
  929. }
  930. if (iport->assigned_device_count > 0) {
  931. /* TODO This is a start failure operation because
  932. * there are still devices assigned to this port.
  933. * There must be no devices assigned to a port on a
  934. * start operation.
  935. */
  936. return SCI_FAILURE_UNSUPPORTED_PORT_CONFIGURATION;
  937. }
  938. if (iport->reserved_rni == SCU_DUMMY_INDEX) {
  939. u16 rni = sci_remote_node_table_allocate_remote_node(
  940. &ihost->available_remote_nodes, 1);
  941. if (rni != SCU_DUMMY_INDEX)
  942. sci_port_construct_dummy_rnc(iport, rni);
  943. else
  944. status = SCI_FAILURE_INSUFFICIENT_RESOURCES;
  945. iport->reserved_rni = rni;
  946. }
  947. if (iport->reserved_tag == SCI_CONTROLLER_INVALID_IO_TAG) {
  948. u16 tag;
  949. tag = isci_alloc_tag(ihost);
  950. if (tag == SCI_CONTROLLER_INVALID_IO_TAG)
  951. status = SCI_FAILURE_INSUFFICIENT_RESOURCES;
  952. else
  953. sci_port_construct_dummy_task(iport, tag);
  954. iport->reserved_tag = tag;
  955. }
  956. if (status == SCI_SUCCESS) {
  957. phy_mask = sci_port_get_phys(iport);
  958. /*
  959. * There are one or more phys assigned to this port. Make sure
  960. * the port's phy mask is in fact legal and supported by the
  961. * silicon.
  962. */
  963. if (sci_port_is_phy_mask_valid(iport, phy_mask) == true) {
  964. port_state_machine_change(iport,
  965. SCI_PORT_READY);
  966. return SCI_SUCCESS;
  967. }
  968. status = SCI_FAILURE;
  969. }
  970. if (status != SCI_SUCCESS)
  971. sci_port_destroy_dummy_resources(iport);
  972. return status;
  973. }
  974. enum sci_status sci_port_stop(struct isci_port *iport)
  975. {
  976. enum sci_port_states state;
  977. state = iport->sm.current_state_id;
  978. switch (state) {
  979. case SCI_PORT_STOPPED:
  980. return SCI_SUCCESS;
  981. case SCI_PORT_SUB_WAITING:
  982. case SCI_PORT_SUB_OPERATIONAL:
  983. case SCI_PORT_SUB_CONFIGURING:
  984. case SCI_PORT_RESETTING:
  985. port_state_machine_change(iport,
  986. SCI_PORT_STOPPING);
  987. return SCI_SUCCESS;
  988. default:
  989. dev_warn(sciport_to_dev(iport),
  990. "%s: in wrong state: %d\n", __func__, state);
  991. return SCI_FAILURE_INVALID_STATE;
  992. }
  993. }
  994. static enum sci_status sci_port_hard_reset(struct isci_port *iport, u32 timeout)
  995. {
  996. enum sci_status status = SCI_FAILURE_INVALID_PHY;
  997. struct isci_phy *iphy = NULL;
  998. enum sci_port_states state;
  999. u32 phy_index;
  1000. state = iport->sm.current_state_id;
  1001. if (state != SCI_PORT_SUB_OPERATIONAL) {
  1002. dev_warn(sciport_to_dev(iport),
  1003. "%s: in wrong state: %d\n", __func__, state);
  1004. return SCI_FAILURE_INVALID_STATE;
  1005. }
  1006. /* Select a phy on which we can send the hard reset request. */
  1007. for (phy_index = 0; phy_index < SCI_MAX_PHYS && !iphy; phy_index++) {
  1008. iphy = iport->phy_table[phy_index];
  1009. if (iphy && !sci_port_active_phy(iport, iphy)) {
  1010. /*
  1011. * We found a phy but it is not ready select
  1012. * different phy
  1013. */
  1014. iphy = NULL;
  1015. }
  1016. }
  1017. /* If we have a phy then go ahead and start the reset procedure */
  1018. if (!iphy)
  1019. return status;
  1020. status = sci_phy_reset(iphy);
  1021. if (status != SCI_SUCCESS)
  1022. return status;
  1023. sci_mod_timer(&iport->timer, timeout);
  1024. iport->not_ready_reason = SCIC_PORT_NOT_READY_HARD_RESET_REQUESTED;
  1025. port_state_machine_change(iport, SCI_PORT_RESETTING);
  1026. return SCI_SUCCESS;
  1027. }
  1028. /**
  1029. * sci_port_add_phy() -
  1030. * @sci_port: This parameter specifies the port in which the phy will be added.
  1031. * @sci_phy: This parameter is the phy which is to be added to the port.
  1032. *
  1033. * This method will add a PHY to the selected port. This method returns an
  1034. * enum sci_status. SCI_SUCCESS the phy has been added to the port. Any other
  1035. * status is a failure to add the phy to the port.
  1036. */
  1037. enum sci_status sci_port_add_phy(struct isci_port *iport,
  1038. struct isci_phy *iphy)
  1039. {
  1040. enum sci_status status;
  1041. enum sci_port_states state;
  1042. state = iport->sm.current_state_id;
  1043. switch (state) {
  1044. case SCI_PORT_STOPPED: {
  1045. struct sci_sas_address port_sas_address;
  1046. /* Read the port assigned SAS Address if there is one */
  1047. sci_port_get_sas_address(iport, &port_sas_address);
  1048. if (port_sas_address.high != 0 && port_sas_address.low != 0) {
  1049. struct sci_sas_address phy_sas_address;
  1050. /* Make sure that the PHY SAS Address matches the SAS Address
  1051. * for this port
  1052. */
  1053. sci_phy_get_sas_address(iphy, &phy_sas_address);
  1054. if (port_sas_address.high != phy_sas_address.high ||
  1055. port_sas_address.low != phy_sas_address.low)
  1056. return SCI_FAILURE_UNSUPPORTED_PORT_CONFIGURATION;
  1057. }
  1058. return sci_port_set_phy(iport, iphy);
  1059. }
  1060. case SCI_PORT_SUB_WAITING:
  1061. case SCI_PORT_SUB_OPERATIONAL:
  1062. status = sci_port_set_phy(iport, iphy);
  1063. if (status != SCI_SUCCESS)
  1064. return status;
  1065. sci_port_general_link_up_handler(iport, iphy, true);
  1066. iport->not_ready_reason = SCIC_PORT_NOT_READY_RECONFIGURING;
  1067. port_state_machine_change(iport, SCI_PORT_SUB_CONFIGURING);
  1068. return status;
  1069. case SCI_PORT_SUB_CONFIGURING:
  1070. status = sci_port_set_phy(iport, iphy);
  1071. if (status != SCI_SUCCESS)
  1072. return status;
  1073. sci_port_general_link_up_handler(iport, iphy, true);
  1074. /* Re-enter the configuring state since this may be the last phy in
  1075. * the port.
  1076. */
  1077. port_state_machine_change(iport,
  1078. SCI_PORT_SUB_CONFIGURING);
  1079. return SCI_SUCCESS;
  1080. default:
  1081. dev_warn(sciport_to_dev(iport),
  1082. "%s: in wrong state: %d\n", __func__, state);
  1083. return SCI_FAILURE_INVALID_STATE;
  1084. }
  1085. }
  1086. /**
  1087. * sci_port_remove_phy() -
  1088. * @sci_port: This parameter specifies the port in which the phy will be added.
  1089. * @sci_phy: This parameter is the phy which is to be added to the port.
  1090. *
  1091. * This method will remove the PHY from the selected PORT. This method returns
  1092. * an enum sci_status. SCI_SUCCESS the phy has been removed from the port. Any
  1093. * other status is a failure to add the phy to the port.
  1094. */
  1095. enum sci_status sci_port_remove_phy(struct isci_port *iport,
  1096. struct isci_phy *iphy)
  1097. {
  1098. enum sci_status status;
  1099. enum sci_port_states state;
  1100. state = iport->sm.current_state_id;
  1101. switch (state) {
  1102. case SCI_PORT_STOPPED:
  1103. return sci_port_clear_phy(iport, iphy);
  1104. case SCI_PORT_SUB_OPERATIONAL:
  1105. status = sci_port_clear_phy(iport, iphy);
  1106. if (status != SCI_SUCCESS)
  1107. return status;
  1108. sci_port_deactivate_phy(iport, iphy, true);
  1109. iport->not_ready_reason = SCIC_PORT_NOT_READY_RECONFIGURING;
  1110. port_state_machine_change(iport,
  1111. SCI_PORT_SUB_CONFIGURING);
  1112. return SCI_SUCCESS;
  1113. case SCI_PORT_SUB_CONFIGURING:
  1114. status = sci_port_clear_phy(iport, iphy);
  1115. if (status != SCI_SUCCESS)
  1116. return status;
  1117. sci_port_deactivate_phy(iport, iphy, true);
  1118. /* Re-enter the configuring state since this may be the last phy in
  1119. * the port
  1120. */
  1121. port_state_machine_change(iport,
  1122. SCI_PORT_SUB_CONFIGURING);
  1123. return SCI_SUCCESS;
  1124. default:
  1125. dev_warn(sciport_to_dev(iport),
  1126. "%s: in wrong state: %d\n", __func__, state);
  1127. return SCI_FAILURE_INVALID_STATE;
  1128. }
  1129. }
  1130. enum sci_status sci_port_link_up(struct isci_port *iport,
  1131. struct isci_phy *iphy)
  1132. {
  1133. enum sci_port_states state;
  1134. state = iport->sm.current_state_id;
  1135. switch (state) {
  1136. case SCI_PORT_SUB_WAITING:
  1137. /* Since this is the first phy going link up for the port we
  1138. * can just enable it and continue
  1139. */
  1140. sci_port_activate_phy(iport, iphy, true);
  1141. port_state_machine_change(iport,
  1142. SCI_PORT_SUB_OPERATIONAL);
  1143. return SCI_SUCCESS;
  1144. case SCI_PORT_SUB_OPERATIONAL:
  1145. sci_port_general_link_up_handler(iport, iphy, true);
  1146. return SCI_SUCCESS;
  1147. case SCI_PORT_RESETTING:
  1148. /* TODO We should make sure that the phy that has gone
  1149. * link up is the same one on which we sent the reset. It is
  1150. * possible that the phy on which we sent the reset is not the
  1151. * one that has gone link up and we want to make sure that
  1152. * phy being reset comes back. Consider the case where a
  1153. * reset is sent but before the hardware processes the reset it
  1154. * get a link up on the port because of a hot plug event.
  1155. * because of the reset request this phy will go link down
  1156. * almost immediately.
  1157. */
  1158. /* In the resetting state we don't notify the user regarding
  1159. * link up and link down notifications.
  1160. */
  1161. sci_port_general_link_up_handler(iport, iphy, false);
  1162. return SCI_SUCCESS;
  1163. default:
  1164. dev_warn(sciport_to_dev(iport),
  1165. "%s: in wrong state: %d\n", __func__, state);
  1166. return SCI_FAILURE_INVALID_STATE;
  1167. }
  1168. }
  1169. enum sci_status sci_port_link_down(struct isci_port *iport,
  1170. struct isci_phy *iphy)
  1171. {
  1172. enum sci_port_states state;
  1173. state = iport->sm.current_state_id;
  1174. switch (state) {
  1175. case SCI_PORT_SUB_OPERATIONAL:
  1176. sci_port_deactivate_phy(iport, iphy, true);
  1177. /* If there are no active phys left in the port, then
  1178. * transition the port to the WAITING state until such time
  1179. * as a phy goes link up
  1180. */
  1181. if (iport->active_phy_mask == 0)
  1182. port_state_machine_change(iport,
  1183. SCI_PORT_SUB_WAITING);
  1184. return SCI_SUCCESS;
  1185. case SCI_PORT_RESETTING:
  1186. /* In the resetting state we don't notify the user regarding
  1187. * link up and link down notifications. */
  1188. sci_port_deactivate_phy(iport, iphy, false);
  1189. return SCI_SUCCESS;
  1190. default:
  1191. dev_warn(sciport_to_dev(iport),
  1192. "%s: in wrong state: %d\n", __func__, state);
  1193. return SCI_FAILURE_INVALID_STATE;
  1194. }
  1195. }
  1196. enum sci_status sci_port_start_io(struct isci_port *iport,
  1197. struct isci_remote_device *idev,
  1198. struct isci_request *ireq)
  1199. {
  1200. enum sci_port_states state;
  1201. state = iport->sm.current_state_id;
  1202. switch (state) {
  1203. case SCI_PORT_SUB_WAITING:
  1204. return SCI_FAILURE_INVALID_STATE;
  1205. case SCI_PORT_SUB_OPERATIONAL:
  1206. iport->started_request_count++;
  1207. return SCI_SUCCESS;
  1208. default:
  1209. dev_warn(sciport_to_dev(iport),
  1210. "%s: in wrong state: %d\n", __func__, state);
  1211. return SCI_FAILURE_INVALID_STATE;
  1212. }
  1213. }
  1214. enum sci_status sci_port_complete_io(struct isci_port *iport,
  1215. struct isci_remote_device *idev,
  1216. struct isci_request *ireq)
  1217. {
  1218. enum sci_port_states state;
  1219. state = iport->sm.current_state_id;
  1220. switch (state) {
  1221. case SCI_PORT_STOPPED:
  1222. dev_warn(sciport_to_dev(iport),
  1223. "%s: in wrong state: %d\n", __func__, state);
  1224. return SCI_FAILURE_INVALID_STATE;
  1225. case SCI_PORT_STOPPING:
  1226. sci_port_decrement_request_count(iport);
  1227. if (iport->started_request_count == 0)
  1228. port_state_machine_change(iport,
  1229. SCI_PORT_STOPPED);
  1230. break;
  1231. case SCI_PORT_READY:
  1232. case SCI_PORT_RESETTING:
  1233. case SCI_PORT_FAILED:
  1234. case SCI_PORT_SUB_WAITING:
  1235. case SCI_PORT_SUB_OPERATIONAL:
  1236. sci_port_decrement_request_count(iport);
  1237. break;
  1238. case SCI_PORT_SUB_CONFIGURING:
  1239. sci_port_decrement_request_count(iport);
  1240. if (iport->started_request_count == 0) {
  1241. port_state_machine_change(iport,
  1242. SCI_PORT_SUB_OPERATIONAL);
  1243. }
  1244. break;
  1245. }
  1246. return SCI_SUCCESS;
  1247. }
  1248. static void sci_port_enable_port_task_scheduler(struct isci_port *iport)
  1249. {
  1250. u32 pts_control_value;
  1251. /* enable the port task scheduler in a suspended state */
  1252. pts_control_value = readl(&iport->port_task_scheduler_registers->control);
  1253. pts_control_value |= SCU_PTSxCR_GEN_BIT(ENABLE) | SCU_PTSxCR_GEN_BIT(SUSPEND);
  1254. writel(pts_control_value, &iport->port_task_scheduler_registers->control);
  1255. }
  1256. static void sci_port_disable_port_task_scheduler(struct isci_port *iport)
  1257. {
  1258. u32 pts_control_value;
  1259. pts_control_value = readl(&iport->port_task_scheduler_registers->control);
  1260. pts_control_value &=
  1261. ~(SCU_PTSxCR_GEN_BIT(ENABLE) | SCU_PTSxCR_GEN_BIT(SUSPEND));
  1262. writel(pts_control_value, &iport->port_task_scheduler_registers->control);
  1263. }
  1264. static void sci_port_post_dummy_remote_node(struct isci_port *iport)
  1265. {
  1266. struct isci_host *ihost = iport->owning_controller;
  1267. u8 phys_index = iport->physical_port_index;
  1268. union scu_remote_node_context *rnc;
  1269. u16 rni = iport->reserved_rni;
  1270. u32 command;
  1271. rnc = &ihost->remote_node_context_table[rni];
  1272. rnc->ssp.is_valid = true;
  1273. command = SCU_CONTEXT_COMMAND_POST_RNC_32 |
  1274. phys_index << SCU_CONTEXT_COMMAND_LOGICAL_PORT_SHIFT | rni;
  1275. sci_controller_post_request(ihost, command);
  1276. /* ensure hardware has seen the post rnc command and give it
  1277. * ample time to act before sending the suspend
  1278. */
  1279. readl(&ihost->smu_registers->interrupt_status); /* flush */
  1280. udelay(10);
  1281. command = SCU_CONTEXT_COMMAND_POST_RNC_SUSPEND_TX_RX |
  1282. phys_index << SCU_CONTEXT_COMMAND_LOGICAL_PORT_SHIFT | rni;
  1283. sci_controller_post_request(ihost, command);
  1284. }
  1285. static void sci_port_stopped_state_enter(struct sci_base_state_machine *sm)
  1286. {
  1287. struct isci_port *iport = container_of(sm, typeof(*iport), sm);
  1288. if (iport->sm.previous_state_id == SCI_PORT_STOPPING) {
  1289. /*
  1290. * If we enter this state becasuse of a request to stop
  1291. * the port then we want to disable the hardwares port
  1292. * task scheduler. */
  1293. sci_port_disable_port_task_scheduler(iport);
  1294. }
  1295. }
  1296. static void sci_port_stopped_state_exit(struct sci_base_state_machine *sm)
  1297. {
  1298. struct isci_port *iport = container_of(sm, typeof(*iport), sm);
  1299. /* Enable and suspend the port task scheduler */
  1300. sci_port_enable_port_task_scheduler(iport);
  1301. }
  1302. static void sci_port_ready_state_enter(struct sci_base_state_machine *sm)
  1303. {
  1304. struct isci_port *iport = container_of(sm, typeof(*iport), sm);
  1305. struct isci_host *ihost = iport->owning_controller;
  1306. u32 prev_state;
  1307. prev_state = iport->sm.previous_state_id;
  1308. if (prev_state == SCI_PORT_RESETTING)
  1309. isci_port_hard_reset_complete(iport, SCI_SUCCESS);
  1310. else
  1311. isci_port_not_ready(ihost, iport);
  1312. /* Post and suspend the dummy remote node context for this port. */
  1313. sci_port_post_dummy_remote_node(iport);
  1314. /* Start the ready substate machine */
  1315. port_state_machine_change(iport,
  1316. SCI_PORT_SUB_WAITING);
  1317. }
  1318. static void sci_port_resetting_state_exit(struct sci_base_state_machine *sm)
  1319. {
  1320. struct isci_port *iport = container_of(sm, typeof(*iport), sm);
  1321. sci_del_timer(&iport->timer);
  1322. }
  1323. static void sci_port_stopping_state_exit(struct sci_base_state_machine *sm)
  1324. {
  1325. struct isci_port *iport = container_of(sm, typeof(*iport), sm);
  1326. sci_del_timer(&iport->timer);
  1327. sci_port_destroy_dummy_resources(iport);
  1328. }
  1329. static void sci_port_failed_state_enter(struct sci_base_state_machine *sm)
  1330. {
  1331. struct isci_port *iport = container_of(sm, typeof(*iport), sm);
  1332. isci_port_hard_reset_complete(iport, SCI_FAILURE_TIMEOUT);
  1333. }
  1334. /* --------------------------------------------------------------------------- */
  1335. static const struct sci_base_state sci_port_state_table[] = {
  1336. [SCI_PORT_STOPPED] = {
  1337. .enter_state = sci_port_stopped_state_enter,
  1338. .exit_state = sci_port_stopped_state_exit
  1339. },
  1340. [SCI_PORT_STOPPING] = {
  1341. .exit_state = sci_port_stopping_state_exit
  1342. },
  1343. [SCI_PORT_READY] = {
  1344. .enter_state = sci_port_ready_state_enter,
  1345. },
  1346. [SCI_PORT_SUB_WAITING] = {
  1347. .enter_state = sci_port_ready_substate_waiting_enter,
  1348. },
  1349. [SCI_PORT_SUB_OPERATIONAL] = {
  1350. .enter_state = sci_port_ready_substate_operational_enter,
  1351. .exit_state = sci_port_ready_substate_operational_exit
  1352. },
  1353. [SCI_PORT_SUB_CONFIGURING] = {
  1354. .enter_state = sci_port_ready_substate_configuring_enter,
  1355. .exit_state = sci_port_ready_substate_configuring_exit
  1356. },
  1357. [SCI_PORT_RESETTING] = {
  1358. .exit_state = sci_port_resetting_state_exit
  1359. },
  1360. [SCI_PORT_FAILED] = {
  1361. .enter_state = sci_port_failed_state_enter,
  1362. }
  1363. };
  1364. void sci_port_construct(struct isci_port *iport, u8 index,
  1365. struct isci_host *ihost)
  1366. {
  1367. sci_init_sm(&iport->sm, sci_port_state_table, SCI_PORT_STOPPED);
  1368. iport->logical_port_index = SCIC_SDS_DUMMY_PORT;
  1369. iport->physical_port_index = index;
  1370. iport->active_phy_mask = 0;
  1371. iport->ready_exit = false;
  1372. iport->owning_controller = ihost;
  1373. iport->started_request_count = 0;
  1374. iport->assigned_device_count = 0;
  1375. iport->reserved_rni = SCU_DUMMY_INDEX;
  1376. iport->reserved_tag = SCI_CONTROLLER_INVALID_IO_TAG;
  1377. sci_init_timer(&iport->timer, port_timeout);
  1378. iport->port_task_scheduler_registers = NULL;
  1379. for (index = 0; index < SCI_MAX_PHYS; index++)
  1380. iport->phy_table[index] = NULL;
  1381. }
  1382. void isci_port_init(struct isci_port *iport, struct isci_host *ihost, int index)
  1383. {
  1384. INIT_LIST_HEAD(&iport->remote_dev_list);
  1385. INIT_LIST_HEAD(&iport->domain_dev_list);
  1386. spin_lock_init(&iport->state_lock);
  1387. init_completion(&iport->start_complete);
  1388. iport->isci_host = ihost;
  1389. isci_port_change_state(iport, isci_freed);
  1390. atomic_set(&iport->event, 0);
  1391. }
  1392. /**
  1393. * isci_port_get_state() - This function gets the status of the port object.
  1394. * @isci_port: This parameter points to the isci_port object
  1395. *
  1396. * status of the object as a isci_status enum.
  1397. */
  1398. enum isci_status isci_port_get_state(
  1399. struct isci_port *isci_port)
  1400. {
  1401. return isci_port->status;
  1402. }
  1403. void sci_port_broadcast_change_received(struct isci_port *iport, struct isci_phy *iphy)
  1404. {
  1405. struct isci_host *ihost = iport->owning_controller;
  1406. /* notify the user. */
  1407. isci_port_bc_change_received(ihost, iport, iphy);
  1408. }
  1409. int isci_port_perform_hard_reset(struct isci_host *ihost, struct isci_port *iport,
  1410. struct isci_phy *iphy)
  1411. {
  1412. unsigned long flags;
  1413. enum sci_status status;
  1414. int idx, ret = TMF_RESP_FUNC_COMPLETE;
  1415. dev_dbg(&ihost->pdev->dev, "%s: iport = %p\n",
  1416. __func__, iport);
  1417. init_completion(&iport->hard_reset_complete);
  1418. spin_lock_irqsave(&ihost->scic_lock, flags);
  1419. #define ISCI_PORT_RESET_TIMEOUT SCIC_SDS_SIGNATURE_FIS_TIMEOUT
  1420. status = sci_port_hard_reset(iport, ISCI_PORT_RESET_TIMEOUT);
  1421. spin_unlock_irqrestore(&ihost->scic_lock, flags);
  1422. if (status == SCI_SUCCESS) {
  1423. wait_for_completion(&iport->hard_reset_complete);
  1424. dev_dbg(&ihost->pdev->dev,
  1425. "%s: iport = %p; hard reset completion\n",
  1426. __func__, iport);
  1427. if (iport->hard_reset_status != SCI_SUCCESS)
  1428. ret = TMF_RESP_FUNC_FAILED;
  1429. } else {
  1430. ret = TMF_RESP_FUNC_FAILED;
  1431. dev_err(&ihost->pdev->dev,
  1432. "%s: iport = %p; sci_port_hard_reset call"
  1433. " failed 0x%x\n",
  1434. __func__, iport, status);
  1435. }
  1436. /* If the hard reset for the port has failed, consider this
  1437. * the same as link failures on all phys in the port.
  1438. */
  1439. if (ret != TMF_RESP_FUNC_COMPLETE) {
  1440. dev_err(&ihost->pdev->dev,
  1441. "%s: iport = %p; hard reset failed "
  1442. "(0x%x) - driving explicit link fail for all phys\n",
  1443. __func__, iport, iport->hard_reset_status);
  1444. /* Down all phys in the port. */
  1445. spin_lock_irqsave(&ihost->scic_lock, flags);
  1446. for (idx = 0; idx < SCI_MAX_PHYS; ++idx) {
  1447. struct isci_phy *iphy = iport->phy_table[idx];
  1448. if (!iphy)
  1449. continue;
  1450. sci_phy_stop(iphy);
  1451. sci_phy_start(iphy);
  1452. }
  1453. spin_unlock_irqrestore(&ihost->scic_lock, flags);
  1454. }
  1455. return ret;
  1456. }
  1457. /**
  1458. * isci_port_deformed() - This function is called by libsas when a port becomes
  1459. * inactive.
  1460. * @phy: This parameter specifies the libsas phy with the inactive port.
  1461. *
  1462. */
  1463. void isci_port_deformed(struct asd_sas_phy *phy)
  1464. {
  1465. pr_debug("%s: sas_phy = %p\n", __func__, phy);
  1466. }
  1467. /**
  1468. * isci_port_formed() - This function is called by libsas when a port becomes
  1469. * active.
  1470. * @phy: This parameter specifies the libsas phy with the active port.
  1471. *
  1472. */
  1473. void isci_port_formed(struct asd_sas_phy *phy)
  1474. {
  1475. pr_debug("%s: sas_phy = %p, sas_port = %p\n", __func__, phy, phy->port);
  1476. }