phy.c 46 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488
  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 __iomem *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 = SAS_PROTOCOL_SSP;
  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 = SAS_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. void scu_link_layer_set_txcomsas_timeout(struct isci_phy *iphy, u32 timeout)
  564. {
  565. u32 val;
  566. /* Extend timeout */
  567. val = readl(&iphy->link_layer_registers->transmit_comsas_signal);
  568. val &= ~SCU_SAS_LLTXCOMSAS_GEN_VAL(NEGTIME, SCU_SAS_LINK_LAYER_TXCOMSAS_NEGTIME_MASK);
  569. val |= SCU_SAS_LLTXCOMSAS_GEN_VAL(NEGTIME, timeout);
  570. writel(val, &iphy->link_layer_registers->transmit_comsas_signal);
  571. }
  572. enum sci_status sci_phy_event_handler(struct isci_phy *iphy, u32 event_code)
  573. {
  574. enum sci_phy_states state = iphy->sm.current_state_id;
  575. switch (state) {
  576. case SCI_PHY_SUB_AWAIT_OSSP_EN:
  577. switch (scu_get_event_code(event_code)) {
  578. case SCU_EVENT_SAS_PHY_DETECTED:
  579. sci_phy_start_sas_link_training(iphy);
  580. iphy->is_in_link_training = true;
  581. break;
  582. case SCU_EVENT_SATA_SPINUP_HOLD:
  583. sci_phy_start_sata_link_training(iphy);
  584. iphy->is_in_link_training = true;
  585. break;
  586. case SCU_EVENT_RECEIVED_IDENTIFY_TIMEOUT:
  587. /* Extend timeout value */
  588. scu_link_layer_set_txcomsas_timeout(iphy, SCU_SAS_LINK_LAYER_TXCOMSAS_NEGTIME_EXTENDED);
  589. /* Start the oob/sn state machine over again */
  590. sci_change_state(&iphy->sm, SCI_PHY_STARTING);
  591. break;
  592. default:
  593. phy_event_dbg(iphy, state, event_code);
  594. return SCI_FAILURE;
  595. }
  596. return SCI_SUCCESS;
  597. case SCI_PHY_SUB_AWAIT_SAS_SPEED_EN:
  598. switch (scu_get_event_code(event_code)) {
  599. case SCU_EVENT_SAS_PHY_DETECTED:
  600. /*
  601. * Why is this being reported again by the controller?
  602. * We would re-enter this state so just stay here */
  603. break;
  604. case SCU_EVENT_SAS_15:
  605. case SCU_EVENT_SAS_15_SSC:
  606. sci_phy_complete_link_training(iphy, SAS_LINK_RATE_1_5_GBPS,
  607. SCI_PHY_SUB_AWAIT_IAF_UF);
  608. break;
  609. case SCU_EVENT_SAS_30:
  610. case SCU_EVENT_SAS_30_SSC:
  611. sci_phy_complete_link_training(iphy, SAS_LINK_RATE_3_0_GBPS,
  612. SCI_PHY_SUB_AWAIT_IAF_UF);
  613. break;
  614. case SCU_EVENT_SAS_60:
  615. case SCU_EVENT_SAS_60_SSC:
  616. sci_phy_complete_link_training(iphy, SAS_LINK_RATE_6_0_GBPS,
  617. SCI_PHY_SUB_AWAIT_IAF_UF);
  618. break;
  619. case SCU_EVENT_SATA_SPINUP_HOLD:
  620. /*
  621. * We were doing SAS PHY link training and received a SATA PHY event
  622. * continue OOB/SN as if this were a SATA PHY */
  623. sci_phy_start_sata_link_training(iphy);
  624. break;
  625. case SCU_EVENT_LINK_FAILURE:
  626. /* Change the timeout value to default */
  627. scu_link_layer_set_txcomsas_timeout(iphy, SCU_SAS_LINK_LAYER_TXCOMSAS_NEGTIME_DEFAULT);
  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_RECEIVED_IDENTIFY_TIMEOUT:
  632. /* Extend the timeout value */
  633. scu_link_layer_set_txcomsas_timeout(iphy, SCU_SAS_LINK_LAYER_TXCOMSAS_NEGTIME_EXTENDED);
  634. /* Start the oob/sn state machine over again */
  635. sci_change_state(&iphy->sm, SCI_PHY_STARTING);
  636. break;
  637. default:
  638. phy_event_warn(iphy, state, event_code);
  639. return SCI_FAILURE;
  640. break;
  641. }
  642. return SCI_SUCCESS;
  643. case SCI_PHY_SUB_AWAIT_IAF_UF:
  644. switch (scu_get_event_code(event_code)) {
  645. case SCU_EVENT_SAS_PHY_DETECTED:
  646. /* Backup the state machine */
  647. sci_phy_start_sas_link_training(iphy);
  648. break;
  649. case SCU_EVENT_SATA_SPINUP_HOLD:
  650. /* We were doing SAS PHY link training and received a
  651. * SATA PHY event continue OOB/SN as if this were a
  652. * SATA PHY
  653. */
  654. sci_phy_start_sata_link_training(iphy);
  655. break;
  656. case SCU_EVENT_RECEIVED_IDENTIFY_TIMEOUT:
  657. /* Extend the timeout value */
  658. scu_link_layer_set_txcomsas_timeout(iphy, SCU_SAS_LINK_LAYER_TXCOMSAS_NEGTIME_EXTENDED);
  659. /* Start the oob/sn state machine over again */
  660. sci_change_state(&iphy->sm, SCI_PHY_STARTING);
  661. break;
  662. case SCU_EVENT_LINK_FAILURE:
  663. scu_link_layer_set_txcomsas_timeout(iphy, SCU_SAS_LINK_LAYER_TXCOMSAS_NEGTIME_DEFAULT);
  664. case SCU_EVENT_HARD_RESET_RECEIVED:
  665. /* Start the oob/sn state machine over again */
  666. sci_change_state(&iphy->sm, SCI_PHY_STARTING);
  667. break;
  668. default:
  669. phy_event_warn(iphy, state, event_code);
  670. return SCI_FAILURE;
  671. }
  672. return SCI_SUCCESS;
  673. case SCI_PHY_SUB_AWAIT_SAS_POWER:
  674. switch (scu_get_event_code(event_code)) {
  675. case SCU_EVENT_LINK_FAILURE:
  676. /* Change the timeout value to default */
  677. scu_link_layer_set_txcomsas_timeout(iphy, SCU_SAS_LINK_LAYER_TXCOMSAS_NEGTIME_DEFAULT);
  678. /* Link failure change state back to the starting state */
  679. sci_change_state(&iphy->sm, SCI_PHY_STARTING);
  680. break;
  681. default:
  682. phy_event_warn(iphy, state, event_code);
  683. return SCI_FAILURE;
  684. }
  685. return SCI_SUCCESS;
  686. case SCI_PHY_SUB_AWAIT_SATA_POWER:
  687. switch (scu_get_event_code(event_code)) {
  688. case SCU_EVENT_LINK_FAILURE:
  689. /* Change the timeout value to default */
  690. scu_link_layer_set_txcomsas_timeout(iphy, SCU_SAS_LINK_LAYER_TXCOMSAS_NEGTIME_DEFAULT);
  691. /* Link failure change state back to the starting state */
  692. sci_change_state(&iphy->sm, SCI_PHY_STARTING);
  693. break;
  694. case SCU_EVENT_SATA_SPINUP_HOLD:
  695. /* These events are received every 10ms and are
  696. * expected while in this state
  697. */
  698. break;
  699. case SCU_EVENT_SAS_PHY_DETECTED:
  700. /* There has been a change in the phy type before OOB/SN for the
  701. * SATA finished start down the SAS link traning path.
  702. */
  703. sci_phy_start_sas_link_training(iphy);
  704. break;
  705. default:
  706. phy_event_warn(iphy, state, event_code);
  707. return SCI_FAILURE;
  708. }
  709. return SCI_SUCCESS;
  710. case SCI_PHY_SUB_AWAIT_SATA_PHY_EN:
  711. switch (scu_get_event_code(event_code)) {
  712. case SCU_EVENT_LINK_FAILURE:
  713. /* Change the timeout value to default */
  714. scu_link_layer_set_txcomsas_timeout(iphy, SCU_SAS_LINK_LAYER_TXCOMSAS_NEGTIME_DEFAULT);
  715. /* Link failure change state back to the starting state */
  716. sci_change_state(&iphy->sm, SCI_PHY_STARTING);
  717. break;
  718. case SCU_EVENT_SATA_SPINUP_HOLD:
  719. /* These events might be received since we dont know how many may be in
  720. * the completion queue while waiting for power
  721. */
  722. break;
  723. case SCU_EVENT_SATA_PHY_DETECTED:
  724. iphy->protocol = SAS_PROTOCOL_SATA;
  725. /* We have received the SATA PHY notification change state */
  726. sci_change_state(&iphy->sm, SCI_PHY_SUB_AWAIT_SATA_SPEED_EN);
  727. break;
  728. case SCU_EVENT_SAS_PHY_DETECTED:
  729. /* There has been a change in the phy type before OOB/SN for the
  730. * SATA finished start down the SAS link traning path.
  731. */
  732. sci_phy_start_sas_link_training(iphy);
  733. break;
  734. default:
  735. phy_event_warn(iphy, state, event_code);
  736. return SCI_FAILURE;
  737. }
  738. return SCI_SUCCESS;
  739. case SCI_PHY_SUB_AWAIT_SATA_SPEED_EN:
  740. switch (scu_get_event_code(event_code)) {
  741. case SCU_EVENT_SATA_PHY_DETECTED:
  742. /*
  743. * The hardware reports multiple SATA PHY detected events
  744. * ignore the extras */
  745. break;
  746. case SCU_EVENT_SATA_15:
  747. case SCU_EVENT_SATA_15_SSC:
  748. sci_phy_complete_link_training(iphy, SAS_LINK_RATE_1_5_GBPS,
  749. SCI_PHY_SUB_AWAIT_SIG_FIS_UF);
  750. break;
  751. case SCU_EVENT_SATA_30:
  752. case SCU_EVENT_SATA_30_SSC:
  753. sci_phy_complete_link_training(iphy, SAS_LINK_RATE_3_0_GBPS,
  754. SCI_PHY_SUB_AWAIT_SIG_FIS_UF);
  755. break;
  756. case SCU_EVENT_SATA_60:
  757. case SCU_EVENT_SATA_60_SSC:
  758. sci_phy_complete_link_training(iphy, SAS_LINK_RATE_6_0_GBPS,
  759. SCI_PHY_SUB_AWAIT_SIG_FIS_UF);
  760. break;
  761. case SCU_EVENT_LINK_FAILURE:
  762. /* Change the timeout value to default */
  763. scu_link_layer_set_txcomsas_timeout(iphy, SCU_SAS_LINK_LAYER_TXCOMSAS_NEGTIME_DEFAULT);
  764. /* Link failure change state back to the starting state */
  765. sci_change_state(&iphy->sm, SCI_PHY_STARTING);
  766. break;
  767. case SCU_EVENT_SAS_PHY_DETECTED:
  768. /*
  769. * There has been a change in the phy type before OOB/SN for the
  770. * SATA finished start down the SAS link traning path. */
  771. sci_phy_start_sas_link_training(iphy);
  772. break;
  773. default:
  774. phy_event_warn(iphy, state, event_code);
  775. return SCI_FAILURE;
  776. }
  777. return SCI_SUCCESS;
  778. case SCI_PHY_SUB_AWAIT_SIG_FIS_UF:
  779. switch (scu_get_event_code(event_code)) {
  780. case SCU_EVENT_SATA_PHY_DETECTED:
  781. /* Backup the state machine */
  782. sci_change_state(&iphy->sm, SCI_PHY_SUB_AWAIT_SATA_SPEED_EN);
  783. break;
  784. case SCU_EVENT_LINK_FAILURE:
  785. /* Change the timeout value to default */
  786. scu_link_layer_set_txcomsas_timeout(iphy, SCU_SAS_LINK_LAYER_TXCOMSAS_NEGTIME_DEFAULT);
  787. /* Link failure change state back to the starting state */
  788. sci_change_state(&iphy->sm, SCI_PHY_STARTING);
  789. break;
  790. default:
  791. phy_event_warn(iphy, state, event_code);
  792. return SCI_FAILURE;
  793. }
  794. return SCI_SUCCESS;
  795. case SCI_PHY_READY:
  796. switch (scu_get_event_code(event_code)) {
  797. case SCU_EVENT_LINK_FAILURE:
  798. /* Set default timeout */
  799. scu_link_layer_set_txcomsas_timeout(iphy, SCU_SAS_LINK_LAYER_TXCOMSAS_NEGTIME_DEFAULT);
  800. /* Link failure change state back to the starting state */
  801. sci_change_state(&iphy->sm, SCI_PHY_STARTING);
  802. break;
  803. case SCU_EVENT_BROADCAST_CHANGE:
  804. case SCU_EVENT_BROADCAST_SES:
  805. case SCU_EVENT_BROADCAST_RESERVED0:
  806. case SCU_EVENT_BROADCAST_RESERVED1:
  807. case SCU_EVENT_BROADCAST_EXPANDER:
  808. case SCU_EVENT_BROADCAST_AEN:
  809. /* Broadcast change received. Notify the port. */
  810. if (phy_get_non_dummy_port(iphy) != NULL)
  811. sci_port_broadcast_change_received(iphy->owning_port, iphy);
  812. else
  813. iphy->bcn_received_while_port_unassigned = true;
  814. break;
  815. case SCU_EVENT_BROADCAST_RESERVED3:
  816. case SCU_EVENT_BROADCAST_RESERVED4:
  817. default:
  818. phy_event_warn(iphy, state, event_code);
  819. return SCI_FAILURE_INVALID_STATE;
  820. }
  821. return SCI_SUCCESS;
  822. case SCI_PHY_RESETTING:
  823. switch (scu_get_event_code(event_code)) {
  824. case SCU_EVENT_HARD_RESET_TRANSMITTED:
  825. /* Link failure change state back to the starting state */
  826. sci_change_state(&iphy->sm, SCI_PHY_STARTING);
  827. break;
  828. default:
  829. phy_event_warn(iphy, state, event_code);
  830. return SCI_FAILURE_INVALID_STATE;
  831. break;
  832. }
  833. return SCI_SUCCESS;
  834. default:
  835. dev_dbg(sciphy_to_dev(iphy), "%s: in wrong state: %s\n",
  836. __func__, phy_state_name(state));
  837. return SCI_FAILURE_INVALID_STATE;
  838. }
  839. }
  840. enum sci_status sci_phy_frame_handler(struct isci_phy *iphy, u32 frame_index)
  841. {
  842. enum sci_phy_states state = iphy->sm.current_state_id;
  843. struct isci_host *ihost = iphy->owning_port->owning_controller;
  844. enum sci_status result;
  845. unsigned long flags;
  846. switch (state) {
  847. case SCI_PHY_SUB_AWAIT_IAF_UF: {
  848. u32 *frame_words;
  849. struct sas_identify_frame iaf;
  850. result = sci_unsolicited_frame_control_get_header(&ihost->uf_control,
  851. frame_index,
  852. (void **)&frame_words);
  853. if (result != SCI_SUCCESS)
  854. return result;
  855. sci_swab32_cpy(&iaf, frame_words, sizeof(iaf) / sizeof(u32));
  856. if (iaf.frame_type == 0) {
  857. u32 state;
  858. spin_lock_irqsave(&iphy->sas_phy.frame_rcvd_lock, flags);
  859. memcpy(&iphy->frame_rcvd.iaf, &iaf, sizeof(iaf));
  860. spin_unlock_irqrestore(&iphy->sas_phy.frame_rcvd_lock, flags);
  861. if (iaf.smp_tport) {
  862. /* We got the IAF for an expander PHY go to the final
  863. * state since there are no power requirements for
  864. * expander phys.
  865. */
  866. state = SCI_PHY_SUB_FINAL;
  867. } else {
  868. /* We got the IAF we can now go to the await spinup
  869. * semaphore state
  870. */
  871. state = SCI_PHY_SUB_AWAIT_SAS_POWER;
  872. }
  873. sci_change_state(&iphy->sm, state);
  874. result = SCI_SUCCESS;
  875. } else
  876. dev_warn(sciphy_to_dev(iphy),
  877. "%s: PHY starting substate machine received "
  878. "unexpected frame id %x\n",
  879. __func__, frame_index);
  880. sci_controller_release_frame(ihost, frame_index);
  881. return result;
  882. }
  883. case SCI_PHY_SUB_AWAIT_SIG_FIS_UF: {
  884. struct dev_to_host_fis *frame_header;
  885. u32 *fis_frame_data;
  886. result = sci_unsolicited_frame_control_get_header(&ihost->uf_control,
  887. frame_index,
  888. (void **)&frame_header);
  889. if (result != SCI_SUCCESS)
  890. return result;
  891. if ((frame_header->fis_type == FIS_REGD2H) &&
  892. !(frame_header->status & ATA_BUSY)) {
  893. sci_unsolicited_frame_control_get_buffer(&ihost->uf_control,
  894. frame_index,
  895. (void **)&fis_frame_data);
  896. spin_lock_irqsave(&iphy->sas_phy.frame_rcvd_lock, flags);
  897. sci_controller_copy_sata_response(&iphy->frame_rcvd.fis,
  898. frame_header,
  899. fis_frame_data);
  900. spin_unlock_irqrestore(&iphy->sas_phy.frame_rcvd_lock, flags);
  901. /* got IAF we can now go to the await spinup semaphore state */
  902. sci_change_state(&iphy->sm, SCI_PHY_SUB_FINAL);
  903. result = SCI_SUCCESS;
  904. } else
  905. dev_warn(sciphy_to_dev(iphy),
  906. "%s: PHY starting substate machine received "
  907. "unexpected frame id %x\n",
  908. __func__, frame_index);
  909. /* Regardless of the result we are done with this frame with it */
  910. sci_controller_release_frame(ihost, frame_index);
  911. return result;
  912. }
  913. default:
  914. dev_dbg(sciphy_to_dev(iphy), "%s: in wrong state: %s\n",
  915. __func__, phy_state_name(state));
  916. return SCI_FAILURE_INVALID_STATE;
  917. }
  918. }
  919. static void sci_phy_starting_initial_substate_enter(struct sci_base_state_machine *sm)
  920. {
  921. struct isci_phy *iphy = container_of(sm, typeof(*iphy), sm);
  922. /* This is just an temporary state go off to the starting state */
  923. sci_change_state(&iphy->sm, SCI_PHY_SUB_AWAIT_OSSP_EN);
  924. }
  925. static void sci_phy_starting_await_sas_power_substate_enter(struct sci_base_state_machine *sm)
  926. {
  927. struct isci_phy *iphy = container_of(sm, typeof(*iphy), sm);
  928. struct isci_host *ihost = iphy->owning_port->owning_controller;
  929. sci_controller_power_control_queue_insert(ihost, iphy);
  930. }
  931. static void sci_phy_starting_await_sas_power_substate_exit(struct sci_base_state_machine *sm)
  932. {
  933. struct isci_phy *iphy = container_of(sm, typeof(*iphy), sm);
  934. struct isci_host *ihost = iphy->owning_port->owning_controller;
  935. sci_controller_power_control_queue_remove(ihost, iphy);
  936. }
  937. static void sci_phy_starting_await_sata_power_substate_enter(struct sci_base_state_machine *sm)
  938. {
  939. struct isci_phy *iphy = container_of(sm, typeof(*iphy), sm);
  940. struct isci_host *ihost = iphy->owning_port->owning_controller;
  941. sci_controller_power_control_queue_insert(ihost, iphy);
  942. }
  943. static void sci_phy_starting_await_sata_power_substate_exit(struct sci_base_state_machine *sm)
  944. {
  945. struct isci_phy *iphy = container_of(sm, typeof(*iphy), sm);
  946. struct isci_host *ihost = iphy->owning_port->owning_controller;
  947. sci_controller_power_control_queue_remove(ihost, iphy);
  948. }
  949. static void sci_phy_starting_await_sata_phy_substate_enter(struct sci_base_state_machine *sm)
  950. {
  951. struct isci_phy *iphy = container_of(sm, typeof(*iphy), sm);
  952. sci_mod_timer(&iphy->sata_timer, SCIC_SDS_SATA_LINK_TRAINING_TIMEOUT);
  953. }
  954. static void sci_phy_starting_await_sata_phy_substate_exit(struct sci_base_state_machine *sm)
  955. {
  956. struct isci_phy *iphy = container_of(sm, typeof(*iphy), sm);
  957. sci_del_timer(&iphy->sata_timer);
  958. }
  959. static void sci_phy_starting_await_sata_speed_substate_enter(struct sci_base_state_machine *sm)
  960. {
  961. struct isci_phy *iphy = container_of(sm, typeof(*iphy), sm);
  962. sci_mod_timer(&iphy->sata_timer, SCIC_SDS_SATA_LINK_TRAINING_TIMEOUT);
  963. }
  964. static void sci_phy_starting_await_sata_speed_substate_exit(struct sci_base_state_machine *sm)
  965. {
  966. struct isci_phy *iphy = container_of(sm, typeof(*iphy), sm);
  967. sci_del_timer(&iphy->sata_timer);
  968. }
  969. static void sci_phy_starting_await_sig_fis_uf_substate_enter(struct sci_base_state_machine *sm)
  970. {
  971. struct isci_phy *iphy = container_of(sm, typeof(*iphy), sm);
  972. if (sci_port_link_detected(iphy->owning_port, iphy)) {
  973. /*
  974. * Clear the PE suspend condition so we can actually
  975. * receive SIG FIS
  976. * The hardware will not respond to the XRDY until the PE
  977. * suspend condition is cleared.
  978. */
  979. sci_phy_resume(iphy);
  980. sci_mod_timer(&iphy->sata_timer,
  981. SCIC_SDS_SIGNATURE_FIS_TIMEOUT);
  982. } else
  983. iphy->is_in_link_training = false;
  984. }
  985. static void sci_phy_starting_await_sig_fis_uf_substate_exit(struct sci_base_state_machine *sm)
  986. {
  987. struct isci_phy *iphy = container_of(sm, typeof(*iphy), sm);
  988. sci_del_timer(&iphy->sata_timer);
  989. }
  990. static void sci_phy_starting_final_substate_enter(struct sci_base_state_machine *sm)
  991. {
  992. struct isci_phy *iphy = container_of(sm, typeof(*iphy), sm);
  993. /* State machine has run to completion so exit out and change
  994. * the base state machine to the ready state
  995. */
  996. sci_change_state(&iphy->sm, SCI_PHY_READY);
  997. }
  998. /**
  999. *
  1000. * @sci_phy: This is the struct isci_phy object to stop.
  1001. *
  1002. * This method will stop the struct isci_phy object. This does not reset the
  1003. * protocol engine it just suspends it and places it in a state where it will
  1004. * not cause the end device to power up. none
  1005. */
  1006. static void scu_link_layer_stop_protocol_engine(
  1007. struct isci_phy *iphy)
  1008. {
  1009. u32 scu_sas_pcfg_value;
  1010. u32 enable_spinup_value;
  1011. /* Suspend the protocol engine and place it in a sata spinup hold state */
  1012. scu_sas_pcfg_value =
  1013. readl(&iphy->link_layer_registers->phy_configuration);
  1014. scu_sas_pcfg_value |=
  1015. (SCU_SAS_PCFG_GEN_BIT(OOB_RESET) |
  1016. SCU_SAS_PCFG_GEN_BIT(SUSPEND_PROTOCOL_ENGINE) |
  1017. SCU_SAS_PCFG_GEN_BIT(SATA_SPINUP_HOLD));
  1018. writel(scu_sas_pcfg_value,
  1019. &iphy->link_layer_registers->phy_configuration);
  1020. /* Disable the notify enable spinup primitives */
  1021. enable_spinup_value = readl(&iphy->link_layer_registers->notify_enable_spinup_control);
  1022. enable_spinup_value &= ~SCU_ENSPINUP_GEN_BIT(ENABLE);
  1023. writel(enable_spinup_value, &iphy->link_layer_registers->notify_enable_spinup_control);
  1024. }
  1025. static void scu_link_layer_start_oob(struct isci_phy *iphy)
  1026. {
  1027. struct scu_link_layer_registers __iomem *ll = iphy->link_layer_registers;
  1028. u32 val;
  1029. /** Reset OOB sequence - start */
  1030. val = readl(&ll->phy_configuration);
  1031. val &= ~(SCU_SAS_PCFG_GEN_BIT(OOB_RESET) |
  1032. SCU_SAS_PCFG_GEN_BIT(OOB_ENABLE) |
  1033. SCU_SAS_PCFG_GEN_BIT(HARD_RESET));
  1034. writel(val, &ll->phy_configuration);
  1035. readl(&ll->phy_configuration); /* flush */
  1036. /** Reset OOB sequence - end */
  1037. /** Start OOB sequence - start */
  1038. val = readl(&ll->phy_configuration);
  1039. val |= SCU_SAS_PCFG_GEN_BIT(OOB_ENABLE);
  1040. writel(val, &ll->phy_configuration);
  1041. readl(&ll->phy_configuration); /* flush */
  1042. /** Start OOB sequence - end */
  1043. }
  1044. /**
  1045. *
  1046. *
  1047. * This method will transmit a hard reset request on the specified phy. The SCU
  1048. * hardware requires that we reset the OOB state machine and set the hard reset
  1049. * bit in the phy configuration register. We then must start OOB over with the
  1050. * hard reset bit set.
  1051. */
  1052. static void scu_link_layer_tx_hard_reset(
  1053. struct isci_phy *iphy)
  1054. {
  1055. u32 phy_configuration_value;
  1056. /*
  1057. * SAS Phys must wait for the HARD_RESET_TX event notification to transition
  1058. * to the starting state. */
  1059. phy_configuration_value =
  1060. readl(&iphy->link_layer_registers->phy_configuration);
  1061. phy_configuration_value &= ~(SCU_SAS_PCFG_GEN_BIT(OOB_ENABLE));
  1062. phy_configuration_value |=
  1063. (SCU_SAS_PCFG_GEN_BIT(HARD_RESET) |
  1064. SCU_SAS_PCFG_GEN_BIT(OOB_RESET));
  1065. writel(phy_configuration_value,
  1066. &iphy->link_layer_registers->phy_configuration);
  1067. /* Now take the OOB state machine out of reset */
  1068. phy_configuration_value |= SCU_SAS_PCFG_GEN_BIT(OOB_ENABLE);
  1069. phy_configuration_value &= ~SCU_SAS_PCFG_GEN_BIT(OOB_RESET);
  1070. writel(phy_configuration_value,
  1071. &iphy->link_layer_registers->phy_configuration);
  1072. }
  1073. static void sci_phy_stopped_state_enter(struct sci_base_state_machine *sm)
  1074. {
  1075. struct isci_phy *iphy = container_of(sm, typeof(*iphy), sm);
  1076. struct isci_port *iport = iphy->owning_port;
  1077. struct isci_host *ihost = iport->owning_controller;
  1078. /*
  1079. * @todo We need to get to the controller to place this PE in a
  1080. * reset state
  1081. */
  1082. sci_del_timer(&iphy->sata_timer);
  1083. scu_link_layer_stop_protocol_engine(iphy);
  1084. if (iphy->sm.previous_state_id != SCI_PHY_INITIAL)
  1085. sci_controller_link_down(ihost, phy_get_non_dummy_port(iphy), iphy);
  1086. }
  1087. static void sci_phy_starting_state_enter(struct sci_base_state_machine *sm)
  1088. {
  1089. struct isci_phy *iphy = container_of(sm, typeof(*iphy), sm);
  1090. struct isci_port *iport = iphy->owning_port;
  1091. struct isci_host *ihost = iport->owning_controller;
  1092. scu_link_layer_stop_protocol_engine(iphy);
  1093. scu_link_layer_start_oob(iphy);
  1094. /* We don't know what kind of phy we are going to be just yet */
  1095. iphy->protocol = SAS_PROTOCOL_NONE;
  1096. iphy->bcn_received_while_port_unassigned = false;
  1097. if (iphy->sm.previous_state_id == SCI_PHY_READY)
  1098. sci_controller_link_down(ihost, phy_get_non_dummy_port(iphy), iphy);
  1099. sci_change_state(&iphy->sm, SCI_PHY_SUB_INITIAL);
  1100. }
  1101. static void sci_phy_ready_state_enter(struct sci_base_state_machine *sm)
  1102. {
  1103. struct isci_phy *iphy = container_of(sm, typeof(*iphy), sm);
  1104. struct isci_port *iport = iphy->owning_port;
  1105. struct isci_host *ihost = iport->owning_controller;
  1106. sci_controller_link_up(ihost, phy_get_non_dummy_port(iphy), iphy);
  1107. }
  1108. static void sci_phy_ready_state_exit(struct sci_base_state_machine *sm)
  1109. {
  1110. struct isci_phy *iphy = container_of(sm, typeof(*iphy), sm);
  1111. sci_phy_suspend(iphy);
  1112. }
  1113. static void sci_phy_resetting_state_enter(struct sci_base_state_machine *sm)
  1114. {
  1115. struct isci_phy *iphy = container_of(sm, typeof(*iphy), sm);
  1116. /* The phy is being reset, therefore deactivate it from the port. In
  1117. * the resetting state we don't notify the user regarding link up and
  1118. * link down notifications
  1119. */
  1120. sci_port_deactivate_phy(iphy->owning_port, iphy, false);
  1121. if (iphy->protocol == SAS_PROTOCOL_SSP) {
  1122. scu_link_layer_tx_hard_reset(iphy);
  1123. } else {
  1124. /* The SCU does not need to have a discrete reset state so
  1125. * just go back to the starting state.
  1126. */
  1127. sci_change_state(&iphy->sm, SCI_PHY_STARTING);
  1128. }
  1129. }
  1130. static const struct sci_base_state sci_phy_state_table[] = {
  1131. [SCI_PHY_INITIAL] = { },
  1132. [SCI_PHY_STOPPED] = {
  1133. .enter_state = sci_phy_stopped_state_enter,
  1134. },
  1135. [SCI_PHY_STARTING] = {
  1136. .enter_state = sci_phy_starting_state_enter,
  1137. },
  1138. [SCI_PHY_SUB_INITIAL] = {
  1139. .enter_state = sci_phy_starting_initial_substate_enter,
  1140. },
  1141. [SCI_PHY_SUB_AWAIT_OSSP_EN] = { },
  1142. [SCI_PHY_SUB_AWAIT_SAS_SPEED_EN] = { },
  1143. [SCI_PHY_SUB_AWAIT_IAF_UF] = { },
  1144. [SCI_PHY_SUB_AWAIT_SAS_POWER] = {
  1145. .enter_state = sci_phy_starting_await_sas_power_substate_enter,
  1146. .exit_state = sci_phy_starting_await_sas_power_substate_exit,
  1147. },
  1148. [SCI_PHY_SUB_AWAIT_SATA_POWER] = {
  1149. .enter_state = sci_phy_starting_await_sata_power_substate_enter,
  1150. .exit_state = sci_phy_starting_await_sata_power_substate_exit
  1151. },
  1152. [SCI_PHY_SUB_AWAIT_SATA_PHY_EN] = {
  1153. .enter_state = sci_phy_starting_await_sata_phy_substate_enter,
  1154. .exit_state = sci_phy_starting_await_sata_phy_substate_exit
  1155. },
  1156. [SCI_PHY_SUB_AWAIT_SATA_SPEED_EN] = {
  1157. .enter_state = sci_phy_starting_await_sata_speed_substate_enter,
  1158. .exit_state = sci_phy_starting_await_sata_speed_substate_exit
  1159. },
  1160. [SCI_PHY_SUB_AWAIT_SIG_FIS_UF] = {
  1161. .enter_state = sci_phy_starting_await_sig_fis_uf_substate_enter,
  1162. .exit_state = sci_phy_starting_await_sig_fis_uf_substate_exit
  1163. },
  1164. [SCI_PHY_SUB_FINAL] = {
  1165. .enter_state = sci_phy_starting_final_substate_enter,
  1166. },
  1167. [SCI_PHY_READY] = {
  1168. .enter_state = sci_phy_ready_state_enter,
  1169. .exit_state = sci_phy_ready_state_exit,
  1170. },
  1171. [SCI_PHY_RESETTING] = {
  1172. .enter_state = sci_phy_resetting_state_enter,
  1173. },
  1174. [SCI_PHY_FINAL] = { },
  1175. };
  1176. void sci_phy_construct(struct isci_phy *iphy,
  1177. struct isci_port *iport, u8 phy_index)
  1178. {
  1179. sci_init_sm(&iphy->sm, sci_phy_state_table, SCI_PHY_INITIAL);
  1180. /* Copy the rest of the input data to our locals */
  1181. iphy->owning_port = iport;
  1182. iphy->phy_index = phy_index;
  1183. iphy->bcn_received_while_port_unassigned = false;
  1184. iphy->protocol = SAS_PROTOCOL_NONE;
  1185. iphy->link_layer_registers = NULL;
  1186. iphy->max_negotiated_speed = SAS_LINK_RATE_UNKNOWN;
  1187. /* Create the SIGNATURE FIS Timeout timer for this phy */
  1188. sci_init_timer(&iphy->sata_timer, phy_sata_timeout);
  1189. }
  1190. void isci_phy_init(struct isci_phy *iphy, struct isci_host *ihost, int index)
  1191. {
  1192. struct sci_oem_params *oem = &ihost->oem_parameters;
  1193. u64 sci_sas_addr;
  1194. __be64 sas_addr;
  1195. sci_sas_addr = oem->phys[index].sas_address.high;
  1196. sci_sas_addr <<= 32;
  1197. sci_sas_addr |= oem->phys[index].sas_address.low;
  1198. sas_addr = cpu_to_be64(sci_sas_addr);
  1199. memcpy(iphy->sas_addr, &sas_addr, sizeof(sas_addr));
  1200. iphy->sas_phy.enabled = 0;
  1201. iphy->sas_phy.id = index;
  1202. iphy->sas_phy.sas_addr = &iphy->sas_addr[0];
  1203. iphy->sas_phy.frame_rcvd = (u8 *)&iphy->frame_rcvd;
  1204. iphy->sas_phy.ha = &ihost->sas_ha;
  1205. iphy->sas_phy.lldd_phy = iphy;
  1206. iphy->sas_phy.enabled = 1;
  1207. iphy->sas_phy.class = SAS;
  1208. iphy->sas_phy.iproto = SAS_PROTOCOL_ALL;
  1209. iphy->sas_phy.tproto = 0;
  1210. iphy->sas_phy.type = PHY_TYPE_PHYSICAL;
  1211. iphy->sas_phy.role = PHY_ROLE_INITIATOR;
  1212. iphy->sas_phy.oob_mode = OOB_NOT_CONNECTED;
  1213. iphy->sas_phy.linkrate = SAS_LINK_RATE_UNKNOWN;
  1214. memset(&iphy->frame_rcvd, 0, sizeof(iphy->frame_rcvd));
  1215. }
  1216. /**
  1217. * isci_phy_control() - This function is one of the SAS Domain Template
  1218. * functions. This is a phy management function.
  1219. * @phy: This parameter specifies the sphy being controlled.
  1220. * @func: This parameter specifies the phy control function being invoked.
  1221. * @buf: This parameter is specific to the phy function being invoked.
  1222. *
  1223. * status, zero indicates success.
  1224. */
  1225. int isci_phy_control(struct asd_sas_phy *sas_phy,
  1226. enum phy_func func,
  1227. void *buf)
  1228. {
  1229. int ret = 0;
  1230. struct isci_phy *iphy = sas_phy->lldd_phy;
  1231. struct asd_sas_port *port = sas_phy->port;
  1232. struct isci_host *ihost = sas_phy->ha->lldd_ha;
  1233. unsigned long flags;
  1234. dev_dbg(&ihost->pdev->dev,
  1235. "%s: phy %p; func %d; buf %p; isci phy %p, port %p\n",
  1236. __func__, sas_phy, func, buf, iphy, port);
  1237. switch (func) {
  1238. case PHY_FUNC_DISABLE:
  1239. spin_lock_irqsave(&ihost->scic_lock, flags);
  1240. scu_link_layer_start_oob(iphy);
  1241. sci_phy_stop(iphy);
  1242. spin_unlock_irqrestore(&ihost->scic_lock, flags);
  1243. break;
  1244. case PHY_FUNC_LINK_RESET:
  1245. spin_lock_irqsave(&ihost->scic_lock, flags);
  1246. scu_link_layer_start_oob(iphy);
  1247. sci_phy_stop(iphy);
  1248. sci_phy_start(iphy);
  1249. spin_unlock_irqrestore(&ihost->scic_lock, flags);
  1250. break;
  1251. case PHY_FUNC_HARD_RESET:
  1252. if (!port)
  1253. return -ENODEV;
  1254. ret = isci_port_perform_hard_reset(ihost, port->lldd_port, iphy);
  1255. break;
  1256. case PHY_FUNC_GET_EVENTS: {
  1257. struct scu_link_layer_registers __iomem *r;
  1258. struct sas_phy *phy = sas_phy->phy;
  1259. r = iphy->link_layer_registers;
  1260. phy->running_disparity_error_count = readl(&r->running_disparity_error_count);
  1261. phy->loss_of_dword_sync_count = readl(&r->loss_of_sync_error_count);
  1262. phy->phy_reset_problem_count = readl(&r->phy_reset_problem_count);
  1263. phy->invalid_dword_count = readl(&r->invalid_dword_counter);
  1264. break;
  1265. }
  1266. default:
  1267. dev_dbg(&ihost->pdev->dev,
  1268. "%s: phy %p; func %d NOT IMPLEMENTED!\n",
  1269. __func__, sas_phy, func);
  1270. ret = -ENOSYS;
  1271. break;
  1272. }
  1273. return ret;
  1274. }