]>
Commit | Line | Data |
---|---|---|
5d0f6131 VV |
1 | /* |
2 | * NVM Express device driver | |
8757ad65 | 3 | * Copyright (c) 2011-2014, Intel Corporation. |
5d0f6131 VV |
4 | * |
5 | * This program is free software; you can redistribute it and/or modify it | |
6 | * under the terms and conditions of the GNU General Public License, | |
7 | * version 2, as published by the Free Software Foundation. | |
8 | * | |
9 | * This program is distributed in the hope it will be useful, but WITHOUT | |
10 | * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or | |
11 | * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for | |
12 | * more details. | |
5d0f6131 VV |
13 | */ |
14 | ||
15 | /* | |
16 | * Refer to the SCSI-NVMe Translation spec for details on how | |
17 | * each command is translated. | |
18 | */ | |
19 | ||
20 | #include <linux/nvme.h> | |
21 | #include <linux/bio.h> | |
22 | #include <linux/bitops.h> | |
23 | #include <linux/blkdev.h> | |
320a3827 | 24 | #include <linux/compat.h> |
5d0f6131 VV |
25 | #include <linux/delay.h> |
26 | #include <linux/errno.h> | |
27 | #include <linux/fs.h> | |
28 | #include <linux/genhd.h> | |
29 | #include <linux/idr.h> | |
30 | #include <linux/init.h> | |
31 | #include <linux/interrupt.h> | |
32 | #include <linux/io.h> | |
33 | #include <linux/kdev_t.h> | |
34 | #include <linux/kthread.h> | |
35 | #include <linux/kernel.h> | |
36 | #include <linux/mm.h> | |
37 | #include <linux/module.h> | |
38 | #include <linux/moduleparam.h> | |
39 | #include <linux/pci.h> | |
40 | #include <linux/poison.h> | |
41 | #include <linux/sched.h> | |
42 | #include <linux/slab.h> | |
43 | #include <linux/types.h> | |
3726897e | 44 | #include <asm/unaligned.h> |
5d0f6131 VV |
45 | #include <scsi/sg.h> |
46 | #include <scsi/scsi.h> | |
47 | ||
48 | ||
49 | static int sg_version_num = 30534; /* 2 digits for each component */ | |
50 | ||
5d0f6131 VV |
51 | /* VPD Page Codes */ |
52 | #define VPD_SUPPORTED_PAGES 0x00 | |
53 | #define VPD_SERIAL_NUMBER 0x80 | |
54 | #define VPD_DEVICE_IDENTIFIERS 0x83 | |
55 | #define VPD_EXTENDED_INQUIRY 0x86 | |
7f749d9c | 56 | #define VPD_BLOCK_LIMITS 0xB0 |
5d0f6131 VV |
57 | #define VPD_BLOCK_DEV_CHARACTERISTICS 0xB1 |
58 | ||
3726897e | 59 | /* format unit paramter list offsets */ |
5d0f6131 VV |
60 | #define FORMAT_UNIT_SHORT_PARM_LIST_LEN 4 |
61 | #define FORMAT_UNIT_LONG_PARM_LIST_LEN 8 | |
62 | #define FORMAT_UNIT_PROT_INT_OFFSET 3 | |
63 | #define FORMAT_UNIT_PROT_FIELD_USAGE_OFFSET 0 | |
64 | #define FORMAT_UNIT_PROT_FIELD_USAGE_MASK 0x07 | |
65 | ||
66 | /* Misc. defines */ | |
5d0f6131 VV |
67 | #define FIXED_SENSE_DATA 0x70 |
68 | #define DESC_FORMAT_SENSE_DATA 0x72 | |
69 | #define FIXED_SENSE_DATA_ADD_LENGTH 10 | |
70 | #define LUN_ENTRY_SIZE 8 | |
71 | #define LUN_DATA_HEADER_SIZE 8 | |
72 | #define ALL_LUNS_RETURNED 0x02 | |
73 | #define ALL_WELL_KNOWN_LUNS_RETURNED 0x01 | |
74 | #define RESTRICTED_LUNS_RETURNED 0x00 | |
75 | #define NVME_POWER_STATE_START_VALID 0x00 | |
76 | #define NVME_POWER_STATE_ACTIVE 0x01 | |
77 | #define NVME_POWER_STATE_IDLE 0x02 | |
78 | #define NVME_POWER_STATE_STANDBY 0x03 | |
79 | #define NVME_POWER_STATE_LU_CONTROL 0x07 | |
80 | #define POWER_STATE_0 0 | |
81 | #define POWER_STATE_1 1 | |
82 | #define POWER_STATE_2 2 | |
83 | #define POWER_STATE_3 3 | |
84 | #define DOWNLOAD_SAVE_ACTIVATE 0x05 | |
85 | #define DOWNLOAD_SAVE_DEFER_ACTIVATE 0x0E | |
86 | #define ACTIVATE_DEFERRED_MICROCODE 0x0F | |
87 | #define FORMAT_UNIT_IMMED_MASK 0x2 | |
88 | #define FORMAT_UNIT_IMMED_OFFSET 1 | |
89 | #define KELVIN_TEMP_FACTOR 273 | |
90 | #define FIXED_FMT_SENSE_DATA_SIZE 18 | |
91 | #define DESC_FMT_SENSE_DATA_SIZE 8 | |
92 | ||
93 | /* SCSI/NVMe defines and bit masks */ | |
94 | #define INQ_STANDARD_INQUIRY_PAGE 0x00 | |
95 | #define INQ_SUPPORTED_VPD_PAGES_PAGE 0x00 | |
96 | #define INQ_UNIT_SERIAL_NUMBER_PAGE 0x80 | |
97 | #define INQ_DEVICE_IDENTIFICATION_PAGE 0x83 | |
98 | #define INQ_EXTENDED_INQUIRY_DATA_PAGE 0x86 | |
7f749d9c | 99 | #define INQ_BDEV_LIMITS_PAGE 0xB0 |
5d0f6131 VV |
100 | #define INQ_BDEV_CHARACTERISTICS_PAGE 0xB1 |
101 | #define INQ_SERIAL_NUMBER_LENGTH 0x14 | |
7f749d9c | 102 | #define INQ_NUM_SUPPORTED_VPD_PAGES 6 |
5d0f6131 VV |
103 | #define VERSION_SPC_4 0x06 |
104 | #define ACA_UNSUPPORTED 0 | |
105 | #define STANDARD_INQUIRY_LENGTH 36 | |
106 | #define ADDITIONAL_STD_INQ_LENGTH 31 | |
107 | #define EXTENDED_INQUIRY_DATA_PAGE_LENGTH 0x3C | |
108 | #define RESERVED_FIELD 0 | |
109 | ||
5d0f6131 VV |
110 | /* Mode Sense/Select defines */ |
111 | #define MODE_PAGE_INFO_EXCEP 0x1C | |
112 | #define MODE_PAGE_CACHING 0x08 | |
113 | #define MODE_PAGE_CONTROL 0x0A | |
114 | #define MODE_PAGE_POWER_CONDITION 0x1A | |
115 | #define MODE_PAGE_RETURN_ALL 0x3F | |
116 | #define MODE_PAGE_BLK_DES_LEN 0x08 | |
117 | #define MODE_PAGE_LLBAA_BLK_DES_LEN 0x10 | |
118 | #define MODE_PAGE_CACHING_LEN 0x14 | |
119 | #define MODE_PAGE_CONTROL_LEN 0x0C | |
120 | #define MODE_PAGE_POW_CND_LEN 0x28 | |
121 | #define MODE_PAGE_INF_EXC_LEN 0x0C | |
122 | #define MODE_PAGE_ALL_LEN 0x54 | |
123 | #define MODE_SENSE6_MPH_SIZE 4 | |
5d0f6131 VV |
124 | #define MODE_SENSE_PAGE_CONTROL_MASK 0xC0 |
125 | #define MODE_SENSE_PAGE_CODE_OFFSET 2 | |
126 | #define MODE_SENSE_PAGE_CODE_MASK 0x3F | |
5d0f6131 VV |
127 | #define MODE_SENSE_LLBAA_MASK 0x10 |
128 | #define MODE_SENSE_LLBAA_SHIFT 4 | |
5d0f6131 VV |
129 | #define MODE_SENSE_DBD_MASK 8 |
130 | #define MODE_SENSE_DBD_SHIFT 3 | |
131 | #define MODE_SENSE10_MPH_SIZE 8 | |
5d0f6131 VV |
132 | #define MODE_SELECT_CDB_PAGE_FORMAT_MASK 0x10 |
133 | #define MODE_SELECT_CDB_SAVE_PAGES_MASK 0x1 | |
134 | #define MODE_SELECT_6_BD_OFFSET 3 | |
135 | #define MODE_SELECT_10_BD_OFFSET 6 | |
136 | #define MODE_SELECT_10_LLBAA_OFFSET 4 | |
137 | #define MODE_SELECT_10_LLBAA_MASK 1 | |
138 | #define MODE_SELECT_6_MPH_SIZE 4 | |
139 | #define MODE_SELECT_10_MPH_SIZE 8 | |
140 | #define CACHING_MODE_PAGE_WCE_MASK 0x04 | |
141 | #define MODE_SENSE_BLK_DESC_ENABLED 0 | |
142 | #define MODE_SENSE_BLK_DESC_COUNT 1 | |
143 | #define MODE_SELECT_PAGE_CODE_MASK 0x3F | |
144 | #define SHORT_DESC_BLOCK 8 | |
145 | #define LONG_DESC_BLOCK 16 | |
146 | #define MODE_PAGE_POW_CND_LEN_FIELD 0x26 | |
147 | #define MODE_PAGE_INF_EXC_LEN_FIELD 0x0A | |
148 | #define MODE_PAGE_CACHING_LEN_FIELD 0x12 | |
149 | #define MODE_PAGE_CONTROL_LEN_FIELD 0x0A | |
150 | #define MODE_SENSE_PC_CURRENT_VALUES 0 | |
151 | ||
152 | /* Log Sense defines */ | |
153 | #define LOG_PAGE_SUPPORTED_LOG_PAGES_PAGE 0x00 | |
154 | #define LOG_PAGE_SUPPORTED_LOG_PAGES_LENGTH 0x07 | |
155 | #define LOG_PAGE_INFORMATIONAL_EXCEPTIONS_PAGE 0x2F | |
156 | #define LOG_PAGE_TEMPERATURE_PAGE 0x0D | |
5d0f6131 | 157 | #define LOG_SENSE_CDB_SP_NOT_ENABLED 0 |
5d0f6131 VV |
158 | #define LOG_SENSE_CDB_PC_MASK 0xC0 |
159 | #define LOG_SENSE_CDB_PC_SHIFT 6 | |
160 | #define LOG_SENSE_CDB_PC_CUMULATIVE_VALUES 1 | |
161 | #define LOG_SENSE_CDB_PAGE_CODE_MASK 0x3F | |
5d0f6131 VV |
162 | #define REMAINING_INFO_EXCP_PAGE_LENGTH 0x8 |
163 | #define LOG_INFO_EXCP_PAGE_LENGTH 0xC | |
164 | #define REMAINING_TEMP_PAGE_LENGTH 0xC | |
165 | #define LOG_TEMP_PAGE_LENGTH 0x10 | |
166 | #define LOG_TEMP_UNKNOWN 0xFF | |
167 | #define SUPPORTED_LOG_PAGES_PAGE_LENGTH 0x3 | |
168 | ||
169 | /* Read Capacity defines */ | |
170 | #define READ_CAP_10_RESP_SIZE 8 | |
171 | #define READ_CAP_16_RESP_SIZE 32 | |
172 | ||
173 | /* NVMe Namespace and Command Defines */ | |
5d0f6131 VV |
174 | #define BYTES_TO_DWORDS 4 |
175 | #define NVME_MAX_FIRMWARE_SLOT 7 | |
176 | ||
177 | /* Report LUNs defines */ | |
178 | #define REPORT_LUNS_FIRST_LUN_OFFSET 8 | |
179 | ||
180 | /* SCSI ADDITIONAL SENSE Codes */ | |
181 | ||
182 | #define SCSI_ASC_NO_SENSE 0x00 | |
183 | #define SCSI_ASC_PERIPHERAL_DEV_WRITE_FAULT 0x03 | |
184 | #define SCSI_ASC_LUN_NOT_READY 0x04 | |
185 | #define SCSI_ASC_WARNING 0x0B | |
186 | #define SCSI_ASC_LOG_BLOCK_GUARD_CHECK_FAILED 0x10 | |
187 | #define SCSI_ASC_LOG_BLOCK_APPTAG_CHECK_FAILED 0x10 | |
188 | #define SCSI_ASC_LOG_BLOCK_REFTAG_CHECK_FAILED 0x10 | |
189 | #define SCSI_ASC_UNRECOVERED_READ_ERROR 0x11 | |
190 | #define SCSI_ASC_MISCOMPARE_DURING_VERIFY 0x1D | |
191 | #define SCSI_ASC_ACCESS_DENIED_INVALID_LUN_ID 0x20 | |
192 | #define SCSI_ASC_ILLEGAL_COMMAND 0x20 | |
193 | #define SCSI_ASC_ILLEGAL_BLOCK 0x21 | |
194 | #define SCSI_ASC_INVALID_CDB 0x24 | |
195 | #define SCSI_ASC_INVALID_LUN 0x25 | |
196 | #define SCSI_ASC_INVALID_PARAMETER 0x26 | |
197 | #define SCSI_ASC_FORMAT_COMMAND_FAILED 0x31 | |
198 | #define SCSI_ASC_INTERNAL_TARGET_FAILURE 0x44 | |
199 | ||
200 | /* SCSI ADDITIONAL SENSE Code Qualifiers */ | |
201 | ||
202 | #define SCSI_ASCQ_CAUSE_NOT_REPORTABLE 0x00 | |
203 | #define SCSI_ASCQ_FORMAT_COMMAND_FAILED 0x01 | |
204 | #define SCSI_ASCQ_LOG_BLOCK_GUARD_CHECK_FAILED 0x01 | |
205 | #define SCSI_ASCQ_LOG_BLOCK_APPTAG_CHECK_FAILED 0x02 | |
206 | #define SCSI_ASCQ_LOG_BLOCK_REFTAG_CHECK_FAILED 0x03 | |
207 | #define SCSI_ASCQ_FORMAT_IN_PROGRESS 0x04 | |
208 | #define SCSI_ASCQ_POWER_LOSS_EXPECTED 0x08 | |
209 | #define SCSI_ASCQ_INVALID_LUN_ID 0x09 | |
210 | ||
3726897e CH |
211 | /* copied from drivers/usb/gadget/function/storage_common.h */ |
212 | static inline u32 get_unaligned_be24(u8 *buf) | |
213 | { | |
214 | return 0xffffff & (u32) get_unaligned_be32(buf - 1); | |
215 | } | |
5d0f6131 VV |
216 | |
217 | /* Struct to gather data that needs to be extracted from a SCSI CDB. | |
218 | Not conforming to any particular CDB variant, but compatible with all. */ | |
219 | ||
220 | struct nvme_trans_io_cdb { | |
221 | u8 fua; | |
222 | u8 prot_info; | |
223 | u64 lba; | |
224 | u32 xfer_len; | |
225 | }; | |
226 | ||
227 | ||
228 | /* Internal Helper Functions */ | |
229 | ||
230 | ||
231 | /* Copy data to userspace memory */ | |
232 | ||
233 | static int nvme_trans_copy_to_user(struct sg_io_hdr *hdr, void *from, | |
234 | unsigned long n) | |
235 | { | |
5d0f6131 VV |
236 | int i; |
237 | void *index = from; | |
238 | size_t remaining = n; | |
239 | size_t xfer_len; | |
240 | ||
241 | if (hdr->iovec_count > 0) { | |
8741ee4c | 242 | struct sg_iovec sgl; |
5d0f6131 VV |
243 | |
244 | for (i = 0; i < hdr->iovec_count; i++) { | |
e61b0a86 | 245 | if (copy_from_user(&sgl, hdr->dxferp + |
8741ee4c | 246 | i * sizeof(struct sg_iovec), |
e61b0a86 | 247 | sizeof(struct sg_iovec))) |
8741ee4c VV |
248 | return -EFAULT; |
249 | xfer_len = min(remaining, sgl.iov_len); | |
e61b0a86 CH |
250 | if (copy_to_user(sgl.iov_base, index, xfer_len)) |
251 | return -EFAULT; | |
252 | ||
5d0f6131 VV |
253 | index += xfer_len; |
254 | remaining -= xfer_len; | |
255 | if (remaining == 0) | |
256 | break; | |
257 | } | |
e61b0a86 | 258 | return 0; |
5d0f6131 | 259 | } |
e61b0a86 CH |
260 | |
261 | if (copy_to_user(hdr->dxferp, from, n)) | |
262 | return -EFAULT; | |
263 | return 0; | |
5d0f6131 VV |
264 | } |
265 | ||
266 | /* Copy data from userspace memory */ | |
267 | ||
268 | static int nvme_trans_copy_from_user(struct sg_io_hdr *hdr, void *to, | |
269 | unsigned long n) | |
270 | { | |
5d0f6131 VV |
271 | int i; |
272 | void *index = to; | |
273 | size_t remaining = n; | |
274 | size_t xfer_len; | |
275 | ||
276 | if (hdr->iovec_count > 0) { | |
8741ee4c | 277 | struct sg_iovec sgl; |
5d0f6131 VV |
278 | |
279 | for (i = 0; i < hdr->iovec_count; i++) { | |
e61b0a86 | 280 | if (copy_from_user(&sgl, hdr->dxferp + |
8741ee4c | 281 | i * sizeof(struct sg_iovec), |
e61b0a86 | 282 | sizeof(struct sg_iovec))) |
8741ee4c VV |
283 | return -EFAULT; |
284 | xfer_len = min(remaining, sgl.iov_len); | |
e61b0a86 CH |
285 | if (copy_from_user(index, sgl.iov_base, xfer_len)) |
286 | return -EFAULT; | |
5d0f6131 VV |
287 | index += xfer_len; |
288 | remaining -= xfer_len; | |
289 | if (remaining == 0) | |
290 | break; | |
291 | } | |
e61b0a86 | 292 | return 0; |
5d0f6131 VV |
293 | } |
294 | ||
e61b0a86 CH |
295 | if (copy_from_user(to, hdr->dxferp, n)) |
296 | return -EFAULT; | |
297 | return 0; | |
5d0f6131 VV |
298 | } |
299 | ||
300 | /* Status/Sense Buffer Writeback */ | |
301 | ||
302 | static int nvme_trans_completion(struct sg_io_hdr *hdr, u8 status, u8 sense_key, | |
303 | u8 asc, u8 ascq) | |
304 | { | |
5d0f6131 VV |
305 | u8 xfer_len; |
306 | u8 resp[DESC_FMT_SENSE_DATA_SIZE]; | |
307 | ||
308 | if (scsi_status_is_good(status)) { | |
309 | hdr->status = SAM_STAT_GOOD; | |
310 | hdr->masked_status = GOOD; | |
311 | hdr->host_status = DID_OK; | |
312 | hdr->driver_status = DRIVER_OK; | |
313 | hdr->sb_len_wr = 0; | |
314 | } else { | |
315 | hdr->status = status; | |
316 | hdr->masked_status = status >> 1; | |
317 | hdr->host_status = DID_OK; | |
318 | hdr->driver_status = DRIVER_OK; | |
319 | ||
320 | memset(resp, 0, DESC_FMT_SENSE_DATA_SIZE); | |
321 | resp[0] = DESC_FORMAT_SENSE_DATA; | |
322 | resp[1] = sense_key; | |
323 | resp[2] = asc; | |
324 | resp[3] = ascq; | |
325 | ||
326 | xfer_len = min_t(u8, hdr->mx_sb_len, DESC_FMT_SENSE_DATA_SIZE); | |
327 | hdr->sb_len_wr = xfer_len; | |
8741ee4c | 328 | if (copy_to_user(hdr->sbp, resp, xfer_len) > 0) |
e61b0a86 | 329 | return -EFAULT; |
5d0f6131 VV |
330 | } |
331 | ||
e61b0a86 | 332 | return 0; |
5d0f6131 VV |
333 | } |
334 | ||
e61b0a86 CH |
335 | /* |
336 | * Take a status code from a lowlevel routine, and if it was a positive NVMe | |
337 | * error code update the sense data based on it. In either case the passed | |
338 | * in value is returned again, unless an -EFAULT from copy_to_user overrides | |
339 | * it. | |
340 | */ | |
5d0f6131 VV |
341 | static int nvme_trans_status_code(struct sg_io_hdr *hdr, int nvme_sc) |
342 | { | |
343 | u8 status, sense_key, asc, ascq; | |
e61b0a86 | 344 | int res; |
5d0f6131 VV |
345 | |
346 | /* For non-nvme (Linux) errors, simply return the error code */ | |
347 | if (nvme_sc < 0) | |
348 | return nvme_sc; | |
349 | ||
350 | /* Mask DNR, More, and reserved fields */ | |
e61b0a86 | 351 | switch (nvme_sc & 0x7FF) { |
5d0f6131 VV |
352 | /* Generic Command Status */ |
353 | case NVME_SC_SUCCESS: | |
354 | status = SAM_STAT_GOOD; | |
355 | sense_key = NO_SENSE; | |
356 | asc = SCSI_ASC_NO_SENSE; | |
357 | ascq = SCSI_ASCQ_CAUSE_NOT_REPORTABLE; | |
358 | break; | |
359 | case NVME_SC_INVALID_OPCODE: | |
360 | status = SAM_STAT_CHECK_CONDITION; | |
361 | sense_key = ILLEGAL_REQUEST; | |
362 | asc = SCSI_ASC_ILLEGAL_COMMAND; | |
363 | ascq = SCSI_ASCQ_CAUSE_NOT_REPORTABLE; | |
364 | break; | |
365 | case NVME_SC_INVALID_FIELD: | |
366 | status = SAM_STAT_CHECK_CONDITION; | |
367 | sense_key = ILLEGAL_REQUEST; | |
368 | asc = SCSI_ASC_INVALID_CDB; | |
369 | ascq = SCSI_ASCQ_CAUSE_NOT_REPORTABLE; | |
370 | break; | |
371 | case NVME_SC_DATA_XFER_ERROR: | |
372 | status = SAM_STAT_CHECK_CONDITION; | |
373 | sense_key = MEDIUM_ERROR; | |
374 | asc = SCSI_ASC_NO_SENSE; | |
375 | ascq = SCSI_ASCQ_CAUSE_NOT_REPORTABLE; | |
376 | break; | |
377 | case NVME_SC_POWER_LOSS: | |
378 | status = SAM_STAT_TASK_ABORTED; | |
379 | sense_key = ABORTED_COMMAND; | |
380 | asc = SCSI_ASC_WARNING; | |
381 | ascq = SCSI_ASCQ_POWER_LOSS_EXPECTED; | |
382 | break; | |
383 | case NVME_SC_INTERNAL: | |
384 | status = SAM_STAT_CHECK_CONDITION; | |
385 | sense_key = HARDWARE_ERROR; | |
386 | asc = SCSI_ASC_INTERNAL_TARGET_FAILURE; | |
387 | ascq = SCSI_ASCQ_CAUSE_NOT_REPORTABLE; | |
388 | break; | |
389 | case NVME_SC_ABORT_REQ: | |
390 | status = SAM_STAT_TASK_ABORTED; | |
391 | sense_key = ABORTED_COMMAND; | |
392 | asc = SCSI_ASC_NO_SENSE; | |
393 | ascq = SCSI_ASCQ_CAUSE_NOT_REPORTABLE; | |
394 | break; | |
395 | case NVME_SC_ABORT_QUEUE: | |
396 | status = SAM_STAT_TASK_ABORTED; | |
397 | sense_key = ABORTED_COMMAND; | |
398 | asc = SCSI_ASC_NO_SENSE; | |
399 | ascq = SCSI_ASCQ_CAUSE_NOT_REPORTABLE; | |
400 | break; | |
401 | case NVME_SC_FUSED_FAIL: | |
402 | status = SAM_STAT_TASK_ABORTED; | |
403 | sense_key = ABORTED_COMMAND; | |
404 | asc = SCSI_ASC_NO_SENSE; | |
405 | ascq = SCSI_ASCQ_CAUSE_NOT_REPORTABLE; | |
406 | break; | |
407 | case NVME_SC_FUSED_MISSING: | |
408 | status = SAM_STAT_TASK_ABORTED; | |
409 | sense_key = ABORTED_COMMAND; | |
410 | asc = SCSI_ASC_NO_SENSE; | |
411 | ascq = SCSI_ASCQ_CAUSE_NOT_REPORTABLE; | |
412 | break; | |
413 | case NVME_SC_INVALID_NS: | |
414 | status = SAM_STAT_CHECK_CONDITION; | |
415 | sense_key = ILLEGAL_REQUEST; | |
416 | asc = SCSI_ASC_ACCESS_DENIED_INVALID_LUN_ID; | |
417 | ascq = SCSI_ASCQ_INVALID_LUN_ID; | |
418 | break; | |
419 | case NVME_SC_LBA_RANGE: | |
420 | status = SAM_STAT_CHECK_CONDITION; | |
421 | sense_key = ILLEGAL_REQUEST; | |
422 | asc = SCSI_ASC_ILLEGAL_BLOCK; | |
423 | ascq = SCSI_ASCQ_CAUSE_NOT_REPORTABLE; | |
424 | break; | |
425 | case NVME_SC_CAP_EXCEEDED: | |
426 | status = SAM_STAT_CHECK_CONDITION; | |
427 | sense_key = MEDIUM_ERROR; | |
428 | asc = SCSI_ASC_NO_SENSE; | |
429 | ascq = SCSI_ASCQ_CAUSE_NOT_REPORTABLE; | |
430 | break; | |
431 | case NVME_SC_NS_NOT_READY: | |
432 | status = SAM_STAT_CHECK_CONDITION; | |
433 | sense_key = NOT_READY; | |
434 | asc = SCSI_ASC_LUN_NOT_READY; | |
435 | ascq = SCSI_ASCQ_CAUSE_NOT_REPORTABLE; | |
436 | break; | |
437 | ||
438 | /* Command Specific Status */ | |
439 | case NVME_SC_INVALID_FORMAT: | |
440 | status = SAM_STAT_CHECK_CONDITION; | |
441 | sense_key = ILLEGAL_REQUEST; | |
442 | asc = SCSI_ASC_FORMAT_COMMAND_FAILED; | |
443 | ascq = SCSI_ASCQ_FORMAT_COMMAND_FAILED; | |
444 | break; | |
445 | case NVME_SC_BAD_ATTRIBUTES: | |
446 | status = SAM_STAT_CHECK_CONDITION; | |
447 | sense_key = ILLEGAL_REQUEST; | |
448 | asc = SCSI_ASC_INVALID_CDB; | |
449 | ascq = SCSI_ASCQ_CAUSE_NOT_REPORTABLE; | |
450 | break; | |
451 | ||
452 | /* Media Errors */ | |
453 | case NVME_SC_WRITE_FAULT: | |
454 | status = SAM_STAT_CHECK_CONDITION; | |
455 | sense_key = MEDIUM_ERROR; | |
456 | asc = SCSI_ASC_PERIPHERAL_DEV_WRITE_FAULT; | |
457 | ascq = SCSI_ASCQ_CAUSE_NOT_REPORTABLE; | |
458 | break; | |
459 | case NVME_SC_READ_ERROR: | |
460 | status = SAM_STAT_CHECK_CONDITION; | |
461 | sense_key = MEDIUM_ERROR; | |
462 | asc = SCSI_ASC_UNRECOVERED_READ_ERROR; | |
463 | ascq = SCSI_ASCQ_CAUSE_NOT_REPORTABLE; | |
464 | break; | |
465 | case NVME_SC_GUARD_CHECK: | |
466 | status = SAM_STAT_CHECK_CONDITION; | |
467 | sense_key = MEDIUM_ERROR; | |
468 | asc = SCSI_ASC_LOG_BLOCK_GUARD_CHECK_FAILED; | |
469 | ascq = SCSI_ASCQ_LOG_BLOCK_GUARD_CHECK_FAILED; | |
470 | break; | |
471 | case NVME_SC_APPTAG_CHECK: | |
472 | status = SAM_STAT_CHECK_CONDITION; | |
473 | sense_key = MEDIUM_ERROR; | |
474 | asc = SCSI_ASC_LOG_BLOCK_APPTAG_CHECK_FAILED; | |
475 | ascq = SCSI_ASCQ_LOG_BLOCK_APPTAG_CHECK_FAILED; | |
476 | break; | |
477 | case NVME_SC_REFTAG_CHECK: | |
478 | status = SAM_STAT_CHECK_CONDITION; | |
479 | sense_key = MEDIUM_ERROR; | |
480 | asc = SCSI_ASC_LOG_BLOCK_REFTAG_CHECK_FAILED; | |
481 | ascq = SCSI_ASCQ_LOG_BLOCK_REFTAG_CHECK_FAILED; | |
482 | break; | |
483 | case NVME_SC_COMPARE_FAILED: | |
484 | status = SAM_STAT_CHECK_CONDITION; | |
485 | sense_key = MISCOMPARE; | |
486 | asc = SCSI_ASC_MISCOMPARE_DURING_VERIFY; | |
487 | ascq = SCSI_ASCQ_CAUSE_NOT_REPORTABLE; | |
488 | break; | |
489 | case NVME_SC_ACCESS_DENIED: | |
490 | status = SAM_STAT_CHECK_CONDITION; | |
491 | sense_key = ILLEGAL_REQUEST; | |
492 | asc = SCSI_ASC_ACCESS_DENIED_INVALID_LUN_ID; | |
493 | ascq = SCSI_ASCQ_INVALID_LUN_ID; | |
494 | break; | |
495 | ||
496 | /* Unspecified/Default */ | |
497 | case NVME_SC_CMDID_CONFLICT: | |
498 | case NVME_SC_CMD_SEQ_ERROR: | |
499 | case NVME_SC_CQ_INVALID: | |
500 | case NVME_SC_QID_INVALID: | |
501 | case NVME_SC_QUEUE_SIZE: | |
502 | case NVME_SC_ABORT_LIMIT: | |
503 | case NVME_SC_ABORT_MISSING: | |
504 | case NVME_SC_ASYNC_LIMIT: | |
505 | case NVME_SC_FIRMWARE_SLOT: | |
506 | case NVME_SC_FIRMWARE_IMAGE: | |
507 | case NVME_SC_INVALID_VECTOR: | |
508 | case NVME_SC_INVALID_LOG_PAGE: | |
509 | default: | |
510 | status = SAM_STAT_CHECK_CONDITION; | |
511 | sense_key = ILLEGAL_REQUEST; | |
512 | asc = SCSI_ASC_NO_SENSE; | |
513 | ascq = SCSI_ASCQ_CAUSE_NOT_REPORTABLE; | |
514 | break; | |
515 | } | |
516 | ||
517 | res = nvme_trans_completion(hdr, status, sense_key, asc, ascq); | |
e61b0a86 | 518 | return res ? res : nvme_sc; |
5d0f6131 VV |
519 | } |
520 | ||
521 | /* INQUIRY Helper Functions */ | |
522 | ||
523 | static int nvme_trans_standard_inquiry_page(struct nvme_ns *ns, | |
524 | struct sg_io_hdr *hdr, u8 *inq_response, | |
525 | int alloc_len) | |
526 | { | |
527 | struct nvme_dev *dev = ns->dev; | |
5d0f6131 | 528 | struct nvme_id_ns *id_ns; |
e61b0a86 | 529 | int res; |
5d0f6131 VV |
530 | int nvme_sc; |
531 | int xfer_len; | |
532 | u8 resp_data_format = 0x02; | |
533 | u8 protect; | |
534 | u8 cmdque = 0x01 << 1; | |
dedf4b15 | 535 | u8 fw_offset = sizeof(dev->firmware_rev); |
5d0f6131 | 536 | |
5d0f6131 | 537 | /* nvme ns identify - use DPS value for PROTECT field */ |
d29ec824 | 538 | nvme_sc = nvme_identify_ns(dev, ns->ns_id, &id_ns); |
5d0f6131 | 539 | res = nvme_trans_status_code(hdr, nvme_sc); |
5d0f6131 | 540 | if (res) |
d29ec824 | 541 | return res; |
e61b0a86 | 542 | |
d29ec824 CH |
543 | if (id_ns->dps) |
544 | protect = 0x01; | |
545 | else | |
546 | protect = 0; | |
547 | kfree(id_ns); | |
5d0f6131 VV |
548 | |
549 | memset(inq_response, 0, STANDARD_INQUIRY_LENGTH); | |
550 | inq_response[2] = VERSION_SPC_4; | |
551 | inq_response[3] = resp_data_format; /*normaca=0 | hisup=0 */ | |
552 | inq_response[4] = ADDITIONAL_STD_INQ_LENGTH; | |
553 | inq_response[5] = protect; /* sccs=0 | acc=0 | tpgs=0 | pc3=0 */ | |
554 | inq_response[7] = cmdque; /* wbus16=0 | sync=0 | vs=0 */ | |
555 | strncpy(&inq_response[8], "NVMe ", 8); | |
556 | strncpy(&inq_response[16], dev->model, 16); | |
dedf4b15 KB |
557 | |
558 | while (dev->firmware_rev[fw_offset - 1] == ' ' && fw_offset > 4) | |
559 | fw_offset--; | |
560 | fw_offset -= 4; | |
561 | strncpy(&inq_response[32], dev->firmware_rev + fw_offset, 4); | |
5d0f6131 VV |
562 | |
563 | xfer_len = min(alloc_len, STANDARD_INQUIRY_LENGTH); | |
d29ec824 | 564 | return nvme_trans_copy_to_user(hdr, inq_response, xfer_len); |
5d0f6131 VV |
565 | } |
566 | ||
567 | static int nvme_trans_supported_vpd_pages(struct nvme_ns *ns, | |
568 | struct sg_io_hdr *hdr, u8 *inq_response, | |
569 | int alloc_len) | |
570 | { | |
5d0f6131 VV |
571 | int xfer_len; |
572 | ||
573 | memset(inq_response, 0, STANDARD_INQUIRY_LENGTH); | |
574 | inq_response[1] = INQ_SUPPORTED_VPD_PAGES_PAGE; /* Page Code */ | |
575 | inq_response[3] = INQ_NUM_SUPPORTED_VPD_PAGES; /* Page Length */ | |
576 | inq_response[4] = INQ_SUPPORTED_VPD_PAGES_PAGE; | |
577 | inq_response[5] = INQ_UNIT_SERIAL_NUMBER_PAGE; | |
578 | inq_response[6] = INQ_DEVICE_IDENTIFICATION_PAGE; | |
579 | inq_response[7] = INQ_EXTENDED_INQUIRY_DATA_PAGE; | |
580 | inq_response[8] = INQ_BDEV_CHARACTERISTICS_PAGE; | |
7f749d9c | 581 | inq_response[9] = INQ_BDEV_LIMITS_PAGE; |
5d0f6131 VV |
582 | |
583 | xfer_len = min(alloc_len, STANDARD_INQUIRY_LENGTH); | |
e61b0a86 | 584 | return nvme_trans_copy_to_user(hdr, inq_response, xfer_len); |
5d0f6131 VV |
585 | } |
586 | ||
587 | static int nvme_trans_unit_serial_page(struct nvme_ns *ns, | |
588 | struct sg_io_hdr *hdr, u8 *inq_response, | |
589 | int alloc_len) | |
590 | { | |
591 | struct nvme_dev *dev = ns->dev; | |
5d0f6131 VV |
592 | int xfer_len; |
593 | ||
594 | memset(inq_response, 0, STANDARD_INQUIRY_LENGTH); | |
595 | inq_response[1] = INQ_UNIT_SERIAL_NUMBER_PAGE; /* Page Code */ | |
596 | inq_response[3] = INQ_SERIAL_NUMBER_LENGTH; /* Page Length */ | |
597 | strncpy(&inq_response[4], dev->serial, INQ_SERIAL_NUMBER_LENGTH); | |
598 | ||
599 | xfer_len = min(alloc_len, STANDARD_INQUIRY_LENGTH); | |
e61b0a86 | 600 | return nvme_trans_copy_to_user(hdr, inq_response, xfer_len); |
5d0f6131 VV |
601 | } |
602 | ||
603 | static int nvme_trans_device_id_page(struct nvme_ns *ns, struct sg_io_hdr *hdr, | |
604 | u8 *inq_response, int alloc_len) | |
605 | { | |
606 | struct nvme_dev *dev = ns->dev; | |
e61b0a86 | 607 | int res; |
5d0f6131 | 608 | int nvme_sc; |
5d0f6131 | 609 | int xfer_len; |
8741ee4c | 610 | __be32 tmp_id = cpu_to_be32(ns->ns_id); |
5d0f6131 | 611 | |
4f1982b4 | 612 | memset(inq_response, 0, alloc_len); |
5d0f6131 | 613 | inq_response[1] = INQ_DEVICE_IDENTIFICATION_PAGE; /* Page Code */ |
4f1982b4 | 614 | if (readl(&dev->bar->vs) >= NVME_VS(1, 1)) { |
d29ec824 CH |
615 | struct nvme_id_ns *id_ns; |
616 | void *eui; | |
617 | int len; | |
5d0f6131 | 618 | |
d29ec824 | 619 | nvme_sc = nvme_identify_ns(dev, ns->ns_id, &id_ns); |
4f1982b4 KB |
620 | res = nvme_trans_status_code(hdr, nvme_sc); |
621 | if (res) | |
d29ec824 | 622 | return res; |
4f1982b4 | 623 | |
d29ec824 CH |
624 | eui = id_ns->eui64; |
625 | len = sizeof(id_ns->eui64); | |
4f1982b4 KB |
626 | if (readl(&dev->bar->vs) >= NVME_VS(1, 2)) { |
627 | if (bitmap_empty(eui, len * 8)) { | |
628 | eui = id_ns->nguid; | |
629 | len = sizeof(id_ns->nguid); | |
630 | } | |
631 | } | |
d29ec824 CH |
632 | if (bitmap_empty(eui, len * 8)) { |
633 | kfree(id_ns); | |
4f1982b4 | 634 | goto scsi_string; |
d29ec824 | 635 | } |
4f1982b4 KB |
636 | |
637 | inq_response[3] = 4 + len; /* Page Length */ | |
638 | /* Designation Descriptor start */ | |
639 | inq_response[4] = 0x01; /* Proto ID=0h | Code set=1h */ | |
640 | inq_response[5] = 0x02; /* PIV=0b | Asso=00b | Designator Type=2h */ | |
641 | inq_response[6] = 0x00; /* Rsvd */ | |
642 | inq_response[7] = len; /* Designator Length */ | |
643 | memcpy(&inq_response[8], eui, len); | |
d29ec824 | 644 | kfree(id_ns); |
4f1982b4 KB |
645 | } else { |
646 | scsi_string: | |
647 | if (alloc_len < 72) { | |
d29ec824 | 648 | return nvme_trans_completion(hdr, |
4f1982b4 KB |
649 | SAM_STAT_CHECK_CONDITION, |
650 | ILLEGAL_REQUEST, SCSI_ASC_INVALID_CDB, | |
651 | SCSI_ASCQ_CAUSE_NOT_REPORTABLE); | |
4f1982b4 KB |
652 | } |
653 | inq_response[3] = 0x48; /* Page Length */ | |
654 | /* Designation Descriptor start */ | |
655 | inq_response[4] = 0x03; /* Proto ID=0h | Code set=3h */ | |
656 | inq_response[5] = 0x08; /* PIV=0b | Asso=00b | Designator Type=8h */ | |
657 | inq_response[6] = 0x00; /* Rsvd */ | |
658 | inq_response[7] = 0x44; /* Designator Length */ | |
659 | ||
e75ec752 | 660 | sprintf(&inq_response[8], "%04x", to_pci_dev(dev->dev)->vendor); |
4f1982b4 KB |
661 | memcpy(&inq_response[12], dev->model, sizeof(dev->model)); |
662 | sprintf(&inq_response[52], "%04x", tmp_id); | |
663 | memcpy(&inq_response[56], dev->serial, sizeof(dev->serial)); | |
664 | } | |
665 | xfer_len = alloc_len; | |
d29ec824 | 666 | return nvme_trans_copy_to_user(hdr, inq_response, xfer_len); |
5d0f6131 VV |
667 | } |
668 | ||
669 | static int nvme_trans_ext_inq_page(struct nvme_ns *ns, struct sg_io_hdr *hdr, | |
670 | int alloc_len) | |
671 | { | |
672 | u8 *inq_response; | |
e61b0a86 | 673 | int res; |
5d0f6131 VV |
674 | int nvme_sc; |
675 | struct nvme_dev *dev = ns->dev; | |
5d0f6131 VV |
676 | struct nvme_id_ctrl *id_ctrl; |
677 | struct nvme_id_ns *id_ns; | |
678 | int xfer_len; | |
679 | u8 microcode = 0x80; | |
680 | u8 spt; | |
681 | u8 spt_lut[8] = {0, 0, 2, 1, 4, 6, 5, 7}; | |
682 | u8 grd_chk, app_chk, ref_chk, protect; | |
683 | u8 uask_sup = 0x20; | |
684 | u8 v_sup; | |
685 | u8 luiclr = 0x01; | |
686 | ||
687 | inq_response = kmalloc(EXTENDED_INQUIRY_DATA_PAGE_LENGTH, GFP_KERNEL); | |
d29ec824 CH |
688 | if (inq_response == NULL) |
689 | return -ENOMEM; | |
5d0f6131 | 690 | |
d29ec824 | 691 | nvme_sc = nvme_identify_ns(dev, ns->ns_id, &id_ns); |
5d0f6131 VV |
692 | res = nvme_trans_status_code(hdr, nvme_sc); |
693 | if (res) | |
d29ec824 CH |
694 | goto out_free_inq; |
695 | ||
696 | spt = spt_lut[id_ns->dpc & 0x07] << 3; | |
697 | if (id_ns->dps) | |
698 | protect = 0x01; | |
699 | else | |
700 | protect = 0; | |
701 | kfree(id_ns); | |
e61b0a86 | 702 | |
5d0f6131 VV |
703 | grd_chk = protect << 2; |
704 | app_chk = protect << 1; | |
705 | ref_chk = protect; | |
706 | ||
d29ec824 | 707 | nvme_sc = nvme_identify_ctrl(dev, &id_ctrl); |
5d0f6131 VV |
708 | res = nvme_trans_status_code(hdr, nvme_sc); |
709 | if (res) | |
d29ec824 | 710 | goto out_free_inq; |
e61b0a86 | 711 | |
5d0f6131 | 712 | v_sup = id_ctrl->vwc; |
d29ec824 | 713 | kfree(id_ctrl); |
5d0f6131 VV |
714 | |
715 | memset(inq_response, 0, EXTENDED_INQUIRY_DATA_PAGE_LENGTH); | |
716 | inq_response[1] = INQ_EXTENDED_INQUIRY_DATA_PAGE; /* Page Code */ | |
717 | inq_response[2] = 0x00; /* Page Length MSB */ | |
718 | inq_response[3] = 0x3C; /* Page Length LSB */ | |
719 | inq_response[4] = microcode | spt | grd_chk | app_chk | ref_chk; | |
720 | inq_response[5] = uask_sup; | |
721 | inq_response[6] = v_sup; | |
722 | inq_response[7] = luiclr; | |
723 | inq_response[8] = 0; | |
724 | inq_response[9] = 0; | |
725 | ||
726 | xfer_len = min(alloc_len, EXTENDED_INQUIRY_DATA_PAGE_LENGTH); | |
727 | res = nvme_trans_copy_to_user(hdr, inq_response, xfer_len); | |
728 | ||
d29ec824 | 729 | out_free_inq: |
5d0f6131 | 730 | kfree(inq_response); |
5d0f6131 VV |
731 | return res; |
732 | } | |
733 | ||
7f749d9c KB |
734 | static int nvme_trans_bdev_limits_page(struct nvme_ns *ns, struct sg_io_hdr *hdr, |
735 | u8 *inq_response, int alloc_len) | |
736 | { | |
737 | __be32 max_sectors = cpu_to_be32(queue_max_hw_sectors(ns->queue)); | |
738 | __be32 max_discard = cpu_to_be32(ns->queue->limits.max_discard_sectors); | |
739 | __be32 discard_desc_count = cpu_to_be32(0x100); | |
740 | ||
741 | memset(inq_response, 0, STANDARD_INQUIRY_LENGTH); | |
742 | inq_response[1] = VPD_BLOCK_LIMITS; | |
743 | inq_response[3] = 0x3c; /* Page Length */ | |
744 | memcpy(&inq_response[8], &max_sectors, sizeof(u32)); | |
745 | memcpy(&inq_response[20], &max_discard, sizeof(u32)); | |
746 | ||
747 | if (max_discard) | |
748 | memcpy(&inq_response[24], &discard_desc_count, sizeof(u32)); | |
749 | ||
750 | return nvme_trans_copy_to_user(hdr, inq_response, 0x3c); | |
751 | } | |
752 | ||
5d0f6131 VV |
753 | static int nvme_trans_bdev_char_page(struct nvme_ns *ns, struct sg_io_hdr *hdr, |
754 | int alloc_len) | |
755 | { | |
756 | u8 *inq_response; | |
e61b0a86 | 757 | int res; |
5d0f6131 VV |
758 | int xfer_len; |
759 | ||
03ea83e9 | 760 | inq_response = kzalloc(EXTENDED_INQUIRY_DATA_PAGE_LENGTH, GFP_KERNEL); |
5d0f6131 VV |
761 | if (inq_response == NULL) { |
762 | res = -ENOMEM; | |
763 | goto out_mem; | |
764 | } | |
765 | ||
5d0f6131 VV |
766 | inq_response[1] = INQ_BDEV_CHARACTERISTICS_PAGE; /* Page Code */ |
767 | inq_response[2] = 0x00; /* Page Length MSB */ | |
768 | inq_response[3] = 0x3C; /* Page Length LSB */ | |
769 | inq_response[4] = 0x00; /* Medium Rotation Rate MSB */ | |
770 | inq_response[5] = 0x01; /* Medium Rotation Rate LSB */ | |
771 | inq_response[6] = 0x00; /* Form Factor */ | |
772 | ||
773 | xfer_len = min(alloc_len, EXTENDED_INQUIRY_DATA_PAGE_LENGTH); | |
774 | res = nvme_trans_copy_to_user(hdr, inq_response, xfer_len); | |
775 | ||
776 | kfree(inq_response); | |
777 | out_mem: | |
778 | return res; | |
779 | } | |
780 | ||
781 | /* LOG SENSE Helper Functions */ | |
782 | ||
783 | static int nvme_trans_log_supp_pages(struct nvme_ns *ns, struct sg_io_hdr *hdr, | |
784 | int alloc_len) | |
785 | { | |
e61b0a86 | 786 | int res; |
5d0f6131 VV |
787 | int xfer_len; |
788 | u8 *log_response; | |
789 | ||
03ea83e9 | 790 | log_response = kzalloc(LOG_PAGE_SUPPORTED_LOG_PAGES_LENGTH, GFP_KERNEL); |
5d0f6131 VV |
791 | if (log_response == NULL) { |
792 | res = -ENOMEM; | |
793 | goto out_mem; | |
794 | } | |
5d0f6131 VV |
795 | |
796 | log_response[0] = LOG_PAGE_SUPPORTED_LOG_PAGES_PAGE; | |
797 | /* Subpage=0x00, Page Length MSB=0 */ | |
798 | log_response[3] = SUPPORTED_LOG_PAGES_PAGE_LENGTH; | |
799 | log_response[4] = LOG_PAGE_SUPPORTED_LOG_PAGES_PAGE; | |
800 | log_response[5] = LOG_PAGE_INFORMATIONAL_EXCEPTIONS_PAGE; | |
801 | log_response[6] = LOG_PAGE_TEMPERATURE_PAGE; | |
802 | ||
803 | xfer_len = min(alloc_len, LOG_PAGE_SUPPORTED_LOG_PAGES_LENGTH); | |
804 | res = nvme_trans_copy_to_user(hdr, log_response, xfer_len); | |
805 | ||
806 | kfree(log_response); | |
807 | out_mem: | |
808 | return res; | |
809 | } | |
810 | ||
811 | static int nvme_trans_log_info_exceptions(struct nvme_ns *ns, | |
812 | struct sg_io_hdr *hdr, int alloc_len) | |
813 | { | |
e61b0a86 | 814 | int res; |
5d0f6131 VV |
815 | int xfer_len; |
816 | u8 *log_response; | |
5d0f6131 VV |
817 | struct nvme_dev *dev = ns->dev; |
818 | struct nvme_smart_log *smart_log; | |
5d0f6131 VV |
819 | u8 temp_c; |
820 | u16 temp_k; | |
821 | ||
03ea83e9 | 822 | log_response = kzalloc(LOG_INFO_EXCP_PAGE_LENGTH, GFP_KERNEL); |
d29ec824 CH |
823 | if (log_response == NULL) |
824 | return -ENOMEM; | |
5d0f6131 | 825 | |
d29ec824 CH |
826 | res = nvme_get_log_page(dev, &smart_log); |
827 | if (res < 0) | |
828 | goto out_free_response; | |
5d0f6131 | 829 | |
5d0f6131 VV |
830 | if (res != NVME_SC_SUCCESS) { |
831 | temp_c = LOG_TEMP_UNKNOWN; | |
832 | } else { | |
5d0f6131 VV |
833 | temp_k = (smart_log->temperature[1] << 8) + |
834 | (smart_log->temperature[0]); | |
835 | temp_c = temp_k - KELVIN_TEMP_FACTOR; | |
836 | } | |
d29ec824 | 837 | kfree(smart_log); |
5d0f6131 VV |
838 | |
839 | log_response[0] = LOG_PAGE_INFORMATIONAL_EXCEPTIONS_PAGE; | |
840 | /* Subpage=0x00, Page Length MSB=0 */ | |
841 | log_response[3] = REMAINING_INFO_EXCP_PAGE_LENGTH; | |
842 | /* Informational Exceptions Log Parameter 1 Start */ | |
843 | /* Parameter Code=0x0000 bytes 4,5 */ | |
844 | log_response[6] = 0x23; /* DU=0, TSD=1, ETC=0, TMC=0, FMT_AND_LNK=11b */ | |
845 | log_response[7] = 0x04; /* PARAMETER LENGTH */ | |
846 | /* Add sense Code and qualifier = 0x00 each */ | |
847 | /* Use Temperature from NVMe Get Log Page, convert to C from K */ | |
848 | log_response[10] = temp_c; | |
849 | ||
850 | xfer_len = min(alloc_len, LOG_INFO_EXCP_PAGE_LENGTH); | |
851 | res = nvme_trans_copy_to_user(hdr, log_response, xfer_len); | |
852 | ||
d29ec824 | 853 | out_free_response: |
5d0f6131 | 854 | kfree(log_response); |
5d0f6131 VV |
855 | return res; |
856 | } | |
857 | ||
858 | static int nvme_trans_log_temperature(struct nvme_ns *ns, struct sg_io_hdr *hdr, | |
859 | int alloc_len) | |
860 | { | |
e61b0a86 | 861 | int res; |
5d0f6131 VV |
862 | int xfer_len; |
863 | u8 *log_response; | |
5d0f6131 VV |
864 | struct nvme_dev *dev = ns->dev; |
865 | struct nvme_smart_log *smart_log; | |
5d0f6131 VV |
866 | u32 feature_resp; |
867 | u8 temp_c_cur, temp_c_thresh; | |
868 | u16 temp_k; | |
869 | ||
03ea83e9 | 870 | log_response = kzalloc(LOG_TEMP_PAGE_LENGTH, GFP_KERNEL); |
d29ec824 CH |
871 | if (log_response == NULL) |
872 | return -ENOMEM; | |
5d0f6131 | 873 | |
d29ec824 CH |
874 | res = nvme_get_log_page(dev, &smart_log); |
875 | if (res < 0) | |
876 | goto out_free_response; | |
5d0f6131 | 877 | |
5d0f6131 VV |
878 | if (res != NVME_SC_SUCCESS) { |
879 | temp_c_cur = LOG_TEMP_UNKNOWN; | |
880 | } else { | |
5d0f6131 VV |
881 | temp_k = (smart_log->temperature[1] << 8) + |
882 | (smart_log->temperature[0]); | |
883 | temp_c_cur = temp_k - KELVIN_TEMP_FACTOR; | |
884 | } | |
d29ec824 | 885 | kfree(smart_log); |
5d0f6131 VV |
886 | |
887 | /* Get Features for Temp Threshold */ | |
888 | res = nvme_get_features(dev, NVME_FEAT_TEMP_THRESH, 0, 0, | |
889 | &feature_resp); | |
890 | if (res != NVME_SC_SUCCESS) | |
891 | temp_c_thresh = LOG_TEMP_UNKNOWN; | |
892 | else | |
893 | temp_c_thresh = (feature_resp & 0xFFFF) - KELVIN_TEMP_FACTOR; | |
894 | ||
895 | log_response[0] = LOG_PAGE_TEMPERATURE_PAGE; | |
896 | /* Subpage=0x00, Page Length MSB=0 */ | |
897 | log_response[3] = REMAINING_TEMP_PAGE_LENGTH; | |
898 | /* Temperature Log Parameter 1 (Temperature) Start */ | |
899 | /* Parameter Code = 0x0000 */ | |
900 | log_response[6] = 0x01; /* Format and Linking = 01b */ | |
901 | log_response[7] = 0x02; /* Parameter Length */ | |
902 | /* Use Temperature from NVMe Get Log Page, convert to C from K */ | |
903 | log_response[9] = temp_c_cur; | |
904 | /* Temperature Log Parameter 2 (Reference Temperature) Start */ | |
905 | log_response[11] = 0x01; /* Parameter Code = 0x0001 */ | |
906 | log_response[12] = 0x01; /* Format and Linking = 01b */ | |
907 | log_response[13] = 0x02; /* Parameter Length */ | |
908 | /* Use Temperature Thresh from NVMe Get Log Page, convert to C from K */ | |
909 | log_response[15] = temp_c_thresh; | |
910 | ||
911 | xfer_len = min(alloc_len, LOG_TEMP_PAGE_LENGTH); | |
912 | res = nvme_trans_copy_to_user(hdr, log_response, xfer_len); | |
913 | ||
d29ec824 | 914 | out_free_response: |
5d0f6131 | 915 | kfree(log_response); |
5d0f6131 VV |
916 | return res; |
917 | } | |
918 | ||
919 | /* MODE SENSE Helper Functions */ | |
920 | ||
921 | static int nvme_trans_fill_mode_parm_hdr(u8 *resp, int len, u8 cdb10, u8 llbaa, | |
922 | u16 mode_data_length, u16 blk_desc_len) | |
923 | { | |
924 | /* Quick check to make sure I don't stomp on my own memory... */ | |
925 | if ((cdb10 && len < 8) || (!cdb10 && len < 4)) | |
e61b0a86 | 926 | return -EINVAL; |
5d0f6131 VV |
927 | |
928 | if (cdb10) { | |
929 | resp[0] = (mode_data_length & 0xFF00) >> 8; | |
930 | resp[1] = (mode_data_length & 0x00FF); | |
90851768 | 931 | resp[3] = 0x10 /* DPOFUA */; |
5d0f6131 VV |
932 | resp[4] = llbaa; |
933 | resp[5] = RESERVED_FIELD; | |
934 | resp[6] = (blk_desc_len & 0xFF00) >> 8; | |
935 | resp[7] = (blk_desc_len & 0x00FF); | |
936 | } else { | |
937 | resp[0] = (mode_data_length & 0x00FF); | |
90851768 | 938 | resp[2] = 0x10 /* DPOFUA */; |
5d0f6131 VV |
939 | resp[3] = (blk_desc_len & 0x00FF); |
940 | } | |
941 | ||
e61b0a86 | 942 | return 0; |
5d0f6131 VV |
943 | } |
944 | ||
945 | static int nvme_trans_fill_blk_desc(struct nvme_ns *ns, struct sg_io_hdr *hdr, | |
946 | u8 *resp, int len, u8 llbaa) | |
947 | { | |
e61b0a86 | 948 | int res; |
5d0f6131 VV |
949 | int nvme_sc; |
950 | struct nvme_dev *dev = ns->dev; | |
5d0f6131 VV |
951 | struct nvme_id_ns *id_ns; |
952 | u8 flbas; | |
953 | u32 lba_length; | |
954 | ||
955 | if (llbaa == 0 && len < MODE_PAGE_BLK_DES_LEN) | |
e61b0a86 | 956 | return -EINVAL; |
5d0f6131 | 957 | else if (llbaa > 0 && len < MODE_PAGE_LLBAA_BLK_DES_LEN) |
e61b0a86 | 958 | return -EINVAL; |
5d0f6131 | 959 | |
d29ec824 | 960 | nvme_sc = nvme_identify_ns(dev, ns->ns_id, &id_ns); |
5d0f6131 VV |
961 | res = nvme_trans_status_code(hdr, nvme_sc); |
962 | if (res) | |
d29ec824 | 963 | return res; |
e61b0a86 | 964 | |
5d0f6131 VV |
965 | flbas = (id_ns->flbas) & 0x0F; |
966 | lba_length = (1 << (id_ns->lbaf[flbas].ds)); | |
967 | ||
968 | if (llbaa == 0) { | |
8741ee4c | 969 | __be32 tmp_cap = cpu_to_be32(le64_to_cpu(id_ns->ncap)); |
5d0f6131 | 970 | /* Byte 4 is reserved */ |
8741ee4c | 971 | __be32 tmp_len = cpu_to_be32(lba_length & 0x00FFFFFF); |
5d0f6131 VV |
972 | |
973 | memcpy(resp, &tmp_cap, sizeof(u32)); | |
974 | memcpy(&resp[4], &tmp_len, sizeof(u32)); | |
975 | } else { | |
8741ee4c VV |
976 | __be64 tmp_cap = cpu_to_be64(le64_to_cpu(id_ns->ncap)); |
977 | __be32 tmp_len = cpu_to_be32(lba_length); | |
5d0f6131 VV |
978 | |
979 | memcpy(resp, &tmp_cap, sizeof(u64)); | |
980 | /* Bytes 8, 9, 10, 11 are reserved */ | |
981 | memcpy(&resp[12], &tmp_len, sizeof(u32)); | |
982 | } | |
983 | ||
d29ec824 | 984 | kfree(id_ns); |
5d0f6131 VV |
985 | return res; |
986 | } | |
987 | ||
988 | static int nvme_trans_fill_control_page(struct nvme_ns *ns, | |
989 | struct sg_io_hdr *hdr, u8 *resp, | |
990 | int len) | |
991 | { | |
992 | if (len < MODE_PAGE_CONTROL_LEN) | |
e61b0a86 | 993 | return -EINVAL; |
5d0f6131 VV |
994 | |
995 | resp[0] = MODE_PAGE_CONTROL; | |
996 | resp[1] = MODE_PAGE_CONTROL_LEN_FIELD; | |
997 | resp[2] = 0x0E; /* TST=000b, TMF_ONLY=0, DPICZ=1, | |
998 | * D_SENSE=1, GLTSD=1, RLEC=0 */ | |
999 | resp[3] = 0x12; /* Q_ALGO_MODIFIER=1h, NUAR=0, QERR=01b */ | |
1000 | /* Byte 4: VS=0, RAC=0, UA_INT=0, SWP=0 */ | |
1001 | resp[5] = 0x40; /* ATO=0, TAS=1, ATMPE=0, RWWP=0, AUTOLOAD=0 */ | |
1002 | /* resp[6] and [7] are obsolete, thus zero */ | |
1003 | resp[8] = 0xFF; /* Busy timeout period = 0xffff */ | |
1004 | resp[9] = 0xFF; | |
1005 | /* Bytes 10,11: Extended selftest completion time = 0x0000 */ | |
1006 | ||
e61b0a86 | 1007 | return 0; |
5d0f6131 VV |
1008 | } |
1009 | ||
1010 | static int nvme_trans_fill_caching_page(struct nvme_ns *ns, | |
1011 | struct sg_io_hdr *hdr, | |
1012 | u8 *resp, int len) | |
1013 | { | |
e61b0a86 | 1014 | int res = 0; |
5d0f6131 VV |
1015 | int nvme_sc; |
1016 | struct nvme_dev *dev = ns->dev; | |
1017 | u32 feature_resp; | |
1018 | u8 vwc; | |
1019 | ||
1020 | if (len < MODE_PAGE_CACHING_LEN) | |
e61b0a86 | 1021 | return -EINVAL; |
5d0f6131 VV |
1022 | |
1023 | nvme_sc = nvme_get_features(dev, NVME_FEAT_VOLATILE_WC, 0, 0, | |
1024 | &feature_resp); | |
1025 | res = nvme_trans_status_code(hdr, nvme_sc); | |
1026 | if (res) | |
e61b0a86 CH |
1027 | return res; |
1028 | ||
5d0f6131 VV |
1029 | vwc = feature_resp & 0x00000001; |
1030 | ||
1031 | resp[0] = MODE_PAGE_CACHING; | |
1032 | resp[1] = MODE_PAGE_CACHING_LEN_FIELD; | |
1033 | resp[2] = vwc << 2; | |
e61b0a86 | 1034 | return 0; |
5d0f6131 VV |
1035 | } |
1036 | ||
1037 | static int nvme_trans_fill_pow_cnd_page(struct nvme_ns *ns, | |
1038 | struct sg_io_hdr *hdr, u8 *resp, | |
1039 | int len) | |
1040 | { | |
5d0f6131 | 1041 | if (len < MODE_PAGE_POW_CND_LEN) |
e61b0a86 | 1042 | return -EINVAL; |
5d0f6131 VV |
1043 | |
1044 | resp[0] = MODE_PAGE_POWER_CONDITION; | |
1045 | resp[1] = MODE_PAGE_POW_CND_LEN_FIELD; | |
1046 | /* All other bytes are zero */ | |
1047 | ||
e61b0a86 | 1048 | return 0; |
5d0f6131 VV |
1049 | } |
1050 | ||
1051 | static int nvme_trans_fill_inf_exc_page(struct nvme_ns *ns, | |
1052 | struct sg_io_hdr *hdr, u8 *resp, | |
1053 | int len) | |
1054 | { | |
5d0f6131 | 1055 | if (len < MODE_PAGE_INF_EXC_LEN) |
e61b0a86 | 1056 | return -EINVAL; |
5d0f6131 VV |
1057 | |
1058 | resp[0] = MODE_PAGE_INFO_EXCEP; | |
1059 | resp[1] = MODE_PAGE_INF_EXC_LEN_FIELD; | |
1060 | resp[2] = 0x88; | |
1061 | /* All other bytes are zero */ | |
1062 | ||
e61b0a86 | 1063 | return 0; |
5d0f6131 VV |
1064 | } |
1065 | ||
1066 | static int nvme_trans_fill_all_pages(struct nvme_ns *ns, struct sg_io_hdr *hdr, | |
1067 | u8 *resp, int len) | |
1068 | { | |
e61b0a86 | 1069 | int res; |
5d0f6131 VV |
1070 | u16 mode_pages_offset_1 = 0; |
1071 | u16 mode_pages_offset_2, mode_pages_offset_3, mode_pages_offset_4; | |
1072 | ||
1073 | mode_pages_offset_2 = mode_pages_offset_1 + MODE_PAGE_CACHING_LEN; | |
1074 | mode_pages_offset_3 = mode_pages_offset_2 + MODE_PAGE_CONTROL_LEN; | |
1075 | mode_pages_offset_4 = mode_pages_offset_3 + MODE_PAGE_POW_CND_LEN; | |
1076 | ||
1077 | res = nvme_trans_fill_caching_page(ns, hdr, &resp[mode_pages_offset_1], | |
1078 | MODE_PAGE_CACHING_LEN); | |
e61b0a86 CH |
1079 | if (res) |
1080 | return res; | |
5d0f6131 VV |
1081 | res = nvme_trans_fill_control_page(ns, hdr, &resp[mode_pages_offset_2], |
1082 | MODE_PAGE_CONTROL_LEN); | |
e61b0a86 CH |
1083 | if (res) |
1084 | return res; | |
5d0f6131 VV |
1085 | res = nvme_trans_fill_pow_cnd_page(ns, hdr, &resp[mode_pages_offset_3], |
1086 | MODE_PAGE_POW_CND_LEN); | |
e61b0a86 CH |
1087 | if (res) |
1088 | return res; | |
1089 | return nvme_trans_fill_inf_exc_page(ns, hdr, &resp[mode_pages_offset_4], | |
5d0f6131 | 1090 | MODE_PAGE_INF_EXC_LEN); |
5d0f6131 VV |
1091 | } |
1092 | ||
1093 | static inline int nvme_trans_get_blk_desc_len(u8 dbd, u8 llbaa) | |
1094 | { | |
1095 | if (dbd == MODE_SENSE_BLK_DESC_ENABLED) { | |
1096 | /* SPC-4: len = 8 x Num_of_descriptors if llbaa = 0, 16x if 1 */ | |
1097 | return 8 * (llbaa + 1) * MODE_SENSE_BLK_DESC_COUNT; | |
1098 | } else { | |
1099 | return 0; | |
1100 | } | |
1101 | } | |
1102 | ||
1103 | static int nvme_trans_mode_page_create(struct nvme_ns *ns, | |
1104 | struct sg_io_hdr *hdr, u8 *cmd, | |
1105 | u16 alloc_len, u8 cdb10, | |
1106 | int (*mode_page_fill_func) | |
1107 | (struct nvme_ns *, | |
1108 | struct sg_io_hdr *hdr, u8 *, int), | |
1109 | u16 mode_pages_tot_len) | |
1110 | { | |
e61b0a86 | 1111 | int res; |
5d0f6131 VV |
1112 | int xfer_len; |
1113 | u8 *response; | |
1114 | u8 dbd, llbaa; | |
1115 | u16 resp_size; | |
1116 | int mph_size; | |
1117 | u16 mode_pages_offset_1; | |
1118 | u16 blk_desc_len, blk_desc_offset, mode_data_length; | |
1119 | ||
3726897e CH |
1120 | dbd = (cmd[1] & MODE_SENSE_DBD_MASK) >> MODE_SENSE_DBD_SHIFT; |
1121 | llbaa = (cmd[1] & MODE_SENSE_LLBAA_MASK) >> MODE_SENSE_LLBAA_SHIFT; | |
1122 | mph_size = cdb10 ? MODE_SENSE10_MPH_SIZE : MODE_SENSE6_MPH_SIZE; | |
1123 | ||
5d0f6131 VV |
1124 | blk_desc_len = nvme_trans_get_blk_desc_len(dbd, llbaa); |
1125 | ||
1126 | resp_size = mph_size + blk_desc_len + mode_pages_tot_len; | |
1127 | /* Refer spc4r34 Table 440 for calculation of Mode data Length field */ | |
1128 | mode_data_length = 3 + (3 * cdb10) + blk_desc_len + mode_pages_tot_len; | |
1129 | ||
1130 | blk_desc_offset = mph_size; | |
1131 | mode_pages_offset_1 = blk_desc_offset + blk_desc_len; | |
1132 | ||
03ea83e9 | 1133 | response = kzalloc(resp_size, GFP_KERNEL); |
5d0f6131 VV |
1134 | if (response == NULL) { |
1135 | res = -ENOMEM; | |
1136 | goto out_mem; | |
1137 | } | |
5d0f6131 VV |
1138 | |
1139 | res = nvme_trans_fill_mode_parm_hdr(&response[0], mph_size, cdb10, | |
1140 | llbaa, mode_data_length, blk_desc_len); | |
e61b0a86 | 1141 | if (res) |
5d0f6131 VV |
1142 | goto out_free; |
1143 | if (blk_desc_len > 0) { | |
1144 | res = nvme_trans_fill_blk_desc(ns, hdr, | |
1145 | &response[blk_desc_offset], | |
1146 | blk_desc_len, llbaa); | |
e61b0a86 | 1147 | if (res) |
5d0f6131 VV |
1148 | goto out_free; |
1149 | } | |
1150 | res = mode_page_fill_func(ns, hdr, &response[mode_pages_offset_1], | |
1151 | mode_pages_tot_len); | |
e61b0a86 | 1152 | if (res) |
5d0f6131 VV |
1153 | goto out_free; |
1154 | ||
1155 | xfer_len = min(alloc_len, resp_size); | |
1156 | res = nvme_trans_copy_to_user(hdr, response, xfer_len); | |
1157 | ||
1158 | out_free: | |
1159 | kfree(response); | |
1160 | out_mem: | |
1161 | return res; | |
1162 | } | |
1163 | ||
1164 | /* Read Capacity Helper Functions */ | |
1165 | ||
1166 | static void nvme_trans_fill_read_cap(u8 *response, struct nvme_id_ns *id_ns, | |
1167 | u8 cdb16) | |
1168 | { | |
1169 | u8 flbas; | |
1170 | u32 lba_length; | |
1171 | u64 rlba; | |
1172 | u8 prot_en; | |
1173 | u8 p_type_lut[4] = {0, 0, 1, 2}; | |
8741ee4c VV |
1174 | __be64 tmp_rlba; |
1175 | __be32 tmp_rlba_32; | |
1176 | __be32 tmp_len; | |
5d0f6131 VV |
1177 | |
1178 | flbas = (id_ns->flbas) & 0x0F; | |
1179 | lba_length = (1 << (id_ns->lbaf[flbas].ds)); | |
1180 | rlba = le64_to_cpup(&id_ns->nsze) - 1; | |
1181 | (id_ns->dps) ? (prot_en = 0x01) : (prot_en = 0); | |
1182 | ||
1183 | if (!cdb16) { | |
1184 | if (rlba > 0xFFFFFFFF) | |
1185 | rlba = 0xFFFFFFFF; | |
1186 | tmp_rlba_32 = cpu_to_be32(rlba); | |
1187 | tmp_len = cpu_to_be32(lba_length); | |
1188 | memcpy(response, &tmp_rlba_32, sizeof(u32)); | |
1189 | memcpy(&response[4], &tmp_len, sizeof(u32)); | |
1190 | } else { | |
1191 | tmp_rlba = cpu_to_be64(rlba); | |
1192 | tmp_len = cpu_to_be32(lba_length); | |
1193 | memcpy(response, &tmp_rlba, sizeof(u64)); | |
1194 | memcpy(&response[8], &tmp_len, sizeof(u32)); | |
1195 | response[12] = (p_type_lut[id_ns->dps & 0x3] << 1) | prot_en; | |
1196 | /* P_I_Exponent = 0x0 | LBPPBE = 0x0 */ | |
1197 | /* LBPME = 0 | LBPRZ = 0 | LALBA = 0x00 */ | |
1198 | /* Bytes 16-31 - Reserved */ | |
1199 | } | |
1200 | } | |
1201 | ||
1202 | /* Start Stop Unit Helper Functions */ | |
1203 | ||
1204 | static int nvme_trans_power_state(struct nvme_ns *ns, struct sg_io_hdr *hdr, | |
1205 | u8 pc, u8 pcmod, u8 start) | |
1206 | { | |
e61b0a86 | 1207 | int res; |
5d0f6131 VV |
1208 | int nvme_sc; |
1209 | struct nvme_dev *dev = ns->dev; | |
5d0f6131 VV |
1210 | struct nvme_id_ctrl *id_ctrl; |
1211 | int lowest_pow_st; /* max npss = lowest power consumption */ | |
1212 | unsigned ps_desired = 0; | |
1213 | ||
d29ec824 | 1214 | nvme_sc = nvme_identify_ctrl(dev, &id_ctrl); |
5d0f6131 VV |
1215 | res = nvme_trans_status_code(hdr, nvme_sc); |
1216 | if (res) | |
d29ec824 | 1217 | return res; |
e61b0a86 | 1218 | |
b8e08084 | 1219 | lowest_pow_st = max(POWER_STATE_0, (int)(id_ctrl->npss - 1)); |
d29ec824 | 1220 | kfree(id_ctrl); |
5d0f6131 VV |
1221 | |
1222 | switch (pc) { | |
1223 | case NVME_POWER_STATE_START_VALID: | |
1224 | /* Action unspecified if POWER CONDITION MODIFIER != 0 */ | |
1225 | if (pcmod == 0 && start == 0x1) | |
1226 | ps_desired = POWER_STATE_0; | |
1227 | if (pcmod == 0 && start == 0x0) | |
1228 | ps_desired = lowest_pow_st; | |
1229 | break; | |
1230 | case NVME_POWER_STATE_ACTIVE: | |
1231 | /* Action unspecified if POWER CONDITION MODIFIER != 0 */ | |
1232 | if (pcmod == 0) | |
1233 | ps_desired = POWER_STATE_0; | |
1234 | break; | |
1235 | case NVME_POWER_STATE_IDLE: | |
1236 | /* Action unspecified if POWER CONDITION MODIFIER != [0,1,2] */ | |
5d0f6131 | 1237 | if (pcmod == 0x0) |
b8e08084 | 1238 | ps_desired = POWER_STATE_1; |
5d0f6131 | 1239 | else if (pcmod == 0x1) |
b8e08084 | 1240 | ps_desired = POWER_STATE_2; |
5d0f6131 | 1241 | else if (pcmod == 0x2) |
b8e08084 | 1242 | ps_desired = POWER_STATE_3; |
5d0f6131 VV |
1243 | break; |
1244 | case NVME_POWER_STATE_STANDBY: | |
1245 | /* Action unspecified if POWER CONDITION MODIFIER != [0,1] */ | |
1246 | if (pcmod == 0x0) | |
b8e08084 | 1247 | ps_desired = max(POWER_STATE_0, (lowest_pow_st - 2)); |
5d0f6131 | 1248 | else if (pcmod == 0x1) |
b8e08084 | 1249 | ps_desired = max(POWER_STATE_0, (lowest_pow_st - 1)); |
5d0f6131 VV |
1250 | break; |
1251 | case NVME_POWER_STATE_LU_CONTROL: | |
1252 | default: | |
1253 | res = nvme_trans_completion(hdr, SAM_STAT_CHECK_CONDITION, | |
1254 | ILLEGAL_REQUEST, SCSI_ASC_INVALID_CDB, | |
1255 | SCSI_ASCQ_CAUSE_NOT_REPORTABLE); | |
1256 | break; | |
1257 | } | |
1258 | nvme_sc = nvme_set_features(dev, NVME_FEAT_POWER_MGMT, ps_desired, 0, | |
1259 | NULL); | |
d29ec824 | 1260 | return nvme_trans_status_code(hdr, nvme_sc); |
5d0f6131 VV |
1261 | } |
1262 | ||
b90c48d0 CH |
1263 | static int nvme_trans_send_activate_fw_cmd(struct nvme_ns *ns, struct sg_io_hdr *hdr, |
1264 | u8 buffer_id) | |
1265 | { | |
1266 | struct nvme_command c; | |
1267 | int nvme_sc; | |
b90c48d0 CH |
1268 | |
1269 | memset(&c, 0, sizeof(c)); | |
1270 | c.common.opcode = nvme_admin_activate_fw; | |
1271 | c.common.cdw10[0] = cpu_to_le32(buffer_id | NVME_FWACT_REPL_ACTV); | |
1272 | ||
d29ec824 | 1273 | nvme_sc = nvme_submit_sync_cmd(ns->queue, &c, NULL, 0); |
e61b0a86 | 1274 | return nvme_trans_status_code(hdr, nvme_sc); |
b90c48d0 | 1275 | } |
5d0f6131 | 1276 | |
b90c48d0 | 1277 | static int nvme_trans_send_download_fw_cmd(struct nvme_ns *ns, struct sg_io_hdr *hdr, |
5d0f6131 VV |
1278 | u8 opcode, u32 tot_len, u32 offset, |
1279 | u8 buffer_id) | |
1280 | { | |
5d0f6131 VV |
1281 | int nvme_sc; |
1282 | struct nvme_dev *dev = ns->dev; | |
1283 | struct nvme_command c; | |
5d0f6131 | 1284 | |
b90c48d0 CH |
1285 | if (hdr->iovec_count > 0) { |
1286 | /* Assuming SGL is not allowed for this command */ | |
1287 | return nvme_trans_completion(hdr, | |
1288 | SAM_STAT_CHECK_CONDITION, | |
1289 | ILLEGAL_REQUEST, | |
1290 | SCSI_ASC_INVALID_CDB, | |
1291 | SCSI_ASCQ_CAUSE_NOT_REPORTABLE); | |
1292 | } | |
5d0f6131 | 1293 | |
d29ec824 CH |
1294 | memset(&c, 0, sizeof(c)); |
1295 | c.common.opcode = nvme_admin_download_fw; | |
b90c48d0 CH |
1296 | c.dlfw.numd = cpu_to_le32((tot_len/BYTES_TO_DWORDS) - 1); |
1297 | c.dlfw.offset = cpu_to_le32(offset/BYTES_TO_DWORDS); | |
1298 | ||
d29ec824 CH |
1299 | nvme_sc = __nvme_submit_sync_cmd(dev->admin_q, &c, NULL, |
1300 | hdr->dxferp, tot_len, NULL, 0); | |
1301 | return nvme_trans_status_code(hdr, nvme_sc); | |
5d0f6131 VV |
1302 | } |
1303 | ||
1304 | /* Mode Select Helper Functions */ | |
1305 | ||
1306 | static inline void nvme_trans_modesel_get_bd_len(u8 *parm_list, u8 cdb10, | |
1307 | u16 *bd_len, u8 *llbaa) | |
1308 | { | |
1309 | if (cdb10) { | |
1310 | /* 10 Byte CDB */ | |
1311 | *bd_len = (parm_list[MODE_SELECT_10_BD_OFFSET] << 8) + | |
1312 | parm_list[MODE_SELECT_10_BD_OFFSET + 1]; | |
9ac16938 | 1313 | *llbaa = parm_list[MODE_SELECT_10_LLBAA_OFFSET] & |
5d0f6131 VV |
1314 | MODE_SELECT_10_LLBAA_MASK; |
1315 | } else { | |
1316 | /* 6 Byte CDB */ | |
1317 | *bd_len = parm_list[MODE_SELECT_6_BD_OFFSET]; | |
1318 | } | |
1319 | } | |
1320 | ||
1321 | static void nvme_trans_modesel_save_bd(struct nvme_ns *ns, u8 *parm_list, | |
1322 | u16 idx, u16 bd_len, u8 llbaa) | |
1323 | { | |
1324 | u16 bd_num; | |
1325 | ||
1326 | bd_num = bd_len / ((llbaa == 0) ? | |
1327 | SHORT_DESC_BLOCK : LONG_DESC_BLOCK); | |
1328 | /* Store block descriptor info if a FORMAT UNIT comes later */ | |
1329 | /* TODO Saving 1st BD info; what to do if multiple BD received? */ | |
1330 | if (llbaa == 0) { | |
1331 | /* Standard Block Descriptor - spc4r34 7.5.5.1 */ | |
1332 | ns->mode_select_num_blocks = | |
1333 | (parm_list[idx + 1] << 16) + | |
1334 | (parm_list[idx + 2] << 8) + | |
1335 | (parm_list[idx + 3]); | |
1336 | ||
1337 | ns->mode_select_block_len = | |
1338 | (parm_list[idx + 5] << 16) + | |
1339 | (parm_list[idx + 6] << 8) + | |
1340 | (parm_list[idx + 7]); | |
1341 | } else { | |
1342 | /* Long LBA Block Descriptor - sbc3r27 6.4.2.3 */ | |
1343 | ns->mode_select_num_blocks = | |
1344 | (((u64)parm_list[idx + 0]) << 56) + | |
1345 | (((u64)parm_list[idx + 1]) << 48) + | |
1346 | (((u64)parm_list[idx + 2]) << 40) + | |
1347 | (((u64)parm_list[idx + 3]) << 32) + | |
1348 | (((u64)parm_list[idx + 4]) << 24) + | |
1349 | (((u64)parm_list[idx + 5]) << 16) + | |
1350 | (((u64)parm_list[idx + 6]) << 8) + | |
1351 | ((u64)parm_list[idx + 7]); | |
1352 | ||
1353 | ns->mode_select_block_len = | |
1354 | (parm_list[idx + 12] << 24) + | |
1355 | (parm_list[idx + 13] << 16) + | |
1356 | (parm_list[idx + 14] << 8) + | |
1357 | (parm_list[idx + 15]); | |
1358 | } | |
1359 | } | |
1360 | ||
710a143d | 1361 | static int nvme_trans_modesel_get_mp(struct nvme_ns *ns, struct sg_io_hdr *hdr, |
5d0f6131 VV |
1362 | u8 *mode_page, u8 page_code) |
1363 | { | |
e61b0a86 | 1364 | int res = 0; |
5d0f6131 VV |
1365 | int nvme_sc; |
1366 | struct nvme_dev *dev = ns->dev; | |
1367 | unsigned dword11; | |
1368 | ||
1369 | switch (page_code) { | |
1370 | case MODE_PAGE_CACHING: | |
1371 | dword11 = ((mode_page[2] & CACHING_MODE_PAGE_WCE_MASK) ? 1 : 0); | |
1372 | nvme_sc = nvme_set_features(dev, NVME_FEAT_VOLATILE_WC, dword11, | |
1373 | 0, NULL); | |
1374 | res = nvme_trans_status_code(hdr, nvme_sc); | |
5d0f6131 VV |
1375 | break; |
1376 | case MODE_PAGE_CONTROL: | |
1377 | break; | |
1378 | case MODE_PAGE_POWER_CONDITION: | |
1379 | /* Verify the OS is not trying to set timers */ | |
1380 | if ((mode_page[2] & 0x01) != 0 || (mode_page[3] & 0x0F) != 0) { | |
1381 | res = nvme_trans_completion(hdr, | |
1382 | SAM_STAT_CHECK_CONDITION, | |
1383 | ILLEGAL_REQUEST, | |
1384 | SCSI_ASC_INVALID_PARAMETER, | |
1385 | SCSI_ASCQ_CAUSE_NOT_REPORTABLE); | |
5d0f6131 VV |
1386 | break; |
1387 | } | |
1388 | break; | |
1389 | default: | |
1390 | res = nvme_trans_completion(hdr, SAM_STAT_CHECK_CONDITION, | |
1391 | ILLEGAL_REQUEST, SCSI_ASC_INVALID_CDB, | |
1392 | SCSI_ASCQ_CAUSE_NOT_REPORTABLE); | |
5d0f6131 VV |
1393 | break; |
1394 | } | |
1395 | ||
1396 | return res; | |
1397 | } | |
1398 | ||
1399 | static int nvme_trans_modesel_data(struct nvme_ns *ns, struct sg_io_hdr *hdr, | |
1400 | u8 *cmd, u16 parm_list_len, u8 pf, | |
1401 | u8 sp, u8 cdb10) | |
1402 | { | |
e61b0a86 | 1403 | int res; |
5d0f6131 VV |
1404 | u8 *parm_list; |
1405 | u16 bd_len; | |
1406 | u8 llbaa = 0; | |
1407 | u16 index, saved_index; | |
1408 | u8 page_code; | |
1409 | u16 mp_size; | |
1410 | ||
1411 | /* Get parm list from data-in/out buffer */ | |
1412 | parm_list = kmalloc(parm_list_len, GFP_KERNEL); | |
1413 | if (parm_list == NULL) { | |
1414 | res = -ENOMEM; | |
1415 | goto out; | |
1416 | } | |
1417 | ||
1418 | res = nvme_trans_copy_from_user(hdr, parm_list, parm_list_len); | |
e61b0a86 | 1419 | if (res) |
5d0f6131 VV |
1420 | goto out_mem; |
1421 | ||
1422 | nvme_trans_modesel_get_bd_len(parm_list, cdb10, &bd_len, &llbaa); | |
1423 | index = (cdb10) ? (MODE_SELECT_10_MPH_SIZE) : (MODE_SELECT_6_MPH_SIZE); | |
1424 | ||
1425 | if (bd_len != 0) { | |
1426 | /* Block Descriptors present, parse */ | |
1427 | nvme_trans_modesel_save_bd(ns, parm_list, index, bd_len, llbaa); | |
1428 | index += bd_len; | |
1429 | } | |
1430 | saved_index = index; | |
1431 | ||
1432 | /* Multiple mode pages may be present; iterate through all */ | |
1433 | /* In 1st Iteration, don't do NVME Command, only check for CDB errors */ | |
1434 | do { | |
1435 | page_code = parm_list[index] & MODE_SELECT_PAGE_CODE_MASK; | |
1436 | mp_size = parm_list[index + 1] + 2; | |
1437 | if ((page_code != MODE_PAGE_CACHING) && | |
1438 | (page_code != MODE_PAGE_CONTROL) && | |
1439 | (page_code != MODE_PAGE_POWER_CONDITION)) { | |
1440 | res = nvme_trans_completion(hdr, | |
1441 | SAM_STAT_CHECK_CONDITION, | |
1442 | ILLEGAL_REQUEST, | |
1443 | SCSI_ASC_INVALID_CDB, | |
1444 | SCSI_ASCQ_CAUSE_NOT_REPORTABLE); | |
1445 | goto out_mem; | |
1446 | } | |
1447 | index += mp_size; | |
1448 | } while (index < parm_list_len); | |
1449 | ||
1450 | /* In 2nd Iteration, do the NVME Commands */ | |
1451 | index = saved_index; | |
1452 | do { | |
1453 | page_code = parm_list[index] & MODE_SELECT_PAGE_CODE_MASK; | |
1454 | mp_size = parm_list[index + 1] + 2; | |
1455 | res = nvme_trans_modesel_get_mp(ns, hdr, &parm_list[index], | |
1456 | page_code); | |
e61b0a86 | 1457 | if (res) |
5d0f6131 VV |
1458 | break; |
1459 | index += mp_size; | |
1460 | } while (index < parm_list_len); | |
1461 | ||
1462 | out_mem: | |
1463 | kfree(parm_list); | |
1464 | out: | |
1465 | return res; | |
1466 | } | |
1467 | ||
1468 | /* Format Unit Helper Functions */ | |
1469 | ||
1470 | static int nvme_trans_fmt_set_blk_size_count(struct nvme_ns *ns, | |
1471 | struct sg_io_hdr *hdr) | |
1472 | { | |
e61b0a86 | 1473 | int res = 0; |
5d0f6131 VV |
1474 | int nvme_sc; |
1475 | struct nvme_dev *dev = ns->dev; | |
5d0f6131 VV |
1476 | u8 flbas; |
1477 | ||
1478 | /* | |
1479 | * SCSI Expects a MODE SELECT would have been issued prior to | |
1480 | * a FORMAT UNIT, and the block size and number would be used | |
1481 | * from the block descriptor in it. If a MODE SELECT had not | |
1482 | * been issued, FORMAT shall use the current values for both. | |
1483 | */ | |
1484 | ||
1485 | if (ns->mode_select_num_blocks == 0 || ns->mode_select_block_len == 0) { | |
d29ec824 CH |
1486 | struct nvme_id_ns *id_ns; |
1487 | ||
1488 | nvme_sc = nvme_identify_ns(dev, ns->ns_id, &id_ns); | |
5d0f6131 VV |
1489 | res = nvme_trans_status_code(hdr, nvme_sc); |
1490 | if (res) | |
d29ec824 | 1491 | return res; |
5d0f6131 VV |
1492 | |
1493 | if (ns->mode_select_num_blocks == 0) | |
8741ee4c | 1494 | ns->mode_select_num_blocks = le64_to_cpu(id_ns->ncap); |
5d0f6131 VV |
1495 | if (ns->mode_select_block_len == 0) { |
1496 | flbas = (id_ns->flbas) & 0x0F; | |
1497 | ns->mode_select_block_len = | |
1498 | (1 << (id_ns->lbaf[flbas].ds)); | |
1499 | } | |
d29ec824 CH |
1500 | |
1501 | kfree(id_ns); | |
5d0f6131 | 1502 | } |
d29ec824 CH |
1503 | |
1504 | return 0; | |
5d0f6131 VV |
1505 | } |
1506 | ||
1507 | static int nvme_trans_fmt_get_parm_header(struct sg_io_hdr *hdr, u8 len, | |
1508 | u8 format_prot_info, u8 *nvme_pf_code) | |
1509 | { | |
e61b0a86 | 1510 | int res; |
5d0f6131 VV |
1511 | u8 *parm_list; |
1512 | u8 pf_usage, pf_code; | |
1513 | ||
1514 | parm_list = kmalloc(len, GFP_KERNEL); | |
1515 | if (parm_list == NULL) { | |
1516 | res = -ENOMEM; | |
1517 | goto out; | |
1518 | } | |
1519 | res = nvme_trans_copy_from_user(hdr, parm_list, len); | |
e61b0a86 | 1520 | if (res) |
5d0f6131 VV |
1521 | goto out_mem; |
1522 | ||
1523 | if ((parm_list[FORMAT_UNIT_IMMED_OFFSET] & | |
1524 | FORMAT_UNIT_IMMED_MASK) != 0) { | |
1525 | res = nvme_trans_completion(hdr, SAM_STAT_CHECK_CONDITION, | |
1526 | ILLEGAL_REQUEST, SCSI_ASC_INVALID_CDB, | |
1527 | SCSI_ASCQ_CAUSE_NOT_REPORTABLE); | |
1528 | goto out_mem; | |
1529 | } | |
1530 | ||
1531 | if (len == FORMAT_UNIT_LONG_PARM_LIST_LEN && | |
1532 | (parm_list[FORMAT_UNIT_PROT_INT_OFFSET] & 0x0F) != 0) { | |
1533 | res = nvme_trans_completion(hdr, SAM_STAT_CHECK_CONDITION, | |
1534 | ILLEGAL_REQUEST, SCSI_ASC_INVALID_CDB, | |
1535 | SCSI_ASCQ_CAUSE_NOT_REPORTABLE); | |
1536 | goto out_mem; | |
1537 | } | |
1538 | pf_usage = parm_list[FORMAT_UNIT_PROT_FIELD_USAGE_OFFSET] & | |
1539 | FORMAT_UNIT_PROT_FIELD_USAGE_MASK; | |
1540 | pf_code = (pf_usage << 2) | format_prot_info; | |
1541 | switch (pf_code) { | |
1542 | case 0: | |
1543 | *nvme_pf_code = 0; | |
1544 | break; | |
1545 | case 2: | |
1546 | *nvme_pf_code = 1; | |
1547 | break; | |
1548 | case 3: | |
1549 | *nvme_pf_code = 2; | |
1550 | break; | |
1551 | case 7: | |
1552 | *nvme_pf_code = 3; | |
1553 | break; | |
1554 | default: | |
1555 | res = nvme_trans_completion(hdr, SAM_STAT_CHECK_CONDITION, | |
1556 | ILLEGAL_REQUEST, SCSI_ASC_INVALID_CDB, | |
1557 | SCSI_ASCQ_CAUSE_NOT_REPORTABLE); | |
1558 | break; | |
1559 | } | |
1560 | ||
1561 | out_mem: | |
1562 | kfree(parm_list); | |
1563 | out: | |
1564 | return res; | |
1565 | } | |
1566 | ||
1567 | static int nvme_trans_fmt_send_cmd(struct nvme_ns *ns, struct sg_io_hdr *hdr, | |
1568 | u8 prot_info) | |
1569 | { | |
e61b0a86 | 1570 | int res; |
5d0f6131 VV |
1571 | int nvme_sc; |
1572 | struct nvme_dev *dev = ns->dev; | |
5d0f6131 VV |
1573 | struct nvme_id_ns *id_ns; |
1574 | u8 i; | |
1575 | u8 flbas, nlbaf; | |
1576 | u8 selected_lbaf = 0xFF; | |
1577 | u32 cdw10 = 0; | |
1578 | struct nvme_command c; | |
1579 | ||
1580 | /* Loop thru LBAF's in id_ns to match reqd lbaf, put in cdw10 */ | |
d29ec824 | 1581 | nvme_sc = nvme_identify_ns(dev, ns->ns_id, &id_ns); |
5d0f6131 VV |
1582 | res = nvme_trans_status_code(hdr, nvme_sc); |
1583 | if (res) | |
d29ec824 | 1584 | return res; |
e61b0a86 | 1585 | |
5d0f6131 VV |
1586 | flbas = (id_ns->flbas) & 0x0F; |
1587 | nlbaf = id_ns->nlbaf; | |
1588 | ||
1589 | for (i = 0; i < nlbaf; i++) { | |
1590 | if (ns->mode_select_block_len == (1 << (id_ns->lbaf[i].ds))) { | |
1591 | selected_lbaf = i; | |
1592 | break; | |
1593 | } | |
1594 | } | |
1595 | if (selected_lbaf > 0x0F) { | |
1596 | res = nvme_trans_completion(hdr, SAM_STAT_CHECK_CONDITION, | |
1597 | ILLEGAL_REQUEST, SCSI_ASC_INVALID_PARAMETER, | |
1598 | SCSI_ASCQ_CAUSE_NOT_REPORTABLE); | |
1599 | } | |
8741ee4c | 1600 | if (ns->mode_select_num_blocks != le64_to_cpu(id_ns->ncap)) { |
5d0f6131 VV |
1601 | res = nvme_trans_completion(hdr, SAM_STAT_CHECK_CONDITION, |
1602 | ILLEGAL_REQUEST, SCSI_ASC_INVALID_PARAMETER, | |
1603 | SCSI_ASCQ_CAUSE_NOT_REPORTABLE); | |
1604 | } | |
1605 | ||
1606 | cdw10 |= prot_info << 5; | |
1607 | cdw10 |= selected_lbaf & 0x0F; | |
1608 | memset(&c, 0, sizeof(c)); | |
1609 | c.format.opcode = nvme_admin_format_nvm; | |
8741ee4c | 1610 | c.format.nsid = cpu_to_le32(ns->ns_id); |
5d0f6131 VV |
1611 | c.format.cdw10 = cpu_to_le32(cdw10); |
1612 | ||
d29ec824 | 1613 | nvme_sc = nvme_submit_sync_cmd(dev->admin_q, &c, NULL, 0); |
5d0f6131 | 1614 | res = nvme_trans_status_code(hdr, nvme_sc); |
5d0f6131 | 1615 | |
d29ec824 | 1616 | kfree(id_ns); |
5d0f6131 VV |
1617 | return res; |
1618 | } | |
1619 | ||
5d0f6131 VV |
1620 | static inline u32 nvme_trans_io_get_num_cmds(struct sg_io_hdr *hdr, |
1621 | struct nvme_trans_io_cdb *cdb_info, | |
1622 | u32 max_blocks) | |
1623 | { | |
1624 | /* If using iovecs, send one nvme command per vector */ | |
1625 | if (hdr->iovec_count > 0) | |
1626 | return hdr->iovec_count; | |
1627 | else if (cdb_info->xfer_len > max_blocks) | |
1628 | return ((cdb_info->xfer_len - 1) / max_blocks) + 1; | |
1629 | else | |
1630 | return 1; | |
1631 | } | |
1632 | ||
1633 | static u16 nvme_trans_io_get_control(struct nvme_ns *ns, | |
1634 | struct nvme_trans_io_cdb *cdb_info) | |
1635 | { | |
1636 | u16 control = 0; | |
1637 | ||
1638 | /* When Protection information support is added, implement here */ | |
1639 | ||
1640 | if (cdb_info->fua > 0) | |
1641 | control |= NVME_RW_FUA; | |
1642 | ||
1643 | return control; | |
1644 | } | |
1645 | ||
1646 | static int nvme_trans_do_nvme_io(struct nvme_ns *ns, struct sg_io_hdr *hdr, | |
1647 | struct nvme_trans_io_cdb *cdb_info, u8 is_write) | |
1648 | { | |
e61b0a86 | 1649 | int nvme_sc = NVME_SC_SUCCESS; |
5d0f6131 | 1650 | u32 num_cmds; |
5d0f6131 VV |
1651 | u64 unit_len; |
1652 | u64 unit_num_blocks; /* Number of blocks to xfer in each nvme cmd */ | |
1653 | u32 retcode; | |
1654 | u32 i = 0; | |
1655 | u64 nvme_offset = 0; | |
8741ee4c | 1656 | void __user *next_mapping_addr; |
5d0f6131 VV |
1657 | struct nvme_command c; |
1658 | u8 opcode = (is_write ? nvme_cmd_write : nvme_cmd_read); | |
1659 | u16 control; | |
ddcb7762 | 1660 | u32 max_blocks = queue_max_hw_sectors(ns->queue); |
5d0f6131 VV |
1661 | |
1662 | num_cmds = nvme_trans_io_get_num_cmds(hdr, cdb_info, max_blocks); | |
1663 | ||
1664 | /* | |
1665 | * This loop handles two cases. | |
1666 | * First, when an SGL is used in the form of an iovec list: | |
1667 | * - Use iov_base as the next mapping address for the nvme command_id | |
1668 | * - Use iov_len as the data transfer length for the command. | |
1669 | * Second, when we have a single buffer | |
1670 | * - If larger than max_blocks, split into chunks, offset | |
1671 | * each nvme command accordingly. | |
1672 | */ | |
1673 | for (i = 0; i < num_cmds; i++) { | |
1674 | memset(&c, 0, sizeof(c)); | |
1675 | if (hdr->iovec_count > 0) { | |
8741ee4c VV |
1676 | struct sg_iovec sgl; |
1677 | ||
1678 | retcode = copy_from_user(&sgl, hdr->dxferp + | |
1679 | i * sizeof(struct sg_iovec), | |
1680 | sizeof(struct sg_iovec)); | |
1681 | if (retcode) | |
1682 | return -EFAULT; | |
1683 | unit_len = sgl.iov_len; | |
5d0f6131 | 1684 | unit_num_blocks = unit_len >> ns->lba_shift; |
8741ee4c | 1685 | next_mapping_addr = sgl.iov_base; |
5d0f6131 VV |
1686 | } else { |
1687 | unit_num_blocks = min((u64)max_blocks, | |
1688 | (cdb_info->xfer_len - nvme_offset)); | |
1689 | unit_len = unit_num_blocks << ns->lba_shift; | |
1690 | next_mapping_addr = hdr->dxferp + | |
1691 | ((1 << ns->lba_shift) * nvme_offset); | |
1692 | } | |
1693 | ||
1694 | c.rw.opcode = opcode; | |
1695 | c.rw.nsid = cpu_to_le32(ns->ns_id); | |
1696 | c.rw.slba = cpu_to_le64(cdb_info->lba + nvme_offset); | |
1697 | c.rw.length = cpu_to_le16(unit_num_blocks - 1); | |
1698 | control = nvme_trans_io_get_control(ns, cdb_info); | |
1699 | c.rw.control = cpu_to_le16(control); | |
1700 | ||
d29ec824 CH |
1701 | if (get_capacity(ns->disk) - unit_num_blocks < |
1702 | cdb_info->lba + nvme_offset) { | |
1703 | nvme_sc = NVME_SC_LBA_RANGE; | |
1704 | break; | |
5d0f6131 | 1705 | } |
d29ec824 CH |
1706 | nvme_sc = __nvme_submit_sync_cmd(ns->queue, &c, NULL, |
1707 | next_mapping_addr, unit_len, NULL, 0); | |
1708 | if (nvme_sc) | |
1709 | break; | |
5d0f6131 VV |
1710 | |
1711 | nvme_offset += unit_num_blocks; | |
5d0f6131 | 1712 | } |
5d0f6131 | 1713 | |
e61b0a86 | 1714 | return nvme_trans_status_code(hdr, nvme_sc); |
5d0f6131 VV |
1715 | } |
1716 | ||
1717 | ||
1718 | /* SCSI Command Translation Functions */ | |
1719 | ||
1720 | static int nvme_trans_io(struct nvme_ns *ns, struct sg_io_hdr *hdr, u8 is_write, | |
1721 | u8 *cmd) | |
1722 | { | |
e61b0a86 | 1723 | int res = 0; |
cbbb7a2e | 1724 | struct nvme_trans_io_cdb cdb_info = { 0, }; |
5d0f6131 VV |
1725 | u8 opcode = cmd[0]; |
1726 | u64 xfer_bytes; | |
1727 | u64 sum_iov_len = 0; | |
8741ee4c | 1728 | struct sg_iovec sgl; |
5d0f6131 | 1729 | int i; |
8741ee4c | 1730 | size_t not_copied; |
5d0f6131 | 1731 | |
cbbb7a2e CH |
1732 | /* |
1733 | * The FUA and WPROTECT fields are not supported in 6-byte CDBs, | |
1734 | * but always in the same place for all others. | |
1735 | */ | |
1736 | switch (opcode) { | |
1737 | case WRITE_6: | |
1738 | case READ_6: | |
1739 | break; | |
1740 | default: | |
1741 | cdb_info.fua = cmd[1] & 0x8; | |
1742 | cdb_info.prot_info = (cmd[1] & 0xe0) >> 5; | |
772ce435 CH |
1743 | if (cdb_info.prot_info && !ns->pi_type) { |
1744 | return nvme_trans_completion(hdr, | |
1745 | SAM_STAT_CHECK_CONDITION, | |
1746 | ILLEGAL_REQUEST, | |
1747 | SCSI_ASC_INVALID_CDB, | |
1748 | SCSI_ASCQ_CAUSE_NOT_REPORTABLE); | |
1749 | } | |
cbbb7a2e CH |
1750 | } |
1751 | ||
5d0f6131 VV |
1752 | switch (opcode) { |
1753 | case WRITE_6: | |
1754 | case READ_6: | |
cbbb7a2e CH |
1755 | cdb_info.lba = get_unaligned_be24(&cmd[1]); |
1756 | cdb_info.xfer_len = cmd[4]; | |
1757 | if (cdb_info.xfer_len == 0) | |
1758 | cdb_info.xfer_len = 256; | |
5d0f6131 VV |
1759 | break; |
1760 | case WRITE_10: | |
1761 | case READ_10: | |
cbbb7a2e CH |
1762 | cdb_info.lba = get_unaligned_be32(&cmd[2]); |
1763 | cdb_info.xfer_len = get_unaligned_be16(&cmd[7]); | |
5d0f6131 VV |
1764 | break; |
1765 | case WRITE_12: | |
1766 | case READ_12: | |
cbbb7a2e CH |
1767 | cdb_info.lba = get_unaligned_be32(&cmd[2]); |
1768 | cdb_info.xfer_len = get_unaligned_be32(&cmd[6]); | |
5d0f6131 VV |
1769 | break; |
1770 | case WRITE_16: | |
1771 | case READ_16: | |
cbbb7a2e CH |
1772 | cdb_info.lba = get_unaligned_be64(&cmd[2]); |
1773 | cdb_info.xfer_len = get_unaligned_be32(&cmd[10]); | |
5d0f6131 VV |
1774 | break; |
1775 | default: | |
1776 | /* Will never really reach here */ | |
e61b0a86 | 1777 | res = -EIO; |
5d0f6131 VV |
1778 | goto out; |
1779 | } | |
1780 | ||
1781 | /* Calculate total length of transfer (in bytes) */ | |
1782 | if (hdr->iovec_count > 0) { | |
5d0f6131 | 1783 | for (i = 0; i < hdr->iovec_count; i++) { |
8741ee4c VV |
1784 | not_copied = copy_from_user(&sgl, hdr->dxferp + |
1785 | i * sizeof(struct sg_iovec), | |
1786 | sizeof(struct sg_iovec)); | |
1787 | if (not_copied) | |
1788 | return -EFAULT; | |
1789 | sum_iov_len += sgl.iov_len; | |
5d0f6131 | 1790 | /* IO vector sizes should be multiples of block size */ |
8741ee4c | 1791 | if (sgl.iov_len % (1 << ns->lba_shift) != 0) { |
5d0f6131 VV |
1792 | res = nvme_trans_completion(hdr, |
1793 | SAM_STAT_CHECK_CONDITION, | |
1794 | ILLEGAL_REQUEST, | |
1795 | SCSI_ASC_INVALID_PARAMETER, | |
1796 | SCSI_ASCQ_CAUSE_NOT_REPORTABLE); | |
1797 | goto out; | |
1798 | } | |
1799 | } | |
1800 | } else { | |
1801 | sum_iov_len = hdr->dxfer_len; | |
1802 | } | |
1803 | ||
1804 | /* As Per sg ioctl howto, if the lengths differ, use the lower one */ | |
1805 | xfer_bytes = min(((u64)hdr->dxfer_len), sum_iov_len); | |
1806 | ||
1807 | /* If block count and actual data buffer size dont match, error out */ | |
1808 | if (xfer_bytes != (cdb_info.xfer_len << ns->lba_shift)) { | |
1809 | res = -EINVAL; | |
1810 | goto out; | |
1811 | } | |
1812 | ||
1813 | /* Check for 0 length transfer - it is not illegal */ | |
1814 | if (cdb_info.xfer_len == 0) | |
1815 | goto out; | |
1816 | ||
1817 | /* Send NVMe IO Command(s) */ | |
1818 | res = nvme_trans_do_nvme_io(ns, hdr, &cdb_info, is_write); | |
e61b0a86 | 1819 | if (res) |
5d0f6131 VV |
1820 | goto out; |
1821 | ||
1822 | out: | |
1823 | return res; | |
1824 | } | |
1825 | ||
1826 | static int nvme_trans_inquiry(struct nvme_ns *ns, struct sg_io_hdr *hdr, | |
1827 | u8 *cmd) | |
1828 | { | |
e61b0a86 | 1829 | int res = 0; |
5d0f6131 VV |
1830 | u8 evpd; |
1831 | u8 page_code; | |
1832 | int alloc_len; | |
1833 | u8 *inq_response; | |
1834 | ||
3726897e CH |
1835 | evpd = cmd[1] & 0x01; |
1836 | page_code = cmd[2]; | |
1837 | alloc_len = get_unaligned_be16(&cmd[3]); | |
5d0f6131 | 1838 | |
4f1982b4 | 1839 | inq_response = kmalloc(alloc_len, GFP_KERNEL); |
5d0f6131 VV |
1840 | if (inq_response == NULL) { |
1841 | res = -ENOMEM; | |
1842 | goto out_mem; | |
1843 | } | |
1844 | ||
1845 | if (evpd == 0) { | |
1846 | if (page_code == INQ_STANDARD_INQUIRY_PAGE) { | |
1847 | res = nvme_trans_standard_inquiry_page(ns, hdr, | |
1848 | inq_response, alloc_len); | |
1849 | } else { | |
1850 | res = nvme_trans_completion(hdr, | |
1851 | SAM_STAT_CHECK_CONDITION, | |
1852 | ILLEGAL_REQUEST, | |
1853 | SCSI_ASC_INVALID_CDB, | |
1854 | SCSI_ASCQ_CAUSE_NOT_REPORTABLE); | |
1855 | } | |
1856 | } else { | |
1857 | switch (page_code) { | |
1858 | case VPD_SUPPORTED_PAGES: | |
1859 | res = nvme_trans_supported_vpd_pages(ns, hdr, | |
1860 | inq_response, alloc_len); | |
1861 | break; | |
1862 | case VPD_SERIAL_NUMBER: | |
1863 | res = nvme_trans_unit_serial_page(ns, hdr, inq_response, | |
1864 | alloc_len); | |
1865 | break; | |
1866 | case VPD_DEVICE_IDENTIFIERS: | |
1867 | res = nvme_trans_device_id_page(ns, hdr, inq_response, | |
1868 | alloc_len); | |
1869 | break; | |
1870 | case VPD_EXTENDED_INQUIRY: | |
1871 | res = nvme_trans_ext_inq_page(ns, hdr, alloc_len); | |
1872 | break; | |
7f749d9c KB |
1873 | case VPD_BLOCK_LIMITS: |
1874 | res = nvme_trans_bdev_limits_page(ns, hdr, inq_response, | |
1875 | alloc_len); | |
1876 | break; | |
5d0f6131 VV |
1877 | case VPD_BLOCK_DEV_CHARACTERISTICS: |
1878 | res = nvme_trans_bdev_char_page(ns, hdr, alloc_len); | |
1879 | break; | |
1880 | default: | |
1881 | res = nvme_trans_completion(hdr, | |
1882 | SAM_STAT_CHECK_CONDITION, | |
1883 | ILLEGAL_REQUEST, | |
1884 | SCSI_ASC_INVALID_CDB, | |
1885 | SCSI_ASCQ_CAUSE_NOT_REPORTABLE); | |
1886 | break; | |
1887 | } | |
1888 | } | |
1889 | kfree(inq_response); | |
1890 | out_mem: | |
1891 | return res; | |
1892 | } | |
1893 | ||
1894 | static int nvme_trans_log_sense(struct nvme_ns *ns, struct sg_io_hdr *hdr, | |
1895 | u8 *cmd) | |
1896 | { | |
e61b0a86 | 1897 | int res; |
5d0f6131 | 1898 | u16 alloc_len; |
5d0f6131 VV |
1899 | u8 pc; |
1900 | u8 page_code; | |
1901 | ||
3726897e | 1902 | if (cmd[1] != LOG_SENSE_CDB_SP_NOT_ENABLED) { |
5d0f6131 VV |
1903 | res = nvme_trans_completion(hdr, SAM_STAT_CHECK_CONDITION, |
1904 | ILLEGAL_REQUEST, SCSI_ASC_INVALID_CDB, | |
1905 | SCSI_ASCQ_CAUSE_NOT_REPORTABLE); | |
1906 | goto out; | |
1907 | } | |
3726897e CH |
1908 | |
1909 | page_code = cmd[2] & LOG_SENSE_CDB_PAGE_CODE_MASK; | |
1910 | pc = (cmd[2] & LOG_SENSE_CDB_PC_MASK) >> LOG_SENSE_CDB_PC_SHIFT; | |
5d0f6131 VV |
1911 | if (pc != LOG_SENSE_CDB_PC_CUMULATIVE_VALUES) { |
1912 | res = nvme_trans_completion(hdr, SAM_STAT_CHECK_CONDITION, | |
1913 | ILLEGAL_REQUEST, SCSI_ASC_INVALID_CDB, | |
1914 | SCSI_ASCQ_CAUSE_NOT_REPORTABLE); | |
1915 | goto out; | |
1916 | } | |
3726897e | 1917 | alloc_len = get_unaligned_be16(&cmd[7]); |
5d0f6131 VV |
1918 | switch (page_code) { |
1919 | case LOG_PAGE_SUPPORTED_LOG_PAGES_PAGE: | |
1920 | res = nvme_trans_log_supp_pages(ns, hdr, alloc_len); | |
1921 | break; | |
1922 | case LOG_PAGE_INFORMATIONAL_EXCEPTIONS_PAGE: | |
1923 | res = nvme_trans_log_info_exceptions(ns, hdr, alloc_len); | |
1924 | break; | |
1925 | case LOG_PAGE_TEMPERATURE_PAGE: | |
1926 | res = nvme_trans_log_temperature(ns, hdr, alloc_len); | |
1927 | break; | |
1928 | default: | |
1929 | res = nvme_trans_completion(hdr, SAM_STAT_CHECK_CONDITION, | |
1930 | ILLEGAL_REQUEST, SCSI_ASC_INVALID_CDB, | |
1931 | SCSI_ASCQ_CAUSE_NOT_REPORTABLE); | |
1932 | break; | |
1933 | } | |
1934 | ||
1935 | out: | |
1936 | return res; | |
1937 | } | |
1938 | ||
1939 | static int nvme_trans_mode_select(struct nvme_ns *ns, struct sg_io_hdr *hdr, | |
1940 | u8 *cmd) | |
1941 | { | |
5d0f6131 VV |
1942 | u8 cdb10 = 0; |
1943 | u16 parm_list_len; | |
1944 | u8 page_format; | |
1945 | u8 save_pages; | |
1946 | ||
3726897e CH |
1947 | page_format = cmd[1] & MODE_SELECT_CDB_PAGE_FORMAT_MASK; |
1948 | save_pages = cmd[1] & MODE_SELECT_CDB_SAVE_PAGES_MASK; | |
5d0f6131 | 1949 | |
3726897e CH |
1950 | if (cmd[0] == MODE_SELECT) { |
1951 | parm_list_len = cmd[4]; | |
5d0f6131 | 1952 | } else { |
3726897e | 1953 | parm_list_len = cmd[7]; |
5d0f6131 VV |
1954 | cdb10 = 1; |
1955 | } | |
1956 | ||
1957 | if (parm_list_len != 0) { | |
1958 | /* | |
1959 | * According to SPC-4 r24, a paramter list length field of 0 | |
1960 | * shall not be considered an error | |
1961 | */ | |
e61b0a86 | 1962 | return nvme_trans_modesel_data(ns, hdr, cmd, parm_list_len, |
5d0f6131 VV |
1963 | page_format, save_pages, cdb10); |
1964 | } | |
1965 | ||
e61b0a86 | 1966 | return 0; |
5d0f6131 VV |
1967 | } |
1968 | ||
1969 | static int nvme_trans_mode_sense(struct nvme_ns *ns, struct sg_io_hdr *hdr, | |
1970 | u8 *cmd) | |
1971 | { | |
e61b0a86 | 1972 | int res = 0; |
5d0f6131 VV |
1973 | u16 alloc_len; |
1974 | u8 cdb10 = 0; | |
5d0f6131 | 1975 | |
3726897e CH |
1976 | if (cmd[0] == MODE_SENSE) { |
1977 | alloc_len = cmd[4]; | |
5d0f6131 | 1978 | } else { |
3726897e | 1979 | alloc_len = get_unaligned_be16(&cmd[7]); |
5d0f6131 VV |
1980 | cdb10 = 1; |
1981 | } | |
1982 | ||
3726897e CH |
1983 | if ((cmd[2] & MODE_SENSE_PAGE_CONTROL_MASK) != |
1984 | MODE_SENSE_PC_CURRENT_VALUES) { | |
5d0f6131 VV |
1985 | res = nvme_trans_completion(hdr, SAM_STAT_CHECK_CONDITION, |
1986 | ILLEGAL_REQUEST, SCSI_ASC_INVALID_CDB, | |
1987 | SCSI_ASCQ_CAUSE_NOT_REPORTABLE); | |
1988 | goto out; | |
1989 | } | |
1990 | ||
3726897e | 1991 | switch (cmd[2] & MODE_SENSE_PAGE_CODE_MASK) { |
5d0f6131 VV |
1992 | case MODE_PAGE_CACHING: |
1993 | res = nvme_trans_mode_page_create(ns, hdr, cmd, alloc_len, | |
1994 | cdb10, | |
1995 | &nvme_trans_fill_caching_page, | |
1996 | MODE_PAGE_CACHING_LEN); | |
1997 | break; | |
1998 | case MODE_PAGE_CONTROL: | |
1999 | res = nvme_trans_mode_page_create(ns, hdr, cmd, alloc_len, | |
2000 | cdb10, | |
2001 | &nvme_trans_fill_control_page, | |
2002 | MODE_PAGE_CONTROL_LEN); | |
2003 | break; | |
2004 | case MODE_PAGE_POWER_CONDITION: | |
2005 | res = nvme_trans_mode_page_create(ns, hdr, cmd, alloc_len, | |
2006 | cdb10, | |
2007 | &nvme_trans_fill_pow_cnd_page, | |
2008 | MODE_PAGE_POW_CND_LEN); | |
2009 | break; | |
2010 | case MODE_PAGE_INFO_EXCEP: | |
2011 | res = nvme_trans_mode_page_create(ns, hdr, cmd, alloc_len, | |
2012 | cdb10, | |
2013 | &nvme_trans_fill_inf_exc_page, | |
2014 | MODE_PAGE_INF_EXC_LEN); | |
2015 | break; | |
2016 | case MODE_PAGE_RETURN_ALL: | |
2017 | res = nvme_trans_mode_page_create(ns, hdr, cmd, alloc_len, | |
2018 | cdb10, | |
2019 | &nvme_trans_fill_all_pages, | |
2020 | MODE_PAGE_ALL_LEN); | |
2021 | break; | |
2022 | default: | |
2023 | res = nvme_trans_completion(hdr, SAM_STAT_CHECK_CONDITION, | |
2024 | ILLEGAL_REQUEST, SCSI_ASC_INVALID_CDB, | |
2025 | SCSI_ASCQ_CAUSE_NOT_REPORTABLE); | |
2026 | break; | |
2027 | } | |
2028 | ||
2029 | out: | |
2030 | return res; | |
2031 | } | |
2032 | ||
2033 | static int nvme_trans_read_capacity(struct nvme_ns *ns, struct sg_io_hdr *hdr, | |
3726897e | 2034 | u8 *cmd, u8 cdb16) |
5d0f6131 | 2035 | { |
e61b0a86 | 2036 | int res; |
5d0f6131 | 2037 | int nvme_sc; |
3726897e CH |
2038 | u32 alloc_len; |
2039 | u32 resp_size; | |
5d0f6131 | 2040 | u32 xfer_len; |
5d0f6131 | 2041 | struct nvme_dev *dev = ns->dev; |
5d0f6131 VV |
2042 | struct nvme_id_ns *id_ns; |
2043 | u8 *response; | |
2044 | ||
5d0f6131 | 2045 | if (cdb16) { |
3726897e | 2046 | alloc_len = get_unaligned_be32(&cmd[10]); |
5d0f6131 | 2047 | resp_size = READ_CAP_16_RESP_SIZE; |
3726897e CH |
2048 | } else { |
2049 | alloc_len = READ_CAP_10_RESP_SIZE; | |
2050 | resp_size = READ_CAP_10_RESP_SIZE; | |
5d0f6131 VV |
2051 | } |
2052 | ||
d29ec824 | 2053 | nvme_sc = nvme_identify_ns(dev, ns->ns_id, &id_ns); |
5d0f6131 VV |
2054 | res = nvme_trans_status_code(hdr, nvme_sc); |
2055 | if (res) | |
d29ec824 | 2056 | return res; |
5d0f6131 | 2057 | |
03ea83e9 | 2058 | response = kzalloc(resp_size, GFP_KERNEL); |
5d0f6131 VV |
2059 | if (response == NULL) { |
2060 | res = -ENOMEM; | |
d29ec824 | 2061 | goto out_free_id; |
5d0f6131 | 2062 | } |
5d0f6131 VV |
2063 | nvme_trans_fill_read_cap(response, id_ns, cdb16); |
2064 | ||
2065 | xfer_len = min(alloc_len, resp_size); | |
2066 | res = nvme_trans_copy_to_user(hdr, response, xfer_len); | |
2067 | ||
2068 | kfree(response); | |
d29ec824 CH |
2069 | out_free_id: |
2070 | kfree(id_ns); | |
5d0f6131 VV |
2071 | return res; |
2072 | } | |
2073 | ||
2074 | static int nvme_trans_report_luns(struct nvme_ns *ns, struct sg_io_hdr *hdr, | |
2075 | u8 *cmd) | |
2076 | { | |
e61b0a86 | 2077 | int res; |
5d0f6131 VV |
2078 | int nvme_sc; |
2079 | u32 alloc_len, xfer_len, resp_size; | |
5d0f6131 VV |
2080 | u8 *response; |
2081 | struct nvme_dev *dev = ns->dev; | |
5d0f6131 VV |
2082 | struct nvme_id_ctrl *id_ctrl; |
2083 | u32 ll_length, lun_id; | |
2084 | u8 lun_id_offset = REPORT_LUNS_FIRST_LUN_OFFSET; | |
8741ee4c | 2085 | __be32 tmp_len; |
5d0f6131 | 2086 | |
3726897e CH |
2087 | switch (cmd[2]) { |
2088 | default: | |
2089 | return nvme_trans_completion(hdr, SAM_STAT_CHECK_CONDITION, | |
5d0f6131 VV |
2090 | ILLEGAL_REQUEST, SCSI_ASC_INVALID_CDB, |
2091 | SCSI_ASCQ_CAUSE_NOT_REPORTABLE); | |
3726897e CH |
2092 | case ALL_LUNS_RETURNED: |
2093 | case ALL_WELL_KNOWN_LUNS_RETURNED: | |
2094 | case RESTRICTED_LUNS_RETURNED: | |
d29ec824 | 2095 | nvme_sc = nvme_identify_ctrl(dev, &id_ctrl); |
5d0f6131 VV |
2096 | res = nvme_trans_status_code(hdr, nvme_sc); |
2097 | if (res) | |
d29ec824 | 2098 | return res; |
e61b0a86 | 2099 | |
8741ee4c | 2100 | ll_length = le32_to_cpu(id_ctrl->nn) * LUN_ENTRY_SIZE; |
5d0f6131 VV |
2101 | resp_size = ll_length + LUN_DATA_HEADER_SIZE; |
2102 | ||
3726897e | 2103 | alloc_len = get_unaligned_be32(&cmd[6]); |
5d0f6131 VV |
2104 | if (alloc_len < resp_size) { |
2105 | res = nvme_trans_completion(hdr, | |
2106 | SAM_STAT_CHECK_CONDITION, | |
2107 | ILLEGAL_REQUEST, SCSI_ASC_INVALID_CDB, | |
2108 | SCSI_ASCQ_CAUSE_NOT_REPORTABLE); | |
d29ec824 | 2109 | goto out_free_id; |
5d0f6131 VV |
2110 | } |
2111 | ||
03ea83e9 | 2112 | response = kzalloc(resp_size, GFP_KERNEL); |
5d0f6131 VV |
2113 | if (response == NULL) { |
2114 | res = -ENOMEM; | |
d29ec824 | 2115 | goto out_free_id; |
5d0f6131 | 2116 | } |
5d0f6131 VV |
2117 | |
2118 | /* The first LUN ID will always be 0 per the SAM spec */ | |
8741ee4c | 2119 | for (lun_id = 0; lun_id < le32_to_cpu(id_ctrl->nn); lun_id++) { |
5d0f6131 VV |
2120 | /* |
2121 | * Set the LUN Id and then increment to the next LUN | |
2122 | * location in the parameter data. | |
2123 | */ | |
8741ee4c | 2124 | __be64 tmp_id = cpu_to_be64(lun_id); |
5d0f6131 VV |
2125 | memcpy(&response[lun_id_offset], &tmp_id, sizeof(u64)); |
2126 | lun_id_offset += LUN_ENTRY_SIZE; | |
2127 | } | |
2128 | tmp_len = cpu_to_be32(ll_length); | |
2129 | memcpy(response, &tmp_len, sizeof(u32)); | |
2130 | } | |
2131 | ||
2132 | xfer_len = min(alloc_len, resp_size); | |
2133 | res = nvme_trans_copy_to_user(hdr, response, xfer_len); | |
2134 | ||
2135 | kfree(response); | |
d29ec824 CH |
2136 | out_free_id: |
2137 | kfree(id_ctrl); | |
5d0f6131 VV |
2138 | return res; |
2139 | } | |
2140 | ||
2141 | static int nvme_trans_request_sense(struct nvme_ns *ns, struct sg_io_hdr *hdr, | |
2142 | u8 *cmd) | |
2143 | { | |
e61b0a86 | 2144 | int res; |
5d0f6131 VV |
2145 | u8 alloc_len, xfer_len, resp_size; |
2146 | u8 desc_format; | |
2147 | u8 *response; | |
2148 | ||
3726897e CH |
2149 | desc_format = cmd[1] & 0x01; |
2150 | alloc_len = cmd[4]; | |
5d0f6131 VV |
2151 | |
2152 | resp_size = ((desc_format) ? (DESC_FMT_SENSE_DATA_SIZE) : | |
2153 | (FIXED_FMT_SENSE_DATA_SIZE)); | |
03ea83e9 | 2154 | response = kzalloc(resp_size, GFP_KERNEL); |
5d0f6131 VV |
2155 | if (response == NULL) { |
2156 | res = -ENOMEM; | |
2157 | goto out; | |
2158 | } | |
5d0f6131 | 2159 | |
3726897e | 2160 | if (desc_format) { |
5d0f6131 VV |
2161 | /* Descriptor Format Sense Data */ |
2162 | response[0] = DESC_FORMAT_SENSE_DATA; | |
2163 | response[1] = NO_SENSE; | |
2164 | /* TODO How is LOW POWER CONDITION ON handled? (byte 2) */ | |
2165 | response[2] = SCSI_ASC_NO_SENSE; | |
2166 | response[3] = SCSI_ASCQ_CAUSE_NOT_REPORTABLE; | |
2167 | /* SDAT_OVFL = 0 | Additional Sense Length = 0 */ | |
2168 | } else { | |
2169 | /* Fixed Format Sense Data */ | |
2170 | response[0] = FIXED_SENSE_DATA; | |
2171 | /* Byte 1 = Obsolete */ | |
2172 | response[2] = NO_SENSE; /* FM, EOM, ILI, SDAT_OVFL = 0 */ | |
2173 | /* Bytes 3-6 - Information - set to zero */ | |
2174 | response[7] = FIXED_SENSE_DATA_ADD_LENGTH; | |
2175 | /* Bytes 8-11 - Cmd Specific Information - set to zero */ | |
2176 | response[12] = SCSI_ASC_NO_SENSE; | |
2177 | response[13] = SCSI_ASCQ_CAUSE_NOT_REPORTABLE; | |
2178 | /* Byte 14 = Field Replaceable Unit Code = 0 */ | |
2179 | /* Bytes 15-17 - SKSV=0; Sense Key Specific = 0 */ | |
2180 | } | |
2181 | ||
2182 | xfer_len = min(alloc_len, resp_size); | |
2183 | res = nvme_trans_copy_to_user(hdr, response, xfer_len); | |
2184 | ||
2185 | kfree(response); | |
2186 | out: | |
2187 | return res; | |
2188 | } | |
2189 | ||
2190 | static int nvme_trans_security_protocol(struct nvme_ns *ns, | |
2191 | struct sg_io_hdr *hdr, | |
2192 | u8 *cmd) | |
2193 | { | |
2194 | return nvme_trans_completion(hdr, SAM_STAT_CHECK_CONDITION, | |
2195 | ILLEGAL_REQUEST, SCSI_ASC_ILLEGAL_COMMAND, | |
2196 | SCSI_ASCQ_CAUSE_NOT_REPORTABLE); | |
2197 | } | |
2198 | ||
d29ec824 CH |
2199 | static int nvme_trans_synchronize_cache(struct nvme_ns *ns, |
2200 | struct sg_io_hdr *hdr) | |
5d0f6131 | 2201 | { |
5d0f6131 | 2202 | int nvme_sc; |
14385de1 | 2203 | struct nvme_command c; |
d29ec824 CH |
2204 | |
2205 | memset(&c, 0, sizeof(c)); | |
2206 | c.common.opcode = nvme_cmd_flush; | |
2207 | c.common.nsid = cpu_to_le32(ns->ns_id); | |
2208 | ||
2209 | nvme_sc = nvme_submit_sync_cmd(ns->queue, &c, NULL, 0); | |
2210 | return nvme_trans_status_code(hdr, nvme_sc); | |
2211 | } | |
2212 | ||
2213 | static int nvme_trans_start_stop(struct nvme_ns *ns, struct sg_io_hdr *hdr, | |
2214 | u8 *cmd) | |
2215 | { | |
5d0f6131 VV |
2216 | u8 immed, pcmod, pc, no_flush, start; |
2217 | ||
3726897e CH |
2218 | immed = cmd[1] & 0x01; |
2219 | pcmod = cmd[3] & 0x0f; | |
2220 | pc = (cmd[4] & 0xf0) >> 4; | |
2221 | no_flush = cmd[4] & 0x04; | |
2222 | start = cmd[4] & 0x01; | |
5d0f6131 VV |
2223 | |
2224 | if (immed != 0) { | |
e61b0a86 | 2225 | return nvme_trans_completion(hdr, SAM_STAT_CHECK_CONDITION, |
5d0f6131 VV |
2226 | ILLEGAL_REQUEST, SCSI_ASC_INVALID_CDB, |
2227 | SCSI_ASCQ_CAUSE_NOT_REPORTABLE); | |
2228 | } else { | |
2229 | if (no_flush == 0) { | |
2230 | /* Issue NVME FLUSH command prior to START STOP UNIT */ | |
d29ec824 | 2231 | int res = nvme_trans_synchronize_cache(ns, hdr); |
5d0f6131 | 2232 | if (res) |
e61b0a86 | 2233 | return res; |
5d0f6131 VV |
2234 | } |
2235 | /* Setup the expected power state transition */ | |
e61b0a86 | 2236 | return nvme_trans_power_state(ns, hdr, pc, pcmod, start); |
5d0f6131 | 2237 | } |
5d0f6131 VV |
2238 | } |
2239 | ||
5d0f6131 VV |
2240 | static int nvme_trans_format_unit(struct nvme_ns *ns, struct sg_io_hdr *hdr, |
2241 | u8 *cmd) | |
2242 | { | |
e61b0a86 | 2243 | int res; |
5d0f6131 VV |
2244 | u8 parm_hdr_len = 0; |
2245 | u8 nvme_pf_code = 0; | |
2246 | u8 format_prot_info, long_list, format_data; | |
2247 | ||
3726897e CH |
2248 | format_prot_info = (cmd[1] & 0xc0) >> 6; |
2249 | long_list = cmd[1] & 0x20; | |
2250 | format_data = cmd[1] & 0x10; | |
5d0f6131 VV |
2251 | |
2252 | if (format_data != 0) { | |
2253 | if (format_prot_info != 0) { | |
2254 | if (long_list == 0) | |
2255 | parm_hdr_len = FORMAT_UNIT_SHORT_PARM_LIST_LEN; | |
2256 | else | |
2257 | parm_hdr_len = FORMAT_UNIT_LONG_PARM_LIST_LEN; | |
2258 | } | |
2259 | } else if (format_data == 0 && format_prot_info != 0) { | |
2260 | res = nvme_trans_completion(hdr, SAM_STAT_CHECK_CONDITION, | |
2261 | ILLEGAL_REQUEST, SCSI_ASC_INVALID_CDB, | |
2262 | SCSI_ASCQ_CAUSE_NOT_REPORTABLE); | |
2263 | goto out; | |
2264 | } | |
2265 | ||
2266 | /* Get parm header from data-in/out buffer */ | |
2267 | /* | |
2268 | * According to the translation spec, the only fields in the parameter | |
2269 | * list we are concerned with are in the header. So allocate only that. | |
2270 | */ | |
2271 | if (parm_hdr_len > 0) { | |
2272 | res = nvme_trans_fmt_get_parm_header(hdr, parm_hdr_len, | |
2273 | format_prot_info, &nvme_pf_code); | |
e61b0a86 | 2274 | if (res) |
5d0f6131 VV |
2275 | goto out; |
2276 | } | |
2277 | ||
2278 | /* Attempt to activate any previously downloaded firmware image */ | |
b90c48d0 | 2279 | res = nvme_trans_send_activate_fw_cmd(ns, hdr, 0); |
5d0f6131 VV |
2280 | |
2281 | /* Determine Block size and count and send format command */ | |
2282 | res = nvme_trans_fmt_set_blk_size_count(ns, hdr); | |
e61b0a86 | 2283 | if (res) |
5d0f6131 VV |
2284 | goto out; |
2285 | ||
2286 | res = nvme_trans_fmt_send_cmd(ns, hdr, nvme_pf_code); | |
2287 | ||
2288 | out: | |
2289 | return res; | |
2290 | } | |
2291 | ||
2292 | static int nvme_trans_test_unit_ready(struct nvme_ns *ns, | |
2293 | struct sg_io_hdr *hdr, | |
2294 | u8 *cmd) | |
2295 | { | |
5d0f6131 VV |
2296 | struct nvme_dev *dev = ns->dev; |
2297 | ||
2298 | if (!(readl(&dev->bar->csts) & NVME_CSTS_RDY)) | |
e61b0a86 | 2299 | return nvme_trans_completion(hdr, SAM_STAT_CHECK_CONDITION, |
5d0f6131 VV |
2300 | NOT_READY, SCSI_ASC_LUN_NOT_READY, |
2301 | SCSI_ASCQ_CAUSE_NOT_REPORTABLE); | |
2302 | else | |
e61b0a86 | 2303 | return nvme_trans_completion(hdr, SAM_STAT_GOOD, NO_SENSE, 0, 0); |
5d0f6131 VV |
2304 | } |
2305 | ||
2306 | static int nvme_trans_write_buffer(struct nvme_ns *ns, struct sg_io_hdr *hdr, | |
2307 | u8 *cmd) | |
2308 | { | |
e61b0a86 | 2309 | int res = 0; |
5d0f6131 VV |
2310 | u32 buffer_offset, parm_list_length; |
2311 | u8 buffer_id, mode; | |
2312 | ||
3726897e | 2313 | parm_list_length = get_unaligned_be24(&cmd[6]); |
5d0f6131 VV |
2314 | if (parm_list_length % BYTES_TO_DWORDS != 0) { |
2315 | /* NVMe expects Firmware file to be a whole number of DWORDS */ | |
2316 | res = nvme_trans_completion(hdr, SAM_STAT_CHECK_CONDITION, | |
2317 | ILLEGAL_REQUEST, SCSI_ASC_INVALID_CDB, | |
2318 | SCSI_ASCQ_CAUSE_NOT_REPORTABLE); | |
2319 | goto out; | |
2320 | } | |
3726897e | 2321 | buffer_id = cmd[2]; |
5d0f6131 VV |
2322 | if (buffer_id > NVME_MAX_FIRMWARE_SLOT) { |
2323 | res = nvme_trans_completion(hdr, SAM_STAT_CHECK_CONDITION, | |
2324 | ILLEGAL_REQUEST, SCSI_ASC_INVALID_CDB, | |
2325 | SCSI_ASCQ_CAUSE_NOT_REPORTABLE); | |
2326 | goto out; | |
2327 | } | |
3726897e CH |
2328 | mode = cmd[1] & 0x1f; |
2329 | buffer_offset = get_unaligned_be24(&cmd[3]); | |
5d0f6131 VV |
2330 | |
2331 | switch (mode) { | |
2332 | case DOWNLOAD_SAVE_ACTIVATE: | |
b90c48d0 | 2333 | res = nvme_trans_send_download_fw_cmd(ns, hdr, nvme_admin_download_fw, |
5d0f6131 VV |
2334 | parm_list_length, buffer_offset, |
2335 | buffer_id); | |
e61b0a86 | 2336 | if (res) |
5d0f6131 | 2337 | goto out; |
b90c48d0 | 2338 | res = nvme_trans_send_activate_fw_cmd(ns, hdr, buffer_id); |
5d0f6131 VV |
2339 | break; |
2340 | case DOWNLOAD_SAVE_DEFER_ACTIVATE: | |
b90c48d0 | 2341 | res = nvme_trans_send_download_fw_cmd(ns, hdr, nvme_admin_download_fw, |
5d0f6131 VV |
2342 | parm_list_length, buffer_offset, |
2343 | buffer_id); | |
2344 | break; | |
2345 | case ACTIVATE_DEFERRED_MICROCODE: | |
b90c48d0 | 2346 | res = nvme_trans_send_activate_fw_cmd(ns, hdr, buffer_id); |
5d0f6131 VV |
2347 | break; |
2348 | default: | |
2349 | res = nvme_trans_completion(hdr, SAM_STAT_CHECK_CONDITION, | |
2350 | ILLEGAL_REQUEST, SCSI_ASC_INVALID_CDB, | |
2351 | SCSI_ASCQ_CAUSE_NOT_REPORTABLE); | |
2352 | break; | |
2353 | } | |
2354 | ||
2355 | out: | |
2356 | return res; | |
2357 | } | |
2358 | ||
ec503733 KB |
2359 | struct scsi_unmap_blk_desc { |
2360 | __be64 slba; | |
2361 | __be32 nlb; | |
2362 | u32 resv; | |
2363 | }; | |
2364 | ||
2365 | struct scsi_unmap_parm_list { | |
2366 | __be16 unmap_data_len; | |
2367 | __be16 unmap_blk_desc_data_len; | |
2368 | u32 resv; | |
2369 | struct scsi_unmap_blk_desc desc[0]; | |
2370 | }; | |
2371 | ||
2372 | static int nvme_trans_unmap(struct nvme_ns *ns, struct sg_io_hdr *hdr, | |
2373 | u8 *cmd) | |
2374 | { | |
ec503733 KB |
2375 | struct scsi_unmap_parm_list *plist; |
2376 | struct nvme_dsm_range *range; | |
ec503733 KB |
2377 | struct nvme_command c; |
2378 | int i, nvme_sc, res = -ENOMEM; | |
2379 | u16 ndesc, list_len; | |
ec503733 | 2380 | |
3726897e | 2381 | list_len = get_unaligned_be16(&cmd[7]); |
ec503733 KB |
2382 | if (!list_len) |
2383 | return -EINVAL; | |
2384 | ||
2385 | plist = kmalloc(list_len, GFP_KERNEL); | |
2386 | if (!plist) | |
2387 | return -ENOMEM; | |
2388 | ||
2389 | res = nvme_trans_copy_from_user(hdr, plist, list_len); | |
e61b0a86 | 2390 | if (res) |
ec503733 KB |
2391 | goto out; |
2392 | ||
2393 | ndesc = be16_to_cpu(plist->unmap_blk_desc_data_len) >> 4; | |
2394 | if (!ndesc || ndesc > 256) { | |
2395 | res = -EINVAL; | |
2396 | goto out; | |
2397 | } | |
2398 | ||
d29ec824 | 2399 | range = kcalloc(ndesc, sizeof(*range), GFP_KERNEL); |
ec503733 KB |
2400 | if (!range) |
2401 | goto out; | |
2402 | ||
2403 | for (i = 0; i < ndesc; i++) { | |
2404 | range[i].nlb = cpu_to_le32(be32_to_cpu(plist->desc[i].nlb)); | |
2405 | range[i].slba = cpu_to_le64(be64_to_cpu(plist->desc[i].slba)); | |
2406 | range[i].cattr = 0; | |
2407 | } | |
2408 | ||
2409 | memset(&c, 0, sizeof(c)); | |
2410 | c.dsm.opcode = nvme_cmd_dsm; | |
2411 | c.dsm.nsid = cpu_to_le32(ns->ns_id); | |
ec503733 KB |
2412 | c.dsm.nr = cpu_to_le32(ndesc - 1); |
2413 | c.dsm.attributes = cpu_to_le32(NVME_DSMGMT_AD); | |
2414 | ||
d29ec824 CH |
2415 | nvme_sc = nvme_submit_sync_cmd(ns->queue, &c, range, |
2416 | ndesc * sizeof(*range)); | |
ec503733 KB |
2417 | res = nvme_trans_status_code(hdr, nvme_sc); |
2418 | ||
d29ec824 | 2419 | kfree(range); |
ec503733 KB |
2420 | out: |
2421 | kfree(plist); | |
2422 | return res; | |
2423 | } | |
2424 | ||
5d0f6131 VV |
2425 | static int nvme_scsi_translate(struct nvme_ns *ns, struct sg_io_hdr *hdr) |
2426 | { | |
2427 | u8 cmd[BLK_MAX_CDB]; | |
2428 | int retcode; | |
2429 | unsigned int opcode; | |
2430 | ||
2431 | if (hdr->cmdp == NULL) | |
2432 | return -EMSGSIZE; | |
2433 | if (copy_from_user(cmd, hdr->cmdp, hdr->cmd_len)) | |
2434 | return -EFAULT; | |
2435 | ||
695a4fe7 KB |
2436 | /* |
2437 | * Prime the hdr with good status for scsi commands that don't require | |
2438 | * an nvme command for translation. | |
2439 | */ | |
2440 | retcode = nvme_trans_status_code(hdr, NVME_SC_SUCCESS); | |
2441 | if (retcode) | |
2442 | return retcode; | |
2443 | ||
5d0f6131 VV |
2444 | opcode = cmd[0]; |
2445 | ||
2446 | switch (opcode) { | |
2447 | case READ_6: | |
2448 | case READ_10: | |
2449 | case READ_12: | |
2450 | case READ_16: | |
2451 | retcode = nvme_trans_io(ns, hdr, 0, cmd); | |
2452 | break; | |
2453 | case WRITE_6: | |
2454 | case WRITE_10: | |
2455 | case WRITE_12: | |
2456 | case WRITE_16: | |
2457 | retcode = nvme_trans_io(ns, hdr, 1, cmd); | |
2458 | break; | |
2459 | case INQUIRY: | |
2460 | retcode = nvme_trans_inquiry(ns, hdr, cmd); | |
2461 | break; | |
2462 | case LOG_SENSE: | |
2463 | retcode = nvme_trans_log_sense(ns, hdr, cmd); | |
2464 | break; | |
2465 | case MODE_SELECT: | |
2466 | case MODE_SELECT_10: | |
2467 | retcode = nvme_trans_mode_select(ns, hdr, cmd); | |
2468 | break; | |
2469 | case MODE_SENSE: | |
2470 | case MODE_SENSE_10: | |
2471 | retcode = nvme_trans_mode_sense(ns, hdr, cmd); | |
2472 | break; | |
2473 | case READ_CAPACITY: | |
3726897e | 2474 | retcode = nvme_trans_read_capacity(ns, hdr, cmd, 0); |
5d0f6131 | 2475 | break; |
eb846d9f | 2476 | case SERVICE_ACTION_IN_16: |
3726897e CH |
2477 | switch (cmd[1]) { |
2478 | case SAI_READ_CAPACITY_16: | |
2479 | retcode = nvme_trans_read_capacity(ns, hdr, cmd, 1); | |
2480 | break; | |
2481 | default: | |
5d0f6131 | 2482 | goto out; |
3726897e | 2483 | } |
5d0f6131 VV |
2484 | break; |
2485 | case REPORT_LUNS: | |
2486 | retcode = nvme_trans_report_luns(ns, hdr, cmd); | |
2487 | break; | |
2488 | case REQUEST_SENSE: | |
2489 | retcode = nvme_trans_request_sense(ns, hdr, cmd); | |
2490 | break; | |
2491 | case SECURITY_PROTOCOL_IN: | |
2492 | case SECURITY_PROTOCOL_OUT: | |
2493 | retcode = nvme_trans_security_protocol(ns, hdr, cmd); | |
2494 | break; | |
2495 | case START_STOP: | |
2496 | retcode = nvme_trans_start_stop(ns, hdr, cmd); | |
2497 | break; | |
2498 | case SYNCHRONIZE_CACHE: | |
d29ec824 | 2499 | retcode = nvme_trans_synchronize_cache(ns, hdr); |
5d0f6131 VV |
2500 | break; |
2501 | case FORMAT_UNIT: | |
2502 | retcode = nvme_trans_format_unit(ns, hdr, cmd); | |
2503 | break; | |
2504 | case TEST_UNIT_READY: | |
2505 | retcode = nvme_trans_test_unit_ready(ns, hdr, cmd); | |
2506 | break; | |
2507 | case WRITE_BUFFER: | |
2508 | retcode = nvme_trans_write_buffer(ns, hdr, cmd); | |
2509 | break; | |
ec503733 KB |
2510 | case UNMAP: |
2511 | retcode = nvme_trans_unmap(ns, hdr, cmd); | |
2512 | break; | |
5d0f6131 VV |
2513 | default: |
2514 | out: | |
2515 | retcode = nvme_trans_completion(hdr, SAM_STAT_CHECK_CONDITION, | |
2516 | ILLEGAL_REQUEST, SCSI_ASC_ILLEGAL_COMMAND, | |
2517 | SCSI_ASCQ_CAUSE_NOT_REPORTABLE); | |
2518 | break; | |
2519 | } | |
2520 | return retcode; | |
2521 | } | |
2522 | ||
2523 | int nvme_sg_io(struct nvme_ns *ns, struct sg_io_hdr __user *u_hdr) | |
2524 | { | |
2525 | struct sg_io_hdr hdr; | |
2526 | int retcode; | |
2527 | ||
2528 | if (!capable(CAP_SYS_ADMIN)) | |
2529 | return -EACCES; | |
2530 | if (copy_from_user(&hdr, u_hdr, sizeof(hdr))) | |
2531 | return -EFAULT; | |
2532 | if (hdr.interface_id != 'S') | |
2533 | return -EINVAL; | |
2534 | if (hdr.cmd_len > BLK_MAX_CDB) | |
2535 | return -EINVAL; | |
2536 | ||
e61b0a86 CH |
2537 | /* |
2538 | * A positive return code means a NVMe status, which has been | |
2539 | * translated to sense data. | |
2540 | */ | |
5d0f6131 VV |
2541 | retcode = nvme_scsi_translate(ns, &hdr); |
2542 | if (retcode < 0) | |
2543 | return retcode; | |
8741ee4c | 2544 | if (copy_to_user(u_hdr, &hdr, sizeof(sg_io_hdr_t)) > 0) |
5d0f6131 | 2545 | return -EFAULT; |
e61b0a86 | 2546 | return 0; |
5d0f6131 VV |
2547 | } |
2548 | ||
2549 | int nvme_sg_get_version_num(int __user *ip) | |
2550 | { | |
2551 | return put_user(sg_version_num, ip); | |
2552 | } |