port.c 52 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737
  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. static void sci_port_get_protocols(struct isci_port *iport, struct sci_phy_proto *proto)
  72. {
  73. u8 index;
  74. proto->all = 0;
  75. for (index = 0; index < SCI_MAX_PHYS; index++) {
  76. struct isci_phy *iphy = iport->phy_table[index];
  77. if (!iphy)
  78. continue;
  79. sci_phy_get_protocols(iphy, proto);
  80. }
  81. }
  82. static u32 sci_port_get_phys(struct isci_port *iport)
  83. {
  84. u32 index;
  85. u32 mask;
  86. mask = 0;
  87. for (index = 0; index < SCI_MAX_PHYS; index++)
  88. if (iport->phy_table[index])
  89. mask |= (1 << index);
  90. return mask;
  91. }
  92. /**
  93. * sci_port_get_properties() - This method simply returns the properties
  94. * regarding the port, such as: physical index, protocols, sas address, etc.
  95. * @port: this parameter specifies the port for which to retrieve the physical
  96. * index.
  97. * @properties: This parameter specifies the properties structure into which to
  98. * copy the requested information.
  99. *
  100. * Indicate if the user specified a valid port. SCI_SUCCESS This value is
  101. * returned if the specified port was valid. SCI_FAILURE_INVALID_PORT This
  102. * value is returned if the specified port is not valid. When this value is
  103. * returned, no data is copied to the properties output parameter.
  104. */
  105. static enum sci_status sci_port_get_properties(struct isci_port *iport,
  106. struct sci_port_properties *prop)
  107. {
  108. if (!iport || iport->logical_port_index == SCIC_SDS_DUMMY_PORT)
  109. return SCI_FAILURE_INVALID_PORT;
  110. prop->index = iport->logical_port_index;
  111. prop->phy_mask = sci_port_get_phys(iport);
  112. sci_port_get_sas_address(iport, &prop->local.sas_address);
  113. sci_port_get_protocols(iport, &prop->local.protocols);
  114. sci_port_get_attached_sas_address(iport, &prop->remote.sas_address);
  115. return SCI_SUCCESS;
  116. }
  117. static void sci_port_bcn_enable(struct isci_port *iport)
  118. {
  119. struct isci_phy *iphy;
  120. u32 val;
  121. int i;
  122. for (i = 0; i < ARRAY_SIZE(iport->phy_table); i++) {
  123. iphy = iport->phy_table[i];
  124. if (!iphy)
  125. continue;
  126. val = readl(&iphy->link_layer_registers->link_layer_control);
  127. /* clear the bit by writing 1. */
  128. writel(val, &iphy->link_layer_registers->link_layer_control);
  129. }
  130. }
  131. static void isci_port_bc_change_received(struct isci_host *ihost,
  132. struct isci_port *iport,
  133. struct isci_phy *iphy)
  134. {
  135. dev_dbg(&ihost->pdev->dev,
  136. "%s: isci_phy = %p, sas_phy = %p\n",
  137. __func__, iphy, &iphy->sas_phy);
  138. ihost->sas_ha.notify_port_event(&iphy->sas_phy, PORTE_BROADCAST_RCVD);
  139. sci_port_bcn_enable(iport);
  140. }
  141. static void isci_port_link_up(struct isci_host *isci_host,
  142. struct isci_port *iport,
  143. struct isci_phy *iphy)
  144. {
  145. unsigned long flags;
  146. struct sci_port_properties properties;
  147. unsigned long success = true;
  148. BUG_ON(iphy->isci_port != NULL);
  149. iphy->isci_port = iport;
  150. dev_dbg(&isci_host->pdev->dev,
  151. "%s: isci_port = %p\n",
  152. __func__, iport);
  153. spin_lock_irqsave(&iphy->sas_phy.frame_rcvd_lock, flags);
  154. isci_port_change_state(iphy->isci_port, isci_starting);
  155. sci_port_get_properties(iport, &properties);
  156. if (iphy->protocol == SCIC_SDS_PHY_PROTOCOL_SATA) {
  157. u64 attached_sas_address;
  158. iphy->sas_phy.oob_mode = SATA_OOB_MODE;
  159. iphy->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 sci_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(&iphy->sas_phy.attached_sas_addr,
  172. &attached_sas_address, sizeof(attached_sas_address));
  173. } else if (iphy->protocol == SCIC_SDS_PHY_PROTOCOL_SAS) {
  174. iphy->sas_phy.oob_mode = SAS_OOB_MODE;
  175. iphy->sas_phy.frame_rcvd_size = sizeof(struct sas_identify_frame);
  176. /* Copy the attached SAS address from the IAF */
  177. memcpy(iphy->sas_phy.attached_sas_addr,
  178. iphy->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. iphy->sas_phy.phy->negotiated_linkrate = sci_phy_linkrate(iphy);
  184. spin_unlock_irqrestore(&iphy->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(&iphy->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. The
  211. * next task sent to this device will be returned as
  212. * SAS_TASK_UNDELIVERED, and the scsi mid layer will
  213. * 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. set_bit(IDEV_GONE, &isci_device->flags);
  222. }
  223. isci_port_change_state(isci_port, isci_stopping);
  224. }
  225. }
  226. /* Notify libsas of the borken link, this will trigger calls to our
  227. * isci_port_deformed and isci_dev_gone functions.
  228. */
  229. sas_phy_disconnected(&isci_phy->sas_phy);
  230. isci_host->sas_ha.notify_phy_event(&isci_phy->sas_phy,
  231. PHYE_LOSS_OF_SIGNAL);
  232. isci_phy->isci_port = NULL;
  233. dev_dbg(&isci_host->pdev->dev,
  234. "%s: isci_port = %p - Done\n", __func__, isci_port);
  235. }
  236. /**
  237. * isci_port_ready() - This function is called by the sci core when a link
  238. * becomes ready.
  239. * @isci_host: This parameter specifies the isci host object.
  240. * @port: This parameter specifies the sci port with the active link.
  241. *
  242. */
  243. static void isci_port_ready(struct isci_host *isci_host, struct isci_port *isci_port)
  244. {
  245. dev_dbg(&isci_host->pdev->dev,
  246. "%s: isci_port = %p\n", __func__, isci_port);
  247. complete_all(&isci_port->start_complete);
  248. isci_port_change_state(isci_port, isci_ready);
  249. return;
  250. }
  251. /**
  252. * isci_port_not_ready() - This function is called by the sci core when a link
  253. * is not ready. All remote devices on this link will be removed if they are
  254. * in the stopping state.
  255. * @isci_host: This parameter specifies the isci host object.
  256. * @port: This parameter specifies the sci port with the active link.
  257. *
  258. */
  259. static void isci_port_not_ready(struct isci_host *isci_host, struct isci_port *isci_port)
  260. {
  261. dev_dbg(&isci_host->pdev->dev,
  262. "%s: isci_port = %p\n", __func__, isci_port);
  263. }
  264. static void isci_port_stop_complete(struct isci_host *ihost,
  265. struct isci_port *iport,
  266. enum sci_status completion_status)
  267. {
  268. dev_dbg(&ihost->pdev->dev, "Port stop complete\n");
  269. }
  270. static bool is_port_ready_state(enum sci_port_states state)
  271. {
  272. switch (state) {
  273. case SCI_PORT_READY:
  274. case SCI_PORT_SUB_WAITING:
  275. case SCI_PORT_SUB_OPERATIONAL:
  276. case SCI_PORT_SUB_CONFIGURING:
  277. return true;
  278. default:
  279. return false;
  280. }
  281. }
  282. /* flag dummy rnc hanling when exiting a ready state */
  283. static void port_state_machine_change(struct isci_port *iport,
  284. enum sci_port_states state)
  285. {
  286. struct sci_base_state_machine *sm = &iport->sm;
  287. enum sci_port_states old_state = sm->current_state_id;
  288. if (is_port_ready_state(old_state) && !is_port_ready_state(state))
  289. iport->ready_exit = true;
  290. sci_change_state(sm, state);
  291. iport->ready_exit = false;
  292. }
  293. /**
  294. * isci_port_hard_reset_complete() - This function is called by the sci core
  295. * when the hard reset complete notification has been received.
  296. * @port: This parameter specifies the sci port with the active link.
  297. * @completion_status: This parameter specifies the core status for the reset
  298. * process.
  299. *
  300. */
  301. static void isci_port_hard_reset_complete(struct isci_port *isci_port,
  302. enum sci_status completion_status)
  303. {
  304. dev_dbg(&isci_port->isci_host->pdev->dev,
  305. "%s: isci_port = %p, completion_status=%x\n",
  306. __func__, isci_port, completion_status);
  307. /* Save the status of the hard reset from the port. */
  308. isci_port->hard_reset_status = completion_status;
  309. if (completion_status != SCI_SUCCESS) {
  310. /* The reset failed. The port state is now SCI_PORT_FAILED. */
  311. if (isci_port->active_phy_mask == 0) {
  312. /* Generate the link down now to the host, since it
  313. * was intercepted by the hard reset state machine when
  314. * it really happened.
  315. */
  316. isci_port_link_down(isci_port->isci_host,
  317. &isci_port->isci_host->phys[
  318. isci_port->last_active_phy],
  319. isci_port);
  320. }
  321. /* Advance the port state so that link state changes will be
  322. * noticed.
  323. */
  324. port_state_machine_change(isci_port, SCI_PORT_SUB_WAITING);
  325. }
  326. complete_all(&isci_port->hard_reset_complete);
  327. }
  328. /* This method will return a true value if the specified phy can be assigned to
  329. * this port The following is a list of phys for each port that are allowed: -
  330. * Port 0 - 3 2 1 0 - Port 1 - 1 - Port 2 - 3 2 - Port 3 - 3 This method
  331. * doesn't preclude all configurations. It merely ensures that a phy is part
  332. * of the allowable set of phy identifiers for that port. For example, one
  333. * could assign phy 3 to port 0 and no other phys. Please refer to
  334. * sci_port_is_phy_mask_valid() for information regarding whether the
  335. * phy_mask for a port can be supported. bool true if this is a valid phy
  336. * assignment for the port false if this is not a valid phy assignment for the
  337. * port
  338. */
  339. bool sci_port_is_valid_phy_assignment(struct isci_port *iport, u32 phy_index)
  340. {
  341. struct isci_host *ihost = iport->owning_controller;
  342. struct sci_user_parameters *user = &ihost->user_parameters;
  343. /* Initialize to invalid value. */
  344. u32 existing_phy_index = SCI_MAX_PHYS;
  345. u32 index;
  346. if ((iport->physical_port_index == 1) && (phy_index != 1))
  347. return false;
  348. if (iport->physical_port_index == 3 && phy_index != 3)
  349. return false;
  350. if (iport->physical_port_index == 2 &&
  351. (phy_index == 0 || phy_index == 1))
  352. return false;
  353. for (index = 0; index < SCI_MAX_PHYS; index++)
  354. if (iport->phy_table[index] && index != phy_index)
  355. existing_phy_index = index;
  356. /* Ensure that all of the phys in the port are capable of
  357. * operating at the same maximum link rate.
  358. */
  359. if (existing_phy_index < SCI_MAX_PHYS &&
  360. user->phys[phy_index].max_speed_generation !=
  361. user->phys[existing_phy_index].max_speed_generation)
  362. return false;
  363. return true;
  364. }
  365. /**
  366. *
  367. * @sci_port: This is the port object for which to determine if the phy mask
  368. * can be supported.
  369. *
  370. * This method will return a true value if the port's phy mask can be supported
  371. * by the SCU. The following is a list of valid PHY mask configurations for
  372. * each port: - Port 0 - [[3 2] 1] 0 - Port 1 - [1] - Port 2 - [[3] 2]
  373. * - Port 3 - [3] This method returns a boolean indication specifying if the
  374. * phy mask can be supported. true if this is a valid phy assignment for the
  375. * port false if this is not a valid phy assignment for the port
  376. */
  377. static bool sci_port_is_phy_mask_valid(
  378. struct isci_port *iport,
  379. u32 phy_mask)
  380. {
  381. if (iport->physical_port_index == 0) {
  382. if (((phy_mask & 0x0F) == 0x0F)
  383. || ((phy_mask & 0x03) == 0x03)
  384. || ((phy_mask & 0x01) == 0x01)
  385. || (phy_mask == 0))
  386. return true;
  387. } else if (iport->physical_port_index == 1) {
  388. if (((phy_mask & 0x02) == 0x02)
  389. || (phy_mask == 0))
  390. return true;
  391. } else if (iport->physical_port_index == 2) {
  392. if (((phy_mask & 0x0C) == 0x0C)
  393. || ((phy_mask & 0x04) == 0x04)
  394. || (phy_mask == 0))
  395. return true;
  396. } else if (iport->physical_port_index == 3) {
  397. if (((phy_mask & 0x08) == 0x08)
  398. || (phy_mask == 0))
  399. return true;
  400. }
  401. return false;
  402. }
  403. /*
  404. * This method retrieves a currently active (i.e. connected) phy contained in
  405. * the port. Currently, the lowest order phy that is connected is returned.
  406. * This method returns a pointer to a SCIS_SDS_PHY object. NULL This value is
  407. * returned if there are no currently active (i.e. connected to a remote end
  408. * point) phys contained in the port. All other values specify a struct sci_phy
  409. * object that is active in the port.
  410. */
  411. static struct isci_phy *sci_port_get_a_connected_phy(struct isci_port *iport)
  412. {
  413. u32 index;
  414. struct isci_phy *iphy;
  415. for (index = 0; index < SCI_MAX_PHYS; index++) {
  416. /* Ensure that the phy is both part of the port and currently
  417. * connected to the remote end-point.
  418. */
  419. iphy = iport->phy_table[index];
  420. if (iphy && sci_port_active_phy(iport, iphy))
  421. return iphy;
  422. }
  423. return NULL;
  424. }
  425. static enum sci_status sci_port_set_phy(struct isci_port *iport, struct isci_phy *iphy)
  426. {
  427. /* Check to see if we can add this phy to a port
  428. * that means that the phy is not part of a port and that the port does
  429. * not already have a phy assinged to the phy index.
  430. */
  431. if (!iport->phy_table[iphy->phy_index] &&
  432. !phy_get_non_dummy_port(iphy) &&
  433. sci_port_is_valid_phy_assignment(iport, iphy->phy_index)) {
  434. /* Phy is being added in the stopped state so we are in MPC mode
  435. * make logical port index = physical port index
  436. */
  437. iport->logical_port_index = iport->physical_port_index;
  438. iport->phy_table[iphy->phy_index] = iphy;
  439. sci_phy_set_port(iphy, iport);
  440. return SCI_SUCCESS;
  441. }
  442. return SCI_FAILURE;
  443. }
  444. static enum sci_status sci_port_clear_phy(struct isci_port *iport, struct isci_phy *iphy)
  445. {
  446. /* Make sure that this phy is part of this port */
  447. if (iport->phy_table[iphy->phy_index] == iphy &&
  448. phy_get_non_dummy_port(iphy) == iport) {
  449. struct isci_host *ihost = iport->owning_controller;
  450. /* Yep it is assigned to this port so remove it */
  451. sci_phy_set_port(iphy, &ihost->ports[SCI_MAX_PORTS]);
  452. iport->phy_table[iphy->phy_index] = NULL;
  453. return SCI_SUCCESS;
  454. }
  455. return SCI_FAILURE;
  456. }
  457. void sci_port_get_sas_address(struct isci_port *iport, struct sci_sas_address *sas)
  458. {
  459. u32 index;
  460. sas->high = 0;
  461. sas->low = 0;
  462. for (index = 0; index < SCI_MAX_PHYS; index++)
  463. if (iport->phy_table[index])
  464. sci_phy_get_sas_address(iport->phy_table[index], sas);
  465. }
  466. void sci_port_get_attached_sas_address(struct isci_port *iport, struct sci_sas_address *sas)
  467. {
  468. struct isci_phy *iphy;
  469. /*
  470. * Ensure that the phy is both part of the port and currently
  471. * connected to the remote end-point.
  472. */
  473. iphy = sci_port_get_a_connected_phy(iport);
  474. if (iphy) {
  475. if (iphy->protocol != SCIC_SDS_PHY_PROTOCOL_SATA) {
  476. sci_phy_get_attached_sas_address(iphy, sas);
  477. } else {
  478. sci_phy_get_sas_address(iphy, sas);
  479. sas->low += iphy->phy_index;
  480. }
  481. } else {
  482. sas->high = 0;
  483. sas->low = 0;
  484. }
  485. }
  486. /**
  487. * sci_port_construct_dummy_rnc() - create dummy rnc for si workaround
  488. *
  489. * @sci_port: logical port on which we need to create the remote node context
  490. * @rni: remote node index for this remote node context.
  491. *
  492. * This routine will construct a dummy remote node context data structure
  493. * This structure will be posted to the hardware to work around a scheduler
  494. * error in the hardware.
  495. */
  496. static void sci_port_construct_dummy_rnc(struct isci_port *iport, u16 rni)
  497. {
  498. union scu_remote_node_context *rnc;
  499. rnc = &iport->owning_controller->remote_node_context_table[rni];
  500. memset(rnc, 0, sizeof(union scu_remote_node_context));
  501. rnc->ssp.remote_sas_address_hi = 0;
  502. rnc->ssp.remote_sas_address_lo = 0;
  503. rnc->ssp.remote_node_index = rni;
  504. rnc->ssp.remote_node_port_width = 1;
  505. rnc->ssp.logical_port_index = iport->physical_port_index;
  506. rnc->ssp.nexus_loss_timer_enable = false;
  507. rnc->ssp.check_bit = false;
  508. rnc->ssp.is_valid = true;
  509. rnc->ssp.is_remote_node_context = true;
  510. rnc->ssp.function_number = 0;
  511. rnc->ssp.arbitration_wait_time = 0;
  512. }
  513. /*
  514. * construct a dummy task context data structure. This
  515. * structure will be posted to the hardwre to work around a scheduler error
  516. * in the hardware.
  517. */
  518. static void sci_port_construct_dummy_task(struct isci_port *iport, u16 tag)
  519. {
  520. struct isci_host *ihost = iport->owning_controller;
  521. struct scu_task_context *task_context;
  522. task_context = &ihost->task_context_table[ISCI_TAG_TCI(tag)];
  523. memset(task_context, 0, sizeof(struct scu_task_context));
  524. task_context->initiator_request = 1;
  525. task_context->connection_rate = 1;
  526. task_context->logical_port_index = iport->physical_port_index;
  527. task_context->protocol_type = SCU_TASK_CONTEXT_PROTOCOL_SSP;
  528. task_context->task_index = ISCI_TAG_TCI(tag);
  529. task_context->valid = SCU_TASK_CONTEXT_VALID;
  530. task_context->context_type = SCU_TASK_CONTEXT_TYPE;
  531. task_context->remote_node_index = iport->reserved_rni;
  532. task_context->do_not_dma_ssp_good_response = 1;
  533. task_context->task_phase = 0x01;
  534. }
  535. static void sci_port_destroy_dummy_resources(struct isci_port *iport)
  536. {
  537. struct isci_host *ihost = iport->owning_controller;
  538. if (iport->reserved_tag != SCI_CONTROLLER_INVALID_IO_TAG)
  539. isci_free_tag(ihost, iport->reserved_tag);
  540. if (iport->reserved_rni != SCU_DUMMY_INDEX)
  541. sci_remote_node_table_release_remote_node_index(&ihost->available_remote_nodes,
  542. 1, iport->reserved_rni);
  543. iport->reserved_rni = SCU_DUMMY_INDEX;
  544. iport->reserved_tag = SCI_CONTROLLER_INVALID_IO_TAG;
  545. }
  546. void sci_port_setup_transports(struct isci_port *iport, u32 device_id)
  547. {
  548. u8 index;
  549. for (index = 0; index < SCI_MAX_PHYS; index++) {
  550. if (iport->active_phy_mask & (1 << index))
  551. sci_phy_setup_transport(iport->phy_table[index], device_id);
  552. }
  553. }
  554. static void sci_port_activate_phy(struct isci_port *iport, struct isci_phy *iphy,
  555. bool do_notify_user)
  556. {
  557. struct isci_host *ihost = iport->owning_controller;
  558. if (iphy->protocol != SCIC_SDS_PHY_PROTOCOL_SATA)
  559. sci_phy_resume(iphy);
  560. iport->active_phy_mask |= 1 << iphy->phy_index;
  561. sci_controller_clear_invalid_phy(ihost, iphy);
  562. if (do_notify_user == true)
  563. isci_port_link_up(ihost, iport, iphy);
  564. }
  565. void sci_port_deactivate_phy(struct isci_port *iport, struct isci_phy *iphy,
  566. bool do_notify_user)
  567. {
  568. struct isci_host *ihost = iport->owning_controller;
  569. iport->active_phy_mask &= ~(1 << iphy->phy_index);
  570. if (!iport->active_phy_mask)
  571. iport->last_active_phy = iphy->phy_index;
  572. iphy->max_negotiated_speed = SAS_LINK_RATE_UNKNOWN;
  573. /* Re-assign the phy back to the LP as if it were a narrow port */
  574. writel(iphy->phy_index,
  575. &iport->port_pe_configuration_register[iphy->phy_index]);
  576. if (do_notify_user == true)
  577. isci_port_link_down(ihost, iphy, iport);
  578. }
  579. static void sci_port_invalid_link_up(struct isci_port *iport, struct isci_phy *iphy)
  580. {
  581. struct isci_host *ihost = iport->owning_controller;
  582. /*
  583. * Check to see if we have alreay reported this link as bad and if
  584. * not go ahead and tell the SCI_USER that we have discovered an
  585. * invalid link.
  586. */
  587. if ((ihost->invalid_phy_mask & (1 << iphy->phy_index)) == 0) {
  588. ihost->invalid_phy_mask |= 1 << iphy->phy_index;
  589. dev_warn(&ihost->pdev->dev, "Invalid link up!\n");
  590. }
  591. }
  592. /**
  593. * sci_port_general_link_up_handler - phy can be assigned to port?
  594. * @sci_port: sci_port object for which has a phy that has gone link up.
  595. * @sci_phy: This is the struct isci_phy object that has gone link up.
  596. * @do_notify_user: This parameter specifies whether to inform the user (via
  597. * sci_port_link_up()) as to the fact that a new phy as become ready.
  598. *
  599. * Determine if this phy can be assigned to this
  600. * port . If the phy is not a valid PHY for
  601. * this port then the function will notify the user. A PHY can only be
  602. * part of a port if it's attached SAS ADDRESS is the same as all other PHYs in
  603. * the same port. none
  604. */
  605. static void sci_port_general_link_up_handler(struct isci_port *iport,
  606. struct isci_phy *iphy,
  607. bool do_notify_user)
  608. {
  609. struct sci_sas_address port_sas_address;
  610. struct sci_sas_address phy_sas_address;
  611. sci_port_get_attached_sas_address(iport, &port_sas_address);
  612. sci_phy_get_attached_sas_address(iphy, &phy_sas_address);
  613. /* If the SAS address of the new phy matches the SAS address of
  614. * other phys in the port OR this is the first phy in the port,
  615. * then activate the phy and allow it to be used for operations
  616. * in this port.
  617. */
  618. if ((phy_sas_address.high == port_sas_address.high &&
  619. phy_sas_address.low == port_sas_address.low) ||
  620. iport->active_phy_mask == 0) {
  621. struct sci_base_state_machine *sm = &iport->sm;
  622. sci_port_activate_phy(iport, iphy, do_notify_user);
  623. if (sm->current_state_id == SCI_PORT_RESETTING)
  624. port_state_machine_change(iport, SCI_PORT_READY);
  625. } else
  626. sci_port_invalid_link_up(iport, iphy);
  627. }
  628. /**
  629. * This method returns false if the port only has a single phy object assigned.
  630. * If there are no phys or more than one phy then the method will return
  631. * true.
  632. * @sci_port: The port for which the wide port condition is to be checked.
  633. *
  634. * bool true Is returned if this is a wide ported port. false Is returned if
  635. * this is a narrow port.
  636. */
  637. static bool sci_port_is_wide(struct isci_port *iport)
  638. {
  639. u32 index;
  640. u32 phy_count = 0;
  641. for (index = 0; index < SCI_MAX_PHYS; index++) {
  642. if (iport->phy_table[index] != NULL) {
  643. phy_count++;
  644. }
  645. }
  646. return phy_count != 1;
  647. }
  648. /**
  649. * This method is called by the PHY object when the link is detected. if the
  650. * port wants the PHY to continue on to the link up state then the port
  651. * layer must return true. If the port object returns false the phy object
  652. * must halt its attempt to go link up.
  653. * @sci_port: The port associated with the phy object.
  654. * @sci_phy: The phy object that is trying to go link up.
  655. *
  656. * true if the phy object can continue to the link up condition. true Is
  657. * returned if this phy can continue to the ready state. false Is returned if
  658. * can not continue on to the ready state. This notification is in place for
  659. * wide ports and direct attached phys. Since there are no wide ported SATA
  660. * devices this could become an invalid port configuration.
  661. */
  662. bool sci_port_link_detected(
  663. struct isci_port *iport,
  664. struct isci_phy *iphy)
  665. {
  666. if ((iport->logical_port_index != SCIC_SDS_DUMMY_PORT) &&
  667. (iphy->protocol == SCIC_SDS_PHY_PROTOCOL_SATA) &&
  668. sci_port_is_wide(iport)) {
  669. sci_port_invalid_link_up(iport, iphy);
  670. return false;
  671. }
  672. return true;
  673. }
  674. static void port_timeout(unsigned long data)
  675. {
  676. struct sci_timer *tmr = (struct sci_timer *)data;
  677. struct isci_port *iport = container_of(tmr, typeof(*iport), timer);
  678. struct isci_host *ihost = iport->owning_controller;
  679. unsigned long flags;
  680. u32 current_state;
  681. spin_lock_irqsave(&ihost->scic_lock, flags);
  682. if (tmr->cancel)
  683. goto done;
  684. current_state = iport->sm.current_state_id;
  685. if (current_state == SCI_PORT_RESETTING) {
  686. /* if the port is still in the resetting state then the timeout
  687. * fired before the reset completed.
  688. */
  689. port_state_machine_change(iport, SCI_PORT_FAILED);
  690. } else if (current_state == SCI_PORT_STOPPED) {
  691. /* if the port is stopped then the start request failed In this
  692. * case stay in the stopped state.
  693. */
  694. dev_err(sciport_to_dev(iport),
  695. "%s: SCIC Port 0x%p failed to stop before tiemout.\n",
  696. __func__,
  697. iport);
  698. } else if (current_state == SCI_PORT_STOPPING) {
  699. /* if the port is still stopping then the stop has not completed */
  700. isci_port_stop_complete(iport->owning_controller,
  701. iport,
  702. SCI_FAILURE_TIMEOUT);
  703. } else {
  704. /* The port is in the ready state and we have a timer
  705. * reporting a timeout this should not happen.
  706. */
  707. dev_err(sciport_to_dev(iport),
  708. "%s: SCIC Port 0x%p is processing a timeout operation "
  709. "in state %d.\n", __func__, iport, current_state);
  710. }
  711. done:
  712. spin_unlock_irqrestore(&ihost->scic_lock, flags);
  713. }
  714. /* --------------------------------------------------------------------------- */
  715. /**
  716. * This function updates the hardwares VIIT entry for this port.
  717. *
  718. *
  719. */
  720. static void sci_port_update_viit_entry(struct isci_port *iport)
  721. {
  722. struct sci_sas_address sas_address;
  723. sci_port_get_sas_address(iport, &sas_address);
  724. writel(sas_address.high,
  725. &iport->viit_registers->initiator_sas_address_hi);
  726. writel(sas_address.low,
  727. &iport->viit_registers->initiator_sas_address_lo);
  728. /* This value get cleared just in case its not already cleared */
  729. writel(0, &iport->viit_registers->reserved);
  730. /* We are required to update the status register last */
  731. writel(SCU_VIIT_ENTRY_ID_VIIT |
  732. SCU_VIIT_IPPT_INITIATOR |
  733. ((1 << iport->physical_port_index) << SCU_VIIT_ENTRY_LPVIE_SHIFT) |
  734. SCU_VIIT_STATUS_ALL_VALID,
  735. &iport->viit_registers->status);
  736. }
  737. enum sas_linkrate sci_port_get_max_allowed_speed(struct isci_port *iport)
  738. {
  739. u16 index;
  740. struct isci_phy *iphy;
  741. enum sas_linkrate max_allowed_speed = SAS_LINK_RATE_6_0_GBPS;
  742. /*
  743. * Loop through all of the phys in this port and find the phy with the
  744. * lowest maximum link rate. */
  745. for (index = 0; index < SCI_MAX_PHYS; index++) {
  746. iphy = iport->phy_table[index];
  747. if (iphy && sci_port_active_phy(iport, iphy) &&
  748. iphy->max_negotiated_speed < max_allowed_speed)
  749. max_allowed_speed = iphy->max_negotiated_speed;
  750. }
  751. return max_allowed_speed;
  752. }
  753. static void sci_port_suspend_port_task_scheduler(struct isci_port *iport)
  754. {
  755. u32 pts_control_value;
  756. pts_control_value = readl(&iport->port_task_scheduler_registers->control);
  757. pts_control_value |= SCU_PTSxCR_GEN_BIT(SUSPEND);
  758. writel(pts_control_value, &iport->port_task_scheduler_registers->control);
  759. }
  760. /**
  761. * sci_port_post_dummy_request() - post dummy/workaround request
  762. * @sci_port: port to post task
  763. *
  764. * Prevent the hardware scheduler from posting new requests to the front
  765. * of the scheduler queue causing a starvation problem for currently
  766. * ongoing requests.
  767. *
  768. */
  769. static void sci_port_post_dummy_request(struct isci_port *iport)
  770. {
  771. struct isci_host *ihost = iport->owning_controller;
  772. u16 tag = iport->reserved_tag;
  773. struct scu_task_context *tc;
  774. u32 command;
  775. tc = &ihost->task_context_table[ISCI_TAG_TCI(tag)];
  776. tc->abort = 0;
  777. command = SCU_CONTEXT_COMMAND_REQUEST_TYPE_POST_TC |
  778. iport->physical_port_index << SCU_CONTEXT_COMMAND_LOGICAL_PORT_SHIFT |
  779. ISCI_TAG_TCI(tag);
  780. sci_controller_post_request(ihost, command);
  781. }
  782. /**
  783. * This routine will abort the dummy request. This will alow the hardware to
  784. * power down parts of the silicon to save power.
  785. *
  786. * @sci_port: The port on which the task must be aborted.
  787. *
  788. */
  789. static void sci_port_abort_dummy_request(struct isci_port *iport)
  790. {
  791. struct isci_host *ihost = iport->owning_controller;
  792. u16 tag = iport->reserved_tag;
  793. struct scu_task_context *tc;
  794. u32 command;
  795. tc = &ihost->task_context_table[ISCI_TAG_TCI(tag)];
  796. tc->abort = 1;
  797. command = SCU_CONTEXT_COMMAND_REQUEST_POST_TC_ABORT |
  798. iport->physical_port_index << SCU_CONTEXT_COMMAND_LOGICAL_PORT_SHIFT |
  799. ISCI_TAG_TCI(tag);
  800. sci_controller_post_request(ihost, command);
  801. }
  802. /**
  803. *
  804. * @sci_port: This is the struct isci_port object to resume.
  805. *
  806. * This method will resume the port task scheduler for this port object. none
  807. */
  808. static void
  809. sci_port_resume_port_task_scheduler(struct isci_port *iport)
  810. {
  811. u32 pts_control_value;
  812. pts_control_value = readl(&iport->port_task_scheduler_registers->control);
  813. pts_control_value &= ~SCU_PTSxCR_GEN_BIT(SUSPEND);
  814. writel(pts_control_value, &iport->port_task_scheduler_registers->control);
  815. }
  816. static void sci_port_ready_substate_waiting_enter(struct sci_base_state_machine *sm)
  817. {
  818. struct isci_port *iport = container_of(sm, typeof(*iport), sm);
  819. sci_port_suspend_port_task_scheduler(iport);
  820. iport->not_ready_reason = SCIC_PORT_NOT_READY_NO_ACTIVE_PHYS;
  821. if (iport->active_phy_mask != 0) {
  822. /* At least one of the phys on the port is ready */
  823. port_state_machine_change(iport,
  824. SCI_PORT_SUB_OPERATIONAL);
  825. }
  826. }
  827. static void sci_port_ready_substate_operational_enter(struct sci_base_state_machine *sm)
  828. {
  829. u32 index;
  830. struct isci_port *iport = container_of(sm, typeof(*iport), sm);
  831. struct isci_host *ihost = iport->owning_controller;
  832. isci_port_ready(ihost, iport);
  833. for (index = 0; index < SCI_MAX_PHYS; index++) {
  834. if (iport->phy_table[index]) {
  835. writel(iport->physical_port_index,
  836. &iport->port_pe_configuration_register[
  837. iport->phy_table[index]->phy_index]);
  838. }
  839. }
  840. sci_port_update_viit_entry(iport);
  841. sci_port_resume_port_task_scheduler(iport);
  842. /*
  843. * Post the dummy task for the port so the hardware can schedule
  844. * io correctly
  845. */
  846. sci_port_post_dummy_request(iport);
  847. }
  848. static void sci_port_invalidate_dummy_remote_node(struct isci_port *iport)
  849. {
  850. struct isci_host *ihost = iport->owning_controller;
  851. u8 phys_index = iport->physical_port_index;
  852. union scu_remote_node_context *rnc;
  853. u16 rni = iport->reserved_rni;
  854. u32 command;
  855. rnc = &ihost->remote_node_context_table[rni];
  856. rnc->ssp.is_valid = false;
  857. /* ensure the preceding tc abort request has reached the
  858. * controller and give it ample time to act before posting the rnc
  859. * invalidate
  860. */
  861. readl(&ihost->smu_registers->interrupt_status); /* flush */
  862. udelay(10);
  863. command = SCU_CONTEXT_COMMAND_POST_RNC_INVALIDATE |
  864. phys_index << SCU_CONTEXT_COMMAND_LOGICAL_PORT_SHIFT | rni;
  865. sci_controller_post_request(ihost, command);
  866. }
  867. /**
  868. *
  869. * @object: This is the object which is cast to a struct isci_port object.
  870. *
  871. * This method will perform the actions required by the struct isci_port on
  872. * exiting the SCI_PORT_SUB_OPERATIONAL. This function reports
  873. * the port not ready and suspends the port task scheduler. none
  874. */
  875. static void sci_port_ready_substate_operational_exit(struct sci_base_state_machine *sm)
  876. {
  877. struct isci_port *iport = container_of(sm, typeof(*iport), sm);
  878. struct isci_host *ihost = iport->owning_controller;
  879. /*
  880. * Kill the dummy task for this port if it has not yet posted
  881. * the hardware will treat this as a NOP and just return abort
  882. * complete.
  883. */
  884. sci_port_abort_dummy_request(iport);
  885. isci_port_not_ready(ihost, iport);
  886. if (iport->ready_exit)
  887. sci_port_invalidate_dummy_remote_node(iport);
  888. }
  889. static void sci_port_ready_substate_configuring_enter(struct sci_base_state_machine *sm)
  890. {
  891. struct isci_port *iport = container_of(sm, typeof(*iport), sm);
  892. struct isci_host *ihost = iport->owning_controller;
  893. if (iport->active_phy_mask == 0) {
  894. isci_port_not_ready(ihost, iport);
  895. port_state_machine_change(iport,
  896. SCI_PORT_SUB_WAITING);
  897. } else if (iport->started_request_count == 0)
  898. port_state_machine_change(iport,
  899. SCI_PORT_SUB_OPERATIONAL);
  900. }
  901. static void sci_port_ready_substate_configuring_exit(struct sci_base_state_machine *sm)
  902. {
  903. struct isci_port *iport = container_of(sm, typeof(*iport), sm);
  904. sci_port_suspend_port_task_scheduler(iport);
  905. if (iport->ready_exit)
  906. sci_port_invalidate_dummy_remote_node(iport);
  907. }
  908. enum sci_status sci_port_start(struct isci_port *iport)
  909. {
  910. struct isci_host *ihost = iport->owning_controller;
  911. enum sci_status status = SCI_SUCCESS;
  912. enum sci_port_states state;
  913. u32 phy_mask;
  914. state = iport->sm.current_state_id;
  915. if (state != SCI_PORT_STOPPED) {
  916. dev_warn(sciport_to_dev(iport),
  917. "%s: in wrong state: %d\n", __func__, state);
  918. return SCI_FAILURE_INVALID_STATE;
  919. }
  920. if (iport->assigned_device_count > 0) {
  921. /* TODO This is a start failure operation because
  922. * there are still devices assigned to this port.
  923. * There must be no devices assigned to a port on a
  924. * start operation.
  925. */
  926. return SCI_FAILURE_UNSUPPORTED_PORT_CONFIGURATION;
  927. }
  928. if (iport->reserved_rni == SCU_DUMMY_INDEX) {
  929. u16 rni = sci_remote_node_table_allocate_remote_node(
  930. &ihost->available_remote_nodes, 1);
  931. if (rni != SCU_DUMMY_INDEX)
  932. sci_port_construct_dummy_rnc(iport, rni);
  933. else
  934. status = SCI_FAILURE_INSUFFICIENT_RESOURCES;
  935. iport->reserved_rni = rni;
  936. }
  937. if (iport->reserved_tag == SCI_CONTROLLER_INVALID_IO_TAG) {
  938. u16 tag;
  939. tag = isci_alloc_tag(ihost);
  940. if (tag == SCI_CONTROLLER_INVALID_IO_TAG)
  941. status = SCI_FAILURE_INSUFFICIENT_RESOURCES;
  942. else
  943. sci_port_construct_dummy_task(iport, tag);
  944. iport->reserved_tag = tag;
  945. }
  946. if (status == SCI_SUCCESS) {
  947. phy_mask = sci_port_get_phys(iport);
  948. /*
  949. * There are one or more phys assigned to this port. Make sure
  950. * the port's phy mask is in fact legal and supported by the
  951. * silicon.
  952. */
  953. if (sci_port_is_phy_mask_valid(iport, phy_mask) == true) {
  954. port_state_machine_change(iport,
  955. SCI_PORT_READY);
  956. return SCI_SUCCESS;
  957. }
  958. status = SCI_FAILURE;
  959. }
  960. if (status != SCI_SUCCESS)
  961. sci_port_destroy_dummy_resources(iport);
  962. return status;
  963. }
  964. enum sci_status sci_port_stop(struct isci_port *iport)
  965. {
  966. enum sci_port_states state;
  967. state = iport->sm.current_state_id;
  968. switch (state) {
  969. case SCI_PORT_STOPPED:
  970. return SCI_SUCCESS;
  971. case SCI_PORT_SUB_WAITING:
  972. case SCI_PORT_SUB_OPERATIONAL:
  973. case SCI_PORT_SUB_CONFIGURING:
  974. case SCI_PORT_RESETTING:
  975. port_state_machine_change(iport,
  976. SCI_PORT_STOPPING);
  977. return SCI_SUCCESS;
  978. default:
  979. dev_warn(sciport_to_dev(iport),
  980. "%s: in wrong state: %d\n", __func__, state);
  981. return SCI_FAILURE_INVALID_STATE;
  982. }
  983. }
  984. static enum sci_status sci_port_hard_reset(struct isci_port *iport, u32 timeout)
  985. {
  986. enum sci_status status = SCI_FAILURE_INVALID_PHY;
  987. struct isci_phy *iphy = NULL;
  988. enum sci_port_states state;
  989. u32 phy_index;
  990. state = iport->sm.current_state_id;
  991. if (state != SCI_PORT_SUB_OPERATIONAL) {
  992. dev_warn(sciport_to_dev(iport),
  993. "%s: in wrong state: %d\n", __func__, state);
  994. return SCI_FAILURE_INVALID_STATE;
  995. }
  996. /* Select a phy on which we can send the hard reset request. */
  997. for (phy_index = 0; phy_index < SCI_MAX_PHYS && !iphy; phy_index++) {
  998. iphy = iport->phy_table[phy_index];
  999. if (iphy && !sci_port_active_phy(iport, iphy)) {
  1000. /*
  1001. * We found a phy but it is not ready select
  1002. * different phy
  1003. */
  1004. iphy = NULL;
  1005. }
  1006. }
  1007. /* If we have a phy then go ahead and start the reset procedure */
  1008. if (!iphy)
  1009. return status;
  1010. status = sci_phy_reset(iphy);
  1011. if (status != SCI_SUCCESS)
  1012. return status;
  1013. sci_mod_timer(&iport->timer, timeout);
  1014. iport->not_ready_reason = SCIC_PORT_NOT_READY_HARD_RESET_REQUESTED;
  1015. port_state_machine_change(iport, SCI_PORT_RESETTING);
  1016. return SCI_SUCCESS;
  1017. }
  1018. /**
  1019. * sci_port_add_phy() -
  1020. * @sci_port: This parameter specifies the port in which the phy will be added.
  1021. * @sci_phy: This parameter is the phy which is to be added to the port.
  1022. *
  1023. * This method will add a PHY to the selected port. This method returns an
  1024. * enum sci_status. SCI_SUCCESS the phy has been added to the port. Any other
  1025. * status is a failure to add the phy to the port.
  1026. */
  1027. enum sci_status sci_port_add_phy(struct isci_port *iport,
  1028. struct isci_phy *iphy)
  1029. {
  1030. enum sci_status status;
  1031. enum sci_port_states state;
  1032. state = iport->sm.current_state_id;
  1033. switch (state) {
  1034. case SCI_PORT_STOPPED: {
  1035. struct sci_sas_address port_sas_address;
  1036. /* Read the port assigned SAS Address if there is one */
  1037. sci_port_get_sas_address(iport, &port_sas_address);
  1038. if (port_sas_address.high != 0 && port_sas_address.low != 0) {
  1039. struct sci_sas_address phy_sas_address;
  1040. /* Make sure that the PHY SAS Address matches the SAS Address
  1041. * for this port
  1042. */
  1043. sci_phy_get_sas_address(iphy, &phy_sas_address);
  1044. if (port_sas_address.high != phy_sas_address.high ||
  1045. port_sas_address.low != phy_sas_address.low)
  1046. return SCI_FAILURE_UNSUPPORTED_PORT_CONFIGURATION;
  1047. }
  1048. return sci_port_set_phy(iport, iphy);
  1049. }
  1050. case SCI_PORT_SUB_WAITING:
  1051. case SCI_PORT_SUB_OPERATIONAL:
  1052. status = sci_port_set_phy(iport, iphy);
  1053. if (status != SCI_SUCCESS)
  1054. return status;
  1055. sci_port_general_link_up_handler(iport, iphy, true);
  1056. iport->not_ready_reason = SCIC_PORT_NOT_READY_RECONFIGURING;
  1057. port_state_machine_change(iport, SCI_PORT_SUB_CONFIGURING);
  1058. return status;
  1059. case SCI_PORT_SUB_CONFIGURING:
  1060. status = sci_port_set_phy(iport, iphy);
  1061. if (status != SCI_SUCCESS)
  1062. return status;
  1063. sci_port_general_link_up_handler(iport, iphy, true);
  1064. /* Re-enter the configuring state since this may be the last phy in
  1065. * the port.
  1066. */
  1067. port_state_machine_change(iport,
  1068. SCI_PORT_SUB_CONFIGURING);
  1069. return SCI_SUCCESS;
  1070. default:
  1071. dev_warn(sciport_to_dev(iport),
  1072. "%s: in wrong state: %d\n", __func__, state);
  1073. return SCI_FAILURE_INVALID_STATE;
  1074. }
  1075. }
  1076. /**
  1077. * sci_port_remove_phy() -
  1078. * @sci_port: This parameter specifies the port in which the phy will be added.
  1079. * @sci_phy: This parameter is the phy which is to be added to the port.
  1080. *
  1081. * This method will remove the PHY from the selected PORT. This method returns
  1082. * an enum sci_status. SCI_SUCCESS the phy has been removed from the port. Any
  1083. * other status is a failure to add the phy to the port.
  1084. */
  1085. enum sci_status sci_port_remove_phy(struct isci_port *iport,
  1086. struct isci_phy *iphy)
  1087. {
  1088. enum sci_status status;
  1089. enum sci_port_states state;
  1090. state = iport->sm.current_state_id;
  1091. switch (state) {
  1092. case SCI_PORT_STOPPED:
  1093. return sci_port_clear_phy(iport, iphy);
  1094. case SCI_PORT_SUB_OPERATIONAL:
  1095. status = sci_port_clear_phy(iport, iphy);
  1096. if (status != SCI_SUCCESS)
  1097. return status;
  1098. sci_port_deactivate_phy(iport, iphy, true);
  1099. iport->not_ready_reason = SCIC_PORT_NOT_READY_RECONFIGURING;
  1100. port_state_machine_change(iport,
  1101. SCI_PORT_SUB_CONFIGURING);
  1102. return SCI_SUCCESS;
  1103. case SCI_PORT_SUB_CONFIGURING:
  1104. status = sci_port_clear_phy(iport, iphy);
  1105. if (status != SCI_SUCCESS)
  1106. return status;
  1107. sci_port_deactivate_phy(iport, iphy, true);
  1108. /* Re-enter the configuring state since this may be the last phy in
  1109. * the port
  1110. */
  1111. port_state_machine_change(iport,
  1112. SCI_PORT_SUB_CONFIGURING);
  1113. return SCI_SUCCESS;
  1114. default:
  1115. dev_warn(sciport_to_dev(iport),
  1116. "%s: in wrong state: %d\n", __func__, state);
  1117. return SCI_FAILURE_INVALID_STATE;
  1118. }
  1119. }
  1120. enum sci_status sci_port_link_up(struct isci_port *iport,
  1121. struct isci_phy *iphy)
  1122. {
  1123. enum sci_port_states state;
  1124. state = iport->sm.current_state_id;
  1125. switch (state) {
  1126. case SCI_PORT_SUB_WAITING:
  1127. /* Since this is the first phy going link up for the port we
  1128. * can just enable it and continue
  1129. */
  1130. sci_port_activate_phy(iport, iphy, true);
  1131. port_state_machine_change(iport,
  1132. SCI_PORT_SUB_OPERATIONAL);
  1133. return SCI_SUCCESS;
  1134. case SCI_PORT_SUB_OPERATIONAL:
  1135. sci_port_general_link_up_handler(iport, iphy, true);
  1136. return SCI_SUCCESS;
  1137. case SCI_PORT_RESETTING:
  1138. /* TODO We should make sure that the phy that has gone
  1139. * link up is the same one on which we sent the reset. It is
  1140. * possible that the phy on which we sent the reset is not the
  1141. * one that has gone link up and we want to make sure that
  1142. * phy being reset comes back. Consider the case where a
  1143. * reset is sent but before the hardware processes the reset it
  1144. * get a link up on the port because of a hot plug event.
  1145. * because of the reset request this phy will go link down
  1146. * almost immediately.
  1147. */
  1148. /* In the resetting state we don't notify the user regarding
  1149. * link up and link down notifications.
  1150. */
  1151. sci_port_general_link_up_handler(iport, iphy, false);
  1152. return SCI_SUCCESS;
  1153. default:
  1154. dev_warn(sciport_to_dev(iport),
  1155. "%s: in wrong state: %d\n", __func__, state);
  1156. return SCI_FAILURE_INVALID_STATE;
  1157. }
  1158. }
  1159. enum sci_status sci_port_link_down(struct isci_port *iport,
  1160. struct isci_phy *iphy)
  1161. {
  1162. enum sci_port_states state;
  1163. state = iport->sm.current_state_id;
  1164. switch (state) {
  1165. case SCI_PORT_SUB_OPERATIONAL:
  1166. sci_port_deactivate_phy(iport, iphy, true);
  1167. /* If there are no active phys left in the port, then
  1168. * transition the port to the WAITING state until such time
  1169. * as a phy goes link up
  1170. */
  1171. if (iport->active_phy_mask == 0)
  1172. port_state_machine_change(iport,
  1173. SCI_PORT_SUB_WAITING);
  1174. return SCI_SUCCESS;
  1175. case SCI_PORT_RESETTING:
  1176. /* In the resetting state we don't notify the user regarding
  1177. * link up and link down notifications. */
  1178. sci_port_deactivate_phy(iport, iphy, false);
  1179. return SCI_SUCCESS;
  1180. default:
  1181. dev_warn(sciport_to_dev(iport),
  1182. "%s: in wrong state: %d\n", __func__, state);
  1183. return SCI_FAILURE_INVALID_STATE;
  1184. }
  1185. }
  1186. enum sci_status sci_port_start_io(struct isci_port *iport,
  1187. struct isci_remote_device *idev,
  1188. struct isci_request *ireq)
  1189. {
  1190. enum sci_port_states state;
  1191. state = iport->sm.current_state_id;
  1192. switch (state) {
  1193. case SCI_PORT_SUB_WAITING:
  1194. return SCI_FAILURE_INVALID_STATE;
  1195. case SCI_PORT_SUB_OPERATIONAL:
  1196. iport->started_request_count++;
  1197. return SCI_SUCCESS;
  1198. default:
  1199. dev_warn(sciport_to_dev(iport),
  1200. "%s: in wrong state: %d\n", __func__, state);
  1201. return SCI_FAILURE_INVALID_STATE;
  1202. }
  1203. }
  1204. enum sci_status sci_port_complete_io(struct isci_port *iport,
  1205. struct isci_remote_device *idev,
  1206. struct isci_request *ireq)
  1207. {
  1208. enum sci_port_states state;
  1209. state = iport->sm.current_state_id;
  1210. switch (state) {
  1211. case SCI_PORT_STOPPED:
  1212. dev_warn(sciport_to_dev(iport),
  1213. "%s: in wrong state: %d\n", __func__, state);
  1214. return SCI_FAILURE_INVALID_STATE;
  1215. case SCI_PORT_STOPPING:
  1216. sci_port_decrement_request_count(iport);
  1217. if (iport->started_request_count == 0)
  1218. port_state_machine_change(iport,
  1219. SCI_PORT_STOPPED);
  1220. break;
  1221. case SCI_PORT_READY:
  1222. case SCI_PORT_RESETTING:
  1223. case SCI_PORT_FAILED:
  1224. case SCI_PORT_SUB_WAITING:
  1225. case SCI_PORT_SUB_OPERATIONAL:
  1226. sci_port_decrement_request_count(iport);
  1227. break;
  1228. case SCI_PORT_SUB_CONFIGURING:
  1229. sci_port_decrement_request_count(iport);
  1230. if (iport->started_request_count == 0) {
  1231. port_state_machine_change(iport,
  1232. SCI_PORT_SUB_OPERATIONAL);
  1233. }
  1234. break;
  1235. }
  1236. return SCI_SUCCESS;
  1237. }
  1238. static void sci_port_enable_port_task_scheduler(struct isci_port *iport)
  1239. {
  1240. u32 pts_control_value;
  1241. /* enable the port task scheduler in a suspended state */
  1242. pts_control_value = readl(&iport->port_task_scheduler_registers->control);
  1243. pts_control_value |= SCU_PTSxCR_GEN_BIT(ENABLE) | SCU_PTSxCR_GEN_BIT(SUSPEND);
  1244. writel(pts_control_value, &iport->port_task_scheduler_registers->control);
  1245. }
  1246. static void sci_port_disable_port_task_scheduler(struct isci_port *iport)
  1247. {
  1248. u32 pts_control_value;
  1249. pts_control_value = readl(&iport->port_task_scheduler_registers->control);
  1250. pts_control_value &=
  1251. ~(SCU_PTSxCR_GEN_BIT(ENABLE) | SCU_PTSxCR_GEN_BIT(SUSPEND));
  1252. writel(pts_control_value, &iport->port_task_scheduler_registers->control);
  1253. }
  1254. static void sci_port_post_dummy_remote_node(struct isci_port *iport)
  1255. {
  1256. struct isci_host *ihost = iport->owning_controller;
  1257. u8 phys_index = iport->physical_port_index;
  1258. union scu_remote_node_context *rnc;
  1259. u16 rni = iport->reserved_rni;
  1260. u32 command;
  1261. rnc = &ihost->remote_node_context_table[rni];
  1262. rnc->ssp.is_valid = true;
  1263. command = SCU_CONTEXT_COMMAND_POST_RNC_32 |
  1264. phys_index << SCU_CONTEXT_COMMAND_LOGICAL_PORT_SHIFT | rni;
  1265. sci_controller_post_request(ihost, command);
  1266. /* ensure hardware has seen the post rnc command and give it
  1267. * ample time to act before sending the suspend
  1268. */
  1269. readl(&ihost->smu_registers->interrupt_status); /* flush */
  1270. udelay(10);
  1271. command = SCU_CONTEXT_COMMAND_POST_RNC_SUSPEND_TX_RX |
  1272. phys_index << SCU_CONTEXT_COMMAND_LOGICAL_PORT_SHIFT | rni;
  1273. sci_controller_post_request(ihost, command);
  1274. }
  1275. static void sci_port_stopped_state_enter(struct sci_base_state_machine *sm)
  1276. {
  1277. struct isci_port *iport = container_of(sm, typeof(*iport), sm);
  1278. if (iport->sm.previous_state_id == SCI_PORT_STOPPING) {
  1279. /*
  1280. * If we enter this state becasuse of a request to stop
  1281. * the port then we want to disable the hardwares port
  1282. * task scheduler. */
  1283. sci_port_disable_port_task_scheduler(iport);
  1284. }
  1285. }
  1286. static void sci_port_stopped_state_exit(struct sci_base_state_machine *sm)
  1287. {
  1288. struct isci_port *iport = container_of(sm, typeof(*iport), sm);
  1289. /* Enable and suspend the port task scheduler */
  1290. sci_port_enable_port_task_scheduler(iport);
  1291. }
  1292. static void sci_port_ready_state_enter(struct sci_base_state_machine *sm)
  1293. {
  1294. struct isci_port *iport = container_of(sm, typeof(*iport), sm);
  1295. struct isci_host *ihost = iport->owning_controller;
  1296. u32 prev_state;
  1297. prev_state = iport->sm.previous_state_id;
  1298. if (prev_state == SCI_PORT_RESETTING)
  1299. isci_port_hard_reset_complete(iport, SCI_SUCCESS);
  1300. else
  1301. isci_port_not_ready(ihost, iport);
  1302. /* Post and suspend the dummy remote node context for this port. */
  1303. sci_port_post_dummy_remote_node(iport);
  1304. /* Start the ready substate machine */
  1305. port_state_machine_change(iport,
  1306. SCI_PORT_SUB_WAITING);
  1307. }
  1308. static void sci_port_resetting_state_exit(struct sci_base_state_machine *sm)
  1309. {
  1310. struct isci_port *iport = container_of(sm, typeof(*iport), sm);
  1311. sci_del_timer(&iport->timer);
  1312. }
  1313. static void sci_port_stopping_state_exit(struct sci_base_state_machine *sm)
  1314. {
  1315. struct isci_port *iport = container_of(sm, typeof(*iport), sm);
  1316. sci_del_timer(&iport->timer);
  1317. sci_port_destroy_dummy_resources(iport);
  1318. }
  1319. static void sci_port_failed_state_enter(struct sci_base_state_machine *sm)
  1320. {
  1321. struct isci_port *iport = container_of(sm, typeof(*iport), sm);
  1322. isci_port_hard_reset_complete(iport, SCI_FAILURE_TIMEOUT);
  1323. }
  1324. /* --------------------------------------------------------------------------- */
  1325. static const struct sci_base_state sci_port_state_table[] = {
  1326. [SCI_PORT_STOPPED] = {
  1327. .enter_state = sci_port_stopped_state_enter,
  1328. .exit_state = sci_port_stopped_state_exit
  1329. },
  1330. [SCI_PORT_STOPPING] = {
  1331. .exit_state = sci_port_stopping_state_exit
  1332. },
  1333. [SCI_PORT_READY] = {
  1334. .enter_state = sci_port_ready_state_enter,
  1335. },
  1336. [SCI_PORT_SUB_WAITING] = {
  1337. .enter_state = sci_port_ready_substate_waiting_enter,
  1338. },
  1339. [SCI_PORT_SUB_OPERATIONAL] = {
  1340. .enter_state = sci_port_ready_substate_operational_enter,
  1341. .exit_state = sci_port_ready_substate_operational_exit
  1342. },
  1343. [SCI_PORT_SUB_CONFIGURING] = {
  1344. .enter_state = sci_port_ready_substate_configuring_enter,
  1345. .exit_state = sci_port_ready_substate_configuring_exit
  1346. },
  1347. [SCI_PORT_RESETTING] = {
  1348. .exit_state = sci_port_resetting_state_exit
  1349. },
  1350. [SCI_PORT_FAILED] = {
  1351. .enter_state = sci_port_failed_state_enter,
  1352. }
  1353. };
  1354. void sci_port_construct(struct isci_port *iport, u8 index,
  1355. struct isci_host *ihost)
  1356. {
  1357. sci_init_sm(&iport->sm, sci_port_state_table, SCI_PORT_STOPPED);
  1358. iport->logical_port_index = SCIC_SDS_DUMMY_PORT;
  1359. iport->physical_port_index = index;
  1360. iport->active_phy_mask = 0;
  1361. iport->last_active_phy = 0;
  1362. iport->ready_exit = false;
  1363. iport->owning_controller = ihost;
  1364. iport->started_request_count = 0;
  1365. iport->assigned_device_count = 0;
  1366. iport->reserved_rni = SCU_DUMMY_INDEX;
  1367. iport->reserved_tag = SCI_CONTROLLER_INVALID_IO_TAG;
  1368. sci_init_timer(&iport->timer, port_timeout);
  1369. iport->port_task_scheduler_registers = NULL;
  1370. for (index = 0; index < SCI_MAX_PHYS; index++)
  1371. iport->phy_table[index] = NULL;
  1372. }
  1373. void isci_port_init(struct isci_port *iport, struct isci_host *ihost, int index)
  1374. {
  1375. INIT_LIST_HEAD(&iport->remote_dev_list);
  1376. INIT_LIST_HEAD(&iport->domain_dev_list);
  1377. spin_lock_init(&iport->state_lock);
  1378. init_completion(&iport->start_complete);
  1379. iport->isci_host = ihost;
  1380. isci_port_change_state(iport, isci_freed);
  1381. }
  1382. /**
  1383. * isci_port_get_state() - This function gets the status of the port object.
  1384. * @isci_port: This parameter points to the isci_port object
  1385. *
  1386. * status of the object as a isci_status enum.
  1387. */
  1388. enum isci_status isci_port_get_state(
  1389. struct isci_port *isci_port)
  1390. {
  1391. return isci_port->status;
  1392. }
  1393. void sci_port_broadcast_change_received(struct isci_port *iport, struct isci_phy *iphy)
  1394. {
  1395. struct isci_host *ihost = iport->owning_controller;
  1396. /* notify the user. */
  1397. isci_port_bc_change_received(ihost, iport, iphy);
  1398. }
  1399. int isci_port_perform_hard_reset(struct isci_host *ihost, struct isci_port *iport,
  1400. struct isci_phy *iphy)
  1401. {
  1402. unsigned long flags;
  1403. enum sci_status status;
  1404. int ret = TMF_RESP_FUNC_COMPLETE;
  1405. dev_dbg(&ihost->pdev->dev, "%s: iport = %p\n",
  1406. __func__, iport);
  1407. init_completion(&iport->hard_reset_complete);
  1408. spin_lock_irqsave(&ihost->scic_lock, flags);
  1409. #define ISCI_PORT_RESET_TIMEOUT SCIC_SDS_SIGNATURE_FIS_TIMEOUT
  1410. status = sci_port_hard_reset(iport, ISCI_PORT_RESET_TIMEOUT);
  1411. spin_unlock_irqrestore(&ihost->scic_lock, flags);
  1412. if (status == SCI_SUCCESS) {
  1413. wait_for_completion(&iport->hard_reset_complete);
  1414. dev_dbg(&ihost->pdev->dev,
  1415. "%s: iport = %p; hard reset completion\n",
  1416. __func__, iport);
  1417. if (iport->hard_reset_status != SCI_SUCCESS) {
  1418. ret = TMF_RESP_FUNC_FAILED;
  1419. dev_err(&ihost->pdev->dev,
  1420. "%s: iport = %p; hard reset failed (0x%x)\n",
  1421. __func__, iport, iport->hard_reset_status);
  1422. }
  1423. } else {
  1424. ret = TMF_RESP_FUNC_FAILED;
  1425. dev_err(&ihost->pdev->dev,
  1426. "%s: iport = %p; sci_port_hard_reset call"
  1427. " failed 0x%x\n",
  1428. __func__, iport, status);
  1429. }
  1430. /* If the hard reset for the port has failed, consider this
  1431. * the same as link failures on all phys in the port.
  1432. */
  1433. if (ret != TMF_RESP_FUNC_COMPLETE) {
  1434. dev_err(&ihost->pdev->dev,
  1435. "%s: iport = %p; hard reset failed "
  1436. "(0x%x) - driving explicit link fail for all phys\n",
  1437. __func__, iport, iport->hard_reset_status);
  1438. }
  1439. return ret;
  1440. }
  1441. /**
  1442. * isci_port_deformed() - This function is called by libsas when a port becomes
  1443. * inactive.
  1444. * @phy: This parameter specifies the libsas phy with the inactive port.
  1445. *
  1446. */
  1447. void isci_port_deformed(struct asd_sas_phy *phy)
  1448. {
  1449. pr_debug("%s: sas_phy = %p\n", __func__, phy);
  1450. }
  1451. /**
  1452. * isci_port_formed() - This function is called by libsas when a port becomes
  1453. * active.
  1454. * @phy: This parameter specifies the libsas phy with the active port.
  1455. *
  1456. */
  1457. void isci_port_formed(struct asd_sas_phy *phy)
  1458. {
  1459. pr_debug("%s: sas_phy = %p, sas_port = %p\n", __func__, phy, phy->port);
  1460. }