blob: e67e032936ef015b66c242eaf9c3111cfb3812c2 [file] [log] [blame]
Linus Torvalds1da177e2005-04-16 15:20:36 -07001/*
2 * Adaptec AAC series RAID controller driver
Alan Coxfa195af2008-10-27 15:16:36 +00003 * (c) Copyright 2001 Red Hat Inc.
Linus Torvalds1da177e2005-04-16 15:20:36 -07004 *
5 * based on the old aacraid driver that is..
6 * Adaptec aacraid device driver for Linux.
7 *
Mahesh Rajashekharae8b12f02011-03-17 02:10:32 -07008 * Copyright (c) 2000-2010 Adaptec, Inc.
Raghava Aditya Renukuntaf4babba2017-02-02 15:53:36 -08009 * 2010-2015 PMC-Sierra, Inc. (aacraid@pmc-sierra.com)
10 * 2016-2017 Microsemi Corp. (aacraid@microsemi.com)
Linus Torvalds1da177e2005-04-16 15:20:36 -070011 *
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
Mark Haverkamp 7c00ffa2005-05-16 18:28:42 -070030 * communication.
Linus Torvalds1da177e2005-04-16 15:20:36 -070031 *
32 */
33
34#include <linux/kernel.h>
35#include <linux/init.h>
Raghava Aditya Renukuntafe523752017-12-26 20:34:48 -080036#include <linux/crash_dump.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070037#include <linux/types.h>
38#include <linux/sched.h>
39#include <linux/pci.h>
40#include <linux/spinlock.h>
41#include <linux/slab.h>
42#include <linux/completion.h>
43#include <linux/blkdev.h>
Al Viro164006d2005-11-30 23:47:05 -050044#include <linux/delay.h>
Christoph Hellwigfe273812006-02-14 18:45:06 +010045#include <linux/kthread.h>
Al Viro6a3670c2006-09-24 23:45:29 +010046#include <linux/interrupt.h>
Raghava Aditya Renukunta3d77d842017-02-02 15:53:25 -080047#include <linux/bcd.h>
Mark Haverkamp8c867b22006-08-03 08:03:30 -070048#include <scsi/scsi.h>
Mark Haverkamp 7c00ffa2005-05-16 18:28:42 -070049#include <scsi/scsi_host.h>
Mark Haverkamp131256c2005-09-26 13:04:56 -070050#include <scsi/scsi_device.h>
Mark Haverkamp8c867b22006-08-03 08:03:30 -070051#include <scsi/scsi_cmnd.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070052
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 */
Salyzyn, Mark8ce3eca2008-01-16 07:39:06 -080062
Linus Torvalds1da177e2005-04-16 15:20:36 -070063static int fib_map_alloc(struct aac_dev *dev)
64{
Raghava Aditya Renukunta3ffd6c52017-02-02 15:53:22 -080065 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;
Raghava Aditya Renukunta423400e2017-02-02 15:53:29 -080069 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 }
Raghava Aditya Renukunta3ffd6c52017-02-02 15:53:22 -080074
Mark Haverkamp 7c00ffa2005-05-16 18:28:42 -070075 dprintk((KERN_INFO
Mahesh Rajashekharaf4819732017-04-05 16:14:16 +053076 "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,
Mark Haverkamp 7c00ffa2005-05-16 18:28:42 -070078 AAC_NUM_MGT_FIB, &dev->hw_fib_pa));
Mahesh Rajashekharaf4819732017-04-05 16:14:16 +053079 dev->hw_fib_va = dma_alloc_coherent(&dev->pdev->dev,
Raghava Aditya Renukunta3ffd6c52017-02-02 15:53:22 -080080 (dev->max_cmd_size + sizeof(struct aac_fib_xporthdr))
Mahesh Rajashekharae8b12f02011-03-17 02:10:32 -070081 * (dev->scsi_host_ptr->can_queue + AAC_NUM_MGT_FIB) + (ALIGN32 - 1),
Mahesh Rajashekharaf4819732017-04-05 16:14:16 +053082 &dev->hw_fib_pa, GFP_KERNEL);
Mahesh Rajashekharae8b12f02011-03-17 02:10:32 -070083 if (dev->hw_fib_va == NULL)
Linus Torvalds1da177e2005-04-16 15:20:36 -070084 return -ENOMEM;
85 return 0;
86}
87
88/**
Mark Haverkampbfb35aa82006-02-01 09:30:55 -080089 * aac_fib_map_free - free the fib objects
Linus Torvalds1da177e2005-04-16 15:20:36 -070090 * @dev: Adapter to free
91 *
92 * Free the PCI mappings and the memory allocated for FIB blocks
93 * on this adapter.
94 */
95
Mark Haverkampbfb35aa82006-02-01 09:30:55 -080096void aac_fib_map_free(struct aac_dev *dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -070097{
Raghava Aditya Renukunta1bff5ab2017-02-16 12:51:14 -080098 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
Mahesh Rajashekharaf4819732017-04-05 16:14:16 +0530109 dma_free_coherent(&dev->pdev->dev, alloc_size, dev->hw_fib_va,
110 dev->hw_fib_pa);
Raghava Aditya Renukunta1bff5ab2017-02-16 12:51:14 -0800111
Salyzyn, Mark9ad52042007-07-17 11:15:08 -0400112 dev->hw_fib_va = NULL;
113 dev->hw_fib_pa = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700114}
115
Raghava Aditya Renukunta3f4ce052016-02-03 15:06:00 -0800116void 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
Linus Torvalds1da177e2005-04-16 15:20:36 -0700138/**
Mark Haverkampbfb35aa82006-02-01 09:30:55 -0800139 * aac_fib_setup - setup the fibs
Linus Torvalds1da177e2005-04-16 15:20:36 -0700140 * @dev: Adapter to set up
141 *
Uwe Kleine-Königb5950762010-11-01 15:38:34 -0400142 * Allocate the PCI space for the fibs, map it and then initialise the
Linus Torvalds1da177e2005-04-16 15:20:36 -0700143 * fib area, the unmapped fib data and also the free list
144 */
145
Mark Haverkampbfb35aa82006-02-01 09:30:55 -0800146int aac_fib_setup(struct aac_dev * dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700147{
148 struct fib *fibptr;
Mark Haverkampa8166a52007-03-15 10:26:22 -0700149 struct hw_fib *hw_fib;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700150 dma_addr_t hw_fib_pa;
151 int i;
Raghava Aditya Renukuntad1ef4da2017-02-02 15:53:17 -0800152 u32 max_cmds;
Mark Haverkamp 7c00ffa2005-05-16 18:28:42 -0700153
154 while (((i = fib_map_alloc(dev)) == -ENOMEM)
155 && (dev->scsi_host_ptr->can_queue > (64 - AAC_NUM_MGT_FIB))) {
Raghava Aditya Renukuntad1ef4da2017-02-02 15:53:17 -0800156 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);
Mark Haverkamp 7c00ffa2005-05-16 18:28:42 -0700160 }
161 if (i<0)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700162 return -ENOMEM;
Salyzyn, Mark8ce3eca2008-01-16 07:39:06 -0800163
Mahesh Rajashekharae8b12f02011-03-17 02:10:32 -0700164 memset(dev->hw_fib_va, 0,
Raghava Aditya Renukunta423400e2017-02-02 15:53:29 -0800165 (dev->max_cmd_size + sizeof(struct aac_fib_xporthdr)) *
Mahesh Rajashekharae8b12f02011-03-17 02:10:32 -0700166 (dev->scsi_host_ptr->can_queue + AAC_NUM_MGT_FIB));
167
Raghava Aditya Renukunta1bff5ab2017-02-16 12:51:14 -0800168 /* 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));
Mahesh Rajashekharae8b12f02011-03-17 02:10:32 -0700172
Raghava Aditya Renukunta1bff5ab2017-02-16 12:51:14 -0800173 /* add Xport header */
174 hw_fib = (struct hw_fib *)((unsigned char *)hw_fib +
175 sizeof(struct aac_fib_xporthdr));
176 hw_fib_pa += sizeof(struct aac_fib_xporthdr);
177
Linus Torvalds1da177e2005-04-16 15:20:36 -0700178 /*
179 * Initialise the fibs
180 */
Salyzyn, Mark8ce3eca2008-01-16 07:39:06 -0800181 for (i = 0, fibptr = &dev->fibs[i];
182 i < (dev->scsi_host_ptr->can_queue + AAC_NUM_MGT_FIB);
183 i++, fibptr++)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700184 {
Mahesh Rajashekhara85d22bb2012-07-14 18:18:51 +0530185 fibptr->flags = 0;
Raghava Aditya Renukunta6bf3b632016-02-03 15:05:59 -0800186 fibptr->size = sizeof(struct fib);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700187 fibptr->dev = dev;
Mark Haverkampa8166a52007-03-15 10:26:22 -0700188 fibptr->hw_fib_va = hw_fib;
189 fibptr->data = (void *) fibptr->hw_fib_va->data;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700190 fibptr->next = fibptr+1; /* Forward chain the fibs */
Arnd Bergmannbc127d92018-12-10 22:32:41 +0100191 init_completion(&fibptr->event_wait);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700192 spin_lock_init(&fibptr->event_lock);
Mark Haverkampa8166a52007-03-15 10:26:22 -0700193 hw_fib->header.XferState = cpu_to_le32(0xffffffff);
Raghava Aditya Renukunta423400e2017-02-02 15:53:29 -0800194 hw_fib->header.SenderSize =
195 cpu_to_le16(dev->max_fib_size); /* ?? max_cmd_size */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700196 fibptr->hw_fib_pa = hw_fib_pa;
Raghava Aditya Renukunta423400e2017-02-02 15:53:29 -0800197 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
Mahesh Rajashekharae8b12f02011-03-17 02:10:32 -0700206 hw_fib = (struct hw_fib *)((unsigned char *)hw_fib +
Raghava Aditya Renukunta3ffd6c52017-02-02 15:53:22 -0800207 dev->max_cmd_size + sizeof(struct aac_fib_xporthdr));
Mahesh Rajashekharae8b12f02011-03-17 02:10:32 -0700208 hw_fib_pa = hw_fib_pa +
Raghava Aditya Renukunta3ffd6c52017-02-02 15:53:22 -0800209 dev->max_cmd_size + sizeof(struct aac_fib_xporthdr);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700210 }
Raghava Aditya Renukunta3f4ce052016-02-03 15:06:00 -0800211
212 /*
213 *Assign vector numbers to fibs
214 */
215 aac_fib_vector_assign(dev);
216
Linus Torvalds1da177e2005-04-16 15:20:36 -0700217 /*
218 * Add the fib chain to the free list
219 */
Mark Haverkamp 7c00ffa2005-05-16 18:28:42 -0700220 dev->fibs[dev->scsi_host_ptr->can_queue + AAC_NUM_MGT_FIB - 1].next = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700221 /*
Raghava Aditya Renukunta6bf3b632016-02-03 15:05:59 -0800222 * Set 8 fibs aside for management tools
223 */
224 dev->free_fib = &dev->fibs[dev->scsi_host_ptr->can_queue];
Linus Torvalds1da177e2005-04-16 15:20:36 -0700225 return 0;
226}
227
228/**
Raghava Aditya Renukunta6bf3b632016-02-03 15:05:59 -0800229 * 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
236struct 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
253/**
Mark Haverkampbfb35aa82006-02-01 09:30:55 -0800254 * aac_fib_alloc - allocate a fib
Linus Torvalds1da177e2005-04-16 15:20:36 -0700255 * @dev: Adapter to allocate the fib for
256 *
257 * Allocate a fib from the adapter fib pool. If the pool is empty we
Mark Haverkamp 7c00ffa2005-05-16 18:28:42 -0700258 * return NULL.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700259 */
Salyzyn, Mark8ce3eca2008-01-16 07:39:06 -0800260
Mark Haverkampbfb35aa82006-02-01 09:30:55 -0800261struct fib *aac_fib_alloc(struct aac_dev *dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700262{
263 struct fib * fibptr;
264 unsigned long flags;
265 spin_lock_irqsave(&dev->fib_lock, flags);
Salyzyn, Mark8ce3eca2008-01-16 07:39:06 -0800266 fibptr = dev->free_fib;
Mark Haverkamp 7c00ffa2005-05-16 18:28:42 -0700267 if(!fibptr){
268 spin_unlock_irqrestore(&dev->fib_lock, flags);
269 return fibptr;
270 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700271 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 */
Mark Haverkampa8166a52007-03-15 10:26:22 -0700282 fibptr->hw_fib_va->header.XferState = 0;
Salyzyn, Markb6ef70f2008-01-08 13:26:43 -0800283 fibptr->flags = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700284 fibptr->callback = NULL;
285 fibptr->callback_data = NULL;
286
287 return fibptr;
288}
289
290/**
Mark Haverkampbfb35aa82006-02-01 09:30:55 -0800291 * aac_fib_free - free a fib
Linus Torvalds1da177e2005-04-16 15:20:36 -0700292 * @fibptr: fib to free up
293 *
294 * Frees up a fib and places it on the appropriate queue
Linus Torvalds1da177e2005-04-16 15:20:36 -0700295 */
Salyzyn, Mark8ce3eca2008-01-16 07:39:06 -0800296
Mark Haverkampbfb35aa82006-02-01 09:30:55 -0800297void aac_fib_free(struct fib *fibptr)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700298{
Mahesh Rajashekharaef616232015-03-26 10:41:30 -0400299 unsigned long flags;
Penchala Narasimha Reddy Chilakala, ERS-HCLTechcacb6dc2009-12-21 18:39:27 +0530300
Mahesh Rajashekharaef616232015-03-26 10:41:30 -0400301 if (fibptr->done == 2)
Penchala Narasimha Reddy Chilakala, ERS-HCLTechcacb6dc2009-12-21 18:39:27 +0530302 return;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700303
304 spin_lock_irqsave(&fibptr->dev->fib_lock, flags);
Mark Haverkamp03d44332007-03-15 10:27:45 -0700305 if (unlikely(fibptr->flags & FIB_CONTEXT_FLAG_TIMED_OUT))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700306 aac_config.fib_timeouts++;
Raghava Aditya Renukunta423400e2017-02-02 15:53:29 -0800307 if (!(fibptr->flags & FIB_CONTEXT_FLAG_NATIVE_HBA) &&
308 fibptr->hw_fib_va->header.XferState != 0) {
Mark Haverkamp03d44332007-03-15 10:27:45 -0700309 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;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700315 spin_unlock_irqrestore(&fibptr->dev->fib_lock, flags);
316}
317
318/**
Mark Haverkampbfb35aa82006-02-01 09:30:55 -0800319 * aac_fib_init - initialise a fib
Linus Torvalds1da177e2005-04-16 15:20:36 -0700320 * @fibptr: The fib to initialize
Salyzyn, Mark8ce3eca2008-01-16 07:39:06 -0800321 *
Linus Torvalds1da177e2005-04-16 15:20:36 -0700322 * Set up the generic fib fields ready for use
323 */
Salyzyn, Mark8ce3eca2008-01-16 07:39:06 -0800324
Mark Haverkampbfb35aa82006-02-01 09:30:55 -0800325void aac_fib_init(struct fib *fibptr)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700326{
Mark Haverkampa8166a52007-03-15 10:26:22 -0700327 struct hw_fib *hw_fib = fibptr->hw_fib_va;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700328
Mahesh Rajashekhara85d22bb2012-07-14 18:18:51 +0530329 memset(&hw_fib->header, 0, sizeof(struct aac_fibhdr));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700330 hw_fib->header.StructType = FIB_MAGIC;
Mark Haverkamp 7c00ffa2005-05-16 18:28:42 -0700331 hw_fib->header.Size = cpu_to_le16(fibptr->dev->max_fib_size);
332 hw_fib->header.XferState = cpu_to_le32(HostOwned | FibInitialized | FibEmpty | FastResponseCapable);
Mahesh Rajashekhara85d22bb2012-07-14 18:18:51 +0530333 hw_fib->header.u.ReceiverFibAddress = cpu_to_le32(fibptr->hw_fib_pa);
Mark Haverkamp 7c00ffa2005-05-16 18:28:42 -0700334 hw_fib->header.SenderSize = cpu_to_le16(fibptr->dev->max_fib_size);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700335}
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 */
Salyzyn, Mark8ce3eca2008-01-16 07:39:06 -0800344
Adrian Bunk 48338692005-04-25 19:45:58 -0700345static void fib_dealloc(struct fib * fibptr)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700346{
Mark Haverkampa8166a52007-03-15 10:26:22 -0700347 struct hw_fib *hw_fib = fibptr->hw_fib_va;
Salyzyn, Mark8ce3eca2008-01-16 07:39:06 -0800348 hw_fib->header.XferState = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700349}
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 */
Salyzyn, Mark8ce3eca2008-01-16 07:39:06 -0800357
Linus Torvalds1da177e2005-04-16 15:20:36 -0700358/**
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 */
Salyzyn, Mark8ce3eca2008-01-16 07:39:06 -0800370
Linus Torvalds1da177e2005-04-16 15:20:36 -0700371static 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;
Mark Haverkampbed30de2005-08-03 15:38:51 -0700374 unsigned long idx;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700375
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];
Mark Haverkampbed30de2005-08-03 15:38:51 -0700384
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) {
Mark Haverkamp1640a2c2005-09-20 12:57:11 -0700389 if (qid == AdapNormCmdQueue)
Mark Haverkampbed30de2005-08-03 15:38:51 -0700390 idx = ADAP_NORM_CMD_ENTRIES;
Mark Haverkamp1640a2c2005-09-20 12:57:11 -0700391 else
Mark Haverkampbed30de2005-08-03 15:38:51 -0700392 idx = ADAP_NORM_RESP_ENTRIES;
393 }
394 if (idx != le32_to_cpu(*(q->headers.consumer)))
Salyzyn, Mark8ce3eca2008-01-16 07:39:06 -0800395 *nonotify = 1;
Mark Haverkampbed30de2005-08-03 15:38:51 -0700396 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700397
Mark Haverkamp1640a2c2005-09-20 12:57:11 -0700398 if (qid == AdapNormCmdQueue) {
Salyzyn, Mark8ce3eca2008-01-16 07:39:06 -0800399 if (*index >= ADAP_NORM_CMD_ENTRIES)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700400 *index = 0; /* Wrap to front of the Producer Queue. */
Mark Haverkamp1640a2c2005-09-20 12:57:11 -0700401 } else {
Salyzyn, Mark8ce3eca2008-01-16 07:39:06 -0800402 if (*index >= ADAP_NORM_RESP_ENTRIES)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700403 *index = 0; /* Wrap to front of the Producer Queue. */
404 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700405
Salyzyn, Mark8ce3eca2008-01-16 07:39:06 -0800406 /* Queue is full */
407 if ((*index + 1) == le32_to_cpu(*(q->headers.consumer))) {
Mark Haverkamp 7c00ffa2005-05-16 18:28:42 -0700408 printk(KERN_WARNING "Queue %d full, %u outstanding.\n",
Mahesh Rajashekharaef616232015-03-26 10:41:30 -0400409 qid, atomic_read(&q->numpending));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700410 return 0;
411 } else {
Salyzyn, Mark8ce3eca2008-01-16 07:39:06 -0800412 *entry = q->base + *index;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700413 return 1;
414 }
Salyzyn, Mark8ce3eca2008-01-16 07:39:06 -0800415}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700416
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
Mark Haverkamp28713322007-01-23 14:59:20 -0800433int aac_queue_get(struct aac_dev * dev, u32 * index, u32 qid, struct hw_fib * hw_fib, int wait, struct fib * fibptr, unsigned long *nonotify)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700434{
435 struct aac_entry * entry = NULL;
436 int map = 0;
Salyzyn, Mark8ce3eca2008-01-16 07:39:06 -0800437
Mark Haverkamp1640a2c2005-09-20 12:57:11 -0700438 if (qid == AdapNormCmdQueue) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700439 /* if no entries wait for some if caller wants to */
Salyzyn, Mark8ce3eca2008-01-16 07:39:06 -0800440 while (!aac_get_entry(dev, qid, &entry, index, nonotify)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700441 printk(KERN_ERR "GetEntries failed\n");
442 }
Salyzyn, Mark8ce3eca2008-01-16 07:39:06 -0800443 /*
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;
Mark Haverkamp1640a2c2005-09-20 12:57:11 -0700448 } else {
Salyzyn, Mark8ce3eca2008-01-16 07:39:06 -0800449 while (!aac_get_entry(dev, qid, &entry, index, nonotify)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700450 /* if no entries wait for some if caller wants to */
451 }
Salyzyn, Mark8ce3eca2008-01-16 07:39:06 -0800452 /*
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 */
Mahesh Rajashekhara85d22bb2012-07-14 18:18:51 +0530458 hw_fib->header.u.ReceiverFibAddress = hw_fib->header.SenderFibAddress; /* Let the adapter now where to find its data */
Salyzyn, Mark8ce3eca2008-01-16 07:39:06 -0800459 map = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700460 }
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
Linus Torvalds1da177e2005-04-16 15:20:36 -0700470/*
Salyzyn, Mark8ce3eca2008-01-16 07:39:06 -0800471 * Define the highest level of host to adapter communication routines.
472 * These routines will support host to adapter FS commuication. These
Linus Torvalds1da177e2005-04-16 15:20:36 -0700473 * 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/**
Mark Haverkampbfb35aa82006-02-01 09:30:55 -0800479 * aac_fib_send - send a fib to the adapter
Linus Torvalds1da177e2005-04-16 15:20:36 -0700480 * @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 */
Salyzyn, Mark8ce3eca2008-01-16 07:39:06 -0800494
Mark Haverkampbfb35aa82006-02-01 09:30:55 -0800495int 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)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700498{
Linus Torvalds1da177e2005-04-16 15:20:36 -0700499 struct aac_dev * dev = fibptr->dev;
Mark Haverkampa8166a52007-03-15 10:26:22 -0700500 struct hw_fib * hw_fib = fibptr->hw_fib_va;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700501 unsigned long flags = 0;
Penchala Narasimha Reddy Chilakala, ERS-HCLTechcacb6dc2009-12-21 18:39:27 +0530502 unsigned long mflags = 0;
Mahesh Rajashekhara11604612012-02-08 22:51:04 -0800503 unsigned long sflags = 0;
Penchala Narasimha Reddy Chilakala, ERS-HCLTechcacb6dc2009-12-21 18:39:27 +0530504
Linus Torvalds1da177e2005-04-16 15:20:36 -0700505 if (!(hw_fib->header.XferState & cpu_to_le32(HostOwned)))
506 return -EBUSY;
Raghava Aditya Renukuntaa0c61432017-02-16 12:51:13 -0800507
508 if (hw_fib->header.XferState & cpu_to_le32(AdapterProcessed))
509 return -EINVAL;
510
Linus Torvalds1da177e2005-04-16 15:20:36 -0700511 /*
Lucas De Marchi25985ed2011-03-30 22:57:33 -0300512 * There are 5 cases with the wait and response requested flags.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700513 * 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
Dongliang Mud98e0002018-08-07 01:14:29 -0400516 * is not requested the Fib will just be deallocaed by the DPC
Linus Torvalds1da177e2005-04-16 15:20:36 -0700517 * routine when the response comes back from the adapter. No
Salyzyn, Mark8ce3eca2008-01-16 07:39:06 -0800518 * further processing will be done besides deleting the Fib. We
Linus Torvalds1da177e2005-04-16 15:20:36 -0700519 * 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 */
Salyzyn, Markb6ef70f2008-01-08 13:26:43 -0800522 fibptr->flags = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700523 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);
Salyzyn, Mark8ce3eca2008-01-16 07:39:06 -0800534 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700535 /*
536 * Map the fib into 32bits by using the fib number
537 */
538
Raghava Aditya Renukunta423400e2017-02-02 15:53:29 -0800539 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
Linus Torvalds1da177e2005-04-16 15:20:36 -0700548 /*
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);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700557 /*
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;
Salyzyn, Mark8ce3eca2008-01-16 07:39:06 -0800563 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700564 /*
565 * Get a queue entry connect the FIB to it and send an notify
566 * the adapter a command is ready.
567 */
Mark Haverkamp1640a2c2005-09-20 12:57:11 -0700568 hw_fib->header.XferState |= cpu_to_le32(NormalPriority);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700569
Linus Torvalds1da177e2005-04-16 15:20:36 -0700570 /*
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;
Salyzyn, Markb6ef70f2008-01-08 13:26:43 -0800577 fibptr->flags = FIB_CONTEXT_FLAG;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700578 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700579
580 fibptr->done = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700581
Mark Haverkamp1640a2c2005-09-20 12:57:11 -0700582 FIB_COUNTER_INCREMENT(aac_config.FibsSent);
583
Mark Haverkamp1640a2c2005-09-20 12:57:11 -0700584 dprintk((KERN_DEBUG "Fib contents:.\n"));
Mark Haverkamp8e0c5eb2005-10-24 10:52:22 -0700585 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)));
Mark Haverkampa8166a52007-03-15 10:26:22 -0700588 dprintk((KERN_DEBUG " hw_fib va being sent=%p\n",fibptr->hw_fib_va));
Mark Haverkamp1640a2c2005-09-20 12:57:11 -0700589 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
Mark Haverkampc8f7b072006-08-03 08:02:24 -0700592 if (!dev->queues)
Mark Haverkamp65101352006-09-19 08:59:23 -0700593 return -EBUSY;
Mark Haverkamp1640a2c2005-09-20 12:57:11 -0700594
Penchala Narasimha Reddy Chilakala, ERS-HCLTechcacb6dc2009-12-21 18:39:27 +0530595 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);
Mark Haverkamp1640a2c2005-09-20 12:57:11 -0700606 spin_lock_irqsave(&fibptr->event_lock, flags);
Penchala Narasimha Reddy Chilakala, ERS-HCLTechcacb6dc2009-12-21 18:39:27 +0530607 }
608
Mahesh Rajashekhara11604612012-02-08 22:51:04 -0800609 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;
Arnd Bergmannbc127d92018-12-10 22:32:41 +0100625 if (wait_for_completion_interruptible(&fibptr->event_wait)) {
Mahesh Rajashekhara11604612012-02-08 22:51:04 -0800626 fibptr->flags &= ~FIB_CONTEXT_FLAG_WAIT;
627 return -EFAULT;
628 }
629 return 0;
630 }
631 return -EINPROGRESS;
632 }
633
Penchala Narasimha Reddy Chilakala, ERS-HCLTechcacb6dc2009-12-21 18:39:27 +0530634 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
Mark Haverkamp8e0c5eb2005-10-24 10:52:22 -0700645
Linus Torvalds1da177e2005-04-16 15:20:36 -0700646 /*
Salyzyn, Mark8ce3eca2008-01-16 07:39:06 -0800647 * If the caller wanted us to wait for response wait now.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700648 */
Salyzyn, Mark8ce3eca2008-01-16 07:39:06 -0800649
Linus Torvalds1da177e2005-04-16 15:20:36 -0700650 if (wait) {
651 spin_unlock_irqrestore(&fibptr->event_lock, flags);
Mark Haverkamp92033442005-09-20 12:56:50 -0700652 /* 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 */
Ben Collins30002f12012-06-11 14:44:44 -0400660 unsigned long timeout = jiffies + (180 * HZ); /* 3 minutes */
Arnd Bergmannbc127d92018-12-10 22:32:41 +0100661 while (!try_wait_for_completion(&fibptr->event_wait)) {
Mark Haverkamp33524b72006-11-21 10:40:08 -0800662 int blink;
Ben Collins30002f12012-06-11 14:44:44 -0400663 if (time_is_before_eq_jiffies(timeout)) {
Mark Haverkamp28713322007-01-23 14:59:20 -0800664 struct aac_queue * q = &dev->queues->queue[AdapNormCmdQueue];
Mahesh Rajashekharaef616232015-03-26 10:41:30 -0400665 atomic_dec(&q->numpending);
Mark Haverkamp92033442005-09-20 12:56:50 -0700666 if (wait == -1) {
Mark Haverkampbfb35aa82006-02-01 09:30:55 -0800667 printk(KERN_ERR "aacraid: aac_fib_send: first asynchronous command timed out.\n"
Mark Haverkamp92033442005-09-20 12:56:50 -0700668 "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 }
Raghava Aditya Renukunta16ae9dd2017-02-16 12:51:12 -0800674
Guilherme G. Piccolibd257b22017-11-17 19:14:53 -0200675 if (unlikely(pci_channel_offline(dev->pdev)))
Raghava Aditya Renukunta16ae9dd2017-02-16 12:51:12 -0800676 return -EFAULT;
677
Mark Haverkamp33524b72006-11-21 10:40:08 -0800678 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 }
Raghava Aditya Renukunta07beca22016-04-25 23:31:26 -0700686 /*
687 * Allow other processes / CPUS to use core
688 */
689 schedule();
Mark Haverkamp92033442005-09-20 12:56:50 -0700690 }
Arnd Bergmannbc127d92018-12-10 22:32:41 +0100691 } else if (wait_for_completion_interruptible(&fibptr->event_wait)) {
Penchala Narasimha Reddy Chilakala, ERS-HCLTechcacb6dc2009-12-21 18:39:27 +0530692 /* Do nothing ... satisfy
Arnd Bergmannbc127d92018-12-10 22:32:41 +0100693 * wait_for_completion_interruptible must_check */
Mark Salyzyne6990c62008-04-14 14:20:16 -0400694 }
Penchala Narasimha Reddy Chilakala, ERS-HCLTechcacb6dc2009-12-21 18:39:27 +0530695
Mark Haverkamp33bb3b22007-03-15 10:27:21 -0700696 spin_lock_irqsave(&fibptr->event_lock, flags);
Penchala Narasimha Reddy Chilakala, ERS-HCLTechcacb6dc2009-12-21 18:39:27 +0530697 if (fibptr->done == 0) {
Mark Haverkamp33bb3b22007-03-15 10:27:21 -0700698 fibptr->done = 2; /* Tell interrupt we aborted */
Mark Haverkampc8f7b072006-08-03 08:02:24 -0700699 spin_unlock_irqrestore(&fibptr->event_lock, flags);
Penchala Narasimha Reddy Chilakala, ERS-HCLTechcacb6dc2009-12-21 18:39:27 +0530700 return -ERESTARTSYS;
Mark Haverkampc8f7b072006-08-03 08:02:24 -0700701 }
Mark Haverkamp33bb3b22007-03-15 10:27:21 -0700702 spin_unlock_irqrestore(&fibptr->event_lock, flags);
Eric Sesterhenn125e1872006-06-23 02:06:06 -0700703 BUG_ON(fibptr->done == 0);
Salyzyn, Mark8ce3eca2008-01-16 07:39:06 -0800704
Salyzyn, Mark912d4e82007-03-26 09:21:14 -0400705 if(unlikely(fibptr->flags & FIB_CONTEXT_FLAG_TIMED_OUT))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700706 return -ETIMEDOUT;
Salyzyn, Mark912d4e82007-03-26 09:21:14 -0400707 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700708 }
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
Raghava Aditya Renukunta423400e2017-02-02 15:53:29 -0800719int 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;
Dave Carroll7d3af7d2018-04-25 10:24:20 -0600726 struct aac_hba_cmd_req *hbacmd = (struct aac_hba_cmd_req *)
727 fibptr->hw_fib_va;
Raghava Aditya Renukunta423400e2017-02-02 15:53:29 -0800728
729 fibptr->flags = (FIB_CONTEXT_FLAG | FIB_CONTEXT_FLAG_NATIVE_HBA);
730 if (callback) {
731 wait = 0;
732 fibptr->callback = callback;
733 fibptr->callback_data = callback_data;
734 } else
735 wait = 1;
736
737
Dave Carroll7d3af7d2018-04-25 10:24:20 -0600738 hbacmd->iu_type = command;
Raghava Aditya Renukunta423400e2017-02-02 15:53:29 -0800739
Dave Carroll7d3af7d2018-04-25 10:24:20 -0600740 if (command == HBA_IU_TYPE_SCSI_CMD_REQ) {
Raghava Aditya Renukunta423400e2017-02-02 15:53:29 -0800741 /* bit1 of request_id must be 0 */
742 hbacmd->request_id =
743 cpu_to_le32((((u32)(fibptr - dev->fibs)) << 2) + 1);
Hannes Reineckec323eab2017-06-30 19:18:11 +0200744 fibptr->flags |= FIB_CONTEXT_FLAG_SCSI_CMD;
Hannes Reineckeb60710e2017-06-30 19:18:10 +0200745 } else if (command != HBA_IU_TYPE_SCSI_TM_REQ)
Raghava Aditya Renukunta423400e2017-02-02 15:53:29 -0800746 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) {
Raghava Aditya Renukunta16ae9dd2017-02-16 12:51:12 -0800772
Raghava Aditya Renukunta423400e2017-02-02 15:53:29 -0800773 spin_unlock_irqrestore(&fibptr->event_lock, flags);
Raghava Aditya Renukunta16ae9dd2017-02-16 12:51:12 -0800774
Guilherme G. Piccolibd257b22017-11-17 19:14:53 -0200775 if (unlikely(pci_channel_offline(dev->pdev)))
Raghava Aditya Renukunta16ae9dd2017-02-16 12:51:12 -0800776 return -EFAULT;
777
Raghava Aditya Renukunta8c41b9b2017-05-10 09:39:49 -0700778 fibptr->flags |= FIB_CONTEXT_FLAG_WAIT;
Arnd Bergmannbc127d92018-12-10 22:32:41 +0100779 if (wait_for_completion_interruptible(&fibptr->event_wait))
Raghava Aditya Renukunta423400e2017-02-02 15:53:29 -0800780 fibptr->done = 2;
Raghava Aditya Renukunta8c41b9b2017-05-10 09:39:49 -0700781 fibptr->flags &= ~(FIB_CONTEXT_FLAG_WAIT);
782
Raghava Aditya Renukunta423400e2017-02-02 15:53:29 -0800783 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
Salyzyn, Mark8ce3eca2008-01-16 07:39:06 -0800801/**
Linus Torvalds1da177e2005-04-16 15:20:36 -0700802 * 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
Salyzyn, Mark8ce3eca2008-01-16 07:39:06 -0800808 * we are a consumer of, and return the address of the queue entry. It does
809 * not change the state of the queue.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700810 */
811
812int 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 */
Salyzyn, Mark8ce3eca2008-01-16 07:39:06 -0800824 if (le32_to_cpu(*q->headers.consumer) >= q->entries)
825 index = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700826 else
Salyzyn, Mark8ce3eca2008-01-16 07:39:06 -0800827 index = le32_to_cpu(*q->headers.consumer);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700828 *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
844void 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;
Salyzyn, Mark8ce3eca2008-01-16 07:39:06 -0800851
Linus Torvalds1da177e2005-04-16 15:20:36 -0700852 if (le32_to_cpu(*q->headers.consumer) >= q->entries)
853 *q->headers.consumer = cpu_to_le32(1);
854 else
Marcin Slusarz36b8dd12008-03-28 14:48:35 -0700855 le32_add_cpu(q->headers.consumer, 1);
Salyzyn, Mark8ce3eca2008-01-16 07:39:06 -0800856
Linus Torvalds1da177e2005-04-16 15:20:36 -0700857 if (wasfull) {
858 switch (qid) {
859
860 case HostNormCmdQueue:
861 notify = HostNormCmdNotFull;
862 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700863 case HostNormRespQueue:
864 notify = HostNormRespNotFull;
865 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700866 default:
867 BUG();
868 return;
869 }
870 aac_adapter_notify(dev, notify);
871 }
Salyzyn, Mark8ce3eca2008-01-16 07:39:06 -0800872}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700873
874/**
Mark Haverkampbfb35aa82006-02-01 09:30:55 -0800875 * aac_fib_adapter_complete - complete adapter issued fib
Linus Torvalds1da177e2005-04-16 15:20:36 -0700876 * @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
Mark Haverkampbfb35aa82006-02-01 09:30:55 -0800883int aac_fib_adapter_complete(struct fib *fibptr, unsigned short size)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700884{
Mark Haverkampa8166a52007-03-15 10:26:22 -0700885 struct hw_fib * hw_fib = fibptr->hw_fib_va;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700886 struct aac_dev * dev = fibptr->dev;
Mark Haverkamp1640a2c2005-09-20 12:57:11 -0700887 struct aac_queue * q;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700888 unsigned long nointr = 0;
Mark Haverkamp1640a2c2005-09-20 12:57:11 -0700889 unsigned long qflags;
890
Mahesh Rajashekhara85d22bb2012-07-14 18:18:51 +0530891 if (dev->comm_interface == AAC_COMM_MESSAGE_TYPE1 ||
Raghava Aditya Renukuntad1ef4da2017-02-02 15:53:17 -0800892 dev->comm_interface == AAC_COMM_MESSAGE_TYPE2 ||
893 dev->comm_interface == AAC_COMM_MESSAGE_TYPE3) {
Mahesh Rajashekharae8b12f02011-03-17 02:10:32 -0700894 kfree(hw_fib);
895 return 0;
896 }
897
Mark Haverkamp1640a2c2005-09-20 12:57:11 -0700898 if (hw_fib->header.XferState == 0) {
Mark Haverkamp28713322007-01-23 14:59:20 -0800899 if (dev->comm_interface == AAC_COMM_MESSAGE)
Mahesh Rajashekharae8b12f02011-03-17 02:10:32 -0700900 kfree(hw_fib);
Salyzyn, Mark8ce3eca2008-01-16 07:39:06 -0800901 return 0;
Mark Haverkamp1640a2c2005-09-20 12:57:11 -0700902 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700903 /*
904 * If we plan to do anything check the structure type first.
Salyzyn, Mark8ce3eca2008-01-16 07:39:06 -0800905 */
Mahesh Rajashekhara85d22bb2012-07-14 18:18:51 +0530906 if (hw_fib->header.StructType != FIB_MAGIC &&
907 hw_fib->header.StructType != FIB_MAGIC2 &&
908 hw_fib->header.StructType != FIB_MAGIC2_64) {
Mark Haverkamp28713322007-01-23 14:59:20 -0800909 if (dev->comm_interface == AAC_COMM_MESSAGE)
Mahesh Rajashekharae8b12f02011-03-17 02:10:32 -0700910 kfree(hw_fib);
Salyzyn, Mark8ce3eca2008-01-16 07:39:06 -0800911 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700912 }
913 /*
914 * This block handles the case where the adapter had sent us a
915 * command and we have finished processing the command. We
Salyzyn, Mark8ce3eca2008-01-16 07:39:06 -0800916 * call completeFib when we are done processing the command
917 * and want to send a response back to the adapter. This will
Linus Torvalds1da177e2005-04-16 15:20:36 -0700918 * send the completed cdb to the adapter.
919 */
920 if (hw_fib->header.XferState & cpu_to_le32(SentFromAdapter)) {
Mark Haverkamp28713322007-01-23 14:59:20 -0800921 if (dev->comm_interface == AAC_COMM_MESSAGE) {
Mark Haverkamp8e0c5eb2005-10-24 10:52:22 -0700922 kfree (hw_fib);
923 } else {
Salyzyn, Mark8ce3eca2008-01-16 07:39:06 -0800924 u32 index;
925 hw_fib->header.XferState |= cpu_to_le32(HostProcessed);
Mark Haverkamp8e0c5eb2005-10-24 10:52:22 -0700926 if (size) {
927 size += sizeof(struct aac_fibhdr);
Salyzyn, Mark8ce3eca2008-01-16 07:39:06 -0800928 if (size > le16_to_cpu(hw_fib->header.SenderSize))
Mark Haverkamp8e0c5eb2005-10-24 10:52:22 -0700929 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);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700939 }
Salyzyn, Mark8ce3eca2008-01-16 07:39:06 -0800940 } else {
941 printk(KERN_WARNING "aac_fib_adapter_complete: "
942 "Unknown xferstate detected.\n");
943 BUG();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700944 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700945 return 0;
946}
947
948/**
Mark Haverkampbfb35aa82006-02-01 09:30:55 -0800949 * aac_fib_complete - fib completion handler
Linus Torvalds1da177e2005-04-16 15:20:36 -0700950 * @fib: FIB to complete
951 *
952 * Will do all necessary work to complete a FIB.
953 */
Salyzyn, Mark8ce3eca2008-01-16 07:39:06 -0800954
Mark Haverkampbfb35aa82006-02-01 09:30:55 -0800955int aac_fib_complete(struct fib *fibptr)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700956{
Mark Haverkampa8166a52007-03-15 10:26:22 -0700957 struct hw_fib * hw_fib = fibptr->hw_fib_va;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700958
Raghava Aditya Renukunta423400e2017-02-02 15:53:29 -0800959 if (fibptr->flags & FIB_CONTEXT_FLAG_NATIVE_HBA) {
960 fib_dealloc(fibptr);
961 return 0;
962 }
963
Linus Torvalds1da177e2005-04-16 15:20:36 -0700964 /*
Raghava Aditya Renukunta423400e2017-02-02 15:53:29 -0800965 * Check for a fib which has already been completed or with a
966 * status wait timeout
Linus Torvalds1da177e2005-04-16 15:20:36 -0700967 */
968
Raghava Aditya Renukunta423400e2017-02-02 15:53:29 -0800969 if (hw_fib->header.XferState == 0 || fibptr->done == 2)
Salyzyn, Mark8ce3eca2008-01-16 07:39:06 -0800970 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700971 /*
972 * If we plan to do anything check the structure type first.
Salyzyn, Mark8ce3eca2008-01-16 07:39:06 -0800973 */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700974
Mahesh Rajashekhara85d22bb2012-07-14 18:18:51 +0530975 if (hw_fib->header.StructType != FIB_MAGIC &&
976 hw_fib->header.StructType != FIB_MAGIC2 &&
977 hw_fib->header.StructType != FIB_MAGIC2_64)
Salyzyn, Mark8ce3eca2008-01-16 07:39:06 -0800978 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700979 /*
Salyzyn, Mark8ce3eca2008-01-16 07:39:06 -0800980 * This block completes a cdb which orginated on the host and we
Linus Torvalds1da177e2005-04-16 15:20:36 -0700981 * 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 */
Penchala Narasimha Reddy Chilakala, ERS-HCLTechcacb6dc2009-12-21 18:39:27 +0530985
Linus Torvalds1da177e2005-04-16 15:20:36 -0700986 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();
Salyzyn, Mark8ce3eca2008-01-16 07:39:06 -08001002 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001003 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
1015void aac_printf(struct aac_dev *dev, u32 val)
1016{
Linus Torvalds1da177e2005-04-16 15:20:36 -07001017 char *cp = dev->printfbuf;
Mark Haverkamp 7c00ffa2005-05-16 18:28:42 -07001018 if (dev->printf_enabled)
1019 {
1020 int length = val & 0xffff;
1021 int level = (val >> 16) & 0xffff;
Salyzyn, Mark8ce3eca2008-01-16 07:39:06 -08001022
Mark Haverkamp 7c00ffa2005-05-16 18:28:42 -07001023 /*
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)
Mark Haverkamp1241f352006-03-27 09:44:23 -08001032 printk(KERN_WARNING "%s:%s", dev->name, cp);
Mark Haverkamp 7c00ffa2005-05-16 18:28:42 -07001033 else
Mark Haverkamp1241f352006-03-27 09:44:23 -08001034 printk(KERN_INFO "%s:%s", dev->name, cp);
Mark Haverkamp 7c00ffa2005-05-16 18:28:42 -07001035 }
Salyzyn, Mark8ce3eca2008-01-16 07:39:06 -08001036 memset(cp, 0, 256);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001037}
1038
Raghava Aditya Renukunta9cb62fa2016-04-25 23:32:09 -07001039static 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
1045static 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}
Mark Haverkamp131256c2005-09-26 13:04:56 -07001064
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
Mahesh Rajashekhara495c0212015-03-26 10:41:25 -04001074#define AIF_SNIFF_TIMEOUT (500*HZ)
Mark Haverkamp131256c2005-09-26 13:04:56 -07001075static void aac_handle_aif(struct aac_dev * dev, struct fib * fibptr)
1076{
Mark Haverkampa8166a52007-03-15 10:26:22 -07001077 struct hw_fib * hw_fib = fibptr->hw_fib_va;
Mark Haverkamp131256c2005-09-26 13:04:56 -07001078 struct aac_aifcmd * aifcmd = (struct aac_aifcmd *)hw_fib->data;
Salyzyn, Mark0995ad32008-01-11 11:56:07 -08001079 u32 channel, id, lun, container;
Mark Haverkamp131256c2005-09-26 13:04:56 -07001080 struct scsi_device *device;
1081 enum {
1082 NOTHING,
1083 DELETE,
1084 ADD,
1085 CHANGE
Salyzyn, Mark0995ad32008-01-11 11:56:07 -08001086 } device_config_needed = NOTHING;
Mark Haverkamp131256c2005-09-26 13:04:56 -07001087
1088 /* Sniff for container changes */
1089
Mark Haverkampc8f7b072006-08-03 08:02:24 -07001090 if (!dev || !dev->fsa_dev)
Mark Haverkamp131256c2005-09-26 13:04:56 -07001091 return;
Salyzyn, Mark0995ad32008-01-11 11:56:07 -08001092 container = channel = id = lun = (u32)-1;
Mark Haverkamp131256c2005-09-26 13:04:56 -07001093
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:
Christoph Hellwigf3307f72007-11-08 17:27:47 +00001102 switch (le32_to_cpu(((__le32 *)aifcmd->data)[0])) {
Mahesh Rajashekharadab04b02015-03-26 10:41:31 -04001103 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);
Raghava Aditya Renukunta423400e2017-02-02 15:53:29 -08001122 device_config_needed = DELETE;
Mahesh Rajashekharadab04b02015-03-26 10:41:31 -04001123 break;
Raghava Aditya Renukunta423400e2017-02-02 15:53:29 -08001124
Mark Haverkamp131256c2005-09-26 13:04:56 -07001125 /*
1126 * Morph or Expand complete
1127 */
1128 case AifDenMorphComplete:
1129 case AifDenVolumeExtendComplete:
Christoph Hellwigf3307f72007-11-08 17:27:47 +00001130 container = le32_to_cpu(((__le32 *)aifcmd->data)[1]);
Mark Haverkamp131256c2005-09-26 13:04:56 -07001131 if (container >= dev->maximum_num_containers)
1132 break;
1133
1134 /*
Christoph Hellwigf64a1812005-10-31 18:32:08 +01001135 * Find the scsi_device associated with the SCSI
Mark Haverkamp131256c2005-09-26 13:04:56 -07001136 * 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)) {
Salyzyn, Mark8ce3eca2008-01-16 07:39:06 -08001142 device = scsi_device_lookup(dev->scsi_host_ptr,
1143 CONTAINER_TO_CHANNEL(container),
1144 CONTAINER_TO_ID(container),
Mark Haverkamp131256c2005-09-26 13:04:56 -07001145 CONTAINER_TO_LUN(container));
1146 if (device) {
1147 dev->fsa_dev[container].config_needed = CHANGE;
1148 dev->fsa_dev[container].config_waiting_on = AifEnConfigChange;
Mark Haverkamp31876f32006-03-27 09:43:55 -08001149 dev->fsa_dev[container].config_waiting_stamp = jiffies;
Mark Haverkamp131256c2005-09-26 13:04:56 -07001150 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;
Mark Haverkamp31876f32006-03-27 09:43:55 -08001162 if ((dev->fsa_dev[container].config_waiting_on ==
Christoph Hellwigf3307f72007-11-08 17:27:47 +00001163 le32_to_cpu(*(__le32 *)aifcmd->data)) &&
Mark Haverkamp31876f32006-03-27 09:43:55 -08001164 time_before(jiffies, dev->fsa_dev[container].config_waiting_stamp + AIF_SNIFF_TIMEOUT))
Mark Haverkamp131256c2005-09-26 13:04:56 -07001165 dev->fsa_dev[container].config_waiting_on = 0;
1166 } else for (container = 0;
1167 container < dev->maximum_num_containers; ++container) {
Mark Haverkamp31876f32006-03-27 09:43:55 -08001168 if ((dev->fsa_dev[container].config_waiting_on ==
Christoph Hellwigf3307f72007-11-08 17:27:47 +00001169 le32_to_cpu(*(__le32 *)aifcmd->data)) &&
Mark Haverkamp31876f32006-03-27 09:43:55 -08001170 time_before(jiffies, dev->fsa_dev[container].config_waiting_stamp + AIF_SNIFF_TIMEOUT))
Mark Haverkamp131256c2005-09-26 13:04:56 -07001171 dev->fsa_dev[container].config_waiting_on = 0;
1172 }
1173 break;
1174
1175 case AifCmdEventNotify:
Christoph Hellwigf3307f72007-11-08 17:27:47 +00001176 switch (le32_to_cpu(((__le32 *)aifcmd->data)[0])) {
Salyzyn, Mark95e852e2008-01-08 12:01:07 -08001177 case AifEnBatteryEvent:
1178 dev->cache_protected =
1179 (((__le32 *)aifcmd->data)[1] == cpu_to_le32(3));
1180 break;
Mark Haverkamp131256c2005-09-26 13:04:56 -07001181 /*
1182 * Add an Array.
1183 */
1184 case AifEnAddContainer:
Christoph Hellwigf3307f72007-11-08 17:27:47 +00001185 container = le32_to_cpu(((__le32 *)aifcmd->data)[1]);
Mark Haverkamp131256c2005-09-26 13:04:56 -07001186 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;
Mark Haverkamp31876f32006-03-27 09:43:55 -08001191 dev->fsa_dev[container].config_waiting_stamp = jiffies;
Mark Haverkamp131256c2005-09-26 13:04:56 -07001192 break;
1193
1194 /*
1195 * Delete an Array.
1196 */
1197 case AifEnDeleteContainer:
Christoph Hellwigf3307f72007-11-08 17:27:47 +00001198 container = le32_to_cpu(((__le32 *)aifcmd->data)[1]);
Mark Haverkamp131256c2005-09-26 13:04:56 -07001199 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;
Mark Haverkamp31876f32006-03-27 09:43:55 -08001204 dev->fsa_dev[container].config_waiting_stamp = jiffies;
Mark Haverkamp131256c2005-09-26 13:04:56 -07001205 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:
Christoph Hellwigf3307f72007-11-08 17:27:47 +00001212 container = le32_to_cpu(((__le32 *)aifcmd->data)[1]);
Mark Haverkamp131256c2005-09-26 13:04:56 -07001213 if (container >= dev->maximum_num_containers)
1214 break;
Mark Haverkamp31876f32006-03-27 09:43:55 -08001215 if (dev->fsa_dev[container].config_waiting_on &&
1216 time_before(jiffies, dev->fsa_dev[container].config_waiting_stamp + AIF_SNIFF_TIMEOUT))
Mark Haverkamp131256c2005-09-26 13:04:56 -07001217 break;
1218 dev->fsa_dev[container].config_needed = CHANGE;
1219 dev->fsa_dev[container].config_waiting_on =
1220 AifEnConfigChange;
Mark Haverkamp31876f32006-03-27 09:43:55 -08001221 dev->fsa_dev[container].config_waiting_stamp = jiffies;
Mark Haverkamp131256c2005-09-26 13:04:56 -07001222 break;
1223
1224 case AifEnConfigChange:
1225 break;
1226
Salyzyn, Markcb1042f2008-01-17 09:25:07 -08001227 case AifEnAddJBOD:
1228 case AifEnDeleteJBOD:
1229 container = le32_to_cpu(((__le32 *)aifcmd->data)[1]);
Mark Salyzyna4576b52008-04-30 15:47:35 -04001230 if ((container >> 28)) {
1231 container = (u32)-1;
Salyzyn, Markcb1042f2008-01-17 09:25:07 -08001232 break;
Mark Salyzyna4576b52008-04-30 15:47:35 -04001233 }
Salyzyn, Markcb1042f2008-01-17 09:25:07 -08001234 channel = (container >> 24) & 0xF;
Mark Salyzyna4576b52008-04-30 15:47:35 -04001235 if (channel >= dev->maximum_num_channels) {
1236 container = (u32)-1;
Salyzyn, Markcb1042f2008-01-17 09:25:07 -08001237 break;
Mark Salyzyna4576b52008-04-30 15:47:35 -04001238 }
Salyzyn, Markcb1042f2008-01-17 09:25:07 -08001239 id = container & 0xFFFF;
Mark Salyzyna4576b52008-04-30 15:47:35 -04001240 if (id >= dev->maximum_num_physicals) {
1241 container = (u32)-1;
Salyzyn, Markcb1042f2008-01-17 09:25:07 -08001242 break;
Mark Salyzyna4576b52008-04-30 15:47:35 -04001243 }
Salyzyn, Markcb1042f2008-01-17 09:25:07 -08001244 lun = (container >> 16) & 0xFF;
Mark Salyzyna4576b52008-04-30 15:47:35 -04001245 container = (u32)-1;
Salyzyn, Markcb1042f2008-01-17 09:25:07 -08001246 channel = aac_phys_to_logical(channel);
1247 device_config_needed =
1248 (((__le32 *)aifcmd->data)[0] ==
1249 cpu_to_le32(AifEnAddJBOD)) ? ADD : DELETE;
Rajashekhara, Mahesh5ca05592010-05-10 04:05:50 -07001250 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 }
Salyzyn, Markcb1042f2008-01-17 09:25:07 -08001260 break;
1261
Salyzyn, Mark0995ad32008-01-11 11:56:07 -08001262 case AifEnEnclosureManagement:
Salyzyn, Markcb1042f2008-01-17 09:25:07 -08001263 /*
1264 * If in JBOD mode, automatic exposure of new
1265 * physical target to be suppressed until configured.
1266 */
1267 if (dev->jbod)
1268 break;
Salyzyn, Mark0995ad32008-01-11 11:56:07 -08001269 switch (le32_to_cpu(((__le32 *)aifcmd->data)[3])) {
1270 case EM_DRIVE_INSERTION:
1271 case EM_DRIVE_REMOVAL:
Mahesh Rajashekhara46154a02015-03-26 10:41:22 -04001272 case EM_SES_DRIVE_INSERTION:
1273 case EM_SES_DRIVE_REMOVAL:
Salyzyn, Mark0995ad32008-01-11 11:56:07 -08001274 container = le32_to_cpu(
1275 ((__le32 *)aifcmd->data)[2]);
Mark Salyzyna4576b52008-04-30 15:47:35 -04001276 if ((container >> 28)) {
1277 container = (u32)-1;
Salyzyn, Mark0995ad32008-01-11 11:56:07 -08001278 break;
Mark Salyzyna4576b52008-04-30 15:47:35 -04001279 }
Salyzyn, Mark0995ad32008-01-11 11:56:07 -08001280 channel = (container >> 24) & 0xF;
Mark Salyzyna4576b52008-04-30 15:47:35 -04001281 if (channel >= dev->maximum_num_channels) {
1282 container = (u32)-1;
Salyzyn, Mark0995ad32008-01-11 11:56:07 -08001283 break;
Mark Salyzyna4576b52008-04-30 15:47:35 -04001284 }
Salyzyn, Mark0995ad32008-01-11 11:56:07 -08001285 id = container & 0xFFFF;
1286 lun = (container >> 16) & 0xFF;
Mark Salyzyna4576b52008-04-30 15:47:35 -04001287 container = (u32)-1;
Salyzyn, Mark0995ad32008-01-11 11:56:07 -08001288 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 =
Mahesh Rajashekhara46154a02015-03-26 10:41:22 -04001299 ((((__le32 *)aifcmd->data)[3]
1300 == cpu_to_le32(EM_DRIVE_INSERTION)) ||
1301 (((__le32 *)aifcmd->data)[3]
1302 == cpu_to_le32(EM_SES_DRIVE_INSERTION))) ?
Salyzyn, Mark0995ad32008-01-11 11:56:07 -08001303 ADD : DELETE;
1304 break;
1305 }
Gustavo A. R. Silva5e420fe2019-02-15 15:42:42 -06001306 break;
1307 case AifBuManagerEvent:
1308 aac_handle_aif_bu(dev, aifcmd);
Salyzyn, Mark0995ad32008-01-11 11:56:07 -08001309 break;
Mark Haverkamp131256c2005-09-26 13:04:56 -07001310 }
1311
1312 /*
1313 * If we are waiting on something and this happens to be
1314 * that thing then set the re-configure flag.
1315 */
1316 if (container != (u32)-1) {
1317 if (container >= dev->maximum_num_containers)
1318 break;
Mark Haverkamp31876f32006-03-27 09:43:55 -08001319 if ((dev->fsa_dev[container].config_waiting_on ==
Christoph Hellwigf3307f72007-11-08 17:27:47 +00001320 le32_to_cpu(*(__le32 *)aifcmd->data)) &&
Mark Haverkamp31876f32006-03-27 09:43:55 -08001321 time_before(jiffies, dev->fsa_dev[container].config_waiting_stamp + AIF_SNIFF_TIMEOUT))
Mark Haverkamp131256c2005-09-26 13:04:56 -07001322 dev->fsa_dev[container].config_waiting_on = 0;
1323 } else for (container = 0;
1324 container < dev->maximum_num_containers; ++container) {
Mark Haverkamp31876f32006-03-27 09:43:55 -08001325 if ((dev->fsa_dev[container].config_waiting_on ==
Christoph Hellwigf3307f72007-11-08 17:27:47 +00001326 le32_to_cpu(*(__le32 *)aifcmd->data)) &&
Mark Haverkamp31876f32006-03-27 09:43:55 -08001327 time_before(jiffies, dev->fsa_dev[container].config_waiting_stamp + AIF_SNIFF_TIMEOUT))
Mark Haverkamp131256c2005-09-26 13:04:56 -07001328 dev->fsa_dev[container].config_waiting_on = 0;
1329 }
1330 break;
1331
1332 case AifCmdJobProgress:
1333 /*
1334 * These are job progress AIF's. When a Clear is being
1335 * done on a container it is initially created then hidden from
1336 * the OS. When the clear completes we don't get a config
1337 * change so we monitor the job status complete on a clear then
1338 * wait for a container change.
1339 */
1340
Christoph Hellwigf3307f72007-11-08 17:27:47 +00001341 if (((__le32 *)aifcmd->data)[1] == cpu_to_le32(AifJobCtrZero) &&
1342 (((__le32 *)aifcmd->data)[6] == ((__le32 *)aifcmd->data)[5] ||
1343 ((__le32 *)aifcmd->data)[4] == cpu_to_le32(AifJobStsSuccess))) {
Mark Haverkamp131256c2005-09-26 13:04:56 -07001344 for (container = 0;
1345 container < dev->maximum_num_containers;
1346 ++container) {
1347 /*
1348 * Stomp on all config sequencing for all
1349 * containers?
1350 */
1351 dev->fsa_dev[container].config_waiting_on =
1352 AifEnContainerChange;
1353 dev->fsa_dev[container].config_needed = ADD;
Mark Haverkamp31876f32006-03-27 09:43:55 -08001354 dev->fsa_dev[container].config_waiting_stamp =
1355 jiffies;
Mark Haverkamp131256c2005-09-26 13:04:56 -07001356 }
1357 }
Christoph Hellwigf3307f72007-11-08 17:27:47 +00001358 if (((__le32 *)aifcmd->data)[1] == cpu_to_le32(AifJobCtrZero) &&
1359 ((__le32 *)aifcmd->data)[6] == 0 &&
1360 ((__le32 *)aifcmd->data)[4] == cpu_to_le32(AifJobStsRunning)) {
Mark Haverkamp131256c2005-09-26 13:04:56 -07001361 for (container = 0;
1362 container < dev->maximum_num_containers;
1363 ++container) {
1364 /*
1365 * Stomp on all config sequencing for all
1366 * containers?
1367 */
1368 dev->fsa_dev[container].config_waiting_on =
1369 AifEnContainerChange;
1370 dev->fsa_dev[container].config_needed = DELETE;
Mark Haverkamp31876f32006-03-27 09:43:55 -08001371 dev->fsa_dev[container].config_waiting_stamp =
1372 jiffies;
Mark Haverkamp131256c2005-09-26 13:04:56 -07001373 }
1374 }
1375 break;
1376 }
1377
Mark Salyzyna4576b52008-04-30 15:47:35 -04001378 container = 0;
1379retry_next:
Colin Ian Kinge13949a2019-02-02 10:40:52 +00001380 if (device_config_needed == NOTHING) {
1381 for (; container < dev->maximum_num_containers; ++container) {
1382 if ((dev->fsa_dev[container].config_waiting_on == 0) &&
1383 (dev->fsa_dev[container].config_needed != NOTHING) &&
1384 time_before(jiffies, dev->fsa_dev[container].config_waiting_stamp + AIF_SNIFF_TIMEOUT)) {
1385 device_config_needed =
1386 dev->fsa_dev[container].config_needed;
1387 dev->fsa_dev[container].config_needed = NOTHING;
1388 channel = CONTAINER_TO_CHANNEL(container);
1389 id = CONTAINER_TO_ID(container);
1390 lun = CONTAINER_TO_LUN(container);
1391 break;
1392 }
Mark Haverkamp131256c2005-09-26 13:04:56 -07001393 }
1394 }
1395 if (device_config_needed == NOTHING)
1396 return;
1397
1398 /*
1399 * If we decided that a re-configuration needs to be done,
1400 * schedule it here on the way out the door, please close the door
1401 * behind you.
1402 */
1403
Mark Haverkamp131256c2005-09-26 13:04:56 -07001404 /*
Christoph Hellwigf64a1812005-10-31 18:32:08 +01001405 * Find the scsi_device associated with the SCSI address,
Mark Haverkamp131256c2005-09-26 13:04:56 -07001406 * and mark it as changed, invalidating the cache. This deals
1407 * with changes to existing device IDs.
1408 */
1409
1410 if (!dev || !dev->scsi_host_ptr)
1411 return;
1412 /*
Mark Haverkampbfb35aa82006-02-01 09:30:55 -08001413 * force reload of disk info via aac_probe_container
Mark Haverkamp131256c2005-09-26 13:04:56 -07001414 */
Salyzyn, Mark0995ad32008-01-11 11:56:07 -08001415 if ((channel == CONTAINER_CHANNEL) &&
1416 (device_config_needed != NOTHING)) {
1417 if (dev->fsa_dev[container].valid == 1)
1418 dev->fsa_dev[container].valid = 2;
Mark Haverkampbfb35aa82006-02-01 09:30:55 -08001419 aac_probe_container(dev, container);
Salyzyn, Mark0995ad32008-01-11 11:56:07 -08001420 }
1421 device = scsi_device_lookup(dev->scsi_host_ptr, channel, id, lun);
Mark Haverkamp131256c2005-09-26 13:04:56 -07001422 if (device) {
1423 switch (device_config_needed) {
1424 case DELETE:
Rajashekhara, Mahesh9cccde92010-05-10 04:29:25 -07001425#if (defined(AAC_DEBUG_INSTRUMENT_AIF_DELETE))
1426 scsi_remove_device(device);
1427#else
Salyzyn, Mark0995ad32008-01-11 11:56:07 -08001428 if (scsi_device_online(device)) {
1429 scsi_device_set_state(device, SDEV_OFFLINE);
1430 sdev_printk(KERN_INFO, device,
1431 "Device offlined - %s\n",
1432 (channel == CONTAINER_CHANNEL) ?
1433 "array deleted" :
1434 "enclosure services event");
1435 }
Rajashekhara, Mahesh9cccde92010-05-10 04:29:25 -07001436#endif
Salyzyn, Mark0995ad32008-01-11 11:56:07 -08001437 break;
1438 case ADD:
1439 if (!scsi_device_online(device)) {
1440 sdev_printk(KERN_INFO, device,
1441 "Device online - %s\n",
1442 (channel == CONTAINER_CHANNEL) ?
1443 "array created" :
1444 "enclosure services event");
1445 scsi_device_set_state(device, SDEV_RUNNING);
1446 }
1447 /* FALLTHRU */
Mark Haverkamp131256c2005-09-26 13:04:56 -07001448 case CHANGE:
Salyzyn, Mark0995ad32008-01-11 11:56:07 -08001449 if ((channel == CONTAINER_CHANNEL)
1450 && (!dev->fsa_dev[container].valid)) {
Rajashekhara, Mahesh9cccde92010-05-10 04:29:25 -07001451#if (defined(AAC_DEBUG_INSTRUMENT_AIF_DELETE))
1452 scsi_remove_device(device);
1453#else
Salyzyn, Mark0995ad32008-01-11 11:56:07 -08001454 if (!scsi_device_online(device))
1455 break;
1456 scsi_device_set_state(device, SDEV_OFFLINE);
1457 sdev_printk(KERN_INFO, device,
1458 "Device offlined - %s\n",
1459 "array failed");
Rajashekhara, Mahesh9cccde92010-05-10 04:29:25 -07001460#endif
Salyzyn, Mark0995ad32008-01-11 11:56:07 -08001461 break;
1462 }
Mark Haverkamp131256c2005-09-26 13:04:56 -07001463 scsi_rescan_device(&device->sdev_gendev);
1464
1465 default:
1466 break;
1467 }
1468 scsi_device_put(device);
Salyzyn, Mark0995ad32008-01-11 11:56:07 -08001469 device_config_needed = NOTHING;
Mark Haverkamp131256c2005-09-26 13:04:56 -07001470 }
Salyzyn, Mark0995ad32008-01-11 11:56:07 -08001471 if (device_config_needed == ADD)
1472 scsi_add_device(dev->scsi_host_ptr, channel, id, lun);
Mark Salyzyna4576b52008-04-30 15:47:35 -04001473 if (channel == CONTAINER_CHANNEL) {
1474 container++;
1475 device_config_needed = NOTHING;
1476 goto retry_next;
1477 }
Mark Haverkamp131256c2005-09-26 13:04:56 -07001478}
1479
Raghava Aditya Renukunta31364322017-02-02 15:53:33 -08001480static int _aac_reset_adapter(struct aac_dev *aac, int forced, u8 reset_type)
Mark Haverkamp8c867b22006-08-03 08:03:30 -07001481{
1482 int index, quirks;
Mahesh Rajashekhara8b1462e2015-08-28 06:38:38 -04001483 int retval;
Mark Haverkamp8c867b22006-08-03 08:03:30 -07001484 struct Scsi_Host *host;
1485 struct scsi_device *dev;
1486 struct scsi_cmnd *command;
1487 struct scsi_cmnd *command_list;
Salyzyn, Mark29c97682007-06-12 09:33:54 -04001488 int jafo = 0;
Raghava Aditya Renukunta31364322017-02-02 15:53:33 -08001489 int bled;
Raghava Aditya Renukunta8105d392017-05-10 09:39:36 -07001490 u64 dmamask;
Raghava Aditya Renukunta8c41b9b2017-05-10 09:39:49 -07001491 int num_of_fibs = 0;
Mark Haverkamp8c867b22006-08-03 08:03:30 -07001492
1493 /*
1494 * Assumptions:
Salyzyn, Mark29c97682007-06-12 09:33:54 -04001495 * - host is locked, unless called by the aacraid thread.
1496 * (a matter of convenience, due to legacy issues surrounding
1497 * eh_host_adapter_reset).
Mark Haverkamp8c867b22006-08-03 08:03:30 -07001498 * - in_reset is asserted, so no new i/o is getting to the
1499 * card.
Salyzyn, Mark29c97682007-06-12 09:33:54 -04001500 * - The card is dead, or will be very shortly ;-/ so no new
1501 * commands are completing in the interrupt service.
Mark Haverkamp8c867b22006-08-03 08:03:30 -07001502 */
1503 host = aac->scsi_host_ptr;
1504 scsi_block_requests(host);
1505 aac_adapter_disable_int(aac);
Dave Carroll1c6b41f2018-04-03 15:50:42 -06001506 if (aac->thread && aac->thread->pid != current->pid) {
Salyzyn, Mark29c97682007-06-12 09:33:54 -04001507 spin_unlock_irq(host->host_lock);
1508 kthread_stop(aac->thread);
Dave Carroll1c6b41f2018-04-03 15:50:42 -06001509 aac->thread = NULL;
Salyzyn, Mark29c97682007-06-12 09:33:54 -04001510 jafo = 1;
1511 }
Mark Haverkamp8c867b22006-08-03 08:03:30 -07001512
1513 /*
1514 * If a positive health, means in a known DEAD PANIC
1515 * state and the adapter could be reset to `try again'.
1516 */
Raghava Aditya Renukunta31364322017-02-02 15:53:33 -08001517 bled = forced ? 0 : aac_adapter_check_health(aac);
1518 retval = aac_adapter_restart(aac, bled, reset_type);
Mark Haverkamp8c867b22006-08-03 08:03:30 -07001519
1520 if (retval)
1521 goto out;
Mark Haverkamp8c867b22006-08-03 08:03:30 -07001522
Mark Haverkampd18b4482006-11-21 10:40:31 -08001523 /*
1524 * Loop through the fibs, close the synchronous FIBS
1525 */
Raghava Aditya Renukunta8c41b9b2017-05-10 09:39:49 -07001526 retval = 1;
1527 num_of_fibs = aac->scsi_host_ptr->can_queue + AAC_NUM_MGT_FIB;
1528 for (index = 0; index < num_of_fibs; index++) {
1529
Mark Haverkampd18b4482006-11-21 10:40:31 -08001530 struct fib *fib = &aac->fibs[index];
Raghava Aditya Renukunta8c41b9b2017-05-10 09:39:49 -07001531 __le32 XferState = fib->hw_fib_va->header.XferState;
1532 bool is_response_expected = false;
1533
1534 if (!(XferState & cpu_to_le32(NoResponseExpected | Async)) &&
1535 (XferState & cpu_to_le32(ResponseExpected)))
1536 is_response_expected = true;
1537
1538 if (is_response_expected
1539 || fib->flags & FIB_CONTEXT_FLAG_WAIT) {
Mark Haverkampd18b4482006-11-21 10:40:31 -08001540 unsigned long flagv;
1541 spin_lock_irqsave(&fib->event_lock, flagv);
Arnd Bergmannbc127d92018-12-10 22:32:41 +01001542 complete(&fib->event_wait);
Mark Haverkampd18b4482006-11-21 10:40:31 -08001543 spin_unlock_irqrestore(&fib->event_lock, flagv);
1544 schedule();
Mark Haverkamp33bb3b22007-03-15 10:27:21 -07001545 retval = 0;
Mark Haverkampd18b4482006-11-21 10:40:31 -08001546 }
1547 }
Mark Haverkamp33bb3b22007-03-15 10:27:21 -07001548 /* Give some extra time for ioctls to complete. */
1549 if (retval == 0)
1550 ssleep(2);
Mark Haverkamp8c867b22006-08-03 08:03:30 -07001551 index = aac->cardtype;
1552
1553 /*
1554 * Re-initialize the adapter, first free resources, then carefully
1555 * apply the initialization sequence to come back again. Only risk
1556 * is a change in Firmware dropping cache, it is assumed the caller
1557 * will ensure that i/o is queisced and the card is flushed in that
1558 * case.
1559 */
Guilherme G. Piccolie4717292017-11-17 19:14:55 -02001560 aac_free_irq(aac);
Mark Haverkamp8c867b22006-08-03 08:03:30 -07001561 aac_fib_map_free(aac);
Mahesh Rajashekharaf4819732017-04-05 16:14:16 +05301562 dma_free_coherent(&aac->pdev->dev, aac->comm_size, aac->comm_addr,
1563 aac->comm_phys);
Mark Haverkamp8c867b22006-08-03 08:03:30 -07001564 aac->comm_addr = NULL;
1565 aac->comm_phys = 0;
1566 kfree(aac->queues);
1567 aac->queues = NULL;
Mark Haverkamp8c867b22006-08-03 08:03:30 -07001568 kfree(aac->fsa_dev);
1569 aac->fsa_dev = NULL;
Raghava Aditya Renukunta8105d392017-05-10 09:39:36 -07001570
1571 dmamask = DMA_BIT_MASK(32);
Salyzyn, Mark94cf6ba2007-12-13 16:14:18 -08001572 quirks = aac_get_driver_ident(index)->quirks;
Raghava Aditya Renukunta8105d392017-05-10 09:39:36 -07001573 if (quirks & AAC_QUIRK_31BIT)
1574 retval = pci_set_dma_mask(aac->pdev, dmamask);
1575 else if (!(quirks & AAC_QUIRK_SRC))
1576 retval = pci_set_dma_mask(aac->pdev, dmamask);
1577 else
1578 retval = pci_set_consistent_dma_mask(aac->pdev, dmamask);
1579
1580 if (quirks & AAC_QUIRK_31BIT && !retval) {
1581 dmamask = DMA_BIT_MASK(31);
1582 retval = pci_set_consistent_dma_mask(aac->pdev, dmamask);
Mark Haverkamp8c867b22006-08-03 08:03:30 -07001583 }
Raghava Aditya Renukunta8105d392017-05-10 09:39:36 -07001584
1585 if (retval)
1586 goto out;
1587
Mark Haverkamp8c867b22006-08-03 08:03:30 -07001588 if ((retval = (*(aac_get_driver_ident(index)->init))(aac)))
1589 goto out;
Raghava Aditya Renukunta8105d392017-05-10 09:39:36 -07001590
Salyzyn, Mark29c97682007-06-12 09:33:54 -04001591 if (jafo) {
Kees Cookf1701682013-07-03 15:04:58 -07001592 aac->thread = kthread_run(aac_command_thread, aac, "%s",
1593 aac->name);
Salyzyn, Mark29c97682007-06-12 09:33:54 -04001594 if (IS_ERR(aac->thread)) {
1595 retval = PTR_ERR(aac->thread);
Dave Carroll1c6b41f2018-04-03 15:50:42 -06001596 aac->thread = NULL;
Salyzyn, Mark29c97682007-06-12 09:33:54 -04001597 goto out;
1598 }
Mark Haverkamp8c867b22006-08-03 08:03:30 -07001599 }
1600 (void)aac_get_adapter_info(aac);
Mark Haverkamp8c867b22006-08-03 08:03:30 -07001601 if ((quirks & AAC_QUIRK_34SG) && (host->sg_tablesize > 34)) {
Salyzyn, Mark8ce3eca2008-01-16 07:39:06 -08001602 host->sg_tablesize = 34;
1603 host->max_sectors = (host->sg_tablesize * 8) + 112;
1604 }
1605 if ((quirks & AAC_QUIRK_17SG) && (host->sg_tablesize > 17)) {
1606 host->sg_tablesize = 17;
1607 host->max_sectors = (host->sg_tablesize * 8) + 112;
1608 }
Mark Haverkamp8c867b22006-08-03 08:03:30 -07001609 aac_get_config_status(aac, 1);
1610 aac_get_containers(aac);
1611 /*
1612 * This is where the assumption that the Adapter is quiesced
1613 * is important.
1614 */
1615 command_list = NULL;
1616 __shost_for_each_device(dev, host) {
1617 unsigned long flags;
1618 spin_lock_irqsave(&dev->list_lock, flags);
1619 list_for_each_entry(command, &dev->cmd_list, list)
1620 if (command->SCp.phase == AAC_OWNER_FIRMWARE) {
1621 command->SCp.buffer = (struct scatterlist *)command_list;
1622 command_list = command;
1623 }
1624 spin_unlock_irqrestore(&dev->list_lock, flags);
1625 }
1626 while ((command = command_list)) {
1627 command_list = (struct scsi_cmnd *)command->SCp.buffer;
1628 command->SCp.buffer = NULL;
1629 command->result = DID_OK << 16
1630 | COMMAND_COMPLETE << 8
1631 | SAM_STAT_TASK_SET_FULL;
1632 command->SCp.phase = AAC_OWNER_ERROR_HANDLER;
1633 command->scsi_done(command);
1634 }
Raghava Aditya Renukuntaa2d03212017-02-16 12:51:18 -08001635 /*
Raghava Aditya Renukunta95900622017-12-26 20:34:25 -08001636 * Any Device that was already marked offline needs to be marked
1637 * running
Raghava Aditya Renukuntaa2d03212017-02-16 12:51:18 -08001638 */
1639 __shost_for_each_device(dev, host) {
Raghava Aditya Renukunta95900622017-12-26 20:34:25 -08001640 if (!scsi_device_online(dev))
1641 scsi_device_set_state(dev, SDEV_RUNNING);
Raghava Aditya Renukuntaa2d03212017-02-16 12:51:18 -08001642 }
Mark Haverkamp8c867b22006-08-03 08:03:30 -07001643 retval = 0;
1644
1645out:
1646 aac->in_reset = 0;
1647 scsi_unblock_requests(host);
Raghava Aditya Renukuntac5313ae2017-12-26 20:34:24 -08001648
Raghava Aditya Renukuntaa2d03212017-02-16 12:51:18 -08001649 /*
1650 * Issue bus rescan to catch any configuration that might have
1651 * occurred
1652 */
Raghava Aditya Renukuntafe523752017-12-26 20:34:48 -08001653 if (!retval && !is_kdump_kernel()) {
Raghava Aditya Renukunta8a30e502017-12-26 20:34:47 -08001654 dev_info(&aac->pdev->dev, "Scheduling bus rescan\n");
1655 aac_schedule_safw_scan_worker(aac);
Raghava Aditya Renukuntaa2d03212017-02-16 12:51:18 -08001656 }
Raghava Aditya Renukunta8a30e502017-12-26 20:34:47 -08001657
Salyzyn, Mark29c97682007-06-12 09:33:54 -04001658 if (jafo) {
1659 spin_lock_irq(host->host_lock);
1660 }
1661 return retval;
1662}
1663
Raghava Aditya Renukunta31364322017-02-02 15:53:33 -08001664int aac_reset_adapter(struct aac_dev *aac, int forced, u8 reset_type)
Salyzyn, Mark29c97682007-06-12 09:33:54 -04001665{
1666 unsigned long flagv = 0;
1667 int retval;
1668 struct Scsi_Host * host;
Raghava Aditya Renukunta31364322017-02-02 15:53:33 -08001669 int bled;
Salyzyn, Mark29c97682007-06-12 09:33:54 -04001670
1671 if (spin_trylock_irqsave(&aac->fib_lock, flagv) == 0)
1672 return -EBUSY;
1673
1674 if (aac->in_reset) {
1675 spin_unlock_irqrestore(&aac->fib_lock, flagv);
1676 return -EBUSY;
1677 }
1678 aac->in_reset = 1;
1679 spin_unlock_irqrestore(&aac->fib_lock, flagv);
1680
1681 /*
1682 * Wait for all commands to complete to this specific
1683 * target (block maximum 60 seconds). Although not necessary,
1684 * it does make us a good storage citizen.
1685 */
1686 host = aac->scsi_host_ptr;
1687 scsi_block_requests(host);
Salyzyn, Mark29c97682007-06-12 09:33:54 -04001688
1689 /* Quiesce build, flush cache, write through mode */
Salyzyn, Markf8583172007-10-30 15:50:49 -04001690 if (forced < 2)
1691 aac_send_shutdown(aac);
Salyzyn, Mark29c97682007-06-12 09:33:54 -04001692 spin_lock_irqsave(host->host_lock, flagv);
Raghava Aditya Renukunta31364322017-02-02 15:53:33 -08001693 bled = forced ? forced :
1694 (aac_check_reset != 0 && aac_check_reset != 1);
1695 retval = _aac_reset_adapter(aac, bled, reset_type);
Salyzyn, Mark29c97682007-06-12 09:33:54 -04001696 spin_unlock_irqrestore(host->host_lock, flagv);
1697
Salyzyn, Markf8583172007-10-30 15:50:49 -04001698 if ((forced < 2) && (retval == -ENODEV)) {
Salyzyn, Mark29c97682007-06-12 09:33:54 -04001699 /* Unwind aac_send_shutdown() IOP_RESET unsupported/disabled */
1700 struct fib * fibctx = aac_fib_alloc(aac);
1701 if (fibctx) {
1702 struct aac_pause *cmd;
1703 int status;
1704
1705 aac_fib_init(fibctx);
1706
1707 cmd = (struct aac_pause *) fib_data(fibctx);
1708
1709 cmd->command = cpu_to_le32(VM_ContainerConfig);
1710 cmd->type = cpu_to_le32(CT_PAUSE_IO);
1711 cmd->timeout = cpu_to_le32(1);
1712 cmd->min = cpu_to_le32(1);
1713 cmd->noRescan = cpu_to_le32(1);
1714 cmd->count = cpu_to_le32(0);
1715
1716 status = aac_fib_send(ContainerCommand,
1717 fibctx,
1718 sizeof(struct aac_pause),
1719 FsaNormal,
1720 -2 /* Timeout silently */, 1,
1721 NULL, NULL);
1722
1723 if (status >= 0)
1724 aac_fib_complete(fibctx);
Penchala Narasimha Reddy Chilakala, ERS-HCLTechcacb6dc2009-12-21 18:39:27 +05301725 /* FIB should be freed only after getting
1726 * the response from the F/W */
1727 if (status != -ERESTARTSYS)
1728 aac_fib_free(fibctx);
Salyzyn, Mark29c97682007-06-12 09:33:54 -04001729 }
1730 }
1731
Mark Haverkamp8c867b22006-08-03 08:03:30 -07001732 return retval;
1733}
1734
1735int aac_check_health(struct aac_dev * aac)
1736{
1737 int BlinkLED;
1738 unsigned long time_now, flagv = 0;
1739 struct list_head * entry;
Mark Haverkamp8c867b22006-08-03 08:03:30 -07001740
1741 /* Extending the scope of fib_lock slightly to protect aac->in_reset */
1742 if (spin_trylock_irqsave(&aac->fib_lock, flagv) == 0)
1743 return 0;
1744
1745 if (aac->in_reset || !(BlinkLED = aac_adapter_check_health(aac))) {
1746 spin_unlock_irqrestore(&aac->fib_lock, flagv);
1747 return 0; /* OK */
1748 }
1749
1750 aac->in_reset = 1;
1751
1752 /* Fake up an AIF:
1753 * aac_aifcmd.command = AifCmdEventNotify = 1
1754 * aac_aifcmd.seqnum = 0xFFFFFFFF
1755 * aac_aifcmd.data[0] = AifEnExpEvent = 23
1756 * aac_aifcmd.data[1] = AifExeFirmwarePanic = 3
1757 * aac.aifcmd.data[2] = AifHighPriority = 3
1758 * aac.aifcmd.data[3] = BlinkLED
1759 */
1760
1761 time_now = jiffies/HZ;
1762 entry = aac->fib_list.next;
1763
1764 /*
1765 * For each Context that is on the
1766 * fibctxList, make a copy of the
1767 * fib, and then set the event to wake up the
1768 * thread that is waiting for it.
1769 */
1770 while (entry != &aac->fib_list) {
1771 /*
1772 * Extract the fibctx
1773 */
1774 struct aac_fib_context *fibctx = list_entry(entry, struct aac_fib_context, next);
1775 struct hw_fib * hw_fib;
1776 struct fib * fib;
1777 /*
1778 * Check if the queue is getting
1779 * backlogged
1780 */
1781 if (fibctx->count > 20) {
1782 /*
1783 * It's *not* jiffies folks,
1784 * but jiffies / HZ, so do not
1785 * panic ...
1786 */
1787 u32 time_last = fibctx->jiffies;
1788 /*
1789 * Has it been > 2 minutes
1790 * since the last read off
1791 * the queue?
1792 */
1793 if ((time_now - time_last) > aif_timeout) {
1794 entry = entry->next;
1795 aac_close_fib_context(aac, fibctx);
1796 continue;
1797 }
1798 }
1799 /*
1800 * Warning: no sleep allowed while
1801 * holding spinlock
1802 */
Salyzyn, Mark4dbc22d2007-04-16 11:21:50 -04001803 hw_fib = kzalloc(sizeof(struct hw_fib), GFP_ATOMIC);
1804 fib = kzalloc(sizeof(struct fib), GFP_ATOMIC);
Mark Haverkamp8c867b22006-08-03 08:03:30 -07001805 if (fib && hw_fib) {
1806 struct aac_aifcmd * aif;
1807
Mark Haverkampa8166a52007-03-15 10:26:22 -07001808 fib->hw_fib_va = hw_fib;
Mark Haverkamp8c867b22006-08-03 08:03:30 -07001809 fib->dev = aac;
1810 aac_fib_init(fib);
1811 fib->type = FSAFS_NTC_FIB_CONTEXT;
1812 fib->size = sizeof (struct fib);
1813 fib->data = hw_fib->data;
1814 aif = (struct aac_aifcmd *)hw_fib->data;
1815 aif->command = cpu_to_le32(AifCmdEventNotify);
Salyzyn, Marka3940da2008-01-08 12:48:25 -08001816 aif->seqnum = cpu_to_le32(0xFFFFFFFF);
1817 ((__le32 *)aif->data)[0] = cpu_to_le32(AifEnExpEvent);
1818 ((__le32 *)aif->data)[1] = cpu_to_le32(AifExeFirmwarePanic);
1819 ((__le32 *)aif->data)[2] = cpu_to_le32(AifHighPriority);
1820 ((__le32 *)aif->data)[3] = cpu_to_le32(BlinkLED);
Mark Haverkamp8c867b22006-08-03 08:03:30 -07001821
1822 /*
1823 * Put the FIB onto the
1824 * fibctx's fibs
1825 */
1826 list_add_tail(&fib->fiblink, &fibctx->fib_list);
1827 fibctx->count++;
1828 /*
1829 * Set the event to wake up the
1830 * thread that will waiting.
1831 */
Arnd Bergmanndc71ecc2018-12-10 22:32:40 +01001832 complete(&fibctx->completion);
Mark Haverkamp8c867b22006-08-03 08:03:30 -07001833 } else {
1834 printk(KERN_WARNING "aifd: didn't allocate NewFib.\n");
1835 kfree(fib);
1836 kfree(hw_fib);
1837 }
1838 entry = entry->next;
1839 }
1840
1841 spin_unlock_irqrestore(&aac->fib_lock, flagv);
1842
1843 if (BlinkLED < 0) {
Guilherme G. Piccoli911e5722017-04-06 18:12:09 -03001844 printk(KERN_ERR "%s: Host adapter is dead (or got a PCI error) %d\n",
1845 aac->name, BlinkLED);
Mark Haverkamp8c867b22006-08-03 08:03:30 -07001846 goto out;
1847 }
1848
1849 printk(KERN_ERR "%s: Host adapter BLINK LED 0x%x\n", aac->name, BlinkLED);
1850
Mark Haverkamp8c867b22006-08-03 08:03:30 -07001851out:
1852 aac->in_reset = 0;
1853 return BlinkLED;
1854}
1855
Raghava Aditya Renukuntaf2d2cab2017-12-26 20:34:40 -08001856static inline int is_safw_raid_volume(struct aac_dev *aac, int bus, int target)
Raghava Aditya Renukunta6223a392017-02-02 15:53:28 -08001857{
Raghava Aditya Renukuntaf2d2cab2017-12-26 20:34:40 -08001858 return bus == CONTAINER_CHANNEL && target < aac->maximum_num_containers;
1859}
1860
Raghava Aditya Renukunta3031c652017-12-26 20:34:42 -08001861static struct scsi_device *aac_lookup_safw_scsi_device(struct aac_dev *dev,
1862 int bus,
1863 int target)
1864{
1865 if (bus != CONTAINER_CHANNEL)
1866 bus = aac_phys_to_logical(bus);
1867
1868 return scsi_device_lookup(dev->scsi_host_ptr, bus, target, 0);
1869}
1870
1871static int aac_add_safw_device(struct aac_dev *dev, int bus, int target)
1872{
1873 if (bus != CONTAINER_CHANNEL)
1874 bus = aac_phys_to_logical(bus);
1875
1876 return scsi_add_device(dev->scsi_host_ptr, bus, target, 0);
1877}
1878
1879static void aac_put_safw_scsi_device(struct scsi_device *sdev)
1880{
1881 if (sdev)
1882 scsi_device_put(sdev);
1883}
1884
1885static void aac_remove_safw_device(struct aac_dev *dev, int bus, int target)
1886{
Raghava Aditya Renukunta6223a392017-02-02 15:53:28 -08001887 struct scsi_device *sdev;
Raghava Aditya Renukunta6223a392017-02-02 15:53:28 -08001888
Raghava Aditya Renukunta3031c652017-12-26 20:34:42 -08001889 sdev = aac_lookup_safw_scsi_device(dev, bus, target);
1890 scsi_remove_device(sdev);
1891 aac_put_safw_scsi_device(sdev);
1892}
Raghava Aditya Renukunta6223a392017-02-02 15:53:28 -08001893
Raghava Aditya Renukuntaf2d2cab2017-12-26 20:34:40 -08001894static inline int aac_is_safw_scan_count_equal(struct aac_dev *dev,
1895 int bus, int target)
1896{
1897 return dev->hba_map[bus][target].scan_counter == dev->scan_counter;
1898}
Raghava Aditya Renukunta6223a392017-02-02 15:53:28 -08001899
Raghava Aditya Renukuntaf2d2cab2017-12-26 20:34:40 -08001900static int aac_is_safw_target_valid(struct aac_dev *dev, int bus, int target)
1901{
1902 if (is_safw_raid_volume(dev, bus, target))
1903 return dev->fsa_dev[target].valid;
1904 else
1905 return aac_is_safw_scan_count_equal(dev, bus, target);
1906}
Raghava Aditya Renukunta6223a392017-02-02 15:53:28 -08001907
Raghava Aditya Renukunta3031c652017-12-26 20:34:42 -08001908static int aac_is_safw_device_exposed(struct aac_dev *dev, int bus, int target)
1909{
1910 int is_exposed = 0;
1911 struct scsi_device *sdev;
Raghava Aditya Renukunta6223a392017-02-02 15:53:28 -08001912
Raghava Aditya Renukunta3031c652017-12-26 20:34:42 -08001913 sdev = aac_lookup_safw_scsi_device(dev, bus, target);
1914 if (sdev)
1915 is_exposed = 1;
1916 aac_put_safw_scsi_device(sdev);
Raghava Aditya Renukunta6223a392017-02-02 15:53:28 -08001917
Raghava Aditya Renukunta3031c652017-12-26 20:34:42 -08001918 return is_exposed;
1919}
Raghava Aditya Renukunta6223a392017-02-02 15:53:28 -08001920
Raghava Aditya Renukunta75be67c2017-12-26 20:34:49 -08001921static int aac_update_safw_host_devices(struct aac_dev *dev)
Raghava Aditya Renukunta6223a392017-02-02 15:53:28 -08001922{
Raghava Aditya Renukunta22906782017-12-26 20:34:41 -08001923 int i;
Raghava Aditya Renukunta6f44a222017-12-26 20:34:43 -08001924 int bus;
1925 int target;
Raghava Aditya Renukunta3031c652017-12-26 20:34:42 -08001926 int is_exposed = 0;
Raghava Aditya Renukunta6f44a222017-12-26 20:34:43 -08001927 int rcode = 0;
1928
Raghava Aditya Renukunta75be67c2017-12-26 20:34:49 -08001929 rcode = aac_setup_safw_adapter(dev);
Raghava Aditya Renukunta6f44a222017-12-26 20:34:43 -08001930 if (unlikely(rcode < 0)) {
1931 goto out;
Raghava Aditya Renukunta6223a392017-02-02 15:53:28 -08001932 }
Raghava Aditya Renukunta6223a392017-02-02 15:53:28 -08001933
Raghava Aditya Renukunta22906782017-12-26 20:34:41 -08001934 for (i = 0; i < AAC_BUS_TARGET_LOOP; i++) {
Raghava Aditya Renukunta6223a392017-02-02 15:53:28 -08001935
Raghava Aditya Renukunta22906782017-12-26 20:34:41 -08001936 bus = get_bus_number(i);
1937 target = get_target_number(i);
Raghava Aditya Renukunta6223a392017-02-02 15:53:28 -08001938
Raghava Aditya Renukunta3031c652017-12-26 20:34:42 -08001939 is_exposed = aac_is_safw_device_exposed(dev, bus, target);
Raghava Aditya Renukunta22906782017-12-26 20:34:41 -08001940
Raghava Aditya Renukunta3031c652017-12-26 20:34:42 -08001941 if (aac_is_safw_target_valid(dev, bus, target) && !is_exposed)
1942 aac_add_safw_device(dev, bus, target);
1943 else if (!aac_is_safw_target_valid(dev, bus, target) &&
1944 is_exposed)
1945 aac_remove_safw_device(dev, bus, target);
Raghava Aditya Renukunta6223a392017-02-02 15:53:28 -08001946 }
Raghava Aditya Renukunta6f44a222017-12-26 20:34:43 -08001947out:
1948 return rcode;
Raghava Aditya Renukunta6223a392017-02-02 15:53:28 -08001949}
1950
Raghava Aditya Renukunta75be67c2017-12-26 20:34:49 -08001951static int aac_scan_safw_host(struct aac_dev *dev)
Raghava Aditya Renukuntaa1367e42017-12-26 20:34:46 -08001952{
1953 int rcode = 0;
1954
Raghava Aditya Renukunta75be67c2017-12-26 20:34:49 -08001955 rcode = aac_update_safw_host_devices(dev);
Raghava Aditya Renukuntaa1367e42017-12-26 20:34:46 -08001956 if (rcode)
1957 aac_schedule_safw_scan_worker(dev);
1958
1959 return rcode;
1960}
1961
Raghava Aditya Renukunta75be67c2017-12-26 20:34:49 -08001962int aac_scan_host(struct aac_dev *dev)
Raghava Aditya Renukunta8ebaa672017-12-26 20:34:45 -08001963{
1964 int rcode = 0;
1965
1966 mutex_lock(&dev->scan_mutex);
1967 if (dev->sa_firmware)
Raghava Aditya Renukunta75be67c2017-12-26 20:34:49 -08001968 rcode = aac_scan_safw_host(dev);
Raghava Aditya Renukunta8ebaa672017-12-26 20:34:45 -08001969 else
1970 scsi_scan_host(dev->scsi_host_ptr);
1971 mutex_unlock(&dev->scan_mutex);
Raghava Aditya Renukuntaa1367e42017-12-26 20:34:46 -08001972
Raghava Aditya Renukunta8ebaa672017-12-26 20:34:45 -08001973 return rcode;
Raghava Aditya Renukunta6223a392017-02-02 15:53:28 -08001974}
1975
1976/**
1977 * aac_handle_sa_aif Handle a message from the firmware
1978 * @dev: Which adapter this fib is from
1979 * @fibptr: Pointer to fibptr from adapter
1980 *
1981 * This routine handles a driver notify fib from the adapter and
1982 * dispatches it to the appropriate routine for handling.
1983 */
1984static void aac_handle_sa_aif(struct aac_dev *dev, struct fib *fibptr)
1985{
Raghava Aditya Renukuntaf2d2cab2017-12-26 20:34:40 -08001986 int i;
Raghava Aditya Renukunta6223a392017-02-02 15:53:28 -08001987 u32 events = 0;
Raghava Aditya Renukunta6223a392017-02-02 15:53:28 -08001988
1989 if (fibptr->hbacmd_size & SA_AIF_HOTPLUG)
1990 events = SA_AIF_HOTPLUG;
1991 else if (fibptr->hbacmd_size & SA_AIF_HARDWARE)
1992 events = SA_AIF_HARDWARE;
1993 else if (fibptr->hbacmd_size & SA_AIF_PDEV_CHANGE)
1994 events = SA_AIF_PDEV_CHANGE;
1995 else if (fibptr->hbacmd_size & SA_AIF_LDEV_CHANGE)
1996 events = SA_AIF_LDEV_CHANGE;
1997 else if (fibptr->hbacmd_size & SA_AIF_BPSTAT_CHANGE)
1998 events = SA_AIF_BPSTAT_CHANGE;
1999 else if (fibptr->hbacmd_size & SA_AIF_BPCFG_CHANGE)
2000 events = SA_AIF_BPCFG_CHANGE;
2001
2002 switch (events) {
2003 case SA_AIF_HOTPLUG:
2004 case SA_AIF_HARDWARE:
2005 case SA_AIF_PDEV_CHANGE:
2006 case SA_AIF_LDEV_CHANGE:
2007 case SA_AIF_BPCFG_CHANGE:
2008
Raghava Aditya Renukunta75be67c2017-12-26 20:34:49 -08002009 aac_scan_host(dev);
Raghava Aditya Renukunta6223a392017-02-02 15:53:28 -08002010
Raghava Aditya Renukunta6223a392017-02-02 15:53:28 -08002011 break;
2012
2013 case SA_AIF_BPSTAT_CHANGE:
2014 /* currently do nothing */
2015 break;
2016 }
2017
2018 for (i = 1; i <= 10; ++i) {
2019 events = src_readl(dev, MUnit.IDR);
2020 if (events & (1<<23)) {
2021 pr_warn(" AIF not cleared by firmware - %d/%d)\n",
2022 i, 10);
2023 ssleep(1);
2024 }
2025 }
2026}
2027
Raghava Aditya Renukunta113156b2017-02-02 15:53:24 -08002028static int get_fib_count(struct aac_dev *dev)
2029{
2030 unsigned int num = 0;
2031 struct list_head *entry;
2032 unsigned long flagv;
2033
2034 /*
2035 * Warning: no sleep allowed while
2036 * holding spinlock. We take the estimate
2037 * and pre-allocate a set of fibs outside the
2038 * lock.
2039 */
2040 num = le32_to_cpu(dev->init->r7.adapter_fibs_size)
2041 / sizeof(struct hw_fib); /* some extra */
2042 spin_lock_irqsave(&dev->fib_lock, flagv);
2043 entry = dev->fib_list.next;
2044 while (entry != &dev->fib_list) {
2045 entry = entry->next;
2046 ++num;
2047 }
2048 spin_unlock_irqrestore(&dev->fib_lock, flagv);
2049
2050 return num;
2051}
2052
2053static int fillup_pools(struct aac_dev *dev, struct hw_fib **hw_fib_pool,
2054 struct fib **fib_pool,
2055 unsigned int num)
2056{
2057 struct hw_fib **hw_fib_p;
2058 struct fib **fib_p;
Raghava Aditya Renukunta113156b2017-02-02 15:53:24 -08002059
2060 hw_fib_p = hw_fib_pool;
2061 fib_p = fib_pool;
2062 while (hw_fib_p < &hw_fib_pool[num]) {
2063 *(hw_fib_p) = kmalloc(sizeof(struct hw_fib), GFP_KERNEL);
2064 if (!(*(hw_fib_p++))) {
2065 --hw_fib_p;
2066 break;
2067 }
2068
2069 *(fib_p) = kmalloc(sizeof(struct fib), GFP_KERNEL);
2070 if (!(*(fib_p++))) {
2071 kfree(*(--hw_fib_p));
2072 break;
2073 }
2074 }
2075
Raghava Aditya Renukuntae4985202017-03-14 09:20:19 -07002076 /*
2077 * Get the actual number of allocated fibs
2078 */
Raghava Aditya Renukunta113156b2017-02-02 15:53:24 -08002079 num = hw_fib_p - hw_fib_pool;
Raghava Aditya Renukuntae4985202017-03-14 09:20:19 -07002080 return num;
Raghava Aditya Renukunta113156b2017-02-02 15:53:24 -08002081}
2082
2083static void wakeup_fibctx_threads(struct aac_dev *dev,
2084 struct hw_fib **hw_fib_pool,
2085 struct fib **fib_pool,
2086 struct fib *fib,
2087 struct hw_fib *hw_fib,
2088 unsigned int num)
2089{
2090 unsigned long flagv;
2091 struct list_head *entry;
2092 struct hw_fib **hw_fib_p;
2093 struct fib **fib_p;
2094 u32 time_now, time_last;
2095 struct hw_fib *hw_newfib;
2096 struct fib *newfib;
2097 struct aac_fib_context *fibctx;
2098
2099 time_now = jiffies/HZ;
2100 spin_lock_irqsave(&dev->fib_lock, flagv);
2101 entry = dev->fib_list.next;
2102 /*
2103 * For each Context that is on the
2104 * fibctxList, make a copy of the
2105 * fib, and then set the event to wake up the
2106 * thread that is waiting for it.
2107 */
2108
2109 hw_fib_p = hw_fib_pool;
2110 fib_p = fib_pool;
2111 while (entry != &dev->fib_list) {
2112 /*
2113 * Extract the fibctx
2114 */
2115 fibctx = list_entry(entry, struct aac_fib_context,
2116 next);
2117 /*
2118 * Check if the queue is getting
2119 * backlogged
2120 */
2121 if (fibctx->count > 20) {
2122 /*
2123 * It's *not* jiffies folks,
2124 * but jiffies / HZ so do not
2125 * panic ...
2126 */
2127 time_last = fibctx->jiffies;
2128 /*
2129 * Has it been > 2 minutes
2130 * since the last read off
2131 * the queue?
2132 */
2133 if ((time_now - time_last) > aif_timeout) {
2134 entry = entry->next;
2135 aac_close_fib_context(dev, fibctx);
2136 continue;
2137 }
2138 }
2139 /*
2140 * Warning: no sleep allowed while
2141 * holding spinlock
2142 */
2143 if (hw_fib_p >= &hw_fib_pool[num]) {
2144 pr_warn("aifd: didn't allocate NewFib\n");
2145 entry = entry->next;
2146 continue;
2147 }
2148
2149 hw_newfib = *hw_fib_p;
2150 *(hw_fib_p++) = NULL;
2151 newfib = *fib_p;
2152 *(fib_p++) = NULL;
2153 /*
2154 * Make the copy of the FIB
2155 */
2156 memcpy(hw_newfib, hw_fib, sizeof(struct hw_fib));
2157 memcpy(newfib, fib, sizeof(struct fib));
2158 newfib->hw_fib_va = hw_newfib;
2159 /*
2160 * Put the FIB onto the
2161 * fibctx's fibs
2162 */
2163 list_add_tail(&newfib->fiblink, &fibctx->fib_list);
2164 fibctx->count++;
2165 /*
2166 * Set the event to wake up the
2167 * thread that is waiting.
2168 */
Arnd Bergmanndc71ecc2018-12-10 22:32:40 +01002169 complete(&fibctx->completion);
Raghava Aditya Renukunta113156b2017-02-02 15:53:24 -08002170
2171 entry = entry->next;
2172 }
2173 /*
2174 * Set the status of this FIB
2175 */
2176 *(__le32 *)hw_fib->data = cpu_to_le32(ST_OK);
2177 aac_fib_adapter_complete(fib, sizeof(u32));
2178 spin_unlock_irqrestore(&dev->fib_lock, flagv);
2179
2180}
2181
2182static void aac_process_events(struct aac_dev *dev)
2183{
2184 struct hw_fib *hw_fib;
2185 struct fib *fib;
2186 unsigned long flags;
2187 spinlock_t *t_lock;
Raghava Aditya Renukunta113156b2017-02-02 15:53:24 -08002188
2189 t_lock = dev->queues->queue[HostNormCmdQueue].lock;
2190 spin_lock_irqsave(t_lock, flags);
2191
2192 while (!list_empty(&(dev->queues->queue[HostNormCmdQueue].cmdq))) {
2193 struct list_head *entry;
2194 struct aac_aifcmd *aifcmd;
2195 unsigned int num;
2196 struct hw_fib **hw_fib_pool, **hw_fib_p;
2197 struct fib **fib_pool, **fib_p;
2198
2199 set_current_state(TASK_RUNNING);
2200
2201 entry = dev->queues->queue[HostNormCmdQueue].cmdq.next;
2202 list_del(entry);
2203
2204 t_lock = dev->queues->queue[HostNormCmdQueue].lock;
2205 spin_unlock_irqrestore(t_lock, flags);
2206
2207 fib = list_entry(entry, struct fib, fiblink);
2208 hw_fib = fib->hw_fib_va;
Raghava Aditya Renukunta6223a392017-02-02 15:53:28 -08002209 if (dev->sa_firmware) {
2210 /* Thor AIF */
2211 aac_handle_sa_aif(dev, fib);
2212 aac_fib_adapter_complete(fib, (u16)sizeof(u32));
Raghava Aditya Renukuntad8447522017-02-16 12:51:23 -08002213 goto free_fib;
Raghava Aditya Renukunta6223a392017-02-02 15:53:28 -08002214 }
Raghava Aditya Renukunta113156b2017-02-02 15:53:24 -08002215 /*
2216 * We will process the FIB here or pass it to a
2217 * worker thread that is TBD. We Really can't
2218 * do anything at this point since we don't have
2219 * anything defined for this thread to do.
2220 */
2221 memset(fib, 0, sizeof(struct fib));
2222 fib->type = FSAFS_NTC_FIB_CONTEXT;
2223 fib->size = sizeof(struct fib);
2224 fib->hw_fib_va = hw_fib;
2225 fib->data = hw_fib->data;
2226 fib->dev = dev;
2227 /*
2228 * We only handle AifRequest fibs from the adapter.
2229 */
2230
2231 aifcmd = (struct aac_aifcmd *) hw_fib->data;
2232 if (aifcmd->command == cpu_to_le32(AifCmdDriverNotify)) {
2233 /* Handle Driver Notify Events */
2234 aac_handle_aif(dev, fib);
2235 *(__le32 *)hw_fib->data = cpu_to_le32(ST_OK);
2236 aac_fib_adapter_complete(fib, (u16)sizeof(u32));
2237 goto free_fib;
2238 }
2239 /*
2240 * The u32 here is important and intended. We are using
2241 * 32bit wrapping time to fit the adapter field
2242 */
2243
2244 /* Sniff events */
2245 if (aifcmd->command == cpu_to_le32(AifCmdEventNotify)
2246 || aifcmd->command == cpu_to_le32(AifCmdJobProgress)) {
2247 aac_handle_aif(dev, fib);
2248 }
2249
2250 /*
2251 * get number of fibs to process
2252 */
2253 num = get_fib_count(dev);
2254 if (!num)
2255 goto free_fib;
2256
2257 hw_fib_pool = kmalloc_array(num, sizeof(struct hw_fib *),
2258 GFP_KERNEL);
2259 if (!hw_fib_pool)
2260 goto free_fib;
2261
2262 fib_pool = kmalloc_array(num, sizeof(struct fib *), GFP_KERNEL);
2263 if (!fib_pool)
2264 goto free_hw_fib_pool;
2265
2266 /*
2267 * Fill up fib pointer pools with actual fibs
2268 * and hw_fibs
2269 */
Raghava Aditya Renukuntae4985202017-03-14 09:20:19 -07002270 num = fillup_pools(dev, hw_fib_pool, fib_pool, num);
2271 if (!num)
Raghava Aditya Renukunta113156b2017-02-02 15:53:24 -08002272 goto free_mem;
2273
2274 /*
2275 * wakeup the thread that is waiting for
2276 * the response from fw (ioctl)
2277 */
2278 wakeup_fibctx_threads(dev, hw_fib_pool, fib_pool,
2279 fib, hw_fib, num);
2280
2281free_mem:
2282 /* Free up the remaining resources */
2283 hw_fib_p = hw_fib_pool;
2284 fib_p = fib_pool;
2285 while (hw_fib_p < &hw_fib_pool[num]) {
2286 kfree(*hw_fib_p);
2287 kfree(*fib_p);
2288 ++fib_p;
2289 ++hw_fib_p;
2290 }
2291 kfree(fib_pool);
2292free_hw_fib_pool:
2293 kfree(hw_fib_pool);
2294free_fib:
2295 kfree(fib);
2296 t_lock = dev->queues->queue[HostNormCmdQueue].lock;
2297 spin_lock_irqsave(t_lock, flags);
2298 }
2299 /*
2300 * There are no more AIF's
2301 */
2302 t_lock = dev->queues->queue[HostNormCmdQueue].lock;
2303 spin_unlock_irqrestore(t_lock, flags);
2304}
Mark Haverkamp8c867b22006-08-03 08:03:30 -07002305
Raghava Aditya Renukunta3d77d842017-02-02 15:53:25 -08002306static int aac_send_wellness_command(struct aac_dev *dev, char *wellness_str,
2307 u32 datasize)
2308{
2309 struct aac_srb *srbcmd;
2310 struct sgmap64 *sg64;
2311 dma_addr_t addr;
2312 char *dma_buf;
2313 struct fib *fibptr;
2314 int ret = -ENOMEM;
2315 u32 vbus, vid;
2316
2317 fibptr = aac_fib_alloc(dev);
2318 if (!fibptr)
2319 goto out;
2320
Mahesh Rajashekharaf4819732017-04-05 16:14:16 +05302321 dma_buf = dma_alloc_coherent(&dev->pdev->dev, datasize, &addr,
2322 GFP_KERNEL);
Raghava Aditya Renukunta3d77d842017-02-02 15:53:25 -08002323 if (!dma_buf)
2324 goto fib_free_out;
2325
2326 aac_fib_init(fibptr);
2327
Raghava Aditya Renukunta1c688562017-02-16 12:51:10 -08002328 vbus = (u32)le16_to_cpu(dev->supplement_adapter_info.virt_device_bus);
2329 vid = (u32)le16_to_cpu(dev->supplement_adapter_info.virt_device_target);
Raghava Aditya Renukunta3d77d842017-02-02 15:53:25 -08002330
2331 srbcmd = (struct aac_srb *)fib_data(fibptr);
2332
2333 srbcmd->function = cpu_to_le32(SRBF_ExecuteScsi);
2334 srbcmd->channel = cpu_to_le32(vbus);
2335 srbcmd->id = cpu_to_le32(vid);
2336 srbcmd->lun = 0;
2337 srbcmd->flags = cpu_to_le32(SRB_DataOut);
2338 srbcmd->timeout = cpu_to_le32(10);
2339 srbcmd->retry_limit = 0;
2340 srbcmd->cdb_size = cpu_to_le32(12);
2341 srbcmd->count = cpu_to_le32(datasize);
2342
2343 memset(srbcmd->cdb, 0, sizeof(srbcmd->cdb));
2344 srbcmd->cdb[0] = BMIC_OUT;
2345 srbcmd->cdb[6] = WRITE_HOST_WELLNESS;
2346 memcpy(dma_buf, (char *)wellness_str, datasize);
2347
2348 sg64 = (struct sgmap64 *)&srbcmd->sg;
2349 sg64->count = cpu_to_le32(1);
2350 sg64->sg[0].addr[1] = cpu_to_le32((u32)(((addr) >> 16) >> 16));
2351 sg64->sg[0].addr[0] = cpu_to_le32((u32)(addr & 0xffffffff));
2352 sg64->sg[0].count = cpu_to_le32(datasize);
2353
2354 ret = aac_fib_send(ScsiPortCommand64, fibptr, sizeof(struct aac_srb),
2355 FsaNormal, 1, 1, NULL, NULL);
2356
Mahesh Rajashekharaf4819732017-04-05 16:14:16 +05302357 dma_free_coherent(&dev->pdev->dev, datasize, dma_buf, addr);
Raghava Aditya Renukunta3d77d842017-02-02 15:53:25 -08002358
2359 /*
2360 * Do not set XferState to zero unless
2361 * receives a response from F/W
2362 */
2363 if (ret >= 0)
2364 aac_fib_complete(fibptr);
2365
2366 /*
2367 * FIB should be freed only after
2368 * getting the response from the F/W
2369 */
2370 if (ret != -ERESTARTSYS)
2371 goto fib_free_out;
2372
2373out:
2374 return ret;
2375fib_free_out:
2376 aac_fib_free(fibptr);
2377 goto out;
2378}
2379
Arnd Bergmann820f1882017-11-07 11:46:05 +01002380int aac_send_safw_hostttime(struct aac_dev *dev, struct timespec64 *now)
Raghava Aditya Renukunta3d77d842017-02-02 15:53:25 -08002381{
2382 struct tm cur_tm;
2383 char wellness_str[] = "<HW>TD\010\0\0\0\0\0\0\0\0\0DW\0\0ZZ";
2384 u32 datasize = sizeof(wellness_str);
Arnd Bergmann820f1882017-11-07 11:46:05 +01002385 time64_t local_time;
Raghava Aditya Renukunta3d77d842017-02-02 15:53:25 -08002386 int ret = -ENODEV;
2387
2388 if (!dev->sa_firmware)
2389 goto out;
2390
Arnd Bergmann820f1882017-11-07 11:46:05 +01002391 local_time = (now->tv_sec - (sys_tz.tz_minuteswest * 60));
2392 time64_to_tm(local_time, 0, &cur_tm);
Raghava Aditya Renukunta3d77d842017-02-02 15:53:25 -08002393 cur_tm.tm_mon += 1;
2394 cur_tm.tm_year += 1900;
2395 wellness_str[8] = bin2bcd(cur_tm.tm_hour);
2396 wellness_str[9] = bin2bcd(cur_tm.tm_min);
2397 wellness_str[10] = bin2bcd(cur_tm.tm_sec);
2398 wellness_str[12] = bin2bcd(cur_tm.tm_mon);
2399 wellness_str[13] = bin2bcd(cur_tm.tm_mday);
2400 wellness_str[14] = bin2bcd(cur_tm.tm_year / 100);
2401 wellness_str[15] = bin2bcd(cur_tm.tm_year % 100);
2402
2403 ret = aac_send_wellness_command(dev, wellness_str, datasize);
2404
2405out:
2406 return ret;
2407}
2408
Arnd Bergmann820f1882017-11-07 11:46:05 +01002409int aac_send_hosttime(struct aac_dev *dev, struct timespec64 *now)
Raghava Aditya Renukunta3d77d842017-02-02 15:53:25 -08002410{
2411 int ret = -ENOMEM;
2412 struct fib *fibptr;
2413 __le32 *info;
2414
2415 fibptr = aac_fib_alloc(dev);
2416 if (!fibptr)
2417 goto out;
2418
2419 aac_fib_init(fibptr);
2420 info = (__le32 *)fib_data(fibptr);
Arnd Bergmann820f1882017-11-07 11:46:05 +01002421 *info = cpu_to_le32(now->tv_sec); /* overflow in y2106 */
Raghava Aditya Renukunta3d77d842017-02-02 15:53:25 -08002422 ret = aac_fib_send(SendHostTime, fibptr, sizeof(*info), FsaNormal,
2423 1, 1, NULL, NULL);
2424
2425 /*
2426 * Do not set XferState to zero unless
2427 * receives a response from F/W
2428 */
2429 if (ret >= 0)
2430 aac_fib_complete(fibptr);
2431
2432 /*
2433 * FIB should be freed only after
2434 * getting the response from the F/W
2435 */
2436 if (ret != -ERESTARTSYS)
2437 aac_fib_free(fibptr);
2438
2439out:
2440 return ret;
2441}
2442
Linus Torvalds1da177e2005-04-16 15:20:36 -07002443/**
2444 * aac_command_thread - command processing thread
2445 * @dev: Adapter to monitor
2446 *
2447 * Waits on the commandready event in it's queue. When the event gets set
2448 * it will pull FIBs off it's queue. It will continue to pull FIBs off
2449 * until the queue is empty. When the queue is empty it will wait for
2450 * more FIBs.
2451 */
Salyzyn, Mark8ce3eca2008-01-16 07:39:06 -08002452
Christoph Hellwigfe273812006-02-14 18:45:06 +01002453int aac_command_thread(void *data)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002454{
Christoph Hellwigfe273812006-02-14 18:45:06 +01002455 struct aac_dev *dev = data;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002456 DECLARE_WAITQUEUE(wait, current);
Salyzyn, Mark29c97682007-06-12 09:33:54 -04002457 unsigned long next_jiffies = jiffies + HZ;
2458 unsigned long next_check_jiffies = next_jiffies;
2459 long difference = HZ;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002460
2461 /*
2462 * We can only have one thread per adapter for AIF's.
2463 */
2464 if (dev->aif_thread)
2465 return -EINVAL;
Christoph Hellwigfe273812006-02-14 18:45:06 +01002466
Linus Torvalds1da177e2005-04-16 15:20:36 -07002467 /*
2468 * Let the DPC know it has a place to send the AIF's to.
2469 */
2470 dev->aif_thread = 1;
Mark Haverkamp2f1309802005-09-26 13:02:15 -07002471 add_wait_queue(&dev->queues->queue[HostNormCmdQueue].cmdready, &wait);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002472 set_current_state(TASK_INTERRUPTIBLE);
Mark Haverkamp2f1309802005-09-26 13:02:15 -07002473 dprintk ((KERN_INFO "aac_command_thread start\n"));
Salyzyn, Mark8ce3eca2008-01-16 07:39:06 -08002474 while (1) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002475
Raghava Aditya Renukunta113156b2017-02-02 15:53:24 -08002476 aac_process_events(dev);
Salyzyn, Mark29c97682007-06-12 09:33:54 -04002477
2478 /*
2479 * Background activity
2480 */
2481 if ((time_before(next_check_jiffies,next_jiffies))
2482 && ((difference = next_check_jiffies - jiffies) <= 0)) {
2483 next_check_jiffies = next_jiffies;
Raghava Aditya Renukunta9473ddb2017-05-10 09:39:48 -07002484 if (aac_adapter_check_health(dev) == 0) {
Salyzyn, Mark29c97682007-06-12 09:33:54 -04002485 difference = ((long)(unsigned)check_interval)
2486 * HZ;
2487 next_check_jiffies = jiffies + difference;
2488 } else if (!dev->queues)
2489 break;
2490 }
2491 if (!time_before(next_check_jiffies,next_jiffies)
2492 && ((difference = next_jiffies - jiffies) <= 0)) {
Arnd Bergmann820f1882017-11-07 11:46:05 +01002493 struct timespec64 now;
Salyzyn, Mark29c97682007-06-12 09:33:54 -04002494 int ret;
2495
2496 /* Don't even try to talk to adapter if its sick */
Raghava Aditya Renukunta9473ddb2017-05-10 09:39:48 -07002497 ret = aac_adapter_check_health(dev);
Raghava Aditya Renukunta849ac6a2017-02-16 12:51:17 -08002498 if (ret || !dev->queues)
Salyzyn, Mark29c97682007-06-12 09:33:54 -04002499 break;
2500 next_check_jiffies = jiffies
2501 + ((long)(unsigned)check_interval)
2502 * HZ;
Arnd Bergmann820f1882017-11-07 11:46:05 +01002503 ktime_get_real_ts64(&now);
Salyzyn, Mark29c97682007-06-12 09:33:54 -04002504
2505 /* Synchronize our watches */
Arnd Bergmann820f1882017-11-07 11:46:05 +01002506 if (((NSEC_PER_SEC - (NSEC_PER_SEC / HZ)) > now.tv_nsec)
2507 && (now.tv_nsec > (NSEC_PER_SEC / HZ)))
Arnd Bergmannd1853972017-11-28 14:25:25 +01002508 difference = HZ + HZ / 2 -
2509 now.tv_nsec / (NSEC_PER_SEC / HZ);
Colin Ian Kingfbdab3e2017-02-24 14:43:30 +00002510 else {
Arnd Bergmann820f1882017-11-07 11:46:05 +01002511 if (now.tv_nsec > NSEC_PER_SEC / 2)
Raghava Aditya Renukunta3d77d842017-02-02 15:53:25 -08002512 ++now.tv_sec;
Salyzyn, Mark29c97682007-06-12 09:33:54 -04002513
Raghava Aditya Renukunta3d77d842017-02-02 15:53:25 -08002514 if (dev->sa_firmware)
2515 ret =
2516 aac_send_safw_hostttime(dev, &now);
2517 else
2518 ret = aac_send_hosttime(dev, &now);
Salyzyn, Mark29c97682007-06-12 09:33:54 -04002519
Salyzyn, Mark29c97682007-06-12 09:33:54 -04002520 difference = (long)(unsigned)update_interval*HZ;
Salyzyn, Mark29c97682007-06-12 09:33:54 -04002521 }
2522 next_jiffies = jiffies + difference;
2523 if (time_before(next_check_jiffies,next_jiffies))
2524 difference = next_check_jiffies - jiffies;
2525 }
2526 if (difference <= 0)
2527 difference = 1;
2528 set_current_state(TASK_INTERRUPTIBLE);
Raghava Aditya Renukuntafc4bf752016-04-25 23:31:57 -07002529
2530 if (kthread_should_stop())
2531 break;
2532
Arnd Bergmannd1853972017-11-28 14:25:25 +01002533 /*
2534 * we probably want usleep_range() here instead of the
2535 * jiffies computation
2536 */
Salyzyn, Mark29c97682007-06-12 09:33:54 -04002537 schedule_timeout(difference);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002538
Christoph Hellwigfe273812006-02-14 18:45:06 +01002539 if (kthread_should_stop())
Linus Torvalds1da177e2005-04-16 15:20:36 -07002540 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002541 }
Mark Haverkamp2f1309802005-09-26 13:02:15 -07002542 if (dev->queues)
2543 remove_wait_queue(&dev->queues->queue[HostNormCmdQueue].cmdready, &wait);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002544 dev->aif_thread = 0;
Mark Haverkamp2f1309802005-09-26 13:02:15 -07002545 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002546}
Mahesh Rajashekhara8b1462e2015-08-28 06:38:38 -04002547
2548int aac_acquire_irq(struct aac_dev *dev)
2549{
2550 int i;
2551 int j;
2552 int ret = 0;
Mahesh Rajashekhara8b1462e2015-08-28 06:38:38 -04002553
Mahesh Rajashekhara8b1462e2015-08-28 06:38:38 -04002554 if (!dev->sync_mode && dev->msi_enabled && dev->max_msix > 1) {
2555 for (i = 0; i < dev->max_msix; i++) {
2556 dev->aac_msix[i].vector_no = i;
2557 dev->aac_msix[i].dev = dev;
Hannes Reinecke0910d8b2016-11-08 08:11:30 +01002558 if (request_irq(pci_irq_vector(dev->pdev, i),
Mahesh Rajashekhara8b1462e2015-08-28 06:38:38 -04002559 dev->a_ops.adapter_intr,
2560 0, "aacraid", &(dev->aac_msix[i]))) {
2561 printk(KERN_ERR "%s%d: Failed to register IRQ for vector %d.\n",
2562 dev->name, dev->id, i);
2563 for (j = 0 ; j < i ; j++)
Hannes Reinecke0910d8b2016-11-08 08:11:30 +01002564 free_irq(pci_irq_vector(dev->pdev, j),
Mahesh Rajashekhara8b1462e2015-08-28 06:38:38 -04002565 &(dev->aac_msix[j]));
2566 pci_disable_msix(dev->pdev);
2567 ret = -1;
2568 }
Mahesh Rajashekhara8b1462e2015-08-28 06:38:38 -04002569 }
2570 } else {
2571 dev->aac_msix[0].vector_no = 0;
2572 dev->aac_msix[0].dev = dev;
2573
2574 if (request_irq(dev->pdev->irq, dev->a_ops.adapter_intr,
2575 IRQF_SHARED, "aacraid",
2576 &(dev->aac_msix[0])) < 0) {
2577 if (dev->msi)
2578 pci_disable_msi(dev->pdev);
2579 printk(KERN_ERR "%s%d: Interrupt unavailable.\n",
2580 dev->name, dev->id);
2581 ret = -1;
2582 }
2583 }
2584 return ret;
2585}
2586
2587void aac_free_irq(struct aac_dev *dev)
2588{
2589 int i;
Mahesh Rajashekhara8b1462e2015-08-28 06:38:38 -04002590
Raghava Aditya Renukunta395e5df2017-05-10 09:39:52 -07002591 if (aac_is_src(dev)) {
Mahesh Rajashekhara8b1462e2015-08-28 06:38:38 -04002592 if (dev->max_msix > 1) {
Hannes Reinecke0910d8b2016-11-08 08:11:30 +01002593 for (i = 0; i < dev->max_msix; i++)
2594 free_irq(pci_irq_vector(dev->pdev, i),
2595 &(dev->aac_msix[i]));
Mahesh Rajashekhara8b1462e2015-08-28 06:38:38 -04002596 } else {
2597 free_irq(dev->pdev->irq, &(dev->aac_msix[0]));
2598 }
2599 } else {
2600 free_irq(dev->pdev->irq, dev);
2601 }
2602 if (dev->msi)
2603 pci_disable_msi(dev->pdev);
2604 else if (dev->max_msix > 1)
2605 pci_disable_msix(dev->pdev);
2606}