Back to home page

OSCL-LXR

 
 

    


0001 // SPDX-License-Identifier: GPL-2.0-only
0002 /****************************************************************************
0003  * Driver for Solarflare network controllers and boards
0004  * Copyright 2005-2006 Fen Systems Ltd.
0005  * Copyright 2006-2013 Solarflare Communications Inc.
0006  */
0007 
0008 #include <linux/module.h>
0009 #include <linux/mtd/mtd.h>
0010 #include <linux/slab.h>
0011 #include <linux/rtnetlink.h>
0012 
0013 #include "net_driver.h"
0014 #include "efx.h"
0015 
0016 #define to_efx_mtd_partition(mtd)               \
0017     container_of(mtd, struct efx_mtd_partition, mtd)
0018 
0019 /* MTD interface */
0020 
0021 static int efx_mtd_erase(struct mtd_info *mtd, struct erase_info *erase)
0022 {
0023     struct efx_nic *efx = mtd->priv;
0024 
0025     return efx->type->mtd_erase(mtd, erase->addr, erase->len);
0026 }
0027 
0028 static void efx_mtd_sync(struct mtd_info *mtd)
0029 {
0030     struct efx_mtd_partition *part = to_efx_mtd_partition(mtd);
0031     struct efx_nic *efx = mtd->priv;
0032     int rc;
0033 
0034     rc = efx->type->mtd_sync(mtd);
0035     if (rc)
0036         pr_err("%s: %s sync failed (%d)\n",
0037                part->name, part->dev_type_name, rc);
0038 }
0039 
0040 static void efx_mtd_remove_partition(struct efx_mtd_partition *part)
0041 {
0042     int rc;
0043 
0044     for (;;) {
0045         rc = mtd_device_unregister(&part->mtd);
0046         if (rc != -EBUSY)
0047             break;
0048         ssleep(1);
0049     }
0050     WARN_ON(rc);
0051     list_del(&part->node);
0052 }
0053 
0054 int efx_mtd_add(struct efx_nic *efx, struct efx_mtd_partition *parts,
0055         size_t n_parts, size_t sizeof_part)
0056 {
0057     struct efx_mtd_partition *part;
0058     size_t i;
0059 
0060     for (i = 0; i < n_parts; i++) {
0061         part = (struct efx_mtd_partition *)((char *)parts +
0062                             i * sizeof_part);
0063 
0064         part->mtd.writesize = 1;
0065 
0066         if (!(part->mtd.flags & MTD_NO_ERASE))
0067             part->mtd.flags |= MTD_WRITEABLE;
0068 
0069         part->mtd.owner = THIS_MODULE;
0070         part->mtd.priv = efx;
0071         part->mtd.name = part->name;
0072         part->mtd._erase = efx_mtd_erase;
0073         part->mtd._read = efx->type->mtd_read;
0074         part->mtd._write = efx->type->mtd_write;
0075         part->mtd._sync = efx_mtd_sync;
0076 
0077         efx->type->mtd_rename(part);
0078 
0079         if (mtd_device_register(&part->mtd, NULL, 0))
0080             goto fail;
0081 
0082         /* Add to list in order - efx_mtd_remove() depends on this */
0083         list_add_tail(&part->node, &efx->mtd_list);
0084     }
0085 
0086     return 0;
0087 
0088 fail:
0089     while (i--) {
0090         part = (struct efx_mtd_partition *)((char *)parts +
0091                             i * sizeof_part);
0092         efx_mtd_remove_partition(part);
0093     }
0094     /* Failure is unlikely here, but probably means we're out of memory */
0095     return -ENOMEM;
0096 }
0097 
0098 void efx_mtd_remove(struct efx_nic *efx)
0099 {
0100     struct efx_mtd_partition *parts, *part, *next;
0101 
0102     WARN_ON(efx_dev_registered(efx));
0103 
0104     if (list_empty(&efx->mtd_list))
0105         return;
0106 
0107     parts = list_first_entry(&efx->mtd_list, struct efx_mtd_partition,
0108                  node);
0109 
0110     list_for_each_entry_safe(part, next, &efx->mtd_list, node)
0111         efx_mtd_remove_partition(part);
0112 
0113     kfree(parts);
0114 }
0115 
0116 void efx_mtd_rename(struct efx_nic *efx)
0117 {
0118     struct efx_mtd_partition *part;
0119 
0120     ASSERT_RTNL();
0121 
0122     list_for_each_entry(part, &efx->mtd_list, node)
0123         efx->type->mtd_rename(part);
0124 }