phy.c 47 KB

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