Linux Kernel  3.7.1
 All Data Structures Namespaces Files Functions Variables Typedefs Enumerations Enumerator Macros Groups Pages
commsup.c
Go to the documentation of this file.
1 /*
2  * Adaptec AAC series RAID controller driver
3  * (c) Copyright 2001 Red Hat Inc.
4  *
5  * based on the old aacraid driver that is..
6  * Adaptec aacraid device driver for Linux.
7  *
8  * Copyright (c) 2000-2010 Adaptec, Inc.
9  * 2010 PMC-Sierra, Inc. ([email protected])
10  *
11  * This program is free software; you can redistribute it and/or modify
12  * it under the terms of the GNU General Public License as published by
13  * the Free Software Foundation; either version 2, or (at your option)
14  * any later version.
15  *
16  * This program is distributed in the hope that it will be useful,
17  * but WITHOUT ANY WARRANTY; without even the implied warranty of
18  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
19  * GNU General Public License for more details.
20  *
21  * You should have received a copy of the GNU General Public License
22  * along with this program; see the file COPYING. If not, write to
23  * the Free Software Foundation, 675 Mass Ave, Cambridge, MA 02139, USA.
24  *
25  * Module Name:
26  * commsup.c
27  *
28  * Abstract: Contain all routines that are required for FSA host/adapter
29  * communication.
30  *
31  */
32 
33 #include <linux/kernel.h>
34 #include <linux/init.h>
35 #include <linux/types.h>
36 #include <linux/sched.h>
37 #include <linux/pci.h>
38 #include <linux/spinlock.h>
39 #include <linux/slab.h>
40 #include <linux/completion.h>
41 #include <linux/blkdev.h>
42 #include <linux/delay.h>
43 #include <linux/kthread.h>
44 #include <linux/interrupt.h>
45 #include <linux/semaphore.h>
46 #include <scsi/scsi.h>
47 #include <scsi/scsi_host.h>
48 #include <scsi/scsi_device.h>
49 #include <scsi/scsi_cmnd.h>
50 
51 #include "aacraid.h"
52 
61 static int fib_map_alloc(struct aac_dev *dev)
62 {
64  "allocate hardware fibs pci_alloc_consistent(%p, %d * (%d + %d), %p)\n",
65  dev->pdev, dev->max_fib_size, dev->scsi_host_ptr->can_queue,
66  AAC_NUM_MGT_FIB, &dev->hw_fib_pa));
68  (dev->max_fib_size + sizeof(struct aac_fib_xporthdr))
69  * (dev->scsi_host_ptr->can_queue + AAC_NUM_MGT_FIB) + (ALIGN32 - 1),
70  &dev->hw_fib_pa);
71  if (dev->hw_fib_va == NULL)
72  return -ENOMEM;
73  return 0;
74 }
75 
84 void aac_fib_map_free(struct aac_dev *dev)
85 {
87  dev->max_fib_size * (dev->scsi_host_ptr->can_queue + AAC_NUM_MGT_FIB),
88  dev->hw_fib_va, dev->hw_fib_pa);
89  dev->hw_fib_va = NULL;
90  dev->hw_fib_pa = 0;
91 }
92 
101 int aac_fib_setup(struct aac_dev * dev)
102 {
103  struct fib *fibptr;
104  struct hw_fib *hw_fib;
105  dma_addr_t hw_fib_pa;
106  int i;
107 
108  while (((i = fib_map_alloc(dev)) == -ENOMEM)
109  && (dev->scsi_host_ptr->can_queue > (64 - AAC_NUM_MGT_FIB))) {
110  dev->init->MaxIoCommands = cpu_to_le32((dev->scsi_host_ptr->can_queue + AAC_NUM_MGT_FIB) >> 1);
111  dev->scsi_host_ptr->can_queue = le32_to_cpu(dev->init->MaxIoCommands) - AAC_NUM_MGT_FIB;
112  }
113  if (i<0)
114  return -ENOMEM;
115 
116  /* 32 byte alignment for PMC */
117  hw_fib_pa = (dev->hw_fib_pa + (ALIGN32 - 1)) & ~(ALIGN32 - 1);
118  dev->hw_fib_va = (struct hw_fib *)((unsigned char *)dev->hw_fib_va +
119  (hw_fib_pa - dev->hw_fib_pa));
120  dev->hw_fib_pa = hw_fib_pa;
121  memset(dev->hw_fib_va, 0,
122  (dev->max_fib_size + sizeof(struct aac_fib_xporthdr)) *
123  (dev->scsi_host_ptr->can_queue + AAC_NUM_MGT_FIB));
124 
125  /* add Xport header */
126  dev->hw_fib_va = (struct hw_fib *)((unsigned char *)dev->hw_fib_va +
127  sizeof(struct aac_fib_xporthdr));
128  dev->hw_fib_pa += sizeof(struct aac_fib_xporthdr);
129 
130  hw_fib = dev->hw_fib_va;
131  hw_fib_pa = dev->hw_fib_pa;
132  /*
133  * Initialise the fibs
134  */
135  for (i = 0, fibptr = &dev->fibs[i];
136  i < (dev->scsi_host_ptr->can_queue + AAC_NUM_MGT_FIB);
137  i++, fibptr++)
138  {
139  fibptr->flags = 0;
140  fibptr->dev = dev;
141  fibptr->hw_fib_va = hw_fib;
142  fibptr->data = (void *) fibptr->hw_fib_va->data;
143  fibptr->next = fibptr+1; /* Forward chain the fibs */
144  sema_init(&fibptr->event_wait, 0);
145  spin_lock_init(&fibptr->event_lock);
146  hw_fib->header.XferState = cpu_to_le32(0xffffffff);
147  hw_fib->header.SenderSize = cpu_to_le16(dev->max_fib_size);
148  fibptr->hw_fib_pa = hw_fib_pa;
149  hw_fib = (struct hw_fib *)((unsigned char *)hw_fib +
150  dev->max_fib_size + sizeof(struct aac_fib_xporthdr));
151  hw_fib_pa = hw_fib_pa +
152  dev->max_fib_size + sizeof(struct aac_fib_xporthdr);
153  }
154  /*
155  * Add the fib chain to the free list
156  */
157  dev->fibs[dev->scsi_host_ptr->can_queue + AAC_NUM_MGT_FIB - 1].next = NULL;
158  /*
159  * Enable this to debug out of queue space
160  */
161  dev->free_fib = &dev->fibs[0];
162  return 0;
163 }
164 
173 struct fib *aac_fib_alloc(struct aac_dev *dev)
174 {
175  struct fib * fibptr;
176  unsigned long flags;
177  spin_lock_irqsave(&dev->fib_lock, flags);
178  fibptr = dev->free_fib;
179  if(!fibptr){
180  spin_unlock_irqrestore(&dev->fib_lock, flags);
181  return fibptr;
182  }
183  dev->free_fib = fibptr->next;
184  spin_unlock_irqrestore(&dev->fib_lock, flags);
185  /*
186  * Set the proper node type code and node byte size
187  */
188  fibptr->type = FSAFS_NTC_FIB_CONTEXT;
189  fibptr->size = sizeof(struct fib);
190  /*
191  * Null out fields that depend on being zero at the start of
192  * each I/O
193  */
194  fibptr->hw_fib_va->header.XferState = 0;
195  fibptr->flags = 0;
196  fibptr->callback = NULL;
197  fibptr->callback_data = NULL;
198 
199  return fibptr;
200 }
201 
209 void aac_fib_free(struct fib *fibptr)
210 {
211  unsigned long flags, flagsv;
212 
213  spin_lock_irqsave(&fibptr->event_lock, flagsv);
214  if (fibptr->done == 2) {
215  spin_unlock_irqrestore(&fibptr->event_lock, flagsv);
216  return;
217  }
218  spin_unlock_irqrestore(&fibptr->event_lock, flagsv);
219 
220  spin_lock_irqsave(&fibptr->dev->fib_lock, flags);
222  aac_config.fib_timeouts++;
223  if (fibptr->hw_fib_va->header.XferState != 0) {
224  printk(KERN_WARNING "aac_fib_free, XferState != 0, fibptr = 0x%p, XferState = 0x%x\n",
225  (void*)fibptr,
226  le32_to_cpu(fibptr->hw_fib_va->header.XferState));
227  }
228  fibptr->next = fibptr->dev->free_fib;
229  fibptr->dev->free_fib = fibptr;
230  spin_unlock_irqrestore(&fibptr->dev->fib_lock, flags);
231 }
232 
240 void aac_fib_init(struct fib *fibptr)
241 {
242  struct hw_fib *hw_fib = fibptr->hw_fib_va;
243 
244  memset(&hw_fib->header, 0, sizeof(struct aac_fibhdr));
245  hw_fib->header.StructType = FIB_MAGIC;
246  hw_fib->header.Size = cpu_to_le16(fibptr->dev->max_fib_size);
248  hw_fib->header.u.ReceiverFibAddress = cpu_to_le32(fibptr->hw_fib_pa);
249  hw_fib->header.SenderSize = cpu_to_le16(fibptr->dev->max_fib_size);
250 }
251 
260 static void fib_dealloc(struct fib * fibptr)
261 {
262  struct hw_fib *hw_fib = fibptr->hw_fib_va;
263  hw_fib->header.XferState = 0;
264 }
265 
266 /*
267  * Commuication primitives define and support the queuing method we use to
268  * support host to adapter commuication. All queue accesses happen through
269  * these routines and are the only routines which have a knowledge of the
270  * how these queues are implemented.
271  */
272 
286 static int aac_get_entry (struct aac_dev * dev, u32 qid, struct aac_entry **entry, u32 * index, unsigned long *nonotify)
287 {
288  struct aac_queue * q;
289  unsigned long idx;
290 
291  /*
292  * All of the queues wrap when they reach the end, so we check
293  * to see if they have reached the end and if they have we just
294  * set the index back to zero. This is a wrap. You could or off
295  * the high bits in all updates but this is a bit faster I think.
296  */
297 
298  q = &dev->queues->queue[qid];
299 
300  idx = *index = le32_to_cpu(*(q->headers.producer));
301  /* Interrupt Moderation, only interrupt for first two entries */
302  if (idx != le32_to_cpu(*(q->headers.consumer))) {
303  if (--idx == 0) {
304  if (qid == AdapNormCmdQueue)
305  idx = ADAP_NORM_CMD_ENTRIES;
306  else
308  }
309  if (idx != le32_to_cpu(*(q->headers.consumer)))
310  *nonotify = 1;
311  }
312 
313  if (qid == AdapNormCmdQueue) {
314  if (*index >= ADAP_NORM_CMD_ENTRIES)
315  *index = 0; /* Wrap to front of the Producer Queue. */
316  } else {
317  if (*index >= ADAP_NORM_RESP_ENTRIES)
318  *index = 0; /* Wrap to front of the Producer Queue. */
319  }
320 
321  /* Queue is full */
322  if ((*index + 1) == le32_to_cpu(*(q->headers.consumer))) {
323  printk(KERN_WARNING "Queue %d full, %u outstanding.\n",
324  qid, q->numpending);
325  return 0;
326  } else {
327  *entry = q->base + *index;
328  return 1;
329  }
330 }
331 
348 int aac_queue_get(struct aac_dev * dev, u32 * index, u32 qid, struct hw_fib * hw_fib, int wait, struct fib * fibptr, unsigned long *nonotify)
349 {
350  struct aac_entry * entry = NULL;
351  int map = 0;
352 
353  if (qid == AdapNormCmdQueue) {
354  /* if no entries wait for some if caller wants to */
355  while (!aac_get_entry(dev, qid, &entry, index, nonotify)) {
356  printk(KERN_ERR "GetEntries failed\n");
357  }
358  /*
359  * Setup queue entry with a command, status and fib mapped
360  */
361  entry->size = cpu_to_le32(le16_to_cpu(hw_fib->header.Size));
362  map = 1;
363  } else {
364  while (!aac_get_entry(dev, qid, &entry, index, nonotify)) {
365  /* if no entries wait for some if caller wants to */
366  }
367  /*
368  * Setup queue entry with command, status and fib mapped
369  */
370  entry->size = cpu_to_le32(le16_to_cpu(hw_fib->header.Size));
371  entry->addr = hw_fib->header.SenderFibAddress;
372  /* Restore adapters pointer to the FIB */
373  hw_fib->header.u.ReceiverFibAddress = hw_fib->header.SenderFibAddress; /* Let the adapter now where to find its data */
374  map = 0;
375  }
376  /*
377  * If MapFib is true than we need to map the Fib and put pointers
378  * in the queue entry.
379  */
380  if (map)
381  entry->addr = cpu_to_le32(fibptr->hw_fib_pa);
382  return 0;
383 }
384 
385 /*
386  * Define the highest level of host to adapter communication routines.
387  * These routines will support host to adapter FS commuication. These
388  * routines have no knowledge of the commuication method used. This level
389  * sends and receives FIBs. This level has no knowledge of how these FIBs
390  * get passed back and forth.
391  */
392 
410 int aac_fib_send(u16 command, struct fib *fibptr, unsigned long size,
411  int priority, int wait, int reply, fib_callback callback,
412  void *callback_data)
413 {
414  struct aac_dev * dev = fibptr->dev;
415  struct hw_fib * hw_fib = fibptr->hw_fib_va;
416  unsigned long flags = 0;
417  unsigned long qflags;
418  unsigned long mflags = 0;
419  unsigned long sflags = 0;
420 
421 
422  if (!(hw_fib->header.XferState & cpu_to_le32(HostOwned)))
423  return -EBUSY;
424  /*
425  * There are 5 cases with the wait and response requested flags.
426  * The only invalid cases are if the caller requests to wait and
427  * does not request a response and if the caller does not want a
428  * response and the Fib is not allocated from pool. If a response
429  * is not requesed the Fib will just be deallocaed by the DPC
430  * routine when the response comes back from the adapter. No
431  * further processing will be done besides deleting the Fib. We
432  * will have a debug mode where the adapter can notify the host
433  * it had a problem and the host can log that fact.
434  */
435  fibptr->flags = 0;
436  if (wait && !reply) {
437  return -EINVAL;
438  } else if (!wait && reply) {
439  hw_fib->header.XferState |= cpu_to_le32(Async | ResponseExpected);
441  } else if (!wait && !reply) {
442  hw_fib->header.XferState |= cpu_to_le32(NoResponseExpected);
443  FIB_COUNTER_INCREMENT(aac_config.NoResponseSent);
444  } else if (wait && reply) {
445  hw_fib->header.XferState |= cpu_to_le32(ResponseExpected);
446  FIB_COUNTER_INCREMENT(aac_config.NormalSent);
447  }
448  /*
449  * Map the fib into 32bits by using the fib number
450  */
451 
452  hw_fib->header.SenderFibAddress = cpu_to_le32(((u32)(fibptr - dev->fibs)) << 2);
453  hw_fib->header.Handle = (u32)(fibptr - dev->fibs) + 1;
454  /*
455  * Set FIB state to indicate where it came from and if we want a
456  * response from the adapter. Also load the command from the
457  * caller.
458  *
459  * Map the hw fib pointer as a 32bit value
460  */
461  hw_fib->header.Command = cpu_to_le16(command);
462  hw_fib->header.XferState |= cpu_to_le32(SentFromHost);
463  /*
464  * Set the size of the Fib we want to send to the adapter
465  */
466  hw_fib->header.Size = cpu_to_le16(sizeof(struct aac_fibhdr) + size);
467  if (le16_to_cpu(hw_fib->header.Size) > le16_to_cpu(hw_fib->header.SenderSize)) {
468  return -EMSGSIZE;
469  }
470  /*
471  * Get a queue entry connect the FIB to it and send an notify
472  * the adapter a command is ready.
473  */
474  hw_fib->header.XferState |= cpu_to_le32(NormalPriority);
475 
476  /*
477  * Fill in the Callback and CallbackContext if we are not
478  * going to wait.
479  */
480  if (!wait) {
481  fibptr->callback = callback;
482  fibptr->callback_data = callback_data;
483  fibptr->flags = FIB_CONTEXT_FLAG;
484  }
485 
486  fibptr->done = 0;
487 
489 
490  dprintk((KERN_DEBUG "Fib contents:.\n"));
491  dprintk((KERN_DEBUG " Command = %d.\n", le32_to_cpu(hw_fib->header.Command)));
492  dprintk((KERN_DEBUG " SubCommand = %d.\n", le32_to_cpu(((struct aac_query_mount *)fib_data(fibptr))->command)));
493  dprintk((KERN_DEBUG " XferState = %x.\n", le32_to_cpu(hw_fib->header.XferState)));
494  dprintk((KERN_DEBUG " hw_fib va being sent=%p\n",fibptr->hw_fib_va));
495  dprintk((KERN_DEBUG " hw_fib pa being sent=%lx\n",(ulong)fibptr->hw_fib_pa));
496  dprintk((KERN_DEBUG " fib being sent=%p\n",fibptr));
497 
498  if (!dev->queues)
499  return -EBUSY;
500 
501  if (wait) {
502 
503  spin_lock_irqsave(&dev->manage_lock, mflags);
505  printk(KERN_INFO "No management Fibs Available:%d\n",
506  dev->management_fib_count);
507  spin_unlock_irqrestore(&dev->manage_lock, mflags);
508  return -EBUSY;
509  }
510  dev->management_fib_count++;
511  spin_unlock_irqrestore(&dev->manage_lock, mflags);
512  spin_lock_irqsave(&fibptr->event_lock, flags);
513  }
514 
515  if (dev->sync_mode) {
516  if (wait)
517  spin_unlock_irqrestore(&fibptr->event_lock, flags);
518  spin_lock_irqsave(&dev->sync_lock, sflags);
519  if (dev->sync_fib) {
520  list_add_tail(&fibptr->fiblink, &dev->sync_fib_list);
521  spin_unlock_irqrestore(&dev->sync_lock, sflags);
522  } else {
523  dev->sync_fib = fibptr;
524  spin_unlock_irqrestore(&dev->sync_lock, sflags);
526  (u32)fibptr->hw_fib_pa, 0, 0, 0, 0, 0,
527  NULL, NULL, NULL, NULL, NULL);
528  }
529  if (wait) {
530  fibptr->flags |= FIB_CONTEXT_FLAG_WAIT;
531  if (down_interruptible(&fibptr->event_wait)) {
532  fibptr->flags &= ~FIB_CONTEXT_FLAG_WAIT;
533  return -EFAULT;
534  }
535  return 0;
536  }
537  return -EINPROGRESS;
538  }
539 
540  if (aac_adapter_deliver(fibptr) != 0) {
541  printk(KERN_ERR "aac_fib_send: returned -EBUSY\n");
542  if (wait) {
543  spin_unlock_irqrestore(&fibptr->event_lock, flags);
544  spin_lock_irqsave(&dev->manage_lock, mflags);
545  dev->management_fib_count--;
546  spin_unlock_irqrestore(&dev->manage_lock, mflags);
547  }
548  return -EBUSY;
549  }
550 
551 
552  /*
553  * If the caller wanted us to wait for response wait now.
554  */
555 
556  if (wait) {
557  spin_unlock_irqrestore(&fibptr->event_lock, flags);
558  /* Only set for first known interruptable command */
559  if (wait < 0) {
560  /*
561  * *VERY* Dangerous to time out a command, the
562  * assumption is made that we have no hope of
563  * functioning because an interrupt routing or other
564  * hardware failure has occurred.
565  */
566  unsigned long timeout = jiffies + (180 * HZ); /* 3 minutes */
567  while (down_trylock(&fibptr->event_wait)) {
568  int blink;
569  if (time_is_before_eq_jiffies(timeout)) {
570  struct aac_queue * q = &dev->queues->queue[AdapNormCmdQueue];
571  spin_lock_irqsave(q->lock, qflags);
572  q->numpending--;
573  spin_unlock_irqrestore(q->lock, qflags);
574  if (wait == -1) {
575  printk(KERN_ERR "aacraid: aac_fib_send: first asynchronous command timed out.\n"
576  "Usually a result of a PCI interrupt routing problem;\n"
577  "update mother board BIOS or consider utilizing one of\n"
578  "the SAFE mode kernel options (acpi, apic etc)\n");
579  }
580  return -ETIMEDOUT;
581  }
582  if ((blink = aac_adapter_check_health(dev)) > 0) {
583  if (wait == -1) {
584  printk(KERN_ERR "aacraid: aac_fib_send: adapter blinkLED 0x%x.\n"
585  "Usually a result of a serious unrecoverable hardware problem\n",
586  blink);
587  }
588  return -EFAULT;
589  }
590  /* We used to udelay() here but that absorbed
591  * a CPU when a timeout occured. Not very
592  * useful. */
593  cpu_relax();
594  }
595  } else if (down_interruptible(&fibptr->event_wait)) {
596  /* Do nothing ... satisfy
597  * down_interruptible must_check */
598  }
599 
600  spin_lock_irqsave(&fibptr->event_lock, flags);
601  if (fibptr->done == 0) {
602  fibptr->done = 2; /* Tell interrupt we aborted */
603  spin_unlock_irqrestore(&fibptr->event_lock, flags);
604  return -ERESTARTSYS;
605  }
606  spin_unlock_irqrestore(&fibptr->event_lock, flags);
607  BUG_ON(fibptr->done == 0);
608 
610  return -ETIMEDOUT;
611  return 0;
612  }
613  /*
614  * If the user does not want a response than return success otherwise
615  * return pending
616  */
617  if (reply)
618  return -EINPROGRESS;
619  else
620  return 0;
621 }
622 
634 int aac_consumer_get(struct aac_dev * dev, struct aac_queue * q, struct aac_entry **entry)
635 {
636  u32 index;
637  int status;
638  if (le32_to_cpu(*q->headers.producer) == le32_to_cpu(*q->headers.consumer)) {
639  status = 0;
640  } else {
641  /*
642  * The consumer index must be wrapped if we have reached
643  * the end of the queue, else we just use the entry
644  * pointed to by the header index
645  */
646  if (le32_to_cpu(*q->headers.consumer) >= q->entries)
647  index = 0;
648  else
649  index = le32_to_cpu(*q->headers.consumer);
650  *entry = q->base + index;
651  status = 1;
652  }
653  return(status);
654 }
655 
666 void aac_consumer_free(struct aac_dev * dev, struct aac_queue *q, u32 qid)
667 {
668  int wasfull = 0;
669  u32 notify;
670 
671  if ((le32_to_cpu(*q->headers.producer)+1) == le32_to_cpu(*q->headers.consumer))
672  wasfull = 1;
673 
674  if (le32_to_cpu(*q->headers.consumer) >= q->entries)
675  *q->headers.consumer = cpu_to_le32(1);
676  else
677  le32_add_cpu(q->headers.consumer, 1);
678 
679  if (wasfull) {
680  switch (qid) {
681 
682  case HostNormCmdQueue:
683  notify = HostNormCmdNotFull;
684  break;
685  case HostNormRespQueue:
686  notify = HostNormRespNotFull;
687  break;
688  default:
689  BUG();
690  return;
691  }
692  aac_adapter_notify(dev, notify);
693  }
694 }
695 
705 int aac_fib_adapter_complete(struct fib *fibptr, unsigned short size)
706 {
707  struct hw_fib * hw_fib = fibptr->hw_fib_va;
708  struct aac_dev * dev = fibptr->dev;
709  struct aac_queue * q;
710  unsigned long nointr = 0;
711  unsigned long qflags;
712 
715  kfree(hw_fib);
716  return 0;
717  }
718 
719  if (hw_fib->header.XferState == 0) {
720  if (dev->comm_interface == AAC_COMM_MESSAGE)
721  kfree(hw_fib);
722  return 0;
723  }
724  /*
725  * If we plan to do anything check the structure type first.
726  */
727  if (hw_fib->header.StructType != FIB_MAGIC &&
728  hw_fib->header.StructType != FIB_MAGIC2 &&
729  hw_fib->header.StructType != FIB_MAGIC2_64) {
730  if (dev->comm_interface == AAC_COMM_MESSAGE)
731  kfree(hw_fib);
732  return -EINVAL;
733  }
734  /*
735  * This block handles the case where the adapter had sent us a
736  * command and we have finished processing the command. We
737  * call completeFib when we are done processing the command
738  * and want to send a response back to the adapter. This will
739  * send the completed cdb to the adapter.
740  */
741  if (hw_fib->header.XferState & cpu_to_le32(SentFromAdapter)) {
742  if (dev->comm_interface == AAC_COMM_MESSAGE) {
743  kfree (hw_fib);
744  } else {
745  u32 index;
746  hw_fib->header.XferState |= cpu_to_le32(HostProcessed);
747  if (size) {
748  size += sizeof(struct aac_fibhdr);
749  if (size > le16_to_cpu(hw_fib->header.SenderSize))
750  return -EMSGSIZE;
751  hw_fib->header.Size = cpu_to_le16(size);
752  }
753  q = &dev->queues->queue[AdapNormRespQueue];
754  spin_lock_irqsave(q->lock, qflags);
755  aac_queue_get(dev, &index, AdapNormRespQueue, hw_fib, 1, NULL, &nointr);
756  *(q->headers.producer) = cpu_to_le32(index + 1);
757  spin_unlock_irqrestore(q->lock, qflags);
758  if (!(nointr & (int)aac_config.irq_mod))
760  }
761  } else {
762  printk(KERN_WARNING "aac_fib_adapter_complete: "
763  "Unknown xferstate detected.\n");
764  BUG();
765  }
766  return 0;
767 }
768 
776 int aac_fib_complete(struct fib *fibptr)
777 {
778  unsigned long flags;
779  struct hw_fib * hw_fib = fibptr->hw_fib_va;
780 
781  /*
782  * Check for a fib which has already been completed
783  */
784 
785  if (hw_fib->header.XferState == 0)
786  return 0;
787  /*
788  * If we plan to do anything check the structure type first.
789  */
790 
791  if (hw_fib->header.StructType != FIB_MAGIC &&
792  hw_fib->header.StructType != FIB_MAGIC2 &&
793  hw_fib->header.StructType != FIB_MAGIC2_64)
794  return -EINVAL;
795  /*
796  * This block completes a cdb which orginated on the host and we
797  * just need to deallocate the cdb or reinit it. At this point the
798  * command is complete that we had sent to the adapter and this
799  * cdb could be reused.
800  */
801  spin_lock_irqsave(&fibptr->event_lock, flags);
802  if (fibptr->done == 2) {
803  spin_unlock_irqrestore(&fibptr->event_lock, flags);
804  return 0;
805  }
806  spin_unlock_irqrestore(&fibptr->event_lock, flags);
807 
808  if((hw_fib->header.XferState & cpu_to_le32(SentFromHost)) &&
809  (hw_fib->header.XferState & cpu_to_le32(AdapterProcessed)))
810  {
811  fib_dealloc(fibptr);
812  }
813  else if(hw_fib->header.XferState & cpu_to_le32(SentFromHost))
814  {
815  /*
816  * This handles the case when the host has aborted the I/O
817  * to the adapter because the adapter is not responding
818  */
819  fib_dealloc(fibptr);
820  } else if(hw_fib->header.XferState & cpu_to_le32(HostOwned)) {
821  fib_dealloc(fibptr);
822  } else {
823  BUG();
824  }
825  return 0;
826 }
827 
837 void aac_printf(struct aac_dev *dev, u32 val)
838 {
839  char *cp = dev->printfbuf;
840  if (dev->printf_enabled)
841  {
842  int length = val & 0xffff;
843  int level = (val >> 16) & 0xffff;
844 
845  /*
846  * The size of the printfbuf is set in port.c
847  * There is no variable or define for it
848  */
849  if (length > 255)
850  length = 255;
851  if (cp[length] != 0)
852  cp[length] = 0;
853  if (level == LOG_AAC_HIGH_ERROR)
854  printk(KERN_WARNING "%s:%s", dev->name, cp);
855  else
856  printk(KERN_INFO "%s:%s", dev->name, cp);
857  }
858  memset(cp, 0, 256);
859 }
860 
861 
871 #define AIF_SNIFF_TIMEOUT (30*HZ)
872 static void aac_handle_aif(struct aac_dev * dev, struct fib * fibptr)
873 {
874  struct hw_fib * hw_fib = fibptr->hw_fib_va;
875  struct aac_aifcmd * aifcmd = (struct aac_aifcmd *)hw_fib->data;
876  u32 channel, id, lun, container;
877  struct scsi_device *device;
878  enum {
879  NOTHING,
880  DELETE,
881  ADD,
882  CHANGE
883  } device_config_needed = NOTHING;
884 
885  /* Sniff for container changes */
886 
887  if (!dev || !dev->fsa_dev)
888  return;
889  container = channel = id = lun = (u32)-1;
890 
891  /*
892  * We have set this up to try and minimize the number of
893  * re-configures that take place. As a result of this when
894  * certain AIF's come in we will set a flag waiting for another
895  * type of AIF before setting the re-config flag.
896  */
897  switch (le32_to_cpu(aifcmd->command)) {
898  case AifCmdDriverNotify:
899  switch (le32_to_cpu(((__le32 *)aifcmd->data)[0])) {
900  /*
901  * Morph or Expand complete
902  */
903  case AifDenMorphComplete:
905  container = le32_to_cpu(((__le32 *)aifcmd->data)[1]);
906  if (container >= dev->maximum_num_containers)
907  break;
908 
909  /*
910  * Find the scsi_device associated with the SCSI
911  * address. Make sure we have the right array, and if
912  * so set the flag to initiate a new re-config once we
913  * see an AifEnConfigChange AIF come through.
914  */
915 
916  if ((dev != NULL) && (dev->scsi_host_ptr != NULL)) {
918  CONTAINER_TO_CHANNEL(container),
919  CONTAINER_TO_ID(container),
920  CONTAINER_TO_LUN(container));
921  if (device) {
922  dev->fsa_dev[container].config_needed = CHANGE;
923  dev->fsa_dev[container].config_waiting_on = AifEnConfigChange;
924  dev->fsa_dev[container].config_waiting_stamp = jiffies;
926  }
927  }
928  }
929 
930  /*
931  * If we are waiting on something and this happens to be
932  * that thing then set the re-configure flag.
933  */
934  if (container != (u32)-1) {
935  if (container >= dev->maximum_num_containers)
936  break;
937  if ((dev->fsa_dev[container].config_waiting_on ==
938  le32_to_cpu(*(__le32 *)aifcmd->data)) &&
939  time_before(jiffies, dev->fsa_dev[container].config_waiting_stamp + AIF_SNIFF_TIMEOUT))
940  dev->fsa_dev[container].config_waiting_on = 0;
941  } else for (container = 0;
942  container < dev->maximum_num_containers; ++container) {
943  if ((dev->fsa_dev[container].config_waiting_on ==
944  le32_to_cpu(*(__le32 *)aifcmd->data)) &&
945  time_before(jiffies, dev->fsa_dev[container].config_waiting_stamp + AIF_SNIFF_TIMEOUT))
946  dev->fsa_dev[container].config_waiting_on = 0;
947  }
948  break;
949 
950  case AifCmdEventNotify:
951  switch (le32_to_cpu(((__le32 *)aifcmd->data)[0])) {
952  case AifEnBatteryEvent:
953  dev->cache_protected =
954  (((__le32 *)aifcmd->data)[1] == cpu_to_le32(3));
955  break;
956  /*
957  * Add an Array.
958  */
959  case AifEnAddContainer:
960  container = le32_to_cpu(((__le32 *)aifcmd->data)[1]);
961  if (container >= dev->maximum_num_containers)
962  break;
963  dev->fsa_dev[container].config_needed = ADD;
964  dev->fsa_dev[container].config_waiting_on =
966  dev->fsa_dev[container].config_waiting_stamp = jiffies;
967  break;
968 
969  /*
970  * Delete an Array.
971  */
973  container = le32_to_cpu(((__le32 *)aifcmd->data)[1]);
974  if (container >= dev->maximum_num_containers)
975  break;
976  dev->fsa_dev[container].config_needed = DELETE;
977  dev->fsa_dev[container].config_waiting_on =
979  dev->fsa_dev[container].config_waiting_stamp = jiffies;
980  break;
981 
982  /*
983  * Container change detected. If we currently are not
984  * waiting on something else, setup to wait on a Config Change.
985  */
987  container = le32_to_cpu(((__le32 *)aifcmd->data)[1]);
988  if (container >= dev->maximum_num_containers)
989  break;
990  if (dev->fsa_dev[container].config_waiting_on &&
991  time_before(jiffies, dev->fsa_dev[container].config_waiting_stamp + AIF_SNIFF_TIMEOUT))
992  break;
993  dev->fsa_dev[container].config_needed = CHANGE;
994  dev->fsa_dev[container].config_waiting_on =
996  dev->fsa_dev[container].config_waiting_stamp = jiffies;
997  break;
998 
999  case AifEnConfigChange:
1000  break;
1001 
1002  case AifEnAddJBOD:
1003  case AifEnDeleteJBOD:
1004  container = le32_to_cpu(((__le32 *)aifcmd->data)[1]);
1005  if ((container >> 28)) {
1006  container = (u32)-1;
1007  break;
1008  }
1009  channel = (container >> 24) & 0xF;
1010  if (channel >= dev->maximum_num_channels) {
1011  container = (u32)-1;
1012  break;
1013  }
1014  id = container & 0xFFFF;
1015  if (id >= dev->maximum_num_physicals) {
1016  container = (u32)-1;
1017  break;
1018  }
1019  lun = (container >> 16) & 0xFF;
1020  container = (u32)-1;
1022  device_config_needed =
1023  (((__le32 *)aifcmd->data)[0] ==
1025  if (device_config_needed == ADD) {
1027  channel,
1028  id,
1029  lun);
1030  if (device) {
1033  }
1034  }
1035  break;
1036 
1038  /*
1039  * If in JBOD mode, automatic exposure of new
1040  * physical target to be suppressed until configured.
1041  */
1042  if (dev->jbod)
1043  break;
1044  switch (le32_to_cpu(((__le32 *)aifcmd->data)[3])) {
1045  case EM_DRIVE_INSERTION:
1046  case EM_DRIVE_REMOVAL:
1047  container = le32_to_cpu(
1048  ((__le32 *)aifcmd->data)[2]);
1049  if ((container >> 28)) {
1050  container = (u32)-1;
1051  break;
1052  }
1053  channel = (container >> 24) & 0xF;
1054  if (channel >= dev->maximum_num_channels) {
1055  container = (u32)-1;
1056  break;
1057  }
1058  id = container & 0xFFFF;
1059  lun = (container >> 16) & 0xFF;
1060  container = (u32)-1;
1061  if (id >= dev->maximum_num_physicals) {
1062  /* legacy dev_t ? */
1063  if ((0x2000 <= id) || lun || channel ||
1064  ((channel = (id >> 7) & 0x3F) >=
1065  dev->maximum_num_channels))
1066  break;
1067  lun = (id >> 4) & 7;
1068  id &= 0xF;
1069  }
1071  device_config_needed =
1072  (((__le32 *)aifcmd->data)[3]
1074  ADD : DELETE;
1075  break;
1076  }
1077  break;
1078  }
1079 
1080  /*
1081  * If we are waiting on something and this happens to be
1082  * that thing then set the re-configure flag.
1083  */
1084  if (container != (u32)-1) {
1085  if (container >= dev->maximum_num_containers)
1086  break;
1087  if ((dev->fsa_dev[container].config_waiting_on ==
1088  le32_to_cpu(*(__le32 *)aifcmd->data)) &&
1089  time_before(jiffies, dev->fsa_dev[container].config_waiting_stamp + AIF_SNIFF_TIMEOUT))
1090  dev->fsa_dev[container].config_waiting_on = 0;
1091  } else for (container = 0;
1092  container < dev->maximum_num_containers; ++container) {
1093  if ((dev->fsa_dev[container].config_waiting_on ==
1094  le32_to_cpu(*(__le32 *)aifcmd->data)) &&
1095  time_before(jiffies, dev->fsa_dev[container].config_waiting_stamp + AIF_SNIFF_TIMEOUT))
1096  dev->fsa_dev[container].config_waiting_on = 0;
1097  }
1098  break;
1099 
1100  case AifCmdJobProgress:
1101  /*
1102  * These are job progress AIF's. When a Clear is being
1103  * done on a container it is initially created then hidden from
1104  * the OS. When the clear completes we don't get a config
1105  * change so we monitor the job status complete on a clear then
1106  * wait for a container change.
1107  */
1108 
1109  if (((__le32 *)aifcmd->data)[1] == cpu_to_le32(AifJobCtrZero) &&
1110  (((__le32 *)aifcmd->data)[6] == ((__le32 *)aifcmd->data)[5] ||
1111  ((__le32 *)aifcmd->data)[4] == cpu_to_le32(AifJobStsSuccess))) {
1112  for (container = 0;
1113  container < dev->maximum_num_containers;
1114  ++container) {
1115  /*
1116  * Stomp on all config sequencing for all
1117  * containers?
1118  */
1119  dev->fsa_dev[container].config_waiting_on =
1121  dev->fsa_dev[container].config_needed = ADD;
1122  dev->fsa_dev[container].config_waiting_stamp =
1123  jiffies;
1124  }
1125  }
1126  if (((__le32 *)aifcmd->data)[1] == cpu_to_le32(AifJobCtrZero) &&
1127  ((__le32 *)aifcmd->data)[6] == 0 &&
1128  ((__le32 *)aifcmd->data)[4] == cpu_to_le32(AifJobStsRunning)) {
1129  for (container = 0;
1130  container < dev->maximum_num_containers;
1131  ++container) {
1132  /*
1133  * Stomp on all config sequencing for all
1134  * containers?
1135  */
1136  dev->fsa_dev[container].config_waiting_on =
1138  dev->fsa_dev[container].config_needed = DELETE;
1139  dev->fsa_dev[container].config_waiting_stamp =
1140  jiffies;
1141  }
1142  }
1143  break;
1144  }
1145 
1146  container = 0;
1147 retry_next:
1148  if (device_config_needed == NOTHING)
1149  for (; container < dev->maximum_num_containers; ++container) {
1150  if ((dev->fsa_dev[container].config_waiting_on == 0) &&
1151  (dev->fsa_dev[container].config_needed != NOTHING) &&
1152  time_before(jiffies, dev->fsa_dev[container].config_waiting_stamp + AIF_SNIFF_TIMEOUT)) {
1153  device_config_needed =
1154  dev->fsa_dev[container].config_needed;
1155  dev->fsa_dev[container].config_needed = NOTHING;
1156  channel = CONTAINER_TO_CHANNEL(container);
1157  id = CONTAINER_TO_ID(container);
1158  lun = CONTAINER_TO_LUN(container);
1159  break;
1160  }
1161  }
1162  if (device_config_needed == NOTHING)
1163  return;
1164 
1165  /*
1166  * If we decided that a re-configuration needs to be done,
1167  * schedule it here on the way out the door, please close the door
1168  * behind you.
1169  */
1170 
1171  /*
1172  * Find the scsi_device associated with the SCSI address,
1173  * and mark it as changed, invalidating the cache. This deals
1174  * with changes to existing device IDs.
1175  */
1176 
1177  if (!dev || !dev->scsi_host_ptr)
1178  return;
1179  /*
1180  * force reload of disk info via aac_probe_container
1181  */
1182  if ((channel == CONTAINER_CHANNEL) &&
1183  (device_config_needed != NOTHING)) {
1184  if (dev->fsa_dev[container].valid == 1)
1185  dev->fsa_dev[container].valid = 2;
1186  aac_probe_container(dev, container);
1187  }
1189  if (device) {
1190  switch (device_config_needed) {
1191  case DELETE:
1192 #if (defined(AAC_DEBUG_INSTRUMENT_AIF_DELETE))
1194 #else
1195  if (scsi_device_online(device)) {
1198  "Device offlined - %s\n",
1199  (channel == CONTAINER_CHANNEL) ?
1200  "array deleted" :
1201  "enclosure services event");
1202  }
1203 #endif
1204  break;
1205  case ADD:
1206  if (!scsi_device_online(device)) {
1208  "Device online - %s\n",
1209  (channel == CONTAINER_CHANNEL) ?
1210  "array created" :
1211  "enclosure services event");
1213  }
1214  /* FALLTHRU */
1215  case CHANGE:
1216  if ((channel == CONTAINER_CHANNEL)
1217  && (!dev->fsa_dev[container].valid)) {
1218 #if (defined(AAC_DEBUG_INSTRUMENT_AIF_DELETE))
1220 #else
1221  if (!scsi_device_online(device))
1222  break;
1225  "Device offlined - %s\n",
1226  "array failed");
1227 #endif
1228  break;
1229  }
1230  scsi_rescan_device(&device->sdev_gendev);
1231 
1232  default:
1233  break;
1234  }
1236  device_config_needed = NOTHING;
1237  }
1238  if (device_config_needed == ADD)
1240  if (channel == CONTAINER_CHANNEL) {
1241  container++;
1242  device_config_needed = NOTHING;
1243  goto retry_next;
1244  }
1245 }
1246 
1247 static int _aac_reset_adapter(struct aac_dev *aac, int forced)
1248 {
1249  int index, quirks;
1250  int retval;
1251  struct Scsi_Host *host;
1252  struct scsi_device *dev;
1253  struct scsi_cmnd *command;
1254  struct scsi_cmnd *command_list;
1255  int jafo = 0;
1256 
1257  /*
1258  * Assumptions:
1259  * - host is locked, unless called by the aacraid thread.
1260  * (a matter of convenience, due to legacy issues surrounding
1261  * eh_host_adapter_reset).
1262  * - in_reset is asserted, so no new i/o is getting to the
1263  * card.
1264  * - The card is dead, or will be very shortly ;-/ so no new
1265  * commands are completing in the interrupt service.
1266  */
1267  host = aac->scsi_host_ptr;
1268  scsi_block_requests(host);
1270  if (aac->thread->pid != current->pid) {
1271  spin_unlock_irq(host->host_lock);
1272  kthread_stop(aac->thread);
1273  jafo = 1;
1274  }
1275 
1276  /*
1277  * If a positive health, means in a known DEAD PANIC
1278  * state and the adapter could be reset to `try again'.
1279  */
1280  retval = aac_adapter_restart(aac, forced ? 0 : aac_adapter_check_health(aac));
1281 
1282  if (retval)
1283  goto out;
1284 
1285  /*
1286  * Loop through the fibs, close the synchronous FIBS
1287  */
1288  for (retval = 1, index = 0; index < (aac->scsi_host_ptr->can_queue + AAC_NUM_MGT_FIB); index++) {
1289  struct fib *fib = &aac->fibs[index];
1290  if (!(fib->hw_fib_va->header.XferState & cpu_to_le32(NoResponseExpected | Async)) &&
1291  (fib->hw_fib_va->header.XferState & cpu_to_le32(ResponseExpected))) {
1292  unsigned long flagv;
1293  spin_lock_irqsave(&fib->event_lock, flagv);
1294  up(&fib->event_wait);
1295  spin_unlock_irqrestore(&fib->event_lock, flagv);
1296  schedule();
1297  retval = 0;
1298  }
1299  }
1300  /* Give some extra time for ioctls to complete. */
1301  if (retval == 0)
1302  ssleep(2);
1303  index = aac->cardtype;
1304 
1305  /*
1306  * Re-initialize the adapter, first free resources, then carefully
1307  * apply the initialization sequence to come back again. Only risk
1308  * is a change in Firmware dropping cache, it is assumed the caller
1309  * will ensure that i/o is queisced and the card is flushed in that
1310  * case.
1311  */
1312  aac_fib_map_free(aac);
1313  pci_free_consistent(aac->pdev, aac->comm_size, aac->comm_addr, aac->comm_phys);
1314  aac->comm_addr = NULL;
1315  aac->comm_phys = 0;
1316  kfree(aac->queues);
1317  aac->queues = NULL;
1318  free_irq(aac->pdev->irq, aac);
1319  if (aac->msi)
1320  pci_disable_msi(aac->pdev);
1321  kfree(aac->fsa_dev);
1322  aac->fsa_dev = NULL;
1323  quirks = aac_get_driver_ident(index)->quirks;
1324  if (quirks & AAC_QUIRK_31BIT) {
1325  if (((retval = pci_set_dma_mask(aac->pdev, DMA_BIT_MASK(31)))) ||
1326  ((retval = pci_set_consistent_dma_mask(aac->pdev, DMA_BIT_MASK(31)))))
1327  goto out;
1328  } else {
1329  if (((retval = pci_set_dma_mask(aac->pdev, DMA_BIT_MASK(32)))) ||
1330  ((retval = pci_set_consistent_dma_mask(aac->pdev, DMA_BIT_MASK(32)))))
1331  goto out;
1332  }
1333  if ((retval = (*(aac_get_driver_ident(index)->init))(aac)))
1334  goto out;
1335  if (quirks & AAC_QUIRK_31BIT)
1336  if ((retval = pci_set_dma_mask(aac->pdev, DMA_BIT_MASK(32))))
1337  goto out;
1338  if (jafo) {
1339  aac->thread = kthread_run(aac_command_thread, aac, aac->name);
1340  if (IS_ERR(aac->thread)) {
1341  retval = PTR_ERR(aac->thread);
1342  goto out;
1343  }
1344  }
1345  (void)aac_get_adapter_info(aac);
1346  if ((quirks & AAC_QUIRK_34SG) && (host->sg_tablesize > 34)) {
1347  host->sg_tablesize = 34;
1348  host->max_sectors = (host->sg_tablesize * 8) + 112;
1349  }
1350  if ((quirks & AAC_QUIRK_17SG) && (host->sg_tablesize > 17)) {
1351  host->sg_tablesize = 17;
1352  host->max_sectors = (host->sg_tablesize * 8) + 112;
1353  }
1354  aac_get_config_status(aac, 1);
1355  aac_get_containers(aac);
1356  /*
1357  * This is where the assumption that the Adapter is quiesced
1358  * is important.
1359  */
1360  command_list = NULL;
1361  __shost_for_each_device(dev, host) {
1362  unsigned long flags;
1363  spin_lock_irqsave(&dev->list_lock, flags);
1364  list_for_each_entry(command, &dev->cmd_list, list)
1365  if (command->SCp.phase == AAC_OWNER_FIRMWARE) {
1366  command->SCp.buffer = (struct scatterlist *)command_list;
1367  command_list = command;
1368  }
1369  spin_unlock_irqrestore(&dev->list_lock, flags);
1370  }
1371  while ((command = command_list)) {
1372  command_list = (struct scsi_cmnd *)command->SCp.buffer;
1373  command->SCp.buffer = NULL;
1374  command->result = DID_OK << 16
1375  | COMMAND_COMPLETE << 8
1377  command->SCp.phase = AAC_OWNER_ERROR_HANDLER;
1378  command->scsi_done(command);
1379  }
1380  retval = 0;
1381 
1382 out:
1383  aac->in_reset = 0;
1384  scsi_unblock_requests(host);
1385  if (jafo) {
1386  spin_lock_irq(host->host_lock);
1387  }
1388  return retval;
1389 }
1390 
1391 int aac_reset_adapter(struct aac_dev * aac, int forced)
1392 {
1393  unsigned long flagv = 0;
1394  int retval;
1395  struct Scsi_Host * host;
1396 
1397  if (spin_trylock_irqsave(&aac->fib_lock, flagv) == 0)
1398  return -EBUSY;
1399 
1400  if (aac->in_reset) {
1401  spin_unlock_irqrestore(&aac->fib_lock, flagv);
1402  return -EBUSY;
1403  }
1404  aac->in_reset = 1;
1405  spin_unlock_irqrestore(&aac->fib_lock, flagv);
1406 
1407  /*
1408  * Wait for all commands to complete to this specific
1409  * target (block maximum 60 seconds). Although not necessary,
1410  * it does make us a good storage citizen.
1411  */
1412  host = aac->scsi_host_ptr;
1413  scsi_block_requests(host);
1414  if (forced < 2) for (retval = 60; retval; --retval) {
1415  struct scsi_device * dev;
1416  struct scsi_cmnd * command;
1417  int active = 0;
1418 
1419  __shost_for_each_device(dev, host) {
1420  spin_lock_irqsave(&dev->list_lock, flagv);
1421  list_for_each_entry(command, &dev->cmd_list, list) {
1422  if (command->SCp.phase == AAC_OWNER_FIRMWARE) {
1423  active++;
1424  break;
1425  }
1426  }
1427  spin_unlock_irqrestore(&dev->list_lock, flagv);
1428  if (active)
1429  break;
1430 
1431  }
1432  /*
1433  * We can exit If all the commands are complete
1434  */
1435  if (active == 0)
1436  break;
1437  ssleep(1);
1438  }
1439 
1440  /* Quiesce build, flush cache, write through mode */
1441  if (forced < 2)
1442  aac_send_shutdown(aac);
1443  spin_lock_irqsave(host->host_lock, flagv);
1444  retval = _aac_reset_adapter(aac, forced ? forced : ((aac_check_reset != 0) && (aac_check_reset != 1)));
1445  spin_unlock_irqrestore(host->host_lock, flagv);
1446 
1447  if ((forced < 2) && (retval == -ENODEV)) {
1448  /* Unwind aac_send_shutdown() IOP_RESET unsupported/disabled */
1449  struct fib * fibctx = aac_fib_alloc(aac);
1450  if (fibctx) {
1451  struct aac_pause *cmd;
1452  int status;
1453 
1454  aac_fib_init(fibctx);
1455 
1456  cmd = (struct aac_pause *) fib_data(fibctx);
1457 
1459  cmd->type = cpu_to_le32(CT_PAUSE_IO);
1460  cmd->timeout = cpu_to_le32(1);
1461  cmd->min = cpu_to_le32(1);
1462  cmd->noRescan = cpu_to_le32(1);
1463  cmd->count = cpu_to_le32(0);
1464 
1465  status = aac_fib_send(ContainerCommand,
1466  fibctx,
1467  sizeof(struct aac_pause),
1468  FsaNormal,
1469  -2 /* Timeout silently */, 1,
1470  NULL, NULL);
1471 
1472  if (status >= 0)
1473  aac_fib_complete(fibctx);
1474  /* FIB should be freed only after getting
1475  * the response from the F/W */
1476  if (status != -ERESTARTSYS)
1477  aac_fib_free(fibctx);
1478  }
1479  }
1480 
1481  return retval;
1482 }
1483 
1484 int aac_check_health(struct aac_dev * aac)
1485 {
1486  int BlinkLED;
1487  unsigned long time_now, flagv = 0;
1488  struct list_head * entry;
1489  struct Scsi_Host * host;
1490 
1491  /* Extending the scope of fib_lock slightly to protect aac->in_reset */
1492  if (spin_trylock_irqsave(&aac->fib_lock, flagv) == 0)
1493  return 0;
1494 
1495  if (aac->in_reset || !(BlinkLED = aac_adapter_check_health(aac))) {
1496  spin_unlock_irqrestore(&aac->fib_lock, flagv);
1497  return 0; /* OK */
1498  }
1499 
1500  aac->in_reset = 1;
1501 
1502  /* Fake up an AIF:
1503  * aac_aifcmd.command = AifCmdEventNotify = 1
1504  * aac_aifcmd.seqnum = 0xFFFFFFFF
1505  * aac_aifcmd.data[0] = AifEnExpEvent = 23
1506  * aac_aifcmd.data[1] = AifExeFirmwarePanic = 3
1507  * aac.aifcmd.data[2] = AifHighPriority = 3
1508  * aac.aifcmd.data[3] = BlinkLED
1509  */
1510 
1511  time_now = jiffies/HZ;
1512  entry = aac->fib_list.next;
1513 
1514  /*
1515  * For each Context that is on the
1516  * fibctxList, make a copy of the
1517  * fib, and then set the event to wake up the
1518  * thread that is waiting for it.
1519  */
1520  while (entry != &aac->fib_list) {
1521  /*
1522  * Extract the fibctx
1523  */
1524  struct aac_fib_context *fibctx = list_entry(entry, struct aac_fib_context, next);
1525  struct hw_fib * hw_fib;
1526  struct fib * fib;
1527  /*
1528  * Check if the queue is getting
1529  * backlogged
1530  */
1531  if (fibctx->count > 20) {
1532  /*
1533  * It's *not* jiffies folks,
1534  * but jiffies / HZ, so do not
1535  * panic ...
1536  */
1537  u32 time_last = fibctx->jiffies;
1538  /*
1539  * Has it been > 2 minutes
1540  * since the last read off
1541  * the queue?
1542  */
1543  if ((time_now - time_last) > aif_timeout) {
1544  entry = entry->next;
1545  aac_close_fib_context(aac, fibctx);
1546  continue;
1547  }
1548  }
1549  /*
1550  * Warning: no sleep allowed while
1551  * holding spinlock
1552  */
1553  hw_fib = kzalloc(sizeof(struct hw_fib), GFP_ATOMIC);
1554  fib = kzalloc(sizeof(struct fib), GFP_ATOMIC);
1555  if (fib && hw_fib) {
1556  struct aac_aifcmd * aif;
1557 
1558  fib->hw_fib_va = hw_fib;
1559  fib->dev = aac;
1560  aac_fib_init(fib);
1561  fib->type = FSAFS_NTC_FIB_CONTEXT;
1562  fib->size = sizeof (struct fib);
1563  fib->data = hw_fib->data;
1564  aif = (struct aac_aifcmd *)hw_fib->data;
1566  aif->seqnum = cpu_to_le32(0xFFFFFFFF);
1567  ((__le32 *)aif->data)[0] = cpu_to_le32(AifEnExpEvent);
1568  ((__le32 *)aif->data)[1] = cpu_to_le32(AifExeFirmwarePanic);
1569  ((__le32 *)aif->data)[2] = cpu_to_le32(AifHighPriority);
1570  ((__le32 *)aif->data)[3] = cpu_to_le32(BlinkLED);
1571 
1572  /*
1573  * Put the FIB onto the
1574  * fibctx's fibs
1575  */
1576  list_add_tail(&fib->fiblink, &fibctx->fib_list);
1577  fibctx->count++;
1578  /*
1579  * Set the event to wake up the
1580  * thread that will waiting.
1581  */
1582  up(&fibctx->wait_sem);
1583  } else {
1584  printk(KERN_WARNING "aifd: didn't allocate NewFib.\n");
1585  kfree(fib);
1586  kfree(hw_fib);
1587  }
1588  entry = entry->next;
1589  }
1590 
1591  spin_unlock_irqrestore(&aac->fib_lock, flagv);
1592 
1593  if (BlinkLED < 0) {
1594  printk(KERN_ERR "%s: Host adapter dead %d\n", aac->name, BlinkLED);
1595  goto out;
1596  }
1597 
1598  printk(KERN_ERR "%s: Host adapter BLINK LED 0x%x\n", aac->name, BlinkLED);
1599 
1600  if (!aac_check_reset || ((aac_check_reset == 1) &&
1601  (aac->supplement_adapter_info.SupportedOptions2 &
1603  goto out;
1604  host = aac->scsi_host_ptr;
1605  if (aac->thread->pid != current->pid)
1606  spin_lock_irqsave(host->host_lock, flagv);
1607  BlinkLED = _aac_reset_adapter(aac, aac_check_reset != 1);
1608  if (aac->thread->pid != current->pid)
1609  spin_unlock_irqrestore(host->host_lock, flagv);
1610  return BlinkLED;
1611 
1612 out:
1613  aac->in_reset = 0;
1614  return BlinkLED;
1615 }
1616 
1617 
1629 {
1630  struct aac_dev *dev = data;
1631  struct hw_fib *hw_fib, *hw_newfib;
1632  struct fib *fib, *newfib;
1633  struct aac_fib_context *fibctx;
1634  unsigned long flags;
1636  unsigned long next_jiffies = jiffies + HZ;
1637  unsigned long next_check_jiffies = next_jiffies;
1638  long difference = HZ;
1639 
1640  /*
1641  * We can only have one thread per adapter for AIF's.
1642  */
1643  if (dev->aif_thread)
1644  return -EINVAL;
1645 
1646  /*
1647  * Let the DPC know it has a place to send the AIF's to.
1648  */
1649  dev->aif_thread = 1;
1650  add_wait_queue(&dev->queues->queue[HostNormCmdQueue].cmdready, &wait);
1652  dprintk ((KERN_INFO "aac_command_thread start\n"));
1653  while (1) {
1654  spin_lock_irqsave(dev->queues->queue[HostNormCmdQueue].lock, flags);
1655  while(!list_empty(&(dev->queues->queue[HostNormCmdQueue].cmdq))) {
1656  struct list_head *entry;
1657  struct aac_aifcmd * aifcmd;
1658 
1660 
1661  entry = dev->queues->queue[HostNormCmdQueue].cmdq.next;
1662  list_del(entry);
1663 
1664  spin_unlock_irqrestore(dev->queues->queue[HostNormCmdQueue].lock, flags);
1665  fib = list_entry(entry, struct fib, fiblink);
1666  /*
1667  * We will process the FIB here or pass it to a
1668  * worker thread that is TBD. We Really can't
1669  * do anything at this point since we don't have
1670  * anything defined for this thread to do.
1671  */
1672  hw_fib = fib->hw_fib_va;
1673  memset(fib, 0, sizeof(struct fib));
1674  fib->type = FSAFS_NTC_FIB_CONTEXT;
1675  fib->size = sizeof(struct fib);
1676  fib->hw_fib_va = hw_fib;
1677  fib->data = hw_fib->data;
1678  fib->dev = dev;
1679  /*
1680  * We only handle AifRequest fibs from the adapter.
1681  */
1682  aifcmd = (struct aac_aifcmd *) hw_fib->data;
1683  if (aifcmd->command == cpu_to_le32(AifCmdDriverNotify)) {
1684  /* Handle Driver Notify Events */
1685  aac_handle_aif(dev, fib);
1686  *(__le32 *)hw_fib->data = cpu_to_le32(ST_OK);
1687  aac_fib_adapter_complete(fib, (u16)sizeof(u32));
1688  } else {
1689  /* The u32 here is important and intended. We are using
1690  32bit wrapping time to fit the adapter field */
1691 
1692  u32 time_now, time_last;
1693  unsigned long flagv;
1694  unsigned num;
1695  struct hw_fib ** hw_fib_pool, ** hw_fib_p;
1696  struct fib ** fib_pool, ** fib_p;
1697 
1698  /* Sniff events */
1699  if ((aifcmd->command ==
1701  (aifcmd->command ==
1703  aac_handle_aif(dev, fib);
1704  }
1705 
1706  time_now = jiffies/HZ;
1707 
1708  /*
1709  * Warning: no sleep allowed while
1710  * holding spinlock. We take the estimate
1711  * and pre-allocate a set of fibs outside the
1712  * lock.
1713  */
1714  num = le32_to_cpu(dev->init->AdapterFibsSize)
1715  / sizeof(struct hw_fib); /* some extra */
1716  spin_lock_irqsave(&dev->fib_lock, flagv);
1717  entry = dev->fib_list.next;
1718  while (entry != &dev->fib_list) {
1719  entry = entry->next;
1720  ++num;
1721  }
1722  spin_unlock_irqrestore(&dev->fib_lock, flagv);
1723  hw_fib_pool = NULL;
1724  fib_pool = NULL;
1725  if (num
1726  && ((hw_fib_pool = kmalloc(sizeof(struct hw_fib *) * num, GFP_KERNEL)))
1727  && ((fib_pool = kmalloc(sizeof(struct fib *) * num, GFP_KERNEL)))) {
1728  hw_fib_p = hw_fib_pool;
1729  fib_p = fib_pool;
1730  while (hw_fib_p < &hw_fib_pool[num]) {
1731  if (!(*(hw_fib_p++) = kmalloc(sizeof(struct hw_fib), GFP_KERNEL))) {
1732  --hw_fib_p;
1733  break;
1734  }
1735  if (!(*(fib_p++) = kmalloc(sizeof(struct fib), GFP_KERNEL))) {
1736  kfree(*(--hw_fib_p));
1737  break;
1738  }
1739  }
1740  if ((num = hw_fib_p - hw_fib_pool) == 0) {
1741  kfree(fib_pool);
1742  fib_pool = NULL;
1743  kfree(hw_fib_pool);
1744  hw_fib_pool = NULL;
1745  }
1746  } else {
1747  kfree(hw_fib_pool);
1748  hw_fib_pool = NULL;
1749  }
1750  spin_lock_irqsave(&dev->fib_lock, flagv);
1751  entry = dev->fib_list.next;
1752  /*
1753  * For each Context that is on the
1754  * fibctxList, make a copy of the
1755  * fib, and then set the event to wake up the
1756  * thread that is waiting for it.
1757  */
1758  hw_fib_p = hw_fib_pool;
1759  fib_p = fib_pool;
1760  while (entry != &dev->fib_list) {
1761  /*
1762  * Extract the fibctx
1763  */
1764  fibctx = list_entry(entry, struct aac_fib_context, next);
1765  /*
1766  * Check if the queue is getting
1767  * backlogged
1768  */
1769  if (fibctx->count > 20)
1770  {
1771  /*
1772  * It's *not* jiffies folks,
1773  * but jiffies / HZ so do not
1774  * panic ...
1775  */
1776  time_last = fibctx->jiffies;
1777  /*
1778  * Has it been > 2 minutes
1779  * since the last read off
1780  * the queue?
1781  */
1782  if ((time_now - time_last) > aif_timeout) {
1783  entry = entry->next;
1784  aac_close_fib_context(dev, fibctx);
1785  continue;
1786  }
1787  }
1788  /*
1789  * Warning: no sleep allowed while
1790  * holding spinlock
1791  */
1792  if (hw_fib_p < &hw_fib_pool[num]) {
1793  hw_newfib = *hw_fib_p;
1794  *(hw_fib_p++) = NULL;
1795  newfib = *fib_p;
1796  *(fib_p++) = NULL;
1797  /*
1798  * Make the copy of the FIB
1799  */
1800  memcpy(hw_newfib, hw_fib, sizeof(struct hw_fib));
1801  memcpy(newfib, fib, sizeof(struct fib));
1802  newfib->hw_fib_va = hw_newfib;
1803  /*
1804  * Put the FIB onto the
1805  * fibctx's fibs
1806  */
1807  list_add_tail(&newfib->fiblink, &fibctx->fib_list);
1808  fibctx->count++;
1809  /*
1810  * Set the event to wake up the
1811  * thread that is waiting.
1812  */
1813  up(&fibctx->wait_sem);
1814  } else {
1815  printk(KERN_WARNING "aifd: didn't allocate NewFib.\n");
1816  }
1817  entry = entry->next;
1818  }
1819  /*
1820  * Set the status of this FIB
1821  */
1822  *(__le32 *)hw_fib->data = cpu_to_le32(ST_OK);
1823  aac_fib_adapter_complete(fib, sizeof(u32));
1824  spin_unlock_irqrestore(&dev->fib_lock, flagv);
1825  /* Free up the remaining resources */
1826  hw_fib_p = hw_fib_pool;
1827  fib_p = fib_pool;
1828  while (hw_fib_p < &hw_fib_pool[num]) {
1829  kfree(*hw_fib_p);
1830  kfree(*fib_p);
1831  ++fib_p;
1832  ++hw_fib_p;
1833  }
1834  kfree(hw_fib_pool);
1835  kfree(fib_pool);
1836  }
1837  kfree(fib);
1838  spin_lock_irqsave(dev->queues->queue[HostNormCmdQueue].lock, flags);
1839  }
1840  /*
1841  * There are no more AIF's
1842  */
1843  spin_unlock_irqrestore(dev->queues->queue[HostNormCmdQueue].lock, flags);
1844 
1845  /*
1846  * Background activity
1847  */
1848  if ((time_before(next_check_jiffies,next_jiffies))
1849  && ((difference = next_check_jiffies - jiffies) <= 0)) {
1850  next_check_jiffies = next_jiffies;
1851  if (aac_check_health(dev) == 0) {
1852  difference = ((long)(unsigned)check_interval)
1853  * HZ;
1854  next_check_jiffies = jiffies + difference;
1855  } else if (!dev->queues)
1856  break;
1857  }
1858  if (!time_before(next_check_jiffies,next_jiffies)
1859  && ((difference = next_jiffies - jiffies) <= 0)) {
1860  struct timeval now;
1861  int ret;
1862 
1863  /* Don't even try to talk to adapter if its sick */
1864  ret = aac_check_health(dev);
1865  if (!ret && !dev->queues)
1866  break;
1867  next_check_jiffies = jiffies
1868  + ((long)(unsigned)check_interval)
1869  * HZ;
1870  do_gettimeofday(&now);
1871 
1872  /* Synchronize our watches */
1873  if (((1000000 - (1000000 / HZ)) > now.tv_usec)
1874  && (now.tv_usec > (1000000 / HZ)))
1875  difference = (((1000000 - now.tv_usec) * HZ)
1876  + 500000) / 1000000;
1877  else if (ret == 0) {
1878  struct fib *fibptr;
1879 
1880  if ((fibptr = aac_fib_alloc(dev))) {
1881  int status;
1882  __le32 *info;
1883 
1884  aac_fib_init(fibptr);
1885 
1886  info = (__le32 *) fib_data(fibptr);
1887  if (now.tv_usec > 500000)
1888  ++now.tv_sec;
1889 
1890  *info = cpu_to_le32(now.tv_sec);
1891 
1892  status = aac_fib_send(SendHostTime,
1893  fibptr,
1894  sizeof(*info),
1895  FsaNormal,
1896  1, 1,
1897  NULL,
1898  NULL);
1899  /* Do not set XferState to zero unless
1900  * receives a response from F/W */
1901  if (status >= 0)
1902  aac_fib_complete(fibptr);
1903  /* FIB should be freed only after
1904  * getting the response from the F/W */
1905  if (status != -ERESTARTSYS)
1906  aac_fib_free(fibptr);
1907  }
1908  difference = (long)(unsigned)update_interval*HZ;
1909  } else {
1910  /* retry shortly */
1911  difference = 10 * HZ;
1912  }
1913  next_jiffies = jiffies + difference;
1914  if (time_before(next_check_jiffies,next_jiffies))
1915  difference = next_check_jiffies - jiffies;
1916  }
1917  if (difference <= 0)
1918  difference = 1;
1920  schedule_timeout(difference);
1921 
1922  if (kthread_should_stop())
1923  break;
1924  }
1925  if (dev->queues)
1926  remove_wait_queue(&dev->queues->queue[HostNormCmdQueue].cmdready, &wait);
1927  dev->aif_thread = 0;
1928  return 0;
1929 }