host.c 101 KB

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