nvme: Add NVM Express driver support
[platform/kernel/u-boot.git] / drivers / nvme / nvme-uclass.c
1 /*
2  * Copyright (C) 2017 NXP Semiconductors
3  * Copyright (C) 2017 Bin Meng <bmeng.cn@gmail.com>
4  *
5  * SPDX-License-Identifier:     GPL-2.0+
6  */
7
8 #include <common.h>
9 #include <errno.h>
10 #include <dm.h>
11 #include <dm/device.h>
12 #include "nvme.h"
13
14 static int nvme_info_init(struct uclass *uc)
15 {
16         struct nvme_info *info = (struct nvme_info *)uc->priv;
17
18         info->ns_num = 0;
19         info->ndev_num = 0;
20         INIT_LIST_HEAD(&info->dev_list);
21         nvme_info = info;
22
23         return 0;
24 }
25
26 static int nvme_uclass_post_probe(struct udevice *udev)
27 {
28         char name[20];
29         char *str;
30         struct udevice *ns_udev;
31         int i, ret;
32         struct nvme_dev *ndev = dev_get_priv(udev);
33
34         /* Create a blk device for each namespace */
35         for (i = 0; i < ndev->nn; i++) {
36                 sprintf(name, "nvme-blk#%d", nvme_info->ns_num);
37                 str = strdup(name);
38                 if (!str)
39                         return -ENOMEM;
40
41                 /* The real blksz and size will be set by nvme_blk_probe() */
42                 ret = blk_create_device(udev, "nvme-blk", str, IF_TYPE_NVME,
43                                         nvme_info->ns_num++, 512, 0, &ns_udev);
44                 if (ret) {
45                         free(str);
46                         nvme_info->ns_num--;
47
48                         return ret;
49                 }
50                 device_set_name_alloced(ns_udev);
51         }
52
53         return 0;
54 }
55
56 UCLASS_DRIVER(nvme) = {
57         .name   = "nvme",
58         .id     = UCLASS_NVME,
59         .init   = nvme_info_init,
60         .post_probe = nvme_uclass_post_probe,
61         .priv_auto_alloc_size = sizeof(struct nvme_info),
62 };