host.c 87 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105210621072108210921102111211221132114211521162117211821192120212121222123212421252126212721282129213021312132213321342135213621372138213921402141214221432144214521462147214821492150215121522153215421552156215721582159216021612162216321642165216621672168216921702171217221732174217521762177217821792180218121822183218421852186218721882189219021912192219321942195219621972198219922002201220222032204220522062207220822092210221122122213221422152216221722182219222022212222222322242225222622272228222922302231223222332234223522362237223822392240224122422243224422452246224722482249225022512252225322542255225622572258225922602261226222632264226522662267226822692270227122722273227422752276227722782279228022812282228322842285228622872288228922902291229222932294229522962297229822992300230123022303230423052306230723082309231023112312231323142315231623172318231923202321232223232324232523262327232823292330233123322333233423352336233723382339234023412342234323442345234623472348234923502351235223532354235523562357235823592360236123622363236423652366236723682369237023712372237323742375237623772378237923802381238223832384238523862387238823892390239123922393239423952396239723982399240024012402240324042405240624072408240924102411241224132414241524162417241824192420242124222423242424252426242724282429243024312432243324342435243624372438243924402441244224432444244524462447244824492450245124522453245424552456245724582459246024612462246324642465246624672468246924702471247224732474247524762477247824792480248124822483248424852486248724882489249024912492249324942495249624972498249925002501250225032504250525062507250825092510251125122513251425152516251725182519252025212522252325242525252625272528252925302531253225332534253525362537253825392540254125422543254425452546254725482549255025512552255325542555255625572558255925602561256225632564256525662567256825692570257125722573257425752576257725782579258025812582258325842585258625872588258925902591259225932594259525962597259825992600260126022603260426052606260726082609261026112612261326142615261626172618261926202621262226232624262526262627262826292630263126322633263426352636263726382639264026412642264326442645264626472648264926502651265226532654265526562657265826592660266126622663266426652666266726682669267026712672267326742675267626772678267926802681268226832684268526862687268826892690269126922693269426952696269726982699270027012702270327042705270627072708270927102711271227132714271527162717271827192720272127222723272427252726272727282729273027312732273327342735273627372738273927402741274227432744274527462747274827492750275127522753275427552756275727582759276027612762276327642765276627672768276927702771277227732774277527762777277827792780278127822783278427852786278727882789279027912792279327942795279627972798279928002801280228032804280528062807280828092810281128122813281428152816281728182819282028212822282328242825282628272828282928302831283228332834283528362837283828392840284128422843284428452846284728482849285028512852285328542855285628572858285928602861286228632864286528662867286828692870287128722873287428752876287728782879288028812882288328842885288628872888288928902891289228932894289528962897289828992900290129022903290429052906290729082909291029112912291329142915291629172918291929202921292229232924292529262927292829292930293129322933293429352936293729382939294029412942294329442945294629472948294929502951295229532954295529562957295829592960296129622963296429652966296729682969297029712972297329742975297629772978297929802981298229832984298529862987298829892990299129922993299429952996299729982999300030013002300330043005300630073008300930103011301230133014301530163017301830193020302130223023302430253026
  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 <linux/circ_buf.h>
  56. #include <linux/device.h>
  57. #include <scsi/sas.h>
  58. #include "host.h"
  59. #include "isci.h"
  60. #include "port.h"
  61. #include "probe_roms.h"
  62. #include "remote_device.h"
  63. #include "request.h"
  64. #include "scu_completion_codes.h"
  65. #include "scu_event_codes.h"
  66. #include "registers.h"
  67. #include "scu_remote_node_context.h"
  68. #include "scu_task_context.h"
  69. #define SCU_CONTEXT_RAM_INIT_STALL_TIME 200
  70. #define smu_max_ports(dcc_value) \
  71. (\
  72. (((dcc_value) & SMU_DEVICE_CONTEXT_CAPACITY_MAX_LP_MASK) \
  73. >> SMU_DEVICE_CONTEXT_CAPACITY_MAX_LP_SHIFT) + 1 \
  74. )
  75. #define smu_max_task_contexts(dcc_value) \
  76. (\
  77. (((dcc_value) & SMU_DEVICE_CONTEXT_CAPACITY_MAX_TC_MASK) \
  78. >> SMU_DEVICE_CONTEXT_CAPACITY_MAX_TC_SHIFT) + 1 \
  79. )
  80. #define smu_max_rncs(dcc_value) \
  81. (\
  82. (((dcc_value) & SMU_DEVICE_CONTEXT_CAPACITY_MAX_RNC_MASK) \
  83. >> SMU_DEVICE_CONTEXT_CAPACITY_MAX_RNC_SHIFT) + 1 \
  84. )
  85. #define SCIC_SDS_CONTROLLER_PHY_START_TIMEOUT 100
  86. /**
  87. *
  88. *
  89. * The number of milliseconds to wait while a given phy is consuming power
  90. * before allowing another set of phys to consume power. Ultimately, this will
  91. * be specified by OEM parameter.
  92. */
  93. #define SCIC_SDS_CONTROLLER_POWER_CONTROL_INTERVAL 500
  94. /**
  95. * NORMALIZE_PUT_POINTER() -
  96. *
  97. * This macro will normalize the completion queue put pointer so its value can
  98. * be used as an array inde
  99. */
  100. #define NORMALIZE_PUT_POINTER(x) \
  101. ((x) & SMU_COMPLETION_QUEUE_PUT_POINTER_MASK)
  102. /**
  103. * NORMALIZE_EVENT_POINTER() -
  104. *
  105. * This macro will normalize the completion queue event entry so its value can
  106. * be used as an index.
  107. */
  108. #define NORMALIZE_EVENT_POINTER(x) \
  109. (\
  110. ((x) & SMU_COMPLETION_QUEUE_GET_EVENT_POINTER_MASK) \
  111. >> SMU_COMPLETION_QUEUE_GET_EVENT_POINTER_SHIFT \
  112. )
  113. /**
  114. * NORMALIZE_GET_POINTER() -
  115. *
  116. * This macro will normalize the completion queue get pointer so its value can
  117. * be used as an index into an array
  118. */
  119. #define NORMALIZE_GET_POINTER(x) \
  120. ((x) & SMU_COMPLETION_QUEUE_GET_POINTER_MASK)
  121. /**
  122. * NORMALIZE_GET_POINTER_CYCLE_BIT() -
  123. *
  124. * This macro will normalize the completion queue cycle pointer so it matches
  125. * the completion queue cycle bit
  126. */
  127. #define NORMALIZE_GET_POINTER_CYCLE_BIT(x) \
  128. ((SMU_CQGR_CYCLE_BIT & (x)) << (31 - SMU_COMPLETION_QUEUE_GET_CYCLE_BIT_SHIFT))
  129. /**
  130. * COMPLETION_QUEUE_CYCLE_BIT() -
  131. *
  132. * This macro will return the cycle bit of the completion queue entry
  133. */
  134. #define COMPLETION_QUEUE_CYCLE_BIT(x) ((x) & 0x80000000)
  135. /* Init the state machine and call the state entry function (if any) */
  136. void sci_init_sm(struct sci_base_state_machine *sm,
  137. const struct sci_base_state *state_table, u32 initial_state)
  138. {
  139. sci_state_transition_t handler;
  140. sm->initial_state_id = initial_state;
  141. sm->previous_state_id = initial_state;
  142. sm->current_state_id = initial_state;
  143. sm->state_table = state_table;
  144. handler = sm->state_table[initial_state].enter_state;
  145. if (handler)
  146. handler(sm);
  147. }
  148. /* Call the state exit fn, update the current state, call the state entry fn */
  149. void sci_change_state(struct sci_base_state_machine *sm, u32 next_state)
  150. {
  151. sci_state_transition_t handler;
  152. handler = sm->state_table[sm->current_state_id].exit_state;
  153. if (handler)
  154. handler(sm);
  155. sm->previous_state_id = sm->current_state_id;
  156. sm->current_state_id = next_state;
  157. handler = sm->state_table[sm->current_state_id].enter_state;
  158. if (handler)
  159. handler(sm);
  160. }
  161. static bool sci_controller_completion_queue_has_entries(struct isci_host *ihost)
  162. {
  163. u32 get_value = ihost->completion_queue_get;
  164. u32 get_index = get_value & SMU_COMPLETION_QUEUE_GET_POINTER_MASK;
  165. if (NORMALIZE_GET_POINTER_CYCLE_BIT(get_value) ==
  166. COMPLETION_QUEUE_CYCLE_BIT(ihost->completion_queue[get_index]))
  167. return true;
  168. return false;
  169. }
  170. static bool sci_controller_isr(struct isci_host *ihost)
  171. {
  172. if (sci_controller_completion_queue_has_entries(ihost)) {
  173. return true;
  174. } else {
  175. /*
  176. * we have a spurious interrupt it could be that we have already
  177. * emptied the completion queue from a previous interrupt */
  178. writel(SMU_ISR_COMPLETION, &ihost->smu_registers->interrupt_status);
  179. /*
  180. * There is a race in the hardware that could cause us not to be notified
  181. * of an interrupt completion if we do not take this step. We will mask
  182. * then unmask the interrupts so if there is another interrupt pending
  183. * the clearing of the interrupt source we get the next interrupt message. */
  184. writel(0xFF000000, &ihost->smu_registers->interrupt_mask);
  185. writel(0, &ihost->smu_registers->interrupt_mask);
  186. }
  187. return false;
  188. }
  189. irqreturn_t isci_msix_isr(int vec, void *data)
  190. {
  191. struct isci_host *ihost = data;
  192. if (sci_controller_isr(ihost))
  193. tasklet_schedule(&ihost->completion_tasklet);
  194. return IRQ_HANDLED;
  195. }
  196. static bool sci_controller_error_isr(struct isci_host *ihost)
  197. {
  198. u32 interrupt_status;
  199. interrupt_status =
  200. readl(&ihost->smu_registers->interrupt_status);
  201. interrupt_status &= (SMU_ISR_QUEUE_ERROR | SMU_ISR_QUEUE_SUSPEND);
  202. if (interrupt_status != 0) {
  203. /*
  204. * There is an error interrupt pending so let it through and handle
  205. * in the callback */
  206. return true;
  207. }
  208. /*
  209. * There is a race in the hardware that could cause us not to be notified
  210. * of an interrupt completion if we do not take this step. We will mask
  211. * then unmask the error interrupts so if there was another interrupt
  212. * pending we will be notified.
  213. * Could we write the value of (SMU_ISR_QUEUE_ERROR | SMU_ISR_QUEUE_SUSPEND)? */
  214. writel(0xff, &ihost->smu_registers->interrupt_mask);
  215. writel(0, &ihost->smu_registers->interrupt_mask);
  216. return false;
  217. }
  218. static void sci_controller_task_completion(struct isci_host *ihost, u32 ent)
  219. {
  220. u32 index = SCU_GET_COMPLETION_INDEX(ent);
  221. struct isci_request *ireq = ihost->reqs[index];
  222. /* Make sure that we really want to process this IO request */
  223. if (test_bit(IREQ_ACTIVE, &ireq->flags) &&
  224. ireq->io_tag != SCI_CONTROLLER_INVALID_IO_TAG &&
  225. ISCI_TAG_SEQ(ireq->io_tag) == ihost->io_request_sequence[index])
  226. /* Yep this is a valid io request pass it along to the
  227. * io request handler
  228. */
  229. sci_io_request_tc_completion(ireq, ent);
  230. }
  231. static void sci_controller_sdma_completion(struct isci_host *ihost, u32 ent)
  232. {
  233. u32 index;
  234. struct isci_request *ireq;
  235. struct isci_remote_device *idev;
  236. index = SCU_GET_COMPLETION_INDEX(ent);
  237. switch (scu_get_command_request_type(ent)) {
  238. case SCU_CONTEXT_COMMAND_REQUEST_TYPE_POST_TC:
  239. case SCU_CONTEXT_COMMAND_REQUEST_TYPE_DUMP_TC:
  240. ireq = ihost->reqs[index];
  241. dev_warn(&ihost->pdev->dev, "%s: %x for io request %p\n",
  242. __func__, ent, ireq);
  243. /* @todo For a post TC operation we need to fail the IO
  244. * request
  245. */
  246. break;
  247. case SCU_CONTEXT_COMMAND_REQUEST_TYPE_DUMP_RNC:
  248. case SCU_CONTEXT_COMMAND_REQUEST_TYPE_OTHER_RNC:
  249. case SCU_CONTEXT_COMMAND_REQUEST_TYPE_POST_RNC:
  250. idev = ihost->device_table[index];
  251. dev_warn(&ihost->pdev->dev, "%s: %x for device %p\n",
  252. __func__, ent, idev);
  253. /* @todo For a port RNC operation we need to fail the
  254. * device
  255. */
  256. break;
  257. default:
  258. dev_warn(&ihost->pdev->dev, "%s: unknown completion type %x\n",
  259. __func__, ent);
  260. break;
  261. }
  262. }
  263. static void sci_controller_unsolicited_frame(struct isci_host *ihost, u32 ent)
  264. {
  265. u32 index;
  266. u32 frame_index;
  267. struct scu_unsolicited_frame_header *frame_header;
  268. struct isci_phy *iphy;
  269. struct isci_remote_device *idev;
  270. enum sci_status result = SCI_FAILURE;
  271. frame_index = SCU_GET_FRAME_INDEX(ent);
  272. frame_header = ihost->uf_control.buffers.array[frame_index].header;
  273. ihost->uf_control.buffers.array[frame_index].state = UNSOLICITED_FRAME_IN_USE;
  274. if (SCU_GET_FRAME_ERROR(ent)) {
  275. /*
  276. * / @todo If the IAF frame or SIGNATURE FIS frame has an error will
  277. * / this cause a problem? We expect the phy initialization will
  278. * / fail if there is an error in the frame. */
  279. sci_controller_release_frame(ihost, frame_index);
  280. return;
  281. }
  282. if (frame_header->is_address_frame) {
  283. index = SCU_GET_PROTOCOL_ENGINE_INDEX(ent);
  284. iphy = &ihost->phys[index];
  285. result = sci_phy_frame_handler(iphy, frame_index);
  286. } else {
  287. index = SCU_GET_COMPLETION_INDEX(ent);
  288. if (index == SCIC_SDS_REMOTE_NODE_CONTEXT_INVALID_INDEX) {
  289. /*
  290. * This is a signature fis or a frame from a direct attached SATA
  291. * device that has not yet been created. In either case forwared
  292. * the frame to the PE and let it take care of the frame data. */
  293. index = SCU_GET_PROTOCOL_ENGINE_INDEX(ent);
  294. iphy = &ihost->phys[index];
  295. result = sci_phy_frame_handler(iphy, frame_index);
  296. } else {
  297. if (index < ihost->remote_node_entries)
  298. idev = ihost->device_table[index];
  299. else
  300. idev = NULL;
  301. if (idev != NULL)
  302. result = sci_remote_device_frame_handler(idev, frame_index);
  303. else
  304. sci_controller_release_frame(ihost, frame_index);
  305. }
  306. }
  307. if (result != SCI_SUCCESS) {
  308. /*
  309. * / @todo Is there any reason to report some additional error message
  310. * / when we get this failure notifiction? */
  311. }
  312. }
  313. static void sci_controller_event_completion(struct isci_host *ihost, u32 ent)
  314. {
  315. struct isci_remote_device *idev;
  316. struct isci_request *ireq;
  317. struct isci_phy *iphy;
  318. u32 index;
  319. index = SCU_GET_COMPLETION_INDEX(ent);
  320. switch (scu_get_event_type(ent)) {
  321. case SCU_EVENT_TYPE_SMU_COMMAND_ERROR:
  322. /* / @todo The driver did something wrong and we need to fix the condtion. */
  323. dev_err(&ihost->pdev->dev,
  324. "%s: SCIC Controller 0x%p received SMU command error "
  325. "0x%x\n",
  326. __func__,
  327. ihost,
  328. ent);
  329. break;
  330. case SCU_EVENT_TYPE_SMU_PCQ_ERROR:
  331. case SCU_EVENT_TYPE_SMU_ERROR:
  332. case SCU_EVENT_TYPE_FATAL_MEMORY_ERROR:
  333. /*
  334. * / @todo This is a hardware failure and its likely that we want to
  335. * / reset the controller. */
  336. dev_err(&ihost->pdev->dev,
  337. "%s: SCIC Controller 0x%p received fatal controller "
  338. "event 0x%x\n",
  339. __func__,
  340. ihost,
  341. ent);
  342. break;
  343. case SCU_EVENT_TYPE_TRANSPORT_ERROR:
  344. ireq = ihost->reqs[index];
  345. sci_io_request_event_handler(ireq, ent);
  346. break;
  347. case SCU_EVENT_TYPE_PTX_SCHEDULE_EVENT:
  348. switch (scu_get_event_specifier(ent)) {
  349. case SCU_EVENT_SPECIFIC_SMP_RESPONSE_NO_PE:
  350. case SCU_EVENT_SPECIFIC_TASK_TIMEOUT:
  351. ireq = ihost->reqs[index];
  352. if (ireq != NULL)
  353. sci_io_request_event_handler(ireq, ent);
  354. else
  355. dev_warn(&ihost->pdev->dev,
  356. "%s: SCIC Controller 0x%p received "
  357. "event 0x%x for io request object "
  358. "that doesnt exist.\n",
  359. __func__,
  360. ihost,
  361. ent);
  362. break;
  363. case SCU_EVENT_SPECIFIC_IT_NEXUS_TIMEOUT:
  364. idev = ihost->device_table[index];
  365. if (idev != NULL)
  366. sci_remote_device_event_handler(idev, ent);
  367. else
  368. dev_warn(&ihost->pdev->dev,
  369. "%s: SCIC Controller 0x%p received "
  370. "event 0x%x for remote device object "
  371. "that doesnt exist.\n",
  372. __func__,
  373. ihost,
  374. ent);
  375. break;
  376. }
  377. break;
  378. case SCU_EVENT_TYPE_BROADCAST_CHANGE:
  379. /*
  380. * direct the broadcast change event to the phy first and then let
  381. * the phy redirect the broadcast change to the port object */
  382. case SCU_EVENT_TYPE_ERR_CNT_EVENT:
  383. /*
  384. * direct error counter event to the phy object since that is where
  385. * we get the event notification. This is a type 4 event. */
  386. case SCU_EVENT_TYPE_OSSP_EVENT:
  387. index = SCU_GET_PROTOCOL_ENGINE_INDEX(ent);
  388. iphy = &ihost->phys[index];
  389. sci_phy_event_handler(iphy, ent);
  390. break;
  391. case SCU_EVENT_TYPE_RNC_SUSPEND_TX:
  392. case SCU_EVENT_TYPE_RNC_SUSPEND_TX_RX:
  393. case SCU_EVENT_TYPE_RNC_OPS_MISC:
  394. if (index < ihost->remote_node_entries) {
  395. idev = ihost->device_table[index];
  396. if (idev != NULL)
  397. sci_remote_device_event_handler(idev, ent);
  398. } else
  399. dev_err(&ihost->pdev->dev,
  400. "%s: SCIC Controller 0x%p received event 0x%x "
  401. "for remote device object 0x%0x that doesnt "
  402. "exist.\n",
  403. __func__,
  404. ihost,
  405. ent,
  406. index);
  407. break;
  408. default:
  409. dev_warn(&ihost->pdev->dev,
  410. "%s: SCIC Controller received unknown event code %x\n",
  411. __func__,
  412. ent);
  413. break;
  414. }
  415. }
  416. static void sci_controller_process_completions(struct isci_host *ihost)
  417. {
  418. u32 completion_count = 0;
  419. u32 ent;
  420. u32 get_index;
  421. u32 get_cycle;
  422. u32 event_get;
  423. u32 event_cycle;
  424. dev_dbg(&ihost->pdev->dev,
  425. "%s: completion queue begining get:0x%08x\n",
  426. __func__,
  427. ihost->completion_queue_get);
  428. /* Get the component parts of the completion queue */
  429. get_index = NORMALIZE_GET_POINTER(ihost->completion_queue_get);
  430. get_cycle = SMU_CQGR_CYCLE_BIT & ihost->completion_queue_get;
  431. event_get = NORMALIZE_EVENT_POINTER(ihost->completion_queue_get);
  432. event_cycle = SMU_CQGR_EVENT_CYCLE_BIT & ihost->completion_queue_get;
  433. while (
  434. NORMALIZE_GET_POINTER_CYCLE_BIT(get_cycle)
  435. == COMPLETION_QUEUE_CYCLE_BIT(ihost->completion_queue[get_index])
  436. ) {
  437. completion_count++;
  438. ent = ihost->completion_queue[get_index];
  439. /* increment the get pointer and check for rollover to toggle the cycle bit */
  440. get_cycle ^= ((get_index+1) & SCU_MAX_COMPLETION_QUEUE_ENTRIES) <<
  441. (SMU_COMPLETION_QUEUE_GET_CYCLE_BIT_SHIFT - SCU_MAX_COMPLETION_QUEUE_SHIFT);
  442. get_index = (get_index+1) & (SCU_MAX_COMPLETION_QUEUE_ENTRIES-1);
  443. dev_dbg(&ihost->pdev->dev,
  444. "%s: completion queue entry:0x%08x\n",
  445. __func__,
  446. ent);
  447. switch (SCU_GET_COMPLETION_TYPE(ent)) {
  448. case SCU_COMPLETION_TYPE_TASK:
  449. sci_controller_task_completion(ihost, ent);
  450. break;
  451. case SCU_COMPLETION_TYPE_SDMA:
  452. sci_controller_sdma_completion(ihost, ent);
  453. break;
  454. case SCU_COMPLETION_TYPE_UFI:
  455. sci_controller_unsolicited_frame(ihost, ent);
  456. break;
  457. case SCU_COMPLETION_TYPE_EVENT:
  458. sci_controller_event_completion(ihost, ent);
  459. break;
  460. case SCU_COMPLETION_TYPE_NOTIFY: {
  461. event_cycle ^= ((event_get+1) & SCU_MAX_EVENTS) <<
  462. (SMU_COMPLETION_QUEUE_GET_EVENT_CYCLE_BIT_SHIFT - SCU_MAX_EVENTS_SHIFT);
  463. event_get = (event_get+1) & (SCU_MAX_EVENTS-1);
  464. sci_controller_event_completion(ihost, ent);
  465. break;
  466. }
  467. default:
  468. dev_warn(&ihost->pdev->dev,
  469. "%s: SCIC Controller received unknown "
  470. "completion type %x\n",
  471. __func__,
  472. ent);
  473. break;
  474. }
  475. }
  476. /* Update the get register if we completed one or more entries */
  477. if (completion_count > 0) {
  478. ihost->completion_queue_get =
  479. SMU_CQGR_GEN_BIT(ENABLE) |
  480. SMU_CQGR_GEN_BIT(EVENT_ENABLE) |
  481. event_cycle |
  482. SMU_CQGR_GEN_VAL(EVENT_POINTER, event_get) |
  483. get_cycle |
  484. SMU_CQGR_GEN_VAL(POINTER, get_index);
  485. writel(ihost->completion_queue_get,
  486. &ihost->smu_registers->completion_queue_get);
  487. }
  488. dev_dbg(&ihost->pdev->dev,
  489. "%s: completion queue ending get:0x%08x\n",
  490. __func__,
  491. ihost->completion_queue_get);
  492. }
  493. static void sci_controller_error_handler(struct isci_host *ihost)
  494. {
  495. u32 interrupt_status;
  496. interrupt_status =
  497. readl(&ihost->smu_registers->interrupt_status);
  498. if ((interrupt_status & SMU_ISR_QUEUE_SUSPEND) &&
  499. sci_controller_completion_queue_has_entries(ihost)) {
  500. sci_controller_process_completions(ihost);
  501. writel(SMU_ISR_QUEUE_SUSPEND, &ihost->smu_registers->interrupt_status);
  502. } else {
  503. dev_err(&ihost->pdev->dev, "%s: status: %#x\n", __func__,
  504. interrupt_status);
  505. sci_change_state(&ihost->sm, SCIC_FAILED);
  506. return;
  507. }
  508. /* If we dont process any completions I am not sure that we want to do this.
  509. * We are in the middle of a hardware fault and should probably be reset.
  510. */
  511. writel(0, &ihost->smu_registers->interrupt_mask);
  512. }
  513. irqreturn_t isci_intx_isr(int vec, void *data)
  514. {
  515. irqreturn_t ret = IRQ_NONE;
  516. struct isci_host *ihost = data;
  517. if (sci_controller_isr(ihost)) {
  518. writel(SMU_ISR_COMPLETION, &ihost->smu_registers->interrupt_status);
  519. tasklet_schedule(&ihost->completion_tasklet);
  520. ret = IRQ_HANDLED;
  521. } else if (sci_controller_error_isr(ihost)) {
  522. spin_lock(&ihost->scic_lock);
  523. sci_controller_error_handler(ihost);
  524. spin_unlock(&ihost->scic_lock);
  525. ret = IRQ_HANDLED;
  526. }
  527. return ret;
  528. }
  529. irqreturn_t isci_error_isr(int vec, void *data)
  530. {
  531. struct isci_host *ihost = data;
  532. if (sci_controller_error_isr(ihost))
  533. sci_controller_error_handler(ihost);
  534. return IRQ_HANDLED;
  535. }
  536. /**
  537. * isci_host_start_complete() - This function is called by the core library,
  538. * through the ISCI Module, to indicate controller start status.
  539. * @isci_host: This parameter specifies the ISCI host object
  540. * @completion_status: This parameter specifies the completion status from the
  541. * core library.
  542. *
  543. */
  544. static void isci_host_start_complete(struct isci_host *ihost, enum sci_status completion_status)
  545. {
  546. if (completion_status != SCI_SUCCESS)
  547. dev_info(&ihost->pdev->dev,
  548. "controller start timed out, continuing...\n");
  549. clear_bit(IHOST_START_PENDING, &ihost->flags);
  550. wake_up(&ihost->eventq);
  551. }
  552. int isci_host_scan_finished(struct Scsi_Host *shost, unsigned long time)
  553. {
  554. struct sas_ha_struct *ha = SHOST_TO_SAS_HA(shost);
  555. struct isci_host *ihost = ha->lldd_ha;
  556. if (test_bit(IHOST_START_PENDING, &ihost->flags))
  557. return 0;
  558. sas_drain_work(ha);
  559. return 1;
  560. }
  561. /**
  562. * sci_controller_get_suggested_start_timeout() - This method returns the
  563. * suggested sci_controller_start() timeout amount. The user is free to
  564. * use any timeout value, but this method provides the suggested minimum
  565. * start timeout value. The returned value is based upon empirical
  566. * information determined as a result of interoperability testing.
  567. * @controller: the handle to the controller object for which to return the
  568. * suggested start timeout.
  569. *
  570. * This method returns the number of milliseconds for the suggested start
  571. * operation timeout.
  572. */
  573. static u32 sci_controller_get_suggested_start_timeout(struct isci_host *ihost)
  574. {
  575. /* Validate the user supplied parameters. */
  576. if (!ihost)
  577. return 0;
  578. /*
  579. * The suggested minimum timeout value for a controller start operation:
  580. *
  581. * Signature FIS Timeout
  582. * + Phy Start Timeout
  583. * + Number of Phy Spin Up Intervals
  584. * ---------------------------------
  585. * Number of milliseconds for the controller start operation.
  586. *
  587. * NOTE: The number of phy spin up intervals will be equivalent
  588. * to the number of phys divided by the number phys allowed
  589. * per interval - 1 (once OEM parameters are supported).
  590. * Currently we assume only 1 phy per interval. */
  591. return SCIC_SDS_SIGNATURE_FIS_TIMEOUT
  592. + SCIC_SDS_CONTROLLER_PHY_START_TIMEOUT
  593. + ((SCI_MAX_PHYS - 1) * SCIC_SDS_CONTROLLER_POWER_CONTROL_INTERVAL);
  594. }
  595. static void sci_controller_enable_interrupts(struct isci_host *ihost)
  596. {
  597. BUG_ON(ihost->smu_registers == NULL);
  598. writel(0, &ihost->smu_registers->interrupt_mask);
  599. }
  600. void sci_controller_disable_interrupts(struct isci_host *ihost)
  601. {
  602. BUG_ON(ihost->smu_registers == NULL);
  603. writel(0xffffffff, &ihost->smu_registers->interrupt_mask);
  604. }
  605. static void sci_controller_enable_port_task_scheduler(struct isci_host *ihost)
  606. {
  607. u32 port_task_scheduler_value;
  608. port_task_scheduler_value =
  609. readl(&ihost->scu_registers->peg0.ptsg.control);
  610. port_task_scheduler_value |=
  611. (SCU_PTSGCR_GEN_BIT(ETM_ENABLE) |
  612. SCU_PTSGCR_GEN_BIT(PTSG_ENABLE));
  613. writel(port_task_scheduler_value,
  614. &ihost->scu_registers->peg0.ptsg.control);
  615. }
  616. static void sci_controller_assign_task_entries(struct isci_host *ihost)
  617. {
  618. u32 task_assignment;
  619. /*
  620. * Assign all the TCs to function 0
  621. * TODO: Do we actually need to read this register to write it back?
  622. */
  623. task_assignment =
  624. readl(&ihost->smu_registers->task_context_assignment[0]);
  625. task_assignment |= (SMU_TCA_GEN_VAL(STARTING, 0)) |
  626. (SMU_TCA_GEN_VAL(ENDING, ihost->task_context_entries - 1)) |
  627. (SMU_TCA_GEN_BIT(RANGE_CHECK_ENABLE));
  628. writel(task_assignment,
  629. &ihost->smu_registers->task_context_assignment[0]);
  630. }
  631. static void sci_controller_initialize_completion_queue(struct isci_host *ihost)
  632. {
  633. u32 index;
  634. u32 completion_queue_control_value;
  635. u32 completion_queue_get_value;
  636. u32 completion_queue_put_value;
  637. ihost->completion_queue_get = 0;
  638. completion_queue_control_value =
  639. (SMU_CQC_QUEUE_LIMIT_SET(SCU_MAX_COMPLETION_QUEUE_ENTRIES - 1) |
  640. SMU_CQC_EVENT_LIMIT_SET(SCU_MAX_EVENTS - 1));
  641. writel(completion_queue_control_value,
  642. &ihost->smu_registers->completion_queue_control);
  643. /* Set the completion queue get pointer and enable the queue */
  644. completion_queue_get_value = (
  645. (SMU_CQGR_GEN_VAL(POINTER, 0))
  646. | (SMU_CQGR_GEN_VAL(EVENT_POINTER, 0))
  647. | (SMU_CQGR_GEN_BIT(ENABLE))
  648. | (SMU_CQGR_GEN_BIT(EVENT_ENABLE))
  649. );
  650. writel(completion_queue_get_value,
  651. &ihost->smu_registers->completion_queue_get);
  652. /* Set the completion queue put pointer */
  653. completion_queue_put_value = (
  654. (SMU_CQPR_GEN_VAL(POINTER, 0))
  655. | (SMU_CQPR_GEN_VAL(EVENT_POINTER, 0))
  656. );
  657. writel(completion_queue_put_value,
  658. &ihost->smu_registers->completion_queue_put);
  659. /* Initialize the cycle bit of the completion queue entries */
  660. for (index = 0; index < SCU_MAX_COMPLETION_QUEUE_ENTRIES; index++) {
  661. /*
  662. * If get.cycle_bit != completion_queue.cycle_bit
  663. * its not a valid completion queue entry
  664. * so at system start all entries are invalid */
  665. ihost->completion_queue[index] = 0x80000000;
  666. }
  667. }
  668. static void sci_controller_initialize_unsolicited_frame_queue(struct isci_host *ihost)
  669. {
  670. u32 frame_queue_control_value;
  671. u32 frame_queue_get_value;
  672. u32 frame_queue_put_value;
  673. /* Write the queue size */
  674. frame_queue_control_value =
  675. SCU_UFQC_GEN_VAL(QUEUE_SIZE, SCU_MAX_UNSOLICITED_FRAMES);
  676. writel(frame_queue_control_value,
  677. &ihost->scu_registers->sdma.unsolicited_frame_queue_control);
  678. /* Setup the get pointer for the unsolicited frame queue */
  679. frame_queue_get_value = (
  680. SCU_UFQGP_GEN_VAL(POINTER, 0)
  681. | SCU_UFQGP_GEN_BIT(ENABLE_BIT)
  682. );
  683. writel(frame_queue_get_value,
  684. &ihost->scu_registers->sdma.unsolicited_frame_get_pointer);
  685. /* Setup the put pointer for the unsolicited frame queue */
  686. frame_queue_put_value = SCU_UFQPP_GEN_VAL(POINTER, 0);
  687. writel(frame_queue_put_value,
  688. &ihost->scu_registers->sdma.unsolicited_frame_put_pointer);
  689. }
  690. static void sci_controller_transition_to_ready(struct isci_host *ihost, enum sci_status status)
  691. {
  692. if (ihost->sm.current_state_id == SCIC_STARTING) {
  693. /*
  694. * We move into the ready state, because some of the phys/ports
  695. * may be up and operational.
  696. */
  697. sci_change_state(&ihost->sm, SCIC_READY);
  698. isci_host_start_complete(ihost, status);
  699. }
  700. }
  701. static bool is_phy_starting(struct isci_phy *iphy)
  702. {
  703. enum sci_phy_states state;
  704. state = iphy->sm.current_state_id;
  705. switch (state) {
  706. case SCI_PHY_STARTING:
  707. case SCI_PHY_SUB_INITIAL:
  708. case SCI_PHY_SUB_AWAIT_SAS_SPEED_EN:
  709. case SCI_PHY_SUB_AWAIT_IAF_UF:
  710. case SCI_PHY_SUB_AWAIT_SAS_POWER:
  711. case SCI_PHY_SUB_AWAIT_SATA_POWER:
  712. case SCI_PHY_SUB_AWAIT_SATA_PHY_EN:
  713. case SCI_PHY_SUB_AWAIT_SATA_SPEED_EN:
  714. case SCI_PHY_SUB_AWAIT_SIG_FIS_UF:
  715. case SCI_PHY_SUB_FINAL:
  716. return true;
  717. default:
  718. return false;
  719. }
  720. }
  721. /**
  722. * sci_controller_start_next_phy - start phy
  723. * @scic: controller
  724. *
  725. * If all the phys have been started, then attempt to transition the
  726. * controller to the READY state and inform the user
  727. * (sci_cb_controller_start_complete()).
  728. */
  729. static enum sci_status sci_controller_start_next_phy(struct isci_host *ihost)
  730. {
  731. struct sci_oem_params *oem = &ihost->oem_parameters;
  732. struct isci_phy *iphy;
  733. enum sci_status status;
  734. status = SCI_SUCCESS;
  735. if (ihost->phy_startup_timer_pending)
  736. return status;
  737. if (ihost->next_phy_to_start >= SCI_MAX_PHYS) {
  738. bool is_controller_start_complete = true;
  739. u32 state;
  740. u8 index;
  741. for (index = 0; index < SCI_MAX_PHYS; index++) {
  742. iphy = &ihost->phys[index];
  743. state = iphy->sm.current_state_id;
  744. if (!phy_get_non_dummy_port(iphy))
  745. continue;
  746. /* The controller start operation is complete iff:
  747. * - all links have been given an opportunity to start
  748. * - have no indication of a connected device
  749. * - have an indication of a connected device and it has
  750. * finished the link training process.
  751. */
  752. if ((iphy->is_in_link_training == false && state == SCI_PHY_INITIAL) ||
  753. (iphy->is_in_link_training == false && state == SCI_PHY_STOPPED) ||
  754. (iphy->is_in_link_training == true && is_phy_starting(iphy)) ||
  755. (ihost->port_agent.phy_ready_mask != ihost->port_agent.phy_configured_mask)) {
  756. is_controller_start_complete = false;
  757. break;
  758. }
  759. }
  760. /*
  761. * The controller has successfully finished the start process.
  762. * Inform the SCI Core user and transition to the READY state. */
  763. if (is_controller_start_complete == true) {
  764. sci_controller_transition_to_ready(ihost, SCI_SUCCESS);
  765. sci_del_timer(&ihost->phy_timer);
  766. ihost->phy_startup_timer_pending = false;
  767. }
  768. } else {
  769. iphy = &ihost->phys[ihost->next_phy_to_start];
  770. if (oem->controller.mode_type == SCIC_PORT_MANUAL_CONFIGURATION_MODE) {
  771. if (phy_get_non_dummy_port(iphy) == NULL) {
  772. ihost->next_phy_to_start++;
  773. /* Caution recursion ahead be forwarned
  774. *
  775. * The PHY was never added to a PORT in MPC mode
  776. * so start the next phy in sequence This phy
  777. * will never go link up and will not draw power
  778. * the OEM parameters either configured the phy
  779. * incorrectly for the PORT or it was never
  780. * assigned to a PORT
  781. */
  782. return sci_controller_start_next_phy(ihost);
  783. }
  784. }
  785. status = sci_phy_start(iphy);
  786. if (status == SCI_SUCCESS) {
  787. sci_mod_timer(&ihost->phy_timer,
  788. SCIC_SDS_CONTROLLER_PHY_START_TIMEOUT);
  789. ihost->phy_startup_timer_pending = true;
  790. } else {
  791. dev_warn(&ihost->pdev->dev,
  792. "%s: Controller stop operation failed "
  793. "to stop phy %d because of status "
  794. "%d.\n",
  795. __func__,
  796. ihost->phys[ihost->next_phy_to_start].phy_index,
  797. status);
  798. }
  799. ihost->next_phy_to_start++;
  800. }
  801. return status;
  802. }
  803. static void phy_startup_timeout(unsigned long data)
  804. {
  805. struct sci_timer *tmr = (struct sci_timer *)data;
  806. struct isci_host *ihost = container_of(tmr, typeof(*ihost), phy_timer);
  807. unsigned long flags;
  808. enum sci_status status;
  809. spin_lock_irqsave(&ihost->scic_lock, flags);
  810. if (tmr->cancel)
  811. goto done;
  812. ihost->phy_startup_timer_pending = false;
  813. do {
  814. status = sci_controller_start_next_phy(ihost);
  815. } while (status != SCI_SUCCESS);
  816. done:
  817. spin_unlock_irqrestore(&ihost->scic_lock, flags);
  818. }
  819. static u16 isci_tci_active(struct isci_host *ihost)
  820. {
  821. return CIRC_CNT(ihost->tci_head, ihost->tci_tail, SCI_MAX_IO_REQUESTS);
  822. }
  823. static enum sci_status sci_controller_start(struct isci_host *ihost,
  824. u32 timeout)
  825. {
  826. enum sci_status result;
  827. u16 index;
  828. if (ihost->sm.current_state_id != SCIC_INITIALIZED) {
  829. dev_warn(&ihost->pdev->dev, "%s invalid state: %d\n",
  830. __func__, ihost->sm.current_state_id);
  831. return SCI_FAILURE_INVALID_STATE;
  832. }
  833. /* Build the TCi free pool */
  834. BUILD_BUG_ON(SCI_MAX_IO_REQUESTS > 1 << sizeof(ihost->tci_pool[0]) * 8);
  835. ihost->tci_head = 0;
  836. ihost->tci_tail = 0;
  837. for (index = 0; index < ihost->task_context_entries; index++)
  838. isci_tci_free(ihost, index);
  839. /* Build the RNi free pool */
  840. sci_remote_node_table_initialize(&ihost->available_remote_nodes,
  841. ihost->remote_node_entries);
  842. /*
  843. * Before anything else lets make sure we will not be
  844. * interrupted by the hardware.
  845. */
  846. sci_controller_disable_interrupts(ihost);
  847. /* Enable the port task scheduler */
  848. sci_controller_enable_port_task_scheduler(ihost);
  849. /* Assign all the task entries to ihost physical function */
  850. sci_controller_assign_task_entries(ihost);
  851. /* Now initialize the completion queue */
  852. sci_controller_initialize_completion_queue(ihost);
  853. /* Initialize the unsolicited frame queue for use */
  854. sci_controller_initialize_unsolicited_frame_queue(ihost);
  855. /* Start all of the ports on this controller */
  856. for (index = 0; index < ihost->logical_port_entries; index++) {
  857. struct isci_port *iport = &ihost->ports[index];
  858. result = sci_port_start(iport);
  859. if (result)
  860. return result;
  861. }
  862. sci_controller_start_next_phy(ihost);
  863. sci_mod_timer(&ihost->timer, timeout);
  864. sci_change_state(&ihost->sm, SCIC_STARTING);
  865. return SCI_SUCCESS;
  866. }
  867. void isci_host_scan_start(struct Scsi_Host *shost)
  868. {
  869. struct isci_host *ihost = SHOST_TO_SAS_HA(shost)->lldd_ha;
  870. unsigned long tmo = sci_controller_get_suggested_start_timeout(ihost);
  871. set_bit(IHOST_START_PENDING, &ihost->flags);
  872. spin_lock_irq(&ihost->scic_lock);
  873. sci_controller_start(ihost, tmo);
  874. sci_controller_enable_interrupts(ihost);
  875. spin_unlock_irq(&ihost->scic_lock);
  876. }
  877. static void isci_host_stop_complete(struct isci_host *ihost, enum sci_status completion_status)
  878. {
  879. sci_controller_disable_interrupts(ihost);
  880. clear_bit(IHOST_STOP_PENDING, &ihost->flags);
  881. wake_up(&ihost->eventq);
  882. }
  883. static void sci_controller_completion_handler(struct isci_host *ihost)
  884. {
  885. /* Empty out the completion queue */
  886. if (sci_controller_completion_queue_has_entries(ihost))
  887. sci_controller_process_completions(ihost);
  888. /* Clear the interrupt and enable all interrupts again */
  889. writel(SMU_ISR_COMPLETION, &ihost->smu_registers->interrupt_status);
  890. /* Could we write the value of SMU_ISR_COMPLETION? */
  891. writel(0xFF000000, &ihost->smu_registers->interrupt_mask);
  892. writel(0, &ihost->smu_registers->interrupt_mask);
  893. }
  894. /**
  895. * isci_host_completion_routine() - This function is the delayed service
  896. * routine that calls the sci core library's completion handler. It's
  897. * scheduled as a tasklet from the interrupt service routine when interrupts
  898. * in use, or set as the timeout function in polled mode.
  899. * @data: This parameter specifies the ISCI host object
  900. *
  901. */
  902. static void isci_host_completion_routine(unsigned long data)
  903. {
  904. struct isci_host *ihost = (struct isci_host *)data;
  905. struct list_head completed_request_list;
  906. struct list_head errored_request_list;
  907. struct list_head *current_position;
  908. struct list_head *next_position;
  909. struct isci_request *request;
  910. struct isci_request *next_request;
  911. struct sas_task *task;
  912. u16 active;
  913. INIT_LIST_HEAD(&completed_request_list);
  914. INIT_LIST_HEAD(&errored_request_list);
  915. spin_lock_irq(&ihost->scic_lock);
  916. sci_controller_completion_handler(ihost);
  917. /* Take the lists of completed I/Os from the host. */
  918. list_splice_init(&ihost->requests_to_complete,
  919. &completed_request_list);
  920. /* Take the list of errored I/Os from the host. */
  921. list_splice_init(&ihost->requests_to_errorback,
  922. &errored_request_list);
  923. spin_unlock_irq(&ihost->scic_lock);
  924. /* Process any completions in the lists. */
  925. list_for_each_safe(current_position, next_position,
  926. &completed_request_list) {
  927. request = list_entry(current_position, struct isci_request,
  928. completed_node);
  929. task = isci_request_access_task(request);
  930. /* Normal notification (task_done) */
  931. dev_dbg(&ihost->pdev->dev,
  932. "%s: Normal - request/task = %p/%p\n",
  933. __func__,
  934. request,
  935. task);
  936. /* Return the task to libsas */
  937. if (task != NULL) {
  938. task->lldd_task = NULL;
  939. if (!(task->task_state_flags & SAS_TASK_STATE_ABORTED)) {
  940. /* If the task is already in the abort path,
  941. * the task_done callback cannot be called.
  942. */
  943. task->task_done(task);
  944. }
  945. }
  946. spin_lock_irq(&ihost->scic_lock);
  947. isci_free_tag(ihost, request->io_tag);
  948. spin_unlock_irq(&ihost->scic_lock);
  949. }
  950. list_for_each_entry_safe(request, next_request, &errored_request_list,
  951. completed_node) {
  952. task = isci_request_access_task(request);
  953. /* Use sas_task_abort */
  954. dev_warn(&ihost->pdev->dev,
  955. "%s: Error - request/task = %p/%p\n",
  956. __func__,
  957. request,
  958. task);
  959. if (task != NULL) {
  960. /* Put the task into the abort path if it's not there
  961. * already.
  962. */
  963. if (!(task->task_state_flags & SAS_TASK_STATE_ABORTED))
  964. sas_task_abort(task);
  965. } else {
  966. /* This is a case where the request has completed with a
  967. * status such that it needed further target servicing,
  968. * but the sas_task reference has already been removed
  969. * from the request. Since it was errored, it was not
  970. * being aborted, so there is nothing to do except free
  971. * it.
  972. */
  973. spin_lock_irq(&ihost->scic_lock);
  974. /* Remove the request from the remote device's list
  975. * of pending requests.
  976. */
  977. list_del_init(&request->dev_node);
  978. isci_free_tag(ihost, request->io_tag);
  979. spin_unlock_irq(&ihost->scic_lock);
  980. }
  981. }
  982. /* the coalesence timeout doubles at each encoding step, so
  983. * update it based on the ilog2 value of the outstanding requests
  984. */
  985. active = isci_tci_active(ihost);
  986. writel(SMU_ICC_GEN_VAL(NUMBER, active) |
  987. SMU_ICC_GEN_VAL(TIMER, ISCI_COALESCE_BASE + ilog2(active)),
  988. &ihost->smu_registers->interrupt_coalesce_control);
  989. }
  990. /**
  991. * sci_controller_stop() - This method will stop an individual controller
  992. * object.This method will invoke the associated user callback upon
  993. * completion. The completion callback is called when the following
  994. * conditions are met: -# the method return status is SCI_SUCCESS. -# the
  995. * controller has been quiesced. This method will ensure that all IO
  996. * requests are quiesced, phys are stopped, and all additional operation by
  997. * the hardware is halted.
  998. * @controller: the handle to the controller object to stop.
  999. * @timeout: This parameter specifies the number of milliseconds in which the
  1000. * stop operation should complete.
  1001. *
  1002. * The controller must be in the STARTED or STOPPED state. Indicate if the
  1003. * controller stop method succeeded or failed in some way. SCI_SUCCESS if the
  1004. * stop operation successfully began. SCI_WARNING_ALREADY_IN_STATE if the
  1005. * controller is already in the STOPPED state. SCI_FAILURE_INVALID_STATE if the
  1006. * controller is not either in the STARTED or STOPPED states.
  1007. */
  1008. static enum sci_status sci_controller_stop(struct isci_host *ihost, u32 timeout)
  1009. {
  1010. if (ihost->sm.current_state_id != SCIC_READY) {
  1011. dev_warn(&ihost->pdev->dev, "%s invalid state: %d\n",
  1012. __func__, ihost->sm.current_state_id);
  1013. return SCI_FAILURE_INVALID_STATE;
  1014. }
  1015. sci_mod_timer(&ihost->timer, timeout);
  1016. sci_change_state(&ihost->sm, SCIC_STOPPING);
  1017. return SCI_SUCCESS;
  1018. }
  1019. /**
  1020. * sci_controller_reset() - This method will reset the supplied core
  1021. * controller regardless of the state of said controller. This operation is
  1022. * considered destructive. In other words, all current operations are wiped
  1023. * out. No IO completions for outstanding devices occur. Outstanding IO
  1024. * requests are not aborted or completed at the actual remote device.
  1025. * @controller: the handle to the controller object to reset.
  1026. *
  1027. * Indicate if the controller reset method succeeded or failed in some way.
  1028. * SCI_SUCCESS if the reset operation successfully started. SCI_FATAL_ERROR if
  1029. * the controller reset operation is unable to complete.
  1030. */
  1031. static enum sci_status sci_controller_reset(struct isci_host *ihost)
  1032. {
  1033. switch (ihost->sm.current_state_id) {
  1034. case SCIC_RESET:
  1035. case SCIC_READY:
  1036. case SCIC_STOPPED:
  1037. case SCIC_FAILED:
  1038. /*
  1039. * The reset operation is not a graceful cleanup, just
  1040. * perform the state transition.
  1041. */
  1042. sci_change_state(&ihost->sm, SCIC_RESETTING);
  1043. return SCI_SUCCESS;
  1044. default:
  1045. dev_warn(&ihost->pdev->dev, "%s invalid state: %d\n",
  1046. __func__, ihost->sm.current_state_id);
  1047. return SCI_FAILURE_INVALID_STATE;
  1048. }
  1049. }
  1050. void isci_host_deinit(struct isci_host *ihost)
  1051. {
  1052. int i;
  1053. /* disable output data selects */
  1054. for (i = 0; i < isci_gpio_count(ihost); i++)
  1055. writel(SGPIO_HW_CONTROL, &ihost->scu_registers->peg0.sgpio.output_data_select[i]);
  1056. for (i = 0; i < SCI_MAX_PORTS; i++) {
  1057. struct isci_port *iport = &ihost->ports[i];
  1058. struct isci_remote_device *idev, *d;
  1059. list_for_each_entry_safe(idev, d, &iport->remote_dev_list, node) {
  1060. if (test_bit(IDEV_ALLOCATED, &idev->flags))
  1061. isci_remote_device_stop(ihost, idev);
  1062. }
  1063. }
  1064. set_bit(IHOST_STOP_PENDING, &ihost->flags);
  1065. spin_lock_irq(&ihost->scic_lock);
  1066. sci_controller_stop(ihost, SCIC_CONTROLLER_STOP_TIMEOUT);
  1067. spin_unlock_irq(&ihost->scic_lock);
  1068. wait_for_stop(ihost);
  1069. /* disable sgpio: where the above wait should give time for the
  1070. * enclosure to sample the gpios going inactive
  1071. */
  1072. writel(0, &ihost->scu_registers->peg0.sgpio.interface_control);
  1073. sci_controller_reset(ihost);
  1074. /* Cancel any/all outstanding port timers */
  1075. for (i = 0; i < ihost->logical_port_entries; i++) {
  1076. struct isci_port *iport = &ihost->ports[i];
  1077. del_timer_sync(&iport->timer.timer);
  1078. }
  1079. /* Cancel any/all outstanding phy timers */
  1080. for (i = 0; i < SCI_MAX_PHYS; i++) {
  1081. struct isci_phy *iphy = &ihost->phys[i];
  1082. del_timer_sync(&iphy->sata_timer.timer);
  1083. }
  1084. del_timer_sync(&ihost->port_agent.timer.timer);
  1085. del_timer_sync(&ihost->power_control.timer.timer);
  1086. del_timer_sync(&ihost->timer.timer);
  1087. del_timer_sync(&ihost->phy_timer.timer);
  1088. }
  1089. static void __iomem *scu_base(struct isci_host *isci_host)
  1090. {
  1091. struct pci_dev *pdev = isci_host->pdev;
  1092. int id = isci_host->id;
  1093. return pcim_iomap_table(pdev)[SCI_SCU_BAR * 2] + SCI_SCU_BAR_SIZE * id;
  1094. }
  1095. static void __iomem *smu_base(struct isci_host *isci_host)
  1096. {
  1097. struct pci_dev *pdev = isci_host->pdev;
  1098. int id = isci_host->id;
  1099. return pcim_iomap_table(pdev)[SCI_SMU_BAR * 2] + SCI_SMU_BAR_SIZE * id;
  1100. }
  1101. static void isci_user_parameters_get(struct sci_user_parameters *u)
  1102. {
  1103. int i;
  1104. for (i = 0; i < SCI_MAX_PHYS; i++) {
  1105. struct sci_phy_user_params *u_phy = &u->phys[i];
  1106. u_phy->max_speed_generation = phy_gen;
  1107. /* we are not exporting these for now */
  1108. u_phy->align_insertion_frequency = 0x7f;
  1109. u_phy->in_connection_align_insertion_frequency = 0xff;
  1110. u_phy->notify_enable_spin_up_insertion_frequency = 0x33;
  1111. }
  1112. u->stp_inactivity_timeout = stp_inactive_to;
  1113. u->ssp_inactivity_timeout = ssp_inactive_to;
  1114. u->stp_max_occupancy_timeout = stp_max_occ_to;
  1115. u->ssp_max_occupancy_timeout = ssp_max_occ_to;
  1116. u->no_outbound_task_timeout = no_outbound_task_to;
  1117. u->max_concurr_spinup = max_concurr_spinup;
  1118. }
  1119. static void sci_controller_initial_state_enter(struct sci_base_state_machine *sm)
  1120. {
  1121. struct isci_host *ihost = container_of(sm, typeof(*ihost), sm);
  1122. sci_change_state(&ihost->sm, SCIC_RESET);
  1123. }
  1124. static inline void sci_controller_starting_state_exit(struct sci_base_state_machine *sm)
  1125. {
  1126. struct isci_host *ihost = container_of(sm, typeof(*ihost), sm);
  1127. sci_del_timer(&ihost->timer);
  1128. }
  1129. #define INTERRUPT_COALESCE_TIMEOUT_BASE_RANGE_LOWER_BOUND_NS 853
  1130. #define INTERRUPT_COALESCE_TIMEOUT_BASE_RANGE_UPPER_BOUND_NS 1280
  1131. #define INTERRUPT_COALESCE_TIMEOUT_MAX_US 2700000
  1132. #define INTERRUPT_COALESCE_NUMBER_MAX 256
  1133. #define INTERRUPT_COALESCE_TIMEOUT_ENCODE_MIN 7
  1134. #define INTERRUPT_COALESCE_TIMEOUT_ENCODE_MAX 28
  1135. /**
  1136. * sci_controller_set_interrupt_coalescence() - This method allows the user to
  1137. * configure the interrupt coalescence.
  1138. * @controller: This parameter represents the handle to the controller object
  1139. * for which its interrupt coalesce register is overridden.
  1140. * @coalesce_number: Used to control the number of entries in the Completion
  1141. * Queue before an interrupt is generated. If the number of entries exceed
  1142. * this number, an interrupt will be generated. The valid range of the input
  1143. * is [0, 256]. A setting of 0 results in coalescing being disabled.
  1144. * @coalesce_timeout: Timeout value in microseconds. The valid range of the
  1145. * input is [0, 2700000] . A setting of 0 is allowed and results in no
  1146. * interrupt coalescing timeout.
  1147. *
  1148. * Indicate if the user successfully set the interrupt coalesce parameters.
  1149. * SCI_SUCCESS The user successfully updated the interrutp coalescence.
  1150. * SCI_FAILURE_INVALID_PARAMETER_VALUE The user input value is out of range.
  1151. */
  1152. static enum sci_status
  1153. sci_controller_set_interrupt_coalescence(struct isci_host *ihost,
  1154. u32 coalesce_number,
  1155. u32 coalesce_timeout)
  1156. {
  1157. u8 timeout_encode = 0;
  1158. u32 min = 0;
  1159. u32 max = 0;
  1160. /* Check if the input parameters fall in the range. */
  1161. if (coalesce_number > INTERRUPT_COALESCE_NUMBER_MAX)
  1162. return SCI_FAILURE_INVALID_PARAMETER_VALUE;
  1163. /*
  1164. * Defined encoding for interrupt coalescing timeout:
  1165. * Value Min Max Units
  1166. * ----- --- --- -----
  1167. * 0 - - Disabled
  1168. * 1 13.3 20.0 ns
  1169. * 2 26.7 40.0
  1170. * 3 53.3 80.0
  1171. * 4 106.7 160.0
  1172. * 5 213.3 320.0
  1173. * 6 426.7 640.0
  1174. * 7 853.3 1280.0
  1175. * 8 1.7 2.6 us
  1176. * 9 3.4 5.1
  1177. * 10 6.8 10.2
  1178. * 11 13.7 20.5
  1179. * 12 27.3 41.0
  1180. * 13 54.6 81.9
  1181. * 14 109.2 163.8
  1182. * 15 218.5 327.7
  1183. * 16 436.9 655.4
  1184. * 17 873.8 1310.7
  1185. * 18 1.7 2.6 ms
  1186. * 19 3.5 5.2
  1187. * 20 7.0 10.5
  1188. * 21 14.0 21.0
  1189. * 22 28.0 41.9
  1190. * 23 55.9 83.9
  1191. * 24 111.8 167.8
  1192. * 25 223.7 335.5
  1193. * 26 447.4 671.1
  1194. * 27 894.8 1342.2
  1195. * 28 1.8 2.7 s
  1196. * Others Undefined */
  1197. /*
  1198. * Use the table above to decide the encode of interrupt coalescing timeout
  1199. * value for register writing. */
  1200. if (coalesce_timeout == 0)
  1201. timeout_encode = 0;
  1202. else{
  1203. /* make the timeout value in unit of (10 ns). */
  1204. coalesce_timeout = coalesce_timeout * 100;
  1205. min = INTERRUPT_COALESCE_TIMEOUT_BASE_RANGE_LOWER_BOUND_NS / 10;
  1206. max = INTERRUPT_COALESCE_TIMEOUT_BASE_RANGE_UPPER_BOUND_NS / 10;
  1207. /* get the encode of timeout for register writing. */
  1208. for (timeout_encode = INTERRUPT_COALESCE_TIMEOUT_ENCODE_MIN;
  1209. timeout_encode <= INTERRUPT_COALESCE_TIMEOUT_ENCODE_MAX;
  1210. timeout_encode++) {
  1211. if (min <= coalesce_timeout && max > coalesce_timeout)
  1212. break;
  1213. else if (coalesce_timeout >= max && coalesce_timeout < min * 2
  1214. && coalesce_timeout <= INTERRUPT_COALESCE_TIMEOUT_MAX_US * 100) {
  1215. if ((coalesce_timeout - max) < (2 * min - coalesce_timeout))
  1216. break;
  1217. else{
  1218. timeout_encode++;
  1219. break;
  1220. }
  1221. } else {
  1222. max = max * 2;
  1223. min = min * 2;
  1224. }
  1225. }
  1226. if (timeout_encode == INTERRUPT_COALESCE_TIMEOUT_ENCODE_MAX + 1)
  1227. /* the value is out of range. */
  1228. return SCI_FAILURE_INVALID_PARAMETER_VALUE;
  1229. }
  1230. writel(SMU_ICC_GEN_VAL(NUMBER, coalesce_number) |
  1231. SMU_ICC_GEN_VAL(TIMER, timeout_encode),
  1232. &ihost->smu_registers->interrupt_coalesce_control);
  1233. ihost->interrupt_coalesce_number = (u16)coalesce_number;
  1234. ihost->interrupt_coalesce_timeout = coalesce_timeout / 100;
  1235. return SCI_SUCCESS;
  1236. }
  1237. static void sci_controller_ready_state_enter(struct sci_base_state_machine *sm)
  1238. {
  1239. struct isci_host *ihost = container_of(sm, typeof(*ihost), sm);
  1240. u32 val;
  1241. /* enable clock gating for power control of the scu unit */
  1242. val = readl(&ihost->smu_registers->clock_gating_control);
  1243. val &= ~(SMU_CGUCR_GEN_BIT(REGCLK_ENABLE) |
  1244. SMU_CGUCR_GEN_BIT(TXCLK_ENABLE) |
  1245. SMU_CGUCR_GEN_BIT(XCLK_ENABLE));
  1246. val |= SMU_CGUCR_GEN_BIT(IDLE_ENABLE);
  1247. writel(val, &ihost->smu_registers->clock_gating_control);
  1248. /* set the default interrupt coalescence number and timeout value. */
  1249. sci_controller_set_interrupt_coalescence(ihost, 0, 0);
  1250. }
  1251. static void sci_controller_ready_state_exit(struct sci_base_state_machine *sm)
  1252. {
  1253. struct isci_host *ihost = container_of(sm, typeof(*ihost), sm);
  1254. /* disable interrupt coalescence. */
  1255. sci_controller_set_interrupt_coalescence(ihost, 0, 0);
  1256. }
  1257. static enum sci_status sci_controller_stop_phys(struct isci_host *ihost)
  1258. {
  1259. u32 index;
  1260. enum sci_status status;
  1261. enum sci_status phy_status;
  1262. status = SCI_SUCCESS;
  1263. for (index = 0; index < SCI_MAX_PHYS; index++) {
  1264. phy_status = sci_phy_stop(&ihost->phys[index]);
  1265. if (phy_status != SCI_SUCCESS &&
  1266. phy_status != SCI_FAILURE_INVALID_STATE) {
  1267. status = SCI_FAILURE;
  1268. dev_warn(&ihost->pdev->dev,
  1269. "%s: Controller stop operation failed to stop "
  1270. "phy %d because of status %d.\n",
  1271. __func__,
  1272. ihost->phys[index].phy_index, phy_status);
  1273. }
  1274. }
  1275. return status;
  1276. }
  1277. static enum sci_status sci_controller_stop_ports(struct isci_host *ihost)
  1278. {
  1279. u32 index;
  1280. enum sci_status port_status;
  1281. enum sci_status status = SCI_SUCCESS;
  1282. for (index = 0; index < ihost->logical_port_entries; index++) {
  1283. struct isci_port *iport = &ihost->ports[index];
  1284. port_status = sci_port_stop(iport);
  1285. if ((port_status != SCI_SUCCESS) &&
  1286. (port_status != SCI_FAILURE_INVALID_STATE)) {
  1287. status = SCI_FAILURE;
  1288. dev_warn(&ihost->pdev->dev,
  1289. "%s: Controller stop operation failed to "
  1290. "stop port %d because of status %d.\n",
  1291. __func__,
  1292. iport->logical_port_index,
  1293. port_status);
  1294. }
  1295. }
  1296. return status;
  1297. }
  1298. static enum sci_status sci_controller_stop_devices(struct isci_host *ihost)
  1299. {
  1300. u32 index;
  1301. enum sci_status status;
  1302. enum sci_status device_status;
  1303. status = SCI_SUCCESS;
  1304. for (index = 0; index < ihost->remote_node_entries; index++) {
  1305. if (ihost->device_table[index] != NULL) {
  1306. /* / @todo What timeout value do we want to provide to this request? */
  1307. device_status = sci_remote_device_stop(ihost->device_table[index], 0);
  1308. if ((device_status != SCI_SUCCESS) &&
  1309. (device_status != SCI_FAILURE_INVALID_STATE)) {
  1310. dev_warn(&ihost->pdev->dev,
  1311. "%s: Controller stop operation failed "
  1312. "to stop device 0x%p because of "
  1313. "status %d.\n",
  1314. __func__,
  1315. ihost->device_table[index], device_status);
  1316. }
  1317. }
  1318. }
  1319. return status;
  1320. }
  1321. static void sci_controller_stopping_state_enter(struct sci_base_state_machine *sm)
  1322. {
  1323. struct isci_host *ihost = container_of(sm, typeof(*ihost), sm);
  1324. /* Stop all of the components for this controller */
  1325. sci_controller_stop_phys(ihost);
  1326. sci_controller_stop_ports(ihost);
  1327. sci_controller_stop_devices(ihost);
  1328. }
  1329. static void sci_controller_stopping_state_exit(struct sci_base_state_machine *sm)
  1330. {
  1331. struct isci_host *ihost = container_of(sm, typeof(*ihost), sm);
  1332. sci_del_timer(&ihost->timer);
  1333. }
  1334. static void sci_controller_reset_hardware(struct isci_host *ihost)
  1335. {
  1336. /* Disable interrupts so we dont take any spurious interrupts */
  1337. sci_controller_disable_interrupts(ihost);
  1338. /* Reset the SCU */
  1339. writel(0xFFFFFFFF, &ihost->smu_registers->soft_reset_control);
  1340. /* Delay for 1ms to before clearing the CQP and UFQPR. */
  1341. udelay(1000);
  1342. /* The write to the CQGR clears the CQP */
  1343. writel(0x00000000, &ihost->smu_registers->completion_queue_get);
  1344. /* The write to the UFQGP clears the UFQPR */
  1345. writel(0, &ihost->scu_registers->sdma.unsolicited_frame_get_pointer);
  1346. }
  1347. static void sci_controller_resetting_state_enter(struct sci_base_state_machine *sm)
  1348. {
  1349. struct isci_host *ihost = container_of(sm, typeof(*ihost), sm);
  1350. sci_controller_reset_hardware(ihost);
  1351. sci_change_state(&ihost->sm, SCIC_RESET);
  1352. }
  1353. static const struct sci_base_state sci_controller_state_table[] = {
  1354. [SCIC_INITIAL] = {
  1355. .enter_state = sci_controller_initial_state_enter,
  1356. },
  1357. [SCIC_RESET] = {},
  1358. [SCIC_INITIALIZING] = {},
  1359. [SCIC_INITIALIZED] = {},
  1360. [SCIC_STARTING] = {
  1361. .exit_state = sci_controller_starting_state_exit,
  1362. },
  1363. [SCIC_READY] = {
  1364. .enter_state = sci_controller_ready_state_enter,
  1365. .exit_state = sci_controller_ready_state_exit,
  1366. },
  1367. [SCIC_RESETTING] = {
  1368. .enter_state = sci_controller_resetting_state_enter,
  1369. },
  1370. [SCIC_STOPPING] = {
  1371. .enter_state = sci_controller_stopping_state_enter,
  1372. .exit_state = sci_controller_stopping_state_exit,
  1373. },
  1374. [SCIC_STOPPED] = {},
  1375. [SCIC_FAILED] = {}
  1376. };
  1377. static void sci_controller_set_default_config_parameters(struct isci_host *ihost)
  1378. {
  1379. /* these defaults are overridden by the platform / firmware */
  1380. u16 index;
  1381. /* Default to APC mode. */
  1382. ihost->oem_parameters.controller.mode_type = SCIC_PORT_AUTOMATIC_CONFIGURATION_MODE;
  1383. /* Default to APC mode. */
  1384. ihost->oem_parameters.controller.max_concurr_spin_up = 1;
  1385. /* Default to no SSC operation. */
  1386. ihost->oem_parameters.controller.do_enable_ssc = false;
  1387. /* Default to short cables on all phys. */
  1388. ihost->oem_parameters.controller.cable_selection_mask = 0;
  1389. /* Initialize all of the port parameter information to narrow ports. */
  1390. for (index = 0; index < SCI_MAX_PORTS; index++) {
  1391. ihost->oem_parameters.ports[index].phy_mask = 0;
  1392. }
  1393. /* Initialize all of the phy parameter information. */
  1394. for (index = 0; index < SCI_MAX_PHYS; index++) {
  1395. /* Default to 3G (i.e. Gen 2). */
  1396. ihost->user_parameters.phys[index].max_speed_generation =
  1397. SCIC_SDS_PARM_GEN2_SPEED;
  1398. /* the frequencies cannot be 0 */
  1399. ihost->user_parameters.phys[index].align_insertion_frequency = 0x7f;
  1400. ihost->user_parameters.phys[index].in_connection_align_insertion_frequency = 0xff;
  1401. ihost->user_parameters.phys[index].notify_enable_spin_up_insertion_frequency = 0x33;
  1402. /*
  1403. * Previous Vitesse based expanders had a arbitration issue that
  1404. * is worked around by having the upper 32-bits of SAS address
  1405. * with a value greater then the Vitesse company identifier.
  1406. * Hence, usage of 0x5FCFFFFF. */
  1407. ihost->oem_parameters.phys[index].sas_address.low = 0x1 + ihost->id;
  1408. ihost->oem_parameters.phys[index].sas_address.high = 0x5FCFFFFF;
  1409. }
  1410. ihost->user_parameters.stp_inactivity_timeout = 5;
  1411. ihost->user_parameters.ssp_inactivity_timeout = 5;
  1412. ihost->user_parameters.stp_max_occupancy_timeout = 5;
  1413. ihost->user_parameters.ssp_max_occupancy_timeout = 20;
  1414. ihost->user_parameters.no_outbound_task_timeout = 2;
  1415. }
  1416. static void controller_timeout(unsigned long data)
  1417. {
  1418. struct sci_timer *tmr = (struct sci_timer *)data;
  1419. struct isci_host *ihost = container_of(tmr, typeof(*ihost), timer);
  1420. struct sci_base_state_machine *sm = &ihost->sm;
  1421. unsigned long flags;
  1422. spin_lock_irqsave(&ihost->scic_lock, flags);
  1423. if (tmr->cancel)
  1424. goto done;
  1425. if (sm->current_state_id == SCIC_STARTING)
  1426. sci_controller_transition_to_ready(ihost, SCI_FAILURE_TIMEOUT);
  1427. else if (sm->current_state_id == SCIC_STOPPING) {
  1428. sci_change_state(sm, SCIC_FAILED);
  1429. isci_host_stop_complete(ihost, SCI_FAILURE_TIMEOUT);
  1430. } else /* / @todo Now what do we want to do in this case? */
  1431. dev_err(&ihost->pdev->dev,
  1432. "%s: Controller timer fired when controller was not "
  1433. "in a state being timed.\n",
  1434. __func__);
  1435. done:
  1436. spin_unlock_irqrestore(&ihost->scic_lock, flags);
  1437. }
  1438. static enum sci_status sci_controller_construct(struct isci_host *ihost,
  1439. void __iomem *scu_base,
  1440. void __iomem *smu_base)
  1441. {
  1442. u8 i;
  1443. sci_init_sm(&ihost->sm, sci_controller_state_table, SCIC_INITIAL);
  1444. ihost->scu_registers = scu_base;
  1445. ihost->smu_registers = smu_base;
  1446. sci_port_configuration_agent_construct(&ihost->port_agent);
  1447. /* Construct the ports for this controller */
  1448. for (i = 0; i < SCI_MAX_PORTS; i++)
  1449. sci_port_construct(&ihost->ports[i], i, ihost);
  1450. sci_port_construct(&ihost->ports[i], SCIC_SDS_DUMMY_PORT, ihost);
  1451. /* Construct the phys for this controller */
  1452. for (i = 0; i < SCI_MAX_PHYS; i++) {
  1453. /* Add all the PHYs to the dummy port */
  1454. sci_phy_construct(&ihost->phys[i],
  1455. &ihost->ports[SCI_MAX_PORTS], i);
  1456. }
  1457. ihost->invalid_phy_mask = 0;
  1458. sci_init_timer(&ihost->timer, controller_timeout);
  1459. /* Initialize the User and OEM parameters to default values. */
  1460. sci_controller_set_default_config_parameters(ihost);
  1461. return sci_controller_reset(ihost);
  1462. }
  1463. int sci_oem_parameters_validate(struct sci_oem_params *oem, u8 version)
  1464. {
  1465. int i;
  1466. for (i = 0; i < SCI_MAX_PORTS; i++)
  1467. if (oem->ports[i].phy_mask > SCIC_SDS_PARM_PHY_MASK_MAX)
  1468. return -EINVAL;
  1469. for (i = 0; i < SCI_MAX_PHYS; i++)
  1470. if (oem->phys[i].sas_address.high == 0 &&
  1471. oem->phys[i].sas_address.low == 0)
  1472. return -EINVAL;
  1473. if (oem->controller.mode_type == SCIC_PORT_AUTOMATIC_CONFIGURATION_MODE) {
  1474. for (i = 0; i < SCI_MAX_PHYS; i++)
  1475. if (oem->ports[i].phy_mask != 0)
  1476. return -EINVAL;
  1477. } else if (oem->controller.mode_type == SCIC_PORT_MANUAL_CONFIGURATION_MODE) {
  1478. u8 phy_mask = 0;
  1479. for (i = 0; i < SCI_MAX_PHYS; i++)
  1480. phy_mask |= oem->ports[i].phy_mask;
  1481. if (phy_mask == 0)
  1482. return -EINVAL;
  1483. } else
  1484. return -EINVAL;
  1485. if (oem->controller.max_concurr_spin_up > MAX_CONCURRENT_DEVICE_SPIN_UP_COUNT ||
  1486. oem->controller.max_concurr_spin_up < 1)
  1487. return -EINVAL;
  1488. if (oem->controller.do_enable_ssc) {
  1489. if (version < ISCI_ROM_VER_1_1 && oem->controller.do_enable_ssc != 1)
  1490. return -EINVAL;
  1491. if (version >= ISCI_ROM_VER_1_1) {
  1492. u8 test = oem->controller.ssc_sata_tx_spread_level;
  1493. switch (test) {
  1494. case 0:
  1495. case 2:
  1496. case 3:
  1497. case 6:
  1498. case 7:
  1499. break;
  1500. default:
  1501. return -EINVAL;
  1502. }
  1503. test = oem->controller.ssc_sas_tx_spread_level;
  1504. if (oem->controller.ssc_sas_tx_type == 0) {
  1505. switch (test) {
  1506. case 0:
  1507. case 2:
  1508. case 3:
  1509. break;
  1510. default:
  1511. return -EINVAL;
  1512. }
  1513. } else if (oem->controller.ssc_sas_tx_type == 1) {
  1514. switch (test) {
  1515. case 0:
  1516. case 3:
  1517. case 6:
  1518. break;
  1519. default:
  1520. return -EINVAL;
  1521. }
  1522. }
  1523. }
  1524. }
  1525. return 0;
  1526. }
  1527. static enum sci_status sci_oem_parameters_set(struct isci_host *ihost)
  1528. {
  1529. u32 state = ihost->sm.current_state_id;
  1530. struct isci_pci_info *pci_info = to_pci_info(ihost->pdev);
  1531. if (state == SCIC_RESET ||
  1532. state == SCIC_INITIALIZING ||
  1533. state == SCIC_INITIALIZED) {
  1534. u8 oem_version = pci_info->orom ? pci_info->orom->hdr.version :
  1535. ISCI_ROM_VER_1_0;
  1536. if (sci_oem_parameters_validate(&ihost->oem_parameters,
  1537. oem_version))
  1538. return SCI_FAILURE_INVALID_PARAMETER_VALUE;
  1539. return SCI_SUCCESS;
  1540. }
  1541. return SCI_FAILURE_INVALID_STATE;
  1542. }
  1543. static u8 max_spin_up(struct isci_host *ihost)
  1544. {
  1545. if (ihost->user_parameters.max_concurr_spinup)
  1546. return min_t(u8, ihost->user_parameters.max_concurr_spinup,
  1547. MAX_CONCURRENT_DEVICE_SPIN_UP_COUNT);
  1548. else
  1549. return min_t(u8, ihost->oem_parameters.controller.max_concurr_spin_up,
  1550. MAX_CONCURRENT_DEVICE_SPIN_UP_COUNT);
  1551. }
  1552. static void power_control_timeout(unsigned long data)
  1553. {
  1554. struct sci_timer *tmr = (struct sci_timer *)data;
  1555. struct isci_host *ihost = container_of(tmr, typeof(*ihost), power_control.timer);
  1556. struct isci_phy *iphy;
  1557. unsigned long flags;
  1558. u8 i;
  1559. spin_lock_irqsave(&ihost->scic_lock, flags);
  1560. if (tmr->cancel)
  1561. goto done;
  1562. ihost->power_control.phys_granted_power = 0;
  1563. if (ihost->power_control.phys_waiting == 0) {
  1564. ihost->power_control.timer_started = false;
  1565. goto done;
  1566. }
  1567. for (i = 0; i < SCI_MAX_PHYS; i++) {
  1568. if (ihost->power_control.phys_waiting == 0)
  1569. break;
  1570. iphy = ihost->power_control.requesters[i];
  1571. if (iphy == NULL)
  1572. continue;
  1573. if (ihost->power_control.phys_granted_power >= max_spin_up(ihost))
  1574. break;
  1575. ihost->power_control.requesters[i] = NULL;
  1576. ihost->power_control.phys_waiting--;
  1577. ihost->power_control.phys_granted_power++;
  1578. sci_phy_consume_power_handler(iphy);
  1579. if (iphy->protocol == SAS_PROTOCOL_SSP) {
  1580. u8 j;
  1581. for (j = 0; j < SCI_MAX_PHYS; j++) {
  1582. struct isci_phy *requester = ihost->power_control.requesters[j];
  1583. /*
  1584. * Search the power_control queue to see if there are other phys
  1585. * attached to the same remote device. If found, take all of
  1586. * them out of await_sas_power state.
  1587. */
  1588. if (requester != NULL && requester != iphy) {
  1589. u8 other = memcmp(requester->frame_rcvd.iaf.sas_addr,
  1590. iphy->frame_rcvd.iaf.sas_addr,
  1591. sizeof(requester->frame_rcvd.iaf.sas_addr));
  1592. if (other == 0) {
  1593. ihost->power_control.requesters[j] = NULL;
  1594. ihost->power_control.phys_waiting--;
  1595. sci_phy_consume_power_handler(requester);
  1596. }
  1597. }
  1598. }
  1599. }
  1600. }
  1601. /*
  1602. * It doesn't matter if the power list is empty, we need to start the
  1603. * timer in case another phy becomes ready.
  1604. */
  1605. sci_mod_timer(tmr, SCIC_SDS_CONTROLLER_POWER_CONTROL_INTERVAL);
  1606. ihost->power_control.timer_started = true;
  1607. done:
  1608. spin_unlock_irqrestore(&ihost->scic_lock, flags);
  1609. }
  1610. void sci_controller_power_control_queue_insert(struct isci_host *ihost,
  1611. struct isci_phy *iphy)
  1612. {
  1613. BUG_ON(iphy == NULL);
  1614. if (ihost->power_control.phys_granted_power < max_spin_up(ihost)) {
  1615. ihost->power_control.phys_granted_power++;
  1616. sci_phy_consume_power_handler(iphy);
  1617. /*
  1618. * stop and start the power_control timer. When the timer fires, the
  1619. * no_of_phys_granted_power will be set to 0
  1620. */
  1621. if (ihost->power_control.timer_started)
  1622. sci_del_timer(&ihost->power_control.timer);
  1623. sci_mod_timer(&ihost->power_control.timer,
  1624. SCIC_SDS_CONTROLLER_POWER_CONTROL_INTERVAL);
  1625. ihost->power_control.timer_started = true;
  1626. } else {
  1627. /*
  1628. * There are phys, attached to the same sas address as this phy, are
  1629. * already in READY state, this phy don't need wait.
  1630. */
  1631. u8 i;
  1632. struct isci_phy *current_phy;
  1633. for (i = 0; i < SCI_MAX_PHYS; i++) {
  1634. u8 other;
  1635. current_phy = &ihost->phys[i];
  1636. other = memcmp(current_phy->frame_rcvd.iaf.sas_addr,
  1637. iphy->frame_rcvd.iaf.sas_addr,
  1638. sizeof(current_phy->frame_rcvd.iaf.sas_addr));
  1639. if (current_phy->sm.current_state_id == SCI_PHY_READY &&
  1640. current_phy->protocol == SAS_PROTOCOL_SSP &&
  1641. other == 0) {
  1642. sci_phy_consume_power_handler(iphy);
  1643. break;
  1644. }
  1645. }
  1646. if (i == SCI_MAX_PHYS) {
  1647. /* Add the phy in the waiting list */
  1648. ihost->power_control.requesters[iphy->phy_index] = iphy;
  1649. ihost->power_control.phys_waiting++;
  1650. }
  1651. }
  1652. }
  1653. void sci_controller_power_control_queue_remove(struct isci_host *ihost,
  1654. struct isci_phy *iphy)
  1655. {
  1656. BUG_ON(iphy == NULL);
  1657. if (ihost->power_control.requesters[iphy->phy_index])
  1658. ihost->power_control.phys_waiting--;
  1659. ihost->power_control.requesters[iphy->phy_index] = NULL;
  1660. }
  1661. static int is_long_cable(int phy, unsigned char selection_byte)
  1662. {
  1663. return !!(selection_byte & (1 << phy));
  1664. }
  1665. static int is_medium_cable(int phy, unsigned char selection_byte)
  1666. {
  1667. return !!(selection_byte & (1 << (phy + 4)));
  1668. }
  1669. static enum cable_selections decode_selection_byte(
  1670. int phy,
  1671. unsigned char selection_byte)
  1672. {
  1673. return ((selection_byte & (1 << phy)) ? 1 : 0)
  1674. + (selection_byte & (1 << (phy + 4)) ? 2 : 0);
  1675. }
  1676. static unsigned char *to_cable_select(struct isci_host *ihost)
  1677. {
  1678. if (is_cable_select_overridden())
  1679. return ((unsigned char *)&cable_selection_override)
  1680. + ihost->id;
  1681. else
  1682. return &ihost->oem_parameters.controller.cable_selection_mask;
  1683. }
  1684. enum cable_selections decode_cable_selection(struct isci_host *ihost, int phy)
  1685. {
  1686. return decode_selection_byte(phy, *to_cable_select(ihost));
  1687. }
  1688. char *lookup_cable_names(enum cable_selections selection)
  1689. {
  1690. static char *cable_names[] = {
  1691. [short_cable] = "short",
  1692. [long_cable] = "long",
  1693. [medium_cable] = "medium",
  1694. [undefined_cable] = "<undefined, assumed long>" /* bit 0==1 */
  1695. };
  1696. return (selection <= undefined_cable) ? cable_names[selection]
  1697. : cable_names[undefined_cable];
  1698. }
  1699. #define AFE_REGISTER_WRITE_DELAY 10
  1700. static void sci_controller_afe_initialization(struct isci_host *ihost)
  1701. {
  1702. struct scu_afe_registers __iomem *afe = &ihost->scu_registers->afe;
  1703. const struct sci_oem_params *oem = &ihost->oem_parameters;
  1704. struct pci_dev *pdev = ihost->pdev;
  1705. u32 afe_status;
  1706. u32 phy_id;
  1707. unsigned char cable_selection_mask = *to_cable_select(ihost);
  1708. /* Clear DFX Status registers */
  1709. writel(0x0081000f, &afe->afe_dfx_master_control0);
  1710. udelay(AFE_REGISTER_WRITE_DELAY);
  1711. if (is_b0(pdev) || is_c0(pdev) || is_c1(pdev)) {
  1712. /* PM Rx Equalization Save, PM SPhy Rx Acknowledgement
  1713. * Timer, PM Stagger Timer
  1714. */
  1715. writel(0x0007FFFF, &afe->afe_pmsn_master_control2);
  1716. udelay(AFE_REGISTER_WRITE_DELAY);
  1717. }
  1718. /* Configure bias currents to normal */
  1719. if (is_a2(pdev))
  1720. writel(0x00005A00, &afe->afe_bias_control);
  1721. else if (is_b0(pdev) || is_c0(pdev))
  1722. writel(0x00005F00, &afe->afe_bias_control);
  1723. else if (is_c1(pdev))
  1724. writel(0x00005500, &afe->afe_bias_control);
  1725. udelay(AFE_REGISTER_WRITE_DELAY);
  1726. /* Enable PLL */
  1727. if (is_a2(pdev))
  1728. writel(0x80040908, &afe->afe_pll_control0);
  1729. else if (is_b0(pdev) || is_c0(pdev))
  1730. writel(0x80040A08, &afe->afe_pll_control0);
  1731. else if (is_c1(pdev)) {
  1732. writel(0x80000B08, &afe->afe_pll_control0);
  1733. udelay(AFE_REGISTER_WRITE_DELAY);
  1734. writel(0x00000B08, &afe->afe_pll_control0);
  1735. udelay(AFE_REGISTER_WRITE_DELAY);
  1736. writel(0x80000B08, &afe->afe_pll_control0);
  1737. }
  1738. udelay(AFE_REGISTER_WRITE_DELAY);
  1739. /* Wait for the PLL to lock */
  1740. do {
  1741. afe_status = readl(&afe->afe_common_block_status);
  1742. udelay(AFE_REGISTER_WRITE_DELAY);
  1743. } while ((afe_status & 0x00001000) == 0);
  1744. if (is_a2(pdev)) {
  1745. /* Shorten SAS SNW lock time (RxLock timer value from 76
  1746. * us to 50 us)
  1747. */
  1748. writel(0x7bcc96ad, &afe->afe_pmsn_master_control0);
  1749. udelay(AFE_REGISTER_WRITE_DELAY);
  1750. }
  1751. for (phy_id = 0; phy_id < SCI_MAX_PHYS; phy_id++) {
  1752. struct scu_afe_transceiver *xcvr = &afe->scu_afe_xcvr[phy_id];
  1753. const struct sci_phy_oem_params *oem_phy = &oem->phys[phy_id];
  1754. int cable_length_long =
  1755. is_long_cable(phy_id, cable_selection_mask);
  1756. int cable_length_medium =
  1757. is_medium_cable(phy_id, cable_selection_mask);
  1758. if (is_a2(pdev)) {
  1759. /* All defaults, except the Receive Word
  1760. * Alignament/Comma Detect Enable....(0xe800)
  1761. */
  1762. writel(0x00004512, &xcvr->afe_xcvr_control0);
  1763. udelay(AFE_REGISTER_WRITE_DELAY);
  1764. writel(0x0050100F, &xcvr->afe_xcvr_control1);
  1765. udelay(AFE_REGISTER_WRITE_DELAY);
  1766. } else if (is_b0(pdev)) {
  1767. /* Configure transmitter SSC parameters */
  1768. writel(0x00030000, &xcvr->afe_tx_ssc_control);
  1769. udelay(AFE_REGISTER_WRITE_DELAY);
  1770. } else if (is_c0(pdev)) {
  1771. /* Configure transmitter SSC parameters */
  1772. writel(0x00010202, &xcvr->afe_tx_ssc_control);
  1773. udelay(AFE_REGISTER_WRITE_DELAY);
  1774. /* All defaults, except the Receive Word
  1775. * Alignament/Comma Detect Enable....(0xe800)
  1776. */
  1777. writel(0x00014500, &xcvr->afe_xcvr_control0);
  1778. udelay(AFE_REGISTER_WRITE_DELAY);
  1779. } else if (is_c1(pdev)) {
  1780. /* Configure transmitter SSC parameters */
  1781. writel(0x00010202, &xcvr->afe_tx_ssc_control);
  1782. udelay(AFE_REGISTER_WRITE_DELAY);
  1783. /* All defaults, except the Receive Word
  1784. * Alignament/Comma Detect Enable....(0xe800)
  1785. */
  1786. writel(0x0001C500, &xcvr->afe_xcvr_control0);
  1787. udelay(AFE_REGISTER_WRITE_DELAY);
  1788. }
  1789. /* Power up TX and RX out from power down (PWRDNTX and
  1790. * PWRDNRX) & increase TX int & ext bias 20%....(0xe85c)
  1791. */
  1792. if (is_a2(pdev))
  1793. writel(0x000003F0, &xcvr->afe_channel_control);
  1794. else if (is_b0(pdev)) {
  1795. writel(0x000003D7, &xcvr->afe_channel_control);
  1796. udelay(AFE_REGISTER_WRITE_DELAY);
  1797. writel(0x000003D4, &xcvr->afe_channel_control);
  1798. } else if (is_c0(pdev)) {
  1799. writel(0x000001E7, &xcvr->afe_channel_control);
  1800. udelay(AFE_REGISTER_WRITE_DELAY);
  1801. writel(0x000001E4, &xcvr->afe_channel_control);
  1802. } else if (is_c1(pdev)) {
  1803. writel(cable_length_long ? 0x000002F7 : 0x000001F7,
  1804. &xcvr->afe_channel_control);
  1805. udelay(AFE_REGISTER_WRITE_DELAY);
  1806. writel(cable_length_long ? 0x000002F4 : 0x000001F4,
  1807. &xcvr->afe_channel_control);
  1808. }
  1809. udelay(AFE_REGISTER_WRITE_DELAY);
  1810. if (is_a2(pdev)) {
  1811. /* Enable TX equalization (0xe824) */
  1812. writel(0x00040000, &xcvr->afe_tx_control);
  1813. udelay(AFE_REGISTER_WRITE_DELAY);
  1814. }
  1815. if (is_a2(pdev) || is_b0(pdev))
  1816. /* RDPI=0x0(RX Power On), RXOOBDETPDNC=0x0,
  1817. * TPD=0x0(TX Power On), RDD=0x0(RX Detect
  1818. * Enabled) ....(0xe800)
  1819. */
  1820. writel(0x00004100, &xcvr->afe_xcvr_control0);
  1821. else if (is_c0(pdev))
  1822. writel(0x00014100, &xcvr->afe_xcvr_control0);
  1823. else if (is_c1(pdev))
  1824. writel(0x0001C100, &xcvr->afe_xcvr_control0);
  1825. udelay(AFE_REGISTER_WRITE_DELAY);
  1826. /* Leave DFE/FFE on */
  1827. if (is_a2(pdev))
  1828. writel(0x3F11103F, &xcvr->afe_rx_ssc_control0);
  1829. else if (is_b0(pdev)) {
  1830. writel(0x3F11103F, &xcvr->afe_rx_ssc_control0);
  1831. udelay(AFE_REGISTER_WRITE_DELAY);
  1832. /* Enable TX equalization (0xe824) */
  1833. writel(0x00040000, &xcvr->afe_tx_control);
  1834. } else if (is_c0(pdev)) {
  1835. writel(0x01400C0F, &xcvr->afe_rx_ssc_control1);
  1836. udelay(AFE_REGISTER_WRITE_DELAY);
  1837. writel(0x3F6F103F, &xcvr->afe_rx_ssc_control0);
  1838. udelay(AFE_REGISTER_WRITE_DELAY);
  1839. /* Enable TX equalization (0xe824) */
  1840. writel(0x00040000, &xcvr->afe_tx_control);
  1841. } else if (is_c1(pdev)) {
  1842. writel(cable_length_long ? 0x01500C0C :
  1843. cable_length_medium ? 0x01400C0D : 0x02400C0D,
  1844. &xcvr->afe_xcvr_control1);
  1845. udelay(AFE_REGISTER_WRITE_DELAY);
  1846. writel(0x000003E0, &xcvr->afe_dfx_rx_control1);
  1847. udelay(AFE_REGISTER_WRITE_DELAY);
  1848. writel(cable_length_long ? 0x33091C1F :
  1849. cable_length_medium ? 0x3315181F : 0x2B17161F,
  1850. &xcvr->afe_rx_ssc_control0);
  1851. udelay(AFE_REGISTER_WRITE_DELAY);
  1852. /* Enable TX equalization (0xe824) */
  1853. writel(0x00040000, &xcvr->afe_tx_control);
  1854. }
  1855. udelay(AFE_REGISTER_WRITE_DELAY);
  1856. writel(oem_phy->afe_tx_amp_control0, &xcvr->afe_tx_amp_control0);
  1857. udelay(AFE_REGISTER_WRITE_DELAY);
  1858. writel(oem_phy->afe_tx_amp_control1, &xcvr->afe_tx_amp_control1);
  1859. udelay(AFE_REGISTER_WRITE_DELAY);
  1860. writel(oem_phy->afe_tx_amp_control2, &xcvr->afe_tx_amp_control2);
  1861. udelay(AFE_REGISTER_WRITE_DELAY);
  1862. writel(oem_phy->afe_tx_amp_control3, &xcvr->afe_tx_amp_control3);
  1863. udelay(AFE_REGISTER_WRITE_DELAY);
  1864. }
  1865. /* Transfer control to the PEs */
  1866. writel(0x00010f00, &afe->afe_dfx_master_control0);
  1867. udelay(AFE_REGISTER_WRITE_DELAY);
  1868. }
  1869. static void sci_controller_initialize_power_control(struct isci_host *ihost)
  1870. {
  1871. sci_init_timer(&ihost->power_control.timer, power_control_timeout);
  1872. memset(ihost->power_control.requesters, 0,
  1873. sizeof(ihost->power_control.requesters));
  1874. ihost->power_control.phys_waiting = 0;
  1875. ihost->power_control.phys_granted_power = 0;
  1876. }
  1877. static enum sci_status sci_controller_initialize(struct isci_host *ihost)
  1878. {
  1879. struct sci_base_state_machine *sm = &ihost->sm;
  1880. enum sci_status result = SCI_FAILURE;
  1881. unsigned long i, state, val;
  1882. if (ihost->sm.current_state_id != SCIC_RESET) {
  1883. dev_warn(&ihost->pdev->dev, "%s invalid state: %d\n",
  1884. __func__, ihost->sm.current_state_id);
  1885. return SCI_FAILURE_INVALID_STATE;
  1886. }
  1887. sci_change_state(sm, SCIC_INITIALIZING);
  1888. sci_init_timer(&ihost->phy_timer, phy_startup_timeout);
  1889. ihost->next_phy_to_start = 0;
  1890. ihost->phy_startup_timer_pending = false;
  1891. sci_controller_initialize_power_control(ihost);
  1892. /*
  1893. * There is nothing to do here for B0 since we do not have to
  1894. * program the AFE registers.
  1895. * / @todo The AFE settings are supposed to be correct for the B0 but
  1896. * / presently they seem to be wrong. */
  1897. sci_controller_afe_initialization(ihost);
  1898. /* Take the hardware out of reset */
  1899. writel(0, &ihost->smu_registers->soft_reset_control);
  1900. /*
  1901. * / @todo Provide meaningfull error code for hardware failure
  1902. * result = SCI_FAILURE_CONTROLLER_HARDWARE; */
  1903. for (i = 100; i >= 1; i--) {
  1904. u32 status;
  1905. /* Loop until the hardware reports success */
  1906. udelay(SCU_CONTEXT_RAM_INIT_STALL_TIME);
  1907. status = readl(&ihost->smu_registers->control_status);
  1908. if ((status & SCU_RAM_INIT_COMPLETED) == SCU_RAM_INIT_COMPLETED)
  1909. break;
  1910. }
  1911. if (i == 0)
  1912. goto out;
  1913. /*
  1914. * Determine what are the actaul device capacities that the
  1915. * hardware will support */
  1916. val = readl(&ihost->smu_registers->device_context_capacity);
  1917. /* Record the smaller of the two capacity values */
  1918. ihost->logical_port_entries = min(smu_max_ports(val), SCI_MAX_PORTS);
  1919. ihost->task_context_entries = min(smu_max_task_contexts(val), SCI_MAX_IO_REQUESTS);
  1920. ihost->remote_node_entries = min(smu_max_rncs(val), SCI_MAX_REMOTE_DEVICES);
  1921. /*
  1922. * Make all PEs that are unassigned match up with the
  1923. * logical ports
  1924. */
  1925. for (i = 0; i < ihost->logical_port_entries; i++) {
  1926. struct scu_port_task_scheduler_group_registers __iomem
  1927. *ptsg = &ihost->scu_registers->peg0.ptsg;
  1928. writel(i, &ptsg->protocol_engine[i]);
  1929. }
  1930. /* Initialize hardware PCI Relaxed ordering in DMA engines */
  1931. val = readl(&ihost->scu_registers->sdma.pdma_configuration);
  1932. val |= SCU_PDMACR_GEN_BIT(PCI_RELAXED_ORDERING_ENABLE);
  1933. writel(val, &ihost->scu_registers->sdma.pdma_configuration);
  1934. val = readl(&ihost->scu_registers->sdma.cdma_configuration);
  1935. val |= SCU_CDMACR_GEN_BIT(PCI_RELAXED_ORDERING_ENABLE);
  1936. writel(val, &ihost->scu_registers->sdma.cdma_configuration);
  1937. /*
  1938. * Initialize the PHYs before the PORTs because the PHY registers
  1939. * are accessed during the port initialization.
  1940. */
  1941. for (i = 0; i < SCI_MAX_PHYS; i++) {
  1942. result = sci_phy_initialize(&ihost->phys[i],
  1943. &ihost->scu_registers->peg0.pe[i].tl,
  1944. &ihost->scu_registers->peg0.pe[i].ll);
  1945. if (result != SCI_SUCCESS)
  1946. goto out;
  1947. }
  1948. for (i = 0; i < ihost->logical_port_entries; i++) {
  1949. struct isci_port *iport = &ihost->ports[i];
  1950. iport->port_task_scheduler_registers = &ihost->scu_registers->peg0.ptsg.port[i];
  1951. iport->port_pe_configuration_register = &ihost->scu_registers->peg0.ptsg.protocol_engine[0];
  1952. iport->viit_registers = &ihost->scu_registers->peg0.viit[i];
  1953. }
  1954. result = sci_port_configuration_agent_initialize(ihost, &ihost->port_agent);
  1955. out:
  1956. /* Advance the controller state machine */
  1957. if (result == SCI_SUCCESS)
  1958. state = SCIC_INITIALIZED;
  1959. else
  1960. state = SCIC_FAILED;
  1961. sci_change_state(sm, state);
  1962. return result;
  1963. }
  1964. static enum sci_status sci_user_parameters_set(struct isci_host *ihost,
  1965. struct sci_user_parameters *sci_parms)
  1966. {
  1967. u32 state = ihost->sm.current_state_id;
  1968. if (state == SCIC_RESET ||
  1969. state == SCIC_INITIALIZING ||
  1970. state == SCIC_INITIALIZED) {
  1971. u16 index;
  1972. /*
  1973. * Validate the user parameters. If they are not legal, then
  1974. * return a failure.
  1975. */
  1976. for (index = 0; index < SCI_MAX_PHYS; index++) {
  1977. struct sci_phy_user_params *user_phy;
  1978. user_phy = &sci_parms->phys[index];
  1979. if (!((user_phy->max_speed_generation <=
  1980. SCIC_SDS_PARM_MAX_SPEED) &&
  1981. (user_phy->max_speed_generation >
  1982. SCIC_SDS_PARM_NO_SPEED)))
  1983. return SCI_FAILURE_INVALID_PARAMETER_VALUE;
  1984. if (user_phy->in_connection_align_insertion_frequency <
  1985. 3)
  1986. return SCI_FAILURE_INVALID_PARAMETER_VALUE;
  1987. if ((user_phy->in_connection_align_insertion_frequency <
  1988. 3) ||
  1989. (user_phy->align_insertion_frequency == 0) ||
  1990. (user_phy->
  1991. notify_enable_spin_up_insertion_frequency ==
  1992. 0))
  1993. return SCI_FAILURE_INVALID_PARAMETER_VALUE;
  1994. }
  1995. if ((sci_parms->stp_inactivity_timeout == 0) ||
  1996. (sci_parms->ssp_inactivity_timeout == 0) ||
  1997. (sci_parms->stp_max_occupancy_timeout == 0) ||
  1998. (sci_parms->ssp_max_occupancy_timeout == 0) ||
  1999. (sci_parms->no_outbound_task_timeout == 0))
  2000. return SCI_FAILURE_INVALID_PARAMETER_VALUE;
  2001. memcpy(&ihost->user_parameters, sci_parms, sizeof(*sci_parms));
  2002. return SCI_SUCCESS;
  2003. }
  2004. return SCI_FAILURE_INVALID_STATE;
  2005. }
  2006. static int sci_controller_mem_init(struct isci_host *ihost)
  2007. {
  2008. struct device *dev = &ihost->pdev->dev;
  2009. dma_addr_t dma;
  2010. size_t size;
  2011. int err;
  2012. size = SCU_MAX_COMPLETION_QUEUE_ENTRIES * sizeof(u32);
  2013. ihost->completion_queue = dmam_alloc_coherent(dev, size, &dma, GFP_KERNEL);
  2014. if (!ihost->completion_queue)
  2015. return -ENOMEM;
  2016. writel(lower_32_bits(dma), &ihost->smu_registers->completion_queue_lower);
  2017. writel(upper_32_bits(dma), &ihost->smu_registers->completion_queue_upper);
  2018. size = ihost->remote_node_entries * sizeof(union scu_remote_node_context);
  2019. ihost->remote_node_context_table = dmam_alloc_coherent(dev, size, &dma,
  2020. GFP_KERNEL);
  2021. if (!ihost->remote_node_context_table)
  2022. return -ENOMEM;
  2023. writel(lower_32_bits(dma), &ihost->smu_registers->remote_node_context_lower);
  2024. writel(upper_32_bits(dma), &ihost->smu_registers->remote_node_context_upper);
  2025. size = ihost->task_context_entries * sizeof(struct scu_task_context),
  2026. ihost->task_context_table = dmam_alloc_coherent(dev, size, &dma, GFP_KERNEL);
  2027. if (!ihost->task_context_table)
  2028. return -ENOMEM;
  2029. ihost->task_context_dma = dma;
  2030. writel(lower_32_bits(dma), &ihost->smu_registers->host_task_table_lower);
  2031. writel(upper_32_bits(dma), &ihost->smu_registers->host_task_table_upper);
  2032. err = sci_unsolicited_frame_control_construct(ihost);
  2033. if (err)
  2034. return err;
  2035. /*
  2036. * Inform the silicon as to the location of the UF headers and
  2037. * address table.
  2038. */
  2039. writel(lower_32_bits(ihost->uf_control.headers.physical_address),
  2040. &ihost->scu_registers->sdma.uf_header_base_address_lower);
  2041. writel(upper_32_bits(ihost->uf_control.headers.physical_address),
  2042. &ihost->scu_registers->sdma.uf_header_base_address_upper);
  2043. writel(lower_32_bits(ihost->uf_control.address_table.physical_address),
  2044. &ihost->scu_registers->sdma.uf_address_table_lower);
  2045. writel(upper_32_bits(ihost->uf_control.address_table.physical_address),
  2046. &ihost->scu_registers->sdma.uf_address_table_upper);
  2047. return 0;
  2048. }
  2049. int isci_host_init(struct isci_host *ihost)
  2050. {
  2051. int err = 0, i;
  2052. enum sci_status status;
  2053. struct sci_user_parameters sci_user_params;
  2054. struct isci_pci_info *pci_info = to_pci_info(ihost->pdev);
  2055. spin_lock_init(&ihost->scic_lock);
  2056. init_waitqueue_head(&ihost->eventq);
  2057. status = sci_controller_construct(ihost, scu_base(ihost),
  2058. smu_base(ihost));
  2059. if (status != SCI_SUCCESS) {
  2060. dev_err(&ihost->pdev->dev,
  2061. "%s: sci_controller_construct failed - status = %x\n",
  2062. __func__,
  2063. status);
  2064. return -ENODEV;
  2065. }
  2066. ihost->sas_ha.dev = &ihost->pdev->dev;
  2067. ihost->sas_ha.lldd_ha = ihost;
  2068. /*
  2069. * grab initial values stored in the controller object for OEM and USER
  2070. * parameters
  2071. */
  2072. isci_user_parameters_get(&sci_user_params);
  2073. status = sci_user_parameters_set(ihost, &sci_user_params);
  2074. if (status != SCI_SUCCESS) {
  2075. dev_warn(&ihost->pdev->dev,
  2076. "%s: sci_user_parameters_set failed\n",
  2077. __func__);
  2078. return -ENODEV;
  2079. }
  2080. /* grab any OEM parameters specified in orom */
  2081. if (pci_info->orom) {
  2082. status = isci_parse_oem_parameters(&ihost->oem_parameters,
  2083. pci_info->orom,
  2084. ihost->id);
  2085. if (status != SCI_SUCCESS) {
  2086. dev_warn(&ihost->pdev->dev,
  2087. "parsing firmware oem parameters failed\n");
  2088. return -EINVAL;
  2089. }
  2090. }
  2091. status = sci_oem_parameters_set(ihost);
  2092. if (status != SCI_SUCCESS) {
  2093. dev_warn(&ihost->pdev->dev,
  2094. "%s: sci_oem_parameters_set failed\n",
  2095. __func__);
  2096. return -ENODEV;
  2097. }
  2098. tasklet_init(&ihost->completion_tasklet,
  2099. isci_host_completion_routine, (unsigned long)ihost);
  2100. INIT_LIST_HEAD(&ihost->requests_to_complete);
  2101. INIT_LIST_HEAD(&ihost->requests_to_errorback);
  2102. spin_lock_irq(&ihost->scic_lock);
  2103. status = sci_controller_initialize(ihost);
  2104. spin_unlock_irq(&ihost->scic_lock);
  2105. if (status != SCI_SUCCESS) {
  2106. dev_warn(&ihost->pdev->dev,
  2107. "%s: sci_controller_initialize failed -"
  2108. " status = 0x%x\n",
  2109. __func__, status);
  2110. return -ENODEV;
  2111. }
  2112. err = sci_controller_mem_init(ihost);
  2113. if (err)
  2114. return err;
  2115. for (i = 0; i < SCI_MAX_PORTS; i++) {
  2116. struct isci_port *iport = &ihost->ports[i];
  2117. INIT_LIST_HEAD(&iport->remote_dev_list);
  2118. iport->isci_host = ihost;
  2119. }
  2120. for (i = 0; i < SCI_MAX_PHYS; i++)
  2121. isci_phy_init(&ihost->phys[i], ihost, i);
  2122. /* enable sgpio */
  2123. writel(1, &ihost->scu_registers->peg0.sgpio.interface_control);
  2124. for (i = 0; i < isci_gpio_count(ihost); i++)
  2125. writel(SGPIO_HW_CONTROL, &ihost->scu_registers->peg0.sgpio.output_data_select[i]);
  2126. writel(0, &ihost->scu_registers->peg0.sgpio.vendor_specific_code);
  2127. for (i = 0; i < SCI_MAX_REMOTE_DEVICES; i++) {
  2128. struct isci_remote_device *idev = &ihost->devices[i];
  2129. INIT_LIST_HEAD(&idev->reqs_in_process);
  2130. INIT_LIST_HEAD(&idev->node);
  2131. }
  2132. for (i = 0; i < SCI_MAX_IO_REQUESTS; i++) {
  2133. struct isci_request *ireq;
  2134. dma_addr_t dma;
  2135. ireq = dmam_alloc_coherent(&ihost->pdev->dev,
  2136. sizeof(struct isci_request), &dma,
  2137. GFP_KERNEL);
  2138. if (!ireq)
  2139. return -ENOMEM;
  2140. ireq->tc = &ihost->task_context_table[i];
  2141. ireq->owning_controller = ihost;
  2142. spin_lock_init(&ireq->state_lock);
  2143. ireq->request_daddr = dma;
  2144. ireq->isci_host = ihost;
  2145. ihost->reqs[i] = ireq;
  2146. }
  2147. return 0;
  2148. }
  2149. void sci_controller_link_up(struct isci_host *ihost, struct isci_port *iport,
  2150. struct isci_phy *iphy)
  2151. {
  2152. switch (ihost->sm.current_state_id) {
  2153. case SCIC_STARTING:
  2154. sci_del_timer(&ihost->phy_timer);
  2155. ihost->phy_startup_timer_pending = false;
  2156. ihost->port_agent.link_up_handler(ihost, &ihost->port_agent,
  2157. iport, iphy);
  2158. sci_controller_start_next_phy(ihost);
  2159. break;
  2160. case SCIC_READY:
  2161. ihost->port_agent.link_up_handler(ihost, &ihost->port_agent,
  2162. iport, iphy);
  2163. break;
  2164. default:
  2165. dev_dbg(&ihost->pdev->dev,
  2166. "%s: SCIC Controller linkup event from phy %d in "
  2167. "unexpected state %d\n", __func__, iphy->phy_index,
  2168. ihost->sm.current_state_id);
  2169. }
  2170. }
  2171. void sci_controller_link_down(struct isci_host *ihost, struct isci_port *iport,
  2172. struct isci_phy *iphy)
  2173. {
  2174. switch (ihost->sm.current_state_id) {
  2175. case SCIC_STARTING:
  2176. case SCIC_READY:
  2177. ihost->port_agent.link_down_handler(ihost, &ihost->port_agent,
  2178. iport, iphy);
  2179. break;
  2180. default:
  2181. dev_dbg(&ihost->pdev->dev,
  2182. "%s: SCIC Controller linkdown event from phy %d in "
  2183. "unexpected state %d\n",
  2184. __func__,
  2185. iphy->phy_index,
  2186. ihost->sm.current_state_id);
  2187. }
  2188. }
  2189. static bool sci_controller_has_remote_devices_stopping(struct isci_host *ihost)
  2190. {
  2191. u32 index;
  2192. for (index = 0; index < ihost->remote_node_entries; index++) {
  2193. if ((ihost->device_table[index] != NULL) &&
  2194. (ihost->device_table[index]->sm.current_state_id == SCI_DEV_STOPPING))
  2195. return true;
  2196. }
  2197. return false;
  2198. }
  2199. void sci_controller_remote_device_stopped(struct isci_host *ihost,
  2200. struct isci_remote_device *idev)
  2201. {
  2202. if (ihost->sm.current_state_id != SCIC_STOPPING) {
  2203. dev_dbg(&ihost->pdev->dev,
  2204. "SCIC Controller 0x%p remote device stopped event "
  2205. "from device 0x%p in unexpected state %d\n",
  2206. ihost, idev,
  2207. ihost->sm.current_state_id);
  2208. return;
  2209. }
  2210. if (!sci_controller_has_remote_devices_stopping(ihost))
  2211. sci_change_state(&ihost->sm, SCIC_STOPPED);
  2212. }
  2213. void sci_controller_post_request(struct isci_host *ihost, u32 request)
  2214. {
  2215. dev_dbg(&ihost->pdev->dev, "%s[%d]: %#x\n",
  2216. __func__, ihost->id, request);
  2217. writel(request, &ihost->smu_registers->post_context_port);
  2218. }
  2219. struct isci_request *sci_request_by_tag(struct isci_host *ihost, u16 io_tag)
  2220. {
  2221. u16 task_index;
  2222. u16 task_sequence;
  2223. task_index = ISCI_TAG_TCI(io_tag);
  2224. if (task_index < ihost->task_context_entries) {
  2225. struct isci_request *ireq = ihost->reqs[task_index];
  2226. if (test_bit(IREQ_ACTIVE, &ireq->flags)) {
  2227. task_sequence = ISCI_TAG_SEQ(io_tag);
  2228. if (task_sequence == ihost->io_request_sequence[task_index])
  2229. return ireq;
  2230. }
  2231. }
  2232. return NULL;
  2233. }
  2234. /**
  2235. * This method allocates remote node index and the reserves the remote node
  2236. * context space for use. This method can fail if there are no more remote
  2237. * node index available.
  2238. * @scic: This is the controller object which contains the set of
  2239. * free remote node ids
  2240. * @sci_dev: This is the device object which is requesting the a remote node
  2241. * id
  2242. * @node_id: This is the remote node id that is assinged to the device if one
  2243. * is available
  2244. *
  2245. * enum sci_status SCI_FAILURE_OUT_OF_RESOURCES if there are no available remote
  2246. * node index available.
  2247. */
  2248. enum sci_status sci_controller_allocate_remote_node_context(struct isci_host *ihost,
  2249. struct isci_remote_device *idev,
  2250. u16 *node_id)
  2251. {
  2252. u16 node_index;
  2253. u32 remote_node_count = sci_remote_device_node_count(idev);
  2254. node_index = sci_remote_node_table_allocate_remote_node(
  2255. &ihost->available_remote_nodes, remote_node_count
  2256. );
  2257. if (node_index != SCIC_SDS_REMOTE_NODE_CONTEXT_INVALID_INDEX) {
  2258. ihost->device_table[node_index] = idev;
  2259. *node_id = node_index;
  2260. return SCI_SUCCESS;
  2261. }
  2262. return SCI_FAILURE_INSUFFICIENT_RESOURCES;
  2263. }
  2264. void sci_controller_free_remote_node_context(struct isci_host *ihost,
  2265. struct isci_remote_device *idev,
  2266. u16 node_id)
  2267. {
  2268. u32 remote_node_count = sci_remote_device_node_count(idev);
  2269. if (ihost->device_table[node_id] == idev) {
  2270. ihost->device_table[node_id] = NULL;
  2271. sci_remote_node_table_release_remote_node_index(
  2272. &ihost->available_remote_nodes, remote_node_count, node_id
  2273. );
  2274. }
  2275. }
  2276. void sci_controller_copy_sata_response(void *response_buffer,
  2277. void *frame_header,
  2278. void *frame_buffer)
  2279. {
  2280. /* XXX type safety? */
  2281. memcpy(response_buffer, frame_header, sizeof(u32));
  2282. memcpy(response_buffer + sizeof(u32),
  2283. frame_buffer,
  2284. sizeof(struct dev_to_host_fis) - sizeof(u32));
  2285. }
  2286. void sci_controller_release_frame(struct isci_host *ihost, u32 frame_index)
  2287. {
  2288. if (sci_unsolicited_frame_control_release_frame(&ihost->uf_control, frame_index))
  2289. writel(ihost->uf_control.get,
  2290. &ihost->scu_registers->sdma.unsolicited_frame_get_pointer);
  2291. }
  2292. void isci_tci_free(struct isci_host *ihost, u16 tci)
  2293. {
  2294. u16 tail = ihost->tci_tail & (SCI_MAX_IO_REQUESTS-1);
  2295. ihost->tci_pool[tail] = tci;
  2296. ihost->tci_tail = tail + 1;
  2297. }
  2298. static u16 isci_tci_alloc(struct isci_host *ihost)
  2299. {
  2300. u16 head = ihost->tci_head & (SCI_MAX_IO_REQUESTS-1);
  2301. u16 tci = ihost->tci_pool[head];
  2302. ihost->tci_head = head + 1;
  2303. return tci;
  2304. }
  2305. static u16 isci_tci_space(struct isci_host *ihost)
  2306. {
  2307. return CIRC_SPACE(ihost->tci_head, ihost->tci_tail, SCI_MAX_IO_REQUESTS);
  2308. }
  2309. u16 isci_alloc_tag(struct isci_host *ihost)
  2310. {
  2311. if (isci_tci_space(ihost)) {
  2312. u16 tci = isci_tci_alloc(ihost);
  2313. u8 seq = ihost->io_request_sequence[tci];
  2314. return ISCI_TAG(seq, tci);
  2315. }
  2316. return SCI_CONTROLLER_INVALID_IO_TAG;
  2317. }
  2318. enum sci_status isci_free_tag(struct isci_host *ihost, u16 io_tag)
  2319. {
  2320. u16 tci = ISCI_TAG_TCI(io_tag);
  2321. u16 seq = ISCI_TAG_SEQ(io_tag);
  2322. /* prevent tail from passing head */
  2323. if (isci_tci_active(ihost) == 0)
  2324. return SCI_FAILURE_INVALID_IO_TAG;
  2325. if (seq == ihost->io_request_sequence[tci]) {
  2326. ihost->io_request_sequence[tci] = (seq+1) & (SCI_MAX_SEQ-1);
  2327. isci_tci_free(ihost, tci);
  2328. return SCI_SUCCESS;
  2329. }
  2330. return SCI_FAILURE_INVALID_IO_TAG;
  2331. }
  2332. enum sci_status sci_controller_start_io(struct isci_host *ihost,
  2333. struct isci_remote_device *idev,
  2334. struct isci_request *ireq)
  2335. {
  2336. enum sci_status status;
  2337. if (ihost->sm.current_state_id != SCIC_READY) {
  2338. dev_warn(&ihost->pdev->dev, "%s invalid state: %d\n",
  2339. __func__, ihost->sm.current_state_id);
  2340. return SCI_FAILURE_INVALID_STATE;
  2341. }
  2342. status = sci_remote_device_start_io(ihost, idev, ireq);
  2343. if (status != SCI_SUCCESS)
  2344. return status;
  2345. set_bit(IREQ_ACTIVE, &ireq->flags);
  2346. sci_controller_post_request(ihost, ireq->post_context);
  2347. return SCI_SUCCESS;
  2348. }
  2349. enum sci_status sci_controller_terminate_request(struct isci_host *ihost,
  2350. struct isci_remote_device *idev,
  2351. struct isci_request *ireq)
  2352. {
  2353. /* terminate an ongoing (i.e. started) core IO request. This does not
  2354. * abort the IO request at the target, but rather removes the IO
  2355. * request from the host controller.
  2356. */
  2357. enum sci_status status;
  2358. if (ihost->sm.current_state_id != SCIC_READY) {
  2359. dev_warn(&ihost->pdev->dev, "%s invalid state: %d\n",
  2360. __func__, ihost->sm.current_state_id);
  2361. return SCI_FAILURE_INVALID_STATE;
  2362. }
  2363. status = sci_io_request_terminate(ireq);
  2364. if (status != SCI_SUCCESS)
  2365. return status;
  2366. /*
  2367. * Utilize the original post context command and or in the POST_TC_ABORT
  2368. * request sub-type.
  2369. */
  2370. sci_controller_post_request(ihost,
  2371. ireq->post_context | SCU_CONTEXT_COMMAND_REQUEST_POST_TC_ABORT);
  2372. return SCI_SUCCESS;
  2373. }
  2374. /**
  2375. * sci_controller_complete_io() - This method will perform core specific
  2376. * completion operations for an IO request. After this method is invoked,
  2377. * the user should consider the IO request as invalid until it is properly
  2378. * reused (i.e. re-constructed).
  2379. * @ihost: The handle to the controller object for which to complete the
  2380. * IO request.
  2381. * @idev: The handle to the remote device object for which to complete
  2382. * the IO request.
  2383. * @ireq: the handle to the io request object to complete.
  2384. */
  2385. enum sci_status sci_controller_complete_io(struct isci_host *ihost,
  2386. struct isci_remote_device *idev,
  2387. struct isci_request *ireq)
  2388. {
  2389. enum sci_status status;
  2390. u16 index;
  2391. switch (ihost->sm.current_state_id) {
  2392. case SCIC_STOPPING:
  2393. /* XXX: Implement this function */
  2394. return SCI_FAILURE;
  2395. case SCIC_READY:
  2396. status = sci_remote_device_complete_io(ihost, idev, ireq);
  2397. if (status != SCI_SUCCESS)
  2398. return status;
  2399. index = ISCI_TAG_TCI(ireq->io_tag);
  2400. clear_bit(IREQ_ACTIVE, &ireq->flags);
  2401. return SCI_SUCCESS;
  2402. default:
  2403. dev_warn(&ihost->pdev->dev, "%s invalid state: %d\n",
  2404. __func__, ihost->sm.current_state_id);
  2405. return SCI_FAILURE_INVALID_STATE;
  2406. }
  2407. }
  2408. enum sci_status sci_controller_continue_io(struct isci_request *ireq)
  2409. {
  2410. struct isci_host *ihost = ireq->owning_controller;
  2411. if (ihost->sm.current_state_id != SCIC_READY) {
  2412. dev_warn(&ihost->pdev->dev, "%s invalid state: %d\n",
  2413. __func__, ihost->sm.current_state_id);
  2414. return SCI_FAILURE_INVALID_STATE;
  2415. }
  2416. set_bit(IREQ_ACTIVE, &ireq->flags);
  2417. sci_controller_post_request(ihost, ireq->post_context);
  2418. return SCI_SUCCESS;
  2419. }
  2420. /**
  2421. * sci_controller_start_task() - This method is called by the SCIC user to
  2422. * send/start a framework task management request.
  2423. * @controller: the handle to the controller object for which to start the task
  2424. * management request.
  2425. * @remote_device: the handle to the remote device object for which to start
  2426. * the task management request.
  2427. * @task_request: the handle to the task request object to start.
  2428. */
  2429. enum sci_task_status sci_controller_start_task(struct isci_host *ihost,
  2430. struct isci_remote_device *idev,
  2431. struct isci_request *ireq)
  2432. {
  2433. enum sci_status status;
  2434. if (ihost->sm.current_state_id != SCIC_READY) {
  2435. dev_warn(&ihost->pdev->dev,
  2436. "%s: SCIC Controller starting task from invalid "
  2437. "state\n",
  2438. __func__);
  2439. return SCI_TASK_FAILURE_INVALID_STATE;
  2440. }
  2441. status = sci_remote_device_start_task(ihost, idev, ireq);
  2442. switch (status) {
  2443. case SCI_FAILURE_RESET_DEVICE_PARTIAL_SUCCESS:
  2444. set_bit(IREQ_ACTIVE, &ireq->flags);
  2445. /*
  2446. * We will let framework know this task request started successfully,
  2447. * although core is still woring on starting the request (to post tc when
  2448. * RNC is resumed.)
  2449. */
  2450. return SCI_SUCCESS;
  2451. case SCI_SUCCESS:
  2452. set_bit(IREQ_ACTIVE, &ireq->flags);
  2453. sci_controller_post_request(ihost, ireq->post_context);
  2454. break;
  2455. default:
  2456. break;
  2457. }
  2458. return status;
  2459. }
  2460. static int sci_write_gpio_tx_gp(struct isci_host *ihost, u8 reg_index, u8 reg_count, u8 *write_data)
  2461. {
  2462. int d;
  2463. /* no support for TX_GP_CFG */
  2464. if (reg_index == 0)
  2465. return -EINVAL;
  2466. for (d = 0; d < isci_gpio_count(ihost); d++) {
  2467. u32 val = 0x444; /* all ODx.n clear */
  2468. int i;
  2469. for (i = 0; i < 3; i++) {
  2470. int bit = (i << 2) + 2;
  2471. bit = try_test_sas_gpio_gp_bit(to_sas_gpio_od(d, i),
  2472. write_data, reg_index,
  2473. reg_count);
  2474. if (bit < 0)
  2475. break;
  2476. /* if od is set, clear the 'invert' bit */
  2477. val &= ~(bit << ((i << 2) + 2));
  2478. }
  2479. if (i < 3)
  2480. break;
  2481. writel(val, &ihost->scu_registers->peg0.sgpio.output_data_select[d]);
  2482. }
  2483. /* unless reg_index is > 1, we should always be able to write at
  2484. * least one register
  2485. */
  2486. return d > 0;
  2487. }
  2488. int isci_gpio_write(struct sas_ha_struct *sas_ha, u8 reg_type, u8 reg_index,
  2489. u8 reg_count, u8 *write_data)
  2490. {
  2491. struct isci_host *ihost = sas_ha->lldd_ha;
  2492. int written;
  2493. switch (reg_type) {
  2494. case SAS_GPIO_REG_TX_GP:
  2495. written = sci_write_gpio_tx_gp(ihost, reg_index, reg_count, write_data);
  2496. break;
  2497. default:
  2498. written = -EINVAL;
  2499. }
  2500. return written;
  2501. }