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