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#include <linux/kernel.h>
34#include <linux/init.h>
35#include <linux/types.h>
36#include <linux/sched.h>
37#include <linux/pci.h>
38#include <linux/spinlock.h>
39#include <linux/slab.h>
40#include <linux/completion.h>
41#include <linux/blkdev.h>
42#include <linux/delay.h>
43#include <linux/kthread.h>
44#include <linux/interrupt.h>
45#include <linux/semaphore.h>
46#include <scsi/scsi.h>
47#include <scsi/scsi_host.h>
48#include <scsi/scsi_device.h>
49#include <scsi/scsi_cmnd.h>
50
51#include "aacraid.h"
52
53
54
55
56
57
58
59
60
61static int fib_map_alloc(struct aac_dev *dev)
62{
63 dprintk((KERN_INFO
64 "allocate hardware fibs pci_alloc_consistent(%p, %d * (%d + %d), %p)\n",
65 dev->pdev, dev->max_fib_size, dev->scsi_host_ptr->can_queue,
66 AAC_NUM_MGT_FIB, &dev->hw_fib_pa));
67 dev->hw_fib_va = pci_alloc_consistent(dev->pdev,
68 (dev->max_fib_size + sizeof(struct aac_fib_xporthdr))
69 * (dev->scsi_host_ptr->can_queue + AAC_NUM_MGT_FIB) + (ALIGN32 - 1),
70 &dev->hw_fib_pa);
71 if (dev->hw_fib_va == NULL)
72 return -ENOMEM;
73 return 0;
74}
75
76
77
78
79
80
81
82
83
84void aac_fib_map_free(struct aac_dev *dev)
85{
86 pci_free_consistent(dev->pdev,
87 dev->max_fib_size * (dev->scsi_host_ptr->can_queue + AAC_NUM_MGT_FIB),
88 dev->hw_fib_va, dev->hw_fib_pa);
89 dev->hw_fib_va = NULL;
90 dev->hw_fib_pa = 0;
91}
92
93
94
95
96
97
98
99
100
101int aac_fib_setup(struct aac_dev * dev)
102{
103 struct fib *fibptr;
104 struct hw_fib *hw_fib;
105 dma_addr_t hw_fib_pa;
106 int i;
107
108 while (((i = fib_map_alloc(dev)) == -ENOMEM)
109 && (dev->scsi_host_ptr->can_queue > (64 - AAC_NUM_MGT_FIB))) {
110 dev->init->MaxIoCommands = cpu_to_le32((dev->scsi_host_ptr->can_queue + AAC_NUM_MGT_FIB) >> 1);
111 dev->scsi_host_ptr->can_queue = le32_to_cpu(dev->init->MaxIoCommands) - AAC_NUM_MGT_FIB;
112 }
113 if (i<0)
114 return -ENOMEM;
115
116
117 hw_fib_pa = (dev->hw_fib_pa + (ALIGN32 - 1)) & ~(ALIGN32 - 1);
118 dev->hw_fib_va = (struct hw_fib *)((unsigned char *)dev->hw_fib_va +
119 (hw_fib_pa - dev->hw_fib_pa));
120 dev->hw_fib_pa = hw_fib_pa;
121 memset(dev->hw_fib_va, 0,
122 (dev->max_fib_size + sizeof(struct aac_fib_xporthdr)) *
123 (dev->scsi_host_ptr->can_queue + AAC_NUM_MGT_FIB));
124
125
126 dev->hw_fib_va = (struct hw_fib *)((unsigned char *)dev->hw_fib_va +
127 sizeof(struct aac_fib_xporthdr));
128 dev->hw_fib_pa += sizeof(struct aac_fib_xporthdr);
129
130 hw_fib = dev->hw_fib_va;
131 hw_fib_pa = dev->hw_fib_pa;
132
133
134
135 for (i = 0, fibptr = &dev->fibs[i];
136 i < (dev->scsi_host_ptr->can_queue + AAC_NUM_MGT_FIB);
137 i++, fibptr++)
138 {
139 fibptr->dev = dev;
140 fibptr->hw_fib_va = hw_fib;
141 fibptr->data = (void *) fibptr->hw_fib_va->data;
142 fibptr->next = fibptr+1;
143 sema_init(&fibptr->event_wait, 0);
144 spin_lock_init(&fibptr->event_lock);
145 hw_fib->header.XferState = cpu_to_le32(0xffffffff);
146 hw_fib->header.SenderSize = cpu_to_le16(dev->max_fib_size);
147 fibptr->hw_fib_pa = hw_fib_pa;
148 hw_fib = (struct hw_fib *)((unsigned char *)hw_fib +
149 dev->max_fib_size + sizeof(struct aac_fib_xporthdr));
150 hw_fib_pa = hw_fib_pa +
151 dev->max_fib_size + sizeof(struct aac_fib_xporthdr);
152 }
153
154
155
156 dev->fibs[dev->scsi_host_ptr->can_queue + AAC_NUM_MGT_FIB - 1].next = NULL;
157
158
159
160 dev->free_fib = &dev->fibs[0];
161 return 0;
162}
163
164
165
166
167
168
169
170
171
172struct fib *aac_fib_alloc(struct aac_dev *dev)
173{
174 struct fib * fibptr;
175 unsigned long flags;
176 spin_lock_irqsave(&dev->fib_lock, flags);
177 fibptr = dev->free_fib;
178 if(!fibptr){
179 spin_unlock_irqrestore(&dev->fib_lock, flags);
180 return fibptr;
181 }
182 dev->free_fib = fibptr->next;
183 spin_unlock_irqrestore(&dev->fib_lock, flags);
184
185
186
187 fibptr->type = FSAFS_NTC_FIB_CONTEXT;
188 fibptr->size = sizeof(struct fib);
189
190
191
192
193 fibptr->hw_fib_va->header.XferState = 0;
194 fibptr->flags = 0;
195 fibptr->callback = NULL;
196 fibptr->callback_data = NULL;
197
198 return fibptr;
199}
200
201
202
203
204
205
206
207
208void aac_fib_free(struct fib *fibptr)
209{
210 unsigned long flags, flagsv;
211
212 spin_lock_irqsave(&fibptr->event_lock, flagsv);
213 if (fibptr->done == 2) {
214 spin_unlock_irqrestore(&fibptr->event_lock, flagsv);
215 return;
216 }
217 spin_unlock_irqrestore(&fibptr->event_lock, flagsv);
218
219 spin_lock_irqsave(&fibptr->dev->fib_lock, flags);
220 if (unlikely(fibptr->flags & FIB_CONTEXT_FLAG_TIMED_OUT))
221 aac_config.fib_timeouts++;
222 if (fibptr->hw_fib_va->header.XferState != 0) {
223 printk(KERN_WARNING "aac_fib_free, XferState != 0, fibptr = 0x%p, XferState = 0x%x\n",
224 (void*)fibptr,
225 le32_to_cpu(fibptr->hw_fib_va->header.XferState));
226 }
227 fibptr->next = fibptr->dev->free_fib;
228 fibptr->dev->free_fib = fibptr;
229 spin_unlock_irqrestore(&fibptr->dev->fib_lock, flags);
230}
231
232
233
234
235
236
237
238
239void aac_fib_init(struct fib *fibptr)
240{
241 struct hw_fib *hw_fib = fibptr->hw_fib_va;
242
243 hw_fib->header.StructType = FIB_MAGIC;
244 hw_fib->header.Size = cpu_to_le16(fibptr->dev->max_fib_size);
245 hw_fib->header.XferState = cpu_to_le32(HostOwned | FibInitialized | FibEmpty | FastResponseCapable);
246 hw_fib->header.SenderFibAddress = 0;
247 hw_fib->header.ReceiverFibAddress = cpu_to_le32(fibptr->hw_fib_pa);
248 hw_fib->header.SenderSize = cpu_to_le16(fibptr->dev->max_fib_size);
249}
250
251
252
253
254
255
256
257
258
259static void fib_dealloc(struct fib * fibptr)
260{
261 struct hw_fib *hw_fib = fibptr->hw_fib_va;
262 BUG_ON(hw_fib->header.StructType != FIB_MAGIC);
263 hw_fib->header.XferState = 0;
264}
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286static int aac_get_entry (struct aac_dev * dev, u32 qid, struct aac_entry **entry, u32 * index, unsigned long *nonotify)
287{
288 struct aac_queue * q;
289 unsigned long idx;
290
291
292
293
294
295
296
297
298 q = &dev->queues->queue[qid];
299
300 idx = *index = le32_to_cpu(*(q->headers.producer));
301
302 if (idx != le32_to_cpu(*(q->headers.consumer))) {
303 if (--idx == 0) {
304 if (qid == AdapNormCmdQueue)
305 idx = ADAP_NORM_CMD_ENTRIES;
306 else
307 idx = ADAP_NORM_RESP_ENTRIES;
308 }
309 if (idx != le32_to_cpu(*(q->headers.consumer)))
310 *nonotify = 1;
311 }
312
313 if (qid == AdapNormCmdQueue) {
314 if (*index >= ADAP_NORM_CMD_ENTRIES)
315 *index = 0;
316 } else {
317 if (*index >= ADAP_NORM_RESP_ENTRIES)
318 *index = 0;
319 }
320
321
322 if ((*index + 1) == le32_to_cpu(*(q->headers.consumer))) {
323 printk(KERN_WARNING "Queue %d full, %u outstanding.\n",
324 qid, q->numpending);
325 return 0;
326 } else {
327 *entry = q->base + *index;
328 return 1;
329 }
330}
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348int aac_queue_get(struct aac_dev * dev, u32 * index, u32 qid, struct hw_fib * hw_fib, int wait, struct fib * fibptr, unsigned long *nonotify)
349{
350 struct aac_entry * entry = NULL;
351 int map = 0;
352
353 if (qid == AdapNormCmdQueue) {
354
355 while (!aac_get_entry(dev, qid, &entry, index, nonotify)) {
356 printk(KERN_ERR "GetEntries failed\n");
357 }
358
359
360
361 entry->size = cpu_to_le32(le16_to_cpu(hw_fib->header.Size));
362 map = 1;
363 } else {
364 while (!aac_get_entry(dev, qid, &entry, index, nonotify)) {
365
366 }
367
368
369
370 entry->size = cpu_to_le32(le16_to_cpu(hw_fib->header.Size));
371 entry->addr = hw_fib->header.SenderFibAddress;
372
373 hw_fib->header.ReceiverFibAddress = hw_fib->header.SenderFibAddress;
374 map = 0;
375 }
376
377
378
379
380 if (map)
381 entry->addr = cpu_to_le32(fibptr->hw_fib_pa);
382 return 0;
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
410int aac_fib_send(u16 command, struct fib *fibptr, unsigned long size,
411 int priority, int wait, int reply, fib_callback callback,
412 void *callback_data)
413{
414 struct aac_dev * dev = fibptr->dev;
415 struct hw_fib * hw_fib = fibptr->hw_fib_va;
416 unsigned long flags = 0;
417 unsigned long qflags;
418 unsigned long mflags = 0;
419
420
421 if (!(hw_fib->header.XferState & cpu_to_le32(HostOwned)))
422 return -EBUSY;
423
424
425
426
427
428
429
430
431
432
433
434 fibptr->flags = 0;
435 if (wait && !reply) {
436 return -EINVAL;
437 } else if (!wait && reply) {
438 hw_fib->header.XferState |= cpu_to_le32(Async | ResponseExpected);
439 FIB_COUNTER_INCREMENT(aac_config.AsyncSent);
440 } else if (!wait && !reply) {
441 hw_fib->header.XferState |= cpu_to_le32(NoResponseExpected);
442 FIB_COUNTER_INCREMENT(aac_config.NoResponseSent);
443 } else if (wait && reply) {
444 hw_fib->header.XferState |= cpu_to_le32(ResponseExpected);
445 FIB_COUNTER_INCREMENT(aac_config.NormalSent);
446 }
447
448
449
450
451 hw_fib->header.SenderFibAddress = cpu_to_le32(((u32)(fibptr - dev->fibs)) << 2);
452 hw_fib->header.SenderData = (u32)(fibptr - dev->fibs);
453
454
455
456
457
458
459
460 hw_fib->header.Command = cpu_to_le16(command);
461 hw_fib->header.XferState |= cpu_to_le32(SentFromHost);
462 fibptr->hw_fib_va->header.Flags = 0;
463
464
465
466 hw_fib->header.Size = cpu_to_le16(sizeof(struct aac_fibhdr) + size);
467 if (le16_to_cpu(hw_fib->header.Size) > le16_to_cpu(hw_fib->header.SenderSize)) {
468 return -EMSGSIZE;
469 }
470
471
472
473
474 hw_fib->header.XferState |= cpu_to_le32(NormalPriority);
475
476
477
478
479
480 if (!wait) {
481 fibptr->callback = callback;
482 fibptr->callback_data = callback_data;
483 fibptr->flags = FIB_CONTEXT_FLAG;
484 }
485
486 fibptr->done = 0;
487
488 FIB_COUNTER_INCREMENT(aac_config.FibsSent);
489
490 dprintk((KERN_DEBUG "Fib contents:.\n"));
491 dprintk((KERN_DEBUG " Command = %d.\n", le32_to_cpu(hw_fib->header.Command)));
492 dprintk((KERN_DEBUG " SubCommand = %d.\n", le32_to_cpu(((struct aac_query_mount *)fib_data(fibptr))->command)));
493 dprintk((KERN_DEBUG " XferState = %x.\n", le32_to_cpu(hw_fib->header.XferState)));
494 dprintk((KERN_DEBUG " hw_fib va being sent=%p\n",fibptr->hw_fib_va));
495 dprintk((KERN_DEBUG " hw_fib pa being sent=%lx\n",(ulong)fibptr->hw_fib_pa));
496 dprintk((KERN_DEBUG " fib being sent=%p\n",fibptr));
497
498 if (!dev->queues)
499 return -EBUSY;
500
501 if (wait) {
502
503 spin_lock_irqsave(&dev->manage_lock, mflags);
504 if (dev->management_fib_count >= AAC_NUM_MGT_FIB) {
505 printk(KERN_INFO "No management Fibs Available:%d\n",
506 dev->management_fib_count);
507 spin_unlock_irqrestore(&dev->manage_lock, mflags);
508 return -EBUSY;
509 }
510 dev->management_fib_count++;
511 spin_unlock_irqrestore(&dev->manage_lock, mflags);
512 spin_lock_irqsave(&fibptr->event_lock, flags);
513 }
514
515 if (aac_adapter_deliver(fibptr) != 0) {
516 printk(KERN_ERR "aac_fib_send: returned -EBUSY\n");
517 if (wait) {
518 spin_unlock_irqrestore(&fibptr->event_lock, flags);
519 spin_lock_irqsave(&dev->manage_lock, mflags);
520 dev->management_fib_count--;
521 spin_unlock_irqrestore(&dev->manage_lock, mflags);
522 }
523 return -EBUSY;
524 }
525
526
527
528
529
530
531 if (wait) {
532 spin_unlock_irqrestore(&fibptr->event_lock, flags);
533
534 if (wait < 0) {
535
536
537
538
539
540
541 unsigned long count = 36000000L;
542 while (down_trylock(&fibptr->event_wait)) {
543 int blink;
544 if (--count == 0) {
545 struct aac_queue * q = &dev->queues->queue[AdapNormCmdQueue];
546 spin_lock_irqsave(q->lock, qflags);
547 q->numpending--;
548 spin_unlock_irqrestore(q->lock, qflags);
549 if (wait == -1) {
550 printk(KERN_ERR "aacraid: aac_fib_send: first asynchronous command timed out.\n"
551 "Usually a result of a PCI interrupt routing problem;\n"
552 "update mother board BIOS or consider utilizing one of\n"
553 "the SAFE mode kernel options (acpi, apic etc)\n");
554 }
555 return -ETIMEDOUT;
556 }
557 if ((blink = aac_adapter_check_health(dev)) > 0) {
558 if (wait == -1) {
559 printk(KERN_ERR "aacraid: aac_fib_send: adapter blinkLED 0x%x.\n"
560 "Usually a result of a serious unrecoverable hardware problem\n",
561 blink);
562 }
563 return -EFAULT;
564 }
565 udelay(5);
566 }
567 } else if (down_interruptible(&fibptr->event_wait)) {
568
569
570 }
571
572 spin_lock_irqsave(&fibptr->event_lock, flags);
573 if (fibptr->done == 0) {
574 fibptr->done = 2;
575 spin_unlock_irqrestore(&fibptr->event_lock, flags);
576 return -ERESTARTSYS;
577 }
578 spin_unlock_irqrestore(&fibptr->event_lock, flags);
579 BUG_ON(fibptr->done == 0);
580
581 if(unlikely(fibptr->flags & FIB_CONTEXT_FLAG_TIMED_OUT))
582 return -ETIMEDOUT;
583 return 0;
584 }
585
586
587
588
589 if (reply)
590 return -EINPROGRESS;
591 else
592 return 0;
593}
594
595
596
597
598
599
600
601
602
603
604
605
606int aac_consumer_get(struct aac_dev * dev, struct aac_queue * q, struct aac_entry **entry)
607{
608 u32 index;
609 int status;
610 if (le32_to_cpu(*q->headers.producer) == le32_to_cpu(*q->headers.consumer)) {
611 status = 0;
612 } else {
613
614
615
616
617
618 if (le32_to_cpu(*q->headers.consumer) >= q->entries)
619 index = 0;
620 else
621 index = le32_to_cpu(*q->headers.consumer);
622 *entry = q->base + index;
623 status = 1;
624 }
625 return(status);
626}
627
628
629
630
631
632
633
634
635
636
637
638void aac_consumer_free(struct aac_dev * dev, struct aac_queue *q, u32 qid)
639{
640 int wasfull = 0;
641 u32 notify;
642
643 if ((le32_to_cpu(*q->headers.producer)+1) == le32_to_cpu(*q->headers.consumer))
644 wasfull = 1;
645
646 if (le32_to_cpu(*q->headers.consumer) >= q->entries)
647 *q->headers.consumer = cpu_to_le32(1);
648 else
649 le32_add_cpu(q->headers.consumer, 1);
650
651 if (wasfull) {
652 switch (qid) {
653
654 case HostNormCmdQueue:
655 notify = HostNormCmdNotFull;
656 break;
657 case HostNormRespQueue:
658 notify = HostNormRespNotFull;
659 break;
660 default:
661 BUG();
662 return;
663 }
664 aac_adapter_notify(dev, notify);
665 }
666}
667
668
669
670
671
672
673
674
675
676
677int aac_fib_adapter_complete(struct fib *fibptr, unsigned short size)
678{
679 struct hw_fib * hw_fib = fibptr->hw_fib_va;
680 struct aac_dev * dev = fibptr->dev;
681 struct aac_queue * q;
682 unsigned long nointr = 0;
683 unsigned long qflags;
684
685 if (dev->comm_interface == AAC_COMM_MESSAGE_TYPE1) {
686 kfree(hw_fib);
687 return 0;
688 }
689
690 if (hw_fib->header.XferState == 0) {
691 if (dev->comm_interface == AAC_COMM_MESSAGE)
692 kfree(hw_fib);
693 return 0;
694 }
695
696
697
698 if (hw_fib->header.StructType != FIB_MAGIC) {
699 if (dev->comm_interface == AAC_COMM_MESSAGE)
700 kfree(hw_fib);
701 return -EINVAL;
702 }
703
704
705
706
707
708
709
710 if (hw_fib->header.XferState & cpu_to_le32(SentFromAdapter)) {
711 if (dev->comm_interface == AAC_COMM_MESSAGE) {
712 kfree (hw_fib);
713 } else {
714 u32 index;
715 hw_fib->header.XferState |= cpu_to_le32(HostProcessed);
716 if (size) {
717 size += sizeof(struct aac_fibhdr);
718 if (size > le16_to_cpu(hw_fib->header.SenderSize))
719 return -EMSGSIZE;
720 hw_fib->header.Size = cpu_to_le16(size);
721 }
722 q = &dev->queues->queue[AdapNormRespQueue];
723 spin_lock_irqsave(q->lock, qflags);
724 aac_queue_get(dev, &index, AdapNormRespQueue, hw_fib, 1, NULL, &nointr);
725 *(q->headers.producer) = cpu_to_le32(index + 1);
726 spin_unlock_irqrestore(q->lock, qflags);
727 if (!(nointr & (int)aac_config.irq_mod))
728 aac_adapter_notify(dev, AdapNormRespQueue);
729 }
730 } else {
731 printk(KERN_WARNING "aac_fib_adapter_complete: "
732 "Unknown xferstate detected.\n");
733 BUG();
734 }
735 return 0;
736}
737
738
739
740
741
742
743
744
745int aac_fib_complete(struct fib *fibptr)
746{
747 unsigned long flags;
748 struct hw_fib * hw_fib = fibptr->hw_fib_va;
749
750
751
752
753
754 if (hw_fib->header.XferState == 0)
755 return 0;
756
757
758
759
760 if (hw_fib->header.StructType != FIB_MAGIC)
761 return -EINVAL;
762
763
764
765
766
767
768 spin_lock_irqsave(&fibptr->event_lock, flags);
769 if (fibptr->done == 2) {
770 spin_unlock_irqrestore(&fibptr->event_lock, flags);
771 return 0;
772 }
773 spin_unlock_irqrestore(&fibptr->event_lock, flags);
774
775 if((hw_fib->header.XferState & cpu_to_le32(SentFromHost)) &&
776 (hw_fib->header.XferState & cpu_to_le32(AdapterProcessed)))
777 {
778 fib_dealloc(fibptr);
779 }
780 else if(hw_fib->header.XferState & cpu_to_le32(SentFromHost))
781 {
782
783
784
785
786 fib_dealloc(fibptr);
787 } else if(hw_fib->header.XferState & cpu_to_le32(HostOwned)) {
788 fib_dealloc(fibptr);
789 } else {
790 BUG();
791 }
792 return 0;
793}
794
795
796
797
798
799
800
801
802
803
804void aac_printf(struct aac_dev *dev, u32 val)
805{
806 char *cp = dev->printfbuf;
807 if (dev->printf_enabled)
808 {
809 int length = val & 0xffff;
810 int level = (val >> 16) & 0xffff;
811
812
813
814
815
816 if (length > 255)
817 length = 255;
818 if (cp[length] != 0)
819 cp[length] = 0;
820 if (level == LOG_AAC_HIGH_ERROR)
821 printk(KERN_WARNING "%s:%s", dev->name, cp);
822 else
823 printk(KERN_INFO "%s:%s", dev->name, cp);
824 }
825 memset(cp, 0, 256);
826}
827
828
829
830
831
832
833
834
835
836
837
838#define AIF_SNIFF_TIMEOUT (30*HZ)
839static void aac_handle_aif(struct aac_dev * dev, struct fib * fibptr)
840{
841 struct hw_fib * hw_fib = fibptr->hw_fib_va;
842 struct aac_aifcmd * aifcmd = (struct aac_aifcmd *)hw_fib->data;
843 u32 channel, id, lun, container;
844 struct scsi_device *device;
845 enum {
846 NOTHING,
847 DELETE,
848 ADD,
849 CHANGE
850 } device_config_needed = NOTHING;
851
852
853
854 if (!dev || !dev->fsa_dev)
855 return;
856 container = channel = id = lun = (u32)-1;
857
858
859
860
861
862
863
864 switch (le32_to_cpu(aifcmd->command)) {
865 case AifCmdDriverNotify:
866 switch (le32_to_cpu(((__le32 *)aifcmd->data)[0])) {
867
868
869
870 case AifDenMorphComplete:
871 case AifDenVolumeExtendComplete:
872 container = le32_to_cpu(((__le32 *)aifcmd->data)[1]);
873 if (container >= dev->maximum_num_containers)
874 break;
875
876
877
878
879
880
881
882
883 if ((dev != NULL) && (dev->scsi_host_ptr != NULL)) {
884 device = scsi_device_lookup(dev->scsi_host_ptr,
885 CONTAINER_TO_CHANNEL(container),
886 CONTAINER_TO_ID(container),
887 CONTAINER_TO_LUN(container));
888 if (device) {
889 dev->fsa_dev[container].config_needed = CHANGE;
890 dev->fsa_dev[container].config_waiting_on = AifEnConfigChange;
891 dev->fsa_dev[container].config_waiting_stamp = jiffies;
892 scsi_device_put(device);
893 }
894 }
895 }
896
897
898
899
900
901 if (container != (u32)-1) {
902 if (container >= dev->maximum_num_containers)
903 break;
904 if ((dev->fsa_dev[container].config_waiting_on ==
905 le32_to_cpu(*(__le32 *)aifcmd->data)) &&
906 time_before(jiffies, dev->fsa_dev[container].config_waiting_stamp + AIF_SNIFF_TIMEOUT))
907 dev->fsa_dev[container].config_waiting_on = 0;
908 } else for (container = 0;
909 container < dev->maximum_num_containers; ++container) {
910 if ((dev->fsa_dev[container].config_waiting_on ==
911 le32_to_cpu(*(__le32 *)aifcmd->data)) &&
912 time_before(jiffies, dev->fsa_dev[container].config_waiting_stamp + AIF_SNIFF_TIMEOUT))
913 dev->fsa_dev[container].config_waiting_on = 0;
914 }
915 break;
916
917 case AifCmdEventNotify:
918 switch (le32_to_cpu(((__le32 *)aifcmd->data)[0])) {
919 case AifEnBatteryEvent:
920 dev->cache_protected =
921 (((__le32 *)aifcmd->data)[1] == cpu_to_le32(3));
922 break;
923
924
925
926 case AifEnAddContainer:
927 container = le32_to_cpu(((__le32 *)aifcmd->data)[1]);
928 if (container >= dev->maximum_num_containers)
929 break;
930 dev->fsa_dev[container].config_needed = ADD;
931 dev->fsa_dev[container].config_waiting_on =
932 AifEnConfigChange;
933 dev->fsa_dev[container].config_waiting_stamp = jiffies;
934 break;
935
936
937
938
939 case AifEnDeleteContainer:
940 container = le32_to_cpu(((__le32 *)aifcmd->data)[1]);
941 if (container >= dev->maximum_num_containers)
942 break;
943 dev->fsa_dev[container].config_needed = DELETE;
944 dev->fsa_dev[container].config_waiting_on =
945 AifEnConfigChange;
946 dev->fsa_dev[container].config_waiting_stamp = jiffies;
947 break;
948
949
950
951
952
953 case AifEnContainerChange:
954 container = le32_to_cpu(((__le32 *)aifcmd->data)[1]);
955 if (container >= dev->maximum_num_containers)
956 break;
957 if (dev->fsa_dev[container].config_waiting_on &&
958 time_before(jiffies, dev->fsa_dev[container].config_waiting_stamp + AIF_SNIFF_TIMEOUT))
959 break;
960 dev->fsa_dev[container].config_needed = CHANGE;
961 dev->fsa_dev[container].config_waiting_on =
962 AifEnConfigChange;
963 dev->fsa_dev[container].config_waiting_stamp = jiffies;
964 break;
965
966 case AifEnConfigChange:
967 break;
968
969 case AifEnAddJBOD:
970 case AifEnDeleteJBOD:
971 container = le32_to_cpu(((__le32 *)aifcmd->data)[1]);
972 if ((container >> 28)) {
973 container = (u32)-1;
974 break;
975 }
976 channel = (container >> 24) & 0xF;
977 if (channel >= dev->maximum_num_channels) {
978 container = (u32)-1;
979 break;
980 }
981 id = container & 0xFFFF;
982 if (id >= dev->maximum_num_physicals) {
983 container = (u32)-1;
984 break;
985 }
986 lun = (container >> 16) & 0xFF;
987 container = (u32)-1;
988 channel = aac_phys_to_logical(channel);
989 device_config_needed =
990 (((__le32 *)aifcmd->data)[0] ==
991 cpu_to_le32(AifEnAddJBOD)) ? ADD : DELETE;
992 if (device_config_needed == ADD) {
993 device = scsi_device_lookup(dev->scsi_host_ptr,
994 channel,
995 id,
996 lun);
997 if (device) {
998 scsi_remove_device(device);
999 scsi_device_put(device);
1000 }
1001 }
1002 break;
1003
1004 case AifEnEnclosureManagement:
1005
1006
1007
1008
1009 if (dev->jbod)
1010 break;
1011 switch (le32_to_cpu(((__le32 *)aifcmd->data)[3])) {
1012 case EM_DRIVE_INSERTION:
1013 case EM_DRIVE_REMOVAL:
1014 container = le32_to_cpu(
1015 ((__le32 *)aifcmd->data)[2]);
1016 if ((container >> 28)) {
1017 container = (u32)-1;
1018 break;
1019 }
1020 channel = (container >> 24) & 0xF;
1021 if (channel >= dev->maximum_num_channels) {
1022 container = (u32)-1;
1023 break;
1024 }
1025 id = container & 0xFFFF;
1026 lun = (container >> 16) & 0xFF;
1027 container = (u32)-1;
1028 if (id >= dev->maximum_num_physicals) {
1029
1030 if ((0x2000 <= id) || lun || channel ||
1031 ((channel = (id >> 7) & 0x3F) >=
1032 dev->maximum_num_channels))
1033 break;
1034 lun = (id >> 4) & 7;
1035 id &= 0xF;
1036 }
1037 channel = aac_phys_to_logical(channel);
1038 device_config_needed =
1039 (((__le32 *)aifcmd->data)[3]
1040 == cpu_to_le32(EM_DRIVE_INSERTION)) ?
1041 ADD : DELETE;
1042 break;
1043 }
1044 break;
1045 }
1046
1047
1048
1049
1050
1051 if (container != (u32)-1) {
1052 if (container >= dev->maximum_num_containers)
1053 break;
1054 if ((dev->fsa_dev[container].config_waiting_on ==
1055 le32_to_cpu(*(__le32 *)aifcmd->data)) &&
1056 time_before(jiffies, dev->fsa_dev[container].config_waiting_stamp + AIF_SNIFF_TIMEOUT))
1057 dev->fsa_dev[container].config_waiting_on = 0;
1058 } else for (container = 0;
1059 container < dev->maximum_num_containers; ++container) {
1060 if ((dev->fsa_dev[container].config_waiting_on ==
1061 le32_to_cpu(*(__le32 *)aifcmd->data)) &&
1062 time_before(jiffies, dev->fsa_dev[container].config_waiting_stamp + AIF_SNIFF_TIMEOUT))
1063 dev->fsa_dev[container].config_waiting_on = 0;
1064 }
1065 break;
1066
1067 case AifCmdJobProgress:
1068
1069
1070
1071
1072
1073
1074
1075
1076 if (((__le32 *)aifcmd->data)[1] == cpu_to_le32(AifJobCtrZero) &&
1077 (((__le32 *)aifcmd->data)[6] == ((__le32 *)aifcmd->data)[5] ||
1078 ((__le32 *)aifcmd->data)[4] == cpu_to_le32(AifJobStsSuccess))) {
1079 for (container = 0;
1080 container < dev->maximum_num_containers;
1081 ++container) {
1082
1083
1084
1085
1086 dev->fsa_dev[container].config_waiting_on =
1087 AifEnContainerChange;
1088 dev->fsa_dev[container].config_needed = ADD;
1089 dev->fsa_dev[container].config_waiting_stamp =
1090 jiffies;
1091 }
1092 }
1093 if (((__le32 *)aifcmd->data)[1] == cpu_to_le32(AifJobCtrZero) &&
1094 ((__le32 *)aifcmd->data)[6] == 0 &&
1095 ((__le32 *)aifcmd->data)[4] == cpu_to_le32(AifJobStsRunning)) {
1096 for (container = 0;
1097 container < dev->maximum_num_containers;
1098 ++container) {
1099
1100
1101
1102
1103 dev->fsa_dev[container].config_waiting_on =
1104 AifEnContainerChange;
1105 dev->fsa_dev[container].config_needed = DELETE;
1106 dev->fsa_dev[container].config_waiting_stamp =
1107 jiffies;
1108 }
1109 }
1110 break;
1111 }
1112
1113 container = 0;
1114retry_next:
1115 if (device_config_needed == NOTHING)
1116 for (; container < dev->maximum_num_containers; ++container) {
1117 if ((dev->fsa_dev[container].config_waiting_on == 0) &&
1118 (dev->fsa_dev[container].config_needed != NOTHING) &&
1119 time_before(jiffies, dev->fsa_dev[container].config_waiting_stamp + AIF_SNIFF_TIMEOUT)) {
1120 device_config_needed =
1121 dev->fsa_dev[container].config_needed;
1122 dev->fsa_dev[container].config_needed = NOTHING;
1123 channel = CONTAINER_TO_CHANNEL(container);
1124 id = CONTAINER_TO_ID(container);
1125 lun = CONTAINER_TO_LUN(container);
1126 break;
1127 }
1128 }
1129 if (device_config_needed == NOTHING)
1130 return;
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144 if (!dev || !dev->scsi_host_ptr)
1145 return;
1146
1147
1148
1149 if ((channel == CONTAINER_CHANNEL) &&
1150 (device_config_needed != NOTHING)) {
1151 if (dev->fsa_dev[container].valid == 1)
1152 dev->fsa_dev[container].valid = 2;
1153 aac_probe_container(dev, container);
1154 }
1155 device = scsi_device_lookup(dev->scsi_host_ptr, channel, id, lun);
1156 if (device) {
1157 switch (device_config_needed) {
1158 case DELETE:
1159#if (defined(AAC_DEBUG_INSTRUMENT_AIF_DELETE))
1160 scsi_remove_device(device);
1161#else
1162 if (scsi_device_online(device)) {
1163 scsi_device_set_state(device, SDEV_OFFLINE);
1164 sdev_printk(KERN_INFO, device,
1165 "Device offlined - %s\n",
1166 (channel == CONTAINER_CHANNEL) ?
1167 "array deleted" :
1168 "enclosure services event");
1169 }
1170#endif
1171 break;
1172 case ADD:
1173 if (!scsi_device_online(device)) {
1174 sdev_printk(KERN_INFO, device,
1175 "Device online - %s\n",
1176 (channel == CONTAINER_CHANNEL) ?
1177 "array created" :
1178 "enclosure services event");
1179 scsi_device_set_state(device, SDEV_RUNNING);
1180 }
1181
1182 case CHANGE:
1183 if ((channel == CONTAINER_CHANNEL)
1184 && (!dev->fsa_dev[container].valid)) {
1185#if (defined(AAC_DEBUG_INSTRUMENT_AIF_DELETE))
1186 scsi_remove_device(device);
1187#else
1188 if (!scsi_device_online(device))
1189 break;
1190 scsi_device_set_state(device, SDEV_OFFLINE);
1191 sdev_printk(KERN_INFO, device,
1192 "Device offlined - %s\n",
1193 "array failed");
1194#endif
1195 break;
1196 }
1197 scsi_rescan_device(&device->sdev_gendev);
1198
1199 default:
1200 break;
1201 }
1202 scsi_device_put(device);
1203 device_config_needed = NOTHING;
1204 }
1205 if (device_config_needed == ADD)
1206 scsi_add_device(dev->scsi_host_ptr, channel, id, lun);
1207 if (channel == CONTAINER_CHANNEL) {
1208 container++;
1209 device_config_needed = NOTHING;
1210 goto retry_next;
1211 }
1212}
1213
1214static int _aac_reset_adapter(struct aac_dev *aac, int forced)
1215{
1216 int index, quirks;
1217 int retval;
1218 struct Scsi_Host *host;
1219 struct scsi_device *dev;
1220 struct scsi_cmnd *command;
1221 struct scsi_cmnd *command_list;
1222 int jafo = 0;
1223
1224
1225
1226
1227
1228
1229
1230
1231
1232
1233
1234 host = aac->scsi_host_ptr;
1235 scsi_block_requests(host);
1236 aac_adapter_disable_int(aac);
1237 if (aac->thread->pid != current->pid) {
1238 spin_unlock_irq(host->host_lock);
1239 kthread_stop(aac->thread);
1240 jafo = 1;
1241 }
1242
1243
1244
1245
1246
1247 retval = aac_adapter_restart(aac, forced ? 0 : aac_adapter_check_health(aac));
1248
1249 if (retval)
1250 goto out;
1251
1252
1253
1254
1255 for (retval = 1, index = 0; index < (aac->scsi_host_ptr->can_queue + AAC_NUM_MGT_FIB); index++) {
1256 struct fib *fib = &aac->fibs[index];
1257 if (!(fib->hw_fib_va->header.XferState & cpu_to_le32(NoResponseExpected | Async)) &&
1258 (fib->hw_fib_va->header.XferState & cpu_to_le32(ResponseExpected))) {
1259 unsigned long flagv;
1260 spin_lock_irqsave(&fib->event_lock, flagv);
1261 up(&fib->event_wait);
1262 spin_unlock_irqrestore(&fib->event_lock, flagv);
1263 schedule();
1264 retval = 0;
1265 }
1266 }
1267
1268 if (retval == 0)
1269 ssleep(2);
1270 index = aac->cardtype;
1271
1272
1273
1274
1275
1276
1277
1278
1279 aac_fib_map_free(aac);
1280 pci_free_consistent(aac->pdev, aac->comm_size, aac->comm_addr, aac->comm_phys);
1281 aac->comm_addr = NULL;
1282 aac->comm_phys = 0;
1283 kfree(aac->queues);
1284 aac->queues = NULL;
1285 free_irq(aac->pdev->irq, aac);
1286 if (aac->msi)
1287 pci_disable_msi(aac->pdev);
1288 kfree(aac->fsa_dev);
1289 aac->fsa_dev = NULL;
1290 quirks = aac_get_driver_ident(index)->quirks;
1291 if (quirks & AAC_QUIRK_31BIT) {
1292 if (((retval = pci_set_dma_mask(aac->pdev, DMA_BIT_MASK(31)))) ||
1293 ((retval = pci_set_consistent_dma_mask(aac->pdev, DMA_BIT_MASK(31)))))
1294 goto out;
1295 } else {
1296 if (((retval = pci_set_dma_mask(aac->pdev, DMA_BIT_MASK(32)))) ||
1297 ((retval = pci_set_consistent_dma_mask(aac->pdev, DMA_BIT_MASK(32)))))
1298 goto out;
1299 }
1300 if ((retval = (*(aac_get_driver_ident(index)->init))(aac)))
1301 goto out;
1302 if (quirks & AAC_QUIRK_31BIT)
1303 if ((retval = pci_set_dma_mask(aac->pdev, DMA_BIT_MASK(32))))
1304 goto out;
1305 if (jafo) {
1306 aac->thread = kthread_run(aac_command_thread, aac, aac->name);
1307 if (IS_ERR(aac->thread)) {
1308 retval = PTR_ERR(aac->thread);
1309 goto out;
1310 }
1311 }
1312 (void)aac_get_adapter_info(aac);
1313 if ((quirks & AAC_QUIRK_34SG) && (host->sg_tablesize > 34)) {
1314 host->sg_tablesize = 34;
1315 host->max_sectors = (host->sg_tablesize * 8) + 112;
1316 }
1317 if ((quirks & AAC_QUIRK_17SG) && (host->sg_tablesize > 17)) {
1318 host->sg_tablesize = 17;
1319 host->max_sectors = (host->sg_tablesize * 8) + 112;
1320 }
1321 aac_get_config_status(aac, 1);
1322 aac_get_containers(aac);
1323
1324
1325
1326
1327 command_list = NULL;
1328 __shost_for_each_device(dev, host) {
1329 unsigned long flags;
1330 spin_lock_irqsave(&dev->list_lock, flags);
1331 list_for_each_entry(command, &dev->cmd_list, list)
1332 if (command->SCp.phase == AAC_OWNER_FIRMWARE) {
1333 command->SCp.buffer = (struct scatterlist *)command_list;
1334 command_list = command;
1335 }
1336 spin_unlock_irqrestore(&dev->list_lock, flags);
1337 }
1338 while ((command = command_list)) {
1339 command_list = (struct scsi_cmnd *)command->SCp.buffer;
1340 command->SCp.buffer = NULL;
1341 command->result = DID_OK << 16
1342 | COMMAND_COMPLETE << 8
1343 | SAM_STAT_TASK_SET_FULL;
1344 command->SCp.phase = AAC_OWNER_ERROR_HANDLER;
1345 command->scsi_done(command);
1346 }
1347 retval = 0;
1348
1349out:
1350 aac->in_reset = 0;
1351 scsi_unblock_requests(host);
1352 if (jafo) {
1353 spin_lock_irq(host->host_lock);
1354 }
1355 return retval;
1356}
1357
1358int aac_reset_adapter(struct aac_dev * aac, int forced)
1359{
1360 unsigned long flagv = 0;
1361 int retval;
1362 struct Scsi_Host * host;
1363
1364 if (spin_trylock_irqsave(&aac->fib_lock, flagv) == 0)
1365 return -EBUSY;
1366
1367 if (aac->in_reset) {
1368 spin_unlock_irqrestore(&aac->fib_lock, flagv);
1369 return -EBUSY;
1370 }
1371 aac->in_reset = 1;
1372 spin_unlock_irqrestore(&aac->fib_lock, flagv);
1373
1374
1375
1376
1377
1378
1379 host = aac->scsi_host_ptr;
1380 scsi_block_requests(host);
1381 if (forced < 2) for (retval = 60; retval; --retval) {
1382 struct scsi_device * dev;
1383 struct scsi_cmnd * command;
1384 int active = 0;
1385
1386 __shost_for_each_device(dev, host) {
1387 spin_lock_irqsave(&dev->list_lock, flagv);
1388 list_for_each_entry(command, &dev->cmd_list, list) {
1389 if (command->SCp.phase == AAC_OWNER_FIRMWARE) {
1390 active++;
1391 break;
1392 }
1393 }
1394 spin_unlock_irqrestore(&dev->list_lock, flagv);
1395 if (active)
1396 break;
1397
1398 }
1399
1400
1401
1402 if (active == 0)
1403 break;
1404 ssleep(1);
1405 }
1406
1407
1408 if (forced < 2)
1409 aac_send_shutdown(aac);
1410 spin_lock_irqsave(host->host_lock, flagv);
1411 retval = _aac_reset_adapter(aac, forced ? forced : ((aac_check_reset != 0) && (aac_check_reset != 1)));
1412 spin_unlock_irqrestore(host->host_lock, flagv);
1413
1414 if ((forced < 2) && (retval == -ENODEV)) {
1415
1416 struct fib * fibctx = aac_fib_alloc(aac);
1417 if (fibctx) {
1418 struct aac_pause *cmd;
1419 int status;
1420
1421 aac_fib_init(fibctx);
1422
1423 cmd = (struct aac_pause *) fib_data(fibctx);
1424
1425 cmd->command = cpu_to_le32(VM_ContainerConfig);
1426 cmd->type = cpu_to_le32(CT_PAUSE_IO);
1427 cmd->timeout = cpu_to_le32(1);
1428 cmd->min = cpu_to_le32(1);
1429 cmd->noRescan = cpu_to_le32(1);
1430 cmd->count = cpu_to_le32(0);
1431
1432 status = aac_fib_send(ContainerCommand,
1433 fibctx,
1434 sizeof(struct aac_pause),
1435 FsaNormal,
1436 -2 , 1,
1437 NULL, NULL);
1438
1439 if (status >= 0)
1440 aac_fib_complete(fibctx);
1441
1442
1443 if (status != -ERESTARTSYS)
1444 aac_fib_free(fibctx);
1445 }
1446 }
1447
1448 return retval;
1449}
1450
1451int aac_check_health(struct aac_dev * aac)
1452{
1453 int BlinkLED;
1454 unsigned long time_now, flagv = 0;
1455 struct list_head * entry;
1456 struct Scsi_Host * host;
1457
1458
1459 if (spin_trylock_irqsave(&aac->fib_lock, flagv) == 0)
1460 return 0;
1461
1462 if (aac->in_reset || !(BlinkLED = aac_adapter_check_health(aac))) {
1463 spin_unlock_irqrestore(&aac->fib_lock, flagv);
1464 return 0;
1465 }
1466
1467 aac->in_reset = 1;
1468
1469
1470
1471
1472
1473
1474
1475
1476
1477
1478 time_now = jiffies/HZ;
1479 entry = aac->fib_list.next;
1480
1481
1482
1483
1484
1485
1486
1487 while (entry != &aac->fib_list) {
1488
1489
1490
1491 struct aac_fib_context *fibctx = list_entry(entry, struct aac_fib_context, next);
1492 struct hw_fib * hw_fib;
1493 struct fib * fib;
1494
1495
1496
1497
1498 if (fibctx->count > 20) {
1499
1500
1501
1502
1503
1504 u32 time_last = fibctx->jiffies;
1505
1506
1507
1508
1509
1510 if ((time_now - time_last) > aif_timeout) {
1511 entry = entry->next;
1512 aac_close_fib_context(aac, fibctx);
1513 continue;
1514 }
1515 }
1516
1517
1518
1519
1520 hw_fib = kzalloc(sizeof(struct hw_fib), GFP_ATOMIC);
1521 fib = kzalloc(sizeof(struct fib), GFP_ATOMIC);
1522 if (fib && hw_fib) {
1523 struct aac_aifcmd * aif;
1524
1525 fib->hw_fib_va = hw_fib;
1526 fib->dev = aac;
1527 aac_fib_init(fib);
1528 fib->type = FSAFS_NTC_FIB_CONTEXT;
1529 fib->size = sizeof (struct fib);
1530 fib->data = hw_fib->data;
1531 aif = (struct aac_aifcmd *)hw_fib->data;
1532 aif->command = cpu_to_le32(AifCmdEventNotify);
1533 aif->seqnum = cpu_to_le32(0xFFFFFFFF);
1534 ((__le32 *)aif->data)[0] = cpu_to_le32(AifEnExpEvent);
1535 ((__le32 *)aif->data)[1] = cpu_to_le32(AifExeFirmwarePanic);
1536 ((__le32 *)aif->data)[2] = cpu_to_le32(AifHighPriority);
1537 ((__le32 *)aif->data)[3] = cpu_to_le32(BlinkLED);
1538
1539
1540
1541
1542
1543 list_add_tail(&fib->fiblink, &fibctx->fib_list);
1544 fibctx->count++;
1545
1546
1547
1548
1549 up(&fibctx->wait_sem);
1550 } else {
1551 printk(KERN_WARNING "aifd: didn't allocate NewFib.\n");
1552 kfree(fib);
1553 kfree(hw_fib);
1554 }
1555 entry = entry->next;
1556 }
1557
1558 spin_unlock_irqrestore(&aac->fib_lock, flagv);
1559
1560 if (BlinkLED < 0) {
1561 printk(KERN_ERR "%s: Host adapter dead %d\n", aac->name, BlinkLED);
1562 goto out;
1563 }
1564
1565 printk(KERN_ERR "%s: Host adapter BLINK LED 0x%x\n", aac->name, BlinkLED);
1566
1567 if (!aac_check_reset || ((aac_check_reset == 1) &&
1568 (aac->supplement_adapter_info.SupportedOptions2 &
1569 AAC_OPTION_IGNORE_RESET)))
1570 goto out;
1571 host = aac->scsi_host_ptr;
1572 if (aac->thread->pid != current->pid)
1573 spin_lock_irqsave(host->host_lock, flagv);
1574 BlinkLED = _aac_reset_adapter(aac, aac_check_reset != 1);
1575 if (aac->thread->pid != current->pid)
1576 spin_unlock_irqrestore(host->host_lock, flagv);
1577 return BlinkLED;
1578
1579out:
1580 aac->in_reset = 0;
1581 return BlinkLED;
1582}
1583
1584
1585
1586
1587
1588
1589
1590
1591
1592
1593
1594
1595int aac_command_thread(void *data)
1596{
1597 struct aac_dev *dev = data;
1598 struct hw_fib *hw_fib, *hw_newfib;
1599 struct fib *fib, *newfib;
1600 struct aac_fib_context *fibctx;
1601 unsigned long flags;
1602 DECLARE_WAITQUEUE(wait, current);
1603 unsigned long next_jiffies = jiffies + HZ;
1604 unsigned long next_check_jiffies = next_jiffies;
1605 long difference = HZ;
1606
1607
1608
1609
1610 if (dev->aif_thread)
1611 return -EINVAL;
1612
1613
1614
1615
1616 dev->aif_thread = 1;
1617 add_wait_queue(&dev->queues->queue[HostNormCmdQueue].cmdready, &wait);
1618 set_current_state(TASK_INTERRUPTIBLE);
1619 dprintk ((KERN_INFO "aac_command_thread start\n"));
1620 while (1) {
1621 spin_lock_irqsave(dev->queues->queue[HostNormCmdQueue].lock, flags);
1622 while(!list_empty(&(dev->queues->queue[HostNormCmdQueue].cmdq))) {
1623 struct list_head *entry;
1624 struct aac_aifcmd * aifcmd;
1625
1626 set_current_state(TASK_RUNNING);
1627
1628 entry = dev->queues->queue[HostNormCmdQueue].cmdq.next;
1629 list_del(entry);
1630
1631 spin_unlock_irqrestore(dev->queues->queue[HostNormCmdQueue].lock, flags);
1632 fib = list_entry(entry, struct fib, fiblink);
1633
1634
1635
1636
1637
1638
1639 hw_fib = fib->hw_fib_va;
1640 memset(fib, 0, sizeof(struct fib));
1641 fib->type = FSAFS_NTC_FIB_CONTEXT;
1642 fib->size = sizeof(struct fib);
1643 fib->hw_fib_va = hw_fib;
1644 fib->data = hw_fib->data;
1645 fib->dev = dev;
1646
1647
1648
1649 aifcmd = (struct aac_aifcmd *) hw_fib->data;
1650 if (aifcmd->command == cpu_to_le32(AifCmdDriverNotify)) {
1651
1652 aac_handle_aif(dev, fib);
1653 *(__le32 *)hw_fib->data = cpu_to_le32(ST_OK);
1654 aac_fib_adapter_complete(fib, (u16)sizeof(u32));
1655 } else {
1656
1657
1658
1659 u32 time_now, time_last;
1660 unsigned long flagv;
1661 unsigned num;
1662 struct hw_fib ** hw_fib_pool, ** hw_fib_p;
1663 struct fib ** fib_pool, ** fib_p;
1664
1665
1666 if ((aifcmd->command ==
1667 cpu_to_le32(AifCmdEventNotify)) ||
1668 (aifcmd->command ==
1669 cpu_to_le32(AifCmdJobProgress))) {
1670 aac_handle_aif(dev, fib);
1671 }
1672
1673 time_now = jiffies/HZ;
1674
1675
1676
1677
1678
1679
1680
1681 num = le32_to_cpu(dev->init->AdapterFibsSize)
1682 / sizeof(struct hw_fib);
1683 spin_lock_irqsave(&dev->fib_lock, flagv);
1684 entry = dev->fib_list.next;
1685 while (entry != &dev->fib_list) {
1686 entry = entry->next;
1687 ++num;
1688 }
1689 spin_unlock_irqrestore(&dev->fib_lock, flagv);
1690 hw_fib_pool = NULL;
1691 fib_pool = NULL;
1692 if (num
1693 && ((hw_fib_pool = kmalloc(sizeof(struct hw_fib *) * num, GFP_KERNEL)))
1694 && ((fib_pool = kmalloc(sizeof(struct fib *) * num, GFP_KERNEL)))) {
1695 hw_fib_p = hw_fib_pool;
1696 fib_p = fib_pool;
1697 while (hw_fib_p < &hw_fib_pool[num]) {
1698 if (!(*(hw_fib_p++) = kmalloc(sizeof(struct hw_fib), GFP_KERNEL))) {
1699 --hw_fib_p;
1700 break;
1701 }
1702 if (!(*(fib_p++) = kmalloc(sizeof(struct fib), GFP_KERNEL))) {
1703 kfree(*(--hw_fib_p));
1704 break;
1705 }
1706 }
1707 if ((num = hw_fib_p - hw_fib_pool) == 0) {
1708 kfree(fib_pool);
1709 fib_pool = NULL;
1710 kfree(hw_fib_pool);
1711 hw_fib_pool = NULL;
1712 }
1713 } else {
1714 kfree(hw_fib_pool);
1715 hw_fib_pool = NULL;
1716 }
1717 spin_lock_irqsave(&dev->fib_lock, flagv);
1718 entry = dev->fib_list.next;
1719
1720
1721
1722
1723
1724
1725 hw_fib_p = hw_fib_pool;
1726 fib_p = fib_pool;
1727 while (entry != &dev->fib_list) {
1728
1729
1730
1731 fibctx = list_entry(entry, struct aac_fib_context, next);
1732
1733
1734
1735
1736 if (fibctx->count > 20)
1737 {
1738
1739
1740
1741
1742
1743 time_last = fibctx->jiffies;
1744
1745
1746
1747
1748
1749 if ((time_now - time_last) > aif_timeout) {
1750 entry = entry->next;
1751 aac_close_fib_context(dev, fibctx);
1752 continue;
1753 }
1754 }
1755
1756
1757
1758
1759 if (hw_fib_p < &hw_fib_pool[num]) {
1760 hw_newfib = *hw_fib_p;
1761 *(hw_fib_p++) = NULL;
1762 newfib = *fib_p;
1763 *(fib_p++) = NULL;
1764
1765
1766
1767 memcpy(hw_newfib, hw_fib, sizeof(struct hw_fib));
1768 memcpy(newfib, fib, sizeof(struct fib));
1769 newfib->hw_fib_va = hw_newfib;
1770
1771
1772
1773
1774 list_add_tail(&newfib->fiblink, &fibctx->fib_list);
1775 fibctx->count++;
1776
1777
1778
1779
1780 up(&fibctx->wait_sem);
1781 } else {
1782 printk(KERN_WARNING "aifd: didn't allocate NewFib.\n");
1783 }
1784 entry = entry->next;
1785 }
1786
1787
1788
1789 *(__le32 *)hw_fib->data = cpu_to_le32(ST_OK);
1790 aac_fib_adapter_complete(fib, sizeof(u32));
1791 spin_unlock_irqrestore(&dev->fib_lock, flagv);
1792
1793 hw_fib_p = hw_fib_pool;
1794 fib_p = fib_pool;
1795 while (hw_fib_p < &hw_fib_pool[num]) {
1796 kfree(*hw_fib_p);
1797 kfree(*fib_p);
1798 ++fib_p;
1799 ++hw_fib_p;
1800 }
1801 kfree(hw_fib_pool);
1802 kfree(fib_pool);
1803 }
1804 kfree(fib);
1805 spin_lock_irqsave(dev->queues->queue[HostNormCmdQueue].lock, flags);
1806 }
1807
1808
1809
1810 spin_unlock_irqrestore(dev->queues->queue[HostNormCmdQueue].lock, flags);
1811
1812
1813
1814
1815 if ((time_before(next_check_jiffies,next_jiffies))
1816 && ((difference = next_check_jiffies - jiffies) <= 0)) {
1817 next_check_jiffies = next_jiffies;
1818 if (aac_check_health(dev) == 0) {
1819 difference = ((long)(unsigned)check_interval)
1820 * HZ;
1821 next_check_jiffies = jiffies + difference;
1822 } else if (!dev->queues)
1823 break;
1824 }
1825 if (!time_before(next_check_jiffies,next_jiffies)
1826 && ((difference = next_jiffies - jiffies) <= 0)) {
1827 struct timeval now;
1828 int ret;
1829
1830
1831 ret = aac_check_health(dev);
1832 if (!ret && !dev->queues)
1833 break;
1834 next_check_jiffies = jiffies
1835 + ((long)(unsigned)check_interval)
1836 * HZ;
1837 do_gettimeofday(&now);
1838
1839
1840 if (((1000000 - (1000000 / HZ)) > now.tv_usec)
1841 && (now.tv_usec > (1000000 / HZ)))
1842 difference = (((1000000 - now.tv_usec) * HZ)
1843 + 500000) / 1000000;
1844 else if (ret == 0) {
1845 struct fib *fibptr;
1846
1847 if ((fibptr = aac_fib_alloc(dev))) {
1848 int status;
1849 __le32 *info;
1850
1851 aac_fib_init(fibptr);
1852
1853 info = (__le32 *) fib_data(fibptr);
1854 if (now.tv_usec > 500000)
1855 ++now.tv_sec;
1856
1857 *info = cpu_to_le32(now.tv_sec);
1858
1859 status = aac_fib_send(SendHostTime,
1860 fibptr,
1861 sizeof(*info),
1862 FsaNormal,
1863 1, 1,
1864 NULL,
1865 NULL);
1866
1867
1868 if (status >= 0)
1869 aac_fib_complete(fibptr);
1870
1871
1872 if (status != -ERESTARTSYS)
1873 aac_fib_free(fibptr);
1874 }
1875 difference = (long)(unsigned)update_interval*HZ;
1876 } else {
1877
1878 difference = 10 * HZ;
1879 }
1880 next_jiffies = jiffies + difference;
1881 if (time_before(next_check_jiffies,next_jiffies))
1882 difference = next_check_jiffies - jiffies;
1883 }
1884 if (difference <= 0)
1885 difference = 1;
1886 set_current_state(TASK_INTERRUPTIBLE);
1887 schedule_timeout(difference);
1888
1889 if (kthread_should_stop())
1890 break;
1891 }
1892 if (dev->queues)
1893 remove_wait_queue(&dev->queues->queue[HostNormCmdQueue].cmdready, &wait);
1894 dev->aif_thread = 0;
1895 return 0;
1896}
1897