2 * NVM Express device driver
3 * Copyright (c) 2011, Intel Corporation.
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.
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
14 * You should have received a copy of the GNU General Public License along with
15 * this program; if not, write to the Free Software Foundation, Inc.,
16 * 51 Franklin St - Fifth Floor, Boston, MA 02110-1301 USA.
20 * Refer to the SCSI-NVMe Translation spec for details on how
21 * each command is translated.
24 #include <linux/nvme.h>
25 #include <linux/bio.h>
26 #include <linux/bitops.h>
27 #include <linux/blkdev.h>
28 #include <linux/delay.h>
29 #include <linux/errno.h>
31 #include <linux/genhd.h>
32 #include <linux/idr.h>
33 #include <linux/init.h>
34 #include <linux/interrupt.h>
36 #include <linux/kdev_t.h>
37 #include <linux/kthread.h>
38 #include <linux/kernel.h>
40 #include <linux/module.h>
41 #include <linux/moduleparam.h>
42 #include <linux/pci.h>
43 #include <linux/poison.h>
44 #include <linux/sched.h>
45 #include <linux/slab.h>
46 #include <linux/types.h>
47 #include <linux/version.h>
49 #include <scsi/scsi.h>
52 static int sg_version_num
= 30534; /* 2 digits for each component */
54 #define SNTI_TRANSLATION_SUCCESS 0
55 #define SNTI_INTERNAL_ERROR 1
58 #define VPD_SUPPORTED_PAGES 0x00
59 #define VPD_SERIAL_NUMBER 0x80
60 #define VPD_DEVICE_IDENTIFIERS 0x83
61 #define VPD_EXTENDED_INQUIRY 0x86
62 #define VPD_BLOCK_DEV_CHARACTERISTICS 0xB1
65 #define REPORT_LUNS_CDB_ALLOC_LENGTH_OFFSET 6
66 #define REPORT_LUNS_SR_OFFSET 2
67 #define READ_CAP_16_CDB_ALLOC_LENGTH_OFFSET 10
68 #define REQUEST_SENSE_CDB_ALLOC_LENGTH_OFFSET 4
69 #define REQUEST_SENSE_DESC_OFFSET 1
70 #define REQUEST_SENSE_DESC_MASK 0x01
71 #define DESCRIPTOR_FORMAT_SENSE_DATA_TYPE 1
72 #define INQUIRY_EVPD_BYTE_OFFSET 1
73 #define INQUIRY_PAGE_CODE_BYTE_OFFSET 2
74 #define INQUIRY_EVPD_BIT_MASK 1
75 #define INQUIRY_CDB_ALLOCATION_LENGTH_OFFSET 3
76 #define START_STOP_UNIT_CDB_IMMED_OFFSET 1
77 #define START_STOP_UNIT_CDB_IMMED_MASK 0x1
78 #define START_STOP_UNIT_CDB_POWER_COND_MOD_OFFSET 3
79 #define START_STOP_UNIT_CDB_POWER_COND_MOD_MASK 0xF
80 #define START_STOP_UNIT_CDB_POWER_COND_OFFSET 4
81 #define START_STOP_UNIT_CDB_POWER_COND_MASK 0xF0
82 #define START_STOP_UNIT_CDB_NO_FLUSH_OFFSET 4
83 #define START_STOP_UNIT_CDB_NO_FLUSH_MASK 0x4
84 #define START_STOP_UNIT_CDB_START_OFFSET 4
85 #define START_STOP_UNIT_CDB_START_MASK 0x1
86 #define WRITE_BUFFER_CDB_MODE_OFFSET 1
87 #define WRITE_BUFFER_CDB_MODE_MASK 0x1F
88 #define WRITE_BUFFER_CDB_BUFFER_ID_OFFSET 2
89 #define WRITE_BUFFER_CDB_BUFFER_OFFSET_OFFSET 3
90 #define WRITE_BUFFER_CDB_PARM_LIST_LENGTH_OFFSET 6
91 #define FORMAT_UNIT_CDB_FORMAT_PROT_INFO_OFFSET 1
92 #define FORMAT_UNIT_CDB_FORMAT_PROT_INFO_MASK 0xC0
93 #define FORMAT_UNIT_CDB_FORMAT_PROT_INFO_SHIFT 6
94 #define FORMAT_UNIT_CDB_LONG_LIST_OFFSET 1
95 #define FORMAT_UNIT_CDB_LONG_LIST_MASK 0x20
96 #define FORMAT_UNIT_CDB_FORMAT_DATA_OFFSET 1
97 #define FORMAT_UNIT_CDB_FORMAT_DATA_MASK 0x10
98 #define FORMAT_UNIT_SHORT_PARM_LIST_LEN 4
99 #define FORMAT_UNIT_LONG_PARM_LIST_LEN 8
100 #define FORMAT_UNIT_PROT_INT_OFFSET 3
101 #define FORMAT_UNIT_PROT_FIELD_USAGE_OFFSET 0
102 #define FORMAT_UNIT_PROT_FIELD_USAGE_MASK 0x07
105 #define NIBBLE_SHIFT 4
106 #define FIXED_SENSE_DATA 0x70
107 #define DESC_FORMAT_SENSE_DATA 0x72
108 #define FIXED_SENSE_DATA_ADD_LENGTH 10
109 #define LUN_ENTRY_SIZE 8
110 #define LUN_DATA_HEADER_SIZE 8
111 #define ALL_LUNS_RETURNED 0x02
112 #define ALL_WELL_KNOWN_LUNS_RETURNED 0x01
113 #define RESTRICTED_LUNS_RETURNED 0x00
114 #define NVME_POWER_STATE_START_VALID 0x00
115 #define NVME_POWER_STATE_ACTIVE 0x01
116 #define NVME_POWER_STATE_IDLE 0x02
117 #define NVME_POWER_STATE_STANDBY 0x03
118 #define NVME_POWER_STATE_LU_CONTROL 0x07
119 #define POWER_STATE_0 0
120 #define POWER_STATE_1 1
121 #define POWER_STATE_2 2
122 #define POWER_STATE_3 3
123 #define DOWNLOAD_SAVE_ACTIVATE 0x05
124 #define DOWNLOAD_SAVE_DEFER_ACTIVATE 0x0E
125 #define ACTIVATE_DEFERRED_MICROCODE 0x0F
126 #define FORMAT_UNIT_IMMED_MASK 0x2
127 #define FORMAT_UNIT_IMMED_OFFSET 1
128 #define KELVIN_TEMP_FACTOR 273
129 #define FIXED_FMT_SENSE_DATA_SIZE 18
130 #define DESC_FMT_SENSE_DATA_SIZE 8
132 /* SCSI/NVMe defines and bit masks */
133 #define INQ_STANDARD_INQUIRY_PAGE 0x00
134 #define INQ_SUPPORTED_VPD_PAGES_PAGE 0x00
135 #define INQ_UNIT_SERIAL_NUMBER_PAGE 0x80
136 #define INQ_DEVICE_IDENTIFICATION_PAGE 0x83
137 #define INQ_EXTENDED_INQUIRY_DATA_PAGE 0x86
138 #define INQ_BDEV_CHARACTERISTICS_PAGE 0xB1
139 #define INQ_SERIAL_NUMBER_LENGTH 0x14
140 #define INQ_NUM_SUPPORTED_VPD_PAGES 5
141 #define VERSION_SPC_4 0x06
142 #define ACA_UNSUPPORTED 0
143 #define STANDARD_INQUIRY_LENGTH 36
144 #define ADDITIONAL_STD_INQ_LENGTH 31
145 #define EXTENDED_INQUIRY_DATA_PAGE_LENGTH 0x3C
146 #define RESERVED_FIELD 0
148 /* SCSI READ/WRITE Defines */
149 #define IO_CDB_WP_MASK 0xE0
150 #define IO_CDB_WP_SHIFT 5
151 #define IO_CDB_FUA_MASK 0x8
152 #define IO_6_CDB_LBA_OFFSET 0
153 #define IO_6_CDB_LBA_MASK 0x001FFFFF
154 #define IO_6_CDB_TX_LEN_OFFSET 4
155 #define IO_6_DEFAULT_TX_LEN 256
156 #define IO_10_CDB_LBA_OFFSET 2
157 #define IO_10_CDB_TX_LEN_OFFSET 7
158 #define IO_10_CDB_WP_OFFSET 1
159 #define IO_10_CDB_FUA_OFFSET 1
160 #define IO_12_CDB_LBA_OFFSET 2
161 #define IO_12_CDB_TX_LEN_OFFSET 6
162 #define IO_12_CDB_WP_OFFSET 1
163 #define IO_12_CDB_FUA_OFFSET 1
164 #define IO_16_CDB_FUA_OFFSET 1
165 #define IO_16_CDB_WP_OFFSET 1
166 #define IO_16_CDB_LBA_OFFSET 2
167 #define IO_16_CDB_TX_LEN_OFFSET 10
169 /* Mode Sense/Select defines */
170 #define MODE_PAGE_INFO_EXCEP 0x1C
171 #define MODE_PAGE_CACHING 0x08
172 #define MODE_PAGE_CONTROL 0x0A
173 #define MODE_PAGE_POWER_CONDITION 0x1A
174 #define MODE_PAGE_RETURN_ALL 0x3F
175 #define MODE_PAGE_BLK_DES_LEN 0x08
176 #define MODE_PAGE_LLBAA_BLK_DES_LEN 0x10
177 #define MODE_PAGE_CACHING_LEN 0x14
178 #define MODE_PAGE_CONTROL_LEN 0x0C
179 #define MODE_PAGE_POW_CND_LEN 0x28
180 #define MODE_PAGE_INF_EXC_LEN 0x0C
181 #define MODE_PAGE_ALL_LEN 0x54
182 #define MODE_SENSE6_MPH_SIZE 4
183 #define MODE_SENSE6_ALLOC_LEN_OFFSET 4
184 #define MODE_SENSE_PAGE_CONTROL_OFFSET 2
185 #define MODE_SENSE_PAGE_CONTROL_MASK 0xC0
186 #define MODE_SENSE_PAGE_CODE_OFFSET 2
187 #define MODE_SENSE_PAGE_CODE_MASK 0x3F
188 #define MODE_SENSE_LLBAA_OFFSET 1
189 #define MODE_SENSE_LLBAA_MASK 0x10
190 #define MODE_SENSE_LLBAA_SHIFT 4
191 #define MODE_SENSE_DBD_OFFSET 1
192 #define MODE_SENSE_DBD_MASK 8
193 #define MODE_SENSE_DBD_SHIFT 3
194 #define MODE_SENSE10_MPH_SIZE 8
195 #define MODE_SENSE10_ALLOC_LEN_OFFSET 7
196 #define MODE_SELECT_CDB_PAGE_FORMAT_OFFSET 1
197 #define MODE_SELECT_CDB_SAVE_PAGES_OFFSET 1
198 #define MODE_SELECT_6_CDB_PARAM_LIST_LENGTH_OFFSET 4
199 #define MODE_SELECT_10_CDB_PARAM_LIST_LENGTH_OFFSET 7
200 #define MODE_SELECT_CDB_PAGE_FORMAT_MASK 0x10
201 #define MODE_SELECT_CDB_SAVE_PAGES_MASK 0x1
202 #define MODE_SELECT_6_BD_OFFSET 3
203 #define MODE_SELECT_10_BD_OFFSET 6
204 #define MODE_SELECT_10_LLBAA_OFFSET 4
205 #define MODE_SELECT_10_LLBAA_MASK 1
206 #define MODE_SELECT_6_MPH_SIZE 4
207 #define MODE_SELECT_10_MPH_SIZE 8
208 #define CACHING_MODE_PAGE_WCE_MASK 0x04
209 #define MODE_SENSE_BLK_DESC_ENABLED 0
210 #define MODE_SENSE_BLK_DESC_COUNT 1
211 #define MODE_SELECT_PAGE_CODE_MASK 0x3F
212 #define SHORT_DESC_BLOCK 8
213 #define LONG_DESC_BLOCK 16
214 #define MODE_PAGE_POW_CND_LEN_FIELD 0x26
215 #define MODE_PAGE_INF_EXC_LEN_FIELD 0x0A
216 #define MODE_PAGE_CACHING_LEN_FIELD 0x12
217 #define MODE_PAGE_CONTROL_LEN_FIELD 0x0A
218 #define MODE_SENSE_PC_CURRENT_VALUES 0
220 /* Log Sense defines */
221 #define LOG_PAGE_SUPPORTED_LOG_PAGES_PAGE 0x00
222 #define LOG_PAGE_SUPPORTED_LOG_PAGES_LENGTH 0x07
223 #define LOG_PAGE_INFORMATIONAL_EXCEPTIONS_PAGE 0x2F
224 #define LOG_PAGE_TEMPERATURE_PAGE 0x0D
225 #define LOG_SENSE_CDB_SP_OFFSET 1
226 #define LOG_SENSE_CDB_SP_NOT_ENABLED 0
227 #define LOG_SENSE_CDB_PC_OFFSET 2
228 #define LOG_SENSE_CDB_PC_MASK 0xC0
229 #define LOG_SENSE_CDB_PC_SHIFT 6
230 #define LOG_SENSE_CDB_PC_CUMULATIVE_VALUES 1
231 #define LOG_SENSE_CDB_PAGE_CODE_MASK 0x3F
232 #define LOG_SENSE_CDB_ALLOC_LENGTH_OFFSET 7
233 #define REMAINING_INFO_EXCP_PAGE_LENGTH 0x8
234 #define LOG_INFO_EXCP_PAGE_LENGTH 0xC
235 #define REMAINING_TEMP_PAGE_LENGTH 0xC
236 #define LOG_TEMP_PAGE_LENGTH 0x10
237 #define LOG_TEMP_UNKNOWN 0xFF
238 #define SUPPORTED_LOG_PAGES_PAGE_LENGTH 0x3
240 /* Read Capacity defines */
241 #define READ_CAP_10_RESP_SIZE 8
242 #define READ_CAP_16_RESP_SIZE 32
244 /* NVMe Namespace and Command Defines */
245 #define NVME_GET_SMART_LOG_PAGE 0x02
246 #define NVME_GET_FEAT_TEMP_THRESH 0x04
247 #define BYTES_TO_DWORDS 4
248 #define NVME_MAX_FIRMWARE_SLOT 7
250 /* Report LUNs defines */
251 #define REPORT_LUNS_FIRST_LUN_OFFSET 8
253 /* SCSI ADDITIONAL SENSE Codes */
255 #define SCSI_ASC_NO_SENSE 0x00
256 #define SCSI_ASC_PERIPHERAL_DEV_WRITE_FAULT 0x03
257 #define SCSI_ASC_LUN_NOT_READY 0x04
258 #define SCSI_ASC_WARNING 0x0B
259 #define SCSI_ASC_LOG_BLOCK_GUARD_CHECK_FAILED 0x10
260 #define SCSI_ASC_LOG_BLOCK_APPTAG_CHECK_FAILED 0x10
261 #define SCSI_ASC_LOG_BLOCK_REFTAG_CHECK_FAILED 0x10
262 #define SCSI_ASC_UNRECOVERED_READ_ERROR 0x11
263 #define SCSI_ASC_MISCOMPARE_DURING_VERIFY 0x1D
264 #define SCSI_ASC_ACCESS_DENIED_INVALID_LUN_ID 0x20
265 #define SCSI_ASC_ILLEGAL_COMMAND 0x20
266 #define SCSI_ASC_ILLEGAL_BLOCK 0x21
267 #define SCSI_ASC_INVALID_CDB 0x24
268 #define SCSI_ASC_INVALID_LUN 0x25
269 #define SCSI_ASC_INVALID_PARAMETER 0x26
270 #define SCSI_ASC_FORMAT_COMMAND_FAILED 0x31
271 #define SCSI_ASC_INTERNAL_TARGET_FAILURE 0x44
273 /* SCSI ADDITIONAL SENSE Code Qualifiers */
275 #define SCSI_ASCQ_CAUSE_NOT_REPORTABLE 0x00
276 #define SCSI_ASCQ_FORMAT_COMMAND_FAILED 0x01
277 #define SCSI_ASCQ_LOG_BLOCK_GUARD_CHECK_FAILED 0x01
278 #define SCSI_ASCQ_LOG_BLOCK_APPTAG_CHECK_FAILED 0x02
279 #define SCSI_ASCQ_LOG_BLOCK_REFTAG_CHECK_FAILED 0x03
280 #define SCSI_ASCQ_FORMAT_IN_PROGRESS 0x04
281 #define SCSI_ASCQ_POWER_LOSS_EXPECTED 0x08
282 #define SCSI_ASCQ_INVALID_LUN_ID 0x09
285 * DEVICE_SPECIFIC_PARAMETER in mode parameter header (see sbc2r16) to
286 * enable DPOFUA support type 0x10 value.
288 #define DEVICE_SPECIFIC_PARAMETER 0
289 #define VPD_ID_DESCRIPTOR_LENGTH sizeof(VPD_IDENTIFICATION_DESCRIPTOR)
291 /* MACROs to extract information from CDBs */
293 #define GET_OPCODE(cdb) cdb[0]
295 #define GET_U8_FROM_CDB(cdb, index) (cdb[index] << 0)
297 #define GET_U16_FROM_CDB(cdb, index) ((cdb[index] << 8) | (cdb[index + 1] << 0))
299 #define GET_U24_FROM_CDB(cdb, index) ((cdb[index] << 16) | \
300 (cdb[index + 1] << 8) | \
301 (cdb[index + 2] << 0))
303 #define GET_U32_FROM_CDB(cdb, index) ((cdb[index] << 24) | \
304 (cdb[index + 1] << 16) | \
305 (cdb[index + 2] << 8) | \
306 (cdb[index + 3] << 0))
308 #define GET_U64_FROM_CDB(cdb, index) ((((u64)cdb[index]) << 56) | \
309 (((u64)cdb[index + 1]) << 48) | \
310 (((u64)cdb[index + 2]) << 40) | \
311 (((u64)cdb[index + 3]) << 32) | \
312 (((u64)cdb[index + 4]) << 24) | \
313 (((u64)cdb[index + 5]) << 16) | \
314 (((u64)cdb[index + 6]) << 8) | \
315 (((u64)cdb[index + 7]) << 0))
317 /* Inquiry Helper Macros */
318 #define GET_INQ_EVPD_BIT(cdb) \
319 ((GET_U8_FROM_CDB(cdb, INQUIRY_EVPD_BYTE_OFFSET) & \
320 INQUIRY_EVPD_BIT_MASK) ? 1 : 0)
322 #define GET_INQ_PAGE_CODE(cdb) \
323 (GET_U8_FROM_CDB(cdb, INQUIRY_PAGE_CODE_BYTE_OFFSET))
325 #define GET_INQ_ALLOC_LENGTH(cdb) \
326 (GET_U16_FROM_CDB(cdb, INQUIRY_CDB_ALLOCATION_LENGTH_OFFSET))
328 /* Report LUNs Helper Macros */
329 #define GET_REPORT_LUNS_ALLOC_LENGTH(cdb) \
330 (GET_U32_FROM_CDB(cdb, REPORT_LUNS_CDB_ALLOC_LENGTH_OFFSET))
332 /* Read Capacity Helper Macros */
333 #define GET_READ_CAP_16_ALLOC_LENGTH(cdb) \
334 (GET_U32_FROM_CDB(cdb, READ_CAP_16_CDB_ALLOC_LENGTH_OFFSET))
336 #define IS_READ_CAP_16(cdb) \
337 ((cdb[0] == SERVICE_ACTION_IN && cdb[1] == SAI_READ_CAPACITY_16) ? 1 : 0)
339 /* Request Sense Helper Macros */
340 #define GET_REQUEST_SENSE_ALLOC_LENGTH(cdb) \
341 (GET_U8_FROM_CDB(cdb, REQUEST_SENSE_CDB_ALLOC_LENGTH_OFFSET))
343 /* Mode Sense Helper Macros */
344 #define GET_MODE_SENSE_DBD(cdb) \
345 ((GET_U8_FROM_CDB(cdb, MODE_SENSE_DBD_OFFSET) & MODE_SENSE_DBD_MASK) >> \
346 MODE_SENSE_DBD_SHIFT)
348 #define GET_MODE_SENSE_LLBAA(cdb) \
349 ((GET_U8_FROM_CDB(cdb, MODE_SENSE_LLBAA_OFFSET) & \
350 MODE_SENSE_LLBAA_MASK) >> MODE_SENSE_LLBAA_SHIFT)
352 #define GET_MODE_SENSE_MPH_SIZE(cdb10) \
353 (cdb10 ? MODE_SENSE10_MPH_SIZE : MODE_SENSE6_MPH_SIZE)
356 /* Struct to gather data that needs to be extracted from a SCSI CDB.
357 Not conforming to any particular CDB variant, but compatible with all. */
359 struct nvme_trans_io_cdb
{
367 /* Internal Helper Functions */
370 /* Copy data to userspace memory */
372 static int nvme_trans_copy_to_user(struct sg_io_hdr
*hdr
, void *from
,
375 int res
= SNTI_TRANSLATION_SUCCESS
;
376 unsigned long not_copied
;
379 size_t remaining
= n
;
382 if (hdr
->iovec_count
> 0) {
383 struct sg_iovec
*sgl
= hdr
->dxferp
;
385 for (i
= 0; i
< hdr
->iovec_count
; i
++) {
386 xfer_len
= min(remaining
, sgl
[i
].iov_len
);
387 not_copied
= copy_to_user(__user sgl
[i
].iov_base
, index
,
394 remaining
-= xfer_len
;
400 not_copied
= copy_to_user(__user hdr
->dxferp
, from
, n
);
406 /* Copy data from userspace memory */
408 static int nvme_trans_copy_from_user(struct sg_io_hdr
*hdr
, void *to
,
411 int res
= SNTI_TRANSLATION_SUCCESS
;
412 unsigned long not_copied
;
415 size_t remaining
= n
;
418 if (hdr
->iovec_count
> 0) {
419 struct sg_iovec
*sgl
= hdr
->dxferp
;
421 for (i
= 0; i
< hdr
->iovec_count
; i
++) {
422 xfer_len
= min(remaining
, sgl
[i
].iov_len
);
423 not_copied
= copy_from_user(index
,
424 __user sgl
[i
].iov_base
, xfer_len
);
430 remaining
-= xfer_len
;
437 not_copied
= copy_from_user(to
, __user hdr
->dxferp
, n
);
443 /* Status/Sense Buffer Writeback */
445 static int nvme_trans_completion(struct sg_io_hdr
*hdr
, u8 status
, u8 sense_key
,
448 int res
= SNTI_TRANSLATION_SUCCESS
;
450 u8 resp
[DESC_FMT_SENSE_DATA_SIZE
];
452 if (scsi_status_is_good(status
)) {
453 hdr
->status
= SAM_STAT_GOOD
;
454 hdr
->masked_status
= GOOD
;
455 hdr
->host_status
= DID_OK
;
456 hdr
->driver_status
= DRIVER_OK
;
459 hdr
->status
= status
;
460 hdr
->masked_status
= status
>> 1;
461 hdr
->host_status
= DID_OK
;
462 hdr
->driver_status
= DRIVER_OK
;
464 memset(resp
, 0, DESC_FMT_SENSE_DATA_SIZE
);
465 resp
[0] = DESC_FORMAT_SENSE_DATA
;
470 xfer_len
= min_t(u8
, hdr
->mx_sb_len
, DESC_FMT_SENSE_DATA_SIZE
);
471 hdr
->sb_len_wr
= xfer_len
;
472 if (copy_to_user(__user hdr
->sbp
, resp
, xfer_len
) > 0)
479 static int nvme_trans_status_code(struct sg_io_hdr
*hdr
, int nvme_sc
)
481 u8 status
, sense_key
, asc
, ascq
;
482 int res
= SNTI_TRANSLATION_SUCCESS
;
484 /* For non-nvme (Linux) errors, simply return the error code */
488 /* Mask DNR, More, and reserved fields */
492 /* Generic Command Status */
493 case NVME_SC_SUCCESS
:
494 status
= SAM_STAT_GOOD
;
495 sense_key
= NO_SENSE
;
496 asc
= SCSI_ASC_NO_SENSE
;
497 ascq
= SCSI_ASCQ_CAUSE_NOT_REPORTABLE
;
499 case NVME_SC_INVALID_OPCODE
:
500 status
= SAM_STAT_CHECK_CONDITION
;
501 sense_key
= ILLEGAL_REQUEST
;
502 asc
= SCSI_ASC_ILLEGAL_COMMAND
;
503 ascq
= SCSI_ASCQ_CAUSE_NOT_REPORTABLE
;
505 case NVME_SC_INVALID_FIELD
:
506 status
= SAM_STAT_CHECK_CONDITION
;
507 sense_key
= ILLEGAL_REQUEST
;
508 asc
= SCSI_ASC_INVALID_CDB
;
509 ascq
= SCSI_ASCQ_CAUSE_NOT_REPORTABLE
;
511 case NVME_SC_DATA_XFER_ERROR
:
512 status
= SAM_STAT_CHECK_CONDITION
;
513 sense_key
= MEDIUM_ERROR
;
514 asc
= SCSI_ASC_NO_SENSE
;
515 ascq
= SCSI_ASCQ_CAUSE_NOT_REPORTABLE
;
517 case NVME_SC_POWER_LOSS
:
518 status
= SAM_STAT_TASK_ABORTED
;
519 sense_key
= ABORTED_COMMAND
;
520 asc
= SCSI_ASC_WARNING
;
521 ascq
= SCSI_ASCQ_POWER_LOSS_EXPECTED
;
523 case NVME_SC_INTERNAL
:
524 status
= SAM_STAT_CHECK_CONDITION
;
525 sense_key
= HARDWARE_ERROR
;
526 asc
= SCSI_ASC_INTERNAL_TARGET_FAILURE
;
527 ascq
= SCSI_ASCQ_CAUSE_NOT_REPORTABLE
;
529 case NVME_SC_ABORT_REQ
:
530 status
= SAM_STAT_TASK_ABORTED
;
531 sense_key
= ABORTED_COMMAND
;
532 asc
= SCSI_ASC_NO_SENSE
;
533 ascq
= SCSI_ASCQ_CAUSE_NOT_REPORTABLE
;
535 case NVME_SC_ABORT_QUEUE
:
536 status
= SAM_STAT_TASK_ABORTED
;
537 sense_key
= ABORTED_COMMAND
;
538 asc
= SCSI_ASC_NO_SENSE
;
539 ascq
= SCSI_ASCQ_CAUSE_NOT_REPORTABLE
;
541 case NVME_SC_FUSED_FAIL
:
542 status
= SAM_STAT_TASK_ABORTED
;
543 sense_key
= ABORTED_COMMAND
;
544 asc
= SCSI_ASC_NO_SENSE
;
545 ascq
= SCSI_ASCQ_CAUSE_NOT_REPORTABLE
;
547 case NVME_SC_FUSED_MISSING
:
548 status
= SAM_STAT_TASK_ABORTED
;
549 sense_key
= ABORTED_COMMAND
;
550 asc
= SCSI_ASC_NO_SENSE
;
551 ascq
= SCSI_ASCQ_CAUSE_NOT_REPORTABLE
;
553 case NVME_SC_INVALID_NS
:
554 status
= SAM_STAT_CHECK_CONDITION
;
555 sense_key
= ILLEGAL_REQUEST
;
556 asc
= SCSI_ASC_ACCESS_DENIED_INVALID_LUN_ID
;
557 ascq
= SCSI_ASCQ_INVALID_LUN_ID
;
559 case NVME_SC_LBA_RANGE
:
560 status
= SAM_STAT_CHECK_CONDITION
;
561 sense_key
= ILLEGAL_REQUEST
;
562 asc
= SCSI_ASC_ILLEGAL_BLOCK
;
563 ascq
= SCSI_ASCQ_CAUSE_NOT_REPORTABLE
;
565 case NVME_SC_CAP_EXCEEDED
:
566 status
= SAM_STAT_CHECK_CONDITION
;
567 sense_key
= MEDIUM_ERROR
;
568 asc
= SCSI_ASC_NO_SENSE
;
569 ascq
= SCSI_ASCQ_CAUSE_NOT_REPORTABLE
;
571 case NVME_SC_NS_NOT_READY
:
572 status
= SAM_STAT_CHECK_CONDITION
;
573 sense_key
= NOT_READY
;
574 asc
= SCSI_ASC_LUN_NOT_READY
;
575 ascq
= SCSI_ASCQ_CAUSE_NOT_REPORTABLE
;
578 /* Command Specific Status */
579 case NVME_SC_INVALID_FORMAT
:
580 status
= SAM_STAT_CHECK_CONDITION
;
581 sense_key
= ILLEGAL_REQUEST
;
582 asc
= SCSI_ASC_FORMAT_COMMAND_FAILED
;
583 ascq
= SCSI_ASCQ_FORMAT_COMMAND_FAILED
;
585 case NVME_SC_BAD_ATTRIBUTES
:
586 status
= SAM_STAT_CHECK_CONDITION
;
587 sense_key
= ILLEGAL_REQUEST
;
588 asc
= SCSI_ASC_INVALID_CDB
;
589 ascq
= SCSI_ASCQ_CAUSE_NOT_REPORTABLE
;
593 case NVME_SC_WRITE_FAULT
:
594 status
= SAM_STAT_CHECK_CONDITION
;
595 sense_key
= MEDIUM_ERROR
;
596 asc
= SCSI_ASC_PERIPHERAL_DEV_WRITE_FAULT
;
597 ascq
= SCSI_ASCQ_CAUSE_NOT_REPORTABLE
;
599 case NVME_SC_READ_ERROR
:
600 status
= SAM_STAT_CHECK_CONDITION
;
601 sense_key
= MEDIUM_ERROR
;
602 asc
= SCSI_ASC_UNRECOVERED_READ_ERROR
;
603 ascq
= SCSI_ASCQ_CAUSE_NOT_REPORTABLE
;
605 case NVME_SC_GUARD_CHECK
:
606 status
= SAM_STAT_CHECK_CONDITION
;
607 sense_key
= MEDIUM_ERROR
;
608 asc
= SCSI_ASC_LOG_BLOCK_GUARD_CHECK_FAILED
;
609 ascq
= SCSI_ASCQ_LOG_BLOCK_GUARD_CHECK_FAILED
;
611 case NVME_SC_APPTAG_CHECK
:
612 status
= SAM_STAT_CHECK_CONDITION
;
613 sense_key
= MEDIUM_ERROR
;
614 asc
= SCSI_ASC_LOG_BLOCK_APPTAG_CHECK_FAILED
;
615 ascq
= SCSI_ASCQ_LOG_BLOCK_APPTAG_CHECK_FAILED
;
617 case NVME_SC_REFTAG_CHECK
:
618 status
= SAM_STAT_CHECK_CONDITION
;
619 sense_key
= MEDIUM_ERROR
;
620 asc
= SCSI_ASC_LOG_BLOCK_REFTAG_CHECK_FAILED
;
621 ascq
= SCSI_ASCQ_LOG_BLOCK_REFTAG_CHECK_FAILED
;
623 case NVME_SC_COMPARE_FAILED
:
624 status
= SAM_STAT_CHECK_CONDITION
;
625 sense_key
= MISCOMPARE
;
626 asc
= SCSI_ASC_MISCOMPARE_DURING_VERIFY
;
627 ascq
= SCSI_ASCQ_CAUSE_NOT_REPORTABLE
;
629 case NVME_SC_ACCESS_DENIED
:
630 status
= SAM_STAT_CHECK_CONDITION
;
631 sense_key
= ILLEGAL_REQUEST
;
632 asc
= SCSI_ASC_ACCESS_DENIED_INVALID_LUN_ID
;
633 ascq
= SCSI_ASCQ_INVALID_LUN_ID
;
636 /* Unspecified/Default */
637 case NVME_SC_CMDID_CONFLICT
:
638 case NVME_SC_CMD_SEQ_ERROR
:
639 case NVME_SC_CQ_INVALID
:
640 case NVME_SC_QID_INVALID
:
641 case NVME_SC_QUEUE_SIZE
:
642 case NVME_SC_ABORT_LIMIT
:
643 case NVME_SC_ABORT_MISSING
:
644 case NVME_SC_ASYNC_LIMIT
:
645 case NVME_SC_FIRMWARE_SLOT
:
646 case NVME_SC_FIRMWARE_IMAGE
:
647 case NVME_SC_INVALID_VECTOR
:
648 case NVME_SC_INVALID_LOG_PAGE
:
650 status
= SAM_STAT_CHECK_CONDITION
;
651 sense_key
= ILLEGAL_REQUEST
;
652 asc
= SCSI_ASC_NO_SENSE
;
653 ascq
= SCSI_ASCQ_CAUSE_NOT_REPORTABLE
;
657 res
= nvme_trans_completion(hdr
, status
, sense_key
, asc
, ascq
);
662 /* INQUIRY Helper Functions */
664 static int nvme_trans_standard_inquiry_page(struct nvme_ns
*ns
,
665 struct sg_io_hdr
*hdr
, u8
*inq_response
,
668 struct nvme_dev
*dev
= ns
->dev
;
671 struct nvme_id_ns
*id_ns
;
672 int res
= SNTI_TRANSLATION_SUCCESS
;
675 u8 resp_data_format
= 0x02;
677 u8 cmdque
= 0x01 << 1;
679 mem
= dma_alloc_coherent(&dev
->pci_dev
->dev
, sizeof(struct nvme_id_ns
),
680 &dma_addr
, GFP_KERNEL
);
686 /* nvme ns identify - use DPS value for PROTECT field */
687 nvme_sc
= nvme_identify(dev
, ns
->ns_id
, 0, dma_addr
);
688 res
= nvme_trans_status_code(hdr
, nvme_sc
);
690 * If nvme_sc was -ve, res will be -ve here.
691 * If nvme_sc was +ve, the status would bace been translated, and res
692 * can only be 0 or -ve.
693 * - If 0 && nvme_sc > 0, then go into next if where res gets nvme_sc
694 * - If -ve, return because its a Linux error.
703 (id_ns
->dps
) ? (protect
= 0x01) : (protect
= 0);
705 memset(inq_response
, 0, STANDARD_INQUIRY_LENGTH
);
706 inq_response
[2] = VERSION_SPC_4
;
707 inq_response
[3] = resp_data_format
; /*normaca=0 | hisup=0 */
708 inq_response
[4] = ADDITIONAL_STD_INQ_LENGTH
;
709 inq_response
[5] = protect
; /* sccs=0 | acc=0 | tpgs=0 | pc3=0 */
710 inq_response
[7] = cmdque
; /* wbus16=0 | sync=0 | vs=0 */
711 strncpy(&inq_response
[8], "NVMe ", 8);
712 strncpy(&inq_response
[16], dev
->model
, 16);
713 strncpy(&inq_response
[32], dev
->firmware_rev
, 4);
715 xfer_len
= min(alloc_len
, STANDARD_INQUIRY_LENGTH
);
716 res
= nvme_trans_copy_to_user(hdr
, inq_response
, xfer_len
);
719 dma_free_coherent(&dev
->pci_dev
->dev
, sizeof(struct nvme_id_ns
), mem
,
725 static int nvme_trans_supported_vpd_pages(struct nvme_ns
*ns
,
726 struct sg_io_hdr
*hdr
, u8
*inq_response
,
729 int res
= SNTI_TRANSLATION_SUCCESS
;
732 memset(inq_response
, 0, STANDARD_INQUIRY_LENGTH
);
733 inq_response
[1] = INQ_SUPPORTED_VPD_PAGES_PAGE
; /* Page Code */
734 inq_response
[3] = INQ_NUM_SUPPORTED_VPD_PAGES
; /* Page Length */
735 inq_response
[4] = INQ_SUPPORTED_VPD_PAGES_PAGE
;
736 inq_response
[5] = INQ_UNIT_SERIAL_NUMBER_PAGE
;
737 inq_response
[6] = INQ_DEVICE_IDENTIFICATION_PAGE
;
738 inq_response
[7] = INQ_EXTENDED_INQUIRY_DATA_PAGE
;
739 inq_response
[8] = INQ_BDEV_CHARACTERISTICS_PAGE
;
741 xfer_len
= min(alloc_len
, STANDARD_INQUIRY_LENGTH
);
742 res
= nvme_trans_copy_to_user(hdr
, inq_response
, xfer_len
);
747 static int nvme_trans_unit_serial_page(struct nvme_ns
*ns
,
748 struct sg_io_hdr
*hdr
, u8
*inq_response
,
751 struct nvme_dev
*dev
= ns
->dev
;
752 int res
= SNTI_TRANSLATION_SUCCESS
;
755 memset(inq_response
, 0, STANDARD_INQUIRY_LENGTH
);
756 inq_response
[1] = INQ_UNIT_SERIAL_NUMBER_PAGE
; /* Page Code */
757 inq_response
[3] = INQ_SERIAL_NUMBER_LENGTH
; /* Page Length */
758 strncpy(&inq_response
[4], dev
->serial
, INQ_SERIAL_NUMBER_LENGTH
);
760 xfer_len
= min(alloc_len
, STANDARD_INQUIRY_LENGTH
);
761 res
= nvme_trans_copy_to_user(hdr
, inq_response
, xfer_len
);
766 static int nvme_trans_device_id_page(struct nvme_ns
*ns
, struct sg_io_hdr
*hdr
,
767 u8
*inq_response
, int alloc_len
)
769 struct nvme_dev
*dev
= ns
->dev
;
772 struct nvme_id_ctrl
*id_ctrl
;
773 int res
= SNTI_TRANSLATION_SUCCESS
;
777 u32 tmp_id
= cpu_to_be64(ns
->ns_id
);
779 mem
= dma_alloc_coherent(&dev
->pci_dev
->dev
, sizeof(struct nvme_id_ns
),
780 &dma_addr
, GFP_KERNEL
);
786 /* nvme controller identify */
787 nvme_sc
= nvme_identify(dev
, 0, 1, dma_addr
);
788 res
= nvme_trans_status_code(hdr
, nvme_sc
);
797 /* Since SCSI tried to save 4 bits... [SPC-4(r34) Table 591] */
798 ieee
[0] = id_ctrl
->ieee
[0] << 4;
799 ieee
[1] = id_ctrl
->ieee
[0] >> 4 | id_ctrl
->ieee
[1] << 4;
800 ieee
[2] = id_ctrl
->ieee
[1] >> 4 | id_ctrl
->ieee
[2] << 4;
801 ieee
[3] = id_ctrl
->ieee
[2] >> 4;
803 memset(inq_response
, 0, STANDARD_INQUIRY_LENGTH
);
804 inq_response
[1] = INQ_DEVICE_IDENTIFICATION_PAGE
; /* Page Code */
805 inq_response
[3] = 20; /* Page Length */
806 /* Designation Descriptor start */
807 inq_response
[4] = 0x01; /* Proto ID=0h | Code set=1h */
808 inq_response
[5] = 0x03; /* PIV=0b | Asso=00b | Designator Type=3h */
809 inq_response
[6] = 0x00; /* Rsvd */
810 inq_response
[7] = 16; /* Designator Length */
811 /* Designator start */
812 inq_response
[8] = 0x60 | ieee
[3]; /* NAA=6h | IEEE ID MSB, High nibble*/
813 inq_response
[9] = ieee
[2]; /* IEEE ID */
814 inq_response
[10] = ieee
[1]; /* IEEE ID */
815 inq_response
[11] = ieee
[0]; /* IEEE ID| Vendor Specific ID... */
816 inq_response
[12] = (dev
->pci_dev
->vendor
& 0xFF00) >> 8;
817 inq_response
[13] = (dev
->pci_dev
->vendor
& 0x00FF);
818 inq_response
[14] = dev
->serial
[0];
819 inq_response
[15] = dev
->serial
[1];
820 inq_response
[16] = dev
->model
[0];
821 inq_response
[17] = dev
->model
[1];
822 memcpy(&inq_response
[18], &tmp_id
, sizeof(u32
));
823 /* Last 2 bytes are zero */
825 xfer_len
= min(alloc_len
, STANDARD_INQUIRY_LENGTH
);
826 res
= nvme_trans_copy_to_user(hdr
, inq_response
, xfer_len
);
829 dma_free_coherent(&dev
->pci_dev
->dev
, sizeof(struct nvme_id_ns
), mem
,
835 static int nvme_trans_ext_inq_page(struct nvme_ns
*ns
, struct sg_io_hdr
*hdr
,
839 int res
= SNTI_TRANSLATION_SUCCESS
;
841 struct nvme_dev
*dev
= ns
->dev
;
844 struct nvme_id_ctrl
*id_ctrl
;
845 struct nvme_id_ns
*id_ns
;
849 u8 spt_lut
[8] = {0, 0, 2, 1, 4, 6, 5, 7};
850 u8 grd_chk
, app_chk
, ref_chk
, protect
;
855 inq_response
= kmalloc(EXTENDED_INQUIRY_DATA_PAGE_LENGTH
, GFP_KERNEL
);
856 if (inq_response
== NULL
) {
861 mem
= dma_alloc_coherent(&dev
->pci_dev
->dev
, sizeof(struct nvme_id_ns
),
862 &dma_addr
, GFP_KERNEL
);
868 /* nvme ns identify */
869 nvme_sc
= nvme_identify(dev
, ns
->ns_id
, 0, dma_addr
);
870 res
= nvme_trans_status_code(hdr
, nvme_sc
);
878 spt
= spt_lut
[(id_ns
->dpc
) & 0x07] << 3;
879 (id_ns
->dps
) ? (protect
= 0x01) : (protect
= 0);
880 grd_chk
= protect
<< 2;
881 app_chk
= protect
<< 1;
884 /* nvme controller identify */
885 nvme_sc
= nvme_identify(dev
, 0, 1, dma_addr
);
886 res
= nvme_trans_status_code(hdr
, nvme_sc
);
894 v_sup
= id_ctrl
->vwc
;
896 memset(inq_response
, 0, EXTENDED_INQUIRY_DATA_PAGE_LENGTH
);
897 inq_response
[1] = INQ_EXTENDED_INQUIRY_DATA_PAGE
; /* Page Code */
898 inq_response
[2] = 0x00; /* Page Length MSB */
899 inq_response
[3] = 0x3C; /* Page Length LSB */
900 inq_response
[4] = microcode
| spt
| grd_chk
| app_chk
| ref_chk
;
901 inq_response
[5] = uask_sup
;
902 inq_response
[6] = v_sup
;
903 inq_response
[7] = luiclr
;
907 xfer_len
= min(alloc_len
, EXTENDED_INQUIRY_DATA_PAGE_LENGTH
);
908 res
= nvme_trans_copy_to_user(hdr
, inq_response
, xfer_len
);
911 dma_free_coherent(&dev
->pci_dev
->dev
, sizeof(struct nvme_id_ns
), mem
,
919 static int nvme_trans_bdev_char_page(struct nvme_ns
*ns
, struct sg_io_hdr
*hdr
,
923 int res
= SNTI_TRANSLATION_SUCCESS
;
926 inq_response
= kmalloc(EXTENDED_INQUIRY_DATA_PAGE_LENGTH
, GFP_KERNEL
);
927 if (inq_response
== NULL
) {
932 memset(inq_response
, 0, EXTENDED_INQUIRY_DATA_PAGE_LENGTH
);
933 inq_response
[1] = INQ_BDEV_CHARACTERISTICS_PAGE
; /* Page Code */
934 inq_response
[2] = 0x00; /* Page Length MSB */
935 inq_response
[3] = 0x3C; /* Page Length LSB */
936 inq_response
[4] = 0x00; /* Medium Rotation Rate MSB */
937 inq_response
[5] = 0x01; /* Medium Rotation Rate LSB */
938 inq_response
[6] = 0x00; /* Form Factor */
940 xfer_len
= min(alloc_len
, EXTENDED_INQUIRY_DATA_PAGE_LENGTH
);
941 res
= nvme_trans_copy_to_user(hdr
, inq_response
, xfer_len
);
948 /* LOG SENSE Helper Functions */
950 static int nvme_trans_log_supp_pages(struct nvme_ns
*ns
, struct sg_io_hdr
*hdr
,
953 int res
= SNTI_TRANSLATION_SUCCESS
;
957 log_response
= kmalloc(LOG_PAGE_SUPPORTED_LOG_PAGES_LENGTH
, GFP_KERNEL
);
958 if (log_response
== NULL
) {
962 memset(log_response
, 0, LOG_PAGE_SUPPORTED_LOG_PAGES_LENGTH
);
964 log_response
[0] = LOG_PAGE_SUPPORTED_LOG_PAGES_PAGE
;
965 /* Subpage=0x00, Page Length MSB=0 */
966 log_response
[3] = SUPPORTED_LOG_PAGES_PAGE_LENGTH
;
967 log_response
[4] = LOG_PAGE_SUPPORTED_LOG_PAGES_PAGE
;
968 log_response
[5] = LOG_PAGE_INFORMATIONAL_EXCEPTIONS_PAGE
;
969 log_response
[6] = LOG_PAGE_TEMPERATURE_PAGE
;
971 xfer_len
= min(alloc_len
, LOG_PAGE_SUPPORTED_LOG_PAGES_LENGTH
);
972 res
= nvme_trans_copy_to_user(hdr
, log_response
, xfer_len
);
979 static int nvme_trans_log_info_exceptions(struct nvme_ns
*ns
,
980 struct sg_io_hdr
*hdr
, int alloc_len
)
982 int res
= SNTI_TRANSLATION_SUCCESS
;
985 struct nvme_command c
;
986 struct nvme_dev
*dev
= ns
->dev
;
987 struct nvme_smart_log
*smart_log
;
993 log_response
= kmalloc(LOG_INFO_EXCP_PAGE_LENGTH
, GFP_KERNEL
);
994 if (log_response
== NULL
) {
998 memset(log_response
, 0, LOG_INFO_EXCP_PAGE_LENGTH
);
1000 mem
= dma_alloc_coherent(&dev
->pci_dev
->dev
,
1001 sizeof(struct nvme_smart_log
),
1002 &dma_addr
, GFP_KERNEL
);
1008 /* Get SMART Log Page */
1009 memset(&c
, 0, sizeof(c
));
1010 c
.common
.opcode
= nvme_admin_get_log_page
;
1011 c
.common
.nsid
= cpu_to_le32(0xFFFFFFFF);
1012 c
.common
.prp1
= cpu_to_le64(dma_addr
);
1013 c
.common
.cdw10
[0] = cpu_to_le32(((sizeof(struct nvme_smart_log
) /
1014 BYTES_TO_DWORDS
) << 16) | NVME_GET_SMART_LOG_PAGE
);
1015 res
= nvme_submit_admin_cmd(dev
, &c
, NULL
);
1016 if (res
!= NVME_SC_SUCCESS
) {
1017 temp_c
= LOG_TEMP_UNKNOWN
;
1020 temp_k
= (smart_log
->temperature
[1] << 8) +
1021 (smart_log
->temperature
[0]);
1022 temp_c
= temp_k
- KELVIN_TEMP_FACTOR
;
1025 log_response
[0] = LOG_PAGE_INFORMATIONAL_EXCEPTIONS_PAGE
;
1026 /* Subpage=0x00, Page Length MSB=0 */
1027 log_response
[3] = REMAINING_INFO_EXCP_PAGE_LENGTH
;
1028 /* Informational Exceptions Log Parameter 1 Start */
1029 /* Parameter Code=0x0000 bytes 4,5 */
1030 log_response
[6] = 0x23; /* DU=0, TSD=1, ETC=0, TMC=0, FMT_AND_LNK=11b */
1031 log_response
[7] = 0x04; /* PARAMETER LENGTH */
1032 /* Add sense Code and qualifier = 0x00 each */
1033 /* Use Temperature from NVMe Get Log Page, convert to C from K */
1034 log_response
[10] = temp_c
;
1036 xfer_len
= min(alloc_len
, LOG_INFO_EXCP_PAGE_LENGTH
);
1037 res
= nvme_trans_copy_to_user(hdr
, log_response
, xfer_len
);
1039 dma_free_coherent(&dev
->pci_dev
->dev
, sizeof(struct nvme_smart_log
),
1042 kfree(log_response
);
1047 static int nvme_trans_log_temperature(struct nvme_ns
*ns
, struct sg_io_hdr
*hdr
,
1050 int res
= SNTI_TRANSLATION_SUCCESS
;
1053 struct nvme_command c
;
1054 struct nvme_dev
*dev
= ns
->dev
;
1055 struct nvme_smart_log
*smart_log
;
1056 dma_addr_t dma_addr
;
1059 u8 temp_c_cur
, temp_c_thresh
;
1062 log_response
= kmalloc(LOG_TEMP_PAGE_LENGTH
, GFP_KERNEL
);
1063 if (log_response
== NULL
) {
1067 memset(log_response
, 0, LOG_TEMP_PAGE_LENGTH
);
1069 mem
= dma_alloc_coherent(&dev
->pci_dev
->dev
,
1070 sizeof(struct nvme_smart_log
),
1071 &dma_addr
, GFP_KERNEL
);
1077 /* Get SMART Log Page */
1078 memset(&c
, 0, sizeof(c
));
1079 c
.common
.opcode
= nvme_admin_get_log_page
;
1080 c
.common
.nsid
= cpu_to_le32(0xFFFFFFFF);
1081 c
.common
.prp1
= cpu_to_le64(dma_addr
);
1082 c
.common
.cdw10
[0] = cpu_to_le32(((sizeof(struct nvme_smart_log
) /
1083 BYTES_TO_DWORDS
) << 16) | NVME_GET_SMART_LOG_PAGE
);
1084 res
= nvme_submit_admin_cmd(dev
, &c
, NULL
);
1085 if (res
!= NVME_SC_SUCCESS
) {
1086 temp_c_cur
= LOG_TEMP_UNKNOWN
;
1089 temp_k
= (smart_log
->temperature
[1] << 8) +
1090 (smart_log
->temperature
[0]);
1091 temp_c_cur
= temp_k
- KELVIN_TEMP_FACTOR
;
1094 /* Get Features for Temp Threshold */
1095 res
= nvme_get_features(dev
, NVME_FEAT_TEMP_THRESH
, 0, 0,
1097 if (res
!= NVME_SC_SUCCESS
)
1098 temp_c_thresh
= LOG_TEMP_UNKNOWN
;
1100 temp_c_thresh
= (feature_resp
& 0xFFFF) - KELVIN_TEMP_FACTOR
;
1102 log_response
[0] = LOG_PAGE_TEMPERATURE_PAGE
;
1103 /* Subpage=0x00, Page Length MSB=0 */
1104 log_response
[3] = REMAINING_TEMP_PAGE_LENGTH
;
1105 /* Temperature Log Parameter 1 (Temperature) Start */
1106 /* Parameter Code = 0x0000 */
1107 log_response
[6] = 0x01; /* Format and Linking = 01b */
1108 log_response
[7] = 0x02; /* Parameter Length */
1109 /* Use Temperature from NVMe Get Log Page, convert to C from K */
1110 log_response
[9] = temp_c_cur
;
1111 /* Temperature Log Parameter 2 (Reference Temperature) Start */
1112 log_response
[11] = 0x01; /* Parameter Code = 0x0001 */
1113 log_response
[12] = 0x01; /* Format and Linking = 01b */
1114 log_response
[13] = 0x02; /* Parameter Length */
1115 /* Use Temperature Thresh from NVMe Get Log Page, convert to C from K */
1116 log_response
[15] = temp_c_thresh
;
1118 xfer_len
= min(alloc_len
, LOG_TEMP_PAGE_LENGTH
);
1119 res
= nvme_trans_copy_to_user(hdr
, log_response
, xfer_len
);
1121 dma_free_coherent(&dev
->pci_dev
->dev
, sizeof(struct nvme_smart_log
),
1124 kfree(log_response
);
1129 /* MODE SENSE Helper Functions */
1131 static int nvme_trans_fill_mode_parm_hdr(u8
*resp
, int len
, u8 cdb10
, u8 llbaa
,
1132 u16 mode_data_length
, u16 blk_desc_len
)
1134 /* Quick check to make sure I don't stomp on my own memory... */
1135 if ((cdb10
&& len
< 8) || (!cdb10
&& len
< 4))
1136 return SNTI_INTERNAL_ERROR
;
1139 resp
[0] = (mode_data_length
& 0xFF00) >> 8;
1140 resp
[1] = (mode_data_length
& 0x00FF);
1141 /* resp[2] and [3] are zero */
1143 resp
[5] = RESERVED_FIELD
;
1144 resp
[6] = (blk_desc_len
& 0xFF00) >> 8;
1145 resp
[7] = (blk_desc_len
& 0x00FF);
1147 resp
[0] = (mode_data_length
& 0x00FF);
1148 /* resp[1] and [2] are zero */
1149 resp
[3] = (blk_desc_len
& 0x00FF);
1152 return SNTI_TRANSLATION_SUCCESS
;
1155 static int nvme_trans_fill_blk_desc(struct nvme_ns
*ns
, struct sg_io_hdr
*hdr
,
1156 u8
*resp
, int len
, u8 llbaa
)
1158 int res
= SNTI_TRANSLATION_SUCCESS
;
1160 struct nvme_dev
*dev
= ns
->dev
;
1161 dma_addr_t dma_addr
;
1163 struct nvme_id_ns
*id_ns
;
1167 if (llbaa
== 0 && len
< MODE_PAGE_BLK_DES_LEN
)
1168 return SNTI_INTERNAL_ERROR
;
1169 else if (llbaa
> 0 && len
< MODE_PAGE_LLBAA_BLK_DES_LEN
)
1170 return SNTI_INTERNAL_ERROR
;
1172 mem
= dma_alloc_coherent(&dev
->pci_dev
->dev
, sizeof(struct nvme_id_ns
),
1173 &dma_addr
, GFP_KERNEL
);
1179 /* nvme ns identify */
1180 nvme_sc
= nvme_identify(dev
, ns
->ns_id
, 0, dma_addr
);
1181 res
= nvme_trans_status_code(hdr
, nvme_sc
);
1189 flbas
= (id_ns
->flbas
) & 0x0F;
1190 lba_length
= (1 << (id_ns
->lbaf
[flbas
].ds
));
1193 u32 tmp_cap
= cpu_to_be32(id_ns
->ncap
);
1194 /* Byte 4 is reserved */
1195 u32 tmp_len
= cpu_to_be32(lba_length
) & 0x00FFFFFF;
1197 memcpy(resp
, &tmp_cap
, sizeof(u32
));
1198 memcpy(&resp
[4], &tmp_len
, sizeof(u32
));
1200 u64 tmp_cap
= cpu_to_be64(id_ns
->ncap
);
1201 u32 tmp_len
= cpu_to_be32(lba_length
);
1203 memcpy(resp
, &tmp_cap
, sizeof(u64
));
1204 /* Bytes 8, 9, 10, 11 are reserved */
1205 memcpy(&resp
[12], &tmp_len
, sizeof(u32
));
1209 dma_free_coherent(&dev
->pci_dev
->dev
, sizeof(struct nvme_id_ns
), mem
,
1215 static int nvme_trans_fill_control_page(struct nvme_ns
*ns
,
1216 struct sg_io_hdr
*hdr
, u8
*resp
,
1219 if (len
< MODE_PAGE_CONTROL_LEN
)
1220 return SNTI_INTERNAL_ERROR
;
1222 resp
[0] = MODE_PAGE_CONTROL
;
1223 resp
[1] = MODE_PAGE_CONTROL_LEN_FIELD
;
1224 resp
[2] = 0x0E; /* TST=000b, TMF_ONLY=0, DPICZ=1,
1225 * D_SENSE=1, GLTSD=1, RLEC=0 */
1226 resp
[3] = 0x12; /* Q_ALGO_MODIFIER=1h, NUAR=0, QERR=01b */
1227 /* Byte 4: VS=0, RAC=0, UA_INT=0, SWP=0 */
1228 resp
[5] = 0x40; /* ATO=0, TAS=1, ATMPE=0, RWWP=0, AUTOLOAD=0 */
1229 /* resp[6] and [7] are obsolete, thus zero */
1230 resp
[8] = 0xFF; /* Busy timeout period = 0xffff */
1232 /* Bytes 10,11: Extended selftest completion time = 0x0000 */
1234 return SNTI_TRANSLATION_SUCCESS
;
1237 static int nvme_trans_fill_caching_page(struct nvme_ns
*ns
,
1238 struct sg_io_hdr
*hdr
,
1241 int res
= SNTI_TRANSLATION_SUCCESS
;
1243 struct nvme_dev
*dev
= ns
->dev
;
1247 if (len
< MODE_PAGE_CACHING_LEN
)
1248 return SNTI_INTERNAL_ERROR
;
1250 nvme_sc
= nvme_get_features(dev
, NVME_FEAT_VOLATILE_WC
, 0, 0,
1252 res
= nvme_trans_status_code(hdr
, nvme_sc
);
1259 vwc
= feature_resp
& 0x00000001;
1261 resp
[0] = MODE_PAGE_CACHING
;
1262 resp
[1] = MODE_PAGE_CACHING_LEN_FIELD
;
1269 static int nvme_trans_fill_pow_cnd_page(struct nvme_ns
*ns
,
1270 struct sg_io_hdr
*hdr
, u8
*resp
,
1273 int res
= SNTI_TRANSLATION_SUCCESS
;
1275 if (len
< MODE_PAGE_POW_CND_LEN
)
1276 return SNTI_INTERNAL_ERROR
;
1278 resp
[0] = MODE_PAGE_POWER_CONDITION
;
1279 resp
[1] = MODE_PAGE_POW_CND_LEN_FIELD
;
1280 /* All other bytes are zero */
1285 static int nvme_trans_fill_inf_exc_page(struct nvme_ns
*ns
,
1286 struct sg_io_hdr
*hdr
, u8
*resp
,
1289 int res
= SNTI_TRANSLATION_SUCCESS
;
1291 if (len
< MODE_PAGE_INF_EXC_LEN
)
1292 return SNTI_INTERNAL_ERROR
;
1294 resp
[0] = MODE_PAGE_INFO_EXCEP
;
1295 resp
[1] = MODE_PAGE_INF_EXC_LEN_FIELD
;
1297 /* All other bytes are zero */
1302 static int nvme_trans_fill_all_pages(struct nvme_ns
*ns
, struct sg_io_hdr
*hdr
,
1305 int res
= SNTI_TRANSLATION_SUCCESS
;
1306 u16 mode_pages_offset_1
= 0;
1307 u16 mode_pages_offset_2
, mode_pages_offset_3
, mode_pages_offset_4
;
1309 mode_pages_offset_2
= mode_pages_offset_1
+ MODE_PAGE_CACHING_LEN
;
1310 mode_pages_offset_3
= mode_pages_offset_2
+ MODE_PAGE_CONTROL_LEN
;
1311 mode_pages_offset_4
= mode_pages_offset_3
+ MODE_PAGE_POW_CND_LEN
;
1313 res
= nvme_trans_fill_caching_page(ns
, hdr
, &resp
[mode_pages_offset_1
],
1314 MODE_PAGE_CACHING_LEN
);
1315 if (res
!= SNTI_TRANSLATION_SUCCESS
)
1317 res
= nvme_trans_fill_control_page(ns
, hdr
, &resp
[mode_pages_offset_2
],
1318 MODE_PAGE_CONTROL_LEN
);
1319 if (res
!= SNTI_TRANSLATION_SUCCESS
)
1321 res
= nvme_trans_fill_pow_cnd_page(ns
, hdr
, &resp
[mode_pages_offset_3
],
1322 MODE_PAGE_POW_CND_LEN
);
1323 if (res
!= SNTI_TRANSLATION_SUCCESS
)
1325 res
= nvme_trans_fill_inf_exc_page(ns
, hdr
, &resp
[mode_pages_offset_4
],
1326 MODE_PAGE_INF_EXC_LEN
);
1327 if (res
!= SNTI_TRANSLATION_SUCCESS
)
1334 static inline int nvme_trans_get_blk_desc_len(u8 dbd
, u8 llbaa
)
1336 if (dbd
== MODE_SENSE_BLK_DESC_ENABLED
) {
1337 /* SPC-4: len = 8 x Num_of_descriptors if llbaa = 0, 16x if 1 */
1338 return 8 * (llbaa
+ 1) * MODE_SENSE_BLK_DESC_COUNT
;
1344 static int nvme_trans_mode_page_create(struct nvme_ns
*ns
,
1345 struct sg_io_hdr
*hdr
, u8
*cmd
,
1346 u16 alloc_len
, u8 cdb10
,
1347 int (*mode_page_fill_func
)
1349 struct sg_io_hdr
*hdr
, u8
*, int),
1350 u16 mode_pages_tot_len
)
1352 int res
= SNTI_TRANSLATION_SUCCESS
;
1358 u16 mode_pages_offset_1
;
1359 u16 blk_desc_len
, blk_desc_offset
, mode_data_length
;
1361 dbd
= GET_MODE_SENSE_DBD(cmd
);
1362 llbaa
= GET_MODE_SENSE_LLBAA(cmd
);
1363 mph_size
= GET_MODE_SENSE_MPH_SIZE(cdb10
);
1364 blk_desc_len
= nvme_trans_get_blk_desc_len(dbd
, llbaa
);
1366 resp_size
= mph_size
+ blk_desc_len
+ mode_pages_tot_len
;
1367 /* Refer spc4r34 Table 440 for calculation of Mode data Length field */
1368 mode_data_length
= 3 + (3 * cdb10
) + blk_desc_len
+ mode_pages_tot_len
;
1370 blk_desc_offset
= mph_size
;
1371 mode_pages_offset_1
= blk_desc_offset
+ blk_desc_len
;
1373 response
= kmalloc(resp_size
, GFP_KERNEL
);
1374 if (response
== NULL
) {
1378 memset(response
, 0, resp_size
);
1380 res
= nvme_trans_fill_mode_parm_hdr(&response
[0], mph_size
, cdb10
,
1381 llbaa
, mode_data_length
, blk_desc_len
);
1382 if (res
!= SNTI_TRANSLATION_SUCCESS
)
1384 if (blk_desc_len
> 0) {
1385 res
= nvme_trans_fill_blk_desc(ns
, hdr
,
1386 &response
[blk_desc_offset
],
1387 blk_desc_len
, llbaa
);
1388 if (res
!= SNTI_TRANSLATION_SUCCESS
)
1391 res
= mode_page_fill_func(ns
, hdr
, &response
[mode_pages_offset_1
],
1392 mode_pages_tot_len
);
1393 if (res
!= SNTI_TRANSLATION_SUCCESS
)
1396 xfer_len
= min(alloc_len
, resp_size
);
1397 res
= nvme_trans_copy_to_user(hdr
, response
, xfer_len
);
1405 /* Read Capacity Helper Functions */
1407 static void nvme_trans_fill_read_cap(u8
*response
, struct nvme_id_ns
*id_ns
,
1414 u8 p_type_lut
[4] = {0, 0, 1, 2};
1419 flbas
= (id_ns
->flbas
) & 0x0F;
1420 lba_length
= (1 << (id_ns
->lbaf
[flbas
].ds
));
1421 rlba
= le64_to_cpup(&id_ns
->nsze
) - 1;
1422 (id_ns
->dps
) ? (prot_en
= 0x01) : (prot_en
= 0);
1425 if (rlba
> 0xFFFFFFFF)
1427 tmp_rlba_32
= cpu_to_be32(rlba
);
1428 tmp_len
= cpu_to_be32(lba_length
);
1429 memcpy(response
, &tmp_rlba_32
, sizeof(u32
));
1430 memcpy(&response
[4], &tmp_len
, sizeof(u32
));
1432 tmp_rlba
= cpu_to_be64(rlba
);
1433 tmp_len
= cpu_to_be32(lba_length
);
1434 memcpy(response
, &tmp_rlba
, sizeof(u64
));
1435 memcpy(&response
[8], &tmp_len
, sizeof(u32
));
1436 response
[12] = (p_type_lut
[id_ns
->dps
& 0x3] << 1) | prot_en
;
1437 /* P_I_Exponent = 0x0 | LBPPBE = 0x0 */
1438 /* LBPME = 0 | LBPRZ = 0 | LALBA = 0x00 */
1439 /* Bytes 16-31 - Reserved */
1443 /* Start Stop Unit Helper Functions */
1445 static int nvme_trans_power_state(struct nvme_ns
*ns
, struct sg_io_hdr
*hdr
,
1446 u8 pc
, u8 pcmod
, u8 start
)
1448 int res
= SNTI_TRANSLATION_SUCCESS
;
1450 struct nvme_dev
*dev
= ns
->dev
;
1451 dma_addr_t dma_addr
;
1453 struct nvme_id_ctrl
*id_ctrl
;
1454 int lowest_pow_st
; /* max npss = lowest power consumption */
1455 unsigned ps_desired
= 0;
1457 /* NVMe Controller Identify */
1458 mem
= dma_alloc_coherent(&dev
->pci_dev
->dev
,
1459 sizeof(struct nvme_id_ctrl
),
1460 &dma_addr
, GFP_KERNEL
);
1465 nvme_sc
= nvme_identify(dev
, 0, 1, dma_addr
);
1466 res
= nvme_trans_status_code(hdr
, nvme_sc
);
1474 lowest_pow_st
= id_ctrl
->npss
- 1;
1477 case NVME_POWER_STATE_START_VALID
:
1478 /* Action unspecified if POWER CONDITION MODIFIER != 0 */
1479 if (pcmod
== 0 && start
== 0x1)
1480 ps_desired
= POWER_STATE_0
;
1481 if (pcmod
== 0 && start
== 0x0)
1482 ps_desired
= lowest_pow_st
;
1484 case NVME_POWER_STATE_ACTIVE
:
1485 /* Action unspecified if POWER CONDITION MODIFIER != 0 */
1487 ps_desired
= POWER_STATE_0
;
1489 case NVME_POWER_STATE_IDLE
:
1490 /* Action unspecified if POWER CONDITION MODIFIER != [0,1,2] */
1491 /* min of desired state and (lps-1) because lps is STOP */
1493 ps_desired
= min(POWER_STATE_1
, (lowest_pow_st
- 1));
1494 else if (pcmod
== 0x1)
1495 ps_desired
= min(POWER_STATE_2
, (lowest_pow_st
- 1));
1496 else if (pcmod
== 0x2)
1497 ps_desired
= min(POWER_STATE_3
, (lowest_pow_st
- 1));
1499 case NVME_POWER_STATE_STANDBY
:
1500 /* Action unspecified if POWER CONDITION MODIFIER != [0,1] */
1502 ps_desired
= max(0, (lowest_pow_st
- 2));
1503 else if (pcmod
== 0x1)
1504 ps_desired
= max(0, (lowest_pow_st
- 1));
1506 case NVME_POWER_STATE_LU_CONTROL
:
1508 res
= nvme_trans_completion(hdr
, SAM_STAT_CHECK_CONDITION
,
1509 ILLEGAL_REQUEST
, SCSI_ASC_INVALID_CDB
,
1510 SCSI_ASCQ_CAUSE_NOT_REPORTABLE
);
1513 nvme_sc
= nvme_set_features(dev
, NVME_FEAT_POWER_MGMT
, ps_desired
, 0,
1515 res
= nvme_trans_status_code(hdr
, nvme_sc
);
1521 dma_free_coherent(&dev
->pci_dev
->dev
, sizeof(struct nvme_id_ctrl
), mem
,
1527 /* Write Buffer Helper Functions */
1528 /* Also using this for Format Unit with hdr passed as NULL, and buffer_id, 0 */
1530 static int nvme_trans_send_fw_cmd(struct nvme_ns
*ns
, struct sg_io_hdr
*hdr
,
1531 u8 opcode
, u32 tot_len
, u32 offset
,
1534 int res
= SNTI_TRANSLATION_SUCCESS
;
1536 struct nvme_dev
*dev
= ns
->dev
;
1537 struct nvme_command c
;
1538 struct nvme_iod
*iod
= NULL
;
1541 memset(&c
, 0, sizeof(c
));
1542 c
.common
.opcode
= opcode
;
1543 if (opcode
== nvme_admin_download_fw
) {
1544 if (hdr
->iovec_count
> 0) {
1545 /* Assuming SGL is not allowed for this command */
1546 res
= nvme_trans_completion(hdr
,
1547 SAM_STAT_CHECK_CONDITION
,
1549 SCSI_ASC_INVALID_CDB
,
1550 SCSI_ASCQ_CAUSE_NOT_REPORTABLE
);
1553 iod
= nvme_map_user_pages(dev
, DMA_TO_DEVICE
,
1554 (unsigned long)hdr
->dxferp
, tot_len
);
1559 length
= nvme_setup_prps(dev
, &c
.common
, iod
, tot_len
,
1561 if (length
!= tot_len
) {
1566 c
.dlfw
.numd
= (tot_len
/BYTES_TO_DWORDS
) - 1;
1567 c
.dlfw
.offset
= offset
/BYTES_TO_DWORDS
;
1568 } else if (opcode
== nvme_admin_activate_fw
) {
1569 c
.common
.cdw10
[0] = buffer_id
;
1570 /* AA=01b Replace & activate at reset */
1571 c
.common
.cdw10
[0] |= 0x00000008;
1574 nvme_sc
= nvme_submit_admin_cmd(dev
, &c
, NULL
);
1575 res
= nvme_trans_status_code(hdr
, nvme_sc
);
1582 if (opcode
== nvme_admin_download_fw
) {
1583 nvme_unmap_user_pages(dev
, DMA_TO_DEVICE
, iod
);
1584 nvme_free_iod(dev
, iod
);
1590 /* Mode Select Helper Functions */
1592 static inline void nvme_trans_modesel_get_bd_len(u8
*parm_list
, u8 cdb10
,
1593 u16
*bd_len
, u8
*llbaa
)
1597 *bd_len
= (parm_list
[MODE_SELECT_10_BD_OFFSET
] << 8) +
1598 parm_list
[MODE_SELECT_10_BD_OFFSET
+ 1];
1599 *llbaa
= parm_list
[MODE_SELECT_10_LLBAA_OFFSET
] &&
1600 MODE_SELECT_10_LLBAA_MASK
;
1603 *bd_len
= parm_list
[MODE_SELECT_6_BD_OFFSET
];
1607 static void nvme_trans_modesel_save_bd(struct nvme_ns
*ns
, u8
*parm_list
,
1608 u16 idx
, u16 bd_len
, u8 llbaa
)
1612 bd_num
= bd_len
/ ((llbaa
== 0) ?
1613 SHORT_DESC_BLOCK
: LONG_DESC_BLOCK
);
1614 /* Store block descriptor info if a FORMAT UNIT comes later */
1615 /* TODO Saving 1st BD info; what to do if multiple BD received? */
1617 /* Standard Block Descriptor - spc4r34 7.5.5.1 */
1618 ns
->mode_select_num_blocks
=
1619 (parm_list
[idx
+ 1] << 16) +
1620 (parm_list
[idx
+ 2] << 8) +
1621 (parm_list
[idx
+ 3]);
1623 ns
->mode_select_block_len
=
1624 (parm_list
[idx
+ 5] << 16) +
1625 (parm_list
[idx
+ 6] << 8) +
1626 (parm_list
[idx
+ 7]);
1628 /* Long LBA Block Descriptor - sbc3r27 6.4.2.3 */
1629 ns
->mode_select_num_blocks
=
1630 (((u64
)parm_list
[idx
+ 0]) << 56) +
1631 (((u64
)parm_list
[idx
+ 1]) << 48) +
1632 (((u64
)parm_list
[idx
+ 2]) << 40) +
1633 (((u64
)parm_list
[idx
+ 3]) << 32) +
1634 (((u64
)parm_list
[idx
+ 4]) << 24) +
1635 (((u64
)parm_list
[idx
+ 5]) << 16) +
1636 (((u64
)parm_list
[idx
+ 6]) << 8) +
1637 ((u64
)parm_list
[idx
+ 7]);
1639 ns
->mode_select_block_len
=
1640 (parm_list
[idx
+ 12] << 24) +
1641 (parm_list
[idx
+ 13] << 16) +
1642 (parm_list
[idx
+ 14] << 8) +
1643 (parm_list
[idx
+ 15]);
1647 static u16
nvme_trans_modesel_get_mp(struct nvme_ns
*ns
, struct sg_io_hdr
*hdr
,
1648 u8
*mode_page
, u8 page_code
)
1650 int res
= SNTI_TRANSLATION_SUCCESS
;
1652 struct nvme_dev
*dev
= ns
->dev
;
1655 switch (page_code
) {
1656 case MODE_PAGE_CACHING
:
1657 dword11
= ((mode_page
[2] & CACHING_MODE_PAGE_WCE_MASK
) ? 1 : 0);
1658 nvme_sc
= nvme_set_features(dev
, NVME_FEAT_VOLATILE_WC
, dword11
,
1660 res
= nvme_trans_status_code(hdr
, nvme_sc
);
1668 case MODE_PAGE_CONTROL
:
1670 case MODE_PAGE_POWER_CONDITION
:
1671 /* Verify the OS is not trying to set timers */
1672 if ((mode_page
[2] & 0x01) != 0 || (mode_page
[3] & 0x0F) != 0) {
1673 res
= nvme_trans_completion(hdr
,
1674 SAM_STAT_CHECK_CONDITION
,
1676 SCSI_ASC_INVALID_PARAMETER
,
1677 SCSI_ASCQ_CAUSE_NOT_REPORTABLE
);
1679 res
= SNTI_INTERNAL_ERROR
;
1684 res
= nvme_trans_completion(hdr
, SAM_STAT_CHECK_CONDITION
,
1685 ILLEGAL_REQUEST
, SCSI_ASC_INVALID_CDB
,
1686 SCSI_ASCQ_CAUSE_NOT_REPORTABLE
);
1688 res
= SNTI_INTERNAL_ERROR
;
1695 static int nvme_trans_modesel_data(struct nvme_ns
*ns
, struct sg_io_hdr
*hdr
,
1696 u8
*cmd
, u16 parm_list_len
, u8 pf
,
1699 int res
= SNTI_TRANSLATION_SUCCESS
;
1703 u16 index
, saved_index
;
1707 /* Get parm list from data-in/out buffer */
1708 parm_list
= kmalloc(parm_list_len
, GFP_KERNEL
);
1709 if (parm_list
== NULL
) {
1714 res
= nvme_trans_copy_from_user(hdr
, parm_list
, parm_list_len
);
1715 if (res
!= SNTI_TRANSLATION_SUCCESS
)
1718 nvme_trans_modesel_get_bd_len(parm_list
, cdb10
, &bd_len
, &llbaa
);
1719 index
= (cdb10
) ? (MODE_SELECT_10_MPH_SIZE
) : (MODE_SELECT_6_MPH_SIZE
);
1722 /* Block Descriptors present, parse */
1723 nvme_trans_modesel_save_bd(ns
, parm_list
, index
, bd_len
, llbaa
);
1726 saved_index
= index
;
1728 /* Multiple mode pages may be present; iterate through all */
1729 /* In 1st Iteration, don't do NVME Command, only check for CDB errors */
1731 page_code
= parm_list
[index
] & MODE_SELECT_PAGE_CODE_MASK
;
1732 mp_size
= parm_list
[index
+ 1] + 2;
1733 if ((page_code
!= MODE_PAGE_CACHING
) &&
1734 (page_code
!= MODE_PAGE_CONTROL
) &&
1735 (page_code
!= MODE_PAGE_POWER_CONDITION
)) {
1736 res
= nvme_trans_completion(hdr
,
1737 SAM_STAT_CHECK_CONDITION
,
1739 SCSI_ASC_INVALID_CDB
,
1740 SCSI_ASCQ_CAUSE_NOT_REPORTABLE
);
1744 } while (index
< parm_list_len
);
1746 /* In 2nd Iteration, do the NVME Commands */
1747 index
= saved_index
;
1749 page_code
= parm_list
[index
] & MODE_SELECT_PAGE_CODE_MASK
;
1750 mp_size
= parm_list
[index
+ 1] + 2;
1751 res
= nvme_trans_modesel_get_mp(ns
, hdr
, &parm_list
[index
],
1753 if (res
!= SNTI_TRANSLATION_SUCCESS
)
1756 } while (index
< parm_list_len
);
1764 /* Format Unit Helper Functions */
1766 static int nvme_trans_fmt_set_blk_size_count(struct nvme_ns
*ns
,
1767 struct sg_io_hdr
*hdr
)
1769 int res
= SNTI_TRANSLATION_SUCCESS
;
1771 struct nvme_dev
*dev
= ns
->dev
;
1772 dma_addr_t dma_addr
;
1774 struct nvme_id_ns
*id_ns
;
1778 * SCSI Expects a MODE SELECT would have been issued prior to
1779 * a FORMAT UNIT, and the block size and number would be used
1780 * from the block descriptor in it. If a MODE SELECT had not
1781 * been issued, FORMAT shall use the current values for both.
1784 if (ns
->mode_select_num_blocks
== 0 || ns
->mode_select_block_len
== 0) {
1785 mem
= dma_alloc_coherent(&dev
->pci_dev
->dev
,
1786 sizeof(struct nvme_id_ns
), &dma_addr
, GFP_KERNEL
);
1791 /* nvme ns identify */
1792 nvme_sc
= nvme_identify(dev
, ns
->ns_id
, 0, dma_addr
);
1793 res
= nvme_trans_status_code(hdr
, nvme_sc
);
1802 if (ns
->mode_select_num_blocks
== 0)
1803 ns
->mode_select_num_blocks
= id_ns
->ncap
;
1804 if (ns
->mode_select_block_len
== 0) {
1805 flbas
= (id_ns
->flbas
) & 0x0F;
1806 ns
->mode_select_block_len
=
1807 (1 << (id_ns
->lbaf
[flbas
].ds
));
1810 dma_free_coherent(&dev
->pci_dev
->dev
, sizeof(struct nvme_id_ns
),
1817 static int nvme_trans_fmt_get_parm_header(struct sg_io_hdr
*hdr
, u8 len
,
1818 u8 format_prot_info
, u8
*nvme_pf_code
)
1820 int res
= SNTI_TRANSLATION_SUCCESS
;
1822 u8 pf_usage
, pf_code
;
1824 parm_list
= kmalloc(len
, GFP_KERNEL
);
1825 if (parm_list
== NULL
) {
1829 res
= nvme_trans_copy_from_user(hdr
, parm_list
, len
);
1830 if (res
!= SNTI_TRANSLATION_SUCCESS
)
1833 if ((parm_list
[FORMAT_UNIT_IMMED_OFFSET
] &
1834 FORMAT_UNIT_IMMED_MASK
) != 0) {
1835 res
= nvme_trans_completion(hdr
, SAM_STAT_CHECK_CONDITION
,
1836 ILLEGAL_REQUEST
, SCSI_ASC_INVALID_CDB
,
1837 SCSI_ASCQ_CAUSE_NOT_REPORTABLE
);
1841 if (len
== FORMAT_UNIT_LONG_PARM_LIST_LEN
&&
1842 (parm_list
[FORMAT_UNIT_PROT_INT_OFFSET
] & 0x0F) != 0) {
1843 res
= nvme_trans_completion(hdr
, SAM_STAT_CHECK_CONDITION
,
1844 ILLEGAL_REQUEST
, SCSI_ASC_INVALID_CDB
,
1845 SCSI_ASCQ_CAUSE_NOT_REPORTABLE
);
1848 pf_usage
= parm_list
[FORMAT_UNIT_PROT_FIELD_USAGE_OFFSET
] &
1849 FORMAT_UNIT_PROT_FIELD_USAGE_MASK
;
1850 pf_code
= (pf_usage
<< 2) | format_prot_info
;
1865 res
= nvme_trans_completion(hdr
, SAM_STAT_CHECK_CONDITION
,
1866 ILLEGAL_REQUEST
, SCSI_ASC_INVALID_CDB
,
1867 SCSI_ASCQ_CAUSE_NOT_REPORTABLE
);
1877 static int nvme_trans_fmt_send_cmd(struct nvme_ns
*ns
, struct sg_io_hdr
*hdr
,
1880 int res
= SNTI_TRANSLATION_SUCCESS
;
1882 struct nvme_dev
*dev
= ns
->dev
;
1883 dma_addr_t dma_addr
;
1885 struct nvme_id_ns
*id_ns
;
1888 u8 selected_lbaf
= 0xFF;
1890 struct nvme_command c
;
1892 /* Loop thru LBAF's in id_ns to match reqd lbaf, put in cdw10 */
1893 mem
= dma_alloc_coherent(&dev
->pci_dev
->dev
, sizeof(struct nvme_id_ns
),
1894 &dma_addr
, GFP_KERNEL
);
1899 /* nvme ns identify */
1900 nvme_sc
= nvme_identify(dev
, ns
->ns_id
, 0, dma_addr
);
1901 res
= nvme_trans_status_code(hdr
, nvme_sc
);
1909 flbas
= (id_ns
->flbas
) & 0x0F;
1910 nlbaf
= id_ns
->nlbaf
;
1912 for (i
= 0; i
< nlbaf
; i
++) {
1913 if (ns
->mode_select_block_len
== (1 << (id_ns
->lbaf
[i
].ds
))) {
1918 if (selected_lbaf
> 0x0F) {
1919 res
= nvme_trans_completion(hdr
, SAM_STAT_CHECK_CONDITION
,
1920 ILLEGAL_REQUEST
, SCSI_ASC_INVALID_PARAMETER
,
1921 SCSI_ASCQ_CAUSE_NOT_REPORTABLE
);
1923 if (ns
->mode_select_num_blocks
!= id_ns
->ncap
) {
1924 res
= nvme_trans_completion(hdr
, SAM_STAT_CHECK_CONDITION
,
1925 ILLEGAL_REQUEST
, SCSI_ASC_INVALID_PARAMETER
,
1926 SCSI_ASCQ_CAUSE_NOT_REPORTABLE
);
1929 cdw10
|= prot_info
<< 5;
1930 cdw10
|= selected_lbaf
& 0x0F;
1931 memset(&c
, 0, sizeof(c
));
1932 c
.format
.opcode
= nvme_admin_format_nvm
;
1933 c
.format
.nsid
= ns
->ns_id
;
1934 c
.format
.cdw10
= cpu_to_le32(cdw10
);
1936 nvme_sc
= nvme_submit_admin_cmd(dev
, &c
, NULL
);
1937 res
= nvme_trans_status_code(hdr
, nvme_sc
);
1944 dma_free_coherent(&dev
->pci_dev
->dev
, sizeof(struct nvme_id_ns
), mem
,
1950 /* Read/Write Helper Functions */
1952 static inline void nvme_trans_get_io_cdb6(u8
*cmd
,
1953 struct nvme_trans_io_cdb
*cdb_info
)
1956 cdb_info
->prot_info
= 0;
1957 cdb_info
->lba
= GET_U32_FROM_CDB(cmd
, IO_6_CDB_LBA_OFFSET
) &
1959 cdb_info
->xfer_len
= GET_U8_FROM_CDB(cmd
, IO_6_CDB_TX_LEN_OFFSET
);
1961 /* sbc3r27 sec 5.32 - TRANSFER LEN of 0 implies a 256 Block transfer */
1962 if (cdb_info
->xfer_len
== 0)
1963 cdb_info
->xfer_len
= IO_6_DEFAULT_TX_LEN
;
1966 static inline void nvme_trans_get_io_cdb10(u8
*cmd
,
1967 struct nvme_trans_io_cdb
*cdb_info
)
1969 cdb_info
->fua
= GET_U8_FROM_CDB(cmd
, IO_10_CDB_FUA_OFFSET
) &
1971 cdb_info
->prot_info
= GET_U8_FROM_CDB(cmd
, IO_10_CDB_WP_OFFSET
) &
1972 IO_CDB_WP_MASK
>> IO_CDB_WP_SHIFT
;
1973 cdb_info
->lba
= GET_U32_FROM_CDB(cmd
, IO_10_CDB_LBA_OFFSET
);
1974 cdb_info
->xfer_len
= GET_U16_FROM_CDB(cmd
, IO_10_CDB_TX_LEN_OFFSET
);
1977 static inline void nvme_trans_get_io_cdb12(u8
*cmd
,
1978 struct nvme_trans_io_cdb
*cdb_info
)
1980 cdb_info
->fua
= GET_U8_FROM_CDB(cmd
, IO_12_CDB_FUA_OFFSET
) &
1982 cdb_info
->prot_info
= GET_U8_FROM_CDB(cmd
, IO_12_CDB_WP_OFFSET
) &
1983 IO_CDB_WP_MASK
>> IO_CDB_WP_SHIFT
;
1984 cdb_info
->lba
= GET_U32_FROM_CDB(cmd
, IO_12_CDB_LBA_OFFSET
);
1985 cdb_info
->xfer_len
= GET_U32_FROM_CDB(cmd
, IO_12_CDB_TX_LEN_OFFSET
);
1988 static inline void nvme_trans_get_io_cdb16(u8
*cmd
,
1989 struct nvme_trans_io_cdb
*cdb_info
)
1991 cdb_info
->fua
= GET_U8_FROM_CDB(cmd
, IO_16_CDB_FUA_OFFSET
) &
1993 cdb_info
->prot_info
= GET_U8_FROM_CDB(cmd
, IO_16_CDB_WP_OFFSET
) &
1994 IO_CDB_WP_MASK
>> IO_CDB_WP_SHIFT
;
1995 cdb_info
->lba
= GET_U64_FROM_CDB(cmd
, IO_16_CDB_LBA_OFFSET
);
1996 cdb_info
->xfer_len
= GET_U32_FROM_CDB(cmd
, IO_16_CDB_TX_LEN_OFFSET
);
1999 static inline u32
nvme_trans_io_get_num_cmds(struct sg_io_hdr
*hdr
,
2000 struct nvme_trans_io_cdb
*cdb_info
,
2003 /* If using iovecs, send one nvme command per vector */
2004 if (hdr
->iovec_count
> 0)
2005 return hdr
->iovec_count
;
2006 else if (cdb_info
->xfer_len
> max_blocks
)
2007 return ((cdb_info
->xfer_len
- 1) / max_blocks
) + 1;
2012 static u16
nvme_trans_io_get_control(struct nvme_ns
*ns
,
2013 struct nvme_trans_io_cdb
*cdb_info
)
2017 /* When Protection information support is added, implement here */
2019 if (cdb_info
->fua
> 0)
2020 control
|= NVME_RW_FUA
;
2025 static int nvme_trans_do_nvme_io(struct nvme_ns
*ns
, struct sg_io_hdr
*hdr
,
2026 struct nvme_trans_io_cdb
*cdb_info
, u8 is_write
)
2028 int res
= SNTI_TRANSLATION_SUCCESS
;
2030 struct nvme_dev
*dev
= ns
->dev
;
2031 struct nvme_queue
*nvmeq
= get_nvmeq(ns
->dev
);
2033 struct nvme_iod
*iod
;
2035 u64 unit_num_blocks
; /* Number of blocks to xfer in each nvme cmd */
2038 u64 nvme_offset
= 0;
2039 void *next_mapping_addr
;
2040 struct nvme_command c
;
2041 u8 opcode
= (is_write
? nvme_cmd_write
: nvme_cmd_read
);
2043 u32 max_blocks
= (dev
->max_hw_sectors
<< 9) >> ns
->lba_shift
;
2045 num_cmds
= nvme_trans_io_get_num_cmds(hdr
, cdb_info
, max_blocks
);
2048 * This loop handles two cases.
2049 * First, when an SGL is used in the form of an iovec list:
2050 * - Use iov_base as the next mapping address for the nvme command_id
2051 * - Use iov_len as the data transfer length for the command.
2052 * Second, when we have a single buffer
2053 * - If larger than max_blocks, split into chunks, offset
2054 * each nvme command accordingly.
2056 for (i
= 0; i
< num_cmds
; i
++) {
2057 memset(&c
, 0, sizeof(c
));
2058 if (hdr
->iovec_count
> 0) {
2059 struct sg_iovec
*sgl
= hdr
->dxferp
;
2061 unit_len
= sgl
[i
].iov_len
;
2062 unit_num_blocks
= unit_len
>> ns
->lba_shift
;
2063 next_mapping_addr
= sgl
[i
].iov_base
;
2065 unit_num_blocks
= min((u64
)max_blocks
,
2066 (cdb_info
->xfer_len
- nvme_offset
));
2067 unit_len
= unit_num_blocks
<< ns
->lba_shift
;
2068 next_mapping_addr
= hdr
->dxferp
+
2069 ((1 << ns
->lba_shift
) * nvme_offset
);
2072 c
.rw
.opcode
= opcode
;
2073 c
.rw
.nsid
= cpu_to_le32(ns
->ns_id
);
2074 c
.rw
.slba
= cpu_to_le64(cdb_info
->lba
+ nvme_offset
);
2075 c
.rw
.length
= cpu_to_le16(unit_num_blocks
- 1);
2076 control
= nvme_trans_io_get_control(ns
, cdb_info
);
2077 c
.rw
.control
= cpu_to_le16(control
);
2079 iod
= nvme_map_user_pages(dev
,
2080 (is_write
) ? DMA_TO_DEVICE
: DMA_FROM_DEVICE
,
2081 (unsigned long)next_mapping_addr
, unit_len
);
2086 retcode
= nvme_setup_prps(dev
, &c
.common
, iod
, unit_len
,
2088 if (retcode
!= unit_len
) {
2089 nvme_unmap_user_pages(dev
,
2090 (is_write
) ? DMA_TO_DEVICE
: DMA_FROM_DEVICE
,
2092 nvme_free_iod(dev
, iod
);
2097 nvme_offset
+= unit_num_blocks
;
2099 nvmeq
= get_nvmeq(dev
);
2101 * Since nvme_submit_sync_cmd sleeps, we can't keep
2102 * preemption disabled. We may be preempted at any
2103 * point, and be rescheduled to a different CPU. That
2104 * will cause cacheline bouncing, but no additional
2105 * races since q_lock already protects against other
2109 nvme_sc
= nvme_submit_sync_cmd(nvmeq
, &c
, NULL
,
2111 if (nvme_sc
!= NVME_SC_SUCCESS
) {
2112 nvme_unmap_user_pages(dev
,
2113 (is_write
) ? DMA_TO_DEVICE
: DMA_FROM_DEVICE
,
2115 nvme_free_iod(dev
, iod
);
2116 res
= nvme_trans_status_code(hdr
, nvme_sc
);
2119 nvme_unmap_user_pages(dev
,
2120 (is_write
) ? DMA_TO_DEVICE
: DMA_FROM_DEVICE
,
2122 nvme_free_iod(dev
, iod
);
2124 res
= nvme_trans_status_code(hdr
, NVME_SC_SUCCESS
);
2131 /* SCSI Command Translation Functions */
2133 static int nvme_trans_io(struct nvme_ns
*ns
, struct sg_io_hdr
*hdr
, u8 is_write
,
2136 int res
= SNTI_TRANSLATION_SUCCESS
;
2137 struct nvme_trans_io_cdb cdb_info
;
2140 u64 sum_iov_len
= 0;
2141 struct sg_iovec
*sgl
;
2144 /* Extract Fields from CDB */
2148 nvme_trans_get_io_cdb6(cmd
, &cdb_info
);
2152 nvme_trans_get_io_cdb10(cmd
, &cdb_info
);
2156 nvme_trans_get_io_cdb12(cmd
, &cdb_info
);
2160 nvme_trans_get_io_cdb16(cmd
, &cdb_info
);
2163 /* Will never really reach here */
2164 res
= SNTI_INTERNAL_ERROR
;
2168 /* Calculate total length of transfer (in bytes) */
2169 if (hdr
->iovec_count
> 0) {
2171 for (i
= 0; i
< hdr
->iovec_count
; i
++) {
2172 sum_iov_len
+= sgl
[i
].iov_len
;
2173 /* IO vector sizes should be multiples of block size */
2174 if (sgl
[i
].iov_len
% (1 << ns
->lba_shift
) != 0) {
2175 res
= nvme_trans_completion(hdr
,
2176 SAM_STAT_CHECK_CONDITION
,
2178 SCSI_ASC_INVALID_PARAMETER
,
2179 SCSI_ASCQ_CAUSE_NOT_REPORTABLE
);
2184 sum_iov_len
= hdr
->dxfer_len
;
2187 /* As Per sg ioctl howto, if the lengths differ, use the lower one */
2188 xfer_bytes
= min(((u64
)hdr
->dxfer_len
), sum_iov_len
);
2190 /* If block count and actual data buffer size dont match, error out */
2191 if (xfer_bytes
!= (cdb_info
.xfer_len
<< ns
->lba_shift
)) {
2196 /* Check for 0 length transfer - it is not illegal */
2197 if (cdb_info
.xfer_len
== 0)
2200 /* Send NVMe IO Command(s) */
2201 res
= nvme_trans_do_nvme_io(ns
, hdr
, &cdb_info
, is_write
);
2202 if (res
!= SNTI_TRANSLATION_SUCCESS
)
2209 static int nvme_trans_inquiry(struct nvme_ns
*ns
, struct sg_io_hdr
*hdr
,
2212 int res
= SNTI_TRANSLATION_SUCCESS
;
2218 evpd
= GET_INQ_EVPD_BIT(cmd
);
2219 page_code
= GET_INQ_PAGE_CODE(cmd
);
2220 alloc_len
= GET_INQ_ALLOC_LENGTH(cmd
);
2222 inq_response
= kmalloc(STANDARD_INQUIRY_LENGTH
, GFP_KERNEL
);
2223 if (inq_response
== NULL
) {
2229 if (page_code
== INQ_STANDARD_INQUIRY_PAGE
) {
2230 res
= nvme_trans_standard_inquiry_page(ns
, hdr
,
2231 inq_response
, alloc_len
);
2233 res
= nvme_trans_completion(hdr
,
2234 SAM_STAT_CHECK_CONDITION
,
2236 SCSI_ASC_INVALID_CDB
,
2237 SCSI_ASCQ_CAUSE_NOT_REPORTABLE
);
2240 switch (page_code
) {
2241 case VPD_SUPPORTED_PAGES
:
2242 res
= nvme_trans_supported_vpd_pages(ns
, hdr
,
2243 inq_response
, alloc_len
);
2245 case VPD_SERIAL_NUMBER
:
2246 res
= nvme_trans_unit_serial_page(ns
, hdr
, inq_response
,
2249 case VPD_DEVICE_IDENTIFIERS
:
2250 res
= nvme_trans_device_id_page(ns
, hdr
, inq_response
,
2253 case VPD_EXTENDED_INQUIRY
:
2254 res
= nvme_trans_ext_inq_page(ns
, hdr
, alloc_len
);
2256 case VPD_BLOCK_DEV_CHARACTERISTICS
:
2257 res
= nvme_trans_bdev_char_page(ns
, hdr
, alloc_len
);
2260 res
= nvme_trans_completion(hdr
,
2261 SAM_STAT_CHECK_CONDITION
,
2263 SCSI_ASC_INVALID_CDB
,
2264 SCSI_ASCQ_CAUSE_NOT_REPORTABLE
);
2268 kfree(inq_response
);
2273 static int nvme_trans_log_sense(struct nvme_ns
*ns
, struct sg_io_hdr
*hdr
,
2276 int res
= SNTI_TRANSLATION_SUCCESS
;
2282 sp
= GET_U8_FROM_CDB(cmd
, LOG_SENSE_CDB_SP_OFFSET
);
2283 if (sp
!= LOG_SENSE_CDB_SP_NOT_ENABLED
) {
2284 res
= nvme_trans_completion(hdr
, SAM_STAT_CHECK_CONDITION
,
2285 ILLEGAL_REQUEST
, SCSI_ASC_INVALID_CDB
,
2286 SCSI_ASCQ_CAUSE_NOT_REPORTABLE
);
2289 pc
= GET_U8_FROM_CDB(cmd
, LOG_SENSE_CDB_PC_OFFSET
);
2290 page_code
= pc
& LOG_SENSE_CDB_PAGE_CODE_MASK
;
2291 pc
= (pc
& LOG_SENSE_CDB_PC_MASK
) >> LOG_SENSE_CDB_PC_SHIFT
;
2292 if (pc
!= LOG_SENSE_CDB_PC_CUMULATIVE_VALUES
) {
2293 res
= nvme_trans_completion(hdr
, SAM_STAT_CHECK_CONDITION
,
2294 ILLEGAL_REQUEST
, SCSI_ASC_INVALID_CDB
,
2295 SCSI_ASCQ_CAUSE_NOT_REPORTABLE
);
2298 alloc_len
= GET_U16_FROM_CDB(cmd
, LOG_SENSE_CDB_ALLOC_LENGTH_OFFSET
);
2299 switch (page_code
) {
2300 case LOG_PAGE_SUPPORTED_LOG_PAGES_PAGE
:
2301 res
= nvme_trans_log_supp_pages(ns
, hdr
, alloc_len
);
2303 case LOG_PAGE_INFORMATIONAL_EXCEPTIONS_PAGE
:
2304 res
= nvme_trans_log_info_exceptions(ns
, hdr
, alloc_len
);
2306 case LOG_PAGE_TEMPERATURE_PAGE
:
2307 res
= nvme_trans_log_temperature(ns
, hdr
, alloc_len
);
2310 res
= nvme_trans_completion(hdr
, SAM_STAT_CHECK_CONDITION
,
2311 ILLEGAL_REQUEST
, SCSI_ASC_INVALID_CDB
,
2312 SCSI_ASCQ_CAUSE_NOT_REPORTABLE
);
2320 static int nvme_trans_mode_select(struct nvme_ns
*ns
, struct sg_io_hdr
*hdr
,
2323 int res
= SNTI_TRANSLATION_SUCCESS
;
2329 page_format
= GET_U8_FROM_CDB(cmd
, MODE_SELECT_CDB_PAGE_FORMAT_OFFSET
);
2330 page_format
&= MODE_SELECT_CDB_PAGE_FORMAT_MASK
;
2332 save_pages
= GET_U8_FROM_CDB(cmd
, MODE_SELECT_CDB_SAVE_PAGES_OFFSET
);
2333 save_pages
&= MODE_SELECT_CDB_SAVE_PAGES_MASK
;
2335 if (GET_OPCODE(cmd
) == MODE_SELECT
) {
2336 parm_list_len
= GET_U8_FROM_CDB(cmd
,
2337 MODE_SELECT_6_CDB_PARAM_LIST_LENGTH_OFFSET
);
2339 parm_list_len
= GET_U16_FROM_CDB(cmd
,
2340 MODE_SELECT_10_CDB_PARAM_LIST_LENGTH_OFFSET
);
2344 if (parm_list_len
!= 0) {
2346 * According to SPC-4 r24, a paramter list length field of 0
2347 * shall not be considered an error
2349 res
= nvme_trans_modesel_data(ns
, hdr
, cmd
, parm_list_len
,
2350 page_format
, save_pages
, cdb10
);
2356 static int nvme_trans_mode_sense(struct nvme_ns
*ns
, struct sg_io_hdr
*hdr
,
2359 int res
= SNTI_TRANSLATION_SUCCESS
;
2365 if (GET_OPCODE(cmd
) == MODE_SENSE
) {
2366 alloc_len
= GET_U8_FROM_CDB(cmd
, MODE_SENSE6_ALLOC_LEN_OFFSET
);
2368 alloc_len
= GET_U16_FROM_CDB(cmd
,
2369 MODE_SENSE10_ALLOC_LEN_OFFSET
);
2373 pc
= GET_U8_FROM_CDB(cmd
, MODE_SENSE_PAGE_CONTROL_OFFSET
) &
2374 MODE_SENSE_PAGE_CONTROL_MASK
;
2375 if (pc
!= MODE_SENSE_PC_CURRENT_VALUES
) {
2376 res
= nvme_trans_completion(hdr
, SAM_STAT_CHECK_CONDITION
,
2377 ILLEGAL_REQUEST
, SCSI_ASC_INVALID_CDB
,
2378 SCSI_ASCQ_CAUSE_NOT_REPORTABLE
);
2382 page_code
= GET_U8_FROM_CDB(cmd
, MODE_SENSE_PAGE_CODE_OFFSET
) &
2383 MODE_SENSE_PAGE_CODE_MASK
;
2384 switch (page_code
) {
2385 case MODE_PAGE_CACHING
:
2386 res
= nvme_trans_mode_page_create(ns
, hdr
, cmd
, alloc_len
,
2388 &nvme_trans_fill_caching_page
,
2389 MODE_PAGE_CACHING_LEN
);
2391 case MODE_PAGE_CONTROL
:
2392 res
= nvme_trans_mode_page_create(ns
, hdr
, cmd
, alloc_len
,
2394 &nvme_trans_fill_control_page
,
2395 MODE_PAGE_CONTROL_LEN
);
2397 case MODE_PAGE_POWER_CONDITION
:
2398 res
= nvme_trans_mode_page_create(ns
, hdr
, cmd
, alloc_len
,
2400 &nvme_trans_fill_pow_cnd_page
,
2401 MODE_PAGE_POW_CND_LEN
);
2403 case MODE_PAGE_INFO_EXCEP
:
2404 res
= nvme_trans_mode_page_create(ns
, hdr
, cmd
, alloc_len
,
2406 &nvme_trans_fill_inf_exc_page
,
2407 MODE_PAGE_INF_EXC_LEN
);
2409 case MODE_PAGE_RETURN_ALL
:
2410 res
= nvme_trans_mode_page_create(ns
, hdr
, cmd
, alloc_len
,
2412 &nvme_trans_fill_all_pages
,
2416 res
= nvme_trans_completion(hdr
, SAM_STAT_CHECK_CONDITION
,
2417 ILLEGAL_REQUEST
, SCSI_ASC_INVALID_CDB
,
2418 SCSI_ASCQ_CAUSE_NOT_REPORTABLE
);
2426 static int nvme_trans_read_capacity(struct nvme_ns
*ns
, struct sg_io_hdr
*hdr
,
2429 int res
= SNTI_TRANSLATION_SUCCESS
;
2431 u32 alloc_len
= READ_CAP_10_RESP_SIZE
;
2432 u32 resp_size
= READ_CAP_10_RESP_SIZE
;
2435 struct nvme_dev
*dev
= ns
->dev
;
2436 dma_addr_t dma_addr
;
2438 struct nvme_id_ns
*id_ns
;
2441 cdb16
= IS_READ_CAP_16(cmd
);
2443 alloc_len
= GET_READ_CAP_16_ALLOC_LENGTH(cmd
);
2444 resp_size
= READ_CAP_16_RESP_SIZE
;
2447 mem
= dma_alloc_coherent(&dev
->pci_dev
->dev
, sizeof(struct nvme_id_ns
),
2448 &dma_addr
, GFP_KERNEL
);
2453 /* nvme ns identify */
2454 nvme_sc
= nvme_identify(dev
, ns
->ns_id
, 0, dma_addr
);
2455 res
= nvme_trans_status_code(hdr
, nvme_sc
);
2464 response
= kmalloc(resp_size
, GFP_KERNEL
);
2465 if (response
== NULL
) {
2469 memset(response
, 0, resp_size
);
2470 nvme_trans_fill_read_cap(response
, id_ns
, cdb16
);
2472 xfer_len
= min(alloc_len
, resp_size
);
2473 res
= nvme_trans_copy_to_user(hdr
, response
, xfer_len
);
2477 dma_free_coherent(&dev
->pci_dev
->dev
, sizeof(struct nvme_id_ns
), mem
,
2483 static int nvme_trans_report_luns(struct nvme_ns
*ns
, struct sg_io_hdr
*hdr
,
2486 int res
= SNTI_TRANSLATION_SUCCESS
;
2488 u32 alloc_len
, xfer_len
, resp_size
;
2491 struct nvme_dev
*dev
= ns
->dev
;
2492 dma_addr_t dma_addr
;
2494 struct nvme_id_ctrl
*id_ctrl
;
2495 u32 ll_length
, lun_id
;
2496 u8 lun_id_offset
= REPORT_LUNS_FIRST_LUN_OFFSET
;
2499 alloc_len
= GET_REPORT_LUNS_ALLOC_LENGTH(cmd
);
2500 select_report
= GET_U8_FROM_CDB(cmd
, REPORT_LUNS_SR_OFFSET
);
2502 if ((select_report
!= ALL_LUNS_RETURNED
) &&
2503 (select_report
!= ALL_WELL_KNOWN_LUNS_RETURNED
) &&
2504 (select_report
!= RESTRICTED_LUNS_RETURNED
)) {
2505 res
= nvme_trans_completion(hdr
, SAM_STAT_CHECK_CONDITION
,
2506 ILLEGAL_REQUEST
, SCSI_ASC_INVALID_CDB
,
2507 SCSI_ASCQ_CAUSE_NOT_REPORTABLE
);
2510 /* NVMe Controller Identify */
2511 mem
= dma_alloc_coherent(&dev
->pci_dev
->dev
,
2512 sizeof(struct nvme_id_ctrl
),
2513 &dma_addr
, GFP_KERNEL
);
2518 nvme_sc
= nvme_identify(dev
, 0, 1, dma_addr
);
2519 res
= nvme_trans_status_code(hdr
, nvme_sc
);
2527 ll_length
= id_ctrl
->nn
* LUN_ENTRY_SIZE
;
2528 resp_size
= ll_length
+ LUN_DATA_HEADER_SIZE
;
2530 if (alloc_len
< resp_size
) {
2531 res
= nvme_trans_completion(hdr
,
2532 SAM_STAT_CHECK_CONDITION
,
2533 ILLEGAL_REQUEST
, SCSI_ASC_INVALID_CDB
,
2534 SCSI_ASCQ_CAUSE_NOT_REPORTABLE
);
2538 response
= kmalloc(resp_size
, GFP_KERNEL
);
2539 if (response
== NULL
) {
2543 memset(response
, 0, resp_size
);
2545 /* The first LUN ID will always be 0 per the SAM spec */
2546 for (lun_id
= 0; lun_id
< id_ctrl
->nn
; lun_id
++) {
2548 * Set the LUN Id and then increment to the next LUN
2549 * location in the parameter data.
2551 u64 tmp_id
= cpu_to_be64(lun_id
);
2552 memcpy(&response
[lun_id_offset
], &tmp_id
, sizeof(u64
));
2553 lun_id_offset
+= LUN_ENTRY_SIZE
;
2555 tmp_len
= cpu_to_be32(ll_length
);
2556 memcpy(response
, &tmp_len
, sizeof(u32
));
2559 xfer_len
= min(alloc_len
, resp_size
);
2560 res
= nvme_trans_copy_to_user(hdr
, response
, xfer_len
);
2564 dma_free_coherent(&dev
->pci_dev
->dev
, sizeof(struct nvme_id_ctrl
), mem
,
2570 static int nvme_trans_request_sense(struct nvme_ns
*ns
, struct sg_io_hdr
*hdr
,
2573 int res
= SNTI_TRANSLATION_SUCCESS
;
2574 u8 alloc_len
, xfer_len
, resp_size
;
2578 alloc_len
= GET_REQUEST_SENSE_ALLOC_LENGTH(cmd
);
2579 desc_format
= GET_U8_FROM_CDB(cmd
, REQUEST_SENSE_DESC_OFFSET
);
2580 desc_format
&= REQUEST_SENSE_DESC_MASK
;
2582 resp_size
= ((desc_format
) ? (DESC_FMT_SENSE_DATA_SIZE
) :
2583 (FIXED_FMT_SENSE_DATA_SIZE
));
2584 response
= kmalloc(resp_size
, GFP_KERNEL
);
2585 if (response
== NULL
) {
2589 memset(response
, 0, resp_size
);
2591 if (desc_format
== DESCRIPTOR_FORMAT_SENSE_DATA_TYPE
) {
2592 /* Descriptor Format Sense Data */
2593 response
[0] = DESC_FORMAT_SENSE_DATA
;
2594 response
[1] = NO_SENSE
;
2595 /* TODO How is LOW POWER CONDITION ON handled? (byte 2) */
2596 response
[2] = SCSI_ASC_NO_SENSE
;
2597 response
[3] = SCSI_ASCQ_CAUSE_NOT_REPORTABLE
;
2598 /* SDAT_OVFL = 0 | Additional Sense Length = 0 */
2600 /* Fixed Format Sense Data */
2601 response
[0] = FIXED_SENSE_DATA
;
2602 /* Byte 1 = Obsolete */
2603 response
[2] = NO_SENSE
; /* FM, EOM, ILI, SDAT_OVFL = 0 */
2604 /* Bytes 3-6 - Information - set to zero */
2605 response
[7] = FIXED_SENSE_DATA_ADD_LENGTH
;
2606 /* Bytes 8-11 - Cmd Specific Information - set to zero */
2607 response
[12] = SCSI_ASC_NO_SENSE
;
2608 response
[13] = SCSI_ASCQ_CAUSE_NOT_REPORTABLE
;
2609 /* Byte 14 = Field Replaceable Unit Code = 0 */
2610 /* Bytes 15-17 - SKSV=0; Sense Key Specific = 0 */
2613 xfer_len
= min(alloc_len
, resp_size
);
2614 res
= nvme_trans_copy_to_user(hdr
, response
, xfer_len
);
2621 static int nvme_trans_security_protocol(struct nvme_ns
*ns
,
2622 struct sg_io_hdr
*hdr
,
2625 return nvme_trans_completion(hdr
, SAM_STAT_CHECK_CONDITION
,
2626 ILLEGAL_REQUEST
, SCSI_ASC_ILLEGAL_COMMAND
,
2627 SCSI_ASCQ_CAUSE_NOT_REPORTABLE
);
2630 static int nvme_trans_start_stop(struct nvme_ns
*ns
, struct sg_io_hdr
*hdr
,
2633 int res
= SNTI_TRANSLATION_SUCCESS
;
2635 struct nvme_queue
*nvmeq
= get_nvmeq(ns
->dev
);
2636 u8 immed
, pcmod
, pc
, no_flush
, start
;
2638 immed
= GET_U8_FROM_CDB(cmd
, START_STOP_UNIT_CDB_IMMED_OFFSET
);
2639 pcmod
= GET_U8_FROM_CDB(cmd
, START_STOP_UNIT_CDB_POWER_COND_MOD_OFFSET
);
2640 pc
= GET_U8_FROM_CDB(cmd
, START_STOP_UNIT_CDB_POWER_COND_OFFSET
);
2641 no_flush
= GET_U8_FROM_CDB(cmd
, START_STOP_UNIT_CDB_NO_FLUSH_OFFSET
);
2642 start
= GET_U8_FROM_CDB(cmd
, START_STOP_UNIT_CDB_START_OFFSET
);
2644 immed
&= START_STOP_UNIT_CDB_IMMED_MASK
;
2645 pcmod
&= START_STOP_UNIT_CDB_POWER_COND_MOD_MASK
;
2646 pc
= (pc
& START_STOP_UNIT_CDB_POWER_COND_MASK
) >> NIBBLE_SHIFT
;
2647 no_flush
&= START_STOP_UNIT_CDB_NO_FLUSH_MASK
;
2648 start
&= START_STOP_UNIT_CDB_START_MASK
;
2651 res
= nvme_trans_completion(hdr
, SAM_STAT_CHECK_CONDITION
,
2652 ILLEGAL_REQUEST
, SCSI_ASC_INVALID_CDB
,
2653 SCSI_ASCQ_CAUSE_NOT_REPORTABLE
);
2655 if (no_flush
== 0) {
2656 /* Issue NVME FLUSH command prior to START STOP UNIT */
2657 nvme_sc
= nvme_submit_flush_data(nvmeq
, ns
);
2659 res
= nvme_trans_status_code(hdr
, nvme_sc
);
2667 /* Setup the expected power state transition */
2668 res
= nvme_trans_power_state(ns
, hdr
, pc
, pcmod
, start
);
2675 static int nvme_trans_synchronize_cache(struct nvme_ns
*ns
,
2676 struct sg_io_hdr
*hdr
, u8
*cmd
)
2678 int res
= SNTI_TRANSLATION_SUCCESS
;
2680 struct nvme_queue
*nvmeq
= get_nvmeq(ns
->dev
);
2682 nvme_sc
= nvme_submit_flush_data(nvmeq
, ns
);
2683 res
= nvme_trans_status_code(hdr
, nvme_sc
);
2693 static int nvme_trans_format_unit(struct nvme_ns
*ns
, struct sg_io_hdr
*hdr
,
2696 int res
= SNTI_TRANSLATION_SUCCESS
;
2697 u8 parm_hdr_len
= 0;
2698 u8 nvme_pf_code
= 0;
2699 u8 format_prot_info
, long_list
, format_data
;
2701 format_prot_info
= GET_U8_FROM_CDB(cmd
,
2702 FORMAT_UNIT_CDB_FORMAT_PROT_INFO_OFFSET
);
2703 long_list
= GET_U8_FROM_CDB(cmd
, FORMAT_UNIT_CDB_LONG_LIST_OFFSET
);
2704 format_data
= GET_U8_FROM_CDB(cmd
, FORMAT_UNIT_CDB_FORMAT_DATA_OFFSET
);
2706 format_prot_info
= (format_prot_info
&
2707 FORMAT_UNIT_CDB_FORMAT_PROT_INFO_MASK
) >>
2708 FORMAT_UNIT_CDB_FORMAT_PROT_INFO_SHIFT
;
2709 long_list
&= FORMAT_UNIT_CDB_LONG_LIST_MASK
;
2710 format_data
&= FORMAT_UNIT_CDB_FORMAT_DATA_MASK
;
2712 if (format_data
!= 0) {
2713 if (format_prot_info
!= 0) {
2715 parm_hdr_len
= FORMAT_UNIT_SHORT_PARM_LIST_LEN
;
2717 parm_hdr_len
= FORMAT_UNIT_LONG_PARM_LIST_LEN
;
2719 } else if (format_data
== 0 && format_prot_info
!= 0) {
2720 res
= nvme_trans_completion(hdr
, SAM_STAT_CHECK_CONDITION
,
2721 ILLEGAL_REQUEST
, SCSI_ASC_INVALID_CDB
,
2722 SCSI_ASCQ_CAUSE_NOT_REPORTABLE
);
2726 /* Get parm header from data-in/out buffer */
2728 * According to the translation spec, the only fields in the parameter
2729 * list we are concerned with are in the header. So allocate only that.
2731 if (parm_hdr_len
> 0) {
2732 res
= nvme_trans_fmt_get_parm_header(hdr
, parm_hdr_len
,
2733 format_prot_info
, &nvme_pf_code
);
2734 if (res
!= SNTI_TRANSLATION_SUCCESS
)
2738 /* Attempt to activate any previously downloaded firmware image */
2739 res
= nvme_trans_send_fw_cmd(ns
, hdr
, nvme_admin_activate_fw
, 0, 0, 0);
2741 /* Determine Block size and count and send format command */
2742 res
= nvme_trans_fmt_set_blk_size_count(ns
, hdr
);
2743 if (res
!= SNTI_TRANSLATION_SUCCESS
)
2746 res
= nvme_trans_fmt_send_cmd(ns
, hdr
, nvme_pf_code
);
2752 static int nvme_trans_test_unit_ready(struct nvme_ns
*ns
,
2753 struct sg_io_hdr
*hdr
,
2756 int res
= SNTI_TRANSLATION_SUCCESS
;
2757 struct nvme_dev
*dev
= ns
->dev
;
2759 if (!(readl(&dev
->bar
->csts
) & NVME_CSTS_RDY
))
2760 res
= nvme_trans_completion(hdr
, SAM_STAT_CHECK_CONDITION
,
2761 NOT_READY
, SCSI_ASC_LUN_NOT_READY
,
2762 SCSI_ASCQ_CAUSE_NOT_REPORTABLE
);
2764 res
= nvme_trans_completion(hdr
, SAM_STAT_GOOD
, NO_SENSE
, 0, 0);
2769 static int nvme_trans_write_buffer(struct nvme_ns
*ns
, struct sg_io_hdr
*hdr
,
2772 int res
= SNTI_TRANSLATION_SUCCESS
;
2773 u32 buffer_offset
, parm_list_length
;
2777 GET_U24_FROM_CDB(cmd
, WRITE_BUFFER_CDB_PARM_LIST_LENGTH_OFFSET
);
2778 if (parm_list_length
% BYTES_TO_DWORDS
!= 0) {
2779 /* NVMe expects Firmware file to be a whole number of DWORDS */
2780 res
= nvme_trans_completion(hdr
, SAM_STAT_CHECK_CONDITION
,
2781 ILLEGAL_REQUEST
, SCSI_ASC_INVALID_CDB
,
2782 SCSI_ASCQ_CAUSE_NOT_REPORTABLE
);
2785 buffer_id
= GET_U8_FROM_CDB(cmd
, WRITE_BUFFER_CDB_BUFFER_ID_OFFSET
);
2786 if (buffer_id
> NVME_MAX_FIRMWARE_SLOT
) {
2787 res
= nvme_trans_completion(hdr
, SAM_STAT_CHECK_CONDITION
,
2788 ILLEGAL_REQUEST
, SCSI_ASC_INVALID_CDB
,
2789 SCSI_ASCQ_CAUSE_NOT_REPORTABLE
);
2792 mode
= GET_U8_FROM_CDB(cmd
, WRITE_BUFFER_CDB_MODE_OFFSET
) &
2793 WRITE_BUFFER_CDB_MODE_MASK
;
2795 GET_U24_FROM_CDB(cmd
, WRITE_BUFFER_CDB_BUFFER_OFFSET_OFFSET
);
2798 case DOWNLOAD_SAVE_ACTIVATE
:
2799 res
= nvme_trans_send_fw_cmd(ns
, hdr
, nvme_admin_download_fw
,
2800 parm_list_length
, buffer_offset
,
2802 if (res
!= SNTI_TRANSLATION_SUCCESS
)
2804 res
= nvme_trans_send_fw_cmd(ns
, hdr
, nvme_admin_activate_fw
,
2805 parm_list_length
, buffer_offset
,
2808 case DOWNLOAD_SAVE_DEFER_ACTIVATE
:
2809 res
= nvme_trans_send_fw_cmd(ns
, hdr
, nvme_admin_download_fw
,
2810 parm_list_length
, buffer_offset
,
2813 case ACTIVATE_DEFERRED_MICROCODE
:
2814 res
= nvme_trans_send_fw_cmd(ns
, hdr
, nvme_admin_activate_fw
,
2815 parm_list_length
, buffer_offset
,
2819 res
= nvme_trans_completion(hdr
, SAM_STAT_CHECK_CONDITION
,
2820 ILLEGAL_REQUEST
, SCSI_ASC_INVALID_CDB
,
2821 SCSI_ASCQ_CAUSE_NOT_REPORTABLE
);
2829 static int nvme_scsi_translate(struct nvme_ns
*ns
, struct sg_io_hdr
*hdr
)
2831 u8 cmd
[BLK_MAX_CDB
];
2833 unsigned int opcode
;
2835 if (hdr
->cmdp
== NULL
)
2837 if (copy_from_user(cmd
, hdr
->cmdp
, hdr
->cmd_len
))
2847 retcode
= nvme_trans_io(ns
, hdr
, 0, cmd
);
2853 retcode
= nvme_trans_io(ns
, hdr
, 1, cmd
);
2856 retcode
= nvme_trans_inquiry(ns
, hdr
, cmd
);
2859 retcode
= nvme_trans_log_sense(ns
, hdr
, cmd
);
2862 case MODE_SELECT_10
:
2863 retcode
= nvme_trans_mode_select(ns
, hdr
, cmd
);
2867 retcode
= nvme_trans_mode_sense(ns
, hdr
, cmd
);
2870 retcode
= nvme_trans_read_capacity(ns
, hdr
, cmd
);
2872 case SERVICE_ACTION_IN
:
2873 if (IS_READ_CAP_16(cmd
))
2874 retcode
= nvme_trans_read_capacity(ns
, hdr
, cmd
);
2879 retcode
= nvme_trans_report_luns(ns
, hdr
, cmd
);
2882 retcode
= nvme_trans_request_sense(ns
, hdr
, cmd
);
2884 case SECURITY_PROTOCOL_IN
:
2885 case SECURITY_PROTOCOL_OUT
:
2886 retcode
= nvme_trans_security_protocol(ns
, hdr
, cmd
);
2889 retcode
= nvme_trans_start_stop(ns
, hdr
, cmd
);
2891 case SYNCHRONIZE_CACHE
:
2892 retcode
= nvme_trans_synchronize_cache(ns
, hdr
, cmd
);
2895 retcode
= nvme_trans_format_unit(ns
, hdr
, cmd
);
2897 case TEST_UNIT_READY
:
2898 retcode
= nvme_trans_test_unit_ready(ns
, hdr
, cmd
);
2901 retcode
= nvme_trans_write_buffer(ns
, hdr
, cmd
);
2905 retcode
= nvme_trans_completion(hdr
, SAM_STAT_CHECK_CONDITION
,
2906 ILLEGAL_REQUEST
, SCSI_ASC_ILLEGAL_COMMAND
,
2907 SCSI_ASCQ_CAUSE_NOT_REPORTABLE
);
2913 int nvme_sg_io(struct nvme_ns
*ns
, struct sg_io_hdr __user
*u_hdr
)
2915 struct sg_io_hdr hdr
;
2918 if (!capable(CAP_SYS_ADMIN
))
2920 if (copy_from_user(&hdr
, u_hdr
, sizeof(hdr
)))
2922 if (hdr
.interface_id
!= 'S')
2924 if (hdr
.cmd_len
> BLK_MAX_CDB
)
2927 retcode
= nvme_scsi_translate(ns
, &hdr
);
2931 retcode
= SNTI_TRANSLATION_SUCCESS
;
2932 if (copy_to_user(__user u_hdr
, &hdr
, sizeof(sg_io_hdr_t
)) > 0)
2938 int nvme_sg_get_version_num(int __user
*ip
)
2940 return put_user(sg_version_num
, ip
);