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
|
/* $OpenBSD: softraid_aoe.c,v 1.36 2014/01/21 04:23:14 jsing Exp $ */
/*
* Copyright (c) 2008 Ted Unangst <tedu@openbsd.org>
* Copyright (c) 2008 Marco Peereboom <marco@openbsd.org>
*
* Permission to use, copy, modify, and distribute this software for any
* purpose with or without fee is hereby granted, provided that the above
* copyright notice and this permission notice appear in all copies.
*
* THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
* WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
* MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
* ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
* WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
* ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
* OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
*/
#include "bio.h"
#include <sys/param.h>
#include <sys/systm.h>
#include <sys/buf.h>
#include <sys/device.h>
#include <sys/ioctl.h>
#include <sys/proc.h>
#include <sys/malloc.h>
#include <sys/kernel.h>
#include <sys/kthread.h>
#include <sys/disk.h>
#include <sys/rwlock.h>
#include <sys/queue.h>
#include <sys/fcntl.h>
#include <sys/disklabel.h>
#include <sys/mount.h>
#include <sys/sensors.h>
#include <sys/stat.h>
#include <sys/conf.h>
#include <sys/uio.h>
#include <scsi/scsi_all.h>
#include <scsi/scsiconf.h>
#include <scsi/scsi_disk.h>
#include <dev/softraidvar.h>
#include <dev/rndvar.h>
#include <sys/socket.h>
#include <sys/mbuf.h>
#include <sys/socketvar.h>
#include <net/if.h>
#include <netinet/in.h>
#include <net/ethertypes.h>
#include <netinet/if_ether.h>
#include <net/if_aoe.h>
/* AOE initiator functions. */
int sr_aoe_create(struct sr_discipline *, struct bioc_createraid *,
int, int64_t);
int sr_aoe_assemble(struct sr_discipline *, struct bioc_createraid *,
int, void *);
int sr_aoe_alloc_resources(struct sr_discipline *);
void sr_aoe_free_resources(struct sr_discipline *);
int sr_aoe_rw(struct sr_workunit *);
/* AOE target functions. */
int sr_aoe_server_create(struct sr_discipline *, struct bioc_createraid *,
int, int64_t);
int sr_aoe_server_assemble(struct sr_discipline *, struct bioc_createraid *,
int, void *);
int sr_aoe_server_alloc_resources(struct sr_discipline *);
void sr_aoe_server_free_resources(struct sr_discipline *);
int sr_aoe_server_start(struct sr_discipline *);
void sr_aoe_request_done(struct aoe_req *, struct aoe_packet *);
void sr_aoe_input(struct aoe_handler *, struct mbuf *);
void sr_aoe_setup(struct aoe_handler *, struct mbuf *);
void sr_aoe_timeout(void *);
/* Discipline initialisation. */
void
sr_aoe_discipline_init(struct sr_discipline *sd)
{
/* Fill out discipline members. */
sd->sd_type = SR_MD_AOE_INIT;
strlcpy(sd->sd_name, "AOE INIT", sizeof(sd->sd_name));
sd->sd_capabilities = SR_CAP_SYSTEM_DISK;
sd->sd_max_wu = SR_RAIDAOE_NOWU;
/* Setup discipline specific function pointers. */
sd->sd_alloc_resources = sr_aoe_alloc_resources;
sd->sd_assemble = sr_aoe_assemble;
sd->sd_create = sr_aoe_create;
sd->sd_free_resources = sr_aoe_free_resources;
sd->sd_scsi_rw = sr_aoe_rw;
sd->sd_scsi_intr = NULL;
}
void
sr_aoe_server_discipline_init(struct sr_discipline *sd)
{
/* Fill out discipline members. */
sd->sd_type = SR_MD_AOE_TARG;
strlcpy(sd->sd_name, "AOE TARG", sizeof(sd->sd_name));
sd->sd_capabilities = 0;
sd->sd_max_wu = SR_RAIDAOE_NOWU;
/* Setup discipline specific function pointers. */
sd->sd_alloc_resources = sr_aoe_server_alloc_resources;
sd->sd_assemble = sr_aoe_server_assemble;
sd->sd_create = sr_aoe_server_create;
sd->sd_free_resources = sr_aoe_server_free_resources;
sd->sd_scsi_inquiry = NULL;
sd->sd_scsi_read_cap = NULL;
sd->sd_scsi_tur = NULL;
sd->sd_scsi_req_sense = NULL;
sd->sd_scsi_start_stop = NULL;
sd->sd_scsi_sync = NULL;
sd->sd_scsi_rw = NULL;
sd->sd_scsi_intr = NULL;
sd->sd_set_chunk_state = NULL;
sd->sd_set_vol_state = NULL;
sd->sd_start_discipline = sr_aoe_server_start;
}
/* AOE initiator */
int
sr_aoe_create(struct sr_discipline *sd, struct bioc_createraid *bc,
int no_chunk, int64_t coerced_size)
{
if (no_chunk != 1) {
sr_error(sd->sd_sc, "%s requires exactly one chunk",
sd->sd_name);
return EINVAL;
}
sd->sd_max_ccb_per_wu = no_chunk;
return 0;
}
int
sr_aoe_assemble(struct sr_discipline *sd, struct bioc_createraid *bc,
int no_chunk, void *data)
{
struct ifnet *ifp;
struct aoe_handler *ah;
struct sr_aoe_config sri;
int rv, s;
#if 0
struct mbuf *m;
struct ether_header *eh;
struct aoe_packet *ap;
int rv;
#endif
if (!(bc->bc_opaque_flags & BIOC_SOIN))
return (EINVAL);
if (bc->bc_opaque_size != sizeof(sri))
return (EINVAL);
if ((rv = copyin(bc->bc_opaque, &sri, sizeof(sri))))
return (rv);
sri.nic[sizeof(sri.nic) - 1] = 0;
sd->sd_max_ccb_per_wu = sd->sd_meta->ssdi.ssd_chunk_no;
ifp = ifunit(sri.nic);
if (!ifp)
return (EINVAL);
ah = malloc(sizeof(*ah), M_DEVBUF, M_WAITOK | M_ZERO);
ah->ifp = ifp;
ah->major = sri.shelf;
ah->minor = sri.slot;
ah->fn = (workq_fn)sr_aoe_input;
TAILQ_INIT(&ah->reqs);
s = splnet();
TAILQ_INSERT_TAIL(&aoe_handlers, ah, next);
splx(s);
sd->mds.mdd_aoe.sra_ah = ah;
sd->mds.mdd_aoe.sra_eaddr = sri.dsteaddr;
#if 0
MGETHDR(m, M_WAIT, MT_HEADER);
eh = mtod(m, struct ether_header *);
memcpy(eh->ether_dhost, sd->mds.mdd_aoe.sra_eaddr, 6);
memcpy(eh->ether_shost, ((struct arpcom *)ifp)->ac_enaddr, 6);
eh->ether_type = htons(ETHERTYPE_AOE);
ap = (struct aoe_packet *)&eh[1];
ap->vers = 1;
ap->flags = 0;
ap->error = 0;
ap->major = sri.shelf;
ap->minor = sri.slot;
ap->command = 1;
ap->tag = 0;
ap->buffercnt = 0;
ap->firmwarevers = 0;
ap->configsectorcnt = 0;
ap->serververs = 0;
ap->ccmd = 0;
ap->configstringlen = 0;
m->m_pkthdr.len = m->m_len = AOE_CFGHDRLEN;
s = splnet();
IFQ_ENQUEUE(&ifp->if_snd, m, NULL, rv);
if ((ifp->if_flags & IFF_OACTIVE) == 0)
(*ifp->if_start)(ifp);
rv = tsleep(ah, PRIBIO|PCATCH, "aoesetup", 30 * hz);
splx(s);
if (rv) {
s = splnet();
TAILQ_REMOVE(&aoe_handlers, ah, next);
splx(s);
free(ah, M_DEVBUF);
return rv;
}
#endif
return 0;
}
void
sr_aoe_setup(struct aoe_handler *ah, struct mbuf *m)
{
struct aoe_packet *ap;
int s;
ap = mtod(m, struct aoe_packet *);
if (ap->command != 1)
goto out;
if (ap->tag != 0)
goto out;
s = splnet();
ah->fn = (workq_fn)sr_aoe_input;
wakeup(ah);
splx(s);
out:
m_freem(m);
}
int
sr_aoe_alloc_resources(struct sr_discipline *sd)
{
DNPRINTF(SR_D_DIS, "%s: sr_aoe_alloc_resources\n",
DEVNAME(sd->sd_sc));
sr_wu_alloc(sd, sizeof(struct sr_workunit));
sr_ccb_alloc(sd);
return 0;
}
void
sr_aoe_free_resources(struct sr_discipline *sd)
{
struct aoe_handler *ah;
int s;
DNPRINTF(SR_D_DIS, "%s: sr_aoe_free_resources\n",
DEVNAME(sd->sd_sc));
ah = sd->mds.mdd_aoe.sra_ah;
if (ah) {
s = splnet();
TAILQ_REMOVE(&aoe_handlers, ah, next);
splx(s);
free(ah, M_DEVBUF);
}
if (sd->sd_meta)
free(sd->sd_meta, M_DEVBUF);
sr_wu_free(sd);
sr_ccb_free(sd);
}
int sr_send_aoe_chunk(struct sr_workunit *wu, daddr_t blk, int i);
int
sr_send_aoe_chunk(struct sr_workunit *wu, daddr_t blk, int i)
{
struct sr_discipline *sd = wu->swu_dis;
struct scsi_xfer *xs = wu->swu_xs;
int s;
daddr_t fragblk;
struct mbuf *m;
struct ether_header *eh;
struct aoe_packet *ap;
struct ifnet *ifp;
struct aoe_handler *ah;
struct aoe_req *ar;
int tag, rv;
int fragsize;
const int aoe_frags = 2;
fragblk = blk + aoe_frags * i;
fragsize = aoe_frags * 512;
if (fragblk + aoe_frags - 1 > wu->swu_blk_end) {
fragsize = (wu->swu_blk_end - fragblk + 1) * 512;
}
tag = ++sd->mds.mdd_aoe.sra_tag;
ah = sd->mds.mdd_aoe.sra_ah;
ar = malloc(sizeof(*ar), M_DEVBUF, M_NOWAIT);
if (!ar) {
splx(s);
return ENOMEM;
}
ar->v = wu;
ar->tag = tag;
ar->len = fragsize;
timeout_set(&ar->to, sr_aoe_timeout, ar);
TAILQ_INSERT_TAIL(&ah->reqs, ar, next);
splx(s);
ifp = ah->ifp;
MGETHDR(m, M_DONTWAIT, MT_HEADER);
if (xs->flags & SCSI_DATA_OUT && m) {
MCLGET(m, M_DONTWAIT);
if (!(m->m_flags & M_EXT)) {
m_freem(m);
m = NULL;
}
}
if (!m) {
s = splbio();
TAILQ_REMOVE(&ah->reqs, ar, next);
splx(s);
free(ar, M_DEVBUF);
return ENOMEM;
}
eh = mtod(m, struct ether_header *);
memcpy(eh->ether_dhost, &sd->mds.mdd_aoe.sra_eaddr, ETHER_ADDR_LEN);
memcpy(eh->ether_shost, ((struct arpcom *)ifp)->ac_enaddr,
ETHER_ADDR_LEN);
eh->ether_type = htons(ETHERTYPE_AOE);
ap = (struct aoe_packet *)&eh[1];
ap->vers = 1;
ap->flags = 0;
ap->error = 0;
ap->major = ah->major;
ap->minor = ah->minor;
ap->command = 0;
ap->tag = tag;
ap->aflags = 0; /* AOE_EXTENDED; */
if (xs->flags & SCSI_DATA_OUT) {
ap->aflags |= AOE_WRITE;
ap->cmd = AOE_WRITE;
memcpy(ap->data, xs->data + (aoe_frags * i * 512), fragsize);
} else {
ap->cmd = AOE_READ;
}
ap->feature = 0;
ap->sectorcnt = fragsize / 512;
AOE_BLK2HDR(fragblk, ap);
m->m_pkthdr.len = m->m_len = AOE_CMDHDRLEN + fragsize;
s = splnet();
IFQ_ENQUEUE(&ifp->if_snd, m, NULL, rv);
if ((ifp->if_flags & IFF_OACTIVE) == 0)
(*ifp->if_start)(ifp);
if (rv == 0)
timeout_add_sec(&ar->to, 10);
splx(s);
if (rv) {
s = splbio();
TAILQ_REMOVE(&ah->reqs, ar, next);
splx(s);
free(ar, M_DEVBUF);
}
return rv;
}
int
sr_aoe_rw(struct sr_workunit *wu)
{
struct sr_discipline *sd = wu->swu_dis;
struct scsi_xfer *xs = wu->swu_xs;
struct sr_chunk *scp;
daddr_t blk;
int s, ios, rt;
int rv, i;
const int aoe_frags = 2;
DNPRINTF(SR_D_DIS, "%s: sr_aoe_rw 0x%02x\n", DEVNAME(sd->sd_sc),
xs->cmd->opcode);
/* blk and scsi error will be handled by sr_validate_io */
if (sr_validate_io(wu, &blk, "sr_aoe_rw"))
goto bad;
/* add 1 to get the inclusive amount, then some more for rounding */
ios = (wu->swu_blk_end - wu->swu_blk_start + 1 + (aoe_frags - 1)) /
aoe_frags;
wu->swu_io_count = ios;
if (xs->flags & SCSI_POLL)
panic("can't AOE poll");
s = splbio();
for (i = 0; i < ios; i++) {
if (xs->flags & SCSI_DATA_IN) {
rt = 0;
ragain:
scp = sd->sd_vol.sv_chunks[0];
switch (scp->src_meta.scm_status) {
case BIOC_SDONLINE:
case BIOC_SDSCRUB:
break;
case BIOC_SDOFFLINE:
case BIOC_SDREBUILD:
case BIOC_SDHOTSPARE:
if (rt++ < sd->sd_meta->ssdi.ssd_chunk_no)
goto ragain;
/* FALLTHROUGH */
default:
/* volume offline */
printf("%s: is offline, can't read\n",
DEVNAME(sd->sd_sc));
goto bad;
}
} else {
scp = sd->sd_vol.sv_chunks[0];
switch (scp->src_meta.scm_status) {
case BIOC_SDONLINE:
case BIOC_SDSCRUB:
case BIOC_SDREBUILD:
break;
case BIOC_SDHOTSPARE: /* should never happen */
case BIOC_SDOFFLINE:
wu->swu_io_count--;
goto bad;
default:
goto bad;
}
}
rv = sr_send_aoe_chunk(wu, blk, i);
if (rv) {
return rv;
}
}
return (0);
bad:
/* wu is unwound by sr_wu_put */
return (1);
}
void
sr_aoe_request_done(struct aoe_req *ar, struct aoe_packet *ap)
{
struct sr_discipline *sd;
struct scsi_xfer *xs;
struct sr_workunit *wu;
daddr_t blk;
int64_t offset;
int len, s;
wu = ar->v;
sd = wu->swu_dis;
xs = wu->swu_xs;
if (!ap || ap->flags & AOE_F_ERROR) {
wu->swu_ios_failed++;
} else {
wu->swu_ios_succeeded++;
len = ar->len; /* XXX check against sector count */
if (xs->flags & SCSI_DATA_IN) {
AOE_HDR2BLK(ap, blk);
/* XXX bounds checking */
offset = (wu->swu_blk_start - blk) * 512;
memcpy(xs->data + offset, ap->data, len);
}
}
wu->swu_ios_complete++;
s = splbio();
if (wu->swu_ios_complete == wu->swu_io_count) {
if (wu->swu_ios_failed == wu->swu_ios_complete)
xs->error = XS_DRIVER_STUFFUP;
else
xs->error = XS_NOERROR;
sr_scsi_done(sd, xs);
}
splx(s);
free(ar, M_DEVBUF);
}
void
sr_aoe_input(struct aoe_handler *ah, struct mbuf *m)
{
struct aoe_packet *ap;
struct aoe_req *ar;
int tag;
int s;
ap = mtod(m, struct aoe_packet *);
tag = ap->tag;
s = splnet();
TAILQ_FOREACH(ar, &ah->reqs, next) {
if (ar->tag == tag) {
timeout_del(&ar->to);
TAILQ_REMOVE(&ah->reqs, ar, next);
break;
}
}
splx(s);
if (!ar)
goto out;
ap = mtod(m, struct aoe_packet *);
sr_aoe_request_done(ar, ap);
out:
m_freem(m);
}
void
sr_aoe_timeout(void *v)
{
struct aoe_req *ar = v;
struct sr_discipline *sd;
struct scsi_xfer *xs;
struct aoe_handler *ah;
struct sr_workunit *wu;
int s;
wu = ar->v;
sd = wu->swu_dis;
xs = wu->swu_xs;
ah = sd->mds.mdd_aoe.sra_ah;
s = splnet();
TAILQ_REMOVE(&ah->reqs, ar, next);
splx(s);
sr_aoe_request_done(ar, NULL);
}
/* AOE target */
void sr_aoe_server(struct aoe_handler *, struct mbuf *);
void sr_aoe_server_create_thread(void *);
void sr_aoe_server_thread(void *);
int
sr_aoe_server_create(struct sr_discipline *sd, struct bioc_createraid *bc,
int no_chunk, int64_t coerced_size)
{
if (no_chunk != 1) {
sr_error(sd->sd_sc, "%s requires exactly one chunk",
sd->sd_name);
return EINVAL;
}
sd->sd_meta->ssdi.ssd_size = coerced_size;
strlcpy(sd->sd_name, "AOE TARG", sizeof(sd->sd_name));
sd->sd_max_ccb_per_wu = no_chunk;
return 0;
}
int
sr_aoe_server_assemble(struct sr_discipline *sd, struct bioc_createraid *bc,
int no_chunk, void *data)
{
sd->sd_max_ccb_per_wu = sd->sd_meta->ssdi.ssd_chunk_no;
return 0;
}
int
sr_aoe_server_alloc_resources(struct sr_discipline *sd)
{
int s, rv = EINVAL;
unsigned char slot;
unsigned short shelf;
const char *nic;
struct aoe_handler *ah;
struct ifnet *ifp;
DNPRINTF(SR_D_DIS, "%s: sr_aoe_server_alloc_resources\n",
DEVNAME(sd->sd_sc));
/* setup runtime values */
/* XXX where do these come from */
slot = 3;
shelf = 4;
nic = "re0";
ifp = ifunit(nic);
if (!ifp) {
printf("%s: sr_aoe_server_alloc_resources: illegal interface "
"%s\n", DEVNAME(sd->sd_sc), nic);
return (EINVAL);
}
shelf = htons(shelf);
ah = malloc(sizeof(*ah), M_DEVBUF, M_WAITOK | M_ZERO);
ah->ifp = ifp;
ah->major = shelf;
ah->minor = slot;
ah->fn = (workq_fn)sr_aoe_server;
TAILQ_INIT(&ah->reqs);
s = splnet();
TAILQ_INSERT_TAIL(&aoe_handlers, ah, next);
splx(s);
sd->mds.mdd_aoe.sra_ah = ah;
memset(&sd->mds.mdd_aoe.sra_eaddr, 0xff,
sizeof(sd->mds.mdd_aoe.sra_eaddr));
sd->mds.mdd_aoe.sra_ifp = ifp;
if (sr_wu_alloc(sd))
goto bad;
if (sr_ccb_alloc(sd))
goto bad;
rv = 0;
bad:
return (rv);
}
void
sr_aoe_server_free_resources(struct sr_discipline *sd)
{
int s;
DNPRINTF(SR_D_DIS, "%s: sr_aoe_server_free_resources\n",
DEVNAME(sd->sd_sc));
s = splnet();
if (sd->mds.mdd_aoe.sra_ah) {
TAILQ_REMOVE(&aoe_handlers, sd->mds.mdd_aoe.sra_ah, next);
free(sd->mds.mdd_aoe.sra_ah, M_DEVBUF);
}
splx(s);
sr_wu_free(sd);
sr_ccb_free(sd);
}
int
sr_aoe_server_start(struct sr_discipline *sd)
{
kthread_create_deferred(sr_aoe_server_create_thread, sd);
return (0);
}
void
sr_aoe_server_create_thread(void *arg)
{
struct sr_discipline *sd = arg;
if (kthread_create(sr_aoe_server_thread, arg, NULL, DEVNAME(sd->sd_sc))
!= 0) {
printf("%s: unable to create AOE thread\n",
DEVNAME(sd->sd_sc));
/* XXX unwind */
return;
}
}
void
sr_aoe_server_thread(void *arg)
{
struct sr_discipline *sd = arg;
struct ifnet *ifp;
struct aoe_handler *ah;
struct aoe_req *ar;
struct aoe_packet *rp, *ap;
struct mbuf *m, *m2;
struct ether_header *eh;
struct buf buf;
daddr_t blk;
int len;
int rv, s;
/* sanity */
if (!sd)
return;
ah = sd->mds.mdd_aoe.sra_ah;
if (ah == NULL)
return;
ifp = sd->mds.mdd_aoe.sra_ifp;
if (ifp == NULL)
return;
printf("%s: AOE target: %s exported via: %s\n",
DEVNAME(sd->sd_sc), sd->sd_meta->ssd_devname, ifp->if_xname);
while (1) {
s = splnet();
resleep:
rv = tsleep(ah, PCATCH | PRIBIO, "aoe targ", 0);
if (rv) {
splx(s);
break;
}
ar = TAILQ_FIRST(&ah->reqs);
if (!ar) {
goto resleep;
}
TAILQ_REMOVE(&ah->reqs, ar, next);
splx(s);
m2 = ar->v;
rp = mtod(m2, struct aoe_packet *);
if (rp->command) {
continue;
}
if (rp->aflags & AOE_AF_WRITE) {
MGETHDR(m, M_DONTWAIT, MT_HEADER);
if (!m)
continue;
len = rp->sectorcnt * 512;
eh = mtod(m, struct ether_header *);
memcpy(eh->ether_dhost, &sd->mds.mdd_aoe.sra_eaddr,
ETHER_ADDR_LEN);
memcpy(eh->ether_shost,
((struct arpcom *)ifp)->ac_enaddr, ETHER_ADDR_LEN);
eh->ether_type = htons(ETHERTYPE_AOE);
ap = (struct aoe_packet *)&eh[1];
AOE_HDR2BLK(ap, blk);
bzero(&buf, sizeof(buf));
buf.b_blkno = blk;
buf.b_flags = B_WRITE | B_PHYS;
buf.b_bcount = len;
buf.b_bufsize = len;
buf.b_resid = len;
buf.b_data = rp->data;
buf.b_error = 0;
buf.b_proc = curproc;
buf.b_dev = sd->sd_vol.sv_chunks[0]->src_dev_mm;
buf.b_vp = sd->sd_vol.sv_chunks[0]->src_vn;
if ((buf.b_flags & B_READ) == 0)
buf.b_vp->v_numoutput++;
LIST_INIT(&buf.b_dep);
s = splbio();
VOP_STRATEGY(&buf);
biowait(&buf);
splx(s);
ap->vers = 1;
ap->flags = AOE_F_RESP;
ap->error = 0;
ap->major = rp->major;
ap->minor = rp->minor;
ap->command = 1;
ap->tag = rp->tag;
ap->aflags = rp->aflags;
ap->feature = 0;
ap->sectorcnt = len / 512;
ap->cmd = AOE_WRITE;
ap->lba0 = 0;
ap->lba1 = 0;
ap->lba2 = 0;
ap->lba3 = 0;
ap->lba4 = 0;
ap->lba5 = 0;
ap->reserved = 0;
m->m_pkthdr.len = m->m_len = AOE_CMDHDRLEN;
s = splnet();
IFQ_ENQUEUE(&ifp->if_snd, m, NULL, rv);
if ((ifp->if_flags & IFF_OACTIVE) == 0)
(*ifp->if_start)(ifp);
splx(s);
} else {
MGETHDR(m, M_DONTWAIT, MT_HEADER);
if (m) {
MCLGET(m, M_DONTWAIT);
if (!(m->m_flags & M_EXT)) {
m_freem(m);
m = NULL;
}
}
if (!m)
continue;
len = rp->sectorcnt * 512;
eh = mtod(m, struct ether_header *);
memcpy(eh->ether_dhost, &sd->mds.mdd_aoe.sra_eaddr,
ETHER_ADDR_LEN);
memcpy(eh->ether_shost,
((struct arpcom *)ifp)->ac_enaddr, ETHER_ADDR_LEN);
eh->ether_type = htons(ETHERTYPE_AOE);
ap = (struct aoe_packet *)&eh[1];
AOE_HDR2BLK(ap, blk);
memset(&buf, 0, sizeof buf);
buf.b_blkno = blk;
buf.b_flags = B_WRITE | B_PHYS;
buf.b_bcount = len;
buf.b_bufsize = len;
buf.b_resid = len;
buf.b_data = ap->data;
buf.b_error = 0;
buf.b_proc = curproc;
buf.b_dev = sd->sd_vol.sv_chunks[0]->src_dev_mm;
buf.b_vp = sd->sd_vol.sv_chunks[0]->src_vn;
if ((buf.b_flags & B_READ) == 0)
buf.b_vp->v_numoutput++;
LIST_INIT(&buf.b_dep);
s = splbio();
VOP_STRATEGY(&buf);
biowait(&buf);
splx(s);
ap->vers = 1;
ap->flags = AOE_F_RESP;
ap->error = 0;
ap->major = rp->major;
ap->minor = rp->minor;
ap->command = 1;
ap->tag = rp->tag;
ap->aflags = rp->aflags;
ap->feature = 0;
ap->sectorcnt = len / 512;
ap->cmd = AOE_READ;
ap->lba0 = 0;
ap->lba1 = 0;
ap->lba2 = 0;
ap->lba3 = 0;
ap->lba4 = 0;
ap->lba5 = 0;
ap->reserved = 0;
m->m_pkthdr.len = m->m_len = AOE_CMDHDRLEN;
s = splnet();
IFQ_ENQUEUE(&ifp->if_snd, m, NULL, rv);
if ((ifp->if_flags & IFF_OACTIVE) == 0)
(*ifp->if_start)(ifp);
splx(s);
}
}
}
void
sr_aoe_server(struct aoe_handler *ah, struct mbuf *m)
{
struct aoe_req *ar;
int s;
ar = malloc(sizeof *ar, M_DEVBUF, M_NOWAIT);
if (!ar) {
/* XXX warning? */
m_freem(m);
return;
}
ar->v = m;
s = splnet();
TAILQ_INSERT_TAIL(&ah->reqs, ar, next);
wakeup(ah);
splx(s);
}
|