mtd: document ABI, move to include/mtd

We're missing a lot of important documentation in include/mtd/mtd-abi.h:

* add a simple description of each ioctl (feel free to expand!)
* give full explanations of recently added and modified operations
* explain the usage of "RAW" that appear in different modes and types of
  operations
* fix some comment style along the way

Also, the file was moved to include/mtd instead of include/linux/mtd

Signed-off-by: Brian Norris <computersforpeace@gmail.com>
Signed-off-by: Artem Bityutskiy <artem.bityutskiy@intel.com>
mtd: nand: kill member `ops' of `struct nand_chip'

The nand_chip.ops field is a struct that is passed around globally with
no particular reason. Every time it is used, it could just as easily be
replaced with a local struct that is updated on each operation. So make
it local.

Signed-off-by: Brian Norris <computersforpeace@gmail.com>
Signed-off-by: Artem Bityutskiy <artem.bityutskiy@intel.com>
mtd: nand: refactor scanning code

A few pieces of code are unnecessarily duplicated. For easier
maintenance, we should fix this.

This should have no functional effect.

Signed-off-by: Brian Norris <computersforpeace@gmail.com>
Signed-off-by: Artem Bityutskiy <artem.bityutskiy@intel.com>
mtd: nand: do not ignore all ECC errors

There are a few reasons not to ignore ECC errors here.

First, mtd->read_oob is being called in raw mode, so there should be no
error correction in the first place.

Second, if we change this such that there *is* error correction in this
function, then we will want to pass the error message upward.

In fact, the code I introduced to "ignore ECC errors" would have been
better if it had just placed this test down in `scan_block_full()' in
the first place. We would like to ignore ECC errors when we are simply
checking for bad block markers (e.g., factory marked), but we may not
want to ignore ECC errors when scanning OOB for a flash-based BBT
pattern (in `scan_read_raw()'; note that the return codes from
`scan_read_raw()' are not actually handled yet).

Signed-off-by: Brian Norris <computersforpeace@gmail.com>
Signed-off-by: Artem Bityutskiy <artem.bityutskiy@intel.com>
mtd: nand: remove unnecessary variable

`writeops' is unnecessary in the function `nand_update_bbt()'

Signed-off-by: Brian Norris <computersforpeace@gmail.com>
Signed-off-by: Artem Bityutskiy <artem.bityutskiy@intel.com>
mtd: nand: fix style

Remove some extra spaces
Consistently use '0x' prefix for bitfield-like constants
Spelling: "aplies" -> "applies"

Signed-off-by: Brian Norris <computersforpeace@gmail.com>
Signed-off-by: Artem Bityutskiy <artem.bityutskiy@intel.com>
mtd: nand: begin restructuring check_create

We will begin restructuring the code for check_create so that we can
make some important changes. For now, we should just begin to get rid of
some goto statements to make things cleaner. This is the first step of a
few, which are separated to make them easier to follow.

This step should just be a code refactor.

Signed-off-by: Brian Norris <computersforpeace@gmail.com>
Signed-off-by: Artem Bityutskiy <artem.bityutskiy@intel.com>
mtd: nand: remove gotos in `check_create()'

This is a second step in restructuring `check_create()'. When we don't
rely on goto statements for our main functionality, the code will become
a little easier to manipulate.

Signed-off-by: Brian Norris <computersforpeace@gmail.com>
Signed-off-by: Artem Bityutskiy <artem.bityutskiy@intel.com>
mtd: define `mtd_is_*()' functions

These functions can be used instead of referencing -EUCLEAN and -EBADMSG
all over the place. They should help make code a little bit more
readable.

Signed-off-by: Brian Norris <computersforpeace@gmail.com>
Signed-off-by: Artem Bityutskiy <artem.bityutskiy@intel.com>
mtd: utilize `mtd_is_*()' functions

Signed-off-by: Brian Norris <computersforpeace@gmail.com>
Signed-off-by: Artem Bityutskiy <artem.bityutskiy@intel.com>
mtd: nand: report ECC errors properly when reading BBT

Instead of just printing a warning when encountering ECC errors, we
should return a proper error status and print a more informative
warning. Later, we will handle these error messages in the upper layers
of the BBT scan.

Note that this patch makes our check for ECC error codes a little bit
more restrictive, leaving all unrecognized errors to the generic "else"
clause. This shouldn't cause problems and could even be a benefit.

This code is based on some findings reported by Matthieu Castet.

Reported-by: Matthieu CASTET <matthieu.castet@parrot.com>
Signed-off-by: Brian Norris <computersforpeace@gmail.com>
Signed-off-by: Artem Bityutskiy <artem.bityutskiy@intel.com>
mtd: nand: scrub BBT on ECC errors

Now that `read_bbt()' returns ECC error codes properly, we handle those
codes when checking the integrity of our flash-based BBT.

The modifications can be described by this new policy:

*) On any uncorrected ECC error, we invalidate the corresponding table
   and retry our version-checking integrity logic.
*) On corrected bitflips, we mark both tables for re-writing to flash
   (a.k.a. scrubbing).

Current integrity checks (i.e., comparing version numbers, etc.) should
take care of all the cases that result in rescanning the device for bad
blocks or falling back to the BBT as found in the mirror descriptor.

Signed-off-by: Brian Norris <computersforpeace@gmail.com>
Signed-off-by: Artem Bityutskiy <artem.bityutskiy@intel.com>
mtd: nand: wait to set BBT version

Because there are so many cases of checking, writing, and re-writing of
the bad block table(s), we might as well wait until the we've settled on
a valid, clean copy of the table. This also prevents us from falsely
incrementing the table version. For example, we may have the following:

  Primary table, with version 0x02
  Mirror table, with version 0x01
  Primary table has uncorrectable ECC errors

If we don't have this fix applied, then we will:

  Choose to read the primary table (higher version)
  Set mirror table version to 0x02
  Read back primary table
  Invalidate table because of ECC errors
  Retry readback operation with mirror table, now version 0x02
  Mirrored table reads cleanly
  Writeback BBT to primary table location (with "version 0x02")

However, the mirrored table shouldn't have a new version number.
Instead, we actually want:

  Choose to read the primary table (higher version)
  Read back primary table
  Invalidate table because of ECC errors
  Retry readback with mirror table (version 0x01)
  Mirrored table reads cleanly
  Set both tables to version 0x01
  Writeback BBT to primary table location (version 0x01)

Signed-off-by: Brian Norris <computersforpeace@gmail.com>
Signed-off-by: Artem Bityutskiy <artem.bityutskiy@intel.com>
mtd: nand: do not scan bad blocks with NAND_BBT_NO_OOB set

Updates to our default function for creating bad block patterns have
broken the "no OOB" feature. The NAND_BBT_NO_OOB option should not be
set while scanning for bad blocks, but we've been passing all BBT
options from nand_chip.bbt_options to the bad block scan. This causes us
to hit the:

	BUG_ON(bd->options & NAND_BBT_NO_OOB);

in create_bbt() when we scan the flash for bad blocks.

Thus, while it can be legal to set NAND_BBT_NO_OOB in a custom badblock
pattern descriptor (presumably with NAND_BBT_CREATE disabled?), we
should not pass it through in our default function.

Also, to help clarify and emphasize that the function creates bad block
patterns only (not, for example, table descriptors for locating
flash-based BBT), I renamed `nand_create_default_bbt_descr' to
`nand_create_badblock_pattern'.

Signed-off-by: Brian Norris <computersforpeace@gmail.com>
Signed-off-by: Artem Bityutskiy <artem.bityutskiy@intel.com>
mtd: nand: invalidate cache on unaligned reads

In rare cases, we are given an unaligned parameter `from' in
`nand_do_read_ops()'. In such cases, we use the page cache
(chip->buffers->databuf) as an intermediate buffer before dumping to the
client buffer. However, there are also cases where this buffer is not
cleanly reusable. In those cases, we need to make sure that we
explicitly invalidate the cache.

This patch prevents accidental reusage of the page cache, and for me,
this solves some problems I come across when reading a corrupted BBT
from flash (NAND_BBT_USE_FLASH and NAND_BBT_NO_OOB).

Note: the rare "unaligned" case is a result of the extra BBT pattern +
version located in the data area instead of OOB.

Also, this patch disables caching on raw reads, since we are reading
without error correction. This is, obviously, prone to errors and should
not be cached.

Signed-off-by: Brian Norris <computersforpeace@gmail.com>
Signed-off-by: Artem Bityutskiy <artem.bityutskiy@intel.com>
mtd: nand: switch `check_pattern()' to standard `memcmp()'

A portion of the `check_pattern()' function is basically a `memcmp()'.
Since it's possible for `memcmp()' to be optimized for a particular
architecture, we should use it instead.

Signed-off-by: Brian Norris <computersforpeace@gmail.com>
Signed-off-by: Artem Bityutskiy <artem.bityutskiy@intel.com>
mtd: nand: initialize ops.mode

Our `ops' information was converted to a local variable recently, and
apparently, old code relied on the fact that the global version was
often left in a valid mode. We can't make this assumption on local
structs, and we shouldn't be relying on a previous state anyway.

Instead, we initialize mode to 0 for don't-care situations (i.e., the
operation does not use OOB anyway) and MTD_OPS_PLACE_OOB when we want to
place OOB data.

This fixes a bug with nand_default_block_markbad(), where we catch on
the BUG() call in nand_fill_oob():

Kernel bug detected[#1]:
...
Call Trace:
[<80307350>] nand_fill_oob.clone.5+0xa4/0x15c
[<803075d8>] nand_do_write_oob+0x1d0/0x260
[<803077c4>] nand_default_block_markbad+0x15c/0x1a8
[<802e8c2c>] part_block_markbad+0x80/0x98
[<802ebc74>] mtd_ioctl+0x6d8/0xbd0
[<802ec1a4>] mtd_unlocked_ioctl+0x38/0x5c
[<800d9c60>] do_vfs_ioctl+0xa4/0x6e4
[<800da2e4>] sys_ioctl+0x44/0xa0
[<8001381c>] stack_done+0x20/0x40

Signed-off-by: Brian Norris <computersforpeace@gmail.com>
Signed-off-by: Artem Bityutskiy <artem.bityutskiy@linux.intel.com>
mtd: nand: add Macronix manufacturer

Macronix is produing SLC NAND MX30LF1208AA, so add their manufacturer
code to the manufacturer lists.

Signed-off-by: Brian Norris <computersforpeace@gmail.com>
Signed-off-by: Artem Bityutskiy <Artem.Bityutskiy@linux.intel.com>
Signed-off-by: David Woodhouse <David.Woodhouse@intel.com>
mtd: nand: add 512 Mbit device code (Macronix)

Macronix MX30LF1208AA is a 512 Mbit NAND with device code 0xF0.

Signed-off-by: Brian Norris <computersforpeace@gmail.com>
Signed-off-by: Artem Bityutskiy <Artem.Bityutskiy@linux.intel.com>
Signed-off-by: David Woodhouse <David.Woodhouse@intel.com>
mtd: nand: scan 1st and 2nd page for Macronix SLC

Signed-off-by: Brian Norris <computersforpeace@gmail.com>
Signed-off-by: Artem Bityutskiy <Artem.Bityutskiy@linux.intel.com>
Signed-off-by: David Woodhouse <David.Woodhouse@intel.com>