phy.c 42 KB

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