nvme: factor out a few helpers from req_completion
[deliverable/linux.git] / drivers / nvme / host / core.c
1 /*
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>
17 #include <linux/delay.h>
18 #include <linux/errno.h>
19 #include <linux/hdreg.h>
20 #include <linux/kernel.h>
21 #include <linux/module.h>
22 #include <linux/list_sort.h>
23 #include <linux/slab.h>
24 #include <linux/types.h>
25 #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>
31
32 #include "nvme.h"
33
34 #define NVME_MINORS (1U << MINORBITS)
35
36 static int nvme_major;
37 module_param(nvme_major, int, 0);
38
39 static int nvme_char_major;
40 module_param(nvme_char_major, int, 0);
41
42 static LIST_HEAD(nvme_ctrl_list);
43 DEFINE_SPINLOCK(dev_list_lock);
44
45 static struct class *nvme_class;
46
47 static void nvme_free_ns(struct kref *kref)
48 {
49 struct nvme_ns *ns = container_of(kref, struct nvme_ns, kref);
50
51 if (ns->type == NVME_NS_LIGHTNVM)
52 nvme_nvm_unregister(ns->queue, ns->disk->disk_name);
53
54 spin_lock(&dev_list_lock);
55 ns->disk->private_data = NULL;
56 spin_unlock(&dev_list_lock);
57
58 nvme_put_ctrl(ns->ctrl);
59 put_disk(ns->disk);
60 kfree(ns);
61 }
62
63 static void nvme_put_ns(struct nvme_ns *ns)
64 {
65 kref_put(&ns->kref, nvme_free_ns);
66 }
67
68 static struct nvme_ns *nvme_get_ns_from_disk(struct gendisk *disk)
69 {
70 struct nvme_ns *ns;
71
72 spin_lock(&dev_list_lock);
73 ns = disk->private_data;
74 if (ns && !kref_get_unless_zero(&ns->kref))
75 ns = NULL;
76 spin_unlock(&dev_list_lock);
77
78 return ns;
79 }
80
81 void nvme_requeue_req(struct request *req)
82 {
83 unsigned long flags;
84
85 blk_mq_requeue_request(req);
86 spin_lock_irqsave(req->q->queue_lock, flags);
87 if (!blk_queue_stopped(req->q))
88 blk_mq_kick_requeue_list(req->q);
89 spin_unlock_irqrestore(req->q->queue_lock, flags);
90 }
91
92 struct request *nvme_alloc_request(struct request_queue *q,
93 struct nvme_command *cmd, unsigned int flags)
94 {
95 bool write = cmd->common.opcode & 1;
96 struct request *req;
97
98 req = blk_mq_alloc_request(q, write, flags);
99 if (IS_ERR(req))
100 return req;
101
102 req->cmd_type = REQ_TYPE_DRV_PRIV;
103 req->cmd_flags |= REQ_FAILFAST_DRIVER;
104 req->__data_len = 0;
105 req->__sector = (sector_t) -1;
106 req->bio = req->biotail = NULL;
107
108 req->cmd = (unsigned char *)cmd;
109 req->cmd_len = sizeof(struct nvme_command);
110 req->special = (void *)0;
111
112 return req;
113 }
114
115 /*
116 * Returns 0 on success. If the result is negative, it's a Linux error code;
117 * if the result is positive, it's an NVM Express status code
118 */
119 int __nvme_submit_sync_cmd(struct request_queue *q, struct nvme_command *cmd,
120 void *buffer, unsigned bufflen, u32 *result, unsigned timeout)
121 {
122 struct request *req;
123 int ret;
124
125 req = nvme_alloc_request(q, cmd, 0);
126 if (IS_ERR(req))
127 return PTR_ERR(req);
128
129 req->timeout = timeout ? timeout : ADMIN_TIMEOUT;
130
131 if (buffer && bufflen) {
132 ret = blk_rq_map_kern(q, req, buffer, bufflen, GFP_KERNEL);
133 if (ret)
134 goto out;
135 }
136
137 blk_execute_rq(req->q, NULL, req, 0);
138 if (result)
139 *result = (u32)(uintptr_t)req->special;
140 ret = req->errors;
141 out:
142 blk_mq_free_request(req);
143 return ret;
144 }
145
146 int nvme_submit_sync_cmd(struct request_queue *q, struct nvme_command *cmd,
147 void *buffer, unsigned bufflen)
148 {
149 return __nvme_submit_sync_cmd(q, cmd, buffer, bufflen, NULL, 0);
150 }
151
152 int __nvme_submit_user_cmd(struct request_queue *q, struct nvme_command *cmd,
153 void __user *ubuffer, unsigned bufflen,
154 void __user *meta_buffer, unsigned meta_len, u32 meta_seed,
155 u32 *result, unsigned timeout)
156 {
157 bool write = cmd->common.opcode & 1;
158 struct nvme_ns *ns = q->queuedata;
159 struct gendisk *disk = ns ? ns->disk : NULL;
160 struct request *req;
161 struct bio *bio = NULL;
162 void *meta = NULL;
163 int ret;
164
165 req = nvme_alloc_request(q, cmd, 0);
166 if (IS_ERR(req))
167 return PTR_ERR(req);
168
169 req->timeout = timeout ? timeout : ADMIN_TIMEOUT;
170
171 if (ubuffer && bufflen) {
172 ret = blk_rq_map_user(q, req, NULL, ubuffer, bufflen,
173 GFP_KERNEL);
174 if (ret)
175 goto out;
176 bio = req->bio;
177
178 if (!disk)
179 goto submit;
180 bio->bi_bdev = bdget_disk(disk, 0);
181 if (!bio->bi_bdev) {
182 ret = -ENODEV;
183 goto out_unmap;
184 }
185
186 if (meta_buffer) {
187 struct bio_integrity_payload *bip;
188
189 meta = kmalloc(meta_len, GFP_KERNEL);
190 if (!meta) {
191 ret = -ENOMEM;
192 goto out_unmap;
193 }
194
195 if (write) {
196 if (copy_from_user(meta, meta_buffer,
197 meta_len)) {
198 ret = -EFAULT;
199 goto out_free_meta;
200 }
201 }
202
203 bip = bio_integrity_alloc(bio, GFP_KERNEL, 1);
204 if (IS_ERR(bip)) {
205 ret = PTR_ERR(bip);
206 goto out_free_meta;
207 }
208
209 bip->bip_iter.bi_size = meta_len;
210 bip->bip_iter.bi_sector = meta_seed;
211
212 ret = bio_integrity_add_page(bio, virt_to_page(meta),
213 meta_len, offset_in_page(meta));
214 if (ret != meta_len) {
215 ret = -ENOMEM;
216 goto out_free_meta;
217 }
218 }
219 }
220 submit:
221 blk_execute_rq(req->q, disk, req, 0);
222 ret = req->errors;
223 if (result)
224 *result = (u32)(uintptr_t)req->special;
225 if (meta && !ret && !write) {
226 if (copy_to_user(meta_buffer, meta, meta_len))
227 ret = -EFAULT;
228 }
229 out_free_meta:
230 kfree(meta);
231 out_unmap:
232 if (bio) {
233 if (disk && bio->bi_bdev)
234 bdput(bio->bi_bdev);
235 blk_rq_unmap_user(bio);
236 }
237 out:
238 blk_mq_free_request(req);
239 return ret;
240 }
241
242 int nvme_submit_user_cmd(struct request_queue *q, struct nvme_command *cmd,
243 void __user *ubuffer, unsigned bufflen, u32 *result,
244 unsigned timeout)
245 {
246 return __nvme_submit_user_cmd(q, cmd, ubuffer, bufflen, NULL, 0, 0,
247 result, timeout);
248 }
249
250 int nvme_identify_ctrl(struct nvme_ctrl *dev, struct nvme_id_ctrl **id)
251 {
252 struct nvme_command c = { };
253 int error;
254
255 /* gcc-4.4.4 (at least) has issues with initializers and anon unions */
256 c.identify.opcode = nvme_admin_identify;
257 c.identify.cns = cpu_to_le32(1);
258
259 *id = kmalloc(sizeof(struct nvme_id_ctrl), GFP_KERNEL);
260 if (!*id)
261 return -ENOMEM;
262
263 error = nvme_submit_sync_cmd(dev->admin_q, &c, *id,
264 sizeof(struct nvme_id_ctrl));
265 if (error)
266 kfree(*id);
267 return error;
268 }
269
270 static int nvme_identify_ns_list(struct nvme_ctrl *dev, unsigned nsid, __le32 *ns_list)
271 {
272 struct nvme_command c = { };
273
274 c.identify.opcode = nvme_admin_identify;
275 c.identify.cns = cpu_to_le32(2);
276 c.identify.nsid = cpu_to_le32(nsid);
277 return nvme_submit_sync_cmd(dev->admin_q, &c, ns_list, 0x1000);
278 }
279
280 int nvme_identify_ns(struct nvme_ctrl *dev, unsigned nsid,
281 struct nvme_id_ns **id)
282 {
283 struct nvme_command c = { };
284 int error;
285
286 /* gcc-4.4.4 (at least) has issues with initializers and anon unions */
287 c.identify.opcode = nvme_admin_identify,
288 c.identify.nsid = cpu_to_le32(nsid),
289
290 *id = kmalloc(sizeof(struct nvme_id_ns), GFP_KERNEL);
291 if (!*id)
292 return -ENOMEM;
293
294 error = nvme_submit_sync_cmd(dev->admin_q, &c, *id,
295 sizeof(struct nvme_id_ns));
296 if (error)
297 kfree(*id);
298 return error;
299 }
300
301 int nvme_get_features(struct nvme_ctrl *dev, unsigned fid, unsigned nsid,
302 dma_addr_t dma_addr, u32 *result)
303 {
304 struct nvme_command c;
305
306 memset(&c, 0, sizeof(c));
307 c.features.opcode = nvme_admin_get_features;
308 c.features.nsid = cpu_to_le32(nsid);
309 c.features.prp1 = cpu_to_le64(dma_addr);
310 c.features.fid = cpu_to_le32(fid);
311
312 return __nvme_submit_sync_cmd(dev->admin_q, &c, NULL, 0, result, 0);
313 }
314
315 int nvme_set_features(struct nvme_ctrl *dev, unsigned fid, unsigned dword11,
316 dma_addr_t dma_addr, u32 *result)
317 {
318 struct nvme_command c;
319
320 memset(&c, 0, sizeof(c));
321 c.features.opcode = nvme_admin_set_features;
322 c.features.prp1 = cpu_to_le64(dma_addr);
323 c.features.fid = cpu_to_le32(fid);
324 c.features.dword11 = cpu_to_le32(dword11);
325
326 return __nvme_submit_sync_cmd(dev->admin_q, &c, NULL, 0, result, 0);
327 }
328
329 int nvme_get_log_page(struct nvme_ctrl *dev, struct nvme_smart_log **log)
330 {
331 struct nvme_command c = { };
332 int error;
333
334 c.common.opcode = nvme_admin_get_log_page,
335 c.common.nsid = cpu_to_le32(0xFFFFFFFF),
336 c.common.cdw10[0] = cpu_to_le32(
337 (((sizeof(struct nvme_smart_log) / 4) - 1) << 16) |
338 NVME_LOG_SMART),
339
340 *log = kmalloc(sizeof(struct nvme_smart_log), GFP_KERNEL);
341 if (!*log)
342 return -ENOMEM;
343
344 error = nvme_submit_sync_cmd(dev->admin_q, &c, *log,
345 sizeof(struct nvme_smart_log));
346 if (error)
347 kfree(*log);
348 return error;
349 }
350
351 int nvme_set_queue_count(struct nvme_ctrl *ctrl, int *count)
352 {
353 u32 q_count = (*count - 1) | ((*count - 1) << 16);
354 u32 result;
355 int status, nr_io_queues;
356
357 status = nvme_set_features(ctrl, NVME_FEAT_NUM_QUEUES, q_count, 0,
358 &result);
359 if (status)
360 return status;
361
362 nr_io_queues = min(result & 0xffff, result >> 16) + 1;
363 *count = min(*count, nr_io_queues);
364 return 0;
365 }
366
367 static int nvme_submit_io(struct nvme_ns *ns, struct nvme_user_io __user *uio)
368 {
369 struct nvme_user_io io;
370 struct nvme_command c;
371 unsigned length, meta_len;
372 void __user *metadata;
373
374 if (copy_from_user(&io, uio, sizeof(io)))
375 return -EFAULT;
376
377 switch (io.opcode) {
378 case nvme_cmd_write:
379 case nvme_cmd_read:
380 case nvme_cmd_compare:
381 break;
382 default:
383 return -EINVAL;
384 }
385
386 length = (io.nblocks + 1) << ns->lba_shift;
387 meta_len = (io.nblocks + 1) * ns->ms;
388 metadata = (void __user *)(uintptr_t)io.metadata;
389
390 if (ns->ext) {
391 length += meta_len;
392 meta_len = 0;
393 } else if (meta_len) {
394 if ((io.metadata & 3) || !io.metadata)
395 return -EINVAL;
396 }
397
398 memset(&c, 0, sizeof(c));
399 c.rw.opcode = io.opcode;
400 c.rw.flags = io.flags;
401 c.rw.nsid = cpu_to_le32(ns->ns_id);
402 c.rw.slba = cpu_to_le64(io.slba);
403 c.rw.length = cpu_to_le16(io.nblocks);
404 c.rw.control = cpu_to_le16(io.control);
405 c.rw.dsmgmt = cpu_to_le32(io.dsmgmt);
406 c.rw.reftag = cpu_to_le32(io.reftag);
407 c.rw.apptag = cpu_to_le16(io.apptag);
408 c.rw.appmask = cpu_to_le16(io.appmask);
409
410 return __nvme_submit_user_cmd(ns->queue, &c,
411 (void __user *)(uintptr_t)io.addr, length,
412 metadata, meta_len, io.slba, NULL, 0);
413 }
414
415 static int nvme_user_cmd(struct nvme_ctrl *ctrl, struct nvme_ns *ns,
416 struct nvme_passthru_cmd __user *ucmd)
417 {
418 struct nvme_passthru_cmd cmd;
419 struct nvme_command c;
420 unsigned timeout = 0;
421 int status;
422
423 if (!capable(CAP_SYS_ADMIN))
424 return -EACCES;
425 if (copy_from_user(&cmd, ucmd, sizeof(cmd)))
426 return -EFAULT;
427
428 memset(&c, 0, sizeof(c));
429 c.common.opcode = cmd.opcode;
430 c.common.flags = cmd.flags;
431 c.common.nsid = cpu_to_le32(cmd.nsid);
432 c.common.cdw2[0] = cpu_to_le32(cmd.cdw2);
433 c.common.cdw2[1] = cpu_to_le32(cmd.cdw3);
434 c.common.cdw10[0] = cpu_to_le32(cmd.cdw10);
435 c.common.cdw10[1] = cpu_to_le32(cmd.cdw11);
436 c.common.cdw10[2] = cpu_to_le32(cmd.cdw12);
437 c.common.cdw10[3] = cpu_to_le32(cmd.cdw13);
438 c.common.cdw10[4] = cpu_to_le32(cmd.cdw14);
439 c.common.cdw10[5] = cpu_to_le32(cmd.cdw15);
440
441 if (cmd.timeout_ms)
442 timeout = msecs_to_jiffies(cmd.timeout_ms);
443
444 status = nvme_submit_user_cmd(ns ? ns->queue : ctrl->admin_q, &c,
445 (void __user *)(uintptr_t)cmd.addr, cmd.data_len,
446 &cmd.result, timeout);
447 if (status >= 0) {
448 if (put_user(cmd.result, &ucmd->result))
449 return -EFAULT;
450 }
451
452 return status;
453 }
454
455 static int nvme_ioctl(struct block_device *bdev, fmode_t mode,
456 unsigned int cmd, unsigned long arg)
457 {
458 struct nvme_ns *ns = bdev->bd_disk->private_data;
459
460 switch (cmd) {
461 case NVME_IOCTL_ID:
462 force_successful_syscall_return();
463 return ns->ns_id;
464 case NVME_IOCTL_ADMIN_CMD:
465 return nvme_user_cmd(ns->ctrl, NULL, (void __user *)arg);
466 case NVME_IOCTL_IO_CMD:
467 return nvme_user_cmd(ns->ctrl, ns, (void __user *)arg);
468 case NVME_IOCTL_SUBMIT_IO:
469 return nvme_submit_io(ns, (void __user *)arg);
470 case SG_GET_VERSION_NUM:
471 return nvme_sg_get_version_num((void __user *)arg);
472 case SG_IO:
473 return nvme_sg_io(ns, (void __user *)arg);
474 default:
475 return -ENOTTY;
476 }
477 }
478
479 #ifdef CONFIG_COMPAT
480 static int nvme_compat_ioctl(struct block_device *bdev, fmode_t mode,
481 unsigned int cmd, unsigned long arg)
482 {
483 switch (cmd) {
484 case SG_IO:
485 return -ENOIOCTLCMD;
486 }
487 return nvme_ioctl(bdev, mode, cmd, arg);
488 }
489 #else
490 #define nvme_compat_ioctl NULL
491 #endif
492
493 static int nvme_open(struct block_device *bdev, fmode_t mode)
494 {
495 return nvme_get_ns_from_disk(bdev->bd_disk) ? 0 : -ENXIO;
496 }
497
498 static void nvme_release(struct gendisk *disk, fmode_t mode)
499 {
500 nvme_put_ns(disk->private_data);
501 }
502
503 static int nvme_getgeo(struct block_device *bdev, struct hd_geometry *geo)
504 {
505 /* some standard values */
506 geo->heads = 1 << 6;
507 geo->sectors = 1 << 5;
508 geo->cylinders = get_capacity(bdev->bd_disk) >> 11;
509 return 0;
510 }
511
512 #ifdef CONFIG_BLK_DEV_INTEGRITY
513 static void nvme_init_integrity(struct nvme_ns *ns)
514 {
515 struct blk_integrity integrity;
516
517 switch (ns->pi_type) {
518 case NVME_NS_DPS_PI_TYPE3:
519 integrity.profile = &t10_pi_type3_crc;
520 break;
521 case NVME_NS_DPS_PI_TYPE1:
522 case NVME_NS_DPS_PI_TYPE2:
523 integrity.profile = &t10_pi_type1_crc;
524 break;
525 default:
526 integrity.profile = NULL;
527 break;
528 }
529 integrity.tuple_size = ns->ms;
530 blk_integrity_register(ns->disk, &integrity);
531 blk_queue_max_integrity_segments(ns->queue, 1);
532 }
533 #else
534 static void nvme_init_integrity(struct nvme_ns *ns)
535 {
536 }
537 #endif /* CONFIG_BLK_DEV_INTEGRITY */
538
539 static void nvme_config_discard(struct nvme_ns *ns)
540 {
541 u32 logical_block_size = queue_logical_block_size(ns->queue);
542 ns->queue->limits.discard_zeroes_data = 0;
543 ns->queue->limits.discard_alignment = logical_block_size;
544 ns->queue->limits.discard_granularity = logical_block_size;
545 blk_queue_max_discard_sectors(ns->queue, 0xffffffff);
546 queue_flag_set_unlocked(QUEUE_FLAG_DISCARD, ns->queue);
547 }
548
549 static int nvme_revalidate_disk(struct gendisk *disk)
550 {
551 struct nvme_ns *ns = disk->private_data;
552 struct nvme_id_ns *id;
553 u8 lbaf, pi_type;
554 u16 old_ms;
555 unsigned short bs;
556
557 if (nvme_identify_ns(ns->ctrl, ns->ns_id, &id)) {
558 dev_warn(ns->ctrl->dev, "%s: Identify failure nvme%dn%d\n",
559 __func__, ns->ctrl->instance, ns->ns_id);
560 return -ENODEV;
561 }
562 if (id->ncap == 0) {
563 kfree(id);
564 return -ENODEV;
565 }
566
567 if (nvme_nvm_ns_supported(ns, id) && ns->type != NVME_NS_LIGHTNVM) {
568 if (nvme_nvm_register(ns->queue, disk->disk_name)) {
569 dev_warn(ns->ctrl->dev,
570 "%s: LightNVM init failure\n", __func__);
571 kfree(id);
572 return -ENODEV;
573 }
574 ns->type = NVME_NS_LIGHTNVM;
575 }
576
577 old_ms = ns->ms;
578 lbaf = id->flbas & NVME_NS_FLBAS_LBA_MASK;
579 ns->lba_shift = id->lbaf[lbaf].ds;
580 ns->ms = le16_to_cpu(id->lbaf[lbaf].ms);
581 ns->ext = ns->ms && (id->flbas & NVME_NS_FLBAS_META_EXT);
582
583 /*
584 * If identify namespace failed, use default 512 byte block size so
585 * block layer can use before failing read/write for 0 capacity.
586 */
587 if (ns->lba_shift == 0)
588 ns->lba_shift = 9;
589 bs = 1 << ns->lba_shift;
590 /* XXX: PI implementation requires metadata equal t10 pi tuple size */
591 pi_type = ns->ms == sizeof(struct t10_pi_tuple) ?
592 id->dps & NVME_NS_DPS_PI_MASK : 0;
593
594 blk_mq_freeze_queue(disk->queue);
595 if (blk_get_integrity(disk) && (ns->pi_type != pi_type ||
596 ns->ms != old_ms ||
597 bs != queue_logical_block_size(disk->queue) ||
598 (ns->ms && ns->ext)))
599 blk_integrity_unregister(disk);
600
601 ns->pi_type = pi_type;
602 blk_queue_logical_block_size(ns->queue, bs);
603
604 if (ns->ms && !blk_get_integrity(disk) && !ns->ext)
605 nvme_init_integrity(ns);
606 if (ns->ms && !(ns->ms == 8 && ns->pi_type) && !blk_get_integrity(disk))
607 set_capacity(disk, 0);
608 else
609 set_capacity(disk, le64_to_cpup(&id->nsze) << (ns->lba_shift - 9));
610
611 if (ns->ctrl->oncs & NVME_CTRL_ONCS_DSM)
612 nvme_config_discard(ns);
613 blk_mq_unfreeze_queue(disk->queue);
614
615 kfree(id);
616 return 0;
617 }
618
619 static char nvme_pr_type(enum pr_type type)
620 {
621 switch (type) {
622 case PR_WRITE_EXCLUSIVE:
623 return 1;
624 case PR_EXCLUSIVE_ACCESS:
625 return 2;
626 case PR_WRITE_EXCLUSIVE_REG_ONLY:
627 return 3;
628 case PR_EXCLUSIVE_ACCESS_REG_ONLY:
629 return 4;
630 case PR_WRITE_EXCLUSIVE_ALL_REGS:
631 return 5;
632 case PR_EXCLUSIVE_ACCESS_ALL_REGS:
633 return 6;
634 default:
635 return 0;
636 }
637 };
638
639 static int nvme_pr_command(struct block_device *bdev, u32 cdw10,
640 u64 key, u64 sa_key, u8 op)
641 {
642 struct nvme_ns *ns = bdev->bd_disk->private_data;
643 struct nvme_command c;
644 u8 data[16] = { 0, };
645
646 put_unaligned_le64(key, &data[0]);
647 put_unaligned_le64(sa_key, &data[8]);
648
649 memset(&c, 0, sizeof(c));
650 c.common.opcode = op;
651 c.common.nsid = cpu_to_le32(ns->ns_id);
652 c.common.cdw10[0] = cpu_to_le32(cdw10);
653
654 return nvme_submit_sync_cmd(ns->queue, &c, data, 16);
655 }
656
657 static int nvme_pr_register(struct block_device *bdev, u64 old,
658 u64 new, unsigned flags)
659 {
660 u32 cdw10;
661
662 if (flags & ~PR_FL_IGNORE_KEY)
663 return -EOPNOTSUPP;
664
665 cdw10 = old ? 2 : 0;
666 cdw10 |= (flags & PR_FL_IGNORE_KEY) ? 1 << 3 : 0;
667 cdw10 |= (1 << 30) | (1 << 31); /* PTPL=1 */
668 return nvme_pr_command(bdev, cdw10, old, new, nvme_cmd_resv_register);
669 }
670
671 static int nvme_pr_reserve(struct block_device *bdev, u64 key,
672 enum pr_type type, unsigned flags)
673 {
674 u32 cdw10;
675
676 if (flags & ~PR_FL_IGNORE_KEY)
677 return -EOPNOTSUPP;
678
679 cdw10 = nvme_pr_type(type) << 8;
680 cdw10 |= ((flags & PR_FL_IGNORE_KEY) ? 1 << 3 : 0);
681 return nvme_pr_command(bdev, cdw10, key, 0, nvme_cmd_resv_acquire);
682 }
683
684 static int nvme_pr_preempt(struct block_device *bdev, u64 old, u64 new,
685 enum pr_type type, bool abort)
686 {
687 u32 cdw10 = nvme_pr_type(type) << 8 | abort ? 2 : 1;
688 return nvme_pr_command(bdev, cdw10, old, new, nvme_cmd_resv_acquire);
689 }
690
691 static int nvme_pr_clear(struct block_device *bdev, u64 key)
692 {
693 u32 cdw10 = 1 | (key ? 1 << 3 : 0);
694 return nvme_pr_command(bdev, cdw10, key, 0, nvme_cmd_resv_register);
695 }
696
697 static int nvme_pr_release(struct block_device *bdev, u64 key, enum pr_type type)
698 {
699 u32 cdw10 = nvme_pr_type(type) << 8 | key ? 1 << 3 : 0;
700 return nvme_pr_command(bdev, cdw10, key, 0, nvme_cmd_resv_release);
701 }
702
703 static const struct pr_ops nvme_pr_ops = {
704 .pr_register = nvme_pr_register,
705 .pr_reserve = nvme_pr_reserve,
706 .pr_release = nvme_pr_release,
707 .pr_preempt = nvme_pr_preempt,
708 .pr_clear = nvme_pr_clear,
709 };
710
711 static const struct block_device_operations nvme_fops = {
712 .owner = THIS_MODULE,
713 .ioctl = nvme_ioctl,
714 .compat_ioctl = nvme_compat_ioctl,
715 .open = nvme_open,
716 .release = nvme_release,
717 .getgeo = nvme_getgeo,
718 .revalidate_disk= nvme_revalidate_disk,
719 .pr_ops = &nvme_pr_ops,
720 };
721
722 static int nvme_wait_ready(struct nvme_ctrl *ctrl, u64 cap, bool enabled)
723 {
724 unsigned long timeout =
725 ((NVME_CAP_TIMEOUT(cap) + 1) * HZ / 2) + jiffies;
726 u32 csts, bit = enabled ? NVME_CSTS_RDY : 0;
727 int ret;
728
729 while ((ret = ctrl->ops->reg_read32(ctrl, NVME_REG_CSTS, &csts)) == 0) {
730 if ((csts & NVME_CSTS_RDY) == bit)
731 break;
732
733 msleep(100);
734 if (fatal_signal_pending(current))
735 return -EINTR;
736 if (time_after(jiffies, timeout)) {
737 dev_err(ctrl->dev,
738 "Device not ready; aborting %s\n", enabled ?
739 "initialisation" : "reset");
740 return -ENODEV;
741 }
742 }
743
744 return ret;
745 }
746
747 /*
748 * If the device has been passed off to us in an enabled state, just clear
749 * the enabled bit. The spec says we should set the 'shutdown notification
750 * bits', but doing so may cause the device to complete commands to the
751 * admin queue ... and we don't know what memory that might be pointing at!
752 */
753 int nvme_disable_ctrl(struct nvme_ctrl *ctrl, u64 cap)
754 {
755 int ret;
756
757 ctrl->ctrl_config &= ~NVME_CC_SHN_MASK;
758 ctrl->ctrl_config &= ~NVME_CC_ENABLE;
759
760 ret = ctrl->ops->reg_write32(ctrl, NVME_REG_CC, ctrl->ctrl_config);
761 if (ret)
762 return ret;
763 return nvme_wait_ready(ctrl, cap, false);
764 }
765
766 int nvme_enable_ctrl(struct nvme_ctrl *ctrl, u64 cap)
767 {
768 /*
769 * Default to a 4K page size, with the intention to update this
770 * path in the future to accomodate architectures with differing
771 * kernel and IO page sizes.
772 */
773 unsigned dev_page_min = NVME_CAP_MPSMIN(cap) + 12, page_shift = 12;
774 int ret;
775
776 if (page_shift < dev_page_min) {
777 dev_err(ctrl->dev,
778 "Minimum device page size %u too large for host (%u)\n",
779 1 << dev_page_min, 1 << page_shift);
780 return -ENODEV;
781 }
782
783 ctrl->page_size = 1 << page_shift;
784
785 ctrl->ctrl_config = NVME_CC_CSS_NVM;
786 ctrl->ctrl_config |= (page_shift - 12) << NVME_CC_MPS_SHIFT;
787 ctrl->ctrl_config |= NVME_CC_ARB_RR | NVME_CC_SHN_NONE;
788 ctrl->ctrl_config |= NVME_CC_IOSQES | NVME_CC_IOCQES;
789 ctrl->ctrl_config |= NVME_CC_ENABLE;
790
791 ret = ctrl->ops->reg_write32(ctrl, NVME_REG_CC, ctrl->ctrl_config);
792 if (ret)
793 return ret;
794 return nvme_wait_ready(ctrl, cap, true);
795 }
796
797 int nvme_shutdown_ctrl(struct nvme_ctrl *ctrl)
798 {
799 unsigned long timeout = SHUTDOWN_TIMEOUT + jiffies;
800 u32 csts;
801 int ret;
802
803 ctrl->ctrl_config &= ~NVME_CC_SHN_MASK;
804 ctrl->ctrl_config |= NVME_CC_SHN_NORMAL;
805
806 ret = ctrl->ops->reg_write32(ctrl, NVME_REG_CC, ctrl->ctrl_config);
807 if (ret)
808 return ret;
809
810 while ((ret = ctrl->ops->reg_read32(ctrl, NVME_REG_CSTS, &csts)) == 0) {
811 if ((csts & NVME_CSTS_SHST_MASK) == NVME_CSTS_SHST_CMPLT)
812 break;
813
814 msleep(100);
815 if (fatal_signal_pending(current))
816 return -EINTR;
817 if (time_after(jiffies, timeout)) {
818 dev_err(ctrl->dev,
819 "Device shutdown incomplete; abort shutdown\n");
820 return -ENODEV;
821 }
822 }
823
824 return ret;
825 }
826
827 /*
828 * Initialize the cached copies of the Identify data and various controller
829 * register in our nvme_ctrl structure. This should be called as soon as
830 * the admin queue is fully up and running.
831 */
832 int nvme_init_identify(struct nvme_ctrl *ctrl)
833 {
834 struct nvme_id_ctrl *id;
835 u64 cap;
836 int ret, page_shift;
837
838 ret = ctrl->ops->reg_read32(ctrl, NVME_REG_VS, &ctrl->vs);
839 if (ret) {
840 dev_err(ctrl->dev, "Reading VS failed (%d)\n", ret);
841 return ret;
842 }
843
844 ret = ctrl->ops->reg_read64(ctrl, NVME_REG_CAP, &cap);
845 if (ret) {
846 dev_err(ctrl->dev, "Reading CAP failed (%d)\n", ret);
847 return ret;
848 }
849 page_shift = NVME_CAP_MPSMIN(cap) + 12;
850
851 if (ctrl->vs >= NVME_VS(1, 1))
852 ctrl->subsystem = NVME_CAP_NSSRC(cap);
853
854 ret = nvme_identify_ctrl(ctrl, &id);
855 if (ret) {
856 dev_err(ctrl->dev, "Identify Controller failed (%d)\n", ret);
857 return -EIO;
858 }
859
860 ctrl->oncs = le16_to_cpup(&id->oncs);
861 atomic_set(&ctrl->abort_limit, id->acl + 1);
862 ctrl->vwc = id->vwc;
863 memcpy(ctrl->serial, id->sn, sizeof(id->sn));
864 memcpy(ctrl->model, id->mn, sizeof(id->mn));
865 memcpy(ctrl->firmware_rev, id->fr, sizeof(id->fr));
866 if (id->mdts)
867 ctrl->max_hw_sectors = 1 << (id->mdts + page_shift - 9);
868 else
869 ctrl->max_hw_sectors = UINT_MAX;
870
871 if ((ctrl->quirks & NVME_QUIRK_STRIPE_SIZE) && id->vs[3]) {
872 unsigned int max_hw_sectors;
873
874 ctrl->stripe_size = 1 << (id->vs[3] + page_shift);
875 max_hw_sectors = ctrl->stripe_size >> (page_shift - 9);
876 if (ctrl->max_hw_sectors) {
877 ctrl->max_hw_sectors = min(max_hw_sectors,
878 ctrl->max_hw_sectors);
879 } else {
880 ctrl->max_hw_sectors = max_hw_sectors;
881 }
882 }
883
884 kfree(id);
885 return 0;
886 }
887
888 static int nvme_dev_open(struct inode *inode, struct file *file)
889 {
890 struct nvme_ctrl *ctrl;
891 int instance = iminor(inode);
892 int ret = -ENODEV;
893
894 spin_lock(&dev_list_lock);
895 list_for_each_entry(ctrl, &nvme_ctrl_list, node) {
896 if (ctrl->instance != instance)
897 continue;
898
899 if (!ctrl->admin_q) {
900 ret = -EWOULDBLOCK;
901 break;
902 }
903 if (!kref_get_unless_zero(&ctrl->kref))
904 break;
905 file->private_data = ctrl;
906 ret = 0;
907 break;
908 }
909 spin_unlock(&dev_list_lock);
910
911 return ret;
912 }
913
914 static int nvme_dev_release(struct inode *inode, struct file *file)
915 {
916 nvme_put_ctrl(file->private_data);
917 return 0;
918 }
919
920 static long nvme_dev_ioctl(struct file *file, unsigned int cmd,
921 unsigned long arg)
922 {
923 struct nvme_ctrl *ctrl = file->private_data;
924 void __user *argp = (void __user *)arg;
925 struct nvme_ns *ns;
926
927 switch (cmd) {
928 case NVME_IOCTL_ADMIN_CMD:
929 return nvme_user_cmd(ctrl, NULL, argp);
930 case NVME_IOCTL_IO_CMD:
931 if (list_empty(&ctrl->namespaces))
932 return -ENOTTY;
933 ns = list_first_entry(&ctrl->namespaces, struct nvme_ns, list);
934 return nvme_user_cmd(ctrl, ns, argp);
935 case NVME_IOCTL_RESET:
936 dev_warn(ctrl->dev, "resetting controller\n");
937 return ctrl->ops->reset_ctrl(ctrl);
938 case NVME_IOCTL_SUBSYS_RESET:
939 return nvme_reset_subsystem(ctrl);
940 default:
941 return -ENOTTY;
942 }
943 }
944
945 static const struct file_operations nvme_dev_fops = {
946 .owner = THIS_MODULE,
947 .open = nvme_dev_open,
948 .release = nvme_dev_release,
949 .unlocked_ioctl = nvme_dev_ioctl,
950 .compat_ioctl = nvme_dev_ioctl,
951 };
952
953 static ssize_t nvme_sysfs_reset(struct device *dev,
954 struct device_attribute *attr, const char *buf,
955 size_t count)
956 {
957 struct nvme_ctrl *ctrl = dev_get_drvdata(dev);
958 int ret;
959
960 ret = ctrl->ops->reset_ctrl(ctrl);
961 if (ret < 0)
962 return ret;
963 return count;
964 }
965 static DEVICE_ATTR(reset_controller, S_IWUSR, NULL, nvme_sysfs_reset);
966
967 static int ns_cmp(void *priv, struct list_head *a, struct list_head *b)
968 {
969 struct nvme_ns *nsa = container_of(a, struct nvme_ns, list);
970 struct nvme_ns *nsb = container_of(b, struct nvme_ns, list);
971
972 return nsa->ns_id - nsb->ns_id;
973 }
974
975 static struct nvme_ns *nvme_find_ns(struct nvme_ctrl *ctrl, unsigned nsid)
976 {
977 struct nvme_ns *ns;
978
979 list_for_each_entry(ns, &ctrl->namespaces, list) {
980 if (ns->ns_id == nsid)
981 return ns;
982 if (ns->ns_id > nsid)
983 break;
984 }
985 return NULL;
986 }
987
988 static void nvme_alloc_ns(struct nvme_ctrl *ctrl, unsigned nsid)
989 {
990 struct nvme_ns *ns;
991 struct gendisk *disk;
992 int node = dev_to_node(ctrl->dev);
993
994 ns = kzalloc_node(sizeof(*ns), GFP_KERNEL, node);
995 if (!ns)
996 return;
997
998 ns->queue = blk_mq_init_queue(ctrl->tagset);
999 if (IS_ERR(ns->queue))
1000 goto out_free_ns;
1001 queue_flag_set_unlocked(QUEUE_FLAG_NOMERGES, ns->queue);
1002 queue_flag_set_unlocked(QUEUE_FLAG_NONROT, ns->queue);
1003 ns->queue->queuedata = ns;
1004 ns->ctrl = ctrl;
1005
1006 disk = alloc_disk_node(0, node);
1007 if (!disk)
1008 goto out_free_queue;
1009
1010 kref_init(&ns->kref);
1011 ns->ns_id = nsid;
1012 ns->disk = disk;
1013 ns->lba_shift = 9; /* set to a default value for 512 until disk is validated */
1014
1015 blk_queue_logical_block_size(ns->queue, 1 << ns->lba_shift);
1016 if (ctrl->max_hw_sectors) {
1017 blk_queue_max_hw_sectors(ns->queue, ctrl->max_hw_sectors);
1018 blk_queue_max_segments(ns->queue,
1019 (ctrl->max_hw_sectors / (ctrl->page_size >> 9)) + 1);
1020 }
1021 if (ctrl->stripe_size)
1022 blk_queue_chunk_sectors(ns->queue, ctrl->stripe_size >> 9);
1023 if (ctrl->vwc & NVME_CTRL_VWC_PRESENT)
1024 blk_queue_flush(ns->queue, REQ_FLUSH | REQ_FUA);
1025 blk_queue_virt_boundary(ns->queue, ctrl->page_size - 1);
1026
1027 disk->major = nvme_major;
1028 disk->first_minor = 0;
1029 disk->fops = &nvme_fops;
1030 disk->private_data = ns;
1031 disk->queue = ns->queue;
1032 disk->driverfs_dev = ctrl->device;
1033 disk->flags = GENHD_FL_EXT_DEVT;
1034 sprintf(disk->disk_name, "nvme%dn%d", ctrl->instance, nsid);
1035
1036 if (nvme_revalidate_disk(ns->disk))
1037 goto out_free_disk;
1038
1039 list_add_tail(&ns->list, &ctrl->namespaces);
1040 kref_get(&ctrl->kref);
1041 if (ns->type != NVME_NS_LIGHTNVM)
1042 add_disk(ns->disk);
1043
1044 return;
1045 out_free_disk:
1046 kfree(disk);
1047 out_free_queue:
1048 blk_cleanup_queue(ns->queue);
1049 out_free_ns:
1050 kfree(ns);
1051 }
1052
1053 static void nvme_ns_remove(struct nvme_ns *ns)
1054 {
1055 bool kill = nvme_io_incapable(ns->ctrl) &&
1056 !blk_queue_dying(ns->queue);
1057
1058 if (kill)
1059 blk_set_queue_dying(ns->queue);
1060 if (ns->disk->flags & GENHD_FL_UP) {
1061 if (blk_get_integrity(ns->disk))
1062 blk_integrity_unregister(ns->disk);
1063 del_gendisk(ns->disk);
1064 }
1065 if (kill || !blk_queue_dying(ns->queue)) {
1066 blk_mq_abort_requeue_list(ns->queue);
1067 blk_cleanup_queue(ns->queue);
1068 }
1069 list_del_init(&ns->list);
1070 nvme_put_ns(ns);
1071 }
1072
1073 static void nvme_validate_ns(struct nvme_ctrl *ctrl, unsigned nsid)
1074 {
1075 struct nvme_ns *ns;
1076
1077 ns = nvme_find_ns(ctrl, nsid);
1078 if (ns) {
1079 if (revalidate_disk(ns->disk))
1080 nvme_ns_remove(ns);
1081 } else
1082 nvme_alloc_ns(ctrl, nsid);
1083 }
1084
1085 static int nvme_scan_ns_list(struct nvme_ctrl *ctrl, unsigned nn)
1086 {
1087 struct nvme_ns *ns;
1088 __le32 *ns_list;
1089 unsigned i, j, nsid, prev = 0, num_lists = DIV_ROUND_UP(nn, 1024);
1090 int ret = 0;
1091
1092 ns_list = kzalloc(0x1000, GFP_KERNEL);
1093 if (!ns_list)
1094 return -ENOMEM;
1095
1096 for (i = 0; i < num_lists; i++) {
1097 ret = nvme_identify_ns_list(ctrl, prev, ns_list);
1098 if (ret)
1099 goto out;
1100
1101 for (j = 0; j < min(nn, 1024U); j++) {
1102 nsid = le32_to_cpu(ns_list[j]);
1103 if (!nsid)
1104 goto out;
1105
1106 nvme_validate_ns(ctrl, nsid);
1107
1108 while (++prev < nsid) {
1109 ns = nvme_find_ns(ctrl, prev);
1110 if (ns)
1111 nvme_ns_remove(ns);
1112 }
1113 }
1114 nn -= j;
1115 }
1116 out:
1117 kfree(ns_list);
1118 return ret;
1119 }
1120
1121 static void __nvme_scan_namespaces(struct nvme_ctrl *ctrl, unsigned nn)
1122 {
1123 struct nvme_ns *ns, *next;
1124 unsigned i;
1125
1126 for (i = 1; i <= nn; i++)
1127 nvme_validate_ns(ctrl, i);
1128
1129 list_for_each_entry_safe(ns, next, &ctrl->namespaces, list) {
1130 if (ns->ns_id > nn)
1131 nvme_ns_remove(ns);
1132 }
1133 }
1134
1135 void nvme_scan_namespaces(struct nvme_ctrl *ctrl)
1136 {
1137 struct nvme_id_ctrl *id;
1138 unsigned nn;
1139
1140 if (nvme_identify_ctrl(ctrl, &id))
1141 return;
1142
1143 nn = le32_to_cpu(id->nn);
1144 if (ctrl->vs >= NVME_VS(1, 1) &&
1145 !(ctrl->quirks & NVME_QUIRK_IDENTIFY_CNS)) {
1146 if (!nvme_scan_ns_list(ctrl, nn))
1147 goto done;
1148 }
1149 __nvme_scan_namespaces(ctrl, le32_to_cpup(&id->nn));
1150 done:
1151 list_sort(NULL, &ctrl->namespaces, ns_cmp);
1152 kfree(id);
1153 }
1154
1155 void nvme_remove_namespaces(struct nvme_ctrl *ctrl)
1156 {
1157 struct nvme_ns *ns, *next;
1158
1159 list_for_each_entry_safe(ns, next, &ctrl->namespaces, list)
1160 nvme_ns_remove(ns);
1161 }
1162
1163 static DEFINE_IDA(nvme_instance_ida);
1164
1165 static int nvme_set_instance(struct nvme_ctrl *ctrl)
1166 {
1167 int instance, error;
1168
1169 do {
1170 if (!ida_pre_get(&nvme_instance_ida, GFP_KERNEL))
1171 return -ENODEV;
1172
1173 spin_lock(&dev_list_lock);
1174 error = ida_get_new(&nvme_instance_ida, &instance);
1175 spin_unlock(&dev_list_lock);
1176 } while (error == -EAGAIN);
1177
1178 if (error)
1179 return -ENODEV;
1180
1181 ctrl->instance = instance;
1182 return 0;
1183 }
1184
1185 static void nvme_release_instance(struct nvme_ctrl *ctrl)
1186 {
1187 spin_lock(&dev_list_lock);
1188 ida_remove(&nvme_instance_ida, ctrl->instance);
1189 spin_unlock(&dev_list_lock);
1190 }
1191
1192 void nvme_uninit_ctrl(struct nvme_ctrl *ctrl)
1193 {
1194 device_remove_file(ctrl->device, &dev_attr_reset_controller);
1195 device_destroy(nvme_class, MKDEV(nvme_char_major, ctrl->instance));
1196
1197 spin_lock(&dev_list_lock);
1198 list_del(&ctrl->node);
1199 spin_unlock(&dev_list_lock);
1200 }
1201
1202 static void nvme_free_ctrl(struct kref *kref)
1203 {
1204 struct nvme_ctrl *ctrl = container_of(kref, struct nvme_ctrl, kref);
1205
1206 put_device(ctrl->device);
1207 nvme_release_instance(ctrl);
1208
1209 ctrl->ops->free_ctrl(ctrl);
1210 }
1211
1212 void nvme_put_ctrl(struct nvme_ctrl *ctrl)
1213 {
1214 kref_put(&ctrl->kref, nvme_free_ctrl);
1215 }
1216
1217 /*
1218 * Initialize a NVMe controller structures. This needs to be called during
1219 * earliest initialization so that we have the initialized structured around
1220 * during probing.
1221 */
1222 int nvme_init_ctrl(struct nvme_ctrl *ctrl, struct device *dev,
1223 const struct nvme_ctrl_ops *ops, unsigned long quirks)
1224 {
1225 int ret;
1226
1227 INIT_LIST_HEAD(&ctrl->namespaces);
1228 kref_init(&ctrl->kref);
1229 ctrl->dev = dev;
1230 ctrl->ops = ops;
1231 ctrl->quirks = quirks;
1232
1233 ret = nvme_set_instance(ctrl);
1234 if (ret)
1235 goto out;
1236
1237 ctrl->device = device_create(nvme_class, ctrl->dev,
1238 MKDEV(nvme_char_major, ctrl->instance),
1239 dev, "nvme%d", ctrl->instance);
1240 if (IS_ERR(ctrl->device)) {
1241 ret = PTR_ERR(ctrl->device);
1242 goto out_release_instance;
1243 }
1244 get_device(ctrl->device);
1245 dev_set_drvdata(ctrl->device, ctrl);
1246
1247 ret = device_create_file(ctrl->device, &dev_attr_reset_controller);
1248 if (ret)
1249 goto out_put_device;
1250
1251 spin_lock(&dev_list_lock);
1252 list_add_tail(&ctrl->node, &nvme_ctrl_list);
1253 spin_unlock(&dev_list_lock);
1254
1255 return 0;
1256
1257 out_put_device:
1258 put_device(ctrl->device);
1259 device_destroy(nvme_class, MKDEV(nvme_char_major, ctrl->instance));
1260 out_release_instance:
1261 nvme_release_instance(ctrl);
1262 out:
1263 return ret;
1264 }
1265
1266 int __init nvme_core_init(void)
1267 {
1268 int result;
1269
1270 result = register_blkdev(nvme_major, "nvme");
1271 if (result < 0)
1272 return result;
1273 else if (result > 0)
1274 nvme_major = result;
1275
1276 result = __register_chrdev(nvme_char_major, 0, NVME_MINORS, "nvme",
1277 &nvme_dev_fops);
1278 if (result < 0)
1279 goto unregister_blkdev;
1280 else if (result > 0)
1281 nvme_char_major = result;
1282
1283 nvme_class = class_create(THIS_MODULE, "nvme");
1284 if (IS_ERR(nvme_class)) {
1285 result = PTR_ERR(nvme_class);
1286 goto unregister_chrdev;
1287 }
1288
1289 return 0;
1290
1291 unregister_chrdev:
1292 __unregister_chrdev(nvme_char_major, 0, NVME_MINORS, "nvme");
1293 unregister_blkdev:
1294 unregister_blkdev(nvme_major, "nvme");
1295 return result;
1296 }
1297
1298 void nvme_core_exit(void)
1299 {
1300 unregister_blkdev(nvme_major, "nvme");
1301 class_destroy(nvme_class);
1302 __unregister_chrdev(nvme_char_major, 0, NVME_MINORS, "nvme");
1303 }
This page took 0.091679 seconds and 6 git commands to generate.