History log of /openbsd-current/sys/dev/ata/wd.c
Revision (<<< Hide revision tags) (Show revision tags >>>) Date Author Comments
# 1.131 26-May-2024 jsg

remove unneeded includes


Revision tags: OPENBSD_7_3_BASE OPENBSD_7_4_BASE OPENBSD_7_5_BASE
# 1.130 23-Oct-2022 krw

bzero(disklabel) or memset(disklabel,0) should be enough for
anyone. No need to also set d_flags to 0.


Revision tags: OPENBSD_7_2_BASE
# 1.129 06-Apr-2022 naddy

constify struct cfattach


Revision tags: OPENBSD_7_1_BASE
# 1.128 09-Jan-2022 jsg

spelling
feedback and ok tb@ jmc@ ok ratchov@


Revision tags: OPENBSD_6_7_BASE OPENBSD_6_8_BASE OPENBSD_6_9_BASE OPENBSD_7_0_BASE
# 1.127 23-Jan-2020 tedu

we've been using LBA48 for < 137GB drives for 17 years, so perhaps
we can remove the XXX now.


Revision tags: OPENBSD_6_6_BASE
# 1.126 07-Jul-2019 kn

Use timeout_add_msec(9)

RECOVERYTIME is twice per second, so sleep for 500 milliseconds
and reflect the unit to be clear.

OK patrick


Revision tags: OPENBSD_6_3_BASE OPENBSD_6_4_BASE OPENBSD_6_5_BASE
# 1.125 30-Dec-2017 guenther

Don't pull in <sys/file.h> just to get fcntl.h

ok deraadt@ krw@


# 1.124 14-Dec-2017 deraadt

Repair flushcache/standby operations to not convert AT_WAIT operations
to AT_POLL, which some chip driver underneath cannot handle.
tested by bluhm also


# 1.123 24-Oct-2017 jsg

remove defines for ioctls the kernel doesn't recognise
ok mpi@


Revision tags: OPENBSD_6_2_BASE
# 1.122 29-May-2017 sf

Add an ioctl to tell storage devices to flush their internal caches

Currently implemented for wd and sd.
Initially ported from netbsd by pedro@

ok deraadt@


# 1.121 04-May-2017 deraadt

Also pass the blk offset to disk_unbusy(), so that it can pass it to
the random subsystem as entropy. This value is pretty much unknown,
and anyways our entropy input ring does not saturate from knowns.
ok mikeb djm


Revision tags: OPENBSD_5_9_BASE OPENBSD_6_0_BASE OPENBSD_6_1_BASE
# 1.120 20-Jan-2016 stefan

Set segflg member of uio struct with symbolic enum value
instead of using magic numbers. No binary change.

Diff from Martin Natano.

ok deraadt@


# 1.119 26-Aug-2015 deraadt

trivial size to free()


# 1.118 17-Aug-2015 krw

The 80's called and want their interface back. Remove last fragments of
ST-506 support. Remove redundant 'precomp' variables/fields and rename
remaining few to 'features' to be consistant with the rest of the code.

Consensus that no ST-506 disks (requiring ISA cards, <120MB, usually
rife with bad sectors that can't be worked around) are still running
OpenBSD.


Revision tags: OPENBSD_5_7_BASE OPENBSD_5_8_BASE
# 1.117 14-Sep-2014 jsg

remove uneeded proc.h includes
ok mpi@ kspillner@


Revision tags: OPENBSD_5_6_BASE
# 1.116 12-Jul-2014 tedu

add a size argument to free. will be used soon, but for now default to 0.
after discussions with beck deraadt kettenis.


# 1.115 10-Jul-2014 deraadt

remove most of the boolean_t infection outside uvm/ddb/pmap; ok jsg


Revision tags: OPENBSD_5_5_BASE
# 1.114 14-Oct-2013 krw

Use DL_SECTOBLK() instead of handrolling same calculation to get
wdsize() return value.


# 1.113 14-Oct-2013 krw

XXsize() returns daddr_t, so calculate the return value in a daddr_t
variable and not (usually) an int.

ok miod@


# 1.112 27-Sep-2013 krw

Try harder to ensure wddump() doesn't decrement the count
of sectors to write by one value and increment the address
the next i/o will start at by a different amount.


# 1.111 15-Sep-2013 krw

Make it clearer that sc_wdc_bio.blkno is actuall a disk hardware
address by using the DL_BLKTOSEC() and DL_SECTOBLK() defines rather
than handrolling the equivalent. Fixes current addition of a 512-byte
block value (b_blkno) to a disk hardware address (DL_GETPOFFSET()).

Tested on real wd(4) hardware by guenther@.


Revision tags: OPENBSD_5_4_BASE
# 1.110 11-Jun-2013 deraadt

final removal of daddr64_t. daddr_t has been 64 bit for a long enough
test period; i think 3 years ago the last bugs fell out.
ok otto beck others


Revision tags: OPENBSD_5_3_BASE
# 1.109 08-Oct-2012 deraadt

Revamp the sequences for suspend/hibernate -> resume so that the code
paths are reflexive. It is now possible to fail part-way through a
suspend sequence, and recover along the resume code path.
Split DVACT_SUSPEND by adding a new DVACT_POWERDOWN method is used
after hibernate (and suspend too) to finish the job. Some drivers
must be converted at the same time to use this instead of shutdown hooks
(the others will follow at a later time)
ok kettenis mlarkin


Revision tags: OPENBSD_5_0_BASE OPENBSD_5_1_BASE OPENBSD_5_2_BASE
# 1.108 06-Jul-2011 matthew

Eliminate redundant buf validation checks in xxstrategy() methods now
that they're implemented consistently in bounds_check_with_label().

Also, per krw's request, change bounds_check_with_label() to return 0
if the checks succeed, and change the drivers to test == -1 instead of
<= 0. (Man page update to follow; intentionally omitting
arch/vax/mba/hp.c from this commit because it doesn't even build
currently and miod@ promises to kill it soon.)

ok krw@


# 1.107 30-Jun-2011 matthew

Refactor some common open/close/detach disk driver code into
subr_disk.c. For now just the MI disk drivers.

ok deraadt@, krw@; jsing@ liked the approach too


# 1.106 20-Jun-2011 matthew

Call bufq_drain() in wddetach() instead of reimplementing the same
logic.


# 1.105 19-Jun-2011 deraadt

Use disk_lock_nointr() in the xxclose() routines so that they cannot
be interrupted.
ok matthew


# 1.104 19-Jun-2011 deraadt

Use disk_lock/disk_unlock directly and in the same way in these drivers,
rather than using various wrappings. Convert vnd to using the sc_dk
rwlock instead of using one of its own.
ok matthew


# 1.103 05-Jun-2011 matthew

Drop kernel support for the useless DIOCWLABEL ioctl and prune a lot
of silly flag twiddling code in various disk drivers.

ok deraadt@, miod@

N.B., users will need a -current disklabel(8) to be able to write new
disklabels to disk now.


# 1.102 03-Jun-2011 matthew

Get rid of the wlabel argument to bounds_check_with_label(). It's
never done anything in OpenBSD and just clutters disk drivers with
silly flag handling.

More cleanup to follow.

ok deraadt@, millert@; no objections krw@


# 1.101 31-May-2011 matthew

Change a few of the more common disk drivers (sd, cd, wd, rd, and vnd)
to return EBUSY if the user tries to modify an open partition's offset
or size. Only sadness can result if a user tries this, and rejecting
it prevents a race between sdstart() and sdstrategy().

Curiously, there was already code in the kernel and in disklabel(8) to
detect/handle this, but it was effectively disabled because the disk
drivers always used something like "/* sc->sc_dk.dk_openmask */ 0",
and this commented out code has existed since even r1.1 in NetBSD.

I had no problems building a release and messing around with
disklabel(8) for a bit with this diff. Canarying the more common MI
disk drivers until we gain confidence that there aren't any
regressions, then we can switch the remaining drivers.

"I am surprised you got me convinced that this stuff is safe" deraadt@
ok krw@


# 1.100 18-Apr-2011 deraadt

keep an eye out for wdc registers returning 0xff. if they return 0xff,
this probably means the controller is dead -- as in, a hot plug eject
event. in that case, mark the driver dead and try harder to return -1
in the interrupt handler.
tested by many
ok dlg, miod did not object


# 1.99 15-Apr-2011 miod

Change wdc_reset_channel() to take a `no wait' argument. Pass in turn this
argument to wdcreset(), to have it skip waiting until active channels see
their BUSY bit clear in the status register.

Use this feature in the resume path, during the first reset operation. The
first reset is supposed to only wake up the controller, and the disks don't
come back until the second reset is issued, therefore waiting for them to
report themselves as ready after the first reset, but before the second, is
moot - and as a matter of fact some controllers, such as the AMD 754 and
clones/offspring (e.g. Geode) keep the BUSY bit asserted after the first
reset.

Last, but not least, make sure wd@ata invokes wd_get_params() again before
returning from the resume code, as we will still be using polled transfers
for a short while.

This causes the Lemote Yeelong to resume within less than one second, instead
of the lousy 30 seconds wait between the two resets; and the wd_get_params()
voodoo prevents it from getting spurious ide interrupts afterwards.

wd_get_params() magic from dlg; rest of the work by yours truly after enough
prodding by dlg@ and pirofti@, among others. ok deraadt@ dlg@


# 1.98 05-Apr-2011 deraadt

wd_hibernate_io() is a standalone disk io writer which tries to not
damage kernel memory very much while doing the job. It is not very
pretty yet, but improving it will need some MI work.
ok mlarkin jsg dlg


Revision tags: OPENBSD_4_9_BASE
# 1.97 31-Dec-2010 kettenis

Reintroduce the code that puts the drive into standby in shutdown, but
only do this if we're going to power down the machine.

"My god, it is a 3 line diff. Commit it before it gets bigger!" deraadt@


# 1.96 30-Dec-2010 kettenis

The NVIDIA MCP89 SATA controller found on the 2010 Apple 13" MacBook Pro
can't address block 0xffffffff in LBA mode, at least in combination with
the Hitachi HTS545032B9SA02 and HTS545050B9SA02 drives that Apple ships.
So use LBA48 mode from that block onwards.

ok marco@, deraadt@, miod@


# 1.95 22-Sep-2010 matthew

All users of physio(9) now pass NULL as the buf pointer argument, so
no point in keeping it around.

"i like this" thib@ (a while back); ok krw@ and oga@; reminder to
update the man page and tweaks jmc@


# 1.94 20-Sep-2010 deraadt

DVACT_ACTIVATE and DVACT_DEACTIVATE do nothing here; remove
the false comments.


# 1.93 08-Sep-2010 jsing

Introduce a disk_lookup() function which calls device_lookup(), before
verifying that the resulting device is present on the disklist. This
avoids a race whereby the disk driver can be accessed as soon as the
softc has been allocated, but before the disk has completed
initialisation and has called disk_attach() (up until this point
dk_label is still a null pointer).

Cut cd(4), sd(4) and wd(4) across to disk_lookup(). All callers of
disk_attach() need to be tested and cut over in due course.

ok deraadt@ krw@


# 1.92 08-Sep-2010 jsing

Store a struct device pointer within struct disk and populate this when
disk_attach() is called by the device driver. We will be building on
this shortly.

ok deraadt@ krw@


# 1.91 01-Sep-2010 dlg

make struct bufq a member of the softc for devices that use it,
rather than it being a pointer to something that needs to be allocated
at attach. since all these devices need a bufq to operate, it makes
sense to have it allocated as part of the softc and get bufq_init
to just initialise all its fields. it also gets rid of the possibility
that you wont be able to allocate the bufq struct during attach,
which is something you dont want to happen.

secondly, it consistently implements a split between wrapper functions
and the per discipline implementation of the bufq handlers. it
consistently does the locking in the wrappers rather than doing
half in the wrappers and the other half in the implementations.

it also consistently handles the outstanding bufq bq pointer in the
wrappers.

this hides most of the implementation inside kern_bufq.c. the only
stuff left in buf.h is for the bits each implementation needs to
put inside struct buf.

tested by thib@ krw@ and me
ok thib@ matthew@
no objection from krw@


# 1.90 31-Aug-2010 deraadt

sort DVACT_ actions in order of use


# 1.89 28-Aug-2010 matthew

Garbage collect struct dkdriver.

ok miod@; "please go ahead" jsing@


Revision tags: OPENBSD_4_8_BASE
# 1.88 23-Jul-2010 jsg

Manuel Bouyer rescinded clauses 3 and 4 of his license text.


# 1.87 22-Jul-2010 kettenis

Give the drive a little bit more time to complete the STANDBY IMMED command.
The ATA spec seems to suggest it may take up to 30s. This seems a little bit
long. To quote Theo: "by that time some drives have probably parked their own
heads in boredom". But it shouldn't hurt unless your drive is seriously
broken. Makes some laptops suspend much better.

ok deraadt@


# 1.86 22-Jul-2010 mlarkin

First part of better pciide resume. Wake the channel by performing two
resets separated by a small delay (a delay intended to offend jsg who will
definitely want to fix it by checking for RDY instead)

Tested on a variety of amd and i386 hardware by myself and deraadt@

ok deraadt


# 1.85 28-Jun-2010 jsing

Move disk_attach() to the end of the attach functions, at which point the
disk is now ready to handle I/O.

ok krw@ dlg@


# 1.84 07-Jun-2010 jsg

partial backout of 1.81 as invoking standby in shutdown
breaks at least socppc and armish


# 1.83 26-May-2010 thib

Reintroduce bufqs. A few changes since it was backed out after some good
comments from dlg@.

No need for a separate bufq.h, keep all of in buf.h; As requested by kittens
and deraadt.

Only sd(4) and wd(4) for now. The rest of the drivers will be converted soon,
also other goodies like heuristics for sd(4) for selecting the bufq type and
the death of disksort() are forthcoming.

Tested on: i386, amd64, sparc64, macppc, loongson and alpha by myself and
phessler.

OK art@, beck@, kettenis@, oga@


# 1.82 24-May-2010 kettenis

Flush cache before suspend.

ok krw@, marco@


# 1.81 23-May-2010 kettenis

Place drive in standby mode before shutdown. Avoids the loud click heard on
many laptops when powering them down.

ok jsg@, oga@, krw@


# 1.80 18-May-2010 dlg

dont let sys/ioctl.h imply that you get the ioctls in dkio.h. this
gets rid of #include <sys/dkio.h> in sys/ioctl.h and adds #include
<sys/dkio.h> to the places that actually want and use the disk
ioctls.

this became an issue when krw@'s X build failed when he was testing
a change to dkio.h.
tested by krw@
help from and ok miod@


# 1.79 23-Apr-2010 jsing

Recycle unused disklabel fields in order to create a disklabel unique
identifier, allowing the disk to be identified without relying on the
device name.

ok deraadt@ krw@ beck@ marco@ todd@


# 1.78 11-Apr-2010 kettenis

Send "STANDBY IMMEDIATE" command to ATA disks upon suspend. For this to work,
pciide(4) needs to formward DVACT_SUSPEND and DVACT_RESUME events to its
children, so do that.

Gets rid of the nasty "click" sound from the disk on many laptops.

ok marco@, jsg@


Revision tags: OPENBSD_4_7_BASE
# 1.77 13-Oct-2009 pirofti

Get rid of devact enum, substitute it with an int and coresponding defines.

This is needed for the addition of further suspend/resume actions.

Okay deraadt@, marco@.


# 1.76 13-Aug-2009 deraadt

Replace the error strings that were being passed around with much simpler
errnos. Note that the error strings are being ignored, since we long ago
decided to not spam the console, and there is no other nice way to use the
errors (without changing the ioctls to pass it back)
The errno is now useful, since we can pass b_error from failing IO up, and
the drive can decide how to use that
ok miod


Revision tags: OPENBSD_4_6_BASE
# 1.75 17-Jun-2009 thib

Revert bufq's. this is inline with the major midlayer reverts that
have been going on. this appears to bring us back to stable state.

lots of testing by oga and ariane and my self.


# 1.74 03-Jun-2009 thib

add a flexible buffer queue (bufq) api, based on the never used
one by tedu@. It doesn't do anything smart yet, it just uses
plain old disksort. we also keep the old method of queueing bufs
since some miods have crazy MD drivers that need some love.

ok beck@, art@
tested by many on many archs.


Revision tags: OPENBSD_4_5_BASE
# 1.73 08-Nov-2008 chl

remove dead stores and newly created unused variables.

Found by LLVM/Clang Static Analyzer.

ok krw@


# 1.72 10-Sep-2008 blambert

Convert timeout_add() calls using multiples of hz to timeout_add_sec()

Really just the low-hanging fruit of (hopefully) forthcoming timeout
conversions.

ok art@, krw@


Revision tags: OPENBSD_4_4_BASE
# 1.71 27-Jun-2008 ray

More removal of clauses 3 and 4 from NetBSD licenses.

OK deraadt@ and millert@


# 1.70 15-Jun-2008 krw

Don't bypass partition bounds check for RAW_PART. We now guarantee that
RAW_PART will always be 0 -> disksize, so the bounds check will always
pass for i/o's to valid addresses. Now the i/o will be properly truncated
if it goes past the end of the device. This prevents various adverse
impacts of issuing i/o's for data past the end of the device.

Repeatedly requested by todd@.

ok weingart@ deraadt@


Revision tags: OPENBSD_4_3_BASE
# 1.69 07-Feb-2008 sthen

enable DMA for 1-sector devices (i.e. most flash devices).
from NetBSD

ok krw thib henning, tested by many


# 1.68 05-Dec-2007 jsg

Switch set features commands to use wdc_exec_command() to allow
them to be properly polled.

Fixes previously problematic machines of form and ckuethe.

ok marco@


# 1.67 26-Nov-2007 jsg

Enable read ahead and write cache enable if supported by
the disk rather than depending on the disk to have it on.

A bunch of people have suggested this should go in.


Revision tags: OPENBSD_4_2_BASE
# 1.66 20-Jun-2007 deraadt

b_cylinder does not need to be set on the callpath down into drivers.
cpu_disklabel can go away, since nothing anymore needs to use it; ok miod


# 1.65 18-Jun-2007 deraadt

avoid modification race in DIOCRLDINFO; ok krw miod


# 1.64 08-Jun-2007 deraadt

all drivers should spoof version 1 labels


# 1.63 07-Jun-2007 ray

strncpy -> strlcpy, plus a little spacing line up.

OK deraadt.


# 1.62 06-Jun-2007 deraadt

now that all partition size/offsets are potentially 64-bit, change the
type of all variables to daddr64_t. this includes the APIs for XXsize()
and XXdump(), all range checks inside bio drivers, internal variables
for disklabel handling, and even uvm's swap offsets. re-read numerous
times by otto, miod, krw, thib to look for errors


# 1.61 05-Jun-2007 deraadt

use six new macros to access & store the 48-bit disklabel fields related
to size. tested on almost all machines, double checked by miod and krw
next comes the type handling surrounding these values


# 1.60 01-Jun-2007 krw

Don't initialize d_partitions[RAW_DISK] just before calling
readdisklabel(), since all readdisklabel()'s do that already.

ok deraadt@


# 1.59 31-May-2007 krw

Kill old, no longer necessary kludge to try fooling readdisklabel()
into using DOS geometry by calling it twice. And don't ignore the
drive state if readdisklabel() returns a non-NULL value.

ok weingart@ grange@


# 1.58 28-Apr-2007 krw

Fix comments above DISKMINOR uses. DISKMINOR provides minor not major
number (wd.c), and the uses are to find the minimum minor to be
detached.


# 1.57 27-Apr-2007 krw

Nuke WDUNIT/WDPART/etc. defines and just use standard DISKUNIT/DISKPART/etc.
ones. No change to wd.o.

ok deraadt@ marco@


# 1.56 26-Apr-2007 krw

Eliminate CPU_BIOS from userland and wd(4) by always using the BIOS
geometry in the disklabel when there is a BIOS geometry to provide.
This removes the option to set a disklabel to 'BIOS' geometry via the
'g b' command in the editor.

Makes reported geometry more consistant and moves MD code to MD land
where it should be.

Doc help from jmc@, Feedback from millert@, marco@, weingart@,
kettenis@.

ok deraadt@


# 1.55 27-Mar-2007 deraadt

OK, I understand why jsg wanted the disk_unbusy() in wdretry(), and he
was right. There is another path that ends up avoiding the disk_unbusy
call I inserted.... tested by robert too


# 1.54 25-Mar-2007 deraadt

wdc retries (including after suspends) caused a disk to remain busy when
it should not; ok dlg
problem spotted using zaurus soft-drive-led diff which is pending
i think i convinced jsg now that this fix is better than putting it
in wdrestart


Revision tags: OPENBSD_4_1_BASE
# 1.53 15-Feb-2007 krw

Don't print the error strings returned by readdisklabel(). If you
need the debug info uncomment the printf's you need. Crude but
effective way to suppress 'no disklabel' errors that pop up at the
most innconvenient times to frighten users. More elegant method,
DPRINTF-like constructs or something, later.

"Yay!" marco@ ok deraadt@


# 1.52 06-Feb-2007 krw

De-register. No object code change.

From Bret Lambert.


# 1.51 17-Oct-2006 krw

For non-CPU_BIOS architectures calculate the number of cylinders on
a disk rather than accepting the cylinder count provided by the disk
or controller. This cylinder count will be '16383' for any disk >8.4G
according to the ATA spec. CPU_BIOS on i386/amd64 has magic to deal
with this, but other archs do not need to be restricted by the needs
of PC BIOS.

Fixes the default MBR OpenBSD partition size and disklabel on non-CPU_BIOS
archtitectures.

No change to behaviour on i386/amd64 machines.

Noted by Stuart Henderson on his Thecus.

Testing by various, including 'old i386 machines' nick@

ok tom@ pedro@ weingart@ deraadt@


# 1.50 04-Oct-2006 krw

Zap bad144 bad sector info in disklabels. Sparsely used if at all for
a few obsolete and easily replaced disk models. And didn't work anyway
according to Bob, who has tried.

ok beck@


Revision tags: OPENBSD_4_0_BASE
# 1.49 21-Aug-2006 krw

Delete #ifdef/#ifndef __OpenBSD__ chunks, leaving just the code we
use. Easier to read and eliminates a potentially confusing unused call
to wdgetdisklabel(). No change to object files.

ok brad@ dlg@ grange@ deraadt@ marco@


# 1.48 12-Aug-2006 krw

Setting d_secsize to DEV_BSIZE (or 1 << DEV_BSHIFT, or 512) and then
setting RAW_PART's p_size to d_secperunit * (d_secsize / DEV_BSIZE) is
a waste of a few ops. And p_size should be in sectors anyway.

Just set RAW_PART's p_size to d_secperunit to make usage consistant
across the tree.

Should be a no-op.


# 1.47 05-Mar-2006 krw

Remove more NRND NetBSDisms.

ok marco@


Revision tags: OPENBSD_3_9_BASE
# 1.46 21-Jan-2006 miod

Invoke disk_detach() and related cleanup work in detach(), rather than
zeroref() - just to be on the safe side, should we mess up our ref count.


# 1.45 09-Nov-2005 uwe

Don't fall back to fake geometry if ata_get_params() fails.
"looks sane" grange@; tested by niall@ krw@ jmc@ jolan@


Revision tags: OPENBSD_3_8_BASE
# 1.44 01-Sep-2005 uwe

On zaurus, don't fall back to fake geometry if ata_get_params() fails,
which can happen if the integrated microdrive's DRQ is not clear. This
fixes the drive geometry in the default disklabel.
Help from weingart@ and grange@; "mention `grange and i will fix this
better after 3.8 or we will be flailed'" deraadt@


# 1.43 27-Aug-2005 jsg

Use the ATA security FREEZE LOCK command on disks before
they are presented to the rest of the system to stop
a password being set that could deny access to the disk.
ok grange@ deraadt@


# 1.42 15-May-2005 grange

Use FLUSHCACHE EXT instead of FLUSHCACHE for LBA48 operations.
First noticed by christos@netbsd (iirc) a long time ago.

Tested by canacar@ and krw@.


Revision tags: OPENBSD_3_6_BASE OPENBSD_3_7_BASE
# 1.41 06-Jul-2004 aaron

Replace magic number with a more descriptive #define. This magic number was
particularly vile because it looks like a typo (i.e., looks like one "f" is
missing), but it's really not. I happened to notice this myself, but the fix
is inspired by NetBSD in the interest of keeping the code similar.

deraadt@ ok


Revision tags: OPENBSD_3_5_BASE SMP_SYNC_A SMP_SYNC_B
# 1.40 03-Mar-2004 tedu

remove bufq support until i figure out just what's going wrong. ok deraadt@


# 1.39 15-Feb-2004 tedu

new arg to disk_unbusy, to record separate read/write statistics.
looked at by various, testing henning@ mcbride@ dan weeks
mostly from netbsd via Pedro Martelletto <pbastos@rdc.puc-rio.br>


# 1.38 02-Feb-2004 tedu

as grange pointed out, BUFQ_ALLOC might fail


# 1.37 21-Jan-2004 tedu

dynamic bufq support. basically a nullop. ok grange@


# 1.36 16-Jan-2004 grange

Do not print CHS info for LBA drives, it's useless for drives with
capacity > 8GB. Total number of cylinders is enough.

ok deraadt@


# 1.35 15-Jan-2004 grange

ansi and some spaces; no f/c


# 1.34 21-Oct-2003 jmc

typos from Tom Cosgrove;


# 1.33 28-Sep-2003 grange

kill trailing spaces, no functional changes


Revision tags: OPENBSD_3_4_BASE
# 1.32 20-Jul-2003 grange

Use LBA48 only when accessing blocks after 137Gb limit or using sectors
count more than 255 to reduce registers writting overhead. Gives me
about 5% perfomance increase in a dumb synthetic test.

Some input and ok from costa@ a long time ago.


# 1.31 25-Jun-2003 tedu

implement new means of manipulating buf queues, bufq.
accessed with BUFQ macros, bufq structs support extensible, potentially
changable algorithms and queue formats. the current default scheme
should support nice priority based queuing, but is missing some vfs_bio.c
support.
only on wd.c for now, other drivers are easy converts.
as a side bonus, this makes the driver code look cleaner.
idea for the name comes from netbsd, but this scheme is incompatible.
thanks to various people for testing.
ok grange@


# 1.30 20-May-2003 tedu

change spaces to tabs so i can mail grange diffs :)
ok grange@


Revision tags: UBC_SYNC_A
# 1.29 09-Apr-2003 ho

sprintf->snprintf. miod@, grange@ ok.


# 1.28 06-Apr-2003 krw

Some easy strcpy elimination.

a) strcpy -> strncpy for copies into disklabel fields d_packname and
d_typename.

b) '16' -> 'sizeof d_packname' or 'sizeof d_typename' in existing
strncpy's.

ok millert@


Revision tags: OPENBSD_3_3_BASE
# 1.27 21-Feb-2003 grange

Fix Manuel Bouyer's copyright, from NetBSD
ok deraadt@


# 1.26 19-Dec-2002 grange

Protect WDCDEBUG_PRINT macro with do {} while (0)
ok costa@


Revision tags: OPENBSD_3_2_BASE UBC_SYNC_B
# 1.25 24-May-2002 art

protect biodone with splbio.


# 1.24 03-May-2002 gluk

LBA48 support.
Adopted from NetBSD by Alexander Yurchenko <grange@openbsd.ru>.

costa@ ok.


Revision tags: OPENBSD_3_1_BASE
# 1.23 14-Mar-2002 millert

Final __P removal plus some cosmetic fixups


# 1.22 14-Mar-2002 millert

First round of __P removal in sys


# 1.21 07-Jan-2002 mickey

pass the proc* down the ioctl chain to controller; mjacob@ csapuntz@ ok


Revision tags: UBC_BASE
# 1.20 06-Nov-2001 miod

branches: 1.20.2;
Replace inclusion of <vm/foo.h> with the correct <uvm/bar.h> when necessary.
(Look ma, I might have broken the tree)


Revision tags: OPENBSD_3_0_BASE
# 1.19 06-Aug-2001 miod

Remove legacy __BDEVSW_DUMP_OLD_TYPE code.


# 1.18 31-Jul-2001 csapuntz

Go down only one Ultra DMA mode at a time. Thanks to Matt Evans for
suggesting this fix.

Update atapiscsi Ultra DMA downgrade logic.


# 1.17 12-Jul-2001 csapuntz

extra wddone caused biodone panics occasionally


# 1.16 30-Apr-2001 csapuntz

Move ioctl stuff to low-level driver


Revision tags: OPENBSD_2_9_BASE
# 1.15 02-Apr-2001 csapuntz

ATA_POLL should have been AT_POLL

Flushcache on shutdown used to pass both AT_POLL and AT_WAIT. The two flags
do not make sense together.

Should fix panic when shutting down from ddb


# 1.14 25-Jan-2001 todd

spelling


Revision tags: OPENBSD_2_8_BASE
# 1.13 30-Jun-2000 art

Convert to new timeout API.


Revision tags: OPENBSD_2_7_BASE
# 1.12 10-Apr-2000 csapuntz

Added support for wd detach (merge from NetBSD). Support for
SCSI/ATAPI detach is not here yet.

Minor cleanup of wdc. Downgrade to UDMA mode 1 before going further.
Want to stay in UDMA modes because they're more error-resilient due to
a CRC.

Got rid of some of the ridiculous amount of softc sharing going on.
Hopefully, this will make the life of whoever goes in and fixes the
ref-counting to be correct easier.


# 1.11 07-Apr-2000 niklas

remove statics


Revision tags: SMP_BASE kame_19991208
# 1.10 17-Nov-1999 csapuntz

branches: 1.10.2;


New interface to ATA registers that goes through the lower-level device
driver (pciide, wdc, etc.)

Remove #define WDCDEBUG from top of files

More fixes to ATAPISCSI logic:

Not all devices transition correctly between phases. Devices are supposed
to keep BSY high until they've set the registers to the next sensible
state. Some devices drop BSY and leave the registers in an old or
nonsense state. Our polling code is extremely sensitive to this
(though an early itnerrupt could also observe this). So, if the device is
in an unexpected state, the new polling code waits for a while in the hope
that it enters a better state.

This seems to fix many of the problems reported.

Also, there was a horrible bug which would cause sense to fail on a ATAPI
command that sent data to the drive


Revision tags: OPENBSD_2_6_BASE
# 1.9 09-Oct-1999 csapuntz

Fix printout at attach time. Print PIO/DMA mode information after having
printed line with the name of hard disk.


# 1.8 09-Oct-1999 csapuntz

wdc layer work:
Separate wdc_probe_caps into wdc_probe_caps and wdc_print_caps for more
flexibility in printing capability information.

Get rid of wdc_final_attach.

Include name of device (e.g. cd0), if possible, on errors.

atapiscsi layer work:
Put a pointer to the SCSI device into ata_drive_datas' drv_softc field

Simplify, simplify, simplify. Got rid of a bunch of fields in atapiscsi_softc

Delay printing capabilities until we know the real device name (e.g. st0)


# 1.7 15-Aug-1999 millert

o Correctly set type and typename in driver-generated label
Should really use viscopy() when copying atap_model -> d_packname.
When wd's become sd's this will be trivial (viscopy() lives in sd.c)


# 1.6 10-Aug-1999 deraadt

support DIOCRLDINFO


# 1.5 05-Aug-1999 niklas

Make IDE compile on alpha again:
use b* instead of mem* functions, adhere to NISADMA for DMA support.


# 1.4 22-Jul-1999 deraadt

shorten dmesg output


# 1.3 22-Jul-1999 csapuntz

Fix bad144intern.


# 1.2 20-Jul-1999 csapuntz

126 -> NBT_BAD

Fix disklabel/bad sectors stuff in wd


# 1.1 18-Jul-1999 csapuntz

Import of NetBSD ATA/IDE stuff.

Introduction of home-grown

To enable this stuff in your configuration, look at the NEWATA conf file
and go through

dev/isa/files.isa
dev/pci/files.pci
conf/files
arch/i386/conf/files.i386

and follow the instructions on commenting/uncommenting stuff


# 1.130 23-Oct-2022 krw

bzero(disklabel) or memset(disklabel,0) should be enough for
anyone. No need to also set d_flags to 0.


Revision tags: OPENBSD_7_2_BASE
# 1.129 06-Apr-2022 naddy

constify struct cfattach


Revision tags: OPENBSD_7_1_BASE
# 1.128 09-Jan-2022 jsg

spelling
feedback and ok tb@ jmc@ ok ratchov@


Revision tags: OPENBSD_6_7_BASE OPENBSD_6_8_BASE OPENBSD_6_9_BASE OPENBSD_7_0_BASE
# 1.127 23-Jan-2020 tedu

we've been using LBA48 for < 137GB drives for 17 years, so perhaps
we can remove the XXX now.


Revision tags: OPENBSD_6_6_BASE
# 1.126 07-Jul-2019 kn

Use timeout_add_msec(9)

RECOVERYTIME is twice per second, so sleep for 500 milliseconds
and reflect the unit to be clear.

OK patrick


Revision tags: OPENBSD_6_3_BASE OPENBSD_6_4_BASE OPENBSD_6_5_BASE
# 1.125 30-Dec-2017 guenther

Don't pull in <sys/file.h> just to get fcntl.h

ok deraadt@ krw@


# 1.124 14-Dec-2017 deraadt

Repair flushcache/standby operations to not convert AT_WAIT operations
to AT_POLL, which some chip driver underneath cannot handle.
tested by bluhm also


# 1.123 24-Oct-2017 jsg

remove defines for ioctls the kernel doesn't recognise
ok mpi@


Revision tags: OPENBSD_6_2_BASE
# 1.122 29-May-2017 sf

Add an ioctl to tell storage devices to flush their internal caches

Currently implemented for wd and sd.
Initially ported from netbsd by pedro@

ok deraadt@


# 1.121 04-May-2017 deraadt

Also pass the blk offset to disk_unbusy(), so that it can pass it to
the random subsystem as entropy. This value is pretty much unknown,
and anyways our entropy input ring does not saturate from knowns.
ok mikeb djm


Revision tags: OPENBSD_5_9_BASE OPENBSD_6_0_BASE OPENBSD_6_1_BASE
# 1.120 20-Jan-2016 stefan

Set segflg member of uio struct with symbolic enum value
instead of using magic numbers. No binary change.

Diff from Martin Natano.

ok deraadt@


# 1.119 26-Aug-2015 deraadt

trivial size to free()


# 1.118 17-Aug-2015 krw

The 80's called and want their interface back. Remove last fragments of
ST-506 support. Remove redundant 'precomp' variables/fields and rename
remaining few to 'features' to be consistant with the rest of the code.

Consensus that no ST-506 disks (requiring ISA cards, <120MB, usually
rife with bad sectors that can't be worked around) are still running
OpenBSD.


Revision tags: OPENBSD_5_7_BASE OPENBSD_5_8_BASE
# 1.117 14-Sep-2014 jsg

remove uneeded proc.h includes
ok mpi@ kspillner@


Revision tags: OPENBSD_5_6_BASE
# 1.116 12-Jul-2014 tedu

add a size argument to free. will be used soon, but for now default to 0.
after discussions with beck deraadt kettenis.


# 1.115 10-Jul-2014 deraadt

remove most of the boolean_t infection outside uvm/ddb/pmap; ok jsg


Revision tags: OPENBSD_5_5_BASE
# 1.114 14-Oct-2013 krw

Use DL_SECTOBLK() instead of handrolling same calculation to get
wdsize() return value.


# 1.113 14-Oct-2013 krw

XXsize() returns daddr_t, so calculate the return value in a daddr_t
variable and not (usually) an int.

ok miod@


# 1.112 27-Sep-2013 krw

Try harder to ensure wddump() doesn't decrement the count
of sectors to write by one value and increment the address
the next i/o will start at by a different amount.


# 1.111 15-Sep-2013 krw

Make it clearer that sc_wdc_bio.blkno is actuall a disk hardware
address by using the DL_BLKTOSEC() and DL_SECTOBLK() defines rather
than handrolling the equivalent. Fixes current addition of a 512-byte
block value (b_blkno) to a disk hardware address (DL_GETPOFFSET()).

Tested on real wd(4) hardware by guenther@.


Revision tags: OPENBSD_5_4_BASE
# 1.110 11-Jun-2013 deraadt

final removal of daddr64_t. daddr_t has been 64 bit for a long enough
test period; i think 3 years ago the last bugs fell out.
ok otto beck others


Revision tags: OPENBSD_5_3_BASE
# 1.109 08-Oct-2012 deraadt

Revamp the sequences for suspend/hibernate -> resume so that the code
paths are reflexive. It is now possible to fail part-way through a
suspend sequence, and recover along the resume code path.
Split DVACT_SUSPEND by adding a new DVACT_POWERDOWN method is used
after hibernate (and suspend too) to finish the job. Some drivers
must be converted at the same time to use this instead of shutdown hooks
(the others will follow at a later time)
ok kettenis mlarkin


Revision tags: OPENBSD_5_0_BASE OPENBSD_5_1_BASE OPENBSD_5_2_BASE
# 1.108 06-Jul-2011 matthew

Eliminate redundant buf validation checks in xxstrategy() methods now
that they're implemented consistently in bounds_check_with_label().

Also, per krw's request, change bounds_check_with_label() to return 0
if the checks succeed, and change the drivers to test == -1 instead of
<= 0. (Man page update to follow; intentionally omitting
arch/vax/mba/hp.c from this commit because it doesn't even build
currently and miod@ promises to kill it soon.)

ok krw@


# 1.107 30-Jun-2011 matthew

Refactor some common open/close/detach disk driver code into
subr_disk.c. For now just the MI disk drivers.

ok deraadt@, krw@; jsing@ liked the approach too


# 1.106 20-Jun-2011 matthew

Call bufq_drain() in wddetach() instead of reimplementing the same
logic.


# 1.105 19-Jun-2011 deraadt

Use disk_lock_nointr() in the xxclose() routines so that they cannot
be interrupted.
ok matthew


# 1.104 19-Jun-2011 deraadt

Use disk_lock/disk_unlock directly and in the same way in these drivers,
rather than using various wrappings. Convert vnd to using the sc_dk
rwlock instead of using one of its own.
ok matthew


# 1.103 05-Jun-2011 matthew

Drop kernel support for the useless DIOCWLABEL ioctl and prune a lot
of silly flag twiddling code in various disk drivers.

ok deraadt@, miod@

N.B., users will need a -current disklabel(8) to be able to write new
disklabels to disk now.


# 1.102 03-Jun-2011 matthew

Get rid of the wlabel argument to bounds_check_with_label(). It's
never done anything in OpenBSD and just clutters disk drivers with
silly flag handling.

More cleanup to follow.

ok deraadt@, millert@; no objections krw@


# 1.101 31-May-2011 matthew

Change a few of the more common disk drivers (sd, cd, wd, rd, and vnd)
to return EBUSY if the user tries to modify an open partition's offset
or size. Only sadness can result if a user tries this, and rejecting
it prevents a race between sdstart() and sdstrategy().

Curiously, there was already code in the kernel and in disklabel(8) to
detect/handle this, but it was effectively disabled because the disk
drivers always used something like "/* sc->sc_dk.dk_openmask */ 0",
and this commented out code has existed since even r1.1 in NetBSD.

I had no problems building a release and messing around with
disklabel(8) for a bit with this diff. Canarying the more common MI
disk drivers until we gain confidence that there aren't any
regressions, then we can switch the remaining drivers.

"I am surprised you got me convinced that this stuff is safe" deraadt@
ok krw@


# 1.100 18-Apr-2011 deraadt

keep an eye out for wdc registers returning 0xff. if they return 0xff,
this probably means the controller is dead -- as in, a hot plug eject
event. in that case, mark the driver dead and try harder to return -1
in the interrupt handler.
tested by many
ok dlg, miod did not object


# 1.99 15-Apr-2011 miod

Change wdc_reset_channel() to take a `no wait' argument. Pass in turn this
argument to wdcreset(), to have it skip waiting until active channels see
their BUSY bit clear in the status register.

Use this feature in the resume path, during the first reset operation. The
first reset is supposed to only wake up the controller, and the disks don't
come back until the second reset is issued, therefore waiting for them to
report themselves as ready after the first reset, but before the second, is
moot - and as a matter of fact some controllers, such as the AMD 754 and
clones/offspring (e.g. Geode) keep the BUSY bit asserted after the first
reset.

Last, but not least, make sure wd@ata invokes wd_get_params() again before
returning from the resume code, as we will still be using polled transfers
for a short while.

This causes the Lemote Yeelong to resume within less than one second, instead
of the lousy 30 seconds wait between the two resets; and the wd_get_params()
voodoo prevents it from getting spurious ide interrupts afterwards.

wd_get_params() magic from dlg; rest of the work by yours truly after enough
prodding by dlg@ and pirofti@, among others. ok deraadt@ dlg@


# 1.98 05-Apr-2011 deraadt

wd_hibernate_io() is a standalone disk io writer which tries to not
damage kernel memory very much while doing the job. It is not very
pretty yet, but improving it will need some MI work.
ok mlarkin jsg dlg


Revision tags: OPENBSD_4_9_BASE
# 1.97 31-Dec-2010 kettenis

Reintroduce the code that puts the drive into standby in shutdown, but
only do this if we're going to power down the machine.

"My god, it is a 3 line diff. Commit it before it gets bigger!" deraadt@


# 1.96 30-Dec-2010 kettenis

The NVIDIA MCP89 SATA controller found on the 2010 Apple 13" MacBook Pro
can't address block 0xffffffff in LBA mode, at least in combination with
the Hitachi HTS545032B9SA02 and HTS545050B9SA02 drives that Apple ships.
So use LBA48 mode from that block onwards.

ok marco@, deraadt@, miod@


# 1.95 22-Sep-2010 matthew

All users of physio(9) now pass NULL as the buf pointer argument, so
no point in keeping it around.

"i like this" thib@ (a while back); ok krw@ and oga@; reminder to
update the man page and tweaks jmc@


# 1.94 20-Sep-2010 deraadt

DVACT_ACTIVATE and DVACT_DEACTIVATE do nothing here; remove
the false comments.


# 1.93 08-Sep-2010 jsing

Introduce a disk_lookup() function which calls device_lookup(), before
verifying that the resulting device is present on the disklist. This
avoids a race whereby the disk driver can be accessed as soon as the
softc has been allocated, but before the disk has completed
initialisation and has called disk_attach() (up until this point
dk_label is still a null pointer).

Cut cd(4), sd(4) and wd(4) across to disk_lookup(). All callers of
disk_attach() need to be tested and cut over in due course.

ok deraadt@ krw@


# 1.92 08-Sep-2010 jsing

Store a struct device pointer within struct disk and populate this when
disk_attach() is called by the device driver. We will be building on
this shortly.

ok deraadt@ krw@


# 1.91 01-Sep-2010 dlg

make struct bufq a member of the softc for devices that use it,
rather than it being a pointer to something that needs to be allocated
at attach. since all these devices need a bufq to operate, it makes
sense to have it allocated as part of the softc and get bufq_init
to just initialise all its fields. it also gets rid of the possibility
that you wont be able to allocate the bufq struct during attach,
which is something you dont want to happen.

secondly, it consistently implements a split between wrapper functions
and the per discipline implementation of the bufq handlers. it
consistently does the locking in the wrappers rather than doing
half in the wrappers and the other half in the implementations.

it also consistently handles the outstanding bufq bq pointer in the
wrappers.

this hides most of the implementation inside kern_bufq.c. the only
stuff left in buf.h is for the bits each implementation needs to
put inside struct buf.

tested by thib@ krw@ and me
ok thib@ matthew@
no objection from krw@


# 1.90 31-Aug-2010 deraadt

sort DVACT_ actions in order of use


# 1.89 28-Aug-2010 matthew

Garbage collect struct dkdriver.

ok miod@; "please go ahead" jsing@


Revision tags: OPENBSD_4_8_BASE
# 1.88 23-Jul-2010 jsg

Manuel Bouyer rescinded clauses 3 and 4 of his license text.


# 1.87 22-Jul-2010 kettenis

Give the drive a little bit more time to complete the STANDBY IMMED command.
The ATA spec seems to suggest it may take up to 30s. This seems a little bit
long. To quote Theo: "by that time some drives have probably parked their own
heads in boredom". But it shouldn't hurt unless your drive is seriously
broken. Makes some laptops suspend much better.

ok deraadt@


# 1.86 22-Jul-2010 mlarkin

First part of better pciide resume. Wake the channel by performing two
resets separated by a small delay (a delay intended to offend jsg who will
definitely want to fix it by checking for RDY instead)

Tested on a variety of amd and i386 hardware by myself and deraadt@

ok deraadt


# 1.85 28-Jun-2010 jsing

Move disk_attach() to the end of the attach functions, at which point the
disk is now ready to handle I/O.

ok krw@ dlg@


# 1.84 07-Jun-2010 jsg

partial backout of 1.81 as invoking standby in shutdown
breaks at least socppc and armish


# 1.83 26-May-2010 thib

Reintroduce bufqs. A few changes since it was backed out after some good
comments from dlg@.

No need for a separate bufq.h, keep all of in buf.h; As requested by kittens
and deraadt.

Only sd(4) and wd(4) for now. The rest of the drivers will be converted soon,
also other goodies like heuristics for sd(4) for selecting the bufq type and
the death of disksort() are forthcoming.

Tested on: i386, amd64, sparc64, macppc, loongson and alpha by myself and
phessler.

OK art@, beck@, kettenis@, oga@


# 1.82 24-May-2010 kettenis

Flush cache before suspend.

ok krw@, marco@


# 1.81 23-May-2010 kettenis

Place drive in standby mode before shutdown. Avoids the loud click heard on
many laptops when powering them down.

ok jsg@, oga@, krw@


# 1.80 18-May-2010 dlg

dont let sys/ioctl.h imply that you get the ioctls in dkio.h. this
gets rid of #include <sys/dkio.h> in sys/ioctl.h and adds #include
<sys/dkio.h> to the places that actually want and use the disk
ioctls.

this became an issue when krw@'s X build failed when he was testing
a change to dkio.h.
tested by krw@
help from and ok miod@


# 1.79 23-Apr-2010 jsing

Recycle unused disklabel fields in order to create a disklabel unique
identifier, allowing the disk to be identified without relying on the
device name.

ok deraadt@ krw@ beck@ marco@ todd@


# 1.78 11-Apr-2010 kettenis

Send "STANDBY IMMEDIATE" command to ATA disks upon suspend. For this to work,
pciide(4) needs to formward DVACT_SUSPEND and DVACT_RESUME events to its
children, so do that.

Gets rid of the nasty "click" sound from the disk on many laptops.

ok marco@, jsg@


Revision tags: OPENBSD_4_7_BASE
# 1.77 13-Oct-2009 pirofti

Get rid of devact enum, substitute it with an int and coresponding defines.

This is needed for the addition of further suspend/resume actions.

Okay deraadt@, marco@.


# 1.76 13-Aug-2009 deraadt

Replace the error strings that were being passed around with much simpler
errnos. Note that the error strings are being ignored, since we long ago
decided to not spam the console, and there is no other nice way to use the
errors (without changing the ioctls to pass it back)
The errno is now useful, since we can pass b_error from failing IO up, and
the drive can decide how to use that
ok miod


Revision tags: OPENBSD_4_6_BASE
# 1.75 17-Jun-2009 thib

Revert bufq's. this is inline with the major midlayer reverts that
have been going on. this appears to bring us back to stable state.

lots of testing by oga and ariane and my self.


# 1.74 03-Jun-2009 thib

add a flexible buffer queue (bufq) api, based on the never used
one by tedu@. It doesn't do anything smart yet, it just uses
plain old disksort. we also keep the old method of queueing bufs
since some miods have crazy MD drivers that need some love.

ok beck@, art@
tested by many on many archs.


Revision tags: OPENBSD_4_5_BASE
# 1.73 08-Nov-2008 chl

remove dead stores and newly created unused variables.

Found by LLVM/Clang Static Analyzer.

ok krw@


# 1.72 10-Sep-2008 blambert

Convert timeout_add() calls using multiples of hz to timeout_add_sec()

Really just the low-hanging fruit of (hopefully) forthcoming timeout
conversions.

ok art@, krw@


Revision tags: OPENBSD_4_4_BASE
# 1.71 27-Jun-2008 ray

More removal of clauses 3 and 4 from NetBSD licenses.

OK deraadt@ and millert@


# 1.70 15-Jun-2008 krw

Don't bypass partition bounds check for RAW_PART. We now guarantee that
RAW_PART will always be 0 -> disksize, so the bounds check will always
pass for i/o's to valid addresses. Now the i/o will be properly truncated
if it goes past the end of the device. This prevents various adverse
impacts of issuing i/o's for data past the end of the device.

Repeatedly requested by todd@.

ok weingart@ deraadt@


Revision tags: OPENBSD_4_3_BASE
# 1.69 07-Feb-2008 sthen

enable DMA for 1-sector devices (i.e. most flash devices).
from NetBSD

ok krw thib henning, tested by many


# 1.68 05-Dec-2007 jsg

Switch set features commands to use wdc_exec_command() to allow
them to be properly polled.

Fixes previously problematic machines of form and ckuethe.

ok marco@


# 1.67 26-Nov-2007 jsg

Enable read ahead and write cache enable if supported by
the disk rather than depending on the disk to have it on.

A bunch of people have suggested this should go in.


Revision tags: OPENBSD_4_2_BASE
# 1.66 20-Jun-2007 deraadt

b_cylinder does not need to be set on the callpath down into drivers.
cpu_disklabel can go away, since nothing anymore needs to use it; ok miod


# 1.65 18-Jun-2007 deraadt

avoid modification race in DIOCRLDINFO; ok krw miod


# 1.64 08-Jun-2007 deraadt

all drivers should spoof version 1 labels


# 1.63 07-Jun-2007 ray

strncpy -> strlcpy, plus a little spacing line up.

OK deraadt.


# 1.62 06-Jun-2007 deraadt

now that all partition size/offsets are potentially 64-bit, change the
type of all variables to daddr64_t. this includes the APIs for XXsize()
and XXdump(), all range checks inside bio drivers, internal variables
for disklabel handling, and even uvm's swap offsets. re-read numerous
times by otto, miod, krw, thib to look for errors


# 1.61 05-Jun-2007 deraadt

use six new macros to access & store the 48-bit disklabel fields related
to size. tested on almost all machines, double checked by miod and krw
next comes the type handling surrounding these values


# 1.60 01-Jun-2007 krw

Don't initialize d_partitions[RAW_DISK] just before calling
readdisklabel(), since all readdisklabel()'s do that already.

ok deraadt@


# 1.59 31-May-2007 krw

Kill old, no longer necessary kludge to try fooling readdisklabel()
into using DOS geometry by calling it twice. And don't ignore the
drive state if readdisklabel() returns a non-NULL value.

ok weingart@ grange@


# 1.58 28-Apr-2007 krw

Fix comments above DISKMINOR uses. DISKMINOR provides minor not major
number (wd.c), and the uses are to find the minimum minor to be
detached.


# 1.57 27-Apr-2007 krw

Nuke WDUNIT/WDPART/etc. defines and just use standard DISKUNIT/DISKPART/etc.
ones. No change to wd.o.

ok deraadt@ marco@


# 1.56 26-Apr-2007 krw

Eliminate CPU_BIOS from userland and wd(4) by always using the BIOS
geometry in the disklabel when there is a BIOS geometry to provide.
This removes the option to set a disklabel to 'BIOS' geometry via the
'g b' command in the editor.

Makes reported geometry more consistant and moves MD code to MD land
where it should be.

Doc help from jmc@, Feedback from millert@, marco@, weingart@,
kettenis@.

ok deraadt@


# 1.55 27-Mar-2007 deraadt

OK, I understand why jsg wanted the disk_unbusy() in wdretry(), and he
was right. There is another path that ends up avoiding the disk_unbusy
call I inserted.... tested by robert too


# 1.54 25-Mar-2007 deraadt

wdc retries (including after suspends) caused a disk to remain busy when
it should not; ok dlg
problem spotted using zaurus soft-drive-led diff which is pending
i think i convinced jsg now that this fix is better than putting it
in wdrestart


Revision tags: OPENBSD_4_1_BASE
# 1.53 15-Feb-2007 krw

Don't print the error strings returned by readdisklabel(). If you
need the debug info uncomment the printf's you need. Crude but
effective way to suppress 'no disklabel' errors that pop up at the
most innconvenient times to frighten users. More elegant method,
DPRINTF-like constructs or something, later.

"Yay!" marco@ ok deraadt@


# 1.52 06-Feb-2007 krw

De-register. No object code change.

From Bret Lambert.


# 1.51 17-Oct-2006 krw

For non-CPU_BIOS architectures calculate the number of cylinders on
a disk rather than accepting the cylinder count provided by the disk
or controller. This cylinder count will be '16383' for any disk >8.4G
according to the ATA spec. CPU_BIOS on i386/amd64 has magic to deal
with this, but other archs do not need to be restricted by the needs
of PC BIOS.

Fixes the default MBR OpenBSD partition size and disklabel on non-CPU_BIOS
archtitectures.

No change to behaviour on i386/amd64 machines.

Noted by Stuart Henderson on his Thecus.

Testing by various, including 'old i386 machines' nick@

ok tom@ pedro@ weingart@ deraadt@


# 1.50 04-Oct-2006 krw

Zap bad144 bad sector info in disklabels. Sparsely used if at all for
a few obsolete and easily replaced disk models. And didn't work anyway
according to Bob, who has tried.

ok beck@


Revision tags: OPENBSD_4_0_BASE
# 1.49 21-Aug-2006 krw

Delete #ifdef/#ifndef __OpenBSD__ chunks, leaving just the code we
use. Easier to read and eliminates a potentially confusing unused call
to wdgetdisklabel(). No change to object files.

ok brad@ dlg@ grange@ deraadt@ marco@


# 1.48 12-Aug-2006 krw

Setting d_secsize to DEV_BSIZE (or 1 << DEV_BSHIFT, or 512) and then
setting RAW_PART's p_size to d_secperunit * (d_secsize / DEV_BSIZE) is
a waste of a few ops. And p_size should be in sectors anyway.

Just set RAW_PART's p_size to d_secperunit to make usage consistant
across the tree.

Should be a no-op.


# 1.47 05-Mar-2006 krw

Remove more NRND NetBSDisms.

ok marco@


Revision tags: OPENBSD_3_9_BASE
# 1.46 21-Jan-2006 miod

Invoke disk_detach() and related cleanup work in detach(), rather than
zeroref() - just to be on the safe side, should we mess up our ref count.


# 1.45 09-Nov-2005 uwe

Don't fall back to fake geometry if ata_get_params() fails.
"looks sane" grange@; tested by niall@ krw@ jmc@ jolan@


Revision tags: OPENBSD_3_8_BASE
# 1.44 01-Sep-2005 uwe

On zaurus, don't fall back to fake geometry if ata_get_params() fails,
which can happen if the integrated microdrive's DRQ is not clear. This
fixes the drive geometry in the default disklabel.
Help from weingart@ and grange@; "mention `grange and i will fix this
better after 3.8 or we will be flailed'" deraadt@


# 1.43 27-Aug-2005 jsg

Use the ATA security FREEZE LOCK command on disks before
they are presented to the rest of the system to stop
a password being set that could deny access to the disk.
ok grange@ deraadt@


# 1.42 15-May-2005 grange

Use FLUSHCACHE EXT instead of FLUSHCACHE for LBA48 operations.
First noticed by christos@netbsd (iirc) a long time ago.

Tested by canacar@ and krw@.


Revision tags: OPENBSD_3_6_BASE OPENBSD_3_7_BASE
# 1.41 06-Jul-2004 aaron

Replace magic number with a more descriptive #define. This magic number was
particularly vile because it looks like a typo (i.e., looks like one "f" is
missing), but it's really not. I happened to notice this myself, but the fix
is inspired by NetBSD in the interest of keeping the code similar.

deraadt@ ok


Revision tags: OPENBSD_3_5_BASE SMP_SYNC_A SMP_SYNC_B
# 1.40 03-Mar-2004 tedu

remove bufq support until i figure out just what's going wrong. ok deraadt@


# 1.39 15-Feb-2004 tedu

new arg to disk_unbusy, to record separate read/write statistics.
looked at by various, testing henning@ mcbride@ dan weeks
mostly from netbsd via Pedro Martelletto <pbastos@rdc.puc-rio.br>


# 1.38 02-Feb-2004 tedu

as grange pointed out, BUFQ_ALLOC might fail


# 1.37 21-Jan-2004 tedu

dynamic bufq support. basically a nullop. ok grange@


# 1.36 16-Jan-2004 grange

Do not print CHS info for LBA drives, it's useless for drives with
capacity > 8GB. Total number of cylinders is enough.

ok deraadt@


# 1.35 15-Jan-2004 grange

ansi and some spaces; no f/c


# 1.34 21-Oct-2003 jmc

typos from Tom Cosgrove;


# 1.33 28-Sep-2003 grange

kill trailing spaces, no functional changes


Revision tags: OPENBSD_3_4_BASE
# 1.32 20-Jul-2003 grange

Use LBA48 only when accessing blocks after 137Gb limit or using sectors
count more than 255 to reduce registers writting overhead. Gives me
about 5% perfomance increase in a dumb synthetic test.

Some input and ok from costa@ a long time ago.


# 1.31 25-Jun-2003 tedu

implement new means of manipulating buf queues, bufq.
accessed with BUFQ macros, bufq structs support extensible, potentially
changable algorithms and queue formats. the current default scheme
should support nice priority based queuing, but is missing some vfs_bio.c
support.
only on wd.c for now, other drivers are easy converts.
as a side bonus, this makes the driver code look cleaner.
idea for the name comes from netbsd, but this scheme is incompatible.
thanks to various people for testing.
ok grange@


# 1.30 20-May-2003 tedu

change spaces to tabs so i can mail grange diffs :)
ok grange@


Revision tags: UBC_SYNC_A
# 1.29 09-Apr-2003 ho

sprintf->snprintf. miod@, grange@ ok.


# 1.28 06-Apr-2003 krw

Some easy strcpy elimination.

a) strcpy -> strncpy for copies into disklabel fields d_packname and
d_typename.

b) '16' -> 'sizeof d_packname' or 'sizeof d_typename' in existing
strncpy's.

ok millert@


Revision tags: OPENBSD_3_3_BASE
# 1.27 21-Feb-2003 grange

Fix Manuel Bouyer's copyright, from NetBSD
ok deraadt@


# 1.26 19-Dec-2002 grange

Protect WDCDEBUG_PRINT macro with do {} while (0)
ok costa@


Revision tags: OPENBSD_3_2_BASE UBC_SYNC_B
# 1.25 24-May-2002 art

protect biodone with splbio.


# 1.24 03-May-2002 gluk

LBA48 support.
Adopted from NetBSD by Alexander Yurchenko <grange@openbsd.ru>.

costa@ ok.


Revision tags: OPENBSD_3_1_BASE
# 1.23 14-Mar-2002 millert

Final __P removal plus some cosmetic fixups


# 1.22 14-Mar-2002 millert

First round of __P removal in sys


# 1.21 07-Jan-2002 mickey

pass the proc* down the ioctl chain to controller; mjacob@ csapuntz@ ok


Revision tags: UBC_BASE
# 1.20 06-Nov-2001 miod

branches: 1.20.2;
Replace inclusion of <vm/foo.h> with the correct <uvm/bar.h> when necessary.
(Look ma, I might have broken the tree)


Revision tags: OPENBSD_3_0_BASE
# 1.19 06-Aug-2001 miod

Remove legacy __BDEVSW_DUMP_OLD_TYPE code.


# 1.18 31-Jul-2001 csapuntz

Go down only one Ultra DMA mode at a time. Thanks to Matt Evans for
suggesting this fix.

Update atapiscsi Ultra DMA downgrade logic.


# 1.17 12-Jul-2001 csapuntz

extra wddone caused biodone panics occasionally


# 1.16 30-Apr-2001 csapuntz

Move ioctl stuff to low-level driver


Revision tags: OPENBSD_2_9_BASE
# 1.15 02-Apr-2001 csapuntz

ATA_POLL should have been AT_POLL

Flushcache on shutdown used to pass both AT_POLL and AT_WAIT. The two flags
do not make sense together.

Should fix panic when shutting down from ddb


# 1.14 25-Jan-2001 todd

spelling


Revision tags: OPENBSD_2_8_BASE
# 1.13 30-Jun-2000 art

Convert to new timeout API.


Revision tags: OPENBSD_2_7_BASE
# 1.12 10-Apr-2000 csapuntz

Added support for wd detach (merge from NetBSD). Support for
SCSI/ATAPI detach is not here yet.

Minor cleanup of wdc. Downgrade to UDMA mode 1 before going further.
Want to stay in UDMA modes because they're more error-resilient due to
a CRC.

Got rid of some of the ridiculous amount of softc sharing going on.
Hopefully, this will make the life of whoever goes in and fixes the
ref-counting to be correct easier.


# 1.11 07-Apr-2000 niklas

remove statics


Revision tags: SMP_BASE kame_19991208
# 1.10 17-Nov-1999 csapuntz

branches: 1.10.2;


New interface to ATA registers that goes through the lower-level device
driver (pciide, wdc, etc.)

Remove #define WDCDEBUG from top of files

More fixes to ATAPISCSI logic:

Not all devices transition correctly between phases. Devices are supposed
to keep BSY high until they've set the registers to the next sensible
state. Some devices drop BSY and leave the registers in an old or
nonsense state. Our polling code is extremely sensitive to this
(though an early itnerrupt could also observe this). So, if the device is
in an unexpected state, the new polling code waits for a while in the hope
that it enters a better state.

This seems to fix many of the problems reported.

Also, there was a horrible bug which would cause sense to fail on a ATAPI
command that sent data to the drive


Revision tags: OPENBSD_2_6_BASE
# 1.9 09-Oct-1999 csapuntz

Fix printout at attach time. Print PIO/DMA mode information after having
printed line with the name of hard disk.


# 1.8 09-Oct-1999 csapuntz

wdc layer work:
Separate wdc_probe_caps into wdc_probe_caps and wdc_print_caps for more
flexibility in printing capability information.

Get rid of wdc_final_attach.

Include name of device (e.g. cd0), if possible, on errors.

atapiscsi layer work:
Put a pointer to the SCSI device into ata_drive_datas' drv_softc field

Simplify, simplify, simplify. Got rid of a bunch of fields in atapiscsi_softc

Delay printing capabilities until we know the real device name (e.g. st0)


# 1.7 15-Aug-1999 millert

o Correctly set type and typename in driver-generated label
Should really use viscopy() when copying atap_model -> d_packname.
When wd's become sd's this will be trivial (viscopy() lives in sd.c)


# 1.6 10-Aug-1999 deraadt

support DIOCRLDINFO


# 1.5 05-Aug-1999 niklas

Make IDE compile on alpha again:
use b* instead of mem* functions, adhere to NISADMA for DMA support.


# 1.4 22-Jul-1999 deraadt

shorten dmesg output


# 1.3 22-Jul-1999 csapuntz

Fix bad144intern.


# 1.2 20-Jul-1999 csapuntz

126 -> NBT_BAD

Fix disklabel/bad sectors stuff in wd


# 1.1 18-Jul-1999 csapuntz

Import of NetBSD ATA/IDE stuff.

Introduction of home-grown

To enable this stuff in your configuration, look at the NEWATA conf file
and go through

dev/isa/files.isa
dev/pci/files.pci
conf/files
arch/i386/conf/files.i386

and follow the instructions on commenting/uncommenting stuff


# 1.129 06-Apr-2022 naddy

constify struct cfattach


Revision tags: OPENBSD_7_1_BASE
# 1.128 09-Jan-2022 jsg

spelling
feedback and ok tb@ jmc@ ok ratchov@


Revision tags: OPENBSD_6_7_BASE OPENBSD_6_8_BASE OPENBSD_6_9_BASE OPENBSD_7_0_BASE
# 1.127 23-Jan-2020 tedu

we've been using LBA48 for < 137GB drives for 17 years, so perhaps
we can remove the XXX now.


Revision tags: OPENBSD_6_6_BASE
# 1.126 07-Jul-2019 kn

Use timeout_add_msec(9)

RECOVERYTIME is twice per second, so sleep for 500 milliseconds
and reflect the unit to be clear.

OK patrick


Revision tags: OPENBSD_6_3_BASE OPENBSD_6_4_BASE OPENBSD_6_5_BASE
# 1.125 30-Dec-2017 guenther

Don't pull in <sys/file.h> just to get fcntl.h

ok deraadt@ krw@


# 1.124 14-Dec-2017 deraadt

Repair flushcache/standby operations to not convert AT_WAIT operations
to AT_POLL, which some chip driver underneath cannot handle.
tested by bluhm also


# 1.123 24-Oct-2017 jsg

remove defines for ioctls the kernel doesn't recognise
ok mpi@


Revision tags: OPENBSD_6_2_BASE
# 1.122 29-May-2017 sf

Add an ioctl to tell storage devices to flush their internal caches

Currently implemented for wd and sd.
Initially ported from netbsd by pedro@

ok deraadt@


# 1.121 04-May-2017 deraadt

Also pass the blk offset to disk_unbusy(), so that it can pass it to
the random subsystem as entropy. This value is pretty much unknown,
and anyways our entropy input ring does not saturate from knowns.
ok mikeb djm


Revision tags: OPENBSD_5_9_BASE OPENBSD_6_0_BASE OPENBSD_6_1_BASE
# 1.120 20-Jan-2016 stefan

Set segflg member of uio struct with symbolic enum value
instead of using magic numbers. No binary change.

Diff from Martin Natano.

ok deraadt@


# 1.119 26-Aug-2015 deraadt

trivial size to free()


# 1.118 17-Aug-2015 krw

The 80's called and want their interface back. Remove last fragments of
ST-506 support. Remove redundant 'precomp' variables/fields and rename
remaining few to 'features' to be consistant with the rest of the code.

Consensus that no ST-506 disks (requiring ISA cards, <120MB, usually
rife with bad sectors that can't be worked around) are still running
OpenBSD.


Revision tags: OPENBSD_5_7_BASE OPENBSD_5_8_BASE
# 1.117 14-Sep-2014 jsg

remove uneeded proc.h includes
ok mpi@ kspillner@


Revision tags: OPENBSD_5_6_BASE
# 1.116 12-Jul-2014 tedu

add a size argument to free. will be used soon, but for now default to 0.
after discussions with beck deraadt kettenis.


# 1.115 10-Jul-2014 deraadt

remove most of the boolean_t infection outside uvm/ddb/pmap; ok jsg


Revision tags: OPENBSD_5_5_BASE
# 1.114 14-Oct-2013 krw

Use DL_SECTOBLK() instead of handrolling same calculation to get
wdsize() return value.


# 1.113 14-Oct-2013 krw

XXsize() returns daddr_t, so calculate the return value in a daddr_t
variable and not (usually) an int.

ok miod@


# 1.112 27-Sep-2013 krw

Try harder to ensure wddump() doesn't decrement the count
of sectors to write by one value and increment the address
the next i/o will start at by a different amount.


# 1.111 15-Sep-2013 krw

Make it clearer that sc_wdc_bio.blkno is actuall a disk hardware
address by using the DL_BLKTOSEC() and DL_SECTOBLK() defines rather
than handrolling the equivalent. Fixes current addition of a 512-byte
block value (b_blkno) to a disk hardware address (DL_GETPOFFSET()).

Tested on real wd(4) hardware by guenther@.


Revision tags: OPENBSD_5_4_BASE
# 1.110 11-Jun-2013 deraadt

final removal of daddr64_t. daddr_t has been 64 bit for a long enough
test period; i think 3 years ago the last bugs fell out.
ok otto beck others


Revision tags: OPENBSD_5_3_BASE
# 1.109 08-Oct-2012 deraadt

Revamp the sequences for suspend/hibernate -> resume so that the code
paths are reflexive. It is now possible to fail part-way through a
suspend sequence, and recover along the resume code path.
Split DVACT_SUSPEND by adding a new DVACT_POWERDOWN method is used
after hibernate (and suspend too) to finish the job. Some drivers
must be converted at the same time to use this instead of shutdown hooks
(the others will follow at a later time)
ok kettenis mlarkin


Revision tags: OPENBSD_5_0_BASE OPENBSD_5_1_BASE OPENBSD_5_2_BASE
# 1.108 06-Jul-2011 matthew

Eliminate redundant buf validation checks in xxstrategy() methods now
that they're implemented consistently in bounds_check_with_label().

Also, per krw's request, change bounds_check_with_label() to return 0
if the checks succeed, and change the drivers to test == -1 instead of
<= 0. (Man page update to follow; intentionally omitting
arch/vax/mba/hp.c from this commit because it doesn't even build
currently and miod@ promises to kill it soon.)

ok krw@


# 1.107 30-Jun-2011 matthew

Refactor some common open/close/detach disk driver code into
subr_disk.c. For now just the MI disk drivers.

ok deraadt@, krw@; jsing@ liked the approach too


# 1.106 20-Jun-2011 matthew

Call bufq_drain() in wddetach() instead of reimplementing the same
logic.


# 1.105 19-Jun-2011 deraadt

Use disk_lock_nointr() in the xxclose() routines so that they cannot
be interrupted.
ok matthew


# 1.104 19-Jun-2011 deraadt

Use disk_lock/disk_unlock directly and in the same way in these drivers,
rather than using various wrappings. Convert vnd to using the sc_dk
rwlock instead of using one of its own.
ok matthew


# 1.103 05-Jun-2011 matthew

Drop kernel support for the useless DIOCWLABEL ioctl and prune a lot
of silly flag twiddling code in various disk drivers.

ok deraadt@, miod@

N.B., users will need a -current disklabel(8) to be able to write new
disklabels to disk now.


# 1.102 03-Jun-2011 matthew

Get rid of the wlabel argument to bounds_check_with_label(). It's
never done anything in OpenBSD and just clutters disk drivers with
silly flag handling.

More cleanup to follow.

ok deraadt@, millert@; no objections krw@


# 1.101 31-May-2011 matthew

Change a few of the more common disk drivers (sd, cd, wd, rd, and vnd)
to return EBUSY if the user tries to modify an open partition's offset
or size. Only sadness can result if a user tries this, and rejecting
it prevents a race between sdstart() and sdstrategy().

Curiously, there was already code in the kernel and in disklabel(8) to
detect/handle this, but it was effectively disabled because the disk
drivers always used something like "/* sc->sc_dk.dk_openmask */ 0",
and this commented out code has existed since even r1.1 in NetBSD.

I had no problems building a release and messing around with
disklabel(8) for a bit with this diff. Canarying the more common MI
disk drivers until we gain confidence that there aren't any
regressions, then we can switch the remaining drivers.

"I am surprised you got me convinced that this stuff is safe" deraadt@
ok krw@


# 1.100 18-Apr-2011 deraadt

keep an eye out for wdc registers returning 0xff. if they return 0xff,
this probably means the controller is dead -- as in, a hot plug eject
event. in that case, mark the driver dead and try harder to return -1
in the interrupt handler.
tested by many
ok dlg, miod did not object


# 1.99 15-Apr-2011 miod

Change wdc_reset_channel() to take a `no wait' argument. Pass in turn this
argument to wdcreset(), to have it skip waiting until active channels see
their BUSY bit clear in the status register.

Use this feature in the resume path, during the first reset operation. The
first reset is supposed to only wake up the controller, and the disks don't
come back until the second reset is issued, therefore waiting for them to
report themselves as ready after the first reset, but before the second, is
moot - and as a matter of fact some controllers, such as the AMD 754 and
clones/offspring (e.g. Geode) keep the BUSY bit asserted after the first
reset.

Last, but not least, make sure wd@ata invokes wd_get_params() again before
returning from the resume code, as we will still be using polled transfers
for a short while.

This causes the Lemote Yeelong to resume within less than one second, instead
of the lousy 30 seconds wait between the two resets; and the wd_get_params()
voodoo prevents it from getting spurious ide interrupts afterwards.

wd_get_params() magic from dlg; rest of the work by yours truly after enough
prodding by dlg@ and pirofti@, among others. ok deraadt@ dlg@


# 1.98 05-Apr-2011 deraadt

wd_hibernate_io() is a standalone disk io writer which tries to not
damage kernel memory very much while doing the job. It is not very
pretty yet, but improving it will need some MI work.
ok mlarkin jsg dlg


Revision tags: OPENBSD_4_9_BASE
# 1.97 31-Dec-2010 kettenis

Reintroduce the code that puts the drive into standby in shutdown, but
only do this if we're going to power down the machine.

"My god, it is a 3 line diff. Commit it before it gets bigger!" deraadt@


# 1.96 30-Dec-2010 kettenis

The NVIDIA MCP89 SATA controller found on the 2010 Apple 13" MacBook Pro
can't address block 0xffffffff in LBA mode, at least in combination with
the Hitachi HTS545032B9SA02 and HTS545050B9SA02 drives that Apple ships.
So use LBA48 mode from that block onwards.

ok marco@, deraadt@, miod@


# 1.95 22-Sep-2010 matthew

All users of physio(9) now pass NULL as the buf pointer argument, so
no point in keeping it around.

"i like this" thib@ (a while back); ok krw@ and oga@; reminder to
update the man page and tweaks jmc@


# 1.94 20-Sep-2010 deraadt

DVACT_ACTIVATE and DVACT_DEACTIVATE do nothing here; remove
the false comments.


# 1.93 08-Sep-2010 jsing

Introduce a disk_lookup() function which calls device_lookup(), before
verifying that the resulting device is present on the disklist. This
avoids a race whereby the disk driver can be accessed as soon as the
softc has been allocated, but before the disk has completed
initialisation and has called disk_attach() (up until this point
dk_label is still a null pointer).

Cut cd(4), sd(4) and wd(4) across to disk_lookup(). All callers of
disk_attach() need to be tested and cut over in due course.

ok deraadt@ krw@


# 1.92 08-Sep-2010 jsing

Store a struct device pointer within struct disk and populate this when
disk_attach() is called by the device driver. We will be building on
this shortly.

ok deraadt@ krw@


# 1.91 01-Sep-2010 dlg

make struct bufq a member of the softc for devices that use it,
rather than it being a pointer to something that needs to be allocated
at attach. since all these devices need a bufq to operate, it makes
sense to have it allocated as part of the softc and get bufq_init
to just initialise all its fields. it also gets rid of the possibility
that you wont be able to allocate the bufq struct during attach,
which is something you dont want to happen.

secondly, it consistently implements a split between wrapper functions
and the per discipline implementation of the bufq handlers. it
consistently does the locking in the wrappers rather than doing
half in the wrappers and the other half in the implementations.

it also consistently handles the outstanding bufq bq pointer in the
wrappers.

this hides most of the implementation inside kern_bufq.c. the only
stuff left in buf.h is for the bits each implementation needs to
put inside struct buf.

tested by thib@ krw@ and me
ok thib@ matthew@
no objection from krw@


# 1.90 31-Aug-2010 deraadt

sort DVACT_ actions in order of use


# 1.89 28-Aug-2010 matthew

Garbage collect struct dkdriver.

ok miod@; "please go ahead" jsing@


Revision tags: OPENBSD_4_8_BASE
# 1.88 23-Jul-2010 jsg

Manuel Bouyer rescinded clauses 3 and 4 of his license text.


# 1.87 22-Jul-2010 kettenis

Give the drive a little bit more time to complete the STANDBY IMMED command.
The ATA spec seems to suggest it may take up to 30s. This seems a little bit
long. To quote Theo: "by that time some drives have probably parked their own
heads in boredom". But it shouldn't hurt unless your drive is seriously
broken. Makes some laptops suspend much better.

ok deraadt@


# 1.86 22-Jul-2010 mlarkin

First part of better pciide resume. Wake the channel by performing two
resets separated by a small delay (a delay intended to offend jsg who will
definitely want to fix it by checking for RDY instead)

Tested on a variety of amd and i386 hardware by myself and deraadt@

ok deraadt


# 1.85 28-Jun-2010 jsing

Move disk_attach() to the end of the attach functions, at which point the
disk is now ready to handle I/O.

ok krw@ dlg@


# 1.84 07-Jun-2010 jsg

partial backout of 1.81 as invoking standby in shutdown
breaks at least socppc and armish


# 1.83 26-May-2010 thib

Reintroduce bufqs. A few changes since it was backed out after some good
comments from dlg@.

No need for a separate bufq.h, keep all of in buf.h; As requested by kittens
and deraadt.

Only sd(4) and wd(4) for now. The rest of the drivers will be converted soon,
also other goodies like heuristics for sd(4) for selecting the bufq type and
the death of disksort() are forthcoming.

Tested on: i386, amd64, sparc64, macppc, loongson and alpha by myself and
phessler.

OK art@, beck@, kettenis@, oga@


# 1.82 24-May-2010 kettenis

Flush cache before suspend.

ok krw@, marco@


# 1.81 23-May-2010 kettenis

Place drive in standby mode before shutdown. Avoids the loud click heard on
many laptops when powering them down.

ok jsg@, oga@, krw@


# 1.80 18-May-2010 dlg

dont let sys/ioctl.h imply that you get the ioctls in dkio.h. this
gets rid of #include <sys/dkio.h> in sys/ioctl.h and adds #include
<sys/dkio.h> to the places that actually want and use the disk
ioctls.

this became an issue when krw@'s X build failed when he was testing
a change to dkio.h.
tested by krw@
help from and ok miod@


# 1.79 23-Apr-2010 jsing

Recycle unused disklabel fields in order to create a disklabel unique
identifier, allowing the disk to be identified without relying on the
device name.

ok deraadt@ krw@ beck@ marco@ todd@


# 1.78 11-Apr-2010 kettenis

Send "STANDBY IMMEDIATE" command to ATA disks upon suspend. For this to work,
pciide(4) needs to formward DVACT_SUSPEND and DVACT_RESUME events to its
children, so do that.

Gets rid of the nasty "click" sound from the disk on many laptops.

ok marco@, jsg@


Revision tags: OPENBSD_4_7_BASE
# 1.77 13-Oct-2009 pirofti

Get rid of devact enum, substitute it with an int and coresponding defines.

This is needed for the addition of further suspend/resume actions.

Okay deraadt@, marco@.


# 1.76 13-Aug-2009 deraadt

Replace the error strings that were being passed around with much simpler
errnos. Note that the error strings are being ignored, since we long ago
decided to not spam the console, and there is no other nice way to use the
errors (without changing the ioctls to pass it back)
The errno is now useful, since we can pass b_error from failing IO up, and
the drive can decide how to use that
ok miod


Revision tags: OPENBSD_4_6_BASE
# 1.75 17-Jun-2009 thib

Revert bufq's. this is inline with the major midlayer reverts that
have been going on. this appears to bring us back to stable state.

lots of testing by oga and ariane and my self.


# 1.74 03-Jun-2009 thib

add a flexible buffer queue (bufq) api, based on the never used
one by tedu@. It doesn't do anything smart yet, it just uses
plain old disksort. we also keep the old method of queueing bufs
since some miods have crazy MD drivers that need some love.

ok beck@, art@
tested by many on many archs.


Revision tags: OPENBSD_4_5_BASE
# 1.73 08-Nov-2008 chl

remove dead stores and newly created unused variables.

Found by LLVM/Clang Static Analyzer.

ok krw@


# 1.72 10-Sep-2008 blambert

Convert timeout_add() calls using multiples of hz to timeout_add_sec()

Really just the low-hanging fruit of (hopefully) forthcoming timeout
conversions.

ok art@, krw@


Revision tags: OPENBSD_4_4_BASE
# 1.71 27-Jun-2008 ray

More removal of clauses 3 and 4 from NetBSD licenses.

OK deraadt@ and millert@


# 1.70 15-Jun-2008 krw

Don't bypass partition bounds check for RAW_PART. We now guarantee that
RAW_PART will always be 0 -> disksize, so the bounds check will always
pass for i/o's to valid addresses. Now the i/o will be properly truncated
if it goes past the end of the device. This prevents various adverse
impacts of issuing i/o's for data past the end of the device.

Repeatedly requested by todd@.

ok weingart@ deraadt@


Revision tags: OPENBSD_4_3_BASE
# 1.69 07-Feb-2008 sthen

enable DMA for 1-sector devices (i.e. most flash devices).
from NetBSD

ok krw thib henning, tested by many


# 1.68 05-Dec-2007 jsg

Switch set features commands to use wdc_exec_command() to allow
them to be properly polled.

Fixes previously problematic machines of form and ckuethe.

ok marco@


# 1.67 26-Nov-2007 jsg

Enable read ahead and write cache enable if supported by
the disk rather than depending on the disk to have it on.

A bunch of people have suggested this should go in.


Revision tags: OPENBSD_4_2_BASE
# 1.66 20-Jun-2007 deraadt

b_cylinder does not need to be set on the callpath down into drivers.
cpu_disklabel can go away, since nothing anymore needs to use it; ok miod


# 1.65 18-Jun-2007 deraadt

avoid modification race in DIOCRLDINFO; ok krw miod


# 1.64 08-Jun-2007 deraadt

all drivers should spoof version 1 labels


# 1.63 07-Jun-2007 ray

strncpy -> strlcpy, plus a little spacing line up.

OK deraadt.


# 1.62 06-Jun-2007 deraadt

now that all partition size/offsets are potentially 64-bit, change the
type of all variables to daddr64_t. this includes the APIs for XXsize()
and XXdump(), all range checks inside bio drivers, internal variables
for disklabel handling, and even uvm's swap offsets. re-read numerous
times by otto, miod, krw, thib to look for errors


# 1.61 05-Jun-2007 deraadt

use six new macros to access & store the 48-bit disklabel fields related
to size. tested on almost all machines, double checked by miod and krw
next comes the type handling surrounding these values


# 1.60 01-Jun-2007 krw

Don't initialize d_partitions[RAW_DISK] just before calling
readdisklabel(), since all readdisklabel()'s do that already.

ok deraadt@


# 1.59 31-May-2007 krw

Kill old, no longer necessary kludge to try fooling readdisklabel()
into using DOS geometry by calling it twice. And don't ignore the
drive state if readdisklabel() returns a non-NULL value.

ok weingart@ grange@


# 1.58 28-Apr-2007 krw

Fix comments above DISKMINOR uses. DISKMINOR provides minor not major
number (wd.c), and the uses are to find the minimum minor to be
detached.


# 1.57 27-Apr-2007 krw

Nuke WDUNIT/WDPART/etc. defines and just use standard DISKUNIT/DISKPART/etc.
ones. No change to wd.o.

ok deraadt@ marco@


# 1.56 26-Apr-2007 krw

Eliminate CPU_BIOS from userland and wd(4) by always using the BIOS
geometry in the disklabel when there is a BIOS geometry to provide.
This removes the option to set a disklabel to 'BIOS' geometry via the
'g b' command in the editor.

Makes reported geometry more consistant and moves MD code to MD land
where it should be.

Doc help from jmc@, Feedback from millert@, marco@, weingart@,
kettenis@.

ok deraadt@


# 1.55 27-Mar-2007 deraadt

OK, I understand why jsg wanted the disk_unbusy() in wdretry(), and he
was right. There is another path that ends up avoiding the disk_unbusy
call I inserted.... tested by robert too


# 1.54 25-Mar-2007 deraadt

wdc retries (including after suspends) caused a disk to remain busy when
it should not; ok dlg
problem spotted using zaurus soft-drive-led diff which is pending
i think i convinced jsg now that this fix is better than putting it
in wdrestart


Revision tags: OPENBSD_4_1_BASE
# 1.53 15-Feb-2007 krw

Don't print the error strings returned by readdisklabel(). If you
need the debug info uncomment the printf's you need. Crude but
effective way to suppress 'no disklabel' errors that pop up at the
most innconvenient times to frighten users. More elegant method,
DPRINTF-like constructs or something, later.

"Yay!" marco@ ok deraadt@


# 1.52 06-Feb-2007 krw

De-register. No object code change.

From Bret Lambert.


# 1.51 17-Oct-2006 krw

For non-CPU_BIOS architectures calculate the number of cylinders on
a disk rather than accepting the cylinder count provided by the disk
or controller. This cylinder count will be '16383' for any disk >8.4G
according to the ATA spec. CPU_BIOS on i386/amd64 has magic to deal
with this, but other archs do not need to be restricted by the needs
of PC BIOS.

Fixes the default MBR OpenBSD partition size and disklabel on non-CPU_BIOS
archtitectures.

No change to behaviour on i386/amd64 machines.

Noted by Stuart Henderson on his Thecus.

Testing by various, including 'old i386 machines' nick@

ok tom@ pedro@ weingart@ deraadt@


# 1.50 04-Oct-2006 krw

Zap bad144 bad sector info in disklabels. Sparsely used if at all for
a few obsolete and easily replaced disk models. And didn't work anyway
according to Bob, who has tried.

ok beck@


Revision tags: OPENBSD_4_0_BASE
# 1.49 21-Aug-2006 krw

Delete #ifdef/#ifndef __OpenBSD__ chunks, leaving just the code we
use. Easier to read and eliminates a potentially confusing unused call
to wdgetdisklabel(). No change to object files.

ok brad@ dlg@ grange@ deraadt@ marco@


# 1.48 12-Aug-2006 krw

Setting d_secsize to DEV_BSIZE (or 1 << DEV_BSHIFT, or 512) and then
setting RAW_PART's p_size to d_secperunit * (d_secsize / DEV_BSIZE) is
a waste of a few ops. And p_size should be in sectors anyway.

Just set RAW_PART's p_size to d_secperunit to make usage consistant
across the tree.

Should be a no-op.


# 1.47 05-Mar-2006 krw

Remove more NRND NetBSDisms.

ok marco@


Revision tags: OPENBSD_3_9_BASE
# 1.46 21-Jan-2006 miod

Invoke disk_detach() and related cleanup work in detach(), rather than
zeroref() - just to be on the safe side, should we mess up our ref count.


# 1.45 09-Nov-2005 uwe

Don't fall back to fake geometry if ata_get_params() fails.
"looks sane" grange@; tested by niall@ krw@ jmc@ jolan@


Revision tags: OPENBSD_3_8_BASE
# 1.44 01-Sep-2005 uwe

On zaurus, don't fall back to fake geometry if ata_get_params() fails,
which can happen if the integrated microdrive's DRQ is not clear. This
fixes the drive geometry in the default disklabel.
Help from weingart@ and grange@; "mention `grange and i will fix this
better after 3.8 or we will be flailed'" deraadt@


# 1.43 27-Aug-2005 jsg

Use the ATA security FREEZE LOCK command on disks before
they are presented to the rest of the system to stop
a password being set that could deny access to the disk.
ok grange@ deraadt@


# 1.42 15-May-2005 grange

Use FLUSHCACHE EXT instead of FLUSHCACHE for LBA48 operations.
First noticed by christos@netbsd (iirc) a long time ago.

Tested by canacar@ and krw@.


Revision tags: OPENBSD_3_6_BASE OPENBSD_3_7_BASE
# 1.41 06-Jul-2004 aaron

Replace magic number with a more descriptive #define. This magic number was
particularly vile because it looks like a typo (i.e., looks like one "f" is
missing), but it's really not. I happened to notice this myself, but the fix
is inspired by NetBSD in the interest of keeping the code similar.

deraadt@ ok


Revision tags: OPENBSD_3_5_BASE SMP_SYNC_A SMP_SYNC_B
# 1.40 03-Mar-2004 tedu

remove bufq support until i figure out just what's going wrong. ok deraadt@


# 1.39 15-Feb-2004 tedu

new arg to disk_unbusy, to record separate read/write statistics.
looked at by various, testing henning@ mcbride@ dan weeks
mostly from netbsd via Pedro Martelletto <pbastos@rdc.puc-rio.br>


# 1.38 02-Feb-2004 tedu

as grange pointed out, BUFQ_ALLOC might fail


# 1.37 21-Jan-2004 tedu

dynamic bufq support. basically a nullop. ok grange@


# 1.36 16-Jan-2004 grange

Do not print CHS info for LBA drives, it's useless for drives with
capacity > 8GB. Total number of cylinders is enough.

ok deraadt@


# 1.35 15-Jan-2004 grange

ansi and some spaces; no f/c


# 1.34 21-Oct-2003 jmc

typos from Tom Cosgrove;


# 1.33 28-Sep-2003 grange

kill trailing spaces, no functional changes


Revision tags: OPENBSD_3_4_BASE
# 1.32 20-Jul-2003 grange

Use LBA48 only when accessing blocks after 137Gb limit or using sectors
count more than 255 to reduce registers writting overhead. Gives me
about 5% perfomance increase in a dumb synthetic test.

Some input and ok from costa@ a long time ago.


# 1.31 25-Jun-2003 tedu

implement new means of manipulating buf queues, bufq.
accessed with BUFQ macros, bufq structs support extensible, potentially
changable algorithms and queue formats. the current default scheme
should support nice priority based queuing, but is missing some vfs_bio.c
support.
only on wd.c for now, other drivers are easy converts.
as a side bonus, this makes the driver code look cleaner.
idea for the name comes from netbsd, but this scheme is incompatible.
thanks to various people for testing.
ok grange@


# 1.30 20-May-2003 tedu

change spaces to tabs so i can mail grange diffs :)
ok grange@


Revision tags: UBC_SYNC_A
# 1.29 09-Apr-2003 ho

sprintf->snprintf. miod@, grange@ ok.


# 1.28 06-Apr-2003 krw

Some easy strcpy elimination.

a) strcpy -> strncpy for copies into disklabel fields d_packname and
d_typename.

b) '16' -> 'sizeof d_packname' or 'sizeof d_typename' in existing
strncpy's.

ok millert@


Revision tags: OPENBSD_3_3_BASE
# 1.27 21-Feb-2003 grange

Fix Manuel Bouyer's copyright, from NetBSD
ok deraadt@


# 1.26 19-Dec-2002 grange

Protect WDCDEBUG_PRINT macro with do {} while (0)
ok costa@


Revision tags: OPENBSD_3_2_BASE UBC_SYNC_B
# 1.25 24-May-2002 art

protect biodone with splbio.


# 1.24 03-May-2002 gluk

LBA48 support.
Adopted from NetBSD by Alexander Yurchenko <grange@openbsd.ru>.

costa@ ok.


Revision tags: OPENBSD_3_1_BASE
# 1.23 14-Mar-2002 millert

Final __P removal plus some cosmetic fixups


# 1.22 14-Mar-2002 millert

First round of __P removal in sys


# 1.21 07-Jan-2002 mickey

pass the proc* down the ioctl chain to controller; mjacob@ csapuntz@ ok


Revision tags: UBC_BASE
# 1.20 06-Nov-2001 miod

branches: 1.20.2;
Replace inclusion of <vm/foo.h> with the correct <uvm/bar.h> when necessary.
(Look ma, I might have broken the tree)


Revision tags: OPENBSD_3_0_BASE
# 1.19 06-Aug-2001 miod

Remove legacy __BDEVSW_DUMP_OLD_TYPE code.


# 1.18 31-Jul-2001 csapuntz

Go down only one Ultra DMA mode at a time. Thanks to Matt Evans for
suggesting this fix.

Update atapiscsi Ultra DMA downgrade logic.


# 1.17 12-Jul-2001 csapuntz

extra wddone caused biodone panics occasionally


# 1.16 30-Apr-2001 csapuntz

Move ioctl stuff to low-level driver


Revision tags: OPENBSD_2_9_BASE
# 1.15 02-Apr-2001 csapuntz

ATA_POLL should have been AT_POLL

Flushcache on shutdown used to pass both AT_POLL and AT_WAIT. The two flags
do not make sense together.

Should fix panic when shutting down from ddb


# 1.14 25-Jan-2001 todd

spelling


Revision tags: OPENBSD_2_8_BASE
# 1.13 30-Jun-2000 art

Convert to new timeout API.


Revision tags: OPENBSD_2_7_BASE
# 1.12 10-Apr-2000 csapuntz

Added support for wd detach (merge from NetBSD). Support for
SCSI/ATAPI detach is not here yet.

Minor cleanup of wdc. Downgrade to UDMA mode 1 before going further.
Want to stay in UDMA modes because they're more error-resilient due to
a CRC.

Got rid of some of the ridiculous amount of softc sharing going on.
Hopefully, this will make the life of whoever goes in and fixes the
ref-counting to be correct easier.


# 1.11 07-Apr-2000 niklas

remove statics


Revision tags: SMP_BASE kame_19991208
# 1.10 17-Nov-1999 csapuntz

branches: 1.10.2;


New interface to ATA registers that goes through the lower-level device
driver (pciide, wdc, etc.)

Remove #define WDCDEBUG from top of files

More fixes to ATAPISCSI logic:

Not all devices transition correctly between phases. Devices are supposed
to keep BSY high until they've set the registers to the next sensible
state. Some devices drop BSY and leave the registers in an old or
nonsense state. Our polling code is extremely sensitive to this
(though an early itnerrupt could also observe this). So, if the device is
in an unexpected state, the new polling code waits for a while in the hope
that it enters a better state.

This seems to fix many of the problems reported.

Also, there was a horrible bug which would cause sense to fail on a ATAPI
command that sent data to the drive


Revision tags: OPENBSD_2_6_BASE
# 1.9 09-Oct-1999 csapuntz

Fix printout at attach time. Print PIO/DMA mode information after having
printed line with the name of hard disk.


# 1.8 09-Oct-1999 csapuntz

wdc layer work:
Separate wdc_probe_caps into wdc_probe_caps and wdc_print_caps for more
flexibility in printing capability information.

Get rid of wdc_final_attach.

Include name of device (e.g. cd0), if possible, on errors.

atapiscsi layer work:
Put a pointer to the SCSI device into ata_drive_datas' drv_softc field

Simplify, simplify, simplify. Got rid of a bunch of fields in atapiscsi_softc

Delay printing capabilities until we know the real device name (e.g. st0)


# 1.7 15-Aug-1999 millert

o Correctly set type and typename in driver-generated label
Should really use viscopy() when copying atap_model -> d_packname.
When wd's become sd's this will be trivial (viscopy() lives in sd.c)


# 1.6 10-Aug-1999 deraadt

support DIOCRLDINFO


# 1.5 05-Aug-1999 niklas

Make IDE compile on alpha again:
use b* instead of mem* functions, adhere to NISADMA for DMA support.


# 1.4 22-Jul-1999 deraadt

shorten dmesg output


# 1.3 22-Jul-1999 csapuntz

Fix bad144intern.


# 1.2 20-Jul-1999 csapuntz

126 -> NBT_BAD

Fix disklabel/bad sectors stuff in wd


# 1.1 18-Jul-1999 csapuntz

Import of NetBSD ATA/IDE stuff.

Introduction of home-grown

To enable this stuff in your configuration, look at the NEWATA conf file
and go through

dev/isa/files.isa
dev/pci/files.pci
conf/files
arch/i386/conf/files.i386

and follow the instructions on commenting/uncommenting stuff


# 1.128 09-Jan-2022 jsg

spelling
feedback and ok tb@ jmc@ ok ratchov@


Revision tags: OPENBSD_6_7_BASE OPENBSD_6_8_BASE OPENBSD_6_9_BASE OPENBSD_7_0_BASE
# 1.127 23-Jan-2020 tedu

we've been using LBA48 for < 137GB drives for 17 years, so perhaps
we can remove the XXX now.


Revision tags: OPENBSD_6_6_BASE
# 1.126 07-Jul-2019 kn

Use timeout_add_msec(9)

RECOVERYTIME is twice per second, so sleep for 500 milliseconds
and reflect the unit to be clear.

OK patrick


Revision tags: OPENBSD_6_3_BASE OPENBSD_6_4_BASE OPENBSD_6_5_BASE
# 1.125 30-Dec-2017 guenther

Don't pull in <sys/file.h> just to get fcntl.h

ok deraadt@ krw@


# 1.124 14-Dec-2017 deraadt

Repair flushcache/standby operations to not convert AT_WAIT operations
to AT_POLL, which some chip driver underneath cannot handle.
tested by bluhm also


# 1.123 24-Oct-2017 jsg

remove defines for ioctls the kernel doesn't recognise
ok mpi@


Revision tags: OPENBSD_6_2_BASE
# 1.122 29-May-2017 sf

Add an ioctl to tell storage devices to flush their internal caches

Currently implemented for wd and sd.
Initially ported from netbsd by pedro@

ok deraadt@


# 1.121 04-May-2017 deraadt

Also pass the blk offset to disk_unbusy(), so that it can pass it to
the random subsystem as entropy. This value is pretty much unknown,
and anyways our entropy input ring does not saturate from knowns.
ok mikeb djm


Revision tags: OPENBSD_5_9_BASE OPENBSD_6_0_BASE OPENBSD_6_1_BASE
# 1.120 20-Jan-2016 stefan

Set segflg member of uio struct with symbolic enum value
instead of using magic numbers. No binary change.

Diff from Martin Natano.

ok deraadt@


# 1.119 26-Aug-2015 deraadt

trivial size to free()


# 1.118 17-Aug-2015 krw

The 80's called and want their interface back. Remove last fragments of
ST-506 support. Remove redundant 'precomp' variables/fields and rename
remaining few to 'features' to be consistant with the rest of the code.

Consensus that no ST-506 disks (requiring ISA cards, <120MB, usually
rife with bad sectors that can't be worked around) are still running
OpenBSD.


Revision tags: OPENBSD_5_7_BASE OPENBSD_5_8_BASE
# 1.117 14-Sep-2014 jsg

remove uneeded proc.h includes
ok mpi@ kspillner@


Revision tags: OPENBSD_5_6_BASE
# 1.116 12-Jul-2014 tedu

add a size argument to free. will be used soon, but for now default to 0.
after discussions with beck deraadt kettenis.


# 1.115 10-Jul-2014 deraadt

remove most of the boolean_t infection outside uvm/ddb/pmap; ok jsg


Revision tags: OPENBSD_5_5_BASE
# 1.114 14-Oct-2013 krw

Use DL_SECTOBLK() instead of handrolling same calculation to get
wdsize() return value.


# 1.113 14-Oct-2013 krw

XXsize() returns daddr_t, so calculate the return value in a daddr_t
variable and not (usually) an int.

ok miod@


# 1.112 27-Sep-2013 krw

Try harder to ensure wddump() doesn't decrement the count
of sectors to write by one value and increment the address
the next i/o will start at by a different amount.


# 1.111 15-Sep-2013 krw

Make it clearer that sc_wdc_bio.blkno is actuall a disk hardware
address by using the DL_BLKTOSEC() and DL_SECTOBLK() defines rather
than handrolling the equivalent. Fixes current addition of a 512-byte
block value (b_blkno) to a disk hardware address (DL_GETPOFFSET()).

Tested on real wd(4) hardware by guenther@.


Revision tags: OPENBSD_5_4_BASE
# 1.110 11-Jun-2013 deraadt

final removal of daddr64_t. daddr_t has been 64 bit for a long enough
test period; i think 3 years ago the last bugs fell out.
ok otto beck others


Revision tags: OPENBSD_5_3_BASE
# 1.109 08-Oct-2012 deraadt

Revamp the sequences for suspend/hibernate -> resume so that the code
paths are reflexive. It is now possible to fail part-way through a
suspend sequence, and recover along the resume code path.
Split DVACT_SUSPEND by adding a new DVACT_POWERDOWN method is used
after hibernate (and suspend too) to finish the job. Some drivers
must be converted at the same time to use this instead of shutdown hooks
(the others will follow at a later time)
ok kettenis mlarkin


Revision tags: OPENBSD_5_0_BASE OPENBSD_5_1_BASE OPENBSD_5_2_BASE
# 1.108 06-Jul-2011 matthew

Eliminate redundant buf validation checks in xxstrategy() methods now
that they're implemented consistently in bounds_check_with_label().

Also, per krw's request, change bounds_check_with_label() to return 0
if the checks succeed, and change the drivers to test == -1 instead of
<= 0. (Man page update to follow; intentionally omitting
arch/vax/mba/hp.c from this commit because it doesn't even build
currently and miod@ promises to kill it soon.)

ok krw@


# 1.107 30-Jun-2011 matthew

Refactor some common open/close/detach disk driver code into
subr_disk.c. For now just the MI disk drivers.

ok deraadt@, krw@; jsing@ liked the approach too


# 1.106 20-Jun-2011 matthew

Call bufq_drain() in wddetach() instead of reimplementing the same
logic.


# 1.105 19-Jun-2011 deraadt

Use disk_lock_nointr() in the xxclose() routines so that they cannot
be interrupted.
ok matthew


# 1.104 19-Jun-2011 deraadt

Use disk_lock/disk_unlock directly and in the same way in these drivers,
rather than using various wrappings. Convert vnd to using the sc_dk
rwlock instead of using one of its own.
ok matthew


# 1.103 05-Jun-2011 matthew

Drop kernel support for the useless DIOCWLABEL ioctl and prune a lot
of silly flag twiddling code in various disk drivers.

ok deraadt@, miod@

N.B., users will need a -current disklabel(8) to be able to write new
disklabels to disk now.


# 1.102 03-Jun-2011 matthew

Get rid of the wlabel argument to bounds_check_with_label(). It's
never done anything in OpenBSD and just clutters disk drivers with
silly flag handling.

More cleanup to follow.

ok deraadt@, millert@; no objections krw@


# 1.101 31-May-2011 matthew

Change a few of the more common disk drivers (sd, cd, wd, rd, and vnd)
to return EBUSY if the user tries to modify an open partition's offset
or size. Only sadness can result if a user tries this, and rejecting
it prevents a race between sdstart() and sdstrategy().

Curiously, there was already code in the kernel and in disklabel(8) to
detect/handle this, but it was effectively disabled because the disk
drivers always used something like "/* sc->sc_dk.dk_openmask */ 0",
and this commented out code has existed since even r1.1 in NetBSD.

I had no problems building a release and messing around with
disklabel(8) for a bit with this diff. Canarying the more common MI
disk drivers until we gain confidence that there aren't any
regressions, then we can switch the remaining drivers.

"I am surprised you got me convinced that this stuff is safe" deraadt@
ok krw@


# 1.100 18-Apr-2011 deraadt

keep an eye out for wdc registers returning 0xff. if they return 0xff,
this probably means the controller is dead -- as in, a hot plug eject
event. in that case, mark the driver dead and try harder to return -1
in the interrupt handler.
tested by many
ok dlg, miod did not object


# 1.99 15-Apr-2011 miod

Change wdc_reset_channel() to take a `no wait' argument. Pass in turn this
argument to wdcreset(), to have it skip waiting until active channels see
their BUSY bit clear in the status register.

Use this feature in the resume path, during the first reset operation. The
first reset is supposed to only wake up the controller, and the disks don't
come back until the second reset is issued, therefore waiting for them to
report themselves as ready after the first reset, but before the second, is
moot - and as a matter of fact some controllers, such as the AMD 754 and
clones/offspring (e.g. Geode) keep the BUSY bit asserted after the first
reset.

Last, but not least, make sure wd@ata invokes wd_get_params() again before
returning from the resume code, as we will still be using polled transfers
for a short while.

This causes the Lemote Yeelong to resume within less than one second, instead
of the lousy 30 seconds wait between the two resets; and the wd_get_params()
voodoo prevents it from getting spurious ide interrupts afterwards.

wd_get_params() magic from dlg; rest of the work by yours truly after enough
prodding by dlg@ and pirofti@, among others. ok deraadt@ dlg@


# 1.98 05-Apr-2011 deraadt

wd_hibernate_io() is a standalone disk io writer which tries to not
damage kernel memory very much while doing the job. It is not very
pretty yet, but improving it will need some MI work.
ok mlarkin jsg dlg


Revision tags: OPENBSD_4_9_BASE
# 1.97 31-Dec-2010 kettenis

Reintroduce the code that puts the drive into standby in shutdown, but
only do this if we're going to power down the machine.

"My god, it is a 3 line diff. Commit it before it gets bigger!" deraadt@


# 1.96 30-Dec-2010 kettenis

The NVIDIA MCP89 SATA controller found on the 2010 Apple 13" MacBook Pro
can't address block 0xffffffff in LBA mode, at least in combination with
the Hitachi HTS545032B9SA02 and HTS545050B9SA02 drives that Apple ships.
So use LBA48 mode from that block onwards.

ok marco@, deraadt@, miod@


# 1.95 22-Sep-2010 matthew

All users of physio(9) now pass NULL as the buf pointer argument, so
no point in keeping it around.

"i like this" thib@ (a while back); ok krw@ and oga@; reminder to
update the man page and tweaks jmc@


# 1.94 20-Sep-2010 deraadt

DVACT_ACTIVATE and DVACT_DEACTIVATE do nothing here; remove
the false comments.


# 1.93 08-Sep-2010 jsing

Introduce a disk_lookup() function which calls device_lookup(), before
verifying that the resulting device is present on the disklist. This
avoids a race whereby the disk driver can be accessed as soon as the
softc has been allocated, but before the disk has completed
initialisation and has called disk_attach() (up until this point
dk_label is still a null pointer).

Cut cd(4), sd(4) and wd(4) across to disk_lookup(). All callers of
disk_attach() need to be tested and cut over in due course.

ok deraadt@ krw@


# 1.92 08-Sep-2010 jsing

Store a struct device pointer within struct disk and populate this when
disk_attach() is called by the device driver. We will be building on
this shortly.

ok deraadt@ krw@


# 1.91 01-Sep-2010 dlg

make struct bufq a member of the softc for devices that use it,
rather than it being a pointer to something that needs to be allocated
at attach. since all these devices need a bufq to operate, it makes
sense to have it allocated as part of the softc and get bufq_init
to just initialise all its fields. it also gets rid of the possibility
that you wont be able to allocate the bufq struct during attach,
which is something you dont want to happen.

secondly, it consistently implements a split between wrapper functions
and the per discipline implementation of the bufq handlers. it
consistently does the locking in the wrappers rather than doing
half in the wrappers and the other half in the implementations.

it also consistently handles the outstanding bufq bq pointer in the
wrappers.

this hides most of the implementation inside kern_bufq.c. the only
stuff left in buf.h is for the bits each implementation needs to
put inside struct buf.

tested by thib@ krw@ and me
ok thib@ matthew@
no objection from krw@


# 1.90 31-Aug-2010 deraadt

sort DVACT_ actions in order of use


# 1.89 28-Aug-2010 matthew

Garbage collect struct dkdriver.

ok miod@; "please go ahead" jsing@


Revision tags: OPENBSD_4_8_BASE
# 1.88 23-Jul-2010 jsg

Manuel Bouyer rescinded clauses 3 and 4 of his license text.


# 1.87 22-Jul-2010 kettenis

Give the drive a little bit more time to complete the STANDBY IMMED command.
The ATA spec seems to suggest it may take up to 30s. This seems a little bit
long. To quote Theo: "by that time some drives have probably parked their own
heads in boredom". But it shouldn't hurt unless your drive is seriously
broken. Makes some laptops suspend much better.

ok deraadt@


# 1.86 22-Jul-2010 mlarkin

First part of better pciide resume. Wake the channel by performing two
resets separated by a small delay (a delay intended to offend jsg who will
definitely want to fix it by checking for RDY instead)

Tested on a variety of amd and i386 hardware by myself and deraadt@

ok deraadt


# 1.85 28-Jun-2010 jsing

Move disk_attach() to the end of the attach functions, at which point the
disk is now ready to handle I/O.

ok krw@ dlg@


# 1.84 07-Jun-2010 jsg

partial backout of 1.81 as invoking standby in shutdown
breaks at least socppc and armish


# 1.83 26-May-2010 thib

Reintroduce bufqs. A few changes since it was backed out after some good
comments from dlg@.

No need for a separate bufq.h, keep all of in buf.h; As requested by kittens
and deraadt.

Only sd(4) and wd(4) for now. The rest of the drivers will be converted soon,
also other goodies like heuristics for sd(4) for selecting the bufq type and
the death of disksort() are forthcoming.

Tested on: i386, amd64, sparc64, macppc, loongson and alpha by myself and
phessler.

OK art@, beck@, kettenis@, oga@


# 1.82 24-May-2010 kettenis

Flush cache before suspend.

ok krw@, marco@


# 1.81 23-May-2010 kettenis

Place drive in standby mode before shutdown. Avoids the loud click heard on
many laptops when powering them down.

ok jsg@, oga@, krw@


# 1.80 18-May-2010 dlg

dont let sys/ioctl.h imply that you get the ioctls in dkio.h. this
gets rid of #include <sys/dkio.h> in sys/ioctl.h and adds #include
<sys/dkio.h> to the places that actually want and use the disk
ioctls.

this became an issue when krw@'s X build failed when he was testing
a change to dkio.h.
tested by krw@
help from and ok miod@


# 1.79 23-Apr-2010 jsing

Recycle unused disklabel fields in order to create a disklabel unique
identifier, allowing the disk to be identified without relying on the
device name.

ok deraadt@ krw@ beck@ marco@ todd@


# 1.78 11-Apr-2010 kettenis

Send "STANDBY IMMEDIATE" command to ATA disks upon suspend. For this to work,
pciide(4) needs to formward DVACT_SUSPEND and DVACT_RESUME events to its
children, so do that.

Gets rid of the nasty "click" sound from the disk on many laptops.

ok marco@, jsg@


Revision tags: OPENBSD_4_7_BASE
# 1.77 13-Oct-2009 pirofti

Get rid of devact enum, substitute it with an int and coresponding defines.

This is needed for the addition of further suspend/resume actions.

Okay deraadt@, marco@.


# 1.76 13-Aug-2009 deraadt

Replace the error strings that were being passed around with much simpler
errnos. Note that the error strings are being ignored, since we long ago
decided to not spam the console, and there is no other nice way to use the
errors (without changing the ioctls to pass it back)
The errno is now useful, since we can pass b_error from failing IO up, and
the drive can decide how to use that
ok miod


Revision tags: OPENBSD_4_6_BASE
# 1.75 17-Jun-2009 thib

Revert bufq's. this is inline with the major midlayer reverts that
have been going on. this appears to bring us back to stable state.

lots of testing by oga and ariane and my self.


# 1.74 03-Jun-2009 thib

add a flexible buffer queue (bufq) api, based on the never used
one by tedu@. It doesn't do anything smart yet, it just uses
plain old disksort. we also keep the old method of queueing bufs
since some miods have crazy MD drivers that need some love.

ok beck@, art@
tested by many on many archs.


Revision tags: OPENBSD_4_5_BASE
# 1.73 08-Nov-2008 chl

remove dead stores and newly created unused variables.

Found by LLVM/Clang Static Analyzer.

ok krw@


# 1.72 10-Sep-2008 blambert

Convert timeout_add() calls using multiples of hz to timeout_add_sec()

Really just the low-hanging fruit of (hopefully) forthcoming timeout
conversions.

ok art@, krw@


Revision tags: OPENBSD_4_4_BASE
# 1.71 27-Jun-2008 ray

More removal of clauses 3 and 4 from NetBSD licenses.

OK deraadt@ and millert@


# 1.70 15-Jun-2008 krw

Don't bypass partition bounds check for RAW_PART. We now guarantee that
RAW_PART will always be 0 -> disksize, so the bounds check will always
pass for i/o's to valid addresses. Now the i/o will be properly truncated
if it goes past the end of the device. This prevents various adverse
impacts of issuing i/o's for data past the end of the device.

Repeatedly requested by todd@.

ok weingart@ deraadt@


Revision tags: OPENBSD_4_3_BASE
# 1.69 07-Feb-2008 sthen

enable DMA for 1-sector devices (i.e. most flash devices).
from NetBSD

ok krw thib henning, tested by many


# 1.68 05-Dec-2007 jsg

Switch set features commands to use wdc_exec_command() to allow
them to be properly polled.

Fixes previously problematic machines of form and ckuethe.

ok marco@


# 1.67 26-Nov-2007 jsg

Enable read ahead and write cache enable if supported by
the disk rather than depending on the disk to have it on.

A bunch of people have suggested this should go in.


Revision tags: OPENBSD_4_2_BASE
# 1.66 20-Jun-2007 deraadt

b_cylinder does not need to be set on the callpath down into drivers.
cpu_disklabel can go away, since nothing anymore needs to use it; ok miod


# 1.65 18-Jun-2007 deraadt

avoid modification race in DIOCRLDINFO; ok krw miod


# 1.64 08-Jun-2007 deraadt

all drivers should spoof version 1 labels


# 1.63 07-Jun-2007 ray

strncpy -> strlcpy, plus a little spacing line up.

OK deraadt.


# 1.62 06-Jun-2007 deraadt

now that all partition size/offsets are potentially 64-bit, change the
type of all variables to daddr64_t. this includes the APIs for XXsize()
and XXdump(), all range checks inside bio drivers, internal variables
for disklabel handling, and even uvm's swap offsets. re-read numerous
times by otto, miod, krw, thib to look for errors


# 1.61 05-Jun-2007 deraadt

use six new macros to access & store the 48-bit disklabel fields related
to size. tested on almost all machines, double checked by miod and krw
next comes the type handling surrounding these values


# 1.60 01-Jun-2007 krw

Don't initialize d_partitions[RAW_DISK] just before calling
readdisklabel(), since all readdisklabel()'s do that already.

ok deraadt@


# 1.59 31-May-2007 krw

Kill old, no longer necessary kludge to try fooling readdisklabel()
into using DOS geometry by calling it twice. And don't ignore the
drive state if readdisklabel() returns a non-NULL value.

ok weingart@ grange@


# 1.58 28-Apr-2007 krw

Fix comments above DISKMINOR uses. DISKMINOR provides minor not major
number (wd.c), and the uses are to find the minimum minor to be
detached.


# 1.57 27-Apr-2007 krw

Nuke WDUNIT/WDPART/etc. defines and just use standard DISKUNIT/DISKPART/etc.
ones. No change to wd.o.

ok deraadt@ marco@


# 1.56 26-Apr-2007 krw

Eliminate CPU_BIOS from userland and wd(4) by always using the BIOS
geometry in the disklabel when there is a BIOS geometry to provide.
This removes the option to set a disklabel to 'BIOS' geometry via the
'g b' command in the editor.

Makes reported geometry more consistant and moves MD code to MD land
where it should be.

Doc help from jmc@, Feedback from millert@, marco@, weingart@,
kettenis@.

ok deraadt@


# 1.55 27-Mar-2007 deraadt

OK, I understand why jsg wanted the disk_unbusy() in wdretry(), and he
was right. There is another path that ends up avoiding the disk_unbusy
call I inserted.... tested by robert too


# 1.54 25-Mar-2007 deraadt

wdc retries (including after suspends) caused a disk to remain busy when
it should not; ok dlg
problem spotted using zaurus soft-drive-led diff which is pending
i think i convinced jsg now that this fix is better than putting it
in wdrestart


Revision tags: OPENBSD_4_1_BASE
# 1.53 15-Feb-2007 krw

Don't print the error strings returned by readdisklabel(). If you
need the debug info uncomment the printf's you need. Crude but
effective way to suppress 'no disklabel' errors that pop up at the
most innconvenient times to frighten users. More elegant method,
DPRINTF-like constructs or something, later.

"Yay!" marco@ ok deraadt@


# 1.52 06-Feb-2007 krw

De-register. No object code change.

From Bret Lambert.


# 1.51 17-Oct-2006 krw

For non-CPU_BIOS architectures calculate the number of cylinders on
a disk rather than accepting the cylinder count provided by the disk
or controller. This cylinder count will be '16383' for any disk >8.4G
according to the ATA spec. CPU_BIOS on i386/amd64 has magic to deal
with this, but other archs do not need to be restricted by the needs
of PC BIOS.

Fixes the default MBR OpenBSD partition size and disklabel on non-CPU_BIOS
archtitectures.

No change to behaviour on i386/amd64 machines.

Noted by Stuart Henderson on his Thecus.

Testing by various, including 'old i386 machines' nick@

ok tom@ pedro@ weingart@ deraadt@


# 1.50 04-Oct-2006 krw

Zap bad144 bad sector info in disklabels. Sparsely used if at all for
a few obsolete and easily replaced disk models. And didn't work anyway
according to Bob, who has tried.

ok beck@


Revision tags: OPENBSD_4_0_BASE
# 1.49 21-Aug-2006 krw

Delete #ifdef/#ifndef __OpenBSD__ chunks, leaving just the code we
use. Easier to read and eliminates a potentially confusing unused call
to wdgetdisklabel(). No change to object files.

ok brad@ dlg@ grange@ deraadt@ marco@


# 1.48 12-Aug-2006 krw

Setting d_secsize to DEV_BSIZE (or 1 << DEV_BSHIFT, or 512) and then
setting RAW_PART's p_size to d_secperunit * (d_secsize / DEV_BSIZE) is
a waste of a few ops. And p_size should be in sectors anyway.

Just set RAW_PART's p_size to d_secperunit to make usage consistant
across the tree.

Should be a no-op.


# 1.47 05-Mar-2006 krw

Remove more NRND NetBSDisms.

ok marco@


Revision tags: OPENBSD_3_9_BASE
# 1.46 21-Jan-2006 miod

Invoke disk_detach() and related cleanup work in detach(), rather than
zeroref() - just to be on the safe side, should we mess up our ref count.


# 1.45 09-Nov-2005 uwe

Don't fall back to fake geometry if ata_get_params() fails.
"looks sane" grange@; tested by niall@ krw@ jmc@ jolan@


Revision tags: OPENBSD_3_8_BASE
# 1.44 01-Sep-2005 uwe

On zaurus, don't fall back to fake geometry if ata_get_params() fails,
which can happen if the integrated microdrive's DRQ is not clear. This
fixes the drive geometry in the default disklabel.
Help from weingart@ and grange@; "mention `grange and i will fix this
better after 3.8 or we will be flailed'" deraadt@


# 1.43 27-Aug-2005 jsg

Use the ATA security FREEZE LOCK command on disks before
they are presented to the rest of the system to stop
a password being set that could deny access to the disk.
ok grange@ deraadt@


# 1.42 15-May-2005 grange

Use FLUSHCACHE EXT instead of FLUSHCACHE for LBA48 operations.
First noticed by christos@netbsd (iirc) a long time ago.

Tested by canacar@ and krw@.


Revision tags: OPENBSD_3_6_BASE OPENBSD_3_7_BASE
# 1.41 06-Jul-2004 aaron

Replace magic number with a more descriptive #define. This magic number was
particularly vile because it looks like a typo (i.e., looks like one "f" is
missing), but it's really not. I happened to notice this myself, but the fix
is inspired by NetBSD in the interest of keeping the code similar.

deraadt@ ok


Revision tags: OPENBSD_3_5_BASE SMP_SYNC_A SMP_SYNC_B
# 1.40 03-Mar-2004 tedu

remove bufq support until i figure out just what's going wrong. ok deraadt@


# 1.39 15-Feb-2004 tedu

new arg to disk_unbusy, to record separate read/write statistics.
looked at by various, testing henning@ mcbride@ dan weeks
mostly from netbsd via Pedro Martelletto <pbastos@rdc.puc-rio.br>


# 1.38 02-Feb-2004 tedu

as grange pointed out, BUFQ_ALLOC might fail


# 1.37 21-Jan-2004 tedu

dynamic bufq support. basically a nullop. ok grange@


# 1.36 16-Jan-2004 grange

Do not print CHS info for LBA drives, it's useless for drives with
capacity > 8GB. Total number of cylinders is enough.

ok deraadt@


# 1.35 15-Jan-2004 grange

ansi and some spaces; no f/c


# 1.34 21-Oct-2003 jmc

typos from Tom Cosgrove;


# 1.33 28-Sep-2003 grange

kill trailing spaces, no functional changes


Revision tags: OPENBSD_3_4_BASE
# 1.32 20-Jul-2003 grange

Use LBA48 only when accessing blocks after 137Gb limit or using sectors
count more than 255 to reduce registers writting overhead. Gives me
about 5% perfomance increase in a dumb synthetic test.

Some input and ok from costa@ a long time ago.


# 1.31 25-Jun-2003 tedu

implement new means of manipulating buf queues, bufq.
accessed with BUFQ macros, bufq structs support extensible, potentially
changable algorithms and queue formats. the current default scheme
should support nice priority based queuing, but is missing some vfs_bio.c
support.
only on wd.c for now, other drivers are easy converts.
as a side bonus, this makes the driver code look cleaner.
idea for the name comes from netbsd, but this scheme is incompatible.
thanks to various people for testing.
ok grange@


# 1.30 20-May-2003 tedu

change spaces to tabs so i can mail grange diffs :)
ok grange@


Revision tags: UBC_SYNC_A
# 1.29 09-Apr-2003 ho

sprintf->snprintf. miod@, grange@ ok.


# 1.28 06-Apr-2003 krw

Some easy strcpy elimination.

a) strcpy -> strncpy for copies into disklabel fields d_packname and
d_typename.

b) '16' -> 'sizeof d_packname' or 'sizeof d_typename' in existing
strncpy's.

ok millert@


Revision tags: OPENBSD_3_3_BASE
# 1.27 21-Feb-2003 grange

Fix Manuel Bouyer's copyright, from NetBSD
ok deraadt@


# 1.26 19-Dec-2002 grange

Protect WDCDEBUG_PRINT macro with do {} while (0)
ok costa@


Revision tags: OPENBSD_3_2_BASE UBC_SYNC_B
# 1.25 24-May-2002 art

protect biodone with splbio.


# 1.24 03-May-2002 gluk

LBA48 support.
Adopted from NetBSD by Alexander Yurchenko <grange@openbsd.ru>.

costa@ ok.


Revision tags: OPENBSD_3_1_BASE
# 1.23 14-Mar-2002 millert

Final __P removal plus some cosmetic fixups


# 1.22 14-Mar-2002 millert

First round of __P removal in sys


# 1.21 07-Jan-2002 mickey

pass the proc* down the ioctl chain to controller; mjacob@ csapuntz@ ok


Revision tags: UBC_BASE
# 1.20 06-Nov-2001 miod

branches: 1.20.2;
Replace inclusion of <vm/foo.h> with the correct <uvm/bar.h> when necessary.
(Look ma, I might have broken the tree)


Revision tags: OPENBSD_3_0_BASE
# 1.19 06-Aug-2001 miod

Remove legacy __BDEVSW_DUMP_OLD_TYPE code.


# 1.18 31-Jul-2001 csapuntz

Go down only one Ultra DMA mode at a time. Thanks to Matt Evans for
suggesting this fix.

Update atapiscsi Ultra DMA downgrade logic.


# 1.17 12-Jul-2001 csapuntz

extra wddone caused biodone panics occasionally


# 1.16 30-Apr-2001 csapuntz

Move ioctl stuff to low-level driver


Revision tags: OPENBSD_2_9_BASE
# 1.15 02-Apr-2001 csapuntz

ATA_POLL should have been AT_POLL

Flushcache on shutdown used to pass both AT_POLL and AT_WAIT. The two flags
do not make sense together.

Should fix panic when shutting down from ddb


# 1.14 25-Jan-2001 todd

spelling


Revision tags: OPENBSD_2_8_BASE
# 1.13 30-Jun-2000 art

Convert to new timeout API.


Revision tags: OPENBSD_2_7_BASE
# 1.12 10-Apr-2000 csapuntz

Added support for wd detach (merge from NetBSD). Support for
SCSI/ATAPI detach is not here yet.

Minor cleanup of wdc. Downgrade to UDMA mode 1 before going further.
Want to stay in UDMA modes because they're more error-resilient due to
a CRC.

Got rid of some of the ridiculous amount of softc sharing going on.
Hopefully, this will make the life of whoever goes in and fixes the
ref-counting to be correct easier.


# 1.11 07-Apr-2000 niklas

remove statics


Revision tags: SMP_BASE kame_19991208
# 1.10 17-Nov-1999 csapuntz

branches: 1.10.2;


New interface to ATA registers that goes through the lower-level device
driver (pciide, wdc, etc.)

Remove #define WDCDEBUG from top of files

More fixes to ATAPISCSI logic:

Not all devices transition correctly between phases. Devices are supposed
to keep BSY high until they've set the registers to the next sensible
state. Some devices drop BSY and leave the registers in an old or
nonsense state. Our polling code is extremely sensitive to this
(though an early itnerrupt could also observe this). So, if the device is
in an unexpected state, the new polling code waits for a while in the hope
that it enters a better state.

This seems to fix many of the problems reported.

Also, there was a horrible bug which would cause sense to fail on a ATAPI
command that sent data to the drive


Revision tags: OPENBSD_2_6_BASE
# 1.9 09-Oct-1999 csapuntz

Fix printout at attach time. Print PIO/DMA mode information after having
printed line with the name of hard disk.


# 1.8 09-Oct-1999 csapuntz

wdc layer work:
Separate wdc_probe_caps into wdc_probe_caps and wdc_print_caps for more
flexibility in printing capability information.

Get rid of wdc_final_attach.

Include name of device (e.g. cd0), if possible, on errors.

atapiscsi layer work:
Put a pointer to the SCSI device into ata_drive_datas' drv_softc field

Simplify, simplify, simplify. Got rid of a bunch of fields in atapiscsi_softc

Delay printing capabilities until we know the real device name (e.g. st0)


# 1.7 15-Aug-1999 millert

o Correctly set type and typename in driver-generated label
Should really use viscopy() when copying atap_model -> d_packname.
When wd's become sd's this will be trivial (viscopy() lives in sd.c)


# 1.6 10-Aug-1999 deraadt

support DIOCRLDINFO


# 1.5 05-Aug-1999 niklas

Make IDE compile on alpha again:
use b* instead of mem* functions, adhere to NISADMA for DMA support.


# 1.4 22-Jul-1999 deraadt

shorten dmesg output


# 1.3 22-Jul-1999 csapuntz

Fix bad144intern.


# 1.2 20-Jul-1999 csapuntz

126 -> NBT_BAD

Fix disklabel/bad sectors stuff in wd


# 1.1 18-Jul-1999 csapuntz

Import of NetBSD ATA/IDE stuff.

Introduction of home-grown

To enable this stuff in your configuration, look at the NEWATA conf file
and go through

dev/isa/files.isa
dev/pci/files.pci
conf/files
arch/i386/conf/files.i386

and follow the instructions on commenting/uncommenting stuff


# 1.127 23-Jan-2020 tedu

we've been using LBA48 for < 137GB drives for 17 years, so perhaps
we can remove the XXX now.


Revision tags: OPENBSD_6_6_BASE
# 1.126 07-Jul-2019 kn

Use timeout_add_msec(9)

RECOVERYTIME is twice per second, so sleep for 500 milliseconds
and reflect the unit to be clear.

OK patrick


Revision tags: OPENBSD_6_3_BASE OPENBSD_6_4_BASE OPENBSD_6_5_BASE
# 1.125 30-Dec-2017 guenther

Don't pull in <sys/file.h> just to get fcntl.h

ok deraadt@ krw@


# 1.124 14-Dec-2017 deraadt

Repair flushcache/standby operations to not convert AT_WAIT operations
to AT_POLL, which some chip driver underneath cannot handle.
tested by bluhm also


# 1.123 24-Oct-2017 jsg

remove defines for ioctls the kernel doesn't recognise
ok mpi@


Revision tags: OPENBSD_6_2_BASE
# 1.122 29-May-2017 sf

Add an ioctl to tell storage devices to flush their internal caches

Currently implemented for wd and sd.
Initially ported from netbsd by pedro@

ok deraadt@


# 1.121 04-May-2017 deraadt

Also pass the blk offset to disk_unbusy(), so that it can pass it to
the random subsystem as entropy. This value is pretty much unknown,
and anyways our entropy input ring does not saturate from knowns.
ok mikeb djm


Revision tags: OPENBSD_5_9_BASE OPENBSD_6_0_BASE OPENBSD_6_1_BASE
# 1.120 20-Jan-2016 stefan

Set segflg member of uio struct with symbolic enum value
instead of using magic numbers. No binary change.

Diff from Martin Natano.

ok deraadt@


# 1.119 26-Aug-2015 deraadt

trivial size to free()


# 1.118 17-Aug-2015 krw

The 80's called and want their interface back. Remove last fragments of
ST-506 support. Remove redundant 'precomp' variables/fields and rename
remaining few to 'features' to be consistant with the rest of the code.

Consensus that no ST-506 disks (requiring ISA cards, <120MB, usually
rife with bad sectors that can't be worked around) are still running
OpenBSD.


Revision tags: OPENBSD_5_7_BASE OPENBSD_5_8_BASE
# 1.117 14-Sep-2014 jsg

remove uneeded proc.h includes
ok mpi@ kspillner@


Revision tags: OPENBSD_5_6_BASE
# 1.116 12-Jul-2014 tedu

add a size argument to free. will be used soon, but for now default to 0.
after discussions with beck deraadt kettenis.


# 1.115 10-Jul-2014 deraadt

remove most of the boolean_t infection outside uvm/ddb/pmap; ok jsg


Revision tags: OPENBSD_5_5_BASE
# 1.114 14-Oct-2013 krw

Use DL_SECTOBLK() instead of handrolling same calculation to get
wdsize() return value.


# 1.113 14-Oct-2013 krw

XXsize() returns daddr_t, so calculate the return value in a daddr_t
variable and not (usually) an int.

ok miod@


# 1.112 27-Sep-2013 krw

Try harder to ensure wddump() doesn't decrement the count
of sectors to write by one value and increment the address
the next i/o will start at by a different amount.


# 1.111 15-Sep-2013 krw

Make it clearer that sc_wdc_bio.blkno is actuall a disk hardware
address by using the DL_BLKTOSEC() and DL_SECTOBLK() defines rather
than handrolling the equivalent. Fixes current addition of a 512-byte
block value (b_blkno) to a disk hardware address (DL_GETPOFFSET()).

Tested on real wd(4) hardware by guenther@.


Revision tags: OPENBSD_5_4_BASE
# 1.110 11-Jun-2013 deraadt

final removal of daddr64_t. daddr_t has been 64 bit for a long enough
test period; i think 3 years ago the last bugs fell out.
ok otto beck others


Revision tags: OPENBSD_5_3_BASE
# 1.109 08-Oct-2012 deraadt

Revamp the sequences for suspend/hibernate -> resume so that the code
paths are reflexive. It is now possible to fail part-way through a
suspend sequence, and recover along the resume code path.
Split DVACT_SUSPEND by adding a new DVACT_POWERDOWN method is used
after hibernate (and suspend too) to finish the job. Some drivers
must be converted at the same time to use this instead of shutdown hooks
(the others will follow at a later time)
ok kettenis mlarkin


Revision tags: OPENBSD_5_0_BASE OPENBSD_5_1_BASE OPENBSD_5_2_BASE
# 1.108 06-Jul-2011 matthew

Eliminate redundant buf validation checks in xxstrategy() methods now
that they're implemented consistently in bounds_check_with_label().

Also, per krw's request, change bounds_check_with_label() to return 0
if the checks succeed, and change the drivers to test == -1 instead of
<= 0. (Man page update to follow; intentionally omitting
arch/vax/mba/hp.c from this commit because it doesn't even build
currently and miod@ promises to kill it soon.)

ok krw@


# 1.107 30-Jun-2011 matthew

Refactor some common open/close/detach disk driver code into
subr_disk.c. For now just the MI disk drivers.

ok deraadt@, krw@; jsing@ liked the approach too


# 1.106 20-Jun-2011 matthew

Call bufq_drain() in wddetach() instead of reimplementing the same
logic.


# 1.105 19-Jun-2011 deraadt

Use disk_lock_nointr() in the xxclose() routines so that they cannot
be interrupted.
ok matthew


# 1.104 19-Jun-2011 deraadt

Use disk_lock/disk_unlock directly and in the same way in these drivers,
rather than using various wrappings. Convert vnd to using the sc_dk
rwlock instead of using one of its own.
ok matthew


# 1.103 05-Jun-2011 matthew

Drop kernel support for the useless DIOCWLABEL ioctl and prune a lot
of silly flag twiddling code in various disk drivers.

ok deraadt@, miod@

N.B., users will need a -current disklabel(8) to be able to write new
disklabels to disk now.


# 1.102 03-Jun-2011 matthew

Get rid of the wlabel argument to bounds_check_with_label(). It's
never done anything in OpenBSD and just clutters disk drivers with
silly flag handling.

More cleanup to follow.

ok deraadt@, millert@; no objections krw@


# 1.101 31-May-2011 matthew

Change a few of the more common disk drivers (sd, cd, wd, rd, and vnd)
to return EBUSY if the user tries to modify an open partition's offset
or size. Only sadness can result if a user tries this, and rejecting
it prevents a race between sdstart() and sdstrategy().

Curiously, there was already code in the kernel and in disklabel(8) to
detect/handle this, but it was effectively disabled because the disk
drivers always used something like "/* sc->sc_dk.dk_openmask */ 0",
and this commented out code has existed since even r1.1 in NetBSD.

I had no problems building a release and messing around with
disklabel(8) for a bit with this diff. Canarying the more common MI
disk drivers until we gain confidence that there aren't any
regressions, then we can switch the remaining drivers.

"I am surprised you got me convinced that this stuff is safe" deraadt@
ok krw@


# 1.100 18-Apr-2011 deraadt

keep an eye out for wdc registers returning 0xff. if they return 0xff,
this probably means the controller is dead -- as in, a hot plug eject
event. in that case, mark the driver dead and try harder to return -1
in the interrupt handler.
tested by many
ok dlg, miod did not object


# 1.99 15-Apr-2011 miod

Change wdc_reset_channel() to take a `no wait' argument. Pass in turn this
argument to wdcreset(), to have it skip waiting until active channels see
their BUSY bit clear in the status register.

Use this feature in the resume path, during the first reset operation. The
first reset is supposed to only wake up the controller, and the disks don't
come back until the second reset is issued, therefore waiting for them to
report themselves as ready after the first reset, but before the second, is
moot - and as a matter of fact some controllers, such as the AMD 754 and
clones/offspring (e.g. Geode) keep the BUSY bit asserted after the first
reset.

Last, but not least, make sure wd@ata invokes wd_get_params() again before
returning from the resume code, as we will still be using polled transfers
for a short while.

This causes the Lemote Yeelong to resume within less than one second, instead
of the lousy 30 seconds wait between the two resets; and the wd_get_params()
voodoo prevents it from getting spurious ide interrupts afterwards.

wd_get_params() magic from dlg; rest of the work by yours truly after enough
prodding by dlg@ and pirofti@, among others. ok deraadt@ dlg@


# 1.98 05-Apr-2011 deraadt

wd_hibernate_io() is a standalone disk io writer which tries to not
damage kernel memory very much while doing the job. It is not very
pretty yet, but improving it will need some MI work.
ok mlarkin jsg dlg


Revision tags: OPENBSD_4_9_BASE
# 1.97 31-Dec-2010 kettenis

Reintroduce the code that puts the drive into standby in shutdown, but
only do this if we're going to power down the machine.

"My god, it is a 3 line diff. Commit it before it gets bigger!" deraadt@


# 1.96 30-Dec-2010 kettenis

The NVIDIA MCP89 SATA controller found on the 2010 Apple 13" MacBook Pro
can't address block 0xffffffff in LBA mode, at least in combination with
the Hitachi HTS545032B9SA02 and HTS545050B9SA02 drives that Apple ships.
So use LBA48 mode from that block onwards.

ok marco@, deraadt@, miod@


# 1.95 22-Sep-2010 matthew

All users of physio(9) now pass NULL as the buf pointer argument, so
no point in keeping it around.

"i like this" thib@ (a while back); ok krw@ and oga@; reminder to
update the man page and tweaks jmc@


# 1.94 20-Sep-2010 deraadt

DVACT_ACTIVATE and DVACT_DEACTIVATE do nothing here; remove
the false comments.


# 1.93 08-Sep-2010 jsing

Introduce a disk_lookup() function which calls device_lookup(), before
verifying that the resulting device is present on the disklist. This
avoids a race whereby the disk driver can be accessed as soon as the
softc has been allocated, but before the disk has completed
initialisation and has called disk_attach() (up until this point
dk_label is still a null pointer).

Cut cd(4), sd(4) and wd(4) across to disk_lookup(). All callers of
disk_attach() need to be tested and cut over in due course.

ok deraadt@ krw@


# 1.92 08-Sep-2010 jsing

Store a struct device pointer within struct disk and populate this when
disk_attach() is called by the device driver. We will be building on
this shortly.

ok deraadt@ krw@


# 1.91 01-Sep-2010 dlg

make struct bufq a member of the softc for devices that use it,
rather than it being a pointer to something that needs to be allocated
at attach. since all these devices need a bufq to operate, it makes
sense to have it allocated as part of the softc and get bufq_init
to just initialise all its fields. it also gets rid of the possibility
that you wont be able to allocate the bufq struct during attach,
which is something you dont want to happen.

secondly, it consistently implements a split between wrapper functions
and the per discipline implementation of the bufq handlers. it
consistently does the locking in the wrappers rather than doing
half in the wrappers and the other half in the implementations.

it also consistently handles the outstanding bufq bq pointer in the
wrappers.

this hides most of the implementation inside kern_bufq.c. the only
stuff left in buf.h is for the bits each implementation needs to
put inside struct buf.

tested by thib@ krw@ and me
ok thib@ matthew@
no objection from krw@


# 1.90 31-Aug-2010 deraadt

sort DVACT_ actions in order of use


# 1.89 28-Aug-2010 matthew

Garbage collect struct dkdriver.

ok miod@; "please go ahead" jsing@


Revision tags: OPENBSD_4_8_BASE
# 1.88 23-Jul-2010 jsg

Manuel Bouyer rescinded clauses 3 and 4 of his license text.


# 1.87 22-Jul-2010 kettenis

Give the drive a little bit more time to complete the STANDBY IMMED command.
The ATA spec seems to suggest it may take up to 30s. This seems a little bit
long. To quote Theo: "by that time some drives have probably parked their own
heads in boredom". But it shouldn't hurt unless your drive is seriously
broken. Makes some laptops suspend much better.

ok deraadt@


# 1.86 22-Jul-2010 mlarkin

First part of better pciide resume. Wake the channel by performing two
resets separated by a small delay (a delay intended to offend jsg who will
definitely want to fix it by checking for RDY instead)

Tested on a variety of amd and i386 hardware by myself and deraadt@

ok deraadt


# 1.85 28-Jun-2010 jsing

Move disk_attach() to the end of the attach functions, at which point the
disk is now ready to handle I/O.

ok krw@ dlg@


# 1.84 07-Jun-2010 jsg

partial backout of 1.81 as invoking standby in shutdown
breaks at least socppc and armish


# 1.83 26-May-2010 thib

Reintroduce bufqs. A few changes since it was backed out after some good
comments from dlg@.

No need for a separate bufq.h, keep all of in buf.h; As requested by kittens
and deraadt.

Only sd(4) and wd(4) for now. The rest of the drivers will be converted soon,
also other goodies like heuristics for sd(4) for selecting the bufq type and
the death of disksort() are forthcoming.

Tested on: i386, amd64, sparc64, macppc, loongson and alpha by myself and
phessler.

OK art@, beck@, kettenis@, oga@


# 1.82 24-May-2010 kettenis

Flush cache before suspend.

ok krw@, marco@


# 1.81 23-May-2010 kettenis

Place drive in standby mode before shutdown. Avoids the loud click heard on
many laptops when powering them down.

ok jsg@, oga@, krw@


# 1.80 18-May-2010 dlg

dont let sys/ioctl.h imply that you get the ioctls in dkio.h. this
gets rid of #include <sys/dkio.h> in sys/ioctl.h and adds #include
<sys/dkio.h> to the places that actually want and use the disk
ioctls.

this became an issue when krw@'s X build failed when he was testing
a change to dkio.h.
tested by krw@
help from and ok miod@


# 1.79 23-Apr-2010 jsing

Recycle unused disklabel fields in order to create a disklabel unique
identifier, allowing the disk to be identified without relying on the
device name.

ok deraadt@ krw@ beck@ marco@ todd@


# 1.78 11-Apr-2010 kettenis

Send "STANDBY IMMEDIATE" command to ATA disks upon suspend. For this to work,
pciide(4) needs to formward DVACT_SUSPEND and DVACT_RESUME events to its
children, so do that.

Gets rid of the nasty "click" sound from the disk on many laptops.

ok marco@, jsg@


Revision tags: OPENBSD_4_7_BASE
# 1.77 13-Oct-2009 pirofti

Get rid of devact enum, substitute it with an int and coresponding defines.

This is needed for the addition of further suspend/resume actions.

Okay deraadt@, marco@.


# 1.76 13-Aug-2009 deraadt

Replace the error strings that were being passed around with much simpler
errnos. Note that the error strings are being ignored, since we long ago
decided to not spam the console, and there is no other nice way to use the
errors (without changing the ioctls to pass it back)
The errno is now useful, since we can pass b_error from failing IO up, and
the drive can decide how to use that
ok miod


Revision tags: OPENBSD_4_6_BASE
# 1.75 17-Jun-2009 thib

Revert bufq's. this is inline with the major midlayer reverts that
have been going on. this appears to bring us back to stable state.

lots of testing by oga and ariane and my self.


# 1.74 03-Jun-2009 thib

add a flexible buffer queue (bufq) api, based on the never used
one by tedu@. It doesn't do anything smart yet, it just uses
plain old disksort. we also keep the old method of queueing bufs
since some miods have crazy MD drivers that need some love.

ok beck@, art@
tested by many on many archs.


Revision tags: OPENBSD_4_5_BASE
# 1.73 08-Nov-2008 chl

remove dead stores and newly created unused variables.

Found by LLVM/Clang Static Analyzer.

ok krw@


# 1.72 10-Sep-2008 blambert

Convert timeout_add() calls using multiples of hz to timeout_add_sec()

Really just the low-hanging fruit of (hopefully) forthcoming timeout
conversions.

ok art@, krw@


Revision tags: OPENBSD_4_4_BASE
# 1.71 27-Jun-2008 ray

More removal of clauses 3 and 4 from NetBSD licenses.

OK deraadt@ and millert@


# 1.70 15-Jun-2008 krw

Don't bypass partition bounds check for RAW_PART. We now guarantee that
RAW_PART will always be 0 -> disksize, so the bounds check will always
pass for i/o's to valid addresses. Now the i/o will be properly truncated
if it goes past the end of the device. This prevents various adverse
impacts of issuing i/o's for data past the end of the device.

Repeatedly requested by todd@.

ok weingart@ deraadt@


Revision tags: OPENBSD_4_3_BASE
# 1.69 07-Feb-2008 sthen

enable DMA for 1-sector devices (i.e. most flash devices).
from NetBSD

ok krw thib henning, tested by many


# 1.68 05-Dec-2007 jsg

Switch set features commands to use wdc_exec_command() to allow
them to be properly polled.

Fixes previously problematic machines of form and ckuethe.

ok marco@


# 1.67 26-Nov-2007 jsg

Enable read ahead and write cache enable if supported by
the disk rather than depending on the disk to have it on.

A bunch of people have suggested this should go in.


Revision tags: OPENBSD_4_2_BASE
# 1.66 20-Jun-2007 deraadt

b_cylinder does not need to be set on the callpath down into drivers.
cpu_disklabel can go away, since nothing anymore needs to use it; ok miod


# 1.65 18-Jun-2007 deraadt

avoid modification race in DIOCRLDINFO; ok krw miod


# 1.64 08-Jun-2007 deraadt

all drivers should spoof version 1 labels


# 1.63 07-Jun-2007 ray

strncpy -> strlcpy, plus a little spacing line up.

OK deraadt.


# 1.62 06-Jun-2007 deraadt

now that all partition size/offsets are potentially 64-bit, change the
type of all variables to daddr64_t. this includes the APIs for XXsize()
and XXdump(), all range checks inside bio drivers, internal variables
for disklabel handling, and even uvm's swap offsets. re-read numerous
times by otto, miod, krw, thib to look for errors


# 1.61 05-Jun-2007 deraadt

use six new macros to access & store the 48-bit disklabel fields related
to size. tested on almost all machines, double checked by miod and krw
next comes the type handling surrounding these values


# 1.60 01-Jun-2007 krw

Don't initialize d_partitions[RAW_DISK] just before calling
readdisklabel(), since all readdisklabel()'s do that already.

ok deraadt@


# 1.59 31-May-2007 krw

Kill old, no longer necessary kludge to try fooling readdisklabel()
into using DOS geometry by calling it twice. And don't ignore the
drive state if readdisklabel() returns a non-NULL value.

ok weingart@ grange@


# 1.58 28-Apr-2007 krw

Fix comments above DISKMINOR uses. DISKMINOR provides minor not major
number (wd.c), and the uses are to find the minimum minor to be
detached.


# 1.57 27-Apr-2007 krw

Nuke WDUNIT/WDPART/etc. defines and just use standard DISKUNIT/DISKPART/etc.
ones. No change to wd.o.

ok deraadt@ marco@


# 1.56 26-Apr-2007 krw

Eliminate CPU_BIOS from userland and wd(4) by always using the BIOS
geometry in the disklabel when there is a BIOS geometry to provide.
This removes the option to set a disklabel to 'BIOS' geometry via the
'g b' command in the editor.

Makes reported geometry more consistant and moves MD code to MD land
where it should be.

Doc help from jmc@, Feedback from millert@, marco@, weingart@,
kettenis@.

ok deraadt@


# 1.55 27-Mar-2007 deraadt

OK, I understand why jsg wanted the disk_unbusy() in wdretry(), and he
was right. There is another path that ends up avoiding the disk_unbusy
call I inserted.... tested by robert too


# 1.54 25-Mar-2007 deraadt

wdc retries (including after suspends) caused a disk to remain busy when
it should not; ok dlg
problem spotted using zaurus soft-drive-led diff which is pending
i think i convinced jsg now that this fix is better than putting it
in wdrestart


Revision tags: OPENBSD_4_1_BASE
# 1.53 15-Feb-2007 krw

Don't print the error strings returned by readdisklabel(). If you
need the debug info uncomment the printf's you need. Crude but
effective way to suppress 'no disklabel' errors that pop up at the
most innconvenient times to frighten users. More elegant method,
DPRINTF-like constructs or something, later.

"Yay!" marco@ ok deraadt@


# 1.52 06-Feb-2007 krw

De-register. No object code change.

From Bret Lambert.


# 1.51 17-Oct-2006 krw

For non-CPU_BIOS architectures calculate the number of cylinders on
a disk rather than accepting the cylinder count provided by the disk
or controller. This cylinder count will be '16383' for any disk >8.4G
according to the ATA spec. CPU_BIOS on i386/amd64 has magic to deal
with this, but other archs do not need to be restricted by the needs
of PC BIOS.

Fixes the default MBR OpenBSD partition size and disklabel on non-CPU_BIOS
archtitectures.

No change to behaviour on i386/amd64 machines.

Noted by Stuart Henderson on his Thecus.

Testing by various, including 'old i386 machines' nick@

ok tom@ pedro@ weingart@ deraadt@


# 1.50 04-Oct-2006 krw

Zap bad144 bad sector info in disklabels. Sparsely used if at all for
a few obsolete and easily replaced disk models. And didn't work anyway
according to Bob, who has tried.

ok beck@


Revision tags: OPENBSD_4_0_BASE
# 1.49 21-Aug-2006 krw

Delete #ifdef/#ifndef __OpenBSD__ chunks, leaving just the code we
use. Easier to read and eliminates a potentially confusing unused call
to wdgetdisklabel(). No change to object files.

ok brad@ dlg@ grange@ deraadt@ marco@


# 1.48 12-Aug-2006 krw

Setting d_secsize to DEV_BSIZE (or 1 << DEV_BSHIFT, or 512) and then
setting RAW_PART's p_size to d_secperunit * (d_secsize / DEV_BSIZE) is
a waste of a few ops. And p_size should be in sectors anyway.

Just set RAW_PART's p_size to d_secperunit to make usage consistant
across the tree.

Should be a no-op.


# 1.47 05-Mar-2006 krw

Remove more NRND NetBSDisms.

ok marco@


Revision tags: OPENBSD_3_9_BASE
# 1.46 21-Jan-2006 miod

Invoke disk_detach() and related cleanup work in detach(), rather than
zeroref() - just to be on the safe side, should we mess up our ref count.


# 1.45 09-Nov-2005 uwe

Don't fall back to fake geometry if ata_get_params() fails.
"looks sane" grange@; tested by niall@ krw@ jmc@ jolan@


Revision tags: OPENBSD_3_8_BASE
# 1.44 01-Sep-2005 uwe

On zaurus, don't fall back to fake geometry if ata_get_params() fails,
which can happen if the integrated microdrive's DRQ is not clear. This
fixes the drive geometry in the default disklabel.
Help from weingart@ and grange@; "mention `grange and i will fix this
better after 3.8 or we will be flailed'" deraadt@


# 1.43 27-Aug-2005 jsg

Use the ATA security FREEZE LOCK command on disks before
they are presented to the rest of the system to stop
a password being set that could deny access to the disk.
ok grange@ deraadt@


# 1.42 15-May-2005 grange

Use FLUSHCACHE EXT instead of FLUSHCACHE for LBA48 operations.
First noticed by christos@netbsd (iirc) a long time ago.

Tested by canacar@ and krw@.


Revision tags: OPENBSD_3_6_BASE OPENBSD_3_7_BASE
# 1.41 06-Jul-2004 aaron

Replace magic number with a more descriptive #define. This magic number was
particularly vile because it looks like a typo (i.e., looks like one "f" is
missing), but it's really not. I happened to notice this myself, but the fix
is inspired by NetBSD in the interest of keeping the code similar.

deraadt@ ok


Revision tags: OPENBSD_3_5_BASE SMP_SYNC_A SMP_SYNC_B
# 1.40 03-Mar-2004 tedu

remove bufq support until i figure out just what's going wrong. ok deraadt@


# 1.39 15-Feb-2004 tedu

new arg to disk_unbusy, to record separate read/write statistics.
looked at by various, testing henning@ mcbride@ dan weeks
mostly from netbsd via Pedro Martelletto <pbastos@rdc.puc-rio.br>


# 1.38 02-Feb-2004 tedu

as grange pointed out, BUFQ_ALLOC might fail


# 1.37 21-Jan-2004 tedu

dynamic bufq support. basically a nullop. ok grange@


# 1.36 16-Jan-2004 grange

Do not print CHS info for LBA drives, it's useless for drives with
capacity > 8GB. Total number of cylinders is enough.

ok deraadt@


# 1.35 15-Jan-2004 grange

ansi and some spaces; no f/c


# 1.34 21-Oct-2003 jmc

typos from Tom Cosgrove;


# 1.33 28-Sep-2003 grange

kill trailing spaces, no functional changes


Revision tags: OPENBSD_3_4_BASE
# 1.32 20-Jul-2003 grange

Use LBA48 only when accessing blocks after 137Gb limit or using sectors
count more than 255 to reduce registers writting overhead. Gives me
about 5% perfomance increase in a dumb synthetic test.

Some input and ok from costa@ a long time ago.


# 1.31 25-Jun-2003 tedu

implement new means of manipulating buf queues, bufq.
accessed with BUFQ macros, bufq structs support extensible, potentially
changable algorithms and queue formats. the current default scheme
should support nice priority based queuing, but is missing some vfs_bio.c
support.
only on wd.c for now, other drivers are easy converts.
as a side bonus, this makes the driver code look cleaner.
idea for the name comes from netbsd, but this scheme is incompatible.
thanks to various people for testing.
ok grange@


# 1.30 20-May-2003 tedu

change spaces to tabs so i can mail grange diffs :)
ok grange@


Revision tags: UBC_SYNC_A
# 1.29 09-Apr-2003 ho

sprintf->snprintf. miod@, grange@ ok.


# 1.28 06-Apr-2003 krw

Some easy strcpy elimination.

a) strcpy -> strncpy for copies into disklabel fields d_packname and
d_typename.

b) '16' -> 'sizeof d_packname' or 'sizeof d_typename' in existing
strncpy's.

ok millert@


Revision tags: OPENBSD_3_3_BASE
# 1.27 21-Feb-2003 grange

Fix Manuel Bouyer's copyright, from NetBSD
ok deraadt@


# 1.26 19-Dec-2002 grange

Protect WDCDEBUG_PRINT macro with do {} while (0)
ok costa@


Revision tags: OPENBSD_3_2_BASE UBC_SYNC_B
# 1.25 24-May-2002 art

protect biodone with splbio.


# 1.24 03-May-2002 gluk

LBA48 support.
Adopted from NetBSD by Alexander Yurchenko <grange@openbsd.ru>.

costa@ ok.


Revision tags: OPENBSD_3_1_BASE
# 1.23 14-Mar-2002 millert

Final __P removal plus some cosmetic fixups


# 1.22 14-Mar-2002 millert

First round of __P removal in sys


# 1.21 07-Jan-2002 mickey

pass the proc* down the ioctl chain to controller; mjacob@ csapuntz@ ok


Revision tags: UBC_BASE
# 1.20 06-Nov-2001 miod

branches: 1.20.2;
Replace inclusion of <vm/foo.h> with the correct <uvm/bar.h> when necessary.
(Look ma, I might have broken the tree)


Revision tags: OPENBSD_3_0_BASE
# 1.19 06-Aug-2001 miod

Remove legacy __BDEVSW_DUMP_OLD_TYPE code.


# 1.18 31-Jul-2001 csapuntz

Go down only one Ultra DMA mode at a time. Thanks to Matt Evans for
suggesting this fix.

Update atapiscsi Ultra DMA downgrade logic.


# 1.17 12-Jul-2001 csapuntz

extra wddone caused biodone panics occasionally


# 1.16 30-Apr-2001 csapuntz

Move ioctl stuff to low-level driver


Revision tags: OPENBSD_2_9_BASE
# 1.15 02-Apr-2001 csapuntz

ATA_POLL should have been AT_POLL

Flushcache on shutdown used to pass both AT_POLL and AT_WAIT. The two flags
do not make sense together.

Should fix panic when shutting down from ddb


# 1.14 25-Jan-2001 todd

spelling


Revision tags: OPENBSD_2_8_BASE
# 1.13 30-Jun-2000 art

Convert to new timeout API.


Revision tags: OPENBSD_2_7_BASE
# 1.12 10-Apr-2000 csapuntz

Added support for wd detach (merge from NetBSD). Support for
SCSI/ATAPI detach is not here yet.

Minor cleanup of wdc. Downgrade to UDMA mode 1 before going further.
Want to stay in UDMA modes because they're more error-resilient due to
a CRC.

Got rid of some of the ridiculous amount of softc sharing going on.
Hopefully, this will make the life of whoever goes in and fixes the
ref-counting to be correct easier.


# 1.11 07-Apr-2000 niklas

remove statics


Revision tags: SMP_BASE kame_19991208
# 1.10 17-Nov-1999 csapuntz

branches: 1.10.2;


New interface to ATA registers that goes through the lower-level device
driver (pciide, wdc, etc.)

Remove #define WDCDEBUG from top of files

More fixes to ATAPISCSI logic:

Not all devices transition correctly between phases. Devices are supposed
to keep BSY high until they've set the registers to the next sensible
state. Some devices drop BSY and leave the registers in an old or
nonsense state. Our polling code is extremely sensitive to this
(though an early itnerrupt could also observe this). So, if the device is
in an unexpected state, the new polling code waits for a while in the hope
that it enters a better state.

This seems to fix many of the problems reported.

Also, there was a horrible bug which would cause sense to fail on a ATAPI
command that sent data to the drive


Revision tags: OPENBSD_2_6_BASE
# 1.9 09-Oct-1999 csapuntz

Fix printout at attach time. Print PIO/DMA mode information after having
printed line with the name of hard disk.


# 1.8 09-Oct-1999 csapuntz

wdc layer work:
Separate wdc_probe_caps into wdc_probe_caps and wdc_print_caps for more
flexibility in printing capability information.

Get rid of wdc_final_attach.

Include name of device (e.g. cd0), if possible, on errors.

atapiscsi layer work:
Put a pointer to the SCSI device into ata_drive_datas' drv_softc field

Simplify, simplify, simplify. Got rid of a bunch of fields in atapiscsi_softc

Delay printing capabilities until we know the real device name (e.g. st0)


# 1.7 15-Aug-1999 millert

o Correctly set type and typename in driver-generated label
Should really use viscopy() when copying atap_model -> d_packname.
When wd's become sd's this will be trivial (viscopy() lives in sd.c)


# 1.6 10-Aug-1999 deraadt

support DIOCRLDINFO


# 1.5 05-Aug-1999 niklas

Make IDE compile on alpha again:
use b* instead of mem* functions, adhere to NISADMA for DMA support.


# 1.4 22-Jul-1999 deraadt

shorten dmesg output


# 1.3 22-Jul-1999 csapuntz

Fix bad144intern.


# 1.2 20-Jul-1999 csapuntz

126 -> NBT_BAD

Fix disklabel/bad sectors stuff in wd


# 1.1 18-Jul-1999 csapuntz

Import of NetBSD ATA/IDE stuff.

Introduction of home-grown

To enable this stuff in your configuration, look at the NEWATA conf file
and go through

dev/isa/files.isa
dev/pci/files.pci
conf/files
arch/i386/conf/files.i386

and follow the instructions on commenting/uncommenting stuff


# 1.126 07-Jul-2019 kn

Use timeout_add_msec(9)

RECOVERYTIME is twice per second, so sleep for 500 milliseconds
and reflect the unit to be clear.

OK patrick


Revision tags: OPENBSD_6_3_BASE OPENBSD_6_4_BASE OPENBSD_6_5_BASE
# 1.125 30-Dec-2017 guenther

Don't pull in <sys/file.h> just to get fcntl.h

ok deraadt@ krw@


# 1.124 14-Dec-2017 deraadt

Repair flushcache/standby operations to not convert AT_WAIT operations
to AT_POLL, which some chip driver underneath cannot handle.
tested by bluhm also


# 1.123 24-Oct-2017 jsg

remove defines for ioctls the kernel doesn't recognise
ok mpi@


Revision tags: OPENBSD_6_2_BASE
# 1.122 29-May-2017 sf

Add an ioctl to tell storage devices to flush their internal caches

Currently implemented for wd and sd.
Initially ported from netbsd by pedro@

ok deraadt@


# 1.121 04-May-2017 deraadt

Also pass the blk offset to disk_unbusy(), so that it can pass it to
the random subsystem as entropy. This value is pretty much unknown,
and anyways our entropy input ring does not saturate from knowns.
ok mikeb djm


Revision tags: OPENBSD_5_9_BASE OPENBSD_6_0_BASE OPENBSD_6_1_BASE
# 1.120 20-Jan-2016 stefan

Set segflg member of uio struct with symbolic enum value
instead of using magic numbers. No binary change.

Diff from Martin Natano.

ok deraadt@


# 1.119 26-Aug-2015 deraadt

trivial size to free()


# 1.118 17-Aug-2015 krw

The 80's called and want their interface back. Remove last fragments of
ST-506 support. Remove redundant 'precomp' variables/fields and rename
remaining few to 'features' to be consistant with the rest of the code.

Consensus that no ST-506 disks (requiring ISA cards, <120MB, usually
rife with bad sectors that can't be worked around) are still running
OpenBSD.


Revision tags: OPENBSD_5_7_BASE OPENBSD_5_8_BASE
# 1.117 14-Sep-2014 jsg

remove uneeded proc.h includes
ok mpi@ kspillner@


Revision tags: OPENBSD_5_6_BASE
# 1.116 12-Jul-2014 tedu

add a size argument to free. will be used soon, but for now default to 0.
after discussions with beck deraadt kettenis.


# 1.115 10-Jul-2014 deraadt

remove most of the boolean_t infection outside uvm/ddb/pmap; ok jsg


Revision tags: OPENBSD_5_5_BASE
# 1.114 14-Oct-2013 krw

Use DL_SECTOBLK() instead of handrolling same calculation to get
wdsize() return value.


# 1.113 14-Oct-2013 krw

XXsize() returns daddr_t, so calculate the return value in a daddr_t
variable and not (usually) an int.

ok miod@


# 1.112 27-Sep-2013 krw

Try harder to ensure wddump() doesn't decrement the count
of sectors to write by one value and increment the address
the next i/o will start at by a different amount.


# 1.111 15-Sep-2013 krw

Make it clearer that sc_wdc_bio.blkno is actuall a disk hardware
address by using the DL_BLKTOSEC() and DL_SECTOBLK() defines rather
than handrolling the equivalent. Fixes current addition of a 512-byte
block value (b_blkno) to a disk hardware address (DL_GETPOFFSET()).

Tested on real wd(4) hardware by guenther@.


Revision tags: OPENBSD_5_4_BASE
# 1.110 11-Jun-2013 deraadt

final removal of daddr64_t. daddr_t has been 64 bit for a long enough
test period; i think 3 years ago the last bugs fell out.
ok otto beck others


Revision tags: OPENBSD_5_3_BASE
# 1.109 08-Oct-2012 deraadt

Revamp the sequences for suspend/hibernate -> resume so that the code
paths are reflexive. It is now possible to fail part-way through a
suspend sequence, and recover along the resume code path.
Split DVACT_SUSPEND by adding a new DVACT_POWERDOWN method is used
after hibernate (and suspend too) to finish the job. Some drivers
must be converted at the same time to use this instead of shutdown hooks
(the others will follow at a later time)
ok kettenis mlarkin


Revision tags: OPENBSD_5_0_BASE OPENBSD_5_1_BASE OPENBSD_5_2_BASE
# 1.108 06-Jul-2011 matthew

Eliminate redundant buf validation checks in xxstrategy() methods now
that they're implemented consistently in bounds_check_with_label().

Also, per krw's request, change bounds_check_with_label() to return 0
if the checks succeed, and change the drivers to test == -1 instead of
<= 0. (Man page update to follow; intentionally omitting
arch/vax/mba/hp.c from this commit because it doesn't even build
currently and miod@ promises to kill it soon.)

ok krw@


# 1.107 30-Jun-2011 matthew

Refactor some common open/close/detach disk driver code into
subr_disk.c. For now just the MI disk drivers.

ok deraadt@, krw@; jsing@ liked the approach too


# 1.106 20-Jun-2011 matthew

Call bufq_drain() in wddetach() instead of reimplementing the same
logic.


# 1.105 19-Jun-2011 deraadt

Use disk_lock_nointr() in the xxclose() routines so that they cannot
be interrupted.
ok matthew


# 1.104 19-Jun-2011 deraadt

Use disk_lock/disk_unlock directly and in the same way in these drivers,
rather than using various wrappings. Convert vnd to using the sc_dk
rwlock instead of using one of its own.
ok matthew


# 1.103 05-Jun-2011 matthew

Drop kernel support for the useless DIOCWLABEL ioctl and prune a lot
of silly flag twiddling code in various disk drivers.

ok deraadt@, miod@

N.B., users will need a -current disklabel(8) to be able to write new
disklabels to disk now.


# 1.102 03-Jun-2011 matthew

Get rid of the wlabel argument to bounds_check_with_label(). It's
never done anything in OpenBSD and just clutters disk drivers with
silly flag handling.

More cleanup to follow.

ok deraadt@, millert@; no objections krw@


# 1.101 31-May-2011 matthew

Change a few of the more common disk drivers (sd, cd, wd, rd, and vnd)
to return EBUSY if the user tries to modify an open partition's offset
or size. Only sadness can result if a user tries this, and rejecting
it prevents a race between sdstart() and sdstrategy().

Curiously, there was already code in the kernel and in disklabel(8) to
detect/handle this, but it was effectively disabled because the disk
drivers always used something like "/* sc->sc_dk.dk_openmask */ 0",
and this commented out code has existed since even r1.1 in NetBSD.

I had no problems building a release and messing around with
disklabel(8) for a bit with this diff. Canarying the more common MI
disk drivers until we gain confidence that there aren't any
regressions, then we can switch the remaining drivers.

"I am surprised you got me convinced that this stuff is safe" deraadt@
ok krw@


# 1.100 18-Apr-2011 deraadt

keep an eye out for wdc registers returning 0xff. if they return 0xff,
this probably means the controller is dead -- as in, a hot plug eject
event. in that case, mark the driver dead and try harder to return -1
in the interrupt handler.
tested by many
ok dlg, miod did not object


# 1.99 15-Apr-2011 miod

Change wdc_reset_channel() to take a `no wait' argument. Pass in turn this
argument to wdcreset(), to have it skip waiting until active channels see
their BUSY bit clear in the status register.

Use this feature in the resume path, during the first reset operation. The
first reset is supposed to only wake up the controller, and the disks don't
come back until the second reset is issued, therefore waiting for them to
report themselves as ready after the first reset, but before the second, is
moot - and as a matter of fact some controllers, such as the AMD 754 and
clones/offspring (e.g. Geode) keep the BUSY bit asserted after the first
reset.

Last, but not least, make sure wd@ata invokes wd_get_params() again before
returning from the resume code, as we will still be using polled transfers
for a short while.

This causes the Lemote Yeelong to resume within less than one second, instead
of the lousy 30 seconds wait between the two resets; and the wd_get_params()
voodoo prevents it from getting spurious ide interrupts afterwards.

wd_get_params() magic from dlg; rest of the work by yours truly after enough
prodding by dlg@ and pirofti@, among others. ok deraadt@ dlg@


# 1.98 05-Apr-2011 deraadt

wd_hibernate_io() is a standalone disk io writer which tries to not
damage kernel memory very much while doing the job. It is not very
pretty yet, but improving it will need some MI work.
ok mlarkin jsg dlg


Revision tags: OPENBSD_4_9_BASE
# 1.97 31-Dec-2010 kettenis

Reintroduce the code that puts the drive into standby in shutdown, but
only do this if we're going to power down the machine.

"My god, it is a 3 line diff. Commit it before it gets bigger!" deraadt@


# 1.96 30-Dec-2010 kettenis

The NVIDIA MCP89 SATA controller found on the 2010 Apple 13" MacBook Pro
can't address block 0xffffffff in LBA mode, at least in combination with
the Hitachi HTS545032B9SA02 and HTS545050B9SA02 drives that Apple ships.
So use LBA48 mode from that block onwards.

ok marco@, deraadt@, miod@


# 1.95 22-Sep-2010 matthew

All users of physio(9) now pass NULL as the buf pointer argument, so
no point in keeping it around.

"i like this" thib@ (a while back); ok krw@ and oga@; reminder to
update the man page and tweaks jmc@


# 1.94 20-Sep-2010 deraadt

DVACT_ACTIVATE and DVACT_DEACTIVATE do nothing here; remove
the false comments.


# 1.93 08-Sep-2010 jsing

Introduce a disk_lookup() function which calls device_lookup(), before
verifying that the resulting device is present on the disklist. This
avoids a race whereby the disk driver can be accessed as soon as the
softc has been allocated, but before the disk has completed
initialisation and has called disk_attach() (up until this point
dk_label is still a null pointer).

Cut cd(4), sd(4) and wd(4) across to disk_lookup(). All callers of
disk_attach() need to be tested and cut over in due course.

ok deraadt@ krw@


# 1.92 08-Sep-2010 jsing

Store a struct device pointer within struct disk and populate this when
disk_attach() is called by the device driver. We will be building on
this shortly.

ok deraadt@ krw@


# 1.91 01-Sep-2010 dlg

make struct bufq a member of the softc for devices that use it,
rather than it being a pointer to something that needs to be allocated
at attach. since all these devices need a bufq to operate, it makes
sense to have it allocated as part of the softc and get bufq_init
to just initialise all its fields. it also gets rid of the possibility
that you wont be able to allocate the bufq struct during attach,
which is something you dont want to happen.

secondly, it consistently implements a split between wrapper functions
and the per discipline implementation of the bufq handlers. it
consistently does the locking in the wrappers rather than doing
half in the wrappers and the other half in the implementations.

it also consistently handles the outstanding bufq bq pointer in the
wrappers.

this hides most of the implementation inside kern_bufq.c. the only
stuff left in buf.h is for the bits each implementation needs to
put inside struct buf.

tested by thib@ krw@ and me
ok thib@ matthew@
no objection from krw@


# 1.90 31-Aug-2010 deraadt

sort DVACT_ actions in order of use


# 1.89 28-Aug-2010 matthew

Garbage collect struct dkdriver.

ok miod@; "please go ahead" jsing@


Revision tags: OPENBSD_4_8_BASE
# 1.88 23-Jul-2010 jsg

Manuel Bouyer rescinded clauses 3 and 4 of his license text.


# 1.87 22-Jul-2010 kettenis

Give the drive a little bit more time to complete the STANDBY IMMED command.
The ATA spec seems to suggest it may take up to 30s. This seems a little bit
long. To quote Theo: "by that time some drives have probably parked their own
heads in boredom". But it shouldn't hurt unless your drive is seriously
broken. Makes some laptops suspend much better.

ok deraadt@


# 1.86 22-Jul-2010 mlarkin

First part of better pciide resume. Wake the channel by performing two
resets separated by a small delay (a delay intended to offend jsg who will
definitely want to fix it by checking for RDY instead)

Tested on a variety of amd and i386 hardware by myself and deraadt@

ok deraadt


# 1.85 28-Jun-2010 jsing

Move disk_attach() to the end of the attach functions, at which point the
disk is now ready to handle I/O.

ok krw@ dlg@


# 1.84 07-Jun-2010 jsg

partial backout of 1.81 as invoking standby in shutdown
breaks at least socppc and armish


# 1.83 26-May-2010 thib

Reintroduce bufqs. A few changes since it was backed out after some good
comments from dlg@.

No need for a separate bufq.h, keep all of in buf.h; As requested by kittens
and deraadt.

Only sd(4) and wd(4) for now. The rest of the drivers will be converted soon,
also other goodies like heuristics for sd(4) for selecting the bufq type and
the death of disksort() are forthcoming.

Tested on: i386, amd64, sparc64, macppc, loongson and alpha by myself and
phessler.

OK art@, beck@, kettenis@, oga@


# 1.82 24-May-2010 kettenis

Flush cache before suspend.

ok krw@, marco@


# 1.81 23-May-2010 kettenis

Place drive in standby mode before shutdown. Avoids the loud click heard on
many laptops when powering them down.

ok jsg@, oga@, krw@


# 1.80 18-May-2010 dlg

dont let sys/ioctl.h imply that you get the ioctls in dkio.h. this
gets rid of #include <sys/dkio.h> in sys/ioctl.h and adds #include
<sys/dkio.h> to the places that actually want and use the disk
ioctls.

this became an issue when krw@'s X build failed when he was testing
a change to dkio.h.
tested by krw@
help from and ok miod@


# 1.79 23-Apr-2010 jsing

Recycle unused disklabel fields in order to create a disklabel unique
identifier, allowing the disk to be identified without relying on the
device name.

ok deraadt@ krw@ beck@ marco@ todd@


# 1.78 11-Apr-2010 kettenis

Send "STANDBY IMMEDIATE" command to ATA disks upon suspend. For this to work,
pciide(4) needs to formward DVACT_SUSPEND and DVACT_RESUME events to its
children, so do that.

Gets rid of the nasty "click" sound from the disk on many laptops.

ok marco@, jsg@


Revision tags: OPENBSD_4_7_BASE
# 1.77 13-Oct-2009 pirofti

Get rid of devact enum, substitute it with an int and coresponding defines.

This is needed for the addition of further suspend/resume actions.

Okay deraadt@, marco@.


# 1.76 13-Aug-2009 deraadt

Replace the error strings that were being passed around with much simpler
errnos. Note that the error strings are being ignored, since we long ago
decided to not spam the console, and there is no other nice way to use the
errors (without changing the ioctls to pass it back)
The errno is now useful, since we can pass b_error from failing IO up, and
the drive can decide how to use that
ok miod


Revision tags: OPENBSD_4_6_BASE
# 1.75 17-Jun-2009 thib

Revert bufq's. this is inline with the major midlayer reverts that
have been going on. this appears to bring us back to stable state.

lots of testing by oga and ariane and my self.


# 1.74 03-Jun-2009 thib

add a flexible buffer queue (bufq) api, based on the never used
one by tedu@. It doesn't do anything smart yet, it just uses
plain old disksort. we also keep the old method of queueing bufs
since some miods have crazy MD drivers that need some love.

ok beck@, art@
tested by many on many archs.


Revision tags: OPENBSD_4_5_BASE
# 1.73 08-Nov-2008 chl

remove dead stores and newly created unused variables.

Found by LLVM/Clang Static Analyzer.

ok krw@


# 1.72 10-Sep-2008 blambert

Convert timeout_add() calls using multiples of hz to timeout_add_sec()

Really just the low-hanging fruit of (hopefully) forthcoming timeout
conversions.

ok art@, krw@


Revision tags: OPENBSD_4_4_BASE
# 1.71 27-Jun-2008 ray

More removal of clauses 3 and 4 from NetBSD licenses.

OK deraadt@ and millert@


# 1.70 15-Jun-2008 krw

Don't bypass partition bounds check for RAW_PART. We now guarantee that
RAW_PART will always be 0 -> disksize, so the bounds check will always
pass for i/o's to valid addresses. Now the i/o will be properly truncated
if it goes past the end of the device. This prevents various adverse
impacts of issuing i/o's for data past the end of the device.

Repeatedly requested by todd@.

ok weingart@ deraadt@


Revision tags: OPENBSD_4_3_BASE
# 1.69 07-Feb-2008 sthen

enable DMA for 1-sector devices (i.e. most flash devices).
from NetBSD

ok krw thib henning, tested by many


# 1.68 05-Dec-2007 jsg

Switch set features commands to use wdc_exec_command() to allow
them to be properly polled.

Fixes previously problematic machines of form and ckuethe.

ok marco@


# 1.67 26-Nov-2007 jsg

Enable read ahead and write cache enable if supported by
the disk rather than depending on the disk to have it on.

A bunch of people have suggested this should go in.


Revision tags: OPENBSD_4_2_BASE
# 1.66 20-Jun-2007 deraadt

b_cylinder does not need to be set on the callpath down into drivers.
cpu_disklabel can go away, since nothing anymore needs to use it; ok miod


# 1.65 18-Jun-2007 deraadt

avoid modification race in DIOCRLDINFO; ok krw miod


# 1.64 08-Jun-2007 deraadt

all drivers should spoof version 1 labels


# 1.63 07-Jun-2007 ray

strncpy -> strlcpy, plus a little spacing line up.

OK deraadt.


# 1.62 06-Jun-2007 deraadt

now that all partition size/offsets are potentially 64-bit, change the
type of all variables to daddr64_t. this includes the APIs for XXsize()
and XXdump(), all range checks inside bio drivers, internal variables
for disklabel handling, and even uvm's swap offsets. re-read numerous
times by otto, miod, krw, thib to look for errors


# 1.61 05-Jun-2007 deraadt

use six new macros to access & store the 48-bit disklabel fields related
to size. tested on almost all machines, double checked by miod and krw
next comes the type handling surrounding these values


# 1.60 01-Jun-2007 krw

Don't initialize d_partitions[RAW_DISK] just before calling
readdisklabel(), since all readdisklabel()'s do that already.

ok deraadt@


# 1.59 31-May-2007 krw

Kill old, no longer necessary kludge to try fooling readdisklabel()
into using DOS geometry by calling it twice. And don't ignore the
drive state if readdisklabel() returns a non-NULL value.

ok weingart@ grange@


# 1.58 28-Apr-2007 krw

Fix comments above DISKMINOR uses. DISKMINOR provides minor not major
number (wd.c), and the uses are to find the minimum minor to be
detached.


# 1.57 27-Apr-2007 krw

Nuke WDUNIT/WDPART/etc. defines and just use standard DISKUNIT/DISKPART/etc.
ones. No change to wd.o.

ok deraadt@ marco@


# 1.56 26-Apr-2007 krw

Eliminate CPU_BIOS from userland and wd(4) by always using the BIOS
geometry in the disklabel when there is a BIOS geometry to provide.
This removes the option to set a disklabel to 'BIOS' geometry via the
'g b' command in the editor.

Makes reported geometry more consistant and moves MD code to MD land
where it should be.

Doc help from jmc@, Feedback from millert@, marco@, weingart@,
kettenis@.

ok deraadt@


# 1.55 27-Mar-2007 deraadt

OK, I understand why jsg wanted the disk_unbusy() in wdretry(), and he
was right. There is another path that ends up avoiding the disk_unbusy
call I inserted.... tested by robert too


# 1.54 25-Mar-2007 deraadt

wdc retries (including after suspends) caused a disk to remain busy when
it should not; ok dlg
problem spotted using zaurus soft-drive-led diff which is pending
i think i convinced jsg now that this fix is better than putting it
in wdrestart


Revision tags: OPENBSD_4_1_BASE
# 1.53 15-Feb-2007 krw

Don't print the error strings returned by readdisklabel(). If you
need the debug info uncomment the printf's you need. Crude but
effective way to suppress 'no disklabel' errors that pop up at the
most innconvenient times to frighten users. More elegant method,
DPRINTF-like constructs or something, later.

"Yay!" marco@ ok deraadt@


# 1.52 06-Feb-2007 krw

De-register. No object code change.

From Bret Lambert.


# 1.51 17-Oct-2006 krw

For non-CPU_BIOS architectures calculate the number of cylinders on
a disk rather than accepting the cylinder count provided by the disk
or controller. This cylinder count will be '16383' for any disk >8.4G
according to the ATA spec. CPU_BIOS on i386/amd64 has magic to deal
with this, but other archs do not need to be restricted by the needs
of PC BIOS.

Fixes the default MBR OpenBSD partition size and disklabel on non-CPU_BIOS
archtitectures.

No change to behaviour on i386/amd64 machines.

Noted by Stuart Henderson on his Thecus.

Testing by various, including 'old i386 machines' nick@

ok tom@ pedro@ weingart@ deraadt@


# 1.50 04-Oct-2006 krw

Zap bad144 bad sector info in disklabels. Sparsely used if at all for
a few obsolete and easily replaced disk models. And didn't work anyway
according to Bob, who has tried.

ok beck@


Revision tags: OPENBSD_4_0_BASE
# 1.49 21-Aug-2006 krw

Delete #ifdef/#ifndef __OpenBSD__ chunks, leaving just the code we
use. Easier to read and eliminates a potentially confusing unused call
to wdgetdisklabel(). No change to object files.

ok brad@ dlg@ grange@ deraadt@ marco@


# 1.48 12-Aug-2006 krw

Setting d_secsize to DEV_BSIZE (or 1 << DEV_BSHIFT, or 512) and then
setting RAW_PART's p_size to d_secperunit * (d_secsize / DEV_BSIZE) is
a waste of a few ops. And p_size should be in sectors anyway.

Just set RAW_PART's p_size to d_secperunit to make usage consistant
across the tree.

Should be a no-op.


# 1.47 05-Mar-2006 krw

Remove more NRND NetBSDisms.

ok marco@


Revision tags: OPENBSD_3_9_BASE
# 1.46 21-Jan-2006 miod

Invoke disk_detach() and related cleanup work in detach(), rather than
zeroref() - just to be on the safe side, should we mess up our ref count.


# 1.45 09-Nov-2005 uwe

Don't fall back to fake geometry if ata_get_params() fails.
"looks sane" grange@; tested by niall@ krw@ jmc@ jolan@


Revision tags: OPENBSD_3_8_BASE
# 1.44 01-Sep-2005 uwe

On zaurus, don't fall back to fake geometry if ata_get_params() fails,
which can happen if the integrated microdrive's DRQ is not clear. This
fixes the drive geometry in the default disklabel.
Help from weingart@ and grange@; "mention `grange and i will fix this
better after 3.8 or we will be flailed'" deraadt@


# 1.43 27-Aug-2005 jsg

Use the ATA security FREEZE LOCK command on disks before
they are presented to the rest of the system to stop
a password being set that could deny access to the disk.
ok grange@ deraadt@


# 1.42 15-May-2005 grange

Use FLUSHCACHE EXT instead of FLUSHCACHE for LBA48 operations.
First noticed by christos@netbsd (iirc) a long time ago.

Tested by canacar@ and krw@.


Revision tags: OPENBSD_3_6_BASE OPENBSD_3_7_BASE
# 1.41 06-Jul-2004 aaron

Replace magic number with a more descriptive #define. This magic number was
particularly vile because it looks like a typo (i.e., looks like one "f" is
missing), but it's really not. I happened to notice this myself, but the fix
is inspired by NetBSD in the interest of keeping the code similar.

deraadt@ ok


Revision tags: OPENBSD_3_5_BASE SMP_SYNC_A SMP_SYNC_B
# 1.40 03-Mar-2004 tedu

remove bufq support until i figure out just what's going wrong. ok deraadt@


# 1.39 15-Feb-2004 tedu

new arg to disk_unbusy, to record separate read/write statistics.
looked at by various, testing henning@ mcbride@ dan weeks
mostly from netbsd via Pedro Martelletto <pbastos@rdc.puc-rio.br>


# 1.38 02-Feb-2004 tedu

as grange pointed out, BUFQ_ALLOC might fail


# 1.37 21-Jan-2004 tedu

dynamic bufq support. basically a nullop. ok grange@


# 1.36 16-Jan-2004 grange

Do not print CHS info for LBA drives, it's useless for drives with
capacity > 8GB. Total number of cylinders is enough.

ok deraadt@


# 1.35 15-Jan-2004 grange

ansi and some spaces; no f/c


# 1.34 21-Oct-2003 jmc

typos from Tom Cosgrove;


# 1.33 28-Sep-2003 grange

kill trailing spaces, no functional changes


Revision tags: OPENBSD_3_4_BASE
# 1.32 20-Jul-2003 grange

Use LBA48 only when accessing blocks after 137Gb limit or using sectors
count more than 255 to reduce registers writting overhead. Gives me
about 5% perfomance increase in a dumb synthetic test.

Some input and ok from costa@ a long time ago.


# 1.31 25-Jun-2003 tedu

implement new means of manipulating buf queues, bufq.
accessed with BUFQ macros, bufq structs support extensible, potentially
changable algorithms and queue formats. the current default scheme
should support nice priority based queuing, but is missing some vfs_bio.c
support.
only on wd.c for now, other drivers are easy converts.
as a side bonus, this makes the driver code look cleaner.
idea for the name comes from netbsd, but this scheme is incompatible.
thanks to various people for testing.
ok grange@


# 1.30 20-May-2003 tedu

change spaces to tabs so i can mail grange diffs :)
ok grange@


Revision tags: UBC_SYNC_A
# 1.29 09-Apr-2003 ho

sprintf->snprintf. miod@, grange@ ok.


# 1.28 06-Apr-2003 krw

Some easy strcpy elimination.

a) strcpy -> strncpy for copies into disklabel fields d_packname and
d_typename.

b) '16' -> 'sizeof d_packname' or 'sizeof d_typename' in existing
strncpy's.

ok millert@


Revision tags: OPENBSD_3_3_BASE
# 1.27 21-Feb-2003 grange

Fix Manuel Bouyer's copyright, from NetBSD
ok deraadt@


# 1.26 19-Dec-2002 grange

Protect WDCDEBUG_PRINT macro with do {} while (0)
ok costa@


Revision tags: OPENBSD_3_2_BASE UBC_SYNC_B
# 1.25 24-May-2002 art

protect biodone with splbio.


# 1.24 03-May-2002 gluk

LBA48 support.
Adopted from NetBSD by Alexander Yurchenko <grange@openbsd.ru>.

costa@ ok.


Revision tags: OPENBSD_3_1_BASE
# 1.23 14-Mar-2002 millert

Final __P removal plus some cosmetic fixups


# 1.22 14-Mar-2002 millert

First round of __P removal in sys


# 1.21 07-Jan-2002 mickey

pass the proc* down the ioctl chain to controller; mjacob@ csapuntz@ ok


Revision tags: UBC_BASE
# 1.20 06-Nov-2001 miod

branches: 1.20.2;
Replace inclusion of <vm/foo.h> with the correct <uvm/bar.h> when necessary.
(Look ma, I might have broken the tree)


Revision tags: OPENBSD_3_0_BASE
# 1.19 06-Aug-2001 miod

Remove legacy __BDEVSW_DUMP_OLD_TYPE code.


# 1.18 31-Jul-2001 csapuntz

Go down only one Ultra DMA mode at a time. Thanks to Matt Evans for
suggesting this fix.

Update atapiscsi Ultra DMA downgrade logic.


# 1.17 12-Jul-2001 csapuntz

extra wddone caused biodone panics occasionally


# 1.16 30-Apr-2001 csapuntz

Move ioctl stuff to low-level driver


Revision tags: OPENBSD_2_9_BASE
# 1.15 02-Apr-2001 csapuntz

ATA_POLL should have been AT_POLL

Flushcache on shutdown used to pass both AT_POLL and AT_WAIT. The two flags
do not make sense together.

Should fix panic when shutting down from ddb


# 1.14 25-Jan-2001 todd

spelling


Revision tags: OPENBSD_2_8_BASE
# 1.13 30-Jun-2000 art

Convert to new timeout API.


Revision tags: OPENBSD_2_7_BASE
# 1.12 10-Apr-2000 csapuntz

Added support for wd detach (merge from NetBSD). Support for
SCSI/ATAPI detach is not here yet.

Minor cleanup of wdc. Downgrade to UDMA mode 1 before going further.
Want to stay in UDMA modes because they're more error-resilient due to
a CRC.

Got rid of some of the ridiculous amount of softc sharing going on.
Hopefully, this will make the life of whoever goes in and fixes the
ref-counting to be correct easier.


# 1.11 07-Apr-2000 niklas

remove statics


Revision tags: SMP_BASE kame_19991208
# 1.10 17-Nov-1999 csapuntz

branches: 1.10.2;


New interface to ATA registers that goes through the lower-level device
driver (pciide, wdc, etc.)

Remove #define WDCDEBUG from top of files

More fixes to ATAPISCSI logic:

Not all devices transition correctly between phases. Devices are supposed
to keep BSY high until they've set the registers to the next sensible
state. Some devices drop BSY and leave the registers in an old or
nonsense state. Our polling code is extremely sensitive to this
(though an early itnerrupt could also observe this). So, if the device is
in an unexpected state, the new polling code waits for a while in the hope
that it enters a better state.

This seems to fix many of the problems reported.

Also, there was a horrible bug which would cause sense to fail on a ATAPI
command that sent data to the drive


Revision tags: OPENBSD_2_6_BASE
# 1.9 09-Oct-1999 csapuntz

Fix printout at attach time. Print PIO/DMA mode information after having
printed line with the name of hard disk.


# 1.8 09-Oct-1999 csapuntz

wdc layer work:
Separate wdc_probe_caps into wdc_probe_caps and wdc_print_caps for more
flexibility in printing capability information.

Get rid of wdc_final_attach.

Include name of device (e.g. cd0), if possible, on errors.

atapiscsi layer work:
Put a pointer to the SCSI device into ata_drive_datas' drv_softc field

Simplify, simplify, simplify. Got rid of a bunch of fields in atapiscsi_softc

Delay printing capabilities until we know the real device name (e.g. st0)


# 1.7 15-Aug-1999 millert

o Correctly set type and typename in driver-generated label
Should really use viscopy() when copying atap_model -> d_packname.
When wd's become sd's this will be trivial (viscopy() lives in sd.c)


# 1.6 10-Aug-1999 deraadt

support DIOCRLDINFO


# 1.5 05-Aug-1999 niklas

Make IDE compile on alpha again:
use b* instead of mem* functions, adhere to NISADMA for DMA support.


# 1.4 22-Jul-1999 deraadt

shorten dmesg output


# 1.3 22-Jul-1999 csapuntz

Fix bad144intern.


# 1.2 20-Jul-1999 csapuntz

126 -> NBT_BAD

Fix disklabel/bad sectors stuff in wd


# 1.1 18-Jul-1999 csapuntz

Import of NetBSD ATA/IDE stuff.

Introduction of home-grown

To enable this stuff in your configuration, look at the NEWATA conf file
and go through

dev/isa/files.isa
dev/pci/files.pci
conf/files
arch/i386/conf/files.i386

and follow the instructions on commenting/uncommenting stuff


# 1.125 30-Dec-2017 guenther

Don't pull in <sys/file.h> just to get fcntl.h

ok deraadt@ krw@


# 1.124 14-Dec-2017 deraadt

Repair flushcache/standby operations to not convert AT_WAIT operations
to AT_POLL, which some chip driver underneath cannot handle.
tested by bluhm also


# 1.123 24-Oct-2017 jsg

remove defines for ioctls the kernel doesn't recognise
ok mpi@


Revision tags: OPENBSD_6_2_BASE
# 1.122 29-May-2017 sf

Add an ioctl to tell storage devices to flush their internal caches

Currently implemented for wd and sd.
Initially ported from netbsd by pedro@

ok deraadt@


# 1.121 04-May-2017 deraadt

Also pass the blk offset to disk_unbusy(), so that it can pass it to
the random subsystem as entropy. This value is pretty much unknown,
and anyways our entropy input ring does not saturate from knowns.
ok mikeb djm


Revision tags: OPENBSD_5_9_BASE OPENBSD_6_0_BASE OPENBSD_6_1_BASE
# 1.120 20-Jan-2016 stefan

Set segflg member of uio struct with symbolic enum value
instead of using magic numbers. No binary change.

Diff from Martin Natano.

ok deraadt@


# 1.119 26-Aug-2015 deraadt

trivial size to free()


# 1.118 17-Aug-2015 krw

The 80's called and want their interface back. Remove last fragments of
ST-506 support. Remove redundant 'precomp' variables/fields and rename
remaining few to 'features' to be consistant with the rest of the code.

Consensus that no ST-506 disks (requiring ISA cards, <120MB, usually
rife with bad sectors that can't be worked around) are still running
OpenBSD.


Revision tags: OPENBSD_5_7_BASE OPENBSD_5_8_BASE
# 1.117 14-Sep-2014 jsg

remove uneeded proc.h includes
ok mpi@ kspillner@


Revision tags: OPENBSD_5_6_BASE
# 1.116 12-Jul-2014 tedu

add a size argument to free. will be used soon, but for now default to 0.
after discussions with beck deraadt kettenis.


# 1.115 10-Jul-2014 deraadt

remove most of the boolean_t infection outside uvm/ddb/pmap; ok jsg


Revision tags: OPENBSD_5_5_BASE
# 1.114 14-Oct-2013 krw

Use DL_SECTOBLK() instead of handrolling same calculation to get
wdsize() return value.


# 1.113 14-Oct-2013 krw

XXsize() returns daddr_t, so calculate the return value in a daddr_t
variable and not (usually) an int.

ok miod@


# 1.112 27-Sep-2013 krw

Try harder to ensure wddump() doesn't decrement the count
of sectors to write by one value and increment the address
the next i/o will start at by a different amount.


# 1.111 15-Sep-2013 krw

Make it clearer that sc_wdc_bio.blkno is actuall a disk hardware
address by using the DL_BLKTOSEC() and DL_SECTOBLK() defines rather
than handrolling the equivalent. Fixes current addition of a 512-byte
block value (b_blkno) to a disk hardware address (DL_GETPOFFSET()).

Tested on real wd(4) hardware by guenther@.


Revision tags: OPENBSD_5_4_BASE
# 1.110 11-Jun-2013 deraadt

final removal of daddr64_t. daddr_t has been 64 bit for a long enough
test period; i think 3 years ago the last bugs fell out.
ok otto beck others


Revision tags: OPENBSD_5_3_BASE
# 1.109 08-Oct-2012 deraadt

Revamp the sequences for suspend/hibernate -> resume so that the code
paths are reflexive. It is now possible to fail part-way through a
suspend sequence, and recover along the resume code path.
Split DVACT_SUSPEND by adding a new DVACT_POWERDOWN method is used
after hibernate (and suspend too) to finish the job. Some drivers
must be converted at the same time to use this instead of shutdown hooks
(the others will follow at a later time)
ok kettenis mlarkin


Revision tags: OPENBSD_5_0_BASE OPENBSD_5_1_BASE OPENBSD_5_2_BASE
# 1.108 06-Jul-2011 matthew

Eliminate redundant buf validation checks in xxstrategy() methods now
that they're implemented consistently in bounds_check_with_label().

Also, per krw's request, change bounds_check_with_label() to return 0
if the checks succeed, and change the drivers to test == -1 instead of
<= 0. (Man page update to follow; intentionally omitting
arch/vax/mba/hp.c from this commit because it doesn't even build
currently and miod@ promises to kill it soon.)

ok krw@


# 1.107 30-Jun-2011 matthew

Refactor some common open/close/detach disk driver code into
subr_disk.c. For now just the MI disk drivers.

ok deraadt@, krw@; jsing@ liked the approach too


# 1.106 20-Jun-2011 matthew

Call bufq_drain() in wddetach() instead of reimplementing the same
logic.


# 1.105 19-Jun-2011 deraadt

Use disk_lock_nointr() in the xxclose() routines so that they cannot
be interrupted.
ok matthew


# 1.104 19-Jun-2011 deraadt

Use disk_lock/disk_unlock directly and in the same way in these drivers,
rather than using various wrappings. Convert vnd to using the sc_dk
rwlock instead of using one of its own.
ok matthew


# 1.103 05-Jun-2011 matthew

Drop kernel support for the useless DIOCWLABEL ioctl and prune a lot
of silly flag twiddling code in various disk drivers.

ok deraadt@, miod@

N.B., users will need a -current disklabel(8) to be able to write new
disklabels to disk now.


# 1.102 03-Jun-2011 matthew

Get rid of the wlabel argument to bounds_check_with_label(). It's
never done anything in OpenBSD and just clutters disk drivers with
silly flag handling.

More cleanup to follow.

ok deraadt@, millert@; no objections krw@


# 1.101 31-May-2011 matthew

Change a few of the more common disk drivers (sd, cd, wd, rd, and vnd)
to return EBUSY if the user tries to modify an open partition's offset
or size. Only sadness can result if a user tries this, and rejecting
it prevents a race between sdstart() and sdstrategy().

Curiously, there was already code in the kernel and in disklabel(8) to
detect/handle this, but it was effectively disabled because the disk
drivers always used something like "/* sc->sc_dk.dk_openmask */ 0",
and this commented out code has existed since even r1.1 in NetBSD.

I had no problems building a release and messing around with
disklabel(8) for a bit with this diff. Canarying the more common MI
disk drivers until we gain confidence that there aren't any
regressions, then we can switch the remaining drivers.

"I am surprised you got me convinced that this stuff is safe" deraadt@
ok krw@


# 1.100 18-Apr-2011 deraadt

keep an eye out for wdc registers returning 0xff. if they return 0xff,
this probably means the controller is dead -- as in, a hot plug eject
event. in that case, mark the driver dead and try harder to return -1
in the interrupt handler.
tested by many
ok dlg, miod did not object


# 1.99 15-Apr-2011 miod

Change wdc_reset_channel() to take a `no wait' argument. Pass in turn this
argument to wdcreset(), to have it skip waiting until active channels see
their BUSY bit clear in the status register.

Use this feature in the resume path, during the first reset operation. The
first reset is supposed to only wake up the controller, and the disks don't
come back until the second reset is issued, therefore waiting for them to
report themselves as ready after the first reset, but before the second, is
moot - and as a matter of fact some controllers, such as the AMD 754 and
clones/offspring (e.g. Geode) keep the BUSY bit asserted after the first
reset.

Last, but not least, make sure wd@ata invokes wd_get_params() again before
returning from the resume code, as we will still be using polled transfers
for a short while.

This causes the Lemote Yeelong to resume within less than one second, instead
of the lousy 30 seconds wait between the two resets; and the wd_get_params()
voodoo prevents it from getting spurious ide interrupts afterwards.

wd_get_params() magic from dlg; rest of the work by yours truly after enough
prodding by dlg@ and pirofti@, among others. ok deraadt@ dlg@


# 1.98 05-Apr-2011 deraadt

wd_hibernate_io() is a standalone disk io writer which tries to not
damage kernel memory very much while doing the job. It is not very
pretty yet, but improving it will need some MI work.
ok mlarkin jsg dlg


Revision tags: OPENBSD_4_9_BASE
# 1.97 31-Dec-2010 kettenis

Reintroduce the code that puts the drive into standby in shutdown, but
only do this if we're going to power down the machine.

"My god, it is a 3 line diff. Commit it before it gets bigger!" deraadt@


# 1.96 30-Dec-2010 kettenis

The NVIDIA MCP89 SATA controller found on the 2010 Apple 13" MacBook Pro
can't address block 0xffffffff in LBA mode, at least in combination with
the Hitachi HTS545032B9SA02 and HTS545050B9SA02 drives that Apple ships.
So use LBA48 mode from that block onwards.

ok marco@, deraadt@, miod@


# 1.95 22-Sep-2010 matthew

All users of physio(9) now pass NULL as the buf pointer argument, so
no point in keeping it around.

"i like this" thib@ (a while back); ok krw@ and oga@; reminder to
update the man page and tweaks jmc@


# 1.94 20-Sep-2010 deraadt

DVACT_ACTIVATE and DVACT_DEACTIVATE do nothing here; remove
the false comments.


# 1.93 08-Sep-2010 jsing

Introduce a disk_lookup() function which calls device_lookup(), before
verifying that the resulting device is present on the disklist. This
avoids a race whereby the disk driver can be accessed as soon as the
softc has been allocated, but before the disk has completed
initialisation and has called disk_attach() (up until this point
dk_label is still a null pointer).

Cut cd(4), sd(4) and wd(4) across to disk_lookup(). All callers of
disk_attach() need to be tested and cut over in due course.

ok deraadt@ krw@


# 1.92 08-Sep-2010 jsing

Store a struct device pointer within struct disk and populate this when
disk_attach() is called by the device driver. We will be building on
this shortly.

ok deraadt@ krw@


# 1.91 01-Sep-2010 dlg

make struct bufq a member of the softc for devices that use it,
rather than it being a pointer to something that needs to be allocated
at attach. since all these devices need a bufq to operate, it makes
sense to have it allocated as part of the softc and get bufq_init
to just initialise all its fields. it also gets rid of the possibility
that you wont be able to allocate the bufq struct during attach,
which is something you dont want to happen.

secondly, it consistently implements a split between wrapper functions
and the per discipline implementation of the bufq handlers. it
consistently does the locking in the wrappers rather than doing
half in the wrappers and the other half in the implementations.

it also consistently handles the outstanding bufq bq pointer in the
wrappers.

this hides most of the implementation inside kern_bufq.c. the only
stuff left in buf.h is for the bits each implementation needs to
put inside struct buf.

tested by thib@ krw@ and me
ok thib@ matthew@
no objection from krw@


# 1.90 31-Aug-2010 deraadt

sort DVACT_ actions in order of use


# 1.89 28-Aug-2010 matthew

Garbage collect struct dkdriver.

ok miod@; "please go ahead" jsing@


Revision tags: OPENBSD_4_8_BASE
# 1.88 23-Jul-2010 jsg

Manuel Bouyer rescinded clauses 3 and 4 of his license text.


# 1.87 22-Jul-2010 kettenis

Give the drive a little bit more time to complete the STANDBY IMMED command.
The ATA spec seems to suggest it may take up to 30s. This seems a little bit
long. To quote Theo: "by that time some drives have probably parked their own
heads in boredom". But it shouldn't hurt unless your drive is seriously
broken. Makes some laptops suspend much better.

ok deraadt@


# 1.86 22-Jul-2010 mlarkin

First part of better pciide resume. Wake the channel by performing two
resets separated by a small delay (a delay intended to offend jsg who will
definitely want to fix it by checking for RDY instead)

Tested on a variety of amd and i386 hardware by myself and deraadt@

ok deraadt


# 1.85 28-Jun-2010 jsing

Move disk_attach() to the end of the attach functions, at which point the
disk is now ready to handle I/O.

ok krw@ dlg@


# 1.84 07-Jun-2010 jsg

partial backout of 1.81 as invoking standby in shutdown
breaks at least socppc and armish


# 1.83 26-May-2010 thib

Reintroduce bufqs. A few changes since it was backed out after some good
comments from dlg@.

No need for a separate bufq.h, keep all of in buf.h; As requested by kittens
and deraadt.

Only sd(4) and wd(4) for now. The rest of the drivers will be converted soon,
also other goodies like heuristics for sd(4) for selecting the bufq type and
the death of disksort() are forthcoming.

Tested on: i386, amd64, sparc64, macppc, loongson and alpha by myself and
phessler.

OK art@, beck@, kettenis@, oga@


# 1.82 24-May-2010 kettenis

Flush cache before suspend.

ok krw@, marco@


# 1.81 23-May-2010 kettenis

Place drive in standby mode before shutdown. Avoids the loud click heard on
many laptops when powering them down.

ok jsg@, oga@, krw@


# 1.80 18-May-2010 dlg

dont let sys/ioctl.h imply that you get the ioctls in dkio.h. this
gets rid of #include <sys/dkio.h> in sys/ioctl.h and adds #include
<sys/dkio.h> to the places that actually want and use the disk
ioctls.

this became an issue when krw@'s X build failed when he was testing
a change to dkio.h.
tested by krw@
help from and ok miod@


# 1.79 23-Apr-2010 jsing

Recycle unused disklabel fields in order to create a disklabel unique
identifier, allowing the disk to be identified without relying on the
device name.

ok deraadt@ krw@ beck@ marco@ todd@


# 1.78 11-Apr-2010 kettenis

Send "STANDBY IMMEDIATE" command to ATA disks upon suspend. For this to work,
pciide(4) needs to formward DVACT_SUSPEND and DVACT_RESUME events to its
children, so do that.

Gets rid of the nasty "click" sound from the disk on many laptops.

ok marco@, jsg@


Revision tags: OPENBSD_4_7_BASE
# 1.77 13-Oct-2009 pirofti

Get rid of devact enum, substitute it with an int and coresponding defines.

This is needed for the addition of further suspend/resume actions.

Okay deraadt@, marco@.


# 1.76 13-Aug-2009 deraadt

Replace the error strings that were being passed around with much simpler
errnos. Note that the error strings are being ignored, since we long ago
decided to not spam the console, and there is no other nice way to use the
errors (without changing the ioctls to pass it back)
The errno is now useful, since we can pass b_error from failing IO up, and
the drive can decide how to use that
ok miod


Revision tags: OPENBSD_4_6_BASE
# 1.75 17-Jun-2009 thib

Revert bufq's. this is inline with the major midlayer reverts that
have been going on. this appears to bring us back to stable state.

lots of testing by oga and ariane and my self.


# 1.74 03-Jun-2009 thib

add a flexible buffer queue (bufq) api, based on the never used
one by tedu@. It doesn't do anything smart yet, it just uses
plain old disksort. we also keep the old method of queueing bufs
since some miods have crazy MD drivers that need some love.

ok beck@, art@
tested by many on many archs.


Revision tags: OPENBSD_4_5_BASE
# 1.73 08-Nov-2008 chl

remove dead stores and newly created unused variables.

Found by LLVM/Clang Static Analyzer.

ok krw@


# 1.72 10-Sep-2008 blambert

Convert timeout_add() calls using multiples of hz to timeout_add_sec()

Really just the low-hanging fruit of (hopefully) forthcoming timeout
conversions.

ok art@, krw@


Revision tags: OPENBSD_4_4_BASE
# 1.71 27-Jun-2008 ray

More removal of clauses 3 and 4 from NetBSD licenses.

OK deraadt@ and millert@


# 1.70 15-Jun-2008 krw

Don't bypass partition bounds check for RAW_PART. We now guarantee that
RAW_PART will always be 0 -> disksize, so the bounds check will always
pass for i/o's to valid addresses. Now the i/o will be properly truncated
if it goes past the end of the device. This prevents various adverse
impacts of issuing i/o's for data past the end of the device.

Repeatedly requested by todd@.

ok weingart@ deraadt@


Revision tags: OPENBSD_4_3_BASE
# 1.69 07-Feb-2008 sthen

enable DMA for 1-sector devices (i.e. most flash devices).
from NetBSD

ok krw thib henning, tested by many


# 1.68 05-Dec-2007 jsg

Switch set features commands to use wdc_exec_command() to allow
them to be properly polled.

Fixes previously problematic machines of form and ckuethe.

ok marco@


# 1.67 26-Nov-2007 jsg

Enable read ahead and write cache enable if supported by
the disk rather than depending on the disk to have it on.

A bunch of people have suggested this should go in.


Revision tags: OPENBSD_4_2_BASE
# 1.66 20-Jun-2007 deraadt

b_cylinder does not need to be set on the callpath down into drivers.
cpu_disklabel can go away, since nothing anymore needs to use it; ok miod


# 1.65 18-Jun-2007 deraadt

avoid modification race in DIOCRLDINFO; ok krw miod


# 1.64 08-Jun-2007 deraadt

all drivers should spoof version 1 labels


# 1.63 07-Jun-2007 ray

strncpy -> strlcpy, plus a little spacing line up.

OK deraadt.


# 1.62 06-Jun-2007 deraadt

now that all partition size/offsets are potentially 64-bit, change the
type of all variables to daddr64_t. this includes the APIs for XXsize()
and XXdump(), all range checks inside bio drivers, internal variables
for disklabel handling, and even uvm's swap offsets. re-read numerous
times by otto, miod, krw, thib to look for errors


# 1.61 05-Jun-2007 deraadt

use six new macros to access & store the 48-bit disklabel fields related
to size. tested on almost all machines, double checked by miod and krw
next comes the type handling surrounding these values


# 1.60 01-Jun-2007 krw

Don't initialize d_partitions[RAW_DISK] just before calling
readdisklabel(), since all readdisklabel()'s do that already.

ok deraadt@


# 1.59 31-May-2007 krw

Kill old, no longer necessary kludge to try fooling readdisklabel()
into using DOS geometry by calling it twice. And don't ignore the
drive state if readdisklabel() returns a non-NULL value.

ok weingart@ grange@


# 1.58 28-Apr-2007 krw

Fix comments above DISKMINOR uses. DISKMINOR provides minor not major
number (wd.c), and the uses are to find the minimum minor to be
detached.


# 1.57 27-Apr-2007 krw

Nuke WDUNIT/WDPART/etc. defines and just use standard DISKUNIT/DISKPART/etc.
ones. No change to wd.o.

ok deraadt@ marco@


# 1.56 26-Apr-2007 krw

Eliminate CPU_BIOS from userland and wd(4) by always using the BIOS
geometry in the disklabel when there is a BIOS geometry to provide.
This removes the option to set a disklabel to 'BIOS' geometry via the
'g b' command in the editor.

Makes reported geometry more consistant and moves MD code to MD land
where it should be.

Doc help from jmc@, Feedback from millert@, marco@, weingart@,
kettenis@.

ok deraadt@


# 1.55 27-Mar-2007 deraadt

OK, I understand why jsg wanted the disk_unbusy() in wdretry(), and he
was right. There is another path that ends up avoiding the disk_unbusy
call I inserted.... tested by robert too


# 1.54 25-Mar-2007 deraadt

wdc retries (including after suspends) caused a disk to remain busy when
it should not; ok dlg
problem spotted using zaurus soft-drive-led diff which is pending
i think i convinced jsg now that this fix is better than putting it
in wdrestart


Revision tags: OPENBSD_4_1_BASE
# 1.53 15-Feb-2007 krw

Don't print the error strings returned by readdisklabel(). If you
need the debug info uncomment the printf's you need. Crude but
effective way to suppress 'no disklabel' errors that pop up at the
most innconvenient times to frighten users. More elegant method,
DPRINTF-like constructs or something, later.

"Yay!" marco@ ok deraadt@


# 1.52 06-Feb-2007 krw

De-register. No object code change.

From Bret Lambert.


# 1.51 17-Oct-2006 krw

For non-CPU_BIOS architectures calculate the number of cylinders on
a disk rather than accepting the cylinder count provided by the disk
or controller. This cylinder count will be '16383' for any disk >8.4G
according to the ATA spec. CPU_BIOS on i386/amd64 has magic to deal
with this, but other archs do not need to be restricted by the needs
of PC BIOS.

Fixes the default MBR OpenBSD partition size and disklabel on non-CPU_BIOS
archtitectures.

No change to behaviour on i386/amd64 machines.

Noted by Stuart Henderson on his Thecus.

Testing by various, including 'old i386 machines' nick@

ok tom@ pedro@ weingart@ deraadt@


# 1.50 04-Oct-2006 krw

Zap bad144 bad sector info in disklabels. Sparsely used if at all for
a few obsolete and easily replaced disk models. And didn't work anyway
according to Bob, who has tried.

ok beck@


Revision tags: OPENBSD_4_0_BASE
# 1.49 21-Aug-2006 krw

Delete #ifdef/#ifndef __OpenBSD__ chunks, leaving just the code we
use. Easier to read and eliminates a potentially confusing unused call
to wdgetdisklabel(). No change to object files.

ok brad@ dlg@ grange@ deraadt@ marco@


# 1.48 12-Aug-2006 krw

Setting d_secsize to DEV_BSIZE (or 1 << DEV_BSHIFT, or 512) and then
setting RAW_PART's p_size to d_secperunit * (d_secsize / DEV_BSIZE) is
a waste of a few ops. And p_size should be in sectors anyway.

Just set RAW_PART's p_size to d_secperunit to make usage consistant
across the tree.

Should be a no-op.


# 1.47 05-Mar-2006 krw

Remove more NRND NetBSDisms.

ok marco@


Revision tags: OPENBSD_3_9_BASE
# 1.46 21-Jan-2006 miod

Invoke disk_detach() and related cleanup work in detach(), rather than
zeroref() - just to be on the safe side, should we mess up our ref count.


# 1.45 09-Nov-2005 uwe

Don't fall back to fake geometry if ata_get_params() fails.
"looks sane" grange@; tested by niall@ krw@ jmc@ jolan@


Revision tags: OPENBSD_3_8_BASE
# 1.44 01-Sep-2005 uwe

On zaurus, don't fall back to fake geometry if ata_get_params() fails,
which can happen if the integrated microdrive's DRQ is not clear. This
fixes the drive geometry in the default disklabel.
Help from weingart@ and grange@; "mention `grange and i will fix this
better after 3.8 or we will be flailed'" deraadt@


# 1.43 27-Aug-2005 jsg

Use the ATA security FREEZE LOCK command on disks before
they are presented to the rest of the system to stop
a password being set that could deny access to the disk.
ok grange@ deraadt@


# 1.42 15-May-2005 grange

Use FLUSHCACHE EXT instead of FLUSHCACHE for LBA48 operations.
First noticed by christos@netbsd (iirc) a long time ago.

Tested by canacar@ and krw@.


Revision tags: OPENBSD_3_6_BASE OPENBSD_3_7_BASE
# 1.41 06-Jul-2004 aaron

Replace magic number with a more descriptive #define. This magic number was
particularly vile because it looks like a typo (i.e., looks like one "f" is
missing), but it's really not. I happened to notice this myself, but the fix
is inspired by NetBSD in the interest of keeping the code similar.

deraadt@ ok


Revision tags: OPENBSD_3_5_BASE SMP_SYNC_A SMP_SYNC_B
# 1.40 03-Mar-2004 tedu

remove bufq support until i figure out just what's going wrong. ok deraadt@


# 1.39 15-Feb-2004 tedu

new arg to disk_unbusy, to record separate read/write statistics.
looked at by various, testing henning@ mcbride@ dan weeks
mostly from netbsd via Pedro Martelletto <pbastos@rdc.puc-rio.br>


# 1.38 02-Feb-2004 tedu

as grange pointed out, BUFQ_ALLOC might fail


# 1.37 21-Jan-2004 tedu

dynamic bufq support. basically a nullop. ok grange@


# 1.36 16-Jan-2004 grange

Do not print CHS info for LBA drives, it's useless for drives with
capacity > 8GB. Total number of cylinders is enough.

ok deraadt@


# 1.35 15-Jan-2004 grange

ansi and some spaces; no f/c


# 1.34 21-Oct-2003 jmc

typos from Tom Cosgrove;


# 1.33 28-Sep-2003 grange

kill trailing spaces, no functional changes


Revision tags: OPENBSD_3_4_BASE
# 1.32 20-Jul-2003 grange

Use LBA48 only when accessing blocks after 137Gb limit or using sectors
count more than 255 to reduce registers writting overhead. Gives me
about 5% perfomance increase in a dumb synthetic test.

Some input and ok from costa@ a long time ago.


# 1.31 25-Jun-2003 tedu

implement new means of manipulating buf queues, bufq.
accessed with BUFQ macros, bufq structs support extensible, potentially
changable algorithms and queue formats. the current default scheme
should support nice priority based queuing, but is missing some vfs_bio.c
support.
only on wd.c for now, other drivers are easy converts.
as a side bonus, this makes the driver code look cleaner.
idea for the name comes from netbsd, but this scheme is incompatible.
thanks to various people for testing.
ok grange@


# 1.30 20-May-2003 tedu

change spaces to tabs so i can mail grange diffs :)
ok grange@


Revision tags: UBC_SYNC_A
# 1.29 09-Apr-2003 ho

sprintf->snprintf. miod@, grange@ ok.


# 1.28 06-Apr-2003 krw

Some easy strcpy elimination.

a) strcpy -> strncpy for copies into disklabel fields d_packname and
d_typename.

b) '16' -> 'sizeof d_packname' or 'sizeof d_typename' in existing
strncpy's.

ok millert@


Revision tags: OPENBSD_3_3_BASE
# 1.27 21-Feb-2003 grange

Fix Manuel Bouyer's copyright, from NetBSD
ok deraadt@


# 1.26 19-Dec-2002 grange

Protect WDCDEBUG_PRINT macro with do {} while (0)
ok costa@


Revision tags: OPENBSD_3_2_BASE UBC_SYNC_B
# 1.25 24-May-2002 art

protect biodone with splbio.


# 1.24 03-May-2002 gluk

LBA48 support.
Adopted from NetBSD by Alexander Yurchenko <grange@openbsd.ru>.

costa@ ok.


Revision tags: OPENBSD_3_1_BASE
# 1.23 14-Mar-2002 millert

Final __P removal plus some cosmetic fixups


# 1.22 14-Mar-2002 millert

First round of __P removal in sys


# 1.21 07-Jan-2002 mickey

pass the proc* down the ioctl chain to controller; mjacob@ csapuntz@ ok


Revision tags: UBC_BASE
# 1.20 06-Nov-2001 miod

branches: 1.20.2;
Replace inclusion of <vm/foo.h> with the correct <uvm/bar.h> when necessary.
(Look ma, I might have broken the tree)


Revision tags: OPENBSD_3_0_BASE
# 1.19 06-Aug-2001 miod

Remove legacy __BDEVSW_DUMP_OLD_TYPE code.


# 1.18 31-Jul-2001 csapuntz

Go down only one Ultra DMA mode at a time. Thanks to Matt Evans for
suggesting this fix.

Update atapiscsi Ultra DMA downgrade logic.


# 1.17 12-Jul-2001 csapuntz

extra wddone caused biodone panics occasionally


# 1.16 30-Apr-2001 csapuntz

Move ioctl stuff to low-level driver


Revision tags: OPENBSD_2_9_BASE
# 1.15 02-Apr-2001 csapuntz

ATA_POLL should have been AT_POLL

Flushcache on shutdown used to pass both AT_POLL and AT_WAIT. The two flags
do not make sense together.

Should fix panic when shutting down from ddb


# 1.14 25-Jan-2001 todd

spelling


Revision tags: OPENBSD_2_8_BASE
# 1.13 30-Jun-2000 art

Convert to new timeout API.


Revision tags: OPENBSD_2_7_BASE
# 1.12 10-Apr-2000 csapuntz

Added support for wd detach (merge from NetBSD). Support for
SCSI/ATAPI detach is not here yet.

Minor cleanup of wdc. Downgrade to UDMA mode 1 before going further.
Want to stay in UDMA modes because they're more error-resilient due to
a CRC.

Got rid of some of the ridiculous amount of softc sharing going on.
Hopefully, this will make the life of whoever goes in and fixes the
ref-counting to be correct easier.


# 1.11 07-Apr-2000 niklas

remove statics


Revision tags: SMP_BASE kame_19991208
# 1.10 17-Nov-1999 csapuntz

branches: 1.10.2;


New interface to ATA registers that goes through the lower-level device
driver (pciide, wdc, etc.)

Remove #define WDCDEBUG from top of files

More fixes to ATAPISCSI logic:

Not all devices transition correctly between phases. Devices are supposed
to keep BSY high until they've set the registers to the next sensible
state. Some devices drop BSY and leave the registers in an old or
nonsense state. Our polling code is extremely sensitive to this
(though an early itnerrupt could also observe this). So, if the device is
in an unexpected state, the new polling code waits for a while in the hope
that it enters a better state.

This seems to fix many of the problems reported.

Also, there was a horrible bug which would cause sense to fail on a ATAPI
command that sent data to the drive


Revision tags: OPENBSD_2_6_BASE
# 1.9 09-Oct-1999 csapuntz

Fix printout at attach time. Print PIO/DMA mode information after having
printed line with the name of hard disk.


# 1.8 09-Oct-1999 csapuntz

wdc layer work:
Separate wdc_probe_caps into wdc_probe_caps and wdc_print_caps for more
flexibility in printing capability information.

Get rid of wdc_final_attach.

Include name of device (e.g. cd0), if possible, on errors.

atapiscsi layer work:
Put a pointer to the SCSI device into ata_drive_datas' drv_softc field

Simplify, simplify, simplify. Got rid of a bunch of fields in atapiscsi_softc

Delay printing capabilities until we know the real device name (e.g. st0)


# 1.7 15-Aug-1999 millert

o Correctly set type and typename in driver-generated label
Should really use viscopy() when copying atap_model -> d_packname.
When wd's become sd's this will be trivial (viscopy() lives in sd.c)


# 1.6 10-Aug-1999 deraadt

support DIOCRLDINFO


# 1.5 05-Aug-1999 niklas

Make IDE compile on alpha again:
use b* instead of mem* functions, adhere to NISADMA for DMA support.


# 1.4 22-Jul-1999 deraadt

shorten dmesg output


# 1.3 22-Jul-1999 csapuntz

Fix bad144intern.


# 1.2 20-Jul-1999 csapuntz

126 -> NBT_BAD

Fix disklabel/bad sectors stuff in wd


# 1.1 18-Jul-1999 csapuntz

Import of NetBSD ATA/IDE stuff.

Introduction of home-grown

To enable this stuff in your configuration, look at the NEWATA conf file
and go through

dev/isa/files.isa
dev/pci/files.pci
conf/files
arch/i386/conf/files.i386

and follow the instructions on commenting/uncommenting stuff