host.c 99 KB

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