libata: add ioctls to support SMART
PATCH 1/1 libata: add ioctls to support SMART
Support for HDIO_DRIVE_CMD and HDIO_DRIVE_TASK in libata. Useful for supporting SMART w/ unmodified smartctl and smartd userland binaries.
Not happy w/ loop after failed ata_qc_new_init(), but needed because smartctl and smartd did not retry after failure. Likely need an option to wait for available qc? Also not sure all the error return codes are correct...
---
drivers/scsi/libata-core.c | 179 +++++++++++++++++++++++++++++++++++++++++++++ drivers/scsi/libata-scsi.c | 11 ++ drivers/scsi/libata.h | 4 + 3 files changed, 194 insertions(+)
drivers/scsi/libata-core.c +179 -0
@@ -41,12 +41,15 @@#include "scsi.h" #include <scsi/scsi_host.h> #include <linux/libata.h> +#include <linux/hdreg.h> #include <asm/io.h> #include <asm/semaphore.h> #include <asm/byteorder.h> #include "libata.h" +#define SECTOR_SIZE 512 +static unsigned int ata_busy_sleep (struct ata_port *ap,unsigned long tmout_pat,unsigned long tmout);
@@ -2897,6 +2900,182 @@ err_out:ata_qc_complete (qc,ATA_ERR ); } +/** +* ata_task_ioctl - Handler for HDIO_DRIVE_TASK ioctl +* @ap: Port associated with device @dev +* @dev: Device to whom we are issuing command +* @arg: User provided data for issuing command +* +* LOCKING: +*/ + +int ata_task_ioctl (struct ata_port *ap,struct ata_device *dev, +void __user *arg) +{ +unsigned long flags; +int rc; +int err =0 ; +u8 status; +u8 args[7 ], *argbuf = args; +int argsize =sizeof (args); +DECLARE_COMPLETION (wait); +struct ata_queued_cmd *qc; + +if (NULL == (void *)arg) +return -EINVAL ; + +if (!ata_dev_present (dev)) +return -ENODEV ; + +if (copy_from_user (argbuf, arg, argsize)) +return -EFAULT ; + +/* Execute command... */ + qc =ata_qc_new_init (ap, dev); +#if 0 +BUG_ON (qc ==NULL ); +#else /* I know this is wrong, still thinking on what else to do... */ +while (qc ==NULL ) { +msleep (150 ); + qc =ata_qc_new_init (ap, dev); + } +#endif + + qc->tf.protocol =ATA_PROT_NODATA ; + qc->tf.feature = args[1 ]; + qc->tf.nsect = args[2 ]; + qc->tf.lbal = args[3 ]; + qc->tf.lbam = args[4 ]; + qc->tf.lbah = args[5 ]; + qc->tf.command = args[0 ]; + qc->tf.flags |=ATA_TFLAG_ISADDR ; + + qc->waiting = &wait; + qc->complete_fn = ata_qc_complete_noop; + +spin_lock_irqsave (&ap->host_set->lock, flags); + rc =ata_qc_issue (qc); +spin_unlock_irqrestore (&ap->host_set->lock, flags); + +if (rc) { + err = -EIO ;/* right rc? */ + goto error; + } +else +wait_for_completion (&wait); + + status =ata_chk_status (ap); +if (!ata_ok (status)) { + err = -EIO ; + goto error; + } + +if (copy_to_user (arg, argbuf, argsize)) + err = -EFAULT ; + +error: +return err; +} + +/** +* ata_task_ioctl - Handler for HDIO_DRIVE_CMD ioctl +* @ap: Port associated with device @dev +* @dev: Device to whom we are issuing command +* @arg: User provided data for issuing command +* +* LOCKING: +*/ + +int ata_cmd_ioctl (struct ata_port *ap,struct ata_device *dev, +void __user *arg) +{ +unsigned long flags; +int rc; +int err =0 ; +u8 status; +u8 args[4 ], *argbuf = args; +int argsize =sizeof (args); +DECLARE_COMPLETION (wait); +struct ata_queued_cmd *qc; + +if (NULL == (void *)arg) +return -EINVAL ; + +if (!ata_dev_present (dev)) +return -ENODEV ; + +if (copy_from_user (args, arg, argsize)) +return -EFAULT ; + +/* Execute command... */ + qc =ata_qc_new_init (ap, dev); +#if 0 +BUG_ON (qc ==NULL ); +#else /* I know this is wrong, still thinking on what else to do... */ +while (qc ==NULL ) { +msleep (150 ); + qc =ata_qc_new_init (ap, dev); + } +#endif + +if (args[3 ]) { + argsize =sizeof (args) + (SECTOR_SIZE * args[3 ]); + argbuf =kmalloc (argsize,GFP_KERNEL ); +if (argbuf ==NULL ) +return -ENOMEM ; +memcpy (argbuf, args,sizeof (args));/* is this necessary? */ + +ata_sg_init_one (qc, (argbuf +sizeof (args)), + (argsize -sizeof (args))); + + qc->pci_dma_dir =PCI_DMA_FROMDEVICE ; + qc->tf.protocol =ATA_PROT_PIO ; + }else { + qc->tf.protocol =ATA_PROT_NODATA ; + } + + qc->nsect = args[3 ]; + qc->tf.feature = args[2 ]; +if (args[0 ] ==WIN_SMART ) {/* hack -- ide driver does this too... */ + qc->tf.nsect = args[3 ]; + qc->tf.lbal = args[1 ]; + qc->tf.lbam =0x4f ; + qc->tf.lbah =0xc2 ; + }else { + qc->tf.nsect = args[1 ]; + } + qc->tf.command = args[0 ]; + qc->tf.flags |=ATA_TFLAG_ISADDR ; + + qc->waiting = &wait; + qc->complete_fn = ata_qc_complete_noop; + +spin_lock_irqsave (&ap->host_set->lock, flags); + rc =ata_qc_issue (qc); +spin_unlock_irqrestore (&ap->host_set->lock, flags); + +if (rc) { + err = -EIO ;/* right rc? */ + goto error; + } +else +wait_for_completion (&wait); + + status =ata_chk_status (ap); +if (!ata_ok (status)) { + err = -EIO ; + goto error; + } + +if (copy_to_user ((void *)arg, argbuf, argsize)) + err = -EFAULT ; +error: +if (argsize >sizeof (args)) +kfree (argbuf); + +return err; +} +int ata_port_start (struct ata_port *ap) {struct pci_dev *pdev = ap->host_set->pdev;
drivers/scsi/libata.h +4 -0
@@ -43,6 +43,10 @@ extern void ata_dev_select(struct ata_po unsigned int wait, unsigned int can_sleep);extern void ata_tf_to_host_nolock (struct ata_port *ap,struct ata_taskfile *tf);extern void swap_buf_le16 (u16 *buf,unsigned int buf_words); +extern int ata_task_ioctl (struct ata_port *ap,struct ata_device *dev, +void __user *arg); +extern int ata_cmd_ioctl (struct ata_port *ap,struct ata_device *dev, +void __user *arg);/* libata-scsi.c */
drivers/scsi/libata-scsi.c +11 -0
@@ -29,6 +29,7 @@#include "scsi.h" #include <scsi/scsi_host.h> #include <linux/libata.h> +#include <linux/hdreg.h> #include <asm/uaccess.h> #include "libata.h"
@@ -99,6 +100,16 @@ int ata_scsi_ioctl(struct scsi_device *sreturn -EINVAL ;return 0 ; +case HDIO_DRIVE_CMD : +if (!capable (CAP_SYS_ADMIN ) || !capable (CAP_SYS_RAWIO )) +return -EACCES ; +return ata_cmd_ioctl (ap, dev, arg); + +case HDIO_DRIVE_TASK : +if (!capable (CAP_SYS_ADMIN ) || !capable (CAP_SYS_RAWIO )) +return -EACCES ; +return ata_task_ioctl (ap, dev, arg); +default : rc = -EOPNOTSUPP ;break ;
Quoted 2 lines · John W. Linville
Support for
HDIO_DRIVE_CMDandHDIO_DRIVE_TASKin libata. Useful for supporting SMART w/ unmodified smartctl and smartd userland binaries.
First let me say that it's a damn fine first attempt, and people should be able to use this for SMART until support is merged officially.
Quoted 3 lines
Not happy w/ loop after failed
ata_qc_new_init(), but needed because smartctl and smartd did not retry after failure. Likely need an option to wait for available qc? Also not sure all the error return codes are correct...
I'd like to implement it a bit differently, and I think this different method will solve some of the open questions you have.
Take a look at http://www.t10.org/ftp/t10/document.04/04-260r2.pdf
I would like to implement HDIO_DRIVE_CMD and HDIO_DRIVE_TASK completely inside libata-scsi.c. These ioctls should translate the ioctl arguments into an ATA-passthru SCSI command, and use the standard "issue a scsi command" kernel API to submit the command and wait for a result.
That implies, then, that you would add code to libata-scsi.c that translates the ATA-passthru SCSI command into an ATA command using the ata_scsi_translate() infrastructure.
Note that you'll need to make up a SCSI opcode, inside the SCSI vendor-specific opcode space, since the ATA-passthru hasn't yet been assigned an official SCSI opcode. SPC-3 (http://www.t10.org/ftp/t10/drafts/spc3/spc3r20a.pdf) lists the available opcodes in section C.3, denoted with a 'V' across all columns.
Once libata-scsi.c can handle the ATA-passthru SCSI command, implementing HDIO_DRIVE_{TASK,CMD} should be quite trivial.
And SMART support will be complete :)
Jeff
Quoted 7 lines · John W. Linville
Support for
HDIO_DRIVE_CMDandHDIO_DRIVE_TASKin libata. Useful for supporting SMART w/ unmodified smartctl and smartd userland binaries.Not happy w/ loop after failed
ata_qc_new_init(), but needed because smartctl and smartd did not retry after failure. Likely need an option to wait for available qc? Also not sure all the error return codes are correct...
YYYYYYYYYYEeeeeeeeeeeeeeeeeeeeeeeeeeehhhhhhhhhhaaaaaaaaaaaaaaaaaaa!!!!!!!!
I know it's a bit kludgy and does not really fit the philosophy of libata but it works and it lets me keep an eye on my drives *now*.
Although just for good measure I'll probably unmount and stop my raid arrays before I use it on the disks. Whats it like for locking on a busy system?
Regards,
Brad
Quoted 4 lines · Jeff Garzik
[...]
That implies, then, that you would add code tolibata-scsi.cthat translates the ATA-passthru SCSI command into an ATA command using theata_scsi_translate()infrastructure.
Speaking of which - I'm working on this and want to find like minded people playing in the libata-scsi.c/04-262r<n>.pdf sandbox, and collaborate with them.
If you're one of these people, ping me off list and we can pool our efforts.
Signature · 3 lines
[email protected] Andy Warner Voice: (612) 801-8549 Fax: (208) 575-5634
Quoted 10 lines · Brad Campbell
Quoted 7 lines · John W. Linville
Support for
HDIO_DRIVE_CMDandHDIO_DRIVE_TASKin libata. Useful for supporting SMART w/ unmodified smartctl and smartd userland binaries.Not happy w/ loop after failed
ata_qc_new_init(), but needed because smartctl and smartd did not retry after failure. Likely need an option to wait for available qc? Also not sure all the error return codes are correct...
Quoted 5 lines
YYYYYYYYYYEeeeeeeeeeeeeeeeeeeeeeeeeeehhhhhhhhhhaaaaaaaaaaaaaaaaaaa!!!!!!!!
I know it's a bit kludgy and does not really fit the philosophy of libata but it works and it lets me keep an eye on my drives *now*.
Although just for good measure I'll probably unmount and stop my raid arrays before I use it on the disks. Whats it like for locking on a busy system?
I wouldn't trust it on a busy system yet -- it submits the command to the device without checking if there is a command already outstanding.
The patch could _definitely_ corrupt data or lock your hardware, since it bypasses the SCSI mechanism that ensures that only one command is executing at a time.
Jeff
-----BEGIN PGP SIGNED MESSAGE-----
Hash: SHA1
John W. Linville wrote:
| Support for HDIO_DRIVE_CMD and HDIO_DRIVE_TASK in libata. Useful for | supporting SMART w/ unmodified smartctl and smartd userland binaries. | | Not happy w/ loop after failed ata_qc_new_init(), but needed because
smartctl
| and smartd did not retry after failure. Likely need an option to wait for | available qc? Also not sure all the error return codes are correct...
Hi,
I just tried to give it a go with libata from 2.6.9-rc1. I had to fix one rejects but the patching seemed to go fine beside that. Nevertheless after a boot with patched libata I get:
smartctl -a /dev/sda
smartctl version 5.30 Copyright (C) 2002-4 Bruce Allen Home page is http://smartmontools.sourceforge.net/
Device: ATA SAMSUNG SP1614N Version: TM10
Serial number: 0735J1FW702444
Device type: disk
Local Time is: Mon Aug 30 19:44:23 2004 CEST
Device does not support SMART
Device does not support Error Counter logging
[GLTSD (Global Logging Target Save Disable) set. Enable Save with '-S on'] Device does not support Self Test logging
I am pretty sure my drive supports SMART. I hope that I understood your post correctly that with this patch it should work.
Cheers,
Prakash
-----BEGIN PGP SIGNATURE-----
Version: GnuPG v1.2.6 (GNU/Linux)
Comment: Using GnuPG with Thunderbird - http://enigmail.mozdev.org
iD8DBQFBM2gkxU2n/+9+t5gRAh1ZAJ0T1/zurwQZg7ddQx4X2aS5x8UMIgCg0hra 3D+Bhyp5MKpLVGdh7WIdPYA=
=Axdm
-----END PGP SIGNATURE-----
Quoted 3 lines · Prakash K. Cheemplavam
I am pretty sure my drive supports SMART. I hope that I understood your post correctly that with this patch it should work.
Prakash,
You need "-d ata" on the smartctl command line (and in smartd.conf, if using smartd) in order for this to work:
smartctl -d ata -a /dev/sda
John
Quoted 6 lines · Andy Warner
Quoted 2 lines · Jeff Garzik
[...]
That implies, then, that you would add code tolibata-scsi.cthat translates the ATA-passthru SCSI command into an ATA command using theata_scsi_translate()infrastructure.
Quoted 6 lines
Speaking of which - I'm working on this and want to find like minded people playing in the libata-scsi.c/04-262r<n>.pdf sandbox, and collaborate with them.
If you're one of these people, ping me off list and we can pool our efforts.
I am trying convince someone to do it for me, since I'm working on new controller support ATM ;-)
It shouldn't be hard -- just a new function "ata_scsi_passthru_xlat" that translates the 04-262r<n>.pdf opcode into struct ata_taskfile. That's all that's needed for Step One.
John mentioned he might look at it... but why don't you guys (and everyone else interested) just work publicly on linux-ide? Share the knowledge and patches :)
Jeff
-----BEGIN PGP SIGNED MESSAGE-----
Hash: SHA1
Prakash K. Cheemplavam wrote:
| John W. Linville wrote: | | Support for HDIO_DRIVE_CMD and HDIO_DRIVE_TASK in libata. Useful for | | supporting SMART w/ unmodified smartctl and smartd userland binaries. ~ > I just tried to give it a go with libata from 2.6.9-rc1. I had to fix | one rejects but the patching seemed to go fine beside that. Nevertheless | after a boot with patched libata I get: | | smartctl -a /dev/sda
[snip]
| Device does not support SMART
Just wanted
Just wanted to say that smartctl -a -d ata /dev/sda works, as John Linville and now Bruce aLlen suggested to try.
Cheers,
Prakash
-----BEGIN PGP SIGNATURE-----
Version: GnuPG v1.2.6 (GNU/Linux)
Comment: Using GnuPG with Thunderbird - http://enigmail.mozdev.org
iD8DBQFBNDsqxU2n/+9+t5gRAinMAJ0W6sfKD4LV7uv6X9XUxWeng2dWjQCfVolo CXg7ylDp8eb6SI+C4GZz/Bk=
=N/WZ
-----END PGP SIGNATURE-----
Quoted 12 lines · Prakash K. Cheemplavam
-----BEGIN PGP SIGNED MESSAGE-----
Hash: SHA1Prakash K. Cheemplavam wrote:
| John W. Linville wrote: | | Support for HDIO_DRIVE_CMD and HDIO_DRIVE_TASK in libata. Useful for | | supporting SMART w/ unmodified smartctl and smartd userland binaries. ~ > I just tried to give it a go with libata from 2.6.9-rc1. I had to fix | one rejects but the patching seemed to go fine beside that. Nevertheless | after a boot with patched libata I get: | | smartctl -a /dev/sda
Quoted 8 lines
[snip]
| Device does not support SMART
Just wanted
Just wanted to say that smartctl -a -d ata
/dev/sdaworks, as John Linville and now Bruce aLlen suggested to try.
As I noted in another email, be careful... that patch bypasses the SCSI command synchronization, so you could potentially send a SMART command to the hardware while another command is still in progress.
Jeff
-----BEGIN PGP SIGNED MESSAGE-----
Hash: SHA1
Jeff Garzik wrote:
| Prakash K. Cheemplavam wrote: |> Just wanted to say that smartctl -a -d ata /dev/sda works, as John |> Linville and now Bruce aLlen suggested to try. | | As I noted in another email, be careful... that patch bypasses the SCSI | command synchronization, so you could potentially send a SMART command | to the hardware while another command is still in progress.
Yup, I read your warning, so I won't run a background monitor. In fact jusr gave it a quick try and saw all is fine and then I am not going to touch it for now. ;-)
Prakash
-----BEGIN PGP SIGNATURE-----
Version: GnuPG v1.2.6 (GNU/Linux)
Comment: Using GnuPG with Thunderbird - http://enigmail.mozdev.org
iD8DBQFBND3UxU2n/+9+t5gRAl9qAKDzA+TNzFDAwpV+JSSHbrLf8EVZFQCg85Ad b8mJa7ZBgt13V/sT2MRVeiI=
=haAQ
-----END PGP SIGNATURE-----