host.c 82 KB

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