This source file includes following definitions.
- nvme_set_queue_dying
- nvme_queue_scan
- nvme_try_sched_reset
- nvme_reset_ctrl
- nvme_reset_ctrl_sync
- nvme_do_delete_ctrl
- nvme_delete_ctrl_work
- nvme_delete_ctrl
- nvme_delete_ctrl_sync
- nvme_ns_has_pi
- nvme_error_status
- nvme_req_needs_retry
- nvme_retry_req
- nvme_complete_rq
- nvme_cancel_request
- nvme_change_ctrl_state
- nvme_state_terminal
- nvme_wait_reset
- nvme_free_ns_head
- nvme_put_ns_head
- nvme_free_ns
- nvme_put_ns
- nvme_clear_nvme_request
- nvme_alloc_request
- nvme_toggle_streams
- nvme_disable_streams
- nvme_enable_streams
- nvme_get_stream_params
- nvme_configure_directives
- nvme_assign_write_stream
- nvme_setup_flush
- nvme_setup_discard
- nvme_setup_write_zeroes
- nvme_setup_rw
- nvme_cleanup_cmd
- nvme_setup_cmd
- nvme_end_sync_rq
- nvme_execute_rq_polled
- __nvme_submit_sync_cmd
- nvme_submit_sync_cmd
- nvme_add_user_metadata
- nvme_submit_user_cmd
- nvme_keep_alive_end_io
- nvme_keep_alive
- nvme_keep_alive_work
- nvme_start_keep_alive
- nvme_stop_keep_alive
- nvme_identify_ctrl
- nvme_identify_ns_descs
- nvme_identify_ns_list
- nvme_identify_ns
- nvme_features
- nvme_set_features
- nvme_get_features
- nvme_set_queue_count
- nvme_enable_aen
- nvme_to_user_ptr
- nvme_submit_io
- nvme_known_admin_effects
- nvme_passthru_start
- nvme_update_formats
- nvme_passthru_end
- nvme_user_cmd
- nvme_user_cmd64
- nvme_get_ns_from_disk
- nvme_put_ns_from_disk
- is_ctrl_ioctl
- nvme_handle_ctrl_ioctl
- nvme_ioctl
- nvme_open
- nvme_release
- nvme_getgeo
- nvme_init_integrity
- nvme_init_integrity
- nvme_set_chunk_size
- nvme_config_discard
- nvme_config_write_zeroes
- nvme_report_ns_ids
- nvme_ns_ids_valid
- nvme_ns_ids_equal
- nvme_update_disk_info
- __nvme_revalidate_disk
- nvme_revalidate_disk
- nvme_pr_type
- nvme_pr_command
- nvme_pr_register
- nvme_pr_reserve
- nvme_pr_preempt
- nvme_pr_clear
- nvme_pr_release
- nvme_sec_submit
- nvme_ns_head_open
- nvme_ns_head_release
- nvme_wait_ready
- nvme_disable_ctrl
- nvme_enable_ctrl
- nvme_shutdown_ctrl
- nvme_set_queue_limits
- nvme_configure_timestamp
- nvme_configure_acre
- nvme_configure_apst
- nvme_set_latency_tolerance
- string_matches
- quirk_matches
- nvme_init_subnqn
- nvme_release_subsystem
- nvme_destroy_subsystem
- nvme_put_subsystem
- __nvme_find_get_subsystem
- nvme_subsys_show_nqn
- nvme_validate_cntlid
- nvme_init_subsystem
- nvme_get_log
- nvme_get_effects_log
- nvme_init_identify
- nvme_dev_open
- nvme_dev_user_cmd
- nvme_dev_ioctl
- nvme_sysfs_reset
- nvme_sysfs_rescan
- dev_to_ns_head
- wwid_show
- nguid_show
- uuid_show
- eui_show
- nsid_show
- nvme_ns_id_attrs_are_visible
- nvme_sysfs_delete
- nvme_sysfs_show_transport
- nvme_sysfs_show_state
- nvme_sysfs_show_subsysnqn
- nvme_sysfs_show_address
- nvme_dev_attrs_are_visible
- __nvme_find_ns_head
- __nvme_check_ids
- nvme_alloc_ns_head
- nvme_init_ns_head
- ns_cmp
- nvme_find_get_ns
- nvme_setup_streams_ns
- nvme_alloc_ns
- nvme_ns_remove
- nvme_validate_ns
- nvme_remove_invalid_namespaces
- nvme_scan_ns_list
- nvme_scan_ns_sequential
- nvme_clear_changed_ns_log
- nvme_scan_work
- nvme_remove_namespaces
- nvme_class_uevent
- nvme_aen_uevent
- nvme_async_event_work
- nvme_ctrl_pp_status
- nvme_get_fw_slot_info
- nvme_fw_act_work
- nvme_handle_aen_notice
- nvme_complete_async_event
- nvme_stop_ctrl
- nvme_start_ctrl
- nvme_uninit_ctrl
- nvme_free_ctrl
- nvme_init_ctrl
- nvme_kill_queues
- nvme_unfreeze
- nvme_wait_freeze_timeout
- nvme_wait_freeze
- nvme_start_freeze
- nvme_stop_queues
- nvme_start_queues
- nvme_sync_queues
- _nvme_check_size
- nvme_core_init
- nvme_core_exit
1
2
3
4
5
6
7 #include <linux/blkdev.h>
8 #include <linux/blk-mq.h>
9 #include <linux/compat.h>
10 #include <linux/delay.h>
11 #include <linux/errno.h>
12 #include <linux/hdreg.h>
13 #include <linux/kernel.h>
14 #include <linux/module.h>
15 #include <linux/backing-dev.h>
16 #include <linux/list_sort.h>
17 #include <linux/slab.h>
18 #include <linux/types.h>
19 #include <linux/pr.h>
20 #include <linux/ptrace.h>
21 #include <linux/nvme_ioctl.h>
22 #include <linux/t10-pi.h>
23 #include <linux/pm_qos.h>
24 #include <asm/unaligned.h>
25
26 #include "nvme.h"
27 #include "fabrics.h"
28
29 #define CREATE_TRACE_POINTS
30 #include "trace.h"
31
32 #define NVME_MINORS (1U << MINORBITS)
33
34 unsigned int admin_timeout = 60;
35 module_param(admin_timeout, uint, 0644);
36 MODULE_PARM_DESC(admin_timeout, "timeout in seconds for admin commands");
37 EXPORT_SYMBOL_GPL(admin_timeout);
38
39 unsigned int nvme_io_timeout = 30;
40 module_param_named(io_timeout, nvme_io_timeout, uint, 0644);
41 MODULE_PARM_DESC(io_timeout, "timeout in seconds for I/O");
42 EXPORT_SYMBOL_GPL(nvme_io_timeout);
43
44 static unsigned char shutdown_timeout = 5;
45 module_param(shutdown_timeout, byte, 0644);
46 MODULE_PARM_DESC(shutdown_timeout, "timeout in seconds for controller shutdown");
47
48 static u8 nvme_max_retries = 5;
49 module_param_named(max_retries, nvme_max_retries, byte, 0644);
50 MODULE_PARM_DESC(max_retries, "max number of retries a command may have");
51
52 static unsigned long default_ps_max_latency_us = 100000;
53 module_param(default_ps_max_latency_us, ulong, 0644);
54 MODULE_PARM_DESC(default_ps_max_latency_us,
55 "max power saving latency for new devices; use PM QOS to change per device");
56
57 static bool force_apst;
58 module_param(force_apst, bool, 0644);
59 MODULE_PARM_DESC(force_apst, "allow APST for newly enumerated devices even if quirked off");
60
61 static bool streams;
62 module_param(streams, bool, 0644);
63 MODULE_PARM_DESC(streams, "turn on support for Streams write directives");
64
65
66
67
68
69
70
71
72
73
74
75
76 struct workqueue_struct *nvme_wq;
77 EXPORT_SYMBOL_GPL(nvme_wq);
78
79 struct workqueue_struct *nvme_reset_wq;
80 EXPORT_SYMBOL_GPL(nvme_reset_wq);
81
82 struct workqueue_struct *nvme_delete_wq;
83 EXPORT_SYMBOL_GPL(nvme_delete_wq);
84
85 static LIST_HEAD(nvme_subsystems);
86 static DEFINE_MUTEX(nvme_subsystems_lock);
87
88 static DEFINE_IDA(nvme_instance_ida);
89 static dev_t nvme_chr_devt;
90 static struct class *nvme_class;
91 static struct class *nvme_subsys_class;
92
93 static int nvme_revalidate_disk(struct gendisk *disk);
94 static void nvme_put_subsystem(struct nvme_subsystem *subsys);
95 static void nvme_remove_invalid_namespaces(struct nvme_ctrl *ctrl,
96 unsigned nsid);
97
98 static void nvme_set_queue_dying(struct nvme_ns *ns)
99 {
100
101
102
103
104 if (!ns->disk || test_and_set_bit(NVME_NS_DEAD, &ns->flags))
105 return;
106 blk_set_queue_dying(ns->queue);
107
108 blk_mq_unquiesce_queue(ns->queue);
109
110
111
112 revalidate_disk(ns->disk);
113 }
114
115 static void nvme_queue_scan(struct nvme_ctrl *ctrl)
116 {
117
118
119
120 if (ctrl->state == NVME_CTRL_LIVE && ctrl->tagset)
121 queue_work(nvme_wq, &ctrl->scan_work);
122 }
123
124
125
126
127
128
129
130 int nvme_try_sched_reset(struct nvme_ctrl *ctrl)
131 {
132 if (ctrl->state != NVME_CTRL_RESETTING)
133 return -EBUSY;
134 if (!queue_work(nvme_reset_wq, &ctrl->reset_work))
135 return -EBUSY;
136 return 0;
137 }
138 EXPORT_SYMBOL_GPL(nvme_try_sched_reset);
139
140 int nvme_reset_ctrl(struct nvme_ctrl *ctrl)
141 {
142 if (!nvme_change_ctrl_state(ctrl, NVME_CTRL_RESETTING))
143 return -EBUSY;
144 if (!queue_work(nvme_reset_wq, &ctrl->reset_work))
145 return -EBUSY;
146 return 0;
147 }
148 EXPORT_SYMBOL_GPL(nvme_reset_ctrl);
149
150 int nvme_reset_ctrl_sync(struct nvme_ctrl *ctrl)
151 {
152 int ret;
153
154 ret = nvme_reset_ctrl(ctrl);
155 if (!ret) {
156 flush_work(&ctrl->reset_work);
157 if (ctrl->state != NVME_CTRL_LIVE)
158 ret = -ENETRESET;
159 }
160
161 return ret;
162 }
163 EXPORT_SYMBOL_GPL(nvme_reset_ctrl_sync);
164
165 static void nvme_do_delete_ctrl(struct nvme_ctrl *ctrl)
166 {
167 dev_info(ctrl->device,
168 "Removing ctrl: NQN \"%s\"\n", ctrl->opts->subsysnqn);
169
170 flush_work(&ctrl->reset_work);
171 nvme_stop_ctrl(ctrl);
172 nvme_remove_namespaces(ctrl);
173 ctrl->ops->delete_ctrl(ctrl);
174 nvme_uninit_ctrl(ctrl);
175 nvme_put_ctrl(ctrl);
176 }
177
178 static void nvme_delete_ctrl_work(struct work_struct *work)
179 {
180 struct nvme_ctrl *ctrl =
181 container_of(work, struct nvme_ctrl, delete_work);
182
183 nvme_do_delete_ctrl(ctrl);
184 }
185
186 int nvme_delete_ctrl(struct nvme_ctrl *ctrl)
187 {
188 if (!nvme_change_ctrl_state(ctrl, NVME_CTRL_DELETING))
189 return -EBUSY;
190 if (!queue_work(nvme_delete_wq, &ctrl->delete_work))
191 return -EBUSY;
192 return 0;
193 }
194 EXPORT_SYMBOL_GPL(nvme_delete_ctrl);
195
196 static int nvme_delete_ctrl_sync(struct nvme_ctrl *ctrl)
197 {
198 int ret = 0;
199
200
201
202
203
204 nvme_get_ctrl(ctrl);
205 if (!nvme_change_ctrl_state(ctrl, NVME_CTRL_DELETING))
206 ret = -EBUSY;
207 if (!ret)
208 nvme_do_delete_ctrl(ctrl);
209 nvme_put_ctrl(ctrl);
210 return ret;
211 }
212
213 static inline bool nvme_ns_has_pi(struct nvme_ns *ns)
214 {
215 return ns->pi_type && ns->ms == sizeof(struct t10_pi_tuple);
216 }
217
218 static blk_status_t nvme_error_status(u16 status)
219 {
220 switch (status & 0x7ff) {
221 case NVME_SC_SUCCESS:
222 return BLK_STS_OK;
223 case NVME_SC_CAP_EXCEEDED:
224 return BLK_STS_NOSPC;
225 case NVME_SC_LBA_RANGE:
226 return BLK_STS_TARGET;
227 case NVME_SC_BAD_ATTRIBUTES:
228 case NVME_SC_ONCS_NOT_SUPPORTED:
229 case NVME_SC_INVALID_OPCODE:
230 case NVME_SC_INVALID_FIELD:
231 case NVME_SC_INVALID_NS:
232 return BLK_STS_NOTSUPP;
233 case NVME_SC_WRITE_FAULT:
234 case NVME_SC_READ_ERROR:
235 case NVME_SC_UNWRITTEN_BLOCK:
236 case NVME_SC_ACCESS_DENIED:
237 case NVME_SC_READ_ONLY:
238 case NVME_SC_COMPARE_FAILED:
239 return BLK_STS_MEDIUM;
240 case NVME_SC_GUARD_CHECK:
241 case NVME_SC_APPTAG_CHECK:
242 case NVME_SC_REFTAG_CHECK:
243 case NVME_SC_INVALID_PI:
244 return BLK_STS_PROTECTION;
245 case NVME_SC_RESERVATION_CONFLICT:
246 return BLK_STS_NEXUS;
247 case NVME_SC_HOST_PATH_ERROR:
248 return BLK_STS_TRANSPORT;
249 default:
250 return BLK_STS_IOERR;
251 }
252 }
253
254 static inline bool nvme_req_needs_retry(struct request *req)
255 {
256 if (blk_noretry_request(req))
257 return false;
258 if (nvme_req(req)->status & NVME_SC_DNR)
259 return false;
260 if (nvme_req(req)->retries >= nvme_max_retries)
261 return false;
262 return true;
263 }
264
265 static void nvme_retry_req(struct request *req)
266 {
267 struct nvme_ns *ns = req->q->queuedata;
268 unsigned long delay = 0;
269 u16 crd;
270
271
272 crd = (nvme_req(req)->status & NVME_SC_CRD) >> 11;
273 if (ns && crd)
274 delay = ns->ctrl->crdt[crd - 1] * 100;
275
276 nvme_req(req)->retries++;
277 blk_mq_requeue_request(req, false);
278 blk_mq_delay_kick_requeue_list(req->q, delay);
279 }
280
281 void nvme_complete_rq(struct request *req)
282 {
283 blk_status_t status = nvme_error_status(nvme_req(req)->status);
284
285 trace_nvme_complete_rq(req);
286
287 if (nvme_req(req)->ctrl->kas)
288 nvme_req(req)->ctrl->comp_seen = true;
289
290 if (unlikely(status != BLK_STS_OK && nvme_req_needs_retry(req))) {
291 if ((req->cmd_flags & REQ_NVME_MPATH) &&
292 blk_path_error(status)) {
293 nvme_failover_req(req);
294 return;
295 }
296
297 if (!blk_queue_dying(req->q)) {
298 nvme_retry_req(req);
299 return;
300 }
301 }
302
303 nvme_trace_bio_complete(req, status);
304 blk_mq_end_request(req, status);
305 }
306 EXPORT_SYMBOL_GPL(nvme_complete_rq);
307
308 bool nvme_cancel_request(struct request *req, void *data, bool reserved)
309 {
310 dev_dbg_ratelimited(((struct nvme_ctrl *) data)->device,
311 "Cancelling I/O %d", req->tag);
312
313
314 if (blk_mq_request_completed(req))
315 return true;
316
317 nvme_req(req)->status = NVME_SC_HOST_ABORTED_CMD;
318 blk_mq_complete_request(req);
319 return true;
320 }
321 EXPORT_SYMBOL_GPL(nvme_cancel_request);
322
323 bool nvme_change_ctrl_state(struct nvme_ctrl *ctrl,
324 enum nvme_ctrl_state new_state)
325 {
326 enum nvme_ctrl_state old_state;
327 unsigned long flags;
328 bool changed = false;
329
330 spin_lock_irqsave(&ctrl->lock, flags);
331
332 old_state = ctrl->state;
333 switch (new_state) {
334 case NVME_CTRL_LIVE:
335 switch (old_state) {
336 case NVME_CTRL_NEW:
337 case NVME_CTRL_RESETTING:
338 case NVME_CTRL_CONNECTING:
339 changed = true;
340
341 default:
342 break;
343 }
344 break;
345 case NVME_CTRL_RESETTING:
346 switch (old_state) {
347 case NVME_CTRL_NEW:
348 case NVME_CTRL_LIVE:
349 changed = true;
350
351 default:
352 break;
353 }
354 break;
355 case NVME_CTRL_CONNECTING:
356 switch (old_state) {
357 case NVME_CTRL_NEW:
358 case NVME_CTRL_RESETTING:
359 changed = true;
360
361 default:
362 break;
363 }
364 break;
365 case NVME_CTRL_DELETING:
366 switch (old_state) {
367 case NVME_CTRL_LIVE:
368 case NVME_CTRL_RESETTING:
369 case NVME_CTRL_CONNECTING:
370 changed = true;
371
372 default:
373 break;
374 }
375 break;
376 case NVME_CTRL_DEAD:
377 switch (old_state) {
378 case NVME_CTRL_DELETING:
379 changed = true;
380
381 default:
382 break;
383 }
384 break;
385 default:
386 break;
387 }
388
389 if (changed) {
390 ctrl->state = new_state;
391 wake_up_all(&ctrl->state_wq);
392 }
393
394 spin_unlock_irqrestore(&ctrl->lock, flags);
395 if (changed && ctrl->state == NVME_CTRL_LIVE)
396 nvme_kick_requeue_lists(ctrl);
397 return changed;
398 }
399 EXPORT_SYMBOL_GPL(nvme_change_ctrl_state);
400
401
402
403
404 static bool nvme_state_terminal(struct nvme_ctrl *ctrl)
405 {
406 switch (ctrl->state) {
407 case NVME_CTRL_NEW:
408 case NVME_CTRL_LIVE:
409 case NVME_CTRL_RESETTING:
410 case NVME_CTRL_CONNECTING:
411 return false;
412 case NVME_CTRL_DELETING:
413 case NVME_CTRL_DEAD:
414 return true;
415 default:
416 WARN_ONCE(1, "Unhandled ctrl state:%d", ctrl->state);
417 return true;
418 }
419 }
420
421
422
423
424
425 bool nvme_wait_reset(struct nvme_ctrl *ctrl)
426 {
427 wait_event(ctrl->state_wq,
428 nvme_change_ctrl_state(ctrl, NVME_CTRL_RESETTING) ||
429 nvme_state_terminal(ctrl));
430 return ctrl->state == NVME_CTRL_RESETTING;
431 }
432 EXPORT_SYMBOL_GPL(nvme_wait_reset);
433
434 static void nvme_free_ns_head(struct kref *ref)
435 {
436 struct nvme_ns_head *head =
437 container_of(ref, struct nvme_ns_head, ref);
438
439 nvme_mpath_remove_disk(head);
440 ida_simple_remove(&head->subsys->ns_ida, head->instance);
441 list_del_init(&head->entry);
442 cleanup_srcu_struct(&head->srcu);
443 nvme_put_subsystem(head->subsys);
444 kfree(head);
445 }
446
447 static void nvme_put_ns_head(struct nvme_ns_head *head)
448 {
449 kref_put(&head->ref, nvme_free_ns_head);
450 }
451
452 static void nvme_free_ns(struct kref *kref)
453 {
454 struct nvme_ns *ns = container_of(kref, struct nvme_ns, kref);
455
456 if (ns->ndev)
457 nvme_nvm_unregister(ns);
458
459 put_disk(ns->disk);
460 nvme_put_ns_head(ns->head);
461 nvme_put_ctrl(ns->ctrl);
462 kfree(ns);
463 }
464
465 static void nvme_put_ns(struct nvme_ns *ns)
466 {
467 kref_put(&ns->kref, nvme_free_ns);
468 }
469
470 static inline void nvme_clear_nvme_request(struct request *req)
471 {
472 if (!(req->rq_flags & RQF_DONTPREP)) {
473 nvme_req(req)->retries = 0;
474 nvme_req(req)->flags = 0;
475 req->rq_flags |= RQF_DONTPREP;
476 }
477 }
478
479 struct request *nvme_alloc_request(struct request_queue *q,
480 struct nvme_command *cmd, blk_mq_req_flags_t flags, int qid)
481 {
482 unsigned op = nvme_is_write(cmd) ? REQ_OP_DRV_OUT : REQ_OP_DRV_IN;
483 struct request *req;
484
485 if (qid == NVME_QID_ANY) {
486 req = blk_mq_alloc_request(q, op, flags);
487 } else {
488 req = blk_mq_alloc_request_hctx(q, op, flags,
489 qid ? qid - 1 : 0);
490 }
491 if (IS_ERR(req))
492 return req;
493
494 req->cmd_flags |= REQ_FAILFAST_DRIVER;
495 nvme_clear_nvme_request(req);
496 nvme_req(req)->cmd = cmd;
497
498 return req;
499 }
500 EXPORT_SYMBOL_GPL(nvme_alloc_request);
501
502 static int nvme_toggle_streams(struct nvme_ctrl *ctrl, bool enable)
503 {
504 struct nvme_command c;
505
506 memset(&c, 0, sizeof(c));
507
508 c.directive.opcode = nvme_admin_directive_send;
509 c.directive.nsid = cpu_to_le32(NVME_NSID_ALL);
510 c.directive.doper = NVME_DIR_SND_ID_OP_ENABLE;
511 c.directive.dtype = NVME_DIR_IDENTIFY;
512 c.directive.tdtype = NVME_DIR_STREAMS;
513 c.directive.endir = enable ? NVME_DIR_ENDIR : 0;
514
515 return nvme_submit_sync_cmd(ctrl->admin_q, &c, NULL, 0);
516 }
517
518 static int nvme_disable_streams(struct nvme_ctrl *ctrl)
519 {
520 return nvme_toggle_streams(ctrl, false);
521 }
522
523 static int nvme_enable_streams(struct nvme_ctrl *ctrl)
524 {
525 return nvme_toggle_streams(ctrl, true);
526 }
527
528 static int nvme_get_stream_params(struct nvme_ctrl *ctrl,
529 struct streams_directive_params *s, u32 nsid)
530 {
531 struct nvme_command c;
532
533 memset(&c, 0, sizeof(c));
534 memset(s, 0, sizeof(*s));
535
536 c.directive.opcode = nvme_admin_directive_recv;
537 c.directive.nsid = cpu_to_le32(nsid);
538 c.directive.numd = cpu_to_le32((sizeof(*s) >> 2) - 1);
539 c.directive.doper = NVME_DIR_RCV_ST_OP_PARAM;
540 c.directive.dtype = NVME_DIR_STREAMS;
541
542 return nvme_submit_sync_cmd(ctrl->admin_q, &c, s, sizeof(*s));
543 }
544
545 static int nvme_configure_directives(struct nvme_ctrl *ctrl)
546 {
547 struct streams_directive_params s;
548 int ret;
549
550 if (!(ctrl->oacs & NVME_CTRL_OACS_DIRECTIVES))
551 return 0;
552 if (!streams)
553 return 0;
554
555 ret = nvme_enable_streams(ctrl);
556 if (ret)
557 return ret;
558
559 ret = nvme_get_stream_params(ctrl, &s, NVME_NSID_ALL);
560 if (ret)
561 return ret;
562
563 ctrl->nssa = le16_to_cpu(s.nssa);
564 if (ctrl->nssa < BLK_MAX_WRITE_HINTS - 1) {
565 dev_info(ctrl->device, "too few streams (%u) available\n",
566 ctrl->nssa);
567 nvme_disable_streams(ctrl);
568 return 0;
569 }
570
571 ctrl->nr_streams = min_t(unsigned, ctrl->nssa, BLK_MAX_WRITE_HINTS - 1);
572 dev_info(ctrl->device, "Using %u streams\n", ctrl->nr_streams);
573 return 0;
574 }
575
576
577
578
579
580 static void nvme_assign_write_stream(struct nvme_ctrl *ctrl,
581 struct request *req, u16 *control,
582 u32 *dsmgmt)
583 {
584 enum rw_hint streamid = req->write_hint;
585
586 if (streamid == WRITE_LIFE_NOT_SET || streamid == WRITE_LIFE_NONE)
587 streamid = 0;
588 else {
589 streamid--;
590 if (WARN_ON_ONCE(streamid > ctrl->nr_streams))
591 return;
592
593 *control |= NVME_RW_DTYPE_STREAMS;
594 *dsmgmt |= streamid << 16;
595 }
596
597 if (streamid < ARRAY_SIZE(req->q->write_hints))
598 req->q->write_hints[streamid] += blk_rq_bytes(req) >> 9;
599 }
600
601 static inline void nvme_setup_flush(struct nvme_ns *ns,
602 struct nvme_command *cmnd)
603 {
604 cmnd->common.opcode = nvme_cmd_flush;
605 cmnd->common.nsid = cpu_to_le32(ns->head->ns_id);
606 }
607
608 static blk_status_t nvme_setup_discard(struct nvme_ns *ns, struct request *req,
609 struct nvme_command *cmnd)
610 {
611 unsigned short segments = blk_rq_nr_discard_segments(req), n = 0;
612 struct nvme_dsm_range *range;
613 struct bio *bio;
614
615
616
617
618
619
620 static const size_t alloc_size = sizeof(*range) * NVME_DSM_MAX_RANGES;
621
622 range = kzalloc(alloc_size, GFP_ATOMIC | __GFP_NOWARN);
623 if (!range) {
624
625
626
627
628
629 if (test_and_set_bit_lock(0, &ns->ctrl->discard_page_busy))
630 return BLK_STS_RESOURCE;
631
632 range = page_address(ns->ctrl->discard_page);
633 }
634
635 __rq_for_each_bio(bio, req) {
636 u64 slba = nvme_block_nr(ns, bio->bi_iter.bi_sector);
637 u32 nlb = bio->bi_iter.bi_size >> ns->lba_shift;
638
639 if (n < segments) {
640 range[n].cattr = cpu_to_le32(0);
641 range[n].nlb = cpu_to_le32(nlb);
642 range[n].slba = cpu_to_le64(slba);
643 }
644 n++;
645 }
646
647 if (WARN_ON_ONCE(n != segments)) {
648 if (virt_to_page(range) == ns->ctrl->discard_page)
649 clear_bit_unlock(0, &ns->ctrl->discard_page_busy);
650 else
651 kfree(range);
652 return BLK_STS_IOERR;
653 }
654
655 cmnd->dsm.opcode = nvme_cmd_dsm;
656 cmnd->dsm.nsid = cpu_to_le32(ns->head->ns_id);
657 cmnd->dsm.nr = cpu_to_le32(segments - 1);
658 cmnd->dsm.attributes = cpu_to_le32(NVME_DSMGMT_AD);
659
660 req->special_vec.bv_page = virt_to_page(range);
661 req->special_vec.bv_offset = offset_in_page(range);
662 req->special_vec.bv_len = alloc_size;
663 req->rq_flags |= RQF_SPECIAL_PAYLOAD;
664
665 return BLK_STS_OK;
666 }
667
668 static inline blk_status_t nvme_setup_write_zeroes(struct nvme_ns *ns,
669 struct request *req, struct nvme_command *cmnd)
670 {
671 if (ns->ctrl->quirks & NVME_QUIRK_DEALLOCATE_ZEROES)
672 return nvme_setup_discard(ns, req, cmnd);
673
674 cmnd->write_zeroes.opcode = nvme_cmd_write_zeroes;
675 cmnd->write_zeroes.nsid = cpu_to_le32(ns->head->ns_id);
676 cmnd->write_zeroes.slba =
677 cpu_to_le64(nvme_block_nr(ns, blk_rq_pos(req)));
678 cmnd->write_zeroes.length =
679 cpu_to_le16((blk_rq_bytes(req) >> ns->lba_shift) - 1);
680 cmnd->write_zeroes.control = 0;
681 return BLK_STS_OK;
682 }
683
684 static inline blk_status_t nvme_setup_rw(struct nvme_ns *ns,
685 struct request *req, struct nvme_command *cmnd)
686 {
687 struct nvme_ctrl *ctrl = ns->ctrl;
688 u16 control = 0;
689 u32 dsmgmt = 0;
690
691 if (req->cmd_flags & REQ_FUA)
692 control |= NVME_RW_FUA;
693 if (req->cmd_flags & (REQ_FAILFAST_DEV | REQ_RAHEAD))
694 control |= NVME_RW_LR;
695
696 if (req->cmd_flags & REQ_RAHEAD)
697 dsmgmt |= NVME_RW_DSM_FREQ_PREFETCH;
698
699 cmnd->rw.opcode = (rq_data_dir(req) ? nvme_cmd_write : nvme_cmd_read);
700 cmnd->rw.nsid = cpu_to_le32(ns->head->ns_id);
701 cmnd->rw.slba = cpu_to_le64(nvme_block_nr(ns, blk_rq_pos(req)));
702 cmnd->rw.length = cpu_to_le16((blk_rq_bytes(req) >> ns->lba_shift) - 1);
703
704 if (req_op(req) == REQ_OP_WRITE && ctrl->nr_streams)
705 nvme_assign_write_stream(ctrl, req, &control, &dsmgmt);
706
707 if (ns->ms) {
708
709
710
711
712
713
714 if (!blk_integrity_rq(req)) {
715 if (WARN_ON_ONCE(!nvme_ns_has_pi(ns)))
716 return BLK_STS_NOTSUPP;
717 control |= NVME_RW_PRINFO_PRACT;
718 }
719
720 switch (ns->pi_type) {
721 case NVME_NS_DPS_PI_TYPE3:
722 control |= NVME_RW_PRINFO_PRCHK_GUARD;
723 break;
724 case NVME_NS_DPS_PI_TYPE1:
725 case NVME_NS_DPS_PI_TYPE2:
726 control |= NVME_RW_PRINFO_PRCHK_GUARD |
727 NVME_RW_PRINFO_PRCHK_REF;
728 cmnd->rw.reftag = cpu_to_le32(t10_pi_ref_tag(req));
729 break;
730 }
731 }
732
733 cmnd->rw.control = cpu_to_le16(control);
734 cmnd->rw.dsmgmt = cpu_to_le32(dsmgmt);
735 return 0;
736 }
737
738 void nvme_cleanup_cmd(struct request *req)
739 {
740 if (req->rq_flags & RQF_SPECIAL_PAYLOAD) {
741 struct nvme_ns *ns = req->rq_disk->private_data;
742 struct page *page = req->special_vec.bv_page;
743
744 if (page == ns->ctrl->discard_page)
745 clear_bit_unlock(0, &ns->ctrl->discard_page_busy);
746 else
747 kfree(page_address(page) + req->special_vec.bv_offset);
748 }
749 }
750 EXPORT_SYMBOL_GPL(nvme_cleanup_cmd);
751
752 blk_status_t nvme_setup_cmd(struct nvme_ns *ns, struct request *req,
753 struct nvme_command *cmd)
754 {
755 blk_status_t ret = BLK_STS_OK;
756
757 nvme_clear_nvme_request(req);
758
759 memset(cmd, 0, sizeof(*cmd));
760 switch (req_op(req)) {
761 case REQ_OP_DRV_IN:
762 case REQ_OP_DRV_OUT:
763 memcpy(cmd, nvme_req(req)->cmd, sizeof(*cmd));
764 break;
765 case REQ_OP_FLUSH:
766 nvme_setup_flush(ns, cmd);
767 break;
768 case REQ_OP_WRITE_ZEROES:
769 ret = nvme_setup_write_zeroes(ns, req, cmd);
770 break;
771 case REQ_OP_DISCARD:
772 ret = nvme_setup_discard(ns, req, cmd);
773 break;
774 case REQ_OP_READ:
775 case REQ_OP_WRITE:
776 ret = nvme_setup_rw(ns, req, cmd);
777 break;
778 default:
779 WARN_ON_ONCE(1);
780 return BLK_STS_IOERR;
781 }
782
783 cmd->common.command_id = req->tag;
784 trace_nvme_setup_cmd(req, cmd);
785 return ret;
786 }
787 EXPORT_SYMBOL_GPL(nvme_setup_cmd);
788
789 static void nvme_end_sync_rq(struct request *rq, blk_status_t error)
790 {
791 struct completion *waiting = rq->end_io_data;
792
793 rq->end_io_data = NULL;
794 complete(waiting);
795 }
796
797 static void nvme_execute_rq_polled(struct request_queue *q,
798 struct gendisk *bd_disk, struct request *rq, int at_head)
799 {
800 DECLARE_COMPLETION_ONSTACK(wait);
801
802 WARN_ON_ONCE(!test_bit(QUEUE_FLAG_POLL, &q->queue_flags));
803
804 rq->cmd_flags |= REQ_HIPRI;
805 rq->end_io_data = &wait;
806 blk_execute_rq_nowait(q, bd_disk, rq, at_head, nvme_end_sync_rq);
807
808 while (!completion_done(&wait)) {
809 blk_poll(q, request_to_qc_t(rq->mq_hctx, rq), true);
810 cond_resched();
811 }
812 }
813
814
815
816
817
818 int __nvme_submit_sync_cmd(struct request_queue *q, struct nvme_command *cmd,
819 union nvme_result *result, void *buffer, unsigned bufflen,
820 unsigned timeout, int qid, int at_head,
821 blk_mq_req_flags_t flags, bool poll)
822 {
823 struct request *req;
824 int ret;
825
826 req = nvme_alloc_request(q, cmd, flags, qid);
827 if (IS_ERR(req))
828 return PTR_ERR(req);
829
830 req->timeout = timeout ? timeout : ADMIN_TIMEOUT;
831
832 if (buffer && bufflen) {
833 ret = blk_rq_map_kern(q, req, buffer, bufflen, GFP_KERNEL);
834 if (ret)
835 goto out;
836 }
837
838 if (poll)
839 nvme_execute_rq_polled(req->q, NULL, req, at_head);
840 else
841 blk_execute_rq(req->q, NULL, req, at_head);
842 if (result)
843 *result = nvme_req(req)->result;
844 if (nvme_req(req)->flags & NVME_REQ_CANCELLED)
845 ret = -EINTR;
846 else
847 ret = nvme_req(req)->status;
848 out:
849 blk_mq_free_request(req);
850 return ret;
851 }
852 EXPORT_SYMBOL_GPL(__nvme_submit_sync_cmd);
853
854 int nvme_submit_sync_cmd(struct request_queue *q, struct nvme_command *cmd,
855 void *buffer, unsigned bufflen)
856 {
857 return __nvme_submit_sync_cmd(q, cmd, NULL, buffer, bufflen, 0,
858 NVME_QID_ANY, 0, 0, false);
859 }
860 EXPORT_SYMBOL_GPL(nvme_submit_sync_cmd);
861
862 static void *nvme_add_user_metadata(struct bio *bio, void __user *ubuf,
863 unsigned len, u32 seed, bool write)
864 {
865 struct bio_integrity_payload *bip;
866 int ret = -ENOMEM;
867 void *buf;
868
869 buf = kmalloc(len, GFP_KERNEL);
870 if (!buf)
871 goto out;
872
873 ret = -EFAULT;
874 if (write && copy_from_user(buf, ubuf, len))
875 goto out_free_meta;
876
877 bip = bio_integrity_alloc(bio, GFP_KERNEL, 1);
878 if (IS_ERR(bip)) {
879 ret = PTR_ERR(bip);
880 goto out_free_meta;
881 }
882
883 bip->bip_iter.bi_size = len;
884 bip->bip_iter.bi_sector = seed;
885 ret = bio_integrity_add_page(bio, virt_to_page(buf), len,
886 offset_in_page(buf));
887 if (ret == len)
888 return buf;
889 ret = -ENOMEM;
890 out_free_meta:
891 kfree(buf);
892 out:
893 return ERR_PTR(ret);
894 }
895
896 static int nvme_submit_user_cmd(struct request_queue *q,
897 struct nvme_command *cmd, void __user *ubuffer,
898 unsigned bufflen, void __user *meta_buffer, unsigned meta_len,
899 u32 meta_seed, u64 *result, unsigned timeout)
900 {
901 bool write = nvme_is_write(cmd);
902 struct nvme_ns *ns = q->queuedata;
903 struct gendisk *disk = ns ? ns->disk : NULL;
904 struct request *req;
905 struct bio *bio = NULL;
906 void *meta = NULL;
907 int ret;
908
909 req = nvme_alloc_request(q, cmd, 0, NVME_QID_ANY);
910 if (IS_ERR(req))
911 return PTR_ERR(req);
912
913 req->timeout = timeout ? timeout : ADMIN_TIMEOUT;
914 nvme_req(req)->flags |= NVME_REQ_USERCMD;
915
916 if (ubuffer && bufflen) {
917 ret = blk_rq_map_user(q, req, NULL, ubuffer, bufflen,
918 GFP_KERNEL);
919 if (ret)
920 goto out;
921 bio = req->bio;
922 bio->bi_disk = disk;
923 if (disk && meta_buffer && meta_len) {
924 meta = nvme_add_user_metadata(bio, meta_buffer, meta_len,
925 meta_seed, write);
926 if (IS_ERR(meta)) {
927 ret = PTR_ERR(meta);
928 goto out_unmap;
929 }
930 req->cmd_flags |= REQ_INTEGRITY;
931 }
932 }
933
934 blk_execute_rq(req->q, disk, req, 0);
935 if (nvme_req(req)->flags & NVME_REQ_CANCELLED)
936 ret = -EINTR;
937 else
938 ret = nvme_req(req)->status;
939 if (result)
940 *result = le64_to_cpu(nvme_req(req)->result.u64);
941 if (meta && !ret && !write) {
942 if (copy_to_user(meta_buffer, meta, meta_len))
943 ret = -EFAULT;
944 }
945 kfree(meta);
946 out_unmap:
947 if (bio)
948 blk_rq_unmap_user(bio);
949 out:
950 blk_mq_free_request(req);
951 return ret;
952 }
953
954 static void nvme_keep_alive_end_io(struct request *rq, blk_status_t status)
955 {
956 struct nvme_ctrl *ctrl = rq->end_io_data;
957 unsigned long flags;
958 bool startka = false;
959
960 blk_mq_free_request(rq);
961
962 if (status) {
963 dev_err(ctrl->device,
964 "failed nvme_keep_alive_end_io error=%d\n",
965 status);
966 return;
967 }
968
969 ctrl->comp_seen = false;
970 spin_lock_irqsave(&ctrl->lock, flags);
971 if (ctrl->state == NVME_CTRL_LIVE ||
972 ctrl->state == NVME_CTRL_CONNECTING)
973 startka = true;
974 spin_unlock_irqrestore(&ctrl->lock, flags);
975 if (startka)
976 queue_delayed_work(nvme_wq, &ctrl->ka_work, ctrl->kato * HZ);
977 }
978
979 static int nvme_keep_alive(struct nvme_ctrl *ctrl)
980 {
981 struct request *rq;
982
983 rq = nvme_alloc_request(ctrl->admin_q, &ctrl->ka_cmd, BLK_MQ_REQ_RESERVED,
984 NVME_QID_ANY);
985 if (IS_ERR(rq))
986 return PTR_ERR(rq);
987
988 rq->timeout = ctrl->kato * HZ;
989 rq->end_io_data = ctrl;
990
991 blk_execute_rq_nowait(rq->q, NULL, rq, 0, nvme_keep_alive_end_io);
992
993 return 0;
994 }
995
996 static void nvme_keep_alive_work(struct work_struct *work)
997 {
998 struct nvme_ctrl *ctrl = container_of(to_delayed_work(work),
999 struct nvme_ctrl, ka_work);
1000 bool comp_seen = ctrl->comp_seen;
1001
1002 if ((ctrl->ctratt & NVME_CTRL_ATTR_TBKAS) && comp_seen) {
1003 dev_dbg(ctrl->device,
1004 "reschedule traffic based keep-alive timer\n");
1005 ctrl->comp_seen = false;
1006 queue_delayed_work(nvme_wq, &ctrl->ka_work, ctrl->kato * HZ);
1007 return;
1008 }
1009
1010 if (nvme_keep_alive(ctrl)) {
1011
1012 dev_err(ctrl->device, "keep-alive failed\n");
1013 nvme_reset_ctrl(ctrl);
1014 return;
1015 }
1016 }
1017
1018 static void nvme_start_keep_alive(struct nvme_ctrl *ctrl)
1019 {
1020 if (unlikely(ctrl->kato == 0))
1021 return;
1022
1023 queue_delayed_work(nvme_wq, &ctrl->ka_work, ctrl->kato * HZ);
1024 }
1025
1026 void nvme_stop_keep_alive(struct nvme_ctrl *ctrl)
1027 {
1028 if (unlikely(ctrl->kato == 0))
1029 return;
1030
1031 cancel_delayed_work_sync(&ctrl->ka_work);
1032 }
1033 EXPORT_SYMBOL_GPL(nvme_stop_keep_alive);
1034
1035 static int nvme_identify_ctrl(struct nvme_ctrl *dev, struct nvme_id_ctrl **id)
1036 {
1037 struct nvme_command c = { };
1038 int error;
1039
1040
1041 c.identify.opcode = nvme_admin_identify;
1042 c.identify.cns = NVME_ID_CNS_CTRL;
1043
1044 *id = kmalloc(sizeof(struct nvme_id_ctrl), GFP_KERNEL);
1045 if (!*id)
1046 return -ENOMEM;
1047
1048 error = nvme_submit_sync_cmd(dev->admin_q, &c, *id,
1049 sizeof(struct nvme_id_ctrl));
1050 if (error)
1051 kfree(*id);
1052 return error;
1053 }
1054
1055 static int nvme_identify_ns_descs(struct nvme_ctrl *ctrl, unsigned nsid,
1056 struct nvme_ns_ids *ids)
1057 {
1058 struct nvme_command c = { };
1059 int status;
1060 void *data;
1061 int pos;
1062 int len;
1063
1064 c.identify.opcode = nvme_admin_identify;
1065 c.identify.nsid = cpu_to_le32(nsid);
1066 c.identify.cns = NVME_ID_CNS_NS_DESC_LIST;
1067
1068 data = kzalloc(NVME_IDENTIFY_DATA_SIZE, GFP_KERNEL);
1069 if (!data)
1070 return -ENOMEM;
1071
1072 status = nvme_submit_sync_cmd(ctrl->admin_q, &c, data,
1073 NVME_IDENTIFY_DATA_SIZE);
1074 if (status) {
1075 dev_warn(ctrl->device,
1076 "Identify Descriptors failed (%d)\n", status);
1077
1078
1079
1080
1081 if (status > 0 && !(status & NVME_SC_DNR))
1082 status = 0;
1083 goto free_data;
1084 }
1085
1086 for (pos = 0; pos < NVME_IDENTIFY_DATA_SIZE; pos += len) {
1087 struct nvme_ns_id_desc *cur = data + pos;
1088
1089 if (cur->nidl == 0)
1090 break;
1091
1092 switch (cur->nidt) {
1093 case NVME_NIDT_EUI64:
1094 if (cur->nidl != NVME_NIDT_EUI64_LEN) {
1095 dev_warn(ctrl->device,
1096 "ctrl returned bogus length: %d for NVME_NIDT_EUI64\n",
1097 cur->nidl);
1098 goto free_data;
1099 }
1100 len = NVME_NIDT_EUI64_LEN;
1101 memcpy(ids->eui64, data + pos + sizeof(*cur), len);
1102 break;
1103 case NVME_NIDT_NGUID:
1104 if (cur->nidl != NVME_NIDT_NGUID_LEN) {
1105 dev_warn(ctrl->device,
1106 "ctrl returned bogus length: %d for NVME_NIDT_NGUID\n",
1107 cur->nidl);
1108 goto free_data;
1109 }
1110 len = NVME_NIDT_NGUID_LEN;
1111 memcpy(ids->nguid, data + pos + sizeof(*cur), len);
1112 break;
1113 case NVME_NIDT_UUID:
1114 if (cur->nidl != NVME_NIDT_UUID_LEN) {
1115 dev_warn(ctrl->device,
1116 "ctrl returned bogus length: %d for NVME_NIDT_UUID\n",
1117 cur->nidl);
1118 goto free_data;
1119 }
1120 len = NVME_NIDT_UUID_LEN;
1121 uuid_copy(&ids->uuid, data + pos + sizeof(*cur));
1122 break;
1123 default:
1124
1125 len = cur->nidl;
1126 break;
1127 }
1128
1129 len += sizeof(*cur);
1130 }
1131 free_data:
1132 kfree(data);
1133 return status;
1134 }
1135
1136 static int nvme_identify_ns_list(struct nvme_ctrl *dev, unsigned nsid, __le32 *ns_list)
1137 {
1138 struct nvme_command c = { };
1139
1140 c.identify.opcode = nvme_admin_identify;
1141 c.identify.cns = NVME_ID_CNS_NS_ACTIVE_LIST;
1142 c.identify.nsid = cpu_to_le32(nsid);
1143 return nvme_submit_sync_cmd(dev->admin_q, &c, ns_list,
1144 NVME_IDENTIFY_DATA_SIZE);
1145 }
1146
1147 static int nvme_identify_ns(struct nvme_ctrl *ctrl,
1148 unsigned nsid, struct nvme_id_ns **id)
1149 {
1150 struct nvme_command c = { };
1151 int error;
1152
1153
1154 c.identify.opcode = nvme_admin_identify;
1155 c.identify.nsid = cpu_to_le32(nsid);
1156 c.identify.cns = NVME_ID_CNS_NS;
1157
1158 *id = kmalloc(sizeof(**id), GFP_KERNEL);
1159 if (!*id)
1160 return -ENOMEM;
1161
1162 error = nvme_submit_sync_cmd(ctrl->admin_q, &c, *id, sizeof(**id));
1163 if (error) {
1164 dev_warn(ctrl->device, "Identify namespace failed (%d)\n", error);
1165 kfree(*id);
1166 }
1167
1168 return error;
1169 }
1170
1171 static int nvme_features(struct nvme_ctrl *dev, u8 op, unsigned int fid,
1172 unsigned int dword11, void *buffer, size_t buflen, u32 *result)
1173 {
1174 union nvme_result res = { 0 };
1175 struct nvme_command c;
1176 int ret;
1177
1178 memset(&c, 0, sizeof(c));
1179 c.features.opcode = op;
1180 c.features.fid = cpu_to_le32(fid);
1181 c.features.dword11 = cpu_to_le32(dword11);
1182
1183 ret = __nvme_submit_sync_cmd(dev->admin_q, &c, &res,
1184 buffer, buflen, 0, NVME_QID_ANY, 0, 0, false);
1185 if (ret >= 0 && result)
1186 *result = le32_to_cpu(res.u32);
1187 return ret;
1188 }
1189
1190 int nvme_set_features(struct nvme_ctrl *dev, unsigned int fid,
1191 unsigned int dword11, void *buffer, size_t buflen,
1192 u32 *result)
1193 {
1194 return nvme_features(dev, nvme_admin_set_features, fid, dword11, buffer,
1195 buflen, result);
1196 }
1197 EXPORT_SYMBOL_GPL(nvme_set_features);
1198
1199 int nvme_get_features(struct nvme_ctrl *dev, unsigned int fid,
1200 unsigned int dword11, void *buffer, size_t buflen,
1201 u32 *result)
1202 {
1203 return nvme_features(dev, nvme_admin_get_features, fid, dword11, buffer,
1204 buflen, result);
1205 }
1206 EXPORT_SYMBOL_GPL(nvme_get_features);
1207
1208 int nvme_set_queue_count(struct nvme_ctrl *ctrl, int *count)
1209 {
1210 u32 q_count = (*count - 1) | ((*count - 1) << 16);
1211 u32 result;
1212 int status, nr_io_queues;
1213
1214 status = nvme_set_features(ctrl, NVME_FEAT_NUM_QUEUES, q_count, NULL, 0,
1215 &result);
1216 if (status < 0)
1217 return status;
1218
1219
1220
1221
1222
1223
1224 if (status > 0) {
1225 dev_err(ctrl->device, "Could not set queue count (%d)\n", status);
1226 *count = 0;
1227 } else {
1228 nr_io_queues = min(result & 0xffff, result >> 16) + 1;
1229 *count = min(*count, nr_io_queues);
1230 }
1231
1232 return 0;
1233 }
1234 EXPORT_SYMBOL_GPL(nvme_set_queue_count);
1235
1236 #define NVME_AEN_SUPPORTED \
1237 (NVME_AEN_CFG_NS_ATTR | NVME_AEN_CFG_FW_ACT | \
1238 NVME_AEN_CFG_ANA_CHANGE | NVME_AEN_CFG_DISC_CHANGE)
1239
1240 static void nvme_enable_aen(struct nvme_ctrl *ctrl)
1241 {
1242 u32 result, supported_aens = ctrl->oaes & NVME_AEN_SUPPORTED;
1243 int status;
1244
1245 if (!supported_aens)
1246 return;
1247
1248 status = nvme_set_features(ctrl, NVME_FEAT_ASYNC_EVENT, supported_aens,
1249 NULL, 0, &result);
1250 if (status)
1251 dev_warn(ctrl->device, "Failed to configure AEN (cfg %x)\n",
1252 supported_aens);
1253
1254 queue_work(nvme_wq, &ctrl->async_event_work);
1255 }
1256
1257
1258
1259
1260
1261
1262 static void __user *nvme_to_user_ptr(uintptr_t ptrval)
1263 {
1264 if (in_compat_syscall())
1265 ptrval = (compat_uptr_t)ptrval;
1266 return (void __user *)ptrval;
1267 }
1268
1269 static int nvme_submit_io(struct nvme_ns *ns, struct nvme_user_io __user *uio)
1270 {
1271 struct nvme_user_io io;
1272 struct nvme_command c;
1273 unsigned length, meta_len;
1274 void __user *metadata;
1275
1276 if (copy_from_user(&io, uio, sizeof(io)))
1277 return -EFAULT;
1278 if (io.flags)
1279 return -EINVAL;
1280
1281 switch (io.opcode) {
1282 case nvme_cmd_write:
1283 case nvme_cmd_read:
1284 case nvme_cmd_compare:
1285 break;
1286 default:
1287 return -EINVAL;
1288 }
1289
1290 length = (io.nblocks + 1) << ns->lba_shift;
1291 meta_len = (io.nblocks + 1) * ns->ms;
1292 metadata = nvme_to_user_ptr(io.metadata);
1293
1294 if (ns->ext) {
1295 length += meta_len;
1296 meta_len = 0;
1297 } else if (meta_len) {
1298 if ((io.metadata & 3) || !io.metadata)
1299 return -EINVAL;
1300 }
1301
1302 memset(&c, 0, sizeof(c));
1303 c.rw.opcode = io.opcode;
1304 c.rw.flags = io.flags;
1305 c.rw.nsid = cpu_to_le32(ns->head->ns_id);
1306 c.rw.slba = cpu_to_le64(io.slba);
1307 c.rw.length = cpu_to_le16(io.nblocks);
1308 c.rw.control = cpu_to_le16(io.control);
1309 c.rw.dsmgmt = cpu_to_le32(io.dsmgmt);
1310 c.rw.reftag = cpu_to_le32(io.reftag);
1311 c.rw.apptag = cpu_to_le16(io.apptag);
1312 c.rw.appmask = cpu_to_le16(io.appmask);
1313
1314 return nvme_submit_user_cmd(ns->queue, &c,
1315 nvme_to_user_ptr(io.addr), length,
1316 metadata, meta_len, lower_32_bits(io.slba), NULL, 0);
1317 }
1318
1319 static u32 nvme_known_admin_effects(u8 opcode)
1320 {
1321 switch (opcode) {
1322 case nvme_admin_format_nvm:
1323 return NVME_CMD_EFFECTS_CSUPP | NVME_CMD_EFFECTS_LBCC |
1324 NVME_CMD_EFFECTS_CSE_MASK;
1325 case nvme_admin_sanitize_nvm:
1326 return NVME_CMD_EFFECTS_CSE_MASK;
1327 default:
1328 break;
1329 }
1330 return 0;
1331 }
1332
1333 static u32 nvme_passthru_start(struct nvme_ctrl *ctrl, struct nvme_ns *ns,
1334 u8 opcode)
1335 {
1336 u32 effects = 0;
1337
1338 if (ns) {
1339 if (ctrl->effects)
1340 effects = le32_to_cpu(ctrl->effects->iocs[opcode]);
1341 if (effects & ~(NVME_CMD_EFFECTS_CSUPP | NVME_CMD_EFFECTS_LBCC))
1342 dev_warn(ctrl->device,
1343 "IO command:%02x has unhandled effects:%08x\n",
1344 opcode, effects);
1345 return 0;
1346 }
1347
1348 if (ctrl->effects)
1349 effects = le32_to_cpu(ctrl->effects->acs[opcode]);
1350 effects |= nvme_known_admin_effects(opcode);
1351
1352
1353
1354
1355
1356 if (effects & (NVME_CMD_EFFECTS_LBCC | NVME_CMD_EFFECTS_CSE_MASK)) {
1357 mutex_lock(&ctrl->scan_lock);
1358 mutex_lock(&ctrl->subsys->lock);
1359 nvme_mpath_start_freeze(ctrl->subsys);
1360 nvme_mpath_wait_freeze(ctrl->subsys);
1361 nvme_start_freeze(ctrl);
1362 nvme_wait_freeze(ctrl);
1363 }
1364 return effects;
1365 }
1366
1367 static void nvme_update_formats(struct nvme_ctrl *ctrl)
1368 {
1369 struct nvme_ns *ns;
1370
1371 down_read(&ctrl->namespaces_rwsem);
1372 list_for_each_entry(ns, &ctrl->namespaces, list)
1373 if (ns->disk && nvme_revalidate_disk(ns->disk))
1374 nvme_set_queue_dying(ns);
1375 up_read(&ctrl->namespaces_rwsem);
1376 }
1377
1378 static void nvme_passthru_end(struct nvme_ctrl *ctrl, u32 effects)
1379 {
1380
1381
1382
1383
1384
1385 if (effects & NVME_CMD_EFFECTS_LBCC)
1386 nvme_update_formats(ctrl);
1387 if (effects & (NVME_CMD_EFFECTS_LBCC | NVME_CMD_EFFECTS_CSE_MASK)) {
1388 nvme_unfreeze(ctrl);
1389 nvme_mpath_unfreeze(ctrl->subsys);
1390 mutex_unlock(&ctrl->subsys->lock);
1391 nvme_remove_invalid_namespaces(ctrl, NVME_NSID_ALL);
1392 mutex_unlock(&ctrl->scan_lock);
1393 }
1394 if (effects & NVME_CMD_EFFECTS_CCC)
1395 nvme_init_identify(ctrl);
1396 if (effects & (NVME_CMD_EFFECTS_NIC | NVME_CMD_EFFECTS_NCC))
1397 nvme_queue_scan(ctrl);
1398 }
1399
1400 static int nvme_user_cmd(struct nvme_ctrl *ctrl, struct nvme_ns *ns,
1401 struct nvme_passthru_cmd __user *ucmd)
1402 {
1403 struct nvme_passthru_cmd cmd;
1404 struct nvme_command c;
1405 unsigned timeout = 0;
1406 u32 effects;
1407 u64 result;
1408 int status;
1409
1410 if (!capable(CAP_SYS_ADMIN))
1411 return -EACCES;
1412 if (copy_from_user(&cmd, ucmd, sizeof(cmd)))
1413 return -EFAULT;
1414 if (cmd.flags)
1415 return -EINVAL;
1416
1417 memset(&c, 0, sizeof(c));
1418 c.common.opcode = cmd.opcode;
1419 c.common.flags = cmd.flags;
1420 c.common.nsid = cpu_to_le32(cmd.nsid);
1421 c.common.cdw2[0] = cpu_to_le32(cmd.cdw2);
1422 c.common.cdw2[1] = cpu_to_le32(cmd.cdw3);
1423 c.common.cdw10 = cpu_to_le32(cmd.cdw10);
1424 c.common.cdw11 = cpu_to_le32(cmd.cdw11);
1425 c.common.cdw12 = cpu_to_le32(cmd.cdw12);
1426 c.common.cdw13 = cpu_to_le32(cmd.cdw13);
1427 c.common.cdw14 = cpu_to_le32(cmd.cdw14);
1428 c.common.cdw15 = cpu_to_le32(cmd.cdw15);
1429
1430 if (cmd.timeout_ms)
1431 timeout = msecs_to_jiffies(cmd.timeout_ms);
1432
1433 effects = nvme_passthru_start(ctrl, ns, cmd.opcode);
1434 status = nvme_submit_user_cmd(ns ? ns->queue : ctrl->admin_q, &c,
1435 nvme_to_user_ptr(cmd.addr), cmd.data_len,
1436 nvme_to_user_ptr(cmd.metadata), cmd.metadata_len,
1437 0, &result, timeout);
1438 nvme_passthru_end(ctrl, effects);
1439
1440 if (status >= 0) {
1441 if (put_user(result, &ucmd->result))
1442 return -EFAULT;
1443 }
1444
1445 return status;
1446 }
1447
1448 static int nvme_user_cmd64(struct nvme_ctrl *ctrl, struct nvme_ns *ns,
1449 struct nvme_passthru_cmd64 __user *ucmd)
1450 {
1451 struct nvme_passthru_cmd64 cmd;
1452 struct nvme_command c;
1453 unsigned timeout = 0;
1454 u32 effects;
1455 int status;
1456
1457 if (!capable(CAP_SYS_ADMIN))
1458 return -EACCES;
1459 if (copy_from_user(&cmd, ucmd, sizeof(cmd)))
1460 return -EFAULT;
1461 if (cmd.flags)
1462 return -EINVAL;
1463
1464 memset(&c, 0, sizeof(c));
1465 c.common.opcode = cmd.opcode;
1466 c.common.flags = cmd.flags;
1467 c.common.nsid = cpu_to_le32(cmd.nsid);
1468 c.common.cdw2[0] = cpu_to_le32(cmd.cdw2);
1469 c.common.cdw2[1] = cpu_to_le32(cmd.cdw3);
1470 c.common.cdw10 = cpu_to_le32(cmd.cdw10);
1471 c.common.cdw11 = cpu_to_le32(cmd.cdw11);
1472 c.common.cdw12 = cpu_to_le32(cmd.cdw12);
1473 c.common.cdw13 = cpu_to_le32(cmd.cdw13);
1474 c.common.cdw14 = cpu_to_le32(cmd.cdw14);
1475 c.common.cdw15 = cpu_to_le32(cmd.cdw15);
1476
1477 if (cmd.timeout_ms)
1478 timeout = msecs_to_jiffies(cmd.timeout_ms);
1479
1480 effects = nvme_passthru_start(ctrl, ns, cmd.opcode);
1481 status = nvme_submit_user_cmd(ns ? ns->queue : ctrl->admin_q, &c,
1482 nvme_to_user_ptr(cmd.addr), cmd.data_len,
1483 nvme_to_user_ptr(cmd.metadata), cmd.metadata_len,
1484 0, &cmd.result, timeout);
1485 nvme_passthru_end(ctrl, effects);
1486
1487 if (status >= 0) {
1488 if (put_user(cmd.result, &ucmd->result))
1489 return -EFAULT;
1490 }
1491
1492 return status;
1493 }
1494
1495
1496
1497
1498
1499 static struct nvme_ns *nvme_get_ns_from_disk(struct gendisk *disk,
1500 struct nvme_ns_head **head, int *srcu_idx)
1501 {
1502 #ifdef CONFIG_NVME_MULTIPATH
1503 if (disk->fops == &nvme_ns_head_ops) {
1504 struct nvme_ns *ns;
1505
1506 *head = disk->private_data;
1507 *srcu_idx = srcu_read_lock(&(*head)->srcu);
1508 ns = nvme_find_path(*head);
1509 if (!ns)
1510 srcu_read_unlock(&(*head)->srcu, *srcu_idx);
1511 return ns;
1512 }
1513 #endif
1514 *head = NULL;
1515 *srcu_idx = -1;
1516 return disk->private_data;
1517 }
1518
1519 static void nvme_put_ns_from_disk(struct nvme_ns_head *head, int idx)
1520 {
1521 if (head)
1522 srcu_read_unlock(&head->srcu, idx);
1523 }
1524
1525 static bool is_ctrl_ioctl(unsigned int cmd)
1526 {
1527 if (cmd == NVME_IOCTL_ADMIN_CMD || cmd == NVME_IOCTL_ADMIN64_CMD)
1528 return true;
1529 if (is_sed_ioctl(cmd))
1530 return true;
1531 return false;
1532 }
1533
1534 static int nvme_handle_ctrl_ioctl(struct nvme_ns *ns, unsigned int cmd,
1535 void __user *argp,
1536 struct nvme_ns_head *head,
1537 int srcu_idx)
1538 {
1539 struct nvme_ctrl *ctrl = ns->ctrl;
1540 int ret;
1541
1542 nvme_get_ctrl(ns->ctrl);
1543 nvme_put_ns_from_disk(head, srcu_idx);
1544
1545 switch (cmd) {
1546 case NVME_IOCTL_ADMIN_CMD:
1547 ret = nvme_user_cmd(ctrl, NULL, argp);
1548 break;
1549 case NVME_IOCTL_ADMIN64_CMD:
1550 ret = nvme_user_cmd64(ctrl, NULL, argp);
1551 break;
1552 default:
1553 ret = sed_ioctl(ctrl->opal_dev, cmd, argp);
1554 break;
1555 }
1556 nvme_put_ctrl(ctrl);
1557 return ret;
1558 }
1559
1560 static int nvme_ioctl(struct block_device *bdev, fmode_t mode,
1561 unsigned int cmd, unsigned long arg)
1562 {
1563 struct nvme_ns_head *head = NULL;
1564 void __user *argp = (void __user *)arg;
1565 struct nvme_ns *ns;
1566 int srcu_idx, ret;
1567
1568 ns = nvme_get_ns_from_disk(bdev->bd_disk, &head, &srcu_idx);
1569 if (unlikely(!ns))
1570 return -EWOULDBLOCK;
1571
1572
1573
1574
1575
1576
1577 if (is_ctrl_ioctl(cmd))
1578 return nvme_handle_ctrl_ioctl(ns, cmd, argp, head, srcu_idx);
1579
1580 switch (cmd) {
1581 case NVME_IOCTL_ID:
1582 force_successful_syscall_return();
1583 ret = ns->head->ns_id;
1584 break;
1585 case NVME_IOCTL_IO_CMD:
1586 ret = nvme_user_cmd(ns->ctrl, ns, argp);
1587 break;
1588 case NVME_IOCTL_SUBMIT_IO:
1589 ret = nvme_submit_io(ns, argp);
1590 break;
1591 case NVME_IOCTL_IO64_CMD:
1592 ret = nvme_user_cmd64(ns->ctrl, ns, argp);
1593 break;
1594 default:
1595 if (ns->ndev)
1596 ret = nvme_nvm_ioctl(ns, cmd, arg);
1597 else
1598 ret = -ENOTTY;
1599 }
1600
1601 nvme_put_ns_from_disk(head, srcu_idx);
1602 return ret;
1603 }
1604
1605 static int nvme_open(struct block_device *bdev, fmode_t mode)
1606 {
1607 struct nvme_ns *ns = bdev->bd_disk->private_data;
1608
1609 #ifdef CONFIG_NVME_MULTIPATH
1610
1611 if (WARN_ON_ONCE(ns->head->disk))
1612 goto fail;
1613 #endif
1614 if (!kref_get_unless_zero(&ns->kref))
1615 goto fail;
1616 if (!try_module_get(ns->ctrl->ops->module))
1617 goto fail_put_ns;
1618
1619 return 0;
1620
1621 fail_put_ns:
1622 nvme_put_ns(ns);
1623 fail:
1624 return -ENXIO;
1625 }
1626
1627 static void nvme_release(struct gendisk *disk, fmode_t mode)
1628 {
1629 struct nvme_ns *ns = disk->private_data;
1630
1631 module_put(ns->ctrl->ops->module);
1632 nvme_put_ns(ns);
1633 }
1634
1635 static int nvme_getgeo(struct block_device *bdev, struct hd_geometry *geo)
1636 {
1637
1638 geo->heads = 1 << 6;
1639 geo->sectors = 1 << 5;
1640 geo->cylinders = get_capacity(bdev->bd_disk) >> 11;
1641 return 0;
1642 }
1643
1644 #ifdef CONFIG_BLK_DEV_INTEGRITY
1645 static void nvme_init_integrity(struct gendisk *disk, u16 ms, u8 pi_type)
1646 {
1647 struct blk_integrity integrity;
1648
1649 memset(&integrity, 0, sizeof(integrity));
1650 switch (pi_type) {
1651 case NVME_NS_DPS_PI_TYPE3:
1652 integrity.profile = &t10_pi_type3_crc;
1653 integrity.tag_size = sizeof(u16) + sizeof(u32);
1654 integrity.flags |= BLK_INTEGRITY_DEVICE_CAPABLE;
1655 break;
1656 case NVME_NS_DPS_PI_TYPE1:
1657 case NVME_NS_DPS_PI_TYPE2:
1658 integrity.profile = &t10_pi_type1_crc;
1659 integrity.tag_size = sizeof(u16);
1660 integrity.flags |= BLK_INTEGRITY_DEVICE_CAPABLE;
1661 break;
1662 default:
1663 integrity.profile = NULL;
1664 break;
1665 }
1666 integrity.tuple_size = ms;
1667 blk_integrity_register(disk, &integrity);
1668 blk_queue_max_integrity_segments(disk->queue, 1);
1669 }
1670 #else
1671 static void nvme_init_integrity(struct gendisk *disk, u16 ms, u8 pi_type)
1672 {
1673 }
1674 #endif
1675
1676 static void nvme_set_chunk_size(struct nvme_ns *ns)
1677 {
1678 u32 chunk_size = (((u32)ns->noiob) << (ns->lba_shift - 9));
1679 blk_queue_chunk_sectors(ns->queue, rounddown_pow_of_two(chunk_size));
1680 }
1681
1682 static void nvme_config_discard(struct gendisk *disk, struct nvme_ns *ns)
1683 {
1684 struct nvme_ctrl *ctrl = ns->ctrl;
1685 struct request_queue *queue = disk->queue;
1686 u32 size = queue_logical_block_size(queue);
1687
1688 if (!(ctrl->oncs & NVME_CTRL_ONCS_DSM)) {
1689 blk_queue_flag_clear(QUEUE_FLAG_DISCARD, queue);
1690 return;
1691 }
1692
1693 if (ctrl->nr_streams && ns->sws && ns->sgs)
1694 size *= ns->sws * ns->sgs;
1695
1696 BUILD_BUG_ON(PAGE_SIZE / sizeof(struct nvme_dsm_range) <
1697 NVME_DSM_MAX_RANGES);
1698
1699 queue->limits.discard_alignment = 0;
1700 queue->limits.discard_granularity = size;
1701
1702
1703 if (blk_queue_flag_test_and_set(QUEUE_FLAG_DISCARD, queue))
1704 return;
1705
1706 blk_queue_max_discard_sectors(queue, UINT_MAX);
1707 blk_queue_max_discard_segments(queue, NVME_DSM_MAX_RANGES);
1708
1709 if (ctrl->quirks & NVME_QUIRK_DEALLOCATE_ZEROES)
1710 blk_queue_max_write_zeroes_sectors(queue, UINT_MAX);
1711 }
1712
1713 static void nvme_config_write_zeroes(struct gendisk *disk, struct nvme_ns *ns)
1714 {
1715 u32 max_sectors;
1716 unsigned short bs = 1 << ns->lba_shift;
1717
1718 if (!(ns->ctrl->oncs & NVME_CTRL_ONCS_WRITE_ZEROES) ||
1719 (ns->ctrl->quirks & NVME_QUIRK_DISABLE_WRITE_ZEROES))
1720 return;
1721
1722
1723
1724
1725
1726
1727
1728
1729
1730
1731 if (ns->ctrl->max_hw_sectors == UINT_MAX)
1732 max_sectors = ((u32)(USHRT_MAX + 1) * bs) >> 9;
1733 else
1734 max_sectors = ((u32)(ns->ctrl->max_hw_sectors + 1) * bs) >> 9;
1735
1736 blk_queue_max_write_zeroes_sectors(disk->queue, max_sectors);
1737 }
1738
1739 static int nvme_report_ns_ids(struct nvme_ctrl *ctrl, unsigned int nsid,
1740 struct nvme_id_ns *id, struct nvme_ns_ids *ids)
1741 {
1742 memset(ids, 0, sizeof(*ids));
1743
1744 if (ctrl->vs >= NVME_VS(1, 1, 0))
1745 memcpy(ids->eui64, id->eui64, sizeof(id->eui64));
1746 if (ctrl->vs >= NVME_VS(1, 2, 0))
1747 memcpy(ids->nguid, id->nguid, sizeof(id->nguid));
1748 if (ctrl->vs >= NVME_VS(1, 3, 0))
1749 return nvme_identify_ns_descs(ctrl, nsid, ids);
1750 return 0;
1751 }
1752
1753 static bool nvme_ns_ids_valid(struct nvme_ns_ids *ids)
1754 {
1755 return !uuid_is_null(&ids->uuid) ||
1756 memchr_inv(ids->nguid, 0, sizeof(ids->nguid)) ||
1757 memchr_inv(ids->eui64, 0, sizeof(ids->eui64));
1758 }
1759
1760 static bool nvme_ns_ids_equal(struct nvme_ns_ids *a, struct nvme_ns_ids *b)
1761 {
1762 return uuid_equal(&a->uuid, &b->uuid) &&
1763 memcmp(&a->nguid, &b->nguid, sizeof(a->nguid)) == 0 &&
1764 memcmp(&a->eui64, &b->eui64, sizeof(a->eui64)) == 0;
1765 }
1766
1767 static void nvme_update_disk_info(struct gendisk *disk,
1768 struct nvme_ns *ns, struct nvme_id_ns *id)
1769 {
1770 sector_t capacity = le64_to_cpu(id->nsze) << (ns->lba_shift - 9);
1771 unsigned short bs = 1 << ns->lba_shift;
1772 u32 atomic_bs, phys_bs, io_opt;
1773
1774 if (ns->lba_shift > PAGE_SHIFT) {
1775
1776 bs = (1 << 9);
1777 }
1778 blk_mq_freeze_queue(disk->queue);
1779 blk_integrity_unregister(disk);
1780
1781 if (id->nabo == 0) {
1782
1783
1784
1785
1786
1787 if (id->nsfeat & (1 << 1) && id->nawupf)
1788 atomic_bs = (1 + le16_to_cpu(id->nawupf)) * bs;
1789 else
1790 atomic_bs = (1 + ns->ctrl->subsys->awupf) * bs;
1791 } else {
1792 atomic_bs = bs;
1793 }
1794 phys_bs = bs;
1795 io_opt = bs;
1796 if (id->nsfeat & (1 << 4)) {
1797
1798 phys_bs *= 1 + le16_to_cpu(id->npwg);
1799
1800 io_opt *= 1 + le16_to_cpu(id->nows);
1801 }
1802
1803 blk_queue_logical_block_size(disk->queue, bs);
1804
1805
1806
1807
1808
1809 blk_queue_physical_block_size(disk->queue, min(phys_bs, atomic_bs));
1810 blk_queue_io_min(disk->queue, phys_bs);
1811 blk_queue_io_opt(disk->queue, io_opt);
1812
1813 if (ns->ms && !ns->ext &&
1814 (ns->ctrl->ops->flags & NVME_F_METADATA_SUPPORTED))
1815 nvme_init_integrity(disk, ns->ms, ns->pi_type);
1816 if ((ns->ms && !nvme_ns_has_pi(ns) && !blk_get_integrity(disk)) ||
1817 ns->lba_shift > PAGE_SHIFT)
1818 capacity = 0;
1819
1820 set_capacity(disk, capacity);
1821
1822 nvme_config_discard(disk, ns);
1823 nvme_config_write_zeroes(disk, ns);
1824
1825 if (id->nsattr & (1 << 0))
1826 set_disk_ro(disk, true);
1827 else
1828 set_disk_ro(disk, false);
1829
1830 blk_mq_unfreeze_queue(disk->queue);
1831 }
1832
1833 static void __nvme_revalidate_disk(struct gendisk *disk, struct nvme_id_ns *id)
1834 {
1835 struct nvme_ns *ns = disk->private_data;
1836
1837
1838
1839
1840
1841 ns->lba_shift = id->lbaf[id->flbas & NVME_NS_FLBAS_LBA_MASK].ds;
1842 if (ns->lba_shift == 0)
1843 ns->lba_shift = 9;
1844 ns->noiob = le16_to_cpu(id->noiob);
1845 ns->ms = le16_to_cpu(id->lbaf[id->flbas & NVME_NS_FLBAS_LBA_MASK].ms);
1846 ns->ext = ns->ms && (id->flbas & NVME_NS_FLBAS_META_EXT);
1847
1848 if (ns->ms == sizeof(struct t10_pi_tuple))
1849 ns->pi_type = id->dps & NVME_NS_DPS_PI_MASK;
1850 else
1851 ns->pi_type = 0;
1852
1853 if (ns->noiob)
1854 nvme_set_chunk_size(ns);
1855 nvme_update_disk_info(disk, ns, id);
1856 #ifdef CONFIG_NVME_MULTIPATH
1857 if (ns->head->disk) {
1858 nvme_update_disk_info(ns->head->disk, ns, id);
1859 blk_queue_stack_limits(ns->head->disk->queue, ns->queue);
1860 revalidate_disk(ns->head->disk);
1861 }
1862 #endif
1863 }
1864
1865 static int nvme_revalidate_disk(struct gendisk *disk)
1866 {
1867 struct nvme_ns *ns = disk->private_data;
1868 struct nvme_ctrl *ctrl = ns->ctrl;
1869 struct nvme_id_ns *id;
1870 struct nvme_ns_ids ids;
1871 int ret = 0;
1872
1873 if (test_bit(NVME_NS_DEAD, &ns->flags)) {
1874 set_capacity(disk, 0);
1875 return -ENODEV;
1876 }
1877
1878 ret = nvme_identify_ns(ctrl, ns->head->ns_id, &id);
1879 if (ret)
1880 goto out;
1881
1882 if (id->ncap == 0) {
1883 ret = -ENODEV;
1884 goto free_id;
1885 }
1886
1887 __nvme_revalidate_disk(disk, id);
1888 ret = nvme_report_ns_ids(ctrl, ns->head->ns_id, id, &ids);
1889 if (ret)
1890 goto free_id;
1891
1892 if (!nvme_ns_ids_equal(&ns->head->ids, &ids)) {
1893 dev_err(ctrl->device,
1894 "identifiers changed for nsid %d\n", ns->head->ns_id);
1895 ret = -ENODEV;
1896 }
1897
1898 free_id:
1899 kfree(id);
1900 out:
1901
1902
1903
1904
1905 if (ret == -ENOMEM || (ret > 0 && !(ret & NVME_SC_DNR)))
1906 ret = 0;
1907 else if (ret > 0)
1908 ret = blk_status_to_errno(nvme_error_status(ret));
1909 return ret;
1910 }
1911
1912 static char nvme_pr_type(enum pr_type type)
1913 {
1914 switch (type) {
1915 case PR_WRITE_EXCLUSIVE:
1916 return 1;
1917 case PR_EXCLUSIVE_ACCESS:
1918 return 2;
1919 case PR_WRITE_EXCLUSIVE_REG_ONLY:
1920 return 3;
1921 case PR_EXCLUSIVE_ACCESS_REG_ONLY:
1922 return 4;
1923 case PR_WRITE_EXCLUSIVE_ALL_REGS:
1924 return 5;
1925 case PR_EXCLUSIVE_ACCESS_ALL_REGS:
1926 return 6;
1927 default:
1928 return 0;
1929 }
1930 };
1931
1932 static int nvme_pr_command(struct block_device *bdev, u32 cdw10,
1933 u64 key, u64 sa_key, u8 op)
1934 {
1935 struct nvme_ns_head *head = NULL;
1936 struct nvme_ns *ns;
1937 struct nvme_command c;
1938 int srcu_idx, ret;
1939 u8 data[16] = { 0, };
1940
1941 ns = nvme_get_ns_from_disk(bdev->bd_disk, &head, &srcu_idx);
1942 if (unlikely(!ns))
1943 return -EWOULDBLOCK;
1944
1945 put_unaligned_le64(key, &data[0]);
1946 put_unaligned_le64(sa_key, &data[8]);
1947
1948 memset(&c, 0, sizeof(c));
1949 c.common.opcode = op;
1950 c.common.nsid = cpu_to_le32(ns->head->ns_id);
1951 c.common.cdw10 = cpu_to_le32(cdw10);
1952
1953 ret = nvme_submit_sync_cmd(ns->queue, &c, data, 16);
1954 nvme_put_ns_from_disk(head, srcu_idx);
1955 return ret;
1956 }
1957
1958 static int nvme_pr_register(struct block_device *bdev, u64 old,
1959 u64 new, unsigned flags)
1960 {
1961 u32 cdw10;
1962
1963 if (flags & ~PR_FL_IGNORE_KEY)
1964 return -EOPNOTSUPP;
1965
1966 cdw10 = old ? 2 : 0;
1967 cdw10 |= (flags & PR_FL_IGNORE_KEY) ? 1 << 3 : 0;
1968 cdw10 |= (1 << 30) | (1 << 31);
1969 return nvme_pr_command(bdev, cdw10, old, new, nvme_cmd_resv_register);
1970 }
1971
1972 static int nvme_pr_reserve(struct block_device *bdev, u64 key,
1973 enum pr_type type, unsigned flags)
1974 {
1975 u32 cdw10;
1976
1977 if (flags & ~PR_FL_IGNORE_KEY)
1978 return -EOPNOTSUPP;
1979
1980 cdw10 = nvme_pr_type(type) << 8;
1981 cdw10 |= ((flags & PR_FL_IGNORE_KEY) ? 1 << 3 : 0);
1982 return nvme_pr_command(bdev, cdw10, key, 0, nvme_cmd_resv_acquire);
1983 }
1984
1985 static int nvme_pr_preempt(struct block_device *bdev, u64 old, u64 new,
1986 enum pr_type type, bool abort)
1987 {
1988 u32 cdw10 = nvme_pr_type(type) << 8 | (abort ? 2 : 1);
1989 return nvme_pr_command(bdev, cdw10, old, new, nvme_cmd_resv_acquire);
1990 }
1991
1992 static int nvme_pr_clear(struct block_device *bdev, u64 key)
1993 {
1994 u32 cdw10 = 1 | (key ? 1 << 3 : 0);
1995 return nvme_pr_command(bdev, cdw10, key, 0, nvme_cmd_resv_register);
1996 }
1997
1998 static int nvme_pr_release(struct block_device *bdev, u64 key, enum pr_type type)
1999 {
2000 u32 cdw10 = nvme_pr_type(type) << 8 | (key ? 1 << 3 : 0);
2001 return nvme_pr_command(bdev, cdw10, key, 0, nvme_cmd_resv_release);
2002 }
2003
2004 static const struct pr_ops nvme_pr_ops = {
2005 .pr_register = nvme_pr_register,
2006 .pr_reserve = nvme_pr_reserve,
2007 .pr_release = nvme_pr_release,
2008 .pr_preempt = nvme_pr_preempt,
2009 .pr_clear = nvme_pr_clear,
2010 };
2011
2012 #ifdef CONFIG_BLK_SED_OPAL
2013 int nvme_sec_submit(void *data, u16 spsp, u8 secp, void *buffer, size_t len,
2014 bool send)
2015 {
2016 struct nvme_ctrl *ctrl = data;
2017 struct nvme_command cmd;
2018
2019 memset(&cmd, 0, sizeof(cmd));
2020 if (send)
2021 cmd.common.opcode = nvme_admin_security_send;
2022 else
2023 cmd.common.opcode = nvme_admin_security_recv;
2024 cmd.common.nsid = 0;
2025 cmd.common.cdw10 = cpu_to_le32(((u32)secp) << 24 | ((u32)spsp) << 8);
2026 cmd.common.cdw11 = cpu_to_le32(len);
2027
2028 return __nvme_submit_sync_cmd(ctrl->admin_q, &cmd, NULL, buffer, len,
2029 ADMIN_TIMEOUT, NVME_QID_ANY, 1, 0, false);
2030 }
2031 EXPORT_SYMBOL_GPL(nvme_sec_submit);
2032 #endif
2033
2034 static const struct block_device_operations nvme_fops = {
2035 .owner = THIS_MODULE,
2036 .ioctl = nvme_ioctl,
2037 .compat_ioctl = nvme_ioctl,
2038 .open = nvme_open,
2039 .release = nvme_release,
2040 .getgeo = nvme_getgeo,
2041 .revalidate_disk= nvme_revalidate_disk,
2042 .pr_ops = &nvme_pr_ops,
2043 };
2044
2045 #ifdef CONFIG_NVME_MULTIPATH
2046 static int nvme_ns_head_open(struct block_device *bdev, fmode_t mode)
2047 {
2048 struct nvme_ns_head *head = bdev->bd_disk->private_data;
2049
2050 if (!kref_get_unless_zero(&head->ref))
2051 return -ENXIO;
2052 return 0;
2053 }
2054
2055 static void nvme_ns_head_release(struct gendisk *disk, fmode_t mode)
2056 {
2057 nvme_put_ns_head(disk->private_data);
2058 }
2059
2060 const struct block_device_operations nvme_ns_head_ops = {
2061 .owner = THIS_MODULE,
2062 .open = nvme_ns_head_open,
2063 .release = nvme_ns_head_release,
2064 .ioctl = nvme_ioctl,
2065 .compat_ioctl = nvme_ioctl,
2066 .getgeo = nvme_getgeo,
2067 .pr_ops = &nvme_pr_ops,
2068 };
2069 #endif
2070
2071 static int nvme_wait_ready(struct nvme_ctrl *ctrl, u64 cap, bool enabled)
2072 {
2073 unsigned long timeout =
2074 ((NVME_CAP_TIMEOUT(cap) + 1) * HZ / 2) + jiffies;
2075 u32 csts, bit = enabled ? NVME_CSTS_RDY : 0;
2076 int ret;
2077
2078 while ((ret = ctrl->ops->reg_read32(ctrl, NVME_REG_CSTS, &csts)) == 0) {
2079 if (csts == ~0)
2080 return -ENODEV;
2081 if ((csts & NVME_CSTS_RDY) == bit)
2082 break;
2083
2084 msleep(100);
2085 if (fatal_signal_pending(current))
2086 return -EINTR;
2087 if (time_after(jiffies, timeout)) {
2088 dev_err(ctrl->device,
2089 "Device not ready; aborting %s\n", enabled ?
2090 "initialisation" : "reset");
2091 return -ENODEV;
2092 }
2093 }
2094
2095 return ret;
2096 }
2097
2098
2099
2100
2101
2102
2103
2104 int nvme_disable_ctrl(struct nvme_ctrl *ctrl)
2105 {
2106 int ret;
2107
2108 ctrl->ctrl_config &= ~NVME_CC_SHN_MASK;
2109 ctrl->ctrl_config &= ~NVME_CC_ENABLE;
2110
2111 ret = ctrl->ops->reg_write32(ctrl, NVME_REG_CC, ctrl->ctrl_config);
2112 if (ret)
2113 return ret;
2114
2115 if (ctrl->quirks & NVME_QUIRK_DELAY_BEFORE_CHK_RDY)
2116 msleep(NVME_QUIRK_DELAY_AMOUNT);
2117
2118 return nvme_wait_ready(ctrl, ctrl->cap, false);
2119 }
2120 EXPORT_SYMBOL_GPL(nvme_disable_ctrl);
2121
2122 int nvme_enable_ctrl(struct nvme_ctrl *ctrl)
2123 {
2124
2125
2126
2127
2128
2129 unsigned dev_page_min, page_shift = 12;
2130 int ret;
2131
2132 ret = ctrl->ops->reg_read64(ctrl, NVME_REG_CAP, &ctrl->cap);
2133 if (ret) {
2134 dev_err(ctrl->device, "Reading CAP failed (%d)\n", ret);
2135 return ret;
2136 }
2137 dev_page_min = NVME_CAP_MPSMIN(ctrl->cap) + 12;
2138
2139 if (page_shift < dev_page_min) {
2140 dev_err(ctrl->device,
2141 "Minimum device page size %u too large for host (%u)\n",
2142 1 << dev_page_min, 1 << page_shift);
2143 return -ENODEV;
2144 }
2145
2146 ctrl->page_size = 1 << page_shift;
2147
2148 ctrl->ctrl_config = NVME_CC_CSS_NVM;
2149 ctrl->ctrl_config |= (page_shift - 12) << NVME_CC_MPS_SHIFT;
2150 ctrl->ctrl_config |= NVME_CC_AMS_RR | NVME_CC_SHN_NONE;
2151 ctrl->ctrl_config |= NVME_CC_IOSQES | NVME_CC_IOCQES;
2152 ctrl->ctrl_config |= NVME_CC_ENABLE;
2153
2154 ret = ctrl->ops->reg_write32(ctrl, NVME_REG_CC, ctrl->ctrl_config);
2155 if (ret)
2156 return ret;
2157 return nvme_wait_ready(ctrl, ctrl->cap, true);
2158 }
2159 EXPORT_SYMBOL_GPL(nvme_enable_ctrl);
2160
2161 int nvme_shutdown_ctrl(struct nvme_ctrl *ctrl)
2162 {
2163 unsigned long timeout = jiffies + (ctrl->shutdown_timeout * HZ);
2164 u32 csts;
2165 int ret;
2166
2167 ctrl->ctrl_config &= ~NVME_CC_SHN_MASK;
2168 ctrl->ctrl_config |= NVME_CC_SHN_NORMAL;
2169
2170 ret = ctrl->ops->reg_write32(ctrl, NVME_REG_CC, ctrl->ctrl_config);
2171 if (ret)
2172 return ret;
2173
2174 while ((ret = ctrl->ops->reg_read32(ctrl, NVME_REG_CSTS, &csts)) == 0) {
2175 if ((csts & NVME_CSTS_SHST_MASK) == NVME_CSTS_SHST_CMPLT)
2176 break;
2177
2178 msleep(100);
2179 if (fatal_signal_pending(current))
2180 return -EINTR;
2181 if (time_after(jiffies, timeout)) {
2182 dev_err(ctrl->device,
2183 "Device shutdown incomplete; abort shutdown\n");
2184 return -ENODEV;
2185 }
2186 }
2187
2188 return ret;
2189 }
2190 EXPORT_SYMBOL_GPL(nvme_shutdown_ctrl);
2191
2192 static void nvme_set_queue_limits(struct nvme_ctrl *ctrl,
2193 struct request_queue *q)
2194 {
2195 bool vwc = false;
2196
2197 if (ctrl->max_hw_sectors) {
2198 u32 max_segments =
2199 (ctrl->max_hw_sectors / (ctrl->page_size >> 9)) + 1;
2200
2201 max_segments = min_not_zero(max_segments, ctrl->max_segments);
2202 blk_queue_max_hw_sectors(q, ctrl->max_hw_sectors);
2203 blk_queue_max_segments(q, min_t(u32, max_segments, USHRT_MAX));
2204 }
2205 if ((ctrl->quirks & NVME_QUIRK_STRIPE_SIZE) &&
2206 is_power_of_2(ctrl->max_hw_sectors))
2207 blk_queue_chunk_sectors(q, ctrl->max_hw_sectors);
2208 blk_queue_virt_boundary(q, ctrl->page_size - 1);
2209 if (ctrl->vwc & NVME_CTRL_VWC_PRESENT)
2210 vwc = true;
2211 blk_queue_write_cache(q, vwc, vwc);
2212 }
2213
2214 static int nvme_configure_timestamp(struct nvme_ctrl *ctrl)
2215 {
2216 __le64 ts;
2217 int ret;
2218
2219 if (!(ctrl->oncs & NVME_CTRL_ONCS_TIMESTAMP))
2220 return 0;
2221
2222 ts = cpu_to_le64(ktime_to_ms(ktime_get_real()));
2223 ret = nvme_set_features(ctrl, NVME_FEAT_TIMESTAMP, 0, &ts, sizeof(ts),
2224 NULL);
2225 if (ret)
2226 dev_warn_once(ctrl->device,
2227 "could not set timestamp (%d)\n", ret);
2228 return ret;
2229 }
2230
2231 static int nvme_configure_acre(struct nvme_ctrl *ctrl)
2232 {
2233 struct nvme_feat_host_behavior *host;
2234 int ret;
2235
2236
2237 if (!ctrl->crdt[0])
2238 return 0;
2239
2240 host = kzalloc(sizeof(*host), GFP_KERNEL);
2241 if (!host)
2242 return 0;
2243
2244 host->acre = NVME_ENABLE_ACRE;
2245 ret = nvme_set_features(ctrl, NVME_FEAT_HOST_BEHAVIOR, 0,
2246 host, sizeof(*host), NULL);
2247 kfree(host);
2248 return ret;
2249 }
2250
2251 static int nvme_configure_apst(struct nvme_ctrl *ctrl)
2252 {
2253
2254
2255
2256
2257
2258
2259
2260
2261
2262
2263
2264
2265
2266
2267
2268
2269 unsigned apste;
2270 struct nvme_feat_auto_pst *table;
2271 u64 max_lat_us = 0;
2272 int max_ps = -1;
2273 int ret;
2274
2275
2276
2277
2278
2279 if (!ctrl->apsta)
2280 return 0;
2281
2282 if (ctrl->npss > 31) {
2283 dev_warn(ctrl->device, "NPSS is invalid; not using APST\n");
2284 return 0;
2285 }
2286
2287 table = kzalloc(sizeof(*table), GFP_KERNEL);
2288 if (!table)
2289 return 0;
2290
2291 if (!ctrl->apst_enabled || ctrl->ps_max_latency_us == 0) {
2292
2293 apste = 0;
2294 dev_dbg(ctrl->device, "APST disabled\n");
2295 } else {
2296 __le64 target = cpu_to_le64(0);
2297 int state;
2298
2299
2300
2301
2302
2303
2304
2305 for (state = (int)ctrl->npss; state >= 0; state--) {
2306 u64 total_latency_us, exit_latency_us, transition_ms;
2307
2308 if (target)
2309 table->entries[state] = target;
2310
2311
2312
2313
2314
2315 if (state == ctrl->npss &&
2316 (ctrl->quirks & NVME_QUIRK_NO_DEEPEST_PS))
2317 continue;
2318
2319
2320
2321
2322
2323 if (!(ctrl->psd[state].flags &
2324 NVME_PS_FLAGS_NON_OP_STATE))
2325 continue;
2326
2327 exit_latency_us =
2328 (u64)le32_to_cpu(ctrl->psd[state].exit_lat);
2329 if (exit_latency_us > ctrl->ps_max_latency_us)
2330 continue;
2331
2332 total_latency_us =
2333 exit_latency_us +
2334 le32_to_cpu(ctrl->psd[state].entry_lat);
2335
2336
2337
2338
2339
2340 transition_ms = total_latency_us + 19;
2341 do_div(transition_ms, 20);
2342 if (transition_ms > (1 << 24) - 1)
2343 transition_ms = (1 << 24) - 1;
2344
2345 target = cpu_to_le64((state << 3) |
2346 (transition_ms << 8));
2347
2348 if (max_ps == -1)
2349 max_ps = state;
2350
2351 if (total_latency_us > max_lat_us)
2352 max_lat_us = total_latency_us;
2353 }
2354
2355 apste = 1;
2356
2357 if (max_ps == -1) {
2358 dev_dbg(ctrl->device, "APST enabled but no non-operational states are available\n");
2359 } else {
2360 dev_dbg(ctrl->device, "APST enabled: max PS = %d, max round-trip latency = %lluus, table = %*phN\n",
2361 max_ps, max_lat_us, (int)sizeof(*table), table);
2362 }
2363 }
2364
2365 ret = nvme_set_features(ctrl, NVME_FEAT_AUTO_PST, apste,
2366 table, sizeof(*table), NULL);
2367 if (ret)
2368 dev_err(ctrl->device, "failed to set APST feature (%d)\n", ret);
2369
2370 kfree(table);
2371 return ret;
2372 }
2373
2374 static void nvme_set_latency_tolerance(struct device *dev, s32 val)
2375 {
2376 struct nvme_ctrl *ctrl = dev_get_drvdata(dev);
2377 u64 latency;
2378
2379 switch (val) {
2380 case PM_QOS_LATENCY_TOLERANCE_NO_CONSTRAINT:
2381 case PM_QOS_LATENCY_ANY:
2382 latency = U64_MAX;
2383 break;
2384
2385 default:
2386 latency = val;
2387 }
2388
2389 if (ctrl->ps_max_latency_us != latency) {
2390 ctrl->ps_max_latency_us = latency;
2391 nvme_configure_apst(ctrl);
2392 }
2393 }
2394
2395 struct nvme_core_quirk_entry {
2396
2397
2398
2399
2400
2401 u16 vid;
2402 const char *mn;
2403 const char *fr;
2404 unsigned long quirks;
2405 };
2406
2407 static const struct nvme_core_quirk_entry core_quirks[] = {
2408 {
2409
2410
2411
2412
2413 .vid = 0x1179,
2414 .mn = "THNSF5256GPUK TOSHIBA",
2415 .quirks = NVME_QUIRK_NO_APST,
2416 },
2417 {
2418
2419
2420
2421
2422
2423 .vid = 0x14a4,
2424 .fr = "22301111",
2425 .quirks = NVME_QUIRK_SIMPLE_SUSPEND,
2426 }
2427 };
2428
2429
2430 static bool string_matches(const char *idstr, const char *match, size_t len)
2431 {
2432 size_t matchlen;
2433
2434 if (!match)
2435 return true;
2436
2437 matchlen = strlen(match);
2438 WARN_ON_ONCE(matchlen > len);
2439
2440 if (memcmp(idstr, match, matchlen))
2441 return false;
2442
2443 for (; matchlen < len; matchlen++)
2444 if (idstr[matchlen] != ' ')
2445 return false;
2446
2447 return true;
2448 }
2449
2450 static bool quirk_matches(const struct nvme_id_ctrl *id,
2451 const struct nvme_core_quirk_entry *q)
2452 {
2453 return q->vid == le16_to_cpu(id->vid) &&
2454 string_matches(id->mn, q->mn, sizeof(id->mn)) &&
2455 string_matches(id->fr, q->fr, sizeof(id->fr));
2456 }
2457
2458 static void nvme_init_subnqn(struct nvme_subsystem *subsys, struct nvme_ctrl *ctrl,
2459 struct nvme_id_ctrl *id)
2460 {
2461 size_t nqnlen;
2462 int off;
2463
2464 if(!(ctrl->quirks & NVME_QUIRK_IGNORE_DEV_SUBNQN)) {
2465 nqnlen = strnlen(id->subnqn, NVMF_NQN_SIZE);
2466 if (nqnlen > 0 && nqnlen < NVMF_NQN_SIZE) {
2467 strlcpy(subsys->subnqn, id->subnqn, NVMF_NQN_SIZE);
2468 return;
2469 }
2470
2471 if (ctrl->vs >= NVME_VS(1, 2, 1))
2472 dev_warn(ctrl->device, "missing or invalid SUBNQN field.\n");
2473 }
2474
2475
2476 off = snprintf(subsys->subnqn, NVMF_NQN_SIZE,
2477 "nqn.2014.08.org.nvmexpress:%04x%04x",
2478 le16_to_cpu(id->vid), le16_to_cpu(id->ssvid));
2479 memcpy(subsys->subnqn + off, id->sn, sizeof(id->sn));
2480 off += sizeof(id->sn);
2481 memcpy(subsys->subnqn + off, id->mn, sizeof(id->mn));
2482 off += sizeof(id->mn);
2483 memset(subsys->subnqn + off, 0, sizeof(subsys->subnqn) - off);
2484 }
2485
2486 static void nvme_release_subsystem(struct device *dev)
2487 {
2488 struct nvme_subsystem *subsys =
2489 container_of(dev, struct nvme_subsystem, dev);
2490
2491 if (subsys->instance >= 0)
2492 ida_simple_remove(&nvme_instance_ida, subsys->instance);
2493 kfree(subsys);
2494 }
2495
2496 static void nvme_destroy_subsystem(struct kref *ref)
2497 {
2498 struct nvme_subsystem *subsys =
2499 container_of(ref, struct nvme_subsystem, ref);
2500
2501 mutex_lock(&nvme_subsystems_lock);
2502 list_del(&subsys->entry);
2503 mutex_unlock(&nvme_subsystems_lock);
2504
2505 ida_destroy(&subsys->ns_ida);
2506 device_del(&subsys->dev);
2507 put_device(&subsys->dev);
2508 }
2509
2510 static void nvme_put_subsystem(struct nvme_subsystem *subsys)
2511 {
2512 kref_put(&subsys->ref, nvme_destroy_subsystem);
2513 }
2514
2515 static struct nvme_subsystem *__nvme_find_get_subsystem(const char *subsysnqn)
2516 {
2517 struct nvme_subsystem *subsys;
2518
2519 lockdep_assert_held(&nvme_subsystems_lock);
2520
2521
2522
2523
2524
2525
2526
2527
2528
2529 if (!strcmp(subsysnqn, NVME_DISC_SUBSYS_NAME))
2530 return NULL;
2531
2532 list_for_each_entry(subsys, &nvme_subsystems, entry) {
2533 if (strcmp(subsys->subnqn, subsysnqn))
2534 continue;
2535 if (!kref_get_unless_zero(&subsys->ref))
2536 continue;
2537 return subsys;
2538 }
2539
2540 return NULL;
2541 }
2542
2543 #define SUBSYS_ATTR_RO(_name, _mode, _show) \
2544 struct device_attribute subsys_attr_##_name = \
2545 __ATTR(_name, _mode, _show, NULL)
2546
2547 static ssize_t nvme_subsys_show_nqn(struct device *dev,
2548 struct device_attribute *attr,
2549 char *buf)
2550 {
2551 struct nvme_subsystem *subsys =
2552 container_of(dev, struct nvme_subsystem, dev);
2553
2554 return snprintf(buf, PAGE_SIZE, "%s\n", subsys->subnqn);
2555 }
2556 static SUBSYS_ATTR_RO(subsysnqn, S_IRUGO, nvme_subsys_show_nqn);
2557
2558 #define nvme_subsys_show_str_function(field) \
2559 static ssize_t subsys_##field##_show(struct device *dev, \
2560 struct device_attribute *attr, char *buf) \
2561 { \
2562 struct nvme_subsystem *subsys = \
2563 container_of(dev, struct nvme_subsystem, dev); \
2564 return sprintf(buf, "%.*s\n", \
2565 (int)sizeof(subsys->field), subsys->field); \
2566 } \
2567 static SUBSYS_ATTR_RO(field, S_IRUGO, subsys_##field##_show);
2568
2569 nvme_subsys_show_str_function(model);
2570 nvme_subsys_show_str_function(serial);
2571 nvme_subsys_show_str_function(firmware_rev);
2572
2573 static struct attribute *nvme_subsys_attrs[] = {
2574 &subsys_attr_model.attr,
2575 &subsys_attr_serial.attr,
2576 &subsys_attr_firmware_rev.attr,
2577 &subsys_attr_subsysnqn.attr,
2578 #ifdef CONFIG_NVME_MULTIPATH
2579 &subsys_attr_iopolicy.attr,
2580 #endif
2581 NULL,
2582 };
2583
2584 static struct attribute_group nvme_subsys_attrs_group = {
2585 .attrs = nvme_subsys_attrs,
2586 };
2587
2588 static const struct attribute_group *nvme_subsys_attrs_groups[] = {
2589 &nvme_subsys_attrs_group,
2590 NULL,
2591 };
2592
2593 static bool nvme_validate_cntlid(struct nvme_subsystem *subsys,
2594 struct nvme_ctrl *ctrl, struct nvme_id_ctrl *id)
2595 {
2596 struct nvme_ctrl *tmp;
2597
2598 lockdep_assert_held(&nvme_subsystems_lock);
2599
2600 list_for_each_entry(tmp, &subsys->ctrls, subsys_entry) {
2601 if (tmp->state == NVME_CTRL_DELETING ||
2602 tmp->state == NVME_CTRL_DEAD)
2603 continue;
2604
2605 if (tmp->cntlid == ctrl->cntlid) {
2606 dev_err(ctrl->device,
2607 "Duplicate cntlid %u with %s, rejecting\n",
2608 ctrl->cntlid, dev_name(tmp->device));
2609 return false;
2610 }
2611
2612 if ((id->cmic & (1 << 1)) ||
2613 (ctrl->opts && ctrl->opts->discovery_nqn))
2614 continue;
2615
2616 dev_err(ctrl->device,
2617 "Subsystem does not support multiple controllers\n");
2618 return false;
2619 }
2620
2621 return true;
2622 }
2623
2624 static int nvme_init_subsystem(struct nvme_ctrl *ctrl, struct nvme_id_ctrl *id)
2625 {
2626 struct nvme_subsystem *subsys, *found;
2627 int ret;
2628
2629 subsys = kzalloc(sizeof(*subsys), GFP_KERNEL);
2630 if (!subsys)
2631 return -ENOMEM;
2632
2633 subsys->instance = -1;
2634 mutex_init(&subsys->lock);
2635 kref_init(&subsys->ref);
2636 INIT_LIST_HEAD(&subsys->ctrls);
2637 INIT_LIST_HEAD(&subsys->nsheads);
2638 nvme_init_subnqn(subsys, ctrl, id);
2639 memcpy(subsys->serial, id->sn, sizeof(subsys->serial));
2640 memcpy(subsys->model, id->mn, sizeof(subsys->model));
2641 memcpy(subsys->firmware_rev, id->fr, sizeof(subsys->firmware_rev));
2642 subsys->vendor_id = le16_to_cpu(id->vid);
2643 subsys->cmic = id->cmic;
2644 subsys->awupf = le16_to_cpu(id->awupf);
2645 #ifdef CONFIG_NVME_MULTIPATH
2646 subsys->iopolicy = NVME_IOPOLICY_NUMA;
2647 #endif
2648
2649 subsys->dev.class = nvme_subsys_class;
2650 subsys->dev.release = nvme_release_subsystem;
2651 subsys->dev.groups = nvme_subsys_attrs_groups;
2652 dev_set_name(&subsys->dev, "nvme-subsys%d", ctrl->instance);
2653 device_initialize(&subsys->dev);
2654
2655 mutex_lock(&nvme_subsystems_lock);
2656 found = __nvme_find_get_subsystem(subsys->subnqn);
2657 if (found) {
2658 put_device(&subsys->dev);
2659 subsys = found;
2660
2661 if (!nvme_validate_cntlid(subsys, ctrl, id)) {
2662 ret = -EINVAL;
2663 goto out_put_subsystem;
2664 }
2665 } else {
2666 ret = device_add(&subsys->dev);
2667 if (ret) {
2668 dev_err(ctrl->device,
2669 "failed to register subsystem device.\n");
2670 put_device(&subsys->dev);
2671 goto out_unlock;
2672 }
2673 ida_init(&subsys->ns_ida);
2674 list_add_tail(&subsys->entry, &nvme_subsystems);
2675 }
2676
2677 ret = sysfs_create_link(&subsys->dev.kobj, &ctrl->device->kobj,
2678 dev_name(ctrl->device));
2679 if (ret) {
2680 dev_err(ctrl->device,
2681 "failed to create sysfs link from subsystem.\n");
2682 goto out_put_subsystem;
2683 }
2684
2685 if (!found)
2686 subsys->instance = ctrl->instance;
2687 ctrl->subsys = subsys;
2688 list_add_tail(&ctrl->subsys_entry, &subsys->ctrls);
2689 mutex_unlock(&nvme_subsystems_lock);
2690 return 0;
2691
2692 out_put_subsystem:
2693 nvme_put_subsystem(subsys);
2694 out_unlock:
2695 mutex_unlock(&nvme_subsystems_lock);
2696 return ret;
2697 }
2698
2699 int nvme_get_log(struct nvme_ctrl *ctrl, u32 nsid, u8 log_page, u8 lsp,
2700 void *log, size_t size, u64 offset)
2701 {
2702 struct nvme_command c = { };
2703 unsigned long dwlen = size / 4 - 1;
2704
2705 c.get_log_page.opcode = nvme_admin_get_log_page;
2706 c.get_log_page.nsid = cpu_to_le32(nsid);
2707 c.get_log_page.lid = log_page;
2708 c.get_log_page.lsp = lsp;
2709 c.get_log_page.numdl = cpu_to_le16(dwlen & ((1 << 16) - 1));
2710 c.get_log_page.numdu = cpu_to_le16(dwlen >> 16);
2711 c.get_log_page.lpol = cpu_to_le32(lower_32_bits(offset));
2712 c.get_log_page.lpou = cpu_to_le32(upper_32_bits(offset));
2713
2714 return nvme_submit_sync_cmd(ctrl->admin_q, &c, log, size);
2715 }
2716
2717 static int nvme_get_effects_log(struct nvme_ctrl *ctrl)
2718 {
2719 int ret;
2720
2721 if (!ctrl->effects)
2722 ctrl->effects = kzalloc(sizeof(*ctrl->effects), GFP_KERNEL);
2723
2724 if (!ctrl->effects)
2725 return 0;
2726
2727 ret = nvme_get_log(ctrl, NVME_NSID_ALL, NVME_LOG_CMD_EFFECTS, 0,
2728 ctrl->effects, sizeof(*ctrl->effects), 0);
2729 if (ret) {
2730 kfree(ctrl->effects);
2731 ctrl->effects = NULL;
2732 }
2733 return ret;
2734 }
2735
2736
2737
2738
2739
2740
2741 int nvme_init_identify(struct nvme_ctrl *ctrl)
2742 {
2743 struct nvme_id_ctrl *id;
2744 int ret, page_shift;
2745 u32 max_hw_sectors;
2746 bool prev_apst_enabled;
2747
2748 ret = ctrl->ops->reg_read32(ctrl, NVME_REG_VS, &ctrl->vs);
2749 if (ret) {
2750 dev_err(ctrl->device, "Reading VS failed (%d)\n", ret);
2751 return ret;
2752 }
2753 page_shift = NVME_CAP_MPSMIN(ctrl->cap) + 12;
2754 ctrl->sqsize = min_t(int, NVME_CAP_MQES(ctrl->cap), ctrl->sqsize);
2755
2756 if (ctrl->vs >= NVME_VS(1, 1, 0))
2757 ctrl->subsystem = NVME_CAP_NSSRC(ctrl->cap);
2758
2759 ret = nvme_identify_ctrl(ctrl, &id);
2760 if (ret) {
2761 dev_err(ctrl->device, "Identify Controller failed (%d)\n", ret);
2762 return -EIO;
2763 }
2764
2765 if (id->lpa & NVME_CTRL_LPA_CMD_EFFECTS_LOG) {
2766 ret = nvme_get_effects_log(ctrl);
2767 if (ret < 0)
2768 goto out_free;
2769 }
2770
2771 if (!(ctrl->ops->flags & NVME_F_FABRICS))
2772 ctrl->cntlid = le16_to_cpu(id->cntlid);
2773
2774 if (!ctrl->identified) {
2775 int i;
2776
2777 ret = nvme_init_subsystem(ctrl, id);
2778 if (ret)
2779 goto out_free;
2780
2781
2782
2783
2784
2785
2786
2787
2788
2789 for (i = 0; i < ARRAY_SIZE(core_quirks); i++) {
2790 if (quirk_matches(id, &core_quirks[i]))
2791 ctrl->quirks |= core_quirks[i].quirks;
2792 }
2793 }
2794
2795 if (force_apst && (ctrl->quirks & NVME_QUIRK_NO_DEEPEST_PS)) {
2796 dev_warn(ctrl->device, "forcibly allowing all power states due to nvme_core.force_apst -- use at your own risk\n");
2797 ctrl->quirks &= ~NVME_QUIRK_NO_DEEPEST_PS;
2798 }
2799
2800 ctrl->crdt[0] = le16_to_cpu(id->crdt1);
2801 ctrl->crdt[1] = le16_to_cpu(id->crdt2);
2802 ctrl->crdt[2] = le16_to_cpu(id->crdt3);
2803
2804 ctrl->oacs = le16_to_cpu(id->oacs);
2805 ctrl->oncs = le16_to_cpu(id->oncs);
2806 ctrl->mtfa = le16_to_cpu(id->mtfa);
2807 ctrl->oaes = le32_to_cpu(id->oaes);
2808 atomic_set(&ctrl->abort_limit, id->acl + 1);
2809 ctrl->vwc = id->vwc;
2810 if (id->mdts)
2811 max_hw_sectors = 1 << (id->mdts + page_shift - 9);
2812 else
2813 max_hw_sectors = UINT_MAX;
2814 ctrl->max_hw_sectors =
2815 min_not_zero(ctrl->max_hw_sectors, max_hw_sectors);
2816
2817 nvme_set_queue_limits(ctrl, ctrl->admin_q);
2818 ctrl->sgls = le32_to_cpu(id->sgls);
2819 ctrl->kas = le16_to_cpu(id->kas);
2820 ctrl->max_namespaces = le32_to_cpu(id->mnan);
2821 ctrl->ctratt = le32_to_cpu(id->ctratt);
2822
2823 if (id->rtd3e) {
2824
2825 u32 transition_time = le32_to_cpu(id->rtd3e) / 1000000;
2826
2827 ctrl->shutdown_timeout = clamp_t(unsigned int, transition_time,
2828 shutdown_timeout, 60);
2829
2830 if (ctrl->shutdown_timeout != shutdown_timeout)
2831 dev_info(ctrl->device,
2832 "Shutdown timeout set to %u seconds\n",
2833 ctrl->shutdown_timeout);
2834 } else
2835 ctrl->shutdown_timeout = shutdown_timeout;
2836
2837 ctrl->npss = id->npss;
2838 ctrl->apsta = id->apsta;
2839 prev_apst_enabled = ctrl->apst_enabled;
2840 if (ctrl->quirks & NVME_QUIRK_NO_APST) {
2841 if (force_apst && id->apsta) {
2842 dev_warn(ctrl->device, "forcibly allowing APST due to nvme_core.force_apst -- use at your own risk\n");
2843 ctrl->apst_enabled = true;
2844 } else {
2845 ctrl->apst_enabled = false;
2846 }
2847 } else {
2848 ctrl->apst_enabled = id->apsta;
2849 }
2850 memcpy(ctrl->psd, id->psd, sizeof(ctrl->psd));
2851
2852 if (ctrl->ops->flags & NVME_F_FABRICS) {
2853 ctrl->icdoff = le16_to_cpu(id->icdoff);
2854 ctrl->ioccsz = le32_to_cpu(id->ioccsz);
2855 ctrl->iorcsz = le32_to_cpu(id->iorcsz);
2856 ctrl->maxcmd = le16_to_cpu(id->maxcmd);
2857
2858
2859
2860
2861
2862 if (ctrl->cntlid != le16_to_cpu(id->cntlid)) {
2863 ret = -EINVAL;
2864 goto out_free;
2865 }
2866
2867 if (!ctrl->opts->discovery_nqn && !ctrl->kas) {
2868 dev_err(ctrl->device,
2869 "keep-alive support is mandatory for fabrics\n");
2870 ret = -EINVAL;
2871 goto out_free;
2872 }
2873 } else {
2874 ctrl->hmpre = le32_to_cpu(id->hmpre);
2875 ctrl->hmmin = le32_to_cpu(id->hmmin);
2876 ctrl->hmminds = le32_to_cpu(id->hmminds);
2877 ctrl->hmmaxd = le16_to_cpu(id->hmmaxd);
2878 }
2879
2880 ret = nvme_mpath_init(ctrl, id);
2881 kfree(id);
2882
2883 if (ret < 0)
2884 return ret;
2885
2886 if (ctrl->apst_enabled && !prev_apst_enabled)
2887 dev_pm_qos_expose_latency_tolerance(ctrl->device);
2888 else if (!ctrl->apst_enabled && prev_apst_enabled)
2889 dev_pm_qos_hide_latency_tolerance(ctrl->device);
2890
2891 ret = nvme_configure_apst(ctrl);
2892 if (ret < 0)
2893 return ret;
2894
2895 ret = nvme_configure_timestamp(ctrl);
2896 if (ret < 0)
2897 return ret;
2898
2899 ret = nvme_configure_directives(ctrl);
2900 if (ret < 0)
2901 return ret;
2902
2903 ret = nvme_configure_acre(ctrl);
2904 if (ret < 0)
2905 return ret;
2906
2907 ctrl->identified = true;
2908
2909 return 0;
2910
2911 out_free:
2912 kfree(id);
2913 return ret;
2914 }
2915 EXPORT_SYMBOL_GPL(nvme_init_identify);
2916
2917 static int nvme_dev_open(struct inode *inode, struct file *file)
2918 {
2919 struct nvme_ctrl *ctrl =
2920 container_of(inode->i_cdev, struct nvme_ctrl, cdev);
2921
2922 switch (ctrl->state) {
2923 case NVME_CTRL_LIVE:
2924 break;
2925 default:
2926 return -EWOULDBLOCK;
2927 }
2928
2929 file->private_data = ctrl;
2930 return 0;
2931 }
2932
2933 static int nvme_dev_user_cmd(struct nvme_ctrl *ctrl, void __user *argp)
2934 {
2935 struct nvme_ns *ns;
2936 int ret;
2937
2938 down_read(&ctrl->namespaces_rwsem);
2939 if (list_empty(&ctrl->namespaces)) {
2940 ret = -ENOTTY;
2941 goto out_unlock;
2942 }
2943
2944 ns = list_first_entry(&ctrl->namespaces, struct nvme_ns, list);
2945 if (ns != list_last_entry(&ctrl->namespaces, struct nvme_ns, list)) {
2946 dev_warn(ctrl->device,
2947 "NVME_IOCTL_IO_CMD not supported when multiple namespaces present!\n");
2948 ret = -EINVAL;
2949 goto out_unlock;
2950 }
2951
2952 dev_warn(ctrl->device,
2953 "using deprecated NVME_IOCTL_IO_CMD ioctl on the char device!\n");
2954 kref_get(&ns->kref);
2955 up_read(&ctrl->namespaces_rwsem);
2956
2957 ret = nvme_user_cmd(ctrl, ns, argp);
2958 nvme_put_ns(ns);
2959 return ret;
2960
2961 out_unlock:
2962 up_read(&ctrl->namespaces_rwsem);
2963 return ret;
2964 }
2965
2966 static long nvme_dev_ioctl(struct file *file, unsigned int cmd,
2967 unsigned long arg)
2968 {
2969 struct nvme_ctrl *ctrl = file->private_data;
2970 void __user *argp = (void __user *)arg;
2971
2972 switch (cmd) {
2973 case NVME_IOCTL_ADMIN_CMD:
2974 return nvme_user_cmd(ctrl, NULL, argp);
2975 case NVME_IOCTL_ADMIN64_CMD:
2976 return nvme_user_cmd64(ctrl, NULL, argp);
2977 case NVME_IOCTL_IO_CMD:
2978 return nvme_dev_user_cmd(ctrl, argp);
2979 case NVME_IOCTL_RESET:
2980 dev_warn(ctrl->device, "resetting controller\n");
2981 return nvme_reset_ctrl_sync(ctrl);
2982 case NVME_IOCTL_SUBSYS_RESET:
2983 return nvme_reset_subsystem(ctrl);
2984 case NVME_IOCTL_RESCAN:
2985 nvme_queue_scan(ctrl);
2986 return 0;
2987 default:
2988 return -ENOTTY;
2989 }
2990 }
2991
2992 static const struct file_operations nvme_dev_fops = {
2993 .owner = THIS_MODULE,
2994 .open = nvme_dev_open,
2995 .unlocked_ioctl = nvme_dev_ioctl,
2996 .compat_ioctl = nvme_dev_ioctl,
2997 };
2998
2999 static ssize_t nvme_sysfs_reset(struct device *dev,
3000 struct device_attribute *attr, const char *buf,
3001 size_t count)
3002 {
3003 struct nvme_ctrl *ctrl = dev_get_drvdata(dev);
3004 int ret;
3005
3006 ret = nvme_reset_ctrl_sync(ctrl);
3007 if (ret < 0)
3008 return ret;
3009 return count;
3010 }
3011 static DEVICE_ATTR(reset_controller, S_IWUSR, NULL, nvme_sysfs_reset);
3012
3013 static ssize_t nvme_sysfs_rescan(struct device *dev,
3014 struct device_attribute *attr, const char *buf,
3015 size_t count)
3016 {
3017 struct nvme_ctrl *ctrl = dev_get_drvdata(dev);
3018
3019 nvme_queue_scan(ctrl);
3020 return count;
3021 }
3022 static DEVICE_ATTR(rescan_controller, S_IWUSR, NULL, nvme_sysfs_rescan);
3023
3024 static inline struct nvme_ns_head *dev_to_ns_head(struct device *dev)
3025 {
3026 struct gendisk *disk = dev_to_disk(dev);
3027
3028 if (disk->fops == &nvme_fops)
3029 return nvme_get_ns_from_dev(dev)->head;
3030 else
3031 return disk->private_data;
3032 }
3033
3034 static ssize_t wwid_show(struct device *dev, struct device_attribute *attr,
3035 char *buf)
3036 {
3037 struct nvme_ns_head *head = dev_to_ns_head(dev);
3038 struct nvme_ns_ids *ids = &head->ids;
3039 struct nvme_subsystem *subsys = head->subsys;
3040 int serial_len = sizeof(subsys->serial);
3041 int model_len = sizeof(subsys->model);
3042
3043 if (!uuid_is_null(&ids->uuid))
3044 return sprintf(buf, "uuid.%pU\n", &ids->uuid);
3045
3046 if (memchr_inv(ids->nguid, 0, sizeof(ids->nguid)))
3047 return sprintf(buf, "eui.%16phN\n", ids->nguid);
3048
3049 if (memchr_inv(ids->eui64, 0, sizeof(ids->eui64)))
3050 return sprintf(buf, "eui.%8phN\n", ids->eui64);
3051
3052 while (serial_len > 0 && (subsys->serial[serial_len - 1] == ' ' ||
3053 subsys->serial[serial_len - 1] == '\0'))
3054 serial_len--;
3055 while (model_len > 0 && (subsys->model[model_len - 1] == ' ' ||
3056 subsys->model[model_len - 1] == '\0'))
3057 model_len--;
3058
3059 return sprintf(buf, "nvme.%04x-%*phN-%*phN-%08x\n", subsys->vendor_id,
3060 serial_len, subsys->serial, model_len, subsys->model,
3061 head->ns_id);
3062 }
3063 static DEVICE_ATTR_RO(wwid);
3064
3065 static ssize_t nguid_show(struct device *dev, struct device_attribute *attr,
3066 char *buf)
3067 {
3068 return sprintf(buf, "%pU\n", dev_to_ns_head(dev)->ids.nguid);
3069 }
3070 static DEVICE_ATTR_RO(nguid);
3071
3072 static ssize_t uuid_show(struct device *dev, struct device_attribute *attr,
3073 char *buf)
3074 {
3075 struct nvme_ns_ids *ids = &dev_to_ns_head(dev)->ids;
3076
3077
3078
3079
3080 if (uuid_is_null(&ids->uuid)) {
3081 printk_ratelimited(KERN_WARNING
3082 "No UUID available providing old NGUID\n");
3083 return sprintf(buf, "%pU\n", ids->nguid);
3084 }
3085 return sprintf(buf, "%pU\n", &ids->uuid);
3086 }
3087 static DEVICE_ATTR_RO(uuid);
3088
3089 static ssize_t eui_show(struct device *dev, struct device_attribute *attr,
3090 char *buf)
3091 {
3092 return sprintf(buf, "%8ph\n", dev_to_ns_head(dev)->ids.eui64);
3093 }
3094 static DEVICE_ATTR_RO(eui);
3095
3096 static ssize_t nsid_show(struct device *dev, struct device_attribute *attr,
3097 char *buf)
3098 {
3099 return sprintf(buf, "%d\n", dev_to_ns_head(dev)->ns_id);
3100 }
3101 static DEVICE_ATTR_RO(nsid);
3102
3103 static struct attribute *nvme_ns_id_attrs[] = {
3104 &dev_attr_wwid.attr,
3105 &dev_attr_uuid.attr,
3106 &dev_attr_nguid.attr,
3107 &dev_attr_eui.attr,
3108 &dev_attr_nsid.attr,
3109 #ifdef CONFIG_NVME_MULTIPATH
3110 &dev_attr_ana_grpid.attr,
3111 &dev_attr_ana_state.attr,
3112 #endif
3113 NULL,
3114 };
3115
3116 static umode_t nvme_ns_id_attrs_are_visible(struct kobject *kobj,
3117 struct attribute *a, int n)
3118 {
3119 struct device *dev = container_of(kobj, struct device, kobj);
3120 struct nvme_ns_ids *ids = &dev_to_ns_head(dev)->ids;
3121
3122 if (a == &dev_attr_uuid.attr) {
3123 if (uuid_is_null(&ids->uuid) &&
3124 !memchr_inv(ids->nguid, 0, sizeof(ids->nguid)))
3125 return 0;
3126 }
3127 if (a == &dev_attr_nguid.attr) {
3128 if (!memchr_inv(ids->nguid, 0, sizeof(ids->nguid)))
3129 return 0;
3130 }
3131 if (a == &dev_attr_eui.attr) {
3132 if (!memchr_inv(ids->eui64, 0, sizeof(ids->eui64)))
3133 return 0;
3134 }
3135 #ifdef CONFIG_NVME_MULTIPATH
3136 if (a == &dev_attr_ana_grpid.attr || a == &dev_attr_ana_state.attr) {
3137 if (dev_to_disk(dev)->fops != &nvme_fops)
3138 return 0;
3139 if (!nvme_ctrl_use_ana(nvme_get_ns_from_dev(dev)->ctrl))
3140 return 0;
3141 }
3142 #endif
3143 return a->mode;
3144 }
3145
3146 static const struct attribute_group nvme_ns_id_attr_group = {
3147 .attrs = nvme_ns_id_attrs,
3148 .is_visible = nvme_ns_id_attrs_are_visible,
3149 };
3150
3151 const struct attribute_group *nvme_ns_id_attr_groups[] = {
3152 &nvme_ns_id_attr_group,
3153 #ifdef CONFIG_NVM
3154 &nvme_nvm_attr_group,
3155 #endif
3156 NULL,
3157 };
3158
3159 #define nvme_show_str_function(field) \
3160 static ssize_t field##_show(struct device *dev, \
3161 struct device_attribute *attr, char *buf) \
3162 { \
3163 struct nvme_ctrl *ctrl = dev_get_drvdata(dev); \
3164 return sprintf(buf, "%.*s\n", \
3165 (int)sizeof(ctrl->subsys->field), ctrl->subsys->field); \
3166 } \
3167 static DEVICE_ATTR(field, S_IRUGO, field##_show, NULL);
3168
3169 nvme_show_str_function(model);
3170 nvme_show_str_function(serial);
3171 nvme_show_str_function(firmware_rev);
3172
3173 #define nvme_show_int_function(field) \
3174 static ssize_t field##_show(struct device *dev, \
3175 struct device_attribute *attr, char *buf) \
3176 { \
3177 struct nvme_ctrl *ctrl = dev_get_drvdata(dev); \
3178 return sprintf(buf, "%d\n", ctrl->field); \
3179 } \
3180 static DEVICE_ATTR(field, S_IRUGO, field##_show, NULL);
3181
3182 nvme_show_int_function(cntlid);
3183 nvme_show_int_function(numa_node);
3184 nvme_show_int_function(queue_count);
3185 nvme_show_int_function(sqsize);
3186
3187 static ssize_t nvme_sysfs_delete(struct device *dev,
3188 struct device_attribute *attr, const char *buf,
3189 size_t count)
3190 {
3191 struct nvme_ctrl *ctrl = dev_get_drvdata(dev);
3192
3193 if (device_remove_file_self(dev, attr))
3194 nvme_delete_ctrl_sync(ctrl);
3195 return count;
3196 }
3197 static DEVICE_ATTR(delete_controller, S_IWUSR, NULL, nvme_sysfs_delete);
3198
3199 static ssize_t nvme_sysfs_show_transport(struct device *dev,
3200 struct device_attribute *attr,
3201 char *buf)
3202 {
3203 struct nvme_ctrl *ctrl = dev_get_drvdata(dev);
3204
3205 return snprintf(buf, PAGE_SIZE, "%s\n", ctrl->ops->name);
3206 }
3207 static DEVICE_ATTR(transport, S_IRUGO, nvme_sysfs_show_transport, NULL);
3208
3209 static ssize_t nvme_sysfs_show_state(struct device *dev,
3210 struct device_attribute *attr,
3211 char *buf)
3212 {
3213 struct nvme_ctrl *ctrl = dev_get_drvdata(dev);
3214 static const char *const state_name[] = {
3215 [NVME_CTRL_NEW] = "new",
3216 [NVME_CTRL_LIVE] = "live",
3217 [NVME_CTRL_RESETTING] = "resetting",
3218 [NVME_CTRL_CONNECTING] = "connecting",
3219 [NVME_CTRL_DELETING] = "deleting",
3220 [NVME_CTRL_DEAD] = "dead",
3221 };
3222
3223 if ((unsigned)ctrl->state < ARRAY_SIZE(state_name) &&
3224 state_name[ctrl->state])
3225 return sprintf(buf, "%s\n", state_name[ctrl->state]);
3226
3227 return sprintf(buf, "unknown state\n");
3228 }
3229
3230 static DEVICE_ATTR(state, S_IRUGO, nvme_sysfs_show_state, NULL);
3231
3232 static ssize_t nvme_sysfs_show_subsysnqn(struct device *dev,
3233 struct device_attribute *attr,
3234 char *buf)
3235 {
3236 struct nvme_ctrl *ctrl = dev_get_drvdata(dev);
3237
3238 return snprintf(buf, PAGE_SIZE, "%s\n", ctrl->subsys->subnqn);
3239 }
3240 static DEVICE_ATTR(subsysnqn, S_IRUGO, nvme_sysfs_show_subsysnqn, NULL);
3241
3242 static ssize_t nvme_sysfs_show_address(struct device *dev,
3243 struct device_attribute *attr,
3244 char *buf)
3245 {
3246 struct nvme_ctrl *ctrl = dev_get_drvdata(dev);
3247
3248 return ctrl->ops->get_address(ctrl, buf, PAGE_SIZE);
3249 }
3250 static DEVICE_ATTR(address, S_IRUGO, nvme_sysfs_show_address, NULL);
3251
3252 static struct attribute *nvme_dev_attrs[] = {
3253 &dev_attr_reset_controller.attr,
3254 &dev_attr_rescan_controller.attr,
3255 &dev_attr_model.attr,
3256 &dev_attr_serial.attr,
3257 &dev_attr_firmware_rev.attr,
3258 &dev_attr_cntlid.attr,
3259 &dev_attr_delete_controller.attr,
3260 &dev_attr_transport.attr,
3261 &dev_attr_subsysnqn.attr,
3262 &dev_attr_address.attr,
3263 &dev_attr_state.attr,
3264 &dev_attr_numa_node.attr,
3265 &dev_attr_queue_count.attr,
3266 &dev_attr_sqsize.attr,
3267 NULL
3268 };
3269
3270 static umode_t nvme_dev_attrs_are_visible(struct kobject *kobj,
3271 struct attribute *a, int n)
3272 {
3273 struct device *dev = container_of(kobj, struct device, kobj);
3274 struct nvme_ctrl *ctrl = dev_get_drvdata(dev);
3275
3276 if (a == &dev_attr_delete_controller.attr && !ctrl->ops->delete_ctrl)
3277 return 0;
3278 if (a == &dev_attr_address.attr && !ctrl->ops->get_address)
3279 return 0;
3280
3281 return a->mode;
3282 }
3283
3284 static struct attribute_group nvme_dev_attrs_group = {
3285 .attrs = nvme_dev_attrs,
3286 .is_visible = nvme_dev_attrs_are_visible,
3287 };
3288
3289 static const struct attribute_group *nvme_dev_attr_groups[] = {
3290 &nvme_dev_attrs_group,
3291 NULL,
3292 };
3293
3294 static struct nvme_ns_head *__nvme_find_ns_head(struct nvme_subsystem *subsys,
3295 unsigned nsid)
3296 {
3297 struct nvme_ns_head *h;
3298
3299 lockdep_assert_held(&subsys->lock);
3300
3301 list_for_each_entry(h, &subsys->nsheads, entry) {
3302 if (h->ns_id == nsid && kref_get_unless_zero(&h->ref))
3303 return h;
3304 }
3305
3306 return NULL;
3307 }
3308
3309 static int __nvme_check_ids(struct nvme_subsystem *subsys,
3310 struct nvme_ns_head *new)
3311 {
3312 struct nvme_ns_head *h;
3313
3314 lockdep_assert_held(&subsys->lock);
3315
3316 list_for_each_entry(h, &subsys->nsheads, entry) {
3317 if (nvme_ns_ids_valid(&new->ids) &&
3318 !list_empty(&h->list) &&
3319 nvme_ns_ids_equal(&new->ids, &h->ids))
3320 return -EINVAL;
3321 }
3322
3323 return 0;
3324 }
3325
3326 static struct nvme_ns_head *nvme_alloc_ns_head(struct nvme_ctrl *ctrl,
3327 unsigned nsid, struct nvme_id_ns *id)
3328 {
3329 struct nvme_ns_head *head;
3330 size_t size = sizeof(*head);
3331 int ret = -ENOMEM;
3332
3333 #ifdef CONFIG_NVME_MULTIPATH
3334 size += num_possible_nodes() * sizeof(struct nvme_ns *);
3335 #endif
3336
3337 head = kzalloc(size, GFP_KERNEL);
3338 if (!head)
3339 goto out;
3340 ret = ida_simple_get(&ctrl->subsys->ns_ida, 1, 0, GFP_KERNEL);
3341 if (ret < 0)
3342 goto out_free_head;
3343 head->instance = ret;
3344 INIT_LIST_HEAD(&head->list);
3345 ret = init_srcu_struct(&head->srcu);
3346 if (ret)
3347 goto out_ida_remove;
3348 head->subsys = ctrl->subsys;
3349 head->ns_id = nsid;
3350 kref_init(&head->ref);
3351
3352 ret = nvme_report_ns_ids(ctrl, nsid, id, &head->ids);
3353 if (ret)
3354 goto out_cleanup_srcu;
3355
3356 ret = __nvme_check_ids(ctrl->subsys, head);
3357 if (ret) {
3358 dev_err(ctrl->device,
3359 "duplicate IDs for nsid %d\n", nsid);
3360 goto out_cleanup_srcu;
3361 }
3362
3363 ret = nvme_mpath_alloc_disk(ctrl, head);
3364 if (ret)
3365 goto out_cleanup_srcu;
3366
3367 list_add_tail(&head->entry, &ctrl->subsys->nsheads);
3368
3369 kref_get(&ctrl->subsys->ref);
3370
3371 return head;
3372 out_cleanup_srcu:
3373 cleanup_srcu_struct(&head->srcu);
3374 out_ida_remove:
3375 ida_simple_remove(&ctrl->subsys->ns_ida, head->instance);
3376 out_free_head:
3377 kfree(head);
3378 out:
3379 if (ret > 0)
3380 ret = blk_status_to_errno(nvme_error_status(ret));
3381 return ERR_PTR(ret);
3382 }
3383
3384 static int nvme_init_ns_head(struct nvme_ns *ns, unsigned nsid,
3385 struct nvme_id_ns *id)
3386 {
3387 struct nvme_ctrl *ctrl = ns->ctrl;
3388 bool is_shared = id->nmic & (1 << 0);
3389 struct nvme_ns_head *head = NULL;
3390 int ret = 0;
3391
3392 mutex_lock(&ctrl->subsys->lock);
3393 if (is_shared)
3394 head = __nvme_find_ns_head(ctrl->subsys, nsid);
3395 if (!head) {
3396 head = nvme_alloc_ns_head(ctrl, nsid, id);
3397 if (IS_ERR(head)) {
3398 ret = PTR_ERR(head);
3399 goto out_unlock;
3400 }
3401 } else {
3402 struct nvme_ns_ids ids;
3403
3404 ret = nvme_report_ns_ids(ctrl, nsid, id, &ids);
3405 if (ret)
3406 goto out_unlock;
3407
3408 if (!nvme_ns_ids_equal(&head->ids, &ids)) {
3409 dev_err(ctrl->device,
3410 "IDs don't match for shared namespace %d\n",
3411 nsid);
3412 ret = -EINVAL;
3413 goto out_unlock;
3414 }
3415 }
3416
3417 list_add_tail(&ns->siblings, &head->list);
3418 ns->head = head;
3419
3420 out_unlock:
3421 mutex_unlock(&ctrl->subsys->lock);
3422 if (ret > 0)
3423 ret = blk_status_to_errno(nvme_error_status(ret));
3424 return ret;
3425 }
3426
3427 static int ns_cmp(void *priv, struct list_head *a, struct list_head *b)
3428 {
3429 struct nvme_ns *nsa = container_of(a, struct nvme_ns, list);
3430 struct nvme_ns *nsb = container_of(b, struct nvme_ns, list);
3431
3432 return nsa->head->ns_id - nsb->head->ns_id;
3433 }
3434
3435 static struct nvme_ns *nvme_find_get_ns(struct nvme_ctrl *ctrl, unsigned nsid)
3436 {
3437 struct nvme_ns *ns, *ret = NULL;
3438
3439 down_read(&ctrl->namespaces_rwsem);
3440 list_for_each_entry(ns, &ctrl->namespaces, list) {
3441 if (ns->head->ns_id == nsid) {
3442 if (!kref_get_unless_zero(&ns->kref))
3443 continue;
3444 ret = ns;
3445 break;
3446 }
3447 if (ns->head->ns_id > nsid)
3448 break;
3449 }
3450 up_read(&ctrl->namespaces_rwsem);
3451 return ret;
3452 }
3453
3454 static int nvme_setup_streams_ns(struct nvme_ctrl *ctrl, struct nvme_ns *ns)
3455 {
3456 struct streams_directive_params s;
3457 int ret;
3458
3459 if (!ctrl->nr_streams)
3460 return 0;
3461
3462 ret = nvme_get_stream_params(ctrl, &s, ns->head->ns_id);
3463 if (ret)
3464 return ret;
3465
3466 ns->sws = le32_to_cpu(s.sws);
3467 ns->sgs = le16_to_cpu(s.sgs);
3468
3469 if (ns->sws) {
3470 unsigned int bs = 1 << ns->lba_shift;
3471
3472 blk_queue_io_min(ns->queue, bs * ns->sws);
3473 if (ns->sgs)
3474 blk_queue_io_opt(ns->queue, bs * ns->sws * ns->sgs);
3475 }
3476
3477 return 0;
3478 }
3479
3480 static int nvme_alloc_ns(struct nvme_ctrl *ctrl, unsigned nsid)
3481 {
3482 struct nvme_ns *ns;
3483 struct gendisk *disk;
3484 struct nvme_id_ns *id;
3485 char disk_name[DISK_NAME_LEN];
3486 int node = ctrl->numa_node, flags = GENHD_FL_EXT_DEVT, ret;
3487
3488 ns = kzalloc_node(sizeof(*ns), GFP_KERNEL, node);
3489 if (!ns)
3490 return -ENOMEM;
3491
3492 ns->queue = blk_mq_init_queue(ctrl->tagset);
3493 if (IS_ERR(ns->queue)) {
3494 ret = PTR_ERR(ns->queue);
3495 goto out_free_ns;
3496 }
3497
3498 if (ctrl->opts && ctrl->opts->data_digest)
3499 ns->queue->backing_dev_info->capabilities
3500 |= BDI_CAP_STABLE_WRITES;
3501
3502 blk_queue_flag_set(QUEUE_FLAG_NONROT, ns->queue);
3503 if (ctrl->ops->flags & NVME_F_PCI_P2PDMA)
3504 blk_queue_flag_set(QUEUE_FLAG_PCI_P2PDMA, ns->queue);
3505
3506 ns->queue->queuedata = ns;
3507 ns->ctrl = ctrl;
3508
3509 kref_init(&ns->kref);
3510 ns->lba_shift = 9;
3511
3512 blk_queue_logical_block_size(ns->queue, 1 << ns->lba_shift);
3513 nvme_set_queue_limits(ctrl, ns->queue);
3514
3515 ret = nvme_identify_ns(ctrl, nsid, &id);
3516 if (ret)
3517 goto out_free_queue;
3518
3519 if (id->ncap == 0) {
3520 ret = -EINVAL;
3521 goto out_free_id;
3522 }
3523
3524 ret = nvme_init_ns_head(ns, nsid, id);
3525 if (ret)
3526 goto out_free_id;
3527 nvme_setup_streams_ns(ctrl, ns);
3528 nvme_set_disk_name(disk_name, ns, ctrl, &flags);
3529
3530 disk = alloc_disk_node(0, node);
3531 if (!disk) {
3532 ret = -ENOMEM;
3533 goto out_unlink_ns;
3534 }
3535
3536 disk->fops = &nvme_fops;
3537 disk->private_data = ns;
3538 disk->queue = ns->queue;
3539 disk->flags = flags;
3540 memcpy(disk->disk_name, disk_name, DISK_NAME_LEN);
3541 ns->disk = disk;
3542
3543 __nvme_revalidate_disk(disk, id);
3544
3545 if ((ctrl->quirks & NVME_QUIRK_LIGHTNVM) && id->vs[0] == 0x1) {
3546 ret = nvme_nvm_register(ns, disk_name, node);
3547 if (ret) {
3548 dev_warn(ctrl->device, "LightNVM init failure\n");
3549 goto out_put_disk;
3550 }
3551 }
3552
3553 down_write(&ctrl->namespaces_rwsem);
3554 list_add_tail(&ns->list, &ctrl->namespaces);
3555 up_write(&ctrl->namespaces_rwsem);
3556
3557 nvme_get_ctrl(ctrl);
3558
3559 device_add_disk(ctrl->device, ns->disk, nvme_ns_id_attr_groups);
3560
3561 nvme_mpath_add_disk(ns, id);
3562 nvme_fault_inject_init(&ns->fault_inject, ns->disk->disk_name);
3563 kfree(id);
3564
3565 return 0;
3566 out_put_disk:
3567
3568 ns->disk->queue = NULL;
3569 put_disk(ns->disk);
3570 out_unlink_ns:
3571 mutex_lock(&ctrl->subsys->lock);
3572 list_del_rcu(&ns->siblings);
3573 mutex_unlock(&ctrl->subsys->lock);
3574 nvme_put_ns_head(ns->head);
3575 out_free_id:
3576 kfree(id);
3577 out_free_queue:
3578 blk_cleanup_queue(ns->queue);
3579 out_free_ns:
3580 kfree(ns);
3581 if (ret > 0)
3582 ret = blk_status_to_errno(nvme_error_status(ret));
3583 return ret;
3584 }
3585
3586 static void nvme_ns_remove(struct nvme_ns *ns)
3587 {
3588 if (test_and_set_bit(NVME_NS_REMOVING, &ns->flags))
3589 return;
3590
3591 nvme_fault_inject_fini(&ns->fault_inject);
3592
3593 mutex_lock(&ns->ctrl->subsys->lock);
3594 list_del_rcu(&ns->siblings);
3595 mutex_unlock(&ns->ctrl->subsys->lock);
3596 synchronize_rcu();
3597 nvme_mpath_clear_current_path(ns);
3598 synchronize_srcu(&ns->head->srcu);
3599
3600 if (ns->disk && ns->disk->flags & GENHD_FL_UP) {
3601 del_gendisk(ns->disk);
3602 blk_cleanup_queue(ns->queue);
3603 if (blk_get_integrity(ns->disk))
3604 blk_integrity_unregister(ns->disk);
3605 }
3606
3607 down_write(&ns->ctrl->namespaces_rwsem);
3608 list_del_init(&ns->list);
3609 up_write(&ns->ctrl->namespaces_rwsem);
3610
3611 nvme_mpath_check_last_path(ns);
3612 nvme_put_ns(ns);
3613 }
3614
3615 static void nvme_validate_ns(struct nvme_ctrl *ctrl, unsigned nsid)
3616 {
3617 struct nvme_ns *ns;
3618
3619 ns = nvme_find_get_ns(ctrl, nsid);
3620 if (ns) {
3621 if (ns->disk && revalidate_disk(ns->disk))
3622 nvme_ns_remove(ns);
3623 nvme_put_ns(ns);
3624 } else
3625 nvme_alloc_ns(ctrl, nsid);
3626 }
3627
3628 static void nvme_remove_invalid_namespaces(struct nvme_ctrl *ctrl,
3629 unsigned nsid)
3630 {
3631 struct nvme_ns *ns, *next;
3632 LIST_HEAD(rm_list);
3633
3634 down_write(&ctrl->namespaces_rwsem);
3635 list_for_each_entry_safe(ns, next, &ctrl->namespaces, list) {
3636 if (ns->head->ns_id > nsid || test_bit(NVME_NS_DEAD, &ns->flags))
3637 list_move_tail(&ns->list, &rm_list);
3638 }
3639 up_write(&ctrl->namespaces_rwsem);
3640
3641 list_for_each_entry_safe(ns, next, &rm_list, list)
3642 nvme_ns_remove(ns);
3643
3644 }
3645
3646 static int nvme_scan_ns_list(struct nvme_ctrl *ctrl, unsigned nn)
3647 {
3648 struct nvme_ns *ns;
3649 __le32 *ns_list;
3650 unsigned i, j, nsid, prev = 0;
3651 unsigned num_lists = DIV_ROUND_UP_ULL((u64)nn, 1024);
3652 int ret = 0;
3653
3654 ns_list = kzalloc(NVME_IDENTIFY_DATA_SIZE, GFP_KERNEL);
3655 if (!ns_list)
3656 return -ENOMEM;
3657
3658 for (i = 0; i < num_lists; i++) {
3659 ret = nvme_identify_ns_list(ctrl, prev, ns_list);
3660 if (ret)
3661 goto free;
3662
3663 for (j = 0; j < min(nn, 1024U); j++) {
3664 nsid = le32_to_cpu(ns_list[j]);
3665 if (!nsid)
3666 goto out;
3667
3668 nvme_validate_ns(ctrl, nsid);
3669
3670 while (++prev < nsid) {
3671 ns = nvme_find_get_ns(ctrl, prev);
3672 if (ns) {
3673 nvme_ns_remove(ns);
3674 nvme_put_ns(ns);
3675 }
3676 }
3677 }
3678 nn -= j;
3679 }
3680 out:
3681 nvme_remove_invalid_namespaces(ctrl, prev);
3682 free:
3683 kfree(ns_list);
3684 return ret;
3685 }
3686
3687 static void nvme_scan_ns_sequential(struct nvme_ctrl *ctrl, unsigned nn)
3688 {
3689 unsigned i;
3690
3691 for (i = 1; i <= nn; i++)
3692 nvme_validate_ns(ctrl, i);
3693
3694 nvme_remove_invalid_namespaces(ctrl, nn);
3695 }
3696
3697 static void nvme_clear_changed_ns_log(struct nvme_ctrl *ctrl)
3698 {
3699 size_t log_size = NVME_MAX_CHANGED_NAMESPACES * sizeof(__le32);
3700 __le32 *log;
3701 int error;
3702
3703 log = kzalloc(log_size, GFP_KERNEL);
3704 if (!log)
3705 return;
3706
3707
3708
3709
3710
3711
3712
3713 error = nvme_get_log(ctrl, NVME_NSID_ALL, NVME_LOG_CHANGED_NS, 0, log,
3714 log_size, 0);
3715 if (error)
3716 dev_warn(ctrl->device,
3717 "reading changed ns log failed: %d\n", error);
3718
3719 kfree(log);
3720 }
3721
3722 static void nvme_scan_work(struct work_struct *work)
3723 {
3724 struct nvme_ctrl *ctrl =
3725 container_of(work, struct nvme_ctrl, scan_work);
3726 struct nvme_id_ctrl *id;
3727 unsigned nn;
3728
3729
3730 if (ctrl->state != NVME_CTRL_LIVE || !ctrl->tagset)
3731 return;
3732
3733 if (test_and_clear_bit(NVME_AER_NOTICE_NS_CHANGED, &ctrl->events)) {
3734 dev_info(ctrl->device, "rescanning namespaces.\n");
3735 nvme_clear_changed_ns_log(ctrl);
3736 }
3737
3738 if (nvme_identify_ctrl(ctrl, &id))
3739 return;
3740
3741 mutex_lock(&ctrl->scan_lock);
3742 nn = le32_to_cpu(id->nn);
3743 if (ctrl->vs >= NVME_VS(1, 1, 0) &&
3744 !(ctrl->quirks & NVME_QUIRK_IDENTIFY_CNS)) {
3745 if (!nvme_scan_ns_list(ctrl, nn))
3746 goto out_free_id;
3747 }
3748 nvme_scan_ns_sequential(ctrl, nn);
3749 out_free_id:
3750 mutex_unlock(&ctrl->scan_lock);
3751 kfree(id);
3752 down_write(&ctrl->namespaces_rwsem);
3753 list_sort(NULL, &ctrl->namespaces, ns_cmp);
3754 up_write(&ctrl->namespaces_rwsem);
3755 }
3756
3757
3758
3759
3760
3761
3762 void nvme_remove_namespaces(struct nvme_ctrl *ctrl)
3763 {
3764 struct nvme_ns *ns, *next;
3765 LIST_HEAD(ns_list);
3766
3767
3768
3769
3770
3771
3772 nvme_mpath_clear_ctrl_paths(ctrl);
3773
3774
3775 flush_work(&ctrl->scan_work);
3776
3777
3778
3779
3780
3781
3782
3783 if (ctrl->state == NVME_CTRL_DEAD)
3784 nvme_kill_queues(ctrl);
3785
3786 down_write(&ctrl->namespaces_rwsem);
3787 list_splice_init(&ctrl->namespaces, &ns_list);
3788 up_write(&ctrl->namespaces_rwsem);
3789
3790 list_for_each_entry_safe(ns, next, &ns_list, list)
3791 nvme_ns_remove(ns);
3792 }
3793 EXPORT_SYMBOL_GPL(nvme_remove_namespaces);
3794
3795 static int nvme_class_uevent(struct device *dev, struct kobj_uevent_env *env)
3796 {
3797 struct nvme_ctrl *ctrl =
3798 container_of(dev, struct nvme_ctrl, ctrl_device);
3799 struct nvmf_ctrl_options *opts = ctrl->opts;
3800 int ret;
3801
3802 ret = add_uevent_var(env, "NVME_TRTYPE=%s", ctrl->ops->name);
3803 if (ret)
3804 return ret;
3805
3806 if (opts) {
3807 ret = add_uevent_var(env, "NVME_TRADDR=%s", opts->traddr);
3808 if (ret)
3809 return ret;
3810
3811 ret = add_uevent_var(env, "NVME_TRSVCID=%s",
3812 opts->trsvcid ?: "none");
3813 if (ret)
3814 return ret;
3815
3816 ret = add_uevent_var(env, "NVME_HOST_TRADDR=%s",
3817 opts->host_traddr ?: "none");
3818 }
3819 return ret;
3820 }
3821
3822 static void nvme_aen_uevent(struct nvme_ctrl *ctrl)
3823 {
3824 char *envp[2] = { NULL, NULL };
3825 u32 aen_result = ctrl->aen_result;
3826
3827 ctrl->aen_result = 0;
3828 if (!aen_result)
3829 return;
3830
3831 envp[0] = kasprintf(GFP_KERNEL, "NVME_AEN=%#08x", aen_result);
3832 if (!envp[0])
3833 return;
3834 kobject_uevent_env(&ctrl->device->kobj, KOBJ_CHANGE, envp);
3835 kfree(envp[0]);
3836 }
3837
3838 static void nvme_async_event_work(struct work_struct *work)
3839 {
3840 struct nvme_ctrl *ctrl =
3841 container_of(work, struct nvme_ctrl, async_event_work);
3842
3843 nvme_aen_uevent(ctrl);
3844 ctrl->ops->submit_async_event(ctrl);
3845 }
3846
3847 static bool nvme_ctrl_pp_status(struct nvme_ctrl *ctrl)
3848 {
3849
3850 u32 csts;
3851
3852 if (ctrl->ops->reg_read32(ctrl, NVME_REG_CSTS, &csts))
3853 return false;
3854
3855 if (csts == ~0)
3856 return false;
3857
3858 return ((ctrl->ctrl_config & NVME_CC_ENABLE) && (csts & NVME_CSTS_PP));
3859 }
3860
3861 static void nvme_get_fw_slot_info(struct nvme_ctrl *ctrl)
3862 {
3863 struct nvme_fw_slot_info_log *log;
3864
3865 log = kmalloc(sizeof(*log), GFP_KERNEL);
3866 if (!log)
3867 return;
3868
3869 if (nvme_get_log(ctrl, NVME_NSID_ALL, NVME_LOG_FW_SLOT, 0, log,
3870 sizeof(*log), 0))
3871 dev_warn(ctrl->device, "Get FW SLOT INFO log error\n");
3872 kfree(log);
3873 }
3874
3875 static void nvme_fw_act_work(struct work_struct *work)
3876 {
3877 struct nvme_ctrl *ctrl = container_of(work,
3878 struct nvme_ctrl, fw_act_work);
3879 unsigned long fw_act_timeout;
3880
3881 if (ctrl->mtfa)
3882 fw_act_timeout = jiffies +
3883 msecs_to_jiffies(ctrl->mtfa * 100);
3884 else
3885 fw_act_timeout = jiffies +
3886 msecs_to_jiffies(admin_timeout * 1000);
3887
3888 nvme_stop_queues(ctrl);
3889 while (nvme_ctrl_pp_status(ctrl)) {
3890 if (time_after(jiffies, fw_act_timeout)) {
3891 dev_warn(ctrl->device,
3892 "Fw activation timeout, reset controller\n");
3893 nvme_try_sched_reset(ctrl);
3894 return;
3895 }
3896 msleep(100);
3897 }
3898
3899 if (!nvme_change_ctrl_state(ctrl, NVME_CTRL_LIVE))
3900 return;
3901
3902 nvme_start_queues(ctrl);
3903
3904 nvme_get_fw_slot_info(ctrl);
3905 }
3906
3907 static void nvme_handle_aen_notice(struct nvme_ctrl *ctrl, u32 result)
3908 {
3909 u32 aer_notice_type = (result & 0xff00) >> 8;
3910
3911 trace_nvme_async_event(ctrl, aer_notice_type);
3912
3913 switch (aer_notice_type) {
3914 case NVME_AER_NOTICE_NS_CHANGED:
3915 set_bit(NVME_AER_NOTICE_NS_CHANGED, &ctrl->events);
3916 nvme_queue_scan(ctrl);
3917 break;
3918 case NVME_AER_NOTICE_FW_ACT_STARTING:
3919
3920
3921
3922
3923
3924 if (nvme_change_ctrl_state(ctrl, NVME_CTRL_RESETTING))
3925 queue_work(nvme_wq, &ctrl->fw_act_work);
3926 break;
3927 #ifdef CONFIG_NVME_MULTIPATH
3928 case NVME_AER_NOTICE_ANA:
3929 if (!ctrl->ana_log_buf)
3930 break;
3931 queue_work(nvme_wq, &ctrl->ana_work);
3932 break;
3933 #endif
3934 case NVME_AER_NOTICE_DISC_CHANGED:
3935 ctrl->aen_result = result;
3936 break;
3937 default:
3938 dev_warn(ctrl->device, "async event result %08x\n", result);
3939 }
3940 }
3941
3942 void nvme_complete_async_event(struct nvme_ctrl *ctrl, __le16 status,
3943 volatile union nvme_result *res)
3944 {
3945 u32 result = le32_to_cpu(res->u32);
3946 u32 aer_type = result & 0x07;
3947
3948 if (le16_to_cpu(status) >> 1 != NVME_SC_SUCCESS)
3949 return;
3950
3951 switch (aer_type) {
3952 case NVME_AER_NOTICE:
3953 nvme_handle_aen_notice(ctrl, result);
3954 break;
3955 case NVME_AER_ERROR:
3956 case NVME_AER_SMART:
3957 case NVME_AER_CSS:
3958 case NVME_AER_VS:
3959 trace_nvme_async_event(ctrl, aer_type);
3960 ctrl->aen_result = result;
3961 break;
3962 default:
3963 break;
3964 }
3965 queue_work(nvme_wq, &ctrl->async_event_work);
3966 }
3967 EXPORT_SYMBOL_GPL(nvme_complete_async_event);
3968
3969 void nvme_stop_ctrl(struct nvme_ctrl *ctrl)
3970 {
3971 nvme_mpath_stop(ctrl);
3972 nvme_stop_keep_alive(ctrl);
3973 flush_work(&ctrl->async_event_work);
3974 cancel_work_sync(&ctrl->fw_act_work);
3975 }
3976 EXPORT_SYMBOL_GPL(nvme_stop_ctrl);
3977
3978 void nvme_start_ctrl(struct nvme_ctrl *ctrl)
3979 {
3980 if (ctrl->kato)
3981 nvme_start_keep_alive(ctrl);
3982
3983 nvme_enable_aen(ctrl);
3984
3985 if (ctrl->queue_count > 1) {
3986 nvme_queue_scan(ctrl);
3987 nvme_start_queues(ctrl);
3988 }
3989 }
3990 EXPORT_SYMBOL_GPL(nvme_start_ctrl);
3991
3992 void nvme_uninit_ctrl(struct nvme_ctrl *ctrl)
3993 {
3994 nvme_fault_inject_fini(&ctrl->fault_inject);
3995 dev_pm_qos_hide_latency_tolerance(ctrl->device);
3996 cdev_device_del(&ctrl->cdev, ctrl->device);
3997 }
3998 EXPORT_SYMBOL_GPL(nvme_uninit_ctrl);
3999
4000 static void nvme_free_ctrl(struct device *dev)
4001 {
4002 struct nvme_ctrl *ctrl =
4003 container_of(dev, struct nvme_ctrl, ctrl_device);
4004 struct nvme_subsystem *subsys = ctrl->subsys;
4005
4006 if (subsys && ctrl->instance != subsys->instance)
4007 ida_simple_remove(&nvme_instance_ida, ctrl->instance);
4008
4009 kfree(ctrl->effects);
4010 nvme_mpath_uninit(ctrl);
4011 __free_page(ctrl->discard_page);
4012
4013 if (subsys) {
4014 mutex_lock(&nvme_subsystems_lock);
4015 list_del(&ctrl->subsys_entry);
4016 sysfs_remove_link(&subsys->dev.kobj, dev_name(ctrl->device));
4017 mutex_unlock(&nvme_subsystems_lock);
4018 }
4019
4020 ctrl->ops->free_ctrl(ctrl);
4021
4022 if (subsys)
4023 nvme_put_subsystem(subsys);
4024 }
4025
4026
4027
4028
4029
4030
4031 int nvme_init_ctrl(struct nvme_ctrl *ctrl, struct device *dev,
4032 const struct nvme_ctrl_ops *ops, unsigned long quirks)
4033 {
4034 int ret;
4035
4036 ctrl->state = NVME_CTRL_NEW;
4037 spin_lock_init(&ctrl->lock);
4038 mutex_init(&ctrl->scan_lock);
4039 INIT_LIST_HEAD(&ctrl->namespaces);
4040 init_rwsem(&ctrl->namespaces_rwsem);
4041 ctrl->dev = dev;
4042 ctrl->ops = ops;
4043 ctrl->quirks = quirks;
4044 INIT_WORK(&ctrl->scan_work, nvme_scan_work);
4045 INIT_WORK(&ctrl->async_event_work, nvme_async_event_work);
4046 INIT_WORK(&ctrl->fw_act_work, nvme_fw_act_work);
4047 INIT_WORK(&ctrl->delete_work, nvme_delete_ctrl_work);
4048 init_waitqueue_head(&ctrl->state_wq);
4049
4050 INIT_DELAYED_WORK(&ctrl->ka_work, nvme_keep_alive_work);
4051 memset(&ctrl->ka_cmd, 0, sizeof(ctrl->ka_cmd));
4052 ctrl->ka_cmd.common.opcode = nvme_admin_keep_alive;
4053
4054 BUILD_BUG_ON(NVME_DSM_MAX_RANGES * sizeof(struct nvme_dsm_range) >
4055 PAGE_SIZE);
4056 ctrl->discard_page = alloc_page(GFP_KERNEL);
4057 if (!ctrl->discard_page) {
4058 ret = -ENOMEM;
4059 goto out;
4060 }
4061
4062 ret = ida_simple_get(&nvme_instance_ida, 0, 0, GFP_KERNEL);
4063 if (ret < 0)
4064 goto out;
4065 ctrl->instance = ret;
4066
4067 device_initialize(&ctrl->ctrl_device);
4068 ctrl->device = &ctrl->ctrl_device;
4069 ctrl->device->devt = MKDEV(MAJOR(nvme_chr_devt), ctrl->instance);
4070 ctrl->device->class = nvme_class;
4071 ctrl->device->parent = ctrl->dev;
4072 ctrl->device->groups = nvme_dev_attr_groups;
4073 ctrl->device->release = nvme_free_ctrl;
4074 dev_set_drvdata(ctrl->device, ctrl);
4075 ret = dev_set_name(ctrl->device, "nvme%d", ctrl->instance);
4076 if (ret)
4077 goto out_release_instance;
4078
4079 cdev_init(&ctrl->cdev, &nvme_dev_fops);
4080 ctrl->cdev.owner = ops->module;
4081 ret = cdev_device_add(&ctrl->cdev, ctrl->device);
4082 if (ret)
4083 goto out_free_name;
4084
4085
4086
4087
4088
4089 ctrl->device->power.set_latency_tolerance = nvme_set_latency_tolerance;
4090 dev_pm_qos_update_user_latency_tolerance(ctrl->device,
4091 min(default_ps_max_latency_us, (unsigned long)S32_MAX));
4092
4093 nvme_fault_inject_init(&ctrl->fault_inject, dev_name(ctrl->device));
4094
4095 return 0;
4096 out_free_name:
4097 kfree_const(ctrl->device->kobj.name);
4098 out_release_instance:
4099 ida_simple_remove(&nvme_instance_ida, ctrl->instance);
4100 out:
4101 if (ctrl->discard_page)
4102 __free_page(ctrl->discard_page);
4103 return ret;
4104 }
4105 EXPORT_SYMBOL_GPL(nvme_init_ctrl);
4106
4107
4108
4109
4110
4111
4112
4113
4114 void nvme_kill_queues(struct nvme_ctrl *ctrl)
4115 {
4116 struct nvme_ns *ns;
4117
4118 down_read(&ctrl->namespaces_rwsem);
4119
4120
4121 if (ctrl->admin_q && !blk_queue_dying(ctrl->admin_q))
4122 blk_mq_unquiesce_queue(ctrl->admin_q);
4123
4124 list_for_each_entry(ns, &ctrl->namespaces, list)
4125 nvme_set_queue_dying(ns);
4126
4127 up_read(&ctrl->namespaces_rwsem);
4128 }
4129 EXPORT_SYMBOL_GPL(nvme_kill_queues);
4130
4131 void nvme_unfreeze(struct nvme_ctrl *ctrl)
4132 {
4133 struct nvme_ns *ns;
4134
4135 down_read(&ctrl->namespaces_rwsem);
4136 list_for_each_entry(ns, &ctrl->namespaces, list)
4137 blk_mq_unfreeze_queue(ns->queue);
4138 up_read(&ctrl->namespaces_rwsem);
4139 }
4140 EXPORT_SYMBOL_GPL(nvme_unfreeze);
4141
4142 void nvme_wait_freeze_timeout(struct nvme_ctrl *ctrl, long timeout)
4143 {
4144 struct nvme_ns *ns;
4145
4146 down_read(&ctrl->namespaces_rwsem);
4147 list_for_each_entry(ns, &ctrl->namespaces, list) {
4148 timeout = blk_mq_freeze_queue_wait_timeout(ns->queue, timeout);
4149 if (timeout <= 0)
4150 break;
4151 }
4152 up_read(&ctrl->namespaces_rwsem);
4153 }
4154 EXPORT_SYMBOL_GPL(nvme_wait_freeze_timeout);
4155
4156 void nvme_wait_freeze(struct nvme_ctrl *ctrl)
4157 {
4158 struct nvme_ns *ns;
4159
4160 down_read(&ctrl->namespaces_rwsem);
4161 list_for_each_entry(ns, &ctrl->namespaces, list)
4162 blk_mq_freeze_queue_wait(ns->queue);
4163 up_read(&ctrl->namespaces_rwsem);
4164 }
4165 EXPORT_SYMBOL_GPL(nvme_wait_freeze);
4166
4167 void nvme_start_freeze(struct nvme_ctrl *ctrl)
4168 {
4169 struct nvme_ns *ns;
4170
4171 down_read(&ctrl->namespaces_rwsem);
4172 list_for_each_entry(ns, &ctrl->namespaces, list)
4173 blk_freeze_queue_start(ns->queue);
4174 up_read(&ctrl->namespaces_rwsem);
4175 }
4176 EXPORT_SYMBOL_GPL(nvme_start_freeze);
4177
4178 void nvme_stop_queues(struct nvme_ctrl *ctrl)
4179 {
4180 struct nvme_ns *ns;
4181
4182 down_read(&ctrl->namespaces_rwsem);
4183 list_for_each_entry(ns, &ctrl->namespaces, list)
4184 blk_mq_quiesce_queue(ns->queue);
4185 up_read(&ctrl->namespaces_rwsem);
4186 }
4187 EXPORT_SYMBOL_GPL(nvme_stop_queues);
4188
4189 void nvme_start_queues(struct nvme_ctrl *ctrl)
4190 {
4191 struct nvme_ns *ns;
4192
4193 down_read(&ctrl->namespaces_rwsem);
4194 list_for_each_entry(ns, &ctrl->namespaces, list)
4195 blk_mq_unquiesce_queue(ns->queue);
4196 up_read(&ctrl->namespaces_rwsem);
4197 }
4198 EXPORT_SYMBOL_GPL(nvme_start_queues);
4199
4200
4201 void nvme_sync_queues(struct nvme_ctrl *ctrl)
4202 {
4203 struct nvme_ns *ns;
4204
4205 down_read(&ctrl->namespaces_rwsem);
4206 list_for_each_entry(ns, &ctrl->namespaces, list)
4207 blk_sync_queue(ns->queue);
4208 up_read(&ctrl->namespaces_rwsem);
4209
4210 if (ctrl->admin_q)
4211 blk_sync_queue(ctrl->admin_q);
4212 }
4213 EXPORT_SYMBOL_GPL(nvme_sync_queues);
4214
4215
4216
4217
4218 static inline void _nvme_check_size(void)
4219 {
4220 BUILD_BUG_ON(sizeof(struct nvme_common_command) != 64);
4221 BUILD_BUG_ON(sizeof(struct nvme_rw_command) != 64);
4222 BUILD_BUG_ON(sizeof(struct nvme_identify) != 64);
4223 BUILD_BUG_ON(sizeof(struct nvme_features) != 64);
4224 BUILD_BUG_ON(sizeof(struct nvme_download_firmware) != 64);
4225 BUILD_BUG_ON(sizeof(struct nvme_format_cmd) != 64);
4226 BUILD_BUG_ON(sizeof(struct nvme_dsm_cmd) != 64);
4227 BUILD_BUG_ON(sizeof(struct nvme_write_zeroes_cmd) != 64);
4228 BUILD_BUG_ON(sizeof(struct nvme_abort_cmd) != 64);
4229 BUILD_BUG_ON(sizeof(struct nvme_get_log_page_command) != 64);
4230 BUILD_BUG_ON(sizeof(struct nvme_command) != 64);
4231 BUILD_BUG_ON(sizeof(struct nvme_id_ctrl) != NVME_IDENTIFY_DATA_SIZE);
4232 BUILD_BUG_ON(sizeof(struct nvme_id_ns) != NVME_IDENTIFY_DATA_SIZE);
4233 BUILD_BUG_ON(sizeof(struct nvme_lba_range_type) != 64);
4234 BUILD_BUG_ON(sizeof(struct nvme_smart_log) != 512);
4235 BUILD_BUG_ON(sizeof(struct nvme_dbbuf) != 64);
4236 BUILD_BUG_ON(sizeof(struct nvme_directive_cmd) != 64);
4237 }
4238
4239
4240 static int __init nvme_core_init(void)
4241 {
4242 int result = -ENOMEM;
4243
4244 _nvme_check_size();
4245
4246 nvme_wq = alloc_workqueue("nvme-wq",
4247 WQ_UNBOUND | WQ_MEM_RECLAIM | WQ_SYSFS, 0);
4248 if (!nvme_wq)
4249 goto out;
4250
4251 nvme_reset_wq = alloc_workqueue("nvme-reset-wq",
4252 WQ_UNBOUND | WQ_MEM_RECLAIM | WQ_SYSFS, 0);
4253 if (!nvme_reset_wq)
4254 goto destroy_wq;
4255
4256 nvme_delete_wq = alloc_workqueue("nvme-delete-wq",
4257 WQ_UNBOUND | WQ_MEM_RECLAIM | WQ_SYSFS, 0);
4258 if (!nvme_delete_wq)
4259 goto destroy_reset_wq;
4260
4261 result = alloc_chrdev_region(&nvme_chr_devt, 0, NVME_MINORS, "nvme");
4262 if (result < 0)
4263 goto destroy_delete_wq;
4264
4265 nvme_class = class_create(THIS_MODULE, "nvme");
4266 if (IS_ERR(nvme_class)) {
4267 result = PTR_ERR(nvme_class);
4268 goto unregister_chrdev;
4269 }
4270 nvme_class->dev_uevent = nvme_class_uevent;
4271
4272 nvme_subsys_class = class_create(THIS_MODULE, "nvme-subsystem");
4273 if (IS_ERR(nvme_subsys_class)) {
4274 result = PTR_ERR(nvme_subsys_class);
4275 goto destroy_class;
4276 }
4277 return 0;
4278
4279 destroy_class:
4280 class_destroy(nvme_class);
4281 unregister_chrdev:
4282 unregister_chrdev_region(nvme_chr_devt, NVME_MINORS);
4283 destroy_delete_wq:
4284 destroy_workqueue(nvme_delete_wq);
4285 destroy_reset_wq:
4286 destroy_workqueue(nvme_reset_wq);
4287 destroy_wq:
4288 destroy_workqueue(nvme_wq);
4289 out:
4290 return result;
4291 }
4292
4293 static void __exit nvme_core_exit(void)
4294 {
4295 class_destroy(nvme_subsys_class);
4296 class_destroy(nvme_class);
4297 unregister_chrdev_region(nvme_chr_devt, NVME_MINORS);
4298 destroy_workqueue(nvme_delete_wq);
4299 destroy_workqueue(nvme_reset_wq);
4300 destroy_workqueue(nvme_wq);
4301 }
4302
4303 MODULE_LICENSE("GPL");
4304 MODULE_VERSION("1.0");
4305 module_init(nvme_core_init);
4306 module_exit(nvme_core_exit);