ufshcd.c 51 KB

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