port.c 52 KB

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