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