phy.c 41 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312
  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 "host.h"
  57. #include "phy.h"
  58. #include "scu_event_codes.h"
  59. #include "probe_roms.h"
  60. /* Maximum arbitration wait time in micro-seconds */
  61. #define SCIC_SDS_PHY_MAX_ARBITRATION_WAIT_TIME (700)
  62. enum sas_linkrate sci_phy_linkrate(struct isci_phy *iphy)
  63. {
  64. return iphy->max_negotiated_speed;
  65. }
  66. static enum sci_status
  67. sci_phy_transport_layer_initialization(struct isci_phy *iphy,
  68. struct scu_transport_layer_registers __iomem *reg)
  69. {
  70. u32 tl_control;
  71. iphy->transport_layer_registers = reg;
  72. writel(SCIC_SDS_REMOTE_NODE_CONTEXT_INVALID_INDEX,
  73. &iphy->transport_layer_registers->stp_rni);
  74. /*
  75. * Hardware team recommends that we enable the STP prefetch for all
  76. * transports
  77. */
  78. tl_control = readl(&iphy->transport_layer_registers->control);
  79. tl_control |= SCU_TLCR_GEN_BIT(STP_WRITE_DATA_PREFETCH);
  80. writel(tl_control, &iphy->transport_layer_registers->control);
  81. return SCI_SUCCESS;
  82. }
  83. static enum sci_status
  84. sci_phy_link_layer_initialization(struct isci_phy *iphy,
  85. struct scu_link_layer_registers __iomem *reg)
  86. {
  87. struct isci_host *ihost = iphy->owning_port->owning_controller;
  88. int phy_idx = iphy->phy_index;
  89. struct sci_phy_user_params *phy_user = &ihost->user_parameters.phys[phy_idx];
  90. struct sci_phy_oem_params *phy_oem =
  91. &ihost->oem_parameters.phys[phy_idx];
  92. u32 phy_configuration;
  93. struct sci_phy_cap phy_cap;
  94. u32 parity_check = 0;
  95. u32 parity_count = 0;
  96. u32 llctl, link_rate;
  97. u32 clksm_value = 0;
  98. iphy->link_layer_registers = reg;
  99. /* Set our IDENTIFY frame data */
  100. #define SCI_END_DEVICE 0x01
  101. writel(SCU_SAS_TIID_GEN_BIT(SMP_INITIATOR) |
  102. SCU_SAS_TIID_GEN_BIT(SSP_INITIATOR) |
  103. SCU_SAS_TIID_GEN_BIT(STP_INITIATOR) |
  104. SCU_SAS_TIID_GEN_BIT(DA_SATA_HOST) |
  105. SCU_SAS_TIID_GEN_VAL(DEVICE_TYPE, SCI_END_DEVICE),
  106. &iphy->link_layer_registers->transmit_identification);
  107. /* Write the device SAS Address */
  108. writel(0xFEDCBA98,
  109. &iphy->link_layer_registers->sas_device_name_high);
  110. writel(phy_idx, &iphy->link_layer_registers->sas_device_name_low);
  111. /* Write the source SAS Address */
  112. writel(phy_oem->sas_address.high,
  113. &iphy->link_layer_registers->source_sas_address_high);
  114. writel(phy_oem->sas_address.low,
  115. &iphy->link_layer_registers->source_sas_address_low);
  116. /* Clear and Set the PHY Identifier */
  117. writel(0, &iphy->link_layer_registers->identify_frame_phy_id);
  118. writel(SCU_SAS_TIPID_GEN_VALUE(ID, phy_idx),
  119. &iphy->link_layer_registers->identify_frame_phy_id);
  120. /* Change the initial state of the phy configuration register */
  121. phy_configuration =
  122. readl(&iphy->link_layer_registers->phy_configuration);
  123. /* Hold OOB state machine in reset */
  124. phy_configuration |= SCU_SAS_PCFG_GEN_BIT(OOB_RESET);
  125. writel(phy_configuration,
  126. &iphy->link_layer_registers->phy_configuration);
  127. /* Configure the SNW capabilities */
  128. phy_cap.all = 0;
  129. phy_cap.start = 1;
  130. phy_cap.gen3_no_ssc = 1;
  131. phy_cap.gen2_no_ssc = 1;
  132. phy_cap.gen1_no_ssc = 1;
  133. if (ihost->oem_parameters.controller.do_enable_ssc == true) {
  134. phy_cap.gen3_ssc = 1;
  135. phy_cap.gen2_ssc = 1;
  136. phy_cap.gen1_ssc = 1;
  137. }
  138. /*
  139. * The SAS specification indicates that the phy_capabilities that
  140. * are transmitted shall have an even parity. Calculate the parity. */
  141. parity_check = phy_cap.all;
  142. while (parity_check != 0) {
  143. if (parity_check & 0x1)
  144. parity_count++;
  145. parity_check >>= 1;
  146. }
  147. /*
  148. * If parity indicates there are an odd number of bits set, then
  149. * set the parity bit to 1 in the phy capabilities. */
  150. if ((parity_count % 2) != 0)
  151. phy_cap.parity = 1;
  152. writel(phy_cap.all, &iphy->link_layer_registers->phy_capabilities);
  153. /* Set the enable spinup period but disable the ability to send
  154. * notify enable spinup
  155. */
  156. writel(SCU_ENSPINUP_GEN_VAL(COUNT,
  157. phy_user->notify_enable_spin_up_insertion_frequency),
  158. &iphy->link_layer_registers->notify_enable_spinup_control);
  159. /* Write the ALIGN Insertion Ferequency for connected phy and
  160. * inpendent of connected state
  161. */
  162. clksm_value = SCU_ALIGN_INSERTION_FREQUENCY_GEN_VAL(CONNECTED,
  163. phy_user->in_connection_align_insertion_frequency);
  164. clksm_value |= SCU_ALIGN_INSERTION_FREQUENCY_GEN_VAL(GENERAL,
  165. phy_user->align_insertion_frequency);
  166. writel(clksm_value, &iphy->link_layer_registers->clock_skew_management);
  167. /* @todo Provide a way to write this register correctly */
  168. writel(0x02108421,
  169. &iphy->link_layer_registers->afe_lookup_table_control);
  170. llctl = SCU_SAS_LLCTL_GEN_VAL(NO_OUTBOUND_TASK_TIMEOUT,
  171. (u8)ihost->user_parameters.no_outbound_task_timeout);
  172. switch (phy_user->max_speed_generation) {
  173. case SCIC_SDS_PARM_GEN3_SPEED:
  174. link_rate = SCU_SAS_LINK_LAYER_CONTROL_MAX_LINK_RATE_GEN3;
  175. break;
  176. case SCIC_SDS_PARM_GEN2_SPEED:
  177. link_rate = SCU_SAS_LINK_LAYER_CONTROL_MAX_LINK_RATE_GEN2;
  178. break;
  179. default:
  180. link_rate = SCU_SAS_LINK_LAYER_CONTROL_MAX_LINK_RATE_GEN1;
  181. break;
  182. }
  183. llctl |= SCU_SAS_LLCTL_GEN_VAL(MAX_LINK_RATE, link_rate);
  184. writel(llctl, &iphy->link_layer_registers->link_layer_control);
  185. if (is_a2(ihost->pdev)) {
  186. /* Program the max ARB time for the PHY to 700us so we inter-operate with
  187. * the PMC expander which shuts down PHYs if the expander PHY generates too
  188. * many breaks. This time value will guarantee that the initiator PHY will
  189. * generate the break.
  190. */
  191. writel(SCIC_SDS_PHY_MAX_ARBITRATION_WAIT_TIME,
  192. &iphy->link_layer_registers->maximum_arbitration_wait_timer_timeout);
  193. }
  194. /* Disable link layer hang detection, rely on the OS timeout for I/O timeouts. */
  195. writel(0, &iphy->link_layer_registers->link_layer_hang_detection_timeout);
  196. /* We can exit the initial state to the stopped state */
  197. sci_change_state(&iphy->sm, SCI_PHY_STOPPED);
  198. return SCI_SUCCESS;
  199. }
  200. static void phy_sata_timeout(unsigned long data)
  201. {
  202. struct sci_timer *tmr = (struct sci_timer *)data;
  203. struct isci_phy *iphy = container_of(tmr, typeof(*iphy), sata_timer);
  204. struct isci_host *ihost = iphy->owning_port->owning_controller;
  205. unsigned long flags;
  206. spin_lock_irqsave(&ihost->scic_lock, flags);
  207. if (tmr->cancel)
  208. goto done;
  209. dev_dbg(sciphy_to_dev(iphy),
  210. "%s: SCIC SDS Phy 0x%p did not receive signature fis before "
  211. "timeout.\n",
  212. __func__,
  213. iphy);
  214. sci_change_state(&iphy->sm, SCI_PHY_STARTING);
  215. done:
  216. spin_unlock_irqrestore(&ihost->scic_lock, flags);
  217. }
  218. /**
  219. * This method returns the port currently containing this phy. If the phy is
  220. * currently contained by the dummy port, then the phy is considered to not
  221. * be part of a port.
  222. * @sci_phy: This parameter specifies the phy for which to retrieve the
  223. * containing port.
  224. *
  225. * This method returns a handle to a port that contains the supplied phy.
  226. * NULL This value is returned if the phy is not part of a real
  227. * port (i.e. it's contained in the dummy port). !NULL All other
  228. * values indicate a handle/pointer to the port containing the phy.
  229. */
  230. struct isci_port *phy_get_non_dummy_port(struct isci_phy *iphy)
  231. {
  232. struct isci_port *iport = iphy->owning_port;
  233. if (iport->physical_port_index == SCIC_SDS_DUMMY_PORT)
  234. return NULL;
  235. return iphy->owning_port;
  236. }
  237. /**
  238. * This method will assign a port to the phy object.
  239. * @out]: iphy This parameter specifies the phy for which to assign a port
  240. * object.
  241. *
  242. *
  243. */
  244. void sci_phy_set_port(
  245. struct isci_phy *iphy,
  246. struct isci_port *iport)
  247. {
  248. iphy->owning_port = iport;
  249. if (iphy->bcn_received_while_port_unassigned) {
  250. iphy->bcn_received_while_port_unassigned = false;
  251. sci_port_broadcast_change_received(iphy->owning_port, iphy);
  252. }
  253. }
  254. enum sci_status sci_phy_initialize(struct isci_phy *iphy,
  255. struct scu_transport_layer_registers __iomem *tl,
  256. struct scu_link_layer_registers __iomem *ll)
  257. {
  258. /* Perfrom the initialization of the TL hardware */
  259. sci_phy_transport_layer_initialization(iphy, tl);
  260. /* Perofrm the initialization of the PE hardware */
  261. sci_phy_link_layer_initialization(iphy, ll);
  262. /* There is nothing that needs to be done in this state just
  263. * transition to the stopped state
  264. */
  265. sci_change_state(&iphy->sm, SCI_PHY_STOPPED);
  266. return SCI_SUCCESS;
  267. }
  268. /**
  269. * This method assigns the direct attached device ID for this phy.
  270. *
  271. * @iphy The phy for which the direct attached device id is to
  272. * be assigned.
  273. * @device_id The direct attached device ID to assign to the phy.
  274. * This will either be the RNi for the device or an invalid RNi if there
  275. * is no current device assigned to the phy.
  276. */
  277. void sci_phy_setup_transport(struct isci_phy *iphy, u32 device_id)
  278. {
  279. u32 tl_control;
  280. writel(device_id, &iphy->transport_layer_registers->stp_rni);
  281. /*
  282. * The read should guarantee that the first write gets posted
  283. * before the next write
  284. */
  285. tl_control = readl(&iphy->transport_layer_registers->control);
  286. tl_control |= SCU_TLCR_GEN_BIT(CLEAR_TCI_NCQ_MAPPING_TABLE);
  287. writel(tl_control, &iphy->transport_layer_registers->control);
  288. }
  289. static void sci_phy_suspend(struct isci_phy *iphy)
  290. {
  291. u32 scu_sas_pcfg_value;
  292. scu_sas_pcfg_value =
  293. readl(&iphy->link_layer_registers->phy_configuration);
  294. scu_sas_pcfg_value |= SCU_SAS_PCFG_GEN_BIT(SUSPEND_PROTOCOL_ENGINE);
  295. writel(scu_sas_pcfg_value,
  296. &iphy->link_layer_registers->phy_configuration);
  297. sci_phy_setup_transport(iphy, SCIC_SDS_REMOTE_NODE_CONTEXT_INVALID_INDEX);
  298. }
  299. void sci_phy_resume(struct isci_phy *iphy)
  300. {
  301. u32 scu_sas_pcfg_value;
  302. scu_sas_pcfg_value =
  303. readl(&iphy->link_layer_registers->phy_configuration);
  304. scu_sas_pcfg_value &= ~SCU_SAS_PCFG_GEN_BIT(SUSPEND_PROTOCOL_ENGINE);
  305. writel(scu_sas_pcfg_value,
  306. &iphy->link_layer_registers->phy_configuration);
  307. }
  308. void sci_phy_get_sas_address(struct isci_phy *iphy, struct sci_sas_address *sas)
  309. {
  310. sas->high = readl(&iphy->link_layer_registers->source_sas_address_high);
  311. sas->low = readl(&iphy->link_layer_registers->source_sas_address_low);
  312. }
  313. void sci_phy_get_attached_sas_address(struct isci_phy *iphy, struct sci_sas_address *sas)
  314. {
  315. struct sas_identify_frame *iaf;
  316. iaf = &iphy->frame_rcvd.iaf;
  317. memcpy(sas, iaf->sas_addr, SAS_ADDR_SIZE);
  318. }
  319. void sci_phy_get_protocols(struct isci_phy *iphy, struct sci_phy_proto *proto)
  320. {
  321. proto->all = readl(&iphy->link_layer_registers->transmit_identification);
  322. }
  323. enum sci_status sci_phy_start(struct isci_phy *iphy)
  324. {
  325. enum sci_phy_states state = iphy->sm.current_state_id;
  326. if (state != SCI_PHY_STOPPED) {
  327. dev_dbg(sciphy_to_dev(iphy),
  328. "%s: in wrong state: %d\n", __func__, state);
  329. return SCI_FAILURE_INVALID_STATE;
  330. }
  331. sci_change_state(&iphy->sm, SCI_PHY_STARTING);
  332. return SCI_SUCCESS;
  333. }
  334. enum sci_status sci_phy_stop(struct isci_phy *iphy)
  335. {
  336. enum sci_phy_states state = iphy->sm.current_state_id;
  337. switch (state) {
  338. case SCI_PHY_SUB_INITIAL:
  339. case SCI_PHY_SUB_AWAIT_OSSP_EN:
  340. case SCI_PHY_SUB_AWAIT_SAS_SPEED_EN:
  341. case SCI_PHY_SUB_AWAIT_SAS_POWER:
  342. case SCI_PHY_SUB_AWAIT_SATA_POWER:
  343. case SCI_PHY_SUB_AWAIT_SATA_PHY_EN:
  344. case SCI_PHY_SUB_AWAIT_SATA_SPEED_EN:
  345. case SCI_PHY_SUB_AWAIT_SIG_FIS_UF:
  346. case SCI_PHY_SUB_FINAL:
  347. case SCI_PHY_READY:
  348. break;
  349. default:
  350. dev_dbg(sciphy_to_dev(iphy),
  351. "%s: in wrong state: %d\n", __func__, state);
  352. return SCI_FAILURE_INVALID_STATE;
  353. }
  354. sci_change_state(&iphy->sm, SCI_PHY_STOPPED);
  355. return SCI_SUCCESS;
  356. }
  357. enum sci_status sci_phy_reset(struct isci_phy *iphy)
  358. {
  359. enum sci_phy_states state = iphy->sm.current_state_id;
  360. if (state != SCI_PHY_READY) {
  361. dev_dbg(sciphy_to_dev(iphy),
  362. "%s: in wrong state: %d\n", __func__, state);
  363. return SCI_FAILURE_INVALID_STATE;
  364. }
  365. sci_change_state(&iphy->sm, SCI_PHY_RESETTING);
  366. return SCI_SUCCESS;
  367. }
  368. enum sci_status sci_phy_consume_power_handler(struct isci_phy *iphy)
  369. {
  370. enum sci_phy_states state = iphy->sm.current_state_id;
  371. switch (state) {
  372. case SCI_PHY_SUB_AWAIT_SAS_POWER: {
  373. u32 enable_spinup;
  374. enable_spinup = readl(&iphy->link_layer_registers->notify_enable_spinup_control);
  375. enable_spinup |= SCU_ENSPINUP_GEN_BIT(ENABLE);
  376. writel(enable_spinup, &iphy->link_layer_registers->notify_enable_spinup_control);
  377. /* Change state to the final state this substate machine has run to completion */
  378. sci_change_state(&iphy->sm, SCI_PHY_SUB_FINAL);
  379. return SCI_SUCCESS;
  380. }
  381. case SCI_PHY_SUB_AWAIT_SATA_POWER: {
  382. u32 scu_sas_pcfg_value;
  383. /* Release the spinup hold state and reset the OOB state machine */
  384. scu_sas_pcfg_value =
  385. readl(&iphy->link_layer_registers->phy_configuration);
  386. scu_sas_pcfg_value &=
  387. ~(SCU_SAS_PCFG_GEN_BIT(SATA_SPINUP_HOLD) | SCU_SAS_PCFG_GEN_BIT(OOB_ENABLE));
  388. scu_sas_pcfg_value |= SCU_SAS_PCFG_GEN_BIT(OOB_RESET);
  389. writel(scu_sas_pcfg_value,
  390. &iphy->link_layer_registers->phy_configuration);
  391. /* Now restart the OOB operation */
  392. scu_sas_pcfg_value &= ~SCU_SAS_PCFG_GEN_BIT(OOB_RESET);
  393. scu_sas_pcfg_value |= SCU_SAS_PCFG_GEN_BIT(OOB_ENABLE);
  394. writel(scu_sas_pcfg_value,
  395. &iphy->link_layer_registers->phy_configuration);
  396. /* Change state to the final state this substate machine has run to completion */
  397. sci_change_state(&iphy->sm, SCI_PHY_SUB_AWAIT_SATA_PHY_EN);
  398. return SCI_SUCCESS;
  399. }
  400. default:
  401. dev_dbg(sciphy_to_dev(iphy),
  402. "%s: in wrong state: %d\n", __func__, state);
  403. return SCI_FAILURE_INVALID_STATE;
  404. }
  405. }
  406. static void sci_phy_start_sas_link_training(struct isci_phy *iphy)
  407. {
  408. /* continue the link training for the phy as if it were a SAS PHY
  409. * instead of a SATA PHY. This is done because the completion queue had a SAS
  410. * PHY DETECTED event when the state machine was expecting a SATA PHY event.
  411. */
  412. u32 phy_control;
  413. phy_control = readl(&iphy->link_layer_registers->phy_configuration);
  414. phy_control |= SCU_SAS_PCFG_GEN_BIT(SATA_SPINUP_HOLD);
  415. writel(phy_control,
  416. &iphy->link_layer_registers->phy_configuration);
  417. sci_change_state(&iphy->sm, SCI_PHY_SUB_AWAIT_SAS_SPEED_EN);
  418. iphy->protocol = SCIC_SDS_PHY_PROTOCOL_SAS;
  419. }
  420. static void sci_phy_start_sata_link_training(struct isci_phy *iphy)
  421. {
  422. /* This method continues the link training for the phy as if it were a SATA PHY
  423. * instead of a SAS PHY. This is done because the completion queue had a SATA
  424. * SPINUP HOLD event when the state machine was expecting a SAS PHY event. none
  425. */
  426. sci_change_state(&iphy->sm, SCI_PHY_SUB_AWAIT_SATA_POWER);
  427. iphy->protocol = SCIC_SDS_PHY_PROTOCOL_SATA;
  428. }
  429. /**
  430. * sci_phy_complete_link_training - perform processing common to
  431. * all protocols upon completion of link training.
  432. * @sci_phy: This parameter specifies the phy object for which link training
  433. * has completed.
  434. * @max_link_rate: This parameter specifies the maximum link rate to be
  435. * associated with this phy.
  436. * @next_state: This parameter specifies the next state for the phy's starting
  437. * sub-state machine.
  438. *
  439. */
  440. static void sci_phy_complete_link_training(struct isci_phy *iphy,
  441. enum sas_linkrate max_link_rate,
  442. u32 next_state)
  443. {
  444. iphy->max_negotiated_speed = max_link_rate;
  445. sci_change_state(&iphy->sm, next_state);
  446. }
  447. enum sci_status sci_phy_event_handler(struct isci_phy *iphy, u32 event_code)
  448. {
  449. enum sci_phy_states state = iphy->sm.current_state_id;
  450. switch (state) {
  451. case SCI_PHY_SUB_AWAIT_OSSP_EN:
  452. switch (scu_get_event_code(event_code)) {
  453. case SCU_EVENT_SAS_PHY_DETECTED:
  454. sci_phy_start_sas_link_training(iphy);
  455. iphy->is_in_link_training = true;
  456. break;
  457. case SCU_EVENT_SATA_SPINUP_HOLD:
  458. sci_phy_start_sata_link_training(iphy);
  459. iphy->is_in_link_training = true;
  460. break;
  461. default:
  462. dev_dbg(sciphy_to_dev(iphy),
  463. "%s: PHY starting substate machine received "
  464. "unexpected event_code %x\n",
  465. __func__,
  466. event_code);
  467. return SCI_FAILURE;
  468. }
  469. return SCI_SUCCESS;
  470. case SCI_PHY_SUB_AWAIT_SAS_SPEED_EN:
  471. switch (scu_get_event_code(event_code)) {
  472. case SCU_EVENT_SAS_PHY_DETECTED:
  473. /*
  474. * Why is this being reported again by the controller?
  475. * We would re-enter this state so just stay here */
  476. break;
  477. case SCU_EVENT_SAS_15:
  478. case SCU_EVENT_SAS_15_SSC:
  479. sci_phy_complete_link_training(iphy, SAS_LINK_RATE_1_5_GBPS,
  480. SCI_PHY_SUB_AWAIT_IAF_UF);
  481. break;
  482. case SCU_EVENT_SAS_30:
  483. case SCU_EVENT_SAS_30_SSC:
  484. sci_phy_complete_link_training(iphy, SAS_LINK_RATE_3_0_GBPS,
  485. SCI_PHY_SUB_AWAIT_IAF_UF);
  486. break;
  487. case SCU_EVENT_SAS_60:
  488. case SCU_EVENT_SAS_60_SSC:
  489. sci_phy_complete_link_training(iphy, SAS_LINK_RATE_6_0_GBPS,
  490. SCI_PHY_SUB_AWAIT_IAF_UF);
  491. break;
  492. case SCU_EVENT_SATA_SPINUP_HOLD:
  493. /*
  494. * We were doing SAS PHY link training and received a SATA PHY event
  495. * continue OOB/SN as if this were a SATA PHY */
  496. sci_phy_start_sata_link_training(iphy);
  497. break;
  498. case SCU_EVENT_LINK_FAILURE:
  499. /* Link failure change state back to the starting state */
  500. sci_change_state(&iphy->sm, SCI_PHY_STARTING);
  501. break;
  502. default:
  503. dev_warn(sciphy_to_dev(iphy),
  504. "%s: PHY starting substate machine received "
  505. "unexpected event_code %x\n",
  506. __func__, event_code);
  507. return SCI_FAILURE;
  508. break;
  509. }
  510. return SCI_SUCCESS;
  511. case SCI_PHY_SUB_AWAIT_IAF_UF:
  512. switch (scu_get_event_code(event_code)) {
  513. case SCU_EVENT_SAS_PHY_DETECTED:
  514. /* Backup the state machine */
  515. sci_phy_start_sas_link_training(iphy);
  516. break;
  517. case SCU_EVENT_SATA_SPINUP_HOLD:
  518. /* We were doing SAS PHY link training and received a
  519. * SATA PHY event continue OOB/SN as if this were a
  520. * SATA PHY
  521. */
  522. sci_phy_start_sata_link_training(iphy);
  523. break;
  524. case SCU_EVENT_RECEIVED_IDENTIFY_TIMEOUT:
  525. case SCU_EVENT_LINK_FAILURE:
  526. case SCU_EVENT_HARD_RESET_RECEIVED:
  527. /* Start the oob/sn state machine over again */
  528. sci_change_state(&iphy->sm, SCI_PHY_STARTING);
  529. break;
  530. default:
  531. dev_warn(sciphy_to_dev(iphy),
  532. "%s: PHY starting substate machine received "
  533. "unexpected event_code %x\n",
  534. __func__, event_code);
  535. return SCI_FAILURE;
  536. }
  537. return SCI_SUCCESS;
  538. case SCI_PHY_SUB_AWAIT_SAS_POWER:
  539. switch (scu_get_event_code(event_code)) {
  540. case SCU_EVENT_LINK_FAILURE:
  541. /* Link failure change state back to the starting state */
  542. sci_change_state(&iphy->sm, SCI_PHY_STARTING);
  543. break;
  544. default:
  545. dev_warn(sciphy_to_dev(iphy),
  546. "%s: PHY starting substate machine received unexpected "
  547. "event_code %x\n",
  548. __func__,
  549. event_code);
  550. return SCI_FAILURE;
  551. }
  552. return SCI_SUCCESS;
  553. case SCI_PHY_SUB_AWAIT_SATA_POWER:
  554. switch (scu_get_event_code(event_code)) {
  555. case SCU_EVENT_LINK_FAILURE:
  556. /* Link failure change state back to the starting state */
  557. sci_change_state(&iphy->sm, SCI_PHY_STARTING);
  558. break;
  559. case SCU_EVENT_SATA_SPINUP_HOLD:
  560. /* These events are received every 10ms and are
  561. * expected while in this state
  562. */
  563. break;
  564. case SCU_EVENT_SAS_PHY_DETECTED:
  565. /* There has been a change in the phy type before OOB/SN for the
  566. * SATA finished start down the SAS link traning path.
  567. */
  568. sci_phy_start_sas_link_training(iphy);
  569. break;
  570. default:
  571. dev_warn(sciphy_to_dev(iphy),
  572. "%s: PHY starting substate machine received "
  573. "unexpected event_code %x\n",
  574. __func__, event_code);
  575. return SCI_FAILURE;
  576. }
  577. return SCI_SUCCESS;
  578. case SCI_PHY_SUB_AWAIT_SATA_PHY_EN:
  579. switch (scu_get_event_code(event_code)) {
  580. case SCU_EVENT_LINK_FAILURE:
  581. /* Link failure change state back to the starting state */
  582. sci_change_state(&iphy->sm, SCI_PHY_STARTING);
  583. break;
  584. case SCU_EVENT_SATA_SPINUP_HOLD:
  585. /* These events might be received since we dont know how many may be in
  586. * the completion queue while waiting for power
  587. */
  588. break;
  589. case SCU_EVENT_SATA_PHY_DETECTED:
  590. iphy->protocol = SCIC_SDS_PHY_PROTOCOL_SATA;
  591. /* We have received the SATA PHY notification change state */
  592. sci_change_state(&iphy->sm, SCI_PHY_SUB_AWAIT_SATA_SPEED_EN);
  593. break;
  594. case SCU_EVENT_SAS_PHY_DETECTED:
  595. /* There has been a change in the phy type before OOB/SN for the
  596. * SATA finished start down the SAS link traning path.
  597. */
  598. sci_phy_start_sas_link_training(iphy);
  599. break;
  600. default:
  601. dev_warn(sciphy_to_dev(iphy),
  602. "%s: PHY starting substate machine received "
  603. "unexpected event_code %x\n",
  604. __func__,
  605. event_code);
  606. return SCI_FAILURE;;
  607. }
  608. return SCI_SUCCESS;
  609. case SCI_PHY_SUB_AWAIT_SATA_SPEED_EN:
  610. switch (scu_get_event_code(event_code)) {
  611. case SCU_EVENT_SATA_PHY_DETECTED:
  612. /*
  613. * The hardware reports multiple SATA PHY detected events
  614. * ignore the extras */
  615. break;
  616. case SCU_EVENT_SATA_15:
  617. case SCU_EVENT_SATA_15_SSC:
  618. sci_phy_complete_link_training(iphy, SAS_LINK_RATE_1_5_GBPS,
  619. SCI_PHY_SUB_AWAIT_SIG_FIS_UF);
  620. break;
  621. case SCU_EVENT_SATA_30:
  622. case SCU_EVENT_SATA_30_SSC:
  623. sci_phy_complete_link_training(iphy, SAS_LINK_RATE_3_0_GBPS,
  624. SCI_PHY_SUB_AWAIT_SIG_FIS_UF);
  625. break;
  626. case SCU_EVENT_SATA_60:
  627. case SCU_EVENT_SATA_60_SSC:
  628. sci_phy_complete_link_training(iphy, SAS_LINK_RATE_6_0_GBPS,
  629. SCI_PHY_SUB_AWAIT_SIG_FIS_UF);
  630. break;
  631. case SCU_EVENT_LINK_FAILURE:
  632. /* Link failure change state back to the starting state */
  633. sci_change_state(&iphy->sm, SCI_PHY_STARTING);
  634. break;
  635. case SCU_EVENT_SAS_PHY_DETECTED:
  636. /*
  637. * There has been a change in the phy type before OOB/SN for the
  638. * SATA finished start down the SAS link traning path. */
  639. sci_phy_start_sas_link_training(iphy);
  640. break;
  641. default:
  642. dev_warn(sciphy_to_dev(iphy),
  643. "%s: PHY starting substate machine received "
  644. "unexpected event_code %x\n",
  645. __func__, event_code);
  646. return SCI_FAILURE;
  647. }
  648. return SCI_SUCCESS;
  649. case SCI_PHY_SUB_AWAIT_SIG_FIS_UF:
  650. switch (scu_get_event_code(event_code)) {
  651. case SCU_EVENT_SATA_PHY_DETECTED:
  652. /* Backup the state machine */
  653. sci_change_state(&iphy->sm, SCI_PHY_SUB_AWAIT_SATA_SPEED_EN);
  654. break;
  655. case SCU_EVENT_LINK_FAILURE:
  656. /* Link failure change state back to the starting state */
  657. sci_change_state(&iphy->sm, SCI_PHY_STARTING);
  658. break;
  659. default:
  660. dev_warn(sciphy_to_dev(iphy),
  661. "%s: PHY starting substate machine received "
  662. "unexpected event_code %x\n",
  663. __func__,
  664. event_code);
  665. return SCI_FAILURE;
  666. }
  667. return SCI_SUCCESS;
  668. case SCI_PHY_READY:
  669. switch (scu_get_event_code(event_code)) {
  670. case SCU_EVENT_LINK_FAILURE:
  671. /* Link failure change state back to the starting state */
  672. sci_change_state(&iphy->sm, SCI_PHY_STARTING);
  673. break;
  674. case SCU_EVENT_BROADCAST_CHANGE:
  675. /* Broadcast change received. Notify the port. */
  676. if (phy_get_non_dummy_port(iphy) != NULL)
  677. sci_port_broadcast_change_received(iphy->owning_port, iphy);
  678. else
  679. iphy->bcn_received_while_port_unassigned = true;
  680. break;
  681. default:
  682. dev_warn(sciphy_to_dev(iphy),
  683. "%sP SCIC PHY 0x%p ready state machine received "
  684. "unexpected event_code %x\n",
  685. __func__, iphy, event_code);
  686. return SCI_FAILURE_INVALID_STATE;
  687. }
  688. return SCI_SUCCESS;
  689. case SCI_PHY_RESETTING:
  690. switch (scu_get_event_code(event_code)) {
  691. case SCU_EVENT_HARD_RESET_TRANSMITTED:
  692. /* Link failure change state back to the starting state */
  693. sci_change_state(&iphy->sm, SCI_PHY_STARTING);
  694. break;
  695. default:
  696. dev_warn(sciphy_to_dev(iphy),
  697. "%s: SCIC PHY 0x%p resetting state machine received "
  698. "unexpected event_code %x\n",
  699. __func__, iphy, event_code);
  700. return SCI_FAILURE_INVALID_STATE;
  701. break;
  702. }
  703. return SCI_SUCCESS;
  704. default:
  705. dev_dbg(sciphy_to_dev(iphy),
  706. "%s: in wrong state: %d\n", __func__, state);
  707. return SCI_FAILURE_INVALID_STATE;
  708. }
  709. }
  710. enum sci_status sci_phy_frame_handler(struct isci_phy *iphy, u32 frame_index)
  711. {
  712. enum sci_phy_states state = iphy->sm.current_state_id;
  713. struct isci_host *ihost = iphy->owning_port->owning_controller;
  714. enum sci_status result;
  715. unsigned long flags;
  716. switch (state) {
  717. case SCI_PHY_SUB_AWAIT_IAF_UF: {
  718. u32 *frame_words;
  719. struct sas_identify_frame iaf;
  720. result = sci_unsolicited_frame_control_get_header(&ihost->uf_control,
  721. frame_index,
  722. (void **)&frame_words);
  723. if (result != SCI_SUCCESS)
  724. return result;
  725. sci_swab32_cpy(&iaf, frame_words, sizeof(iaf) / sizeof(u32));
  726. if (iaf.frame_type == 0) {
  727. u32 state;
  728. spin_lock_irqsave(&iphy->sas_phy.frame_rcvd_lock, flags);
  729. memcpy(&iphy->frame_rcvd.iaf, &iaf, sizeof(iaf));
  730. spin_unlock_irqrestore(&iphy->sas_phy.frame_rcvd_lock, flags);
  731. if (iaf.smp_tport) {
  732. /* We got the IAF for an expander PHY go to the final
  733. * state since there are no power requirements for
  734. * expander phys.
  735. */
  736. state = SCI_PHY_SUB_FINAL;
  737. } else {
  738. /* We got the IAF we can now go to the await spinup
  739. * semaphore state
  740. */
  741. state = SCI_PHY_SUB_AWAIT_SAS_POWER;
  742. }
  743. sci_change_state(&iphy->sm, state);
  744. result = SCI_SUCCESS;
  745. } else
  746. dev_warn(sciphy_to_dev(iphy),
  747. "%s: PHY starting substate machine received "
  748. "unexpected frame id %x\n",
  749. __func__, frame_index);
  750. sci_controller_release_frame(ihost, frame_index);
  751. return result;
  752. }
  753. case SCI_PHY_SUB_AWAIT_SIG_FIS_UF: {
  754. struct dev_to_host_fis *frame_header;
  755. u32 *fis_frame_data;
  756. result = sci_unsolicited_frame_control_get_header(&ihost->uf_control,
  757. frame_index,
  758. (void **)&frame_header);
  759. if (result != SCI_SUCCESS)
  760. return result;
  761. if ((frame_header->fis_type == FIS_REGD2H) &&
  762. !(frame_header->status & ATA_BUSY)) {
  763. sci_unsolicited_frame_control_get_buffer(&ihost->uf_control,
  764. frame_index,
  765. (void **)&fis_frame_data);
  766. spin_lock_irqsave(&iphy->sas_phy.frame_rcvd_lock, flags);
  767. sci_controller_copy_sata_response(&iphy->frame_rcvd.fis,
  768. frame_header,
  769. fis_frame_data);
  770. spin_unlock_irqrestore(&iphy->sas_phy.frame_rcvd_lock, flags);
  771. /* got IAF we can now go to the await spinup semaphore state */
  772. sci_change_state(&iphy->sm, SCI_PHY_SUB_FINAL);
  773. result = SCI_SUCCESS;
  774. } else
  775. dev_warn(sciphy_to_dev(iphy),
  776. "%s: PHY starting substate machine received "
  777. "unexpected frame id %x\n",
  778. __func__, frame_index);
  779. /* Regardless of the result we are done with this frame with it */
  780. sci_controller_release_frame(ihost, frame_index);
  781. return result;
  782. }
  783. default:
  784. dev_dbg(sciphy_to_dev(iphy),
  785. "%s: in wrong state: %d\n", __func__, state);
  786. return SCI_FAILURE_INVALID_STATE;
  787. }
  788. }
  789. static void sci_phy_starting_initial_substate_enter(struct sci_base_state_machine *sm)
  790. {
  791. struct isci_phy *iphy = container_of(sm, typeof(*iphy), sm);
  792. /* This is just an temporary state go off to the starting state */
  793. sci_change_state(&iphy->sm, SCI_PHY_SUB_AWAIT_OSSP_EN);
  794. }
  795. static void sci_phy_starting_await_sas_power_substate_enter(struct sci_base_state_machine *sm)
  796. {
  797. struct isci_phy *iphy = container_of(sm, typeof(*iphy), sm);
  798. struct isci_host *ihost = iphy->owning_port->owning_controller;
  799. sci_controller_power_control_queue_insert(ihost, iphy);
  800. }
  801. static void sci_phy_starting_await_sas_power_substate_exit(struct sci_base_state_machine *sm)
  802. {
  803. struct isci_phy *iphy = container_of(sm, typeof(*iphy), sm);
  804. struct isci_host *ihost = iphy->owning_port->owning_controller;
  805. sci_controller_power_control_queue_remove(ihost, iphy);
  806. }
  807. static void sci_phy_starting_await_sata_power_substate_enter(struct sci_base_state_machine *sm)
  808. {
  809. struct isci_phy *iphy = container_of(sm, typeof(*iphy), sm);
  810. struct isci_host *ihost = iphy->owning_port->owning_controller;
  811. sci_controller_power_control_queue_insert(ihost, iphy);
  812. }
  813. static void sci_phy_starting_await_sata_power_substate_exit(struct sci_base_state_machine *sm)
  814. {
  815. struct isci_phy *iphy = container_of(sm, typeof(*iphy), sm);
  816. struct isci_host *ihost = iphy->owning_port->owning_controller;
  817. sci_controller_power_control_queue_remove(ihost, iphy);
  818. }
  819. static void sci_phy_starting_await_sata_phy_substate_enter(struct sci_base_state_machine *sm)
  820. {
  821. struct isci_phy *iphy = container_of(sm, typeof(*iphy), sm);
  822. sci_mod_timer(&iphy->sata_timer, SCIC_SDS_SATA_LINK_TRAINING_TIMEOUT);
  823. }
  824. static void sci_phy_starting_await_sata_phy_substate_exit(struct sci_base_state_machine *sm)
  825. {
  826. struct isci_phy *iphy = container_of(sm, typeof(*iphy), sm);
  827. sci_del_timer(&iphy->sata_timer);
  828. }
  829. static void sci_phy_starting_await_sata_speed_substate_enter(struct sci_base_state_machine *sm)
  830. {
  831. struct isci_phy *iphy = container_of(sm, typeof(*iphy), sm);
  832. sci_mod_timer(&iphy->sata_timer, SCIC_SDS_SATA_LINK_TRAINING_TIMEOUT);
  833. }
  834. static void sci_phy_starting_await_sata_speed_substate_exit(struct sci_base_state_machine *sm)
  835. {
  836. struct isci_phy *iphy = container_of(sm, typeof(*iphy), sm);
  837. sci_del_timer(&iphy->sata_timer);
  838. }
  839. static void sci_phy_starting_await_sig_fis_uf_substate_enter(struct sci_base_state_machine *sm)
  840. {
  841. struct isci_phy *iphy = container_of(sm, typeof(*iphy), sm);
  842. if (sci_port_link_detected(iphy->owning_port, iphy)) {
  843. /*
  844. * Clear the PE suspend condition so we can actually
  845. * receive SIG FIS
  846. * The hardware will not respond to the XRDY until the PE
  847. * suspend condition is cleared.
  848. */
  849. sci_phy_resume(iphy);
  850. sci_mod_timer(&iphy->sata_timer,
  851. SCIC_SDS_SIGNATURE_FIS_TIMEOUT);
  852. } else
  853. iphy->is_in_link_training = false;
  854. }
  855. static void sci_phy_starting_await_sig_fis_uf_substate_exit(struct sci_base_state_machine *sm)
  856. {
  857. struct isci_phy *iphy = container_of(sm, typeof(*iphy), sm);
  858. sci_del_timer(&iphy->sata_timer);
  859. }
  860. static void sci_phy_starting_final_substate_enter(struct sci_base_state_machine *sm)
  861. {
  862. struct isci_phy *iphy = container_of(sm, typeof(*iphy), sm);
  863. /* State machine has run to completion so exit out and change
  864. * the base state machine to the ready state
  865. */
  866. sci_change_state(&iphy->sm, SCI_PHY_READY);
  867. }
  868. /**
  869. *
  870. * @sci_phy: This is the struct isci_phy object to stop.
  871. *
  872. * This method will stop the struct isci_phy object. This does not reset the
  873. * protocol engine it just suspends it and places it in a state where it will
  874. * not cause the end device to power up. none
  875. */
  876. static void scu_link_layer_stop_protocol_engine(
  877. struct isci_phy *iphy)
  878. {
  879. u32 scu_sas_pcfg_value;
  880. u32 enable_spinup_value;
  881. /* Suspend the protocol engine and place it in a sata spinup hold state */
  882. scu_sas_pcfg_value =
  883. readl(&iphy->link_layer_registers->phy_configuration);
  884. scu_sas_pcfg_value |=
  885. (SCU_SAS_PCFG_GEN_BIT(OOB_RESET) |
  886. SCU_SAS_PCFG_GEN_BIT(SUSPEND_PROTOCOL_ENGINE) |
  887. SCU_SAS_PCFG_GEN_BIT(SATA_SPINUP_HOLD));
  888. writel(scu_sas_pcfg_value,
  889. &iphy->link_layer_registers->phy_configuration);
  890. /* Disable the notify enable spinup primitives */
  891. enable_spinup_value = readl(&iphy->link_layer_registers->notify_enable_spinup_control);
  892. enable_spinup_value &= ~SCU_ENSPINUP_GEN_BIT(ENABLE);
  893. writel(enable_spinup_value, &iphy->link_layer_registers->notify_enable_spinup_control);
  894. }
  895. /**
  896. *
  897. *
  898. * This method will start the OOB/SN state machine for this struct isci_phy object.
  899. */
  900. static void scu_link_layer_start_oob(
  901. struct isci_phy *iphy)
  902. {
  903. u32 scu_sas_pcfg_value;
  904. scu_sas_pcfg_value =
  905. readl(&iphy->link_layer_registers->phy_configuration);
  906. scu_sas_pcfg_value |= SCU_SAS_PCFG_GEN_BIT(OOB_ENABLE);
  907. scu_sas_pcfg_value &=
  908. ~(SCU_SAS_PCFG_GEN_BIT(OOB_RESET) |
  909. SCU_SAS_PCFG_GEN_BIT(HARD_RESET));
  910. writel(scu_sas_pcfg_value,
  911. &iphy->link_layer_registers->phy_configuration);
  912. }
  913. /**
  914. *
  915. *
  916. * This method will transmit a hard reset request on the specified phy. The SCU
  917. * hardware requires that we reset the OOB state machine and set the hard reset
  918. * bit in the phy configuration register. We then must start OOB over with the
  919. * hard reset bit set.
  920. */
  921. static void scu_link_layer_tx_hard_reset(
  922. struct isci_phy *iphy)
  923. {
  924. u32 phy_configuration_value;
  925. /*
  926. * SAS Phys must wait for the HARD_RESET_TX event notification to transition
  927. * to the starting state. */
  928. phy_configuration_value =
  929. readl(&iphy->link_layer_registers->phy_configuration);
  930. phy_configuration_value |=
  931. (SCU_SAS_PCFG_GEN_BIT(HARD_RESET) |
  932. SCU_SAS_PCFG_GEN_BIT(OOB_RESET));
  933. writel(phy_configuration_value,
  934. &iphy->link_layer_registers->phy_configuration);
  935. /* Now take the OOB state machine out of reset */
  936. phy_configuration_value |= SCU_SAS_PCFG_GEN_BIT(OOB_ENABLE);
  937. phy_configuration_value &= ~SCU_SAS_PCFG_GEN_BIT(OOB_RESET);
  938. writel(phy_configuration_value,
  939. &iphy->link_layer_registers->phy_configuration);
  940. }
  941. static void sci_phy_stopped_state_enter(struct sci_base_state_machine *sm)
  942. {
  943. struct isci_phy *iphy = container_of(sm, typeof(*iphy), sm);
  944. struct isci_port *iport = iphy->owning_port;
  945. struct isci_host *ihost = iport->owning_controller;
  946. /*
  947. * @todo We need to get to the controller to place this PE in a
  948. * reset state
  949. */
  950. sci_del_timer(&iphy->sata_timer);
  951. scu_link_layer_stop_protocol_engine(iphy);
  952. if (iphy->sm.previous_state_id != SCI_PHY_INITIAL)
  953. sci_controller_link_down(ihost, phy_get_non_dummy_port(iphy), iphy);
  954. }
  955. static void sci_phy_starting_state_enter(struct sci_base_state_machine *sm)
  956. {
  957. struct isci_phy *iphy = container_of(sm, typeof(*iphy), sm);
  958. struct isci_port *iport = iphy->owning_port;
  959. struct isci_host *ihost = iport->owning_controller;
  960. scu_link_layer_stop_protocol_engine(iphy);
  961. scu_link_layer_start_oob(iphy);
  962. /* We don't know what kind of phy we are going to be just yet */
  963. iphy->protocol = SCIC_SDS_PHY_PROTOCOL_UNKNOWN;
  964. iphy->bcn_received_while_port_unassigned = false;
  965. if (iphy->sm.previous_state_id == SCI_PHY_READY)
  966. sci_controller_link_down(ihost, phy_get_non_dummy_port(iphy), iphy);
  967. sci_change_state(&iphy->sm, SCI_PHY_SUB_INITIAL);
  968. }
  969. static void sci_phy_ready_state_enter(struct sci_base_state_machine *sm)
  970. {
  971. struct isci_phy *iphy = container_of(sm, typeof(*iphy), sm);
  972. struct isci_port *iport = iphy->owning_port;
  973. struct isci_host *ihost = iport->owning_controller;
  974. sci_controller_link_up(ihost, phy_get_non_dummy_port(iphy), iphy);
  975. }
  976. static void sci_phy_ready_state_exit(struct sci_base_state_machine *sm)
  977. {
  978. struct isci_phy *iphy = container_of(sm, typeof(*iphy), sm);
  979. sci_phy_suspend(iphy);
  980. }
  981. static void sci_phy_resetting_state_enter(struct sci_base_state_machine *sm)
  982. {
  983. struct isci_phy *iphy = container_of(sm, typeof(*iphy), sm);
  984. /* The phy is being reset, therefore deactivate it from the port. In
  985. * the resetting state we don't notify the user regarding link up and
  986. * link down notifications
  987. */
  988. sci_port_deactivate_phy(iphy->owning_port, iphy, false);
  989. if (iphy->protocol == SCIC_SDS_PHY_PROTOCOL_SAS) {
  990. scu_link_layer_tx_hard_reset(iphy);
  991. } else {
  992. /* The SCU does not need to have a discrete reset state so
  993. * just go back to the starting state.
  994. */
  995. sci_change_state(&iphy->sm, SCI_PHY_STARTING);
  996. }
  997. }
  998. static const struct sci_base_state sci_phy_state_table[] = {
  999. [SCI_PHY_INITIAL] = { },
  1000. [SCI_PHY_STOPPED] = {
  1001. .enter_state = sci_phy_stopped_state_enter,
  1002. },
  1003. [SCI_PHY_STARTING] = {
  1004. .enter_state = sci_phy_starting_state_enter,
  1005. },
  1006. [SCI_PHY_SUB_INITIAL] = {
  1007. .enter_state = sci_phy_starting_initial_substate_enter,
  1008. },
  1009. [SCI_PHY_SUB_AWAIT_OSSP_EN] = { },
  1010. [SCI_PHY_SUB_AWAIT_SAS_SPEED_EN] = { },
  1011. [SCI_PHY_SUB_AWAIT_IAF_UF] = { },
  1012. [SCI_PHY_SUB_AWAIT_SAS_POWER] = {
  1013. .enter_state = sci_phy_starting_await_sas_power_substate_enter,
  1014. .exit_state = sci_phy_starting_await_sas_power_substate_exit,
  1015. },
  1016. [SCI_PHY_SUB_AWAIT_SATA_POWER] = {
  1017. .enter_state = sci_phy_starting_await_sata_power_substate_enter,
  1018. .exit_state = sci_phy_starting_await_sata_power_substate_exit
  1019. },
  1020. [SCI_PHY_SUB_AWAIT_SATA_PHY_EN] = {
  1021. .enter_state = sci_phy_starting_await_sata_phy_substate_enter,
  1022. .exit_state = sci_phy_starting_await_sata_phy_substate_exit
  1023. },
  1024. [SCI_PHY_SUB_AWAIT_SATA_SPEED_EN] = {
  1025. .enter_state = sci_phy_starting_await_sata_speed_substate_enter,
  1026. .exit_state = sci_phy_starting_await_sata_speed_substate_exit
  1027. },
  1028. [SCI_PHY_SUB_AWAIT_SIG_FIS_UF] = {
  1029. .enter_state = sci_phy_starting_await_sig_fis_uf_substate_enter,
  1030. .exit_state = sci_phy_starting_await_sig_fis_uf_substate_exit
  1031. },
  1032. [SCI_PHY_SUB_FINAL] = {
  1033. .enter_state = sci_phy_starting_final_substate_enter,
  1034. },
  1035. [SCI_PHY_READY] = {
  1036. .enter_state = sci_phy_ready_state_enter,
  1037. .exit_state = sci_phy_ready_state_exit,
  1038. },
  1039. [SCI_PHY_RESETTING] = {
  1040. .enter_state = sci_phy_resetting_state_enter,
  1041. },
  1042. [SCI_PHY_FINAL] = { },
  1043. };
  1044. void sci_phy_construct(struct isci_phy *iphy,
  1045. struct isci_port *iport, u8 phy_index)
  1046. {
  1047. sci_init_sm(&iphy->sm, sci_phy_state_table, SCI_PHY_INITIAL);
  1048. /* Copy the rest of the input data to our locals */
  1049. iphy->owning_port = iport;
  1050. iphy->phy_index = phy_index;
  1051. iphy->bcn_received_while_port_unassigned = false;
  1052. iphy->protocol = SCIC_SDS_PHY_PROTOCOL_UNKNOWN;
  1053. iphy->link_layer_registers = NULL;
  1054. iphy->max_negotiated_speed = SAS_LINK_RATE_UNKNOWN;
  1055. /* Create the SIGNATURE FIS Timeout timer for this phy */
  1056. sci_init_timer(&iphy->sata_timer, phy_sata_timeout);
  1057. }
  1058. void isci_phy_init(struct isci_phy *iphy, struct isci_host *ihost, int index)
  1059. {
  1060. struct sci_oem_params *oem = &ihost->oem_parameters;
  1061. u64 sci_sas_addr;
  1062. __be64 sas_addr;
  1063. sci_sas_addr = oem->phys[index].sas_address.high;
  1064. sci_sas_addr <<= 32;
  1065. sci_sas_addr |= oem->phys[index].sas_address.low;
  1066. sas_addr = cpu_to_be64(sci_sas_addr);
  1067. memcpy(iphy->sas_addr, &sas_addr, sizeof(sas_addr));
  1068. iphy->isci_port = NULL;
  1069. iphy->sas_phy.enabled = 0;
  1070. iphy->sas_phy.id = index;
  1071. iphy->sas_phy.sas_addr = &iphy->sas_addr[0];
  1072. iphy->sas_phy.frame_rcvd = (u8 *)&iphy->frame_rcvd;
  1073. iphy->sas_phy.ha = &ihost->sas_ha;
  1074. iphy->sas_phy.lldd_phy = iphy;
  1075. iphy->sas_phy.enabled = 1;
  1076. iphy->sas_phy.class = SAS;
  1077. iphy->sas_phy.iproto = SAS_PROTOCOL_ALL;
  1078. iphy->sas_phy.tproto = 0;
  1079. iphy->sas_phy.type = PHY_TYPE_PHYSICAL;
  1080. iphy->sas_phy.role = PHY_ROLE_INITIATOR;
  1081. iphy->sas_phy.oob_mode = OOB_NOT_CONNECTED;
  1082. iphy->sas_phy.linkrate = SAS_LINK_RATE_UNKNOWN;
  1083. memset(&iphy->frame_rcvd, 0, sizeof(iphy->frame_rcvd));
  1084. }
  1085. /**
  1086. * isci_phy_control() - This function is one of the SAS Domain Template
  1087. * functions. This is a phy management function.
  1088. * @phy: This parameter specifies the sphy being controlled.
  1089. * @func: This parameter specifies the phy control function being invoked.
  1090. * @buf: This parameter is specific to the phy function being invoked.
  1091. *
  1092. * status, zero indicates success.
  1093. */
  1094. int isci_phy_control(struct asd_sas_phy *sas_phy,
  1095. enum phy_func func,
  1096. void *buf)
  1097. {
  1098. int ret = 0;
  1099. struct isci_phy *iphy = sas_phy->lldd_phy;
  1100. struct isci_port *iport = iphy->isci_port;
  1101. struct isci_host *ihost = sas_phy->ha->lldd_ha;
  1102. unsigned long flags;
  1103. dev_dbg(&ihost->pdev->dev,
  1104. "%s: phy %p; func %d; buf %p; isci phy %p, port %p\n",
  1105. __func__, sas_phy, func, buf, iphy, iport);
  1106. switch (func) {
  1107. case PHY_FUNC_DISABLE:
  1108. spin_lock_irqsave(&ihost->scic_lock, flags);
  1109. sci_phy_stop(iphy);
  1110. spin_unlock_irqrestore(&ihost->scic_lock, flags);
  1111. break;
  1112. case PHY_FUNC_LINK_RESET:
  1113. spin_lock_irqsave(&ihost->scic_lock, flags);
  1114. sci_phy_stop(iphy);
  1115. sci_phy_start(iphy);
  1116. spin_unlock_irqrestore(&ihost->scic_lock, flags);
  1117. break;
  1118. case PHY_FUNC_HARD_RESET:
  1119. if (!iport)
  1120. return -ENODEV;
  1121. /* Perform the port reset. */
  1122. ret = isci_port_perform_hard_reset(ihost, iport, iphy);
  1123. break;
  1124. default:
  1125. dev_dbg(&ihost->pdev->dev,
  1126. "%s: phy %p; func %d NOT IMPLEMENTED!\n",
  1127. __func__, sas_phy, func);
  1128. ret = -ENOSYS;
  1129. break;
  1130. }
  1131. return ret;
  1132. }