ufshcd.c 44 KB

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