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