diff options
author | Linus Torvalds <torvalds@linux-foundation.org> | 2014-02-05 18:53:26 -0500 |
---|---|---|
committer | Linus Torvalds <torvalds@linux-foundation.org> | 2014-02-05 18:53:26 -0500 |
commit | 8352650a5c1a3cd75476a25aaae8b1c6ade1c3f8 (patch) | |
tree | 85abee4f2216f5f5d806a76c35def6e90be45d82 /drivers/block | |
parent | 71c27a8c67e85b216f150696b04f698bff9256fa (diff) | |
parent | 9ac27090f61ea6735a62b0a98c7669c833bcdc09 (diff) |
Merge git://git.infradead.org/users/willy/linux-nvme
Pull NVMe driver update from Matthew Wilcox:
"Looks like I missed the merge window ... but these are almost all
bugfixes anyway (the ones that aren't have been baking for months)"
* git://git.infradead.org/users/willy/linux-nvme:
NVMe: Namespace use after free on surprise removal
NVMe: Correct uses of INIT_WORK
NVMe: Include device and queue numbers in interrupt name
NVMe: Add a pci_driver shutdown method
NVMe: Disable admin queue on init failure
NVMe: Dynamically allocate partition numbers
NVMe: Async IO queue deletion
NVMe: Surprise removal handling
NVMe: Abort timed out commands
NVMe: Schedule reset for failed controllers
NVMe: Device resume error handling
NVMe: Cache dev->pci_dev in a local pointer
NVMe: Fix lockdep warnings
NVMe: compat SG_IO ioctl
NVMe: remove deprecated IRQF_DISABLED
NVMe: Avoid shift operation when writing cq head doorbell
Diffstat (limited to 'drivers/block')
-rw-r--r-- | drivers/block/nvme-core.c | 610 | ||||
-rw-r--r-- | drivers/block/nvme-scsi.c | 147 |
2 files changed, 652 insertions, 105 deletions
diff --git a/drivers/block/nvme-core.c b/drivers/block/nvme-core.c index 1f14ac403945..51824d1f23ea 100644 --- a/drivers/block/nvme-core.c +++ b/drivers/block/nvme-core.c | |||
@@ -46,7 +46,6 @@ | |||
46 | #define NVME_Q_DEPTH 1024 | 46 | #define NVME_Q_DEPTH 1024 |
47 | #define SQ_SIZE(depth) (depth * sizeof(struct nvme_command)) | 47 | #define SQ_SIZE(depth) (depth * sizeof(struct nvme_command)) |
48 | #define CQ_SIZE(depth) (depth * sizeof(struct nvme_completion)) | 48 | #define CQ_SIZE(depth) (depth * sizeof(struct nvme_completion)) |
49 | #define NVME_MINORS 64 | ||
50 | #define ADMIN_TIMEOUT (60 * HZ) | 49 | #define ADMIN_TIMEOUT (60 * HZ) |
51 | 50 | ||
52 | static int nvme_major; | 51 | static int nvme_major; |
@@ -58,6 +57,17 @@ module_param(use_threaded_interrupts, int, 0); | |||
58 | static DEFINE_SPINLOCK(dev_list_lock); | 57 | static DEFINE_SPINLOCK(dev_list_lock); |
59 | static LIST_HEAD(dev_list); | 58 | static LIST_HEAD(dev_list); |
60 | static struct task_struct *nvme_thread; | 59 | static struct task_struct *nvme_thread; |
60 | static struct workqueue_struct *nvme_workq; | ||
61 | |||
62 | static void nvme_reset_failed_dev(struct work_struct *ws); | ||
63 | |||
64 | struct async_cmd_info { | ||
65 | struct kthread_work work; | ||
66 | struct kthread_worker *worker; | ||
67 | u32 result; | ||
68 | int status; | ||
69 | void *ctx; | ||
70 | }; | ||
61 | 71 | ||
62 | /* | 72 | /* |
63 | * An NVM Express queue. Each device has at least two (one for admin | 73 | * An NVM Express queue. Each device has at least two (one for admin |
@@ -66,6 +76,7 @@ static struct task_struct *nvme_thread; | |||
66 | struct nvme_queue { | 76 | struct nvme_queue { |
67 | struct device *q_dmadev; | 77 | struct device *q_dmadev; |
68 | struct nvme_dev *dev; | 78 | struct nvme_dev *dev; |
79 | char irqname[24]; /* nvme4294967295-65535\0 */ | ||
69 | spinlock_t q_lock; | 80 | spinlock_t q_lock; |
70 | struct nvme_command *sq_cmds; | 81 | struct nvme_command *sq_cmds; |
71 | volatile struct nvme_completion *cqes; | 82 | volatile struct nvme_completion *cqes; |
@@ -80,9 +91,11 @@ struct nvme_queue { | |||
80 | u16 sq_head; | 91 | u16 sq_head; |
81 | u16 sq_tail; | 92 | u16 sq_tail; |
82 | u16 cq_head; | 93 | u16 cq_head; |
94 | u16 qid; | ||
83 | u8 cq_phase; | 95 | u8 cq_phase; |
84 | u8 cqe_seen; | 96 | u8 cqe_seen; |
85 | u8 q_suspended; | 97 | u8 q_suspended; |
98 | struct async_cmd_info cmdinfo; | ||
86 | unsigned long cmdid_data[]; | 99 | unsigned long cmdid_data[]; |
87 | }; | 100 | }; |
88 | 101 | ||
@@ -97,6 +110,7 @@ static inline void _nvme_check_size(void) | |||
97 | BUILD_BUG_ON(sizeof(struct nvme_delete_queue) != 64); | 110 | BUILD_BUG_ON(sizeof(struct nvme_delete_queue) != 64); |
98 | BUILD_BUG_ON(sizeof(struct nvme_features) != 64); | 111 | BUILD_BUG_ON(sizeof(struct nvme_features) != 64); |
99 | BUILD_BUG_ON(sizeof(struct nvme_format_cmd) != 64); | 112 | BUILD_BUG_ON(sizeof(struct nvme_format_cmd) != 64); |
113 | BUILD_BUG_ON(sizeof(struct nvme_abort_cmd) != 64); | ||
100 | BUILD_BUG_ON(sizeof(struct nvme_command) != 64); | 114 | BUILD_BUG_ON(sizeof(struct nvme_command) != 64); |
101 | BUILD_BUG_ON(sizeof(struct nvme_id_ctrl) != 4096); | 115 | BUILD_BUG_ON(sizeof(struct nvme_id_ctrl) != 4096); |
102 | BUILD_BUG_ON(sizeof(struct nvme_id_ns) != 4096); | 116 | BUILD_BUG_ON(sizeof(struct nvme_id_ns) != 4096); |
@@ -111,6 +125,7 @@ struct nvme_cmd_info { | |||
111 | nvme_completion_fn fn; | 125 | nvme_completion_fn fn; |
112 | void *ctx; | 126 | void *ctx; |
113 | unsigned long timeout; | 127 | unsigned long timeout; |
128 | int aborted; | ||
114 | }; | 129 | }; |
115 | 130 | ||
116 | static struct nvme_cmd_info *nvme_cmd_info(struct nvme_queue *nvmeq) | 131 | static struct nvme_cmd_info *nvme_cmd_info(struct nvme_queue *nvmeq) |
@@ -154,6 +169,7 @@ static int alloc_cmdid(struct nvme_queue *nvmeq, void *ctx, | |||
154 | info[cmdid].fn = handler; | 169 | info[cmdid].fn = handler; |
155 | info[cmdid].ctx = ctx; | 170 | info[cmdid].ctx = ctx; |
156 | info[cmdid].timeout = jiffies + timeout; | 171 | info[cmdid].timeout = jiffies + timeout; |
172 | info[cmdid].aborted = 0; | ||
157 | return cmdid; | 173 | return cmdid; |
158 | } | 174 | } |
159 | 175 | ||
@@ -172,6 +188,7 @@ static int alloc_cmdid_killable(struct nvme_queue *nvmeq, void *ctx, | |||
172 | #define CMD_CTX_COMPLETED (0x310 + CMD_CTX_BASE) | 188 | #define CMD_CTX_COMPLETED (0x310 + CMD_CTX_BASE) |
173 | #define CMD_CTX_INVALID (0x314 + CMD_CTX_BASE) | 189 | #define CMD_CTX_INVALID (0x314 + CMD_CTX_BASE) |
174 | #define CMD_CTX_FLUSH (0x318 + CMD_CTX_BASE) | 190 | #define CMD_CTX_FLUSH (0x318 + CMD_CTX_BASE) |
191 | #define CMD_CTX_ABORT (0x31C + CMD_CTX_BASE) | ||
175 | 192 | ||
176 | static void special_completion(struct nvme_dev *dev, void *ctx, | 193 | static void special_completion(struct nvme_dev *dev, void *ctx, |
177 | struct nvme_completion *cqe) | 194 | struct nvme_completion *cqe) |
@@ -180,6 +197,10 @@ static void special_completion(struct nvme_dev *dev, void *ctx, | |||
180 | return; | 197 | return; |
181 | if (ctx == CMD_CTX_FLUSH) | 198 | if (ctx == CMD_CTX_FLUSH) |
182 | return; | 199 | return; |
200 | if (ctx == CMD_CTX_ABORT) { | ||
201 | ++dev->abort_limit; | ||
202 | return; | ||
203 | } | ||
183 | if (ctx == CMD_CTX_COMPLETED) { | 204 | if (ctx == CMD_CTX_COMPLETED) { |
184 | dev_warn(&dev->pci_dev->dev, | 205 | dev_warn(&dev->pci_dev->dev, |
185 | "completed id %d twice on queue %d\n", | 206 | "completed id %d twice on queue %d\n", |
@@ -196,6 +217,15 @@ static void special_completion(struct nvme_dev *dev, void *ctx, | |||
196 | dev_warn(&dev->pci_dev->dev, "Unknown special completion %p\n", ctx); | 217 | dev_warn(&dev->pci_dev->dev, "Unknown special completion %p\n", ctx); |
197 | } | 218 | } |
198 | 219 | ||
220 | static void async_completion(struct nvme_dev *dev, void *ctx, | ||
221 | struct nvme_completion *cqe) | ||
222 | { | ||
223 | struct async_cmd_info *cmdinfo = ctx; | ||
224 | cmdinfo->result = le32_to_cpup(&cqe->result); | ||
225 | cmdinfo->status = le16_to_cpup(&cqe->status) >> 1; | ||
226 | queue_kthread_work(cmdinfo->worker, &cmdinfo->work); | ||
227 | } | ||
228 | |||
199 | /* | 229 | /* |
200 | * Called with local interrupts disabled and the q_lock held. May not sleep. | 230 | * Called with local interrupts disabled and the q_lock held. May not sleep. |
201 | */ | 231 | */ |
@@ -693,7 +723,7 @@ static int nvme_process_cq(struct nvme_queue *nvmeq) | |||
693 | if (head == nvmeq->cq_head && phase == nvmeq->cq_phase) | 723 | if (head == nvmeq->cq_head && phase == nvmeq->cq_phase) |
694 | return 0; | 724 | return 0; |
695 | 725 | ||
696 | writel(head, nvmeq->q_db + (1 << nvmeq->dev->db_stride)); | 726 | writel(head, nvmeq->q_db + nvmeq->dev->db_stride); |
697 | nvmeq->cq_head = head; | 727 | nvmeq->cq_head = head; |
698 | nvmeq->cq_phase = phase; | 728 | nvmeq->cq_phase = phase; |
699 | 729 | ||
@@ -804,12 +834,34 @@ int nvme_submit_sync_cmd(struct nvme_queue *nvmeq, struct nvme_command *cmd, | |||
804 | return cmdinfo.status; | 834 | return cmdinfo.status; |
805 | } | 835 | } |
806 | 836 | ||
837 | static int nvme_submit_async_cmd(struct nvme_queue *nvmeq, | ||
838 | struct nvme_command *cmd, | ||
839 | struct async_cmd_info *cmdinfo, unsigned timeout) | ||
840 | { | ||
841 | int cmdid; | ||
842 | |||
843 | cmdid = alloc_cmdid_killable(nvmeq, cmdinfo, async_completion, timeout); | ||
844 | if (cmdid < 0) | ||
845 | return cmdid; | ||
846 | cmdinfo->status = -EINTR; | ||
847 | cmd->common.command_id = cmdid; | ||
848 | nvme_submit_cmd(nvmeq, cmd); | ||
849 | return 0; | ||
850 | } | ||
851 | |||
807 | int nvme_submit_admin_cmd(struct nvme_dev *dev, struct nvme_command *cmd, | 852 | int nvme_submit_admin_cmd(struct nvme_dev *dev, struct nvme_command *cmd, |
808 | u32 *result) | 853 | u32 *result) |
809 | { | 854 | { |
810 | return nvme_submit_sync_cmd(dev->queues[0], cmd, result, ADMIN_TIMEOUT); | 855 | return nvme_submit_sync_cmd(dev->queues[0], cmd, result, ADMIN_TIMEOUT); |
811 | } | 856 | } |
812 | 857 | ||
858 | static int nvme_submit_admin_cmd_async(struct nvme_dev *dev, | ||
859 | struct nvme_command *cmd, struct async_cmd_info *cmdinfo) | ||
860 | { | ||
861 | return nvme_submit_async_cmd(dev->queues[0], cmd, cmdinfo, | ||
862 | ADMIN_TIMEOUT); | ||
863 | } | ||
864 | |||
813 | static int adapter_delete_queue(struct nvme_dev *dev, u8 opcode, u16 id) | 865 | static int adapter_delete_queue(struct nvme_dev *dev, u8 opcode, u16 id) |
814 | { | 866 | { |
815 | int status; | 867 | int status; |
@@ -920,6 +972,56 @@ int nvme_set_features(struct nvme_dev *dev, unsigned fid, unsigned dword11, | |||
920 | } | 972 | } |
921 | 973 | ||
922 | /** | 974 | /** |
975 | * nvme_abort_cmd - Attempt aborting a command | ||
976 | * @cmdid: Command id of a timed out IO | ||
977 | * @queue: The queue with timed out IO | ||
978 | * | ||
979 | * Schedule controller reset if the command was already aborted once before and | ||
980 | * still hasn't been returned to the driver, or if this is the admin queue. | ||
981 | */ | ||
982 | static void nvme_abort_cmd(int cmdid, struct nvme_queue *nvmeq) | ||
983 | { | ||
984 | int a_cmdid; | ||
985 | struct nvme_command cmd; | ||
986 | struct nvme_dev *dev = nvmeq->dev; | ||
987 | struct nvme_cmd_info *info = nvme_cmd_info(nvmeq); | ||
988 | |||
989 | if (!nvmeq->qid || info[cmdid].aborted) { | ||
990 | if (work_busy(&dev->reset_work)) | ||
991 | return; | ||
992 | list_del_init(&dev->node); | ||
993 | dev_warn(&dev->pci_dev->dev, | ||
994 | "I/O %d QID %d timeout, reset controller\n", cmdid, | ||
995 | nvmeq->qid); | ||
996 | PREPARE_WORK(&dev->reset_work, nvme_reset_failed_dev); | ||
997 | queue_work(nvme_workq, &dev->reset_work); | ||
998 | return; | ||
999 | } | ||
1000 | |||
1001 | if (!dev->abort_limit) | ||
1002 | return; | ||
1003 | |||
1004 | a_cmdid = alloc_cmdid(dev->queues[0], CMD_CTX_ABORT, special_completion, | ||
1005 | ADMIN_TIMEOUT); | ||
1006 | if (a_cmdid < 0) | ||
1007 | return; | ||
1008 | |||
1009 | memset(&cmd, 0, sizeof(cmd)); | ||
1010 | cmd.abort.opcode = nvme_admin_abort_cmd; | ||
1011 | cmd.abort.cid = cmdid; | ||
1012 | cmd.abort.sqid = cpu_to_le16(nvmeq->qid); | ||
1013 | cmd.abort.command_id = a_cmdid; | ||
1014 | |||
1015 | --dev->abort_limit; | ||
1016 | info[cmdid].aborted = 1; | ||
1017 | info[cmdid].timeout = jiffies + ADMIN_TIMEOUT; | ||
1018 | |||
1019 | dev_warn(nvmeq->q_dmadev, "Aborting I/O %d QID %d\n", cmdid, | ||
1020 | nvmeq->qid); | ||
1021 | nvme_submit_cmd(dev->queues[0], &cmd); | ||
1022 | } | ||
1023 | |||
1024 | /** | ||
923 | * nvme_cancel_ios - Cancel outstanding I/Os | 1025 | * nvme_cancel_ios - Cancel outstanding I/Os |
924 | * @queue: The queue to cancel I/Os on | 1026 | * @queue: The queue to cancel I/Os on |
925 | * @timeout: True to only cancel I/Os which have timed out | 1027 | * @timeout: True to only cancel I/Os which have timed out |
@@ -942,7 +1044,12 @@ static void nvme_cancel_ios(struct nvme_queue *nvmeq, bool timeout) | |||
942 | continue; | 1044 | continue; |
943 | if (info[cmdid].ctx == CMD_CTX_CANCELLED) | 1045 | if (info[cmdid].ctx == CMD_CTX_CANCELLED) |
944 | continue; | 1046 | continue; |
945 | dev_warn(nvmeq->q_dmadev, "Cancelling I/O %d\n", cmdid); | 1047 | if (timeout && nvmeq->dev->initialized) { |
1048 | nvme_abort_cmd(cmdid, nvmeq); | ||
1049 | continue; | ||
1050 | } | ||
1051 | dev_warn(nvmeq->q_dmadev, "Cancelling I/O %d QID %d\n", cmdid, | ||
1052 | nvmeq->qid); | ||
946 | ctx = cancel_cmdid(nvmeq, cmdid, &fn); | 1053 | ctx = cancel_cmdid(nvmeq, cmdid, &fn); |
947 | fn(nvmeq->dev, ctx, &cqe); | 1054 | fn(nvmeq->dev, ctx, &cqe); |
948 | } | 1055 | } |
@@ -964,26 +1071,31 @@ static void nvme_free_queue(struct nvme_queue *nvmeq) | |||
964 | kfree(nvmeq); | 1071 | kfree(nvmeq); |
965 | } | 1072 | } |
966 | 1073 | ||
967 | static void nvme_free_queues(struct nvme_dev *dev) | 1074 | static void nvme_free_queues(struct nvme_dev *dev, int lowest) |
968 | { | 1075 | { |
969 | int i; | 1076 | int i; |
970 | 1077 | ||
971 | for (i = dev->queue_count - 1; i >= 0; i--) { | 1078 | for (i = dev->queue_count - 1; i >= lowest; i--) { |
972 | nvme_free_queue(dev->queues[i]); | 1079 | nvme_free_queue(dev->queues[i]); |
973 | dev->queue_count--; | 1080 | dev->queue_count--; |
974 | dev->queues[i] = NULL; | 1081 | dev->queues[i] = NULL; |
975 | } | 1082 | } |
976 | } | 1083 | } |
977 | 1084 | ||
978 | static void nvme_disable_queue(struct nvme_dev *dev, int qid) | 1085 | /** |
1086 | * nvme_suspend_queue - put queue into suspended state | ||
1087 | * @nvmeq - queue to suspend | ||
1088 | * | ||
1089 | * Returns 1 if already suspended, 0 otherwise. | ||
1090 | */ | ||
1091 | static int nvme_suspend_queue(struct nvme_queue *nvmeq) | ||
979 | { | 1092 | { |
980 | struct nvme_queue *nvmeq = dev->queues[qid]; | 1093 | int vector = nvmeq->dev->entry[nvmeq->cq_vector].vector; |
981 | int vector = dev->entry[nvmeq->cq_vector].vector; | ||
982 | 1094 | ||
983 | spin_lock_irq(&nvmeq->q_lock); | 1095 | spin_lock_irq(&nvmeq->q_lock); |
984 | if (nvmeq->q_suspended) { | 1096 | if (nvmeq->q_suspended) { |
985 | spin_unlock_irq(&nvmeq->q_lock); | 1097 | spin_unlock_irq(&nvmeq->q_lock); |
986 | return; | 1098 | return 1; |
987 | } | 1099 | } |
988 | nvmeq->q_suspended = 1; | 1100 | nvmeq->q_suspended = 1; |
989 | spin_unlock_irq(&nvmeq->q_lock); | 1101 | spin_unlock_irq(&nvmeq->q_lock); |
@@ -991,18 +1103,35 @@ static void nvme_disable_queue(struct nvme_dev *dev, int qid) | |||
991 | irq_set_affinity_hint(vector, NULL); | 1103 | irq_set_affinity_hint(vector, NULL); |
992 | free_irq(vector, nvmeq); | 1104 | free_irq(vector, nvmeq); |
993 | 1105 | ||
994 | /* Don't tell the adapter to delete the admin queue */ | 1106 | return 0; |
995 | if (qid) { | 1107 | } |
996 | adapter_delete_sq(dev, qid); | ||
997 | adapter_delete_cq(dev, qid); | ||
998 | } | ||
999 | 1108 | ||
1109 | static void nvme_clear_queue(struct nvme_queue *nvmeq) | ||
1110 | { | ||
1000 | spin_lock_irq(&nvmeq->q_lock); | 1111 | spin_lock_irq(&nvmeq->q_lock); |
1001 | nvme_process_cq(nvmeq); | 1112 | nvme_process_cq(nvmeq); |
1002 | nvme_cancel_ios(nvmeq, false); | 1113 | nvme_cancel_ios(nvmeq, false); |
1003 | spin_unlock_irq(&nvmeq->q_lock); | 1114 | spin_unlock_irq(&nvmeq->q_lock); |
1004 | } | 1115 | } |
1005 | 1116 | ||
1117 | static void nvme_disable_queue(struct nvme_dev *dev, int qid) | ||
1118 | { | ||
1119 | struct nvme_queue *nvmeq = dev->queues[qid]; | ||
1120 | |||
1121 | if (!nvmeq) | ||
1122 | return; | ||
1123 | if (nvme_suspend_queue(nvmeq)) | ||
1124 | return; | ||
1125 | |||
1126 | /* Don't tell the adapter to delete the admin queue. | ||
1127 | * Don't tell a removed adapter to delete IO queues. */ | ||
1128 | if (qid && readl(&dev->bar->csts) != -1) { | ||
1129 | adapter_delete_sq(dev, qid); | ||
1130 | adapter_delete_cq(dev, qid); | ||
1131 | } | ||
1132 | nvme_clear_queue(nvmeq); | ||
1133 | } | ||
1134 | |||
1006 | static struct nvme_queue *nvme_alloc_queue(struct nvme_dev *dev, int qid, | 1135 | static struct nvme_queue *nvme_alloc_queue(struct nvme_dev *dev, int qid, |
1007 | int depth, int vector) | 1136 | int depth, int vector) |
1008 | { | 1137 | { |
@@ -1025,15 +1154,18 @@ static struct nvme_queue *nvme_alloc_queue(struct nvme_dev *dev, int qid, | |||
1025 | 1154 | ||
1026 | nvmeq->q_dmadev = dmadev; | 1155 | nvmeq->q_dmadev = dmadev; |
1027 | nvmeq->dev = dev; | 1156 | nvmeq->dev = dev; |
1157 | snprintf(nvmeq->irqname, sizeof(nvmeq->irqname), "nvme%dq%d", | ||
1158 | dev->instance, qid); | ||
1028 | spin_lock_init(&nvmeq->q_lock); | 1159 | spin_lock_init(&nvmeq->q_lock); |
1029 | nvmeq->cq_head = 0; | 1160 | nvmeq->cq_head = 0; |
1030 | nvmeq->cq_phase = 1; | 1161 | nvmeq->cq_phase = 1; |
1031 | init_waitqueue_head(&nvmeq->sq_full); | 1162 | init_waitqueue_head(&nvmeq->sq_full); |
1032 | init_waitqueue_entry(&nvmeq->sq_cong_wait, nvme_thread); | 1163 | init_waitqueue_entry(&nvmeq->sq_cong_wait, nvme_thread); |
1033 | bio_list_init(&nvmeq->sq_cong); | 1164 | bio_list_init(&nvmeq->sq_cong); |
1034 | nvmeq->q_db = &dev->dbs[qid << (dev->db_stride + 1)]; | 1165 | nvmeq->q_db = &dev->dbs[qid * 2 * dev->db_stride]; |
1035 | nvmeq->q_depth = depth; | 1166 | nvmeq->q_depth = depth; |
1036 | nvmeq->cq_vector = vector; | 1167 | nvmeq->cq_vector = vector; |
1168 | nvmeq->qid = qid; | ||
1037 | nvmeq->q_suspended = 1; | 1169 | nvmeq->q_suspended = 1; |
1038 | dev->queue_count++; | 1170 | dev->queue_count++; |
1039 | 1171 | ||
@@ -1052,11 +1184,10 @@ static int queue_request_irq(struct nvme_dev *dev, struct nvme_queue *nvmeq, | |||
1052 | { | 1184 | { |
1053 | if (use_threaded_interrupts) | 1185 | if (use_threaded_interrupts) |
1054 | return request_threaded_irq(dev->entry[nvmeq->cq_vector].vector, | 1186 | return request_threaded_irq(dev->entry[nvmeq->cq_vector].vector, |
1055 | nvme_irq_check, nvme_irq, | 1187 | nvme_irq_check, nvme_irq, IRQF_SHARED, |
1056 | IRQF_DISABLED | IRQF_SHARED, | ||
1057 | name, nvmeq); | 1188 | name, nvmeq); |
1058 | return request_irq(dev->entry[nvmeq->cq_vector].vector, nvme_irq, | 1189 | return request_irq(dev->entry[nvmeq->cq_vector].vector, nvme_irq, |
1059 | IRQF_DISABLED | IRQF_SHARED, name, nvmeq); | 1190 | IRQF_SHARED, name, nvmeq); |
1060 | } | 1191 | } |
1061 | 1192 | ||
1062 | static void nvme_init_queue(struct nvme_queue *nvmeq, u16 qid) | 1193 | static void nvme_init_queue(struct nvme_queue *nvmeq, u16 qid) |
@@ -1067,7 +1198,7 @@ static void nvme_init_queue(struct nvme_queue *nvmeq, u16 qid) | |||
1067 | nvmeq->sq_tail = 0; | 1198 | nvmeq->sq_tail = 0; |
1068 | nvmeq->cq_head = 0; | 1199 | nvmeq->cq_head = 0; |
1069 | nvmeq->cq_phase = 1; | 1200 | nvmeq->cq_phase = 1; |
1070 | nvmeq->q_db = &dev->dbs[qid << (dev->db_stride + 1)]; | 1201 | nvmeq->q_db = &dev->dbs[qid * 2 * dev->db_stride]; |
1071 | memset(nvmeq->cmdid_data, 0, extra); | 1202 | memset(nvmeq->cmdid_data, 0, extra); |
1072 | memset((void *)nvmeq->cqes, 0, CQ_SIZE(nvmeq->q_depth)); | 1203 | memset((void *)nvmeq->cqes, 0, CQ_SIZE(nvmeq->q_depth)); |
1073 | nvme_cancel_ios(nvmeq, false); | 1204 | nvme_cancel_ios(nvmeq, false); |
@@ -1087,13 +1218,13 @@ static int nvme_create_queue(struct nvme_queue *nvmeq, int qid) | |||
1087 | if (result < 0) | 1218 | if (result < 0) |
1088 | goto release_cq; | 1219 | goto release_cq; |
1089 | 1220 | ||
1090 | result = queue_request_irq(dev, nvmeq, "nvme"); | 1221 | result = queue_request_irq(dev, nvmeq, nvmeq->irqname); |
1091 | if (result < 0) | 1222 | if (result < 0) |
1092 | goto release_sq; | 1223 | goto release_sq; |
1093 | 1224 | ||
1094 | spin_lock(&nvmeq->q_lock); | 1225 | spin_lock_irq(&nvmeq->q_lock); |
1095 | nvme_init_queue(nvmeq, qid); | 1226 | nvme_init_queue(nvmeq, qid); |
1096 | spin_unlock(&nvmeq->q_lock); | 1227 | spin_unlock_irq(&nvmeq->q_lock); |
1097 | 1228 | ||
1098 | return result; | 1229 | return result; |
1099 | 1230 | ||
@@ -1205,13 +1336,13 @@ static int nvme_configure_admin_queue(struct nvme_dev *dev) | |||
1205 | if (result) | 1336 | if (result) |
1206 | return result; | 1337 | return result; |
1207 | 1338 | ||
1208 | result = queue_request_irq(dev, nvmeq, "nvme admin"); | 1339 | result = queue_request_irq(dev, nvmeq, nvmeq->irqname); |
1209 | if (result) | 1340 | if (result) |
1210 | return result; | 1341 | return result; |
1211 | 1342 | ||
1212 | spin_lock(&nvmeq->q_lock); | 1343 | spin_lock_irq(&nvmeq->q_lock); |
1213 | nvme_init_queue(nvmeq, 0); | 1344 | nvme_init_queue(nvmeq, 0); |
1214 | spin_unlock(&nvmeq->q_lock); | 1345 | spin_unlock_irq(&nvmeq->q_lock); |
1215 | return result; | 1346 | return result; |
1216 | } | 1347 | } |
1217 | 1348 | ||
@@ -1487,10 +1618,47 @@ static int nvme_ioctl(struct block_device *bdev, fmode_t mode, unsigned int cmd, | |||
1487 | } | 1618 | } |
1488 | } | 1619 | } |
1489 | 1620 | ||
1621 | #ifdef CONFIG_COMPAT | ||
1622 | static int nvme_compat_ioctl(struct block_device *bdev, fmode_t mode, | ||
1623 | unsigned int cmd, unsigned long arg) | ||
1624 | { | ||
1625 | struct nvme_ns *ns = bdev->bd_disk->private_data; | ||
1626 | |||
1627 | switch (cmd) { | ||
1628 | case SG_IO: | ||
1629 | return nvme_sg_io32(ns, arg); | ||
1630 | } | ||
1631 | return nvme_ioctl(bdev, mode, cmd, arg); | ||
1632 | } | ||
1633 | #else | ||
1634 | #define nvme_compat_ioctl NULL | ||
1635 | #endif | ||
1636 | |||
1637 | static int nvme_open(struct block_device *bdev, fmode_t mode) | ||
1638 | { | ||
1639 | struct nvme_ns *ns = bdev->bd_disk->private_data; | ||
1640 | struct nvme_dev *dev = ns->dev; | ||
1641 | |||
1642 | kref_get(&dev->kref); | ||
1643 | return 0; | ||
1644 | } | ||
1645 | |||
1646 | static void nvme_free_dev(struct kref *kref); | ||
1647 | |||
1648 | static void nvme_release(struct gendisk *disk, fmode_t mode) | ||
1649 | { | ||
1650 | struct nvme_ns *ns = disk->private_data; | ||
1651 | struct nvme_dev *dev = ns->dev; | ||
1652 | |||
1653 | kref_put(&dev->kref, nvme_free_dev); | ||
1654 | } | ||
1655 | |||
1490 | static const struct block_device_operations nvme_fops = { | 1656 | static const struct block_device_operations nvme_fops = { |
1491 | .owner = THIS_MODULE, | 1657 | .owner = THIS_MODULE, |
1492 | .ioctl = nvme_ioctl, | 1658 | .ioctl = nvme_ioctl, |
1493 | .compat_ioctl = nvme_ioctl, | 1659 | .compat_ioctl = nvme_compat_ioctl, |
1660 | .open = nvme_open, | ||
1661 | .release = nvme_release, | ||
1494 | }; | 1662 | }; |
1495 | 1663 | ||
1496 | static void nvme_resubmit_bios(struct nvme_queue *nvmeq) | 1664 | static void nvme_resubmit_bios(struct nvme_queue *nvmeq) |
@@ -1514,13 +1682,25 @@ static void nvme_resubmit_bios(struct nvme_queue *nvmeq) | |||
1514 | 1682 | ||
1515 | static int nvme_kthread(void *data) | 1683 | static int nvme_kthread(void *data) |
1516 | { | 1684 | { |
1517 | struct nvme_dev *dev; | 1685 | struct nvme_dev *dev, *next; |
1518 | 1686 | ||
1519 | while (!kthread_should_stop()) { | 1687 | while (!kthread_should_stop()) { |
1520 | set_current_state(TASK_INTERRUPTIBLE); | 1688 | set_current_state(TASK_INTERRUPTIBLE); |
1521 | spin_lock(&dev_list_lock); | 1689 | spin_lock(&dev_list_lock); |
1522 | list_for_each_entry(dev, &dev_list, node) { | 1690 | list_for_each_entry_safe(dev, next, &dev_list, node) { |
1523 | int i; | 1691 | int i; |
1692 | if (readl(&dev->bar->csts) & NVME_CSTS_CFS && | ||
1693 | dev->initialized) { | ||
1694 | if (work_busy(&dev->reset_work)) | ||
1695 | continue; | ||
1696 | list_del_init(&dev->node); | ||
1697 | dev_warn(&dev->pci_dev->dev, | ||
1698 | "Failed status, reset controller\n"); | ||
1699 | PREPARE_WORK(&dev->reset_work, | ||
1700 | nvme_reset_failed_dev); | ||
1701 | queue_work(nvme_workq, &dev->reset_work); | ||
1702 | continue; | ||
1703 | } | ||
1524 | for (i = 0; i < dev->queue_count; i++) { | 1704 | for (i = 0; i < dev->queue_count; i++) { |
1525 | struct nvme_queue *nvmeq = dev->queues[i]; | 1705 | struct nvme_queue *nvmeq = dev->queues[i]; |
1526 | if (!nvmeq) | 1706 | if (!nvmeq) |
@@ -1541,33 +1721,6 @@ static int nvme_kthread(void *data) | |||
1541 | return 0; | 1721 | return 0; |
1542 | } | 1722 | } |
1543 | 1723 | ||
1544 | static DEFINE_IDA(nvme_index_ida); | ||
1545 | |||
1546 | static int nvme_get_ns_idx(void) | ||
1547 | { | ||
1548 | int index, error; | ||
1549 | |||
1550 | do { | ||
1551 | if (!ida_pre_get(&nvme_index_ida, GFP_KERNEL)) | ||
1552 | return -1; | ||
1553 | |||
1554 | spin_lock(&dev_list_lock); | ||
1555 | error = ida_get_new(&nvme_index_ida, &index); | ||
1556 | spin_unlock(&dev_list_lock); | ||
1557 | } while (error == -EAGAIN); | ||
1558 | |||
1559 | if (error) | ||
1560 | index = -1; | ||
1561 | return index; | ||
1562 | } | ||
1563 | |||
1564 | static void nvme_put_ns_idx(int index) | ||
1565 | { | ||
1566 | spin_lock(&dev_list_lock); | ||
1567 | ida_remove(&nvme_index_ida, index); | ||
1568 | spin_unlock(&dev_list_lock); | ||
1569 | } | ||
1570 | |||
1571 | static void nvme_config_discard(struct nvme_ns *ns) | 1724 | static void nvme_config_discard(struct nvme_ns *ns) |
1572 | { | 1725 | { |
1573 | u32 logical_block_size = queue_logical_block_size(ns->queue); | 1726 | u32 logical_block_size = queue_logical_block_size(ns->queue); |
@@ -1601,7 +1754,7 @@ static struct nvme_ns *nvme_alloc_ns(struct nvme_dev *dev, unsigned nsid, | |||
1601 | ns->dev = dev; | 1754 | ns->dev = dev; |
1602 | ns->queue->queuedata = ns; | 1755 | ns->queue->queuedata = ns; |
1603 | 1756 | ||
1604 | disk = alloc_disk(NVME_MINORS); | 1757 | disk = alloc_disk(0); |
1605 | if (!disk) | 1758 | if (!disk) |
1606 | goto out_free_queue; | 1759 | goto out_free_queue; |
1607 | ns->ns_id = nsid; | 1760 | ns->ns_id = nsid; |
@@ -1614,12 +1767,12 @@ static struct nvme_ns *nvme_alloc_ns(struct nvme_dev *dev, unsigned nsid, | |||
1614 | blk_queue_max_hw_sectors(ns->queue, dev->max_hw_sectors); | 1767 | blk_queue_max_hw_sectors(ns->queue, dev->max_hw_sectors); |
1615 | 1768 | ||
1616 | disk->major = nvme_major; | 1769 | disk->major = nvme_major; |
1617 | disk->minors = NVME_MINORS; | 1770 | disk->first_minor = 0; |
1618 | disk->first_minor = NVME_MINORS * nvme_get_ns_idx(); | ||
1619 | disk->fops = &nvme_fops; | 1771 | disk->fops = &nvme_fops; |
1620 | disk->private_data = ns; | 1772 | disk->private_data = ns; |
1621 | disk->queue = ns->queue; | 1773 | disk->queue = ns->queue; |
1622 | disk->driverfs_dev = &dev->pci_dev->dev; | 1774 | disk->driverfs_dev = &dev->pci_dev->dev; |
1775 | disk->flags = GENHD_FL_EXT_DEVT; | ||
1623 | sprintf(disk->disk_name, "nvme%dn%d", dev->instance, nsid); | 1776 | sprintf(disk->disk_name, "nvme%dn%d", dev->instance, nsid); |
1624 | set_capacity(disk, le64_to_cpup(&id->nsze) << (ns->lba_shift - 9)); | 1777 | set_capacity(disk, le64_to_cpup(&id->nsze) << (ns->lba_shift - 9)); |
1625 | 1778 | ||
@@ -1635,15 +1788,6 @@ static struct nvme_ns *nvme_alloc_ns(struct nvme_dev *dev, unsigned nsid, | |||
1635 | return NULL; | 1788 | return NULL; |
1636 | } | 1789 | } |
1637 | 1790 | ||
1638 | static void nvme_ns_free(struct nvme_ns *ns) | ||
1639 | { | ||
1640 | int index = ns->disk->first_minor / NVME_MINORS; | ||
1641 | put_disk(ns->disk); | ||
1642 | nvme_put_ns_idx(index); | ||
1643 | blk_cleanup_queue(ns->queue); | ||
1644 | kfree(ns); | ||
1645 | } | ||
1646 | |||
1647 | static int set_queue_count(struct nvme_dev *dev, int count) | 1791 | static int set_queue_count(struct nvme_dev *dev, int count) |
1648 | { | 1792 | { |
1649 | int status; | 1793 | int status; |
@@ -1659,11 +1803,12 @@ static int set_queue_count(struct nvme_dev *dev, int count) | |||
1659 | 1803 | ||
1660 | static size_t db_bar_size(struct nvme_dev *dev, unsigned nr_io_queues) | 1804 | static size_t db_bar_size(struct nvme_dev *dev, unsigned nr_io_queues) |
1661 | { | 1805 | { |
1662 | return 4096 + ((nr_io_queues + 1) << (dev->db_stride + 3)); | 1806 | return 4096 + ((nr_io_queues + 1) * 8 * dev->db_stride); |
1663 | } | 1807 | } |
1664 | 1808 | ||
1665 | static int nvme_setup_io_queues(struct nvme_dev *dev) | 1809 | static int nvme_setup_io_queues(struct nvme_dev *dev) |
1666 | { | 1810 | { |
1811 | struct nvme_queue *adminq = dev->queues[0]; | ||
1667 | struct pci_dev *pdev = dev->pci_dev; | 1812 | struct pci_dev *pdev = dev->pci_dev; |
1668 | int result, cpu, i, vecs, nr_io_queues, size, q_depth; | 1813 | int result, cpu, i, vecs, nr_io_queues, size, q_depth; |
1669 | 1814 | ||
@@ -1690,7 +1835,7 @@ static int nvme_setup_io_queues(struct nvme_dev *dev) | |||
1690 | } | 1835 | } |
1691 | 1836 | ||
1692 | /* Deregister the admin queue's interrupt */ | 1837 | /* Deregister the admin queue's interrupt */ |
1693 | free_irq(dev->entry[0].vector, dev->queues[0]); | 1838 | free_irq(dev->entry[0].vector, adminq); |
1694 | 1839 | ||
1695 | vecs = nr_io_queues; | 1840 | vecs = nr_io_queues; |
1696 | for (i = 0; i < vecs; i++) | 1841 | for (i = 0; i < vecs; i++) |
@@ -1728,9 +1873,9 @@ static int nvme_setup_io_queues(struct nvme_dev *dev) | |||
1728 | */ | 1873 | */ |
1729 | nr_io_queues = vecs; | 1874 | nr_io_queues = vecs; |
1730 | 1875 | ||
1731 | result = queue_request_irq(dev, dev->queues[0], "nvme admin"); | 1876 | result = queue_request_irq(dev, adminq, adminq->irqname); |
1732 | if (result) { | 1877 | if (result) { |
1733 | dev->queues[0]->q_suspended = 1; | 1878 | adminq->q_suspended = 1; |
1734 | goto free_queues; | 1879 | goto free_queues; |
1735 | } | 1880 | } |
1736 | 1881 | ||
@@ -1739,9 +1884,9 @@ static int nvme_setup_io_queues(struct nvme_dev *dev) | |||
1739 | for (i = dev->queue_count - 1; i > nr_io_queues; i--) { | 1884 | for (i = dev->queue_count - 1; i > nr_io_queues; i--) { |
1740 | struct nvme_queue *nvmeq = dev->queues[i]; | 1885 | struct nvme_queue *nvmeq = dev->queues[i]; |
1741 | 1886 | ||
1742 | spin_lock(&nvmeq->q_lock); | 1887 | spin_lock_irq(&nvmeq->q_lock); |
1743 | nvme_cancel_ios(nvmeq, false); | 1888 | nvme_cancel_ios(nvmeq, false); |
1744 | spin_unlock(&nvmeq->q_lock); | 1889 | spin_unlock_irq(&nvmeq->q_lock); |
1745 | 1890 | ||
1746 | nvme_free_queue(nvmeq); | 1891 | nvme_free_queue(nvmeq); |
1747 | dev->queue_count--; | 1892 | dev->queue_count--; |
@@ -1782,7 +1927,7 @@ static int nvme_setup_io_queues(struct nvme_dev *dev) | |||
1782 | return 0; | 1927 | return 0; |
1783 | 1928 | ||
1784 | free_queues: | 1929 | free_queues: |
1785 | nvme_free_queues(dev); | 1930 | nvme_free_queues(dev, 1); |
1786 | return result; | 1931 | return result; |
1787 | } | 1932 | } |
1788 | 1933 | ||
@@ -1794,6 +1939,7 @@ static int nvme_setup_io_queues(struct nvme_dev *dev) | |||
1794 | */ | 1939 | */ |
1795 | static int nvme_dev_add(struct nvme_dev *dev) | 1940 | static int nvme_dev_add(struct nvme_dev *dev) |
1796 | { | 1941 | { |
1942 | struct pci_dev *pdev = dev->pci_dev; | ||
1797 | int res; | 1943 | int res; |
1798 | unsigned nn, i; | 1944 | unsigned nn, i; |
1799 | struct nvme_ns *ns; | 1945 | struct nvme_ns *ns; |
@@ -1803,8 +1949,7 @@ static int nvme_dev_add(struct nvme_dev *dev) | |||
1803 | dma_addr_t dma_addr; | 1949 | dma_addr_t dma_addr; |
1804 | int shift = NVME_CAP_MPSMIN(readq(&dev->bar->cap)) + 12; | 1950 | int shift = NVME_CAP_MPSMIN(readq(&dev->bar->cap)) + 12; |
1805 | 1951 | ||
1806 | mem = dma_alloc_coherent(&dev->pci_dev->dev, 8192, &dma_addr, | 1952 | mem = dma_alloc_coherent(&pdev->dev, 8192, &dma_addr, GFP_KERNEL); |
1807 | GFP_KERNEL); | ||
1808 | if (!mem) | 1953 | if (!mem) |
1809 | return -ENOMEM; | 1954 | return -ENOMEM; |
1810 | 1955 | ||
@@ -1817,13 +1962,14 @@ static int nvme_dev_add(struct nvme_dev *dev) | |||
1817 | ctrl = mem; | 1962 | ctrl = mem; |
1818 | nn = le32_to_cpup(&ctrl->nn); | 1963 | nn = le32_to_cpup(&ctrl->nn); |
1819 | dev->oncs = le16_to_cpup(&ctrl->oncs); | 1964 | dev->oncs = le16_to_cpup(&ctrl->oncs); |
1965 | dev->abort_limit = ctrl->acl + 1; | ||
1820 | memcpy(dev->serial, ctrl->sn, sizeof(ctrl->sn)); | 1966 | memcpy(dev->serial, ctrl->sn, sizeof(ctrl->sn)); |
1821 | memcpy(dev->model, ctrl->mn, sizeof(ctrl->mn)); | 1967 | memcpy(dev->model, ctrl->mn, sizeof(ctrl->mn)); |
1822 | memcpy(dev->firmware_rev, ctrl->fr, sizeof(ctrl->fr)); | 1968 | memcpy(dev->firmware_rev, ctrl->fr, sizeof(ctrl->fr)); |
1823 | if (ctrl->mdts) | 1969 | if (ctrl->mdts) |
1824 | dev->max_hw_sectors = 1 << (ctrl->mdts + shift - 9); | 1970 | dev->max_hw_sectors = 1 << (ctrl->mdts + shift - 9); |
1825 | if ((dev->pci_dev->vendor == PCI_VENDOR_ID_INTEL) && | 1971 | if ((pdev->vendor == PCI_VENDOR_ID_INTEL) && |
1826 | (dev->pci_dev->device == 0x0953) && ctrl->vs[3]) | 1972 | (pdev->device == 0x0953) && ctrl->vs[3]) |
1827 | dev->stripe_size = 1 << (ctrl->vs[3] + shift); | 1973 | dev->stripe_size = 1 << (ctrl->vs[3] + shift); |
1828 | 1974 | ||
1829 | id_ns = mem; | 1975 | id_ns = mem; |
@@ -1871,16 +2017,21 @@ static int nvme_dev_map(struct nvme_dev *dev) | |||
1871 | dma_set_mask_and_coherent(&pdev->dev, DMA_BIT_MASK(32))) | 2017 | dma_set_mask_and_coherent(&pdev->dev, DMA_BIT_MASK(32))) |
1872 | goto disable; | 2018 | goto disable; |
1873 | 2019 | ||
1874 | pci_set_drvdata(pdev, dev); | ||
1875 | dev->bar = ioremap(pci_resource_start(pdev, 0), 8192); | 2020 | dev->bar = ioremap(pci_resource_start(pdev, 0), 8192); |
1876 | if (!dev->bar) | 2021 | if (!dev->bar) |
1877 | goto disable; | 2022 | goto disable; |
1878 | 2023 | if (readl(&dev->bar->csts) == -1) { | |
1879 | dev->db_stride = NVME_CAP_STRIDE(readq(&dev->bar->cap)); | 2024 | result = -ENODEV; |
2025 | goto unmap; | ||
2026 | } | ||
2027 | dev->db_stride = 1 << NVME_CAP_STRIDE(readq(&dev->bar->cap)); | ||
1880 | dev->dbs = ((void __iomem *)dev->bar) + 4096; | 2028 | dev->dbs = ((void __iomem *)dev->bar) + 4096; |
1881 | 2029 | ||
1882 | return 0; | 2030 | return 0; |
1883 | 2031 | ||
2032 | unmap: | ||
2033 | iounmap(dev->bar); | ||
2034 | dev->bar = NULL; | ||
1884 | disable: | 2035 | disable: |
1885 | pci_release_regions(pdev); | 2036 | pci_release_regions(pdev); |
1886 | disable_pci: | 2037 | disable_pci: |
@@ -1898,37 +2049,183 @@ static void nvme_dev_unmap(struct nvme_dev *dev) | |||
1898 | if (dev->bar) { | 2049 | if (dev->bar) { |
1899 | iounmap(dev->bar); | 2050 | iounmap(dev->bar); |
1900 | dev->bar = NULL; | 2051 | dev->bar = NULL; |
2052 | pci_release_regions(dev->pci_dev); | ||
1901 | } | 2053 | } |
1902 | 2054 | ||
1903 | pci_release_regions(dev->pci_dev); | ||
1904 | if (pci_is_enabled(dev->pci_dev)) | 2055 | if (pci_is_enabled(dev->pci_dev)) |
1905 | pci_disable_device(dev->pci_dev); | 2056 | pci_disable_device(dev->pci_dev); |
1906 | } | 2057 | } |
1907 | 2058 | ||
2059 | struct nvme_delq_ctx { | ||
2060 | struct task_struct *waiter; | ||
2061 | struct kthread_worker *worker; | ||
2062 | atomic_t refcount; | ||
2063 | }; | ||
2064 | |||
2065 | static void nvme_wait_dq(struct nvme_delq_ctx *dq, struct nvme_dev *dev) | ||
2066 | { | ||
2067 | dq->waiter = current; | ||
2068 | mb(); | ||
2069 | |||
2070 | for (;;) { | ||
2071 | set_current_state(TASK_KILLABLE); | ||
2072 | if (!atomic_read(&dq->refcount)) | ||
2073 | break; | ||
2074 | if (!schedule_timeout(ADMIN_TIMEOUT) || | ||
2075 | fatal_signal_pending(current)) { | ||
2076 | set_current_state(TASK_RUNNING); | ||
2077 | |||
2078 | nvme_disable_ctrl(dev, readq(&dev->bar->cap)); | ||
2079 | nvme_disable_queue(dev, 0); | ||
2080 | |||
2081 | send_sig(SIGKILL, dq->worker->task, 1); | ||
2082 | flush_kthread_worker(dq->worker); | ||
2083 | return; | ||
2084 | } | ||
2085 | } | ||
2086 | set_current_state(TASK_RUNNING); | ||
2087 | } | ||
2088 | |||
2089 | static void nvme_put_dq(struct nvme_delq_ctx *dq) | ||
2090 | { | ||
2091 | atomic_dec(&dq->refcount); | ||
2092 | if (dq->waiter) | ||
2093 | wake_up_process(dq->waiter); | ||
2094 | } | ||
2095 | |||
2096 | static struct nvme_delq_ctx *nvme_get_dq(struct nvme_delq_ctx *dq) | ||
2097 | { | ||
2098 | atomic_inc(&dq->refcount); | ||
2099 | return dq; | ||
2100 | } | ||
2101 | |||
2102 | static void nvme_del_queue_end(struct nvme_queue *nvmeq) | ||
2103 | { | ||
2104 | struct nvme_delq_ctx *dq = nvmeq->cmdinfo.ctx; | ||
2105 | |||
2106 | nvme_clear_queue(nvmeq); | ||
2107 | nvme_put_dq(dq); | ||
2108 | } | ||
2109 | |||
2110 | static int adapter_async_del_queue(struct nvme_queue *nvmeq, u8 opcode, | ||
2111 | kthread_work_func_t fn) | ||
2112 | { | ||
2113 | struct nvme_command c; | ||
2114 | |||
2115 | memset(&c, 0, sizeof(c)); | ||
2116 | c.delete_queue.opcode = opcode; | ||
2117 | c.delete_queue.qid = cpu_to_le16(nvmeq->qid); | ||
2118 | |||
2119 | init_kthread_work(&nvmeq->cmdinfo.work, fn); | ||
2120 | return nvme_submit_admin_cmd_async(nvmeq->dev, &c, &nvmeq->cmdinfo); | ||
2121 | } | ||
2122 | |||
2123 | static void nvme_del_cq_work_handler(struct kthread_work *work) | ||
2124 | { | ||
2125 | struct nvme_queue *nvmeq = container_of(work, struct nvme_queue, | ||
2126 | cmdinfo.work); | ||
2127 | nvme_del_queue_end(nvmeq); | ||
2128 | } | ||
2129 | |||
2130 | static int nvme_delete_cq(struct nvme_queue *nvmeq) | ||
2131 | { | ||
2132 | return adapter_async_del_queue(nvmeq, nvme_admin_delete_cq, | ||
2133 | nvme_del_cq_work_handler); | ||
2134 | } | ||
2135 | |||
2136 | static void nvme_del_sq_work_handler(struct kthread_work *work) | ||
2137 | { | ||
2138 | struct nvme_queue *nvmeq = container_of(work, struct nvme_queue, | ||
2139 | cmdinfo.work); | ||
2140 | int status = nvmeq->cmdinfo.status; | ||
2141 | |||
2142 | if (!status) | ||
2143 | status = nvme_delete_cq(nvmeq); | ||
2144 | if (status) | ||
2145 | nvme_del_queue_end(nvmeq); | ||
2146 | } | ||
2147 | |||
2148 | static int nvme_delete_sq(struct nvme_queue *nvmeq) | ||
2149 | { | ||
2150 | return adapter_async_del_queue(nvmeq, nvme_admin_delete_sq, | ||
2151 | nvme_del_sq_work_handler); | ||
2152 | } | ||
2153 | |||
2154 | static void nvme_del_queue_start(struct kthread_work *work) | ||
2155 | { | ||
2156 | struct nvme_queue *nvmeq = container_of(work, struct nvme_queue, | ||
2157 | cmdinfo.work); | ||
2158 | allow_signal(SIGKILL); | ||
2159 | if (nvme_delete_sq(nvmeq)) | ||
2160 | nvme_del_queue_end(nvmeq); | ||
2161 | } | ||
2162 | |||
2163 | static void nvme_disable_io_queues(struct nvme_dev *dev) | ||
2164 | { | ||
2165 | int i; | ||
2166 | DEFINE_KTHREAD_WORKER_ONSTACK(worker); | ||
2167 | struct nvme_delq_ctx dq; | ||
2168 | struct task_struct *kworker_task = kthread_run(kthread_worker_fn, | ||
2169 | &worker, "nvme%d", dev->instance); | ||
2170 | |||
2171 | if (IS_ERR(kworker_task)) { | ||
2172 | dev_err(&dev->pci_dev->dev, | ||
2173 | "Failed to create queue del task\n"); | ||
2174 | for (i = dev->queue_count - 1; i > 0; i--) | ||
2175 | nvme_disable_queue(dev, i); | ||
2176 | return; | ||
2177 | } | ||
2178 | |||
2179 | dq.waiter = NULL; | ||
2180 | atomic_set(&dq.refcount, 0); | ||
2181 | dq.worker = &worker; | ||
2182 | for (i = dev->queue_count - 1; i > 0; i--) { | ||
2183 | struct nvme_queue *nvmeq = dev->queues[i]; | ||
2184 | |||
2185 | if (nvme_suspend_queue(nvmeq)) | ||
2186 | continue; | ||
2187 | nvmeq->cmdinfo.ctx = nvme_get_dq(&dq); | ||
2188 | nvmeq->cmdinfo.worker = dq.worker; | ||
2189 | init_kthread_work(&nvmeq->cmdinfo.work, nvme_del_queue_start); | ||
2190 | queue_kthread_work(dq.worker, &nvmeq->cmdinfo.work); | ||
2191 | } | ||
2192 | nvme_wait_dq(&dq, dev); | ||
2193 | kthread_stop(kworker_task); | ||
2194 | } | ||
2195 | |||
1908 | static void nvme_dev_shutdown(struct nvme_dev *dev) | 2196 | static void nvme_dev_shutdown(struct nvme_dev *dev) |
1909 | { | 2197 | { |
1910 | int i; | 2198 | int i; |
1911 | 2199 | ||
1912 | for (i = dev->queue_count - 1; i >= 0; i--) | 2200 | dev->initialized = 0; |
1913 | nvme_disable_queue(dev, i); | ||
1914 | 2201 | ||
1915 | spin_lock(&dev_list_lock); | 2202 | spin_lock(&dev_list_lock); |
1916 | list_del_init(&dev->node); | 2203 | list_del_init(&dev->node); |
1917 | spin_unlock(&dev_list_lock); | 2204 | spin_unlock(&dev_list_lock); |
1918 | 2205 | ||
1919 | if (dev->bar) | 2206 | if (!dev->bar || (dev->bar && readl(&dev->bar->csts) == -1)) { |
2207 | for (i = dev->queue_count - 1; i >= 0; i--) { | ||
2208 | struct nvme_queue *nvmeq = dev->queues[i]; | ||
2209 | nvme_suspend_queue(nvmeq); | ||
2210 | nvme_clear_queue(nvmeq); | ||
2211 | } | ||
2212 | } else { | ||
2213 | nvme_disable_io_queues(dev); | ||
1920 | nvme_shutdown_ctrl(dev); | 2214 | nvme_shutdown_ctrl(dev); |
2215 | nvme_disable_queue(dev, 0); | ||
2216 | } | ||
1921 | nvme_dev_unmap(dev); | 2217 | nvme_dev_unmap(dev); |
1922 | } | 2218 | } |
1923 | 2219 | ||
1924 | static void nvme_dev_remove(struct nvme_dev *dev) | 2220 | static void nvme_dev_remove(struct nvme_dev *dev) |
1925 | { | 2221 | { |
1926 | struct nvme_ns *ns, *next; | 2222 | struct nvme_ns *ns; |
1927 | 2223 | ||
1928 | list_for_each_entry_safe(ns, next, &dev->namespaces, list) { | 2224 | list_for_each_entry(ns, &dev->namespaces, list) { |
1929 | list_del(&ns->list); | 2225 | if (ns->disk->flags & GENHD_FL_UP) |
1930 | del_gendisk(ns->disk); | 2226 | del_gendisk(ns->disk); |
1931 | nvme_ns_free(ns); | 2227 | if (!blk_queue_dying(ns->queue)) |
2228 | blk_cleanup_queue(ns->queue); | ||
1932 | } | 2229 | } |
1933 | } | 2230 | } |
1934 | 2231 | ||
@@ -1985,14 +2282,22 @@ static void nvme_release_instance(struct nvme_dev *dev) | |||
1985 | spin_unlock(&dev_list_lock); | 2282 | spin_unlock(&dev_list_lock); |
1986 | } | 2283 | } |
1987 | 2284 | ||
2285 | static void nvme_free_namespaces(struct nvme_dev *dev) | ||
2286 | { | ||
2287 | struct nvme_ns *ns, *next; | ||
2288 | |||
2289 | list_for_each_entry_safe(ns, next, &dev->namespaces, list) { | ||
2290 | list_del(&ns->list); | ||
2291 | put_disk(ns->disk); | ||
2292 | kfree(ns); | ||
2293 | } | ||
2294 | } | ||
2295 | |||
1988 | static void nvme_free_dev(struct kref *kref) | 2296 | static void nvme_free_dev(struct kref *kref) |
1989 | { | 2297 | { |
1990 | struct nvme_dev *dev = container_of(kref, struct nvme_dev, kref); | 2298 | struct nvme_dev *dev = container_of(kref, struct nvme_dev, kref); |
1991 | nvme_dev_remove(dev); | 2299 | |
1992 | nvme_dev_shutdown(dev); | 2300 | nvme_free_namespaces(dev); |
1993 | nvme_free_queues(dev); | ||
1994 | nvme_release_instance(dev); | ||
1995 | nvme_release_prp_pools(dev); | ||
1996 | kfree(dev->queues); | 2301 | kfree(dev->queues); |
1997 | kfree(dev->entry); | 2302 | kfree(dev->entry); |
1998 | kfree(dev); | 2303 | kfree(dev); |
@@ -2056,6 +2361,7 @@ static int nvme_dev_start(struct nvme_dev *dev) | |||
2056 | return result; | 2361 | return result; |
2057 | 2362 | ||
2058 | disable: | 2363 | disable: |
2364 | nvme_disable_queue(dev, 0); | ||
2059 | spin_lock(&dev_list_lock); | 2365 | spin_lock(&dev_list_lock); |
2060 | list_del_init(&dev->node); | 2366 | list_del_init(&dev->node); |
2061 | spin_unlock(&dev_list_lock); | 2367 | spin_unlock(&dev_list_lock); |
@@ -2064,6 +2370,71 @@ static int nvme_dev_start(struct nvme_dev *dev) | |||
2064 | return result; | 2370 | return result; |
2065 | } | 2371 | } |
2066 | 2372 | ||
2373 | static int nvme_remove_dead_ctrl(void *arg) | ||
2374 | { | ||
2375 | struct nvme_dev *dev = (struct nvme_dev *)arg; | ||
2376 | struct pci_dev *pdev = dev->pci_dev; | ||
2377 | |||
2378 | if (pci_get_drvdata(pdev)) | ||
2379 | pci_stop_and_remove_bus_device(pdev); | ||
2380 | kref_put(&dev->kref, nvme_free_dev); | ||
2381 | return 0; | ||
2382 | } | ||
2383 | |||
2384 | static void nvme_remove_disks(struct work_struct *ws) | ||
2385 | { | ||
2386 | int i; | ||
2387 | struct nvme_dev *dev = container_of(ws, struct nvme_dev, reset_work); | ||
2388 | |||
2389 | nvme_dev_remove(dev); | ||
2390 | spin_lock(&dev_list_lock); | ||
2391 | for (i = dev->queue_count - 1; i > 0; i--) { | ||
2392 | BUG_ON(!dev->queues[i] || !dev->queues[i]->q_suspended); | ||
2393 | nvme_free_queue(dev->queues[i]); | ||
2394 | dev->queue_count--; | ||
2395 | dev->queues[i] = NULL; | ||
2396 | } | ||
2397 | spin_unlock(&dev_list_lock); | ||
2398 | } | ||
2399 | |||
2400 | static int nvme_dev_resume(struct nvme_dev *dev) | ||
2401 | { | ||
2402 | int ret; | ||
2403 | |||
2404 | ret = nvme_dev_start(dev); | ||
2405 | if (ret && ret != -EBUSY) | ||
2406 | return ret; | ||
2407 | if (ret == -EBUSY) { | ||
2408 | spin_lock(&dev_list_lock); | ||
2409 | PREPARE_WORK(&dev->reset_work, nvme_remove_disks); | ||
2410 | queue_work(nvme_workq, &dev->reset_work); | ||
2411 | spin_unlock(&dev_list_lock); | ||
2412 | } | ||
2413 | dev->initialized = 1; | ||
2414 | return 0; | ||
2415 | } | ||
2416 | |||
2417 | static void nvme_dev_reset(struct nvme_dev *dev) | ||
2418 | { | ||
2419 | nvme_dev_shutdown(dev); | ||
2420 | if (nvme_dev_resume(dev)) { | ||
2421 | dev_err(&dev->pci_dev->dev, "Device failed to resume\n"); | ||
2422 | kref_get(&dev->kref); | ||
2423 | if (IS_ERR(kthread_run(nvme_remove_dead_ctrl, dev, "nvme%d", | ||
2424 | dev->instance))) { | ||
2425 | dev_err(&dev->pci_dev->dev, | ||
2426 | "Failed to start controller remove task\n"); | ||
2427 | kref_put(&dev->kref, nvme_free_dev); | ||
2428 | } | ||
2429 | } | ||
2430 | } | ||
2431 | |||
2432 | static void nvme_reset_failed_dev(struct work_struct *ws) | ||
2433 | { | ||
2434 | struct nvme_dev *dev = container_of(ws, struct nvme_dev, reset_work); | ||
2435 | nvme_dev_reset(dev); | ||
2436 | } | ||
2437 | |||
2067 | static int nvme_probe(struct pci_dev *pdev, const struct pci_device_id *id) | 2438 | static int nvme_probe(struct pci_dev *pdev, const struct pci_device_id *id) |
2068 | { | 2439 | { |
2069 | int result = -ENOMEM; | 2440 | int result = -ENOMEM; |
@@ -2082,8 +2453,9 @@ static int nvme_probe(struct pci_dev *pdev, const struct pci_device_id *id) | |||
2082 | goto free; | 2453 | goto free; |
2083 | 2454 | ||
2084 | INIT_LIST_HEAD(&dev->namespaces); | 2455 | INIT_LIST_HEAD(&dev->namespaces); |
2456 | INIT_WORK(&dev->reset_work, nvme_reset_failed_dev); | ||
2085 | dev->pci_dev = pdev; | 2457 | dev->pci_dev = pdev; |
2086 | 2458 | pci_set_drvdata(pdev, dev); | |
2087 | result = nvme_set_instance(dev); | 2459 | result = nvme_set_instance(dev); |
2088 | if (result) | 2460 | if (result) |
2089 | goto free; | 2461 | goto free; |
@@ -2099,6 +2471,7 @@ static int nvme_probe(struct pci_dev *pdev, const struct pci_device_id *id) | |||
2099 | goto release_pools; | 2471 | goto release_pools; |
2100 | } | 2472 | } |
2101 | 2473 | ||
2474 | kref_init(&dev->kref); | ||
2102 | result = nvme_dev_add(dev); | 2475 | result = nvme_dev_add(dev); |
2103 | if (result) | 2476 | if (result) |
2104 | goto shutdown; | 2477 | goto shutdown; |
@@ -2113,15 +2486,16 @@ static int nvme_probe(struct pci_dev *pdev, const struct pci_device_id *id) | |||
2113 | if (result) | 2486 | if (result) |
2114 | goto remove; | 2487 | goto remove; |
2115 | 2488 | ||
2116 | kref_init(&dev->kref); | 2489 | dev->initialized = 1; |
2117 | return 0; | 2490 | return 0; |
2118 | 2491 | ||
2119 | remove: | 2492 | remove: |
2120 | nvme_dev_remove(dev); | 2493 | nvme_dev_remove(dev); |
2494 | nvme_free_namespaces(dev); | ||
2121 | shutdown: | 2495 | shutdown: |
2122 | nvme_dev_shutdown(dev); | 2496 | nvme_dev_shutdown(dev); |
2123 | release_pools: | 2497 | release_pools: |
2124 | nvme_free_queues(dev); | 2498 | nvme_free_queues(dev, 0); |
2125 | nvme_release_prp_pools(dev); | 2499 | nvme_release_prp_pools(dev); |
2126 | release: | 2500 | release: |
2127 | nvme_release_instance(dev); | 2501 | nvme_release_instance(dev); |
@@ -2132,10 +2506,28 @@ static int nvme_probe(struct pci_dev *pdev, const struct pci_device_id *id) | |||
2132 | return result; | 2506 | return result; |
2133 | } | 2507 | } |
2134 | 2508 | ||
2509 | static void nvme_shutdown(struct pci_dev *pdev) | ||
2510 | { | ||
2511 | struct nvme_dev *dev = pci_get_drvdata(pdev); | ||
2512 | nvme_dev_shutdown(dev); | ||
2513 | } | ||
2514 | |||
2135 | static void nvme_remove(struct pci_dev *pdev) | 2515 | static void nvme_remove(struct pci_dev *pdev) |
2136 | { | 2516 | { |
2137 | struct nvme_dev *dev = pci_get_drvdata(pdev); | 2517 | struct nvme_dev *dev = pci_get_drvdata(pdev); |
2518 | |||
2519 | spin_lock(&dev_list_lock); | ||
2520 | list_del_init(&dev->node); | ||
2521 | spin_unlock(&dev_list_lock); | ||
2522 | |||
2523 | pci_set_drvdata(pdev, NULL); | ||
2524 | flush_work(&dev->reset_work); | ||
2138 | misc_deregister(&dev->miscdev); | 2525 | misc_deregister(&dev->miscdev); |
2526 | nvme_dev_remove(dev); | ||
2527 | nvme_dev_shutdown(dev); | ||
2528 | nvme_free_queues(dev, 0); | ||
2529 | nvme_release_instance(dev); | ||
2530 | nvme_release_prp_pools(dev); | ||
2139 | kref_put(&dev->kref, nvme_free_dev); | 2531 | kref_put(&dev->kref, nvme_free_dev); |
2140 | } | 2532 | } |
2141 | 2533 | ||
@@ -2159,13 +2551,12 @@ static int nvme_resume(struct device *dev) | |||
2159 | { | 2551 | { |
2160 | struct pci_dev *pdev = to_pci_dev(dev); | 2552 | struct pci_dev *pdev = to_pci_dev(dev); |
2161 | struct nvme_dev *ndev = pci_get_drvdata(pdev); | 2553 | struct nvme_dev *ndev = pci_get_drvdata(pdev); |
2162 | int ret; | ||
2163 | 2554 | ||
2164 | ret = nvme_dev_start(ndev); | 2555 | if (nvme_dev_resume(ndev) && !work_busy(&ndev->reset_work)) { |
2165 | /* XXX: should remove gendisks if resume fails */ | 2556 | PREPARE_WORK(&ndev->reset_work, nvme_reset_failed_dev); |
2166 | if (ret) | 2557 | queue_work(nvme_workq, &ndev->reset_work); |
2167 | nvme_free_queues(ndev); | 2558 | } |
2168 | return ret; | 2559 | return 0; |
2169 | } | 2560 | } |
2170 | 2561 | ||
2171 | static SIMPLE_DEV_PM_OPS(nvme_dev_pm_ops, nvme_suspend, nvme_resume); | 2562 | static SIMPLE_DEV_PM_OPS(nvme_dev_pm_ops, nvme_suspend, nvme_resume); |
@@ -2192,6 +2583,7 @@ static struct pci_driver nvme_driver = { | |||
2192 | .id_table = nvme_id_table, | 2583 | .id_table = nvme_id_table, |
2193 | .probe = nvme_probe, | 2584 | .probe = nvme_probe, |
2194 | .remove = nvme_remove, | 2585 | .remove = nvme_remove, |
2586 | .shutdown = nvme_shutdown, | ||
2195 | .driver = { | 2587 | .driver = { |
2196 | .pm = &nvme_dev_pm_ops, | 2588 | .pm = &nvme_dev_pm_ops, |
2197 | }, | 2589 | }, |
@@ -2206,9 +2598,14 @@ static int __init nvme_init(void) | |||
2206 | if (IS_ERR(nvme_thread)) | 2598 | if (IS_ERR(nvme_thread)) |
2207 | return PTR_ERR(nvme_thread); | 2599 | return PTR_ERR(nvme_thread); |
2208 | 2600 | ||
2601 | result = -ENOMEM; | ||
2602 | nvme_workq = create_singlethread_workqueue("nvme"); | ||
2603 | if (!nvme_workq) | ||
2604 | goto kill_kthread; | ||
2605 | |||
2209 | result = register_blkdev(nvme_major, "nvme"); | 2606 | result = register_blkdev(nvme_major, "nvme"); |
2210 | if (result < 0) | 2607 | if (result < 0) |
2211 | goto kill_kthread; | 2608 | goto kill_workq; |
2212 | else if (result > 0) | 2609 | else if (result > 0) |
2213 | nvme_major = result; | 2610 | nvme_major = result; |
2214 | 2611 | ||
@@ -2219,6 +2616,8 @@ static int __init nvme_init(void) | |||
2219 | 2616 | ||
2220 | unregister_blkdev: | 2617 | unregister_blkdev: |
2221 | unregister_blkdev(nvme_major, "nvme"); | 2618 | unregister_blkdev(nvme_major, "nvme"); |
2619 | kill_workq: | ||
2620 | destroy_workqueue(nvme_workq); | ||
2222 | kill_kthread: | 2621 | kill_kthread: |
2223 | kthread_stop(nvme_thread); | 2622 | kthread_stop(nvme_thread); |
2224 | return result; | 2623 | return result; |
@@ -2228,6 +2627,7 @@ static void __exit nvme_exit(void) | |||
2228 | { | 2627 | { |
2229 | pci_unregister_driver(&nvme_driver); | 2628 | pci_unregister_driver(&nvme_driver); |
2230 | unregister_blkdev(nvme_major, "nvme"); | 2629 | unregister_blkdev(nvme_major, "nvme"); |
2630 | destroy_workqueue(nvme_workq); | ||
2231 | kthread_stop(nvme_thread); | 2631 | kthread_stop(nvme_thread); |
2232 | } | 2632 | } |
2233 | 2633 | ||
diff --git a/drivers/block/nvme-scsi.c b/drivers/block/nvme-scsi.c index 4a4ff4eb8e23..4a0ceb64e269 100644 --- a/drivers/block/nvme-scsi.c +++ b/drivers/block/nvme-scsi.c | |||
@@ -25,6 +25,7 @@ | |||
25 | #include <linux/bio.h> | 25 | #include <linux/bio.h> |
26 | #include <linux/bitops.h> | 26 | #include <linux/bitops.h> |
27 | #include <linux/blkdev.h> | 27 | #include <linux/blkdev.h> |
28 | #include <linux/compat.h> | ||
28 | #include <linux/delay.h> | 29 | #include <linux/delay.h> |
29 | #include <linux/errno.h> | 30 | #include <linux/errno.h> |
30 | #include <linux/fs.h> | 31 | #include <linux/fs.h> |
@@ -3038,6 +3039,152 @@ int nvme_sg_io(struct nvme_ns *ns, struct sg_io_hdr __user *u_hdr) | |||
3038 | return retcode; | 3039 | return retcode; |
3039 | } | 3040 | } |
3040 | 3041 | ||
3042 | #ifdef CONFIG_COMPAT | ||
3043 | typedef struct sg_io_hdr32 { | ||
3044 | compat_int_t interface_id; /* [i] 'S' for SCSI generic (required) */ | ||
3045 | compat_int_t dxfer_direction; /* [i] data transfer direction */ | ||
3046 | unsigned char cmd_len; /* [i] SCSI command length ( <= 16 bytes) */ | ||
3047 | unsigned char mx_sb_len; /* [i] max length to write to sbp */ | ||
3048 | unsigned short iovec_count; /* [i] 0 implies no scatter gather */ | ||
3049 | compat_uint_t dxfer_len; /* [i] byte count of data transfer */ | ||
3050 | compat_uint_t dxferp; /* [i], [*io] points to data transfer memory | ||
3051 | or scatter gather list */ | ||
3052 | compat_uptr_t cmdp; /* [i], [*i] points to command to perform */ | ||
3053 | compat_uptr_t sbp; /* [i], [*o] points to sense_buffer memory */ | ||
3054 | compat_uint_t timeout; /* [i] MAX_UINT->no timeout (unit: millisec) */ | ||
3055 | compat_uint_t flags; /* [i] 0 -> default, see SG_FLAG... */ | ||
3056 | compat_int_t pack_id; /* [i->o] unused internally (normally) */ | ||
3057 | compat_uptr_t usr_ptr; /* [i->o] unused internally */ | ||
3058 | unsigned char status; /* [o] scsi status */ | ||
3059 | unsigned char masked_status; /* [o] shifted, masked scsi status */ | ||
3060 | unsigned char msg_status; /* [o] messaging level data (optional) */ | ||
3061 | unsigned char sb_len_wr; /* [o] byte count actually written to sbp */ | ||
3062 | unsigned short host_status; /* [o] errors from host adapter */ | ||
3063 | unsigned short driver_status; /* [o] errors from software driver */ | ||
3064 | compat_int_t resid; /* [o] dxfer_len - actual_transferred */ | ||
3065 | compat_uint_t duration; /* [o] time taken by cmd (unit: millisec) */ | ||
3066 | compat_uint_t info; /* [o] auxiliary information */ | ||
3067 | } sg_io_hdr32_t; /* 64 bytes long (on sparc32) */ | ||
3068 | |||
3069 | typedef struct sg_iovec32 { | ||
3070 | compat_uint_t iov_base; | ||
3071 | compat_uint_t iov_len; | ||
3072 | } sg_iovec32_t; | ||
3073 | |||
3074 | static int sg_build_iovec(sg_io_hdr_t __user *sgio, void __user *dxferp, u16 iovec_count) | ||
3075 | { | ||
3076 | sg_iovec_t __user *iov = (sg_iovec_t __user *) (sgio + 1); | ||
3077 | sg_iovec32_t __user *iov32 = dxferp; | ||
3078 | int i; | ||
3079 | |||
3080 | for (i = 0; i < iovec_count; i++) { | ||
3081 | u32 base, len; | ||
3082 | |||
3083 | if (get_user(base, &iov32[i].iov_base) || | ||
3084 | get_user(len, &iov32[i].iov_len) || | ||
3085 | put_user(compat_ptr(base), &iov[i].iov_base) || | ||
3086 | put_user(len, &iov[i].iov_len)) | ||
3087 | return -EFAULT; | ||
3088 | } | ||
3089 | |||
3090 | if (put_user(iov, &sgio->dxferp)) | ||
3091 | return -EFAULT; | ||
3092 | return 0; | ||
3093 | } | ||
3094 | |||
3095 | int nvme_sg_io32(struct nvme_ns *ns, unsigned long arg) | ||
3096 | { | ||
3097 | sg_io_hdr32_t __user *sgio32 = (sg_io_hdr32_t __user *)arg; | ||
3098 | sg_io_hdr_t __user *sgio; | ||
3099 | u16 iovec_count; | ||
3100 | u32 data; | ||
3101 | void __user *dxferp; | ||
3102 | int err; | ||
3103 | int interface_id; | ||
3104 | |||
3105 | if (get_user(interface_id, &sgio32->interface_id)) | ||
3106 | return -EFAULT; | ||
3107 | if (interface_id != 'S') | ||
3108 | return -EINVAL; | ||
3109 | |||
3110 | if (get_user(iovec_count, &sgio32->iovec_count)) | ||
3111 | return -EFAULT; | ||
3112 | |||
3113 | { | ||
3114 | void __user *top = compat_alloc_user_space(0); | ||
3115 | void __user *new = compat_alloc_user_space(sizeof(sg_io_hdr_t) + | ||
3116 | (iovec_count * sizeof(sg_iovec_t))); | ||
3117 | if (new > top) | ||
3118 | return -EINVAL; | ||
3119 | |||
3120 | sgio = new; | ||
3121 | } | ||
3122 | |||
3123 | /* Ok, now construct. */ | ||
3124 | if (copy_in_user(&sgio->interface_id, &sgio32->interface_id, | ||
3125 | (2 * sizeof(int)) + | ||
3126 | (2 * sizeof(unsigned char)) + | ||
3127 | (1 * sizeof(unsigned short)) + | ||
3128 | (1 * sizeof(unsigned int)))) | ||
3129 | return -EFAULT; | ||
3130 | |||
3131 | if (get_user(data, &sgio32->dxferp)) | ||
3132 | return -EFAULT; | ||
3133 | dxferp = compat_ptr(data); | ||
3134 | if (iovec_count) { | ||
3135 | if (sg_build_iovec(sgio, dxferp, iovec_count)) | ||
3136 | return -EFAULT; | ||
3137 | } else { | ||
3138 | if (put_user(dxferp, &sgio->dxferp)) | ||
3139 | return -EFAULT; | ||
3140 | } | ||
3141 | |||
3142 | { | ||
3143 | unsigned char __user *cmdp; | ||
3144 | unsigned char __user *sbp; | ||
3145 | |||
3146 | if (get_user(data, &sgio32->cmdp)) | ||
3147 | return -EFAULT; | ||
3148 | cmdp = compat_ptr(data); | ||
3149 | |||
3150 | if (get_user(data, &sgio32->sbp)) | ||
3151 | return -EFAULT; | ||
3152 | sbp = compat_ptr(data); | ||
3153 | |||
3154 | if (put_user(cmdp, &sgio->cmdp) || | ||
3155 | put_user(sbp, &sgio->sbp)) | ||
3156 | return -EFAULT; | ||
3157 | } | ||
3158 | |||
3159 | if (copy_in_user(&sgio->timeout, &sgio32->timeout, | ||
3160 | 3 * sizeof(int))) | ||
3161 | return -EFAULT; | ||
3162 | |||
3163 | if (get_user(data, &sgio32->usr_ptr)) | ||
3164 | return -EFAULT; | ||
3165 | if (put_user(compat_ptr(data), &sgio->usr_ptr)) | ||
3166 | return -EFAULT; | ||
3167 | |||
3168 | err = nvme_sg_io(ns, sgio); | ||
3169 | if (err >= 0) { | ||
3170 | void __user *datap; | ||
3171 | |||
3172 | if (copy_in_user(&sgio32->pack_id, &sgio->pack_id, | ||
3173 | sizeof(int)) || | ||
3174 | get_user(datap, &sgio->usr_ptr) || | ||
3175 | put_user((u32)(unsigned long)datap, | ||
3176 | &sgio32->usr_ptr) || | ||
3177 | copy_in_user(&sgio32->status, &sgio->status, | ||
3178 | (4 * sizeof(unsigned char)) + | ||
3179 | (2 * sizeof(unsigned short)) + | ||
3180 | (3 * sizeof(int)))) | ||
3181 | err = -EFAULT; | ||
3182 | } | ||
3183 | |||
3184 | return err; | ||
3185 | } | ||
3186 | #endif | ||
3187 | |||
3041 | int nvme_sg_get_version_num(int __user *ip) | 3188 | int nvme_sg_get_version_num(int __user *ip) |
3042 | { | 3189 | { |
3043 | return put_user(sg_version_num, ip); | 3190 | return put_user(sg_version_num, ip); |