port.c 60 KB

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