forked from ~ljy/RK356X_SDK_RELEASE

hc
2024-01-31 f70575805708cabdedea7498aaa3f710fde4d920
kernel/drivers/nvme/host/lightnvm.c
....@@ -1,23 +1,9 @@
1
+// SPDX-License-Identifier: GPL-2.0
12 /*
23 * nvme-lightnvm.c - LightNVM NVMe device
34 *
45 * Copyright (C) 2014-2015 IT University of Copenhagen
56 * Initial release: Matias Bjorling <mb@lightnvm.io>
6
- *
7
- * This program is free software; you can redistribute it and/or
8
- * modify it under the terms of the GNU General Public License version
9
- * 2 as published by the Free Software Foundation.
10
- *
11
- * This program is distributed in the hope that it will be useful, but
12
- * WITHOUT ANY WARRANTY; without even the implied warranty of
13
- * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14
- * General Public License for more details.
15
- *
16
- * You should have received a copy of the GNU General Public License
17
- * along with this program; see the file COPYING. If not, write to
18
- * the Free Software Foundation, 675 Mass Ave, Cambridge, MA 02139,
19
- * USA.
20
- *
217 */
228
239 #include "nvme.h"
....@@ -185,7 +171,7 @@
185171 __le32 tdresv;
186172 __le32 thresv;
187173 __le32 rsvd2[8];
188
- __u8 blk[0];
174
+ __u8 blk[];
189175 };
190176
191177 struct nvme_nvm_id20_addrf {
....@@ -567,23 +553,28 @@
567553 * Expect the lba in device format
568554 */
569555 static int nvme_nvm_get_chk_meta(struct nvm_dev *ndev,
570
- struct nvm_chk_meta *meta,
571
- sector_t slba, int nchks)
556
+ sector_t slba, int nchks,
557
+ struct nvm_chk_meta *meta)
572558 {
573559 struct nvm_geo *geo = &ndev->geo;
574560 struct nvme_ns *ns = ndev->q->queuedata;
575561 struct nvme_ctrl *ctrl = ns->ctrl;
576
- struct nvme_nvm_chk_meta *dev_meta = (struct nvme_nvm_chk_meta *)meta;
562
+ struct nvme_nvm_chk_meta *dev_meta, *dev_meta_off;
577563 struct ppa_addr ppa;
578564 size_t left = nchks * sizeof(struct nvme_nvm_chk_meta);
579565 size_t log_pos, offset, len;
580
- int ret, i, max_len;
566
+ int i, max_len;
567
+ int ret = 0;
581568
582569 /*
583570 * limit requests to maximum 256K to avoid issuing arbitrary large
584571 * requests when the device does not specific a maximum transfer size.
585572 */
586573 max_len = min_t(unsigned int, ctrl->max_hw_sectors << 9, 256 * 1024);
574
+
575
+ dev_meta = kmalloc(max_len, GFP_KERNEL);
576
+ if (!dev_meta)
577
+ return -ENOMEM;
587578
588579 /* Normalize lba address space to obtain log offset */
589580 ppa.ppa = slba;
....@@ -598,29 +589,34 @@
598589 while (left) {
599590 len = min_t(unsigned int, left, max_len);
600591
592
+ memset(dev_meta, 0, max_len);
593
+ dev_meta_off = dev_meta;
594
+
601595 ret = nvme_get_log(ctrl, ns->head->ns_id,
602
- NVME_NVM_LOG_REPORT_CHUNK, 0, dev_meta, len,
603
- offset);
596
+ NVME_NVM_LOG_REPORT_CHUNK, 0, NVME_CSI_NVM,
597
+ dev_meta, len, offset);
604598 if (ret) {
605599 dev_err(ctrl->device, "Get REPORT CHUNK log error\n");
606600 break;
607601 }
608602
609603 for (i = 0; i < len; i += sizeof(struct nvme_nvm_chk_meta)) {
610
- meta->state = dev_meta->state;
611
- meta->type = dev_meta->type;
612
- meta->wi = dev_meta->wi;
613
- meta->slba = le64_to_cpu(dev_meta->slba);
614
- meta->cnlb = le64_to_cpu(dev_meta->cnlb);
615
- meta->wp = le64_to_cpu(dev_meta->wp);
604
+ meta->state = dev_meta_off->state;
605
+ meta->type = dev_meta_off->type;
606
+ meta->wi = dev_meta_off->wi;
607
+ meta->slba = le64_to_cpu(dev_meta_off->slba);
608
+ meta->cnlb = le64_to_cpu(dev_meta_off->cnlb);
609
+ meta->wp = le64_to_cpu(dev_meta_off->wp);
616610
617611 meta++;
618
- dev_meta++;
612
+ dev_meta_off++;
619613 }
620614
621615 offset += len;
622616 left -= len;
623617 }
618
+
619
+ kfree(dev_meta);
624620
625621 return ret;
626622 }
....@@ -657,25 +653,28 @@
657653
658654 nvme_nvm_rqtocmd(rqd, ns, cmd);
659655
660
- rq = nvme_alloc_request(q, (struct nvme_command *)cmd, 0, NVME_QID_ANY);
656
+ rq = nvme_alloc_request(q, (struct nvme_command *)cmd, 0);
661657 if (IS_ERR(rq))
662658 return rq;
663659
664660 rq->cmd_flags &= ~REQ_FAILFAST_DRIVER;
665661
666662 if (rqd->bio)
667
- blk_init_request_from_bio(rq, rqd->bio);
663
+ blk_rq_append_bio(rq, &rqd->bio);
668664 else
669665 rq->ioprio = IOPRIO_PRIO_VALUE(IOPRIO_CLASS_BE, IOPRIO_NORM);
670666
671667 return rq;
672668 }
673669
674
-static int nvme_nvm_submit_io(struct nvm_dev *dev, struct nvm_rq *rqd)
670
+static int nvme_nvm_submit_io(struct nvm_dev *dev, struct nvm_rq *rqd,
671
+ void *buf)
675672 {
673
+ struct nvm_geo *geo = &dev->geo;
676674 struct request_queue *q = dev->q;
677675 struct nvme_nvm_command *cmd;
678676 struct request *rq;
677
+ int ret;
679678
680679 cmd = kzalloc(sizeof(struct nvme_nvm_command), GFP_KERNEL);
681680 if (!cmd)
....@@ -683,8 +682,15 @@
683682
684683 rq = nvme_nvm_alloc_request(q, rqd, cmd);
685684 if (IS_ERR(rq)) {
686
- kfree(cmd);
687
- return PTR_ERR(rq);
685
+ ret = PTR_ERR(rq);
686
+ goto err_free_cmd;
687
+ }
688
+
689
+ if (buf) {
690
+ ret = blk_rq_map_kern(q, rq, buf, geo->csecs * rqd->nr_ppas,
691
+ GFP_KERNEL);
692
+ if (ret)
693
+ goto err_free_cmd;
688694 }
689695
690696 rq->end_io_data = rqd;
....@@ -692,41 +698,18 @@
692698 blk_execute_rq_nowait(q, NULL, rq, 0, nvme_nvm_end_io);
693699
694700 return 0;
695
-}
696701
697
-static int nvme_nvm_submit_io_sync(struct nvm_dev *dev, struct nvm_rq *rqd)
698
-{
699
- struct request_queue *q = dev->q;
700
- struct request *rq;
701
- struct nvme_nvm_command cmd;
702
- int ret = 0;
703
-
704
- memset(&cmd, 0, sizeof(struct nvme_nvm_command));
705
-
706
- rq = nvme_nvm_alloc_request(q, rqd, &cmd);
707
- if (IS_ERR(rq))
708
- return PTR_ERR(rq);
709
-
710
- /* I/Os can fail and the error is signaled through rqd. Callers must
711
- * handle the error accordingly.
712
- */
713
- blk_execute_rq(q, NULL, rq, 0);
714
- if (nvme_req(rq)->flags & NVME_REQ_CANCELLED)
715
- ret = -EINTR;
716
-
717
- rqd->ppa_status = le64_to_cpu(nvme_req(rq)->result.u64);
718
- rqd->error = nvme_req(rq)->status;
719
-
720
- blk_mq_free_request(rq);
721
-
702
+err_free_cmd:
703
+ kfree(cmd);
722704 return ret;
723705 }
724706
725
-static void *nvme_nvm_create_dma_pool(struct nvm_dev *nvmdev, char *name)
707
+static void *nvme_nvm_create_dma_pool(struct nvm_dev *nvmdev, char *name,
708
+ int size)
726709 {
727710 struct nvme_ns *ns = nvmdev->q->queuedata;
728711
729
- return dma_pool_create(name, ns->ctrl->dev, PAGE_SIZE, PAGE_SIZE, 0);
712
+ return dma_pool_create(name, ns->ctrl->dev, size, PAGE_SIZE, 0);
730713 }
731714
732715 static void nvme_nvm_destroy_dma_pool(void *pool)
....@@ -757,7 +740,6 @@
757740 .get_chk_meta = nvme_nvm_get_chk_meta,
758741
759742 .submit_io = nvme_nvm_submit_io,
760
- .submit_io_sync = nvme_nvm_submit_io_sync,
761743
762744 .create_dma_pool = nvme_nvm_create_dma_pool,
763745 .destroy_dma_pool = nvme_nvm_destroy_dma_pool,
....@@ -785,14 +767,14 @@
785767 DECLARE_COMPLETION_ONSTACK(wait);
786768 int ret = 0;
787769
788
- rq = nvme_alloc_request(q, (struct nvme_command *)vcmd, 0,
789
- NVME_QID_ANY);
770
+ rq = nvme_alloc_request(q, (struct nvme_command *)vcmd, 0);
790771 if (IS_ERR(rq)) {
791772 ret = -ENOMEM;
792773 goto err_cmd;
793774 }
794775
795
- rq->timeout = timeout ? timeout : ADMIN_TIMEOUT;
776
+ if (timeout)
777
+ rq->timeout = timeout;
796778
797779 if (ppa_buf && ppa_len) {
798780 ppa_list = dma_pool_alloc(dev->dma_pool, GFP_KERNEL, &ppa_dma);
....@@ -926,9 +908,9 @@
926908 /* cdw11-12 */
927909 c.ph_rw.length = cpu_to_le16(vcmd.nppas);
928910 c.ph_rw.control = cpu_to_le16(vcmd.control);
929
- c.common.cdw10[3] = cpu_to_le32(vcmd.cdw13);
930
- c.common.cdw10[4] = cpu_to_le32(vcmd.cdw14);
931
- c.common.cdw10[5] = cpu_to_le32(vcmd.cdw15);
911
+ c.common.cdw13 = cpu_to_le32(vcmd.cdw13);
912
+ c.common.cdw14 = cpu_to_le32(vcmd.cdw14);
913
+ c.common.cdw15 = cpu_to_le32(vcmd.cdw15);
932914
933915 if (vcmd.timeout_ms)
934916 timeout = msecs_to_jiffies(vcmd.timeout_ms);
....@@ -963,28 +945,27 @@
963945 }
964946 }
965947
966
-void nvme_nvm_update_nvm_info(struct nvme_ns *ns)
967
-{
968
- struct nvm_dev *ndev = ns->ndev;
969
- struct nvm_geo *geo = &ndev->geo;
970
-
971
- if (geo->version == NVM_OCSSD_SPEC_12)
972
- return;
973
-
974
- geo->csecs = 1 << ns->lba_shift;
975
- geo->sos = ns->ms;
976
-}
977
-
978948 int nvme_nvm_register(struct nvme_ns *ns, char *disk_name, int node)
979949 {
980950 struct request_queue *q = ns->queue;
981951 struct nvm_dev *dev;
952
+ struct nvm_geo *geo;
982953
983954 _nvme_nvm_check_size();
984955
985956 dev = nvm_alloc_dev(node);
986957 if (!dev)
987958 return -ENOMEM;
959
+
960
+ /* Note that csecs and sos will be overridden if it is a 1.2 drive. */
961
+ geo = &dev->geo;
962
+ geo->csecs = 1 << ns->lba_shift;
963
+ geo->sos = ns->ms;
964
+ if (ns->features & NVME_NS_EXT_LBAS)
965
+ geo->ext = true;
966
+ else
967
+ geo->ext = false;
968
+ geo->mdts = ns->ctrl->max_hw_sectors;
988969
989970 dev->q = q;
990971 memcpy(dev->name, disk_name, DISK_NAME_LEN);
....@@ -1193,42 +1174,6 @@
11931174 static NVM_DEV_ATTR_12_RO(media_capabilities);
11941175 static NVM_DEV_ATTR_12_RO(max_phys_secs);
11951176
1196
-static struct attribute *nvm_dev_attrs_12[] = {
1197
- &dev_attr_version.attr,
1198
- &dev_attr_capabilities.attr,
1199
-
1200
- &dev_attr_vendor_opcode.attr,
1201
- &dev_attr_device_mode.attr,
1202
- &dev_attr_media_manager.attr,
1203
- &dev_attr_ppa_format.attr,
1204
- &dev_attr_media_type.attr,
1205
- &dev_attr_flash_media_type.attr,
1206
- &dev_attr_num_channels.attr,
1207
- &dev_attr_num_luns.attr,
1208
- &dev_attr_num_planes.attr,
1209
- &dev_attr_num_blocks.attr,
1210
- &dev_attr_num_pages.attr,
1211
- &dev_attr_page_size.attr,
1212
- &dev_attr_hw_sector_size.attr,
1213
- &dev_attr_oob_sector_size.attr,
1214
- &dev_attr_read_typ.attr,
1215
- &dev_attr_read_max.attr,
1216
- &dev_attr_prog_typ.attr,
1217
- &dev_attr_prog_max.attr,
1218
- &dev_attr_erase_typ.attr,
1219
- &dev_attr_erase_max.attr,
1220
- &dev_attr_multiplane_modes.attr,
1221
- &dev_attr_media_capabilities.attr,
1222
- &dev_attr_max_phys_secs.attr,
1223
-
1224
- NULL,
1225
-};
1226
-
1227
-static const struct attribute_group nvm_dev_attr_group_12 = {
1228
- .name = "lightnvm",
1229
- .attrs = nvm_dev_attrs_12,
1230
-};
1231
-
12321177 /* 2.0 values */
12331178 static NVM_DEV_ATTR_20_RO(groups);
12341179 static NVM_DEV_ATTR_20_RO(punits);
....@@ -1244,10 +1189,37 @@
12441189 static NVM_DEV_ATTR_20_RO(reset_typ);
12451190 static NVM_DEV_ATTR_20_RO(reset_max);
12461191
1247
-static struct attribute *nvm_dev_attrs_20[] = {
1192
+static struct attribute *nvm_dev_attrs[] = {
1193
+ /* version agnostic attrs */
12481194 &dev_attr_version.attr,
12491195 &dev_attr_capabilities.attr,
1196
+ &dev_attr_read_typ.attr,
1197
+ &dev_attr_read_max.attr,
12501198
1199
+ /* 1.2 attrs */
1200
+ &dev_attr_vendor_opcode.attr,
1201
+ &dev_attr_device_mode.attr,
1202
+ &dev_attr_media_manager.attr,
1203
+ &dev_attr_ppa_format.attr,
1204
+ &dev_attr_media_type.attr,
1205
+ &dev_attr_flash_media_type.attr,
1206
+ &dev_attr_num_channels.attr,
1207
+ &dev_attr_num_luns.attr,
1208
+ &dev_attr_num_planes.attr,
1209
+ &dev_attr_num_blocks.attr,
1210
+ &dev_attr_num_pages.attr,
1211
+ &dev_attr_page_size.attr,
1212
+ &dev_attr_hw_sector_size.attr,
1213
+ &dev_attr_oob_sector_size.attr,
1214
+ &dev_attr_prog_typ.attr,
1215
+ &dev_attr_prog_max.attr,
1216
+ &dev_attr_erase_typ.attr,
1217
+ &dev_attr_erase_max.attr,
1218
+ &dev_attr_multiplane_modes.attr,
1219
+ &dev_attr_media_capabilities.attr,
1220
+ &dev_attr_max_phys_secs.attr,
1221
+
1222
+ /* 2.0 attrs */
12511223 &dev_attr_groups.attr,
12521224 &dev_attr_punits.attr,
12531225 &dev_attr_chunks.attr,
....@@ -1258,8 +1230,6 @@
12581230 &dev_attr_maxocpu.attr,
12591231 &dev_attr_mw_cunits.attr,
12601232
1261
- &dev_attr_read_typ.attr,
1262
- &dev_attr_read_max.attr,
12631233 &dev_attr_write_typ.attr,
12641234 &dev_attr_write_max.attr,
12651235 &dev_attr_reset_typ.attr,
....@@ -1268,44 +1238,38 @@
12681238 NULL,
12691239 };
12701240
1271
-static const struct attribute_group nvm_dev_attr_group_20 = {
1272
- .name = "lightnvm",
1273
- .attrs = nvm_dev_attrs_20,
1274
-};
1275
-
1276
-int nvme_nvm_register_sysfs(struct nvme_ns *ns)
1241
+static umode_t nvm_dev_attrs_visible(struct kobject *kobj,
1242
+ struct attribute *attr, int index)
12771243 {
1244
+ struct device *dev = container_of(kobj, struct device, kobj);
1245
+ struct gendisk *disk = dev_to_disk(dev);
1246
+ struct nvme_ns *ns = disk->private_data;
12781247 struct nvm_dev *ndev = ns->ndev;
1279
- struct nvm_geo *geo = &ndev->geo;
1248
+ struct device_attribute *dev_attr =
1249
+ container_of(attr, typeof(*dev_attr), attr);
12801250
12811251 if (!ndev)
1282
- return -EINVAL;
1252
+ return 0;
12831253
1284
- switch (geo->major_ver_id) {
1254
+ if (dev_attr->show == nvm_dev_attr_show)
1255
+ return attr->mode;
1256
+
1257
+ switch (ndev->geo.major_ver_id) {
12851258 case 1:
1286
- return sysfs_create_group(&disk_to_dev(ns->disk)->kobj,
1287
- &nvm_dev_attr_group_12);
1288
- case 2:
1289
- return sysfs_create_group(&disk_to_dev(ns->disk)->kobj,
1290
- &nvm_dev_attr_group_20);
1291
- }
1292
-
1293
- return -EINVAL;
1294
-}
1295
-
1296
-void nvme_nvm_unregister_sysfs(struct nvme_ns *ns)
1297
-{
1298
- struct nvm_dev *ndev = ns->ndev;
1299
- struct nvm_geo *geo = &ndev->geo;
1300
-
1301
- switch (geo->major_ver_id) {
1302
- case 1:
1303
- sysfs_remove_group(&disk_to_dev(ns->disk)->kobj,
1304
- &nvm_dev_attr_group_12);
1259
+ if (dev_attr->show == nvm_dev_attr_show_12)
1260
+ return attr->mode;
13051261 break;
13061262 case 2:
1307
- sysfs_remove_group(&disk_to_dev(ns->disk)->kobj,
1308
- &nvm_dev_attr_group_20);
1263
+ if (dev_attr->show == nvm_dev_attr_show_20)
1264
+ return attr->mode;
13091265 break;
13101266 }
1267
+
1268
+ return 0;
13111269 }
1270
+
1271
+const struct attribute_group nvme_nvm_attr_group = {
1272
+ .name = "lightnvm",
1273
+ .attrs = nvm_dev_attrs,
1274
+ .is_visible = nvm_dev_attrs_visible,
1275
+};