ufshcd.c 45 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800
  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. enum {
  43. UFSHCD_MAX_CHANNEL = 0,
  44. UFSHCD_MAX_ID = 1,
  45. UFSHCD_MAX_LUNS = 8,
  46. UFSHCD_CMD_PER_LUN = 32,
  47. UFSHCD_CAN_QUEUE = 32,
  48. };
  49. /* UFSHCD states */
  50. enum {
  51. UFSHCD_STATE_OPERATIONAL,
  52. UFSHCD_STATE_RESET,
  53. UFSHCD_STATE_ERROR,
  54. };
  55. /* Interrupt configuration options */
  56. enum {
  57. UFSHCD_INT_DISABLE,
  58. UFSHCD_INT_ENABLE,
  59. UFSHCD_INT_CLEAR,
  60. };
  61. /* Interrupt aggregation options */
  62. enum {
  63. INT_AGGR_RESET,
  64. INT_AGGR_CONFIG,
  65. };
  66. /**
  67. * ufshcd_get_intr_mask - Get the interrupt bit mask
  68. * @hba - Pointer to adapter instance
  69. *
  70. * Returns interrupt bit mask per version
  71. */
  72. static inline u32 ufshcd_get_intr_mask(struct ufs_hba *hba)
  73. {
  74. if (hba->ufs_version == UFSHCI_VERSION_10)
  75. return INTERRUPT_MASK_ALL_VER_10;
  76. else
  77. return INTERRUPT_MASK_ALL_VER_11;
  78. }
  79. /**
  80. * ufshcd_get_ufs_version - Get the UFS version supported by the HBA
  81. * @hba - Pointer to adapter instance
  82. *
  83. * Returns UFSHCI version supported by the controller
  84. */
  85. static inline u32 ufshcd_get_ufs_version(struct ufs_hba *hba)
  86. {
  87. return ufshcd_readl(hba, REG_UFS_VERSION);
  88. }
  89. /**
  90. * ufshcd_is_device_present - Check if any device connected to
  91. * the host controller
  92. * @reg_hcs - host controller status register value
  93. *
  94. * Returns 1 if device present, 0 if no device detected
  95. */
  96. static inline int ufshcd_is_device_present(u32 reg_hcs)
  97. {
  98. return (DEVICE_PRESENT & reg_hcs) ? 1 : 0;
  99. }
  100. /**
  101. * ufshcd_get_tr_ocs - Get the UTRD Overall Command Status
  102. * @lrb: pointer to local command reference block
  103. *
  104. * This function is used to get the OCS field from UTRD
  105. * Returns the OCS field in the UTRD
  106. */
  107. static inline int ufshcd_get_tr_ocs(struct ufshcd_lrb *lrbp)
  108. {
  109. return lrbp->utr_descriptor_ptr->header.dword_2 & MASK_OCS;
  110. }
  111. /**
  112. * ufshcd_get_tmr_ocs - Get the UTMRD Overall Command Status
  113. * @task_req_descp: pointer to utp_task_req_desc structure
  114. *
  115. * This function is used to get the OCS field from UTMRD
  116. * Returns the OCS field in the UTMRD
  117. */
  118. static inline int
  119. ufshcd_get_tmr_ocs(struct utp_task_req_desc *task_req_descp)
  120. {
  121. return task_req_descp->header.dword_2 & MASK_OCS;
  122. }
  123. /**
  124. * ufshcd_get_tm_free_slot - get a free slot for task management request
  125. * @hba: per adapter instance
  126. *
  127. * Returns maximum number of task management request slots in case of
  128. * task management queue full or returns the free slot number
  129. */
  130. static inline int ufshcd_get_tm_free_slot(struct ufs_hba *hba)
  131. {
  132. return find_first_zero_bit(&hba->outstanding_tasks, hba->nutmrs);
  133. }
  134. /**
  135. * ufshcd_utrl_clear - Clear a bit in UTRLCLR register
  136. * @hba: per adapter instance
  137. * @pos: position of the bit to be cleared
  138. */
  139. static inline void ufshcd_utrl_clear(struct ufs_hba *hba, u32 pos)
  140. {
  141. ufshcd_writel(hba, ~(1 << pos), REG_UTP_TRANSFER_REQ_LIST_CLEAR);
  142. }
  143. /**
  144. * ufshcd_get_lists_status - Check UCRDY, UTRLRDY and UTMRLRDY
  145. * @reg: Register value of host controller status
  146. *
  147. * Returns integer, 0 on Success and positive value if failed
  148. */
  149. static inline int ufshcd_get_lists_status(u32 reg)
  150. {
  151. /*
  152. * The mask 0xFF is for the following HCS register bits
  153. * Bit Description
  154. * 0 Device Present
  155. * 1 UTRLRDY
  156. * 2 UTMRLRDY
  157. * 3 UCRDY
  158. * 4 HEI
  159. * 5 DEI
  160. * 6-7 reserved
  161. */
  162. return (((reg) & (0xFF)) >> 1) ^ (0x07);
  163. }
  164. /**
  165. * ufshcd_get_uic_cmd_result - Get the UIC command result
  166. * @hba: Pointer to adapter instance
  167. *
  168. * This function gets the result of UIC command completion
  169. * Returns 0 on success, non zero value on error
  170. */
  171. static inline int ufshcd_get_uic_cmd_result(struct ufs_hba *hba)
  172. {
  173. return ufshcd_readl(hba, REG_UIC_COMMAND_ARG_2) &
  174. MASK_UIC_COMMAND_RESULT;
  175. }
  176. /**
  177. * ufshcd_is_valid_req_rsp - checks if controller TR response is valid
  178. * @ucd_rsp_ptr: pointer to response UPIU
  179. *
  180. * This function checks the response UPIU for valid transaction type in
  181. * response field
  182. * Returns 0 on success, non-zero on failure
  183. */
  184. static inline int
  185. ufshcd_is_valid_req_rsp(struct utp_upiu_rsp *ucd_rsp_ptr)
  186. {
  187. return ((be32_to_cpu(ucd_rsp_ptr->header.dword_0) >> 24) ==
  188. UPIU_TRANSACTION_RESPONSE) ? 0 : DID_ERROR << 16;
  189. }
  190. /**
  191. * ufshcd_get_rsp_upiu_result - Get the result from response UPIU
  192. * @ucd_rsp_ptr: pointer to response UPIU
  193. *
  194. * This function gets the response status and scsi_status from response UPIU
  195. * Returns the response result code.
  196. */
  197. static inline int
  198. ufshcd_get_rsp_upiu_result(struct utp_upiu_rsp *ucd_rsp_ptr)
  199. {
  200. return be32_to_cpu(ucd_rsp_ptr->header.dword_1) & MASK_RSP_UPIU_RESULT;
  201. }
  202. /**
  203. * ufshcd_config_int_aggr - Configure interrupt aggregation values.
  204. * Currently there is no use case where we want to configure
  205. * interrupt aggregation dynamically. So to configure interrupt
  206. * aggregation, #define INT_AGGR_COUNTER_THRESHOLD_VALUE and
  207. * INT_AGGR_TIMEOUT_VALUE are used.
  208. * @hba: per adapter instance
  209. * @option: Interrupt aggregation option
  210. */
  211. static inline void
  212. ufshcd_config_int_aggr(struct ufs_hba *hba, int option)
  213. {
  214. switch (option) {
  215. case INT_AGGR_RESET:
  216. ufshcd_writel(hba, INT_AGGR_ENABLE |
  217. INT_AGGR_COUNTER_AND_TIMER_RESET,
  218. REG_UTP_TRANSFER_REQ_INT_AGG_CONTROL);
  219. break;
  220. case INT_AGGR_CONFIG:
  221. ufshcd_writel(hba, INT_AGGR_ENABLE | INT_AGGR_PARAM_WRITE |
  222. INT_AGGR_COUNTER_THRESHOLD_VALUE |
  223. INT_AGGR_TIMEOUT_VALUE,
  224. REG_UTP_TRANSFER_REQ_INT_AGG_CONTROL);
  225. break;
  226. }
  227. }
  228. /**
  229. * ufshcd_enable_run_stop_reg - Enable run-stop registers,
  230. * When run-stop registers are set to 1, it indicates the
  231. * host controller that it can process the requests
  232. * @hba: per adapter instance
  233. */
  234. static void ufshcd_enable_run_stop_reg(struct ufs_hba *hba)
  235. {
  236. ufshcd_writel(hba, UTP_TASK_REQ_LIST_RUN_STOP_BIT,
  237. REG_UTP_TASK_REQ_LIST_RUN_STOP);
  238. ufshcd_writel(hba, UTP_TRANSFER_REQ_LIST_RUN_STOP_BIT,
  239. REG_UTP_TRANSFER_REQ_LIST_RUN_STOP);
  240. }
  241. /**
  242. * ufshcd_hba_start - Start controller initialization sequence
  243. * @hba: per adapter instance
  244. */
  245. static inline void ufshcd_hba_start(struct ufs_hba *hba)
  246. {
  247. ufshcd_writel(hba, CONTROLLER_ENABLE, REG_CONTROLLER_ENABLE);
  248. }
  249. /**
  250. * ufshcd_is_hba_active - Get controller state
  251. * @hba: per adapter instance
  252. *
  253. * Returns zero if controller is active, 1 otherwise
  254. */
  255. static inline int ufshcd_is_hba_active(struct ufs_hba *hba)
  256. {
  257. return (ufshcd_readl(hba, REG_CONTROLLER_ENABLE) & 0x1) ? 0 : 1;
  258. }
  259. /**
  260. * ufshcd_send_command - Send SCSI or device management commands
  261. * @hba: per adapter instance
  262. * @task_tag: Task tag of the command
  263. */
  264. static inline
  265. void ufshcd_send_command(struct ufs_hba *hba, unsigned int task_tag)
  266. {
  267. __set_bit(task_tag, &hba->outstanding_reqs);
  268. ufshcd_writel(hba, 1 << task_tag, REG_UTP_TRANSFER_REQ_DOOR_BELL);
  269. }
  270. /**
  271. * ufshcd_copy_sense_data - Copy sense data in case of check condition
  272. * @lrb - pointer to local reference block
  273. */
  274. static inline void ufshcd_copy_sense_data(struct ufshcd_lrb *lrbp)
  275. {
  276. int len;
  277. if (lrbp->sense_buffer) {
  278. len = be16_to_cpu(lrbp->ucd_rsp_ptr->sense_data_len);
  279. memcpy(lrbp->sense_buffer,
  280. lrbp->ucd_rsp_ptr->sense_data,
  281. min_t(int, len, SCSI_SENSE_BUFFERSIZE));
  282. }
  283. }
  284. /**
  285. * ufshcd_hba_capabilities - Read controller capabilities
  286. * @hba: per adapter instance
  287. */
  288. static inline void ufshcd_hba_capabilities(struct ufs_hba *hba)
  289. {
  290. hba->capabilities = ufshcd_readl(hba, REG_CONTROLLER_CAPABILITIES);
  291. /* nutrs and nutmrs are 0 based values */
  292. hba->nutrs = (hba->capabilities & MASK_TRANSFER_REQUESTS_SLOTS) + 1;
  293. hba->nutmrs =
  294. ((hba->capabilities & MASK_TASK_MANAGEMENT_REQUEST_SLOTS) >> 16) + 1;
  295. }
  296. /**
  297. * ufshcd_ready_for_uic_cmd - Check if controller is ready
  298. * to accept UIC commands
  299. * @hba: per adapter instance
  300. * Return true on success, else false
  301. */
  302. static inline bool ufshcd_ready_for_uic_cmd(struct ufs_hba *hba)
  303. {
  304. if (ufshcd_readl(hba, REG_CONTROLLER_STATUS) & UIC_COMMAND_READY)
  305. return true;
  306. else
  307. return false;
  308. }
  309. /**
  310. * ufshcd_dispatch_uic_cmd - Dispatch UIC commands to unipro layers
  311. * @hba: per adapter instance
  312. * @uic_cmd: UIC command
  313. *
  314. * Mutex must be held.
  315. */
  316. static inline void
  317. ufshcd_dispatch_uic_cmd(struct ufs_hba *hba, struct uic_command *uic_cmd)
  318. {
  319. WARN_ON(hba->active_uic_cmd);
  320. hba->active_uic_cmd = uic_cmd;
  321. /* Write Args */
  322. ufshcd_writel(hba, uic_cmd->argument1, REG_UIC_COMMAND_ARG_1);
  323. ufshcd_writel(hba, uic_cmd->argument2, REG_UIC_COMMAND_ARG_2);
  324. ufshcd_writel(hba, uic_cmd->argument3, REG_UIC_COMMAND_ARG_3);
  325. /* Write UIC Cmd */
  326. ufshcd_writel(hba, uic_cmd->command & COMMAND_OPCODE_MASK,
  327. REG_UIC_COMMAND);
  328. }
  329. /**
  330. * ufshcd_wait_for_uic_cmd - Wait complectioin of UIC command
  331. * @hba: per adapter instance
  332. * @uic_command: UIC command
  333. *
  334. * Must be called with mutex held.
  335. * Returns 0 only if success.
  336. */
  337. static int
  338. ufshcd_wait_for_uic_cmd(struct ufs_hba *hba, struct uic_command *uic_cmd)
  339. {
  340. int ret;
  341. unsigned long flags;
  342. if (wait_for_completion_timeout(&uic_cmd->done,
  343. msecs_to_jiffies(UIC_CMD_TIMEOUT)))
  344. ret = uic_cmd->argument2 & MASK_UIC_COMMAND_RESULT;
  345. else
  346. ret = -ETIMEDOUT;
  347. spin_lock_irqsave(hba->host->host_lock, flags);
  348. hba->active_uic_cmd = NULL;
  349. spin_unlock_irqrestore(hba->host->host_lock, flags);
  350. return ret;
  351. }
  352. /**
  353. * __ufshcd_send_uic_cmd - Send UIC commands and retrieve the result
  354. * @hba: per adapter instance
  355. * @uic_cmd: UIC command
  356. *
  357. * Identical to ufshcd_send_uic_cmd() expect mutex. Must be called
  358. * with mutex held.
  359. * Returns 0 only if success.
  360. */
  361. static int
  362. __ufshcd_send_uic_cmd(struct ufs_hba *hba, struct uic_command *uic_cmd)
  363. {
  364. int ret;
  365. unsigned long flags;
  366. if (!ufshcd_ready_for_uic_cmd(hba)) {
  367. dev_err(hba->dev,
  368. "Controller not ready to accept UIC commands\n");
  369. return -EIO;
  370. }
  371. init_completion(&uic_cmd->done);
  372. spin_lock_irqsave(hba->host->host_lock, flags);
  373. ufshcd_dispatch_uic_cmd(hba, uic_cmd);
  374. spin_unlock_irqrestore(hba->host->host_lock, flags);
  375. ret = ufshcd_wait_for_uic_cmd(hba, uic_cmd);
  376. return ret;
  377. }
  378. /**
  379. * ufshcd_send_uic_cmd - Send UIC commands and retrieve the result
  380. * @hba: per adapter instance
  381. * @uic_cmd: UIC command
  382. *
  383. * Returns 0 only if success.
  384. */
  385. static int
  386. ufshcd_send_uic_cmd(struct ufs_hba *hba, struct uic_command *uic_cmd)
  387. {
  388. int ret;
  389. mutex_lock(&hba->uic_cmd_mutex);
  390. ret = __ufshcd_send_uic_cmd(hba, uic_cmd);
  391. mutex_unlock(&hba->uic_cmd_mutex);
  392. return ret;
  393. }
  394. /**
  395. * ufshcd_map_sg - Map scatter-gather list to prdt
  396. * @lrbp - pointer to local reference block
  397. *
  398. * Returns 0 in case of success, non-zero value in case of failure
  399. */
  400. static int ufshcd_map_sg(struct ufshcd_lrb *lrbp)
  401. {
  402. struct ufshcd_sg_entry *prd_table;
  403. struct scatterlist *sg;
  404. struct scsi_cmnd *cmd;
  405. int sg_segments;
  406. int i;
  407. cmd = lrbp->cmd;
  408. sg_segments = scsi_dma_map(cmd);
  409. if (sg_segments < 0)
  410. return sg_segments;
  411. if (sg_segments) {
  412. lrbp->utr_descriptor_ptr->prd_table_length =
  413. cpu_to_le16((u16) (sg_segments));
  414. prd_table = (struct ufshcd_sg_entry *)lrbp->ucd_prdt_ptr;
  415. scsi_for_each_sg(cmd, sg, sg_segments, i) {
  416. prd_table[i].size =
  417. cpu_to_le32(((u32) sg_dma_len(sg))-1);
  418. prd_table[i].base_addr =
  419. cpu_to_le32(lower_32_bits(sg->dma_address));
  420. prd_table[i].upper_addr =
  421. cpu_to_le32(upper_32_bits(sg->dma_address));
  422. }
  423. } else {
  424. lrbp->utr_descriptor_ptr->prd_table_length = 0;
  425. }
  426. return 0;
  427. }
  428. /**
  429. * ufshcd_enable_intr - enable interrupts
  430. * @hba: per adapter instance
  431. * @intrs: interrupt bits
  432. */
  433. static void ufshcd_enable_intr(struct ufs_hba *hba, u32 intrs)
  434. {
  435. u32 set = ufshcd_readl(hba, REG_INTERRUPT_ENABLE);
  436. if (hba->ufs_version == UFSHCI_VERSION_10) {
  437. u32 rw;
  438. rw = set & INTERRUPT_MASK_RW_VER_10;
  439. set = rw | ((set ^ intrs) & intrs);
  440. } else {
  441. set |= intrs;
  442. }
  443. ufshcd_writel(hba, set, REG_INTERRUPT_ENABLE);
  444. }
  445. /**
  446. * ufshcd_disable_intr - disable interrupts
  447. * @hba: per adapter instance
  448. * @intrs: interrupt bits
  449. */
  450. static void ufshcd_disable_intr(struct ufs_hba *hba, u32 intrs)
  451. {
  452. u32 set = ufshcd_readl(hba, REG_INTERRUPT_ENABLE);
  453. if (hba->ufs_version == UFSHCI_VERSION_10) {
  454. u32 rw;
  455. rw = (set & INTERRUPT_MASK_RW_VER_10) &
  456. ~(intrs & INTERRUPT_MASK_RW_VER_10);
  457. set = rw | ((set & intrs) & ~INTERRUPT_MASK_RW_VER_10);
  458. } else {
  459. set &= ~intrs;
  460. }
  461. ufshcd_writel(hba, set, REG_INTERRUPT_ENABLE);
  462. }
  463. /**
  464. * ufshcd_compose_upiu - form UFS Protocol Information Unit(UPIU)
  465. * @lrb - pointer to local reference block
  466. */
  467. static void ufshcd_compose_upiu(struct ufshcd_lrb *lrbp)
  468. {
  469. struct utp_transfer_req_desc *req_desc;
  470. struct utp_upiu_cmd *ucd_cmd_ptr;
  471. u32 data_direction;
  472. u32 upiu_flags;
  473. ucd_cmd_ptr = lrbp->ucd_cmd_ptr;
  474. req_desc = lrbp->utr_descriptor_ptr;
  475. switch (lrbp->command_type) {
  476. case UTP_CMD_TYPE_SCSI:
  477. if (lrbp->cmd->sc_data_direction == DMA_FROM_DEVICE) {
  478. data_direction = UTP_DEVICE_TO_HOST;
  479. upiu_flags = UPIU_CMD_FLAGS_READ;
  480. } else if (lrbp->cmd->sc_data_direction == DMA_TO_DEVICE) {
  481. data_direction = UTP_HOST_TO_DEVICE;
  482. upiu_flags = UPIU_CMD_FLAGS_WRITE;
  483. } else {
  484. data_direction = UTP_NO_DATA_TRANSFER;
  485. upiu_flags = UPIU_CMD_FLAGS_NONE;
  486. }
  487. /* Transfer request descriptor header fields */
  488. req_desc->header.dword_0 =
  489. cpu_to_le32(data_direction | UTP_SCSI_COMMAND);
  490. /*
  491. * assigning invalid value for command status. Controller
  492. * updates OCS on command completion, with the command
  493. * status
  494. */
  495. req_desc->header.dword_2 =
  496. cpu_to_le32(OCS_INVALID_COMMAND_STATUS);
  497. /* command descriptor fields */
  498. ucd_cmd_ptr->header.dword_0 =
  499. cpu_to_be32(UPIU_HEADER_DWORD(UPIU_TRANSACTION_COMMAND,
  500. upiu_flags,
  501. lrbp->lun,
  502. lrbp->task_tag));
  503. ucd_cmd_ptr->header.dword_1 =
  504. cpu_to_be32(
  505. UPIU_HEADER_DWORD(UPIU_COMMAND_SET_TYPE_SCSI,
  506. 0,
  507. 0,
  508. 0));
  509. /* Total EHS length and Data segment length will be zero */
  510. ucd_cmd_ptr->header.dword_2 = 0;
  511. ucd_cmd_ptr->exp_data_transfer_len =
  512. cpu_to_be32(lrbp->cmd->sdb.length);
  513. memcpy(ucd_cmd_ptr->cdb,
  514. lrbp->cmd->cmnd,
  515. (min_t(unsigned short,
  516. lrbp->cmd->cmd_len,
  517. MAX_CDB_SIZE)));
  518. break;
  519. case UTP_CMD_TYPE_DEV_MANAGE:
  520. /* For query function implementation */
  521. break;
  522. case UTP_CMD_TYPE_UFS:
  523. /* For UFS native command implementation */
  524. break;
  525. } /* end of switch */
  526. }
  527. /**
  528. * ufshcd_queuecommand - main entry point for SCSI requests
  529. * @cmd: command from SCSI Midlayer
  530. * @done: call back function
  531. *
  532. * Returns 0 for success, non-zero in case of failure
  533. */
  534. static int ufshcd_queuecommand(struct Scsi_Host *host, struct scsi_cmnd *cmd)
  535. {
  536. struct ufshcd_lrb *lrbp;
  537. struct ufs_hba *hba;
  538. unsigned long flags;
  539. int tag;
  540. int err = 0;
  541. hba = shost_priv(host);
  542. tag = cmd->request->tag;
  543. if (hba->ufshcd_state != UFSHCD_STATE_OPERATIONAL) {
  544. err = SCSI_MLQUEUE_HOST_BUSY;
  545. goto out;
  546. }
  547. lrbp = &hba->lrb[tag];
  548. lrbp->cmd = cmd;
  549. lrbp->sense_bufflen = SCSI_SENSE_BUFFERSIZE;
  550. lrbp->sense_buffer = cmd->sense_buffer;
  551. lrbp->task_tag = tag;
  552. lrbp->lun = cmd->device->lun;
  553. lrbp->command_type = UTP_CMD_TYPE_SCSI;
  554. /* form UPIU before issuing the command */
  555. ufshcd_compose_upiu(lrbp);
  556. err = ufshcd_map_sg(lrbp);
  557. if (err)
  558. goto out;
  559. /* issue command to the controller */
  560. spin_lock_irqsave(hba->host->host_lock, flags);
  561. ufshcd_send_command(hba, tag);
  562. spin_unlock_irqrestore(hba->host->host_lock, flags);
  563. out:
  564. return err;
  565. }
  566. /**
  567. * ufshcd_memory_alloc - allocate memory for host memory space data structures
  568. * @hba: per adapter instance
  569. *
  570. * 1. Allocate DMA memory for Command Descriptor array
  571. * Each command descriptor consist of Command UPIU, Response UPIU and PRDT
  572. * 2. Allocate DMA memory for UTP Transfer Request Descriptor List (UTRDL).
  573. * 3. Allocate DMA memory for UTP Task Management Request Descriptor List
  574. * (UTMRDL)
  575. * 4. Allocate memory for local reference block(lrb).
  576. *
  577. * Returns 0 for success, non-zero in case of failure
  578. */
  579. static int ufshcd_memory_alloc(struct ufs_hba *hba)
  580. {
  581. size_t utmrdl_size, utrdl_size, ucdl_size;
  582. /* Allocate memory for UTP command descriptors */
  583. ucdl_size = (sizeof(struct utp_transfer_cmd_desc) * hba->nutrs);
  584. hba->ucdl_base_addr = dmam_alloc_coherent(hba->dev,
  585. ucdl_size,
  586. &hba->ucdl_dma_addr,
  587. GFP_KERNEL);
  588. /*
  589. * UFSHCI requires UTP command descriptor to be 128 byte aligned.
  590. * make sure hba->ucdl_dma_addr is aligned to PAGE_SIZE
  591. * if hba->ucdl_dma_addr is aligned to PAGE_SIZE, then it will
  592. * be aligned to 128 bytes as well
  593. */
  594. if (!hba->ucdl_base_addr ||
  595. WARN_ON(hba->ucdl_dma_addr & (PAGE_SIZE - 1))) {
  596. dev_err(hba->dev,
  597. "Command Descriptor Memory allocation failed\n");
  598. goto out;
  599. }
  600. /*
  601. * Allocate memory for UTP Transfer descriptors
  602. * UFSHCI requires 1024 byte alignment of UTRD
  603. */
  604. utrdl_size = (sizeof(struct utp_transfer_req_desc) * hba->nutrs);
  605. hba->utrdl_base_addr = dmam_alloc_coherent(hba->dev,
  606. utrdl_size,
  607. &hba->utrdl_dma_addr,
  608. GFP_KERNEL);
  609. if (!hba->utrdl_base_addr ||
  610. WARN_ON(hba->utrdl_dma_addr & (PAGE_SIZE - 1))) {
  611. dev_err(hba->dev,
  612. "Transfer Descriptor Memory allocation failed\n");
  613. goto out;
  614. }
  615. /*
  616. * Allocate memory for UTP Task Management descriptors
  617. * UFSHCI requires 1024 byte alignment of UTMRD
  618. */
  619. utmrdl_size = sizeof(struct utp_task_req_desc) * hba->nutmrs;
  620. hba->utmrdl_base_addr = dmam_alloc_coherent(hba->dev,
  621. utmrdl_size,
  622. &hba->utmrdl_dma_addr,
  623. GFP_KERNEL);
  624. if (!hba->utmrdl_base_addr ||
  625. WARN_ON(hba->utmrdl_dma_addr & (PAGE_SIZE - 1))) {
  626. dev_err(hba->dev,
  627. "Task Management Descriptor Memory allocation failed\n");
  628. goto out;
  629. }
  630. /* Allocate memory for local reference block */
  631. hba->lrb = devm_kzalloc(hba->dev,
  632. hba->nutrs * sizeof(struct ufshcd_lrb),
  633. GFP_KERNEL);
  634. if (!hba->lrb) {
  635. dev_err(hba->dev, "LRB Memory allocation failed\n");
  636. goto out;
  637. }
  638. return 0;
  639. out:
  640. return -ENOMEM;
  641. }
  642. /**
  643. * ufshcd_host_memory_configure - configure local reference block with
  644. * memory offsets
  645. * @hba: per adapter instance
  646. *
  647. * Configure Host memory space
  648. * 1. Update Corresponding UTRD.UCDBA and UTRD.UCDBAU with UCD DMA
  649. * address.
  650. * 2. Update each UTRD with Response UPIU offset, Response UPIU length
  651. * and PRDT offset.
  652. * 3. Save the corresponding addresses of UTRD, UCD.CMD, UCD.RSP and UCD.PRDT
  653. * into local reference block.
  654. */
  655. static void ufshcd_host_memory_configure(struct ufs_hba *hba)
  656. {
  657. struct utp_transfer_cmd_desc *cmd_descp;
  658. struct utp_transfer_req_desc *utrdlp;
  659. dma_addr_t cmd_desc_dma_addr;
  660. dma_addr_t cmd_desc_element_addr;
  661. u16 response_offset;
  662. u16 prdt_offset;
  663. int cmd_desc_size;
  664. int i;
  665. utrdlp = hba->utrdl_base_addr;
  666. cmd_descp = hba->ucdl_base_addr;
  667. response_offset =
  668. offsetof(struct utp_transfer_cmd_desc, response_upiu);
  669. prdt_offset =
  670. offsetof(struct utp_transfer_cmd_desc, prd_table);
  671. cmd_desc_size = sizeof(struct utp_transfer_cmd_desc);
  672. cmd_desc_dma_addr = hba->ucdl_dma_addr;
  673. for (i = 0; i < hba->nutrs; i++) {
  674. /* Configure UTRD with command descriptor base address */
  675. cmd_desc_element_addr =
  676. (cmd_desc_dma_addr + (cmd_desc_size * i));
  677. utrdlp[i].command_desc_base_addr_lo =
  678. cpu_to_le32(lower_32_bits(cmd_desc_element_addr));
  679. utrdlp[i].command_desc_base_addr_hi =
  680. cpu_to_le32(upper_32_bits(cmd_desc_element_addr));
  681. /* Response upiu and prdt offset should be in double words */
  682. utrdlp[i].response_upiu_offset =
  683. cpu_to_le16((response_offset >> 2));
  684. utrdlp[i].prd_table_offset =
  685. cpu_to_le16((prdt_offset >> 2));
  686. utrdlp[i].response_upiu_length =
  687. cpu_to_le16(ALIGNED_UPIU_SIZE >> 2);
  688. hba->lrb[i].utr_descriptor_ptr = (utrdlp + i);
  689. hba->lrb[i].ucd_cmd_ptr =
  690. (struct utp_upiu_cmd *)(cmd_descp + i);
  691. hba->lrb[i].ucd_rsp_ptr =
  692. (struct utp_upiu_rsp *)cmd_descp[i].response_upiu;
  693. hba->lrb[i].ucd_prdt_ptr =
  694. (struct ufshcd_sg_entry *)cmd_descp[i].prd_table;
  695. }
  696. }
  697. /**
  698. * ufshcd_dme_link_startup - Notify Unipro to perform link startup
  699. * @hba: per adapter instance
  700. *
  701. * UIC_CMD_DME_LINK_STARTUP command must be issued to Unipro layer,
  702. * in order to initialize the Unipro link startup procedure.
  703. * Once the Unipro links are up, the device connected to the controller
  704. * is detected.
  705. *
  706. * Returns 0 on success, non-zero value on failure
  707. */
  708. static int ufshcd_dme_link_startup(struct ufs_hba *hba)
  709. {
  710. struct uic_command uic_cmd = {0};
  711. int ret;
  712. uic_cmd.command = UIC_CMD_DME_LINK_STARTUP;
  713. ret = ufshcd_send_uic_cmd(hba, &uic_cmd);
  714. if (ret)
  715. dev_err(hba->dev,
  716. "dme-link-startup: error code %d\n", ret);
  717. return ret;
  718. }
  719. /**
  720. * ufshcd_make_hba_operational - Make UFS controller operational
  721. * @hba: per adapter instance
  722. *
  723. * To bring UFS host controller to operational state,
  724. * 1. Check if device is present
  725. * 2. Enable required interrupts
  726. * 3. Configure interrupt aggregation
  727. * 4. Program UTRL and UTMRL base addres
  728. * 5. Configure run-stop-registers
  729. *
  730. * Returns 0 on success, non-zero value on failure
  731. */
  732. static int ufshcd_make_hba_operational(struct ufs_hba *hba)
  733. {
  734. int err = 0;
  735. u32 reg;
  736. /* check if device present */
  737. reg = ufshcd_readl(hba, REG_CONTROLLER_STATUS);
  738. if (!ufshcd_is_device_present(reg)) {
  739. dev_err(hba->dev, "cc: Device not present\n");
  740. err = -ENXIO;
  741. goto out;
  742. }
  743. /* Enable required interrupts */
  744. ufshcd_enable_intr(hba, UFSHCD_ENABLE_INTRS);
  745. /* Configure interrupt aggregation */
  746. ufshcd_config_int_aggr(hba, INT_AGGR_CONFIG);
  747. /* Configure UTRL and UTMRL base address registers */
  748. ufshcd_writel(hba, lower_32_bits(hba->utrdl_dma_addr),
  749. REG_UTP_TRANSFER_REQ_LIST_BASE_L);
  750. ufshcd_writel(hba, upper_32_bits(hba->utrdl_dma_addr),
  751. REG_UTP_TRANSFER_REQ_LIST_BASE_H);
  752. ufshcd_writel(hba, lower_32_bits(hba->utmrdl_dma_addr),
  753. REG_UTP_TASK_REQ_LIST_BASE_L);
  754. ufshcd_writel(hba, upper_32_bits(hba->utmrdl_dma_addr),
  755. REG_UTP_TASK_REQ_LIST_BASE_H);
  756. /*
  757. * UCRDY, UTMRLDY and UTRLRDY bits must be 1
  758. * DEI, HEI bits must be 0
  759. */
  760. if (!(ufshcd_get_lists_status(reg))) {
  761. ufshcd_enable_run_stop_reg(hba);
  762. } else {
  763. dev_err(hba->dev,
  764. "Host controller not ready to process requests");
  765. err = -EIO;
  766. goto out;
  767. }
  768. if (hba->ufshcd_state == UFSHCD_STATE_RESET)
  769. scsi_unblock_requests(hba->host);
  770. hba->ufshcd_state = UFSHCD_STATE_OPERATIONAL;
  771. out:
  772. return err;
  773. }
  774. /**
  775. * ufshcd_hba_enable - initialize the controller
  776. * @hba: per adapter instance
  777. *
  778. * The controller resets itself and controller firmware initialization
  779. * sequence kicks off. When controller is ready it will set
  780. * the Host Controller Enable bit to 1.
  781. *
  782. * Returns 0 on success, non-zero value on failure
  783. */
  784. static int ufshcd_hba_enable(struct ufs_hba *hba)
  785. {
  786. int retry;
  787. /*
  788. * msleep of 1 and 5 used in this function might result in msleep(20),
  789. * but it was necessary to send the UFS FPGA to reset mode during
  790. * development and testing of this driver. msleep can be changed to
  791. * mdelay and retry count can be reduced based on the controller.
  792. */
  793. if (!ufshcd_is_hba_active(hba)) {
  794. /* change controller state to "reset state" */
  795. ufshcd_hba_stop(hba);
  796. /*
  797. * This delay is based on the testing done with UFS host
  798. * controller FPGA. The delay can be changed based on the
  799. * host controller used.
  800. */
  801. msleep(5);
  802. }
  803. /* start controller initialization sequence */
  804. ufshcd_hba_start(hba);
  805. /*
  806. * To initialize a UFS host controller HCE bit must be set to 1.
  807. * During initialization the HCE bit value changes from 1->0->1.
  808. * When the host controller completes initialization sequence
  809. * it sets the value of HCE bit to 1. The same HCE bit is read back
  810. * to check if the controller has completed initialization sequence.
  811. * So without this delay the value HCE = 1, set in the previous
  812. * instruction might be read back.
  813. * This delay can be changed based on the controller.
  814. */
  815. msleep(1);
  816. /* wait for the host controller to complete initialization */
  817. retry = 10;
  818. while (ufshcd_is_hba_active(hba)) {
  819. if (retry) {
  820. retry--;
  821. } else {
  822. dev_err(hba->dev,
  823. "Controller enable failed\n");
  824. return -EIO;
  825. }
  826. msleep(5);
  827. }
  828. return 0;
  829. }
  830. /**
  831. * ufshcd_link_startup - Initialize unipro link startup
  832. * @hba: per adapter instance
  833. *
  834. * Returns 0 for success, non-zero in case of failure
  835. */
  836. static int ufshcd_link_startup(struct ufs_hba *hba)
  837. {
  838. int ret;
  839. /* enable UIC related interrupts */
  840. ufshcd_enable_intr(hba, UIC_COMMAND_COMPL);
  841. ret = ufshcd_dme_link_startup(hba);
  842. if (ret)
  843. goto out;
  844. ret = ufshcd_make_hba_operational(hba);
  845. out:
  846. if (ret)
  847. dev_err(hba->dev, "link startup failed %d\n", ret);
  848. return ret;
  849. }
  850. /**
  851. * ufshcd_do_reset - reset the host controller
  852. * @hba: per adapter instance
  853. *
  854. * Returns SUCCESS/FAILED
  855. */
  856. static int ufshcd_do_reset(struct ufs_hba *hba)
  857. {
  858. struct ufshcd_lrb *lrbp;
  859. unsigned long flags;
  860. int tag;
  861. /* block commands from midlayer */
  862. scsi_block_requests(hba->host);
  863. spin_lock_irqsave(hba->host->host_lock, flags);
  864. hba->ufshcd_state = UFSHCD_STATE_RESET;
  865. /* send controller to reset state */
  866. ufshcd_hba_stop(hba);
  867. spin_unlock_irqrestore(hba->host->host_lock, flags);
  868. /* abort outstanding commands */
  869. for (tag = 0; tag < hba->nutrs; tag++) {
  870. if (test_bit(tag, &hba->outstanding_reqs)) {
  871. lrbp = &hba->lrb[tag];
  872. scsi_dma_unmap(lrbp->cmd);
  873. lrbp->cmd->result = DID_RESET << 16;
  874. lrbp->cmd->scsi_done(lrbp->cmd);
  875. lrbp->cmd = NULL;
  876. }
  877. }
  878. /* clear outstanding request/task bit maps */
  879. hba->outstanding_reqs = 0;
  880. hba->outstanding_tasks = 0;
  881. /* Host controller enable */
  882. if (ufshcd_hba_enable(hba)) {
  883. dev_err(hba->dev,
  884. "Reset: Controller initialization failed\n");
  885. return FAILED;
  886. }
  887. if (ufshcd_link_startup(hba)) {
  888. dev_err(hba->dev,
  889. "Reset: Link start-up failed\n");
  890. return FAILED;
  891. }
  892. return SUCCESS;
  893. }
  894. /**
  895. * ufshcd_slave_alloc - handle initial SCSI device configurations
  896. * @sdev: pointer to SCSI device
  897. *
  898. * Returns success
  899. */
  900. static int ufshcd_slave_alloc(struct scsi_device *sdev)
  901. {
  902. struct ufs_hba *hba;
  903. hba = shost_priv(sdev->host);
  904. sdev->tagged_supported = 1;
  905. /* Mode sense(6) is not supported by UFS, so use Mode sense(10) */
  906. sdev->use_10_for_ms = 1;
  907. scsi_set_tag_type(sdev, MSG_SIMPLE_TAG);
  908. /*
  909. * Inform SCSI Midlayer that the LUN queue depth is same as the
  910. * controller queue depth. If a LUN queue depth is less than the
  911. * controller queue depth and if the LUN reports
  912. * SAM_STAT_TASK_SET_FULL, the LUN queue depth will be adjusted
  913. * with scsi_adjust_queue_depth.
  914. */
  915. scsi_activate_tcq(sdev, hba->nutrs);
  916. return 0;
  917. }
  918. /**
  919. * ufshcd_slave_destroy - remove SCSI device configurations
  920. * @sdev: pointer to SCSI device
  921. */
  922. static void ufshcd_slave_destroy(struct scsi_device *sdev)
  923. {
  924. struct ufs_hba *hba;
  925. hba = shost_priv(sdev->host);
  926. scsi_deactivate_tcq(sdev, hba->nutrs);
  927. }
  928. /**
  929. * ufshcd_task_req_compl - handle task management request completion
  930. * @hba: per adapter instance
  931. * @index: index of the completed request
  932. *
  933. * Returns SUCCESS/FAILED
  934. */
  935. static int ufshcd_task_req_compl(struct ufs_hba *hba, u32 index)
  936. {
  937. struct utp_task_req_desc *task_req_descp;
  938. struct utp_upiu_task_rsp *task_rsp_upiup;
  939. unsigned long flags;
  940. int ocs_value;
  941. int task_result;
  942. spin_lock_irqsave(hba->host->host_lock, flags);
  943. /* Clear completed tasks from outstanding_tasks */
  944. __clear_bit(index, &hba->outstanding_tasks);
  945. task_req_descp = hba->utmrdl_base_addr;
  946. ocs_value = ufshcd_get_tmr_ocs(&task_req_descp[index]);
  947. if (ocs_value == OCS_SUCCESS) {
  948. task_rsp_upiup = (struct utp_upiu_task_rsp *)
  949. task_req_descp[index].task_rsp_upiu;
  950. task_result = be32_to_cpu(task_rsp_upiup->header.dword_1);
  951. task_result = ((task_result & MASK_TASK_RESPONSE) >> 8);
  952. if (task_result != UPIU_TASK_MANAGEMENT_FUNC_COMPL &&
  953. task_result != UPIU_TASK_MANAGEMENT_FUNC_SUCCEEDED)
  954. task_result = FAILED;
  955. else
  956. task_result = SUCCESS;
  957. } else {
  958. task_result = FAILED;
  959. dev_err(hba->dev,
  960. "trc: Invalid ocs = %x\n", ocs_value);
  961. }
  962. spin_unlock_irqrestore(hba->host->host_lock, flags);
  963. return task_result;
  964. }
  965. /**
  966. * ufshcd_adjust_lun_qdepth - Update LUN queue depth if device responds with
  967. * SAM_STAT_TASK_SET_FULL SCSI command status.
  968. * @cmd: pointer to SCSI command
  969. */
  970. static void ufshcd_adjust_lun_qdepth(struct scsi_cmnd *cmd)
  971. {
  972. struct ufs_hba *hba;
  973. int i;
  974. int lun_qdepth = 0;
  975. hba = shost_priv(cmd->device->host);
  976. /*
  977. * LUN queue depth can be obtained by counting outstanding commands
  978. * on the LUN.
  979. */
  980. for (i = 0; i < hba->nutrs; i++) {
  981. if (test_bit(i, &hba->outstanding_reqs)) {
  982. /*
  983. * Check if the outstanding command belongs
  984. * to the LUN which reported SAM_STAT_TASK_SET_FULL.
  985. */
  986. if (cmd->device->lun == hba->lrb[i].lun)
  987. lun_qdepth++;
  988. }
  989. }
  990. /*
  991. * LUN queue depth will be total outstanding commands, except the
  992. * command for which the LUN reported SAM_STAT_TASK_SET_FULL.
  993. */
  994. scsi_adjust_queue_depth(cmd->device, MSG_SIMPLE_TAG, lun_qdepth - 1);
  995. }
  996. /**
  997. * ufshcd_scsi_cmd_status - Update SCSI command result based on SCSI status
  998. * @lrb: pointer to local reference block of completed command
  999. * @scsi_status: SCSI command status
  1000. *
  1001. * Returns value base on SCSI command status
  1002. */
  1003. static inline int
  1004. ufshcd_scsi_cmd_status(struct ufshcd_lrb *lrbp, int scsi_status)
  1005. {
  1006. int result = 0;
  1007. switch (scsi_status) {
  1008. case SAM_STAT_GOOD:
  1009. result |= DID_OK << 16 |
  1010. COMMAND_COMPLETE << 8 |
  1011. SAM_STAT_GOOD;
  1012. break;
  1013. case SAM_STAT_CHECK_CONDITION:
  1014. result |= DID_OK << 16 |
  1015. COMMAND_COMPLETE << 8 |
  1016. SAM_STAT_CHECK_CONDITION;
  1017. ufshcd_copy_sense_data(lrbp);
  1018. break;
  1019. case SAM_STAT_BUSY:
  1020. result |= SAM_STAT_BUSY;
  1021. break;
  1022. case SAM_STAT_TASK_SET_FULL:
  1023. /*
  1024. * If a LUN reports SAM_STAT_TASK_SET_FULL, then the LUN queue
  1025. * depth needs to be adjusted to the exact number of
  1026. * outstanding commands the LUN can handle at any given time.
  1027. */
  1028. ufshcd_adjust_lun_qdepth(lrbp->cmd);
  1029. result |= SAM_STAT_TASK_SET_FULL;
  1030. break;
  1031. case SAM_STAT_TASK_ABORTED:
  1032. result |= SAM_STAT_TASK_ABORTED;
  1033. break;
  1034. default:
  1035. result |= DID_ERROR << 16;
  1036. break;
  1037. } /* end of switch */
  1038. return result;
  1039. }
  1040. /**
  1041. * ufshcd_transfer_rsp_status - Get overall status of the response
  1042. * @hba: per adapter instance
  1043. * @lrb: pointer to local reference block of completed command
  1044. *
  1045. * Returns result of the command to notify SCSI midlayer
  1046. */
  1047. static inline int
  1048. ufshcd_transfer_rsp_status(struct ufs_hba *hba, struct ufshcd_lrb *lrbp)
  1049. {
  1050. int result = 0;
  1051. int scsi_status;
  1052. int ocs;
  1053. /* overall command status of utrd */
  1054. ocs = ufshcd_get_tr_ocs(lrbp);
  1055. switch (ocs) {
  1056. case OCS_SUCCESS:
  1057. /* check if the returned transfer response is valid */
  1058. result = ufshcd_is_valid_req_rsp(lrbp->ucd_rsp_ptr);
  1059. if (result) {
  1060. dev_err(hba->dev,
  1061. "Invalid response = %x\n", result);
  1062. break;
  1063. }
  1064. /*
  1065. * get the response UPIU result to extract
  1066. * the SCSI command status
  1067. */
  1068. result = ufshcd_get_rsp_upiu_result(lrbp->ucd_rsp_ptr);
  1069. /*
  1070. * get the result based on SCSI status response
  1071. * to notify the SCSI midlayer of the command status
  1072. */
  1073. scsi_status = result & MASK_SCSI_STATUS;
  1074. result = ufshcd_scsi_cmd_status(lrbp, scsi_status);
  1075. break;
  1076. case OCS_ABORTED:
  1077. result |= DID_ABORT << 16;
  1078. break;
  1079. case OCS_INVALID_CMD_TABLE_ATTR:
  1080. case OCS_INVALID_PRDT_ATTR:
  1081. case OCS_MISMATCH_DATA_BUF_SIZE:
  1082. case OCS_MISMATCH_RESP_UPIU_SIZE:
  1083. case OCS_PEER_COMM_FAILURE:
  1084. case OCS_FATAL_ERROR:
  1085. default:
  1086. result |= DID_ERROR << 16;
  1087. dev_err(hba->dev,
  1088. "OCS error from controller = %x\n", ocs);
  1089. break;
  1090. } /* end of switch */
  1091. return result;
  1092. }
  1093. /**
  1094. * ufshcd_uic_cmd_compl - handle completion of uic command
  1095. * @hba: per adapter instance
  1096. */
  1097. static void ufshcd_uic_cmd_compl(struct ufs_hba *hba)
  1098. {
  1099. if (hba->active_uic_cmd) {
  1100. hba->active_uic_cmd->argument2 |=
  1101. ufshcd_get_uic_cmd_result(hba);
  1102. complete(&hba->active_uic_cmd->done);
  1103. }
  1104. }
  1105. /**
  1106. * ufshcd_transfer_req_compl - handle SCSI and query command completion
  1107. * @hba: per adapter instance
  1108. */
  1109. static void ufshcd_transfer_req_compl(struct ufs_hba *hba)
  1110. {
  1111. struct ufshcd_lrb *lrb;
  1112. unsigned long completed_reqs;
  1113. u32 tr_doorbell;
  1114. int result;
  1115. int index;
  1116. lrb = hba->lrb;
  1117. tr_doorbell = ufshcd_readl(hba, REG_UTP_TRANSFER_REQ_DOOR_BELL);
  1118. completed_reqs = tr_doorbell ^ hba->outstanding_reqs;
  1119. for (index = 0; index < hba->nutrs; index++) {
  1120. if (test_bit(index, &completed_reqs)) {
  1121. result = ufshcd_transfer_rsp_status(hba, &lrb[index]);
  1122. if (lrb[index].cmd) {
  1123. scsi_dma_unmap(lrb[index].cmd);
  1124. lrb[index].cmd->result = result;
  1125. lrb[index].cmd->scsi_done(lrb[index].cmd);
  1126. /* Mark completed command as NULL in LRB */
  1127. lrb[index].cmd = NULL;
  1128. }
  1129. } /* end of if */
  1130. } /* end of for */
  1131. /* clear corresponding bits of completed commands */
  1132. hba->outstanding_reqs ^= completed_reqs;
  1133. /* Reset interrupt aggregation counters */
  1134. ufshcd_config_int_aggr(hba, INT_AGGR_RESET);
  1135. }
  1136. /**
  1137. * ufshcd_fatal_err_handler - handle fatal errors
  1138. * @hba: per adapter instance
  1139. */
  1140. static void ufshcd_fatal_err_handler(struct work_struct *work)
  1141. {
  1142. struct ufs_hba *hba;
  1143. hba = container_of(work, struct ufs_hba, feh_workq);
  1144. /* check if reset is already in progress */
  1145. if (hba->ufshcd_state != UFSHCD_STATE_RESET)
  1146. ufshcd_do_reset(hba);
  1147. }
  1148. /**
  1149. * ufshcd_err_handler - Check for fatal errors
  1150. * @work: pointer to a work queue structure
  1151. */
  1152. static void ufshcd_err_handler(struct ufs_hba *hba)
  1153. {
  1154. u32 reg;
  1155. if (hba->errors & INT_FATAL_ERRORS)
  1156. goto fatal_eh;
  1157. if (hba->errors & UIC_ERROR) {
  1158. reg = ufshcd_readl(hba, REG_UIC_ERROR_CODE_DATA_LINK_LAYER);
  1159. if (reg & UIC_DATA_LINK_LAYER_ERROR_PA_INIT)
  1160. goto fatal_eh;
  1161. }
  1162. return;
  1163. fatal_eh:
  1164. hba->ufshcd_state = UFSHCD_STATE_ERROR;
  1165. schedule_work(&hba->feh_workq);
  1166. }
  1167. /**
  1168. * ufshcd_tmc_handler - handle task management function completion
  1169. * @hba: per adapter instance
  1170. */
  1171. static void ufshcd_tmc_handler(struct ufs_hba *hba)
  1172. {
  1173. u32 tm_doorbell;
  1174. tm_doorbell = ufshcd_readl(hba, REG_UTP_TASK_REQ_DOOR_BELL);
  1175. hba->tm_condition = tm_doorbell ^ hba->outstanding_tasks;
  1176. wake_up_interruptible(&hba->ufshcd_tm_wait_queue);
  1177. }
  1178. /**
  1179. * ufshcd_sl_intr - Interrupt service routine
  1180. * @hba: per adapter instance
  1181. * @intr_status: contains interrupts generated by the controller
  1182. */
  1183. static void ufshcd_sl_intr(struct ufs_hba *hba, u32 intr_status)
  1184. {
  1185. hba->errors = UFSHCD_ERROR_MASK & intr_status;
  1186. if (hba->errors)
  1187. ufshcd_err_handler(hba);
  1188. if (intr_status & UIC_COMMAND_COMPL)
  1189. ufshcd_uic_cmd_compl(hba);
  1190. if (intr_status & UTP_TASK_REQ_COMPL)
  1191. ufshcd_tmc_handler(hba);
  1192. if (intr_status & UTP_TRANSFER_REQ_COMPL)
  1193. ufshcd_transfer_req_compl(hba);
  1194. }
  1195. /**
  1196. * ufshcd_intr - Main interrupt service routine
  1197. * @irq: irq number
  1198. * @__hba: pointer to adapter instance
  1199. *
  1200. * Returns IRQ_HANDLED - If interrupt is valid
  1201. * IRQ_NONE - If invalid interrupt
  1202. */
  1203. static irqreturn_t ufshcd_intr(int irq, void *__hba)
  1204. {
  1205. u32 intr_status;
  1206. irqreturn_t retval = IRQ_NONE;
  1207. struct ufs_hba *hba = __hba;
  1208. spin_lock(hba->host->host_lock);
  1209. intr_status = ufshcd_readl(hba, REG_INTERRUPT_STATUS);
  1210. if (intr_status) {
  1211. ufshcd_writel(hba, intr_status, REG_INTERRUPT_STATUS);
  1212. ufshcd_sl_intr(hba, intr_status);
  1213. retval = IRQ_HANDLED;
  1214. }
  1215. spin_unlock(hba->host->host_lock);
  1216. return retval;
  1217. }
  1218. /**
  1219. * ufshcd_issue_tm_cmd - issues task management commands to controller
  1220. * @hba: per adapter instance
  1221. * @lrbp: pointer to local reference block
  1222. *
  1223. * Returns SUCCESS/FAILED
  1224. */
  1225. static int
  1226. ufshcd_issue_tm_cmd(struct ufs_hba *hba,
  1227. struct ufshcd_lrb *lrbp,
  1228. u8 tm_function)
  1229. {
  1230. struct utp_task_req_desc *task_req_descp;
  1231. struct utp_upiu_task_req *task_req_upiup;
  1232. struct Scsi_Host *host;
  1233. unsigned long flags;
  1234. int free_slot = 0;
  1235. int err;
  1236. host = hba->host;
  1237. spin_lock_irqsave(host->host_lock, flags);
  1238. /* If task management queue is full */
  1239. free_slot = ufshcd_get_tm_free_slot(hba);
  1240. if (free_slot >= hba->nutmrs) {
  1241. spin_unlock_irqrestore(host->host_lock, flags);
  1242. dev_err(hba->dev, "Task management queue full\n");
  1243. err = FAILED;
  1244. goto out;
  1245. }
  1246. task_req_descp = hba->utmrdl_base_addr;
  1247. task_req_descp += free_slot;
  1248. /* Configure task request descriptor */
  1249. task_req_descp->header.dword_0 = cpu_to_le32(UTP_REQ_DESC_INT_CMD);
  1250. task_req_descp->header.dword_2 =
  1251. cpu_to_le32(OCS_INVALID_COMMAND_STATUS);
  1252. /* Configure task request UPIU */
  1253. task_req_upiup =
  1254. (struct utp_upiu_task_req *) task_req_descp->task_req_upiu;
  1255. task_req_upiup->header.dword_0 =
  1256. cpu_to_be32(UPIU_HEADER_DWORD(UPIU_TRANSACTION_TASK_REQ, 0,
  1257. lrbp->lun, lrbp->task_tag));
  1258. task_req_upiup->header.dword_1 =
  1259. cpu_to_be32(UPIU_HEADER_DWORD(0, tm_function, 0, 0));
  1260. task_req_upiup->input_param1 = lrbp->lun;
  1261. task_req_upiup->input_param1 =
  1262. cpu_to_be32(task_req_upiup->input_param1);
  1263. task_req_upiup->input_param2 = lrbp->task_tag;
  1264. task_req_upiup->input_param2 =
  1265. cpu_to_be32(task_req_upiup->input_param2);
  1266. /* send command to the controller */
  1267. __set_bit(free_slot, &hba->outstanding_tasks);
  1268. ufshcd_writel(hba, 1 << free_slot, REG_UTP_TASK_REQ_DOOR_BELL);
  1269. spin_unlock_irqrestore(host->host_lock, flags);
  1270. /* wait until the task management command is completed */
  1271. err =
  1272. wait_event_interruptible_timeout(hba->ufshcd_tm_wait_queue,
  1273. (test_bit(free_slot,
  1274. &hba->tm_condition) != 0),
  1275. 60 * HZ);
  1276. if (!err) {
  1277. dev_err(hba->dev,
  1278. "Task management command timed-out\n");
  1279. err = FAILED;
  1280. goto out;
  1281. }
  1282. clear_bit(free_slot, &hba->tm_condition);
  1283. err = ufshcd_task_req_compl(hba, free_slot);
  1284. out:
  1285. return err;
  1286. }
  1287. /**
  1288. * ufshcd_device_reset - reset device and abort all the pending commands
  1289. * @cmd: SCSI command pointer
  1290. *
  1291. * Returns SUCCESS/FAILED
  1292. */
  1293. static int ufshcd_device_reset(struct scsi_cmnd *cmd)
  1294. {
  1295. struct Scsi_Host *host;
  1296. struct ufs_hba *hba;
  1297. unsigned int tag;
  1298. u32 pos;
  1299. int err;
  1300. host = cmd->device->host;
  1301. hba = shost_priv(host);
  1302. tag = cmd->request->tag;
  1303. err = ufshcd_issue_tm_cmd(hba, &hba->lrb[tag], UFS_LOGICAL_RESET);
  1304. if (err == FAILED)
  1305. goto out;
  1306. for (pos = 0; pos < hba->nutrs; pos++) {
  1307. if (test_bit(pos, &hba->outstanding_reqs) &&
  1308. (hba->lrb[tag].lun == hba->lrb[pos].lun)) {
  1309. /* clear the respective UTRLCLR register bit */
  1310. ufshcd_utrl_clear(hba, pos);
  1311. clear_bit(pos, &hba->outstanding_reqs);
  1312. if (hba->lrb[pos].cmd) {
  1313. scsi_dma_unmap(hba->lrb[pos].cmd);
  1314. hba->lrb[pos].cmd->result =
  1315. DID_ABORT << 16;
  1316. hba->lrb[pos].cmd->scsi_done(cmd);
  1317. hba->lrb[pos].cmd = NULL;
  1318. }
  1319. }
  1320. } /* end of for */
  1321. out:
  1322. return err;
  1323. }
  1324. /**
  1325. * ufshcd_host_reset - Main reset function registered with scsi layer
  1326. * @cmd: SCSI command pointer
  1327. *
  1328. * Returns SUCCESS/FAILED
  1329. */
  1330. static int ufshcd_host_reset(struct scsi_cmnd *cmd)
  1331. {
  1332. struct ufs_hba *hba;
  1333. hba = shost_priv(cmd->device->host);
  1334. if (hba->ufshcd_state == UFSHCD_STATE_RESET)
  1335. return SUCCESS;
  1336. return ufshcd_do_reset(hba);
  1337. }
  1338. /**
  1339. * ufshcd_abort - abort a specific command
  1340. * @cmd: SCSI command pointer
  1341. *
  1342. * Returns SUCCESS/FAILED
  1343. */
  1344. static int ufshcd_abort(struct scsi_cmnd *cmd)
  1345. {
  1346. struct Scsi_Host *host;
  1347. struct ufs_hba *hba;
  1348. unsigned long flags;
  1349. unsigned int tag;
  1350. int err;
  1351. host = cmd->device->host;
  1352. hba = shost_priv(host);
  1353. tag = cmd->request->tag;
  1354. spin_lock_irqsave(host->host_lock, flags);
  1355. /* check if command is still pending */
  1356. if (!(test_bit(tag, &hba->outstanding_reqs))) {
  1357. err = FAILED;
  1358. spin_unlock_irqrestore(host->host_lock, flags);
  1359. goto out;
  1360. }
  1361. spin_unlock_irqrestore(host->host_lock, flags);
  1362. err = ufshcd_issue_tm_cmd(hba, &hba->lrb[tag], UFS_ABORT_TASK);
  1363. if (err == FAILED)
  1364. goto out;
  1365. scsi_dma_unmap(cmd);
  1366. spin_lock_irqsave(host->host_lock, flags);
  1367. /* clear the respective UTRLCLR register bit */
  1368. ufshcd_utrl_clear(hba, tag);
  1369. __clear_bit(tag, &hba->outstanding_reqs);
  1370. hba->lrb[tag].cmd = NULL;
  1371. spin_unlock_irqrestore(host->host_lock, flags);
  1372. out:
  1373. return err;
  1374. }
  1375. /**
  1376. * ufshcd_async_scan - asynchronous execution for link startup
  1377. * @data: data pointer to pass to this function
  1378. * @cookie: cookie data
  1379. */
  1380. static void ufshcd_async_scan(void *data, async_cookie_t cookie)
  1381. {
  1382. struct ufs_hba *hba = (struct ufs_hba *)data;
  1383. int ret;
  1384. ret = ufshcd_link_startup(hba);
  1385. if (!ret)
  1386. scsi_scan_host(hba->host);
  1387. }
  1388. static struct scsi_host_template ufshcd_driver_template = {
  1389. .module = THIS_MODULE,
  1390. .name = UFSHCD,
  1391. .proc_name = UFSHCD,
  1392. .queuecommand = ufshcd_queuecommand,
  1393. .slave_alloc = ufshcd_slave_alloc,
  1394. .slave_destroy = ufshcd_slave_destroy,
  1395. .eh_abort_handler = ufshcd_abort,
  1396. .eh_device_reset_handler = ufshcd_device_reset,
  1397. .eh_host_reset_handler = ufshcd_host_reset,
  1398. .this_id = -1,
  1399. .sg_tablesize = SG_ALL,
  1400. .cmd_per_lun = UFSHCD_CMD_PER_LUN,
  1401. .can_queue = UFSHCD_CAN_QUEUE,
  1402. };
  1403. /**
  1404. * ufshcd_suspend - suspend power management function
  1405. * @hba: per adapter instance
  1406. * @state: power state
  1407. *
  1408. * Returns -ENOSYS
  1409. */
  1410. int ufshcd_suspend(struct ufs_hba *hba, pm_message_t state)
  1411. {
  1412. /*
  1413. * TODO:
  1414. * 1. Block SCSI requests from SCSI midlayer
  1415. * 2. Change the internal driver state to non operational
  1416. * 3. Set UTRLRSR and UTMRLRSR bits to zero
  1417. * 4. Wait until outstanding commands are completed
  1418. * 5. Set HCE to zero to send the UFS host controller to reset state
  1419. */
  1420. return -ENOSYS;
  1421. }
  1422. EXPORT_SYMBOL_GPL(ufshcd_suspend);
  1423. /**
  1424. * ufshcd_resume - resume power management function
  1425. * @hba: per adapter instance
  1426. *
  1427. * Returns -ENOSYS
  1428. */
  1429. int ufshcd_resume(struct ufs_hba *hba)
  1430. {
  1431. /*
  1432. * TODO:
  1433. * 1. Set HCE to 1, to start the UFS host controller
  1434. * initialization process
  1435. * 2. Set UTRLRSR and UTMRLRSR bits to 1
  1436. * 3. Change the internal driver state to operational
  1437. * 4. Unblock SCSI requests from SCSI midlayer
  1438. */
  1439. return -ENOSYS;
  1440. }
  1441. EXPORT_SYMBOL_GPL(ufshcd_resume);
  1442. /**
  1443. * ufshcd_remove - de-allocate SCSI host and host memory space
  1444. * data structure memory
  1445. * @hba - per adapter instance
  1446. */
  1447. void ufshcd_remove(struct ufs_hba *hba)
  1448. {
  1449. /* disable interrupts */
  1450. ufshcd_disable_intr(hba, hba->intr_mask);
  1451. ufshcd_hba_stop(hba);
  1452. scsi_remove_host(hba->host);
  1453. scsi_host_put(hba->host);
  1454. }
  1455. EXPORT_SYMBOL_GPL(ufshcd_remove);
  1456. /**
  1457. * ufshcd_init - Driver initialization routine
  1458. * @dev: pointer to device handle
  1459. * @hba_handle: driver private handle
  1460. * @mmio_base: base register address
  1461. * @irq: Interrupt line of device
  1462. * Returns 0 on success, non-zero value on failure
  1463. */
  1464. int ufshcd_init(struct device *dev, struct ufs_hba **hba_handle,
  1465. void __iomem *mmio_base, unsigned int irq)
  1466. {
  1467. struct Scsi_Host *host;
  1468. struct ufs_hba *hba;
  1469. int err;
  1470. if (!dev) {
  1471. dev_err(dev,
  1472. "Invalid memory reference for dev is NULL\n");
  1473. err = -ENODEV;
  1474. goto out_error;
  1475. }
  1476. if (!mmio_base) {
  1477. dev_err(dev,
  1478. "Invalid memory reference for mmio_base is NULL\n");
  1479. err = -ENODEV;
  1480. goto out_error;
  1481. }
  1482. host = scsi_host_alloc(&ufshcd_driver_template,
  1483. sizeof(struct ufs_hba));
  1484. if (!host) {
  1485. dev_err(dev, "scsi_host_alloc failed\n");
  1486. err = -ENOMEM;
  1487. goto out_error;
  1488. }
  1489. hba = shost_priv(host);
  1490. hba->host = host;
  1491. hba->dev = dev;
  1492. hba->mmio_base = mmio_base;
  1493. hba->irq = irq;
  1494. /* Read capabilities registers */
  1495. ufshcd_hba_capabilities(hba);
  1496. /* Get UFS version supported by the controller */
  1497. hba->ufs_version = ufshcd_get_ufs_version(hba);
  1498. /* Get Interrupt bit mask per version */
  1499. hba->intr_mask = ufshcd_get_intr_mask(hba);
  1500. /* Allocate memory for host memory space */
  1501. err = ufshcd_memory_alloc(hba);
  1502. if (err) {
  1503. dev_err(hba->dev, "Memory allocation failed\n");
  1504. goto out_disable;
  1505. }
  1506. /* Configure LRB */
  1507. ufshcd_host_memory_configure(hba);
  1508. host->can_queue = hba->nutrs;
  1509. host->cmd_per_lun = hba->nutrs;
  1510. host->max_id = UFSHCD_MAX_ID;
  1511. host->max_lun = UFSHCD_MAX_LUNS;
  1512. host->max_channel = UFSHCD_MAX_CHANNEL;
  1513. host->unique_id = host->host_no;
  1514. host->max_cmd_len = MAX_CDB_SIZE;
  1515. /* Initailize wait queue for task management */
  1516. init_waitqueue_head(&hba->ufshcd_tm_wait_queue);
  1517. /* Initialize work queues */
  1518. INIT_WORK(&hba->feh_workq, ufshcd_fatal_err_handler);
  1519. /* Initialize UIC command mutex */
  1520. mutex_init(&hba->uic_cmd_mutex);
  1521. /* IRQ registration */
  1522. err = devm_request_irq(dev, irq, ufshcd_intr, IRQF_SHARED, UFSHCD, hba);
  1523. if (err) {
  1524. dev_err(hba->dev, "request irq failed\n");
  1525. goto out_disable;
  1526. }
  1527. /* Enable SCSI tag mapping */
  1528. err = scsi_init_shared_tag_map(host, host->can_queue);
  1529. if (err) {
  1530. dev_err(hba->dev, "init shared queue failed\n");
  1531. goto out_disable;
  1532. }
  1533. err = scsi_add_host(host, hba->dev);
  1534. if (err) {
  1535. dev_err(hba->dev, "scsi_add_host failed\n");
  1536. goto out_disable;
  1537. }
  1538. /* Host controller enable */
  1539. err = ufshcd_hba_enable(hba);
  1540. if (err) {
  1541. dev_err(hba->dev, "Host controller enable failed\n");
  1542. goto out_remove_scsi_host;
  1543. }
  1544. *hba_handle = hba;
  1545. /*
  1546. * The device-initialize-sequence hasn't been invoked yet.
  1547. * Set the device to power-off state
  1548. */
  1549. ufshcd_set_ufs_dev_poweroff(hba);
  1550. async_schedule(ufshcd_async_scan, hba);
  1551. return 0;
  1552. out_remove_scsi_host:
  1553. scsi_remove_host(hba->host);
  1554. out_disable:
  1555. scsi_host_put(host);
  1556. out_error:
  1557. return err;
  1558. }
  1559. EXPORT_SYMBOL_GPL(ufshcd_init);
  1560. MODULE_AUTHOR("Santosh Yaragnavi <santosh.sy@samsung.com>");
  1561. MODULE_AUTHOR("Vinayak Holikatti <h.vinayak@samsung.com>");
  1562. MODULE_DESCRIPTION("Generic UFS host controller driver Core");
  1563. MODULE_LICENSE("GPL");
  1564. MODULE_VERSION(UFSHCD_DRIVER_VERSION);