host.c 82 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154215521562157215821592160216121622163216421652166216721682169217021712172217321742175217621772178217921802181218221832184218521862187218821892190219121922193219421952196219721982199220022012202220322042205220622072208220922102211221222132214221522162217221822192220222122222223222422252226222722282229223022312232223322342235223622372238223922402241224222432244224522462247224822492250225122522253225422552256225722582259226022612262226322642265226622672268226922702271227222732274227522762277227822792280228122822283228422852286228722882289229022912292229322942295229622972298229923002301230223032304230523062307230823092310231123122313231423152316231723182319232023212322232323242325232623272328232923302331233223332334233523362337233823392340234123422343234423452346234723482349235023512352235323542355235623572358235923602361236223632364236523662367236823692370237123722373237423752376237723782379238023812382238323842385238623872388238923902391239223932394239523962397239823992400240124022403240424052406240724082409241024112412241324142415241624172418241924202421242224232424242524262427242824292430243124322433243424352436243724382439244024412442244324442445244624472448244924502451245224532454245524562457245824592460246124622463246424652466246724682469247024712472247324742475247624772478247924802481248224832484248524862487248824892490249124922493249424952496249724982499250025012502250325042505250625072508250925102511251225132514251525162517251825192520252125222523252425252526252725282529253025312532253325342535253625372538253925402541254225432544254525462547254825492550255125522553255425552556255725582559256025612562256325642565256625672568256925702571257225732574257525762577257825792580258125822583258425852586258725882589259025912592259325942595259625972598259926002601260226032604260526062607260826092610261126122613261426152616261726182619262026212622262326242625262626272628262926302631263226332634263526362637263826392640264126422643264426452646264726482649265026512652265326542655265626572658265926602661266226632664266526662667266826692670267126722673267426752676267726782679268026812682268326842685268626872688268926902691269226932694269526962697269826992700270127022703270427052706270727082709271027112712271327142715271627172718271927202721272227232724272527262727272827292730273127322733273427352736273727382739274027412742274327442745274627472748274927502751275227532754275527562757275827592760276127622763276427652766276727682769277027712772277327742775277627772778277927802781278227832784278527862787278827892790279127922793279427952796279727982799280028012802280328042805280628072808280928102811281228132814281528162817281828192820282128222823282428252826282728282829283028312832283328342835
  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, enum sci_status completion_status)
  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_STOPPED:
  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. void isci_host_deinit(struct isci_host *ihost)
  1051. {
  1052. int i;
  1053. /* disable output data selects */
  1054. for (i = 0; i < isci_gpio_count(ihost); i++)
  1055. writel(SGPIO_HW_CONTROL, &ihost->scu_registers->peg0.sgpio.output_data_select[i]);
  1056. for (i = 0; i < SCI_MAX_PORTS; i++) {
  1057. struct isci_port *iport = &ihost->ports[i];
  1058. struct isci_remote_device *idev, *d;
  1059. list_for_each_entry_safe(idev, d, &iport->remote_dev_list, node) {
  1060. if (test_bit(IDEV_ALLOCATED, &idev->flags))
  1061. isci_remote_device_stop(ihost, idev);
  1062. }
  1063. }
  1064. set_bit(IHOST_STOP_PENDING, &ihost->flags);
  1065. spin_lock_irq(&ihost->scic_lock);
  1066. sci_controller_stop(ihost, SCIC_CONTROLLER_STOP_TIMEOUT);
  1067. spin_unlock_irq(&ihost->scic_lock);
  1068. wait_for_stop(ihost);
  1069. /* disable sgpio: where the above wait should give time for the
  1070. * enclosure to sample the gpios going inactive
  1071. */
  1072. writel(0, &ihost->scu_registers->peg0.sgpio.interface_control);
  1073. sci_controller_reset(ihost);
  1074. /* Cancel any/all outstanding port timers */
  1075. for (i = 0; i < ihost->logical_port_entries; i++) {
  1076. struct isci_port *iport = &ihost->ports[i];
  1077. del_timer_sync(&iport->timer.timer);
  1078. }
  1079. /* Cancel any/all outstanding phy timers */
  1080. for (i = 0; i < SCI_MAX_PHYS; i++) {
  1081. struct isci_phy *iphy = &ihost->phys[i];
  1082. del_timer_sync(&iphy->sata_timer.timer);
  1083. }
  1084. del_timer_sync(&ihost->port_agent.timer.timer);
  1085. del_timer_sync(&ihost->power_control.timer.timer);
  1086. del_timer_sync(&ihost->timer.timer);
  1087. del_timer_sync(&ihost->phy_timer.timer);
  1088. }
  1089. static void __iomem *scu_base(struct isci_host *isci_host)
  1090. {
  1091. struct pci_dev *pdev = isci_host->pdev;
  1092. int id = isci_host->id;
  1093. return pcim_iomap_table(pdev)[SCI_SCU_BAR * 2] + SCI_SCU_BAR_SIZE * id;
  1094. }
  1095. static void __iomem *smu_base(struct isci_host *isci_host)
  1096. {
  1097. struct pci_dev *pdev = isci_host->pdev;
  1098. int id = isci_host->id;
  1099. return pcim_iomap_table(pdev)[SCI_SMU_BAR * 2] + SCI_SMU_BAR_SIZE * id;
  1100. }
  1101. static void sci_controller_initial_state_enter(struct sci_base_state_machine *sm)
  1102. {
  1103. struct isci_host *ihost = container_of(sm, typeof(*ihost), sm);
  1104. sci_change_state(&ihost->sm, SCIC_RESET);
  1105. }
  1106. static inline void sci_controller_starting_state_exit(struct sci_base_state_machine *sm)
  1107. {
  1108. struct isci_host *ihost = container_of(sm, typeof(*ihost), sm);
  1109. sci_del_timer(&ihost->timer);
  1110. }
  1111. #define INTERRUPT_COALESCE_TIMEOUT_BASE_RANGE_LOWER_BOUND_NS 853
  1112. #define INTERRUPT_COALESCE_TIMEOUT_BASE_RANGE_UPPER_BOUND_NS 1280
  1113. #define INTERRUPT_COALESCE_TIMEOUT_MAX_US 2700000
  1114. #define INTERRUPT_COALESCE_NUMBER_MAX 256
  1115. #define INTERRUPT_COALESCE_TIMEOUT_ENCODE_MIN 7
  1116. #define INTERRUPT_COALESCE_TIMEOUT_ENCODE_MAX 28
  1117. /**
  1118. * sci_controller_set_interrupt_coalescence() - This method allows the user to
  1119. * configure the interrupt coalescence.
  1120. * @controller: This parameter represents the handle to the controller object
  1121. * for which its interrupt coalesce register is overridden.
  1122. * @coalesce_number: Used to control the number of entries in the Completion
  1123. * Queue before an interrupt is generated. If the number of entries exceed
  1124. * this number, an interrupt will be generated. The valid range of the input
  1125. * is [0, 256]. A setting of 0 results in coalescing being disabled.
  1126. * @coalesce_timeout: Timeout value in microseconds. The valid range of the
  1127. * input is [0, 2700000] . A setting of 0 is allowed and results in no
  1128. * interrupt coalescing timeout.
  1129. *
  1130. * Indicate if the user successfully set the interrupt coalesce parameters.
  1131. * SCI_SUCCESS The user successfully updated the interrutp coalescence.
  1132. * SCI_FAILURE_INVALID_PARAMETER_VALUE The user input value is out of range.
  1133. */
  1134. static enum sci_status
  1135. sci_controller_set_interrupt_coalescence(struct isci_host *ihost,
  1136. u32 coalesce_number,
  1137. u32 coalesce_timeout)
  1138. {
  1139. u8 timeout_encode = 0;
  1140. u32 min = 0;
  1141. u32 max = 0;
  1142. /* Check if the input parameters fall in the range. */
  1143. if (coalesce_number > INTERRUPT_COALESCE_NUMBER_MAX)
  1144. return SCI_FAILURE_INVALID_PARAMETER_VALUE;
  1145. /*
  1146. * Defined encoding for interrupt coalescing timeout:
  1147. * Value Min Max Units
  1148. * ----- --- --- -----
  1149. * 0 - - Disabled
  1150. * 1 13.3 20.0 ns
  1151. * 2 26.7 40.0
  1152. * 3 53.3 80.0
  1153. * 4 106.7 160.0
  1154. * 5 213.3 320.0
  1155. * 6 426.7 640.0
  1156. * 7 853.3 1280.0
  1157. * 8 1.7 2.6 us
  1158. * 9 3.4 5.1
  1159. * 10 6.8 10.2
  1160. * 11 13.7 20.5
  1161. * 12 27.3 41.0
  1162. * 13 54.6 81.9
  1163. * 14 109.2 163.8
  1164. * 15 218.5 327.7
  1165. * 16 436.9 655.4
  1166. * 17 873.8 1310.7
  1167. * 18 1.7 2.6 ms
  1168. * 19 3.5 5.2
  1169. * 20 7.0 10.5
  1170. * 21 14.0 21.0
  1171. * 22 28.0 41.9
  1172. * 23 55.9 83.9
  1173. * 24 111.8 167.8
  1174. * 25 223.7 335.5
  1175. * 26 447.4 671.1
  1176. * 27 894.8 1342.2
  1177. * 28 1.8 2.7 s
  1178. * Others Undefined */
  1179. /*
  1180. * Use the table above to decide the encode of interrupt coalescing timeout
  1181. * value for register writing. */
  1182. if (coalesce_timeout == 0)
  1183. timeout_encode = 0;
  1184. else{
  1185. /* make the timeout value in unit of (10 ns). */
  1186. coalesce_timeout = coalesce_timeout * 100;
  1187. min = INTERRUPT_COALESCE_TIMEOUT_BASE_RANGE_LOWER_BOUND_NS / 10;
  1188. max = INTERRUPT_COALESCE_TIMEOUT_BASE_RANGE_UPPER_BOUND_NS / 10;
  1189. /* get the encode of timeout for register writing. */
  1190. for (timeout_encode = INTERRUPT_COALESCE_TIMEOUT_ENCODE_MIN;
  1191. timeout_encode <= INTERRUPT_COALESCE_TIMEOUT_ENCODE_MAX;
  1192. timeout_encode++) {
  1193. if (min <= coalesce_timeout && max > coalesce_timeout)
  1194. break;
  1195. else if (coalesce_timeout >= max && coalesce_timeout < min * 2
  1196. && coalesce_timeout <= INTERRUPT_COALESCE_TIMEOUT_MAX_US * 100) {
  1197. if ((coalesce_timeout - max) < (2 * min - coalesce_timeout))
  1198. break;
  1199. else{
  1200. timeout_encode++;
  1201. break;
  1202. }
  1203. } else {
  1204. max = max * 2;
  1205. min = min * 2;
  1206. }
  1207. }
  1208. if (timeout_encode == INTERRUPT_COALESCE_TIMEOUT_ENCODE_MAX + 1)
  1209. /* the value is out of range. */
  1210. return SCI_FAILURE_INVALID_PARAMETER_VALUE;
  1211. }
  1212. writel(SMU_ICC_GEN_VAL(NUMBER, coalesce_number) |
  1213. SMU_ICC_GEN_VAL(TIMER, timeout_encode),
  1214. &ihost->smu_registers->interrupt_coalesce_control);
  1215. ihost->interrupt_coalesce_number = (u16)coalesce_number;
  1216. ihost->interrupt_coalesce_timeout = coalesce_timeout / 100;
  1217. return SCI_SUCCESS;
  1218. }
  1219. static void sci_controller_ready_state_enter(struct sci_base_state_machine *sm)
  1220. {
  1221. struct isci_host *ihost = container_of(sm, typeof(*ihost), sm);
  1222. u32 val;
  1223. /* enable clock gating for power control of the scu unit */
  1224. val = readl(&ihost->smu_registers->clock_gating_control);
  1225. val &= ~(SMU_CGUCR_GEN_BIT(REGCLK_ENABLE) |
  1226. SMU_CGUCR_GEN_BIT(TXCLK_ENABLE) |
  1227. SMU_CGUCR_GEN_BIT(XCLK_ENABLE));
  1228. val |= SMU_CGUCR_GEN_BIT(IDLE_ENABLE);
  1229. writel(val, &ihost->smu_registers->clock_gating_control);
  1230. /* set the default interrupt coalescence number and timeout value. */
  1231. sci_controller_set_interrupt_coalescence(ihost, 0, 0);
  1232. }
  1233. static void sci_controller_ready_state_exit(struct sci_base_state_machine *sm)
  1234. {
  1235. struct isci_host *ihost = container_of(sm, typeof(*ihost), sm);
  1236. /* disable interrupt coalescence. */
  1237. sci_controller_set_interrupt_coalescence(ihost, 0, 0);
  1238. }
  1239. static enum sci_status sci_controller_stop_phys(struct isci_host *ihost)
  1240. {
  1241. u32 index;
  1242. enum sci_status status;
  1243. enum sci_status phy_status;
  1244. status = SCI_SUCCESS;
  1245. for (index = 0; index < SCI_MAX_PHYS; index++) {
  1246. phy_status = sci_phy_stop(&ihost->phys[index]);
  1247. if (phy_status != SCI_SUCCESS &&
  1248. phy_status != SCI_FAILURE_INVALID_STATE) {
  1249. status = SCI_FAILURE;
  1250. dev_warn(&ihost->pdev->dev,
  1251. "%s: Controller stop operation failed to stop "
  1252. "phy %d because of status %d.\n",
  1253. __func__,
  1254. ihost->phys[index].phy_index, phy_status);
  1255. }
  1256. }
  1257. return status;
  1258. }
  1259. static enum sci_status sci_controller_stop_ports(struct isci_host *ihost)
  1260. {
  1261. u32 index;
  1262. enum sci_status port_status;
  1263. enum sci_status status = SCI_SUCCESS;
  1264. for (index = 0; index < ihost->logical_port_entries; index++) {
  1265. struct isci_port *iport = &ihost->ports[index];
  1266. port_status = sci_port_stop(iport);
  1267. if ((port_status != SCI_SUCCESS) &&
  1268. (port_status != SCI_FAILURE_INVALID_STATE)) {
  1269. status = SCI_FAILURE;
  1270. dev_warn(&ihost->pdev->dev,
  1271. "%s: Controller stop operation failed to "
  1272. "stop port %d because of status %d.\n",
  1273. __func__,
  1274. iport->logical_port_index,
  1275. port_status);
  1276. }
  1277. }
  1278. return status;
  1279. }
  1280. static enum sci_status sci_controller_stop_devices(struct isci_host *ihost)
  1281. {
  1282. u32 index;
  1283. enum sci_status status;
  1284. enum sci_status device_status;
  1285. status = SCI_SUCCESS;
  1286. for (index = 0; index < ihost->remote_node_entries; index++) {
  1287. if (ihost->device_table[index] != NULL) {
  1288. /* / @todo What timeout value do we want to provide to this request? */
  1289. device_status = sci_remote_device_stop(ihost->device_table[index], 0);
  1290. if ((device_status != SCI_SUCCESS) &&
  1291. (device_status != SCI_FAILURE_INVALID_STATE)) {
  1292. dev_warn(&ihost->pdev->dev,
  1293. "%s: Controller stop operation failed "
  1294. "to stop device 0x%p because of "
  1295. "status %d.\n",
  1296. __func__,
  1297. ihost->device_table[index], device_status);
  1298. }
  1299. }
  1300. }
  1301. return status;
  1302. }
  1303. static void sci_controller_stopping_state_enter(struct sci_base_state_machine *sm)
  1304. {
  1305. struct isci_host *ihost = container_of(sm, typeof(*ihost), sm);
  1306. /* Stop all of the components for this controller */
  1307. sci_controller_stop_phys(ihost);
  1308. sci_controller_stop_ports(ihost);
  1309. sci_controller_stop_devices(ihost);
  1310. }
  1311. static void sci_controller_stopping_state_exit(struct sci_base_state_machine *sm)
  1312. {
  1313. struct isci_host *ihost = container_of(sm, typeof(*ihost), sm);
  1314. sci_del_timer(&ihost->timer);
  1315. }
  1316. static void sci_controller_reset_hardware(struct isci_host *ihost)
  1317. {
  1318. /* Disable interrupts so we dont take any spurious interrupts */
  1319. sci_controller_disable_interrupts(ihost);
  1320. /* Reset the SCU */
  1321. writel(0xFFFFFFFF, &ihost->smu_registers->soft_reset_control);
  1322. /* Delay for 1ms to before clearing the CQP and UFQPR. */
  1323. udelay(1000);
  1324. /* The write to the CQGR clears the CQP */
  1325. writel(0x00000000, &ihost->smu_registers->completion_queue_get);
  1326. /* The write to the UFQGP clears the UFQPR */
  1327. writel(0, &ihost->scu_registers->sdma.unsolicited_frame_get_pointer);
  1328. }
  1329. static void sci_controller_resetting_state_enter(struct sci_base_state_machine *sm)
  1330. {
  1331. struct isci_host *ihost = container_of(sm, typeof(*ihost), sm);
  1332. sci_controller_reset_hardware(ihost);
  1333. sci_change_state(&ihost->sm, SCIC_RESET);
  1334. }
  1335. static const struct sci_base_state sci_controller_state_table[] = {
  1336. [SCIC_INITIAL] = {
  1337. .enter_state = sci_controller_initial_state_enter,
  1338. },
  1339. [SCIC_RESET] = {},
  1340. [SCIC_INITIALIZING] = {},
  1341. [SCIC_INITIALIZED] = {},
  1342. [SCIC_STARTING] = {
  1343. .exit_state = sci_controller_starting_state_exit,
  1344. },
  1345. [SCIC_READY] = {
  1346. .enter_state = sci_controller_ready_state_enter,
  1347. .exit_state = sci_controller_ready_state_exit,
  1348. },
  1349. [SCIC_RESETTING] = {
  1350. .enter_state = sci_controller_resetting_state_enter,
  1351. },
  1352. [SCIC_STOPPING] = {
  1353. .enter_state = sci_controller_stopping_state_enter,
  1354. .exit_state = sci_controller_stopping_state_exit,
  1355. },
  1356. [SCIC_STOPPED] = {},
  1357. [SCIC_FAILED] = {}
  1358. };
  1359. static void controller_timeout(unsigned long data)
  1360. {
  1361. struct sci_timer *tmr = (struct sci_timer *)data;
  1362. struct isci_host *ihost = container_of(tmr, typeof(*ihost), timer);
  1363. struct sci_base_state_machine *sm = &ihost->sm;
  1364. unsigned long flags;
  1365. spin_lock_irqsave(&ihost->scic_lock, flags);
  1366. if (tmr->cancel)
  1367. goto done;
  1368. if (sm->current_state_id == SCIC_STARTING)
  1369. sci_controller_transition_to_ready(ihost, SCI_FAILURE_TIMEOUT);
  1370. else if (sm->current_state_id == SCIC_STOPPING) {
  1371. sci_change_state(sm, SCIC_FAILED);
  1372. isci_host_stop_complete(ihost, SCI_FAILURE_TIMEOUT);
  1373. } else /* / @todo Now what do we want to do in this case? */
  1374. dev_err(&ihost->pdev->dev,
  1375. "%s: Controller timer fired when controller was not "
  1376. "in a state being timed.\n",
  1377. __func__);
  1378. done:
  1379. spin_unlock_irqrestore(&ihost->scic_lock, flags);
  1380. }
  1381. static enum sci_status sci_controller_construct(struct isci_host *ihost,
  1382. void __iomem *scu_base,
  1383. void __iomem *smu_base)
  1384. {
  1385. u8 i;
  1386. sci_init_sm(&ihost->sm, sci_controller_state_table, SCIC_INITIAL);
  1387. ihost->scu_registers = scu_base;
  1388. ihost->smu_registers = smu_base;
  1389. sci_port_configuration_agent_construct(&ihost->port_agent);
  1390. /* Construct the ports for this controller */
  1391. for (i = 0; i < SCI_MAX_PORTS; i++)
  1392. sci_port_construct(&ihost->ports[i], i, ihost);
  1393. sci_port_construct(&ihost->ports[i], SCIC_SDS_DUMMY_PORT, ihost);
  1394. /* Construct the phys for this controller */
  1395. for (i = 0; i < SCI_MAX_PHYS; i++) {
  1396. /* Add all the PHYs to the dummy port */
  1397. sci_phy_construct(&ihost->phys[i],
  1398. &ihost->ports[SCI_MAX_PORTS], i);
  1399. }
  1400. ihost->invalid_phy_mask = 0;
  1401. sci_init_timer(&ihost->timer, controller_timeout);
  1402. return sci_controller_reset(ihost);
  1403. }
  1404. int sci_oem_parameters_validate(struct sci_oem_params *oem, u8 version)
  1405. {
  1406. int i;
  1407. for (i = 0; i < SCI_MAX_PORTS; i++)
  1408. if (oem->ports[i].phy_mask > SCIC_SDS_PARM_PHY_MASK_MAX)
  1409. return -EINVAL;
  1410. for (i = 0; i < SCI_MAX_PHYS; i++)
  1411. if (oem->phys[i].sas_address.high == 0 &&
  1412. oem->phys[i].sas_address.low == 0)
  1413. return -EINVAL;
  1414. if (oem->controller.mode_type == SCIC_PORT_AUTOMATIC_CONFIGURATION_MODE) {
  1415. for (i = 0; i < SCI_MAX_PHYS; i++)
  1416. if (oem->ports[i].phy_mask != 0)
  1417. return -EINVAL;
  1418. } else if (oem->controller.mode_type == SCIC_PORT_MANUAL_CONFIGURATION_MODE) {
  1419. u8 phy_mask = 0;
  1420. for (i = 0; i < SCI_MAX_PHYS; i++)
  1421. phy_mask |= oem->ports[i].phy_mask;
  1422. if (phy_mask == 0)
  1423. return -EINVAL;
  1424. } else
  1425. return -EINVAL;
  1426. if (oem->controller.max_concurr_spin_up > MAX_CONCURRENT_DEVICE_SPIN_UP_COUNT ||
  1427. oem->controller.max_concurr_spin_up < 1)
  1428. return -EINVAL;
  1429. if (oem->controller.do_enable_ssc) {
  1430. if (version < ISCI_ROM_VER_1_1 && oem->controller.do_enable_ssc != 1)
  1431. return -EINVAL;
  1432. if (version >= ISCI_ROM_VER_1_1) {
  1433. u8 test = oem->controller.ssc_sata_tx_spread_level;
  1434. switch (test) {
  1435. case 0:
  1436. case 2:
  1437. case 3:
  1438. case 6:
  1439. case 7:
  1440. break;
  1441. default:
  1442. return -EINVAL;
  1443. }
  1444. test = oem->controller.ssc_sas_tx_spread_level;
  1445. if (oem->controller.ssc_sas_tx_type == 0) {
  1446. switch (test) {
  1447. case 0:
  1448. case 2:
  1449. case 3:
  1450. break;
  1451. default:
  1452. return -EINVAL;
  1453. }
  1454. } else if (oem->controller.ssc_sas_tx_type == 1) {
  1455. switch (test) {
  1456. case 0:
  1457. case 3:
  1458. case 6:
  1459. break;
  1460. default:
  1461. return -EINVAL;
  1462. }
  1463. }
  1464. }
  1465. }
  1466. return 0;
  1467. }
  1468. static u8 max_spin_up(struct isci_host *ihost)
  1469. {
  1470. if (ihost->user_parameters.max_concurr_spinup)
  1471. return min_t(u8, ihost->user_parameters.max_concurr_spinup,
  1472. MAX_CONCURRENT_DEVICE_SPIN_UP_COUNT);
  1473. else
  1474. return min_t(u8, ihost->oem_parameters.controller.max_concurr_spin_up,
  1475. MAX_CONCURRENT_DEVICE_SPIN_UP_COUNT);
  1476. }
  1477. static void power_control_timeout(unsigned long data)
  1478. {
  1479. struct sci_timer *tmr = (struct sci_timer *)data;
  1480. struct isci_host *ihost = container_of(tmr, typeof(*ihost), power_control.timer);
  1481. struct isci_phy *iphy;
  1482. unsigned long flags;
  1483. u8 i;
  1484. spin_lock_irqsave(&ihost->scic_lock, flags);
  1485. if (tmr->cancel)
  1486. goto done;
  1487. ihost->power_control.phys_granted_power = 0;
  1488. if (ihost->power_control.phys_waiting == 0) {
  1489. ihost->power_control.timer_started = false;
  1490. goto done;
  1491. }
  1492. for (i = 0; i < SCI_MAX_PHYS; i++) {
  1493. if (ihost->power_control.phys_waiting == 0)
  1494. break;
  1495. iphy = ihost->power_control.requesters[i];
  1496. if (iphy == NULL)
  1497. continue;
  1498. if (ihost->power_control.phys_granted_power >= max_spin_up(ihost))
  1499. break;
  1500. ihost->power_control.requesters[i] = NULL;
  1501. ihost->power_control.phys_waiting--;
  1502. ihost->power_control.phys_granted_power++;
  1503. sci_phy_consume_power_handler(iphy);
  1504. if (iphy->protocol == SAS_PROTOCOL_SSP) {
  1505. u8 j;
  1506. for (j = 0; j < SCI_MAX_PHYS; j++) {
  1507. struct isci_phy *requester = ihost->power_control.requesters[j];
  1508. /*
  1509. * Search the power_control queue to see if there are other phys
  1510. * attached to the same remote device. If found, take all of
  1511. * them out of await_sas_power state.
  1512. */
  1513. if (requester != NULL && requester != iphy) {
  1514. u8 other = memcmp(requester->frame_rcvd.iaf.sas_addr,
  1515. iphy->frame_rcvd.iaf.sas_addr,
  1516. sizeof(requester->frame_rcvd.iaf.sas_addr));
  1517. if (other == 0) {
  1518. ihost->power_control.requesters[j] = NULL;
  1519. ihost->power_control.phys_waiting--;
  1520. sci_phy_consume_power_handler(requester);
  1521. }
  1522. }
  1523. }
  1524. }
  1525. }
  1526. /*
  1527. * It doesn't matter if the power list is empty, we need to start the
  1528. * timer in case another phy becomes ready.
  1529. */
  1530. sci_mod_timer(tmr, SCIC_SDS_CONTROLLER_POWER_CONTROL_INTERVAL);
  1531. ihost->power_control.timer_started = true;
  1532. done:
  1533. spin_unlock_irqrestore(&ihost->scic_lock, flags);
  1534. }
  1535. void sci_controller_power_control_queue_insert(struct isci_host *ihost,
  1536. struct isci_phy *iphy)
  1537. {
  1538. BUG_ON(iphy == NULL);
  1539. if (ihost->power_control.phys_granted_power < max_spin_up(ihost)) {
  1540. ihost->power_control.phys_granted_power++;
  1541. sci_phy_consume_power_handler(iphy);
  1542. /*
  1543. * stop and start the power_control timer. When the timer fires, the
  1544. * no_of_phys_granted_power will be set to 0
  1545. */
  1546. if (ihost->power_control.timer_started)
  1547. sci_del_timer(&ihost->power_control.timer);
  1548. sci_mod_timer(&ihost->power_control.timer,
  1549. SCIC_SDS_CONTROLLER_POWER_CONTROL_INTERVAL);
  1550. ihost->power_control.timer_started = true;
  1551. } else {
  1552. /*
  1553. * There are phys, attached to the same sas address as this phy, are
  1554. * already in READY state, this phy don't need wait.
  1555. */
  1556. u8 i;
  1557. struct isci_phy *current_phy;
  1558. for (i = 0; i < SCI_MAX_PHYS; i++) {
  1559. u8 other;
  1560. current_phy = &ihost->phys[i];
  1561. other = memcmp(current_phy->frame_rcvd.iaf.sas_addr,
  1562. iphy->frame_rcvd.iaf.sas_addr,
  1563. sizeof(current_phy->frame_rcvd.iaf.sas_addr));
  1564. if (current_phy->sm.current_state_id == SCI_PHY_READY &&
  1565. current_phy->protocol == SAS_PROTOCOL_SSP &&
  1566. other == 0) {
  1567. sci_phy_consume_power_handler(iphy);
  1568. break;
  1569. }
  1570. }
  1571. if (i == SCI_MAX_PHYS) {
  1572. /* Add the phy in the waiting list */
  1573. ihost->power_control.requesters[iphy->phy_index] = iphy;
  1574. ihost->power_control.phys_waiting++;
  1575. }
  1576. }
  1577. }
  1578. void sci_controller_power_control_queue_remove(struct isci_host *ihost,
  1579. struct isci_phy *iphy)
  1580. {
  1581. BUG_ON(iphy == NULL);
  1582. if (ihost->power_control.requesters[iphy->phy_index])
  1583. ihost->power_control.phys_waiting--;
  1584. ihost->power_control.requesters[iphy->phy_index] = NULL;
  1585. }
  1586. static int is_long_cable(int phy, unsigned char selection_byte)
  1587. {
  1588. return !!(selection_byte & (1 << phy));
  1589. }
  1590. static int is_medium_cable(int phy, unsigned char selection_byte)
  1591. {
  1592. return !!(selection_byte & (1 << (phy + 4)));
  1593. }
  1594. static enum cable_selections decode_selection_byte(
  1595. int phy,
  1596. unsigned char selection_byte)
  1597. {
  1598. return ((selection_byte & (1 << phy)) ? 1 : 0)
  1599. + (selection_byte & (1 << (phy + 4)) ? 2 : 0);
  1600. }
  1601. static unsigned char *to_cable_select(struct isci_host *ihost)
  1602. {
  1603. if (is_cable_select_overridden())
  1604. return ((unsigned char *)&cable_selection_override)
  1605. + ihost->id;
  1606. else
  1607. return &ihost->oem_parameters.controller.cable_selection_mask;
  1608. }
  1609. enum cable_selections decode_cable_selection(struct isci_host *ihost, int phy)
  1610. {
  1611. return decode_selection_byte(phy, *to_cable_select(ihost));
  1612. }
  1613. char *lookup_cable_names(enum cable_selections selection)
  1614. {
  1615. static char *cable_names[] = {
  1616. [short_cable] = "short",
  1617. [long_cable] = "long",
  1618. [medium_cable] = "medium",
  1619. [undefined_cable] = "<undefined, assumed long>" /* bit 0==1 */
  1620. };
  1621. return (selection <= undefined_cable) ? cable_names[selection]
  1622. : cable_names[undefined_cable];
  1623. }
  1624. #define AFE_REGISTER_WRITE_DELAY 10
  1625. static void sci_controller_afe_initialization(struct isci_host *ihost)
  1626. {
  1627. struct scu_afe_registers __iomem *afe = &ihost->scu_registers->afe;
  1628. const struct sci_oem_params *oem = &ihost->oem_parameters;
  1629. struct pci_dev *pdev = ihost->pdev;
  1630. u32 afe_status;
  1631. u32 phy_id;
  1632. unsigned char cable_selection_mask = *to_cable_select(ihost);
  1633. /* Clear DFX Status registers */
  1634. writel(0x0081000f, &afe->afe_dfx_master_control0);
  1635. udelay(AFE_REGISTER_WRITE_DELAY);
  1636. if (is_b0(pdev) || is_c0(pdev) || is_c1(pdev)) {
  1637. /* PM Rx Equalization Save, PM SPhy Rx Acknowledgement
  1638. * Timer, PM Stagger Timer
  1639. */
  1640. writel(0x0007FFFF, &afe->afe_pmsn_master_control2);
  1641. udelay(AFE_REGISTER_WRITE_DELAY);
  1642. }
  1643. /* Configure bias currents to normal */
  1644. if (is_a2(pdev))
  1645. writel(0x00005A00, &afe->afe_bias_control);
  1646. else if (is_b0(pdev) || is_c0(pdev))
  1647. writel(0x00005F00, &afe->afe_bias_control);
  1648. else if (is_c1(pdev))
  1649. writel(0x00005500, &afe->afe_bias_control);
  1650. udelay(AFE_REGISTER_WRITE_DELAY);
  1651. /* Enable PLL */
  1652. if (is_a2(pdev))
  1653. writel(0x80040908, &afe->afe_pll_control0);
  1654. else if (is_b0(pdev) || is_c0(pdev))
  1655. writel(0x80040A08, &afe->afe_pll_control0);
  1656. else if (is_c1(pdev)) {
  1657. writel(0x80000B08, &afe->afe_pll_control0);
  1658. udelay(AFE_REGISTER_WRITE_DELAY);
  1659. writel(0x00000B08, &afe->afe_pll_control0);
  1660. udelay(AFE_REGISTER_WRITE_DELAY);
  1661. writel(0x80000B08, &afe->afe_pll_control0);
  1662. }
  1663. udelay(AFE_REGISTER_WRITE_DELAY);
  1664. /* Wait for the PLL to lock */
  1665. do {
  1666. afe_status = readl(&afe->afe_common_block_status);
  1667. udelay(AFE_REGISTER_WRITE_DELAY);
  1668. } while ((afe_status & 0x00001000) == 0);
  1669. if (is_a2(pdev)) {
  1670. /* Shorten SAS SNW lock time (RxLock timer value from 76
  1671. * us to 50 us)
  1672. */
  1673. writel(0x7bcc96ad, &afe->afe_pmsn_master_control0);
  1674. udelay(AFE_REGISTER_WRITE_DELAY);
  1675. }
  1676. for (phy_id = 0; phy_id < SCI_MAX_PHYS; phy_id++) {
  1677. struct scu_afe_transceiver *xcvr = &afe->scu_afe_xcvr[phy_id];
  1678. const struct sci_phy_oem_params *oem_phy = &oem->phys[phy_id];
  1679. int cable_length_long =
  1680. is_long_cable(phy_id, cable_selection_mask);
  1681. int cable_length_medium =
  1682. is_medium_cable(phy_id, cable_selection_mask);
  1683. if (is_a2(pdev)) {
  1684. /* All defaults, except the Receive Word
  1685. * Alignament/Comma Detect Enable....(0xe800)
  1686. */
  1687. writel(0x00004512, &xcvr->afe_xcvr_control0);
  1688. udelay(AFE_REGISTER_WRITE_DELAY);
  1689. writel(0x0050100F, &xcvr->afe_xcvr_control1);
  1690. udelay(AFE_REGISTER_WRITE_DELAY);
  1691. } else if (is_b0(pdev)) {
  1692. /* Configure transmitter SSC parameters */
  1693. writel(0x00030000, &xcvr->afe_tx_ssc_control);
  1694. udelay(AFE_REGISTER_WRITE_DELAY);
  1695. } else if (is_c0(pdev)) {
  1696. /* Configure transmitter SSC parameters */
  1697. writel(0x00010202, &xcvr->afe_tx_ssc_control);
  1698. udelay(AFE_REGISTER_WRITE_DELAY);
  1699. /* All defaults, except the Receive Word
  1700. * Alignament/Comma Detect Enable....(0xe800)
  1701. */
  1702. writel(0x00014500, &xcvr->afe_xcvr_control0);
  1703. udelay(AFE_REGISTER_WRITE_DELAY);
  1704. } else if (is_c1(pdev)) {
  1705. /* Configure transmitter SSC parameters */
  1706. writel(0x00010202, &xcvr->afe_tx_ssc_control);
  1707. udelay(AFE_REGISTER_WRITE_DELAY);
  1708. /* All defaults, except the Receive Word
  1709. * Alignament/Comma Detect Enable....(0xe800)
  1710. */
  1711. writel(0x0001C500, &xcvr->afe_xcvr_control0);
  1712. udelay(AFE_REGISTER_WRITE_DELAY);
  1713. }
  1714. /* Power up TX and RX out from power down (PWRDNTX and
  1715. * PWRDNRX) & increase TX int & ext bias 20%....(0xe85c)
  1716. */
  1717. if (is_a2(pdev))
  1718. writel(0x000003F0, &xcvr->afe_channel_control);
  1719. else if (is_b0(pdev)) {
  1720. writel(0x000003D7, &xcvr->afe_channel_control);
  1721. udelay(AFE_REGISTER_WRITE_DELAY);
  1722. writel(0x000003D4, &xcvr->afe_channel_control);
  1723. } else if (is_c0(pdev)) {
  1724. writel(0x000001E7, &xcvr->afe_channel_control);
  1725. udelay(AFE_REGISTER_WRITE_DELAY);
  1726. writel(0x000001E4, &xcvr->afe_channel_control);
  1727. } else if (is_c1(pdev)) {
  1728. writel(cable_length_long ? 0x000002F7 : 0x000001F7,
  1729. &xcvr->afe_channel_control);
  1730. udelay(AFE_REGISTER_WRITE_DELAY);
  1731. writel(cable_length_long ? 0x000002F4 : 0x000001F4,
  1732. &xcvr->afe_channel_control);
  1733. }
  1734. udelay(AFE_REGISTER_WRITE_DELAY);
  1735. if (is_a2(pdev)) {
  1736. /* Enable TX equalization (0xe824) */
  1737. writel(0x00040000, &xcvr->afe_tx_control);
  1738. udelay(AFE_REGISTER_WRITE_DELAY);
  1739. }
  1740. if (is_a2(pdev) || is_b0(pdev))
  1741. /* RDPI=0x0(RX Power On), RXOOBDETPDNC=0x0,
  1742. * TPD=0x0(TX Power On), RDD=0x0(RX Detect
  1743. * Enabled) ....(0xe800)
  1744. */
  1745. writel(0x00004100, &xcvr->afe_xcvr_control0);
  1746. else if (is_c0(pdev))
  1747. writel(0x00014100, &xcvr->afe_xcvr_control0);
  1748. else if (is_c1(pdev))
  1749. writel(0x0001C100, &xcvr->afe_xcvr_control0);
  1750. udelay(AFE_REGISTER_WRITE_DELAY);
  1751. /* Leave DFE/FFE on */
  1752. if (is_a2(pdev))
  1753. writel(0x3F11103F, &xcvr->afe_rx_ssc_control0);
  1754. else if (is_b0(pdev)) {
  1755. writel(0x3F11103F, &xcvr->afe_rx_ssc_control0);
  1756. udelay(AFE_REGISTER_WRITE_DELAY);
  1757. /* Enable TX equalization (0xe824) */
  1758. writel(0x00040000, &xcvr->afe_tx_control);
  1759. } else if (is_c0(pdev)) {
  1760. writel(0x01400C0F, &xcvr->afe_rx_ssc_control1);
  1761. udelay(AFE_REGISTER_WRITE_DELAY);
  1762. writel(0x3F6F103F, &xcvr->afe_rx_ssc_control0);
  1763. udelay(AFE_REGISTER_WRITE_DELAY);
  1764. /* Enable TX equalization (0xe824) */
  1765. writel(0x00040000, &xcvr->afe_tx_control);
  1766. } else if (is_c1(pdev)) {
  1767. writel(cable_length_long ? 0x01500C0C :
  1768. cable_length_medium ? 0x01400C0D : 0x02400C0D,
  1769. &xcvr->afe_xcvr_control1);
  1770. udelay(AFE_REGISTER_WRITE_DELAY);
  1771. writel(0x000003E0, &xcvr->afe_dfx_rx_control1);
  1772. udelay(AFE_REGISTER_WRITE_DELAY);
  1773. writel(cable_length_long ? 0x33091C1F :
  1774. cable_length_medium ? 0x3315181F : 0x2B17161F,
  1775. &xcvr->afe_rx_ssc_control0);
  1776. udelay(AFE_REGISTER_WRITE_DELAY);
  1777. /* Enable TX equalization (0xe824) */
  1778. writel(0x00040000, &xcvr->afe_tx_control);
  1779. }
  1780. udelay(AFE_REGISTER_WRITE_DELAY);
  1781. writel(oem_phy->afe_tx_amp_control0, &xcvr->afe_tx_amp_control0);
  1782. udelay(AFE_REGISTER_WRITE_DELAY);
  1783. writel(oem_phy->afe_tx_amp_control1, &xcvr->afe_tx_amp_control1);
  1784. udelay(AFE_REGISTER_WRITE_DELAY);
  1785. writel(oem_phy->afe_tx_amp_control2, &xcvr->afe_tx_amp_control2);
  1786. udelay(AFE_REGISTER_WRITE_DELAY);
  1787. writel(oem_phy->afe_tx_amp_control3, &xcvr->afe_tx_amp_control3);
  1788. udelay(AFE_REGISTER_WRITE_DELAY);
  1789. }
  1790. /* Transfer control to the PEs */
  1791. writel(0x00010f00, &afe->afe_dfx_master_control0);
  1792. udelay(AFE_REGISTER_WRITE_DELAY);
  1793. }
  1794. static void sci_controller_initialize_power_control(struct isci_host *ihost)
  1795. {
  1796. sci_init_timer(&ihost->power_control.timer, power_control_timeout);
  1797. memset(ihost->power_control.requesters, 0,
  1798. sizeof(ihost->power_control.requesters));
  1799. ihost->power_control.phys_waiting = 0;
  1800. ihost->power_control.phys_granted_power = 0;
  1801. }
  1802. static enum sci_status sci_controller_initialize(struct isci_host *ihost)
  1803. {
  1804. struct sci_base_state_machine *sm = &ihost->sm;
  1805. enum sci_status result = SCI_FAILURE;
  1806. unsigned long i, state, val;
  1807. if (ihost->sm.current_state_id != SCIC_RESET) {
  1808. dev_warn(&ihost->pdev->dev, "%s invalid state: %d\n",
  1809. __func__, ihost->sm.current_state_id);
  1810. return SCI_FAILURE_INVALID_STATE;
  1811. }
  1812. sci_change_state(sm, SCIC_INITIALIZING);
  1813. sci_init_timer(&ihost->phy_timer, phy_startup_timeout);
  1814. ihost->next_phy_to_start = 0;
  1815. ihost->phy_startup_timer_pending = false;
  1816. sci_controller_initialize_power_control(ihost);
  1817. /*
  1818. * There is nothing to do here for B0 since we do not have to
  1819. * program the AFE registers.
  1820. * / @todo The AFE settings are supposed to be correct for the B0 but
  1821. * / presently they seem to be wrong. */
  1822. sci_controller_afe_initialization(ihost);
  1823. /* Take the hardware out of reset */
  1824. writel(0, &ihost->smu_registers->soft_reset_control);
  1825. /*
  1826. * / @todo Provide meaningfull error code for hardware failure
  1827. * result = SCI_FAILURE_CONTROLLER_HARDWARE; */
  1828. for (i = 100; i >= 1; i--) {
  1829. u32 status;
  1830. /* Loop until the hardware reports success */
  1831. udelay(SCU_CONTEXT_RAM_INIT_STALL_TIME);
  1832. status = readl(&ihost->smu_registers->control_status);
  1833. if ((status & SCU_RAM_INIT_COMPLETED) == SCU_RAM_INIT_COMPLETED)
  1834. break;
  1835. }
  1836. if (i == 0)
  1837. goto out;
  1838. /*
  1839. * Determine what are the actaul device capacities that the
  1840. * hardware will support */
  1841. val = readl(&ihost->smu_registers->device_context_capacity);
  1842. /* Record the smaller of the two capacity values */
  1843. ihost->logical_port_entries = min(smu_max_ports(val), SCI_MAX_PORTS);
  1844. ihost->task_context_entries = min(smu_max_task_contexts(val), SCI_MAX_IO_REQUESTS);
  1845. ihost->remote_node_entries = min(smu_max_rncs(val), SCI_MAX_REMOTE_DEVICES);
  1846. /*
  1847. * Make all PEs that are unassigned match up with the
  1848. * logical ports
  1849. */
  1850. for (i = 0; i < ihost->logical_port_entries; i++) {
  1851. struct scu_port_task_scheduler_group_registers __iomem
  1852. *ptsg = &ihost->scu_registers->peg0.ptsg;
  1853. writel(i, &ptsg->protocol_engine[i]);
  1854. }
  1855. /* Initialize hardware PCI Relaxed ordering in DMA engines */
  1856. val = readl(&ihost->scu_registers->sdma.pdma_configuration);
  1857. val |= SCU_PDMACR_GEN_BIT(PCI_RELAXED_ORDERING_ENABLE);
  1858. writel(val, &ihost->scu_registers->sdma.pdma_configuration);
  1859. val = readl(&ihost->scu_registers->sdma.cdma_configuration);
  1860. val |= SCU_CDMACR_GEN_BIT(PCI_RELAXED_ORDERING_ENABLE);
  1861. writel(val, &ihost->scu_registers->sdma.cdma_configuration);
  1862. /*
  1863. * Initialize the PHYs before the PORTs because the PHY registers
  1864. * are accessed during the port initialization.
  1865. */
  1866. for (i = 0; i < SCI_MAX_PHYS; i++) {
  1867. result = sci_phy_initialize(&ihost->phys[i],
  1868. &ihost->scu_registers->peg0.pe[i].tl,
  1869. &ihost->scu_registers->peg0.pe[i].ll);
  1870. if (result != SCI_SUCCESS)
  1871. goto out;
  1872. }
  1873. for (i = 0; i < ihost->logical_port_entries; i++) {
  1874. struct isci_port *iport = &ihost->ports[i];
  1875. iport->port_task_scheduler_registers = &ihost->scu_registers->peg0.ptsg.port[i];
  1876. iport->port_pe_configuration_register = &ihost->scu_registers->peg0.ptsg.protocol_engine[0];
  1877. iport->viit_registers = &ihost->scu_registers->peg0.viit[i];
  1878. }
  1879. result = sci_port_configuration_agent_initialize(ihost, &ihost->port_agent);
  1880. out:
  1881. /* Advance the controller state machine */
  1882. if (result == SCI_SUCCESS)
  1883. state = SCIC_INITIALIZED;
  1884. else
  1885. state = SCIC_FAILED;
  1886. sci_change_state(sm, state);
  1887. return result;
  1888. }
  1889. static int sci_controller_dma_alloc(struct isci_host *ihost)
  1890. {
  1891. struct device *dev = &ihost->pdev->dev;
  1892. size_t size;
  1893. int i;
  1894. /* detect re-initialization */
  1895. if (ihost->completion_queue)
  1896. return 0;
  1897. size = SCU_MAX_COMPLETION_QUEUE_ENTRIES * sizeof(u32);
  1898. ihost->completion_queue = dmam_alloc_coherent(dev, size, &ihost->cq_dma,
  1899. GFP_KERNEL);
  1900. if (!ihost->completion_queue)
  1901. return -ENOMEM;
  1902. size = ihost->remote_node_entries * sizeof(union scu_remote_node_context);
  1903. ihost->remote_node_context_table = dmam_alloc_coherent(dev, size, &ihost->rnc_dma,
  1904. GFP_KERNEL);
  1905. if (!ihost->remote_node_context_table)
  1906. return -ENOMEM;
  1907. size = ihost->task_context_entries * sizeof(struct scu_task_context),
  1908. ihost->task_context_table = dmam_alloc_coherent(dev, size, &ihost->tc_dma,
  1909. GFP_KERNEL);
  1910. if (!ihost->task_context_table)
  1911. return -ENOMEM;
  1912. size = SCI_UFI_TOTAL_SIZE;
  1913. ihost->ufi_buf = dmam_alloc_coherent(dev, size, &ihost->ufi_dma, GFP_KERNEL);
  1914. if (!ihost->ufi_buf)
  1915. return -ENOMEM;
  1916. for (i = 0; i < SCI_MAX_IO_REQUESTS; i++) {
  1917. struct isci_request *ireq;
  1918. dma_addr_t dma;
  1919. ireq = dmam_alloc_coherent(dev, sizeof(*ireq), &dma, GFP_KERNEL);
  1920. if (!ireq)
  1921. return -ENOMEM;
  1922. ireq->tc = &ihost->task_context_table[i];
  1923. ireq->owning_controller = ihost;
  1924. spin_lock_init(&ireq->state_lock);
  1925. ireq->request_daddr = dma;
  1926. ireq->isci_host = ihost;
  1927. ihost->reqs[i] = ireq;
  1928. }
  1929. return 0;
  1930. }
  1931. static int sci_controller_mem_init(struct isci_host *ihost)
  1932. {
  1933. int err = sci_controller_dma_alloc(ihost);
  1934. if (err)
  1935. return err;
  1936. writel(lower_32_bits(ihost->cq_dma), &ihost->smu_registers->completion_queue_lower);
  1937. writel(upper_32_bits(ihost->cq_dma), &ihost->smu_registers->completion_queue_upper);
  1938. writel(lower_32_bits(ihost->rnc_dma), &ihost->smu_registers->remote_node_context_lower);
  1939. writel(upper_32_bits(ihost->rnc_dma), &ihost->smu_registers->remote_node_context_upper);
  1940. writel(lower_32_bits(ihost->tc_dma), &ihost->smu_registers->host_task_table_lower);
  1941. writel(upper_32_bits(ihost->tc_dma), &ihost->smu_registers->host_task_table_upper);
  1942. sci_unsolicited_frame_control_construct(ihost);
  1943. /*
  1944. * Inform the silicon as to the location of the UF headers and
  1945. * address table.
  1946. */
  1947. writel(lower_32_bits(ihost->uf_control.headers.physical_address),
  1948. &ihost->scu_registers->sdma.uf_header_base_address_lower);
  1949. writel(upper_32_bits(ihost->uf_control.headers.physical_address),
  1950. &ihost->scu_registers->sdma.uf_header_base_address_upper);
  1951. writel(lower_32_bits(ihost->uf_control.address_table.physical_address),
  1952. &ihost->scu_registers->sdma.uf_address_table_lower);
  1953. writel(upper_32_bits(ihost->uf_control.address_table.physical_address),
  1954. &ihost->scu_registers->sdma.uf_address_table_upper);
  1955. return 0;
  1956. }
  1957. /**
  1958. * isci_host_init - (re-)initialize hardware and internal (private) state
  1959. * @ihost: host to init
  1960. *
  1961. * Any public facing objects (like asd_sas_port, and asd_sas_phys), or
  1962. * one-time initialization objects like locks and waitqueues, are
  1963. * not touched (they are initialized in isci_host_alloc)
  1964. */
  1965. int isci_host_init(struct isci_host *ihost)
  1966. {
  1967. int i, err;
  1968. enum sci_status status;
  1969. status = sci_controller_construct(ihost, scu_base(ihost), smu_base(ihost));
  1970. if (status != SCI_SUCCESS) {
  1971. dev_err(&ihost->pdev->dev,
  1972. "%s: sci_controller_construct failed - status = %x\n",
  1973. __func__,
  1974. status);
  1975. return -ENODEV;
  1976. }
  1977. spin_lock_irq(&ihost->scic_lock);
  1978. status = sci_controller_initialize(ihost);
  1979. spin_unlock_irq(&ihost->scic_lock);
  1980. if (status != SCI_SUCCESS) {
  1981. dev_warn(&ihost->pdev->dev,
  1982. "%s: sci_controller_initialize failed -"
  1983. " status = 0x%x\n",
  1984. __func__, status);
  1985. return -ENODEV;
  1986. }
  1987. err = sci_controller_mem_init(ihost);
  1988. if (err)
  1989. return err;
  1990. /* enable sgpio */
  1991. writel(1, &ihost->scu_registers->peg0.sgpio.interface_control);
  1992. for (i = 0; i < isci_gpio_count(ihost); i++)
  1993. writel(SGPIO_HW_CONTROL, &ihost->scu_registers->peg0.sgpio.output_data_select[i]);
  1994. writel(0, &ihost->scu_registers->peg0.sgpio.vendor_specific_code);
  1995. return 0;
  1996. }
  1997. void sci_controller_link_up(struct isci_host *ihost, struct isci_port *iport,
  1998. struct isci_phy *iphy)
  1999. {
  2000. switch (ihost->sm.current_state_id) {
  2001. case SCIC_STARTING:
  2002. sci_del_timer(&ihost->phy_timer);
  2003. ihost->phy_startup_timer_pending = false;
  2004. ihost->port_agent.link_up_handler(ihost, &ihost->port_agent,
  2005. iport, iphy);
  2006. sci_controller_start_next_phy(ihost);
  2007. break;
  2008. case SCIC_READY:
  2009. ihost->port_agent.link_up_handler(ihost, &ihost->port_agent,
  2010. iport, iphy);
  2011. break;
  2012. default:
  2013. dev_dbg(&ihost->pdev->dev,
  2014. "%s: SCIC Controller linkup event from phy %d in "
  2015. "unexpected state %d\n", __func__, iphy->phy_index,
  2016. ihost->sm.current_state_id);
  2017. }
  2018. }
  2019. void sci_controller_link_down(struct isci_host *ihost, struct isci_port *iport,
  2020. struct isci_phy *iphy)
  2021. {
  2022. switch (ihost->sm.current_state_id) {
  2023. case SCIC_STARTING:
  2024. case SCIC_READY:
  2025. ihost->port_agent.link_down_handler(ihost, &ihost->port_agent,
  2026. iport, iphy);
  2027. break;
  2028. default:
  2029. dev_dbg(&ihost->pdev->dev,
  2030. "%s: SCIC Controller linkdown event from phy %d in "
  2031. "unexpected state %d\n",
  2032. __func__,
  2033. iphy->phy_index,
  2034. ihost->sm.current_state_id);
  2035. }
  2036. }
  2037. static bool sci_controller_has_remote_devices_stopping(struct isci_host *ihost)
  2038. {
  2039. u32 index;
  2040. for (index = 0; index < ihost->remote_node_entries; index++) {
  2041. if ((ihost->device_table[index] != NULL) &&
  2042. (ihost->device_table[index]->sm.current_state_id == SCI_DEV_STOPPING))
  2043. return true;
  2044. }
  2045. return false;
  2046. }
  2047. void sci_controller_remote_device_stopped(struct isci_host *ihost,
  2048. struct isci_remote_device *idev)
  2049. {
  2050. if (ihost->sm.current_state_id != SCIC_STOPPING) {
  2051. dev_dbg(&ihost->pdev->dev,
  2052. "SCIC Controller 0x%p remote device stopped event "
  2053. "from device 0x%p in unexpected state %d\n",
  2054. ihost, idev,
  2055. ihost->sm.current_state_id);
  2056. return;
  2057. }
  2058. if (!sci_controller_has_remote_devices_stopping(ihost))
  2059. sci_change_state(&ihost->sm, SCIC_STOPPED);
  2060. }
  2061. void sci_controller_post_request(struct isci_host *ihost, u32 request)
  2062. {
  2063. dev_dbg(&ihost->pdev->dev, "%s[%d]: %#x\n",
  2064. __func__, ihost->id, request);
  2065. writel(request, &ihost->smu_registers->post_context_port);
  2066. }
  2067. struct isci_request *sci_request_by_tag(struct isci_host *ihost, u16 io_tag)
  2068. {
  2069. u16 task_index;
  2070. u16 task_sequence;
  2071. task_index = ISCI_TAG_TCI(io_tag);
  2072. if (task_index < ihost->task_context_entries) {
  2073. struct isci_request *ireq = ihost->reqs[task_index];
  2074. if (test_bit(IREQ_ACTIVE, &ireq->flags)) {
  2075. task_sequence = ISCI_TAG_SEQ(io_tag);
  2076. if (task_sequence == ihost->io_request_sequence[task_index])
  2077. return ireq;
  2078. }
  2079. }
  2080. return NULL;
  2081. }
  2082. /**
  2083. * This method allocates remote node index and the reserves the remote node
  2084. * context space for use. This method can fail if there are no more remote
  2085. * node index available.
  2086. * @scic: This is the controller object which contains the set of
  2087. * free remote node ids
  2088. * @sci_dev: This is the device object which is requesting the a remote node
  2089. * id
  2090. * @node_id: This is the remote node id that is assinged to the device if one
  2091. * is available
  2092. *
  2093. * enum sci_status SCI_FAILURE_OUT_OF_RESOURCES if there are no available remote
  2094. * node index available.
  2095. */
  2096. enum sci_status sci_controller_allocate_remote_node_context(struct isci_host *ihost,
  2097. struct isci_remote_device *idev,
  2098. u16 *node_id)
  2099. {
  2100. u16 node_index;
  2101. u32 remote_node_count = sci_remote_device_node_count(idev);
  2102. node_index = sci_remote_node_table_allocate_remote_node(
  2103. &ihost->available_remote_nodes, remote_node_count
  2104. );
  2105. if (node_index != SCIC_SDS_REMOTE_NODE_CONTEXT_INVALID_INDEX) {
  2106. ihost->device_table[node_index] = idev;
  2107. *node_id = node_index;
  2108. return SCI_SUCCESS;
  2109. }
  2110. return SCI_FAILURE_INSUFFICIENT_RESOURCES;
  2111. }
  2112. void sci_controller_free_remote_node_context(struct isci_host *ihost,
  2113. struct isci_remote_device *idev,
  2114. u16 node_id)
  2115. {
  2116. u32 remote_node_count = sci_remote_device_node_count(idev);
  2117. if (ihost->device_table[node_id] == idev) {
  2118. ihost->device_table[node_id] = NULL;
  2119. sci_remote_node_table_release_remote_node_index(
  2120. &ihost->available_remote_nodes, remote_node_count, node_id
  2121. );
  2122. }
  2123. }
  2124. void sci_controller_copy_sata_response(void *response_buffer,
  2125. void *frame_header,
  2126. void *frame_buffer)
  2127. {
  2128. /* XXX type safety? */
  2129. memcpy(response_buffer, frame_header, sizeof(u32));
  2130. memcpy(response_buffer + sizeof(u32),
  2131. frame_buffer,
  2132. sizeof(struct dev_to_host_fis) - sizeof(u32));
  2133. }
  2134. void sci_controller_release_frame(struct isci_host *ihost, u32 frame_index)
  2135. {
  2136. if (sci_unsolicited_frame_control_release_frame(&ihost->uf_control, frame_index))
  2137. writel(ihost->uf_control.get,
  2138. &ihost->scu_registers->sdma.unsolicited_frame_get_pointer);
  2139. }
  2140. void isci_tci_free(struct isci_host *ihost, u16 tci)
  2141. {
  2142. u16 tail = ihost->tci_tail & (SCI_MAX_IO_REQUESTS-1);
  2143. ihost->tci_pool[tail] = tci;
  2144. ihost->tci_tail = tail + 1;
  2145. }
  2146. static u16 isci_tci_alloc(struct isci_host *ihost)
  2147. {
  2148. u16 head = ihost->tci_head & (SCI_MAX_IO_REQUESTS-1);
  2149. u16 tci = ihost->tci_pool[head];
  2150. ihost->tci_head = head + 1;
  2151. return tci;
  2152. }
  2153. static u16 isci_tci_space(struct isci_host *ihost)
  2154. {
  2155. return CIRC_SPACE(ihost->tci_head, ihost->tci_tail, SCI_MAX_IO_REQUESTS);
  2156. }
  2157. u16 isci_alloc_tag(struct isci_host *ihost)
  2158. {
  2159. if (isci_tci_space(ihost)) {
  2160. u16 tci = isci_tci_alloc(ihost);
  2161. u8 seq = ihost->io_request_sequence[tci];
  2162. return ISCI_TAG(seq, tci);
  2163. }
  2164. return SCI_CONTROLLER_INVALID_IO_TAG;
  2165. }
  2166. enum sci_status isci_free_tag(struct isci_host *ihost, u16 io_tag)
  2167. {
  2168. u16 tci = ISCI_TAG_TCI(io_tag);
  2169. u16 seq = ISCI_TAG_SEQ(io_tag);
  2170. /* prevent tail from passing head */
  2171. if (isci_tci_active(ihost) == 0)
  2172. return SCI_FAILURE_INVALID_IO_TAG;
  2173. if (seq == ihost->io_request_sequence[tci]) {
  2174. ihost->io_request_sequence[tci] = (seq+1) & (SCI_MAX_SEQ-1);
  2175. isci_tci_free(ihost, tci);
  2176. return SCI_SUCCESS;
  2177. }
  2178. return SCI_FAILURE_INVALID_IO_TAG;
  2179. }
  2180. enum sci_status sci_controller_start_io(struct isci_host *ihost,
  2181. struct isci_remote_device *idev,
  2182. struct isci_request *ireq)
  2183. {
  2184. enum sci_status status;
  2185. if (ihost->sm.current_state_id != SCIC_READY) {
  2186. dev_warn(&ihost->pdev->dev, "%s invalid state: %d\n",
  2187. __func__, ihost->sm.current_state_id);
  2188. return SCI_FAILURE_INVALID_STATE;
  2189. }
  2190. status = sci_remote_device_start_io(ihost, idev, ireq);
  2191. if (status != SCI_SUCCESS)
  2192. return status;
  2193. set_bit(IREQ_ACTIVE, &ireq->flags);
  2194. sci_controller_post_request(ihost, ireq->post_context);
  2195. return SCI_SUCCESS;
  2196. }
  2197. enum sci_status sci_controller_terminate_request(struct isci_host *ihost,
  2198. struct isci_remote_device *idev,
  2199. struct isci_request *ireq)
  2200. {
  2201. /* terminate an ongoing (i.e. started) core IO request. This does not
  2202. * abort the IO request at the target, but rather removes the IO
  2203. * request from the host controller.
  2204. */
  2205. enum sci_status status;
  2206. if (ihost->sm.current_state_id != SCIC_READY) {
  2207. dev_warn(&ihost->pdev->dev, "%s invalid state: %d\n",
  2208. __func__, ihost->sm.current_state_id);
  2209. return SCI_FAILURE_INVALID_STATE;
  2210. }
  2211. status = sci_io_request_terminate(ireq);
  2212. if (status != SCI_SUCCESS)
  2213. return status;
  2214. /*
  2215. * Utilize the original post context command and or in the POST_TC_ABORT
  2216. * request sub-type.
  2217. */
  2218. sci_controller_post_request(ihost,
  2219. ireq->post_context | SCU_CONTEXT_COMMAND_REQUEST_POST_TC_ABORT);
  2220. return SCI_SUCCESS;
  2221. }
  2222. /**
  2223. * sci_controller_complete_io() - This method will perform core specific
  2224. * completion operations for an IO request. After this method is invoked,
  2225. * the user should consider the IO request as invalid until it is properly
  2226. * reused (i.e. re-constructed).
  2227. * @ihost: The handle to the controller object for which to complete the
  2228. * IO request.
  2229. * @idev: The handle to the remote device object for which to complete
  2230. * the IO request.
  2231. * @ireq: the handle to the io request object to complete.
  2232. */
  2233. enum sci_status sci_controller_complete_io(struct isci_host *ihost,
  2234. struct isci_remote_device *idev,
  2235. struct isci_request *ireq)
  2236. {
  2237. enum sci_status status;
  2238. u16 index;
  2239. switch (ihost->sm.current_state_id) {
  2240. case SCIC_STOPPING:
  2241. /* XXX: Implement this function */
  2242. return SCI_FAILURE;
  2243. case SCIC_READY:
  2244. status = sci_remote_device_complete_io(ihost, idev, ireq);
  2245. if (status != SCI_SUCCESS)
  2246. return status;
  2247. index = ISCI_TAG_TCI(ireq->io_tag);
  2248. clear_bit(IREQ_ACTIVE, &ireq->flags);
  2249. return SCI_SUCCESS;
  2250. default:
  2251. dev_warn(&ihost->pdev->dev, "%s invalid state: %d\n",
  2252. __func__, ihost->sm.current_state_id);
  2253. return SCI_FAILURE_INVALID_STATE;
  2254. }
  2255. }
  2256. enum sci_status sci_controller_continue_io(struct isci_request *ireq)
  2257. {
  2258. struct isci_host *ihost = ireq->owning_controller;
  2259. if (ihost->sm.current_state_id != SCIC_READY) {
  2260. dev_warn(&ihost->pdev->dev, "%s invalid state: %d\n",
  2261. __func__, ihost->sm.current_state_id);
  2262. return SCI_FAILURE_INVALID_STATE;
  2263. }
  2264. set_bit(IREQ_ACTIVE, &ireq->flags);
  2265. sci_controller_post_request(ihost, ireq->post_context);
  2266. return SCI_SUCCESS;
  2267. }
  2268. /**
  2269. * sci_controller_start_task() - This method is called by the SCIC user to
  2270. * send/start a framework task management request.
  2271. * @controller: the handle to the controller object for which to start the task
  2272. * management request.
  2273. * @remote_device: the handle to the remote device object for which to start
  2274. * the task management request.
  2275. * @task_request: the handle to the task request object to start.
  2276. */
  2277. enum sci_task_status sci_controller_start_task(struct isci_host *ihost,
  2278. struct isci_remote_device *idev,
  2279. struct isci_request *ireq)
  2280. {
  2281. enum sci_status status;
  2282. if (ihost->sm.current_state_id != SCIC_READY) {
  2283. dev_warn(&ihost->pdev->dev,
  2284. "%s: SCIC Controller starting task from invalid "
  2285. "state\n",
  2286. __func__);
  2287. return SCI_TASK_FAILURE_INVALID_STATE;
  2288. }
  2289. status = sci_remote_device_start_task(ihost, idev, ireq);
  2290. switch (status) {
  2291. case SCI_FAILURE_RESET_DEVICE_PARTIAL_SUCCESS:
  2292. set_bit(IREQ_ACTIVE, &ireq->flags);
  2293. /*
  2294. * We will let framework know this task request started successfully,
  2295. * although core is still woring on starting the request (to post tc when
  2296. * RNC is resumed.)
  2297. */
  2298. return SCI_SUCCESS;
  2299. case SCI_SUCCESS:
  2300. set_bit(IREQ_ACTIVE, &ireq->flags);
  2301. sci_controller_post_request(ihost, ireq->post_context);
  2302. break;
  2303. default:
  2304. break;
  2305. }
  2306. return status;
  2307. }
  2308. static int sci_write_gpio_tx_gp(struct isci_host *ihost, u8 reg_index, u8 reg_count, u8 *write_data)
  2309. {
  2310. int d;
  2311. /* no support for TX_GP_CFG */
  2312. if (reg_index == 0)
  2313. return -EINVAL;
  2314. for (d = 0; d < isci_gpio_count(ihost); d++) {
  2315. u32 val = 0x444; /* all ODx.n clear */
  2316. int i;
  2317. for (i = 0; i < 3; i++) {
  2318. int bit = (i << 2) + 2;
  2319. bit = try_test_sas_gpio_gp_bit(to_sas_gpio_od(d, i),
  2320. write_data, reg_index,
  2321. reg_count);
  2322. if (bit < 0)
  2323. break;
  2324. /* if od is set, clear the 'invert' bit */
  2325. val &= ~(bit << ((i << 2) + 2));
  2326. }
  2327. if (i < 3)
  2328. break;
  2329. writel(val, &ihost->scu_registers->peg0.sgpio.output_data_select[d]);
  2330. }
  2331. /* unless reg_index is > 1, we should always be able to write at
  2332. * least one register
  2333. */
  2334. return d > 0;
  2335. }
  2336. int isci_gpio_write(struct sas_ha_struct *sas_ha, u8 reg_type, u8 reg_index,
  2337. u8 reg_count, u8 *write_data)
  2338. {
  2339. struct isci_host *ihost = sas_ha->lldd_ha;
  2340. int written;
  2341. switch (reg_type) {
  2342. case SAS_GPIO_REG_TX_GP:
  2343. written = sci_write_gpio_tx_gp(ihost, reg_index, reg_count, write_data);
  2344. break;
  2345. default:
  2346. written = -EINVAL;
  2347. }
  2348. return written;
  2349. }