host.c 101 KB

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