host.c 88 KB

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