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