blob: d2ef17ea44fa640c82e68e6001258aa2199a5f4c [file] [log] [blame]
Linus Torvalds1da177e2005-04-16 15:20:36 -07001/*
2 * Adaptec AAC series RAID controller driver
3 * (c) Copyright 2001 Red Hat Inc. <alan@redhat.com>
4 *
5 * based on the old aacraid driver that is..
6 * Adaptec aacraid device driver for Linux.
7 *
8 * Copyright (c) 2000 Adaptec, Inc. (aacraid@adaptec.com)
9 *
10 * This program is free software; you can redistribute it and/or modify
11 * it under the terms of the GNU General Public License as published by
12 * the Free Software Foundation; either version 2, or (at your option)
13 * any later version.
14 *
15 * This program is distributed in the hope that it will be useful,
16 * but WITHOUT ANY WARRANTY; without even the implied warranty of
17 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
18 * GNU General Public License for more details.
19 *
20 * You should have received a copy of the GNU General Public License
21 * along with this program; see the file COPYING. If not, write to
22 * the Free Software Foundation, 675 Mass Ave, Cambridge, MA 02139, USA.
23 *
24 * Module Name:
25 * commsup.c
26 *
27 * Abstract: Contain all routines that are required for FSA host/adapter
Mark Haverkamp 7c00ffa2005-05-16 18:28:42 -070028 * communication.
Linus Torvalds1da177e2005-04-16 15:20:36 -070029 *
30 */
31
32#include <linux/kernel.h>
33#include <linux/init.h>
34#include <linux/types.h>
35#include <linux/sched.h>
36#include <linux/pci.h>
37#include <linux/spinlock.h>
38#include <linux/slab.h>
39#include <linux/completion.h>
40#include <linux/blkdev.h>
Al Viro164006d2005-11-30 23:47:05 -050041#include <linux/delay.h>
Christoph Hellwigfe273812006-02-14 18:45:06 +010042#include <linux/kthread.h>
Mark Haverkamp 7c00ffa2005-05-16 18:28:42 -070043#include <scsi/scsi_host.h>
Mark Haverkamp131256c2005-09-26 13:04:56 -070044#include <scsi/scsi_device.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070045#include <asm/semaphore.h>
46
47#include "aacraid.h"
48
49/**
50 * fib_map_alloc - allocate the fib objects
51 * @dev: Adapter to allocate for
52 *
53 * Allocate and map the shared PCI space for the FIB blocks used to
54 * talk to the Adaptec firmware.
55 */
56
57static int fib_map_alloc(struct aac_dev *dev)
58{
Mark Haverkamp 7c00ffa2005-05-16 18:28:42 -070059 dprintk((KERN_INFO
60 "allocate hardware fibs pci_alloc_consistent(%p, %d * (%d + %d), %p)\n",
61 dev->pdev, dev->max_fib_size, dev->scsi_host_ptr->can_queue,
62 AAC_NUM_MGT_FIB, &dev->hw_fib_pa));
63 if((dev->hw_fib_va = pci_alloc_consistent(dev->pdev, dev->max_fib_size
64 * (dev->scsi_host_ptr->can_queue + AAC_NUM_MGT_FIB),
65 &dev->hw_fib_pa))==NULL)
Linus Torvalds1da177e2005-04-16 15:20:36 -070066 return -ENOMEM;
67 return 0;
68}
69
70/**
Mark Haverkampbfb35aa82006-02-01 09:30:55 -080071 * aac_fib_map_free - free the fib objects
Linus Torvalds1da177e2005-04-16 15:20:36 -070072 * @dev: Adapter to free
73 *
74 * Free the PCI mappings and the memory allocated for FIB blocks
75 * on this adapter.
76 */
77
Mark Haverkampbfb35aa82006-02-01 09:30:55 -080078void aac_fib_map_free(struct aac_dev *dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -070079{
Mark Haverkamp 7c00ffa2005-05-16 18:28:42 -070080 pci_free_consistent(dev->pdev, dev->max_fib_size * (dev->scsi_host_ptr->can_queue + AAC_NUM_MGT_FIB), dev->hw_fib_va, dev->hw_fib_pa);
Linus Torvalds1da177e2005-04-16 15:20:36 -070081}
82
83/**
Mark Haverkampbfb35aa82006-02-01 09:30:55 -080084 * aac_fib_setup - setup the fibs
Linus Torvalds1da177e2005-04-16 15:20:36 -070085 * @dev: Adapter to set up
86 *
87 * Allocate the PCI space for the fibs, map it and then intialise the
88 * fib area, the unmapped fib data and also the free list
89 */
90
Mark Haverkampbfb35aa82006-02-01 09:30:55 -080091int aac_fib_setup(struct aac_dev * dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -070092{
93 struct fib *fibptr;
94 struct hw_fib *hw_fib_va;
95 dma_addr_t hw_fib_pa;
96 int i;
Mark Haverkamp 7c00ffa2005-05-16 18:28:42 -070097
98 while (((i = fib_map_alloc(dev)) == -ENOMEM)
99 && (dev->scsi_host_ptr->can_queue > (64 - AAC_NUM_MGT_FIB))) {
100 dev->init->MaxIoCommands = cpu_to_le32((dev->scsi_host_ptr->can_queue + AAC_NUM_MGT_FIB) >> 1);
101 dev->scsi_host_ptr->can_queue = le32_to_cpu(dev->init->MaxIoCommands) - AAC_NUM_MGT_FIB;
102 }
103 if (i<0)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700104 return -ENOMEM;
105
106 hw_fib_va = dev->hw_fib_va;
107 hw_fib_pa = dev->hw_fib_pa;
Mark Haverkamp 7c00ffa2005-05-16 18:28:42 -0700108 memset(hw_fib_va, 0, dev->max_fib_size * (dev->scsi_host_ptr->can_queue + AAC_NUM_MGT_FIB));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700109 /*
110 * Initialise the fibs
111 */
Mark Haverkamp 7c00ffa2005-05-16 18:28:42 -0700112 for (i = 0, fibptr = &dev->fibs[i]; i < (dev->scsi_host_ptr->can_queue + AAC_NUM_MGT_FIB); i++, fibptr++)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700113 {
114 fibptr->dev = dev;
115 fibptr->hw_fib = hw_fib_va;
116 fibptr->data = (void *) fibptr->hw_fib->data;
117 fibptr->next = fibptr+1; /* Forward chain the fibs */
118 init_MUTEX_LOCKED(&fibptr->event_wait);
119 spin_lock_init(&fibptr->event_lock);
Mark Haverkamp 56b58712005-04-27 06:05:51 -0700120 hw_fib_va->header.XferState = cpu_to_le32(0xffffffff);
Mark Haverkamp 7c00ffa2005-05-16 18:28:42 -0700121 hw_fib_va->header.SenderSize = cpu_to_le16(dev->max_fib_size);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700122 fibptr->hw_fib_pa = hw_fib_pa;
Mark Haverkamp 7c00ffa2005-05-16 18:28:42 -0700123 hw_fib_va = (struct hw_fib *)((unsigned char *)hw_fib_va + dev->max_fib_size);
124 hw_fib_pa = hw_fib_pa + dev->max_fib_size;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700125 }
126 /*
127 * Add the fib chain to the free list
128 */
Mark Haverkamp 7c00ffa2005-05-16 18:28:42 -0700129 dev->fibs[dev->scsi_host_ptr->can_queue + AAC_NUM_MGT_FIB - 1].next = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700130 /*
131 * Enable this to debug out of queue space
132 */
133 dev->free_fib = &dev->fibs[0];
134 return 0;
135}
136
137/**
Mark Haverkampbfb35aa82006-02-01 09:30:55 -0800138 * aac_fib_alloc - allocate a fib
Linus Torvalds1da177e2005-04-16 15:20:36 -0700139 * @dev: Adapter to allocate the fib for
140 *
141 * Allocate a fib from the adapter fib pool. If the pool is empty we
Mark Haverkamp 7c00ffa2005-05-16 18:28:42 -0700142 * return NULL.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700143 */
144
Mark Haverkampbfb35aa82006-02-01 09:30:55 -0800145struct fib *aac_fib_alloc(struct aac_dev *dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700146{
147 struct fib * fibptr;
148 unsigned long flags;
149 spin_lock_irqsave(&dev->fib_lock, flags);
150 fibptr = dev->free_fib;
Mark Haverkamp 7c00ffa2005-05-16 18:28:42 -0700151 if(!fibptr){
152 spin_unlock_irqrestore(&dev->fib_lock, flags);
153 return fibptr;
154 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700155 dev->free_fib = fibptr->next;
156 spin_unlock_irqrestore(&dev->fib_lock, flags);
157 /*
158 * Set the proper node type code and node byte size
159 */
160 fibptr->type = FSAFS_NTC_FIB_CONTEXT;
161 fibptr->size = sizeof(struct fib);
162 /*
163 * Null out fields that depend on being zero at the start of
164 * each I/O
165 */
166 fibptr->hw_fib->header.XferState = 0;
167 fibptr->callback = NULL;
168 fibptr->callback_data = NULL;
169
170 return fibptr;
171}
172
173/**
Mark Haverkampbfb35aa82006-02-01 09:30:55 -0800174 * aac_fib_free - free a fib
Linus Torvalds1da177e2005-04-16 15:20:36 -0700175 * @fibptr: fib to free up
176 *
177 * Frees up a fib and places it on the appropriate queue
178 * (either free or timed out)
179 */
180
Mark Haverkampbfb35aa82006-02-01 09:30:55 -0800181void aac_fib_free(struct fib *fibptr)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700182{
183 unsigned long flags;
184
185 spin_lock_irqsave(&fibptr->dev->fib_lock, flags);
186 if (fibptr->flags & FIB_CONTEXT_FLAG_TIMED_OUT) {
187 aac_config.fib_timeouts++;
188 fibptr->next = fibptr->dev->timeout_fib;
189 fibptr->dev->timeout_fib = fibptr;
190 } else {
191 if (fibptr->hw_fib->header.XferState != 0) {
Mark Haverkampbfb35aa82006-02-01 09:30:55 -0800192 printk(KERN_WARNING "aac_fib_free, XferState != 0, fibptr = 0x%p, XferState = 0x%x\n",
Linus Torvalds1da177e2005-04-16 15:20:36 -0700193 (void*)fibptr,
194 le32_to_cpu(fibptr->hw_fib->header.XferState));
195 }
196 fibptr->next = fibptr->dev->free_fib;
197 fibptr->dev->free_fib = fibptr;
198 }
199 spin_unlock_irqrestore(&fibptr->dev->fib_lock, flags);
200}
201
202/**
Mark Haverkampbfb35aa82006-02-01 09:30:55 -0800203 * aac_fib_init - initialise a fib
Linus Torvalds1da177e2005-04-16 15:20:36 -0700204 * @fibptr: The fib to initialize
205 *
206 * Set up the generic fib fields ready for use
207 */
208
Mark Haverkampbfb35aa82006-02-01 09:30:55 -0800209void aac_fib_init(struct fib *fibptr)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700210{
211 struct hw_fib *hw_fib = fibptr->hw_fib;
212
213 hw_fib->header.StructType = FIB_MAGIC;
Mark Haverkamp 7c00ffa2005-05-16 18:28:42 -0700214 hw_fib->header.Size = cpu_to_le16(fibptr->dev->max_fib_size);
215 hw_fib->header.XferState = cpu_to_le32(HostOwned | FibInitialized | FibEmpty | FastResponseCapable);
Mark Haverkamp8e0c5eb2005-10-24 10:52:22 -0700216 hw_fib->header.SenderFibAddress = 0; /* Filled in later if needed */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700217 hw_fib->header.ReceiverFibAddress = cpu_to_le32(fibptr->hw_fib_pa);
Mark Haverkamp 7c00ffa2005-05-16 18:28:42 -0700218 hw_fib->header.SenderSize = cpu_to_le16(fibptr->dev->max_fib_size);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700219}
220
221/**
222 * fib_deallocate - deallocate a fib
223 * @fibptr: fib to deallocate
224 *
225 * Will deallocate and return to the free pool the FIB pointed to by the
226 * caller.
227 */
228
Adrian Bunk 48338692005-04-25 19:45:58 -0700229static void fib_dealloc(struct fib * fibptr)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700230{
231 struct hw_fib *hw_fib = fibptr->hw_fib;
232 if(hw_fib->header.StructType != FIB_MAGIC)
233 BUG();
234 hw_fib->header.XferState = 0;
235}
236
237/*
238 * Commuication primitives define and support the queuing method we use to
239 * support host to adapter commuication. All queue accesses happen through
240 * these routines and are the only routines which have a knowledge of the
241 * how these queues are implemented.
242 */
243
244/**
245 * aac_get_entry - get a queue entry
246 * @dev: Adapter
247 * @qid: Queue Number
248 * @entry: Entry return
249 * @index: Index return
250 * @nonotify: notification control
251 *
252 * With a priority the routine returns a queue entry if the queue has free entries. If the queue
253 * is full(no free entries) than no entry is returned and the function returns 0 otherwise 1 is
254 * returned.
255 */
256
257static int aac_get_entry (struct aac_dev * dev, u32 qid, struct aac_entry **entry, u32 * index, unsigned long *nonotify)
258{
259 struct aac_queue * q;
Mark Haverkampbed30de2005-08-03 15:38:51 -0700260 unsigned long idx;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700261
262 /*
263 * All of the queues wrap when they reach the end, so we check
264 * to see if they have reached the end and if they have we just
265 * set the index back to zero. This is a wrap. You could or off
266 * the high bits in all updates but this is a bit faster I think.
267 */
268
269 q = &dev->queues->queue[qid];
Mark Haverkampbed30de2005-08-03 15:38:51 -0700270
271 idx = *index = le32_to_cpu(*(q->headers.producer));
272 /* Interrupt Moderation, only interrupt for first two entries */
273 if (idx != le32_to_cpu(*(q->headers.consumer))) {
274 if (--idx == 0) {
Mark Haverkamp1640a2c2005-09-20 12:57:11 -0700275 if (qid == AdapNormCmdQueue)
Mark Haverkampbed30de2005-08-03 15:38:51 -0700276 idx = ADAP_NORM_CMD_ENTRIES;
Mark Haverkamp1640a2c2005-09-20 12:57:11 -0700277 else
Mark Haverkampbed30de2005-08-03 15:38:51 -0700278 idx = ADAP_NORM_RESP_ENTRIES;
279 }
280 if (idx != le32_to_cpu(*(q->headers.consumer)))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700281 *nonotify = 1;
Mark Haverkampbed30de2005-08-03 15:38:51 -0700282 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700283
Mark Haverkamp1640a2c2005-09-20 12:57:11 -0700284 if (qid == AdapNormCmdQueue) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700285 if (*index >= ADAP_NORM_CMD_ENTRIES)
286 *index = 0; /* Wrap to front of the Producer Queue. */
Mark Haverkamp1640a2c2005-09-20 12:57:11 -0700287 } else {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700288 if (*index >= ADAP_NORM_RESP_ENTRIES)
289 *index = 0; /* Wrap to front of the Producer Queue. */
290 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700291
292 if ((*index + 1) == le32_to_cpu(*(q->headers.consumer))) { /* Queue is full */
Mark Haverkamp 7c00ffa2005-05-16 18:28:42 -0700293 printk(KERN_WARNING "Queue %d full, %u outstanding.\n",
Linus Torvalds1da177e2005-04-16 15:20:36 -0700294 qid, q->numpending);
295 return 0;
296 } else {
297 *entry = q->base + *index;
298 return 1;
299 }
300}
301
302/**
303 * aac_queue_get - get the next free QE
304 * @dev: Adapter
305 * @index: Returned index
306 * @priority: Priority of fib
307 * @fib: Fib to associate with the queue entry
308 * @wait: Wait if queue full
309 * @fibptr: Driver fib object to go with fib
310 * @nonotify: Don't notify the adapter
311 *
312 * Gets the next free QE off the requested priorty adapter command
313 * queue and associates the Fib with the QE. The QE represented by
314 * index is ready to insert on the queue when this routine returns
315 * success.
316 */
317
318static int aac_queue_get(struct aac_dev * dev, u32 * index, u32 qid, struct hw_fib * hw_fib, int wait, struct fib * fibptr, unsigned long *nonotify)
319{
320 struct aac_entry * entry = NULL;
321 int map = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700322
Mark Haverkamp1640a2c2005-09-20 12:57:11 -0700323 if (qid == AdapNormCmdQueue) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700324 /* if no entries wait for some if caller wants to */
325 while (!aac_get_entry(dev, qid, &entry, index, nonotify))
326 {
327 printk(KERN_ERR "GetEntries failed\n");
328 }
329 /*
330 * Setup queue entry with a command, status and fib mapped
331 */
332 entry->size = cpu_to_le32(le16_to_cpu(hw_fib->header.Size));
333 map = 1;
Mark Haverkamp1640a2c2005-09-20 12:57:11 -0700334 } else {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700335 while(!aac_get_entry(dev, qid, &entry, index, nonotify))
336 {
337 /* if no entries wait for some if caller wants to */
338 }
339 /*
340 * Setup queue entry with command, status and fib mapped
341 */
342 entry->size = cpu_to_le32(le16_to_cpu(hw_fib->header.Size));
343 entry->addr = hw_fib->header.SenderFibAddress;
344 /* Restore adapters pointer to the FIB */
345 hw_fib->header.ReceiverFibAddress = hw_fib->header.SenderFibAddress; /* Let the adapter now where to find its data */
346 map = 0;
347 }
348 /*
349 * If MapFib is true than we need to map the Fib and put pointers
350 * in the queue entry.
351 */
352 if (map)
353 entry->addr = cpu_to_le32(fibptr->hw_fib_pa);
354 return 0;
355}
356
Linus Torvalds1da177e2005-04-16 15:20:36 -0700357/*
358 * Define the highest level of host to adapter communication routines.
359 * These routines will support host to adapter FS commuication. These
360 * routines have no knowledge of the commuication method used. This level
361 * sends and receives FIBs. This level has no knowledge of how these FIBs
362 * get passed back and forth.
363 */
364
365/**
Mark Haverkampbfb35aa82006-02-01 09:30:55 -0800366 * aac_fib_send - send a fib to the adapter
Linus Torvalds1da177e2005-04-16 15:20:36 -0700367 * @command: Command to send
368 * @fibptr: The fib
369 * @size: Size of fib data area
370 * @priority: Priority of Fib
371 * @wait: Async/sync select
372 * @reply: True if a reply is wanted
373 * @callback: Called with reply
374 * @callback_data: Passed to callback
375 *
376 * Sends the requested FIB to the adapter and optionally will wait for a
377 * response FIB. If the caller does not wish to wait for a response than
378 * an event to wait on must be supplied. This event will be set when a
379 * response FIB is received from the adapter.
380 */
381
Mark Haverkampbfb35aa82006-02-01 09:30:55 -0800382int aac_fib_send(u16 command, struct fib *fibptr, unsigned long size,
383 int priority, int wait, int reply, fib_callback callback,
384 void *callback_data)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700385{
Linus Torvalds1da177e2005-04-16 15:20:36 -0700386 struct aac_dev * dev = fibptr->dev;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700387 struct hw_fib * hw_fib = fibptr->hw_fib;
388 struct aac_queue * q;
389 unsigned long flags = 0;
Mark Haverkamp1640a2c2005-09-20 12:57:11 -0700390 unsigned long qflags;
391
Linus Torvalds1da177e2005-04-16 15:20:36 -0700392 if (!(hw_fib->header.XferState & cpu_to_le32(HostOwned)))
393 return -EBUSY;
394 /*
395 * There are 5 cases with the wait and reponse requested flags.
396 * The only invalid cases are if the caller requests to wait and
397 * does not request a response and if the caller does not want a
398 * response and the Fib is not allocated from pool. If a response
399 * is not requesed the Fib will just be deallocaed by the DPC
400 * routine when the response comes back from the adapter. No
401 * further processing will be done besides deleting the Fib. We
402 * will have a debug mode where the adapter can notify the host
403 * it had a problem and the host can log that fact.
404 */
405 if (wait && !reply) {
406 return -EINVAL;
407 } else if (!wait && reply) {
408 hw_fib->header.XferState |= cpu_to_le32(Async | ResponseExpected);
409 FIB_COUNTER_INCREMENT(aac_config.AsyncSent);
410 } else if (!wait && !reply) {
411 hw_fib->header.XferState |= cpu_to_le32(NoResponseExpected);
412 FIB_COUNTER_INCREMENT(aac_config.NoResponseSent);
413 } else if (wait && reply) {
414 hw_fib->header.XferState |= cpu_to_le32(ResponseExpected);
415 FIB_COUNTER_INCREMENT(aac_config.NormalSent);
416 }
417 /*
418 * Map the fib into 32bits by using the fib number
419 */
420
Mark Haverkamp8e0c5eb2005-10-24 10:52:22 -0700421 hw_fib->header.SenderFibAddress = cpu_to_le32(((u32)(fibptr - dev->fibs)) << 2);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700422 hw_fib->header.SenderData = (u32)(fibptr - dev->fibs);
423 /*
424 * Set FIB state to indicate where it came from and if we want a
425 * response from the adapter. Also load the command from the
426 * caller.
427 *
428 * Map the hw fib pointer as a 32bit value
429 */
430 hw_fib->header.Command = cpu_to_le16(command);
431 hw_fib->header.XferState |= cpu_to_le32(SentFromHost);
432 fibptr->hw_fib->header.Flags = 0; /* 0 the flags field - internal only*/
433 /*
434 * Set the size of the Fib we want to send to the adapter
435 */
436 hw_fib->header.Size = cpu_to_le16(sizeof(struct aac_fibhdr) + size);
437 if (le16_to_cpu(hw_fib->header.Size) > le16_to_cpu(hw_fib->header.SenderSize)) {
438 return -EMSGSIZE;
439 }
440 /*
441 * Get a queue entry connect the FIB to it and send an notify
442 * the adapter a command is ready.
443 */
Mark Haverkamp1640a2c2005-09-20 12:57:11 -0700444 hw_fib->header.XferState |= cpu_to_le32(NormalPriority);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700445
Linus Torvalds1da177e2005-04-16 15:20:36 -0700446 /*
447 * Fill in the Callback and CallbackContext if we are not
448 * going to wait.
449 */
450 if (!wait) {
451 fibptr->callback = callback;
452 fibptr->callback_data = callback_data;
453 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700454
455 fibptr->done = 0;
456 fibptr->flags = 0;
457
Mark Haverkamp1640a2c2005-09-20 12:57:11 -0700458 FIB_COUNTER_INCREMENT(aac_config.FibsSent);
459
Mark Haverkamp1640a2c2005-09-20 12:57:11 -0700460 dprintk((KERN_DEBUG "Fib contents:.\n"));
Mark Haverkamp8e0c5eb2005-10-24 10:52:22 -0700461 dprintk((KERN_DEBUG " Command = %d.\n", le32_to_cpu(hw_fib->header.Command)));
462 dprintk((KERN_DEBUG " SubCommand = %d.\n", le32_to_cpu(((struct aac_query_mount *)fib_data(fibptr))->command)));
463 dprintk((KERN_DEBUG " XferState = %x.\n", le32_to_cpu(hw_fib->header.XferState)));
Mark Haverkamp1640a2c2005-09-20 12:57:11 -0700464 dprintk((KERN_DEBUG " hw_fib va being sent=%p\n",fibptr->hw_fib));
465 dprintk((KERN_DEBUG " hw_fib pa being sent=%lx\n",(ulong)fibptr->hw_fib_pa));
466 dprintk((KERN_DEBUG " fib being sent=%p\n",fibptr));
467
468 q = &dev->queues->queue[AdapNormCmdQueue];
469
470 if(wait)
471 spin_lock_irqsave(&fibptr->event_lock, flags);
472 spin_lock_irqsave(q->lock, qflags);
Mark Haverkamp8e0c5eb2005-10-24 10:52:22 -0700473 if (dev->new_comm_interface) {
474 unsigned long count = 10000000L; /* 50 seconds */
Mark Haverkamp8e0c5eb2005-10-24 10:52:22 -0700475 q->numpending++;
476 spin_unlock_irqrestore(q->lock, qflags);
477 while (aac_adapter_send(fibptr) != 0) {
478 if (--count == 0) {
479 if (wait)
480 spin_unlock_irqrestore(&fibptr->event_lock, flags);
481 spin_lock_irqsave(q->lock, qflags);
482 q->numpending--;
Mark Haverkamp8e0c5eb2005-10-24 10:52:22 -0700483 spin_unlock_irqrestore(q->lock, qflags);
484 return -ETIMEDOUT;
485 }
486 udelay(5);
487 }
488 } else {
489 u32 index;
490 unsigned long nointr = 0;
491 aac_queue_get( dev, &index, AdapNormCmdQueue, hw_fib, 1, fibptr, &nointr);
Mark Haverkamp1640a2c2005-09-20 12:57:11 -0700492
Mark Haverkamp8e0c5eb2005-10-24 10:52:22 -0700493 q->numpending++;
494 *(q->headers.producer) = cpu_to_le32(index + 1);
495 spin_unlock_irqrestore(q->lock, qflags);
Mark Haverkampbfb35aa82006-02-01 09:30:55 -0800496 dprintk((KERN_DEBUG "aac_fib_send: inserting a queue entry at index %d.\n",index));
Mark Haverkamp8e0c5eb2005-10-24 10:52:22 -0700497 if (!(nointr & aac_config.irq_mod))
498 aac_adapter_notify(dev, AdapNormCmdQueue);
499 }
500
Linus Torvalds1da177e2005-04-16 15:20:36 -0700501 /*
502 * If the caller wanted us to wait for response wait now.
503 */
504
505 if (wait) {
506 spin_unlock_irqrestore(&fibptr->event_lock, flags);
Mark Haverkamp92033442005-09-20 12:56:50 -0700507 /* Only set for first known interruptable command */
508 if (wait < 0) {
509 /*
510 * *VERY* Dangerous to time out a command, the
511 * assumption is made that we have no hope of
512 * functioning because an interrupt routing or other
513 * hardware failure has occurred.
514 */
515 unsigned long count = 36000000L; /* 3 minutes */
Mark Haverkamp92033442005-09-20 12:56:50 -0700516 while (down_trylock(&fibptr->event_wait)) {
517 if (--count == 0) {
518 spin_lock_irqsave(q->lock, qflags);
519 q->numpending--;
Mark Haverkamp92033442005-09-20 12:56:50 -0700520 spin_unlock_irqrestore(q->lock, qflags);
521 if (wait == -1) {
Mark Haverkampbfb35aa82006-02-01 09:30:55 -0800522 printk(KERN_ERR "aacraid: aac_fib_send: first asynchronous command timed out.\n"
Mark Haverkamp92033442005-09-20 12:56:50 -0700523 "Usually a result of a PCI interrupt routing problem;\n"
524 "update mother board BIOS or consider utilizing one of\n"
525 "the SAFE mode kernel options (acpi, apic etc)\n");
526 }
527 return -ETIMEDOUT;
528 }
529 udelay(5);
530 }
531 } else
532 down(&fibptr->event_wait);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700533 if(fibptr->done == 0)
534 BUG();
535
536 if((fibptr->flags & FIB_CONTEXT_FLAG_TIMED_OUT)){
537 return -ETIMEDOUT;
538 } else {
539 return 0;
540 }
541 }
542 /*
543 * If the user does not want a response than return success otherwise
544 * return pending
545 */
546 if (reply)
547 return -EINPROGRESS;
548 else
549 return 0;
550}
551
552/**
553 * aac_consumer_get - get the top of the queue
554 * @dev: Adapter
555 * @q: Queue
556 * @entry: Return entry
557 *
558 * Will return a pointer to the entry on the top of the queue requested that
559 * we are a consumer of, and return the address of the queue entry. It does
560 * not change the state of the queue.
561 */
562
563int aac_consumer_get(struct aac_dev * dev, struct aac_queue * q, struct aac_entry **entry)
564{
565 u32 index;
566 int status;
567 if (le32_to_cpu(*q->headers.producer) == le32_to_cpu(*q->headers.consumer)) {
568 status = 0;
569 } else {
570 /*
571 * The consumer index must be wrapped if we have reached
572 * the end of the queue, else we just use the entry
573 * pointed to by the header index
574 */
575 if (le32_to_cpu(*q->headers.consumer) >= q->entries)
576 index = 0;
577 else
578 index = le32_to_cpu(*q->headers.consumer);
579 *entry = q->base + index;
580 status = 1;
581 }
582 return(status);
583}
584
585/**
586 * aac_consumer_free - free consumer entry
587 * @dev: Adapter
588 * @q: Queue
589 * @qid: Queue ident
590 *
591 * Frees up the current top of the queue we are a consumer of. If the
592 * queue was full notify the producer that the queue is no longer full.
593 */
594
595void aac_consumer_free(struct aac_dev * dev, struct aac_queue *q, u32 qid)
596{
597 int wasfull = 0;
598 u32 notify;
599
600 if ((le32_to_cpu(*q->headers.producer)+1) == le32_to_cpu(*q->headers.consumer))
601 wasfull = 1;
602
603 if (le32_to_cpu(*q->headers.consumer) >= q->entries)
604 *q->headers.consumer = cpu_to_le32(1);
605 else
606 *q->headers.consumer = cpu_to_le32(le32_to_cpu(*q->headers.consumer)+1);
607
608 if (wasfull) {
609 switch (qid) {
610
611 case HostNormCmdQueue:
612 notify = HostNormCmdNotFull;
613 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700614 case HostNormRespQueue:
615 notify = HostNormRespNotFull;
616 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700617 default:
618 BUG();
619 return;
620 }
621 aac_adapter_notify(dev, notify);
622 }
623}
624
625/**
Mark Haverkampbfb35aa82006-02-01 09:30:55 -0800626 * aac_fib_adapter_complete - complete adapter issued fib
Linus Torvalds1da177e2005-04-16 15:20:36 -0700627 * @fibptr: fib to complete
628 * @size: size of fib
629 *
630 * Will do all necessary work to complete a FIB that was sent from
631 * the adapter.
632 */
633
Mark Haverkampbfb35aa82006-02-01 09:30:55 -0800634int aac_fib_adapter_complete(struct fib *fibptr, unsigned short size)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700635{
636 struct hw_fib * hw_fib = fibptr->hw_fib;
637 struct aac_dev * dev = fibptr->dev;
Mark Haverkamp1640a2c2005-09-20 12:57:11 -0700638 struct aac_queue * q;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700639 unsigned long nointr = 0;
Mark Haverkamp1640a2c2005-09-20 12:57:11 -0700640 unsigned long qflags;
641
642 if (hw_fib->header.XferState == 0) {
Mark Haverkamp8e0c5eb2005-10-24 10:52:22 -0700643 if (dev->new_comm_interface)
644 kfree (hw_fib);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700645 return 0;
Mark Haverkamp1640a2c2005-09-20 12:57:11 -0700646 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700647 /*
648 * If we plan to do anything check the structure type first.
649 */
650 if ( hw_fib->header.StructType != FIB_MAGIC ) {
Mark Haverkamp8e0c5eb2005-10-24 10:52:22 -0700651 if (dev->new_comm_interface)
652 kfree (hw_fib);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700653 return -EINVAL;
654 }
655 /*
656 * This block handles the case where the adapter had sent us a
657 * command and we have finished processing the command. We
658 * call completeFib when we are done processing the command
659 * and want to send a response back to the adapter. This will
660 * send the completed cdb to the adapter.
661 */
662 if (hw_fib->header.XferState & cpu_to_le32(SentFromAdapter)) {
Mark Haverkamp8e0c5eb2005-10-24 10:52:22 -0700663 if (dev->new_comm_interface) {
664 kfree (hw_fib);
665 } else {
666 u32 index;
667 hw_fib->header.XferState |= cpu_to_le32(HostProcessed);
668 if (size) {
669 size += sizeof(struct aac_fibhdr);
670 if (size > le16_to_cpu(hw_fib->header.SenderSize))
671 return -EMSGSIZE;
672 hw_fib->header.Size = cpu_to_le16(size);
673 }
674 q = &dev->queues->queue[AdapNormRespQueue];
675 spin_lock_irqsave(q->lock, qflags);
676 aac_queue_get(dev, &index, AdapNormRespQueue, hw_fib, 1, NULL, &nointr);
677 *(q->headers.producer) = cpu_to_le32(index + 1);
678 spin_unlock_irqrestore(q->lock, qflags);
679 if (!(nointr & (int)aac_config.irq_mod))
680 aac_adapter_notify(dev, AdapNormRespQueue);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700681 }
682 }
683 else
684 {
Mark Haverkampbfb35aa82006-02-01 09:30:55 -0800685 printk(KERN_WARNING "aac_fib_adapter_complete: Unknown xferstate detected.\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700686 BUG();
687 }
688 return 0;
689}
690
691/**
Mark Haverkampbfb35aa82006-02-01 09:30:55 -0800692 * aac_fib_complete - fib completion handler
Linus Torvalds1da177e2005-04-16 15:20:36 -0700693 * @fib: FIB to complete
694 *
695 * Will do all necessary work to complete a FIB.
696 */
697
Mark Haverkampbfb35aa82006-02-01 09:30:55 -0800698int aac_fib_complete(struct fib *fibptr)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700699{
700 struct hw_fib * hw_fib = fibptr->hw_fib;
701
702 /*
703 * Check for a fib which has already been completed
704 */
705
706 if (hw_fib->header.XferState == 0)
707 return 0;
708 /*
709 * If we plan to do anything check the structure type first.
710 */
711
712 if (hw_fib->header.StructType != FIB_MAGIC)
713 return -EINVAL;
714 /*
715 * This block completes a cdb which orginated on the host and we
716 * just need to deallocate the cdb or reinit it. At this point the
717 * command is complete that we had sent to the adapter and this
718 * cdb could be reused.
719 */
720 if((hw_fib->header.XferState & cpu_to_le32(SentFromHost)) &&
721 (hw_fib->header.XferState & cpu_to_le32(AdapterProcessed)))
722 {
723 fib_dealloc(fibptr);
724 }
725 else if(hw_fib->header.XferState & cpu_to_le32(SentFromHost))
726 {
727 /*
728 * This handles the case when the host has aborted the I/O
729 * to the adapter because the adapter is not responding
730 */
731 fib_dealloc(fibptr);
732 } else if(hw_fib->header.XferState & cpu_to_le32(HostOwned)) {
733 fib_dealloc(fibptr);
734 } else {
735 BUG();
736 }
737 return 0;
738}
739
740/**
741 * aac_printf - handle printf from firmware
742 * @dev: Adapter
743 * @val: Message info
744 *
745 * Print a message passed to us by the controller firmware on the
746 * Adaptec board
747 */
748
749void aac_printf(struct aac_dev *dev, u32 val)
750{
Linus Torvalds1da177e2005-04-16 15:20:36 -0700751 char *cp = dev->printfbuf;
Mark Haverkamp 7c00ffa2005-05-16 18:28:42 -0700752 if (dev->printf_enabled)
753 {
754 int length = val & 0xffff;
755 int level = (val >> 16) & 0xffff;
756
757 /*
758 * The size of the printfbuf is set in port.c
759 * There is no variable or define for it
760 */
761 if (length > 255)
762 length = 255;
763 if (cp[length] != 0)
764 cp[length] = 0;
765 if (level == LOG_AAC_HIGH_ERROR)
Mark Haverkamp1241f352006-03-27 09:44:23 -0800766 printk(KERN_WARNING "%s:%s", dev->name, cp);
Mark Haverkamp 7c00ffa2005-05-16 18:28:42 -0700767 else
Mark Haverkamp1241f352006-03-27 09:44:23 -0800768 printk(KERN_INFO "%s:%s", dev->name, cp);
Mark Haverkamp 7c00ffa2005-05-16 18:28:42 -0700769 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700770 memset(cp, 0, 256);
771}
772
Mark Haverkamp131256c2005-09-26 13:04:56 -0700773
774/**
775 * aac_handle_aif - Handle a message from the firmware
776 * @dev: Which adapter this fib is from
777 * @fibptr: Pointer to fibptr from adapter
778 *
779 * This routine handles a driver notify fib from the adapter and
780 * dispatches it to the appropriate routine for handling.
781 */
782
Mark Haverkamp31876f32006-03-27 09:43:55 -0800783#define AIF_SNIFF_TIMEOUT (30*HZ)
Mark Haverkamp131256c2005-09-26 13:04:56 -0700784static void aac_handle_aif(struct aac_dev * dev, struct fib * fibptr)
785{
786 struct hw_fib * hw_fib = fibptr->hw_fib;
787 struct aac_aifcmd * aifcmd = (struct aac_aifcmd *)hw_fib->data;
788 int busy;
789 u32 container;
790 struct scsi_device *device;
791 enum {
792 NOTHING,
793 DELETE,
794 ADD,
795 CHANGE
796 } device_config_needed;
797
798 /* Sniff for container changes */
799
800 if (!dev)
801 return;
802 container = (u32)-1;
803
804 /*
805 * We have set this up to try and minimize the number of
806 * re-configures that take place. As a result of this when
807 * certain AIF's come in we will set a flag waiting for another
808 * type of AIF before setting the re-config flag.
809 */
810 switch (le32_to_cpu(aifcmd->command)) {
811 case AifCmdDriverNotify:
812 switch (le32_to_cpu(((u32 *)aifcmd->data)[0])) {
813 /*
814 * Morph or Expand complete
815 */
816 case AifDenMorphComplete:
817 case AifDenVolumeExtendComplete:
818 container = le32_to_cpu(((u32 *)aifcmd->data)[1]);
819 if (container >= dev->maximum_num_containers)
820 break;
821
822 /*
Christoph Hellwigf64a1812005-10-31 18:32:08 +0100823 * Find the scsi_device associated with the SCSI
Mark Haverkamp131256c2005-09-26 13:04:56 -0700824 * address. Make sure we have the right array, and if
825 * so set the flag to initiate a new re-config once we
826 * see an AifEnConfigChange AIF come through.
827 */
828
829 if ((dev != NULL) && (dev->scsi_host_ptr != NULL)) {
830 device = scsi_device_lookup(dev->scsi_host_ptr,
831 CONTAINER_TO_CHANNEL(container),
832 CONTAINER_TO_ID(container),
833 CONTAINER_TO_LUN(container));
834 if (device) {
835 dev->fsa_dev[container].config_needed = CHANGE;
836 dev->fsa_dev[container].config_waiting_on = AifEnConfigChange;
Mark Haverkamp31876f32006-03-27 09:43:55 -0800837 dev->fsa_dev[container].config_waiting_stamp = jiffies;
Mark Haverkamp131256c2005-09-26 13:04:56 -0700838 scsi_device_put(device);
839 }
840 }
841 }
842
843 /*
844 * If we are waiting on something and this happens to be
845 * that thing then set the re-configure flag.
846 */
847 if (container != (u32)-1) {
848 if (container >= dev->maximum_num_containers)
849 break;
Mark Haverkamp31876f32006-03-27 09:43:55 -0800850 if ((dev->fsa_dev[container].config_waiting_on ==
851 le32_to_cpu(*(u32 *)aifcmd->data)) &&
852 time_before(jiffies, dev->fsa_dev[container].config_waiting_stamp + AIF_SNIFF_TIMEOUT))
Mark Haverkamp131256c2005-09-26 13:04:56 -0700853 dev->fsa_dev[container].config_waiting_on = 0;
854 } else for (container = 0;
855 container < dev->maximum_num_containers; ++container) {
Mark Haverkamp31876f32006-03-27 09:43:55 -0800856 if ((dev->fsa_dev[container].config_waiting_on ==
857 le32_to_cpu(*(u32 *)aifcmd->data)) &&
858 time_before(jiffies, dev->fsa_dev[container].config_waiting_stamp + AIF_SNIFF_TIMEOUT))
Mark Haverkamp131256c2005-09-26 13:04:56 -0700859 dev->fsa_dev[container].config_waiting_on = 0;
860 }
861 break;
862
863 case AifCmdEventNotify:
864 switch (le32_to_cpu(((u32 *)aifcmd->data)[0])) {
865 /*
866 * Add an Array.
867 */
868 case AifEnAddContainer:
869 container = le32_to_cpu(((u32 *)aifcmd->data)[1]);
870 if (container >= dev->maximum_num_containers)
871 break;
872 dev->fsa_dev[container].config_needed = ADD;
873 dev->fsa_dev[container].config_waiting_on =
874 AifEnConfigChange;
Mark Haverkamp31876f32006-03-27 09:43:55 -0800875 dev->fsa_dev[container].config_waiting_stamp = jiffies;
Mark Haverkamp131256c2005-09-26 13:04:56 -0700876 break;
877
878 /*
879 * Delete an Array.
880 */
881 case AifEnDeleteContainer:
882 container = le32_to_cpu(((u32 *)aifcmd->data)[1]);
883 if (container >= dev->maximum_num_containers)
884 break;
885 dev->fsa_dev[container].config_needed = DELETE;
886 dev->fsa_dev[container].config_waiting_on =
887 AifEnConfigChange;
Mark Haverkamp31876f32006-03-27 09:43:55 -0800888 dev->fsa_dev[container].config_waiting_stamp = jiffies;
Mark Haverkamp131256c2005-09-26 13:04:56 -0700889 break;
890
891 /*
892 * Container change detected. If we currently are not
893 * waiting on something else, setup to wait on a Config Change.
894 */
895 case AifEnContainerChange:
896 container = le32_to_cpu(((u32 *)aifcmd->data)[1]);
897 if (container >= dev->maximum_num_containers)
898 break;
Mark Haverkamp31876f32006-03-27 09:43:55 -0800899 if (dev->fsa_dev[container].config_waiting_on &&
900 time_before(jiffies, dev->fsa_dev[container].config_waiting_stamp + AIF_SNIFF_TIMEOUT))
Mark Haverkamp131256c2005-09-26 13:04:56 -0700901 break;
902 dev->fsa_dev[container].config_needed = CHANGE;
903 dev->fsa_dev[container].config_waiting_on =
904 AifEnConfigChange;
Mark Haverkamp31876f32006-03-27 09:43:55 -0800905 dev->fsa_dev[container].config_waiting_stamp = jiffies;
Mark Haverkamp131256c2005-09-26 13:04:56 -0700906 break;
907
908 case AifEnConfigChange:
909 break;
910
911 }
912
913 /*
914 * If we are waiting on something and this happens to be
915 * that thing then set the re-configure flag.
916 */
917 if (container != (u32)-1) {
918 if (container >= dev->maximum_num_containers)
919 break;
Mark Haverkamp31876f32006-03-27 09:43:55 -0800920 if ((dev->fsa_dev[container].config_waiting_on ==
921 le32_to_cpu(*(u32 *)aifcmd->data)) &&
922 time_before(jiffies, dev->fsa_dev[container].config_waiting_stamp + AIF_SNIFF_TIMEOUT))
Mark Haverkamp131256c2005-09-26 13:04:56 -0700923 dev->fsa_dev[container].config_waiting_on = 0;
924 } else for (container = 0;
925 container < dev->maximum_num_containers; ++container) {
Mark Haverkamp31876f32006-03-27 09:43:55 -0800926 if ((dev->fsa_dev[container].config_waiting_on ==
927 le32_to_cpu(*(u32 *)aifcmd->data)) &&
928 time_before(jiffies, dev->fsa_dev[container].config_waiting_stamp + AIF_SNIFF_TIMEOUT))
Mark Haverkamp131256c2005-09-26 13:04:56 -0700929 dev->fsa_dev[container].config_waiting_on = 0;
930 }
931 break;
932
933 case AifCmdJobProgress:
934 /*
935 * These are job progress AIF's. When a Clear is being
936 * done on a container it is initially created then hidden from
937 * the OS. When the clear completes we don't get a config
938 * change so we monitor the job status complete on a clear then
939 * wait for a container change.
940 */
941
942 if ((((u32 *)aifcmd->data)[1] == cpu_to_le32(AifJobCtrZero))
943 && ((((u32 *)aifcmd->data)[6] == ((u32 *)aifcmd->data)[5])
944 || (((u32 *)aifcmd->data)[4] == cpu_to_le32(AifJobStsSuccess)))) {
945 for (container = 0;
946 container < dev->maximum_num_containers;
947 ++container) {
948 /*
949 * Stomp on all config sequencing for all
950 * containers?
951 */
952 dev->fsa_dev[container].config_waiting_on =
953 AifEnContainerChange;
954 dev->fsa_dev[container].config_needed = ADD;
Mark Haverkamp31876f32006-03-27 09:43:55 -0800955 dev->fsa_dev[container].config_waiting_stamp =
956 jiffies;
Mark Haverkamp131256c2005-09-26 13:04:56 -0700957 }
958 }
959 if ((((u32 *)aifcmd->data)[1] == cpu_to_le32(AifJobCtrZero))
960 && (((u32 *)aifcmd->data)[6] == 0)
961 && (((u32 *)aifcmd->data)[4] == cpu_to_le32(AifJobStsRunning))) {
962 for (container = 0;
963 container < dev->maximum_num_containers;
964 ++container) {
965 /*
966 * Stomp on all config sequencing for all
967 * containers?
968 */
969 dev->fsa_dev[container].config_waiting_on =
970 AifEnContainerChange;
971 dev->fsa_dev[container].config_needed = DELETE;
Mark Haverkamp31876f32006-03-27 09:43:55 -0800972 dev->fsa_dev[container].config_waiting_stamp =
973 jiffies;
Mark Haverkamp131256c2005-09-26 13:04:56 -0700974 }
975 }
976 break;
977 }
978
979 device_config_needed = NOTHING;
980 for (container = 0; container < dev->maximum_num_containers;
981 ++container) {
Mark Haverkamp31876f32006-03-27 09:43:55 -0800982 if ((dev->fsa_dev[container].config_waiting_on == 0) &&
983 (dev->fsa_dev[container].config_needed != NOTHING) &&
984 time_before(jiffies, dev->fsa_dev[container].config_waiting_stamp + AIF_SNIFF_TIMEOUT)) {
Mark Haverkamp131256c2005-09-26 13:04:56 -0700985 device_config_needed =
986 dev->fsa_dev[container].config_needed;
987 dev->fsa_dev[container].config_needed = NOTHING;
988 break;
989 }
990 }
991 if (device_config_needed == NOTHING)
992 return;
993
994 /*
995 * If we decided that a re-configuration needs to be done,
996 * schedule it here on the way out the door, please close the door
997 * behind you.
998 */
999
1000 busy = 0;
1001
1002
1003 /*
Christoph Hellwigf64a1812005-10-31 18:32:08 +01001004 * Find the scsi_device associated with the SCSI address,
Mark Haverkamp131256c2005-09-26 13:04:56 -07001005 * and mark it as changed, invalidating the cache. This deals
1006 * with changes to existing device IDs.
1007 */
1008
1009 if (!dev || !dev->scsi_host_ptr)
1010 return;
1011 /*
Mark Haverkampbfb35aa82006-02-01 09:30:55 -08001012 * force reload of disk info via aac_probe_container
Mark Haverkamp131256c2005-09-26 13:04:56 -07001013 */
1014 if ((device_config_needed == CHANGE)
1015 && (dev->fsa_dev[container].valid == 1))
1016 dev->fsa_dev[container].valid = 2;
1017 if ((device_config_needed == CHANGE) ||
1018 (device_config_needed == ADD))
Mark Haverkampbfb35aa82006-02-01 09:30:55 -08001019 aac_probe_container(dev, container);
Mark Haverkamp131256c2005-09-26 13:04:56 -07001020 device = scsi_device_lookup(dev->scsi_host_ptr,
1021 CONTAINER_TO_CHANNEL(container),
1022 CONTAINER_TO_ID(container),
1023 CONTAINER_TO_LUN(container));
1024 if (device) {
1025 switch (device_config_needed) {
1026 case DELETE:
1027 scsi_remove_device(device);
1028 break;
1029 case CHANGE:
1030 if (!dev->fsa_dev[container].valid) {
1031 scsi_remove_device(device);
1032 break;
1033 }
1034 scsi_rescan_device(&device->sdev_gendev);
1035
1036 default:
1037 break;
1038 }
1039 scsi_device_put(device);
1040 }
1041 if (device_config_needed == ADD) {
1042 scsi_add_device(dev->scsi_host_ptr,
1043 CONTAINER_TO_CHANNEL(container),
1044 CONTAINER_TO_ID(container),
1045 CONTAINER_TO_LUN(container));
1046 }
1047
1048}
1049
Linus Torvalds1da177e2005-04-16 15:20:36 -07001050/**
1051 * aac_command_thread - command processing thread
1052 * @dev: Adapter to monitor
1053 *
1054 * Waits on the commandready event in it's queue. When the event gets set
1055 * it will pull FIBs off it's queue. It will continue to pull FIBs off
1056 * until the queue is empty. When the queue is empty it will wait for
1057 * more FIBs.
1058 */
1059
Christoph Hellwigfe273812006-02-14 18:45:06 +01001060int aac_command_thread(void *data)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001061{
Christoph Hellwigfe273812006-02-14 18:45:06 +01001062 struct aac_dev *dev = data;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001063 struct hw_fib *hw_fib, *hw_newfib;
1064 struct fib *fib, *newfib;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001065 struct aac_fib_context *fibctx;
1066 unsigned long flags;
1067 DECLARE_WAITQUEUE(wait, current);
1068
1069 /*
1070 * We can only have one thread per adapter for AIF's.
1071 */
1072 if (dev->aif_thread)
1073 return -EINVAL;
Christoph Hellwigfe273812006-02-14 18:45:06 +01001074
Linus Torvalds1da177e2005-04-16 15:20:36 -07001075 /*
1076 * Let the DPC know it has a place to send the AIF's to.
1077 */
1078 dev->aif_thread = 1;
Mark Haverkamp2f1309802005-09-26 13:02:15 -07001079 add_wait_queue(&dev->queues->queue[HostNormCmdQueue].cmdready, &wait);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001080 set_current_state(TASK_INTERRUPTIBLE);
Mark Haverkamp2f1309802005-09-26 13:02:15 -07001081 dprintk ((KERN_INFO "aac_command_thread start\n"));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001082 while(1)
1083 {
Mark Haverkamp2f1309802005-09-26 13:02:15 -07001084 spin_lock_irqsave(dev->queues->queue[HostNormCmdQueue].lock, flags);
1085 while(!list_empty(&(dev->queues->queue[HostNormCmdQueue].cmdq))) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001086 struct list_head *entry;
1087 struct aac_aifcmd * aifcmd;
1088
1089 set_current_state(TASK_RUNNING);
Mark Haverkamp2f1309802005-09-26 13:02:15 -07001090
1091 entry = dev->queues->queue[HostNormCmdQueue].cmdq.next;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001092 list_del(entry);
Mark Haverkamp2f1309802005-09-26 13:02:15 -07001093
1094 spin_unlock_irqrestore(dev->queues->queue[HostNormCmdQueue].lock, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001095 fib = list_entry(entry, struct fib, fiblink);
1096 /*
1097 * We will process the FIB here or pass it to a
1098 * worker thread that is TBD. We Really can't
1099 * do anything at this point since we don't have
1100 * anything defined for this thread to do.
1101 */
1102 hw_fib = fib->hw_fib;
1103 memset(fib, 0, sizeof(struct fib));
1104 fib->type = FSAFS_NTC_FIB_CONTEXT;
1105 fib->size = sizeof( struct fib );
1106 fib->hw_fib = hw_fib;
1107 fib->data = hw_fib->data;
1108 fib->dev = dev;
1109 /*
1110 * We only handle AifRequest fibs from the adapter.
1111 */
1112 aifcmd = (struct aac_aifcmd *) hw_fib->data;
1113 if (aifcmd->command == cpu_to_le32(AifCmdDriverNotify)) {
1114 /* Handle Driver Notify Events */
Mark Haverkamp131256c2005-09-26 13:04:56 -07001115 aac_handle_aif(dev, fib);
Mark Haverkamp 56b58712005-04-27 06:05:51 -07001116 *(__le32 *)hw_fib->data = cpu_to_le32(ST_OK);
Mark Haverkampbfb35aa82006-02-01 09:30:55 -08001117 aac_fib_adapter_complete(fib, (u16)sizeof(u32));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001118 } else {
1119 struct list_head *entry;
1120 /* The u32 here is important and intended. We are using
1121 32bit wrapping time to fit the adapter field */
1122
1123 u32 time_now, time_last;
1124 unsigned long flagv;
Mark Haverkamp2f1309802005-09-26 13:02:15 -07001125 unsigned num;
1126 struct hw_fib ** hw_fib_pool, ** hw_fib_p;
1127 struct fib ** fib_pool, ** fib_p;
Mark Haverkamp131256c2005-09-26 13:04:56 -07001128
1129 /* Sniff events */
1130 if ((aifcmd->command ==
1131 cpu_to_le32(AifCmdEventNotify)) ||
1132 (aifcmd->command ==
1133 cpu_to_le32(AifCmdJobProgress))) {
1134 aac_handle_aif(dev, fib);
1135 }
1136
Linus Torvalds1da177e2005-04-16 15:20:36 -07001137 time_now = jiffies/HZ;
1138
Mark Haverkamp2f1309802005-09-26 13:02:15 -07001139 /*
1140 * Warning: no sleep allowed while
1141 * holding spinlock. We take the estimate
1142 * and pre-allocate a set of fibs outside the
1143 * lock.
1144 */
1145 num = le32_to_cpu(dev->init->AdapterFibsSize)
1146 / sizeof(struct hw_fib); /* some extra */
1147 spin_lock_irqsave(&dev->fib_lock, flagv);
1148 entry = dev->fib_list.next;
1149 while (entry != &dev->fib_list) {
1150 entry = entry->next;
1151 ++num;
1152 }
1153 spin_unlock_irqrestore(&dev->fib_lock, flagv);
1154 hw_fib_pool = NULL;
1155 fib_pool = NULL;
1156 if (num
1157 && ((hw_fib_pool = kmalloc(sizeof(struct hw_fib *) * num, GFP_KERNEL)))
1158 && ((fib_pool = kmalloc(sizeof(struct fib *) * num, GFP_KERNEL)))) {
1159 hw_fib_p = hw_fib_pool;
1160 fib_p = fib_pool;
1161 while (hw_fib_p < &hw_fib_pool[num]) {
1162 if (!(*(hw_fib_p++) = kmalloc(sizeof(struct hw_fib), GFP_KERNEL))) {
1163 --hw_fib_p;
1164 break;
1165 }
1166 if (!(*(fib_p++) = kmalloc(sizeof(struct fib), GFP_KERNEL))) {
1167 kfree(*(--hw_fib_p));
1168 break;
1169 }
1170 }
1171 if ((num = hw_fib_p - hw_fib_pool) == 0) {
1172 kfree(fib_pool);
1173 fib_pool = NULL;
1174 kfree(hw_fib_pool);
1175 hw_fib_pool = NULL;
1176 }
Jesper Juhlc9475cb2005-11-07 01:01:26 -08001177 } else {
Mark Haverkamp2f1309802005-09-26 13:02:15 -07001178 kfree(hw_fib_pool);
1179 hw_fib_pool = NULL;
1180 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001181 spin_lock_irqsave(&dev->fib_lock, flagv);
1182 entry = dev->fib_list.next;
1183 /*
1184 * For each Context that is on the
1185 * fibctxList, make a copy of the
1186 * fib, and then set the event to wake up the
1187 * thread that is waiting for it.
1188 */
Mark Haverkamp2f1309802005-09-26 13:02:15 -07001189 hw_fib_p = hw_fib_pool;
1190 fib_p = fib_pool;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001191 while (entry != &dev->fib_list) {
1192 /*
1193 * Extract the fibctx
1194 */
1195 fibctx = list_entry(entry, struct aac_fib_context, next);
1196 /*
1197 * Check if the queue is getting
1198 * backlogged
1199 */
1200 if (fibctx->count > 20)
1201 {
1202 /*
1203 * It's *not* jiffies folks,
1204 * but jiffies / HZ so do not
1205 * panic ...
1206 */
1207 time_last = fibctx->jiffies;
1208 /*
1209 * Has it been > 2 minutes
1210 * since the last read off
1211 * the queue?
1212 */
Mark Haverkamp404d9a92006-05-10 09:12:48 -07001213 if ((time_now - time_last) > aif_timeout) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001214 entry = entry->next;
1215 aac_close_fib_context(dev, fibctx);
1216 continue;
1217 }
1218 }
1219 /*
1220 * Warning: no sleep allowed while
1221 * holding spinlock
1222 */
Mark Haverkamp2f1309802005-09-26 13:02:15 -07001223 if (hw_fib_p < &hw_fib_pool[num]) {
1224 hw_newfib = *hw_fib_p;
1225 *(hw_fib_p++) = NULL;
1226 newfib = *fib_p;
1227 *(fib_p++) = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001228 /*
1229 * Make the copy of the FIB
1230 */
1231 memcpy(hw_newfib, hw_fib, sizeof(struct hw_fib));
1232 memcpy(newfib, fib, sizeof(struct fib));
1233 newfib->hw_fib = hw_newfib;
1234 /*
1235 * Put the FIB onto the
1236 * fibctx's fibs
1237 */
1238 list_add_tail(&newfib->fiblink, &fibctx->fib_list);
1239 fibctx->count++;
1240 /*
1241 * Set the event to wake up the
Mark Haverkamp2f1309802005-09-26 13:02:15 -07001242 * thread that is waiting.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001243 */
1244 up(&fibctx->wait_sem);
1245 } else {
1246 printk(KERN_WARNING "aifd: didn't allocate NewFib.\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07001247 }
1248 entry = entry->next;
1249 }
1250 /*
1251 * Set the status of this FIB
1252 */
Mark Haverkamp 56b58712005-04-27 06:05:51 -07001253 *(__le32 *)hw_fib->data = cpu_to_le32(ST_OK);
Mark Haverkampbfb35aa82006-02-01 09:30:55 -08001254 aac_fib_adapter_complete(fib, sizeof(u32));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001255 spin_unlock_irqrestore(&dev->fib_lock, flagv);
Mark Haverkamp2f1309802005-09-26 13:02:15 -07001256 /* Free up the remaining resources */
1257 hw_fib_p = hw_fib_pool;
1258 fib_p = fib_pool;
1259 while (hw_fib_p < &hw_fib_pool[num]) {
Jesper Juhlc9475cb2005-11-07 01:01:26 -08001260 kfree(*hw_fib_p);
1261 kfree(*fib_p);
Mark Haverkamp2f1309802005-09-26 13:02:15 -07001262 ++fib_p;
1263 ++hw_fib_p;
1264 }
Jesper Juhlc9475cb2005-11-07 01:01:26 -08001265 kfree(hw_fib_pool);
1266 kfree(fib_pool);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001267 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001268 kfree(fib);
Mark Haverkamp2f1309802005-09-26 13:02:15 -07001269 spin_lock_irqsave(dev->queues->queue[HostNormCmdQueue].lock, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001270 }
1271 /*
1272 * There are no more AIF's
1273 */
Mark Haverkamp2f1309802005-09-26 13:02:15 -07001274 spin_unlock_irqrestore(dev->queues->queue[HostNormCmdQueue].lock, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001275 schedule();
1276
Christoph Hellwigfe273812006-02-14 18:45:06 +01001277 if (kthread_should_stop())
Linus Torvalds1da177e2005-04-16 15:20:36 -07001278 break;
1279 set_current_state(TASK_INTERRUPTIBLE);
1280 }
Mark Haverkamp2f1309802005-09-26 13:02:15 -07001281 if (dev->queues)
1282 remove_wait_queue(&dev->queues->queue[HostNormCmdQueue].cmdready, &wait);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001283 dev->aif_thread = 0;
Mark Haverkamp2f1309802005-09-26 13:02:15 -07001284 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001285}