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/nvme.h>
21 #include <linux/bio.h>
22 #include <linux/bitops.h>
23 #include <linux/blkdev.h>
24 #include <linux/compat.h>
25 #include <linux/delay.h>
26 #include <linux/errno.h>
28 #include <linux/genhd.h>
29 #include <linux/idr.h>
30 #include <linux/init.h>
31 #include <linux/interrupt.h>
33 #include <linux/kdev_t.h>
34 #include <linux/kthread.h>
35 #include <linux/kernel.h>
37 #include <linux/module.h>
38 #include <linux/moduleparam.h>
39 #include <linux/pci.h>
40 #include <linux/poison.h>
41 #include <linux/sched.h>
42 #include <linux/slab.h>
43 #include <linux/types.h>
44 #include <asm/unaligned.h>
46 #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 NVME_POWER_STATE_START_VALID 0x00
76 #define NVME_POWER_STATE_ACTIVE 0x01
77 #define NVME_POWER_STATE_IDLE 0x02
78 #define NVME_POWER_STATE_STANDBY 0x03
79 #define NVME_POWER_STATE_LU_CONTROL 0x07
80 #define POWER_STATE_0 0
81 #define POWER_STATE_1 1
82 #define POWER_STATE_2 2
83 #define POWER_STATE_3 3
84 #define DOWNLOAD_SAVE_ACTIVATE 0x05
85 #define DOWNLOAD_SAVE_DEFER_ACTIVATE 0x0E
86 #define ACTIVATE_DEFERRED_MICROCODE 0x0F
87 #define FORMAT_UNIT_IMMED_MASK 0x2
88 #define FORMAT_UNIT_IMMED_OFFSET 1
89 #define KELVIN_TEMP_FACTOR 273
90 #define FIXED_FMT_SENSE_DATA_SIZE 18
91 #define DESC_FMT_SENSE_DATA_SIZE 8
93 /* SCSI/NVMe defines and bit masks */
94 #define INQ_STANDARD_INQUIRY_PAGE 0x00
95 #define INQ_SUPPORTED_VPD_PAGES_PAGE 0x00
96 #define INQ_UNIT_SERIAL_NUMBER_PAGE 0x80
97 #define INQ_DEVICE_IDENTIFICATION_PAGE 0x83
98 #define INQ_EXTENDED_INQUIRY_DATA_PAGE 0x86
99 #define INQ_BDEV_LIMITS_PAGE 0xB0
100 #define INQ_BDEV_CHARACTERISTICS_PAGE 0xB1
101 #define INQ_SERIAL_NUMBER_LENGTH 0x14
102 #define INQ_NUM_SUPPORTED_VPD_PAGES 6
103 #define VERSION_SPC_4 0x06
104 #define ACA_UNSUPPORTED 0
105 #define STANDARD_INQUIRY_LENGTH 36
106 #define ADDITIONAL_STD_INQ_LENGTH 31
107 #define EXTENDED_INQUIRY_DATA_PAGE_LENGTH 0x3C
108 #define RESERVED_FIELD 0
110 /* Mode Sense/Select defines */
111 #define MODE_PAGE_INFO_EXCEP 0x1C
112 #define MODE_PAGE_CACHING 0x08
113 #define MODE_PAGE_CONTROL 0x0A
114 #define MODE_PAGE_POWER_CONDITION 0x1A
115 #define MODE_PAGE_RETURN_ALL 0x3F
116 #define MODE_PAGE_BLK_DES_LEN 0x08
117 #define MODE_PAGE_LLBAA_BLK_DES_LEN 0x10
118 #define MODE_PAGE_CACHING_LEN 0x14
119 #define MODE_PAGE_CONTROL_LEN 0x0C
120 #define MODE_PAGE_POW_CND_LEN 0x28
121 #define MODE_PAGE_INF_EXC_LEN 0x0C
122 #define MODE_PAGE_ALL_LEN 0x54
123 #define MODE_SENSE6_MPH_SIZE 4
124 #define MODE_SENSE_PAGE_CONTROL_MASK 0xC0
125 #define MODE_SENSE_PAGE_CODE_OFFSET 2
126 #define MODE_SENSE_PAGE_CODE_MASK 0x3F
127 #define MODE_SENSE_LLBAA_MASK 0x10
128 #define MODE_SENSE_LLBAA_SHIFT 4
129 #define MODE_SENSE_DBD_MASK 8
130 #define MODE_SENSE_DBD_SHIFT 3
131 #define MODE_SENSE10_MPH_SIZE 8
132 #define MODE_SELECT_CDB_PAGE_FORMAT_MASK 0x10
133 #define MODE_SELECT_CDB_SAVE_PAGES_MASK 0x1
134 #define MODE_SELECT_6_BD_OFFSET 3
135 #define MODE_SELECT_10_BD_OFFSET 6
136 #define MODE_SELECT_10_LLBAA_OFFSET 4
137 #define MODE_SELECT_10_LLBAA_MASK 1
138 #define MODE_SELECT_6_MPH_SIZE 4
139 #define MODE_SELECT_10_MPH_SIZE 8
140 #define CACHING_MODE_PAGE_WCE_MASK 0x04
141 #define MODE_SENSE_BLK_DESC_ENABLED 0
142 #define MODE_SENSE_BLK_DESC_COUNT 1
143 #define MODE_SELECT_PAGE_CODE_MASK 0x3F
144 #define SHORT_DESC_BLOCK 8
145 #define LONG_DESC_BLOCK 16
146 #define MODE_PAGE_POW_CND_LEN_FIELD 0x26
147 #define MODE_PAGE_INF_EXC_LEN_FIELD 0x0A
148 #define MODE_PAGE_CACHING_LEN_FIELD 0x12
149 #define MODE_PAGE_CONTROL_LEN_FIELD 0x0A
150 #define MODE_SENSE_PC_CURRENT_VALUES 0
152 /* Log Sense defines */
153 #define LOG_PAGE_SUPPORTED_LOG_PAGES_PAGE 0x00
154 #define LOG_PAGE_SUPPORTED_LOG_PAGES_LENGTH 0x07
155 #define LOG_PAGE_INFORMATIONAL_EXCEPTIONS_PAGE 0x2F
156 #define LOG_PAGE_TEMPERATURE_PAGE 0x0D
157 #define LOG_SENSE_CDB_SP_NOT_ENABLED 0
158 #define LOG_SENSE_CDB_PC_MASK 0xC0
159 #define LOG_SENSE_CDB_PC_SHIFT 6
160 #define LOG_SENSE_CDB_PC_CUMULATIVE_VALUES 1
161 #define LOG_SENSE_CDB_PAGE_CODE_MASK 0x3F
162 #define REMAINING_INFO_EXCP_PAGE_LENGTH 0x8
163 #define LOG_INFO_EXCP_PAGE_LENGTH 0xC
164 #define REMAINING_TEMP_PAGE_LENGTH 0xC
165 #define LOG_TEMP_PAGE_LENGTH 0x10
166 #define LOG_TEMP_UNKNOWN 0xFF
167 #define SUPPORTED_LOG_PAGES_PAGE_LENGTH 0x3
169 /* Read Capacity defines */
170 #define READ_CAP_10_RESP_SIZE 8
171 #define READ_CAP_16_RESP_SIZE 32
173 /* NVMe Namespace and Command Defines */
174 #define BYTES_TO_DWORDS 4
175 #define NVME_MAX_FIRMWARE_SLOT 7
177 /* Report LUNs defines */
178 #define REPORT_LUNS_FIRST_LUN_OFFSET 8
180 /* SCSI ADDITIONAL SENSE Codes */
182 #define SCSI_ASC_NO_SENSE 0x00
183 #define SCSI_ASC_PERIPHERAL_DEV_WRITE_FAULT 0x03
184 #define SCSI_ASC_LUN_NOT_READY 0x04
185 #define SCSI_ASC_WARNING 0x0B
186 #define SCSI_ASC_LOG_BLOCK_GUARD_CHECK_FAILED 0x10
187 #define SCSI_ASC_LOG_BLOCK_APPTAG_CHECK_FAILED 0x10
188 #define SCSI_ASC_LOG_BLOCK_REFTAG_CHECK_FAILED 0x10
189 #define SCSI_ASC_UNRECOVERED_READ_ERROR 0x11
190 #define SCSI_ASC_MISCOMPARE_DURING_VERIFY 0x1D
191 #define SCSI_ASC_ACCESS_DENIED_INVALID_LUN_ID 0x20
192 #define SCSI_ASC_ILLEGAL_COMMAND 0x20
193 #define SCSI_ASC_ILLEGAL_BLOCK 0x21
194 #define SCSI_ASC_INVALID_CDB 0x24
195 #define SCSI_ASC_INVALID_LUN 0x25
196 #define SCSI_ASC_INVALID_PARAMETER 0x26
197 #define SCSI_ASC_FORMAT_COMMAND_FAILED 0x31
198 #define SCSI_ASC_INTERNAL_TARGET_FAILURE 0x44
200 /* SCSI ADDITIONAL SENSE Code Qualifiers */
202 #define SCSI_ASCQ_CAUSE_NOT_REPORTABLE 0x00
203 #define SCSI_ASCQ_FORMAT_COMMAND_FAILED 0x01
204 #define SCSI_ASCQ_LOG_BLOCK_GUARD_CHECK_FAILED 0x01
205 #define SCSI_ASCQ_LOG_BLOCK_APPTAG_CHECK_FAILED 0x02
206 #define SCSI_ASCQ_LOG_BLOCK_REFTAG_CHECK_FAILED 0x03
207 #define SCSI_ASCQ_FORMAT_IN_PROGRESS 0x04
208 #define SCSI_ASCQ_POWER_LOSS_EXPECTED 0x08
209 #define SCSI_ASCQ_INVALID_LUN_ID 0x09
211 /* copied from drivers/usb/gadget/function/storage_common.h */
212 static inline u32 get_unaligned_be24(u8 *buf)
214 return 0xffffff & (u32) get_unaligned_be32(buf - 1);
217 /* Struct to gather data that needs to be extracted from a SCSI CDB.
218 Not conforming to any particular CDB variant, but compatible with all. */
220 struct nvme_trans_io_cdb {
228 /* Internal Helper Functions */
231 /* Copy data to userspace memory */
233 static int nvme_trans_copy_to_user(struct sg_io_hdr *hdr, void *from,
238 size_t remaining = n;
241 if (hdr->iovec_count > 0) {
244 for (i = 0; i < hdr->iovec_count; i++) {
245 if (copy_from_user(&sgl, hdr->dxferp +
246 i * sizeof(struct sg_iovec),
247 sizeof(struct sg_iovec)))
249 xfer_len = min(remaining, sgl.iov_len);
250 if (copy_to_user(sgl.iov_base, index, xfer_len))
254 remaining -= xfer_len;
261 if (copy_to_user(hdr->dxferp, from, n))
266 /* Copy data from userspace memory */
268 static int nvme_trans_copy_from_user(struct sg_io_hdr *hdr, void *to,
273 size_t remaining = n;
276 if (hdr->iovec_count > 0) {
279 for (i = 0; i < hdr->iovec_count; i++) {
280 if (copy_from_user(&sgl, hdr->dxferp +
281 i * sizeof(struct sg_iovec),
282 sizeof(struct sg_iovec)))
284 xfer_len = min(remaining, sgl.iov_len);
285 if (copy_from_user(index, sgl.iov_base, xfer_len))
288 remaining -= xfer_len;
295 if (copy_from_user(to, hdr->dxferp, n))
300 /* Status/Sense Buffer Writeback */
302 static int nvme_trans_completion(struct sg_io_hdr *hdr, u8 status, u8 sense_key,
306 u8 resp[DESC_FMT_SENSE_DATA_SIZE];
308 if (scsi_status_is_good(status)) {
309 hdr->status = SAM_STAT_GOOD;
310 hdr->masked_status = GOOD;
311 hdr->host_status = DID_OK;
312 hdr->driver_status = DRIVER_OK;
315 hdr->status = status;
316 hdr->masked_status = status >> 1;
317 hdr->host_status = DID_OK;
318 hdr->driver_status = DRIVER_OK;
320 memset(resp, 0, DESC_FMT_SENSE_DATA_SIZE);
321 resp[0] = DESC_FORMAT_SENSE_DATA;
326 xfer_len = min_t(u8, hdr->mx_sb_len, DESC_FMT_SENSE_DATA_SIZE);
327 hdr->sb_len_wr = xfer_len;
328 if (copy_to_user(hdr->sbp, resp, xfer_len) > 0)
336 * Take a status code from a lowlevel routine, and if it was a positive NVMe
337 * error code update the sense data based on it. In either case the passed
338 * in value is returned again, unless an -EFAULT from copy_to_user overrides
341 static int nvme_trans_status_code(struct sg_io_hdr *hdr, int nvme_sc)
343 u8 status, sense_key, asc, ascq;
346 /* For non-nvme (Linux) errors, simply return the error code */
350 /* Mask DNR, More, and reserved fields */
351 switch (nvme_sc & 0x7FF) {
352 /* Generic Command Status */
353 case NVME_SC_SUCCESS:
354 status = SAM_STAT_GOOD;
355 sense_key = NO_SENSE;
356 asc = SCSI_ASC_NO_SENSE;
357 ascq = SCSI_ASCQ_CAUSE_NOT_REPORTABLE;
359 case NVME_SC_INVALID_OPCODE:
360 status = SAM_STAT_CHECK_CONDITION;
361 sense_key = ILLEGAL_REQUEST;
362 asc = SCSI_ASC_ILLEGAL_COMMAND;
363 ascq = SCSI_ASCQ_CAUSE_NOT_REPORTABLE;
365 case NVME_SC_INVALID_FIELD:
366 status = SAM_STAT_CHECK_CONDITION;
367 sense_key = ILLEGAL_REQUEST;
368 asc = SCSI_ASC_INVALID_CDB;
369 ascq = SCSI_ASCQ_CAUSE_NOT_REPORTABLE;
371 case NVME_SC_DATA_XFER_ERROR:
372 status = SAM_STAT_CHECK_CONDITION;
373 sense_key = MEDIUM_ERROR;
374 asc = SCSI_ASC_NO_SENSE;
375 ascq = SCSI_ASCQ_CAUSE_NOT_REPORTABLE;
377 case NVME_SC_POWER_LOSS:
378 status = SAM_STAT_TASK_ABORTED;
379 sense_key = ABORTED_COMMAND;
380 asc = SCSI_ASC_WARNING;
381 ascq = SCSI_ASCQ_POWER_LOSS_EXPECTED;
383 case NVME_SC_INTERNAL:
384 status = SAM_STAT_CHECK_CONDITION;
385 sense_key = HARDWARE_ERROR;
386 asc = SCSI_ASC_INTERNAL_TARGET_FAILURE;
387 ascq = SCSI_ASCQ_CAUSE_NOT_REPORTABLE;
389 case NVME_SC_ABORT_REQ:
390 status = SAM_STAT_TASK_ABORTED;
391 sense_key = ABORTED_COMMAND;
392 asc = SCSI_ASC_NO_SENSE;
393 ascq = SCSI_ASCQ_CAUSE_NOT_REPORTABLE;
395 case NVME_SC_ABORT_QUEUE:
396 status = SAM_STAT_TASK_ABORTED;
397 sense_key = ABORTED_COMMAND;
398 asc = SCSI_ASC_NO_SENSE;
399 ascq = SCSI_ASCQ_CAUSE_NOT_REPORTABLE;
401 case NVME_SC_FUSED_FAIL:
402 status = SAM_STAT_TASK_ABORTED;
403 sense_key = ABORTED_COMMAND;
404 asc = SCSI_ASC_NO_SENSE;
405 ascq = SCSI_ASCQ_CAUSE_NOT_REPORTABLE;
407 case NVME_SC_FUSED_MISSING:
408 status = SAM_STAT_TASK_ABORTED;
409 sense_key = ABORTED_COMMAND;
410 asc = SCSI_ASC_NO_SENSE;
411 ascq = SCSI_ASCQ_CAUSE_NOT_REPORTABLE;
413 case NVME_SC_INVALID_NS:
414 status = SAM_STAT_CHECK_CONDITION;
415 sense_key = ILLEGAL_REQUEST;
416 asc = SCSI_ASC_ACCESS_DENIED_INVALID_LUN_ID;
417 ascq = SCSI_ASCQ_INVALID_LUN_ID;
419 case NVME_SC_LBA_RANGE:
420 status = SAM_STAT_CHECK_CONDITION;
421 sense_key = ILLEGAL_REQUEST;
422 asc = SCSI_ASC_ILLEGAL_BLOCK;
423 ascq = SCSI_ASCQ_CAUSE_NOT_REPORTABLE;
425 case NVME_SC_CAP_EXCEEDED:
426 status = SAM_STAT_CHECK_CONDITION;
427 sense_key = MEDIUM_ERROR;
428 asc = SCSI_ASC_NO_SENSE;
429 ascq = SCSI_ASCQ_CAUSE_NOT_REPORTABLE;
431 case NVME_SC_NS_NOT_READY:
432 status = SAM_STAT_CHECK_CONDITION;
433 sense_key = NOT_READY;
434 asc = SCSI_ASC_LUN_NOT_READY;
435 ascq = SCSI_ASCQ_CAUSE_NOT_REPORTABLE;
438 /* Command Specific Status */
439 case NVME_SC_INVALID_FORMAT:
440 status = SAM_STAT_CHECK_CONDITION;
441 sense_key = ILLEGAL_REQUEST;
442 asc = SCSI_ASC_FORMAT_COMMAND_FAILED;
443 ascq = SCSI_ASCQ_FORMAT_COMMAND_FAILED;
445 case NVME_SC_BAD_ATTRIBUTES:
446 status = SAM_STAT_CHECK_CONDITION;
447 sense_key = ILLEGAL_REQUEST;
448 asc = SCSI_ASC_INVALID_CDB;
449 ascq = SCSI_ASCQ_CAUSE_NOT_REPORTABLE;
453 case NVME_SC_WRITE_FAULT:
454 status = SAM_STAT_CHECK_CONDITION;
455 sense_key = MEDIUM_ERROR;
456 asc = SCSI_ASC_PERIPHERAL_DEV_WRITE_FAULT;
457 ascq = SCSI_ASCQ_CAUSE_NOT_REPORTABLE;
459 case NVME_SC_READ_ERROR:
460 status = SAM_STAT_CHECK_CONDITION;
461 sense_key = MEDIUM_ERROR;
462 asc = SCSI_ASC_UNRECOVERED_READ_ERROR;
463 ascq = SCSI_ASCQ_CAUSE_NOT_REPORTABLE;
465 case NVME_SC_GUARD_CHECK:
466 status = SAM_STAT_CHECK_CONDITION;
467 sense_key = MEDIUM_ERROR;
468 asc = SCSI_ASC_LOG_BLOCK_GUARD_CHECK_FAILED;
469 ascq = SCSI_ASCQ_LOG_BLOCK_GUARD_CHECK_FAILED;
471 case NVME_SC_APPTAG_CHECK:
472 status = SAM_STAT_CHECK_CONDITION;
473 sense_key = MEDIUM_ERROR;
474 asc = SCSI_ASC_LOG_BLOCK_APPTAG_CHECK_FAILED;
475 ascq = SCSI_ASCQ_LOG_BLOCK_APPTAG_CHECK_FAILED;
477 case NVME_SC_REFTAG_CHECK:
478 status = SAM_STAT_CHECK_CONDITION;
479 sense_key = MEDIUM_ERROR;
480 asc = SCSI_ASC_LOG_BLOCK_REFTAG_CHECK_FAILED;
481 ascq = SCSI_ASCQ_LOG_BLOCK_REFTAG_CHECK_FAILED;
483 case NVME_SC_COMPARE_FAILED:
484 status = SAM_STAT_CHECK_CONDITION;
485 sense_key = MISCOMPARE;
486 asc = SCSI_ASC_MISCOMPARE_DURING_VERIFY;
487 ascq = SCSI_ASCQ_CAUSE_NOT_REPORTABLE;
489 case NVME_SC_ACCESS_DENIED:
490 status = SAM_STAT_CHECK_CONDITION;
491 sense_key = ILLEGAL_REQUEST;
492 asc = SCSI_ASC_ACCESS_DENIED_INVALID_LUN_ID;
493 ascq = SCSI_ASCQ_INVALID_LUN_ID;
496 /* Unspecified/Default */
497 case NVME_SC_CMDID_CONFLICT:
498 case NVME_SC_CMD_SEQ_ERROR:
499 case NVME_SC_CQ_INVALID:
500 case NVME_SC_QID_INVALID:
501 case NVME_SC_QUEUE_SIZE:
502 case NVME_SC_ABORT_LIMIT:
503 case NVME_SC_ABORT_MISSING:
504 case NVME_SC_ASYNC_LIMIT:
505 case NVME_SC_FIRMWARE_SLOT:
506 case NVME_SC_FIRMWARE_IMAGE:
507 case NVME_SC_INVALID_VECTOR:
508 case NVME_SC_INVALID_LOG_PAGE:
510 status = SAM_STAT_CHECK_CONDITION;
511 sense_key = ILLEGAL_REQUEST;
512 asc = SCSI_ASC_NO_SENSE;
513 ascq = SCSI_ASCQ_CAUSE_NOT_REPORTABLE;
517 res = nvme_trans_completion(hdr, status, sense_key, asc, ascq);
518 return res ? res : nvme_sc;
521 /* INQUIRY Helper Functions */
523 static int nvme_trans_standard_inquiry_page(struct nvme_ns *ns,
524 struct sg_io_hdr *hdr, u8 *inq_response,
527 struct nvme_dev *dev = ns->dev;
530 struct nvme_id_ns *id_ns;
534 u8 resp_data_format = 0x02;
536 u8 cmdque = 0x01 << 1;
537 u8 fw_offset = sizeof(dev->firmware_rev);
539 mem = dma_alloc_coherent(dev->dev, sizeof(struct nvme_id_ns),
540 &dma_addr, GFP_KERNEL);
546 /* nvme ns identify - use DPS value for PROTECT field */
547 nvme_sc = nvme_identify(dev, ns->ns_id, 0, dma_addr);
548 res = nvme_trans_status_code(hdr, nvme_sc);
553 (id_ns->dps) ? (protect = 0x01) : (protect = 0);
555 memset(inq_response, 0, STANDARD_INQUIRY_LENGTH);
556 inq_response[2] = VERSION_SPC_4;
557 inq_response[3] = resp_data_format; /*normaca=0 | hisup=0 */
558 inq_response[4] = ADDITIONAL_STD_INQ_LENGTH;
559 inq_response[5] = protect; /* sccs=0 | acc=0 | tpgs=0 | pc3=0 */
560 inq_response[7] = cmdque; /* wbus16=0 | sync=0 | vs=0 */
561 strncpy(&inq_response[8], "NVMe ", 8);
562 strncpy(&inq_response[16], dev->model, 16);
564 while (dev->firmware_rev[fw_offset - 1] == ' ' && fw_offset > 4)
567 strncpy(&inq_response[32], dev->firmware_rev + fw_offset, 4);
569 xfer_len = min(alloc_len, STANDARD_INQUIRY_LENGTH);
570 res = nvme_trans_copy_to_user(hdr, inq_response, xfer_len);
573 dma_free_coherent(dev->dev, sizeof(struct nvme_id_ns), mem, dma_addr);
578 static int nvme_trans_supported_vpd_pages(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_SUPPORTED_VPD_PAGES_PAGE; /* Page Code */
586 inq_response[3] = INQ_NUM_SUPPORTED_VPD_PAGES; /* Page Length */
587 inq_response[4] = INQ_SUPPORTED_VPD_PAGES_PAGE;
588 inq_response[5] = INQ_UNIT_SERIAL_NUMBER_PAGE;
589 inq_response[6] = INQ_DEVICE_IDENTIFICATION_PAGE;
590 inq_response[7] = INQ_EXTENDED_INQUIRY_DATA_PAGE;
591 inq_response[8] = INQ_BDEV_CHARACTERISTICS_PAGE;
592 inq_response[9] = INQ_BDEV_LIMITS_PAGE;
594 xfer_len = min(alloc_len, STANDARD_INQUIRY_LENGTH);
595 return nvme_trans_copy_to_user(hdr, inq_response, xfer_len);
598 static int nvme_trans_unit_serial_page(struct nvme_ns *ns,
599 struct sg_io_hdr *hdr, u8 *inq_response,
602 struct nvme_dev *dev = ns->dev;
605 memset(inq_response, 0, STANDARD_INQUIRY_LENGTH);
606 inq_response[1] = INQ_UNIT_SERIAL_NUMBER_PAGE; /* Page Code */
607 inq_response[3] = INQ_SERIAL_NUMBER_LENGTH; /* Page Length */
608 strncpy(&inq_response[4], dev->serial, INQ_SERIAL_NUMBER_LENGTH);
610 xfer_len = min(alloc_len, STANDARD_INQUIRY_LENGTH);
611 return nvme_trans_copy_to_user(hdr, inq_response, xfer_len);
614 static int nvme_trans_device_id_page(struct nvme_ns *ns, struct sg_io_hdr *hdr,
615 u8 *inq_response, int alloc_len)
617 struct nvme_dev *dev = ns->dev;
623 __be32 tmp_id = cpu_to_be32(ns->ns_id);
625 mem = dma_alloc_coherent(dev->dev, sizeof(struct nvme_id_ns),
626 &dma_addr, GFP_KERNEL);
632 memset(inq_response, 0, alloc_len);
633 inq_response[1] = INQ_DEVICE_IDENTIFICATION_PAGE; /* Page Code */
634 if (readl(&dev->bar->vs) >= NVME_VS(1, 1)) {
635 struct nvme_id_ns *id_ns = mem;
636 void *eui = id_ns->eui64;
637 int len = sizeof(id_ns->eui64);
639 nvme_sc = nvme_identify(dev, ns->ns_id, 0, dma_addr);
640 res = nvme_trans_status_code(hdr, nvme_sc);
644 if (readl(&dev->bar->vs) >= NVME_VS(1, 2)) {
645 if (bitmap_empty(eui, len * 8)) {
647 len = sizeof(id_ns->nguid);
650 if (bitmap_empty(eui, len * 8))
653 inq_response[3] = 4 + len; /* Page Length */
654 /* Designation Descriptor start */
655 inq_response[4] = 0x01; /* Proto ID=0h | Code set=1h */
656 inq_response[5] = 0x02; /* PIV=0b | Asso=00b | Designator Type=2h */
657 inq_response[6] = 0x00; /* Rsvd */
658 inq_response[7] = len; /* Designator Length */
659 memcpy(&inq_response[8], eui, len);
662 if (alloc_len < 72) {
663 res = nvme_trans_completion(hdr,
664 SAM_STAT_CHECK_CONDITION,
665 ILLEGAL_REQUEST, SCSI_ASC_INVALID_CDB,
666 SCSI_ASCQ_CAUSE_NOT_REPORTABLE);
669 inq_response[3] = 0x48; /* Page Length */
670 /* Designation Descriptor start */
671 inq_response[4] = 0x03; /* Proto ID=0h | Code set=3h */
672 inq_response[5] = 0x08; /* PIV=0b | Asso=00b | Designator Type=8h */
673 inq_response[6] = 0x00; /* Rsvd */
674 inq_response[7] = 0x44; /* Designator Length */
676 sprintf(&inq_response[8], "%04x", to_pci_dev(dev->dev)->vendor);
677 memcpy(&inq_response[12], dev->model, sizeof(dev->model));
678 sprintf(&inq_response[52], "%04x", tmp_id);
679 memcpy(&inq_response[56], dev->serial, sizeof(dev->serial));
681 xfer_len = alloc_len;
682 res = nvme_trans_copy_to_user(hdr, inq_response, xfer_len);
685 dma_free_coherent(dev->dev, sizeof(struct nvme_id_ns), mem, dma_addr);
690 static int nvme_trans_ext_inq_page(struct nvme_ns *ns, struct sg_io_hdr *hdr,
696 struct nvme_dev *dev = ns->dev;
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) {
716 mem = dma_alloc_coherent(dev->dev, sizeof(struct nvme_id_ns),
717 &dma_addr, GFP_KERNEL);
723 /* nvme ns identify */
724 nvme_sc = nvme_identify(dev, ns->ns_id, 0, dma_addr);
725 res = nvme_trans_status_code(hdr, nvme_sc);
730 spt = spt_lut[(id_ns->dpc) & 0x07] << 3;
731 (id_ns->dps) ? (protect = 0x01) : (protect = 0);
732 grd_chk = protect << 2;
733 app_chk = protect << 1;
736 /* nvme controller identify */
737 nvme_sc = nvme_identify(dev, 0, 1, dma_addr);
738 res = nvme_trans_status_code(hdr, nvme_sc);
743 v_sup = id_ctrl->vwc;
745 memset(inq_response, 0, EXTENDED_INQUIRY_DATA_PAGE_LENGTH);
746 inq_response[1] = INQ_EXTENDED_INQUIRY_DATA_PAGE; /* Page Code */
747 inq_response[2] = 0x00; /* Page Length MSB */
748 inq_response[3] = 0x3C; /* Page Length LSB */
749 inq_response[4] = microcode | spt | grd_chk | app_chk | ref_chk;
750 inq_response[5] = uask_sup;
751 inq_response[6] = v_sup;
752 inq_response[7] = luiclr;
756 xfer_len = min(alloc_len, EXTENDED_INQUIRY_DATA_PAGE_LENGTH);
757 res = nvme_trans_copy_to_user(hdr, inq_response, xfer_len);
760 dma_free_coherent(dev->dev, sizeof(struct nvme_id_ns), mem, dma_addr);
767 static int nvme_trans_bdev_limits_page(struct nvme_ns *ns, struct sg_io_hdr *hdr,
768 u8 *inq_response, int alloc_len)
770 __be32 max_sectors = cpu_to_be32(queue_max_hw_sectors(ns->queue));
771 __be32 max_discard = cpu_to_be32(ns->queue->limits.max_discard_sectors);
772 __be32 discard_desc_count = cpu_to_be32(0x100);
774 memset(inq_response, 0, STANDARD_INQUIRY_LENGTH);
775 inq_response[1] = VPD_BLOCK_LIMITS;
776 inq_response[3] = 0x3c; /* Page Length */
777 memcpy(&inq_response[8], &max_sectors, sizeof(u32));
778 memcpy(&inq_response[20], &max_discard, sizeof(u32));
781 memcpy(&inq_response[24], &discard_desc_count, sizeof(u32));
783 return nvme_trans_copy_to_user(hdr, inq_response, 0x3c);
786 static int nvme_trans_bdev_char_page(struct nvme_ns *ns, struct sg_io_hdr *hdr,
793 inq_response = kzalloc(EXTENDED_INQUIRY_DATA_PAGE_LENGTH, GFP_KERNEL);
794 if (inq_response == NULL) {
799 inq_response[1] = INQ_BDEV_CHARACTERISTICS_PAGE; /* Page Code */
800 inq_response[2] = 0x00; /* Page Length MSB */
801 inq_response[3] = 0x3C; /* Page Length LSB */
802 inq_response[4] = 0x00; /* Medium Rotation Rate MSB */
803 inq_response[5] = 0x01; /* Medium Rotation Rate LSB */
804 inq_response[6] = 0x00; /* Form Factor */
806 xfer_len = min(alloc_len, EXTENDED_INQUIRY_DATA_PAGE_LENGTH);
807 res = nvme_trans_copy_to_user(hdr, inq_response, xfer_len);
814 /* LOG SENSE Helper Functions */
816 static int nvme_trans_log_supp_pages(struct nvme_ns *ns, struct sg_io_hdr *hdr,
823 log_response = kzalloc(LOG_PAGE_SUPPORTED_LOG_PAGES_LENGTH, GFP_KERNEL);
824 if (log_response == NULL) {
829 log_response[0] = LOG_PAGE_SUPPORTED_LOG_PAGES_PAGE;
830 /* Subpage=0x00, Page Length MSB=0 */
831 log_response[3] = SUPPORTED_LOG_PAGES_PAGE_LENGTH;
832 log_response[4] = LOG_PAGE_SUPPORTED_LOG_PAGES_PAGE;
833 log_response[5] = LOG_PAGE_INFORMATIONAL_EXCEPTIONS_PAGE;
834 log_response[6] = LOG_PAGE_TEMPERATURE_PAGE;
836 xfer_len = min(alloc_len, LOG_PAGE_SUPPORTED_LOG_PAGES_LENGTH);
837 res = nvme_trans_copy_to_user(hdr, log_response, xfer_len);
844 static int nvme_trans_log_info_exceptions(struct nvme_ns *ns,
845 struct sg_io_hdr *hdr, int alloc_len)
850 struct nvme_command c;
851 struct nvme_dev *dev = ns->dev;
852 struct nvme_smart_log *smart_log;
858 log_response = kzalloc(LOG_INFO_EXCP_PAGE_LENGTH, GFP_KERNEL);
859 if (log_response == NULL) {
864 mem = dma_alloc_coherent(dev->dev, sizeof(struct nvme_smart_log),
865 &dma_addr, GFP_KERNEL);
871 /* Get SMART Log Page */
872 memset(&c, 0, sizeof(c));
873 c.common.opcode = nvme_admin_get_log_page;
874 c.common.nsid = cpu_to_le32(0xFFFFFFFF);
875 c.common.prp1 = cpu_to_le64(dma_addr);
876 c.common.cdw10[0] = cpu_to_le32((((sizeof(struct nvme_smart_log) /
877 BYTES_TO_DWORDS) - 1) << 16) | NVME_LOG_SMART);
878 res = nvme_submit_sync_cmd(dev->admin_q, &c);
879 if (res != NVME_SC_SUCCESS) {
880 temp_c = LOG_TEMP_UNKNOWN;
883 temp_k = (smart_log->temperature[1] << 8) +
884 (smart_log->temperature[0]);
885 temp_c = temp_k - KELVIN_TEMP_FACTOR;
888 log_response[0] = LOG_PAGE_INFORMATIONAL_EXCEPTIONS_PAGE;
889 /* Subpage=0x00, Page Length MSB=0 */
890 log_response[3] = REMAINING_INFO_EXCP_PAGE_LENGTH;
891 /* Informational Exceptions Log Parameter 1 Start */
892 /* Parameter Code=0x0000 bytes 4,5 */
893 log_response[6] = 0x23; /* DU=0, TSD=1, ETC=0, TMC=0, FMT_AND_LNK=11b */
894 log_response[7] = 0x04; /* PARAMETER LENGTH */
895 /* Add sense Code and qualifier = 0x00 each */
896 /* Use Temperature from NVMe Get Log Page, convert to C from K */
897 log_response[10] = temp_c;
899 xfer_len = min(alloc_len, LOG_INFO_EXCP_PAGE_LENGTH);
900 res = nvme_trans_copy_to_user(hdr, log_response, xfer_len);
902 dma_free_coherent(dev->dev, sizeof(struct nvme_smart_log),
910 static int nvme_trans_log_temperature(struct nvme_ns *ns, struct sg_io_hdr *hdr,
916 struct nvme_command c;
917 struct nvme_dev *dev = ns->dev;
918 struct nvme_smart_log *smart_log;
922 u8 temp_c_cur, temp_c_thresh;
925 log_response = kzalloc(LOG_TEMP_PAGE_LENGTH, GFP_KERNEL);
926 if (log_response == NULL) {
931 mem = dma_alloc_coherent(dev->dev, sizeof(struct nvme_smart_log),
932 &dma_addr, GFP_KERNEL);
938 /* Get SMART Log Page */
939 memset(&c, 0, sizeof(c));
940 c.common.opcode = nvme_admin_get_log_page;
941 c.common.nsid = cpu_to_le32(0xFFFFFFFF);
942 c.common.prp1 = cpu_to_le64(dma_addr);
943 c.common.cdw10[0] = cpu_to_le32((((sizeof(struct nvme_smart_log) /
944 BYTES_TO_DWORDS) - 1) << 16) | NVME_LOG_SMART);
945 res = nvme_submit_sync_cmd(dev->admin_q, &c);
946 if (res != NVME_SC_SUCCESS) {
947 temp_c_cur = LOG_TEMP_UNKNOWN;
950 temp_k = (smart_log->temperature[1] << 8) +
951 (smart_log->temperature[0]);
952 temp_c_cur = temp_k - KELVIN_TEMP_FACTOR;
955 /* Get Features for Temp Threshold */
956 res = nvme_get_features(dev, NVME_FEAT_TEMP_THRESH, 0, 0,
958 if (res != NVME_SC_SUCCESS)
959 temp_c_thresh = LOG_TEMP_UNKNOWN;
961 temp_c_thresh = (feature_resp & 0xFFFF) - KELVIN_TEMP_FACTOR;
963 log_response[0] = LOG_PAGE_TEMPERATURE_PAGE;
964 /* Subpage=0x00, Page Length MSB=0 */
965 log_response[3] = REMAINING_TEMP_PAGE_LENGTH;
966 /* Temperature Log Parameter 1 (Temperature) Start */
967 /* Parameter Code = 0x0000 */
968 log_response[6] = 0x01; /* Format and Linking = 01b */
969 log_response[7] = 0x02; /* Parameter Length */
970 /* Use Temperature from NVMe Get Log Page, convert to C from K */
971 log_response[9] = temp_c_cur;
972 /* Temperature Log Parameter 2 (Reference Temperature) Start */
973 log_response[11] = 0x01; /* Parameter Code = 0x0001 */
974 log_response[12] = 0x01; /* Format and Linking = 01b */
975 log_response[13] = 0x02; /* Parameter Length */
976 /* Use Temperature Thresh from NVMe Get Log Page, convert to C from K */
977 log_response[15] = temp_c_thresh;
979 xfer_len = min(alloc_len, LOG_TEMP_PAGE_LENGTH);
980 res = nvme_trans_copy_to_user(hdr, log_response, xfer_len);
982 dma_free_coherent(dev->dev, sizeof(struct nvme_smart_log),
990 /* MODE SENSE Helper Functions */
992 static int nvme_trans_fill_mode_parm_hdr(u8 *resp, int len, u8 cdb10, u8 llbaa,
993 u16 mode_data_length, u16 blk_desc_len)
995 /* Quick check to make sure I don't stomp on my own memory... */
996 if ((cdb10 && len < 8) || (!cdb10 && len < 4))
1000 resp[0] = (mode_data_length & 0xFF00) >> 8;
1001 resp[1] = (mode_data_length & 0x00FF);
1002 /* resp[2] and [3] are zero */
1004 resp[5] = RESERVED_FIELD;
1005 resp[6] = (blk_desc_len & 0xFF00) >> 8;
1006 resp[7] = (blk_desc_len & 0x00FF);
1008 resp[0] = (mode_data_length & 0x00FF);
1009 /* resp[1] and [2] are zero */
1010 resp[3] = (blk_desc_len & 0x00FF);
1016 static int nvme_trans_fill_blk_desc(struct nvme_ns *ns, struct sg_io_hdr *hdr,
1017 u8 *resp, int len, u8 llbaa)
1021 struct nvme_dev *dev = ns->dev;
1022 dma_addr_t dma_addr;
1024 struct nvme_id_ns *id_ns;
1028 if (llbaa == 0 && len < MODE_PAGE_BLK_DES_LEN)
1030 else if (llbaa > 0 && len < MODE_PAGE_LLBAA_BLK_DES_LEN)
1033 mem = dma_alloc_coherent(dev->dev, sizeof(struct nvme_id_ns),
1034 &dma_addr, GFP_KERNEL);
1040 /* nvme ns identify */
1041 nvme_sc = nvme_identify(dev, ns->ns_id, 0, dma_addr);
1042 res = nvme_trans_status_code(hdr, nvme_sc);
1047 flbas = (id_ns->flbas) & 0x0F;
1048 lba_length = (1 << (id_ns->lbaf[flbas].ds));
1051 __be32 tmp_cap = cpu_to_be32(le64_to_cpu(id_ns->ncap));
1052 /* Byte 4 is reserved */
1053 __be32 tmp_len = cpu_to_be32(lba_length & 0x00FFFFFF);
1055 memcpy(resp, &tmp_cap, sizeof(u32));
1056 memcpy(&resp[4], &tmp_len, sizeof(u32));
1058 __be64 tmp_cap = cpu_to_be64(le64_to_cpu(id_ns->ncap));
1059 __be32 tmp_len = cpu_to_be32(lba_length);
1061 memcpy(resp, &tmp_cap, sizeof(u64));
1062 /* Bytes 8, 9, 10, 11 are reserved */
1063 memcpy(&resp[12], &tmp_len, sizeof(u32));
1067 dma_free_coherent(dev->dev, sizeof(struct nvme_id_ns), mem, dma_addr);
1072 static int nvme_trans_fill_control_page(struct nvme_ns *ns,
1073 struct sg_io_hdr *hdr, u8 *resp,
1076 if (len < MODE_PAGE_CONTROL_LEN)
1079 resp[0] = MODE_PAGE_CONTROL;
1080 resp[1] = MODE_PAGE_CONTROL_LEN_FIELD;
1081 resp[2] = 0x0E; /* TST=000b, TMF_ONLY=0, DPICZ=1,
1082 * D_SENSE=1, GLTSD=1, RLEC=0 */
1083 resp[3] = 0x12; /* Q_ALGO_MODIFIER=1h, NUAR=0, QERR=01b */
1084 /* Byte 4: VS=0, RAC=0, UA_INT=0, SWP=0 */
1085 resp[5] = 0x40; /* ATO=0, TAS=1, ATMPE=0, RWWP=0, AUTOLOAD=0 */
1086 /* resp[6] and [7] are obsolete, thus zero */
1087 resp[8] = 0xFF; /* Busy timeout period = 0xffff */
1089 /* Bytes 10,11: Extended selftest completion time = 0x0000 */
1094 static int nvme_trans_fill_caching_page(struct nvme_ns *ns,
1095 struct sg_io_hdr *hdr,
1100 struct nvme_dev *dev = ns->dev;
1104 if (len < MODE_PAGE_CACHING_LEN)
1107 nvme_sc = nvme_get_features(dev, NVME_FEAT_VOLATILE_WC, 0, 0,
1109 res = nvme_trans_status_code(hdr, nvme_sc);
1113 vwc = feature_resp & 0x00000001;
1115 resp[0] = MODE_PAGE_CACHING;
1116 resp[1] = MODE_PAGE_CACHING_LEN_FIELD;
1121 static int nvme_trans_fill_pow_cnd_page(struct nvme_ns *ns,
1122 struct sg_io_hdr *hdr, u8 *resp,
1125 if (len < MODE_PAGE_POW_CND_LEN)
1128 resp[0] = MODE_PAGE_POWER_CONDITION;
1129 resp[1] = MODE_PAGE_POW_CND_LEN_FIELD;
1130 /* All other bytes are zero */
1135 static int nvme_trans_fill_inf_exc_page(struct nvme_ns *ns,
1136 struct sg_io_hdr *hdr, u8 *resp,
1139 if (len < MODE_PAGE_INF_EXC_LEN)
1142 resp[0] = MODE_PAGE_INFO_EXCEP;
1143 resp[1] = MODE_PAGE_INF_EXC_LEN_FIELD;
1145 /* All other bytes are zero */
1150 static int nvme_trans_fill_all_pages(struct nvme_ns *ns, struct sg_io_hdr *hdr,
1154 u16 mode_pages_offset_1 = 0;
1155 u16 mode_pages_offset_2, mode_pages_offset_3, mode_pages_offset_4;
1157 mode_pages_offset_2 = mode_pages_offset_1 + MODE_PAGE_CACHING_LEN;
1158 mode_pages_offset_3 = mode_pages_offset_2 + MODE_PAGE_CONTROL_LEN;
1159 mode_pages_offset_4 = mode_pages_offset_3 + MODE_PAGE_POW_CND_LEN;
1161 res = nvme_trans_fill_caching_page(ns, hdr, &resp[mode_pages_offset_1],
1162 MODE_PAGE_CACHING_LEN);
1165 res = nvme_trans_fill_control_page(ns, hdr, &resp[mode_pages_offset_2],
1166 MODE_PAGE_CONTROL_LEN);
1169 res = nvme_trans_fill_pow_cnd_page(ns, hdr, &resp[mode_pages_offset_3],
1170 MODE_PAGE_POW_CND_LEN);
1173 return nvme_trans_fill_inf_exc_page(ns, hdr, &resp[mode_pages_offset_4],
1174 MODE_PAGE_INF_EXC_LEN);
1177 static inline int nvme_trans_get_blk_desc_len(u8 dbd, u8 llbaa)
1179 if (dbd == MODE_SENSE_BLK_DESC_ENABLED) {
1180 /* SPC-4: len = 8 x Num_of_descriptors if llbaa = 0, 16x if 1 */
1181 return 8 * (llbaa + 1) * MODE_SENSE_BLK_DESC_COUNT;
1187 static int nvme_trans_mode_page_create(struct nvme_ns *ns,
1188 struct sg_io_hdr *hdr, u8 *cmd,
1189 u16 alloc_len, u8 cdb10,
1190 int (*mode_page_fill_func)
1192 struct sg_io_hdr *hdr, u8 *, int),
1193 u16 mode_pages_tot_len)
1201 u16 mode_pages_offset_1;
1202 u16 blk_desc_len, blk_desc_offset, mode_data_length;
1204 dbd = (cmd[1] & MODE_SENSE_DBD_MASK) >> MODE_SENSE_DBD_SHIFT;
1205 llbaa = (cmd[1] & MODE_SENSE_LLBAA_MASK) >> MODE_SENSE_LLBAA_SHIFT;
1206 mph_size = cdb10 ? MODE_SENSE10_MPH_SIZE : MODE_SENSE6_MPH_SIZE;
1208 blk_desc_len = nvme_trans_get_blk_desc_len(dbd, llbaa);
1210 resp_size = mph_size + blk_desc_len + mode_pages_tot_len;
1211 /* Refer spc4r34 Table 440 for calculation of Mode data Length field */
1212 mode_data_length = 3 + (3 * cdb10) + blk_desc_len + mode_pages_tot_len;
1214 blk_desc_offset = mph_size;
1215 mode_pages_offset_1 = blk_desc_offset + blk_desc_len;
1217 response = kzalloc(resp_size, GFP_KERNEL);
1218 if (response == NULL) {
1223 res = nvme_trans_fill_mode_parm_hdr(&response[0], mph_size, cdb10,
1224 llbaa, mode_data_length, blk_desc_len);
1227 if (blk_desc_len > 0) {
1228 res = nvme_trans_fill_blk_desc(ns, hdr,
1229 &response[blk_desc_offset],
1230 blk_desc_len, llbaa);
1234 res = mode_page_fill_func(ns, hdr, &response[mode_pages_offset_1],
1235 mode_pages_tot_len);
1239 xfer_len = min(alloc_len, resp_size);
1240 res = nvme_trans_copy_to_user(hdr, response, xfer_len);
1248 /* Read Capacity Helper Functions */
1250 static void nvme_trans_fill_read_cap(u8 *response, struct nvme_id_ns *id_ns,
1257 u8 p_type_lut[4] = {0, 0, 1, 2};
1262 flbas = (id_ns->flbas) & 0x0F;
1263 lba_length = (1 << (id_ns->lbaf[flbas].ds));
1264 rlba = le64_to_cpup(&id_ns->nsze) - 1;
1265 (id_ns->dps) ? (prot_en = 0x01) : (prot_en = 0);
1268 if (rlba > 0xFFFFFFFF)
1270 tmp_rlba_32 = cpu_to_be32(rlba);
1271 tmp_len = cpu_to_be32(lba_length);
1272 memcpy(response, &tmp_rlba_32, sizeof(u32));
1273 memcpy(&response[4], &tmp_len, sizeof(u32));
1275 tmp_rlba = cpu_to_be64(rlba);
1276 tmp_len = cpu_to_be32(lba_length);
1277 memcpy(response, &tmp_rlba, sizeof(u64));
1278 memcpy(&response[8], &tmp_len, sizeof(u32));
1279 response[12] = (p_type_lut[id_ns->dps & 0x3] << 1) | prot_en;
1280 /* P_I_Exponent = 0x0 | LBPPBE = 0x0 */
1281 /* LBPME = 0 | LBPRZ = 0 | LALBA = 0x00 */
1282 /* Bytes 16-31 - Reserved */
1286 /* Start Stop Unit Helper Functions */
1288 static int nvme_trans_power_state(struct nvme_ns *ns, struct sg_io_hdr *hdr,
1289 u8 pc, u8 pcmod, u8 start)
1293 struct nvme_dev *dev = ns->dev;
1294 dma_addr_t dma_addr;
1296 struct nvme_id_ctrl *id_ctrl;
1297 int lowest_pow_st; /* max npss = lowest power consumption */
1298 unsigned ps_desired = 0;
1300 /* NVMe Controller Identify */
1301 mem = dma_alloc_coherent(dev->dev, sizeof(struct nvme_id_ctrl),
1302 &dma_addr, GFP_KERNEL);
1307 nvme_sc = nvme_identify(dev, 0, 1, dma_addr);
1308 res = nvme_trans_status_code(hdr, nvme_sc);
1313 lowest_pow_st = max(POWER_STATE_0, (int)(id_ctrl->npss - 1));
1316 case NVME_POWER_STATE_START_VALID:
1317 /* Action unspecified if POWER CONDITION MODIFIER != 0 */
1318 if (pcmod == 0 && start == 0x1)
1319 ps_desired = POWER_STATE_0;
1320 if (pcmod == 0 && start == 0x0)
1321 ps_desired = lowest_pow_st;
1323 case NVME_POWER_STATE_ACTIVE:
1324 /* Action unspecified if POWER CONDITION MODIFIER != 0 */
1326 ps_desired = POWER_STATE_0;
1328 case NVME_POWER_STATE_IDLE:
1329 /* Action unspecified if POWER CONDITION MODIFIER != [0,1,2] */
1331 ps_desired = POWER_STATE_1;
1332 else if (pcmod == 0x1)
1333 ps_desired = POWER_STATE_2;
1334 else if (pcmod == 0x2)
1335 ps_desired = POWER_STATE_3;
1337 case NVME_POWER_STATE_STANDBY:
1338 /* Action unspecified if POWER CONDITION MODIFIER != [0,1] */
1340 ps_desired = max(POWER_STATE_0, (lowest_pow_st - 2));
1341 else if (pcmod == 0x1)
1342 ps_desired = max(POWER_STATE_0, (lowest_pow_st - 1));
1344 case NVME_POWER_STATE_LU_CONTROL:
1346 res = nvme_trans_completion(hdr, SAM_STAT_CHECK_CONDITION,
1347 ILLEGAL_REQUEST, SCSI_ASC_INVALID_CDB,
1348 SCSI_ASCQ_CAUSE_NOT_REPORTABLE);
1351 nvme_sc = nvme_set_features(dev, NVME_FEAT_POWER_MGMT, ps_desired, 0,
1353 res = nvme_trans_status_code(hdr, nvme_sc);
1356 dma_free_coherent(dev->dev, sizeof(struct nvme_id_ctrl), mem, dma_addr);
1361 static int nvme_trans_send_activate_fw_cmd(struct nvme_ns *ns, struct sg_io_hdr *hdr,
1364 struct nvme_command c;
1367 memset(&c, 0, sizeof(c));
1368 c.common.opcode = nvme_admin_activate_fw;
1369 c.common.cdw10[0] = cpu_to_le32(buffer_id | NVME_FWACT_REPL_ACTV);
1371 nvme_sc = nvme_submit_sync_cmd(ns->queue, &c);
1372 return nvme_trans_status_code(hdr, nvme_sc);
1375 static int nvme_trans_send_download_fw_cmd(struct nvme_ns *ns, struct sg_io_hdr *hdr,
1376 u8 opcode, u32 tot_len, u32 offset,
1381 struct nvme_dev *dev = ns->dev;
1382 struct nvme_command c;
1383 struct nvme_iod *iod = NULL;
1386 memset(&c, 0, sizeof(c));
1387 c.common.opcode = nvme_admin_download_fw;
1389 if (hdr->iovec_count > 0) {
1390 /* Assuming SGL is not allowed for this command */
1391 return nvme_trans_completion(hdr,
1392 SAM_STAT_CHECK_CONDITION,
1394 SCSI_ASC_INVALID_CDB,
1395 SCSI_ASCQ_CAUSE_NOT_REPORTABLE);
1397 iod = nvme_map_user_pages(dev, DMA_TO_DEVICE,
1398 (unsigned long)hdr->dxferp, tot_len);
1400 return PTR_ERR(iod);
1401 length = nvme_setup_prps(dev, iod, tot_len, GFP_KERNEL);
1402 if (length != tot_len) {
1407 c.dlfw.prp1 = cpu_to_le64(sg_dma_address(iod->sg));
1408 c.dlfw.prp2 = cpu_to_le64(iod->first_dma);
1409 c.dlfw.numd = cpu_to_le32((tot_len/BYTES_TO_DWORDS) - 1);
1410 c.dlfw.offset = cpu_to_le32(offset/BYTES_TO_DWORDS);
1412 nvme_sc = nvme_submit_sync_cmd(dev->admin_q, &c);
1413 res = nvme_trans_status_code(hdr, nvme_sc);
1416 nvme_unmap_user_pages(dev, DMA_TO_DEVICE, iod);
1417 nvme_free_iod(dev, iod);
1421 /* Mode Select Helper Functions */
1423 static inline void nvme_trans_modesel_get_bd_len(u8 *parm_list, u8 cdb10,
1424 u16 *bd_len, u8 *llbaa)
1428 *bd_len = (parm_list[MODE_SELECT_10_BD_OFFSET] << 8) +
1429 parm_list[MODE_SELECT_10_BD_OFFSET + 1];
1430 *llbaa = parm_list[MODE_SELECT_10_LLBAA_OFFSET] &
1431 MODE_SELECT_10_LLBAA_MASK;
1434 *bd_len = parm_list[MODE_SELECT_6_BD_OFFSET];
1438 static void nvme_trans_modesel_save_bd(struct nvme_ns *ns, u8 *parm_list,
1439 u16 idx, u16 bd_len, u8 llbaa)
1443 bd_num = bd_len / ((llbaa == 0) ?
1444 SHORT_DESC_BLOCK : LONG_DESC_BLOCK);
1445 /* Store block descriptor info if a FORMAT UNIT comes later */
1446 /* TODO Saving 1st BD info; what to do if multiple BD received? */
1448 /* Standard Block Descriptor - spc4r34 7.5.5.1 */
1449 ns->mode_select_num_blocks =
1450 (parm_list[idx + 1] << 16) +
1451 (parm_list[idx + 2] << 8) +
1452 (parm_list[idx + 3]);
1454 ns->mode_select_block_len =
1455 (parm_list[idx + 5] << 16) +
1456 (parm_list[idx + 6] << 8) +
1457 (parm_list[idx + 7]);
1459 /* Long LBA Block Descriptor - sbc3r27 6.4.2.3 */
1460 ns->mode_select_num_blocks =
1461 (((u64)parm_list[idx + 0]) << 56) +
1462 (((u64)parm_list[idx + 1]) << 48) +
1463 (((u64)parm_list[idx + 2]) << 40) +
1464 (((u64)parm_list[idx + 3]) << 32) +
1465 (((u64)parm_list[idx + 4]) << 24) +
1466 (((u64)parm_list[idx + 5]) << 16) +
1467 (((u64)parm_list[idx + 6]) << 8) +
1468 ((u64)parm_list[idx + 7]);
1470 ns->mode_select_block_len =
1471 (parm_list[idx + 12] << 24) +
1472 (parm_list[idx + 13] << 16) +
1473 (parm_list[idx + 14] << 8) +
1474 (parm_list[idx + 15]);
1478 static int nvme_trans_modesel_get_mp(struct nvme_ns *ns, struct sg_io_hdr *hdr,
1479 u8 *mode_page, u8 page_code)
1483 struct nvme_dev *dev = ns->dev;
1486 switch (page_code) {
1487 case MODE_PAGE_CACHING:
1488 dword11 = ((mode_page[2] & CACHING_MODE_PAGE_WCE_MASK) ? 1 : 0);
1489 nvme_sc = nvme_set_features(dev, NVME_FEAT_VOLATILE_WC, dword11,
1491 res = nvme_trans_status_code(hdr, nvme_sc);
1493 case MODE_PAGE_CONTROL:
1495 case MODE_PAGE_POWER_CONDITION:
1496 /* Verify the OS is not trying to set timers */
1497 if ((mode_page[2] & 0x01) != 0 || (mode_page[3] & 0x0F) != 0) {
1498 res = nvme_trans_completion(hdr,
1499 SAM_STAT_CHECK_CONDITION,
1501 SCSI_ASC_INVALID_PARAMETER,
1502 SCSI_ASCQ_CAUSE_NOT_REPORTABLE);
1507 res = nvme_trans_completion(hdr, SAM_STAT_CHECK_CONDITION,
1508 ILLEGAL_REQUEST, SCSI_ASC_INVALID_CDB,
1509 SCSI_ASCQ_CAUSE_NOT_REPORTABLE);
1516 static int nvme_trans_modesel_data(struct nvme_ns *ns, struct sg_io_hdr *hdr,
1517 u8 *cmd, u16 parm_list_len, u8 pf,
1524 u16 index, saved_index;
1528 /* Get parm list from data-in/out buffer */
1529 parm_list = kmalloc(parm_list_len, GFP_KERNEL);
1530 if (parm_list == NULL) {
1535 res = nvme_trans_copy_from_user(hdr, parm_list, parm_list_len);
1539 nvme_trans_modesel_get_bd_len(parm_list, cdb10, &bd_len, &llbaa);
1540 index = (cdb10) ? (MODE_SELECT_10_MPH_SIZE) : (MODE_SELECT_6_MPH_SIZE);
1543 /* Block Descriptors present, parse */
1544 nvme_trans_modesel_save_bd(ns, parm_list, index, bd_len, llbaa);
1547 saved_index = index;
1549 /* Multiple mode pages may be present; iterate through all */
1550 /* In 1st Iteration, don't do NVME Command, only check for CDB errors */
1552 page_code = parm_list[index] & MODE_SELECT_PAGE_CODE_MASK;
1553 mp_size = parm_list[index + 1] + 2;
1554 if ((page_code != MODE_PAGE_CACHING) &&
1555 (page_code != MODE_PAGE_CONTROL) &&
1556 (page_code != MODE_PAGE_POWER_CONDITION)) {
1557 res = nvme_trans_completion(hdr,
1558 SAM_STAT_CHECK_CONDITION,
1560 SCSI_ASC_INVALID_CDB,
1561 SCSI_ASCQ_CAUSE_NOT_REPORTABLE);
1565 } while (index < parm_list_len);
1567 /* In 2nd Iteration, do the NVME Commands */
1568 index = saved_index;
1570 page_code = parm_list[index] & MODE_SELECT_PAGE_CODE_MASK;
1571 mp_size = parm_list[index + 1] + 2;
1572 res = nvme_trans_modesel_get_mp(ns, hdr, &parm_list[index],
1577 } while (index < parm_list_len);
1585 /* Format Unit Helper Functions */
1587 static int nvme_trans_fmt_set_blk_size_count(struct nvme_ns *ns,
1588 struct sg_io_hdr *hdr)
1592 struct nvme_dev *dev = ns->dev;
1593 dma_addr_t dma_addr;
1595 struct nvme_id_ns *id_ns;
1599 * SCSI Expects a MODE SELECT would have been issued prior to
1600 * a FORMAT UNIT, and the block size and number would be used
1601 * from the block descriptor in it. If a MODE SELECT had not
1602 * been issued, FORMAT shall use the current values for both.
1605 if (ns->mode_select_num_blocks == 0 || ns->mode_select_block_len == 0) {
1606 mem = dma_alloc_coherent(dev->dev,
1607 sizeof(struct nvme_id_ns), &dma_addr, GFP_KERNEL);
1612 /* nvme ns identify */
1613 nvme_sc = nvme_identify(dev, ns->ns_id, 0, dma_addr);
1614 res = nvme_trans_status_code(hdr, nvme_sc);
1620 if (ns->mode_select_num_blocks == 0)
1621 ns->mode_select_num_blocks = le64_to_cpu(id_ns->ncap);
1622 if (ns->mode_select_block_len == 0) {
1623 flbas = (id_ns->flbas) & 0x0F;
1624 ns->mode_select_block_len =
1625 (1 << (id_ns->lbaf[flbas].ds));
1628 dma_free_coherent(dev->dev, sizeof(struct nvme_id_ns),
1635 static int nvme_trans_fmt_get_parm_header(struct sg_io_hdr *hdr, u8 len,
1636 u8 format_prot_info, u8 *nvme_pf_code)
1640 u8 pf_usage, pf_code;
1642 parm_list = kmalloc(len, GFP_KERNEL);
1643 if (parm_list == NULL) {
1647 res = nvme_trans_copy_from_user(hdr, parm_list, len);
1651 if ((parm_list[FORMAT_UNIT_IMMED_OFFSET] &
1652 FORMAT_UNIT_IMMED_MASK) != 0) {
1653 res = nvme_trans_completion(hdr, SAM_STAT_CHECK_CONDITION,
1654 ILLEGAL_REQUEST, SCSI_ASC_INVALID_CDB,
1655 SCSI_ASCQ_CAUSE_NOT_REPORTABLE);
1659 if (len == FORMAT_UNIT_LONG_PARM_LIST_LEN &&
1660 (parm_list[FORMAT_UNIT_PROT_INT_OFFSET] & 0x0F) != 0) {
1661 res = nvme_trans_completion(hdr, SAM_STAT_CHECK_CONDITION,
1662 ILLEGAL_REQUEST, SCSI_ASC_INVALID_CDB,
1663 SCSI_ASCQ_CAUSE_NOT_REPORTABLE);
1666 pf_usage = parm_list[FORMAT_UNIT_PROT_FIELD_USAGE_OFFSET] &
1667 FORMAT_UNIT_PROT_FIELD_USAGE_MASK;
1668 pf_code = (pf_usage << 2) | format_prot_info;
1683 res = nvme_trans_completion(hdr, SAM_STAT_CHECK_CONDITION,
1684 ILLEGAL_REQUEST, SCSI_ASC_INVALID_CDB,
1685 SCSI_ASCQ_CAUSE_NOT_REPORTABLE);
1695 static int nvme_trans_fmt_send_cmd(struct nvme_ns *ns, struct sg_io_hdr *hdr,
1700 struct nvme_dev *dev = ns->dev;
1701 dma_addr_t dma_addr;
1703 struct nvme_id_ns *id_ns;
1706 u8 selected_lbaf = 0xFF;
1708 struct nvme_command c;
1710 /* Loop thru LBAF's in id_ns to match reqd lbaf, put in cdw10 */
1711 mem = dma_alloc_coherent(dev->dev, sizeof(struct nvme_id_ns),
1712 &dma_addr, GFP_KERNEL);
1717 /* nvme ns identify */
1718 nvme_sc = nvme_identify(dev, ns->ns_id, 0, dma_addr);
1719 res = nvme_trans_status_code(hdr, nvme_sc);
1724 flbas = (id_ns->flbas) & 0x0F;
1725 nlbaf = id_ns->nlbaf;
1727 for (i = 0; i < nlbaf; i++) {
1728 if (ns->mode_select_block_len == (1 << (id_ns->lbaf[i].ds))) {
1733 if (selected_lbaf > 0x0F) {
1734 res = nvme_trans_completion(hdr, SAM_STAT_CHECK_CONDITION,
1735 ILLEGAL_REQUEST, SCSI_ASC_INVALID_PARAMETER,
1736 SCSI_ASCQ_CAUSE_NOT_REPORTABLE);
1738 if (ns->mode_select_num_blocks != le64_to_cpu(id_ns->ncap)) {
1739 res = nvme_trans_completion(hdr, SAM_STAT_CHECK_CONDITION,
1740 ILLEGAL_REQUEST, SCSI_ASC_INVALID_PARAMETER,
1741 SCSI_ASCQ_CAUSE_NOT_REPORTABLE);
1744 cdw10 |= prot_info << 5;
1745 cdw10 |= selected_lbaf & 0x0F;
1746 memset(&c, 0, sizeof(c));
1747 c.format.opcode = nvme_admin_format_nvm;
1748 c.format.nsid = cpu_to_le32(ns->ns_id);
1749 c.format.cdw10 = cpu_to_le32(cdw10);
1751 nvme_sc = nvme_submit_sync_cmd(dev->admin_q, &c);
1752 res = nvme_trans_status_code(hdr, nvme_sc);
1755 dma_free_coherent(dev->dev, sizeof(struct nvme_id_ns), mem, dma_addr);
1760 static inline u32 nvme_trans_io_get_num_cmds(struct sg_io_hdr *hdr,
1761 struct nvme_trans_io_cdb *cdb_info,
1764 /* If using iovecs, send one nvme command per vector */
1765 if (hdr->iovec_count > 0)
1766 return hdr->iovec_count;
1767 else if (cdb_info->xfer_len > max_blocks)
1768 return ((cdb_info->xfer_len - 1) / max_blocks) + 1;
1773 static u16 nvme_trans_io_get_control(struct nvme_ns *ns,
1774 struct nvme_trans_io_cdb *cdb_info)
1778 /* When Protection information support is added, implement here */
1780 if (cdb_info->fua > 0)
1781 control |= NVME_RW_FUA;
1786 static int nvme_trans_do_nvme_io(struct nvme_ns *ns, struct sg_io_hdr *hdr,
1787 struct nvme_trans_io_cdb *cdb_info, u8 is_write)
1789 int nvme_sc = NVME_SC_SUCCESS;
1790 struct nvme_dev *dev = ns->dev;
1792 struct nvme_iod *iod;
1794 u64 unit_num_blocks; /* Number of blocks to xfer in each nvme cmd */
1797 u64 nvme_offset = 0;
1798 void __user *next_mapping_addr;
1799 struct nvme_command c;
1800 u8 opcode = (is_write ? nvme_cmd_write : nvme_cmd_read);
1802 u32 max_blocks = queue_max_hw_sectors(ns->queue);
1804 num_cmds = nvme_trans_io_get_num_cmds(hdr, cdb_info, max_blocks);
1807 * This loop handles two cases.
1808 * First, when an SGL is used in the form of an iovec list:
1809 * - Use iov_base as the next mapping address for the nvme command_id
1810 * - Use iov_len as the data transfer length for the command.
1811 * Second, when we have a single buffer
1812 * - If larger than max_blocks, split into chunks, offset
1813 * each nvme command accordingly.
1815 for (i = 0; i < num_cmds; i++) {
1816 memset(&c, 0, sizeof(c));
1817 if (hdr->iovec_count > 0) {
1818 struct sg_iovec sgl;
1820 retcode = copy_from_user(&sgl, hdr->dxferp +
1821 i * sizeof(struct sg_iovec),
1822 sizeof(struct sg_iovec));
1825 unit_len = sgl.iov_len;
1826 unit_num_blocks = unit_len >> ns->lba_shift;
1827 next_mapping_addr = sgl.iov_base;
1829 unit_num_blocks = min((u64)max_blocks,
1830 (cdb_info->xfer_len - nvme_offset));
1831 unit_len = unit_num_blocks << ns->lba_shift;
1832 next_mapping_addr = hdr->dxferp +
1833 ((1 << ns->lba_shift) * nvme_offset);
1836 c.rw.opcode = opcode;
1837 c.rw.nsid = cpu_to_le32(ns->ns_id);
1838 c.rw.slba = cpu_to_le64(cdb_info->lba + nvme_offset);
1839 c.rw.length = cpu_to_le16(unit_num_blocks - 1);
1840 control = nvme_trans_io_get_control(ns, cdb_info);
1841 c.rw.control = cpu_to_le16(control);
1843 iod = nvme_map_user_pages(dev,
1844 (is_write) ? DMA_TO_DEVICE : DMA_FROM_DEVICE,
1845 (unsigned long)next_mapping_addr, unit_len);
1847 return PTR_ERR(iod);
1849 retcode = nvme_setup_prps(dev, iod, unit_len, GFP_KERNEL);
1850 if (retcode != unit_len) {
1851 nvme_unmap_user_pages(dev,
1852 (is_write) ? DMA_TO_DEVICE : DMA_FROM_DEVICE,
1854 nvme_free_iod(dev, iod);
1857 c.rw.prp1 = cpu_to_le64(sg_dma_address(iod->sg));
1858 c.rw.prp2 = cpu_to_le64(iod->first_dma);
1860 nvme_offset += unit_num_blocks;
1862 nvme_sc = nvme_submit_sync_cmd(ns->queue, &c);
1864 nvme_unmap_user_pages(dev,
1865 (is_write) ? DMA_TO_DEVICE : DMA_FROM_DEVICE,
1867 nvme_free_iod(dev, iod);
1870 if (nvme_sc != NVME_SC_SUCCESS)
1874 return nvme_trans_status_code(hdr, nvme_sc);
1878 /* SCSI Command Translation Functions */
1880 static int nvme_trans_io(struct nvme_ns *ns, struct sg_io_hdr *hdr, u8 is_write,
1884 struct nvme_trans_io_cdb cdb_info = { 0, };
1887 u64 sum_iov_len = 0;
1888 struct sg_iovec sgl;
1893 * The FUA and WPROTECT fields are not supported in 6-byte CDBs,
1894 * but always in the same place for all others.
1901 cdb_info.fua = cmd[1] & 0x8;
1902 cdb_info.prot_info = (cmd[1] & 0xe0) >> 5;
1908 cdb_info.lba = get_unaligned_be24(&cmd[1]);
1909 cdb_info.xfer_len = cmd[4];
1910 if (cdb_info.xfer_len == 0)
1911 cdb_info.xfer_len = 256;
1915 cdb_info.lba = get_unaligned_be32(&cmd[2]);
1916 cdb_info.xfer_len = get_unaligned_be16(&cmd[7]);
1920 cdb_info.lba = get_unaligned_be32(&cmd[2]);
1921 cdb_info.xfer_len = get_unaligned_be32(&cmd[6]);
1925 cdb_info.lba = get_unaligned_be64(&cmd[2]);
1926 cdb_info.xfer_len = get_unaligned_be32(&cmd[10]);
1929 /* Will never really reach here */
1934 /* Calculate total length of transfer (in bytes) */
1935 if (hdr->iovec_count > 0) {
1936 for (i = 0; i < hdr->iovec_count; i++) {
1937 not_copied = copy_from_user(&sgl, hdr->dxferp +
1938 i * sizeof(struct sg_iovec),
1939 sizeof(struct sg_iovec));
1942 sum_iov_len += sgl.iov_len;
1943 /* IO vector sizes should be multiples of block size */
1944 if (sgl.iov_len % (1 << ns->lba_shift) != 0) {
1945 res = nvme_trans_completion(hdr,
1946 SAM_STAT_CHECK_CONDITION,
1948 SCSI_ASC_INVALID_PARAMETER,
1949 SCSI_ASCQ_CAUSE_NOT_REPORTABLE);
1954 sum_iov_len = hdr->dxfer_len;
1957 /* As Per sg ioctl howto, if the lengths differ, use the lower one */
1958 xfer_bytes = min(((u64)hdr->dxfer_len), sum_iov_len);
1960 /* If block count and actual data buffer size dont match, error out */
1961 if (xfer_bytes != (cdb_info.xfer_len << ns->lba_shift)) {
1966 /* Check for 0 length transfer - it is not illegal */
1967 if (cdb_info.xfer_len == 0)
1970 /* Send NVMe IO Command(s) */
1971 res = nvme_trans_do_nvme_io(ns, hdr, &cdb_info, is_write);
1979 static int nvme_trans_inquiry(struct nvme_ns *ns, struct sg_io_hdr *hdr,
1988 evpd = cmd[1] & 0x01;
1990 alloc_len = get_unaligned_be16(&cmd[3]);
1992 inq_response = kmalloc(alloc_len, GFP_KERNEL);
1993 if (inq_response == NULL) {
1999 if (page_code == INQ_STANDARD_INQUIRY_PAGE) {
2000 res = nvme_trans_standard_inquiry_page(ns, hdr,
2001 inq_response, alloc_len);
2003 res = nvme_trans_completion(hdr,
2004 SAM_STAT_CHECK_CONDITION,
2006 SCSI_ASC_INVALID_CDB,
2007 SCSI_ASCQ_CAUSE_NOT_REPORTABLE);
2010 switch (page_code) {
2011 case VPD_SUPPORTED_PAGES:
2012 res = nvme_trans_supported_vpd_pages(ns, hdr,
2013 inq_response, alloc_len);
2015 case VPD_SERIAL_NUMBER:
2016 res = nvme_trans_unit_serial_page(ns, hdr, inq_response,
2019 case VPD_DEVICE_IDENTIFIERS:
2020 res = nvme_trans_device_id_page(ns, hdr, inq_response,
2023 case VPD_EXTENDED_INQUIRY:
2024 res = nvme_trans_ext_inq_page(ns, hdr, alloc_len);
2026 case VPD_BLOCK_LIMITS:
2027 res = nvme_trans_bdev_limits_page(ns, hdr, inq_response,
2030 case VPD_BLOCK_DEV_CHARACTERISTICS:
2031 res = nvme_trans_bdev_char_page(ns, hdr, alloc_len);
2034 res = nvme_trans_completion(hdr,
2035 SAM_STAT_CHECK_CONDITION,
2037 SCSI_ASC_INVALID_CDB,
2038 SCSI_ASCQ_CAUSE_NOT_REPORTABLE);
2042 kfree(inq_response);
2047 static int nvme_trans_log_sense(struct nvme_ns *ns, struct sg_io_hdr *hdr,
2055 if (cmd[1] != LOG_SENSE_CDB_SP_NOT_ENABLED) {
2056 res = nvme_trans_completion(hdr, SAM_STAT_CHECK_CONDITION,
2057 ILLEGAL_REQUEST, SCSI_ASC_INVALID_CDB,
2058 SCSI_ASCQ_CAUSE_NOT_REPORTABLE);
2062 page_code = cmd[2] & LOG_SENSE_CDB_PAGE_CODE_MASK;
2063 pc = (cmd[2] & LOG_SENSE_CDB_PC_MASK) >> LOG_SENSE_CDB_PC_SHIFT;
2064 if (pc != LOG_SENSE_CDB_PC_CUMULATIVE_VALUES) {
2065 res = nvme_trans_completion(hdr, SAM_STAT_CHECK_CONDITION,
2066 ILLEGAL_REQUEST, SCSI_ASC_INVALID_CDB,
2067 SCSI_ASCQ_CAUSE_NOT_REPORTABLE);
2070 alloc_len = get_unaligned_be16(&cmd[7]);
2071 switch (page_code) {
2072 case LOG_PAGE_SUPPORTED_LOG_PAGES_PAGE:
2073 res = nvme_trans_log_supp_pages(ns, hdr, alloc_len);
2075 case LOG_PAGE_INFORMATIONAL_EXCEPTIONS_PAGE:
2076 res = nvme_trans_log_info_exceptions(ns, hdr, alloc_len);
2078 case LOG_PAGE_TEMPERATURE_PAGE:
2079 res = nvme_trans_log_temperature(ns, hdr, alloc_len);
2082 res = nvme_trans_completion(hdr, SAM_STAT_CHECK_CONDITION,
2083 ILLEGAL_REQUEST, SCSI_ASC_INVALID_CDB,
2084 SCSI_ASCQ_CAUSE_NOT_REPORTABLE);
2092 static int nvme_trans_mode_select(struct nvme_ns *ns, struct sg_io_hdr *hdr,
2100 page_format = cmd[1] & MODE_SELECT_CDB_PAGE_FORMAT_MASK;
2101 save_pages = cmd[1] & MODE_SELECT_CDB_SAVE_PAGES_MASK;
2103 if (cmd[0] == MODE_SELECT) {
2104 parm_list_len = cmd[4];
2106 parm_list_len = cmd[7];
2110 if (parm_list_len != 0) {
2112 * According to SPC-4 r24, a paramter list length field of 0
2113 * shall not be considered an error
2115 return nvme_trans_modesel_data(ns, hdr, cmd, parm_list_len,
2116 page_format, save_pages, cdb10);
2122 static int nvme_trans_mode_sense(struct nvme_ns *ns, struct sg_io_hdr *hdr,
2129 if (cmd[0] == MODE_SENSE) {
2132 alloc_len = get_unaligned_be16(&cmd[7]);
2136 if ((cmd[2] & MODE_SENSE_PAGE_CONTROL_MASK) !=
2137 MODE_SENSE_PC_CURRENT_VALUES) {
2138 res = nvme_trans_completion(hdr, SAM_STAT_CHECK_CONDITION,
2139 ILLEGAL_REQUEST, SCSI_ASC_INVALID_CDB,
2140 SCSI_ASCQ_CAUSE_NOT_REPORTABLE);
2144 switch (cmd[2] & MODE_SENSE_PAGE_CODE_MASK) {
2145 case MODE_PAGE_CACHING:
2146 res = nvme_trans_mode_page_create(ns, hdr, cmd, alloc_len,
2148 &nvme_trans_fill_caching_page,
2149 MODE_PAGE_CACHING_LEN);
2151 case MODE_PAGE_CONTROL:
2152 res = nvme_trans_mode_page_create(ns, hdr, cmd, alloc_len,
2154 &nvme_trans_fill_control_page,
2155 MODE_PAGE_CONTROL_LEN);
2157 case MODE_PAGE_POWER_CONDITION:
2158 res = nvme_trans_mode_page_create(ns, hdr, cmd, alloc_len,
2160 &nvme_trans_fill_pow_cnd_page,
2161 MODE_PAGE_POW_CND_LEN);
2163 case MODE_PAGE_INFO_EXCEP:
2164 res = nvme_trans_mode_page_create(ns, hdr, cmd, alloc_len,
2166 &nvme_trans_fill_inf_exc_page,
2167 MODE_PAGE_INF_EXC_LEN);
2169 case MODE_PAGE_RETURN_ALL:
2170 res = nvme_trans_mode_page_create(ns, hdr, cmd, alloc_len,
2172 &nvme_trans_fill_all_pages,
2176 res = nvme_trans_completion(hdr, SAM_STAT_CHECK_CONDITION,
2177 ILLEGAL_REQUEST, SCSI_ASC_INVALID_CDB,
2178 SCSI_ASCQ_CAUSE_NOT_REPORTABLE);
2186 static int nvme_trans_read_capacity(struct nvme_ns *ns, struct sg_io_hdr *hdr,
2194 struct nvme_dev *dev = ns->dev;
2195 dma_addr_t dma_addr;
2197 struct nvme_id_ns *id_ns;
2201 alloc_len = get_unaligned_be32(&cmd[10]);
2202 resp_size = READ_CAP_16_RESP_SIZE;
2204 alloc_len = READ_CAP_10_RESP_SIZE;
2205 resp_size = READ_CAP_10_RESP_SIZE;
2208 mem = dma_alloc_coherent(dev->dev, sizeof(struct nvme_id_ns),
2209 &dma_addr, GFP_KERNEL);
2214 /* nvme ns identify */
2215 nvme_sc = nvme_identify(dev, ns->ns_id, 0, dma_addr);
2216 res = nvme_trans_status_code(hdr, nvme_sc);
2222 response = kzalloc(resp_size, GFP_KERNEL);
2223 if (response == NULL) {
2227 nvme_trans_fill_read_cap(response, id_ns, cdb16);
2229 xfer_len = min(alloc_len, resp_size);
2230 res = nvme_trans_copy_to_user(hdr, response, xfer_len);
2234 dma_free_coherent(dev->dev, sizeof(struct nvme_id_ns), mem, dma_addr);
2239 static int nvme_trans_report_luns(struct nvme_ns *ns, struct sg_io_hdr *hdr,
2244 u32 alloc_len, xfer_len, resp_size;
2246 struct nvme_dev *dev = ns->dev;
2247 dma_addr_t dma_addr;
2249 struct nvme_id_ctrl *id_ctrl;
2250 u32 ll_length, lun_id;
2251 u8 lun_id_offset = REPORT_LUNS_FIRST_LUN_OFFSET;
2256 return nvme_trans_completion(hdr, SAM_STAT_CHECK_CONDITION,
2257 ILLEGAL_REQUEST, SCSI_ASC_INVALID_CDB,
2258 SCSI_ASCQ_CAUSE_NOT_REPORTABLE);
2259 case ALL_LUNS_RETURNED:
2260 case ALL_WELL_KNOWN_LUNS_RETURNED:
2261 case RESTRICTED_LUNS_RETURNED:
2262 /* NVMe Controller Identify */
2263 mem = dma_alloc_coherent(dev->dev, sizeof(struct nvme_id_ctrl),
2264 &dma_addr, GFP_KERNEL);
2269 nvme_sc = nvme_identify(dev, 0, 1, dma_addr);
2270 res = nvme_trans_status_code(hdr, nvme_sc);
2275 ll_length = le32_to_cpu(id_ctrl->nn) * LUN_ENTRY_SIZE;
2276 resp_size = ll_length + LUN_DATA_HEADER_SIZE;
2278 alloc_len = get_unaligned_be32(&cmd[6]);
2279 if (alloc_len < resp_size) {
2280 res = nvme_trans_completion(hdr,
2281 SAM_STAT_CHECK_CONDITION,
2282 ILLEGAL_REQUEST, SCSI_ASC_INVALID_CDB,
2283 SCSI_ASCQ_CAUSE_NOT_REPORTABLE);
2287 response = kzalloc(resp_size, GFP_KERNEL);
2288 if (response == NULL) {
2293 /* The first LUN ID will always be 0 per the SAM spec */
2294 for (lun_id = 0; lun_id < le32_to_cpu(id_ctrl->nn); lun_id++) {
2296 * Set the LUN Id and then increment to the next LUN
2297 * location in the parameter data.
2299 __be64 tmp_id = cpu_to_be64(lun_id);
2300 memcpy(&response[lun_id_offset], &tmp_id, sizeof(u64));
2301 lun_id_offset += LUN_ENTRY_SIZE;
2303 tmp_len = cpu_to_be32(ll_length);
2304 memcpy(response, &tmp_len, sizeof(u32));
2307 xfer_len = min(alloc_len, resp_size);
2308 res = nvme_trans_copy_to_user(hdr, response, xfer_len);
2312 dma_free_coherent(dev->dev, sizeof(struct nvme_id_ctrl), mem, dma_addr);
2317 static int nvme_trans_request_sense(struct nvme_ns *ns, struct sg_io_hdr *hdr,
2321 u8 alloc_len, xfer_len, resp_size;
2325 desc_format = cmd[1] & 0x01;
2328 resp_size = ((desc_format) ? (DESC_FMT_SENSE_DATA_SIZE) :
2329 (FIXED_FMT_SENSE_DATA_SIZE));
2330 response = kzalloc(resp_size, GFP_KERNEL);
2331 if (response == NULL) {
2337 /* Descriptor Format Sense Data */
2338 response[0] = DESC_FORMAT_SENSE_DATA;
2339 response[1] = NO_SENSE;
2340 /* TODO How is LOW POWER CONDITION ON handled? (byte 2) */
2341 response[2] = SCSI_ASC_NO_SENSE;
2342 response[3] = SCSI_ASCQ_CAUSE_NOT_REPORTABLE;
2343 /* SDAT_OVFL = 0 | Additional Sense Length = 0 */
2345 /* Fixed Format Sense Data */
2346 response[0] = FIXED_SENSE_DATA;
2347 /* Byte 1 = Obsolete */
2348 response[2] = NO_SENSE; /* FM, EOM, ILI, SDAT_OVFL = 0 */
2349 /* Bytes 3-6 - Information - set to zero */
2350 response[7] = FIXED_SENSE_DATA_ADD_LENGTH;
2351 /* Bytes 8-11 - Cmd Specific Information - set to zero */
2352 response[12] = SCSI_ASC_NO_SENSE;
2353 response[13] = SCSI_ASCQ_CAUSE_NOT_REPORTABLE;
2354 /* Byte 14 = Field Replaceable Unit Code = 0 */
2355 /* Bytes 15-17 - SKSV=0; Sense Key Specific = 0 */
2358 xfer_len = min(alloc_len, resp_size);
2359 res = nvme_trans_copy_to_user(hdr, response, xfer_len);
2366 static int nvme_trans_security_protocol(struct nvme_ns *ns,
2367 struct sg_io_hdr *hdr,
2370 return nvme_trans_completion(hdr, SAM_STAT_CHECK_CONDITION,
2371 ILLEGAL_REQUEST, SCSI_ASC_ILLEGAL_COMMAND,
2372 SCSI_ASCQ_CAUSE_NOT_REPORTABLE);
2375 static int nvme_trans_start_stop(struct nvme_ns *ns, struct sg_io_hdr *hdr,
2380 struct nvme_command c;
2381 u8 immed, pcmod, pc, no_flush, start;
2383 immed = cmd[1] & 0x01;
2384 pcmod = cmd[3] & 0x0f;
2385 pc = (cmd[4] & 0xf0) >> 4;
2386 no_flush = cmd[4] & 0x04;
2387 start = cmd[4] & 0x01;
2390 return nvme_trans_completion(hdr, SAM_STAT_CHECK_CONDITION,
2391 ILLEGAL_REQUEST, SCSI_ASC_INVALID_CDB,
2392 SCSI_ASCQ_CAUSE_NOT_REPORTABLE);
2394 if (no_flush == 0) {
2395 /* Issue NVME FLUSH command prior to START STOP UNIT */
2396 memset(&c, 0, sizeof(c));
2397 c.common.opcode = nvme_cmd_flush;
2398 c.common.nsid = cpu_to_le32(ns->ns_id);
2400 nvme_sc = nvme_submit_sync_cmd(ns->queue, &c);
2401 res = nvme_trans_status_code(hdr, nvme_sc);
2405 /* Setup the expected power state transition */
2406 return nvme_trans_power_state(ns, hdr, pc, pcmod, start);
2410 static int nvme_trans_synchronize_cache(struct nvme_ns *ns,
2411 struct sg_io_hdr *hdr, u8 *cmd)
2414 struct nvme_command c;
2416 memset(&c, 0, sizeof(c));
2417 c.common.opcode = nvme_cmd_flush;
2418 c.common.nsid = cpu_to_le32(ns->ns_id);
2420 nvme_sc = nvme_submit_sync_cmd(ns->queue, &c);
2421 return nvme_trans_status_code(hdr, nvme_sc);
2424 static int nvme_trans_format_unit(struct nvme_ns *ns, struct sg_io_hdr *hdr,
2428 u8 parm_hdr_len = 0;
2429 u8 nvme_pf_code = 0;
2430 u8 format_prot_info, long_list, format_data;
2432 format_prot_info = (cmd[1] & 0xc0) >> 6;
2433 long_list = cmd[1] & 0x20;
2434 format_data = cmd[1] & 0x10;
2436 if (format_data != 0) {
2437 if (format_prot_info != 0) {
2439 parm_hdr_len = FORMAT_UNIT_SHORT_PARM_LIST_LEN;
2441 parm_hdr_len = FORMAT_UNIT_LONG_PARM_LIST_LEN;
2443 } else if (format_data == 0 && format_prot_info != 0) {
2444 res = nvme_trans_completion(hdr, SAM_STAT_CHECK_CONDITION,
2445 ILLEGAL_REQUEST, SCSI_ASC_INVALID_CDB,
2446 SCSI_ASCQ_CAUSE_NOT_REPORTABLE);
2450 /* Get parm header from data-in/out buffer */
2452 * According to the translation spec, the only fields in the parameter
2453 * list we are concerned with are in the header. So allocate only that.
2455 if (parm_hdr_len > 0) {
2456 res = nvme_trans_fmt_get_parm_header(hdr, parm_hdr_len,
2457 format_prot_info, &nvme_pf_code);
2462 /* Attempt to activate any previously downloaded firmware image */
2463 res = nvme_trans_send_activate_fw_cmd(ns, hdr, 0);
2465 /* Determine Block size and count and send format command */
2466 res = nvme_trans_fmt_set_blk_size_count(ns, hdr);
2470 res = nvme_trans_fmt_send_cmd(ns, hdr, nvme_pf_code);
2476 static int nvme_trans_test_unit_ready(struct nvme_ns *ns,
2477 struct sg_io_hdr *hdr,
2480 struct nvme_dev *dev = ns->dev;
2482 if (!(readl(&dev->bar->csts) & NVME_CSTS_RDY))
2483 return nvme_trans_completion(hdr, SAM_STAT_CHECK_CONDITION,
2484 NOT_READY, SCSI_ASC_LUN_NOT_READY,
2485 SCSI_ASCQ_CAUSE_NOT_REPORTABLE);
2487 return nvme_trans_completion(hdr, SAM_STAT_GOOD, NO_SENSE, 0, 0);
2490 static int nvme_trans_write_buffer(struct nvme_ns *ns, struct sg_io_hdr *hdr,
2494 u32 buffer_offset, parm_list_length;
2497 parm_list_length = get_unaligned_be24(&cmd[6]);
2498 if (parm_list_length % BYTES_TO_DWORDS != 0) {
2499 /* NVMe expects Firmware file to be a whole number of DWORDS */
2500 res = nvme_trans_completion(hdr, SAM_STAT_CHECK_CONDITION,
2501 ILLEGAL_REQUEST, SCSI_ASC_INVALID_CDB,
2502 SCSI_ASCQ_CAUSE_NOT_REPORTABLE);
2506 if (buffer_id > NVME_MAX_FIRMWARE_SLOT) {
2507 res = nvme_trans_completion(hdr, SAM_STAT_CHECK_CONDITION,
2508 ILLEGAL_REQUEST, SCSI_ASC_INVALID_CDB,
2509 SCSI_ASCQ_CAUSE_NOT_REPORTABLE);
2512 mode = cmd[1] & 0x1f;
2513 buffer_offset = get_unaligned_be24(&cmd[3]);
2516 case DOWNLOAD_SAVE_ACTIVATE:
2517 res = nvme_trans_send_download_fw_cmd(ns, hdr, nvme_admin_download_fw,
2518 parm_list_length, buffer_offset,
2522 res = nvme_trans_send_activate_fw_cmd(ns, hdr, buffer_id);
2524 case DOWNLOAD_SAVE_DEFER_ACTIVATE:
2525 res = nvme_trans_send_download_fw_cmd(ns, hdr, nvme_admin_download_fw,
2526 parm_list_length, buffer_offset,
2529 case ACTIVATE_DEFERRED_MICROCODE:
2530 res = nvme_trans_send_activate_fw_cmd(ns, hdr, buffer_id);
2533 res = nvme_trans_completion(hdr, SAM_STAT_CHECK_CONDITION,
2534 ILLEGAL_REQUEST, SCSI_ASC_INVALID_CDB,
2535 SCSI_ASCQ_CAUSE_NOT_REPORTABLE);
2543 struct scsi_unmap_blk_desc {
2549 struct scsi_unmap_parm_list {
2550 __be16 unmap_data_len;
2551 __be16 unmap_blk_desc_data_len;
2553 struct scsi_unmap_blk_desc desc[0];
2556 static int nvme_trans_unmap(struct nvme_ns *ns, struct sg_io_hdr *hdr,
2559 struct nvme_dev *dev = ns->dev;
2560 struct scsi_unmap_parm_list *plist;
2561 struct nvme_dsm_range *range;
2562 struct nvme_command c;
2563 int i, nvme_sc, res = -ENOMEM;
2564 u16 ndesc, list_len;
2565 dma_addr_t dma_addr;
2567 list_len = get_unaligned_be16(&cmd[7]);
2571 plist = kmalloc(list_len, GFP_KERNEL);
2575 res = nvme_trans_copy_from_user(hdr, plist, list_len);
2579 ndesc = be16_to_cpu(plist->unmap_blk_desc_data_len) >> 4;
2580 if (!ndesc || ndesc > 256) {
2585 range = dma_alloc_coherent(dev->dev, ndesc * sizeof(*range),
2586 &dma_addr, GFP_KERNEL);
2590 for (i = 0; i < ndesc; i++) {
2591 range[i].nlb = cpu_to_le32(be32_to_cpu(plist->desc[i].nlb));
2592 range[i].slba = cpu_to_le64(be64_to_cpu(plist->desc[i].slba));
2596 memset(&c, 0, sizeof(c));
2597 c.dsm.opcode = nvme_cmd_dsm;
2598 c.dsm.nsid = cpu_to_le32(ns->ns_id);
2599 c.dsm.prp1 = cpu_to_le64(dma_addr);
2600 c.dsm.nr = cpu_to_le32(ndesc - 1);
2601 c.dsm.attributes = cpu_to_le32(NVME_DSMGMT_AD);
2603 nvme_sc = nvme_submit_sync_cmd(ns->queue, &c);
2604 res = nvme_trans_status_code(hdr, nvme_sc);
2606 dma_free_coherent(dev->dev, ndesc * sizeof(*range), range, dma_addr);
2612 static int nvme_scsi_translate(struct nvme_ns *ns, struct sg_io_hdr *hdr)
2614 u8 cmd[BLK_MAX_CDB];
2616 unsigned int opcode;
2618 if (hdr->cmdp == NULL)
2620 if (copy_from_user(cmd, hdr->cmdp, hdr->cmd_len))
2624 * Prime the hdr with good status for scsi commands that don't require
2625 * an nvme command for translation.
2627 retcode = nvme_trans_status_code(hdr, NVME_SC_SUCCESS);
2638 retcode = nvme_trans_io(ns, hdr, 0, cmd);
2644 retcode = nvme_trans_io(ns, hdr, 1, cmd);
2647 retcode = nvme_trans_inquiry(ns, hdr, cmd);
2650 retcode = nvme_trans_log_sense(ns, hdr, cmd);
2653 case MODE_SELECT_10:
2654 retcode = nvme_trans_mode_select(ns, hdr, cmd);
2658 retcode = nvme_trans_mode_sense(ns, hdr, cmd);
2661 retcode = nvme_trans_read_capacity(ns, hdr, cmd, 0);
2663 case SERVICE_ACTION_IN_16:
2665 case SAI_READ_CAPACITY_16:
2666 retcode = nvme_trans_read_capacity(ns, hdr, cmd, 1);
2673 retcode = nvme_trans_report_luns(ns, hdr, cmd);
2676 retcode = nvme_trans_request_sense(ns, hdr, cmd);
2678 case SECURITY_PROTOCOL_IN:
2679 case SECURITY_PROTOCOL_OUT:
2680 retcode = nvme_trans_security_protocol(ns, hdr, cmd);
2683 retcode = nvme_trans_start_stop(ns, hdr, cmd);
2685 case SYNCHRONIZE_CACHE:
2686 retcode = nvme_trans_synchronize_cache(ns, hdr, cmd);
2689 retcode = nvme_trans_format_unit(ns, hdr, cmd);
2691 case TEST_UNIT_READY:
2692 retcode = nvme_trans_test_unit_ready(ns, hdr, cmd);
2695 retcode = nvme_trans_write_buffer(ns, hdr, cmd);
2698 retcode = nvme_trans_unmap(ns, hdr, cmd);
2702 retcode = nvme_trans_completion(hdr, SAM_STAT_CHECK_CONDITION,
2703 ILLEGAL_REQUEST, SCSI_ASC_ILLEGAL_COMMAND,
2704 SCSI_ASCQ_CAUSE_NOT_REPORTABLE);
2710 int nvme_sg_io(struct nvme_ns *ns, struct sg_io_hdr __user *u_hdr)
2712 struct sg_io_hdr hdr;
2715 if (!capable(CAP_SYS_ADMIN))
2717 if (copy_from_user(&hdr, u_hdr, sizeof(hdr)))
2719 if (hdr.interface_id != 'S')
2721 if (hdr.cmd_len > BLK_MAX_CDB)
2725 * A positive return code means a NVMe status, which has been
2726 * translated to sense data.
2728 retcode = nvme_scsi_translate(ns, &hdr);
2731 if (copy_to_user(u_hdr, &hdr, sizeof(sg_io_hdr_t)) > 0)
2736 int nvme_sg_get_version_num(int __user *ip)
2738 return put_user(sg_version_num, ip);