phy.c 43 KB

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