ufshcd.c 69 KB

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