phy.c 40 KB

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