~ubuntu-branches/ubuntu/utopic/xen/utopic

« back to all changes in this revision

Viewing changes to xen/drivers/scsi/aacraid/commsup.c

  • Committer: Bazaar Package Importer
  • Author(s): Bastian Blank
  • Date: 2010-05-06 15:47:38 UTC
  • mto: (1.3.1) (15.1.1 sid) (4.1.1 experimental)
  • mto: This revision was merged to the branch mainline in revision 3.
  • Revision ID: james.westby@ubuntu.com-20100506154738-agoz0rlafrh1fnq7
Tags: upstream-4.0.0
ImportĀ upstreamĀ versionĀ 4.0.0

Show diffs side-by-side

added added

removed removed

Lines of Context:
1
 
/*
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
 
 
7
 
 * Adaptec aacraid device driver for Linux.
8
 
 *
9
 
 * Copyright (c) 2000 Adaptec, Inc. (aacraid@adaptec.com)
10
 
 *
11
 
 * This program is free software; you can redistribute it and/or modify
12
 
 * it under the terms of the GNU General Public License as published by
13
 
 * the Free Software Foundation; either version 2, or (at your option)
14
 
 * any later version.
15
 
 *
16
 
 * This program is distributed in the hope that it will be useful,
17
 
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
18
 
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
19
 
 * GNU General Public License for more details.
20
 
 *
21
 
 * You should have received a copy of the GNU General Public License
22
 
 * along with this program; see the file COPYING.  If not, write to
23
 
 * the Free Software Foundation, 675 Mass Ave, Cambridge, MA 02139, USA.
24
 
 *
25
 
 * Module Name:
26
 
 *  commsup.c
27
 
 *
28
 
 * Abstract: Contain all routines that are required for FSA host/adapter
29
 
 *    commuication.
30
 
 *
31
 
 *
32
 
 */
33
 
 
34
 
#include <linux/config.h>
35
 
#include <linux/kernel.h>
36
 
#include <linux/init.h>
37
 
#include <linux/types.h>
38
 
#include <linux/sched.h>
39
 
#include <linux/pci.h>
40
 
#include <linux/spinlock.h>
41
 
#include <linux/slab.h>
42
 
/*#include <linux/completion.h>*/
43
 
/*#include <asm/semaphore.h>*/
44
 
#include <linux/blk.h>
45
 
#include <asm/uaccess.h>
46
 
 
47
 
#include <xeno/interrupt.h>
48
 
#include <xeno/delay.h>
49
 
 
50
 
#include "scsi.h"
51
 
#include "hosts.h"
52
 
 
53
 
#include "aacraid.h"
54
 
 
55
 
/**
56
 
 *      fib_map_alloc           -       allocate the fib objects
57
 
 *      @dev: Adapter to allocate for
58
 
 *
59
 
 *      Allocate and map the shared PCI space for the FIB blocks used to
60
 
 *      talk to the Adaptec firmware.
61
 
 */
62
 
 
63
 
static int fib_map_alloc(struct aac_dev *dev)
64
 
{
65
 
        if((dev->hw_fib_va = pci_alloc_consistent(dev->pdev, sizeof(struct hw_fib) * AAC_NUM_FIB, &dev->hw_fib_pa))==NULL)
66
 
                return -ENOMEM;
67
 
        return 0;
68
 
}
69
 
 
70
 
/**
71
 
 *      fib_map_free            -       free the fib objects
72
 
 *      @dev: Adapter to free
73
 
 *
74
 
 *      Free the PCI mappings and the memory allocated for FIB blocks
75
 
 *      on this adapter.
76
 
 */
77
 
 
78
 
void fib_map_free(struct aac_dev *dev)
79
 
{
80
 
        pci_free_consistent(dev->pdev, sizeof(struct hw_fib) * AAC_NUM_FIB, dev->hw_fib_va, dev->hw_fib_pa);
81
 
}
82
 
 
83
 
/**
84
 
 *      fib_setup       -       setup the fibs
85
 
 *      @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
 
 
91
 
int fib_setup(struct aac_dev * dev)
92
 
{
93
 
        struct fib *fibptr;
94
 
        struct hw_fib *hw_fib_va;
95
 
        dma_addr_t hw_fib_pa;
96
 
        int i;
97
 
        
98
 
        if(fib_map_alloc(dev)<0)
99
 
                return -ENOMEM;
100
 
                
101
 
        hw_fib_va = dev->hw_fib_va;
102
 
        hw_fib_pa = dev->hw_fib_pa;
103
 
        memset(hw_fib_va, 0, sizeof(struct hw_fib) * AAC_NUM_FIB);
104
 
        /*
105
 
         *      Initialise the fibs
106
 
         */
107
 
        for (i = 0, fibptr = &dev->fibs[i]; i < AAC_NUM_FIB; i++, fibptr++) 
108
 
        {
109
 
                fibptr->dev = dev;
110
 
                fibptr->hw_fib = hw_fib_va;
111
 
                fibptr->data = (void *) fibptr->hw_fib->data;
112
 
                fibptr->next = fibptr+1;        /* Forward chain the fibs */
113
 
#if 0
114
 
                init_MUTEX_LOCKED(&fibptr->event_wait);
115
 
#endif
116
 
                spin_lock_init(&fibptr->event_lock);
117
 
                hw_fib_va->header.XferState = cpu_to_le32(0xffffffff);
118
 
                hw_fib_va->header.SenderSize = cpu_to_le16(sizeof(struct hw_fib));
119
 
                fibptr->hw_fib_pa = hw_fib_pa;
120
 
                hw_fib_va = (struct hw_fib *)((unsigned char *)hw_fib_va + sizeof(struct hw_fib));
121
 
                hw_fib_pa = hw_fib_pa + sizeof(struct hw_fib); 
122
 
        }
123
 
        /*
124
 
         *      Add the fib chain to the free list
125
 
         */
126
 
        dev->fibs[AAC_NUM_FIB-1].next = NULL;
127
 
        /*
128
 
         *      Enable this to debug out of queue space
129
 
         */
130
 
        dev->free_fib = &dev->fibs[0];
131
 
        return 0;
132
 
}
133
 
 
134
 
/**
135
 
 *      fib_alloc       -       allocate a fib
136
 
 *      @dev: Adapter to allocate the fib for
137
 
 *
138
 
 *      Allocate a fib from the adapter fib pool. If the pool is empty we
139
 
 *      wait for fibs to become free.
140
 
 */
141
 
 
142
 
struct fib * fib_alloc(struct aac_dev *dev)
143
 
{
144
 
        struct fib * fibptr;
145
 
        unsigned long flags;
146
 
        
147
 
        spin_lock_irqsave(&dev->fib_lock, flags);
148
 
        fibptr = dev->free_fib; 
149
 
        if(!fibptr)
150
 
                BUG();
151
 
        dev->free_fib = fibptr->next;
152
 
        spin_unlock_irqrestore(&dev->fib_lock, flags);
153
 
        /*
154
 
         *      Set the proper node type code and node byte size
155
 
         */
156
 
        fibptr->type = FSAFS_NTC_FIB_CONTEXT;
157
 
        fibptr->size = sizeof(struct fib);
158
 
        /*
159
 
         *      Null out fields that depend on being zero at the start of
160
 
         *      each I/O
161
 
         */
162
 
        fibptr->hw_fib->header.XferState = cpu_to_le32(0);
163
 
        fibptr->callback = NULL;
164
 
        fibptr->callback_data = NULL;
165
 
 
166
 
        return fibptr;
167
 
}
168
 
 
169
 
/**
170
 
 *      fib_free        -       free a fib
171
 
 *      @fibptr: fib to free up
172
 
 *
173
 
 *      Frees up a fib and places it on the appropriate queue
174
 
 *      (either free or timed out)
175
 
 */
176
 
 
177
 
void fib_free(struct fib * fibptr)
178
 
{
179
 
        unsigned long flags;
180
 
 
181
 
        spin_lock_irqsave(&fibptr->dev->fib_lock, flags);
182
 
 
183
 
        if (fibptr->flags & FIB_CONTEXT_FLAG_TIMED_OUT) {
184
 
                aac_config.fib_timeouts++;
185
 
                fibptr->next = fibptr->dev->timeout_fib;
186
 
                fibptr->dev->timeout_fib = fibptr;
187
 
        } else {
188
 
                if (fibptr->hw_fib->header.XferState != 0) {
189
 
                        printk(KERN_WARNING "fib_free, XferState != 0, fibptr = 0x%p, XferState = 0x%x\n", 
190
 
                                 (void*)fibptr, fibptr->hw_fib->header.XferState);
191
 
                }
192
 
                fibptr->next = fibptr->dev->free_fib;
193
 
                fibptr->dev->free_fib = fibptr;
194
 
        }       
195
 
        spin_unlock_irqrestore(&fibptr->dev->fib_lock, flags);
196
 
}
197
 
 
198
 
/**
199
 
 *      fib_init        -       initialise a fib
200
 
 *      @fibptr: The fib to initialize
201
 
 *      
202
 
 *      Set up the generic fib fields ready for use
203
 
 */
204
 
 
205
 
void fib_init(struct fib *fibptr)
206
 
{
207
 
        struct hw_fib *hw_fib = fibptr->hw_fib;
208
 
 
209
 
        hw_fib->header.StructType = FIB_MAGIC;
210
 
        hw_fib->header.Size = cpu_to_le16(sizeof(struct hw_fib));
211
 
        hw_fib->header.XferState = cpu_to_le32(HostOwned | FibInitialized | FibEmpty | FastResponseCapable);
212
 
        hw_fib->header.SenderFibAddress = cpu_to_le32(fibptr->hw_fib_pa);
213
 
        hw_fib->header.ReceiverFibAddress = cpu_to_le32(fibptr->hw_fib_pa);
214
 
        hw_fib->header.SenderSize = cpu_to_le16(sizeof(struct hw_fib));
215
 
}
216
 
 
217
 
/**
218
 
 *      fib_deallocate          -       deallocate a fib
219
 
 *      @fibptr: fib to deallocate
220
 
 *
221
 
 *      Will deallocate and return to the free pool the FIB pointed to by the
222
 
 *      caller.
223
 
 */
224
 
 
225
 
void fib_dealloc(struct fib * fibptr)
226
 
{
227
 
        struct hw_fib *hw_fib = fibptr->hw_fib;
228
 
        if(hw_fib->header.StructType != FIB_MAGIC) 
229
 
                BUG();
230
 
        hw_fib->header.XferState = cpu_to_le32(0);        
231
 
}
232
 
 
233
 
/*
234
 
 *      Commuication primitives define and support the queuing method we use to
235
 
 *      support host to adapter commuication. All queue accesses happen through
236
 
 *      these routines and are the only routines which have a knowledge of the
237
 
 *       how these queues are implemented.
238
 
 */
239
 
 
240
 
/**
241
 
 *      aac_get_entry           -       get a queue entry
242
 
 *      @dev: Adapter
243
 
 *      @qid: Queue Number
244
 
 *      @entry: Entry return
245
 
 *      @index: Index return
246
 
 *      @nonotify: notification control
247
 
 *
248
 
 *      With a priority the routine returns a queue entry if the queue has free entries. If the queue
249
 
 *      is full(no free entries) than no entry is returned and the function returns 0 otherwise 1 is
250
 
 *      returned.
251
 
 */
252
 
 
253
 
static int aac_get_entry (struct aac_dev * dev, u32 qid, struct aac_entry **entry, u32 * index, unsigned long *nonotify)
254
 
{
255
 
        struct aac_queue * q;
256
 
 
257
 
        /*
258
 
         *      All of the queues wrap when they reach the end, so we check
259
 
         *      to see if they have reached the end and if they have we just
260
 
         *      set the index back to zero. This is a wrap. You could or off
261
 
         *      the high bits in all updates but this is a bit faster I think.
262
 
         */
263
 
 
264
 
        q = &dev->queues->queue[qid];
265
 
        
266
 
        *index = le32_to_cpu(*(q->headers.producer));
267
 
        if ((*index - 2) == le32_to_cpu(*(q->headers.consumer)))
268
 
                        *nonotify = 1; 
269
 
 
270
 
        if (qid == AdapHighCmdQueue) {
271
 
                if (*index >= ADAP_HIGH_CMD_ENTRIES)
272
 
                        *index = 0;
273
 
        } else if (qid == AdapNormCmdQueue) {
274
 
                if (*index >= ADAP_NORM_CMD_ENTRIES) 
275
 
                        *index = 0; /* Wrap to front of the Producer Queue. */
276
 
        }
277
 
        else if (qid == AdapHighRespQueue) 
278
 
        {
279
 
                if (*index >= ADAP_HIGH_RESP_ENTRIES)
280
 
                        *index = 0;
281
 
        }
282
 
        else if (qid == AdapNormRespQueue) 
283
 
        {
284
 
                if (*index >= ADAP_NORM_RESP_ENTRIES) 
285
 
                        *index = 0; /* Wrap to front of the Producer Queue. */
286
 
        }
287
 
        else BUG();
288
 
 
289
 
        if (*index + 1 == le32_to_cpu(*(q->headers.consumer))) { /* Queue is full */
290
 
                printk(KERN_WARNING "Queue %d full, %ld outstanding.\n", qid, q->numpending);
291
 
                return 0;
292
 
        } else {
293
 
                *entry = q->base + *index;
294
 
                return 1;
295
 
        }
296
 
}   
297
 
 
298
 
/**
299
 
 *      aac_queue_get           -       get the next free QE
300
 
 *      @dev: Adapter
301
 
 *      @index: Returned index
302
 
 *      @priority: Priority of fib
303
 
 *      @fib: Fib to associate with the queue entry
304
 
 *      @wait: Wait if queue full
305
 
 *      @fibptr: Driver fib object to go with fib
306
 
 *      @nonotify: Don't notify the adapter
307
 
 *
308
 
 *      Gets the next free QE off the requested priorty adapter command
309
 
 *      queue and associates the Fib with the QE. The QE represented by
310
 
 *      index is ready to insert on the queue when this routine returns
311
 
 *      success.
312
 
 */
313
 
 
314
 
static 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)
315
 
{
316
 
        struct aac_entry * entry = NULL;
317
 
        int map = 0;
318
 
        struct aac_queue * q = &dev->queues->queue[qid];
319
 
                
320
 
        spin_lock_irqsave(q->lock, q->SavedIrql);
321
 
            
322
 
        if (qid == AdapHighCmdQueue || qid == AdapNormCmdQueue) 
323
 
        {
324
 
                /*  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;
334
 
        }
335
 
        else if (qid == AdapHighRespQueue || qid == AdapNormRespQueue)
336
 
        {
337
 
                while(!aac_get_entry(dev, qid, &entry, index, nonotify)) 
338
 
                {
339
 
                        /* if no entries wait for some if caller wants to */
340
 
                }
341
 
                /*
342
 
                 *      Setup queue entry with command, status and fib mapped
343
 
                 */
344
 
                entry->size = cpu_to_le32(le16_to_cpu(hw_fib->header.Size));
345
 
                entry->addr = hw_fib->header.SenderFibAddress;
346
 
                        /* Restore adapters pointer to the FIB */
347
 
                hw_fib->header.ReceiverFibAddress = hw_fib->header.SenderFibAddress;    /* Let the adapter now where to find its data */
348
 
                map = 0;
349
 
        } 
350
 
        /*
351
 
         *      If MapFib is true than we need to map the Fib and put pointers
352
 
         *      in the queue entry.
353
 
         */
354
 
        if (map)
355
 
                entry->addr = fibptr->hw_fib_pa;
356
 
        return 0;
357
 
}
358
 
 
359
 
 
360
 
/**
361
 
 *      aac_insert_entry        -       insert a queue entry
362
 
 *      @dev: Adapter
363
 
 *      @index: Index of entry to insert
364
 
 *      @qid: Queue number
365
 
 *      @nonotify: Suppress adapter notification
366
 
 *
367
 
 *      Gets the next free QE off the requested priorty adapter command
368
 
 *      queue and associates the Fib with the QE. The QE represented by
369
 
 *      index is ready to insert on the queue when this routine returns
370
 
 *      success.
371
 
 */
372
 
 
373
 
static int aac_insert_entry(struct aac_dev * dev, u32 index, u32 qid, unsigned long nonotify) 
374
 
{
375
 
        struct aac_queue * q = &dev->queues->queue[qid];
376
 
 
377
 
        if(q == NULL)
378
 
                BUG();
379
 
        *(q->headers.producer) = cpu_to_le32(index + 1);
380
 
        spin_unlock_irqrestore(q->lock, q->SavedIrql);
381
 
 
382
 
        if (qid == AdapHighCmdQueue ||
383
 
            qid == AdapNormCmdQueue ||
384
 
            qid == AdapHighRespQueue ||
385
 
            qid == AdapNormRespQueue)
386
 
        {
387
 
                if (!nonotify)
388
 
                        aac_adapter_notify(dev, qid);
389
 
        }
390
 
        else
391
 
                printk("Suprise insert!\n");
392
 
        return 0;
393
 
}
394
 
 
395
 
/*
396
 
 *      Define the highest level of host to adapter communication routines. 
397
 
 *      These routines will support host to adapter FS commuication. These 
398
 
 *      routines have no knowledge of the commuication method used. This level
399
 
 *      sends and receives FIBs. This level has no knowledge of how these FIBs
400
 
 *      get passed back and forth.
401
 
 */
402
 
 
403
 
/**
404
 
 *      fib_send        -       send a fib to the adapter
405
 
 *      @command: Command to send
406
 
 *      @fibptr: The fib
407
 
 *      @size: Size of fib data area
408
 
 *      @priority: Priority of Fib
409
 
 *      @wait: Async/sync select
410
 
 *      @reply: True if a reply is wanted
411
 
 *      @callback: Called with reply
412
 
 *      @callback_data: Passed to callback
413
 
 *
414
 
 *      Sends the requested FIB to the adapter and optionally will wait for a
415
 
 *      response FIB. If the caller does not wish to wait for a response than
416
 
 *      an event to wait on must be supplied. This event will be set when a
417
 
 *      response FIB is received from the adapter.
418
 
 */
419
 
 
420
 
int fib_send(u16 command, struct fib * fibptr, unsigned long size,  int priority, int wait, int reply, fib_callback callback, void * callback_data)
421
 
{
422
 
        u32 index;
423
 
        u32 qid;
424
 
        struct aac_dev * dev = fibptr->dev;
425
 
        unsigned long nointr = 0;
426
 
        struct hw_fib * hw_fib = fibptr->hw_fib;
427
 
        struct aac_queue * q;
428
 
        unsigned long flags = 0;
429
 
 
430
 
        if (!(le32_to_cpu(hw_fib->header.XferState) & HostOwned))
431
 
                return -EBUSY;
432
 
        /*
433
 
         *      There are 5 cases with the wait and reponse requested flags. 
434
 
         *      The only invalid cases are if the caller requests to wait and
435
 
         *      does not request a response and if the caller does not want a
436
 
         *      response and the Fibis not allocated from pool. If a response
437
 
         *      is not requesed the Fib will just be deallocaed by the DPC
438
 
         *      routine when the response comes back from the adapter. No
439
 
         *      further processing will be done besides deleting the Fib. We 
440
 
         *      will have a debug mode where the adapter can notify the host
441
 
         *      it had a problem and the host can log that fact.
442
 
         */
443
 
        if (wait && !reply) {
444
 
                return -EINVAL;
445
 
        } else if (!wait && reply) {
446
 
                hw_fib->header.XferState |= cpu_to_le32(Async | ResponseExpected);
447
 
                FIB_COUNTER_INCREMENT(aac_config.AsyncSent);
448
 
        } else if (!wait && !reply) {
449
 
                hw_fib->header.XferState |= cpu_to_le32(NoResponseExpected);
450
 
                FIB_COUNTER_INCREMENT(aac_config.NoResponseSent);
451
 
        } else if (wait && reply) {
452
 
                hw_fib->header.XferState |= cpu_to_le32(ResponseExpected);
453
 
                FIB_COUNTER_INCREMENT(aac_config.NormalSent);
454
 
        } 
455
 
        /*
456
 
         *      Map the fib into 32bits by using the fib number
457
 
         */
458
 
 
459
 
//      hw_fib->header.SenderFibAddress = ((u32)(fibptr-dev->fibs)) << 1;
460
 
        hw_fib->header.SenderFibAddress = cpu_to_le32((u32)(ulong)fibptr->hw_fib_pa);
461
 
        hw_fib->header.SenderData = (u32)(fibptr - dev->fibs);
462
 
        /*
463
 
         *      Set FIB state to indicate where it came from and if we want a
464
 
         *      response from the adapter. Also load the command from the
465
 
         *      caller.
466
 
         *
467
 
         *      Map the hw fib pointer as a 32bit value
468
 
         */
469
 
        hw_fib->header.Command = cpu_to_le16(command);
470
 
        hw_fib->header.XferState |= cpu_to_le32(SentFromHost);
471
 
        fibptr->hw_fib->header.Flags = 0;       /* 0 the flags field - internal only*/
472
 
        /*
473
 
         *      Set the size of the Fib we want to send to the adapter
474
 
         */
475
 
        hw_fib->header.Size = cpu_to_le16(sizeof(struct aac_fibhdr) + size);
476
 
        if (le16_to_cpu(hw_fib->header.Size) > le16_to_cpu(hw_fib->header.SenderSize)) {
477
 
                return -EMSGSIZE;
478
 
        }                
479
 
        /*
480
 
         *      Get a queue entry connect the FIB to it and send an notify
481
 
         *      the adapter a command is ready.
482
 
         */
483
 
        if (priority == FsaHigh) {
484
 
                hw_fib->header.XferState |= cpu_to_le32(HighPriority);
485
 
                qid = AdapHighCmdQueue;
486
 
        } else {
487
 
                hw_fib->header.XferState |= cpu_to_le32(NormalPriority);
488
 
                qid = AdapNormCmdQueue;
489
 
        }
490
 
        q = &dev->queues->queue[qid];
491
 
 
492
 
        if(wait)
493
 
                spin_lock_irqsave(&fibptr->event_lock, flags);
494
 
        if(aac_queue_get( dev, &index, qid, hw_fib, 1, fibptr, &nointr)<0)
495
 
                return -EWOULDBLOCK;
496
 
        dprintk((KERN_DEBUG "fib_send: inserting a queue entry at index %d.\n",index));
497
 
        dprintk((KERN_DEBUG "Fib contents:.\n"));
498
 
        dprintk((KERN_DEBUG "  Command =               %d.\n", hw_fib->header.Command));
499
 
        dprintk((KERN_DEBUG "  XferState  =            %x.\n", hw_fib->header.XferState));
500
 
        dprintk((KERN_DEBUG "  hw_fib va being sent=%p\n",fibptr->hw_fib));
501
 
        dprintk((KERN_DEBUG "  hw_fib pa being sent=%lx\n",(ulong)fibptr->hw_fib_pa));
502
 
        dprintk((KERN_DEBUG "  fib being sent=%p\n",fibptr));
503
 
        /*
504
 
         *      Fill in the Callback and CallbackContext if we are not
505
 
         *      going to wait.
506
 
         */
507
 
        if (!wait) {
508
 
                fibptr->callback = callback;
509
 
                fibptr->callback_data = callback_data;
510
 
        }
511
 
        FIB_COUNTER_INCREMENT(aac_config.FibsSent);
512
 
        list_add_tail(&fibptr->queue, &q->pendingq);
513
 
        q->numpending++;
514
 
 
515
 
        fibptr->done = 0;
516
 
        fibptr->flags = 0;
517
 
 
518
 
        if(aac_insert_entry(dev, index, qid, (nointr & aac_config.irq_mod)) < 0)
519
 
                return -EWOULDBLOCK;
520
 
        /*
521
 
         *      If the caller wanted us to wait for response wait now. 
522
 
         */
523
 
    
524
 
        if (wait) {
525
 
                spin_unlock_irqrestore(&fibptr->event_lock, flags);
526
 
                while (!fibptr->done)
527
 
                        aac_command_thread(dev); 
528
 
                if((fibptr->flags & FIB_CONTEXT_FLAG_TIMED_OUT))
529
 
                        return -ETIMEDOUT;
530
 
                else
531
 
                        return 0;
532
 
        }
533
 
        /*
534
 
         *      If the user does not want a response than return success otherwise
535
 
         *      return pending
536
 
         */
537
 
        if (reply)
538
 
                return -EINPROGRESS;
539
 
        else
540
 
                return 0;
541
 
}
542
 
 
543
 
/** 
544
 
 *      aac_consumer_get        -       get the top of the queue
545
 
 *      @dev: Adapter
546
 
 *      @q: Queue
547
 
 *      @entry: Return entry
548
 
 *
549
 
 *      Will return a pointer to the entry on the top of the queue requested that
550
 
 *      we are a consumer of, and return the address of the queue entry. It does
551
 
 *      not change the state of the queue. 
552
 
 */
553
 
 
554
 
int aac_consumer_get(struct aac_dev * dev, struct aac_queue * q, struct aac_entry **entry)
555
 
{
556
 
        u32 index;
557
 
        int status;
558
 
        if (le32_to_cpu(*q->headers.producer) == le32_to_cpu(*q->headers.consumer)) {
559
 
                status = 0;
560
 
        } else {
561
 
                /*
562
 
                 *      The consumer index must be wrapped if we have reached
563
 
                 *      the end of the queue, else we just use the entry
564
 
                 *      pointed to by the header index
565
 
                 */
566
 
                if (le32_to_cpu(*q->headers.consumer) >= q->entries) 
567
 
                        index = 0;              
568
 
                else
569
 
                        index = le32_to_cpu(*q->headers.consumer);
570
 
                *entry = q->base + index;
571
 
                status = 1;
572
 
        }
573
 
        return(status);
574
 
}
575
 
 
576
 
int aac_consumer_avail(struct aac_dev *dev, struct aac_queue * q)
577
 
{
578
 
        return (le32_to_cpu(*q->headers.producer) != le32_to_cpu(*q->headers.consumer));
579
 
}
580
 
 
581
 
 
582
 
/**
583
 
 *      aac_consumer_free       -       free consumer entry
584
 
 *      @dev: Adapter
585
 
 *      @q: Queue
586
 
 *      @qid: Queue ident
587
 
 *
588
 
 *      Frees up the current top of the queue we are a consumer of. If the
589
 
 *      queue was full notify the producer that the queue is no longer full.
590
 
 */
591
 
 
592
 
void aac_consumer_free(struct aac_dev * dev, struct aac_queue *q, u32 qid)
593
 
{
594
 
        int wasfull = 0;
595
 
        u32 notify;
596
 
 
597
 
        if ((le32_to_cpu(*q->headers.producer)+1) == le32_to_cpu(*q->headers.consumer))
598
 
                wasfull = 1;
599
 
        
600
 
        if (le32_to_cpu(*q->headers.consumer) >= q->entries)
601
 
                *q->headers.consumer = cpu_to_le32(1);
602
 
        else
603
 
                *q->headers.consumer = cpu_to_le32(le32_to_cpu(*q->headers.consumer)+1);
604
 
        
605
 
        if (wasfull) {
606
 
                switch (qid) {
607
 
 
608
 
                case HostNormCmdQueue:
609
 
                        notify = HostNormCmdNotFull;
610
 
                        break;
611
 
                case HostHighCmdQueue:
612
 
                        notify = HostHighCmdNotFull;
613
 
                        break;
614
 
                case HostNormRespQueue:
615
 
                        notify = HostNormRespNotFull;
616
 
                        break;
617
 
                case HostHighRespQueue:
618
 
                        notify = HostHighRespNotFull;
619
 
                        break;
620
 
                default:
621
 
                        BUG();
622
 
                        return;
623
 
                }
624
 
                aac_adapter_notify(dev, notify);
625
 
        }
626
 
}        
627
 
 
628
 
/**
629
 
 *      fib_adapter_complete    -       complete adapter issued fib
630
 
 *      @fibptr: fib to complete
631
 
 *      @size: size of fib
632
 
 *
633
 
 *      Will do all necessary work to complete a FIB that was sent from
634
 
 *      the adapter.
635
 
 */
636
 
 
637
 
int fib_adapter_complete(struct fib * fibptr, unsigned short size)
638
 
{
639
 
        struct hw_fib * hw_fib = fibptr->hw_fib;
640
 
        struct aac_dev * dev = fibptr->dev;
641
 
        unsigned long nointr = 0;
642
 
        if (le32_to_cpu(hw_fib->header.XferState) == 0)
643
 
                return 0;
644
 
        /*
645
 
         *      If we plan to do anything check the structure type first.
646
 
         */ 
647
 
        if ( hw_fib->header.StructType != FIB_MAGIC ) {
648
 
                return -EINVAL;
649
 
        }
650
 
        /*
651
 
         *      This block handles the case where the adapter had sent us a
652
 
         *      command and we have finished processing the command. We
653
 
         *      call completeFib when we are done processing the command 
654
 
         *      and want to send a response back to the adapter. This will 
655
 
         *      send the completed cdb to the adapter.
656
 
         */
657
 
        if (hw_fib->header.XferState & cpu_to_le32(SentFromAdapter)) {
658
 
                hw_fib->header.XferState |= cpu_to_le32(HostProcessed);
659
 
                if (hw_fib->header.XferState & cpu_to_le32(HighPriority)) {
660
 
                        u32 index;
661
 
                        if (size) 
662
 
                        {
663
 
                                size += sizeof(struct aac_fibhdr);
664
 
                                if (size > le16_to_cpu(hw_fib->header.SenderSize))
665
 
                                        return -EMSGSIZE;
666
 
                                hw_fib->header.Size = cpu_to_le16(size);
667
 
                        }
668
 
                        if(aac_queue_get(dev, &index, AdapHighRespQueue, hw_fib, 1, NULL, &nointr) < 0) {
669
 
                                return -EWOULDBLOCK;
670
 
                        }
671
 
                        if (aac_insert_entry(dev, index, AdapHighRespQueue,  (nointr & (int)aac_config.irq_mod)) != 0) {
672
 
                        }
673
 
                }
674
 
                else if (hw_fib->header.XferState & NormalPriority) 
675
 
                {
676
 
                        u32 index;
677
 
 
678
 
                        if (size) {
679
 
                                size += sizeof(struct aac_fibhdr);
680
 
                                if (size > le16_to_cpu(hw_fib->header.SenderSize)) 
681
 
                                        return -EMSGSIZE;
682
 
                                hw_fib->header.Size = cpu_to_le16(size);
683
 
                        }
684
 
                        if (aac_queue_get(dev, &index, AdapNormRespQueue, hw_fib, 1, NULL, &nointr) < 0) 
685
 
                                return -EWOULDBLOCK;
686
 
                        if (aac_insert_entry(dev, index, AdapNormRespQueue, 
687
 
                                (nointr & (int)aac_config.irq_mod)) != 0) 
688
 
                        {
689
 
                        }
690
 
                }
691
 
        }
692
 
        else 
693
 
        {
694
 
                printk(KERN_WARNING "fib_adapter_complete: Unknown xferstate detected.\n");
695
 
                BUG();
696
 
        }   
697
 
        return 0;
698
 
}
699
 
 
700
 
/**
701
 
 *      fib_complete    -       fib completion handler
702
 
 *      @fib: FIB to complete
703
 
 *
704
 
 *      Will do all necessary work to complete a FIB.
705
 
 */
706
 
 
707
 
int fib_complete(struct fib * fibptr)
708
 
{
709
 
        struct hw_fib * hw_fib = fibptr->hw_fib;
710
 
 
711
 
        /*
712
 
         *      Check for a fib which has already been completed
713
 
         */
714
 
 
715
 
        if (hw_fib->header.XferState == cpu_to_le32(0))
716
 
                return 0;
717
 
        /*
718
 
         *      If we plan to do anything check the structure type first.
719
 
         */ 
720
 
 
721
 
        if (hw_fib->header.StructType != FIB_MAGIC)
722
 
                return -EINVAL;
723
 
        /*
724
 
         *      This block completes a cdb which orginated on the host and we 
725
 
         *      just need to deallocate the cdb or reinit it. At this point the
726
 
         *      command is complete that we had sent to the adapter and this
727
 
         *      cdb could be reused.
728
 
         */
729
 
        if((hw_fib->header.XferState & cpu_to_le32(SentFromHost)) &&
730
 
                (hw_fib->header.XferState & cpu_to_le32(AdapterProcessed)))
731
 
        {
732
 
                fib_dealloc(fibptr);
733
 
        }
734
 
        else if(hw_fib->header.XferState & cpu_to_le32(SentFromHost))
735
 
        {
736
 
                /*
737
 
                 *      This handles the case when the host has aborted the I/O
738
 
                 *      to the adapter because the adapter is not responding
739
 
                 */
740
 
                fib_dealloc(fibptr);
741
 
        } else if(hw_fib->header.XferState & cpu_to_le32(HostOwned)) {
742
 
                fib_dealloc(fibptr);
743
 
        } else {
744
 
                BUG();
745
 
        }   
746
 
        return 0;
747
 
}
748
 
 
749
 
/**
750
 
 *      aac_printf      -       handle printf from firmware
751
 
 *      @dev: Adapter
752
 
 *      @val: Message info
753
 
 *
754
 
 *      Print a message passed to us by the controller firmware on the
755
 
 *      Adaptec board
756
 
 */
757
 
 
758
 
void aac_printf(struct aac_dev *dev, u32 val)
759
 
{
760
 
        int length = val & 0xffff;
761
 
        int level = (val >> 16) & 0xffff;
762
 
        char *cp = dev->printfbuf;
763
 
        
764
 
        /*
765
 
         *      The size of the printfbuf is set in port.c
766
 
         *      There is no variable or define for it
767
 
         */
768
 
        if (length > 255)
769
 
                length = 255;
770
 
        if (cp[length] != 0)
771
 
                cp[length] = 0;
772
 
        if (level == LOG_HIGH_ERROR)
773
 
                printk(KERN_WARNING "aacraid:%s", cp);
774
 
        else
775
 
                printk(KERN_INFO "aacraid:%s", cp);
776
 
        memset(cp, 0,  256);
777
 
}
778
 
 
779
 
 
780
 
/**
781
 
 *      aac_handle_aif          -       Handle a message from the firmware
782
 
 *      @dev: Which adapter this fib is from
783
 
 *      @fibptr: Pointer to fibptr from adapter
784
 
 *
785
 
 *      This routine handles a driver notify fib from the adapter and
786
 
 *      dispatches it to the appropriate routine for handling.
787
 
 */
788
 
 
789
 
#define CONTAINER_TO_BUS(cont)          (0)
790
 
#define CONTAINER_TO_TARGET(cont)       ((cont))
791
 
#define CONTAINER_TO_LUN(cont)          (0)
792
 
 
793
 
static void aac_handle_aif(struct aac_dev * dev, struct fib * fibptr)
794
 
{
795
 
#if 0
796
 
        struct hw_fib * hw_fib = fibptr->hw_fib;
797
 
        struct aac_aifcmd * aifcmd = (struct aac_aifcmd *)hw_fib->data;
798
 
        int busy;
799
 
        u32 container;
800
 
        mm_segment_t fs;
801
 
 
802
 
        /* Sniff for container changes */
803
 
        dprintk ((KERN_INFO "AifCmdDriverNotify=%x\n", le32_to_cpu(*(u32 *)aifcmd->data)));
804
 
        switch (le32_to_cpu(*(u32 *)aifcmd->data)) {
805
 
        case AifDenMorphComplete:
806
 
        case AifDenVolumeExtendComplete:
807
 
        case AifEnContainerChange: /* Not really a driver notify Event */
808
 
 
809
 
                busy = 0;
810
 
                container = le32_to_cpu(((u32 *)aifcmd->data)[1]);
811
 
                dprintk ((KERN_INFO "container=%d(%d,%d,%d,%d) ",
812
 
                  container,
813
 
                  (dev && dev->scsi_host_ptr)
814
 
                    ? dev->scsi_host_ptr->host_no
815
 
                    : -1,
816
 
                  CONTAINER_TO_BUS(container),
817
 
                  CONTAINER_TO_TARGET(container),
818
 
                  CONTAINER_TO_LUN(container)));
819
 
 
820
 
                /*
821
 
                 *      Find the Scsi_Device associated with the SCSI address,
822
 
                 * and mark it as changed, invalidating the cache. This deals
823
 
                 * with changes to existing device IDs.
824
 
                 */
825
 
 
826
 
                if ((dev != (struct aac_dev *)NULL)
827
 
                 && (dev->scsi_host_ptr != (struct Scsi_Host *)NULL)) {
828
 
                        Scsi_Device * device;
829
 
 
830
 
                        for (device = dev->scsi_host_ptr->host_queue;
831
 
                          device != (Scsi_Device *)NULL;
832
 
                          device = device->next) {
833
 
                                dprintk((KERN_INFO
834
 
                                  "aifd: device (%d,%d,%d,%d)?\n",
835
 
                                  dev->scsi_host_ptr->host_no,
836
 
                                  device->channel,
837
 
                                  device->id,
838
 
                                  device->lun));
839
 
                                if ((device->channel == CONTAINER_TO_BUS(container))
840
 
                                 && (device->id == CONTAINER_TO_TARGET(container))
841
 
                                 && (device->lun == CONTAINER_TO_LUN(container))) {
842
 
                                        busy |= (device->access_count != 0);
843
 
                                        if (busy == 0) {
844
 
                                                device->removable = TRUE;
845
 
                                        }
846
 
                                }
847
 
                        }
848
 
                }
849
 
                dprintk (("busy=%d\n", busy));
850
 
 
851
 
                /*
852
 
                 * if (busy == 0) {
853
 
                 *      scan_scsis(dev->scsi_host_ptr, 1,
854
 
                 *        CONTAINER_TO_BUS(container),
855
 
                 *        CONTAINER_TO_TARGET(container),
856
 
                 *        CONTAINER_TO_LUN(container));
857
 
                 * }
858
 
                 * is not exported as accessible, so we need to go around it
859
 
                 * another way. So, we look for the "proc/scsi/scsi" entry in
860
 
                 * the proc filesystem (using proc_scsi as a shortcut) and send
861
 
                 * it a message. This deals with new devices that have
862
 
                 * appeared. If the device has gone offline, scan_scsis will
863
 
                 * also discover this, but we do not want the device to
864
 
                 * go away. We need to check the access_count for the
865
 
                 * device since we are not wanting the devices to go away.
866
 
                 */
867
 
                if (busy == 0 && proc_scsi != NULL) {
868
 
                        struct proc_dir_entry * entry;
869
 
 
870
 
                        dprintk((KERN_INFO "proc_scsi=%p ", proc_scsi));
871
 
                        for (entry = proc_scsi->subdir; entry != (struct proc_dir_entry *)NULL; entry = entry->next) {
872
 
                                dprintk(("\"%.*s\"[%d]=%x ", entry->namelen,
873
 
                                  entry->name, entry->namelen, entry->low_ino));
874
 
                                if ((entry->low_ino != 0) && (entry->namelen == 4) && (memcmp ("scsi", entry->name, 4) == 0)) {
875
 
                                        dprintk(("%p->write_proc=%p ", entry, entry->write_proc));
876
 
                                        if (entry->write_proc != (int (*)(struct file *, const char *, unsigned long, void *))NULL) {
877
 
                                                char buffer[80];
878
 
                                                int length;
879
 
 
880
 
                                                sprintf (buffer,
881
 
                                                  "scsi add-single-device %d %d %d %d\n",
882
 
                                                  dev->scsi_host_ptr->host_no,
883
 
                                                  CONTAINER_TO_BUS(container),
884
 
                                                  CONTAINER_TO_TARGET(container),
885
 
                                                  CONTAINER_TO_LUN(container));
886
 
                                                length = strlen (buffer);
887
 
                                                dprintk((KERN_INFO "echo %.*s > /proc/scsi/scsi\n", length-1, buffer));
888
 
                                                fs = get_fs();
889
 
                                                set_fs(get_ds());
890
 
                                                length = entry->write_proc(NULL, buffer, length, NULL);
891
 
                                                set_fs(fs);
892
 
                                                dprintk((KERN_INFO "returns %d\n", length));
893
 
                                        }
894
 
                                        break;
895
 
                                }
896
 
                        }
897
 
                }
898
 
        }
899
 
#endif
900
 
}
901
 
 
902
 
/**
903
 
 *      aac_command_thread      -       command processing thread
904
 
 *      @dev: Adapter to monitor
905
 
 *
906
 
 *      Waits on the commandready event in it's queue. When the event gets set
907
 
 *      it will pull FIBs off it's queue. It will continue to pull FIBs off
908
 
 *      until the queue is empty. When the queue is empty it will wait for
909
 
 *      more FIBs.
910
 
 */
911
 
void aac_command_thread(struct aac_dev * dev)
912
 
{
913
 
        struct hw_fib *hw_fib, *hw_newfib;
914
 
        struct fib *fib, *newfib;
915
 
        struct aac_queue_block *queues = dev->queues;
916
 
        struct aac_fib_context *fibctx;
917
 
        unsigned long flags;
918
 
        static spinlock_t lock = SPIN_LOCK_UNLOCKED;
919
 
 
920
 
        spin_lock_irqsave(&lock, flags);
921
 
 
922
 
        {
923
 
                spin_lock(queues->queue[HostNormCmdQueue].lock);
924
 
                while(!list_empty(&(queues->queue[HostNormCmdQueue].cmdq))) {
925
 
                        struct list_head *entry;
926
 
                        struct aac_aifcmd * aifcmd;
927
 
 
928
 
                        entry = queues->queue[HostNormCmdQueue].cmdq.next;
929
 
                        list_del(entry);
930
 
        
931
 
                        spin_unlock(queues->queue[HostNormCmdQueue].lock);
932
 
                        fib = list_entry(entry, struct fib, fiblink);
933
 
                        /*
934
 
                         *      We will process the FIB here or pass it to a 
935
 
                         *      worker thread that is TBD. We Really can't 
936
 
                         *      do anything at this point since we don't have
937
 
                         *      anything defined for this thread to do.
938
 
                         */
939
 
                        hw_fib = fib->hw_fib;
940
 
                        
941
 
                        memset(fib, 0, sizeof(struct fib));
942
 
                        fib->type = FSAFS_NTC_FIB_CONTEXT;
943
 
                        fib->size = sizeof( struct fib );
944
 
                        fib->hw_fib = hw_fib;
945
 
                        fib->data = hw_fib->data;
946
 
                        fib->dev = dev;
947
 
                        /*
948
 
                         *      We only handle AifRequest fibs from the adapter.
949
 
                         */
950
 
                        aifcmd = (struct aac_aifcmd *) hw_fib->data;
951
 
                        if (aifcmd->command == cpu_to_le32(AifCmdDriverNotify)) {
952
 
                                /* Handle Driver Notify Events */
953
 
                                aac_handle_aif(dev, fib);
954
 
                                *(u32 *)hw_fib->data = cpu_to_le32(ST_OK);
955
 
                                fib_adapter_complete(fib, sizeof(u32));
956
 
                        } else {
957
 
                                struct list_head *entry;
958
 
                                /* The u32 here is important and intended. We are using
959
 
                                   32bit wrapping time to fit the adapter field */
960
 
                                   
961
 
                                u32 time_now, time_last;
962
 
                                unsigned long flagv;
963
 
                                
964
 
                                /* Sniff events */
965
 
                                if (aifcmd->command == cpu_to_le32(AifCmdEventNotify))
966
 
                                        aac_handle_aif(dev, fib);
967
 
 
968
 
                                time_now = jiffies/HZ;
969
 
 
970
 
                                spin_lock_irqsave(&dev->fib_lock, flagv);
971
 
                                entry = dev->fib_list.next;
972
 
                                /*
973
 
                                 * For each Context that is on the 
974
 
                                 * fibctxList, make a copy of the
975
 
                                 * fib, and then set the event to wake up the
976
 
                                 * thread that is waiting for it.
977
 
                                 */
978
 
                                while (entry != &dev->fib_list) {
979
 
                                        /*
980
 
                                         * Extract the fibctx
981
 
                                         */
982
 
                                        fibctx = list_entry(entry, struct aac_fib_context, next);
983
 
                                        /*
984
 
                                         * Check if the queue is getting
985
 
                                         * backlogged
986
 
                                         */
987
 
                                        if (fibctx->count > 20)
988
 
                                        {
989
 
                                                /*
990
 
                                                 * It's *not* jiffies folks,
991
 
                                                 * but jiffies / HZ, so do not
992
 
                                                 * panic ...
993
 
                                                 */
994
 
                                                time_last = fibctx->jiffies;
995
 
                                                /*
996
 
                                                 * Has it been > 2 minutes 
997
 
                                                 * since the last read off
998
 
                                                 * the queue?
999
 
                                                 */
1000
 
                                                if ((time_now - time_last) > 120) {
1001
 
                                                        entry = entry->next;
1002
 
                                                        aac_close_fib_context(dev, fibctx);
1003
 
                                                        continue;
1004
 
                                                }
1005
 
                                        }
1006
 
                                        /*
1007
 
                                         * Warning: no sleep allowed while
1008
 
                                         * holding spinlock
1009
 
                                         */
1010
 
                                        hw_newfib = kmalloc(sizeof(struct hw_fib), GFP_ATOMIC);
1011
 
                                        newfib = kmalloc(sizeof(struct fib), GFP_ATOMIC);
1012
 
                                        if (newfib && hw_newfib) {
1013
 
                                                /*
1014
 
                                                 * Make the copy of the FIB
1015
 
                                                 * FIXME: check if we need to fix other fields up
1016
 
                                                 */
1017
 
                                                memcpy(hw_newfib, hw_fib, sizeof(struct hw_fib));
1018
 
                                                memcpy(newfib, fib, sizeof(struct fib));
1019
 
                                                newfib->hw_fib = hw_newfib;
1020
 
                                                /*
1021
 
                                                 * Put the FIB onto the
1022
 
                                                 * fibctx's fibs
1023
 
                                                 */
1024
 
                                                list_add_tail(&newfib->fiblink, &fibctx->fib_list);
1025
 
                                                fibctx->count++;
1026
 
                                        } else {
1027
 
                                                printk(KERN_WARNING "aifd: didn't allocate NewFib.\n");
1028
 
                                                if(newfib)
1029
 
                                                        kfree(newfib);
1030
 
                                                if(hw_newfib)
1031
 
                                                        kfree(hw_newfib);
1032
 
                                        }
1033
 
                                        entry = entry->next;
1034
 
                                }
1035
 
                                /*
1036
 
                                 *      Set the status of this FIB
1037
 
                                 */
1038
 
                                *(u32 *)hw_fib->data = cpu_to_le32(ST_OK);
1039
 
                                fib_adapter_complete(fib, sizeof(u32));
1040
 
                                spin_unlock_irqrestore(&dev->fib_lock, flagv);
1041
 
                        }
1042
 
                        spin_lock(queues->queue[HostNormCmdQueue].lock);
1043
 
                        kfree(fib);
1044
 
                }
1045
 
                /*
1046
 
                 *      There are no more AIF's
1047
 
                 */
1048
 
                spin_unlock(queues->queue[HostNormCmdQueue].lock);
1049
 
        }
1050
 
 
1051
 
        spin_unlock_irqrestore(&lock, flags);
1052
 
}