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