host.c 81 KB

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