phy.c 46 KB

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