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