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>
46 #include <scsi/scsi_request.h>
50 static int sg_version_num = 30534; /* 2 digits for each component */
53 #define VPD_SUPPORTED_PAGES 0x00
54 #define VPD_SERIAL_NUMBER 0x80
55 #define VPD_DEVICE_IDENTIFIERS 0x83
56 #define VPD_EXTENDED_INQUIRY 0x86
57 #define VPD_BLOCK_LIMITS 0xB0
58 #define VPD_BLOCK_DEV_CHARACTERISTICS 0xB1
60 /* format unit paramter list offsets */
61 #define FORMAT_UNIT_SHORT_PARM_LIST_LEN 4
62 #define FORMAT_UNIT_LONG_PARM_LIST_LEN 8
63 #define FORMAT_UNIT_PROT_INT_OFFSET 3
64 #define FORMAT_UNIT_PROT_FIELD_USAGE_OFFSET 0
65 #define FORMAT_UNIT_PROT_FIELD_USAGE_MASK 0x07
68 #define FIXED_SENSE_DATA 0x70
69 #define DESC_FORMAT_SENSE_DATA 0x72
70 #define FIXED_SENSE_DATA_ADD_LENGTH 10
71 #define LUN_ENTRY_SIZE 8
72 #define LUN_DATA_HEADER_SIZE 8
73 #define ALL_LUNS_RETURNED 0x02
74 #define ALL_WELL_KNOWN_LUNS_RETURNED 0x01
75 #define RESTRICTED_LUNS_RETURNED 0x00
76 #define DOWNLOAD_SAVE_ACTIVATE 0x05
77 #define DOWNLOAD_SAVE_DEFER_ACTIVATE 0x0E
78 #define ACTIVATE_DEFERRED_MICROCODE 0x0F
79 #define FORMAT_UNIT_IMMED_MASK 0x2
80 #define FORMAT_UNIT_IMMED_OFFSET 1
81 #define KELVIN_TEMP_FACTOR 273
82 #define FIXED_FMT_SENSE_DATA_SIZE 18
83 #define DESC_FMT_SENSE_DATA_SIZE 8
85 /* SCSI/NVMe defines and bit masks */
86 #define INQ_STANDARD_INQUIRY_PAGE 0x00
87 #define INQ_SUPPORTED_VPD_PAGES_PAGE 0x00
88 #define INQ_UNIT_SERIAL_NUMBER_PAGE 0x80
89 #define INQ_DEVICE_IDENTIFICATION_PAGE 0x83
90 #define INQ_EXTENDED_INQUIRY_DATA_PAGE 0x86
91 #define INQ_BDEV_LIMITS_PAGE 0xB0
92 #define INQ_BDEV_CHARACTERISTICS_PAGE 0xB1
93 #define INQ_SERIAL_NUMBER_LENGTH 0x14
94 #define INQ_NUM_SUPPORTED_VPD_PAGES 6
95 #define VERSION_SPC_4 0x06
96 #define ACA_UNSUPPORTED 0
97 #define STANDARD_INQUIRY_LENGTH 36
98 #define ADDITIONAL_STD_INQ_LENGTH 31
99 #define EXTENDED_INQUIRY_DATA_PAGE_LENGTH 0x3C
100 #define RESERVED_FIELD 0
102 /* Mode Sense/Select defines */
103 #define MODE_PAGE_INFO_EXCEP 0x1C
104 #define MODE_PAGE_CACHING 0x08
105 #define MODE_PAGE_CONTROL 0x0A
106 #define MODE_PAGE_POWER_CONDITION 0x1A
107 #define MODE_PAGE_RETURN_ALL 0x3F
108 #define MODE_PAGE_BLK_DES_LEN 0x08
109 #define MODE_PAGE_LLBAA_BLK_DES_LEN 0x10
110 #define MODE_PAGE_CACHING_LEN 0x14
111 #define MODE_PAGE_CONTROL_LEN 0x0C
112 #define MODE_PAGE_POW_CND_LEN 0x28
113 #define MODE_PAGE_INF_EXC_LEN 0x0C
114 #define MODE_PAGE_ALL_LEN 0x54
115 #define MODE_SENSE6_MPH_SIZE 4
116 #define MODE_SENSE_PAGE_CONTROL_MASK 0xC0
117 #define MODE_SENSE_PAGE_CODE_OFFSET 2
118 #define MODE_SENSE_PAGE_CODE_MASK 0x3F
119 #define MODE_SENSE_LLBAA_MASK 0x10
120 #define MODE_SENSE_LLBAA_SHIFT 4
121 #define MODE_SENSE_DBD_MASK 8
122 #define MODE_SENSE_DBD_SHIFT 3
123 #define MODE_SENSE10_MPH_SIZE 8
124 #define MODE_SELECT_CDB_PAGE_FORMAT_MASK 0x10
125 #define MODE_SELECT_CDB_SAVE_PAGES_MASK 0x1
126 #define MODE_SELECT_6_BD_OFFSET 3
127 #define MODE_SELECT_10_BD_OFFSET 6
128 #define MODE_SELECT_10_LLBAA_OFFSET 4
129 #define MODE_SELECT_10_LLBAA_MASK 1
130 #define MODE_SELECT_6_MPH_SIZE 4
131 #define MODE_SELECT_10_MPH_SIZE 8
132 #define CACHING_MODE_PAGE_WCE_MASK 0x04
133 #define MODE_SENSE_BLK_DESC_ENABLED 0
134 #define MODE_SENSE_BLK_DESC_COUNT 1
135 #define MODE_SELECT_PAGE_CODE_MASK 0x3F
136 #define SHORT_DESC_BLOCK 8
137 #define LONG_DESC_BLOCK 16
138 #define MODE_PAGE_POW_CND_LEN_FIELD 0x26
139 #define MODE_PAGE_INF_EXC_LEN_FIELD 0x0A
140 #define MODE_PAGE_CACHING_LEN_FIELD 0x12
141 #define MODE_PAGE_CONTROL_LEN_FIELD 0x0A
142 #define MODE_SENSE_PC_CURRENT_VALUES 0
144 /* Log Sense defines */
145 #define LOG_PAGE_SUPPORTED_LOG_PAGES_PAGE 0x00
146 #define LOG_PAGE_SUPPORTED_LOG_PAGES_LENGTH 0x07
147 #define LOG_PAGE_INFORMATIONAL_EXCEPTIONS_PAGE 0x2F
148 #define LOG_PAGE_TEMPERATURE_PAGE 0x0D
149 #define LOG_SENSE_CDB_SP_NOT_ENABLED 0
150 #define LOG_SENSE_CDB_PC_MASK 0xC0
151 #define LOG_SENSE_CDB_PC_SHIFT 6
152 #define LOG_SENSE_CDB_PC_CUMULATIVE_VALUES 1
153 #define LOG_SENSE_CDB_PAGE_CODE_MASK 0x3F
154 #define REMAINING_INFO_EXCP_PAGE_LENGTH 0x8
155 #define LOG_INFO_EXCP_PAGE_LENGTH 0xC
156 #define REMAINING_TEMP_PAGE_LENGTH 0xC
157 #define LOG_TEMP_PAGE_LENGTH 0x10
158 #define LOG_TEMP_UNKNOWN 0xFF
159 #define SUPPORTED_LOG_PAGES_PAGE_LENGTH 0x3
161 /* Read Capacity defines */
162 #define READ_CAP_10_RESP_SIZE 8
163 #define READ_CAP_16_RESP_SIZE 32
165 /* NVMe Namespace and Command Defines */
166 #define BYTES_TO_DWORDS 4
167 #define NVME_MAX_FIRMWARE_SLOT 7
169 /* Report LUNs defines */
170 #define REPORT_LUNS_FIRST_LUN_OFFSET 8
172 /* SCSI ADDITIONAL SENSE Codes */
174 #define SCSI_ASC_NO_SENSE 0x00
175 #define SCSI_ASC_PERIPHERAL_DEV_WRITE_FAULT 0x03
176 #define SCSI_ASC_LUN_NOT_READY 0x04
177 #define SCSI_ASC_WARNING 0x0B
178 #define SCSI_ASC_LOG_BLOCK_GUARD_CHECK_FAILED 0x10
179 #define SCSI_ASC_LOG_BLOCK_APPTAG_CHECK_FAILED 0x10
180 #define SCSI_ASC_LOG_BLOCK_REFTAG_CHECK_FAILED 0x10
181 #define SCSI_ASC_UNRECOVERED_READ_ERROR 0x11
182 #define SCSI_ASC_MISCOMPARE_DURING_VERIFY 0x1D
183 #define SCSI_ASC_ACCESS_DENIED_INVALID_LUN_ID 0x20
184 #define SCSI_ASC_ILLEGAL_COMMAND 0x20
185 #define SCSI_ASC_ILLEGAL_BLOCK 0x21
186 #define SCSI_ASC_INVALID_CDB 0x24
187 #define SCSI_ASC_INVALID_LUN 0x25
188 #define SCSI_ASC_INVALID_PARAMETER 0x26
189 #define SCSI_ASC_FORMAT_COMMAND_FAILED 0x31
190 #define SCSI_ASC_INTERNAL_TARGET_FAILURE 0x44
192 /* SCSI ADDITIONAL SENSE Code Qualifiers */
194 #define SCSI_ASCQ_CAUSE_NOT_REPORTABLE 0x00
195 #define SCSI_ASCQ_FORMAT_COMMAND_FAILED 0x01
196 #define SCSI_ASCQ_LOG_BLOCK_GUARD_CHECK_FAILED 0x01
197 #define SCSI_ASCQ_LOG_BLOCK_APPTAG_CHECK_FAILED 0x02
198 #define SCSI_ASCQ_LOG_BLOCK_REFTAG_CHECK_FAILED 0x03
199 #define SCSI_ASCQ_FORMAT_IN_PROGRESS 0x04
200 #define SCSI_ASCQ_POWER_LOSS_EXPECTED 0x08
201 #define SCSI_ASCQ_INVALID_LUN_ID 0x09
203 /* copied from drivers/usb/gadget/function/storage_common.h */
204 static inline u32 get_unaligned_be24(u8 *buf)
206 return 0xffffff & (u32) get_unaligned_be32(buf - 1);
209 /* Struct to gather data that needs to be extracted from a SCSI CDB.
210 Not conforming to any particular CDB variant, but compatible with all. */
212 struct nvme_trans_io_cdb {
220 /* Internal Helper Functions */
223 /* Copy data to userspace memory */
225 static int nvme_trans_copy_to_user(struct sg_io_hdr *hdr, void *from,
230 size_t remaining = n;
233 if (hdr->iovec_count > 0) {
236 for (i = 0; i < hdr->iovec_count; i++) {
237 if (copy_from_user(&sgl, hdr->dxferp +
238 i * sizeof(struct sg_iovec),
239 sizeof(struct sg_iovec)))
241 xfer_len = min(remaining, sgl.iov_len);
242 if (copy_to_user(sgl.iov_base, index, xfer_len))
246 remaining -= xfer_len;
253 if (copy_to_user(hdr->dxferp, from, n))
258 /* Copy data from userspace memory */
260 static int nvme_trans_copy_from_user(struct sg_io_hdr *hdr, void *to,
265 size_t remaining = n;
268 if (hdr->iovec_count > 0) {
271 for (i = 0; i < hdr->iovec_count; i++) {
272 if (copy_from_user(&sgl, hdr->dxferp +
273 i * sizeof(struct sg_iovec),
274 sizeof(struct sg_iovec)))
276 xfer_len = min(remaining, sgl.iov_len);
277 if (copy_from_user(index, sgl.iov_base, xfer_len))
280 remaining -= xfer_len;
287 if (copy_from_user(to, hdr->dxferp, n))
292 /* Status/Sense Buffer Writeback */
294 static int nvme_trans_completion(struct sg_io_hdr *hdr, u8 status, u8 sense_key,
298 u8 resp[DESC_FMT_SENSE_DATA_SIZE];
300 if (scsi_status_is_good(status)) {
301 hdr->status = SAM_STAT_GOOD;
302 hdr->masked_status = GOOD;
303 hdr->host_status = DID_OK;
304 hdr->driver_status = DRIVER_OK;
307 hdr->status = status;
308 hdr->masked_status = status >> 1;
309 hdr->host_status = DID_OK;
310 hdr->driver_status = DRIVER_OK;
312 memset(resp, 0, DESC_FMT_SENSE_DATA_SIZE);
313 resp[0] = DESC_FORMAT_SENSE_DATA;
318 xfer_len = min_t(u8, hdr->mx_sb_len, DESC_FMT_SENSE_DATA_SIZE);
319 hdr->sb_len_wr = xfer_len;
320 if (copy_to_user(hdr->sbp, resp, xfer_len) > 0)
328 * Take a status code from a lowlevel routine, and if it was a positive NVMe
329 * error code update the sense data based on it. In either case the passed
330 * in value is returned again, unless an -EFAULT from copy_to_user overrides
333 static int nvme_trans_status_code(struct sg_io_hdr *hdr, int nvme_sc)
335 u8 status, sense_key, asc, ascq;
338 /* For non-nvme (Linux) errors, simply return the error code */
342 /* Mask DNR, More, and reserved fields */
343 switch (nvme_sc & 0x7FF) {
344 /* Generic Command Status */
345 case NVME_SC_SUCCESS:
346 status = SAM_STAT_GOOD;
347 sense_key = NO_SENSE;
348 asc = SCSI_ASC_NO_SENSE;
349 ascq = SCSI_ASCQ_CAUSE_NOT_REPORTABLE;
351 case NVME_SC_INVALID_OPCODE:
352 status = SAM_STAT_CHECK_CONDITION;
353 sense_key = ILLEGAL_REQUEST;
354 asc = SCSI_ASC_ILLEGAL_COMMAND;
355 ascq = SCSI_ASCQ_CAUSE_NOT_REPORTABLE;
357 case NVME_SC_INVALID_FIELD:
358 status = SAM_STAT_CHECK_CONDITION;
359 sense_key = ILLEGAL_REQUEST;
360 asc = SCSI_ASC_INVALID_CDB;
361 ascq = SCSI_ASCQ_CAUSE_NOT_REPORTABLE;
363 case NVME_SC_DATA_XFER_ERROR:
364 status = SAM_STAT_CHECK_CONDITION;
365 sense_key = MEDIUM_ERROR;
366 asc = SCSI_ASC_NO_SENSE;
367 ascq = SCSI_ASCQ_CAUSE_NOT_REPORTABLE;
369 case NVME_SC_POWER_LOSS:
370 status = SAM_STAT_TASK_ABORTED;
371 sense_key = ABORTED_COMMAND;
372 asc = SCSI_ASC_WARNING;
373 ascq = SCSI_ASCQ_POWER_LOSS_EXPECTED;
375 case NVME_SC_INTERNAL:
376 status = SAM_STAT_CHECK_CONDITION;
377 sense_key = HARDWARE_ERROR;
378 asc = SCSI_ASC_INTERNAL_TARGET_FAILURE;
379 ascq = SCSI_ASCQ_CAUSE_NOT_REPORTABLE;
381 case NVME_SC_ABORT_REQ:
382 status = SAM_STAT_TASK_ABORTED;
383 sense_key = ABORTED_COMMAND;
384 asc = SCSI_ASC_NO_SENSE;
385 ascq = SCSI_ASCQ_CAUSE_NOT_REPORTABLE;
387 case NVME_SC_ABORT_QUEUE:
388 status = SAM_STAT_TASK_ABORTED;
389 sense_key = ABORTED_COMMAND;
390 asc = SCSI_ASC_NO_SENSE;
391 ascq = SCSI_ASCQ_CAUSE_NOT_REPORTABLE;
393 case NVME_SC_FUSED_FAIL:
394 status = SAM_STAT_TASK_ABORTED;
395 sense_key = ABORTED_COMMAND;
396 asc = SCSI_ASC_NO_SENSE;
397 ascq = SCSI_ASCQ_CAUSE_NOT_REPORTABLE;
399 case NVME_SC_FUSED_MISSING:
400 status = SAM_STAT_TASK_ABORTED;
401 sense_key = ABORTED_COMMAND;
402 asc = SCSI_ASC_NO_SENSE;
403 ascq = SCSI_ASCQ_CAUSE_NOT_REPORTABLE;
405 case NVME_SC_INVALID_NS:
406 status = SAM_STAT_CHECK_CONDITION;
407 sense_key = ILLEGAL_REQUEST;
408 asc = SCSI_ASC_ACCESS_DENIED_INVALID_LUN_ID;
409 ascq = SCSI_ASCQ_INVALID_LUN_ID;
411 case NVME_SC_LBA_RANGE:
412 status = SAM_STAT_CHECK_CONDITION;
413 sense_key = ILLEGAL_REQUEST;
414 asc = SCSI_ASC_ILLEGAL_BLOCK;
415 ascq = SCSI_ASCQ_CAUSE_NOT_REPORTABLE;
417 case NVME_SC_CAP_EXCEEDED:
418 status = SAM_STAT_CHECK_CONDITION;
419 sense_key = MEDIUM_ERROR;
420 asc = SCSI_ASC_NO_SENSE;
421 ascq = SCSI_ASCQ_CAUSE_NOT_REPORTABLE;
423 case NVME_SC_NS_NOT_READY:
424 status = SAM_STAT_CHECK_CONDITION;
425 sense_key = NOT_READY;
426 asc = SCSI_ASC_LUN_NOT_READY;
427 ascq = SCSI_ASCQ_CAUSE_NOT_REPORTABLE;
430 /* Command Specific Status */
431 case NVME_SC_INVALID_FORMAT:
432 status = SAM_STAT_CHECK_CONDITION;
433 sense_key = ILLEGAL_REQUEST;
434 asc = SCSI_ASC_FORMAT_COMMAND_FAILED;
435 ascq = SCSI_ASCQ_FORMAT_COMMAND_FAILED;
437 case NVME_SC_BAD_ATTRIBUTES:
438 status = SAM_STAT_CHECK_CONDITION;
439 sense_key = ILLEGAL_REQUEST;
440 asc = SCSI_ASC_INVALID_CDB;
441 ascq = SCSI_ASCQ_CAUSE_NOT_REPORTABLE;
445 case NVME_SC_WRITE_FAULT:
446 status = SAM_STAT_CHECK_CONDITION;
447 sense_key = MEDIUM_ERROR;
448 asc = SCSI_ASC_PERIPHERAL_DEV_WRITE_FAULT;
449 ascq = SCSI_ASCQ_CAUSE_NOT_REPORTABLE;
451 case NVME_SC_READ_ERROR:
452 status = SAM_STAT_CHECK_CONDITION;
453 sense_key = MEDIUM_ERROR;
454 asc = SCSI_ASC_UNRECOVERED_READ_ERROR;
455 ascq = SCSI_ASCQ_CAUSE_NOT_REPORTABLE;
457 case NVME_SC_GUARD_CHECK:
458 status = SAM_STAT_CHECK_CONDITION;
459 sense_key = MEDIUM_ERROR;
460 asc = SCSI_ASC_LOG_BLOCK_GUARD_CHECK_FAILED;
461 ascq = SCSI_ASCQ_LOG_BLOCK_GUARD_CHECK_FAILED;
463 case NVME_SC_APPTAG_CHECK:
464 status = SAM_STAT_CHECK_CONDITION;
465 sense_key = MEDIUM_ERROR;
466 asc = SCSI_ASC_LOG_BLOCK_APPTAG_CHECK_FAILED;
467 ascq = SCSI_ASCQ_LOG_BLOCK_APPTAG_CHECK_FAILED;
469 case NVME_SC_REFTAG_CHECK:
470 status = SAM_STAT_CHECK_CONDITION;
471 sense_key = MEDIUM_ERROR;
472 asc = SCSI_ASC_LOG_BLOCK_REFTAG_CHECK_FAILED;
473 ascq = SCSI_ASCQ_LOG_BLOCK_REFTAG_CHECK_FAILED;
475 case NVME_SC_COMPARE_FAILED:
476 status = SAM_STAT_CHECK_CONDITION;
477 sense_key = MISCOMPARE;
478 asc = SCSI_ASC_MISCOMPARE_DURING_VERIFY;
479 ascq = SCSI_ASCQ_CAUSE_NOT_REPORTABLE;
481 case NVME_SC_ACCESS_DENIED:
482 status = SAM_STAT_CHECK_CONDITION;
483 sense_key = ILLEGAL_REQUEST;
484 asc = SCSI_ASC_ACCESS_DENIED_INVALID_LUN_ID;
485 ascq = SCSI_ASCQ_INVALID_LUN_ID;
488 /* Unspecified/Default */
489 case NVME_SC_CMDID_CONFLICT:
490 case NVME_SC_CMD_SEQ_ERROR:
491 case NVME_SC_CQ_INVALID:
492 case NVME_SC_QID_INVALID:
493 case NVME_SC_QUEUE_SIZE:
494 case NVME_SC_ABORT_LIMIT:
495 case NVME_SC_ABORT_MISSING:
496 case NVME_SC_ASYNC_LIMIT:
497 case NVME_SC_FIRMWARE_SLOT:
498 case NVME_SC_FIRMWARE_IMAGE:
499 case NVME_SC_INVALID_VECTOR:
500 case NVME_SC_INVALID_LOG_PAGE:
502 status = SAM_STAT_CHECK_CONDITION;
503 sense_key = ILLEGAL_REQUEST;
504 asc = SCSI_ASC_NO_SENSE;
505 ascq = SCSI_ASCQ_CAUSE_NOT_REPORTABLE;
509 res = nvme_trans_completion(hdr, status, sense_key, asc, ascq);
510 return res ? res : nvme_sc;
513 /* INQUIRY Helper Functions */
515 static int nvme_trans_standard_inquiry_page(struct nvme_ns *ns,
516 struct sg_io_hdr *hdr, u8 *inq_response,
519 struct nvme_ctrl *ctrl = ns->ctrl;
520 struct nvme_id_ns *id_ns;
524 u8 resp_data_format = 0x02;
526 u8 cmdque = 0x01 << 1;
527 u8 fw_offset = sizeof(ctrl->firmware_rev);
529 /* nvme ns identify - use DPS value for PROTECT field */
530 nvme_sc = nvme_identify_ns(ctrl, ns->ns_id, &id_ns);
531 res = nvme_trans_status_code(hdr, nvme_sc);
541 memset(inq_response, 0, STANDARD_INQUIRY_LENGTH);
542 inq_response[2] = VERSION_SPC_4;
543 inq_response[3] = resp_data_format; /*normaca=0 | hisup=0 */
544 inq_response[4] = ADDITIONAL_STD_INQ_LENGTH;
545 inq_response[5] = protect; /* sccs=0 | acc=0 | tpgs=0 | pc3=0 */
546 inq_response[7] = cmdque; /* wbus16=0 | sync=0 | vs=0 */
547 strncpy(&inq_response[8], "NVMe ", 8);
548 strncpy(&inq_response[16], ctrl->model, 16);
550 while (ctrl->firmware_rev[fw_offset - 1] == ' ' && fw_offset > 4)
553 strncpy(&inq_response[32], ctrl->firmware_rev + fw_offset, 4);
555 xfer_len = min(alloc_len, STANDARD_INQUIRY_LENGTH);
556 return nvme_trans_copy_to_user(hdr, inq_response, xfer_len);
559 static int nvme_trans_supported_vpd_pages(struct nvme_ns *ns,
560 struct sg_io_hdr *hdr, u8 *inq_response,
565 memset(inq_response, 0, STANDARD_INQUIRY_LENGTH);
566 inq_response[1] = INQ_SUPPORTED_VPD_PAGES_PAGE; /* Page Code */
567 inq_response[3] = INQ_NUM_SUPPORTED_VPD_PAGES; /* Page Length */
568 inq_response[4] = INQ_SUPPORTED_VPD_PAGES_PAGE;
569 inq_response[5] = INQ_UNIT_SERIAL_NUMBER_PAGE;
570 inq_response[6] = INQ_DEVICE_IDENTIFICATION_PAGE;
571 inq_response[7] = INQ_EXTENDED_INQUIRY_DATA_PAGE;
572 inq_response[8] = INQ_BDEV_CHARACTERISTICS_PAGE;
573 inq_response[9] = INQ_BDEV_LIMITS_PAGE;
575 xfer_len = min(alloc_len, STANDARD_INQUIRY_LENGTH);
576 return nvme_trans_copy_to_user(hdr, inq_response, xfer_len);
579 static int nvme_trans_unit_serial_page(struct nvme_ns *ns,
580 struct sg_io_hdr *hdr, u8 *inq_response,
585 memset(inq_response, 0, STANDARD_INQUIRY_LENGTH);
586 inq_response[1] = INQ_UNIT_SERIAL_NUMBER_PAGE; /* Page Code */
587 inq_response[3] = INQ_SERIAL_NUMBER_LENGTH; /* Page Length */
588 strncpy(&inq_response[4], ns->ctrl->serial, INQ_SERIAL_NUMBER_LENGTH);
590 xfer_len = min(alloc_len, STANDARD_INQUIRY_LENGTH);
591 return nvme_trans_copy_to_user(hdr, inq_response, xfer_len);
594 static int nvme_fill_device_id_eui64(struct nvme_ns *ns, struct sg_io_hdr *hdr,
595 u8 *inq_response, int alloc_len)
597 struct nvme_id_ns *id_ns;
602 nvme_sc = nvme_identify_ns(ns->ctrl, ns->ns_id, &id_ns);
603 res = nvme_trans_status_code(hdr, nvme_sc);
608 len = sizeof(id_ns->eui64);
610 if (ns->ctrl->vs >= NVME_VS(1, 2, 0)) {
611 if (bitmap_empty(eui, len * 8)) {
613 len = sizeof(id_ns->nguid);
617 if (bitmap_empty(eui, len * 8)) {
622 memset(inq_response, 0, alloc_len);
623 inq_response[1] = INQ_DEVICE_IDENTIFICATION_PAGE;
624 inq_response[3] = 4 + len; /* Page Length */
626 /* Designation Descriptor start */
627 inq_response[4] = 0x01; /* Proto ID=0h | Code set=1h */
628 inq_response[5] = 0x02; /* PIV=0b | Asso=00b | Designator Type=2h */
629 inq_response[6] = 0x00; /* Rsvd */
630 inq_response[7] = len; /* Designator Length */
631 memcpy(&inq_response[8], eui, len);
633 res = nvme_trans_copy_to_user(hdr, inq_response, alloc_len);
639 static int nvme_fill_device_id_scsi_string(struct nvme_ns *ns,
640 struct sg_io_hdr *hdr, u8 *inq_response, int alloc_len)
642 struct nvme_ctrl *ctrl = ns->ctrl;
643 struct nvme_id_ctrl *id_ctrl;
646 if (alloc_len < 72) {
647 return nvme_trans_completion(hdr,
648 SAM_STAT_CHECK_CONDITION,
649 ILLEGAL_REQUEST, SCSI_ASC_INVALID_CDB,
650 SCSI_ASCQ_CAUSE_NOT_REPORTABLE);
653 nvme_sc = nvme_identify_ctrl(ctrl, &id_ctrl);
654 res = nvme_trans_status_code(hdr, nvme_sc);
658 memset(inq_response, 0, alloc_len);
659 inq_response[1] = INQ_DEVICE_IDENTIFICATION_PAGE;
660 inq_response[3] = 0x48; /* Page Length */
662 /* Designation Descriptor start */
663 inq_response[4] = 0x03; /* Proto ID=0h | Code set=3h */
664 inq_response[5] = 0x08; /* PIV=0b | Asso=00b | Designator Type=8h */
665 inq_response[6] = 0x00; /* Rsvd */
666 inq_response[7] = 0x44; /* Designator Length */
668 sprintf(&inq_response[8], "%04x", le16_to_cpu(id_ctrl->vid));
669 memcpy(&inq_response[12], ctrl->model, sizeof(ctrl->model));
670 sprintf(&inq_response[52], "%04x", cpu_to_be32(ns->ns_id));
671 memcpy(&inq_response[56], ctrl->serial, sizeof(ctrl->serial));
673 res = nvme_trans_copy_to_user(hdr, inq_response, alloc_len);
678 static int nvme_trans_device_id_page(struct nvme_ns *ns, struct sg_io_hdr *hdr,
679 u8 *resp, int alloc_len)
683 if (ns->ctrl->vs >= NVME_VS(1, 1, 0)) {
684 res = nvme_fill_device_id_eui64(ns, hdr, resp, alloc_len);
685 if (res != -EOPNOTSUPP)
689 return nvme_fill_device_id_scsi_string(ns, hdr, resp, alloc_len);
692 static int nvme_trans_ext_inq_page(struct nvme_ns *ns, struct sg_io_hdr *hdr,
698 struct nvme_ctrl *ctrl = ns->ctrl;
699 struct nvme_id_ctrl *id_ctrl;
700 struct nvme_id_ns *id_ns;
704 u8 spt_lut[8] = {0, 0, 2, 1, 4, 6, 5, 7};
705 u8 grd_chk, app_chk, ref_chk, protect;
710 inq_response = kmalloc(EXTENDED_INQUIRY_DATA_PAGE_LENGTH, GFP_KERNEL);
711 if (inq_response == NULL)
714 nvme_sc = nvme_identify_ns(ctrl, ns->ns_id, &id_ns);
715 res = nvme_trans_status_code(hdr, nvme_sc);
719 spt = spt_lut[id_ns->dpc & 0x07] << 3;
726 grd_chk = protect << 2;
727 app_chk = protect << 1;
730 nvme_sc = nvme_identify_ctrl(ctrl, &id_ctrl);
731 res = nvme_trans_status_code(hdr, nvme_sc);
735 v_sup = id_ctrl->vwc;
738 memset(inq_response, 0, EXTENDED_INQUIRY_DATA_PAGE_LENGTH);
739 inq_response[1] = INQ_EXTENDED_INQUIRY_DATA_PAGE; /* Page Code */
740 inq_response[2] = 0x00; /* Page Length MSB */
741 inq_response[3] = 0x3C; /* Page Length LSB */
742 inq_response[4] = microcode | spt | grd_chk | app_chk | ref_chk;
743 inq_response[5] = uask_sup;
744 inq_response[6] = v_sup;
745 inq_response[7] = luiclr;
749 xfer_len = min(alloc_len, EXTENDED_INQUIRY_DATA_PAGE_LENGTH);
750 res = nvme_trans_copy_to_user(hdr, inq_response, xfer_len);
757 static int nvme_trans_bdev_limits_page(struct nvme_ns *ns, struct sg_io_hdr *hdr,
758 u8 *inq_response, int alloc_len)
760 __be32 max_sectors = cpu_to_be32(
761 nvme_block_nr(ns, queue_max_hw_sectors(ns->queue)));
762 __be32 max_discard = cpu_to_be32(ns->queue->limits.max_discard_sectors);
763 __be32 discard_desc_count = cpu_to_be32(0x100);
765 memset(inq_response, 0, STANDARD_INQUIRY_LENGTH);
766 inq_response[1] = VPD_BLOCK_LIMITS;
767 inq_response[3] = 0x3c; /* Page Length */
768 memcpy(&inq_response[8], &max_sectors, sizeof(u32));
769 memcpy(&inq_response[20], &max_discard, sizeof(u32));
772 memcpy(&inq_response[24], &discard_desc_count, sizeof(u32));
774 return nvme_trans_copy_to_user(hdr, inq_response, 0x3c);
777 static int nvme_trans_bdev_char_page(struct nvme_ns *ns, struct sg_io_hdr *hdr,
784 inq_response = kzalloc(EXTENDED_INQUIRY_DATA_PAGE_LENGTH, GFP_KERNEL);
785 if (inq_response == NULL) {
790 inq_response[1] = INQ_BDEV_CHARACTERISTICS_PAGE; /* Page Code */
791 inq_response[2] = 0x00; /* Page Length MSB */
792 inq_response[3] = 0x3C; /* Page Length LSB */
793 inq_response[4] = 0x00; /* Medium Rotation Rate MSB */
794 inq_response[5] = 0x01; /* Medium Rotation Rate LSB */
795 inq_response[6] = 0x00; /* Form Factor */
797 xfer_len = min(alloc_len, EXTENDED_INQUIRY_DATA_PAGE_LENGTH);
798 res = nvme_trans_copy_to_user(hdr, inq_response, xfer_len);
805 /* LOG SENSE Helper Functions */
807 static int nvme_trans_log_supp_pages(struct nvme_ns *ns, struct sg_io_hdr *hdr,
814 log_response = kzalloc(LOG_PAGE_SUPPORTED_LOG_PAGES_LENGTH, GFP_KERNEL);
815 if (log_response == NULL) {
820 log_response[0] = LOG_PAGE_SUPPORTED_LOG_PAGES_PAGE;
821 /* Subpage=0x00, Page Length MSB=0 */
822 log_response[3] = SUPPORTED_LOG_PAGES_PAGE_LENGTH;
823 log_response[4] = LOG_PAGE_SUPPORTED_LOG_PAGES_PAGE;
824 log_response[5] = LOG_PAGE_INFORMATIONAL_EXCEPTIONS_PAGE;
825 log_response[6] = LOG_PAGE_TEMPERATURE_PAGE;
827 xfer_len = min(alloc_len, LOG_PAGE_SUPPORTED_LOG_PAGES_LENGTH);
828 res = nvme_trans_copy_to_user(hdr, log_response, xfer_len);
835 static int nvme_trans_log_info_exceptions(struct nvme_ns *ns,
836 struct sg_io_hdr *hdr, int alloc_len)
841 struct nvme_smart_log *smart_log;
845 log_response = kzalloc(LOG_INFO_EXCP_PAGE_LENGTH, GFP_KERNEL);
846 if (log_response == NULL)
849 res = nvme_get_log_page(ns->ctrl, &smart_log);
851 goto out_free_response;
853 if (res != NVME_SC_SUCCESS) {
854 temp_c = LOG_TEMP_UNKNOWN;
856 temp_k = (smart_log->temperature[1] << 8) +
857 (smart_log->temperature[0]);
858 temp_c = temp_k - KELVIN_TEMP_FACTOR;
862 log_response[0] = LOG_PAGE_INFORMATIONAL_EXCEPTIONS_PAGE;
863 /* Subpage=0x00, Page Length MSB=0 */
864 log_response[3] = REMAINING_INFO_EXCP_PAGE_LENGTH;
865 /* Informational Exceptions Log Parameter 1 Start */
866 /* Parameter Code=0x0000 bytes 4,5 */
867 log_response[6] = 0x23; /* DU=0, TSD=1, ETC=0, TMC=0, FMT_AND_LNK=11b */
868 log_response[7] = 0x04; /* PARAMETER LENGTH */
869 /* Add sense Code and qualifier = 0x00 each */
870 /* Use Temperature from NVMe Get Log Page, convert to C from K */
871 log_response[10] = temp_c;
873 xfer_len = min(alloc_len, LOG_INFO_EXCP_PAGE_LENGTH);
874 res = nvme_trans_copy_to_user(hdr, log_response, xfer_len);
881 static int nvme_trans_log_temperature(struct nvme_ns *ns, struct sg_io_hdr *hdr,
887 struct nvme_smart_log *smart_log;
889 u8 temp_c_cur, temp_c_thresh;
892 log_response = kzalloc(LOG_TEMP_PAGE_LENGTH, GFP_KERNEL);
893 if (log_response == NULL)
896 res = nvme_get_log_page(ns->ctrl, &smart_log);
898 goto out_free_response;
900 if (res != NVME_SC_SUCCESS) {
901 temp_c_cur = LOG_TEMP_UNKNOWN;
903 temp_k = (smart_log->temperature[1] << 8) +
904 (smart_log->temperature[0]);
905 temp_c_cur = temp_k - KELVIN_TEMP_FACTOR;
909 /* Get Features for Temp Threshold */
910 res = nvme_get_features(ns->ctrl, NVME_FEAT_TEMP_THRESH, 0, NULL, 0,
912 if (res != NVME_SC_SUCCESS)
913 temp_c_thresh = LOG_TEMP_UNKNOWN;
915 temp_c_thresh = (feature_resp & 0xFFFF) - KELVIN_TEMP_FACTOR;
917 log_response[0] = LOG_PAGE_TEMPERATURE_PAGE;
918 /* Subpage=0x00, Page Length MSB=0 */
919 log_response[3] = REMAINING_TEMP_PAGE_LENGTH;
920 /* Temperature Log Parameter 1 (Temperature) Start */
921 /* Parameter Code = 0x0000 */
922 log_response[6] = 0x01; /* Format and Linking = 01b */
923 log_response[7] = 0x02; /* Parameter Length */
924 /* Use Temperature from NVMe Get Log Page, convert to C from K */
925 log_response[9] = temp_c_cur;
926 /* Temperature Log Parameter 2 (Reference Temperature) Start */
927 log_response[11] = 0x01; /* Parameter Code = 0x0001 */
928 log_response[12] = 0x01; /* Format and Linking = 01b */
929 log_response[13] = 0x02; /* Parameter Length */
930 /* Use Temperature Thresh from NVMe Get Log Page, convert to C from K */
931 log_response[15] = temp_c_thresh;
933 xfer_len = min(alloc_len, LOG_TEMP_PAGE_LENGTH);
934 res = nvme_trans_copy_to_user(hdr, log_response, xfer_len);
941 /* MODE SENSE Helper Functions */
943 static int nvme_trans_fill_mode_parm_hdr(u8 *resp, int len, u8 cdb10, u8 llbaa,
944 u16 mode_data_length, u16 blk_desc_len)
946 /* Quick check to make sure I don't stomp on my own memory... */
947 if ((cdb10 && len < 8) || (!cdb10 && len < 4))
951 resp[0] = (mode_data_length & 0xFF00) >> 8;
952 resp[1] = (mode_data_length & 0x00FF);
953 resp[3] = 0x10 /* DPOFUA */;
955 resp[5] = RESERVED_FIELD;
956 resp[6] = (blk_desc_len & 0xFF00) >> 8;
957 resp[7] = (blk_desc_len & 0x00FF);
959 resp[0] = (mode_data_length & 0x00FF);
960 resp[2] = 0x10 /* DPOFUA */;
961 resp[3] = (blk_desc_len & 0x00FF);
967 static int nvme_trans_fill_blk_desc(struct nvme_ns *ns, struct sg_io_hdr *hdr,
968 u8 *resp, int len, u8 llbaa)
972 struct nvme_id_ns *id_ns;
976 if (llbaa == 0 && len < MODE_PAGE_BLK_DES_LEN)
978 else if (llbaa > 0 && len < MODE_PAGE_LLBAA_BLK_DES_LEN)
981 nvme_sc = nvme_identify_ns(ns->ctrl, ns->ns_id, &id_ns);
982 res = nvme_trans_status_code(hdr, nvme_sc);
986 flbas = (id_ns->flbas) & 0x0F;
987 lba_length = (1 << (id_ns->lbaf[flbas].ds));
990 __be32 tmp_cap = cpu_to_be32(le64_to_cpu(id_ns->ncap));
991 /* Byte 4 is reserved */
992 __be32 tmp_len = cpu_to_be32(lba_length & 0x00FFFFFF);
994 memcpy(resp, &tmp_cap, sizeof(u32));
995 memcpy(&resp[4], &tmp_len, sizeof(u32));
997 __be64 tmp_cap = cpu_to_be64(le64_to_cpu(id_ns->ncap));
998 __be32 tmp_len = cpu_to_be32(lba_length);
1000 memcpy(resp, &tmp_cap, sizeof(u64));
1001 /* Bytes 8, 9, 10, 11 are reserved */
1002 memcpy(&resp[12], &tmp_len, sizeof(u32));
1009 static int nvme_trans_fill_control_page(struct nvme_ns *ns,
1010 struct sg_io_hdr *hdr, u8 *resp,
1013 if (len < MODE_PAGE_CONTROL_LEN)
1016 resp[0] = MODE_PAGE_CONTROL;
1017 resp[1] = MODE_PAGE_CONTROL_LEN_FIELD;
1018 resp[2] = 0x0E; /* TST=000b, TMF_ONLY=0, DPICZ=1,
1019 * D_SENSE=1, GLTSD=1, RLEC=0 */
1020 resp[3] = 0x12; /* Q_ALGO_MODIFIER=1h, NUAR=0, QERR=01b */
1021 /* Byte 4: VS=0, RAC=0, UA_INT=0, SWP=0 */
1022 resp[5] = 0x40; /* ATO=0, TAS=1, ATMPE=0, RWWP=0, AUTOLOAD=0 */
1023 /* resp[6] and [7] are obsolete, thus zero */
1024 resp[8] = 0xFF; /* Busy timeout period = 0xffff */
1026 /* Bytes 10,11: Extended selftest completion time = 0x0000 */
1031 static int nvme_trans_fill_caching_page(struct nvme_ns *ns,
1032 struct sg_io_hdr *hdr,
1040 if (len < MODE_PAGE_CACHING_LEN)
1043 nvme_sc = nvme_get_features(ns->ctrl, NVME_FEAT_VOLATILE_WC, 0, NULL, 0,
1045 res = nvme_trans_status_code(hdr, nvme_sc);
1049 vwc = feature_resp & 0x00000001;
1051 resp[0] = MODE_PAGE_CACHING;
1052 resp[1] = MODE_PAGE_CACHING_LEN_FIELD;
1057 static int nvme_trans_fill_pow_cnd_page(struct nvme_ns *ns,
1058 struct sg_io_hdr *hdr, u8 *resp,
1061 if (len < MODE_PAGE_POW_CND_LEN)
1064 resp[0] = MODE_PAGE_POWER_CONDITION;
1065 resp[1] = MODE_PAGE_POW_CND_LEN_FIELD;
1066 /* All other bytes are zero */
1071 static int nvme_trans_fill_inf_exc_page(struct nvme_ns *ns,
1072 struct sg_io_hdr *hdr, u8 *resp,
1075 if (len < MODE_PAGE_INF_EXC_LEN)
1078 resp[0] = MODE_PAGE_INFO_EXCEP;
1079 resp[1] = MODE_PAGE_INF_EXC_LEN_FIELD;
1081 /* All other bytes are zero */
1086 static int nvme_trans_fill_all_pages(struct nvme_ns *ns, struct sg_io_hdr *hdr,
1090 u16 mode_pages_offset_1 = 0;
1091 u16 mode_pages_offset_2, mode_pages_offset_3, mode_pages_offset_4;
1093 mode_pages_offset_2 = mode_pages_offset_1 + MODE_PAGE_CACHING_LEN;
1094 mode_pages_offset_3 = mode_pages_offset_2 + MODE_PAGE_CONTROL_LEN;
1095 mode_pages_offset_4 = mode_pages_offset_3 + MODE_PAGE_POW_CND_LEN;
1097 res = nvme_trans_fill_caching_page(ns, hdr, &resp[mode_pages_offset_1],
1098 MODE_PAGE_CACHING_LEN);
1101 res = nvme_trans_fill_control_page(ns, hdr, &resp[mode_pages_offset_2],
1102 MODE_PAGE_CONTROL_LEN);
1105 res = nvme_trans_fill_pow_cnd_page(ns, hdr, &resp[mode_pages_offset_3],
1106 MODE_PAGE_POW_CND_LEN);
1109 return nvme_trans_fill_inf_exc_page(ns, hdr, &resp[mode_pages_offset_4],
1110 MODE_PAGE_INF_EXC_LEN);
1113 static inline int nvme_trans_get_blk_desc_len(u8 dbd, u8 llbaa)
1115 if (dbd == MODE_SENSE_BLK_DESC_ENABLED) {
1116 /* SPC-4: len = 8 x Num_of_descriptors if llbaa = 0, 16x if 1 */
1117 return 8 * (llbaa + 1) * MODE_SENSE_BLK_DESC_COUNT;
1123 static int nvme_trans_mode_page_create(struct nvme_ns *ns,
1124 struct sg_io_hdr *hdr, u8 *cmd,
1125 u16 alloc_len, u8 cdb10,
1126 int (*mode_page_fill_func)
1128 struct sg_io_hdr *hdr, u8 *, int),
1129 u16 mode_pages_tot_len)
1137 u16 mode_pages_offset_1;
1138 u16 blk_desc_len, blk_desc_offset, mode_data_length;
1140 dbd = (cmd[1] & MODE_SENSE_DBD_MASK) >> MODE_SENSE_DBD_SHIFT;
1141 llbaa = (cmd[1] & MODE_SENSE_LLBAA_MASK) >> MODE_SENSE_LLBAA_SHIFT;
1142 mph_size = cdb10 ? MODE_SENSE10_MPH_SIZE : MODE_SENSE6_MPH_SIZE;
1144 blk_desc_len = nvme_trans_get_blk_desc_len(dbd, llbaa);
1146 resp_size = mph_size + blk_desc_len + mode_pages_tot_len;
1147 /* Refer spc4r34 Table 440 for calculation of Mode data Length field */
1148 mode_data_length = 3 + (3 * cdb10) + blk_desc_len + mode_pages_tot_len;
1150 blk_desc_offset = mph_size;
1151 mode_pages_offset_1 = blk_desc_offset + blk_desc_len;
1153 response = kzalloc(resp_size, GFP_KERNEL);
1154 if (response == NULL) {
1159 res = nvme_trans_fill_mode_parm_hdr(&response[0], mph_size, cdb10,
1160 llbaa, mode_data_length, blk_desc_len);
1163 if (blk_desc_len > 0) {
1164 res = nvme_trans_fill_blk_desc(ns, hdr,
1165 &response[blk_desc_offset],
1166 blk_desc_len, llbaa);
1170 res = mode_page_fill_func(ns, hdr, &response[mode_pages_offset_1],
1171 mode_pages_tot_len);
1175 xfer_len = min(alloc_len, resp_size);
1176 res = nvme_trans_copy_to_user(hdr, response, xfer_len);
1184 /* Read Capacity Helper Functions */
1186 static void nvme_trans_fill_read_cap(u8 *response, struct nvme_id_ns *id_ns,
1193 u8 p_type_lut[4] = {0, 0, 1, 2};
1198 flbas = (id_ns->flbas) & 0x0F;
1199 lba_length = (1 << (id_ns->lbaf[flbas].ds));
1200 rlba = le64_to_cpup(&id_ns->nsze) - 1;
1201 (id_ns->dps) ? (prot_en = 0x01) : (prot_en = 0);
1204 if (rlba > 0xFFFFFFFF)
1206 tmp_rlba_32 = cpu_to_be32(rlba);
1207 tmp_len = cpu_to_be32(lba_length);
1208 memcpy(response, &tmp_rlba_32, sizeof(u32));
1209 memcpy(&response[4], &tmp_len, sizeof(u32));
1211 tmp_rlba = cpu_to_be64(rlba);
1212 tmp_len = cpu_to_be32(lba_length);
1213 memcpy(response, &tmp_rlba, sizeof(u64));
1214 memcpy(&response[8], &tmp_len, sizeof(u32));
1215 response[12] = (p_type_lut[id_ns->dps & 0x3] << 1) | prot_en;
1216 /* P_I_Exponent = 0x0 | LBPPBE = 0x0 */
1217 /* LBPME = 0 | LBPRZ = 0 | LALBA = 0x00 */
1218 /* Bytes 16-31 - Reserved */
1222 /* Start Stop Unit Helper Functions */
1224 static int nvme_trans_send_activate_fw_cmd(struct nvme_ns *ns, struct sg_io_hdr *hdr,
1227 struct nvme_command c;
1230 memset(&c, 0, sizeof(c));
1231 c.common.opcode = nvme_admin_activate_fw;
1232 c.common.cdw10[0] = cpu_to_le32(buffer_id | NVME_FWACT_REPL_ACTV);
1234 nvme_sc = nvme_submit_sync_cmd(ns->queue, &c, NULL, 0);
1235 return nvme_trans_status_code(hdr, nvme_sc);
1238 static int nvme_trans_send_download_fw_cmd(struct nvme_ns *ns, struct sg_io_hdr *hdr,
1239 u8 opcode, u32 tot_len, u32 offset,
1243 struct nvme_command c;
1245 if (hdr->iovec_count > 0) {
1246 /* Assuming SGL is not allowed for this command */
1247 return nvme_trans_completion(hdr,
1248 SAM_STAT_CHECK_CONDITION,
1250 SCSI_ASC_INVALID_CDB,
1251 SCSI_ASCQ_CAUSE_NOT_REPORTABLE);
1254 memset(&c, 0, sizeof(c));
1255 c.common.opcode = nvme_admin_download_fw;
1256 c.dlfw.numd = cpu_to_le32((tot_len/BYTES_TO_DWORDS) - 1);
1257 c.dlfw.offset = cpu_to_le32(offset/BYTES_TO_DWORDS);
1259 nvme_sc = nvme_submit_user_cmd(ns->ctrl->admin_q, &c,
1260 hdr->dxferp, tot_len, NULL, 0);
1261 return nvme_trans_status_code(hdr, nvme_sc);
1264 /* Mode Select Helper Functions */
1266 static inline void nvme_trans_modesel_get_bd_len(u8 *parm_list, u8 cdb10,
1267 u16 *bd_len, u8 *llbaa)
1271 *bd_len = (parm_list[MODE_SELECT_10_BD_OFFSET] << 8) +
1272 parm_list[MODE_SELECT_10_BD_OFFSET + 1];
1273 *llbaa = parm_list[MODE_SELECT_10_LLBAA_OFFSET] &
1274 MODE_SELECT_10_LLBAA_MASK;
1277 *bd_len = parm_list[MODE_SELECT_6_BD_OFFSET];
1281 static void nvme_trans_modesel_save_bd(struct nvme_ns *ns, u8 *parm_list,
1282 u16 idx, u16 bd_len, u8 llbaa)
1284 /* Store block descriptor info if a FORMAT UNIT comes later */
1285 /* TODO Saving 1st BD info; what to do if multiple BD received? */
1287 /* Standard Block Descriptor - spc4r34 7.5.5.1 */
1288 ns->mode_select_num_blocks =
1289 (parm_list[idx + 1] << 16) +
1290 (parm_list[idx + 2] << 8) +
1291 (parm_list[idx + 3]);
1293 ns->mode_select_block_len =
1294 (parm_list[idx + 5] << 16) +
1295 (parm_list[idx + 6] << 8) +
1296 (parm_list[idx + 7]);
1298 /* Long LBA Block Descriptor - sbc3r27 6.4.2.3 */
1299 ns->mode_select_num_blocks =
1300 (((u64)parm_list[idx + 0]) << 56) +
1301 (((u64)parm_list[idx + 1]) << 48) +
1302 (((u64)parm_list[idx + 2]) << 40) +
1303 (((u64)parm_list[idx + 3]) << 32) +
1304 (((u64)parm_list[idx + 4]) << 24) +
1305 (((u64)parm_list[idx + 5]) << 16) +
1306 (((u64)parm_list[idx + 6]) << 8) +
1307 ((u64)parm_list[idx + 7]);
1309 ns->mode_select_block_len =
1310 (parm_list[idx + 12] << 24) +
1311 (parm_list[idx + 13] << 16) +
1312 (parm_list[idx + 14] << 8) +
1313 (parm_list[idx + 15]);
1317 static int nvme_trans_modesel_get_mp(struct nvme_ns *ns, struct sg_io_hdr *hdr,
1318 u8 *mode_page, u8 page_code)
1324 switch (page_code) {
1325 case MODE_PAGE_CACHING:
1326 dword11 = ((mode_page[2] & CACHING_MODE_PAGE_WCE_MASK) ? 1 : 0);
1327 nvme_sc = nvme_set_features(ns->ctrl, NVME_FEAT_VOLATILE_WC,
1328 dword11, NULL, 0, NULL);
1329 res = nvme_trans_status_code(hdr, nvme_sc);
1331 case MODE_PAGE_CONTROL:
1333 case MODE_PAGE_POWER_CONDITION:
1334 /* Verify the OS is not trying to set timers */
1335 if ((mode_page[2] & 0x01) != 0 || (mode_page[3] & 0x0F) != 0) {
1336 res = nvme_trans_completion(hdr,
1337 SAM_STAT_CHECK_CONDITION,
1339 SCSI_ASC_INVALID_PARAMETER,
1340 SCSI_ASCQ_CAUSE_NOT_REPORTABLE);
1345 res = nvme_trans_completion(hdr, SAM_STAT_CHECK_CONDITION,
1346 ILLEGAL_REQUEST, SCSI_ASC_INVALID_CDB,
1347 SCSI_ASCQ_CAUSE_NOT_REPORTABLE);
1354 static int nvme_trans_modesel_data(struct nvme_ns *ns, struct sg_io_hdr *hdr,
1355 u8 *cmd, u16 parm_list_len, u8 pf,
1362 u16 index, saved_index;
1366 /* Get parm list from data-in/out buffer */
1367 parm_list = kmalloc(parm_list_len, GFP_KERNEL);
1368 if (parm_list == NULL) {
1373 res = nvme_trans_copy_from_user(hdr, parm_list, parm_list_len);
1377 nvme_trans_modesel_get_bd_len(parm_list, cdb10, &bd_len, &llbaa);
1378 index = (cdb10) ? (MODE_SELECT_10_MPH_SIZE) : (MODE_SELECT_6_MPH_SIZE);
1381 /* Block Descriptors present, parse */
1382 nvme_trans_modesel_save_bd(ns, parm_list, index, bd_len, llbaa);
1385 saved_index = index;
1387 /* Multiple mode pages may be present; iterate through all */
1388 /* In 1st Iteration, don't do NVME Command, only check for CDB errors */
1390 page_code = parm_list[index] & MODE_SELECT_PAGE_CODE_MASK;
1391 mp_size = parm_list[index + 1] + 2;
1392 if ((page_code != MODE_PAGE_CACHING) &&
1393 (page_code != MODE_PAGE_CONTROL) &&
1394 (page_code != MODE_PAGE_POWER_CONDITION)) {
1395 res = nvme_trans_completion(hdr,
1396 SAM_STAT_CHECK_CONDITION,
1398 SCSI_ASC_INVALID_CDB,
1399 SCSI_ASCQ_CAUSE_NOT_REPORTABLE);
1403 } while (index < parm_list_len);
1405 /* In 2nd Iteration, do the NVME Commands */
1406 index = saved_index;
1408 page_code = parm_list[index] & MODE_SELECT_PAGE_CODE_MASK;
1409 mp_size = parm_list[index + 1] + 2;
1410 res = nvme_trans_modesel_get_mp(ns, hdr, &parm_list[index],
1415 } while (index < parm_list_len);
1423 /* Format Unit Helper Functions */
1425 static int nvme_trans_fmt_set_blk_size_count(struct nvme_ns *ns,
1426 struct sg_io_hdr *hdr)
1433 * SCSI Expects a MODE SELECT would have been issued prior to
1434 * a FORMAT UNIT, and the block size and number would be used
1435 * from the block descriptor in it. If a MODE SELECT had not
1436 * been issued, FORMAT shall use the current values for both.
1439 if (ns->mode_select_num_blocks == 0 || ns->mode_select_block_len == 0) {
1440 struct nvme_id_ns *id_ns;
1442 nvme_sc = nvme_identify_ns(ns->ctrl, ns->ns_id, &id_ns);
1443 res = nvme_trans_status_code(hdr, nvme_sc);
1447 if (ns->mode_select_num_blocks == 0)
1448 ns->mode_select_num_blocks = le64_to_cpu(id_ns->ncap);
1449 if (ns->mode_select_block_len == 0) {
1450 flbas = (id_ns->flbas) & 0x0F;
1451 ns->mode_select_block_len =
1452 (1 << (id_ns->lbaf[flbas].ds));
1461 static int nvme_trans_fmt_get_parm_header(struct sg_io_hdr *hdr, u8 len,
1462 u8 format_prot_info, u8 *nvme_pf_code)
1466 u8 pf_usage, pf_code;
1468 parm_list = kmalloc(len, GFP_KERNEL);
1469 if (parm_list == NULL) {
1473 res = nvme_trans_copy_from_user(hdr, parm_list, len);
1477 if ((parm_list[FORMAT_UNIT_IMMED_OFFSET] &
1478 FORMAT_UNIT_IMMED_MASK) != 0) {
1479 res = nvme_trans_completion(hdr, SAM_STAT_CHECK_CONDITION,
1480 ILLEGAL_REQUEST, SCSI_ASC_INVALID_CDB,
1481 SCSI_ASCQ_CAUSE_NOT_REPORTABLE);
1485 if (len == FORMAT_UNIT_LONG_PARM_LIST_LEN &&
1486 (parm_list[FORMAT_UNIT_PROT_INT_OFFSET] & 0x0F) != 0) {
1487 res = nvme_trans_completion(hdr, SAM_STAT_CHECK_CONDITION,
1488 ILLEGAL_REQUEST, SCSI_ASC_INVALID_CDB,
1489 SCSI_ASCQ_CAUSE_NOT_REPORTABLE);
1492 pf_usage = parm_list[FORMAT_UNIT_PROT_FIELD_USAGE_OFFSET] &
1493 FORMAT_UNIT_PROT_FIELD_USAGE_MASK;
1494 pf_code = (pf_usage << 2) | format_prot_info;
1509 res = nvme_trans_completion(hdr, SAM_STAT_CHECK_CONDITION,
1510 ILLEGAL_REQUEST, SCSI_ASC_INVALID_CDB,
1511 SCSI_ASCQ_CAUSE_NOT_REPORTABLE);
1521 static int nvme_trans_fmt_send_cmd(struct nvme_ns *ns, struct sg_io_hdr *hdr,
1526 struct nvme_id_ns *id_ns;
1529 u8 selected_lbaf = 0xFF;
1531 struct nvme_command c;
1533 /* Loop thru LBAF's in id_ns to match reqd lbaf, put in cdw10 */
1534 nvme_sc = nvme_identify_ns(ns->ctrl, ns->ns_id, &id_ns);
1535 res = nvme_trans_status_code(hdr, nvme_sc);
1539 nlbaf = id_ns->nlbaf;
1541 for (i = 0; i < nlbaf; i++) {
1542 if (ns->mode_select_block_len == (1 << (id_ns->lbaf[i].ds))) {
1547 if (selected_lbaf > 0x0F) {
1548 res = nvme_trans_completion(hdr, SAM_STAT_CHECK_CONDITION,
1549 ILLEGAL_REQUEST, SCSI_ASC_INVALID_PARAMETER,
1550 SCSI_ASCQ_CAUSE_NOT_REPORTABLE);
1552 if (ns->mode_select_num_blocks != le64_to_cpu(id_ns->ncap)) {
1553 res = nvme_trans_completion(hdr, SAM_STAT_CHECK_CONDITION,
1554 ILLEGAL_REQUEST, SCSI_ASC_INVALID_PARAMETER,
1555 SCSI_ASCQ_CAUSE_NOT_REPORTABLE);
1558 cdw10 |= prot_info << 5;
1559 cdw10 |= selected_lbaf & 0x0F;
1560 memset(&c, 0, sizeof(c));
1561 c.format.opcode = nvme_admin_format_nvm;
1562 c.format.nsid = cpu_to_le32(ns->ns_id);
1563 c.format.cdw10 = cpu_to_le32(cdw10);
1565 nvme_sc = nvme_submit_sync_cmd(ns->ctrl->admin_q, &c, NULL, 0);
1566 res = nvme_trans_status_code(hdr, nvme_sc);
1572 static inline u32 nvme_trans_io_get_num_cmds(struct sg_io_hdr *hdr,
1573 struct nvme_trans_io_cdb *cdb_info,
1576 /* If using iovecs, send one nvme command per vector */
1577 if (hdr->iovec_count > 0)
1578 return hdr->iovec_count;
1579 else if (cdb_info->xfer_len > max_blocks)
1580 return ((cdb_info->xfer_len - 1) / max_blocks) + 1;
1585 static u16 nvme_trans_io_get_control(struct nvme_ns *ns,
1586 struct nvme_trans_io_cdb *cdb_info)
1590 /* When Protection information support is added, implement here */
1592 if (cdb_info->fua > 0)
1593 control |= NVME_RW_FUA;
1598 static int nvme_trans_do_nvme_io(struct nvme_ns *ns, struct sg_io_hdr *hdr,
1599 struct nvme_trans_io_cdb *cdb_info, u8 is_write)
1601 int nvme_sc = NVME_SC_SUCCESS;
1604 u64 unit_num_blocks; /* Number of blocks to xfer in each nvme cmd */
1607 u64 nvme_offset = 0;
1608 void __user *next_mapping_addr;
1609 struct nvme_command c;
1610 u8 opcode = (is_write ? nvme_cmd_write : nvme_cmd_read);
1612 u32 max_blocks = queue_max_hw_sectors(ns->queue) >> (ns->lba_shift - 9);
1614 num_cmds = nvme_trans_io_get_num_cmds(hdr, cdb_info, max_blocks);
1617 * This loop handles two cases.
1618 * First, when an SGL is used in the form of an iovec list:
1619 * - Use iov_base as the next mapping address for the nvme command_id
1620 * - Use iov_len as the data transfer length for the command.
1621 * Second, when we have a single buffer
1622 * - If larger than max_blocks, split into chunks, offset
1623 * each nvme command accordingly.
1625 for (i = 0; i < num_cmds; i++) {
1626 memset(&c, 0, sizeof(c));
1627 if (hdr->iovec_count > 0) {
1628 struct sg_iovec sgl;
1630 retcode = copy_from_user(&sgl, hdr->dxferp +
1631 i * sizeof(struct sg_iovec),
1632 sizeof(struct sg_iovec));
1635 unit_len = sgl.iov_len;
1636 unit_num_blocks = unit_len >> ns->lba_shift;
1637 next_mapping_addr = sgl.iov_base;
1639 unit_num_blocks = min((u64)max_blocks,
1640 (cdb_info->xfer_len - nvme_offset));
1641 unit_len = unit_num_blocks << ns->lba_shift;
1642 next_mapping_addr = hdr->dxferp +
1643 ((1 << ns->lba_shift) * nvme_offset);
1646 c.rw.opcode = opcode;
1647 c.rw.nsid = cpu_to_le32(ns->ns_id);
1648 c.rw.slba = cpu_to_le64(cdb_info->lba + nvme_offset);
1649 c.rw.length = cpu_to_le16(unit_num_blocks - 1);
1650 control = nvme_trans_io_get_control(ns, cdb_info);
1651 c.rw.control = cpu_to_le16(control);
1653 if (get_capacity(ns->disk) - unit_num_blocks <
1654 cdb_info->lba + nvme_offset) {
1655 nvme_sc = NVME_SC_LBA_RANGE;
1658 nvme_sc = nvme_submit_user_cmd(ns->queue, &c,
1659 next_mapping_addr, unit_len, NULL, 0);
1663 nvme_offset += unit_num_blocks;
1666 return nvme_trans_status_code(hdr, nvme_sc);
1670 /* SCSI Command Translation Functions */
1672 static int nvme_trans_io(struct nvme_ns *ns, struct sg_io_hdr *hdr, u8 is_write,
1676 struct nvme_trans_io_cdb cdb_info = { 0, };
1679 u64 sum_iov_len = 0;
1680 struct sg_iovec sgl;
1685 * The FUA and WPROTECT fields are not supported in 6-byte CDBs,
1686 * but always in the same place for all others.
1693 cdb_info.fua = cmd[1] & 0x8;
1694 cdb_info.prot_info = (cmd[1] & 0xe0) >> 5;
1695 if (cdb_info.prot_info && !ns->pi_type) {
1696 return nvme_trans_completion(hdr,
1697 SAM_STAT_CHECK_CONDITION,
1699 SCSI_ASC_INVALID_CDB,
1700 SCSI_ASCQ_CAUSE_NOT_REPORTABLE);
1707 cdb_info.lba = get_unaligned_be24(&cmd[1]);
1708 cdb_info.xfer_len = cmd[4];
1709 if (cdb_info.xfer_len == 0)
1710 cdb_info.xfer_len = 256;
1714 cdb_info.lba = get_unaligned_be32(&cmd[2]);
1715 cdb_info.xfer_len = get_unaligned_be16(&cmd[7]);
1719 cdb_info.lba = get_unaligned_be32(&cmd[2]);
1720 cdb_info.xfer_len = get_unaligned_be32(&cmd[6]);
1724 cdb_info.lba = get_unaligned_be64(&cmd[2]);
1725 cdb_info.xfer_len = get_unaligned_be32(&cmd[10]);
1728 /* Will never really reach here */
1733 /* Calculate total length of transfer (in bytes) */
1734 if (hdr->iovec_count > 0) {
1735 for (i = 0; i < hdr->iovec_count; i++) {
1736 not_copied = copy_from_user(&sgl, hdr->dxferp +
1737 i * sizeof(struct sg_iovec),
1738 sizeof(struct sg_iovec));
1741 sum_iov_len += sgl.iov_len;
1742 /* IO vector sizes should be multiples of block size */
1743 if (sgl.iov_len % (1 << ns->lba_shift) != 0) {
1744 res = nvme_trans_completion(hdr,
1745 SAM_STAT_CHECK_CONDITION,
1747 SCSI_ASC_INVALID_PARAMETER,
1748 SCSI_ASCQ_CAUSE_NOT_REPORTABLE);
1753 sum_iov_len = hdr->dxfer_len;
1756 /* As Per sg ioctl howto, if the lengths differ, use the lower one */
1757 xfer_bytes = min(((u64)hdr->dxfer_len), sum_iov_len);
1759 /* If block count and actual data buffer size dont match, error out */
1760 if (xfer_bytes != (cdb_info.xfer_len << ns->lba_shift)) {
1765 /* Check for 0 length transfer - it is not illegal */
1766 if (cdb_info.xfer_len == 0)
1769 /* Send NVMe IO Command(s) */
1770 res = nvme_trans_do_nvme_io(ns, hdr, &cdb_info, is_write);
1778 static int nvme_trans_inquiry(struct nvme_ns *ns, struct sg_io_hdr *hdr,
1787 evpd = cmd[1] & 0x01;
1789 alloc_len = get_unaligned_be16(&cmd[3]);
1791 inq_response = kmalloc(max(alloc_len, STANDARD_INQUIRY_LENGTH),
1793 if (inq_response == NULL) {
1799 if (page_code == INQ_STANDARD_INQUIRY_PAGE) {
1800 res = nvme_trans_standard_inquiry_page(ns, hdr,
1801 inq_response, alloc_len);
1803 res = nvme_trans_completion(hdr,
1804 SAM_STAT_CHECK_CONDITION,
1806 SCSI_ASC_INVALID_CDB,
1807 SCSI_ASCQ_CAUSE_NOT_REPORTABLE);
1810 switch (page_code) {
1811 case VPD_SUPPORTED_PAGES:
1812 res = nvme_trans_supported_vpd_pages(ns, hdr,
1813 inq_response, alloc_len);
1815 case VPD_SERIAL_NUMBER:
1816 res = nvme_trans_unit_serial_page(ns, hdr, inq_response,
1819 case VPD_DEVICE_IDENTIFIERS:
1820 res = nvme_trans_device_id_page(ns, hdr, inq_response,
1823 case VPD_EXTENDED_INQUIRY:
1824 res = nvme_trans_ext_inq_page(ns, hdr, alloc_len);
1826 case VPD_BLOCK_LIMITS:
1827 res = nvme_trans_bdev_limits_page(ns, hdr, inq_response,
1830 case VPD_BLOCK_DEV_CHARACTERISTICS:
1831 res = nvme_trans_bdev_char_page(ns, hdr, alloc_len);
1834 res = nvme_trans_completion(hdr,
1835 SAM_STAT_CHECK_CONDITION,
1837 SCSI_ASC_INVALID_CDB,
1838 SCSI_ASCQ_CAUSE_NOT_REPORTABLE);
1842 kfree(inq_response);
1847 static int nvme_trans_log_sense(struct nvme_ns *ns, struct sg_io_hdr *hdr,
1855 if (cmd[1] != LOG_SENSE_CDB_SP_NOT_ENABLED) {
1856 res = nvme_trans_completion(hdr, SAM_STAT_CHECK_CONDITION,
1857 ILLEGAL_REQUEST, SCSI_ASC_INVALID_CDB,
1858 SCSI_ASCQ_CAUSE_NOT_REPORTABLE);
1862 page_code = cmd[2] & LOG_SENSE_CDB_PAGE_CODE_MASK;
1863 pc = (cmd[2] & LOG_SENSE_CDB_PC_MASK) >> LOG_SENSE_CDB_PC_SHIFT;
1864 if (pc != LOG_SENSE_CDB_PC_CUMULATIVE_VALUES) {
1865 res = nvme_trans_completion(hdr, SAM_STAT_CHECK_CONDITION,
1866 ILLEGAL_REQUEST, SCSI_ASC_INVALID_CDB,
1867 SCSI_ASCQ_CAUSE_NOT_REPORTABLE);
1870 alloc_len = get_unaligned_be16(&cmd[7]);
1871 switch (page_code) {
1872 case LOG_PAGE_SUPPORTED_LOG_PAGES_PAGE:
1873 res = nvme_trans_log_supp_pages(ns, hdr, alloc_len);
1875 case LOG_PAGE_INFORMATIONAL_EXCEPTIONS_PAGE:
1876 res = nvme_trans_log_info_exceptions(ns, hdr, alloc_len);
1878 case LOG_PAGE_TEMPERATURE_PAGE:
1879 res = nvme_trans_log_temperature(ns, hdr, alloc_len);
1882 res = nvme_trans_completion(hdr, SAM_STAT_CHECK_CONDITION,
1883 ILLEGAL_REQUEST, SCSI_ASC_INVALID_CDB,
1884 SCSI_ASCQ_CAUSE_NOT_REPORTABLE);
1892 static int nvme_trans_mode_select(struct nvme_ns *ns, struct sg_io_hdr *hdr,
1900 page_format = cmd[1] & MODE_SELECT_CDB_PAGE_FORMAT_MASK;
1901 save_pages = cmd[1] & MODE_SELECT_CDB_SAVE_PAGES_MASK;
1903 if (cmd[0] == MODE_SELECT) {
1904 parm_list_len = cmd[4];
1906 parm_list_len = cmd[7];
1910 if (parm_list_len != 0) {
1912 * According to SPC-4 r24, a paramter list length field of 0
1913 * shall not be considered an error
1915 return nvme_trans_modesel_data(ns, hdr, cmd, parm_list_len,
1916 page_format, save_pages, cdb10);
1922 static int nvme_trans_mode_sense(struct nvme_ns *ns, struct sg_io_hdr *hdr,
1929 if (cmd[0] == MODE_SENSE) {
1932 alloc_len = get_unaligned_be16(&cmd[7]);
1936 if ((cmd[2] & MODE_SENSE_PAGE_CONTROL_MASK) !=
1937 MODE_SENSE_PC_CURRENT_VALUES) {
1938 res = nvme_trans_completion(hdr, SAM_STAT_CHECK_CONDITION,
1939 ILLEGAL_REQUEST, SCSI_ASC_INVALID_CDB,
1940 SCSI_ASCQ_CAUSE_NOT_REPORTABLE);
1944 switch (cmd[2] & MODE_SENSE_PAGE_CODE_MASK) {
1945 case MODE_PAGE_CACHING:
1946 res = nvme_trans_mode_page_create(ns, hdr, cmd, alloc_len,
1948 &nvme_trans_fill_caching_page,
1949 MODE_PAGE_CACHING_LEN);
1951 case MODE_PAGE_CONTROL:
1952 res = nvme_trans_mode_page_create(ns, hdr, cmd, alloc_len,
1954 &nvme_trans_fill_control_page,
1955 MODE_PAGE_CONTROL_LEN);
1957 case MODE_PAGE_POWER_CONDITION:
1958 res = nvme_trans_mode_page_create(ns, hdr, cmd, alloc_len,
1960 &nvme_trans_fill_pow_cnd_page,
1961 MODE_PAGE_POW_CND_LEN);
1963 case MODE_PAGE_INFO_EXCEP:
1964 res = nvme_trans_mode_page_create(ns, hdr, cmd, alloc_len,
1966 &nvme_trans_fill_inf_exc_page,
1967 MODE_PAGE_INF_EXC_LEN);
1969 case MODE_PAGE_RETURN_ALL:
1970 res = nvme_trans_mode_page_create(ns, hdr, cmd, alloc_len,
1972 &nvme_trans_fill_all_pages,
1976 res = nvme_trans_completion(hdr, SAM_STAT_CHECK_CONDITION,
1977 ILLEGAL_REQUEST, SCSI_ASC_INVALID_CDB,
1978 SCSI_ASCQ_CAUSE_NOT_REPORTABLE);
1986 static int nvme_trans_read_capacity(struct nvme_ns *ns, struct sg_io_hdr *hdr,
1994 struct nvme_id_ns *id_ns;
1998 alloc_len = get_unaligned_be32(&cmd[10]);
1999 resp_size = READ_CAP_16_RESP_SIZE;
2001 alloc_len = READ_CAP_10_RESP_SIZE;
2002 resp_size = READ_CAP_10_RESP_SIZE;
2005 nvme_sc = nvme_identify_ns(ns->ctrl, ns->ns_id, &id_ns);
2006 res = nvme_trans_status_code(hdr, nvme_sc);
2010 response = kzalloc(resp_size, GFP_KERNEL);
2011 if (response == NULL) {
2015 nvme_trans_fill_read_cap(response, id_ns, cdb16);
2017 xfer_len = min(alloc_len, resp_size);
2018 res = nvme_trans_copy_to_user(hdr, response, xfer_len);
2026 static int nvme_trans_report_luns(struct nvme_ns *ns, struct sg_io_hdr *hdr,
2031 u32 alloc_len, xfer_len, resp_size;
2033 struct nvme_id_ctrl *id_ctrl;
2034 u32 ll_length, lun_id;
2035 u8 lun_id_offset = REPORT_LUNS_FIRST_LUN_OFFSET;
2040 return nvme_trans_completion(hdr, SAM_STAT_CHECK_CONDITION,
2041 ILLEGAL_REQUEST, SCSI_ASC_INVALID_CDB,
2042 SCSI_ASCQ_CAUSE_NOT_REPORTABLE);
2043 case ALL_LUNS_RETURNED:
2044 case ALL_WELL_KNOWN_LUNS_RETURNED:
2045 case RESTRICTED_LUNS_RETURNED:
2046 nvme_sc = nvme_identify_ctrl(ns->ctrl, &id_ctrl);
2047 res = nvme_trans_status_code(hdr, nvme_sc);
2051 ll_length = le32_to_cpu(id_ctrl->nn) * LUN_ENTRY_SIZE;
2052 resp_size = ll_length + LUN_DATA_HEADER_SIZE;
2054 alloc_len = get_unaligned_be32(&cmd[6]);
2055 if (alloc_len < resp_size) {
2056 res = nvme_trans_completion(hdr,
2057 SAM_STAT_CHECK_CONDITION,
2058 ILLEGAL_REQUEST, SCSI_ASC_INVALID_CDB,
2059 SCSI_ASCQ_CAUSE_NOT_REPORTABLE);
2063 response = kzalloc(resp_size, GFP_KERNEL);
2064 if (response == NULL) {
2069 /* The first LUN ID will always be 0 per the SAM spec */
2070 for (lun_id = 0; lun_id < le32_to_cpu(id_ctrl->nn); lun_id++) {
2072 * Set the LUN Id and then increment to the next LUN
2073 * location in the parameter data.
2075 __be64 tmp_id = cpu_to_be64(lun_id);
2076 memcpy(&response[lun_id_offset], &tmp_id, sizeof(u64));
2077 lun_id_offset += LUN_ENTRY_SIZE;
2079 tmp_len = cpu_to_be32(ll_length);
2080 memcpy(response, &tmp_len, sizeof(u32));
2083 xfer_len = min(alloc_len, resp_size);
2084 res = nvme_trans_copy_to_user(hdr, response, xfer_len);
2092 static int nvme_trans_request_sense(struct nvme_ns *ns, struct sg_io_hdr *hdr,
2096 u8 alloc_len, xfer_len, resp_size;
2100 desc_format = cmd[1] & 0x01;
2103 resp_size = ((desc_format) ? (DESC_FMT_SENSE_DATA_SIZE) :
2104 (FIXED_FMT_SENSE_DATA_SIZE));
2105 response = kzalloc(resp_size, GFP_KERNEL);
2106 if (response == NULL) {
2112 /* Descriptor Format Sense Data */
2113 response[0] = DESC_FORMAT_SENSE_DATA;
2114 response[1] = NO_SENSE;
2115 /* TODO How is LOW POWER CONDITION ON handled? (byte 2) */
2116 response[2] = SCSI_ASC_NO_SENSE;
2117 response[3] = SCSI_ASCQ_CAUSE_NOT_REPORTABLE;
2118 /* SDAT_OVFL = 0 | Additional Sense Length = 0 */
2120 /* Fixed Format Sense Data */
2121 response[0] = FIXED_SENSE_DATA;
2122 /* Byte 1 = Obsolete */
2123 response[2] = NO_SENSE; /* FM, EOM, ILI, SDAT_OVFL = 0 */
2124 /* Bytes 3-6 - Information - set to zero */
2125 response[7] = FIXED_SENSE_DATA_ADD_LENGTH;
2126 /* Bytes 8-11 - Cmd Specific Information - set to zero */
2127 response[12] = SCSI_ASC_NO_SENSE;
2128 response[13] = SCSI_ASCQ_CAUSE_NOT_REPORTABLE;
2129 /* Byte 14 = Field Replaceable Unit Code = 0 */
2130 /* Bytes 15-17 - SKSV=0; Sense Key Specific = 0 */
2133 xfer_len = min(alloc_len, resp_size);
2134 res = nvme_trans_copy_to_user(hdr, response, xfer_len);
2141 static int nvme_trans_synchronize_cache(struct nvme_ns *ns,
2142 struct sg_io_hdr *hdr)
2145 struct nvme_command c;
2147 memset(&c, 0, sizeof(c));
2148 c.common.opcode = nvme_cmd_flush;
2149 c.common.nsid = cpu_to_le32(ns->ns_id);
2151 nvme_sc = nvme_submit_sync_cmd(ns->queue, &c, NULL, 0);
2152 return nvme_trans_status_code(hdr, nvme_sc);
2155 static int nvme_trans_format_unit(struct nvme_ns *ns, struct sg_io_hdr *hdr,
2159 u8 parm_hdr_len = 0;
2160 u8 nvme_pf_code = 0;
2161 u8 format_prot_info, long_list, format_data;
2163 format_prot_info = (cmd[1] & 0xc0) >> 6;
2164 long_list = cmd[1] & 0x20;
2165 format_data = cmd[1] & 0x10;
2167 if (format_data != 0) {
2168 if (format_prot_info != 0) {
2170 parm_hdr_len = FORMAT_UNIT_SHORT_PARM_LIST_LEN;
2172 parm_hdr_len = FORMAT_UNIT_LONG_PARM_LIST_LEN;
2174 } else if (format_data == 0 && format_prot_info != 0) {
2175 res = nvme_trans_completion(hdr, SAM_STAT_CHECK_CONDITION,
2176 ILLEGAL_REQUEST, SCSI_ASC_INVALID_CDB,
2177 SCSI_ASCQ_CAUSE_NOT_REPORTABLE);
2181 /* Get parm header from data-in/out buffer */
2183 * According to the translation spec, the only fields in the parameter
2184 * list we are concerned with are in the header. So allocate only that.
2186 if (parm_hdr_len > 0) {
2187 res = nvme_trans_fmt_get_parm_header(hdr, parm_hdr_len,
2188 format_prot_info, &nvme_pf_code);
2193 /* Attempt to activate any previously downloaded firmware image */
2194 res = nvme_trans_send_activate_fw_cmd(ns, hdr, 0);
2196 /* Determine Block size and count and send format command */
2197 res = nvme_trans_fmt_set_blk_size_count(ns, hdr);
2201 res = nvme_trans_fmt_send_cmd(ns, hdr, nvme_pf_code);
2207 static int nvme_trans_test_unit_ready(struct nvme_ns *ns,
2208 struct sg_io_hdr *hdr,
2211 if (nvme_ctrl_ready(ns->ctrl))
2212 return nvme_trans_completion(hdr, SAM_STAT_CHECK_CONDITION,
2213 NOT_READY, SCSI_ASC_LUN_NOT_READY,
2214 SCSI_ASCQ_CAUSE_NOT_REPORTABLE);
2216 return nvme_trans_completion(hdr, SAM_STAT_GOOD, NO_SENSE, 0, 0);
2219 static int nvme_trans_write_buffer(struct nvme_ns *ns, struct sg_io_hdr *hdr,
2223 u32 buffer_offset, parm_list_length;
2226 parm_list_length = get_unaligned_be24(&cmd[6]);
2227 if (parm_list_length % BYTES_TO_DWORDS != 0) {
2228 /* NVMe expects Firmware file to be a whole number of DWORDS */
2229 res = nvme_trans_completion(hdr, SAM_STAT_CHECK_CONDITION,
2230 ILLEGAL_REQUEST, SCSI_ASC_INVALID_CDB,
2231 SCSI_ASCQ_CAUSE_NOT_REPORTABLE);
2235 if (buffer_id > NVME_MAX_FIRMWARE_SLOT) {
2236 res = nvme_trans_completion(hdr, SAM_STAT_CHECK_CONDITION,
2237 ILLEGAL_REQUEST, SCSI_ASC_INVALID_CDB,
2238 SCSI_ASCQ_CAUSE_NOT_REPORTABLE);
2241 mode = cmd[1] & 0x1f;
2242 buffer_offset = get_unaligned_be24(&cmd[3]);
2245 case DOWNLOAD_SAVE_ACTIVATE:
2246 res = nvme_trans_send_download_fw_cmd(ns, hdr, nvme_admin_download_fw,
2247 parm_list_length, buffer_offset,
2251 res = nvme_trans_send_activate_fw_cmd(ns, hdr, buffer_id);
2253 case DOWNLOAD_SAVE_DEFER_ACTIVATE:
2254 res = nvme_trans_send_download_fw_cmd(ns, hdr, nvme_admin_download_fw,
2255 parm_list_length, buffer_offset,
2258 case ACTIVATE_DEFERRED_MICROCODE:
2259 res = nvme_trans_send_activate_fw_cmd(ns, hdr, buffer_id);
2262 res = nvme_trans_completion(hdr, SAM_STAT_CHECK_CONDITION,
2263 ILLEGAL_REQUEST, SCSI_ASC_INVALID_CDB,
2264 SCSI_ASCQ_CAUSE_NOT_REPORTABLE);
2272 struct scsi_unmap_blk_desc {
2278 struct scsi_unmap_parm_list {
2279 __be16 unmap_data_len;
2280 __be16 unmap_blk_desc_data_len;
2282 struct scsi_unmap_blk_desc desc[0];
2285 static int nvme_trans_unmap(struct nvme_ns *ns, struct sg_io_hdr *hdr,
2288 struct scsi_unmap_parm_list *plist;
2289 struct nvme_dsm_range *range;
2290 struct nvme_command c;
2291 int i, nvme_sc, res;
2292 u16 ndesc, list_len;
2294 list_len = get_unaligned_be16(&cmd[7]);
2298 plist = kmalloc(list_len, GFP_KERNEL);
2302 res = nvme_trans_copy_from_user(hdr, plist, list_len);
2306 ndesc = be16_to_cpu(plist->unmap_blk_desc_data_len) >> 4;
2307 if (!ndesc || ndesc > 256) {
2312 range = kcalloc(ndesc, sizeof(*range), GFP_KERNEL);
2318 for (i = 0; i < ndesc; i++) {
2319 range[i].nlb = cpu_to_le32(be32_to_cpu(plist->desc[i].nlb));
2320 range[i].slba = cpu_to_le64(be64_to_cpu(plist->desc[i].slba));
2324 memset(&c, 0, sizeof(c));
2325 c.dsm.opcode = nvme_cmd_dsm;
2326 c.dsm.nsid = cpu_to_le32(ns->ns_id);
2327 c.dsm.nr = cpu_to_le32(ndesc - 1);
2328 c.dsm.attributes = cpu_to_le32(NVME_DSMGMT_AD);
2330 nvme_sc = nvme_submit_sync_cmd(ns->queue, &c, range,
2331 ndesc * sizeof(*range));
2332 res = nvme_trans_status_code(hdr, nvme_sc);
2340 static int nvme_scsi_translate(struct nvme_ns *ns, struct sg_io_hdr *hdr)
2344 unsigned int opcode;
2346 if (hdr->cmdp == NULL)
2348 if (hdr->cmd_len > sizeof(cmd))
2350 if (copy_from_user(cmd, hdr->cmdp, hdr->cmd_len))
2354 * Prime the hdr with good status for scsi commands that don't require
2355 * an nvme command for translation.
2357 retcode = nvme_trans_status_code(hdr, NVME_SC_SUCCESS);
2368 retcode = nvme_trans_io(ns, hdr, 0, cmd);
2374 retcode = nvme_trans_io(ns, hdr, 1, cmd);
2377 retcode = nvme_trans_inquiry(ns, hdr, cmd);
2380 retcode = nvme_trans_log_sense(ns, hdr, cmd);
2383 case MODE_SELECT_10:
2384 retcode = nvme_trans_mode_select(ns, hdr, cmd);
2388 retcode = nvme_trans_mode_sense(ns, hdr, cmd);
2391 retcode = nvme_trans_read_capacity(ns, hdr, cmd, 0);
2393 case SERVICE_ACTION_IN_16:
2395 case SAI_READ_CAPACITY_16:
2396 retcode = nvme_trans_read_capacity(ns, hdr, cmd, 1);
2403 retcode = nvme_trans_report_luns(ns, hdr, cmd);
2406 retcode = nvme_trans_request_sense(ns, hdr, cmd);
2408 case SYNCHRONIZE_CACHE:
2409 retcode = nvme_trans_synchronize_cache(ns, hdr);
2412 retcode = nvme_trans_format_unit(ns, hdr, cmd);
2414 case TEST_UNIT_READY:
2415 retcode = nvme_trans_test_unit_ready(ns, hdr, cmd);
2418 retcode = nvme_trans_write_buffer(ns, hdr, cmd);
2421 retcode = nvme_trans_unmap(ns, hdr, cmd);
2425 retcode = nvme_trans_completion(hdr, SAM_STAT_CHECK_CONDITION,
2426 ILLEGAL_REQUEST, SCSI_ASC_ILLEGAL_COMMAND,
2427 SCSI_ASCQ_CAUSE_NOT_REPORTABLE);
2433 int nvme_sg_io(struct nvme_ns *ns, struct sg_io_hdr __user *u_hdr)
2435 struct sg_io_hdr hdr;
2438 if (!capable(CAP_SYS_ADMIN))
2440 if (copy_from_user(&hdr, u_hdr, sizeof(hdr)))
2442 if (hdr.interface_id != 'S')
2446 * A positive return code means a NVMe status, which has been
2447 * translated to sense data.
2449 retcode = nvme_scsi_translate(ns, &hdr);
2452 if (copy_to_user(u_hdr, &hdr, sizeof(sg_io_hdr_t)) > 0)
2457 int nvme_sg_get_version_num(int __user *ip)
2459 return put_user(sg_version_num, ip);