NetBSD Problem Report #36716

From martin@duskware.de  Wed Aug  1 07:33:17 2007
Return-Path: <martin@duskware.de>
Received: from mail.netbsd.org (mail.netbsd.org [204.152.190.11])
	by narn.NetBSD.org (Postfix) with ESMTP id 5B43A63BA03
	for <gnats-bugs@gnats.netbsd.org>; Wed,  1 Aug 2007 07:33:17 +0000 (UTC)
Message-Id: <20070801052116.94F5C63BA03@narn.NetBSD.org>
Date: Wed,  1 Aug 2007 05:21:16 +0000 (UTC)
From: rumble@ephemeral.org
Reply-To: rumble@ephemeral.org
To: netbsd-bugs-owner@NetBSD.org
Subject: cd(4) problem with transfers exceeding 65535 bytes
X-Send-Pr-Version: www-1.0

>Number:         36716
>Category:       kern
>Synopsis:       cd(4) problem with transfers exceeding 65535 bytes
>Confidential:   no
>Severity:       serious
>Priority:       medium
>Responsible:    kern-bug-people
>State:          closed
>Class:          sw-bug
>Submitter-Id:   net
>Arrival-Date:   Wed Aug 01 07:35:00 +0000 2007
>Closed-Date:    Mon Dec 17 12:21:03 +0000 2007
>Last-Modified:  Mon Dec 17 12:21:03 +0000 2007
>Originator:     Stephen M. Rumble
>Release:        4.99.25
>Organization:
>Environment:
NetBSD x31.ephemeral.org 4.99.25 NetBSD 4.99.25 (X31) #1: Tue Jul 31 22:36:09 EDT 2007  rumble@x31.ephemeral.org:/usr/src/sys/arch/i386/compile/X31 i386
>Description:
When reading an EFS file system from CD, cd(4) will often start transfers greater than 65535 bytes. This is due to the fact that bounce buffering often rounds accesses to 33 sectors (66k). The following errors result:

On my Thinkpad X31:

    piixide0:1: unable to load xfer table DMA map for drive 0, error=22
    piixide0:1:0: lost interrupt
        type: atapi tc_bcount: 67584 tc_skip: 0

    ... repeats indefinitely ...

In QEMU (0.8.2nb2):

    piixide0:1:0 lost interrupt
        type: atapi tc_bcount: 0 tc_skip: 67584

    ... repeats indefinitely ...

I have no idea where error gets set to 22 in the X31 case, as that doesn't appear to be a return value of bus_dmamem_map. The DMA map error comes from sys/dev/pci/pciide_common.c:567. The lost interrupt error comes from sys/dev/ic/wdc.c:1324.

I've managed to work around the problem is two different ways.

The first alters dev/scsipi/cd.c and limits bounce buffers to a maximum of MAXPHYS bytes. Any additional I/O is done in another buf. A patch can be found here:
    http://mail-index.netbsd.org/tech-kern/2007/07/28/0005.html

The second workaround alters dev/scsipi/atapi_wdc.c line 598 to read as follows:
    xfer->c_bcount <= 0xfffd ? xfer->c_bcount : 0xfffd,

Presently transfers are limited to 0xffff (the highest value representable in the cylinder registers). Setting it to 0xfffd or lower results in normal function. 0xfffe (which, if I understand the ATAPI docs correctly is the same as 0xffff) results in the same error.

I don't understand how either of the workarounds succeed. It seems clear that the problem has to do with exceeding 0xffff somewhere, but whether this has to do with MAXPHYS, or the cylinder register limitations, I do not know.

The first workaround simply limits the size of the request from the scsipi layer, but the individual device reads are still potentially as many as 0xffff bytes. The second gets a larger request from scsipi, but limits the individual device reads to <= 0xfffd bytes. Both work independently, which seems especially odd.
>How-To-Repeat:
It should be easily repeatable with an atapi cdrom when used with a disklabel where d_secsize != 2048.
>Fix:
Workarounds exist, but the true cause of the errors are unknown.

>Release-Note:

>Audit-Trail:
From: Izumi Tsutsui <tsutsui@ceres.dti.ne.jp>
To: gnats-bugs@NetBSD.org
Cc: kern-bug-people@NetBSD.org, gnats-admin@NetBSD.org,
	netbsd-bugs@NetBSD.org, tsutsui@ceres.dti.ne.jp
Subject: Re: kern/36716: cd(4) problem with transfers exceeding 65535 bytes
Date: Wed, 1 Aug 2007 21:26:54 +0900

 rumble@ephemeral.org wrote:

 > >How-To-Repeat:
 > It should be easily repeatable with an atapi cdrom when used
 > with a disklabel where d_secsize != 2048.

 I saw the similar problem on sgimips with CD-ROM which had sgivolhdr:
 http://mail-index.netbsd.org/port-sgimips/2005/09/12/0000.html
 http://mail-index.netbsd.org/port-sgimips/2005/09/14/0000.html

 It seems the proper workaround to fix readdisklabel(9) to
 always return d_secsize = 2048 for CDs, but I'm not sure.
 ---
 Izumi Tsutsui

From: "Stephen M. Rumble" <stephen.rumble@utoronto.ca>
To: gnats-bugs@NetBSD.org, Izumi Tsutsui <tsutsui@ceres.dti.ne.jp>
Cc: kern-bug-people@NetBSD.org, gnats-admin@NetBSD.org,
	netbsd-bugs@NetBSD.org
Subject: Re: kern/36716: cd(4) problem with transfers exceeding 65535 bytes
Date: Wed, 01 Aug 2007 12:29:59 -0400

 Quoting Izumi Tsutsui <tsutsui@ceres.dti.ne.jp>:

 > The following reply was made to PR kern/36716; it has been noted by GNATS.
 >
 > From: Izumi Tsutsui <tsutsui@ceres.dti.ne.jp>
 > To: gnats-bugs@NetBSD.org
 > Cc: kern-bug-people@NetBSD.org, gnats-admin@NetBSD.org,
 > 	netbsd-bugs@NetBSD.org, tsutsui@ceres.dti.ne.jp
 > Subject: Re: kern/36716: cd(4) problem with transfers exceeding 65535 bytes
 > Date: Wed, 1 Aug 2007 21:26:54 +0900
 >
 >  rumble@ephemeral.org wrote:
 >
 >  > >How-To-Repeat:
 >  > It should be easily repeatable with an atapi cdrom when used
 >  > with a disklabel where d_secsize != 2048.
 >
 >  I saw the similar problem on sgimips with CD-ROM which had sgivolhdr:
 >  http://mail-index.netbsd.org/port-sgimips/2005/09/12/0000.html
 >  http://mail-index.netbsd.org/port-sgimips/2005/09/14/0000.html
 >
 >  It seems the proper workaround to fix readdisklabel(9) to
 >  always return d_secsize = 2048 for CDs, but I'm not sure.

 If I understand things correctly, the problem is that non-2048b  
 sectors weren't working properly and your solution was to use 2048b  
 sectors and update the disklabel offsets/lengths in memory, yes? If  
 that's the case, I think it's only hiding the problem. We need  
 d_secsize == 512 for EFS images, and it should 'just work'.

 If you're able to repeat the tests, I'd be interested to know if my  
 aforementioned cd(4) bounce patch resolves the issue for you.

 The problem as I see it is that cd(4) will make requests > MAXPHYS  
 when bounce buffers are used and we need to round up one more sector  
 to accommodate unaligned accesses. Is sending such a request to scsipi  
 an invalid thing to do? If it's not permitted, it should be asserted  
 somewhere, and I think my patch is the proper solution.

 If, however, it is entirely valid, we have problems elsewhere. So long  
 as in cd.c:796 nblks is <= 32 (corresponding to a maximum transfer of  
 MAXPHYS), the underlying subsystems seem happy. However, if b_bcount  
 is > (2^16), we can't just cap nblks, rather we appear to need to  
 split up requests. I'm assuming nblks is passed to the device, and  
 b_bcount is used to initiate DMA transfers until everything is read.  
 Is it illegal to send a device a read command that cannot be handled  
 in one DMA?

 What's really confusing to me is that, in the atapi case, if we leave  
 everything else alone (letting nblks be 33, which is 66k) and instead  
 limit the DMA transfer size in atapi_wdc.c to 0xfffd, everything  
 works. However, if we permit transfers of 0xfffe or 0xffff, it breaks.  
 If we cap nblks at 32, then 0xffff-sized transfers are fine in that  
 interrupts don't get lost, but we don't end up getting all of the  
 requested data from the drive.

 It seems that restricting the DMA size in atapi_wdc.c just happens to  
 work by chance and wouldn't fix the real problem (which appears to  
 exist for SCSI as well).

 So, in short, questions I'd love to have answered:
    1) Is issuing requests > MAXPHYS to scsipi inherently bad or unsupported?
    2) To satisfy my curiosity, can anybody tell me why limiting the ATAPI DMA
       transfer size to 0xfffd or lower avoids the problem, while limiting to
       0xfffe or 0xffff does not?

 Steve

From: Izumi Tsutsui <tsutsui@ceres.dti.ne.jp>
To: rumble@ephemeral.org
Cc: gnats-bugs@NetBSD.org, kern-bug-people@NetBSD.org,
	gnats-admin@NetBSD.org, netbsd-bugs@NetBSD.org,
	tsutsui@ceres.dti.ne.jp
Subject: Re: kern/36716: cd(4) problem with transfers exceeding 65535 bytes
Date: Thu, 2 Aug 2007 02:02:05 +0900

 stephen.rumble@utoronto.ca wrote:

 > If I understand things correctly, the problem is that non-2048b  
 > sectors weren't working properly and your solution was to use 2048b  
 > sectors and update the disklabel offsets/lengths in memory, yes? If  
 > that's the case, I think it's only hiding the problem. We need  
 > d_secsize == 512 for EFS images, and it should 'just work'.

 Hmm, I thought d_secsize represented hardware sector size
 so it should be the same with params.blksize, but if d_secsize
 represents logical sector size, my fix might be wrong.

 I'm not sure why my fix works if d_secsize should be 512 though,
 and I wonder if logical sector size which is different from hardware
 sector size should be handled in lower drivers such as cd(4) or
 upper filesystem layers.

 >    1) Is issuing requests > MAXPHYS to scsipi inherently bad or unsupported?

 Ususally lower device drivers don't allocate resources for
 xfers more than MAXPHYS. In most case bus_dmamap_create(9)
 takes MAXPHYS for "size" arg so bus_dmamap_load(9) will
 fail on a request for >MAXPHYS xfer.

 >    2) To satisfy my curiosity, can anybody tell me why limiting the ATAPI DMA
 >       transfer size to 0xfffd or lower avoids the problem, while limiting to
 >       0xfffe or 0xffff does not?

 I guess DMA on pciide(4) is done per 32bit and can't across 64k boundary.
 (per dev/pci/pciide_common.c etc.)
 ---
 Izumi Tsutsui

From: "Stephen M. Rumble" <stephen.rumble@utoronto.ca>
To: Izumi Tsutsui <tsutsui@ceres.dti.ne.jp>
Cc: gnats-bugs@NetBSD.org, kern-bug-people@NetBSD.org,
	gnats-admin@NetBSD.org, netbsd-bugs@NetBSD.org
Subject: Re: kern/36716: cd(4) problem with transfers exceeding 65535 bytes
Date: Wed, 01 Aug 2007 13:26:45 -0400

 Quoting Izumi Tsutsui <tsutsui@ceres.dti.ne.jp>:

 > stephen.rumble@utoronto.ca wrote:
 >
 >> If I understand things correctly, the problem is that non-2048b
 >> sectors weren't working properly and your solution was to use 2048b
 >> sectors and update the disklabel offsets/lengths in memory, yes? If
 >> that's the case, I think it's only hiding the problem. We need
 >> d_secsize == 512 for EFS images, and it should 'just work'.
 >
 > Hmm, I thought d_secsize represented hardware sector size
 > so it should be the same with params.blksize, but if d_secsize
 > represents logical sector size, my fix might be wrong.
 >
 > I'm not sure why my fix works if d_secsize should be 512 though,
 > and I wonder if logical sector size which is different from hardware
 > sector size should be handled in lower drivers such as cd(4) or
 > upper filesystem layers.

 I think that d_secsize typically represents the hardware sector size.  
 However, with cd(4) it is used as the logical sector size. The  
 hardware sector size is read from the drive. If it differs from what  
 the disklabel says, cd(4) will adjust the offsets/lengths accordingly,  
 and employ bounce buffers if necessary (see cdbounce and the related  
 logic in cdstrategy).

 >>    1) Is issuing requests > MAXPHYS to scsipi inherently bad or unsupported?
 >
 > Ususally lower device drivers don't allocate resources for
 > xfers more than MAXPHYS. In most case bus_dmamap_create(9)
 > takes MAXPHYS for "size" arg so bus_dmamap_load(9) will
 > fail on a request for >MAXPHYS xfer.

 In the case of ATAPI, I think the hardware is limited to 0xffff bytes  
 (or is it 0xfffe?). I'll have to trace what dma size  
 pciide_dma_table_setup is trying to load.

 In any event, if devices are typically unable to handle > MAXPHYS  
 transfers, then scsipi shouldn't be handing such requests down to  
 them, no? I get the impression that MAXPHYS is used all over the place  
 and it's assumed by each layer that appropriately-sized requests will  
 be passed down. If <= MAXPHYS is an invariant, it should probably be  
 asserted somewhere.

 >>    2) To satisfy my curiosity, can anybody tell me why limiting the  
 >>  ATAPI DMA
 >>       transfer size to 0xfffd or lower avoids the problem, while limiting to
 >>       0xfffe or 0xffff does not?
 >
 > I guess DMA on pciide(4) is done per 32bit and can't across 64k boundary.
 > (per dev/pci/pciide_common.c etc.)

 None of those values exceed 64k and 0xfffd works, but is not an  
 integral multiple of 4.

 Thanks for the input. It looks like fixing cd(4) may be the correct way to go.

 Steve

From: "Stephen M. Rumble" <rumble@netbsd.org>
To: gnats-bugs@NetBSD.org
Cc: 
Subject: PR/36716 CVS commit: src/sys/dev/scsipi
Date: Sat,  4 Aug 2007 02:58:59 +0000 (UTC)

 Module Name:	src
 Committed By:	rumble
 Date:		Sat Aug  4 02:58:59 UTC 2007

 Modified Files:
 	src/sys/dev/scsipi: cd.c

 Log Message:
 Be sure not to make requests over MAXPHYS when employing bounce buffers.
 It was previously possible to round up one sector length if the start block
 were not properly aligned.

 Resolves PR kern/36716. This may also fix the following reported issues
 on port-sgimips:
     http://mail-index.netbsd.org/port-sgimips/2005/09/12/0000.html
     http://mail-index.netbsd.org/port-sgimips/2005/09/14/0000.html

 While here, be sure to put back allocated bufs, which previously appear
 to have been leaked. Further, remove the dead write bounce code which never
 got executed anyway.

 No complaints on tech-kern.


 To generate a diff of this commit:
 cvs rdiff -r1.267 -r1.268 src/sys/dev/scsipi/cd.c

 Please note that diffs are not public domain; they are subject to the
 copyright notices on the relevant files.

State-Changed-From-To: open->closed
State-Changed-By: tsutsui@netbsd.org
State-Changed-When: Mon, 17 Dec 2007 21:21:03 +0900
State-Changed-Why:
Fix committed by rumble@.


>Unformatted:

NetBSD Home
NetBSD PR Database Search

(Contact us) $NetBSD: query-full-pr,v 1.51 2026/08/10 02:28:17 riastradh Exp $
$NetBSD: gnats_config.sh,v 1.10 2026/05/13 22:00:09 riastradh Exp $
Copyright © 1994-2026 The NetBSD Foundation, Inc. ALL RIGHTS RESERVED.