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
15 #include <linux/blkdev.h>
16 #include <linux/blk-mq.h>
17 #include <linux/delay.h>
18 #include <linux/errno.h>
19 #include <linux/hdreg.h>
20 #include <linux/kernel.h>
21 #include <linux/module.h>
22 #include <linux/list_sort.h>
23 #include <linux/slab.h>
24 #include <linux/types.h>
26 #include <linux/ptrace.h>
27 #include <linux/nvme_ioctl.h>
28 #include <linux/t10-pi.h>
29 #include <linux/pm_qos.h>
31 #include <asm/unaligned.h>
36 #define NVME_MINORS (1U << MINORBITS)
38 unsigned char admin_timeout
= 60;
39 module_param(admin_timeout
, byte
, 0644);
40 MODULE_PARM_DESC(admin_timeout
, "timeout in seconds for admin commands");
41 EXPORT_SYMBOL_GPL(admin_timeout
);
43 unsigned char nvme_io_timeout
= 30;
44 module_param_named(io_timeout
, nvme_io_timeout
, byte
, 0644);
45 MODULE_PARM_DESC(io_timeout
, "timeout in seconds for I/O");
46 EXPORT_SYMBOL_GPL(nvme_io_timeout
);
48 unsigned char shutdown_timeout
= 5;
49 module_param(shutdown_timeout
, byte
, 0644);
50 MODULE_PARM_DESC(shutdown_timeout
, "timeout in seconds for controller shutdown");
52 unsigned int nvme_max_retries
= 5;
53 module_param_named(max_retries
, nvme_max_retries
, uint
, 0644);
54 MODULE_PARM_DESC(max_retries
, "max number of retries a command may have");
55 EXPORT_SYMBOL_GPL(nvme_max_retries
);
57 static int nvme_char_major
;
58 module_param(nvme_char_major
, int, 0);
60 static unsigned long default_ps_max_latency_us
= 25000;
61 module_param(default_ps_max_latency_us
, ulong
, 0644);
62 MODULE_PARM_DESC(default_ps_max_latency_us
,
63 "max power saving latency for new devices; use PM QOS to change per device");
65 static LIST_HEAD(nvme_ctrl_list
);
66 static DEFINE_SPINLOCK(dev_list_lock
);
68 static struct class *nvme_class
;
70 void nvme_cancel_request(struct request
*req
, void *data
, bool reserved
)
74 if (!blk_mq_request_started(req
))
77 dev_dbg_ratelimited(((struct nvme_ctrl
*) data
)->device
,
78 "Cancelling I/O %d", req
->tag
);
80 status
= NVME_SC_ABORT_REQ
;
81 if (blk_queue_dying(req
->q
))
82 status
|= NVME_SC_DNR
;
83 blk_mq_complete_request(req
, status
);
85 EXPORT_SYMBOL_GPL(nvme_cancel_request
);
87 bool nvme_change_ctrl_state(struct nvme_ctrl
*ctrl
,
88 enum nvme_ctrl_state new_state
)
90 enum nvme_ctrl_state old_state
;
93 spin_lock_irq(&ctrl
->lock
);
95 old_state
= ctrl
->state
;
100 case NVME_CTRL_RESETTING
:
101 case NVME_CTRL_RECONNECTING
:
108 case NVME_CTRL_RESETTING
:
112 case NVME_CTRL_RECONNECTING
:
119 case NVME_CTRL_RECONNECTING
:
128 case NVME_CTRL_DELETING
:
131 case NVME_CTRL_RESETTING
:
132 case NVME_CTRL_RECONNECTING
:
141 case NVME_CTRL_DELETING
:
153 ctrl
->state
= new_state
;
155 spin_unlock_irq(&ctrl
->lock
);
159 EXPORT_SYMBOL_GPL(nvme_change_ctrl_state
);
161 static void nvme_free_ns(struct kref
*kref
)
163 struct nvme_ns
*ns
= container_of(kref
, struct nvme_ns
, kref
);
166 nvme_nvm_unregister(ns
);
169 spin_lock(&dev_list_lock
);
170 ns
->disk
->private_data
= NULL
;
171 spin_unlock(&dev_list_lock
);
175 ida_simple_remove(&ns
->ctrl
->ns_ida
, ns
->instance
);
176 nvme_put_ctrl(ns
->ctrl
);
180 static void nvme_put_ns(struct nvme_ns
*ns
)
182 kref_put(&ns
->kref
, nvme_free_ns
);
185 static struct nvme_ns
*nvme_get_ns_from_disk(struct gendisk
*disk
)
189 spin_lock(&dev_list_lock
);
190 ns
= disk
->private_data
;
192 if (!kref_get_unless_zero(&ns
->kref
))
194 if (!try_module_get(ns
->ctrl
->ops
->module
))
197 spin_unlock(&dev_list_lock
);
202 kref_put(&ns
->kref
, nvme_free_ns
);
204 spin_unlock(&dev_list_lock
);
208 void nvme_requeue_req(struct request
*req
)
210 blk_mq_requeue_request(req
, !blk_mq_queue_stopped(req
->q
));
212 EXPORT_SYMBOL_GPL(nvme_requeue_req
);
214 struct request
*nvme_alloc_request(struct request_queue
*q
,
215 struct nvme_command
*cmd
, unsigned int flags
, int qid
)
217 unsigned op
= nvme_is_write(cmd
) ? REQ_OP_DRV_OUT
: REQ_OP_DRV_IN
;
220 if (qid
== NVME_QID_ANY
) {
221 req
= blk_mq_alloc_request(q
, op
, flags
);
223 req
= blk_mq_alloc_request_hctx(q
, op
, flags
,
229 req
->cmd_flags
|= REQ_FAILFAST_DRIVER
;
230 nvme_req(req
)->cmd
= cmd
;
234 EXPORT_SYMBOL_GPL(nvme_alloc_request
);
236 static inline void nvme_setup_flush(struct nvme_ns
*ns
,
237 struct nvme_command
*cmnd
)
239 memset(cmnd
, 0, sizeof(*cmnd
));
240 cmnd
->common
.opcode
= nvme_cmd_flush
;
241 cmnd
->common
.nsid
= cpu_to_le32(ns
->ns_id
);
244 static inline int nvme_setup_discard(struct nvme_ns
*ns
, struct request
*req
,
245 struct nvme_command
*cmnd
)
247 unsigned short segments
= blk_rq_nr_discard_segments(req
), n
= 0;
248 struct nvme_dsm_range
*range
;
251 range
= kmalloc_array(segments
, sizeof(*range
), GFP_ATOMIC
);
253 return BLK_MQ_RQ_QUEUE_BUSY
;
255 __rq_for_each_bio(bio
, req
) {
256 u64 slba
= nvme_block_nr(ns
, bio
->bi_iter
.bi_sector
);
257 u32 nlb
= bio
->bi_iter
.bi_size
>> ns
->lba_shift
;
259 range
[n
].cattr
= cpu_to_le32(0);
260 range
[n
].nlb
= cpu_to_le32(nlb
);
261 range
[n
].slba
= cpu_to_le64(slba
);
265 if (WARN_ON_ONCE(n
!= segments
)) {
267 return BLK_MQ_RQ_QUEUE_ERROR
;
270 memset(cmnd
, 0, sizeof(*cmnd
));
271 cmnd
->dsm
.opcode
= nvme_cmd_dsm
;
272 cmnd
->dsm
.nsid
= cpu_to_le32(ns
->ns_id
);
273 cmnd
->dsm
.nr
= segments
- 1;
274 cmnd
->dsm
.attributes
= cpu_to_le32(NVME_DSMGMT_AD
);
276 req
->special_vec
.bv_page
= virt_to_page(range
);
277 req
->special_vec
.bv_offset
= offset_in_page(range
);
278 req
->special_vec
.bv_len
= sizeof(*range
) * segments
;
279 req
->rq_flags
|= RQF_SPECIAL_PAYLOAD
;
281 return BLK_MQ_RQ_QUEUE_OK
;
284 static inline void nvme_setup_rw(struct nvme_ns
*ns
, struct request
*req
,
285 struct nvme_command
*cmnd
)
290 if (req
->cmd_flags
& REQ_FUA
)
291 control
|= NVME_RW_FUA
;
292 if (req
->cmd_flags
& (REQ_FAILFAST_DEV
| REQ_RAHEAD
))
293 control
|= NVME_RW_LR
;
295 if (req
->cmd_flags
& REQ_RAHEAD
)
296 dsmgmt
|= NVME_RW_DSM_FREQ_PREFETCH
;
298 memset(cmnd
, 0, sizeof(*cmnd
));
299 cmnd
->rw
.opcode
= (rq_data_dir(req
) ? nvme_cmd_write
: nvme_cmd_read
);
300 cmnd
->rw
.nsid
= cpu_to_le32(ns
->ns_id
);
301 cmnd
->rw
.slba
= cpu_to_le64(nvme_block_nr(ns
, blk_rq_pos(req
)));
302 cmnd
->rw
.length
= cpu_to_le16((blk_rq_bytes(req
) >> ns
->lba_shift
) - 1);
305 switch (ns
->pi_type
) {
306 case NVME_NS_DPS_PI_TYPE3
:
307 control
|= NVME_RW_PRINFO_PRCHK_GUARD
;
309 case NVME_NS_DPS_PI_TYPE1
:
310 case NVME_NS_DPS_PI_TYPE2
:
311 control
|= NVME_RW_PRINFO_PRCHK_GUARD
|
312 NVME_RW_PRINFO_PRCHK_REF
;
313 cmnd
->rw
.reftag
= cpu_to_le32(
314 nvme_block_nr(ns
, blk_rq_pos(req
)));
317 if (!blk_integrity_rq(req
))
318 control
|= NVME_RW_PRINFO_PRACT
;
321 cmnd
->rw
.control
= cpu_to_le16(control
);
322 cmnd
->rw
.dsmgmt
= cpu_to_le32(dsmgmt
);
325 int nvme_setup_cmd(struct nvme_ns
*ns
, struct request
*req
,
326 struct nvme_command
*cmd
)
328 int ret
= BLK_MQ_RQ_QUEUE_OK
;
330 switch (req_op(req
)) {
333 memcpy(cmd
, nvme_req(req
)->cmd
, sizeof(*cmd
));
336 nvme_setup_flush(ns
, cmd
);
339 ret
= nvme_setup_discard(ns
, req
, cmd
);
343 nvme_setup_rw(ns
, req
, cmd
);
347 return BLK_MQ_RQ_QUEUE_ERROR
;
350 cmd
->common
.command_id
= req
->tag
;
353 EXPORT_SYMBOL_GPL(nvme_setup_cmd
);
356 * Returns 0 on success. If the result is negative, it's a Linux error code;
357 * if the result is positive, it's an NVM Express status code
359 int __nvme_submit_sync_cmd(struct request_queue
*q
, struct nvme_command
*cmd
,
360 union nvme_result
*result
, void *buffer
, unsigned bufflen
,
361 unsigned timeout
, int qid
, int at_head
, int flags
)
366 req
= nvme_alloc_request(q
, cmd
, flags
, qid
);
370 req
->timeout
= timeout
? timeout
: ADMIN_TIMEOUT
;
372 if (buffer
&& bufflen
) {
373 ret
= blk_rq_map_kern(q
, req
, buffer
, bufflen
, GFP_KERNEL
);
378 blk_execute_rq(req
->q
, NULL
, req
, at_head
);
380 *result
= nvme_req(req
)->result
;
383 blk_mq_free_request(req
);
386 EXPORT_SYMBOL_GPL(__nvme_submit_sync_cmd
);
388 int nvme_submit_sync_cmd(struct request_queue
*q
, struct nvme_command
*cmd
,
389 void *buffer
, unsigned bufflen
)
391 return __nvme_submit_sync_cmd(q
, cmd
, NULL
, buffer
, bufflen
, 0,
394 EXPORT_SYMBOL_GPL(nvme_submit_sync_cmd
);
396 int __nvme_submit_user_cmd(struct request_queue
*q
, struct nvme_command
*cmd
,
397 void __user
*ubuffer
, unsigned bufflen
,
398 void __user
*meta_buffer
, unsigned meta_len
, u32 meta_seed
,
399 u32
*result
, unsigned timeout
)
401 bool write
= nvme_is_write(cmd
);
402 struct nvme_ns
*ns
= q
->queuedata
;
403 struct gendisk
*disk
= ns
? ns
->disk
: NULL
;
405 struct bio
*bio
= NULL
;
409 req
= nvme_alloc_request(q
, cmd
, 0, NVME_QID_ANY
);
413 req
->timeout
= timeout
? timeout
: ADMIN_TIMEOUT
;
415 if (ubuffer
&& bufflen
) {
416 ret
= blk_rq_map_user(q
, req
, NULL
, ubuffer
, bufflen
,
424 bio
->bi_bdev
= bdget_disk(disk
, 0);
430 if (meta_buffer
&& meta_len
) {
431 struct bio_integrity_payload
*bip
;
433 meta
= kmalloc(meta_len
, GFP_KERNEL
);
440 if (copy_from_user(meta
, meta_buffer
,
447 bip
= bio_integrity_alloc(bio
, GFP_KERNEL
, 1);
453 bip
->bip_iter
.bi_size
= meta_len
;
454 bip
->bip_iter
.bi_sector
= meta_seed
;
456 ret
= bio_integrity_add_page(bio
, virt_to_page(meta
),
457 meta_len
, offset_in_page(meta
));
458 if (ret
!= meta_len
) {
465 blk_execute_rq(req
->q
, disk
, req
, 0);
468 *result
= le32_to_cpu(nvme_req(req
)->result
.u32
);
469 if (meta
&& !ret
&& !write
) {
470 if (copy_to_user(meta_buffer
, meta
, meta_len
))
477 if (disk
&& bio
->bi_bdev
)
479 blk_rq_unmap_user(bio
);
482 blk_mq_free_request(req
);
486 int nvme_submit_user_cmd(struct request_queue
*q
, struct nvme_command
*cmd
,
487 void __user
*ubuffer
, unsigned bufflen
, u32
*result
,
490 return __nvme_submit_user_cmd(q
, cmd
, ubuffer
, bufflen
, NULL
, 0, 0,
494 static void nvme_keep_alive_end_io(struct request
*rq
, int error
)
496 struct nvme_ctrl
*ctrl
= rq
->end_io_data
;
498 blk_mq_free_request(rq
);
501 dev_err(ctrl
->device
,
502 "failed nvme_keep_alive_end_io error=%d\n", error
);
506 schedule_delayed_work(&ctrl
->ka_work
, ctrl
->kato
* HZ
);
509 static int nvme_keep_alive(struct nvme_ctrl
*ctrl
)
511 struct nvme_command c
;
514 memset(&c
, 0, sizeof(c
));
515 c
.common
.opcode
= nvme_admin_keep_alive
;
517 rq
= nvme_alloc_request(ctrl
->admin_q
, &c
, BLK_MQ_REQ_RESERVED
,
522 rq
->timeout
= ctrl
->kato
* HZ
;
523 rq
->end_io_data
= ctrl
;
525 blk_execute_rq_nowait(rq
->q
, NULL
, rq
, 0, nvme_keep_alive_end_io
);
530 static void nvme_keep_alive_work(struct work_struct
*work
)
532 struct nvme_ctrl
*ctrl
= container_of(to_delayed_work(work
),
533 struct nvme_ctrl
, ka_work
);
535 if (nvme_keep_alive(ctrl
)) {
536 /* allocation failure, reset the controller */
537 dev_err(ctrl
->device
, "keep-alive failed\n");
538 ctrl
->ops
->reset_ctrl(ctrl
);
543 void nvme_start_keep_alive(struct nvme_ctrl
*ctrl
)
545 if (unlikely(ctrl
->kato
== 0))
548 INIT_DELAYED_WORK(&ctrl
->ka_work
, nvme_keep_alive_work
);
549 schedule_delayed_work(&ctrl
->ka_work
, ctrl
->kato
* HZ
);
551 EXPORT_SYMBOL_GPL(nvme_start_keep_alive
);
553 void nvme_stop_keep_alive(struct nvme_ctrl
*ctrl
)
555 if (unlikely(ctrl
->kato
== 0))
558 cancel_delayed_work_sync(&ctrl
->ka_work
);
560 EXPORT_SYMBOL_GPL(nvme_stop_keep_alive
);
562 int nvme_identify_ctrl(struct nvme_ctrl
*dev
, struct nvme_id_ctrl
**id
)
564 struct nvme_command c
= { };
567 /* gcc-4.4.4 (at least) has issues with initializers and anon unions */
568 c
.identify
.opcode
= nvme_admin_identify
;
569 c
.identify
.cns
= NVME_ID_CNS_CTRL
;
571 *id
= kmalloc(sizeof(struct nvme_id_ctrl
), GFP_KERNEL
);
575 error
= nvme_submit_sync_cmd(dev
->admin_q
, &c
, *id
,
576 sizeof(struct nvme_id_ctrl
));
582 static int nvme_identify_ns_list(struct nvme_ctrl
*dev
, unsigned nsid
, __le32
*ns_list
)
584 struct nvme_command c
= { };
586 c
.identify
.opcode
= nvme_admin_identify
;
587 c
.identify
.cns
= NVME_ID_CNS_NS_ACTIVE_LIST
;
588 c
.identify
.nsid
= cpu_to_le32(nsid
);
589 return nvme_submit_sync_cmd(dev
->admin_q
, &c
, ns_list
, 0x1000);
592 int nvme_identify_ns(struct nvme_ctrl
*dev
, unsigned nsid
,
593 struct nvme_id_ns
**id
)
595 struct nvme_command c
= { };
598 /* gcc-4.4.4 (at least) has issues with initializers and anon unions */
599 c
.identify
.opcode
= nvme_admin_identify
;
600 c
.identify
.nsid
= cpu_to_le32(nsid
);
601 c
.identify
.cns
= NVME_ID_CNS_NS
;
603 *id
= kmalloc(sizeof(struct nvme_id_ns
), GFP_KERNEL
);
607 error
= nvme_submit_sync_cmd(dev
->admin_q
, &c
, *id
,
608 sizeof(struct nvme_id_ns
));
614 int nvme_get_features(struct nvme_ctrl
*dev
, unsigned fid
, unsigned nsid
,
615 void *buffer
, size_t buflen
, u32
*result
)
617 struct nvme_command c
;
618 union nvme_result res
;
621 memset(&c
, 0, sizeof(c
));
622 c
.features
.opcode
= nvme_admin_get_features
;
623 c
.features
.nsid
= cpu_to_le32(nsid
);
624 c
.features
.fid
= cpu_to_le32(fid
);
626 ret
= __nvme_submit_sync_cmd(dev
->admin_q
, &c
, &res
, buffer
, buflen
, 0,
628 if (ret
>= 0 && result
)
629 *result
= le32_to_cpu(res
.u32
);
633 int nvme_set_features(struct nvme_ctrl
*dev
, unsigned fid
, unsigned dword11
,
634 void *buffer
, size_t buflen
, u32
*result
)
636 struct nvme_command c
;
637 union nvme_result res
;
640 memset(&c
, 0, sizeof(c
));
641 c
.features
.opcode
= nvme_admin_set_features
;
642 c
.features
.fid
= cpu_to_le32(fid
);
643 c
.features
.dword11
= cpu_to_le32(dword11
);
645 ret
= __nvme_submit_sync_cmd(dev
->admin_q
, &c
, &res
,
646 buffer
, buflen
, 0, NVME_QID_ANY
, 0, 0);
647 if (ret
>= 0 && result
)
648 *result
= le32_to_cpu(res
.u32
);
652 int nvme_get_log_page(struct nvme_ctrl
*dev
, struct nvme_smart_log
**log
)
654 struct nvme_command c
= { };
657 c
.common
.opcode
= nvme_admin_get_log_page
,
658 c
.common
.nsid
= cpu_to_le32(0xFFFFFFFF),
659 c
.common
.cdw10
[0] = cpu_to_le32(
660 (((sizeof(struct nvme_smart_log
) / 4) - 1) << 16) |
663 *log
= kmalloc(sizeof(struct nvme_smart_log
), GFP_KERNEL
);
667 error
= nvme_submit_sync_cmd(dev
->admin_q
, &c
, *log
,
668 sizeof(struct nvme_smart_log
));
674 int nvme_set_queue_count(struct nvme_ctrl
*ctrl
, int *count
)
676 u32 q_count
= (*count
- 1) | ((*count
- 1) << 16);
678 int status
, nr_io_queues
;
680 status
= nvme_set_features(ctrl
, NVME_FEAT_NUM_QUEUES
, q_count
, NULL
, 0,
686 * Degraded controllers might return an error when setting the queue
687 * count. We still want to be able to bring them online and offer
688 * access to the admin queue, as that might be only way to fix them up.
691 dev_err(ctrl
->dev
, "Could not set queue count (%d)\n", status
);
694 nr_io_queues
= min(result
& 0xffff, result
>> 16) + 1;
695 *count
= min(*count
, nr_io_queues
);
700 EXPORT_SYMBOL_GPL(nvme_set_queue_count
);
702 static int nvme_submit_io(struct nvme_ns
*ns
, struct nvme_user_io __user
*uio
)
704 struct nvme_user_io io
;
705 struct nvme_command c
;
706 unsigned length
, meta_len
;
707 void __user
*metadata
;
709 if (copy_from_user(&io
, uio
, sizeof(io
)))
717 case nvme_cmd_compare
:
723 length
= (io
.nblocks
+ 1) << ns
->lba_shift
;
724 meta_len
= (io
.nblocks
+ 1) * ns
->ms
;
725 metadata
= (void __user
*)(uintptr_t)io
.metadata
;
730 } else if (meta_len
) {
731 if ((io
.metadata
& 3) || !io
.metadata
)
735 memset(&c
, 0, sizeof(c
));
736 c
.rw
.opcode
= io
.opcode
;
737 c
.rw
.flags
= io
.flags
;
738 c
.rw
.nsid
= cpu_to_le32(ns
->ns_id
);
739 c
.rw
.slba
= cpu_to_le64(io
.slba
);
740 c
.rw
.length
= cpu_to_le16(io
.nblocks
);
741 c
.rw
.control
= cpu_to_le16(io
.control
);
742 c
.rw
.dsmgmt
= cpu_to_le32(io
.dsmgmt
);
743 c
.rw
.reftag
= cpu_to_le32(io
.reftag
);
744 c
.rw
.apptag
= cpu_to_le16(io
.apptag
);
745 c
.rw
.appmask
= cpu_to_le16(io
.appmask
);
747 return __nvme_submit_user_cmd(ns
->queue
, &c
,
748 (void __user
*)(uintptr_t)io
.addr
, length
,
749 metadata
, meta_len
, io
.slba
, NULL
, 0);
752 static int nvme_user_cmd(struct nvme_ctrl
*ctrl
, struct nvme_ns
*ns
,
753 struct nvme_passthru_cmd __user
*ucmd
)
755 struct nvme_passthru_cmd cmd
;
756 struct nvme_command c
;
757 unsigned timeout
= 0;
760 if (!capable(CAP_SYS_ADMIN
))
762 if (copy_from_user(&cmd
, ucmd
, sizeof(cmd
)))
767 memset(&c
, 0, sizeof(c
));
768 c
.common
.opcode
= cmd
.opcode
;
769 c
.common
.flags
= cmd
.flags
;
770 c
.common
.nsid
= cpu_to_le32(cmd
.nsid
);
771 c
.common
.cdw2
[0] = cpu_to_le32(cmd
.cdw2
);
772 c
.common
.cdw2
[1] = cpu_to_le32(cmd
.cdw3
);
773 c
.common
.cdw10
[0] = cpu_to_le32(cmd
.cdw10
);
774 c
.common
.cdw10
[1] = cpu_to_le32(cmd
.cdw11
);
775 c
.common
.cdw10
[2] = cpu_to_le32(cmd
.cdw12
);
776 c
.common
.cdw10
[3] = cpu_to_le32(cmd
.cdw13
);
777 c
.common
.cdw10
[4] = cpu_to_le32(cmd
.cdw14
);
778 c
.common
.cdw10
[5] = cpu_to_le32(cmd
.cdw15
);
781 timeout
= msecs_to_jiffies(cmd
.timeout_ms
);
783 status
= nvme_submit_user_cmd(ns
? ns
->queue
: ctrl
->admin_q
, &c
,
784 (void __user
*)(uintptr_t)cmd
.addr
, cmd
.data_len
,
785 &cmd
.result
, timeout
);
787 if (put_user(cmd
.result
, &ucmd
->result
))
794 static int nvme_ioctl(struct block_device
*bdev
, fmode_t mode
,
795 unsigned int cmd
, unsigned long arg
)
797 struct nvme_ns
*ns
= bdev
->bd_disk
->private_data
;
801 force_successful_syscall_return();
803 case NVME_IOCTL_ADMIN_CMD
:
804 return nvme_user_cmd(ns
->ctrl
, NULL
, (void __user
*)arg
);
805 case NVME_IOCTL_IO_CMD
:
806 return nvme_user_cmd(ns
->ctrl
, ns
, (void __user
*)arg
);
807 case NVME_IOCTL_SUBMIT_IO
:
808 return nvme_submit_io(ns
, (void __user
*)arg
);
809 #ifdef CONFIG_BLK_DEV_NVME_SCSI
810 case SG_GET_VERSION_NUM
:
811 return nvme_sg_get_version_num((void __user
*)arg
);
813 return nvme_sg_io(ns
, (void __user
*)arg
);
818 return nvme_nvm_ioctl(ns
, cmd
, arg
);
820 if (is_sed_ioctl(cmd
))
821 return sed_ioctl(ns
->ctrl
->opal_dev
, cmd
,
822 (void __user
*) arg
);
828 static int nvme_compat_ioctl(struct block_device
*bdev
, fmode_t mode
,
829 unsigned int cmd
, unsigned long arg
)
835 return nvme_ioctl(bdev
, mode
, cmd
, arg
);
838 #define nvme_compat_ioctl NULL
841 static int nvme_open(struct block_device
*bdev
, fmode_t mode
)
843 return nvme_get_ns_from_disk(bdev
->bd_disk
) ? 0 : -ENXIO
;
846 static void nvme_release(struct gendisk
*disk
, fmode_t mode
)
848 struct nvme_ns
*ns
= disk
->private_data
;
850 module_put(ns
->ctrl
->ops
->module
);
854 static int nvme_getgeo(struct block_device
*bdev
, struct hd_geometry
*geo
)
856 /* some standard values */
858 geo
->sectors
= 1 << 5;
859 geo
->cylinders
= get_capacity(bdev
->bd_disk
) >> 11;
863 #ifdef CONFIG_BLK_DEV_INTEGRITY
864 static void nvme_init_integrity(struct nvme_ns
*ns
)
866 struct blk_integrity integrity
;
868 memset(&integrity
, 0, sizeof(integrity
));
869 switch (ns
->pi_type
) {
870 case NVME_NS_DPS_PI_TYPE3
:
871 integrity
.profile
= &t10_pi_type3_crc
;
872 integrity
.tag_size
= sizeof(u16
) + sizeof(u32
);
873 integrity
.flags
|= BLK_INTEGRITY_DEVICE_CAPABLE
;
875 case NVME_NS_DPS_PI_TYPE1
:
876 case NVME_NS_DPS_PI_TYPE2
:
877 integrity
.profile
= &t10_pi_type1_crc
;
878 integrity
.tag_size
= sizeof(u16
);
879 integrity
.flags
|= BLK_INTEGRITY_DEVICE_CAPABLE
;
882 integrity
.profile
= NULL
;
885 integrity
.tuple_size
= ns
->ms
;
886 blk_integrity_register(ns
->disk
, &integrity
);
887 blk_queue_max_integrity_segments(ns
->queue
, 1);
890 static void nvme_init_integrity(struct nvme_ns
*ns
)
893 #endif /* CONFIG_BLK_DEV_INTEGRITY */
895 static void nvme_config_discard(struct nvme_ns
*ns
)
897 struct nvme_ctrl
*ctrl
= ns
->ctrl
;
898 u32 logical_block_size
= queue_logical_block_size(ns
->queue
);
900 BUILD_BUG_ON(PAGE_SIZE
/ sizeof(struct nvme_dsm_range
) <
901 NVME_DSM_MAX_RANGES
);
903 if (ctrl
->quirks
& NVME_QUIRK_DISCARD_ZEROES
)
904 ns
->queue
->limits
.discard_zeroes_data
= 1;
906 ns
->queue
->limits
.discard_zeroes_data
= 0;
908 ns
->queue
->limits
.discard_alignment
= logical_block_size
;
909 ns
->queue
->limits
.discard_granularity
= logical_block_size
;
910 blk_queue_max_discard_sectors(ns
->queue
, UINT_MAX
);
911 blk_queue_max_discard_segments(ns
->queue
, NVME_DSM_MAX_RANGES
);
912 queue_flag_set_unlocked(QUEUE_FLAG_DISCARD
, ns
->queue
);
915 static int nvme_revalidate_ns(struct nvme_ns
*ns
, struct nvme_id_ns
**id
)
917 if (nvme_identify_ns(ns
->ctrl
, ns
->ns_id
, id
)) {
918 dev_warn(ns
->ctrl
->dev
, "%s: Identify failure\n", __func__
);
922 if ((*id
)->ncap
== 0) {
927 if (ns
->ctrl
->vs
>= NVME_VS(1, 1, 0))
928 memcpy(ns
->eui
, (*id
)->eui64
, sizeof(ns
->eui
));
929 if (ns
->ctrl
->vs
>= NVME_VS(1, 2, 0))
930 memcpy(ns
->uuid
, (*id
)->nguid
, sizeof(ns
->uuid
));
935 static void __nvme_revalidate_disk(struct gendisk
*disk
, struct nvme_id_ns
*id
)
937 struct nvme_ns
*ns
= disk
->private_data
;
943 lbaf
= id
->flbas
& NVME_NS_FLBAS_LBA_MASK
;
944 ns
->lba_shift
= id
->lbaf
[lbaf
].ds
;
945 ns
->ms
= le16_to_cpu(id
->lbaf
[lbaf
].ms
);
946 ns
->ext
= ns
->ms
&& (id
->flbas
& NVME_NS_FLBAS_META_EXT
);
949 * If identify namespace failed, use default 512 byte block size so
950 * block layer can use before failing read/write for 0 capacity.
952 if (ns
->lba_shift
== 0)
954 bs
= 1 << ns
->lba_shift
;
955 /* XXX: PI implementation requires metadata equal t10 pi tuple size */
956 pi_type
= ns
->ms
== sizeof(struct t10_pi_tuple
) ?
957 id
->dps
& NVME_NS_DPS_PI_MASK
: 0;
959 blk_mq_freeze_queue(disk
->queue
);
960 if (blk_get_integrity(disk
) && (ns
->pi_type
!= pi_type
||
962 bs
!= queue_logical_block_size(disk
->queue
) ||
963 (ns
->ms
&& ns
->ext
)))
964 blk_integrity_unregister(disk
);
966 ns
->pi_type
= pi_type
;
967 blk_queue_logical_block_size(ns
->queue
, bs
);
969 if (ns
->ms
&& !blk_get_integrity(disk
) && !ns
->ext
)
970 nvme_init_integrity(ns
);
971 if (ns
->ms
&& !(ns
->ms
== 8 && ns
->pi_type
) && !blk_get_integrity(disk
))
972 set_capacity(disk
, 0);
974 set_capacity(disk
, le64_to_cpup(&id
->nsze
) << (ns
->lba_shift
- 9));
976 if (ns
->ctrl
->oncs
& NVME_CTRL_ONCS_DSM
)
977 nvme_config_discard(ns
);
978 blk_mq_unfreeze_queue(disk
->queue
);
981 static int nvme_revalidate_disk(struct gendisk
*disk
)
983 struct nvme_ns
*ns
= disk
->private_data
;
984 struct nvme_id_ns
*id
= NULL
;
987 if (test_bit(NVME_NS_DEAD
, &ns
->flags
)) {
988 set_capacity(disk
, 0);
992 ret
= nvme_revalidate_ns(ns
, &id
);
996 __nvme_revalidate_disk(disk
, id
);
1002 static char nvme_pr_type(enum pr_type type
)
1005 case PR_WRITE_EXCLUSIVE
:
1007 case PR_EXCLUSIVE_ACCESS
:
1009 case PR_WRITE_EXCLUSIVE_REG_ONLY
:
1011 case PR_EXCLUSIVE_ACCESS_REG_ONLY
:
1013 case PR_WRITE_EXCLUSIVE_ALL_REGS
:
1015 case PR_EXCLUSIVE_ACCESS_ALL_REGS
:
1022 static int nvme_pr_command(struct block_device
*bdev
, u32 cdw10
,
1023 u64 key
, u64 sa_key
, u8 op
)
1025 struct nvme_ns
*ns
= bdev
->bd_disk
->private_data
;
1026 struct nvme_command c
;
1027 u8 data
[16] = { 0, };
1029 put_unaligned_le64(key
, &data
[0]);
1030 put_unaligned_le64(sa_key
, &data
[8]);
1032 memset(&c
, 0, sizeof(c
));
1033 c
.common
.opcode
= op
;
1034 c
.common
.nsid
= cpu_to_le32(ns
->ns_id
);
1035 c
.common
.cdw10
[0] = cpu_to_le32(cdw10
);
1037 return nvme_submit_sync_cmd(ns
->queue
, &c
, data
, 16);
1040 static int nvme_pr_register(struct block_device
*bdev
, u64 old
,
1041 u64
new, unsigned flags
)
1045 if (flags
& ~PR_FL_IGNORE_KEY
)
1048 cdw10
= old
? 2 : 0;
1049 cdw10
|= (flags
& PR_FL_IGNORE_KEY
) ? 1 << 3 : 0;
1050 cdw10
|= (1 << 30) | (1 << 31); /* PTPL=1 */
1051 return nvme_pr_command(bdev
, cdw10
, old
, new, nvme_cmd_resv_register
);
1054 static int nvme_pr_reserve(struct block_device
*bdev
, u64 key
,
1055 enum pr_type type
, unsigned flags
)
1059 if (flags
& ~PR_FL_IGNORE_KEY
)
1062 cdw10
= nvme_pr_type(type
) << 8;
1063 cdw10
|= ((flags
& PR_FL_IGNORE_KEY
) ? 1 << 3 : 0);
1064 return nvme_pr_command(bdev
, cdw10
, key
, 0, nvme_cmd_resv_acquire
);
1067 static int nvme_pr_preempt(struct block_device
*bdev
, u64 old
, u64
new,
1068 enum pr_type type
, bool abort
)
1070 u32 cdw10
= nvme_pr_type(type
) << 8 | abort
? 2 : 1;
1071 return nvme_pr_command(bdev
, cdw10
, old
, new, nvme_cmd_resv_acquire
);
1074 static int nvme_pr_clear(struct block_device
*bdev
, u64 key
)
1076 u32 cdw10
= 1 | (key
? 1 << 3 : 0);
1077 return nvme_pr_command(bdev
, cdw10
, key
, 0, nvme_cmd_resv_register
);
1080 static int nvme_pr_release(struct block_device
*bdev
, u64 key
, enum pr_type type
)
1082 u32 cdw10
= nvme_pr_type(type
) << 8 | key
? 1 << 3 : 0;
1083 return nvme_pr_command(bdev
, cdw10
, key
, 0, nvme_cmd_resv_release
);
1086 static const struct pr_ops nvme_pr_ops
= {
1087 .pr_register
= nvme_pr_register
,
1088 .pr_reserve
= nvme_pr_reserve
,
1089 .pr_release
= nvme_pr_release
,
1090 .pr_preempt
= nvme_pr_preempt
,
1091 .pr_clear
= nvme_pr_clear
,
1094 #ifdef CONFIG_BLK_SED_OPAL
1095 int nvme_sec_submit(void *data
, u16 spsp
, u8 secp
, void *buffer
, size_t len
,
1098 struct nvme_ctrl
*ctrl
= data
;
1099 struct nvme_command cmd
;
1101 memset(&cmd
, 0, sizeof(cmd
));
1103 cmd
.common
.opcode
= nvme_admin_security_send
;
1105 cmd
.common
.opcode
= nvme_admin_security_recv
;
1106 cmd
.common
.nsid
= 0;
1107 cmd
.common
.cdw10
[0] = cpu_to_le32(((u32
)secp
) << 24 | ((u32
)spsp
) << 8);
1108 cmd
.common
.cdw10
[1] = cpu_to_le32(len
);
1110 return __nvme_submit_sync_cmd(ctrl
->admin_q
, &cmd
, NULL
, buffer
, len
,
1111 ADMIN_TIMEOUT
, NVME_QID_ANY
, 1, 0);
1113 EXPORT_SYMBOL_GPL(nvme_sec_submit
);
1114 #endif /* CONFIG_BLK_SED_OPAL */
1116 static const struct block_device_operations nvme_fops
= {
1117 .owner
= THIS_MODULE
,
1118 .ioctl
= nvme_ioctl
,
1119 .compat_ioctl
= nvme_compat_ioctl
,
1121 .release
= nvme_release
,
1122 .getgeo
= nvme_getgeo
,
1123 .revalidate_disk
= nvme_revalidate_disk
,
1124 .pr_ops
= &nvme_pr_ops
,
1127 static int nvme_wait_ready(struct nvme_ctrl
*ctrl
, u64 cap
, bool enabled
)
1129 unsigned long timeout
=
1130 ((NVME_CAP_TIMEOUT(cap
) + 1) * HZ
/ 2) + jiffies
;
1131 u32 csts
, bit
= enabled
? NVME_CSTS_RDY
: 0;
1134 while ((ret
= ctrl
->ops
->reg_read32(ctrl
, NVME_REG_CSTS
, &csts
)) == 0) {
1137 if ((csts
& NVME_CSTS_RDY
) == bit
)
1141 if (fatal_signal_pending(current
))
1143 if (time_after(jiffies
, timeout
)) {
1144 dev_err(ctrl
->device
,
1145 "Device not ready; aborting %s\n", enabled
?
1146 "initialisation" : "reset");
1155 * If the device has been passed off to us in an enabled state, just clear
1156 * the enabled bit. The spec says we should set the 'shutdown notification
1157 * bits', but doing so may cause the device to complete commands to the
1158 * admin queue ... and we don't know what memory that might be pointing at!
1160 int nvme_disable_ctrl(struct nvme_ctrl
*ctrl
, u64 cap
)
1164 ctrl
->ctrl_config
&= ~NVME_CC_SHN_MASK
;
1165 ctrl
->ctrl_config
&= ~NVME_CC_ENABLE
;
1167 ret
= ctrl
->ops
->reg_write32(ctrl
, NVME_REG_CC
, ctrl
->ctrl_config
);
1171 if (ctrl
->quirks
& NVME_QUIRK_DELAY_BEFORE_CHK_RDY
)
1172 msleep(NVME_QUIRK_DELAY_AMOUNT
);
1174 return nvme_wait_ready(ctrl
, cap
, false);
1176 EXPORT_SYMBOL_GPL(nvme_disable_ctrl
);
1178 int nvme_enable_ctrl(struct nvme_ctrl
*ctrl
, u64 cap
)
1181 * Default to a 4K page size, with the intention to update this
1182 * path in the future to accomodate architectures with differing
1183 * kernel and IO page sizes.
1185 unsigned dev_page_min
= NVME_CAP_MPSMIN(cap
) + 12, page_shift
= 12;
1188 if (page_shift
< dev_page_min
) {
1189 dev_err(ctrl
->device
,
1190 "Minimum device page size %u too large for host (%u)\n",
1191 1 << dev_page_min
, 1 << page_shift
);
1195 ctrl
->page_size
= 1 << page_shift
;
1197 ctrl
->ctrl_config
= NVME_CC_CSS_NVM
;
1198 ctrl
->ctrl_config
|= (page_shift
- 12) << NVME_CC_MPS_SHIFT
;
1199 ctrl
->ctrl_config
|= NVME_CC_ARB_RR
| NVME_CC_SHN_NONE
;
1200 ctrl
->ctrl_config
|= NVME_CC_IOSQES
| NVME_CC_IOCQES
;
1201 ctrl
->ctrl_config
|= NVME_CC_ENABLE
;
1203 ret
= ctrl
->ops
->reg_write32(ctrl
, NVME_REG_CC
, ctrl
->ctrl_config
);
1206 return nvme_wait_ready(ctrl
, cap
, true);
1208 EXPORT_SYMBOL_GPL(nvme_enable_ctrl
);
1210 int nvme_shutdown_ctrl(struct nvme_ctrl
*ctrl
)
1212 unsigned long timeout
= SHUTDOWN_TIMEOUT
+ jiffies
;
1216 ctrl
->ctrl_config
&= ~NVME_CC_SHN_MASK
;
1217 ctrl
->ctrl_config
|= NVME_CC_SHN_NORMAL
;
1219 ret
= ctrl
->ops
->reg_write32(ctrl
, NVME_REG_CC
, ctrl
->ctrl_config
);
1223 while ((ret
= ctrl
->ops
->reg_read32(ctrl
, NVME_REG_CSTS
, &csts
)) == 0) {
1224 if ((csts
& NVME_CSTS_SHST_MASK
) == NVME_CSTS_SHST_CMPLT
)
1228 if (fatal_signal_pending(current
))
1230 if (time_after(jiffies
, timeout
)) {
1231 dev_err(ctrl
->device
,
1232 "Device shutdown incomplete; abort shutdown\n");
1239 EXPORT_SYMBOL_GPL(nvme_shutdown_ctrl
);
1241 static void nvme_set_queue_limits(struct nvme_ctrl
*ctrl
,
1242 struct request_queue
*q
)
1246 if (ctrl
->max_hw_sectors
) {
1248 (ctrl
->max_hw_sectors
/ (ctrl
->page_size
>> 9)) + 1;
1250 blk_queue_max_hw_sectors(q
, ctrl
->max_hw_sectors
);
1251 blk_queue_max_segments(q
, min_t(u32
, max_segments
, USHRT_MAX
));
1253 if (ctrl
->quirks
& NVME_QUIRK_STRIPE_SIZE
)
1254 blk_queue_chunk_sectors(q
, ctrl
->max_hw_sectors
);
1255 blk_queue_virt_boundary(q
, ctrl
->page_size
- 1);
1256 if (ctrl
->vwc
& NVME_CTRL_VWC_PRESENT
)
1258 blk_queue_write_cache(q
, vwc
, vwc
);
1261 static void nvme_configure_apst(struct nvme_ctrl
*ctrl
)
1264 * APST (Autonomous Power State Transition) lets us program a
1265 * table of power state transitions that the controller will
1266 * perform automatically. We configure it with a simple
1267 * heuristic: we are willing to spend at most 2% of the time
1268 * transitioning between power states. Therefore, when running
1269 * in any given state, we will enter the next lower-power
1270 * non-operational state after waiting 100 * (enlat + exlat)
1271 * microseconds, as long as that state's total latency is under
1272 * the requested maximum latency.
1274 * We will not autonomously enter any non-operational state for
1275 * which the total latency exceeds ps_max_latency_us. Users
1276 * can set ps_max_latency_us to zero to turn off APST.
1280 struct nvme_feat_auto_pst
*table
;
1284 * If APST isn't supported or if we haven't been initialized yet,
1285 * then don't do anything.
1290 if (ctrl
->npss
> 31) {
1291 dev_warn(ctrl
->device
, "NPSS is invalid; not using APST\n");
1295 table
= kzalloc(sizeof(*table
), GFP_KERNEL
);
1299 if (ctrl
->ps_max_latency_us
== 0) {
1300 /* Turn off APST. */
1303 __le64 target
= cpu_to_le64(0);
1307 * Walk through all states from lowest- to highest-power.
1308 * According to the spec, lower-numbered states use more
1309 * power. NPSS, despite the name, is the index of the
1310 * lowest-power state, not the number of states.
1312 for (state
= (int)ctrl
->npss
; state
>= 0; state
--) {
1313 u64 total_latency_us
, transition_ms
;
1316 table
->entries
[state
] = target
;
1319 * Is this state a useful non-operational state for
1320 * higher-power states to autonomously transition to?
1322 if (!(ctrl
->psd
[state
].flags
&
1323 NVME_PS_FLAGS_NON_OP_STATE
))
1327 (u64
)le32_to_cpu(ctrl
->psd
[state
].entry_lat
) +
1328 + le32_to_cpu(ctrl
->psd
[state
].exit_lat
);
1329 if (total_latency_us
> ctrl
->ps_max_latency_us
)
1333 * This state is good. Use it as the APST idle
1334 * target for higher power states.
1336 transition_ms
= total_latency_us
+ 19;
1337 do_div(transition_ms
, 20);
1338 if (transition_ms
> (1 << 24) - 1)
1339 transition_ms
= (1 << 24) - 1;
1341 target
= cpu_to_le64((state
<< 3) |
1342 (transition_ms
<< 8));
1348 ret
= nvme_set_features(ctrl
, NVME_FEAT_AUTO_PST
, apste
,
1349 table
, sizeof(*table
), NULL
);
1351 dev_err(ctrl
->device
, "failed to set APST feature (%d)\n", ret
);
1356 static void nvme_set_latency_tolerance(struct device
*dev
, s32 val
)
1358 struct nvme_ctrl
*ctrl
= dev_get_drvdata(dev
);
1362 case PM_QOS_LATENCY_TOLERANCE_NO_CONSTRAINT
:
1363 case PM_QOS_LATENCY_ANY
:
1371 if (ctrl
->ps_max_latency_us
!= latency
) {
1372 ctrl
->ps_max_latency_us
= latency
;
1373 nvme_configure_apst(ctrl
);
1377 struct nvme_core_quirk_entry
{
1379 * NVMe model and firmware strings are padded with spaces. For
1380 * simplicity, strings in the quirk table are padded with NULLs
1386 unsigned long quirks
;
1389 static const struct nvme_core_quirk_entry core_quirks
[] = {
1391 * Seen on a Samsung "SM951 NVMe SAMSUNG 256GB": using APST causes
1392 * the controller to go out to lunch. It dies when the watchdog
1393 * timer reads CSTS and gets 0xffffffff.
1398 .quirks
= NVME_QUIRK_NO_APST
,
1402 /* match is null-terminated but idstr is space-padded. */
1403 static bool string_matches(const char *idstr
, const char *match
, size_t len
)
1410 matchlen
= strlen(match
);
1411 WARN_ON_ONCE(matchlen
> len
);
1413 if (memcmp(idstr
, match
, matchlen
))
1416 for (; matchlen
< len
; matchlen
++)
1417 if (idstr
[matchlen
] != ' ')
1423 static bool quirk_matches(const struct nvme_id_ctrl
*id
,
1424 const struct nvme_core_quirk_entry
*q
)
1426 return q
->vid
== le16_to_cpu(id
->vid
) &&
1427 string_matches(id
->mn
, q
->mn
, sizeof(id
->mn
)) &&
1428 string_matches(id
->fr
, q
->fr
, sizeof(id
->fr
));
1432 * Initialize the cached copies of the Identify data and various controller
1433 * register in our nvme_ctrl structure. This should be called as soon as
1434 * the admin queue is fully up and running.
1436 int nvme_init_identify(struct nvme_ctrl
*ctrl
)
1438 struct nvme_id_ctrl
*id
;
1440 int ret
, page_shift
;
1444 ret
= ctrl
->ops
->reg_read32(ctrl
, NVME_REG_VS
, &ctrl
->vs
);
1446 dev_err(ctrl
->device
, "Reading VS failed (%d)\n", ret
);
1450 ret
= ctrl
->ops
->reg_read64(ctrl
, NVME_REG_CAP
, &cap
);
1452 dev_err(ctrl
->device
, "Reading CAP failed (%d)\n", ret
);
1455 page_shift
= NVME_CAP_MPSMIN(cap
) + 12;
1457 if (ctrl
->vs
>= NVME_VS(1, 1, 0))
1458 ctrl
->subsystem
= NVME_CAP_NSSRC(cap
);
1460 ret
= nvme_identify_ctrl(ctrl
, &id
);
1462 dev_err(ctrl
->device
, "Identify Controller failed (%d)\n", ret
);
1466 if (!ctrl
->identified
) {
1468 * Check for quirks. Quirk can depend on firmware version,
1469 * so, in principle, the set of quirks present can change
1470 * across a reset. As a possible future enhancement, we
1471 * could re-scan for quirks every time we reinitialize
1472 * the device, but we'd have to make sure that the driver
1473 * behaves intelligently if the quirks change.
1478 for (i
= 0; i
< ARRAY_SIZE(core_quirks
); i
++) {
1479 if (quirk_matches(id
, &core_quirks
[i
]))
1480 ctrl
->quirks
|= core_quirks
[i
].quirks
;
1484 ctrl
->oacs
= le16_to_cpu(id
->oacs
);
1485 ctrl
->vid
= le16_to_cpu(id
->vid
);
1486 ctrl
->oncs
= le16_to_cpup(&id
->oncs
);
1487 atomic_set(&ctrl
->abort_limit
, id
->acl
+ 1);
1488 ctrl
->vwc
= id
->vwc
;
1489 ctrl
->cntlid
= le16_to_cpup(&id
->cntlid
);
1490 memcpy(ctrl
->serial
, id
->sn
, sizeof(id
->sn
));
1491 memcpy(ctrl
->model
, id
->mn
, sizeof(id
->mn
));
1492 memcpy(ctrl
->firmware_rev
, id
->fr
, sizeof(id
->fr
));
1494 max_hw_sectors
= 1 << (id
->mdts
+ page_shift
- 9);
1496 max_hw_sectors
= UINT_MAX
;
1497 ctrl
->max_hw_sectors
=
1498 min_not_zero(ctrl
->max_hw_sectors
, max_hw_sectors
);
1500 nvme_set_queue_limits(ctrl
, ctrl
->admin_q
);
1501 ctrl
->sgls
= le32_to_cpu(id
->sgls
);
1502 ctrl
->kas
= le16_to_cpu(id
->kas
);
1504 ctrl
->npss
= id
->npss
;
1505 prev_apsta
= ctrl
->apsta
;
1506 ctrl
->apsta
= (ctrl
->quirks
& NVME_QUIRK_NO_APST
) ? 0 : id
->apsta
;
1507 memcpy(ctrl
->psd
, id
->psd
, sizeof(ctrl
->psd
));
1509 if (ctrl
->ops
->is_fabrics
) {
1510 ctrl
->icdoff
= le16_to_cpu(id
->icdoff
);
1511 ctrl
->ioccsz
= le32_to_cpu(id
->ioccsz
);
1512 ctrl
->iorcsz
= le32_to_cpu(id
->iorcsz
);
1513 ctrl
->maxcmd
= le16_to_cpu(id
->maxcmd
);
1516 * In fabrics we need to verify the cntlid matches the
1519 if (ctrl
->cntlid
!= le16_to_cpu(id
->cntlid
))
1522 if (!ctrl
->opts
->discovery_nqn
&& !ctrl
->kas
) {
1524 "keep-alive support is mandatory for fabrics\n");
1528 ctrl
->cntlid
= le16_to_cpu(id
->cntlid
);
1533 if (ctrl
->apsta
&& !prev_apsta
)
1534 dev_pm_qos_expose_latency_tolerance(ctrl
->device
);
1535 else if (!ctrl
->apsta
&& prev_apsta
)
1536 dev_pm_qos_hide_latency_tolerance(ctrl
->device
);
1538 nvme_configure_apst(ctrl
);
1540 ctrl
->identified
= true;
1544 EXPORT_SYMBOL_GPL(nvme_init_identify
);
1546 static int nvme_dev_open(struct inode
*inode
, struct file
*file
)
1548 struct nvme_ctrl
*ctrl
;
1549 int instance
= iminor(inode
);
1552 spin_lock(&dev_list_lock
);
1553 list_for_each_entry(ctrl
, &nvme_ctrl_list
, node
) {
1554 if (ctrl
->instance
!= instance
)
1557 if (!ctrl
->admin_q
) {
1561 if (!kref_get_unless_zero(&ctrl
->kref
))
1563 file
->private_data
= ctrl
;
1567 spin_unlock(&dev_list_lock
);
1572 static int nvme_dev_release(struct inode
*inode
, struct file
*file
)
1574 nvme_put_ctrl(file
->private_data
);
1578 static int nvme_dev_user_cmd(struct nvme_ctrl
*ctrl
, void __user
*argp
)
1583 mutex_lock(&ctrl
->namespaces_mutex
);
1584 if (list_empty(&ctrl
->namespaces
)) {
1589 ns
= list_first_entry(&ctrl
->namespaces
, struct nvme_ns
, list
);
1590 if (ns
!= list_last_entry(&ctrl
->namespaces
, struct nvme_ns
, list
)) {
1591 dev_warn(ctrl
->device
,
1592 "NVME_IOCTL_IO_CMD not supported when multiple namespaces present!\n");
1597 dev_warn(ctrl
->device
,
1598 "using deprecated NVME_IOCTL_IO_CMD ioctl on the char device!\n");
1599 kref_get(&ns
->kref
);
1600 mutex_unlock(&ctrl
->namespaces_mutex
);
1602 ret
= nvme_user_cmd(ctrl
, ns
, argp
);
1607 mutex_unlock(&ctrl
->namespaces_mutex
);
1611 static long nvme_dev_ioctl(struct file
*file
, unsigned int cmd
,
1614 struct nvme_ctrl
*ctrl
= file
->private_data
;
1615 void __user
*argp
= (void __user
*)arg
;
1618 case NVME_IOCTL_ADMIN_CMD
:
1619 return nvme_user_cmd(ctrl
, NULL
, argp
);
1620 case NVME_IOCTL_IO_CMD
:
1621 return nvme_dev_user_cmd(ctrl
, argp
);
1622 case NVME_IOCTL_RESET
:
1623 dev_warn(ctrl
->device
, "resetting controller\n");
1624 return ctrl
->ops
->reset_ctrl(ctrl
);
1625 case NVME_IOCTL_SUBSYS_RESET
:
1626 return nvme_reset_subsystem(ctrl
);
1627 case NVME_IOCTL_RESCAN
:
1628 nvme_queue_scan(ctrl
);
1635 static const struct file_operations nvme_dev_fops
= {
1636 .owner
= THIS_MODULE
,
1637 .open
= nvme_dev_open
,
1638 .release
= nvme_dev_release
,
1639 .unlocked_ioctl
= nvme_dev_ioctl
,
1640 .compat_ioctl
= nvme_dev_ioctl
,
1643 static ssize_t
nvme_sysfs_reset(struct device
*dev
,
1644 struct device_attribute
*attr
, const char *buf
,
1647 struct nvme_ctrl
*ctrl
= dev_get_drvdata(dev
);
1650 ret
= ctrl
->ops
->reset_ctrl(ctrl
);
1655 static DEVICE_ATTR(reset_controller
, S_IWUSR
, NULL
, nvme_sysfs_reset
);
1657 static ssize_t
nvme_sysfs_rescan(struct device
*dev
,
1658 struct device_attribute
*attr
, const char *buf
,
1661 struct nvme_ctrl
*ctrl
= dev_get_drvdata(dev
);
1663 nvme_queue_scan(ctrl
);
1666 static DEVICE_ATTR(rescan_controller
, S_IWUSR
, NULL
, nvme_sysfs_rescan
);
1668 static ssize_t
wwid_show(struct device
*dev
, struct device_attribute
*attr
,
1671 struct nvme_ns
*ns
= nvme_get_ns_from_dev(dev
);
1672 struct nvme_ctrl
*ctrl
= ns
->ctrl
;
1673 int serial_len
= sizeof(ctrl
->serial
);
1674 int model_len
= sizeof(ctrl
->model
);
1676 if (memchr_inv(ns
->uuid
, 0, sizeof(ns
->uuid
)))
1677 return sprintf(buf
, "eui.%16phN\n", ns
->uuid
);
1679 if (memchr_inv(ns
->eui
, 0, sizeof(ns
->eui
)))
1680 return sprintf(buf
, "eui.%8phN\n", ns
->eui
);
1682 while (ctrl
->serial
[serial_len
- 1] == ' ')
1684 while (ctrl
->model
[model_len
- 1] == ' ')
1687 return sprintf(buf
, "nvme.%04x-%*phN-%*phN-%08x\n", ctrl
->vid
,
1688 serial_len
, ctrl
->serial
, model_len
, ctrl
->model
, ns
->ns_id
);
1690 static DEVICE_ATTR(wwid
, S_IRUGO
, wwid_show
, NULL
);
1692 static ssize_t
uuid_show(struct device
*dev
, struct device_attribute
*attr
,
1695 struct nvme_ns
*ns
= nvme_get_ns_from_dev(dev
);
1696 return sprintf(buf
, "%pU\n", ns
->uuid
);
1698 static DEVICE_ATTR(uuid
, S_IRUGO
, uuid_show
, NULL
);
1700 static ssize_t
eui_show(struct device
*dev
, struct device_attribute
*attr
,
1703 struct nvme_ns
*ns
= nvme_get_ns_from_dev(dev
);
1704 return sprintf(buf
, "%8phd\n", ns
->eui
);
1706 static DEVICE_ATTR(eui
, S_IRUGO
, eui_show
, NULL
);
1708 static ssize_t
nsid_show(struct device
*dev
, struct device_attribute
*attr
,
1711 struct nvme_ns
*ns
= nvme_get_ns_from_dev(dev
);
1712 return sprintf(buf
, "%d\n", ns
->ns_id
);
1714 static DEVICE_ATTR(nsid
, S_IRUGO
, nsid_show
, NULL
);
1716 static struct attribute
*nvme_ns_attrs
[] = {
1717 &dev_attr_wwid
.attr
,
1718 &dev_attr_uuid
.attr
,
1720 &dev_attr_nsid
.attr
,
1724 static umode_t
nvme_ns_attrs_are_visible(struct kobject
*kobj
,
1725 struct attribute
*a
, int n
)
1727 struct device
*dev
= container_of(kobj
, struct device
, kobj
);
1728 struct nvme_ns
*ns
= nvme_get_ns_from_dev(dev
);
1730 if (a
== &dev_attr_uuid
.attr
) {
1731 if (!memchr_inv(ns
->uuid
, 0, sizeof(ns
->uuid
)))
1734 if (a
== &dev_attr_eui
.attr
) {
1735 if (!memchr_inv(ns
->eui
, 0, sizeof(ns
->eui
)))
1741 static const struct attribute_group nvme_ns_attr_group
= {
1742 .attrs
= nvme_ns_attrs
,
1743 .is_visible
= nvme_ns_attrs_are_visible
,
1746 #define nvme_show_str_function(field) \
1747 static ssize_t field##_show(struct device *dev, \
1748 struct device_attribute *attr, char *buf) \
1750 struct nvme_ctrl *ctrl = dev_get_drvdata(dev); \
1751 return sprintf(buf, "%.*s\n", (int)sizeof(ctrl->field), ctrl->field); \
1753 static DEVICE_ATTR(field, S_IRUGO, field##_show, NULL);
1755 #define nvme_show_int_function(field) \
1756 static ssize_t field##_show(struct device *dev, \
1757 struct device_attribute *attr, char *buf) \
1759 struct nvme_ctrl *ctrl = dev_get_drvdata(dev); \
1760 return sprintf(buf, "%d\n", ctrl->field); \
1762 static DEVICE_ATTR(field, S_IRUGO, field##_show, NULL);
1764 nvme_show_str_function(model
);
1765 nvme_show_str_function(serial
);
1766 nvme_show_str_function(firmware_rev
);
1767 nvme_show_int_function(cntlid
);
1769 static ssize_t
nvme_sysfs_delete(struct device
*dev
,
1770 struct device_attribute
*attr
, const char *buf
,
1773 struct nvme_ctrl
*ctrl
= dev_get_drvdata(dev
);
1775 if (device_remove_file_self(dev
, attr
))
1776 ctrl
->ops
->delete_ctrl(ctrl
);
1779 static DEVICE_ATTR(delete_controller
, S_IWUSR
, NULL
, nvme_sysfs_delete
);
1781 static ssize_t
nvme_sysfs_show_transport(struct device
*dev
,
1782 struct device_attribute
*attr
,
1785 struct nvme_ctrl
*ctrl
= dev_get_drvdata(dev
);
1787 return snprintf(buf
, PAGE_SIZE
, "%s\n", ctrl
->ops
->name
);
1789 static DEVICE_ATTR(transport
, S_IRUGO
, nvme_sysfs_show_transport
, NULL
);
1791 static ssize_t
nvme_sysfs_show_state(struct device
*dev
,
1792 struct device_attribute
*attr
,
1795 struct nvme_ctrl
*ctrl
= dev_get_drvdata(dev
);
1796 static const char *const state_name
[] = {
1797 [NVME_CTRL_NEW
] = "new",
1798 [NVME_CTRL_LIVE
] = "live",
1799 [NVME_CTRL_RESETTING
] = "resetting",
1800 [NVME_CTRL_RECONNECTING
]= "reconnecting",
1801 [NVME_CTRL_DELETING
] = "deleting",
1802 [NVME_CTRL_DEAD
] = "dead",
1805 if ((unsigned)ctrl
->state
< ARRAY_SIZE(state_name
) &&
1806 state_name
[ctrl
->state
])
1807 return sprintf(buf
, "%s\n", state_name
[ctrl
->state
]);
1809 return sprintf(buf
, "unknown state\n");
1812 static DEVICE_ATTR(state
, S_IRUGO
, nvme_sysfs_show_state
, NULL
);
1814 static ssize_t
nvme_sysfs_show_subsysnqn(struct device
*dev
,
1815 struct device_attribute
*attr
,
1818 struct nvme_ctrl
*ctrl
= dev_get_drvdata(dev
);
1820 return snprintf(buf
, PAGE_SIZE
, "%s\n",
1821 ctrl
->ops
->get_subsysnqn(ctrl
));
1823 static DEVICE_ATTR(subsysnqn
, S_IRUGO
, nvme_sysfs_show_subsysnqn
, NULL
);
1825 static ssize_t
nvme_sysfs_show_address(struct device
*dev
,
1826 struct device_attribute
*attr
,
1829 struct nvme_ctrl
*ctrl
= dev_get_drvdata(dev
);
1831 return ctrl
->ops
->get_address(ctrl
, buf
, PAGE_SIZE
);
1833 static DEVICE_ATTR(address
, S_IRUGO
, nvme_sysfs_show_address
, NULL
);
1835 static struct attribute
*nvme_dev_attrs
[] = {
1836 &dev_attr_reset_controller
.attr
,
1837 &dev_attr_rescan_controller
.attr
,
1838 &dev_attr_model
.attr
,
1839 &dev_attr_serial
.attr
,
1840 &dev_attr_firmware_rev
.attr
,
1841 &dev_attr_cntlid
.attr
,
1842 &dev_attr_delete_controller
.attr
,
1843 &dev_attr_transport
.attr
,
1844 &dev_attr_subsysnqn
.attr
,
1845 &dev_attr_address
.attr
,
1846 &dev_attr_state
.attr
,
1850 #define CHECK_ATTR(ctrl, a, name) \
1851 if ((a) == &dev_attr_##name.attr && \
1852 !(ctrl)->ops->get_##name) \
1855 static umode_t
nvme_dev_attrs_are_visible(struct kobject
*kobj
,
1856 struct attribute
*a
, int n
)
1858 struct device
*dev
= container_of(kobj
, struct device
, kobj
);
1859 struct nvme_ctrl
*ctrl
= dev_get_drvdata(dev
);
1861 if (a
== &dev_attr_delete_controller
.attr
) {
1862 if (!ctrl
->ops
->delete_ctrl
)
1866 CHECK_ATTR(ctrl
, a
, subsysnqn
);
1867 CHECK_ATTR(ctrl
, a
, address
);
1872 static struct attribute_group nvme_dev_attrs_group
= {
1873 .attrs
= nvme_dev_attrs
,
1874 .is_visible
= nvme_dev_attrs_are_visible
,
1877 static const struct attribute_group
*nvme_dev_attr_groups
[] = {
1878 &nvme_dev_attrs_group
,
1882 static int ns_cmp(void *priv
, struct list_head
*a
, struct list_head
*b
)
1884 struct nvme_ns
*nsa
= container_of(a
, struct nvme_ns
, list
);
1885 struct nvme_ns
*nsb
= container_of(b
, struct nvme_ns
, list
);
1887 return nsa
->ns_id
- nsb
->ns_id
;
1890 static struct nvme_ns
*nvme_find_get_ns(struct nvme_ctrl
*ctrl
, unsigned nsid
)
1892 struct nvme_ns
*ns
, *ret
= NULL
;
1894 mutex_lock(&ctrl
->namespaces_mutex
);
1895 list_for_each_entry(ns
, &ctrl
->namespaces
, list
) {
1896 if (ns
->ns_id
== nsid
) {
1897 kref_get(&ns
->kref
);
1901 if (ns
->ns_id
> nsid
)
1904 mutex_unlock(&ctrl
->namespaces_mutex
);
1908 static void nvme_alloc_ns(struct nvme_ctrl
*ctrl
, unsigned nsid
)
1911 struct gendisk
*disk
;
1912 struct nvme_id_ns
*id
;
1913 char disk_name
[DISK_NAME_LEN
];
1914 int node
= dev_to_node(ctrl
->dev
);
1916 ns
= kzalloc_node(sizeof(*ns
), GFP_KERNEL
, node
);
1920 ns
->instance
= ida_simple_get(&ctrl
->ns_ida
, 1, 0, GFP_KERNEL
);
1921 if (ns
->instance
< 0)
1924 ns
->queue
= blk_mq_init_queue(ctrl
->tagset
);
1925 if (IS_ERR(ns
->queue
))
1926 goto out_release_instance
;
1927 queue_flag_set_unlocked(QUEUE_FLAG_NONROT
, ns
->queue
);
1928 ns
->queue
->queuedata
= ns
;
1931 kref_init(&ns
->kref
);
1933 ns
->lba_shift
= 9; /* set to a default value for 512 until disk is validated */
1935 blk_queue_logical_block_size(ns
->queue
, 1 << ns
->lba_shift
);
1936 nvme_set_queue_limits(ctrl
, ns
->queue
);
1938 sprintf(disk_name
, "nvme%dn%d", ctrl
->instance
, ns
->instance
);
1940 if (nvme_revalidate_ns(ns
, &id
))
1941 goto out_free_queue
;
1943 if (nvme_nvm_ns_supported(ns
, id
) &&
1944 nvme_nvm_register(ns
, disk_name
, node
)) {
1945 dev_warn(ctrl
->dev
, "%s: LightNVM init failure\n", __func__
);
1949 disk
= alloc_disk_node(0, node
);
1953 disk
->fops
= &nvme_fops
;
1954 disk
->private_data
= ns
;
1955 disk
->queue
= ns
->queue
;
1956 disk
->flags
= GENHD_FL_EXT_DEVT
;
1957 memcpy(disk
->disk_name
, disk_name
, DISK_NAME_LEN
);
1960 __nvme_revalidate_disk(disk
, id
);
1962 mutex_lock(&ctrl
->namespaces_mutex
);
1963 list_add_tail(&ns
->list
, &ctrl
->namespaces
);
1964 mutex_unlock(&ctrl
->namespaces_mutex
);
1966 kref_get(&ctrl
->kref
);
1970 device_add_disk(ctrl
->device
, ns
->disk
);
1971 if (sysfs_create_group(&disk_to_dev(ns
->disk
)->kobj
,
1972 &nvme_ns_attr_group
))
1973 pr_warn("%s: failed to create sysfs group for identification\n",
1974 ns
->disk
->disk_name
);
1975 if (ns
->ndev
&& nvme_nvm_register_sysfs(ns
))
1976 pr_warn("%s: failed to register lightnvm sysfs group for identification\n",
1977 ns
->disk
->disk_name
);
1982 blk_cleanup_queue(ns
->queue
);
1983 out_release_instance
:
1984 ida_simple_remove(&ctrl
->ns_ida
, ns
->instance
);
1989 static void nvme_ns_remove(struct nvme_ns
*ns
)
1991 if (test_and_set_bit(NVME_NS_REMOVING
, &ns
->flags
))
1994 if (ns
->disk
&& ns
->disk
->flags
& GENHD_FL_UP
) {
1995 if (blk_get_integrity(ns
->disk
))
1996 blk_integrity_unregister(ns
->disk
);
1997 sysfs_remove_group(&disk_to_dev(ns
->disk
)->kobj
,
1998 &nvme_ns_attr_group
);
2000 nvme_nvm_unregister_sysfs(ns
);
2001 del_gendisk(ns
->disk
);
2002 blk_mq_abort_requeue_list(ns
->queue
);
2003 blk_cleanup_queue(ns
->queue
);
2006 mutex_lock(&ns
->ctrl
->namespaces_mutex
);
2007 list_del_init(&ns
->list
);
2008 mutex_unlock(&ns
->ctrl
->namespaces_mutex
);
2013 static void nvme_validate_ns(struct nvme_ctrl
*ctrl
, unsigned nsid
)
2017 ns
= nvme_find_get_ns(ctrl
, nsid
);
2019 if (ns
->disk
&& revalidate_disk(ns
->disk
))
2023 nvme_alloc_ns(ctrl
, nsid
);
2026 static void nvme_remove_invalid_namespaces(struct nvme_ctrl
*ctrl
,
2029 struct nvme_ns
*ns
, *next
;
2031 list_for_each_entry_safe(ns
, next
, &ctrl
->namespaces
, list
) {
2032 if (ns
->ns_id
> nsid
)
2037 static int nvme_scan_ns_list(struct nvme_ctrl
*ctrl
, unsigned nn
)
2041 unsigned i
, j
, nsid
, prev
= 0, num_lists
= DIV_ROUND_UP(nn
, 1024);
2044 ns_list
= kzalloc(0x1000, GFP_KERNEL
);
2048 for (i
= 0; i
< num_lists
; i
++) {
2049 ret
= nvme_identify_ns_list(ctrl
, prev
, ns_list
);
2053 for (j
= 0; j
< min(nn
, 1024U); j
++) {
2054 nsid
= le32_to_cpu(ns_list
[j
]);
2058 nvme_validate_ns(ctrl
, nsid
);
2060 while (++prev
< nsid
) {
2061 ns
= nvme_find_get_ns(ctrl
, prev
);
2071 nvme_remove_invalid_namespaces(ctrl
, prev
);
2077 static void nvme_scan_ns_sequential(struct nvme_ctrl
*ctrl
, unsigned nn
)
2081 for (i
= 1; i
<= nn
; i
++)
2082 nvme_validate_ns(ctrl
, i
);
2084 nvme_remove_invalid_namespaces(ctrl
, nn
);
2087 static void nvme_scan_work(struct work_struct
*work
)
2089 struct nvme_ctrl
*ctrl
=
2090 container_of(work
, struct nvme_ctrl
, scan_work
);
2091 struct nvme_id_ctrl
*id
;
2094 if (ctrl
->state
!= NVME_CTRL_LIVE
)
2097 if (nvme_identify_ctrl(ctrl
, &id
))
2100 nn
= le32_to_cpu(id
->nn
);
2101 if (ctrl
->vs
>= NVME_VS(1, 1, 0) &&
2102 !(ctrl
->quirks
& NVME_QUIRK_IDENTIFY_CNS
)) {
2103 if (!nvme_scan_ns_list(ctrl
, nn
))
2106 nvme_scan_ns_sequential(ctrl
, nn
);
2108 mutex_lock(&ctrl
->namespaces_mutex
);
2109 list_sort(NULL
, &ctrl
->namespaces
, ns_cmp
);
2110 mutex_unlock(&ctrl
->namespaces_mutex
);
2114 void nvme_queue_scan(struct nvme_ctrl
*ctrl
)
2117 * Do not queue new scan work when a controller is reset during
2120 if (ctrl
->state
== NVME_CTRL_LIVE
)
2121 schedule_work(&ctrl
->scan_work
);
2123 EXPORT_SYMBOL_GPL(nvme_queue_scan
);
2126 * This function iterates the namespace list unlocked to allow recovery from
2127 * controller failure. It is up to the caller to ensure the namespace list is
2128 * not modified by scan work while this function is executing.
2130 void nvme_remove_namespaces(struct nvme_ctrl
*ctrl
)
2132 struct nvme_ns
*ns
, *next
;
2135 * The dead states indicates the controller was not gracefully
2136 * disconnected. In that case, we won't be able to flush any data while
2137 * removing the namespaces' disks; fail all the queues now to avoid
2138 * potentially having to clean up the failed sync later.
2140 if (ctrl
->state
== NVME_CTRL_DEAD
)
2141 nvme_kill_queues(ctrl
);
2143 list_for_each_entry_safe(ns
, next
, &ctrl
->namespaces
, list
)
2146 EXPORT_SYMBOL_GPL(nvme_remove_namespaces
);
2148 static void nvme_async_event_work(struct work_struct
*work
)
2150 struct nvme_ctrl
*ctrl
=
2151 container_of(work
, struct nvme_ctrl
, async_event_work
);
2153 spin_lock_irq(&ctrl
->lock
);
2154 while (ctrl
->event_limit
> 0) {
2155 int aer_idx
= --ctrl
->event_limit
;
2157 spin_unlock_irq(&ctrl
->lock
);
2158 ctrl
->ops
->submit_async_event(ctrl
, aer_idx
);
2159 spin_lock_irq(&ctrl
->lock
);
2161 spin_unlock_irq(&ctrl
->lock
);
2164 void nvme_complete_async_event(struct nvme_ctrl
*ctrl
, __le16 status
,
2165 union nvme_result
*res
)
2167 u32 result
= le32_to_cpu(res
->u32
);
2170 switch (le16_to_cpu(status
) >> 1) {
2171 case NVME_SC_SUCCESS
:
2174 case NVME_SC_ABORT_REQ
:
2175 ++ctrl
->event_limit
;
2176 schedule_work(&ctrl
->async_event_work
);
2185 switch (result
& 0xff07) {
2186 case NVME_AER_NOTICE_NS_CHANGED
:
2187 dev_info(ctrl
->device
, "rescanning\n");
2188 nvme_queue_scan(ctrl
);
2191 dev_warn(ctrl
->device
, "async event result %08x\n", result
);
2194 EXPORT_SYMBOL_GPL(nvme_complete_async_event
);
2196 void nvme_queue_async_events(struct nvme_ctrl
*ctrl
)
2198 ctrl
->event_limit
= NVME_NR_AERS
;
2199 schedule_work(&ctrl
->async_event_work
);
2201 EXPORT_SYMBOL_GPL(nvme_queue_async_events
);
2203 static DEFINE_IDA(nvme_instance_ida
);
2205 static int nvme_set_instance(struct nvme_ctrl
*ctrl
)
2207 int instance
, error
;
2210 if (!ida_pre_get(&nvme_instance_ida
, GFP_KERNEL
))
2213 spin_lock(&dev_list_lock
);
2214 error
= ida_get_new(&nvme_instance_ida
, &instance
);
2215 spin_unlock(&dev_list_lock
);
2216 } while (error
== -EAGAIN
);
2221 ctrl
->instance
= instance
;
2225 static void nvme_release_instance(struct nvme_ctrl
*ctrl
)
2227 spin_lock(&dev_list_lock
);
2228 ida_remove(&nvme_instance_ida
, ctrl
->instance
);
2229 spin_unlock(&dev_list_lock
);
2232 void nvme_uninit_ctrl(struct nvme_ctrl
*ctrl
)
2234 flush_work(&ctrl
->async_event_work
);
2235 flush_work(&ctrl
->scan_work
);
2236 nvme_remove_namespaces(ctrl
);
2238 device_destroy(nvme_class
, MKDEV(nvme_char_major
, ctrl
->instance
));
2240 spin_lock(&dev_list_lock
);
2241 list_del(&ctrl
->node
);
2242 spin_unlock(&dev_list_lock
);
2244 EXPORT_SYMBOL_GPL(nvme_uninit_ctrl
);
2246 static void nvme_free_ctrl(struct kref
*kref
)
2248 struct nvme_ctrl
*ctrl
= container_of(kref
, struct nvme_ctrl
, kref
);
2250 put_device(ctrl
->device
);
2251 nvme_release_instance(ctrl
);
2252 ida_destroy(&ctrl
->ns_ida
);
2254 ctrl
->ops
->free_ctrl(ctrl
);
2257 void nvme_put_ctrl(struct nvme_ctrl
*ctrl
)
2259 kref_put(&ctrl
->kref
, nvme_free_ctrl
);
2261 EXPORT_SYMBOL_GPL(nvme_put_ctrl
);
2264 * Initialize a NVMe controller structures. This needs to be called during
2265 * earliest initialization so that we have the initialized structured around
2268 int nvme_init_ctrl(struct nvme_ctrl
*ctrl
, struct device
*dev
,
2269 const struct nvme_ctrl_ops
*ops
, unsigned long quirks
)
2273 ctrl
->state
= NVME_CTRL_NEW
;
2274 spin_lock_init(&ctrl
->lock
);
2275 INIT_LIST_HEAD(&ctrl
->namespaces
);
2276 mutex_init(&ctrl
->namespaces_mutex
);
2277 kref_init(&ctrl
->kref
);
2280 ctrl
->quirks
= quirks
;
2281 INIT_WORK(&ctrl
->scan_work
, nvme_scan_work
);
2282 INIT_WORK(&ctrl
->async_event_work
, nvme_async_event_work
);
2284 ret
= nvme_set_instance(ctrl
);
2288 ctrl
->device
= device_create_with_groups(nvme_class
, ctrl
->dev
,
2289 MKDEV(nvme_char_major
, ctrl
->instance
),
2290 ctrl
, nvme_dev_attr_groups
,
2291 "nvme%d", ctrl
->instance
);
2292 if (IS_ERR(ctrl
->device
)) {
2293 ret
= PTR_ERR(ctrl
->device
);
2294 goto out_release_instance
;
2296 get_device(ctrl
->device
);
2297 ida_init(&ctrl
->ns_ida
);
2299 spin_lock(&dev_list_lock
);
2300 list_add_tail(&ctrl
->node
, &nvme_ctrl_list
);
2301 spin_unlock(&dev_list_lock
);
2304 * Initialize latency tolerance controls. The sysfs files won't
2305 * be visible to userspace unless the device actually supports APST.
2307 ctrl
->device
->power
.set_latency_tolerance
= nvme_set_latency_tolerance
;
2308 dev_pm_qos_update_user_latency_tolerance(ctrl
->device
,
2309 min(default_ps_max_latency_us
, (unsigned long)S32_MAX
));
2312 out_release_instance
:
2313 nvme_release_instance(ctrl
);
2317 EXPORT_SYMBOL_GPL(nvme_init_ctrl
);
2320 * nvme_kill_queues(): Ends all namespace queues
2321 * @ctrl: the dead controller that needs to end
2323 * Call this function when the driver determines it is unable to get the
2324 * controller in a state capable of servicing IO.
2326 void nvme_kill_queues(struct nvme_ctrl
*ctrl
)
2330 mutex_lock(&ctrl
->namespaces_mutex
);
2331 list_for_each_entry(ns
, &ctrl
->namespaces
, list
) {
2333 * Revalidating a dead namespace sets capacity to 0. This will
2334 * end buffered writers dirtying pages that can't be synced.
2336 if (!ns
->disk
|| test_and_set_bit(NVME_NS_DEAD
, &ns
->flags
))
2338 revalidate_disk(ns
->disk
);
2339 blk_set_queue_dying(ns
->queue
);
2340 blk_mq_abort_requeue_list(ns
->queue
);
2341 blk_mq_start_stopped_hw_queues(ns
->queue
, true);
2343 mutex_unlock(&ctrl
->namespaces_mutex
);
2345 EXPORT_SYMBOL_GPL(nvme_kill_queues
);
2347 void nvme_unfreeze(struct nvme_ctrl
*ctrl
)
2351 mutex_lock(&ctrl
->namespaces_mutex
);
2352 list_for_each_entry(ns
, &ctrl
->namespaces
, list
)
2353 blk_mq_unfreeze_queue(ns
->queue
);
2354 mutex_unlock(&ctrl
->namespaces_mutex
);
2356 EXPORT_SYMBOL_GPL(nvme_unfreeze
);
2358 void nvme_wait_freeze_timeout(struct nvme_ctrl
*ctrl
, long timeout
)
2362 mutex_lock(&ctrl
->namespaces_mutex
);
2363 list_for_each_entry(ns
, &ctrl
->namespaces
, list
) {
2364 timeout
= blk_mq_freeze_queue_wait_timeout(ns
->queue
, timeout
);
2368 mutex_unlock(&ctrl
->namespaces_mutex
);
2370 EXPORT_SYMBOL_GPL(nvme_wait_freeze_timeout
);
2372 void nvme_wait_freeze(struct nvme_ctrl
*ctrl
)
2376 mutex_lock(&ctrl
->namespaces_mutex
);
2377 list_for_each_entry(ns
, &ctrl
->namespaces
, list
)
2378 blk_mq_freeze_queue_wait(ns
->queue
);
2379 mutex_unlock(&ctrl
->namespaces_mutex
);
2381 EXPORT_SYMBOL_GPL(nvme_wait_freeze
);
2383 void nvme_start_freeze(struct nvme_ctrl
*ctrl
)
2387 mutex_lock(&ctrl
->namespaces_mutex
);
2388 list_for_each_entry(ns
, &ctrl
->namespaces
, list
)
2389 blk_freeze_queue_start(ns
->queue
);
2390 mutex_unlock(&ctrl
->namespaces_mutex
);
2392 EXPORT_SYMBOL_GPL(nvme_start_freeze
);
2394 void nvme_stop_queues(struct nvme_ctrl
*ctrl
)
2398 mutex_lock(&ctrl
->namespaces_mutex
);
2399 list_for_each_entry(ns
, &ctrl
->namespaces
, list
)
2400 blk_mq_quiesce_queue(ns
->queue
);
2401 mutex_unlock(&ctrl
->namespaces_mutex
);
2403 EXPORT_SYMBOL_GPL(nvme_stop_queues
);
2405 void nvme_start_queues(struct nvme_ctrl
*ctrl
)
2409 mutex_lock(&ctrl
->namespaces_mutex
);
2410 list_for_each_entry(ns
, &ctrl
->namespaces
, list
) {
2411 blk_mq_start_stopped_hw_queues(ns
->queue
, true);
2412 blk_mq_kick_requeue_list(ns
->queue
);
2414 mutex_unlock(&ctrl
->namespaces_mutex
);
2416 EXPORT_SYMBOL_GPL(nvme_start_queues
);
2418 int __init
nvme_core_init(void)
2422 result
= __register_chrdev(nvme_char_major
, 0, NVME_MINORS
, "nvme",
2426 else if (result
> 0)
2427 nvme_char_major
= result
;
2429 nvme_class
= class_create(THIS_MODULE
, "nvme");
2430 if (IS_ERR(nvme_class
)) {
2431 result
= PTR_ERR(nvme_class
);
2432 goto unregister_chrdev
;
2438 __unregister_chrdev(nvme_char_major
, 0, NVME_MINORS
, "nvme");
2442 void nvme_core_exit(void)
2444 class_destroy(nvme_class
);
2445 __unregister_chrdev(nvme_char_major
, 0, NVME_MINORS
, "nvme");
2448 MODULE_LICENSE("GPL");
2449 MODULE_VERSION("1.0");
2450 module_init(nvme_core_init
);
2451 module_exit(nvme_core_exit
);