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