treewide: Replace GPLv2 boilerplate/reference with SPDX - rule 17
[linux-2.6-block.git] / drivers / scsi / aacraid / dpcsup.c
CommitLineData
1da177e4
LT
1/*
2 * Adaptec AAC series RAID controller driver
fa195afe 3 * (c) Copyright 2001 Red Hat Inc.
1da177e4
LT
4 *
5 * based on the old aacraid driver that is..
6 * Adaptec aacraid device driver for Linux.
7 *
e8b12f0f 8 * Copyright (c) 2000-2010 Adaptec, Inc.
f4babba0
RAR
9 * 2010-2015 PMC-Sierra, Inc. (aacraid@pmc-sierra.com)
10 * 2016-2017 Microsemi Corp. (aacraid@microsemi.com)
1da177e4
LT
11 *
12 * This program is free software; you can redistribute it and/or modify
13 * it under the terms of the GNU General Public License as published by
14 * the Free Software Foundation; either version 2, or (at your option)
15 * any later version.
16 *
17 * This program is distributed in the hope that it will be useful,
18 * but WITHOUT ANY WARRANTY; without even the implied warranty of
19 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
20 * GNU General Public License for more details.
21 *
22 * You should have received a copy of the GNU General Public License
23 * along with this program; see the file COPYING. If not, write to
24 * the Free Software Foundation, 675 Mass Ave, Cambridge, MA 02139, USA.
25 *
26 * Module Name:
27 * dpcsup.c
28 *
29 * Abstract: All DPC processing routines for the cyclone board occur here.
30 *
31 *
32 */
33
34#include <linux/kernel.h>
35#include <linux/init.h>
36#include <linux/types.h>
1da177e4
LT
37#include <linux/spinlock.h>
38#include <linux/slab.h>
39#include <linux/completion.h>
40#include <linux/blkdev.h>
1da177e4
LT
41
42#include "aacraid.h"
43
44/**
45 * aac_response_normal - Handle command replies
46 * @q: Queue to read from
47 *
48 * This DPC routine will be run when the adapter interrupts us to let us
49 * know there is a response on our normal priority queue. We will pull off
50 * all QE there are and wake up all the waiters before exiting. We will
51 * take a spinlock out on the queue before operating on it.
52 */
53
54unsigned int aac_response_normal(struct aac_queue * q)
55{
56 struct aac_dev * dev = q->dev;
57 struct aac_entry *entry;
58 struct hw_fib * hwfib;
59 struct fib * fib;
60 int consumed = 0;
cacb6dc3 61 unsigned long flags, mflags;
1da177e4 62
cacb6dc3 63 spin_lock_irqsave(q->lock, flags);
1da177e4
LT
64 /*
65 * Keep pulling response QEs off the response queue and waking
66 * up the waiters until there are no more QEs. We then return
d98e000c 67 * back to the system. If no response was requested we just
1da177e4
LT
68 * deallocate the Fib here and continue.
69 */
70 while(aac_consumer_get(dev, q, &entry))
71 {
72 int fast;
73 u32 index = le32_to_cpu(entry->addr);
74 fast = index & 0x01;
8e0c5ebd 75 fib = &dev->fibs[index >> 2];
a8166a52 76 hwfib = fib->hw_fib_va;
1da177e4
LT
77
78 aac_consumer_free(dev, q, HostNormRespQueue);
79 /*
80 * Remove this fib from the Outstanding I/O queue.
81 * But only if it has not already been timed out.
82 *
83 * If the fib has been timed out already, then just
84 * continue. The caller has already been notified that
85 * the fib timed out.
86 */
ef616233 87 atomic_dec(&dev->queues->queue[AdapNormCmdQueue].numpending);
03d44337
MH
88
89 if (unlikely(fib->flags & FIB_CONTEXT_FLAG_TIMED_OUT)) {
90 spin_unlock_irqrestore(q->lock, flags);
91 aac_fib_complete(fib);
92 aac_fib_free(fib);
93 spin_lock_irqsave(q->lock, flags);
1da177e4
LT
94 continue;
95 }
96 spin_unlock_irqrestore(q->lock, flags);
97
98 if (fast) {
99 /*
100 * Doctor the fib
101 */
56b58712 102 *(__le32 *)hwfib->data = cpu_to_le32(ST_OK);
1da177e4 103 hwfib->header.XferState |= cpu_to_le32(AdapterProcessed);
85d22bbf 104 fib->flags |= FIB_CONTEXT_FLAG_FASTRESP;
1da177e4
LT
105 }
106
107 FIB_COUNTER_INCREMENT(aac_config.FibRecved);
108
109 if (hwfib->header.Command == cpu_to_le16(NuFileSystem))
110 {
56b58712 111 __le32 *pstatus = (__le32 *)hwfib->data;
1da177e4
LT
112 if (*pstatus & cpu_to_le32(0xffff0000))
113 *pstatus = cpu_to_le32(ST_OK);
114 }
115 if (hwfib->header.XferState & cpu_to_le32(NoResponseExpected | Async))
116 {
117 if (hwfib->header.XferState & cpu_to_le32(NoResponseExpected))
118 FIB_COUNTER_INCREMENT(aac_config.NoResponseRecved);
119 else
120 FIB_COUNTER_INCREMENT(aac_config.AsyncRecved);
121 /*
122 * NOTE: we cannot touch the fib after this
123 * call, because it may have been deallocated.
124 */
125 fib->callback(fib->callback_data, fib);
126 } else {
127 unsigned long flagv;
128 spin_lock_irqsave(&fib->event_lock, flagv);
cacb6dc3 129 if (!fib->done) {
c8f7b073 130 fib->done = 1;
bc127d93 131 complete(&fib->event_wait);
cacb6dc3 132 }
1da177e4 133 spin_unlock_irqrestore(&fib->event_lock, flagv);
cacb6dc3
PNRCEH
134
135 spin_lock_irqsave(&dev->manage_lock, mflags);
136 dev->management_fib_count--;
137 spin_unlock_irqrestore(&dev->manage_lock, mflags);
138
1da177e4 139 FIB_COUNTER_INCREMENT(aac_config.NormalRecved);
c8f7b073 140 if (fib->done == 2) {
cacb6dc3
PNRCEH
141 spin_lock_irqsave(&fib->event_lock, flagv);
142 fib->done = 0;
143 spin_unlock_irqrestore(&fib->event_lock, flagv);
c8f7b073
MH
144 aac_fib_complete(fib);
145 aac_fib_free(fib);
146 }
1da177e4
LT
147 }
148 consumed++;
149 spin_lock_irqsave(q->lock, flags);
150 }
151
152 if (consumed > aac_config.peak_fibs)
153 aac_config.peak_fibs = consumed;
154 if (consumed == 0)
155 aac_config.zero_fibs++;
156
157 spin_unlock_irqrestore(q->lock, flags);
158 return 0;
159}
160
161
162/**
163 * aac_command_normal - handle commands
164 * @q: queue to process
165 *
166 * This DPC routine will be queued when the adapter interrupts us to
167 * let us know there is a command on our normal priority queue. We will
168 * pull off all QE there are and wake up all the waiters before exiting.
169 * We will take a spinlock out on the queue before operating on it.
170 */
171
172unsigned int aac_command_normal(struct aac_queue *q)
173{
174 struct aac_dev * dev = q->dev;
175 struct aac_entry *entry;
176 unsigned long flags;
177
178 spin_lock_irqsave(q->lock, flags);
179
180 /*
181 * Keep pulling response QEs off the response queue and waking
182 * up the waiters until there are no more QEs. We then return
183 * back to the system.
184 */
185 while(aac_consumer_get(dev, q, &entry))
186 {
187 struct fib fibctx;
188 struct hw_fib * hw_fib;
189 u32 index;
190 struct fib *fib = &fibctx;
191
192 index = le32_to_cpu(entry->addr) / sizeof(struct hw_fib);
193 hw_fib = &dev->aif_base_va[index];
194
195 /*
196 * Allocate a FIB at all costs. For non queued stuff
197 * we can just use the stack so we are happy. We need
198 * a fib object in order to manage the linked lists
199 */
200 if (dev->aif_thread)
201 if((fib = kmalloc(sizeof(struct fib), GFP_ATOMIC)) == NULL)
202 fib = &fibctx;
203
204 memset(fib, 0, sizeof(struct fib));
205 INIT_LIST_HEAD(&fib->fiblink);
206 fib->type = FSAFS_NTC_FIB_CONTEXT;
207 fib->size = sizeof(struct fib);
a8166a52 208 fib->hw_fib_va = hw_fib;
1da177e4
LT
209 fib->data = hw_fib->data;
210 fib->dev = dev;
211
212
213 if (dev->aif_thread && fib != &fibctx) {
214 list_add_tail(&fib->fiblink, &q->cmdq);
215 aac_consumer_free(dev, q, HostNormCmdQueue);
216 wake_up_interruptible(&q->cmdready);
217 } else {
218 aac_consumer_free(dev, q, HostNormCmdQueue);
219 spin_unlock_irqrestore(q->lock, flags);
220 /*
221 * Set the status of this FIB
222 */
56b58712 223 *(__le32 *)hw_fib->data = cpu_to_le32(ST_OK);
bfb35aa8 224 aac_fib_adapter_complete(fib, sizeof(u32));
1da177e4
LT
225 spin_lock_irqsave(q->lock, flags);
226 }
227 }
228 spin_unlock_irqrestore(q->lock, flags);
229 return 0;
230}
8e0c5ebd 231
e8b12f0f
MR
232/*
233 *
234 * aac_aif_callback
235 * @context: the context set in the fib - here it is scsi cmd
236 * @fibptr: pointer to the fib
237 *
238 * Handles the AIFs - new method (SRC)
239 *
240 */
241
242static void aac_aif_callback(void *context, struct fib * fibptr)
243{
244 struct fib *fibctx;
245 struct aac_dev *dev;
246 struct aac_aifcmd *cmd;
247 int status;
248
249 fibctx = (struct fib *)context;
250 BUG_ON(fibptr == NULL);
251 dev = fibptr->dev;
252
3ffd6c5a
RAR
253 if ((fibptr->hw_fib_va->header.XferState &
254 cpu_to_le32(NoMoreAifDataAvailable)) ||
255 dev->sa_firmware) {
e8b12f0f
MR
256 aac_fib_complete(fibptr);
257 aac_fib_free(fibptr);
258 return;
259 }
260
261 aac_intr_normal(dev, 0, 1, 0, fibptr->hw_fib_va);
262
263 aac_fib_init(fibctx);
264 cmd = (struct aac_aifcmd *) fib_data(fibctx);
265 cmd->command = cpu_to_le32(AifReqEvent);
266
267 status = aac_fib_send(AifRequest,
268 fibctx,
269 sizeof(struct hw_fib)-sizeof(struct aac_fibhdr),
270 FsaNormal,
271 0, 1,
272 (fib_callback)aac_aif_callback, fibctx);
273}
274
8e0c5ebd
MH
275
276/**
277 * aac_intr_normal - Handle command replies
278 * @dev: Device
279 * @index: completion reference
280 *
281 * This DPC routine will be run when the adapter interrupts us to let us
282 * know there is a response on our normal priority queue. We will pull off
283 * all QE there are and wake up all the waiters before exiting.
284 */
3ffd6c5a
RAR
285unsigned int aac_intr_normal(struct aac_dev *dev, u32 index, int isAif,
286 int isFastResponse, struct hw_fib *aif_fib)
8e0c5ebd 287{
cacb6dc3 288 unsigned long mflags;
f3307f72 289 dprintk((KERN_INFO "aac_intr_normal(%p,%x)\n", dev, index));
e8b12f0f 290 if (isAif == 1) { /* AIF - common */
8e0c5ebd
MH
291 struct hw_fib * hw_fib;
292 struct fib * fib;
293 struct aac_queue *q = &dev->queues->queue[HostNormCmdQueue];
294 unsigned long flags;
295
8e0c5ebd
MH
296 /*
297 * Allocate a FIB. For non queued stuff we can just use
298 * the stack so we are happy. We need a fib object in order to
299 * manage the linked lists.
300 */
301 if ((!dev->aif_thread)
4dbc22d7 302 || (!(fib = kzalloc(sizeof(struct fib),GFP_ATOMIC))))
8e0c5ebd 303 return 1;
4dbc22d7 304 if (!(hw_fib = kzalloc(sizeof(struct hw_fib),GFP_ATOMIC))) {
8e0c5ebd
MH
305 kfree (fib);
306 return 1;
307 }
3ffd6c5a
RAR
308 if (dev->sa_firmware) {
309 fib->hbacmd_size = index; /* store event type */
310 } else if (aif_fib != NULL) {
e8b12f0f
MR
311 memcpy(hw_fib, aif_fib, sizeof(struct hw_fib));
312 } else {
3ffd6c5a
RAR
313 memcpy(hw_fib, (struct hw_fib *)
314 (((uintptr_t)(dev->regs.sa)) + index),
315 sizeof(struct hw_fib));
e8b12f0f 316 }
8e0c5ebd
MH
317 INIT_LIST_HEAD(&fib->fiblink);
318 fib->type = FSAFS_NTC_FIB_CONTEXT;
319 fib->size = sizeof(struct fib);
a8166a52 320 fib->hw_fib_va = hw_fib;
8e0c5ebd
MH
321 fib->data = hw_fib->data;
322 fib->dev = dev;
323
324 spin_lock_irqsave(q->lock, flags);
325 list_add_tail(&fib->fiblink, &q->cmdq);
326 wake_up_interruptible(&q->cmdready);
327 spin_unlock_irqrestore(q->lock, flags);
328 return 1;
e8b12f0f
MR
329 } else if (isAif == 2) { /* AIF - new (SRC) */
330 struct fib *fibctx;
331 struct aac_aifcmd *cmd;
332
333 fibctx = aac_fib_alloc(dev);
334 if (!fibctx)
335 return 1;
336 aac_fib_init(fibctx);
337
338 cmd = (struct aac_aifcmd *) fib_data(fibctx);
339 cmd->command = cpu_to_le32(AifReqEvent);
340
341 return aac_fib_send(AifRequest,
342 fibctx,
343 sizeof(struct hw_fib)-sizeof(struct aac_fibhdr),
344 FsaNormal,
345 0, 1,
346 (fib_callback)aac_aif_callback, fibctx);
8e0c5ebd 347 } else {
e8b12f0f 348 struct fib *fib = &dev->fibs[index];
423400e6 349 int start_callback = 0;
8e0c5ebd
MH
350
351 /*
352 * Remove this fib from the Outstanding I/O queue.
353 * But only if it has not already been timed out.
354 *
355 * If the fib has been timed out already, then just
356 * continue. The caller has already been notified that
357 * the fib timed out.
358 */
ef616233 359 atomic_dec(&dev->queues->queue[AdapNormCmdQueue].numpending);
03d44337
MH
360
361 if (unlikely(fib->flags & FIB_CONTEXT_FLAG_TIMED_OUT)) {
362 aac_fib_complete(fib);
363 aac_fib_free(fib);
8e0c5ebd
MH
364 return 0;
365 }
366
8e0c5ebd
MH
367 FIB_COUNTER_INCREMENT(aac_config.FibRecved);
368
423400e6
RAR
369 if (fib->flags & FIB_CONTEXT_FLAG_NATIVE_HBA) {
370
371 if (isFastResponse)
372 fib->flags |= FIB_CONTEXT_FLAG_FASTRESP;
373
374 if (fib->callback) {
375 start_callback = 1;
376 } else {
377 unsigned long flagv;
bc127d93 378 int completed = 0;
423400e6
RAR
379
380 dprintk((KERN_INFO "event_wait up\n"));
381 spin_lock_irqsave(&fib->event_lock, flagv);
382 if (fib->done == 2) {
383 fib->done = 1;
bc127d93 384 completed = 1;
423400e6
RAR
385 } else {
386 fib->done = 1;
bc127d93 387 complete(&fib->event_wait);
423400e6
RAR
388 }
389 spin_unlock_irqrestore(&fib->event_lock, flagv);
390
391 spin_lock_irqsave(&dev->manage_lock, mflags);
392 dev->management_fib_count--;
393 spin_unlock_irqrestore(&dev->manage_lock,
394 mflags);
395
396 FIB_COUNTER_INCREMENT(aac_config.NativeRecved);
bc127d93 397 if (completed)
423400e6
RAR
398 aac_fib_complete(fib);
399 }
8e0c5ebd 400 } else {
423400e6
RAR
401 struct hw_fib *hwfib = fib->hw_fib_va;
402
403 if (isFastResponse) {
404 /* Doctor the fib */
405 *(__le32 *)hwfib->data = cpu_to_le32(ST_OK);
406 hwfib->header.XferState |=
407 cpu_to_le32(AdapterProcessed);
408 fib->flags |= FIB_CONTEXT_FLAG_FASTRESP;
cacb6dc3 409 }
cacb6dc3 410
423400e6
RAR
411 if (hwfib->header.Command ==
412 cpu_to_le16(NuFileSystem)) {
413 __le32 *pstatus = (__le32 *)hwfib->data;
cacb6dc3 414
423400e6
RAR
415 if (*pstatus & cpu_to_le32(0xffff0000))
416 *pstatus = cpu_to_le32(ST_OK);
417 }
418 if (hwfib->header.XferState &
419 cpu_to_le32(NoResponseExpected | Async)) {
420 if (hwfib->header.XferState & cpu_to_le32(
421 NoResponseExpected))
422 FIB_COUNTER_INCREMENT(
423 aac_config.NoResponseRecved);
424 else
425 FIB_COUNTER_INCREMENT(
426 aac_config.AsyncRecved);
427 start_callback = 1;
428 } else {
429 unsigned long flagv;
bc127d93 430 int completed = 0;
423400e6
RAR
431
432 dprintk((KERN_INFO "event_wait up\n"));
cacb6dc3 433 spin_lock_irqsave(&fib->event_lock, flagv);
423400e6
RAR
434 if (fib->done == 2) {
435 fib->done = 1;
bc127d93 436 completed = 1;
423400e6
RAR
437 } else {
438 fib->done = 1;
bc127d93 439 complete(&fib->event_wait);
423400e6 440 }
cacb6dc3 441 spin_unlock_irqrestore(&fib->event_lock, flagv);
423400e6
RAR
442
443 spin_lock_irqsave(&dev->manage_lock, mflags);
444 dev->management_fib_count--;
445 spin_unlock_irqrestore(&dev->manage_lock,
446 mflags);
447
448 FIB_COUNTER_INCREMENT(aac_config.NormalRecved);
bc127d93 449 if (completed)
423400e6
RAR
450 aac_fib_complete(fib);
451 }
452 }
453
454
455 if (start_callback) {
456 /*
457 * NOTE: we cannot touch the fib after this
458 * call, because it may have been deallocated.
459 */
460 if (likely(fib->callback && fib->callback_data)) {
461 fib->callback(fib->callback_data, fib);
462 } else {
cacb6dc3 463 aac_fib_complete(fib);
423400e6 464 aac_fib_free(fib);
cacb6dc3
PNRCEH
465 }
466
8e0c5ebd
MH
467 }
468 return 0;
469 }
470}