phy.c 41 KB

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