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