linux-hotplug.vger.kernel.org archive mirror
 help / color / mirror / Atom feed
From: Tejun Heo <htejun@gmail.com>
To: linux-hotplug@vger.kernel.org
Subject: Re: Early-boot kernel panics from udev-165/extras/ata_id/ata_id.c
Date: Mon, 17 Jan 2011 15:28:18 +0000	[thread overview]
Message-ID: <20110117152818.GF27123@htj.dyndns.org> (raw)
In-Reply-To: <4D263BF6.6050305@verizon.net>

[-- Attachment #1: Type: text/plain, Size: 58 bytes --]

And, of course, forgot to attach.  Here it is.

-- 
tejun

[-- Attachment #2: test-identify-packet.c --]
[-- Type: text/x-c++src, Size: 3283 bytes --]

#include <stdio.h>
#include <stdlib.h>
#include <stdint.h>
#include <string.h>
#include <errno.h>
#include <unistd.h>
#include <fcntl.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <sys/ioctl.h>
#include <scsi/sg.h>
#include <scsi/scsi_ioctl.h>
#include <linux/bsg.h>

#define COMMAND_TIMEOUT_MSEC (30 * 1000)

static int disk_identify_packet_device_command(int	  fd,
					       void	 *buf,
					       size_t	  buf_len)
{
	struct sg_io_v4 io_v4;
	uint8_t cdb[16];
	uint8_t sense[32];
	uint8_t *desc = sense+8;
	int ret;

	/*
	 * ATA Pass-Through 16 byte command, as described in
	 *
	 *  T10 04-262r8 ATA Command Pass-Through
	 *
	 * from http://www.t10.org/ftp/t10/document.04/04-262r8.pdf
	 */
	memset(cdb, 0, sizeof(cdb));
	cdb[0] = 0x85;			/* OPERATION CODE: 16 byte pass through */
	cdb[1] = 4 << 1;		/* PROTOCOL: PIO Data-in */
	cdb[2] = 0x2e;			/* OFF_LINE=0, CK_COND=1, T_DIR=1, BYT_BLOK=1, T_LENGTH=2 */
	cdb[3] = 0;			/* FEATURES */
	cdb[4] = 0;			/* FEATURES */
	cdb[5] = 0;			/* SECTORS */
	cdb[6] = 1;			/* SECTORS */
	cdb[7] = 0;			/* LBA LOW */
	cdb[8] = 0;			/* LBA LOW */
	cdb[9] = 0;			/* LBA MID */
	cdb[10] = 0;			/* LBA MID */
	cdb[11] = 0;			/* LBA HIGH */
	cdb[12] = 0;			/* LBA HIGH */
	cdb[13] = 0;			/* DEVICE */
	cdb[14] = 0xA1;			/* Command: ATA IDENTIFY PACKET DEVICE */;
	cdb[15] = 0;			/* CONTROL */
	memset(sense, 0, sizeof(sense));

	memset(&io_v4, 0, sizeof(struct sg_io_v4));
	io_v4.guard = 'Q';
	io_v4.protocol = BSG_PROTOCOL_SCSI;
	io_v4.subprotocol = BSG_SUB_PROTOCOL_SCSI_CMD;
	io_v4.request_len = sizeof (cdb);
	io_v4.request = (uintptr_t) cdb;
	io_v4.max_response_len = sizeof (sense);
	io_v4.response = (uintptr_t) sense;
	io_v4.din_xfer_len = buf_len;
	io_v4.din_xferp = (uintptr_t) buf;
	io_v4.timeout = COMMAND_TIMEOUT_MSEC;

	ret = ioctl(fd, SG_IO, &io_v4);
	if (ret != 0) {
		/* could be that the driver doesn't do version 4, try version 3 */
		if (errno == EINVAL) {
			struct sg_io_hdr io_hdr;

			memset(&io_hdr, 0, sizeof(struct sg_io_hdr));
			io_hdr.interface_id = 'S';
			io_hdr.cmdp = (unsigned char*) cdb;
			io_hdr.cmd_len = sizeof (cdb);
			io_hdr.dxferp = buf;
			io_hdr.dxfer_len = buf_len;
			io_hdr.sbp = sense;
			io_hdr.mx_sb_len = sizeof (sense);
			io_hdr.dxfer_direction = SG_DXFER_FROM_DEV;
			io_hdr.timeout = COMMAND_TIMEOUT_MSEC;

			ret = ioctl(fd, SG_IO, &io_hdr);
			if (ret != 0)
				goto out;
		} else {
			goto out;
		}
	}

	if (!(sense[0] == 0x72 && desc[0] == 0x9 && desc[1] == 0x0c)) {
		errno = EIO;
		ret = -1;
		goto out;
	}

 out:
	return ret;
}

int main(int argc, char *argv[])
{
	char buf[2048];
	char *id;
	char *path;
	int offset = 0;
	int fd;
	FILE *fp;

	if (argc < 2) {
		fprintf(stderr,
			"Usage: test-identify-packet /dev/srX [OFFSET]\n");
		return 1;
	}

	path = argv[1];
	if (argc > 2)
		offset = atoi(argv[2]);

	if (offset < 0 || offset > 512) {
		fprintf(stderr, "offset out of range\n");
		return 1;
	}

	fd = open(path, O_RDONLY|O_NONBLOCK);
	if (fd < 0) {
		perror("open");
		return 1;
	}

	fp = popen("hexdump -Cv", "w");
	if (!fp) {
		perror("popen");
		return 1;
	}

	id = (void *)((((unsigned long)buf + 511) & ~511) + offset);
	printf("id buffer=%p\n", id);

	disk_identify_packet_device_command(fd, id, 512);

	fwrite(id, 512, 1, fp);
	fclose(fp);

	return 0;
}

  parent reply	other threads:[~2011-01-17 15:28 UTC|newest]

Thread overview: 18+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2011-01-06 22:02 Early-boot kernel panics from udev-165/extras/ata_id/ata_id.c John Stanley
2011-01-06 22:29 ` Greg KH
2011-01-07  2:13 ` Robby Workman
2011-01-07  8:06 ` Ozan Çağlayan
2011-01-10  8:10 ` Hannes Reinecke
2011-01-10 11:35 ` Ozan Çağlayan
2011-01-11 13:25 ` Tejun Heo
2011-01-17  3:53 ` John Stanley
2011-01-17  4:03 ` John Stanley
2011-01-17  5:07 ` John Stanley
2011-01-17 15:27 ` Tejun Heo
2011-01-17 15:28 ` Tejun Heo [this message]
2011-01-18  3:38 ` John Stanley
2011-01-18 15:09 ` Tejun Heo
2011-01-18 21:48 ` John Stanley
2011-01-19  2:07 ` Brad Price
2011-01-19 20:20 ` John Stanley
2011-01-20 12:59   ` [PATCH #upstream-fixes] libata: set queue DMA alignment to sector Tejun Heo

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=20110117152818.GF27123@htj.dyndns.org \
    --to=htejun@gmail.com \
    --cc=linux-hotplug@vger.kernel.org \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).