host.c 89 KB

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