1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20#include <linux/kernel.h>
21#include <linux/init.h>
22#include <linux/crash_dump.h>
23#include <linux/types.h>
24#include <linux/sched.h>
25#include <linux/pci.h>
26#include <linux/spinlock.h>
27#include <linux/slab.h>
28#include <linux/completion.h>
29#include <linux/blkdev.h>
30#include <linux/delay.h>
31#include <linux/kthread.h>
32#include <linux/interrupt.h>
33#include <linux/bcd.h>
34#include <scsi/scsi.h>
35#include <scsi/scsi_host.h>
36#include <scsi/scsi_device.h>
37#include <scsi/scsi_cmnd.h>
38
39#include "aacraid.h"
40
41
42
43
44
45
46
47
48
49static int fib_map_alloc(struct aac_dev *dev)
50{
51 if (dev->max_fib_size > AAC_MAX_NATIVE_SIZE)
52 dev->max_cmd_size = AAC_MAX_NATIVE_SIZE;
53 else
54 dev->max_cmd_size = dev->max_fib_size;
55 if (dev->max_fib_size < AAC_MAX_NATIVE_SIZE) {
56 dev->max_cmd_size = AAC_MAX_NATIVE_SIZE;
57 } else {
58 dev->max_cmd_size = dev->max_fib_size;
59 }
60
61 dprintk((KERN_INFO
62 "allocate hardware fibs dma_alloc_coherent(%p, %d * (%d + %d), %p)\n",
63 &dev->pdev->dev, dev->max_cmd_size, dev->scsi_host_ptr->can_queue,
64 AAC_NUM_MGT_FIB, &dev->hw_fib_pa));
65 dev->hw_fib_va = dma_alloc_coherent(&dev->pdev->dev,
66 (dev->max_cmd_size + sizeof(struct aac_fib_xporthdr))
67 * (dev->scsi_host_ptr->can_queue + AAC_NUM_MGT_FIB) + (ALIGN32 - 1),
68 &dev->hw_fib_pa, GFP_KERNEL);
69 if (dev->hw_fib_va == NULL)
70 return -ENOMEM;
71 return 0;
72}
73
74
75
76
77
78
79
80
81
82void aac_fib_map_free(struct aac_dev *dev)
83{
84 size_t alloc_size;
85 size_t fib_size;
86 int num_fibs;
87
88 if(!dev->hw_fib_va || !dev->max_cmd_size)
89 return;
90
91 num_fibs = dev->scsi_host_ptr->can_queue + AAC_NUM_MGT_FIB;
92 fib_size = dev->max_fib_size + sizeof(struct aac_fib_xporthdr);
93 alloc_size = fib_size * num_fibs + ALIGN32 - 1;
94
95 dma_free_coherent(&dev->pdev->dev, alloc_size, dev->hw_fib_va,
96 dev->hw_fib_pa);
97
98 dev->hw_fib_va = NULL;
99 dev->hw_fib_pa = 0;
100}
101
102void aac_fib_vector_assign(struct aac_dev *dev)
103{
104 u32 i = 0;
105 u32 vector = 1;
106 struct fib *fibptr = NULL;
107
108 for (i = 0, fibptr = &dev->fibs[i];
109 i < (dev->scsi_host_ptr->can_queue + AAC_NUM_MGT_FIB);
110 i++, fibptr++) {
111 if ((dev->max_msix == 1) ||
112 (i > ((dev->scsi_host_ptr->can_queue + AAC_NUM_MGT_FIB - 1)
113 - dev->vector_cap))) {
114 fibptr->vector_no = 0;
115 } else {
116 fibptr->vector_no = vector;
117 vector++;
118 if (vector == dev->max_msix)
119 vector = 1;
120 }
121 }
122}
123
124
125
126
127
128
129
130
131
132int aac_fib_setup(struct aac_dev * dev)
133{
134 struct fib *fibptr;
135 struct hw_fib *hw_fib;
136 dma_addr_t hw_fib_pa;
137 int i;
138 u32 max_cmds;
139
140 while (((i = fib_map_alloc(dev)) == -ENOMEM)
141 && (dev->scsi_host_ptr->can_queue > (64 - AAC_NUM_MGT_FIB))) {
142 max_cmds = (dev->scsi_host_ptr->can_queue+AAC_NUM_MGT_FIB) >> 1;
143 dev->scsi_host_ptr->can_queue = max_cmds - AAC_NUM_MGT_FIB;
144 if (dev->comm_interface != AAC_COMM_MESSAGE_TYPE3)
145 dev->init->r7.max_io_commands = cpu_to_le32(max_cmds);
146 }
147 if (i<0)
148 return -ENOMEM;
149
150 memset(dev->hw_fib_va, 0,
151 (dev->max_cmd_size + sizeof(struct aac_fib_xporthdr)) *
152 (dev->scsi_host_ptr->can_queue + AAC_NUM_MGT_FIB));
153
154
155 hw_fib_pa = (dev->hw_fib_pa + (ALIGN32 - 1)) & ~(ALIGN32 - 1);
156 hw_fib = (struct hw_fib *)((unsigned char *)dev->hw_fib_va +
157 (hw_fib_pa - dev->hw_fib_pa));
158
159
160 hw_fib = (struct hw_fib *)((unsigned char *)hw_fib +
161 sizeof(struct aac_fib_xporthdr));
162 hw_fib_pa += sizeof(struct aac_fib_xporthdr);
163
164
165
166
167 for (i = 0, fibptr = &dev->fibs[i];
168 i < (dev->scsi_host_ptr->can_queue + AAC_NUM_MGT_FIB);
169 i++, fibptr++)
170 {
171 fibptr->flags = 0;
172 fibptr->size = sizeof(struct fib);
173 fibptr->dev = dev;
174 fibptr->hw_fib_va = hw_fib;
175 fibptr->data = (void *) fibptr->hw_fib_va->data;
176 fibptr->next = fibptr+1;
177 init_completion(&fibptr->event_wait);
178 spin_lock_init(&fibptr->event_lock);
179 hw_fib->header.XferState = cpu_to_le32(0xffffffff);
180 hw_fib->header.SenderSize =
181 cpu_to_le16(dev->max_fib_size);
182 fibptr->hw_fib_pa = hw_fib_pa;
183 fibptr->hw_sgl_pa = hw_fib_pa +
184 offsetof(struct aac_hba_cmd_req, sge[2]);
185
186
187
188
189 fibptr->hw_error_pa = hw_fib_pa +
190 offsetof(struct aac_native_hba, resp.resp_bytes[0]);
191
192 hw_fib = (struct hw_fib *)((unsigned char *)hw_fib +
193 dev->max_cmd_size + sizeof(struct aac_fib_xporthdr));
194 hw_fib_pa = hw_fib_pa +
195 dev->max_cmd_size + sizeof(struct aac_fib_xporthdr);
196 }
197
198
199
200
201 aac_fib_vector_assign(dev);
202
203
204
205
206 dev->fibs[dev->scsi_host_ptr->can_queue + AAC_NUM_MGT_FIB - 1].next = NULL;
207
208
209
210 dev->free_fib = &dev->fibs[dev->scsi_host_ptr->can_queue];
211 return 0;
212}
213
214
215
216
217
218
219
220
221
222struct fib *aac_fib_alloc_tag(struct aac_dev *dev, struct scsi_cmnd *scmd)
223{
224 struct fib *fibptr;
225
226 fibptr = &dev->fibs[scmd->request->tag];
227
228
229
230
231 fibptr->hw_fib_va->header.XferState = 0;
232 fibptr->type = FSAFS_NTC_FIB_CONTEXT;
233 fibptr->callback_data = NULL;
234 fibptr->callback = NULL;
235 fibptr->flags = 0;
236
237 return fibptr;
238}
239
240
241
242
243
244
245
246
247
248struct fib *aac_fib_alloc(struct aac_dev *dev)
249{
250 struct fib * fibptr;
251 unsigned long flags;
252 spin_lock_irqsave(&dev->fib_lock, flags);
253 fibptr = dev->free_fib;
254 if(!fibptr){
255 spin_unlock_irqrestore(&dev->fib_lock, flags);
256 return fibptr;
257 }
258 dev->free_fib = fibptr->next;
259 spin_unlock_irqrestore(&dev->fib_lock, flags);
260
261
262
263 fibptr->type = FSAFS_NTC_FIB_CONTEXT;
264 fibptr->size = sizeof(struct fib);
265
266
267
268
269 fibptr->hw_fib_va->header.XferState = 0;
270 fibptr->flags = 0;
271 fibptr->callback = NULL;
272 fibptr->callback_data = NULL;
273
274 return fibptr;
275}
276
277
278
279
280
281
282
283
284void aac_fib_free(struct fib *fibptr)
285{
286 unsigned long flags;
287
288 if (fibptr->done == 2)
289 return;
290
291 spin_lock_irqsave(&fibptr->dev->fib_lock, flags);
292 if (unlikely(fibptr->flags & FIB_CONTEXT_FLAG_TIMED_OUT))
293 aac_config.fib_timeouts++;
294 if (!(fibptr->flags & FIB_CONTEXT_FLAG_NATIVE_HBA) &&
295 fibptr->hw_fib_va->header.XferState != 0) {
296 printk(KERN_WARNING "aac_fib_free, XferState != 0, fibptr = 0x%p, XferState = 0x%x\n",
297 (void*)fibptr,
298 le32_to_cpu(fibptr->hw_fib_va->header.XferState));
299 }
300 fibptr->next = fibptr->dev->free_fib;
301 fibptr->dev->free_fib = fibptr;
302 spin_unlock_irqrestore(&fibptr->dev->fib_lock, flags);
303}
304
305
306
307
308
309
310
311
312void aac_fib_init(struct fib *fibptr)
313{
314 struct hw_fib *hw_fib = fibptr->hw_fib_va;
315
316 memset(&hw_fib->header, 0, sizeof(struct aac_fibhdr));
317 hw_fib->header.StructType = FIB_MAGIC;
318 hw_fib->header.Size = cpu_to_le16(fibptr->dev->max_fib_size);
319 hw_fib->header.XferState = cpu_to_le32(HostOwned | FibInitialized | FibEmpty | FastResponseCapable);
320 hw_fib->header.u.ReceiverFibAddress = cpu_to_le32(fibptr->hw_fib_pa);
321 hw_fib->header.SenderSize = cpu_to_le16(fibptr->dev->max_fib_size);
322}
323
324
325
326
327
328
329
330
331
332static void fib_dealloc(struct fib * fibptr)
333{
334 struct hw_fib *hw_fib = fibptr->hw_fib_va;
335 hw_fib->header.XferState = 0;
336}
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358static int aac_get_entry (struct aac_dev * dev, u32 qid, struct aac_entry **entry, u32 * index, unsigned long *nonotify)
359{
360 struct aac_queue * q;
361 unsigned long idx;
362
363
364
365
366
367
368
369
370 q = &dev->queues->queue[qid];
371
372 idx = *index = le32_to_cpu(*(q->headers.producer));
373
374 if (idx != le32_to_cpu(*(q->headers.consumer))) {
375 if (--idx == 0) {
376 if (qid == AdapNormCmdQueue)
377 idx = ADAP_NORM_CMD_ENTRIES;
378 else
379 idx = ADAP_NORM_RESP_ENTRIES;
380 }
381 if (idx != le32_to_cpu(*(q->headers.consumer)))
382 *nonotify = 1;
383 }
384
385 if (qid == AdapNormCmdQueue) {
386 if (*index >= ADAP_NORM_CMD_ENTRIES)
387 *index = 0;
388 } else {
389 if (*index >= ADAP_NORM_RESP_ENTRIES)
390 *index = 0;
391 }
392
393
394 if ((*index + 1) == le32_to_cpu(*(q->headers.consumer))) {
395 printk(KERN_WARNING "Queue %d full, %u outstanding.\n",
396 qid, atomic_read(&q->numpending));
397 return 0;
398 } else {
399 *entry = q->base + *index;
400 return 1;
401 }
402}
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420int aac_queue_get(struct aac_dev * dev, u32 * index, u32 qid, struct hw_fib * hw_fib, int wait, struct fib * fibptr, unsigned long *nonotify)
421{
422 struct aac_entry * entry = NULL;
423 int map = 0;
424
425 if (qid == AdapNormCmdQueue) {
426
427 while (!aac_get_entry(dev, qid, &entry, index, nonotify)) {
428 printk(KERN_ERR "GetEntries failed\n");
429 }
430
431
432
433 entry->size = cpu_to_le32(le16_to_cpu(hw_fib->header.Size));
434 map = 1;
435 } else {
436 while (!aac_get_entry(dev, qid, &entry, index, nonotify)) {
437
438 }
439
440
441
442 entry->size = cpu_to_le32(le16_to_cpu(hw_fib->header.Size));
443 entry->addr = hw_fib->header.SenderFibAddress;
444
445 hw_fib->header.u.ReceiverFibAddress = hw_fib->header.SenderFibAddress;
446 map = 0;
447 }
448
449
450
451
452 if (map)
453 entry->addr = cpu_to_le32(fibptr->hw_fib_pa);
454 return 0;
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
482int aac_fib_send(u16 command, struct fib *fibptr, unsigned long size,
483 int priority, int wait, int reply, fib_callback callback,
484 void *callback_data)
485{
486 struct aac_dev * dev = fibptr->dev;
487 struct hw_fib * hw_fib = fibptr->hw_fib_va;
488 unsigned long flags = 0;
489 unsigned long mflags = 0;
490 unsigned long sflags = 0;
491
492 if (!(hw_fib->header.XferState & cpu_to_le32(HostOwned)))
493 return -EBUSY;
494
495 if (hw_fib->header.XferState & cpu_to_le32(AdapterProcessed))
496 return -EINVAL;
497
498
499
500
501
502
503
504
505
506
507
508
509 fibptr->flags = 0;
510 if (wait && !reply) {
511 return -EINVAL;
512 } else if (!wait && reply) {
513 hw_fib->header.XferState |= cpu_to_le32(Async | ResponseExpected);
514 FIB_COUNTER_INCREMENT(aac_config.AsyncSent);
515 } else if (!wait && !reply) {
516 hw_fib->header.XferState |= cpu_to_le32(NoResponseExpected);
517 FIB_COUNTER_INCREMENT(aac_config.NoResponseSent);
518 } else if (wait && reply) {
519 hw_fib->header.XferState |= cpu_to_le32(ResponseExpected);
520 FIB_COUNTER_INCREMENT(aac_config.NormalSent);
521 }
522
523
524
525
526 hw_fib->header.SenderFibAddress =
527 cpu_to_le32(((u32)(fibptr - dev->fibs)) << 2);
528
529
530
531
532 hw_fib->header.Handle =
533 cpu_to_le32((((u32)(fibptr - dev->fibs)) << 2) + 1);
534
535
536
537
538
539
540
541
542 hw_fib->header.Command = cpu_to_le16(command);
543 hw_fib->header.XferState |= cpu_to_le32(SentFromHost);
544
545
546
547 hw_fib->header.Size = cpu_to_le16(sizeof(struct aac_fibhdr) + size);
548 if (le16_to_cpu(hw_fib->header.Size) > le16_to_cpu(hw_fib->header.SenderSize)) {
549 return -EMSGSIZE;
550 }
551
552
553
554
555 hw_fib->header.XferState |= cpu_to_le32(NormalPriority);
556
557
558
559
560
561 if (!wait) {
562 fibptr->callback = callback;
563 fibptr->callback_data = callback_data;
564 fibptr->flags = FIB_CONTEXT_FLAG;
565 }
566
567 fibptr->done = 0;
568
569 FIB_COUNTER_INCREMENT(aac_config.FibsSent);
570
571 dprintk((KERN_DEBUG "Fib contents:.\n"));
572 dprintk((KERN_DEBUG " Command = %d.\n", le32_to_cpu(hw_fib->header.Command)));
573 dprintk((KERN_DEBUG " SubCommand = %d.\n", le32_to_cpu(((struct aac_query_mount *)fib_data(fibptr))->command)));
574 dprintk((KERN_DEBUG " XferState = %x.\n", le32_to_cpu(hw_fib->header.XferState)));
575 dprintk((KERN_DEBUG " hw_fib va being sent=%p\n",fibptr->hw_fib_va));
576 dprintk((KERN_DEBUG " hw_fib pa being sent=%lx\n",(ulong)fibptr->hw_fib_pa));
577 dprintk((KERN_DEBUG " fib being sent=%p\n",fibptr));
578
579 if (!dev->queues)
580 return -EBUSY;
581
582 if (wait) {
583
584 spin_lock_irqsave(&dev->manage_lock, mflags);
585 if (dev->management_fib_count >= AAC_NUM_MGT_FIB) {
586 printk(KERN_INFO "No management Fibs Available:%d\n",
587 dev->management_fib_count);
588 spin_unlock_irqrestore(&dev->manage_lock, mflags);
589 return -EBUSY;
590 }
591 dev->management_fib_count++;
592 spin_unlock_irqrestore(&dev->manage_lock, mflags);
593 spin_lock_irqsave(&fibptr->event_lock, flags);
594 }
595
596 if (dev->sync_mode) {
597 if (wait)
598 spin_unlock_irqrestore(&fibptr->event_lock, flags);
599 spin_lock_irqsave(&dev->sync_lock, sflags);
600 if (dev->sync_fib) {
601 list_add_tail(&fibptr->fiblink, &dev->sync_fib_list);
602 spin_unlock_irqrestore(&dev->sync_lock, sflags);
603 } else {
604 dev->sync_fib = fibptr;
605 spin_unlock_irqrestore(&dev->sync_lock, sflags);
606 aac_adapter_sync_cmd(dev, SEND_SYNCHRONOUS_FIB,
607 (u32)fibptr->hw_fib_pa, 0, 0, 0, 0, 0,
608 NULL, NULL, NULL, NULL, NULL);
609 }
610 if (wait) {
611 fibptr->flags |= FIB_CONTEXT_FLAG_WAIT;
612 if (wait_for_completion_interruptible(&fibptr->event_wait)) {
613 fibptr->flags &= ~FIB_CONTEXT_FLAG_WAIT;
614 return -EFAULT;
615 }
616 return 0;
617 }
618 return -EINPROGRESS;
619 }
620
621 if (aac_adapter_deliver(fibptr) != 0) {
622 printk(KERN_ERR "aac_fib_send: returned -EBUSY\n");
623 if (wait) {
624 spin_unlock_irqrestore(&fibptr->event_lock, flags);
625 spin_lock_irqsave(&dev->manage_lock, mflags);
626 dev->management_fib_count--;
627 spin_unlock_irqrestore(&dev->manage_lock, mflags);
628 }
629 return -EBUSY;
630 }
631
632
633
634
635
636
637 if (wait) {
638 spin_unlock_irqrestore(&fibptr->event_lock, flags);
639
640 if (wait < 0) {
641
642
643
644
645
646
647 unsigned long timeout = jiffies + (180 * HZ);
648 while (!try_wait_for_completion(&fibptr->event_wait)) {
649 int blink;
650 if (time_is_before_eq_jiffies(timeout)) {
651 struct aac_queue * q = &dev->queues->queue[AdapNormCmdQueue];
652 atomic_dec(&q->numpending);
653 if (wait == -1) {
654 printk(KERN_ERR "aacraid: aac_fib_send: first asynchronous command timed out.\n"
655 "Usually a result of a PCI interrupt routing problem;\n"
656 "update mother board BIOS or consider utilizing one of\n"
657 "the SAFE mode kernel options (acpi, apic etc)\n");
658 }
659 return -ETIMEDOUT;
660 }
661
662 if (unlikely(aac_pci_offline(dev)))
663 return -EFAULT;
664
665 if ((blink = aac_adapter_check_health(dev)) > 0) {
666 if (wait == -1) {
667 printk(KERN_ERR "aacraid: aac_fib_send: adapter blinkLED 0x%x.\n"
668 "Usually a result of a serious unrecoverable hardware problem\n",
669 blink);
670 }
671 return -EFAULT;
672 }
673
674
675
676 schedule();
677 }
678 } else if (wait_for_completion_interruptible(&fibptr->event_wait)) {
679
680
681 }
682
683 spin_lock_irqsave(&fibptr->event_lock, flags);
684 if (fibptr->done == 0) {
685 fibptr->done = 2;
686 spin_unlock_irqrestore(&fibptr->event_lock, flags);
687 return -ERESTARTSYS;
688 }
689 spin_unlock_irqrestore(&fibptr->event_lock, flags);
690 BUG_ON(fibptr->done == 0);
691
692 if(unlikely(fibptr->flags & FIB_CONTEXT_FLAG_TIMED_OUT))
693 return -ETIMEDOUT;
694 return 0;
695 }
696
697
698
699
700 if (reply)
701 return -EINPROGRESS;
702 else
703 return 0;
704}
705
706int aac_hba_send(u8 command, struct fib *fibptr, fib_callback callback,
707 void *callback_data)
708{
709 struct aac_dev *dev = fibptr->dev;
710 int wait;
711 unsigned long flags = 0;
712 unsigned long mflags = 0;
713 struct aac_hba_cmd_req *hbacmd = (struct aac_hba_cmd_req *)
714 fibptr->hw_fib_va;
715
716 fibptr->flags = (FIB_CONTEXT_FLAG | FIB_CONTEXT_FLAG_NATIVE_HBA);
717 if (callback) {
718 wait = 0;
719 fibptr->callback = callback;
720 fibptr->callback_data = callback_data;
721 } else
722 wait = 1;
723
724
725 hbacmd->iu_type = command;
726
727 if (command == HBA_IU_TYPE_SCSI_CMD_REQ) {
728
729 hbacmd->request_id =
730 cpu_to_le32((((u32)(fibptr - dev->fibs)) << 2) + 1);
731 fibptr->flags |= FIB_CONTEXT_FLAG_SCSI_CMD;
732 } else
733 return -EINVAL;
734
735
736 if (wait) {
737 spin_lock_irqsave(&dev->manage_lock, mflags);
738 if (dev->management_fib_count >= AAC_NUM_MGT_FIB) {
739 spin_unlock_irqrestore(&dev->manage_lock, mflags);
740 return -EBUSY;
741 }
742 dev->management_fib_count++;
743 spin_unlock_irqrestore(&dev->manage_lock, mflags);
744 spin_lock_irqsave(&fibptr->event_lock, flags);
745 }
746
747 if (aac_adapter_deliver(fibptr) != 0) {
748 if (wait) {
749 spin_unlock_irqrestore(&fibptr->event_lock, flags);
750 spin_lock_irqsave(&dev->manage_lock, mflags);
751 dev->management_fib_count--;
752 spin_unlock_irqrestore(&dev->manage_lock, mflags);
753 }
754 return -EBUSY;
755 }
756 FIB_COUNTER_INCREMENT(aac_config.NativeSent);
757
758 if (wait) {
759
760 spin_unlock_irqrestore(&fibptr->event_lock, flags);
761
762 if (unlikely(aac_pci_offline(dev)))
763 return -EFAULT;
764
765 fibptr->flags |= FIB_CONTEXT_FLAG_WAIT;
766 if (wait_for_completion_interruptible(&fibptr->event_wait))
767 fibptr->done = 2;
768 fibptr->flags &= ~(FIB_CONTEXT_FLAG_WAIT);
769
770 spin_lock_irqsave(&fibptr->event_lock, flags);
771 if ((fibptr->done == 0) || (fibptr->done == 2)) {
772 fibptr->done = 2;
773 spin_unlock_irqrestore(&fibptr->event_lock, flags);
774 return -ERESTARTSYS;
775 }
776 spin_unlock_irqrestore(&fibptr->event_lock, flags);
777 WARN_ON(fibptr->done == 0);
778
779 if (unlikely(fibptr->flags & FIB_CONTEXT_FLAG_TIMED_OUT))
780 return -ETIMEDOUT;
781
782 return 0;
783 }
784
785 return -EINPROGRESS;
786}
787
788
789
790
791
792
793
794
795
796
797
798
799int aac_consumer_get(struct aac_dev * dev, struct aac_queue * q, struct aac_entry **entry)
800{
801 u32 index;
802 int status;
803 if (le32_to_cpu(*q->headers.producer) == le32_to_cpu(*q->headers.consumer)) {
804 status = 0;
805 } else {
806
807
808
809
810
811 if (le32_to_cpu(*q->headers.consumer) >= q->entries)
812 index = 0;
813 else
814 index = le32_to_cpu(*q->headers.consumer);
815 *entry = q->base + index;
816 status = 1;
817 }
818 return(status);
819}
820
821
822
823
824
825
826
827
828
829
830
831void aac_consumer_free(struct aac_dev * dev, struct aac_queue *q, u32 qid)
832{
833 int wasfull = 0;
834 u32 notify;
835
836 if ((le32_to_cpu(*q->headers.producer)+1) == le32_to_cpu(*q->headers.consumer))
837 wasfull = 1;
838
839 if (le32_to_cpu(*q->headers.consumer) >= q->entries)
840 *q->headers.consumer = cpu_to_le32(1);
841 else
842 le32_add_cpu(q->headers.consumer, 1);
843
844 if (wasfull) {
845 switch (qid) {
846
847 case HostNormCmdQueue:
848 notify = HostNormCmdNotFull;
849 break;
850 case HostNormRespQueue:
851 notify = HostNormRespNotFull;
852 break;
853 default:
854 BUG();
855 return;
856 }
857 aac_adapter_notify(dev, notify);
858 }
859}
860
861
862
863
864
865
866
867
868
869
870int aac_fib_adapter_complete(struct fib *fibptr, unsigned short size)
871{
872 struct hw_fib * hw_fib = fibptr->hw_fib_va;
873 struct aac_dev * dev = fibptr->dev;
874 struct aac_queue * q;
875 unsigned long nointr = 0;
876 unsigned long qflags;
877
878 if (dev->comm_interface == AAC_COMM_MESSAGE_TYPE1 ||
879 dev->comm_interface == AAC_COMM_MESSAGE_TYPE2 ||
880 dev->comm_interface == AAC_COMM_MESSAGE_TYPE3) {
881 kfree(hw_fib);
882 return 0;
883 }
884
885 if (hw_fib->header.XferState == 0) {
886 if (dev->comm_interface == AAC_COMM_MESSAGE)
887 kfree(hw_fib);
888 return 0;
889 }
890
891
892
893 if (hw_fib->header.StructType != FIB_MAGIC &&
894 hw_fib->header.StructType != FIB_MAGIC2 &&
895 hw_fib->header.StructType != FIB_MAGIC2_64) {
896 if (dev->comm_interface == AAC_COMM_MESSAGE)
897 kfree(hw_fib);
898 return -EINVAL;
899 }
900
901
902
903
904
905
906
907 if (hw_fib->header.XferState & cpu_to_le32(SentFromAdapter)) {
908 if (dev->comm_interface == AAC_COMM_MESSAGE) {
909 kfree (hw_fib);
910 } else {
911 u32 index;
912 hw_fib->header.XferState |= cpu_to_le32(HostProcessed);
913 if (size) {
914 size += sizeof(struct aac_fibhdr);
915 if (size > le16_to_cpu(hw_fib->header.SenderSize))
916 return -EMSGSIZE;
917 hw_fib->header.Size = cpu_to_le16(size);
918 }
919 q = &dev->queues->queue[AdapNormRespQueue];
920 spin_lock_irqsave(q->lock, qflags);
921 aac_queue_get(dev, &index, AdapNormRespQueue, hw_fib, 1, NULL, &nointr);
922 *(q->headers.producer) = cpu_to_le32(index + 1);
923 spin_unlock_irqrestore(q->lock, qflags);
924 if (!(nointr & (int)aac_config.irq_mod))
925 aac_adapter_notify(dev, AdapNormRespQueue);
926 }
927 } else {
928 printk(KERN_WARNING "aac_fib_adapter_complete: "
929 "Unknown xferstate detected.\n");
930 BUG();
931 }
932 return 0;
933}
934
935
936
937
938
939
940
941
942int aac_fib_complete(struct fib *fibptr)
943{
944 struct hw_fib * hw_fib = fibptr->hw_fib_va;
945
946 if (fibptr->flags & FIB_CONTEXT_FLAG_NATIVE_HBA) {
947 fib_dealloc(fibptr);
948 return 0;
949 }
950
951
952
953
954
955
956 if (hw_fib->header.XferState == 0 || fibptr->done == 2)
957 return 0;
958
959
960
961
962 if (hw_fib->header.StructType != FIB_MAGIC &&
963 hw_fib->header.StructType != FIB_MAGIC2 &&
964 hw_fib->header.StructType != FIB_MAGIC2_64)
965 return -EINVAL;
966
967
968
969
970
971
972
973 if((hw_fib->header.XferState & cpu_to_le32(SentFromHost)) &&
974 (hw_fib->header.XferState & cpu_to_le32(AdapterProcessed)))
975 {
976 fib_dealloc(fibptr);
977 }
978 else if(hw_fib->header.XferState & cpu_to_le32(SentFromHost))
979 {
980
981
982
983
984 fib_dealloc(fibptr);
985 } else if(hw_fib->header.XferState & cpu_to_le32(HostOwned)) {
986 fib_dealloc(fibptr);
987 } else {
988 BUG();
989 }
990 return 0;
991}
992
993
994
995
996
997
998
999
1000
1001
1002void aac_printf(struct aac_dev *dev, u32 val)
1003{
1004 char *cp = dev->printfbuf;
1005 if (dev->printf_enabled)
1006 {
1007 int length = val & 0xffff;
1008 int level = (val >> 16) & 0xffff;
1009
1010
1011
1012
1013
1014 if (length > 255)
1015 length = 255;
1016 if (cp[length] != 0)
1017 cp[length] = 0;
1018 if (level == LOG_AAC_HIGH_ERROR)
1019 printk(KERN_WARNING "%s:%s", dev->name, cp);
1020 else
1021 printk(KERN_INFO "%s:%s", dev->name, cp);
1022 }
1023 memset(cp, 0, 256);
1024}
1025
1026static inline int aac_aif_data(struct aac_aifcmd *aifcmd, uint32_t index)
1027{
1028 return le32_to_cpu(((__le32 *)aifcmd->data)[index]);
1029}
1030
1031
1032static void aac_handle_aif_bu(struct aac_dev *dev, struct aac_aifcmd *aifcmd)
1033{
1034 switch (aac_aif_data(aifcmd, 1)) {
1035 case AifBuCacheDataLoss:
1036 if (aac_aif_data(aifcmd, 2))
1037 dev_info(&dev->pdev->dev, "Backup unit had cache data loss - [%d]\n",
1038 aac_aif_data(aifcmd, 2));
1039 else
1040 dev_info(&dev->pdev->dev, "Backup Unit had cache data loss\n");
1041 break;
1042 case AifBuCacheDataRecover:
1043 if (aac_aif_data(aifcmd, 2))
1044 dev_info(&dev->pdev->dev, "DDR cache data recovered successfully - [%d]\n",
1045 aac_aif_data(aifcmd, 2));
1046 else
1047 dev_info(&dev->pdev->dev, "DDR cache data recovered successfully\n");
1048 break;
1049 }
1050}
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061#define AIF_SNIFF_TIMEOUT (500*HZ)
1062static void aac_handle_aif(struct aac_dev * dev, struct fib * fibptr)
1063{
1064 struct hw_fib * hw_fib = fibptr->hw_fib_va;
1065 struct aac_aifcmd * aifcmd = (struct aac_aifcmd *)hw_fib->data;
1066 u32 channel, id, lun, container;
1067 struct scsi_device *device;
1068 enum {
1069 NOTHING,
1070 DELETE,
1071 ADD,
1072 CHANGE
1073 } device_config_needed = NOTHING;
1074
1075
1076
1077 if (!dev || !dev->fsa_dev)
1078 return;
1079 container = channel = id = lun = (u32)-1;
1080
1081
1082
1083
1084
1085
1086
1087 switch (le32_to_cpu(aifcmd->command)) {
1088 case AifCmdDriverNotify:
1089 switch (le32_to_cpu(((__le32 *)aifcmd->data)[0])) {
1090 case AifRawDeviceRemove:
1091 container = le32_to_cpu(((__le32 *)aifcmd->data)[1]);
1092 if ((container >> 28)) {
1093 container = (u32)-1;
1094 break;
1095 }
1096 channel = (container >> 24) & 0xF;
1097 if (channel >= dev->maximum_num_channels) {
1098 container = (u32)-1;
1099 break;
1100 }
1101 id = container & 0xFFFF;
1102 if (id >= dev->maximum_num_physicals) {
1103 container = (u32)-1;
1104 break;
1105 }
1106 lun = (container >> 16) & 0xFF;
1107 container = (u32)-1;
1108 channel = aac_phys_to_logical(channel);
1109 device_config_needed = DELETE;
1110 break;
1111
1112
1113
1114
1115 case AifDenMorphComplete:
1116 case AifDenVolumeExtendComplete:
1117 container = le32_to_cpu(((__le32 *)aifcmd->data)[1]);
1118 if (container >= dev->maximum_num_containers)
1119 break;
1120
1121
1122
1123
1124
1125
1126
1127
1128 if ((dev != NULL) && (dev->scsi_host_ptr != NULL)) {
1129 device = scsi_device_lookup(dev->scsi_host_ptr,
1130 CONTAINER_TO_CHANNEL(container),
1131 CONTAINER_TO_ID(container),
1132 CONTAINER_TO_LUN(container));
1133 if (device) {
1134 dev->fsa_dev[container].config_needed = CHANGE;
1135 dev->fsa_dev[container].config_waiting_on = AifEnConfigChange;
1136 dev->fsa_dev[container].config_waiting_stamp = jiffies;
1137 scsi_device_put(device);
1138 }
1139 }
1140 }
1141
1142
1143
1144
1145
1146 if (container != (u32)-1) {
1147 if (container >= dev->maximum_num_containers)
1148 break;
1149 if ((dev->fsa_dev[container].config_waiting_on ==
1150 le32_to_cpu(*(__le32 *)aifcmd->data)) &&
1151 time_before(jiffies, dev->fsa_dev[container].config_waiting_stamp + AIF_SNIFF_TIMEOUT))
1152 dev->fsa_dev[container].config_waiting_on = 0;
1153 } else for (container = 0;
1154 container < dev->maximum_num_containers; ++container) {
1155 if ((dev->fsa_dev[container].config_waiting_on ==
1156 le32_to_cpu(*(__le32 *)aifcmd->data)) &&
1157 time_before(jiffies, dev->fsa_dev[container].config_waiting_stamp + AIF_SNIFF_TIMEOUT))
1158 dev->fsa_dev[container].config_waiting_on = 0;
1159 }
1160 break;
1161
1162 case AifCmdEventNotify:
1163 switch (le32_to_cpu(((__le32 *)aifcmd->data)[0])) {
1164 case AifEnBatteryEvent:
1165 dev->cache_protected =
1166 (((__le32 *)aifcmd->data)[1] == cpu_to_le32(3));
1167 break;
1168
1169
1170
1171 case AifEnAddContainer:
1172 container = le32_to_cpu(((__le32 *)aifcmd->data)[1]);
1173 if (container >= dev->maximum_num_containers)
1174 break;
1175 dev->fsa_dev[container].config_needed = ADD;
1176 dev->fsa_dev[container].config_waiting_on =
1177 AifEnConfigChange;
1178 dev->fsa_dev[container].config_waiting_stamp = jiffies;
1179 break;
1180
1181
1182
1183
1184 case AifEnDeleteContainer:
1185 container = le32_to_cpu(((__le32 *)aifcmd->data)[1]);
1186 if (container >= dev->maximum_num_containers)
1187 break;
1188 dev->fsa_dev[container].config_needed = DELETE;
1189 dev->fsa_dev[container].config_waiting_on =
1190 AifEnConfigChange;
1191 dev->fsa_dev[container].config_waiting_stamp = jiffies;
1192 break;
1193
1194
1195
1196
1197
1198 case AifEnContainerChange:
1199 container = le32_to_cpu(((__le32 *)aifcmd->data)[1]);
1200 if (container >= dev->maximum_num_containers)
1201 break;
1202 if (dev->fsa_dev[container].config_waiting_on &&
1203 time_before(jiffies, dev->fsa_dev[container].config_waiting_stamp + AIF_SNIFF_TIMEOUT))
1204 break;
1205 dev->fsa_dev[container].config_needed = CHANGE;
1206 dev->fsa_dev[container].config_waiting_on =
1207 AifEnConfigChange;
1208 dev->fsa_dev[container].config_waiting_stamp = jiffies;
1209 break;
1210
1211 case AifEnConfigChange:
1212 break;
1213
1214 case AifEnAddJBOD:
1215 case AifEnDeleteJBOD:
1216 container = le32_to_cpu(((__le32 *)aifcmd->data)[1]);
1217 if ((container >> 28)) {
1218 container = (u32)-1;
1219 break;
1220 }
1221 channel = (container >> 24) & 0xF;
1222 if (channel >= dev->maximum_num_channels) {
1223 container = (u32)-1;
1224 break;
1225 }
1226 id = container & 0xFFFF;
1227 if (id >= dev->maximum_num_physicals) {
1228 container = (u32)-1;
1229 break;
1230 }
1231 lun = (container >> 16) & 0xFF;
1232 container = (u32)-1;
1233 channel = aac_phys_to_logical(channel);
1234 device_config_needed =
1235 (((__le32 *)aifcmd->data)[0] ==
1236 cpu_to_le32(AifEnAddJBOD)) ? ADD : DELETE;
1237 if (device_config_needed == ADD) {
1238 device = scsi_device_lookup(dev->scsi_host_ptr,
1239 channel,
1240 id,
1241 lun);
1242 if (device) {
1243 scsi_remove_device(device);
1244 scsi_device_put(device);
1245 }
1246 }
1247 break;
1248
1249 case AifEnEnclosureManagement:
1250
1251
1252
1253
1254 if (dev->jbod)
1255 break;
1256 switch (le32_to_cpu(((__le32 *)aifcmd->data)[3])) {
1257 case EM_DRIVE_INSERTION:
1258 case EM_DRIVE_REMOVAL:
1259 case EM_SES_DRIVE_INSERTION:
1260 case EM_SES_DRIVE_REMOVAL:
1261 container = le32_to_cpu(
1262 ((__le32 *)aifcmd->data)[2]);
1263 if ((container >> 28)) {
1264 container = (u32)-1;
1265 break;
1266 }
1267 channel = (container >> 24) & 0xF;
1268 if (channel >= dev->maximum_num_channels) {
1269 container = (u32)-1;
1270 break;
1271 }
1272 id = container & 0xFFFF;
1273 lun = (container >> 16) & 0xFF;
1274 container = (u32)-1;
1275 if (id >= dev->maximum_num_physicals) {
1276
1277 if ((0x2000 <= id) || lun || channel ||
1278 ((channel = (id >> 7) & 0x3F) >=
1279 dev->maximum_num_channels))
1280 break;
1281 lun = (id >> 4) & 7;
1282 id &= 0xF;
1283 }
1284 channel = aac_phys_to_logical(channel);
1285 device_config_needed =
1286 ((((__le32 *)aifcmd->data)[3]
1287 == cpu_to_le32(EM_DRIVE_INSERTION)) ||
1288 (((__le32 *)aifcmd->data)[3]
1289 == cpu_to_le32(EM_SES_DRIVE_INSERTION))) ?
1290 ADD : DELETE;
1291 break;
1292 }
1293 break;
1294 case AifBuManagerEvent:
1295 aac_handle_aif_bu(dev, aifcmd);
1296 break;
1297 }
1298
1299
1300
1301
1302
1303 if (container != (u32)-1) {
1304 if (container >= dev->maximum_num_containers)
1305 break;
1306 if ((dev->fsa_dev[container].config_waiting_on ==
1307 le32_to_cpu(*(__le32 *)aifcmd->data)) &&
1308 time_before(jiffies, dev->fsa_dev[container].config_waiting_stamp + AIF_SNIFF_TIMEOUT))
1309 dev->fsa_dev[container].config_waiting_on = 0;
1310 } else for (container = 0;
1311 container < dev->maximum_num_containers; ++container) {
1312 if ((dev->fsa_dev[container].config_waiting_on ==
1313 le32_to_cpu(*(__le32 *)aifcmd->data)) &&
1314 time_before(jiffies, dev->fsa_dev[container].config_waiting_stamp + AIF_SNIFF_TIMEOUT))
1315 dev->fsa_dev[container].config_waiting_on = 0;
1316 }
1317 break;
1318
1319 case AifCmdJobProgress:
1320
1321
1322
1323
1324
1325
1326
1327
1328 if (((__le32 *)aifcmd->data)[1] == cpu_to_le32(AifJobCtrZero) &&
1329 (((__le32 *)aifcmd->data)[6] == ((__le32 *)aifcmd->data)[5] ||
1330 ((__le32 *)aifcmd->data)[4] == cpu_to_le32(AifJobStsSuccess))) {
1331 for (container = 0;
1332 container < dev->maximum_num_containers;
1333 ++container) {
1334
1335
1336
1337
1338 dev->fsa_dev[container].config_waiting_on =
1339 AifEnContainerChange;
1340 dev->fsa_dev[container].config_needed = ADD;
1341 dev->fsa_dev[container].config_waiting_stamp =
1342 jiffies;
1343 }
1344 }
1345 if (((__le32 *)aifcmd->data)[1] == cpu_to_le32(AifJobCtrZero) &&
1346 ((__le32 *)aifcmd->data)[6] == 0 &&
1347 ((__le32 *)aifcmd->data)[4] == cpu_to_le32(AifJobStsRunning)) {
1348 for (container = 0;
1349 container < dev->maximum_num_containers;
1350 ++container) {
1351
1352
1353
1354
1355 dev->fsa_dev[container].config_waiting_on =
1356 AifEnContainerChange;
1357 dev->fsa_dev[container].config_needed = DELETE;
1358 dev->fsa_dev[container].config_waiting_stamp =
1359 jiffies;
1360 }
1361 }
1362 break;
1363 }
1364
1365 container = 0;
1366retry_next:
1367 if (device_config_needed == NOTHING) {
1368 for (; container < dev->maximum_num_containers; ++container) {
1369 if ((dev->fsa_dev[container].config_waiting_on == 0) &&
1370 (dev->fsa_dev[container].config_needed != NOTHING) &&
1371 time_before(jiffies, dev->fsa_dev[container].config_waiting_stamp + AIF_SNIFF_TIMEOUT)) {
1372 device_config_needed =
1373 dev->fsa_dev[container].config_needed;
1374 dev->fsa_dev[container].config_needed = NOTHING;
1375 channel = CONTAINER_TO_CHANNEL(container);
1376 id = CONTAINER_TO_ID(container);
1377 lun = CONTAINER_TO_LUN(container);
1378 break;
1379 }
1380 }
1381 }
1382 if (device_config_needed == NOTHING)
1383 return;
1384
1385
1386
1387
1388
1389
1390
1391
1392
1393
1394
1395
1396
1397 if (!dev || !dev->scsi_host_ptr)
1398 return;
1399
1400
1401
1402 if ((channel == CONTAINER_CHANNEL) &&
1403 (device_config_needed != NOTHING)) {
1404 if (dev->fsa_dev[container].valid == 1)
1405 dev->fsa_dev[container].valid = 2;
1406 aac_probe_container(dev, container);
1407 }
1408 device = scsi_device_lookup(dev->scsi_host_ptr, channel, id, lun);
1409 if (device) {
1410 switch (device_config_needed) {
1411 case DELETE:
1412#if (defined(AAC_DEBUG_INSTRUMENT_AIF_DELETE))
1413 scsi_remove_device(device);
1414#else
1415 if (scsi_device_online(device)) {
1416 scsi_device_set_state(device, SDEV_OFFLINE);
1417 sdev_printk(KERN_INFO, device,
1418 "Device offlined - %s\n",
1419 (channel == CONTAINER_CHANNEL) ?
1420 "array deleted" :
1421 "enclosure services event");
1422 }
1423#endif
1424 break;
1425 case ADD:
1426 if (!scsi_device_online(device)) {
1427 sdev_printk(KERN_INFO, device,
1428 "Device online - %s\n",
1429 (channel == CONTAINER_CHANNEL) ?
1430 "array created" :
1431 "enclosure services event");
1432 scsi_device_set_state(device, SDEV_RUNNING);
1433 }
1434
1435 case CHANGE:
1436 if ((channel == CONTAINER_CHANNEL)
1437 && (!dev->fsa_dev[container].valid)) {
1438#if (defined(AAC_DEBUG_INSTRUMENT_AIF_DELETE))
1439 scsi_remove_device(device);
1440#else
1441 if (!scsi_device_online(device))
1442 break;
1443 scsi_device_set_state(device, SDEV_OFFLINE);
1444 sdev_printk(KERN_INFO, device,
1445 "Device offlined - %s\n",
1446 "array failed");
1447#endif
1448 break;
1449 }
1450 scsi_rescan_device(&device->sdev_gendev);
1451
1452 default:
1453 break;
1454 }
1455 scsi_device_put(device);
1456 device_config_needed = NOTHING;
1457 }
1458 if (device_config_needed == ADD)
1459 scsi_add_device(dev->scsi_host_ptr, channel, id, lun);
1460 if (channel == CONTAINER_CHANNEL) {
1461 container++;
1462 device_config_needed = NOTHING;
1463 goto retry_next;
1464 }
1465}
1466
1467static void aac_schedule_bus_scan(struct aac_dev *aac)
1468{
1469 if (aac->sa_firmware)
1470 aac_schedule_safw_scan_worker(aac);
1471 else
1472 aac_schedule_src_reinit_aif_worker(aac);
1473}
1474
1475static int _aac_reset_adapter(struct aac_dev *aac, int forced, u8 reset_type)
1476{
1477 int index, quirks;
1478 int retval;
1479 struct Scsi_Host *host = aac->scsi_host_ptr;
1480 int jafo = 0;
1481 int bled;
1482 u64 dmamask;
1483 int num_of_fibs = 0;
1484
1485
1486
1487
1488
1489
1490
1491
1492
1493
1494
1495 aac_adapter_disable_int(aac);
1496 if (aac->thread && aac->thread->pid != current->pid) {
1497 spin_unlock_irq(host->host_lock);
1498 kthread_stop(aac->thread);
1499 aac->thread = NULL;
1500 jafo = 1;
1501 }
1502
1503
1504
1505
1506
1507 bled = forced ? 0 : aac_adapter_check_health(aac);
1508 retval = aac_adapter_restart(aac, bled, reset_type);
1509
1510 if (retval)
1511 goto out;
1512
1513
1514
1515
1516 retval = 1;
1517 num_of_fibs = aac->scsi_host_ptr->can_queue + AAC_NUM_MGT_FIB;
1518 for (index = 0; index < num_of_fibs; index++) {
1519
1520 struct fib *fib = &aac->fibs[index];
1521 __le32 XferState = fib->hw_fib_va->header.XferState;
1522 bool is_response_expected = false;
1523
1524 if (!(XferState & cpu_to_le32(NoResponseExpected | Async)) &&
1525 (XferState & cpu_to_le32(ResponseExpected)))
1526 is_response_expected = true;
1527
1528 if (is_response_expected
1529 || fib->flags & FIB_CONTEXT_FLAG_WAIT) {
1530 unsigned long flagv;
1531 spin_lock_irqsave(&fib->event_lock, flagv);
1532 complete(&fib->event_wait);
1533 spin_unlock_irqrestore(&fib->event_lock, flagv);
1534 schedule();
1535 retval = 0;
1536 }
1537 }
1538
1539 if (retval == 0)
1540 ssleep(2);
1541 index = aac->cardtype;
1542
1543
1544
1545
1546
1547
1548
1549
1550 aac_free_irq(aac);
1551 aac_fib_map_free(aac);
1552 dma_free_coherent(&aac->pdev->dev, aac->comm_size, aac->comm_addr,
1553 aac->comm_phys);
1554 aac->comm_addr = NULL;
1555 aac->comm_phys = 0;
1556 kfree(aac->queues);
1557 aac->queues = NULL;
1558 kfree(aac->fsa_dev);
1559 aac->fsa_dev = NULL;
1560
1561 dmamask = DMA_BIT_MASK(32);
1562 quirks = aac_get_driver_ident(index)->quirks;
1563 if (quirks & AAC_QUIRK_31BIT)
1564 retval = pci_set_dma_mask(aac->pdev, dmamask);
1565 else if (!(quirks & AAC_QUIRK_SRC))
1566 retval = pci_set_dma_mask(aac->pdev, dmamask);
1567 else
1568 retval = pci_set_consistent_dma_mask(aac->pdev, dmamask);
1569
1570 if (quirks & AAC_QUIRK_31BIT && !retval) {
1571 dmamask = DMA_BIT_MASK(31);
1572 retval = pci_set_consistent_dma_mask(aac->pdev, dmamask);
1573 }
1574
1575 if (retval)
1576 goto out;
1577
1578 if ((retval = (*(aac_get_driver_ident(index)->init))(aac)))
1579 goto out;
1580
1581 if (jafo) {
1582 aac->thread = kthread_run(aac_command_thread, aac, "%s",
1583 aac->name);
1584 if (IS_ERR(aac->thread)) {
1585 retval = PTR_ERR(aac->thread);
1586 aac->thread = NULL;
1587 goto out;
1588 }
1589 }
1590 (void)aac_get_adapter_info(aac);
1591 if ((quirks & AAC_QUIRK_34SG) && (host->sg_tablesize > 34)) {
1592 host->sg_tablesize = 34;
1593 host->max_sectors = (host->sg_tablesize * 8) + 112;
1594 }
1595 if ((quirks & AAC_QUIRK_17SG) && (host->sg_tablesize > 17)) {
1596 host->sg_tablesize = 17;
1597 host->max_sectors = (host->sg_tablesize * 8) + 112;
1598 }
1599 aac_get_config_status(aac, 1);
1600 aac_get_containers(aac);
1601
1602
1603
1604
1605 scsi_host_complete_all_commands(host, DID_RESET);
1606
1607 retval = 0;
1608out:
1609 aac->in_reset = 0;
1610
1611
1612
1613
1614
1615 if (!retval && !is_kdump_kernel()) {
1616 dev_info(&aac->pdev->dev, "Scheduling bus rescan\n");
1617 aac_schedule_bus_scan(aac);
1618 }
1619
1620 if (jafo) {
1621 spin_lock_irq(host->host_lock);
1622 }
1623 return retval;
1624}
1625
1626int aac_reset_adapter(struct aac_dev *aac, int forced, u8 reset_type)
1627{
1628 unsigned long flagv = 0;
1629 int retval, unblock_retval;
1630 struct Scsi_Host *host = aac->scsi_host_ptr;
1631 int bled;
1632
1633 if (spin_trylock_irqsave(&aac->fib_lock, flagv) == 0)
1634 return -EBUSY;
1635
1636 if (aac->in_reset) {
1637 spin_unlock_irqrestore(&aac->fib_lock, flagv);
1638 return -EBUSY;
1639 }
1640 aac->in_reset = 1;
1641 spin_unlock_irqrestore(&aac->fib_lock, flagv);
1642
1643
1644
1645
1646
1647
1648 scsi_host_block(host);
1649
1650
1651 if (forced < 2)
1652 aac_send_shutdown(aac);
1653 spin_lock_irqsave(host->host_lock, flagv);
1654 bled = forced ? forced :
1655 (aac_check_reset != 0 && aac_check_reset != 1);
1656 retval = _aac_reset_adapter(aac, bled, reset_type);
1657 spin_unlock_irqrestore(host->host_lock, flagv);
1658
1659 unblock_retval = scsi_host_unblock(host, SDEV_RUNNING);
1660 if (!retval)
1661 retval = unblock_retval;
1662 if ((forced < 2) && (retval == -ENODEV)) {
1663
1664 struct fib * fibctx = aac_fib_alloc(aac);
1665 if (fibctx) {
1666 struct aac_pause *cmd;
1667 int status;
1668
1669 aac_fib_init(fibctx);
1670
1671 cmd = (struct aac_pause *) fib_data(fibctx);
1672
1673 cmd->command = cpu_to_le32(VM_ContainerConfig);
1674 cmd->type = cpu_to_le32(CT_PAUSE_IO);
1675 cmd->timeout = cpu_to_le32(1);
1676 cmd->min = cpu_to_le32(1);
1677 cmd->noRescan = cpu_to_le32(1);
1678 cmd->count = cpu_to_le32(0);
1679
1680 status = aac_fib_send(ContainerCommand,
1681 fibctx,
1682 sizeof(struct aac_pause),
1683 FsaNormal,
1684 -2 , 1,
1685 NULL, NULL);
1686
1687 if (status >= 0)
1688 aac_fib_complete(fibctx);
1689
1690
1691 if (status != -ERESTARTSYS)
1692 aac_fib_free(fibctx);
1693 }
1694 }
1695
1696 return retval;
1697}
1698
1699int aac_check_health(struct aac_dev * aac)
1700{
1701 int BlinkLED;
1702 unsigned long time_now, flagv = 0;
1703 struct list_head * entry;
1704
1705
1706 if (spin_trylock_irqsave(&aac->fib_lock, flagv) == 0)
1707 return 0;
1708
1709 if (aac->in_reset || !(BlinkLED = aac_adapter_check_health(aac))) {
1710 spin_unlock_irqrestore(&aac->fib_lock, flagv);
1711 return 0;
1712 }
1713
1714 aac->in_reset = 1;
1715
1716
1717
1718
1719
1720
1721
1722
1723
1724
1725 time_now = jiffies/HZ;
1726 entry = aac->fib_list.next;
1727
1728
1729
1730
1731
1732
1733
1734 while (entry != &aac->fib_list) {
1735
1736
1737
1738 struct aac_fib_context *fibctx = list_entry(entry, struct aac_fib_context, next);
1739 struct hw_fib * hw_fib;
1740 struct fib * fib;
1741
1742
1743
1744
1745 if (fibctx->count > 20) {
1746
1747
1748
1749
1750
1751 u32 time_last = fibctx->jiffies;
1752
1753
1754
1755
1756
1757 if ((time_now - time_last) > aif_timeout) {
1758 entry = entry->next;
1759 aac_close_fib_context(aac, fibctx);
1760 continue;
1761 }
1762 }
1763
1764
1765
1766
1767 hw_fib = kzalloc(sizeof(struct hw_fib), GFP_ATOMIC);
1768 fib = kzalloc(sizeof(struct fib), GFP_ATOMIC);
1769 if (fib && hw_fib) {
1770 struct aac_aifcmd * aif;
1771
1772 fib->hw_fib_va = hw_fib;
1773 fib->dev = aac;
1774 aac_fib_init(fib);
1775 fib->type = FSAFS_NTC_FIB_CONTEXT;
1776 fib->size = sizeof (struct fib);
1777 fib->data = hw_fib->data;
1778 aif = (struct aac_aifcmd *)hw_fib->data;
1779 aif->command = cpu_to_le32(AifCmdEventNotify);
1780 aif->seqnum = cpu_to_le32(0xFFFFFFFF);
1781 ((__le32 *)aif->data)[0] = cpu_to_le32(AifEnExpEvent);
1782 ((__le32 *)aif->data)[1] = cpu_to_le32(AifExeFirmwarePanic);
1783 ((__le32 *)aif->data)[2] = cpu_to_le32(AifHighPriority);
1784 ((__le32 *)aif->data)[3] = cpu_to_le32(BlinkLED);
1785
1786
1787
1788
1789
1790 list_add_tail(&fib->fiblink, &fibctx->fib_list);
1791 fibctx->count++;
1792
1793
1794
1795
1796 complete(&fibctx->completion);
1797 } else {
1798 printk(KERN_WARNING "aifd: didn't allocate NewFib.\n");
1799 kfree(fib);
1800 kfree(hw_fib);
1801 }
1802 entry = entry->next;
1803 }
1804
1805 spin_unlock_irqrestore(&aac->fib_lock, flagv);
1806
1807 if (BlinkLED < 0) {
1808 printk(KERN_ERR "%s: Host adapter is dead (or got a PCI error) %d\n",
1809 aac->name, BlinkLED);
1810 goto out;
1811 }
1812
1813 printk(KERN_ERR "%s: Host adapter BLINK LED 0x%x\n", aac->name, BlinkLED);
1814
1815out:
1816 aac->in_reset = 0;
1817 return BlinkLED;
1818}
1819
1820static inline int is_safw_raid_volume(struct aac_dev *aac, int bus, int target)
1821{
1822 return bus == CONTAINER_CHANNEL && target < aac->maximum_num_containers;
1823}
1824
1825static struct scsi_device *aac_lookup_safw_scsi_device(struct aac_dev *dev,
1826 int bus,
1827 int target)
1828{
1829 if (bus != CONTAINER_CHANNEL)
1830 bus = aac_phys_to_logical(bus);
1831
1832 return scsi_device_lookup(dev->scsi_host_ptr, bus, target, 0);
1833}
1834
1835static int aac_add_safw_device(struct aac_dev *dev, int bus, int target)
1836{
1837 if (bus != CONTAINER_CHANNEL)
1838 bus = aac_phys_to_logical(bus);
1839
1840 return scsi_add_device(dev->scsi_host_ptr, bus, target, 0);
1841}
1842
1843static void aac_put_safw_scsi_device(struct scsi_device *sdev)
1844{
1845 if (sdev)
1846 scsi_device_put(sdev);
1847}
1848
1849static void aac_remove_safw_device(struct aac_dev *dev, int bus, int target)
1850{
1851 struct scsi_device *sdev;
1852
1853 sdev = aac_lookup_safw_scsi_device(dev, bus, target);
1854 scsi_remove_device(sdev);
1855 aac_put_safw_scsi_device(sdev);
1856}
1857
1858static inline int aac_is_safw_scan_count_equal(struct aac_dev *dev,
1859 int bus, int target)
1860{
1861 return dev->hba_map[bus][target].scan_counter == dev->scan_counter;
1862}
1863
1864static int aac_is_safw_target_valid(struct aac_dev *dev, int bus, int target)
1865{
1866 if (is_safw_raid_volume(dev, bus, target))
1867 return dev->fsa_dev[target].valid;
1868 else
1869 return aac_is_safw_scan_count_equal(dev, bus, target);
1870}
1871
1872static int aac_is_safw_device_exposed(struct aac_dev *dev, int bus, int target)
1873{
1874 int is_exposed = 0;
1875 struct scsi_device *sdev;
1876
1877 sdev = aac_lookup_safw_scsi_device(dev, bus, target);
1878 if (sdev)
1879 is_exposed = 1;
1880 aac_put_safw_scsi_device(sdev);
1881
1882 return is_exposed;
1883}
1884
1885static int aac_update_safw_host_devices(struct aac_dev *dev)
1886{
1887 int i;
1888 int bus;
1889 int target;
1890 int is_exposed = 0;
1891 int rcode = 0;
1892
1893 rcode = aac_setup_safw_adapter(dev);
1894 if (unlikely(rcode < 0)) {
1895 goto out;
1896 }
1897
1898 for (i = 0; i < AAC_BUS_TARGET_LOOP; i++) {
1899
1900 bus = get_bus_number(i);
1901 target = get_target_number(i);
1902
1903 is_exposed = aac_is_safw_device_exposed(dev, bus, target);
1904
1905 if (aac_is_safw_target_valid(dev, bus, target) && !is_exposed)
1906 aac_add_safw_device(dev, bus, target);
1907 else if (!aac_is_safw_target_valid(dev, bus, target) &&
1908 is_exposed)
1909 aac_remove_safw_device(dev, bus, target);
1910 }
1911out:
1912 return rcode;
1913}
1914
1915static int aac_scan_safw_host(struct aac_dev *dev)
1916{
1917 int rcode = 0;
1918
1919 rcode = aac_update_safw_host_devices(dev);
1920 if (rcode)
1921 aac_schedule_safw_scan_worker(dev);
1922
1923 return rcode;
1924}
1925
1926int aac_scan_host(struct aac_dev *dev)
1927{
1928 int rcode = 0;
1929
1930 mutex_lock(&dev->scan_mutex);
1931 if (dev->sa_firmware)
1932 rcode = aac_scan_safw_host(dev);
1933 else
1934 scsi_scan_host(dev->scsi_host_ptr);
1935 mutex_unlock(&dev->scan_mutex);
1936
1937 return rcode;
1938}
1939
1940void aac_src_reinit_aif_worker(struct work_struct *work)
1941{
1942 struct aac_dev *dev = container_of(to_delayed_work(work),
1943 struct aac_dev, src_reinit_aif_worker);
1944
1945 wait_event(dev->scsi_host_ptr->host_wait,
1946 !scsi_host_in_recovery(dev->scsi_host_ptr));
1947 aac_reinit_aif(dev, dev->cardtype);
1948}
1949
1950
1951
1952
1953
1954
1955
1956
1957
1958static void aac_handle_sa_aif(struct aac_dev *dev, struct fib *fibptr)
1959{
1960 int i;
1961 u32 events = 0;
1962
1963 if (fibptr->hbacmd_size & SA_AIF_HOTPLUG)
1964 events = SA_AIF_HOTPLUG;
1965 else if (fibptr->hbacmd_size & SA_AIF_HARDWARE)
1966 events = SA_AIF_HARDWARE;
1967 else if (fibptr->hbacmd_size & SA_AIF_PDEV_CHANGE)
1968 events = SA_AIF_PDEV_CHANGE;
1969 else if (fibptr->hbacmd_size & SA_AIF_LDEV_CHANGE)
1970 events = SA_AIF_LDEV_CHANGE;
1971 else if (fibptr->hbacmd_size & SA_AIF_BPSTAT_CHANGE)
1972 events = SA_AIF_BPSTAT_CHANGE;
1973 else if (fibptr->hbacmd_size & SA_AIF_BPCFG_CHANGE)
1974 events = SA_AIF_BPCFG_CHANGE;
1975
1976 switch (events) {
1977 case SA_AIF_HOTPLUG:
1978 case SA_AIF_HARDWARE:
1979 case SA_AIF_PDEV_CHANGE:
1980 case SA_AIF_LDEV_CHANGE:
1981 case SA_AIF_BPCFG_CHANGE:
1982
1983 aac_scan_host(dev);
1984
1985 break;
1986
1987 case SA_AIF_BPSTAT_CHANGE:
1988
1989 break;
1990 }
1991
1992 for (i = 1; i <= 10; ++i) {
1993 events = src_readl(dev, MUnit.IDR);
1994 if (events & (1<<23)) {
1995 pr_warn(" AIF not cleared by firmware - %d/%d)\n",
1996 i, 10);
1997 ssleep(1);
1998 }
1999 }
2000}
2001
2002static int get_fib_count(struct aac_dev *dev)
2003{
2004 unsigned int num = 0;
2005 struct list_head *entry;
2006 unsigned long flagv;
2007
2008
2009
2010
2011
2012
2013
2014 num = le32_to_cpu(dev->init->r7.adapter_fibs_size)
2015 / sizeof(struct hw_fib);
2016 spin_lock_irqsave(&dev->fib_lock, flagv);
2017 entry = dev->fib_list.next;
2018 while (entry != &dev->fib_list) {
2019 entry = entry->next;
2020 ++num;
2021 }
2022 spin_unlock_irqrestore(&dev->fib_lock, flagv);
2023
2024 return num;
2025}
2026
2027static int fillup_pools(struct aac_dev *dev, struct hw_fib **hw_fib_pool,
2028 struct fib **fib_pool,
2029 unsigned int num)
2030{
2031 struct hw_fib **hw_fib_p;
2032 struct fib **fib_p;
2033
2034 hw_fib_p = hw_fib_pool;
2035 fib_p = fib_pool;
2036 while (hw_fib_p < &hw_fib_pool[num]) {
2037 *(hw_fib_p) = kmalloc(sizeof(struct hw_fib), GFP_KERNEL);
2038 if (!(*(hw_fib_p++))) {
2039 --hw_fib_p;
2040 break;
2041 }
2042
2043 *(fib_p) = kmalloc(sizeof(struct fib), GFP_KERNEL);
2044 if (!(*(fib_p++))) {
2045 kfree(*(--hw_fib_p));
2046 break;
2047 }
2048 }
2049
2050
2051
2052
2053 num = hw_fib_p - hw_fib_pool;
2054 return num;
2055}
2056
2057static void wakeup_fibctx_threads(struct aac_dev *dev,
2058 struct hw_fib **hw_fib_pool,
2059 struct fib **fib_pool,
2060 struct fib *fib,
2061 struct hw_fib *hw_fib,
2062 unsigned int num)
2063{
2064 unsigned long flagv;
2065 struct list_head *entry;
2066 struct hw_fib **hw_fib_p;
2067 struct fib **fib_p;
2068 u32 time_now, time_last;
2069 struct hw_fib *hw_newfib;
2070 struct fib *newfib;
2071 struct aac_fib_context *fibctx;
2072
2073 time_now = jiffies/HZ;
2074 spin_lock_irqsave(&dev->fib_lock, flagv);
2075 entry = dev->fib_list.next;
2076
2077
2078
2079
2080
2081
2082
2083 hw_fib_p = hw_fib_pool;
2084 fib_p = fib_pool;
2085 while (entry != &dev->fib_list) {
2086
2087
2088
2089 fibctx = list_entry(entry, struct aac_fib_context,
2090 next);
2091
2092
2093
2094
2095 if (fibctx->count > 20) {
2096
2097
2098
2099
2100
2101 time_last = fibctx->jiffies;
2102
2103
2104
2105
2106
2107 if ((time_now - time_last) > aif_timeout) {
2108 entry = entry->next;
2109 aac_close_fib_context(dev, fibctx);
2110 continue;
2111 }
2112 }
2113
2114
2115
2116
2117 if (hw_fib_p >= &hw_fib_pool[num]) {
2118 pr_warn("aifd: didn't allocate NewFib\n");
2119 entry = entry->next;
2120 continue;
2121 }
2122
2123 hw_newfib = *hw_fib_p;
2124 *(hw_fib_p++) = NULL;
2125 newfib = *fib_p;
2126 *(fib_p++) = NULL;
2127
2128
2129
2130 memcpy(hw_newfib, hw_fib, sizeof(struct hw_fib));
2131 memcpy(newfib, fib, sizeof(struct fib));
2132 newfib->hw_fib_va = hw_newfib;
2133
2134
2135
2136
2137 list_add_tail(&newfib->fiblink, &fibctx->fib_list);
2138 fibctx->count++;
2139
2140
2141
2142
2143 complete(&fibctx->completion);
2144
2145 entry = entry->next;
2146 }
2147
2148
2149
2150 *(__le32 *)hw_fib->data = cpu_to_le32(ST_OK);
2151 aac_fib_adapter_complete(fib, sizeof(u32));
2152 spin_unlock_irqrestore(&dev->fib_lock, flagv);
2153
2154}
2155
2156static void aac_process_events(struct aac_dev *dev)
2157{
2158 struct hw_fib *hw_fib;
2159 struct fib *fib;
2160 unsigned long flags;
2161 spinlock_t *t_lock;
2162
2163 t_lock = dev->queues->queue[HostNormCmdQueue].lock;
2164 spin_lock_irqsave(t_lock, flags);
2165
2166 while (!list_empty(&(dev->queues->queue[HostNormCmdQueue].cmdq))) {
2167 struct list_head *entry;
2168 struct aac_aifcmd *aifcmd;
2169 unsigned int num;
2170 struct hw_fib **hw_fib_pool, **hw_fib_p;
2171 struct fib **fib_pool, **fib_p;
2172
2173 set_current_state(TASK_RUNNING);
2174
2175 entry = dev->queues->queue[HostNormCmdQueue].cmdq.next;
2176 list_del(entry);
2177
2178 t_lock = dev->queues->queue[HostNormCmdQueue].lock;
2179 spin_unlock_irqrestore(t_lock, flags);
2180
2181 fib = list_entry(entry, struct fib, fiblink);
2182 hw_fib = fib->hw_fib_va;
2183 if (dev->sa_firmware) {
2184
2185 aac_handle_sa_aif(dev, fib);
2186 aac_fib_adapter_complete(fib, (u16)sizeof(u32));
2187 goto free_fib;
2188 }
2189
2190
2191
2192
2193
2194
2195 memset(fib, 0, sizeof(struct fib));
2196 fib->type = FSAFS_NTC_FIB_CONTEXT;
2197 fib->size = sizeof(struct fib);
2198 fib->hw_fib_va = hw_fib;
2199 fib->data = hw_fib->data;
2200 fib->dev = dev;
2201
2202
2203
2204
2205 aifcmd = (struct aac_aifcmd *) hw_fib->data;
2206 if (aifcmd->command == cpu_to_le32(AifCmdDriverNotify)) {
2207
2208 aac_handle_aif(dev, fib);
2209 *(__le32 *)hw_fib->data = cpu_to_le32(ST_OK);
2210 aac_fib_adapter_complete(fib, (u16)sizeof(u32));
2211 goto free_fib;
2212 }
2213
2214
2215
2216
2217
2218
2219 if (aifcmd->command == cpu_to_le32(AifCmdEventNotify)
2220 || aifcmd->command == cpu_to_le32(AifCmdJobProgress)) {
2221 aac_handle_aif(dev, fib);
2222 }
2223
2224
2225
2226
2227 num = get_fib_count(dev);
2228 if (!num)
2229 goto free_fib;
2230
2231 hw_fib_pool = kmalloc_array(num, sizeof(struct hw_fib *),
2232 GFP_KERNEL);
2233 if (!hw_fib_pool)
2234 goto free_fib;
2235
2236 fib_pool = kmalloc_array(num, sizeof(struct fib *), GFP_KERNEL);
2237 if (!fib_pool)
2238 goto free_hw_fib_pool;
2239
2240
2241
2242
2243
2244 num = fillup_pools(dev, hw_fib_pool, fib_pool, num);
2245 if (!num)
2246 goto free_mem;
2247
2248
2249
2250
2251
2252 wakeup_fibctx_threads(dev, hw_fib_pool, fib_pool,
2253 fib, hw_fib, num);
2254
2255free_mem:
2256
2257 hw_fib_p = hw_fib_pool;
2258 fib_p = fib_pool;
2259 while (hw_fib_p < &hw_fib_pool[num]) {
2260 kfree(*hw_fib_p);
2261 kfree(*fib_p);
2262 ++fib_p;
2263 ++hw_fib_p;
2264 }
2265 kfree(fib_pool);
2266free_hw_fib_pool:
2267 kfree(hw_fib_pool);
2268free_fib:
2269 kfree(fib);
2270 t_lock = dev->queues->queue[HostNormCmdQueue].lock;
2271 spin_lock_irqsave(t_lock, flags);
2272 }
2273
2274
2275
2276 t_lock = dev->queues->queue[HostNormCmdQueue].lock;
2277 spin_unlock_irqrestore(t_lock, flags);
2278}
2279
2280static int aac_send_wellness_command(struct aac_dev *dev, char *wellness_str,
2281 u32 datasize)
2282{
2283 struct aac_srb *srbcmd;
2284 struct sgmap64 *sg64;
2285 dma_addr_t addr;
2286 char *dma_buf;
2287 struct fib *fibptr;
2288 int ret = -ENOMEM;
2289 u32 vbus, vid;
2290
2291 fibptr = aac_fib_alloc(dev);
2292 if (!fibptr)
2293 goto out;
2294
2295 dma_buf = dma_alloc_coherent(&dev->pdev->dev, datasize, &addr,
2296 GFP_KERNEL);
2297 if (!dma_buf)
2298 goto fib_free_out;
2299
2300 aac_fib_init(fibptr);
2301
2302 vbus = (u32)le16_to_cpu(dev->supplement_adapter_info.virt_device_bus);
2303 vid = (u32)le16_to_cpu(dev->supplement_adapter_info.virt_device_target);
2304
2305 srbcmd = (struct aac_srb *)fib_data(fibptr);
2306
2307 srbcmd->function = cpu_to_le32(SRBF_ExecuteScsi);
2308 srbcmd->channel = cpu_to_le32(vbus);
2309 srbcmd->id = cpu_to_le32(vid);
2310 srbcmd->lun = 0;
2311 srbcmd->flags = cpu_to_le32(SRB_DataOut);
2312 srbcmd->timeout = cpu_to_le32(10);
2313 srbcmd->retry_limit = 0;
2314 srbcmd->cdb_size = cpu_to_le32(12);
2315 srbcmd->count = cpu_to_le32(datasize);
2316
2317 memset(srbcmd->cdb, 0, sizeof(srbcmd->cdb));
2318 srbcmd->cdb[0] = BMIC_OUT;
2319 srbcmd->cdb[6] = WRITE_HOST_WELLNESS;
2320 memcpy(dma_buf, (char *)wellness_str, datasize);
2321
2322 sg64 = (struct sgmap64 *)&srbcmd->sg;
2323 sg64->count = cpu_to_le32(1);
2324 sg64->sg[0].addr[1] = cpu_to_le32((u32)(((addr) >> 16) >> 16));
2325 sg64->sg[0].addr[0] = cpu_to_le32((u32)(addr & 0xffffffff));
2326 sg64->sg[0].count = cpu_to_le32(datasize);
2327
2328 ret = aac_fib_send(ScsiPortCommand64, fibptr, sizeof(struct aac_srb),
2329 FsaNormal, 1, 1, NULL, NULL);
2330
2331 dma_free_coherent(&dev->pdev->dev, datasize, dma_buf, addr);
2332
2333
2334
2335
2336
2337 if (ret >= 0)
2338 aac_fib_complete(fibptr);
2339
2340
2341
2342
2343
2344 if (ret != -ERESTARTSYS)
2345 goto fib_free_out;
2346
2347out:
2348 return ret;
2349fib_free_out:
2350 aac_fib_free(fibptr);
2351 goto out;
2352}
2353
2354static int aac_send_safw_hostttime(struct aac_dev *dev, struct timespec64 *now)
2355{
2356 struct tm cur_tm;
2357 char wellness_str[] = "<HW>TD\010\0\0\0\0\0\0\0\0\0DW\0\0ZZ";
2358 u32 datasize = sizeof(wellness_str);
2359 time64_t local_time;
2360 int ret = -ENODEV;
2361
2362 if (!dev->sa_firmware)
2363 goto out;
2364
2365 local_time = (now->tv_sec - (sys_tz.tz_minuteswest * 60));
2366 time64_to_tm(local_time, 0, &cur_tm);
2367 cur_tm.tm_mon += 1;
2368 cur_tm.tm_year += 1900;
2369 wellness_str[8] = bin2bcd(cur_tm.tm_hour);
2370 wellness_str[9] = bin2bcd(cur_tm.tm_min);
2371 wellness_str[10] = bin2bcd(cur_tm.tm_sec);
2372 wellness_str[12] = bin2bcd(cur_tm.tm_mon);
2373 wellness_str[13] = bin2bcd(cur_tm.tm_mday);
2374 wellness_str[14] = bin2bcd(cur_tm.tm_year / 100);
2375 wellness_str[15] = bin2bcd(cur_tm.tm_year % 100);
2376
2377 ret = aac_send_wellness_command(dev, wellness_str, datasize);
2378
2379out:
2380 return ret;
2381}
2382
2383static int aac_send_hosttime(struct aac_dev *dev, struct timespec64 *now)
2384{
2385 int ret = -ENOMEM;
2386 struct fib *fibptr;
2387 __le32 *info;
2388
2389 fibptr = aac_fib_alloc(dev);
2390 if (!fibptr)
2391 goto out;
2392
2393 aac_fib_init(fibptr);
2394 info = (__le32 *)fib_data(fibptr);
2395 *info = cpu_to_le32(now->tv_sec);
2396 ret = aac_fib_send(SendHostTime, fibptr, sizeof(*info), FsaNormal,
2397 1, 1, NULL, NULL);
2398
2399
2400
2401
2402
2403 if (ret >= 0)
2404 aac_fib_complete(fibptr);
2405
2406
2407
2408
2409
2410 if (ret != -ERESTARTSYS)
2411 aac_fib_free(fibptr);
2412
2413out:
2414 return ret;
2415}
2416
2417
2418
2419
2420
2421
2422
2423
2424
2425
2426
2427int aac_command_thread(void *data)
2428{
2429 struct aac_dev *dev = data;
2430 DECLARE_WAITQUEUE(wait, current);
2431 unsigned long next_jiffies = jiffies + HZ;
2432 unsigned long next_check_jiffies = next_jiffies;
2433 long difference = HZ;
2434
2435
2436
2437
2438 if (dev->aif_thread)
2439 return -EINVAL;
2440
2441
2442
2443
2444 dev->aif_thread = 1;
2445 add_wait_queue(&dev->queues->queue[HostNormCmdQueue].cmdready, &wait);
2446 set_current_state(TASK_INTERRUPTIBLE);
2447 dprintk ((KERN_INFO "aac_command_thread start\n"));
2448 while (1) {
2449
2450 aac_process_events(dev);
2451
2452
2453
2454
2455 if ((time_before(next_check_jiffies,next_jiffies))
2456 && ((difference = next_check_jiffies - jiffies) <= 0)) {
2457 next_check_jiffies = next_jiffies;
2458 if (aac_adapter_check_health(dev) == 0) {
2459 difference = ((long)(unsigned)check_interval)
2460 * HZ;
2461 next_check_jiffies = jiffies + difference;
2462 } else if (!dev->queues)
2463 break;
2464 }
2465 if (!time_before(next_check_jiffies,next_jiffies)
2466 && ((difference = next_jiffies - jiffies) <= 0)) {
2467 struct timespec64 now;
2468 int ret;
2469
2470
2471 ret = aac_adapter_check_health(dev);
2472 if (ret || !dev->queues)
2473 break;
2474 next_check_jiffies = jiffies
2475 + ((long)(unsigned)check_interval)
2476 * HZ;
2477 ktime_get_real_ts64(&now);
2478
2479
2480 if (((NSEC_PER_SEC - (NSEC_PER_SEC / HZ)) > now.tv_nsec)
2481 && (now.tv_nsec > (NSEC_PER_SEC / HZ)))
2482 difference = HZ + HZ / 2 -
2483 now.tv_nsec / (NSEC_PER_SEC / HZ);
2484 else {
2485 if (now.tv_nsec > NSEC_PER_SEC / 2)
2486 ++now.tv_sec;
2487
2488 if (dev->sa_firmware)
2489 ret =
2490 aac_send_safw_hostttime(dev, &now);
2491 else
2492 ret = aac_send_hosttime(dev, &now);
2493
2494 difference = (long)(unsigned)update_interval*HZ;
2495 }
2496 next_jiffies = jiffies + difference;
2497 if (time_before(next_check_jiffies,next_jiffies))
2498 difference = next_check_jiffies - jiffies;
2499 }
2500 if (difference <= 0)
2501 difference = 1;
2502 set_current_state(TASK_INTERRUPTIBLE);
2503
2504 if (kthread_should_stop())
2505 break;
2506
2507
2508
2509
2510
2511 schedule_timeout(difference);
2512
2513 if (kthread_should_stop())
2514 break;
2515 }
2516 if (dev->queues)
2517 remove_wait_queue(&dev->queues->queue[HostNormCmdQueue].cmdready, &wait);
2518 dev->aif_thread = 0;
2519 return 0;
2520}
2521
2522int aac_acquire_irq(struct aac_dev *dev)
2523{
2524 int i;
2525 int j;
2526 int ret = 0;
2527
2528 if (!dev->sync_mode && dev->msi_enabled && dev->max_msix > 1) {
2529 for (i = 0; i < dev->max_msix; i++) {
2530 dev->aac_msix[i].vector_no = i;
2531 dev->aac_msix[i].dev = dev;
2532 if (request_irq(pci_irq_vector(dev->pdev, i),
2533 dev->a_ops.adapter_intr,
2534 0, "aacraid", &(dev->aac_msix[i]))) {
2535 printk(KERN_ERR "%s%d: Failed to register IRQ for vector %d.\n",
2536 dev->name, dev->id, i);
2537 for (j = 0 ; j < i ; j++)
2538 free_irq(pci_irq_vector(dev->pdev, j),
2539 &(dev->aac_msix[j]));
2540 pci_disable_msix(dev->pdev);
2541 ret = -1;
2542 }
2543 }
2544 } else {
2545 dev->aac_msix[0].vector_no = 0;
2546 dev->aac_msix[0].dev = dev;
2547
2548 if (request_irq(dev->pdev->irq, dev->a_ops.adapter_intr,
2549 IRQF_SHARED, "aacraid",
2550 &(dev->aac_msix[0])) < 0) {
2551 if (dev->msi)
2552 pci_disable_msi(dev->pdev);
2553 printk(KERN_ERR "%s%d: Interrupt unavailable.\n",
2554 dev->name, dev->id);
2555 ret = -1;
2556 }
2557 }
2558 return ret;
2559}
2560
2561void aac_free_irq(struct aac_dev *dev)
2562{
2563 int i;
2564
2565 if (aac_is_src(dev)) {
2566 if (dev->max_msix > 1) {
2567 for (i = 0; i < dev->max_msix; i++)
2568 free_irq(pci_irq_vector(dev->pdev, i),
2569 &(dev->aac_msix[i]));
2570 } else {
2571 free_irq(dev->pdev->irq, &(dev->aac_msix[0]));
2572 }
2573 } else {
2574 free_irq(dev->pdev->irq, dev);
2575 }
2576 if (dev->msi)
2577 pci_disable_msi(dev->pdev);
2578 else if (dev->max_msix > 1)
2579 pci_disable_msix(dev->pdev);
2580}
2581