blob: 7ded308fd67bbcfad8b55257cb20f5a99a1f1f27 [file] [log] [blame]
Christoph Hellwig21d34712015-11-26 09:08:36 +01001/*
2 * NVM Express device driver
3 * Copyright (c) 2011-2014, Intel Corporation.
4 *
5 * This program is free software; you can redistribute it and/or modify it
6 * under the terms and conditions of the GNU General Public License,
7 * version 2, as published by the Free Software Foundation.
8 *
9 * This program is distributed in the hope it will be useful, but WITHOUT
10 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
11 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for
12 * more details.
13 */
14
15#include <linux/blkdev.h>
16#include <linux/blk-mq.h>
Christoph Hellwig5fd4ce12015-11-28 15:03:49 +010017#include <linux/delay.h>
Christoph Hellwig21d34712015-11-26 09:08:36 +010018#include <linux/errno.h>
Christoph Hellwig1673f1f2015-11-26 10:54:19 +010019#include <linux/hdreg.h>
Christoph Hellwig21d34712015-11-26 09:08:36 +010020#include <linux/kernel.h>
Christoph Hellwig5bae7f72015-11-28 15:39:07 +010021#include <linux/module.h>
22#include <linux/list_sort.h>
Christoph Hellwig21d34712015-11-26 09:08:36 +010023#include <linux/slab.h>
24#include <linux/types.h>
Christoph Hellwig1673f1f2015-11-26 10:54:19 +010025#include <linux/pr.h>
26#include <linux/ptrace.h>
27#include <linux/nvme_ioctl.h>
28#include <linux/t10-pi.h>
29#include <scsi/sg.h>
30#include <asm/unaligned.h>
Christoph Hellwig21d34712015-11-26 09:08:36 +010031
32#include "nvme.h"
33
Christoph Hellwigf3ca80f2015-11-28 15:40:19 +010034#define NVME_MINORS (1U << MINORBITS)
35
Ming Linba0ba7d2016-02-10 10:03:30 -080036unsigned char admin_timeout = 60;
37module_param(admin_timeout, byte, 0644);
38MODULE_PARM_DESC(admin_timeout, "timeout in seconds for admin commands");
Ming Lin576d55d2016-02-10 10:03:32 -080039EXPORT_SYMBOL_GPL(admin_timeout);
Ming Linba0ba7d2016-02-10 10:03:30 -080040
41unsigned char nvme_io_timeout = 30;
42module_param_named(io_timeout, nvme_io_timeout, byte, 0644);
43MODULE_PARM_DESC(io_timeout, "timeout in seconds for I/O");
Ming Lin576d55d2016-02-10 10:03:32 -080044EXPORT_SYMBOL_GPL(nvme_io_timeout);
Ming Linba0ba7d2016-02-10 10:03:30 -080045
46unsigned char shutdown_timeout = 5;
47module_param(shutdown_timeout, byte, 0644);
48MODULE_PARM_DESC(shutdown_timeout, "timeout in seconds for controller shutdown");
49
Christoph Hellwig5bae7f72015-11-28 15:39:07 +010050static int nvme_major;
51module_param(nvme_major, int, 0);
52
Christoph Hellwigf3ca80f2015-11-28 15:40:19 +010053static int nvme_char_major;
54module_param(nvme_char_major, int, 0);
55
56static LIST_HEAD(nvme_ctrl_list);
Ming Lin9f2482b2016-02-10 10:03:31 -080057static DEFINE_SPINLOCK(dev_list_lock);
Christoph Hellwig1673f1f2015-11-26 10:54:19 +010058
Christoph Hellwigf3ca80f2015-11-28 15:40:19 +010059static struct class *nvme_class;
60
Ming Linc55a2fd2016-05-18 14:05:02 -070061void nvme_cancel_request(struct request *req, void *data, bool reserved)
62{
63 int status;
64
65 if (!blk_mq_request_started(req))
66 return;
67
68 dev_dbg_ratelimited(((struct nvme_ctrl *) data)->device,
69 "Cancelling I/O %d", req->tag);
70
71 status = NVME_SC_ABORT_REQ;
72 if (blk_queue_dying(req->q))
73 status |= NVME_SC_DNR;
74 blk_mq_complete_request(req, status);
75}
76EXPORT_SYMBOL_GPL(nvme_cancel_request);
77
Christoph Hellwigbb8d2612016-04-26 13:51:57 +020078bool nvme_change_ctrl_state(struct nvme_ctrl *ctrl,
79 enum nvme_ctrl_state new_state)
80{
81 enum nvme_ctrl_state old_state = ctrl->state;
82 bool changed = false;
83
84 spin_lock_irq(&ctrl->lock);
85 switch (new_state) {
86 case NVME_CTRL_LIVE:
87 switch (old_state) {
88 case NVME_CTRL_RESETTING:
89 changed = true;
90 /* FALLTHRU */
91 default:
92 break;
93 }
94 break;
95 case NVME_CTRL_RESETTING:
96 switch (old_state) {
97 case NVME_CTRL_NEW:
98 case NVME_CTRL_LIVE:
99 changed = true;
100 /* FALLTHRU */
101 default:
102 break;
103 }
104 break;
105 case NVME_CTRL_DELETING:
106 switch (old_state) {
107 case NVME_CTRL_LIVE:
108 case NVME_CTRL_RESETTING:
109 changed = true;
110 /* FALLTHRU */
111 default:
112 break;
113 }
114 break;
Keith Busch0ff9d4e2016-05-12 08:37:14 -0600115 case NVME_CTRL_DEAD:
116 switch (old_state) {
117 case NVME_CTRL_DELETING:
118 changed = true;
119 /* FALLTHRU */
120 default:
121 break;
122 }
123 break;
Christoph Hellwigbb8d2612016-04-26 13:51:57 +0200124 default:
125 break;
126 }
127 spin_unlock_irq(&ctrl->lock);
128
129 if (changed)
130 ctrl->state = new_state;
131
132 return changed;
133}
134EXPORT_SYMBOL_GPL(nvme_change_ctrl_state);
135
Christoph Hellwig1673f1f2015-11-26 10:54:19 +0100136static void nvme_free_ns(struct kref *kref)
137{
138 struct nvme_ns *ns = container_of(kref, struct nvme_ns, kref);
139
140 if (ns->type == NVME_NS_LIGHTNVM)
141 nvme_nvm_unregister(ns->queue, ns->disk->disk_name);
142
143 spin_lock(&dev_list_lock);
144 ns->disk->private_data = NULL;
145 spin_unlock(&dev_list_lock);
146
Christoph Hellwig1673f1f2015-11-26 10:54:19 +0100147 put_disk(ns->disk);
Keith Busch075790e2016-02-24 09:15:53 -0700148 ida_simple_remove(&ns->ctrl->ns_ida, ns->instance);
149 nvme_put_ctrl(ns->ctrl);
Christoph Hellwig1673f1f2015-11-26 10:54:19 +0100150 kfree(ns);
151}
152
Christoph Hellwig5bae7f72015-11-28 15:39:07 +0100153static void nvme_put_ns(struct nvme_ns *ns)
Christoph Hellwig1673f1f2015-11-26 10:54:19 +0100154{
155 kref_put(&ns->kref, nvme_free_ns);
156}
157
158static struct nvme_ns *nvme_get_ns_from_disk(struct gendisk *disk)
159{
160 struct nvme_ns *ns;
161
162 spin_lock(&dev_list_lock);
163 ns = disk->private_data;
Sagi Grimberge439bb12016-02-10 10:03:29 -0800164 if (ns) {
165 if (!kref_get_unless_zero(&ns->kref))
166 goto fail;
167 if (!try_module_get(ns->ctrl->ops->module))
168 goto fail_put_ns;
169 }
Christoph Hellwig1673f1f2015-11-26 10:54:19 +0100170 spin_unlock(&dev_list_lock);
171
172 return ns;
Sagi Grimberge439bb12016-02-10 10:03:29 -0800173
174fail_put_ns:
175 kref_put(&ns->kref, nvme_free_ns);
176fail:
177 spin_unlock(&dev_list_lock);
178 return NULL;
Christoph Hellwig1673f1f2015-11-26 10:54:19 +0100179}
180
Christoph Hellwig7688faa2015-11-28 15:41:58 +0100181void nvme_requeue_req(struct request *req)
182{
183 unsigned long flags;
184
185 blk_mq_requeue_request(req);
186 spin_lock_irqsave(req->q->queue_lock, flags);
187 if (!blk_queue_stopped(req->q))
188 blk_mq_kick_requeue_list(req->q);
189 spin_unlock_irqrestore(req->q->queue_lock, flags);
190}
Ming Lin576d55d2016-02-10 10:03:32 -0800191EXPORT_SYMBOL_GPL(nvme_requeue_req);
Christoph Hellwig7688faa2015-11-28 15:41:58 +0100192
Christoph Hellwig41609822015-11-20 09:00:02 +0100193struct request *nvme_alloc_request(struct request_queue *q,
194 struct nvme_command *cmd, unsigned int flags)
Christoph Hellwig21d34712015-11-26 09:08:36 +0100195{
196 bool write = cmd->common.opcode & 1;
Christoph Hellwig21d34712015-11-26 09:08:36 +0100197 struct request *req;
Christoph Hellwig21d34712015-11-26 09:08:36 +0100198
Christoph Hellwig41609822015-11-20 09:00:02 +0100199 req = blk_mq_alloc_request(q, write, flags);
Christoph Hellwig21d34712015-11-26 09:08:36 +0100200 if (IS_ERR(req))
Christoph Hellwig41609822015-11-20 09:00:02 +0100201 return req;
Christoph Hellwig21d34712015-11-26 09:08:36 +0100202
203 req->cmd_type = REQ_TYPE_DRV_PRIV;
204 req->cmd_flags |= REQ_FAILFAST_DRIVER;
205 req->__data_len = 0;
206 req->__sector = (sector_t) -1;
207 req->bio = req->biotail = NULL;
208
Christoph Hellwig21d34712015-11-26 09:08:36 +0100209 req->cmd = (unsigned char *)cmd;
210 req->cmd_len = sizeof(struct nvme_command);
Christoph Hellwig21d34712015-11-26 09:08:36 +0100211
Christoph Hellwig41609822015-11-20 09:00:02 +0100212 return req;
213}
Ming Lin576d55d2016-02-10 10:03:32 -0800214EXPORT_SYMBOL_GPL(nvme_alloc_request);
Christoph Hellwig41609822015-11-20 09:00:02 +0100215
Ming Lin8093f7c2016-04-12 13:10:14 -0600216static inline void nvme_setup_flush(struct nvme_ns *ns,
217 struct nvme_command *cmnd)
218{
219 memset(cmnd, 0, sizeof(*cmnd));
220 cmnd->common.opcode = nvme_cmd_flush;
221 cmnd->common.nsid = cpu_to_le32(ns->ns_id);
222}
223
224static inline int nvme_setup_discard(struct nvme_ns *ns, struct request *req,
225 struct nvme_command *cmnd)
226{
227 struct nvme_dsm_range *range;
228 struct page *page;
229 int offset;
230 unsigned int nr_bytes = blk_rq_bytes(req);
231
232 range = kmalloc(sizeof(*range), GFP_ATOMIC);
233 if (!range)
234 return BLK_MQ_RQ_QUEUE_BUSY;
235
236 range->cattr = cpu_to_le32(0);
237 range->nlb = cpu_to_le32(nr_bytes >> ns->lba_shift);
238 range->slba = cpu_to_le64(nvme_block_nr(ns, blk_rq_pos(req)));
239
240 memset(cmnd, 0, sizeof(*cmnd));
241 cmnd->dsm.opcode = nvme_cmd_dsm;
242 cmnd->dsm.nsid = cpu_to_le32(ns->ns_id);
243 cmnd->dsm.nr = 0;
244 cmnd->dsm.attributes = cpu_to_le32(NVME_DSMGMT_AD);
245
246 req->completion_data = range;
247 page = virt_to_page(range);
248 offset = offset_in_page(range);
249 blk_add_request_payload(req, page, offset, sizeof(*range));
250
251 /*
252 * we set __data_len back to the size of the area to be discarded
253 * on disk. This allows us to report completion on the full amount
254 * of blocks described by the request.
255 */
256 req->__data_len = nr_bytes;
257
258 return 0;
259}
260
261static inline void nvme_setup_rw(struct nvme_ns *ns, struct request *req,
262 struct nvme_command *cmnd)
263{
264 u16 control = 0;
265 u32 dsmgmt = 0;
266
267 if (req->cmd_flags & REQ_FUA)
268 control |= NVME_RW_FUA;
269 if (req->cmd_flags & (REQ_FAILFAST_DEV | REQ_RAHEAD))
270 control |= NVME_RW_LR;
271
272 if (req->cmd_flags & REQ_RAHEAD)
273 dsmgmt |= NVME_RW_DSM_FREQ_PREFETCH;
274
275 memset(cmnd, 0, sizeof(*cmnd));
276 cmnd->rw.opcode = (rq_data_dir(req) ? nvme_cmd_write : nvme_cmd_read);
277 cmnd->rw.command_id = req->tag;
278 cmnd->rw.nsid = cpu_to_le32(ns->ns_id);
279 cmnd->rw.slba = cpu_to_le64(nvme_block_nr(ns, blk_rq_pos(req)));
280 cmnd->rw.length = cpu_to_le16((blk_rq_bytes(req) >> ns->lba_shift) - 1);
281
282 if (ns->ms) {
283 switch (ns->pi_type) {
284 case NVME_NS_DPS_PI_TYPE3:
285 control |= NVME_RW_PRINFO_PRCHK_GUARD;
286 break;
287 case NVME_NS_DPS_PI_TYPE1:
288 case NVME_NS_DPS_PI_TYPE2:
289 control |= NVME_RW_PRINFO_PRCHK_GUARD |
290 NVME_RW_PRINFO_PRCHK_REF;
291 cmnd->rw.reftag = cpu_to_le32(
292 nvme_block_nr(ns, blk_rq_pos(req)));
293 break;
294 }
295 if (!blk_integrity_rq(req))
296 control |= NVME_RW_PRINFO_PRACT;
297 }
298
299 cmnd->rw.control = cpu_to_le16(control);
300 cmnd->rw.dsmgmt = cpu_to_le32(dsmgmt);
301}
302
303int nvme_setup_cmd(struct nvme_ns *ns, struct request *req,
304 struct nvme_command *cmd)
305{
306 int ret = 0;
307
308 if (req->cmd_type == REQ_TYPE_DRV_PRIV)
309 memcpy(cmd, req->cmd, sizeof(*cmd));
Mike Christie3a5e02c2016-06-05 14:32:23 -0500310 else if (req_op(req) == REQ_OP_FLUSH)
Ming Lin8093f7c2016-04-12 13:10:14 -0600311 nvme_setup_flush(ns, cmd);
Mike Christiec2df40d2016-06-05 14:32:17 -0500312 else if (req_op(req) == REQ_OP_DISCARD)
Ming Lin8093f7c2016-04-12 13:10:14 -0600313 ret = nvme_setup_discard(ns, req, cmd);
314 else
315 nvme_setup_rw(ns, req, cmd);
316
317 return ret;
318}
319EXPORT_SYMBOL_GPL(nvme_setup_cmd);
320
Christoph Hellwig41609822015-11-20 09:00:02 +0100321/*
322 * Returns 0 on success. If the result is negative, it's a Linux error code;
323 * if the result is positive, it's an NVM Express status code
324 */
325int __nvme_submit_sync_cmd(struct request_queue *q, struct nvme_command *cmd,
Christoph Hellwig1cb3cce2016-02-29 15:59:47 +0100326 struct nvme_completion *cqe, void *buffer, unsigned bufflen,
327 unsigned timeout)
Christoph Hellwig41609822015-11-20 09:00:02 +0100328{
329 struct request *req;
330 int ret;
331
332 req = nvme_alloc_request(q, cmd, 0);
333 if (IS_ERR(req))
334 return PTR_ERR(req);
335
336 req->timeout = timeout ? timeout : ADMIN_TIMEOUT;
Christoph Hellwig1cb3cce2016-02-29 15:59:47 +0100337 req->special = cqe;
Christoph Hellwig41609822015-11-20 09:00:02 +0100338
Christoph Hellwig21d34712015-11-26 09:08:36 +0100339 if (buffer && bufflen) {
340 ret = blk_rq_map_kern(q, req, buffer, bufflen, GFP_KERNEL);
341 if (ret)
342 goto out;
Christoph Hellwig41609822015-11-20 09:00:02 +0100343 }
344
345 blk_execute_rq(req->q, NULL, req, 0);
Christoph Hellwig41609822015-11-20 09:00:02 +0100346 ret = req->errors;
347 out:
348 blk_mq_free_request(req);
349 return ret;
350}
351
352int nvme_submit_sync_cmd(struct request_queue *q, struct nvme_command *cmd,
353 void *buffer, unsigned bufflen)
354{
Christoph Hellwig1cb3cce2016-02-29 15:59:47 +0100355 return __nvme_submit_sync_cmd(q, cmd, NULL, buffer, bufflen, 0);
Christoph Hellwig41609822015-11-20 09:00:02 +0100356}
Ming Lin576d55d2016-02-10 10:03:32 -0800357EXPORT_SYMBOL_GPL(nvme_submit_sync_cmd);
Christoph Hellwig41609822015-11-20 09:00:02 +0100358
Keith Busch0b7f1f22015-10-23 09:47:28 -0600359int __nvme_submit_user_cmd(struct request_queue *q, struct nvme_command *cmd,
360 void __user *ubuffer, unsigned bufflen,
361 void __user *meta_buffer, unsigned meta_len, u32 meta_seed,
362 u32 *result, unsigned timeout)
Christoph Hellwig41609822015-11-20 09:00:02 +0100363{
Keith Busch0b7f1f22015-10-23 09:47:28 -0600364 bool write = cmd->common.opcode & 1;
Christoph Hellwig1cb3cce2016-02-29 15:59:47 +0100365 struct nvme_completion cqe;
Keith Busch0b7f1f22015-10-23 09:47:28 -0600366 struct nvme_ns *ns = q->queuedata;
367 struct gendisk *disk = ns ? ns->disk : NULL;
Christoph Hellwig41609822015-11-20 09:00:02 +0100368 struct request *req;
Keith Busch0b7f1f22015-10-23 09:47:28 -0600369 struct bio *bio = NULL;
370 void *meta = NULL;
Christoph Hellwig41609822015-11-20 09:00:02 +0100371 int ret;
372
373 req = nvme_alloc_request(q, cmd, 0);
374 if (IS_ERR(req))
375 return PTR_ERR(req);
376
377 req->timeout = timeout ? timeout : ADMIN_TIMEOUT;
Christoph Hellwig1cb3cce2016-02-29 15:59:47 +0100378 req->special = &cqe;
Christoph Hellwig41609822015-11-20 09:00:02 +0100379
380 if (ubuffer && bufflen) {
Christoph Hellwig21d34712015-11-26 09:08:36 +0100381 ret = blk_rq_map_user(q, req, NULL, ubuffer, bufflen,
382 GFP_KERNEL);
383 if (ret)
384 goto out;
385 bio = req->bio;
Christoph Hellwig21d34712015-11-26 09:08:36 +0100386
Keith Busch0b7f1f22015-10-23 09:47:28 -0600387 if (!disk)
388 goto submit;
389 bio->bi_bdev = bdget_disk(disk, 0);
390 if (!bio->bi_bdev) {
391 ret = -ENODEV;
392 goto out_unmap;
393 }
394
Keith Busche9fc63d2016-02-24 09:15:58 -0700395 if (meta_buffer && meta_len) {
Keith Busch0b7f1f22015-10-23 09:47:28 -0600396 struct bio_integrity_payload *bip;
397
398 meta = kmalloc(meta_len, GFP_KERNEL);
399 if (!meta) {
400 ret = -ENOMEM;
401 goto out_unmap;
402 }
403
404 if (write) {
405 if (copy_from_user(meta, meta_buffer,
406 meta_len)) {
407 ret = -EFAULT;
408 goto out_free_meta;
409 }
410 }
411
412 bip = bio_integrity_alloc(bio, GFP_KERNEL, 1);
Keith Busch06c1e392015-12-03 09:32:21 -0700413 if (IS_ERR(bip)) {
414 ret = PTR_ERR(bip);
Keith Busch0b7f1f22015-10-23 09:47:28 -0600415 goto out_free_meta;
416 }
417
418 bip->bip_iter.bi_size = meta_len;
419 bip->bip_iter.bi_sector = meta_seed;
420
421 ret = bio_integrity_add_page(bio, virt_to_page(meta),
422 meta_len, offset_in_page(meta));
423 if (ret != meta_len) {
424 ret = -ENOMEM;
425 goto out_free_meta;
426 }
427 }
428 }
429 submit:
430 blk_execute_rq(req->q, disk, req, 0);
431 ret = req->errors;
Christoph Hellwig21d34712015-11-26 09:08:36 +0100432 if (result)
Christoph Hellwig1cb3cce2016-02-29 15:59:47 +0100433 *result = le32_to_cpu(cqe.result);
Keith Busch0b7f1f22015-10-23 09:47:28 -0600434 if (meta && !ret && !write) {
435 if (copy_to_user(meta_buffer, meta, meta_len))
436 ret = -EFAULT;
437 }
438 out_free_meta:
439 kfree(meta);
440 out_unmap:
441 if (bio) {
442 if (disk && bio->bi_bdev)
443 bdput(bio->bi_bdev);
444 blk_rq_unmap_user(bio);
445 }
Christoph Hellwig21d34712015-11-26 09:08:36 +0100446 out:
447 blk_mq_free_request(req);
448 return ret;
449}
450
Keith Busch0b7f1f22015-10-23 09:47:28 -0600451int nvme_submit_user_cmd(struct request_queue *q, struct nvme_command *cmd,
452 void __user *ubuffer, unsigned bufflen, u32 *result,
453 unsigned timeout)
454{
455 return __nvme_submit_user_cmd(q, cmd, ubuffer, bufflen, NULL, 0, 0,
456 result, timeout);
457}
458
Christoph Hellwig1c63dc62015-11-26 10:06:56 +0100459int nvme_identify_ctrl(struct nvme_ctrl *dev, struct nvme_id_ctrl **id)
Christoph Hellwig21d34712015-11-26 09:08:36 +0100460{
461 struct nvme_command c = { };
462 int error;
463
464 /* gcc-4.4.4 (at least) has issues with initializers and anon unions */
465 c.identify.opcode = nvme_admin_identify;
466 c.identify.cns = cpu_to_le32(1);
467
468 *id = kmalloc(sizeof(struct nvme_id_ctrl), GFP_KERNEL);
469 if (!*id)
470 return -ENOMEM;
471
472 error = nvme_submit_sync_cmd(dev->admin_q, &c, *id,
473 sizeof(struct nvme_id_ctrl));
474 if (error)
475 kfree(*id);
476 return error;
477}
478
Keith Busch540c8012015-10-22 15:45:06 -0600479static int nvme_identify_ns_list(struct nvme_ctrl *dev, unsigned nsid, __le32 *ns_list)
480{
481 struct nvme_command c = { };
482
483 c.identify.opcode = nvme_admin_identify;
484 c.identify.cns = cpu_to_le32(2);
485 c.identify.nsid = cpu_to_le32(nsid);
486 return nvme_submit_sync_cmd(dev->admin_q, &c, ns_list, 0x1000);
487}
488
Christoph Hellwig1c63dc62015-11-26 10:06:56 +0100489int nvme_identify_ns(struct nvme_ctrl *dev, unsigned nsid,
Christoph Hellwig21d34712015-11-26 09:08:36 +0100490 struct nvme_id_ns **id)
491{
492 struct nvme_command c = { };
493 int error;
494
495 /* gcc-4.4.4 (at least) has issues with initializers and anon unions */
496 c.identify.opcode = nvme_admin_identify,
497 c.identify.nsid = cpu_to_le32(nsid),
498
499 *id = kmalloc(sizeof(struct nvme_id_ns), GFP_KERNEL);
500 if (!*id)
501 return -ENOMEM;
502
503 error = nvme_submit_sync_cmd(dev->admin_q, &c, *id,
504 sizeof(struct nvme_id_ns));
505 if (error)
506 kfree(*id);
507 return error;
508}
509
Christoph Hellwig1c63dc62015-11-26 10:06:56 +0100510int nvme_get_features(struct nvme_ctrl *dev, unsigned fid, unsigned nsid,
Christoph Hellwig21d34712015-11-26 09:08:36 +0100511 dma_addr_t dma_addr, u32 *result)
512{
513 struct nvme_command c;
Christoph Hellwig1cb3cce2016-02-29 15:59:47 +0100514 struct nvme_completion cqe;
515 int ret;
Christoph Hellwig21d34712015-11-26 09:08:36 +0100516
517 memset(&c, 0, sizeof(c));
518 c.features.opcode = nvme_admin_get_features;
519 c.features.nsid = cpu_to_le32(nsid);
520 c.features.prp1 = cpu_to_le64(dma_addr);
521 c.features.fid = cpu_to_le32(fid);
522
Christoph Hellwig1cb3cce2016-02-29 15:59:47 +0100523 ret = __nvme_submit_sync_cmd(dev->admin_q, &c, &cqe, NULL, 0, 0);
524 if (ret >= 0)
525 *result = le32_to_cpu(cqe.result);
526 return ret;
Christoph Hellwig21d34712015-11-26 09:08:36 +0100527}
528
Christoph Hellwig1c63dc62015-11-26 10:06:56 +0100529int nvme_set_features(struct nvme_ctrl *dev, unsigned fid, unsigned dword11,
Christoph Hellwig21d34712015-11-26 09:08:36 +0100530 dma_addr_t dma_addr, u32 *result)
531{
532 struct nvme_command c;
Christoph Hellwig1cb3cce2016-02-29 15:59:47 +0100533 struct nvme_completion cqe;
534 int ret;
Christoph Hellwig21d34712015-11-26 09:08:36 +0100535
536 memset(&c, 0, sizeof(c));
537 c.features.opcode = nvme_admin_set_features;
538 c.features.prp1 = cpu_to_le64(dma_addr);
539 c.features.fid = cpu_to_le32(fid);
540 c.features.dword11 = cpu_to_le32(dword11);
541
Christoph Hellwig1cb3cce2016-02-29 15:59:47 +0100542 ret = __nvme_submit_sync_cmd(dev->admin_q, &c, &cqe, NULL, 0, 0);
543 if (ret >= 0)
544 *result = le32_to_cpu(cqe.result);
545 return ret;
Christoph Hellwig21d34712015-11-26 09:08:36 +0100546}
547
Christoph Hellwig1c63dc62015-11-26 10:06:56 +0100548int nvme_get_log_page(struct nvme_ctrl *dev, struct nvme_smart_log **log)
Christoph Hellwig21d34712015-11-26 09:08:36 +0100549{
550 struct nvme_command c = { };
551 int error;
552
553 c.common.opcode = nvme_admin_get_log_page,
554 c.common.nsid = cpu_to_le32(0xFFFFFFFF),
555 c.common.cdw10[0] = cpu_to_le32(
556 (((sizeof(struct nvme_smart_log) / 4) - 1) << 16) |
557 NVME_LOG_SMART),
558
559 *log = kmalloc(sizeof(struct nvme_smart_log), GFP_KERNEL);
560 if (!*log)
561 return -ENOMEM;
562
563 error = nvme_submit_sync_cmd(dev->admin_q, &c, *log,
564 sizeof(struct nvme_smart_log));
565 if (error)
566 kfree(*log);
567 return error;
568}
Christoph Hellwig1673f1f2015-11-26 10:54:19 +0100569
Christoph Hellwig9a0be7a2015-11-26 11:09:06 +0100570int nvme_set_queue_count(struct nvme_ctrl *ctrl, int *count)
571{
572 u32 q_count = (*count - 1) | ((*count - 1) << 16);
573 u32 result;
574 int status, nr_io_queues;
575
576 status = nvme_set_features(ctrl, NVME_FEAT_NUM_QUEUES, q_count, 0,
577 &result);
578 if (status)
579 return status;
580
581 nr_io_queues = min(result & 0xffff, result >> 16) + 1;
582 *count = min(*count, nr_io_queues);
583 return 0;
584}
Ming Lin576d55d2016-02-10 10:03:32 -0800585EXPORT_SYMBOL_GPL(nvme_set_queue_count);
Christoph Hellwig9a0be7a2015-11-26 11:09:06 +0100586
Christoph Hellwig1673f1f2015-11-26 10:54:19 +0100587static int nvme_submit_io(struct nvme_ns *ns, struct nvme_user_io __user *uio)
588{
589 struct nvme_user_io io;
590 struct nvme_command c;
591 unsigned length, meta_len;
592 void __user *metadata;
593
594 if (copy_from_user(&io, uio, sizeof(io)))
595 return -EFAULT;
Keith Busch63088ec2016-02-24 09:15:57 -0700596 if (io.flags)
597 return -EINVAL;
Christoph Hellwig1673f1f2015-11-26 10:54:19 +0100598
599 switch (io.opcode) {
600 case nvme_cmd_write:
601 case nvme_cmd_read:
602 case nvme_cmd_compare:
603 break;
604 default:
605 return -EINVAL;
606 }
607
608 length = (io.nblocks + 1) << ns->lba_shift;
609 meta_len = (io.nblocks + 1) * ns->ms;
610 metadata = (void __user *)(uintptr_t)io.metadata;
611
612 if (ns->ext) {
613 length += meta_len;
614 meta_len = 0;
615 } else if (meta_len) {
616 if ((io.metadata & 3) || !io.metadata)
617 return -EINVAL;
618 }
619
620 memset(&c, 0, sizeof(c));
621 c.rw.opcode = io.opcode;
622 c.rw.flags = io.flags;
623 c.rw.nsid = cpu_to_le32(ns->ns_id);
624 c.rw.slba = cpu_to_le64(io.slba);
625 c.rw.length = cpu_to_le16(io.nblocks);
626 c.rw.control = cpu_to_le16(io.control);
627 c.rw.dsmgmt = cpu_to_le32(io.dsmgmt);
628 c.rw.reftag = cpu_to_le32(io.reftag);
629 c.rw.apptag = cpu_to_le16(io.apptag);
630 c.rw.appmask = cpu_to_le16(io.appmask);
631
632 return __nvme_submit_user_cmd(ns->queue, &c,
633 (void __user *)(uintptr_t)io.addr, length,
634 metadata, meta_len, io.slba, NULL, 0);
635}
636
Christoph Hellwigf3ca80f2015-11-28 15:40:19 +0100637static int nvme_user_cmd(struct nvme_ctrl *ctrl, struct nvme_ns *ns,
Christoph Hellwig1673f1f2015-11-26 10:54:19 +0100638 struct nvme_passthru_cmd __user *ucmd)
639{
640 struct nvme_passthru_cmd cmd;
641 struct nvme_command c;
642 unsigned timeout = 0;
643 int status;
644
645 if (!capable(CAP_SYS_ADMIN))
646 return -EACCES;
647 if (copy_from_user(&cmd, ucmd, sizeof(cmd)))
648 return -EFAULT;
Keith Busch63088ec2016-02-24 09:15:57 -0700649 if (cmd.flags)
650 return -EINVAL;
Christoph Hellwig1673f1f2015-11-26 10:54:19 +0100651
652 memset(&c, 0, sizeof(c));
653 c.common.opcode = cmd.opcode;
654 c.common.flags = cmd.flags;
655 c.common.nsid = cpu_to_le32(cmd.nsid);
656 c.common.cdw2[0] = cpu_to_le32(cmd.cdw2);
657 c.common.cdw2[1] = cpu_to_le32(cmd.cdw3);
658 c.common.cdw10[0] = cpu_to_le32(cmd.cdw10);
659 c.common.cdw10[1] = cpu_to_le32(cmd.cdw11);
660 c.common.cdw10[2] = cpu_to_le32(cmd.cdw12);
661 c.common.cdw10[3] = cpu_to_le32(cmd.cdw13);
662 c.common.cdw10[4] = cpu_to_le32(cmd.cdw14);
663 c.common.cdw10[5] = cpu_to_le32(cmd.cdw15);
664
665 if (cmd.timeout_ms)
666 timeout = msecs_to_jiffies(cmd.timeout_ms);
667
668 status = nvme_submit_user_cmd(ns ? ns->queue : ctrl->admin_q, &c,
Arnd Bergmannd1ea7be2015-12-08 16:22:17 +0100669 (void __user *)(uintptr_t)cmd.addr, cmd.data_len,
Christoph Hellwig1673f1f2015-11-26 10:54:19 +0100670 &cmd.result, timeout);
671 if (status >= 0) {
672 if (put_user(cmd.result, &ucmd->result))
673 return -EFAULT;
674 }
675
676 return status;
677}
678
679static int nvme_ioctl(struct block_device *bdev, fmode_t mode,
680 unsigned int cmd, unsigned long arg)
681{
682 struct nvme_ns *ns = bdev->bd_disk->private_data;
683
684 switch (cmd) {
685 case NVME_IOCTL_ID:
686 force_successful_syscall_return();
687 return ns->ns_id;
688 case NVME_IOCTL_ADMIN_CMD:
689 return nvme_user_cmd(ns->ctrl, NULL, (void __user *)arg);
690 case NVME_IOCTL_IO_CMD:
691 return nvme_user_cmd(ns->ctrl, ns, (void __user *)arg);
692 case NVME_IOCTL_SUBMIT_IO:
693 return nvme_submit_io(ns, (void __user *)arg);
Christoph Hellwig44907332015-12-24 15:27:02 +0100694#ifdef CONFIG_BLK_DEV_NVME_SCSI
Christoph Hellwig1673f1f2015-11-26 10:54:19 +0100695 case SG_GET_VERSION_NUM:
696 return nvme_sg_get_version_num((void __user *)arg);
697 case SG_IO:
698 return nvme_sg_io(ns, (void __user *)arg);
Christoph Hellwig44907332015-12-24 15:27:02 +0100699#endif
Christoph Hellwig1673f1f2015-11-26 10:54:19 +0100700 default:
701 return -ENOTTY;
702 }
703}
704
705#ifdef CONFIG_COMPAT
706static int nvme_compat_ioctl(struct block_device *bdev, fmode_t mode,
707 unsigned int cmd, unsigned long arg)
708{
709 switch (cmd) {
710 case SG_IO:
711 return -ENOIOCTLCMD;
712 }
713 return nvme_ioctl(bdev, mode, cmd, arg);
714}
715#else
716#define nvme_compat_ioctl NULL
717#endif
718
719static int nvme_open(struct block_device *bdev, fmode_t mode)
720{
721 return nvme_get_ns_from_disk(bdev->bd_disk) ? 0 : -ENXIO;
722}
723
724static void nvme_release(struct gendisk *disk, fmode_t mode)
725{
Sagi Grimberge439bb12016-02-10 10:03:29 -0800726 struct nvme_ns *ns = disk->private_data;
727
728 module_put(ns->ctrl->ops->module);
729 nvme_put_ns(ns);
Christoph Hellwig1673f1f2015-11-26 10:54:19 +0100730}
731
732static int nvme_getgeo(struct block_device *bdev, struct hd_geometry *geo)
733{
734 /* some standard values */
735 geo->heads = 1 << 6;
736 geo->sectors = 1 << 5;
737 geo->cylinders = get_capacity(bdev->bd_disk) >> 11;
738 return 0;
739}
740
741#ifdef CONFIG_BLK_DEV_INTEGRITY
742static void nvme_init_integrity(struct nvme_ns *ns)
743{
744 struct blk_integrity integrity;
745
746 switch (ns->pi_type) {
747 case NVME_NS_DPS_PI_TYPE3:
748 integrity.profile = &t10_pi_type3_crc;
Nicholas Bellingerba36c212016-04-09 03:04:42 +0000749 integrity.tag_size = sizeof(u16) + sizeof(u32);
750 integrity.flags |= BLK_INTEGRITY_DEVICE_CAPABLE;
Christoph Hellwig1673f1f2015-11-26 10:54:19 +0100751 break;
752 case NVME_NS_DPS_PI_TYPE1:
753 case NVME_NS_DPS_PI_TYPE2:
754 integrity.profile = &t10_pi_type1_crc;
Nicholas Bellingerba36c212016-04-09 03:04:42 +0000755 integrity.tag_size = sizeof(u16);
756 integrity.flags |= BLK_INTEGRITY_DEVICE_CAPABLE;
Christoph Hellwig1673f1f2015-11-26 10:54:19 +0100757 break;
758 default:
759 integrity.profile = NULL;
760 break;
761 }
762 integrity.tuple_size = ns->ms;
763 blk_integrity_register(ns->disk, &integrity);
764 blk_queue_max_integrity_segments(ns->queue, 1);
765}
766#else
767static void nvme_init_integrity(struct nvme_ns *ns)
768{
769}
770#endif /* CONFIG_BLK_DEV_INTEGRITY */
771
772static void nvme_config_discard(struct nvme_ns *ns)
773{
Keith Busch08095e72016-03-04 13:15:17 -0700774 struct nvme_ctrl *ctrl = ns->ctrl;
Christoph Hellwig1673f1f2015-11-26 10:54:19 +0100775 u32 logical_block_size = queue_logical_block_size(ns->queue);
Keith Busch08095e72016-03-04 13:15:17 -0700776
777 if (ctrl->quirks & NVME_QUIRK_DISCARD_ZEROES)
778 ns->queue->limits.discard_zeroes_data = 1;
779 else
780 ns->queue->limits.discard_zeroes_data = 0;
781
Christoph Hellwig1673f1f2015-11-26 10:54:19 +0100782 ns->queue->limits.discard_alignment = logical_block_size;
783 ns->queue->limits.discard_granularity = logical_block_size;
Minfei Huangbd0fc282016-05-17 15:58:41 +0800784 blk_queue_max_discard_sectors(ns->queue, UINT_MAX);
Christoph Hellwig1673f1f2015-11-26 10:54:19 +0100785 queue_flag_set_unlocked(QUEUE_FLAG_DISCARD, ns->queue);
786}
787
Christoph Hellwig5bae7f72015-11-28 15:39:07 +0100788static int nvme_revalidate_disk(struct gendisk *disk)
Christoph Hellwig1673f1f2015-11-26 10:54:19 +0100789{
790 struct nvme_ns *ns = disk->private_data;
791 struct nvme_id_ns *id;
792 u8 lbaf, pi_type;
793 u16 old_ms;
794 unsigned short bs;
795
Keith Busch69d9a992016-02-24 09:15:56 -0700796 if (test_bit(NVME_NS_DEAD, &ns->flags)) {
797 set_capacity(disk, 0);
798 return -ENODEV;
799 }
Christoph Hellwig1673f1f2015-11-26 10:54:19 +0100800 if (nvme_identify_ns(ns->ctrl, ns->ns_id, &id)) {
Sagi Grimberg1b3c47c2016-02-10 08:51:15 -0700801 dev_warn(disk_to_dev(ns->disk), "%s: Identify failure\n",
802 __func__);
Christoph Hellwig1673f1f2015-11-26 10:54:19 +0100803 return -ENODEV;
804 }
805 if (id->ncap == 0) {
806 kfree(id);
807 return -ENODEV;
808 }
809
810 if (nvme_nvm_ns_supported(ns, id) && ns->type != NVME_NS_LIGHTNVM) {
811 if (nvme_nvm_register(ns->queue, disk->disk_name)) {
Sagi Grimberg1b3c47c2016-02-10 08:51:15 -0700812 dev_warn(disk_to_dev(ns->disk),
Christoph Hellwig1673f1f2015-11-26 10:54:19 +0100813 "%s: LightNVM init failure\n", __func__);
814 kfree(id);
815 return -ENODEV;
816 }
817 ns->type = NVME_NS_LIGHTNVM;
818 }
819
Keith Busch2b9b6e82015-12-22 10:10:45 -0700820 if (ns->ctrl->vs >= NVME_VS(1, 1))
821 memcpy(ns->eui, id->eui64, sizeof(ns->eui));
822 if (ns->ctrl->vs >= NVME_VS(1, 2))
823 memcpy(ns->uuid, id->nguid, sizeof(ns->uuid));
824
Christoph Hellwig1673f1f2015-11-26 10:54:19 +0100825 old_ms = ns->ms;
826 lbaf = id->flbas & NVME_NS_FLBAS_LBA_MASK;
827 ns->lba_shift = id->lbaf[lbaf].ds;
828 ns->ms = le16_to_cpu(id->lbaf[lbaf].ms);
829 ns->ext = ns->ms && (id->flbas & NVME_NS_FLBAS_META_EXT);
830
831 /*
832 * If identify namespace failed, use default 512 byte block size so
833 * block layer can use before failing read/write for 0 capacity.
834 */
835 if (ns->lba_shift == 0)
836 ns->lba_shift = 9;
837 bs = 1 << ns->lba_shift;
Christoph Hellwig1673f1f2015-11-26 10:54:19 +0100838 /* XXX: PI implementation requires metadata equal t10 pi tuple size */
839 pi_type = ns->ms == sizeof(struct t10_pi_tuple) ?
840 id->dps & NVME_NS_DPS_PI_MASK : 0;
841
842 blk_mq_freeze_queue(disk->queue);
843 if (blk_get_integrity(disk) && (ns->pi_type != pi_type ||
844 ns->ms != old_ms ||
845 bs != queue_logical_block_size(disk->queue) ||
846 (ns->ms && ns->ext)))
847 blk_integrity_unregister(disk);
848
849 ns->pi_type = pi_type;
850 blk_queue_logical_block_size(ns->queue, bs);
851
Keith Busch4b9d5b12015-11-20 09:13:30 +0100852 if (ns->ms && !blk_get_integrity(disk) && !ns->ext)
Christoph Hellwig1673f1f2015-11-26 10:54:19 +0100853 nvme_init_integrity(ns);
Christoph Hellwig1673f1f2015-11-26 10:54:19 +0100854 if (ns->ms && !(ns->ms == 8 && ns->pi_type) && !blk_get_integrity(disk))
855 set_capacity(disk, 0);
856 else
857 set_capacity(disk, le64_to_cpup(&id->nsze) << (ns->lba_shift - 9));
858
859 if (ns->ctrl->oncs & NVME_CTRL_ONCS_DSM)
860 nvme_config_discard(ns);
861 blk_mq_unfreeze_queue(disk->queue);
862
863 kfree(id);
864 return 0;
865}
866
867static char nvme_pr_type(enum pr_type type)
868{
869 switch (type) {
870 case PR_WRITE_EXCLUSIVE:
871 return 1;
872 case PR_EXCLUSIVE_ACCESS:
873 return 2;
874 case PR_WRITE_EXCLUSIVE_REG_ONLY:
875 return 3;
876 case PR_EXCLUSIVE_ACCESS_REG_ONLY:
877 return 4;
878 case PR_WRITE_EXCLUSIVE_ALL_REGS:
879 return 5;
880 case PR_EXCLUSIVE_ACCESS_ALL_REGS:
881 return 6;
882 default:
883 return 0;
884 }
885};
886
887static int nvme_pr_command(struct block_device *bdev, u32 cdw10,
888 u64 key, u64 sa_key, u8 op)
889{
890 struct nvme_ns *ns = bdev->bd_disk->private_data;
891 struct nvme_command c;
892 u8 data[16] = { 0, };
893
894 put_unaligned_le64(key, &data[0]);
895 put_unaligned_le64(sa_key, &data[8]);
896
897 memset(&c, 0, sizeof(c));
898 c.common.opcode = op;
899 c.common.nsid = cpu_to_le32(ns->ns_id);
900 c.common.cdw10[0] = cpu_to_le32(cdw10);
901
902 return nvme_submit_sync_cmd(ns->queue, &c, data, 16);
903}
904
905static int nvme_pr_register(struct block_device *bdev, u64 old,
906 u64 new, unsigned flags)
907{
908 u32 cdw10;
909
910 if (flags & ~PR_FL_IGNORE_KEY)
911 return -EOPNOTSUPP;
912
913 cdw10 = old ? 2 : 0;
914 cdw10 |= (flags & PR_FL_IGNORE_KEY) ? 1 << 3 : 0;
915 cdw10 |= (1 << 30) | (1 << 31); /* PTPL=1 */
916 return nvme_pr_command(bdev, cdw10, old, new, nvme_cmd_resv_register);
917}
918
919static int nvme_pr_reserve(struct block_device *bdev, u64 key,
920 enum pr_type type, unsigned flags)
921{
922 u32 cdw10;
923
924 if (flags & ~PR_FL_IGNORE_KEY)
925 return -EOPNOTSUPP;
926
927 cdw10 = nvme_pr_type(type) << 8;
928 cdw10 |= ((flags & PR_FL_IGNORE_KEY) ? 1 << 3 : 0);
929 return nvme_pr_command(bdev, cdw10, key, 0, nvme_cmd_resv_acquire);
930}
931
932static int nvme_pr_preempt(struct block_device *bdev, u64 old, u64 new,
933 enum pr_type type, bool abort)
934{
935 u32 cdw10 = nvme_pr_type(type) << 8 | abort ? 2 : 1;
936 return nvme_pr_command(bdev, cdw10, old, new, nvme_cmd_resv_acquire);
937}
938
939static int nvme_pr_clear(struct block_device *bdev, u64 key)
940{
Dan Carpenter8c0b3912015-12-09 13:24:06 +0300941 u32 cdw10 = 1 | (key ? 1 << 3 : 0);
Christoph Hellwig1673f1f2015-11-26 10:54:19 +0100942 return nvme_pr_command(bdev, cdw10, key, 0, nvme_cmd_resv_register);
943}
944
945static int nvme_pr_release(struct block_device *bdev, u64 key, enum pr_type type)
946{
947 u32 cdw10 = nvme_pr_type(type) << 8 | key ? 1 << 3 : 0;
948 return nvme_pr_command(bdev, cdw10, key, 0, nvme_cmd_resv_release);
949}
950
951static const struct pr_ops nvme_pr_ops = {
952 .pr_register = nvme_pr_register,
953 .pr_reserve = nvme_pr_reserve,
954 .pr_release = nvme_pr_release,
955 .pr_preempt = nvme_pr_preempt,
956 .pr_clear = nvme_pr_clear,
957};
958
Christoph Hellwig5bae7f72015-11-28 15:39:07 +0100959static const struct block_device_operations nvme_fops = {
Christoph Hellwig1673f1f2015-11-26 10:54:19 +0100960 .owner = THIS_MODULE,
961 .ioctl = nvme_ioctl,
962 .compat_ioctl = nvme_compat_ioctl,
963 .open = nvme_open,
964 .release = nvme_release,
965 .getgeo = nvme_getgeo,
966 .revalidate_disk= nvme_revalidate_disk,
967 .pr_ops = &nvme_pr_ops,
968};
969
Christoph Hellwig5fd4ce12015-11-28 15:03:49 +0100970static int nvme_wait_ready(struct nvme_ctrl *ctrl, u64 cap, bool enabled)
971{
972 unsigned long timeout =
973 ((NVME_CAP_TIMEOUT(cap) + 1) * HZ / 2) + jiffies;
974 u32 csts, bit = enabled ? NVME_CSTS_RDY : 0;
975 int ret;
976
977 while ((ret = ctrl->ops->reg_read32(ctrl, NVME_REG_CSTS, &csts)) == 0) {
978 if ((csts & NVME_CSTS_RDY) == bit)
979 break;
980
981 msleep(100);
982 if (fatal_signal_pending(current))
983 return -EINTR;
984 if (time_after(jiffies, timeout)) {
Sagi Grimberg1b3c47c2016-02-10 08:51:15 -0700985 dev_err(ctrl->device,
Christoph Hellwig5fd4ce12015-11-28 15:03:49 +0100986 "Device not ready; aborting %s\n", enabled ?
987 "initialisation" : "reset");
988 return -ENODEV;
989 }
990 }
991
992 return ret;
993}
994
995/*
996 * If the device has been passed off to us in an enabled state, just clear
997 * the enabled bit. The spec says we should set the 'shutdown notification
998 * bits', but doing so may cause the device to complete commands to the
999 * admin queue ... and we don't know what memory that might be pointing at!
1000 */
1001int nvme_disable_ctrl(struct nvme_ctrl *ctrl, u64 cap)
1002{
1003 int ret;
1004
1005 ctrl->ctrl_config &= ~NVME_CC_SHN_MASK;
1006 ctrl->ctrl_config &= ~NVME_CC_ENABLE;
1007
1008 ret = ctrl->ops->reg_write32(ctrl, NVME_REG_CC, ctrl->ctrl_config);
1009 if (ret)
1010 return ret;
1011 return nvme_wait_ready(ctrl, cap, false);
1012}
Ming Lin576d55d2016-02-10 10:03:32 -08001013EXPORT_SYMBOL_GPL(nvme_disable_ctrl);
Christoph Hellwig5fd4ce12015-11-28 15:03:49 +01001014
1015int nvme_enable_ctrl(struct nvme_ctrl *ctrl, u64 cap)
1016{
1017 /*
1018 * Default to a 4K page size, with the intention to update this
1019 * path in the future to accomodate architectures with differing
1020 * kernel and IO page sizes.
1021 */
1022 unsigned dev_page_min = NVME_CAP_MPSMIN(cap) + 12, page_shift = 12;
1023 int ret;
1024
1025 if (page_shift < dev_page_min) {
Sagi Grimberg1b3c47c2016-02-10 08:51:15 -07001026 dev_err(ctrl->device,
Christoph Hellwig5fd4ce12015-11-28 15:03:49 +01001027 "Minimum device page size %u too large for host (%u)\n",
1028 1 << dev_page_min, 1 << page_shift);
1029 return -ENODEV;
1030 }
1031
1032 ctrl->page_size = 1 << page_shift;
1033
1034 ctrl->ctrl_config = NVME_CC_CSS_NVM;
1035 ctrl->ctrl_config |= (page_shift - 12) << NVME_CC_MPS_SHIFT;
1036 ctrl->ctrl_config |= NVME_CC_ARB_RR | NVME_CC_SHN_NONE;
1037 ctrl->ctrl_config |= NVME_CC_IOSQES | NVME_CC_IOCQES;
1038 ctrl->ctrl_config |= NVME_CC_ENABLE;
1039
1040 ret = ctrl->ops->reg_write32(ctrl, NVME_REG_CC, ctrl->ctrl_config);
1041 if (ret)
1042 return ret;
1043 return nvme_wait_ready(ctrl, cap, true);
1044}
Ming Lin576d55d2016-02-10 10:03:32 -08001045EXPORT_SYMBOL_GPL(nvme_enable_ctrl);
Christoph Hellwig5fd4ce12015-11-28 15:03:49 +01001046
1047int nvme_shutdown_ctrl(struct nvme_ctrl *ctrl)
1048{
1049 unsigned long timeout = SHUTDOWN_TIMEOUT + jiffies;
1050 u32 csts;
1051 int ret;
1052
1053 ctrl->ctrl_config &= ~NVME_CC_SHN_MASK;
1054 ctrl->ctrl_config |= NVME_CC_SHN_NORMAL;
1055
1056 ret = ctrl->ops->reg_write32(ctrl, NVME_REG_CC, ctrl->ctrl_config);
1057 if (ret)
1058 return ret;
1059
1060 while ((ret = ctrl->ops->reg_read32(ctrl, NVME_REG_CSTS, &csts)) == 0) {
1061 if ((csts & NVME_CSTS_SHST_MASK) == NVME_CSTS_SHST_CMPLT)
1062 break;
1063
1064 msleep(100);
1065 if (fatal_signal_pending(current))
1066 return -EINTR;
1067 if (time_after(jiffies, timeout)) {
Sagi Grimberg1b3c47c2016-02-10 08:51:15 -07001068 dev_err(ctrl->device,
Christoph Hellwig5fd4ce12015-11-28 15:03:49 +01001069 "Device shutdown incomplete; abort shutdown\n");
1070 return -ENODEV;
1071 }
1072 }
1073
1074 return ret;
1075}
Ming Lin576d55d2016-02-10 10:03:32 -08001076EXPORT_SYMBOL_GPL(nvme_shutdown_ctrl);
Christoph Hellwig5fd4ce12015-11-28 15:03:49 +01001077
Christoph Hellwigda358252016-03-02 18:07:11 +01001078static void nvme_set_queue_limits(struct nvme_ctrl *ctrl,
1079 struct request_queue *q)
1080{
Jens Axboe7c88cb02016-04-12 15:43:09 -06001081 bool vwc = false;
1082
Christoph Hellwigda358252016-03-02 18:07:11 +01001083 if (ctrl->max_hw_sectors) {
Christoph Hellwig45686b62016-03-02 18:07:12 +01001084 u32 max_segments =
1085 (ctrl->max_hw_sectors / (ctrl->page_size >> 9)) + 1;
1086
Christoph Hellwigda358252016-03-02 18:07:11 +01001087 blk_queue_max_hw_sectors(q, ctrl->max_hw_sectors);
Christoph Hellwig45686b62016-03-02 18:07:12 +01001088 blk_queue_max_segments(q, min_t(u32, max_segments, USHRT_MAX));
Christoph Hellwigda358252016-03-02 18:07:11 +01001089 }
1090 if (ctrl->stripe_size)
1091 blk_queue_chunk_sectors(q, ctrl->stripe_size >> 9);
Christoph Hellwigda358252016-03-02 18:07:11 +01001092 blk_queue_virt_boundary(q, ctrl->page_size - 1);
Jens Axboe7c88cb02016-04-12 15:43:09 -06001093 if (ctrl->vwc & NVME_CTRL_VWC_PRESENT)
1094 vwc = true;
1095 blk_queue_write_cache(q, vwc, vwc);
Christoph Hellwigda358252016-03-02 18:07:11 +01001096}
1097
Christoph Hellwig7fd89302015-11-28 15:37:52 +01001098/*
1099 * Initialize the cached copies of the Identify data and various controller
1100 * register in our nvme_ctrl structure. This should be called as soon as
1101 * the admin queue is fully up and running.
1102 */
1103int nvme_init_identify(struct nvme_ctrl *ctrl)
1104{
1105 struct nvme_id_ctrl *id;
1106 u64 cap;
1107 int ret, page_shift;
Christoph Hellwiga229dbf2016-06-06 23:20:48 +02001108 u32 max_hw_sectors;
Christoph Hellwig7fd89302015-11-28 15:37:52 +01001109
Christoph Hellwigf3ca80f2015-11-28 15:40:19 +01001110 ret = ctrl->ops->reg_read32(ctrl, NVME_REG_VS, &ctrl->vs);
1111 if (ret) {
Sagi Grimberg1b3c47c2016-02-10 08:51:15 -07001112 dev_err(ctrl->device, "Reading VS failed (%d)\n", ret);
Christoph Hellwigf3ca80f2015-11-28 15:40:19 +01001113 return ret;
1114 }
1115
Christoph Hellwig7fd89302015-11-28 15:37:52 +01001116 ret = ctrl->ops->reg_read64(ctrl, NVME_REG_CAP, &cap);
1117 if (ret) {
Sagi Grimberg1b3c47c2016-02-10 08:51:15 -07001118 dev_err(ctrl->device, "Reading CAP failed (%d)\n", ret);
Christoph Hellwig7fd89302015-11-28 15:37:52 +01001119 return ret;
1120 }
1121 page_shift = NVME_CAP_MPSMIN(cap) + 12;
1122
Christoph Hellwigf3ca80f2015-11-28 15:40:19 +01001123 if (ctrl->vs >= NVME_VS(1, 1))
1124 ctrl->subsystem = NVME_CAP_NSSRC(cap);
1125
Christoph Hellwig7fd89302015-11-28 15:37:52 +01001126 ret = nvme_identify_ctrl(ctrl, &id);
1127 if (ret) {
Sagi Grimberg1b3c47c2016-02-10 08:51:15 -07001128 dev_err(ctrl->device, "Identify Controller failed (%d)\n", ret);
Christoph Hellwig7fd89302015-11-28 15:37:52 +01001129 return -EIO;
1130 }
1131
Keith Busch118472a2016-02-18 09:57:48 -07001132 ctrl->vid = le16_to_cpu(id->vid);
Christoph Hellwig7fd89302015-11-28 15:37:52 +01001133 ctrl->oncs = le16_to_cpup(&id->oncs);
Christoph Hellwig6bf25d12015-11-20 09:36:44 +01001134 atomic_set(&ctrl->abort_limit, id->acl + 1);
Christoph Hellwig7fd89302015-11-28 15:37:52 +01001135 ctrl->vwc = id->vwc;
Ming Lin931e1c22016-02-26 13:24:19 -08001136 ctrl->cntlid = le16_to_cpup(&id->cntlid);
Christoph Hellwig7fd89302015-11-28 15:37:52 +01001137 memcpy(ctrl->serial, id->sn, sizeof(id->sn));
1138 memcpy(ctrl->model, id->mn, sizeof(id->mn));
1139 memcpy(ctrl->firmware_rev, id->fr, sizeof(id->fr));
1140 if (id->mdts)
Christoph Hellwiga229dbf2016-06-06 23:20:48 +02001141 max_hw_sectors = 1 << (id->mdts + page_shift - 9);
Christoph Hellwig7fd89302015-11-28 15:37:52 +01001142 else
Christoph Hellwiga229dbf2016-06-06 23:20:48 +02001143 max_hw_sectors = UINT_MAX;
1144 ctrl->max_hw_sectors =
1145 min_not_zero(ctrl->max_hw_sectors, max_hw_sectors);
Christoph Hellwig7fd89302015-11-28 15:37:52 +01001146
1147 if ((ctrl->quirks & NVME_QUIRK_STRIPE_SIZE) && id->vs[3]) {
1148 unsigned int max_hw_sectors;
1149
1150 ctrl->stripe_size = 1 << (id->vs[3] + page_shift);
1151 max_hw_sectors = ctrl->stripe_size >> (page_shift - 9);
1152 if (ctrl->max_hw_sectors) {
1153 ctrl->max_hw_sectors = min(max_hw_sectors,
1154 ctrl->max_hw_sectors);
1155 } else {
1156 ctrl->max_hw_sectors = max_hw_sectors;
1157 }
1158 }
1159
Christoph Hellwigda358252016-03-02 18:07:11 +01001160 nvme_set_queue_limits(ctrl, ctrl->admin_q);
1161
Christoph Hellwig7fd89302015-11-28 15:37:52 +01001162 kfree(id);
1163 return 0;
1164}
Ming Lin576d55d2016-02-10 10:03:32 -08001165EXPORT_SYMBOL_GPL(nvme_init_identify);
Christoph Hellwig7fd89302015-11-28 15:37:52 +01001166
Christoph Hellwigf3ca80f2015-11-28 15:40:19 +01001167static int nvme_dev_open(struct inode *inode, struct file *file)
Christoph Hellwig1673f1f2015-11-26 10:54:19 +01001168{
Christoph Hellwigf3ca80f2015-11-28 15:40:19 +01001169 struct nvme_ctrl *ctrl;
1170 int instance = iminor(inode);
1171 int ret = -ENODEV;
Christoph Hellwig1673f1f2015-11-26 10:54:19 +01001172
Christoph Hellwigf3ca80f2015-11-28 15:40:19 +01001173 spin_lock(&dev_list_lock);
1174 list_for_each_entry(ctrl, &nvme_ctrl_list, node) {
1175 if (ctrl->instance != instance)
1176 continue;
1177
1178 if (!ctrl->admin_q) {
1179 ret = -EWOULDBLOCK;
1180 break;
1181 }
1182 if (!kref_get_unless_zero(&ctrl->kref))
1183 break;
1184 file->private_data = ctrl;
1185 ret = 0;
1186 break;
1187 }
1188 spin_unlock(&dev_list_lock);
1189
1190 return ret;
Christoph Hellwig1673f1f2015-11-26 10:54:19 +01001191}
1192
Christoph Hellwigf3ca80f2015-11-28 15:40:19 +01001193static int nvme_dev_release(struct inode *inode, struct file *file)
Christoph Hellwig1673f1f2015-11-26 10:54:19 +01001194{
Christoph Hellwigf3ca80f2015-11-28 15:40:19 +01001195 nvme_put_ctrl(file->private_data);
1196 return 0;
Christoph Hellwig1673f1f2015-11-26 10:54:19 +01001197}
1198
Christoph Hellwigbfd89472015-12-24 15:27:01 +01001199static int nvme_dev_user_cmd(struct nvme_ctrl *ctrl, void __user *argp)
1200{
1201 struct nvme_ns *ns;
1202 int ret;
1203
1204 mutex_lock(&ctrl->namespaces_mutex);
1205 if (list_empty(&ctrl->namespaces)) {
1206 ret = -ENOTTY;
1207 goto out_unlock;
1208 }
1209
1210 ns = list_first_entry(&ctrl->namespaces, struct nvme_ns, list);
1211 if (ns != list_last_entry(&ctrl->namespaces, struct nvme_ns, list)) {
Sagi Grimberg1b3c47c2016-02-10 08:51:15 -07001212 dev_warn(ctrl->device,
Christoph Hellwigbfd89472015-12-24 15:27:01 +01001213 "NVME_IOCTL_IO_CMD not supported when multiple namespaces present!\n");
1214 ret = -EINVAL;
1215 goto out_unlock;
1216 }
1217
Sagi Grimberg1b3c47c2016-02-10 08:51:15 -07001218 dev_warn(ctrl->device,
Christoph Hellwigbfd89472015-12-24 15:27:01 +01001219 "using deprecated NVME_IOCTL_IO_CMD ioctl on the char device!\n");
1220 kref_get(&ns->kref);
1221 mutex_unlock(&ctrl->namespaces_mutex);
1222
1223 ret = nvme_user_cmd(ctrl, ns, argp);
1224 nvme_put_ns(ns);
1225 return ret;
1226
1227out_unlock:
1228 mutex_unlock(&ctrl->namespaces_mutex);
1229 return ret;
1230}
1231
Christoph Hellwigf3ca80f2015-11-28 15:40:19 +01001232static long nvme_dev_ioctl(struct file *file, unsigned int cmd,
1233 unsigned long arg)
1234{
1235 struct nvme_ctrl *ctrl = file->private_data;
1236 void __user *argp = (void __user *)arg;
Christoph Hellwigf3ca80f2015-11-28 15:40:19 +01001237
1238 switch (cmd) {
1239 case NVME_IOCTL_ADMIN_CMD:
1240 return nvme_user_cmd(ctrl, NULL, argp);
1241 case NVME_IOCTL_IO_CMD:
Christoph Hellwigbfd89472015-12-24 15:27:01 +01001242 return nvme_dev_user_cmd(ctrl, argp);
Christoph Hellwigf3ca80f2015-11-28 15:40:19 +01001243 case NVME_IOCTL_RESET:
Sagi Grimberg1b3c47c2016-02-10 08:51:15 -07001244 dev_warn(ctrl->device, "resetting controller\n");
Christoph Hellwigf3ca80f2015-11-28 15:40:19 +01001245 return ctrl->ops->reset_ctrl(ctrl);
1246 case NVME_IOCTL_SUBSYS_RESET:
1247 return nvme_reset_subsystem(ctrl);
Keith Busch9ec3bb22016-04-29 15:45:18 -06001248 case NVME_IOCTL_RESCAN:
1249 nvme_queue_scan(ctrl);
1250 return 0;
Christoph Hellwigf3ca80f2015-11-28 15:40:19 +01001251 default:
1252 return -ENOTTY;
1253 }
1254}
1255
1256static const struct file_operations nvme_dev_fops = {
1257 .owner = THIS_MODULE,
1258 .open = nvme_dev_open,
1259 .release = nvme_dev_release,
1260 .unlocked_ioctl = nvme_dev_ioctl,
1261 .compat_ioctl = nvme_dev_ioctl,
1262};
1263
1264static ssize_t nvme_sysfs_reset(struct device *dev,
1265 struct device_attribute *attr, const char *buf,
1266 size_t count)
1267{
1268 struct nvme_ctrl *ctrl = dev_get_drvdata(dev);
1269 int ret;
1270
1271 ret = ctrl->ops->reset_ctrl(ctrl);
1272 if (ret < 0)
1273 return ret;
1274 return count;
1275}
1276static DEVICE_ATTR(reset_controller, S_IWUSR, NULL, nvme_sysfs_reset);
1277
Keith Busch9ec3bb22016-04-29 15:45:18 -06001278static ssize_t nvme_sysfs_rescan(struct device *dev,
1279 struct device_attribute *attr, const char *buf,
1280 size_t count)
1281{
1282 struct nvme_ctrl *ctrl = dev_get_drvdata(dev);
1283
1284 nvme_queue_scan(ctrl);
1285 return count;
1286}
1287static DEVICE_ATTR(rescan_controller, S_IWUSR, NULL, nvme_sysfs_rescan);
1288
Keith Busch118472a2016-02-18 09:57:48 -07001289static ssize_t wwid_show(struct device *dev, struct device_attribute *attr,
1290 char *buf)
1291{
1292 struct nvme_ns *ns = dev_to_disk(dev)->private_data;
1293 struct nvme_ctrl *ctrl = ns->ctrl;
1294 int serial_len = sizeof(ctrl->serial);
1295 int model_len = sizeof(ctrl->model);
1296
1297 if (memchr_inv(ns->uuid, 0, sizeof(ns->uuid)))
1298 return sprintf(buf, "eui.%16phN\n", ns->uuid);
1299
1300 if (memchr_inv(ns->eui, 0, sizeof(ns->eui)))
1301 return sprintf(buf, "eui.%8phN\n", ns->eui);
1302
1303 while (ctrl->serial[serial_len - 1] == ' ')
1304 serial_len--;
1305 while (ctrl->model[model_len - 1] == ' ')
1306 model_len--;
1307
1308 return sprintf(buf, "nvme.%04x-%*phN-%*phN-%08x\n", ctrl->vid,
1309 serial_len, ctrl->serial, model_len, ctrl->model, ns->ns_id);
1310}
1311static DEVICE_ATTR(wwid, S_IRUGO, wwid_show, NULL);
1312
Keith Busch2b9b6e82015-12-22 10:10:45 -07001313static ssize_t uuid_show(struct device *dev, struct device_attribute *attr,
1314 char *buf)
1315{
1316 struct nvme_ns *ns = dev_to_disk(dev)->private_data;
1317 return sprintf(buf, "%pU\n", ns->uuid);
1318}
1319static DEVICE_ATTR(uuid, S_IRUGO, uuid_show, NULL);
1320
1321static ssize_t eui_show(struct device *dev, struct device_attribute *attr,
1322 char *buf)
1323{
1324 struct nvme_ns *ns = dev_to_disk(dev)->private_data;
1325 return sprintf(buf, "%8phd\n", ns->eui);
1326}
1327static DEVICE_ATTR(eui, S_IRUGO, eui_show, NULL);
1328
1329static ssize_t nsid_show(struct device *dev, struct device_attribute *attr,
1330 char *buf)
1331{
1332 struct nvme_ns *ns = dev_to_disk(dev)->private_data;
1333 return sprintf(buf, "%d\n", ns->ns_id);
1334}
1335static DEVICE_ATTR(nsid, S_IRUGO, nsid_show, NULL);
1336
1337static struct attribute *nvme_ns_attrs[] = {
Keith Busch118472a2016-02-18 09:57:48 -07001338 &dev_attr_wwid.attr,
Keith Busch2b9b6e82015-12-22 10:10:45 -07001339 &dev_attr_uuid.attr,
1340 &dev_attr_eui.attr,
1341 &dev_attr_nsid.attr,
1342 NULL,
1343};
1344
1345static umode_t nvme_attrs_are_visible(struct kobject *kobj,
1346 struct attribute *a, int n)
1347{
1348 struct device *dev = container_of(kobj, struct device, kobj);
1349 struct nvme_ns *ns = dev_to_disk(dev)->private_data;
1350
1351 if (a == &dev_attr_uuid.attr) {
1352 if (!memchr_inv(ns->uuid, 0, sizeof(ns->uuid)))
1353 return 0;
1354 }
1355 if (a == &dev_attr_eui.attr) {
1356 if (!memchr_inv(ns->eui, 0, sizeof(ns->eui)))
1357 return 0;
1358 }
1359 return a->mode;
1360}
1361
1362static const struct attribute_group nvme_ns_attr_group = {
1363 .attrs = nvme_ns_attrs,
1364 .is_visible = nvme_attrs_are_visible,
1365};
1366
Ming Lin931e1c22016-02-26 13:24:19 -08001367#define nvme_show_str_function(field) \
Keith Busch779ff7562016-01-12 15:09:31 -07001368static ssize_t field##_show(struct device *dev, \
1369 struct device_attribute *attr, char *buf) \
1370{ \
1371 struct nvme_ctrl *ctrl = dev_get_drvdata(dev); \
1372 return sprintf(buf, "%.*s\n", (int)sizeof(ctrl->field), ctrl->field); \
1373} \
1374static DEVICE_ATTR(field, S_IRUGO, field##_show, NULL);
1375
Ming Lin931e1c22016-02-26 13:24:19 -08001376#define nvme_show_int_function(field) \
1377static ssize_t field##_show(struct device *dev, \
1378 struct device_attribute *attr, char *buf) \
1379{ \
1380 struct nvme_ctrl *ctrl = dev_get_drvdata(dev); \
1381 return sprintf(buf, "%d\n", ctrl->field); \
1382} \
1383static DEVICE_ATTR(field, S_IRUGO, field##_show, NULL);
1384
1385nvme_show_str_function(model);
1386nvme_show_str_function(serial);
1387nvme_show_str_function(firmware_rev);
1388nvme_show_int_function(cntlid);
Keith Busch779ff7562016-01-12 15:09:31 -07001389
1390static struct attribute *nvme_dev_attrs[] = {
1391 &dev_attr_reset_controller.attr,
Keith Busch9ec3bb22016-04-29 15:45:18 -06001392 &dev_attr_rescan_controller.attr,
Keith Busch779ff7562016-01-12 15:09:31 -07001393 &dev_attr_model.attr,
1394 &dev_attr_serial.attr,
1395 &dev_attr_firmware_rev.attr,
Ming Lin931e1c22016-02-26 13:24:19 -08001396 &dev_attr_cntlid.attr,
Keith Busch779ff7562016-01-12 15:09:31 -07001397 NULL
1398};
1399
1400static struct attribute_group nvme_dev_attrs_group = {
1401 .attrs = nvme_dev_attrs,
1402};
1403
1404static const struct attribute_group *nvme_dev_attr_groups[] = {
1405 &nvme_dev_attrs_group,
1406 NULL,
1407};
1408
Christoph Hellwig5bae7f72015-11-28 15:39:07 +01001409static int ns_cmp(void *priv, struct list_head *a, struct list_head *b)
1410{
1411 struct nvme_ns *nsa = container_of(a, struct nvme_ns, list);
1412 struct nvme_ns *nsb = container_of(b, struct nvme_ns, list);
1413
1414 return nsa->ns_id - nsb->ns_id;
1415}
1416
1417static struct nvme_ns *nvme_find_ns(struct nvme_ctrl *ctrl, unsigned nsid)
1418{
1419 struct nvme_ns *ns;
1420
Christoph Hellwig69d3b8a2015-12-24 15:27:00 +01001421 lockdep_assert_held(&ctrl->namespaces_mutex);
1422
Christoph Hellwig5bae7f72015-11-28 15:39:07 +01001423 list_for_each_entry(ns, &ctrl->namespaces, list) {
1424 if (ns->ns_id == nsid)
1425 return ns;
1426 if (ns->ns_id > nsid)
1427 break;
1428 }
1429 return NULL;
1430}
1431
1432static void nvme_alloc_ns(struct nvme_ctrl *ctrl, unsigned nsid)
1433{
1434 struct nvme_ns *ns;
1435 struct gendisk *disk;
1436 int node = dev_to_node(ctrl->dev);
1437
Christoph Hellwig69d3b8a2015-12-24 15:27:00 +01001438 lockdep_assert_held(&ctrl->namespaces_mutex);
1439
Christoph Hellwig5bae7f72015-11-28 15:39:07 +01001440 ns = kzalloc_node(sizeof(*ns), GFP_KERNEL, node);
1441 if (!ns)
1442 return;
1443
Keith Busch075790e2016-02-24 09:15:53 -07001444 ns->instance = ida_simple_get(&ctrl->ns_ida, 1, 0, GFP_KERNEL);
1445 if (ns->instance < 0)
1446 goto out_free_ns;
1447
Christoph Hellwig5bae7f72015-11-28 15:39:07 +01001448 ns->queue = blk_mq_init_queue(ctrl->tagset);
1449 if (IS_ERR(ns->queue))
Keith Busch075790e2016-02-24 09:15:53 -07001450 goto out_release_instance;
Christoph Hellwig5bae7f72015-11-28 15:39:07 +01001451 queue_flag_set_unlocked(QUEUE_FLAG_NONROT, ns->queue);
1452 ns->queue->queuedata = ns;
1453 ns->ctrl = ctrl;
1454
1455 disk = alloc_disk_node(0, node);
1456 if (!disk)
1457 goto out_free_queue;
1458
1459 kref_init(&ns->kref);
1460 ns->ns_id = nsid;
1461 ns->disk = disk;
1462 ns->lba_shift = 9; /* set to a default value for 512 until disk is validated */
Christoph Hellwig5bae7f72015-11-28 15:39:07 +01001463
Christoph Hellwigda358252016-03-02 18:07:11 +01001464
Christoph Hellwig5bae7f72015-11-28 15:39:07 +01001465 blk_queue_logical_block_size(ns->queue, 1 << ns->lba_shift);
Christoph Hellwigda358252016-03-02 18:07:11 +01001466 nvme_set_queue_limits(ctrl, ns->queue);
Christoph Hellwig5bae7f72015-11-28 15:39:07 +01001467
1468 disk->major = nvme_major;
1469 disk->first_minor = 0;
1470 disk->fops = &nvme_fops;
1471 disk->private_data = ns;
1472 disk->queue = ns->queue;
1473 disk->driverfs_dev = ctrl->device;
1474 disk->flags = GENHD_FL_EXT_DEVT;
Keith Busch075790e2016-02-24 09:15:53 -07001475 sprintf(disk->disk_name, "nvme%dn%d", ctrl->instance, ns->instance);
Christoph Hellwig5bae7f72015-11-28 15:39:07 +01001476
Christoph Hellwig5bae7f72015-11-28 15:39:07 +01001477 if (nvme_revalidate_disk(ns->disk))
1478 goto out_free_disk;
1479
Ming Lin0bf77e92016-04-25 14:20:18 -07001480 list_add_tail_rcu(&ns->list, &ctrl->namespaces);
Christoph Hellwig5bae7f72015-11-28 15:39:07 +01001481 kref_get(&ctrl->kref);
Keith Busch2b9b6e82015-12-22 10:10:45 -07001482 if (ns->type == NVME_NS_LIGHTNVM)
1483 return;
Christoph Hellwig5bae7f72015-11-28 15:39:07 +01001484
Keith Busch2b9b6e82015-12-22 10:10:45 -07001485 add_disk(ns->disk);
1486 if (sysfs_create_group(&disk_to_dev(ns->disk)->kobj,
1487 &nvme_ns_attr_group))
1488 pr_warn("%s: failed to create sysfs group for identification\n",
1489 ns->disk->disk_name);
Christoph Hellwig5bae7f72015-11-28 15:39:07 +01001490 return;
1491 out_free_disk:
1492 kfree(disk);
Christoph Hellwig5bae7f72015-11-28 15:39:07 +01001493 out_free_queue:
1494 blk_cleanup_queue(ns->queue);
Keith Busch075790e2016-02-24 09:15:53 -07001495 out_release_instance:
1496 ida_simple_remove(&ctrl->ns_ida, ns->instance);
Christoph Hellwig5bae7f72015-11-28 15:39:07 +01001497 out_free_ns:
1498 kfree(ns);
1499}
1500
1501static void nvme_ns_remove(struct nvme_ns *ns)
1502{
Ming Linb7b9c222016-04-25 14:20:19 -07001503 lockdep_assert_held(&ns->ctrl->namespaces_mutex);
1504
Keith Busch646017a2016-02-24 09:15:54 -07001505 if (test_and_set_bit(NVME_NS_REMOVING, &ns->flags))
1506 return;
1507
Christoph Hellwig5bae7f72015-11-28 15:39:07 +01001508 if (ns->disk->flags & GENHD_FL_UP) {
1509 if (blk_get_integrity(ns->disk))
1510 blk_integrity_unregister(ns->disk);
Keith Busch2b9b6e82015-12-22 10:10:45 -07001511 sysfs_remove_group(&disk_to_dev(ns->disk)->kobj,
1512 &nvme_ns_attr_group);
Christoph Hellwig5bae7f72015-11-28 15:39:07 +01001513 del_gendisk(ns->disk);
Christoph Hellwig5bae7f72015-11-28 15:39:07 +01001514 blk_mq_abort_requeue_list(ns->queue);
1515 blk_cleanup_queue(ns->queue);
1516 }
1517 list_del_init(&ns->list);
Ming Lin0bf77e92016-04-25 14:20:18 -07001518 synchronize_rcu();
Christoph Hellwig5bae7f72015-11-28 15:39:07 +01001519 nvme_put_ns(ns);
1520}
1521
Keith Busch540c8012015-10-22 15:45:06 -06001522static void nvme_validate_ns(struct nvme_ctrl *ctrl, unsigned nsid)
1523{
1524 struct nvme_ns *ns;
1525
1526 ns = nvme_find_ns(ctrl, nsid);
1527 if (ns) {
1528 if (revalidate_disk(ns->disk))
1529 nvme_ns_remove(ns);
1530 } else
1531 nvme_alloc_ns(ctrl, nsid);
1532}
1533
Sunad Bhandary47b0e502016-05-27 15:59:43 +05301534static void nvme_remove_invalid_namespaces(struct nvme_ctrl *ctrl,
1535 unsigned nsid)
1536{
1537 struct nvme_ns *ns, *next;
1538
1539 list_for_each_entry_safe(ns, next, &ctrl->namespaces, list) {
1540 if (ns->ns_id > nsid)
1541 nvme_ns_remove(ns);
1542 }
1543}
1544
Keith Busch540c8012015-10-22 15:45:06 -06001545static int nvme_scan_ns_list(struct nvme_ctrl *ctrl, unsigned nn)
1546{
1547 struct nvme_ns *ns;
1548 __le32 *ns_list;
1549 unsigned i, j, nsid, prev = 0, num_lists = DIV_ROUND_UP(nn, 1024);
1550 int ret = 0;
1551
1552 ns_list = kzalloc(0x1000, GFP_KERNEL);
1553 if (!ns_list)
1554 return -ENOMEM;
1555
1556 for (i = 0; i < num_lists; i++) {
1557 ret = nvme_identify_ns_list(ctrl, prev, ns_list);
1558 if (ret)
Sunad Bhandary47b0e502016-05-27 15:59:43 +05301559 goto free;
Keith Busch540c8012015-10-22 15:45:06 -06001560
1561 for (j = 0; j < min(nn, 1024U); j++) {
1562 nsid = le32_to_cpu(ns_list[j]);
1563 if (!nsid)
1564 goto out;
1565
1566 nvme_validate_ns(ctrl, nsid);
1567
1568 while (++prev < nsid) {
1569 ns = nvme_find_ns(ctrl, prev);
1570 if (ns)
1571 nvme_ns_remove(ns);
1572 }
1573 }
1574 nn -= j;
1575 }
1576 out:
Sunad Bhandary47b0e502016-05-27 15:59:43 +05301577 nvme_remove_invalid_namespaces(ctrl, prev);
1578 free:
Keith Busch540c8012015-10-22 15:45:06 -06001579 kfree(ns_list);
1580 return ret;
1581}
1582
Christoph Hellwig5955be22016-04-26 13:51:59 +02001583static void nvme_scan_ns_sequential(struct nvme_ctrl *ctrl, unsigned nn)
Christoph Hellwig5bae7f72015-11-28 15:39:07 +01001584{
Christoph Hellwig5bae7f72015-11-28 15:39:07 +01001585 unsigned i;
1586
Christoph Hellwig69d3b8a2015-12-24 15:27:00 +01001587 lockdep_assert_held(&ctrl->namespaces_mutex);
1588
Keith Busch540c8012015-10-22 15:45:06 -06001589 for (i = 1; i <= nn; i++)
1590 nvme_validate_ns(ctrl, i);
1591
Sunad Bhandary47b0e502016-05-27 15:59:43 +05301592 nvme_remove_invalid_namespaces(ctrl, nn);
Christoph Hellwig5bae7f72015-11-28 15:39:07 +01001593}
1594
Christoph Hellwig5955be22016-04-26 13:51:59 +02001595static void nvme_scan_work(struct work_struct *work)
Christoph Hellwig5bae7f72015-11-28 15:39:07 +01001596{
Christoph Hellwig5955be22016-04-26 13:51:59 +02001597 struct nvme_ctrl *ctrl =
1598 container_of(work, struct nvme_ctrl, scan_work);
Christoph Hellwig5bae7f72015-11-28 15:39:07 +01001599 struct nvme_id_ctrl *id;
Keith Busch540c8012015-10-22 15:45:06 -06001600 unsigned nn;
Christoph Hellwig5bae7f72015-11-28 15:39:07 +01001601
Christoph Hellwig5955be22016-04-26 13:51:59 +02001602 if (ctrl->state != NVME_CTRL_LIVE)
1603 return;
1604
Christoph Hellwig5bae7f72015-11-28 15:39:07 +01001605 if (nvme_identify_ctrl(ctrl, &id))
1606 return;
Keith Busch540c8012015-10-22 15:45:06 -06001607
Christoph Hellwig69d3b8a2015-12-24 15:27:00 +01001608 mutex_lock(&ctrl->namespaces_mutex);
Keith Busch540c8012015-10-22 15:45:06 -06001609 nn = le32_to_cpu(id->nn);
1610 if (ctrl->vs >= NVME_VS(1, 1) &&
1611 !(ctrl->quirks & NVME_QUIRK_IDENTIFY_CNS)) {
1612 if (!nvme_scan_ns_list(ctrl, nn))
1613 goto done;
1614 }
Christoph Hellwig5955be22016-04-26 13:51:59 +02001615 nvme_scan_ns_sequential(ctrl, nn);
Keith Busch540c8012015-10-22 15:45:06 -06001616 done:
1617 list_sort(NULL, &ctrl->namespaces, ns_cmp);
Christoph Hellwig69d3b8a2015-12-24 15:27:00 +01001618 mutex_unlock(&ctrl->namespaces_mutex);
Christoph Hellwig5bae7f72015-11-28 15:39:07 +01001619 kfree(id);
Christoph Hellwig5955be22016-04-26 13:51:59 +02001620
1621 if (ctrl->ops->post_scan)
1622 ctrl->ops->post_scan(ctrl);
Christoph Hellwig5bae7f72015-11-28 15:39:07 +01001623}
Christoph Hellwig5955be22016-04-26 13:51:59 +02001624
1625void nvme_queue_scan(struct nvme_ctrl *ctrl)
1626{
1627 /*
1628 * Do not queue new scan work when a controller is reset during
1629 * removal.
1630 */
1631 if (ctrl->state == NVME_CTRL_LIVE)
1632 schedule_work(&ctrl->scan_work);
1633}
1634EXPORT_SYMBOL_GPL(nvme_queue_scan);
Christoph Hellwig5bae7f72015-11-28 15:39:07 +01001635
1636void nvme_remove_namespaces(struct nvme_ctrl *ctrl)
1637{
1638 struct nvme_ns *ns, *next;
1639
Keith Busch0ff9d4e2016-05-12 08:37:14 -06001640 /*
1641 * The dead states indicates the controller was not gracefully
1642 * disconnected. In that case, we won't be able to flush any data while
1643 * removing the namespaces' disks; fail all the queues now to avoid
1644 * potentially having to clean up the failed sync later.
1645 */
1646 if (ctrl->state == NVME_CTRL_DEAD)
1647 nvme_kill_queues(ctrl);
1648
Ming Linb7b9c222016-04-25 14:20:19 -07001649 mutex_lock(&ctrl->namespaces_mutex);
Christoph Hellwig5bae7f72015-11-28 15:39:07 +01001650 list_for_each_entry_safe(ns, next, &ctrl->namespaces, list)
1651 nvme_ns_remove(ns);
Ming Linb7b9c222016-04-25 14:20:19 -07001652 mutex_unlock(&ctrl->namespaces_mutex);
Christoph Hellwig5bae7f72015-11-28 15:39:07 +01001653}
Ming Lin576d55d2016-02-10 10:03:32 -08001654EXPORT_SYMBOL_GPL(nvme_remove_namespaces);
Christoph Hellwig5bae7f72015-11-28 15:39:07 +01001655
Christoph Hellwigf866fc422016-04-26 13:52:00 +02001656static void nvme_async_event_work(struct work_struct *work)
1657{
1658 struct nvme_ctrl *ctrl =
1659 container_of(work, struct nvme_ctrl, async_event_work);
1660
1661 spin_lock_irq(&ctrl->lock);
1662 while (ctrl->event_limit > 0) {
1663 int aer_idx = --ctrl->event_limit;
1664
1665 spin_unlock_irq(&ctrl->lock);
1666 ctrl->ops->submit_async_event(ctrl, aer_idx);
1667 spin_lock_irq(&ctrl->lock);
1668 }
1669 spin_unlock_irq(&ctrl->lock);
1670}
1671
1672void nvme_complete_async_event(struct nvme_ctrl *ctrl,
1673 struct nvme_completion *cqe)
1674{
1675 u16 status = le16_to_cpu(cqe->status) >> 1;
1676 u32 result = le32_to_cpu(cqe->result);
1677
1678 if (status == NVME_SC_SUCCESS || status == NVME_SC_ABORT_REQ) {
1679 ++ctrl->event_limit;
1680 schedule_work(&ctrl->async_event_work);
1681 }
1682
1683 if (status != NVME_SC_SUCCESS)
1684 return;
1685
1686 switch (result & 0xff07) {
1687 case NVME_AER_NOTICE_NS_CHANGED:
1688 dev_info(ctrl->device, "rescanning\n");
1689 nvme_queue_scan(ctrl);
1690 break;
1691 default:
1692 dev_warn(ctrl->device, "async event result %08x\n", result);
1693 }
1694}
1695EXPORT_SYMBOL_GPL(nvme_complete_async_event);
1696
1697void nvme_queue_async_events(struct nvme_ctrl *ctrl)
1698{
1699 ctrl->event_limit = NVME_NR_AERS;
1700 schedule_work(&ctrl->async_event_work);
1701}
1702EXPORT_SYMBOL_GPL(nvme_queue_async_events);
1703
Christoph Hellwigf3ca80f2015-11-28 15:40:19 +01001704static DEFINE_IDA(nvme_instance_ida);
1705
1706static int nvme_set_instance(struct nvme_ctrl *ctrl)
1707{
1708 int instance, error;
1709
1710 do {
1711 if (!ida_pre_get(&nvme_instance_ida, GFP_KERNEL))
1712 return -ENODEV;
1713
1714 spin_lock(&dev_list_lock);
1715 error = ida_get_new(&nvme_instance_ida, &instance);
1716 spin_unlock(&dev_list_lock);
1717 } while (error == -EAGAIN);
1718
1719 if (error)
1720 return -ENODEV;
1721
1722 ctrl->instance = instance;
1723 return 0;
1724}
1725
1726static void nvme_release_instance(struct nvme_ctrl *ctrl)
1727{
1728 spin_lock(&dev_list_lock);
1729 ida_remove(&nvme_instance_ida, ctrl->instance);
1730 spin_unlock(&dev_list_lock);
1731}
1732
Keith Busch53029b02015-11-28 15:41:02 +01001733void nvme_uninit_ctrl(struct nvme_ctrl *ctrl)
Ming Lin576d55d2016-02-10 10:03:32 -08001734{
Christoph Hellwigf866fc422016-04-26 13:52:00 +02001735 flush_work(&ctrl->async_event_work);
Christoph Hellwig5955be22016-04-26 13:51:59 +02001736 flush_work(&ctrl->scan_work);
1737 nvme_remove_namespaces(ctrl);
1738
Keith Busch53029b02015-11-28 15:41:02 +01001739 device_destroy(nvme_class, MKDEV(nvme_char_major, ctrl->instance));
Christoph Hellwigf3ca80f2015-11-28 15:40:19 +01001740
1741 spin_lock(&dev_list_lock);
1742 list_del(&ctrl->node);
1743 spin_unlock(&dev_list_lock);
Keith Busch53029b02015-11-28 15:41:02 +01001744}
Ming Lin576d55d2016-02-10 10:03:32 -08001745EXPORT_SYMBOL_GPL(nvme_uninit_ctrl);
Keith Busch53029b02015-11-28 15:41:02 +01001746
1747static void nvme_free_ctrl(struct kref *kref)
1748{
1749 struct nvme_ctrl *ctrl = container_of(kref, struct nvme_ctrl, kref);
Christoph Hellwigf3ca80f2015-11-28 15:40:19 +01001750
1751 put_device(ctrl->device);
1752 nvme_release_instance(ctrl);
Keith Busch075790e2016-02-24 09:15:53 -07001753 ida_destroy(&ctrl->ns_ida);
Christoph Hellwigf3ca80f2015-11-28 15:40:19 +01001754
1755 ctrl->ops->free_ctrl(ctrl);
1756}
1757
1758void nvme_put_ctrl(struct nvme_ctrl *ctrl)
1759{
1760 kref_put(&ctrl->kref, nvme_free_ctrl);
1761}
Ming Lin576d55d2016-02-10 10:03:32 -08001762EXPORT_SYMBOL_GPL(nvme_put_ctrl);
Christoph Hellwigf3ca80f2015-11-28 15:40:19 +01001763
1764/*
1765 * Initialize a NVMe controller structures. This needs to be called during
1766 * earliest initialization so that we have the initialized structured around
1767 * during probing.
1768 */
1769int nvme_init_ctrl(struct nvme_ctrl *ctrl, struct device *dev,
1770 const struct nvme_ctrl_ops *ops, unsigned long quirks)
1771{
1772 int ret;
1773
Christoph Hellwigbb8d2612016-04-26 13:51:57 +02001774 ctrl->state = NVME_CTRL_NEW;
1775 spin_lock_init(&ctrl->lock);
Christoph Hellwigf3ca80f2015-11-28 15:40:19 +01001776 INIT_LIST_HEAD(&ctrl->namespaces);
Christoph Hellwig69d3b8a2015-12-24 15:27:00 +01001777 mutex_init(&ctrl->namespaces_mutex);
Christoph Hellwigf3ca80f2015-11-28 15:40:19 +01001778 kref_init(&ctrl->kref);
1779 ctrl->dev = dev;
1780 ctrl->ops = ops;
1781 ctrl->quirks = quirks;
Christoph Hellwig5955be22016-04-26 13:51:59 +02001782 INIT_WORK(&ctrl->scan_work, nvme_scan_work);
Christoph Hellwigf866fc422016-04-26 13:52:00 +02001783 INIT_WORK(&ctrl->async_event_work, nvme_async_event_work);
Christoph Hellwigf3ca80f2015-11-28 15:40:19 +01001784
1785 ret = nvme_set_instance(ctrl);
1786 if (ret)
1787 goto out;
1788
Keith Busch779ff7562016-01-12 15:09:31 -07001789 ctrl->device = device_create_with_groups(nvme_class, ctrl->dev,
Christoph Hellwigf3ca80f2015-11-28 15:40:19 +01001790 MKDEV(nvme_char_major, ctrl->instance),
Christoph Hellwigf4f0f632016-02-09 12:44:03 -07001791 ctrl, nvme_dev_attr_groups,
Keith Busch779ff7562016-01-12 15:09:31 -07001792 "nvme%d", ctrl->instance);
Christoph Hellwigf3ca80f2015-11-28 15:40:19 +01001793 if (IS_ERR(ctrl->device)) {
1794 ret = PTR_ERR(ctrl->device);
1795 goto out_release_instance;
1796 }
1797 get_device(ctrl->device);
Keith Busch075790e2016-02-24 09:15:53 -07001798 ida_init(&ctrl->ns_ida);
Christoph Hellwigf3ca80f2015-11-28 15:40:19 +01001799
Christoph Hellwigf3ca80f2015-11-28 15:40:19 +01001800 spin_lock(&dev_list_lock);
1801 list_add_tail(&ctrl->node, &nvme_ctrl_list);
1802 spin_unlock(&dev_list_lock);
1803
1804 return 0;
Christoph Hellwigf3ca80f2015-11-28 15:40:19 +01001805out_release_instance:
1806 nvme_release_instance(ctrl);
1807out:
1808 return ret;
1809}
Ming Lin576d55d2016-02-10 10:03:32 -08001810EXPORT_SYMBOL_GPL(nvme_init_ctrl);
Christoph Hellwigf3ca80f2015-11-28 15:40:19 +01001811
Keith Busch69d9a992016-02-24 09:15:56 -07001812/**
1813 * nvme_kill_queues(): Ends all namespace queues
1814 * @ctrl: the dead controller that needs to end
1815 *
1816 * Call this function when the driver determines it is unable to get the
1817 * controller in a state capable of servicing IO.
1818 */
1819void nvme_kill_queues(struct nvme_ctrl *ctrl)
1820{
1821 struct nvme_ns *ns;
1822
Ming Lin0bf77e92016-04-25 14:20:18 -07001823 rcu_read_lock();
1824 list_for_each_entry_rcu(ns, &ctrl->namespaces, list) {
Keith Busch69d9a992016-02-24 09:15:56 -07001825 if (!kref_get_unless_zero(&ns->kref))
1826 continue;
1827
1828 /*
1829 * Revalidating a dead namespace sets capacity to 0. This will
1830 * end buffered writers dirtying pages that can't be synced.
1831 */
1832 if (!test_and_set_bit(NVME_NS_DEAD, &ns->flags))
1833 revalidate_disk(ns->disk);
1834
1835 blk_set_queue_dying(ns->queue);
1836 blk_mq_abort_requeue_list(ns->queue);
1837 blk_mq_start_stopped_hw_queues(ns->queue, true);
1838
1839 nvme_put_ns(ns);
1840 }
Ming Lin0bf77e92016-04-25 14:20:18 -07001841 rcu_read_unlock();
Keith Busch69d9a992016-02-24 09:15:56 -07001842}
Linus Torvalds237045f2016-03-18 17:13:31 -07001843EXPORT_SYMBOL_GPL(nvme_kill_queues);
Keith Busch69d9a992016-02-24 09:15:56 -07001844
Keith Busch25646262016-01-04 09:10:57 -07001845void nvme_stop_queues(struct nvme_ctrl *ctrl)
Sagi Grimberg363c9aa2015-12-24 15:26:59 +01001846{
1847 struct nvme_ns *ns;
1848
Ming Lin0bf77e92016-04-25 14:20:18 -07001849 rcu_read_lock();
1850 list_for_each_entry_rcu(ns, &ctrl->namespaces, list) {
Sagi Grimberg363c9aa2015-12-24 15:26:59 +01001851 spin_lock_irq(ns->queue->queue_lock);
1852 queue_flag_set(QUEUE_FLAG_STOPPED, ns->queue);
1853 spin_unlock_irq(ns->queue->queue_lock);
1854
1855 blk_mq_cancel_requeue_work(ns->queue);
1856 blk_mq_stop_hw_queues(ns->queue);
1857 }
Ming Lin0bf77e92016-04-25 14:20:18 -07001858 rcu_read_unlock();
Sagi Grimberg363c9aa2015-12-24 15:26:59 +01001859}
Ming Lin576d55d2016-02-10 10:03:32 -08001860EXPORT_SYMBOL_GPL(nvme_stop_queues);
Sagi Grimberg363c9aa2015-12-24 15:26:59 +01001861
Keith Busch25646262016-01-04 09:10:57 -07001862void nvme_start_queues(struct nvme_ctrl *ctrl)
Sagi Grimberg363c9aa2015-12-24 15:26:59 +01001863{
1864 struct nvme_ns *ns;
1865
Ming Lin0bf77e92016-04-25 14:20:18 -07001866 rcu_read_lock();
1867 list_for_each_entry_rcu(ns, &ctrl->namespaces, list) {
Sagi Grimberg363c9aa2015-12-24 15:26:59 +01001868 queue_flag_clear_unlocked(QUEUE_FLAG_STOPPED, ns->queue);
Sagi Grimberg363c9aa2015-12-24 15:26:59 +01001869 blk_mq_start_stopped_hw_queues(ns->queue, true);
1870 blk_mq_kick_requeue_list(ns->queue);
1871 }
Ming Lin0bf77e92016-04-25 14:20:18 -07001872 rcu_read_unlock();
Sagi Grimberg363c9aa2015-12-24 15:26:59 +01001873}
Ming Lin576d55d2016-02-10 10:03:32 -08001874EXPORT_SYMBOL_GPL(nvme_start_queues);
Sagi Grimberg363c9aa2015-12-24 15:26:59 +01001875
Christoph Hellwig5bae7f72015-11-28 15:39:07 +01001876int __init nvme_core_init(void)
1877{
1878 int result;
1879
1880 result = register_blkdev(nvme_major, "nvme");
1881 if (result < 0)
1882 return result;
1883 else if (result > 0)
1884 nvme_major = result;
1885
Christoph Hellwigf3ca80f2015-11-28 15:40:19 +01001886 result = __register_chrdev(nvme_char_major, 0, NVME_MINORS, "nvme",
1887 &nvme_dev_fops);
1888 if (result < 0)
1889 goto unregister_blkdev;
1890 else if (result > 0)
1891 nvme_char_major = result;
1892
1893 nvme_class = class_create(THIS_MODULE, "nvme");
1894 if (IS_ERR(nvme_class)) {
1895 result = PTR_ERR(nvme_class);
1896 goto unregister_chrdev;
1897 }
1898
Christoph Hellwig5bae7f72015-11-28 15:39:07 +01001899 return 0;
Christoph Hellwigf3ca80f2015-11-28 15:40:19 +01001900
1901 unregister_chrdev:
1902 __unregister_chrdev(nvme_char_major, 0, NVME_MINORS, "nvme");
1903 unregister_blkdev:
1904 unregister_blkdev(nvme_major, "nvme");
1905 return result;
Christoph Hellwig5bae7f72015-11-28 15:39:07 +01001906}
1907
1908void nvme_core_exit(void)
1909{
Christoph Hellwigf3ca80f2015-11-28 15:40:19 +01001910 class_destroy(nvme_class);
1911 __unregister_chrdev(nvme_char_major, 0, NVME_MINORS, "nvme");
Wang Sheng-Hui23bd63c2016-04-28 16:19:31 +08001912 unregister_blkdev(nvme_major, "nvme");
Christoph Hellwig5bae7f72015-11-28 15:39:07 +01001913}
Ming Lin576d55d2016-02-10 10:03:32 -08001914
1915MODULE_LICENSE("GPL");
1916MODULE_VERSION("1.0");
1917module_init(nvme_core_init);
1918module_exit(nvme_core_exit);