ufshcd.c 72 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154215521562157215821592160216121622163216421652166216721682169217021712172217321742175217621772178217921802181218221832184218521862187218821892190219121922193219421952196219721982199220022012202220322042205220622072208220922102211221222132214221522162217221822192220222122222223222422252226222722282229223022312232223322342235223622372238223922402241224222432244224522462247224822492250225122522253225422552256225722582259226022612262226322642265226622672268226922702271227222732274227522762277227822792280228122822283228422852286228722882289229022912292229322942295229622972298229923002301230223032304230523062307230823092310231123122313231423152316231723182319232023212322232323242325232623272328232923302331233223332334233523362337233823392340234123422343234423452346234723482349235023512352235323542355235623572358235923602361236223632364236523662367236823692370237123722373237423752376237723782379238023812382238323842385238623872388238923902391239223932394239523962397239823992400240124022403240424052406240724082409241024112412241324142415241624172418241924202421242224232424242524262427242824292430243124322433243424352436243724382439244024412442244324442445244624472448244924502451245224532454245524562457245824592460246124622463246424652466246724682469247024712472247324742475247624772478247924802481248224832484248524862487248824892490249124922493249424952496249724982499250025012502250325042505250625072508250925102511251225132514251525162517251825192520252125222523252425252526252725282529253025312532253325342535253625372538253925402541254225432544254525462547254825492550255125522553255425552556255725582559256025612562256325642565256625672568256925702571257225732574257525762577257825792580258125822583258425852586258725882589259025912592259325942595259625972598259926002601260226032604260526062607260826092610261126122613261426152616261726182619262026212622262326242625262626272628262926302631263226332634263526362637263826392640264126422643264426452646264726482649265026512652265326542655265626572658265926602661266226632664266526662667266826692670267126722673267426752676267726782679268026812682268326842685268626872688268926902691269226932694269526962697269826992700270127022703270427052706270727082709271027112712271327142715271627172718271927202721272227232724272527262727272827292730273127322733273427352736273727382739274027412742274327442745274627472748274927502751275227532754275527562757275827592760276127622763276427652766276727682769277027712772277327742775277627772778277927802781278227832784278527862787278827892790279127922793279427952796279727982799280028012802280328042805280628072808280928102811281228132814281528162817281828192820282128222823282428252826282728282829283028312832283328342835283628372838283928402841
  1. /*
  2. * Universal Flash Storage Host controller driver Core
  3. *
  4. * This code is based on drivers/scsi/ufs/ufshcd.c
  5. * Copyright (C) 2011-2013 Samsung India Software Operations
  6. *
  7. * Authors:
  8. * Santosh Yaraganavi <santosh.sy@samsung.com>
  9. * Vinayak Holikatti <h.vinayak@samsung.com>
  10. *
  11. * This program is free software; you can redistribute it and/or
  12. * modify it under the terms of the GNU General Public License
  13. * as published by the Free Software Foundation; either version 2
  14. * of the License, or (at your option) any later version.
  15. * See the COPYING file in the top-level directory or visit
  16. * <http://www.gnu.org/licenses/gpl-2.0.html>
  17. *
  18. * This program is distributed in the hope that it will be useful,
  19. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  20. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  21. * GNU General Public License for more details.
  22. *
  23. * This program is provided "AS IS" and "WITH ALL FAULTS" and
  24. * without warranty of any kind. You are solely responsible for
  25. * determining the appropriateness of using and distributing
  26. * the program and assume all risks associated with your exercise
  27. * of rights with respect to the program, including but not limited
  28. * to infringement of third party rights, the risks and costs of
  29. * program errors, damage to or loss of data, programs or equipment,
  30. * and unavailability or interruption of operations. Under no
  31. * circumstances will the contributor of this Program be liable for
  32. * any damages of any kind arising from your use or distribution of
  33. * this program.
  34. */
  35. #include <linux/async.h>
  36. #include "ufshcd.h"
  37. #define UFSHCD_ENABLE_INTRS (UTP_TRANSFER_REQ_COMPL |\
  38. UTP_TASK_REQ_COMPL |\
  39. UFSHCD_ERROR_MASK)
  40. /* UIC command timeout, unit: ms */
  41. #define UIC_CMD_TIMEOUT 500
  42. /* NOP OUT retries waiting for NOP IN response */
  43. #define NOP_OUT_RETRIES 10
  44. /* Timeout after 30 msecs if NOP OUT hangs without response */
  45. #define NOP_OUT_TIMEOUT 30 /* msecs */
  46. /* Query request retries */
  47. #define QUERY_REQ_RETRIES 10
  48. /* Query request timeout */
  49. #define QUERY_REQ_TIMEOUT 30 /* msec */
  50. /* Expose the flag value from utp_upiu_query.value */
  51. #define MASK_QUERY_UPIU_FLAG_LOC 0xFF
  52. /* Interrupt aggregation default timeout, unit: 40us */
  53. #define INT_AGGR_DEF_TO 0x02
  54. enum {
  55. UFSHCD_MAX_CHANNEL = 0,
  56. UFSHCD_MAX_ID = 1,
  57. UFSHCD_MAX_LUNS = 8,
  58. UFSHCD_CMD_PER_LUN = 32,
  59. UFSHCD_CAN_QUEUE = 32,
  60. };
  61. /* UFSHCD states */
  62. enum {
  63. UFSHCD_STATE_OPERATIONAL,
  64. UFSHCD_STATE_RESET,
  65. UFSHCD_STATE_ERROR,
  66. };
  67. /* Interrupt configuration options */
  68. enum {
  69. UFSHCD_INT_DISABLE,
  70. UFSHCD_INT_ENABLE,
  71. UFSHCD_INT_CLEAR,
  72. };
  73. /*
  74. * ufshcd_wait_for_register - wait for register value to change
  75. * @hba - per-adapter interface
  76. * @reg - mmio register offset
  77. * @mask - mask to apply to read register value
  78. * @val - wait condition
  79. * @interval_us - polling interval in microsecs
  80. * @timeout_ms - timeout in millisecs
  81. *
  82. * Returns -ETIMEDOUT on error, zero on success
  83. */
  84. static int ufshcd_wait_for_register(struct ufs_hba *hba, u32 reg, u32 mask,
  85. u32 val, unsigned long interval_us, unsigned long timeout_ms)
  86. {
  87. int err = 0;
  88. unsigned long timeout = jiffies + msecs_to_jiffies(timeout_ms);
  89. /* ignore bits that we don't intend to wait on */
  90. val = val & mask;
  91. while ((ufshcd_readl(hba, reg) & mask) != val) {
  92. /* wakeup within 50us of expiry */
  93. usleep_range(interval_us, interval_us + 50);
  94. if (time_after(jiffies, timeout)) {
  95. if ((ufshcd_readl(hba, reg) & mask) != val)
  96. err = -ETIMEDOUT;
  97. break;
  98. }
  99. }
  100. return err;
  101. }
  102. /**
  103. * ufshcd_get_intr_mask - Get the interrupt bit mask
  104. * @hba - Pointer to adapter instance
  105. *
  106. * Returns interrupt bit mask per version
  107. */
  108. static inline u32 ufshcd_get_intr_mask(struct ufs_hba *hba)
  109. {
  110. if (hba->ufs_version == UFSHCI_VERSION_10)
  111. return INTERRUPT_MASK_ALL_VER_10;
  112. else
  113. return INTERRUPT_MASK_ALL_VER_11;
  114. }
  115. /**
  116. * ufshcd_get_ufs_version - Get the UFS version supported by the HBA
  117. * @hba - Pointer to adapter instance
  118. *
  119. * Returns UFSHCI version supported by the controller
  120. */
  121. static inline u32 ufshcd_get_ufs_version(struct ufs_hba *hba)
  122. {
  123. return ufshcd_readl(hba, REG_UFS_VERSION);
  124. }
  125. /**
  126. * ufshcd_is_device_present - Check if any device connected to
  127. * the host controller
  128. * @reg_hcs - host controller status register value
  129. *
  130. * Returns 1 if device present, 0 if no device detected
  131. */
  132. static inline int ufshcd_is_device_present(u32 reg_hcs)
  133. {
  134. return (DEVICE_PRESENT & reg_hcs) ? 1 : 0;
  135. }
  136. /**
  137. * ufshcd_get_tr_ocs - Get the UTRD Overall Command Status
  138. * @lrb: pointer to local command reference block
  139. *
  140. * This function is used to get the OCS field from UTRD
  141. * Returns the OCS field in the UTRD
  142. */
  143. static inline int ufshcd_get_tr_ocs(struct ufshcd_lrb *lrbp)
  144. {
  145. return lrbp->utr_descriptor_ptr->header.dword_2 & MASK_OCS;
  146. }
  147. /**
  148. * ufshcd_get_tmr_ocs - Get the UTMRD Overall Command Status
  149. * @task_req_descp: pointer to utp_task_req_desc structure
  150. *
  151. * This function is used to get the OCS field from UTMRD
  152. * Returns the OCS field in the UTMRD
  153. */
  154. static inline int
  155. ufshcd_get_tmr_ocs(struct utp_task_req_desc *task_req_descp)
  156. {
  157. return task_req_descp->header.dword_2 & MASK_OCS;
  158. }
  159. /**
  160. * ufshcd_get_tm_free_slot - get a free slot for task management request
  161. * @hba: per adapter instance
  162. *
  163. * Returns maximum number of task management request slots in case of
  164. * task management queue full or returns the free slot number
  165. */
  166. static inline int ufshcd_get_tm_free_slot(struct ufs_hba *hba)
  167. {
  168. return find_first_zero_bit(&hba->outstanding_tasks, hba->nutmrs);
  169. }
  170. /**
  171. * ufshcd_utrl_clear - Clear a bit in UTRLCLR register
  172. * @hba: per adapter instance
  173. * @pos: position of the bit to be cleared
  174. */
  175. static inline void ufshcd_utrl_clear(struct ufs_hba *hba, u32 pos)
  176. {
  177. ufshcd_writel(hba, ~(1 << pos), REG_UTP_TRANSFER_REQ_LIST_CLEAR);
  178. }
  179. /**
  180. * ufshcd_get_lists_status - Check UCRDY, UTRLRDY and UTMRLRDY
  181. * @reg: Register value of host controller status
  182. *
  183. * Returns integer, 0 on Success and positive value if failed
  184. */
  185. static inline int ufshcd_get_lists_status(u32 reg)
  186. {
  187. /*
  188. * The mask 0xFF is for the following HCS register bits
  189. * Bit Description
  190. * 0 Device Present
  191. * 1 UTRLRDY
  192. * 2 UTMRLRDY
  193. * 3 UCRDY
  194. * 4 HEI
  195. * 5 DEI
  196. * 6-7 reserved
  197. */
  198. return (((reg) & (0xFF)) >> 1) ^ (0x07);
  199. }
  200. /**
  201. * ufshcd_get_uic_cmd_result - Get the UIC command result
  202. * @hba: Pointer to adapter instance
  203. *
  204. * This function gets the result of UIC command completion
  205. * Returns 0 on success, non zero value on error
  206. */
  207. static inline int ufshcd_get_uic_cmd_result(struct ufs_hba *hba)
  208. {
  209. return ufshcd_readl(hba, REG_UIC_COMMAND_ARG_2) &
  210. MASK_UIC_COMMAND_RESULT;
  211. }
  212. /**
  213. * ufshcd_get_dme_attr_val - Get the value of attribute returned by UIC command
  214. * @hba: Pointer to adapter instance
  215. *
  216. * This function gets UIC command argument3
  217. * Returns 0 on success, non zero value on error
  218. */
  219. static inline u32 ufshcd_get_dme_attr_val(struct ufs_hba *hba)
  220. {
  221. return ufshcd_readl(hba, REG_UIC_COMMAND_ARG_3);
  222. }
  223. /**
  224. * ufshcd_get_req_rsp - returns the TR response transaction type
  225. * @ucd_rsp_ptr: pointer to response UPIU
  226. */
  227. static inline int
  228. ufshcd_get_req_rsp(struct utp_upiu_rsp *ucd_rsp_ptr)
  229. {
  230. return be32_to_cpu(ucd_rsp_ptr->header.dword_0) >> 24;
  231. }
  232. /**
  233. * ufshcd_get_rsp_upiu_result - Get the result from response UPIU
  234. * @ucd_rsp_ptr: pointer to response UPIU
  235. *
  236. * This function gets the response status and scsi_status from response UPIU
  237. * Returns the response result code.
  238. */
  239. static inline int
  240. ufshcd_get_rsp_upiu_result(struct utp_upiu_rsp *ucd_rsp_ptr)
  241. {
  242. return be32_to_cpu(ucd_rsp_ptr->header.dword_1) & MASK_RSP_UPIU_RESULT;
  243. }
  244. /*
  245. * ufshcd_get_rsp_upiu_data_seg_len - Get the data segment length
  246. * from response UPIU
  247. * @ucd_rsp_ptr: pointer to response UPIU
  248. *
  249. * Return the data segment length.
  250. */
  251. static inline unsigned int
  252. ufshcd_get_rsp_upiu_data_seg_len(struct utp_upiu_rsp *ucd_rsp_ptr)
  253. {
  254. return be32_to_cpu(ucd_rsp_ptr->header.dword_2) &
  255. MASK_RSP_UPIU_DATA_SEG_LEN;
  256. }
  257. /**
  258. * ufshcd_is_exception_event - Check if the device raised an exception event
  259. * @ucd_rsp_ptr: pointer to response UPIU
  260. *
  261. * The function checks if the device raised an exception event indicated in
  262. * the Device Information field of response UPIU.
  263. *
  264. * Returns true if exception is raised, false otherwise.
  265. */
  266. static inline bool ufshcd_is_exception_event(struct utp_upiu_rsp *ucd_rsp_ptr)
  267. {
  268. return be32_to_cpu(ucd_rsp_ptr->header.dword_2) &
  269. MASK_RSP_EXCEPTION_EVENT ? true : false;
  270. }
  271. /**
  272. * ufshcd_reset_intr_aggr - Reset interrupt aggregation values.
  273. * @hba: per adapter instance
  274. */
  275. static inline void
  276. ufshcd_reset_intr_aggr(struct ufs_hba *hba)
  277. {
  278. ufshcd_writel(hba, INT_AGGR_ENABLE |
  279. INT_AGGR_COUNTER_AND_TIMER_RESET,
  280. REG_UTP_TRANSFER_REQ_INT_AGG_CONTROL);
  281. }
  282. /**
  283. * ufshcd_config_intr_aggr - Configure interrupt aggregation values.
  284. * @hba: per adapter instance
  285. * @cnt: Interrupt aggregation counter threshold
  286. * @tmout: Interrupt aggregation timeout value
  287. */
  288. static inline void
  289. ufshcd_config_intr_aggr(struct ufs_hba *hba, u8 cnt, u8 tmout)
  290. {
  291. ufshcd_writel(hba, INT_AGGR_ENABLE | INT_AGGR_PARAM_WRITE |
  292. INT_AGGR_COUNTER_THLD_VAL(cnt) |
  293. INT_AGGR_TIMEOUT_VAL(tmout),
  294. REG_UTP_TRANSFER_REQ_INT_AGG_CONTROL);
  295. }
  296. /**
  297. * ufshcd_enable_run_stop_reg - Enable run-stop registers,
  298. * When run-stop registers are set to 1, it indicates the
  299. * host controller that it can process the requests
  300. * @hba: per adapter instance
  301. */
  302. static void ufshcd_enable_run_stop_reg(struct ufs_hba *hba)
  303. {
  304. ufshcd_writel(hba, UTP_TASK_REQ_LIST_RUN_STOP_BIT,
  305. REG_UTP_TASK_REQ_LIST_RUN_STOP);
  306. ufshcd_writel(hba, UTP_TRANSFER_REQ_LIST_RUN_STOP_BIT,
  307. REG_UTP_TRANSFER_REQ_LIST_RUN_STOP);
  308. }
  309. /**
  310. * ufshcd_hba_start - Start controller initialization sequence
  311. * @hba: per adapter instance
  312. */
  313. static inline void ufshcd_hba_start(struct ufs_hba *hba)
  314. {
  315. ufshcd_writel(hba, CONTROLLER_ENABLE, REG_CONTROLLER_ENABLE);
  316. }
  317. /**
  318. * ufshcd_is_hba_active - Get controller state
  319. * @hba: per adapter instance
  320. *
  321. * Returns zero if controller is active, 1 otherwise
  322. */
  323. static inline int ufshcd_is_hba_active(struct ufs_hba *hba)
  324. {
  325. return (ufshcd_readl(hba, REG_CONTROLLER_ENABLE) & 0x1) ? 0 : 1;
  326. }
  327. /**
  328. * ufshcd_send_command - Send SCSI or device management commands
  329. * @hba: per adapter instance
  330. * @task_tag: Task tag of the command
  331. */
  332. static inline
  333. void ufshcd_send_command(struct ufs_hba *hba, unsigned int task_tag)
  334. {
  335. __set_bit(task_tag, &hba->outstanding_reqs);
  336. ufshcd_writel(hba, 1 << task_tag, REG_UTP_TRANSFER_REQ_DOOR_BELL);
  337. }
  338. /**
  339. * ufshcd_copy_sense_data - Copy sense data in case of check condition
  340. * @lrb - pointer to local reference block
  341. */
  342. static inline void ufshcd_copy_sense_data(struct ufshcd_lrb *lrbp)
  343. {
  344. int len;
  345. if (lrbp->sense_buffer &&
  346. ufshcd_get_rsp_upiu_data_seg_len(lrbp->ucd_rsp_ptr)) {
  347. len = be16_to_cpu(lrbp->ucd_rsp_ptr->sr.sense_data_len);
  348. memcpy(lrbp->sense_buffer,
  349. lrbp->ucd_rsp_ptr->sr.sense_data,
  350. min_t(int, len, SCSI_SENSE_BUFFERSIZE));
  351. }
  352. }
  353. /**
  354. * ufshcd_query_to_cpu() - formats the buffer to native cpu endian
  355. * @response: upiu query response to convert
  356. */
  357. static inline void ufshcd_query_to_cpu(struct utp_upiu_query *response)
  358. {
  359. response->length = be16_to_cpu(response->length);
  360. response->value = be32_to_cpu(response->value);
  361. }
  362. /**
  363. * ufshcd_query_to_be() - formats the buffer to big endian
  364. * @request: upiu query request to convert
  365. */
  366. static inline void ufshcd_query_to_be(struct utp_upiu_query *request)
  367. {
  368. request->length = cpu_to_be16(request->length);
  369. request->value = cpu_to_be32(request->value);
  370. }
  371. /**
  372. * ufshcd_copy_query_response() - Copy the Query Response and the data
  373. * descriptor
  374. * @hba: per adapter instance
  375. * @lrb - pointer to local reference block
  376. */
  377. static
  378. void ufshcd_copy_query_response(struct ufs_hba *hba, struct ufshcd_lrb *lrbp)
  379. {
  380. struct ufs_query_res *query_res = &hba->dev_cmd.query.response;
  381. /* Get the UPIU response */
  382. query_res->response = ufshcd_get_rsp_upiu_result(lrbp->ucd_rsp_ptr) >>
  383. UPIU_RSP_CODE_OFFSET;
  384. memcpy(&query_res->upiu_res, &lrbp->ucd_rsp_ptr->qr, QUERY_OSF_SIZE);
  385. ufshcd_query_to_cpu(&query_res->upiu_res);
  386. /* Get the descriptor */
  387. if (lrbp->ucd_rsp_ptr->qr.opcode == UPIU_QUERY_OPCODE_READ_DESC) {
  388. u8 *descp = (u8 *)&lrbp->ucd_rsp_ptr +
  389. GENERAL_UPIU_REQUEST_SIZE;
  390. u16 len;
  391. /* data segment length */
  392. len = be32_to_cpu(lrbp->ucd_rsp_ptr->header.dword_2) &
  393. MASK_QUERY_DATA_SEG_LEN;
  394. memcpy(hba->dev_cmd.query.descriptor, descp,
  395. min_t(u16, len, QUERY_DESC_MAX_SIZE));
  396. }
  397. }
  398. /**
  399. * ufshcd_hba_capabilities - Read controller capabilities
  400. * @hba: per adapter instance
  401. */
  402. static inline void ufshcd_hba_capabilities(struct ufs_hba *hba)
  403. {
  404. hba->capabilities = ufshcd_readl(hba, REG_CONTROLLER_CAPABILITIES);
  405. /* nutrs and nutmrs are 0 based values */
  406. hba->nutrs = (hba->capabilities & MASK_TRANSFER_REQUESTS_SLOTS) + 1;
  407. hba->nutmrs =
  408. ((hba->capabilities & MASK_TASK_MANAGEMENT_REQUEST_SLOTS) >> 16) + 1;
  409. }
  410. /**
  411. * ufshcd_ready_for_uic_cmd - Check if controller is ready
  412. * to accept UIC commands
  413. * @hba: per adapter instance
  414. * Return true on success, else false
  415. */
  416. static inline bool ufshcd_ready_for_uic_cmd(struct ufs_hba *hba)
  417. {
  418. if (ufshcd_readl(hba, REG_CONTROLLER_STATUS) & UIC_COMMAND_READY)
  419. return true;
  420. else
  421. return false;
  422. }
  423. /**
  424. * ufshcd_dispatch_uic_cmd - Dispatch UIC commands to unipro layers
  425. * @hba: per adapter instance
  426. * @uic_cmd: UIC command
  427. *
  428. * Mutex must be held.
  429. */
  430. static inline void
  431. ufshcd_dispatch_uic_cmd(struct ufs_hba *hba, struct uic_command *uic_cmd)
  432. {
  433. WARN_ON(hba->active_uic_cmd);
  434. hba->active_uic_cmd = uic_cmd;
  435. /* Write Args */
  436. ufshcd_writel(hba, uic_cmd->argument1, REG_UIC_COMMAND_ARG_1);
  437. ufshcd_writel(hba, uic_cmd->argument2, REG_UIC_COMMAND_ARG_2);
  438. ufshcd_writel(hba, uic_cmd->argument3, REG_UIC_COMMAND_ARG_3);
  439. /* Write UIC Cmd */
  440. ufshcd_writel(hba, uic_cmd->command & COMMAND_OPCODE_MASK,
  441. REG_UIC_COMMAND);
  442. }
  443. /**
  444. * ufshcd_wait_for_uic_cmd - Wait complectioin of UIC command
  445. * @hba: per adapter instance
  446. * @uic_command: UIC command
  447. *
  448. * Must be called with mutex held.
  449. * Returns 0 only if success.
  450. */
  451. static int
  452. ufshcd_wait_for_uic_cmd(struct ufs_hba *hba, struct uic_command *uic_cmd)
  453. {
  454. int ret;
  455. unsigned long flags;
  456. if (wait_for_completion_timeout(&uic_cmd->done,
  457. msecs_to_jiffies(UIC_CMD_TIMEOUT)))
  458. ret = uic_cmd->argument2 & MASK_UIC_COMMAND_RESULT;
  459. else
  460. ret = -ETIMEDOUT;
  461. spin_lock_irqsave(hba->host->host_lock, flags);
  462. hba->active_uic_cmd = NULL;
  463. spin_unlock_irqrestore(hba->host->host_lock, flags);
  464. return ret;
  465. }
  466. /**
  467. * __ufshcd_send_uic_cmd - Send UIC commands and retrieve the result
  468. * @hba: per adapter instance
  469. * @uic_cmd: UIC command
  470. *
  471. * Identical to ufshcd_send_uic_cmd() expect mutex. Must be called
  472. * with mutex held.
  473. * Returns 0 only if success.
  474. */
  475. static int
  476. __ufshcd_send_uic_cmd(struct ufs_hba *hba, struct uic_command *uic_cmd)
  477. {
  478. int ret;
  479. unsigned long flags;
  480. if (!ufshcd_ready_for_uic_cmd(hba)) {
  481. dev_err(hba->dev,
  482. "Controller not ready to accept UIC commands\n");
  483. return -EIO;
  484. }
  485. init_completion(&uic_cmd->done);
  486. spin_lock_irqsave(hba->host->host_lock, flags);
  487. ufshcd_dispatch_uic_cmd(hba, uic_cmd);
  488. spin_unlock_irqrestore(hba->host->host_lock, flags);
  489. ret = ufshcd_wait_for_uic_cmd(hba, uic_cmd);
  490. return ret;
  491. }
  492. /**
  493. * ufshcd_send_uic_cmd - Send UIC commands and retrieve the result
  494. * @hba: per adapter instance
  495. * @uic_cmd: UIC command
  496. *
  497. * Returns 0 only if success.
  498. */
  499. static int
  500. ufshcd_send_uic_cmd(struct ufs_hba *hba, struct uic_command *uic_cmd)
  501. {
  502. int ret;
  503. mutex_lock(&hba->uic_cmd_mutex);
  504. ret = __ufshcd_send_uic_cmd(hba, uic_cmd);
  505. mutex_unlock(&hba->uic_cmd_mutex);
  506. return ret;
  507. }
  508. /**
  509. * ufshcd_map_sg - Map scatter-gather list to prdt
  510. * @lrbp - pointer to local reference block
  511. *
  512. * Returns 0 in case of success, non-zero value in case of failure
  513. */
  514. static int ufshcd_map_sg(struct ufshcd_lrb *lrbp)
  515. {
  516. struct ufshcd_sg_entry *prd_table;
  517. struct scatterlist *sg;
  518. struct scsi_cmnd *cmd;
  519. int sg_segments;
  520. int i;
  521. cmd = lrbp->cmd;
  522. sg_segments = scsi_dma_map(cmd);
  523. if (sg_segments < 0)
  524. return sg_segments;
  525. if (sg_segments) {
  526. lrbp->utr_descriptor_ptr->prd_table_length =
  527. cpu_to_le16((u16) (sg_segments));
  528. prd_table = (struct ufshcd_sg_entry *)lrbp->ucd_prdt_ptr;
  529. scsi_for_each_sg(cmd, sg, sg_segments, i) {
  530. prd_table[i].size =
  531. cpu_to_le32(((u32) sg_dma_len(sg))-1);
  532. prd_table[i].base_addr =
  533. cpu_to_le32(lower_32_bits(sg->dma_address));
  534. prd_table[i].upper_addr =
  535. cpu_to_le32(upper_32_bits(sg->dma_address));
  536. }
  537. } else {
  538. lrbp->utr_descriptor_ptr->prd_table_length = 0;
  539. }
  540. return 0;
  541. }
  542. /**
  543. * ufshcd_enable_intr - enable interrupts
  544. * @hba: per adapter instance
  545. * @intrs: interrupt bits
  546. */
  547. static void ufshcd_enable_intr(struct ufs_hba *hba, u32 intrs)
  548. {
  549. u32 set = ufshcd_readl(hba, REG_INTERRUPT_ENABLE);
  550. if (hba->ufs_version == UFSHCI_VERSION_10) {
  551. u32 rw;
  552. rw = set & INTERRUPT_MASK_RW_VER_10;
  553. set = rw | ((set ^ intrs) & intrs);
  554. } else {
  555. set |= intrs;
  556. }
  557. ufshcd_writel(hba, set, REG_INTERRUPT_ENABLE);
  558. }
  559. /**
  560. * ufshcd_disable_intr - disable interrupts
  561. * @hba: per adapter instance
  562. * @intrs: interrupt bits
  563. */
  564. static void ufshcd_disable_intr(struct ufs_hba *hba, u32 intrs)
  565. {
  566. u32 set = ufshcd_readl(hba, REG_INTERRUPT_ENABLE);
  567. if (hba->ufs_version == UFSHCI_VERSION_10) {
  568. u32 rw;
  569. rw = (set & INTERRUPT_MASK_RW_VER_10) &
  570. ~(intrs & INTERRUPT_MASK_RW_VER_10);
  571. set = rw | ((set & intrs) & ~INTERRUPT_MASK_RW_VER_10);
  572. } else {
  573. set &= ~intrs;
  574. }
  575. ufshcd_writel(hba, set, REG_INTERRUPT_ENABLE);
  576. }
  577. /**
  578. * ufshcd_prepare_req_desc_hdr() - Fills the requests header
  579. * descriptor according to request
  580. * @lrbp: pointer to local reference block
  581. * @upiu_flags: flags required in the header
  582. * @cmd_dir: requests data direction
  583. */
  584. static void ufshcd_prepare_req_desc_hdr(struct ufshcd_lrb *lrbp,
  585. u32 *upiu_flags, enum dma_data_direction cmd_dir)
  586. {
  587. struct utp_transfer_req_desc *req_desc = lrbp->utr_descriptor_ptr;
  588. u32 data_direction;
  589. u32 dword_0;
  590. if (cmd_dir == DMA_FROM_DEVICE) {
  591. data_direction = UTP_DEVICE_TO_HOST;
  592. *upiu_flags = UPIU_CMD_FLAGS_READ;
  593. } else if (cmd_dir == DMA_TO_DEVICE) {
  594. data_direction = UTP_HOST_TO_DEVICE;
  595. *upiu_flags = UPIU_CMD_FLAGS_WRITE;
  596. } else {
  597. data_direction = UTP_NO_DATA_TRANSFER;
  598. *upiu_flags = UPIU_CMD_FLAGS_NONE;
  599. }
  600. dword_0 = data_direction | (lrbp->command_type
  601. << UPIU_COMMAND_TYPE_OFFSET);
  602. if (lrbp->intr_cmd)
  603. dword_0 |= UTP_REQ_DESC_INT_CMD;
  604. /* Transfer request descriptor header fields */
  605. req_desc->header.dword_0 = cpu_to_le32(dword_0);
  606. /*
  607. * assigning invalid value for command status. Controller
  608. * updates OCS on command completion, with the command
  609. * status
  610. */
  611. req_desc->header.dword_2 =
  612. cpu_to_le32(OCS_INVALID_COMMAND_STATUS);
  613. }
  614. /**
  615. * ufshcd_prepare_utp_scsi_cmd_upiu() - fills the utp_transfer_req_desc,
  616. * for scsi commands
  617. * @lrbp - local reference block pointer
  618. * @upiu_flags - flags
  619. */
  620. static
  621. void ufshcd_prepare_utp_scsi_cmd_upiu(struct ufshcd_lrb *lrbp, u32 upiu_flags)
  622. {
  623. struct utp_upiu_req *ucd_req_ptr = lrbp->ucd_req_ptr;
  624. /* command descriptor fields */
  625. ucd_req_ptr->header.dword_0 = UPIU_HEADER_DWORD(
  626. UPIU_TRANSACTION_COMMAND, upiu_flags,
  627. lrbp->lun, lrbp->task_tag);
  628. ucd_req_ptr->header.dword_1 = UPIU_HEADER_DWORD(
  629. UPIU_COMMAND_SET_TYPE_SCSI, 0, 0, 0);
  630. /* Total EHS length and Data segment length will be zero */
  631. ucd_req_ptr->header.dword_2 = 0;
  632. ucd_req_ptr->sc.exp_data_transfer_len =
  633. cpu_to_be32(lrbp->cmd->sdb.length);
  634. memcpy(ucd_req_ptr->sc.cdb, lrbp->cmd->cmnd,
  635. (min_t(unsigned short, lrbp->cmd->cmd_len, MAX_CDB_SIZE)));
  636. }
  637. /**
  638. * ufshcd_prepare_utp_query_req_upiu() - fills the utp_transfer_req_desc,
  639. * for query requsts
  640. * @hba: UFS hba
  641. * @lrbp: local reference block pointer
  642. * @upiu_flags: flags
  643. */
  644. static void ufshcd_prepare_utp_query_req_upiu(struct ufs_hba *hba,
  645. struct ufshcd_lrb *lrbp, u32 upiu_flags)
  646. {
  647. struct utp_upiu_req *ucd_req_ptr = lrbp->ucd_req_ptr;
  648. struct ufs_query *query = &hba->dev_cmd.query;
  649. u16 len = query->request.upiu_req.length;
  650. u8 *descp = (u8 *)lrbp->ucd_req_ptr + GENERAL_UPIU_REQUEST_SIZE;
  651. /* Query request header */
  652. ucd_req_ptr->header.dword_0 = UPIU_HEADER_DWORD(
  653. UPIU_TRANSACTION_QUERY_REQ, upiu_flags,
  654. lrbp->lun, lrbp->task_tag);
  655. ucd_req_ptr->header.dword_1 = UPIU_HEADER_DWORD(
  656. 0, query->request.query_func, 0, 0);
  657. /* Data segment length */
  658. ucd_req_ptr->header.dword_2 = UPIU_HEADER_DWORD(
  659. 0, 0, len >> 8, (u8)len);
  660. /* Copy the Query Request buffer as is */
  661. memcpy(&ucd_req_ptr->qr, &query->request.upiu_req,
  662. QUERY_OSF_SIZE);
  663. ufshcd_query_to_be(&ucd_req_ptr->qr);
  664. /* Copy the Descriptor */
  665. if ((len > 0) && (query->request.upiu_req.opcode ==
  666. UPIU_QUERY_OPCODE_WRITE_DESC)) {
  667. memcpy(descp, query->descriptor,
  668. min_t(u16, len, QUERY_DESC_MAX_SIZE));
  669. }
  670. }
  671. static inline void ufshcd_prepare_utp_nop_upiu(struct ufshcd_lrb *lrbp)
  672. {
  673. struct utp_upiu_req *ucd_req_ptr = lrbp->ucd_req_ptr;
  674. memset(ucd_req_ptr, 0, sizeof(struct utp_upiu_req));
  675. /* command descriptor fields */
  676. ucd_req_ptr->header.dword_0 =
  677. UPIU_HEADER_DWORD(
  678. UPIU_TRANSACTION_NOP_OUT, 0, 0, lrbp->task_tag);
  679. }
  680. /**
  681. * ufshcd_compose_upiu - form UFS Protocol Information Unit(UPIU)
  682. * @hba - per adapter instance
  683. * @lrb - pointer to local reference block
  684. */
  685. static int ufshcd_compose_upiu(struct ufs_hba *hba, struct ufshcd_lrb *lrbp)
  686. {
  687. u32 upiu_flags;
  688. int ret = 0;
  689. switch (lrbp->command_type) {
  690. case UTP_CMD_TYPE_SCSI:
  691. if (likely(lrbp->cmd)) {
  692. ufshcd_prepare_req_desc_hdr(lrbp, &upiu_flags,
  693. lrbp->cmd->sc_data_direction);
  694. ufshcd_prepare_utp_scsi_cmd_upiu(lrbp, upiu_flags);
  695. } else {
  696. ret = -EINVAL;
  697. }
  698. break;
  699. case UTP_CMD_TYPE_DEV_MANAGE:
  700. ufshcd_prepare_req_desc_hdr(lrbp, &upiu_flags, DMA_NONE);
  701. if (hba->dev_cmd.type == DEV_CMD_TYPE_QUERY)
  702. ufshcd_prepare_utp_query_req_upiu(
  703. hba, lrbp, upiu_flags);
  704. else if (hba->dev_cmd.type == DEV_CMD_TYPE_NOP)
  705. ufshcd_prepare_utp_nop_upiu(lrbp);
  706. else
  707. ret = -EINVAL;
  708. break;
  709. case UTP_CMD_TYPE_UFS:
  710. /* For UFS native command implementation */
  711. ret = -ENOTSUPP;
  712. dev_err(hba->dev, "%s: UFS native command are not supported\n",
  713. __func__);
  714. break;
  715. default:
  716. ret = -ENOTSUPP;
  717. dev_err(hba->dev, "%s: unknown command type: 0x%x\n",
  718. __func__, lrbp->command_type);
  719. break;
  720. } /* end of switch */
  721. return ret;
  722. }
  723. /**
  724. * ufshcd_queuecommand - main entry point for SCSI requests
  725. * @cmd: command from SCSI Midlayer
  726. * @done: call back function
  727. *
  728. * Returns 0 for success, non-zero in case of failure
  729. */
  730. static int ufshcd_queuecommand(struct Scsi_Host *host, struct scsi_cmnd *cmd)
  731. {
  732. struct ufshcd_lrb *lrbp;
  733. struct ufs_hba *hba;
  734. unsigned long flags;
  735. int tag;
  736. int err = 0;
  737. hba = shost_priv(host);
  738. tag = cmd->request->tag;
  739. if (hba->ufshcd_state != UFSHCD_STATE_OPERATIONAL) {
  740. err = SCSI_MLQUEUE_HOST_BUSY;
  741. goto out;
  742. }
  743. /* acquire the tag to make sure device cmds don't use it */
  744. if (test_and_set_bit_lock(tag, &hba->lrb_in_use)) {
  745. /*
  746. * Dev manage command in progress, requeue the command.
  747. * Requeuing the command helps in cases where the request *may*
  748. * find different tag instead of waiting for dev manage command
  749. * completion.
  750. */
  751. err = SCSI_MLQUEUE_HOST_BUSY;
  752. goto out;
  753. }
  754. lrbp = &hba->lrb[tag];
  755. WARN_ON(lrbp->cmd);
  756. lrbp->cmd = cmd;
  757. lrbp->sense_bufflen = SCSI_SENSE_BUFFERSIZE;
  758. lrbp->sense_buffer = cmd->sense_buffer;
  759. lrbp->task_tag = tag;
  760. lrbp->lun = cmd->device->lun;
  761. lrbp->intr_cmd = false;
  762. lrbp->command_type = UTP_CMD_TYPE_SCSI;
  763. /* form UPIU before issuing the command */
  764. ufshcd_compose_upiu(hba, lrbp);
  765. err = ufshcd_map_sg(lrbp);
  766. if (err) {
  767. lrbp->cmd = NULL;
  768. clear_bit_unlock(tag, &hba->lrb_in_use);
  769. goto out;
  770. }
  771. /* issue command to the controller */
  772. spin_lock_irqsave(hba->host->host_lock, flags);
  773. ufshcd_send_command(hba, tag);
  774. spin_unlock_irqrestore(hba->host->host_lock, flags);
  775. out:
  776. return err;
  777. }
  778. static int ufshcd_compose_dev_cmd(struct ufs_hba *hba,
  779. struct ufshcd_lrb *lrbp, enum dev_cmd_type cmd_type, int tag)
  780. {
  781. lrbp->cmd = NULL;
  782. lrbp->sense_bufflen = 0;
  783. lrbp->sense_buffer = NULL;
  784. lrbp->task_tag = tag;
  785. lrbp->lun = 0; /* device management cmd is not specific to any LUN */
  786. lrbp->command_type = UTP_CMD_TYPE_DEV_MANAGE;
  787. lrbp->intr_cmd = true; /* No interrupt aggregation */
  788. hba->dev_cmd.type = cmd_type;
  789. return ufshcd_compose_upiu(hba, lrbp);
  790. }
  791. static int
  792. ufshcd_clear_cmd(struct ufs_hba *hba, int tag)
  793. {
  794. int err = 0;
  795. unsigned long flags;
  796. u32 mask = 1 << tag;
  797. /* clear outstanding transaction before retry */
  798. spin_lock_irqsave(hba->host->host_lock, flags);
  799. ufshcd_utrl_clear(hba, tag);
  800. spin_unlock_irqrestore(hba->host->host_lock, flags);
  801. /*
  802. * wait for for h/w to clear corresponding bit in door-bell.
  803. * max. wait is 1 sec.
  804. */
  805. err = ufshcd_wait_for_register(hba,
  806. REG_UTP_TRANSFER_REQ_DOOR_BELL,
  807. mask, ~mask, 1000, 1000);
  808. return err;
  809. }
  810. /**
  811. * ufshcd_dev_cmd_completion() - handles device management command responses
  812. * @hba: per adapter instance
  813. * @lrbp: pointer to local reference block
  814. */
  815. static int
  816. ufshcd_dev_cmd_completion(struct ufs_hba *hba, struct ufshcd_lrb *lrbp)
  817. {
  818. int resp;
  819. int err = 0;
  820. resp = ufshcd_get_req_rsp(lrbp->ucd_rsp_ptr);
  821. switch (resp) {
  822. case UPIU_TRANSACTION_NOP_IN:
  823. if (hba->dev_cmd.type != DEV_CMD_TYPE_NOP) {
  824. err = -EINVAL;
  825. dev_err(hba->dev, "%s: unexpected response %x\n",
  826. __func__, resp);
  827. }
  828. break;
  829. case UPIU_TRANSACTION_QUERY_RSP:
  830. ufshcd_copy_query_response(hba, lrbp);
  831. break;
  832. case UPIU_TRANSACTION_REJECT_UPIU:
  833. /* TODO: handle Reject UPIU Response */
  834. err = -EPERM;
  835. dev_err(hba->dev, "%s: Reject UPIU not fully implemented\n",
  836. __func__);
  837. break;
  838. default:
  839. err = -EINVAL;
  840. dev_err(hba->dev, "%s: Invalid device management cmd response: %x\n",
  841. __func__, resp);
  842. break;
  843. }
  844. return err;
  845. }
  846. static int ufshcd_wait_for_dev_cmd(struct ufs_hba *hba,
  847. struct ufshcd_lrb *lrbp, int max_timeout)
  848. {
  849. int err = 0;
  850. unsigned long time_left;
  851. unsigned long flags;
  852. time_left = wait_for_completion_timeout(hba->dev_cmd.complete,
  853. msecs_to_jiffies(max_timeout));
  854. spin_lock_irqsave(hba->host->host_lock, flags);
  855. hba->dev_cmd.complete = NULL;
  856. if (likely(time_left)) {
  857. err = ufshcd_get_tr_ocs(lrbp);
  858. if (!err)
  859. err = ufshcd_dev_cmd_completion(hba, lrbp);
  860. }
  861. spin_unlock_irqrestore(hba->host->host_lock, flags);
  862. if (!time_left) {
  863. err = -ETIMEDOUT;
  864. if (!ufshcd_clear_cmd(hba, lrbp->task_tag))
  865. /* sucessfully cleared the command, retry if needed */
  866. err = -EAGAIN;
  867. }
  868. return err;
  869. }
  870. /**
  871. * ufshcd_get_dev_cmd_tag - Get device management command tag
  872. * @hba: per-adapter instance
  873. * @tag: pointer to variable with available slot value
  874. *
  875. * Get a free slot and lock it until device management command
  876. * completes.
  877. *
  878. * Returns false if free slot is unavailable for locking, else
  879. * return true with tag value in @tag.
  880. */
  881. static bool ufshcd_get_dev_cmd_tag(struct ufs_hba *hba, int *tag_out)
  882. {
  883. int tag;
  884. bool ret = false;
  885. unsigned long tmp;
  886. if (!tag_out)
  887. goto out;
  888. do {
  889. tmp = ~hba->lrb_in_use;
  890. tag = find_last_bit(&tmp, hba->nutrs);
  891. if (tag >= hba->nutrs)
  892. goto out;
  893. } while (test_and_set_bit_lock(tag, &hba->lrb_in_use));
  894. *tag_out = tag;
  895. ret = true;
  896. out:
  897. return ret;
  898. }
  899. static inline void ufshcd_put_dev_cmd_tag(struct ufs_hba *hba, int tag)
  900. {
  901. clear_bit_unlock(tag, &hba->lrb_in_use);
  902. }
  903. /**
  904. * ufshcd_exec_dev_cmd - API for sending device management requests
  905. * @hba - UFS hba
  906. * @cmd_type - specifies the type (NOP, Query...)
  907. * @timeout - time in seconds
  908. *
  909. * NOTE: Since there is only one available tag for device management commands,
  910. * it is expected you hold the hba->dev_cmd.lock mutex.
  911. */
  912. static int ufshcd_exec_dev_cmd(struct ufs_hba *hba,
  913. enum dev_cmd_type cmd_type, int timeout)
  914. {
  915. struct ufshcd_lrb *lrbp;
  916. int err;
  917. int tag;
  918. struct completion wait;
  919. unsigned long flags;
  920. /*
  921. * Get free slot, sleep if slots are unavailable.
  922. * Even though we use wait_event() which sleeps indefinitely,
  923. * the maximum wait time is bounded by SCSI request timeout.
  924. */
  925. wait_event(hba->dev_cmd.tag_wq, ufshcd_get_dev_cmd_tag(hba, &tag));
  926. init_completion(&wait);
  927. lrbp = &hba->lrb[tag];
  928. WARN_ON(lrbp->cmd);
  929. err = ufshcd_compose_dev_cmd(hba, lrbp, cmd_type, tag);
  930. if (unlikely(err))
  931. goto out_put_tag;
  932. hba->dev_cmd.complete = &wait;
  933. spin_lock_irqsave(hba->host->host_lock, flags);
  934. ufshcd_send_command(hba, tag);
  935. spin_unlock_irqrestore(hba->host->host_lock, flags);
  936. err = ufshcd_wait_for_dev_cmd(hba, lrbp, timeout);
  937. out_put_tag:
  938. ufshcd_put_dev_cmd_tag(hba, tag);
  939. wake_up(&hba->dev_cmd.tag_wq);
  940. return err;
  941. }
  942. /**
  943. * ufshcd_query_flag() - API function for sending flag query requests
  944. * hba: per-adapter instance
  945. * query_opcode: flag query to perform
  946. * idn: flag idn to access
  947. * flag_res: the flag value after the query request completes
  948. *
  949. * Returns 0 for success, non-zero in case of failure
  950. */
  951. static int ufshcd_query_flag(struct ufs_hba *hba, enum query_opcode opcode,
  952. enum flag_idn idn, bool *flag_res)
  953. {
  954. struct ufs_query_req *request;
  955. struct ufs_query_res *response;
  956. int err;
  957. BUG_ON(!hba);
  958. mutex_lock(&hba->dev_cmd.lock);
  959. request = &hba->dev_cmd.query.request;
  960. response = &hba->dev_cmd.query.response;
  961. memset(request, 0, sizeof(struct ufs_query_req));
  962. memset(response, 0, sizeof(struct ufs_query_res));
  963. switch (opcode) {
  964. case UPIU_QUERY_OPCODE_SET_FLAG:
  965. case UPIU_QUERY_OPCODE_CLEAR_FLAG:
  966. case UPIU_QUERY_OPCODE_TOGGLE_FLAG:
  967. request->query_func = UPIU_QUERY_FUNC_STANDARD_WRITE_REQUEST;
  968. break;
  969. case UPIU_QUERY_OPCODE_READ_FLAG:
  970. request->query_func = UPIU_QUERY_FUNC_STANDARD_READ_REQUEST;
  971. if (!flag_res) {
  972. /* No dummy reads */
  973. dev_err(hba->dev, "%s: Invalid argument for read request\n",
  974. __func__);
  975. err = -EINVAL;
  976. goto out_unlock;
  977. }
  978. break;
  979. default:
  980. dev_err(hba->dev,
  981. "%s: Expected query flag opcode but got = %d\n",
  982. __func__, opcode);
  983. err = -EINVAL;
  984. goto out_unlock;
  985. }
  986. request->upiu_req.opcode = opcode;
  987. request->upiu_req.idn = idn;
  988. /* Send query request */
  989. err = ufshcd_exec_dev_cmd(hba, DEV_CMD_TYPE_QUERY,
  990. QUERY_REQ_TIMEOUT);
  991. if (err) {
  992. dev_err(hba->dev,
  993. "%s: Sending flag query for idn %d failed, err = %d\n",
  994. __func__, idn, err);
  995. goto out_unlock;
  996. }
  997. if (flag_res)
  998. *flag_res = (response->upiu_res.value &
  999. MASK_QUERY_UPIU_FLAG_LOC) & 0x1;
  1000. out_unlock:
  1001. mutex_unlock(&hba->dev_cmd.lock);
  1002. return err;
  1003. }
  1004. /**
  1005. * ufshcd_query_attr - API function for sending attribute requests
  1006. * hba: per-adapter instance
  1007. * opcode: attribute opcode
  1008. * idn: attribute idn to access
  1009. * index: index field
  1010. * selector: selector field
  1011. * attr_val: the attribute value after the query request completes
  1012. *
  1013. * Returns 0 for success, non-zero in case of failure
  1014. */
  1015. int ufshcd_query_attr(struct ufs_hba *hba, enum query_opcode opcode,
  1016. enum attr_idn idn, u8 index, u8 selector, u32 *attr_val)
  1017. {
  1018. struct ufs_query_req *request;
  1019. struct ufs_query_res *response;
  1020. int err;
  1021. BUG_ON(!hba);
  1022. if (!attr_val) {
  1023. dev_err(hba->dev, "%s: attribute value required for opcode 0x%x\n",
  1024. __func__, opcode);
  1025. err = -EINVAL;
  1026. goto out;
  1027. }
  1028. mutex_lock(&hba->dev_cmd.lock);
  1029. request = &hba->dev_cmd.query.request;
  1030. response = &hba->dev_cmd.query.response;
  1031. memset(request, 0, sizeof(struct ufs_query_req));
  1032. memset(response, 0, sizeof(struct ufs_query_res));
  1033. switch (opcode) {
  1034. case UPIU_QUERY_OPCODE_WRITE_ATTR:
  1035. request->query_func = UPIU_QUERY_FUNC_STANDARD_WRITE_REQUEST;
  1036. request->upiu_req.value = *attr_val;
  1037. break;
  1038. case UPIU_QUERY_OPCODE_READ_ATTR:
  1039. request->query_func = UPIU_QUERY_FUNC_STANDARD_READ_REQUEST;
  1040. break;
  1041. default:
  1042. dev_err(hba->dev, "%s: Expected query attr opcode but got = 0x%.2x\n",
  1043. __func__, opcode);
  1044. err = -EINVAL;
  1045. goto out_unlock;
  1046. }
  1047. request->upiu_req.opcode = opcode;
  1048. request->upiu_req.idn = idn;
  1049. request->upiu_req.index = index;
  1050. request->upiu_req.selector = selector;
  1051. /* Send query request */
  1052. err = ufshcd_exec_dev_cmd(hba, DEV_CMD_TYPE_QUERY,
  1053. QUERY_REQ_TIMEOUT);
  1054. if (err) {
  1055. dev_err(hba->dev, "%s: opcode 0x%.2x for idn %d failed, err = %d\n",
  1056. __func__, opcode, idn, err);
  1057. goto out_unlock;
  1058. }
  1059. *attr_val = response->upiu_res.value;
  1060. out_unlock:
  1061. mutex_unlock(&hba->dev_cmd.lock);
  1062. out:
  1063. return err;
  1064. }
  1065. /**
  1066. * ufshcd_memory_alloc - allocate memory for host memory space data structures
  1067. * @hba: per adapter instance
  1068. *
  1069. * 1. Allocate DMA memory for Command Descriptor array
  1070. * Each command descriptor consist of Command UPIU, Response UPIU and PRDT
  1071. * 2. Allocate DMA memory for UTP Transfer Request Descriptor List (UTRDL).
  1072. * 3. Allocate DMA memory for UTP Task Management Request Descriptor List
  1073. * (UTMRDL)
  1074. * 4. Allocate memory for local reference block(lrb).
  1075. *
  1076. * Returns 0 for success, non-zero in case of failure
  1077. */
  1078. static int ufshcd_memory_alloc(struct ufs_hba *hba)
  1079. {
  1080. size_t utmrdl_size, utrdl_size, ucdl_size;
  1081. /* Allocate memory for UTP command descriptors */
  1082. ucdl_size = (sizeof(struct utp_transfer_cmd_desc) * hba->nutrs);
  1083. hba->ucdl_base_addr = dmam_alloc_coherent(hba->dev,
  1084. ucdl_size,
  1085. &hba->ucdl_dma_addr,
  1086. GFP_KERNEL);
  1087. /*
  1088. * UFSHCI requires UTP command descriptor to be 128 byte aligned.
  1089. * make sure hba->ucdl_dma_addr is aligned to PAGE_SIZE
  1090. * if hba->ucdl_dma_addr is aligned to PAGE_SIZE, then it will
  1091. * be aligned to 128 bytes as well
  1092. */
  1093. if (!hba->ucdl_base_addr ||
  1094. WARN_ON(hba->ucdl_dma_addr & (PAGE_SIZE - 1))) {
  1095. dev_err(hba->dev,
  1096. "Command Descriptor Memory allocation failed\n");
  1097. goto out;
  1098. }
  1099. /*
  1100. * Allocate memory for UTP Transfer descriptors
  1101. * UFSHCI requires 1024 byte alignment of UTRD
  1102. */
  1103. utrdl_size = (sizeof(struct utp_transfer_req_desc) * hba->nutrs);
  1104. hba->utrdl_base_addr = dmam_alloc_coherent(hba->dev,
  1105. utrdl_size,
  1106. &hba->utrdl_dma_addr,
  1107. GFP_KERNEL);
  1108. if (!hba->utrdl_base_addr ||
  1109. WARN_ON(hba->utrdl_dma_addr & (PAGE_SIZE - 1))) {
  1110. dev_err(hba->dev,
  1111. "Transfer Descriptor Memory allocation failed\n");
  1112. goto out;
  1113. }
  1114. /*
  1115. * Allocate memory for UTP Task Management descriptors
  1116. * UFSHCI requires 1024 byte alignment of UTMRD
  1117. */
  1118. utmrdl_size = sizeof(struct utp_task_req_desc) * hba->nutmrs;
  1119. hba->utmrdl_base_addr = dmam_alloc_coherent(hba->dev,
  1120. utmrdl_size,
  1121. &hba->utmrdl_dma_addr,
  1122. GFP_KERNEL);
  1123. if (!hba->utmrdl_base_addr ||
  1124. WARN_ON(hba->utmrdl_dma_addr & (PAGE_SIZE - 1))) {
  1125. dev_err(hba->dev,
  1126. "Task Management Descriptor Memory allocation failed\n");
  1127. goto out;
  1128. }
  1129. /* Allocate memory for local reference block */
  1130. hba->lrb = devm_kzalloc(hba->dev,
  1131. hba->nutrs * sizeof(struct ufshcd_lrb),
  1132. GFP_KERNEL);
  1133. if (!hba->lrb) {
  1134. dev_err(hba->dev, "LRB Memory allocation failed\n");
  1135. goto out;
  1136. }
  1137. return 0;
  1138. out:
  1139. return -ENOMEM;
  1140. }
  1141. /**
  1142. * ufshcd_host_memory_configure - configure local reference block with
  1143. * memory offsets
  1144. * @hba: per adapter instance
  1145. *
  1146. * Configure Host memory space
  1147. * 1. Update Corresponding UTRD.UCDBA and UTRD.UCDBAU with UCD DMA
  1148. * address.
  1149. * 2. Update each UTRD with Response UPIU offset, Response UPIU length
  1150. * and PRDT offset.
  1151. * 3. Save the corresponding addresses of UTRD, UCD.CMD, UCD.RSP and UCD.PRDT
  1152. * into local reference block.
  1153. */
  1154. static void ufshcd_host_memory_configure(struct ufs_hba *hba)
  1155. {
  1156. struct utp_transfer_cmd_desc *cmd_descp;
  1157. struct utp_transfer_req_desc *utrdlp;
  1158. dma_addr_t cmd_desc_dma_addr;
  1159. dma_addr_t cmd_desc_element_addr;
  1160. u16 response_offset;
  1161. u16 prdt_offset;
  1162. int cmd_desc_size;
  1163. int i;
  1164. utrdlp = hba->utrdl_base_addr;
  1165. cmd_descp = hba->ucdl_base_addr;
  1166. response_offset =
  1167. offsetof(struct utp_transfer_cmd_desc, response_upiu);
  1168. prdt_offset =
  1169. offsetof(struct utp_transfer_cmd_desc, prd_table);
  1170. cmd_desc_size = sizeof(struct utp_transfer_cmd_desc);
  1171. cmd_desc_dma_addr = hba->ucdl_dma_addr;
  1172. for (i = 0; i < hba->nutrs; i++) {
  1173. /* Configure UTRD with command descriptor base address */
  1174. cmd_desc_element_addr =
  1175. (cmd_desc_dma_addr + (cmd_desc_size * i));
  1176. utrdlp[i].command_desc_base_addr_lo =
  1177. cpu_to_le32(lower_32_bits(cmd_desc_element_addr));
  1178. utrdlp[i].command_desc_base_addr_hi =
  1179. cpu_to_le32(upper_32_bits(cmd_desc_element_addr));
  1180. /* Response upiu and prdt offset should be in double words */
  1181. utrdlp[i].response_upiu_offset =
  1182. cpu_to_le16((response_offset >> 2));
  1183. utrdlp[i].prd_table_offset =
  1184. cpu_to_le16((prdt_offset >> 2));
  1185. utrdlp[i].response_upiu_length =
  1186. cpu_to_le16(ALIGNED_UPIU_SIZE >> 2);
  1187. hba->lrb[i].utr_descriptor_ptr = (utrdlp + i);
  1188. hba->lrb[i].ucd_req_ptr =
  1189. (struct utp_upiu_req *)(cmd_descp + i);
  1190. hba->lrb[i].ucd_rsp_ptr =
  1191. (struct utp_upiu_rsp *)cmd_descp[i].response_upiu;
  1192. hba->lrb[i].ucd_prdt_ptr =
  1193. (struct ufshcd_sg_entry *)cmd_descp[i].prd_table;
  1194. }
  1195. }
  1196. /**
  1197. * ufshcd_dme_link_startup - Notify Unipro to perform link startup
  1198. * @hba: per adapter instance
  1199. *
  1200. * UIC_CMD_DME_LINK_STARTUP command must be issued to Unipro layer,
  1201. * in order to initialize the Unipro link startup procedure.
  1202. * Once the Unipro links are up, the device connected to the controller
  1203. * is detected.
  1204. *
  1205. * Returns 0 on success, non-zero value on failure
  1206. */
  1207. static int ufshcd_dme_link_startup(struct ufs_hba *hba)
  1208. {
  1209. struct uic_command uic_cmd = {0};
  1210. int ret;
  1211. uic_cmd.command = UIC_CMD_DME_LINK_STARTUP;
  1212. ret = ufshcd_send_uic_cmd(hba, &uic_cmd);
  1213. if (ret)
  1214. dev_err(hba->dev,
  1215. "dme-link-startup: error code %d\n", ret);
  1216. return ret;
  1217. }
  1218. /**
  1219. * ufshcd_dme_set_attr - UIC command for DME_SET, DME_PEER_SET
  1220. * @hba: per adapter instance
  1221. * @attr_sel: uic command argument1
  1222. * @attr_set: attribute set type as uic command argument2
  1223. * @mib_val: setting value as uic command argument3
  1224. * @peer: indicate whether peer or local
  1225. *
  1226. * Returns 0 on success, non-zero value on failure
  1227. */
  1228. int ufshcd_dme_set_attr(struct ufs_hba *hba, u32 attr_sel,
  1229. u8 attr_set, u32 mib_val, u8 peer)
  1230. {
  1231. struct uic_command uic_cmd = {0};
  1232. static const char *const action[] = {
  1233. "dme-set",
  1234. "dme-peer-set"
  1235. };
  1236. const char *set = action[!!peer];
  1237. int ret;
  1238. uic_cmd.command = peer ?
  1239. UIC_CMD_DME_PEER_SET : UIC_CMD_DME_SET;
  1240. uic_cmd.argument1 = attr_sel;
  1241. uic_cmd.argument2 = UIC_ARG_ATTR_TYPE(attr_set);
  1242. uic_cmd.argument3 = mib_val;
  1243. ret = ufshcd_send_uic_cmd(hba, &uic_cmd);
  1244. if (ret)
  1245. dev_err(hba->dev, "%s: attr-id 0x%x val 0x%x error code %d\n",
  1246. set, UIC_GET_ATTR_ID(attr_sel), mib_val, ret);
  1247. return ret;
  1248. }
  1249. EXPORT_SYMBOL_GPL(ufshcd_dme_set_attr);
  1250. /**
  1251. * ufshcd_dme_get_attr - UIC command for DME_GET, DME_PEER_GET
  1252. * @hba: per adapter instance
  1253. * @attr_sel: uic command argument1
  1254. * @mib_val: the value of the attribute as returned by the UIC command
  1255. * @peer: indicate whether peer or local
  1256. *
  1257. * Returns 0 on success, non-zero value on failure
  1258. */
  1259. int ufshcd_dme_get_attr(struct ufs_hba *hba, u32 attr_sel,
  1260. u32 *mib_val, u8 peer)
  1261. {
  1262. struct uic_command uic_cmd = {0};
  1263. static const char *const action[] = {
  1264. "dme-get",
  1265. "dme-peer-get"
  1266. };
  1267. const char *get = action[!!peer];
  1268. int ret;
  1269. uic_cmd.command = peer ?
  1270. UIC_CMD_DME_PEER_GET : UIC_CMD_DME_GET;
  1271. uic_cmd.argument1 = attr_sel;
  1272. ret = ufshcd_send_uic_cmd(hba, &uic_cmd);
  1273. if (ret) {
  1274. dev_err(hba->dev, "%s: attr-id 0x%x error code %d\n",
  1275. get, UIC_GET_ATTR_ID(attr_sel), ret);
  1276. goto out;
  1277. }
  1278. if (mib_val)
  1279. *mib_val = uic_cmd.argument3;
  1280. out:
  1281. return ret;
  1282. }
  1283. EXPORT_SYMBOL_GPL(ufshcd_dme_get_attr);
  1284. /**
  1285. * ufshcd_complete_dev_init() - checks device readiness
  1286. * hba: per-adapter instance
  1287. *
  1288. * Set fDeviceInit flag and poll until device toggles it.
  1289. */
  1290. static int ufshcd_complete_dev_init(struct ufs_hba *hba)
  1291. {
  1292. int i, retries, err = 0;
  1293. bool flag_res = 1;
  1294. for (retries = QUERY_REQ_RETRIES; retries > 0; retries--) {
  1295. /* Set the fDeviceInit flag */
  1296. err = ufshcd_query_flag(hba, UPIU_QUERY_OPCODE_SET_FLAG,
  1297. QUERY_FLAG_IDN_FDEVICEINIT, NULL);
  1298. if (!err || err == -ETIMEDOUT)
  1299. break;
  1300. dev_dbg(hba->dev, "%s: error %d retrying\n", __func__, err);
  1301. }
  1302. if (err) {
  1303. dev_err(hba->dev,
  1304. "%s setting fDeviceInit flag failed with error %d\n",
  1305. __func__, err);
  1306. goto out;
  1307. }
  1308. /* poll for max. 100 iterations for fDeviceInit flag to clear */
  1309. for (i = 0; i < 100 && !err && flag_res; i++) {
  1310. for (retries = QUERY_REQ_RETRIES; retries > 0; retries--) {
  1311. err = ufshcd_query_flag(hba,
  1312. UPIU_QUERY_OPCODE_READ_FLAG,
  1313. QUERY_FLAG_IDN_FDEVICEINIT, &flag_res);
  1314. if (!err || err == -ETIMEDOUT)
  1315. break;
  1316. dev_dbg(hba->dev, "%s: error %d retrying\n", __func__,
  1317. err);
  1318. }
  1319. }
  1320. if (err)
  1321. dev_err(hba->dev,
  1322. "%s reading fDeviceInit flag failed with error %d\n",
  1323. __func__, err);
  1324. else if (flag_res)
  1325. dev_err(hba->dev,
  1326. "%s fDeviceInit was not cleared by the device\n",
  1327. __func__);
  1328. out:
  1329. return err;
  1330. }
  1331. /**
  1332. * ufshcd_make_hba_operational - Make UFS controller operational
  1333. * @hba: per adapter instance
  1334. *
  1335. * To bring UFS host controller to operational state,
  1336. * 1. Check if device is present
  1337. * 2. Enable required interrupts
  1338. * 3. Configure interrupt aggregation
  1339. * 4. Program UTRL and UTMRL base addres
  1340. * 5. Configure run-stop-registers
  1341. *
  1342. * Returns 0 on success, non-zero value on failure
  1343. */
  1344. static int ufshcd_make_hba_operational(struct ufs_hba *hba)
  1345. {
  1346. int err = 0;
  1347. u32 reg;
  1348. /* check if device present */
  1349. reg = ufshcd_readl(hba, REG_CONTROLLER_STATUS);
  1350. if (!ufshcd_is_device_present(reg)) {
  1351. dev_err(hba->dev, "cc: Device not present\n");
  1352. err = -ENXIO;
  1353. goto out;
  1354. }
  1355. /* Enable required interrupts */
  1356. ufshcd_enable_intr(hba, UFSHCD_ENABLE_INTRS);
  1357. /* Configure interrupt aggregation */
  1358. ufshcd_config_intr_aggr(hba, hba->nutrs - 1, INT_AGGR_DEF_TO);
  1359. /* Configure UTRL and UTMRL base address registers */
  1360. ufshcd_writel(hba, lower_32_bits(hba->utrdl_dma_addr),
  1361. REG_UTP_TRANSFER_REQ_LIST_BASE_L);
  1362. ufshcd_writel(hba, upper_32_bits(hba->utrdl_dma_addr),
  1363. REG_UTP_TRANSFER_REQ_LIST_BASE_H);
  1364. ufshcd_writel(hba, lower_32_bits(hba->utmrdl_dma_addr),
  1365. REG_UTP_TASK_REQ_LIST_BASE_L);
  1366. ufshcd_writel(hba, upper_32_bits(hba->utmrdl_dma_addr),
  1367. REG_UTP_TASK_REQ_LIST_BASE_H);
  1368. /*
  1369. * UCRDY, UTMRLDY and UTRLRDY bits must be 1
  1370. * DEI, HEI bits must be 0
  1371. */
  1372. if (!(ufshcd_get_lists_status(reg))) {
  1373. ufshcd_enable_run_stop_reg(hba);
  1374. } else {
  1375. dev_err(hba->dev,
  1376. "Host controller not ready to process requests");
  1377. err = -EIO;
  1378. goto out;
  1379. }
  1380. if (hba->ufshcd_state == UFSHCD_STATE_RESET)
  1381. scsi_unblock_requests(hba->host);
  1382. hba->ufshcd_state = UFSHCD_STATE_OPERATIONAL;
  1383. out:
  1384. return err;
  1385. }
  1386. /**
  1387. * ufshcd_hba_enable - initialize the controller
  1388. * @hba: per adapter instance
  1389. *
  1390. * The controller resets itself and controller firmware initialization
  1391. * sequence kicks off. When controller is ready it will set
  1392. * the Host Controller Enable bit to 1.
  1393. *
  1394. * Returns 0 on success, non-zero value on failure
  1395. */
  1396. static int ufshcd_hba_enable(struct ufs_hba *hba)
  1397. {
  1398. int retry;
  1399. /*
  1400. * msleep of 1 and 5 used in this function might result in msleep(20),
  1401. * but it was necessary to send the UFS FPGA to reset mode during
  1402. * development and testing of this driver. msleep can be changed to
  1403. * mdelay and retry count can be reduced based on the controller.
  1404. */
  1405. if (!ufshcd_is_hba_active(hba)) {
  1406. /* change controller state to "reset state" */
  1407. ufshcd_hba_stop(hba);
  1408. /*
  1409. * This delay is based on the testing done with UFS host
  1410. * controller FPGA. The delay can be changed based on the
  1411. * host controller used.
  1412. */
  1413. msleep(5);
  1414. }
  1415. /* start controller initialization sequence */
  1416. ufshcd_hba_start(hba);
  1417. /*
  1418. * To initialize a UFS host controller HCE bit must be set to 1.
  1419. * During initialization the HCE bit value changes from 1->0->1.
  1420. * When the host controller completes initialization sequence
  1421. * it sets the value of HCE bit to 1. The same HCE bit is read back
  1422. * to check if the controller has completed initialization sequence.
  1423. * So without this delay the value HCE = 1, set in the previous
  1424. * instruction might be read back.
  1425. * This delay can be changed based on the controller.
  1426. */
  1427. msleep(1);
  1428. /* wait for the host controller to complete initialization */
  1429. retry = 10;
  1430. while (ufshcd_is_hba_active(hba)) {
  1431. if (retry) {
  1432. retry--;
  1433. } else {
  1434. dev_err(hba->dev,
  1435. "Controller enable failed\n");
  1436. return -EIO;
  1437. }
  1438. msleep(5);
  1439. }
  1440. return 0;
  1441. }
  1442. /**
  1443. * ufshcd_link_startup - Initialize unipro link startup
  1444. * @hba: per adapter instance
  1445. *
  1446. * Returns 0 for success, non-zero in case of failure
  1447. */
  1448. static int ufshcd_link_startup(struct ufs_hba *hba)
  1449. {
  1450. int ret;
  1451. /* enable UIC related interrupts */
  1452. ufshcd_enable_intr(hba, UIC_COMMAND_COMPL);
  1453. ret = ufshcd_dme_link_startup(hba);
  1454. if (ret)
  1455. goto out;
  1456. ret = ufshcd_make_hba_operational(hba);
  1457. out:
  1458. if (ret)
  1459. dev_err(hba->dev, "link startup failed %d\n", ret);
  1460. return ret;
  1461. }
  1462. /**
  1463. * ufshcd_verify_dev_init() - Verify device initialization
  1464. * @hba: per-adapter instance
  1465. *
  1466. * Send NOP OUT UPIU and wait for NOP IN response to check whether the
  1467. * device Transport Protocol (UTP) layer is ready after a reset.
  1468. * If the UTP layer at the device side is not initialized, it may
  1469. * not respond with NOP IN UPIU within timeout of %NOP_OUT_TIMEOUT
  1470. * and we retry sending NOP OUT for %NOP_OUT_RETRIES iterations.
  1471. */
  1472. static int ufshcd_verify_dev_init(struct ufs_hba *hba)
  1473. {
  1474. int err = 0;
  1475. int retries;
  1476. mutex_lock(&hba->dev_cmd.lock);
  1477. for (retries = NOP_OUT_RETRIES; retries > 0; retries--) {
  1478. err = ufshcd_exec_dev_cmd(hba, DEV_CMD_TYPE_NOP,
  1479. NOP_OUT_TIMEOUT);
  1480. if (!err || err == -ETIMEDOUT)
  1481. break;
  1482. dev_dbg(hba->dev, "%s: error %d retrying\n", __func__, err);
  1483. }
  1484. mutex_unlock(&hba->dev_cmd.lock);
  1485. if (err)
  1486. dev_err(hba->dev, "%s: NOP OUT failed %d\n", __func__, err);
  1487. return err;
  1488. }
  1489. /**
  1490. * ufshcd_do_reset - reset the host controller
  1491. * @hba: per adapter instance
  1492. *
  1493. * Returns SUCCESS/FAILED
  1494. */
  1495. static int ufshcd_do_reset(struct ufs_hba *hba)
  1496. {
  1497. struct ufshcd_lrb *lrbp;
  1498. unsigned long flags;
  1499. int tag;
  1500. /* block commands from midlayer */
  1501. scsi_block_requests(hba->host);
  1502. spin_lock_irqsave(hba->host->host_lock, flags);
  1503. hba->ufshcd_state = UFSHCD_STATE_RESET;
  1504. /* send controller to reset state */
  1505. ufshcd_hba_stop(hba);
  1506. spin_unlock_irqrestore(hba->host->host_lock, flags);
  1507. /* abort outstanding commands */
  1508. for (tag = 0; tag < hba->nutrs; tag++) {
  1509. if (test_bit(tag, &hba->outstanding_reqs)) {
  1510. lrbp = &hba->lrb[tag];
  1511. if (lrbp->cmd) {
  1512. scsi_dma_unmap(lrbp->cmd);
  1513. lrbp->cmd->result = DID_RESET << 16;
  1514. lrbp->cmd->scsi_done(lrbp->cmd);
  1515. lrbp->cmd = NULL;
  1516. clear_bit_unlock(tag, &hba->lrb_in_use);
  1517. }
  1518. }
  1519. }
  1520. /* complete device management command */
  1521. if (hba->dev_cmd.complete)
  1522. complete(hba->dev_cmd.complete);
  1523. /* clear outstanding request/task bit maps */
  1524. hba->outstanding_reqs = 0;
  1525. hba->outstanding_tasks = 0;
  1526. /* Host controller enable */
  1527. if (ufshcd_hba_enable(hba)) {
  1528. dev_err(hba->dev,
  1529. "Reset: Controller initialization failed\n");
  1530. return FAILED;
  1531. }
  1532. if (ufshcd_link_startup(hba)) {
  1533. dev_err(hba->dev,
  1534. "Reset: Link start-up failed\n");
  1535. return FAILED;
  1536. }
  1537. return SUCCESS;
  1538. }
  1539. /**
  1540. * ufshcd_slave_alloc - handle initial SCSI device configurations
  1541. * @sdev: pointer to SCSI device
  1542. *
  1543. * Returns success
  1544. */
  1545. static int ufshcd_slave_alloc(struct scsi_device *sdev)
  1546. {
  1547. struct ufs_hba *hba;
  1548. hba = shost_priv(sdev->host);
  1549. sdev->tagged_supported = 1;
  1550. /* Mode sense(6) is not supported by UFS, so use Mode sense(10) */
  1551. sdev->use_10_for_ms = 1;
  1552. scsi_set_tag_type(sdev, MSG_SIMPLE_TAG);
  1553. /*
  1554. * Inform SCSI Midlayer that the LUN queue depth is same as the
  1555. * controller queue depth. If a LUN queue depth is less than the
  1556. * controller queue depth and if the LUN reports
  1557. * SAM_STAT_TASK_SET_FULL, the LUN queue depth will be adjusted
  1558. * with scsi_adjust_queue_depth.
  1559. */
  1560. scsi_activate_tcq(sdev, hba->nutrs);
  1561. return 0;
  1562. }
  1563. /**
  1564. * ufshcd_slave_destroy - remove SCSI device configurations
  1565. * @sdev: pointer to SCSI device
  1566. */
  1567. static void ufshcd_slave_destroy(struct scsi_device *sdev)
  1568. {
  1569. struct ufs_hba *hba;
  1570. hba = shost_priv(sdev->host);
  1571. scsi_deactivate_tcq(sdev, hba->nutrs);
  1572. }
  1573. /**
  1574. * ufshcd_task_req_compl - handle task management request completion
  1575. * @hba: per adapter instance
  1576. * @index: index of the completed request
  1577. *
  1578. * Returns SUCCESS/FAILED
  1579. */
  1580. static int ufshcd_task_req_compl(struct ufs_hba *hba, u32 index)
  1581. {
  1582. struct utp_task_req_desc *task_req_descp;
  1583. struct utp_upiu_task_rsp *task_rsp_upiup;
  1584. unsigned long flags;
  1585. int ocs_value;
  1586. int task_result;
  1587. spin_lock_irqsave(hba->host->host_lock, flags);
  1588. /* Clear completed tasks from outstanding_tasks */
  1589. __clear_bit(index, &hba->outstanding_tasks);
  1590. task_req_descp = hba->utmrdl_base_addr;
  1591. ocs_value = ufshcd_get_tmr_ocs(&task_req_descp[index]);
  1592. if (ocs_value == OCS_SUCCESS) {
  1593. task_rsp_upiup = (struct utp_upiu_task_rsp *)
  1594. task_req_descp[index].task_rsp_upiu;
  1595. task_result = be32_to_cpu(task_rsp_upiup->header.dword_1);
  1596. task_result = ((task_result & MASK_TASK_RESPONSE) >> 8);
  1597. if (task_result != UPIU_TASK_MANAGEMENT_FUNC_COMPL &&
  1598. task_result != UPIU_TASK_MANAGEMENT_FUNC_SUCCEEDED)
  1599. task_result = FAILED;
  1600. else
  1601. task_result = SUCCESS;
  1602. } else {
  1603. task_result = FAILED;
  1604. dev_err(hba->dev,
  1605. "trc: Invalid ocs = %x\n", ocs_value);
  1606. }
  1607. spin_unlock_irqrestore(hba->host->host_lock, flags);
  1608. return task_result;
  1609. }
  1610. /**
  1611. * ufshcd_adjust_lun_qdepth - Update LUN queue depth if device responds with
  1612. * SAM_STAT_TASK_SET_FULL SCSI command status.
  1613. * @cmd: pointer to SCSI command
  1614. */
  1615. static void ufshcd_adjust_lun_qdepth(struct scsi_cmnd *cmd)
  1616. {
  1617. struct ufs_hba *hba;
  1618. int i;
  1619. int lun_qdepth = 0;
  1620. hba = shost_priv(cmd->device->host);
  1621. /*
  1622. * LUN queue depth can be obtained by counting outstanding commands
  1623. * on the LUN.
  1624. */
  1625. for (i = 0; i < hba->nutrs; i++) {
  1626. if (test_bit(i, &hba->outstanding_reqs)) {
  1627. /*
  1628. * Check if the outstanding command belongs
  1629. * to the LUN which reported SAM_STAT_TASK_SET_FULL.
  1630. */
  1631. if (cmd->device->lun == hba->lrb[i].lun)
  1632. lun_qdepth++;
  1633. }
  1634. }
  1635. /*
  1636. * LUN queue depth will be total outstanding commands, except the
  1637. * command for which the LUN reported SAM_STAT_TASK_SET_FULL.
  1638. */
  1639. scsi_adjust_queue_depth(cmd->device, MSG_SIMPLE_TAG, lun_qdepth - 1);
  1640. }
  1641. /**
  1642. * ufshcd_scsi_cmd_status - Update SCSI command result based on SCSI status
  1643. * @lrb: pointer to local reference block of completed command
  1644. * @scsi_status: SCSI command status
  1645. *
  1646. * Returns value base on SCSI command status
  1647. */
  1648. static inline int
  1649. ufshcd_scsi_cmd_status(struct ufshcd_lrb *lrbp, int scsi_status)
  1650. {
  1651. int result = 0;
  1652. switch (scsi_status) {
  1653. case SAM_STAT_CHECK_CONDITION:
  1654. ufshcd_copy_sense_data(lrbp);
  1655. case SAM_STAT_GOOD:
  1656. result |= DID_OK << 16 |
  1657. COMMAND_COMPLETE << 8 |
  1658. scsi_status;
  1659. break;
  1660. case SAM_STAT_TASK_SET_FULL:
  1661. /*
  1662. * If a LUN reports SAM_STAT_TASK_SET_FULL, then the LUN queue
  1663. * depth needs to be adjusted to the exact number of
  1664. * outstanding commands the LUN can handle at any given time.
  1665. */
  1666. ufshcd_adjust_lun_qdepth(lrbp->cmd);
  1667. case SAM_STAT_BUSY:
  1668. case SAM_STAT_TASK_ABORTED:
  1669. ufshcd_copy_sense_data(lrbp);
  1670. result |= scsi_status;
  1671. break;
  1672. default:
  1673. result |= DID_ERROR << 16;
  1674. break;
  1675. } /* end of switch */
  1676. return result;
  1677. }
  1678. /**
  1679. * ufshcd_transfer_rsp_status - Get overall status of the response
  1680. * @hba: per adapter instance
  1681. * @lrb: pointer to local reference block of completed command
  1682. *
  1683. * Returns result of the command to notify SCSI midlayer
  1684. */
  1685. static inline int
  1686. ufshcd_transfer_rsp_status(struct ufs_hba *hba, struct ufshcd_lrb *lrbp)
  1687. {
  1688. int result = 0;
  1689. int scsi_status;
  1690. int ocs;
  1691. /* overall command status of utrd */
  1692. ocs = ufshcd_get_tr_ocs(lrbp);
  1693. switch (ocs) {
  1694. case OCS_SUCCESS:
  1695. result = ufshcd_get_req_rsp(lrbp->ucd_rsp_ptr);
  1696. switch (result) {
  1697. case UPIU_TRANSACTION_RESPONSE:
  1698. /*
  1699. * get the response UPIU result to extract
  1700. * the SCSI command status
  1701. */
  1702. result = ufshcd_get_rsp_upiu_result(lrbp->ucd_rsp_ptr);
  1703. /*
  1704. * get the result based on SCSI status response
  1705. * to notify the SCSI midlayer of the command status
  1706. */
  1707. scsi_status = result & MASK_SCSI_STATUS;
  1708. result = ufshcd_scsi_cmd_status(lrbp, scsi_status);
  1709. if (ufshcd_is_exception_event(lrbp->ucd_rsp_ptr))
  1710. schedule_work(&hba->eeh_work);
  1711. break;
  1712. case UPIU_TRANSACTION_REJECT_UPIU:
  1713. /* TODO: handle Reject UPIU Response */
  1714. result = DID_ERROR << 16;
  1715. dev_err(hba->dev,
  1716. "Reject UPIU not fully implemented\n");
  1717. break;
  1718. default:
  1719. result = DID_ERROR << 16;
  1720. dev_err(hba->dev,
  1721. "Unexpected request response code = %x\n",
  1722. result);
  1723. break;
  1724. }
  1725. break;
  1726. case OCS_ABORTED:
  1727. result |= DID_ABORT << 16;
  1728. break;
  1729. case OCS_INVALID_CMD_TABLE_ATTR:
  1730. case OCS_INVALID_PRDT_ATTR:
  1731. case OCS_MISMATCH_DATA_BUF_SIZE:
  1732. case OCS_MISMATCH_RESP_UPIU_SIZE:
  1733. case OCS_PEER_COMM_FAILURE:
  1734. case OCS_FATAL_ERROR:
  1735. default:
  1736. result |= DID_ERROR << 16;
  1737. dev_err(hba->dev,
  1738. "OCS error from controller = %x\n", ocs);
  1739. break;
  1740. } /* end of switch */
  1741. return result;
  1742. }
  1743. /**
  1744. * ufshcd_uic_cmd_compl - handle completion of uic command
  1745. * @hba: per adapter instance
  1746. */
  1747. static void ufshcd_uic_cmd_compl(struct ufs_hba *hba)
  1748. {
  1749. if (hba->active_uic_cmd) {
  1750. hba->active_uic_cmd->argument2 |=
  1751. ufshcd_get_uic_cmd_result(hba);
  1752. hba->active_uic_cmd->argument3 =
  1753. ufshcd_get_dme_attr_val(hba);
  1754. complete(&hba->active_uic_cmd->done);
  1755. }
  1756. }
  1757. /**
  1758. * ufshcd_transfer_req_compl - handle SCSI and query command completion
  1759. * @hba: per adapter instance
  1760. */
  1761. static void ufshcd_transfer_req_compl(struct ufs_hba *hba)
  1762. {
  1763. struct ufshcd_lrb *lrbp;
  1764. struct scsi_cmnd *cmd;
  1765. unsigned long completed_reqs;
  1766. u32 tr_doorbell;
  1767. int result;
  1768. int index;
  1769. bool int_aggr_reset = false;
  1770. tr_doorbell = ufshcd_readl(hba, REG_UTP_TRANSFER_REQ_DOOR_BELL);
  1771. completed_reqs = tr_doorbell ^ hba->outstanding_reqs;
  1772. for (index = 0; index < hba->nutrs; index++) {
  1773. if (test_bit(index, &completed_reqs)) {
  1774. lrbp = &hba->lrb[index];
  1775. cmd = lrbp->cmd;
  1776. /*
  1777. * Don't skip resetting interrupt aggregation counters
  1778. * if a regular command is present.
  1779. */
  1780. int_aggr_reset |= !lrbp->intr_cmd;
  1781. if (cmd) {
  1782. result = ufshcd_transfer_rsp_status(hba, lrbp);
  1783. scsi_dma_unmap(cmd);
  1784. cmd->result = result;
  1785. /* Mark completed command as NULL in LRB */
  1786. lrbp->cmd = NULL;
  1787. clear_bit_unlock(index, &hba->lrb_in_use);
  1788. /* Do not touch lrbp after scsi done */
  1789. cmd->scsi_done(cmd);
  1790. } else if (lrbp->command_type ==
  1791. UTP_CMD_TYPE_DEV_MANAGE) {
  1792. if (hba->dev_cmd.complete)
  1793. complete(hba->dev_cmd.complete);
  1794. }
  1795. } /* end of if */
  1796. } /* end of for */
  1797. /* clear corresponding bits of completed commands */
  1798. hba->outstanding_reqs ^= completed_reqs;
  1799. /* we might have free'd some tags above */
  1800. wake_up(&hba->dev_cmd.tag_wq);
  1801. /* Reset interrupt aggregation counters */
  1802. if (int_aggr_reset)
  1803. ufshcd_reset_intr_aggr(hba);
  1804. }
  1805. /**
  1806. * ufshcd_disable_ee - disable exception event
  1807. * @hba: per-adapter instance
  1808. * @mask: exception event to disable
  1809. *
  1810. * Disables exception event in the device so that the EVENT_ALERT
  1811. * bit is not set.
  1812. *
  1813. * Returns zero on success, non-zero error value on failure.
  1814. */
  1815. static int ufshcd_disable_ee(struct ufs_hba *hba, u16 mask)
  1816. {
  1817. int err = 0;
  1818. u32 val;
  1819. if (!(hba->ee_ctrl_mask & mask))
  1820. goto out;
  1821. val = hba->ee_ctrl_mask & ~mask;
  1822. val &= 0xFFFF; /* 2 bytes */
  1823. err = ufshcd_query_attr(hba, UPIU_QUERY_OPCODE_WRITE_ATTR,
  1824. QUERY_ATTR_IDN_EE_CONTROL, 0, 0, &val);
  1825. if (!err)
  1826. hba->ee_ctrl_mask &= ~mask;
  1827. out:
  1828. return err;
  1829. }
  1830. /**
  1831. * ufshcd_enable_ee - enable exception event
  1832. * @hba: per-adapter instance
  1833. * @mask: exception event to enable
  1834. *
  1835. * Enable corresponding exception event in the device to allow
  1836. * device to alert host in critical scenarios.
  1837. *
  1838. * Returns zero on success, non-zero error value on failure.
  1839. */
  1840. static int ufshcd_enable_ee(struct ufs_hba *hba, u16 mask)
  1841. {
  1842. int err = 0;
  1843. u32 val;
  1844. if (hba->ee_ctrl_mask & mask)
  1845. goto out;
  1846. val = hba->ee_ctrl_mask | mask;
  1847. val &= 0xFFFF; /* 2 bytes */
  1848. err = ufshcd_query_attr(hba, UPIU_QUERY_OPCODE_WRITE_ATTR,
  1849. QUERY_ATTR_IDN_EE_CONTROL, 0, 0, &val);
  1850. if (!err)
  1851. hba->ee_ctrl_mask |= mask;
  1852. out:
  1853. return err;
  1854. }
  1855. /**
  1856. * ufshcd_enable_auto_bkops - Allow device managed BKOPS
  1857. * @hba: per-adapter instance
  1858. *
  1859. * Allow device to manage background operations on its own. Enabling
  1860. * this might lead to inconsistent latencies during normal data transfers
  1861. * as the device is allowed to manage its own way of handling background
  1862. * operations.
  1863. *
  1864. * Returns zero on success, non-zero on failure.
  1865. */
  1866. static int ufshcd_enable_auto_bkops(struct ufs_hba *hba)
  1867. {
  1868. int err = 0;
  1869. if (hba->auto_bkops_enabled)
  1870. goto out;
  1871. err = ufshcd_query_flag(hba, UPIU_QUERY_OPCODE_SET_FLAG,
  1872. QUERY_FLAG_IDN_BKOPS_EN, NULL);
  1873. if (err) {
  1874. dev_err(hba->dev, "%s: failed to enable bkops %d\n",
  1875. __func__, err);
  1876. goto out;
  1877. }
  1878. hba->auto_bkops_enabled = true;
  1879. /* No need of URGENT_BKOPS exception from the device */
  1880. err = ufshcd_disable_ee(hba, MASK_EE_URGENT_BKOPS);
  1881. if (err)
  1882. dev_err(hba->dev, "%s: failed to disable exception event %d\n",
  1883. __func__, err);
  1884. out:
  1885. return err;
  1886. }
  1887. /**
  1888. * ufshcd_disable_auto_bkops - block device in doing background operations
  1889. * @hba: per-adapter instance
  1890. *
  1891. * Disabling background operations improves command response latency but
  1892. * has drawback of device moving into critical state where the device is
  1893. * not-operable. Make sure to call ufshcd_enable_auto_bkops() whenever the
  1894. * host is idle so that BKOPS are managed effectively without any negative
  1895. * impacts.
  1896. *
  1897. * Returns zero on success, non-zero on failure.
  1898. */
  1899. static int ufshcd_disable_auto_bkops(struct ufs_hba *hba)
  1900. {
  1901. int err = 0;
  1902. if (!hba->auto_bkops_enabled)
  1903. goto out;
  1904. /*
  1905. * If host assisted BKOPs is to be enabled, make sure
  1906. * urgent bkops exception is allowed.
  1907. */
  1908. err = ufshcd_enable_ee(hba, MASK_EE_URGENT_BKOPS);
  1909. if (err) {
  1910. dev_err(hba->dev, "%s: failed to enable exception event %d\n",
  1911. __func__, err);
  1912. goto out;
  1913. }
  1914. err = ufshcd_query_flag(hba, UPIU_QUERY_OPCODE_CLEAR_FLAG,
  1915. QUERY_FLAG_IDN_BKOPS_EN, NULL);
  1916. if (err) {
  1917. dev_err(hba->dev, "%s: failed to disable bkops %d\n",
  1918. __func__, err);
  1919. ufshcd_disable_ee(hba, MASK_EE_URGENT_BKOPS);
  1920. goto out;
  1921. }
  1922. hba->auto_bkops_enabled = false;
  1923. out:
  1924. return err;
  1925. }
  1926. /**
  1927. * ufshcd_force_reset_auto_bkops - force enable of auto bkops
  1928. * @hba: per adapter instance
  1929. *
  1930. * After a device reset the device may toggle the BKOPS_EN flag
  1931. * to default value. The s/w tracking variables should be updated
  1932. * as well. Do this by forcing enable of auto bkops.
  1933. */
  1934. static void ufshcd_force_reset_auto_bkops(struct ufs_hba *hba)
  1935. {
  1936. hba->auto_bkops_enabled = false;
  1937. hba->ee_ctrl_mask |= MASK_EE_URGENT_BKOPS;
  1938. ufshcd_enable_auto_bkops(hba);
  1939. }
  1940. static inline int ufshcd_get_bkops_status(struct ufs_hba *hba, u32 *status)
  1941. {
  1942. return ufshcd_query_attr(hba, UPIU_QUERY_OPCODE_READ_ATTR,
  1943. QUERY_ATTR_IDN_BKOPS_STATUS, 0, 0, status);
  1944. }
  1945. /**
  1946. * ufshcd_urgent_bkops - handle urgent bkops exception event
  1947. * @hba: per-adapter instance
  1948. *
  1949. * Enable fBackgroundOpsEn flag in the device to permit background
  1950. * operations.
  1951. */
  1952. static int ufshcd_urgent_bkops(struct ufs_hba *hba)
  1953. {
  1954. int err;
  1955. u32 status = 0;
  1956. err = ufshcd_get_bkops_status(hba, &status);
  1957. if (err) {
  1958. dev_err(hba->dev, "%s: failed to get BKOPS status %d\n",
  1959. __func__, err);
  1960. goto out;
  1961. }
  1962. status = status & 0xF;
  1963. /* handle only if status indicates performance impact or critical */
  1964. if (status >= BKOPS_STATUS_PERF_IMPACT)
  1965. err = ufshcd_enable_auto_bkops(hba);
  1966. out:
  1967. return err;
  1968. }
  1969. static inline int ufshcd_get_ee_status(struct ufs_hba *hba, u32 *status)
  1970. {
  1971. return ufshcd_query_attr(hba, UPIU_QUERY_OPCODE_READ_ATTR,
  1972. QUERY_ATTR_IDN_EE_STATUS, 0, 0, status);
  1973. }
  1974. /**
  1975. * ufshcd_exception_event_handler - handle exceptions raised by device
  1976. * @work: pointer to work data
  1977. *
  1978. * Read bExceptionEventStatus attribute from the device and handle the
  1979. * exception event accordingly.
  1980. */
  1981. static void ufshcd_exception_event_handler(struct work_struct *work)
  1982. {
  1983. struct ufs_hba *hba;
  1984. int err;
  1985. u32 status = 0;
  1986. hba = container_of(work, struct ufs_hba, eeh_work);
  1987. pm_runtime_get_sync(hba->dev);
  1988. err = ufshcd_get_ee_status(hba, &status);
  1989. if (err) {
  1990. dev_err(hba->dev, "%s: failed to get exception status %d\n",
  1991. __func__, err);
  1992. goto out;
  1993. }
  1994. status &= hba->ee_ctrl_mask;
  1995. if (status & MASK_EE_URGENT_BKOPS) {
  1996. err = ufshcd_urgent_bkops(hba);
  1997. if (err)
  1998. dev_err(hba->dev, "%s: failed to handle urgent bkops %d\n",
  1999. __func__, err);
  2000. }
  2001. out:
  2002. pm_runtime_put_sync(hba->dev);
  2003. return;
  2004. }
  2005. /**
  2006. * ufshcd_fatal_err_handler - handle fatal errors
  2007. * @hba: per adapter instance
  2008. */
  2009. static void ufshcd_fatal_err_handler(struct work_struct *work)
  2010. {
  2011. struct ufs_hba *hba;
  2012. hba = container_of(work, struct ufs_hba, feh_workq);
  2013. pm_runtime_get_sync(hba->dev);
  2014. /* check if reset is already in progress */
  2015. if (hba->ufshcd_state != UFSHCD_STATE_RESET)
  2016. ufshcd_do_reset(hba);
  2017. pm_runtime_put_sync(hba->dev);
  2018. }
  2019. /**
  2020. * ufshcd_err_handler - Check for fatal errors
  2021. * @work: pointer to a work queue structure
  2022. */
  2023. static void ufshcd_err_handler(struct ufs_hba *hba)
  2024. {
  2025. u32 reg;
  2026. if (hba->errors & INT_FATAL_ERRORS)
  2027. goto fatal_eh;
  2028. if (hba->errors & UIC_ERROR) {
  2029. reg = ufshcd_readl(hba, REG_UIC_ERROR_CODE_DATA_LINK_LAYER);
  2030. if (reg & UIC_DATA_LINK_LAYER_ERROR_PA_INIT)
  2031. goto fatal_eh;
  2032. }
  2033. return;
  2034. fatal_eh:
  2035. hba->ufshcd_state = UFSHCD_STATE_ERROR;
  2036. schedule_work(&hba->feh_workq);
  2037. }
  2038. /**
  2039. * ufshcd_tmc_handler - handle task management function completion
  2040. * @hba: per adapter instance
  2041. */
  2042. static void ufshcd_tmc_handler(struct ufs_hba *hba)
  2043. {
  2044. u32 tm_doorbell;
  2045. tm_doorbell = ufshcd_readl(hba, REG_UTP_TASK_REQ_DOOR_BELL);
  2046. hba->tm_condition = tm_doorbell ^ hba->outstanding_tasks;
  2047. wake_up_interruptible(&hba->ufshcd_tm_wait_queue);
  2048. }
  2049. /**
  2050. * ufshcd_sl_intr - Interrupt service routine
  2051. * @hba: per adapter instance
  2052. * @intr_status: contains interrupts generated by the controller
  2053. */
  2054. static void ufshcd_sl_intr(struct ufs_hba *hba, u32 intr_status)
  2055. {
  2056. hba->errors = UFSHCD_ERROR_MASK & intr_status;
  2057. if (hba->errors)
  2058. ufshcd_err_handler(hba);
  2059. if (intr_status & UIC_COMMAND_COMPL)
  2060. ufshcd_uic_cmd_compl(hba);
  2061. if (intr_status & UTP_TASK_REQ_COMPL)
  2062. ufshcd_tmc_handler(hba);
  2063. if (intr_status & UTP_TRANSFER_REQ_COMPL)
  2064. ufshcd_transfer_req_compl(hba);
  2065. }
  2066. /**
  2067. * ufshcd_intr - Main interrupt service routine
  2068. * @irq: irq number
  2069. * @__hba: pointer to adapter instance
  2070. *
  2071. * Returns IRQ_HANDLED - If interrupt is valid
  2072. * IRQ_NONE - If invalid interrupt
  2073. */
  2074. static irqreturn_t ufshcd_intr(int irq, void *__hba)
  2075. {
  2076. u32 intr_status;
  2077. irqreturn_t retval = IRQ_NONE;
  2078. struct ufs_hba *hba = __hba;
  2079. spin_lock(hba->host->host_lock);
  2080. intr_status = ufshcd_readl(hba, REG_INTERRUPT_STATUS);
  2081. if (intr_status) {
  2082. ufshcd_writel(hba, intr_status, REG_INTERRUPT_STATUS);
  2083. ufshcd_sl_intr(hba, intr_status);
  2084. retval = IRQ_HANDLED;
  2085. }
  2086. spin_unlock(hba->host->host_lock);
  2087. return retval;
  2088. }
  2089. /**
  2090. * ufshcd_issue_tm_cmd - issues task management commands to controller
  2091. * @hba: per adapter instance
  2092. * @lrbp: pointer to local reference block
  2093. *
  2094. * Returns SUCCESS/FAILED
  2095. */
  2096. static int
  2097. ufshcd_issue_tm_cmd(struct ufs_hba *hba,
  2098. struct ufshcd_lrb *lrbp,
  2099. u8 tm_function)
  2100. {
  2101. struct utp_task_req_desc *task_req_descp;
  2102. struct utp_upiu_task_req *task_req_upiup;
  2103. struct Scsi_Host *host;
  2104. unsigned long flags;
  2105. int free_slot = 0;
  2106. int err;
  2107. host = hba->host;
  2108. spin_lock_irqsave(host->host_lock, flags);
  2109. /* If task management queue is full */
  2110. free_slot = ufshcd_get_tm_free_slot(hba);
  2111. if (free_slot >= hba->nutmrs) {
  2112. spin_unlock_irqrestore(host->host_lock, flags);
  2113. dev_err(hba->dev, "Task management queue full\n");
  2114. err = FAILED;
  2115. goto out;
  2116. }
  2117. task_req_descp = hba->utmrdl_base_addr;
  2118. task_req_descp += free_slot;
  2119. /* Configure task request descriptor */
  2120. task_req_descp->header.dword_0 = cpu_to_le32(UTP_REQ_DESC_INT_CMD);
  2121. task_req_descp->header.dword_2 =
  2122. cpu_to_le32(OCS_INVALID_COMMAND_STATUS);
  2123. /* Configure task request UPIU */
  2124. task_req_upiup =
  2125. (struct utp_upiu_task_req *) task_req_descp->task_req_upiu;
  2126. task_req_upiup->header.dword_0 =
  2127. UPIU_HEADER_DWORD(UPIU_TRANSACTION_TASK_REQ, 0,
  2128. lrbp->lun, lrbp->task_tag);
  2129. task_req_upiup->header.dword_1 =
  2130. UPIU_HEADER_DWORD(0, tm_function, 0, 0);
  2131. task_req_upiup->input_param1 = lrbp->lun;
  2132. task_req_upiup->input_param1 =
  2133. cpu_to_be32(task_req_upiup->input_param1);
  2134. task_req_upiup->input_param2 = lrbp->task_tag;
  2135. task_req_upiup->input_param2 =
  2136. cpu_to_be32(task_req_upiup->input_param2);
  2137. /* send command to the controller */
  2138. __set_bit(free_slot, &hba->outstanding_tasks);
  2139. ufshcd_writel(hba, 1 << free_slot, REG_UTP_TASK_REQ_DOOR_BELL);
  2140. spin_unlock_irqrestore(host->host_lock, flags);
  2141. /* wait until the task management command is completed */
  2142. err =
  2143. wait_event_interruptible_timeout(hba->ufshcd_tm_wait_queue,
  2144. (test_bit(free_slot,
  2145. &hba->tm_condition) != 0),
  2146. 60 * HZ);
  2147. if (!err) {
  2148. dev_err(hba->dev,
  2149. "Task management command timed-out\n");
  2150. err = FAILED;
  2151. goto out;
  2152. }
  2153. clear_bit(free_slot, &hba->tm_condition);
  2154. err = ufshcd_task_req_compl(hba, free_slot);
  2155. out:
  2156. return err;
  2157. }
  2158. /**
  2159. * ufshcd_device_reset - reset device and abort all the pending commands
  2160. * @cmd: SCSI command pointer
  2161. *
  2162. * Returns SUCCESS/FAILED
  2163. */
  2164. static int ufshcd_device_reset(struct scsi_cmnd *cmd)
  2165. {
  2166. struct Scsi_Host *host;
  2167. struct ufs_hba *hba;
  2168. unsigned int tag;
  2169. u32 pos;
  2170. int err;
  2171. host = cmd->device->host;
  2172. hba = shost_priv(host);
  2173. tag = cmd->request->tag;
  2174. err = ufshcd_issue_tm_cmd(hba, &hba->lrb[tag], UFS_LOGICAL_RESET);
  2175. if (err == FAILED)
  2176. goto out;
  2177. for (pos = 0; pos < hba->nutrs; pos++) {
  2178. if (test_bit(pos, &hba->outstanding_reqs) &&
  2179. (hba->lrb[tag].lun == hba->lrb[pos].lun)) {
  2180. /* clear the respective UTRLCLR register bit */
  2181. ufshcd_utrl_clear(hba, pos);
  2182. clear_bit(pos, &hba->outstanding_reqs);
  2183. if (hba->lrb[pos].cmd) {
  2184. scsi_dma_unmap(hba->lrb[pos].cmd);
  2185. hba->lrb[pos].cmd->result =
  2186. DID_ABORT << 16;
  2187. hba->lrb[pos].cmd->scsi_done(cmd);
  2188. hba->lrb[pos].cmd = NULL;
  2189. clear_bit_unlock(pos, &hba->lrb_in_use);
  2190. wake_up(&hba->dev_cmd.tag_wq);
  2191. }
  2192. }
  2193. } /* end of for */
  2194. out:
  2195. return err;
  2196. }
  2197. /**
  2198. * ufshcd_host_reset - Main reset function registered with scsi layer
  2199. * @cmd: SCSI command pointer
  2200. *
  2201. * Returns SUCCESS/FAILED
  2202. */
  2203. static int ufshcd_host_reset(struct scsi_cmnd *cmd)
  2204. {
  2205. struct ufs_hba *hba;
  2206. hba = shost_priv(cmd->device->host);
  2207. if (hba->ufshcd_state == UFSHCD_STATE_RESET)
  2208. return SUCCESS;
  2209. return ufshcd_do_reset(hba);
  2210. }
  2211. /**
  2212. * ufshcd_abort - abort a specific command
  2213. * @cmd: SCSI command pointer
  2214. *
  2215. * Returns SUCCESS/FAILED
  2216. */
  2217. static int ufshcd_abort(struct scsi_cmnd *cmd)
  2218. {
  2219. struct Scsi_Host *host;
  2220. struct ufs_hba *hba;
  2221. unsigned long flags;
  2222. unsigned int tag;
  2223. int err;
  2224. host = cmd->device->host;
  2225. hba = shost_priv(host);
  2226. tag = cmd->request->tag;
  2227. spin_lock_irqsave(host->host_lock, flags);
  2228. /* check if command is still pending */
  2229. if (!(test_bit(tag, &hba->outstanding_reqs))) {
  2230. err = FAILED;
  2231. spin_unlock_irqrestore(host->host_lock, flags);
  2232. goto out;
  2233. }
  2234. spin_unlock_irqrestore(host->host_lock, flags);
  2235. err = ufshcd_issue_tm_cmd(hba, &hba->lrb[tag], UFS_ABORT_TASK);
  2236. if (err == FAILED)
  2237. goto out;
  2238. scsi_dma_unmap(cmd);
  2239. spin_lock_irqsave(host->host_lock, flags);
  2240. /* clear the respective UTRLCLR register bit */
  2241. ufshcd_utrl_clear(hba, tag);
  2242. __clear_bit(tag, &hba->outstanding_reqs);
  2243. hba->lrb[tag].cmd = NULL;
  2244. spin_unlock_irqrestore(host->host_lock, flags);
  2245. clear_bit_unlock(tag, &hba->lrb_in_use);
  2246. wake_up(&hba->dev_cmd.tag_wq);
  2247. out:
  2248. return err;
  2249. }
  2250. /**
  2251. * ufshcd_async_scan - asynchronous execution for link startup
  2252. * @data: data pointer to pass to this function
  2253. * @cookie: cookie data
  2254. */
  2255. static void ufshcd_async_scan(void *data, async_cookie_t cookie)
  2256. {
  2257. struct ufs_hba *hba = (struct ufs_hba *)data;
  2258. int ret;
  2259. ret = ufshcd_link_startup(hba);
  2260. if (ret)
  2261. goto out;
  2262. ret = ufshcd_verify_dev_init(hba);
  2263. if (ret)
  2264. goto out;
  2265. ret = ufshcd_complete_dev_init(hba);
  2266. if (ret)
  2267. goto out;
  2268. ufshcd_force_reset_auto_bkops(hba);
  2269. scsi_scan_host(hba->host);
  2270. pm_runtime_put_sync(hba->dev);
  2271. out:
  2272. return;
  2273. }
  2274. static struct scsi_host_template ufshcd_driver_template = {
  2275. .module = THIS_MODULE,
  2276. .name = UFSHCD,
  2277. .proc_name = UFSHCD,
  2278. .queuecommand = ufshcd_queuecommand,
  2279. .slave_alloc = ufshcd_slave_alloc,
  2280. .slave_destroy = ufshcd_slave_destroy,
  2281. .eh_abort_handler = ufshcd_abort,
  2282. .eh_device_reset_handler = ufshcd_device_reset,
  2283. .eh_host_reset_handler = ufshcd_host_reset,
  2284. .this_id = -1,
  2285. .sg_tablesize = SG_ALL,
  2286. .cmd_per_lun = UFSHCD_CMD_PER_LUN,
  2287. .can_queue = UFSHCD_CAN_QUEUE,
  2288. };
  2289. /**
  2290. * ufshcd_suspend - suspend power management function
  2291. * @hba: per adapter instance
  2292. * @state: power state
  2293. *
  2294. * Returns -ENOSYS
  2295. */
  2296. int ufshcd_suspend(struct ufs_hba *hba, pm_message_t state)
  2297. {
  2298. /*
  2299. * TODO:
  2300. * 1. Block SCSI requests from SCSI midlayer
  2301. * 2. Change the internal driver state to non operational
  2302. * 3. Set UTRLRSR and UTMRLRSR bits to zero
  2303. * 4. Wait until outstanding commands are completed
  2304. * 5. Set HCE to zero to send the UFS host controller to reset state
  2305. */
  2306. return -ENOSYS;
  2307. }
  2308. EXPORT_SYMBOL_GPL(ufshcd_suspend);
  2309. /**
  2310. * ufshcd_resume - resume power management function
  2311. * @hba: per adapter instance
  2312. *
  2313. * Returns -ENOSYS
  2314. */
  2315. int ufshcd_resume(struct ufs_hba *hba)
  2316. {
  2317. /*
  2318. * TODO:
  2319. * 1. Set HCE to 1, to start the UFS host controller
  2320. * initialization process
  2321. * 2. Set UTRLRSR and UTMRLRSR bits to 1
  2322. * 3. Change the internal driver state to operational
  2323. * 4. Unblock SCSI requests from SCSI midlayer
  2324. */
  2325. return -ENOSYS;
  2326. }
  2327. EXPORT_SYMBOL_GPL(ufshcd_resume);
  2328. int ufshcd_runtime_suspend(struct ufs_hba *hba)
  2329. {
  2330. if (!hba)
  2331. return 0;
  2332. /*
  2333. * The device is idle with no requests in the queue,
  2334. * allow background operations.
  2335. */
  2336. return ufshcd_enable_auto_bkops(hba);
  2337. }
  2338. EXPORT_SYMBOL(ufshcd_runtime_suspend);
  2339. int ufshcd_runtime_resume(struct ufs_hba *hba)
  2340. {
  2341. if (!hba)
  2342. return 0;
  2343. return ufshcd_disable_auto_bkops(hba);
  2344. }
  2345. EXPORT_SYMBOL(ufshcd_runtime_resume);
  2346. int ufshcd_runtime_idle(struct ufs_hba *hba)
  2347. {
  2348. return 0;
  2349. }
  2350. EXPORT_SYMBOL(ufshcd_runtime_idle);
  2351. /**
  2352. * ufshcd_remove - de-allocate SCSI host and host memory space
  2353. * data structure memory
  2354. * @hba - per adapter instance
  2355. */
  2356. void ufshcd_remove(struct ufs_hba *hba)
  2357. {
  2358. scsi_remove_host(hba->host);
  2359. /* disable interrupts */
  2360. ufshcd_disable_intr(hba, hba->intr_mask);
  2361. ufshcd_hba_stop(hba);
  2362. scsi_host_put(hba->host);
  2363. }
  2364. EXPORT_SYMBOL_GPL(ufshcd_remove);
  2365. /**
  2366. * ufshcd_init - Driver initialization routine
  2367. * @dev: pointer to device handle
  2368. * @hba_handle: driver private handle
  2369. * @mmio_base: base register address
  2370. * @irq: Interrupt line of device
  2371. * Returns 0 on success, non-zero value on failure
  2372. */
  2373. int ufshcd_init(struct device *dev, struct ufs_hba **hba_handle,
  2374. void __iomem *mmio_base, unsigned int irq)
  2375. {
  2376. struct Scsi_Host *host;
  2377. struct ufs_hba *hba;
  2378. int err;
  2379. if (!dev) {
  2380. dev_err(dev,
  2381. "Invalid memory reference for dev is NULL\n");
  2382. err = -ENODEV;
  2383. goto out_error;
  2384. }
  2385. if (!mmio_base) {
  2386. dev_err(dev,
  2387. "Invalid memory reference for mmio_base is NULL\n");
  2388. err = -ENODEV;
  2389. goto out_error;
  2390. }
  2391. host = scsi_host_alloc(&ufshcd_driver_template,
  2392. sizeof(struct ufs_hba));
  2393. if (!host) {
  2394. dev_err(dev, "scsi_host_alloc failed\n");
  2395. err = -ENOMEM;
  2396. goto out_error;
  2397. }
  2398. hba = shost_priv(host);
  2399. hba->host = host;
  2400. hba->dev = dev;
  2401. hba->mmio_base = mmio_base;
  2402. hba->irq = irq;
  2403. /* Read capabilities registers */
  2404. ufshcd_hba_capabilities(hba);
  2405. /* Get UFS version supported by the controller */
  2406. hba->ufs_version = ufshcd_get_ufs_version(hba);
  2407. /* Get Interrupt bit mask per version */
  2408. hba->intr_mask = ufshcd_get_intr_mask(hba);
  2409. /* Allocate memory for host memory space */
  2410. err = ufshcd_memory_alloc(hba);
  2411. if (err) {
  2412. dev_err(hba->dev, "Memory allocation failed\n");
  2413. goto out_disable;
  2414. }
  2415. /* Configure LRB */
  2416. ufshcd_host_memory_configure(hba);
  2417. host->can_queue = hba->nutrs;
  2418. host->cmd_per_lun = hba->nutrs;
  2419. host->max_id = UFSHCD_MAX_ID;
  2420. host->max_lun = UFSHCD_MAX_LUNS;
  2421. host->max_channel = UFSHCD_MAX_CHANNEL;
  2422. host->unique_id = host->host_no;
  2423. host->max_cmd_len = MAX_CDB_SIZE;
  2424. /* Initailize wait queue for task management */
  2425. init_waitqueue_head(&hba->ufshcd_tm_wait_queue);
  2426. /* Initialize work queues */
  2427. INIT_WORK(&hba->feh_workq, ufshcd_fatal_err_handler);
  2428. INIT_WORK(&hba->eeh_work, ufshcd_exception_event_handler);
  2429. /* Initialize UIC command mutex */
  2430. mutex_init(&hba->uic_cmd_mutex);
  2431. /* Initialize mutex for device management commands */
  2432. mutex_init(&hba->dev_cmd.lock);
  2433. /* Initialize device management tag acquire wait queue */
  2434. init_waitqueue_head(&hba->dev_cmd.tag_wq);
  2435. /* IRQ registration */
  2436. err = devm_request_irq(dev, irq, ufshcd_intr, IRQF_SHARED, UFSHCD, hba);
  2437. if (err) {
  2438. dev_err(hba->dev, "request irq failed\n");
  2439. goto out_disable;
  2440. }
  2441. /* Enable SCSI tag mapping */
  2442. err = scsi_init_shared_tag_map(host, host->can_queue);
  2443. if (err) {
  2444. dev_err(hba->dev, "init shared queue failed\n");
  2445. goto out_disable;
  2446. }
  2447. err = scsi_add_host(host, hba->dev);
  2448. if (err) {
  2449. dev_err(hba->dev, "scsi_add_host failed\n");
  2450. goto out_disable;
  2451. }
  2452. /* Host controller enable */
  2453. err = ufshcd_hba_enable(hba);
  2454. if (err) {
  2455. dev_err(hba->dev, "Host controller enable failed\n");
  2456. goto out_remove_scsi_host;
  2457. }
  2458. *hba_handle = hba;
  2459. /* Hold auto suspend until async scan completes */
  2460. pm_runtime_get_sync(dev);
  2461. async_schedule(ufshcd_async_scan, hba);
  2462. return 0;
  2463. out_remove_scsi_host:
  2464. scsi_remove_host(hba->host);
  2465. out_disable:
  2466. scsi_host_put(host);
  2467. out_error:
  2468. return err;
  2469. }
  2470. EXPORT_SYMBOL_GPL(ufshcd_init);
  2471. MODULE_AUTHOR("Santosh Yaragnavi <santosh.sy@samsung.com>");
  2472. MODULE_AUTHOR("Vinayak Holikatti <h.vinayak@samsung.com>");
  2473. MODULE_DESCRIPTION("Generic UFS host controller driver Core");
  2474. MODULE_LICENSE("GPL");
  2475. MODULE_VERSION(UFSHCD_DRIVER_VERSION);