port.c 61 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991
  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 "port.h"
  57. #include "request.h"
  58. #define SCIC_SDS_PORT_HARD_RESET_TIMEOUT (1000)
  59. #define SCU_DUMMY_INDEX (0xFFFF)
  60. static void isci_port_change_state(struct isci_port *iport, enum isci_status status)
  61. {
  62. unsigned long flags;
  63. dev_dbg(&iport->isci_host->pdev->dev,
  64. "%s: iport = %p, state = 0x%x\n",
  65. __func__, iport, status);
  66. /* XXX pointless lock */
  67. spin_lock_irqsave(&iport->state_lock, flags);
  68. iport->status = status;
  69. spin_unlock_irqrestore(&iport->state_lock, flags);
  70. }
  71. /*
  72. * This function will indicate which protocols are supported by this port.
  73. * @sci_port: a handle corresponding to the SAS port for which to return the
  74. * supported protocols.
  75. * @protocols: This parameter specifies a pointer to a data structure
  76. * which the core will copy the protocol values for the port from the
  77. * transmit_identification register.
  78. */
  79. static void
  80. scic_sds_port_get_protocols(struct scic_sds_port *sci_port,
  81. struct scic_phy_proto *protocols)
  82. {
  83. u8 index;
  84. protocols->all = 0;
  85. for (index = 0; index < SCI_MAX_PHYS; index++) {
  86. if (sci_port->phy_table[index] != NULL) {
  87. scic_sds_phy_get_protocols(sci_port->phy_table[index],
  88. protocols);
  89. }
  90. }
  91. }
  92. /**
  93. * This method requests a list (mask) of the phys contained in the supplied SAS
  94. * port.
  95. * @sci_port: a handle corresponding to the SAS port for which to return the
  96. * phy mask.
  97. *
  98. * Return a bit mask indicating which phys are a part of this port. Each bit
  99. * corresponds to a phy identifier (e.g. bit 0 = phy id 0).
  100. */
  101. static u32 scic_sds_port_get_phys(struct scic_sds_port *sci_port)
  102. {
  103. u32 index;
  104. u32 mask;
  105. mask = 0;
  106. for (index = 0; index < SCI_MAX_PHYS; index++) {
  107. if (sci_port->phy_table[index] != NULL) {
  108. mask |= (1 << index);
  109. }
  110. }
  111. return mask;
  112. }
  113. /**
  114. * scic_port_get_properties() - This method simply returns the properties
  115. * regarding the port, such as: physical index, protocols, sas address, etc.
  116. * @port: this parameter specifies the port for which to retrieve the physical
  117. * index.
  118. * @properties: This parameter specifies the properties structure into which to
  119. * copy the requested information.
  120. *
  121. * Indicate if the user specified a valid port. SCI_SUCCESS This value is
  122. * returned if the specified port was valid. SCI_FAILURE_INVALID_PORT This
  123. * value is returned if the specified port is not valid. When this value is
  124. * returned, no data is copied to the properties output parameter.
  125. */
  126. static enum sci_status scic_port_get_properties(struct scic_sds_port *port,
  127. struct scic_port_properties *prop)
  128. {
  129. if ((port == NULL) ||
  130. (port->logical_port_index == SCIC_SDS_DUMMY_PORT))
  131. return SCI_FAILURE_INVALID_PORT;
  132. prop->index = port->logical_port_index;
  133. prop->phy_mask = scic_sds_port_get_phys(port);
  134. scic_sds_port_get_sas_address(port, &prop->local.sas_address);
  135. scic_sds_port_get_protocols(port, &prop->local.protocols);
  136. scic_sds_port_get_attached_sas_address(port, &prop->remote.sas_address);
  137. return SCI_SUCCESS;
  138. }
  139. static void scic_port_bcn_enable(struct scic_sds_port *sci_port)
  140. {
  141. struct scic_sds_phy *sci_phy;
  142. u32 val;
  143. int i;
  144. for (i = 0; i < ARRAY_SIZE(sci_port->phy_table); i++) {
  145. sci_phy = sci_port->phy_table[i];
  146. if (!sci_phy)
  147. continue;
  148. val = readl(&sci_phy->link_layer_registers->link_layer_control);
  149. /* clear the bit by writing 1. */
  150. writel(val, &sci_phy->link_layer_registers->link_layer_control);
  151. }
  152. }
  153. /* called under scic_lock to stabilize phy:port associations */
  154. void isci_port_bcn_enable(struct isci_host *ihost, struct isci_port *iport)
  155. {
  156. int i;
  157. clear_bit(IPORT_BCN_BLOCKED, &iport->flags);
  158. wake_up(&ihost->eventq);
  159. if (!test_and_clear_bit(IPORT_BCN_PENDING, &iport->flags))
  160. return;
  161. for (i = 0; i < ARRAY_SIZE(iport->sci.phy_table); i++) {
  162. struct scic_sds_phy *sci_phy = iport->sci.phy_table[i];
  163. struct isci_phy *iphy = sci_phy_to_iphy(sci_phy);
  164. if (!sci_phy)
  165. continue;
  166. ihost->sas_ha.notify_port_event(&iphy->sas_phy,
  167. PORTE_BROADCAST_RCVD);
  168. break;
  169. }
  170. }
  171. void isci_port_bc_change_received(struct isci_host *ihost,
  172. struct scic_sds_port *sci_port,
  173. struct scic_sds_phy *sci_phy)
  174. {
  175. struct isci_phy *iphy = sci_phy_to_iphy(sci_phy);
  176. struct isci_port *iport = iphy->isci_port;
  177. if (iport && test_bit(IPORT_BCN_BLOCKED, &iport->flags)) {
  178. dev_dbg(&ihost->pdev->dev,
  179. "%s: disabled BCN; isci_phy = %p, sas_phy = %p\n",
  180. __func__, iphy, &iphy->sas_phy);
  181. set_bit(IPORT_BCN_PENDING, &iport->flags);
  182. atomic_inc(&iport->event);
  183. wake_up(&ihost->eventq);
  184. } else {
  185. dev_dbg(&ihost->pdev->dev,
  186. "%s: isci_phy = %p, sas_phy = %p\n",
  187. __func__, iphy, &iphy->sas_phy);
  188. ihost->sas_ha.notify_port_event(&iphy->sas_phy,
  189. PORTE_BROADCAST_RCVD);
  190. }
  191. scic_port_bcn_enable(sci_port);
  192. }
  193. static void isci_port_link_up(struct isci_host *isci_host,
  194. struct scic_sds_port *port,
  195. struct scic_sds_phy *phy)
  196. {
  197. unsigned long flags;
  198. struct scic_port_properties properties;
  199. struct isci_phy *isci_phy = sci_phy_to_iphy(phy);
  200. struct isci_port *isci_port = sci_port_to_iport(port);
  201. unsigned long success = true;
  202. BUG_ON(isci_phy->isci_port != NULL);
  203. isci_phy->isci_port = isci_port;
  204. dev_dbg(&isci_host->pdev->dev,
  205. "%s: isci_port = %p\n",
  206. __func__, isci_port);
  207. spin_lock_irqsave(&isci_phy->sas_phy.frame_rcvd_lock, flags);
  208. isci_port_change_state(isci_phy->isci_port, isci_starting);
  209. scic_port_get_properties(port, &properties);
  210. if (phy->protocol == SCIC_SDS_PHY_PROTOCOL_SATA) {
  211. u64 attached_sas_address;
  212. isci_phy->sas_phy.oob_mode = SATA_OOB_MODE;
  213. isci_phy->sas_phy.frame_rcvd_size = sizeof(struct dev_to_host_fis);
  214. /*
  215. * For direct-attached SATA devices, the SCI core will
  216. * automagically assign a SAS address to the end device
  217. * for the purpose of creating a port. This SAS address
  218. * will not be the same as assigned to the PHY and needs
  219. * to be obtained from struct scic_port_properties properties.
  220. */
  221. attached_sas_address = properties.remote.sas_address.high;
  222. attached_sas_address <<= 32;
  223. attached_sas_address |= properties.remote.sas_address.low;
  224. swab64s(&attached_sas_address);
  225. memcpy(&isci_phy->sas_phy.attached_sas_addr,
  226. &attached_sas_address, sizeof(attached_sas_address));
  227. } else if (phy->protocol == SCIC_SDS_PHY_PROTOCOL_SAS) {
  228. isci_phy->sas_phy.oob_mode = SAS_OOB_MODE;
  229. isci_phy->sas_phy.frame_rcvd_size = sizeof(struct sas_identify_frame);
  230. /* Copy the attached SAS address from the IAF */
  231. memcpy(isci_phy->sas_phy.attached_sas_addr,
  232. isci_phy->frame_rcvd.iaf.sas_addr, SAS_ADDR_SIZE);
  233. } else {
  234. dev_err(&isci_host->pdev->dev, "%s: unkown target\n", __func__);
  235. success = false;
  236. }
  237. isci_phy->sas_phy.phy->negotiated_linkrate = sci_phy_linkrate(phy);
  238. spin_unlock_irqrestore(&isci_phy->sas_phy.frame_rcvd_lock, flags);
  239. /* Notify libsas that we have an address frame, if indeed
  240. * we've found an SSP, SMP, or STP target */
  241. if (success)
  242. isci_host->sas_ha.notify_port_event(&isci_phy->sas_phy,
  243. PORTE_BYTES_DMAED);
  244. }
  245. /**
  246. * isci_port_link_down() - This function is called by the sci core when a link
  247. * becomes inactive.
  248. * @isci_host: This parameter specifies the isci host object.
  249. * @phy: This parameter specifies the isci phy with the active link.
  250. * @port: This parameter specifies the isci port with the active link.
  251. *
  252. */
  253. static void isci_port_link_down(struct isci_host *isci_host,
  254. struct isci_phy *isci_phy,
  255. struct isci_port *isci_port)
  256. {
  257. struct isci_remote_device *isci_device;
  258. dev_dbg(&isci_host->pdev->dev,
  259. "%s: isci_port = %p\n", __func__, isci_port);
  260. if (isci_port) {
  261. /* check to see if this is the last phy on this port. */
  262. if (isci_phy->sas_phy.port &&
  263. isci_phy->sas_phy.port->num_phys == 1) {
  264. atomic_inc(&isci_port->event);
  265. isci_port_bcn_enable(isci_host, isci_port);
  266. /* change the state for all devices on this port. The
  267. * next task sent to this device will be returned as
  268. * SAS_TASK_UNDELIVERED, and the scsi mid layer will
  269. * remove the target
  270. */
  271. list_for_each_entry(isci_device,
  272. &isci_port->remote_dev_list,
  273. node) {
  274. dev_dbg(&isci_host->pdev->dev,
  275. "%s: isci_device = %p\n",
  276. __func__, isci_device);
  277. set_bit(IDEV_GONE, &isci_device->flags);
  278. }
  279. }
  280. isci_port_change_state(isci_port, isci_stopping);
  281. }
  282. /* Notify libsas of the borken link, this will trigger calls to our
  283. * isci_port_deformed and isci_dev_gone functions.
  284. */
  285. sas_phy_disconnected(&isci_phy->sas_phy);
  286. isci_host->sas_ha.notify_phy_event(&isci_phy->sas_phy,
  287. PHYE_LOSS_OF_SIGNAL);
  288. isci_phy->isci_port = NULL;
  289. dev_dbg(&isci_host->pdev->dev,
  290. "%s: isci_port = %p - Done\n", __func__, isci_port);
  291. }
  292. /**
  293. * isci_port_ready() - This function is called by the sci core when a link
  294. * becomes ready.
  295. * @isci_host: This parameter specifies the isci host object.
  296. * @port: This parameter specifies the sci port with the active link.
  297. *
  298. */
  299. static void isci_port_ready(struct isci_host *isci_host, struct isci_port *isci_port)
  300. {
  301. dev_dbg(&isci_host->pdev->dev,
  302. "%s: isci_port = %p\n", __func__, isci_port);
  303. complete_all(&isci_port->start_complete);
  304. isci_port_change_state(isci_port, isci_ready);
  305. return;
  306. }
  307. /**
  308. * isci_port_not_ready() - This function is called by the sci core when a link
  309. * is not ready. All remote devices on this link will be removed if they are
  310. * in the stopping state.
  311. * @isci_host: This parameter specifies the isci host object.
  312. * @port: This parameter specifies the sci port with the active link.
  313. *
  314. */
  315. static void isci_port_not_ready(struct isci_host *isci_host, struct isci_port *isci_port)
  316. {
  317. dev_dbg(&isci_host->pdev->dev,
  318. "%s: isci_port = %p\n", __func__, isci_port);
  319. }
  320. static void isci_port_stop_complete(struct scic_sds_controller *scic,
  321. struct scic_sds_port *sci_port,
  322. enum sci_status completion_status)
  323. {
  324. dev_dbg(&scic_to_ihost(scic)->pdev->dev, "Port stop complete\n");
  325. }
  326. /**
  327. * isci_port_hard_reset_complete() - This function is called by the sci core
  328. * when the hard reset complete notification has been received.
  329. * @port: This parameter specifies the sci port with the active link.
  330. * @completion_status: This parameter specifies the core status for the reset
  331. * process.
  332. *
  333. */
  334. static void isci_port_hard_reset_complete(struct isci_port *isci_port,
  335. enum sci_status completion_status)
  336. {
  337. dev_dbg(&isci_port->isci_host->pdev->dev,
  338. "%s: isci_port = %p, completion_status=%x\n",
  339. __func__, isci_port, completion_status);
  340. /* Save the status of the hard reset from the port. */
  341. isci_port->hard_reset_status = completion_status;
  342. complete_all(&isci_port->hard_reset_complete);
  343. }
  344. /* This method will return a true value if the specified phy can be assigned to
  345. * this port The following is a list of phys for each port that are allowed: -
  346. * Port 0 - 3 2 1 0 - Port 1 - 1 - Port 2 - 3 2 - Port 3 - 3 This method
  347. * doesn't preclude all configurations. It merely ensures that a phy is part
  348. * of the allowable set of phy identifiers for that port. For example, one
  349. * could assign phy 3 to port 0 and no other phys. Please refer to
  350. * scic_sds_port_is_phy_mask_valid() for information regarding whether the
  351. * phy_mask for a port can be supported. bool true if this is a valid phy
  352. * assignment for the port false if this is not a valid phy assignment for the
  353. * port
  354. */
  355. bool scic_sds_port_is_valid_phy_assignment(struct scic_sds_port *sci_port,
  356. u32 phy_index)
  357. {
  358. /* Initialize to invalid value. */
  359. u32 existing_phy_index = SCI_MAX_PHYS;
  360. u32 index;
  361. if ((sci_port->physical_port_index == 1) && (phy_index != 1)) {
  362. return false;
  363. }
  364. if (sci_port->physical_port_index == 3 && phy_index != 3) {
  365. return false;
  366. }
  367. if (
  368. (sci_port->physical_port_index == 2)
  369. && ((phy_index == 0) || (phy_index == 1))
  370. ) {
  371. return false;
  372. }
  373. for (index = 0; index < SCI_MAX_PHYS; index++) {
  374. if ((sci_port->phy_table[index] != NULL)
  375. && (index != phy_index)) {
  376. existing_phy_index = index;
  377. }
  378. }
  379. /*
  380. * Ensure that all of the phys in the port are capable of
  381. * operating at the same maximum link rate. */
  382. if (
  383. (existing_phy_index < SCI_MAX_PHYS)
  384. && (sci_port->owning_controller->user_parameters.sds1.phys[
  385. phy_index].max_speed_generation !=
  386. sci_port->owning_controller->user_parameters.sds1.phys[
  387. existing_phy_index].max_speed_generation)
  388. )
  389. return false;
  390. return true;
  391. }
  392. /**
  393. *
  394. * @sci_port: This is the port object for which to determine if the phy mask
  395. * can be supported.
  396. *
  397. * This method will return a true value if the port's phy mask can be supported
  398. * by the SCU. The following is a list of valid PHY mask configurations for
  399. * each port: - Port 0 - [[3 2] 1] 0 - Port 1 - [1] - Port 2 - [[3] 2]
  400. * - Port 3 - [3] This method returns a boolean indication specifying if the
  401. * phy mask can be supported. true if this is a valid phy assignment for the
  402. * port false if this is not a valid phy assignment for the port
  403. */
  404. static bool scic_sds_port_is_phy_mask_valid(
  405. struct scic_sds_port *sci_port,
  406. u32 phy_mask)
  407. {
  408. if (sci_port->physical_port_index == 0) {
  409. if (((phy_mask & 0x0F) == 0x0F)
  410. || ((phy_mask & 0x03) == 0x03)
  411. || ((phy_mask & 0x01) == 0x01)
  412. || (phy_mask == 0))
  413. return true;
  414. } else if (sci_port->physical_port_index == 1) {
  415. if (((phy_mask & 0x02) == 0x02)
  416. || (phy_mask == 0))
  417. return true;
  418. } else if (sci_port->physical_port_index == 2) {
  419. if (((phy_mask & 0x0C) == 0x0C)
  420. || ((phy_mask & 0x04) == 0x04)
  421. || (phy_mask == 0))
  422. return true;
  423. } else if (sci_port->physical_port_index == 3) {
  424. if (((phy_mask & 0x08) == 0x08)
  425. || (phy_mask == 0))
  426. return true;
  427. }
  428. return false;
  429. }
  430. /**
  431. *
  432. * @sci_port: This parameter specifies the port from which to return a
  433. * connected phy.
  434. *
  435. * This method retrieves a currently active (i.e. connected) phy contained in
  436. * the port. Currently, the lowest order phy that is connected is returned.
  437. * This method returns a pointer to a SCIS_SDS_PHY object. NULL This value is
  438. * returned if there are no currently active (i.e. connected to a remote end
  439. * point) phys contained in the port. All other values specify a struct scic_sds_phy
  440. * object that is active in the port.
  441. */
  442. static struct scic_sds_phy *scic_sds_port_get_a_connected_phy(
  443. struct scic_sds_port *sci_port
  444. ) {
  445. u32 index;
  446. struct scic_sds_phy *phy;
  447. for (index = 0; index < SCI_MAX_PHYS; index++) {
  448. /*
  449. * Ensure that the phy is both part of the port and currently
  450. * connected to the remote end-point. */
  451. phy = sci_port->phy_table[index];
  452. if (
  453. (phy != NULL)
  454. && scic_sds_port_active_phy(sci_port, phy)
  455. ) {
  456. return phy;
  457. }
  458. }
  459. return NULL;
  460. }
  461. /**
  462. * scic_sds_port_set_phy() -
  463. * @out]: port The port object to which the phy assignement is being made.
  464. * @out]: phy The phy which is being assigned to the port.
  465. *
  466. * This method attempts to make the assignment of the phy to the port. If
  467. * successful the phy is assigned to the ports phy table. bool true if the phy
  468. * assignment can be made. false if the phy assignement can not be made. This
  469. * is a functional test that only fails if the phy is currently assigned to a
  470. * different port.
  471. */
  472. static enum sci_status scic_sds_port_set_phy(
  473. struct scic_sds_port *port,
  474. struct scic_sds_phy *phy)
  475. {
  476. /*
  477. * Check to see if we can add this phy to a port
  478. * that means that the phy is not part of a port and that the port does
  479. * not already have a phy assinged to the phy index. */
  480. if (
  481. (port->phy_table[phy->phy_index] == NULL)
  482. && (phy_get_non_dummy_port(phy) == NULL)
  483. && scic_sds_port_is_valid_phy_assignment(port, phy->phy_index)
  484. ) {
  485. /*
  486. * Phy is being added in the stopped state so we are in MPC mode
  487. * make logical port index = physical port index */
  488. port->logical_port_index = port->physical_port_index;
  489. port->phy_table[phy->phy_index] = phy;
  490. scic_sds_phy_set_port(phy, port);
  491. return SCI_SUCCESS;
  492. }
  493. return SCI_FAILURE;
  494. }
  495. /**
  496. * scic_sds_port_clear_phy() -
  497. * @out]: port The port from which the phy is being cleared.
  498. * @out]: phy The phy being cleared from the port.
  499. *
  500. * This method will clear the phy assigned to this port. This method fails if
  501. * this phy is not currently assinged to this port. bool true if the phy is
  502. * removed from the port. false if this phy is not assined to this port.
  503. */
  504. static enum sci_status scic_sds_port_clear_phy(
  505. struct scic_sds_port *port,
  506. struct scic_sds_phy *phy)
  507. {
  508. /* Make sure that this phy is part of this port */
  509. if (port->phy_table[phy->phy_index] == phy &&
  510. phy_get_non_dummy_port(phy) == port) {
  511. struct scic_sds_controller *scic = port->owning_controller;
  512. struct isci_host *ihost = scic_to_ihost(scic);
  513. /* Yep it is assigned to this port so remove it */
  514. scic_sds_phy_set_port(phy, &ihost->ports[SCI_MAX_PORTS].sci);
  515. port->phy_table[phy->phy_index] = NULL;
  516. return SCI_SUCCESS;
  517. }
  518. return SCI_FAILURE;
  519. }
  520. /**
  521. * This method requests the SAS address for the supplied SAS port from the SCI
  522. * implementation.
  523. * @sci_port: a handle corresponding to the SAS port for which to return the
  524. * SAS address.
  525. * @sas_address: This parameter specifies a pointer to a SAS address structure
  526. * into which the core will copy the SAS address for the port.
  527. *
  528. */
  529. void scic_sds_port_get_sas_address(
  530. struct scic_sds_port *sci_port,
  531. struct sci_sas_address *sas_address)
  532. {
  533. u32 index;
  534. sas_address->high = 0;
  535. sas_address->low = 0;
  536. for (index = 0; index < SCI_MAX_PHYS; index++) {
  537. if (sci_port->phy_table[index] != NULL) {
  538. scic_sds_phy_get_sas_address(sci_port->phy_table[index], sas_address);
  539. }
  540. }
  541. }
  542. /*
  543. * This function requests the SAS address for the device directly attached to
  544. * this SAS port.
  545. * @sci_port: a handle corresponding to the SAS port for which to return the
  546. * SAS address.
  547. * @sas_address: This parameter specifies a pointer to a SAS address structure
  548. * into which the core will copy the SAS address for the device directly
  549. * attached to the port.
  550. *
  551. */
  552. void scic_sds_port_get_attached_sas_address(
  553. struct scic_sds_port *sci_port,
  554. struct sci_sas_address *sas_address)
  555. {
  556. struct scic_sds_phy *sci_phy;
  557. /*
  558. * Ensure that the phy is both part of the port and currently
  559. * connected to the remote end-point.
  560. */
  561. sci_phy = scic_sds_port_get_a_connected_phy(sci_port);
  562. if (sci_phy) {
  563. if (sci_phy->protocol != SCIC_SDS_PHY_PROTOCOL_SATA) {
  564. scic_sds_phy_get_attached_sas_address(sci_phy,
  565. sas_address);
  566. } else {
  567. scic_sds_phy_get_sas_address(sci_phy, sas_address);
  568. sas_address->low += sci_phy->phy_index;
  569. }
  570. } else {
  571. sas_address->high = 0;
  572. sas_address->low = 0;
  573. }
  574. }
  575. /**
  576. * scic_sds_port_construct_dummy_rnc() - create dummy rnc for si workaround
  577. *
  578. * @sci_port: logical port on which we need to create the remote node context
  579. * @rni: remote node index for this remote node context.
  580. *
  581. * This routine will construct a dummy remote node context data structure
  582. * This structure will be posted to the hardware to work around a scheduler
  583. * error in the hardware.
  584. */
  585. static void scic_sds_port_construct_dummy_rnc(struct scic_sds_port *sci_port, u16 rni)
  586. {
  587. union scu_remote_node_context *rnc;
  588. rnc = &sci_port->owning_controller->remote_node_context_table[rni];
  589. memset(rnc, 0, sizeof(union scu_remote_node_context));
  590. rnc->ssp.remote_sas_address_hi = 0;
  591. rnc->ssp.remote_sas_address_lo = 0;
  592. rnc->ssp.remote_node_index = rni;
  593. rnc->ssp.remote_node_port_width = 1;
  594. rnc->ssp.logical_port_index = sci_port->physical_port_index;
  595. rnc->ssp.nexus_loss_timer_enable = false;
  596. rnc->ssp.check_bit = false;
  597. rnc->ssp.is_valid = true;
  598. rnc->ssp.is_remote_node_context = true;
  599. rnc->ssp.function_number = 0;
  600. rnc->ssp.arbitration_wait_time = 0;
  601. }
  602. /*
  603. * construct a dummy task context data structure. This
  604. * structure will be posted to the hardwre to work around a scheduler error
  605. * in the hardware.
  606. */
  607. static void scic_sds_port_construct_dummy_task(struct scic_sds_port *sci_port, u16 tag)
  608. {
  609. struct scu_task_context *task_context;
  610. task_context = scic_sds_controller_get_task_context_buffer(sci_port->owning_controller, tag);
  611. memset(task_context, 0, sizeof(struct scu_task_context));
  612. task_context->abort = 0;
  613. task_context->priority = 0;
  614. task_context->initiator_request = 1;
  615. task_context->connection_rate = 1;
  616. task_context->protocol_engine_index = 0;
  617. task_context->logical_port_index = sci_port->physical_port_index;
  618. task_context->protocol_type = SCU_TASK_CONTEXT_PROTOCOL_SSP;
  619. task_context->task_index = ISCI_TAG_TCI(tag);
  620. task_context->valid = SCU_TASK_CONTEXT_VALID;
  621. task_context->context_type = SCU_TASK_CONTEXT_TYPE;
  622. task_context->remote_node_index = sci_port->reserved_rni;
  623. task_context->command_code = 0;
  624. task_context->link_layer_control = 0;
  625. task_context->do_not_dma_ssp_good_response = 1;
  626. task_context->strict_ordering = 0;
  627. task_context->control_frame = 0;
  628. task_context->timeout_enable = 0;
  629. task_context->block_guard_enable = 0;
  630. task_context->address_modifier = 0;
  631. task_context->task_phase = 0x01;
  632. }
  633. static void scic_sds_port_destroy_dummy_resources(struct scic_sds_port *sci_port)
  634. {
  635. struct scic_sds_controller *scic = sci_port->owning_controller;
  636. if (sci_port->reserved_tci != SCU_DUMMY_INDEX)
  637. scic_controller_free_io_tag(scic, sci_port->reserved_tci);
  638. if (sci_port->reserved_rni != SCU_DUMMY_INDEX)
  639. scic_sds_remote_node_table_release_remote_node_index(&scic->available_remote_nodes,
  640. 1, sci_port->reserved_rni);
  641. sci_port->reserved_rni = SCU_DUMMY_INDEX;
  642. sci_port->reserved_tci = SCU_DUMMY_INDEX;
  643. }
  644. /**
  645. * This method performs initialization of the supplied port. Initialization
  646. * includes: - state machine initialization - member variable initialization
  647. * - configuring the phy_mask
  648. * @sci_port:
  649. * @transport_layer_registers:
  650. * @port_task_scheduler_registers:
  651. * @port_configuration_regsiter:
  652. *
  653. * enum sci_status SCI_FAILURE_UNSUPPORTED_PORT_CONFIGURATION This value is returned
  654. * if the phy being added to the port
  655. */
  656. enum sci_status scic_sds_port_initialize(
  657. struct scic_sds_port *sci_port,
  658. void __iomem *port_task_scheduler_registers,
  659. void __iomem *port_configuration_regsiter,
  660. void __iomem *viit_registers)
  661. {
  662. sci_port->port_task_scheduler_registers = port_task_scheduler_registers;
  663. sci_port->port_pe_configuration_register = port_configuration_regsiter;
  664. sci_port->viit_registers = viit_registers;
  665. return SCI_SUCCESS;
  666. }
  667. /**
  668. * This method assigns the direct attached device ID for this port.
  669. *
  670. * @param[in] sci_port The port for which the direct attached device id is to
  671. * be assigned.
  672. * @param[in] device_id The direct attached device ID to assign to the port.
  673. * This will be the RNi for the device
  674. */
  675. void scic_sds_port_setup_transports(
  676. struct scic_sds_port *sci_port,
  677. u32 device_id)
  678. {
  679. u8 index;
  680. for (index = 0; index < SCI_MAX_PHYS; index++) {
  681. if (sci_port->active_phy_mask & (1 << index))
  682. scic_sds_phy_setup_transport(sci_port->phy_table[index], device_id);
  683. }
  684. }
  685. /**
  686. *
  687. * @sci_port: This is the port on which the phy should be enabled.
  688. * @sci_phy: This is the specific phy which to enable.
  689. * @do_notify_user: This parameter specifies whether to inform the user (via
  690. * scic_cb_port_link_up()) as to the fact that a new phy as become ready.
  691. *
  692. * This function will activate the phy in the port.
  693. * Activation includes: - adding
  694. * the phy to the port - enabling the Protocol Engine in the silicon. -
  695. * notifying the user that the link is up. none
  696. */
  697. static void scic_sds_port_activate_phy(struct scic_sds_port *sci_port,
  698. struct scic_sds_phy *sci_phy,
  699. bool do_notify_user)
  700. {
  701. struct scic_sds_controller *scic = sci_port->owning_controller;
  702. struct isci_host *ihost = scic_to_ihost(scic);
  703. if (sci_phy->protocol != SCIC_SDS_PHY_PROTOCOL_SATA)
  704. scic_sds_phy_resume(sci_phy);
  705. sci_port->active_phy_mask |= 1 << sci_phy->phy_index;
  706. scic_sds_controller_clear_invalid_phy(scic, sci_phy);
  707. if (do_notify_user == true)
  708. isci_port_link_up(ihost, sci_port, sci_phy);
  709. }
  710. void scic_sds_port_deactivate_phy(struct scic_sds_port *sci_port,
  711. struct scic_sds_phy *sci_phy,
  712. bool do_notify_user)
  713. {
  714. struct scic_sds_controller *scic = scic_sds_port_get_controller(sci_port);
  715. struct isci_port *iport = sci_port_to_iport(sci_port);
  716. struct isci_host *ihost = scic_to_ihost(scic);
  717. struct isci_phy *iphy = sci_phy_to_iphy(sci_phy);
  718. sci_port->active_phy_mask &= ~(1 << sci_phy->phy_index);
  719. sci_phy->max_negotiated_speed = SAS_LINK_RATE_UNKNOWN;
  720. /* Re-assign the phy back to the LP as if it were a narrow port */
  721. writel(sci_phy->phy_index,
  722. &sci_port->port_pe_configuration_register[sci_phy->phy_index]);
  723. if (do_notify_user == true)
  724. isci_port_link_down(ihost, iphy, iport);
  725. }
  726. /**
  727. *
  728. * @sci_port: This is the port on which the phy should be disabled.
  729. * @sci_phy: This is the specific phy which to disabled.
  730. *
  731. * This function will disable the phy and report that the phy is not valid for
  732. * this port object. None
  733. */
  734. static void scic_sds_port_invalid_link_up(struct scic_sds_port *sci_port,
  735. struct scic_sds_phy *sci_phy)
  736. {
  737. struct scic_sds_controller *scic = sci_port->owning_controller;
  738. /*
  739. * Check to see if we have alreay reported this link as bad and if
  740. * not go ahead and tell the SCI_USER that we have discovered an
  741. * invalid link.
  742. */
  743. if ((scic->invalid_phy_mask & (1 << sci_phy->phy_index)) == 0) {
  744. scic_sds_controller_set_invalid_phy(scic, sci_phy);
  745. dev_warn(&scic_to_ihost(scic)->pdev->dev, "Invalid link up!\n");
  746. }
  747. }
  748. static bool is_port_ready_state(enum scic_sds_port_states state)
  749. {
  750. switch (state) {
  751. case SCI_PORT_READY:
  752. case SCI_PORT_SUB_WAITING:
  753. case SCI_PORT_SUB_OPERATIONAL:
  754. case SCI_PORT_SUB_CONFIGURING:
  755. return true;
  756. default:
  757. return false;
  758. }
  759. }
  760. /* flag dummy rnc hanling when exiting a ready state */
  761. static void port_state_machine_change(struct scic_sds_port *sci_port,
  762. enum scic_sds_port_states state)
  763. {
  764. struct sci_base_state_machine *sm = &sci_port->sm;
  765. enum scic_sds_port_states old_state = sm->current_state_id;
  766. if (is_port_ready_state(old_state) && !is_port_ready_state(state))
  767. sci_port->ready_exit = true;
  768. sci_change_state(sm, state);
  769. sci_port->ready_exit = false;
  770. }
  771. /**
  772. * scic_sds_port_general_link_up_handler - phy can be assigned to port?
  773. * @sci_port: scic_sds_port object for which has a phy that has gone link up.
  774. * @sci_phy: This is the struct scic_sds_phy object that has gone link up.
  775. * @do_notify_user: This parameter specifies whether to inform the user (via
  776. * scic_cb_port_link_up()) as to the fact that a new phy as become ready.
  777. *
  778. * Determine if this phy can be assigned to this
  779. * port . If the phy is not a valid PHY for
  780. * this port then the function will notify the user. A PHY can only be
  781. * part of a port if it's attached SAS ADDRESS is the same as all other PHYs in
  782. * the same port. none
  783. */
  784. static void scic_sds_port_general_link_up_handler(struct scic_sds_port *sci_port,
  785. struct scic_sds_phy *sci_phy,
  786. bool do_notify_user)
  787. {
  788. struct sci_sas_address port_sas_address;
  789. struct sci_sas_address phy_sas_address;
  790. scic_sds_port_get_attached_sas_address(sci_port, &port_sas_address);
  791. scic_sds_phy_get_attached_sas_address(sci_phy, &phy_sas_address);
  792. /* If the SAS address of the new phy matches the SAS address of
  793. * other phys in the port OR this is the first phy in the port,
  794. * then activate the phy and allow it to be used for operations
  795. * in this port.
  796. */
  797. if ((phy_sas_address.high == port_sas_address.high &&
  798. phy_sas_address.low == port_sas_address.low) ||
  799. sci_port->active_phy_mask == 0) {
  800. struct sci_base_state_machine *sm = &sci_port->sm;
  801. scic_sds_port_activate_phy(sci_port, sci_phy, do_notify_user);
  802. if (sm->current_state_id == SCI_PORT_RESETTING)
  803. port_state_machine_change(sci_port, SCI_PORT_READY);
  804. } else
  805. scic_sds_port_invalid_link_up(sci_port, sci_phy);
  806. }
  807. /**
  808. * This method returns false if the port only has a single phy object assigned.
  809. * If there are no phys or more than one phy then the method will return
  810. * true.
  811. * @sci_port: The port for which the wide port condition is to be checked.
  812. *
  813. * bool true Is returned if this is a wide ported port. false Is returned if
  814. * this is a narrow port.
  815. */
  816. static bool scic_sds_port_is_wide(struct scic_sds_port *sci_port)
  817. {
  818. u32 index;
  819. u32 phy_count = 0;
  820. for (index = 0; index < SCI_MAX_PHYS; index++) {
  821. if (sci_port->phy_table[index] != NULL) {
  822. phy_count++;
  823. }
  824. }
  825. return phy_count != 1;
  826. }
  827. /**
  828. * This method is called by the PHY object when the link is detected. if the
  829. * port wants the PHY to continue on to the link up state then the port
  830. * layer must return true. If the port object returns false the phy object
  831. * must halt its attempt to go link up.
  832. * @sci_port: The port associated with the phy object.
  833. * @sci_phy: The phy object that is trying to go link up.
  834. *
  835. * true if the phy object can continue to the link up condition. true Is
  836. * returned if this phy can continue to the ready state. false Is returned if
  837. * can not continue on to the ready state. This notification is in place for
  838. * wide ports and direct attached phys. Since there are no wide ported SATA
  839. * devices this could become an invalid port configuration.
  840. */
  841. bool scic_sds_port_link_detected(
  842. struct scic_sds_port *sci_port,
  843. struct scic_sds_phy *sci_phy)
  844. {
  845. if ((sci_port->logical_port_index != SCIC_SDS_DUMMY_PORT) &&
  846. (sci_phy->protocol == SCIC_SDS_PHY_PROTOCOL_SATA) &&
  847. scic_sds_port_is_wide(sci_port)) {
  848. scic_sds_port_invalid_link_up(sci_port, sci_phy);
  849. return false;
  850. }
  851. return true;
  852. }
  853. static void port_timeout(unsigned long data)
  854. {
  855. struct sci_timer *tmr = (struct sci_timer *)data;
  856. struct scic_sds_port *sci_port = container_of(tmr, typeof(*sci_port), timer);
  857. struct isci_host *ihost = scic_to_ihost(sci_port->owning_controller);
  858. unsigned long flags;
  859. u32 current_state;
  860. spin_lock_irqsave(&ihost->scic_lock, flags);
  861. if (tmr->cancel)
  862. goto done;
  863. current_state = sci_port->sm.current_state_id;
  864. if (current_state == SCI_PORT_RESETTING) {
  865. /* if the port is still in the resetting state then the timeout
  866. * fired before the reset completed.
  867. */
  868. port_state_machine_change(sci_port, SCI_PORT_FAILED);
  869. } else if (current_state == SCI_PORT_STOPPED) {
  870. /* if the port is stopped then the start request failed In this
  871. * case stay in the stopped state.
  872. */
  873. dev_err(sciport_to_dev(sci_port),
  874. "%s: SCIC Port 0x%p failed to stop before tiemout.\n",
  875. __func__,
  876. sci_port);
  877. } else if (current_state == SCI_PORT_STOPPING) {
  878. /* if the port is still stopping then the stop has not completed */
  879. isci_port_stop_complete(sci_port->owning_controller,
  880. sci_port,
  881. SCI_FAILURE_TIMEOUT);
  882. } else {
  883. /* The port is in the ready state and we have a timer
  884. * reporting a timeout this should not happen.
  885. */
  886. dev_err(sciport_to_dev(sci_port),
  887. "%s: SCIC Port 0x%p is processing a timeout operation "
  888. "in state %d.\n", __func__, sci_port, current_state);
  889. }
  890. done:
  891. spin_unlock_irqrestore(&ihost->scic_lock, flags);
  892. }
  893. /* --------------------------------------------------------------------------- */
  894. /**
  895. * This function updates the hardwares VIIT entry for this port.
  896. *
  897. *
  898. */
  899. static void scic_sds_port_update_viit_entry(struct scic_sds_port *sci_port)
  900. {
  901. struct sci_sas_address sas_address;
  902. scic_sds_port_get_sas_address(sci_port, &sas_address);
  903. writel(sas_address.high,
  904. &sci_port->viit_registers->initiator_sas_address_hi);
  905. writel(sas_address.low,
  906. &sci_port->viit_registers->initiator_sas_address_lo);
  907. /* This value get cleared just in case its not already cleared */
  908. writel(0, &sci_port->viit_registers->reserved);
  909. /* We are required to update the status register last */
  910. writel(SCU_VIIT_ENTRY_ID_VIIT |
  911. SCU_VIIT_IPPT_INITIATOR |
  912. ((1 << sci_port->physical_port_index) << SCU_VIIT_ENTRY_LPVIE_SHIFT) |
  913. SCU_VIIT_STATUS_ALL_VALID,
  914. &sci_port->viit_registers->status);
  915. }
  916. /**
  917. * This method returns the maximum allowed speed for data transfers on this
  918. * port. This maximum allowed speed evaluates to the maximum speed of the
  919. * slowest phy in the port.
  920. * @sci_port: This parameter specifies the port for which to retrieve the
  921. * maximum allowed speed.
  922. *
  923. * This method returns the maximum negotiated speed of the slowest phy in the
  924. * port.
  925. */
  926. enum sas_linkrate scic_sds_port_get_max_allowed_speed(
  927. struct scic_sds_port *sci_port)
  928. {
  929. u16 index;
  930. enum sas_linkrate max_allowed_speed = SAS_LINK_RATE_6_0_GBPS;
  931. struct scic_sds_phy *phy = NULL;
  932. /*
  933. * Loop through all of the phys in this port and find the phy with the
  934. * lowest maximum link rate. */
  935. for (index = 0; index < SCI_MAX_PHYS; index++) {
  936. phy = sci_port->phy_table[index];
  937. if (
  938. (phy != NULL)
  939. && (scic_sds_port_active_phy(sci_port, phy) == true)
  940. && (phy->max_negotiated_speed < max_allowed_speed)
  941. )
  942. max_allowed_speed = phy->max_negotiated_speed;
  943. }
  944. return max_allowed_speed;
  945. }
  946. /**
  947. *
  948. * @sci_port: This is the struct scic_sds_port object to suspend.
  949. *
  950. * This method will susped the port task scheduler for this port object. none
  951. */
  952. static void
  953. scic_sds_port_suspend_port_task_scheduler(struct scic_sds_port *port)
  954. {
  955. u32 pts_control_value;
  956. pts_control_value = readl(&port->port_task_scheduler_registers->control);
  957. pts_control_value |= SCU_PTSxCR_GEN_BIT(SUSPEND);
  958. writel(pts_control_value, &port->port_task_scheduler_registers->control);
  959. }
  960. /**
  961. * scic_sds_port_post_dummy_request() - post dummy/workaround request
  962. * @sci_port: port to post task
  963. *
  964. * Prevent the hardware scheduler from posting new requests to the front
  965. * of the scheduler queue causing a starvation problem for currently
  966. * ongoing requests.
  967. *
  968. */
  969. static void scic_sds_port_post_dummy_request(struct scic_sds_port *sci_port)
  970. {
  971. u32 command;
  972. struct scu_task_context *task_context;
  973. struct scic_sds_controller *scic = sci_port->owning_controller;
  974. u16 tci = sci_port->reserved_tci;
  975. task_context = scic_sds_controller_get_task_context_buffer(scic, tci);
  976. task_context->abort = 0;
  977. command = SCU_CONTEXT_COMMAND_REQUEST_TYPE_POST_TC |
  978. sci_port->physical_port_index << SCU_CONTEXT_COMMAND_LOGICAL_PORT_SHIFT |
  979. tci;
  980. scic_sds_controller_post_request(scic, command);
  981. }
  982. /**
  983. * This routine will abort the dummy request. This will alow the hardware to
  984. * power down parts of the silicon to save power.
  985. *
  986. * @sci_port: The port on which the task must be aborted.
  987. *
  988. */
  989. static void scic_sds_port_abort_dummy_request(struct scic_sds_port *sci_port)
  990. {
  991. struct scic_sds_controller *scic = sci_port->owning_controller;
  992. u16 tci = sci_port->reserved_tci;
  993. struct scu_task_context *tc;
  994. u32 command;
  995. tc = scic_sds_controller_get_task_context_buffer(scic, tci);
  996. tc->abort = 1;
  997. command = SCU_CONTEXT_COMMAND_REQUEST_POST_TC_ABORT |
  998. sci_port->physical_port_index << SCU_CONTEXT_COMMAND_LOGICAL_PORT_SHIFT |
  999. tci;
  1000. scic_sds_controller_post_request(scic, command);
  1001. }
  1002. /**
  1003. *
  1004. * @sci_port: This is the struct scic_sds_port object to resume.
  1005. *
  1006. * This method will resume the port task scheduler for this port object. none
  1007. */
  1008. static void
  1009. scic_sds_port_resume_port_task_scheduler(struct scic_sds_port *port)
  1010. {
  1011. u32 pts_control_value;
  1012. pts_control_value = readl(&port->port_task_scheduler_registers->control);
  1013. pts_control_value &= ~SCU_PTSxCR_GEN_BIT(SUSPEND);
  1014. writel(pts_control_value, &port->port_task_scheduler_registers->control);
  1015. }
  1016. static void scic_sds_port_ready_substate_waiting_enter(struct sci_base_state_machine *sm)
  1017. {
  1018. struct scic_sds_port *sci_port = container_of(sm, typeof(*sci_port), sm);
  1019. scic_sds_port_suspend_port_task_scheduler(sci_port);
  1020. sci_port->not_ready_reason = SCIC_PORT_NOT_READY_NO_ACTIVE_PHYS;
  1021. if (sci_port->active_phy_mask != 0) {
  1022. /* At least one of the phys on the port is ready */
  1023. port_state_machine_change(sci_port,
  1024. SCI_PORT_SUB_OPERATIONAL);
  1025. }
  1026. }
  1027. static void scic_sds_port_ready_substate_operational_enter(struct sci_base_state_machine *sm)
  1028. {
  1029. u32 index;
  1030. struct scic_sds_port *sci_port = container_of(sm, typeof(*sci_port), sm);
  1031. struct scic_sds_controller *scic = sci_port->owning_controller;
  1032. struct isci_host *ihost = scic_to_ihost(scic);
  1033. struct isci_port *iport = sci_port_to_iport(sci_port);
  1034. isci_port_ready(ihost, iport);
  1035. for (index = 0; index < SCI_MAX_PHYS; index++) {
  1036. if (sci_port->phy_table[index]) {
  1037. writel(sci_port->physical_port_index,
  1038. &sci_port->port_pe_configuration_register[
  1039. sci_port->phy_table[index]->phy_index]);
  1040. }
  1041. }
  1042. scic_sds_port_update_viit_entry(sci_port);
  1043. scic_sds_port_resume_port_task_scheduler(sci_port);
  1044. /*
  1045. * Post the dummy task for the port so the hardware can schedule
  1046. * io correctly
  1047. */
  1048. scic_sds_port_post_dummy_request(sci_port);
  1049. }
  1050. static void scic_sds_port_invalidate_dummy_remote_node(struct scic_sds_port *sci_port)
  1051. {
  1052. struct scic_sds_controller *scic = sci_port->owning_controller;
  1053. u8 phys_index = sci_port->physical_port_index;
  1054. union scu_remote_node_context *rnc;
  1055. u16 rni = sci_port->reserved_rni;
  1056. u32 command;
  1057. rnc = &scic->remote_node_context_table[rni];
  1058. rnc->ssp.is_valid = false;
  1059. /* ensure the preceding tc abort request has reached the
  1060. * controller and give it ample time to act before posting the rnc
  1061. * invalidate
  1062. */
  1063. readl(&scic->smu_registers->interrupt_status); /* flush */
  1064. udelay(10);
  1065. command = SCU_CONTEXT_COMMAND_POST_RNC_INVALIDATE |
  1066. phys_index << SCU_CONTEXT_COMMAND_LOGICAL_PORT_SHIFT | rni;
  1067. scic_sds_controller_post_request(scic, command);
  1068. }
  1069. /**
  1070. *
  1071. * @object: This is the object which is cast to a struct scic_sds_port object.
  1072. *
  1073. * This method will perform the actions required by the struct scic_sds_port on
  1074. * exiting the SCI_PORT_SUB_OPERATIONAL. This function reports
  1075. * the port not ready and suspends the port task scheduler. none
  1076. */
  1077. static void scic_sds_port_ready_substate_operational_exit(struct sci_base_state_machine *sm)
  1078. {
  1079. struct scic_sds_port *sci_port = container_of(sm, typeof(*sci_port), sm);
  1080. struct scic_sds_controller *scic = sci_port->owning_controller;
  1081. struct isci_host *ihost = scic_to_ihost(scic);
  1082. struct isci_port *iport = sci_port_to_iport(sci_port);
  1083. /*
  1084. * Kill the dummy task for this port if it has not yet posted
  1085. * the hardware will treat this as a NOP and just return abort
  1086. * complete.
  1087. */
  1088. scic_sds_port_abort_dummy_request(sci_port);
  1089. isci_port_not_ready(ihost, iport);
  1090. if (sci_port->ready_exit)
  1091. scic_sds_port_invalidate_dummy_remote_node(sci_port);
  1092. }
  1093. static void scic_sds_port_ready_substate_configuring_enter(struct sci_base_state_machine *sm)
  1094. {
  1095. struct scic_sds_port *sci_port = container_of(sm, typeof(*sci_port), sm);
  1096. struct scic_sds_controller *scic = sci_port->owning_controller;
  1097. struct isci_host *ihost = scic_to_ihost(scic);
  1098. struct isci_port *iport = sci_port_to_iport(sci_port);
  1099. if (sci_port->active_phy_mask == 0) {
  1100. isci_port_not_ready(ihost, iport);
  1101. port_state_machine_change(sci_port,
  1102. SCI_PORT_SUB_WAITING);
  1103. } else if (sci_port->started_request_count == 0)
  1104. port_state_machine_change(sci_port,
  1105. SCI_PORT_SUB_OPERATIONAL);
  1106. }
  1107. static void scic_sds_port_ready_substate_configuring_exit(struct sci_base_state_machine *sm)
  1108. {
  1109. struct scic_sds_port *sci_port = container_of(sm, typeof(*sci_port), sm);
  1110. scic_sds_port_suspend_port_task_scheduler(sci_port);
  1111. if (sci_port->ready_exit)
  1112. scic_sds_port_invalidate_dummy_remote_node(sci_port);
  1113. }
  1114. enum sci_status scic_sds_port_start(struct scic_sds_port *sci_port)
  1115. {
  1116. struct scic_sds_controller *scic = sci_port->owning_controller;
  1117. enum sci_status status = SCI_SUCCESS;
  1118. enum scic_sds_port_states state;
  1119. u32 phy_mask;
  1120. state = sci_port->sm.current_state_id;
  1121. if (state != SCI_PORT_STOPPED) {
  1122. dev_warn(sciport_to_dev(sci_port),
  1123. "%s: in wrong state: %d\n", __func__, state);
  1124. return SCI_FAILURE_INVALID_STATE;
  1125. }
  1126. if (sci_port->assigned_device_count > 0) {
  1127. /* TODO This is a start failure operation because
  1128. * there are still devices assigned to this port.
  1129. * There must be no devices assigned to a port on a
  1130. * start operation.
  1131. */
  1132. return SCI_FAILURE_UNSUPPORTED_PORT_CONFIGURATION;
  1133. }
  1134. if (sci_port->reserved_rni == SCU_DUMMY_INDEX) {
  1135. u16 rni = scic_sds_remote_node_table_allocate_remote_node(
  1136. &scic->available_remote_nodes, 1);
  1137. if (rni != SCU_DUMMY_INDEX)
  1138. scic_sds_port_construct_dummy_rnc(sci_port, rni);
  1139. else
  1140. status = SCI_FAILURE_INSUFFICIENT_RESOURCES;
  1141. sci_port->reserved_rni = rni;
  1142. }
  1143. if (sci_port->reserved_tci == SCU_DUMMY_INDEX) {
  1144. /* Allocate a TCI and remove the sequence nibble */
  1145. u16 tci = scic_controller_allocate_io_tag(scic);
  1146. if (tci != SCU_DUMMY_INDEX)
  1147. scic_sds_port_construct_dummy_task(sci_port, tci);
  1148. else
  1149. status = SCI_FAILURE_INSUFFICIENT_RESOURCES;
  1150. sci_port->reserved_tci = tci;
  1151. }
  1152. if (status == SCI_SUCCESS) {
  1153. phy_mask = scic_sds_port_get_phys(sci_port);
  1154. /*
  1155. * There are one or more phys assigned to this port. Make sure
  1156. * the port's phy mask is in fact legal and supported by the
  1157. * silicon.
  1158. */
  1159. if (scic_sds_port_is_phy_mask_valid(sci_port, phy_mask) == true) {
  1160. port_state_machine_change(sci_port,
  1161. SCI_PORT_READY);
  1162. return SCI_SUCCESS;
  1163. }
  1164. status = SCI_FAILURE;
  1165. }
  1166. if (status != SCI_SUCCESS)
  1167. scic_sds_port_destroy_dummy_resources(sci_port);
  1168. return status;
  1169. }
  1170. enum sci_status scic_sds_port_stop(struct scic_sds_port *sci_port)
  1171. {
  1172. enum scic_sds_port_states state;
  1173. state = sci_port->sm.current_state_id;
  1174. switch (state) {
  1175. case SCI_PORT_STOPPED:
  1176. return SCI_SUCCESS;
  1177. case SCI_PORT_SUB_WAITING:
  1178. case SCI_PORT_SUB_OPERATIONAL:
  1179. case SCI_PORT_SUB_CONFIGURING:
  1180. case SCI_PORT_RESETTING:
  1181. port_state_machine_change(sci_port,
  1182. SCI_PORT_STOPPING);
  1183. return SCI_SUCCESS;
  1184. default:
  1185. dev_warn(sciport_to_dev(sci_port),
  1186. "%s: in wrong state: %d\n", __func__, state);
  1187. return SCI_FAILURE_INVALID_STATE;
  1188. }
  1189. }
  1190. static enum sci_status scic_port_hard_reset(struct scic_sds_port *sci_port, u32 timeout)
  1191. {
  1192. enum sci_status status = SCI_FAILURE_INVALID_PHY;
  1193. struct scic_sds_phy *selected_phy = NULL;
  1194. enum scic_sds_port_states state;
  1195. u32 phy_index;
  1196. state = sci_port->sm.current_state_id;
  1197. if (state != SCI_PORT_SUB_OPERATIONAL) {
  1198. dev_warn(sciport_to_dev(sci_port),
  1199. "%s: in wrong state: %d\n", __func__, state);
  1200. return SCI_FAILURE_INVALID_STATE;
  1201. }
  1202. /* Select a phy on which we can send the hard reset request. */
  1203. for (phy_index = 0; phy_index < SCI_MAX_PHYS && !selected_phy; phy_index++) {
  1204. selected_phy = sci_port->phy_table[phy_index];
  1205. if (selected_phy &&
  1206. !scic_sds_port_active_phy(sci_port, selected_phy)) {
  1207. /*
  1208. * We found a phy but it is not ready select
  1209. * different phy
  1210. */
  1211. selected_phy = NULL;
  1212. }
  1213. }
  1214. /* If we have a phy then go ahead and start the reset procedure */
  1215. if (!selected_phy)
  1216. return status;
  1217. status = scic_sds_phy_reset(selected_phy);
  1218. if (status != SCI_SUCCESS)
  1219. return status;
  1220. sci_mod_timer(&sci_port->timer, timeout);
  1221. sci_port->not_ready_reason = SCIC_PORT_NOT_READY_HARD_RESET_REQUESTED;
  1222. port_state_machine_change(sci_port,
  1223. SCI_PORT_RESETTING);
  1224. return SCI_SUCCESS;
  1225. }
  1226. /**
  1227. * scic_sds_port_add_phy() -
  1228. * @sci_port: This parameter specifies the port in which the phy will be added.
  1229. * @sci_phy: This parameter is the phy which is to be added to the port.
  1230. *
  1231. * This method will add a PHY to the selected port. This method returns an
  1232. * enum sci_status. SCI_SUCCESS the phy has been added to the port. Any other
  1233. * status is a failure to add the phy to the port.
  1234. */
  1235. enum sci_status scic_sds_port_add_phy(struct scic_sds_port *sci_port,
  1236. struct scic_sds_phy *sci_phy)
  1237. {
  1238. enum sci_status status;
  1239. enum scic_sds_port_states state;
  1240. state = sci_port->sm.current_state_id;
  1241. switch (state) {
  1242. case SCI_PORT_STOPPED: {
  1243. struct sci_sas_address port_sas_address;
  1244. /* Read the port assigned SAS Address if there is one */
  1245. scic_sds_port_get_sas_address(sci_port, &port_sas_address);
  1246. if (port_sas_address.high != 0 && port_sas_address.low != 0) {
  1247. struct sci_sas_address phy_sas_address;
  1248. /* Make sure that the PHY SAS Address matches the SAS Address
  1249. * for this port
  1250. */
  1251. scic_sds_phy_get_sas_address(sci_phy, &phy_sas_address);
  1252. if (port_sas_address.high != phy_sas_address.high ||
  1253. port_sas_address.low != phy_sas_address.low)
  1254. return SCI_FAILURE_UNSUPPORTED_PORT_CONFIGURATION;
  1255. }
  1256. return scic_sds_port_set_phy(sci_port, sci_phy);
  1257. }
  1258. case SCI_PORT_SUB_WAITING:
  1259. case SCI_PORT_SUB_OPERATIONAL:
  1260. status = scic_sds_port_set_phy(sci_port, sci_phy);
  1261. if (status != SCI_SUCCESS)
  1262. return status;
  1263. scic_sds_port_general_link_up_handler(sci_port, sci_phy, true);
  1264. sci_port->not_ready_reason = SCIC_PORT_NOT_READY_RECONFIGURING;
  1265. port_state_machine_change(sci_port, SCI_PORT_SUB_CONFIGURING);
  1266. return status;
  1267. case SCI_PORT_SUB_CONFIGURING:
  1268. status = scic_sds_port_set_phy(sci_port, sci_phy);
  1269. if (status != SCI_SUCCESS)
  1270. return status;
  1271. scic_sds_port_general_link_up_handler(sci_port, sci_phy, true);
  1272. /* Re-enter the configuring state since this may be the last phy in
  1273. * the port.
  1274. */
  1275. port_state_machine_change(sci_port,
  1276. SCI_PORT_SUB_CONFIGURING);
  1277. return SCI_SUCCESS;
  1278. default:
  1279. dev_warn(sciport_to_dev(sci_port),
  1280. "%s: in wrong state: %d\n", __func__, state);
  1281. return SCI_FAILURE_INVALID_STATE;
  1282. }
  1283. }
  1284. /**
  1285. * scic_sds_port_remove_phy() -
  1286. * @sci_port: This parameter specifies the port in which the phy will be added.
  1287. * @sci_phy: This parameter is the phy which is to be added to the port.
  1288. *
  1289. * This method will remove the PHY from the selected PORT. This method returns
  1290. * an enum sci_status. SCI_SUCCESS the phy has been removed from the port. Any
  1291. * other status is a failure to add the phy to the port.
  1292. */
  1293. enum sci_status scic_sds_port_remove_phy(struct scic_sds_port *sci_port,
  1294. struct scic_sds_phy *sci_phy)
  1295. {
  1296. enum sci_status status;
  1297. enum scic_sds_port_states state;
  1298. state = sci_port->sm.current_state_id;
  1299. switch (state) {
  1300. case SCI_PORT_STOPPED:
  1301. return scic_sds_port_clear_phy(sci_port, sci_phy);
  1302. case SCI_PORT_SUB_OPERATIONAL:
  1303. status = scic_sds_port_clear_phy(sci_port, sci_phy);
  1304. if (status != SCI_SUCCESS)
  1305. return status;
  1306. scic_sds_port_deactivate_phy(sci_port, sci_phy, true);
  1307. sci_port->not_ready_reason = SCIC_PORT_NOT_READY_RECONFIGURING;
  1308. port_state_machine_change(sci_port,
  1309. SCI_PORT_SUB_CONFIGURING);
  1310. return SCI_SUCCESS;
  1311. case SCI_PORT_SUB_CONFIGURING:
  1312. status = scic_sds_port_clear_phy(sci_port, sci_phy);
  1313. if (status != SCI_SUCCESS)
  1314. return status;
  1315. scic_sds_port_deactivate_phy(sci_port, sci_phy, true);
  1316. /* Re-enter the configuring state since this may be the last phy in
  1317. * the port
  1318. */
  1319. port_state_machine_change(sci_port,
  1320. SCI_PORT_SUB_CONFIGURING);
  1321. return SCI_SUCCESS;
  1322. default:
  1323. dev_warn(sciport_to_dev(sci_port),
  1324. "%s: in wrong state: %d\n", __func__, state);
  1325. return SCI_FAILURE_INVALID_STATE;
  1326. }
  1327. }
  1328. enum sci_status scic_sds_port_link_up(struct scic_sds_port *sci_port,
  1329. struct scic_sds_phy *sci_phy)
  1330. {
  1331. enum scic_sds_port_states state;
  1332. state = sci_port->sm.current_state_id;
  1333. switch (state) {
  1334. case SCI_PORT_SUB_WAITING:
  1335. /* Since this is the first phy going link up for the port we
  1336. * can just enable it and continue
  1337. */
  1338. scic_sds_port_activate_phy(sci_port, sci_phy, true);
  1339. port_state_machine_change(sci_port,
  1340. SCI_PORT_SUB_OPERATIONAL);
  1341. return SCI_SUCCESS;
  1342. case SCI_PORT_SUB_OPERATIONAL:
  1343. scic_sds_port_general_link_up_handler(sci_port, sci_phy, true);
  1344. return SCI_SUCCESS;
  1345. case SCI_PORT_RESETTING:
  1346. /* TODO We should make sure that the phy that has gone
  1347. * link up is the same one on which we sent the reset. It is
  1348. * possible that the phy on which we sent the reset is not the
  1349. * one that has gone link up and we want to make sure that
  1350. * phy being reset comes back. Consider the case where a
  1351. * reset is sent but before the hardware processes the reset it
  1352. * get a link up on the port because of a hot plug event.
  1353. * because of the reset request this phy will go link down
  1354. * almost immediately.
  1355. */
  1356. /* In the resetting state we don't notify the user regarding
  1357. * link up and link down notifications.
  1358. */
  1359. scic_sds_port_general_link_up_handler(sci_port, sci_phy, false);
  1360. return SCI_SUCCESS;
  1361. default:
  1362. dev_warn(sciport_to_dev(sci_port),
  1363. "%s: in wrong state: %d\n", __func__, state);
  1364. return SCI_FAILURE_INVALID_STATE;
  1365. }
  1366. }
  1367. enum sci_status scic_sds_port_link_down(struct scic_sds_port *sci_port,
  1368. struct scic_sds_phy *sci_phy)
  1369. {
  1370. enum scic_sds_port_states state;
  1371. state = sci_port->sm.current_state_id;
  1372. switch (state) {
  1373. case SCI_PORT_SUB_OPERATIONAL:
  1374. scic_sds_port_deactivate_phy(sci_port, sci_phy, true);
  1375. /* If there are no active phys left in the port, then
  1376. * transition the port to the WAITING state until such time
  1377. * as a phy goes link up
  1378. */
  1379. if (sci_port->active_phy_mask == 0)
  1380. port_state_machine_change(sci_port,
  1381. SCI_PORT_SUB_WAITING);
  1382. return SCI_SUCCESS;
  1383. case SCI_PORT_RESETTING:
  1384. /* In the resetting state we don't notify the user regarding
  1385. * link up and link down notifications. */
  1386. scic_sds_port_deactivate_phy(sci_port, sci_phy, false);
  1387. return SCI_SUCCESS;
  1388. default:
  1389. dev_warn(sciport_to_dev(sci_port),
  1390. "%s: in wrong state: %d\n", __func__, state);
  1391. return SCI_FAILURE_INVALID_STATE;
  1392. }
  1393. }
  1394. enum sci_status scic_sds_port_start_io(struct scic_sds_port *sci_port,
  1395. struct scic_sds_remote_device *sci_dev,
  1396. struct scic_sds_request *sci_req)
  1397. {
  1398. enum scic_sds_port_states state;
  1399. state = sci_port->sm.current_state_id;
  1400. switch (state) {
  1401. case SCI_PORT_SUB_WAITING:
  1402. return SCI_FAILURE_INVALID_STATE;
  1403. case SCI_PORT_SUB_OPERATIONAL:
  1404. sci_port->started_request_count++;
  1405. return SCI_SUCCESS;
  1406. default:
  1407. dev_warn(sciport_to_dev(sci_port),
  1408. "%s: in wrong state: %d\n", __func__, state);
  1409. return SCI_FAILURE_INVALID_STATE;
  1410. }
  1411. }
  1412. enum sci_status scic_sds_port_complete_io(struct scic_sds_port *sci_port,
  1413. struct scic_sds_remote_device *sci_dev,
  1414. struct scic_sds_request *sci_req)
  1415. {
  1416. enum scic_sds_port_states state;
  1417. state = sci_port->sm.current_state_id;
  1418. switch (state) {
  1419. case SCI_PORT_STOPPED:
  1420. dev_warn(sciport_to_dev(sci_port),
  1421. "%s: in wrong state: %d\n", __func__, state);
  1422. return SCI_FAILURE_INVALID_STATE;
  1423. case SCI_PORT_STOPPING:
  1424. scic_sds_port_decrement_request_count(sci_port);
  1425. if (sci_port->started_request_count == 0)
  1426. port_state_machine_change(sci_port,
  1427. SCI_PORT_STOPPED);
  1428. break;
  1429. case SCI_PORT_READY:
  1430. case SCI_PORT_RESETTING:
  1431. case SCI_PORT_FAILED:
  1432. case SCI_PORT_SUB_WAITING:
  1433. case SCI_PORT_SUB_OPERATIONAL:
  1434. scic_sds_port_decrement_request_count(sci_port);
  1435. break;
  1436. case SCI_PORT_SUB_CONFIGURING:
  1437. scic_sds_port_decrement_request_count(sci_port);
  1438. if (sci_port->started_request_count == 0) {
  1439. port_state_machine_change(sci_port,
  1440. SCI_PORT_SUB_OPERATIONAL);
  1441. }
  1442. break;
  1443. }
  1444. return SCI_SUCCESS;
  1445. }
  1446. /**
  1447. *
  1448. * @sci_port: This is the port object which to suspend.
  1449. *
  1450. * This method will enable the SCU Port Task Scheduler for this port object but
  1451. * will leave the port task scheduler in a suspended state. none
  1452. */
  1453. static void
  1454. scic_sds_port_enable_port_task_scheduler(struct scic_sds_port *port)
  1455. {
  1456. u32 pts_control_value;
  1457. pts_control_value = readl(&port->port_task_scheduler_registers->control);
  1458. pts_control_value |= SCU_PTSxCR_GEN_BIT(ENABLE) | SCU_PTSxCR_GEN_BIT(SUSPEND);
  1459. writel(pts_control_value, &port->port_task_scheduler_registers->control);
  1460. }
  1461. /**
  1462. *
  1463. * @sci_port: This is the port object which to resume.
  1464. *
  1465. * This method will disable the SCU port task scheduler for this port object.
  1466. * none
  1467. */
  1468. static void
  1469. scic_sds_port_disable_port_task_scheduler(struct scic_sds_port *port)
  1470. {
  1471. u32 pts_control_value;
  1472. pts_control_value = readl(&port->port_task_scheduler_registers->control);
  1473. pts_control_value &=
  1474. ~(SCU_PTSxCR_GEN_BIT(ENABLE) | SCU_PTSxCR_GEN_BIT(SUSPEND));
  1475. writel(pts_control_value, &port->port_task_scheduler_registers->control);
  1476. }
  1477. static void scic_sds_port_post_dummy_remote_node(struct scic_sds_port *sci_port)
  1478. {
  1479. struct scic_sds_controller *scic = sci_port->owning_controller;
  1480. u8 phys_index = sci_port->physical_port_index;
  1481. union scu_remote_node_context *rnc;
  1482. u16 rni = sci_port->reserved_rni;
  1483. u32 command;
  1484. rnc = &scic->remote_node_context_table[rni];
  1485. rnc->ssp.is_valid = true;
  1486. command = SCU_CONTEXT_COMMAND_POST_RNC_32 |
  1487. phys_index << SCU_CONTEXT_COMMAND_LOGICAL_PORT_SHIFT | rni;
  1488. scic_sds_controller_post_request(scic, command);
  1489. /* ensure hardware has seen the post rnc command and give it
  1490. * ample time to act before sending the suspend
  1491. */
  1492. readl(&scic->smu_registers->interrupt_status); /* flush */
  1493. udelay(10);
  1494. command = SCU_CONTEXT_COMMAND_POST_RNC_SUSPEND_TX_RX |
  1495. phys_index << SCU_CONTEXT_COMMAND_LOGICAL_PORT_SHIFT | rni;
  1496. scic_sds_controller_post_request(scic, command);
  1497. }
  1498. static void scic_sds_port_stopped_state_enter(struct sci_base_state_machine *sm)
  1499. {
  1500. struct scic_sds_port *sci_port = container_of(sm, typeof(*sci_port), sm);
  1501. if (sci_port->sm.previous_state_id == SCI_PORT_STOPPING) {
  1502. /*
  1503. * If we enter this state becasuse of a request to stop
  1504. * the port then we want to disable the hardwares port
  1505. * task scheduler. */
  1506. scic_sds_port_disable_port_task_scheduler(sci_port);
  1507. }
  1508. }
  1509. static void scic_sds_port_stopped_state_exit(struct sci_base_state_machine *sm)
  1510. {
  1511. struct scic_sds_port *sci_port = container_of(sm, typeof(*sci_port), sm);
  1512. /* Enable and suspend the port task scheduler */
  1513. scic_sds_port_enable_port_task_scheduler(sci_port);
  1514. }
  1515. static void scic_sds_port_ready_state_enter(struct sci_base_state_machine *sm)
  1516. {
  1517. struct scic_sds_port *sci_port = container_of(sm, typeof(*sci_port), sm);
  1518. struct scic_sds_controller *scic = sci_port->owning_controller;
  1519. struct isci_host *ihost = scic_to_ihost(scic);
  1520. struct isci_port *iport = sci_port_to_iport(sci_port);
  1521. u32 prev_state;
  1522. prev_state = sci_port->sm.previous_state_id;
  1523. if (prev_state == SCI_PORT_RESETTING)
  1524. isci_port_hard_reset_complete(iport, SCI_SUCCESS);
  1525. else
  1526. isci_port_not_ready(ihost, iport);
  1527. /* Post and suspend the dummy remote node context for this port. */
  1528. scic_sds_port_post_dummy_remote_node(sci_port);
  1529. /* Start the ready substate machine */
  1530. port_state_machine_change(sci_port,
  1531. SCI_PORT_SUB_WAITING);
  1532. }
  1533. static void scic_sds_port_resetting_state_exit(struct sci_base_state_machine *sm)
  1534. {
  1535. struct scic_sds_port *sci_port = container_of(sm, typeof(*sci_port), sm);
  1536. sci_del_timer(&sci_port->timer);
  1537. }
  1538. static void scic_sds_port_stopping_state_exit(struct sci_base_state_machine *sm)
  1539. {
  1540. struct scic_sds_port *sci_port = container_of(sm, typeof(*sci_port), sm);
  1541. sci_del_timer(&sci_port->timer);
  1542. scic_sds_port_destroy_dummy_resources(sci_port);
  1543. }
  1544. static void scic_sds_port_failed_state_enter(struct sci_base_state_machine *sm)
  1545. {
  1546. struct scic_sds_port *sci_port = container_of(sm, typeof(*sci_port), sm);
  1547. struct isci_port *iport = sci_port_to_iport(sci_port);
  1548. isci_port_hard_reset_complete(iport, SCI_FAILURE_TIMEOUT);
  1549. }
  1550. /* --------------------------------------------------------------------------- */
  1551. static const struct sci_base_state scic_sds_port_state_table[] = {
  1552. [SCI_PORT_STOPPED] = {
  1553. .enter_state = scic_sds_port_stopped_state_enter,
  1554. .exit_state = scic_sds_port_stopped_state_exit
  1555. },
  1556. [SCI_PORT_STOPPING] = {
  1557. .exit_state = scic_sds_port_stopping_state_exit
  1558. },
  1559. [SCI_PORT_READY] = {
  1560. .enter_state = scic_sds_port_ready_state_enter,
  1561. },
  1562. [SCI_PORT_SUB_WAITING] = {
  1563. .enter_state = scic_sds_port_ready_substate_waiting_enter,
  1564. },
  1565. [SCI_PORT_SUB_OPERATIONAL] = {
  1566. .enter_state = scic_sds_port_ready_substate_operational_enter,
  1567. .exit_state = scic_sds_port_ready_substate_operational_exit
  1568. },
  1569. [SCI_PORT_SUB_CONFIGURING] = {
  1570. .enter_state = scic_sds_port_ready_substate_configuring_enter,
  1571. .exit_state = scic_sds_port_ready_substate_configuring_exit
  1572. },
  1573. [SCI_PORT_RESETTING] = {
  1574. .exit_state = scic_sds_port_resetting_state_exit
  1575. },
  1576. [SCI_PORT_FAILED] = {
  1577. .enter_state = scic_sds_port_failed_state_enter,
  1578. }
  1579. };
  1580. void scic_sds_port_construct(struct scic_sds_port *sci_port, u8 index,
  1581. struct scic_sds_controller *scic)
  1582. {
  1583. sci_init_sm(&sci_port->sm, scic_sds_port_state_table, SCI_PORT_STOPPED);
  1584. sci_port->logical_port_index = SCIC_SDS_DUMMY_PORT;
  1585. sci_port->physical_port_index = index;
  1586. sci_port->active_phy_mask = 0;
  1587. sci_port->ready_exit = false;
  1588. sci_port->owning_controller = scic;
  1589. sci_port->started_request_count = 0;
  1590. sci_port->assigned_device_count = 0;
  1591. sci_port->reserved_rni = SCU_DUMMY_INDEX;
  1592. sci_port->reserved_tci = SCU_DUMMY_INDEX;
  1593. sci_init_timer(&sci_port->timer, port_timeout);
  1594. sci_port->port_task_scheduler_registers = NULL;
  1595. for (index = 0; index < SCI_MAX_PHYS; index++)
  1596. sci_port->phy_table[index] = NULL;
  1597. }
  1598. void isci_port_init(struct isci_port *iport, struct isci_host *ihost, int index)
  1599. {
  1600. INIT_LIST_HEAD(&iport->remote_dev_list);
  1601. INIT_LIST_HEAD(&iport->domain_dev_list);
  1602. spin_lock_init(&iport->state_lock);
  1603. init_completion(&iport->start_complete);
  1604. iport->isci_host = ihost;
  1605. isci_port_change_state(iport, isci_freed);
  1606. atomic_set(&iport->event, 0);
  1607. }
  1608. /**
  1609. * isci_port_get_state() - This function gets the status of the port object.
  1610. * @isci_port: This parameter points to the isci_port object
  1611. *
  1612. * status of the object as a isci_status enum.
  1613. */
  1614. enum isci_status isci_port_get_state(
  1615. struct isci_port *isci_port)
  1616. {
  1617. return isci_port->status;
  1618. }
  1619. void scic_sds_port_broadcast_change_received(
  1620. struct scic_sds_port *sci_port,
  1621. struct scic_sds_phy *sci_phy)
  1622. {
  1623. struct scic_sds_controller *scic = sci_port->owning_controller;
  1624. struct isci_host *ihost = scic_to_ihost(scic);
  1625. /* notify the user. */
  1626. isci_port_bc_change_received(ihost, sci_port, sci_phy);
  1627. }
  1628. int isci_port_perform_hard_reset(struct isci_host *ihost, struct isci_port *iport,
  1629. struct isci_phy *iphy)
  1630. {
  1631. unsigned long flags;
  1632. enum sci_status status;
  1633. int idx, ret = TMF_RESP_FUNC_COMPLETE;
  1634. dev_dbg(&ihost->pdev->dev, "%s: iport = %p\n",
  1635. __func__, iport);
  1636. init_completion(&iport->hard_reset_complete);
  1637. spin_lock_irqsave(&ihost->scic_lock, flags);
  1638. #define ISCI_PORT_RESET_TIMEOUT SCIC_SDS_SIGNATURE_FIS_TIMEOUT
  1639. status = scic_port_hard_reset(&iport->sci, ISCI_PORT_RESET_TIMEOUT);
  1640. spin_unlock_irqrestore(&ihost->scic_lock, flags);
  1641. if (status == SCI_SUCCESS) {
  1642. wait_for_completion(&iport->hard_reset_complete);
  1643. dev_dbg(&ihost->pdev->dev,
  1644. "%s: iport = %p; hard reset completion\n",
  1645. __func__, iport);
  1646. if (iport->hard_reset_status != SCI_SUCCESS)
  1647. ret = TMF_RESP_FUNC_FAILED;
  1648. } else {
  1649. ret = TMF_RESP_FUNC_FAILED;
  1650. dev_err(&ihost->pdev->dev,
  1651. "%s: iport = %p; scic_port_hard_reset call"
  1652. " failed 0x%x\n",
  1653. __func__, iport, status);
  1654. }
  1655. /* If the hard reset for the port has failed, consider this
  1656. * the same as link failures on all phys in the port.
  1657. */
  1658. if (ret != TMF_RESP_FUNC_COMPLETE) {
  1659. dev_err(&ihost->pdev->dev,
  1660. "%s: iport = %p; hard reset failed "
  1661. "(0x%x) - driving explicit link fail for all phys\n",
  1662. __func__, iport, iport->hard_reset_status);
  1663. /* Down all phys in the port. */
  1664. spin_lock_irqsave(&ihost->scic_lock, flags);
  1665. for (idx = 0; idx < SCI_MAX_PHYS; ++idx) {
  1666. if (iport->sci.phy_table[idx] != NULL) {
  1667. scic_sds_phy_stop(
  1668. iport->sci.phy_table[idx]);
  1669. scic_sds_phy_start(
  1670. iport->sci.phy_table[idx]);
  1671. }
  1672. }
  1673. spin_unlock_irqrestore(&ihost->scic_lock, flags);
  1674. }
  1675. return ret;
  1676. }
  1677. /**
  1678. * isci_port_deformed() - This function is called by libsas when a port becomes
  1679. * inactive.
  1680. * @phy: This parameter specifies the libsas phy with the inactive port.
  1681. *
  1682. */
  1683. void isci_port_deformed(struct asd_sas_phy *phy)
  1684. {
  1685. pr_debug("%s: sas_phy = %p\n", __func__, phy);
  1686. }
  1687. /**
  1688. * isci_port_formed() - This function is called by libsas when a port becomes
  1689. * active.
  1690. * @phy: This parameter specifies the libsas phy with the active port.
  1691. *
  1692. */
  1693. void isci_port_formed(struct asd_sas_phy *phy)
  1694. {
  1695. pr_debug("%s: sas_phy = %p, sas_port = %p\n", __func__, phy, phy->port);
  1696. }