Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1 | /* Copyright (c) 2004 Coraid, Inc. See COPYING for GPL terms. */ |
| 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 | |
| 17 | /* add attributes for our block devices in sysfs */ |
| 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 | |
| 67 | static void |
| 68 | aoedisk_add_sysfs(struct aoedev *d) |
| 69 | { |
| 70 | sysfs_create_file(&d->gd->kobj, &disk_attr_state.attr); |
| 71 | sysfs_create_file(&d->gd->kobj, &disk_attr_mac.attr); |
| 72 | sysfs_create_file(&d->gd->kobj, &disk_attr_netif.attr); |
Ed L Cashin | 4613ed2 | 2005-04-29 10:24:25 -0400 | [diff] [blame] | 73 | sysfs_create_file(&d->gd->kobj, &disk_attr_fwver.attr); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 74 | } |
| 75 | void |
| 76 | aoedisk_rm_sysfs(struct aoedev *d) |
| 77 | { |
| 78 | sysfs_remove_link(&d->gd->kobj, "state"); |
| 79 | sysfs_remove_link(&d->gd->kobj, "mac"); |
| 80 | sysfs_remove_link(&d->gd->kobj, "netif"); |
Ed L Cashin | 4613ed2 | 2005-04-29 10:24:25 -0400 | [diff] [blame] | 81 | sysfs_remove_link(&d->gd->kobj, "firmware-version"); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 82 | } |
| 83 | |
| 84 | static int |
| 85 | aoeblk_open(struct inode *inode, struct file *filp) |
| 86 | { |
| 87 | struct aoedev *d; |
| 88 | ulong flags; |
| 89 | |
| 90 | d = inode->i_bdev->bd_disk->private_data; |
| 91 | |
| 92 | spin_lock_irqsave(&d->lock, flags); |
| 93 | if (d->flags & DEVFL_UP) { |
| 94 | d->nopen++; |
| 95 | spin_unlock_irqrestore(&d->lock, flags); |
| 96 | return 0; |
| 97 | } |
| 98 | spin_unlock_irqrestore(&d->lock, flags); |
| 99 | return -ENODEV; |
| 100 | } |
| 101 | |
| 102 | static int |
| 103 | aoeblk_release(struct inode *inode, struct file *filp) |
| 104 | { |
| 105 | struct aoedev *d; |
| 106 | ulong flags; |
| 107 | |
| 108 | d = inode->i_bdev->bd_disk->private_data; |
| 109 | |
| 110 | spin_lock_irqsave(&d->lock, flags); |
| 111 | |
Ed L. Cashin | 5f7702f | 2006-01-19 13:46:27 -0500 | [diff] [blame] | 112 | if (--d->nopen == 0) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 113 | spin_unlock_irqrestore(&d->lock, flags); |
| 114 | aoecmd_cfg(d->aoemajor, d->aoeminor); |
| 115 | return 0; |
| 116 | } |
| 117 | spin_unlock_irqrestore(&d->lock, flags); |
| 118 | |
| 119 | return 0; |
| 120 | } |
| 121 | |
| 122 | static int |
| 123 | aoeblk_make_request(request_queue_t *q, struct bio *bio) |
| 124 | { |
| 125 | struct aoedev *d; |
| 126 | struct buf *buf; |
| 127 | struct sk_buff *sl; |
| 128 | ulong flags; |
| 129 | |
| 130 | blk_queue_bounce(q, &bio); |
| 131 | |
| 132 | d = bio->bi_bdev->bd_disk->private_data; |
| 133 | buf = mempool_alloc(d->bufpool, GFP_NOIO); |
| 134 | if (buf == NULL) { |
| 135 | printk(KERN_INFO "aoe: aoeblk_make_request: buf allocation " |
| 136 | "failure\n"); |
| 137 | bio_endio(bio, bio->bi_size, -ENOMEM); |
| 138 | return 0; |
| 139 | } |
| 140 | memset(buf, 0, sizeof(*buf)); |
| 141 | INIT_LIST_HEAD(&buf->bufs); |
ecashin@coraid.com | 0c6f0e7 | 2005-04-18 22:00:22 -0700 | [diff] [blame] | 142 | buf->start_time = jiffies; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 143 | buf->bio = bio; |
| 144 | buf->resid = bio->bi_size; |
| 145 | buf->sector = bio->bi_sector; |
| 146 | buf->bv = buf->bio->bi_io_vec; |
| 147 | buf->bv_resid = buf->bv->bv_len; |
| 148 | buf->bufaddr = page_address(buf->bv->bv_page) + buf->bv->bv_offset; |
| 149 | |
| 150 | spin_lock_irqsave(&d->lock, flags); |
| 151 | |
| 152 | if ((d->flags & DEVFL_UP) == 0) { |
| 153 | printk(KERN_INFO "aoe: aoeblk_make_request: device %ld.%ld is not up\n", |
| 154 | d->aoemajor, d->aoeminor); |
| 155 | spin_unlock_irqrestore(&d->lock, flags); |
| 156 | mempool_free(buf, d->bufpool); |
| 157 | bio_endio(bio, bio->bi_size, -ENXIO); |
| 158 | return 0; |
| 159 | } |
| 160 | |
| 161 | list_add_tail(&buf->bufs, &d->bufq); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 162 | |
Ed L. Cashin | 3ae1c24 | 2006-01-19 13:46:19 -0500 | [diff] [blame] | 163 | aoecmd_work(d); |
ecashin@coraid.com | a4b3836 | 2005-04-18 22:00:22 -0700 | [diff] [blame] | 164 | sl = d->sendq_hd; |
| 165 | d->sendq_hd = d->sendq_tl = NULL; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 166 | |
| 167 | spin_unlock_irqrestore(&d->lock, flags); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 168 | aoenet_xmit(sl); |
Ed L. Cashin | 3ae1c24 | 2006-01-19 13:46:19 -0500 | [diff] [blame] | 169 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 170 | return 0; |
| 171 | } |
| 172 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 173 | static int |
Christoph Hellwig | a885c8c | 2006-01-08 01:02:50 -0800 | [diff] [blame] | 174 | aoeblk_getgeo(struct block_device *bdev, struct hd_geometry *geo) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 175 | { |
Christoph Hellwig | a885c8c | 2006-01-08 01:02:50 -0800 | [diff] [blame] | 176 | struct aoedev *d = bdev->bd_disk->private_data; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 177 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 178 | if ((d->flags & DEVFL_UP) == 0) { |
| 179 | printk(KERN_ERR "aoe: aoeblk_ioctl: disk not up\n"); |
| 180 | return -ENODEV; |
| 181 | } |
| 182 | |
Christoph Hellwig | a885c8c | 2006-01-08 01:02:50 -0800 | [diff] [blame] | 183 | geo->cylinders = d->geo.cylinders; |
| 184 | geo->heads = d->geo.heads; |
| 185 | geo->sectors = d->geo.sectors; |
| 186 | return 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 187 | } |
| 188 | |
| 189 | static struct block_device_operations aoe_bdops = { |
| 190 | .open = aoeblk_open, |
| 191 | .release = aoeblk_release, |
Christoph Hellwig | a885c8c | 2006-01-08 01:02:50 -0800 | [diff] [blame] | 192 | .getgeo = aoeblk_getgeo, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 193 | .owner = THIS_MODULE, |
| 194 | }; |
| 195 | |
| 196 | /* alloc_disk and add_disk can sleep */ |
| 197 | void |
| 198 | aoeblk_gdalloc(void *vp) |
| 199 | { |
| 200 | struct aoedev *d = vp; |
| 201 | struct gendisk *gd; |
| 202 | ulong flags; |
| 203 | |
| 204 | gd = alloc_disk(AOE_PARTITIONS); |
| 205 | if (gd == NULL) { |
| 206 | printk(KERN_ERR "aoe: aoeblk_gdalloc: cannot allocate disk " |
| 207 | "structure for %ld.%ld\n", d->aoemajor, d->aoeminor); |
| 208 | spin_lock_irqsave(&d->lock, flags); |
Ed L. Cashin | 3ae1c24 | 2006-01-19 13:46:19 -0500 | [diff] [blame] | 209 | d->flags &= ~DEVFL_GDALLOC; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 210 | spin_unlock_irqrestore(&d->lock, flags); |
| 211 | return; |
| 212 | } |
| 213 | |
Matthew Dobson | 93d2341 | 2006-03-26 01:37:50 -0800 | [diff] [blame] | 214 | d->bufpool = mempool_create_slab_pool(MIN_BUFS, buf_pool_cache); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 215 | if (d->bufpool == NULL) { |
| 216 | printk(KERN_ERR "aoe: aoeblk_gdalloc: cannot allocate bufpool " |
| 217 | "for %ld.%ld\n", d->aoemajor, d->aoeminor); |
| 218 | put_disk(gd); |
| 219 | spin_lock_irqsave(&d->lock, flags); |
Ed L. Cashin | 3ae1c24 | 2006-01-19 13:46:19 -0500 | [diff] [blame] | 220 | d->flags &= ~DEVFL_GDALLOC; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 221 | spin_unlock_irqrestore(&d->lock, flags); |
| 222 | return; |
| 223 | } |
| 224 | |
| 225 | spin_lock_irqsave(&d->lock, flags); |
| 226 | blk_queue_make_request(&d->blkq, aoeblk_make_request); |
| 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); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 244 | } |
| 245 | |
| 246 | void |
| 247 | aoeblk_exit(void) |
| 248 | { |
| 249 | kmem_cache_destroy(buf_pool_cache); |
| 250 | } |
| 251 | |
| 252 | int __init |
| 253 | aoeblk_init(void) |
| 254 | { |
| 255 | buf_pool_cache = kmem_cache_create("aoe_bufs", |
| 256 | sizeof(struct buf), |
| 257 | 0, 0, NULL, NULL); |
| 258 | if (buf_pool_cache == NULL) |
| 259 | return -ENOMEM; |
| 260 | |
| 261 | return 0; |
| 262 | } |
| 263 | |