[v12,2/5] gennvm: Generic NVM manager
diff mbox

Message ID 1445957719-5368-3-git-send-email-m@bjorling.me
State New
Headers show

Commit Message

Matias Bjorling Oct. 27, 2015, 2:55 p.m. UTC
The implementation for Open-Channel SSDs is divided into media
management and targets. This patch implements a generic media manager
for open-channel SSDs. After a media manager has been initialized,
single or multiple targets can be instantiated with the media managed as
the backend.

Signed-off-by: Matias Bjørling <m@bjorling.me>
---
 drivers/lightnvm/Kconfig  |   7 +
 drivers/lightnvm/Makefile |   1 +
 drivers/lightnvm/gennvm.c | 475 ++++++++++++++++++++++++++++++++++++++++++++++
 drivers/lightnvm/gennvm.h |  46 +++++
 4 files changed, 529 insertions(+)
 create mode 100644 drivers/lightnvm/gennvm.c
 create mode 100644 drivers/lightnvm/gennvm.h

Comments

kernel test robot Oct. 27, 2015, 8:43 p.m. UTC | #1
Hi Matias,

[auto build test ERROR on next-20151022 -- if it's inappropriate base, please suggest rules for selecting the more suitable base]

url:    https://github.com/0day-ci/linux/commits/Matias-Bj-rling/Support-for-Open-Channel-SSDs/20151027-230440
config: microblaze-allmodconfig (attached as .config)
reproduce:
        wget https://git.kernel.org/cgit/linux/kernel/git/wfg/lkp-tests.git/plain/sbin/make.cross -O ~/bin/make.cross
        chmod +x ~/bin/make.cross
        # save the attached .config to linux build tree
        make.cross ARCH=microblaze 

All errors (new ones prefixed by >>):

   ERROR: "isa_io_base" [drivers/staging/comedi/drivers/dt2814.ko] undefined!
   ERROR: "isa_io_base" [drivers/staging/comedi/drivers/dt2811.ko] undefined!
   ERROR: "isa_io_base" [drivers/staging/comedi/drivers/dt2801.ko] undefined!
   ERROR: "isa_io_base" [drivers/staging/comedi/drivers/dmm32at.ko] undefined!
   ERROR: "isa_io_base" [drivers/staging/comedi/drivers/das800.ko] undefined!
   ERROR: "isa_io_base" [drivers/staging/comedi/drivers/das6402.ko] undefined!
   ERROR: "isa_io_base" [drivers/staging/comedi/drivers/das1800.ko] undefined!
   ERROR: "isa_io_base" [drivers/staging/comedi/drivers/das16m1.ko] undefined!
   ERROR: "isa_io_base" [drivers/staging/comedi/drivers/das16.ko] undefined!
   ERROR: "isa_io_base" [drivers/staging/comedi/drivers/das08.ko] undefined!
   ERROR: "isa_io_base" [drivers/staging/comedi/drivers/dac02.ko] undefined!
   ERROR: "isa_io_base" [drivers/staging/comedi/drivers/contec_pci_dio.ko] undefined!
   ERROR: "isa_io_base" [drivers/staging/comedi/drivers/comedi_parport.ko] undefined!
   ERROR: "isa_io_base" [drivers/staging/comedi/drivers/comedi_8255.ko] undefined!
   ERROR: "isa_io_base" [drivers/staging/comedi/drivers/comedi_8254.ko] undefined!
   ERROR: "isa_io_base" [drivers/staging/comedi/drivers/cb_pcimdda.ko] undefined!
   ERROR: "isa_io_base" [drivers/staging/comedi/drivers/cb_pcimdas.ko] undefined!
   ERROR: "isa_io_base" [drivers/staging/comedi/drivers/cb_pcidda.ko] undefined!
   ERROR: "isa_io_base" [drivers/staging/comedi/drivers/cb_pcidas.ko] undefined!
   ERROR: "isa_io_base" [drivers/staging/comedi/drivers/c6xdigio.ko] undefined!
   ERROR: "isa_io_base" [drivers/staging/comedi/drivers/amplc_pci263.ko] undefined!
   ERROR: "isa_io_base" [drivers/staging/comedi/drivers/amplc_pci236.ko] undefined!
   ERROR: "isa_io_base" [drivers/staging/comedi/drivers/amplc_pci230.ko] undefined!
   ERROR: "isa_io_base" [drivers/staging/comedi/drivers/amplc_pci224.ko] undefined!
   ERROR: "isa_io_base" [drivers/staging/comedi/drivers/amplc_pc263.ko] undefined!
   ERROR: "isa_io_base" [drivers/staging/comedi/drivers/amplc_dio200_common.ko] undefined!
   ERROR: "isa_io_base" [drivers/staging/comedi/drivers/aio_iiro_16.ko] undefined!
   ERROR: "isa_io_base" [drivers/staging/comedi/drivers/aio_aio12_8.ko] undefined!
   ERROR: "isa_io_base" [drivers/staging/comedi/drivers/adv_pci_dio.ko] undefined!
   ERROR: "isa_io_base" [drivers/staging/comedi/drivers/adv_pci1724.ko] undefined!
   ERROR: "isa_io_base" [drivers/staging/comedi/drivers/adv_pci1723.ko] undefined!
   ERROR: "isa_io_base" [drivers/staging/comedi/drivers/adv_pci1710.ko] undefined!
   ERROR: "isa_io_base" [drivers/staging/comedi/drivers/adq12b.ko] undefined!
   ERROR: "isa_io_base" [drivers/staging/comedi/drivers/adl_pci9118.ko] undefined!
   ERROR: "isa_io_base" [drivers/staging/comedi/drivers/adl_pci9111.ko] undefined!
   ERROR: "isa_io_base" [drivers/staging/comedi/drivers/adl_pci8164.ko] undefined!
   ERROR: "isa_io_base" [drivers/staging/comedi/drivers/adl_pci7x3x.ko] undefined!
   ERROR: "isa_io_base" [drivers/staging/comedi/drivers/adl_pci6208.ko] undefined!
   ERROR: "isa_io_base" [drivers/staging/comedi/drivers/addi_watchdog.ko] undefined!
   ERROR: "isa_io_base" [drivers/staging/comedi/drivers/addi_apci_3xxx.ko] undefined!
   ERROR: "isa_io_base" [drivers/staging/comedi/drivers/addi_apci_3501.ko] undefined!
   ERROR: "isa_io_base" [drivers/staging/comedi/drivers/addi_apci_3120.ko] undefined!
   ERROR: "isa_io_base" [drivers/staging/comedi/drivers/addi_apci_2200.ko] undefined!
   ERROR: "isa_io_base" [drivers/staging/comedi/drivers/addi_apci_2032.ko] undefined!
   ERROR: "isa_io_base" [drivers/staging/comedi/drivers/addi_apci_16xx.ko] undefined!
   ERROR: "isa_io_base" [drivers/staging/comedi/drivers/addi_apci_1564.ko] undefined!
   ERROR: "isa_io_base" [drivers/staging/comedi/drivers/addi_apci_1516.ko] undefined!
   ERROR: "isa_io_base" [drivers/staging/comedi/drivers/addi_apci_1500.ko] undefined!
   ERROR: "isa_io_base" [drivers/staging/comedi/drivers/addi_apci_1032.ko] undefined!
   ERROR: "isa_io_base" [drivers/scsi/qla2xxx/qla2xxx.ko] undefined!
   ERROR: "isa_io_base" [drivers/scsi/ppa.ko] undefined!
   ERROR: "isa_io_base" [drivers/scsi/nsp32.ko] undefined!
   ERROR: "isa_io_base" [drivers/scsi/mvsas/mvsas.ko] undefined!
   ERROR: "isa_io_base" [drivers/scsi/megaraid.ko] undefined!
   ERROR: "isa_io_base" [drivers/scsi/ips.ko] undefined!
   ERROR: "isa_io_base" [drivers/scsi/initio.ko] undefined!
   ERROR: "isa_io_base" [drivers/scsi/imm.ko] undefined!
   ERROR: "isa_io_base" [drivers/scsi/fdomain.ko] undefined!
   ERROR: "isa_io_base" [drivers/scsi/dmx3191d.ko] undefined!
   ERROR: "isa_io_base" [drivers/scsi/dc395x.ko] undefined!
   ERROR: "isa_io_base" [drivers/scsi/atp870u.ko] undefined!
   ERROR: "isa_io_base" [drivers/scsi/aic94xx/aic94xx.ko] undefined!
   ERROR: "isa_io_base" [drivers/scsi/aic7xxx/aic7xxx.ko] undefined!
   ERROR: "isa_io_base" [drivers/scsi/aic7xxx/aic79xx.ko] undefined!
   ERROR: "isa_io_base" [drivers/scsi/advansys.ko] undefined!
   ERROR: "isa_io_base" [drivers/scsi/a100u2w.ko] undefined!
   ERROR: "isa_io_base" [drivers/scsi/3w-xxxx.ko] undefined!
   ERROR: "isa_io_base" [drivers/rtc/rtc-bq4802.ko] undefined!
   ERROR: "isa_io_base" [drivers/platform/chrome/cros_ec_lpc.ko] undefined!
   ERROR: "isa_io_base" [drivers/parport/parport_pc.ko] undefined!
   ERROR: "isa_io_base" [drivers/net/wireless/hostap/hostap_plx.ko] undefined!
   ERROR: "isa_io_base" [drivers/net/wireless/atmel.ko] undefined!
   ERROR: "isa_io_base" [drivers/net/wan/lmc/lmc.ko] undefined!
   ERROR: "isa_io_base" [drivers/net/wan/farsync.ko] undefined!
   ERROR: "isa_io_base" [drivers/net/irda/vlsi_ir.ko] undefined!
   ERROR: "isa_io_base" [drivers/net/irda/donauboe.ko] undefined!
   ERROR: "isa_io_base" [drivers/net/hamradio/yam.ko] undefined!
   ERROR: "isa_io_base" [drivers/net/hamradio/baycom_ser_hdx.ko] undefined!
   ERROR: "isa_io_base" [drivers/net/hamradio/baycom_ser_fdx.ko] undefined!
   ERROR: "isa_io_base" [drivers/net/ethernet/via/via-rhine.ko] undefined!
   ERROR: "isa_io_base" [drivers/net/ethernet/ti/tlan.ko] undefined!
   ERROR: "isa_io_base" [drivers/net/ethernet/sis/sis900.ko] undefined!
   ERROR: "isa_io_base" [drivers/net/ethernet/sis/sis190.ko] undefined!
   ERROR: "isa_io_base" [drivers/net/ethernet/intel/e1000/e1000.ko] undefined!
   ERROR: "isa_io_base" [drivers/net/ethernet/hp/hp100.ko] undefined!
   ERROR: "isa_io_base" [drivers/net/ethernet/dec/tulip/de4x5.ko] undefined!
   ERROR: "isa_io_base" [drivers/net/ethernet/amd/pcnet32.ko] undefined!
   ERROR: "isa_io_base" [drivers/net/ethernet/8390/ne2k-pci.ko] undefined!
   ERROR: "isa_io_base" [drivers/net/ethernet/8390/8390.ko] undefined!
   ERROR: "isa_io_base" [drivers/net/ethernet/3com/3c59x.ko] undefined!
   ERROR: "isa_io_base" [drivers/net/can/sja1000/sja1000_isa.ko] undefined!
   ERROR: "isa_io_base" [drivers/net/can/cc770/cc770_isa.ko] undefined!
   ERROR: "isa_io_base" [drivers/net/arcnet/com90xx.ko] undefined!
   ERROR: "isa_io_base" [drivers/net/arcnet/com90io.ko] undefined!
   ERROR: "isa_io_base" [drivers/net/arcnet/com20020.ko] undefined!
   ERROR: "isa_io_base" [drivers/net/arcnet/com20020-pci.ko] undefined!
   ERROR: "isa_io_base" [drivers/misc/altera-stapl/altera-stapl.ko] undefined!
   ERROR: "isa_io_base" [drivers/message/fusion/mptbase.ko] undefined!
   ERROR: "isa_io_base" [drivers/media/radio/radio-maxiradio.ko] undefined!
   ERROR: "isa_io_base" [drivers/media/pci/dm1105/dm1105.ko] undefined!
>> ERROR: "__udivdi3" [drivers/lightnvm/gennvm.ko] undefined!
>> ERROR: "__umoddi3" [drivers/lightnvm/gennvm.ko] undefined!
   ERROR: "isa_io_base" [drivers/leds/leds-ot200.ko] undefined!
   ERROR: "isa_io_base" [drivers/isdn/hysdn/hysdn.ko] undefined!
   ERROR: "isa_io_base" [drivers/isdn/hisax/hisax_fcpcipnp.ko] undefined!
   ERROR: "isa_io_base" [drivers/isdn/hisax/hisax.ko] undefined!
   ERROR: "isa_io_base" [drivers/isdn/hisax/hfc4s8s_l1.ko] undefined!
   ERROR: "isa_io_base" [drivers/isdn/hardware/mISDN/w6692.ko] undefined!
   ERROR: "isa_io_base" [drivers/isdn/hardware/mISDN/speedfax.ko] undefined!
   ERROR: "isa_io_base" [drivers/isdn/hardware/mISDN/netjet.ko] undefined!
   ERROR: "isa_io_base" [drivers/isdn/hardware/mISDN/mISDNinfineon.ko] undefined!
   ERROR: "isa_io_base" [drivers/isdn/hardware/mISDN/hfcmulti.ko] undefined!
   ERROR: "isa_io_base" [drivers/isdn/hardware/mISDN/avmfritz.ko] undefined!
   ERROR: "isa_io_base" [drivers/isdn/hardware/eicon/divas.ko] undefined!
   ERROR: "isa_io_base" [drivers/isdn/hardware/avm/b1pci.ko] undefined!
   ERROR: "isa_io_base" [drivers/isdn/hardware/avm/b1dma.ko] undefined!
   ERROR: "isa_io_base" [drivers/isdn/hardware/avm/b1.ko] undefined!
   ERROR: "isa_io_base" [drivers/input/touchscreen/mk712.ko] undefined!
   ERROR: "isa_io_base" [drivers/input/serio/pcips2.ko] undefined!
   ERROR: "isa_io_base" [drivers/input/joystick/tmdc.ko] undefined!
   ERROR: "isa_io_base" [drivers/input/joystick/sidewinder.ko] undefined!
   ERROR: "isa_io_base" [drivers/input/joystick/joydump.ko] undefined!
   ERROR: "isa_io_base" [drivers/input/joystick/interact.ko] undefined!
   ERROR: "isa_io_base" [drivers/input/joystick/guillemot.ko] undefined!
   ERROR: "isa_io_base" [drivers/input/joystick/grip_mp.ko] undefined!
   ERROR: "isa_io_base" [drivers/input/joystick/grip.ko] undefined!
   ERROR: "isa_io_base" [drivers/input/joystick/gf2k.ko] undefined!
   ERROR: "isa_io_base" [drivers/input/joystick/cobra.ko] undefined!
   ERROR: "isa_io_base" [drivers/input/joystick/analog.ko] undefined!
   ERROR: "isa_io_base" [drivers/input/joystick/adi.ko] undefined!
   ERROR: "isa_io_base" [drivers/input/joystick/a3d.ko] undefined!
   ERROR: "isa_io_base" [drivers/input/gameport/ns558.ko] undefined!
   ERROR: "isa_io_base" [drivers/input/gameport/lightning.ko] undefined!
   ERROR: "isa_io_base" [drivers/input/gameport/gameport.ko] undefined!
   ERROR: "isa_io_base" [drivers/input/gameport/fm801-gp.ko] undefined!
   ERROR: "isa_io_base" [drivers/i2c/busses/i2c-viapro.ko] undefined!
   ERROR: "isa_io_base" [drivers/i2c/busses/i2c-via.ko] undefined!
   ERROR: "isa_io_base" [drivers/i2c/busses/i2c-sis96x.ko] undefined!
   ERROR: "isa_io_base" [drivers/i2c/busses/i2c-sis630.ko] undefined!
   ERROR: "isa_io_base" [drivers/i2c/busses/i2c-sis5595.ko] undefined!
   ERROR: "isa_io_base" [drivers/i2c/busses/i2c-piix4.ko] undefined!
   ERROR: "isa_io_base" [drivers/i2c/busses/i2c-parport-light.ko] undefined!
   ERROR: "isa_io_base" [drivers/i2c/busses/i2c-nforce2.ko] undefined!
   ERROR: "isa_io_base" [drivers/i2c/busses/i2c-isch.ko] undefined!
   ERROR: "isa_io_base" [drivers/i2c/busses/i2c-i801.ko] undefined!
   ERROR: "isa_io_base" [drivers/i2c/busses/i2c-amd8111.ko] undefined!
   ERROR: "isa_io_base" [drivers/i2c/busses/i2c-amd756.ko] undefined!
   ERROR: "isa_io_base" [drivers/i2c/busses/i2c-ali15x3.ko] undefined!
   ERROR: "isa_io_base" [drivers/i2c/busses/i2c-ali1563.ko] undefined!
   ERROR: "isa_io_base" [drivers/i2c/busses/i2c-ali1535.ko] undefined!
   ERROR: "isa_io_base" [drivers/hwmon/w83627hf.ko] undefined!
   ERROR: "isa_io_base" [drivers/hwmon/w83627ehf.ko] undefined!
   ERROR: "isa_io_base" [drivers/hwmon/vt8231.ko] undefined!
   ERROR: "isa_io_base" [drivers/hwmon/vt1211.ko] undefined!
   ERROR: "isa_io_base" [drivers/hwmon/via686a.ko] undefined!
   ERROR: "isa_io_base" [drivers/hwmon/smsc47m1.ko] undefined!
   ERROR: "isa_io_base" [drivers/hwmon/smsc47b397.ko] undefined!
   ERROR: "isa_io_base" [drivers/hwmon/sis5595.ko] undefined!
   ERROR: "isa_io_base" [drivers/hwmon/sch56xx-common.ko] undefined!
   ERROR: "isa_io_base" [drivers/hwmon/pc87427.ko] undefined!
   ERROR: "isa_io_base" [drivers/hwmon/pc87360.ko] undefined!
   ERROR: "isa_io_base" [drivers/hwmon/nct6775.ko] undefined!
   ERROR: "isa_io_base" [drivers/hwmon/nct6683.ko] undefined!
   ERROR: "isa_io_base" [drivers/hwmon/it87.ko] undefined!
   ERROR: "isa_io_base" [drivers/hwmon/f71882fg.ko] undefined!
   ERROR: "isa_io_base" [drivers/hwmon/f71805f.ko] undefined!
   ERROR: "isa_io_base" [drivers/hwmon/dme1737.ko] undefined!
   ERROR: "isa_io_base" [drivers/gpu/drm/qxl/qxl.ko] undefined!
   ERROR: "isa_io_base" [drivers/gpu/drm/cirrus/cirrus.ko] undefined!
   ERROR: "isa_io_base" [drivers/gpu/drm/bochs/bochs-drm.ko] undefined!
   ERROR: "isa_io_base" [drivers/gpio/gpio-vx855.ko] undefined!
   ERROR: "isa_io_base" [drivers/gpio/gpio-ts5500.ko] undefined!
   ERROR: "isa_io_base" [drivers/gpio/gpio-sch311x.ko] undefined!
   ERROR: "isa_io_base" [drivers/gpio/gpio-amd8111.ko] undefined!
   ERROR: "isa_io_base" [drivers/char/tpm/tpm_atmel.ko] undefined!
   ERROR: "isa_io_base" [drivers/char/ipmi/ipmi_si.ko] undefined!
   ERROR: "isa_io_base" [drivers/block/paride/on26.ko] undefined!
   ERROR: "isa_io_base" [drivers/block/paride/on20.ko] undefined!
   ERROR: "isa_io_base" [drivers/block/paride/ktti.ko] undefined!
   ERROR: "isa_io_base" [drivers/block/paride/kbic.ko] undefined!
   ERROR: "isa_io_base" [drivers/block/paride/frpw.ko] undefined!
   ERROR: "isa_io_base" [drivers/block/paride/friq.ko] undefined!
   ERROR: "isa_io_base" [drivers/block/paride/fit3.ko] undefined!
   ERROR: "isa_io_base" [drivers/block/paride/fit2.ko] undefined!
   ERROR: "isa_io_base" [drivers/block/paride/epia.ko] undefined!
   ERROR: "isa_io_base" [drivers/block/paride/epat.ko] undefined!
   ERROR: "isa_io_base" [drivers/block/paride/dstr.ko] undefined!
   ERROR: "isa_io_base" [drivers/block/paride/comm.ko] undefined!
   ERROR: "isa_io_base" [drivers/block/paride/bpck6.ko] undefined!
   ERROR: "isa_io_base" [drivers/block/paride/bpck.ko] undefined!
   ERROR: "isa_io_base" [drivers/block/paride/aten.ko] undefined!
   ERROR: "isa_io_base" [drivers/atm/zatm.ko] undefined!
   ERROR: "isa_io_base" [drivers/atm/horizon.ko] undefined!
   ERROR: "isa_io_base" [drivers/atm/ambassador.ko] undefined!
   ERROR: "isa_io_base" [drivers/ata/pata_sc1200.ko] undefined!
   ERROR: "isa_io_base" [drivers/ata/pata_pdc202xx_old.ko] undefined!
   ERROR: "isa_io_base" [drivers/ata/pata_optidma.ko] undefined!
   ERROR: "isa_io_base" [drivers/ata/pata_legacy.ko] undefined!
   ERROR: "isa_io_base" [drivers/ata/pata_hpt3x2n.ko] undefined!
   ERROR: "isa_io_base" [drivers/ata/pata_hpt37x.ko] undefined!
   ERROR: "isa_io_base" [drivers/ata/pata_cypress.ko] undefined!
   ERROR: "isa_io_base" [drivers/ata/pata_cmd64x.ko] undefined!

---
0-DAY kernel test infrastructure                Open Source Technology Center
https://lists.01.org/pipermail/kbuild-all                   Intel Corporation
kernel test robot Oct. 27, 2015, 9:18 p.m. UTC | #2
Hi Matias,

[auto build test ERROR on next-20151022 -- if it's inappropriate base, please suggest rules for selecting the more suitable base]

url:    https://github.com/0day-ci/linux/commits/Matias-Bj-rling/Support-for-Open-Channel-SSDs/20151027-230440
config: i386-allyesconfig (attached as .config)
reproduce:
        # save the attached .config to linux build tree
        make ARCH=i386 

All errors (new ones prefixed by >>):

   drivers/built-in.o: In function `addr_to_generic_mode':
>> gennvm.c:(.text+0x88e078): undefined reference to `__umoddi3'
>> gennvm.c:(.text+0x88e090): undefined reference to `__udivdi3'
   gennvm.c:(.text+0x88e0a1): undefined reference to `__umoddi3'
   gennvm.c:(.text+0x88e0c7): undefined reference to `__udivdi3'
   gennvm.c:(.text+0x88e0d5): undefined reference to `__umoddi3'
   gennvm.c:(.text+0x88e10d): undefined reference to `__udivdi3'
   gennvm.c:(.text+0x88e11b): undefined reference to `__umoddi3'
   gennvm.c:(.text+0x88e144): undefined reference to `__udivdi3'
   drivers/built-in.o: In function `gennvm_block_map':
   gennvm.c:(.text+0x88e298): undefined reference to `__udivdi3'
   gennvm.c:(.text+0x88e2c9): undefined reference to `__udivdi3'
   drivers/built-in.o: In function `gennvm_erase_blk':
   gennvm.c:(.text+0x88e459): undefined reference to `__umoddi3'

---
0-DAY kernel test infrastructure                Open Source Technology Center
https://lists.01.org/pipermail/kbuild-all                   Intel Corporation

Patch
diff mbox

diff --git a/drivers/lightnvm/Kconfig b/drivers/lightnvm/Kconfig
index d4f309f..2a27971 100644
--- a/drivers/lightnvm/Kconfig
+++ b/drivers/lightnvm/Kconfig
@@ -25,4 +25,11 @@  config NVM_DEBUG
 
 	It is required to create/remove targets without IOCTLs.
 
+config NVM_GENNVM
+	tristate "Generic NVM manager for Open-Channel SSDs"
+	---help---
+	NVM media manager for Open-Channel SSDs that offload management
+	functionality to device, while keeping data placement and garbage
+	collection decisions on the host.
+
 endif # NVM
diff --git a/drivers/lightnvm/Makefile b/drivers/lightnvm/Makefile
index 38185e9..e2428e0 100644
--- a/drivers/lightnvm/Makefile
+++ b/drivers/lightnvm/Makefile
@@ -3,3 +3,4 @@ 
 #
 
 obj-$(CONFIG_NVM)		:= core.o
+obj-$(CONFIG_NVM_GENNVM) 	+= gennvm.o
diff --git a/drivers/lightnvm/gennvm.c b/drivers/lightnvm/gennvm.c
new file mode 100644
index 0000000..ba4d962
--- /dev/null
+++ b/drivers/lightnvm/gennvm.c
@@ -0,0 +1,475 @@ 
+/*
+ * Copyright (C) 2015 Matias Bjorling <m@bjorling.me>
+ *
+ * This program is free software; you can redistribute it and/or
+ * modify it under the terms of the GNU General Public License version
+ * 2 as published by the Free Software Foundation.
+ *
+ * This program is distributed in the hope that it will be useful, but
+ * WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
+ * General Public License for more details.
+ *
+ * You should have received a copy of the GNU General Public License
+ * along with this program; see the file COPYING.  If not, write to
+ * the Free Software Foundation, 675 Mass Ave, Cambridge, MA 02139,
+ * USA.
+ *
+ * Implementation of a generic nvm manager for Open-Channel SSDs.
+ */
+
+#include "gennvm.h"
+
+static void gennvm_blocks_free(struct nvm_dev *dev)
+{
+	struct gen_nvm *gn = dev->mp;
+	struct gen_lun *lun;
+	int i;
+
+	gennvm_for_each_lun(gn, lun, i) {
+		if (!lun->vlun.blocks)
+			break;
+		vfree(lun->vlun.blocks);
+	}
+}
+
+static void gennvm_luns_free(struct nvm_dev *dev)
+{
+	struct gen_nvm *gn = dev->mp;
+
+	kfree(gn->luns);
+}
+
+static int gennvm_luns_init(struct nvm_dev *dev, struct gen_nvm *gn)
+{
+	struct gen_lun *lun;
+	int i;
+
+	gn->luns = kcalloc(dev->nr_luns, sizeof(struct gen_lun), GFP_KERNEL);
+	if (!gn->luns)
+		return -ENOMEM;
+
+	gennvm_for_each_lun(gn, lun, i) {
+		spin_lock_init(&lun->vlun.lock);
+		INIT_LIST_HEAD(&lun->free_list);
+		INIT_LIST_HEAD(&lun->used_list);
+		INIT_LIST_HEAD(&lun->bb_list);
+
+		lun->reserved_blocks = 2; /* for GC only */
+		lun->vlun.id = i;
+		lun->vlun.lun_id = i % dev->luns_per_chnl;
+		lun->vlun.chnl_id = i / dev->luns_per_chnl;
+		lun->vlun.nr_free_blocks = dev->blks_per_lun;
+	}
+	return 0;
+}
+
+static int gennvm_block_bb(u32 lun_id, void *bb_bitmap, unsigned int nr_blocks,
+								void *private)
+{
+	struct gen_nvm *gn = private;
+	struct gen_lun *lun = &gn->luns[lun_id];
+	struct nvm_block *block;
+	int i;
+
+	if (unlikely(bitmap_empty(bb_bitmap, nr_blocks)))
+		return 0;
+
+	i = -1;
+	while ((i = find_next_bit(bb_bitmap, nr_blocks, i + 1)) <
+			nr_blocks) {
+		block = &lun->vlun.blocks[i];
+		if (!block) {
+			pr_err("gen_nvm: BB data is out of bounds.\n");
+			return -EINVAL;
+		}
+		list_move_tail(&block->list, &lun->bb_list);
+	}
+
+	return 0;
+}
+
+static int gennvm_block_map(u64 slba, u32 nlb, __le64 *entries, void *private)
+{
+	struct nvm_dev *dev = private;
+	struct gen_nvm *gn = dev->mp;
+	sector_t max_pages = dev->total_pages * (dev->sec_size >> 9);
+	u64 elba = slba + nlb;
+	struct gen_lun *lun;
+	struct nvm_block *blk;
+	u64 i;
+	int lun_id;
+
+	if (unlikely(elba > dev->total_pages)) {
+		pr_err("gen_nvm: L2P data from device is out of bounds!\n");
+		return -EINVAL;
+	}
+
+	for (i = 0; i < nlb; i++) {
+		u64 pba = le64_to_cpu(entries[i]);
+
+		if (unlikely(pba >= max_pages && pba != U64_MAX)) {
+			pr_err("gen_nvm: L2P data entry is out of bounds!\n");
+			return -EINVAL;
+		}
+
+		/* Address zero is a special one. The first page on a disk is
+		 * protected. It often holds internal device boot
+		 * information.
+		 */
+		if (!pba)
+			continue;
+
+		/* resolve block from physical address */
+		lun_id = pba / dev->sec_per_lun;
+		lun = &gn->luns[lun_id];
+
+		/* Calculate block offset into lun */
+		pba = pba - (dev->sec_per_lun * lun_id);
+		blk = &lun->vlun.blocks[pba / dev->sec_per_blk];
+
+		if (!blk->type) {
+			/* at this point, we don't know anything about the
+			 * block. It's up to the FTL on top to re-etablish the
+			 * block state
+			 */
+			list_move_tail(&blk->list, &lun->used_list);
+			blk->type = 1;
+			lun->vlun.nr_free_blocks--;
+		}
+	}
+
+	return 0;
+}
+
+static int gennvm_blocks_init(struct nvm_dev *dev, struct gen_nvm *gn)
+{
+	struct gen_lun *lun;
+	struct nvm_block *block;
+	sector_t lun_iter, blk_iter, cur_block_id = 0;
+	int ret;
+
+	gennvm_for_each_lun(gn, lun, lun_iter) {
+		lun->vlun.blocks = vzalloc(sizeof(struct nvm_block) *
+							dev->blks_per_lun);
+		if (!lun->vlun.blocks)
+			return -ENOMEM;
+
+		for (blk_iter = 0; blk_iter < dev->blks_per_lun; blk_iter++) {
+			block = &lun->vlun.blocks[blk_iter];
+
+			INIT_LIST_HEAD(&block->list);
+
+			block->lun = &lun->vlun;
+			block->id = cur_block_id++;
+
+			/* First block is reserved for device */
+			if (unlikely(lun_iter == 0 && blk_iter == 0))
+				continue;
+
+			list_add_tail(&block->list, &lun->free_list);
+		}
+
+		if (dev->ops->get_bb_tbl) {
+			ret = dev->ops->get_bb_tbl(dev->q, lun->vlun.id,
+					dev->blks_per_lun, gennvm_block_bb, gn);
+			if (ret)
+				pr_err("gen_nvm: could not read BB table\n");
+		}
+	}
+
+	if (dev->ops->get_l2p_tbl) {
+		ret = dev->ops->get_l2p_tbl(dev->q, 0, dev->total_pages,
+							gennvm_block_map, dev);
+		if (ret) {
+			pr_err("gen_nvm: could not read L2P table.\n");
+			pr_warn("gen_nvm: default block initialization");
+		}
+	}
+
+	return 0;
+}
+
+static int gennvm_register(struct nvm_dev *dev)
+{
+	struct gen_nvm *gn;
+	int ret;
+
+	gn = kzalloc(sizeof(struct gen_nvm), GFP_KERNEL);
+	if (!gn)
+		return -ENOMEM;
+
+	gn->nr_luns = dev->nr_luns;
+	dev->mp = gn;
+
+	ret = gennvm_luns_init(dev, gn);
+	if (ret) {
+		pr_err("gen_nvm: could not initialize luns\n");
+		goto err;
+	}
+
+	ret = gennvm_blocks_init(dev, gn);
+	if (ret) {
+		pr_err("gen_nvm: could not initialize blocks\n");
+		goto err;
+	}
+
+	return 1;
+err:
+	kfree(gn);
+	return ret;
+}
+
+static void gennvm_unregister(struct nvm_dev *dev)
+{
+	gennvm_blocks_free(dev);
+	gennvm_luns_free(dev);
+	kfree(dev->mp);
+	dev->mp = NULL;
+}
+
+static struct nvm_block *gennvm_get_blk(struct nvm_dev *dev,
+				struct nvm_lun *vlun, unsigned long flags)
+{
+	struct gen_lun *lun = container_of(vlun, struct gen_lun, vlun);
+	struct nvm_block *blk = NULL;
+	int is_gc = flags & NVM_IOTYPE_GC;
+
+	BUG_ON(!lun);
+
+	spin_lock(&vlun->lock);
+
+	if (list_empty(&lun->free_list)) {
+		pr_err_ratelimited("gen_nvm: lun %u have no free pages available",
+								lun->vlun.id);
+		spin_unlock(&vlun->lock);
+		goto out;
+	}
+
+	while (!is_gc && lun->vlun.nr_free_blocks < lun->reserved_blocks) {
+		spin_unlock(&vlun->lock);
+		goto out;
+	}
+
+	blk = list_first_entry(&lun->free_list, struct nvm_block, list);
+	list_move_tail(&blk->list, &lun->used_list);
+	blk->type = 1;
+
+	lun->vlun.nr_free_blocks--;
+
+	spin_unlock(&vlun->lock);
+out:
+	return blk;
+}
+
+static void gennvm_put_blk(struct nvm_dev *dev, struct nvm_block *blk)
+{
+	struct nvm_lun *vlun = blk->lun;
+	struct gen_lun *lun = container_of(vlun, struct gen_lun, vlun);
+
+	spin_lock(&vlun->lock);
+
+	switch (blk->type) {
+	case 1:
+		list_move_tail(&blk->list, &lun->free_list);
+		lun->vlun.nr_free_blocks++;
+		blk->type = 0;
+		break;
+	case 2:
+		list_move_tail(&blk->list, &lun->bb_list);
+		break;
+	default:
+		BUG();
+	}
+
+	spin_unlock(&vlun->lock);
+}
+
+static void gennvm_addr_to_generic_mode(struct nvm_dev *dev, struct nvm_rq *rqd)
+{
+	int i;
+
+	if (rqd->nr_pages > 1)
+		for (i = 0; i < rqd->nr_pages; i++)
+			rqd->ppa_list[i] = addr_to_generic_mode(dev,
+							rqd->ppa_list[i]);
+	else
+		rqd->ppa_addr = addr_to_generic_mode(dev, rqd->ppa_addr);
+}
+
+static void gennvm_generic_to_addr_mode(struct nvm_dev *dev, struct nvm_rq *rqd)
+{
+	int i;
+
+	if (rqd->nr_pages > 1)
+		for (i = 0; i < rqd->nr_pages; i++)
+			rqd->ppa_list[i] = generic_to_addr_mode(dev,
+							rqd->ppa_list[i]);
+	else
+		rqd->ppa_addr = generic_to_addr_mode(dev, rqd->ppa_addr);
+}
+
+static int gennvm_submit_io(struct nvm_dev *dev, struct nvm_rq *rqd)
+{
+	if (!dev->ops->submit_io)
+		return 0;
+
+	/* Convert address space */
+	gennvm_generic_to_addr_mode(dev, rqd);
+
+	rqd->dev = dev;
+	return dev->ops->submit_io(dev->q, rqd);
+}
+
+static void gennvm_blk_set_type(struct nvm_dev *dev, struct ppa_addr *ppa,
+								int type)
+{
+	struct gen_nvm *gn = dev->mp;
+	struct gen_lun *lun;
+	struct nvm_block *blk;
+
+	BUG_ON(ppa->g.ch > dev->nr_chnls);
+	BUG_ON(ppa->g.lun > dev->luns_per_chnl);
+	BUG_ON(ppa->g.blk > dev->blks_per_lun);
+
+	lun = &gn->luns[ppa->g.lun * ppa->g.ch];
+	blk = &lun->vlun.blocks[ppa->g.blk];
+
+	/* will be moved to bb list on put_blk from target */
+	blk->type = type;
+}
+
+/* mark block bad. It is expected the target recover from the error. */
+static void gennvm_mark_blk_bad(struct nvm_dev *dev, struct nvm_rq *rqd)
+{
+	int i;
+
+	if (!dev->ops->set_bb)
+		return;
+
+	if (dev->ops->set_bb(dev->q, rqd, 1))
+		return;
+
+	gennvm_addr_to_generic_mode(dev, rqd);
+
+	/* look up blocks and mark them as bad */
+	if (rqd->nr_pages > 1)
+		for (i = 0; i < rqd->nr_pages; i++)
+			gennvm_blk_set_type(dev, &rqd->ppa_list[i], 2);
+	else
+		gennvm_blk_set_type(dev, &rqd->ppa_addr, 2);
+}
+
+static int gennvm_end_io(struct nvm_rq *rqd, int error)
+{
+	struct nvm_tgt_instance *ins = rqd->ins;
+	int ret = 0;
+
+	switch (error) {
+	case NVM_RSP_SUCCESS:
+		break;
+	case NVM_RSP_ERR_EMPTYPAGE:
+		break;
+	case NVM_RSP_ERR_FAILWRITE:
+		gennvm_mark_blk_bad(rqd->dev, rqd);
+	default:
+		ret++;
+	}
+
+	ret += ins->tt->end_io(rqd, error);
+
+	return ret;
+}
+
+static int gennvm_erase_blk(struct nvm_dev *dev, struct nvm_block *blk,
+							unsigned long flags)
+{
+	int plane_cnt = 0, pl_idx, ret;
+	struct ppa_addr addr;
+	struct nvm_rq rqd;
+
+	if (!dev->ops->erase_block)
+		return 0;
+
+	addr = block_to_ppa(dev, blk);
+
+	if (dev->plane_mode == NVM_PLANE_SINGLE) {
+		rqd.nr_pages = 1;
+		rqd.ppa_addr = addr;
+	} else {
+		plane_cnt = (1 << dev->plane_mode);
+		rqd.nr_pages = plane_cnt;
+
+		rqd.ppa_list = nvm_dev_dma_alloc(dev, GFP_KERNEL,
+							&rqd.dma_ppa_list);
+		if (!rqd.ppa_list) {
+			pr_err("gen_nvm: failed to allocate dma memory\n");
+			return -ENOMEM;
+		}
+
+		for (pl_idx = 0; pl_idx < plane_cnt; pl_idx++) {
+			addr.g.pl = pl_idx;
+			rqd.ppa_list[pl_idx] = addr;
+		}
+	}
+
+	gennvm_generic_to_addr_mode(dev, &rqd);
+
+	ret = dev->ops->erase_block(dev->q, &rqd);
+
+	if (plane_cnt)
+		nvm_dev_dma_free(dev, rqd.ppa_list, rqd.dma_ppa_list);
+
+	return ret;
+}
+
+static struct nvm_lun *gennvm_get_lun(struct nvm_dev *dev, int lunid)
+{
+	struct gen_nvm *gn = dev->mp;
+
+	return &gn->luns[lunid].vlun;
+}
+
+static void gennvm_free_blocks_print(struct nvm_dev *dev)
+{
+	struct gen_nvm *gn = dev->mp;
+	struct gen_lun *lun;
+	unsigned int i;
+
+	gennvm_for_each_lun(gn, lun, i)
+		pr_info("%s: lun%8u\t%u\n",
+					dev->name, i, lun->vlun.nr_free_blocks);
+}
+
+static struct nvmm_type gennvm = {
+	.name		= "gennvm",
+	.version	= {0, 1, 0},
+
+	.register_mgr	= gennvm_register,
+	.unregister_mgr	= gennvm_unregister,
+
+	.get_blk	= gennvm_get_blk,
+	.put_blk	= gennvm_put_blk,
+
+	.submit_io	= gennvm_submit_io,
+	.end_io		= gennvm_end_io,
+	.erase_blk	= gennvm_erase_blk,
+
+	.get_lun	= gennvm_get_lun,
+	.free_blocks_print = gennvm_free_blocks_print,
+};
+
+static int __init gennvm_module_init(void)
+{
+	return nvm_register_mgr(&gennvm);
+}
+
+static void gennvm_module_exit(void)
+{
+	nvm_unregister_mgr(&gennvm);
+}
+
+module_init(gennvm_module_init);
+module_exit(gennvm_module_exit);
+MODULE_LICENSE("GPL v2");
+MODULE_DESCRIPTION("Block manager for Hybrid Open-Channel SSDs");
diff --git a/drivers/lightnvm/gennvm.h b/drivers/lightnvm/gennvm.h
new file mode 100644
index 0000000..d23bd35
--- /dev/null
+++ b/drivers/lightnvm/gennvm.h
@@ -0,0 +1,46 @@ 
+/*
+ * Copyright: Matias Bjorling <mb@bjorling.me>
+ *
+ * This program is free software; you can redistribute it and/or
+ * modify it under the terms of the GNU General Public License version
+ * 2 as published by the Free Software Foundation.
+ *
+ * This program is distributed in the hope that it will be useful, but
+ * WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
+ * General Public License for more details.
+ *
+ */
+
+#ifndef GENNVM_H_
+#define GENNVM_H_
+
+#include <linux/module.h>
+#include <linux/vmalloc.h>
+
+#include <linux/lightnvm.h>
+
+struct gen_lun {
+	struct nvm_lun vlun;
+
+	int reserved_blocks;
+	/* lun block lists */
+	struct list_head used_list;	/* In-use blocks */
+	struct list_head free_list;	/* Not used blocks i.e. released
+					 * and ready for use
+					 */
+	struct list_head bb_list;	/* Bad blocks. Mutually exclusive with
+					 * free_list and used_list
+					 */
+};
+
+struct gen_nvm {
+	int nr_luns;
+	struct gen_lun *luns;
+};
+
+#define gennvm_for_each_lun(bm, lun, i) \
+		for ((i) = 0, lun = &(bm)->luns[0]; \
+			(i) < (bm)->nr_luns; (i)++, lun = &(bm)->luns[(i)])
+
+#endif /* GENNVM_H_ */