aboutsummaryrefslogtreecommitdiffstats
path: root/drivers/block
diff options
context:
space:
mode:
Diffstat (limited to 'drivers/block')
-rw-r--r--drivers/block/Kconfig2
-rw-r--r--drivers/block/Makefile1
-rw-r--r--drivers/block/amiflop.c2
-rw-r--r--drivers/block/aoe/aoeblk.c15
-rw-r--r--drivers/block/aoe/aoechr.c3
-rw-r--r--drivers/block/brd.c13
-rw-r--r--drivers/block/cciss.c94
-rw-r--r--drivers/block/cciss.h1
-rw-r--r--drivers/block/cciss_scsi.c1
-rw-r--r--drivers/block/cpqarray.c2
-rw-r--r--drivers/block/drbd/drbd_bitmap.c5
-rw-r--r--drivers/block/drbd/drbd_int.h9
-rw-r--r--drivers/block/drbd/drbd_main.c4
-rw-r--r--drivers/block/drbd/drbd_nl.c4
-rw-r--r--drivers/block/drbd/drbd_req.c8
-rw-r--r--drivers/block/floppy.c1
-rw-r--r--drivers/block/loop.c299
-rw-r--r--drivers/block/mtip32xx/Kconfig9
-rw-r--r--drivers/block/mtip32xx/Makefile5
-rw-r--r--drivers/block/mtip32xx/mtip32xx.c3651
-rw-r--r--drivers/block/mtip32xx/mtip32xx.h423
-rw-r--r--drivers/block/nbd.c69
-rw-r--r--drivers/block/paride/bpck6.c5
-rw-r--r--drivers/block/paride/pcd.c2
-rw-r--r--drivers/block/paride/pd.c3
-rw-r--r--drivers/block/paride/pf.c4
-rw-r--r--drivers/block/paride/pg.c4
-rw-r--r--drivers/block/paride/pt.c4
-rw-r--r--drivers/block/pktcdvd.c13
-rw-r--r--drivers/block/ps3disk.c1
-rw-r--r--drivers/block/ps3vram.c7
-rw-r--r--drivers/block/rbd.c107
-rw-r--r--drivers/block/swim.c1
-rw-r--r--drivers/block/swim3.c362
-rw-r--r--drivers/block/sx8.c12
-rw-r--r--drivers/block/ub.c3
-rw-r--r--drivers/block/umem.c4
-rw-r--r--drivers/block/virtio_blk.c122
-rw-r--r--drivers/block/xd.c2
-rw-r--r--drivers/block/xen-blkback/blkback.c112
-rw-r--r--drivers/block/xen-blkback/common.h139
-rw-r--r--drivers/block/xen-blkback/xenbus.c153
-rw-r--r--drivers/block/xen-blkfront.c203
-rw-r--r--drivers/block/xsysace.c10
44 files changed, 5217 insertions, 677 deletions
diff --git a/drivers/block/Kconfig b/drivers/block/Kconfig
index 35e56e1c948f..4e4c8a4a5fd3 100644
--- a/drivers/block/Kconfig
+++ b/drivers/block/Kconfig
@@ -116,6 +116,8 @@ config PARIDE
116 116
117source "drivers/block/paride/Kconfig" 117source "drivers/block/paride/Kconfig"
118 118
119source "drivers/block/mtip32xx/Kconfig"
120
119config BLK_CPQ_DA 121config BLK_CPQ_DA
120 tristate "Compaq SMART2 support" 122 tristate "Compaq SMART2 support"
121 depends on PCI && VIRT_TO_BUS 123 depends on PCI && VIRT_TO_BUS
diff --git a/drivers/block/Makefile b/drivers/block/Makefile
index 349539ad3ad9..5b795059f8fb 100644
--- a/drivers/block/Makefile
+++ b/drivers/block/Makefile
@@ -40,5 +40,6 @@ obj-$(CONFIG_XEN_BLKDEV_FRONTEND) += xen-blkfront.o
40obj-$(CONFIG_XEN_BLKDEV_BACKEND) += xen-blkback/ 40obj-$(CONFIG_XEN_BLKDEV_BACKEND) += xen-blkback/
41obj-$(CONFIG_BLK_DEV_DRBD) += drbd/ 41obj-$(CONFIG_BLK_DEV_DRBD) += drbd/
42obj-$(CONFIG_BLK_DEV_RBD) += rbd.o 42obj-$(CONFIG_BLK_DEV_RBD) += rbd.o
43obj-$(CONFIG_BLK_DEV_PCIESSD_MTIP32XX) += mtip32xx/
43 44
44swim_mod-y := swim.o swim_asm.o 45swim_mod-y := swim.o swim_asm.o
diff --git a/drivers/block/amiflop.c b/drivers/block/amiflop.c
index 8eba86bba599..386146d792d1 100644
--- a/drivers/block/amiflop.c
+++ b/drivers/block/amiflop.c
@@ -63,7 +63,7 @@
63#include <linux/mutex.h> 63#include <linux/mutex.h>
64#include <linux/amifdreg.h> 64#include <linux/amifdreg.h>
65#include <linux/amifd.h> 65#include <linux/amifd.h>
66#include <linux/buffer_head.h> 66#include <linux/fs.h>
67#include <linux/blkdev.h> 67#include <linux/blkdev.h>
68#include <linux/elevator.h> 68#include <linux/elevator.h>
69#include <linux/interrupt.h> 69#include <linux/interrupt.h>
diff --git a/drivers/block/aoe/aoeblk.c b/drivers/block/aoe/aoeblk.c
index 528f6318ded1..321de7b6c442 100644
--- a/drivers/block/aoe/aoeblk.c
+++ b/drivers/block/aoe/aoeblk.c
@@ -15,6 +15,7 @@
15#include <linux/genhd.h> 15#include <linux/genhd.h>
16#include <linux/netdevice.h> 16#include <linux/netdevice.h>
17#include <linux/mutex.h> 17#include <linux/mutex.h>
18#include <linux/export.h>
18#include "aoe.h" 19#include "aoe.h"
19 20
20static DEFINE_MUTEX(aoeblk_mutex); 21static DEFINE_MUTEX(aoeblk_mutex);
@@ -159,7 +160,7 @@ aoeblk_release(struct gendisk *disk, fmode_t mode)
159 return 0; 160 return 0;
160} 161}
161 162
162static int 163static void
163aoeblk_make_request(struct request_queue *q, struct bio *bio) 164aoeblk_make_request(struct request_queue *q, struct bio *bio)
164{ 165{
165 struct sk_buff_head queue; 166 struct sk_buff_head queue;
@@ -172,25 +173,25 @@ aoeblk_make_request(struct request_queue *q, struct bio *bio)
172 if (bio == NULL) { 173 if (bio == NULL) {
173 printk(KERN_ERR "aoe: bio is NULL\n"); 174 printk(KERN_ERR "aoe: bio is NULL\n");
174 BUG(); 175 BUG();
175 return 0; 176 return;
176 } 177 }
177 d = bio->bi_bdev->bd_disk->private_data; 178 d = bio->bi_bdev->bd_disk->private_data;
178 if (d == NULL) { 179 if (d == NULL) {
179 printk(KERN_ERR "aoe: bd_disk->private_data is NULL\n"); 180 printk(KERN_ERR "aoe: bd_disk->private_data is NULL\n");
180 BUG(); 181 BUG();
181 bio_endio(bio, -ENXIO); 182 bio_endio(bio, -ENXIO);
182 return 0; 183 return;
183 } else if (bio->bi_io_vec == NULL) { 184 } else if (bio->bi_io_vec == NULL) {
184 printk(KERN_ERR "aoe: bi_io_vec is NULL\n"); 185 printk(KERN_ERR "aoe: bi_io_vec is NULL\n");
185 BUG(); 186 BUG();
186 bio_endio(bio, -ENXIO); 187 bio_endio(bio, -ENXIO);
187 return 0; 188 return;
188 } 189 }
189 buf = mempool_alloc(d->bufpool, GFP_NOIO); 190 buf = mempool_alloc(d->bufpool, GFP_NOIO);
190 if (buf == NULL) { 191 if (buf == NULL) {
191 printk(KERN_INFO "aoe: buf allocation failure\n"); 192 printk(KERN_INFO "aoe: buf allocation failure\n");
192 bio_endio(bio, -ENOMEM); 193 bio_endio(bio, -ENOMEM);
193 return 0; 194 return;
194 } 195 }
195 memset(buf, 0, sizeof(*buf)); 196 memset(buf, 0, sizeof(*buf));
196 INIT_LIST_HEAD(&buf->bufs); 197 INIT_LIST_HEAD(&buf->bufs);
@@ -211,7 +212,7 @@ aoeblk_make_request(struct request_queue *q, struct bio *bio)
211 spin_unlock_irqrestore(&d->lock, flags); 212 spin_unlock_irqrestore(&d->lock, flags);
212 mempool_free(buf, d->bufpool); 213 mempool_free(buf, d->bufpool);
213 bio_endio(bio, -ENXIO); 214 bio_endio(bio, -ENXIO);
214 return 0; 215 return;
215 } 216 }
216 217
217 list_add_tail(&buf->bufs, &d->bufq); 218 list_add_tail(&buf->bufs, &d->bufq);
@@ -222,8 +223,6 @@ aoeblk_make_request(struct request_queue *q, struct bio *bio)
222 223
223 spin_unlock_irqrestore(&d->lock, flags); 224 spin_unlock_irqrestore(&d->lock, flags);
224 aoenet_xmit(&queue); 225 aoenet_xmit(&queue);
225
226 return 0;
227} 226}
228 227
229static int 228static int
diff --git a/drivers/block/aoe/aoechr.c b/drivers/block/aoe/aoechr.c
index 146296ca4965..e86d2062a164 100644
--- a/drivers/block/aoe/aoechr.c
+++ b/drivers/block/aoe/aoechr.c
@@ -11,6 +11,7 @@
11#include <linux/slab.h> 11#include <linux/slab.h>
12#include <linux/mutex.h> 12#include <linux/mutex.h>
13#include <linux/skbuff.h> 13#include <linux/skbuff.h>
14#include <linux/export.h>
14#include "aoe.h" 15#include "aoe.h"
15 16
16enum { 17enum {
@@ -269,7 +270,7 @@ static const struct file_operations aoe_fops = {
269 .llseek = noop_llseek, 270 .llseek = noop_llseek,
270}; 271};
271 272
272static char *aoe_devnode(struct device *dev, mode_t *mode) 273static char *aoe_devnode(struct device *dev, umode_t *mode)
273{ 274{
274 return kasprintf(GFP_KERNEL, "etherd/%s", dev_name(dev)); 275 return kasprintf(GFP_KERNEL, "etherd/%s", dev_name(dev));
275} 276}
diff --git a/drivers/block/brd.c b/drivers/block/brd.c
index dba1c32e1ddf..ec246437f5a4 100644
--- a/drivers/block/brd.c
+++ b/drivers/block/brd.c
@@ -17,7 +17,7 @@
17#include <linux/highmem.h> 17#include <linux/highmem.h>
18#include <linux/mutex.h> 18#include <linux/mutex.h>
19#include <linux/radix-tree.h> 19#include <linux/radix-tree.h>
20#include <linux/buffer_head.h> /* invalidate_bh_lrus() */ 20#include <linux/fs.h>
21#include <linux/slab.h> 21#include <linux/slab.h>
22 22
23#include <asm/uaccess.h> 23#include <asm/uaccess.h>
@@ -323,7 +323,7 @@ out:
323 return err; 323 return err;
324} 324}
325 325
326static int brd_make_request(struct request_queue *q, struct bio *bio) 326static void brd_make_request(struct request_queue *q, struct bio *bio)
327{ 327{
328 struct block_device *bdev = bio->bi_bdev; 328 struct block_device *bdev = bio->bi_bdev;
329 struct brd_device *brd = bdev->bd_disk->private_data; 329 struct brd_device *brd = bdev->bd_disk->private_data;
@@ -359,8 +359,6 @@ static int brd_make_request(struct request_queue *q, struct bio *bio)
359 359
360out: 360out:
361 bio_endio(bio, err); 361 bio_endio(bio, err);
362
363 return 0;
364} 362}
365 363
366#ifdef CONFIG_BLK_DEV_XIP 364#ifdef CONFIG_BLK_DEV_XIP
@@ -404,14 +402,13 @@ static int brd_ioctl(struct block_device *bdev, fmode_t mode,
404 error = -EBUSY; 402 error = -EBUSY;
405 if (bdev->bd_openers <= 1) { 403 if (bdev->bd_openers <= 1) {
406 /* 404 /*
407 * Invalidate the cache first, so it isn't written 405 * Kill the cache first, so it isn't written back to the
408 * back to the device. 406 * device.
409 * 407 *
410 * Another thread might instantiate more buffercache here, 408 * Another thread might instantiate more buffercache here,
411 * but there is not much we can do to close that race. 409 * but there is not much we can do to close that race.
412 */ 410 */
413 invalidate_bh_lrus(); 411 kill_bdev(bdev);
414 truncate_inode_pages(bdev->bd_inode->i_mapping, 0);
415 brd_free_pages(brd); 412 brd_free_pages(brd);
416 error = 0; 413 error = 0;
417 } 414 }
diff --git a/drivers/block/cciss.c b/drivers/block/cciss.c
index 8f4ef656a1af..b0f553b26d0f 100644
--- a/drivers/block/cciss.c
+++ b/drivers/block/cciss.c
@@ -24,6 +24,7 @@
24#include <linux/interrupt.h> 24#include <linux/interrupt.h>
25#include <linux/types.h> 25#include <linux/types.h>
26#include <linux/pci.h> 26#include <linux/pci.h>
27#include <linux/pci-aspm.h>
27#include <linux/kernel.h> 28#include <linux/kernel.h>
28#include <linux/slab.h> 29#include <linux/slab.h>
29#include <linux/delay.h> 30#include <linux/delay.h>
@@ -68,6 +69,10 @@ static int cciss_tape_cmds = 6;
68module_param(cciss_tape_cmds, int, 0644); 69module_param(cciss_tape_cmds, int, 0644);
69MODULE_PARM_DESC(cciss_tape_cmds, 70MODULE_PARM_DESC(cciss_tape_cmds,
70 "number of commands to allocate for tape devices (default: 6)"); 71 "number of commands to allocate for tape devices (default: 6)");
72static int cciss_simple_mode;
73module_param(cciss_simple_mode, int, S_IRUGO|S_IWUSR);
74MODULE_PARM_DESC(cciss_simple_mode,
75 "Use 'simple mode' rather than 'performant mode'");
71 76
72static DEFINE_MUTEX(cciss_mutex); 77static DEFINE_MUTEX(cciss_mutex);
73static struct proc_dir_entry *proc_cciss; 78static struct proc_dir_entry *proc_cciss;
@@ -176,6 +181,7 @@ static void cciss_geometry_inquiry(ctlr_info_t *h, int logvol,
176 unsigned int block_size, InquiryData_struct *inq_buff, 181 unsigned int block_size, InquiryData_struct *inq_buff,
177 drive_info_struct *drv); 182 drive_info_struct *drv);
178static void __devinit cciss_interrupt_mode(ctlr_info_t *); 183static void __devinit cciss_interrupt_mode(ctlr_info_t *);
184static int __devinit cciss_enter_simple_mode(struct ctlr_info *h);
179static void start_io(ctlr_info_t *h); 185static void start_io(ctlr_info_t *h);
180static int sendcmd_withirq(ctlr_info_t *h, __u8 cmd, void *buff, size_t size, 186static int sendcmd_withirq(ctlr_info_t *h, __u8 cmd, void *buff, size_t size,
181 __u8 page_code, unsigned char scsi3addr[], 187 __u8 page_code, unsigned char scsi3addr[],
@@ -388,7 +394,7 @@ static void cciss_seq_show_header(struct seq_file *seq)
388 h->product_name, 394 h->product_name,
389 (unsigned long)h->board_id, 395 (unsigned long)h->board_id,
390 h->firm_ver[0], h->firm_ver[1], h->firm_ver[2], 396 h->firm_ver[0], h->firm_ver[1], h->firm_ver[2],
391 h->firm_ver[3], (unsigned int)h->intr[PERF_MODE_INT], 397 h->firm_ver[3], (unsigned int)h->intr[h->intr_mode],
392 h->num_luns, 398 h->num_luns,
393 h->Qdepth, h->commands_outstanding, 399 h->Qdepth, h->commands_outstanding,
394 h->maxQsinceinit, h->max_outstanding, h->maxSG); 400 h->maxQsinceinit, h->max_outstanding, h->maxSG);
@@ -636,6 +642,18 @@ static ssize_t host_store_rescan(struct device *dev,
636} 642}
637static DEVICE_ATTR(rescan, S_IWUSR, NULL, host_store_rescan); 643static DEVICE_ATTR(rescan, S_IWUSR, NULL, host_store_rescan);
638 644
645static ssize_t host_show_transport_mode(struct device *dev,
646 struct device_attribute *attr,
647 char *buf)
648{
649 struct ctlr_info *h = to_hba(dev);
650
651 return snprintf(buf, 20, "%s\n",
652 h->transMethod & CFGTBL_Trans_Performant ?
653 "performant" : "simple");
654}
655static DEVICE_ATTR(transport_mode, S_IRUGO, host_show_transport_mode, NULL);
656
639static ssize_t dev_show_unique_id(struct device *dev, 657static ssize_t dev_show_unique_id(struct device *dev,
640 struct device_attribute *attr, 658 struct device_attribute *attr,
641 char *buf) 659 char *buf)
@@ -808,6 +826,7 @@ static DEVICE_ATTR(usage_count, S_IRUGO, cciss_show_usage_count, NULL);
808static struct attribute *cciss_host_attrs[] = { 826static struct attribute *cciss_host_attrs[] = {
809 &dev_attr_rescan.attr, 827 &dev_attr_rescan.attr,
810 &dev_attr_resettable.attr, 828 &dev_attr_resettable.attr,
829 &dev_attr_transport_mode.attr,
811 NULL 830 NULL
812}; 831};
813 832
@@ -1716,7 +1735,7 @@ static int cciss_ioctl(struct block_device *bdev, fmode_t mode,
1716 case CCISS_BIG_PASSTHRU: 1735 case CCISS_BIG_PASSTHRU:
1717 return cciss_bigpassthru(h, argp); 1736 return cciss_bigpassthru(h, argp);
1718 1737
1719 /* scsi_cmd_ioctl handles these, below, though some are not */ 1738 /* scsi_cmd_blk_ioctl handles these, below, though some are not */
1720 /* very meaningful for cciss. SG_IO is the main one people want. */ 1739 /* very meaningful for cciss. SG_IO is the main one people want. */
1721 1740
1722 case SG_GET_VERSION_NUM: 1741 case SG_GET_VERSION_NUM:
@@ -1727,9 +1746,9 @@ static int cciss_ioctl(struct block_device *bdev, fmode_t mode,
1727 case SG_EMULATED_HOST: 1746 case SG_EMULATED_HOST:
1728 case SG_IO: 1747 case SG_IO:
1729 case SCSI_IOCTL_SEND_COMMAND: 1748 case SCSI_IOCTL_SEND_COMMAND:
1730 return scsi_cmd_ioctl(disk->queue, disk, mode, cmd, argp); 1749 return scsi_cmd_blk_ioctl(bdev, mode, cmd, argp);
1731 1750
1732 /* scsi_cmd_ioctl would normally handle these, below, but */ 1751 /* scsi_cmd_blk_ioctl would normally handle these, below, but */
1733 /* they aren't a good fit for cciss, as CD-ROMs are */ 1752 /* they aren't a good fit for cciss, as CD-ROMs are */
1734 /* not supported, and we don't have any bus/target/lun */ 1753 /* not supported, and we don't have any bus/target/lun */
1735 /* which we present to the kernel. */ 1754 /* which we present to the kernel. */
@@ -2582,6 +2601,8 @@ static int fill_cmd(ctlr_info_t *h, CommandList_struct *c, __u8 cmd, void *buff,
2582 c->Request.Timeout = 0; 2601 c->Request.Timeout = 0;
2583 c->Request.CDB[0] = BMIC_WRITE; 2602 c->Request.CDB[0] = BMIC_WRITE;
2584 c->Request.CDB[6] = BMIC_CACHE_FLUSH; 2603 c->Request.CDB[6] = BMIC_CACHE_FLUSH;
2604 c->Request.CDB[7] = (size >> 8) & 0xFF;
2605 c->Request.CDB[8] = size & 0xFF;
2585 break; 2606 break;
2586 case TEST_UNIT_READY: 2607 case TEST_UNIT_READY:
2587 c->Request.CDBLen = 6; 2608 c->Request.CDBLen = 6;
@@ -3984,6 +4005,9 @@ static void __devinit cciss_put_controller_into_performant_mode(ctlr_info_t *h)
3984{ 4005{
3985 __u32 trans_support; 4006 __u32 trans_support;
3986 4007
4008 if (cciss_simple_mode)
4009 return;
4010
3987 dev_dbg(&h->pdev->dev, "Trying to put board into Performant mode\n"); 4011 dev_dbg(&h->pdev->dev, "Trying to put board into Performant mode\n");
3988 /* Attempt to put controller into performant mode if supported */ 4012 /* Attempt to put controller into performant mode if supported */
3989 /* Does board support performant mode? */ 4013 /* Does board support performant mode? */
@@ -4081,7 +4105,7 @@ static void __devinit cciss_interrupt_mode(ctlr_info_t *h)
4081default_int_mode: 4105default_int_mode:
4082#endif /* CONFIG_PCI_MSI */ 4106#endif /* CONFIG_PCI_MSI */
4083 /* if we get here we're going to use the default interrupt mode */ 4107 /* if we get here we're going to use the default interrupt mode */
4084 h->intr[PERF_MODE_INT] = h->pdev->irq; 4108 h->intr[h->intr_mode] = h->pdev->irq;
4085 return; 4109 return;
4086} 4110}
4087 4111
@@ -4298,6 +4322,10 @@ static int __devinit cciss_pci_init(ctlr_info_t *h)
4298 dev_warn(&h->pdev->dev, "controller appears to be disabled\n"); 4322 dev_warn(&h->pdev->dev, "controller appears to be disabled\n");
4299 return -ENODEV; 4323 return -ENODEV;
4300 } 4324 }
4325
4326 pci_disable_link_state(h->pdev, PCIE_LINK_STATE_L0S |
4327 PCIE_LINK_STATE_L1 | PCIE_LINK_STATE_CLKPM);
4328
4301 err = pci_enable_device(h->pdev); 4329 err = pci_enable_device(h->pdev);
4302 if (err) { 4330 if (err) {
4303 dev_warn(&h->pdev->dev, "Unable to Enable PCI device\n"); 4331 dev_warn(&h->pdev->dev, "Unable to Enable PCI device\n");
@@ -4341,6 +4369,9 @@ static int __devinit cciss_pci_init(ctlr_info_t *h)
4341 } 4369 }
4342 cciss_enable_scsi_prefetch(h); 4370 cciss_enable_scsi_prefetch(h);
4343 cciss_p600_dma_prefetch_quirk(h); 4371 cciss_p600_dma_prefetch_quirk(h);
4372 err = cciss_enter_simple_mode(h);
4373 if (err)
4374 goto err_out_free_res;
4344 cciss_put_controller_into_performant_mode(h); 4375 cciss_put_controller_into_performant_mode(h);
4345 return 0; 4376 return 0;
4346 4377
@@ -4533,6 +4564,13 @@ static int cciss_controller_hard_reset(struct pci_dev *pdev,
4533 pmcsr &= ~PCI_PM_CTRL_STATE_MASK; 4564 pmcsr &= ~PCI_PM_CTRL_STATE_MASK;
4534 pmcsr |= PCI_D0; 4565 pmcsr |= PCI_D0;
4535 pci_write_config_word(pdev, pos + PCI_PM_CTRL, pmcsr); 4566 pci_write_config_word(pdev, pos + PCI_PM_CTRL, pmcsr);
4567
4568 /*
4569 * The P600 requires a small delay when changing states.
4570 * Otherwise we may think the board did not reset and we bail.
4571 * This for kdump only and is particular to the P600.
4572 */
4573 msleep(500);
4536 } 4574 }
4537 return 0; 4575 return 0;
4538} 4576}
@@ -4843,20 +4881,20 @@ static int cciss_request_irq(ctlr_info_t *h,
4843 irqreturn_t (*intxhandler)(int, void *)) 4881 irqreturn_t (*intxhandler)(int, void *))
4844{ 4882{
4845 if (h->msix_vector || h->msi_vector) { 4883 if (h->msix_vector || h->msi_vector) {
4846 if (!request_irq(h->intr[PERF_MODE_INT], msixhandler, 4884 if (!request_irq(h->intr[h->intr_mode], msixhandler,
4847 IRQF_DISABLED, h->devname, h)) 4885 0, h->devname, h))
4848 return 0; 4886 return 0;
4849 dev_err(&h->pdev->dev, "Unable to get msi irq %d" 4887 dev_err(&h->pdev->dev, "Unable to get msi irq %d"
4850 " for %s\n", h->intr[PERF_MODE_INT], 4888 " for %s\n", h->intr[h->intr_mode],
4851 h->devname); 4889 h->devname);
4852 return -1; 4890 return -1;
4853 } 4891 }
4854 4892
4855 if (!request_irq(h->intr[PERF_MODE_INT], intxhandler, 4893 if (!request_irq(h->intr[h->intr_mode], intxhandler,
4856 IRQF_DISABLED, h->devname, h)) 4894 IRQF_SHARED, h->devname, h))
4857 return 0; 4895 return 0;
4858 dev_err(&h->pdev->dev, "Unable to get irq %d for %s\n", 4896 dev_err(&h->pdev->dev, "Unable to get irq %d for %s\n",
4859 h->intr[PERF_MODE_INT], h->devname); 4897 h->intr[h->intr_mode], h->devname);
4860 return -1; 4898 return -1;
4861} 4899}
4862 4900
@@ -4887,7 +4925,7 @@ static void cciss_undo_allocations_after_kdump_soft_reset(ctlr_info_t *h)
4887{ 4925{
4888 int ctlr = h->ctlr; 4926 int ctlr = h->ctlr;
4889 4927
4890 free_irq(h->intr[PERF_MODE_INT], h); 4928 free_irq(h->intr[h->intr_mode], h);
4891#ifdef CONFIG_PCI_MSI 4929#ifdef CONFIG_PCI_MSI
4892 if (h->msix_vector) 4930 if (h->msix_vector)
4893 pci_disable_msix(h->pdev); 4931 pci_disable_msix(h->pdev);
@@ -4953,6 +4991,7 @@ reinit_after_soft_reset:
4953 h = hba[i]; 4991 h = hba[i];
4954 h->pdev = pdev; 4992 h->pdev = pdev;
4955 h->busy_initializing = 1; 4993 h->busy_initializing = 1;
4994 h->intr_mode = cciss_simple_mode ? SIMPLE_MODE_INT : PERF_MODE_INT;
4956 INIT_LIST_HEAD(&h->cmpQ); 4995 INIT_LIST_HEAD(&h->cmpQ);
4957 INIT_LIST_HEAD(&h->reqQ); 4996 INIT_LIST_HEAD(&h->reqQ);
4958 mutex_init(&h->busy_shutting_down); 4997 mutex_init(&h->busy_shutting_down);
@@ -5009,7 +5048,7 @@ reinit_after_soft_reset:
5009 5048
5010 dev_info(&h->pdev->dev, "%s: <0x%x> at PCI %s IRQ %d%s using DAC\n", 5049 dev_info(&h->pdev->dev, "%s: <0x%x> at PCI %s IRQ %d%s using DAC\n",
5011 h->devname, pdev->device, pci_name(pdev), 5050 h->devname, pdev->device, pci_name(pdev),
5012 h->intr[PERF_MODE_INT], dac ? "" : " not"); 5051 h->intr[h->intr_mode], dac ? "" : " not");
5013 5052
5014 if (cciss_allocate_cmd_pool(h)) 5053 if (cciss_allocate_cmd_pool(h))
5015 goto clean4; 5054 goto clean4;
@@ -5056,7 +5095,7 @@ reinit_after_soft_reset:
5056 spin_lock_irqsave(&h->lock, flags); 5095 spin_lock_irqsave(&h->lock, flags);
5057 h->access.set_intr_mask(h, CCISS_INTR_OFF); 5096 h->access.set_intr_mask(h, CCISS_INTR_OFF);
5058 spin_unlock_irqrestore(&h->lock, flags); 5097 spin_unlock_irqrestore(&h->lock, flags);
5059 free_irq(h->intr[PERF_MODE_INT], h); 5098 free_irq(h->intr[h->intr_mode], h);
5060 rc = cciss_request_irq(h, cciss_msix_discard_completions, 5099 rc = cciss_request_irq(h, cciss_msix_discard_completions,
5061 cciss_intx_discard_completions); 5100 cciss_intx_discard_completions);
5062 if (rc) { 5101 if (rc) {
@@ -5126,6 +5165,7 @@ reinit_after_soft_reset:
5126 h->cciss_max_sectors = 8192; 5165 h->cciss_max_sectors = 8192;
5127 5166
5128 rebuild_lun_table(h, 1, 0); 5167 rebuild_lun_table(h, 1, 0);
5168 cciss_engage_scsi(h);
5129 h->busy_initializing = 0; 5169 h->busy_initializing = 0;
5130 return 1; 5170 return 1;
5131 5171
@@ -5133,7 +5173,7 @@ clean4:
5133 cciss_free_cmd_pool(h); 5173 cciss_free_cmd_pool(h);
5134 cciss_free_scatterlists(h); 5174 cciss_free_scatterlists(h);
5135 cciss_free_sg_chain_blocks(h->cmd_sg_list, h->nr_cmds); 5175 cciss_free_sg_chain_blocks(h->cmd_sg_list, h->nr_cmds);
5136 free_irq(h->intr[PERF_MODE_INT], h); 5176 free_irq(h->intr[h->intr_mode], h);
5137clean2: 5177clean2:
5138 unregister_blkdev(h->major, h->devname); 5178 unregister_blkdev(h->major, h->devname);
5139clean1: 5179clean1:
@@ -5172,9 +5212,31 @@ static void cciss_shutdown(struct pci_dev *pdev)
5172 if (return_code != IO_OK) 5212 if (return_code != IO_OK)
5173 dev_warn(&h->pdev->dev, "Error flushing cache\n"); 5213 dev_warn(&h->pdev->dev, "Error flushing cache\n");
5174 h->access.set_intr_mask(h, CCISS_INTR_OFF); 5214 h->access.set_intr_mask(h, CCISS_INTR_OFF);
5175 free_irq(h->intr[PERF_MODE_INT], h); 5215 free_irq(h->intr[h->intr_mode], h);
5176} 5216}
5177 5217
5218static int __devinit cciss_enter_simple_mode(struct ctlr_info *h)
5219{
5220 u32 trans_support;
5221
5222 trans_support = readl(&(h->cfgtable->TransportSupport));
5223 if (!(trans_support & SIMPLE_MODE))
5224 return -ENOTSUPP;
5225
5226 h->max_commands = readl(&(h->cfgtable->CmdsOutMax));
5227 writel(CFGTBL_Trans_Simple, &(h->cfgtable->HostWrite.TransportRequest));
5228 writel(CFGTBL_ChangeReq, h->vaddr + SA5_DOORBELL);
5229 cciss_wait_for_mode_change_ack(h);
5230 print_cfg_table(h);
5231 if (!(readl(&(h->cfgtable->TransportActive)) & CFGTBL_Trans_Simple)) {
5232 dev_warn(&h->pdev->dev, "unable to get board into simple mode\n");
5233 return -ENODEV;
5234 }
5235 h->transMethod = CFGTBL_Trans_Simple;
5236 return 0;
5237}
5238
5239
5178static void __devexit cciss_remove_one(struct pci_dev *pdev) 5240static void __devexit cciss_remove_one(struct pci_dev *pdev)
5179{ 5241{
5180 ctlr_info_t *h; 5242 ctlr_info_t *h;
diff --git a/drivers/block/cciss.h b/drivers/block/cciss.h
index c049548e68b7..7fda30e4a241 100644
--- a/drivers/block/cciss.h
+++ b/drivers/block/cciss.h
@@ -92,6 +92,7 @@ struct ctlr_info
92 unsigned int intr[4]; 92 unsigned int intr[4];
93 unsigned int msix_vector; 93 unsigned int msix_vector;
94 unsigned int msi_vector; 94 unsigned int msi_vector;
95 int intr_mode;
95 int cciss_max_sectors; 96 int cciss_max_sectors;
96 BYTE cciss_read; 97 BYTE cciss_read;
97 BYTE cciss_write; 98 BYTE cciss_write;
diff --git a/drivers/block/cciss_scsi.c b/drivers/block/cciss_scsi.c
index 951a4e33b92b..e820b68d2f6c 100644
--- a/drivers/block/cciss_scsi.c
+++ b/drivers/block/cciss_scsi.c
@@ -1720,5 +1720,6 @@ static int cciss_eh_abort_handler(struct scsi_cmnd *scsicmd)
1720/* If no tape support, then these become defined out of existence */ 1720/* If no tape support, then these become defined out of existence */
1721 1721
1722#define cciss_scsi_setup(cntl_num) 1722#define cciss_scsi_setup(cntl_num)
1723#define cciss_engage_scsi(h)
1723 1724
1724#endif /* CONFIG_CISS_SCSI_TAPE */ 1725#endif /* CONFIG_CISS_SCSI_TAPE */
diff --git a/drivers/block/cpqarray.c b/drivers/block/cpqarray.c
index b2fceb53e809..9125bbeacd4d 100644
--- a/drivers/block/cpqarray.c
+++ b/drivers/block/cpqarray.c
@@ -620,6 +620,7 @@ static int cpqarray_pci_init(ctlr_info_t *c, struct pci_dev *pdev)
620 } 620 }
621 vendor_id = pdev->vendor; 621 vendor_id = pdev->vendor;
622 device_id = pdev->device; 622 device_id = pdev->device;
623 revision = pdev->revision;
623 irq = pdev->irq; 624 irq = pdev->irq;
624 625
625 for(i=0; i<6; i++) 626 for(i=0; i<6; i++)
@@ -632,7 +633,6 @@ static int cpqarray_pci_init(ctlr_info_t *c, struct pci_dev *pdev)
632 } 633 }
633 634
634 pci_read_config_word(pdev, PCI_COMMAND, &command); 635 pci_read_config_word(pdev, PCI_COMMAND, &command);
635 pci_read_config_byte(pdev, PCI_CLASS_REVISION, &revision);
636 pci_read_config_byte(pdev, PCI_CACHE_LINE_SIZE, &cache_line_size); 636 pci_read_config_byte(pdev, PCI_CACHE_LINE_SIZE, &cache_line_size);
637 pci_read_config_byte(pdev, PCI_LATENCY_TIMER, &latency_timer); 637 pci_read_config_byte(pdev, PCI_LATENCY_TIMER, &latency_timer);
638 638
diff --git a/drivers/block/drbd/drbd_bitmap.c b/drivers/block/drbd/drbd_bitmap.c
index 7b976296b564..912f585a760f 100644
--- a/drivers/block/drbd/drbd_bitmap.c
+++ b/drivers/block/drbd/drbd_bitmap.c
@@ -378,15 +378,14 @@ static struct page **bm_realloc_pages(struct drbd_bitmap *b, unsigned long want)
378 * thread. As we have no disk yet, we are not in the IO path, 378 * thread. As we have no disk yet, we are not in the IO path,
379 * not even the IO path of the peer. */ 379 * not even the IO path of the peer. */
380 bytes = sizeof(struct page *)*want; 380 bytes = sizeof(struct page *)*want;
381 new_pages = kmalloc(bytes, GFP_KERNEL); 381 new_pages = kzalloc(bytes, GFP_KERNEL);
382 if (!new_pages) { 382 if (!new_pages) {
383 new_pages = vmalloc(bytes); 383 new_pages = vzalloc(bytes);
384 if (!new_pages) 384 if (!new_pages)
385 return NULL; 385 return NULL;
386 vmalloced = 1; 386 vmalloced = 1;
387 } 387 }
388 388
389 memset(new_pages, 0, bytes);
390 if (want >= have) { 389 if (want >= have) {
391 for (i = 0; i < have; i++) 390 for (i = 0; i < have; i++)
392 new_pages[i] = old_pages[i]; 391 new_pages[i] = old_pages[i];
diff --git a/drivers/block/drbd/drbd_int.h b/drivers/block/drbd/drbd_int.h
index ef2ceed3be4b..8d680562ba73 100644
--- a/drivers/block/drbd/drbd_int.h
+++ b/drivers/block/drbd/drbd_int.h
@@ -28,7 +28,6 @@
28 28
29#include <linux/compiler.h> 29#include <linux/compiler.h>
30#include <linux/types.h> 30#include <linux/types.h>
31#include <linux/version.h>
32#include <linux/list.h> 31#include <linux/list.h>
33#include <linux/sched.h> 32#include <linux/sched.h>
34#include <linux/bitops.h> 33#include <linux/bitops.h>
@@ -60,8 +59,8 @@
60 59
61/* module parameter, defined in drbd_main.c */ 60/* module parameter, defined in drbd_main.c */
62extern unsigned int minor_count; 61extern unsigned int minor_count;
63extern int disable_sendpage; 62extern bool disable_sendpage;
64extern int allow_oos; 63extern bool allow_oos;
65extern unsigned int cn_idx; 64extern unsigned int cn_idx;
66 65
67#ifdef CONFIG_DRBD_FAULT_INJECTION 66#ifdef CONFIG_DRBD_FAULT_INJECTION
@@ -928,7 +927,7 @@ struct drbd_md {
928#define NL_INT64(pn,pr,member) __u64 member; 927#define NL_INT64(pn,pr,member) __u64 member;
929#define NL_BIT(pn,pr,member) unsigned member:1; 928#define NL_BIT(pn,pr,member) unsigned member:1;
930#define NL_STRING(pn,pr,member,len) unsigned char member[len]; int member ## _len; 929#define NL_STRING(pn,pr,member,len) unsigned char member[len]; int member ## _len;
931#include "linux/drbd_nl.h" 930#include <linux/drbd_nl.h>
932 931
933struct drbd_backing_dev { 932struct drbd_backing_dev {
934 struct block_device *backing_bdev; 933 struct block_device *backing_bdev;
@@ -1507,7 +1506,7 @@ extern void drbd_free_mdev(struct drbd_conf *mdev);
1507extern int proc_details; 1506extern int proc_details;
1508 1507
1509/* drbd_req */ 1508/* drbd_req */
1510extern int drbd_make_request(struct request_queue *q, struct bio *bio); 1509extern void drbd_make_request(struct request_queue *q, struct bio *bio);
1511extern int drbd_read_remote(struct drbd_conf *mdev, struct drbd_request *req); 1510extern int drbd_read_remote(struct drbd_conf *mdev, struct drbd_request *req);
1512extern int drbd_merge_bvec(struct request_queue *q, struct bvec_merge_data *bvm, struct bio_vec *bvec); 1511extern int drbd_merge_bvec(struct request_queue *q, struct bvec_merge_data *bvm, struct bio_vec *bvec);
1513extern int is_valid_ar_handle(struct drbd_request *, sector_t); 1512extern int is_valid_ar_handle(struct drbd_request *, sector_t);
diff --git a/drivers/block/drbd/drbd_main.c b/drivers/block/drbd/drbd_main.c
index 0358e55356c8..211fc44f84be 100644
--- a/drivers/block/drbd/drbd_main.c
+++ b/drivers/block/drbd/drbd_main.c
@@ -117,8 +117,8 @@ module_param(fault_devs, int, 0644);
117 117
118/* module parameter, defined */ 118/* module parameter, defined */
119unsigned int minor_count = DRBD_MINOR_COUNT_DEF; 119unsigned int minor_count = DRBD_MINOR_COUNT_DEF;
120int disable_sendpage; 120bool disable_sendpage;
121int allow_oos; 121bool allow_oos;
122unsigned int cn_idx = CN_IDX_DRBD; 122unsigned int cn_idx = CN_IDX_DRBD;
123int proc_details; /* Detail level in proc drbd*/ 123int proc_details; /* Detail level in proc drbd*/
124 124
diff --git a/drivers/block/drbd/drbd_nl.c b/drivers/block/drbd/drbd_nl.c
index 0feab261e295..af2a25049bce 100644
--- a/drivers/block/drbd/drbd_nl.c
+++ b/drivers/block/drbd/drbd_nl.c
@@ -94,7 +94,7 @@ static int name ## _from_tags(struct drbd_conf *mdev, \
94 arg->member ## _len = dlen; \ 94 arg->member ## _len = dlen; \
95 memcpy(arg->member, tags, min_t(size_t, dlen, len)); \ 95 memcpy(arg->member, tags, min_t(size_t, dlen, len)); \
96 break; 96 break;
97#include "linux/drbd_nl.h" 97#include <linux/drbd_nl.h>
98 98
99/* Generate the struct to tag_list functions */ 99/* Generate the struct to tag_list functions */
100#define NL_PACKET(name, number, fields) \ 100#define NL_PACKET(name, number, fields) \
@@ -129,7 +129,7 @@ name ## _to_tags(struct drbd_conf *mdev, \
129 put_unaligned(arg->member ## _len, tags++); \ 129 put_unaligned(arg->member ## _len, tags++); \
130 memcpy(tags, arg->member, arg->member ## _len); \ 130 memcpy(tags, arg->member, arg->member ## _len); \
131 tags = (unsigned short *)((char *)tags + arg->member ## _len); 131 tags = (unsigned short *)((char *)tags + arg->member ## _len);
132#include "linux/drbd_nl.h" 132#include <linux/drbd_nl.h>
133 133
134void drbd_bcast_ev_helper(struct drbd_conf *mdev, char *helper_name); 134void drbd_bcast_ev_helper(struct drbd_conf *mdev, char *helper_name);
135void drbd_nl_send_reply(struct cn_msg *, int); 135void drbd_nl_send_reply(struct cn_msg *, int);
diff --git a/drivers/block/drbd/drbd_req.c b/drivers/block/drbd/drbd_req.c
index 3424d675b769..4a0f314086e5 100644
--- a/drivers/block/drbd/drbd_req.c
+++ b/drivers/block/drbd/drbd_req.c
@@ -1073,7 +1073,7 @@ static int drbd_fail_request_early(struct drbd_conf *mdev, int is_write)
1073 return 0; 1073 return 0;
1074} 1074}
1075 1075
1076int drbd_make_request(struct request_queue *q, struct bio *bio) 1076void drbd_make_request(struct request_queue *q, struct bio *bio)
1077{ 1077{
1078 unsigned int s_enr, e_enr; 1078 unsigned int s_enr, e_enr;
1079 struct drbd_conf *mdev = (struct drbd_conf *) q->queuedata; 1079 struct drbd_conf *mdev = (struct drbd_conf *) q->queuedata;
@@ -1081,7 +1081,7 @@ int drbd_make_request(struct request_queue *q, struct bio *bio)
1081 1081
1082 if (drbd_fail_request_early(mdev, bio_data_dir(bio) & WRITE)) { 1082 if (drbd_fail_request_early(mdev, bio_data_dir(bio) & WRITE)) {
1083 bio_endio(bio, -EPERM); 1083 bio_endio(bio, -EPERM);
1084 return 0; 1084 return;
1085 } 1085 }
1086 1086
1087 start_time = jiffies; 1087 start_time = jiffies;
@@ -1100,7 +1100,8 @@ int drbd_make_request(struct request_queue *q, struct bio *bio)
1100 1100
1101 if (likely(s_enr == e_enr)) { 1101 if (likely(s_enr == e_enr)) {
1102 inc_ap_bio(mdev, 1); 1102 inc_ap_bio(mdev, 1);
1103 return drbd_make_request_common(mdev, bio, start_time); 1103 drbd_make_request_common(mdev, bio, start_time);
1104 return;
1104 } 1105 }
1105 1106
1106 /* can this bio be split generically? 1107 /* can this bio be split generically?
@@ -1148,7 +1149,6 @@ int drbd_make_request(struct request_queue *q, struct bio *bio)
1148 1149
1149 bio_pair_release(bp); 1150 bio_pair_release(bp);
1150 } 1151 }
1151 return 0;
1152} 1152}
1153 1153
1154/* This is called by bio_add_page(). With this function we reduce 1154/* This is called by bio_add_page(). With this function we reduce
diff --git a/drivers/block/floppy.c b/drivers/block/floppy.c
index 9955a53733b2..510fb10ec45a 100644
--- a/drivers/block/floppy.c
+++ b/drivers/block/floppy.c
@@ -188,7 +188,6 @@ static int print_unex = 1;
188#include <linux/init.h> 188#include <linux/init.h>
189#include <linux/platform_device.h> 189#include <linux/platform_device.h>
190#include <linux/mod_devicetable.h> 190#include <linux/mod_devicetable.h>
191#include <linux/buffer_head.h> /* for invalidate_buffers() */
192#include <linux/mutex.h> 191#include <linux/mutex.h>
193#include <linux/io.h> 192#include <linux/io.h>
194#include <linux/uaccess.h> 193#include <linux/uaccess.h>
diff --git a/drivers/block/loop.c b/drivers/block/loop.c
index 4720c7ade0ae..f00257782fcc 100644
--- a/drivers/block/loop.c
+++ b/drivers/block/loop.c
@@ -69,13 +69,14 @@
69#include <linux/freezer.h> 69#include <linux/freezer.h>
70#include <linux/mutex.h> 70#include <linux/mutex.h>
71#include <linux/writeback.h> 71#include <linux/writeback.h>
72#include <linux/buffer_head.h> /* for invalidate_bdev() */
73#include <linux/completion.h> 72#include <linux/completion.h>
74#include <linux/highmem.h> 73#include <linux/highmem.h>
75#include <linux/kthread.h> 74#include <linux/kthread.h>
76#include <linux/splice.h> 75#include <linux/splice.h>
77#include <linux/sysfs.h> 76#include <linux/sysfs.h>
78#include <linux/miscdevice.h> 77#include <linux/miscdevice.h>
78#include <linux/falloc.h>
79
79#include <asm/uaccess.h> 80#include <asm/uaccess.h>
80 81
81static DEFINE_IDR(loop_index_idr); 82static DEFINE_IDR(loop_index_idr);
@@ -159,17 +160,19 @@ static struct loop_func_table *xfer_funcs[MAX_LO_CRYPT] = {
159 &xor_funcs 160 &xor_funcs
160}; 161};
161 162
162static loff_t get_loop_size(struct loop_device *lo, struct file *file) 163static loff_t get_size(loff_t offset, loff_t sizelimit, struct file *file)
163{ 164{
164 loff_t size, offset, loopsize; 165 loff_t size, loopsize;
165 166
166 /* Compute loopsize in bytes */ 167 /* Compute loopsize in bytes */
167 size = i_size_read(file->f_mapping->host); 168 size = i_size_read(file->f_mapping->host);
168 offset = lo->lo_offset;
169 loopsize = size - offset; 169 loopsize = size - offset;
170 if (lo->lo_sizelimit > 0 && lo->lo_sizelimit < loopsize) 170 /* offset is beyond i_size, wierd but possible */
171 loopsize = lo->lo_sizelimit; 171 if (loopsize < 0)
172 return 0;
172 173
174 if (sizelimit > 0 && sizelimit < loopsize)
175 loopsize = sizelimit;
173 /* 176 /*
174 * Unfortunately, if we want to do I/O on the device, 177 * Unfortunately, if we want to do I/O on the device,
175 * the number of 512-byte sectors has to fit into a sector_t. 178 * the number of 512-byte sectors has to fit into a sector_t.
@@ -177,17 +180,25 @@ static loff_t get_loop_size(struct loop_device *lo, struct file *file)
177 return loopsize >> 9; 180 return loopsize >> 9;
178} 181}
179 182
183static loff_t get_loop_size(struct loop_device *lo, struct file *file)
184{
185 return get_size(lo->lo_offset, lo->lo_sizelimit, file);
186}
187
180static int 188static int
181figure_loop_size(struct loop_device *lo) 189figure_loop_size(struct loop_device *lo, loff_t offset, loff_t sizelimit)
182{ 190{
183 loff_t size = get_loop_size(lo, lo->lo_backing_file); 191 loff_t size = get_size(offset, sizelimit, lo->lo_backing_file);
184 sector_t x = (sector_t)size; 192 sector_t x = (sector_t)size;
185 193
186 if (unlikely((loff_t)x != size)) 194 if (unlikely((loff_t)x != size))
187 return -EFBIG; 195 return -EFBIG;
188 196 if (lo->lo_offset != offset)
197 lo->lo_offset = offset;
198 if (lo->lo_sizelimit != sizelimit)
199 lo->lo_sizelimit = sizelimit;
189 set_capacity(lo->lo_disk, x); 200 set_capacity(lo->lo_disk, x);
190 return 0; 201 return 0;
191} 202}
192 203
193static inline int 204static inline int
@@ -203,74 +214,6 @@ lo_do_transfer(struct loop_device *lo, int cmd,
203} 214}
204 215
205/** 216/**
206 * do_lo_send_aops - helper for writing data to a loop device
207 *
208 * This is the fast version for backing filesystems which implement the address
209 * space operations write_begin and write_end.
210 */
211static int do_lo_send_aops(struct loop_device *lo, struct bio_vec *bvec,
212 loff_t pos, struct page *unused)
213{
214 struct file *file = lo->lo_backing_file; /* kudos to NFsckingS */
215 struct address_space *mapping = file->f_mapping;
216 pgoff_t index;
217 unsigned offset, bv_offs;
218 int len, ret;
219
220 mutex_lock(&mapping->host->i_mutex);
221 index = pos >> PAGE_CACHE_SHIFT;
222 offset = pos & ((pgoff_t)PAGE_CACHE_SIZE - 1);
223 bv_offs = bvec->bv_offset;
224 len = bvec->bv_len;
225 while (len > 0) {
226 sector_t IV;
227 unsigned size, copied;
228 int transfer_result;
229 struct page *page;
230 void *fsdata;
231
232 IV = ((sector_t)index << (PAGE_CACHE_SHIFT - 9))+(offset >> 9);
233 size = PAGE_CACHE_SIZE - offset;
234 if (size > len)
235 size = len;
236
237 ret = pagecache_write_begin(file, mapping, pos, size, 0,
238 &page, &fsdata);
239 if (ret)
240 goto fail;
241
242 file_update_time(file);
243
244 transfer_result = lo_do_transfer(lo, WRITE, page, offset,
245 bvec->bv_page, bv_offs, size, IV);
246 copied = size;
247 if (unlikely(transfer_result))
248 copied = 0;
249
250 ret = pagecache_write_end(file, mapping, pos, size, copied,
251 page, fsdata);
252 if (ret < 0 || ret != copied)
253 goto fail;
254
255 if (unlikely(transfer_result))
256 goto fail;
257
258 bv_offs += copied;
259 len -= copied;
260 offset = 0;
261 index++;
262 pos += copied;
263 }
264 ret = 0;
265out:
266 mutex_unlock(&mapping->host->i_mutex);
267 return ret;
268fail:
269 ret = -1;
270 goto out;
271}
272
273/**
274 * __do_lo_send_write - helper for writing data to a loop device 217 * __do_lo_send_write - helper for writing data to a loop device
275 * 218 *
276 * This helper just factors out common code between do_lo_send_direct_write() 219 * This helper just factors out common code between do_lo_send_direct_write()
@@ -297,10 +240,8 @@ static int __do_lo_send_write(struct file *file,
297/** 240/**
298 * do_lo_send_direct_write - helper for writing data to a loop device 241 * do_lo_send_direct_write - helper for writing data to a loop device
299 * 242 *
300 * This is the fast, non-transforming version for backing filesystems which do 243 * This is the fast, non-transforming version that does not need double
301 * not implement the address space operations write_begin and write_end. 244 * buffering.
302 * It uses the write file operation which should be present on all writeable
303 * filesystems.
304 */ 245 */
305static int do_lo_send_direct_write(struct loop_device *lo, 246static int do_lo_send_direct_write(struct loop_device *lo,
306 struct bio_vec *bvec, loff_t pos, struct page *page) 247 struct bio_vec *bvec, loff_t pos, struct page *page)
@@ -316,15 +257,9 @@ static int do_lo_send_direct_write(struct loop_device *lo,
316/** 257/**
317 * do_lo_send_write - helper for writing data to a loop device 258 * do_lo_send_write - helper for writing data to a loop device
318 * 259 *
319 * This is the slow, transforming version for filesystems which do not 260 * This is the slow, transforming version that needs to double buffer the
320 * implement the address space operations write_begin and write_end. It 261 * data as it cannot do the transformations in place without having direct
321 * uses the write file operation which should be present on all writeable 262 * access to the destination pages of the backing file.
322 * filesystems.
323 *
324 * Using fops->write is slower than using aops->{prepare,commit}_write in the
325 * transforming case because we need to double buffer the data as we cannot do
326 * the transformations in place as we do not have direct access to the
327 * destination pages of the backing file.
328 */ 263 */
329static int do_lo_send_write(struct loop_device *lo, struct bio_vec *bvec, 264static int do_lo_send_write(struct loop_device *lo, struct bio_vec *bvec,
330 loff_t pos, struct page *page) 265 loff_t pos, struct page *page)
@@ -350,17 +285,16 @@ static int lo_send(struct loop_device *lo, struct bio *bio, loff_t pos)
350 struct page *page = NULL; 285 struct page *page = NULL;
351 int i, ret = 0; 286 int i, ret = 0;
352 287
353 do_lo_send = do_lo_send_aops; 288 if (lo->transfer != transfer_none) {
354 if (!(lo->lo_flags & LO_FLAGS_USE_AOPS)) { 289 page = alloc_page(GFP_NOIO | __GFP_HIGHMEM);
290 if (unlikely(!page))
291 goto fail;
292 kmap(page);
293 do_lo_send = do_lo_send_write;
294 } else {
355 do_lo_send = do_lo_send_direct_write; 295 do_lo_send = do_lo_send_direct_write;
356 if (lo->transfer != transfer_none) {
357 page = alloc_page(GFP_NOIO | __GFP_HIGHMEM);
358 if (unlikely(!page))
359 goto fail;
360 kmap(page);
361 do_lo_send = do_lo_send_write;
362 }
363 } 296 }
297
364 bio_for_each_segment(bvec, bio, i) { 298 bio_for_each_segment(bvec, bio, i) {
365 ret = do_lo_send(lo, bvec, pos, page); 299 ret = do_lo_send(lo, bvec, pos, page);
366 if (ret < 0) 300 if (ret < 0)
@@ -447,7 +381,8 @@ do_lo_receive(struct loop_device *lo,
447 381
448 if (retval < 0) 382 if (retval < 0)
449 return retval; 383 return retval;
450 384 if (retval != bvec->bv_len)
385 return -EIO;
451 return 0; 386 return 0;
452} 387}
453 388
@@ -484,6 +419,29 @@ static int do_bio_filebacked(struct loop_device *lo, struct bio *bio)
484 } 419 }
485 } 420 }
486 421
422 /*
423 * We use punch hole to reclaim the free space used by the
424 * image a.k.a. discard. However we do not support discard if
425 * encryption is enabled, because it may give an attacker
426 * useful information.
427 */
428 if (bio->bi_rw & REQ_DISCARD) {
429 struct file *file = lo->lo_backing_file;
430 int mode = FALLOC_FL_PUNCH_HOLE | FALLOC_FL_KEEP_SIZE;
431
432 if ((!file->f_op->fallocate) ||
433 lo->lo_encrypt_key_size) {
434 ret = -EOPNOTSUPP;
435 goto out;
436 }
437 ret = file->f_op->fallocate(file, mode, pos,
438 bio->bi_size);
439 if (unlikely(ret && ret != -EINVAL &&
440 ret != -EOPNOTSUPP))
441 ret = -EIO;
442 goto out;
443 }
444
487 ret = lo_send(lo, bio, pos); 445 ret = lo_send(lo, bio, pos);
488 446
489 if ((bio->bi_rw & REQ_FUA) && !ret) { 447 if ((bio->bi_rw & REQ_FUA) && !ret) {
@@ -514,7 +472,7 @@ static struct bio *loop_get_bio(struct loop_device *lo)
514 return bio_list_pop(&lo->lo_bio_list); 472 return bio_list_pop(&lo->lo_bio_list);
515} 473}
516 474
517static int loop_make_request(struct request_queue *q, struct bio *old_bio) 475static void loop_make_request(struct request_queue *q, struct bio *old_bio)
518{ 476{
519 struct loop_device *lo = q->queuedata; 477 struct loop_device *lo = q->queuedata;
520 int rw = bio_rw(old_bio); 478 int rw = bio_rw(old_bio);
@@ -532,12 +490,11 @@ static int loop_make_request(struct request_queue *q, struct bio *old_bio)
532 loop_add_bio(lo, old_bio); 490 loop_add_bio(lo, old_bio);
533 wake_up(&lo->lo_event); 491 wake_up(&lo->lo_event);
534 spin_unlock_irq(&lo->lo_lock); 492 spin_unlock_irq(&lo->lo_lock);
535 return 0; 493 return;
536 494
537out: 495out:
538 spin_unlock_irq(&lo->lo_lock); 496 spin_unlock_irq(&lo->lo_lock);
539 bio_io_error(old_bio); 497 bio_io_error(old_bio);
540 return 0;
541} 498}
542 499
543struct switch_request { 500struct switch_request {
@@ -700,7 +657,7 @@ static int loop_change_fd(struct loop_device *lo, struct block_device *bdev,
700 goto out_putf; 657 goto out_putf;
701 658
702 fput(old_file); 659 fput(old_file);
703 if (max_part > 0) 660 if (lo->lo_flags & LO_FLAGS_PARTSCAN)
704 ioctl_by_bdev(bdev, BLKRRPART, 0); 661 ioctl_by_bdev(bdev, BLKRRPART, 0);
705 return 0; 662 return 0;
706 663
@@ -777,16 +734,25 @@ static ssize_t loop_attr_autoclear_show(struct loop_device *lo, char *buf)
777 return sprintf(buf, "%s\n", autoclear ? "1" : "0"); 734 return sprintf(buf, "%s\n", autoclear ? "1" : "0");
778} 735}
779 736
737static ssize_t loop_attr_partscan_show(struct loop_device *lo, char *buf)
738{
739 int partscan = (lo->lo_flags & LO_FLAGS_PARTSCAN);
740
741 return sprintf(buf, "%s\n", partscan ? "1" : "0");
742}
743
780LOOP_ATTR_RO(backing_file); 744LOOP_ATTR_RO(backing_file);
781LOOP_ATTR_RO(offset); 745LOOP_ATTR_RO(offset);
782LOOP_ATTR_RO(sizelimit); 746LOOP_ATTR_RO(sizelimit);
783LOOP_ATTR_RO(autoclear); 747LOOP_ATTR_RO(autoclear);
748LOOP_ATTR_RO(partscan);
784 749
785static struct attribute *loop_attrs[] = { 750static struct attribute *loop_attrs[] = {
786 &loop_attr_backing_file.attr, 751 &loop_attr_backing_file.attr,
787 &loop_attr_offset.attr, 752 &loop_attr_offset.attr,
788 &loop_attr_sizelimit.attr, 753 &loop_attr_sizelimit.attr,
789 &loop_attr_autoclear.attr, 754 &loop_attr_autoclear.attr,
755 &loop_attr_partscan.attr,
790 NULL, 756 NULL,
791}; 757};
792 758
@@ -807,6 +773,35 @@ static void loop_sysfs_exit(struct loop_device *lo)
807 &loop_attribute_group); 773 &loop_attribute_group);
808} 774}
809 775
776static void loop_config_discard(struct loop_device *lo)
777{
778 struct file *file = lo->lo_backing_file;
779 struct inode *inode = file->f_mapping->host;
780 struct request_queue *q = lo->lo_queue;
781
782 /*
783 * We use punch hole to reclaim the free space used by the
784 * image a.k.a. discard. However we do support discard if
785 * encryption is enabled, because it may give an attacker
786 * useful information.
787 */
788 if ((!file->f_op->fallocate) ||
789 lo->lo_encrypt_key_size) {
790 q->limits.discard_granularity = 0;
791 q->limits.discard_alignment = 0;
792 q->limits.max_discard_sectors = 0;
793 q->limits.discard_zeroes_data = 0;
794 queue_flag_clear_unlocked(QUEUE_FLAG_DISCARD, q);
795 return;
796 }
797
798 q->limits.discard_granularity = inode->i_sb->s_blocksize;
799 q->limits.discard_alignment = 0;
800 q->limits.max_discard_sectors = UINT_MAX >> 9;
801 q->limits.discard_zeroes_data = 1;
802 queue_flag_set_unlocked(QUEUE_FLAG_DISCARD, q);
803}
804
810static int loop_set_fd(struct loop_device *lo, fmode_t mode, 805static int loop_set_fd(struct loop_device *lo, fmode_t mode,
811 struct block_device *bdev, unsigned int arg) 806 struct block_device *bdev, unsigned int arg)
812{ 807{
@@ -849,35 +844,23 @@ static int loop_set_fd(struct loop_device *lo, fmode_t mode,
849 mapping = file->f_mapping; 844 mapping = file->f_mapping;
850 inode = mapping->host; 845 inode = mapping->host;
851 846
852 if (!(file->f_mode & FMODE_WRITE))
853 lo_flags |= LO_FLAGS_READ_ONLY;
854
855 error = -EINVAL; 847 error = -EINVAL;
856 if (S_ISREG(inode->i_mode) || S_ISBLK(inode->i_mode)) { 848 if (!S_ISREG(inode->i_mode) && !S_ISBLK(inode->i_mode))
857 const struct address_space_operations *aops = mapping->a_ops; 849 goto out_putf;
858
859 if (aops->write_begin)
860 lo_flags |= LO_FLAGS_USE_AOPS;
861 if (!(lo_flags & LO_FLAGS_USE_AOPS) && !file->f_op->write)
862 lo_flags |= LO_FLAGS_READ_ONLY;
863 850
864 lo_blocksize = S_ISBLK(inode->i_mode) ? 851 if (!(file->f_mode & FMODE_WRITE) || !(mode & FMODE_WRITE) ||
865 inode->i_bdev->bd_block_size : PAGE_SIZE; 852 !file->f_op->write)
853 lo_flags |= LO_FLAGS_READ_ONLY;
866 854
867 error = 0; 855 lo_blocksize = S_ISBLK(inode->i_mode) ?
868 } else { 856 inode->i_bdev->bd_block_size : PAGE_SIZE;
869 goto out_putf;
870 }
871 857
858 error = -EFBIG;
872 size = get_loop_size(lo, file); 859 size = get_loop_size(lo, file);
873 860 if ((loff_t)(sector_t)size != size)
874 if ((loff_t)(sector_t)size != size) {
875 error = -EFBIG;
876 goto out_putf; 861 goto out_putf;
877 }
878 862
879 if (!(mode & FMODE_WRITE)) 863 error = 0;
880 lo_flags |= LO_FLAGS_READ_ONLY;
881 864
882 set_device_ro(bdev, (lo_flags & LO_FLAGS_READ_ONLY) != 0); 865 set_device_ro(bdev, (lo_flags & LO_FLAGS_READ_ONLY) != 0);
883 866
@@ -919,7 +902,9 @@ static int loop_set_fd(struct loop_device *lo, fmode_t mode,
919 } 902 }
920 lo->lo_state = Lo_bound; 903 lo->lo_state = Lo_bound;
921 wake_up_process(lo->lo_thread); 904 wake_up_process(lo->lo_thread);
922 if (max_part > 0) 905 if (part_shift)
906 lo->lo_flags |= LO_FLAGS_PARTSCAN;
907 if (lo->lo_flags & LO_FLAGS_PARTSCAN)
923 ioctl_by_bdev(bdev, BLKRRPART, 0); 908 ioctl_by_bdev(bdev, BLKRRPART, 0);
924 return 0; 909 return 0;
925 910
@@ -980,10 +965,11 @@ loop_init_xfer(struct loop_device *lo, struct loop_func_table *xfer,
980 return err; 965 return err;
981} 966}
982 967
983static int loop_clr_fd(struct loop_device *lo, struct block_device *bdev) 968static int loop_clr_fd(struct loop_device *lo)
984{ 969{
985 struct file *filp = lo->lo_backing_file; 970 struct file *filp = lo->lo_backing_file;
986 gfp_t gfp = lo->old_gfp_mask; 971 gfp_t gfp = lo->old_gfp_mask;
972 struct block_device *bdev = lo->lo_device;
987 973
988 if (lo->lo_state != Lo_bound) 974 if (lo->lo_state != Lo_bound)
989 return -ENXIO; 975 return -ENXIO;
@@ -1012,7 +998,6 @@ static int loop_clr_fd(struct loop_device *lo, struct block_device *bdev)
1012 lo->lo_offset = 0; 998 lo->lo_offset = 0;
1013 lo->lo_sizelimit = 0; 999 lo->lo_sizelimit = 0;
1014 lo->lo_encrypt_key_size = 0; 1000 lo->lo_encrypt_key_size = 0;
1015 lo->lo_flags = 0;
1016 lo->lo_thread = NULL; 1001 lo->lo_thread = NULL;
1017 memset(lo->lo_encrypt_key, 0, LO_KEY_SIZE); 1002 memset(lo->lo_encrypt_key, 0, LO_KEY_SIZE);
1018 memset(lo->lo_crypt_name, 0, LO_NAME_SIZE); 1003 memset(lo->lo_crypt_name, 0, LO_NAME_SIZE);
@@ -1030,8 +1015,11 @@ static int loop_clr_fd(struct loop_device *lo, struct block_device *bdev)
1030 lo->lo_state = Lo_unbound; 1015 lo->lo_state = Lo_unbound;
1031 /* This is safe: open() is still holding a reference. */ 1016 /* This is safe: open() is still holding a reference. */
1032 module_put(THIS_MODULE); 1017 module_put(THIS_MODULE);
1033 if (max_part > 0 && bdev) 1018 if (lo->lo_flags & LO_FLAGS_PARTSCAN && bdev)
1034 ioctl_by_bdev(bdev, BLKRRPART, 0); 1019 ioctl_by_bdev(bdev, BLKRRPART, 0);
1020 lo->lo_flags = 0;
1021 if (!part_shift)
1022 lo->lo_disk->flags |= GENHD_FL_NO_PART_SCAN;
1035 mutex_unlock(&lo->lo_ctl_mutex); 1023 mutex_unlock(&lo->lo_ctl_mutex);
1036 /* 1024 /*
1037 * Need not hold lo_ctl_mutex to fput backing file. 1025 * Need not hold lo_ctl_mutex to fput backing file.
@@ -1080,11 +1068,10 @@ loop_set_status(struct loop_device *lo, const struct loop_info64 *info)
1080 1068
1081 if (lo->lo_offset != info->lo_offset || 1069 if (lo->lo_offset != info->lo_offset ||
1082 lo->lo_sizelimit != info->lo_sizelimit) { 1070 lo->lo_sizelimit != info->lo_sizelimit) {
1083 lo->lo_offset = info->lo_offset; 1071 if (figure_loop_size(lo, info->lo_offset, info->lo_sizelimit))
1084 lo->lo_sizelimit = info->lo_sizelimit;
1085 if (figure_loop_size(lo))
1086 return -EFBIG; 1072 return -EFBIG;
1087 } 1073 }
1074 loop_config_discard(lo);
1088 1075
1089 memcpy(lo->lo_file_name, info->lo_file_name, LO_NAME_SIZE); 1076 memcpy(lo->lo_file_name, info->lo_file_name, LO_NAME_SIZE);
1090 memcpy(lo->lo_crypt_name, info->lo_crypt_name, LO_NAME_SIZE); 1077 memcpy(lo->lo_crypt_name, info->lo_crypt_name, LO_NAME_SIZE);
@@ -1100,6 +1087,13 @@ loop_set_status(struct loop_device *lo, const struct loop_info64 *info)
1100 (info->lo_flags & LO_FLAGS_AUTOCLEAR)) 1087 (info->lo_flags & LO_FLAGS_AUTOCLEAR))
1101 lo->lo_flags ^= LO_FLAGS_AUTOCLEAR; 1088 lo->lo_flags ^= LO_FLAGS_AUTOCLEAR;
1102 1089
1090 if ((info->lo_flags & LO_FLAGS_PARTSCAN) &&
1091 !(lo->lo_flags & LO_FLAGS_PARTSCAN)) {
1092 lo->lo_flags |= LO_FLAGS_PARTSCAN;
1093 lo->lo_disk->flags &= ~GENHD_FL_NO_PART_SCAN;
1094 ioctl_by_bdev(lo->lo_device, BLKRRPART, 0);
1095 }
1096
1103 lo->lo_encrypt_key_size = info->lo_encrypt_key_size; 1097 lo->lo_encrypt_key_size = info->lo_encrypt_key_size;
1104 lo->lo_init[0] = info->lo_init[0]; 1098 lo->lo_init[0] = info->lo_init[0];
1105 lo->lo_init[1] = info->lo_init[1]; 1099 lo->lo_init[1] = info->lo_init[1];
@@ -1260,7 +1254,7 @@ static int loop_set_capacity(struct loop_device *lo, struct block_device *bdev)
1260 err = -ENXIO; 1254 err = -ENXIO;
1261 if (unlikely(lo->lo_state != Lo_bound)) 1255 if (unlikely(lo->lo_state != Lo_bound))
1262 goto out; 1256 goto out;
1263 err = figure_loop_size(lo); 1257 err = figure_loop_size(lo, lo->lo_offset, lo->lo_sizelimit);
1264 if (unlikely(err)) 1258 if (unlikely(err))
1265 goto out; 1259 goto out;
1266 sec = get_capacity(lo->lo_disk); 1260 sec = get_capacity(lo->lo_disk);
@@ -1293,18 +1287,24 @@ static int lo_ioctl(struct block_device *bdev, fmode_t mode,
1293 break; 1287 break;
1294 case LOOP_CLR_FD: 1288 case LOOP_CLR_FD:
1295 /* loop_clr_fd would have unlocked lo_ctl_mutex on success */ 1289 /* loop_clr_fd would have unlocked lo_ctl_mutex on success */
1296 err = loop_clr_fd(lo, bdev); 1290 err = loop_clr_fd(lo);
1297 if (!err) 1291 if (!err)
1298 goto out_unlocked; 1292 goto out_unlocked;
1299 break; 1293 break;
1300 case LOOP_SET_STATUS: 1294 case LOOP_SET_STATUS:
1301 err = loop_set_status_old(lo, (struct loop_info __user *) arg); 1295 err = -EPERM;
1296 if ((mode & FMODE_WRITE) || capable(CAP_SYS_ADMIN))
1297 err = loop_set_status_old(lo,
1298 (struct loop_info __user *)arg);
1302 break; 1299 break;
1303 case LOOP_GET_STATUS: 1300 case LOOP_GET_STATUS:
1304 err = loop_get_status_old(lo, (struct loop_info __user *) arg); 1301 err = loop_get_status_old(lo, (struct loop_info __user *) arg);
1305 break; 1302 break;
1306 case LOOP_SET_STATUS64: 1303 case LOOP_SET_STATUS64:
1307 err = loop_set_status64(lo, (struct loop_info64 __user *) arg); 1304 err = -EPERM;
1305 if ((mode & FMODE_WRITE) || capable(CAP_SYS_ADMIN))
1306 err = loop_set_status64(lo,
1307 (struct loop_info64 __user *) arg);
1308 break; 1308 break;
1309 case LOOP_GET_STATUS64: 1309 case LOOP_GET_STATUS64:
1310 err = loop_get_status64(lo, (struct loop_info64 __user *) arg); 1310 err = loop_get_status64(lo, (struct loop_info64 __user *) arg);
@@ -1513,7 +1513,7 @@ static int lo_release(struct gendisk *disk, fmode_t mode)
1513 * In autoclear mode, stop the loop thread 1513 * In autoclear mode, stop the loop thread
1514 * and remove configuration after last close. 1514 * and remove configuration after last close.
1515 */ 1515 */
1516 err = loop_clr_fd(lo, NULL); 1516 err = loop_clr_fd(lo);
1517 if (!err) 1517 if (!err)
1518 goto out_unlocked; 1518 goto out_unlocked;
1519 } else { 1519 } else {
@@ -1635,6 +1635,27 @@ static int loop_add(struct loop_device **l, int i)
1635 if (!disk) 1635 if (!disk)
1636 goto out_free_queue; 1636 goto out_free_queue;
1637 1637
1638 /*
1639 * Disable partition scanning by default. The in-kernel partition
1640 * scanning can be requested individually per-device during its
1641 * setup. Userspace can always add and remove partitions from all
1642 * devices. The needed partition minors are allocated from the
1643 * extended minor space, the main loop device numbers will continue
1644 * to match the loop minors, regardless of the number of partitions
1645 * used.
1646 *
1647 * If max_part is given, partition scanning is globally enabled for
1648 * all loop devices. The minors for the main loop devices will be
1649 * multiples of max_part.
1650 *
1651 * Note: Global-for-all-devices, set-only-at-init, read-only module
1652 * parameteters like 'max_loop' and 'max_part' make things needlessly
1653 * complicated, are too static, inflexible and may surprise
1654 * userspace tools. Parameters like this in general should be avoided.
1655 */
1656 if (!part_shift)
1657 disk->flags |= GENHD_FL_NO_PART_SCAN;
1658 disk->flags |= GENHD_FL_EXT_DEVT;
1638 mutex_init(&lo->lo_ctl_mutex); 1659 mutex_init(&lo->lo_ctl_mutex);
1639 lo->lo_number = i; 1660 lo->lo_number = i;
1640 lo->lo_thread = NULL; 1661 lo->lo_thread = NULL;
diff --git a/drivers/block/mtip32xx/Kconfig b/drivers/block/mtip32xx/Kconfig
new file mode 100644
index 000000000000..b5dd14e072f2
--- /dev/null
+++ b/drivers/block/mtip32xx/Kconfig
@@ -0,0 +1,9 @@
1#
2# mtip32xx device driver configuration
3#
4
5config BLK_DEV_PCIESSD_MTIP32XX
6 tristate "Block Device Driver for Micron PCIe SSDs"
7 depends on HOTPLUG_PCI_PCIE
8 help
9 This enables the block driver for Micron PCIe SSDs.
diff --git a/drivers/block/mtip32xx/Makefile b/drivers/block/mtip32xx/Makefile
new file mode 100644
index 000000000000..4fbef8c8329b
--- /dev/null
+++ b/drivers/block/mtip32xx/Makefile
@@ -0,0 +1,5 @@
1#
2# Makefile for Block device driver for Micron PCIe SSD
3#
4
5obj-$(CONFIG_BLK_DEV_PCIESSD_MTIP32XX) += mtip32xx.o
diff --git a/drivers/block/mtip32xx/mtip32xx.c b/drivers/block/mtip32xx/mtip32xx.c
new file mode 100644
index 000000000000..b74eab70c3d0
--- /dev/null
+++ b/drivers/block/mtip32xx/mtip32xx.c
@@ -0,0 +1,3651 @@
1/*
2 * Driver for the Micron P320 SSD
3 * Copyright (C) 2011 Micron Technology, Inc.
4 *
5 * Portions of this code were derived from works subjected to the
6 * following copyright:
7 * Copyright (C) 2009 Integrated Device Technology, Inc.
8 *
9 * This program is free software; you can redistribute it and/or modify
10 * it under the terms of the GNU General Public License as published by
11 * the Free Software Foundation; either version 2 of the License, or
12 * (at your option) any later version.
13 *
14 * This program is distributed in the hope that it will be useful,
15 * but WITHOUT ANY WARRANTY; without even the implied warranty of
16 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17 * GNU General Public License for more details.
18 *
19 */
20
21#include <linux/pci.h>
22#include <linux/interrupt.h>
23#include <linux/ata.h>
24#include <linux/delay.h>
25#include <linux/hdreg.h>
26#include <linux/uaccess.h>
27#include <linux/random.h>
28#include <linux/smp.h>
29#include <linux/compat.h>
30#include <linux/fs.h>
31#include <linux/module.h>
32#include <linux/genhd.h>
33#include <linux/blkdev.h>
34#include <linux/bio.h>
35#include <linux/dma-mapping.h>
36#include <linux/idr.h>
37#include <linux/kthread.h>
38#include <../drivers/ata/ahci.h>
39#include "mtip32xx.h"
40
41#define HW_CMD_SLOT_SZ (MTIP_MAX_COMMAND_SLOTS * 32)
42#define HW_CMD_TBL_SZ (AHCI_CMD_TBL_HDR_SZ + (MTIP_MAX_SG * 16))
43#define HW_CMD_TBL_AR_SZ (HW_CMD_TBL_SZ * MTIP_MAX_COMMAND_SLOTS)
44#define HW_PORT_PRIV_DMA_SZ \
45 (HW_CMD_SLOT_SZ + HW_CMD_TBL_AR_SZ + AHCI_RX_FIS_SZ)
46
47#define HOST_HSORG 0xFC
48#define HSORG_DISABLE_SLOTGRP_INTR (1<<24)
49#define HSORG_DISABLE_SLOTGRP_PXIS (1<<16)
50#define HSORG_HWREV 0xFF00
51#define HSORG_STYLE 0x8
52#define HSORG_SLOTGROUPS 0x7
53
54#define PORT_COMMAND_ISSUE 0x38
55#define PORT_SDBV 0x7C
56
57#define PORT_OFFSET 0x100
58#define PORT_MEM_SIZE 0x80
59
60#define PORT_IRQ_ERR \
61 (PORT_IRQ_HBUS_ERR | PORT_IRQ_IF_ERR | PORT_IRQ_CONNECT | \
62 PORT_IRQ_PHYRDY | PORT_IRQ_UNK_FIS | PORT_IRQ_BAD_PMP | \
63 PORT_IRQ_TF_ERR | PORT_IRQ_HBUS_DATA_ERR | PORT_IRQ_IF_NONFATAL | \
64 PORT_IRQ_OVERFLOW)
65#define PORT_IRQ_LEGACY \
66 (PORT_IRQ_PIOS_FIS | PORT_IRQ_D2H_REG_FIS)
67#define PORT_IRQ_HANDLED \
68 (PORT_IRQ_SDB_FIS | PORT_IRQ_LEGACY | \
69 PORT_IRQ_TF_ERR | PORT_IRQ_IF_ERR | \
70 PORT_IRQ_CONNECT | PORT_IRQ_PHYRDY)
71#define DEF_PORT_IRQ \
72 (PORT_IRQ_ERR | PORT_IRQ_LEGACY | PORT_IRQ_SDB_FIS)
73
74/* product numbers */
75#define MTIP_PRODUCT_UNKNOWN 0x00
76#define MTIP_PRODUCT_ASICFPGA 0x11
77
78/* Device instance number, incremented each time a device is probed. */
79static int instance;
80
81/*
82 * Global variable used to hold the major block device number
83 * allocated in mtip_init().
84 */
85static int mtip_major;
86
87static DEFINE_SPINLOCK(rssd_index_lock);
88static DEFINE_IDA(rssd_index_ida);
89
90static int mtip_block_initialize(struct driver_data *dd);
91
92#ifdef CONFIG_COMPAT
93struct mtip_compat_ide_task_request_s {
94 __u8 io_ports[8];
95 __u8 hob_ports[8];
96 ide_reg_valid_t out_flags;
97 ide_reg_valid_t in_flags;
98 int data_phase;
99 int req_cmd;
100 compat_ulong_t out_size;
101 compat_ulong_t in_size;
102};
103#endif
104
105/*
106 * This function check_for_surprise_removal is called
107 * while card is removed from the system and it will
108 * read the vendor id from the configration space
109 *
110 * @pdev Pointer to the pci_dev structure.
111 *
112 * return value
113 * true if device removed, else false
114 */
115static bool mtip_check_surprise_removal(struct pci_dev *pdev)
116{
117 u16 vendor_id = 0;
118
119 /* Read the vendorID from the configuration space */
120 pci_read_config_word(pdev, 0x00, &vendor_id);
121 if (vendor_id == 0xFFFF)
122 return true; /* device removed */
123
124 return false; /* device present */
125}
126
127/*
128 * This function is called for clean the pending command in the
129 * command slot during the surprise removal of device and return
130 * error to the upper layer.
131 *
132 * @dd Pointer to the DRIVER_DATA structure.
133 *
134 * return value
135 * None
136 */
137static void mtip_command_cleanup(struct driver_data *dd)
138{
139 int group = 0, commandslot = 0, commandindex = 0;
140 struct mtip_cmd *command;
141 struct mtip_port *port = dd->port;
142
143 for (group = 0; group < 4; group++) {
144 for (commandslot = 0; commandslot < 32; commandslot++) {
145 if (!(port->allocated[group] & (1 << commandslot)))
146 continue;
147
148 commandindex = group << 5 | commandslot;
149 command = &port->commands[commandindex];
150
151 if (atomic_read(&command->active)
152 && (command->async_callback)) {
153 command->async_callback(command->async_data,
154 -ENODEV);
155 command->async_callback = NULL;
156 command->async_data = NULL;
157 }
158
159 dma_unmap_sg(&port->dd->pdev->dev,
160 command->sg,
161 command->scatter_ents,
162 command->direction);
163 }
164 }
165
166 up(&port->cmd_slot);
167
168 atomic_set(&dd->drv_cleanup_done, true);
169}
170
171/*
172 * Obtain an empty command slot.
173 *
174 * This function needs to be reentrant since it could be called
175 * at the same time on multiple CPUs. The allocation of the
176 * command slot must be atomic.
177 *
178 * @port Pointer to the port data structure.
179 *
180 * return value
181 * >= 0 Index of command slot obtained.
182 * -1 No command slots available.
183 */
184static int get_slot(struct mtip_port *port)
185{
186 int slot, i;
187 unsigned int num_command_slots = port->dd->slot_groups * 32;
188
189 /*
190 * Try 10 times, because there is a small race here.
191 * that's ok, because it's still cheaper than a lock.
192 *
193 * Race: Since this section is not protected by lock, same bit
194 * could be chosen by different process contexts running in
195 * different processor. So instead of costly lock, we are going
196 * with loop.
197 */
198 for (i = 0; i < 10; i++) {
199 slot = find_next_zero_bit(port->allocated,
200 num_command_slots, 1);
201 if ((slot < num_command_slots) &&
202 (!test_and_set_bit(slot, port->allocated)))
203 return slot;
204 }
205 dev_warn(&port->dd->pdev->dev, "Failed to get a tag.\n");
206
207 if (mtip_check_surprise_removal(port->dd->pdev)) {
208 /* Device not present, clean outstanding commands */
209 mtip_command_cleanup(port->dd);
210 }
211 return -1;
212}
213
214/*
215 * Release a command slot.
216 *
217 * @port Pointer to the port data structure.
218 * @tag Tag of command to release
219 *
220 * return value
221 * None
222 */
223static inline void release_slot(struct mtip_port *port, int tag)
224{
225 smp_mb__before_clear_bit();
226 clear_bit(tag, port->allocated);
227 smp_mb__after_clear_bit();
228}
229
230/*
231 * Reset the HBA (without sleeping)
232 *
233 * Just like hba_reset, except does not call sleep, so can be
234 * run from interrupt/tasklet context.
235 *
236 * @dd Pointer to the driver data structure.
237 *
238 * return value
239 * 0 The reset was successful.
240 * -1 The HBA Reset bit did not clear.
241 */
242static int hba_reset_nosleep(struct driver_data *dd)
243{
244 unsigned long timeout;
245
246 /* Chip quirk: quiesce any chip function */
247 mdelay(10);
248
249 /* Set the reset bit */
250 writel(HOST_RESET, dd->mmio + HOST_CTL);
251
252 /* Flush */
253 readl(dd->mmio + HOST_CTL);
254
255 /*
256 * Wait 10ms then spin for up to 1 second
257 * waiting for reset acknowledgement
258 */
259 timeout = jiffies + msecs_to_jiffies(1000);
260 mdelay(10);
261 while ((readl(dd->mmio + HOST_CTL) & HOST_RESET)
262 && time_before(jiffies, timeout))
263 mdelay(1);
264
265 if (readl(dd->mmio + HOST_CTL) & HOST_RESET)
266 return -1;
267
268 return 0;
269}
270
271/*
272 * Issue a command to the hardware.
273 *
274 * Set the appropriate bit in the s_active and Command Issue hardware
275 * registers, causing hardware command processing to begin.
276 *
277 * @port Pointer to the port structure.
278 * @tag The tag of the command to be issued.
279 *
280 * return value
281 * None
282 */
283static inline void mtip_issue_ncq_command(struct mtip_port *port, int tag)
284{
285 unsigned long flags = 0;
286
287 atomic_set(&port->commands[tag].active, 1);
288
289 spin_lock_irqsave(&port->cmd_issue_lock, flags);
290
291 writel((1 << MTIP_TAG_BIT(tag)),
292 port->s_active[MTIP_TAG_INDEX(tag)]);
293 writel((1 << MTIP_TAG_BIT(tag)),
294 port->cmd_issue[MTIP_TAG_INDEX(tag)]);
295
296 spin_unlock_irqrestore(&port->cmd_issue_lock, flags);
297}
298
299/*
300 * Enable/disable the reception of FIS
301 *
302 * @port Pointer to the port data structure
303 * @enable 1 to enable, 0 to disable
304 *
305 * return value
306 * Previous state: 1 enabled, 0 disabled
307 */
308static int mtip_enable_fis(struct mtip_port *port, int enable)
309{
310 u32 tmp;
311
312 /* enable FIS reception */
313 tmp = readl(port->mmio + PORT_CMD);
314 if (enable)
315 writel(tmp | PORT_CMD_FIS_RX, port->mmio + PORT_CMD);
316 else
317 writel(tmp & ~PORT_CMD_FIS_RX, port->mmio + PORT_CMD);
318
319 /* Flush */
320 readl(port->mmio + PORT_CMD);
321
322 return (((tmp & PORT_CMD_FIS_RX) == PORT_CMD_FIS_RX));
323}
324
325/*
326 * Enable/disable the DMA engine
327 *
328 * @port Pointer to the port data structure
329 * @enable 1 to enable, 0 to disable
330 *
331 * return value
332 * Previous state: 1 enabled, 0 disabled.
333 */
334static int mtip_enable_engine(struct mtip_port *port, int enable)
335{
336 u32 tmp;
337
338 /* enable FIS reception */
339 tmp = readl(port->mmio + PORT_CMD);
340 if (enable)
341 writel(tmp | PORT_CMD_START, port->mmio + PORT_CMD);
342 else
343 writel(tmp & ~PORT_CMD_START, port->mmio + PORT_CMD);
344
345 readl(port->mmio + PORT_CMD);
346 return (((tmp & PORT_CMD_START) == PORT_CMD_START));
347}
348
349/*
350 * Enables the port DMA engine and FIS reception.
351 *
352 * return value
353 * None
354 */
355static inline void mtip_start_port(struct mtip_port *port)
356{
357 /* Enable FIS reception */
358 mtip_enable_fis(port, 1);
359
360 /* Enable the DMA engine */
361 mtip_enable_engine(port, 1);
362}
363
364/*
365 * Deinitialize a port by disabling port interrupts, the DMA engine,
366 * and FIS reception.
367 *
368 * @port Pointer to the port structure
369 *
370 * return value
371 * None
372 */
373static inline void mtip_deinit_port(struct mtip_port *port)
374{
375 /* Disable interrupts on this port */
376 writel(0, port->mmio + PORT_IRQ_MASK);
377
378 /* Disable the DMA engine */
379 mtip_enable_engine(port, 0);
380
381 /* Disable FIS reception */
382 mtip_enable_fis(port, 0);
383}
384
385/*
386 * Initialize a port.
387 *
388 * This function deinitializes the port by calling mtip_deinit_port() and
389 * then initializes it by setting the command header and RX FIS addresses,
390 * clearing the SError register and any pending port interrupts before
391 * re-enabling the default set of port interrupts.
392 *
393 * @port Pointer to the port structure.
394 *
395 * return value
396 * None
397 */
398static void mtip_init_port(struct mtip_port *port)
399{
400 int i;
401 mtip_deinit_port(port);
402
403 /* Program the command list base and FIS base addresses */
404 if (readl(port->dd->mmio + HOST_CAP) & HOST_CAP_64) {
405 writel((port->command_list_dma >> 16) >> 16,
406 port->mmio + PORT_LST_ADDR_HI);
407 writel((port->rxfis_dma >> 16) >> 16,
408 port->mmio + PORT_FIS_ADDR_HI);
409 }
410
411 writel(port->command_list_dma & 0xFFFFFFFF,
412 port->mmio + PORT_LST_ADDR);
413 writel(port->rxfis_dma & 0xFFFFFFFF, port->mmio + PORT_FIS_ADDR);
414
415 /* Clear SError */
416 writel(readl(port->mmio + PORT_SCR_ERR), port->mmio + PORT_SCR_ERR);
417
418 /* reset the completed registers.*/
419 for (i = 0; i < port->dd->slot_groups; i++)
420 writel(0xFFFFFFFF, port->completed[i]);
421
422 /* Clear any pending interrupts for this port */
423 writel(readl(port->mmio + PORT_IRQ_STAT), port->mmio + PORT_IRQ_STAT);
424
425 /* Enable port interrupts */
426 writel(DEF_PORT_IRQ, port->mmio + PORT_IRQ_MASK);
427}
428
429/*
430 * Restart a port
431 *
432 * @port Pointer to the port data structure.
433 *
434 * return value
435 * None
436 */
437static void mtip_restart_port(struct mtip_port *port)
438{
439 unsigned long timeout;
440
441 /* Disable the DMA engine */
442 mtip_enable_engine(port, 0);
443
444 /* Chip quirk: wait up to 500ms for PxCMD.CR == 0 */
445 timeout = jiffies + msecs_to_jiffies(500);
446 while ((readl(port->mmio + PORT_CMD) & PORT_CMD_LIST_ON)
447 && time_before(jiffies, timeout))
448 ;
449
450 /*
451 * Chip quirk: escalate to hba reset if
452 * PxCMD.CR not clear after 500 ms
453 */
454 if (readl(port->mmio + PORT_CMD) & PORT_CMD_LIST_ON) {
455 dev_warn(&port->dd->pdev->dev,
456 "PxCMD.CR not clear, escalating reset\n");
457
458 if (hba_reset_nosleep(port->dd))
459 dev_err(&port->dd->pdev->dev,
460 "HBA reset escalation failed.\n");
461
462 /* 30 ms delay before com reset to quiesce chip */
463 mdelay(30);
464 }
465
466 dev_warn(&port->dd->pdev->dev, "Issuing COM reset\n");
467
468 /* Set PxSCTL.DET */
469 writel(readl(port->mmio + PORT_SCR_CTL) |
470 1, port->mmio + PORT_SCR_CTL);
471 readl(port->mmio + PORT_SCR_CTL);
472
473 /* Wait 1 ms to quiesce chip function */
474 timeout = jiffies + msecs_to_jiffies(1);
475 while (time_before(jiffies, timeout))
476 ;
477
478 /* Clear PxSCTL.DET */
479 writel(readl(port->mmio + PORT_SCR_CTL) & ~1,
480 port->mmio + PORT_SCR_CTL);
481 readl(port->mmio + PORT_SCR_CTL);
482
483 /* Wait 500 ms for bit 0 of PORT_SCR_STS to be set */
484 timeout = jiffies + msecs_to_jiffies(500);
485 while (((readl(port->mmio + PORT_SCR_STAT) & 0x01) == 0)
486 && time_before(jiffies, timeout))
487 ;
488
489 if ((readl(port->mmio + PORT_SCR_STAT) & 0x01) == 0)
490 dev_warn(&port->dd->pdev->dev,
491 "COM reset failed\n");
492
493 /* Clear SError, the PxSERR.DIAG.x should be set so clear it */
494 writel(readl(port->mmio + PORT_SCR_ERR), port->mmio + PORT_SCR_ERR);
495
496 /* Enable the DMA engine */
497 mtip_enable_engine(port, 1);
498}
499
500/*
501 * Called periodically to see if any read/write commands are
502 * taking too long to complete.
503 *
504 * @data Pointer to the PORT data structure.
505 *
506 * return value
507 * None
508 */
509static void mtip_timeout_function(unsigned long int data)
510{
511 struct mtip_port *port = (struct mtip_port *) data;
512 struct host_to_dev_fis *fis;
513 struct mtip_cmd *command;
514 int tag, cmdto_cnt = 0;
515 unsigned int bit, group;
516 unsigned int num_command_slots = port->dd->slot_groups * 32;
517
518 if (unlikely(!port))
519 return;
520
521 if (atomic_read(&port->dd->resumeflag) == true) {
522 mod_timer(&port->cmd_timer,
523 jiffies + msecs_to_jiffies(30000));
524 return;
525 }
526
527 for (tag = 0; tag < num_command_slots; tag++) {
528 /*
529 * Skip internal command slot as it has
530 * its own timeout mechanism
531 */
532 if (tag == MTIP_TAG_INTERNAL)
533 continue;
534
535 if (atomic_read(&port->commands[tag].active) &&
536 (time_after(jiffies, port->commands[tag].comp_time))) {
537 group = tag >> 5;
538 bit = tag & 0x1F;
539
540 command = &port->commands[tag];
541 fis = (struct host_to_dev_fis *) command->command;
542
543 dev_warn(&port->dd->pdev->dev,
544 "Timeout for command tag %d\n", tag);
545
546 cmdto_cnt++;
547 if (cmdto_cnt == 1)
548 set_bit(MTIP_FLAG_EH_ACTIVE_BIT, &port->flags);
549
550 /*
551 * Clear the completed bit. This should prevent
552 * any interrupt handlers from trying to retire
553 * the command.
554 */
555 writel(1 << bit, port->completed[group]);
556
557 /* Call the async completion callback. */
558 if (likely(command->async_callback))
559 command->async_callback(command->async_data,
560 -EIO);
561 command->async_callback = NULL;
562 command->comp_func = NULL;
563
564 /* Unmap the DMA scatter list entries */
565 dma_unmap_sg(&port->dd->pdev->dev,
566 command->sg,
567 command->scatter_ents,
568 command->direction);
569
570 /*
571 * Clear the allocated bit and active tag for the
572 * command.
573 */
574 atomic_set(&port->commands[tag].active, 0);
575 release_slot(port, tag);
576
577 up(&port->cmd_slot);
578 }
579 }
580
581 if (cmdto_cnt) {
582 dev_warn(&port->dd->pdev->dev,
583 "%d commands timed out: restarting port",
584 cmdto_cnt);
585 mtip_restart_port(port);
586 clear_bit(MTIP_FLAG_EH_ACTIVE_BIT, &port->flags);
587 wake_up_interruptible(&port->svc_wait);
588 }
589
590 /* Restart the timer */
591 mod_timer(&port->cmd_timer,
592 jiffies + msecs_to_jiffies(MTIP_TIMEOUT_CHECK_PERIOD));
593}
594
595/*
596 * IO completion function.
597 *
598 * This completion function is called by the driver ISR when a
599 * command that was issued by the kernel completes. It first calls the
600 * asynchronous completion function which normally calls back into the block
601 * layer passing the asynchronous callback data, then unmaps the
602 * scatter list associated with the completed command, and finally
603 * clears the allocated bit associated with the completed command.
604 *
605 * @port Pointer to the port data structure.
606 * @tag Tag of the command.
607 * @data Pointer to driver_data.
608 * @status Completion status.
609 *
610 * return value
611 * None
612 */
613static void mtip_async_complete(struct mtip_port *port,
614 int tag,
615 void *data,
616 int status)
617{
618 struct mtip_cmd *command;
619 struct driver_data *dd = data;
620 int cb_status = status ? -EIO : 0;
621
622 if (unlikely(!dd) || unlikely(!port))
623 return;
624
625 command = &port->commands[tag];
626
627 if (unlikely(status == PORT_IRQ_TF_ERR)) {
628 dev_warn(&port->dd->pdev->dev,
629 "Command tag %d failed due to TFE\n", tag);
630 }
631
632 /* Upper layer callback */
633 if (likely(command->async_callback))
634 command->async_callback(command->async_data, cb_status);
635
636 command->async_callback = NULL;
637 command->comp_func = NULL;
638
639 /* Unmap the DMA scatter list entries */
640 dma_unmap_sg(&dd->pdev->dev,
641 command->sg,
642 command->scatter_ents,
643 command->direction);
644
645 /* Clear the allocated and active bits for the command */
646 atomic_set(&port->commands[tag].active, 0);
647 release_slot(port, tag);
648
649 up(&port->cmd_slot);
650}
651
652/*
653 * Internal command completion callback function.
654 *
655 * This function is normally called by the driver ISR when an internal
656 * command completed. This function signals the command completion by
657 * calling complete().
658 *
659 * @port Pointer to the port data structure.
660 * @tag Tag of the command that has completed.
661 * @data Pointer to a completion structure.
662 * @status Completion status.
663 *
664 * return value
665 * None
666 */
667static void mtip_completion(struct mtip_port *port,
668 int tag,
669 void *data,
670 int status)
671{
672 struct mtip_cmd *command = &port->commands[tag];
673 struct completion *waiting = data;
674 if (unlikely(status == PORT_IRQ_TF_ERR))
675 dev_warn(&port->dd->pdev->dev,
676 "Internal command %d completed with TFE\n", tag);
677
678 command->async_callback = NULL;
679 command->comp_func = NULL;
680
681 complete(waiting);
682}
683
684/*
685 * Helper function for tag logging
686 */
687static void print_tags(struct driver_data *dd,
688 char *msg,
689 unsigned long *tagbits)
690{
691 unsigned int tag, count = 0;
692
693 for (tag = 0; tag < (dd->slot_groups) * 32; tag++) {
694 if (test_bit(tag, tagbits))
695 count++;
696 }
697 if (count)
698 dev_info(&dd->pdev->dev, "%s [%i tags]\n", msg, count);
699}
700
701/*
702 * Handle an error.
703 *
704 * @dd Pointer to the DRIVER_DATA structure.
705 *
706 * return value
707 * None
708 */
709static void mtip_handle_tfe(struct driver_data *dd)
710{
711 int group, tag, bit, reissue;
712 struct mtip_port *port;
713 struct mtip_cmd *command;
714 u32 completed;
715 struct host_to_dev_fis *fis;
716 unsigned long tagaccum[SLOTBITS_IN_LONGS];
717
718 dev_warn(&dd->pdev->dev, "Taskfile error\n");
719
720 port = dd->port;
721
722 /* Stop the timer to prevent command timeouts. */
723 del_timer(&port->cmd_timer);
724
725 /* Set eh_active */
726 set_bit(MTIP_FLAG_EH_ACTIVE_BIT, &port->flags);
727
728 /* Loop through all the groups */
729 for (group = 0; group < dd->slot_groups; group++) {
730 completed = readl(port->completed[group]);
731
732 /* clear completed status register in the hardware.*/
733 writel(completed, port->completed[group]);
734
735 /* clear the tag accumulator */
736 memset(tagaccum, 0, SLOTBITS_IN_LONGS * sizeof(long));
737
738 /* Process successfully completed commands */
739 for (bit = 0; bit < 32 && completed; bit++) {
740 if (!(completed & (1<<bit)))
741 continue;
742 tag = (group << 5) + bit;
743
744 /* Skip the internal command slot */
745 if (tag == MTIP_TAG_INTERNAL)
746 continue;
747
748 command = &port->commands[tag];
749 if (likely(command->comp_func)) {
750 set_bit(tag, tagaccum);
751 atomic_set(&port->commands[tag].active, 0);
752 command->comp_func(port,
753 tag,
754 command->comp_data,
755 0);
756 } else {
757 dev_err(&port->dd->pdev->dev,
758 "Missing completion func for tag %d",
759 tag);
760 if (mtip_check_surprise_removal(dd->pdev)) {
761 mtip_command_cleanup(dd);
762 /* don't proceed further */
763 return;
764 }
765 }
766 }
767 }
768 print_tags(dd, "TFE tags completed:", tagaccum);
769
770 /* Restart the port */
771 mdelay(20);
772 mtip_restart_port(port);
773
774 /* clear the tag accumulator */
775 memset(tagaccum, 0, SLOTBITS_IN_LONGS * sizeof(long));
776
777 /* Loop through all the groups */
778 for (group = 0; group < dd->slot_groups; group++) {
779 for (bit = 0; bit < 32; bit++) {
780 reissue = 1;
781 tag = (group << 5) + bit;
782
783 /* If the active bit is set re-issue the command */
784 if (atomic_read(&port->commands[tag].active) == 0)
785 continue;
786
787 fis = (struct host_to_dev_fis *)
788 port->commands[tag].command;
789
790 /* Should re-issue? */
791 if (tag == MTIP_TAG_INTERNAL ||
792 fis->command == ATA_CMD_SET_FEATURES)
793 reissue = 0;
794
795 /*
796 * First check if this command has
797 * exceeded its retries.
798 */
799 if (reissue &&
800 (port->commands[tag].retries-- > 0)) {
801
802 set_bit(tag, tagaccum);
803
804 /* Update the timeout value. */
805 port->commands[tag].comp_time =
806 jiffies + msecs_to_jiffies(
807 MTIP_NCQ_COMMAND_TIMEOUT_MS);
808 /* Re-issue the command. */
809 mtip_issue_ncq_command(port, tag);
810
811 continue;
812 }
813
814 /* Retire a command that will not be reissued */
815 dev_warn(&port->dd->pdev->dev,
816 "retiring tag %d\n", tag);
817 atomic_set(&port->commands[tag].active, 0);
818
819 if (port->commands[tag].comp_func)
820 port->commands[tag].comp_func(
821 port,
822 tag,
823 port->commands[tag].comp_data,
824 PORT_IRQ_TF_ERR);
825 else
826 dev_warn(&port->dd->pdev->dev,
827 "Bad completion for tag %d\n",
828 tag);
829 }
830 }
831 print_tags(dd, "TFE tags reissued:", tagaccum);
832
833 /* clear eh_active */
834 clear_bit(MTIP_FLAG_EH_ACTIVE_BIT, &port->flags);
835 wake_up_interruptible(&port->svc_wait);
836
837 mod_timer(&port->cmd_timer,
838 jiffies + msecs_to_jiffies(MTIP_TIMEOUT_CHECK_PERIOD));
839}
840
841/*
842 * Handle a set device bits interrupt
843 */
844static inline void mtip_process_sdbf(struct driver_data *dd)
845{
846 struct mtip_port *port = dd->port;
847 int group, tag, bit;
848 u32 completed;
849 struct mtip_cmd *command;
850
851 /* walk all bits in all slot groups */
852 for (group = 0; group < dd->slot_groups; group++) {
853 completed = readl(port->completed[group]);
854
855 /* clear completed status register in the hardware.*/
856 writel(completed, port->completed[group]);
857
858 /* Process completed commands. */
859 for (bit = 0;
860 (bit < 32) && completed;
861 bit++, completed >>= 1) {
862 if (completed & 0x01) {
863 tag = (group << 5) | bit;
864
865 /* skip internal command slot. */
866 if (unlikely(tag == MTIP_TAG_INTERNAL))
867 continue;
868
869 command = &port->commands[tag];
870 /* make internal callback */
871 if (likely(command->comp_func)) {
872 command->comp_func(
873 port,
874 tag,
875 command->comp_data,
876 0);
877 } else {
878 dev_warn(&dd->pdev->dev,
879 "Null completion "
880 "for tag %d",
881 tag);
882
883 if (mtip_check_surprise_removal(
884 dd->pdev)) {
885 mtip_command_cleanup(dd);
886 return;
887 }
888 }
889 }
890 }
891 }
892}
893
894/*
895 * Process legacy pio and d2h interrupts
896 */
897static inline void mtip_process_legacy(struct driver_data *dd, u32 port_stat)
898{
899 struct mtip_port *port = dd->port;
900 struct mtip_cmd *cmd = &port->commands[MTIP_TAG_INTERNAL];
901
902 if (test_bit(MTIP_FLAG_IC_ACTIVE_BIT, &port->flags) &&
903 (cmd != NULL) && !(readl(port->cmd_issue[MTIP_TAG_INTERNAL])
904 & (1 << MTIP_TAG_INTERNAL))) {
905 if (cmd->comp_func) {
906 cmd->comp_func(port,
907 MTIP_TAG_INTERNAL,
908 cmd->comp_data,
909 0);
910 return;
911 }
912 }
913
914 dev_warn(&dd->pdev->dev, "IRQ status 0x%x ignored.\n", port_stat);
915
916 return;
917}
918
919/*
920 * Demux and handle errors
921 */
922static inline void mtip_process_errors(struct driver_data *dd, u32 port_stat)
923{
924 if (likely(port_stat & (PORT_IRQ_TF_ERR | PORT_IRQ_IF_ERR)))
925 mtip_handle_tfe(dd);
926
927 if (unlikely(port_stat & PORT_IRQ_CONNECT)) {
928 dev_warn(&dd->pdev->dev,
929 "Clearing PxSERR.DIAG.x\n");
930 writel((1 << 26), dd->port->mmio + PORT_SCR_ERR);
931 }
932
933 if (unlikely(port_stat & PORT_IRQ_PHYRDY)) {
934 dev_warn(&dd->pdev->dev,
935 "Clearing PxSERR.DIAG.n\n");
936 writel((1 << 16), dd->port->mmio + PORT_SCR_ERR);
937 }
938
939 if (unlikely(port_stat & ~PORT_IRQ_HANDLED)) {
940 dev_warn(&dd->pdev->dev,
941 "Port stat errors %x unhandled\n",
942 (port_stat & ~PORT_IRQ_HANDLED));
943 }
944}
945
946static inline irqreturn_t mtip_handle_irq(struct driver_data *data)
947{
948 struct driver_data *dd = (struct driver_data *) data;
949 struct mtip_port *port = dd->port;
950 u32 hba_stat, port_stat;
951 int rv = IRQ_NONE;
952
953 hba_stat = readl(dd->mmio + HOST_IRQ_STAT);
954 if (hba_stat) {
955 rv = IRQ_HANDLED;
956
957 /* Acknowledge the interrupt status on the port.*/
958 port_stat = readl(port->mmio + PORT_IRQ_STAT);
959 writel(port_stat, port->mmio + PORT_IRQ_STAT);
960
961 /* Demux port status */
962 if (likely(port_stat & PORT_IRQ_SDB_FIS))
963 mtip_process_sdbf(dd);
964
965 if (unlikely(port_stat & PORT_IRQ_ERR)) {
966 if (unlikely(mtip_check_surprise_removal(dd->pdev))) {
967 mtip_command_cleanup(dd);
968 /* don't proceed further */
969 return IRQ_HANDLED;
970 }
971
972 mtip_process_errors(dd, port_stat & PORT_IRQ_ERR);
973 }
974
975 if (unlikely(port_stat & PORT_IRQ_LEGACY))
976 mtip_process_legacy(dd, port_stat & PORT_IRQ_LEGACY);
977 }
978
979 /* acknowledge interrupt */
980 writel(hba_stat, dd->mmio + HOST_IRQ_STAT);
981
982 return rv;
983}
984
985/*
986 * Wrapper for mtip_handle_irq
987 * (ignores return code)
988 */
989static void mtip_tasklet(unsigned long data)
990{
991 mtip_handle_irq((struct driver_data *) data);
992}
993
994/*
995 * HBA interrupt subroutine.
996 *
997 * @irq IRQ number.
998 * @instance Pointer to the driver data structure.
999 *
1000 * return value
1001 * IRQ_HANDLED A HBA interrupt was pending and handled.
1002 * IRQ_NONE This interrupt was not for the HBA.
1003 */
1004static irqreturn_t mtip_irq_handler(int irq, void *instance)
1005{
1006 struct driver_data *dd = instance;
1007 tasklet_schedule(&dd->tasklet);
1008 return IRQ_HANDLED;
1009}
1010
1011static void mtip_issue_non_ncq_command(struct mtip_port *port, int tag)
1012{
1013 atomic_set(&port->commands[tag].active, 1);
1014 writel(1 << MTIP_TAG_BIT(tag),
1015 port->cmd_issue[MTIP_TAG_INDEX(tag)]);
1016}
1017
1018/*
1019 * Wait for port to quiesce
1020 *
1021 * @port Pointer to port data structure
1022 * @timeout Max duration to wait (ms)
1023 *
1024 * return value
1025 * 0 Success
1026 * -EBUSY Commands still active
1027 */
1028static int mtip_quiesce_io(struct mtip_port *port, unsigned long timeout)
1029{
1030 unsigned long to;
1031 unsigned int n;
1032 unsigned int active = 1;
1033
1034 to = jiffies + msecs_to_jiffies(timeout);
1035 do {
1036 if (test_bit(MTIP_FLAG_SVC_THD_ACTIVE_BIT, &port->flags) &&
1037 test_bit(MTIP_FLAG_ISSUE_CMDS_BIT, &port->flags)) {
1038 msleep(20);
1039 continue; /* svc thd is actively issuing commands */
1040 }
1041 /*
1042 * Ignore s_active bit 0 of array element 0.
1043 * This bit will always be set
1044 */
1045 active = readl(port->s_active[0]) & 0xFFFFFFFE;
1046 for (n = 1; n < port->dd->slot_groups; n++)
1047 active |= readl(port->s_active[n]);
1048
1049 if (!active)
1050 break;
1051
1052 msleep(20);
1053 } while (time_before(jiffies, to));
1054
1055 return active ? -EBUSY : 0;
1056}
1057
1058/*
1059 * Execute an internal command and wait for the completion.
1060 *
1061 * @port Pointer to the port data structure.
1062 * @fis Pointer to the FIS that describes the command.
1063 * @fis_len Length in WORDS of the FIS.
1064 * @buffer DMA accessible for command data.
1065 * @buf_len Length, in bytes, of the data buffer.
1066 * @opts Command header options, excluding the FIS length
1067 * and the number of PRD entries.
1068 * @timeout Time in ms to wait for the command to complete.
1069 *
1070 * return value
1071 * 0 Command completed successfully.
1072 * -EFAULT The buffer address is not correctly aligned.
1073 * -EBUSY Internal command or other IO in progress.
1074 * -EAGAIN Time out waiting for command to complete.
1075 */
1076static int mtip_exec_internal_command(struct mtip_port *port,
1077 void *fis,
1078 int fis_len,
1079 dma_addr_t buffer,
1080 int buf_len,
1081 u32 opts,
1082 gfp_t atomic,
1083 unsigned long timeout)
1084{
1085 struct mtip_cmd_sg *command_sg;
1086 DECLARE_COMPLETION_ONSTACK(wait);
1087 int rv = 0;
1088 struct mtip_cmd *int_cmd = &port->commands[MTIP_TAG_INTERNAL];
1089
1090 /* Make sure the buffer is 8 byte aligned. This is asic specific. */
1091 if (buffer & 0x00000007) {
1092 dev_err(&port->dd->pdev->dev,
1093 "SG buffer is not 8 byte aligned\n");
1094 return -EFAULT;
1095 }
1096
1097 /* Only one internal command should be running at a time */
1098 if (test_and_set_bit(MTIP_TAG_INTERNAL, port->allocated)) {
1099 dev_warn(&port->dd->pdev->dev,
1100 "Internal command already active\n");
1101 return -EBUSY;
1102 }
1103 set_bit(MTIP_FLAG_IC_ACTIVE_BIT, &port->flags);
1104
1105 if (atomic == GFP_KERNEL) {
1106 /* wait for io to complete if non atomic */
1107 if (mtip_quiesce_io(port, 5000) < 0) {
1108 dev_warn(&port->dd->pdev->dev,
1109 "Failed to quiesce IO\n");
1110 release_slot(port, MTIP_TAG_INTERNAL);
1111 clear_bit(MTIP_FLAG_IC_ACTIVE_BIT, &port->flags);
1112 wake_up_interruptible(&port->svc_wait);
1113 return -EBUSY;
1114 }
1115
1116 /* Set the completion function and data for the command. */
1117 int_cmd->comp_data = &wait;
1118 int_cmd->comp_func = mtip_completion;
1119
1120 } else {
1121 /* Clear completion - we're going to poll */
1122 int_cmd->comp_data = NULL;
1123 int_cmd->comp_func = NULL;
1124 }
1125
1126 /* Copy the command to the command table */
1127 memcpy(int_cmd->command, fis, fis_len*4);
1128
1129 /* Populate the SG list */
1130 int_cmd->command_header->opts =
1131 __force_bit2int cpu_to_le32(opts | fis_len);
1132 if (buf_len) {
1133 command_sg = int_cmd->command + AHCI_CMD_TBL_HDR_SZ;
1134
1135 command_sg->info =
1136 __force_bit2int cpu_to_le32((buf_len-1) & 0x3FFFFF);
1137 command_sg->dba =
1138 __force_bit2int cpu_to_le32(buffer & 0xFFFFFFFF);
1139 command_sg->dba_upper =
1140 __force_bit2int cpu_to_le32((buffer >> 16) >> 16);
1141
1142 int_cmd->command_header->opts |=
1143 __force_bit2int cpu_to_le32((1 << 16));
1144 }
1145
1146 /* Populate the command header */
1147 int_cmd->command_header->byte_count = 0;
1148
1149 /* Issue the command to the hardware */
1150 mtip_issue_non_ncq_command(port, MTIP_TAG_INTERNAL);
1151
1152 /* Poll if atomic, wait_for_completion otherwise */
1153 if (atomic == GFP_KERNEL) {
1154 /* Wait for the command to complete or timeout. */
1155 if (wait_for_completion_timeout(
1156 &wait,
1157 msecs_to_jiffies(timeout)) == 0) {
1158 dev_err(&port->dd->pdev->dev,
1159 "Internal command did not complete [%d] "
1160 "within timeout of %lu ms\n",
1161 atomic, timeout);
1162 rv = -EAGAIN;
1163 }
1164
1165 if (readl(port->cmd_issue[MTIP_TAG_INTERNAL])
1166 & (1 << MTIP_TAG_INTERNAL)) {
1167 dev_warn(&port->dd->pdev->dev,
1168 "Retiring internal command but CI is 1.\n");
1169 }
1170
1171 } else {
1172 /* Spin for <timeout> checking if command still outstanding */
1173 timeout = jiffies + msecs_to_jiffies(timeout);
1174
1175 while ((readl(
1176 port->cmd_issue[MTIP_TAG_INTERNAL])
1177 & (1 << MTIP_TAG_INTERNAL))
1178 && time_before(jiffies, timeout))
1179 ;
1180
1181 if (readl(port->cmd_issue[MTIP_TAG_INTERNAL])
1182 & (1 << MTIP_TAG_INTERNAL)) {
1183 dev_err(&port->dd->pdev->dev,
1184 "Internal command did not complete [%d]\n",
1185 atomic);
1186 rv = -EAGAIN;
1187 }
1188 }
1189
1190 /* Clear the allocated and active bits for the internal command. */
1191 atomic_set(&int_cmd->active, 0);
1192 release_slot(port, MTIP_TAG_INTERNAL);
1193 clear_bit(MTIP_FLAG_IC_ACTIVE_BIT, &port->flags);
1194 wake_up_interruptible(&port->svc_wait);
1195
1196 return rv;
1197}
1198
1199/*
1200 * Byte-swap ATA ID strings.
1201 *
1202 * ATA identify data contains strings in byte-swapped 16-bit words.
1203 * They must be swapped (on all architectures) to be usable as C strings.
1204 * This function swaps bytes in-place.
1205 *
1206 * @buf The buffer location of the string
1207 * @len The number of bytes to swap
1208 *
1209 * return value
1210 * None
1211 */
1212static inline void ata_swap_string(u16 *buf, unsigned int len)
1213{
1214 int i;
1215 for (i = 0; i < (len/2); i++)
1216 be16_to_cpus(&buf[i]);
1217}
1218
1219/*
1220 * Request the device identity information.
1221 *
1222 * If a user space buffer is not specified, i.e. is NULL, the
1223 * identify information is still read from the drive and placed
1224 * into the identify data buffer (@e port->identify) in the
1225 * port data structure.
1226 * When the identify buffer contains valid identify information @e
1227 * port->identify_valid is non-zero.
1228 *
1229 * @port Pointer to the port structure.
1230 * @user_buffer A user space buffer where the identify data should be
1231 * copied.
1232 *
1233 * return value
1234 * 0 Command completed successfully.
1235 * -EFAULT An error occurred while coping data to the user buffer.
1236 * -1 Command failed.
1237 */
1238static int mtip_get_identify(struct mtip_port *port, void __user *user_buffer)
1239{
1240 int rv = 0;
1241 struct host_to_dev_fis fis;
1242
1243 /* Build the FIS. */
1244 memset(&fis, 0, sizeof(struct host_to_dev_fis));
1245 fis.type = 0x27;
1246 fis.opts = 1 << 7;
1247 fis.command = ATA_CMD_ID_ATA;
1248
1249 /* Set the identify information as invalid. */
1250 port->identify_valid = 0;
1251
1252 /* Clear the identify information. */
1253 memset(port->identify, 0, sizeof(u16) * ATA_ID_WORDS);
1254
1255 /* Execute the command. */
1256 if (mtip_exec_internal_command(port,
1257 &fis,
1258 5,
1259 port->identify_dma,
1260 sizeof(u16) * ATA_ID_WORDS,
1261 0,
1262 GFP_KERNEL,
1263 MTIP_INTERNAL_COMMAND_TIMEOUT_MS)
1264 < 0) {
1265 rv = -1;
1266 goto out;
1267 }
1268
1269 /*
1270 * Perform any necessary byte-swapping. Yes, the kernel does in fact
1271 * perform field-sensitive swapping on the string fields.
1272 * See the kernel use of ata_id_string() for proof of this.
1273 */
1274#ifdef __LITTLE_ENDIAN
1275 ata_swap_string(port->identify + 27, 40); /* model string*/
1276 ata_swap_string(port->identify + 23, 8); /* firmware string*/
1277 ata_swap_string(port->identify + 10, 20); /* serial# string*/
1278#else
1279 {
1280 int i;
1281 for (i = 0; i < ATA_ID_WORDS; i++)
1282 port->identify[i] = le16_to_cpu(port->identify[i]);
1283 }
1284#endif
1285
1286 /* Set the identify buffer as valid. */
1287 port->identify_valid = 1;
1288
1289 if (user_buffer) {
1290 if (copy_to_user(
1291 user_buffer,
1292 port->identify,
1293 ATA_ID_WORDS * sizeof(u16))) {
1294 rv = -EFAULT;
1295 goto out;
1296 }
1297 }
1298
1299out:
1300 return rv;
1301}
1302
1303/*
1304 * Issue a standby immediate command to the device.
1305 *
1306 * @port Pointer to the port structure.
1307 *
1308 * return value
1309 * 0 Command was executed successfully.
1310 * -1 An error occurred while executing the command.
1311 */
1312static int mtip_standby_immediate(struct mtip_port *port)
1313{
1314 int rv;
1315 struct host_to_dev_fis fis;
1316
1317 /* Build the FIS. */
1318 memset(&fis, 0, sizeof(struct host_to_dev_fis));
1319 fis.type = 0x27;
1320 fis.opts = 1 << 7;
1321 fis.command = ATA_CMD_STANDBYNOW1;
1322
1323 /* Execute the command. Use a 15-second timeout for large drives. */
1324 rv = mtip_exec_internal_command(port,
1325 &fis,
1326 5,
1327 0,
1328 0,
1329 0,
1330 GFP_KERNEL,
1331 15000);
1332
1333 return rv;
1334}
1335
1336/*
1337 * Get the drive capacity.
1338 *
1339 * @dd Pointer to the device data structure.
1340 * @sectors Pointer to the variable that will receive the sector count.
1341 *
1342 * return value
1343 * 1 Capacity was returned successfully.
1344 * 0 The identify information is invalid.
1345 */
1346static bool mtip_hw_get_capacity(struct driver_data *dd, sector_t *sectors)
1347{
1348 struct mtip_port *port = dd->port;
1349 u64 total, raw0, raw1, raw2, raw3;
1350 raw0 = port->identify[100];
1351 raw1 = port->identify[101];
1352 raw2 = port->identify[102];
1353 raw3 = port->identify[103];
1354 total = raw0 | raw1<<16 | raw2<<32 | raw3<<48;
1355 *sectors = total;
1356 return (bool) !!port->identify_valid;
1357}
1358
1359/*
1360 * Reset the HBA.
1361 *
1362 * Resets the HBA by setting the HBA Reset bit in the Global
1363 * HBA Control register. After setting the HBA Reset bit the
1364 * function waits for 1 second before reading the HBA Reset
1365 * bit to make sure it has cleared. If HBA Reset is not clear
1366 * an error is returned. Cannot be used in non-blockable
1367 * context.
1368 *
1369 * @dd Pointer to the driver data structure.
1370 *
1371 * return value
1372 * 0 The reset was successful.
1373 * -1 The HBA Reset bit did not clear.
1374 */
1375static int mtip_hba_reset(struct driver_data *dd)
1376{
1377 mtip_deinit_port(dd->port);
1378
1379 /* Set the reset bit */
1380 writel(HOST_RESET, dd->mmio + HOST_CTL);
1381
1382 /* Flush */
1383 readl(dd->mmio + HOST_CTL);
1384
1385 /* Wait for reset to clear */
1386 ssleep(1);
1387
1388 /* Check the bit has cleared */
1389 if (readl(dd->mmio + HOST_CTL) & HOST_RESET) {
1390 dev_err(&dd->pdev->dev,
1391 "Reset bit did not clear.\n");
1392 return -1;
1393 }
1394
1395 return 0;
1396}
1397
1398/*
1399 * Display the identify command data.
1400 *
1401 * @port Pointer to the port data structure.
1402 *
1403 * return value
1404 * None
1405 */
1406static void mtip_dump_identify(struct mtip_port *port)
1407{
1408 sector_t sectors;
1409 unsigned short revid;
1410 char cbuf[42];
1411
1412 if (!port->identify_valid)
1413 return;
1414
1415 strlcpy(cbuf, (char *)(port->identify+10), 21);
1416 dev_info(&port->dd->pdev->dev,
1417 "Serial No.: %s\n", cbuf);
1418
1419 strlcpy(cbuf, (char *)(port->identify+23), 9);
1420 dev_info(&port->dd->pdev->dev,
1421 "Firmware Ver.: %s\n", cbuf);
1422
1423 strlcpy(cbuf, (char *)(port->identify+27), 41);
1424 dev_info(&port->dd->pdev->dev, "Model: %s\n", cbuf);
1425
1426 if (mtip_hw_get_capacity(port->dd, &sectors))
1427 dev_info(&port->dd->pdev->dev,
1428 "Capacity: %llu sectors (%llu MB)\n",
1429 (u64)sectors,
1430 ((u64)sectors) * ATA_SECT_SIZE >> 20);
1431
1432 pci_read_config_word(port->dd->pdev, PCI_REVISION_ID, &revid);
1433 switch (revid & 0xFF) {
1434 case 0x1:
1435 strlcpy(cbuf, "A0", 3);
1436 break;
1437 case 0x3:
1438 strlcpy(cbuf, "A2", 3);
1439 break;
1440 default:
1441 strlcpy(cbuf, "?", 2);
1442 break;
1443 }
1444 dev_info(&port->dd->pdev->dev,
1445 "Card Type: %s\n", cbuf);
1446}
1447
1448/*
1449 * Map the commands scatter list into the command table.
1450 *
1451 * @command Pointer to the command.
1452 * @nents Number of scatter list entries.
1453 *
1454 * return value
1455 * None
1456 */
1457static inline void fill_command_sg(struct driver_data *dd,
1458 struct mtip_cmd *command,
1459 int nents)
1460{
1461 int n;
1462 unsigned int dma_len;
1463 struct mtip_cmd_sg *command_sg;
1464 struct scatterlist *sg = command->sg;
1465
1466 command_sg = command->command + AHCI_CMD_TBL_HDR_SZ;
1467
1468 for (n = 0; n < nents; n++) {
1469 dma_len = sg_dma_len(sg);
1470 if (dma_len > 0x400000)
1471 dev_err(&dd->pdev->dev,
1472 "DMA segment length truncated\n");
1473 command_sg->info = __force_bit2int
1474 cpu_to_le32((dma_len-1) & 0x3FFFFF);
1475 command_sg->dba = __force_bit2int
1476 cpu_to_le32(sg_dma_address(sg));
1477 command_sg->dba_upper = __force_bit2int
1478 cpu_to_le32((sg_dma_address(sg) >> 16) >> 16);
1479 command_sg++;
1480 sg++;
1481 }
1482}
1483
1484/*
1485 * @brief Execute a drive command.
1486 *
1487 * return value 0 The command completed successfully.
1488 * return value -1 An error occurred while executing the command.
1489 */
1490static int exec_drive_task(struct mtip_port *port, u8 *command)
1491{
1492 struct host_to_dev_fis fis;
1493 struct host_to_dev_fis *reply = (port->rxfis + RX_FIS_D2H_REG);
1494
1495 /* Build the FIS. */
1496 memset(&fis, 0, sizeof(struct host_to_dev_fis));
1497 fis.type = 0x27;
1498 fis.opts = 1 << 7;
1499 fis.command = command[0];
1500 fis.features = command[1];
1501 fis.sect_count = command[2];
1502 fis.sector = command[3];
1503 fis.cyl_low = command[4];
1504 fis.cyl_hi = command[5];
1505 fis.device = command[6] & ~0x10; /* Clear the dev bit*/
1506
1507
1508 dbg_printk(MTIP_DRV_NAME "%s: User Command: cmd %x, feat %x, "
1509 "nsect %x, sect %x, lcyl %x, "
1510 "hcyl %x, sel %x\n",
1511 __func__,
1512 command[0],
1513 command[1],
1514 command[2],
1515 command[3],
1516 command[4],
1517 command[5],
1518 command[6]);
1519
1520 /* Execute the command. */
1521 if (mtip_exec_internal_command(port,
1522 &fis,
1523 5,
1524 0,
1525 0,
1526 0,
1527 GFP_KERNEL,
1528 MTIP_IOCTL_COMMAND_TIMEOUT_MS) < 0) {
1529 return -1;
1530 }
1531
1532 command[0] = reply->command; /* Status*/
1533 command[1] = reply->features; /* Error*/
1534 command[4] = reply->cyl_low;
1535 command[5] = reply->cyl_hi;
1536
1537 dbg_printk(MTIP_DRV_NAME "%s: Completion Status: stat %x, "
1538 "err %x , cyl_lo %x cyl_hi %x\n",
1539 __func__,
1540 command[0],
1541 command[1],
1542 command[4],
1543 command[5]);
1544
1545 return 0;
1546}
1547
1548/*
1549 * @brief Execute a drive command.
1550 *
1551 * @param port Pointer to the port data structure.
1552 * @param command Pointer to the user specified command parameters.
1553 * @param user_buffer Pointer to the user space buffer where read sector
1554 * data should be copied.
1555 *
1556 * return value 0 The command completed successfully.
1557 * return value -EFAULT An error occurred while copying the completion
1558 * data to the user space buffer.
1559 * return value -1 An error occurred while executing the command.
1560 */
1561static int exec_drive_command(struct mtip_port *port, u8 *command,
1562 void __user *user_buffer)
1563{
1564 struct host_to_dev_fis fis;
1565 struct host_to_dev_fis *reply = (port->rxfis + RX_FIS_D2H_REG);
1566
1567 /* Build the FIS. */
1568 memset(&fis, 0, sizeof(struct host_to_dev_fis));
1569 fis.type = 0x27;
1570 fis.opts = 1 << 7;
1571 fis.command = command[0];
1572 fis.features = command[2];
1573 fis.sect_count = command[3];
1574 if (fis.command == ATA_CMD_SMART) {
1575 fis.sector = command[1];
1576 fis.cyl_low = 0x4F;
1577 fis.cyl_hi = 0xC2;
1578 }
1579
1580 dbg_printk(MTIP_DRV_NAME
1581 "%s: User Command: cmd %x, sect %x, "
1582 "feat %x, sectcnt %x\n",
1583 __func__,
1584 command[0],
1585 command[1],
1586 command[2],
1587 command[3]);
1588
1589 memset(port->sector_buffer, 0x00, ATA_SECT_SIZE);
1590
1591 /* Execute the command. */
1592 if (mtip_exec_internal_command(port,
1593 &fis,
1594 5,
1595 port->sector_buffer_dma,
1596 (command[3] != 0) ? ATA_SECT_SIZE : 0,
1597 0,
1598 GFP_KERNEL,
1599 MTIP_IOCTL_COMMAND_TIMEOUT_MS)
1600 < 0) {
1601 return -1;
1602 }
1603
1604 /* Collect the completion status. */
1605 command[0] = reply->command; /* Status*/
1606 command[1] = reply->features; /* Error*/
1607 command[2] = command[3];
1608
1609 dbg_printk(MTIP_DRV_NAME
1610 "%s: Completion Status: stat %x, "
1611 "err %x, cmd %x\n",
1612 __func__,
1613 command[0],
1614 command[1],
1615 command[2]);
1616
1617 if (user_buffer && command[3]) {
1618 if (copy_to_user(user_buffer,
1619 port->sector_buffer,
1620 ATA_SECT_SIZE * command[3])) {
1621 return -EFAULT;
1622 }
1623 }
1624
1625 return 0;
1626}
1627
1628/*
1629 * Indicates whether a command has a single sector payload.
1630 *
1631 * @command passed to the device to perform the certain event.
1632 * @features passed to the device to perform the certain event.
1633 *
1634 * return value
1635 * 1 command is one that always has a single sector payload,
1636 * regardless of the value in the Sector Count field.
1637 * 0 otherwise
1638 *
1639 */
1640static unsigned int implicit_sector(unsigned char command,
1641 unsigned char features)
1642{
1643 unsigned int rv = 0;
1644
1645 /* list of commands that have an implicit sector count of 1 */
1646 switch (command) {
1647 case ATA_CMD_SEC_SET_PASS:
1648 case ATA_CMD_SEC_UNLOCK:
1649 case ATA_CMD_SEC_ERASE_PREP:
1650 case ATA_CMD_SEC_ERASE_UNIT:
1651 case ATA_CMD_SEC_FREEZE_LOCK:
1652 case ATA_CMD_SEC_DISABLE_PASS:
1653 case ATA_CMD_PMP_READ:
1654 case ATA_CMD_PMP_WRITE:
1655 rv = 1;
1656 break;
1657 case ATA_CMD_SET_MAX:
1658 if (features == ATA_SET_MAX_UNLOCK)
1659 rv = 1;
1660 break;
1661 case ATA_CMD_SMART:
1662 if ((features == ATA_SMART_READ_VALUES) ||
1663 (features == ATA_SMART_READ_THRESHOLDS))
1664 rv = 1;
1665 break;
1666 case ATA_CMD_CONF_OVERLAY:
1667 if ((features == ATA_DCO_IDENTIFY) ||
1668 (features == ATA_DCO_SET))
1669 rv = 1;
1670 break;
1671 }
1672 return rv;
1673}
1674
1675/*
1676 * Executes a taskfile
1677 * See ide_taskfile_ioctl() for derivation
1678 */
1679static int exec_drive_taskfile(struct driver_data *dd,
1680 void __user *buf,
1681 ide_task_request_t *req_task,
1682 int outtotal)
1683{
1684 struct host_to_dev_fis fis;
1685 struct host_to_dev_fis *reply;
1686 u8 *outbuf = NULL;
1687 u8 *inbuf = NULL;
1688 dma_addr_t outbuf_dma = 0;
1689 dma_addr_t inbuf_dma = 0;
1690 dma_addr_t dma_buffer = 0;
1691 int err = 0;
1692 unsigned int taskin = 0;
1693 unsigned int taskout = 0;
1694 u8 nsect = 0;
1695 unsigned int timeout = MTIP_IOCTL_COMMAND_TIMEOUT_MS;
1696 unsigned int force_single_sector;
1697 unsigned int transfer_size;
1698 unsigned long task_file_data;
1699 int intotal = outtotal + req_task->out_size;
1700
1701 taskout = req_task->out_size;
1702 taskin = req_task->in_size;
1703 /* 130560 = 512 * 0xFF*/
1704 if (taskin > 130560 || taskout > 130560) {
1705 err = -EINVAL;
1706 goto abort;
1707 }
1708
1709 if (taskout) {
1710 outbuf = kzalloc(taskout, GFP_KERNEL);
1711 if (outbuf == NULL) {
1712 err = -ENOMEM;
1713 goto abort;
1714 }
1715 if (copy_from_user(outbuf, buf + outtotal, taskout)) {
1716 err = -EFAULT;
1717 goto abort;
1718 }
1719 outbuf_dma = pci_map_single(dd->pdev,
1720 outbuf,
1721 taskout,
1722 DMA_TO_DEVICE);
1723 if (outbuf_dma == 0) {
1724 err = -ENOMEM;
1725 goto abort;
1726 }
1727 dma_buffer = outbuf_dma;
1728 }
1729
1730 if (taskin) {
1731 inbuf = kzalloc(taskin, GFP_KERNEL);
1732 if (inbuf == NULL) {
1733 err = -ENOMEM;
1734 goto abort;
1735 }
1736
1737 if (copy_from_user(inbuf, buf + intotal, taskin)) {
1738 err = -EFAULT;
1739 goto abort;
1740 }
1741 inbuf_dma = pci_map_single(dd->pdev,
1742 inbuf,
1743 taskin, DMA_FROM_DEVICE);
1744 if (inbuf_dma == 0) {
1745 err = -ENOMEM;
1746 goto abort;
1747 }
1748 dma_buffer = inbuf_dma;
1749 }
1750
1751 /* only supports PIO and non-data commands from this ioctl. */
1752 switch (req_task->data_phase) {
1753 case TASKFILE_OUT:
1754 nsect = taskout / ATA_SECT_SIZE;
1755 reply = (dd->port->rxfis + RX_FIS_PIO_SETUP);
1756 break;
1757 case TASKFILE_IN:
1758 reply = (dd->port->rxfis + RX_FIS_PIO_SETUP);
1759 break;
1760 case TASKFILE_NO_DATA:
1761 reply = (dd->port->rxfis + RX_FIS_D2H_REG);
1762 break;
1763 default:
1764 err = -EINVAL;
1765 goto abort;
1766 }
1767
1768 /* Build the FIS. */
1769 memset(&fis, 0, sizeof(struct host_to_dev_fis));
1770
1771 fis.type = 0x27;
1772 fis.opts = 1 << 7;
1773 fis.command = req_task->io_ports[7];
1774 fis.features = req_task->io_ports[1];
1775 fis.sect_count = req_task->io_ports[2];
1776 fis.lba_low = req_task->io_ports[3];
1777 fis.lba_mid = req_task->io_ports[4];
1778 fis.lba_hi = req_task->io_ports[5];
1779 /* Clear the dev bit*/
1780 fis.device = req_task->io_ports[6] & ~0x10;
1781
1782 if ((req_task->in_flags.all == 0) && (req_task->out_flags.all & 1)) {
1783 req_task->in_flags.all =
1784 IDE_TASKFILE_STD_IN_FLAGS |
1785 (IDE_HOB_STD_IN_FLAGS << 8);
1786 fis.lba_low_ex = req_task->hob_ports[3];
1787 fis.lba_mid_ex = req_task->hob_ports[4];
1788 fis.lba_hi_ex = req_task->hob_ports[5];
1789 fis.features_ex = req_task->hob_ports[1];
1790 fis.sect_cnt_ex = req_task->hob_ports[2];
1791
1792 } else {
1793 req_task->in_flags.all = IDE_TASKFILE_STD_IN_FLAGS;
1794 }
1795
1796 force_single_sector = implicit_sector(fis.command, fis.features);
1797
1798 if ((taskin || taskout) && (!fis.sect_count)) {
1799 if (nsect)
1800 fis.sect_count = nsect;
1801 else {
1802 if (!force_single_sector) {
1803 dev_warn(&dd->pdev->dev,
1804 "data movement but "
1805 "sect_count is 0\n");
1806 err = -EINVAL;
1807 goto abort;
1808 }
1809 }
1810 }
1811
1812 dbg_printk(MTIP_DRV_NAME
1813 "taskfile: cmd %x, feat %x, nsect %x,"
1814 " sect/lbal %x, lcyl/lbam %x, hcyl/lbah %x,"
1815 " head/dev %x\n",
1816 fis.command,
1817 fis.features,
1818 fis.sect_count,
1819 fis.lba_low,
1820 fis.lba_mid,
1821 fis.lba_hi,
1822 fis.device);
1823
1824 switch (fis.command) {
1825 case ATA_CMD_DOWNLOAD_MICRO:
1826 /* Change timeout for Download Microcode to 60 seconds.*/
1827 timeout = 60000;
1828 break;
1829 case ATA_CMD_SEC_ERASE_UNIT:
1830 /* Change timeout for Security Erase Unit to 4 minutes.*/
1831 timeout = 240000;
1832 break;
1833 case ATA_CMD_STANDBYNOW1:
1834 /* Change timeout for standby immediate to 10 seconds.*/
1835 timeout = 10000;
1836 break;
1837 case 0xF7:
1838 case 0xFA:
1839 /* Change timeout for vendor unique command to 10 secs */
1840 timeout = 10000;
1841 break;
1842 case ATA_CMD_SMART:
1843 /* Change timeout for vendor unique command to 10 secs */
1844 timeout = 10000;
1845 break;
1846 default:
1847 timeout = MTIP_IOCTL_COMMAND_TIMEOUT_MS;
1848 break;
1849 }
1850
1851 /* Determine the correct transfer size.*/
1852 if (force_single_sector)
1853 transfer_size = ATA_SECT_SIZE;
1854 else
1855 transfer_size = ATA_SECT_SIZE * fis.sect_count;
1856
1857 /* Execute the command.*/
1858 if (mtip_exec_internal_command(dd->port,
1859 &fis,
1860 5,
1861 dma_buffer,
1862 transfer_size,
1863 0,
1864 GFP_KERNEL,
1865 timeout) < 0) {
1866 err = -EIO;
1867 goto abort;
1868 }
1869
1870 task_file_data = readl(dd->port->mmio+PORT_TFDATA);
1871
1872 if ((req_task->data_phase == TASKFILE_IN) && !(task_file_data & 1)) {
1873 reply = dd->port->rxfis + RX_FIS_PIO_SETUP;
1874 req_task->io_ports[7] = reply->control;
1875 } else {
1876 reply = dd->port->rxfis + RX_FIS_D2H_REG;
1877 req_task->io_ports[7] = reply->command;
1878 }
1879
1880 /* reclaim the DMA buffers.*/
1881 if (inbuf_dma)
1882 pci_unmap_single(dd->pdev, inbuf_dma,
1883 taskin, DMA_FROM_DEVICE);
1884 if (outbuf_dma)
1885 pci_unmap_single(dd->pdev, outbuf_dma,
1886 taskout, DMA_TO_DEVICE);
1887 inbuf_dma = 0;
1888 outbuf_dma = 0;
1889
1890 /* return the ATA registers to the caller.*/
1891 req_task->io_ports[1] = reply->features;
1892 req_task->io_ports[2] = reply->sect_count;
1893 req_task->io_ports[3] = reply->lba_low;
1894 req_task->io_ports[4] = reply->lba_mid;
1895 req_task->io_ports[5] = reply->lba_hi;
1896 req_task->io_ports[6] = reply->device;
1897
1898 if (req_task->out_flags.all & 1) {
1899
1900 req_task->hob_ports[3] = reply->lba_low_ex;
1901 req_task->hob_ports[4] = reply->lba_mid_ex;
1902 req_task->hob_ports[5] = reply->lba_hi_ex;
1903 req_task->hob_ports[1] = reply->features_ex;
1904 req_task->hob_ports[2] = reply->sect_cnt_ex;
1905 }
1906
1907 /* Com rest after secure erase or lowlevel format */
1908 if (((fis.command == ATA_CMD_SEC_ERASE_UNIT) ||
1909 ((fis.command == 0xFC) &&
1910 (fis.features == 0x27 || fis.features == 0x72 ||
1911 fis.features == 0x62 || fis.features == 0x26))) &&
1912 !(reply->command & 1)) {
1913 mtip_restart_port(dd->port);
1914 }
1915
1916 dbg_printk(MTIP_DRV_NAME
1917 "%s: Completion: stat %x,"
1918 "err %x, sect_cnt %x, lbalo %x,"
1919 "lbamid %x, lbahi %x, dev %x\n",
1920 __func__,
1921 req_task->io_ports[7],
1922 req_task->io_ports[1],
1923 req_task->io_ports[2],
1924 req_task->io_ports[3],
1925 req_task->io_ports[4],
1926 req_task->io_ports[5],
1927 req_task->io_ports[6]);
1928
1929 if (taskout) {
1930 if (copy_to_user(buf + outtotal, outbuf, taskout)) {
1931 err = -EFAULT;
1932 goto abort;
1933 }
1934 }
1935 if (taskin) {
1936 if (copy_to_user(buf + intotal, inbuf, taskin)) {
1937 err = -EFAULT;
1938 goto abort;
1939 }
1940 }
1941abort:
1942 if (inbuf_dma)
1943 pci_unmap_single(dd->pdev, inbuf_dma,
1944 taskin, DMA_FROM_DEVICE);
1945 if (outbuf_dma)
1946 pci_unmap_single(dd->pdev, outbuf_dma,
1947 taskout, DMA_TO_DEVICE);
1948 kfree(outbuf);
1949 kfree(inbuf);
1950
1951 return err;
1952}
1953
1954/*
1955 * Handle IOCTL calls from the Block Layer.
1956 *
1957 * This function is called by the Block Layer when it receives an IOCTL
1958 * command that it does not understand. If the IOCTL command is not supported
1959 * this function returns -ENOTTY.
1960 *
1961 * @dd Pointer to the driver data structure.
1962 * @cmd IOCTL command passed from the Block Layer.
1963 * @arg IOCTL argument passed from the Block Layer.
1964 *
1965 * return value
1966 * 0 The IOCTL completed successfully.
1967 * -ENOTTY The specified command is not supported.
1968 * -EFAULT An error occurred copying data to a user space buffer.
1969 * -EIO An error occurred while executing the command.
1970 */
1971static int mtip_hw_ioctl(struct driver_data *dd, unsigned int cmd,
1972 unsigned long arg)
1973{
1974 switch (cmd) {
1975 case HDIO_GET_IDENTITY:
1976 if (mtip_get_identify(dd->port, (void __user *) arg) < 0) {
1977 dev_warn(&dd->pdev->dev,
1978 "Unable to read identity\n");
1979 return -EIO;
1980 }
1981
1982 break;
1983 case HDIO_DRIVE_CMD:
1984 {
1985 u8 drive_command[4];
1986
1987 /* Copy the user command info to our buffer. */
1988 if (copy_from_user(drive_command,
1989 (void __user *) arg,
1990 sizeof(drive_command)))
1991 return -EFAULT;
1992
1993 /* Execute the drive command. */
1994 if (exec_drive_command(dd->port,
1995 drive_command,
1996 (void __user *) (arg+4)))
1997 return -EIO;
1998
1999 /* Copy the status back to the users buffer. */
2000 if (copy_to_user((void __user *) arg,
2001 drive_command,
2002 sizeof(drive_command)))
2003 return -EFAULT;
2004
2005 break;
2006 }
2007 case HDIO_DRIVE_TASK:
2008 {
2009 u8 drive_command[7];
2010
2011 /* Copy the user command info to our buffer. */
2012 if (copy_from_user(drive_command,
2013 (void __user *) arg,
2014 sizeof(drive_command)))
2015 return -EFAULT;
2016
2017 /* Execute the drive command. */
2018 if (exec_drive_task(dd->port, drive_command))
2019 return -EIO;
2020
2021 /* Copy the status back to the users buffer. */
2022 if (copy_to_user((void __user *) arg,
2023 drive_command,
2024 sizeof(drive_command)))
2025 return -EFAULT;
2026
2027 break;
2028 }
2029 case HDIO_DRIVE_TASKFILE: {
2030 ide_task_request_t req_task;
2031 int ret, outtotal;
2032
2033 if (copy_from_user(&req_task, (void __user *) arg,
2034 sizeof(req_task)))
2035 return -EFAULT;
2036
2037 outtotal = sizeof(req_task);
2038
2039 ret = exec_drive_taskfile(dd, (void __user *) arg,
2040 &req_task, outtotal);
2041
2042 if (copy_to_user((void __user *) arg, &req_task,
2043 sizeof(req_task)))
2044 return -EFAULT;
2045
2046 return ret;
2047 }
2048
2049 default:
2050 return -EINVAL;
2051 }
2052 return 0;
2053}
2054
2055/*
2056 * Submit an IO to the hw
2057 *
2058 * This function is called by the block layer to issue an io
2059 * to the device. Upon completion, the callback function will
2060 * be called with the data parameter passed as the callback data.
2061 *
2062 * @dd Pointer to the driver data structure.
2063 * @start First sector to read.
2064 * @nsect Number of sectors to read.
2065 * @nents Number of entries in scatter list for the read command.
2066 * @tag The tag of this read command.
2067 * @callback Pointer to the function that should be called
2068 * when the read completes.
2069 * @data Callback data passed to the callback function
2070 * when the read completes.
2071 * @barrier If non-zero, this command must be completed before
2072 * issuing any other commands.
2073 * @dir Direction (read or write)
2074 *
2075 * return value
2076 * None
2077 */
2078static void mtip_hw_submit_io(struct driver_data *dd, sector_t start,
2079 int nsect, int nents, int tag, void *callback,
2080 void *data, int barrier, int dir)
2081{
2082 struct host_to_dev_fis *fis;
2083 struct mtip_port *port = dd->port;
2084 struct mtip_cmd *command = &port->commands[tag];
2085
2086 /* Map the scatter list for DMA access */
2087 if (dir == READ)
2088 nents = dma_map_sg(&dd->pdev->dev, command->sg,
2089 nents, DMA_FROM_DEVICE);
2090 else
2091 nents = dma_map_sg(&dd->pdev->dev, command->sg,
2092 nents, DMA_TO_DEVICE);
2093
2094 command->scatter_ents = nents;
2095
2096 /*
2097 * The number of retries for this command before it is
2098 * reported as a failure to the upper layers.
2099 */
2100 command->retries = MTIP_MAX_RETRIES;
2101
2102 /* Fill out fis */
2103 fis = command->command;
2104 fis->type = 0x27;
2105 fis->opts = 1 << 7;
2106 fis->command =
2107 (dir == READ ? ATA_CMD_FPDMA_READ : ATA_CMD_FPDMA_WRITE);
2108 *((unsigned int *) &fis->lba_low) = (start & 0xFFFFFF);
2109 *((unsigned int *) &fis->lba_low_ex) = ((start >> 24) & 0xFFFFFF);
2110 fis->device = 1 << 6;
2111 if (barrier)
2112 fis->device |= FUA_BIT;
2113 fis->features = nsect & 0xFF;
2114 fis->features_ex = (nsect >> 8) & 0xFF;
2115 fis->sect_count = ((tag << 3) | (tag >> 5));
2116 fis->sect_cnt_ex = 0;
2117 fis->control = 0;
2118 fis->res2 = 0;
2119 fis->res3 = 0;
2120 fill_command_sg(dd, command, nents);
2121
2122 /* Populate the command header */
2123 command->command_header->opts =
2124 __force_bit2int cpu_to_le32(
2125 (nents << 16) | 5 | AHCI_CMD_PREFETCH);
2126 command->command_header->byte_count = 0;
2127
2128 /*
2129 * Set the completion function and data for the command
2130 * within this layer.
2131 */
2132 command->comp_data = dd;
2133 command->comp_func = mtip_async_complete;
2134 command->direction = (dir == READ ? DMA_FROM_DEVICE : DMA_TO_DEVICE);
2135
2136 /*
2137 * Set the completion function and data for the command passed
2138 * from the upper layer.
2139 */
2140 command->async_data = data;
2141 command->async_callback = callback;
2142
2143 /*
2144 * To prevent this command from being issued
2145 * if an internal command is in progress or error handling is active.
2146 */
2147 if (unlikely(test_bit(MTIP_FLAG_IC_ACTIVE_BIT, &port->flags) ||
2148 test_bit(MTIP_FLAG_EH_ACTIVE_BIT, &port->flags))) {
2149 set_bit(tag, port->cmds_to_issue);
2150 set_bit(MTIP_FLAG_ISSUE_CMDS_BIT, &port->flags);
2151 return;
2152 }
2153
2154 /* Issue the command to the hardware */
2155 mtip_issue_ncq_command(port, tag);
2156
2157 /* Set the command's timeout value.*/
2158 port->commands[tag].comp_time = jiffies + msecs_to_jiffies(
2159 MTIP_NCQ_COMMAND_TIMEOUT_MS);
2160}
2161
2162/*
2163 * Release a command slot.
2164 *
2165 * @dd Pointer to the driver data structure.
2166 * @tag Slot tag
2167 *
2168 * return value
2169 * None
2170 */
2171static void mtip_hw_release_scatterlist(struct driver_data *dd, int tag)
2172{
2173 release_slot(dd->port, tag);
2174}
2175
2176/*
2177 * Obtain a command slot and return its associated scatter list.
2178 *
2179 * @dd Pointer to the driver data structure.
2180 * @tag Pointer to an int that will receive the allocated command
2181 * slot tag.
2182 *
2183 * return value
2184 * Pointer to the scatter list for the allocated command slot
2185 * or NULL if no command slots are available.
2186 */
2187static struct scatterlist *mtip_hw_get_scatterlist(struct driver_data *dd,
2188 int *tag)
2189{
2190 /*
2191 * It is possible that, even with this semaphore, a thread
2192 * may think that no command slots are available. Therefore, we
2193 * need to make an attempt to get_slot().
2194 */
2195 down(&dd->port->cmd_slot);
2196 *tag = get_slot(dd->port);
2197
2198 if (unlikely(*tag < 0))
2199 return NULL;
2200
2201 return dd->port->commands[*tag].sg;
2202}
2203
2204/*
2205 * Sysfs register/status dump.
2206 *
2207 * @dev Pointer to the device structure, passed by the kernrel.
2208 * @attr Pointer to the device_attribute structure passed by the kernel.
2209 * @buf Pointer to the char buffer that will receive the stats info.
2210 *
2211 * return value
2212 * The size, in bytes, of the data copied into buf.
2213 */
2214static ssize_t hw_show_registers(struct device *dev,
2215 struct device_attribute *attr,
2216 char *buf)
2217{
2218 u32 group_allocated;
2219 struct driver_data *dd = dev_to_disk(dev)->private_data;
2220 int size = 0;
2221 int n;
2222
2223 size += sprintf(&buf[size], "%s:\ns_active:\n", __func__);
2224
2225 for (n = 0; n < dd->slot_groups; n++)
2226 size += sprintf(&buf[size], "0x%08x\n",
2227 readl(dd->port->s_active[n]));
2228
2229 size += sprintf(&buf[size], "Command Issue:\n");
2230
2231 for (n = 0; n < dd->slot_groups; n++)
2232 size += sprintf(&buf[size], "0x%08x\n",
2233 readl(dd->port->cmd_issue[n]));
2234
2235 size += sprintf(&buf[size], "Allocated:\n");
2236
2237 for (n = 0; n < dd->slot_groups; n++) {
2238 if (sizeof(long) > sizeof(u32))
2239 group_allocated =
2240 dd->port->allocated[n/2] >> (32*(n&1));
2241 else
2242 group_allocated = dd->port->allocated[n];
2243 size += sprintf(&buf[size], "0x%08x\n",
2244 group_allocated);
2245 }
2246
2247 size += sprintf(&buf[size], "completed:\n");
2248
2249 for (n = 0; n < dd->slot_groups; n++)
2250 size += sprintf(&buf[size], "0x%08x\n",
2251 readl(dd->port->completed[n]));
2252
2253 size += sprintf(&buf[size], "PORT_IRQ_STAT 0x%08x\n",
2254 readl(dd->port->mmio + PORT_IRQ_STAT));
2255 size += sprintf(&buf[size], "HOST_IRQ_STAT 0x%08x\n",
2256 readl(dd->mmio + HOST_IRQ_STAT));
2257
2258 return size;
2259}
2260static DEVICE_ATTR(registers, S_IRUGO, hw_show_registers, NULL);
2261
2262/*
2263 * Create the sysfs related attributes.
2264 *
2265 * @dd Pointer to the driver data structure.
2266 * @kobj Pointer to the kobj for the block device.
2267 *
2268 * return value
2269 * 0 Operation completed successfully.
2270 * -EINVAL Invalid parameter.
2271 */
2272static int mtip_hw_sysfs_init(struct driver_data *dd, struct kobject *kobj)
2273{
2274 if (!kobj || !dd)
2275 return -EINVAL;
2276
2277 if (sysfs_create_file(kobj, &dev_attr_registers.attr))
2278 dev_warn(&dd->pdev->dev,
2279 "Error creating registers sysfs entry\n");
2280 return 0;
2281}
2282
2283/*
2284 * Remove the sysfs related attributes.
2285 *
2286 * @dd Pointer to the driver data structure.
2287 * @kobj Pointer to the kobj for the block device.
2288 *
2289 * return value
2290 * 0 Operation completed successfully.
2291 * -EINVAL Invalid parameter.
2292 */
2293static int mtip_hw_sysfs_exit(struct driver_data *dd, struct kobject *kobj)
2294{
2295 if (!kobj || !dd)
2296 return -EINVAL;
2297
2298 sysfs_remove_file(kobj, &dev_attr_registers.attr);
2299
2300 return 0;
2301}
2302
2303/*
2304 * Perform any init/resume time hardware setup
2305 *
2306 * @dd Pointer to the driver data structure.
2307 *
2308 * return value
2309 * None
2310 */
2311static inline void hba_setup(struct driver_data *dd)
2312{
2313 u32 hwdata;
2314 hwdata = readl(dd->mmio + HOST_HSORG);
2315
2316 /* interrupt bug workaround: use only 1 IS bit.*/
2317 writel(hwdata |
2318 HSORG_DISABLE_SLOTGRP_INTR |
2319 HSORG_DISABLE_SLOTGRP_PXIS,
2320 dd->mmio + HOST_HSORG);
2321}
2322
2323/*
2324 * Detect the details of the product, and store anything needed
2325 * into the driver data structure. This includes product type and
2326 * version and number of slot groups.
2327 *
2328 * @dd Pointer to the driver data structure.
2329 *
2330 * return value
2331 * None
2332 */
2333static void mtip_detect_product(struct driver_data *dd)
2334{
2335 u32 hwdata;
2336 unsigned int rev, slotgroups;
2337
2338 /*
2339 * HBA base + 0xFC [15:0] - vendor-specific hardware interface
2340 * info register:
2341 * [15:8] hardware/software interface rev#
2342 * [ 3] asic-style interface
2343 * [ 2:0] number of slot groups, minus 1 (only valid for asic-style).
2344 */
2345 hwdata = readl(dd->mmio + HOST_HSORG);
2346
2347 dd->product_type = MTIP_PRODUCT_UNKNOWN;
2348 dd->slot_groups = 1;
2349
2350 if (hwdata & 0x8) {
2351 dd->product_type = MTIP_PRODUCT_ASICFPGA;
2352 rev = (hwdata & HSORG_HWREV) >> 8;
2353 slotgroups = (hwdata & HSORG_SLOTGROUPS) + 1;
2354 dev_info(&dd->pdev->dev,
2355 "ASIC-FPGA design, HS rev 0x%x, "
2356 "%i slot groups [%i slots]\n",
2357 rev,
2358 slotgroups,
2359 slotgroups * 32);
2360
2361 if (slotgroups > MTIP_MAX_SLOT_GROUPS) {
2362 dev_warn(&dd->pdev->dev,
2363 "Warning: driver only supports "
2364 "%i slot groups.\n", MTIP_MAX_SLOT_GROUPS);
2365 slotgroups = MTIP_MAX_SLOT_GROUPS;
2366 }
2367 dd->slot_groups = slotgroups;
2368 return;
2369 }
2370
2371 dev_warn(&dd->pdev->dev, "Unrecognized product id\n");
2372}
2373
2374/*
2375 * Blocking wait for FTL rebuild to complete
2376 *
2377 * @dd Pointer to the DRIVER_DATA structure.
2378 *
2379 * return value
2380 * 0 FTL rebuild completed successfully
2381 * -EFAULT FTL rebuild error/timeout/interruption
2382 */
2383static int mtip_ftl_rebuild_poll(struct driver_data *dd)
2384{
2385 unsigned long timeout, cnt = 0, start;
2386
2387 dev_warn(&dd->pdev->dev,
2388 "FTL rebuild in progress. Polling for completion.\n");
2389
2390 start = jiffies;
2391 dd->ftlrebuildflag = 1;
2392 timeout = jiffies + msecs_to_jiffies(MTIP_FTL_REBUILD_TIMEOUT_MS);
2393
2394 do {
2395 if (mtip_check_surprise_removal(dd->pdev))
2396 return -EFAULT;
2397
2398 if (mtip_get_identify(dd->port, NULL) < 0)
2399 return -EFAULT;
2400
2401 if (*(dd->port->identify + MTIP_FTL_REBUILD_OFFSET) ==
2402 MTIP_FTL_REBUILD_MAGIC) {
2403 ssleep(1);
2404 /* Print message every 3 minutes */
2405 if (cnt++ >= 180) {
2406 dev_warn(&dd->pdev->dev,
2407 "FTL rebuild in progress (%d secs).\n",
2408 jiffies_to_msecs(jiffies - start) / 1000);
2409 cnt = 0;
2410 }
2411 } else {
2412 dev_warn(&dd->pdev->dev,
2413 "FTL rebuild complete (%d secs).\n",
2414 jiffies_to_msecs(jiffies - start) / 1000);
2415 dd->ftlrebuildflag = 0;
2416 mtip_block_initialize(dd);
2417 break;
2418 }
2419 ssleep(10);
2420 } while (time_before(jiffies, timeout));
2421
2422 /* Check for timeout */
2423 if (dd->ftlrebuildflag) {
2424 dev_err(&dd->pdev->dev,
2425 "Timed out waiting for FTL rebuild to complete (%d secs).\n",
2426 jiffies_to_msecs(jiffies - start) / 1000);
2427 return -EFAULT;
2428 }
2429
2430 return 0;
2431}
2432
2433/*
2434 * service thread to issue queued commands
2435 *
2436 * @data Pointer to the driver data structure.
2437 *
2438 * return value
2439 * 0
2440 */
2441
2442static int mtip_service_thread(void *data)
2443{
2444 struct driver_data *dd = (struct driver_data *)data;
2445 unsigned long slot, slot_start, slot_wrap;
2446 unsigned int num_cmd_slots = dd->slot_groups * 32;
2447 struct mtip_port *port = dd->port;
2448
2449 while (1) {
2450 /*
2451 * the condition is to check neither an internal command is
2452 * is in progress nor error handling is active
2453 */
2454 wait_event_interruptible(port->svc_wait, (port->flags) &&
2455 !test_bit(MTIP_FLAG_IC_ACTIVE_BIT, &port->flags) &&
2456 !test_bit(MTIP_FLAG_EH_ACTIVE_BIT, &port->flags));
2457
2458 if (kthread_should_stop())
2459 break;
2460
2461 set_bit(MTIP_FLAG_SVC_THD_ACTIVE_BIT, &port->flags);
2462 if (test_bit(MTIP_FLAG_ISSUE_CMDS_BIT, &port->flags)) {
2463 slot = 1;
2464 /* used to restrict the loop to one iteration */
2465 slot_start = num_cmd_slots;
2466 slot_wrap = 0;
2467 while (1) {
2468 slot = find_next_bit(port->cmds_to_issue,
2469 num_cmd_slots, slot);
2470 if (slot_wrap == 1) {
2471 if ((slot_start >= slot) ||
2472 (slot >= num_cmd_slots))
2473 break;
2474 }
2475 if (unlikely(slot_start == num_cmd_slots))
2476 slot_start = slot;
2477
2478 if (unlikely(slot == num_cmd_slots)) {
2479 slot = 1;
2480 slot_wrap = 1;
2481 continue;
2482 }
2483
2484 /* Issue the command to the hardware */
2485 mtip_issue_ncq_command(port, slot);
2486
2487 /* Set the command's timeout value.*/
2488 port->commands[slot].comp_time = jiffies +
2489 msecs_to_jiffies(MTIP_NCQ_COMMAND_TIMEOUT_MS);
2490
2491 clear_bit(slot, port->cmds_to_issue);
2492 }
2493
2494 clear_bit(MTIP_FLAG_ISSUE_CMDS_BIT, &port->flags);
2495 } else if (test_bit(MTIP_FLAG_REBUILD_BIT, &port->flags)) {
2496 mtip_ftl_rebuild_poll(dd);
2497 clear_bit(MTIP_FLAG_REBUILD_BIT, &port->flags);
2498 }
2499 clear_bit(MTIP_FLAG_SVC_THD_ACTIVE_BIT, &port->flags);
2500
2501 if (test_bit(MTIP_FLAG_SVC_THD_SHOULD_STOP_BIT, &port->flags))
2502 break;
2503 }
2504 return 0;
2505}
2506
2507/*
2508 * Called once for each card.
2509 *
2510 * @dd Pointer to the driver data structure.
2511 *
2512 * return value
2513 * 0 on success, else an error code.
2514 */
2515static int mtip_hw_init(struct driver_data *dd)
2516{
2517 int i;
2518 int rv;
2519 unsigned int num_command_slots;
2520
2521 dd->mmio = pcim_iomap_table(dd->pdev)[MTIP_ABAR];
2522
2523 mtip_detect_product(dd);
2524 if (dd->product_type == MTIP_PRODUCT_UNKNOWN) {
2525 rv = -EIO;
2526 goto out1;
2527 }
2528 num_command_slots = dd->slot_groups * 32;
2529
2530 hba_setup(dd);
2531
2532 tasklet_init(&dd->tasklet, mtip_tasklet, (unsigned long)dd);
2533
2534 dd->port = kzalloc(sizeof(struct mtip_port), GFP_KERNEL);
2535 if (!dd->port) {
2536 dev_err(&dd->pdev->dev,
2537 "Memory allocation: port structure\n");
2538 return -ENOMEM;
2539 }
2540
2541 /* Counting semaphore to track command slot usage */
2542 sema_init(&dd->port->cmd_slot, num_command_slots - 1);
2543
2544 /* Spinlock to prevent concurrent issue */
2545 spin_lock_init(&dd->port->cmd_issue_lock);
2546
2547 /* Set the port mmio base address. */
2548 dd->port->mmio = dd->mmio + PORT_OFFSET;
2549 dd->port->dd = dd;
2550
2551 /* Allocate memory for the command list. */
2552 dd->port->command_list =
2553 dmam_alloc_coherent(&dd->pdev->dev,
2554 HW_PORT_PRIV_DMA_SZ + (ATA_SECT_SIZE * 2),
2555 &dd->port->command_list_dma,
2556 GFP_KERNEL);
2557 if (!dd->port->command_list) {
2558 dev_err(&dd->pdev->dev,
2559 "Memory allocation: command list\n");
2560 rv = -ENOMEM;
2561 goto out1;
2562 }
2563
2564 /* Clear the memory we have allocated. */
2565 memset(dd->port->command_list,
2566 0,
2567 HW_PORT_PRIV_DMA_SZ + (ATA_SECT_SIZE * 2));
2568
2569 /* Setup the addresse of the RX FIS. */
2570 dd->port->rxfis = dd->port->command_list + HW_CMD_SLOT_SZ;
2571 dd->port->rxfis_dma = dd->port->command_list_dma + HW_CMD_SLOT_SZ;
2572
2573 /* Setup the address of the command tables. */
2574 dd->port->command_table = dd->port->rxfis + AHCI_RX_FIS_SZ;
2575 dd->port->command_tbl_dma = dd->port->rxfis_dma + AHCI_RX_FIS_SZ;
2576
2577 /* Setup the address of the identify data. */
2578 dd->port->identify = dd->port->command_table +
2579 HW_CMD_TBL_AR_SZ;
2580 dd->port->identify_dma = dd->port->command_tbl_dma +
2581 HW_CMD_TBL_AR_SZ;
2582
2583 /* Setup the address of the sector buffer. */
2584 dd->port->sector_buffer = (void *) dd->port->identify + ATA_SECT_SIZE;
2585 dd->port->sector_buffer_dma = dd->port->identify_dma + ATA_SECT_SIZE;
2586
2587 /* Point the command headers at the command tables. */
2588 for (i = 0; i < num_command_slots; i++) {
2589 dd->port->commands[i].command_header =
2590 dd->port->command_list +
2591 (sizeof(struct mtip_cmd_hdr) * i);
2592 dd->port->commands[i].command_header_dma =
2593 dd->port->command_list_dma +
2594 (sizeof(struct mtip_cmd_hdr) * i);
2595
2596 dd->port->commands[i].command =
2597 dd->port->command_table + (HW_CMD_TBL_SZ * i);
2598 dd->port->commands[i].command_dma =
2599 dd->port->command_tbl_dma + (HW_CMD_TBL_SZ * i);
2600
2601 if (readl(dd->mmio + HOST_CAP) & HOST_CAP_64)
2602 dd->port->commands[i].command_header->ctbau =
2603 __force_bit2int cpu_to_le32(
2604 (dd->port->commands[i].command_dma >> 16) >> 16);
2605 dd->port->commands[i].command_header->ctba =
2606 __force_bit2int cpu_to_le32(
2607 dd->port->commands[i].command_dma & 0xFFFFFFFF);
2608
2609 /*
2610 * If this is not done, a bug is reported by the stock
2611 * FC11 i386. Due to the fact that it has lots of kernel
2612 * debugging enabled.
2613 */
2614 sg_init_table(dd->port->commands[i].sg, MTIP_MAX_SG);
2615
2616 /* Mark all commands as currently inactive.*/
2617 atomic_set(&dd->port->commands[i].active, 0);
2618 }
2619
2620 /* Setup the pointers to the extended s_active and CI registers. */
2621 for (i = 0; i < dd->slot_groups; i++) {
2622 dd->port->s_active[i] =
2623 dd->port->mmio + i*0x80 + PORT_SCR_ACT;
2624 dd->port->cmd_issue[i] =
2625 dd->port->mmio + i*0x80 + PORT_COMMAND_ISSUE;
2626 dd->port->completed[i] =
2627 dd->port->mmio + i*0x80 + PORT_SDBV;
2628 }
2629
2630 /* Reset the HBA. */
2631 if (mtip_hba_reset(dd) < 0) {
2632 dev_err(&dd->pdev->dev,
2633 "Card did not reset within timeout\n");
2634 rv = -EIO;
2635 goto out2;
2636 }
2637
2638 mtip_init_port(dd->port);
2639 mtip_start_port(dd->port);
2640
2641 /* Setup the ISR and enable interrupts. */
2642 rv = devm_request_irq(&dd->pdev->dev,
2643 dd->pdev->irq,
2644 mtip_irq_handler,
2645 IRQF_SHARED,
2646 dev_driver_string(&dd->pdev->dev),
2647 dd);
2648
2649 if (rv) {
2650 dev_err(&dd->pdev->dev,
2651 "Unable to allocate IRQ %d\n", dd->pdev->irq);
2652 goto out2;
2653 }
2654
2655 /* Enable interrupts on the HBA. */
2656 writel(readl(dd->mmio + HOST_CTL) | HOST_IRQ_EN,
2657 dd->mmio + HOST_CTL);
2658
2659 init_timer(&dd->port->cmd_timer);
2660 init_waitqueue_head(&dd->port->svc_wait);
2661
2662 dd->port->cmd_timer.data = (unsigned long int) dd->port;
2663 dd->port->cmd_timer.function = mtip_timeout_function;
2664 mod_timer(&dd->port->cmd_timer,
2665 jiffies + msecs_to_jiffies(MTIP_TIMEOUT_CHECK_PERIOD));
2666
2667 if (mtip_get_identify(dd->port, NULL) < 0) {
2668 rv = -EFAULT;
2669 goto out3;
2670 }
2671
2672 if (*(dd->port->identify + MTIP_FTL_REBUILD_OFFSET) ==
2673 MTIP_FTL_REBUILD_MAGIC) {
2674 set_bit(MTIP_FLAG_REBUILD_BIT, &dd->port->flags);
2675 return MTIP_FTL_REBUILD_MAGIC;
2676 }
2677 mtip_dump_identify(dd->port);
2678 return rv;
2679
2680out3:
2681 del_timer_sync(&dd->port->cmd_timer);
2682
2683 /* Disable interrupts on the HBA. */
2684 writel(readl(dd->mmio + HOST_CTL) & ~HOST_IRQ_EN,
2685 dd->mmio + HOST_CTL);
2686
2687 /*Release the IRQ. */
2688 devm_free_irq(&dd->pdev->dev, dd->pdev->irq, dd);
2689
2690out2:
2691 mtip_deinit_port(dd->port);
2692
2693 /* Free the command/command header memory. */
2694 dmam_free_coherent(&dd->pdev->dev,
2695 HW_PORT_PRIV_DMA_SZ + (ATA_SECT_SIZE * 2),
2696 dd->port->command_list,
2697 dd->port->command_list_dma);
2698out1:
2699 /* Free the memory allocated for the for structure. */
2700 kfree(dd->port);
2701
2702 return rv;
2703}
2704
2705/*
2706 * Called to deinitialize an interface.
2707 *
2708 * @dd Pointer to the driver data structure.
2709 *
2710 * return value
2711 * 0
2712 */
2713static int mtip_hw_exit(struct driver_data *dd)
2714{
2715 /*
2716 * Send standby immediate (E0h) to the drive so that it
2717 * saves its state.
2718 */
2719 if (atomic_read(&dd->drv_cleanup_done) != true) {
2720
2721 mtip_standby_immediate(dd->port);
2722
2723 /* de-initialize the port. */
2724 mtip_deinit_port(dd->port);
2725
2726 /* Disable interrupts on the HBA. */
2727 writel(readl(dd->mmio + HOST_CTL) & ~HOST_IRQ_EN,
2728 dd->mmio + HOST_CTL);
2729 }
2730
2731 del_timer_sync(&dd->port->cmd_timer);
2732
2733 /* Release the IRQ. */
2734 devm_free_irq(&dd->pdev->dev, dd->pdev->irq, dd);
2735
2736 /* Stop the bottom half tasklet. */
2737 tasklet_kill(&dd->tasklet);
2738
2739 /* Free the command/command header memory. */
2740 dmam_free_coherent(&dd->pdev->dev,
2741 HW_PORT_PRIV_DMA_SZ + (ATA_SECT_SIZE * 2),
2742 dd->port->command_list,
2743 dd->port->command_list_dma);
2744 /* Free the memory allocated for the for structure. */
2745 kfree(dd->port);
2746
2747 return 0;
2748}
2749
2750/*
2751 * Issue a Standby Immediate command to the device.
2752 *
2753 * This function is called by the Block Layer just before the
2754 * system powers off during a shutdown.
2755 *
2756 * @dd Pointer to the driver data structure.
2757 *
2758 * return value
2759 * 0
2760 */
2761static int mtip_hw_shutdown(struct driver_data *dd)
2762{
2763 /*
2764 * Send standby immediate (E0h) to the drive so that it
2765 * saves its state.
2766 */
2767 mtip_standby_immediate(dd->port);
2768
2769 return 0;
2770}
2771
2772/*
2773 * Suspend function
2774 *
2775 * This function is called by the Block Layer just before the
2776 * system hibernates.
2777 *
2778 * @dd Pointer to the driver data structure.
2779 *
2780 * return value
2781 * 0 Suspend was successful
2782 * -EFAULT Suspend was not successful
2783 */
2784static int mtip_hw_suspend(struct driver_data *dd)
2785{
2786 /*
2787 * Send standby immediate (E0h) to the drive
2788 * so that it saves its state.
2789 */
2790 if (mtip_standby_immediate(dd->port) != 0) {
2791 dev_err(&dd->pdev->dev,
2792 "Failed standby-immediate command\n");
2793 return -EFAULT;
2794 }
2795
2796 /* Disable interrupts on the HBA.*/
2797 writel(readl(dd->mmio + HOST_CTL) & ~HOST_IRQ_EN,
2798 dd->mmio + HOST_CTL);
2799 mtip_deinit_port(dd->port);
2800
2801 return 0;
2802}
2803
2804/*
2805 * Resume function
2806 *
2807 * This function is called by the Block Layer as the
2808 * system resumes.
2809 *
2810 * @dd Pointer to the driver data structure.
2811 *
2812 * return value
2813 * 0 Resume was successful
2814 * -EFAULT Resume was not successful
2815 */
2816static int mtip_hw_resume(struct driver_data *dd)
2817{
2818 /* Perform any needed hardware setup steps */
2819 hba_setup(dd);
2820
2821 /* Reset the HBA */
2822 if (mtip_hba_reset(dd) != 0) {
2823 dev_err(&dd->pdev->dev,
2824 "Unable to reset the HBA\n");
2825 return -EFAULT;
2826 }
2827
2828 /*
2829 * Enable the port, DMA engine, and FIS reception specific
2830 * h/w in controller.
2831 */
2832 mtip_init_port(dd->port);
2833 mtip_start_port(dd->port);
2834
2835 /* Enable interrupts on the HBA.*/
2836 writel(readl(dd->mmio + HOST_CTL) | HOST_IRQ_EN,
2837 dd->mmio + HOST_CTL);
2838
2839 return 0;
2840}
2841
2842/*
2843 * Helper function for reusing disk name
2844 * upon hot insertion.
2845 */
2846static int rssd_disk_name_format(char *prefix,
2847 int index,
2848 char *buf,
2849 int buflen)
2850{
2851 const int base = 'z' - 'a' + 1;
2852 char *begin = buf + strlen(prefix);
2853 char *end = buf + buflen;
2854 char *p;
2855 int unit;
2856
2857 p = end - 1;
2858 *p = '\0';
2859 unit = base;
2860 do {
2861 if (p == begin)
2862 return -EINVAL;
2863 *--p = 'a' + (index % unit);
2864 index = (index / unit) - 1;
2865 } while (index >= 0);
2866
2867 memmove(begin, p, end - p);
2868 memcpy(buf, prefix, strlen(prefix));
2869
2870 return 0;
2871}
2872
2873/*
2874 * Block layer IOCTL handler.
2875 *
2876 * @dev Pointer to the block_device structure.
2877 * @mode ignored
2878 * @cmd IOCTL command passed from the user application.
2879 * @arg Argument passed from the user application.
2880 *
2881 * return value
2882 * 0 IOCTL completed successfully.
2883 * -ENOTTY IOCTL not supported or invalid driver data
2884 * structure pointer.
2885 */
2886static int mtip_block_ioctl(struct block_device *dev,
2887 fmode_t mode,
2888 unsigned cmd,
2889 unsigned long arg)
2890{
2891 struct driver_data *dd = dev->bd_disk->private_data;
2892
2893 if (!capable(CAP_SYS_ADMIN))
2894 return -EACCES;
2895
2896 if (!dd)
2897 return -ENOTTY;
2898
2899 switch (cmd) {
2900 case BLKFLSBUF:
2901 return -ENOTTY;
2902 default:
2903 return mtip_hw_ioctl(dd, cmd, arg);
2904 }
2905}
2906
2907#ifdef CONFIG_COMPAT
2908/*
2909 * Block layer compat IOCTL handler.
2910 *
2911 * @dev Pointer to the block_device structure.
2912 * @mode ignored
2913 * @cmd IOCTL command passed from the user application.
2914 * @arg Argument passed from the user application.
2915 *
2916 * return value
2917 * 0 IOCTL completed successfully.
2918 * -ENOTTY IOCTL not supported or invalid driver data
2919 * structure pointer.
2920 */
2921static int mtip_block_compat_ioctl(struct block_device *dev,
2922 fmode_t mode,
2923 unsigned cmd,
2924 unsigned long arg)
2925{
2926 struct driver_data *dd = dev->bd_disk->private_data;
2927
2928 if (!capable(CAP_SYS_ADMIN))
2929 return -EACCES;
2930
2931 if (!dd)
2932 return -ENOTTY;
2933
2934 switch (cmd) {
2935 case BLKFLSBUF:
2936 return -ENOTTY;
2937 case HDIO_DRIVE_TASKFILE: {
2938 struct mtip_compat_ide_task_request_s __user *compat_req_task;
2939 ide_task_request_t req_task;
2940 int compat_tasksize, outtotal, ret;
2941
2942 compat_tasksize =
2943 sizeof(struct mtip_compat_ide_task_request_s);
2944
2945 compat_req_task =
2946 (struct mtip_compat_ide_task_request_s __user *) arg;
2947
2948 if (copy_from_user(&req_task, (void __user *) arg,
2949 compat_tasksize - (2 * sizeof(compat_long_t))))
2950 return -EFAULT;
2951
2952 if (get_user(req_task.out_size, &compat_req_task->out_size))
2953 return -EFAULT;
2954
2955 if (get_user(req_task.in_size, &compat_req_task->in_size))
2956 return -EFAULT;
2957
2958 outtotal = sizeof(struct mtip_compat_ide_task_request_s);
2959
2960 ret = exec_drive_taskfile(dd, (void __user *) arg,
2961 &req_task, outtotal);
2962
2963 if (copy_to_user((void __user *) arg, &req_task,
2964 compat_tasksize -
2965 (2 * sizeof(compat_long_t))))
2966 return -EFAULT;
2967
2968 if (put_user(req_task.out_size, &compat_req_task->out_size))
2969 return -EFAULT;
2970
2971 if (put_user(req_task.in_size, &compat_req_task->in_size))
2972 return -EFAULT;
2973
2974 return ret;
2975 }
2976 default:
2977 return mtip_hw_ioctl(dd, cmd, arg);
2978 }
2979}
2980#endif
2981
2982/*
2983 * Obtain the geometry of the device.
2984 *
2985 * You may think that this function is obsolete, but some applications,
2986 * fdisk for example still used CHS values. This function describes the
2987 * device as having 224 heads and 56 sectors per cylinder. These values are
2988 * chosen so that each cylinder is aligned on a 4KB boundary. Since a
2989 * partition is described in terms of a start and end cylinder this means
2990 * that each partition is also 4KB aligned. Non-aligned partitions adversely
2991 * affects performance.
2992 *
2993 * @dev Pointer to the block_device strucutre.
2994 * @geo Pointer to a hd_geometry structure.
2995 *
2996 * return value
2997 * 0 Operation completed successfully.
2998 * -ENOTTY An error occurred while reading the drive capacity.
2999 */
3000static int mtip_block_getgeo(struct block_device *dev,
3001 struct hd_geometry *geo)
3002{
3003 struct driver_data *dd = dev->bd_disk->private_data;
3004 sector_t capacity;
3005
3006 if (!dd)
3007 return -ENOTTY;
3008
3009 if (!(mtip_hw_get_capacity(dd, &capacity))) {
3010 dev_warn(&dd->pdev->dev,
3011 "Could not get drive capacity.\n");
3012 return -ENOTTY;
3013 }
3014
3015 geo->heads = 224;
3016 geo->sectors = 56;
3017 sector_div(capacity, (geo->heads * geo->sectors));
3018 geo->cylinders = capacity;
3019 return 0;
3020}
3021
3022/*
3023 * Block device operation function.
3024 *
3025 * This structure contains pointers to the functions required by the block
3026 * layer.
3027 */
3028static const struct block_device_operations mtip_block_ops = {
3029 .ioctl = mtip_block_ioctl,
3030#ifdef CONFIG_COMPAT
3031 .compat_ioctl = mtip_block_compat_ioctl,
3032#endif
3033 .getgeo = mtip_block_getgeo,
3034 .owner = THIS_MODULE
3035};
3036
3037/*
3038 * Block layer make request function.
3039 *
3040 * This function is called by the kernel to process a BIO for
3041 * the P320 device.
3042 *
3043 * @queue Pointer to the request queue. Unused other than to obtain
3044 * the driver data structure.
3045 * @bio Pointer to the BIO.
3046 *
3047 */
3048static void mtip_make_request(struct request_queue *queue, struct bio *bio)
3049{
3050 struct driver_data *dd = queue->queuedata;
3051 struct scatterlist *sg;
3052 struct bio_vec *bvec;
3053 int nents = 0;
3054 int tag = 0;
3055
3056 if (unlikely(!bio_has_data(bio))) {
3057 blk_queue_flush(queue, 0);
3058 bio_endio(bio, 0);
3059 return;
3060 }
3061
3062 sg = mtip_hw_get_scatterlist(dd, &tag);
3063 if (likely(sg != NULL)) {
3064 blk_queue_bounce(queue, &bio);
3065
3066 if (unlikely((bio)->bi_vcnt > MTIP_MAX_SG)) {
3067 dev_warn(&dd->pdev->dev,
3068 "Maximum number of SGL entries exceeded");
3069 bio_io_error(bio);
3070 mtip_hw_release_scatterlist(dd, tag);
3071 return;
3072 }
3073
3074 /* Create the scatter list for this bio. */
3075 bio_for_each_segment(bvec, bio, nents) {
3076 sg_set_page(&sg[nents],
3077 bvec->bv_page,
3078 bvec->bv_len,
3079 bvec->bv_offset);
3080 }
3081
3082 /* Issue the read/write. */
3083 mtip_hw_submit_io(dd,
3084 bio->bi_sector,
3085 bio_sectors(bio),
3086 nents,
3087 tag,
3088 bio_endio,
3089 bio,
3090 bio->bi_rw & REQ_FUA,
3091 bio_data_dir(bio));
3092 } else
3093 bio_io_error(bio);
3094}
3095
3096/*
3097 * Block layer initialization function.
3098 *
3099 * This function is called once by the PCI layer for each P320
3100 * device that is connected to the system.
3101 *
3102 * @dd Pointer to the driver data structure.
3103 *
3104 * return value
3105 * 0 on success else an error code.
3106 */
3107static int mtip_block_initialize(struct driver_data *dd)
3108{
3109 int rv = 0, wait_for_rebuild = 0;
3110 sector_t capacity;
3111 unsigned int index = 0;
3112 struct kobject *kobj;
3113 unsigned char thd_name[16];
3114
3115 if (dd->disk)
3116 goto skip_create_disk; /* hw init done, before rebuild */
3117
3118 /* Initialize the protocol layer. */
3119 wait_for_rebuild = mtip_hw_init(dd);
3120 if (wait_for_rebuild < 0) {
3121 dev_err(&dd->pdev->dev,
3122 "Protocol layer initialization failed\n");
3123 rv = -EINVAL;
3124 goto protocol_init_error;
3125 }
3126
3127 dd->disk = alloc_disk(MTIP_MAX_MINORS);
3128 if (dd->disk == NULL) {
3129 dev_err(&dd->pdev->dev,
3130 "Unable to allocate gendisk structure\n");
3131 rv = -EINVAL;
3132 goto alloc_disk_error;
3133 }
3134
3135 /* Generate the disk name, implemented same as in sd.c */
3136 do {
3137 if (!ida_pre_get(&rssd_index_ida, GFP_KERNEL))
3138 goto ida_get_error;
3139
3140 spin_lock(&rssd_index_lock);
3141 rv = ida_get_new(&rssd_index_ida, &index);
3142 spin_unlock(&rssd_index_lock);
3143 } while (rv == -EAGAIN);
3144
3145 if (rv)
3146 goto ida_get_error;
3147
3148 rv = rssd_disk_name_format("rssd",
3149 index,
3150 dd->disk->disk_name,
3151 DISK_NAME_LEN);
3152 if (rv)
3153 goto disk_index_error;
3154
3155 dd->disk->driverfs_dev = &dd->pdev->dev;
3156 dd->disk->major = dd->major;
3157 dd->disk->first_minor = dd->instance * MTIP_MAX_MINORS;
3158 dd->disk->fops = &mtip_block_ops;
3159 dd->disk->private_data = dd;
3160 dd->index = index;
3161
3162 /*
3163 * if rebuild pending, start the service thread, and delay the block
3164 * queue creation and add_disk()
3165 */
3166 if (wait_for_rebuild == MTIP_FTL_REBUILD_MAGIC)
3167 goto start_service_thread;
3168
3169skip_create_disk:
3170 /* Allocate the request queue. */
3171 dd->queue = blk_alloc_queue(GFP_KERNEL);
3172 if (dd->queue == NULL) {
3173 dev_err(&dd->pdev->dev,
3174 "Unable to allocate request queue\n");
3175 rv = -ENOMEM;
3176 goto block_queue_alloc_init_error;
3177 }
3178
3179 /* Attach our request function to the request queue. */
3180 blk_queue_make_request(dd->queue, mtip_make_request);
3181
3182 dd->disk->queue = dd->queue;
3183 dd->queue->queuedata = dd;
3184
3185 /* Set device limits. */
3186 set_bit(QUEUE_FLAG_NONROT, &dd->queue->queue_flags);
3187 blk_queue_max_segments(dd->queue, MTIP_MAX_SG);
3188 blk_queue_physical_block_size(dd->queue, 4096);
3189 blk_queue_io_min(dd->queue, 4096);
3190 blk_queue_flush(dd->queue, 0);
3191
3192 /* Set the capacity of the device in 512 byte sectors. */
3193 if (!(mtip_hw_get_capacity(dd, &capacity))) {
3194 dev_warn(&dd->pdev->dev,
3195 "Could not read drive capacity\n");
3196 rv = -EIO;
3197 goto read_capacity_error;
3198 }
3199 set_capacity(dd->disk, capacity);
3200
3201 /* Enable the block device and add it to /dev */
3202 add_disk(dd->disk);
3203
3204 /*
3205 * Now that the disk is active, initialize any sysfs attributes
3206 * managed by the protocol layer.
3207 */
3208 kobj = kobject_get(&disk_to_dev(dd->disk)->kobj);
3209 if (kobj) {
3210 mtip_hw_sysfs_init(dd, kobj);
3211 kobject_put(kobj);
3212 }
3213
3214 if (dd->mtip_svc_handler)
3215 return rv; /* service thread created for handling rebuild */
3216
3217start_service_thread:
3218 sprintf(thd_name, "mtip_svc_thd_%02d", index);
3219
3220 dd->mtip_svc_handler = kthread_run(mtip_service_thread,
3221 dd, thd_name);
3222
3223 if (IS_ERR(dd->mtip_svc_handler)) {
3224 printk(KERN_ERR "mtip32xx: service thread failed to start\n");
3225 dd->mtip_svc_handler = NULL;
3226 rv = -EFAULT;
3227 goto kthread_run_error;
3228 }
3229
3230 return rv;
3231
3232kthread_run_error:
3233 /* Delete our gendisk. This also removes the device from /dev */
3234 del_gendisk(dd->disk);
3235
3236read_capacity_error:
3237 blk_cleanup_queue(dd->queue);
3238
3239block_queue_alloc_init_error:
3240disk_index_error:
3241 spin_lock(&rssd_index_lock);
3242 ida_remove(&rssd_index_ida, index);
3243 spin_unlock(&rssd_index_lock);
3244
3245ida_get_error:
3246 put_disk(dd->disk);
3247
3248alloc_disk_error:
3249 mtip_hw_exit(dd); /* De-initialize the protocol layer. */
3250
3251protocol_init_error:
3252 return rv;
3253}
3254
3255/*
3256 * Block layer deinitialization function.
3257 *
3258 * Called by the PCI layer as each P320 device is removed.
3259 *
3260 * @dd Pointer to the driver data structure.
3261 *
3262 * return value
3263 * 0
3264 */
3265static int mtip_block_remove(struct driver_data *dd)
3266{
3267 struct kobject *kobj;
3268
3269 if (dd->mtip_svc_handler) {
3270 set_bit(MTIP_FLAG_SVC_THD_SHOULD_STOP_BIT, &dd->port->flags);
3271 wake_up_interruptible(&dd->port->svc_wait);
3272 kthread_stop(dd->mtip_svc_handler);
3273 }
3274
3275 /* Clean up the sysfs attributes managed by the protocol layer. */
3276 kobj = kobject_get(&disk_to_dev(dd->disk)->kobj);
3277 if (kobj) {
3278 mtip_hw_sysfs_exit(dd, kobj);
3279 kobject_put(kobj);
3280 }
3281
3282 /*
3283 * Delete our gendisk structure. This also removes the device
3284 * from /dev
3285 */
3286 del_gendisk(dd->disk);
3287 blk_cleanup_queue(dd->queue);
3288 dd->disk = NULL;
3289 dd->queue = NULL;
3290
3291 /* De-initialize the protocol layer. */
3292 mtip_hw_exit(dd);
3293
3294 return 0;
3295}
3296
3297/*
3298 * Function called by the PCI layer when just before the
3299 * machine shuts down.
3300 *
3301 * If a protocol layer shutdown function is present it will be called
3302 * by this function.
3303 *
3304 * @dd Pointer to the driver data structure.
3305 *
3306 * return value
3307 * 0
3308 */
3309static int mtip_block_shutdown(struct driver_data *dd)
3310{
3311 dev_info(&dd->pdev->dev,
3312 "Shutting down %s ...\n", dd->disk->disk_name);
3313
3314 /* Delete our gendisk structure, and cleanup the blk queue. */
3315 del_gendisk(dd->disk);
3316 blk_cleanup_queue(dd->queue);
3317 dd->disk = NULL;
3318 dd->queue = NULL;
3319
3320 mtip_hw_shutdown(dd);
3321 return 0;
3322}
3323
3324static int mtip_block_suspend(struct driver_data *dd)
3325{
3326 dev_info(&dd->pdev->dev,
3327 "Suspending %s ...\n", dd->disk->disk_name);
3328 mtip_hw_suspend(dd);
3329 return 0;
3330}
3331
3332static int mtip_block_resume(struct driver_data *dd)
3333{
3334 dev_info(&dd->pdev->dev, "Resuming %s ...\n",
3335 dd->disk->disk_name);
3336 mtip_hw_resume(dd);
3337 return 0;
3338}
3339
3340/*
3341 * Called for each supported PCI device detected.
3342 *
3343 * This function allocates the private data structure, enables the
3344 * PCI device and then calls the block layer initialization function.
3345 *
3346 * return value
3347 * 0 on success else an error code.
3348 */
3349static int mtip_pci_probe(struct pci_dev *pdev,
3350 const struct pci_device_id *ent)
3351{
3352 int rv = 0;
3353 struct driver_data *dd = NULL;
3354
3355 /* Allocate memory for this devices private data. */
3356 dd = kzalloc(sizeof(struct driver_data), GFP_KERNEL);
3357 if (dd == NULL) {
3358 dev_err(&pdev->dev,
3359 "Unable to allocate memory for driver data\n");
3360 return -ENOMEM;
3361 }
3362
3363 /* Set the atomic variable as 1 in case of SRSI */
3364 atomic_set(&dd->drv_cleanup_done, true);
3365
3366 atomic_set(&dd->resumeflag, false);
3367
3368 /* Attach the private data to this PCI device. */
3369 pci_set_drvdata(pdev, dd);
3370
3371 rv = pcim_enable_device(pdev);
3372 if (rv < 0) {
3373 dev_err(&pdev->dev, "Unable to enable device\n");
3374 goto iomap_err;
3375 }
3376
3377 /* Map BAR5 to memory. */
3378 rv = pcim_iomap_regions(pdev, 1 << MTIP_ABAR, MTIP_DRV_NAME);
3379 if (rv < 0) {
3380 dev_err(&pdev->dev, "Unable to map regions\n");
3381 goto iomap_err;
3382 }
3383
3384 if (!pci_set_dma_mask(pdev, DMA_BIT_MASK(64))) {
3385 rv = pci_set_consistent_dma_mask(pdev, DMA_BIT_MASK(64));
3386
3387 if (rv) {
3388 rv = pci_set_consistent_dma_mask(pdev,
3389 DMA_BIT_MASK(32));
3390 if (rv) {
3391 dev_warn(&pdev->dev,
3392 "64-bit DMA enable failed\n");
3393 goto setmask_err;
3394 }
3395 }
3396 }
3397
3398 pci_set_master(pdev);
3399
3400 if (pci_enable_msi(pdev)) {
3401 dev_warn(&pdev->dev,
3402 "Unable to enable MSI interrupt.\n");
3403 goto block_initialize_err;
3404 }
3405
3406 /* Copy the info we may need later into the private data structure. */
3407 dd->major = mtip_major;
3408 dd->instance = instance;
3409 dd->pdev = pdev;
3410
3411 /* Initialize the block layer. */
3412 rv = mtip_block_initialize(dd);
3413 if (rv < 0) {
3414 dev_err(&pdev->dev,
3415 "Unable to initialize block layer\n");
3416 goto block_initialize_err;
3417 }
3418
3419 /*
3420 * Increment the instance count so that each device has a unique
3421 * instance number.
3422 */
3423 instance++;
3424
3425 goto done;
3426
3427block_initialize_err:
3428 pci_disable_msi(pdev);
3429
3430setmask_err:
3431 pcim_iounmap_regions(pdev, 1 << MTIP_ABAR);
3432
3433iomap_err:
3434 kfree(dd);
3435 pci_set_drvdata(pdev, NULL);
3436 return rv;
3437done:
3438 /* Set the atomic variable as 0 in case of SRSI */
3439 atomic_set(&dd->drv_cleanup_done, true);
3440
3441 return rv;
3442}
3443
3444/*
3445 * Called for each probed device when the device is removed or the
3446 * driver is unloaded.
3447 *
3448 * return value
3449 * None
3450 */
3451static void mtip_pci_remove(struct pci_dev *pdev)
3452{
3453 struct driver_data *dd = pci_get_drvdata(pdev);
3454 int counter = 0;
3455
3456 if (mtip_check_surprise_removal(pdev)) {
3457 while (atomic_read(&dd->drv_cleanup_done) == false) {
3458 counter++;
3459 msleep(20);
3460 if (counter == 10) {
3461 /* Cleanup the outstanding commands */
3462 mtip_command_cleanup(dd);
3463 break;
3464 }
3465 }
3466 }
3467 /* Set the atomic variable as 1 in case of SRSI */
3468 atomic_set(&dd->drv_cleanup_done, true);
3469
3470 /* Clean up the block layer. */
3471 mtip_block_remove(dd);
3472
3473 pci_disable_msi(pdev);
3474
3475 kfree(dd);
3476 pcim_iounmap_regions(pdev, 1 << MTIP_ABAR);
3477}
3478
3479/*
3480 * Called for each probed device when the device is suspended.
3481 *
3482 * return value
3483 * 0 Success
3484 * <0 Error
3485 */
3486static int mtip_pci_suspend(struct pci_dev *pdev, pm_message_t mesg)
3487{
3488 int rv = 0;
3489 struct driver_data *dd = pci_get_drvdata(pdev);
3490
3491 if (!dd) {
3492 dev_err(&pdev->dev,
3493 "Driver private datastructure is NULL\n");
3494 return -EFAULT;
3495 }
3496
3497 atomic_set(&dd->resumeflag, true);
3498
3499 /* Disable ports & interrupts then send standby immediate */
3500 rv = mtip_block_suspend(dd);
3501 if (rv < 0) {
3502 dev_err(&pdev->dev,
3503 "Failed to suspend controller\n");
3504 return rv;
3505 }
3506
3507 /*
3508 * Save the pci config space to pdev structure &
3509 * disable the device
3510 */
3511 pci_save_state(pdev);
3512 pci_disable_device(pdev);
3513
3514 /* Move to Low power state*/
3515 pci_set_power_state(pdev, PCI_D3hot);
3516
3517 return rv;
3518}
3519
3520/*
3521 * Called for each probed device when the device is resumed.
3522 *
3523 * return value
3524 * 0 Success
3525 * <0 Error
3526 */
3527static int mtip_pci_resume(struct pci_dev *pdev)
3528{
3529 int rv = 0;
3530 struct driver_data *dd;
3531
3532 dd = pci_get_drvdata(pdev);
3533 if (!dd) {
3534 dev_err(&pdev->dev,
3535 "Driver private datastructure is NULL\n");
3536 return -EFAULT;
3537 }
3538
3539 /* Move the device to active State */
3540 pci_set_power_state(pdev, PCI_D0);
3541
3542 /* Restore PCI configuration space */
3543 pci_restore_state(pdev);
3544
3545 /* Enable the PCI device*/
3546 rv = pcim_enable_device(pdev);
3547 if (rv < 0) {
3548 dev_err(&pdev->dev,
3549 "Failed to enable card during resume\n");
3550 goto err;
3551 }
3552 pci_set_master(pdev);
3553
3554 /*
3555 * Calls hbaReset, initPort, & startPort function
3556 * then enables interrupts
3557 */
3558 rv = mtip_block_resume(dd);
3559 if (rv < 0)
3560 dev_err(&pdev->dev, "Unable to resume\n");
3561
3562err:
3563 atomic_set(&dd->resumeflag, false);
3564
3565 return rv;
3566}
3567
3568/*
3569 * Shutdown routine
3570 *
3571 * return value
3572 * None
3573 */
3574static void mtip_pci_shutdown(struct pci_dev *pdev)
3575{
3576 struct driver_data *dd = pci_get_drvdata(pdev);
3577 if (dd)
3578 mtip_block_shutdown(dd);
3579}
3580
3581/* Table of device ids supported by this driver. */
3582static DEFINE_PCI_DEVICE_TABLE(mtip_pci_tbl) = {
3583 { PCI_DEVICE(PCI_VENDOR_ID_MICRON, P320_DEVICE_ID) },
3584 { 0 }
3585};
3586
3587/* Structure that describes the PCI driver functions. */
3588static struct pci_driver mtip_pci_driver = {
3589 .name = MTIP_DRV_NAME,
3590 .id_table = mtip_pci_tbl,
3591 .probe = mtip_pci_probe,
3592 .remove = mtip_pci_remove,
3593 .suspend = mtip_pci_suspend,
3594 .resume = mtip_pci_resume,
3595 .shutdown = mtip_pci_shutdown,
3596};
3597
3598MODULE_DEVICE_TABLE(pci, mtip_pci_tbl);
3599
3600/*
3601 * Module initialization function.
3602 *
3603 * Called once when the module is loaded. This function allocates a major
3604 * block device number to the Cyclone devices and registers the PCI layer
3605 * of the driver.
3606 *
3607 * Return value
3608 * 0 on success else error code.
3609 */
3610static int __init mtip_init(void)
3611{
3612 printk(KERN_INFO MTIP_DRV_NAME " Version " MTIP_DRV_VERSION "\n");
3613
3614 /* Allocate a major block device number to use with this driver. */
3615 mtip_major = register_blkdev(0, MTIP_DRV_NAME);
3616 if (mtip_major < 0) {
3617 printk(KERN_ERR "Unable to register block device (%d)\n",
3618 mtip_major);
3619 return -EBUSY;
3620 }
3621
3622 /* Register our PCI operations. */
3623 return pci_register_driver(&mtip_pci_driver);
3624}
3625
3626/*
3627 * Module de-initialization function.
3628 *
3629 * Called once when the module is unloaded. This function deallocates
3630 * the major block device number allocated by mtip_init() and
3631 * unregisters the PCI layer of the driver.
3632 *
3633 * Return value
3634 * none
3635 */
3636static void __exit mtip_exit(void)
3637{
3638 /* Release the allocated major block device number. */
3639 unregister_blkdev(mtip_major, MTIP_DRV_NAME);
3640
3641 /* Unregister the PCI driver. */
3642 pci_unregister_driver(&mtip_pci_driver);
3643}
3644
3645MODULE_AUTHOR("Micron Technology, Inc");
3646MODULE_DESCRIPTION("Micron RealSSD PCIe Block Driver");
3647MODULE_LICENSE("GPL");
3648MODULE_VERSION(MTIP_DRV_VERSION);
3649
3650module_init(mtip_init);
3651module_exit(mtip_exit);
diff --git a/drivers/block/mtip32xx/mtip32xx.h b/drivers/block/mtip32xx/mtip32xx.h
new file mode 100644
index 000000000000..723d7c4946dc
--- /dev/null
+++ b/drivers/block/mtip32xx/mtip32xx.h
@@ -0,0 +1,423 @@
1/*
2 * mtip32xx.h - Header file for the P320 SSD Block Driver
3 * Copyright (C) 2011 Micron Technology, Inc.
4 *
5 * Portions of this code were derived from works subjected to the
6 * following copyright:
7 * Copyright (C) 2009 Integrated Device Technology, Inc.
8 *
9 * This program is free software; you can redistribute it and/or modify
10 * it under the terms of the GNU General Public License as published by
11 * the Free Software Foundation; either version 2 of the License, or
12 * (at your option) any later version.
13 *
14 * This program is distributed in the hope that it will be useful,
15 * but WITHOUT ANY WARRANTY; without even the implied warranty of
16 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17 * GNU General Public License for more details.
18 *
19 */
20
21#ifndef __MTIP32XX_H__
22#define __MTIP32XX_H__
23
24#include <linux/spinlock.h>
25#include <linux/rwsem.h>
26#include <linux/ata.h>
27#include <linux/interrupt.h>
28#include <linux/genhd.h>
29#include <linux/version.h>
30
31/* Offset of Subsystem Device ID in pci confoguration space */
32#define PCI_SUBSYSTEM_DEVICEID 0x2E
33
34/* offset of Device Control register in PCIe extended capabilites space */
35#define PCIE_CONFIG_EXT_DEVICE_CONTROL_OFFSET 0x48
36
37/* # of times to retry timed out IOs */
38#define MTIP_MAX_RETRIES 5
39
40/* Various timeout values in ms */
41#define MTIP_NCQ_COMMAND_TIMEOUT_MS 5000
42#define MTIP_IOCTL_COMMAND_TIMEOUT_MS 5000
43#define MTIP_INTERNAL_COMMAND_TIMEOUT_MS 5000
44
45/* check for timeouts every 500ms */
46#define MTIP_TIMEOUT_CHECK_PERIOD 500
47
48/* ftl rebuild */
49#define MTIP_FTL_REBUILD_OFFSET 142
50#define MTIP_FTL_REBUILD_MAGIC 0xED51
51#define MTIP_FTL_REBUILD_TIMEOUT_MS 2400000
52
53/* Macro to extract the tag bit number from a tag value. */
54#define MTIP_TAG_BIT(tag) (tag & 0x1F)
55
56/*
57 * Macro to extract the tag index from a tag value. The index
58 * is used to access the correct s_active/Command Issue register based
59 * on the tag value.
60 */
61#define MTIP_TAG_INDEX(tag) (tag >> 5)
62
63/*
64 * Maximum number of scatter gather entries
65 * a single command may have.
66 */
67#define MTIP_MAX_SG 128
68
69/*
70 * Maximum number of slot groups (Command Issue & s_active registers)
71 * NOTE: This is the driver maximum; check dd->slot_groups for actual value.
72 */
73#define MTIP_MAX_SLOT_GROUPS 8
74
75/* Internal command tag. */
76#define MTIP_TAG_INTERNAL 0
77
78/* Micron Vendor ID & P320x SSD Device ID */
79#define PCI_VENDOR_ID_MICRON 0x1344
80#define P320_DEVICE_ID 0x5150
81
82/* Driver name and version strings */
83#define MTIP_DRV_NAME "mtip32xx"
84#define MTIP_DRV_VERSION "1.2.6os3"
85
86/* Maximum number of minor device numbers per device. */
87#define MTIP_MAX_MINORS 16
88
89/* Maximum number of supported command slots. */
90#define MTIP_MAX_COMMAND_SLOTS (MTIP_MAX_SLOT_GROUPS * 32)
91
92/*
93 * Per-tag bitfield size in longs.
94 * Linux bit manipulation functions
95 * (i.e. test_and_set_bit, find_next_zero_bit)
96 * manipulate memory in longs, so we try to make the math work.
97 * take the slot groups and find the number of longs, rounding up.
98 * Careful! i386 and x86_64 use different size longs!
99 */
100#define U32_PER_LONG (sizeof(long) / sizeof(u32))
101#define SLOTBITS_IN_LONGS ((MTIP_MAX_SLOT_GROUPS + \
102 (U32_PER_LONG-1))/U32_PER_LONG)
103
104/* BAR number used to access the HBA registers. */
105#define MTIP_ABAR 5
106
107/* Forced Unit Access Bit */
108#define FUA_BIT 0x80
109
110#ifdef DEBUG
111 #define dbg_printk(format, arg...) \
112 printk(pr_fmt(format), ##arg);
113#else
114 #define dbg_printk(format, arg...)
115#endif
116
117#define __force_bit2int (unsigned int __force)
118
119/* below are bit numbers in 'flags' defined in mtip_port */
120#define MTIP_FLAG_IC_ACTIVE_BIT 0
121#define MTIP_FLAG_EH_ACTIVE_BIT 1
122#define MTIP_FLAG_SVC_THD_ACTIVE_BIT 2
123#define MTIP_FLAG_ISSUE_CMDS_BIT 4
124#define MTIP_FLAG_REBUILD_BIT 5
125#define MTIP_FLAG_SVC_THD_SHOULD_STOP_BIT 8
126
127/* Register Frame Information Structure (FIS), host to device. */
128struct host_to_dev_fis {
129 /*
130 * FIS type.
131 * - 27h Register FIS, host to device.
132 * - 34h Register FIS, device to host.
133 * - 39h DMA Activate FIS, device to host.
134 * - 41h DMA Setup FIS, bi-directional.
135 * - 46h Data FIS, bi-directional.
136 * - 58h BIST Activate FIS, bi-directional.
137 * - 5Fh PIO Setup FIS, device to host.
138 * - A1h Set Device Bits FIS, device to host.
139 */
140 unsigned char type;
141 unsigned char opts;
142 unsigned char command;
143 unsigned char features;
144
145 union {
146 unsigned char lba_low;
147 unsigned char sector;
148 };
149 union {
150 unsigned char lba_mid;
151 unsigned char cyl_low;
152 };
153 union {
154 unsigned char lba_hi;
155 unsigned char cyl_hi;
156 };
157 union {
158 unsigned char device;
159 unsigned char head;
160 };
161
162 union {
163 unsigned char lba_low_ex;
164 unsigned char sector_ex;
165 };
166 union {
167 unsigned char lba_mid_ex;
168 unsigned char cyl_low_ex;
169 };
170 union {
171 unsigned char lba_hi_ex;
172 unsigned char cyl_hi_ex;
173 };
174 unsigned char features_ex;
175
176 unsigned char sect_count;
177 unsigned char sect_cnt_ex;
178 unsigned char res2;
179 unsigned char control;
180
181 unsigned int res3;
182};
183
184/* Command header structure. */
185struct mtip_cmd_hdr {
186 /*
187 * Command options.
188 * - Bits 31:16 Number of PRD entries.
189 * - Bits 15:8 Unused in this implementation.
190 * - Bit 7 Prefetch bit, informs the drive to prefetch PRD entries.
191 * - Bit 6 Write bit, should be set when writing data to the device.
192 * - Bit 5 Unused in this implementation.
193 * - Bits 4:0 Length of the command FIS in DWords (DWord = 4 bytes).
194 */
195 unsigned int opts;
196 /* This field is unsed when using NCQ. */
197 union {
198 unsigned int byte_count;
199 unsigned int status;
200 };
201 /*
202 * Lower 32 bits of the command table address associated with this
203 * header. The command table addresses must be 128 byte aligned.
204 */
205 unsigned int ctba;
206 /*
207 * If 64 bit addressing is used this field is the upper 32 bits
208 * of the command table address associated with this command.
209 */
210 unsigned int ctbau;
211 /* Reserved and unused. */
212 unsigned int res[4];
213};
214
215/* Command scatter gather structure (PRD). */
216struct mtip_cmd_sg {
217 /*
218 * Low 32 bits of the data buffer address. For P320 this
219 * address must be 8 byte aligned signified by bits 2:0 being
220 * set to 0.
221 */
222 unsigned int dba;
223 /*
224 * When 64 bit addressing is used this field is the upper
225 * 32 bits of the data buffer address.
226 */
227 unsigned int dba_upper;
228 /* Unused. */
229 unsigned int reserved;
230 /*
231 * Bit 31: interrupt when this data block has been transferred.
232 * Bits 30..22: reserved
233 * Bits 21..0: byte count (minus 1). For P320 the byte count must be
234 * 8 byte aligned signified by bits 2:0 being set to 1.
235 */
236 unsigned int info;
237};
238struct mtip_port;
239
240/* Structure used to describe a command. */
241struct mtip_cmd {
242
243 struct mtip_cmd_hdr *command_header; /* ptr to command header entry */
244
245 dma_addr_t command_header_dma; /* corresponding physical address */
246
247 void *command; /* ptr to command table entry */
248
249 dma_addr_t command_dma; /* corresponding physical address */
250
251 void *comp_data; /* data passed to completion function comp_func() */
252 /*
253 * Completion function called by the ISR upon completion of
254 * a command.
255 */
256 void (*comp_func)(struct mtip_port *port,
257 int tag,
258 void *data,
259 int status);
260 /* Additional callback function that may be called by comp_func() */
261 void (*async_callback)(void *data, int status);
262
263 void *async_data; /* Addl. data passed to async_callback() */
264
265 int scatter_ents; /* Number of scatter list entries used */
266
267 struct scatterlist sg[MTIP_MAX_SG]; /* Scatter list entries */
268
269 int retries; /* The number of retries left for this command. */
270
271 int direction; /* Data transfer direction */
272
273 unsigned long comp_time; /* command completion time, in jiffies */
274
275 atomic_t active; /* declares if this command sent to the drive. */
276};
277
278/* Structure used to describe a port. */
279struct mtip_port {
280 /* Pointer back to the driver data for this port. */
281 struct driver_data *dd;
282 /*
283 * Used to determine if the data pointed to by the
284 * identify field is valid.
285 */
286 unsigned long identify_valid;
287 /* Base address of the memory mapped IO for the port. */
288 void __iomem *mmio;
289 /* Array of pointers to the memory mapped s_active registers. */
290 void __iomem *s_active[MTIP_MAX_SLOT_GROUPS];
291 /* Array of pointers to the memory mapped completed registers. */
292 void __iomem *completed[MTIP_MAX_SLOT_GROUPS];
293 /* Array of pointers to the memory mapped Command Issue registers. */
294 void __iomem *cmd_issue[MTIP_MAX_SLOT_GROUPS];
295 /*
296 * Pointer to the beginning of the command header memory as used
297 * by the driver.
298 */
299 void *command_list;
300 /*
301 * Pointer to the beginning of the command header memory as used
302 * by the DMA.
303 */
304 dma_addr_t command_list_dma;
305 /*
306 * Pointer to the beginning of the RX FIS memory as used
307 * by the driver.
308 */
309 void *rxfis;
310 /*
311 * Pointer to the beginning of the RX FIS memory as used
312 * by the DMA.
313 */
314 dma_addr_t rxfis_dma;
315 /*
316 * Pointer to the beginning of the command table memory as used
317 * by the driver.
318 */
319 void *command_table;
320 /*
321 * Pointer to the beginning of the command table memory as used
322 * by the DMA.
323 */
324 dma_addr_t command_tbl_dma;
325 /*
326 * Pointer to the beginning of the identify data memory as used
327 * by the driver.
328 */
329 u16 *identify;
330 /*
331 * Pointer to the beginning of the identify data memory as used
332 * by the DMA.
333 */
334 dma_addr_t identify_dma;
335 /*
336 * Pointer to the beginning of a sector buffer that is used
337 * by the driver when issuing internal commands.
338 */
339 u16 *sector_buffer;
340 /*
341 * Pointer to the beginning of a sector buffer that is used
342 * by the DMA when the driver issues internal commands.
343 */
344 dma_addr_t sector_buffer_dma;
345 /*
346 * Bit significant, used to determine if a command slot has
347 * been allocated. i.e. the slot is in use. Bits are cleared
348 * when the command slot and all associated data structures
349 * are no longer needed.
350 */
351 unsigned long allocated[SLOTBITS_IN_LONGS];
352 /*
353 * used to queue commands when an internal command is in progress
354 * or error handling is active
355 */
356 unsigned long cmds_to_issue[SLOTBITS_IN_LONGS];
357 /*
358 * Array of command slots. Structure includes pointers to the
359 * command header and command table, and completion function and data
360 * pointers.
361 */
362 struct mtip_cmd commands[MTIP_MAX_COMMAND_SLOTS];
363 /* Used by mtip_service_thread to wait for an event */
364 wait_queue_head_t svc_wait;
365 /*
366 * indicates the state of the port. Also, helps the service thread
367 * to determine its action on wake up.
368 */
369 unsigned long flags;
370 /*
371 * Timer used to complete commands that have been active for too long.
372 */
373 struct timer_list cmd_timer;
374 /*
375 * Semaphore used to block threads if there are no
376 * command slots available.
377 */
378 struct semaphore cmd_slot;
379 /* Spinlock for working around command-issue bug. */
380 spinlock_t cmd_issue_lock;
381};
382
383/*
384 * Driver private data structure.
385 *
386 * One structure is allocated per probed device.
387 */
388struct driver_data {
389 void __iomem *mmio; /* Base address of the HBA registers. */
390
391 int major; /* Major device number. */
392
393 int instance; /* Instance number. First device probed is 0, ... */
394
395 struct gendisk *disk; /* Pointer to our gendisk structure. */
396
397 struct pci_dev *pdev; /* Pointer to the PCI device structure. */
398
399 struct request_queue *queue; /* Our request queue. */
400
401 struct mtip_port *port; /* Pointer to the port data structure. */
402
403 /* Tasklet used to process the bottom half of the ISR. */
404 struct tasklet_struct tasklet;
405
406 unsigned product_type; /* magic value declaring the product type */
407
408 unsigned slot_groups; /* number of slot groups the product supports */
409
410 atomic_t drv_cleanup_done; /* Atomic variable for SRSI */
411
412 unsigned long index; /* Index to determine the disk name */
413
414 unsigned int ftlrebuildflag; /* FTL rebuild flag */
415
416 atomic_t resumeflag; /* Atomic variable to track suspend/resume */
417
418 atomic_t eh_active; /* Flag for error handling tracking */
419
420 struct task_struct *mtip_svc_handler; /* task_struct of svc thd */
421};
422
423#endif
diff --git a/drivers/block/nbd.c b/drivers/block/nbd.c
index f533f3375e24..c3f0ee16594d 100644
--- a/drivers/block/nbd.c
+++ b/drivers/block/nbd.c
@@ -127,8 +127,7 @@ static void sock_shutdown(struct nbd_device *lo, int lock)
127 if (lock) 127 if (lock)
128 mutex_lock(&lo->tx_lock); 128 mutex_lock(&lo->tx_lock);
129 if (lo->sock) { 129 if (lo->sock) {
130 printk(KERN_WARNING "%s: shutting down socket\n", 130 dev_warn(disk_to_dev(lo->disk), "shutting down socket\n");
131 lo->disk->disk_name);
132 kernel_sock_shutdown(lo->sock, SHUT_RDWR); 131 kernel_sock_shutdown(lo->sock, SHUT_RDWR);
133 lo->sock = NULL; 132 lo->sock = NULL;
134 } 133 }
@@ -158,8 +157,9 @@ static int sock_xmit(struct nbd_device *lo, int send, void *buf, int size,
158 sigset_t blocked, oldset; 157 sigset_t blocked, oldset;
159 158
160 if (unlikely(!sock)) { 159 if (unlikely(!sock)) {
161 printk(KERN_ERR "%s: Attempted %s on closed socket in sock_xmit\n", 160 dev_err(disk_to_dev(lo->disk),
162 lo->disk->disk_name, (send ? "send" : "recv")); 161 "Attempted %s on closed socket in sock_xmit\n",
162 (send ? "send" : "recv"));
163 return -EINVAL; 163 return -EINVAL;
164 } 164 }
165 165
@@ -250,8 +250,8 @@ static int nbd_send_req(struct nbd_device *lo, struct request *req)
250 result = sock_xmit(lo, 1, &request, sizeof(request), 250 result = sock_xmit(lo, 1, &request, sizeof(request),
251 (nbd_cmd(req) == NBD_CMD_WRITE) ? MSG_MORE : 0); 251 (nbd_cmd(req) == NBD_CMD_WRITE) ? MSG_MORE : 0);
252 if (result <= 0) { 252 if (result <= 0) {
253 printk(KERN_ERR "%s: Send control failed (result %d)\n", 253 dev_err(disk_to_dev(lo->disk),
254 lo->disk->disk_name, result); 254 "Send control failed (result %d)\n", result);
255 goto error_out; 255 goto error_out;
256 } 256 }
257 257
@@ -270,8 +270,9 @@ static int nbd_send_req(struct nbd_device *lo, struct request *req)
270 lo->disk->disk_name, req, bvec->bv_len); 270 lo->disk->disk_name, req, bvec->bv_len);
271 result = sock_send_bvec(lo, bvec, flags); 271 result = sock_send_bvec(lo, bvec, flags);
272 if (result <= 0) { 272 if (result <= 0) {
273 printk(KERN_ERR "%s: Send data failed (result %d)\n", 273 dev_err(disk_to_dev(lo->disk),
274 lo->disk->disk_name, result); 274 "Send data failed (result %d)\n",
275 result);
275 goto error_out; 276 goto error_out;
276 } 277 }
277 } 278 }
@@ -328,14 +329,13 @@ static struct request *nbd_read_stat(struct nbd_device *lo)
328 reply.magic = 0; 329 reply.magic = 0;
329 result = sock_xmit(lo, 0, &reply, sizeof(reply), MSG_WAITALL); 330 result = sock_xmit(lo, 0, &reply, sizeof(reply), MSG_WAITALL);
330 if (result <= 0) { 331 if (result <= 0) {
331 printk(KERN_ERR "%s: Receive control failed (result %d)\n", 332 dev_err(disk_to_dev(lo->disk),
332 lo->disk->disk_name, result); 333 "Receive control failed (result %d)\n", result);
333 goto harderror; 334 goto harderror;
334 } 335 }
335 336
336 if (ntohl(reply.magic) != NBD_REPLY_MAGIC) { 337 if (ntohl(reply.magic) != NBD_REPLY_MAGIC) {
337 printk(KERN_ERR "%s: Wrong magic (0x%lx)\n", 338 dev_err(disk_to_dev(lo->disk), "Wrong magic (0x%lx)\n",
338 lo->disk->disk_name,
339 (unsigned long)ntohl(reply.magic)); 339 (unsigned long)ntohl(reply.magic));
340 result = -EPROTO; 340 result = -EPROTO;
341 goto harderror; 341 goto harderror;
@@ -347,15 +347,15 @@ static struct request *nbd_read_stat(struct nbd_device *lo)
347 if (result != -ENOENT) 347 if (result != -ENOENT)
348 goto harderror; 348 goto harderror;
349 349
350 printk(KERN_ERR "%s: Unexpected reply (%p)\n", 350 dev_err(disk_to_dev(lo->disk), "Unexpected reply (%p)\n",
351 lo->disk->disk_name, reply.handle); 351 reply.handle);
352 result = -EBADR; 352 result = -EBADR;
353 goto harderror; 353 goto harderror;
354 } 354 }
355 355
356 if (ntohl(reply.error)) { 356 if (ntohl(reply.error)) {
357 printk(KERN_ERR "%s: Other side returned error (%d)\n", 357 dev_err(disk_to_dev(lo->disk), "Other side returned error (%d)\n",
358 lo->disk->disk_name, ntohl(reply.error)); 358 ntohl(reply.error));
359 req->errors++; 359 req->errors++;
360 return req; 360 return req;
361 } 361 }
@@ -369,8 +369,8 @@ static struct request *nbd_read_stat(struct nbd_device *lo)
369 rq_for_each_segment(bvec, req, iter) { 369 rq_for_each_segment(bvec, req, iter) {
370 result = sock_recv_bvec(lo, bvec); 370 result = sock_recv_bvec(lo, bvec);
371 if (result <= 0) { 371 if (result <= 0) {
372 printk(KERN_ERR "%s: Receive data failed (result %d)\n", 372 dev_err(disk_to_dev(lo->disk), "Receive data failed (result %d)\n",
373 lo->disk->disk_name, result); 373 result);
374 req->errors++; 374 req->errors++;
375 return req; 375 return req;
376 } 376 }
@@ -405,10 +405,10 @@ static int nbd_do_it(struct nbd_device *lo)
405 405
406 BUG_ON(lo->magic != LO_MAGIC); 406 BUG_ON(lo->magic != LO_MAGIC);
407 407
408 lo->pid = current->pid; 408 lo->pid = task_pid_nr(current);
409 ret = sysfs_create_file(&disk_to_dev(lo->disk)->kobj, &pid_attr.attr); 409 ret = device_create_file(disk_to_dev(lo->disk), &pid_attr);
410 if (ret) { 410 if (ret) {
411 printk(KERN_ERR "nbd: sysfs_create_file failed!"); 411 dev_err(disk_to_dev(lo->disk), "device_create_file failed!\n");
412 lo->pid = 0; 412 lo->pid = 0;
413 return ret; 413 return ret;
414 } 414 }
@@ -416,7 +416,7 @@ static int nbd_do_it(struct nbd_device *lo)
416 while ((req = nbd_read_stat(lo)) != NULL) 416 while ((req = nbd_read_stat(lo)) != NULL)
417 nbd_end_request(req); 417 nbd_end_request(req);
418 418
419 sysfs_remove_file(&disk_to_dev(lo->disk)->kobj, &pid_attr.attr); 419 device_remove_file(disk_to_dev(lo->disk), &pid_attr);
420 lo->pid = 0; 420 lo->pid = 0;
421 return 0; 421 return 0;
422} 422}
@@ -457,8 +457,8 @@ static void nbd_handle_req(struct nbd_device *lo, struct request *req)
457 if (rq_data_dir(req) == WRITE) { 457 if (rq_data_dir(req) == WRITE) {
458 nbd_cmd(req) = NBD_CMD_WRITE; 458 nbd_cmd(req) = NBD_CMD_WRITE;
459 if (lo->flags & NBD_READ_ONLY) { 459 if (lo->flags & NBD_READ_ONLY) {
460 printk(KERN_ERR "%s: Write on read-only\n", 460 dev_err(disk_to_dev(lo->disk),
461 lo->disk->disk_name); 461 "Write on read-only\n");
462 goto error_out; 462 goto error_out;
463 } 463 }
464 } 464 }
@@ -468,16 +468,15 @@ static void nbd_handle_req(struct nbd_device *lo, struct request *req)
468 mutex_lock(&lo->tx_lock); 468 mutex_lock(&lo->tx_lock);
469 if (unlikely(!lo->sock)) { 469 if (unlikely(!lo->sock)) {
470 mutex_unlock(&lo->tx_lock); 470 mutex_unlock(&lo->tx_lock);
471 printk(KERN_ERR "%s: Attempted send on closed socket\n", 471 dev_err(disk_to_dev(lo->disk),
472 lo->disk->disk_name); 472 "Attempted send on closed socket\n");
473 goto error_out; 473 goto error_out;
474 } 474 }
475 475
476 lo->active_req = req; 476 lo->active_req = req;
477 477
478 if (nbd_send_req(lo, req) != 0) { 478 if (nbd_send_req(lo, req) != 0) {
479 printk(KERN_ERR "%s: Request send failed\n", 479 dev_err(disk_to_dev(lo->disk), "Request send failed\n");
480 lo->disk->disk_name);
481 req->errors++; 480 req->errors++;
482 nbd_end_request(req); 481 nbd_end_request(req);
483 } else { 482 } else {
@@ -549,8 +548,8 @@ static void do_nbd_request(struct request_queue *q)
549 BUG_ON(lo->magic != LO_MAGIC); 548 BUG_ON(lo->magic != LO_MAGIC);
550 549
551 if (unlikely(!lo->sock)) { 550 if (unlikely(!lo->sock)) {
552 printk(KERN_ERR "%s: Attempted send on closed socket\n", 551 dev_err(disk_to_dev(lo->disk),
553 lo->disk->disk_name); 552 "Attempted send on closed socket\n");
554 req->errors++; 553 req->errors++;
555 nbd_end_request(req); 554 nbd_end_request(req);
556 spin_lock_irq(q->queue_lock); 555 spin_lock_irq(q->queue_lock);
@@ -576,7 +575,7 @@ static int __nbd_ioctl(struct block_device *bdev, struct nbd_device *lo,
576 case NBD_DISCONNECT: { 575 case NBD_DISCONNECT: {
577 struct request sreq; 576 struct request sreq;
578 577
579 printk(KERN_INFO "%s: NBD_DISCONNECT\n", lo->disk->disk_name); 578 dev_info(disk_to_dev(lo->disk), "NBD_DISCONNECT\n");
580 579
581 blk_rq_init(NULL, &sreq); 580 blk_rq_init(NULL, &sreq);
582 sreq.cmd_type = REQ_TYPE_SPECIAL; 581 sreq.cmd_type = REQ_TYPE_SPECIAL;
@@ -674,7 +673,7 @@ static int __nbd_ioctl(struct block_device *bdev, struct nbd_device *lo,
674 file = lo->file; 673 file = lo->file;
675 lo->file = NULL; 674 lo->file = NULL;
676 nbd_clear_que(lo); 675 nbd_clear_que(lo);
677 printk(KERN_WARNING "%s: queue cleared\n", lo->disk->disk_name); 676 dev_warn(disk_to_dev(lo->disk), "queue cleared\n");
678 if (file) 677 if (file)
679 fput(file); 678 fput(file);
680 lo->bytesize = 0; 679 lo->bytesize = 0;
@@ -694,8 +693,8 @@ static int __nbd_ioctl(struct block_device *bdev, struct nbd_device *lo,
694 return 0; 693 return 0;
695 694
696 case NBD_PRINT_DEBUG: 695 case NBD_PRINT_DEBUG:
697 printk(KERN_INFO "%s: next = %p, prev = %p, head = %p\n", 696 dev_info(disk_to_dev(lo->disk),
698 bdev->bd_disk->disk_name, 697 "next = %p, prev = %p, head = %p\n",
699 lo->queue_head.next, lo->queue_head.prev, 698 lo->queue_head.next, lo->queue_head.prev,
700 &lo->queue_head); 699 &lo->queue_head);
701 return 0; 700 return 0;
@@ -745,7 +744,7 @@ static int __init nbd_init(void)
745 BUILD_BUG_ON(sizeof(struct nbd_request) != 28); 744 BUILD_BUG_ON(sizeof(struct nbd_request) != 28);
746 745
747 if (max_part < 0) { 746 if (max_part < 0) {
748 printk(KERN_CRIT "nbd: max_part must be >= 0\n"); 747 printk(KERN_ERR "nbd: max_part must be >= 0\n");
749 return -EINVAL; 748 return -EINVAL;
750 } 749 }
751 750
diff --git a/drivers/block/paride/bpck6.c b/drivers/block/paride/bpck6.c
index ad124525ac23..ec64e7f5d1ce 100644
--- a/drivers/block/paride/bpck6.c
+++ b/drivers/block/paride/bpck6.c
@@ -20,9 +20,6 @@
20*/ 20*/
21 21
22 22
23/* PARAMETERS */
24static int verbose; /* set this to 1 to see debugging messages and whatnot */
25
26#define BACKPACK_VERSION "2.0.2" 23#define BACKPACK_VERSION "2.0.2"
27 24
28#include <linux/module.h> 25#include <linux/module.h>
@@ -36,6 +33,8 @@ static int verbose; /* set this to 1 to see debugging messages and whatnot */
36#include "ppc6lnx.c" 33#include "ppc6lnx.c"
37#include "paride.h" 34#include "paride.h"
38 35
36/* PARAMETERS */
37static bool verbose; /* set this to 1 to see debugging messages and whatnot */
39 38
40 39
41#define PPCSTRUCT(pi) ((Interface *)(pi->private)) 40#define PPCSTRUCT(pi) ((Interface *)(pi->private))
diff --git a/drivers/block/paride/pcd.c b/drivers/block/paride/pcd.c
index 46b8136c31bb..ba2b6b5e5910 100644
--- a/drivers/block/paride/pcd.c
+++ b/drivers/block/paride/pcd.c
@@ -144,7 +144,7 @@ enum {D_PRT, D_PRO, D_UNI, D_MOD, D_SLV, D_DLY};
144static DEFINE_MUTEX(pcd_mutex); 144static DEFINE_MUTEX(pcd_mutex);
145static DEFINE_SPINLOCK(pcd_lock); 145static DEFINE_SPINLOCK(pcd_lock);
146 146
147module_param(verbose, bool, 0644); 147module_param(verbose, int, 0644);
148module_param(major, int, 0); 148module_param(major, int, 0);
149module_param(name, charp, 0); 149module_param(name, charp, 0);
150module_param(nice, int, 0); 150module_param(nice, int, 0);
diff --git a/drivers/block/paride/pd.c b/drivers/block/paride/pd.c
index 869e7676d46f..831e3ac156e6 100644
--- a/drivers/block/paride/pd.c
+++ b/drivers/block/paride/pd.c
@@ -124,8 +124,9 @@
124 by default. 124 by default.
125 125
126*/ 126*/
127#include <linux/types.h>
127 128
128static int verbose = 0; 129static bool verbose = 0;
129static int major = PD_MAJOR; 130static int major = PD_MAJOR;
130static char *name = PD_NAME; 131static char *name = PD_NAME;
131static int cluster = 64; 132static int cluster = 64;
diff --git a/drivers/block/paride/pf.c b/drivers/block/paride/pf.c
index f21b520ef419..ec8f9ed6326e 100644
--- a/drivers/block/paride/pf.c
+++ b/drivers/block/paride/pf.c
@@ -118,13 +118,15 @@
118#define PF_NAME "pf" 118#define PF_NAME "pf"
119#define PF_UNITS 4 119#define PF_UNITS 4
120 120
121#include <linux/types.h>
122
121/* Here are things one can override from the insmod command. 123/* Here are things one can override from the insmod command.
122 Most are autoprobed by paride unless set here. Verbose is off 124 Most are autoprobed by paride unless set here. Verbose is off
123 by default. 125 by default.
124 126
125*/ 127*/
126 128
127static int verbose = 0; 129static bool verbose = 0;
128static int major = PF_MAJOR; 130static int major = PF_MAJOR;
129static char *name = PF_NAME; 131static char *name = PF_NAME;
130static int cluster = 64; 132static int cluster = 64;
diff --git a/drivers/block/paride/pg.c b/drivers/block/paride/pg.c
index 6b9a2000d56a..4a27b1de5fcb 100644
--- a/drivers/block/paride/pg.c
+++ b/drivers/block/paride/pg.c
@@ -130,13 +130,14 @@
130#define PI_PG 4 130#define PI_PG 4
131#endif 131#endif
132 132
133#include <linux/types.h>
133/* Here are things one can override from the insmod command. 134/* Here are things one can override from the insmod command.
134 Most are autoprobed by paride unless set here. Verbose is 0 135 Most are autoprobed by paride unless set here. Verbose is 0
135 by default. 136 by default.
136 137
137*/ 138*/
138 139
139static int verbose = 0; 140static bool verbose = 0;
140static int major = PG_MAJOR; 141static int major = PG_MAJOR;
141static char *name = PG_NAME; 142static char *name = PG_NAME;
142static int disable = 0; 143static int disable = 0;
@@ -630,6 +631,7 @@ static ssize_t pg_read(struct file *filp, char __user *buf, size_t count, loff_t
630 if (dev->status & 0x10) 631 if (dev->status & 0x10)
631 return -ETIME; 632 return -ETIME;
632 633
634 memset(&hdr, 0, sizeof(hdr));
633 hdr.magic = PG_MAGIC; 635 hdr.magic = PG_MAGIC;
634 hdr.dlen = dev->dlen; 636 hdr.dlen = dev->dlen;
635 copy = 0; 637 copy = 0;
diff --git a/drivers/block/paride/pt.c b/drivers/block/paride/pt.c
index 7179f79d7468..2596042eb987 100644
--- a/drivers/block/paride/pt.c
+++ b/drivers/block/paride/pt.c
@@ -109,13 +109,15 @@
109#define PT_NAME "pt" 109#define PT_NAME "pt"
110#define PT_UNITS 4 110#define PT_UNITS 4
111 111
112#include <linux/types.h>
113
112/* Here are things one can override from the insmod command. 114/* Here are things one can override from the insmod command.
113 Most are autoprobed by paride unless set here. Verbose is on 115 Most are autoprobed by paride unless set here. Verbose is on
114 by default. 116 by default.
115 117
116*/ 118*/
117 119
118static int verbose = 0; 120static bool verbose = 0;
119static int major = PT_MAJOR; 121static int major = PT_MAJOR;
120static char *name = PT_NAME; 122static char *name = PT_NAME;
121static int disable = 0; 123static int disable = 0;
diff --git a/drivers/block/pktcdvd.c b/drivers/block/pktcdvd.c
index e133f094ab08..d59edeabd93f 100644
--- a/drivers/block/pktcdvd.c
+++ b/drivers/block/pktcdvd.c
@@ -2444,7 +2444,7 @@ static void pkt_end_io_read_cloned(struct bio *bio, int err)
2444 pkt_bio_finished(pd); 2444 pkt_bio_finished(pd);
2445} 2445}
2446 2446
2447static int pkt_make_request(struct request_queue *q, struct bio *bio) 2447static void pkt_make_request(struct request_queue *q, struct bio *bio)
2448{ 2448{
2449 struct pktcdvd_device *pd; 2449 struct pktcdvd_device *pd;
2450 char b[BDEVNAME_SIZE]; 2450 char b[BDEVNAME_SIZE];
@@ -2473,7 +2473,7 @@ static int pkt_make_request(struct request_queue *q, struct bio *bio)
2473 cloned_bio->bi_end_io = pkt_end_io_read_cloned; 2473 cloned_bio->bi_end_io = pkt_end_io_read_cloned;
2474 pd->stats.secs_r += bio->bi_size >> 9; 2474 pd->stats.secs_r += bio->bi_size >> 9;
2475 pkt_queue_bio(pd, cloned_bio); 2475 pkt_queue_bio(pd, cloned_bio);
2476 return 0; 2476 return;
2477 } 2477 }
2478 2478
2479 if (!test_bit(PACKET_WRITABLE, &pd->flags)) { 2479 if (!test_bit(PACKET_WRITABLE, &pd->flags)) {
@@ -2509,7 +2509,7 @@ static int pkt_make_request(struct request_queue *q, struct bio *bio)
2509 pkt_make_request(q, &bp->bio1); 2509 pkt_make_request(q, &bp->bio1);
2510 pkt_make_request(q, &bp->bio2); 2510 pkt_make_request(q, &bp->bio2);
2511 bio_pair_release(bp); 2511 bio_pair_release(bp);
2512 return 0; 2512 return;
2513 } 2513 }
2514 } 2514 }
2515 2515
@@ -2533,7 +2533,7 @@ static int pkt_make_request(struct request_queue *q, struct bio *bio)
2533 } 2533 }
2534 spin_unlock(&pkt->lock); 2534 spin_unlock(&pkt->lock);
2535 spin_unlock(&pd->cdrw.active_list_lock); 2535 spin_unlock(&pd->cdrw.active_list_lock);
2536 return 0; 2536 return;
2537 } else { 2537 } else {
2538 blocked_bio = 1; 2538 blocked_bio = 1;
2539 } 2539 }
@@ -2584,10 +2584,9 @@ static int pkt_make_request(struct request_queue *q, struct bio *bio)
2584 */ 2584 */
2585 wake_up(&pd->wqueue); 2585 wake_up(&pd->wqueue);
2586 } 2586 }
2587 return 0; 2587 return;
2588end_io: 2588end_io:
2589 bio_io_error(bio); 2589 bio_io_error(bio);
2590 return 0;
2591} 2590}
2592 2591
2593 2592
@@ -2818,7 +2817,7 @@ static const struct block_device_operations pktcdvd_ops = {
2818 .check_events = pkt_check_events, 2817 .check_events = pkt_check_events,
2819}; 2818};
2820 2819
2821static char *pktcdvd_devnode(struct gendisk *gd, mode_t *mode) 2820static char *pktcdvd_devnode(struct gendisk *gd, umode_t *mode)
2822{ 2821{
2823 return kasprintf(GFP_KERNEL, "pktcdvd/%s", gd->disk_name); 2822 return kasprintf(GFP_KERNEL, "pktcdvd/%s", gd->disk_name);
2824} 2823}
diff --git a/drivers/block/ps3disk.c b/drivers/block/ps3disk.c
index 8e1ce2e2916a..da0abc1838c1 100644
--- a/drivers/block/ps3disk.c
+++ b/drivers/block/ps3disk.c
@@ -21,6 +21,7 @@
21#include <linux/ata.h> 21#include <linux/ata.h>
22#include <linux/blkdev.h> 22#include <linux/blkdev.h>
23#include <linux/slab.h> 23#include <linux/slab.h>
24#include <linux/module.h>
24 25
25#include <asm/lv1call.h> 26#include <asm/lv1call.h>
26#include <asm/ps3stor.h> 27#include <asm/ps3stor.h>
diff --git a/drivers/block/ps3vram.c b/drivers/block/ps3vram.c
index b3bdb8af89cf..f58cdcfb305f 100644
--- a/drivers/block/ps3vram.c
+++ b/drivers/block/ps3vram.c
@@ -10,6 +10,7 @@
10 10
11#include <linux/blkdev.h> 11#include <linux/blkdev.h>
12#include <linux/delay.h> 12#include <linux/delay.h>
13#include <linux/module.h>
13#include <linux/proc_fs.h> 14#include <linux/proc_fs.h>
14#include <linux/seq_file.h> 15#include <linux/seq_file.h>
15#include <linux/slab.h> 16#include <linux/slab.h>
@@ -596,7 +597,7 @@ out:
596 return next; 597 return next;
597} 598}
598 599
599static int ps3vram_make_request(struct request_queue *q, struct bio *bio) 600static void ps3vram_make_request(struct request_queue *q, struct bio *bio)
600{ 601{
601 struct ps3_system_bus_device *dev = q->queuedata; 602 struct ps3_system_bus_device *dev = q->queuedata;
602 struct ps3vram_priv *priv = ps3_system_bus_get_drvdata(dev); 603 struct ps3vram_priv *priv = ps3_system_bus_get_drvdata(dev);
@@ -610,13 +611,11 @@ static int ps3vram_make_request(struct request_queue *q, struct bio *bio)
610 spin_unlock_irq(&priv->lock); 611 spin_unlock_irq(&priv->lock);
611 612
612 if (busy) 613 if (busy)
613 return 0; 614 return;
614 615
615 do { 616 do {
616 bio = ps3vram_do_bio(dev, bio); 617 bio = ps3vram_do_bio(dev, bio);
617 } while (bio); 618 } while (bio);
618
619 return 0;
620} 619}
621 620
622static int __devinit ps3vram_probe(struct ps3_system_bus_device *dev) 621static int __devinit ps3vram_probe(struct ps3_system_bus_device *dev)
diff --git a/drivers/block/rbd.c b/drivers/block/rbd.c
index 15f65b5f3fc7..3fd31dec8c9c 100644
--- a/drivers/block/rbd.c
+++ b/drivers/block/rbd.c
@@ -183,16 +183,12 @@ static LIST_HEAD(rbd_client_list); /* clients */
183 183
184static int __rbd_init_snaps_header(struct rbd_device *rbd_dev); 184static int __rbd_init_snaps_header(struct rbd_device *rbd_dev);
185static void rbd_dev_release(struct device *dev); 185static void rbd_dev_release(struct device *dev);
186static ssize_t rbd_snap_rollback(struct device *dev,
187 struct device_attribute *attr,
188 const char *buf,
189 size_t size);
190static ssize_t rbd_snap_add(struct device *dev, 186static ssize_t rbd_snap_add(struct device *dev,
191 struct device_attribute *attr, 187 struct device_attribute *attr,
192 const char *buf, 188 const char *buf,
193 size_t count); 189 size_t count);
194static void __rbd_remove_snap_dev(struct rbd_device *rbd_dev, 190static void __rbd_remove_snap_dev(struct rbd_device *rbd_dev,
195 struct rbd_snap *snap);; 191 struct rbd_snap *snap);
196 192
197 193
198static struct rbd_device *dev_to_rbd(struct device *dev) 194static struct rbd_device *dev_to_rbd(struct device *dev)
@@ -260,7 +256,7 @@ static struct rbd_client *rbd_client_create(struct ceph_options *opt,
260 kref_init(&rbdc->kref); 256 kref_init(&rbdc->kref);
261 INIT_LIST_HEAD(&rbdc->node); 257 INIT_LIST_HEAD(&rbdc->node);
262 258
263 rbdc->client = ceph_create_client(opt, rbdc); 259 rbdc->client = ceph_create_client(opt, rbdc, 0, 0);
264 if (IS_ERR(rbdc->client)) 260 if (IS_ERR(rbdc->client))
265 goto out_rbdc; 261 goto out_rbdc;
266 opt = NULL; /* Now rbdc->client is responsible for opt */ 262 opt = NULL; /* Now rbdc->client is responsible for opt */
@@ -461,6 +457,10 @@ static int rbd_header_from_disk(struct rbd_image_header *header,
461 u32 snap_count = le32_to_cpu(ondisk->snap_count); 457 u32 snap_count = le32_to_cpu(ondisk->snap_count);
462 int ret = -ENOMEM; 458 int ret = -ENOMEM;
463 459
460 if (memcmp(ondisk, RBD_HEADER_TEXT, sizeof(RBD_HEADER_TEXT))) {
461 return -ENXIO;
462 }
463
464 init_rwsem(&header->snap_rwsem); 464 init_rwsem(&header->snap_rwsem);
465 header->snap_names_len = le64_to_cpu(ondisk->snap_names_len); 465 header->snap_names_len = le64_to_cpu(ondisk->snap_names_len);
466 header->snapc = kmalloc(sizeof(struct ceph_snap_context) + 466 header->snapc = kmalloc(sizeof(struct ceph_snap_context) +
@@ -1356,32 +1356,6 @@ fail:
1356} 1356}
1357 1357
1358/* 1358/*
1359 * Request sync osd rollback
1360 */
1361static int rbd_req_sync_rollback_obj(struct rbd_device *dev,
1362 u64 snapid,
1363 const char *obj)
1364{
1365 struct ceph_osd_req_op *ops;
1366 int ret = rbd_create_rw_ops(&ops, 1, CEPH_OSD_OP_ROLLBACK, 0);
1367 if (ret < 0)
1368 return ret;
1369
1370 ops[0].snap.snapid = snapid;
1371
1372 ret = rbd_req_sync_op(dev, NULL,
1373 CEPH_NOSNAP,
1374 0,
1375 CEPH_OSD_FLAG_WRITE | CEPH_OSD_FLAG_ONDISK,
1376 ops,
1377 1, obj, 0, 0, NULL, NULL, NULL);
1378
1379 rbd_destroy_ops(ops);
1380
1381 return ret;
1382}
1383
1384/*
1385 * Request sync osd read 1359 * Request sync osd read
1386 */ 1360 */
1387static int rbd_req_sync_exec(struct rbd_device *dev, 1361static int rbd_req_sync_exec(struct rbd_device *dev,
@@ -1610,8 +1584,13 @@ static int rbd_read_header(struct rbd_device *rbd_dev,
1610 goto out_dh; 1584 goto out_dh;
1611 1585
1612 rc = rbd_header_from_disk(header, dh, snap_count, GFP_KERNEL); 1586 rc = rbd_header_from_disk(header, dh, snap_count, GFP_KERNEL);
1613 if (rc < 0) 1587 if (rc < 0) {
1588 if (rc == -ENXIO) {
1589 pr_warning("unrecognized header format"
1590 " for image %s", rbd_dev->obj);
1591 }
1614 goto out_dh; 1592 goto out_dh;
1593 }
1615 1594
1616 if (snap_count != header->total_snaps) { 1595 if (snap_count != header->total_snaps) {
1617 snap_count = header->total_snaps; 1596 snap_count = header->total_snaps;
@@ -1882,7 +1861,6 @@ static DEVICE_ATTR(name, S_IRUGO, rbd_name_show, NULL);
1882static DEVICE_ATTR(refresh, S_IWUSR, NULL, rbd_image_refresh); 1861static DEVICE_ATTR(refresh, S_IWUSR, NULL, rbd_image_refresh);
1883static DEVICE_ATTR(current_snap, S_IRUGO, rbd_snap_show, NULL); 1862static DEVICE_ATTR(current_snap, S_IRUGO, rbd_snap_show, NULL);
1884static DEVICE_ATTR(create_snap, S_IWUSR, NULL, rbd_snap_add); 1863static DEVICE_ATTR(create_snap, S_IWUSR, NULL, rbd_snap_add);
1885static DEVICE_ATTR(rollback_snap, S_IWUSR, NULL, rbd_snap_rollback);
1886 1864
1887static struct attribute *rbd_attrs[] = { 1865static struct attribute *rbd_attrs[] = {
1888 &dev_attr_size.attr, 1866 &dev_attr_size.attr,
@@ -1893,7 +1871,6 @@ static struct attribute *rbd_attrs[] = {
1893 &dev_attr_current_snap.attr, 1871 &dev_attr_current_snap.attr,
1894 &dev_attr_refresh.attr, 1872 &dev_attr_refresh.attr,
1895 &dev_attr_create_snap.attr, 1873 &dev_attr_create_snap.attr,
1896 &dev_attr_rollback_snap.attr,
1897 NULL 1874 NULL
1898}; 1875};
1899 1876
@@ -2207,6 +2184,8 @@ static ssize_t rbd_add(struct bus_type *bus,
2207 INIT_LIST_HEAD(&rbd_dev->node); 2184 INIT_LIST_HEAD(&rbd_dev->node);
2208 INIT_LIST_HEAD(&rbd_dev->snaps); 2185 INIT_LIST_HEAD(&rbd_dev->snaps);
2209 2186
2187 init_rwsem(&rbd_dev->header.snap_rwsem);
2188
2210 /* generate unique id: find highest unique id, add one */ 2189 /* generate unique id: find highest unique id, add one */
2211 mutex_lock_nested(&ctl_mutex, SINGLE_DEPTH_NESTING); 2190 mutex_lock_nested(&ctl_mutex, SINGLE_DEPTH_NESTING);
2212 2191
@@ -2424,64 +2403,6 @@ err_unlock:
2424 return ret; 2403 return ret;
2425} 2404}
2426 2405
2427static ssize_t rbd_snap_rollback(struct device *dev,
2428 struct device_attribute *attr,
2429 const char *buf,
2430 size_t count)
2431{
2432 struct rbd_device *rbd_dev = dev_to_rbd(dev);
2433 int ret;
2434 u64 snapid;
2435 u64 cur_ofs;
2436 char *seg_name = NULL;
2437 char *snap_name = kmalloc(count + 1, GFP_KERNEL);
2438 ret = -ENOMEM;
2439 if (!snap_name)
2440 return ret;
2441
2442 /* parse snaps add command */
2443 snprintf(snap_name, count, "%s", buf);
2444 seg_name = kmalloc(RBD_MAX_SEG_NAME_LEN + 1, GFP_NOIO);
2445 if (!seg_name)
2446 goto done;
2447
2448 mutex_lock_nested(&ctl_mutex, SINGLE_DEPTH_NESTING);
2449
2450 ret = snap_by_name(&rbd_dev->header, snap_name, &snapid, NULL);
2451 if (ret < 0)
2452 goto done_unlock;
2453
2454 dout("snapid=%lld\n", snapid);
2455
2456 cur_ofs = 0;
2457 while (cur_ofs < rbd_dev->header.image_size) {
2458 cur_ofs += rbd_get_segment(&rbd_dev->header,
2459 rbd_dev->obj,
2460 cur_ofs, (u64)-1,
2461 seg_name, NULL);
2462 dout("seg_name=%s\n", seg_name);
2463
2464 ret = rbd_req_sync_rollback_obj(rbd_dev, snapid, seg_name);
2465 if (ret < 0)
2466 pr_warning("could not roll back obj %s err=%d\n",
2467 seg_name, ret);
2468 }
2469
2470 ret = __rbd_update_snaps(rbd_dev);
2471 if (ret < 0)
2472 goto done_unlock;
2473
2474 ret = count;
2475
2476done_unlock:
2477 mutex_unlock(&ctl_mutex);
2478done:
2479 kfree(seg_name);
2480 kfree(snap_name);
2481
2482 return ret;
2483}
2484
2485static struct bus_attribute rbd_bus_attrs[] = { 2406static struct bus_attribute rbd_bus_attrs[] = {
2486 __ATTR(add, S_IWUSR, NULL, rbd_add), 2407 __ATTR(add, S_IWUSR, NULL, rbd_add),
2487 __ATTR(remove, S_IWUSR, NULL, rbd_remove), 2408 __ATTR(remove, S_IWUSR, NULL, rbd_remove),
diff --git a/drivers/block/swim.c b/drivers/block/swim.c
index fd5adcd55944..6d5a914b9619 100644
--- a/drivers/block/swim.c
+++ b/drivers/block/swim.c
@@ -26,7 +26,6 @@
26#include <linux/delay.h> 26#include <linux/delay.h>
27#include <linux/platform_device.h> 27#include <linux/platform_device.h>
28 28
29#include <asm/macintosh.h>
30#include <asm/mac_via.h> 29#include <asm/mac_via.h>
31 30
32#define CARDNAME "swim" 31#define CARDNAME "swim"
diff --git a/drivers/block/swim3.c b/drivers/block/swim3.c
index ae3e167e17ad..89ddab127e33 100644
--- a/drivers/block/swim3.c
+++ b/drivers/block/swim3.c
@@ -16,6 +16,8 @@
16 * handle GCR disks 16 * handle GCR disks
17 */ 17 */
18 18
19#undef DEBUG
20
19#include <linux/stddef.h> 21#include <linux/stddef.h>
20#include <linux/kernel.h> 22#include <linux/kernel.h>
21#include <linux/sched.h> 23#include <linux/sched.h>
@@ -36,13 +38,11 @@
36#include <asm/machdep.h> 38#include <asm/machdep.h>
37#include <asm/pmac_feature.h> 39#include <asm/pmac_feature.h>
38 40
39static DEFINE_MUTEX(swim3_mutex);
40static struct request_queue *swim3_queue;
41static struct gendisk *disks[2];
42static struct request *fd_req;
43
44#define MAX_FLOPPIES 2 41#define MAX_FLOPPIES 2
45 42
43static DEFINE_MUTEX(swim3_mutex);
44static struct gendisk *disks[MAX_FLOPPIES];
45
46enum swim_state { 46enum swim_state {
47 idle, 47 idle,
48 locating, 48 locating,
@@ -177,7 +177,6 @@ struct swim3 {
177 177
178struct floppy_state { 178struct floppy_state {
179 enum swim_state state; 179 enum swim_state state;
180 spinlock_t lock;
181 struct swim3 __iomem *swim3; /* hardware registers */ 180 struct swim3 __iomem *swim3; /* hardware registers */
182 struct dbdma_regs __iomem *dma; /* DMA controller registers */ 181 struct dbdma_regs __iomem *dma; /* DMA controller registers */
183 int swim3_intr; /* interrupt number for SWIM3 */ 182 int swim3_intr; /* interrupt number for SWIM3 */
@@ -204,8 +203,20 @@ struct floppy_state {
204 int wanted; 203 int wanted;
205 struct macio_dev *mdev; 204 struct macio_dev *mdev;
206 char dbdma_cmd_space[5 * sizeof(struct dbdma_cmd)]; 205 char dbdma_cmd_space[5 * sizeof(struct dbdma_cmd)];
206 int index;
207 struct request *cur_req;
207}; 208};
208 209
210#define swim3_err(fmt, arg...) dev_err(&fs->mdev->ofdev.dev, "[fd%d] " fmt, fs->index, arg)
211#define swim3_warn(fmt, arg...) dev_warn(&fs->mdev->ofdev.dev, "[fd%d] " fmt, fs->index, arg)
212#define swim3_info(fmt, arg...) dev_info(&fs->mdev->ofdev.dev, "[fd%d] " fmt, fs->index, arg)
213
214#ifdef DEBUG
215#define swim3_dbg(fmt, arg...) dev_dbg(&fs->mdev->ofdev.dev, "[fd%d] " fmt, fs->index, arg)
216#else
217#define swim3_dbg(fmt, arg...) do { } while(0)
218#endif
219
209static struct floppy_state floppy_states[MAX_FLOPPIES]; 220static struct floppy_state floppy_states[MAX_FLOPPIES];
210static int floppy_count = 0; 221static int floppy_count = 0;
211static DEFINE_SPINLOCK(swim3_lock); 222static DEFINE_SPINLOCK(swim3_lock);
@@ -224,17 +235,8 @@ static unsigned short write_postamble[] = {
224 0, 0, 0, 0, 0, 0 235 0, 0, 0, 0, 0, 0
225}; 236};
226 237
227static void swim3_select(struct floppy_state *fs, int sel);
228static void swim3_action(struct floppy_state *fs, int action);
229static int swim3_readbit(struct floppy_state *fs, int bit);
230static void do_fd_request(struct request_queue * q);
231static void start_request(struct floppy_state *fs);
232static void set_timeout(struct floppy_state *fs, int nticks,
233 void (*proc)(unsigned long));
234static void scan_track(struct floppy_state *fs);
235static void seek_track(struct floppy_state *fs, int n); 238static void seek_track(struct floppy_state *fs, int n);
236static void init_dma(struct dbdma_cmd *cp, int cmd, void *buf, int count); 239static void init_dma(struct dbdma_cmd *cp, int cmd, void *buf, int count);
237static void setup_transfer(struct floppy_state *fs);
238static void act(struct floppy_state *fs); 240static void act(struct floppy_state *fs);
239static void scan_timeout(unsigned long data); 241static void scan_timeout(unsigned long data);
240static void seek_timeout(unsigned long data); 242static void seek_timeout(unsigned long data);
@@ -254,18 +256,21 @@ static unsigned int floppy_check_events(struct gendisk *disk,
254 unsigned int clearing); 256 unsigned int clearing);
255static int floppy_revalidate(struct gendisk *disk); 257static int floppy_revalidate(struct gendisk *disk);
256 258
257static bool swim3_end_request(int err, unsigned int nr_bytes) 259static bool swim3_end_request(struct floppy_state *fs, int err, unsigned int nr_bytes)
258{ 260{
259 if (__blk_end_request(fd_req, err, nr_bytes)) 261 struct request *req = fs->cur_req;
260 return true; 262 int rc;
261 263
262 fd_req = NULL; 264 swim3_dbg(" end request, err=%d nr_bytes=%d, cur_req=%p\n",
263 return false; 265 err, nr_bytes, req);
264}
265 266
266static bool swim3_end_request_cur(int err) 267 if (err)
267{ 268 nr_bytes = blk_rq_cur_bytes(req);
268 return swim3_end_request(err, blk_rq_cur_bytes(fd_req)); 269 rc = __blk_end_request(req, err, nr_bytes);
270 if (rc)
271 return true;
272 fs->cur_req = NULL;
273 return false;
269} 274}
270 275
271static void swim3_select(struct floppy_state *fs, int sel) 276static void swim3_select(struct floppy_state *fs, int sel)
@@ -303,50 +308,53 @@ static int swim3_readbit(struct floppy_state *fs, int bit)
303 return (stat & DATA) == 0; 308 return (stat & DATA) == 0;
304} 309}
305 310
306static void do_fd_request(struct request_queue * q)
307{
308 int i;
309
310 for(i=0; i<floppy_count; i++) {
311 struct floppy_state *fs = &floppy_states[i];
312 if (fs->mdev->media_bay &&
313 check_media_bay(fs->mdev->media_bay) != MB_FD)
314 continue;
315 start_request(fs);
316 }
317}
318
319static void start_request(struct floppy_state *fs) 311static void start_request(struct floppy_state *fs)
320{ 312{
321 struct request *req; 313 struct request *req;
322 unsigned long x; 314 unsigned long x;
323 315
316 swim3_dbg("start request, initial state=%d\n", fs->state);
317
324 if (fs->state == idle && fs->wanted) { 318 if (fs->state == idle && fs->wanted) {
325 fs->state = available; 319 fs->state = available;
326 wake_up(&fs->wait); 320 wake_up(&fs->wait);
327 return; 321 return;
328 } 322 }
329 while (fs->state == idle) { 323 while (fs->state == idle) {
330 if (!fd_req) { 324 swim3_dbg("start request, idle loop, cur_req=%p\n", fs->cur_req);
331 fd_req = blk_fetch_request(swim3_queue); 325 if (!fs->cur_req) {
332 if (!fd_req) 326 fs->cur_req = blk_fetch_request(disks[fs->index]->queue);
327 swim3_dbg(" fetched request %p\n", fs->cur_req);
328 if (!fs->cur_req)
333 break; 329 break;
334 } 330 }
335 req = fd_req; 331 req = fs->cur_req;
336#if 0 332
337 printk("do_fd_req: dev=%s cmd=%d sec=%ld nr_sec=%u buf=%p\n", 333 if (fs->mdev->media_bay &&
338 req->rq_disk->disk_name, req->cmd, 334 check_media_bay(fs->mdev->media_bay) != MB_FD) {
339 (long)blk_rq_pos(req), blk_rq_sectors(req), req->buffer); 335 swim3_dbg("%s", " media bay absent, dropping req\n");
340 printk(" errors=%d current_nr_sectors=%u\n", 336 swim3_end_request(fs, -ENODEV, 0);
341 req->errors, blk_rq_cur_sectors(req)); 337 continue;
338 }
339
340#if 0 /* This is really too verbose */
341 swim3_dbg("do_fd_req: dev=%s cmd=%d sec=%ld nr_sec=%u buf=%p\n",
342 req->rq_disk->disk_name, req->cmd,
343 (long)blk_rq_pos(req), blk_rq_sectors(req),
344 req->buffer);
345 swim3_dbg(" errors=%d current_nr_sectors=%u\n",
346 req->errors, blk_rq_cur_sectors(req));
342#endif 347#endif
343 348
344 if (blk_rq_pos(req) >= fs->total_secs) { 349 if (blk_rq_pos(req) >= fs->total_secs) {
345 swim3_end_request_cur(-EIO); 350 swim3_dbg(" pos out of bounds (%ld, max is %ld)\n",
351 (long)blk_rq_pos(req), (long)fs->total_secs);
352 swim3_end_request(fs, -EIO, 0);
346 continue; 353 continue;
347 } 354 }
348 if (fs->ejected) { 355 if (fs->ejected) {
349 swim3_end_request_cur(-EIO); 356 swim3_dbg("%s", " disk ejected\n");
357 swim3_end_request(fs, -EIO, 0);
350 continue; 358 continue;
351 } 359 }
352 360
@@ -354,7 +362,8 @@ static void start_request(struct floppy_state *fs)
354 if (fs->write_prot < 0) 362 if (fs->write_prot < 0)
355 fs->write_prot = swim3_readbit(fs, WRITE_PROT); 363 fs->write_prot = swim3_readbit(fs, WRITE_PROT);
356 if (fs->write_prot) { 364 if (fs->write_prot) {
357 swim3_end_request_cur(-EIO); 365 swim3_dbg("%s", " try to write, disk write protected\n");
366 swim3_end_request(fs, -EIO, 0);
358 continue; 367 continue;
359 } 368 }
360 } 369 }
@@ -369,7 +378,6 @@ static void start_request(struct floppy_state *fs)
369 x = ((long)blk_rq_pos(req)) % fs->secpercyl; 378 x = ((long)blk_rq_pos(req)) % fs->secpercyl;
370 fs->head = x / fs->secpertrack; 379 fs->head = x / fs->secpertrack;
371 fs->req_sector = x % fs->secpertrack + 1; 380 fs->req_sector = x % fs->secpertrack + 1;
372 fd_req = req;
373 fs->state = do_transfer; 381 fs->state = do_transfer;
374 fs->retries = 0; 382 fs->retries = 0;
375 383
@@ -377,12 +385,14 @@ static void start_request(struct floppy_state *fs)
377 } 385 }
378} 386}
379 387
388static void do_fd_request(struct request_queue * q)
389{
390 start_request(q->queuedata);
391}
392
380static void set_timeout(struct floppy_state *fs, int nticks, 393static void set_timeout(struct floppy_state *fs, int nticks,
381 void (*proc)(unsigned long)) 394 void (*proc)(unsigned long))
382{ 395{
383 unsigned long flags;
384
385 spin_lock_irqsave(&fs->lock, flags);
386 if (fs->timeout_pending) 396 if (fs->timeout_pending)
387 del_timer(&fs->timeout); 397 del_timer(&fs->timeout);
388 fs->timeout.expires = jiffies + nticks; 398 fs->timeout.expires = jiffies + nticks;
@@ -390,7 +400,6 @@ static void set_timeout(struct floppy_state *fs, int nticks,
390 fs->timeout.data = (unsigned long) fs; 400 fs->timeout.data = (unsigned long) fs;
391 add_timer(&fs->timeout); 401 add_timer(&fs->timeout);
392 fs->timeout_pending = 1; 402 fs->timeout_pending = 1;
393 spin_unlock_irqrestore(&fs->lock, flags);
394} 403}
395 404
396static inline void scan_track(struct floppy_state *fs) 405static inline void scan_track(struct floppy_state *fs)
@@ -442,40 +451,45 @@ static inline void setup_transfer(struct floppy_state *fs)
442 struct swim3 __iomem *sw = fs->swim3; 451 struct swim3 __iomem *sw = fs->swim3;
443 struct dbdma_cmd *cp = fs->dma_cmd; 452 struct dbdma_cmd *cp = fs->dma_cmd;
444 struct dbdma_regs __iomem *dr = fs->dma; 453 struct dbdma_regs __iomem *dr = fs->dma;
454 struct request *req = fs->cur_req;
445 455
446 if (blk_rq_cur_sectors(fd_req) <= 0) { 456 if (blk_rq_cur_sectors(req) <= 0) {
447 printk(KERN_ERR "swim3: transfer 0 sectors?\n"); 457 swim3_warn("%s", "Transfer 0 sectors ?\n");
448 return; 458 return;
449 } 459 }
450 if (rq_data_dir(fd_req) == WRITE) 460 if (rq_data_dir(req) == WRITE)
451 n = 1; 461 n = 1;
452 else { 462 else {
453 n = fs->secpertrack - fs->req_sector + 1; 463 n = fs->secpertrack - fs->req_sector + 1;
454 if (n > blk_rq_cur_sectors(fd_req)) 464 if (n > blk_rq_cur_sectors(req))
455 n = blk_rq_cur_sectors(fd_req); 465 n = blk_rq_cur_sectors(req);
456 } 466 }
467
468 swim3_dbg(" setup xfer at sect %d (of %d) head %d for %d\n",
469 fs->req_sector, fs->secpertrack, fs->head, n);
470
457 fs->scount = n; 471 fs->scount = n;
458 swim3_select(fs, fs->head? READ_DATA_1: READ_DATA_0); 472 swim3_select(fs, fs->head? READ_DATA_1: READ_DATA_0);
459 out_8(&sw->sector, fs->req_sector); 473 out_8(&sw->sector, fs->req_sector);
460 out_8(&sw->nsect, n); 474 out_8(&sw->nsect, n);
461 out_8(&sw->gap3, 0); 475 out_8(&sw->gap3, 0);
462 out_le32(&dr->cmdptr, virt_to_bus(cp)); 476 out_le32(&dr->cmdptr, virt_to_bus(cp));
463 if (rq_data_dir(fd_req) == WRITE) { 477 if (rq_data_dir(req) == WRITE) {
464 /* Set up 3 dma commands: write preamble, data, postamble */ 478 /* Set up 3 dma commands: write preamble, data, postamble */
465 init_dma(cp, OUTPUT_MORE, write_preamble, sizeof(write_preamble)); 479 init_dma(cp, OUTPUT_MORE, write_preamble, sizeof(write_preamble));
466 ++cp; 480 ++cp;
467 init_dma(cp, OUTPUT_MORE, fd_req->buffer, 512); 481 init_dma(cp, OUTPUT_MORE, req->buffer, 512);
468 ++cp; 482 ++cp;
469 init_dma(cp, OUTPUT_LAST, write_postamble, sizeof(write_postamble)); 483 init_dma(cp, OUTPUT_LAST, write_postamble, sizeof(write_postamble));
470 } else { 484 } else {
471 init_dma(cp, INPUT_LAST, fd_req->buffer, n * 512); 485 init_dma(cp, INPUT_LAST, req->buffer, n * 512);
472 } 486 }
473 ++cp; 487 ++cp;
474 out_le16(&cp->command, DBDMA_STOP); 488 out_le16(&cp->command, DBDMA_STOP);
475 out_8(&sw->control_bic, DO_ACTION | WRITE_SECTORS); 489 out_8(&sw->control_bic, DO_ACTION | WRITE_SECTORS);
476 in_8(&sw->error); 490 in_8(&sw->error);
477 out_8(&sw->control_bic, DO_ACTION | WRITE_SECTORS); 491 out_8(&sw->control_bic, DO_ACTION | WRITE_SECTORS);
478 if (rq_data_dir(fd_req) == WRITE) 492 if (rq_data_dir(req) == WRITE)
479 out_8(&sw->control_bis, WRITE_SECTORS); 493 out_8(&sw->control_bis, WRITE_SECTORS);
480 in_8(&sw->intr); 494 in_8(&sw->intr);
481 out_le32(&dr->control, (RUN << 16) | RUN); 495 out_le32(&dr->control, (RUN << 16) | RUN);
@@ -488,12 +502,16 @@ static inline void setup_transfer(struct floppy_state *fs)
488static void act(struct floppy_state *fs) 502static void act(struct floppy_state *fs)
489{ 503{
490 for (;;) { 504 for (;;) {
505 swim3_dbg(" act loop, state=%d, req_cyl=%d, cur_cyl=%d\n",
506 fs->state, fs->req_cyl, fs->cur_cyl);
507
491 switch (fs->state) { 508 switch (fs->state) {
492 case idle: 509 case idle:
493 return; /* XXX shouldn't get here */ 510 return; /* XXX shouldn't get here */
494 511
495 case locating: 512 case locating:
496 if (swim3_readbit(fs, TRACK_ZERO)) { 513 if (swim3_readbit(fs, TRACK_ZERO)) {
514 swim3_dbg("%s", " locate track 0\n");
497 fs->cur_cyl = 0; 515 fs->cur_cyl = 0;
498 if (fs->req_cyl == 0) 516 if (fs->req_cyl == 0)
499 fs->state = do_transfer; 517 fs->state = do_transfer;
@@ -511,7 +529,7 @@ static void act(struct floppy_state *fs)
511 break; 529 break;
512 } 530 }
513 if (fs->req_cyl == fs->cur_cyl) { 531 if (fs->req_cyl == fs->cur_cyl) {
514 printk("whoops, seeking 0\n"); 532 swim3_warn("%s", "Whoops, seeking 0\n");
515 fs->state = do_transfer; 533 fs->state = do_transfer;
516 break; 534 break;
517 } 535 }
@@ -527,7 +545,9 @@ static void act(struct floppy_state *fs)
527 case do_transfer: 545 case do_transfer:
528 if (fs->cur_cyl != fs->req_cyl) { 546 if (fs->cur_cyl != fs->req_cyl) {
529 if (fs->retries > 5) { 547 if (fs->retries > 5) {
530 swim3_end_request_cur(-EIO); 548 swim3_err("Wrong cylinder in transfer, want: %d got %d\n",
549 fs->req_cyl, fs->cur_cyl);
550 swim3_end_request(fs, -EIO, 0);
531 fs->state = idle; 551 fs->state = idle;
532 return; 552 return;
533 } 553 }
@@ -542,7 +562,7 @@ static void act(struct floppy_state *fs)
542 return; 562 return;
543 563
544 default: 564 default:
545 printk(KERN_ERR"swim3: unknown state %d\n", fs->state); 565 swim3_err("Unknown state %d\n", fs->state);
546 return; 566 return;
547 } 567 }
548 } 568 }
@@ -552,59 +572,75 @@ static void scan_timeout(unsigned long data)
552{ 572{
553 struct floppy_state *fs = (struct floppy_state *) data; 573 struct floppy_state *fs = (struct floppy_state *) data;
554 struct swim3 __iomem *sw = fs->swim3; 574 struct swim3 __iomem *sw = fs->swim3;
575 unsigned long flags;
576
577 swim3_dbg("* scan timeout, state=%d\n", fs->state);
555 578
579 spin_lock_irqsave(&swim3_lock, flags);
556 fs->timeout_pending = 0; 580 fs->timeout_pending = 0;
557 out_8(&sw->control_bic, DO_ACTION | WRITE_SECTORS); 581 out_8(&sw->control_bic, DO_ACTION | WRITE_SECTORS);
558 out_8(&sw->select, RELAX); 582 out_8(&sw->select, RELAX);
559 out_8(&sw->intr_enable, 0); 583 out_8(&sw->intr_enable, 0);
560 fs->cur_cyl = -1; 584 fs->cur_cyl = -1;
561 if (fs->retries > 5) { 585 if (fs->retries > 5) {
562 swim3_end_request_cur(-EIO); 586 swim3_end_request(fs, -EIO, 0);
563 fs->state = idle; 587 fs->state = idle;
564 start_request(fs); 588 start_request(fs);
565 } else { 589 } else {
566 fs->state = jogging; 590 fs->state = jogging;
567 act(fs); 591 act(fs);
568 } 592 }
593 spin_unlock_irqrestore(&swim3_lock, flags);
569} 594}
570 595
571static void seek_timeout(unsigned long data) 596static void seek_timeout(unsigned long data)
572{ 597{
573 struct floppy_state *fs = (struct floppy_state *) data; 598 struct floppy_state *fs = (struct floppy_state *) data;
574 struct swim3 __iomem *sw = fs->swim3; 599 struct swim3 __iomem *sw = fs->swim3;
600 unsigned long flags;
601
602 swim3_dbg("* seek timeout, state=%d\n", fs->state);
575 603
604 spin_lock_irqsave(&swim3_lock, flags);
576 fs->timeout_pending = 0; 605 fs->timeout_pending = 0;
577 out_8(&sw->control_bic, DO_SEEK); 606 out_8(&sw->control_bic, DO_SEEK);
578 out_8(&sw->select, RELAX); 607 out_8(&sw->select, RELAX);
579 out_8(&sw->intr_enable, 0); 608 out_8(&sw->intr_enable, 0);
580 printk(KERN_ERR "swim3: seek timeout\n"); 609 swim3_err("%s", "Seek timeout\n");
581 swim3_end_request_cur(-EIO); 610 swim3_end_request(fs, -EIO, 0);
582 fs->state = idle; 611 fs->state = idle;
583 start_request(fs); 612 start_request(fs);
613 spin_unlock_irqrestore(&swim3_lock, flags);
584} 614}
585 615
586static void settle_timeout(unsigned long data) 616static void settle_timeout(unsigned long data)
587{ 617{
588 struct floppy_state *fs = (struct floppy_state *) data; 618 struct floppy_state *fs = (struct floppy_state *) data;
589 struct swim3 __iomem *sw = fs->swim3; 619 struct swim3 __iomem *sw = fs->swim3;
620 unsigned long flags;
621
622 swim3_dbg("* settle timeout, state=%d\n", fs->state);
590 623
624 spin_lock_irqsave(&swim3_lock, flags);
591 fs->timeout_pending = 0; 625 fs->timeout_pending = 0;
592 if (swim3_readbit(fs, SEEK_COMPLETE)) { 626 if (swim3_readbit(fs, SEEK_COMPLETE)) {
593 out_8(&sw->select, RELAX); 627 out_8(&sw->select, RELAX);
594 fs->state = locating; 628 fs->state = locating;
595 act(fs); 629 act(fs);
596 return; 630 goto unlock;
597 } 631 }
598 out_8(&sw->select, RELAX); 632 out_8(&sw->select, RELAX);
599 if (fs->settle_time < 2*HZ) { 633 if (fs->settle_time < 2*HZ) {
600 ++fs->settle_time; 634 ++fs->settle_time;
601 set_timeout(fs, 1, settle_timeout); 635 set_timeout(fs, 1, settle_timeout);
602 return; 636 goto unlock;
603 } 637 }
604 printk(KERN_ERR "swim3: seek settle timeout\n"); 638 swim3_err("%s", "Seek settle timeout\n");
605 swim3_end_request_cur(-EIO); 639 swim3_end_request(fs, -EIO, 0);
606 fs->state = idle; 640 fs->state = idle;
607 start_request(fs); 641 start_request(fs);
642 unlock:
643 spin_unlock_irqrestore(&swim3_lock, flags);
608} 644}
609 645
610static void xfer_timeout(unsigned long data) 646static void xfer_timeout(unsigned long data)
@@ -612,8 +648,12 @@ static void xfer_timeout(unsigned long data)
612 struct floppy_state *fs = (struct floppy_state *) data; 648 struct floppy_state *fs = (struct floppy_state *) data;
613 struct swim3 __iomem *sw = fs->swim3; 649 struct swim3 __iomem *sw = fs->swim3;
614 struct dbdma_regs __iomem *dr = fs->dma; 650 struct dbdma_regs __iomem *dr = fs->dma;
651 unsigned long flags;
615 int n; 652 int n;
616 653
654 swim3_dbg("* xfer timeout, state=%d\n", fs->state);
655
656 spin_lock_irqsave(&swim3_lock, flags);
617 fs->timeout_pending = 0; 657 fs->timeout_pending = 0;
618 out_le32(&dr->control, RUN << 16); 658 out_le32(&dr->control, RUN << 16);
619 /* We must wait a bit for dbdma to stop */ 659 /* We must wait a bit for dbdma to stop */
@@ -622,12 +662,13 @@ static void xfer_timeout(unsigned long data)
622 out_8(&sw->intr_enable, 0); 662 out_8(&sw->intr_enable, 0);
623 out_8(&sw->control_bic, WRITE_SECTORS | DO_ACTION); 663 out_8(&sw->control_bic, WRITE_SECTORS | DO_ACTION);
624 out_8(&sw->select, RELAX); 664 out_8(&sw->select, RELAX);
625 printk(KERN_ERR "swim3: timeout %sing sector %ld\n", 665 swim3_err("Timeout %sing sector %ld\n",
626 (rq_data_dir(fd_req)==WRITE? "writ": "read"), 666 (rq_data_dir(fs->cur_req)==WRITE? "writ": "read"),
627 (long)blk_rq_pos(fd_req)); 667 (long)blk_rq_pos(fs->cur_req));
628 swim3_end_request_cur(-EIO); 668 swim3_end_request(fs, -EIO, 0);
629 fs->state = idle; 669 fs->state = idle;
630 start_request(fs); 670 start_request(fs);
671 spin_unlock_irqrestore(&swim3_lock, flags);
631} 672}
632 673
633static irqreturn_t swim3_interrupt(int irq, void *dev_id) 674static irqreturn_t swim3_interrupt(int irq, void *dev_id)
@@ -638,12 +679,17 @@ static irqreturn_t swim3_interrupt(int irq, void *dev_id)
638 int stat, resid; 679 int stat, resid;
639 struct dbdma_regs __iomem *dr; 680 struct dbdma_regs __iomem *dr;
640 struct dbdma_cmd *cp; 681 struct dbdma_cmd *cp;
682 unsigned long flags;
683 struct request *req = fs->cur_req;
684
685 swim3_dbg("* interrupt, state=%d\n", fs->state);
641 686
687 spin_lock_irqsave(&swim3_lock, flags);
642 intr = in_8(&sw->intr); 688 intr = in_8(&sw->intr);
643 err = (intr & ERROR_INTR)? in_8(&sw->error): 0; 689 err = (intr & ERROR_INTR)? in_8(&sw->error): 0;
644 if ((intr & ERROR_INTR) && fs->state != do_transfer) 690 if ((intr & ERROR_INTR) && fs->state != do_transfer)
645 printk(KERN_ERR "swim3_interrupt, state=%d, dir=%x, intr=%x, err=%x\n", 691 swim3_err("Non-transfer error interrupt: state=%d, dir=%x, intr=%x, err=%x\n",
646 fs->state, rq_data_dir(fd_req), intr, err); 692 fs->state, rq_data_dir(req), intr, err);
647 switch (fs->state) { 693 switch (fs->state) {
648 case locating: 694 case locating:
649 if (intr & SEEN_SECTOR) { 695 if (intr & SEEN_SECTOR) {
@@ -653,10 +699,10 @@ static irqreturn_t swim3_interrupt(int irq, void *dev_id)
653 del_timer(&fs->timeout); 699 del_timer(&fs->timeout);
654 fs->timeout_pending = 0; 700 fs->timeout_pending = 0;
655 if (sw->ctrack == 0xff) { 701 if (sw->ctrack == 0xff) {
656 printk(KERN_ERR "swim3: seen sector but cyl=ff?\n"); 702 swim3_err("%s", "Seen sector but cyl=ff?\n");
657 fs->cur_cyl = -1; 703 fs->cur_cyl = -1;
658 if (fs->retries > 5) { 704 if (fs->retries > 5) {
659 swim3_end_request_cur(-EIO); 705 swim3_end_request(fs, -EIO, 0);
660 fs->state = idle; 706 fs->state = idle;
661 start_request(fs); 707 start_request(fs);
662 } else { 708 } else {
@@ -668,8 +714,8 @@ static irqreturn_t swim3_interrupt(int irq, void *dev_id)
668 fs->cur_cyl = sw->ctrack; 714 fs->cur_cyl = sw->ctrack;
669 fs->cur_sector = sw->csect; 715 fs->cur_sector = sw->csect;
670 if (fs->expect_cyl != -1 && fs->expect_cyl != fs->cur_cyl) 716 if (fs->expect_cyl != -1 && fs->expect_cyl != fs->cur_cyl)
671 printk(KERN_ERR "swim3: expected cyl %d, got %d\n", 717 swim3_err("Expected cyl %d, got %d\n",
672 fs->expect_cyl, fs->cur_cyl); 718 fs->expect_cyl, fs->cur_cyl);
673 fs->state = do_transfer; 719 fs->state = do_transfer;
674 act(fs); 720 act(fs);
675 } 721 }
@@ -704,7 +750,7 @@ static irqreturn_t swim3_interrupt(int irq, void *dev_id)
704 fs->timeout_pending = 0; 750 fs->timeout_pending = 0;
705 dr = fs->dma; 751 dr = fs->dma;
706 cp = fs->dma_cmd; 752 cp = fs->dma_cmd;
707 if (rq_data_dir(fd_req) == WRITE) 753 if (rq_data_dir(req) == WRITE)
708 ++cp; 754 ++cp;
709 /* 755 /*
710 * Check that the main data transfer has finished. 756 * Check that the main data transfer has finished.
@@ -729,31 +775,32 @@ static irqreturn_t swim3_interrupt(int irq, void *dev_id)
729 if (intr & ERROR_INTR) { 775 if (intr & ERROR_INTR) {
730 n = fs->scount - 1 - resid / 512; 776 n = fs->scount - 1 - resid / 512;
731 if (n > 0) { 777 if (n > 0) {
732 blk_update_request(fd_req, 0, n << 9); 778 blk_update_request(req, 0, n << 9);
733 fs->req_sector += n; 779 fs->req_sector += n;
734 } 780 }
735 if (fs->retries < 5) { 781 if (fs->retries < 5) {
736 ++fs->retries; 782 ++fs->retries;
737 act(fs); 783 act(fs);
738 } else { 784 } else {
739 printk("swim3: error %sing block %ld (err=%x)\n", 785 swim3_err("Error %sing block %ld (err=%x)\n",
740 rq_data_dir(fd_req) == WRITE? "writ": "read", 786 rq_data_dir(req) == WRITE? "writ": "read",
741 (long)blk_rq_pos(fd_req), err); 787 (long)blk_rq_pos(req), err);
742 swim3_end_request_cur(-EIO); 788 swim3_end_request(fs, -EIO, 0);
743 fs->state = idle; 789 fs->state = idle;
744 } 790 }
745 } else { 791 } else {
746 if ((stat & ACTIVE) == 0 || resid != 0) { 792 if ((stat & ACTIVE) == 0 || resid != 0) {
747 /* musta been an error */ 793 /* musta been an error */
748 printk(KERN_ERR "swim3: fd dma: stat=%x resid=%d\n", stat, resid); 794 swim3_err("fd dma error: stat=%x resid=%d\n", stat, resid);
749 printk(KERN_ERR " state=%d, dir=%x, intr=%x, err=%x\n", 795 swim3_err(" state=%d, dir=%x, intr=%x, err=%x\n",
750 fs->state, rq_data_dir(fd_req), intr, err); 796 fs->state, rq_data_dir(req), intr, err);
751 swim3_end_request_cur(-EIO); 797 swim3_end_request(fs, -EIO, 0);
752 fs->state = idle; 798 fs->state = idle;
753 start_request(fs); 799 start_request(fs);
754 break; 800 break;
755 } 801 }
756 if (swim3_end_request(0, fs->scount << 9)) { 802 fs->retries = 0;
803 if (swim3_end_request(fs, 0, fs->scount << 9)) {
757 fs->req_sector += fs->scount; 804 fs->req_sector += fs->scount;
758 if (fs->req_sector > fs->secpertrack) { 805 if (fs->req_sector > fs->secpertrack) {
759 fs->req_sector -= fs->secpertrack; 806 fs->req_sector -= fs->secpertrack;
@@ -770,8 +817,9 @@ static irqreturn_t swim3_interrupt(int irq, void *dev_id)
770 start_request(fs); 817 start_request(fs);
771 break; 818 break;
772 default: 819 default:
773 printk(KERN_ERR "swim3: don't know what to do in state %d\n", fs->state); 820 swim3_err("Don't know what to do in state %d\n", fs->state);
774 } 821 }
822 spin_unlock_irqrestore(&swim3_lock, flags);
775 return IRQ_HANDLED; 823 return IRQ_HANDLED;
776} 824}
777 825
@@ -781,26 +829,31 @@ static void fd_dma_interrupt(int irq, void *dev_id)
781} 829}
782*/ 830*/
783 831
832/* Called under the mutex to grab exclusive access to a drive */
784static int grab_drive(struct floppy_state *fs, enum swim_state state, 833static int grab_drive(struct floppy_state *fs, enum swim_state state,
785 int interruptible) 834 int interruptible)
786{ 835{
787 unsigned long flags; 836 unsigned long flags;
788 837
789 spin_lock_irqsave(&fs->lock, flags); 838 swim3_dbg("%s", "-> grab drive\n");
790 if (fs->state != idle) { 839
840 spin_lock_irqsave(&swim3_lock, flags);
841 if (fs->state != idle && fs->state != available) {
791 ++fs->wanted; 842 ++fs->wanted;
792 while (fs->state != available) { 843 while (fs->state != available) {
844 spin_unlock_irqrestore(&swim3_lock, flags);
793 if (interruptible && signal_pending(current)) { 845 if (interruptible && signal_pending(current)) {
794 --fs->wanted; 846 --fs->wanted;
795 spin_unlock_irqrestore(&fs->lock, flags);
796 return -EINTR; 847 return -EINTR;
797 } 848 }
798 interruptible_sleep_on(&fs->wait); 849 interruptible_sleep_on(&fs->wait);
850 spin_lock_irqsave(&swim3_lock, flags);
799 } 851 }
800 --fs->wanted; 852 --fs->wanted;
801 } 853 }
802 fs->state = state; 854 fs->state = state;
803 spin_unlock_irqrestore(&fs->lock, flags); 855 spin_unlock_irqrestore(&swim3_lock, flags);
856
804 return 0; 857 return 0;
805} 858}
806 859
@@ -808,10 +861,12 @@ static void release_drive(struct floppy_state *fs)
808{ 861{
809 unsigned long flags; 862 unsigned long flags;
810 863
811 spin_lock_irqsave(&fs->lock, flags); 864 swim3_dbg("%s", "-> release drive\n");
865
866 spin_lock_irqsave(&swim3_lock, flags);
812 fs->state = idle; 867 fs->state = idle;
813 start_request(fs); 868 start_request(fs);
814 spin_unlock_irqrestore(&fs->lock, flags); 869 spin_unlock_irqrestore(&swim3_lock, flags);
815} 870}
816 871
817static int fd_eject(struct floppy_state *fs) 872static int fd_eject(struct floppy_state *fs)
@@ -966,6 +1021,7 @@ static int floppy_release(struct gendisk *disk, fmode_t mode)
966{ 1021{
967 struct floppy_state *fs = disk->private_data; 1022 struct floppy_state *fs = disk->private_data;
968 struct swim3 __iomem *sw = fs->swim3; 1023 struct swim3 __iomem *sw = fs->swim3;
1024
969 mutex_lock(&swim3_mutex); 1025 mutex_lock(&swim3_mutex);
970 if (fs->ref_count > 0 && --fs->ref_count == 0) { 1026 if (fs->ref_count > 0 && --fs->ref_count == 0) {
971 swim3_action(fs, MOTOR_OFF); 1027 swim3_action(fs, MOTOR_OFF);
@@ -1031,30 +1087,48 @@ static const struct block_device_operations floppy_fops = {
1031 .revalidate_disk= floppy_revalidate, 1087 .revalidate_disk= floppy_revalidate,
1032}; 1088};
1033 1089
1090static void swim3_mb_event(struct macio_dev* mdev, int mb_state)
1091{
1092 struct floppy_state *fs = macio_get_drvdata(mdev);
1093 struct swim3 __iomem *sw = fs->swim3;
1094
1095 if (!fs)
1096 return;
1097 if (mb_state != MB_FD)
1098 return;
1099
1100 /* Clear state */
1101 out_8(&sw->intr_enable, 0);
1102 in_8(&sw->intr);
1103 in_8(&sw->error);
1104}
1105
1034static int swim3_add_device(struct macio_dev *mdev, int index) 1106static int swim3_add_device(struct macio_dev *mdev, int index)
1035{ 1107{
1036 struct device_node *swim = mdev->ofdev.dev.of_node; 1108 struct device_node *swim = mdev->ofdev.dev.of_node;
1037 struct floppy_state *fs = &floppy_states[index]; 1109 struct floppy_state *fs = &floppy_states[index];
1038 int rc = -EBUSY; 1110 int rc = -EBUSY;
1039 1111
1112 /* Do this first for message macros */
1113 memset(fs, 0, sizeof(*fs));
1114 fs->mdev = mdev;
1115 fs->index = index;
1116
1040 /* Check & Request resources */ 1117 /* Check & Request resources */
1041 if (macio_resource_count(mdev) < 2) { 1118 if (macio_resource_count(mdev) < 2) {
1042 printk(KERN_WARNING "ifd%d: no address for %s\n", 1119 swim3_err("%s", "No address in device-tree\n");
1043 index, swim->full_name);
1044 return -ENXIO; 1120 return -ENXIO;
1045 } 1121 }
1046 if (macio_irq_count(mdev) < 2) { 1122 if (macio_irq_count(mdev) < 1) {
1047 printk(KERN_WARNING "fd%d: no intrs for device %s\n", 1123 swim3_err("%s", "No interrupt in device-tree\n");
1048 index, swim->full_name); 1124 return -ENXIO;
1049 } 1125 }
1050 if (macio_request_resource(mdev, 0, "swim3 (mmio)")) { 1126 if (macio_request_resource(mdev, 0, "swim3 (mmio)")) {
1051 printk(KERN_ERR "fd%d: can't request mmio resource for %s\n", 1127 swim3_err("%s", "Can't request mmio resource\n");
1052 index, swim->full_name);
1053 return -EBUSY; 1128 return -EBUSY;
1054 } 1129 }
1055 if (macio_request_resource(mdev, 1, "swim3 (dma)")) { 1130 if (macio_request_resource(mdev, 1, "swim3 (dma)")) {
1056 printk(KERN_ERR "fd%d: can't request dma resource for %s\n", 1131 swim3_err("%s", "Can't request dma resource\n");
1057 index, swim->full_name);
1058 macio_release_resource(mdev, 0); 1132 macio_release_resource(mdev, 0);
1059 return -EBUSY; 1133 return -EBUSY;
1060 } 1134 }
@@ -1063,22 +1137,18 @@ static int swim3_add_device(struct macio_dev *mdev, int index)
1063 if (mdev->media_bay == NULL) 1137 if (mdev->media_bay == NULL)
1064 pmac_call_feature(PMAC_FTR_SWIM3_ENABLE, swim, 0, 1); 1138 pmac_call_feature(PMAC_FTR_SWIM3_ENABLE, swim, 0, 1);
1065 1139
1066 memset(fs, 0, sizeof(*fs));
1067 spin_lock_init(&fs->lock);
1068 fs->state = idle; 1140 fs->state = idle;
1069 fs->swim3 = (struct swim3 __iomem *) 1141 fs->swim3 = (struct swim3 __iomem *)
1070 ioremap(macio_resource_start(mdev, 0), 0x200); 1142 ioremap(macio_resource_start(mdev, 0), 0x200);
1071 if (fs->swim3 == NULL) { 1143 if (fs->swim3 == NULL) {
1072 printk("fd%d: couldn't map registers for %s\n", 1144 swim3_err("%s", "Couldn't map mmio registers\n");
1073 index, swim->full_name);
1074 rc = -ENOMEM; 1145 rc = -ENOMEM;
1075 goto out_release; 1146 goto out_release;
1076 } 1147 }
1077 fs->dma = (struct dbdma_regs __iomem *) 1148 fs->dma = (struct dbdma_regs __iomem *)
1078 ioremap(macio_resource_start(mdev, 1), 0x200); 1149 ioremap(macio_resource_start(mdev, 1), 0x200);
1079 if (fs->dma == NULL) { 1150 if (fs->dma == NULL) {
1080 printk("fd%d: couldn't map DMA for %s\n", 1151 swim3_err("%s", "Couldn't map dma registers\n");
1081 index, swim->full_name);
1082 iounmap(fs->swim3); 1152 iounmap(fs->swim3);
1083 rc = -ENOMEM; 1153 rc = -ENOMEM;
1084 goto out_release; 1154 goto out_release;
@@ -1090,31 +1160,25 @@ static int swim3_add_device(struct macio_dev *mdev, int index)
1090 fs->secpercyl = 36; 1160 fs->secpercyl = 36;
1091 fs->secpertrack = 18; 1161 fs->secpertrack = 18;
1092 fs->total_secs = 2880; 1162 fs->total_secs = 2880;
1093 fs->mdev = mdev;
1094 init_waitqueue_head(&fs->wait); 1163 init_waitqueue_head(&fs->wait);
1095 1164
1096 fs->dma_cmd = (struct dbdma_cmd *) DBDMA_ALIGN(fs->dbdma_cmd_space); 1165 fs->dma_cmd = (struct dbdma_cmd *) DBDMA_ALIGN(fs->dbdma_cmd_space);
1097 memset(fs->dma_cmd, 0, 2 * sizeof(struct dbdma_cmd)); 1166 memset(fs->dma_cmd, 0, 2 * sizeof(struct dbdma_cmd));
1098 st_le16(&fs->dma_cmd[1].command, DBDMA_STOP); 1167 st_le16(&fs->dma_cmd[1].command, DBDMA_STOP);
1099 1168
1169 if (mdev->media_bay == NULL || check_media_bay(mdev->media_bay) == MB_FD)
1170 swim3_mb_event(mdev, MB_FD);
1171
1100 if (request_irq(fs->swim3_intr, swim3_interrupt, 0, "SWIM3", fs)) { 1172 if (request_irq(fs->swim3_intr, swim3_interrupt, 0, "SWIM3", fs)) {
1101 printk(KERN_ERR "fd%d: couldn't request irq %d for %s\n", 1173 swim3_err("%s", "Couldn't request interrupt\n");
1102 index, fs->swim3_intr, swim->full_name);
1103 pmac_call_feature(PMAC_FTR_SWIM3_ENABLE, swim, 0, 0); 1174 pmac_call_feature(PMAC_FTR_SWIM3_ENABLE, swim, 0, 0);
1104 goto out_unmap; 1175 goto out_unmap;
1105 return -EBUSY; 1176 return -EBUSY;
1106 } 1177 }
1107/*
1108 if (request_irq(fs->dma_intr, fd_dma_interrupt, 0, "SWIM3-dma", fs)) {
1109 printk(KERN_ERR "Couldn't get irq %d for SWIM3 DMA",
1110 fs->dma_intr);
1111 return -EBUSY;
1112 }
1113*/
1114 1178
1115 init_timer(&fs->timeout); 1179 init_timer(&fs->timeout);
1116 1180
1117 printk(KERN_INFO "fd%d: SWIM3 floppy controller %s\n", floppy_count, 1181 swim3_info("SWIM3 floppy controller %s\n",
1118 mdev->media_bay ? "in media bay" : ""); 1182 mdev->media_bay ? "in media bay" : "");
1119 1183
1120 return 0; 1184 return 0;
@@ -1132,41 +1196,42 @@ static int swim3_add_device(struct macio_dev *mdev, int index)
1132 1196
1133static int __devinit swim3_attach(struct macio_dev *mdev, const struct of_device_id *match) 1197static int __devinit swim3_attach(struct macio_dev *mdev, const struct of_device_id *match)
1134{ 1198{
1135 int i, rc;
1136 struct gendisk *disk; 1199 struct gendisk *disk;
1200 int index, rc;
1201
1202 index = floppy_count++;
1203 if (index >= MAX_FLOPPIES)
1204 return -ENXIO;
1137 1205
1138 /* Add the drive */ 1206 /* Add the drive */
1139 rc = swim3_add_device(mdev, floppy_count); 1207 rc = swim3_add_device(mdev, index);
1140 if (rc) 1208 if (rc)
1141 return rc; 1209 return rc;
1210 /* Now register that disk. Same comment about failure handling */
1211 disk = disks[index] = alloc_disk(1);
1212 if (disk == NULL)
1213 return -ENOMEM;
1214 disk->queue = blk_init_queue(do_fd_request, &swim3_lock);
1215 if (disk->queue == NULL) {
1216 put_disk(disk);
1217 return -ENOMEM;
1218 }
1219 disk->queue->queuedata = &floppy_states[index];
1142 1220
1143 /* Now create the queue if not there yet */ 1221 if (index == 0) {
1144 if (swim3_queue == NULL) {
1145 /* If we failed, there isn't much we can do as the driver is still 1222 /* If we failed, there isn't much we can do as the driver is still
1146 * too dumb to remove the device, just bail out 1223 * too dumb to remove the device, just bail out
1147 */ 1224 */
1148 if (register_blkdev(FLOPPY_MAJOR, "fd")) 1225 if (register_blkdev(FLOPPY_MAJOR, "fd"))
1149 return 0; 1226 return 0;
1150 swim3_queue = blk_init_queue(do_fd_request, &swim3_lock);
1151 if (swim3_queue == NULL) {
1152 unregister_blkdev(FLOPPY_MAJOR, "fd");
1153 return 0;
1154 }
1155 } 1227 }
1156 1228
1157 /* Now register that disk. Same comment about failure handling */
1158 i = floppy_count++;
1159 disk = disks[i] = alloc_disk(1);
1160 if (disk == NULL)
1161 return 0;
1162
1163 disk->major = FLOPPY_MAJOR; 1229 disk->major = FLOPPY_MAJOR;
1164 disk->first_minor = i; 1230 disk->first_minor = index;
1165 disk->fops = &floppy_fops; 1231 disk->fops = &floppy_fops;
1166 disk->private_data = &floppy_states[i]; 1232 disk->private_data = &floppy_states[index];
1167 disk->queue = swim3_queue;
1168 disk->flags |= GENHD_FL_REMOVABLE; 1233 disk->flags |= GENHD_FL_REMOVABLE;
1169 sprintf(disk->disk_name, "fd%d", i); 1234 sprintf(disk->disk_name, "fd%d", index);
1170 set_capacity(disk, 2880); 1235 set_capacity(disk, 2880);
1171 add_disk(disk); 1236 add_disk(disk);
1172 1237
@@ -1194,6 +1259,9 @@ static struct macio_driver swim3_driver =
1194 .of_match_table = swim3_match, 1259 .of_match_table = swim3_match,
1195 }, 1260 },
1196 .probe = swim3_attach, 1261 .probe = swim3_attach,
1262#ifdef CONFIG_PMAC_MEDIABAY
1263 .mediabay_event = swim3_mb_event,
1264#endif
1197#if 0 1265#if 0
1198 .suspend = swim3_suspend, 1266 .suspend = swim3_suspend,
1199 .resume = swim3_resume, 1267 .resume = swim3_resume,
diff --git a/drivers/block/sx8.c b/drivers/block/sx8.c
index b70f0fca9a42..e7472f567c9d 100644
--- a/drivers/block/sx8.c
+++ b/drivers/block/sx8.c
@@ -619,8 +619,10 @@ static int carm_array_info (struct carm_host *host, unsigned int array_idx)
619 host->state == HST_DEV_SCAN); 619 host->state == HST_DEV_SCAN);
620 spin_unlock_irq(&host->lock); 620 spin_unlock_irq(&host->lock);
621 621
622 DPRINTK("blk_insert_request, tag == %u\n", idx); 622 DPRINTK("blk_execute_rq_nowait, tag == %u\n", idx);
623 blk_insert_request(host->oob_q, crq->rq, 1, crq); 623 crq->rq->cmd_type = REQ_TYPE_SPECIAL;
624 crq->rq->special = crq;
625 blk_execute_rq_nowait(host->oob_q, NULL, crq->rq, true, NULL);
624 626
625 return 0; 627 return 0;
626 628
@@ -658,8 +660,10 @@ static int carm_send_special (struct carm_host *host, carm_sspc_t func)
658 BUG_ON(rc < 0); 660 BUG_ON(rc < 0);
659 crq->msg_bucket = (u32) rc; 661 crq->msg_bucket = (u32) rc;
660 662
661 DPRINTK("blk_insert_request, tag == %u\n", idx); 663 DPRINTK("blk_execute_rq_nowait, tag == %u\n", idx);
662 blk_insert_request(host->oob_q, crq->rq, 1, crq); 664 crq->rq->cmd_type = REQ_TYPE_SPECIAL;
665 crq->rq->special = crq;
666 blk_execute_rq_nowait(host->oob_q, NULL, crq->rq, true, NULL);
663 667
664 return 0; 668 return 0;
665} 669}
diff --git a/drivers/block/ub.c b/drivers/block/ub.c
index 0e376d46bdd1..7333b9e44411 100644
--- a/drivers/block/ub.c
+++ b/drivers/block/ub.c
@@ -1744,12 +1744,11 @@ static int ub_bd_release(struct gendisk *disk, fmode_t mode)
1744static int ub_bd_ioctl(struct block_device *bdev, fmode_t mode, 1744static int ub_bd_ioctl(struct block_device *bdev, fmode_t mode,
1745 unsigned int cmd, unsigned long arg) 1745 unsigned int cmd, unsigned long arg)
1746{ 1746{
1747 struct gendisk *disk = bdev->bd_disk;
1748 void __user *usermem = (void __user *) arg; 1747 void __user *usermem = (void __user *) arg;
1749 int ret; 1748 int ret;
1750 1749
1751 mutex_lock(&ub_mutex); 1750 mutex_lock(&ub_mutex);
1752 ret = scsi_cmd_ioctl(disk->queue, disk, mode, cmd, usermem); 1751 ret = scsi_cmd_blk_ioctl(bdev, mode, cmd, usermem);
1753 mutex_unlock(&ub_mutex); 1752 mutex_unlock(&ub_mutex);
1754 1753
1755 return ret; 1754 return ret;
diff --git a/drivers/block/umem.c b/drivers/block/umem.c
index 031ca720d926..aa2712060bfb 100644
--- a/drivers/block/umem.c
+++ b/drivers/block/umem.c
@@ -513,7 +513,7 @@ static void process_page(unsigned long data)
513 } 513 }
514} 514}
515 515
516static int mm_make_request(struct request_queue *q, struct bio *bio) 516static void mm_make_request(struct request_queue *q, struct bio *bio)
517{ 517{
518 struct cardinfo *card = q->queuedata; 518 struct cardinfo *card = q->queuedata;
519 pr_debug("mm_make_request %llu %u\n", 519 pr_debug("mm_make_request %llu %u\n",
@@ -525,7 +525,7 @@ static int mm_make_request(struct request_queue *q, struct bio *bio)
525 card->biotail = &bio->bi_next; 525 card->biotail = &bio->bi_next;
526 spin_unlock_irq(&card->lock); 526 spin_unlock_irq(&card->lock);
527 527
528 return 0; 528 return;
529} 529}
530 530
531static irqreturn_t mm_interrupt(int irq, void *__card) 531static irqreturn_t mm_interrupt(int irq, void *__card)
diff --git a/drivers/block/virtio_blk.c b/drivers/block/virtio_blk.c
index 079c08808d8a..c4a60badf252 100644
--- a/drivers/block/virtio_blk.c
+++ b/drivers/block/virtio_blk.c
@@ -3,15 +3,20 @@
3#include <linux/slab.h> 3#include <linux/slab.h>
4#include <linux/blkdev.h> 4#include <linux/blkdev.h>
5#include <linux/hdreg.h> 5#include <linux/hdreg.h>
6#include <linux/module.h>
7#include <linux/mutex.h>
6#include <linux/virtio.h> 8#include <linux/virtio.h>
7#include <linux/virtio_blk.h> 9#include <linux/virtio_blk.h>
8#include <linux/scatterlist.h> 10#include <linux/scatterlist.h>
9#include <linux/string_helpers.h> 11#include <linux/string_helpers.h>
10#include <scsi/scsi_cmnd.h> 12#include <scsi/scsi_cmnd.h>
13#include <linux/idr.h>
11 14
12#define PART_BITS 4 15#define PART_BITS 4
13 16
14static int major, index; 17static int major;
18static DEFINE_IDA(vd_index_ida);
19
15struct workqueue_struct *virtblk_wq; 20struct workqueue_struct *virtblk_wq;
16 21
17struct virtio_blk 22struct virtio_blk
@@ -32,9 +37,18 @@ struct virtio_blk
32 /* Process context for config space updates */ 37 /* Process context for config space updates */
33 struct work_struct config_work; 38 struct work_struct config_work;
34 39
40 /* Lock for config space updates */
41 struct mutex config_lock;
42
43 /* enable config space updates */
44 bool config_enable;
45
35 /* What host tells us, plus 2 for header & tailer. */ 46 /* What host tells us, plus 2 for header & tailer. */
36 unsigned int sg_elems; 47 unsigned int sg_elems;
37 48
49 /* Ida index - used to track minor number allocations. */
50 int index;
51
38 /* Scatterlist: can be too big for stack. */ 52 /* Scatterlist: can be too big for stack. */
39 struct scatterlist sg[/*sg_elems*/]; 53 struct scatterlist sg[/*sg_elems*/];
40}; 54};
@@ -165,7 +179,7 @@ static bool do_req(struct request_queue *q, struct virtio_blk *vblk,
165 } 179 }
166 } 180 }
167 181
168 if (virtqueue_add_buf(vblk->vq, vblk->sg, out, in, vbr) < 0) { 182 if (virtqueue_add_buf(vblk->vq, vblk->sg, out, in, vbr, GFP_ATOMIC)<0) {
169 mempool_free(vbr, vblk->pool); 183 mempool_free(vbr, vblk->pool);
170 return false; 184 return false;
171 } 185 }
@@ -236,8 +250,8 @@ static int virtblk_ioctl(struct block_device *bdev, fmode_t mode,
236 if (!virtio_has_feature(vblk->vdev, VIRTIO_BLK_F_SCSI)) 250 if (!virtio_has_feature(vblk->vdev, VIRTIO_BLK_F_SCSI))
237 return -ENOTTY; 251 return -ENOTTY;
238 252
239 return scsi_cmd_ioctl(disk->queue, disk, mode, cmd, 253 return scsi_cmd_blk_ioctl(bdev, mode, cmd,
240 (void __user *)data); 254 (void __user *)data);
241} 255}
242 256
243/* We provide getgeo only to please some old bootloader/partitioning tools */ 257/* We provide getgeo only to please some old bootloader/partitioning tools */
@@ -276,6 +290,11 @@ static int index_to_minor(int index)
276 return index << PART_BITS; 290 return index << PART_BITS;
277} 291}
278 292
293static int minor_to_index(int minor)
294{
295 return minor >> PART_BITS;
296}
297
279static ssize_t virtblk_serial_show(struct device *dev, 298static ssize_t virtblk_serial_show(struct device *dev,
280 struct device_attribute *attr, char *buf) 299 struct device_attribute *attr, char *buf)
281{ 300{
@@ -306,6 +325,10 @@ static void virtblk_config_changed_work(struct work_struct *work)
306 char cap_str_2[10], cap_str_10[10]; 325 char cap_str_2[10], cap_str_10[10];
307 u64 capacity, size; 326 u64 capacity, size;
308 327
328 mutex_lock(&vblk->config_lock);
329 if (!vblk->config_enable)
330 goto done;
331
309 /* Host must always specify the capacity. */ 332 /* Host must always specify the capacity. */
310 vdev->config->get(vdev, offsetof(struct virtio_blk_config, capacity), 333 vdev->config->get(vdev, offsetof(struct virtio_blk_config, capacity),
311 &capacity, sizeof(capacity)); 334 &capacity, sizeof(capacity));
@@ -328,6 +351,8 @@ static void virtblk_config_changed_work(struct work_struct *work)
328 cap_str_10, cap_str_2); 351 cap_str_10, cap_str_2);
329 352
330 set_capacity(vblk->disk, capacity); 353 set_capacity(vblk->disk, capacity);
354done:
355 mutex_unlock(&vblk->config_lock);
331} 356}
332 357
333static void virtblk_config_changed(struct virtio_device *vdev) 358static void virtblk_config_changed(struct virtio_device *vdev)
@@ -337,18 +362,33 @@ static void virtblk_config_changed(struct virtio_device *vdev)
337 queue_work(virtblk_wq, &vblk->config_work); 362 queue_work(virtblk_wq, &vblk->config_work);
338} 363}
339 364
365static int init_vq(struct virtio_blk *vblk)
366{
367 int err = 0;
368
369 /* We expect one virtqueue, for output. */
370 vblk->vq = virtio_find_single_vq(vblk->vdev, blk_done, "requests");
371 if (IS_ERR(vblk->vq))
372 err = PTR_ERR(vblk->vq);
373
374 return err;
375}
376
340static int __devinit virtblk_probe(struct virtio_device *vdev) 377static int __devinit virtblk_probe(struct virtio_device *vdev)
341{ 378{
342 struct virtio_blk *vblk; 379 struct virtio_blk *vblk;
343 struct request_queue *q; 380 struct request_queue *q;
344 int err; 381 int err, index;
345 u64 cap; 382 u64 cap;
346 u32 v, blk_size, sg_elems, opt_io_size; 383 u32 v, blk_size, sg_elems, opt_io_size;
347 u16 min_io_size; 384 u16 min_io_size;
348 u8 physical_block_exp, alignment_offset; 385 u8 physical_block_exp, alignment_offset;
349 386
350 if (index_to_minor(index) >= 1 << MINORBITS) 387 err = ida_simple_get(&vd_index_ida, 0, minor_to_index(1 << MINORBITS),
351 return -ENOSPC; 388 GFP_KERNEL);
389 if (err < 0)
390 goto out;
391 index = err;
352 392
353 /* We need to know how many segments before we allocate. */ 393 /* We need to know how many segments before we allocate. */
354 err = virtio_config_val(vdev, VIRTIO_BLK_F_SEG_MAX, 394 err = virtio_config_val(vdev, VIRTIO_BLK_F_SEG_MAX,
@@ -365,7 +405,7 @@ static int __devinit virtblk_probe(struct virtio_device *vdev)
365 sizeof(vblk->sg[0]) * sg_elems, GFP_KERNEL); 405 sizeof(vblk->sg[0]) * sg_elems, GFP_KERNEL);
366 if (!vblk) { 406 if (!vblk) {
367 err = -ENOMEM; 407 err = -ENOMEM;
368 goto out; 408 goto out_free_index;
369 } 409 }
370 410
371 INIT_LIST_HEAD(&vblk->reqs); 411 INIT_LIST_HEAD(&vblk->reqs);
@@ -373,14 +413,13 @@ static int __devinit virtblk_probe(struct virtio_device *vdev)
373 vblk->vdev = vdev; 413 vblk->vdev = vdev;
374 vblk->sg_elems = sg_elems; 414 vblk->sg_elems = sg_elems;
375 sg_init_table(vblk->sg, vblk->sg_elems); 415 sg_init_table(vblk->sg, vblk->sg_elems);
416 mutex_init(&vblk->config_lock);
376 INIT_WORK(&vblk->config_work, virtblk_config_changed_work); 417 INIT_WORK(&vblk->config_work, virtblk_config_changed_work);
418 vblk->config_enable = true;
377 419
378 /* We expect one virtqueue, for output. */ 420 err = init_vq(vblk);
379 vblk->vq = virtio_find_single_vq(vdev, blk_done, "requests"); 421 if (err)
380 if (IS_ERR(vblk->vq)) {
381 err = PTR_ERR(vblk->vq);
382 goto out_free_vblk; 422 goto out_free_vblk;
383 }
384 423
385 vblk->pool = mempool_create_kmalloc_pool(1,sizeof(struct virtblk_req)); 424 vblk->pool = mempool_create_kmalloc_pool(1,sizeof(struct virtblk_req));
386 if (!vblk->pool) { 425 if (!vblk->pool) {
@@ -421,7 +460,7 @@ static int __devinit virtblk_probe(struct virtio_device *vdev)
421 vblk->disk->private_data = vblk; 460 vblk->disk->private_data = vblk;
422 vblk->disk->fops = &virtblk_fops; 461 vblk->disk->fops = &virtblk_fops;
423 vblk->disk->driverfs_dev = &vdev->dev; 462 vblk->disk->driverfs_dev = &vdev->dev;
424 index++; 463 vblk->index = index;
425 464
426 /* configure queue flush support */ 465 /* configure queue flush support */
427 if (virtio_has_feature(vdev, VIRTIO_BLK_F_FLUSH)) 466 if (virtio_has_feature(vdev, VIRTIO_BLK_F_FLUSH))
@@ -516,6 +555,8 @@ out_free_vq:
516 vdev->config->del_vqs(vdev); 555 vdev->config->del_vqs(vdev);
517out_free_vblk: 556out_free_vblk:
518 kfree(vblk); 557 kfree(vblk);
558out_free_index:
559 ida_simple_remove(&vd_index_ida, index);
519out: 560out:
520 return err; 561 return err;
521} 562}
@@ -523,8 +564,12 @@ out:
523static void __devexit virtblk_remove(struct virtio_device *vdev) 564static void __devexit virtblk_remove(struct virtio_device *vdev)
524{ 565{
525 struct virtio_blk *vblk = vdev->priv; 566 struct virtio_blk *vblk = vdev->priv;
567 int index = vblk->index;
526 568
527 flush_work(&vblk->config_work); 569 /* Prevent config work handler from accessing the device. */
570 mutex_lock(&vblk->config_lock);
571 vblk->config_enable = false;
572 mutex_unlock(&vblk->config_lock);
528 573
529 /* Nothing should be pending. */ 574 /* Nothing should be pending. */
530 BUG_ON(!list_empty(&vblk->reqs)); 575 BUG_ON(!list_empty(&vblk->reqs));
@@ -532,13 +577,56 @@ static void __devexit virtblk_remove(struct virtio_device *vdev)
532 /* Stop all the virtqueues. */ 577 /* Stop all the virtqueues. */
533 vdev->config->reset(vdev); 578 vdev->config->reset(vdev);
534 579
580 flush_work(&vblk->config_work);
581
535 del_gendisk(vblk->disk); 582 del_gendisk(vblk->disk);
536 blk_cleanup_queue(vblk->disk->queue); 583 blk_cleanup_queue(vblk->disk->queue);
537 put_disk(vblk->disk); 584 put_disk(vblk->disk);
538 mempool_destroy(vblk->pool); 585 mempool_destroy(vblk->pool);
539 vdev->config->del_vqs(vdev); 586 vdev->config->del_vqs(vdev);
540 kfree(vblk); 587 kfree(vblk);
588 ida_simple_remove(&vd_index_ida, index);
589}
590
591#ifdef CONFIG_PM
592static int virtblk_freeze(struct virtio_device *vdev)
593{
594 struct virtio_blk *vblk = vdev->priv;
595
596 /* Ensure we don't receive any more interrupts */
597 vdev->config->reset(vdev);
598
599 /* Prevent config work handler from accessing the device. */
600 mutex_lock(&vblk->config_lock);
601 vblk->config_enable = false;
602 mutex_unlock(&vblk->config_lock);
603
604 flush_work(&vblk->config_work);
605
606 spin_lock_irq(vblk->disk->queue->queue_lock);
607 blk_stop_queue(vblk->disk->queue);
608 spin_unlock_irq(vblk->disk->queue->queue_lock);
609 blk_sync_queue(vblk->disk->queue);
610
611 vdev->config->del_vqs(vdev);
612 return 0;
613}
614
615static int virtblk_restore(struct virtio_device *vdev)
616{
617 struct virtio_blk *vblk = vdev->priv;
618 int ret;
619
620 vblk->config_enable = true;
621 ret = init_vq(vdev->priv);
622 if (!ret) {
623 spin_lock_irq(vblk->disk->queue->queue_lock);
624 blk_start_queue(vblk->disk->queue);
625 spin_unlock_irq(vblk->disk->queue->queue_lock);
626 }
627 return ret;
541} 628}
629#endif
542 630
543static const struct virtio_device_id id_table[] = { 631static const struct virtio_device_id id_table[] = {
544 { VIRTIO_ID_BLOCK, VIRTIO_DEV_ANY_ID }, 632 { VIRTIO_ID_BLOCK, VIRTIO_DEV_ANY_ID },
@@ -565,6 +653,10 @@ static struct virtio_driver __refdata virtio_blk = {
565 .probe = virtblk_probe, 653 .probe = virtblk_probe,
566 .remove = __devexit_p(virtblk_remove), 654 .remove = __devexit_p(virtblk_remove),
567 .config_changed = virtblk_config_changed, 655 .config_changed = virtblk_config_changed,
656#ifdef CONFIG_PM
657 .freeze = virtblk_freeze,
658 .restore = virtblk_restore,
659#endif
568}; 660};
569 661
570static int __init init(void) 662static int __init init(void)
diff --git a/drivers/block/xd.c b/drivers/block/xd.c
index 4abd2bcd20fb..51a972704db5 100644
--- a/drivers/block/xd.c
+++ b/drivers/block/xd.c
@@ -148,7 +148,7 @@ static volatile int xdc_busy;
148static struct timer_list xd_watchdog_int; 148static struct timer_list xd_watchdog_int;
149 149
150static volatile u_char xd_error; 150static volatile u_char xd_error;
151static int nodma = XD_DONT_USE_DMA; 151static bool nodma = XD_DONT_USE_DMA;
152 152
153static struct request_queue *xd_queue; 153static struct request_queue *xd_queue;
154 154
diff --git a/drivers/block/xen-blkback/blkback.c b/drivers/block/xen-blkback/blkback.c
index 2330a9ad5e95..0088bf60f368 100644
--- a/drivers/block/xen-blkback/blkback.c
+++ b/drivers/block/xen-blkback/blkback.c
@@ -258,13 +258,16 @@ irqreturn_t xen_blkif_be_int(int irq, void *dev_id)
258 258
259static void print_stats(struct xen_blkif *blkif) 259static void print_stats(struct xen_blkif *blkif)
260{ 260{
261 pr_info("xen-blkback (%s): oo %3d | rd %4d | wr %4d | f %4d\n", 261 pr_info("xen-blkback (%s): oo %3d | rd %4d | wr %4d | f %4d"
262 " | ds %4d\n",
262 current->comm, blkif->st_oo_req, 263 current->comm, blkif->st_oo_req,
263 blkif->st_rd_req, blkif->st_wr_req, blkif->st_f_req); 264 blkif->st_rd_req, blkif->st_wr_req,
265 blkif->st_f_req, blkif->st_ds_req);
264 blkif->st_print = jiffies + msecs_to_jiffies(10 * 1000); 266 blkif->st_print = jiffies + msecs_to_jiffies(10 * 1000);
265 blkif->st_rd_req = 0; 267 blkif->st_rd_req = 0;
266 blkif->st_wr_req = 0; 268 blkif->st_wr_req = 0;
267 blkif->st_oo_req = 0; 269 blkif->st_oo_req = 0;
270 blkif->st_ds_req = 0;
268} 271}
269 272
270int xen_blkif_schedule(void *arg) 273int xen_blkif_schedule(void *arg)
@@ -356,7 +359,7 @@ static int xen_blkbk_map(struct blkif_request *req,
356{ 359{
357 struct gnttab_map_grant_ref map[BLKIF_MAX_SEGMENTS_PER_REQUEST]; 360 struct gnttab_map_grant_ref map[BLKIF_MAX_SEGMENTS_PER_REQUEST];
358 int i; 361 int i;
359 int nseg = req->nr_segments; 362 int nseg = req->u.rw.nr_segments;
360 int ret = 0; 363 int ret = 0;
361 364
362 /* 365 /*
@@ -396,7 +399,7 @@ static int xen_blkbk_map(struct blkif_request *req,
396 continue; 399 continue;
397 400
398 ret = m2p_add_override(PFN_DOWN(map[i].dev_bus_addr), 401 ret = m2p_add_override(PFN_DOWN(map[i].dev_bus_addr),
399 blkbk->pending_page(pending_req, i), false); 402 blkbk->pending_page(pending_req, i), NULL);
400 if (ret) { 403 if (ret) {
401 pr_alert(DRV_PFX "Failed to install M2P override for %lx (ret: %d)\n", 404 pr_alert(DRV_PFX "Failed to install M2P override for %lx (ret: %d)\n",
402 (unsigned long)map[i].dev_bus_addr, ret); 405 (unsigned long)map[i].dev_bus_addr, ret);
@@ -410,6 +413,56 @@ static int xen_blkbk_map(struct blkif_request *req,
410 return ret; 413 return ret;
411} 414}
412 415
416static int dispatch_discard_io(struct xen_blkif *blkif,
417 struct blkif_request *req)
418{
419 int err = 0;
420 int status = BLKIF_RSP_OKAY;
421 struct block_device *bdev = blkif->vbd.bdev;
422
423 blkif->st_ds_req++;
424
425 xen_blkif_get(blkif);
426 if (blkif->blk_backend_type == BLKIF_BACKEND_PHY ||
427 blkif->blk_backend_type == BLKIF_BACKEND_FILE) {
428 unsigned long secure = (blkif->vbd.discard_secure &&
429 (req->u.discard.flag & BLKIF_DISCARD_SECURE)) ?
430 BLKDEV_DISCARD_SECURE : 0;
431 err = blkdev_issue_discard(bdev,
432 req->u.discard.sector_number,
433 req->u.discard.nr_sectors,
434 GFP_KERNEL, secure);
435 } else
436 err = -EOPNOTSUPP;
437
438 if (err == -EOPNOTSUPP) {
439 pr_debug(DRV_PFX "discard op failed, not supported\n");
440 status = BLKIF_RSP_EOPNOTSUPP;
441 } else if (err)
442 status = BLKIF_RSP_ERROR;
443
444 make_response(blkif, req->u.discard.id, req->operation, status);
445 xen_blkif_put(blkif);
446 return err;
447}
448
449static void xen_blk_drain_io(struct xen_blkif *blkif)
450{
451 atomic_set(&blkif->drain, 1);
452 do {
453 /* The initial value is one, and one refcnt taken at the
454 * start of the xen_blkif_schedule thread. */
455 if (atomic_read(&blkif->refcnt) <= 2)
456 break;
457 wait_for_completion_interruptible_timeout(
458 &blkif->drain_complete, HZ);
459
460 if (!atomic_read(&blkif->drain))
461 break;
462 } while (!kthread_should_stop());
463 atomic_set(&blkif->drain, 0);
464}
465
413/* 466/*
414 * Completion callback on the bio's. Called as bh->b_end_io() 467 * Completion callback on the bio's. Called as bh->b_end_io()
415 */ 468 */
@@ -422,6 +475,11 @@ static void __end_block_io_op(struct pending_req *pending_req, int error)
422 pr_debug(DRV_PFX "flush diskcache op failed, not supported\n"); 475 pr_debug(DRV_PFX "flush diskcache op failed, not supported\n");
423 xen_blkbk_flush_diskcache(XBT_NIL, pending_req->blkif->be, 0); 476 xen_blkbk_flush_diskcache(XBT_NIL, pending_req->blkif->be, 0);
424 pending_req->status = BLKIF_RSP_EOPNOTSUPP; 477 pending_req->status = BLKIF_RSP_EOPNOTSUPP;
478 } else if ((pending_req->operation == BLKIF_OP_WRITE_BARRIER) &&
479 (error == -EOPNOTSUPP)) {
480 pr_debug(DRV_PFX "write barrier op failed, not supported\n");
481 xen_blkbk_barrier(XBT_NIL, pending_req->blkif->be, 0);
482 pending_req->status = BLKIF_RSP_EOPNOTSUPP;
425 } else if (error) { 483 } else if (error) {
426 pr_debug(DRV_PFX "Buffer not up-to-date at end of operation," 484 pr_debug(DRV_PFX "Buffer not up-to-date at end of operation,"
427 " error=%d\n", error); 485 " error=%d\n", error);
@@ -438,6 +496,10 @@ static void __end_block_io_op(struct pending_req *pending_req, int error)
438 make_response(pending_req->blkif, pending_req->id, 496 make_response(pending_req->blkif, pending_req->id,
439 pending_req->operation, pending_req->status); 497 pending_req->operation, pending_req->status);
440 xen_blkif_put(pending_req->blkif); 498 xen_blkif_put(pending_req->blkif);
499 if (atomic_read(&pending_req->blkif->refcnt) <= 2) {
500 if (atomic_read(&pending_req->blkif->drain))
501 complete(&pending_req->blkif->drain_complete);
502 }
441 free_req(pending_req); 503 free_req(pending_req);
442 } 504 }
443} 505}
@@ -505,8 +567,11 @@ __do_block_io_op(struct xen_blkif *blkif)
505 567
506 /* Apply all sanity checks to /private copy/ of request. */ 568 /* Apply all sanity checks to /private copy/ of request. */
507 barrier(); 569 barrier();
508 570 if (unlikely(req.operation == BLKIF_OP_DISCARD)) {
509 if (dispatch_rw_block_io(blkif, &req, pending_req)) 571 free_req(pending_req);
572 if (dispatch_discard_io(blkif, &req))
573 break;
574 } else if (dispatch_rw_block_io(blkif, &req, pending_req))
510 break; 575 break;
511 576
512 /* Yield point for this unbounded loop. */ 577 /* Yield point for this unbounded loop. */
@@ -532,7 +597,6 @@ do_block_io_op(struct xen_blkif *blkif)
532 597
533 return more_to_do; 598 return more_to_do;
534} 599}
535
536/* 600/*
537 * Transmutation of the 'struct blkif_request' to a proper 'struct bio' 601 * Transmutation of the 'struct blkif_request' to a proper 'struct bio'
538 * and call the 'submit_bio' to pass it to the underlying storage. 602 * and call the 'submit_bio' to pass it to the underlying storage.
@@ -549,6 +613,7 @@ static int dispatch_rw_block_io(struct xen_blkif *blkif,
549 int i, nbio = 0; 613 int i, nbio = 0;
550 int operation; 614 int operation;
551 struct blk_plug plug; 615 struct blk_plug plug;
616 bool drain = false;
552 617
553 switch (req->operation) { 618 switch (req->operation) {
554 case BLKIF_OP_READ: 619 case BLKIF_OP_READ:
@@ -559,11 +624,12 @@ static int dispatch_rw_block_io(struct xen_blkif *blkif,
559 blkif->st_wr_req++; 624 blkif->st_wr_req++;
560 operation = WRITE_ODIRECT; 625 operation = WRITE_ODIRECT;
561 break; 626 break;
627 case BLKIF_OP_WRITE_BARRIER:
628 drain = true;
562 case BLKIF_OP_FLUSH_DISKCACHE: 629 case BLKIF_OP_FLUSH_DISKCACHE:
563 blkif->st_f_req++; 630 blkif->st_f_req++;
564 operation = WRITE_FLUSH; 631 operation = WRITE_FLUSH;
565 break; 632 break;
566 case BLKIF_OP_WRITE_BARRIER:
567 default: 633 default:
568 operation = 0; /* make gcc happy */ 634 operation = 0; /* make gcc happy */
569 goto fail_response; 635 goto fail_response;
@@ -571,7 +637,8 @@ static int dispatch_rw_block_io(struct xen_blkif *blkif,
571 } 637 }
572 638
573 /* Check that the number of segments is sane. */ 639 /* Check that the number of segments is sane. */
574 nseg = req->nr_segments; 640 nseg = req->u.rw.nr_segments;
641
575 if (unlikely(nseg == 0 && operation != WRITE_FLUSH) || 642 if (unlikely(nseg == 0 && operation != WRITE_FLUSH) ||
576 unlikely(nseg > BLKIF_MAX_SEGMENTS_PER_REQUEST)) { 643 unlikely(nseg > BLKIF_MAX_SEGMENTS_PER_REQUEST)) {
577 pr_debug(DRV_PFX "Bad number of segments in request (%d)\n", 644 pr_debug(DRV_PFX "Bad number of segments in request (%d)\n",
@@ -580,12 +647,12 @@ static int dispatch_rw_block_io(struct xen_blkif *blkif,
580 goto fail_response; 647 goto fail_response;
581 } 648 }
582 649
583 preq.dev = req->handle; 650 preq.dev = req->u.rw.handle;
584 preq.sector_number = req->u.rw.sector_number; 651 preq.sector_number = req->u.rw.sector_number;
585 preq.nr_sects = 0; 652 preq.nr_sects = 0;
586 653
587 pending_req->blkif = blkif; 654 pending_req->blkif = blkif;
588 pending_req->id = req->id; 655 pending_req->id = req->u.rw.id;
589 pending_req->operation = req->operation; 656 pending_req->operation = req->operation;
590 pending_req->status = BLKIF_RSP_OKAY; 657 pending_req->status = BLKIF_RSP_OKAY;
591 pending_req->nr_pages = nseg; 658 pending_req->nr_pages = nseg;
@@ -621,6 +688,12 @@ static int dispatch_rw_block_io(struct xen_blkif *blkif,
621 } 688 }
622 } 689 }
623 690
691 /* Wait on all outstanding I/O's and once that has been completed
692 * issue the WRITE_FLUSH.
693 */
694 if (drain)
695 xen_blk_drain_io(pending_req->blkif);
696
624 /* 697 /*
625 * If we have failed at this point, we need to undo the M2P override, 698 * If we have failed at this point, we need to undo the M2P override,
626 * set gnttab_set_unmap_op on all of the grant references and perform 699 * set gnttab_set_unmap_op on all of the grant references and perform
@@ -630,7 +703,10 @@ static int dispatch_rw_block_io(struct xen_blkif *blkif,
630 if (xen_blkbk_map(req, pending_req, seg)) 703 if (xen_blkbk_map(req, pending_req, seg))
631 goto fail_flush; 704 goto fail_flush;
632 705
633 /* This corresponding xen_blkif_put is done in __end_block_io_op */ 706 /*
707 * This corresponding xen_blkif_put is done in __end_block_io_op, or
708 * below (in "!bio") if we are handling a BLKIF_OP_DISCARD.
709 */
634 xen_blkif_get(blkif); 710 xen_blkif_get(blkif);
635 711
636 for (i = 0; i < nseg; i++) { 712 for (i = 0; i < nseg; i++) {
@@ -654,7 +730,7 @@ static int dispatch_rw_block_io(struct xen_blkif *blkif,
654 preq.sector_number += seg[i].nsec; 730 preq.sector_number += seg[i].nsec;
655 } 731 }
656 732
657 /* This will be hit if the operation was a flush. */ 733 /* This will be hit if the operation was a flush or discard. */
658 if (!bio) { 734 if (!bio) {
659 BUG_ON(operation != WRITE_FLUSH); 735 BUG_ON(operation != WRITE_FLUSH);
660 736
@@ -685,7 +761,7 @@ static int dispatch_rw_block_io(struct xen_blkif *blkif,
685 761
686 if (operation == READ) 762 if (operation == READ)
687 blkif->st_rd_sect += preq.nr_sects; 763 blkif->st_rd_sect += preq.nr_sects;
688 else if (operation == WRITE || operation == WRITE_FLUSH) 764 else if (operation & WRITE)
689 blkif->st_wr_sect += preq.nr_sects; 765 blkif->st_wr_sect += preq.nr_sects;
690 766
691 return 0; 767 return 0;
@@ -694,7 +770,7 @@ static int dispatch_rw_block_io(struct xen_blkif *blkif,
694 xen_blkbk_unmap(pending_req); 770 xen_blkbk_unmap(pending_req);
695 fail_response: 771 fail_response:
696 /* Haven't submitted any bio's yet. */ 772 /* Haven't submitted any bio's yet. */
697 make_response(blkif, req->id, req->operation, BLKIF_RSP_ERROR); 773 make_response(blkif, req->u.rw.id, req->operation, BLKIF_RSP_ERROR);
698 free_req(pending_req); 774 free_req(pending_req);
699 msleep(1); /* back off a bit */ 775 msleep(1); /* back off a bit */
700 return -EIO; 776 return -EIO;
@@ -765,9 +841,9 @@ static int __init xen_blkif_init(void)
765 841
766 mmap_pages = xen_blkif_reqs * BLKIF_MAX_SEGMENTS_PER_REQUEST; 842 mmap_pages = xen_blkif_reqs * BLKIF_MAX_SEGMENTS_PER_REQUEST;
767 843
768 blkbk->pending_reqs = kmalloc(sizeof(blkbk->pending_reqs[0]) * 844 blkbk->pending_reqs = kzalloc(sizeof(blkbk->pending_reqs[0]) *
769 xen_blkif_reqs, GFP_KERNEL); 845 xen_blkif_reqs, GFP_KERNEL);
770 blkbk->pending_grant_handles = kzalloc(sizeof(blkbk->pending_grant_handles[0]) * 846 blkbk->pending_grant_handles = kmalloc(sizeof(blkbk->pending_grant_handles[0]) *
771 mmap_pages, GFP_KERNEL); 847 mmap_pages, GFP_KERNEL);
772 blkbk->pending_pages = kzalloc(sizeof(blkbk->pending_pages[0]) * 848 blkbk->pending_pages = kzalloc(sizeof(blkbk->pending_pages[0]) *
773 mmap_pages, GFP_KERNEL); 849 mmap_pages, GFP_KERNEL);
@@ -790,8 +866,6 @@ static int __init xen_blkif_init(void)
790 if (rc) 866 if (rc)
791 goto failed_init; 867 goto failed_init;
792 868
793 memset(blkbk->pending_reqs, 0, sizeof(blkbk->pending_reqs));
794
795 INIT_LIST_HEAD(&blkbk->pending_free); 869 INIT_LIST_HEAD(&blkbk->pending_free);
796 spin_lock_init(&blkbk->pending_free_lock); 870 spin_lock_init(&blkbk->pending_free_lock);
797 init_waitqueue_head(&blkbk->pending_free_wq); 871 init_waitqueue_head(&blkbk->pending_free_wq);
diff --git a/drivers/block/xen-blkback/common.h b/drivers/block/xen-blkback/common.h
index 00c57c90e2d6..d0ee7edc9be8 100644
--- a/drivers/block/xen-blkback/common.h
+++ b/drivers/block/xen-blkback/common.h
@@ -27,7 +27,6 @@
27#ifndef __XEN_BLKIF__BACKEND__COMMON_H__ 27#ifndef __XEN_BLKIF__BACKEND__COMMON_H__
28#define __XEN_BLKIF__BACKEND__COMMON_H__ 28#define __XEN_BLKIF__BACKEND__COMMON_H__
29 29
30#include <linux/version.h>
31#include <linux/module.h> 30#include <linux/module.h>
32#include <linux/interrupt.h> 31#include <linux/interrupt.h>
33#include <linux/slab.h> 32#include <linux/slab.h>
@@ -61,32 +60,66 @@ struct blkif_common_response {
61 char dummy; 60 char dummy;
62}; 61};
63 62
64/* i386 protocol version */ 63struct blkif_x86_32_request_rw {
65#pragma pack(push, 4)
66struct blkif_x86_32_request {
67 uint8_t operation; /* BLKIF_OP_??? */
68 uint8_t nr_segments; /* number of segments */ 64 uint8_t nr_segments; /* number of segments */
69 blkif_vdev_t handle; /* only for read/write requests */ 65 blkif_vdev_t handle; /* only for read/write requests */
70 uint64_t id; /* private guest value, echoed in resp */ 66 uint64_t id; /* private guest value, echoed in resp */
71 blkif_sector_t sector_number;/* start sector idx on disk (r/w only) */ 67 blkif_sector_t sector_number;/* start sector idx on disk (r/w only) */
72 struct blkif_request_segment seg[BLKIF_MAX_SEGMENTS_PER_REQUEST]; 68 struct blkif_request_segment seg[BLKIF_MAX_SEGMENTS_PER_REQUEST];
73}; 69} __attribute__((__packed__));
70
71struct blkif_x86_32_request_discard {
72 uint8_t flag; /* BLKIF_DISCARD_SECURE or zero */
73 blkif_vdev_t _pad1; /* was "handle" for read/write requests */
74 uint64_t id; /* private guest value, echoed in resp */
75 blkif_sector_t sector_number;/* start sector idx on disk (r/w only) */
76 uint64_t nr_sectors;
77} __attribute__((__packed__));
78
79struct blkif_x86_32_request {
80 uint8_t operation; /* BLKIF_OP_??? */
81 union {
82 struct blkif_x86_32_request_rw rw;
83 struct blkif_x86_32_request_discard discard;
84 } u;
85} __attribute__((__packed__));
86
87/* i386 protocol version */
88#pragma pack(push, 4)
74struct blkif_x86_32_response { 89struct blkif_x86_32_response {
75 uint64_t id; /* copied from request */ 90 uint64_t id; /* copied from request */
76 uint8_t operation; /* copied from request */ 91 uint8_t operation; /* copied from request */
77 int16_t status; /* BLKIF_RSP_??? */ 92 int16_t status; /* BLKIF_RSP_??? */
78}; 93};
79#pragma pack(pop) 94#pragma pack(pop)
80
81/* x86_64 protocol version */ 95/* x86_64 protocol version */
82struct blkif_x86_64_request { 96
83 uint8_t operation; /* BLKIF_OP_??? */ 97struct blkif_x86_64_request_rw {
84 uint8_t nr_segments; /* number of segments */ 98 uint8_t nr_segments; /* number of segments */
85 blkif_vdev_t handle; /* only for read/write requests */ 99 blkif_vdev_t handle; /* only for read/write requests */
86 uint64_t __attribute__((__aligned__(8))) id; 100 uint32_t _pad1; /* offsetof(blkif_reqest..,u.rw.id)==8 */
101 uint64_t id;
87 blkif_sector_t sector_number;/* start sector idx on disk (r/w only) */ 102 blkif_sector_t sector_number;/* start sector idx on disk (r/w only) */
88 struct blkif_request_segment seg[BLKIF_MAX_SEGMENTS_PER_REQUEST]; 103 struct blkif_request_segment seg[BLKIF_MAX_SEGMENTS_PER_REQUEST];
89}; 104} __attribute__((__packed__));
105
106struct blkif_x86_64_request_discard {
107 uint8_t flag; /* BLKIF_DISCARD_SECURE or zero */
108 blkif_vdev_t _pad1; /* was "handle" for read/write requests */
109 uint32_t _pad2; /* offsetof(blkif_..,u.discard.id)==8 */
110 uint64_t id;
111 blkif_sector_t sector_number;/* start sector idx on disk (r/w only) */
112 uint64_t nr_sectors;
113} __attribute__((__packed__));
114
115struct blkif_x86_64_request {
116 uint8_t operation; /* BLKIF_OP_??? */
117 union {
118 struct blkif_x86_64_request_rw rw;
119 struct blkif_x86_64_request_discard discard;
120 } u;
121} __attribute__((__packed__));
122
90struct blkif_x86_64_response { 123struct blkif_x86_64_response {
91 uint64_t __attribute__((__aligned__(8))) id; 124 uint64_t __attribute__((__aligned__(8))) id;
92 uint8_t operation; /* copied from request */ 125 uint8_t operation; /* copied from request */
@@ -113,6 +146,11 @@ enum blkif_protocol {
113 BLKIF_PROTOCOL_X86_64 = 3, 146 BLKIF_PROTOCOL_X86_64 = 3,
114}; 147};
115 148
149enum blkif_backend_type {
150 BLKIF_BACKEND_PHY = 1,
151 BLKIF_BACKEND_FILE = 2,
152};
153
116struct xen_vbd { 154struct xen_vbd {
117 /* What the domain refers to this vbd as. */ 155 /* What the domain refers to this vbd as. */
118 blkif_vdev_t handle; 156 blkif_vdev_t handle;
@@ -126,6 +164,7 @@ struct xen_vbd {
126 /* Cached size parameter. */ 164 /* Cached size parameter. */
127 sector_t size; 165 sector_t size;
128 bool flush_support; 166 bool flush_support;
167 bool discard_secure;
129}; 168};
130 169
131struct backend_info; 170struct backend_info;
@@ -138,8 +177,9 @@ struct xen_blkif {
138 unsigned int irq; 177 unsigned int irq;
139 /* Comms information. */ 178 /* Comms information. */
140 enum blkif_protocol blk_protocol; 179 enum blkif_protocol blk_protocol;
180 enum blkif_backend_type blk_backend_type;
141 union blkif_back_rings blk_rings; 181 union blkif_back_rings blk_rings;
142 struct vm_struct *blk_ring_area; 182 void *blk_ring;
143 /* The VBD attached to this interface. */ 183 /* The VBD attached to this interface. */
144 struct xen_vbd vbd; 184 struct xen_vbd vbd;
145 /* Back pointer to the backend_info. */ 185 /* Back pointer to the backend_info. */
@@ -149,6 +189,9 @@ struct xen_blkif {
149 atomic_t refcnt; 189 atomic_t refcnt;
150 190
151 wait_queue_head_t wq; 191 wait_queue_head_t wq;
192 /* for barrier (drain) requests */
193 struct completion drain_complete;
194 atomic_t drain;
152 /* One thread per one blkif. */ 195 /* One thread per one blkif. */
153 struct task_struct *xenblkd; 196 struct task_struct *xenblkd;
154 unsigned int waiting_reqs; 197 unsigned int waiting_reqs;
@@ -159,13 +202,11 @@ struct xen_blkif {
159 int st_wr_req; 202 int st_wr_req;
160 int st_oo_req; 203 int st_oo_req;
161 int st_f_req; 204 int st_f_req;
205 int st_ds_req;
162 int st_rd_sect; 206 int st_rd_sect;
163 int st_wr_sect; 207 int st_wr_sect;
164 208
165 wait_queue_head_t waiting_to_free; 209 wait_queue_head_t waiting_to_free;
166
167 grant_handle_t shmem_handle;
168 grant_ref_t shmem_ref;
169}; 210};
170 211
171 212
@@ -182,7 +223,7 @@ struct xen_blkif {
182 223
183struct phys_req { 224struct phys_req {
184 unsigned short dev; 225 unsigned short dev;
185 unsigned short nr_sects; 226 blkif_sector_t nr_sects;
186 struct block_device *bdev; 227 struct block_device *bdev;
187 blkif_sector_t sector_number; 228 blkif_sector_t sector_number;
188}; 229};
@@ -196,6 +237,8 @@ int xen_blkif_schedule(void *arg);
196int xen_blkbk_flush_diskcache(struct xenbus_transaction xbt, 237int xen_blkbk_flush_diskcache(struct xenbus_transaction xbt,
197 struct backend_info *be, int state); 238 struct backend_info *be, int state);
198 239
240int xen_blkbk_barrier(struct xenbus_transaction xbt,
241 struct backend_info *be, int state);
199struct xenbus_device *xen_blkbk_xenbus(struct backend_info *be); 242struct xenbus_device *xen_blkbk_xenbus(struct backend_info *be);
200 243
201static inline void blkif_get_x86_32_req(struct blkif_request *dst, 244static inline void blkif_get_x86_32_req(struct blkif_request *dst,
@@ -203,15 +246,29 @@ static inline void blkif_get_x86_32_req(struct blkif_request *dst,
203{ 246{
204 int i, n = BLKIF_MAX_SEGMENTS_PER_REQUEST; 247 int i, n = BLKIF_MAX_SEGMENTS_PER_REQUEST;
205 dst->operation = src->operation; 248 dst->operation = src->operation;
206 dst->nr_segments = src->nr_segments; 249 switch (src->operation) {
207 dst->handle = src->handle; 250 case BLKIF_OP_READ:
208 dst->id = src->id; 251 case BLKIF_OP_WRITE:
209 dst->u.rw.sector_number = src->sector_number; 252 case BLKIF_OP_WRITE_BARRIER:
210 barrier(); 253 case BLKIF_OP_FLUSH_DISKCACHE:
211 if (n > dst->nr_segments) 254 dst->u.rw.nr_segments = src->u.rw.nr_segments;
212 n = dst->nr_segments; 255 dst->u.rw.handle = src->u.rw.handle;
213 for (i = 0; i < n; i++) 256 dst->u.rw.id = src->u.rw.id;
214 dst->u.rw.seg[i] = src->seg[i]; 257 dst->u.rw.sector_number = src->u.rw.sector_number;
258 barrier();
259 if (n > dst->u.rw.nr_segments)
260 n = dst->u.rw.nr_segments;
261 for (i = 0; i < n; i++)
262 dst->u.rw.seg[i] = src->u.rw.seg[i];
263 break;
264 case BLKIF_OP_DISCARD:
265 dst->u.discard.flag = src->u.discard.flag;
266 dst->u.discard.sector_number = src->u.discard.sector_number;
267 dst->u.discard.nr_sectors = src->u.discard.nr_sectors;
268 break;
269 default:
270 break;
271 }
215} 272}
216 273
217static inline void blkif_get_x86_64_req(struct blkif_request *dst, 274static inline void blkif_get_x86_64_req(struct blkif_request *dst,
@@ -219,15 +276,29 @@ static inline void blkif_get_x86_64_req(struct blkif_request *dst,
219{ 276{
220 int i, n = BLKIF_MAX_SEGMENTS_PER_REQUEST; 277 int i, n = BLKIF_MAX_SEGMENTS_PER_REQUEST;
221 dst->operation = src->operation; 278 dst->operation = src->operation;
222 dst->nr_segments = src->nr_segments; 279 switch (src->operation) {
223 dst->handle = src->handle; 280 case BLKIF_OP_READ:
224 dst->id = src->id; 281 case BLKIF_OP_WRITE:
225 dst->u.rw.sector_number = src->sector_number; 282 case BLKIF_OP_WRITE_BARRIER:
226 barrier(); 283 case BLKIF_OP_FLUSH_DISKCACHE:
227 if (n > dst->nr_segments) 284 dst->u.rw.nr_segments = src->u.rw.nr_segments;
228 n = dst->nr_segments; 285 dst->u.rw.handle = src->u.rw.handle;
229 for (i = 0; i < n; i++) 286 dst->u.rw.id = src->u.rw.id;
230 dst->u.rw.seg[i] = src->seg[i]; 287 dst->u.rw.sector_number = src->u.rw.sector_number;
288 barrier();
289 if (n > dst->u.rw.nr_segments)
290 n = dst->u.rw.nr_segments;
291 for (i = 0; i < n; i++)
292 dst->u.rw.seg[i] = src->u.rw.seg[i];
293 break;
294 case BLKIF_OP_DISCARD:
295 dst->u.discard.flag = src->u.discard.flag;
296 dst->u.discard.sector_number = src->u.discard.sector_number;
297 dst->u.discard.nr_sectors = src->u.discard.nr_sectors;
298 break;
299 default:
300 break;
301 }
231} 302}
232 303
233#endif /* __XEN_BLKIF__BACKEND__COMMON_H__ */ 304#endif /* __XEN_BLKIF__BACKEND__COMMON_H__ */
diff --git a/drivers/block/xen-blkback/xenbus.c b/drivers/block/xen-blkback/xenbus.c
index 5fd2010f7d2b..24a2fb57e5d0 100644
--- a/drivers/block/xen-blkback/xenbus.c
+++ b/drivers/block/xen-blkback/xenbus.c
@@ -114,44 +114,14 @@ static struct xen_blkif *xen_blkif_alloc(domid_t domid)
114 spin_lock_init(&blkif->blk_ring_lock); 114 spin_lock_init(&blkif->blk_ring_lock);
115 atomic_set(&blkif->refcnt, 1); 115 atomic_set(&blkif->refcnt, 1);
116 init_waitqueue_head(&blkif->wq); 116 init_waitqueue_head(&blkif->wq);
117 init_completion(&blkif->drain_complete);
118 atomic_set(&blkif->drain, 0);
117 blkif->st_print = jiffies; 119 blkif->st_print = jiffies;
118 init_waitqueue_head(&blkif->waiting_to_free); 120 init_waitqueue_head(&blkif->waiting_to_free);
119 121
120 return blkif; 122 return blkif;
121} 123}
122 124
123static int map_frontend_page(struct xen_blkif *blkif, unsigned long shared_page)
124{
125 struct gnttab_map_grant_ref op;
126
127 gnttab_set_map_op(&op, (unsigned long)blkif->blk_ring_area->addr,
128 GNTMAP_host_map, shared_page, blkif->domid);
129
130 if (HYPERVISOR_grant_table_op(GNTTABOP_map_grant_ref, &op, 1))
131 BUG();
132
133 if (op.status) {
134 DPRINTK("Grant table operation failure !\n");
135 return op.status;
136 }
137
138 blkif->shmem_ref = shared_page;
139 blkif->shmem_handle = op.handle;
140
141 return 0;
142}
143
144static void unmap_frontend_page(struct xen_blkif *blkif)
145{
146 struct gnttab_unmap_grant_ref op;
147
148 gnttab_set_unmap_op(&op, (unsigned long)blkif->blk_ring_area->addr,
149 GNTMAP_host_map, blkif->shmem_handle);
150
151 if (HYPERVISOR_grant_table_op(GNTTABOP_unmap_grant_ref, &op, 1))
152 BUG();
153}
154
155static int xen_blkif_map(struct xen_blkif *blkif, unsigned long shared_page, 125static int xen_blkif_map(struct xen_blkif *blkif, unsigned long shared_page,
156 unsigned int evtchn) 126 unsigned int evtchn)
157{ 127{
@@ -161,35 +131,29 @@ static int xen_blkif_map(struct xen_blkif *blkif, unsigned long shared_page,
161 if (blkif->irq) 131 if (blkif->irq)
162 return 0; 132 return 0;
163 133
164 blkif->blk_ring_area = alloc_vm_area(PAGE_SIZE); 134 err = xenbus_map_ring_valloc(blkif->be->dev, shared_page, &blkif->blk_ring);
165 if (!blkif->blk_ring_area) 135 if (err < 0)
166 return -ENOMEM;
167
168 err = map_frontend_page(blkif, shared_page);
169 if (err) {
170 free_vm_area(blkif->blk_ring_area);
171 return err; 136 return err;
172 }
173 137
174 switch (blkif->blk_protocol) { 138 switch (blkif->blk_protocol) {
175 case BLKIF_PROTOCOL_NATIVE: 139 case BLKIF_PROTOCOL_NATIVE:
176 { 140 {
177 struct blkif_sring *sring; 141 struct blkif_sring *sring;
178 sring = (struct blkif_sring *)blkif->blk_ring_area->addr; 142 sring = (struct blkif_sring *)blkif->blk_ring;
179 BACK_RING_INIT(&blkif->blk_rings.native, sring, PAGE_SIZE); 143 BACK_RING_INIT(&blkif->blk_rings.native, sring, PAGE_SIZE);
180 break; 144 break;
181 } 145 }
182 case BLKIF_PROTOCOL_X86_32: 146 case BLKIF_PROTOCOL_X86_32:
183 { 147 {
184 struct blkif_x86_32_sring *sring_x86_32; 148 struct blkif_x86_32_sring *sring_x86_32;
185 sring_x86_32 = (struct blkif_x86_32_sring *)blkif->blk_ring_area->addr; 149 sring_x86_32 = (struct blkif_x86_32_sring *)blkif->blk_ring;
186 BACK_RING_INIT(&blkif->blk_rings.x86_32, sring_x86_32, PAGE_SIZE); 150 BACK_RING_INIT(&blkif->blk_rings.x86_32, sring_x86_32, PAGE_SIZE);
187 break; 151 break;
188 } 152 }
189 case BLKIF_PROTOCOL_X86_64: 153 case BLKIF_PROTOCOL_X86_64:
190 { 154 {
191 struct blkif_x86_64_sring *sring_x86_64; 155 struct blkif_x86_64_sring *sring_x86_64;
192 sring_x86_64 = (struct blkif_x86_64_sring *)blkif->blk_ring_area->addr; 156 sring_x86_64 = (struct blkif_x86_64_sring *)blkif->blk_ring;
193 BACK_RING_INIT(&blkif->blk_rings.x86_64, sring_x86_64, PAGE_SIZE); 157 BACK_RING_INIT(&blkif->blk_rings.x86_64, sring_x86_64, PAGE_SIZE);
194 break; 158 break;
195 } 159 }
@@ -201,8 +165,7 @@ static int xen_blkif_map(struct xen_blkif *blkif, unsigned long shared_page,
201 xen_blkif_be_int, 0, 165 xen_blkif_be_int, 0,
202 "blkif-backend", blkif); 166 "blkif-backend", blkif);
203 if (err < 0) { 167 if (err < 0) {
204 unmap_frontend_page(blkif); 168 xenbus_unmap_ring_vfree(blkif->be->dev, blkif->blk_ring);
205 free_vm_area(blkif->blk_ring_area);
206 blkif->blk_rings.common.sring = NULL; 169 blkif->blk_rings.common.sring = NULL;
207 return err; 170 return err;
208 } 171 }
@@ -228,8 +191,7 @@ static void xen_blkif_disconnect(struct xen_blkif *blkif)
228 } 191 }
229 192
230 if (blkif->blk_rings.common.sring) { 193 if (blkif->blk_rings.common.sring) {
231 unmap_frontend_page(blkif); 194 xenbus_unmap_ring_vfree(blkif->be->dev, blkif->blk_ring);
232 free_vm_area(blkif->blk_ring_area);
233 blkif->blk_rings.common.sring = NULL; 195 blkif->blk_rings.common.sring = NULL;
234 } 196 }
235} 197}
@@ -272,6 +234,7 @@ VBD_SHOW(oo_req, "%d\n", be->blkif->st_oo_req);
272VBD_SHOW(rd_req, "%d\n", be->blkif->st_rd_req); 234VBD_SHOW(rd_req, "%d\n", be->blkif->st_rd_req);
273VBD_SHOW(wr_req, "%d\n", be->blkif->st_wr_req); 235VBD_SHOW(wr_req, "%d\n", be->blkif->st_wr_req);
274VBD_SHOW(f_req, "%d\n", be->blkif->st_f_req); 236VBD_SHOW(f_req, "%d\n", be->blkif->st_f_req);
237VBD_SHOW(ds_req, "%d\n", be->blkif->st_ds_req);
275VBD_SHOW(rd_sect, "%d\n", be->blkif->st_rd_sect); 238VBD_SHOW(rd_sect, "%d\n", be->blkif->st_rd_sect);
276VBD_SHOW(wr_sect, "%d\n", be->blkif->st_wr_sect); 239VBD_SHOW(wr_sect, "%d\n", be->blkif->st_wr_sect);
277 240
@@ -280,6 +243,7 @@ static struct attribute *xen_vbdstat_attrs[] = {
280 &dev_attr_rd_req.attr, 243 &dev_attr_rd_req.attr,
281 &dev_attr_wr_req.attr, 244 &dev_attr_wr_req.attr,
282 &dev_attr_f_req.attr, 245 &dev_attr_f_req.attr,
246 &dev_attr_ds_req.attr,
283 &dev_attr_rd_sect.attr, 247 &dev_attr_rd_sect.attr,
284 &dev_attr_wr_sect.attr, 248 &dev_attr_wr_sect.attr,
285 NULL 249 NULL
@@ -374,6 +338,9 @@ static int xen_vbd_create(struct xen_blkif *blkif, blkif_vdev_t handle,
374 if (q && q->flush_flags) 338 if (q && q->flush_flags)
375 vbd->flush_support = true; 339 vbd->flush_support = true;
376 340
341 if (q && blk_queue_secdiscard(q))
342 vbd->discard_secure = true;
343
377 DPRINTK("Successful creation of handle=%04x (dom=%u)\n", 344 DPRINTK("Successful creation of handle=%04x (dom=%u)\n",
378 handle, blkif->domid); 345 handle, blkif->domid);
379 return 0; 346 return 0;
@@ -419,6 +386,82 @@ int xen_blkbk_flush_diskcache(struct xenbus_transaction xbt,
419 return err; 386 return err;
420} 387}
421 388
389int xen_blkbk_discard(struct xenbus_transaction xbt, struct backend_info *be)
390{
391 struct xenbus_device *dev = be->dev;
392 struct xen_blkif *blkif = be->blkif;
393 char *type;
394 int err;
395 int state = 0;
396
397 type = xenbus_read(XBT_NIL, dev->nodename, "type", NULL);
398 if (!IS_ERR(type)) {
399 if (strncmp(type, "file", 4) == 0) {
400 state = 1;
401 blkif->blk_backend_type = BLKIF_BACKEND_FILE;
402 }
403 if (strncmp(type, "phy", 3) == 0) {
404 struct block_device *bdev = be->blkif->vbd.bdev;
405 struct request_queue *q = bdev_get_queue(bdev);
406 if (blk_queue_discard(q)) {
407 err = xenbus_printf(xbt, dev->nodename,
408 "discard-granularity", "%u",
409 q->limits.discard_granularity);
410 if (err) {
411 xenbus_dev_fatal(dev, err,
412 "writing discard-granularity");
413 goto kfree;
414 }
415 err = xenbus_printf(xbt, dev->nodename,
416 "discard-alignment", "%u",
417 q->limits.discard_alignment);
418 if (err) {
419 xenbus_dev_fatal(dev, err,
420 "writing discard-alignment");
421 goto kfree;
422 }
423 state = 1;
424 blkif->blk_backend_type = BLKIF_BACKEND_PHY;
425 }
426 /* Optional. */
427 err = xenbus_printf(xbt, dev->nodename,
428 "discard-secure", "%d",
429 blkif->vbd.discard_secure);
430 if (err) {
431 xenbus_dev_fatal(dev, err,
432 "writting discard-secure");
433 goto kfree;
434 }
435 }
436 } else {
437 err = PTR_ERR(type);
438 xenbus_dev_fatal(dev, err, "reading type");
439 goto out;
440 }
441
442 err = xenbus_printf(xbt, dev->nodename, "feature-discard",
443 "%d", state);
444 if (err)
445 xenbus_dev_fatal(dev, err, "writing feature-discard");
446kfree:
447 kfree(type);
448out:
449 return err;
450}
451int xen_blkbk_barrier(struct xenbus_transaction xbt,
452 struct backend_info *be, int state)
453{
454 struct xenbus_device *dev = be->dev;
455 int err;
456
457 err = xenbus_printf(xbt, dev->nodename, "feature-barrier",
458 "%d", state);
459 if (err)
460 xenbus_dev_fatal(dev, err, "writing feature-barrier");
461
462 return err;
463}
464
422/* 465/*
423 * Entry point to this code when a new device is created. Allocate the basic 466 * Entry point to this code when a new device is created. Allocate the basic
424 * structures, and watch the store waiting for the hotplug scripts to tell us 467 * structures, and watch the store waiting for the hotplug scripts to tell us
@@ -582,7 +625,7 @@ static void frontend_changed(struct xenbus_device *dev,
582 case XenbusStateConnected: 625 case XenbusStateConnected:
583 /* 626 /*
584 * Ensure we connect even when two watches fire in 627 * Ensure we connect even when two watches fire in
585 * close successsion and we miss the intermediate value 628 * close succession and we miss the intermediate value
586 * of frontend_state. 629 * of frontend_state.
587 */ 630 */
588 if (dev->state == XenbusStateConnected) 631 if (dev->state == XenbusStateConnected)
@@ -650,6 +693,11 @@ again:
650 if (err) 693 if (err)
651 goto abort; 694 goto abort;
652 695
696 err = xen_blkbk_discard(xbt, be);
697
698 /* If we can't advertise it is OK. */
699 err = xen_blkbk_barrier(xbt, be, be->blkif->vbd.flush_support);
700
653 err = xenbus_printf(xbt, dev->nodename, "sectors", "%llu", 701 err = xenbus_printf(xbt, dev->nodename, "sectors", "%llu",
654 (unsigned long long)vbd_sz(&be->blkif->vbd)); 702 (unsigned long long)vbd_sz(&be->blkif->vbd));
655 if (err) { 703 if (err) {
@@ -751,17 +799,14 @@ static const struct xenbus_device_id xen_blkbk_ids[] = {
751}; 799};
752 800
753 801
754static struct xenbus_driver xen_blkbk = { 802static DEFINE_XENBUS_DRIVER(xen_blkbk, ,
755 .name = "vbd",
756 .owner = THIS_MODULE,
757 .ids = xen_blkbk_ids,
758 .probe = xen_blkbk_probe, 803 .probe = xen_blkbk_probe,
759 .remove = xen_blkbk_remove, 804 .remove = xen_blkbk_remove,
760 .otherend_changed = frontend_changed 805 .otherend_changed = frontend_changed
761}; 806);
762 807
763 808
764int xen_blkif_xenbus_init(void) 809int xen_blkif_xenbus_init(void)
765{ 810{
766 return xenbus_register_backend(&xen_blkbk); 811 return xenbus_register_backend(&xen_blkbk_driver);
767} 812}
diff --git a/drivers/block/xen-blkfront.c b/drivers/block/xen-blkfront.c
index 9ea8c2576c70..2f22874c0a37 100644
--- a/drivers/block/xen-blkfront.c
+++ b/drivers/block/xen-blkfront.c
@@ -98,6 +98,10 @@ struct blkfront_info
98 unsigned long shadow_free; 98 unsigned long shadow_free;
99 unsigned int feature_flush; 99 unsigned int feature_flush;
100 unsigned int flush_op; 100 unsigned int flush_op;
101 unsigned int feature_discard:1;
102 unsigned int feature_secdiscard:1;
103 unsigned int discard_granularity;
104 unsigned int discard_alignment;
101 int is_ready; 105 int is_ready;
102}; 106};
103 107
@@ -132,15 +136,15 @@ static int get_id_from_freelist(struct blkfront_info *info)
132{ 136{
133 unsigned long free = info->shadow_free; 137 unsigned long free = info->shadow_free;
134 BUG_ON(free >= BLK_RING_SIZE); 138 BUG_ON(free >= BLK_RING_SIZE);
135 info->shadow_free = info->shadow[free].req.id; 139 info->shadow_free = info->shadow[free].req.u.rw.id;
136 info->shadow[free].req.id = 0x0fffffee; /* debug */ 140 info->shadow[free].req.u.rw.id = 0x0fffffee; /* debug */
137 return free; 141 return free;
138} 142}
139 143
140static void add_id_to_freelist(struct blkfront_info *info, 144static void add_id_to_freelist(struct blkfront_info *info,
141 unsigned long id) 145 unsigned long id)
142{ 146{
143 info->shadow[id].req.id = info->shadow_free; 147 info->shadow[id].req.u.rw.id = info->shadow_free;
144 info->shadow[id].request = NULL; 148 info->shadow[id].request = NULL;
145 info->shadow_free = id; 149 info->shadow_free = id;
146} 150}
@@ -153,7 +157,7 @@ static int xlbd_reserve_minors(unsigned int minor, unsigned int nr)
153 if (end > nr_minors) { 157 if (end > nr_minors) {
154 unsigned long *bitmap, *old; 158 unsigned long *bitmap, *old;
155 159
156 bitmap = kzalloc(BITS_TO_LONGS(end) * sizeof(*bitmap), 160 bitmap = kcalloc(BITS_TO_LONGS(end), sizeof(*bitmap),
157 GFP_KERNEL); 161 GFP_KERNEL);
158 if (bitmap == NULL) 162 if (bitmap == NULL)
159 return -ENOMEM; 163 return -ENOMEM;
@@ -284,9 +288,9 @@ static int blkif_queue_request(struct request *req)
284 id = get_id_from_freelist(info); 288 id = get_id_from_freelist(info);
285 info->shadow[id].request = req; 289 info->shadow[id].request = req;
286 290
287 ring_req->id = id; 291 ring_req->u.rw.id = id;
288 ring_req->u.rw.sector_number = (blkif_sector_t)blk_rq_pos(req); 292 ring_req->u.rw.sector_number = (blkif_sector_t)blk_rq_pos(req);
289 ring_req->handle = info->handle; 293 ring_req->u.rw.handle = info->handle;
290 294
291 ring_req->operation = rq_data_dir(req) ? 295 ring_req->operation = rq_data_dir(req) ?
292 BLKIF_OP_WRITE : BLKIF_OP_READ; 296 BLKIF_OP_WRITE : BLKIF_OP_READ;
@@ -302,29 +306,41 @@ static int blkif_queue_request(struct request *req)
302 ring_req->operation = info->flush_op; 306 ring_req->operation = info->flush_op;
303 } 307 }
304 308
305 ring_req->nr_segments = blk_rq_map_sg(req->q, req, info->sg); 309 if (unlikely(req->cmd_flags & (REQ_DISCARD | REQ_SECURE))) {
306 BUG_ON(ring_req->nr_segments > BLKIF_MAX_SEGMENTS_PER_REQUEST); 310 /* id, sector_number and handle are set above. */
307 311 ring_req->operation = BLKIF_OP_DISCARD;
308 for_each_sg(info->sg, sg, ring_req->nr_segments, i) { 312 ring_req->u.discard.nr_sectors = blk_rq_sectors(req);
309 buffer_mfn = pfn_to_mfn(page_to_pfn(sg_page(sg))); 313 if ((req->cmd_flags & REQ_SECURE) && info->feature_secdiscard)
310 fsect = sg->offset >> 9; 314 ring_req->u.discard.flag = BLKIF_DISCARD_SECURE;
311 lsect = fsect + (sg->length >> 9) - 1; 315 else
312 /* install a grant reference. */ 316 ring_req->u.discard.flag = 0;
313 ref = gnttab_claim_grant_reference(&gref_head); 317 } else {
314 BUG_ON(ref == -ENOSPC); 318 ring_req->u.rw.nr_segments = blk_rq_map_sg(req->q, req,
315 319 info->sg);
316 gnttab_grant_foreign_access_ref( 320 BUG_ON(ring_req->u.rw.nr_segments >
317 ref, 321 BLKIF_MAX_SEGMENTS_PER_REQUEST);
318 info->xbdev->otherend_id, 322
319 buffer_mfn, 323 for_each_sg(info->sg, sg, ring_req->u.rw.nr_segments, i) {
320 rq_data_dir(req) ); 324 buffer_mfn = pfn_to_mfn(page_to_pfn(sg_page(sg)));
321 325 fsect = sg->offset >> 9;
322 info->shadow[id].frame[i] = mfn_to_pfn(buffer_mfn); 326 lsect = fsect + (sg->length >> 9) - 1;
323 ring_req->u.rw.seg[i] = 327 /* install a grant reference. */
324 (struct blkif_request_segment) { 328 ref = gnttab_claim_grant_reference(&gref_head);
325 .gref = ref, 329 BUG_ON(ref == -ENOSPC);
326 .first_sect = fsect, 330
327 .last_sect = lsect }; 331 gnttab_grant_foreign_access_ref(
332 ref,
333 info->xbdev->otherend_id,
334 buffer_mfn,
335 rq_data_dir(req));
336
337 info->shadow[id].frame[i] = mfn_to_pfn(buffer_mfn);
338 ring_req->u.rw.seg[i] =
339 (struct blkif_request_segment) {
340 .gref = ref,
341 .first_sect = fsect,
342 .last_sect = lsect };
343 }
328 } 344 }
329 345
330 info->ring.req_prod_pvt++; 346 info->ring.req_prod_pvt++;
@@ -370,7 +386,9 @@ static void do_blkif_request(struct request_queue *rq)
370 386
371 blk_start_request(req); 387 blk_start_request(req);
372 388
373 if (req->cmd_type != REQ_TYPE_FS) { 389 if ((req->cmd_type != REQ_TYPE_FS) ||
390 ((req->cmd_flags & (REQ_FLUSH | REQ_FUA)) &&
391 !info->flush_op)) {
374 __blk_end_request_all(req, -EIO); 392 __blk_end_request_all(req, -EIO);
375 continue; 393 continue;
376 } 394 }
@@ -399,6 +417,7 @@ wait:
399static int xlvbd_init_blk_queue(struct gendisk *gd, u16 sector_size) 417static int xlvbd_init_blk_queue(struct gendisk *gd, u16 sector_size)
400{ 418{
401 struct request_queue *rq; 419 struct request_queue *rq;
420 struct blkfront_info *info = gd->private_data;
402 421
403 rq = blk_init_queue(do_blkif_request, &blkif_io_lock); 422 rq = blk_init_queue(do_blkif_request, &blkif_io_lock);
404 if (rq == NULL) 423 if (rq == NULL)
@@ -406,6 +425,15 @@ static int xlvbd_init_blk_queue(struct gendisk *gd, u16 sector_size)
406 425
407 queue_flag_set_unlocked(QUEUE_FLAG_VIRT, rq); 426 queue_flag_set_unlocked(QUEUE_FLAG_VIRT, rq);
408 427
428 if (info->feature_discard) {
429 queue_flag_set_unlocked(QUEUE_FLAG_DISCARD, rq);
430 blk_queue_max_discard_sectors(rq, get_capacity(gd));
431 rq->limits.discard_granularity = info->discard_granularity;
432 rq->limits.discard_alignment = info->discard_alignment;
433 if (info->feature_secdiscard)
434 queue_flag_set_unlocked(QUEUE_FLAG_SECDISCARD, rq);
435 }
436
409 /* Hard sector size and max sectors impersonate the equiv. hardware. */ 437 /* Hard sector size and max sectors impersonate the equiv. hardware. */
410 blk_queue_logical_block_size(rq, sector_size); 438 blk_queue_logical_block_size(rq, sector_size);
411 blk_queue_max_hw_sectors(rq, 512); 439 blk_queue_max_hw_sectors(rq, 512);
@@ -685,7 +713,9 @@ static void blkif_free(struct blkfront_info *info, int suspend)
685static void blkif_completion(struct blk_shadow *s) 713static void blkif_completion(struct blk_shadow *s)
686{ 714{
687 int i; 715 int i;
688 for (i = 0; i < s->req.nr_segments; i++) 716 /* Do not let BLKIF_OP_DISCARD as nr_segment is in the same place
717 * flag. */
718 for (i = 0; i < s->req.u.rw.nr_segments; i++)
689 gnttab_end_foreign_access(s->req.u.rw.seg[i].gref, 0, 0UL); 719 gnttab_end_foreign_access(s->req.u.rw.seg[i].gref, 0, 0UL);
690} 720}
691 721
@@ -716,12 +746,26 @@ static irqreturn_t blkif_interrupt(int irq, void *dev_id)
716 id = bret->id; 746 id = bret->id;
717 req = info->shadow[id].request; 747 req = info->shadow[id].request;
718 748
719 blkif_completion(&info->shadow[id]); 749 if (bret->operation != BLKIF_OP_DISCARD)
750 blkif_completion(&info->shadow[id]);
720 751
721 add_id_to_freelist(info, id); 752 add_id_to_freelist(info, id);
722 753
723 error = (bret->status == BLKIF_RSP_OKAY) ? 0 : -EIO; 754 error = (bret->status == BLKIF_RSP_OKAY) ? 0 : -EIO;
724 switch (bret->operation) { 755 switch (bret->operation) {
756 case BLKIF_OP_DISCARD:
757 if (unlikely(bret->status == BLKIF_RSP_EOPNOTSUPP)) {
758 struct request_queue *rq = info->rq;
759 printk(KERN_WARNING "blkfront: %s: discard op failed\n",
760 info->gd->disk_name);
761 error = -EOPNOTSUPP;
762 info->feature_discard = 0;
763 info->feature_secdiscard = 0;
764 queue_flag_clear(QUEUE_FLAG_DISCARD, rq);
765 queue_flag_clear(QUEUE_FLAG_SECDISCARD, rq);
766 }
767 __blk_end_request_all(req, error);
768 break;
725 case BLKIF_OP_FLUSH_DISKCACHE: 769 case BLKIF_OP_FLUSH_DISKCACHE:
726 case BLKIF_OP_WRITE_BARRIER: 770 case BLKIF_OP_WRITE_BARRIER:
727 if (unlikely(bret->status == BLKIF_RSP_EOPNOTSUPP)) { 771 if (unlikely(bret->status == BLKIF_RSP_EOPNOTSUPP)) {
@@ -732,7 +776,7 @@ static irqreturn_t blkif_interrupt(int irq, void *dev_id)
732 error = -EOPNOTSUPP; 776 error = -EOPNOTSUPP;
733 } 777 }
734 if (unlikely(bret->status == BLKIF_RSP_ERROR && 778 if (unlikely(bret->status == BLKIF_RSP_ERROR &&
735 info->shadow[id].req.nr_segments == 0)) { 779 info->shadow[id].req.u.rw.nr_segments == 0)) {
736 printk(KERN_WARNING "blkfront: %s: empty write %s op failed\n", 780 printk(KERN_WARNING "blkfront: %s: empty write %s op failed\n",
737 info->flush_op == BLKIF_OP_WRITE_BARRIER ? 781 info->flush_op == BLKIF_OP_WRITE_BARRIER ?
738 "barrier" : "flush disk cache", 782 "barrier" : "flush disk cache",
@@ -953,8 +997,8 @@ static int blkfront_probe(struct xenbus_device *dev,
953 INIT_WORK(&info->work, blkif_restart_queue); 997 INIT_WORK(&info->work, blkif_restart_queue);
954 998
955 for (i = 0; i < BLK_RING_SIZE; i++) 999 for (i = 0; i < BLK_RING_SIZE; i++)
956 info->shadow[i].req.id = i+1; 1000 info->shadow[i].req.u.rw.id = i+1;
957 info->shadow[BLK_RING_SIZE-1].req.id = 0x0fffffff; 1001 info->shadow[BLK_RING_SIZE-1].req.u.rw.id = 0x0fffffff;
958 1002
959 /* Front end dir is a number, which is used as the id. */ 1003 /* Front end dir is a number, which is used as the id. */
960 info->handle = simple_strtoul(strrchr(dev->nodename, '/')+1, NULL, 0); 1004 info->handle = simple_strtoul(strrchr(dev->nodename, '/')+1, NULL, 0);
@@ -988,9 +1032,9 @@ static int blkif_recover(struct blkfront_info *info)
988 /* Stage 2: Set up free list. */ 1032 /* Stage 2: Set up free list. */
989 memset(&info->shadow, 0, sizeof(info->shadow)); 1033 memset(&info->shadow, 0, sizeof(info->shadow));
990 for (i = 0; i < BLK_RING_SIZE; i++) 1034 for (i = 0; i < BLK_RING_SIZE; i++)
991 info->shadow[i].req.id = i+1; 1035 info->shadow[i].req.u.rw.id = i+1;
992 info->shadow_free = info->ring.req_prod_pvt; 1036 info->shadow_free = info->ring.req_prod_pvt;
993 info->shadow[BLK_RING_SIZE-1].req.id = 0x0fffffff; 1037 info->shadow[BLK_RING_SIZE-1].req.u.rw.id = 0x0fffffff;
994 1038
995 /* Stage 3: Find pending requests and requeue them. */ 1039 /* Stage 3: Find pending requests and requeue them. */
996 for (i = 0; i < BLK_RING_SIZE; i++) { 1040 for (i = 0; i < BLK_RING_SIZE; i++) {
@@ -1003,17 +1047,19 @@ static int blkif_recover(struct blkfront_info *info)
1003 *req = copy[i].req; 1047 *req = copy[i].req;
1004 1048
1005 /* We get a new request id, and must reset the shadow state. */ 1049 /* We get a new request id, and must reset the shadow state. */
1006 req->id = get_id_from_freelist(info); 1050 req->u.rw.id = get_id_from_freelist(info);
1007 memcpy(&info->shadow[req->id], &copy[i], sizeof(copy[i])); 1051 memcpy(&info->shadow[req->u.rw.id], &copy[i], sizeof(copy[i]));
1008 1052
1053 if (req->operation != BLKIF_OP_DISCARD) {
1009 /* Rewrite any grant references invalidated by susp/resume. */ 1054 /* Rewrite any grant references invalidated by susp/resume. */
1010 for (j = 0; j < req->nr_segments; j++) 1055 for (j = 0; j < req->u.rw.nr_segments; j++)
1011 gnttab_grant_foreign_access_ref( 1056 gnttab_grant_foreign_access_ref(
1012 req->u.rw.seg[j].gref, 1057 req->u.rw.seg[j].gref,
1013 info->xbdev->otherend_id, 1058 info->xbdev->otherend_id,
1014 pfn_to_mfn(info->shadow[req->id].frame[j]), 1059 pfn_to_mfn(info->shadow[req->u.rw.id].frame[j]),
1015 rq_data_dir(info->shadow[req->id].request)); 1060 rq_data_dir(info->shadow[req->u.rw.id].request));
1016 info->shadow[req->id].req = *req; 1061 }
1062 info->shadow[req->u.rw.id].req = *req;
1017 1063
1018 info->ring.req_prod_pvt++; 1064 info->ring.req_prod_pvt++;
1019 } 1065 }
@@ -1098,6 +1144,41 @@ blkfront_closing(struct blkfront_info *info)
1098 bdput(bdev); 1144 bdput(bdev);
1099} 1145}
1100 1146
1147static void blkfront_setup_discard(struct blkfront_info *info)
1148{
1149 int err;
1150 char *type;
1151 unsigned int discard_granularity;
1152 unsigned int discard_alignment;
1153 unsigned int discard_secure;
1154
1155 type = xenbus_read(XBT_NIL, info->xbdev->otherend, "type", NULL);
1156 if (IS_ERR(type))
1157 return;
1158
1159 info->feature_secdiscard = 0;
1160 if (strncmp(type, "phy", 3) == 0) {
1161 err = xenbus_gather(XBT_NIL, info->xbdev->otherend,
1162 "discard-granularity", "%u", &discard_granularity,
1163 "discard-alignment", "%u", &discard_alignment,
1164 NULL);
1165 if (!err) {
1166 info->feature_discard = 1;
1167 info->discard_granularity = discard_granularity;
1168 info->discard_alignment = discard_alignment;
1169 }
1170 err = xenbus_gather(XBT_NIL, info->xbdev->otherend,
1171 "discard-secure", "%d", &discard_secure,
1172 NULL);
1173 if (!err)
1174 info->feature_secdiscard = discard_secure;
1175
1176 } else if (strncmp(type, "file", 4) == 0)
1177 info->feature_discard = 1;
1178
1179 kfree(type);
1180}
1181
1101/* 1182/*
1102 * Invoked when the backend is finally 'ready' (and has told produced 1183 * Invoked when the backend is finally 'ready' (and has told produced
1103 * the details about the physical device - #sectors, size, etc). 1184 * the details about the physical device - #sectors, size, etc).
@@ -1108,7 +1189,7 @@ static void blkfront_connect(struct blkfront_info *info)
1108 unsigned long sector_size; 1189 unsigned long sector_size;
1109 unsigned int binfo; 1190 unsigned int binfo;
1110 int err; 1191 int err;
1111 int barrier, flush; 1192 int barrier, flush, discard;
1112 1193
1113 switch (info->connected) { 1194 switch (info->connected) {
1114 case BLKIF_STATE_CONNECTED: 1195 case BLKIF_STATE_CONNECTED:
@@ -1178,7 +1259,14 @@ static void blkfront_connect(struct blkfront_info *info)
1178 info->feature_flush = REQ_FLUSH; 1259 info->feature_flush = REQ_FLUSH;
1179 info->flush_op = BLKIF_OP_FLUSH_DISKCACHE; 1260 info->flush_op = BLKIF_OP_FLUSH_DISKCACHE;
1180 } 1261 }
1181 1262
1263 err = xenbus_gather(XBT_NIL, info->xbdev->otherend,
1264 "feature-discard", "%d", &discard,
1265 NULL);
1266
1267 if (!err && discard)
1268 blkfront_setup_discard(info);
1269
1182 err = xlvbd_alloc_gendisk(sectors, info, binfo, sector_size); 1270 err = xlvbd_alloc_gendisk(sectors, info, binfo, sector_size);
1183 if (err) { 1271 if (err) {
1184 xenbus_dev_fatal(info->xbdev, err, "xlvbd_add at %s", 1272 xenbus_dev_fatal(info->xbdev, err, "xlvbd_add at %s",
@@ -1372,19 +1460,18 @@ static const struct xenbus_device_id blkfront_ids[] = {
1372 { "" } 1460 { "" }
1373}; 1461};
1374 1462
1375static struct xenbus_driver blkfront = { 1463static DEFINE_XENBUS_DRIVER(blkfront, ,
1376 .name = "vbd",
1377 .owner = THIS_MODULE,
1378 .ids = blkfront_ids,
1379 .probe = blkfront_probe, 1464 .probe = blkfront_probe,
1380 .remove = blkfront_remove, 1465 .remove = blkfront_remove,
1381 .resume = blkfront_resume, 1466 .resume = blkfront_resume,
1382 .otherend_changed = blkback_changed, 1467 .otherend_changed = blkback_changed,
1383 .is_ready = blkfront_is_ready, 1468 .is_ready = blkfront_is_ready,
1384}; 1469);
1385 1470
1386static int __init xlblk_init(void) 1471static int __init xlblk_init(void)
1387{ 1472{
1473 int ret;
1474
1388 if (!xen_domain()) 1475 if (!xen_domain())
1389 return -ENODEV; 1476 return -ENODEV;
1390 1477
@@ -1394,14 +1481,20 @@ static int __init xlblk_init(void)
1394 return -ENODEV; 1481 return -ENODEV;
1395 } 1482 }
1396 1483
1397 return xenbus_register_frontend(&blkfront); 1484 ret = xenbus_register_frontend(&blkfront_driver);
1485 if (ret) {
1486 unregister_blkdev(XENVBD_MAJOR, DEV_NAME);
1487 return ret;
1488 }
1489
1490 return 0;
1398} 1491}
1399module_init(xlblk_init); 1492module_init(xlblk_init);
1400 1493
1401 1494
1402static void __exit xlblk_exit(void) 1495static void __exit xlblk_exit(void)
1403{ 1496{
1404 return xenbus_unregister_driver(&blkfront); 1497 return xenbus_unregister_driver(&blkfront_driver);
1405} 1498}
1406module_exit(xlblk_exit); 1499module_exit(xlblk_exit);
1407 1500
diff --git a/drivers/block/xsysace.c b/drivers/block/xsysace.c
index fb1975d82a73..1a17e338735e 100644
--- a/drivers/block/xsysace.c
+++ b/drivers/block/xsysace.c
@@ -456,7 +456,7 @@ static inline void ace_fsm_yieldirq(struct ace_device *ace)
456{ 456{
457 dev_dbg(ace->dev, "ace_fsm_yieldirq()\n"); 457 dev_dbg(ace->dev, "ace_fsm_yieldirq()\n");
458 458
459 if (ace->irq == NO_IRQ) 459 if (!ace->irq)
460 /* No IRQ assigned, so need to poll */ 460 /* No IRQ assigned, so need to poll */
461 tasklet_schedule(&ace->fsm_tasklet); 461 tasklet_schedule(&ace->fsm_tasklet);
462 ace->fsm_continue_flag = 0; 462 ace->fsm_continue_flag = 0;
@@ -1034,12 +1034,12 @@ static int __devinit ace_setup(struct ace_device *ace)
1034 ACE_CTRL_DATABUFRDYIRQ | ACE_CTRL_ERRORIRQ); 1034 ACE_CTRL_DATABUFRDYIRQ | ACE_CTRL_ERRORIRQ);
1035 1035
1036 /* Now we can hook up the irq handler */ 1036 /* Now we can hook up the irq handler */
1037 if (ace->irq != NO_IRQ) { 1037 if (ace->irq) {
1038 rc = request_irq(ace->irq, ace_interrupt, 0, "systemace", ace); 1038 rc = request_irq(ace->irq, ace_interrupt, 0, "systemace", ace);
1039 if (rc) { 1039 if (rc) {
1040 /* Failure - fall back to polled mode */ 1040 /* Failure - fall back to polled mode */
1041 dev_err(ace->dev, "request_irq failed\n"); 1041 dev_err(ace->dev, "request_irq failed\n");
1042 ace->irq = NO_IRQ; 1042 ace->irq = 0;
1043 } 1043 }
1044 } 1044 }
1045 1045
@@ -1086,7 +1086,7 @@ static void __devexit ace_teardown(struct ace_device *ace)
1086 1086
1087 tasklet_kill(&ace->fsm_tasklet); 1087 tasklet_kill(&ace->fsm_tasklet);
1088 1088
1089 if (ace->irq != NO_IRQ) 1089 if (ace->irq)
1090 free_irq(ace->irq, ace); 1090 free_irq(ace->irq, ace);
1091 1091
1092 iounmap(ace->baseaddr); 1092 iounmap(ace->baseaddr);
@@ -1156,7 +1156,7 @@ static int __devinit ace_probe(struct platform_device *dev)
1156 resource_size_t physaddr = 0; 1156 resource_size_t physaddr = 0;
1157 int bus_width = ACE_BUS_WIDTH_16; /* FIXME: should not be hard coded */ 1157 int bus_width = ACE_BUS_WIDTH_16; /* FIXME: should not be hard coded */
1158 u32 id = dev->id; 1158 u32 id = dev->id;
1159 int irq = NO_IRQ; 1159 int irq = 0;
1160 int i; 1160 int i;
1161 1161
1162 dev_dbg(&dev->dev, "ace_probe(%p)\n", dev); 1162 dev_dbg(&dev->dev, "ace_probe(%p)\n", dev);