ufshcd.c 70 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047204820492050205120522053205420552056205720582059206020612062206320642065206620672068206920702071207220732074207520762077207820792080208120822083208420852086208720882089209020912092209320942095209620972098209921002101210221032104210521062107210821092110211121122113211421152116211721182119212021212122212321242125212621272128212921302131213221332134213521362137213821392140214121422143214421452146214721482149215021512152215321542155215621572158215921602161216221632164216521662167216821692170217121722173217421752176217721782179218021812182218321842185218621872188218921902191219221932194219521962197219821992200220122022203220422052206220722082209221022112212221322142215221622172218221922202221222222232224222522262227222822292230223122322233223422352236223722382239224022412242224322442245224622472248224922502251225222532254225522562257225822592260226122622263226422652266226722682269227022712272227322742275227622772278227922802281228222832284228522862287228822892290229122922293229422952296229722982299230023012302230323042305230623072308230923102311231223132314231523162317231823192320232123222323232423252326232723282329233023312332233323342335233623372338233923402341234223432344234523462347234823492350235123522353235423552356235723582359236023612362236323642365236623672368236923702371237223732374237523762377237823792380238123822383238423852386238723882389239023912392239323942395239623972398239924002401240224032404240524062407240824092410241124122413241424152416241724182419242024212422242324242425242624272428242924302431243224332434243524362437243824392440244124422443244424452446244724482449245024512452245324542455245624572458245924602461246224632464246524662467246824692470247124722473247424752476247724782479248024812482248324842485248624872488248924902491249224932494249524962497249824992500250125022503250425052506250725082509251025112512251325142515251625172518251925202521252225232524252525262527252825292530253125322533253425352536253725382539254025412542254325442545254625472548254925502551255225532554255525562557255825592560256125622563256425652566256725682569257025712572257325742575257625772578257925802581258225832584258525862587258825892590259125922593259425952596259725982599260026012602260326042605260626072608260926102611261226132614261526162617261826192620262126222623262426252626262726282629263026312632263326342635263626372638263926402641264226432644264526462647264826492650265126522653265426552656265726582659266026612662266326642665266626672668266926702671267226732674267526762677267826792680268126822683268426852686268726882689269026912692269326942695269626972698269927002701270227032704270527062707270827092710271127122713271427152716271727182719272027212722272327242725272627272728272927302731273227332734273527362737273827392740274127422743274427452746274727482749
  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. pm_runtime_get_sync(hba->dev);
  1906. err = ufshcd_get_ee_status(hba, &status);
  1907. if (err) {
  1908. dev_err(hba->dev, "%s: failed to get exception status %d\n",
  1909. __func__, err);
  1910. goto out;
  1911. }
  1912. status &= hba->ee_ctrl_mask;
  1913. if (status & MASK_EE_URGENT_BKOPS) {
  1914. err = ufshcd_urgent_bkops(hba);
  1915. if (err)
  1916. dev_err(hba->dev, "%s: failed to handle urgent bkops %d\n",
  1917. __func__, err);
  1918. }
  1919. out:
  1920. pm_runtime_put_sync(hba->dev);
  1921. return;
  1922. }
  1923. /**
  1924. * ufshcd_fatal_err_handler - handle fatal errors
  1925. * @hba: per adapter instance
  1926. */
  1927. static void ufshcd_fatal_err_handler(struct work_struct *work)
  1928. {
  1929. struct ufs_hba *hba;
  1930. hba = container_of(work, struct ufs_hba, feh_workq);
  1931. pm_runtime_get_sync(hba->dev);
  1932. /* check if reset is already in progress */
  1933. if (hba->ufshcd_state != UFSHCD_STATE_RESET)
  1934. ufshcd_do_reset(hba);
  1935. pm_runtime_put_sync(hba->dev);
  1936. }
  1937. /**
  1938. * ufshcd_err_handler - Check for fatal errors
  1939. * @work: pointer to a work queue structure
  1940. */
  1941. static void ufshcd_err_handler(struct ufs_hba *hba)
  1942. {
  1943. u32 reg;
  1944. if (hba->errors & INT_FATAL_ERRORS)
  1945. goto fatal_eh;
  1946. if (hba->errors & UIC_ERROR) {
  1947. reg = ufshcd_readl(hba, REG_UIC_ERROR_CODE_DATA_LINK_LAYER);
  1948. if (reg & UIC_DATA_LINK_LAYER_ERROR_PA_INIT)
  1949. goto fatal_eh;
  1950. }
  1951. return;
  1952. fatal_eh:
  1953. hba->ufshcd_state = UFSHCD_STATE_ERROR;
  1954. schedule_work(&hba->feh_workq);
  1955. }
  1956. /**
  1957. * ufshcd_tmc_handler - handle task management function completion
  1958. * @hba: per adapter instance
  1959. */
  1960. static void ufshcd_tmc_handler(struct ufs_hba *hba)
  1961. {
  1962. u32 tm_doorbell;
  1963. tm_doorbell = ufshcd_readl(hba, REG_UTP_TASK_REQ_DOOR_BELL);
  1964. hba->tm_condition = tm_doorbell ^ hba->outstanding_tasks;
  1965. wake_up_interruptible(&hba->ufshcd_tm_wait_queue);
  1966. }
  1967. /**
  1968. * ufshcd_sl_intr - Interrupt service routine
  1969. * @hba: per adapter instance
  1970. * @intr_status: contains interrupts generated by the controller
  1971. */
  1972. static void ufshcd_sl_intr(struct ufs_hba *hba, u32 intr_status)
  1973. {
  1974. hba->errors = UFSHCD_ERROR_MASK & intr_status;
  1975. if (hba->errors)
  1976. ufshcd_err_handler(hba);
  1977. if (intr_status & UIC_COMMAND_COMPL)
  1978. ufshcd_uic_cmd_compl(hba);
  1979. if (intr_status & UTP_TASK_REQ_COMPL)
  1980. ufshcd_tmc_handler(hba);
  1981. if (intr_status & UTP_TRANSFER_REQ_COMPL)
  1982. ufshcd_transfer_req_compl(hba);
  1983. }
  1984. /**
  1985. * ufshcd_intr - Main interrupt service routine
  1986. * @irq: irq number
  1987. * @__hba: pointer to adapter instance
  1988. *
  1989. * Returns IRQ_HANDLED - If interrupt is valid
  1990. * IRQ_NONE - If invalid interrupt
  1991. */
  1992. static irqreturn_t ufshcd_intr(int irq, void *__hba)
  1993. {
  1994. u32 intr_status;
  1995. irqreturn_t retval = IRQ_NONE;
  1996. struct ufs_hba *hba = __hba;
  1997. spin_lock(hba->host->host_lock);
  1998. intr_status = ufshcd_readl(hba, REG_INTERRUPT_STATUS);
  1999. if (intr_status) {
  2000. ufshcd_writel(hba, intr_status, REG_INTERRUPT_STATUS);
  2001. ufshcd_sl_intr(hba, intr_status);
  2002. retval = IRQ_HANDLED;
  2003. }
  2004. spin_unlock(hba->host->host_lock);
  2005. return retval;
  2006. }
  2007. /**
  2008. * ufshcd_issue_tm_cmd - issues task management commands to controller
  2009. * @hba: per adapter instance
  2010. * @lrbp: pointer to local reference block
  2011. *
  2012. * Returns SUCCESS/FAILED
  2013. */
  2014. static int
  2015. ufshcd_issue_tm_cmd(struct ufs_hba *hba,
  2016. struct ufshcd_lrb *lrbp,
  2017. u8 tm_function)
  2018. {
  2019. struct utp_task_req_desc *task_req_descp;
  2020. struct utp_upiu_task_req *task_req_upiup;
  2021. struct Scsi_Host *host;
  2022. unsigned long flags;
  2023. int free_slot = 0;
  2024. int err;
  2025. host = hba->host;
  2026. spin_lock_irqsave(host->host_lock, flags);
  2027. /* If task management queue is full */
  2028. free_slot = ufshcd_get_tm_free_slot(hba);
  2029. if (free_slot >= hba->nutmrs) {
  2030. spin_unlock_irqrestore(host->host_lock, flags);
  2031. dev_err(hba->dev, "Task management queue full\n");
  2032. err = FAILED;
  2033. goto out;
  2034. }
  2035. task_req_descp = hba->utmrdl_base_addr;
  2036. task_req_descp += free_slot;
  2037. /* Configure task request descriptor */
  2038. task_req_descp->header.dword_0 = cpu_to_le32(UTP_REQ_DESC_INT_CMD);
  2039. task_req_descp->header.dword_2 =
  2040. cpu_to_le32(OCS_INVALID_COMMAND_STATUS);
  2041. /* Configure task request UPIU */
  2042. task_req_upiup =
  2043. (struct utp_upiu_task_req *) task_req_descp->task_req_upiu;
  2044. task_req_upiup->header.dword_0 =
  2045. UPIU_HEADER_DWORD(UPIU_TRANSACTION_TASK_REQ, 0,
  2046. lrbp->lun, lrbp->task_tag);
  2047. task_req_upiup->header.dword_1 =
  2048. UPIU_HEADER_DWORD(0, tm_function, 0, 0);
  2049. task_req_upiup->input_param1 = lrbp->lun;
  2050. task_req_upiup->input_param1 =
  2051. cpu_to_be32(task_req_upiup->input_param1);
  2052. task_req_upiup->input_param2 = lrbp->task_tag;
  2053. task_req_upiup->input_param2 =
  2054. cpu_to_be32(task_req_upiup->input_param2);
  2055. /* send command to the controller */
  2056. __set_bit(free_slot, &hba->outstanding_tasks);
  2057. ufshcd_writel(hba, 1 << free_slot, REG_UTP_TASK_REQ_DOOR_BELL);
  2058. spin_unlock_irqrestore(host->host_lock, flags);
  2059. /* wait until the task management command is completed */
  2060. err =
  2061. wait_event_interruptible_timeout(hba->ufshcd_tm_wait_queue,
  2062. (test_bit(free_slot,
  2063. &hba->tm_condition) != 0),
  2064. 60 * HZ);
  2065. if (!err) {
  2066. dev_err(hba->dev,
  2067. "Task management command timed-out\n");
  2068. err = FAILED;
  2069. goto out;
  2070. }
  2071. clear_bit(free_slot, &hba->tm_condition);
  2072. err = ufshcd_task_req_compl(hba, free_slot);
  2073. out:
  2074. return err;
  2075. }
  2076. /**
  2077. * ufshcd_device_reset - reset device and abort all the pending commands
  2078. * @cmd: SCSI command pointer
  2079. *
  2080. * Returns SUCCESS/FAILED
  2081. */
  2082. static int ufshcd_device_reset(struct scsi_cmnd *cmd)
  2083. {
  2084. struct Scsi_Host *host;
  2085. struct ufs_hba *hba;
  2086. unsigned int tag;
  2087. u32 pos;
  2088. int err;
  2089. host = cmd->device->host;
  2090. hba = shost_priv(host);
  2091. tag = cmd->request->tag;
  2092. err = ufshcd_issue_tm_cmd(hba, &hba->lrb[tag], UFS_LOGICAL_RESET);
  2093. if (err == FAILED)
  2094. goto out;
  2095. for (pos = 0; pos < hba->nutrs; pos++) {
  2096. if (test_bit(pos, &hba->outstanding_reqs) &&
  2097. (hba->lrb[tag].lun == hba->lrb[pos].lun)) {
  2098. /* clear the respective UTRLCLR register bit */
  2099. ufshcd_utrl_clear(hba, pos);
  2100. clear_bit(pos, &hba->outstanding_reqs);
  2101. if (hba->lrb[pos].cmd) {
  2102. scsi_dma_unmap(hba->lrb[pos].cmd);
  2103. hba->lrb[pos].cmd->result =
  2104. DID_ABORT << 16;
  2105. hba->lrb[pos].cmd->scsi_done(cmd);
  2106. hba->lrb[pos].cmd = NULL;
  2107. clear_bit_unlock(pos, &hba->lrb_in_use);
  2108. wake_up(&hba->dev_cmd.tag_wq);
  2109. }
  2110. }
  2111. } /* end of for */
  2112. out:
  2113. return err;
  2114. }
  2115. /**
  2116. * ufshcd_host_reset - Main reset function registered with scsi layer
  2117. * @cmd: SCSI command pointer
  2118. *
  2119. * Returns SUCCESS/FAILED
  2120. */
  2121. static int ufshcd_host_reset(struct scsi_cmnd *cmd)
  2122. {
  2123. struct ufs_hba *hba;
  2124. hba = shost_priv(cmd->device->host);
  2125. if (hba->ufshcd_state == UFSHCD_STATE_RESET)
  2126. return SUCCESS;
  2127. return ufshcd_do_reset(hba);
  2128. }
  2129. /**
  2130. * ufshcd_abort - abort a specific command
  2131. * @cmd: SCSI command pointer
  2132. *
  2133. * Returns SUCCESS/FAILED
  2134. */
  2135. static int ufshcd_abort(struct scsi_cmnd *cmd)
  2136. {
  2137. struct Scsi_Host *host;
  2138. struct ufs_hba *hba;
  2139. unsigned long flags;
  2140. unsigned int tag;
  2141. int err;
  2142. host = cmd->device->host;
  2143. hba = shost_priv(host);
  2144. tag = cmd->request->tag;
  2145. spin_lock_irqsave(host->host_lock, flags);
  2146. /* check if command is still pending */
  2147. if (!(test_bit(tag, &hba->outstanding_reqs))) {
  2148. err = FAILED;
  2149. spin_unlock_irqrestore(host->host_lock, flags);
  2150. goto out;
  2151. }
  2152. spin_unlock_irqrestore(host->host_lock, flags);
  2153. err = ufshcd_issue_tm_cmd(hba, &hba->lrb[tag], UFS_ABORT_TASK);
  2154. if (err == FAILED)
  2155. goto out;
  2156. scsi_dma_unmap(cmd);
  2157. spin_lock_irqsave(host->host_lock, flags);
  2158. /* clear the respective UTRLCLR register bit */
  2159. ufshcd_utrl_clear(hba, tag);
  2160. __clear_bit(tag, &hba->outstanding_reqs);
  2161. hba->lrb[tag].cmd = NULL;
  2162. spin_unlock_irqrestore(host->host_lock, flags);
  2163. clear_bit_unlock(tag, &hba->lrb_in_use);
  2164. wake_up(&hba->dev_cmd.tag_wq);
  2165. out:
  2166. return err;
  2167. }
  2168. /**
  2169. * ufshcd_async_scan - asynchronous execution for link startup
  2170. * @data: data pointer to pass to this function
  2171. * @cookie: cookie data
  2172. */
  2173. static void ufshcd_async_scan(void *data, async_cookie_t cookie)
  2174. {
  2175. struct ufs_hba *hba = (struct ufs_hba *)data;
  2176. int ret;
  2177. ret = ufshcd_link_startup(hba);
  2178. if (ret)
  2179. goto out;
  2180. ret = ufshcd_verify_dev_init(hba);
  2181. if (ret)
  2182. goto out;
  2183. ret = ufshcd_complete_dev_init(hba);
  2184. if (ret)
  2185. goto out;
  2186. ufshcd_force_reset_auto_bkops(hba);
  2187. scsi_scan_host(hba->host);
  2188. pm_runtime_put_sync(hba->dev);
  2189. out:
  2190. return;
  2191. }
  2192. static struct scsi_host_template ufshcd_driver_template = {
  2193. .module = THIS_MODULE,
  2194. .name = UFSHCD,
  2195. .proc_name = UFSHCD,
  2196. .queuecommand = ufshcd_queuecommand,
  2197. .slave_alloc = ufshcd_slave_alloc,
  2198. .slave_destroy = ufshcd_slave_destroy,
  2199. .eh_abort_handler = ufshcd_abort,
  2200. .eh_device_reset_handler = ufshcd_device_reset,
  2201. .eh_host_reset_handler = ufshcd_host_reset,
  2202. .this_id = -1,
  2203. .sg_tablesize = SG_ALL,
  2204. .cmd_per_lun = UFSHCD_CMD_PER_LUN,
  2205. .can_queue = UFSHCD_CAN_QUEUE,
  2206. };
  2207. /**
  2208. * ufshcd_suspend - suspend power management function
  2209. * @hba: per adapter instance
  2210. * @state: power state
  2211. *
  2212. * Returns -ENOSYS
  2213. */
  2214. int ufshcd_suspend(struct ufs_hba *hba, pm_message_t state)
  2215. {
  2216. /*
  2217. * TODO:
  2218. * 1. Block SCSI requests from SCSI midlayer
  2219. * 2. Change the internal driver state to non operational
  2220. * 3. Set UTRLRSR and UTMRLRSR bits to zero
  2221. * 4. Wait until outstanding commands are completed
  2222. * 5. Set HCE to zero to send the UFS host controller to reset state
  2223. */
  2224. return -ENOSYS;
  2225. }
  2226. EXPORT_SYMBOL_GPL(ufshcd_suspend);
  2227. /**
  2228. * ufshcd_resume - resume power management function
  2229. * @hba: per adapter instance
  2230. *
  2231. * Returns -ENOSYS
  2232. */
  2233. int ufshcd_resume(struct ufs_hba *hba)
  2234. {
  2235. /*
  2236. * TODO:
  2237. * 1. Set HCE to 1, to start the UFS host controller
  2238. * initialization process
  2239. * 2. Set UTRLRSR and UTMRLRSR bits to 1
  2240. * 3. Change the internal driver state to operational
  2241. * 4. Unblock SCSI requests from SCSI midlayer
  2242. */
  2243. return -ENOSYS;
  2244. }
  2245. EXPORT_SYMBOL_GPL(ufshcd_resume);
  2246. int ufshcd_runtime_suspend(struct ufs_hba *hba)
  2247. {
  2248. if (!hba)
  2249. return 0;
  2250. /*
  2251. * The device is idle with no requests in the queue,
  2252. * allow background operations.
  2253. */
  2254. return ufshcd_enable_auto_bkops(hba);
  2255. }
  2256. EXPORT_SYMBOL(ufshcd_runtime_suspend);
  2257. int ufshcd_runtime_resume(struct ufs_hba *hba)
  2258. {
  2259. if (!hba)
  2260. return 0;
  2261. return ufshcd_disable_auto_bkops(hba);
  2262. }
  2263. EXPORT_SYMBOL(ufshcd_runtime_resume);
  2264. int ufshcd_runtime_idle(struct ufs_hba *hba)
  2265. {
  2266. return 0;
  2267. }
  2268. EXPORT_SYMBOL(ufshcd_runtime_idle);
  2269. /**
  2270. * ufshcd_remove - de-allocate SCSI host and host memory space
  2271. * data structure memory
  2272. * @hba - per adapter instance
  2273. */
  2274. void ufshcd_remove(struct ufs_hba *hba)
  2275. {
  2276. scsi_remove_host(hba->host);
  2277. /* disable interrupts */
  2278. ufshcd_disable_intr(hba, hba->intr_mask);
  2279. ufshcd_hba_stop(hba);
  2280. scsi_host_put(hba->host);
  2281. }
  2282. EXPORT_SYMBOL_GPL(ufshcd_remove);
  2283. /**
  2284. * ufshcd_init - Driver initialization routine
  2285. * @dev: pointer to device handle
  2286. * @hba_handle: driver private handle
  2287. * @mmio_base: base register address
  2288. * @irq: Interrupt line of device
  2289. * Returns 0 on success, non-zero value on failure
  2290. */
  2291. int ufshcd_init(struct device *dev, struct ufs_hba **hba_handle,
  2292. void __iomem *mmio_base, unsigned int irq)
  2293. {
  2294. struct Scsi_Host *host;
  2295. struct ufs_hba *hba;
  2296. int err;
  2297. if (!dev) {
  2298. dev_err(dev,
  2299. "Invalid memory reference for dev is NULL\n");
  2300. err = -ENODEV;
  2301. goto out_error;
  2302. }
  2303. if (!mmio_base) {
  2304. dev_err(dev,
  2305. "Invalid memory reference for mmio_base is NULL\n");
  2306. err = -ENODEV;
  2307. goto out_error;
  2308. }
  2309. host = scsi_host_alloc(&ufshcd_driver_template,
  2310. sizeof(struct ufs_hba));
  2311. if (!host) {
  2312. dev_err(dev, "scsi_host_alloc failed\n");
  2313. err = -ENOMEM;
  2314. goto out_error;
  2315. }
  2316. hba = shost_priv(host);
  2317. hba->host = host;
  2318. hba->dev = dev;
  2319. hba->mmio_base = mmio_base;
  2320. hba->irq = irq;
  2321. /* Read capabilities registers */
  2322. ufshcd_hba_capabilities(hba);
  2323. /* Get UFS version supported by the controller */
  2324. hba->ufs_version = ufshcd_get_ufs_version(hba);
  2325. /* Get Interrupt bit mask per version */
  2326. hba->intr_mask = ufshcd_get_intr_mask(hba);
  2327. /* Allocate memory for host memory space */
  2328. err = ufshcd_memory_alloc(hba);
  2329. if (err) {
  2330. dev_err(hba->dev, "Memory allocation failed\n");
  2331. goto out_disable;
  2332. }
  2333. /* Configure LRB */
  2334. ufshcd_host_memory_configure(hba);
  2335. host->can_queue = hba->nutrs;
  2336. host->cmd_per_lun = hba->nutrs;
  2337. host->max_id = UFSHCD_MAX_ID;
  2338. host->max_lun = UFSHCD_MAX_LUNS;
  2339. host->max_channel = UFSHCD_MAX_CHANNEL;
  2340. host->unique_id = host->host_no;
  2341. host->max_cmd_len = MAX_CDB_SIZE;
  2342. /* Initailize wait queue for task management */
  2343. init_waitqueue_head(&hba->ufshcd_tm_wait_queue);
  2344. /* Initialize work queues */
  2345. INIT_WORK(&hba->feh_workq, ufshcd_fatal_err_handler);
  2346. INIT_WORK(&hba->eeh_work, ufshcd_exception_event_handler);
  2347. /* Initialize UIC command mutex */
  2348. mutex_init(&hba->uic_cmd_mutex);
  2349. /* Initialize mutex for device management commands */
  2350. mutex_init(&hba->dev_cmd.lock);
  2351. /* Initialize device management tag acquire wait queue */
  2352. init_waitqueue_head(&hba->dev_cmd.tag_wq);
  2353. /* IRQ registration */
  2354. err = devm_request_irq(dev, irq, ufshcd_intr, IRQF_SHARED, UFSHCD, hba);
  2355. if (err) {
  2356. dev_err(hba->dev, "request irq failed\n");
  2357. goto out_disable;
  2358. }
  2359. /* Enable SCSI tag mapping */
  2360. err = scsi_init_shared_tag_map(host, host->can_queue);
  2361. if (err) {
  2362. dev_err(hba->dev, "init shared queue failed\n");
  2363. goto out_disable;
  2364. }
  2365. err = scsi_add_host(host, hba->dev);
  2366. if (err) {
  2367. dev_err(hba->dev, "scsi_add_host failed\n");
  2368. goto out_disable;
  2369. }
  2370. /* Host controller enable */
  2371. err = ufshcd_hba_enable(hba);
  2372. if (err) {
  2373. dev_err(hba->dev, "Host controller enable failed\n");
  2374. goto out_remove_scsi_host;
  2375. }
  2376. *hba_handle = hba;
  2377. /* Hold auto suspend until async scan completes */
  2378. pm_runtime_get_sync(dev);
  2379. async_schedule(ufshcd_async_scan, hba);
  2380. return 0;
  2381. out_remove_scsi_host:
  2382. scsi_remove_host(hba->host);
  2383. out_disable:
  2384. scsi_host_put(host);
  2385. out_error:
  2386. return err;
  2387. }
  2388. EXPORT_SYMBOL_GPL(ufshcd_init);
  2389. MODULE_AUTHOR("Santosh Yaragnavi <santosh.sy@samsung.com>");
  2390. MODULE_AUTHOR("Vinayak Holikatti <h.vinayak@samsung.com>");
  2391. MODULE_DESCRIPTION("Generic UFS host controller driver Core");
  2392. MODULE_LICENSE("GPL");
  2393. MODULE_VERSION(UFSHCD_DRIVER_VERSION);