linux-kernel.vger.kernel.org archive mirror
 help / color / mirror / Atom feed
* [PATCH] mtd: rawnand: denali: do not pass zero maxchips to nand_scan()
@ 2018-08-21  8:23 Masahiro Yamada
  2018-08-21  8:42 ` Miquel Raynal
  2018-08-24 12:55 ` Boris Brezillon
  0 siblings, 2 replies; 5+ messages in thread
From: Masahiro Yamada @ 2018-08-21  8:23 UTC (permalink / raw)
  To: linux-mtd, Boris Brezillon, Miquel Raynal
  Cc: Masahiro Yamada, linux-kernel, Marek Vasut, Brian Norris,
	Richard Weinberger, David Woodhouse

Commit 49aa76b16676 ("mtd: rawnand: do not execute nand_scan_ident()
if maxchips is zero") gave a new meaning for calling nand_scan_ident()
with maxchips=0.

It is a special usage for some drivers such as docg4, but in fact
the Denali driver may pass maxchips=0 to nand_scan() when the driver
is enabled but no NAND chip is found on the board for some reasons.

If nand_scan_with_ids() is called with maxchips=0, nand_scan_ident()
is skipped, i.e. nand_set_defaults() is skipped.  Therefore, the
driver must have set chip->controller beforehand.  Otherwise,
nand_attach() causes NULL pointer dereference.

In fact, the Denali controller knows the number of connected chips
before calling nand_scan_ident(); if DEVICE_RESET fails, there is no
chip in that chip select.  Then, denali_reset_banks() sets the maxchips
to the number of detected chips.  If no chip is found, it is zero.

The reason of this trick was, as commit f486287d2372 ("mtd: nand:
denali: fix bank reset function to detect the number of chips")
explained, nand_scan_ident() issued Set Features (0xEF) command
to all CS lines, some of which may not be connected with a chip.
Then, the driver would wait until R/B# response, which never happens.

This problem was solved by commit 107b7d6a7ad4 ("mtd: rawnand: avoid
setting again the timings to mode 0 after a reset").  In the current
code, nand_setup_data_interface() is called from nand_scan_tail(),
which is after the chip detection is done.

Remove the code that is causing NULL pointer dereference.  Now, the
maxchips passed to nand_scan() is the maximum number of chip selects
supported by the IP (typically 4 or 8).  Leave all the chip detection
process to nand_scan_ident().

Signed-off-by: Masahiro Yamada <yamada.masahiro@socionext.com>
---

 drivers/mtd/nand/raw/denali.c | 1 -
 1 file changed, 1 deletion(-)

diff --git a/drivers/mtd/nand/raw/denali.c b/drivers/mtd/nand/raw/denali.c
index ca18612..3e4b8e1 100644
--- a/drivers/mtd/nand/raw/denali.c
+++ b/drivers/mtd/nand/raw/denali.c
@@ -1086,7 +1086,6 @@ static void denali_reset_banks(struct denali_nand_info *denali)
 	}
 
 	dev_dbg(denali->dev, "%d chips connected\n", i);
-	denali->max_banks = i;
 }
 
 static void denali_hw_init(struct denali_nand_info *denali)
-- 
2.7.4


^ permalink raw reply related	[flat|nested] 5+ messages in thread

* Re: [PATCH] mtd: rawnand: denali: do not pass zero maxchips to nand_scan()
  2018-08-21  8:23 [PATCH] mtd: rawnand: denali: do not pass zero maxchips to nand_scan() Masahiro Yamada
@ 2018-08-21  8:42 ` Miquel Raynal
  2018-08-24 12:55 ` Boris Brezillon
  1 sibling, 0 replies; 5+ messages in thread
From: Miquel Raynal @ 2018-08-21  8:42 UTC (permalink / raw)
  To: Masahiro Yamada
  Cc: linux-mtd, Boris Brezillon, linux-kernel, Marek Vasut,
	Brian Norris, Richard Weinberger, David Woodhouse

Hi Masahiro,

Masahiro Yamada <yamada.masahiro@socionext.com> wrote on Tue, 21 Aug
2018 17:23:19 +0900:

> Commit 49aa76b16676 ("mtd: rawnand: do not execute nand_scan_ident()
> if maxchips is zero") gave a new meaning for calling nand_scan_ident()
> with maxchips=0.
> 
> It is a special usage for some drivers such as docg4, but in fact
> the Denali driver may pass maxchips=0 to nand_scan() when the driver
> is enabled but no NAND chip is found on the board for some reasons.
> 
> If nand_scan_with_ids() is called with maxchips=0, nand_scan_ident()
> is skipped, i.e. nand_set_defaults() is skipped.  Therefore, the
> driver must have set chip->controller beforehand.  Otherwise,
> nand_attach() causes NULL pointer dereference.
> 
> In fact, the Denali controller knows the number of connected chips
> before calling nand_scan_ident(); if DEVICE_RESET fails, there is no
> chip in that chip select.  Then, denali_reset_banks() sets the maxchips
> to the number of detected chips.  If no chip is found, it is zero.
> 
> The reason of this trick was, as commit f486287d2372 ("mtd: nand:
> denali: fix bank reset function to detect the number of chips")
> explained, nand_scan_ident() issued Set Features (0xEF) command
> to all CS lines, some of which may not be connected with a chip.
> Then, the driver would wait until R/B# response, which never happens.
> 
> This problem was solved by commit 107b7d6a7ad4 ("mtd: rawnand: avoid
> setting again the timings to mode 0 after a reset").  In the current
> code, nand_setup_data_interface() is called from nand_scan_tail(),
> which is after the chip detection is done.
> 
> Remove the code that is causing NULL pointer dereference.  Now, the
> maxchips passed to nand_scan() is the maximum number of chip selects
> supported by the IP (typically 4 or 8).  Leave all the chip detection
> process to nand_scan_ident().
> 
> Signed-off-by: Masahiro Yamada <yamada.masahiro@socionext.com>

Thanks for the fix.

Actually the docg4 driver is getting removed and the special handling
in nand_scan() about having 0 maxchips will also disappear.
Nonetheless, I think the below code was buggy and I would like to apply
the fix anyway.

> ---
> 
>  drivers/mtd/nand/raw/denali.c | 1 -
>  1 file changed, 1 deletion(-)
> 
> diff --git a/drivers/mtd/nand/raw/denali.c b/drivers/mtd/nand/raw/denali.c
> index ca18612..3e4b8e1 100644
> --- a/drivers/mtd/nand/raw/denali.c
> +++ b/drivers/mtd/nand/raw/denali.c
> @@ -1086,7 +1086,6 @@ static void denali_reset_banks(struct denali_nand_info *denali)
>  	}
>  
>  	dev_dbg(denali->dev, "%d chips connected\n", i);
> -	denali->max_banks = i;
>  }
>  
>  static void denali_hw_init(struct denali_nand_info *denali)

Thanks,
Miquèl

^ permalink raw reply	[flat|nested] 5+ messages in thread

* Re: [PATCH] mtd: rawnand: denali: do not pass zero maxchips to nand_scan()
  2018-08-21  8:23 [PATCH] mtd: rawnand: denali: do not pass zero maxchips to nand_scan() Masahiro Yamada
  2018-08-21  8:42 ` Miquel Raynal
@ 2018-08-24 12:55 ` Boris Brezillon
  2018-08-24 15:04   ` Masahiro Yamada
  1 sibling, 1 reply; 5+ messages in thread
From: Boris Brezillon @ 2018-08-24 12:55 UTC (permalink / raw)
  To: Masahiro Yamada
  Cc: linux-mtd, Miquel Raynal, linux-kernel, Marek Vasut,
	Brian Norris, Richard Weinberger, David Woodhouse

Hi Masahiro,

On Tue, 21 Aug 2018 17:23:19 +0900
Masahiro Yamada <yamada.masahiro@socionext.com> wrote:

> Commit 49aa76b16676 ("mtd: rawnand: do not execute nand_scan_ident()
> if maxchips is zero") gave a new meaning for calling nand_scan_ident()
> with maxchips=0.
> 
> It is a special usage for some drivers such as docg4, but in fact
> the Denali driver may pass maxchips=0 to nand_scan() when the driver
> is enabled but no NAND chip is found on the board for some reasons.
> 
> If nand_scan_with_ids() is called with maxchips=0, nand_scan_ident()
> is skipped, i.e. nand_set_defaults() is skipped.  Therefore, the
> driver must have set chip->controller beforehand.  Otherwise,
> nand_attach() causes NULL pointer dereference.
> 
> In fact, the Denali controller knows the number of connected chips
> before calling nand_scan_ident(); if DEVICE_RESET fails, there is no
> chip in that chip select.  Then, denali_reset_banks() sets the maxchips
> to the number of detected chips.  If no chip is found, it is zero.
> 
> The reason of this trick was, as commit f486287d2372 ("mtd: nand:
> denali: fix bank reset function to detect the number of chips")
> explained, nand_scan_ident() issued Set Features (0xEF) command
> to all CS lines, some of which may not be connected with a chip.
> Then, the driver would wait until R/B# response, which never happens.
> 
> This problem was solved by commit 107b7d6a7ad4 ("mtd: rawnand: avoid
> setting again the timings to mode 0 after a reset").  In the current
> code, nand_setup_data_interface() is called from nand_scan_tail(),
> which is after the chip detection is done.
> 
> Remove the code that is causing NULL pointer dereference.  Now, the
> maxchips passed to nand_scan() is the maximum number of chip selects
> supported by the IP (typically 4 or 8).  Leave all the chip detection
> process to nand_scan_ident().
> 
> Signed-off-by: Masahiro Yamada <yamada.masahiro@socionext.com>
> ---
> 
>  drivers/mtd/nand/raw/denali.c | 1 -
>  1 file changed, 1 deletion(-)
> 
> diff --git a/drivers/mtd/nand/raw/denali.c b/drivers/mtd/nand/raw/denali.c
> index ca18612..3e4b8e1 100644
> --- a/drivers/mtd/nand/raw/denali.c
> +++ b/drivers/mtd/nand/raw/denali.c
> @@ -1086,7 +1086,6 @@ static void denali_reset_banks(struct denali_nand_info *denali)
>  	}
>  
>  	dev_dbg(denali->dev, "%d chips connected\n", i);
> -	denali->max_banks = i;

Shouldn't we instead avoid calling nand_scan() when
denali->max_banks=0? I mean, what's the point of calling this function
if you know for sure it will fail.

Last question: do we still need this denali_reset_banks()? If it's only
about resetting the chip to detect how many are actually present,
that's already done by nand_scan().

>  }
>  
>  static void denali_hw_init(struct denali_nand_info *denali)


^ permalink raw reply	[flat|nested] 5+ messages in thread

* Re: [PATCH] mtd: rawnand: denali: do not pass zero maxchips to nand_scan()
  2018-08-24 12:55 ` Boris Brezillon
@ 2018-08-24 15:04   ` Masahiro Yamada
  2018-08-24 15:19     ` Boris Brezillon
  0 siblings, 1 reply; 5+ messages in thread
From: Masahiro Yamada @ 2018-08-24 15:04 UTC (permalink / raw)
  To: Boris Brezillon
  Cc: linux-mtd, Miquel Raynal, Linux Kernel Mailing List, Marek Vasut,
	Brian Norris, Richard Weinberger, David Woodhouse

Hi Boris,

2018-08-24 21:55 GMT+09:00 Boris Brezillon <boris.brezillon@bootlin.com>:
> Hi Masahiro,
>
> On Tue, 21 Aug 2018 17:23:19 +0900
> Masahiro Yamada <yamada.masahiro@socionext.com> wrote:
>
>> Commit 49aa76b16676 ("mtd: rawnand: do not execute nand_scan_ident()
>> if maxchips is zero") gave a new meaning for calling nand_scan_ident()
>> with maxchips=0.
>>
>> It is a special usage for some drivers such as docg4, but in fact
>> the Denali driver may pass maxchips=0 to nand_scan() when the driver
>> is enabled but no NAND chip is found on the board for some reasons.
>>
>> If nand_scan_with_ids() is called with maxchips=0, nand_scan_ident()
>> is skipped, i.e. nand_set_defaults() is skipped.  Therefore, the
>> driver must have set chip->controller beforehand.  Otherwise,
>> nand_attach() causes NULL pointer dereference.
>>
>> In fact, the Denali controller knows the number of connected chips
>> before calling nand_scan_ident(); if DEVICE_RESET fails, there is no
>> chip in that chip select.  Then, denali_reset_banks() sets the maxchips
>> to the number of detected chips.  If no chip is found, it is zero.
>>
>> The reason of this trick was, as commit f486287d2372 ("mtd: nand:
>> denali: fix bank reset function to detect the number of chips")
>> explained, nand_scan_ident() issued Set Features (0xEF) command
>> to all CS lines, some of which may not be connected with a chip.
>> Then, the driver would wait until R/B# response, which never happens.
>>
>> This problem was solved by commit 107b7d6a7ad4 ("mtd: rawnand: avoid
>> setting again the timings to mode 0 after a reset").  In the current
>> code, nand_setup_data_interface() is called from nand_scan_tail(),
>> which is after the chip detection is done.
>>
>> Remove the code that is causing NULL pointer dereference.  Now, the
>> maxchips passed to nand_scan() is the maximum number of chip selects
>> supported by the IP (typically 4 or 8).  Leave all the chip detection
>> process to nand_scan_ident().
>>
>> Signed-off-by: Masahiro Yamada <yamada.masahiro@socionext.com>
>> ---
>>
>>  drivers/mtd/nand/raw/denali.c | 1 -
>>  1 file changed, 1 deletion(-)
>>
>> diff --git a/drivers/mtd/nand/raw/denali.c b/drivers/mtd/nand/raw/denali.c
>> index ca18612..3e4b8e1 100644
>> --- a/drivers/mtd/nand/raw/denali.c
>> +++ b/drivers/mtd/nand/raw/denali.c
>> @@ -1086,7 +1086,6 @@ static void denali_reset_banks(struct denali_nand_info *denali)
>>       }
>>
>>       dev_dbg(denali->dev, "%d chips connected\n", i);
>> -     denali->max_banks = i;
>
> Shouldn't we instead avoid calling nand_scan() when
> denali->max_banks=0? I mean, what's the point of calling this function
> if you know for sure it will fail.


Right.  If no chip is found, it should error out with -ENODEV or something.



> Last question: do we still need this denali_reset_banks()? If it's only
> about resetting the chip to detect how many are actually present,
> that's already done by nand_scan().

I thought this too.

Please give me time to answer this question.
I need to check the datasheet and test on my boards.

If I can remove denali_reset_banks() entirely,
it would be the best.

Thanks.



>>  }
>>
>>  static void denali_hw_init(struct denali_nand_info *denali)
>



-- 
Best Regards
Masahiro Yamada

^ permalink raw reply	[flat|nested] 5+ messages in thread

* Re: [PATCH] mtd: rawnand: denali: do not pass zero maxchips to nand_scan()
  2018-08-24 15:04   ` Masahiro Yamada
@ 2018-08-24 15:19     ` Boris Brezillon
  0 siblings, 0 replies; 5+ messages in thread
From: Boris Brezillon @ 2018-08-24 15:19 UTC (permalink / raw)
  To: Masahiro Yamada
  Cc: linux-mtd, Miquel Raynal, Linux Kernel Mailing List, Marek Vasut,
	Brian Norris, Richard Weinberger, David Woodhouse

On Sat, 25 Aug 2018 00:04:43 +0900
Masahiro Yamada <yamada.masahiro@socionext.com> wrote:

> Hi Boris,
> 
> 2018-08-24 21:55 GMT+09:00 Boris Brezillon <boris.brezillon@bootlin.com>:
> > Hi Masahiro,
> >
> > On Tue, 21 Aug 2018 17:23:19 +0900
> > Masahiro Yamada <yamada.masahiro@socionext.com> wrote:
> >  
> >> Commit 49aa76b16676 ("mtd: rawnand: do not execute nand_scan_ident()
> >> if maxchips is zero") gave a new meaning for calling nand_scan_ident()
> >> with maxchips=0.
> >>
> >> It is a special usage for some drivers such as docg4, but in fact
> >> the Denali driver may pass maxchips=0 to nand_scan() when the driver
> >> is enabled but no NAND chip is found on the board for some reasons.
> >>
> >> If nand_scan_with_ids() is called with maxchips=0, nand_scan_ident()
> >> is skipped, i.e. nand_set_defaults() is skipped.  Therefore, the
> >> driver must have set chip->controller beforehand.  Otherwise,
> >> nand_attach() causes NULL pointer dereference.
> >>
> >> In fact, the Denali controller knows the number of connected chips
> >> before calling nand_scan_ident(); if DEVICE_RESET fails, there is no
> >> chip in that chip select.  Then, denali_reset_banks() sets the maxchips
> >> to the number of detected chips.  If no chip is found, it is zero.
> >>
> >> The reason of this trick was, as commit f486287d2372 ("mtd: nand:
> >> denali: fix bank reset function to detect the number of chips")
> >> explained, nand_scan_ident() issued Set Features (0xEF) command
> >> to all CS lines, some of which may not be connected with a chip.
> >> Then, the driver would wait until R/B# response, which never happens.
> >>
> >> This problem was solved by commit 107b7d6a7ad4 ("mtd: rawnand: avoid
> >> setting again the timings to mode 0 after a reset").  In the current
> >> code, nand_setup_data_interface() is called from nand_scan_tail(),
> >> which is after the chip detection is done.
> >>
> >> Remove the code that is causing NULL pointer dereference.  Now, the
> >> maxchips passed to nand_scan() is the maximum number of chip selects
> >> supported by the IP (typically 4 or 8).  Leave all the chip detection
> >> process to nand_scan_ident().
> >>
> >> Signed-off-by: Masahiro Yamada <yamada.masahiro@socionext.com>
> >> ---
> >>
> >>  drivers/mtd/nand/raw/denali.c | 1 -
> >>  1 file changed, 1 deletion(-)
> >>
> >> diff --git a/drivers/mtd/nand/raw/denali.c b/drivers/mtd/nand/raw/denali.c
> >> index ca18612..3e4b8e1 100644
> >> --- a/drivers/mtd/nand/raw/denali.c
> >> +++ b/drivers/mtd/nand/raw/denali.c
> >> @@ -1086,7 +1086,6 @@ static void denali_reset_banks(struct denali_nand_info *denali)
> >>       }
> >>
> >>       dev_dbg(denali->dev, "%d chips connected\n", i);
> >> -     denali->max_banks = i;  
> >
> > Shouldn't we instead avoid calling nand_scan() when
> > denali->max_banks=0? I mean, what's the point of calling this function
> > if you know for sure it will fail.  
> 
> 
> Right.  If no chip is found, it should error out with -ENODEV or something.
> 
> 
> 
> > Last question: do we still need this denali_reset_banks()? If it's only
> > about resetting the chip to detect how many are actually present,
> > that's already done by nand_scan().  
> 
> I thought this too.
> 
> Please give me time to answer this question.
> I need to check the datasheet and test on my boards.
> 
> If I can remove denali_reset_banks() entirely,
> it would be the best.

I'd like the fix to be as simple as possible, so that I can queue it
for -rc2. Please consider the solution where nand_scan() is skipped
when denali->max_banks=0 first. We can then decide to remove
denali_reset_banks() if that's appropriate.

Thanks,

Boris

^ permalink raw reply	[flat|nested] 5+ messages in thread

end of thread, other threads:[~2018-08-24 15:19 UTC | newest]

Thread overview: 5+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2018-08-21  8:23 [PATCH] mtd: rawnand: denali: do not pass zero maxchips to nand_scan() Masahiro Yamada
2018-08-21  8:42 ` Miquel Raynal
2018-08-24 12:55 ` Boris Brezillon
2018-08-24 15:04   ` Masahiro Yamada
2018-08-24 15:19     ` Boris Brezillon

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).