host.c 100 KB

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