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