Ed L. Cashin | 2611464 | 2006-09-20 14:36:48 -0400 | [diff] [blame] | 1 | /* Copyright (c) 2006 Coraid, Inc. See COPYING for GPL terms. */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2 | /* |
| 3 | * aoeblk.c |
| 4 | * block device routines |
| 5 | */ |
| 6 | |
| 7 | #include <linux/hdreg.h> |
| 8 | #include <linux/blkdev.h> |
Andrew Morton | 43cbe2c | 2007-12-10 15:49:13 -0800 | [diff] [blame] | 9 | #include <linux/backing-dev.h> |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 10 | #include <linux/fs.h> |
| 11 | #include <linux/ioctl.h> |
| 12 | #include <linux/genhd.h> |
| 13 | #include <linux/netdevice.h> |
| 14 | #include "aoe.h" |
| 15 | |
Christoph Lameter | e18b890 | 2006-12-06 20:33:20 -0800 | [diff] [blame] | 16 | static struct kmem_cache *buf_pool_cache; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 17 | |
Kay Sievers | edfaa7c | 2007-05-21 22:08:01 +0200 | [diff] [blame^] | 18 | static ssize_t aoedisk_show_state(struct device *dev, |
| 19 | struct device_attribute *attr, char *page) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 20 | { |
Kay Sievers | edfaa7c | 2007-05-21 22:08:01 +0200 | [diff] [blame^] | 21 | struct gendisk *disk = dev_to_disk(dev); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 22 | struct aoedev *d = disk->private_data; |
| 23 | |
| 24 | return snprintf(page, PAGE_SIZE, |
| 25 | "%s%s\n", |
| 26 | (d->flags & DEVFL_UP) ? "up" : "down", |
Ed L. Cashin | 3ae1c24 | 2006-01-19 13:46:19 -0500 | [diff] [blame] | 27 | (d->flags & DEVFL_PAUSE) ? ",paused" : |
| 28 | (d->nopen && !(d->flags & DEVFL_UP)) ? ",closewait" : ""); |
| 29 | /* I'd rather see nopen exported so we can ditch closewait */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 30 | } |
Kay Sievers | edfaa7c | 2007-05-21 22:08:01 +0200 | [diff] [blame^] | 31 | static ssize_t aoedisk_show_mac(struct device *dev, |
| 32 | struct device_attribute *attr, char *page) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 33 | { |
Kay Sievers | edfaa7c | 2007-05-21 22:08:01 +0200 | [diff] [blame^] | 34 | struct gendisk *disk = dev_to_disk(dev); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 35 | struct aoedev *d = disk->private_data; |
| 36 | |
| 37 | return snprintf(page, PAGE_SIZE, "%012llx\n", |
| 38 | (unsigned long long)mac_addr(d->addr)); |
| 39 | } |
Kay Sievers | edfaa7c | 2007-05-21 22:08:01 +0200 | [diff] [blame^] | 40 | static ssize_t aoedisk_show_netif(struct device *dev, |
| 41 | struct device_attribute *attr, char *page) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 42 | { |
Kay Sievers | edfaa7c | 2007-05-21 22:08:01 +0200 | [diff] [blame^] | 43 | struct gendisk *disk = dev_to_disk(dev); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 44 | struct aoedev *d = disk->private_data; |
| 45 | |
| 46 | return snprintf(page, PAGE_SIZE, "%s\n", d->ifp->name); |
| 47 | } |
Ed L Cashin | 4613ed2 | 2005-04-29 10:24:25 -0400 | [diff] [blame] | 48 | /* firmware version */ |
Kay Sievers | edfaa7c | 2007-05-21 22:08:01 +0200 | [diff] [blame^] | 49 | static ssize_t aoedisk_show_fwver(struct device *dev, |
| 50 | struct device_attribute *attr, char *page) |
Ed L Cashin | 4613ed2 | 2005-04-29 10:24:25 -0400 | [diff] [blame] | 51 | { |
Kay Sievers | edfaa7c | 2007-05-21 22:08:01 +0200 | [diff] [blame^] | 52 | struct gendisk *disk = dev_to_disk(dev); |
Ed L Cashin | 4613ed2 | 2005-04-29 10:24:25 -0400 | [diff] [blame] | 53 | struct aoedev *d = disk->private_data; |
| 54 | |
| 55 | return snprintf(page, PAGE_SIZE, "0x%04x\n", (unsigned int) d->fw_ver); |
| 56 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 57 | |
Kay Sievers | edfaa7c | 2007-05-21 22:08:01 +0200 | [diff] [blame^] | 58 | static DEVICE_ATTR(state, S_IRUGO, aoedisk_show_state, NULL); |
| 59 | static DEVICE_ATTR(mac, S_IRUGO, aoedisk_show_mac, NULL); |
| 60 | static DEVICE_ATTR(netif, S_IRUGO, aoedisk_show_netif, NULL); |
| 61 | static struct device_attribute dev_attr_firmware_version = { |
| 62 | .attr = { .name = "firmware-version", .mode = S_IRUGO, .owner = THIS_MODULE }, |
| 63 | .show = aoedisk_show_fwver, |
Ed L Cashin | 4613ed2 | 2005-04-29 10:24:25 -0400 | [diff] [blame] | 64 | }; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 65 | |
Greg Kroah-Hartman | 4ca5224 | 2002-04-09 12:14:34 -0700 | [diff] [blame] | 66 | static struct attribute *aoe_attrs[] = { |
Kay Sievers | edfaa7c | 2007-05-21 22:08:01 +0200 | [diff] [blame^] | 67 | &dev_attr_state.attr, |
| 68 | &dev_attr_mac.attr, |
| 69 | &dev_attr_netif.attr, |
| 70 | &dev_attr_firmware_version.attr, |
| 71 | NULL, |
Greg Kroah-Hartman | 4ca5224 | 2002-04-09 12:14:34 -0700 | [diff] [blame] | 72 | }; |
| 73 | |
| 74 | static const struct attribute_group attr_group = { |
| 75 | .attrs = aoe_attrs, |
| 76 | }; |
| 77 | |
| 78 | static int |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 79 | aoedisk_add_sysfs(struct aoedev *d) |
| 80 | { |
Kay Sievers | edfaa7c | 2007-05-21 22:08:01 +0200 | [diff] [blame^] | 81 | return sysfs_create_group(&d->gd->dev.kobj, &attr_group); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 82 | } |
| 83 | void |
| 84 | aoedisk_rm_sysfs(struct aoedev *d) |
| 85 | { |
Kay Sievers | edfaa7c | 2007-05-21 22:08:01 +0200 | [diff] [blame^] | 86 | sysfs_remove_group(&d->gd->dev.kobj, &attr_group); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 87 | } |
| 88 | |
| 89 | static int |
| 90 | aoeblk_open(struct inode *inode, struct file *filp) |
| 91 | { |
| 92 | struct aoedev *d; |
| 93 | ulong flags; |
| 94 | |
| 95 | d = inode->i_bdev->bd_disk->private_data; |
| 96 | |
| 97 | spin_lock_irqsave(&d->lock, flags); |
| 98 | if (d->flags & DEVFL_UP) { |
| 99 | d->nopen++; |
| 100 | spin_unlock_irqrestore(&d->lock, flags); |
| 101 | return 0; |
| 102 | } |
| 103 | spin_unlock_irqrestore(&d->lock, flags); |
| 104 | return -ENODEV; |
| 105 | } |
| 106 | |
| 107 | static int |
| 108 | aoeblk_release(struct inode *inode, struct file *filp) |
| 109 | { |
| 110 | struct aoedev *d; |
| 111 | ulong flags; |
| 112 | |
| 113 | d = inode->i_bdev->bd_disk->private_data; |
| 114 | |
| 115 | spin_lock_irqsave(&d->lock, flags); |
| 116 | |
Ed L. Cashin | 5f7702f | 2006-01-19 13:46:27 -0500 | [diff] [blame] | 117 | if (--d->nopen == 0) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 118 | spin_unlock_irqrestore(&d->lock, flags); |
| 119 | aoecmd_cfg(d->aoemajor, d->aoeminor); |
| 120 | return 0; |
| 121 | } |
| 122 | spin_unlock_irqrestore(&d->lock, flags); |
| 123 | |
| 124 | return 0; |
| 125 | } |
| 126 | |
| 127 | static int |
Jens Axboe | 165125e | 2007-07-24 09:28:11 +0200 | [diff] [blame] | 128 | aoeblk_make_request(struct request_queue *q, struct bio *bio) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 129 | { |
| 130 | struct aoedev *d; |
| 131 | struct buf *buf; |
| 132 | struct sk_buff *sl; |
| 133 | ulong flags; |
| 134 | |
| 135 | blk_queue_bounce(q, &bio); |
| 136 | |
| 137 | d = bio->bi_bdev->bd_disk->private_data; |
| 138 | buf = mempool_alloc(d->bufpool, GFP_NOIO); |
| 139 | if (buf == NULL) { |
Ed L. Cashin | a12c93f | 2006-09-20 14:36:51 -0400 | [diff] [blame] | 140 | printk(KERN_INFO "aoe: buf allocation failure\n"); |
NeilBrown | 6712ecf | 2007-09-27 12:47:43 +0200 | [diff] [blame] | 141 | bio_endio(bio, -ENOMEM); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 142 | return 0; |
| 143 | } |
| 144 | memset(buf, 0, sizeof(*buf)); |
| 145 | INIT_LIST_HEAD(&buf->bufs); |
ecashin@coraid.com | 0c6f0e7 | 2005-04-18 22:00:22 -0700 | [diff] [blame] | 146 | buf->start_time = jiffies; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 147 | buf->bio = bio; |
| 148 | buf->resid = bio->bi_size; |
| 149 | buf->sector = bio->bi_sector; |
Ed L. Cashin | 392e484 | 2006-09-20 14:36:50 -0400 | [diff] [blame] | 150 | buf->bv = &bio->bi_io_vec[bio->bi_idx]; |
| 151 | WARN_ON(buf->bv->bv_len == 0); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 152 | buf->bv_resid = buf->bv->bv_len; |
| 153 | buf->bufaddr = page_address(buf->bv->bv_page) + buf->bv->bv_offset; |
| 154 | |
| 155 | spin_lock_irqsave(&d->lock, flags); |
| 156 | |
| 157 | if ((d->flags & DEVFL_UP) == 0) { |
Ed L. Cashin | a12c93f | 2006-09-20 14:36:51 -0400 | [diff] [blame] | 158 | printk(KERN_INFO "aoe: device %ld.%ld is not up\n", |
| 159 | d->aoemajor, d->aoeminor); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 160 | spin_unlock_irqrestore(&d->lock, flags); |
| 161 | mempool_free(buf, d->bufpool); |
NeilBrown | 6712ecf | 2007-09-27 12:47:43 +0200 | [diff] [blame] | 162 | bio_endio(bio, -ENXIO); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 163 | return 0; |
| 164 | } |
| 165 | |
| 166 | list_add_tail(&buf->bufs, &d->bufq); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 167 | |
Ed L. Cashin | 3ae1c24 | 2006-01-19 13:46:19 -0500 | [diff] [blame] | 168 | aoecmd_work(d); |
ecashin@coraid.com | a4b3836 | 2005-04-18 22:00:22 -0700 | [diff] [blame] | 169 | sl = d->sendq_hd; |
| 170 | d->sendq_hd = d->sendq_tl = NULL; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 171 | |
| 172 | spin_unlock_irqrestore(&d->lock, flags); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 173 | aoenet_xmit(sl); |
Ed L. Cashin | 3ae1c24 | 2006-01-19 13:46:19 -0500 | [diff] [blame] | 174 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 175 | return 0; |
| 176 | } |
| 177 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 178 | static int |
Christoph Hellwig | a885c8c | 2006-01-08 01:02:50 -0800 | [diff] [blame] | 179 | aoeblk_getgeo(struct block_device *bdev, struct hd_geometry *geo) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 180 | { |
Christoph Hellwig | a885c8c | 2006-01-08 01:02:50 -0800 | [diff] [blame] | 181 | struct aoedev *d = bdev->bd_disk->private_data; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 182 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 183 | if ((d->flags & DEVFL_UP) == 0) { |
Ed L. Cashin | a12c93f | 2006-09-20 14:36:51 -0400 | [diff] [blame] | 184 | printk(KERN_ERR "aoe: disk not up\n"); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 185 | return -ENODEV; |
| 186 | } |
| 187 | |
Christoph Hellwig | a885c8c | 2006-01-08 01:02:50 -0800 | [diff] [blame] | 188 | geo->cylinders = d->geo.cylinders; |
| 189 | geo->heads = d->geo.heads; |
| 190 | geo->sectors = d->geo.sectors; |
| 191 | return 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 192 | } |
| 193 | |
| 194 | static struct block_device_operations aoe_bdops = { |
| 195 | .open = aoeblk_open, |
| 196 | .release = aoeblk_release, |
Christoph Hellwig | a885c8c | 2006-01-08 01:02:50 -0800 | [diff] [blame] | 197 | .getgeo = aoeblk_getgeo, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 198 | .owner = THIS_MODULE, |
| 199 | }; |
| 200 | |
| 201 | /* alloc_disk and add_disk can sleep */ |
| 202 | void |
| 203 | aoeblk_gdalloc(void *vp) |
| 204 | { |
| 205 | struct aoedev *d = vp; |
| 206 | struct gendisk *gd; |
| 207 | ulong flags; |
| 208 | |
| 209 | gd = alloc_disk(AOE_PARTITIONS); |
| 210 | if (gd == NULL) { |
Ed L. Cashin | a12c93f | 2006-09-20 14:36:51 -0400 | [diff] [blame] | 211 | printk(KERN_ERR "aoe: cannot allocate disk structure for %ld.%ld\n", |
Ed L. Cashin | 6bb6285 | 2006-09-20 14:36:49 -0400 | [diff] [blame] | 212 | d->aoemajor, d->aoeminor); |
Andrew Morton | 43cbe2c | 2007-12-10 15:49:13 -0800 | [diff] [blame] | 213 | goto err; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 214 | } |
| 215 | |
Matthew Dobson | 93d2341 | 2006-03-26 01:37:50 -0800 | [diff] [blame] | 216 | d->bufpool = mempool_create_slab_pool(MIN_BUFS, buf_pool_cache); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 217 | if (d->bufpool == NULL) { |
Ed L. Cashin | a12c93f | 2006-09-20 14:36:51 -0400 | [diff] [blame] | 218 | printk(KERN_ERR "aoe: cannot allocate bufpool for %ld.%ld\n", |
Ed L. Cashin | 6bb6285 | 2006-09-20 14:36:49 -0400 | [diff] [blame] | 219 | d->aoemajor, d->aoeminor); |
Andrew Morton | 43cbe2c | 2007-12-10 15:49:13 -0800 | [diff] [blame] | 220 | goto err_disk; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 221 | } |
| 222 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 223 | blk_queue_make_request(&d->blkq, aoeblk_make_request); |
Andrew Morton | 43cbe2c | 2007-12-10 15:49:13 -0800 | [diff] [blame] | 224 | if (bdi_init(&d->blkq.backing_dev_info)) |
| 225 | goto err_mempool; |
| 226 | spin_lock_irqsave(&d->lock, flags); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 227 | gd->major = AOE_MAJOR; |
| 228 | gd->first_minor = d->sysminor * AOE_PARTITIONS; |
| 229 | gd->fops = &aoe_bdops; |
| 230 | gd->private_data = d; |
| 231 | gd->capacity = d->ssize; |
| 232 | snprintf(gd->disk_name, sizeof gd->disk_name, "etherd/e%ld.%ld", |
| 233 | d->aoemajor, d->aoeminor); |
| 234 | |
| 235 | gd->queue = &d->blkq; |
| 236 | d->gd = gd; |
Ed L. Cashin | 3ae1c24 | 2006-01-19 13:46:19 -0500 | [diff] [blame] | 237 | d->flags &= ~DEVFL_GDALLOC; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 238 | d->flags |= DEVFL_UP; |
| 239 | |
| 240 | spin_unlock_irqrestore(&d->lock, flags); |
| 241 | |
| 242 | add_disk(gd); |
| 243 | aoedisk_add_sysfs(d); |
Andrew Morton | 43cbe2c | 2007-12-10 15:49:13 -0800 | [diff] [blame] | 244 | return; |
| 245 | |
| 246 | err_mempool: |
| 247 | mempool_destroy(d->bufpool); |
| 248 | err_disk: |
| 249 | put_disk(gd); |
| 250 | err: |
| 251 | spin_lock_irqsave(&d->lock, flags); |
| 252 | d->flags &= ~DEVFL_GDALLOC; |
| 253 | spin_unlock_irqrestore(&d->lock, flags); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 254 | } |
| 255 | |
| 256 | void |
| 257 | aoeblk_exit(void) |
| 258 | { |
| 259 | kmem_cache_destroy(buf_pool_cache); |
| 260 | } |
| 261 | |
| 262 | int __init |
| 263 | aoeblk_init(void) |
| 264 | { |
Paul Mundt | 20c2df8 | 2007-07-20 10:11:58 +0900 | [diff] [blame] | 265 | buf_pool_cache = kmem_cache_create("aoe_bufs", |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 266 | sizeof(struct buf), |
Paul Mundt | 20c2df8 | 2007-07-20 10:11:58 +0900 | [diff] [blame] | 267 | 0, 0, NULL); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 268 | if (buf_pool_cache == NULL) |
| 269 | return -ENOMEM; |
| 270 | |
| 271 | return 0; |
| 272 | } |
| 273 | |