forked from ~ljy/RK356X_SDK_RELEASE

hc
2024-05-10 cde9070d9970eef1f7ec2360586c802a16230ad8
kernel/drivers/mtd/nand/raw/nandsim.c
....@@ -1,3 +1,4 @@
1
+// SPDX-License-Identifier: GPL-2.0-or-later
12 /*
23 * NAND flash simulator.
34 *
....@@ -7,20 +8,6 @@
78 *
89 * Note: NS means "NAND Simulator".
910 * Note: Input means input TO flash chip, output means output FROM chip.
10
- *
11
- * This program is free software; you can redistribute it and/or modify it
12
- * under the terms of the GNU General Public License as published by the
13
- * Free Software Foundation; either version 2, or (at your option) any later
14
- * version.
15
- *
16
- * This program is distributed in the hope that it will be useful, but
17
- * WITHOUT ANY WARRANTY; without even the implied warranty of
18
- * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General
19
- * Public License for more details.
20
- *
21
- * You should have received a copy of the GNU General Public License
22
- * along with this program; if not, write to the Free Software
23
- * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307, USA
2411 */
2512
2613 #define pr_fmt(fmt) "[nandsim]" fmt
....@@ -298,6 +285,8 @@
298285 * The structure which describes all the internal simulator data.
299286 */
300287 struct nandsim {
288
+ struct nand_chip chip;
289
+ struct nand_controller base;
301290 struct mtd_partition partitions[CONFIG_NANDSIM_MAX_PARTS];
302291 unsigned int nbparts;
303292
....@@ -364,6 +353,9 @@
364353 void *file_buf;
365354 struct page *held_pages[NS_MAX_HELD_PAGES];
366355 int held_cnt;
356
+
357
+ /* debugfs entry */
358
+ struct dentry *dent;
367359 };
368360
369361 /*
....@@ -443,7 +435,7 @@
443435 /* MTD structure for NAND controller */
444436 static struct mtd_info *nsmtd;
445437
446
-static int nandsim_debugfs_show(struct seq_file *m, void *private)
438
+static int ns_show(struct seq_file *m, void *private)
447439 {
448440 unsigned long wmin = -1, wmax = 0, avg;
449441 unsigned long deciles[10], decile_max[10], tot = 0;
....@@ -494,30 +486,18 @@
494486
495487 return 0;
496488 }
497
-
498
-static int nandsim_debugfs_open(struct inode *inode, struct file *file)
499
-{
500
- return single_open(file, nandsim_debugfs_show, inode->i_private);
501
-}
502
-
503
-static const struct file_operations dfs_fops = {
504
- .open = nandsim_debugfs_open,
505
- .read = seq_read,
506
- .llseek = seq_lseek,
507
- .release = single_release,
508
-};
489
+DEFINE_SHOW_ATTRIBUTE(ns);
509490
510491 /**
511
- * nandsim_debugfs_create - initialize debugfs
512
- * @dev: nandsim device description object
492
+ * ns_debugfs_create - initialize debugfs
493
+ * @ns: nandsim device description object
513494 *
514495 * This function creates all debugfs files for UBI device @ubi. Returns zero in
515496 * case of success and a negative error code in case of failure.
516497 */
517
-static int nandsim_debugfs_create(struct nandsim *dev)
498
+static int ns_debugfs_create(struct nandsim *ns)
518499 {
519500 struct dentry *root = nsmtd->dbg.dfs_dir;
520
- struct dentry *dent;
521501
522502 /*
523503 * Just skip debugfs initialization when the debugfs directory is
....@@ -530,14 +510,19 @@
530510 return 0;
531511 }
532512
533
- dent = debugfs_create_file("nandsim_wear_report", S_IRUSR,
534
- root, dev, &dfs_fops);
535
- if (IS_ERR_OR_NULL(dent)) {
513
+ ns->dent = debugfs_create_file("nandsim_wear_report", 0400, root, ns,
514
+ &ns_fops);
515
+ if (IS_ERR_OR_NULL(ns->dent)) {
536516 NS_ERR("cannot create \"nandsim_wear_report\" debugfs entry\n");
537517 return -1;
538518 }
539519
540520 return 0;
521
+}
522
+
523
+static void ns_debugfs_remove(struct nandsim *ns)
524
+{
525
+ debugfs_remove_recursive(ns->dent);
541526 }
542527
543528 /*
....@@ -546,7 +531,7 @@
546531 *
547532 * RETURNS: 0 if success, -ENOMEM if memory alloc fails.
548533 */
549
-static int __init alloc_device(struct nandsim *ns)
534
+static int __init ns_alloc_device(struct nandsim *ns)
550535 {
551536 struct file *cfile;
552537 int i, err;
....@@ -558,12 +543,12 @@
558543 if (!(cfile->f_mode & FMODE_CAN_READ)) {
559544 NS_ERR("alloc_device: cache file not readable\n");
560545 err = -EINVAL;
561
- goto err_close;
546
+ goto err_close_filp;
562547 }
563548 if (!(cfile->f_mode & FMODE_CAN_WRITE)) {
564549 NS_ERR("alloc_device: cache file not writeable\n");
565550 err = -EINVAL;
566
- goto err_close;
551
+ goto err_close_filp;
567552 }
568553 ns->pages_written =
569554 vzalloc(array_size(sizeof(unsigned long),
....@@ -571,16 +556,24 @@
571556 if (!ns->pages_written) {
572557 NS_ERR("alloc_device: unable to allocate pages written array\n");
573558 err = -ENOMEM;
574
- goto err_close;
559
+ goto err_close_filp;
575560 }
576561 ns->file_buf = kmalloc(ns->geom.pgszoob, GFP_KERNEL);
577562 if (!ns->file_buf) {
578563 NS_ERR("alloc_device: unable to allocate file buf\n");
579564 err = -ENOMEM;
580
- goto err_free;
565
+ goto err_free_pw;
581566 }
582567 ns->cfile = cfile;
568
+
583569 return 0;
570
+
571
+err_free_pw:
572
+ vfree(ns->pages_written);
573
+err_close_filp:
574
+ filp_close(cfile, NULL);
575
+
576
+ return err;
584577 }
585578
586579 ns->pages = vmalloc(array_size(sizeof(union ns_mem), ns->geom.pgnum));
....@@ -595,22 +588,22 @@
595588 ns->geom.pgszoob, 0, 0, NULL);
596589 if (!ns->nand_pages_slab) {
597590 NS_ERR("cache_create: unable to create kmem_cache\n");
598
- return -ENOMEM;
591
+ err = -ENOMEM;
592
+ goto err_free_pg;
599593 }
600594
601595 return 0;
602596
603
-err_free:
604
- vfree(ns->pages_written);
605
-err_close:
606
- filp_close(cfile, NULL);
597
+err_free_pg:
598
+ vfree(ns->pages);
599
+
607600 return err;
608601 }
609602
610603 /*
611604 * Free any allocated pages, and free the array of page pointers.
612605 */
613
-static void free_device(struct nandsim *ns)
606
+static void ns_free_device(struct nandsim *ns)
614607 {
615608 int i;
616609
....@@ -632,7 +625,7 @@
632625 }
633626 }
634627
635
-static char __init *get_partition_name(int i)
628
+static char __init *ns_get_partition_name(int i)
636629 {
637630 return kasprintf(GFP_KERNEL, "NAND simulator partition %d", i);
638631 }
....@@ -642,7 +635,7 @@
642635 *
643636 * RETURNS: 0 if success, -ERRNO if failure.
644637 */
645
-static int __init init_nandsim(struct mtd_info *mtd)
638
+static int __init ns_init(struct mtd_info *mtd)
646639 {
647640 struct nand_chip *chip = mtd_to_nand(mtd);
648641 struct nandsim *ns = nand_get_controller_data(chip);
....@@ -654,9 +647,6 @@
654647 NS_ERR("init_nandsim: nandsim is already initialized\n");
655648 return -EIO;
656649 }
657
-
658
- /* Force mtd to not do delays */
659
- chip->chip_delay = 0;
660650
661651 /* Initialize the NAND flash parameters */
662652 ns->busw = chip->options & NAND_BUSWIDTH_16 ? 16 : 8;
....@@ -718,7 +708,7 @@
718708 NS_ERR("bad partition size.\n");
719709 return -EINVAL;
720710 }
721
- ns->partitions[i].name = get_partition_name(i);
711
+ ns->partitions[i].name = ns_get_partition_name(i);
722712 if (!ns->partitions[i].name) {
723713 NS_ERR("unable to allocate memory.\n");
724714 return -ENOMEM;
....@@ -732,12 +722,14 @@
732722 if (remains) {
733723 if (parts_num + 1 > ARRAY_SIZE(ns->partitions)) {
734724 NS_ERR("too many partitions.\n");
735
- return -EINVAL;
725
+ ret = -EINVAL;
726
+ goto free_partition_names;
736727 }
737
- ns->partitions[i].name = get_partition_name(i);
728
+ ns->partitions[i].name = ns_get_partition_name(i);
738729 if (!ns->partitions[i].name) {
739730 NS_ERR("unable to allocate memory.\n");
740
- return -ENOMEM;
731
+ ret = -ENOMEM;
732
+ goto free_partition_names;
741733 }
742734 ns->partitions[i].offset = next_offset;
743735 ns->partitions[i].size = remains;
....@@ -764,33 +756,48 @@
764756 printk("sector address bytes: %u\n", ns->geom.secaddrbytes);
765757 printk("options: %#x\n", ns->options);
766758
767
- if ((ret = alloc_device(ns)) != 0)
768
- return ret;
759
+ ret = ns_alloc_device(ns);
760
+ if (ret)
761
+ goto free_partition_names;
769762
770763 /* Allocate / initialize the internal buffer */
771764 ns->buf.byte = kmalloc(ns->geom.pgszoob, GFP_KERNEL);
772765 if (!ns->buf.byte) {
773766 NS_ERR("init_nandsim: unable to allocate %u bytes for the internal buffer\n",
774767 ns->geom.pgszoob);
775
- return -ENOMEM;
768
+ ret = -ENOMEM;
769
+ goto free_device;
776770 }
777771 memset(ns->buf.byte, 0xFF, ns->geom.pgszoob);
778772
779773 return 0;
774
+
775
+free_device:
776
+ ns_free_device(ns);
777
+free_partition_names:
778
+ for (i = 0; i < ARRAY_SIZE(ns->partitions); ++i)
779
+ kfree(ns->partitions[i].name);
780
+
781
+ return ret;
780782 }
781783
782784 /*
783785 * Free the nandsim structure.
784786 */
785
-static void free_nandsim(struct nandsim *ns)
787
+static void ns_free(struct nandsim *ns)
786788 {
789
+ int i;
790
+
791
+ for (i = 0; i < ARRAY_SIZE(ns->partitions); ++i)
792
+ kfree(ns->partitions[i].name);
793
+
787794 kfree(ns->buf.byte);
788
- free_device(ns);
795
+ ns_free_device(ns);
789796
790797 return;
791798 }
792799
793
-static int parse_badblocks(struct nandsim *ns, struct mtd_info *mtd)
800
+static int ns_parse_badblocks(struct nandsim *ns, struct mtd_info *mtd)
794801 {
795802 char *w;
796803 int zero_ok;
....@@ -818,7 +825,7 @@
818825 return 0;
819826 }
820827
821
-static int parse_weakblocks(void)
828
+static int ns_parse_weakblocks(void)
822829 {
823830 char *w;
824831 int zero_ok;
....@@ -855,7 +862,7 @@
855862 return 0;
856863 }
857864
858
-static int erase_error(unsigned int erase_block_no)
865
+static int ns_erase_error(unsigned int erase_block_no)
859866 {
860867 struct weak_block *wb;
861868
....@@ -869,7 +876,7 @@
869876 return 0;
870877 }
871878
872
-static int parse_weakpages(void)
879
+static int ns_parse_weakpages(void)
873880 {
874881 char *w;
875882 int zero_ok;
....@@ -906,7 +913,7 @@
906913 return 0;
907914 }
908915
909
-static int write_error(unsigned int page_no)
916
+static int ns_write_error(unsigned int page_no)
910917 {
911918 struct weak_page *wp;
912919
....@@ -920,7 +927,7 @@
920927 return 0;
921928 }
922929
923
-static int parse_gravepages(void)
930
+static int ns_parse_gravepages(void)
924931 {
925932 char *g;
926933 int zero_ok;
....@@ -957,7 +964,7 @@
957964 return 0;
958965 }
959966
960
-static int read_error(unsigned int page_no)
967
+static int ns_read_error(unsigned int page_no)
961968 {
962969 struct grave_page *gp;
963970
....@@ -971,25 +978,7 @@
971978 return 0;
972979 }
973980
974
-static void free_lists(void)
975
-{
976
- struct list_head *pos, *n;
977
- list_for_each_safe(pos, n, &weak_blocks) {
978
- list_del(pos);
979
- kfree(list_entry(pos, struct weak_block, list));
980
- }
981
- list_for_each_safe(pos, n, &weak_pages) {
982
- list_del(pos);
983
- kfree(list_entry(pos, struct weak_page, list));
984
- }
985
- list_for_each_safe(pos, n, &grave_pages) {
986
- list_del(pos);
987
- kfree(list_entry(pos, struct grave_page, list));
988
- }
989
- kfree(erase_block_wear);
990
-}
991
-
992
-static int setup_wear_reporting(struct mtd_info *mtd)
981
+static int ns_setup_wear_reporting(struct mtd_info *mtd)
993982 {
994983 size_t mem;
995984
....@@ -1007,7 +996,7 @@
1007996 return 0;
1008997 }
1009998
1010
-static void update_wear(unsigned int erase_block_no)
999
+static void ns_update_wear(unsigned int erase_block_no)
10111000 {
10121001 if (!erase_block_wear)
10131002 return;
....@@ -1026,7 +1015,7 @@
10261015 /*
10271016 * Returns the string representation of 'state' state.
10281017 */
1029
-static char *get_state_name(uint32_t state)
1018
+static char *ns_get_state_name(uint32_t state)
10301019 {
10311020 switch (NS_STATE(state)) {
10321021 case STATE_CMD_READ0:
....@@ -1086,7 +1075,7 @@
10861075 *
10871076 * RETURNS: 1 if wrong command, 0 if right.
10881077 */
1089
-static int check_command(int cmd)
1078
+static int ns_check_command(int cmd)
10901079 {
10911080 switch (cmd) {
10921081
....@@ -1113,7 +1102,7 @@
11131102 /*
11141103 * Returns state after command is accepted by command number.
11151104 */
1116
-static uint32_t get_state_by_command(unsigned command)
1105
+static uint32_t ns_get_state_by_command(unsigned command)
11171106 {
11181107 switch (command) {
11191108 case NAND_CMD_READ0:
....@@ -1151,7 +1140,7 @@
11511140 /*
11521141 * Move an address byte to the correspondent internal register.
11531142 */
1154
-static inline void accept_addr_byte(struct nandsim *ns, u_char bt)
1143
+static inline void ns_accept_addr_byte(struct nandsim *ns, u_char bt)
11551144 {
11561145 uint byte = (uint)bt;
11571146
....@@ -1169,9 +1158,10 @@
11691158 /*
11701159 * Switch to STATE_READY state.
11711160 */
1172
-static inline void switch_to_ready_state(struct nandsim *ns, u_char status)
1161
+static inline void ns_switch_to_ready_state(struct nandsim *ns, u_char status)
11731162 {
1174
- NS_DBG("switch_to_ready_state: switch to %s state\n", get_state_name(STATE_READY));
1163
+ NS_DBG("switch_to_ready_state: switch to %s state\n",
1164
+ ns_get_state_name(STATE_READY));
11751165
11761166 ns->state = STATE_READY;
11771167 ns->nxstate = STATE_UNKNOWN;
....@@ -1228,7 +1218,7 @@
12281218 * -1 - several matches.
12291219 * 0 - operation is found.
12301220 */
1231
-static int find_operation(struct nandsim *ns, uint32_t flag)
1221
+static int ns_find_operation(struct nandsim *ns, uint32_t flag)
12321222 {
12331223 int opsfound = 0;
12341224 int i, j, idx = 0;
....@@ -1281,7 +1271,8 @@
12811271 ns->state = ns->op[ns->stateidx];
12821272 ns->nxstate = ns->op[ns->stateidx + 1];
12831273 NS_DBG("find_operation: operation found, index: %d, state: %s, nxstate %s\n",
1284
- idx, get_state_name(ns->state), get_state_name(ns->nxstate));
1274
+ idx, ns_get_state_name(ns->state),
1275
+ ns_get_state_name(ns->nxstate));
12851276 return 0;
12861277 }
12871278
....@@ -1289,13 +1280,13 @@
12891280 /* Nothing was found. Try to ignore previous commands (if any) and search again */
12901281 if (ns->npstates != 0) {
12911282 NS_DBG("find_operation: no operation found, try again with state %s\n",
1292
- get_state_name(ns->state));
1283
+ ns_get_state_name(ns->state));
12931284 ns->npstates = 0;
1294
- return find_operation(ns, 0);
1285
+ return ns_find_operation(ns, 0);
12951286
12961287 }
12971288 NS_DBG("find_operation: no operations found\n");
1298
- switch_to_ready_state(ns, NS_STATUS_FAILED(ns));
1289
+ ns_switch_to_ready_state(ns, NS_STATUS_FAILED(ns));
12991290 return -2;
13001291 }
13011292
....@@ -1312,7 +1303,7 @@
13121303 return -1;
13131304 }
13141305
1315
-static void put_pages(struct nandsim *ns)
1306
+static void ns_put_pages(struct nandsim *ns)
13161307 {
13171308 int i;
13181309
....@@ -1321,7 +1312,8 @@
13211312 }
13221313
13231314 /* Get page cache pages in advance to provide NOFS memory allocation */
1324
-static int get_pages(struct nandsim *ns, struct file *file, size_t count, loff_t pos)
1315
+static int ns_get_pages(struct nandsim *ns, struct file *file, size_t count,
1316
+ loff_t pos)
13251317 {
13261318 pgoff_t index, start_index, end_index;
13271319 struct page *page;
....@@ -1341,7 +1333,7 @@
13411333 page = find_or_create_page(mapping, index, GFP_NOFS);
13421334 }
13431335 if (page == NULL) {
1344
- put_pages(ns);
1336
+ ns_put_pages(ns);
13451337 return -ENOMEM;
13461338 }
13471339 unlock_page(page);
....@@ -1351,35 +1343,37 @@
13511343 return 0;
13521344 }
13531345
1354
-static ssize_t read_file(struct nandsim *ns, struct file *file, void *buf, size_t count, loff_t pos)
1346
+static ssize_t ns_read_file(struct nandsim *ns, struct file *file, void *buf,
1347
+ size_t count, loff_t pos)
13551348 {
13561349 ssize_t tx;
13571350 int err;
13581351 unsigned int noreclaim_flag;
13591352
1360
- err = get_pages(ns, file, count, pos);
1353
+ err = ns_get_pages(ns, file, count, pos);
13611354 if (err)
13621355 return err;
13631356 noreclaim_flag = memalloc_noreclaim_save();
13641357 tx = kernel_read(file, buf, count, &pos);
13651358 memalloc_noreclaim_restore(noreclaim_flag);
1366
- put_pages(ns);
1359
+ ns_put_pages(ns);
13671360 return tx;
13681361 }
13691362
1370
-static ssize_t write_file(struct nandsim *ns, struct file *file, void *buf, size_t count, loff_t pos)
1363
+static ssize_t ns_write_file(struct nandsim *ns, struct file *file, void *buf,
1364
+ size_t count, loff_t pos)
13711365 {
13721366 ssize_t tx;
13731367 int err;
13741368 unsigned int noreclaim_flag;
13751369
1376
- err = get_pages(ns, file, count, pos);
1370
+ err = ns_get_pages(ns, file, count, pos);
13771371 if (err)
13781372 return err;
13791373 noreclaim_flag = memalloc_noreclaim_save();
13801374 tx = kernel_write(file, buf, count, &pos);
13811375 memalloc_noreclaim_restore(noreclaim_flag);
1382
- put_pages(ns);
1376
+ ns_put_pages(ns);
13831377 return tx;
13841378 }
13851379
....@@ -1399,11 +1393,11 @@
13991393 return NS_GET_PAGE(ns)->byte + ns->regs.column + ns->regs.off;
14001394 }
14011395
1402
-static int do_read_error(struct nandsim *ns, int num)
1396
+static int ns_do_read_error(struct nandsim *ns, int num)
14031397 {
14041398 unsigned int page_no = ns->regs.row;
14051399
1406
- if (read_error(page_no)) {
1400
+ if (ns_read_error(page_no)) {
14071401 prandom_bytes(ns->buf.byte, num);
14081402 NS_WARN("simulating read error in page %u\n", page_no);
14091403 return 1;
....@@ -1411,7 +1405,7 @@
14111405 return 0;
14121406 }
14131407
1414
-static void do_bit_flips(struct nandsim *ns, int num)
1408
+static void ns_do_bit_flips(struct nandsim *ns, int num)
14151409 {
14161410 if (bitflips && prandom_u32() < (1 << 22)) {
14171411 int flips = 1;
....@@ -1431,7 +1425,7 @@
14311425 /*
14321426 * Fill the NAND buffer with data read from the specified page.
14331427 */
1434
-static void read_page(struct nandsim *ns, int num)
1428
+static void ns_read_page(struct nandsim *ns, int num)
14351429 {
14361430 union ns_mem *mypage;
14371431
....@@ -1445,15 +1439,16 @@
14451439
14461440 NS_DBG("read_page: page %d written, reading from %d\n",
14471441 ns->regs.row, ns->regs.column + ns->regs.off);
1448
- if (do_read_error(ns, num))
1442
+ if (ns_do_read_error(ns, num))
14491443 return;
14501444 pos = (loff_t)NS_RAW_OFFSET(ns) + ns->regs.off;
1451
- tx = read_file(ns, ns->cfile, ns->buf.byte, num, pos);
1445
+ tx = ns_read_file(ns, ns->cfile, ns->buf.byte, num,
1446
+ pos);
14521447 if (tx != num) {
14531448 NS_ERR("read_page: read error for page %d ret %ld\n", ns->regs.row, (long)tx);
14541449 return;
14551450 }
1456
- do_bit_flips(ns, num);
1451
+ ns_do_bit_flips(ns, num);
14571452 }
14581453 return;
14591454 }
....@@ -1465,17 +1460,17 @@
14651460 } else {
14661461 NS_DBG("read_page: page %d allocated, reading from %d\n",
14671462 ns->regs.row, ns->regs.column + ns->regs.off);
1468
- if (do_read_error(ns, num))
1463
+ if (ns_do_read_error(ns, num))
14691464 return;
14701465 memcpy(ns->buf.byte, NS_PAGE_BYTE_OFF(ns), num);
1471
- do_bit_flips(ns, num);
1466
+ ns_do_bit_flips(ns, num);
14721467 }
14731468 }
14741469
14751470 /*
14761471 * Erase all pages in the specified sector.
14771472 */
1478
-static void erase_sector(struct nandsim *ns)
1473
+static void ns_erase_sector(struct nandsim *ns)
14791474 {
14801475 union ns_mem *mypage;
14811476 int i;
....@@ -1503,7 +1498,7 @@
15031498 /*
15041499 * Program the specified page with the contents from the NAND buffer.
15051500 */
1506
-static int prog_page(struct nandsim *ns, int num)
1501
+static int ns_prog_page(struct nandsim *ns, int num)
15071502 {
15081503 int i;
15091504 union ns_mem *mypage;
....@@ -1522,7 +1517,7 @@
15221517 memset(ns->file_buf, 0xff, ns->geom.pgszoob);
15231518 } else {
15241519 all = 0;
1525
- tx = read_file(ns, ns->cfile, pg_off, num, off);
1520
+ tx = ns_read_file(ns, ns->cfile, pg_off, num, off);
15261521 if (tx != num) {
15271522 NS_ERR("prog_page: read error for page %d ret %ld\n", ns->regs.row, (long)tx);
15281523 return -1;
....@@ -1532,14 +1527,15 @@
15321527 pg_off[i] &= ns->buf.byte[i];
15331528 if (all) {
15341529 loff_t pos = (loff_t)ns->regs.row * ns->geom.pgszoob;
1535
- tx = write_file(ns, ns->cfile, ns->file_buf, ns->geom.pgszoob, pos);
1530
+ tx = ns_write_file(ns, ns->cfile, ns->file_buf,
1531
+ ns->geom.pgszoob, pos);
15361532 if (tx != ns->geom.pgszoob) {
15371533 NS_ERR("prog_page: write error for page %d ret %ld\n", ns->regs.row, (long)tx);
15381534 return -1;
15391535 }
15401536 __set_bit(ns->regs.row, ns->pages_written);
15411537 } else {
1542
- tx = write_file(ns, ns->cfile, pg_off, num, off);
1538
+ tx = ns_write_file(ns, ns->cfile, pg_off, num, off);
15431539 if (tx != num) {
15441540 NS_ERR("prog_page: write error for page %d ret %ld\n", ns->regs.row, (long)tx);
15451541 return -1;
....@@ -1577,7 +1573,7 @@
15771573 *
15781574 * RETURNS: 0 if success, -1 if error.
15791575 */
1580
-static int do_state_action(struct nandsim *ns, uint32_t action)
1576
+static int ns_do_state_action(struct nandsim *ns, uint32_t action)
15811577 {
15821578 int num;
15831579 int busdiv = ns->busw == 8 ? 1 : 2;
....@@ -1604,7 +1600,7 @@
16041600 break;
16051601 }
16061602 num = ns->geom.pgszoob - ns->regs.off - ns->regs.column;
1607
- read_page(ns, num);
1603
+ ns_read_page(ns, num);
16081604
16091605 NS_DBG("do_state_action: (ACTION_CPY:) copy %d bytes to int buf, raw offset %d\n",
16101606 num, NS_RAW_OFFSET(ns) + ns->regs.off);
....@@ -1647,14 +1643,14 @@
16471643 ns->regs.row, NS_RAW_OFFSET(ns));
16481644 NS_LOG("erase sector %u\n", erase_block_no);
16491645
1650
- erase_sector(ns);
1646
+ ns_erase_sector(ns);
16511647
16521648 NS_MDELAY(erase_delay);
16531649
16541650 if (erase_block_wear)
1655
- update_wear(erase_block_no);
1651
+ ns_update_wear(erase_block_no);
16561652
1657
- if (erase_error(erase_block_no)) {
1653
+ if (ns_erase_error(erase_block_no)) {
16581654 NS_WARN("simulating erase failure in erase block %u\n", erase_block_no);
16591655 return -1;
16601656 }
....@@ -1678,7 +1674,7 @@
16781674 return -1;
16791675 }
16801676
1681
- if (prog_page(ns, num) == -1)
1677
+ if (ns_prog_page(ns, num) == -1)
16821678 return -1;
16831679
16841680 page_no = ns->regs.row;
....@@ -1690,7 +1686,7 @@
16901686 NS_UDELAY(programm_delay);
16911687 NS_UDELAY(output_cycle * ns->geom.pgsz / 1000 / busdiv);
16921688
1693
- if (write_error(page_no)) {
1689
+ if (ns_write_error(page_no)) {
16941690 NS_WARN("simulating write failure in page %u\n", page_no);
16951691 return -1;
16961692 }
....@@ -1727,7 +1723,7 @@
17271723 /*
17281724 * Switch simulator's state.
17291725 */
1730
-static void switch_state(struct nandsim *ns)
1726
+static void ns_switch_state(struct nandsim *ns)
17311727 {
17321728 if (ns->op) {
17331729 /*
....@@ -1741,11 +1737,13 @@
17411737
17421738 NS_DBG("switch_state: operation is known, switch to the next state, "
17431739 "state: %s, nxstate: %s\n",
1744
- get_state_name(ns->state), get_state_name(ns->nxstate));
1740
+ ns_get_state_name(ns->state),
1741
+ ns_get_state_name(ns->nxstate));
17451742
17461743 /* See, whether we need to do some action */
1747
- if ((ns->state & ACTION_MASK) && do_state_action(ns, ns->state) < 0) {
1748
- switch_to_ready_state(ns, NS_STATUS_FAILED(ns));
1744
+ if ((ns->state & ACTION_MASK) &&
1745
+ ns_do_state_action(ns, ns->state) < 0) {
1746
+ ns_switch_to_ready_state(ns, NS_STATUS_FAILED(ns));
17491747 return;
17501748 }
17511749
....@@ -1759,15 +1757,16 @@
17591757 * The only event causing the switch_state function to
17601758 * be called with yet unknown operation is new command.
17611759 */
1762
- ns->state = get_state_by_command(ns->regs.command);
1760
+ ns->state = ns_get_state_by_command(ns->regs.command);
17631761
17641762 NS_DBG("switch_state: operation is unknown, try to find it\n");
17651763
1766
- if (find_operation(ns, 0) != 0)
1764
+ if (ns_find_operation(ns, 0))
17671765 return;
17681766
1769
- if ((ns->state & ACTION_MASK) && do_state_action(ns, ns->state) < 0) {
1770
- switch_to_ready_state(ns, NS_STATUS_FAILED(ns));
1767
+ if ((ns->state & ACTION_MASK) &&
1768
+ ns_do_state_action(ns, ns->state) < 0) {
1769
+ ns_switch_to_ready_state(ns, NS_STATUS_FAILED(ns));
17711770 return;
17721771 }
17731772 }
....@@ -1795,7 +1794,7 @@
17951794
17961795 NS_DBG("switch_state: operation complete, switch to STATE_READY state\n");
17971796
1798
- switch_to_ready_state(ns, status);
1797
+ ns_switch_to_ready_state(ns, status);
17991798
18001799 return;
18011800 } else if (ns->nxstate & (STATE_DATAIN_MASK | STATE_DATAOUT_MASK)) {
....@@ -1809,7 +1808,8 @@
18091808
18101809 NS_DBG("switch_state: the next state is data I/O, switch, "
18111810 "state: %s, nxstate: %s\n",
1812
- get_state_name(ns->state), get_state_name(ns->nxstate));
1811
+ ns_get_state_name(ns->state),
1812
+ ns_get_state_name(ns->nxstate));
18131813
18141814 /*
18151815 * Set the internal register to the count of bytes which
....@@ -1872,9 +1872,8 @@
18721872 }
18731873 }
18741874
1875
-static u_char ns_nand_read_byte(struct mtd_info *mtd)
1875
+static u_char ns_nand_read_byte(struct nand_chip *chip)
18761876 {
1877
- struct nand_chip *chip = mtd_to_nand(mtd);
18781877 struct nandsim *ns = nand_get_controller_data(chip);
18791878 u_char outb = 0x00;
18801879
....@@ -1888,8 +1887,8 @@
18881887 return outb;
18891888 }
18901889 if (!(ns->state & STATE_DATAOUT_MASK)) {
1891
- NS_WARN("read_byte: unexpected data output cycle, state is %s "
1892
- "return %#x\n", get_state_name(ns->state), (uint)outb);
1890
+ NS_WARN("read_byte: unexpected data output cycle, state is %s return %#x\n",
1891
+ ns_get_state_name(ns->state), (uint)outb);
18931892 return outb;
18941893 }
18951894
....@@ -1928,15 +1927,14 @@
19281927 NS_DBG("read_byte: all bytes were read\n");
19291928
19301929 if (NS_STATE(ns->nxstate) == STATE_READY)
1931
- switch_state(ns);
1930
+ ns_switch_state(ns);
19321931 }
19331932
19341933 return outb;
19351934 }
19361935
1937
-static void ns_nand_write_byte(struct mtd_info *mtd, u_char byte)
1936
+static void ns_nand_write_byte(struct nand_chip *chip, u_char byte)
19381937 {
1939
- struct nand_chip *chip = mtd_to_nand(mtd);
19401938 struct nandsim *ns = nand_get_controller_data(chip);
19411939
19421940 /* Sanity and correctness checks */
....@@ -1956,12 +1954,12 @@
19561954
19571955 if (byte == NAND_CMD_RESET) {
19581956 NS_LOG("reset chip\n");
1959
- switch_to_ready_state(ns, NS_STATUS_OK(ns));
1957
+ ns_switch_to_ready_state(ns, NS_STATUS_OK(ns));
19601958 return;
19611959 }
19621960
19631961 /* Check that the command byte is correct */
1964
- if (check_command(byte)) {
1962
+ if (ns_check_command(byte)) {
19651963 NS_ERR("write_byte: unknown command %#x\n", (uint)byte);
19661964 return;
19671965 }
....@@ -1970,7 +1968,7 @@
19701968 || NS_STATE(ns->state) == STATE_DATAOUT) {
19711969 int row = ns->regs.row;
19721970
1973
- switch_state(ns);
1971
+ ns_switch_state(ns);
19741972 if (byte == NAND_CMD_RNDOUT)
19751973 ns->regs.row = row;
19761974 }
....@@ -1985,16 +1983,17 @@
19851983 * was expected but command was input. In this case ignore
19861984 * previous command(s)/state(s) and accept the last one.
19871985 */
1988
- NS_WARN("write_byte: command (%#x) wasn't expected, expected state is %s, "
1989
- "ignore previous states\n", (uint)byte, get_state_name(ns->nxstate));
1986
+ NS_WARN("write_byte: command (%#x) wasn't expected, expected state is %s, ignore previous states\n",
1987
+ (uint)byte,
1988
+ ns_get_state_name(ns->nxstate));
19901989 }
1991
- switch_to_ready_state(ns, NS_STATUS_FAILED(ns));
1990
+ ns_switch_to_ready_state(ns, NS_STATUS_FAILED(ns));
19921991 }
19931992
19941993 NS_DBG("command byte corresponding to %s state accepted\n",
1995
- get_state_name(get_state_by_command(byte)));
1994
+ ns_get_state_name(ns_get_state_by_command(byte)));
19961995 ns->regs.command = byte;
1997
- switch_state(ns);
1996
+ ns_switch_state(ns);
19981997
19991998 } else if (ns->lines.ale == 1) {
20001999 /*
....@@ -2005,11 +2004,13 @@
20052004
20062005 NS_DBG("write_byte: operation isn't known yet, identify it\n");
20072006
2008
- if (find_operation(ns, 1) < 0)
2007
+ if (ns_find_operation(ns, 1) < 0)
20092008 return;
20102009
2011
- if ((ns->state & ACTION_MASK) && do_state_action(ns, ns->state) < 0) {
2012
- switch_to_ready_state(ns, NS_STATUS_FAILED(ns));
2010
+ if ((ns->state & ACTION_MASK) &&
2011
+ ns_do_state_action(ns, ns->state) < 0) {
2012
+ ns_switch_to_ready_state(ns,
2013
+ NS_STATUS_FAILED(ns));
20132014 return;
20142015 }
20152016
....@@ -2031,20 +2032,20 @@
20312032
20322033 /* Check that chip is expecting address */
20332034 if (!(ns->nxstate & STATE_ADDR_MASK)) {
2034
- NS_ERR("write_byte: address (%#x) isn't expected, expected state is %s, "
2035
- "switch to STATE_READY\n", (uint)byte, get_state_name(ns->nxstate));
2036
- switch_to_ready_state(ns, NS_STATUS_FAILED(ns));
2035
+ NS_ERR("write_byte: address (%#x) isn't expected, expected state is %s, switch to STATE_READY\n",
2036
+ (uint)byte, ns_get_state_name(ns->nxstate));
2037
+ ns_switch_to_ready_state(ns, NS_STATUS_FAILED(ns));
20372038 return;
20382039 }
20392040
20402041 /* Check if this is expected byte */
20412042 if (ns->regs.count == ns->regs.num) {
20422043 NS_ERR("write_byte: no more address bytes expected\n");
2043
- switch_to_ready_state(ns, NS_STATUS_FAILED(ns));
2044
+ ns_switch_to_ready_state(ns, NS_STATUS_FAILED(ns));
20442045 return;
20452046 }
20462047
2047
- accept_addr_byte(ns, byte);
2048
+ ns_accept_addr_byte(ns, byte);
20482049
20492050 ns->regs.count += 1;
20502051
....@@ -2053,7 +2054,7 @@
20532054
20542055 if (ns->regs.count == ns->regs.num) {
20552056 NS_DBG("address (%#x, %#x) is accepted\n", ns->regs.row, ns->regs.column);
2056
- switch_state(ns);
2057
+ ns_switch_state(ns);
20572058 }
20582059
20592060 } else {
....@@ -2063,10 +2064,10 @@
20632064
20642065 /* Check that chip is expecting data input */
20652066 if (!(ns->state & STATE_DATAIN_MASK)) {
2066
- NS_ERR("write_byte: data input (%#x) isn't expected, state is %s, "
2067
- "switch to %s\n", (uint)byte,
2068
- get_state_name(ns->state), get_state_name(STATE_READY));
2069
- switch_to_ready_state(ns, NS_STATUS_FAILED(ns));
2067
+ NS_ERR("write_byte: data input (%#x) isn't expected, state is %s, switch to %s\n",
2068
+ (uint)byte, ns_get_state_name(ns->state),
2069
+ ns_get_state_name(STATE_READY));
2070
+ ns_switch_to_ready_state(ns, NS_STATUS_FAILED(ns));
20702071 return;
20712072 }
20722073
....@@ -2089,51 +2090,23 @@
20892090 return;
20902091 }
20912092
2092
-static void ns_hwcontrol(struct mtd_info *mtd, int cmd, unsigned int bitmask)
2093
+static void ns_nand_write_buf(struct nand_chip *chip, const u_char *buf,
2094
+ int len)
20932095 {
2094
- struct nand_chip *chip = mtd_to_nand(mtd);
2095
- struct nandsim *ns = nand_get_controller_data(chip);
2096
-
2097
- ns->lines.cle = bitmask & NAND_CLE ? 1 : 0;
2098
- ns->lines.ale = bitmask & NAND_ALE ? 1 : 0;
2099
- ns->lines.ce = bitmask & NAND_NCE ? 1 : 0;
2100
-
2101
- if (cmd != NAND_CMD_NONE)
2102
- ns_nand_write_byte(mtd, cmd);
2103
-}
2104
-
2105
-static int ns_device_ready(struct mtd_info *mtd)
2106
-{
2107
- NS_DBG("device_ready\n");
2108
- return 1;
2109
-}
2110
-
2111
-static uint16_t ns_nand_read_word(struct mtd_info *mtd)
2112
-{
2113
- struct nand_chip *chip = mtd_to_nand(mtd);
2114
-
2115
- NS_DBG("read_word\n");
2116
-
2117
- return chip->read_byte(mtd) | (chip->read_byte(mtd) << 8);
2118
-}
2119
-
2120
-static void ns_nand_write_buf(struct mtd_info *mtd, const u_char *buf, int len)
2121
-{
2122
- struct nand_chip *chip = mtd_to_nand(mtd);
21232096 struct nandsim *ns = nand_get_controller_data(chip);
21242097
21252098 /* Check that chip is expecting data input */
21262099 if (!(ns->state & STATE_DATAIN_MASK)) {
2127
- NS_ERR("write_buf: data input isn't expected, state is %s, "
2128
- "switch to STATE_READY\n", get_state_name(ns->state));
2129
- switch_to_ready_state(ns, NS_STATUS_FAILED(ns));
2100
+ NS_ERR("write_buf: data input isn't expected, state is %s, switch to STATE_READY\n",
2101
+ ns_get_state_name(ns->state));
2102
+ ns_switch_to_ready_state(ns, NS_STATUS_FAILED(ns));
21302103 return;
21312104 }
21322105
21332106 /* Check if these are expected bytes */
21342107 if (ns->regs.count + len > ns->regs.num) {
21352108 NS_ERR("write_buf: too many input bytes\n");
2136
- switch_to_ready_state(ns, NS_STATUS_FAILED(ns));
2109
+ ns_switch_to_ready_state(ns, NS_STATUS_FAILED(ns));
21372110 return;
21382111 }
21392112
....@@ -2145,9 +2118,8 @@
21452118 }
21462119 }
21472120
2148
-static void ns_nand_read_buf(struct mtd_info *mtd, u_char *buf, int len)
2121
+static void ns_nand_read_buf(struct nand_chip *chip, u_char *buf, int len)
21492122 {
2150
- struct nand_chip *chip = mtd_to_nand(mtd);
21512123 struct nandsim *ns = nand_get_controller_data(chip);
21522124
21532125 /* Sanity and correctness checks */
....@@ -2161,7 +2133,7 @@
21612133 }
21622134 if (!(ns->state & STATE_DATAOUT_MASK)) {
21632135 NS_WARN("read_buf: unexpected data output cycle, current state is %s\n",
2164
- get_state_name(ns->state));
2136
+ ns_get_state_name(ns->state));
21652137 return;
21662138 }
21672139
....@@ -2169,7 +2141,7 @@
21692141 int i;
21702142
21712143 for (i = 0; i < len; i++)
2172
- buf[i] = mtd_to_nand(mtd)->read_byte(mtd);
2144
+ buf[i] = ns_nand_read_byte(chip);
21732145
21742146 return;
21752147 }
....@@ -2177,7 +2149,7 @@
21772149 /* Check if these are expected bytes */
21782150 if (ns->regs.count + len > ns->regs.num) {
21792151 NS_ERR("read_buf: too many bytes to read\n");
2180
- switch_to_ready_state(ns, NS_STATUS_FAILED(ns));
2152
+ ns_switch_to_ready_state(ns, NS_STATUS_FAILED(ns));
21812153 return;
21822154 }
21832155
....@@ -2186,15 +2158,61 @@
21862158
21872159 if (ns->regs.count == ns->regs.num) {
21882160 if (NS_STATE(ns->nxstate) == STATE_READY)
2189
- switch_state(ns);
2161
+ ns_switch_state(ns);
21902162 }
21912163
21922164 return;
21932165 }
21942166
2167
+static int ns_exec_op(struct nand_chip *chip, const struct nand_operation *op,
2168
+ bool check_only)
2169
+{
2170
+ int i;
2171
+ unsigned int op_id;
2172
+ const struct nand_op_instr *instr = NULL;
2173
+ struct nandsim *ns = nand_get_controller_data(chip);
2174
+
2175
+ if (check_only)
2176
+ return 0;
2177
+
2178
+ ns->lines.ce = 1;
2179
+
2180
+ for (op_id = 0; op_id < op->ninstrs; op_id++) {
2181
+ instr = &op->instrs[op_id];
2182
+ ns->lines.cle = 0;
2183
+ ns->lines.ale = 0;
2184
+
2185
+ switch (instr->type) {
2186
+ case NAND_OP_CMD_INSTR:
2187
+ ns->lines.cle = 1;
2188
+ ns_nand_write_byte(chip, instr->ctx.cmd.opcode);
2189
+ break;
2190
+ case NAND_OP_ADDR_INSTR:
2191
+ ns->lines.ale = 1;
2192
+ for (i = 0; i < instr->ctx.addr.naddrs; i++)
2193
+ ns_nand_write_byte(chip, instr->ctx.addr.addrs[i]);
2194
+ break;
2195
+ case NAND_OP_DATA_IN_INSTR:
2196
+ ns_nand_read_buf(chip, instr->ctx.data.buf.in, instr->ctx.data.len);
2197
+ break;
2198
+ case NAND_OP_DATA_OUT_INSTR:
2199
+ ns_nand_write_buf(chip, instr->ctx.data.buf.out, instr->ctx.data.len);
2200
+ break;
2201
+ case NAND_OP_WAITRDY_INSTR:
2202
+ /* we are always ready */
2203
+ break;
2204
+ }
2205
+ }
2206
+
2207
+ return 0;
2208
+}
2209
+
21952210 static int ns_attach_chip(struct nand_chip *chip)
21962211 {
21972212 unsigned int eccsteps, eccbytes;
2213
+
2214
+ chip->ecc.engine_type = NAND_ECC_ENGINE_TYPE_SOFT;
2215
+ chip->ecc.algo = bch ? NAND_ECC_ALGO_BCH : NAND_ECC_ALGO_HAMMING;
21982216
21992217 if (!bch)
22002218 return 0;
....@@ -2219,8 +2237,6 @@
22192237 return -EINVAL;
22202238 }
22212239
2222
- chip->ecc.mode = NAND_ECC_SOFT;
2223
- chip->ecc.algo = NAND_ECC_BCH;
22242240 chip->ecc.size = 512;
22252241 chip->ecc.strength = bch;
22262242 chip->ecc.bytes = eccbytes;
....@@ -2232,6 +2248,7 @@
22322248
22332249 static const struct nand_controller_ops ns_controller_ops = {
22342250 .attach_chip = ns_attach_chip,
2251
+ .exec_op = ns_exec_op,
22352252 };
22362253
22372254 /*
....@@ -2239,139 +2256,163 @@
22392256 */
22402257 static int __init ns_init_module(void)
22412258 {
2259
+ struct list_head *pos, *n;
22422260 struct nand_chip *chip;
2243
- struct nandsim *nand;
2244
- int retval = -ENOMEM, i;
2261
+ struct nandsim *ns;
2262
+ int ret;
22452263
22462264 if (bus_width != 8 && bus_width != 16) {
22472265 NS_ERR("wrong bus width (%d), use only 8 or 16\n", bus_width);
22482266 return -EINVAL;
22492267 }
22502268
2251
- /* Allocate and initialize mtd_info, nand_chip and nandsim structures */
2252
- chip = kzalloc(sizeof(struct nand_chip) + sizeof(struct nandsim),
2253
- GFP_KERNEL);
2254
- if (!chip) {
2269
+ ns = kzalloc(sizeof(struct nandsim), GFP_KERNEL);
2270
+ if (!ns) {
22552271 NS_ERR("unable to allocate core structures.\n");
22562272 return -ENOMEM;
22572273 }
2274
+ chip = &ns->chip;
22582275 nsmtd = nand_to_mtd(chip);
2259
- nand = (struct nandsim *)(chip + 1);
2260
- nand_set_controller_data(chip, (void *)nand);
2276
+ nand_set_controller_data(chip, (void *)ns);
22612277
2262
- /*
2263
- * Register simulator's callbacks.
2264
- */
2265
- chip->cmd_ctrl = ns_hwcontrol;
2266
- chip->read_byte = ns_nand_read_byte;
2267
- chip->dev_ready = ns_device_ready;
2268
- chip->write_buf = ns_nand_write_buf;
2269
- chip->read_buf = ns_nand_read_buf;
2270
- chip->read_word = ns_nand_read_word;
2271
- chip->ecc.mode = NAND_ECC_SOFT;
2272
- chip->ecc.algo = NAND_ECC_HAMMING;
22732278 /* The NAND_SKIP_BBTSCAN option is necessary for 'overridesize' */
22742279 /* and 'badblocks' parameters to work */
22752280 chip->options |= NAND_SKIP_BBTSCAN;
22762281
22772282 switch (bbt) {
22782283 case 2:
2279
- chip->bbt_options |= NAND_BBT_NO_OOB;
2284
+ chip->bbt_options |= NAND_BBT_NO_OOB;
2285
+ fallthrough;
22802286 case 1:
2281
- chip->bbt_options |= NAND_BBT_USE_FLASH;
2287
+ chip->bbt_options |= NAND_BBT_USE_FLASH;
2288
+ fallthrough;
22822289 case 0:
22832290 break;
22842291 default:
22852292 NS_ERR("bbt has to be 0..2\n");
2286
- retval = -EINVAL;
2287
- goto error;
2293
+ ret = -EINVAL;
2294
+ goto free_ns_struct;
22882295 }
22892296 /*
22902297 * Perform minimum nandsim structure initialization to handle
22912298 * the initial ID read command correctly
22922299 */
22932300 if (id_bytes[6] != 0xFF || id_bytes[7] != 0xFF)
2294
- nand->geom.idbytes = 8;
2301
+ ns->geom.idbytes = 8;
22952302 else if (id_bytes[4] != 0xFF || id_bytes[5] != 0xFF)
2296
- nand->geom.idbytes = 6;
2303
+ ns->geom.idbytes = 6;
22972304 else if (id_bytes[2] != 0xFF || id_bytes[3] != 0xFF)
2298
- nand->geom.idbytes = 4;
2305
+ ns->geom.idbytes = 4;
22992306 else
2300
- nand->geom.idbytes = 2;
2301
- nand->regs.status = NS_STATUS_OK(nand);
2302
- nand->nxstate = STATE_UNKNOWN;
2303
- nand->options |= OPT_PAGE512; /* temporary value */
2304
- memcpy(nand->ids, id_bytes, sizeof(nand->ids));
2307
+ ns->geom.idbytes = 2;
2308
+ ns->regs.status = NS_STATUS_OK(ns);
2309
+ ns->nxstate = STATE_UNKNOWN;
2310
+ ns->options |= OPT_PAGE512; /* temporary value */
2311
+ memcpy(ns->ids, id_bytes, sizeof(ns->ids));
23052312 if (bus_width == 16) {
2306
- nand->busw = 16;
2313
+ ns->busw = 16;
23072314 chip->options |= NAND_BUSWIDTH_16;
23082315 }
23092316
23102317 nsmtd->owner = THIS_MODULE;
23112318
2312
- if ((retval = parse_weakblocks()) != 0)
2313
- goto error;
2319
+ ret = ns_parse_weakblocks();
2320
+ if (ret)
2321
+ goto free_ns_struct;
23142322
2315
- if ((retval = parse_weakpages()) != 0)
2316
- goto error;
2323
+ ret = ns_parse_weakpages();
2324
+ if (ret)
2325
+ goto free_wb_list;
23172326
2318
- if ((retval = parse_gravepages()) != 0)
2319
- goto error;
2327
+ ret = ns_parse_gravepages();
2328
+ if (ret)
2329
+ goto free_wp_list;
23202330
2321
- chip->dummy_controller.ops = &ns_controller_ops;
2322
- retval = nand_scan(chip, 1);
2323
- if (retval) {
2331
+ nand_controller_init(&ns->base);
2332
+ ns->base.ops = &ns_controller_ops;
2333
+ chip->controller = &ns->base;
2334
+
2335
+ ret = nand_scan(chip, 1);
2336
+ if (ret) {
23242337 NS_ERR("Could not scan NAND Simulator device\n");
2325
- goto error;
2338
+ goto free_gp_list;
23262339 }
23272340
23282341 if (overridesize) {
23292342 uint64_t new_size = (uint64_t)nsmtd->erasesize << overridesize;
2343
+ struct nand_memory_organization *memorg;
2344
+ u64 targetsize;
2345
+
2346
+ memorg = nanddev_get_memorg(&chip->base);
2347
+
23302348 if (new_size >> overridesize != nsmtd->erasesize) {
23312349 NS_ERR("overridesize is too big\n");
2332
- retval = -EINVAL;
2333
- goto err_exit;
2350
+ ret = -EINVAL;
2351
+ goto cleanup_nand;
23342352 }
2353
+
23352354 /* N.B. This relies on nand_scan not doing anything with the size before we change it */
23362355 nsmtd->size = new_size;
2337
- chip->chipsize = new_size;
2356
+ memorg->eraseblocks_per_lun = 1 << overridesize;
2357
+ targetsize = nanddev_target_size(&chip->base);
23382358 chip->chip_shift = ffs(nsmtd->erasesize) + overridesize - 1;
2339
- chip->pagemask = (chip->chipsize >> chip->page_shift) - 1;
2359
+ chip->pagemask = (targetsize >> chip->page_shift) - 1;
23402360 }
23412361
2342
- if ((retval = setup_wear_reporting(nsmtd)) != 0)
2343
- goto err_exit;
2362
+ ret = ns_setup_wear_reporting(nsmtd);
2363
+ if (ret)
2364
+ goto cleanup_nand;
23442365
2345
- if ((retval = init_nandsim(nsmtd)) != 0)
2346
- goto err_exit;
2366
+ ret = ns_init(nsmtd);
2367
+ if (ret)
2368
+ goto free_ebw;
23472369
2348
- if ((retval = nand_create_bbt(chip)) != 0)
2349
- goto err_exit;
2370
+ ret = nand_create_bbt(chip);
2371
+ if (ret)
2372
+ goto free_ns_object;
23502373
2351
- if ((retval = parse_badblocks(nand, nsmtd)) != 0)
2352
- goto err_exit;
2374
+ ret = ns_parse_badblocks(ns, nsmtd);
2375
+ if (ret)
2376
+ goto free_ns_object;
23532377
23542378 /* Register NAND partitions */
2355
- retval = mtd_device_register(nsmtd, &nand->partitions[0],
2356
- nand->nbparts);
2357
- if (retval != 0)
2358
- goto err_exit;
2379
+ ret = mtd_device_register(nsmtd, &ns->partitions[0], ns->nbparts);
2380
+ if (ret)
2381
+ goto free_ns_object;
23592382
2360
- if ((retval = nandsim_debugfs_create(nand)) != 0)
2361
- goto err_exit;
2383
+ ret = ns_debugfs_create(ns);
2384
+ if (ret)
2385
+ goto unregister_mtd;
23622386
23632387 return 0;
23642388
2365
-err_exit:
2366
- free_nandsim(nand);
2367
- nand_release(chip);
2368
- for (i = 0;i < ARRAY_SIZE(nand->partitions); ++i)
2369
- kfree(nand->partitions[i].name);
2370
-error:
2371
- kfree(chip);
2372
- free_lists();
2389
+unregister_mtd:
2390
+ WARN_ON(mtd_device_unregister(nsmtd));
2391
+free_ns_object:
2392
+ ns_free(ns);
2393
+free_ebw:
2394
+ kfree(erase_block_wear);
2395
+cleanup_nand:
2396
+ nand_cleanup(chip);
2397
+free_gp_list:
2398
+ list_for_each_safe(pos, n, &grave_pages) {
2399
+ list_del(pos);
2400
+ kfree(list_entry(pos, struct grave_page, list));
2401
+ }
2402
+free_wp_list:
2403
+ list_for_each_safe(pos, n, &weak_pages) {
2404
+ list_del(pos);
2405
+ kfree(list_entry(pos, struct weak_page, list));
2406
+ }
2407
+free_wb_list:
2408
+ list_for_each_safe(pos, n, &weak_blocks) {
2409
+ list_del(pos);
2410
+ kfree(list_entry(pos, struct weak_block, list));
2411
+ }
2412
+free_ns_struct:
2413
+ kfree(ns);
23732414
2374
- return retval;
2415
+ return ret;
23752416 }
23762417
23772418 module_init(ns_init_module);
....@@ -2383,18 +2424,35 @@
23832424 {
23842425 struct nand_chip *chip = mtd_to_nand(nsmtd);
23852426 struct nandsim *ns = nand_get_controller_data(chip);
2386
- int i;
2427
+ struct list_head *pos, *n;
23872428
2388
- free_nandsim(ns); /* Free nandsim private resources */
2389
- nand_release(chip); /* Unregister driver */
2390
- for (i = 0;i < ARRAY_SIZE(ns->partitions); ++i)
2391
- kfree(ns->partitions[i].name);
2392
- kfree(mtd_to_nand(nsmtd)); /* Free other structures */
2393
- free_lists();
2429
+ ns_debugfs_remove(ns);
2430
+ WARN_ON(mtd_device_unregister(nsmtd));
2431
+ ns_free(ns);
2432
+ kfree(erase_block_wear);
2433
+ nand_cleanup(chip);
2434
+
2435
+ list_for_each_safe(pos, n, &grave_pages) {
2436
+ list_del(pos);
2437
+ kfree(list_entry(pos, struct grave_page, list));
2438
+ }
2439
+
2440
+ list_for_each_safe(pos, n, &weak_pages) {
2441
+ list_del(pos);
2442
+ kfree(list_entry(pos, struct weak_page, list));
2443
+ }
2444
+
2445
+ list_for_each_safe(pos, n, &weak_blocks) {
2446
+ list_del(pos);
2447
+ kfree(list_entry(pos, struct weak_block, list));
2448
+ }
2449
+
2450
+ kfree(ns);
23942451 }
23952452
23962453 module_exit(ns_cleanup_module);
23972454
23982455 MODULE_LICENSE ("GPL");
2456
+MODULE_IMPORT_NS(VFS_internal_I_am_really_a_filesystem_and_am_NOT_a_driver);
23992457 MODULE_AUTHOR ("Artem B. Bityuckiy");
24002458 MODULE_DESCRIPTION ("The NAND flash simulator");