host.c 81 KB

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