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