port.c 59 KB

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