ufshcd.c 44 KB

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