host.c 97 KB

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