46 #include <linux/module.h>
47 #include <linux/kernel.h>
49 #include <linux/pci.h>
53 #include <linux/slab.h>
56 #include <linux/errno.h>
57 #include <linux/types.h>
58 #include <linux/wait.h>
59 #include <linux/bitops.h>
62 #include <asm/byteorder.h>
63 #include <scsi/scsi.h>
73 #define UFSHCD "ufshcd"
74 #define UFSHCD_DRIVER_VERSION "0.1"
225 static inline u32 ufshcd_get_ufs_version(
struct ufs_hba *hba)
237 static inline int ufshcd_is_device_present(
u32 reg_hcs)
249 static inline int ufshcd_get_tr_ocs(
struct ufshcd_lrb *lrbp)
274 static inline int ufshcd_get_tm_free_slot(
struct ufs_hba *hba)
284 static inline void ufshcd_utrl_clear(
struct ufs_hba *hba,
u32 pos)
296 static inline int ufshcd_get_lists_status(
u32 reg)
309 return (((reg) & (0xFF)) >> 1) ^ (0x07);
319 static inline int ufshcd_get_uic_cmd_result(
struct ufs_hba *hba)
330 static inline void ufshcd_free_hba_memory(
struct ufs_hba *hba)
332 size_t utmrdl_size, utrdl_size, ucdl_size;
366 ufshcd_is_valid_req_rsp(
struct utp_upiu_rsp *ucd_rsp_ptr)
380 ufshcd_get_rsp_upiu_result(
struct utp_upiu_rsp *ucd_rsp_ptr)
421 static void ufshcd_enable_run_stop_reg(
struct ufs_hba *hba)
435 static inline void ufshcd_hba_stop(
struct ufs_hba *hba)
444 static inline void ufshcd_hba_start(
struct ufs_hba *hba)
455 static inline int ufshcd_is_hba_active(
struct ufs_hba *hba)
477 static inline void ufshcd_copy_sense_data(
struct ufshcd_lrb *lrbp)
492 static inline void ufshcd_hba_capabilities(
struct ufs_hba *hba)
530 static int ufshcd_map_sg(
struct ufshcd_lrb *lrbp)
569 static void ufshcd_int_config(
struct ufs_hba *hba,
u32 option)
591 static void ufshcd_compose_upiu(
struct ufshcd_lrb *lrbp)
615 req_desc->
header.dword_0 =
623 req_desc->
header.dword_2 =
627 ucd_cmd_ptr->
header.dword_0 =
632 ucd_cmd_ptr->
header.dword_1 =
640 ucd_cmd_ptr->
header.dword_2 = 0;
647 (
min_t(
unsigned short,
675 hba = shost_priv(host);
695 ufshcd_compose_upiu(lrbp);
696 err = ufshcd_map_sg(lrbp);
702 ufshcd_send_command(hba, tag);
703 spin_unlock_irqrestore(hba->
host->host_lock, flags);
721 static int ufshcd_memory_alloc(
struct ufs_hba *hba)
723 size_t utmrdl_size, utrdl_size, ucdl_size;
741 "Command Descriptor Memory allocation failed\n");
757 "Transfer Descriptor Memory allocation failed\n");
773 "Task Management Descriptor Memory allocation failed\n");
780 dev_err(&hba->
pdev->dev,
"LRB Memory allocation failed\n");
785 ufshcd_free_hba_memory(hba);
802 static void ufshcd_host_memory_configure(
struct ufs_hba *hba)
824 for (i = 0; i < hba->
nutrs; i++) {
826 cmd_desc_element_addr =
827 (cmd_desc_dma_addr + (cmd_desc_size *
i));
841 hba->
lrb[
i].utr_descriptor_ptr = (utrdlp +
i);
842 hba->
lrb[
i].ucd_cmd_ptr =
844 hba->
lrb[
i].ucd_rsp_ptr =
846 hba->
lrb[
i].ucd_prdt_ptr =
862 static int ufshcd_dme_link_startup(
struct ufs_hba *hba)
871 "Controller not ready"
872 " to accept UIC commands\n");
890 ufshcd_send_uic_command(hba, uic_cmd);
891 spin_unlock_irqrestore(hba->
host->host_lock, flags);
907 static int ufshcd_make_hba_operational(
struct ufs_hba *hba)
914 if (!ufshcd_is_device_present(reg)) {
915 dev_err(&hba->
pdev->dev,
"cc: Device not present\n");
924 if (!(ufshcd_get_lists_status(reg))) {
925 ufshcd_enable_run_stop_reg(hba);
928 "Host controller not ready to process requests");
964 static int ufshcd_hba_enable(
struct ufs_hba *hba)
974 if (!ufshcd_is_hba_active(hba)) {
977 ufshcd_hba_stop(hba);
988 ufshcd_hba_start(hba);
1004 while (ufshcd_is_hba_active(hba)) {
1009 "Controller enable failed\n");
1029 static int ufshcd_initialize_hba(
struct ufs_hba *hba)
1031 if (ufshcd_hba_enable(hba))
1045 return ufshcd_dme_link_startup(hba);
1054 static int ufshcd_do_reset(
struct ufs_hba *hba)
1057 unsigned long flags;
1067 ufshcd_hba_stop(hba);
1068 spin_unlock_irqrestore(hba->
host->host_lock, flags);
1071 for (tag = 0; tag < hba->
nutrs; tag++) {
1076 lrbp->
cmd->scsi_done(lrbp->
cmd);
1086 if (ufshcd_initialize_hba(hba)) {
1088 "Reset: Controller initialization failed\n");
1100 static int ufshcd_slave_alloc(
struct scsi_device *sdev)
1104 hba = shost_priv(sdev->
host);
1118 scsi_activate_tcq(sdev, hba->
nutrs);
1126 static void ufshcd_slave_destroy(
struct scsi_device *sdev)
1130 hba = shost_priv(sdev->
host);
1131 scsi_deactivate_tcq(sdev, hba->
nutrs);
1145 unsigned long flags;
1155 ocs_value = ufshcd_get_tmr_ocs(&task_req_descp[index]);
1159 task_req_descp[index].task_rsp_upiu;
1171 "trc: Invalid ocs = %x\n", ocs_value);
1173 spin_unlock_irqrestore(hba->
host->host_lock, flags);
1182 static void ufshcd_adjust_lun_qdepth(
struct scsi_cmnd *cmd)
1188 hba = shost_priv(cmd->
device->host);
1194 for (i = 0; i < hba->
nutrs; i++) {
1201 if (cmd->
device->lun == hba->
lrb[i].lun)
1225 switch (scsi_status) {
1235 ufshcd_copy_sense_data(lrbp);
1247 ufshcd_adjust_lun_qdepth(lrbp->
cmd);
1276 ocs = ufshcd_get_tr_ocs(lrbp);
1282 result = ufshcd_is_valid_req_rsp(lrbp->
ucd_rsp_ptr);
1285 "Invalid response = %x\n", result);
1293 result = ufshcd_get_rsp_upiu_result(lrbp->
ucd_rsp_ptr);
1300 result = ufshcd_scsi_cmd_status(lrbp, scsi_status);
1314 "OCS error from controller = %x\n", ocs);
1325 static void ufshcd_transfer_req_compl(
struct ufs_hba *hba)
1328 unsigned long completed_reqs;
1338 for (index = 0; index < hba->
nutrs; index++) {
1339 if (
test_bit(index, &completed_reqs)) {
1341 result = ufshcd_transfer_rsp_status(hba, &lrb[index]);
1343 if (lrb[index].cmd) {
1346 lrb[
index].
cmd->scsi_done(lrb[index].cmd);
1374 !(ufshcd_get_uic_cmd_result(hba))) {
1376 if (ufshcd_make_hba_operational(hba))
1378 "cc: hba not operational state\n");
1387 static void ufshcd_fatal_err_handler(
struct work_struct *work)
1394 ufshcd_do_reset(hba);
1401 static void ufshcd_err_handler(
struct ufs_hba *hba)
1425 static void ufshcd_tmc_handler(
struct ufs_hba *hba)
1439 static void ufshcd_sl_intr(
struct ufs_hba *hba,
u32 intr_status)
1443 ufshcd_err_handler(hba);
1449 ufshcd_tmc_handler(hba);
1452 ufshcd_transfer_req_compl(hba);
1463 static irqreturn_t ufshcd_intr(
int irq,
void *__hba)
1469 spin_lock(hba->
host->host_lock);
1473 ufshcd_sl_intr(hba, intr_status);
1481 spin_unlock(hba->
host->host_lock);
1493 ufshcd_issue_tm_cmd(
struct ufs_hba *hba,
1500 unsigned long flags;
1509 free_slot = ufshcd_get_tm_free_slot(hba);
1510 if (free_slot >= hba->
nutmrs) {
1511 spin_unlock_irqrestore(host->
host_lock, flags);
1512 dev_err(&hba->
pdev->dev,
"Task management queue full\n");
1522 task_req_descp->
header.dword_2 =
1528 task_req_upiup->
header.dword_0 =
1531 task_req_upiup->
header.dword_1 =
1546 spin_unlock_irqrestore(host->
host_lock, flags);
1556 "Task management command timed-out\n");
1561 err = ufshcd_task_req_compl(hba, free_slot);
1572 static int ufshcd_device_reset(
struct scsi_cmnd *cmd)
1580 host = cmd->
device->host;
1581 hba = shost_priv(host);
1588 for (pos = 0; pos < hba->
nutrs; pos++) {
1590 (hba->
lrb[tag].lun == hba->
lrb[pos].lun)) {
1593 ufshcd_utrl_clear(hba, pos);
1597 if (hba->
lrb[pos].cmd) {
1599 hba->
lrb[
pos].cmd->result =
1601 hba->
lrb[
pos].cmd->scsi_done(cmd);
1616 static int ufshcd_host_reset(
struct scsi_cmnd *cmd)
1620 hba = shost_priv(cmd->
device->host);
1625 return ufshcd_do_reset(hba);
1634 static int ufshcd_abort(
struct scsi_cmnd *cmd)
1638 unsigned long flags;
1642 host = cmd->
device->host;
1643 hba = shost_priv(host);
1651 spin_unlock_irqrestore(host->
host_lock, flags);
1654 spin_unlock_irqrestore(host->
host_lock, flags);
1665 ufshcd_utrl_clear(hba, tag);
1669 spin_unlock_irqrestore(host->
host_lock, flags);
1678 .queuecommand = ufshcd_queuecommand,
1679 .slave_alloc = ufshcd_slave_alloc,
1680 .slave_destroy = ufshcd_slave_destroy,
1681 .eh_abort_handler = ufshcd_abort,
1682 .eh_device_reset_handler = ufshcd_device_reset,
1683 .eh_host_reset_handler = ufshcd_host_reset,
1694 static void ufshcd_shutdown(
struct pci_dev *pdev)
1696 ufshcd_hba_stop((
struct ufs_hba *)pci_get_drvdata(pdev));
1727 static int ufshcd_resume(
struct pci_dev *pdev)
1747 static void ufshcd_hba_free(
struct ufs_hba *hba)
1750 ufshcd_free_hba_memory(hba);
1759 static void ufshcd_remove(
struct pci_dev *pdev)
1761 struct ufs_hba *hba = pci_get_drvdata(pdev);
1767 ufshcd_hba_stop(hba);
1768 ufshcd_hba_free(hba);
1772 pci_set_drvdata(pdev,
NULL);
1784 static int ufshcd_set_dma_mask(
struct ufs_hba *hba)
1798 err = pci_set_dma_mask(hba->
pdev, dma_mask);
1802 err = pci_set_consistent_dma_mask(hba->
pdev, dma_mask);
1823 dev_err(&pdev->
dev,
"pci_enable_device failed\n");
1832 dev_err(&pdev->
dev,
"scsi_host_alloc failed\n");
1836 hba = shost_priv(host);
1840 dev_err(&pdev->
dev,
"request regions failed\n");
1848 goto out_release_regions;
1855 ufshcd_hba_capabilities(hba);
1860 err = ufshcd_set_dma_mask(hba);
1862 dev_err(&pdev->
dev,
"set dma mask failed\n");
1867 err = ufshcd_memory_alloc(hba);
1869 dev_err(&pdev->
dev,
"Memory allocation failed\n");
1874 ufshcd_host_memory_configure(hba);
1899 err = scsi_init_shared_tag_map(host, host->
can_queue);
1901 dev_err(&pdev->
dev,
"init shared queue failed\n");
1905 pci_set_drvdata(pdev, hba);
1907 err = scsi_add_host(host, &pdev->
dev);
1909 dev_err(&pdev->
dev,
"scsi_add_host failed\n");
1914 err = ufshcd_initialize_hba(hba);
1916 dev_err(&pdev->
dev,
"Initialization failed\n");
1925 ufshcd_free_hba_memory(hba);
1928 out_release_regions:
1946 static struct pci_driver ufshcd_pci_driver = {
1948 .id_table = ufshcd_pci_tbl,
1949 .probe = ufshcd_probe,
1951 .shutdown = ufshcd_shutdown,
1953 .suspend = ufshcd_suspend,
1954 .resume = ufshcd_resume,