/*
 * ahci.c
 *
 *  Created on: May 11, 2013
 *      Author: root
 */

#include <kernel/ahci.h>
#include <kernel/kernel.h>
#include <kernel/pci.h>
#include <kernel/page.h>
#include <kernel/cpu.h>
#include <kernel/ide.h>
#include <kernel/mm.h>
#include <kernel/idt.h>
#include <string.h>
#include <kernel/ext2.h>
#include <kernel/irq.h>
#include <kernel/malloc.h>
#include <kernel/io_dev.h>

HBA_PORT *sata_port;
extern ata_t ata_drives[2];
struct sata_dev {
	HBA_MEM *abar;
	unsigned long nr_ports;
	unsigned long devid;
	unsigned char irq;
};
typedef struct sata_dev sata_dev_t;

sata_dev_t g_sata_instance;

/*
 * 0x0000-0x1000 - bios data
 * 0x1000-0x2000 - int86 trampoline
 * 0x2000-0x4000 - vesa data
 * 0x4000-0x5000 - AHCI cmd list base addr
 */
#define AHCI_BASE	0x4000


int ahci_identify(HBA_PORT *port);

static int check_type(HBA_PORT *port)
{
	unsigned long ssts;
	unsigned char ipm;
	unsigned char det;

	int j = 0;
	while (j<100){
		ksleep(10);
		ssts = iomem_readl((void *)port+HBA_PORT_SSTS);
		det = ssts & 0x0f;
		ipm = (ssts >> 8) & 0x0F;
		if (det == HBA_PORT_DET_PRESENT_COMMU_OK)
			break;
		j++;
	}
	if (det != HBA_PORT_DET_PRESENT_COMMU_OK )
		return AHCI_DEV_NULL;
	if (ipm != HBA_PORT_IPM_ACTIVE)
		return AHCI_DEV_NULL;

	switch (iomem_readl((void *)port+HBA_PORT_SIG)){
	case SATA_SIG_ATAPI:
		return AHCI_DEV_SATAPI;
	case SATA_SIG_SEMB:
		return AHCI_DEV_SEMB;
	case SATA_SIG_PM:
		return AHCI_DEV_PM;
	default:
		return AHCI_DEV_SATA;
	}
}

void port_rebase(HBA_PORT *port, int portno, int slots)
{
	//stop_cmd(port);	// Stop command engine

	// Command list offset: 1K*portno
	// Command list entry size = 32
	// Command list entry maxim count = 32
	// Command list maxim size = 32*32 = 1K per port
	port->clb = AHCI_BASE + (portno<<10);
	port->clbu = 0;	//do not support 64bit
	memset((void*)(port->clb), 0, 1024);

	// FIS offset: 32K+256*portno
	// FIS entry size = 256 bytes per port
	port->fb = AHCI_BASE + (32<<10) + (portno<<8);
	port->fbu = 0;
	memset((void*)(port->fb), 0, 256);

	// Command table offset: 40K + 8K*portno
	// Command table size = 256*32 = 8K per port
	HBA_CMD_HEADER *cmdheader = (HBA_CMD_HEADER*)(port->clb);
	int i;
	for (i=0; i<slots; i++){
		cmdheader[i].prdtl = 8;	// 8 prdt entries per command table
					// 256 bytes per command table, 64+16+48+16*8
		// Command table offset: 40K + 8K*portno + cmdheader_index*256
		cmdheader[i].ctba = AHCI_BASE + (40<<10) + (portno<<13) + (i<<8);
		cmdheader[i].ctbau = 0;
		memset((void*)cmdheader[i].ctba, 0, 256);
	}

	//start_cmd(port);	// Start command engine
}

void stop_cmd(HBA_PORT *port)
{
	// Clear ST (bit0)
	volatile unsigned long tmp;
	LOG_INFO("in");
	tmp = iomem_readl((void *)port+HBA_PORT_CMD);
	tmp &= (~HBA_PORT_CMD_ST);
	iomem_writel((void *)port+HBA_PORT_CMD, tmp);

	// Wait until FR (bit14), CR (bit15) are cleared
	do {
		tmp = iomem_readl((void *)port + HBA_PORT_CMD);
	} while (tmp & HBA_PORT_CMD_CR);

	do {
		tmp = iomem_readl((void *)port+HBA_PORT_CMD);
	} while (tmp & HBA_PORT_CMD_FR);

	tmp &= (~HBA_PORT_CMD_FRE);
	iomem_writel((void *)port+HBA_PORT_CMD, tmp);
	LOG_INFO("end");
}

int start_cmd(HBA_PORT *port)
{
	LOG_INFO("in");
	unsigned long tmp;
//	iomem_writel((void *)port+HBA_PORT_SERR, 0xFFFFFFFF);
//	iomem_writel((void *)port+HBA_PORT_IS, 0xFFFFFFFF);

	tmp = iomem_readl((void *)port+HBA_PORT_CMD);

	// Set FRE (bit4) and ST (bit0)
	tmp |= (HBA_PORT_CMD_FRE | HBA_PORT_CMD_ST);
	iomem_writel((void *)port+HBA_PORT_CMD, tmp);

	LOG_INFO("end");
	return 0;
}


/*
 * based on Serial ATA AHCI 1.3 Specification
 */
void probe_port(HBA_MEM *abar)
{
	unsigned int pi = iomem_readl((void *)abar + HBA_MEM_PI);
	int i = 0;
	int nr_ports = (iomem_readl((void *)abar + HBA_MEM_CAP) & HBA_MEM_CAP_NP_MASK);
	int slots = ((iomem_readl((void *)abar + HBA_MEM_CAP) & HBA_MEM_CAP_NCS_MASK) >> 8);
	LOG_INFO("number of ports:%d %d slots per port", nr_ports, slots);
	nr_ports++;
	while (i<nr_ports){
		if (pi & 1){
			//ok, we found a port implemented, make sure the port is in idle state
			HBA_PORT *port = &abar->ports[i];
			volatile unsigned long tmp;
			tmp = iomem_readl((void *)port + HBA_PORT_CMD);
			if (!(tmp & (HBA_PORT_CMD_ST | HBA_PORT_CMD_CR | HBA_PORT_CMD_FRE | HBA_PORT_CMD_FR))){
				LOG_INFO("port is not in idle state, forcing it to be");
				stop_cmd(port);
			}
			LOG_INFO("rebase port:%d", i);
			//set port command list base address and FIS base address
			port_rebase(port, i, slots);

			//det reset, disable slumber and Partial state
			//reset port, send COMRESET signal
			iomem_writel((void *)port+HBA_PORT_SCTL, 0x301);
			ksleep(50);
			iomem_writel((void *)port+HBA_PORT_SCTL, 0x300);

			if (iomem_readl((void *)abar+HBA_MEM_CAP) & HBA_MEM_CAP_SSS){
				iomem_writel((void *)port+HBA_PORT_CMD,
						iomem_readl((void *)port+HBA_PORT_CMD) | HBA_PORT_CMD_SUD | HBA_PORT_CMD_POD|HBA_PORT_CMD_ICC);
				ksleep(10);
			}
			//clear interrupt status and error status register
			iomem_writel((void *)port+HBA_PORT_IS, 0xFFFFFFFF);
			iomem_writel((void *)port+HBA_PORT_SERR, 0xFFFFFFFF);
			//enable default interrupt
			iomem_writel((void *)port+HBA_PORT_IE, HBA_PORT_IE_DEFAULT);
			//we should see 0x127 after port reset
			tmp = iomem_readl((void *)port + HBA_PORT_TFD);
			LOG_INFO("port tfd:%d", tmp);

			int dt = check_type(port);
			if (dt == AHCI_DEV_SATA){
				LOG_INFO("SATA drive found at port %d", i);
				LOG_INFO("port clb:%x", port->clb);
				sata_port = port;
			}
			else if (dt == AHCI_DEV_SATAPI){
				LOG_INFO("SATAPI drive found at port %d", i);
			}
			else if (dt == AHCI_DEV_SEMB){
				LOG_INFO("SEMB drive found at port %d", i);
			}
			else if (dt == AHCI_DEV_PM){
				LOG_INFO("PM drive found at port %d", i);
			}
			else {
				LOG_INFO("NO drive found at port %d", i);
			}
		}
		pi >>=1;
		i++;
	}

	g_sata_instance.nr_ports = nr_ports;

}

int ahci_port_reset(HBA_PORT *port)
{
	unsigned long tmp;
	LOG_INFO("in");
	stop_cmd(port);

	tmp = iomem_readl((void *)port+HBA_PORT_SERR);
	iomem_writel((void *)port+HBA_PORT_SERR, tmp);

	/*
	 * BSY or DRQ is really nasty,
	 * in linux, send interface COMRESET signal,
	 * where CLO(command list overflow) was token
	 * in FreeBSD,Spec says,sending CLO may cause hardware
	 * clear the BSY bit,
	 */

	tmp = iomem_readl((void *)port+HBA_PORT_TFD);
	if (tmp & (HBA_PORT_TFD_DRQ | HBA_PORT_TFD_BSY)){
		iomem_writel((void *)port+HBA_PORT_SCTL, 0x301);//comreset, disable p,s
		ksleep(1);
		iomem_writel((void *)port+HBA_PORT_SCTL, 0x300);
	}

	start_cmd(port);
	LOG_INFO("end");
	return 0;
}


int ahci_end_irq(HBA_PORT *port)
{
	LOG_INFO("in");
	unsigned long serr, is;
	serr = iomem_readl((void *)port+HBA_PORT_SERR);
	iomem_writel((void *)port+HBA_PORT_SERR, serr);
	//ack interrupts
	is = iomem_readl((void *)port+HBA_PORT_IS);
	iomem_writel((void *)port+HBA_PORT_IS, is);


	if (is & HBA_PORT_IS_ERR_DEF){
		ahci_port_reset(port);
	}
	LOG_INFO("end");
	return 0;

}


int ahci_interrupt(trap_frame_t *regs)
{
	//LOG_INFO("interrupt %d", regs->vector);
	LOG_INFO("interrupt:%d", regs->vector);
	HBA_MEM *abar = g_sata_instance.abar;
	HBA_PORT *port;
	unsigned long tmp;
	unsigned long irq_ack = 0;

	//read irq pending status register
	tmp = iomem_readl((void *)abar + HBA_MEM_IPS);

	int i;
	for (i=0; i<g_sata_instance.nr_ports; i++){
		port = &g_sata_instance.abar->ports[i];
		if ((tmp & (1<<i)) && port){
			ahci_end_irq(port);
			irq_ack |= (1<<i);
		}
	}
	//send ack
	iomem_writel((void *)abar+HBA_MEM_IPS, irq_ack);

	pic_send_eoi(g_sata_instance.irq);
	return 0;
}



void ahci_print_info(HBA_MEM *abar)
{
	unsigned long ver, cap, impl;
	ver = iomem_readl((void *)abar+0x10);
	cap = iomem_readl((void *)abar);
	impl = iomem_readl((void *)abar+0x0c);
	char *speed_s;
	int speed = (cap >> 20) & 0x0f;
	if (speed == 1)
		speed_s = "1.5";
	else if (speed == 2)
		speed_s = "3";
	else
		speed_s = "?";

	LOG_INFO("%x%x.%x%x %d slots %d ports %s gbps %x impl",
			(ver >>24) & 0xFF,
			(ver >> 16) & 0xFF,
			(ver >> 8) & 0xFF,
			ver & 0xFF,
			((cap >> 8) & 0x1f) +1,
			(cap & 0x1f) +1,
			speed_s,
			impl);


}


int ahci_reset(HBA_MEM *abar)
{
	volatile unsigned long tmp;
	//enable ahci
	tmp = iomem_readl((void *)abar + HBA_MEM_GHC);
	if (!(tmp & HBA_MEM_GHC_AE)){
		tmp |= HBA_MEM_GHC_AE;
		iomem_writel((void *)abar+HBA_MEM_GHC, tmp);
	}
	//send reset command
	tmp = iomem_readl(((void *)abar + HBA_MEM_GHC));
	if (!(tmp & HBA_MEM_GHC_HR)){
		tmp |= HBA_MEM_GHC_HR;
		iomem_writel((void *)abar + HBA_MEM_GHC, tmp);
		//sleep 1s
		ksleep(1000);
	}

	tmp = iomem_readl((void *)abar+HBA_MEM_GHC);
	if (tmp & HBA_MEM_GHC_HR){
		LOG_INFO("reset ahci controller timeout\n");
		return -EIO;
	}

	//reenable ahci mode
	tmp = iomem_readl((void *)abar+HBA_MEM_GHC);
	if (!(tmp & HBA_MEM_GHC_AE)){
		tmp |= HBA_MEM_GHC_AE;
		iomem_writel((void *)abar+HBA_MEM_GHC, tmp);
	}

	//clear pending interrupt
	//iomem_writel((void *)abar+HBA_MEM_IPS, 0);

	//enable interrupt
	iomem_writel((void *)abar+HBA_MEM_GHC,
			iomem_readl((void *)abar+HBA_MEM_GHC) | HBA_MEM_GHC_IE);

	return 0;
}

int sata_rw_block(ata_t *ata, void *buff, int mode, int sec_cnt)
{
	int retval;
	if (ata->flag & ATA_FLAG_DMA){
		if (ata->lba48 || ata->lba28){
			if (sec_cnt > 128){

			} else {
				//only support 2T capacity hdd
				retval = ahci_read((HBA_PORT *)ata->base, ata->currentblock, 0, sec_cnt, (char *)PA(buff));
				if (retval < 0){
					return retval;
				}

			}

		}
	}

	return 0;
}







static int sata_read_write(int device, char *buffer, size_t size, int mode)
{
	int blocks = 0;
	ata_t *ata = &ata_drives[DEV_MINOR(device)];

	if(size < ata->blocksize)
		size = ata->blocksize;

	// round up to next block
	if(size % ata->blocksize != 0)
		size = size + (size % ata->blocksize);

	// calculate number of blocks
	blocks = size / ata->blocksize;

	if (sata_rw_block(ata, (void *)buffer, mode, blocks) < 0)
		return -1;

	ata->currentblock += blocks;
	return blocks * ata->blocksize;
}


static int do_sata_seek(int device, int pos, int origin)
{
	ata_t *ata = &ata_drives[DEV_MINOR(device)];

	if (origin == SEEK_START)
		ata->currentblock = (pos / ata->blocksize) + ata->partition_block_offset;
	else if (origin == SEEK_CURRENT)
		ata->currentblock += pos / ata->blocksize;
	else
		return -1;

	return ata->currentblock * ata->blocksize;
}

int sata_read(dev_t *dev, void *buf, size_t size)
{
	return sata_read_write(dev->devid, (char *)buf, size, ATA_READ);
}

int sata_write(dev_t *dev, void *buf, size_t size)
{
	return sata_read_write(dev->devid, (char *)buf, size, ATA_WRITE);
}

int sata_seek(dev_t *dev, int pos, int whence)
{
	return do_sata_seek(dev->devid, pos, whence);
}


int sata_open(dev_t *dev)
{
	int minor = DEV_MINOR(dev->devid);
	if (ata_drives[minor].locked == 1)
		return -1;
	ata_drives[minor].locked = 1;
	return 0;

}

int sata_close(dev_t *dev)
{
	int minor = DEV_MINOR(dev->devid);
	ata_drives[minor].locked = 0;
	return 0;
}



int sata_do_request(struct _io_request *req)
{
	int minor = DEV_MINOR(req->dev->devid);
	ata_t *ata = &ata_drives[minor];
	int ret;

	if(req == NULL)
		return -EINVAL;

	if (!(req->flag & IO_TARGET_BLOCK))
		return -EINVAL;
	LOG_INFO("ata io request in");
	ata->locked = 1;
	sata_seek(req->dev, (req->_u.b_req.block_start * ata->blocksize), SEEK_START);

	ret = sata_read_write(req->dev->devid, req->buf, (req->_u.b_req.block_nums * ata->blocksize), req->io_req_type);
	if (ret < 0){
		LOG_INFO("req ret:%d", ret);
		req->io_state = IO_ERROR;
		req->error = ret;
		goto END;
	}

	req->io_state = IO_COMPLETED;
	req->error = ret;
END:
	ata->locked = 0;
	return ret;
}




static struct _dev_ops ata_ops = {
	.open = sata_open,
	.read = sata_read,
	.write = sata_write,
	.close = sata_close,
	.io_request = sata_do_request,
};





int ahci_init()
{
	pci_dev_t *dev;
	HBA_MEM *abar;
	dev = pci_lookup_dev(0x01, 0x06);
	if (dev == NULL){
		LOG_INFO("can not find ahci controller");
		return -ENODEV;
	}

	LOG_INFO("ahci controller founded, vendor id:%x dev id:%x irq:%d", dev->vendor, dev->device, dev->irq);

	if (!(dev->resource[PCI_AHCI_BAR].flag & RESOURCE_TYPE_MEM)){
		LOG_INFO("ahci error, no iomem");
		return -EIO;
	}

	unsigned long start, end;
	start = dev->resource[PCI_AHCI_BAR].start;
	end = dev->resource[PCI_AHCI_BAR].end;
	LOG_INFO("base addr:%x end:%x", start, end);
	//map iomem
	map_page(start, start, get_cr3(), PAGE_PRE | PAGE_RW, NULL);
	//enable pci memory access
	pci_dev_enable_dev(dev);
	pci_dev_set_bus_master(dev);
	abar = (HBA_MEM *)start;
	if (ahci_reset(abar) < 0){
		LOG_INFO("reset controller failed");
		//unmap page
		return -EIO;
	}

	//probe port available
	probe_port(abar);

	ahci_print_info(abar);
	//enable interrupt in pci configuration space
	pci_dev_enable_int(dev);


	g_sata_instance.irq = dev->irq;
	g_sata_instance.devid = dev->dev;
	g_sata_instance.abar = abar;

	LOG_INFO("register irq %d for ahci", dev->irq);
	register_irq(dev->irq, (unsigned long)ahci_interrupt, IRQ_REPLACE);
	enable_irq(dev->irq);

	if (sata_port){
		if (start_cmd(sata_port) < 0){
			LOG_ERROR("start port failed");
			return -EIO;
		}
	}


	ahci_identify(sata_port);

	while (1);

	register_device("sata", &ata_ops, 0, NULL);


	return 0;
}


#define ATA_DEV_BUSY 0x80
#define ATA_DEV_DRQ 0x08

// Find a free command list slot
int find_cmdslot(HBA_PORT *port)
{
	// If not set in SACT and CI, the slot is free
	unsigned long slots = (port->sact | port->ci);
	int i;
	for ( i=0; i<32; i++){
		if ((slots & 1) == 0)
			return i;
		slots >>= 1;
	}
	LOG_INFO("Cannot find free command list entry\n");
	return -1;
}

int ahci_setup_cmdfis(HBA_CMD_TBL* cmdtbl, unsigned long low, unsigned long high, unsigned long count, char cmd)
{
	FIS_REG_H2D *cmdfis = (FIS_REG_H2D*)(&cmdtbl->cfis);
	cmdfis->fis_type = FIS_TYPE_REG_H2D;
	cmdfis->c = 1;	// Command
	cmdfis->command = cmd;	//only support lba48
	cmdfis->featureh = 0x01;

	cmdfis->lba0 = (unsigned char)low;
	cmdfis->lba1 = (unsigned char)(low>>8);
	cmdfis->lba2 = (unsigned char)(low>>16);
	cmdfis->device = (1<<6);	// LBA mode

	cmdfis->lba3 = (unsigned char)(low>>24);
	cmdfis->lba4 = (unsigned char)high;
	cmdfis->lba5 = (unsigned char)(high>>8);

	cmdfis->countl = (count & 0xFF);
	cmdfis->counth = (count & 0xFF00) >> 8;

	cmdfis->control = 0x08;
	return 0;
}

int ahci_issue_cmd(HBA_PORT *port, int slot)
{
	unsigned long tmp;
	// The below loop waits until the port is no longer busy before issuing a new command
	tmp = iomem_readl((void *)port+HBA_PORT_TFD);
	while (tmp & (HBA_PORT_TFD_DRQ | HBA_PORT_TFD_BSY)){
		LOG_INFO("port is busy");
		ahci_port_reset(port);
		tmp = iomem_readl((void *)port+HBA_PORT_TFD);
	}

	iomem_writel((void *)port + HBA_PORT_SACT,
			iomem_readl((void *)port+HBA_PORT_SACT) | (1<<slot));

	// Issue command
	iomem_writel((void *)port + HBA_PORT_CI, (1<<slot));

	return 0;

}


int ahci_read(HBA_PORT *port, unsigned long startl, unsigned long starth, unsigned long count, char *buf)
{
	//port->is = (unsigned long)-1;		// Clear pending interrupt bits
	int slot = find_cmdslot(port);
	if (slot == -1)
		return -1;
	LOG_INFO("find free slot:%d", slot);
	HBA_CMD_HEADER *cmdheader = (HBA_CMD_HEADER*)port->clb;
	cmdheader += slot;
	cmdheader->cfl = sizeof(FIS_REG_H2D)/sizeof(unsigned long);	// Command FIS size
	cmdheader->w = 0;		// Read from device
	cmdheader->prdtl = (unsigned short)((count-1)>>4) + 1;	// PRDT entries count

	HBA_CMD_TBL *cmdtbl = (HBA_CMD_TBL*)(cmdheader->ctba);
	memset(cmdtbl, 0, sizeof(HBA_CMD_TBL) +
 		(cmdheader->prdtl-1)*sizeof(HBA_PRDT_ENTRY));

	// 8K bytes (16 sectors) per PRDT
	int i;
	for (i=0; i<cmdheader->prdtl-1; i++){
		cmdtbl->prdt_entry[i].dba = (unsigned long)buf;
		cmdtbl->prdt_entry[i].dbc = 8*1024;	// 8K bytes
		cmdtbl->prdt_entry[i].i = 1;
		buf += 8*1024;	// 8K bytes
		count -= 16;	// 16 sectors
	}
	// Last entry
	cmdtbl->prdt_entry[i].dba = (unsigned long)buf;
	cmdtbl->prdt_entry[i].dbc = count<<9;	// 512 bytes per sector
	cmdtbl->prdt_entry[i].i = 1;

	// Setup command
	ahci_setup_cmdfis(cmdtbl, startl, starth, count, ATA_CMD_READ_DMA_EXT);

	ahci_issue_cmd(port, slot);

	LOG_INFO("read disk end");
	return 0;
}


int ahci_identify(HBA_PORT *port)
{
	int nr = ATA_MASTER;
	short *ide_buf =(short *)kmalloc(512);

	int slot = find_cmdslot(port);

	HBA_CMD_HEADER *cmdheader = (HBA_CMD_HEADER*)port->clb;
	cmdheader += slot;
	cmdheader->cfl = sizeof(FIS_REG_H2D)/sizeof(unsigned long);	// Command FIS size
	cmdheader->w = 1;		// Read from device
	cmdheader->prdtl = 1;	// PRDT entries count


	HBA_CMD_TBL *cmdtbl = (HBA_CMD_TBL*)(cmdheader->ctba);
	memset(cmdtbl, 0, sizeof(HBA_CMD_TBL) +
	 		(cmdheader->prdtl)*sizeof(HBA_PRDT_ENTRY));

	cmdtbl->prdt_entry[0].dba = PA((unsigned long)ide_buf);
	cmdtbl->prdt_entry[0].dbc = 1<<9;
	cmdtbl->prdt_entry[0].i = 1;

	FIS_REG_H2D *cmdfis = (FIS_REG_H2D*)(&cmdtbl->cfis);
	cmdfis->fis_type = FIS_TYPE_REG_H2D;
	cmdfis->c = 1;
	cmdfis->command = ATA_CMD_IDENTIFY;

	ahci_issue_cmd(port, slot);

	ksleep(1000);

	ata_drives[nr].controller = 0;
	ata_drives[nr].cylinders = ide_buf[1];
	ata_drives[nr].heads = ide_buf[3];
	ata_drives[nr].sectors = ide_buf[6];
	ata_drives[nr].base = (unsigned long)port;
	ata_drives[nr].drive = 0;
	ata_strcpy(ata_drives[nr].serial_number,(char *)(ide_buf + 10), 20);
	ata_strcpy(ata_drives[nr].firmware_revision, (char *)(ide_buf + 23), 8);
	ata_strcpy(ata_drives[nr].model, (char *)(ide_buf + 27), 40);
	ata_drives[nr].serial_number[19] = 0;
	ata_drives[nr].firmware_revision[7] = 0;
	ata_drives[nr].model[39] = 0;

	ata_drives[nr].lba28 = ((ide_buf[49] & 0x200) == 0x200);
	ata_drives[nr].lba48 = ((ide_buf[83] & 0x400) == 0x400);
	if (ide_buf[49] & 0x100){
		ata_drives[nr].flag |= ATA_FLAG_DMA;
	}

	if (ata_drives[nr].lba28)
		ata_drives[nr].blocks = (ide_buf[61] << 16) + ide_buf[60];
	else if (ata_drives[nr].lba48)
		ata_drives[nr].blocks = (u64)(ide_buf[100]);


	ata_drives[nr].blocksize = 512;	//ide_buf[5]; it does not work in vmware

	LOG_INFO("serial number:%s\nfirmware version:%s\nmodel_number:%s\n" \
		 "cylinders:%d\nheads:%d\nsectors:%d\n%s\nblock size:%d\nblocks:%d\n",
		 ata_drives[nr].serial_number, ata_drives[nr].firmware_revision, ata_drives[nr].model,
		 ata_drives[nr].cylinders, ata_drives[nr].heads, ata_drives[nr].sectors,
		 ata_drives[nr].lba48 ? "LBA48":(ata_drives[nr].lba28 ? "LBA28":"CHS"),ata_drives[nr].blocksize, ata_drives[nr].blocks );

	kfree(ide_buf);

	return 0;

}



