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