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#include <linux/kernel.h>
33#include <linux/init.h>
34#include <linux/types.h>
35#include <linux/sched.h>
36#include <linux/pci.h>
37#include <linux/spinlock.h>
38#include <linux/slab.h>
39#include <linux/completion.h>
40#include <linux/blkdev.h>
41#include <asm/semaphore.h>
42
43#include "aacraid.h"
44
45
46
47
48
49
50
51
52
53static int fib_map_alloc(struct aac_dev *dev)
54{
55 if((dev->hw_fib_va = pci_alloc_consistent(dev->pdev, sizeof(struct hw_fib) * AAC_NUM_FIB, &dev->hw_fib_pa))==NULL)
56 return -ENOMEM;
57 return 0;
58}
59
60
61
62
63
64
65
66
67
68void fib_map_free(struct aac_dev *dev)
69{
70 pci_free_consistent(dev->pdev, sizeof(struct hw_fib) * AAC_NUM_FIB, dev->hw_fib_va, dev->hw_fib_pa);
71}
72
73
74
75
76
77
78
79
80
81int fib_setup(struct aac_dev * dev)
82{
83 struct fib *fibptr;
84 struct hw_fib *hw_fib_va;
85 dma_addr_t hw_fib_pa;
86 int i;
87
88 if(fib_map_alloc(dev)<0)
89 return -ENOMEM;
90
91 hw_fib_va = dev->hw_fib_va;
92 hw_fib_pa = dev->hw_fib_pa;
93 memset(hw_fib_va, 0, sizeof(struct hw_fib) * AAC_NUM_FIB);
94
95
96
97 for (i = 0, fibptr = &dev->fibs[i]; i < AAC_NUM_FIB; i++, fibptr++)
98 {
99 fibptr->dev = dev;
100 fibptr->hw_fib = hw_fib_va;
101 fibptr->data = (void *) fibptr->hw_fib->data;
102 fibptr->next = fibptr+1;
103 init_MUTEX_LOCKED(&fibptr->event_wait);
104 spin_lock_init(&fibptr->event_lock);
105 hw_fib_va->header.XferState = cpu_to_le32(0xffffffff);
106 hw_fib_va->header.SenderSize = cpu_to_le16(sizeof(struct hw_fib));
107 fibptr->hw_fib_pa = hw_fib_pa;
108 hw_fib_va = (struct hw_fib *)((unsigned char *)hw_fib_va + sizeof(struct hw_fib));
109 hw_fib_pa = hw_fib_pa + sizeof(struct hw_fib);
110 }
111
112
113
114 dev->fibs[AAC_NUM_FIB-1].next = NULL;
115
116
117
118 dev->free_fib = &dev->fibs[0];
119 return 0;
120}
121
122
123
124
125
126
127
128
129
130struct fib * fib_alloc(struct aac_dev *dev)
131{
132 struct fib * fibptr;
133 unsigned long flags;
134 spin_lock_irqsave(&dev->fib_lock, flags);
135 fibptr = dev->free_fib;
136
137
138 if(!fibptr)
139 BUG();
140 dev->free_fib = fibptr->next;
141 spin_unlock_irqrestore(&dev->fib_lock, flags);
142
143
144
145 fibptr->type = FSAFS_NTC_FIB_CONTEXT;
146 fibptr->size = sizeof(struct fib);
147
148
149
150
151 fibptr->hw_fib->header.XferState = cpu_to_le32(0);
152 fibptr->callback = NULL;
153 fibptr->callback_data = NULL;
154
155 return fibptr;
156}
157
158
159
160
161
162
163
164
165
166void fib_free(struct fib * fibptr)
167{
168 unsigned long flags;
169
170 spin_lock_irqsave(&fibptr->dev->fib_lock, flags);
171 if (fibptr->flags & FIB_CONTEXT_FLAG_TIMED_OUT) {
172 aac_config.fib_timeouts++;
173 fibptr->next = fibptr->dev->timeout_fib;
174 fibptr->dev->timeout_fib = fibptr;
175 } else {
176 if (fibptr->hw_fib->header.XferState != 0) {
177 printk(KERN_WARNING "fib_free, XferState != 0, fibptr = 0x%p, XferState = 0x%x\n",
178 (void*)fibptr, fibptr->hw_fib->header.XferState);
179 }
180 fibptr->next = fibptr->dev->free_fib;
181 fibptr->dev->free_fib = fibptr;
182 }
183 spin_unlock_irqrestore(&fibptr->dev->fib_lock, flags);
184}
185
186
187
188
189
190
191
192
193void fib_init(struct fib *fibptr)
194{
195 struct hw_fib *hw_fib = fibptr->hw_fib;
196
197 hw_fib->header.StructType = FIB_MAGIC;
198 hw_fib->header.Size = cpu_to_le16(sizeof(struct hw_fib));
199 hw_fib->header.XferState = cpu_to_le32(HostOwned | FibInitialized | FibEmpty | FastResponseCapable);
200 hw_fib->header.SenderFibAddress = cpu_to_le32(fibptr->hw_fib_pa);
201 hw_fib->header.ReceiverFibAddress = cpu_to_le32(fibptr->hw_fib_pa);
202 hw_fib->header.SenderSize = cpu_to_le16(sizeof(struct hw_fib));
203}
204
205
206
207
208
209
210
211
212
213void fib_dealloc(struct fib * fibptr)
214{
215 struct hw_fib *hw_fib = fibptr->hw_fib;
216 if(hw_fib->header.StructType != FIB_MAGIC)
217 BUG();
218 hw_fib->header.XferState = cpu_to_le32(0);
219}
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241static int aac_get_entry (struct aac_dev * dev, u32 qid, struct aac_entry **entry, u32 * index, unsigned long *nonotify)
242{
243 struct aac_queue * q;
244
245
246
247
248
249
250
251
252 q = &dev->queues->queue[qid];
253
254 *index = le32_to_cpu(*(q->headers.producer));
255 if ((*index - 2) == le32_to_cpu(*(q->headers.consumer)))
256 *nonotify = 1;
257
258 if (qid == AdapHighCmdQueue) {
259 if (*index >= ADAP_HIGH_CMD_ENTRIES)
260 *index = 0;
261 } else if (qid == AdapNormCmdQueue) {
262 if (*index >= ADAP_NORM_CMD_ENTRIES)
263 *index = 0;
264 }
265 else if (qid == AdapHighRespQueue)
266 {
267 if (*index >= ADAP_HIGH_RESP_ENTRIES)
268 *index = 0;
269 }
270 else if (qid == AdapNormRespQueue)
271 {
272 if (*index >= ADAP_NORM_RESP_ENTRIES)
273 *index = 0;
274 }
275 else {
276 printk("aacraid: invalid qid\n");
277 BUG();
278 }
279
280 if ((*index + 1) == le32_to_cpu(*(q->headers.consumer))) {
281 printk(KERN_WARNING "Queue %d full, %d outstanding.\n",
282 qid, q->numpending);
283 return 0;
284 } else {
285 *entry = q->base + *index;
286 return 1;
287 }
288}
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306static int aac_queue_get(struct aac_dev * dev, u32 * index, u32 qid, struct hw_fib * hw_fib, int wait, struct fib * fibptr, unsigned long *nonotify)
307{
308 struct aac_entry * entry = NULL;
309 int map = 0;
310 struct aac_queue * q = &dev->queues->queue[qid];
311
312 spin_lock_irqsave(q->lock, q->SavedIrql);
313
314 if (qid == AdapHighCmdQueue || qid == AdapNormCmdQueue)
315 {
316
317 while (!aac_get_entry(dev, qid, &entry, index, nonotify))
318 {
319 printk(KERN_ERR "GetEntries failed\n");
320 }
321
322
323
324 entry->size = cpu_to_le32(le16_to_cpu(hw_fib->header.Size));
325 map = 1;
326 }
327 else if (qid == AdapHighRespQueue || qid == AdapNormRespQueue)
328 {
329 while(!aac_get_entry(dev, qid, &entry, index, nonotify))
330 {
331
332 }
333
334
335
336 entry->size = cpu_to_le32(le16_to_cpu(hw_fib->header.Size));
337 entry->addr = hw_fib->header.SenderFibAddress;
338
339 hw_fib->header.ReceiverFibAddress = hw_fib->header.SenderFibAddress;
340 map = 0;
341 }
342
343
344
345
346 if (map)
347 entry->addr = fibptr->hw_fib_pa;
348 return 0;
349}
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365static int aac_insert_entry(struct aac_dev * dev, u32 index, u32 qid, unsigned long nonotify)
366{
367 struct aac_queue * q = &dev->queues->queue[qid];
368
369 if(q == NULL)
370 BUG();
371 *(q->headers.producer) = cpu_to_le32(index + 1);
372 spin_unlock_irqrestore(q->lock, q->SavedIrql);
373
374 if (qid == AdapHighCmdQueue ||
375 qid == AdapNormCmdQueue ||
376 qid == AdapHighRespQueue ||
377 qid == AdapNormRespQueue)
378 {
379 if (!nonotify)
380 aac_adapter_notify(dev, qid);
381 }
382 else
383 printk("Suprise insert!\n");
384 return 0;
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
412int fib_send(u16 command, struct fib * fibptr, unsigned long size, int priority, int wait, int reply, fib_callback callback, void * callback_data)
413{
414 u32 index;
415 u32 qid;
416 struct aac_dev * dev = fibptr->dev;
417 unsigned long nointr = 0;
418 struct hw_fib * hw_fib = fibptr->hw_fib;
419 struct aac_queue * q;
420 unsigned long flags = 0;
421 if (!(le32_to_cpu(hw_fib->header.XferState) & HostOwned))
422 return -EBUSY;
423
424
425
426
427
428
429
430
431
432
433
434 if (wait && !reply) {
435 return -EINVAL;
436 } else if (!wait && reply) {
437 hw_fib->header.XferState |= cpu_to_le32(Async | ResponseExpected);
438 FIB_COUNTER_INCREMENT(aac_config.AsyncSent);
439 } else if (!wait && !reply) {
440 hw_fib->header.XferState |= cpu_to_le32(NoResponseExpected);
441 FIB_COUNTER_INCREMENT(aac_config.NoResponseSent);
442 } else if (wait && reply) {
443 hw_fib->header.XferState |= cpu_to_le32(ResponseExpected);
444 FIB_COUNTER_INCREMENT(aac_config.NormalSent);
445 }
446
447
448
449
450 hw_fib->header.SenderFibAddress = cpu_to_le32(((u32)(fibptr-dev->fibs)) << 1);
451 hw_fib->header.SenderData = (u32)(fibptr - dev->fibs);
452
453
454
455
456
457
458
459 hw_fib->header.Command = cpu_to_le16(command);
460 hw_fib->header.XferState |= cpu_to_le32(SentFromHost);
461 fibptr->hw_fib->header.Flags = 0;
462
463
464
465 hw_fib->header.Size = cpu_to_le16(sizeof(struct aac_fibhdr) + size);
466 if (le16_to_cpu(hw_fib->header.Size) > le16_to_cpu(hw_fib->header.SenderSize)) {
467 return -EMSGSIZE;
468 }
469
470
471
472
473 if (priority == FsaHigh) {
474 hw_fib->header.XferState |= cpu_to_le32(HighPriority);
475 qid = AdapHighCmdQueue;
476 } else {
477 hw_fib->header.XferState |= cpu_to_le32(NormalPriority);
478 qid = AdapNormCmdQueue;
479 }
480 q = &dev->queues->queue[qid];
481
482 if(wait)
483 spin_lock_irqsave(&fibptr->event_lock, flags);
484 if(aac_queue_get( dev, &index, qid, hw_fib, 1, fibptr, &nointr)<0)
485 return -EWOULDBLOCK;
486 dprintk((KERN_DEBUG "fib_send: inserting a queue entry at index %d.\n",index));
487 dprintk((KERN_DEBUG "Fib contents:.\n"));
488 dprintk((KERN_DEBUG " Command = %d.\n", hw_fib->header.Command));
489 dprintk((KERN_DEBUG " XferState = %x.\n", hw_fib->header.XferState));
490 dprintk((KERN_DEBUG " hw_fib va being sent=%p\n",fibptr->hw_fib));
491 dprintk((KERN_DEBUG " hw_fib pa being sent=%lx\n",(ulong)fibptr->hw_fib_pa));
492 dprintk((KERN_DEBUG " fib being sent=%p\n",fibptr));
493
494
495
496
497 if (!wait) {
498 fibptr->callback = callback;
499 fibptr->callback_data = callback_data;
500 }
501 FIB_COUNTER_INCREMENT(aac_config.FibsSent);
502 list_add_tail(&fibptr->queue, &q->pendingq);
503 q->numpending++;
504
505 fibptr->done = 0;
506 fibptr->flags = 0;
507
508 if(aac_insert_entry(dev, index, qid, (nointr & aac_config.irq_mod)) < 0)
509 return -EWOULDBLOCK;
510
511
512
513
514 if (wait) {
515 spin_unlock_irqrestore(&fibptr->event_lock, flags);
516 down(&fibptr->event_wait);
517 if(fibptr->done == 0)
518 BUG();
519
520 if((fibptr->flags & FIB_CONTEXT_FLAG_TIMED_OUT)){
521 return -ETIMEDOUT;
522 } else {
523 return 0;
524 }
525 }
526
527
528
529
530 if (reply)
531 return -EINPROGRESS;
532 else
533 return 0;
534}
535
536
537
538
539
540
541
542
543
544
545
546
547int aac_consumer_get(struct aac_dev * dev, struct aac_queue * q, struct aac_entry **entry)
548{
549 u32 index;
550 int status;
551 if (le32_to_cpu(*q->headers.producer) == le32_to_cpu(*q->headers.consumer)) {
552 status = 0;
553 } else {
554
555
556
557
558
559 if (le32_to_cpu(*q->headers.consumer) >= q->entries)
560 index = 0;
561 else
562 index = le32_to_cpu(*q->headers.consumer);
563 *entry = q->base + index;
564 status = 1;
565 }
566 return(status);
567}
568
569int aac_consumer_avail(struct aac_dev *dev, struct aac_queue * q)
570{
571 return (le32_to_cpu(*q->headers.producer) != le32_to_cpu(*q->headers.consumer));
572}
573
574
575
576
577
578
579
580
581
582
583
584
585void aac_consumer_free(struct aac_dev * dev, struct aac_queue *q, u32 qid)
586{
587 int wasfull = 0;
588 u32 notify;
589
590 if ((le32_to_cpu(*q->headers.producer)+1) == le32_to_cpu(*q->headers.consumer))
591 wasfull = 1;
592
593 if (le32_to_cpu(*q->headers.consumer) >= q->entries)
594 *q->headers.consumer = cpu_to_le32(1);
595 else
596 *q->headers.consumer = cpu_to_le32(le32_to_cpu(*q->headers.consumer)+1);
597
598 if (wasfull) {
599 switch (qid) {
600
601 case HostNormCmdQueue:
602 notify = HostNormCmdNotFull;
603 break;
604 case HostHighCmdQueue:
605 notify = HostHighCmdNotFull;
606 break;
607 case HostNormRespQueue:
608 notify = HostNormRespNotFull;
609 break;
610 case HostHighRespQueue:
611 notify = HostHighRespNotFull;
612 break;
613 default:
614 BUG();
615 return;
616 }
617 aac_adapter_notify(dev, notify);
618 }
619}
620
621
622
623
624
625
626
627
628
629
630int fib_adapter_complete(struct fib * fibptr, unsigned short size)
631{
632 struct hw_fib * hw_fib = fibptr->hw_fib;
633 struct aac_dev * dev = fibptr->dev;
634 unsigned long nointr = 0;
635 if (le32_to_cpu(hw_fib->header.XferState) == 0)
636 return 0;
637
638
639
640 if ( hw_fib->header.StructType != FIB_MAGIC ) {
641 return -EINVAL;
642 }
643
644
645
646
647
648
649
650 if (hw_fib->header.XferState & cpu_to_le32(SentFromAdapter)) {
651 hw_fib->header.XferState |= cpu_to_le32(HostProcessed);
652 if (hw_fib->header.XferState & cpu_to_le32(HighPriority)) {
653 u32 index;
654 if (size)
655 {
656 size += sizeof(struct aac_fibhdr);
657 if (size > le16_to_cpu(hw_fib->header.SenderSize))
658 return -EMSGSIZE;
659 hw_fib->header.Size = cpu_to_le16(size);
660 }
661 if(aac_queue_get(dev, &index, AdapHighRespQueue, hw_fib, 1, NULL, &nointr) < 0) {
662 return -EWOULDBLOCK;
663 }
664 if (aac_insert_entry(dev, index, AdapHighRespQueue, (nointr & (int)aac_config.irq_mod)) != 0) {
665 }
666 }
667 else if (hw_fib->header.XferState & NormalPriority)
668 {
669 u32 index;
670
671 if (size) {
672 size += sizeof(struct aac_fibhdr);
673 if (size > le16_to_cpu(hw_fib->header.SenderSize))
674 return -EMSGSIZE;
675 hw_fib->header.Size = cpu_to_le16(size);
676 }
677 if (aac_queue_get(dev, &index, AdapNormRespQueue, hw_fib, 1, NULL, &nointr) < 0)
678 return -EWOULDBLOCK;
679 if (aac_insert_entry(dev, index, AdapNormRespQueue, (nointr & (int)aac_config.irq_mod)) != 0)
680 {
681 }
682 }
683 }
684 else
685 {
686 printk(KERN_WARNING "fib_adapter_complete: Unknown xferstate detected.\n");
687 BUG();
688 }
689 return 0;
690}
691
692
693
694
695
696
697
698
699int fib_complete(struct fib * fibptr)
700{
701 struct hw_fib * hw_fib = fibptr->hw_fib;
702
703
704
705
706
707 if (hw_fib->header.XferState == cpu_to_le32(0))
708 return 0;
709
710
711
712
713 if (hw_fib->header.StructType != FIB_MAGIC)
714 return -EINVAL;
715
716
717
718
719
720
721 if((hw_fib->header.XferState & cpu_to_le32(SentFromHost)) &&
722 (hw_fib->header.XferState & cpu_to_le32(AdapterProcessed)))
723 {
724 fib_dealloc(fibptr);
725 }
726 else if(hw_fib->header.XferState & cpu_to_le32(SentFromHost))
727 {
728
729
730
731
732 fib_dealloc(fibptr);
733 } else if(hw_fib->header.XferState & cpu_to_le32(HostOwned)) {
734 fib_dealloc(fibptr);
735 } else {
736 BUG();
737 }
738 return 0;
739}
740
741
742
743
744
745
746
747
748
749
750void aac_printf(struct aac_dev *dev, u32 val)
751{
752 int length = val & 0xffff;
753 int level = (val >> 16) & 0xffff;
754 char *cp = dev->printfbuf;
755
756
757
758
759
760 if (length > 255)
761 length = 255;
762 if (cp[length] != 0)
763 cp[length] = 0;
764 if (level == LOG_AAC_HIGH_ERROR)
765 printk(KERN_WARNING "aacraid:%s", cp);
766 else
767 printk(KERN_INFO "aacraid:%s", cp);
768 memset(cp, 0, 256);
769}
770
771
772
773
774
775
776
777
778
779
780
781int aac_command_thread(struct aac_dev * dev)
782{
783 struct hw_fib *hw_fib, *hw_newfib;
784 struct fib *fib, *newfib;
785 struct aac_queue_block *queues = dev->queues;
786 struct aac_fib_context *fibctx;
787 unsigned long flags;
788 DECLARE_WAITQUEUE(wait, current);
789
790
791
792
793 if (dev->aif_thread)
794 return -EINVAL;
795
796
797
798
799 daemonize("aacraid");
800 allow_signal(SIGKILL);
801
802
803
804 dev->aif_thread = 1;
805 add_wait_queue(&queues->queue[HostNormCmdQueue].cmdready, &wait);
806 set_current_state(TASK_INTERRUPTIBLE);
807 while(1)
808 {
809 spin_lock_irqsave(queues->queue[HostNormCmdQueue].lock, flags);
810 while(!list_empty(&(queues->queue[HostNormCmdQueue].cmdq))) {
811 struct list_head *entry;
812 struct aac_aifcmd * aifcmd;
813
814 set_current_state(TASK_RUNNING);
815
816 entry = queues->queue[HostNormCmdQueue].cmdq.next;
817 list_del(entry);
818
819 spin_unlock_irqrestore(queues->queue[HostNormCmdQueue].lock, flags);
820 fib = list_entry(entry, struct fib, fiblink);
821
822
823
824
825
826
827 hw_fib = fib->hw_fib;
828 memset(fib, 0, sizeof(struct fib));
829 fib->type = FSAFS_NTC_FIB_CONTEXT;
830 fib->size = sizeof( struct fib );
831 fib->hw_fib = hw_fib;
832 fib->data = hw_fib->data;
833 fib->dev = dev;
834
835
836
837 aifcmd = (struct aac_aifcmd *) hw_fib->data;
838 if (aifcmd->command == cpu_to_le32(AifCmdDriverNotify)) {
839
840 *(u32 *)hw_fib->data = cpu_to_le32(ST_OK);
841 fib_adapter_complete(fib, sizeof(u32));
842 } else {
843 struct list_head *entry;
844
845
846
847 u32 time_now, time_last;
848 unsigned long flagv;
849
850 time_now = jiffies/HZ;
851
852 spin_lock_irqsave(&dev->fib_lock, flagv);
853 entry = dev->fib_list.next;
854
855
856
857
858
859
860 while (entry != &dev->fib_list) {
861
862
863
864 fibctx = list_entry(entry, struct aac_fib_context, next);
865
866
867
868
869 if (fibctx->count > 20)
870 {
871
872
873
874
875
876 time_last = fibctx->jiffies;
877
878
879
880
881
882 if ((time_now - time_last) > 120) {
883 entry = entry->next;
884 aac_close_fib_context(dev, fibctx);
885 continue;
886 }
887 }
888
889
890
891
892 hw_newfib = kmalloc(sizeof(struct hw_fib), GFP_ATOMIC);
893 newfib = kmalloc(sizeof(struct fib), GFP_ATOMIC);
894 if (newfib && hw_newfib) {
895
896
897
898 memcpy(hw_newfib, hw_fib, sizeof(struct hw_fib));
899 memcpy(newfib, fib, sizeof(struct fib));
900 newfib->hw_fib = hw_newfib;
901
902
903
904
905 list_add_tail(&newfib->fiblink, &fibctx->fib_list);
906 fibctx->count++;
907
908
909
910
911 up(&fibctx->wait_sem);
912 } else {
913 printk(KERN_WARNING "aifd: didn't allocate NewFib.\n");
914 if(newfib)
915 kfree(newfib);
916 if(hw_newfib)
917 kfree(hw_newfib);
918 }
919 entry = entry->next;
920 }
921
922
923
924 *(u32 *)hw_fib->data = cpu_to_le32(ST_OK);
925 fib_adapter_complete(fib, sizeof(u32));
926 spin_unlock_irqrestore(&dev->fib_lock, flagv);
927 }
928 spin_lock_irqsave(queues->queue[HostNormCmdQueue].lock, flags);
929 kfree(fib);
930 }
931
932
933
934 spin_unlock_irqrestore(queues->queue[HostNormCmdQueue].lock, flags);
935 schedule();
936
937 if(signal_pending(current))
938 break;
939 set_current_state(TASK_INTERRUPTIBLE);
940 }
941 remove_wait_queue(&queues->queue[HostNormCmdQueue].cmdready, &wait);
942 dev->aif_thread = 0;
943 complete_and_exit(&dev->aif_completion, 0);
944}
945