2 * NVM Express device driver
3 * Copyright (c) 2011-2014, 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
16 * Refer to the SCSI-NVMe Translation spec for details on how
17 * each command is translated.
20 #include <linux/bio.h>
21 #include <linux/bitops.h>
22 #include <linux/blkdev.h>
23 #include <linux/compat.h>
24 #include <linux/delay.h>
25 #include <linux/errno.h>
27 #include <linux/genhd.h>
28 #include <linux/idr.h>
29 #include <linux/init.h>
30 #include <linux/interrupt.h>
32 #include <linux/kdev_t.h>
33 #include <linux/kthread.h>
34 #include <linux/kernel.h>
36 #include <linux/module.h>
37 #include <linux/moduleparam.h>
38 #include <linux/pci.h>
39 #include <linux/poison.h>
40 #include <linux/sched.h>
41 #include <linux/slab.h>
42 #include <linux/types.h>
43 #include <asm/unaligned.h>
45 #include <scsi/scsi.h>
49 static int sg_version_num
= 30534; /* 2 digits for each component */
52 #define VPD_SUPPORTED_PAGES 0x00
53 #define VPD_SERIAL_NUMBER 0x80
54 #define VPD_DEVICE_IDENTIFIERS 0x83
55 #define VPD_EXTENDED_INQUIRY 0x86
56 #define VPD_BLOCK_LIMITS 0xB0
57 #define VPD_BLOCK_DEV_CHARACTERISTICS 0xB1
59 /* format unit paramter list offsets */
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
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 DOWNLOAD_SAVE_ACTIVATE 0x05
76 #define DOWNLOAD_SAVE_DEFER_ACTIVATE 0x0E
77 #define ACTIVATE_DEFERRED_MICROCODE 0x0F
78 #define FORMAT_UNIT_IMMED_MASK 0x2
79 #define FORMAT_UNIT_IMMED_OFFSET 1
80 #define KELVIN_TEMP_FACTOR 273
81 #define FIXED_FMT_SENSE_DATA_SIZE 18
82 #define DESC_FMT_SENSE_DATA_SIZE 8
84 /* SCSI/NVMe defines and bit masks */
85 #define INQ_STANDARD_INQUIRY_PAGE 0x00
86 #define INQ_SUPPORTED_VPD_PAGES_PAGE 0x00
87 #define INQ_UNIT_SERIAL_NUMBER_PAGE 0x80
88 #define INQ_DEVICE_IDENTIFICATION_PAGE 0x83
89 #define INQ_EXTENDED_INQUIRY_DATA_PAGE 0x86
90 #define INQ_BDEV_LIMITS_PAGE 0xB0
91 #define INQ_BDEV_CHARACTERISTICS_PAGE 0xB1
92 #define INQ_SERIAL_NUMBER_LENGTH 0x14
93 #define INQ_NUM_SUPPORTED_VPD_PAGES 6
94 #define VERSION_SPC_4 0x06
95 #define ACA_UNSUPPORTED 0
96 #define STANDARD_INQUIRY_LENGTH 36
97 #define ADDITIONAL_STD_INQ_LENGTH 31
98 #define EXTENDED_INQUIRY_DATA_PAGE_LENGTH 0x3C
99 #define RESERVED_FIELD 0
101 /* Mode Sense/Select defines */
102 #define MODE_PAGE_INFO_EXCEP 0x1C
103 #define MODE_PAGE_CACHING 0x08
104 #define MODE_PAGE_CONTROL 0x0A
105 #define MODE_PAGE_POWER_CONDITION 0x1A
106 #define MODE_PAGE_RETURN_ALL 0x3F
107 #define MODE_PAGE_BLK_DES_LEN 0x08
108 #define MODE_PAGE_LLBAA_BLK_DES_LEN 0x10
109 #define MODE_PAGE_CACHING_LEN 0x14
110 #define MODE_PAGE_CONTROL_LEN 0x0C
111 #define MODE_PAGE_POW_CND_LEN 0x28
112 #define MODE_PAGE_INF_EXC_LEN 0x0C
113 #define MODE_PAGE_ALL_LEN 0x54
114 #define MODE_SENSE6_MPH_SIZE 4
115 #define MODE_SENSE_PAGE_CONTROL_MASK 0xC0
116 #define MODE_SENSE_PAGE_CODE_OFFSET 2
117 #define MODE_SENSE_PAGE_CODE_MASK 0x3F
118 #define MODE_SENSE_LLBAA_MASK 0x10
119 #define MODE_SENSE_LLBAA_SHIFT 4
120 #define MODE_SENSE_DBD_MASK 8
121 #define MODE_SENSE_DBD_SHIFT 3
122 #define MODE_SENSE10_MPH_SIZE 8
123 #define MODE_SELECT_CDB_PAGE_FORMAT_MASK 0x10
124 #define MODE_SELECT_CDB_SAVE_PAGES_MASK 0x1
125 #define MODE_SELECT_6_BD_OFFSET 3
126 #define MODE_SELECT_10_BD_OFFSET 6
127 #define MODE_SELECT_10_LLBAA_OFFSET 4
128 #define MODE_SELECT_10_LLBAA_MASK 1
129 #define MODE_SELECT_6_MPH_SIZE 4
130 #define MODE_SELECT_10_MPH_SIZE 8
131 #define CACHING_MODE_PAGE_WCE_MASK 0x04
132 #define MODE_SENSE_BLK_DESC_ENABLED 0
133 #define MODE_SENSE_BLK_DESC_COUNT 1
134 #define MODE_SELECT_PAGE_CODE_MASK 0x3F
135 #define SHORT_DESC_BLOCK 8
136 #define LONG_DESC_BLOCK 16
137 #define MODE_PAGE_POW_CND_LEN_FIELD 0x26
138 #define MODE_PAGE_INF_EXC_LEN_FIELD 0x0A
139 #define MODE_PAGE_CACHING_LEN_FIELD 0x12
140 #define MODE_PAGE_CONTROL_LEN_FIELD 0x0A
141 #define MODE_SENSE_PC_CURRENT_VALUES 0
143 /* Log Sense defines */
144 #define LOG_PAGE_SUPPORTED_LOG_PAGES_PAGE 0x00
145 #define LOG_PAGE_SUPPORTED_LOG_PAGES_LENGTH 0x07
146 #define LOG_PAGE_INFORMATIONAL_EXCEPTIONS_PAGE 0x2F
147 #define LOG_PAGE_TEMPERATURE_PAGE 0x0D
148 #define LOG_SENSE_CDB_SP_NOT_ENABLED 0
149 #define LOG_SENSE_CDB_PC_MASK 0xC0
150 #define LOG_SENSE_CDB_PC_SHIFT 6
151 #define LOG_SENSE_CDB_PC_CUMULATIVE_VALUES 1
152 #define LOG_SENSE_CDB_PAGE_CODE_MASK 0x3F
153 #define REMAINING_INFO_EXCP_PAGE_LENGTH 0x8
154 #define LOG_INFO_EXCP_PAGE_LENGTH 0xC
155 #define REMAINING_TEMP_PAGE_LENGTH 0xC
156 #define LOG_TEMP_PAGE_LENGTH 0x10
157 #define LOG_TEMP_UNKNOWN 0xFF
158 #define SUPPORTED_LOG_PAGES_PAGE_LENGTH 0x3
160 /* Read Capacity defines */
161 #define READ_CAP_10_RESP_SIZE 8
162 #define READ_CAP_16_RESP_SIZE 32
164 /* NVMe Namespace and Command Defines */
165 #define BYTES_TO_DWORDS 4
166 #define NVME_MAX_FIRMWARE_SLOT 7
168 /* Report LUNs defines */
169 #define REPORT_LUNS_FIRST_LUN_OFFSET 8
171 /* SCSI ADDITIONAL SENSE Codes */
173 #define SCSI_ASC_NO_SENSE 0x00
174 #define SCSI_ASC_PERIPHERAL_DEV_WRITE_FAULT 0x03
175 #define SCSI_ASC_LUN_NOT_READY 0x04
176 #define SCSI_ASC_WARNING 0x0B
177 #define SCSI_ASC_LOG_BLOCK_GUARD_CHECK_FAILED 0x10
178 #define SCSI_ASC_LOG_BLOCK_APPTAG_CHECK_FAILED 0x10
179 #define SCSI_ASC_LOG_BLOCK_REFTAG_CHECK_FAILED 0x10
180 #define SCSI_ASC_UNRECOVERED_READ_ERROR 0x11
181 #define SCSI_ASC_MISCOMPARE_DURING_VERIFY 0x1D
182 #define SCSI_ASC_ACCESS_DENIED_INVALID_LUN_ID 0x20
183 #define SCSI_ASC_ILLEGAL_COMMAND 0x20
184 #define SCSI_ASC_ILLEGAL_BLOCK 0x21
185 #define SCSI_ASC_INVALID_CDB 0x24
186 #define SCSI_ASC_INVALID_LUN 0x25
187 #define SCSI_ASC_INVALID_PARAMETER 0x26
188 #define SCSI_ASC_FORMAT_COMMAND_FAILED 0x31
189 #define SCSI_ASC_INTERNAL_TARGET_FAILURE 0x44
191 /* SCSI ADDITIONAL SENSE Code Qualifiers */
193 #define SCSI_ASCQ_CAUSE_NOT_REPORTABLE 0x00
194 #define SCSI_ASCQ_FORMAT_COMMAND_FAILED 0x01
195 #define SCSI_ASCQ_LOG_BLOCK_GUARD_CHECK_FAILED 0x01
196 #define SCSI_ASCQ_LOG_BLOCK_APPTAG_CHECK_FAILED 0x02
197 #define SCSI_ASCQ_LOG_BLOCK_REFTAG_CHECK_FAILED 0x03
198 #define SCSI_ASCQ_FORMAT_IN_PROGRESS 0x04
199 #define SCSI_ASCQ_POWER_LOSS_EXPECTED 0x08
200 #define SCSI_ASCQ_INVALID_LUN_ID 0x09
202 /* copied from drivers/usb/gadget/function/storage_common.h */
203 static inline u32
get_unaligned_be24(u8
*buf
)
205 return 0xffffff & (u32
) get_unaligned_be32(buf
- 1);
208 /* Struct to gather data that needs to be extracted from a SCSI CDB.
209 Not conforming to any particular CDB variant, but compatible with all. */
211 struct nvme_trans_io_cdb
{
219 /* Internal Helper Functions */
222 /* Copy data to userspace memory */
224 static int nvme_trans_copy_to_user(struct sg_io_hdr
*hdr
, void *from
,
229 size_t remaining
= n
;
232 if (hdr
->iovec_count
> 0) {
235 for (i
= 0; i
< hdr
->iovec_count
; i
++) {
236 if (copy_from_user(&sgl
, hdr
->dxferp
+
237 i
* sizeof(struct sg_iovec
),
238 sizeof(struct sg_iovec
)))
240 xfer_len
= min(remaining
, sgl
.iov_len
);
241 if (copy_to_user(sgl
.iov_base
, index
, xfer_len
))
245 remaining
-= xfer_len
;
252 if (copy_to_user(hdr
->dxferp
, from
, n
))
257 /* Copy data from userspace memory */
259 static int nvme_trans_copy_from_user(struct sg_io_hdr
*hdr
, void *to
,
264 size_t remaining
= n
;
267 if (hdr
->iovec_count
> 0) {
270 for (i
= 0; i
< hdr
->iovec_count
; i
++) {
271 if (copy_from_user(&sgl
, hdr
->dxferp
+
272 i
* sizeof(struct sg_iovec
),
273 sizeof(struct sg_iovec
)))
275 xfer_len
= min(remaining
, sgl
.iov_len
);
276 if (copy_from_user(index
, sgl
.iov_base
, xfer_len
))
279 remaining
-= xfer_len
;
286 if (copy_from_user(to
, hdr
->dxferp
, n
))
291 /* Status/Sense Buffer Writeback */
293 static int nvme_trans_completion(struct sg_io_hdr
*hdr
, u8 status
, u8 sense_key
,
297 u8 resp
[DESC_FMT_SENSE_DATA_SIZE
];
299 if (scsi_status_is_good(status
)) {
300 hdr
->status
= SAM_STAT_GOOD
;
301 hdr
->masked_status
= GOOD
;
302 hdr
->host_status
= DID_OK
;
303 hdr
->driver_status
= DRIVER_OK
;
306 hdr
->status
= status
;
307 hdr
->masked_status
= status
>> 1;
308 hdr
->host_status
= DID_OK
;
309 hdr
->driver_status
= DRIVER_OK
;
311 memset(resp
, 0, DESC_FMT_SENSE_DATA_SIZE
);
312 resp
[0] = DESC_FORMAT_SENSE_DATA
;
317 xfer_len
= min_t(u8
, hdr
->mx_sb_len
, DESC_FMT_SENSE_DATA_SIZE
);
318 hdr
->sb_len_wr
= xfer_len
;
319 if (copy_to_user(hdr
->sbp
, resp
, xfer_len
) > 0)
327 * Take a status code from a lowlevel routine, and if it was a positive NVMe
328 * error code update the sense data based on it. In either case the passed
329 * in value is returned again, unless an -EFAULT from copy_to_user overrides
332 static int nvme_trans_status_code(struct sg_io_hdr
*hdr
, int nvme_sc
)
334 u8 status
, sense_key
, asc
, ascq
;
337 /* For non-nvme (Linux) errors, simply return the error code */
341 /* Mask DNR, More, and reserved fields */
342 switch (nvme_sc
& 0x7FF) {
343 /* Generic Command Status */
344 case NVME_SC_SUCCESS
:
345 status
= SAM_STAT_GOOD
;
346 sense_key
= NO_SENSE
;
347 asc
= SCSI_ASC_NO_SENSE
;
348 ascq
= SCSI_ASCQ_CAUSE_NOT_REPORTABLE
;
350 case NVME_SC_INVALID_OPCODE
:
351 status
= SAM_STAT_CHECK_CONDITION
;
352 sense_key
= ILLEGAL_REQUEST
;
353 asc
= SCSI_ASC_ILLEGAL_COMMAND
;
354 ascq
= SCSI_ASCQ_CAUSE_NOT_REPORTABLE
;
356 case NVME_SC_INVALID_FIELD
:
357 status
= SAM_STAT_CHECK_CONDITION
;
358 sense_key
= ILLEGAL_REQUEST
;
359 asc
= SCSI_ASC_INVALID_CDB
;
360 ascq
= SCSI_ASCQ_CAUSE_NOT_REPORTABLE
;
362 case NVME_SC_DATA_XFER_ERROR
:
363 status
= SAM_STAT_CHECK_CONDITION
;
364 sense_key
= MEDIUM_ERROR
;
365 asc
= SCSI_ASC_NO_SENSE
;
366 ascq
= SCSI_ASCQ_CAUSE_NOT_REPORTABLE
;
368 case NVME_SC_POWER_LOSS
:
369 status
= SAM_STAT_TASK_ABORTED
;
370 sense_key
= ABORTED_COMMAND
;
371 asc
= SCSI_ASC_WARNING
;
372 ascq
= SCSI_ASCQ_POWER_LOSS_EXPECTED
;
374 case NVME_SC_INTERNAL
:
375 status
= SAM_STAT_CHECK_CONDITION
;
376 sense_key
= HARDWARE_ERROR
;
377 asc
= SCSI_ASC_INTERNAL_TARGET_FAILURE
;
378 ascq
= SCSI_ASCQ_CAUSE_NOT_REPORTABLE
;
380 case NVME_SC_ABORT_REQ
:
381 status
= SAM_STAT_TASK_ABORTED
;
382 sense_key
= ABORTED_COMMAND
;
383 asc
= SCSI_ASC_NO_SENSE
;
384 ascq
= SCSI_ASCQ_CAUSE_NOT_REPORTABLE
;
386 case NVME_SC_ABORT_QUEUE
:
387 status
= SAM_STAT_TASK_ABORTED
;
388 sense_key
= ABORTED_COMMAND
;
389 asc
= SCSI_ASC_NO_SENSE
;
390 ascq
= SCSI_ASCQ_CAUSE_NOT_REPORTABLE
;
392 case NVME_SC_FUSED_FAIL
:
393 status
= SAM_STAT_TASK_ABORTED
;
394 sense_key
= ABORTED_COMMAND
;
395 asc
= SCSI_ASC_NO_SENSE
;
396 ascq
= SCSI_ASCQ_CAUSE_NOT_REPORTABLE
;
398 case NVME_SC_FUSED_MISSING
:
399 status
= SAM_STAT_TASK_ABORTED
;
400 sense_key
= ABORTED_COMMAND
;
401 asc
= SCSI_ASC_NO_SENSE
;
402 ascq
= SCSI_ASCQ_CAUSE_NOT_REPORTABLE
;
404 case NVME_SC_INVALID_NS
:
405 status
= SAM_STAT_CHECK_CONDITION
;
406 sense_key
= ILLEGAL_REQUEST
;
407 asc
= SCSI_ASC_ACCESS_DENIED_INVALID_LUN_ID
;
408 ascq
= SCSI_ASCQ_INVALID_LUN_ID
;
410 case NVME_SC_LBA_RANGE
:
411 status
= SAM_STAT_CHECK_CONDITION
;
412 sense_key
= ILLEGAL_REQUEST
;
413 asc
= SCSI_ASC_ILLEGAL_BLOCK
;
414 ascq
= SCSI_ASCQ_CAUSE_NOT_REPORTABLE
;
416 case NVME_SC_CAP_EXCEEDED
:
417 status
= SAM_STAT_CHECK_CONDITION
;
418 sense_key
= MEDIUM_ERROR
;
419 asc
= SCSI_ASC_NO_SENSE
;
420 ascq
= SCSI_ASCQ_CAUSE_NOT_REPORTABLE
;
422 case NVME_SC_NS_NOT_READY
:
423 status
= SAM_STAT_CHECK_CONDITION
;
424 sense_key
= NOT_READY
;
425 asc
= SCSI_ASC_LUN_NOT_READY
;
426 ascq
= SCSI_ASCQ_CAUSE_NOT_REPORTABLE
;
429 /* Command Specific Status */
430 case NVME_SC_INVALID_FORMAT
:
431 status
= SAM_STAT_CHECK_CONDITION
;
432 sense_key
= ILLEGAL_REQUEST
;
433 asc
= SCSI_ASC_FORMAT_COMMAND_FAILED
;
434 ascq
= SCSI_ASCQ_FORMAT_COMMAND_FAILED
;
436 case NVME_SC_BAD_ATTRIBUTES
:
437 status
= SAM_STAT_CHECK_CONDITION
;
438 sense_key
= ILLEGAL_REQUEST
;
439 asc
= SCSI_ASC_INVALID_CDB
;
440 ascq
= SCSI_ASCQ_CAUSE_NOT_REPORTABLE
;
444 case NVME_SC_WRITE_FAULT
:
445 status
= SAM_STAT_CHECK_CONDITION
;
446 sense_key
= MEDIUM_ERROR
;
447 asc
= SCSI_ASC_PERIPHERAL_DEV_WRITE_FAULT
;
448 ascq
= SCSI_ASCQ_CAUSE_NOT_REPORTABLE
;
450 case NVME_SC_READ_ERROR
:
451 status
= SAM_STAT_CHECK_CONDITION
;
452 sense_key
= MEDIUM_ERROR
;
453 asc
= SCSI_ASC_UNRECOVERED_READ_ERROR
;
454 ascq
= SCSI_ASCQ_CAUSE_NOT_REPORTABLE
;
456 case NVME_SC_GUARD_CHECK
:
457 status
= SAM_STAT_CHECK_CONDITION
;
458 sense_key
= MEDIUM_ERROR
;
459 asc
= SCSI_ASC_LOG_BLOCK_GUARD_CHECK_FAILED
;
460 ascq
= SCSI_ASCQ_LOG_BLOCK_GUARD_CHECK_FAILED
;
462 case NVME_SC_APPTAG_CHECK
:
463 status
= SAM_STAT_CHECK_CONDITION
;
464 sense_key
= MEDIUM_ERROR
;
465 asc
= SCSI_ASC_LOG_BLOCK_APPTAG_CHECK_FAILED
;
466 ascq
= SCSI_ASCQ_LOG_BLOCK_APPTAG_CHECK_FAILED
;
468 case NVME_SC_REFTAG_CHECK
:
469 status
= SAM_STAT_CHECK_CONDITION
;
470 sense_key
= MEDIUM_ERROR
;
471 asc
= SCSI_ASC_LOG_BLOCK_REFTAG_CHECK_FAILED
;
472 ascq
= SCSI_ASCQ_LOG_BLOCK_REFTAG_CHECK_FAILED
;
474 case NVME_SC_COMPARE_FAILED
:
475 status
= SAM_STAT_CHECK_CONDITION
;
476 sense_key
= MISCOMPARE
;
477 asc
= SCSI_ASC_MISCOMPARE_DURING_VERIFY
;
478 ascq
= SCSI_ASCQ_CAUSE_NOT_REPORTABLE
;
480 case NVME_SC_ACCESS_DENIED
:
481 status
= SAM_STAT_CHECK_CONDITION
;
482 sense_key
= ILLEGAL_REQUEST
;
483 asc
= SCSI_ASC_ACCESS_DENIED_INVALID_LUN_ID
;
484 ascq
= SCSI_ASCQ_INVALID_LUN_ID
;
487 /* Unspecified/Default */
488 case NVME_SC_CMDID_CONFLICT
:
489 case NVME_SC_CMD_SEQ_ERROR
:
490 case NVME_SC_CQ_INVALID
:
491 case NVME_SC_QID_INVALID
:
492 case NVME_SC_QUEUE_SIZE
:
493 case NVME_SC_ABORT_LIMIT
:
494 case NVME_SC_ABORT_MISSING
:
495 case NVME_SC_ASYNC_LIMIT
:
496 case NVME_SC_FIRMWARE_SLOT
:
497 case NVME_SC_FIRMWARE_IMAGE
:
498 case NVME_SC_INVALID_VECTOR
:
499 case NVME_SC_INVALID_LOG_PAGE
:
501 status
= SAM_STAT_CHECK_CONDITION
;
502 sense_key
= ILLEGAL_REQUEST
;
503 asc
= SCSI_ASC_NO_SENSE
;
504 ascq
= SCSI_ASCQ_CAUSE_NOT_REPORTABLE
;
508 res
= nvme_trans_completion(hdr
, status
, sense_key
, asc
, ascq
);
509 return res
? res
: nvme_sc
;
512 /* INQUIRY Helper Functions */
514 static int nvme_trans_standard_inquiry_page(struct nvme_ns
*ns
,
515 struct sg_io_hdr
*hdr
, u8
*inq_response
,
518 struct nvme_ctrl
*ctrl
= ns
->ctrl
;
519 struct nvme_id_ns
*id_ns
;
523 u8 resp_data_format
= 0x02;
525 u8 cmdque
= 0x01 << 1;
526 u8 fw_offset
= sizeof(ctrl
->firmware_rev
);
528 /* nvme ns identify - use DPS value for PROTECT field */
529 nvme_sc
= nvme_identify_ns(ctrl
, ns
->ns_id
, &id_ns
);
530 res
= nvme_trans_status_code(hdr
, nvme_sc
);
540 memset(inq_response
, 0, STANDARD_INQUIRY_LENGTH
);
541 inq_response
[2] = VERSION_SPC_4
;
542 inq_response
[3] = resp_data_format
; /*normaca=0 | hisup=0 */
543 inq_response
[4] = ADDITIONAL_STD_INQ_LENGTH
;
544 inq_response
[5] = protect
; /* sccs=0 | acc=0 | tpgs=0 | pc3=0 */
545 inq_response
[7] = cmdque
; /* wbus16=0 | sync=0 | vs=0 */
546 strncpy(&inq_response
[8], "NVMe ", 8);
547 strncpy(&inq_response
[16], ctrl
->model
, 16);
549 while (ctrl
->firmware_rev
[fw_offset
- 1] == ' ' && fw_offset
> 4)
552 strncpy(&inq_response
[32], ctrl
->firmware_rev
+ fw_offset
, 4);
554 xfer_len
= min(alloc_len
, STANDARD_INQUIRY_LENGTH
);
555 return nvme_trans_copy_to_user(hdr
, inq_response
, xfer_len
);
558 static int nvme_trans_supported_vpd_pages(struct nvme_ns
*ns
,
559 struct sg_io_hdr
*hdr
, u8
*inq_response
,
564 memset(inq_response
, 0, STANDARD_INQUIRY_LENGTH
);
565 inq_response
[1] = INQ_SUPPORTED_VPD_PAGES_PAGE
; /* Page Code */
566 inq_response
[3] = INQ_NUM_SUPPORTED_VPD_PAGES
; /* Page Length */
567 inq_response
[4] = INQ_SUPPORTED_VPD_PAGES_PAGE
;
568 inq_response
[5] = INQ_UNIT_SERIAL_NUMBER_PAGE
;
569 inq_response
[6] = INQ_DEVICE_IDENTIFICATION_PAGE
;
570 inq_response
[7] = INQ_EXTENDED_INQUIRY_DATA_PAGE
;
571 inq_response
[8] = INQ_BDEV_CHARACTERISTICS_PAGE
;
572 inq_response
[9] = INQ_BDEV_LIMITS_PAGE
;
574 xfer_len
= min(alloc_len
, STANDARD_INQUIRY_LENGTH
);
575 return nvme_trans_copy_to_user(hdr
, inq_response
, xfer_len
);
578 static int nvme_trans_unit_serial_page(struct nvme_ns
*ns
,
579 struct sg_io_hdr
*hdr
, u8
*inq_response
,
584 memset(inq_response
, 0, STANDARD_INQUIRY_LENGTH
);
585 inq_response
[1] = INQ_UNIT_SERIAL_NUMBER_PAGE
; /* Page Code */
586 inq_response
[3] = INQ_SERIAL_NUMBER_LENGTH
; /* Page Length */
587 strncpy(&inq_response
[4], ns
->ctrl
->serial
, INQ_SERIAL_NUMBER_LENGTH
);
589 xfer_len
= min(alloc_len
, STANDARD_INQUIRY_LENGTH
);
590 return nvme_trans_copy_to_user(hdr
, inq_response
, xfer_len
);
593 static int nvme_fill_device_id_eui64(struct nvme_ns
*ns
, struct sg_io_hdr
*hdr
,
594 u8
*inq_response
, int alloc_len
)
596 struct nvme_id_ns
*id_ns
;
601 nvme_sc
= nvme_identify_ns(ns
->ctrl
, ns
->ns_id
, &id_ns
);
602 res
= nvme_trans_status_code(hdr
, nvme_sc
);
607 len
= sizeof(id_ns
->eui64
);
609 if (ns
->ctrl
->vs
>= NVME_VS(1, 2)) {
610 if (bitmap_empty(eui
, len
* 8)) {
612 len
= sizeof(id_ns
->nguid
);
616 if (bitmap_empty(eui
, len
* 8)) {
621 memset(inq_response
, 0, alloc_len
);
622 inq_response
[1] = INQ_DEVICE_IDENTIFICATION_PAGE
;
623 inq_response
[3] = 4 + len
; /* Page Length */
625 /* Designation Descriptor start */
626 inq_response
[4] = 0x01; /* Proto ID=0h | Code set=1h */
627 inq_response
[5] = 0x02; /* PIV=0b | Asso=00b | Designator Type=2h */
628 inq_response
[6] = 0x00; /* Rsvd */
629 inq_response
[7] = len
; /* Designator Length */
630 memcpy(&inq_response
[8], eui
, len
);
632 res
= nvme_trans_copy_to_user(hdr
, inq_response
, alloc_len
);
638 static int nvme_fill_device_id_scsi_string(struct nvme_ns
*ns
,
639 struct sg_io_hdr
*hdr
, u8
*inq_response
, int alloc_len
)
641 struct nvme_ctrl
*ctrl
= ns
->ctrl
;
642 struct nvme_id_ctrl
*id_ctrl
;
645 if (alloc_len
< 72) {
646 return nvme_trans_completion(hdr
,
647 SAM_STAT_CHECK_CONDITION
,
648 ILLEGAL_REQUEST
, SCSI_ASC_INVALID_CDB
,
649 SCSI_ASCQ_CAUSE_NOT_REPORTABLE
);
652 nvme_sc
= nvme_identify_ctrl(ctrl
, &id_ctrl
);
653 res
= nvme_trans_status_code(hdr
, nvme_sc
);
657 memset(inq_response
, 0, alloc_len
);
658 inq_response
[1] = INQ_DEVICE_IDENTIFICATION_PAGE
;
659 inq_response
[3] = 0x48; /* Page Length */
661 /* Designation Descriptor start */
662 inq_response
[4] = 0x03; /* Proto ID=0h | Code set=3h */
663 inq_response
[5] = 0x08; /* PIV=0b | Asso=00b | Designator Type=8h */
664 inq_response
[6] = 0x00; /* Rsvd */
665 inq_response
[7] = 0x44; /* Designator Length */
667 sprintf(&inq_response
[8], "%04x", le16_to_cpu(id_ctrl
->vid
));
668 memcpy(&inq_response
[12], ctrl
->model
, sizeof(ctrl
->model
));
669 sprintf(&inq_response
[52], "%04x", cpu_to_be32(ns
->ns_id
));
670 memcpy(&inq_response
[56], ctrl
->serial
, sizeof(ctrl
->serial
));
672 res
= nvme_trans_copy_to_user(hdr
, inq_response
, alloc_len
);
677 static int nvme_trans_device_id_page(struct nvme_ns
*ns
, struct sg_io_hdr
*hdr
,
678 u8
*resp
, int alloc_len
)
682 if (ns
->ctrl
->vs
>= NVME_VS(1, 1)) {
683 res
= nvme_fill_device_id_eui64(ns
, hdr
, resp
, alloc_len
);
684 if (res
!= -EOPNOTSUPP
)
688 return nvme_fill_device_id_scsi_string(ns
, hdr
, resp
, alloc_len
);
691 static int nvme_trans_ext_inq_page(struct nvme_ns
*ns
, struct sg_io_hdr
*hdr
,
697 struct nvme_ctrl
*ctrl
= ns
->ctrl
;
698 struct nvme_id_ctrl
*id_ctrl
;
699 struct nvme_id_ns
*id_ns
;
703 u8 spt_lut
[8] = {0, 0, 2, 1, 4, 6, 5, 7};
704 u8 grd_chk
, app_chk
, ref_chk
, protect
;
709 inq_response
= kmalloc(EXTENDED_INQUIRY_DATA_PAGE_LENGTH
, GFP_KERNEL
);
710 if (inq_response
== NULL
)
713 nvme_sc
= nvme_identify_ns(ctrl
, ns
->ns_id
, &id_ns
);
714 res
= nvme_trans_status_code(hdr
, nvme_sc
);
718 spt
= spt_lut
[id_ns
->dpc
& 0x07] << 3;
725 grd_chk
= protect
<< 2;
726 app_chk
= protect
<< 1;
729 nvme_sc
= nvme_identify_ctrl(ctrl
, &id_ctrl
);
730 res
= nvme_trans_status_code(hdr
, nvme_sc
);
734 v_sup
= id_ctrl
->vwc
;
737 memset(inq_response
, 0, EXTENDED_INQUIRY_DATA_PAGE_LENGTH
);
738 inq_response
[1] = INQ_EXTENDED_INQUIRY_DATA_PAGE
; /* Page Code */
739 inq_response
[2] = 0x00; /* Page Length MSB */
740 inq_response
[3] = 0x3C; /* Page Length LSB */
741 inq_response
[4] = microcode
| spt
| grd_chk
| app_chk
| ref_chk
;
742 inq_response
[5] = uask_sup
;
743 inq_response
[6] = v_sup
;
744 inq_response
[7] = luiclr
;
748 xfer_len
= min(alloc_len
, EXTENDED_INQUIRY_DATA_PAGE_LENGTH
);
749 res
= nvme_trans_copy_to_user(hdr
, inq_response
, xfer_len
);
756 static int nvme_trans_bdev_limits_page(struct nvme_ns
*ns
, struct sg_io_hdr
*hdr
,
757 u8
*inq_response
, int alloc_len
)
759 __be32 max_sectors
= cpu_to_be32(
760 nvme_block_nr(ns
, queue_max_hw_sectors(ns
->queue
)));
761 __be32 max_discard
= cpu_to_be32(ns
->queue
->limits
.max_discard_sectors
);
762 __be32 discard_desc_count
= cpu_to_be32(0x100);
764 memset(inq_response
, 0, STANDARD_INQUIRY_LENGTH
);
765 inq_response
[1] = VPD_BLOCK_LIMITS
;
766 inq_response
[3] = 0x3c; /* Page Length */
767 memcpy(&inq_response
[8], &max_sectors
, sizeof(u32
));
768 memcpy(&inq_response
[20], &max_discard
, sizeof(u32
));
771 memcpy(&inq_response
[24], &discard_desc_count
, sizeof(u32
));
773 return nvme_trans_copy_to_user(hdr
, inq_response
, 0x3c);
776 static int nvme_trans_bdev_char_page(struct nvme_ns
*ns
, struct sg_io_hdr
*hdr
,
783 inq_response
= kzalloc(EXTENDED_INQUIRY_DATA_PAGE_LENGTH
, GFP_KERNEL
);
784 if (inq_response
== NULL
) {
789 inq_response
[1] = INQ_BDEV_CHARACTERISTICS_PAGE
; /* Page Code */
790 inq_response
[2] = 0x00; /* Page Length MSB */
791 inq_response
[3] = 0x3C; /* Page Length LSB */
792 inq_response
[4] = 0x00; /* Medium Rotation Rate MSB */
793 inq_response
[5] = 0x01; /* Medium Rotation Rate LSB */
794 inq_response
[6] = 0x00; /* Form Factor */
796 xfer_len
= min(alloc_len
, EXTENDED_INQUIRY_DATA_PAGE_LENGTH
);
797 res
= nvme_trans_copy_to_user(hdr
, inq_response
, xfer_len
);
804 /* LOG SENSE Helper Functions */
806 static int nvme_trans_log_supp_pages(struct nvme_ns
*ns
, struct sg_io_hdr
*hdr
,
813 log_response
= kzalloc(LOG_PAGE_SUPPORTED_LOG_PAGES_LENGTH
, GFP_KERNEL
);
814 if (log_response
== NULL
) {
819 log_response
[0] = LOG_PAGE_SUPPORTED_LOG_PAGES_PAGE
;
820 /* Subpage=0x00, Page Length MSB=0 */
821 log_response
[3] = SUPPORTED_LOG_PAGES_PAGE_LENGTH
;
822 log_response
[4] = LOG_PAGE_SUPPORTED_LOG_PAGES_PAGE
;
823 log_response
[5] = LOG_PAGE_INFORMATIONAL_EXCEPTIONS_PAGE
;
824 log_response
[6] = LOG_PAGE_TEMPERATURE_PAGE
;
826 xfer_len
= min(alloc_len
, LOG_PAGE_SUPPORTED_LOG_PAGES_LENGTH
);
827 res
= nvme_trans_copy_to_user(hdr
, log_response
, xfer_len
);
834 static int nvme_trans_log_info_exceptions(struct nvme_ns
*ns
,
835 struct sg_io_hdr
*hdr
, int alloc_len
)
840 struct nvme_smart_log
*smart_log
;
844 log_response
= kzalloc(LOG_INFO_EXCP_PAGE_LENGTH
, GFP_KERNEL
);
845 if (log_response
== NULL
)
848 res
= nvme_get_log_page(ns
->ctrl
, &smart_log
);
850 goto out_free_response
;
852 if (res
!= NVME_SC_SUCCESS
) {
853 temp_c
= LOG_TEMP_UNKNOWN
;
855 temp_k
= (smart_log
->temperature
[1] << 8) +
856 (smart_log
->temperature
[0]);
857 temp_c
= temp_k
- KELVIN_TEMP_FACTOR
;
861 log_response
[0] = LOG_PAGE_INFORMATIONAL_EXCEPTIONS_PAGE
;
862 /* Subpage=0x00, Page Length MSB=0 */
863 log_response
[3] = REMAINING_INFO_EXCP_PAGE_LENGTH
;
864 /* Informational Exceptions Log Parameter 1 Start */
865 /* Parameter Code=0x0000 bytes 4,5 */
866 log_response
[6] = 0x23; /* DU=0, TSD=1, ETC=0, TMC=0, FMT_AND_LNK=11b */
867 log_response
[7] = 0x04; /* PARAMETER LENGTH */
868 /* Add sense Code and qualifier = 0x00 each */
869 /* Use Temperature from NVMe Get Log Page, convert to C from K */
870 log_response
[10] = temp_c
;
872 xfer_len
= min(alloc_len
, LOG_INFO_EXCP_PAGE_LENGTH
);
873 res
= nvme_trans_copy_to_user(hdr
, log_response
, xfer_len
);
880 static int nvme_trans_log_temperature(struct nvme_ns
*ns
, struct sg_io_hdr
*hdr
,
886 struct nvme_smart_log
*smart_log
;
888 u8 temp_c_cur
, temp_c_thresh
;
891 log_response
= kzalloc(LOG_TEMP_PAGE_LENGTH
, GFP_KERNEL
);
892 if (log_response
== NULL
)
895 res
= nvme_get_log_page(ns
->ctrl
, &smart_log
);
897 goto out_free_response
;
899 if (res
!= NVME_SC_SUCCESS
) {
900 temp_c_cur
= LOG_TEMP_UNKNOWN
;
902 temp_k
= (smart_log
->temperature
[1] << 8) +
903 (smart_log
->temperature
[0]);
904 temp_c_cur
= temp_k
- KELVIN_TEMP_FACTOR
;
908 /* Get Features for Temp Threshold */
909 res
= nvme_get_features(ns
->ctrl
, NVME_FEAT_TEMP_THRESH
, 0, NULL
, 0,
911 if (res
!= NVME_SC_SUCCESS
)
912 temp_c_thresh
= LOG_TEMP_UNKNOWN
;
914 temp_c_thresh
= (feature_resp
& 0xFFFF) - KELVIN_TEMP_FACTOR
;
916 log_response
[0] = LOG_PAGE_TEMPERATURE_PAGE
;
917 /* Subpage=0x00, Page Length MSB=0 */
918 log_response
[3] = REMAINING_TEMP_PAGE_LENGTH
;
919 /* Temperature Log Parameter 1 (Temperature) Start */
920 /* Parameter Code = 0x0000 */
921 log_response
[6] = 0x01; /* Format and Linking = 01b */
922 log_response
[7] = 0x02; /* Parameter Length */
923 /* Use Temperature from NVMe Get Log Page, convert to C from K */
924 log_response
[9] = temp_c_cur
;
925 /* Temperature Log Parameter 2 (Reference Temperature) Start */
926 log_response
[11] = 0x01; /* Parameter Code = 0x0001 */
927 log_response
[12] = 0x01; /* Format and Linking = 01b */
928 log_response
[13] = 0x02; /* Parameter Length */
929 /* Use Temperature Thresh from NVMe Get Log Page, convert to C from K */
930 log_response
[15] = temp_c_thresh
;
932 xfer_len
= min(alloc_len
, LOG_TEMP_PAGE_LENGTH
);
933 res
= nvme_trans_copy_to_user(hdr
, log_response
, xfer_len
);
940 /* MODE SENSE Helper Functions */
942 static int nvme_trans_fill_mode_parm_hdr(u8
*resp
, int len
, u8 cdb10
, u8 llbaa
,
943 u16 mode_data_length
, u16 blk_desc_len
)
945 /* Quick check to make sure I don't stomp on my own memory... */
946 if ((cdb10
&& len
< 8) || (!cdb10
&& len
< 4))
950 resp
[0] = (mode_data_length
& 0xFF00) >> 8;
951 resp
[1] = (mode_data_length
& 0x00FF);
952 resp
[3] = 0x10 /* DPOFUA */;
954 resp
[5] = RESERVED_FIELD
;
955 resp
[6] = (blk_desc_len
& 0xFF00) >> 8;
956 resp
[7] = (blk_desc_len
& 0x00FF);
958 resp
[0] = (mode_data_length
& 0x00FF);
959 resp
[2] = 0x10 /* DPOFUA */;
960 resp
[3] = (blk_desc_len
& 0x00FF);
966 static int nvme_trans_fill_blk_desc(struct nvme_ns
*ns
, struct sg_io_hdr
*hdr
,
967 u8
*resp
, int len
, u8 llbaa
)
971 struct nvme_id_ns
*id_ns
;
975 if (llbaa
== 0 && len
< MODE_PAGE_BLK_DES_LEN
)
977 else if (llbaa
> 0 && len
< MODE_PAGE_LLBAA_BLK_DES_LEN
)
980 nvme_sc
= nvme_identify_ns(ns
->ctrl
, ns
->ns_id
, &id_ns
);
981 res
= nvme_trans_status_code(hdr
, nvme_sc
);
985 flbas
= (id_ns
->flbas
) & 0x0F;
986 lba_length
= (1 << (id_ns
->lbaf
[flbas
].ds
));
989 __be32 tmp_cap
= cpu_to_be32(le64_to_cpu(id_ns
->ncap
));
990 /* Byte 4 is reserved */
991 __be32 tmp_len
= cpu_to_be32(lba_length
& 0x00FFFFFF);
993 memcpy(resp
, &tmp_cap
, sizeof(u32
));
994 memcpy(&resp
[4], &tmp_len
, sizeof(u32
));
996 __be64 tmp_cap
= cpu_to_be64(le64_to_cpu(id_ns
->ncap
));
997 __be32 tmp_len
= cpu_to_be32(lba_length
);
999 memcpy(resp
, &tmp_cap
, sizeof(u64
));
1000 /* Bytes 8, 9, 10, 11 are reserved */
1001 memcpy(&resp
[12], &tmp_len
, sizeof(u32
));
1008 static int nvme_trans_fill_control_page(struct nvme_ns
*ns
,
1009 struct sg_io_hdr
*hdr
, u8
*resp
,
1012 if (len
< MODE_PAGE_CONTROL_LEN
)
1015 resp
[0] = MODE_PAGE_CONTROL
;
1016 resp
[1] = MODE_PAGE_CONTROL_LEN_FIELD
;
1017 resp
[2] = 0x0E; /* TST=000b, TMF_ONLY=0, DPICZ=1,
1018 * D_SENSE=1, GLTSD=1, RLEC=0 */
1019 resp
[3] = 0x12; /* Q_ALGO_MODIFIER=1h, NUAR=0, QERR=01b */
1020 /* Byte 4: VS=0, RAC=0, UA_INT=0, SWP=0 */
1021 resp
[5] = 0x40; /* ATO=0, TAS=1, ATMPE=0, RWWP=0, AUTOLOAD=0 */
1022 /* resp[6] and [7] are obsolete, thus zero */
1023 resp
[8] = 0xFF; /* Busy timeout period = 0xffff */
1025 /* Bytes 10,11: Extended selftest completion time = 0x0000 */
1030 static int nvme_trans_fill_caching_page(struct nvme_ns
*ns
,
1031 struct sg_io_hdr
*hdr
,
1039 if (len
< MODE_PAGE_CACHING_LEN
)
1042 nvme_sc
= nvme_get_features(ns
->ctrl
, NVME_FEAT_VOLATILE_WC
, 0, NULL
, 0,
1044 res
= nvme_trans_status_code(hdr
, nvme_sc
);
1048 vwc
= feature_resp
& 0x00000001;
1050 resp
[0] = MODE_PAGE_CACHING
;
1051 resp
[1] = MODE_PAGE_CACHING_LEN_FIELD
;
1056 static int nvme_trans_fill_pow_cnd_page(struct nvme_ns
*ns
,
1057 struct sg_io_hdr
*hdr
, u8
*resp
,
1060 if (len
< MODE_PAGE_POW_CND_LEN
)
1063 resp
[0] = MODE_PAGE_POWER_CONDITION
;
1064 resp
[1] = MODE_PAGE_POW_CND_LEN_FIELD
;
1065 /* All other bytes are zero */
1070 static int nvme_trans_fill_inf_exc_page(struct nvme_ns
*ns
,
1071 struct sg_io_hdr
*hdr
, u8
*resp
,
1074 if (len
< MODE_PAGE_INF_EXC_LEN
)
1077 resp
[0] = MODE_PAGE_INFO_EXCEP
;
1078 resp
[1] = MODE_PAGE_INF_EXC_LEN_FIELD
;
1080 /* All other bytes are zero */
1085 static int nvme_trans_fill_all_pages(struct nvme_ns
*ns
, struct sg_io_hdr
*hdr
,
1089 u16 mode_pages_offset_1
= 0;
1090 u16 mode_pages_offset_2
, mode_pages_offset_3
, mode_pages_offset_4
;
1092 mode_pages_offset_2
= mode_pages_offset_1
+ MODE_PAGE_CACHING_LEN
;
1093 mode_pages_offset_3
= mode_pages_offset_2
+ MODE_PAGE_CONTROL_LEN
;
1094 mode_pages_offset_4
= mode_pages_offset_3
+ MODE_PAGE_POW_CND_LEN
;
1096 res
= nvme_trans_fill_caching_page(ns
, hdr
, &resp
[mode_pages_offset_1
],
1097 MODE_PAGE_CACHING_LEN
);
1100 res
= nvme_trans_fill_control_page(ns
, hdr
, &resp
[mode_pages_offset_2
],
1101 MODE_PAGE_CONTROL_LEN
);
1104 res
= nvme_trans_fill_pow_cnd_page(ns
, hdr
, &resp
[mode_pages_offset_3
],
1105 MODE_PAGE_POW_CND_LEN
);
1108 return nvme_trans_fill_inf_exc_page(ns
, hdr
, &resp
[mode_pages_offset_4
],
1109 MODE_PAGE_INF_EXC_LEN
);
1112 static inline int nvme_trans_get_blk_desc_len(u8 dbd
, u8 llbaa
)
1114 if (dbd
== MODE_SENSE_BLK_DESC_ENABLED
) {
1115 /* SPC-4: len = 8 x Num_of_descriptors if llbaa = 0, 16x if 1 */
1116 return 8 * (llbaa
+ 1) * MODE_SENSE_BLK_DESC_COUNT
;
1122 static int nvme_trans_mode_page_create(struct nvme_ns
*ns
,
1123 struct sg_io_hdr
*hdr
, u8
*cmd
,
1124 u16 alloc_len
, u8 cdb10
,
1125 int (*mode_page_fill_func
)
1127 struct sg_io_hdr
*hdr
, u8
*, int),
1128 u16 mode_pages_tot_len
)
1136 u16 mode_pages_offset_1
;
1137 u16 blk_desc_len
, blk_desc_offset
, mode_data_length
;
1139 dbd
= (cmd
[1] & MODE_SENSE_DBD_MASK
) >> MODE_SENSE_DBD_SHIFT
;
1140 llbaa
= (cmd
[1] & MODE_SENSE_LLBAA_MASK
) >> MODE_SENSE_LLBAA_SHIFT
;
1141 mph_size
= cdb10
? MODE_SENSE10_MPH_SIZE
: MODE_SENSE6_MPH_SIZE
;
1143 blk_desc_len
= nvme_trans_get_blk_desc_len(dbd
, llbaa
);
1145 resp_size
= mph_size
+ blk_desc_len
+ mode_pages_tot_len
;
1146 /* Refer spc4r34 Table 440 for calculation of Mode data Length field */
1147 mode_data_length
= 3 + (3 * cdb10
) + blk_desc_len
+ mode_pages_tot_len
;
1149 blk_desc_offset
= mph_size
;
1150 mode_pages_offset_1
= blk_desc_offset
+ blk_desc_len
;
1152 response
= kzalloc(resp_size
, GFP_KERNEL
);
1153 if (response
== NULL
) {
1158 res
= nvme_trans_fill_mode_parm_hdr(&response
[0], mph_size
, cdb10
,
1159 llbaa
, mode_data_length
, blk_desc_len
);
1162 if (blk_desc_len
> 0) {
1163 res
= nvme_trans_fill_blk_desc(ns
, hdr
,
1164 &response
[blk_desc_offset
],
1165 blk_desc_len
, llbaa
);
1169 res
= mode_page_fill_func(ns
, hdr
, &response
[mode_pages_offset_1
],
1170 mode_pages_tot_len
);
1174 xfer_len
= min(alloc_len
, resp_size
);
1175 res
= nvme_trans_copy_to_user(hdr
, response
, xfer_len
);
1183 /* Read Capacity Helper Functions */
1185 static void nvme_trans_fill_read_cap(u8
*response
, struct nvme_id_ns
*id_ns
,
1192 u8 p_type_lut
[4] = {0, 0, 1, 2};
1197 flbas
= (id_ns
->flbas
) & 0x0F;
1198 lba_length
= (1 << (id_ns
->lbaf
[flbas
].ds
));
1199 rlba
= le64_to_cpup(&id_ns
->nsze
) - 1;
1200 (id_ns
->dps
) ? (prot_en
= 0x01) : (prot_en
= 0);
1203 if (rlba
> 0xFFFFFFFF)
1205 tmp_rlba_32
= cpu_to_be32(rlba
);
1206 tmp_len
= cpu_to_be32(lba_length
);
1207 memcpy(response
, &tmp_rlba_32
, sizeof(u32
));
1208 memcpy(&response
[4], &tmp_len
, sizeof(u32
));
1210 tmp_rlba
= cpu_to_be64(rlba
);
1211 tmp_len
= cpu_to_be32(lba_length
);
1212 memcpy(response
, &tmp_rlba
, sizeof(u64
));
1213 memcpy(&response
[8], &tmp_len
, sizeof(u32
));
1214 response
[12] = (p_type_lut
[id_ns
->dps
& 0x3] << 1) | prot_en
;
1215 /* P_I_Exponent = 0x0 | LBPPBE = 0x0 */
1216 /* LBPME = 0 | LBPRZ = 0 | LALBA = 0x00 */
1217 /* Bytes 16-31 - Reserved */
1221 /* Start Stop Unit Helper Functions */
1223 static int nvme_trans_send_activate_fw_cmd(struct nvme_ns
*ns
, struct sg_io_hdr
*hdr
,
1226 struct nvme_command c
;
1229 memset(&c
, 0, sizeof(c
));
1230 c
.common
.opcode
= nvme_admin_activate_fw
;
1231 c
.common
.cdw10
[0] = cpu_to_le32(buffer_id
| NVME_FWACT_REPL_ACTV
);
1233 nvme_sc
= nvme_submit_sync_cmd(ns
->queue
, &c
, NULL
, 0);
1234 return nvme_trans_status_code(hdr
, nvme_sc
);
1237 static int nvme_trans_send_download_fw_cmd(struct nvme_ns
*ns
, struct sg_io_hdr
*hdr
,
1238 u8 opcode
, u32 tot_len
, u32 offset
,
1242 struct nvme_command c
;
1244 if (hdr
->iovec_count
> 0) {
1245 /* Assuming SGL is not allowed for this command */
1246 return nvme_trans_completion(hdr
,
1247 SAM_STAT_CHECK_CONDITION
,
1249 SCSI_ASC_INVALID_CDB
,
1250 SCSI_ASCQ_CAUSE_NOT_REPORTABLE
);
1253 memset(&c
, 0, sizeof(c
));
1254 c
.common
.opcode
= nvme_admin_download_fw
;
1255 c
.dlfw
.numd
= cpu_to_le32((tot_len
/BYTES_TO_DWORDS
) - 1);
1256 c
.dlfw
.offset
= cpu_to_le32(offset
/BYTES_TO_DWORDS
);
1258 nvme_sc
= nvme_submit_user_cmd(ns
->ctrl
->admin_q
, &c
,
1259 hdr
->dxferp
, tot_len
, NULL
, 0);
1260 return nvme_trans_status_code(hdr
, nvme_sc
);
1263 /* Mode Select Helper Functions */
1265 static inline void nvme_trans_modesel_get_bd_len(u8
*parm_list
, u8 cdb10
,
1266 u16
*bd_len
, u8
*llbaa
)
1270 *bd_len
= (parm_list
[MODE_SELECT_10_BD_OFFSET
] << 8) +
1271 parm_list
[MODE_SELECT_10_BD_OFFSET
+ 1];
1272 *llbaa
= parm_list
[MODE_SELECT_10_LLBAA_OFFSET
] &
1273 MODE_SELECT_10_LLBAA_MASK
;
1276 *bd_len
= parm_list
[MODE_SELECT_6_BD_OFFSET
];
1280 static void nvme_trans_modesel_save_bd(struct nvme_ns
*ns
, u8
*parm_list
,
1281 u16 idx
, u16 bd_len
, u8 llbaa
)
1285 bd_num
= bd_len
/ ((llbaa
== 0) ?
1286 SHORT_DESC_BLOCK
: LONG_DESC_BLOCK
);
1287 /* Store block descriptor info if a FORMAT UNIT comes later */
1288 /* TODO Saving 1st BD info; what to do if multiple BD received? */
1290 /* Standard Block Descriptor - spc4r34 7.5.5.1 */
1291 ns
->mode_select_num_blocks
=
1292 (parm_list
[idx
+ 1] << 16) +
1293 (parm_list
[idx
+ 2] << 8) +
1294 (parm_list
[idx
+ 3]);
1296 ns
->mode_select_block_len
=
1297 (parm_list
[idx
+ 5] << 16) +
1298 (parm_list
[idx
+ 6] << 8) +
1299 (parm_list
[idx
+ 7]);
1301 /* Long LBA Block Descriptor - sbc3r27 6.4.2.3 */
1302 ns
->mode_select_num_blocks
=
1303 (((u64
)parm_list
[idx
+ 0]) << 56) +
1304 (((u64
)parm_list
[idx
+ 1]) << 48) +
1305 (((u64
)parm_list
[idx
+ 2]) << 40) +
1306 (((u64
)parm_list
[idx
+ 3]) << 32) +
1307 (((u64
)parm_list
[idx
+ 4]) << 24) +
1308 (((u64
)parm_list
[idx
+ 5]) << 16) +
1309 (((u64
)parm_list
[idx
+ 6]) << 8) +
1310 ((u64
)parm_list
[idx
+ 7]);
1312 ns
->mode_select_block_len
=
1313 (parm_list
[idx
+ 12] << 24) +
1314 (parm_list
[idx
+ 13] << 16) +
1315 (parm_list
[idx
+ 14] << 8) +
1316 (parm_list
[idx
+ 15]);
1320 static int nvme_trans_modesel_get_mp(struct nvme_ns
*ns
, struct sg_io_hdr
*hdr
,
1321 u8
*mode_page
, u8 page_code
)
1327 switch (page_code
) {
1328 case MODE_PAGE_CACHING
:
1329 dword11
= ((mode_page
[2] & CACHING_MODE_PAGE_WCE_MASK
) ? 1 : 0);
1330 nvme_sc
= nvme_set_features(ns
->ctrl
, NVME_FEAT_VOLATILE_WC
,
1331 dword11
, NULL
, 0, NULL
);
1332 res
= nvme_trans_status_code(hdr
, nvme_sc
);
1334 case MODE_PAGE_CONTROL
:
1336 case MODE_PAGE_POWER_CONDITION
:
1337 /* Verify the OS is not trying to set timers */
1338 if ((mode_page
[2] & 0x01) != 0 || (mode_page
[3] & 0x0F) != 0) {
1339 res
= nvme_trans_completion(hdr
,
1340 SAM_STAT_CHECK_CONDITION
,
1342 SCSI_ASC_INVALID_PARAMETER
,
1343 SCSI_ASCQ_CAUSE_NOT_REPORTABLE
);
1348 res
= nvme_trans_completion(hdr
, SAM_STAT_CHECK_CONDITION
,
1349 ILLEGAL_REQUEST
, SCSI_ASC_INVALID_CDB
,
1350 SCSI_ASCQ_CAUSE_NOT_REPORTABLE
);
1357 static int nvme_trans_modesel_data(struct nvme_ns
*ns
, struct sg_io_hdr
*hdr
,
1358 u8
*cmd
, u16 parm_list_len
, u8 pf
,
1365 u16 index
, saved_index
;
1369 /* Get parm list from data-in/out buffer */
1370 parm_list
= kmalloc(parm_list_len
, GFP_KERNEL
);
1371 if (parm_list
== NULL
) {
1376 res
= nvme_trans_copy_from_user(hdr
, parm_list
, parm_list_len
);
1380 nvme_trans_modesel_get_bd_len(parm_list
, cdb10
, &bd_len
, &llbaa
);
1381 index
= (cdb10
) ? (MODE_SELECT_10_MPH_SIZE
) : (MODE_SELECT_6_MPH_SIZE
);
1384 /* Block Descriptors present, parse */
1385 nvme_trans_modesel_save_bd(ns
, parm_list
, index
, bd_len
, llbaa
);
1388 saved_index
= index
;
1390 /* Multiple mode pages may be present; iterate through all */
1391 /* In 1st Iteration, don't do NVME Command, only check for CDB errors */
1393 page_code
= parm_list
[index
] & MODE_SELECT_PAGE_CODE_MASK
;
1394 mp_size
= parm_list
[index
+ 1] + 2;
1395 if ((page_code
!= MODE_PAGE_CACHING
) &&
1396 (page_code
!= MODE_PAGE_CONTROL
) &&
1397 (page_code
!= MODE_PAGE_POWER_CONDITION
)) {
1398 res
= nvme_trans_completion(hdr
,
1399 SAM_STAT_CHECK_CONDITION
,
1401 SCSI_ASC_INVALID_CDB
,
1402 SCSI_ASCQ_CAUSE_NOT_REPORTABLE
);
1406 } while (index
< parm_list_len
);
1408 /* In 2nd Iteration, do the NVME Commands */
1409 index
= saved_index
;
1411 page_code
= parm_list
[index
] & MODE_SELECT_PAGE_CODE_MASK
;
1412 mp_size
= parm_list
[index
+ 1] + 2;
1413 res
= nvme_trans_modesel_get_mp(ns
, hdr
, &parm_list
[index
],
1418 } while (index
< parm_list_len
);
1426 /* Format Unit Helper Functions */
1428 static int nvme_trans_fmt_set_blk_size_count(struct nvme_ns
*ns
,
1429 struct sg_io_hdr
*hdr
)
1436 * SCSI Expects a MODE SELECT would have been issued prior to
1437 * a FORMAT UNIT, and the block size and number would be used
1438 * from the block descriptor in it. If a MODE SELECT had not
1439 * been issued, FORMAT shall use the current values for both.
1442 if (ns
->mode_select_num_blocks
== 0 || ns
->mode_select_block_len
== 0) {
1443 struct nvme_id_ns
*id_ns
;
1445 nvme_sc
= nvme_identify_ns(ns
->ctrl
, ns
->ns_id
, &id_ns
);
1446 res
= nvme_trans_status_code(hdr
, nvme_sc
);
1450 if (ns
->mode_select_num_blocks
== 0)
1451 ns
->mode_select_num_blocks
= le64_to_cpu(id_ns
->ncap
);
1452 if (ns
->mode_select_block_len
== 0) {
1453 flbas
= (id_ns
->flbas
) & 0x0F;
1454 ns
->mode_select_block_len
=
1455 (1 << (id_ns
->lbaf
[flbas
].ds
));
1464 static int nvme_trans_fmt_get_parm_header(struct sg_io_hdr
*hdr
, u8 len
,
1465 u8 format_prot_info
, u8
*nvme_pf_code
)
1469 u8 pf_usage
, pf_code
;
1471 parm_list
= kmalloc(len
, GFP_KERNEL
);
1472 if (parm_list
== NULL
) {
1476 res
= nvme_trans_copy_from_user(hdr
, parm_list
, len
);
1480 if ((parm_list
[FORMAT_UNIT_IMMED_OFFSET
] &
1481 FORMAT_UNIT_IMMED_MASK
) != 0) {
1482 res
= nvme_trans_completion(hdr
, SAM_STAT_CHECK_CONDITION
,
1483 ILLEGAL_REQUEST
, SCSI_ASC_INVALID_CDB
,
1484 SCSI_ASCQ_CAUSE_NOT_REPORTABLE
);
1488 if (len
== FORMAT_UNIT_LONG_PARM_LIST_LEN
&&
1489 (parm_list
[FORMAT_UNIT_PROT_INT_OFFSET
] & 0x0F) != 0) {
1490 res
= nvme_trans_completion(hdr
, SAM_STAT_CHECK_CONDITION
,
1491 ILLEGAL_REQUEST
, SCSI_ASC_INVALID_CDB
,
1492 SCSI_ASCQ_CAUSE_NOT_REPORTABLE
);
1495 pf_usage
= parm_list
[FORMAT_UNIT_PROT_FIELD_USAGE_OFFSET
] &
1496 FORMAT_UNIT_PROT_FIELD_USAGE_MASK
;
1497 pf_code
= (pf_usage
<< 2) | format_prot_info
;
1512 res
= nvme_trans_completion(hdr
, SAM_STAT_CHECK_CONDITION
,
1513 ILLEGAL_REQUEST
, SCSI_ASC_INVALID_CDB
,
1514 SCSI_ASCQ_CAUSE_NOT_REPORTABLE
);
1524 static int nvme_trans_fmt_send_cmd(struct nvme_ns
*ns
, struct sg_io_hdr
*hdr
,
1529 struct nvme_id_ns
*id_ns
;
1532 u8 selected_lbaf
= 0xFF;
1534 struct nvme_command c
;
1536 /* Loop thru LBAF's in id_ns to match reqd lbaf, put in cdw10 */
1537 nvme_sc
= nvme_identify_ns(ns
->ctrl
, ns
->ns_id
, &id_ns
);
1538 res
= nvme_trans_status_code(hdr
, nvme_sc
);
1542 flbas
= (id_ns
->flbas
) & 0x0F;
1543 nlbaf
= id_ns
->nlbaf
;
1545 for (i
= 0; i
< nlbaf
; i
++) {
1546 if (ns
->mode_select_block_len
== (1 << (id_ns
->lbaf
[i
].ds
))) {
1551 if (selected_lbaf
> 0x0F) {
1552 res
= nvme_trans_completion(hdr
, SAM_STAT_CHECK_CONDITION
,
1553 ILLEGAL_REQUEST
, SCSI_ASC_INVALID_PARAMETER
,
1554 SCSI_ASCQ_CAUSE_NOT_REPORTABLE
);
1556 if (ns
->mode_select_num_blocks
!= le64_to_cpu(id_ns
->ncap
)) {
1557 res
= nvme_trans_completion(hdr
, SAM_STAT_CHECK_CONDITION
,
1558 ILLEGAL_REQUEST
, SCSI_ASC_INVALID_PARAMETER
,
1559 SCSI_ASCQ_CAUSE_NOT_REPORTABLE
);
1562 cdw10
|= prot_info
<< 5;
1563 cdw10
|= selected_lbaf
& 0x0F;
1564 memset(&c
, 0, sizeof(c
));
1565 c
.format
.opcode
= nvme_admin_format_nvm
;
1566 c
.format
.nsid
= cpu_to_le32(ns
->ns_id
);
1567 c
.format
.cdw10
= cpu_to_le32(cdw10
);
1569 nvme_sc
= nvme_submit_sync_cmd(ns
->ctrl
->admin_q
, &c
, NULL
, 0);
1570 res
= nvme_trans_status_code(hdr
, nvme_sc
);
1576 static inline u32
nvme_trans_io_get_num_cmds(struct sg_io_hdr
*hdr
,
1577 struct nvme_trans_io_cdb
*cdb_info
,
1580 /* If using iovecs, send one nvme command per vector */
1581 if (hdr
->iovec_count
> 0)
1582 return hdr
->iovec_count
;
1583 else if (cdb_info
->xfer_len
> max_blocks
)
1584 return ((cdb_info
->xfer_len
- 1) / max_blocks
) + 1;
1589 static u16
nvme_trans_io_get_control(struct nvme_ns
*ns
,
1590 struct nvme_trans_io_cdb
*cdb_info
)
1594 /* When Protection information support is added, implement here */
1596 if (cdb_info
->fua
> 0)
1597 control
|= NVME_RW_FUA
;
1602 static int nvme_trans_do_nvme_io(struct nvme_ns
*ns
, struct sg_io_hdr
*hdr
,
1603 struct nvme_trans_io_cdb
*cdb_info
, u8 is_write
)
1605 int nvme_sc
= NVME_SC_SUCCESS
;
1608 u64 unit_num_blocks
; /* Number of blocks to xfer in each nvme cmd */
1611 u64 nvme_offset
= 0;
1612 void __user
*next_mapping_addr
;
1613 struct nvme_command c
;
1614 u8 opcode
= (is_write
? nvme_cmd_write
: nvme_cmd_read
);
1616 u32 max_blocks
= queue_max_hw_sectors(ns
->queue
);
1618 num_cmds
= nvme_trans_io_get_num_cmds(hdr
, cdb_info
, max_blocks
);
1621 * This loop handles two cases.
1622 * First, when an SGL is used in the form of an iovec list:
1623 * - Use iov_base as the next mapping address for the nvme command_id
1624 * - Use iov_len as the data transfer length for the command.
1625 * Second, when we have a single buffer
1626 * - If larger than max_blocks, split into chunks, offset
1627 * each nvme command accordingly.
1629 for (i
= 0; i
< num_cmds
; i
++) {
1630 memset(&c
, 0, sizeof(c
));
1631 if (hdr
->iovec_count
> 0) {
1632 struct sg_iovec sgl
;
1634 retcode
= copy_from_user(&sgl
, hdr
->dxferp
+
1635 i
* sizeof(struct sg_iovec
),
1636 sizeof(struct sg_iovec
));
1639 unit_len
= sgl
.iov_len
;
1640 unit_num_blocks
= unit_len
>> ns
->lba_shift
;
1641 next_mapping_addr
= sgl
.iov_base
;
1643 unit_num_blocks
= min((u64
)max_blocks
,
1644 (cdb_info
->xfer_len
- nvme_offset
));
1645 unit_len
= unit_num_blocks
<< ns
->lba_shift
;
1646 next_mapping_addr
= hdr
->dxferp
+
1647 ((1 << ns
->lba_shift
) * nvme_offset
);
1650 c
.rw
.opcode
= opcode
;
1651 c
.rw
.nsid
= cpu_to_le32(ns
->ns_id
);
1652 c
.rw
.slba
= cpu_to_le64(cdb_info
->lba
+ nvme_offset
);
1653 c
.rw
.length
= cpu_to_le16(unit_num_blocks
- 1);
1654 control
= nvme_trans_io_get_control(ns
, cdb_info
);
1655 c
.rw
.control
= cpu_to_le16(control
);
1657 if (get_capacity(ns
->disk
) - unit_num_blocks
<
1658 cdb_info
->lba
+ nvme_offset
) {
1659 nvme_sc
= NVME_SC_LBA_RANGE
;
1662 nvme_sc
= nvme_submit_user_cmd(ns
->queue
, &c
,
1663 next_mapping_addr
, unit_len
, NULL
, 0);
1667 nvme_offset
+= unit_num_blocks
;
1670 return nvme_trans_status_code(hdr
, nvme_sc
);
1674 /* SCSI Command Translation Functions */
1676 static int nvme_trans_io(struct nvme_ns
*ns
, struct sg_io_hdr
*hdr
, u8 is_write
,
1680 struct nvme_trans_io_cdb cdb_info
= { 0, };
1683 u64 sum_iov_len
= 0;
1684 struct sg_iovec sgl
;
1689 * The FUA and WPROTECT fields are not supported in 6-byte CDBs,
1690 * but always in the same place for all others.
1697 cdb_info
.fua
= cmd
[1] & 0x8;
1698 cdb_info
.prot_info
= (cmd
[1] & 0xe0) >> 5;
1699 if (cdb_info
.prot_info
&& !ns
->pi_type
) {
1700 return nvme_trans_completion(hdr
,
1701 SAM_STAT_CHECK_CONDITION
,
1703 SCSI_ASC_INVALID_CDB
,
1704 SCSI_ASCQ_CAUSE_NOT_REPORTABLE
);
1711 cdb_info
.lba
= get_unaligned_be24(&cmd
[1]);
1712 cdb_info
.xfer_len
= cmd
[4];
1713 if (cdb_info
.xfer_len
== 0)
1714 cdb_info
.xfer_len
= 256;
1718 cdb_info
.lba
= get_unaligned_be32(&cmd
[2]);
1719 cdb_info
.xfer_len
= get_unaligned_be16(&cmd
[7]);
1723 cdb_info
.lba
= get_unaligned_be32(&cmd
[2]);
1724 cdb_info
.xfer_len
= get_unaligned_be32(&cmd
[6]);
1728 cdb_info
.lba
= get_unaligned_be64(&cmd
[2]);
1729 cdb_info
.xfer_len
= get_unaligned_be32(&cmd
[10]);
1732 /* Will never really reach here */
1737 /* Calculate total length of transfer (in bytes) */
1738 if (hdr
->iovec_count
> 0) {
1739 for (i
= 0; i
< hdr
->iovec_count
; i
++) {
1740 not_copied
= copy_from_user(&sgl
, hdr
->dxferp
+
1741 i
* sizeof(struct sg_iovec
),
1742 sizeof(struct sg_iovec
));
1745 sum_iov_len
+= sgl
.iov_len
;
1746 /* IO vector sizes should be multiples of block size */
1747 if (sgl
.iov_len
% (1 << ns
->lba_shift
) != 0) {
1748 res
= nvme_trans_completion(hdr
,
1749 SAM_STAT_CHECK_CONDITION
,
1751 SCSI_ASC_INVALID_PARAMETER
,
1752 SCSI_ASCQ_CAUSE_NOT_REPORTABLE
);
1757 sum_iov_len
= hdr
->dxfer_len
;
1760 /* As Per sg ioctl howto, if the lengths differ, use the lower one */
1761 xfer_bytes
= min(((u64
)hdr
->dxfer_len
), sum_iov_len
);
1763 /* If block count and actual data buffer size dont match, error out */
1764 if (xfer_bytes
!= (cdb_info
.xfer_len
<< ns
->lba_shift
)) {
1769 /* Check for 0 length transfer - it is not illegal */
1770 if (cdb_info
.xfer_len
== 0)
1773 /* Send NVMe IO Command(s) */
1774 res
= nvme_trans_do_nvme_io(ns
, hdr
, &cdb_info
, is_write
);
1782 static int nvme_trans_inquiry(struct nvme_ns
*ns
, struct sg_io_hdr
*hdr
,
1791 evpd
= cmd
[1] & 0x01;
1793 alloc_len
= get_unaligned_be16(&cmd
[3]);
1795 inq_response
= kmalloc(max(alloc_len
, STANDARD_INQUIRY_LENGTH
),
1797 if (inq_response
== NULL
) {
1803 if (page_code
== INQ_STANDARD_INQUIRY_PAGE
) {
1804 res
= nvme_trans_standard_inquiry_page(ns
, hdr
,
1805 inq_response
, alloc_len
);
1807 res
= nvme_trans_completion(hdr
,
1808 SAM_STAT_CHECK_CONDITION
,
1810 SCSI_ASC_INVALID_CDB
,
1811 SCSI_ASCQ_CAUSE_NOT_REPORTABLE
);
1814 switch (page_code
) {
1815 case VPD_SUPPORTED_PAGES
:
1816 res
= nvme_trans_supported_vpd_pages(ns
, hdr
,
1817 inq_response
, alloc_len
);
1819 case VPD_SERIAL_NUMBER
:
1820 res
= nvme_trans_unit_serial_page(ns
, hdr
, inq_response
,
1823 case VPD_DEVICE_IDENTIFIERS
:
1824 res
= nvme_trans_device_id_page(ns
, hdr
, inq_response
,
1827 case VPD_EXTENDED_INQUIRY
:
1828 res
= nvme_trans_ext_inq_page(ns
, hdr
, alloc_len
);
1830 case VPD_BLOCK_LIMITS
:
1831 res
= nvme_trans_bdev_limits_page(ns
, hdr
, inq_response
,
1834 case VPD_BLOCK_DEV_CHARACTERISTICS
:
1835 res
= nvme_trans_bdev_char_page(ns
, hdr
, alloc_len
);
1838 res
= nvme_trans_completion(hdr
,
1839 SAM_STAT_CHECK_CONDITION
,
1841 SCSI_ASC_INVALID_CDB
,
1842 SCSI_ASCQ_CAUSE_NOT_REPORTABLE
);
1846 kfree(inq_response
);
1851 static int nvme_trans_log_sense(struct nvme_ns
*ns
, struct sg_io_hdr
*hdr
,
1859 if (cmd
[1] != LOG_SENSE_CDB_SP_NOT_ENABLED
) {
1860 res
= nvme_trans_completion(hdr
, SAM_STAT_CHECK_CONDITION
,
1861 ILLEGAL_REQUEST
, SCSI_ASC_INVALID_CDB
,
1862 SCSI_ASCQ_CAUSE_NOT_REPORTABLE
);
1866 page_code
= cmd
[2] & LOG_SENSE_CDB_PAGE_CODE_MASK
;
1867 pc
= (cmd
[2] & LOG_SENSE_CDB_PC_MASK
) >> LOG_SENSE_CDB_PC_SHIFT
;
1868 if (pc
!= LOG_SENSE_CDB_PC_CUMULATIVE_VALUES
) {
1869 res
= nvme_trans_completion(hdr
, SAM_STAT_CHECK_CONDITION
,
1870 ILLEGAL_REQUEST
, SCSI_ASC_INVALID_CDB
,
1871 SCSI_ASCQ_CAUSE_NOT_REPORTABLE
);
1874 alloc_len
= get_unaligned_be16(&cmd
[7]);
1875 switch (page_code
) {
1876 case LOG_PAGE_SUPPORTED_LOG_PAGES_PAGE
:
1877 res
= nvme_trans_log_supp_pages(ns
, hdr
, alloc_len
);
1879 case LOG_PAGE_INFORMATIONAL_EXCEPTIONS_PAGE
:
1880 res
= nvme_trans_log_info_exceptions(ns
, hdr
, alloc_len
);
1882 case LOG_PAGE_TEMPERATURE_PAGE
:
1883 res
= nvme_trans_log_temperature(ns
, hdr
, alloc_len
);
1886 res
= nvme_trans_completion(hdr
, SAM_STAT_CHECK_CONDITION
,
1887 ILLEGAL_REQUEST
, SCSI_ASC_INVALID_CDB
,
1888 SCSI_ASCQ_CAUSE_NOT_REPORTABLE
);
1896 static int nvme_trans_mode_select(struct nvme_ns
*ns
, struct sg_io_hdr
*hdr
,
1904 page_format
= cmd
[1] & MODE_SELECT_CDB_PAGE_FORMAT_MASK
;
1905 save_pages
= cmd
[1] & MODE_SELECT_CDB_SAVE_PAGES_MASK
;
1907 if (cmd
[0] == MODE_SELECT
) {
1908 parm_list_len
= cmd
[4];
1910 parm_list_len
= cmd
[7];
1914 if (parm_list_len
!= 0) {
1916 * According to SPC-4 r24, a paramter list length field of 0
1917 * shall not be considered an error
1919 return nvme_trans_modesel_data(ns
, hdr
, cmd
, parm_list_len
,
1920 page_format
, save_pages
, cdb10
);
1926 static int nvme_trans_mode_sense(struct nvme_ns
*ns
, struct sg_io_hdr
*hdr
,
1933 if (cmd
[0] == MODE_SENSE
) {
1936 alloc_len
= get_unaligned_be16(&cmd
[7]);
1940 if ((cmd
[2] & MODE_SENSE_PAGE_CONTROL_MASK
) !=
1941 MODE_SENSE_PC_CURRENT_VALUES
) {
1942 res
= nvme_trans_completion(hdr
, SAM_STAT_CHECK_CONDITION
,
1943 ILLEGAL_REQUEST
, SCSI_ASC_INVALID_CDB
,
1944 SCSI_ASCQ_CAUSE_NOT_REPORTABLE
);
1948 switch (cmd
[2] & MODE_SENSE_PAGE_CODE_MASK
) {
1949 case MODE_PAGE_CACHING
:
1950 res
= nvme_trans_mode_page_create(ns
, hdr
, cmd
, alloc_len
,
1952 &nvme_trans_fill_caching_page
,
1953 MODE_PAGE_CACHING_LEN
);
1955 case MODE_PAGE_CONTROL
:
1956 res
= nvme_trans_mode_page_create(ns
, hdr
, cmd
, alloc_len
,
1958 &nvme_trans_fill_control_page
,
1959 MODE_PAGE_CONTROL_LEN
);
1961 case MODE_PAGE_POWER_CONDITION
:
1962 res
= nvme_trans_mode_page_create(ns
, hdr
, cmd
, alloc_len
,
1964 &nvme_trans_fill_pow_cnd_page
,
1965 MODE_PAGE_POW_CND_LEN
);
1967 case MODE_PAGE_INFO_EXCEP
:
1968 res
= nvme_trans_mode_page_create(ns
, hdr
, cmd
, alloc_len
,
1970 &nvme_trans_fill_inf_exc_page
,
1971 MODE_PAGE_INF_EXC_LEN
);
1973 case MODE_PAGE_RETURN_ALL
:
1974 res
= nvme_trans_mode_page_create(ns
, hdr
, cmd
, alloc_len
,
1976 &nvme_trans_fill_all_pages
,
1980 res
= nvme_trans_completion(hdr
, SAM_STAT_CHECK_CONDITION
,
1981 ILLEGAL_REQUEST
, SCSI_ASC_INVALID_CDB
,
1982 SCSI_ASCQ_CAUSE_NOT_REPORTABLE
);
1990 static int nvme_trans_read_capacity(struct nvme_ns
*ns
, struct sg_io_hdr
*hdr
,
1998 struct nvme_id_ns
*id_ns
;
2002 alloc_len
= get_unaligned_be32(&cmd
[10]);
2003 resp_size
= READ_CAP_16_RESP_SIZE
;
2005 alloc_len
= READ_CAP_10_RESP_SIZE
;
2006 resp_size
= READ_CAP_10_RESP_SIZE
;
2009 nvme_sc
= nvme_identify_ns(ns
->ctrl
, ns
->ns_id
, &id_ns
);
2010 res
= nvme_trans_status_code(hdr
, nvme_sc
);
2014 response
= kzalloc(resp_size
, GFP_KERNEL
);
2015 if (response
== NULL
) {
2019 nvme_trans_fill_read_cap(response
, id_ns
, cdb16
);
2021 xfer_len
= min(alloc_len
, resp_size
);
2022 res
= nvme_trans_copy_to_user(hdr
, response
, xfer_len
);
2030 static int nvme_trans_report_luns(struct nvme_ns
*ns
, struct sg_io_hdr
*hdr
,
2035 u32 alloc_len
, xfer_len
, resp_size
;
2037 struct nvme_id_ctrl
*id_ctrl
;
2038 u32 ll_length
, lun_id
;
2039 u8 lun_id_offset
= REPORT_LUNS_FIRST_LUN_OFFSET
;
2044 return nvme_trans_completion(hdr
, SAM_STAT_CHECK_CONDITION
,
2045 ILLEGAL_REQUEST
, SCSI_ASC_INVALID_CDB
,
2046 SCSI_ASCQ_CAUSE_NOT_REPORTABLE
);
2047 case ALL_LUNS_RETURNED
:
2048 case ALL_WELL_KNOWN_LUNS_RETURNED
:
2049 case RESTRICTED_LUNS_RETURNED
:
2050 nvme_sc
= nvme_identify_ctrl(ns
->ctrl
, &id_ctrl
);
2051 res
= nvme_trans_status_code(hdr
, nvme_sc
);
2055 ll_length
= le32_to_cpu(id_ctrl
->nn
) * LUN_ENTRY_SIZE
;
2056 resp_size
= ll_length
+ LUN_DATA_HEADER_SIZE
;
2058 alloc_len
= get_unaligned_be32(&cmd
[6]);
2059 if (alloc_len
< resp_size
) {
2060 res
= nvme_trans_completion(hdr
,
2061 SAM_STAT_CHECK_CONDITION
,
2062 ILLEGAL_REQUEST
, SCSI_ASC_INVALID_CDB
,
2063 SCSI_ASCQ_CAUSE_NOT_REPORTABLE
);
2067 response
= kzalloc(resp_size
, GFP_KERNEL
);
2068 if (response
== NULL
) {
2073 /* The first LUN ID will always be 0 per the SAM spec */
2074 for (lun_id
= 0; lun_id
< le32_to_cpu(id_ctrl
->nn
); lun_id
++) {
2076 * Set the LUN Id and then increment to the next LUN
2077 * location in the parameter data.
2079 __be64 tmp_id
= cpu_to_be64(lun_id
);
2080 memcpy(&response
[lun_id_offset
], &tmp_id
, sizeof(u64
));
2081 lun_id_offset
+= LUN_ENTRY_SIZE
;
2083 tmp_len
= cpu_to_be32(ll_length
);
2084 memcpy(response
, &tmp_len
, sizeof(u32
));
2087 xfer_len
= min(alloc_len
, resp_size
);
2088 res
= nvme_trans_copy_to_user(hdr
, response
, xfer_len
);
2096 static int nvme_trans_request_sense(struct nvme_ns
*ns
, struct sg_io_hdr
*hdr
,
2100 u8 alloc_len
, xfer_len
, resp_size
;
2104 desc_format
= cmd
[1] & 0x01;
2107 resp_size
= ((desc_format
) ? (DESC_FMT_SENSE_DATA_SIZE
) :
2108 (FIXED_FMT_SENSE_DATA_SIZE
));
2109 response
= kzalloc(resp_size
, GFP_KERNEL
);
2110 if (response
== NULL
) {
2116 /* Descriptor Format Sense Data */
2117 response
[0] = DESC_FORMAT_SENSE_DATA
;
2118 response
[1] = NO_SENSE
;
2119 /* TODO How is LOW POWER CONDITION ON handled? (byte 2) */
2120 response
[2] = SCSI_ASC_NO_SENSE
;
2121 response
[3] = SCSI_ASCQ_CAUSE_NOT_REPORTABLE
;
2122 /* SDAT_OVFL = 0 | Additional Sense Length = 0 */
2124 /* Fixed Format Sense Data */
2125 response
[0] = FIXED_SENSE_DATA
;
2126 /* Byte 1 = Obsolete */
2127 response
[2] = NO_SENSE
; /* FM, EOM, ILI, SDAT_OVFL = 0 */
2128 /* Bytes 3-6 - Information - set to zero */
2129 response
[7] = FIXED_SENSE_DATA_ADD_LENGTH
;
2130 /* Bytes 8-11 - Cmd Specific Information - set to zero */
2131 response
[12] = SCSI_ASC_NO_SENSE
;
2132 response
[13] = SCSI_ASCQ_CAUSE_NOT_REPORTABLE
;
2133 /* Byte 14 = Field Replaceable Unit Code = 0 */
2134 /* Bytes 15-17 - SKSV=0; Sense Key Specific = 0 */
2137 xfer_len
= min(alloc_len
, resp_size
);
2138 res
= nvme_trans_copy_to_user(hdr
, response
, xfer_len
);
2145 static int nvme_trans_security_protocol(struct nvme_ns
*ns
,
2146 struct sg_io_hdr
*hdr
,
2149 return nvme_trans_completion(hdr
, SAM_STAT_CHECK_CONDITION
,
2150 ILLEGAL_REQUEST
, SCSI_ASC_ILLEGAL_COMMAND
,
2151 SCSI_ASCQ_CAUSE_NOT_REPORTABLE
);
2154 static int nvme_trans_synchronize_cache(struct nvme_ns
*ns
,
2155 struct sg_io_hdr
*hdr
)
2158 struct nvme_command c
;
2160 memset(&c
, 0, sizeof(c
));
2161 c
.common
.opcode
= nvme_cmd_flush
;
2162 c
.common
.nsid
= cpu_to_le32(ns
->ns_id
);
2164 nvme_sc
= nvme_submit_sync_cmd(ns
->queue
, &c
, NULL
, 0);
2165 return nvme_trans_status_code(hdr
, nvme_sc
);
2168 static int nvme_trans_start_stop(struct nvme_ns
*ns
, struct sg_io_hdr
*hdr
,
2171 u8 immed
, pcmod
, no_flush
, start
;
2173 immed
= cmd
[1] & 0x01;
2174 pcmod
= cmd
[3] & 0x0f;
2175 no_flush
= cmd
[4] & 0x04;
2176 start
= cmd
[4] & 0x01;
2179 return nvme_trans_completion(hdr
, SAM_STAT_CHECK_CONDITION
,
2180 ILLEGAL_REQUEST
, SCSI_ASC_INVALID_CDB
,
2181 SCSI_ASCQ_CAUSE_NOT_REPORTABLE
);
2183 if (no_flush
== 0) {
2184 /* Issue NVME FLUSH command prior to START STOP UNIT */
2185 int res
= nvme_trans_synchronize_cache(ns
, hdr
);
2194 static int nvme_trans_format_unit(struct nvme_ns
*ns
, struct sg_io_hdr
*hdr
,
2198 u8 parm_hdr_len
= 0;
2199 u8 nvme_pf_code
= 0;
2200 u8 format_prot_info
, long_list
, format_data
;
2202 format_prot_info
= (cmd
[1] & 0xc0) >> 6;
2203 long_list
= cmd
[1] & 0x20;
2204 format_data
= cmd
[1] & 0x10;
2206 if (format_data
!= 0) {
2207 if (format_prot_info
!= 0) {
2209 parm_hdr_len
= FORMAT_UNIT_SHORT_PARM_LIST_LEN
;
2211 parm_hdr_len
= FORMAT_UNIT_LONG_PARM_LIST_LEN
;
2213 } else if (format_data
== 0 && format_prot_info
!= 0) {
2214 res
= nvme_trans_completion(hdr
, SAM_STAT_CHECK_CONDITION
,
2215 ILLEGAL_REQUEST
, SCSI_ASC_INVALID_CDB
,
2216 SCSI_ASCQ_CAUSE_NOT_REPORTABLE
);
2220 /* Get parm header from data-in/out buffer */
2222 * According to the translation spec, the only fields in the parameter
2223 * list we are concerned with are in the header. So allocate only that.
2225 if (parm_hdr_len
> 0) {
2226 res
= nvme_trans_fmt_get_parm_header(hdr
, parm_hdr_len
,
2227 format_prot_info
, &nvme_pf_code
);
2232 /* Attempt to activate any previously downloaded firmware image */
2233 res
= nvme_trans_send_activate_fw_cmd(ns
, hdr
, 0);
2235 /* Determine Block size and count and send format command */
2236 res
= nvme_trans_fmt_set_blk_size_count(ns
, hdr
);
2240 res
= nvme_trans_fmt_send_cmd(ns
, hdr
, nvme_pf_code
);
2246 static int nvme_trans_test_unit_ready(struct nvme_ns
*ns
,
2247 struct sg_io_hdr
*hdr
,
2250 if (nvme_ctrl_ready(ns
->ctrl
))
2251 return nvme_trans_completion(hdr
, SAM_STAT_CHECK_CONDITION
,
2252 NOT_READY
, SCSI_ASC_LUN_NOT_READY
,
2253 SCSI_ASCQ_CAUSE_NOT_REPORTABLE
);
2255 return nvme_trans_completion(hdr
, SAM_STAT_GOOD
, NO_SENSE
, 0, 0);
2258 static int nvme_trans_write_buffer(struct nvme_ns
*ns
, struct sg_io_hdr
*hdr
,
2262 u32 buffer_offset
, parm_list_length
;
2265 parm_list_length
= get_unaligned_be24(&cmd
[6]);
2266 if (parm_list_length
% BYTES_TO_DWORDS
!= 0) {
2267 /* NVMe expects Firmware file to be a whole number of DWORDS */
2268 res
= nvme_trans_completion(hdr
, SAM_STAT_CHECK_CONDITION
,
2269 ILLEGAL_REQUEST
, SCSI_ASC_INVALID_CDB
,
2270 SCSI_ASCQ_CAUSE_NOT_REPORTABLE
);
2274 if (buffer_id
> NVME_MAX_FIRMWARE_SLOT
) {
2275 res
= nvme_trans_completion(hdr
, SAM_STAT_CHECK_CONDITION
,
2276 ILLEGAL_REQUEST
, SCSI_ASC_INVALID_CDB
,
2277 SCSI_ASCQ_CAUSE_NOT_REPORTABLE
);
2280 mode
= cmd
[1] & 0x1f;
2281 buffer_offset
= get_unaligned_be24(&cmd
[3]);
2284 case DOWNLOAD_SAVE_ACTIVATE
:
2285 res
= nvme_trans_send_download_fw_cmd(ns
, hdr
, nvme_admin_download_fw
,
2286 parm_list_length
, buffer_offset
,
2290 res
= nvme_trans_send_activate_fw_cmd(ns
, hdr
, buffer_id
);
2292 case DOWNLOAD_SAVE_DEFER_ACTIVATE
:
2293 res
= nvme_trans_send_download_fw_cmd(ns
, hdr
, nvme_admin_download_fw
,
2294 parm_list_length
, buffer_offset
,
2297 case ACTIVATE_DEFERRED_MICROCODE
:
2298 res
= nvme_trans_send_activate_fw_cmd(ns
, hdr
, buffer_id
);
2301 res
= nvme_trans_completion(hdr
, SAM_STAT_CHECK_CONDITION
,
2302 ILLEGAL_REQUEST
, SCSI_ASC_INVALID_CDB
,
2303 SCSI_ASCQ_CAUSE_NOT_REPORTABLE
);
2311 struct scsi_unmap_blk_desc
{
2317 struct scsi_unmap_parm_list
{
2318 __be16 unmap_data_len
;
2319 __be16 unmap_blk_desc_data_len
;
2321 struct scsi_unmap_blk_desc desc
[0];
2324 static int nvme_trans_unmap(struct nvme_ns
*ns
, struct sg_io_hdr
*hdr
,
2327 struct scsi_unmap_parm_list
*plist
;
2328 struct nvme_dsm_range
*range
;
2329 struct nvme_command c
;
2330 int i
, nvme_sc
, res
;
2331 u16 ndesc
, list_len
;
2333 list_len
= get_unaligned_be16(&cmd
[7]);
2337 plist
= kmalloc(list_len
, GFP_KERNEL
);
2341 res
= nvme_trans_copy_from_user(hdr
, plist
, list_len
);
2345 ndesc
= be16_to_cpu(plist
->unmap_blk_desc_data_len
) >> 4;
2346 if (!ndesc
|| ndesc
> 256) {
2351 range
= kcalloc(ndesc
, sizeof(*range
), GFP_KERNEL
);
2357 for (i
= 0; i
< ndesc
; i
++) {
2358 range
[i
].nlb
= cpu_to_le32(be32_to_cpu(plist
->desc
[i
].nlb
));
2359 range
[i
].slba
= cpu_to_le64(be64_to_cpu(plist
->desc
[i
].slba
));
2363 memset(&c
, 0, sizeof(c
));
2364 c
.dsm
.opcode
= nvme_cmd_dsm
;
2365 c
.dsm
.nsid
= cpu_to_le32(ns
->ns_id
);
2366 c
.dsm
.nr
= cpu_to_le32(ndesc
- 1);
2367 c
.dsm
.attributes
= cpu_to_le32(NVME_DSMGMT_AD
);
2369 nvme_sc
= nvme_submit_sync_cmd(ns
->queue
, &c
, range
,
2370 ndesc
* sizeof(*range
));
2371 res
= nvme_trans_status_code(hdr
, nvme_sc
);
2379 static int nvme_scsi_translate(struct nvme_ns
*ns
, struct sg_io_hdr
*hdr
)
2381 u8 cmd
[BLK_MAX_CDB
];
2383 unsigned int opcode
;
2385 if (hdr
->cmdp
== NULL
)
2387 if (copy_from_user(cmd
, hdr
->cmdp
, hdr
->cmd_len
))
2391 * Prime the hdr with good status for scsi commands that don't require
2392 * an nvme command for translation.
2394 retcode
= nvme_trans_status_code(hdr
, NVME_SC_SUCCESS
);
2405 retcode
= nvme_trans_io(ns
, hdr
, 0, cmd
);
2411 retcode
= nvme_trans_io(ns
, hdr
, 1, cmd
);
2414 retcode
= nvme_trans_inquiry(ns
, hdr
, cmd
);
2417 retcode
= nvme_trans_log_sense(ns
, hdr
, cmd
);
2420 case MODE_SELECT_10
:
2421 retcode
= nvme_trans_mode_select(ns
, hdr
, cmd
);
2425 retcode
= nvme_trans_mode_sense(ns
, hdr
, cmd
);
2428 retcode
= nvme_trans_read_capacity(ns
, hdr
, cmd
, 0);
2430 case SERVICE_ACTION_IN_16
:
2432 case SAI_READ_CAPACITY_16
:
2433 retcode
= nvme_trans_read_capacity(ns
, hdr
, cmd
, 1);
2440 retcode
= nvme_trans_report_luns(ns
, hdr
, cmd
);
2443 retcode
= nvme_trans_request_sense(ns
, hdr
, cmd
);
2445 case SECURITY_PROTOCOL_IN
:
2446 case SECURITY_PROTOCOL_OUT
:
2447 retcode
= nvme_trans_security_protocol(ns
, hdr
, cmd
);
2450 retcode
= nvme_trans_start_stop(ns
, hdr
, cmd
);
2452 case SYNCHRONIZE_CACHE
:
2453 retcode
= nvme_trans_synchronize_cache(ns
, hdr
);
2456 retcode
= nvme_trans_format_unit(ns
, hdr
, cmd
);
2458 case TEST_UNIT_READY
:
2459 retcode
= nvme_trans_test_unit_ready(ns
, hdr
, cmd
);
2462 retcode
= nvme_trans_write_buffer(ns
, hdr
, cmd
);
2465 retcode
= nvme_trans_unmap(ns
, hdr
, cmd
);
2469 retcode
= nvme_trans_completion(hdr
, SAM_STAT_CHECK_CONDITION
,
2470 ILLEGAL_REQUEST
, SCSI_ASC_ILLEGAL_COMMAND
,
2471 SCSI_ASCQ_CAUSE_NOT_REPORTABLE
);
2477 int nvme_sg_io(struct nvme_ns
*ns
, struct sg_io_hdr __user
*u_hdr
)
2479 struct sg_io_hdr hdr
;
2482 if (!capable(CAP_SYS_ADMIN
))
2484 if (copy_from_user(&hdr
, u_hdr
, sizeof(hdr
)))
2486 if (hdr
.interface_id
!= 'S')
2488 if (hdr
.cmd_len
> BLK_MAX_CDB
)
2492 * A positive return code means a NVMe status, which has been
2493 * translated to sense data.
2495 retcode
= nvme_scsi_translate(ns
, &hdr
);
2498 if (copy_to_user(u_hdr
, &hdr
, sizeof(sg_io_hdr_t
)) > 0)
2503 int nvme_sg_get_version_num(int __user
*ip
)
2505 return put_user(sg_version_num
, ip
);