port.c 52 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749
  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. 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_resume_phy(struct isci_port *iport, struct isci_phy *iphy)
  555. {
  556. sci_phy_resume(iphy);
  557. iport->enabled_phy_mask |= 1 << iphy->phy_index;
  558. }
  559. static void sci_port_activate_phy(struct isci_port *iport,
  560. struct isci_phy *iphy,
  561. u8 flags)
  562. {
  563. struct isci_host *ihost = iport->owning_controller;
  564. if (iphy->protocol != SCIC_SDS_PHY_PROTOCOL_SATA && (flags & PF_RESUME))
  565. sci_phy_resume(iphy);
  566. iport->active_phy_mask |= 1 << iphy->phy_index;
  567. sci_controller_clear_invalid_phy(ihost, iphy);
  568. if (flags & PF_NOTIFY)
  569. isci_port_link_up(ihost, iport, iphy);
  570. }
  571. void sci_port_deactivate_phy(struct isci_port *iport, struct isci_phy *iphy,
  572. bool do_notify_user)
  573. {
  574. struct isci_host *ihost = iport->owning_controller;
  575. iport->active_phy_mask &= ~(1 << iphy->phy_index);
  576. iport->enabled_phy_mask &= ~(1 << iphy->phy_index);
  577. if (!iport->active_phy_mask)
  578. iport->last_active_phy = iphy->phy_index;
  579. iphy->max_negotiated_speed = SAS_LINK_RATE_UNKNOWN;
  580. /* Re-assign the phy back to the LP as if it were a narrow port for APC
  581. * mode. For MPC mode, the phy will remain in the port.
  582. */
  583. if (iport->owning_controller->oem_parameters.controller.mode_type ==
  584. SCIC_PORT_AUTOMATIC_CONFIGURATION_MODE)
  585. writel(iphy->phy_index,
  586. &iport->port_pe_configuration_register[iphy->phy_index]);
  587. if (do_notify_user == true)
  588. isci_port_link_down(ihost, iphy, iport);
  589. }
  590. static void sci_port_invalid_link_up(struct isci_port *iport, struct isci_phy *iphy)
  591. {
  592. struct isci_host *ihost = iport->owning_controller;
  593. /*
  594. * Check to see if we have alreay reported this link as bad and if
  595. * not go ahead and tell the SCI_USER that we have discovered an
  596. * invalid link.
  597. */
  598. if ((ihost->invalid_phy_mask & (1 << iphy->phy_index)) == 0) {
  599. ihost->invalid_phy_mask |= 1 << iphy->phy_index;
  600. dev_warn(&ihost->pdev->dev, "Invalid link up!\n");
  601. }
  602. }
  603. /**
  604. * sci_port_general_link_up_handler - phy can be assigned to port?
  605. * @sci_port: sci_port object for which has a phy that has gone link up.
  606. * @sci_phy: This is the struct isci_phy object that has gone link up.
  607. * @flags: PF_RESUME, PF_NOTIFY to sci_port_activate_phy
  608. *
  609. * Determine if this phy can be assigned to this port . If the phy is
  610. * not a valid PHY for this port then the function will notify the user.
  611. * A PHY can only be part of a port if it's attached SAS ADDRESS is the
  612. * same as all other PHYs in the same port.
  613. */
  614. static void sci_port_general_link_up_handler(struct isci_port *iport,
  615. struct isci_phy *iphy,
  616. u8 flags)
  617. {
  618. struct sci_sas_address port_sas_address;
  619. struct sci_sas_address phy_sas_address;
  620. sci_port_get_attached_sas_address(iport, &port_sas_address);
  621. sci_phy_get_attached_sas_address(iphy, &phy_sas_address);
  622. /* If the SAS address of the new phy matches the SAS address of
  623. * other phys in the port OR this is the first phy in the port,
  624. * then activate the phy and allow it to be used for operations
  625. * in this port.
  626. */
  627. if ((phy_sas_address.high == port_sas_address.high &&
  628. phy_sas_address.low == port_sas_address.low) ||
  629. iport->active_phy_mask == 0) {
  630. struct sci_base_state_machine *sm = &iport->sm;
  631. sci_port_activate_phy(iport, iphy, flags);
  632. if (sm->current_state_id == SCI_PORT_RESETTING)
  633. port_state_machine_change(iport, SCI_PORT_READY);
  634. } else
  635. sci_port_invalid_link_up(iport, iphy);
  636. }
  637. /**
  638. * This method returns false if the port only has a single phy object assigned.
  639. * If there are no phys or more than one phy then the method will return
  640. * true.
  641. * @sci_port: The port for which the wide port condition is to be checked.
  642. *
  643. * bool true Is returned if this is a wide ported port. false Is returned if
  644. * this is a narrow port.
  645. */
  646. static bool sci_port_is_wide(struct isci_port *iport)
  647. {
  648. u32 index;
  649. u32 phy_count = 0;
  650. for (index = 0; index < SCI_MAX_PHYS; index++) {
  651. if (iport->phy_table[index] != NULL) {
  652. phy_count++;
  653. }
  654. }
  655. return phy_count != 1;
  656. }
  657. /**
  658. * This method is called by the PHY object when the link is detected. if the
  659. * port wants the PHY to continue on to the link up state then the port
  660. * layer must return true. If the port object returns false the phy object
  661. * must halt its attempt to go link up.
  662. * @sci_port: The port associated with the phy object.
  663. * @sci_phy: The phy object that is trying to go link up.
  664. *
  665. * true if the phy object can continue to the link up condition. true Is
  666. * returned if this phy can continue to the ready state. false Is returned if
  667. * can not continue on to the ready state. This notification is in place for
  668. * wide ports and direct attached phys. Since there are no wide ported SATA
  669. * devices this could become an invalid port configuration.
  670. */
  671. bool sci_port_link_detected(
  672. struct isci_port *iport,
  673. struct isci_phy *iphy)
  674. {
  675. if ((iport->logical_port_index != SCIC_SDS_DUMMY_PORT) &&
  676. (iphy->protocol == SCIC_SDS_PHY_PROTOCOL_SATA)) {
  677. if (sci_port_is_wide(iport)) {
  678. sci_port_invalid_link_up(iport, iphy);
  679. return false;
  680. } else {
  681. struct isci_host *ihost = iport->owning_controller;
  682. struct isci_port *dst_port = &(ihost->ports[iphy->phy_index]);
  683. writel(iphy->phy_index,
  684. &dst_port->port_pe_configuration_register[iphy->phy_index]);
  685. }
  686. }
  687. return true;
  688. }
  689. static void port_timeout(unsigned long data)
  690. {
  691. struct sci_timer *tmr = (struct sci_timer *)data;
  692. struct isci_port *iport = container_of(tmr, typeof(*iport), timer);
  693. struct isci_host *ihost = iport->owning_controller;
  694. unsigned long flags;
  695. u32 current_state;
  696. spin_lock_irqsave(&ihost->scic_lock, flags);
  697. if (tmr->cancel)
  698. goto done;
  699. current_state = iport->sm.current_state_id;
  700. if (current_state == SCI_PORT_RESETTING) {
  701. /* if the port is still in the resetting state then the timeout
  702. * fired before the reset completed.
  703. */
  704. port_state_machine_change(iport, SCI_PORT_FAILED);
  705. } else if (current_state == SCI_PORT_STOPPED) {
  706. /* if the port is stopped then the start request failed In this
  707. * case stay in the stopped state.
  708. */
  709. dev_err(sciport_to_dev(iport),
  710. "%s: SCIC Port 0x%p failed to stop before tiemout.\n",
  711. __func__,
  712. iport);
  713. } else if (current_state == SCI_PORT_STOPPING) {
  714. /* if the port is still stopping then the stop has not completed */
  715. isci_port_stop_complete(iport->owning_controller,
  716. iport,
  717. SCI_FAILURE_TIMEOUT);
  718. } else {
  719. /* The port is in the ready state and we have a timer
  720. * reporting a timeout this should not happen.
  721. */
  722. dev_err(sciport_to_dev(iport),
  723. "%s: SCIC Port 0x%p is processing a timeout operation "
  724. "in state %d.\n", __func__, iport, current_state);
  725. }
  726. done:
  727. spin_unlock_irqrestore(&ihost->scic_lock, flags);
  728. }
  729. /* --------------------------------------------------------------------------- */
  730. /**
  731. * This function updates the hardwares VIIT entry for this port.
  732. *
  733. *
  734. */
  735. static void sci_port_update_viit_entry(struct isci_port *iport)
  736. {
  737. struct sci_sas_address sas_address;
  738. sci_port_get_sas_address(iport, &sas_address);
  739. writel(sas_address.high,
  740. &iport->viit_registers->initiator_sas_address_hi);
  741. writel(sas_address.low,
  742. &iport->viit_registers->initiator_sas_address_lo);
  743. /* This value get cleared just in case its not already cleared */
  744. writel(0, &iport->viit_registers->reserved);
  745. /* We are required to update the status register last */
  746. writel(SCU_VIIT_ENTRY_ID_VIIT |
  747. SCU_VIIT_IPPT_INITIATOR |
  748. ((1 << iport->physical_port_index) << SCU_VIIT_ENTRY_LPVIE_SHIFT) |
  749. SCU_VIIT_STATUS_ALL_VALID,
  750. &iport->viit_registers->status);
  751. }
  752. enum sas_linkrate sci_port_get_max_allowed_speed(struct isci_port *iport)
  753. {
  754. u16 index;
  755. struct isci_phy *iphy;
  756. enum sas_linkrate max_allowed_speed = SAS_LINK_RATE_6_0_GBPS;
  757. /*
  758. * Loop through all of the phys in this port and find the phy with the
  759. * lowest maximum link rate. */
  760. for (index = 0; index < SCI_MAX_PHYS; index++) {
  761. iphy = iport->phy_table[index];
  762. if (iphy && sci_port_active_phy(iport, iphy) &&
  763. iphy->max_negotiated_speed < max_allowed_speed)
  764. max_allowed_speed = iphy->max_negotiated_speed;
  765. }
  766. return max_allowed_speed;
  767. }
  768. static void sci_port_suspend_port_task_scheduler(struct isci_port *iport)
  769. {
  770. u32 pts_control_value;
  771. pts_control_value = readl(&iport->port_task_scheduler_registers->control);
  772. pts_control_value |= SCU_PTSxCR_GEN_BIT(SUSPEND);
  773. writel(pts_control_value, &iport->port_task_scheduler_registers->control);
  774. }
  775. /**
  776. * sci_port_post_dummy_request() - post dummy/workaround request
  777. * @sci_port: port to post task
  778. *
  779. * Prevent the hardware scheduler from posting new requests to the front
  780. * of the scheduler queue causing a starvation problem for currently
  781. * ongoing requests.
  782. *
  783. */
  784. static void sci_port_post_dummy_request(struct isci_port *iport)
  785. {
  786. struct isci_host *ihost = iport->owning_controller;
  787. u16 tag = iport->reserved_tag;
  788. struct scu_task_context *tc;
  789. u32 command;
  790. tc = &ihost->task_context_table[ISCI_TAG_TCI(tag)];
  791. tc->abort = 0;
  792. command = SCU_CONTEXT_COMMAND_REQUEST_TYPE_POST_TC |
  793. iport->physical_port_index << SCU_CONTEXT_COMMAND_LOGICAL_PORT_SHIFT |
  794. ISCI_TAG_TCI(tag);
  795. sci_controller_post_request(ihost, command);
  796. }
  797. /**
  798. * This routine will abort the dummy request. This will alow the hardware to
  799. * power down parts of the silicon to save power.
  800. *
  801. * @sci_port: The port on which the task must be aborted.
  802. *
  803. */
  804. static void sci_port_abort_dummy_request(struct isci_port *iport)
  805. {
  806. struct isci_host *ihost = iport->owning_controller;
  807. u16 tag = iport->reserved_tag;
  808. struct scu_task_context *tc;
  809. u32 command;
  810. tc = &ihost->task_context_table[ISCI_TAG_TCI(tag)];
  811. tc->abort = 1;
  812. command = SCU_CONTEXT_COMMAND_REQUEST_POST_TC_ABORT |
  813. iport->physical_port_index << SCU_CONTEXT_COMMAND_LOGICAL_PORT_SHIFT |
  814. ISCI_TAG_TCI(tag);
  815. sci_controller_post_request(ihost, command);
  816. }
  817. /**
  818. *
  819. * @sci_port: This is the struct isci_port object to resume.
  820. *
  821. * This method will resume the port task scheduler for this port object. none
  822. */
  823. static void
  824. sci_port_resume_port_task_scheduler(struct isci_port *iport)
  825. {
  826. u32 pts_control_value;
  827. pts_control_value = readl(&iport->port_task_scheduler_registers->control);
  828. pts_control_value &= ~SCU_PTSxCR_GEN_BIT(SUSPEND);
  829. writel(pts_control_value, &iport->port_task_scheduler_registers->control);
  830. }
  831. static void sci_port_ready_substate_waiting_enter(struct sci_base_state_machine *sm)
  832. {
  833. struct isci_port *iport = container_of(sm, typeof(*iport), sm);
  834. sci_port_suspend_port_task_scheduler(iport);
  835. iport->not_ready_reason = SCIC_PORT_NOT_READY_NO_ACTIVE_PHYS;
  836. if (iport->active_phy_mask != 0) {
  837. /* At least one of the phys on the port is ready */
  838. port_state_machine_change(iport,
  839. SCI_PORT_SUB_OPERATIONAL);
  840. }
  841. }
  842. static void scic_sds_port_ready_substate_waiting_exit(
  843. struct sci_base_state_machine *sm)
  844. {
  845. struct isci_port *iport = container_of(sm, typeof(*iport), sm);
  846. sci_port_resume_port_task_scheduler(iport);
  847. }
  848. static void sci_port_ready_substate_operational_enter(struct sci_base_state_machine *sm)
  849. {
  850. u32 index;
  851. struct isci_port *iport = container_of(sm, typeof(*iport), sm);
  852. struct isci_host *ihost = iport->owning_controller;
  853. isci_port_ready(ihost, iport);
  854. for (index = 0; index < SCI_MAX_PHYS; index++) {
  855. if (iport->phy_table[index]) {
  856. writel(iport->physical_port_index,
  857. &iport->port_pe_configuration_register[
  858. iport->phy_table[index]->phy_index]);
  859. if (((iport->active_phy_mask^iport->enabled_phy_mask) & (1 << index)) != 0)
  860. sci_port_resume_phy(iport, iport->phy_table[index]);
  861. }
  862. }
  863. sci_port_update_viit_entry(iport);
  864. /*
  865. * Post the dummy task for the port so the hardware can schedule
  866. * io correctly
  867. */
  868. sci_port_post_dummy_request(iport);
  869. }
  870. static void sci_port_invalidate_dummy_remote_node(struct isci_port *iport)
  871. {
  872. struct isci_host *ihost = iport->owning_controller;
  873. u8 phys_index = iport->physical_port_index;
  874. union scu_remote_node_context *rnc;
  875. u16 rni = iport->reserved_rni;
  876. u32 command;
  877. rnc = &ihost->remote_node_context_table[rni];
  878. rnc->ssp.is_valid = false;
  879. /* ensure the preceding tc abort request has reached the
  880. * controller and give it ample time to act before posting the rnc
  881. * invalidate
  882. */
  883. readl(&ihost->smu_registers->interrupt_status); /* flush */
  884. udelay(10);
  885. command = SCU_CONTEXT_COMMAND_POST_RNC_INVALIDATE |
  886. phys_index << SCU_CONTEXT_COMMAND_LOGICAL_PORT_SHIFT | rni;
  887. sci_controller_post_request(ihost, command);
  888. }
  889. /**
  890. *
  891. * @object: This is the object which is cast to a struct isci_port object.
  892. *
  893. * This method will perform the actions required by the struct isci_port on
  894. * exiting the SCI_PORT_SUB_OPERATIONAL. This function reports
  895. * the port not ready and suspends the port task scheduler. none
  896. */
  897. static void sci_port_ready_substate_operational_exit(struct sci_base_state_machine *sm)
  898. {
  899. struct isci_port *iport = container_of(sm, typeof(*iport), sm);
  900. struct isci_host *ihost = iport->owning_controller;
  901. /*
  902. * Kill the dummy task for this port if it has not yet posted
  903. * the hardware will treat this as a NOP and just return abort
  904. * complete.
  905. */
  906. sci_port_abort_dummy_request(iport);
  907. isci_port_not_ready(ihost, iport);
  908. if (iport->ready_exit)
  909. sci_port_invalidate_dummy_remote_node(iport);
  910. }
  911. static void sci_port_ready_substate_configuring_enter(struct sci_base_state_machine *sm)
  912. {
  913. struct isci_port *iport = container_of(sm, typeof(*iport), sm);
  914. struct isci_host *ihost = iport->owning_controller;
  915. if (iport->active_phy_mask == 0) {
  916. isci_port_not_ready(ihost, iport);
  917. port_state_machine_change(iport, SCI_PORT_SUB_WAITING);
  918. } else
  919. port_state_machine_change(iport, SCI_PORT_SUB_OPERATIONAL);
  920. }
  921. enum sci_status sci_port_start(struct isci_port *iport)
  922. {
  923. struct isci_host *ihost = iport->owning_controller;
  924. enum sci_status status = SCI_SUCCESS;
  925. enum sci_port_states state;
  926. u32 phy_mask;
  927. state = iport->sm.current_state_id;
  928. if (state != SCI_PORT_STOPPED) {
  929. dev_warn(sciport_to_dev(iport),
  930. "%s: in wrong state: %d\n", __func__, state);
  931. return SCI_FAILURE_INVALID_STATE;
  932. }
  933. if (iport->assigned_device_count > 0) {
  934. /* TODO This is a start failure operation because
  935. * there are still devices assigned to this port.
  936. * There must be no devices assigned to a port on a
  937. * start operation.
  938. */
  939. return SCI_FAILURE_UNSUPPORTED_PORT_CONFIGURATION;
  940. }
  941. if (iport->reserved_rni == SCU_DUMMY_INDEX) {
  942. u16 rni = sci_remote_node_table_allocate_remote_node(
  943. &ihost->available_remote_nodes, 1);
  944. if (rni != SCU_DUMMY_INDEX)
  945. sci_port_construct_dummy_rnc(iport, rni);
  946. else
  947. status = SCI_FAILURE_INSUFFICIENT_RESOURCES;
  948. iport->reserved_rni = rni;
  949. }
  950. if (iport->reserved_tag == SCI_CONTROLLER_INVALID_IO_TAG) {
  951. u16 tag;
  952. tag = isci_alloc_tag(ihost);
  953. if (tag == SCI_CONTROLLER_INVALID_IO_TAG)
  954. status = SCI_FAILURE_INSUFFICIENT_RESOURCES;
  955. else
  956. sci_port_construct_dummy_task(iport, tag);
  957. iport->reserved_tag = tag;
  958. }
  959. if (status == SCI_SUCCESS) {
  960. phy_mask = sci_port_get_phys(iport);
  961. /*
  962. * There are one or more phys assigned to this port. Make sure
  963. * the port's phy mask is in fact legal and supported by the
  964. * silicon.
  965. */
  966. if (sci_port_is_phy_mask_valid(iport, phy_mask) == true) {
  967. port_state_machine_change(iport,
  968. SCI_PORT_READY);
  969. return SCI_SUCCESS;
  970. }
  971. status = SCI_FAILURE;
  972. }
  973. if (status != SCI_SUCCESS)
  974. sci_port_destroy_dummy_resources(iport);
  975. return status;
  976. }
  977. enum sci_status sci_port_stop(struct isci_port *iport)
  978. {
  979. enum sci_port_states state;
  980. state = iport->sm.current_state_id;
  981. switch (state) {
  982. case SCI_PORT_STOPPED:
  983. return SCI_SUCCESS;
  984. case SCI_PORT_SUB_WAITING:
  985. case SCI_PORT_SUB_OPERATIONAL:
  986. case SCI_PORT_SUB_CONFIGURING:
  987. case SCI_PORT_RESETTING:
  988. port_state_machine_change(iport,
  989. SCI_PORT_STOPPING);
  990. return SCI_SUCCESS;
  991. default:
  992. dev_warn(sciport_to_dev(iport),
  993. "%s: in wrong state: %d\n", __func__, state);
  994. return SCI_FAILURE_INVALID_STATE;
  995. }
  996. }
  997. static enum sci_status sci_port_hard_reset(struct isci_port *iport, u32 timeout)
  998. {
  999. enum sci_status status = SCI_FAILURE_INVALID_PHY;
  1000. struct isci_phy *iphy = NULL;
  1001. enum sci_port_states state;
  1002. u32 phy_index;
  1003. state = iport->sm.current_state_id;
  1004. if (state != SCI_PORT_SUB_OPERATIONAL) {
  1005. dev_warn(sciport_to_dev(iport),
  1006. "%s: in wrong state: %d\n", __func__, state);
  1007. return SCI_FAILURE_INVALID_STATE;
  1008. }
  1009. /* Select a phy on which we can send the hard reset request. */
  1010. for (phy_index = 0; phy_index < SCI_MAX_PHYS && !iphy; phy_index++) {
  1011. iphy = iport->phy_table[phy_index];
  1012. if (iphy && !sci_port_active_phy(iport, iphy)) {
  1013. /*
  1014. * We found a phy but it is not ready select
  1015. * different phy
  1016. */
  1017. iphy = NULL;
  1018. }
  1019. }
  1020. /* If we have a phy then go ahead and start the reset procedure */
  1021. if (!iphy)
  1022. return status;
  1023. status = sci_phy_reset(iphy);
  1024. if (status != SCI_SUCCESS)
  1025. return status;
  1026. sci_mod_timer(&iport->timer, timeout);
  1027. iport->not_ready_reason = SCIC_PORT_NOT_READY_HARD_RESET_REQUESTED;
  1028. port_state_machine_change(iport, SCI_PORT_RESETTING);
  1029. return SCI_SUCCESS;
  1030. }
  1031. /**
  1032. * sci_port_add_phy() -
  1033. * @sci_port: This parameter specifies the port in which the phy will be added.
  1034. * @sci_phy: This parameter is the phy which is to be added to the port.
  1035. *
  1036. * This method will add a PHY to the selected port. This method returns an
  1037. * enum sci_status. SCI_SUCCESS the phy has been added to the port. Any other
  1038. * status is a failure to add the phy to the port.
  1039. */
  1040. enum sci_status sci_port_add_phy(struct isci_port *iport,
  1041. struct isci_phy *iphy)
  1042. {
  1043. enum sci_status status;
  1044. enum sci_port_states state;
  1045. state = iport->sm.current_state_id;
  1046. switch (state) {
  1047. case SCI_PORT_STOPPED: {
  1048. struct sci_sas_address port_sas_address;
  1049. /* Read the port assigned SAS Address if there is one */
  1050. sci_port_get_sas_address(iport, &port_sas_address);
  1051. if (port_sas_address.high != 0 && port_sas_address.low != 0) {
  1052. struct sci_sas_address phy_sas_address;
  1053. /* Make sure that the PHY SAS Address matches the SAS Address
  1054. * for this port
  1055. */
  1056. sci_phy_get_sas_address(iphy, &phy_sas_address);
  1057. if (port_sas_address.high != phy_sas_address.high ||
  1058. port_sas_address.low != phy_sas_address.low)
  1059. return SCI_FAILURE_UNSUPPORTED_PORT_CONFIGURATION;
  1060. }
  1061. return sci_port_set_phy(iport, iphy);
  1062. }
  1063. case SCI_PORT_SUB_WAITING:
  1064. case SCI_PORT_SUB_OPERATIONAL:
  1065. status = sci_port_set_phy(iport, iphy);
  1066. if (status != SCI_SUCCESS)
  1067. return status;
  1068. sci_port_general_link_up_handler(iport, iphy, PF_NOTIFY|PF_RESUME);
  1069. iport->not_ready_reason = SCIC_PORT_NOT_READY_RECONFIGURING;
  1070. port_state_machine_change(iport, SCI_PORT_SUB_CONFIGURING);
  1071. return status;
  1072. case SCI_PORT_SUB_CONFIGURING:
  1073. status = sci_port_set_phy(iport, iphy);
  1074. if (status != SCI_SUCCESS)
  1075. return status;
  1076. sci_port_general_link_up_handler(iport, iphy, PF_NOTIFY);
  1077. /* Re-enter the configuring state since this may be the last phy in
  1078. * the port.
  1079. */
  1080. port_state_machine_change(iport,
  1081. SCI_PORT_SUB_CONFIGURING);
  1082. return SCI_SUCCESS;
  1083. default:
  1084. dev_warn(sciport_to_dev(iport),
  1085. "%s: in wrong state: %d\n", __func__, state);
  1086. return SCI_FAILURE_INVALID_STATE;
  1087. }
  1088. }
  1089. /**
  1090. * sci_port_remove_phy() -
  1091. * @sci_port: This parameter specifies the port in which the phy will be added.
  1092. * @sci_phy: This parameter is the phy which is to be added to the port.
  1093. *
  1094. * This method will remove the PHY from the selected PORT. This method returns
  1095. * an enum sci_status. SCI_SUCCESS the phy has been removed from the port. Any
  1096. * other status is a failure to add the phy to the port.
  1097. */
  1098. enum sci_status sci_port_remove_phy(struct isci_port *iport,
  1099. struct isci_phy *iphy)
  1100. {
  1101. enum sci_status status;
  1102. enum sci_port_states state;
  1103. state = iport->sm.current_state_id;
  1104. switch (state) {
  1105. case SCI_PORT_STOPPED:
  1106. return sci_port_clear_phy(iport, iphy);
  1107. case SCI_PORT_SUB_OPERATIONAL:
  1108. status = sci_port_clear_phy(iport, iphy);
  1109. if (status != SCI_SUCCESS)
  1110. return status;
  1111. sci_port_deactivate_phy(iport, iphy, true);
  1112. iport->not_ready_reason = SCIC_PORT_NOT_READY_RECONFIGURING;
  1113. port_state_machine_change(iport,
  1114. SCI_PORT_SUB_CONFIGURING);
  1115. return SCI_SUCCESS;
  1116. case SCI_PORT_SUB_CONFIGURING:
  1117. status = sci_port_clear_phy(iport, iphy);
  1118. if (status != SCI_SUCCESS)
  1119. return status;
  1120. sci_port_deactivate_phy(iport, iphy, true);
  1121. /* Re-enter the configuring state since this may be the last phy in
  1122. * the port
  1123. */
  1124. port_state_machine_change(iport,
  1125. SCI_PORT_SUB_CONFIGURING);
  1126. return SCI_SUCCESS;
  1127. default:
  1128. dev_warn(sciport_to_dev(iport),
  1129. "%s: in wrong state: %d\n", __func__, state);
  1130. return SCI_FAILURE_INVALID_STATE;
  1131. }
  1132. }
  1133. enum sci_status sci_port_link_up(struct isci_port *iport,
  1134. struct isci_phy *iphy)
  1135. {
  1136. enum sci_port_states state;
  1137. state = iport->sm.current_state_id;
  1138. switch (state) {
  1139. case SCI_PORT_SUB_WAITING:
  1140. /* Since this is the first phy going link up for the port we
  1141. * can just enable it and continue
  1142. */
  1143. sci_port_activate_phy(iport, iphy, PF_NOTIFY|PF_RESUME);
  1144. port_state_machine_change(iport,
  1145. SCI_PORT_SUB_OPERATIONAL);
  1146. return SCI_SUCCESS;
  1147. case SCI_PORT_SUB_OPERATIONAL:
  1148. sci_port_general_link_up_handler(iport, iphy, PF_NOTIFY|PF_RESUME);
  1149. return SCI_SUCCESS;
  1150. case SCI_PORT_RESETTING:
  1151. /* TODO We should make sure that the phy that has gone
  1152. * link up is the same one on which we sent the reset. It is
  1153. * possible that the phy on which we sent the reset is not the
  1154. * one that has gone link up and we want to make sure that
  1155. * phy being reset comes back. Consider the case where a
  1156. * reset is sent but before the hardware processes the reset it
  1157. * get a link up on the port because of a hot plug event.
  1158. * because of the reset request this phy will go link down
  1159. * almost immediately.
  1160. */
  1161. /* In the resetting state we don't notify the user regarding
  1162. * link up and link down notifications.
  1163. */
  1164. sci_port_general_link_up_handler(iport, iphy, PF_RESUME);
  1165. return SCI_SUCCESS;
  1166. default:
  1167. dev_warn(sciport_to_dev(iport),
  1168. "%s: in wrong state: %d\n", __func__, state);
  1169. return SCI_FAILURE_INVALID_STATE;
  1170. }
  1171. }
  1172. enum sci_status sci_port_link_down(struct isci_port *iport,
  1173. struct isci_phy *iphy)
  1174. {
  1175. enum sci_port_states state;
  1176. state = iport->sm.current_state_id;
  1177. switch (state) {
  1178. case SCI_PORT_SUB_OPERATIONAL:
  1179. sci_port_deactivate_phy(iport, iphy, true);
  1180. /* If there are no active phys left in the port, then
  1181. * transition the port to the WAITING state until such time
  1182. * as a phy goes link up
  1183. */
  1184. if (iport->active_phy_mask == 0)
  1185. port_state_machine_change(iport,
  1186. SCI_PORT_SUB_WAITING);
  1187. return SCI_SUCCESS;
  1188. case SCI_PORT_RESETTING:
  1189. /* In the resetting state we don't notify the user regarding
  1190. * link up and link down notifications. */
  1191. sci_port_deactivate_phy(iport, iphy, false);
  1192. return SCI_SUCCESS;
  1193. default:
  1194. dev_warn(sciport_to_dev(iport),
  1195. "%s: in wrong state: %d\n", __func__, state);
  1196. return SCI_FAILURE_INVALID_STATE;
  1197. }
  1198. }
  1199. enum sci_status sci_port_start_io(struct isci_port *iport,
  1200. struct isci_remote_device *idev,
  1201. struct isci_request *ireq)
  1202. {
  1203. enum sci_port_states state;
  1204. state = iport->sm.current_state_id;
  1205. switch (state) {
  1206. case SCI_PORT_SUB_WAITING:
  1207. return SCI_FAILURE_INVALID_STATE;
  1208. case SCI_PORT_SUB_OPERATIONAL:
  1209. iport->started_request_count++;
  1210. return SCI_SUCCESS;
  1211. default:
  1212. dev_warn(sciport_to_dev(iport),
  1213. "%s: in wrong state: %d\n", __func__, state);
  1214. return SCI_FAILURE_INVALID_STATE;
  1215. }
  1216. }
  1217. enum sci_status sci_port_complete_io(struct isci_port *iport,
  1218. struct isci_remote_device *idev,
  1219. struct isci_request *ireq)
  1220. {
  1221. enum sci_port_states state;
  1222. state = iport->sm.current_state_id;
  1223. switch (state) {
  1224. case SCI_PORT_STOPPED:
  1225. dev_warn(sciport_to_dev(iport),
  1226. "%s: in wrong state: %d\n", __func__, state);
  1227. return SCI_FAILURE_INVALID_STATE;
  1228. case SCI_PORT_STOPPING:
  1229. sci_port_decrement_request_count(iport);
  1230. if (iport->started_request_count == 0)
  1231. port_state_machine_change(iport,
  1232. SCI_PORT_STOPPED);
  1233. break;
  1234. case SCI_PORT_READY:
  1235. case SCI_PORT_RESETTING:
  1236. case SCI_PORT_FAILED:
  1237. case SCI_PORT_SUB_WAITING:
  1238. case SCI_PORT_SUB_OPERATIONAL:
  1239. sci_port_decrement_request_count(iport);
  1240. break;
  1241. case SCI_PORT_SUB_CONFIGURING:
  1242. sci_port_decrement_request_count(iport);
  1243. if (iport->started_request_count == 0) {
  1244. port_state_machine_change(iport,
  1245. SCI_PORT_SUB_OPERATIONAL);
  1246. }
  1247. break;
  1248. }
  1249. return SCI_SUCCESS;
  1250. }
  1251. static void sci_port_enable_port_task_scheduler(struct isci_port *iport)
  1252. {
  1253. u32 pts_control_value;
  1254. /* enable the port task scheduler in a suspended state */
  1255. pts_control_value = readl(&iport->port_task_scheduler_registers->control);
  1256. pts_control_value |= SCU_PTSxCR_GEN_BIT(ENABLE) | SCU_PTSxCR_GEN_BIT(SUSPEND);
  1257. writel(pts_control_value, &iport->port_task_scheduler_registers->control);
  1258. }
  1259. static void sci_port_disable_port_task_scheduler(struct isci_port *iport)
  1260. {
  1261. u32 pts_control_value;
  1262. pts_control_value = readl(&iport->port_task_scheduler_registers->control);
  1263. pts_control_value &=
  1264. ~(SCU_PTSxCR_GEN_BIT(ENABLE) | SCU_PTSxCR_GEN_BIT(SUSPEND));
  1265. writel(pts_control_value, &iport->port_task_scheduler_registers->control);
  1266. }
  1267. static void sci_port_post_dummy_remote_node(struct isci_port *iport)
  1268. {
  1269. struct isci_host *ihost = iport->owning_controller;
  1270. u8 phys_index = iport->physical_port_index;
  1271. union scu_remote_node_context *rnc;
  1272. u16 rni = iport->reserved_rni;
  1273. u32 command;
  1274. rnc = &ihost->remote_node_context_table[rni];
  1275. rnc->ssp.is_valid = true;
  1276. command = SCU_CONTEXT_COMMAND_POST_RNC_32 |
  1277. phys_index << SCU_CONTEXT_COMMAND_LOGICAL_PORT_SHIFT | rni;
  1278. sci_controller_post_request(ihost, command);
  1279. /* ensure hardware has seen the post rnc command and give it
  1280. * ample time to act before sending the suspend
  1281. */
  1282. readl(&ihost->smu_registers->interrupt_status); /* flush */
  1283. udelay(10);
  1284. command = SCU_CONTEXT_COMMAND_POST_RNC_SUSPEND_TX_RX |
  1285. phys_index << SCU_CONTEXT_COMMAND_LOGICAL_PORT_SHIFT | rni;
  1286. sci_controller_post_request(ihost, command);
  1287. }
  1288. static void sci_port_stopped_state_enter(struct sci_base_state_machine *sm)
  1289. {
  1290. struct isci_port *iport = container_of(sm, typeof(*iport), sm);
  1291. if (iport->sm.previous_state_id == SCI_PORT_STOPPING) {
  1292. /*
  1293. * If we enter this state becasuse of a request to stop
  1294. * the port then we want to disable the hardwares port
  1295. * task scheduler. */
  1296. sci_port_disable_port_task_scheduler(iport);
  1297. }
  1298. }
  1299. static void sci_port_stopped_state_exit(struct sci_base_state_machine *sm)
  1300. {
  1301. struct isci_port *iport = container_of(sm, typeof(*iport), sm);
  1302. /* Enable and suspend the port task scheduler */
  1303. sci_port_enable_port_task_scheduler(iport);
  1304. }
  1305. static void sci_port_ready_state_enter(struct sci_base_state_machine *sm)
  1306. {
  1307. struct isci_port *iport = container_of(sm, typeof(*iport), sm);
  1308. struct isci_host *ihost = iport->owning_controller;
  1309. u32 prev_state;
  1310. prev_state = iport->sm.previous_state_id;
  1311. if (prev_state == SCI_PORT_RESETTING)
  1312. isci_port_hard_reset_complete(iport, SCI_SUCCESS);
  1313. else
  1314. isci_port_not_ready(ihost, iport);
  1315. /* Post and suspend the dummy remote node context for this port. */
  1316. sci_port_post_dummy_remote_node(iport);
  1317. /* Start the ready substate machine */
  1318. port_state_machine_change(iport,
  1319. SCI_PORT_SUB_WAITING);
  1320. }
  1321. static void sci_port_resetting_state_exit(struct sci_base_state_machine *sm)
  1322. {
  1323. struct isci_port *iport = container_of(sm, typeof(*iport), sm);
  1324. sci_del_timer(&iport->timer);
  1325. }
  1326. static void sci_port_stopping_state_exit(struct sci_base_state_machine *sm)
  1327. {
  1328. struct isci_port *iport = container_of(sm, typeof(*iport), sm);
  1329. sci_del_timer(&iport->timer);
  1330. sci_port_destroy_dummy_resources(iport);
  1331. }
  1332. static void sci_port_failed_state_enter(struct sci_base_state_machine *sm)
  1333. {
  1334. struct isci_port *iport = container_of(sm, typeof(*iport), sm);
  1335. isci_port_hard_reset_complete(iport, SCI_FAILURE_TIMEOUT);
  1336. }
  1337. /* --------------------------------------------------------------------------- */
  1338. static const struct sci_base_state sci_port_state_table[] = {
  1339. [SCI_PORT_STOPPED] = {
  1340. .enter_state = sci_port_stopped_state_enter,
  1341. .exit_state = sci_port_stopped_state_exit
  1342. },
  1343. [SCI_PORT_STOPPING] = {
  1344. .exit_state = sci_port_stopping_state_exit
  1345. },
  1346. [SCI_PORT_READY] = {
  1347. .enter_state = sci_port_ready_state_enter,
  1348. },
  1349. [SCI_PORT_SUB_WAITING] = {
  1350. .enter_state = sci_port_ready_substate_waiting_enter,
  1351. .exit_state = scic_sds_port_ready_substate_waiting_exit,
  1352. },
  1353. [SCI_PORT_SUB_OPERATIONAL] = {
  1354. .enter_state = sci_port_ready_substate_operational_enter,
  1355. .exit_state = sci_port_ready_substate_operational_exit
  1356. },
  1357. [SCI_PORT_SUB_CONFIGURING] = {
  1358. .enter_state = sci_port_ready_substate_configuring_enter
  1359. },
  1360. [SCI_PORT_RESETTING] = {
  1361. .exit_state = sci_port_resetting_state_exit
  1362. },
  1363. [SCI_PORT_FAILED] = {
  1364. .enter_state = sci_port_failed_state_enter,
  1365. }
  1366. };
  1367. void sci_port_construct(struct isci_port *iport, u8 index,
  1368. struct isci_host *ihost)
  1369. {
  1370. sci_init_sm(&iport->sm, sci_port_state_table, SCI_PORT_STOPPED);
  1371. iport->logical_port_index = SCIC_SDS_DUMMY_PORT;
  1372. iport->physical_port_index = index;
  1373. iport->active_phy_mask = 0;
  1374. iport->enabled_phy_mask = 0;
  1375. iport->last_active_phy = 0;
  1376. iport->ready_exit = false;
  1377. iport->owning_controller = ihost;
  1378. iport->started_request_count = 0;
  1379. iport->assigned_device_count = 0;
  1380. iport->reserved_rni = SCU_DUMMY_INDEX;
  1381. iport->reserved_tag = SCI_CONTROLLER_INVALID_IO_TAG;
  1382. sci_init_timer(&iport->timer, port_timeout);
  1383. iport->port_task_scheduler_registers = NULL;
  1384. for (index = 0; index < SCI_MAX_PHYS; index++)
  1385. iport->phy_table[index] = NULL;
  1386. }
  1387. void isci_port_init(struct isci_port *iport, struct isci_host *ihost, int index)
  1388. {
  1389. INIT_LIST_HEAD(&iport->remote_dev_list);
  1390. INIT_LIST_HEAD(&iport->domain_dev_list);
  1391. spin_lock_init(&iport->state_lock);
  1392. init_completion(&iport->start_complete);
  1393. iport->isci_host = ihost;
  1394. isci_port_change_state(iport, isci_freed);
  1395. }
  1396. /**
  1397. * isci_port_get_state() - This function gets the status of the port object.
  1398. * @isci_port: This parameter points to the isci_port object
  1399. *
  1400. * status of the object as a isci_status enum.
  1401. */
  1402. enum isci_status isci_port_get_state(
  1403. struct isci_port *isci_port)
  1404. {
  1405. return isci_port->status;
  1406. }
  1407. void sci_port_broadcast_change_received(struct isci_port *iport, struct isci_phy *iphy)
  1408. {
  1409. struct isci_host *ihost = iport->owning_controller;
  1410. /* notify the user. */
  1411. isci_port_bc_change_received(ihost, iport, iphy);
  1412. }
  1413. int isci_port_perform_hard_reset(struct isci_host *ihost, struct isci_port *iport,
  1414. struct isci_phy *iphy)
  1415. {
  1416. unsigned long flags;
  1417. enum sci_status status;
  1418. int ret = TMF_RESP_FUNC_COMPLETE;
  1419. dev_dbg(&ihost->pdev->dev, "%s: iport = %p\n",
  1420. __func__, iport);
  1421. init_completion(&iport->hard_reset_complete);
  1422. spin_lock_irqsave(&ihost->scic_lock, flags);
  1423. #define ISCI_PORT_RESET_TIMEOUT SCIC_SDS_SIGNATURE_FIS_TIMEOUT
  1424. status = sci_port_hard_reset(iport, ISCI_PORT_RESET_TIMEOUT);
  1425. spin_unlock_irqrestore(&ihost->scic_lock, flags);
  1426. if (status == SCI_SUCCESS) {
  1427. wait_for_completion(&iport->hard_reset_complete);
  1428. dev_dbg(&ihost->pdev->dev,
  1429. "%s: iport = %p; hard reset completion\n",
  1430. __func__, iport);
  1431. if (iport->hard_reset_status != SCI_SUCCESS) {
  1432. ret = TMF_RESP_FUNC_FAILED;
  1433. dev_err(&ihost->pdev->dev,
  1434. "%s: iport = %p; hard reset failed (0x%x)\n",
  1435. __func__, iport, iport->hard_reset_status);
  1436. }
  1437. } else {
  1438. ret = TMF_RESP_FUNC_FAILED;
  1439. dev_err(&ihost->pdev->dev,
  1440. "%s: iport = %p; sci_port_hard_reset call"
  1441. " failed 0x%x\n",
  1442. __func__, iport, status);
  1443. }
  1444. /* If the hard reset for the port has failed, consider this
  1445. * the same as link failures on all phys in the port.
  1446. */
  1447. if (ret != TMF_RESP_FUNC_COMPLETE) {
  1448. dev_err(&ihost->pdev->dev,
  1449. "%s: iport = %p; hard reset failed "
  1450. "(0x%x) - driving explicit link fail for all phys\n",
  1451. __func__, iport, iport->hard_reset_status);
  1452. }
  1453. return ret;
  1454. }
  1455. /**
  1456. * isci_port_deformed() - This function is called by libsas when a port becomes
  1457. * inactive.
  1458. * @phy: This parameter specifies the libsas phy with the inactive port.
  1459. *
  1460. */
  1461. void isci_port_deformed(struct asd_sas_phy *phy)
  1462. {
  1463. pr_debug("%s: sas_phy = %p\n", __func__, phy);
  1464. }
  1465. /**
  1466. * isci_port_formed() - This function is called by libsas when a port becomes
  1467. * active.
  1468. * @phy: This parameter specifies the libsas phy with the active port.
  1469. *
  1470. */
  1471. void isci_port_formed(struct asd_sas_phy *phy)
  1472. {
  1473. pr_debug("%s: sas_phy = %p, sas_port = %p\n", __func__, phy, phy->port);
  1474. }