forked from MatiasBjorling/kvblade
-
Notifications
You must be signed in to change notification settings - Fork 1
/
kvblade.c
1465 lines (1195 loc) · 40.8 KB
/
kvblade.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/* Copyright (C) 2006 Coraid, Inc. See COPYING for GPL terms. */
/* Copyright (C) 2019 John Sharratt. See COPYING for GPL terms. */
#include <linux/module.h>
#include <linux/blkdev.h>
#include <linux/netdevice.h>
#include <linux/kthread.h>
#include <linux/ata.h>
#include <linux/ctype.h>
#include <uapi/linux/hdreg.h>
#include "aoe.h"
//#define AOE_DEBUG
//#define AOE_DEBUG_VERBOSE
#define xprintk(L, fmt, arg...) printk(L "kvblade: " "%s: " fmt, __func__, ## arg)
#define iprintk(fmt, arg...) xprintk(KERN_INFO, fmt, ## arg)
#define eprintk(fmt, arg...) xprintk(KERN_ERR, fmt, ## arg)
#define wprintk(fmt, arg...) xprintk(KERN_WARN, fmt, ## arg)
#define dprintk(fmt, arg...) if(0);else xprintk(KERN_DEBUG, fmt, ## arg)
#ifdef AOE_DEBUG
#define tiprintk(fmt, arg...) printk(KERN_INFO fmt, ## arg)
#define teprintk(fmt, arg...) printk(KERN_ERR fmt, ## arg)
#define twprintk(fmt, arg...) printk(KERN_WARN fmt, ## arg)
#else
#define tiprintk(fmt, arg...) trace_printk(KERN_INFO fmt, ## arg)
#define teprintk(fmt, arg...) trace_printk(KERN_ERR fmt, ## arg)
#define wtprintk(fmt, arg...) trace_printk(KERN_WARN fmt, ## arg)
#endif
#define nelem(A) (sizeof (A) / sizeof (A)[0])
#define MAXSECTORS(mtu) (((mtu) - sizeof (struct aoe_hdr) - sizeof (struct aoe_atahdr)) / 512)
#define MAXBUFFERS 1024
#define MAXIOVECS 16
#define HEADERSIZE sizeof(struct aoe_hdr) + sizeof(struct aoe_cfghdr)
#define bio_sector(bio) ((bio)->bi_iter.bi_sector)
#define bio_size(bio) ((bio)->bi_iter.bi_size)
#define bio_idx(bio) ((bio)->bi_iter.bi_idx)
#ifndef KERNEL_SECTOR_SIZE
#define KERNEL_SECTOR_SIZE 512
#endif
enum {
ATA_MODEL_LEN = 40,
ATA_LBA28MAX = 0x0fffffff,
};
enum
{
AOEERR_CMD= 1,
AOEERR_ARG,
AOEERR_DEV,
AOEERR_CFG,
AOEERR_VER,
};
struct aoetarget;
struct aoereq {
struct sk_buff *skb; // Reference to the packet that initiated the request
struct aoetarget *d; // Reference to the device that the request will be actioned on
struct aoethread* t; // Reference to the thread thats processing this command
int err;
struct bio bio; // The BIO structure is cached in the AOE request to minimize the calls to memory allocation
struct bio_vec bvl[MAXIOVECS]; // These must be placed together as the BIO implementation requires it
} ____cacheline_aligned_in_smp typedef aoereq_t;
struct aoetarget_thread {
atomic_t busy;
} ____cacheline_aligned_in_smp typedef aoetarget_thread_t;
struct aoetarget {
// This next 64 bytes are aligned and packed together so that
// the driver keeps a single cache line hot per device
struct hlist_node node;
__be16 major;
__be16 minor;
struct net_device* netdev;
struct block_device* blkdev;
struct aoetarget_thread* devthread_percpu;
struct kobject kobj;
int nconfig;
loff_t scnt;
unsigned char config[1024];
char path[256];
char model[ATA_MODEL_LEN];
char sn[ATA_ID_SERNO_LEN];
struct rcu_head rcu; // List head used to delay the release of this object till after RCU sync
} ____cacheline_aligned_in_smp typedef aoetarget_t;
struct kvblade_sysfs_entry {
struct attribute attr;
ssize_t(*show)(struct aoetarget *, char *);
ssize_t(*store)(struct aoetarget *, const char *, size_t);
};
struct aoethread {
struct sk_buff_head skb_outq;
struct sk_buff_head skb_inq;
struct sk_buff_head skb_com;
atomic_t announce_all;
struct completion ktrendez;
struct task_struct* task;
} ____cacheline_aligned_in_smp typedef aoethread_t;
struct core
{
spinlock_t lock;
struct hlist_head devlist;
struct kmem_cache* aoe_rq_cache;
struct kobject kvblade_kobj;
struct aoethread* thread_percpu;
} ____cacheline_aligned_in_smp typedef core_t;
static core_t root;
static struct kobj_type kvblade_ktype;
static void kvblade_release(struct kobject *kobj) {
}
static ssize_t kvblade_get_capacity(struct block_device *bd) {
if (bd->bd_part != NULL)
return bd->bd_part->nr_sects;
return get_capacity(bd->bd_disk);
}
static ssize_t kvblade_sysfs_args(char *p, char *argv[], int argv_max) {
int argc = 0;
while (*p) {
while (*p && isspace(*p))
++p;
if (*p == '\0')
break;
if (argc < argv_max)
argv[argc++] = p;
else {
teprintk("kvblade: too many args!\n");
return -1;
}
while (*p && !isspace(*p))
++p;
if (*p)
*p++ = '\0';
}
return argc;
}
static struct sk_buff * skb_new(struct aoethread* t, struct net_device *dev, ulong len) {
struct sk_buff *skb;
if (len < ETH_ZLEN)
len = ETH_ZLEN;
skb = __alloc_skb(len, GFP_ATOMIC, SKB_ALLOC_FCLONE, numa_node_id());
if (!skb)
skb = __alloc_skb(len, GFP_ATOMIC & ~__GFP_DMA, 0, NUMA_NO_NODE);
if (skb) {
skb_reserve(skb, HEADERSIZE);
skb_reset_network_header(skb);
skb_reset_mac_header(skb);
skb->dev = dev;
skb->protocol = __constant_htons(ETH_P_AOE);
skb->priority = 0;
skb->next = skb->prev = NULL;
skb->ip_summed = CHECKSUM_NONE;
skb_put(skb, len);
}
return skb;
}
static char* spncpy(char *d, const char *s, int n) {
char *r = d;
memset(d, ' ', n);
while (n-- > 0) {
if (*s == '\0')
break;
*d++ = *s++;
}
return r;
}
static int count_busy(struct aoetarget *d) {
int n;
struct aoetarget_thread* dt;
int ret =0;
for (n = 0; n < num_online_cpus(); n++) {
dt = (struct aoetarget_thread*)per_cpu_ptr(d->devthread_percpu, n);
ret += atomic_read(&dt->busy);
}
return ret;
}
static void wake(struct aoethread* t)
{
wake_up_process(t->task);
}
static int ata_maxsectors(struct aoetarget *d) {
int ret = MAXSECTORS(d->netdev->mtu);
if (ret > 64)
ret = 64;
else if (ret > 32)
ret = 32;
else if (ret > 16)
ret = 16;
else if (ret > 8)
ret = 8;
return ret;
}
static void announce(struct aoetarget *d, struct aoethread* t) {
struct sk_buff* skb;
struct aoe_hdr *aoe;
struct aoe_cfghdr *cfg;
int len = HEADERSIZE + d->nconfig;
skb = skb_new(t, d->netdev, len);
if (skb == NULL)
return;
aoe = (struct aoe_hdr *) skb_mac_header(skb);
cfg = (struct aoe_cfghdr *)(aoe+1);
memset(aoe, 0, sizeof *aoe);
memcpy(aoe->src, d->netdev->dev_addr, ETH_ALEN);
memset(aoe->dst, 0xFF, ETH_ALEN);
aoe->type = __constant_htons(ETH_P_AOE);
aoe->verfl = AOE_HVER | AOEFL_RSP;
aoe->major = cpu_to_be16(d->major);
aoe->minor = d->minor;
aoe->cmd = AOECMD_CFG;
memset(cfg, 0, sizeof *cfg);
cfg->bufcnt = cpu_to_be16(MAXBUFFERS);
cfg->fwver = __constant_htons(0x0002);
cfg->scnt = ata_maxsectors(d);
cfg->aoeccmd = AOE_HVER;
if (d->nconfig) {
*((__be16*)&cfg->cslen[0]) = cpu_to_be16(d->nconfig);
memcpy((cfg+1), d->config, d->nconfig);
}
#ifdef AOE_DEBUG_VERBOSE
tiprintk("kvblade: sending announce for %04X.%02X\n", aoe->major, aoe->minor);
#endif
skb_queue_tail(&t->skb_outq, skb);
wake(t);
}
static ssize_t kvblade_add(u32 major, u32 minor, char *ifname, char *path) {
struct net_device *nd;
struct net* ns;
struct block_device *bd;
struct aoetarget *d, *td;
int ret = 0;
struct aoethread* t;
int n;
struct aoetarget_thread* dt;
tiprintk("kvblade: kvblade_add %04X.%02X\n", major, minor);
nd = dev_get_by_name(&init_net, ifname);
if (nd == NULL) {
rcu_read_lock();
for_each_net_rcu(ns) {
nd = dev_get_by_name_rcu(ns, ifname);
if (nd != NULL) break;
}
rcu_read_unlock();
if (nd == NULL) {
teprintk("kvblade: add failed: interface %s not found.\n", ifname);
return -ENOENT;
}
}
dev_put(nd);
bd = blkdev_get_by_path(path, FMODE_READ | FMODE_WRITE, NULL);
if (!bd || IS_ERR(bd)) {
teprintk("kvblade: add failed: can't open block device %s: %ld\n", path, PTR_ERR(bd));
return -ENOENT;
}
if (kvblade_get_capacity(bd) == 0) {
teprintk("kvblade: add failed: zero sized block device.\n");
ret = -ENOENT;
goto err;
}
spin_lock(&root.lock);
rcu_read_lock();
hlist_for_each_entry_rcu_notrace(td, &root.devlist, node)
{
if (td->major == major &&
td->minor == minor &&
td->netdev == nd)
{
rcu_read_unlock();
spin_unlock(&root.lock);
teprintk("kvblade: add failed: device %d.%d already exists on %s.\n", major, minor, ifname);
ret = -EEXIST;
goto err;
}
}
rcu_read_unlock();
d = kmalloc(sizeof (struct aoetarget), GFP_KERNEL);
if (!d) {
spin_unlock(&root.lock);
teprintk("kvblade: add failed: kmalloc error for %d.%d\n", major, minor);
ret = -ENOMEM;
goto err;
}
memset(d, 0, sizeof (struct aoetarget));
INIT_HLIST_NODE(&d->node);
d->blkdev = bd;
d->netdev = nd;
d->major = major;
d->minor = minor;
d->scnt = kvblade_get_capacity(bd);
strncpy(d->path, path, nelem(d->path) - 1);
spncpy(d->model, "EtherDrive(R) kvblade", nelem(d->model));
spncpy(d->sn, "SN HERE", nelem(d->sn));
d->devthread_percpu = (struct aoetarget_thread*)alloc_percpu(struct aoetarget_thread);
if (!d->devthread_percpu) {
spin_unlock(&root.lock);
kfree(d);
teprintk("kvblade: add failed: alloc_percpu error for %d.%d\n", major, minor);
ret = -ENOMEM;
goto err;
}
for (n = 0; n < num_online_cpus(); n++) {
dt = (struct aoetarget_thread*)per_cpu_ptr(d->devthread_percpu, n);
memset(dt, 0, sizeof(struct aoetarget_thread));
}
ret = kobject_init_and_add(&d->kobj, &kvblade_ktype, &root.kvblade_kobj, "%d.%d@%s", major, minor, ifname);
if (ret) {
spin_unlock(&root.lock);
kfree(d);
teprintk("kvblade: add failed: kobject_init_and_add error for %d.%d\n", major, minor);
goto err;
}
hlist_add_head_rcu(&d->node, &root.devlist);
spin_unlock(&root.lock);
tiprintk("kvblade: added %s as %d.%d@%s: %Lu sectors.\n", path, major, minor, ifname, d->scnt);
t = (struct aoethread*)per_cpu_ptr(root.thread_percpu, get_cpu());
atomic_set(&t->announce_all, 1);
wake(t);
put_cpu();
return 0;
err:
blkdev_put(bd, FMODE_READ | FMODE_WRITE);
return ret;
}
static ssize_t kvblade_readd(u32 major, u32 minor, char *ifname, char *path) {
struct aoetarget *d, *td;
struct block_device *obd = NULL;
struct block_device *bd = NULL;
int ret = 0;
bd = blkdev_get_by_path(path, FMODE_READ | FMODE_WRITE, NULL);
if (!bd || IS_ERR(bd)) {
teprintk("kvblade: readd failed: can't open block device %s: %ld\n", path, PTR_ERR(bd));
return -ENOENT;
}
spin_lock(&root.lock);
d = NULL;
rcu_read_lock();
hlist_for_each_entry_rcu_notrace(td, &root.devlist, node) {
if (td->major == major &&
td->minor == minor &&
strcmp(td->netdev->name, ifname) == 0)
{
d = td;
break;
}
}
if (d == NULL) {
rcu_read_unlock();
spin_unlock(&root.lock);
ret = -ENOENT;
goto out;
}
// Replace the block device reference and release the old one
obd = d->blkdev;
d->blkdev = bd;
bd = NULL;
// We are finished (fall through and exit)
rcu_read_unlock();
spin_unlock(&root.lock);
out:
if (bd != NULL) {
blkdev_put(bd, FMODE_READ | FMODE_WRITE);
bd = NULL;
}
if (obd != NULL) {
blkdev_put(obd, FMODE_READ | FMODE_WRITE);
obd = NULL;
}
return ret;
}
void kvblade_del_rcu(struct rcu_head* head) {
struct aoetarget *d = container_of(head, aoetarget_t, rcu);
blkdev_put(d->blkdev, FMODE_READ | FMODE_WRITE);
kobject_del(&d->kobj);
kobject_put(&d->kobj);
if (d->devthread_percpu != NULL) {
free_percpu(d->devthread_percpu);
d->devthread_percpu = NULL;
}
}
static ssize_t kvblade_del(u32 major, u32 minor, char *ifname) {
struct aoetarget *d;
int ret;
spin_lock(&root.lock);
rcu_read_lock();
hlist_for_each_entry_rcu_notrace(d, &root.devlist, node) {
if (d->major == major &&
d->minor == minor &&
strcmp(d->netdev->name, ifname) == 0)
{
break;
}
}
if (d == NULL) {
rcu_read_unlock();
teprintk("kvblade: del failed: device %d.%d@%s not found.\n", major, minor, ifname);
ret = -ENOENT;
goto err;
} else if (count_busy(d)) {
rcu_read_unlock();
teprintk("kvblade: del failed: device %d.%d@%s is busy.\n", major, minor, ifname);
ret = -EBUSY;
goto err;
}
hlist_del_rcu(&d->node);
rcu_read_unlock();
spin_unlock(&root.lock);
call_rcu(&d->rcu, kvblade_del_rcu);
return 0;
err:
spin_unlock(&root.lock);
return ret;
}
static ssize_t store_add(struct aoetarget *dev, const char *page, size_t len) {
int error = 0;
char *argv[16];
char *p;
p = kmalloc(len + 1, GFP_KERNEL);
memcpy(p, page, len);
p[len] = '\0';
if (kvblade_sysfs_args(p, argv, nelem(argv)) != 4) {
teprintk("kvblade: bad arg count for add\n");
error = -EINVAL;
} else {
error = kvblade_add(simple_strtoul(argv[0], NULL, 0),
simple_strtoul(argv[1], NULL, 0),
argv[2], argv[3]);
}
kfree(p);
return error ? error : len;
}
static struct kvblade_sysfs_entry kvblade_sysfs_add = __ATTR(add, 0644, NULL, store_add);
static ssize_t store_del(struct aoetarget *dev, const char *page, size_t len) {
int error = 0;
char *argv[16];
char *p;
p = kmalloc(len + 1, GFP_KERNEL);
memcpy(p, page, len);
p[len] = '\0';
if (kvblade_sysfs_args(p, argv, nelem(argv)) != 3) {
teprintk("kvblade: bad arg count for del\n");
error = -EINVAL;
} else {
error = kvblade_del(simple_strtoul(argv[0], NULL, 0),
simple_strtoul(argv[1], NULL, 0),
argv[2]);
}
kfree(p);
return error ? error : len;
}
static struct kvblade_sysfs_entry kvblade_sysfs_del = __ATTR(del, 0644, NULL, store_del);
static ssize_t store_readd(struct aoetarget *dev, const char *page, size_t len) {
int error = 0;
char *argv[16];
char *p;
p = kmalloc(len + 1, GFP_KERNEL);
memcpy(p, page, len);
p[len] = '\0';
if (kvblade_sysfs_args(p, argv, nelem(argv)) != 4) {
teprintk("kvblade: bad arg count for readd\n");
error = -EINVAL;
} else {
error = kvblade_readd(simple_strtoul(argv[0], NULL, 0),
simple_strtoul(argv[1], NULL, 0),
argv[2], argv[3]);
}
kfree(p);
return error ? error : len;
}
static struct kvblade_sysfs_entry kvblade_sysfs_readd = __ATTR(readd, 0644, NULL, store_readd);
static ssize_t store_announce(struct aoetarget *dev, const char *page, size_t len) {
int error = 0;
struct aoethread* t;
t = (struct aoethread*)per_cpu_ptr(root.thread_percpu, get_cpu());
atomic_set(&t->announce_all, 1);
wake(t);
put_cpu();
return error ? error : len;
}
static struct kvblade_sysfs_entry kvblade_sysfs_announce = __ATTR(announce, 0644, NULL, store_announce);
static ssize_t show_scnt(struct aoetarget *dev, char *page) {
return sprintf(page, "%Ld\n", dev->scnt);
}
static struct kvblade_sysfs_entry kvblade_sysfs_scnt = __ATTR(scst, 0644, show_scnt, NULL);
static ssize_t show_busy(struct aoetarget *dev, char *page) {
return sprintf(page, "%d\n", count_busy(dev));
}
static struct kvblade_sysfs_entry kvblade_sysfs_busy = __ATTR(busy, 0644, show_busy, NULL);
static ssize_t show_bdev(struct aoetarget *dev, char *page) {
return print_dev_t(page, dev->blkdev->bd_dev);
}
static struct kvblade_sysfs_entry kvblade_sysfs_bdev = __ATTR(bdev, 0644, show_bdev, NULL);
static ssize_t show_bpath(struct aoetarget *dev, char *page) {
return sprintf(page, "%.*s\n", (int) nelem(dev->path), dev->path);
}
static struct kvblade_sysfs_entry kvblade_sysfs_bpath = __ATTR(bpath, 0644, show_bpath, NULL);
static ssize_t show_model(struct aoetarget *dev, char *page) {
return sprintf(page, "%.*s\n", (int) nelem(dev->model), dev->model);
}
static ssize_t store_model(struct aoetarget *dev, const char *page, size_t len) {
spncpy(dev->model, page, nelem(dev->model));
return 0;
}
static struct kvblade_sysfs_entry kvblade_sysfs_model = __ATTR(model, 0644, show_model, store_model);
static ssize_t show_sn(struct aoetarget *dev, char *page) {
return sprintf(page, "%.*s\n", (int) nelem(dev->sn), dev->sn);
}
static ssize_t store_sn(struct aoetarget *dev, const char *page, size_t len) {
spncpy(dev->sn, page, nelem(dev->sn));
return 0;
}
static struct kvblade_sysfs_entry kvblade_sysfs_sn = __ATTR(sn, 0644, show_sn, store_sn);
static ssize_t show_mtu(struct aoetarget *dev, char *page) {
return sprintf(page, "%d\n", (int)(ata_maxsectors(dev) * KERNEL_SECTOR_SIZE));
}
static struct kvblade_sysfs_entry kvblade_sysfs_mtu = __ATTR(mtu, 0644, show_mtu, NULL);
static struct attribute *kvblade_ktype_attrs[] = {
&kvblade_sysfs_scnt.attr,
&kvblade_sysfs_busy.attr,
&kvblade_sysfs_bdev.attr,
&kvblade_sysfs_bpath.attr,
&kvblade_sysfs_model.attr,
&kvblade_sysfs_sn.attr,
&kvblade_sysfs_mtu.attr,
NULL,
};
static struct attribute *kvblade_ktype_ops_attrs[] = {
&kvblade_sysfs_add.attr,
&kvblade_sysfs_del.attr,
&kvblade_sysfs_readd.attr,
&kvblade_sysfs_announce.attr,
NULL,
};
static ssize_t kvblade_attr_show(struct kobject *kobj, struct attribute *attr, char *page) {
struct kvblade_sysfs_entry *entry;
struct aoetarget *dev;
entry = container_of(attr, struct kvblade_sysfs_entry, attr);
dev = container_of(kobj, struct aoetarget, kobj);
if (!entry->show) {
return -EIO;
}
return entry->show(dev, page);
}
static ssize_t kvblade_attr_store(struct kobject *kobj, struct attribute *attr,
const char *page, size_t length) {
ssize_t ret;
struct kvblade_sysfs_entry *entry;
entry = container_of(attr, struct kvblade_sysfs_entry, attr);
if (kobj == &root.kvblade_kobj) {
ret = entry->store(NULL, page, length);
} else {
struct aoetarget *dev = container_of(kobj, struct aoetarget, kobj);
if (!entry->store)
return -EIO;
ret = entry->store(dev, page, length);
}
return ret;
}
static const struct sysfs_ops kvblade_sysfs_ops = {
.show = kvblade_attr_show,
.store = kvblade_attr_store,
};
static struct kobj_type kvblade_ktype = {
.default_attrs = kvblade_ktype_attrs,
.sysfs_ops = &kvblade_sysfs_ops,
.release = kvblade_release,
};
static struct kobj_type kvblade_ktype_ops = {
.default_attrs = kvblade_ktype_ops_attrs,
.sysfs_ops = &kvblade_sysfs_ops,
.release = kvblade_release,
};
static void setfld(u16 *a, int idx, int len, char *str) {
u8 *p;
for (p = (u8*) (a + idx); len; p += 2, len -= 2) {
p[1] = *str ? *str++ : ' ';
p[0] = *str ? *str++ : ' ';
}
}
static int ata_identify(struct aoetarget *d, struct aoe_atahdr *ata) {
char buf[64];
u16 *words = (u16 *)(ata+1);
u8 *cp;
loff_t scnt;
memset(words, 0, 512);
words[47] = 0x8000;
words[49] = 0x0200;
words[50] = 0x4000;
words[83] = 0x5400;
words[84] = 0x4000;
words[86] = 0x1400;
words[87] = 0x4000;
words[93] = 0x400b;
sprintf(buf, "V%d.%d\n", 0, 2);
setfld(words, 23, 8, buf);
setfld(words, 27, nelem(d->model), d->model);
setfld(words, 10, nelem(d->sn), d->sn);
scnt = d->scnt;
cp = (u8 *) & words[100];
*cp++ = scnt;
*cp++ = (scnt >>= 8);
*cp++ = (scnt >>= 8);
*cp++ = (scnt >>= 8);
*cp++ = (scnt >>= 8);
*cp++ = (scnt >>= 8);
scnt = d->scnt;
cp = (u8 *) & words[60];
if (scnt & ~ATA_LBA28MAX)
scnt = ATA_LBA28MAX;
*cp++ = scnt;
*cp++ = (scnt >>= 8);
*cp++ = (scnt >>= 8);
*cp++ = (scnt >>= 8) & 0xf;
return 512;
}
static void skb_setlen(struct sk_buff* skb, int len)
{
if (len > skb_headlen(skb)) {
skb->data_len -= skb->len - len;
skb->len = len;
} else {
skb->len = len;
skb->data_len = 0;
skb_set_tail_pointer(skb, len);
}
}
static void ktcom(struct aoethread* t, struct sk_buff *skb) {
struct aoereq *rq, **prq;
struct aoetarget *d;
struct aoetarget_thread *dt;
struct aoe_hdr *aoe;
struct aoe_atahdr *ata;
struct bio *bio;
int len;
unsigned int bytes;
prq = (struct aoereq **)(&skb->cb[0]);
rq = *prq;
bio = &rq->bio;
d = rq->d;
aoe = (struct aoe_hdr *) skb_mac_header(skb);
ata = (struct aoe_atahdr *)(aoe+1);
len = sizeof *aoe + sizeof *ata;
if (!bio->bi_status) {
bytes = ata->scnt * KERNEL_SECTOR_SIZE;
if (bio_data_dir(bio) == READ)
len += bytes;
ata->scnt = 0;
ata->cmdstat = ATA_DRDY;
ata->errfeat = 0;
// should increment lba here, too
} else {
teprintk("kvblade: I/O error %d on %s (status=%d)\n", blk_status_to_errno(bio->bi_status), d->kobj.name, bio->bi_status);
ata->cmdstat = ATA_ERR | ATA_DF;
ata->errfeat = ATA_UNC | ATA_ABORTED;
}
dt = (struct aoetarget_thread*)per_cpu_ptr(d->devthread_percpu, get_cpu());
atomic_dec(&dt->busy);
put_cpu();
skb_setlen(skb, len);
if (unlikely(!pskb_may_pull(skb, ETH_HLEN))) {
dev_kfree_skb(skb);
return;
}
dev_queue_xmit(skb);
kmem_cache_free(root.aoe_rq_cache, rq);
}
static void ata_io_complete(struct bio *bio) {
int error = blk_status_to_errno(bio->bi_status);
struct aoethread *t;
struct aoetarget* d;
struct aoereq *rq, **prq;
struct sk_buff *skb;
int cpu;
rq = bio->bi_private;
rq->err = error;
skb = rq->skb;
d = rq->d;
prq = (struct aoereq **)(&skb->cb[0]);
*prq = rq;
cpu = get_cpu();
t = (struct aoethread*)per_cpu_ptr(root.thread_percpu, cpu);
rq->t = t;
skb_queue_tail(&t->skb_com, skb);
wake(t);
put_cpu();
}
static inline loff_t readlba(u8 *lba) {
loff_t n = 0ULL;
int i;
for (i = 5; i >= 0; i--) {
n <<= 8;
n |= lba[i];
}
return n;
}
static struct bio* rq_init_bio(struct aoereq *rq) {
struct bio *bio;
bio = &rq->bio;
bio_init(bio, bio->bi_inline_vecs, MAXIOVECS);
return bio;
}
static int skb_add_pages(struct sk_buff* skb, struct bio *bio, int len) {
unsigned int offset = sizeof(struct aoe_hdr) + sizeof(struct aoe_atahdr);
int sg_n, sg_i;
int sg_max = skb_shinfo(skb)->nr_frags + 2;
struct scatterlist sg_tbl[sg_max], *sgentry;
// Validate that everything is ok
if (offset + len > skb->len) {
teprintk("kvblade: packet I/O is out of range: (%d), max %d\n", offset + len, skb->len);
return 0;
}
// Create the source scatterlist from the received packet
sg_init_table(sg_tbl, sg_max);
sg_n = skb_to_sgvec(skb, sg_tbl, offset, len);
if (sg_n <= 0) {
return 0;
}
// Loop through all the scatterlist items and add them into the BIO
for_each_sg(sg_tbl, sgentry, sg_n, sg_i) {
if (bio_add_page(bio,
sg_page(sgentry),
sgentry->length,
sgentry->offset) < sgentry->length)
return 0;
}
return len;
}
static struct sk_buff * rcv_ata(struct aoetarget *d, struct aoethread *t, struct sk_buff *skb) {
struct aoe_hdr *aoe;
struct aoe_atahdr *ata;
struct aoereq *rq;
struct aoetarget_thread *dt;
struct bio *bio;
sector_t lba;
int len, rw;
unsigned int data_len;
aoe = (struct aoe_hdr *) skb_mac_header(skb);
ata = (struct aoe_atahdr *)(aoe+1);
lba = readlba(&ata->lba0);
len = sizeof *aoe + sizeof *ata;
data_len = ata->scnt * KERNEL_SECTOR_SIZE;
switch (ata->cmdstat) {
do {
case ATA_CMD_PIO_READ:
lba &= ATA_LBA28MAX;
case ATA_CMD_PIO_READ_EXT:
lba &= 0x0000FFFFFFFFFFFFULL;
rw = READ;
break;
case ATA_CMD_PIO_WRITE:
lba &= ATA_LBA28MAX;
case ATA_CMD_PIO_WRITE_EXT:
lba &= 0x0000FFFFFFFFFFFFULL;
rw = WRITE;
} while (0);
// Default to error unless it is succesful
ata->cmdstat = ATA_ERR;
ata->errfeat = 0;
// Do a check on the IO range
if ((lba + ata->scnt) > d->scnt) {
teprintk("kvblade: sector I/O is out of range: %Lu (%d), max %Lu\n", (long long) lba, ata->scnt, d->scnt);
ata->errfeat = ATA_IDNF;
break;
}
rq = (aoereq_t*) kmem_cache_alloc_node(root.aoe_rq_cache, GFP_ATOMIC & ~ __GFP_DMA, numa_node_id());
if (unlikely(rq == NULL)) {
rq = (aoereq_t*) kmem_cache_alloc_node(root.aoe_rq_cache, GFP_KERNEL, numa_node_id());
if (unlikely(rq == NULL)) {
teprintk("kvblade: failed to allocate ATA request memory\n");
ata->errfeat = ATA_ABORTED;
break;
}
}
prefetchw(rq);
bio = rq_init_bio(rq);
prefetchw(bio);
len += data_len;
if (len > skb->len) {
int delta = len - skb->len;
if (skb->data_len > 0 ||
skb->tail + delta > skb->end) {
teprintk("kvblade: failed to expand SKB as it is non-linear or does not have enough space (len=%d skb->len=%d)\n", len, skb->len);
ata->errfeat = ATA_ABORTED;
break;
}
skb_put(skb, delta);
}
rq->d = d;
rq->t = t;
rq->skb = skb;
bio_sector(bio) = lba;
bio_set_dev(bio, d->blkdev);
bio->bi_end_io = ata_io_complete;
bio->bi_private = rq;
if (skb_add_pages(skb, bio, data_len) <= 0) {
kmem_cache_free(root.aoe_rq_cache, rq);
teprintk("kvblade: can't bio_add_page for %d sectors\n", ata->scnt);
goto drop;
}
dt = (struct aoetarget_thread*)per_cpu_ptr(d->devthread_percpu, get_cpu());
atomic_inc(&dt->busy);
put_cpu();
if (rw == WRITE) {
bio_set_op_attrs(bio, REQ_OP_WRITE, 0);
} else {
bio_set_op_attrs(bio, REQ_OP_READ, 0);
}
submit_bio(bio);
return NULL;
default: