blob: fea3cb6a090be6d8ccae70eabfaa75dbe9c9a4a2 [file] [log] [blame]
Sreekanth Reddyf92363d2012-11-30 07:44:21 +05301/*
2 * This is the Fusion MPT base driver providing common API layer interface
3 * for access to MPT (Message Passing Technology) firmware.
4 *
5 * This code is based on drivers/scsi/mpt3sas/mpt3sas_base.c
Sreekanth Reddya4ffce02014-09-12 15:35:29 +05306 * Copyright (C) 2012-2014 LSI Corporation
Sreekanth Reddya03bd152015-01-12 11:39:02 +05307 * Copyright (C) 2013-2014 Avago Technologies
8 * (mailto: MPT-FusionLinux.pdl@avagotech.com)
Sreekanth Reddyf92363d2012-11-30 07:44:21 +05309 *
10 * This program is free software; you can redistribute it and/or
11 * modify it under the terms of the GNU General Public License
12 * as published by the Free Software Foundation; either version 2
13 * of the License, or (at your option) any later version.
14 *
15 * This program is distributed in the hope that it will be useful,
16 * but WITHOUT ANY WARRANTY; without even the implied warranty of
17 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
18 * GNU General Public License for more details.
19 *
20 * NO WARRANTY
21 * THE PROGRAM IS PROVIDED ON AN "AS IS" BASIS, WITHOUT WARRANTIES OR
22 * CONDITIONS OF ANY KIND, EITHER EXPRESS OR IMPLIED INCLUDING, WITHOUT
23 * LIMITATION, ANY WARRANTIES OR CONDITIONS OF TITLE, NON-INFRINGEMENT,
24 * MERCHANTABILITY OR FITNESS FOR A PARTICULAR PURPOSE. Each Recipient is
25 * solely responsible for determining the appropriateness of using and
26 * distributing the Program and assumes all risks associated with its
27 * exercise of rights under this Agreement, including but not limited to
28 * the risks and costs of program errors, damage to or loss of data,
29 * programs or equipment, and unavailability or interruption of operations.
30
31 * DISCLAIMER OF LIABILITY
32 * NEITHER RECIPIENT NOR ANY CONTRIBUTORS SHALL HAVE ANY LIABILITY FOR ANY
33 * DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
34 * DAMAGES (INCLUDING WITHOUT LIMITATION LOST PROFITS), HOWEVER CAUSED AND
35 * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR
36 * TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE
37 * USE OR DISTRIBUTION OF THE PROGRAM OR THE EXERCISE OF ANY RIGHTS GRANTED
38 * HEREUNDER, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGES
39
40 * You should have received a copy of the GNU General Public License
41 * along with this program; if not, write to the Free Software
42 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301,
43 * USA.
44 */
45
Sreekanth Reddyf92363d2012-11-30 07:44:21 +053046#include <linux/kernel.h>
47#include <linux/module.h>
48#include <linux/errno.h>
49#include <linux/init.h>
50#include <linux/slab.h>
51#include <linux/types.h>
52#include <linux/pci.h>
53#include <linux/kdev_t.h>
54#include <linux/blkdev.h>
55#include <linux/delay.h>
56#include <linux/interrupt.h>
57#include <linux/dma-mapping.h>
58#include <linux/io.h>
59#include <linux/time.h>
Tina Ruchandani23409bd2016-04-13 00:01:40 -070060#include <linux/ktime.h>
Sreekanth Reddyf92363d2012-11-30 07:44:21 +053061#include <linux/kthread.h>
Suganath Prabu Subramani016d5c32017-10-31 18:02:28 +053062#include <asm/page.h> /* To get host page size per arch */
Sreekanth Reddyf92363d2012-11-30 07:44:21 +053063#include <linux/aer.h>
64
65
66#include "mpt3sas_base.h"
67
68static MPT_CALLBACK mpt_callbacks[MPT_MAX_CALLBACKS];
69
70
71#define FAULT_POLLING_INTERVAL 1000 /* in milliseconds */
72
73 /* maximum controller queue depth */
74#define MAX_HBA_QUEUE_DEPTH 30000
75#define MAX_CHAIN_DEPTH 100000
76static int max_queue_depth = -1;
Tomas Henzlab9f5ad2019-06-14 16:41:43 +020077module_param(max_queue_depth, int, 0444);
Sreekanth Reddyf92363d2012-11-30 07:44:21 +053078MODULE_PARM_DESC(max_queue_depth, " max controller queue depth ");
79
80static int max_sgl_entries = -1;
Tomas Henzlab9f5ad2019-06-14 16:41:43 +020081module_param(max_sgl_entries, int, 0444);
Sreekanth Reddyf92363d2012-11-30 07:44:21 +053082MODULE_PARM_DESC(max_sgl_entries, " max sg entries ");
83
84static int msix_disable = -1;
Tomas Henzlab9f5ad2019-06-14 16:41:43 +020085module_param(msix_disable, int, 0444);
Sreekanth Reddyf92363d2012-11-30 07:44:21 +053086MODULE_PARM_DESC(msix_disable, " disable msix routed interrupts (default=0)");
87
Suganath Prabu Subramani64038302016-02-08 22:13:39 +053088static int smp_affinity_enable = 1;
Tomas Henzlab9f5ad2019-06-14 16:41:43 +020089module_param(smp_affinity_enable, int, 0444);
Colin Ian King23b389c2018-04-29 13:31:49 +010090MODULE_PARM_DESC(smp_affinity_enable, "SMP affinity feature enable/disable Default: enable(1)");
Suganath Prabu Subramani64038302016-02-08 22:13:39 +053091
Sreekanth Reddyfb77bb52015-06-30 12:24:47 +053092static int max_msix_vectors = -1;
Tomas Henzlab9f5ad2019-06-14 16:41:43 +020093module_param(max_msix_vectors, int, 0444);
Sreekanth Reddy9c500062013-08-14 18:23:20 +053094MODULE_PARM_DESC(max_msix_vectors,
Sreekanth Reddyfb77bb52015-06-30 12:24:47 +053095 " max msix vectors");
Sreekanth Reddyf92363d2012-11-30 07:44:21 +053096
Suganath Prabu288addd2019-02-15 02:40:29 -050097static int irqpoll_weight = -1;
Tomas Henzlab9f5ad2019-06-14 16:41:43 +020098module_param(irqpoll_weight, int, 0444);
Suganath Prabu288addd2019-02-15 02:40:29 -050099MODULE_PARM_DESC(irqpoll_weight,
100 "irq poll weight (default= one fourth of HBA queue depth)");
101
Sreekanth Reddyf92363d2012-11-30 07:44:21 +0530102static int mpt3sas_fwfault_debug;
103MODULE_PARM_DESC(mpt3sas_fwfault_debug,
104 " enable detection of firmware fault and halt firmware - (default=0)");
105
Suganath Prabu Sca7e1e92019-05-31 08:14:42 -0400106static int perf_mode = -1;
Tomas Henzlab9f5ad2019-06-14 16:41:43 +0200107module_param(perf_mode, int, 0444);
Suganath Prabu Sca7e1e92019-05-31 08:14:42 -0400108MODULE_PARM_DESC(perf_mode,
109 "Performance mode (only for Aero/Sea Generation), options:\n\t\t"
110 "0 - balanced: high iops mode is enabled &\n\t\t"
111 "interrupt coalescing is enabled only on high iops queues,\n\t\t"
112 "1 - iops: high iops mode is disabled &\n\t\t"
113 "interrupt coalescing is enabled on all queues,\n\t\t"
114 "2 - latency: high iops mode is disabled &\n\t\t"
115 "interrupt coalescing is enabled on all queues with timeout value 0xA,\n"
Sreekanth Reddy381abbd2019-06-24 10:42:53 -0400116 "\t\tdefault - default perf_mode is 'balanced'"
Suganath Prabu Sca7e1e92019-05-31 08:14:42 -0400117 );
118
119enum mpt3sas_perf_mode {
120 MPT_PERF_MODE_DEFAULT = -1,
121 MPT_PERF_MODE_BALANCED = 0,
122 MPT_PERF_MODE_IOPS = 1,
123 MPT_PERF_MODE_LATENCY = 2,
124};
125
Sreekanth Reddy9b05c912014-09-12 15:35:31 +0530126static int
Calvin Owens98c56ad2016-07-28 21:38:21 -0700127_base_get_ioc_facts(struct MPT3SAS_ADAPTER *ioc);
Sreekanth Reddyf92363d2012-11-30 07:44:21 +0530128
129/**
Chaitra P Bd37306c2018-05-31 06:34:50 -0400130 * mpt3sas_base_check_cmd_timeout - Function
131 * to check timeout and command termination due
132 * to Host reset.
Sreekanth Reddyf92363d2012-11-30 07:44:21 +0530133 *
Chaitra P Bd37306c2018-05-31 06:34:50 -0400134 * @ioc: per adapter object.
135 * @status: Status of issued command.
136 * @mpi_request:mf request pointer.
137 * @sz: size of buffer.
138 *
139 * @Returns - 1/0 Reset to be done or Not
140 */
141u8
142mpt3sas_base_check_cmd_timeout(struct MPT3SAS_ADAPTER *ioc,
143 u8 status, void *mpi_request, int sz)
144{
145 u8 issue_reset = 0;
146
147 if (!(status & MPT3_CMD_RESET))
148 issue_reset = 1;
149
Joe Perches919d8a32018-09-17 08:01:09 -0700150 ioc_err(ioc, "Command %s\n",
151 issue_reset == 0 ? "terminated due to Host Reset" : "Timeout");
Chaitra P Bd37306c2018-05-31 06:34:50 -0400152 _debug_dump_mf(mpi_request, sz);
153
154 return issue_reset;
155}
156
157/**
Sreekanth Reddyf92363d2012-11-30 07:44:21 +0530158 * _scsih_set_fwfault_debug - global setting of ioc->fwfault_debug.
Bart Van Assche4beb4862018-06-15 14:42:01 -0700159 * @val: ?
160 * @kp: ?
Sreekanth Reddyf92363d2012-11-30 07:44:21 +0530161 *
Bart Van Assche4beb4862018-06-15 14:42:01 -0700162 * Return: ?
Sreekanth Reddyf92363d2012-11-30 07:44:21 +0530163 */
164static int
Kees Cooke4dca7b2017-10-17 19:04:42 -0700165_scsih_set_fwfault_debug(const char *val, const struct kernel_param *kp)
Sreekanth Reddyf92363d2012-11-30 07:44:21 +0530166{
167 int ret = param_set_int(val, kp);
168 struct MPT3SAS_ADAPTER *ioc;
169
170 if (ret)
171 return ret;
172
Sreekanth Reddy08c4d552015-11-11 17:30:33 +0530173 /* global ioc spinlock to protect controller list on list operations */
Sreekanth Reddyf92363d2012-11-30 07:44:21 +0530174 pr_info("setting fwfault_debug(%d)\n", mpt3sas_fwfault_debug);
Sreekanth Reddy08c4d552015-11-11 17:30:33 +0530175 spin_lock(&gioc_lock);
Sreekanth Reddyf92363d2012-11-30 07:44:21 +0530176 list_for_each_entry(ioc, &mpt3sas_ioc_list, list)
177 ioc->fwfault_debug = mpt3sas_fwfault_debug;
Sreekanth Reddy08c4d552015-11-11 17:30:33 +0530178 spin_unlock(&gioc_lock);
Sreekanth Reddyf92363d2012-11-30 07:44:21 +0530179 return 0;
180}
181module_param_call(mpt3sas_fwfault_debug, _scsih_set_fwfault_debug,
182 param_get_int, &mpt3sas_fwfault_debug, 0644);
183
184/**
Suganath Prabub8992022018-12-07 12:58:33 +0530185 * _base_readl_aero - retry readl for max three times.
186 * @addr - MPT Fusion system interface register address
187 *
188 * Retry the readl() for max three times if it gets zero value
189 * while reading the system interface register.
190 */
191static inline u32
192_base_readl_aero(const volatile void __iomem *addr)
193{
194 u32 i = 0, ret_val;
195
196 do {
197 ret_val = readl(addr);
198 i++;
199 } while (ret_val == 0 && i < 3);
200
201 return ret_val;
202}
203
204static inline u32
205_base_readl(const volatile void __iomem *addr)
206{
207 return readl(addr);
208}
209
210/**
Suganath Prabu Subramanib4472d72018-02-07 02:51:50 -0800211 * _base_clone_reply_to_sys_mem - copies reply to reply free iomem
212 * in BAR0 space.
213 *
214 * @ioc: per adapter object
215 * @reply: reply message frame(lower 32bit addr)
216 * @index: System request message index.
Suganath Prabu Subramanib4472d72018-02-07 02:51:50 -0800217 */
218static void
219_base_clone_reply_to_sys_mem(struct MPT3SAS_ADAPTER *ioc, u32 reply,
220 u32 index)
221{
222 /*
223 * 256 is offset within sys register.
224 * 256 offset MPI frame starts. Max MPI frame supported is 32.
225 * 32 * 128 = 4K. From here, Clone of reply free for mcpu starts
226 */
227 u16 cmd_credit = ioc->facts.RequestCredit + 1;
228 void __iomem *reply_free_iomem = (void __iomem *)ioc->chip +
229 MPI_FRAME_START_OFFSET +
230 (cmd_credit * ioc->request_sz) + (index * sizeof(u32));
231
232 writel(reply, reply_free_iomem);
233}
234
235/**
Suganath Prabu Subramanie5747432018-02-07 02:51:49 -0800236 * _base_clone_mpi_to_sys_mem - Writes/copies MPI frames
237 * to system/BAR0 region.
238 *
Bart Van Assche4beb4862018-06-15 14:42:01 -0700239 * @dst_iomem: Pointer to the destination location in BAR0 space.
Suganath Prabu Subramanie5747432018-02-07 02:51:49 -0800240 * @src: Pointer to the Source data.
241 * @size: Size of data to be copied.
242 */
243static void
244_base_clone_mpi_to_sys_mem(void *dst_iomem, void *src, u32 size)
245{
246 int i;
247 u32 *src_virt_mem = (u32 *)src;
248
249 for (i = 0; i < size/4; i++)
250 writel((u32)src_virt_mem[i],
251 (void __iomem *)dst_iomem + (i * 4));
252}
253
254/**
Suganath Prabu Subramani182ac782018-02-07 02:51:48 -0800255 * _base_clone_to_sys_mem - Writes/copies data to system/BAR0 region
256 *
257 * @dst_iomem: Pointer to the destination location in BAR0 space.
258 * @src: Pointer to the Source data.
259 * @size: Size of data to be copied.
260 */
261static void
262_base_clone_to_sys_mem(void __iomem *dst_iomem, void *src, u32 size)
263{
264 int i;
265 u32 *src_virt_mem = (u32 *)(src);
266
267 for (i = 0; i < size/4; i++)
268 writel((u32)src_virt_mem[i],
269 (void __iomem *)dst_iomem + (i * 4));
270}
271
272/**
Suganath Prabu Subramani22ae5a32018-02-07 02:51:47 -0800273 * _base_get_chain - Calculates and Returns virtual chain address
274 * for the provided smid in BAR0 space.
275 *
276 * @ioc: per adapter object
277 * @smid: system request message index
278 * @sge_chain_count: Scatter gather chain count.
279 *
Bart Van Assche4beb4862018-06-15 14:42:01 -0700280 * Return: the chain address.
Suganath Prabu Subramani22ae5a32018-02-07 02:51:47 -0800281 */
282static inline void __iomem*
283_base_get_chain(struct MPT3SAS_ADAPTER *ioc, u16 smid,
284 u8 sge_chain_count)
285{
286 void __iomem *base_chain, *chain_virt;
287 u16 cmd_credit = ioc->facts.RequestCredit + 1;
288
289 base_chain = (void __iomem *)ioc->chip + MPI_FRAME_START_OFFSET +
290 (cmd_credit * ioc->request_sz) +
291 REPLY_FREE_POOL_SIZE;
292 chain_virt = base_chain + (smid * ioc->facts.MaxChainDepth *
293 ioc->request_sz) + (sge_chain_count * ioc->request_sz);
294 return chain_virt;
295}
296
297/**
298 * _base_get_chain_phys - Calculates and Returns physical address
299 * in BAR0 for scatter gather chains, for
300 * the provided smid.
301 *
302 * @ioc: per adapter object
303 * @smid: system request message index
304 * @sge_chain_count: Scatter gather chain count.
305 *
Bart Van Assche4beb4862018-06-15 14:42:01 -0700306 * Return: Physical chain address.
Suganath Prabu Subramani22ae5a32018-02-07 02:51:47 -0800307 */
Arnd Bergmann6f9e09f2018-03-01 14:07:07 +0100308static inline phys_addr_t
Suganath Prabu Subramani22ae5a32018-02-07 02:51:47 -0800309_base_get_chain_phys(struct MPT3SAS_ADAPTER *ioc, u16 smid,
310 u8 sge_chain_count)
311{
Arnd Bergmann6f9e09f2018-03-01 14:07:07 +0100312 phys_addr_t base_chain_phys, chain_phys;
Suganath Prabu Subramani22ae5a32018-02-07 02:51:47 -0800313 u16 cmd_credit = ioc->facts.RequestCredit + 1;
314
Arnd Bergmann6f9e09f2018-03-01 14:07:07 +0100315 base_chain_phys = ioc->chip_phys + MPI_FRAME_START_OFFSET +
Suganath Prabu Subramani22ae5a32018-02-07 02:51:47 -0800316 (cmd_credit * ioc->request_sz) +
317 REPLY_FREE_POOL_SIZE;
318 chain_phys = base_chain_phys + (smid * ioc->facts.MaxChainDepth *
319 ioc->request_sz) + (sge_chain_count * ioc->request_sz);
320 return chain_phys;
321}
322
323/**
324 * _base_get_buffer_bar0 - Calculates and Returns BAR0 mapped Host
325 * buffer address for the provided smid.
326 * (Each smid can have 64K starts from 17024)
327 *
328 * @ioc: per adapter object
329 * @smid: system request message index
330 *
Bart Van Assche4beb4862018-06-15 14:42:01 -0700331 * Return: Pointer to buffer location in BAR0.
Suganath Prabu Subramani22ae5a32018-02-07 02:51:47 -0800332 */
333
334static void __iomem *
335_base_get_buffer_bar0(struct MPT3SAS_ADAPTER *ioc, u16 smid)
336{
337 u16 cmd_credit = ioc->facts.RequestCredit + 1;
338 // Added extra 1 to reach end of chain.
339 void __iomem *chain_end = _base_get_chain(ioc,
340 cmd_credit + 1,
341 ioc->facts.MaxChainDepth);
342 return chain_end + (smid * 64 * 1024);
343}
344
345/**
346 * _base_get_buffer_phys_bar0 - Calculates and Returns BAR0 mapped
347 * Host buffer Physical address for the provided smid.
348 * (Each smid can have 64K starts from 17024)
349 *
350 * @ioc: per adapter object
351 * @smid: system request message index
352 *
Bart Van Assche4beb4862018-06-15 14:42:01 -0700353 * Return: Pointer to buffer location in BAR0.
Suganath Prabu Subramani22ae5a32018-02-07 02:51:47 -0800354 */
Arnd Bergmann6f9e09f2018-03-01 14:07:07 +0100355static phys_addr_t
Suganath Prabu Subramani22ae5a32018-02-07 02:51:47 -0800356_base_get_buffer_phys_bar0(struct MPT3SAS_ADAPTER *ioc, u16 smid)
357{
358 u16 cmd_credit = ioc->facts.RequestCredit + 1;
Arnd Bergmann6f9e09f2018-03-01 14:07:07 +0100359 phys_addr_t chain_end_phys = _base_get_chain_phys(ioc,
Suganath Prabu Subramani22ae5a32018-02-07 02:51:47 -0800360 cmd_credit + 1,
361 ioc->facts.MaxChainDepth);
362 return chain_end_phys + (smid * 64 * 1024);
363}
364
365/**
Suganath Prabu Subramani182ac782018-02-07 02:51:48 -0800366 * _base_get_chain_buffer_dma_to_chain_buffer - Iterates chain
367 * lookup list and Provides chain_buffer
368 * address for the matching dma address.
369 * (Each smid can have 64K starts from 17024)
370 *
371 * @ioc: per adapter object
372 * @chain_buffer_dma: Chain buffer dma address.
373 *
Bart Van Assche4beb4862018-06-15 14:42:01 -0700374 * Return: Pointer to chain buffer. Or Null on Failure.
Suganath Prabu Subramani182ac782018-02-07 02:51:48 -0800375 */
376static void *
377_base_get_chain_buffer_dma_to_chain_buffer(struct MPT3SAS_ADAPTER *ioc,
378 dma_addr_t chain_buffer_dma)
379{
Chaitra P B93204b72018-04-24 05:28:32 -0400380 u16 index, j;
381 struct chain_tracker *ct;
Suganath Prabu Subramani182ac782018-02-07 02:51:48 -0800382
Chaitra P B93204b72018-04-24 05:28:32 -0400383 for (index = 0; index < ioc->scsiio_depth; index++) {
384 for (j = 0; j < ioc->chains_needed_per_io; j++) {
385 ct = &ioc->chain_lookup[index].chains_per_smid[j];
386 if (ct && ct->chain_buffer_dma == chain_buffer_dma)
387 return ct->chain_buffer;
388 }
Suganath Prabu Subramani182ac782018-02-07 02:51:48 -0800389 }
Joe Perches919d8a32018-09-17 08:01:09 -0700390 ioc_info(ioc, "Provided chain_buffer_dma address is not in the lookup list\n");
Suganath Prabu Subramani182ac782018-02-07 02:51:48 -0800391 return NULL;
392}
393
394/**
395 * _clone_sg_entries - MPI EP's scsiio and config requests
396 * are handled here. Base function for
397 * double buffering, before submitting
398 * the requests.
399 *
400 * @ioc: per adapter object.
401 * @mpi_request: mf request pointer.
402 * @smid: system request message index.
Suganath Prabu Subramani182ac782018-02-07 02:51:48 -0800403 */
404static void _clone_sg_entries(struct MPT3SAS_ADAPTER *ioc,
405 void *mpi_request, u16 smid)
406{
407 Mpi2SGESimple32_t *sgel, *sgel_next;
408 u32 sgl_flags, sge_chain_count = 0;
409 bool is_write = 0;
410 u16 i = 0;
411 void __iomem *buffer_iomem;
Arnd Bergmann6f9e09f2018-03-01 14:07:07 +0100412 phys_addr_t buffer_iomem_phys;
Suganath Prabu Subramani182ac782018-02-07 02:51:48 -0800413 void __iomem *buff_ptr;
Arnd Bergmann6f9e09f2018-03-01 14:07:07 +0100414 phys_addr_t buff_ptr_phys;
Suganath Prabu Subramani182ac782018-02-07 02:51:48 -0800415 void __iomem *dst_chain_addr[MCPU_MAX_CHAINS_PER_IO];
Arnd Bergmann6f9e09f2018-03-01 14:07:07 +0100416 void *src_chain_addr[MCPU_MAX_CHAINS_PER_IO];
417 phys_addr_t dst_addr_phys;
Suganath Prabu Subramani182ac782018-02-07 02:51:48 -0800418 MPI2RequestHeader_t *request_hdr;
419 struct scsi_cmnd *scmd;
420 struct scatterlist *sg_scmd = NULL;
421 int is_scsiio_req = 0;
422
423 request_hdr = (MPI2RequestHeader_t *) mpi_request;
424
425 if (request_hdr->Function == MPI2_FUNCTION_SCSI_IO_REQUEST) {
426 Mpi25SCSIIORequest_t *scsiio_request =
427 (Mpi25SCSIIORequest_t *)mpi_request;
428 sgel = (Mpi2SGESimple32_t *) &scsiio_request->SGL;
429 is_scsiio_req = 1;
430 } else if (request_hdr->Function == MPI2_FUNCTION_CONFIG) {
431 Mpi2ConfigRequest_t *config_req =
432 (Mpi2ConfigRequest_t *)mpi_request;
433 sgel = (Mpi2SGESimple32_t *) &config_req->PageBufferSGE;
434 } else
435 return;
436
437 /* From smid we can get scsi_cmd, once we have sg_scmd,
438 * we just need to get sg_virt and sg_next to get virual
439 * address associated with sgel->Address.
440 */
441
442 if (is_scsiio_req) {
443 /* Get scsi_cmd using smid */
444 scmd = mpt3sas_scsih_scsi_lookup_get(ioc, smid);
445 if (scmd == NULL) {
Joe Perches919d8a32018-09-17 08:01:09 -0700446 ioc_err(ioc, "scmd is NULL\n");
Suganath Prabu Subramani182ac782018-02-07 02:51:48 -0800447 return;
448 }
449
450 /* Get sg_scmd from scmd provided */
451 sg_scmd = scsi_sglist(scmd);
452 }
453
454 /*
455 * 0 - 255 System register
456 * 256 - 4352 MPI Frame. (This is based on maxCredit 32)
457 * 4352 - 4864 Reply_free pool (512 byte is reserved
458 * considering maxCredit 32. Reply need extra
459 * room, for mCPU case kept four times of
460 * maxCredit).
461 * 4864 - 17152 SGE chain element. (32cmd * 3 chain of
462 * 128 byte size = 12288)
463 * 17152 - x Host buffer mapped with smid.
464 * (Each smid can have 64K Max IO.)
465 * BAR0+Last 1K MSIX Addr and Data
466 * Total size in use 2113664 bytes of 4MB BAR0
467 */
468
469 buffer_iomem = _base_get_buffer_bar0(ioc, smid);
470 buffer_iomem_phys = _base_get_buffer_phys_bar0(ioc, smid);
471
472 buff_ptr = buffer_iomem;
473 buff_ptr_phys = buffer_iomem_phys;
Arnd Bergmann6f9e09f2018-03-01 14:07:07 +0100474 WARN_ON(buff_ptr_phys > U32_MAX);
Suganath Prabu Subramani182ac782018-02-07 02:51:48 -0800475
Chaitra P Bcf6bf972018-04-24 05:28:30 -0400476 if (le32_to_cpu(sgel->FlagsLength) &
Suganath Prabu Subramani182ac782018-02-07 02:51:48 -0800477 (MPI2_SGE_FLAGS_HOST_TO_IOC << MPI2_SGE_FLAGS_SHIFT))
478 is_write = 1;
479
480 for (i = 0; i < MPT_MIN_PHYS_SEGMENTS + ioc->facts.MaxChainDepth; i++) {
481
Chaitra P Bcf6bf972018-04-24 05:28:30 -0400482 sgl_flags =
483 (le32_to_cpu(sgel->FlagsLength) >> MPI2_SGE_FLAGS_SHIFT);
Suganath Prabu Subramani182ac782018-02-07 02:51:48 -0800484
485 switch (sgl_flags & MPI2_SGE_FLAGS_ELEMENT_MASK) {
486 case MPI2_SGE_FLAGS_CHAIN_ELEMENT:
487 /*
488 * Helper function which on passing
489 * chain_buffer_dma returns chain_buffer. Get
490 * the virtual address for sgel->Address
491 */
492 sgel_next =
493 _base_get_chain_buffer_dma_to_chain_buffer(ioc,
Chaitra P Bcf6bf972018-04-24 05:28:30 -0400494 le32_to_cpu(sgel->Address));
Suganath Prabu Subramani182ac782018-02-07 02:51:48 -0800495 if (sgel_next == NULL)
496 return;
497 /*
498 * This is coping 128 byte chain
499 * frame (not a host buffer)
500 */
501 dst_chain_addr[sge_chain_count] =
502 _base_get_chain(ioc,
503 smid, sge_chain_count);
504 src_chain_addr[sge_chain_count] =
505 (void *) sgel_next;
Arnd Bergmann6f9e09f2018-03-01 14:07:07 +0100506 dst_addr_phys = _base_get_chain_phys(ioc,
Suganath Prabu Subramani182ac782018-02-07 02:51:48 -0800507 smid, sge_chain_count);
Arnd Bergmann6f9e09f2018-03-01 14:07:07 +0100508 WARN_ON(dst_addr_phys > U32_MAX);
Chaitra P Bcf6bf972018-04-24 05:28:30 -0400509 sgel->Address =
510 cpu_to_le32(lower_32_bits(dst_addr_phys));
Suganath Prabu Subramani182ac782018-02-07 02:51:48 -0800511 sgel = sgel_next;
512 sge_chain_count++;
513 break;
514 case MPI2_SGE_FLAGS_SIMPLE_ELEMENT:
515 if (is_write) {
516 if (is_scsiio_req) {
517 _base_clone_to_sys_mem(buff_ptr,
518 sg_virt(sg_scmd),
Chaitra P Bcf6bf972018-04-24 05:28:30 -0400519 (le32_to_cpu(sgel->FlagsLength) &
520 0x00ffffff));
Arnd Bergmann6f9e09f2018-03-01 14:07:07 +0100521 /*
522 * FIXME: this relies on a a zero
523 * PCI mem_offset.
524 */
Chaitra P Bcf6bf972018-04-24 05:28:30 -0400525 sgel->Address =
526 cpu_to_le32((u32)buff_ptr_phys);
Suganath Prabu Subramani182ac782018-02-07 02:51:48 -0800527 } else {
528 _base_clone_to_sys_mem(buff_ptr,
529 ioc->config_vaddr,
Chaitra P Bcf6bf972018-04-24 05:28:30 -0400530 (le32_to_cpu(sgel->FlagsLength) &
531 0x00ffffff));
532 sgel->Address =
533 cpu_to_le32((u32)buff_ptr_phys);
Suganath Prabu Subramani182ac782018-02-07 02:51:48 -0800534 }
535 }
Chaitra P Bcf6bf972018-04-24 05:28:30 -0400536 buff_ptr += (le32_to_cpu(sgel->FlagsLength) &
537 0x00ffffff);
538 buff_ptr_phys += (le32_to_cpu(sgel->FlagsLength) &
539 0x00ffffff);
540 if ((le32_to_cpu(sgel->FlagsLength) &
Suganath Prabu Subramani182ac782018-02-07 02:51:48 -0800541 (MPI2_SGE_FLAGS_END_OF_BUFFER
542 << MPI2_SGE_FLAGS_SHIFT)))
543 goto eob_clone_chain;
544 else {
545 /*
546 * Every single element in MPT will have
547 * associated sg_next. Better to sanity that
548 * sg_next is not NULL, but it will be a bug
549 * if it is null.
550 */
551 if (is_scsiio_req) {
552 sg_scmd = sg_next(sg_scmd);
553 if (sg_scmd)
554 sgel++;
555 else
556 goto eob_clone_chain;
557 }
558 }
559 break;
560 }
561 }
562
563eob_clone_chain:
564 for (i = 0; i < sge_chain_count; i++) {
565 if (is_scsiio_req)
566 _base_clone_to_sys_mem(dst_chain_addr[i],
567 src_chain_addr[i], ioc->request_sz);
568 }
569}
570
571/**
Sreekanth Reddyf92363d2012-11-30 07:44:21 +0530572 * mpt3sas_remove_dead_ioc_func - kthread context to remove dead ioc
573 * @arg: input argument, used to derive ioc
574 *
Bart Van Assche4beb4862018-06-15 14:42:01 -0700575 * Return:
576 * 0 if controller is removed from pci subsystem.
577 * -1 for other case.
Sreekanth Reddyf92363d2012-11-30 07:44:21 +0530578 */
579static int mpt3sas_remove_dead_ioc_func(void *arg)
580{
581 struct MPT3SAS_ADAPTER *ioc = (struct MPT3SAS_ADAPTER *)arg;
582 struct pci_dev *pdev;
583
Nathan Chancellorb51d577a2018-09-20 13:10:02 -0700584 if (!ioc)
Sreekanth Reddyf92363d2012-11-30 07:44:21 +0530585 return -1;
586
587 pdev = ioc->pdev;
Nathan Chancellorb51d577a2018-09-20 13:10:02 -0700588 if (!pdev)
Sreekanth Reddyf92363d2012-11-30 07:44:21 +0530589 return -1;
Rafael J. Wysocki64cdb412014-01-10 15:27:56 +0100590 pci_stop_and_remove_bus_device_locked(pdev);
Sreekanth Reddyf92363d2012-11-30 07:44:21 +0530591 return 0;
592}
593
594/**
595 * _base_fault_reset_work - workq handling ioc fault conditions
596 * @work: input argument, used to derive ioc
Sreekanth Reddyf92363d2012-11-30 07:44:21 +0530597 *
Bart Van Assche4beb4862018-06-15 14:42:01 -0700598 * Context: sleep.
Sreekanth Reddyf92363d2012-11-30 07:44:21 +0530599 */
600static void
601_base_fault_reset_work(struct work_struct *work)
602{
603 struct MPT3SAS_ADAPTER *ioc =
604 container_of(work, struct MPT3SAS_ADAPTER, fault_reset_work.work);
605 unsigned long flags;
606 u32 doorbell;
607 int rc;
608 struct task_struct *p;
609
610
611 spin_lock_irqsave(&ioc->ioc_reset_in_progress_lock, flags);
Sreekanth Reddy16e179b2015-11-11 17:30:27 +0530612 if (ioc->shost_recovery || ioc->pci_error_recovery)
Sreekanth Reddyf92363d2012-11-30 07:44:21 +0530613 goto rearm_timer;
614 spin_unlock_irqrestore(&ioc->ioc_reset_in_progress_lock, flags);
615
616 doorbell = mpt3sas_base_get_iocstate(ioc, 0);
617 if ((doorbell & MPI2_IOC_STATE_MASK) == MPI2_IOC_STATE_MASK) {
Joe Perches919d8a32018-09-17 08:01:09 -0700618 ioc_err(ioc, "SAS host is non-operational !!!!\n");
Sreekanth Reddyf92363d2012-11-30 07:44:21 +0530619
Sreekanth Reddy16e179b2015-11-11 17:30:27 +0530620 /* It may be possible that EEH recovery can resolve some of
621 * pci bus failure issues rather removing the dead ioc function
622 * by considering controller is in a non-operational state. So
623 * here priority is given to the EEH recovery. If it doesn't
624 * not resolve this issue, mpt3sas driver will consider this
625 * controller to non-operational state and remove the dead ioc
626 * function.
627 */
628 if (ioc->non_operational_loop++ < 5) {
629 spin_lock_irqsave(&ioc->ioc_reset_in_progress_lock,
630 flags);
631 goto rearm_timer;
632 }
633
Sreekanth Reddyf92363d2012-11-30 07:44:21 +0530634 /*
635 * Call _scsih_flush_pending_cmds callback so that we flush all
636 * pending commands back to OS. This call is required to aovid
637 * deadlock at block layer. Dead IOC will fail to do diag reset,
638 * and this call is safe since dead ioc will never return any
639 * command back from HW.
640 */
641 ioc->schedule_dead_ioc_flush_running_cmds(ioc);
642 /*
643 * Set remove_host flag early since kernel thread will
644 * take some time to execute.
645 */
646 ioc->remove_host = 1;
647 /*Remove the Dead Host */
648 p = kthread_run(mpt3sas_remove_dead_ioc_func, ioc,
Sreekanth Reddyc84b06a2015-11-11 17:30:35 +0530649 "%s_dead_ioc_%d", ioc->driver_name, ioc->id);
Sreekanth Reddyf92363d2012-11-30 07:44:21 +0530650 if (IS_ERR(p))
Joe Perches919d8a32018-09-17 08:01:09 -0700651 ioc_err(ioc, "%s: Running mpt3sas_dead_ioc thread failed !!!!\n",
652 __func__);
Sreekanth Reddyf92363d2012-11-30 07:44:21 +0530653 else
Joe Perches919d8a32018-09-17 08:01:09 -0700654 ioc_err(ioc, "%s: Running mpt3sas_dead_ioc thread success !!!!\n",
655 __func__);
Sreekanth Reddyf92363d2012-11-30 07:44:21 +0530656 return; /* don't rearm timer */
657 }
658
Sreekanth Reddy16e179b2015-11-11 17:30:27 +0530659 ioc->non_operational_loop = 0;
660
Sreekanth Reddyf92363d2012-11-30 07:44:21 +0530661 if ((doorbell & MPI2_IOC_STATE_MASK) != MPI2_IOC_STATE_OPERATIONAL) {
Calvin Owens98c56ad2016-07-28 21:38:21 -0700662 rc = mpt3sas_base_hard_reset_handler(ioc, FORCE_BIG_HAMMER);
Joe Perches919d8a32018-09-17 08:01:09 -0700663 ioc_warn(ioc, "%s: hard reset: %s\n",
664 __func__, rc == 0 ? "success" : "failed");
Sreekanth Reddyf92363d2012-11-30 07:44:21 +0530665 doorbell = mpt3sas_base_get_iocstate(ioc, 0);
666 if ((doorbell & MPI2_IOC_STATE_MASK) == MPI2_IOC_STATE_FAULT)
667 mpt3sas_base_fault_info(ioc, doorbell &
668 MPI2_DOORBELL_DATA_MASK);
669 if (rc && (doorbell & MPI2_IOC_STATE_MASK) !=
670 MPI2_IOC_STATE_OPERATIONAL)
671 return; /* don't rearm timer */
672 }
673
674 spin_lock_irqsave(&ioc->ioc_reset_in_progress_lock, flags);
675 rearm_timer:
676 if (ioc->fault_reset_work_q)
677 queue_delayed_work(ioc->fault_reset_work_q,
678 &ioc->fault_reset_work,
679 msecs_to_jiffies(FAULT_POLLING_INTERVAL));
680 spin_unlock_irqrestore(&ioc->ioc_reset_in_progress_lock, flags);
681}
682
683/**
684 * mpt3sas_base_start_watchdog - start the fault_reset_work_q
685 * @ioc: per adapter object
Sreekanth Reddyf92363d2012-11-30 07:44:21 +0530686 *
Bart Van Assche4beb4862018-06-15 14:42:01 -0700687 * Context: sleep.
Sreekanth Reddyf92363d2012-11-30 07:44:21 +0530688 */
689void
690mpt3sas_base_start_watchdog(struct MPT3SAS_ADAPTER *ioc)
691{
692 unsigned long flags;
693
694 if (ioc->fault_reset_work_q)
695 return;
696
697 /* initialize fault polling */
698
699 INIT_DELAYED_WORK(&ioc->fault_reset_work, _base_fault_reset_work);
700 snprintf(ioc->fault_reset_work_q_name,
Sreekanth Reddyc84b06a2015-11-11 17:30:35 +0530701 sizeof(ioc->fault_reset_work_q_name), "poll_%s%d_status",
702 ioc->driver_name, ioc->id);
Sreekanth Reddyf92363d2012-11-30 07:44:21 +0530703 ioc->fault_reset_work_q =
704 create_singlethread_workqueue(ioc->fault_reset_work_q_name);
705 if (!ioc->fault_reset_work_q) {
Joe Perches919d8a32018-09-17 08:01:09 -0700706 ioc_err(ioc, "%s: failed (line=%d)\n", __func__, __LINE__);
Bart Van Assche199fd792018-06-15 14:41:54 -0700707 return;
Sreekanth Reddyf92363d2012-11-30 07:44:21 +0530708 }
709 spin_lock_irqsave(&ioc->ioc_reset_in_progress_lock, flags);
710 if (ioc->fault_reset_work_q)
711 queue_delayed_work(ioc->fault_reset_work_q,
712 &ioc->fault_reset_work,
713 msecs_to_jiffies(FAULT_POLLING_INTERVAL));
714 spin_unlock_irqrestore(&ioc->ioc_reset_in_progress_lock, flags);
715}
716
717/**
718 * mpt3sas_base_stop_watchdog - stop the fault_reset_work_q
719 * @ioc: per adapter object
Sreekanth Reddyf92363d2012-11-30 07:44:21 +0530720 *
Bart Van Assche4beb4862018-06-15 14:42:01 -0700721 * Context: sleep.
Sreekanth Reddyf92363d2012-11-30 07:44:21 +0530722 */
723void
724mpt3sas_base_stop_watchdog(struct MPT3SAS_ADAPTER *ioc)
725{
726 unsigned long flags;
727 struct workqueue_struct *wq;
728
729 spin_lock_irqsave(&ioc->ioc_reset_in_progress_lock, flags);
730 wq = ioc->fault_reset_work_q;
731 ioc->fault_reset_work_q = NULL;
732 spin_unlock_irqrestore(&ioc->ioc_reset_in_progress_lock, flags);
733 if (wq) {
Reddy, Sreekanth4dc06fd2014-07-14 12:01:35 +0530734 if (!cancel_delayed_work_sync(&ioc->fault_reset_work))
Sreekanth Reddyf92363d2012-11-30 07:44:21 +0530735 flush_workqueue(wq);
736 destroy_workqueue(wq);
737 }
738}
739
740/**
741 * mpt3sas_base_fault_info - verbose translation of firmware FAULT code
742 * @ioc: per adapter object
743 * @fault_code: fault code
Sreekanth Reddyf92363d2012-11-30 07:44:21 +0530744 */
745void
746mpt3sas_base_fault_info(struct MPT3SAS_ADAPTER *ioc , u16 fault_code)
747{
Joe Perches919d8a32018-09-17 08:01:09 -0700748 ioc_err(ioc, "fault_state(0x%04x)!\n", fault_code);
Sreekanth Reddyf92363d2012-11-30 07:44:21 +0530749}
750
751/**
752 * mpt3sas_halt_firmware - halt's mpt controller firmware
753 * @ioc: per adapter object
754 *
755 * For debugging timeout related issues. Writing 0xCOFFEE00
756 * to the doorbell register will halt controller firmware. With
757 * the purpose to stop both driver and firmware, the enduser can
758 * obtain a ring buffer from controller UART.
759 */
760void
761mpt3sas_halt_firmware(struct MPT3SAS_ADAPTER *ioc)
762{
763 u32 doorbell;
764
765 if (!ioc->fwfault_debug)
766 return;
767
768 dump_stack();
769
Suganath Prabu306eaf22018-12-07 12:58:34 +0530770 doorbell = ioc->base_readl(&ioc->chip->Doorbell);
Sreekanth Reddyf92363d2012-11-30 07:44:21 +0530771 if ((doorbell & MPI2_IOC_STATE_MASK) == MPI2_IOC_STATE_FAULT)
772 mpt3sas_base_fault_info(ioc , doorbell);
773 else {
774 writel(0xC0FFEE00, &ioc->chip->Doorbell);
Joe Perches919d8a32018-09-17 08:01:09 -0700775 ioc_err(ioc, "Firmware is halted due to command timeout\n");
Sreekanth Reddyf92363d2012-11-30 07:44:21 +0530776 }
777
778 if (ioc->fwfault_debug == 2)
779 for (;;)
780 ;
781 else
782 panic("panic in %s\n", __func__);
783}
784
Sreekanth Reddyf92363d2012-11-30 07:44:21 +0530785/**
786 * _base_sas_ioc_info - verbose translation of the ioc status
787 * @ioc: per adapter object
788 * @mpi_reply: reply mf payload returned from firmware
789 * @request_hdr: request mf
Sreekanth Reddyf92363d2012-11-30 07:44:21 +0530790 */
791static void
792_base_sas_ioc_info(struct MPT3SAS_ADAPTER *ioc, MPI2DefaultReply_t *mpi_reply,
793 MPI2RequestHeader_t *request_hdr)
794{
795 u16 ioc_status = le16_to_cpu(mpi_reply->IOCStatus) &
796 MPI2_IOCSTATUS_MASK;
797 char *desc = NULL;
798 u16 frame_sz;
799 char *func_str = NULL;
800
801 /* SCSI_IO, RAID_PASS are handled from _scsih_scsi_ioc_info */
802 if (request_hdr->Function == MPI2_FUNCTION_SCSI_IO_REQUEST ||
803 request_hdr->Function == MPI2_FUNCTION_RAID_SCSI_IO_PASSTHROUGH ||
804 request_hdr->Function == MPI2_FUNCTION_EVENT_NOTIFICATION)
805 return;
806
807 if (ioc_status == MPI2_IOCSTATUS_CONFIG_INVALID_PAGE)
808 return;
809
810 switch (ioc_status) {
811
812/****************************************************************************
813* Common IOCStatus values for all replies
814****************************************************************************/
815
816 case MPI2_IOCSTATUS_INVALID_FUNCTION:
817 desc = "invalid function";
818 break;
819 case MPI2_IOCSTATUS_BUSY:
820 desc = "busy";
821 break;
822 case MPI2_IOCSTATUS_INVALID_SGL:
823 desc = "invalid sgl";
824 break;
825 case MPI2_IOCSTATUS_INTERNAL_ERROR:
826 desc = "internal error";
827 break;
828 case MPI2_IOCSTATUS_INVALID_VPID:
829 desc = "invalid vpid";
830 break;
831 case MPI2_IOCSTATUS_INSUFFICIENT_RESOURCES:
832 desc = "insufficient resources";
833 break;
Suganath prabu Subramanib130b0d2016-01-28 12:06:58 +0530834 case MPI2_IOCSTATUS_INSUFFICIENT_POWER:
835 desc = "insufficient power";
836 break;
Sreekanth Reddyf92363d2012-11-30 07:44:21 +0530837 case MPI2_IOCSTATUS_INVALID_FIELD:
838 desc = "invalid field";
839 break;
840 case MPI2_IOCSTATUS_INVALID_STATE:
841 desc = "invalid state";
842 break;
843 case MPI2_IOCSTATUS_OP_STATE_NOT_SUPPORTED:
844 desc = "op state not supported";
845 break;
846
847/****************************************************************************
848* Config IOCStatus values
849****************************************************************************/
850
851 case MPI2_IOCSTATUS_CONFIG_INVALID_ACTION:
852 desc = "config invalid action";
853 break;
854 case MPI2_IOCSTATUS_CONFIG_INVALID_TYPE:
855 desc = "config invalid type";
856 break;
857 case MPI2_IOCSTATUS_CONFIG_INVALID_PAGE:
858 desc = "config invalid page";
859 break;
860 case MPI2_IOCSTATUS_CONFIG_INVALID_DATA:
861 desc = "config invalid data";
862 break;
863 case MPI2_IOCSTATUS_CONFIG_NO_DEFAULTS:
864 desc = "config no defaults";
865 break;
866 case MPI2_IOCSTATUS_CONFIG_CANT_COMMIT:
867 desc = "config cant commit";
868 break;
869
870/****************************************************************************
871* SCSI IO Reply
872****************************************************************************/
873
874 case MPI2_IOCSTATUS_SCSI_RECOVERED_ERROR:
875 case MPI2_IOCSTATUS_SCSI_INVALID_DEVHANDLE:
876 case MPI2_IOCSTATUS_SCSI_DEVICE_NOT_THERE:
877 case MPI2_IOCSTATUS_SCSI_DATA_OVERRUN:
878 case MPI2_IOCSTATUS_SCSI_DATA_UNDERRUN:
879 case MPI2_IOCSTATUS_SCSI_IO_DATA_ERROR:
880 case MPI2_IOCSTATUS_SCSI_PROTOCOL_ERROR:
881 case MPI2_IOCSTATUS_SCSI_TASK_TERMINATED:
882 case MPI2_IOCSTATUS_SCSI_RESIDUAL_MISMATCH:
883 case MPI2_IOCSTATUS_SCSI_TASK_MGMT_FAILED:
884 case MPI2_IOCSTATUS_SCSI_IOC_TERMINATED:
885 case MPI2_IOCSTATUS_SCSI_EXT_TERMINATED:
886 break;
887
888/****************************************************************************
889* For use by SCSI Initiator and SCSI Target end-to-end data protection
890****************************************************************************/
891
892 case MPI2_IOCSTATUS_EEDP_GUARD_ERROR:
893 desc = "eedp guard error";
894 break;
895 case MPI2_IOCSTATUS_EEDP_REF_TAG_ERROR:
896 desc = "eedp ref tag error";
897 break;
898 case MPI2_IOCSTATUS_EEDP_APP_TAG_ERROR:
899 desc = "eedp app tag error";
900 break;
901
902/****************************************************************************
903* SCSI Target values
904****************************************************************************/
905
906 case MPI2_IOCSTATUS_TARGET_INVALID_IO_INDEX:
907 desc = "target invalid io index";
908 break;
909 case MPI2_IOCSTATUS_TARGET_ABORTED:
910 desc = "target aborted";
911 break;
912 case MPI2_IOCSTATUS_TARGET_NO_CONN_RETRYABLE:
913 desc = "target no conn retryable";
914 break;
915 case MPI2_IOCSTATUS_TARGET_NO_CONNECTION:
916 desc = "target no connection";
917 break;
918 case MPI2_IOCSTATUS_TARGET_XFER_COUNT_MISMATCH:
919 desc = "target xfer count mismatch";
920 break;
921 case MPI2_IOCSTATUS_TARGET_DATA_OFFSET_ERROR:
922 desc = "target data offset error";
923 break;
924 case MPI2_IOCSTATUS_TARGET_TOO_MUCH_WRITE_DATA:
925 desc = "target too much write data";
926 break;
927 case MPI2_IOCSTATUS_TARGET_IU_TOO_SHORT:
928 desc = "target iu too short";
929 break;
930 case MPI2_IOCSTATUS_TARGET_ACK_NAK_TIMEOUT:
931 desc = "target ack nak timeout";
932 break;
933 case MPI2_IOCSTATUS_TARGET_NAK_RECEIVED:
934 desc = "target nak received";
935 break;
936
937/****************************************************************************
938* Serial Attached SCSI values
939****************************************************************************/
940
941 case MPI2_IOCSTATUS_SAS_SMP_REQUEST_FAILED:
942 desc = "smp request failed";
943 break;
944 case MPI2_IOCSTATUS_SAS_SMP_DATA_OVERRUN:
945 desc = "smp data overrun";
946 break;
947
948/****************************************************************************
949* Diagnostic Buffer Post / Diagnostic Release values
950****************************************************************************/
951
952 case MPI2_IOCSTATUS_DIAGNOSTIC_RELEASED:
953 desc = "diagnostic released";
954 break;
955 default:
956 break;
957 }
958
959 if (!desc)
960 return;
961
962 switch (request_hdr->Function) {
963 case MPI2_FUNCTION_CONFIG:
964 frame_sz = sizeof(Mpi2ConfigRequest_t) + ioc->sge_size;
965 func_str = "config_page";
966 break;
967 case MPI2_FUNCTION_SCSI_TASK_MGMT:
968 frame_sz = sizeof(Mpi2SCSITaskManagementRequest_t);
969 func_str = "task_mgmt";
970 break;
971 case MPI2_FUNCTION_SAS_IO_UNIT_CONTROL:
972 frame_sz = sizeof(Mpi2SasIoUnitControlRequest_t);
973 func_str = "sas_iounit_ctl";
974 break;
975 case MPI2_FUNCTION_SCSI_ENCLOSURE_PROCESSOR:
976 frame_sz = sizeof(Mpi2SepRequest_t);
977 func_str = "enclosure";
978 break;
979 case MPI2_FUNCTION_IOC_INIT:
980 frame_sz = sizeof(Mpi2IOCInitRequest_t);
981 func_str = "ioc_init";
982 break;
983 case MPI2_FUNCTION_PORT_ENABLE:
984 frame_sz = sizeof(Mpi2PortEnableRequest_t);
985 func_str = "port_enable";
986 break;
987 case MPI2_FUNCTION_SMP_PASSTHROUGH:
988 frame_sz = sizeof(Mpi2SmpPassthroughRequest_t) + ioc->sge_size;
989 func_str = "smp_passthru";
990 break;
Suganath Prabu Subramaniaff39e62017-10-31 18:02:29 +0530991 case MPI2_FUNCTION_NVME_ENCAPSULATED:
992 frame_sz = sizeof(Mpi26NVMeEncapsulatedRequest_t) +
993 ioc->sge_size;
994 func_str = "nvme_encapsulated";
995 break;
Sreekanth Reddyf92363d2012-11-30 07:44:21 +0530996 default:
997 frame_sz = 32;
998 func_str = "unknown";
999 break;
1000 }
1001
Joe Perches919d8a32018-09-17 08:01:09 -07001002 ioc_warn(ioc, "ioc_status: %s(0x%04x), request(0x%p),(%s)\n",
1003 desc, ioc_status, request_hdr, func_str);
Sreekanth Reddyf92363d2012-11-30 07:44:21 +05301004
1005 _debug_dump_mf(request_hdr, frame_sz/4);
1006}
1007
1008/**
1009 * _base_display_event_data - verbose translation of firmware asyn events
1010 * @ioc: per adapter object
1011 * @mpi_reply: reply mf payload returned from firmware
Sreekanth Reddyf92363d2012-11-30 07:44:21 +05301012 */
1013static void
1014_base_display_event_data(struct MPT3SAS_ADAPTER *ioc,
1015 Mpi2EventNotificationReply_t *mpi_reply)
1016{
1017 char *desc = NULL;
1018 u16 event;
1019
1020 if (!(ioc->logging_level & MPT_DEBUG_EVENTS))
1021 return;
1022
1023 event = le16_to_cpu(mpi_reply->Event);
1024
1025 switch (event) {
1026 case MPI2_EVENT_LOG_DATA:
1027 desc = "Log Data";
1028 break;
1029 case MPI2_EVENT_STATE_CHANGE:
1030 desc = "Status Change";
1031 break;
1032 case MPI2_EVENT_HARD_RESET_RECEIVED:
1033 desc = "Hard Reset Received";
1034 break;
1035 case MPI2_EVENT_EVENT_CHANGE:
1036 desc = "Event Change";
1037 break;
1038 case MPI2_EVENT_SAS_DEVICE_STATUS_CHANGE:
1039 desc = "Device Status Change";
1040 break;
1041 case MPI2_EVENT_IR_OPERATION_STATUS:
Sreekanth Reddy7786ab62015-11-11 17:30:28 +05301042 if (!ioc->hide_ir_msg)
1043 desc = "IR Operation Status";
Sreekanth Reddyf92363d2012-11-30 07:44:21 +05301044 break;
1045 case MPI2_EVENT_SAS_DISCOVERY:
1046 {
1047 Mpi2EventDataSasDiscovery_t *event_data =
1048 (Mpi2EventDataSasDiscovery_t *)mpi_reply->EventData;
Joe Perches919d8a32018-09-17 08:01:09 -07001049 ioc_info(ioc, "Discovery: (%s)",
1050 event_data->ReasonCode == MPI2_EVENT_SAS_DISC_RC_STARTED ?
1051 "start" : "stop");
Sreekanth Reddyf92363d2012-11-30 07:44:21 +05301052 if (event_data->DiscoveryStatus)
Colin Ian Kingbbaf61e2017-08-15 14:51:45 +01001053 pr_cont(" discovery_status(0x%08x)",
Sreekanth Reddyf92363d2012-11-30 07:44:21 +05301054 le32_to_cpu(event_data->DiscoveryStatus));
Colin Ian Kingbbaf61e2017-08-15 14:51:45 +01001055 pr_cont("\n");
Sreekanth Reddyf92363d2012-11-30 07:44:21 +05301056 return;
1057 }
1058 case MPI2_EVENT_SAS_BROADCAST_PRIMITIVE:
1059 desc = "SAS Broadcast Primitive";
1060 break;
1061 case MPI2_EVENT_SAS_INIT_DEVICE_STATUS_CHANGE:
1062 desc = "SAS Init Device Status Change";
1063 break;
1064 case MPI2_EVENT_SAS_INIT_TABLE_OVERFLOW:
1065 desc = "SAS Init Table Overflow";
1066 break;
1067 case MPI2_EVENT_SAS_TOPOLOGY_CHANGE_LIST:
1068 desc = "SAS Topology Change List";
1069 break;
1070 case MPI2_EVENT_SAS_ENCL_DEVICE_STATUS_CHANGE:
1071 desc = "SAS Enclosure Device Status Change";
1072 break;
1073 case MPI2_EVENT_IR_VOLUME:
Sreekanth Reddy7786ab62015-11-11 17:30:28 +05301074 if (!ioc->hide_ir_msg)
1075 desc = "IR Volume";
Sreekanth Reddyf92363d2012-11-30 07:44:21 +05301076 break;
1077 case MPI2_EVENT_IR_PHYSICAL_DISK:
Sreekanth Reddy7786ab62015-11-11 17:30:28 +05301078 if (!ioc->hide_ir_msg)
1079 desc = "IR Physical Disk";
Sreekanth Reddyf92363d2012-11-30 07:44:21 +05301080 break;
1081 case MPI2_EVENT_IR_CONFIGURATION_CHANGE_LIST:
Sreekanth Reddy7786ab62015-11-11 17:30:28 +05301082 if (!ioc->hide_ir_msg)
1083 desc = "IR Configuration Change List";
Sreekanth Reddyf92363d2012-11-30 07:44:21 +05301084 break;
1085 case MPI2_EVENT_LOG_ENTRY_ADDED:
Sreekanth Reddy7786ab62015-11-11 17:30:28 +05301086 if (!ioc->hide_ir_msg)
1087 desc = "Log Entry Added";
Sreekanth Reddyf92363d2012-11-30 07:44:21 +05301088 break;
Sreekanth Reddy2d8ce8c2015-01-12 11:38:56 +05301089 case MPI2_EVENT_TEMP_THRESHOLD:
1090 desc = "Temperature Threshold";
1091 break;
Chaitra P Ba470a512016-05-06 14:29:27 +05301092 case MPI2_EVENT_ACTIVE_CABLE_EXCEPTION:
Sreekanth Reddyb99b1992017-10-10 18:41:14 +05301093 desc = "Cable Event";
Chaitra P Ba470a512016-05-06 14:29:27 +05301094 break;
Chaitra P B95540b82018-04-24 05:28:35 -04001095 case MPI2_EVENT_SAS_DEVICE_DISCOVERY_ERROR:
1096 desc = "SAS Device Discovery Error";
1097 break;
Suganath Prabu Subramani4318c732017-10-31 18:02:32 +05301098 case MPI2_EVENT_PCIE_DEVICE_STATUS_CHANGE:
1099 desc = "PCIE Device Status Change";
1100 break;
1101 case MPI2_EVENT_PCIE_ENUMERATION:
1102 {
1103 Mpi26EventDataPCIeEnumeration_t *event_data =
1104 (Mpi26EventDataPCIeEnumeration_t *)mpi_reply->EventData;
Joe Perches919d8a32018-09-17 08:01:09 -07001105 ioc_info(ioc, "PCIE Enumeration: (%s)",
1106 event_data->ReasonCode == MPI26_EVENT_PCIE_ENUM_RC_STARTED ?
1107 "start" : "stop");
Suganath Prabu Subramani4318c732017-10-31 18:02:32 +05301108 if (event_data->EnumerationStatus)
Joe Perches919d8a32018-09-17 08:01:09 -07001109 pr_cont("enumeration_status(0x%08x)",
1110 le32_to_cpu(event_data->EnumerationStatus));
1111 pr_cont("\n");
Suganath Prabu Subramani4318c732017-10-31 18:02:32 +05301112 return;
1113 }
1114 case MPI2_EVENT_PCIE_TOPOLOGY_CHANGE_LIST:
1115 desc = "PCIE Topology Change List";
1116 break;
Sreekanth Reddyf92363d2012-11-30 07:44:21 +05301117 }
1118
1119 if (!desc)
1120 return;
1121
Joe Perches919d8a32018-09-17 08:01:09 -07001122 ioc_info(ioc, "%s\n", desc);
Sreekanth Reddyf92363d2012-11-30 07:44:21 +05301123}
Sreekanth Reddyf92363d2012-11-30 07:44:21 +05301124
1125/**
1126 * _base_sas_log_info - verbose translation of firmware log info
1127 * @ioc: per adapter object
1128 * @log_info: log info
Sreekanth Reddyf92363d2012-11-30 07:44:21 +05301129 */
1130static void
1131_base_sas_log_info(struct MPT3SAS_ADAPTER *ioc , u32 log_info)
1132{
1133 union loginfo_type {
1134 u32 loginfo;
1135 struct {
1136 u32 subcode:16;
1137 u32 code:8;
1138 u32 originator:4;
1139 u32 bus_type:4;
1140 } dw;
1141 };
1142 union loginfo_type sas_loginfo;
1143 char *originator_str = NULL;
1144
1145 sas_loginfo.loginfo = log_info;
1146 if (sas_loginfo.dw.bus_type != 3 /*SAS*/)
1147 return;
1148
1149 /* each nexus loss loginfo */
1150 if (log_info == 0x31170000)
1151 return;
1152
1153 /* eat the loginfos associated with task aborts */
1154 if (ioc->ignore_loginfos && (log_info == 0x30050000 || log_info ==
1155 0x31140000 || log_info == 0x31130000))
1156 return;
1157
1158 switch (sas_loginfo.dw.originator) {
1159 case 0:
1160 originator_str = "IOP";
1161 break;
1162 case 1:
1163 originator_str = "PL";
1164 break;
1165 case 2:
Sreekanth Reddy7786ab62015-11-11 17:30:28 +05301166 if (!ioc->hide_ir_msg)
1167 originator_str = "IR";
1168 else
1169 originator_str = "WarpDrive";
Sreekanth Reddyf92363d2012-11-30 07:44:21 +05301170 break;
1171 }
1172
Joe Perches919d8a32018-09-17 08:01:09 -07001173 ioc_warn(ioc, "log_info(0x%08x): originator(%s), code(0x%02x), sub_code(0x%04x)\n",
1174 log_info,
1175 originator_str, sas_loginfo.dw.code, sas_loginfo.dw.subcode);
Sreekanth Reddyf92363d2012-11-30 07:44:21 +05301176}
1177
1178/**
1179 * _base_display_reply_info -
1180 * @ioc: per adapter object
1181 * @smid: system request message index
1182 * @msix_index: MSIX table index supplied by the OS
1183 * @reply: reply message frame(lower 32bit addr)
Sreekanth Reddyf92363d2012-11-30 07:44:21 +05301184 */
1185static void
1186_base_display_reply_info(struct MPT3SAS_ADAPTER *ioc, u16 smid, u8 msix_index,
1187 u32 reply)
1188{
1189 MPI2DefaultReply_t *mpi_reply;
1190 u16 ioc_status;
1191 u32 loginfo = 0;
1192
1193 mpi_reply = mpt3sas_base_get_reply_virt_addr(ioc, reply);
1194 if (unlikely(!mpi_reply)) {
Joe Perches919d8a32018-09-17 08:01:09 -07001195 ioc_err(ioc, "mpi_reply not valid at %s:%d/%s()!\n",
1196 __FILE__, __LINE__, __func__);
Sreekanth Reddyf92363d2012-11-30 07:44:21 +05301197 return;
1198 }
1199 ioc_status = le16_to_cpu(mpi_reply->IOCStatus);
Sreekanth Reddyaf009412015-11-11 17:30:23 +05301200
Sreekanth Reddyf92363d2012-11-30 07:44:21 +05301201 if ((ioc_status & MPI2_IOCSTATUS_MASK) &&
1202 (ioc->logging_level & MPT_DEBUG_REPLY)) {
1203 _base_sas_ioc_info(ioc , mpi_reply,
1204 mpt3sas_base_get_msg_frame(ioc, smid));
1205 }
Sreekanth Reddyaf009412015-11-11 17:30:23 +05301206
Sreekanth Reddyf92363d2012-11-30 07:44:21 +05301207 if (ioc_status & MPI2_IOCSTATUS_FLAG_LOG_INFO_AVAILABLE) {
1208 loginfo = le32_to_cpu(mpi_reply->IOCLogInfo);
1209 _base_sas_log_info(ioc, loginfo);
1210 }
1211
1212 if (ioc_status || loginfo) {
1213 ioc_status &= MPI2_IOCSTATUS_MASK;
1214 mpt3sas_trigger_mpi(ioc, ioc_status, loginfo);
1215 }
1216}
1217
1218/**
1219 * mpt3sas_base_done - base internal command completion routine
1220 * @ioc: per adapter object
1221 * @smid: system request message index
1222 * @msix_index: MSIX table index supplied by the OS
1223 * @reply: reply message frame(lower 32bit addr)
1224 *
Bart Van Assche4beb4862018-06-15 14:42:01 -07001225 * Return:
1226 * 1 meaning mf should be freed from _base_interrupt
1227 * 0 means the mf is freed from this function.
Sreekanth Reddyf92363d2012-11-30 07:44:21 +05301228 */
1229u8
1230mpt3sas_base_done(struct MPT3SAS_ADAPTER *ioc, u16 smid, u8 msix_index,
1231 u32 reply)
1232{
1233 MPI2DefaultReply_t *mpi_reply;
1234
1235 mpi_reply = mpt3sas_base_get_reply_virt_addr(ioc, reply);
1236 if (mpi_reply && mpi_reply->Function == MPI2_FUNCTION_EVENT_ACK)
Suganath prabu Subramanifd0331b2016-01-28 12:07:02 +05301237 return mpt3sas_check_for_pending_internal_cmds(ioc, smid);
Sreekanth Reddyf92363d2012-11-30 07:44:21 +05301238
1239 if (ioc->base_cmds.status == MPT3_CMD_NOT_USED)
1240 return 1;
1241
1242 ioc->base_cmds.status |= MPT3_CMD_COMPLETE;
1243 if (mpi_reply) {
1244 ioc->base_cmds.status |= MPT3_CMD_REPLY_VALID;
1245 memcpy(ioc->base_cmds.reply, mpi_reply, mpi_reply->MsgLength*4);
1246 }
1247 ioc->base_cmds.status &= ~MPT3_CMD_PENDING;
1248
1249 complete(&ioc->base_cmds.done);
1250 return 1;
1251}
1252
1253/**
1254 * _base_async_event - main callback handler for firmware asyn events
1255 * @ioc: per adapter object
1256 * @msix_index: MSIX table index supplied by the OS
1257 * @reply: reply message frame(lower 32bit addr)
1258 *
Bart Van Assche4beb4862018-06-15 14:42:01 -07001259 * Return:
1260 * 1 meaning mf should be freed from _base_interrupt
1261 * 0 means the mf is freed from this function.
Sreekanth Reddyf92363d2012-11-30 07:44:21 +05301262 */
1263static u8
1264_base_async_event(struct MPT3SAS_ADAPTER *ioc, u8 msix_index, u32 reply)
1265{
1266 Mpi2EventNotificationReply_t *mpi_reply;
1267 Mpi2EventAckRequest_t *ack_request;
1268 u16 smid;
Suganath prabu Subramanifd0331b2016-01-28 12:07:02 +05301269 struct _event_ack_list *delayed_event_ack;
Sreekanth Reddyf92363d2012-11-30 07:44:21 +05301270
1271 mpi_reply = mpt3sas_base_get_reply_virt_addr(ioc, reply);
1272 if (!mpi_reply)
1273 return 1;
1274 if (mpi_reply->Function != MPI2_FUNCTION_EVENT_NOTIFICATION)
1275 return 1;
Sreekanth Reddyaf009412015-11-11 17:30:23 +05301276
Sreekanth Reddyf92363d2012-11-30 07:44:21 +05301277 _base_display_event_data(ioc, mpi_reply);
Sreekanth Reddyaf009412015-11-11 17:30:23 +05301278
Sreekanth Reddyf92363d2012-11-30 07:44:21 +05301279 if (!(mpi_reply->AckRequired & MPI2_EVENT_NOTIFICATION_ACK_REQUIRED))
1280 goto out;
1281 smid = mpt3sas_base_get_smid(ioc, ioc->base_cb_idx);
1282 if (!smid) {
Suganath prabu Subramanifd0331b2016-01-28 12:07:02 +05301283 delayed_event_ack = kzalloc(sizeof(*delayed_event_ack),
1284 GFP_ATOMIC);
1285 if (!delayed_event_ack)
1286 goto out;
1287 INIT_LIST_HEAD(&delayed_event_ack->list);
1288 delayed_event_ack->Event = mpi_reply->Event;
1289 delayed_event_ack->EventContext = mpi_reply->EventContext;
1290 list_add_tail(&delayed_event_ack->list,
1291 &ioc->delayed_event_ack_list);
Joe Perches919d8a32018-09-17 08:01:09 -07001292 dewtprintk(ioc,
1293 ioc_info(ioc, "DELAYED: EVENT ACK: event (0x%04x)\n",
1294 le16_to_cpu(mpi_reply->Event)));
Sreekanth Reddyf92363d2012-11-30 07:44:21 +05301295 goto out;
1296 }
1297
1298 ack_request = mpt3sas_base_get_msg_frame(ioc, smid);
1299 memset(ack_request, 0, sizeof(Mpi2EventAckRequest_t));
1300 ack_request->Function = MPI2_FUNCTION_EVENT_ACK;
1301 ack_request->Event = mpi_reply->Event;
1302 ack_request->EventContext = mpi_reply->EventContext;
1303 ack_request->VF_ID = 0; /* TODO */
1304 ack_request->VP_ID = 0;
Suganath Prabu S078a4cc2019-05-31 08:14:34 -04001305 ioc->put_smid_default(ioc, smid);
Sreekanth Reddyf92363d2012-11-30 07:44:21 +05301306
1307 out:
1308
1309 /* scsih callback handler */
1310 mpt3sas_scsih_event_callback(ioc, msix_index, reply);
1311
1312 /* ctl callback handler */
1313 mpt3sas_ctl_event_callback(ioc, msix_index, reply);
1314
1315 return 1;
1316}
1317
Colin Ian King61dfb8a2018-01-11 16:55:39 +00001318static struct scsiio_tracker *
Suganath Prabu Subramanidbec4c902018-01-04 04:57:11 -08001319_get_st_from_smid(struct MPT3SAS_ADAPTER *ioc, u16 smid)
Hannes Reinecke12e7c6782018-01-04 04:57:05 -08001320{
Suganath Prabu Subramanidbec4c902018-01-04 04:57:11 -08001321 struct scsi_cmnd *cmd;
1322
Hannes Reinecke12e7c6782018-01-04 04:57:05 -08001323 if (WARN_ON(!smid) ||
1324 WARN_ON(smid >= ioc->hi_priority_smid))
1325 return NULL;
Suganath Prabu Subramanidbec4c902018-01-04 04:57:11 -08001326
1327 cmd = mpt3sas_scsih_scsi_lookup_get(ioc, smid);
1328 if (cmd)
1329 return scsi_cmd_priv(cmd);
1330
1331 return NULL;
Hannes Reinecke12e7c6782018-01-04 04:57:05 -08001332}
1333
Sreekanth Reddyf92363d2012-11-30 07:44:21 +05301334/**
1335 * _base_get_cb_idx - obtain the callback index
1336 * @ioc: per adapter object
1337 * @smid: system request message index
1338 *
Bart Van Assche4beb4862018-06-15 14:42:01 -07001339 * Return: callback index.
Sreekanth Reddyf92363d2012-11-30 07:44:21 +05301340 */
1341static u8
1342_base_get_cb_idx(struct MPT3SAS_ADAPTER *ioc, u16 smid)
1343{
1344 int i;
Hannes Reineckeb0cd285e2018-01-04 04:57:07 -08001345 u16 ctl_smid = ioc->scsiio_depth - INTERNAL_SCSIIO_CMDS_COUNT + 1;
Hannes Reineckeba4494d2018-01-04 04:57:01 -08001346 u8 cb_idx = 0xFF;
Sreekanth Reddyf92363d2012-11-30 07:44:21 +05301347
1348 if (smid < ioc->hi_priority_smid) {
Hannes Reinecke12e7c6782018-01-04 04:57:05 -08001349 struct scsiio_tracker *st;
1350
Hannes Reineckeb0cd285e2018-01-04 04:57:07 -08001351 if (smid < ctl_smid) {
Suganath Prabu Subramanidbec4c902018-01-04 04:57:11 -08001352 st = _get_st_from_smid(ioc, smid);
Hannes Reineckeb0cd285e2018-01-04 04:57:07 -08001353 if (st)
1354 cb_idx = st->cb_idx;
1355 } else if (smid == ctl_smid)
1356 cb_idx = ioc->ctl_cb_idx;
Sreekanth Reddyf92363d2012-11-30 07:44:21 +05301357 } else if (smid < ioc->internal_smid) {
1358 i = smid - ioc->hi_priority_smid;
1359 cb_idx = ioc->hpr_lookup[i].cb_idx;
1360 } else if (smid <= ioc->hba_queue_depth) {
1361 i = smid - ioc->internal_smid;
1362 cb_idx = ioc->internal_lookup[i].cb_idx;
Hannes Reineckeba4494d2018-01-04 04:57:01 -08001363 }
Sreekanth Reddyf92363d2012-11-30 07:44:21 +05301364 return cb_idx;
1365}
1366
1367/**
1368 * _base_mask_interrupts - disable interrupts
1369 * @ioc: per adapter object
1370 *
1371 * Disabling ResetIRQ, Reply and Doorbell Interrupts
Sreekanth Reddyf92363d2012-11-30 07:44:21 +05301372 */
1373static void
1374_base_mask_interrupts(struct MPT3SAS_ADAPTER *ioc)
1375{
1376 u32 him_register;
1377
1378 ioc->mask_interrupts = 1;
Suganath Prabu306eaf22018-12-07 12:58:34 +05301379 him_register = ioc->base_readl(&ioc->chip->HostInterruptMask);
Sreekanth Reddyf92363d2012-11-30 07:44:21 +05301380 him_register |= MPI2_HIM_DIM + MPI2_HIM_RIM + MPI2_HIM_RESET_IRQ_MASK;
1381 writel(him_register, &ioc->chip->HostInterruptMask);
Suganath Prabu306eaf22018-12-07 12:58:34 +05301382 ioc->base_readl(&ioc->chip->HostInterruptMask);
Sreekanth Reddyf92363d2012-11-30 07:44:21 +05301383}
1384
1385/**
1386 * _base_unmask_interrupts - enable interrupts
1387 * @ioc: per adapter object
1388 *
1389 * Enabling only Reply Interrupts
Sreekanth Reddyf92363d2012-11-30 07:44:21 +05301390 */
1391static void
1392_base_unmask_interrupts(struct MPT3SAS_ADAPTER *ioc)
1393{
1394 u32 him_register;
1395
Suganath Prabu306eaf22018-12-07 12:58:34 +05301396 him_register = ioc->base_readl(&ioc->chip->HostInterruptMask);
Sreekanth Reddyf92363d2012-11-30 07:44:21 +05301397 him_register &= ~MPI2_HIM_RIM;
1398 writel(him_register, &ioc->chip->HostInterruptMask);
1399 ioc->mask_interrupts = 0;
1400}
1401
1402union reply_descriptor {
1403 u64 word;
1404 struct {
1405 u32 low;
1406 u32 high;
1407 } u;
1408};
1409
Suganath Prabu51e3b2a2019-02-15 02:40:28 -05001410static u32 base_mod64(u64 dividend, u32 divisor)
Sreekanth Reddyf92363d2012-11-30 07:44:21 +05301411{
Suganath Prabu51e3b2a2019-02-15 02:40:28 -05001412 u32 remainder;
1413
1414 if (!divisor)
1415 pr_err("mpt3sas: DIVISOR is zero, in div fn\n");
1416 remainder = do_div(dividend, divisor);
1417 return remainder;
1418}
1419
Sreekanth Reddyf92363d2012-11-30 07:44:21 +05301420/**
Suganath Prabu233af102019-02-15 02:40:25 -05001421 * _base_process_reply_queue - Process reply descriptors from reply
1422 * descriptor post queue.
1423 * @reply_q: per IRQ's reply queue object.
Sreekanth Reddyf92363d2012-11-30 07:44:21 +05301424 *
Suganath Prabu233af102019-02-15 02:40:25 -05001425 * Return: number of reply descriptors processed from reply
1426 * descriptor queue.
Sreekanth Reddyf92363d2012-11-30 07:44:21 +05301427 */
Suganath Prabu233af102019-02-15 02:40:25 -05001428static int
1429_base_process_reply_queue(struct adapter_reply_queue *reply_q)
Sreekanth Reddyf92363d2012-11-30 07:44:21 +05301430{
Sreekanth Reddyf92363d2012-11-30 07:44:21 +05301431 union reply_descriptor rd;
Suganath Prabu288addd2019-02-15 02:40:29 -05001432 u64 completed_cmds;
Suganath Prabu2c063502019-02-15 02:40:24 -05001433 u8 request_descript_type;
Sreekanth Reddyf92363d2012-11-30 07:44:21 +05301434 u16 smid;
1435 u8 cb_idx;
1436 u32 reply;
1437 u8 msix_index = reply_q->msix_index;
1438 struct MPT3SAS_ADAPTER *ioc = reply_q->ioc;
1439 Mpi2ReplyDescriptorsUnion_t *rpf;
1440 u8 rc;
1441
Suganath Prabu233af102019-02-15 02:40:25 -05001442 completed_cmds = 0;
Sreekanth Reddyf92363d2012-11-30 07:44:21 +05301443 if (!atomic_add_unless(&reply_q->busy, 1, 1))
Suganath Prabu233af102019-02-15 02:40:25 -05001444 return completed_cmds;
Sreekanth Reddyf92363d2012-11-30 07:44:21 +05301445
1446 rpf = &reply_q->reply_post_free[reply_q->reply_post_host_index];
Suganath Prabu2c063502019-02-15 02:40:24 -05001447 request_descript_type = rpf->Default.ReplyFlags
Sreekanth Reddyf92363d2012-11-30 07:44:21 +05301448 & MPI2_RPY_DESCRIPT_FLAGS_TYPE_MASK;
Suganath Prabu2c063502019-02-15 02:40:24 -05001449 if (request_descript_type == MPI2_RPY_DESCRIPT_FLAGS_UNUSED) {
Sreekanth Reddyf92363d2012-11-30 07:44:21 +05301450 atomic_dec(&reply_q->busy);
Suganath Prabu233af102019-02-15 02:40:25 -05001451 return completed_cmds;
Sreekanth Reddyf92363d2012-11-30 07:44:21 +05301452 }
1453
Sreekanth Reddyf92363d2012-11-30 07:44:21 +05301454 cb_idx = 0xFF;
1455 do {
1456 rd.word = le64_to_cpu(rpf->Words);
1457 if (rd.u.low == UINT_MAX || rd.u.high == UINT_MAX)
1458 goto out;
1459 reply = 0;
1460 smid = le16_to_cpu(rpf->Default.DescriptorTypeDependent1);
Suganath Prabu2c063502019-02-15 02:40:24 -05001461 if (request_descript_type ==
Sreekanth Reddyf92363d2012-11-30 07:44:21 +05301462 MPI25_RPY_DESCRIPT_FLAGS_FAST_PATH_SCSI_IO_SUCCESS ||
Suganath Prabu2c063502019-02-15 02:40:24 -05001463 request_descript_type ==
Suganath Prabu Subramaniaff39e62017-10-31 18:02:29 +05301464 MPI2_RPY_DESCRIPT_FLAGS_SCSI_IO_SUCCESS ||
Suganath Prabu2c063502019-02-15 02:40:24 -05001465 request_descript_type ==
Suganath Prabu Subramaniaff39e62017-10-31 18:02:29 +05301466 MPI26_RPY_DESCRIPT_FLAGS_PCIE_ENCAPSULATED_SUCCESS) {
Sreekanth Reddyf92363d2012-11-30 07:44:21 +05301467 cb_idx = _base_get_cb_idx(ioc, smid);
1468 if ((likely(cb_idx < MPT_MAX_CALLBACKS)) &&
1469 (likely(mpt_callbacks[cb_idx] != NULL))) {
1470 rc = mpt_callbacks[cb_idx](ioc, smid,
1471 msix_index, 0);
1472 if (rc)
1473 mpt3sas_base_free_smid(ioc, smid);
1474 }
Suganath Prabu2c063502019-02-15 02:40:24 -05001475 } else if (request_descript_type ==
Sreekanth Reddyf92363d2012-11-30 07:44:21 +05301476 MPI2_RPY_DESCRIPT_FLAGS_ADDRESS_REPLY) {
1477 reply = le32_to_cpu(
1478 rpf->AddressReply.ReplyFrameAddress);
1479 if (reply > ioc->reply_dma_max_address ||
1480 reply < ioc->reply_dma_min_address)
1481 reply = 0;
1482 if (smid) {
1483 cb_idx = _base_get_cb_idx(ioc, smid);
1484 if ((likely(cb_idx < MPT_MAX_CALLBACKS)) &&
1485 (likely(mpt_callbacks[cb_idx] != NULL))) {
1486 rc = mpt_callbacks[cb_idx](ioc, smid,
1487 msix_index, reply);
1488 if (reply)
1489 _base_display_reply_info(ioc,
1490 smid, msix_index, reply);
1491 if (rc)
1492 mpt3sas_base_free_smid(ioc,
1493 smid);
1494 }
1495 } else {
1496 _base_async_event(ioc, msix_index, reply);
1497 }
1498
1499 /* reply free queue handling */
1500 if (reply) {
1501 ioc->reply_free_host_index =
1502 (ioc->reply_free_host_index ==
1503 (ioc->reply_free_queue_depth - 1)) ?
1504 0 : ioc->reply_free_host_index + 1;
1505 ioc->reply_free[ioc->reply_free_host_index] =
1506 cpu_to_le32(reply);
Suganath Prabu Subramanib4472d72018-02-07 02:51:50 -08001507 if (ioc->is_mcpu_endpoint)
1508 _base_clone_reply_to_sys_mem(ioc,
Chaitra P Bcf6bf972018-04-24 05:28:30 -04001509 reply,
Suganath Prabu Subramanib4472d72018-02-07 02:51:50 -08001510 ioc->reply_free_host_index);
Sreekanth Reddyf92363d2012-11-30 07:44:21 +05301511 writel(ioc->reply_free_host_index,
1512 &ioc->chip->ReplyFreeHostIndex);
1513 }
1514 }
1515
1516 rpf->Words = cpu_to_le64(ULLONG_MAX);
1517 reply_q->reply_post_host_index =
1518 (reply_q->reply_post_host_index ==
1519 (ioc->reply_post_queue_depth - 1)) ? 0 :
1520 reply_q->reply_post_host_index + 1;
Suganath Prabu2c063502019-02-15 02:40:24 -05001521 request_descript_type =
Sreekanth Reddyf92363d2012-11-30 07:44:21 +05301522 reply_q->reply_post_free[reply_q->reply_post_host_index].
1523 Default.ReplyFlags & MPI2_RPY_DESCRIPT_FLAGS_TYPE_MASK;
1524 completed_cmds++;
Chaitra P B6b4c3352017-01-23 15:26:09 +05301525 /* Update the reply post host index after continuously
1526 * processing the threshold number of Reply Descriptors.
1527 * So that FW can find enough entries to post the Reply
1528 * Descriptors in the reply descriptor post queue.
1529 */
Suganath Prabu288addd2019-02-15 02:40:29 -05001530 if (!base_mod64(completed_cmds, ioc->thresh_hold)) {
Chaitra P B6b4c3352017-01-23 15:26:09 +05301531 if (ioc->combined_reply_queue) {
1532 writel(reply_q->reply_post_host_index |
1533 ((msix_index & 7) <<
1534 MPI2_RPHI_MSIX_INDEX_SHIFT),
1535 ioc->replyPostRegisterIndex[msix_index/8]);
1536 } else {
1537 writel(reply_q->reply_post_host_index |
1538 (msix_index <<
1539 MPI2_RPHI_MSIX_INDEX_SHIFT),
1540 &ioc->chip->ReplyPostHostIndex);
1541 }
Suganath Prabu320e77a2019-02-15 02:40:27 -05001542 if (!reply_q->irq_poll_scheduled) {
1543 reply_q->irq_poll_scheduled = true;
1544 irq_poll_sched(&reply_q->irqpoll);
1545 }
Colin Ian Kingea9006d2019-03-20 16:50:00 +00001546 atomic_dec(&reply_q->busy);
1547 return completed_cmds;
Chaitra P B6b4c3352017-01-23 15:26:09 +05301548 }
Suganath Prabu2c063502019-02-15 02:40:24 -05001549 if (request_descript_type == MPI2_RPY_DESCRIPT_FLAGS_UNUSED)
Sreekanth Reddyf92363d2012-11-30 07:44:21 +05301550 goto out;
1551 if (!reply_q->reply_post_host_index)
1552 rpf = reply_q->reply_post_free;
1553 else
1554 rpf++;
1555 } while (1);
1556
1557 out:
1558
1559 if (!completed_cmds) {
1560 atomic_dec(&reply_q->busy);
Suganath Prabu233af102019-02-15 02:40:25 -05001561 return completed_cmds;
Sreekanth Reddyf92363d2012-11-30 07:44:21 +05301562 }
1563
Sreekanth Reddy7786ab62015-11-11 17:30:28 +05301564 if (ioc->is_warpdrive) {
1565 writel(reply_q->reply_post_host_index,
1566 ioc->reply_post_host_index[msix_index]);
1567 atomic_dec(&reply_q->busy);
Suganath Prabu233af102019-02-15 02:40:25 -05001568 return completed_cmds;
Sreekanth Reddy7786ab62015-11-11 17:30:28 +05301569 }
Sreekanth Reddyfb77bb52015-06-30 12:24:47 +05301570
1571 /* Update Reply Post Host Index.
1572 * For those HBA's which support combined reply queue feature
1573 * 1. Get the correct Supplemental Reply Post Host Index Register.
1574 * i.e. (msix_index / 8)th entry from Supplemental Reply Post Host
1575 * Index Register address bank i.e replyPostRegisterIndex[],
1576 * 2. Then update this register with new reply host index value
1577 * in ReplyPostIndex field and the MSIxIndex field with
1578 * msix_index value reduced to a value between 0 and 7,
1579 * using a modulo 8 operation. Since each Supplemental Reply Post
1580 * Host Index Register supports 8 MSI-X vectors.
1581 *
1582 * For other HBA's just update the Reply Post Host Index register with
1583 * new reply host index value in ReplyPostIndex Field and msix_index
1584 * value in MSIxIndex field.
1585 */
Suganath Prabu Subramani0bb337c2016-10-26 13:34:38 +05301586 if (ioc->combined_reply_queue)
Sreekanth Reddyfb77bb52015-06-30 12:24:47 +05301587 writel(reply_q->reply_post_host_index | ((msix_index & 7) <<
1588 MPI2_RPHI_MSIX_INDEX_SHIFT),
1589 ioc->replyPostRegisterIndex[msix_index/8]);
1590 else
1591 writel(reply_q->reply_post_host_index | (msix_index <<
1592 MPI2_RPHI_MSIX_INDEX_SHIFT),
1593 &ioc->chip->ReplyPostHostIndex);
Sreekanth Reddyf92363d2012-11-30 07:44:21 +05301594 atomic_dec(&reply_q->busy);
Suganath Prabu233af102019-02-15 02:40:25 -05001595 return completed_cmds;
1596}
1597
1598/**
1599 * _base_interrupt - MPT adapter (IOC) specific interrupt handler.
1600 * @irq: irq number (not used)
1601 * @bus_id: bus identifier cookie == pointer to MPT_ADAPTER structure
1602 *
1603 * Return: IRQ_HANDLED if processed, else IRQ_NONE.
1604 */
1605static irqreturn_t
1606_base_interrupt(int irq, void *bus_id)
1607{
1608 struct adapter_reply_queue *reply_q = bus_id;
1609 struct MPT3SAS_ADAPTER *ioc = reply_q->ioc;
1610
1611 if (ioc->mask_interrupts)
1612 return IRQ_NONE;
Suganath Prabu320e77a2019-02-15 02:40:27 -05001613 if (reply_q->irq_poll_scheduled)
1614 return IRQ_HANDLED;
Suganath Prabu233af102019-02-15 02:40:25 -05001615 return ((_base_process_reply_queue(reply_q) > 0) ?
1616 IRQ_HANDLED : IRQ_NONE);
Sreekanth Reddyf92363d2012-11-30 07:44:21 +05301617}
1618
1619/**
Suganath Prabu320e77a2019-02-15 02:40:27 -05001620 * _base_irqpoll - IRQ poll callback handler
1621 * @irqpoll - irq_poll object
1622 * @budget - irq poll weight
1623 *
1624 * returns number of reply descriptors processed
1625 */
1626static int
1627_base_irqpoll(struct irq_poll *irqpoll, int budget)
1628{
1629 struct adapter_reply_queue *reply_q;
1630 int num_entries = 0;
1631
1632 reply_q = container_of(irqpoll, struct adapter_reply_queue,
1633 irqpoll);
1634 if (reply_q->irq_line_enable) {
1635 disable_irq(reply_q->os_irq);
1636 reply_q->irq_line_enable = false;
1637 }
1638 num_entries = _base_process_reply_queue(reply_q);
1639 if (num_entries < budget) {
1640 irq_poll_complete(irqpoll);
1641 reply_q->irq_poll_scheduled = false;
1642 reply_q->irq_line_enable = true;
1643 enable_irq(reply_q->os_irq);
1644 }
1645
1646 return num_entries;
1647}
1648
1649/**
1650 * _base_init_irqpolls - initliaze IRQ polls
1651 * @ioc: per adapter object
1652 *
1653 * returns nothing
1654 */
1655static void
1656_base_init_irqpolls(struct MPT3SAS_ADAPTER *ioc)
1657{
1658 struct adapter_reply_queue *reply_q, *next;
1659
1660 if (list_empty(&ioc->reply_queue_list))
1661 return;
1662
1663 list_for_each_entry_safe(reply_q, next, &ioc->reply_queue_list, list) {
1664 irq_poll_init(&reply_q->irqpoll,
1665 ioc->hba_queue_depth/4, _base_irqpoll);
1666 reply_q->irq_poll_scheduled = false;
1667 reply_q->irq_line_enable = true;
1668 reply_q->os_irq = pci_irq_vector(ioc->pdev,
1669 reply_q->msix_index);
1670 }
Sreekanth Reddyf92363d2012-11-30 07:44:21 +05301671}
1672
1673/**
1674 * _base_is_controller_msix_enabled - is controller support muli-reply queues
1675 * @ioc: per adapter object
1676 *
Bart Van Assche4beb4862018-06-15 14:42:01 -07001677 * Return: Whether or not MSI/X is enabled.
Sreekanth Reddyf92363d2012-11-30 07:44:21 +05301678 */
1679static inline int
1680_base_is_controller_msix_enabled(struct MPT3SAS_ADAPTER *ioc)
1681{
1682 return (ioc->facts.IOCCapabilities &
1683 MPI2_IOCFACTS_CAPABILITY_MSI_X_INDEX) && ioc->msix_enable;
1684}
1685
1686/**
Chaitra P B5f0dfb72016-05-06 14:29:31 +05301687 * mpt3sas_base_sync_reply_irqs - flush pending MSIX interrupts
Sreekanth Reddyf92363d2012-11-30 07:44:21 +05301688 * @ioc: per adapter object
Chaitra P B5f0dfb72016-05-06 14:29:31 +05301689 * Context: non ISR conext
Sreekanth Reddyf92363d2012-11-30 07:44:21 +05301690 *
Chaitra P B5f0dfb72016-05-06 14:29:31 +05301691 * Called when a Task Management request has completed.
Sreekanth Reddyf92363d2012-11-30 07:44:21 +05301692 */
1693void
Chaitra P B5f0dfb72016-05-06 14:29:31 +05301694mpt3sas_base_sync_reply_irqs(struct MPT3SAS_ADAPTER *ioc)
Sreekanth Reddyf92363d2012-11-30 07:44:21 +05301695{
1696 struct adapter_reply_queue *reply_q;
1697
1698 /* If MSIX capability is turned off
1699 * then multi-queues are not enabled
1700 */
1701 if (!_base_is_controller_msix_enabled(ioc))
1702 return;
1703
1704 list_for_each_entry(reply_q, &ioc->reply_queue_list, list) {
Chaitra P B5f0dfb72016-05-06 14:29:31 +05301705 if (ioc->shost_recovery || ioc->remove_host ||
1706 ioc->pci_error_recovery)
Sreekanth Reddyf92363d2012-11-30 07:44:21 +05301707 return;
1708 /* TMs are on msix_index == 0 */
1709 if (reply_q->msix_index == 0)
1710 continue;
Suganath Prabu320e77a2019-02-15 02:40:27 -05001711 if (reply_q->irq_poll_scheduled) {
1712 /* Calling irq_poll_disable will wait for any pending
1713 * callbacks to have completed.
1714 */
1715 irq_poll_disable(&reply_q->irqpoll);
1716 irq_poll_enable(&reply_q->irqpoll);
1717 reply_q->irq_poll_scheduled = false;
1718 reply_q->irq_line_enable = true;
1719 enable_irq(reply_q->os_irq);
1720 continue;
1721 }
Hannes Reinecke1d55abc2017-02-22 11:31:41 +01001722 synchronize_irq(pci_irq_vector(ioc->pdev, reply_q->msix_index));
Sreekanth Reddyf92363d2012-11-30 07:44:21 +05301723 }
1724}
1725
1726/**
1727 * mpt3sas_base_release_callback_handler - clear interrupt callback handler
1728 * @cb_idx: callback index
Sreekanth Reddyf92363d2012-11-30 07:44:21 +05301729 */
1730void
1731mpt3sas_base_release_callback_handler(u8 cb_idx)
1732{
1733 mpt_callbacks[cb_idx] = NULL;
1734}
1735
1736/**
1737 * mpt3sas_base_register_callback_handler - obtain index for the interrupt callback handler
1738 * @cb_func: callback function
1739 *
Bart Van Assche4beb4862018-06-15 14:42:01 -07001740 * Return: Index of @cb_func.
Sreekanth Reddyf92363d2012-11-30 07:44:21 +05301741 */
1742u8
1743mpt3sas_base_register_callback_handler(MPT_CALLBACK cb_func)
1744{
1745 u8 cb_idx;
1746
1747 for (cb_idx = MPT_MAX_CALLBACKS-1; cb_idx; cb_idx--)
1748 if (mpt_callbacks[cb_idx] == NULL)
1749 break;
1750
1751 mpt_callbacks[cb_idx] = cb_func;
1752 return cb_idx;
1753}
1754
1755/**
1756 * mpt3sas_base_initialize_callback_handler - initialize the interrupt callback handler
Sreekanth Reddyf92363d2012-11-30 07:44:21 +05301757 */
1758void
1759mpt3sas_base_initialize_callback_handler(void)
1760{
1761 u8 cb_idx;
1762
1763 for (cb_idx = 0; cb_idx < MPT_MAX_CALLBACKS; cb_idx++)
1764 mpt3sas_base_release_callback_handler(cb_idx);
1765}
1766
1767
1768/**
1769 * _base_build_zero_len_sge - build zero length sg entry
1770 * @ioc: per adapter object
1771 * @paddr: virtual address for SGE
1772 *
1773 * Create a zero length scatter gather entry to insure the IOCs hardware has
1774 * something to use if the target device goes brain dead and tries
1775 * to send data even when none is asked for.
Sreekanth Reddyf92363d2012-11-30 07:44:21 +05301776 */
1777static void
1778_base_build_zero_len_sge(struct MPT3SAS_ADAPTER *ioc, void *paddr)
1779{
1780 u32 flags_length = (u32)((MPI2_SGE_FLAGS_LAST_ELEMENT |
1781 MPI2_SGE_FLAGS_END_OF_BUFFER | MPI2_SGE_FLAGS_END_OF_LIST |
1782 MPI2_SGE_FLAGS_SIMPLE_ELEMENT) <<
1783 MPI2_SGE_FLAGS_SHIFT);
1784 ioc->base_add_sg_single(paddr, flags_length, -1);
1785}
1786
1787/**
1788 * _base_add_sg_single_32 - Place a simple 32 bit SGE at address pAddr.
1789 * @paddr: virtual address for SGE
1790 * @flags_length: SGE flags and data transfer length
1791 * @dma_addr: Physical address
Sreekanth Reddyf92363d2012-11-30 07:44:21 +05301792 */
1793static void
1794_base_add_sg_single_32(void *paddr, u32 flags_length, dma_addr_t dma_addr)
1795{
1796 Mpi2SGESimple32_t *sgel = paddr;
1797
1798 flags_length |= (MPI2_SGE_FLAGS_32_BIT_ADDRESSING |
1799 MPI2_SGE_FLAGS_SYSTEM_ADDRESS) << MPI2_SGE_FLAGS_SHIFT;
1800 sgel->FlagsLength = cpu_to_le32(flags_length);
1801 sgel->Address = cpu_to_le32(dma_addr);
1802}
1803
1804
1805/**
1806 * _base_add_sg_single_64 - Place a simple 64 bit SGE at address pAddr.
1807 * @paddr: virtual address for SGE
1808 * @flags_length: SGE flags and data transfer length
1809 * @dma_addr: Physical address
Sreekanth Reddyf92363d2012-11-30 07:44:21 +05301810 */
1811static void
1812_base_add_sg_single_64(void *paddr, u32 flags_length, dma_addr_t dma_addr)
1813{
1814 Mpi2SGESimple64_t *sgel = paddr;
1815
1816 flags_length |= (MPI2_SGE_FLAGS_64_BIT_ADDRESSING |
1817 MPI2_SGE_FLAGS_SYSTEM_ADDRESS) << MPI2_SGE_FLAGS_SHIFT;
1818 sgel->FlagsLength = cpu_to_le32(flags_length);
1819 sgel->Address = cpu_to_le64(dma_addr);
1820}
1821
1822/**
1823 * _base_get_chain_buffer_tracker - obtain chain tracker
1824 * @ioc: per adapter object
Suganath Prabu Subramanidbec4c902018-01-04 04:57:11 -08001825 * @scmd: SCSI commands of the IO request
Sreekanth Reddyf92363d2012-11-30 07:44:21 +05301826 *
Bart Van Assche4beb4862018-06-15 14:42:01 -07001827 * Return: chain tracker from chain_lookup table using key as
Chaitra P B93204b72018-04-24 05:28:32 -04001828 * smid and smid's chain_offset.
Sreekanth Reddyf92363d2012-11-30 07:44:21 +05301829 */
1830static struct chain_tracker *
Suganath Prabu Subramanidbec4c902018-01-04 04:57:11 -08001831_base_get_chain_buffer_tracker(struct MPT3SAS_ADAPTER *ioc,
1832 struct scsi_cmnd *scmd)
Sreekanth Reddyf92363d2012-11-30 07:44:21 +05301833{
1834 struct chain_tracker *chain_req;
Suganath Prabu Subramanidbec4c902018-01-04 04:57:11 -08001835 struct scsiio_tracker *st = scsi_cmd_priv(scmd);
Chaitra P B93204b72018-04-24 05:28:32 -04001836 u16 smid = st->smid;
1837 u8 chain_offset =
1838 atomic_read(&ioc->chain_lookup[smid - 1].chain_offset);
Sreekanth Reddyf92363d2012-11-30 07:44:21 +05301839
Chaitra P B93204b72018-04-24 05:28:32 -04001840 if (chain_offset == ioc->chains_needed_per_io)
Sreekanth Reddyf92363d2012-11-30 07:44:21 +05301841 return NULL;
Chaitra P B93204b72018-04-24 05:28:32 -04001842
1843 chain_req = &ioc->chain_lookup[smid - 1].chains_per_smid[chain_offset];
1844 atomic_inc(&ioc->chain_lookup[smid - 1].chain_offset);
Sreekanth Reddyf92363d2012-11-30 07:44:21 +05301845 return chain_req;
1846}
1847
1848
1849/**
1850 * _base_build_sg - build generic sg
1851 * @ioc: per adapter object
1852 * @psge: virtual address for SGE
1853 * @data_out_dma: physical address for WRITES
1854 * @data_out_sz: data xfer size for WRITES
1855 * @data_in_dma: physical address for READS
1856 * @data_in_sz: data xfer size for READS
Sreekanth Reddyf92363d2012-11-30 07:44:21 +05301857 */
1858static void
1859_base_build_sg(struct MPT3SAS_ADAPTER *ioc, void *psge,
1860 dma_addr_t data_out_dma, size_t data_out_sz, dma_addr_t data_in_dma,
1861 size_t data_in_sz)
1862{
1863 u32 sgl_flags;
1864
1865 if (!data_out_sz && !data_in_sz) {
1866 _base_build_zero_len_sge(ioc, psge);
1867 return;
1868 }
1869
1870 if (data_out_sz && data_in_sz) {
1871 /* WRITE sgel first */
1872 sgl_flags = (MPI2_SGE_FLAGS_SIMPLE_ELEMENT |
1873 MPI2_SGE_FLAGS_END_OF_BUFFER | MPI2_SGE_FLAGS_HOST_TO_IOC);
1874 sgl_flags = sgl_flags << MPI2_SGE_FLAGS_SHIFT;
1875 ioc->base_add_sg_single(psge, sgl_flags |
1876 data_out_sz, data_out_dma);
1877
1878 /* incr sgel */
1879 psge += ioc->sge_size;
1880
1881 /* READ sgel last */
1882 sgl_flags = (MPI2_SGE_FLAGS_SIMPLE_ELEMENT |
1883 MPI2_SGE_FLAGS_LAST_ELEMENT | MPI2_SGE_FLAGS_END_OF_BUFFER |
1884 MPI2_SGE_FLAGS_END_OF_LIST);
1885 sgl_flags = sgl_flags << MPI2_SGE_FLAGS_SHIFT;
1886 ioc->base_add_sg_single(psge, sgl_flags |
1887 data_in_sz, data_in_dma);
1888 } else if (data_out_sz) /* WRITE */ {
1889 sgl_flags = (MPI2_SGE_FLAGS_SIMPLE_ELEMENT |
1890 MPI2_SGE_FLAGS_LAST_ELEMENT | MPI2_SGE_FLAGS_END_OF_BUFFER |
1891 MPI2_SGE_FLAGS_END_OF_LIST | MPI2_SGE_FLAGS_HOST_TO_IOC);
1892 sgl_flags = sgl_flags << MPI2_SGE_FLAGS_SHIFT;
1893 ioc->base_add_sg_single(psge, sgl_flags |
1894 data_out_sz, data_out_dma);
1895 } else if (data_in_sz) /* READ */ {
1896 sgl_flags = (MPI2_SGE_FLAGS_SIMPLE_ELEMENT |
1897 MPI2_SGE_FLAGS_LAST_ELEMENT | MPI2_SGE_FLAGS_END_OF_BUFFER |
1898 MPI2_SGE_FLAGS_END_OF_LIST);
1899 sgl_flags = sgl_flags << MPI2_SGE_FLAGS_SHIFT;
1900 ioc->base_add_sg_single(psge, sgl_flags |
1901 data_in_sz, data_in_dma);
1902 }
1903}
1904
Suganath Prabu Subramaniaff39e62017-10-31 18:02:29 +05301905/* IEEE format sgls */
1906
1907/**
1908 * _base_build_nvme_prp - This function is called for NVMe end devices to build
1909 * a native SGL (NVMe PRP). The native SGL is built starting in the first PRP
1910 * entry of the NVMe message (PRP1). If the data buffer is small enough to be
1911 * described entirely using PRP1, then PRP2 is not used. If needed, PRP2 is
1912 * used to describe a larger data buffer. If the data buffer is too large to
1913 * describe using the two PRP entriess inside the NVMe message, then PRP1
1914 * describes the first data memory segment, and PRP2 contains a pointer to a PRP
1915 * list located elsewhere in memory to describe the remaining data memory
1916 * segments. The PRP list will be contiguous.
Bart Van Assche4beb4862018-06-15 14:42:01 -07001917 *
Suganath Prabu Subramaniaff39e62017-10-31 18:02:29 +05301918 * The native SGL for NVMe devices is a Physical Region Page (PRP). A PRP
1919 * consists of a list of PRP entries to describe a number of noncontigous
1920 * physical memory segments as a single memory buffer, just as a SGL does. Note
1921 * however, that this function is only used by the IOCTL call, so the memory
1922 * given will be guaranteed to be contiguous. There is no need to translate
1923 * non-contiguous SGL into a PRP in this case. All PRPs will describe
1924 * contiguous space that is one page size each.
1925 *
1926 * Each NVMe message contains two PRP entries. The first (PRP1) either contains
1927 * a PRP list pointer or a PRP element, depending upon the command. PRP2
1928 * contains the second PRP element if the memory being described fits within 2
1929 * PRP entries, or a PRP list pointer if the PRP spans more than two entries.
1930 *
1931 * A PRP list pointer contains the address of a PRP list, structured as a linear
1932 * array of PRP entries. Each PRP entry in this list describes a segment of
1933 * physical memory.
1934 *
1935 * Each 64-bit PRP entry comprises an address and an offset field. The address
1936 * always points at the beginning of a 4KB physical memory page, and the offset
1937 * describes where within that 4KB page the memory segment begins. Only the
1938 * first element in a PRP list may contain a non-zero offest, implying that all
1939 * memory segments following the first begin at the start of a 4KB page.
1940 *
1941 * Each PRP element normally describes 4KB of physical memory, with exceptions
1942 * for the first and last elements in the list. If the memory being described
1943 * by the list begins at a non-zero offset within the first 4KB page, then the
1944 * first PRP element will contain a non-zero offset indicating where the region
1945 * begins within the 4KB page. The last memory segment may end before the end
1946 * of the 4KB segment, depending upon the overall size of the memory being
1947 * described by the PRP list.
1948 *
1949 * Since PRP entries lack any indication of size, the overall data buffer length
1950 * is used to determine where the end of the data memory buffer is located, and
1951 * how many PRP entries are required to describe it.
1952 *
1953 * @ioc: per adapter object
1954 * @smid: system request message index for getting asscociated SGL
1955 * @nvme_encap_request: the NVMe request msg frame pointer
1956 * @data_out_dma: physical address for WRITES
1957 * @data_out_sz: data xfer size for WRITES
1958 * @data_in_dma: physical address for READS
1959 * @data_in_sz: data xfer size for READS
Suganath Prabu Subramaniaff39e62017-10-31 18:02:29 +05301960 */
1961static void
1962_base_build_nvme_prp(struct MPT3SAS_ADAPTER *ioc, u16 smid,
1963 Mpi26NVMeEncapsulatedRequest_t *nvme_encap_request,
1964 dma_addr_t data_out_dma, size_t data_out_sz, dma_addr_t data_in_dma,
1965 size_t data_in_sz)
1966{
1967 int prp_size = NVME_PRP_SIZE;
Arnd Bergmannd8335ae2017-11-06 14:35:16 +01001968 __le64 *prp_entry, *prp1_entry, *prp2_entry;
1969 __le64 *prp_page;
1970 dma_addr_t prp_entry_dma, prp_page_dma, dma_addr;
Suganath Prabu Subramaniaff39e62017-10-31 18:02:29 +05301971 u32 offset, entry_len;
1972 u32 page_mask_result, page_mask;
Suganath Prabu Subramaniaff39e62017-10-31 18:02:29 +05301973 size_t length;
Bart Van Assche84203b32018-06-15 14:41:57 -07001974 struct mpt3sas_nvme_cmd *nvme_cmd =
1975 (void *)nvme_encap_request->NVMe_Command;
Suganath Prabu Subramaniaff39e62017-10-31 18:02:29 +05301976
1977 /*
1978 * Not all commands require a data transfer. If no data, just return
1979 * without constructing any PRP.
1980 */
1981 if (!data_in_sz && !data_out_sz)
1982 return;
Bart Van Assche84203b32018-06-15 14:41:57 -07001983 prp1_entry = &nvme_cmd->prp1;
1984 prp2_entry = &nvme_cmd->prp2;
Suganath Prabu Subramaniaff39e62017-10-31 18:02:29 +05301985 prp_entry = prp1_entry;
1986 /*
1987 * For the PRP entries, use the specially allocated buffer of
1988 * contiguous memory.
1989 */
Suganath Prabu Subramani494f4012017-10-31 18:02:38 +05301990 prp_page = (__le64 *)mpt3sas_base_get_pcie_sgl(ioc, smid);
Arnd Bergmannd8335ae2017-11-06 14:35:16 +01001991 prp_page_dma = mpt3sas_base_get_pcie_sgl_dma(ioc, smid);
Suganath Prabu Subramaniaff39e62017-10-31 18:02:29 +05301992
1993 /*
1994 * Check if we are within 1 entry of a page boundary we don't
1995 * want our first entry to be a PRP List entry.
1996 */
1997 page_mask = ioc->page_size - 1;
1998 page_mask_result = (uintptr_t)((u8 *)prp_page + prp_size) & page_mask;
1999 if (!page_mask_result) {
2000 /* Bump up to next page boundary. */
Suganath Prabu Subramani494f4012017-10-31 18:02:38 +05302001 prp_page = (__le64 *)((u8 *)prp_page + prp_size);
Arnd Bergmannd8335ae2017-11-06 14:35:16 +01002002 prp_page_dma = prp_page_dma + prp_size;
Suganath Prabu Subramaniaff39e62017-10-31 18:02:29 +05302003 }
2004
2005 /*
2006 * Set PRP physical pointer, which initially points to the current PRP
2007 * DMA memory page.
2008 */
Arnd Bergmannd8335ae2017-11-06 14:35:16 +01002009 prp_entry_dma = prp_page_dma;
Suganath Prabu Subramaniaff39e62017-10-31 18:02:29 +05302010
2011 /* Get physical address and length of the data buffer. */
2012 if (data_in_sz) {
Arnd Bergmannd8335ae2017-11-06 14:35:16 +01002013 dma_addr = data_in_dma;
Suganath Prabu Subramaniaff39e62017-10-31 18:02:29 +05302014 length = data_in_sz;
2015 } else {
Arnd Bergmannd8335ae2017-11-06 14:35:16 +01002016 dma_addr = data_out_dma;
Suganath Prabu Subramaniaff39e62017-10-31 18:02:29 +05302017 length = data_out_sz;
2018 }
2019
2020 /* Loop while the length is not zero. */
2021 while (length) {
2022 /*
2023 * Check if we need to put a list pointer here if we are at
2024 * page boundary - prp_size (8 bytes).
2025 */
Arnd Bergmannd8335ae2017-11-06 14:35:16 +01002026 page_mask_result = (prp_entry_dma + prp_size) & page_mask;
Suganath Prabu Subramaniaff39e62017-10-31 18:02:29 +05302027 if (!page_mask_result) {
2028 /*
2029 * This is the last entry in a PRP List, so we need to
2030 * put a PRP list pointer here. What this does is:
2031 * - bump the current memory pointer to the next
2032 * address, which will be the next full page.
2033 * - set the PRP Entry to point to that page. This
2034 * is now the PRP List pointer.
2035 * - bump the PRP Entry pointer the start of the
2036 * next page. Since all of this PRP memory is
2037 * contiguous, no need to get a new page - it's
2038 * just the next address.
2039 */
Arnd Bergmannd8335ae2017-11-06 14:35:16 +01002040 prp_entry_dma++;
2041 *prp_entry = cpu_to_le64(prp_entry_dma);
Suganath Prabu Subramaniaff39e62017-10-31 18:02:29 +05302042 prp_entry++;
2043 }
2044
2045 /* Need to handle if entry will be part of a page. */
Arnd Bergmannd8335ae2017-11-06 14:35:16 +01002046 offset = dma_addr & page_mask;
Suganath Prabu Subramaniaff39e62017-10-31 18:02:29 +05302047 entry_len = ioc->page_size - offset;
2048
2049 if (prp_entry == prp1_entry) {
2050 /*
2051 * Must fill in the first PRP pointer (PRP1) before
2052 * moving on.
2053 */
Arnd Bergmannd8335ae2017-11-06 14:35:16 +01002054 *prp1_entry = cpu_to_le64(dma_addr);
Suganath Prabu Subramaniaff39e62017-10-31 18:02:29 +05302055
2056 /*
2057 * Now point to the second PRP entry within the
2058 * command (PRP2).
2059 */
2060 prp_entry = prp2_entry;
2061 } else if (prp_entry == prp2_entry) {
2062 /*
2063 * Should the PRP2 entry be a PRP List pointer or just
2064 * a regular PRP pointer? If there is more than one
2065 * more page of data, must use a PRP List pointer.
2066 */
2067 if (length > ioc->page_size) {
2068 /*
2069 * PRP2 will contain a PRP List pointer because
2070 * more PRP's are needed with this command. The
2071 * list will start at the beginning of the
2072 * contiguous buffer.
2073 */
Arnd Bergmannd8335ae2017-11-06 14:35:16 +01002074 *prp2_entry = cpu_to_le64(prp_entry_dma);
Suganath Prabu Subramaniaff39e62017-10-31 18:02:29 +05302075
2076 /*
2077 * The next PRP Entry will be the start of the
2078 * first PRP List.
2079 */
2080 prp_entry = prp_page;
2081 } else {
2082 /*
2083 * After this, the PRP Entries are complete.
2084 * This command uses 2 PRP's and no PRP list.
2085 */
Arnd Bergmannd8335ae2017-11-06 14:35:16 +01002086 *prp2_entry = cpu_to_le64(dma_addr);
Suganath Prabu Subramaniaff39e62017-10-31 18:02:29 +05302087 }
2088 } else {
2089 /*
2090 * Put entry in list and bump the addresses.
2091 *
2092 * After PRP1 and PRP2 are filled in, this will fill in
2093 * all remaining PRP entries in a PRP List, one per
2094 * each time through the loop.
2095 */
Arnd Bergmannd8335ae2017-11-06 14:35:16 +01002096 *prp_entry = cpu_to_le64(dma_addr);
Suganath Prabu Subramaniaff39e62017-10-31 18:02:29 +05302097 prp_entry++;
Arnd Bergmannd8335ae2017-11-06 14:35:16 +01002098 prp_entry_dma++;
Suganath Prabu Subramaniaff39e62017-10-31 18:02:29 +05302099 }
2100
2101 /*
2102 * Bump the phys address of the command's data buffer by the
2103 * entry_len.
2104 */
Arnd Bergmannd8335ae2017-11-06 14:35:16 +01002105 dma_addr += entry_len;
Suganath Prabu Subramaniaff39e62017-10-31 18:02:29 +05302106
2107 /* Decrement length accounting for last partial page. */
2108 if (entry_len > length)
2109 length = 0;
2110 else
2111 length -= entry_len;
2112 }
2113}
2114
Suganath Prabu Subramani016d5c32017-10-31 18:02:28 +05302115/**
2116 * base_make_prp_nvme -
2117 * Prepare PRPs(Physical Region Page)- SGLs specific to NVMe drives only
2118 *
2119 * @ioc: per adapter object
2120 * @scmd: SCSI command from the mid-layer
2121 * @mpi_request: mpi request
2122 * @smid: msg Index
2123 * @sge_count: scatter gather element count.
2124 *
Bart Van Assche4beb4862018-06-15 14:42:01 -07002125 * Return: true: PRPs are built
Suganath Prabu Subramani016d5c32017-10-31 18:02:28 +05302126 * false: IEEE SGLs needs to be built
2127 */
Suganath Prabu Subramani494f4012017-10-31 18:02:38 +05302128static void
Suganath Prabu Subramani016d5c32017-10-31 18:02:28 +05302129base_make_prp_nvme(struct MPT3SAS_ADAPTER *ioc,
2130 struct scsi_cmnd *scmd,
2131 Mpi25SCSIIORequest_t *mpi_request,
2132 u16 smid, int sge_count)
2133{
Arnd Bergmannd8335ae2017-11-06 14:35:16 +01002134 int sge_len, num_prp_in_chain = 0;
Suganath Prabu Subramani016d5c32017-10-31 18:02:28 +05302135 Mpi25IeeeSgeChain64_t *main_chain_element, *ptr_first_sgl;
Suganath Prabu Subramani494f4012017-10-31 18:02:38 +05302136 __le64 *curr_buff;
Arnd Bergmannd8335ae2017-11-06 14:35:16 +01002137 dma_addr_t msg_dma, sge_addr, offset;
Suganath Prabu Subramani016d5c32017-10-31 18:02:28 +05302138 u32 page_mask, page_mask_result;
2139 struct scatterlist *sg_scmd;
2140 u32 first_prp_len;
2141 int data_len = scsi_bufflen(scmd);
2142 u32 nvme_pg_size;
2143
2144 nvme_pg_size = max_t(u32, ioc->page_size, NVME_PRP_PAGE_SIZE);
2145 /*
2146 * Nvme has a very convoluted prp format. One prp is required
2147 * for each page or partial page. Driver need to split up OS sg_list
2148 * entries if it is longer than one page or cross a page
2149 * boundary. Driver also have to insert a PRP list pointer entry as
2150 * the last entry in each physical page of the PRP list.
2151 *
2152 * NOTE: The first PRP "entry" is actually placed in the first
2153 * SGL entry in the main message as IEEE 64 format. The 2nd
2154 * entry in the main message is the chain element, and the rest
2155 * of the PRP entries are built in the contiguous pcie buffer.
2156 */
2157 page_mask = nvme_pg_size - 1;
2158
2159 /*
2160 * Native SGL is needed.
2161 * Put a chain element in main message frame that points to the first
2162 * chain buffer.
2163 *
2164 * NOTE: The ChainOffset field must be 0 when using a chain pointer to
2165 * a native SGL.
2166 */
2167
2168 /* Set main message chain element pointer */
2169 main_chain_element = (pMpi25IeeeSgeChain64_t)&mpi_request->SGL;
2170 /*
2171 * For NVMe the chain element needs to be the 2nd SG entry in the main
2172 * message.
2173 */
2174 main_chain_element = (Mpi25IeeeSgeChain64_t *)
2175 ((u8 *)main_chain_element + sizeof(MPI25_IEEE_SGE_CHAIN64));
2176
2177 /*
2178 * For the PRP entries, use the specially allocated buffer of
2179 * contiguous memory. Normal chain buffers can't be used
2180 * because each chain buffer would need to be the size of an OS
2181 * page (4k).
2182 */
2183 curr_buff = mpt3sas_base_get_pcie_sgl(ioc, smid);
Arnd Bergmannd8335ae2017-11-06 14:35:16 +01002184 msg_dma = mpt3sas_base_get_pcie_sgl_dma(ioc, smid);
Suganath Prabu Subramani016d5c32017-10-31 18:02:28 +05302185
Arnd Bergmannd8335ae2017-11-06 14:35:16 +01002186 main_chain_element->Address = cpu_to_le64(msg_dma);
Suganath Prabu Subramani016d5c32017-10-31 18:02:28 +05302187 main_chain_element->NextChainOffset = 0;
2188 main_chain_element->Flags = MPI2_IEEE_SGE_FLAGS_CHAIN_ELEMENT |
2189 MPI2_IEEE_SGE_FLAGS_SYSTEM_ADDR |
2190 MPI26_IEEE_SGE_FLAGS_NSF_NVME_PRP;
2191
2192 /* Build first prp, sge need not to be page aligned*/
2193 ptr_first_sgl = (pMpi25IeeeSgeChain64_t)&mpi_request->SGL;
2194 sg_scmd = scsi_sglist(scmd);
2195 sge_addr = sg_dma_address(sg_scmd);
2196 sge_len = sg_dma_len(sg_scmd);
2197
Arnd Bergmannd8335ae2017-11-06 14:35:16 +01002198 offset = sge_addr & page_mask;
Suganath Prabu Subramani016d5c32017-10-31 18:02:28 +05302199 first_prp_len = nvme_pg_size - offset;
2200
2201 ptr_first_sgl->Address = cpu_to_le64(sge_addr);
2202 ptr_first_sgl->Length = cpu_to_le32(first_prp_len);
2203
2204 data_len -= first_prp_len;
2205
2206 if (sge_len > first_prp_len) {
2207 sge_addr += first_prp_len;
2208 sge_len -= first_prp_len;
2209 } else if (data_len && (sge_len == first_prp_len)) {
2210 sg_scmd = sg_next(sg_scmd);
2211 sge_addr = sg_dma_address(sg_scmd);
2212 sge_len = sg_dma_len(sg_scmd);
2213 }
2214
2215 for (;;) {
Arnd Bergmannd8335ae2017-11-06 14:35:16 +01002216 offset = sge_addr & page_mask;
Suganath Prabu Subramani016d5c32017-10-31 18:02:28 +05302217
2218 /* Put PRP pointer due to page boundary*/
2219 page_mask_result = (uintptr_t)(curr_buff + 1) & page_mask;
2220 if (unlikely(!page_mask_result)) {
2221 scmd_printk(KERN_NOTICE,
2222 scmd, "page boundary curr_buff: 0x%p\n",
2223 curr_buff);
Arnd Bergmannd8335ae2017-11-06 14:35:16 +01002224 msg_dma += 8;
2225 *curr_buff = cpu_to_le64(msg_dma);
Suganath Prabu Subramani016d5c32017-10-31 18:02:28 +05302226 curr_buff++;
2227 num_prp_in_chain++;
2228 }
2229
2230 *curr_buff = cpu_to_le64(sge_addr);
2231 curr_buff++;
Arnd Bergmannd8335ae2017-11-06 14:35:16 +01002232 msg_dma += 8;
Suganath Prabu Subramani016d5c32017-10-31 18:02:28 +05302233 num_prp_in_chain++;
2234
2235 sge_addr += nvme_pg_size;
2236 sge_len -= nvme_pg_size;
2237 data_len -= nvme_pg_size;
2238
2239 if (data_len <= 0)
2240 break;
2241
2242 if (sge_len > 0)
2243 continue;
2244
2245 sg_scmd = sg_next(sg_scmd);
2246 sge_addr = sg_dma_address(sg_scmd);
2247 sge_len = sg_dma_len(sg_scmd);
2248 }
2249
2250 main_chain_element->Length =
2251 cpu_to_le32(num_prp_in_chain * sizeof(u64));
2252 return;
2253}
2254
2255static bool
2256base_is_prp_possible(struct MPT3SAS_ADAPTER *ioc,
2257 struct _pcie_device *pcie_device, struct scsi_cmnd *scmd, int sge_count)
2258{
2259 u32 data_length = 0;
Suganath Prabu Subramani016d5c32017-10-31 18:02:28 +05302260 bool build_prp = true;
2261
Suganath Prabu Subramani494f4012017-10-31 18:02:38 +05302262 data_length = scsi_bufflen(scmd);
Suganath Prabu5bb309d2019-08-03 09:59:50 -04002263 if (pcie_device &&
2264 (mpt3sas_scsih_is_pcie_scsi_device(pcie_device->device_info))) {
2265 build_prp = false;
2266 return build_prp;
2267 }
Suganath Prabu Subramani016d5c32017-10-31 18:02:28 +05302268
2269 /* If Datalenth is <= 16K and number of SGE’s entries are <= 2
2270 * we built IEEE SGL
2271 */
2272 if ((data_length <= NVME_PRP_PAGE_SIZE*4) && (sge_count <= 2))
2273 build_prp = false;
2274
2275 return build_prp;
2276}
2277
2278/**
2279 * _base_check_pcie_native_sgl - This function is called for PCIe end devices to
2280 * determine if the driver needs to build a native SGL. If so, that native
2281 * SGL is built in the special contiguous buffers allocated especially for
2282 * PCIe SGL creation. If the driver will not build a native SGL, return
2283 * TRUE and a normal IEEE SGL will be built. Currently this routine
2284 * supports NVMe.
2285 * @ioc: per adapter object
2286 * @mpi_request: mf request pointer
2287 * @smid: system request message index
2288 * @scmd: scsi command
2289 * @pcie_device: points to the PCIe device's info
2290 *
Bart Van Assche4beb4862018-06-15 14:42:01 -07002291 * Return: 0 if native SGL was built, 1 if no SGL was built
Suganath Prabu Subramani016d5c32017-10-31 18:02:28 +05302292 */
2293static int
2294_base_check_pcie_native_sgl(struct MPT3SAS_ADAPTER *ioc,
2295 Mpi25SCSIIORequest_t *mpi_request, u16 smid, struct scsi_cmnd *scmd,
2296 struct _pcie_device *pcie_device)
2297{
Suganath Prabu Subramani016d5c32017-10-31 18:02:28 +05302298 int sges_left;
2299
2300 /* Get the SG list pointer and info. */
Suganath Prabu Subramani016d5c32017-10-31 18:02:28 +05302301 sges_left = scsi_dma_map(scmd);
2302 if (sges_left < 0) {
2303 sdev_printk(KERN_ERR, scmd->device,
2304 "scsi_dma_map failed: request for %d bytes!\n",
2305 scsi_bufflen(scmd));
2306 return 1;
2307 }
2308
2309 /* Check if we need to build a native SG list. */
2310 if (base_is_prp_possible(ioc, pcie_device,
2311 scmd, sges_left) == 0) {
2312 /* We built a native SG list, just return. */
2313 goto out;
2314 }
2315
2316 /*
2317 * Build native NVMe PRP.
2318 */
2319 base_make_prp_nvme(ioc, scmd, mpi_request,
2320 smid, sges_left);
2321
2322 return 0;
2323out:
2324 scsi_dma_unmap(scmd);
2325 return 1;
2326}
Sreekanth Reddyf92363d2012-11-30 07:44:21 +05302327
2328/**
2329 * _base_add_sg_single_ieee - add sg element for IEEE format
2330 * @paddr: virtual address for SGE
2331 * @flags: SGE flags
2332 * @chain_offset: number of 128 byte elements from start of segment
2333 * @length: data transfer length
2334 * @dma_addr: Physical address
Sreekanth Reddyf92363d2012-11-30 07:44:21 +05302335 */
2336static void
2337_base_add_sg_single_ieee(void *paddr, u8 flags, u8 chain_offset, u32 length,
2338 dma_addr_t dma_addr)
2339{
2340 Mpi25IeeeSgeChain64_t *sgel = paddr;
2341
2342 sgel->Flags = flags;
2343 sgel->NextChainOffset = chain_offset;
2344 sgel->Length = cpu_to_le32(length);
2345 sgel->Address = cpu_to_le64(dma_addr);
2346}
2347
2348/**
2349 * _base_build_zero_len_sge_ieee - build zero length sg entry for IEEE format
2350 * @ioc: per adapter object
2351 * @paddr: virtual address for SGE
2352 *
2353 * Create a zero length scatter gather entry to insure the IOCs hardware has
2354 * something to use if the target device goes brain dead and tries
2355 * to send data even when none is asked for.
Sreekanth Reddyf92363d2012-11-30 07:44:21 +05302356 */
2357static void
2358_base_build_zero_len_sge_ieee(struct MPT3SAS_ADAPTER *ioc, void *paddr)
2359{
2360 u8 sgl_flags = (MPI2_IEEE_SGE_FLAGS_SIMPLE_ELEMENT |
2361 MPI2_IEEE_SGE_FLAGS_SYSTEM_ADDR |
2362 MPI25_IEEE_SGE_FLAGS_END_OF_LIST);
Suganath prabu Subramanib130b0d2016-01-28 12:06:58 +05302363
Sreekanth Reddyf92363d2012-11-30 07:44:21 +05302364 _base_add_sg_single_ieee(paddr, sgl_flags, 0, 0, -1);
2365}
2366
2367/**
Sreekanth Reddy471ef9d2015-11-11 17:30:24 +05302368 * _base_build_sg_scmd - main sg creation routine
Suganath Prabu Subramani016d5c32017-10-31 18:02:28 +05302369 * pcie_device is unused here!
Sreekanth Reddy471ef9d2015-11-11 17:30:24 +05302370 * @ioc: per adapter object
2371 * @scmd: scsi command
2372 * @smid: system request message index
Suganath Prabu Subramani016d5c32017-10-31 18:02:28 +05302373 * @unused: unused pcie_device pointer
Sreekanth Reddy471ef9d2015-11-11 17:30:24 +05302374 * Context: none.
2375 *
2376 * The main routine that builds scatter gather table from a given
2377 * scsi request sent via the .queuecommand main handler.
2378 *
Bart Van Assche4beb4862018-06-15 14:42:01 -07002379 * Return: 0 success, anything else error
Sreekanth Reddy471ef9d2015-11-11 17:30:24 +05302380 */
2381static int
2382_base_build_sg_scmd(struct MPT3SAS_ADAPTER *ioc,
Suganath Prabu Subramani016d5c32017-10-31 18:02:28 +05302383 struct scsi_cmnd *scmd, u16 smid, struct _pcie_device *unused)
Sreekanth Reddy471ef9d2015-11-11 17:30:24 +05302384{
2385 Mpi2SCSIIORequest_t *mpi_request;
2386 dma_addr_t chain_dma;
2387 struct scatterlist *sg_scmd;
2388 void *sg_local, *chain;
2389 u32 chain_offset;
2390 u32 chain_length;
2391 u32 chain_flags;
2392 int sges_left;
2393 u32 sges_in_segment;
2394 u32 sgl_flags;
2395 u32 sgl_flags_last_element;
2396 u32 sgl_flags_end_buffer;
2397 struct chain_tracker *chain_req;
2398
2399 mpi_request = mpt3sas_base_get_msg_frame(ioc, smid);
2400
2401 /* init scatter gather flags */
2402 sgl_flags = MPI2_SGE_FLAGS_SIMPLE_ELEMENT;
2403 if (scmd->sc_data_direction == DMA_TO_DEVICE)
2404 sgl_flags |= MPI2_SGE_FLAGS_HOST_TO_IOC;
2405 sgl_flags_last_element = (sgl_flags | MPI2_SGE_FLAGS_LAST_ELEMENT)
2406 << MPI2_SGE_FLAGS_SHIFT;
2407 sgl_flags_end_buffer = (sgl_flags | MPI2_SGE_FLAGS_LAST_ELEMENT |
2408 MPI2_SGE_FLAGS_END_OF_BUFFER | MPI2_SGE_FLAGS_END_OF_LIST)
2409 << MPI2_SGE_FLAGS_SHIFT;
2410 sgl_flags = sgl_flags << MPI2_SGE_FLAGS_SHIFT;
2411
2412 sg_scmd = scsi_sglist(scmd);
2413 sges_left = scsi_dma_map(scmd);
2414 if (sges_left < 0) {
2415 sdev_printk(KERN_ERR, scmd->device,
Christoph Hellwig1c2048b2018-10-11 09:35:25 +02002416 "scsi_dma_map failed: request for %d bytes!\n",
Sreekanth Reddy471ef9d2015-11-11 17:30:24 +05302417 scsi_bufflen(scmd));
2418 return -ENOMEM;
2419 }
2420
2421 sg_local = &mpi_request->SGL;
2422 sges_in_segment = ioc->max_sges_in_main_message;
2423 if (sges_left <= sges_in_segment)
2424 goto fill_in_last_segment;
2425
2426 mpi_request->ChainOffset = (offsetof(Mpi2SCSIIORequest_t, SGL) +
2427 (sges_in_segment * ioc->sge_size))/4;
2428
2429 /* fill in main message segment when there is a chain following */
2430 while (sges_in_segment) {
2431 if (sges_in_segment == 1)
2432 ioc->base_add_sg_single(sg_local,
2433 sgl_flags_last_element | sg_dma_len(sg_scmd),
2434 sg_dma_address(sg_scmd));
2435 else
2436 ioc->base_add_sg_single(sg_local, sgl_flags |
2437 sg_dma_len(sg_scmd), sg_dma_address(sg_scmd));
2438 sg_scmd = sg_next(sg_scmd);
2439 sg_local += ioc->sge_size;
2440 sges_left--;
2441 sges_in_segment--;
2442 }
2443
2444 /* initializing the chain flags and pointers */
2445 chain_flags = MPI2_SGE_FLAGS_CHAIN_ELEMENT << MPI2_SGE_FLAGS_SHIFT;
Suganath Prabu Subramanidbec4c902018-01-04 04:57:11 -08002446 chain_req = _base_get_chain_buffer_tracker(ioc, scmd);
Sreekanth Reddy471ef9d2015-11-11 17:30:24 +05302447 if (!chain_req)
2448 return -1;
2449 chain = chain_req->chain_buffer;
2450 chain_dma = chain_req->chain_buffer_dma;
2451 do {
2452 sges_in_segment = (sges_left <=
2453 ioc->max_sges_in_chain_message) ? sges_left :
2454 ioc->max_sges_in_chain_message;
2455 chain_offset = (sges_left == sges_in_segment) ?
2456 0 : (sges_in_segment * ioc->sge_size)/4;
2457 chain_length = sges_in_segment * ioc->sge_size;
2458 if (chain_offset) {
2459 chain_offset = chain_offset <<
2460 MPI2_SGE_CHAIN_OFFSET_SHIFT;
2461 chain_length += ioc->sge_size;
2462 }
2463 ioc->base_add_sg_single(sg_local, chain_flags | chain_offset |
2464 chain_length, chain_dma);
2465 sg_local = chain;
2466 if (!chain_offset)
2467 goto fill_in_last_segment;
2468
2469 /* fill in chain segments */
2470 while (sges_in_segment) {
2471 if (sges_in_segment == 1)
2472 ioc->base_add_sg_single(sg_local,
2473 sgl_flags_last_element |
2474 sg_dma_len(sg_scmd),
2475 sg_dma_address(sg_scmd));
2476 else
2477 ioc->base_add_sg_single(sg_local, sgl_flags |
2478 sg_dma_len(sg_scmd),
2479 sg_dma_address(sg_scmd));
2480 sg_scmd = sg_next(sg_scmd);
2481 sg_local += ioc->sge_size;
2482 sges_left--;
2483 sges_in_segment--;
2484 }
2485
Suganath Prabu Subramanidbec4c902018-01-04 04:57:11 -08002486 chain_req = _base_get_chain_buffer_tracker(ioc, scmd);
Sreekanth Reddy471ef9d2015-11-11 17:30:24 +05302487 if (!chain_req)
2488 return -1;
2489 chain = chain_req->chain_buffer;
2490 chain_dma = chain_req->chain_buffer_dma;
2491 } while (1);
2492
2493
2494 fill_in_last_segment:
2495
2496 /* fill the last segment */
2497 while (sges_left) {
2498 if (sges_left == 1)
2499 ioc->base_add_sg_single(sg_local, sgl_flags_end_buffer |
2500 sg_dma_len(sg_scmd), sg_dma_address(sg_scmd));
2501 else
2502 ioc->base_add_sg_single(sg_local, sgl_flags |
2503 sg_dma_len(sg_scmd), sg_dma_address(sg_scmd));
2504 sg_scmd = sg_next(sg_scmd);
2505 sg_local += ioc->sge_size;
2506 sges_left--;
2507 }
2508
2509 return 0;
2510}
2511
2512/**
Sreekanth Reddyf92363d2012-11-30 07:44:21 +05302513 * _base_build_sg_scmd_ieee - main sg creation routine for IEEE format
2514 * @ioc: per adapter object
2515 * @scmd: scsi command
2516 * @smid: system request message index
Suganath Prabu Subramani016d5c32017-10-31 18:02:28 +05302517 * @pcie_device: Pointer to pcie_device. If set, the pcie native sgl will be
2518 * constructed on need.
Sreekanth Reddyf92363d2012-11-30 07:44:21 +05302519 * Context: none.
2520 *
2521 * The main routine that builds scatter gather table from a given
2522 * scsi request sent via the .queuecommand main handler.
2523 *
Bart Van Assche4beb4862018-06-15 14:42:01 -07002524 * Return: 0 success, anything else error
Sreekanth Reddyf92363d2012-11-30 07:44:21 +05302525 */
2526static int
2527_base_build_sg_scmd_ieee(struct MPT3SAS_ADAPTER *ioc,
Suganath Prabu Subramani016d5c32017-10-31 18:02:28 +05302528 struct scsi_cmnd *scmd, u16 smid, struct _pcie_device *pcie_device)
Sreekanth Reddyf92363d2012-11-30 07:44:21 +05302529{
Suganath Prabu Subramani016d5c32017-10-31 18:02:28 +05302530 Mpi25SCSIIORequest_t *mpi_request;
Sreekanth Reddyf92363d2012-11-30 07:44:21 +05302531 dma_addr_t chain_dma;
2532 struct scatterlist *sg_scmd;
2533 void *sg_local, *chain;
2534 u32 chain_offset;
2535 u32 chain_length;
Sreekanth Reddyf92363d2012-11-30 07:44:21 +05302536 int sges_left;
2537 u32 sges_in_segment;
2538 u8 simple_sgl_flags;
2539 u8 simple_sgl_flags_last;
2540 u8 chain_sgl_flags;
2541 struct chain_tracker *chain_req;
2542
2543 mpi_request = mpt3sas_base_get_msg_frame(ioc, smid);
2544
2545 /* init scatter gather flags */
2546 simple_sgl_flags = MPI2_IEEE_SGE_FLAGS_SIMPLE_ELEMENT |
2547 MPI2_IEEE_SGE_FLAGS_SYSTEM_ADDR;
2548 simple_sgl_flags_last = simple_sgl_flags |
2549 MPI25_IEEE_SGE_FLAGS_END_OF_LIST;
2550 chain_sgl_flags = MPI2_IEEE_SGE_FLAGS_CHAIN_ELEMENT |
2551 MPI2_IEEE_SGE_FLAGS_SYSTEM_ADDR;
2552
Suganath Prabu Subramani016d5c32017-10-31 18:02:28 +05302553 /* Check if we need to build a native SG list. */
2554 if ((pcie_device) && (_base_check_pcie_native_sgl(ioc, mpi_request,
2555 smid, scmd, pcie_device) == 0)) {
2556 /* We built a native SG list, just return. */
2557 return 0;
2558 }
2559
Sreekanth Reddyf92363d2012-11-30 07:44:21 +05302560 sg_scmd = scsi_sglist(scmd);
2561 sges_left = scsi_dma_map(scmd);
Sreekanth Reddy62f5c742015-06-30 12:25:01 +05302562 if (sges_left < 0) {
Sreekanth Reddyf92363d2012-11-30 07:44:21 +05302563 sdev_printk(KERN_ERR, scmd->device,
Christoph Hellwig1c2048b2018-10-11 09:35:25 +02002564 "scsi_dma_map failed: request for %d bytes!\n",
Sreekanth Reddyf92363d2012-11-30 07:44:21 +05302565 scsi_bufflen(scmd));
2566 return -ENOMEM;
2567 }
2568
2569 sg_local = &mpi_request->SGL;
2570 sges_in_segment = (ioc->request_sz -
Suganath Prabu Subramani016d5c32017-10-31 18:02:28 +05302571 offsetof(Mpi25SCSIIORequest_t, SGL))/ioc->sge_size_ieee;
Sreekanth Reddyf92363d2012-11-30 07:44:21 +05302572 if (sges_left <= sges_in_segment)
2573 goto fill_in_last_segment;
2574
2575 mpi_request->ChainOffset = (sges_in_segment - 1 /* chain element */) +
Suganath Prabu Subramani016d5c32017-10-31 18:02:28 +05302576 (offsetof(Mpi25SCSIIORequest_t, SGL)/ioc->sge_size_ieee);
Sreekanth Reddyf92363d2012-11-30 07:44:21 +05302577
2578 /* fill in main message segment when there is a chain following */
2579 while (sges_in_segment > 1) {
2580 _base_add_sg_single_ieee(sg_local, simple_sgl_flags, 0,
2581 sg_dma_len(sg_scmd), sg_dma_address(sg_scmd));
2582 sg_scmd = sg_next(sg_scmd);
2583 sg_local += ioc->sge_size_ieee;
2584 sges_left--;
2585 sges_in_segment--;
2586 }
2587
Wei Yongjun25ef16d2012-12-12 02:26:51 +05302588 /* initializing the pointers */
Suganath Prabu Subramanidbec4c902018-01-04 04:57:11 -08002589 chain_req = _base_get_chain_buffer_tracker(ioc, scmd);
Sreekanth Reddyf92363d2012-11-30 07:44:21 +05302590 if (!chain_req)
2591 return -1;
2592 chain = chain_req->chain_buffer;
2593 chain_dma = chain_req->chain_buffer_dma;
2594 do {
2595 sges_in_segment = (sges_left <=
2596 ioc->max_sges_in_chain_message) ? sges_left :
2597 ioc->max_sges_in_chain_message;
2598 chain_offset = (sges_left == sges_in_segment) ?
2599 0 : sges_in_segment;
2600 chain_length = sges_in_segment * ioc->sge_size_ieee;
2601 if (chain_offset)
2602 chain_length += ioc->sge_size_ieee;
2603 _base_add_sg_single_ieee(sg_local, chain_sgl_flags,
2604 chain_offset, chain_length, chain_dma);
2605
2606 sg_local = chain;
2607 if (!chain_offset)
2608 goto fill_in_last_segment;
2609
2610 /* fill in chain segments */
2611 while (sges_in_segment) {
2612 _base_add_sg_single_ieee(sg_local, simple_sgl_flags, 0,
2613 sg_dma_len(sg_scmd), sg_dma_address(sg_scmd));
2614 sg_scmd = sg_next(sg_scmd);
2615 sg_local += ioc->sge_size_ieee;
2616 sges_left--;
2617 sges_in_segment--;
2618 }
2619
Suganath Prabu Subramanidbec4c902018-01-04 04:57:11 -08002620 chain_req = _base_get_chain_buffer_tracker(ioc, scmd);
Sreekanth Reddyf92363d2012-11-30 07:44:21 +05302621 if (!chain_req)
2622 return -1;
2623 chain = chain_req->chain_buffer;
2624 chain_dma = chain_req->chain_buffer_dma;
2625 } while (1);
2626
2627
2628 fill_in_last_segment:
2629
2630 /* fill the last segment */
Sreekanth Reddy62f5c742015-06-30 12:25:01 +05302631 while (sges_left > 0) {
Sreekanth Reddyf92363d2012-11-30 07:44:21 +05302632 if (sges_left == 1)
2633 _base_add_sg_single_ieee(sg_local,
2634 simple_sgl_flags_last, 0, sg_dma_len(sg_scmd),
2635 sg_dma_address(sg_scmd));
2636 else
2637 _base_add_sg_single_ieee(sg_local, simple_sgl_flags, 0,
2638 sg_dma_len(sg_scmd), sg_dma_address(sg_scmd));
2639 sg_scmd = sg_next(sg_scmd);
2640 sg_local += ioc->sge_size_ieee;
2641 sges_left--;
2642 }
2643
2644 return 0;
2645}
2646
2647/**
2648 * _base_build_sg_ieee - build generic sg for IEEE format
2649 * @ioc: per adapter object
2650 * @psge: virtual address for SGE
2651 * @data_out_dma: physical address for WRITES
2652 * @data_out_sz: data xfer size for WRITES
2653 * @data_in_dma: physical address for READS
2654 * @data_in_sz: data xfer size for READS
Sreekanth Reddyf92363d2012-11-30 07:44:21 +05302655 */
2656static void
2657_base_build_sg_ieee(struct MPT3SAS_ADAPTER *ioc, void *psge,
2658 dma_addr_t data_out_dma, size_t data_out_sz, dma_addr_t data_in_dma,
2659 size_t data_in_sz)
2660{
2661 u8 sgl_flags;
2662
2663 if (!data_out_sz && !data_in_sz) {
2664 _base_build_zero_len_sge_ieee(ioc, psge);
2665 return;
2666 }
2667
2668 if (data_out_sz && data_in_sz) {
2669 /* WRITE sgel first */
2670 sgl_flags = MPI2_IEEE_SGE_FLAGS_SIMPLE_ELEMENT |
2671 MPI2_IEEE_SGE_FLAGS_SYSTEM_ADDR;
2672 _base_add_sg_single_ieee(psge, sgl_flags, 0, data_out_sz,
2673 data_out_dma);
2674
2675 /* incr sgel */
2676 psge += ioc->sge_size_ieee;
2677
2678 /* READ sgel last */
2679 sgl_flags |= MPI25_IEEE_SGE_FLAGS_END_OF_LIST;
2680 _base_add_sg_single_ieee(psge, sgl_flags, 0, data_in_sz,
2681 data_in_dma);
2682 } else if (data_out_sz) /* WRITE */ {
2683 sgl_flags = MPI2_IEEE_SGE_FLAGS_SIMPLE_ELEMENT |
2684 MPI25_IEEE_SGE_FLAGS_END_OF_LIST |
2685 MPI2_IEEE_SGE_FLAGS_SYSTEM_ADDR;
2686 _base_add_sg_single_ieee(psge, sgl_flags, 0, data_out_sz,
2687 data_out_dma);
2688 } else if (data_in_sz) /* READ */ {
2689 sgl_flags = MPI2_IEEE_SGE_FLAGS_SIMPLE_ELEMENT |
2690 MPI25_IEEE_SGE_FLAGS_END_OF_LIST |
2691 MPI2_IEEE_SGE_FLAGS_SYSTEM_ADDR;
2692 _base_add_sg_single_ieee(psge, sgl_flags, 0, data_in_sz,
2693 data_in_dma);
2694 }
2695}
2696
2697#define convert_to_kb(x) ((x) << (PAGE_SHIFT - 10))
2698
2699/**
2700 * _base_config_dma_addressing - set dma addressing
2701 * @ioc: per adapter object
2702 * @pdev: PCI device struct
2703 *
Bart Van Assche4beb4862018-06-15 14:42:01 -07002704 * Return: 0 for success, non-zero for failure.
Sreekanth Reddyf92363d2012-11-30 07:44:21 +05302705 */
2706static int
2707_base_config_dma_addressing(struct MPT3SAS_ADAPTER *ioc, struct pci_dev *pdev)
2708{
Christoph Hellwig1c2048b2018-10-11 09:35:25 +02002709 u64 required_mask, coherent_mask;
Sreekanth Reddyf92363d2012-11-30 07:44:21 +05302710 struct sysinfo s;
Suganath Prabudf9a6062019-07-30 03:43:57 -04002711 /* Set 63 bit DMA mask for all SAS3 and SAS35 controllers */
2712 int dma_mask = (ioc->hba_mpi_version_belonged > MPI2_VERSION) ? 63 : 64;
Sreekanth Reddy9b05c912014-09-12 15:35:31 +05302713
Suganath Prabu Subramani0448f012018-02-07 02:51:46 -08002714 if (ioc->is_mcpu_endpoint)
2715 goto try_32bit;
2716
Christoph Hellwig1c2048b2018-10-11 09:35:25 +02002717 required_mask = dma_get_required_mask(&pdev->dev);
2718 if (sizeof(dma_addr_t) == 4 || required_mask == 32)
2719 goto try_32bit;
Sreekanth Reddyf92363d2012-11-30 07:44:21 +05302720
Christoph Hellwig1c2048b2018-10-11 09:35:25 +02002721 if (ioc->dma_mask)
Suganath Prabudf9a6062019-07-30 03:43:57 -04002722 coherent_mask = DMA_BIT_MASK(dma_mask);
Christoph Hellwig1c2048b2018-10-11 09:35:25 +02002723 else
2724 coherent_mask = DMA_BIT_MASK(32);
2725
Suganath Prabudf9a6062019-07-30 03:43:57 -04002726 if (dma_set_mask(&pdev->dev, DMA_BIT_MASK(dma_mask)) ||
Christoph Hellwig1c2048b2018-10-11 09:35:25 +02002727 dma_set_coherent_mask(&pdev->dev, coherent_mask))
2728 goto try_32bit;
2729
2730 ioc->base_add_sg_single = &_base_add_sg_single_64;
2731 ioc->sge_size = sizeof(Mpi2SGESimple64_t);
Suganath Prabudf9a6062019-07-30 03:43:57 -04002732 ioc->dma_mask = dma_mask;
Christoph Hellwig1c2048b2018-10-11 09:35:25 +02002733 goto out;
Sreekanth Reddyf92363d2012-11-30 07:44:21 +05302734
Suganath Prabu Subramani0448f012018-02-07 02:51:46 -08002735 try_32bit:
Christoph Hellwig1c2048b2018-10-11 09:35:25 +02002736 if (dma_set_mask_and_coherent(&pdev->dev, DMA_BIT_MASK(32)))
Sreekanth Reddyf92363d2012-11-30 07:44:21 +05302737 return -ENODEV;
2738
Christoph Hellwig1c2048b2018-10-11 09:35:25 +02002739 ioc->base_add_sg_single = &_base_add_sg_single_32;
2740 ioc->sge_size = sizeof(Mpi2SGESimple32_t);
2741 ioc->dma_mask = 32;
Sreekanth Reddyf92363d2012-11-30 07:44:21 +05302742 out:
2743 si_meminfo(&s);
Joe Perches919d8a32018-09-17 08:01:09 -07002744 ioc_info(ioc, "%d BIT PCI BUS DMA ADDRESSING SUPPORTED, total mem (%ld kB)\n",
2745 ioc->dma_mask, convert_to_kb(s.totalram));
Sreekanth Reddyf92363d2012-11-30 07:44:21 +05302746
2747 return 0;
2748}
2749
Sreekanth Reddy9b05c912014-09-12 15:35:31 +05302750static int
2751_base_change_consistent_dma_mask(struct MPT3SAS_ADAPTER *ioc,
2752 struct pci_dev *pdev)
2753{
Suganath Prabudf9a6062019-07-30 03:43:57 -04002754 if (pci_set_consistent_dma_mask(pdev, DMA_BIT_MASK(ioc->dma_mask))) {
Sreekanth Reddy9b05c912014-09-12 15:35:31 +05302755 if (pci_set_consistent_dma_mask(pdev, DMA_BIT_MASK(32)))
2756 return -ENODEV;
2757 }
2758 return 0;
2759}
2760
Sreekanth Reddyf92363d2012-11-30 07:44:21 +05302761/**
2762 * _base_check_enable_msix - checks MSIX capabable.
2763 * @ioc: per adapter object
2764 *
2765 * Check to see if card is capable of MSIX, and set number
2766 * of available msix vectors
2767 */
2768static int
2769_base_check_enable_msix(struct MPT3SAS_ADAPTER *ioc)
2770{
2771 int base;
2772 u16 message_control;
2773
Sreekanth Reddy42081172015-11-11 17:30:26 +05302774 /* Check whether controller SAS2008 B0 controller,
2775 * if it is SAS2008 B0 controller use IO-APIC instead of MSIX
2776 */
2777 if (ioc->pdev->device == MPI2_MFGPAGE_DEVID_SAS2008 &&
2778 ioc->pdev->revision == SAS2_PCI_DEVICE_B0_REVISION) {
2779 return -EINVAL;
2780 }
2781
Sreekanth Reddyf92363d2012-11-30 07:44:21 +05302782 base = pci_find_capability(ioc->pdev, PCI_CAP_ID_MSIX);
2783 if (!base) {
Joe Perches919d8a32018-09-17 08:01:09 -07002784 dfailprintk(ioc, ioc_info(ioc, "msix not supported\n"));
Sreekanth Reddyf92363d2012-11-30 07:44:21 +05302785 return -EINVAL;
2786 }
2787
2788 /* get msix vector count */
Sreekanth Reddy42081172015-11-11 17:30:26 +05302789 /* NUMA_IO not supported for older controllers */
2790 if (ioc->pdev->device == MPI2_MFGPAGE_DEVID_SAS2004 ||
2791 ioc->pdev->device == MPI2_MFGPAGE_DEVID_SAS2008 ||
2792 ioc->pdev->device == MPI2_MFGPAGE_DEVID_SAS2108_1 ||
2793 ioc->pdev->device == MPI2_MFGPAGE_DEVID_SAS2108_2 ||
2794 ioc->pdev->device == MPI2_MFGPAGE_DEVID_SAS2108_3 ||
2795 ioc->pdev->device == MPI2_MFGPAGE_DEVID_SAS2116_1 ||
2796 ioc->pdev->device == MPI2_MFGPAGE_DEVID_SAS2116_2)
2797 ioc->msix_vector_count = 1;
2798 else {
2799 pci_read_config_word(ioc->pdev, base + 2, &message_control);
2800 ioc->msix_vector_count = (message_control & 0x3FF) + 1;
2801 }
Joe Perches919d8a32018-09-17 08:01:09 -07002802 dinitprintk(ioc, ioc_info(ioc, "msix is supported, vector_count(%d)\n",
2803 ioc->msix_vector_count));
Sreekanth Reddyf92363d2012-11-30 07:44:21 +05302804 return 0;
2805}
2806
2807/**
2808 * _base_free_irq - free irq
2809 * @ioc: per adapter object
2810 *
2811 * Freeing respective reply_queue from the list.
2812 */
2813static void
2814_base_free_irq(struct MPT3SAS_ADAPTER *ioc)
2815{
2816 struct adapter_reply_queue *reply_q, *next;
2817
2818 if (list_empty(&ioc->reply_queue_list))
2819 return;
2820
2821 list_for_each_entry_safe(reply_q, next, &ioc->reply_queue_list, list) {
2822 list_del(&reply_q->list);
Sreekanth Reddy610ef1e2019-06-24 10:42:55 -04002823 if (ioc->smp_affinity_enable)
Suganath Prabu S728bbc62019-05-31 08:14:40 -04002824 irq_set_affinity_hint(pci_irq_vector(ioc->pdev,
2825 reply_q->msix_index), NULL);
Hannes Reinecke1d55abc2017-02-22 11:31:41 +01002826 free_irq(pci_irq_vector(ioc->pdev, reply_q->msix_index),
2827 reply_q);
Sreekanth Reddyf92363d2012-11-30 07:44:21 +05302828 kfree(reply_q);
2829 }
2830}
2831
2832/**
2833 * _base_request_irq - request irq
2834 * @ioc: per adapter object
2835 * @index: msix index into vector table
Sreekanth Reddyf92363d2012-11-30 07:44:21 +05302836 *
2837 * Inserting respective reply_queue into the list.
2838 */
2839static int
Hannes Reinecke1d55abc2017-02-22 11:31:41 +01002840_base_request_irq(struct MPT3SAS_ADAPTER *ioc, u8 index)
Sreekanth Reddyf92363d2012-11-30 07:44:21 +05302841{
Hannes Reinecke1d55abc2017-02-22 11:31:41 +01002842 struct pci_dev *pdev = ioc->pdev;
Sreekanth Reddyf92363d2012-11-30 07:44:21 +05302843 struct adapter_reply_queue *reply_q;
2844 int r;
2845
2846 reply_q = kzalloc(sizeof(struct adapter_reply_queue), GFP_KERNEL);
2847 if (!reply_q) {
Joe Perches919d8a32018-09-17 08:01:09 -07002848 ioc_err(ioc, "unable to allocate memory %zu!\n",
2849 sizeof(struct adapter_reply_queue));
Sreekanth Reddyf92363d2012-11-30 07:44:21 +05302850 return -ENOMEM;
2851 }
2852 reply_q->ioc = ioc;
2853 reply_q->msix_index = index;
Sreekanth Reddy14b31142015-01-12 11:39:03 +05302854
Sreekanth Reddyf92363d2012-11-30 07:44:21 +05302855 atomic_set(&reply_q->busy, 0);
2856 if (ioc->msix_enable)
2857 snprintf(reply_q->name, MPT_NAME_LENGTH, "%s%d-msix%d",
Sreekanth Reddyc84b06a2015-11-11 17:30:35 +05302858 ioc->driver_name, ioc->id, index);
Sreekanth Reddyf92363d2012-11-30 07:44:21 +05302859 else
2860 snprintf(reply_q->name, MPT_NAME_LENGTH, "%s%d",
Sreekanth Reddyc84b06a2015-11-11 17:30:35 +05302861 ioc->driver_name, ioc->id);
Hannes Reinecke1d55abc2017-02-22 11:31:41 +01002862 r = request_irq(pci_irq_vector(pdev, index), _base_interrupt,
2863 IRQF_SHARED, reply_q->name, reply_q);
Sreekanth Reddyf92363d2012-11-30 07:44:21 +05302864 if (r) {
Joe Perchesfc7d5102018-09-17 08:01:11 -07002865 pr_err("%s: unable to allocate interrupt %d!\n",
Hannes Reinecke1d55abc2017-02-22 11:31:41 +01002866 reply_q->name, pci_irq_vector(pdev, index));
Suganath prabu Subramanida3cec22016-02-11 15:02:55 +05302867 kfree(reply_q);
Sreekanth Reddyf92363d2012-11-30 07:44:21 +05302868 return -EBUSY;
2869 }
2870
2871 INIT_LIST_HEAD(&reply_q->list);
2872 list_add_tail(&reply_q->list, &ioc->reply_queue_list);
2873 return 0;
2874}
2875
2876/**
2877 * _base_assign_reply_queues - assigning msix index for each cpu
2878 * @ioc: per adapter object
2879 *
2880 * The enduser would need to set the affinity via /proc/irq/#/smp_affinity
2881 *
2882 * It would nice if we could call irq_set_affinity, however it is not
2883 * an exported symbol
2884 */
2885static void
2886_base_assign_reply_queues(struct MPT3SAS_ADAPTER *ioc)
2887{
Martin K. Petersen91b265b2014-01-03 19:16:56 -05002888 unsigned int cpu, nr_cpus, nr_msix, index = 0;
Sreekanth Reddy14b31142015-01-12 11:39:03 +05302889 struct adapter_reply_queue *reply_q;
Suganath Prabu S728bbc62019-05-31 08:14:40 -04002890 int local_numa_node;
Sreekanth Reddyf92363d2012-11-30 07:44:21 +05302891
2892 if (!_base_is_controller_msix_enabled(ioc))
2893 return;
Sreekanth Reddyeedc42a2019-06-24 10:42:56 -04002894
2895 if (ioc->msix_load_balance)
Suganath Prabu51e3b2a2019-02-15 02:40:28 -05002896 return;
Sreekanth Reddyf92363d2012-11-30 07:44:21 +05302897
2898 memset(ioc->cpu_msix_table, 0, ioc->cpu_msix_table_sz);
2899
Martin K. Petersen91b265b2014-01-03 19:16:56 -05002900 nr_cpus = num_online_cpus();
2901 nr_msix = ioc->reply_queue_count = min(ioc->reply_queue_count,
2902 ioc->facts.MaxMSIxVectors);
2903 if (!nr_msix)
2904 return;
Sreekanth Reddyf92363d2012-11-30 07:44:21 +05302905
Sreekanth Reddy610ef1e2019-06-24 10:42:55 -04002906 if (ioc->smp_affinity_enable) {
Suganath Prabu S728bbc62019-05-31 08:14:40 -04002907
2908 /*
2909 * set irq affinity to local numa node for those irqs
2910 * corresponding to high iops queues.
2911 */
2912 if (ioc->high_iops_queues) {
2913 local_numa_node = dev_to_node(&ioc->pdev->dev);
2914 for (index = 0; index < ioc->high_iops_queues;
2915 index++) {
2916 irq_set_affinity_hint(pci_irq_vector(ioc->pdev,
2917 index), cpumask_of_node(local_numa_node));
2918 }
2919 }
2920
Hannes Reinecke1d55abc2017-02-22 11:31:41 +01002921 list_for_each_entry(reply_q, &ioc->reply_queue_list, list) {
Suganath Prabu S728bbc62019-05-31 08:14:40 -04002922 const cpumask_t *mask;
2923
2924 if (reply_q->msix_index < ioc->high_iops_queues)
2925 continue;
2926
2927 mask = pci_irq_get_affinity(ioc->pdev,
2928 reply_q->msix_index);
Hannes Reinecke1d55abc2017-02-22 11:31:41 +01002929 if (!mask) {
Joe Perches919d8a32018-09-17 08:01:09 -07002930 ioc_warn(ioc, "no affinity for msi %x\n",
2931 reply_q->msix_index);
Suganath Prabu S728bbc62019-05-31 08:14:40 -04002932 goto fall_back;
Hannes Reinecke1d55abc2017-02-22 11:31:41 +01002933 }
2934
Tomas Henzl4a8842d2018-01-19 16:22:05 +01002935 for_each_cpu_and(cpu, mask, cpu_online_mask) {
2936 if (cpu >= ioc->cpu_msix_table_sz)
2937 break;
Hannes Reinecke1d55abc2017-02-22 11:31:41 +01002938 ioc->cpu_msix_table[cpu] = reply_q->msix_index;
Tomas Henzl4a8842d2018-01-19 16:22:05 +01002939 }
Hannes Reinecke1d55abc2017-02-22 11:31:41 +01002940 }
2941 return;
2942 }
Suganath Prabu S728bbc62019-05-31 08:14:40 -04002943
2944fall_back:
Martin K. Petersen91b265b2014-01-03 19:16:56 -05002945 cpu = cpumask_first(cpu_online_mask);
Suganath Prabu S728bbc62019-05-31 08:14:40 -04002946 nr_msix -= ioc->high_iops_queues;
2947 index = 0;
Sreekanth Reddyf92363d2012-11-30 07:44:21 +05302948
Sreekanth Reddy14b31142015-01-12 11:39:03 +05302949 list_for_each_entry(reply_q, &ioc->reply_queue_list, list) {
Martin K. Petersen91b265b2014-01-03 19:16:56 -05002950 unsigned int i, group = nr_cpus / nr_msix;
2951
Suganath Prabu S728bbc62019-05-31 08:14:40 -04002952 if (reply_q->msix_index < ioc->high_iops_queues)
2953 continue;
2954
Sreekanth Reddy14b31142015-01-12 11:39:03 +05302955 if (cpu >= nr_cpus)
2956 break;
2957
Martin K. Petersen91b265b2014-01-03 19:16:56 -05002958 if (index < nr_cpus % nr_msix)
2959 group++;
2960
2961 for (i = 0 ; i < group ; i++) {
Hannes Reinecke1d55abc2017-02-22 11:31:41 +01002962 ioc->cpu_msix_table[cpu] = reply_q->msix_index;
Martin K. Petersen91b265b2014-01-03 19:16:56 -05002963 cpu = cpumask_next(cpu, cpu_online_mask);
Sreekanth Reddyf92363d2012-11-30 07:44:21 +05302964 }
Martin K. Petersen91b265b2014-01-03 19:16:56 -05002965 index++;
Sreekanth Reddy14b31142015-01-12 11:39:03 +05302966 }
Sreekanth Reddyf92363d2012-11-30 07:44:21 +05302967}
2968
2969/**
Suganath Prabu S18fd3d82019-05-31 08:14:36 -04002970 * _base_check_and_enable_high_iops_queues - enable high iops mode
2971 * @ ioc - per adapter object
2972 * @ hba_msix_vector_count - msix vectors supported by HBA
2973 *
2974 * Enable high iops queues only if
2975 * - HBA is a SEA/AERO controller and
2976 * - MSI-Xs vector supported by the HBA is 128 and
2977 * - total CPU count in the system >=16 and
2978 * - loaded driver with default max_msix_vectors module parameter and
2979 * - system booted in non kdump mode
2980 *
2981 * returns nothing.
2982 */
2983static void
2984_base_check_and_enable_high_iops_queues(struct MPT3SAS_ADAPTER *ioc,
2985 int hba_msix_vector_count)
2986{
Sreekanth Reddy48d6f0a2019-06-24 10:42:54 -04002987 u16 lnksta, speed;
Suganath Prabu Sca7e1e92019-05-31 08:14:42 -04002988
2989 if (perf_mode == MPT_PERF_MODE_IOPS ||
2990 perf_mode == MPT_PERF_MODE_LATENCY) {
2991 ioc->high_iops_queues = 0;
2992 return;
2993 }
2994
2995 if (perf_mode == MPT_PERF_MODE_DEFAULT) {
2996
Sreekanth Reddy48d6f0a2019-06-24 10:42:54 -04002997 pcie_capability_read_word(ioc->pdev, PCI_EXP_LNKSTA, &lnksta);
2998 speed = lnksta & PCI_EXP_LNKSTA_CLS;
Suganath Prabu Sca7e1e92019-05-31 08:14:42 -04002999
Sreekanth Reddy48d6f0a2019-06-24 10:42:54 -04003000 if (speed < 0x4) {
Suganath Prabu Sca7e1e92019-05-31 08:14:42 -04003001 ioc->high_iops_queues = 0;
3002 return;
3003 }
3004 }
Suganath Prabu S18fd3d82019-05-31 08:14:36 -04003005
3006 if (!reset_devices && ioc->is_aero_ioc &&
3007 hba_msix_vector_count == MPT3SAS_GEN35_MAX_MSIX_QUEUES &&
3008 num_online_cpus() >= MPT3SAS_HIGH_IOPS_REPLY_QUEUES &&
3009 max_msix_vectors == -1)
3010 ioc->high_iops_queues = MPT3SAS_HIGH_IOPS_REPLY_QUEUES;
3011 else
3012 ioc->high_iops_queues = 0;
3013}
3014
3015/**
Sreekanth Reddyf92363d2012-11-30 07:44:21 +05303016 * _base_disable_msix - disables msix
3017 * @ioc: per adapter object
3018 *
3019 */
3020static void
3021_base_disable_msix(struct MPT3SAS_ADAPTER *ioc)
3022{
3023 if (!ioc->msix_enable)
3024 return;
Suganath Prabu S728bbc62019-05-31 08:14:40 -04003025 pci_free_irq_vectors(ioc->pdev);
Sreekanth Reddyf92363d2012-11-30 07:44:21 +05303026 ioc->msix_enable = 0;
3027}
3028
3029/**
Suganath Prabu S728bbc62019-05-31 08:14:40 -04003030 * _base_alloc_irq_vectors - allocate msix vectors
3031 * @ioc: per adapter object
3032 *
3033 */
3034static int
3035_base_alloc_irq_vectors(struct MPT3SAS_ADAPTER *ioc)
3036{
3037 int i, irq_flags = PCI_IRQ_MSIX;
3038 struct irq_affinity desc = { .pre_vectors = ioc->high_iops_queues };
3039 struct irq_affinity *descp = &desc;
3040
Sreekanth Reddy610ef1e2019-06-24 10:42:55 -04003041 if (ioc->smp_affinity_enable)
Suganath Prabu S728bbc62019-05-31 08:14:40 -04003042 irq_flags |= PCI_IRQ_AFFINITY;
3043 else
3044 descp = NULL;
3045
3046 ioc_info(ioc, " %d %d\n", ioc->high_iops_queues,
3047 ioc->msix_vector_count);
3048
3049 i = pci_alloc_irq_vectors_affinity(ioc->pdev,
3050 ioc->high_iops_queues,
3051 ioc->msix_vector_count, irq_flags, descp);
3052
3053 return i;
3054}
3055
3056/**
Sreekanth Reddyf92363d2012-11-30 07:44:21 +05303057 * _base_enable_msix - enables msix, failback to io_apic
3058 * @ioc: per adapter object
3059 *
3060 */
3061static int
3062_base_enable_msix(struct MPT3SAS_ADAPTER *ioc)
3063{
Sreekanth Reddyf92363d2012-11-30 07:44:21 +05303064 int r;
Suganath Prabu Subramanibb350662016-10-26 13:34:33 +05303065 int i, local_max_msix_vectors;
Sreekanth Reddyf92363d2012-11-30 07:44:21 +05303066 u8 try_msix = 0;
3067
Sreekanth Reddyeedc42a2019-06-24 10:42:56 -04003068 ioc->msix_load_balance = false;
3069
Sreekanth Reddyf92363d2012-11-30 07:44:21 +05303070 if (msix_disable == -1 || msix_disable == 0)
3071 try_msix = 1;
3072
3073 if (!try_msix)
3074 goto try_ioapic;
3075
3076 if (_base_check_enable_msix(ioc) != 0)
3077 goto try_ioapic;
3078
Suganath Prabu S18fd3d82019-05-31 08:14:36 -04003079 ioc_info(ioc, "MSI-X vectors supported: %d\n", ioc->msix_vector_count);
3080 pr_info("\t no of cores: %d, max_msix_vectors: %d\n",
3081 ioc->cpu_count, max_msix_vectors);
Suganath Prabu Sca7e1e92019-05-31 08:14:42 -04003082 if (ioc->is_aero_ioc)
3083 _base_check_and_enable_high_iops_queues(ioc,
3084 ioc->msix_vector_count);
Suganath Prabu S18fd3d82019-05-31 08:14:36 -04003085 ioc->reply_queue_count =
3086 min_t(int, ioc->cpu_count + ioc->high_iops_queues,
3087 ioc->msix_vector_count);
Sreekanth Reddy9c500062013-08-14 18:23:20 +05303088
Sreekanth Reddy9b05c912014-09-12 15:35:31 +05303089 if (!ioc->rdpq_array_enable && max_msix_vectors == -1)
Sreekanth Reddy06f5f972017-10-10 18:41:16 +05303090 local_max_msix_vectors = (reset_devices) ? 1 : 8;
Suganath Prabu Subramanibb350662016-10-26 13:34:33 +05303091 else
3092 local_max_msix_vectors = max_msix_vectors;
Sreekanth Reddy9b05c912014-09-12 15:35:31 +05303093
Hannes Reinecke1d55abc2017-02-22 11:31:41 +01003094 if (local_max_msix_vectors > 0)
Suganath Prabu Subramanibb350662016-10-26 13:34:33 +05303095 ioc->reply_queue_count = min_t(int, local_max_msix_vectors,
Sreekanth Reddy9c500062013-08-14 18:23:20 +05303096 ioc->reply_queue_count);
Hannes Reinecke1d55abc2017-02-22 11:31:41 +01003097 else if (local_max_msix_vectors == 0)
Sreekanth Reddy9b05c912014-09-12 15:35:31 +05303098 goto try_ioapic;
Sreekanth Reddy9c500062013-08-14 18:23:20 +05303099
Sreekanth Reddyeedc42a2019-06-24 10:42:56 -04003100 /*
3101 * Enable msix_load_balance only if combined reply queue mode is
3102 * disabled on SAS3 & above generation HBA devices.
3103 */
3104 if (!ioc->combined_reply_queue &&
3105 ioc->hba_mpi_version_belonged != MPI2_VERSION) {
3106 ioc->msix_load_balance = true;
3107 }
3108
3109 /*
3110 * smp affinity setting is not need when msix load balance
3111 * is enabled.
3112 */
3113 if (ioc->msix_load_balance)
Sreekanth Reddy610ef1e2019-06-24 10:42:55 -04003114 ioc->smp_affinity_enable = 0;
Suganath Prabu Subramani64038302016-02-08 22:13:39 +05303115
Suganath Prabu S728bbc62019-05-31 08:14:40 -04003116 r = _base_alloc_irq_vectors(ioc);
Hannes Reinecke1d55abc2017-02-22 11:31:41 +01003117 if (r < 0) {
Joe Perches919d8a32018-09-17 08:01:09 -07003118 dfailprintk(ioc,
3119 ioc_info(ioc, "pci_alloc_irq_vectors failed (r=%d) !!!\n",
3120 r));
Sreekanth Reddyf92363d2012-11-30 07:44:21 +05303121 goto try_ioapic;
3122 }
3123
3124 ioc->msix_enable = 1;
Hannes Reinecke1d55abc2017-02-22 11:31:41 +01003125 ioc->reply_queue_count = r;
3126 for (i = 0; i < ioc->reply_queue_count; i++) {
3127 r = _base_request_irq(ioc, i);
Sreekanth Reddyf92363d2012-11-30 07:44:21 +05303128 if (r) {
3129 _base_free_irq(ioc);
3130 _base_disable_msix(ioc);
Sreekanth Reddyf92363d2012-11-30 07:44:21 +05303131 goto try_ioapic;
3132 }
3133 }
3134
Suganath Prabu S18fd3d82019-05-31 08:14:36 -04003135 ioc_info(ioc, "High IOPs queues : %s\n",
3136 ioc->high_iops_queues ? "enabled" : "disabled");
3137
Sreekanth Reddyf92363d2012-11-30 07:44:21 +05303138 return 0;
3139
3140/* failback to io_apic interrupt routing */
3141 try_ioapic:
Suganath Prabu S18fd3d82019-05-31 08:14:36 -04003142 ioc->high_iops_queues = 0;
3143 ioc_info(ioc, "High IOPs queues : disabled\n");
Sreekanth Reddy9b05c912014-09-12 15:35:31 +05303144 ioc->reply_queue_count = 1;
Hannes Reinecke1d55abc2017-02-22 11:31:41 +01003145 r = pci_alloc_irq_vectors(ioc->pdev, 1, 1, PCI_IRQ_LEGACY);
3146 if (r < 0) {
Joe Perches919d8a32018-09-17 08:01:09 -07003147 dfailprintk(ioc,
3148 ioc_info(ioc, "pci_alloc_irq_vector(legacy) failed (r=%d) !!!\n",
3149 r));
Hannes Reinecke1d55abc2017-02-22 11:31:41 +01003150 } else
3151 r = _base_request_irq(ioc, 0);
Sreekanth Reddyf92363d2012-11-30 07:44:21 +05303152
3153 return r;
3154}
3155
3156/**
Sreekanth Reddy580d4e32015-06-30 12:24:50 +05303157 * mpt3sas_base_unmap_resources - free controller resources
3158 * @ioc: per adapter object
3159 */
Calvin Owens8bbb1cf2016-07-28 21:38:22 -07003160static void
Sreekanth Reddy580d4e32015-06-30 12:24:50 +05303161mpt3sas_base_unmap_resources(struct MPT3SAS_ADAPTER *ioc)
3162{
3163 struct pci_dev *pdev = ioc->pdev;
3164
Joe Perches1f95a472018-09-17 08:01:13 -07003165 dexitprintk(ioc, ioc_info(ioc, "%s\n", __func__));
Sreekanth Reddy580d4e32015-06-30 12:24:50 +05303166
3167 _base_free_irq(ioc);
3168 _base_disable_msix(ioc);
3169
Chaitra P B2b48be62018-05-31 06:34:51 -04003170 kfree(ioc->replyPostRegisterIndex);
3171 ioc->replyPostRegisterIndex = NULL;
3172
Sreekanth Reddy580d4e32015-06-30 12:24:50 +05303173
3174 if (ioc->chip_phys) {
3175 iounmap(ioc->chip);
3176 ioc->chip_phys = 0;
3177 }
3178
3179 if (pci_is_enabled(pdev)) {
3180 pci_release_selected_regions(ioc->pdev, ioc->bars);
3181 pci_disable_pcie_error_reporting(pdev);
3182 pci_disable_device(pdev);
3183 }
3184}
3185
Suganath Prabu9c067c02019-08-03 09:59:54 -04003186static int
3187_base_diag_reset(struct MPT3SAS_ADAPTER *ioc);
3188
3189/**
3190 * _base_check_for_fault_and_issue_reset - check if IOC is in fault state
3191 * and if it is in fault state then issue diag reset.
3192 * @ioc: per adapter object
3193 *
3194 * Returns: 0 for success, non-zero for failure.
3195 */
3196static int
3197_base_check_for_fault_and_issue_reset(struct MPT3SAS_ADAPTER *ioc)
3198{
3199 u32 ioc_state;
3200 int rc = -EFAULT;
3201
3202 dinitprintk(ioc, pr_info("%s\n", __func__));
3203 if (ioc->pci_error_recovery)
3204 return 0;
3205 ioc_state = mpt3sas_base_get_iocstate(ioc, 0);
3206 dhsprintk(ioc, pr_info("%s: ioc_state(0x%08x)\n", __func__, ioc_state));
3207
3208 if ((ioc_state & MPI2_IOC_STATE_MASK) == MPI2_IOC_STATE_FAULT) {
3209 mpt3sas_base_fault_info(ioc, ioc_state &
3210 MPI2_DOORBELL_DATA_MASK);
3211 rc = _base_diag_reset(ioc);
3212 }
3213
3214 return rc;
3215}
3216
Sreekanth Reddy580d4e32015-06-30 12:24:50 +05303217/**
Sreekanth Reddyf92363d2012-11-30 07:44:21 +05303218 * mpt3sas_base_map_resources - map in controller resources (io/irq/memap)
3219 * @ioc: per adapter object
3220 *
Bart Van Assche4beb4862018-06-15 14:42:01 -07003221 * Return: 0 for success, non-zero for failure.
Sreekanth Reddyf92363d2012-11-30 07:44:21 +05303222 */
3223int
3224mpt3sas_base_map_resources(struct MPT3SAS_ADAPTER *ioc)
3225{
3226 struct pci_dev *pdev = ioc->pdev;
3227 u32 memap_sz;
3228 u32 pio_sz;
Suganath Prabu9c067c02019-08-03 09:59:54 -04003229 int i, r = 0, rc;
Sreekanth Reddyf92363d2012-11-30 07:44:21 +05303230 u64 pio_chip = 0;
Arnd Bergmann6f9e09f2018-03-01 14:07:07 +01003231 phys_addr_t chip_phys = 0;
Sreekanth Reddyf92363d2012-11-30 07:44:21 +05303232 struct adapter_reply_queue *reply_q;
3233
Joe Perches919d8a32018-09-17 08:01:09 -07003234 dinitprintk(ioc, ioc_info(ioc, "%s\n", __func__));
Sreekanth Reddyf92363d2012-11-30 07:44:21 +05303235
3236 ioc->bars = pci_select_bars(pdev, IORESOURCE_MEM);
3237 if (pci_enable_device_mem(pdev)) {
Joe Perches919d8a32018-09-17 08:01:09 -07003238 ioc_warn(ioc, "pci_enable_device_mem: failed\n");
Joe Lawrencecf9bd21a2013-08-08 16:45:39 -04003239 ioc->bars = 0;
Sreekanth Reddyf92363d2012-11-30 07:44:21 +05303240 return -ENODEV;
3241 }
3242
3243
3244 if (pci_request_selected_regions(pdev, ioc->bars,
Sreekanth Reddyc84b06a2015-11-11 17:30:35 +05303245 ioc->driver_name)) {
Joe Perches919d8a32018-09-17 08:01:09 -07003246 ioc_warn(ioc, "pci_request_selected_regions: failed\n");
Joe Lawrencecf9bd21a2013-08-08 16:45:39 -04003247 ioc->bars = 0;
Sreekanth Reddyf92363d2012-11-30 07:44:21 +05303248 r = -ENODEV;
3249 goto out_fail;
3250 }
3251
3252/* AER (Advanced Error Reporting) hooks */
3253 pci_enable_pcie_error_reporting(pdev);
3254
3255 pci_set_master(pdev);
3256
3257
3258 if (_base_config_dma_addressing(ioc, pdev) != 0) {
Joe Perches919d8a32018-09-17 08:01:09 -07003259 ioc_warn(ioc, "no suitable DMA mask for %s\n", pci_name(pdev));
Sreekanth Reddyf92363d2012-11-30 07:44:21 +05303260 r = -ENODEV;
3261 goto out_fail;
3262 }
3263
Sreekanth Reddy5aeeb782015-07-15 10:19:56 +05303264 for (i = 0, memap_sz = 0, pio_sz = 0; (i < DEVICE_COUNT_RESOURCE) &&
3265 (!memap_sz || !pio_sz); i++) {
Sreekanth Reddyf92363d2012-11-30 07:44:21 +05303266 if (pci_resource_flags(pdev, i) & IORESOURCE_IO) {
3267 if (pio_sz)
3268 continue;
3269 pio_chip = (u64)pci_resource_start(pdev, i);
3270 pio_sz = pci_resource_len(pdev, i);
3271 } else if (pci_resource_flags(pdev, i) & IORESOURCE_MEM) {
3272 if (memap_sz)
3273 continue;
3274 ioc->chip_phys = pci_resource_start(pdev, i);
Arnd Bergmann6f9e09f2018-03-01 14:07:07 +01003275 chip_phys = ioc->chip_phys;
Sreekanth Reddyf92363d2012-11-30 07:44:21 +05303276 memap_sz = pci_resource_len(pdev, i);
3277 ioc->chip = ioremap(ioc->chip_phys, memap_sz);
Sreekanth Reddyf92363d2012-11-30 07:44:21 +05303278 }
3279 }
3280
Sreekanth Reddy5aeeb782015-07-15 10:19:56 +05303281 if (ioc->chip == NULL) {
Joe Perches919d8a32018-09-17 08:01:09 -07003282 ioc_err(ioc, "unable to map adapter memory! or resource not found\n");
Sreekanth Reddy5aeeb782015-07-15 10:19:56 +05303283 r = -EINVAL;
3284 goto out_fail;
3285 }
3286
Sreekanth Reddyf92363d2012-11-30 07:44:21 +05303287 _base_mask_interrupts(ioc);
Sreekanth Reddy9b05c912014-09-12 15:35:31 +05303288
Calvin Owens98c56ad2016-07-28 21:38:21 -07003289 r = _base_get_ioc_facts(ioc);
Suganath Prabu9c067c02019-08-03 09:59:54 -04003290 if (r) {
3291 rc = _base_check_for_fault_and_issue_reset(ioc);
3292 if (rc || (_base_get_ioc_facts(ioc)))
3293 goto out_fail;
3294 }
Sreekanth Reddy9b05c912014-09-12 15:35:31 +05303295
3296 if (!ioc->rdpq_array_enable_assigned) {
3297 ioc->rdpq_array_enable = ioc->rdpq_array_capable;
3298 ioc->rdpq_array_enable_assigned = 1;
3299 }
3300
Sreekanth Reddyf92363d2012-11-30 07:44:21 +05303301 r = _base_enable_msix(ioc);
3302 if (r)
3303 goto out_fail;
3304
Suganath Prabu320e77a2019-02-15 02:40:27 -05003305 if (!ioc->is_driver_loading)
3306 _base_init_irqpolls(ioc);
Sreekanth Reddyfb77bb52015-06-30 12:24:47 +05303307 /* Use the Combined reply queue feature only for SAS3 C0 & higher
3308 * revision HBAs and also only when reply queue count is greater than 8
3309 */
Chaitra P B2b48be62018-05-31 06:34:51 -04003310 if (ioc->combined_reply_queue) {
Sreekanth Reddyfb77bb52015-06-30 12:24:47 +05303311 /* Determine the Supplemental Reply Post Host Index Registers
3312 * Addresse. Supplemental Reply Post Host Index Registers
3313 * starts at offset MPI25_SUP_REPLY_POST_HOST_INDEX_OFFSET and
3314 * each register is at offset bytes of
3315 * MPT3_SUP_REPLY_POST_HOST_INDEX_REG_OFFSET from previous one.
3316 */
3317 ioc->replyPostRegisterIndex = kcalloc(
Suganath Prabu Subramani0bb337c2016-10-26 13:34:38 +05303318 ioc->combined_reply_index_count,
Sreekanth Reddyfb77bb52015-06-30 12:24:47 +05303319 sizeof(resource_size_t *), GFP_KERNEL);
3320 if (!ioc->replyPostRegisterIndex) {
Joe Perches1f95a472018-09-17 08:01:13 -07003321 dfailprintk(ioc,
3322 ioc_warn(ioc, "allocation for reply Post Register Index failed!!!\n"));
Sreekanth Reddyfb77bb52015-06-30 12:24:47 +05303323 r = -ENOMEM;
3324 goto out_fail;
3325 }
3326
Suganath Prabu Subramani0bb337c2016-10-26 13:34:38 +05303327 for (i = 0; i < ioc->combined_reply_index_count; i++) {
Sreekanth Reddyfb77bb52015-06-30 12:24:47 +05303328 ioc->replyPostRegisterIndex[i] = (resource_size_t *)
Chaitra P Bcf6bf972018-04-24 05:28:30 -04003329 ((u8 __force *)&ioc->chip->Doorbell +
Sreekanth Reddyfb77bb52015-06-30 12:24:47 +05303330 MPI25_SUP_REPLY_POST_HOST_INDEX_OFFSET +
3331 (i * MPT3_SUP_REPLY_POST_HOST_INDEX_REG_OFFSET));
3332 }
Chaitra P B2b48be62018-05-31 06:34:51 -04003333 }
Sreekanth Reddyfb77bb52015-06-30 12:24:47 +05303334
Greg Edwardsce7c6c92016-07-30 10:06:26 -06003335 if (ioc->is_warpdrive) {
3336 ioc->reply_post_host_index[0] = (resource_size_t __iomem *)
3337 &ioc->chip->ReplyPostHostIndex;
3338
3339 for (i = 1; i < ioc->cpu_msix_table_sz; i++)
3340 ioc->reply_post_host_index[i] =
3341 (resource_size_t __iomem *)
3342 ((u8 __iomem *)&ioc->chip->Doorbell + (0x4000 + ((i - 1)
3343 * 4)));
3344 }
3345
Sreekanth Reddyf92363d2012-11-30 07:44:21 +05303346 list_for_each_entry(reply_q, &ioc->reply_queue_list, list)
Joe Perchesfc7d5102018-09-17 08:01:11 -07003347 pr_info("%s: %s enabled: IRQ %d\n",
3348 reply_q->name,
3349 ioc->msix_enable ? "PCI-MSI-X" : "IO-APIC",
3350 pci_irq_vector(ioc->pdev, reply_q->msix_index));
Sreekanth Reddyf92363d2012-11-30 07:44:21 +05303351
Joe Perches919d8a32018-09-17 08:01:09 -07003352 ioc_info(ioc, "iomem(%pap), mapped(0x%p), size(%d)\n",
3353 &chip_phys, ioc->chip, memap_sz);
3354 ioc_info(ioc, "ioport(0x%016llx), size(%d)\n",
3355 (unsigned long long)pio_chip, pio_sz);
Sreekanth Reddyf92363d2012-11-30 07:44:21 +05303356
3357 /* Save PCI configuration state for recovery from PCI AER/EEH errors */
3358 pci_save_state(pdev);
3359 return 0;
3360
3361 out_fail:
Sreekanth Reddy580d4e32015-06-30 12:24:50 +05303362 mpt3sas_base_unmap_resources(ioc);
Sreekanth Reddyf92363d2012-11-30 07:44:21 +05303363 return r;
3364}
3365
3366/**
3367 * mpt3sas_base_get_msg_frame - obtain request mf pointer
3368 * @ioc: per adapter object
3369 * @smid: system request message index(smid zero is invalid)
3370 *
Bart Van Assche4beb4862018-06-15 14:42:01 -07003371 * Return: virt pointer to message frame.
Sreekanth Reddyf92363d2012-11-30 07:44:21 +05303372 */
3373void *
3374mpt3sas_base_get_msg_frame(struct MPT3SAS_ADAPTER *ioc, u16 smid)
3375{
3376 return (void *)(ioc->request + (smid * ioc->request_sz));
3377}
3378
3379/**
3380 * mpt3sas_base_get_sense_buffer - obtain a sense buffer virt addr
3381 * @ioc: per adapter object
3382 * @smid: system request message index
3383 *
Bart Van Assche4beb4862018-06-15 14:42:01 -07003384 * Return: virt pointer to sense buffer.
Sreekanth Reddyf92363d2012-11-30 07:44:21 +05303385 */
3386void *
3387mpt3sas_base_get_sense_buffer(struct MPT3SAS_ADAPTER *ioc, u16 smid)
3388{
3389 return (void *)(ioc->sense + ((smid - 1) * SCSI_SENSE_BUFFERSIZE));
3390}
3391
3392/**
3393 * mpt3sas_base_get_sense_buffer_dma - obtain a sense buffer dma addr
3394 * @ioc: per adapter object
3395 * @smid: system request message index
3396 *
Bart Van Assche4beb4862018-06-15 14:42:01 -07003397 * Return: phys pointer to the low 32bit address of the sense buffer.
Sreekanth Reddyf92363d2012-11-30 07:44:21 +05303398 */
3399__le32
3400mpt3sas_base_get_sense_buffer_dma(struct MPT3SAS_ADAPTER *ioc, u16 smid)
3401{
3402 return cpu_to_le32(ioc->sense_dma + ((smid - 1) *
3403 SCSI_SENSE_BUFFERSIZE));
3404}
3405
3406/**
Suganath Prabu Subramani016d5c32017-10-31 18:02:28 +05303407 * mpt3sas_base_get_pcie_sgl - obtain a PCIe SGL virt addr
3408 * @ioc: per adapter object
3409 * @smid: system request message index
3410 *
Bart Van Assche4beb4862018-06-15 14:42:01 -07003411 * Return: virt pointer to a PCIe SGL.
Suganath Prabu Subramani016d5c32017-10-31 18:02:28 +05303412 */
3413void *
3414mpt3sas_base_get_pcie_sgl(struct MPT3SAS_ADAPTER *ioc, u16 smid)
3415{
Suganath Prabu Subramanidbec4c902018-01-04 04:57:11 -08003416 return (void *)(ioc->pcie_sg_lookup[smid - 1].pcie_sgl);
Suganath Prabu Subramani016d5c32017-10-31 18:02:28 +05303417}
3418
3419/**
3420 * mpt3sas_base_get_pcie_sgl_dma - obtain a PCIe SGL dma addr
3421 * @ioc: per adapter object
3422 * @smid: system request message index
3423 *
Bart Van Assche4beb4862018-06-15 14:42:01 -07003424 * Return: phys pointer to the address of the PCIe buffer.
Suganath Prabu Subramani016d5c32017-10-31 18:02:28 +05303425 */
Arnd Bergmannd8335ae2017-11-06 14:35:16 +01003426dma_addr_t
Suganath Prabu Subramani016d5c32017-10-31 18:02:28 +05303427mpt3sas_base_get_pcie_sgl_dma(struct MPT3SAS_ADAPTER *ioc, u16 smid)
3428{
Suganath Prabu Subramanidbec4c902018-01-04 04:57:11 -08003429 return ioc->pcie_sg_lookup[smid - 1].pcie_sgl_dma;
Suganath Prabu Subramani016d5c32017-10-31 18:02:28 +05303430}
3431
3432/**
Sreekanth Reddyf92363d2012-11-30 07:44:21 +05303433 * mpt3sas_base_get_reply_virt_addr - obtain reply frames virt address
3434 * @ioc: per adapter object
3435 * @phys_addr: lower 32 physical addr of the reply
3436 *
3437 * Converts 32bit lower physical addr into a virt address.
3438 */
3439void *
3440mpt3sas_base_get_reply_virt_addr(struct MPT3SAS_ADAPTER *ioc, u32 phys_addr)
3441{
3442 if (!phys_addr)
3443 return NULL;
3444 return ioc->reply + (phys_addr - (u32)ioc->reply_dma);
3445}
3446
Suganath Prabu S02136512019-05-31 08:14:37 -04003447/**
3448 * _base_get_msix_index - get the msix index
3449 * @ioc: per adapter object
3450 * @scmd: scsi_cmnd object
3451 *
3452 * returns msix index of general reply queues,
3453 * i.e. reply queue on which IO request's reply
3454 * should be posted by the HBA firmware.
3455 */
Suganath prabu Subramani03d1fb32016-01-28 12:07:06 +05303456static inline u8
Suganath Prabu S02136512019-05-31 08:14:37 -04003457_base_get_msix_index(struct MPT3SAS_ADAPTER *ioc,
3458 struct scsi_cmnd *scmd)
Suganath prabu Subramani03d1fb32016-01-28 12:07:06 +05303459{
Suganath Prabu51e3b2a2019-02-15 02:40:28 -05003460 /* Enables reply_queue load balancing */
3461 if (ioc->msix_load_balance)
3462 return ioc->reply_queue_count ?
3463 base_mod64(atomic64_add_return(1,
3464 &ioc->total_io_cnt), ioc->reply_queue_count) : 0;
3465
Suganath prabu Subramani03d1fb32016-01-28 12:07:06 +05303466 return ioc->cpu_msix_table[raw_smp_processor_id()];
3467}
3468
Sreekanth Reddyf92363d2012-11-30 07:44:21 +05303469/**
Suganath Prabu S5dd48a52019-05-31 08:14:38 -04003470 * _base_get_high_iops_msix_index - get the msix index of
3471 * high iops queues
3472 * @ioc: per adapter object
3473 * @scmd: scsi_cmnd object
3474 *
3475 * Returns: msix index of high iops reply queues.
3476 * i.e. high iops reply queue on which IO request's
3477 * reply should be posted by the HBA firmware.
3478 */
3479static inline u8
3480_base_get_high_iops_msix_index(struct MPT3SAS_ADAPTER *ioc,
3481 struct scsi_cmnd *scmd)
3482{
3483 /**
3484 * Round robin the IO interrupts among the high iops
3485 * reply queues in terms of batch count 16 when outstanding
3486 * IOs on the target device is >=8.
3487 */
3488 if (atomic_read(&scmd->device->device_busy) >
3489 MPT3SAS_DEVICE_HIGH_IOPS_DEPTH)
3490 return base_mod64((
3491 atomic64_add_return(1, &ioc->high_iops_outstanding) /
3492 MPT3SAS_HIGH_IOPS_BATCH_COUNT),
3493 MPT3SAS_HIGH_IOPS_REPLY_QUEUES);
3494
3495 return _base_get_msix_index(ioc, scmd);
3496}
3497
3498/**
Sreekanth Reddyf92363d2012-11-30 07:44:21 +05303499 * mpt3sas_base_get_smid - obtain a free smid from internal queue
3500 * @ioc: per adapter object
3501 * @cb_idx: callback index
3502 *
Bart Van Assche4beb4862018-06-15 14:42:01 -07003503 * Return: smid (zero is invalid)
Sreekanth Reddyf92363d2012-11-30 07:44:21 +05303504 */
3505u16
3506mpt3sas_base_get_smid(struct MPT3SAS_ADAPTER *ioc, u8 cb_idx)
3507{
3508 unsigned long flags;
3509 struct request_tracker *request;
3510 u16 smid;
3511
3512 spin_lock_irqsave(&ioc->scsi_lookup_lock, flags);
3513 if (list_empty(&ioc->internal_free_list)) {
3514 spin_unlock_irqrestore(&ioc->scsi_lookup_lock, flags);
Joe Perches919d8a32018-09-17 08:01:09 -07003515 ioc_err(ioc, "%s: smid not available\n", __func__);
Sreekanth Reddyf92363d2012-11-30 07:44:21 +05303516 return 0;
3517 }
3518
3519 request = list_entry(ioc->internal_free_list.next,
3520 struct request_tracker, tracker_list);
3521 request->cb_idx = cb_idx;
3522 smid = request->smid;
3523 list_del(&request->tracker_list);
3524 spin_unlock_irqrestore(&ioc->scsi_lookup_lock, flags);
3525 return smid;
3526}
3527
3528/**
3529 * mpt3sas_base_get_smid_scsiio - obtain a free smid from scsiio queue
3530 * @ioc: per adapter object
3531 * @cb_idx: callback index
3532 * @scmd: pointer to scsi command object
3533 *
Bart Van Assche4beb4862018-06-15 14:42:01 -07003534 * Return: smid (zero is invalid)
Sreekanth Reddyf92363d2012-11-30 07:44:21 +05303535 */
3536u16
3537mpt3sas_base_get_smid_scsiio(struct MPT3SAS_ADAPTER *ioc, u8 cb_idx,
3538 struct scsi_cmnd *scmd)
3539{
Suganath Prabu Subramanidbec4c902018-01-04 04:57:11 -08003540 struct scsiio_tracker *request = scsi_cmd_priv(scmd);
3541 unsigned int tag = scmd->request->tag;
Sreekanth Reddyf92363d2012-11-30 07:44:21 +05303542 u16 smid;
3543
Suganath Prabu Subramanidbec4c902018-01-04 04:57:11 -08003544 smid = tag + 1;
Sreekanth Reddyf92363d2012-11-30 07:44:21 +05303545 request->cb_idx = cb_idx;
Suganath Prabu Subramanidbec4c902018-01-04 04:57:11 -08003546 request->smid = smid;
Suganath Prabu S998c30012019-05-31 08:14:39 -04003547 request->scmd = scmd;
Suganath Prabu Subramanidbec4c902018-01-04 04:57:11 -08003548 INIT_LIST_HEAD(&request->chain_list);
Sreekanth Reddyf92363d2012-11-30 07:44:21 +05303549 return smid;
3550}
3551
3552/**
3553 * mpt3sas_base_get_smid_hpr - obtain a free smid from hi-priority queue
3554 * @ioc: per adapter object
3555 * @cb_idx: callback index
3556 *
Bart Van Assche4beb4862018-06-15 14:42:01 -07003557 * Return: smid (zero is invalid)
Sreekanth Reddyf92363d2012-11-30 07:44:21 +05303558 */
3559u16
3560mpt3sas_base_get_smid_hpr(struct MPT3SAS_ADAPTER *ioc, u8 cb_idx)
3561{
3562 unsigned long flags;
3563 struct request_tracker *request;
3564 u16 smid;
3565
3566 spin_lock_irqsave(&ioc->scsi_lookup_lock, flags);
3567 if (list_empty(&ioc->hpr_free_list)) {
3568 spin_unlock_irqrestore(&ioc->scsi_lookup_lock, flags);
3569 return 0;
3570 }
3571
3572 request = list_entry(ioc->hpr_free_list.next,
3573 struct request_tracker, tracker_list);
3574 request->cb_idx = cb_idx;
3575 smid = request->smid;
3576 list_del(&request->tracker_list);
3577 spin_unlock_irqrestore(&ioc->scsi_lookup_lock, flags);
3578 return smid;
3579}
3580
Hannes Reinecke6a2d4612018-01-04 04:57:03 -08003581static void
3582_base_recovery_check(struct MPT3SAS_ADAPTER *ioc)
3583{
3584 /*
3585 * See _wait_for_commands_to_complete() call with regards to this code.
3586 */
3587 if (ioc->shost_recovery && ioc->pending_io_count) {
Ming Leic84b0232018-06-24 22:03:26 +08003588 ioc->pending_io_count = scsi_host_busy(ioc->shost);
Hannes Reinecke272e2532018-01-04 04:57:10 -08003589 if (ioc->pending_io_count == 0)
Hannes Reinecke6a2d4612018-01-04 04:57:03 -08003590 wake_up(&ioc->reset_wq);
Hannes Reinecke6a2d4612018-01-04 04:57:03 -08003591 }
3592}
3593
Suganath Prabu Subramanidbec4c902018-01-04 04:57:11 -08003594void mpt3sas_base_clear_st(struct MPT3SAS_ADAPTER *ioc,
3595 struct scsiio_tracker *st)
3596{
3597 if (WARN_ON(st->smid == 0))
3598 return;
3599 st->cb_idx = 0xFF;
3600 st->direct_io = 0;
Suganath Prabu S998c30012019-05-31 08:14:39 -04003601 st->scmd = NULL;
Chaitra P B93204b72018-04-24 05:28:32 -04003602 atomic_set(&ioc->chain_lookup[st->smid - 1].chain_offset, 0);
Sreekanth Reddye7018312018-07-12 12:53:29 -04003603 st->smid = 0;
Suganath Prabu Subramanidbec4c902018-01-04 04:57:11 -08003604}
3605
Sreekanth Reddyf92363d2012-11-30 07:44:21 +05303606/**
3607 * mpt3sas_base_free_smid - put smid back on free_list
3608 * @ioc: per adapter object
3609 * @smid: system request message index
Sreekanth Reddyf92363d2012-11-30 07:44:21 +05303610 */
3611void
3612mpt3sas_base_free_smid(struct MPT3SAS_ADAPTER *ioc, u16 smid)
3613{
3614 unsigned long flags;
3615 int i;
Sreekanth Reddyf92363d2012-11-30 07:44:21 +05303616
Sreekanth Reddyf92363d2012-11-30 07:44:21 +05303617 if (smid < ioc->hi_priority_smid) {
Suganath Prabu Subramanidbec4c902018-01-04 04:57:11 -08003618 struct scsiio_tracker *st;
Sreekanth Reddyc2fe7422019-03-04 07:26:35 -05003619 void *request;
Sreekanth Reddyf92363d2012-11-30 07:44:21 +05303620
Suganath Prabu Subramanidbec4c902018-01-04 04:57:11 -08003621 st = _get_st_from_smid(ioc, smid);
3622 if (!st) {
3623 _base_recovery_check(ioc);
3624 return;
3625 }
Sreekanth Reddyc2fe7422019-03-04 07:26:35 -05003626
3627 /* Clear MPI request frame */
3628 request = mpt3sas_base_get_msg_frame(ioc, smid);
3629 memset(request, 0, ioc->request_sz);
3630
Suganath Prabu Subramanidbec4c902018-01-04 04:57:11 -08003631 mpt3sas_base_clear_st(ioc, st);
Hannes Reinecke6a2d4612018-01-04 04:57:03 -08003632 _base_recovery_check(ioc);
Sreekanth Reddyf92363d2012-11-30 07:44:21 +05303633 return;
Suganath Prabu Subramanidbec4c902018-01-04 04:57:11 -08003634 }
3635
3636 spin_lock_irqsave(&ioc->scsi_lookup_lock, flags);
3637 if (smid < ioc->internal_smid) {
Sreekanth Reddyf92363d2012-11-30 07:44:21 +05303638 /* hi-priority */
3639 i = smid - ioc->hi_priority_smid;
3640 ioc->hpr_lookup[i].cb_idx = 0xFF;
3641 list_add(&ioc->hpr_lookup[i].tracker_list, &ioc->hpr_free_list);
3642 } else if (smid <= ioc->hba_queue_depth) {
3643 /* internal queue */
3644 i = smid - ioc->internal_smid;
3645 ioc->internal_lookup[i].cb_idx = 0xFF;
3646 list_add(&ioc->internal_lookup[i].tracker_list,
3647 &ioc->internal_free_list);
3648 }
3649 spin_unlock_irqrestore(&ioc->scsi_lookup_lock, flags);
3650}
3651
3652/**
Suganath Prabu Subramanie5747432018-02-07 02:51:49 -08003653 * _base_mpi_ep_writeq - 32 bit write to MMIO
3654 * @b: data payload
3655 * @addr: address in MMIO space
3656 * @writeq_lock: spin lock
3657 *
3658 * This special handling for MPI EP to take care of 32 bit
3659 * environment where its not quarenteed to send the entire word
3660 * in one transfer.
3661 */
3662static inline void
3663_base_mpi_ep_writeq(__u64 b, volatile void __iomem *addr,
3664 spinlock_t *writeq_lock)
3665{
3666 unsigned long flags;
Suganath Prabu Subramanie5747432018-02-07 02:51:49 -08003667
3668 spin_lock_irqsave(writeq_lock, flags);
Sreekanth Reddy09c2f952018-07-31 01:36:36 -04003669 __raw_writel((u32)(b), addr);
3670 __raw_writel((u32)(b >> 32), (addr + 4));
Suganath Prabu Subramanie5747432018-02-07 02:51:49 -08003671 spin_unlock_irqrestore(writeq_lock, flags);
3672}
3673
3674/**
Sreekanth Reddyf92363d2012-11-30 07:44:21 +05303675 * _base_writeq - 64 bit write to MMIO
Sreekanth Reddyf92363d2012-11-30 07:44:21 +05303676 * @b: data payload
3677 * @addr: address in MMIO space
3678 * @writeq_lock: spin lock
3679 *
3680 * Glue for handling an atomic 64 bit word to MMIO. This special handling takes
3681 * care of 32 bit environment where its not quarenteed to send the entire word
3682 * in one transfer.
3683 */
3684#if defined(writeq) && defined(CONFIG_64BIT)
3685static inline void
3686_base_writeq(__u64 b, volatile void __iomem *addr, spinlock_t *writeq_lock)
3687{
Stephan Günther23c38282018-12-16 13:08:21 +01003688 wmb();
Sreekanth Reddy09c2f952018-07-31 01:36:36 -04003689 __raw_writeq(b, addr);
Stephan Günther23c38282018-12-16 13:08:21 +01003690 barrier();
Sreekanth Reddyf92363d2012-11-30 07:44:21 +05303691}
3692#else
3693static inline void
3694_base_writeq(__u64 b, volatile void __iomem *addr, spinlock_t *writeq_lock)
3695{
Suganath Prabu Subramanie5747432018-02-07 02:51:49 -08003696 _base_mpi_ep_writeq(b, addr, writeq_lock);
Sreekanth Reddyf92363d2012-11-30 07:44:21 +05303697}
3698#endif
3699
Sreekanth Reddyf92363d2012-11-30 07:44:21 +05303700/**
Suganath Prabu S998c30012019-05-31 08:14:39 -04003701 * _base_set_and_get_msix_index - get the msix index and assign to msix_io
3702 * variable of scsi tracker
3703 * @ioc: per adapter object
3704 * @smid: system request message index
3705 *
3706 * returns msix index.
3707 */
3708static u8
3709_base_set_and_get_msix_index(struct MPT3SAS_ADAPTER *ioc, u16 smid)
3710{
3711 struct scsiio_tracker *st = NULL;
3712
3713 if (smid < ioc->hi_priority_smid)
3714 st = _get_st_from_smid(ioc, smid);
3715
3716 if (st == NULL)
3717 return _base_get_msix_index(ioc, NULL);
3718
3719 st->msix_io = ioc->get_msix_index_for_smlio(ioc, st->scmd);
3720 return st->msix_io;
3721}
3722
3723/**
Suganath Prabu Subramanie5747432018-02-07 02:51:49 -08003724 * _base_put_smid_mpi_ep_scsi_io - send SCSI_IO request to firmware
3725 * @ioc: per adapter object
3726 * @smid: system request message index
3727 * @handle: device handle
Suganath Prabu Subramanie5747432018-02-07 02:51:49 -08003728 */
3729static void
Suganath Prabu S078a4cc2019-05-31 08:14:34 -04003730_base_put_smid_mpi_ep_scsi_io(struct MPT3SAS_ADAPTER *ioc,
3731 u16 smid, u16 handle)
Suganath Prabu Subramanie5747432018-02-07 02:51:49 -08003732{
3733 Mpi2RequestDescriptorUnion_t descriptor;
3734 u64 *request = (u64 *)&descriptor;
3735 void *mpi_req_iomem;
3736 __le32 *mfp = (__le32 *)mpt3sas_base_get_msg_frame(ioc, smid);
3737
3738 _clone_sg_entries(ioc, (void *) mfp, smid);
Chaitra P Bcf6bf972018-04-24 05:28:30 -04003739 mpi_req_iomem = (void __force *)ioc->chip +
Suganath Prabu Subramanie5747432018-02-07 02:51:49 -08003740 MPI_FRAME_START_OFFSET + (smid * ioc->request_sz);
3741 _base_clone_mpi_to_sys_mem(mpi_req_iomem, (void *)mfp,
3742 ioc->request_sz);
3743 descriptor.SCSIIO.RequestFlags = MPI2_REQ_DESCRIPT_FLAGS_SCSI_IO;
Suganath Prabu S998c30012019-05-31 08:14:39 -04003744 descriptor.SCSIIO.MSIxIndex = _base_set_and_get_msix_index(ioc, smid);
Suganath Prabu Subramanie5747432018-02-07 02:51:49 -08003745 descriptor.SCSIIO.SMID = cpu_to_le16(smid);
3746 descriptor.SCSIIO.DevHandle = cpu_to_le16(handle);
3747 descriptor.SCSIIO.LMID = 0;
3748 _base_mpi_ep_writeq(*request, &ioc->chip->RequestDescriptorPostLow,
3749 &ioc->scsi_lookup_lock);
3750}
3751
3752/**
Suganath Prabu Subramani81c16f82016-10-26 13:34:40 +05303753 * _base_put_smid_scsi_io - send SCSI_IO request to firmware
Sreekanth Reddyf92363d2012-11-30 07:44:21 +05303754 * @ioc: per adapter object
3755 * @smid: system request message index
3756 * @handle: device handle
Sreekanth Reddyf92363d2012-11-30 07:44:21 +05303757 */
Suganath Prabu Subramani81c16f82016-10-26 13:34:40 +05303758static void
3759_base_put_smid_scsi_io(struct MPT3SAS_ADAPTER *ioc, u16 smid, u16 handle)
Sreekanth Reddyf92363d2012-11-30 07:44:21 +05303760{
3761 Mpi2RequestDescriptorUnion_t descriptor;
3762 u64 *request = (u64 *)&descriptor;
3763
3764
3765 descriptor.SCSIIO.RequestFlags = MPI2_REQ_DESCRIPT_FLAGS_SCSI_IO;
Suganath Prabu S998c30012019-05-31 08:14:39 -04003766 descriptor.SCSIIO.MSIxIndex = _base_set_and_get_msix_index(ioc, smid);
Sreekanth Reddyf92363d2012-11-30 07:44:21 +05303767 descriptor.SCSIIO.SMID = cpu_to_le16(smid);
3768 descriptor.SCSIIO.DevHandle = cpu_to_le16(handle);
3769 descriptor.SCSIIO.LMID = 0;
3770 _base_writeq(*request, &ioc->chip->RequestDescriptorPostLow,
3771 &ioc->scsi_lookup_lock);
3772}
3773
3774/**
Suganath Prabu S078a4cc2019-05-31 08:14:34 -04003775 * _base_put_smid_fast_path - send fast path request to firmware
Sreekanth Reddyf92363d2012-11-30 07:44:21 +05303776 * @ioc: per adapter object
3777 * @smid: system request message index
3778 * @handle: device handle
Sreekanth Reddyf92363d2012-11-30 07:44:21 +05303779 */
Suganath Prabu S078a4cc2019-05-31 08:14:34 -04003780static void
3781_base_put_smid_fast_path(struct MPT3SAS_ADAPTER *ioc, u16 smid,
Sreekanth Reddyf92363d2012-11-30 07:44:21 +05303782 u16 handle)
3783{
3784 Mpi2RequestDescriptorUnion_t descriptor;
3785 u64 *request = (u64 *)&descriptor;
3786
3787 descriptor.SCSIIO.RequestFlags =
3788 MPI25_REQ_DESCRIPT_FLAGS_FAST_PATH_SCSI_IO;
Suganath Prabu S998c30012019-05-31 08:14:39 -04003789 descriptor.SCSIIO.MSIxIndex = _base_set_and_get_msix_index(ioc, smid);
Sreekanth Reddyf92363d2012-11-30 07:44:21 +05303790 descriptor.SCSIIO.SMID = cpu_to_le16(smid);
3791 descriptor.SCSIIO.DevHandle = cpu_to_le16(handle);
3792 descriptor.SCSIIO.LMID = 0;
3793 _base_writeq(*request, &ioc->chip->RequestDescriptorPostLow,
3794 &ioc->scsi_lookup_lock);
3795}
3796
3797/**
Suganath Prabu S078a4cc2019-05-31 08:14:34 -04003798 * _base_put_smid_hi_priority - send Task Management request to firmware
Sreekanth Reddyf92363d2012-11-30 07:44:21 +05303799 * @ioc: per adapter object
3800 * @smid: system request message index
Suganath prabu Subramani03d1fb32016-01-28 12:07:06 +05303801 * @msix_task: msix_task will be same as msix of IO incase of task abort else 0.
Sreekanth Reddyf92363d2012-11-30 07:44:21 +05303802 */
Suganath Prabu S078a4cc2019-05-31 08:14:34 -04003803static void
3804_base_put_smid_hi_priority(struct MPT3SAS_ADAPTER *ioc, u16 smid,
Suganath prabu Subramani03d1fb32016-01-28 12:07:06 +05303805 u16 msix_task)
Sreekanth Reddyf92363d2012-11-30 07:44:21 +05303806{
3807 Mpi2RequestDescriptorUnion_t descriptor;
Suganath Prabu Subramanie5747432018-02-07 02:51:49 -08003808 void *mpi_req_iomem;
3809 u64 *request;
3810
3811 if (ioc->is_mcpu_endpoint) {
Suganath Prabu Subramanie5747432018-02-07 02:51:49 -08003812 __le32 *mfp = (__le32 *)mpt3sas_base_get_msg_frame(ioc, smid);
3813
Suganath Prabu Subramanie5747432018-02-07 02:51:49 -08003814 /* TBD 256 is offset within sys register. */
Chaitra P Bcf6bf972018-04-24 05:28:30 -04003815 mpi_req_iomem = (void __force *)ioc->chip
3816 + MPI_FRAME_START_OFFSET
Suganath Prabu Subramanie5747432018-02-07 02:51:49 -08003817 + (smid * ioc->request_sz);
3818 _base_clone_mpi_to_sys_mem(mpi_req_iomem, (void *)mfp,
3819 ioc->request_sz);
3820 }
3821
3822 request = (u64 *)&descriptor;
Sreekanth Reddyf92363d2012-11-30 07:44:21 +05303823
3824 descriptor.HighPriority.RequestFlags =
3825 MPI2_REQ_DESCRIPT_FLAGS_HIGH_PRIORITY;
Suganath prabu Subramani03d1fb32016-01-28 12:07:06 +05303826 descriptor.HighPriority.MSIxIndex = msix_task;
Sreekanth Reddyf92363d2012-11-30 07:44:21 +05303827 descriptor.HighPriority.SMID = cpu_to_le16(smid);
3828 descriptor.HighPriority.LMID = 0;
3829 descriptor.HighPriority.Reserved1 = 0;
Suganath Prabu Subramanie5747432018-02-07 02:51:49 -08003830 if (ioc->is_mcpu_endpoint)
3831 _base_mpi_ep_writeq(*request,
3832 &ioc->chip->RequestDescriptorPostLow,
3833 &ioc->scsi_lookup_lock);
3834 else
3835 _base_writeq(*request, &ioc->chip->RequestDescriptorPostLow,
3836 &ioc->scsi_lookup_lock);
Sreekanth Reddyf92363d2012-11-30 07:44:21 +05303837}
3838
3839/**
Suganath Prabu S40114bd2018-02-14 02:16:37 -08003840 * mpt3sas_base_put_smid_nvme_encap - send NVMe encapsulated request to
Suganath Prabu Subramaniaff39e62017-10-31 18:02:29 +05303841 * firmware
3842 * @ioc: per adapter object
3843 * @smid: system request message index
Suganath Prabu Subramaniaff39e62017-10-31 18:02:29 +05303844 */
Suganath Prabu S40114bd2018-02-14 02:16:37 -08003845void
3846mpt3sas_base_put_smid_nvme_encap(struct MPT3SAS_ADAPTER *ioc, u16 smid)
Suganath Prabu Subramaniaff39e62017-10-31 18:02:29 +05303847{
3848 Mpi2RequestDescriptorUnion_t descriptor;
3849 u64 *request = (u64 *)&descriptor;
3850
3851 descriptor.Default.RequestFlags =
3852 MPI26_REQ_DESCRIPT_FLAGS_PCIE_ENCAPSULATED;
Suganath Prabu S998c30012019-05-31 08:14:39 -04003853 descriptor.Default.MSIxIndex = _base_set_and_get_msix_index(ioc, smid);
Suganath Prabu Subramaniaff39e62017-10-31 18:02:29 +05303854 descriptor.Default.SMID = cpu_to_le16(smid);
3855 descriptor.Default.LMID = 0;
3856 descriptor.Default.DescriptorTypeDependent = 0;
3857 _base_writeq(*request, &ioc->chip->RequestDescriptorPostLow,
3858 &ioc->scsi_lookup_lock);
3859}
3860
3861/**
Suganath Prabu S078a4cc2019-05-31 08:14:34 -04003862 * _base_put_smid_default - Default, primarily used for config pages
Sreekanth Reddyf92363d2012-11-30 07:44:21 +05303863 * @ioc: per adapter object
3864 * @smid: system request message index
Sreekanth Reddyf92363d2012-11-30 07:44:21 +05303865 */
Suganath Prabu S078a4cc2019-05-31 08:14:34 -04003866static void
3867_base_put_smid_default(struct MPT3SAS_ADAPTER *ioc, u16 smid)
Sreekanth Reddyf92363d2012-11-30 07:44:21 +05303868{
3869 Mpi2RequestDescriptorUnion_t descriptor;
Suganath Prabu Subramanie5747432018-02-07 02:51:49 -08003870 void *mpi_req_iomem;
3871 u64 *request;
Sreekanth Reddyf92363d2012-11-30 07:44:21 +05303872
Suganath Prabu Subramanie5747432018-02-07 02:51:49 -08003873 if (ioc->is_mcpu_endpoint) {
3874 __le32 *mfp = (__le32 *)mpt3sas_base_get_msg_frame(ioc, smid);
3875
Suganath Prabu Subramanie5747432018-02-07 02:51:49 -08003876 _clone_sg_entries(ioc, (void *) mfp, smid);
3877 /* TBD 256 is offset within sys register */
Chaitra P Bcf6bf972018-04-24 05:28:30 -04003878 mpi_req_iomem = (void __force *)ioc->chip +
Suganath Prabu Subramanie5747432018-02-07 02:51:49 -08003879 MPI_FRAME_START_OFFSET + (smid * ioc->request_sz);
3880 _base_clone_mpi_to_sys_mem(mpi_req_iomem, (void *)mfp,
3881 ioc->request_sz);
3882 }
3883 request = (u64 *)&descriptor;
Sreekanth Reddyf92363d2012-11-30 07:44:21 +05303884 descriptor.Default.RequestFlags = MPI2_REQ_DESCRIPT_FLAGS_DEFAULT_TYPE;
Suganath Prabu S998c30012019-05-31 08:14:39 -04003885 descriptor.Default.MSIxIndex = _base_set_and_get_msix_index(ioc, smid);
Sreekanth Reddyf92363d2012-11-30 07:44:21 +05303886 descriptor.Default.SMID = cpu_to_le16(smid);
3887 descriptor.Default.LMID = 0;
3888 descriptor.Default.DescriptorTypeDependent = 0;
Suganath Prabu Subramanie5747432018-02-07 02:51:49 -08003889 if (ioc->is_mcpu_endpoint)
3890 _base_mpi_ep_writeq(*request,
3891 &ioc->chip->RequestDescriptorPostLow,
3892 &ioc->scsi_lookup_lock);
3893 else
3894 _base_writeq(*request, &ioc->chip->RequestDescriptorPostLow,
3895 &ioc->scsi_lookup_lock);
Sreekanth Reddyf92363d2012-11-30 07:44:21 +05303896}
3897
Sreekanth Reddy1117b312014-09-12 15:35:30 +05303898/**
Suganath Prabu S79c74d02019-05-31 08:14:35 -04003899 * _base_put_smid_scsi_io_atomic - send SCSI_IO request to firmware using
3900 * Atomic Request Descriptor
3901 * @ioc: per adapter object
3902 * @smid: system request message index
3903 * @handle: device handle, unused in this function, for function type match
3904 *
3905 * Return nothing.
3906 */
3907static void
3908_base_put_smid_scsi_io_atomic(struct MPT3SAS_ADAPTER *ioc, u16 smid,
3909 u16 handle)
3910{
3911 Mpi26AtomicRequestDescriptor_t descriptor;
3912 u32 *request = (u32 *)&descriptor;
3913
3914 descriptor.RequestFlags = MPI2_REQ_DESCRIPT_FLAGS_SCSI_IO;
Suganath Prabu S998c30012019-05-31 08:14:39 -04003915 descriptor.MSIxIndex = _base_set_and_get_msix_index(ioc, smid);
Suganath Prabu S79c74d02019-05-31 08:14:35 -04003916 descriptor.SMID = cpu_to_le16(smid);
3917
3918 writel(cpu_to_le32(*request), &ioc->chip->AtomicRequestDescriptorPost);
3919}
3920
3921/**
3922 * _base_put_smid_fast_path_atomic - send fast path request to firmware
3923 * using Atomic Request Descriptor
3924 * @ioc: per adapter object
3925 * @smid: system request message index
3926 * @handle: device handle, unused in this function, for function type match
3927 * Return nothing
3928 */
3929static void
3930_base_put_smid_fast_path_atomic(struct MPT3SAS_ADAPTER *ioc, u16 smid,
3931 u16 handle)
3932{
3933 Mpi26AtomicRequestDescriptor_t descriptor;
3934 u32 *request = (u32 *)&descriptor;
3935
3936 descriptor.RequestFlags = MPI25_REQ_DESCRIPT_FLAGS_FAST_PATH_SCSI_IO;
Suganath Prabu S998c30012019-05-31 08:14:39 -04003937 descriptor.MSIxIndex = _base_set_and_get_msix_index(ioc, smid);
Suganath Prabu S79c74d02019-05-31 08:14:35 -04003938 descriptor.SMID = cpu_to_le16(smid);
3939
3940 writel(cpu_to_le32(*request), &ioc->chip->AtomicRequestDescriptorPost);
3941}
3942
3943/**
3944 * _base_put_smid_hi_priority_atomic - send Task Management request to
3945 * firmware using Atomic Request Descriptor
3946 * @ioc: per adapter object
3947 * @smid: system request message index
3948 * @msix_task: msix_task will be same as msix of IO incase of task abort else 0
3949 *
3950 * Return nothing.
3951 */
3952static void
3953_base_put_smid_hi_priority_atomic(struct MPT3SAS_ADAPTER *ioc, u16 smid,
3954 u16 msix_task)
3955{
3956 Mpi26AtomicRequestDescriptor_t descriptor;
3957 u32 *request = (u32 *)&descriptor;
3958
3959 descriptor.RequestFlags = MPI2_REQ_DESCRIPT_FLAGS_HIGH_PRIORITY;
3960 descriptor.MSIxIndex = msix_task;
3961 descriptor.SMID = cpu_to_le16(smid);
3962
3963 writel(cpu_to_le32(*request), &ioc->chip->AtomicRequestDescriptorPost);
3964}
3965
3966/**
3967 * _base_put_smid_default - Default, primarily used for config pages
3968 * use Atomic Request Descriptor
3969 * @ioc: per adapter object
3970 * @smid: system request message index
3971 *
3972 * Return nothing.
3973 */
3974static void
3975_base_put_smid_default_atomic(struct MPT3SAS_ADAPTER *ioc, u16 smid)
3976{
3977 Mpi26AtomicRequestDescriptor_t descriptor;
3978 u32 *request = (u32 *)&descriptor;
3979
3980 descriptor.RequestFlags = MPI2_REQ_DESCRIPT_FLAGS_DEFAULT_TYPE;
Suganath Prabu S998c30012019-05-31 08:14:39 -04003981 descriptor.MSIxIndex = _base_set_and_get_msix_index(ioc, smid);
Suganath Prabu S79c74d02019-05-31 08:14:35 -04003982 descriptor.SMID = cpu_to_le16(smid);
3983
3984 writel(cpu_to_le32(*request), &ioc->chip->AtomicRequestDescriptorPost);
3985}
3986
3987/**
Sreekanth Reddy989e43c2015-11-11 17:30:32 +05303988 * _base_display_OEMs_branding - Display branding string
Sreekanth Reddy1117b312014-09-12 15:35:30 +05303989 * @ioc: per adapter object
Sreekanth Reddy1117b312014-09-12 15:35:30 +05303990 */
3991static void
Sreekanth Reddy989e43c2015-11-11 17:30:32 +05303992_base_display_OEMs_branding(struct MPT3SAS_ADAPTER *ioc)
Sreekanth Reddy1117b312014-09-12 15:35:30 +05303993{
3994 if (ioc->pdev->subsystem_vendor != PCI_VENDOR_ID_INTEL)
3995 return;
3996
Sreekanth Reddy989e43c2015-11-11 17:30:32 +05303997 switch (ioc->pdev->subsystem_vendor) {
3998 case PCI_VENDOR_ID_INTEL:
3999 switch (ioc->pdev->device) {
4000 case MPI2_MFGPAGE_DEVID_SAS2008:
4001 switch (ioc->pdev->subsystem_device) {
4002 case MPT2SAS_INTEL_RMS2LL080_SSDID:
Joe Perches919d8a32018-09-17 08:01:09 -07004003 ioc_info(ioc, "%s\n",
4004 MPT2SAS_INTEL_RMS2LL080_BRANDING);
Sreekanth Reddy989e43c2015-11-11 17:30:32 +05304005 break;
4006 case MPT2SAS_INTEL_RMS2LL040_SSDID:
Joe Perches919d8a32018-09-17 08:01:09 -07004007 ioc_info(ioc, "%s\n",
4008 MPT2SAS_INTEL_RMS2LL040_BRANDING);
Sreekanth Reddy989e43c2015-11-11 17:30:32 +05304009 break;
4010 case MPT2SAS_INTEL_SSD910_SSDID:
Joe Perches919d8a32018-09-17 08:01:09 -07004011 ioc_info(ioc, "%s\n",
4012 MPT2SAS_INTEL_SSD910_BRANDING);
Sreekanth Reddy989e43c2015-11-11 17:30:32 +05304013 break;
4014 default:
Joe Perches919d8a32018-09-17 08:01:09 -07004015 ioc_info(ioc, "Intel(R) Controller: Subsystem ID: 0x%X\n",
4016 ioc->pdev->subsystem_device);
Sreekanth Reddy989e43c2015-11-11 17:30:32 +05304017 break;
4018 }
Gustavo A. R. Silva7850b512019-02-15 15:54:14 -06004019 break;
Sreekanth Reddy989e43c2015-11-11 17:30:32 +05304020 case MPI2_MFGPAGE_DEVID_SAS2308_2:
4021 switch (ioc->pdev->subsystem_device) {
4022 case MPT2SAS_INTEL_RS25GB008_SSDID:
Joe Perches919d8a32018-09-17 08:01:09 -07004023 ioc_info(ioc, "%s\n",
4024 MPT2SAS_INTEL_RS25GB008_BRANDING);
Sreekanth Reddy989e43c2015-11-11 17:30:32 +05304025 break;
4026 case MPT2SAS_INTEL_RMS25JB080_SSDID:
Joe Perches919d8a32018-09-17 08:01:09 -07004027 ioc_info(ioc, "%s\n",
4028 MPT2SAS_INTEL_RMS25JB080_BRANDING);
Sreekanth Reddy989e43c2015-11-11 17:30:32 +05304029 break;
4030 case MPT2SAS_INTEL_RMS25JB040_SSDID:
Joe Perches919d8a32018-09-17 08:01:09 -07004031 ioc_info(ioc, "%s\n",
4032 MPT2SAS_INTEL_RMS25JB040_BRANDING);
Sreekanth Reddy989e43c2015-11-11 17:30:32 +05304033 break;
4034 case MPT2SAS_INTEL_RMS25KB080_SSDID:
Joe Perches919d8a32018-09-17 08:01:09 -07004035 ioc_info(ioc, "%s\n",
4036 MPT2SAS_INTEL_RMS25KB080_BRANDING);
Sreekanth Reddy989e43c2015-11-11 17:30:32 +05304037 break;
4038 case MPT2SAS_INTEL_RMS25KB040_SSDID:
Joe Perches919d8a32018-09-17 08:01:09 -07004039 ioc_info(ioc, "%s\n",
4040 MPT2SAS_INTEL_RMS25KB040_BRANDING);
Sreekanth Reddy989e43c2015-11-11 17:30:32 +05304041 break;
4042 case MPT2SAS_INTEL_RMS25LB040_SSDID:
Joe Perches919d8a32018-09-17 08:01:09 -07004043 ioc_info(ioc, "%s\n",
4044 MPT2SAS_INTEL_RMS25LB040_BRANDING);
Sreekanth Reddy989e43c2015-11-11 17:30:32 +05304045 break;
4046 case MPT2SAS_INTEL_RMS25LB080_SSDID:
Joe Perches919d8a32018-09-17 08:01:09 -07004047 ioc_info(ioc, "%s\n",
4048 MPT2SAS_INTEL_RMS25LB080_BRANDING);
Sreekanth Reddy989e43c2015-11-11 17:30:32 +05304049 break;
4050 default:
Joe Perches919d8a32018-09-17 08:01:09 -07004051 ioc_info(ioc, "Intel(R) Controller: Subsystem ID: 0x%X\n",
4052 ioc->pdev->subsystem_device);
Sreekanth Reddy989e43c2015-11-11 17:30:32 +05304053 break;
4054 }
Gustavo A. R. Silva7850b512019-02-15 15:54:14 -06004055 break;
Sreekanth Reddy989e43c2015-11-11 17:30:32 +05304056 case MPI25_MFGPAGE_DEVID_SAS3008:
4057 switch (ioc->pdev->subsystem_device) {
4058 case MPT3SAS_INTEL_RMS3JC080_SSDID:
Joe Perches919d8a32018-09-17 08:01:09 -07004059 ioc_info(ioc, "%s\n",
4060 MPT3SAS_INTEL_RMS3JC080_BRANDING);
Sreekanth Reddy989e43c2015-11-11 17:30:32 +05304061 break;
Sreekanth Reddy1117b312014-09-12 15:35:30 +05304062
Sreekanth Reddy989e43c2015-11-11 17:30:32 +05304063 case MPT3SAS_INTEL_RS3GC008_SSDID:
Joe Perches919d8a32018-09-17 08:01:09 -07004064 ioc_info(ioc, "%s\n",
4065 MPT3SAS_INTEL_RS3GC008_BRANDING);
Sreekanth Reddy989e43c2015-11-11 17:30:32 +05304066 break;
4067 case MPT3SAS_INTEL_RS3FC044_SSDID:
Joe Perches919d8a32018-09-17 08:01:09 -07004068 ioc_info(ioc, "%s\n",
4069 MPT3SAS_INTEL_RS3FC044_BRANDING);
Sreekanth Reddy989e43c2015-11-11 17:30:32 +05304070 break;
4071 case MPT3SAS_INTEL_RS3UC080_SSDID:
Joe Perches919d8a32018-09-17 08:01:09 -07004072 ioc_info(ioc, "%s\n",
4073 MPT3SAS_INTEL_RS3UC080_BRANDING);
Sreekanth Reddy989e43c2015-11-11 17:30:32 +05304074 break;
4075 default:
Joe Perches919d8a32018-09-17 08:01:09 -07004076 ioc_info(ioc, "Intel(R) Controller: Subsystem ID: 0x%X\n",
4077 ioc->pdev->subsystem_device);
Sreekanth Reddy989e43c2015-11-11 17:30:32 +05304078 break;
4079 }
Sreekanth Reddy1117b312014-09-12 15:35:30 +05304080 break;
4081 default:
Joe Perches919d8a32018-09-17 08:01:09 -07004082 ioc_info(ioc, "Intel(R) Controller: Subsystem ID: 0x%X\n",
4083 ioc->pdev->subsystem_device);
Sreekanth Reddyd8eb4a42015-06-30 12:25:02 +05304084 break;
4085 }
4086 break;
Sreekanth Reddy989e43c2015-11-11 17:30:32 +05304087 case PCI_VENDOR_ID_DELL:
4088 switch (ioc->pdev->device) {
4089 case MPI2_MFGPAGE_DEVID_SAS2008:
4090 switch (ioc->pdev->subsystem_device) {
4091 case MPT2SAS_DELL_6GBPS_SAS_HBA_SSDID:
Joe Perches919d8a32018-09-17 08:01:09 -07004092 ioc_info(ioc, "%s\n",
4093 MPT2SAS_DELL_6GBPS_SAS_HBA_BRANDING);
Sreekanth Reddy989e43c2015-11-11 17:30:32 +05304094 break;
4095 case MPT2SAS_DELL_PERC_H200_ADAPTER_SSDID:
Joe Perches919d8a32018-09-17 08:01:09 -07004096 ioc_info(ioc, "%s\n",
4097 MPT2SAS_DELL_PERC_H200_ADAPTER_BRANDING);
Sreekanth Reddy989e43c2015-11-11 17:30:32 +05304098 break;
4099 case MPT2SAS_DELL_PERC_H200_INTEGRATED_SSDID:
Joe Perches919d8a32018-09-17 08:01:09 -07004100 ioc_info(ioc, "%s\n",
4101 MPT2SAS_DELL_PERC_H200_INTEGRATED_BRANDING);
Sreekanth Reddy989e43c2015-11-11 17:30:32 +05304102 break;
4103 case MPT2SAS_DELL_PERC_H200_MODULAR_SSDID:
Joe Perches919d8a32018-09-17 08:01:09 -07004104 ioc_info(ioc, "%s\n",
4105 MPT2SAS_DELL_PERC_H200_MODULAR_BRANDING);
Sreekanth Reddy989e43c2015-11-11 17:30:32 +05304106 break;
4107 case MPT2SAS_DELL_PERC_H200_EMBEDDED_SSDID:
Joe Perches919d8a32018-09-17 08:01:09 -07004108 ioc_info(ioc, "%s\n",
4109 MPT2SAS_DELL_PERC_H200_EMBEDDED_BRANDING);
Sreekanth Reddy989e43c2015-11-11 17:30:32 +05304110 break;
4111 case MPT2SAS_DELL_PERC_H200_SSDID:
Joe Perches919d8a32018-09-17 08:01:09 -07004112 ioc_info(ioc, "%s\n",
4113 MPT2SAS_DELL_PERC_H200_BRANDING);
Sreekanth Reddy989e43c2015-11-11 17:30:32 +05304114 break;
4115 case MPT2SAS_DELL_6GBPS_SAS_SSDID:
Joe Perches919d8a32018-09-17 08:01:09 -07004116 ioc_info(ioc, "%s\n",
4117 MPT2SAS_DELL_6GBPS_SAS_BRANDING);
Sreekanth Reddy989e43c2015-11-11 17:30:32 +05304118 break;
4119 default:
Joe Perches919d8a32018-09-17 08:01:09 -07004120 ioc_info(ioc, "Dell 6Gbps HBA: Subsystem ID: 0x%X\n",
4121 ioc->pdev->subsystem_device);
Sreekanth Reddy989e43c2015-11-11 17:30:32 +05304122 break;
4123 }
4124 break;
4125 case MPI25_MFGPAGE_DEVID_SAS3008:
4126 switch (ioc->pdev->subsystem_device) {
4127 case MPT3SAS_DELL_12G_HBA_SSDID:
Joe Perches919d8a32018-09-17 08:01:09 -07004128 ioc_info(ioc, "%s\n",
4129 MPT3SAS_DELL_12G_HBA_BRANDING);
Sreekanth Reddy989e43c2015-11-11 17:30:32 +05304130 break;
4131 default:
Joe Perches919d8a32018-09-17 08:01:09 -07004132 ioc_info(ioc, "Dell 12Gbps HBA: Subsystem ID: 0x%X\n",
4133 ioc->pdev->subsystem_device);
Sreekanth Reddy989e43c2015-11-11 17:30:32 +05304134 break;
4135 }
4136 break;
4137 default:
Joe Perches919d8a32018-09-17 08:01:09 -07004138 ioc_info(ioc, "Dell HBA: Subsystem ID: 0x%X\n",
4139 ioc->pdev->subsystem_device);
Sreekanth Reddy989e43c2015-11-11 17:30:32 +05304140 break;
4141 }
4142 break;
4143 case PCI_VENDOR_ID_CISCO:
4144 switch (ioc->pdev->device) {
4145 case MPI25_MFGPAGE_DEVID_SAS3008:
4146 switch (ioc->pdev->subsystem_device) {
4147 case MPT3SAS_CISCO_12G_8E_HBA_SSDID:
Joe Perches919d8a32018-09-17 08:01:09 -07004148 ioc_info(ioc, "%s\n",
4149 MPT3SAS_CISCO_12G_8E_HBA_BRANDING);
Sreekanth Reddy989e43c2015-11-11 17:30:32 +05304150 break;
4151 case MPT3SAS_CISCO_12G_8I_HBA_SSDID:
Joe Perches919d8a32018-09-17 08:01:09 -07004152 ioc_info(ioc, "%s\n",
4153 MPT3SAS_CISCO_12G_8I_HBA_BRANDING);
Sreekanth Reddy989e43c2015-11-11 17:30:32 +05304154 break;
4155 case MPT3SAS_CISCO_12G_AVILA_HBA_SSDID:
Joe Perches919d8a32018-09-17 08:01:09 -07004156 ioc_info(ioc, "%s\n",
4157 MPT3SAS_CISCO_12G_AVILA_HBA_BRANDING);
Sreekanth Reddy989e43c2015-11-11 17:30:32 +05304158 break;
4159 default:
Joe Perches919d8a32018-09-17 08:01:09 -07004160 ioc_info(ioc, "Cisco 12Gbps SAS HBA: Subsystem ID: 0x%X\n",
4161 ioc->pdev->subsystem_device);
Sreekanth Reddy989e43c2015-11-11 17:30:32 +05304162 break;
4163 }
4164 break;
4165 case MPI25_MFGPAGE_DEVID_SAS3108_1:
4166 switch (ioc->pdev->subsystem_device) {
4167 case MPT3SAS_CISCO_12G_AVILA_HBA_SSDID:
Joe Perches919d8a32018-09-17 08:01:09 -07004168 ioc_info(ioc, "%s\n",
4169 MPT3SAS_CISCO_12G_AVILA_HBA_BRANDING);
Sreekanth Reddy989e43c2015-11-11 17:30:32 +05304170 break;
4171 case MPT3SAS_CISCO_12G_COLUSA_MEZZANINE_HBA_SSDID:
Joe Perches919d8a32018-09-17 08:01:09 -07004172 ioc_info(ioc, "%s\n",
4173 MPT3SAS_CISCO_12G_COLUSA_MEZZANINE_HBA_BRANDING);
Sreekanth Reddy989e43c2015-11-11 17:30:32 +05304174 break;
4175 default:
Joe Perches919d8a32018-09-17 08:01:09 -07004176 ioc_info(ioc, "Cisco 12Gbps SAS HBA: Subsystem ID: 0x%X\n",
4177 ioc->pdev->subsystem_device);
Sreekanth Reddy989e43c2015-11-11 17:30:32 +05304178 break;
4179 }
4180 break;
4181 default:
Joe Perches919d8a32018-09-17 08:01:09 -07004182 ioc_info(ioc, "Cisco SAS HBA: Subsystem ID: 0x%X\n",
4183 ioc->pdev->subsystem_device);
Sreekanth Reddy989e43c2015-11-11 17:30:32 +05304184 break;
4185 }
4186 break;
4187 case MPT2SAS_HP_3PAR_SSVID:
4188 switch (ioc->pdev->device) {
4189 case MPI2_MFGPAGE_DEVID_SAS2004:
4190 switch (ioc->pdev->subsystem_device) {
4191 case MPT2SAS_HP_DAUGHTER_2_4_INTERNAL_SSDID:
Joe Perches919d8a32018-09-17 08:01:09 -07004192 ioc_info(ioc, "%s\n",
4193 MPT2SAS_HP_DAUGHTER_2_4_INTERNAL_BRANDING);
Sreekanth Reddy989e43c2015-11-11 17:30:32 +05304194 break;
4195 default:
Joe Perches919d8a32018-09-17 08:01:09 -07004196 ioc_info(ioc, "HP 6Gbps SAS HBA: Subsystem ID: 0x%X\n",
4197 ioc->pdev->subsystem_device);
Sreekanth Reddy989e43c2015-11-11 17:30:32 +05304198 break;
4199 }
Gustavo A. R. Silva7850b512019-02-15 15:54:14 -06004200 break;
Sreekanth Reddy989e43c2015-11-11 17:30:32 +05304201 case MPI2_MFGPAGE_DEVID_SAS2308_2:
4202 switch (ioc->pdev->subsystem_device) {
4203 case MPT2SAS_HP_2_4_INTERNAL_SSDID:
Joe Perches919d8a32018-09-17 08:01:09 -07004204 ioc_info(ioc, "%s\n",
4205 MPT2SAS_HP_2_4_INTERNAL_BRANDING);
Sreekanth Reddy989e43c2015-11-11 17:30:32 +05304206 break;
4207 case MPT2SAS_HP_2_4_EXTERNAL_SSDID:
Joe Perches919d8a32018-09-17 08:01:09 -07004208 ioc_info(ioc, "%s\n",
4209 MPT2SAS_HP_2_4_EXTERNAL_BRANDING);
Sreekanth Reddy989e43c2015-11-11 17:30:32 +05304210 break;
4211 case MPT2SAS_HP_1_4_INTERNAL_1_4_EXTERNAL_SSDID:
Joe Perches919d8a32018-09-17 08:01:09 -07004212 ioc_info(ioc, "%s\n",
4213 MPT2SAS_HP_1_4_INTERNAL_1_4_EXTERNAL_BRANDING);
Sreekanth Reddy989e43c2015-11-11 17:30:32 +05304214 break;
4215 case MPT2SAS_HP_EMBEDDED_2_4_INTERNAL_SSDID:
Joe Perches919d8a32018-09-17 08:01:09 -07004216 ioc_info(ioc, "%s\n",
4217 MPT2SAS_HP_EMBEDDED_2_4_INTERNAL_BRANDING);
Sreekanth Reddy989e43c2015-11-11 17:30:32 +05304218 break;
4219 default:
Joe Perches919d8a32018-09-17 08:01:09 -07004220 ioc_info(ioc, "HP 6Gbps SAS HBA: Subsystem ID: 0x%X\n",
4221 ioc->pdev->subsystem_device);
Sreekanth Reddy989e43c2015-11-11 17:30:32 +05304222 break;
4223 }
Gustavo A. R. Silva7850b512019-02-15 15:54:14 -06004224 break;
Sreekanth Reddy989e43c2015-11-11 17:30:32 +05304225 default:
Joe Perches919d8a32018-09-17 08:01:09 -07004226 ioc_info(ioc, "HP SAS HBA: Subsystem ID: 0x%X\n",
4227 ioc->pdev->subsystem_device);
Sreekanth Reddy989e43c2015-11-11 17:30:32 +05304228 break;
4229 }
Sreekanth Reddy38e41412015-06-30 12:24:57 +05304230 default:
Sreekanth Reddy38e41412015-06-30 12:24:57 +05304231 break;
4232 }
4233}
Sreekanth Reddyfb84dfc2015-06-30 12:24:56 +05304234
4235/**
Chaitra P B3d29ed82018-04-24 05:28:39 -04004236 * _base_display_fwpkg_version - sends FWUpload request to pull FWPkg
4237 * version from FW Image Header.
4238 * @ioc: per adapter object
4239 *
Bart Van Assche4beb4862018-06-15 14:42:01 -07004240 * Return: 0 for success, non-zero for failure.
Chaitra P B3d29ed82018-04-24 05:28:39 -04004241 */
4242 static int
4243_base_display_fwpkg_version(struct MPT3SAS_ADAPTER *ioc)
4244{
4245 Mpi2FWImageHeader_t *FWImgHdr;
4246 Mpi25FWUploadRequest_t *mpi_request;
4247 Mpi2FWUploadReply_t mpi_reply;
4248 int r = 0;
4249 void *fwpkg_data = NULL;
4250 dma_addr_t fwpkg_data_dma;
4251 u16 smid, ioc_status;
4252 size_t data_length;
4253
Joe Perches919d8a32018-09-17 08:01:09 -07004254 dinitprintk(ioc, ioc_info(ioc, "%s\n", __func__));
Chaitra P B3d29ed82018-04-24 05:28:39 -04004255
4256 if (ioc->base_cmds.status & MPT3_CMD_PENDING) {
Joe Perches919d8a32018-09-17 08:01:09 -07004257 ioc_err(ioc, "%s: internal command already in use\n", __func__);
Chaitra P B3d29ed82018-04-24 05:28:39 -04004258 return -EAGAIN;
4259 }
4260
4261 data_length = sizeof(Mpi2FWImageHeader_t);
Christoph Hellwig1c2048b2018-10-11 09:35:25 +02004262 fwpkg_data = dma_alloc_coherent(&ioc->pdev->dev, data_length,
4263 &fwpkg_data_dma, GFP_KERNEL);
Chaitra P B3d29ed82018-04-24 05:28:39 -04004264 if (!fwpkg_data) {
Joe Perches919d8a32018-09-17 08:01:09 -07004265 ioc_err(ioc, "failure at %s:%d/%s()!\n",
4266 __FILE__, __LINE__, __func__);
Chaitra P B3d29ed82018-04-24 05:28:39 -04004267 return -ENOMEM;
4268 }
4269
4270 smid = mpt3sas_base_get_smid(ioc, ioc->base_cb_idx);
4271 if (!smid) {
Joe Perches919d8a32018-09-17 08:01:09 -07004272 ioc_err(ioc, "%s: failed obtaining a smid\n", __func__);
Chaitra P B3d29ed82018-04-24 05:28:39 -04004273 r = -EAGAIN;
4274 goto out;
4275 }
4276
4277 ioc->base_cmds.status = MPT3_CMD_PENDING;
4278 mpi_request = mpt3sas_base_get_msg_frame(ioc, smid);
4279 ioc->base_cmds.smid = smid;
4280 memset(mpi_request, 0, sizeof(Mpi25FWUploadRequest_t));
4281 mpi_request->Function = MPI2_FUNCTION_FW_UPLOAD;
4282 mpi_request->ImageType = MPI2_FW_UPLOAD_ITYPE_FW_FLASH;
4283 mpi_request->ImageSize = cpu_to_le32(data_length);
4284 ioc->build_sg(ioc, &mpi_request->SGL, 0, 0, fwpkg_data_dma,
4285 data_length);
4286 init_completion(&ioc->base_cmds.done);
Suganath Prabu S078a4cc2019-05-31 08:14:34 -04004287 ioc->put_smid_default(ioc, smid);
Chaitra P B3d29ed82018-04-24 05:28:39 -04004288 /* Wait for 15 seconds */
4289 wait_for_completion_timeout(&ioc->base_cmds.done,
4290 FW_IMG_HDR_READ_TIMEOUT*HZ);
Joe Perches919d8a32018-09-17 08:01:09 -07004291 ioc_info(ioc, "%s: complete\n", __func__);
Chaitra P B3d29ed82018-04-24 05:28:39 -04004292 if (!(ioc->base_cmds.status & MPT3_CMD_COMPLETE)) {
Joe Perches919d8a32018-09-17 08:01:09 -07004293 ioc_err(ioc, "%s: timeout\n", __func__);
Chaitra P B3d29ed82018-04-24 05:28:39 -04004294 _debug_dump_mf(mpi_request,
4295 sizeof(Mpi25FWUploadRequest_t)/4);
4296 r = -ETIME;
4297 } else {
4298 memset(&mpi_reply, 0, sizeof(Mpi2FWUploadReply_t));
4299 if (ioc->base_cmds.status & MPT3_CMD_REPLY_VALID) {
4300 memcpy(&mpi_reply, ioc->base_cmds.reply,
4301 sizeof(Mpi2FWUploadReply_t));
4302 ioc_status = le16_to_cpu(mpi_reply.IOCStatus) &
4303 MPI2_IOCSTATUS_MASK;
4304 if (ioc_status == MPI2_IOCSTATUS_SUCCESS) {
4305 FWImgHdr = (Mpi2FWImageHeader_t *)fwpkg_data;
4306 if (FWImgHdr->PackageVersion.Word) {
Joe Perches919d8a32018-09-17 08:01:09 -07004307 ioc_info(ioc, "FW Package Version (%02d.%02d.%02d.%02d)\n",
4308 FWImgHdr->PackageVersion.Struct.Major,
4309 FWImgHdr->PackageVersion.Struct.Minor,
4310 FWImgHdr->PackageVersion.Struct.Unit,
4311 FWImgHdr->PackageVersion.Struct.Dev);
Chaitra P B3d29ed82018-04-24 05:28:39 -04004312 }
4313 } else {
4314 _debug_dump_mf(&mpi_reply,
4315 sizeof(Mpi2FWUploadReply_t)/4);
4316 }
4317 }
4318 }
4319 ioc->base_cmds.status = MPT3_CMD_NOT_USED;
4320out:
4321 if (fwpkg_data)
Christoph Hellwig1c2048b2018-10-11 09:35:25 +02004322 dma_free_coherent(&ioc->pdev->dev, data_length, fwpkg_data,
Chaitra P B3d29ed82018-04-24 05:28:39 -04004323 fwpkg_data_dma);
4324 return r;
4325}
4326
4327/**
Sreekanth Reddyf92363d2012-11-30 07:44:21 +05304328 * _base_display_ioc_capabilities - Disply IOC's capabilities.
4329 * @ioc: per adapter object
Sreekanth Reddyf92363d2012-11-30 07:44:21 +05304330 */
4331static void
4332_base_display_ioc_capabilities(struct MPT3SAS_ADAPTER *ioc)
4333{
4334 int i = 0;
4335 char desc[16];
4336 u32 iounit_pg1_flags;
4337 u32 bios_version;
4338
4339 bios_version = le32_to_cpu(ioc->bios_pg3.BiosVersion);
4340 strncpy(desc, ioc->manu_pg0.ChipName, 16);
Joe Perches919d8a32018-09-17 08:01:09 -07004341 ioc_info(ioc, "%s: FWVersion(%02d.%02d.%02d.%02d), ChipRevision(0x%02x), BiosVersion(%02d.%02d.%02d.%02d)\n",
4342 desc,
4343 (ioc->facts.FWVersion.Word & 0xFF000000) >> 24,
4344 (ioc->facts.FWVersion.Word & 0x00FF0000) >> 16,
4345 (ioc->facts.FWVersion.Word & 0x0000FF00) >> 8,
4346 ioc->facts.FWVersion.Word & 0x000000FF,
4347 ioc->pdev->revision,
4348 (bios_version & 0xFF000000) >> 24,
4349 (bios_version & 0x00FF0000) >> 16,
4350 (bios_version & 0x0000FF00) >> 8,
4351 bios_version & 0x000000FF);
Sreekanth Reddyf92363d2012-11-30 07:44:21 +05304352
Sreekanth Reddy989e43c2015-11-11 17:30:32 +05304353 _base_display_OEMs_branding(ioc);
Sreekanth Reddy1117b312014-09-12 15:35:30 +05304354
Suganath Prabu Subramani016d5c32017-10-31 18:02:28 +05304355 if (ioc->facts.ProtocolFlags & MPI2_IOCFACTS_PROTOCOL_NVME_DEVICES) {
4356 pr_info("%sNVMe", i ? "," : "");
4357 i++;
4358 }
4359
Joe Perches919d8a32018-09-17 08:01:09 -07004360 ioc_info(ioc, "Protocol=(");
Sreekanth Reddyf92363d2012-11-30 07:44:21 +05304361
4362 if (ioc->facts.ProtocolFlags & MPI2_IOCFACTS_PROTOCOL_SCSI_INITIATOR) {
Joe Perches919d8a32018-09-17 08:01:09 -07004363 pr_cont("Initiator");
Sreekanth Reddyf92363d2012-11-30 07:44:21 +05304364 i++;
4365 }
4366
4367 if (ioc->facts.ProtocolFlags & MPI2_IOCFACTS_PROTOCOL_SCSI_TARGET) {
Joe Perches919d8a32018-09-17 08:01:09 -07004368 pr_cont("%sTarget", i ? "," : "");
Sreekanth Reddyf92363d2012-11-30 07:44:21 +05304369 i++;
4370 }
4371
4372 i = 0;
Joe Perches919d8a32018-09-17 08:01:09 -07004373 pr_cont("), Capabilities=(");
Sreekanth Reddyf92363d2012-11-30 07:44:21 +05304374
Sreekanth Reddy7786ab62015-11-11 17:30:28 +05304375 if (!ioc->hide_ir_msg) {
4376 if (ioc->facts.IOCCapabilities &
Sreekanth Reddyf92363d2012-11-30 07:44:21 +05304377 MPI2_IOCFACTS_CAPABILITY_INTEGRATED_RAID) {
Joe Perches919d8a32018-09-17 08:01:09 -07004378 pr_cont("Raid");
Sreekanth Reddyf92363d2012-11-30 07:44:21 +05304379 i++;
Sreekanth Reddy7786ab62015-11-11 17:30:28 +05304380 }
Sreekanth Reddyf92363d2012-11-30 07:44:21 +05304381 }
4382
4383 if (ioc->facts.IOCCapabilities & MPI2_IOCFACTS_CAPABILITY_TLR) {
Joe Perches919d8a32018-09-17 08:01:09 -07004384 pr_cont("%sTLR", i ? "," : "");
Sreekanth Reddyf92363d2012-11-30 07:44:21 +05304385 i++;
4386 }
4387
4388 if (ioc->facts.IOCCapabilities & MPI2_IOCFACTS_CAPABILITY_MULTICAST) {
Joe Perches919d8a32018-09-17 08:01:09 -07004389 pr_cont("%sMulticast", i ? "," : "");
Sreekanth Reddyf92363d2012-11-30 07:44:21 +05304390 i++;
4391 }
4392
4393 if (ioc->facts.IOCCapabilities &
4394 MPI2_IOCFACTS_CAPABILITY_BIDIRECTIONAL_TARGET) {
Joe Perches919d8a32018-09-17 08:01:09 -07004395 pr_cont("%sBIDI Target", i ? "," : "");
Sreekanth Reddyf92363d2012-11-30 07:44:21 +05304396 i++;
4397 }
4398
4399 if (ioc->facts.IOCCapabilities & MPI2_IOCFACTS_CAPABILITY_EEDP) {
Joe Perches919d8a32018-09-17 08:01:09 -07004400 pr_cont("%sEEDP", i ? "," : "");
Sreekanth Reddyf92363d2012-11-30 07:44:21 +05304401 i++;
4402 }
4403
4404 if (ioc->facts.IOCCapabilities &
4405 MPI2_IOCFACTS_CAPABILITY_SNAPSHOT_BUFFER) {
Joe Perches919d8a32018-09-17 08:01:09 -07004406 pr_cont("%sSnapshot Buffer", i ? "," : "");
Sreekanth Reddyf92363d2012-11-30 07:44:21 +05304407 i++;
4408 }
4409
4410 if (ioc->facts.IOCCapabilities &
4411 MPI2_IOCFACTS_CAPABILITY_DIAG_TRACE_BUFFER) {
Joe Perches919d8a32018-09-17 08:01:09 -07004412 pr_cont("%sDiag Trace Buffer", i ? "," : "");
Sreekanth Reddyf92363d2012-11-30 07:44:21 +05304413 i++;
4414 }
4415
4416 if (ioc->facts.IOCCapabilities &
4417 MPI2_IOCFACTS_CAPABILITY_EXTENDED_BUFFER) {
Joe Perches919d8a32018-09-17 08:01:09 -07004418 pr_cont("%sDiag Extended Buffer", i ? "," : "");
Sreekanth Reddyf92363d2012-11-30 07:44:21 +05304419 i++;
4420 }
4421
4422 if (ioc->facts.IOCCapabilities &
4423 MPI2_IOCFACTS_CAPABILITY_TASK_SET_FULL_HANDLING) {
Joe Perches919d8a32018-09-17 08:01:09 -07004424 pr_cont("%sTask Set Full", i ? "," : "");
Sreekanth Reddyf92363d2012-11-30 07:44:21 +05304425 i++;
4426 }
4427
4428 iounit_pg1_flags = le32_to_cpu(ioc->iounit_pg1.Flags);
4429 if (!(iounit_pg1_flags & MPI2_IOUNITPAGE1_NATIVE_COMMAND_Q_DISABLE)) {
Joe Perches919d8a32018-09-17 08:01:09 -07004430 pr_cont("%sNCQ", i ? "," : "");
Sreekanth Reddyf92363d2012-11-30 07:44:21 +05304431 i++;
4432 }
4433
Joe Perches919d8a32018-09-17 08:01:09 -07004434 pr_cont(")\n");
Sreekanth Reddyf92363d2012-11-30 07:44:21 +05304435}
4436
4437/**
4438 * mpt3sas_base_update_missing_delay - change the missing delay timers
4439 * @ioc: per adapter object
4440 * @device_missing_delay: amount of time till device is reported missing
4441 * @io_missing_delay: interval IO is returned when there is a missing device
4442 *
Sreekanth Reddyf92363d2012-11-30 07:44:21 +05304443 * Passed on the command line, this function will modify the device missing
4444 * delay, as well as the io missing delay. This should be called at driver
4445 * load time.
4446 */
4447void
4448mpt3sas_base_update_missing_delay(struct MPT3SAS_ADAPTER *ioc,
4449 u16 device_missing_delay, u8 io_missing_delay)
4450{
4451 u16 dmd, dmd_new, dmd_orignal;
4452 u8 io_missing_delay_original;
4453 u16 sz;
4454 Mpi2SasIOUnitPage1_t *sas_iounit_pg1 = NULL;
4455 Mpi2ConfigReply_t mpi_reply;
4456 u8 num_phys = 0;
4457 u16 ioc_status;
4458
4459 mpt3sas_config_get_number_hba_phys(ioc, &num_phys);
4460 if (!num_phys)
4461 return;
4462
4463 sz = offsetof(Mpi2SasIOUnitPage1_t, PhyData) + (num_phys *
4464 sizeof(Mpi2SasIOUnit1PhyData_t));
4465 sas_iounit_pg1 = kzalloc(sz, GFP_KERNEL);
4466 if (!sas_iounit_pg1) {
Joe Perches919d8a32018-09-17 08:01:09 -07004467 ioc_err(ioc, "failure at %s:%d/%s()!\n",
4468 __FILE__, __LINE__, __func__);
Sreekanth Reddyf92363d2012-11-30 07:44:21 +05304469 goto out;
4470 }
4471 if ((mpt3sas_config_get_sas_iounit_pg1(ioc, &mpi_reply,
4472 sas_iounit_pg1, sz))) {
Joe Perches919d8a32018-09-17 08:01:09 -07004473 ioc_err(ioc, "failure at %s:%d/%s()!\n",
4474 __FILE__, __LINE__, __func__);
Sreekanth Reddyf92363d2012-11-30 07:44:21 +05304475 goto out;
4476 }
4477 ioc_status = le16_to_cpu(mpi_reply.IOCStatus) &
4478 MPI2_IOCSTATUS_MASK;
4479 if (ioc_status != MPI2_IOCSTATUS_SUCCESS) {
Joe Perches919d8a32018-09-17 08:01:09 -07004480 ioc_err(ioc, "failure at %s:%d/%s()!\n",
4481 __FILE__, __LINE__, __func__);
Sreekanth Reddyf92363d2012-11-30 07:44:21 +05304482 goto out;
4483 }
4484
4485 /* device missing delay */
4486 dmd = sas_iounit_pg1->ReportDeviceMissingDelay;
4487 if (dmd & MPI2_SASIOUNIT1_REPORT_MISSING_UNIT_16)
4488 dmd = (dmd & MPI2_SASIOUNIT1_REPORT_MISSING_TIMEOUT_MASK) * 16;
4489 else
4490 dmd = dmd & MPI2_SASIOUNIT1_REPORT_MISSING_TIMEOUT_MASK;
4491 dmd_orignal = dmd;
4492 if (device_missing_delay > 0x7F) {
4493 dmd = (device_missing_delay > 0x7F0) ? 0x7F0 :
4494 device_missing_delay;
4495 dmd = dmd / 16;
4496 dmd |= MPI2_SASIOUNIT1_REPORT_MISSING_UNIT_16;
4497 } else
4498 dmd = device_missing_delay;
4499 sas_iounit_pg1->ReportDeviceMissingDelay = dmd;
4500
4501 /* io missing delay */
4502 io_missing_delay_original = sas_iounit_pg1->IODeviceMissingDelay;
4503 sas_iounit_pg1->IODeviceMissingDelay = io_missing_delay;
4504
4505 if (!mpt3sas_config_set_sas_iounit_pg1(ioc, &mpi_reply, sas_iounit_pg1,
4506 sz)) {
4507 if (dmd & MPI2_SASIOUNIT1_REPORT_MISSING_UNIT_16)
4508 dmd_new = (dmd &
4509 MPI2_SASIOUNIT1_REPORT_MISSING_TIMEOUT_MASK) * 16;
4510 else
4511 dmd_new =
4512 dmd & MPI2_SASIOUNIT1_REPORT_MISSING_TIMEOUT_MASK;
Joe Perches919d8a32018-09-17 08:01:09 -07004513 ioc_info(ioc, "device_missing_delay: old(%d), new(%d)\n",
4514 dmd_orignal, dmd_new);
4515 ioc_info(ioc, "ioc_missing_delay: old(%d), new(%d)\n",
4516 io_missing_delay_original,
4517 io_missing_delay);
Sreekanth Reddyf92363d2012-11-30 07:44:21 +05304518 ioc->device_missing_delay = dmd_new;
4519 ioc->io_missing_delay = io_missing_delay;
4520 }
4521
4522out:
4523 kfree(sas_iounit_pg1);
4524}
Bart Van Assche4beb4862018-06-15 14:42:01 -07004525
Sreekanth Reddyf92363d2012-11-30 07:44:21 +05304526/**
Suganath Prabu Sca7e1e92019-05-31 08:14:42 -04004527 * _base_update_ioc_page1_inlinewith_perf_mode - Update IOC Page1 fields
4528 * according to performance mode.
4529 * @ioc : per adapter object
4530 *
4531 * Return nothing.
4532 */
4533static void
4534_base_update_ioc_page1_inlinewith_perf_mode(struct MPT3SAS_ADAPTER *ioc)
4535{
4536 Mpi2IOCPage1_t ioc_pg1;
4537 Mpi2ConfigReply_t mpi_reply;
4538
4539 mpt3sas_config_get_ioc_pg1(ioc, &mpi_reply, &ioc->ioc_pg1_copy);
4540 memcpy(&ioc_pg1, &ioc->ioc_pg1_copy, sizeof(Mpi2IOCPage1_t));
4541
4542 switch (perf_mode) {
4543 case MPT_PERF_MODE_DEFAULT:
4544 case MPT_PERF_MODE_BALANCED:
4545 if (ioc->high_iops_queues) {
4546 ioc_info(ioc,
4547 "Enable interrupt coalescing only for first\t"
4548 "%d reply queues\n",
4549 MPT3SAS_HIGH_IOPS_REPLY_QUEUES);
4550 /*
4551 * If 31st bit is zero then interrupt coalescing is
4552 * enabled for all reply descriptor post queues.
4553 * If 31st bit is set to one then user can
4554 * enable/disable interrupt coalescing on per reply
4555 * descriptor post queue group(8) basis. So to enable
4556 * interrupt coalescing only on first reply descriptor
4557 * post queue group 31st bit and zero th bit is enabled.
4558 */
4559 ioc_pg1.ProductSpecific = cpu_to_le32(0x80000000 |
4560 ((1 << MPT3SAS_HIGH_IOPS_REPLY_QUEUES/8) - 1));
4561 mpt3sas_config_set_ioc_pg1(ioc, &mpi_reply, &ioc_pg1);
4562 ioc_info(ioc, "performance mode: balanced\n");
4563 return;
4564 }
Gustavo A. R. Silva6ea3b182019-06-11 10:02:19 -05004565 /* Fall through */
Suganath Prabu Sca7e1e92019-05-31 08:14:42 -04004566 case MPT_PERF_MODE_LATENCY:
4567 /*
4568 * Enable interrupt coalescing on all reply queues
4569 * with timeout value 0xA
4570 */
4571 ioc_pg1.CoalescingTimeout = cpu_to_le32(0xa);
4572 ioc_pg1.Flags |= cpu_to_le32(MPI2_IOCPAGE1_REPLY_COALESCING);
4573 ioc_pg1.ProductSpecific = 0;
4574 mpt3sas_config_set_ioc_pg1(ioc, &mpi_reply, &ioc_pg1);
4575 ioc_info(ioc, "performance mode: latency\n");
4576 break;
4577 case MPT_PERF_MODE_IOPS:
4578 /*
4579 * Enable interrupt coalescing on all reply queues.
4580 */
4581 ioc_info(ioc,
4582 "performance mode: iops with coalescing timeout: 0x%x\n",
4583 le32_to_cpu(ioc_pg1.CoalescingTimeout));
4584 ioc_pg1.Flags |= cpu_to_le32(MPI2_IOCPAGE1_REPLY_COALESCING);
4585 ioc_pg1.ProductSpecific = 0;
4586 mpt3sas_config_set_ioc_pg1(ioc, &mpi_reply, &ioc_pg1);
4587 break;
4588 }
4589}
4590
4591/**
Sreekanth Reddyf92363d2012-11-30 07:44:21 +05304592 * _base_static_config_pages - static start of day config pages
4593 * @ioc: per adapter object
Sreekanth Reddyf92363d2012-11-30 07:44:21 +05304594 */
4595static void
4596_base_static_config_pages(struct MPT3SAS_ADAPTER *ioc)
4597{
4598 Mpi2ConfigReply_t mpi_reply;
4599 u32 iounit_pg1_flags;
4600
Chaitra P Bc1a6c5a2018-04-24 05:28:41 -04004601 ioc->nvme_abort_timeout = 30;
Sreekanth Reddyf92363d2012-11-30 07:44:21 +05304602 mpt3sas_config_get_manufacturing_pg0(ioc, &mpi_reply, &ioc->manu_pg0);
4603 if (ioc->ir_firmware)
4604 mpt3sas_config_get_manufacturing_pg10(ioc, &mpi_reply,
4605 &ioc->manu_pg10);
4606
4607 /*
4608 * Ensure correct T10 PI operation if vendor left EEDPTagMode
4609 * flag unset in NVDATA.
4610 */
4611 mpt3sas_config_get_manufacturing_pg11(ioc, &mpi_reply, &ioc->manu_pg11);
Suganath Prabu6cd1bc72018-10-31 18:53:37 +05304612 if (!ioc->is_gen35_ioc && ioc->manu_pg11.EEDPTagMode == 0) {
Sreekanth Reddyf92363d2012-11-30 07:44:21 +05304613 pr_err("%s: overriding NVDATA EEDPTagMode setting\n",
4614 ioc->name);
4615 ioc->manu_pg11.EEDPTagMode &= ~0x3;
4616 ioc->manu_pg11.EEDPTagMode |= 0x1;
4617 mpt3sas_config_set_manufacturing_pg11(ioc, &mpi_reply,
4618 &ioc->manu_pg11);
4619 }
Chaitra P Bc1a6c5a2018-04-24 05:28:41 -04004620 if (ioc->manu_pg11.AddlFlags2 & NVME_TASK_MNGT_CUSTOM_MASK)
4621 ioc->tm_custom_handling = 1;
4622 else {
4623 ioc->tm_custom_handling = 0;
4624 if (ioc->manu_pg11.NVMeAbortTO < NVME_TASK_ABORT_MIN_TIMEOUT)
4625 ioc->nvme_abort_timeout = NVME_TASK_ABORT_MIN_TIMEOUT;
4626 else if (ioc->manu_pg11.NVMeAbortTO >
4627 NVME_TASK_ABORT_MAX_TIMEOUT)
4628 ioc->nvme_abort_timeout = NVME_TASK_ABORT_MAX_TIMEOUT;
4629 else
4630 ioc->nvme_abort_timeout = ioc->manu_pg11.NVMeAbortTO;
4631 }
Sreekanth Reddyf92363d2012-11-30 07:44:21 +05304632
4633 mpt3sas_config_get_bios_pg2(ioc, &mpi_reply, &ioc->bios_pg2);
4634 mpt3sas_config_get_bios_pg3(ioc, &mpi_reply, &ioc->bios_pg3);
4635 mpt3sas_config_get_ioc_pg8(ioc, &mpi_reply, &ioc->ioc_pg8);
4636 mpt3sas_config_get_iounit_pg0(ioc, &mpi_reply, &ioc->iounit_pg0);
4637 mpt3sas_config_get_iounit_pg1(ioc, &mpi_reply, &ioc->iounit_pg1);
Sreekanth Reddy2d8ce8c2015-01-12 11:38:56 +05304638 mpt3sas_config_get_iounit_pg8(ioc, &mpi_reply, &ioc->iounit_pg8);
Sreekanth Reddyf92363d2012-11-30 07:44:21 +05304639 _base_display_ioc_capabilities(ioc);
4640
4641 /*
4642 * Enable task_set_full handling in iounit_pg1 when the
4643 * facts capabilities indicate that its supported.
4644 */
4645 iounit_pg1_flags = le32_to_cpu(ioc->iounit_pg1.Flags);
4646 if ((ioc->facts.IOCCapabilities &
4647 MPI2_IOCFACTS_CAPABILITY_TASK_SET_FULL_HANDLING))
4648 iounit_pg1_flags &=
4649 ~MPI2_IOUNITPAGE1_DISABLE_TASK_SET_FULL_HANDLING;
4650 else
4651 iounit_pg1_flags |=
4652 MPI2_IOUNITPAGE1_DISABLE_TASK_SET_FULL_HANDLING;
4653 ioc->iounit_pg1.Flags = cpu_to_le32(iounit_pg1_flags);
4654 mpt3sas_config_set_iounit_pg1(ioc, &mpi_reply, &ioc->iounit_pg1);
Sreekanth Reddy2d8ce8c2015-01-12 11:38:56 +05304655
4656 if (ioc->iounit_pg8.NumSensors)
4657 ioc->temp_sensors_count = ioc->iounit_pg8.NumSensors;
Suganath Prabu Sca7e1e92019-05-31 08:14:42 -04004658 if (ioc->is_aero_ioc)
4659 _base_update_ioc_page1_inlinewith_perf_mode(ioc);
Sreekanth Reddyf92363d2012-11-30 07:44:21 +05304660}
4661
4662/**
Chaitra P B22a923c2018-04-24 05:28:38 -04004663 * mpt3sas_free_enclosure_list - release memory
4664 * @ioc: per adapter object
4665 *
4666 * Free memory allocated during encloure add.
Chaitra P B22a923c2018-04-24 05:28:38 -04004667 */
4668void
4669mpt3sas_free_enclosure_list(struct MPT3SAS_ADAPTER *ioc)
4670{
4671 struct _enclosure_node *enclosure_dev, *enclosure_dev_next;
4672
4673 /* Free enclosure list */
4674 list_for_each_entry_safe(enclosure_dev,
4675 enclosure_dev_next, &ioc->enclosure_list, list) {
4676 list_del(&enclosure_dev->list);
4677 kfree(enclosure_dev);
4678 }
4679}
4680
4681/**
Sreekanth Reddyf92363d2012-11-30 07:44:21 +05304682 * _base_release_memory_pools - release memory
4683 * @ioc: per adapter object
4684 *
4685 * Free memory allocated from _base_allocate_memory_pools.
Sreekanth Reddyf92363d2012-11-30 07:44:21 +05304686 */
4687static void
4688_base_release_memory_pools(struct MPT3SAS_ADAPTER *ioc)
4689{
Sreekanth Reddy9b05c912014-09-12 15:35:31 +05304690 int i = 0;
Chaitra P B93204b72018-04-24 05:28:32 -04004691 int j = 0;
4692 struct chain_tracker *ct;
Sreekanth Reddy9b05c912014-09-12 15:35:31 +05304693 struct reply_post_struct *rps;
Sreekanth Reddyf92363d2012-11-30 07:44:21 +05304694
Joe Perches919d8a32018-09-17 08:01:09 -07004695 dexitprintk(ioc, ioc_info(ioc, "%s\n", __func__));
Sreekanth Reddyf92363d2012-11-30 07:44:21 +05304696
4697 if (ioc->request) {
Christoph Hellwig1c2048b2018-10-11 09:35:25 +02004698 dma_free_coherent(&ioc->pdev->dev, ioc->request_dma_sz,
Sreekanth Reddyf92363d2012-11-30 07:44:21 +05304699 ioc->request, ioc->request_dma);
Joe Perches919d8a32018-09-17 08:01:09 -07004700 dexitprintk(ioc,
4701 ioc_info(ioc, "request_pool(0x%p): free\n",
4702 ioc->request));
Sreekanth Reddyf92363d2012-11-30 07:44:21 +05304703 ioc->request = NULL;
4704 }
4705
4706 if (ioc->sense) {
Romain Periere9d98412017-07-06 10:13:07 +02004707 dma_pool_free(ioc->sense_dma_pool, ioc->sense, ioc->sense_dma);
4708 dma_pool_destroy(ioc->sense_dma_pool);
Joe Perches919d8a32018-09-17 08:01:09 -07004709 dexitprintk(ioc,
4710 ioc_info(ioc, "sense_pool(0x%p): free\n",
4711 ioc->sense));
Sreekanth Reddyf92363d2012-11-30 07:44:21 +05304712 ioc->sense = NULL;
4713 }
4714
4715 if (ioc->reply) {
Romain Periere9d98412017-07-06 10:13:07 +02004716 dma_pool_free(ioc->reply_dma_pool, ioc->reply, ioc->reply_dma);
4717 dma_pool_destroy(ioc->reply_dma_pool);
Joe Perches919d8a32018-09-17 08:01:09 -07004718 dexitprintk(ioc,
4719 ioc_info(ioc, "reply_pool(0x%p): free\n",
4720 ioc->reply));
Sreekanth Reddyf92363d2012-11-30 07:44:21 +05304721 ioc->reply = NULL;
4722 }
4723
4724 if (ioc->reply_free) {
Romain Periere9d98412017-07-06 10:13:07 +02004725 dma_pool_free(ioc->reply_free_dma_pool, ioc->reply_free,
Sreekanth Reddyf92363d2012-11-30 07:44:21 +05304726 ioc->reply_free_dma);
Romain Periere9d98412017-07-06 10:13:07 +02004727 dma_pool_destroy(ioc->reply_free_dma_pool);
Joe Perches919d8a32018-09-17 08:01:09 -07004728 dexitprintk(ioc,
4729 ioc_info(ioc, "reply_free_pool(0x%p): free\n",
4730 ioc->reply_free));
Sreekanth Reddyf92363d2012-11-30 07:44:21 +05304731 ioc->reply_free = NULL;
4732 }
4733
Sreekanth Reddy9b05c912014-09-12 15:35:31 +05304734 if (ioc->reply_post) {
4735 do {
4736 rps = &ioc->reply_post[i];
4737 if (rps->reply_post_free) {
Romain Periere9d98412017-07-06 10:13:07 +02004738 dma_pool_free(
Sreekanth Reddy9b05c912014-09-12 15:35:31 +05304739 ioc->reply_post_free_dma_pool,
4740 rps->reply_post_free,
4741 rps->reply_post_free_dma);
Joe Perches919d8a32018-09-17 08:01:09 -07004742 dexitprintk(ioc,
4743 ioc_info(ioc, "reply_post_free_pool(0x%p): free\n",
4744 rps->reply_post_free));
Sreekanth Reddy9b05c912014-09-12 15:35:31 +05304745 rps->reply_post_free = NULL;
4746 }
4747 } while (ioc->rdpq_array_enable &&
4748 (++i < ioc->reply_queue_count));
Chaitra P Bcd332232018-04-24 05:28:31 -04004749 if (ioc->reply_post_free_array &&
4750 ioc->rdpq_array_enable) {
4751 dma_pool_free(ioc->reply_post_free_array_dma_pool,
4752 ioc->reply_post_free_array,
4753 ioc->reply_post_free_array_dma);
4754 ioc->reply_post_free_array = NULL;
4755 }
4756 dma_pool_destroy(ioc->reply_post_free_array_dma_pool);
Romain Periere9d98412017-07-06 10:13:07 +02004757 dma_pool_destroy(ioc->reply_post_free_dma_pool);
Sreekanth Reddy9b05c912014-09-12 15:35:31 +05304758 kfree(ioc->reply_post);
Sreekanth Reddyf92363d2012-11-30 07:44:21 +05304759 }
4760
Suganath Prabu Subramani016d5c32017-10-31 18:02:28 +05304761 if (ioc->pcie_sgl_dma_pool) {
4762 for (i = 0; i < ioc->scsiio_depth; i++) {
Suganath Prabu Subramanidbec4c902018-01-04 04:57:11 -08004763 dma_pool_free(ioc->pcie_sgl_dma_pool,
4764 ioc->pcie_sg_lookup[i].pcie_sgl,
4765 ioc->pcie_sg_lookup[i].pcie_sgl_dma);
Suganath Prabu Subramani016d5c32017-10-31 18:02:28 +05304766 }
4767 if (ioc->pcie_sgl_dma_pool)
Romain Perier13a06402017-11-20 20:32:46 +01004768 dma_pool_destroy(ioc->pcie_sgl_dma_pool);
Suganath Prabu Subramani016d5c32017-10-31 18:02:28 +05304769 }
4770
Sreekanth Reddyf92363d2012-11-30 07:44:21 +05304771 if (ioc->config_page) {
Joe Perches919d8a32018-09-17 08:01:09 -07004772 dexitprintk(ioc,
4773 ioc_info(ioc, "config_page(0x%p): free\n",
4774 ioc->config_page));
Christoph Hellwig1c2048b2018-10-11 09:35:25 +02004775 dma_free_coherent(&ioc->pdev->dev, ioc->config_page_sz,
Sreekanth Reddyf92363d2012-11-30 07:44:21 +05304776 ioc->config_page, ioc->config_page_dma);
4777 }
4778
Sreekanth Reddyf92363d2012-11-30 07:44:21 +05304779 kfree(ioc->hpr_lookup);
4780 kfree(ioc->internal_lookup);
4781 if (ioc->chain_lookup) {
Chaitra P B93204b72018-04-24 05:28:32 -04004782 for (i = 0; i < ioc->scsiio_depth; i++) {
Chaitra P B74522a92018-04-24 05:28:33 -04004783 for (j = ioc->chains_per_prp_buffer;
4784 j < ioc->chains_needed_per_io; j++) {
Chaitra P B93204b72018-04-24 05:28:32 -04004785 ct = &ioc->chain_lookup[i].chains_per_smid[j];
4786 if (ct && ct->chain_buffer)
4787 dma_pool_free(ioc->chain_dma_pool,
4788 ct->chain_buffer,
4789 ct->chain_buffer_dma);
4790 }
4791 kfree(ioc->chain_lookup[i].chains_per_smid);
Sreekanth Reddyf92363d2012-11-30 07:44:21 +05304792 }
Romain Periere9d98412017-07-06 10:13:07 +02004793 dma_pool_destroy(ioc->chain_dma_pool);
Chaitra P B93204b72018-04-24 05:28:32 -04004794 kfree(ioc->chain_lookup);
Sreekanth Reddyf92363d2012-11-30 07:44:21 +05304795 ioc->chain_lookup = NULL;
4796 }
4797}
4798
4799/**
Chaitra P Be21fef62018-04-24 05:28:34 -04004800 * is_MSB_are_same - checks whether all reply queues in a set are
4801 * having same upper 32bits in their base memory address.
4802 * @reply_pool_start_address: Base address of a reply queue set
4803 * @pool_sz: Size of single Reply Descriptor Post Queues pool size
4804 *
Bart Van Assche4beb4862018-06-15 14:42:01 -07004805 * Return: 1 if reply queues in a set have a same upper 32bits in their base
4806 * memory address, else 0.
Chaitra P Be21fef62018-04-24 05:28:34 -04004807 */
4808
4809static int
4810is_MSB_are_same(long reply_pool_start_address, u32 pool_sz)
4811{
4812 long reply_pool_end_address;
4813
4814 reply_pool_end_address = reply_pool_start_address + pool_sz;
4815
4816 if (upper_32_bits(reply_pool_start_address) ==
4817 upper_32_bits(reply_pool_end_address))
4818 return 1;
4819 else
4820 return 0;
4821}
4822
4823/**
Sreekanth Reddyf92363d2012-11-30 07:44:21 +05304824 * _base_allocate_memory_pools - allocate start of day memory pools
4825 * @ioc: per adapter object
Sreekanth Reddyf92363d2012-11-30 07:44:21 +05304826 *
Bart Van Assche4beb4862018-06-15 14:42:01 -07004827 * Return: 0 success, anything else error.
Sreekanth Reddyf92363d2012-11-30 07:44:21 +05304828 */
4829static int
Calvin Owens98c56ad2016-07-28 21:38:21 -07004830_base_allocate_memory_pools(struct MPT3SAS_ADAPTER *ioc)
Sreekanth Reddyf92363d2012-11-30 07:44:21 +05304831{
4832 struct mpt3sas_facts *facts;
4833 u16 max_sge_elements;
4834 u16 chains_needed_per_io;
Chaitra P Bcd332232018-04-24 05:28:31 -04004835 u32 sz, total_sz, reply_post_free_sz, reply_post_free_array_sz;
Sreekanth Reddyf92363d2012-11-30 07:44:21 +05304836 u32 retry_sz;
Suganath Prabu Subramani016d5c32017-10-31 18:02:28 +05304837 u16 max_request_credit, nvme_blocks_needed;
Sreekanth Reddyf92363d2012-11-30 07:44:21 +05304838 unsigned short sg_tablesize;
4839 u16 sge_size;
Chaitra P B93204b72018-04-24 05:28:32 -04004840 int i, j;
4841 struct chain_tracker *ct;
Sreekanth Reddyf92363d2012-11-30 07:44:21 +05304842
Joe Perches919d8a32018-09-17 08:01:09 -07004843 dinitprintk(ioc, ioc_info(ioc, "%s\n", __func__));
Sreekanth Reddyf92363d2012-11-30 07:44:21 +05304844
4845
4846 retry_sz = 0;
4847 facts = &ioc->facts;
4848
4849 /* command line tunables for max sgl entries */
4850 if (max_sgl_entries != -1)
4851 sg_tablesize = max_sgl_entries;
Sreekanth Reddy471ef9d2015-11-11 17:30:24 +05304852 else {
4853 if (ioc->hba_mpi_version_belonged == MPI2_VERSION)
4854 sg_tablesize = MPT2SAS_SG_DEPTH;
4855 else
4856 sg_tablesize = MPT3SAS_SG_DEPTH;
4857 }
Sreekanth Reddyf92363d2012-11-30 07:44:21 +05304858
Sreekanth Reddy06f5f972017-10-10 18:41:16 +05304859 /* max sgl entries <= MPT_KDUMP_MIN_PHYS_SEGMENTS in KDUMP mode */
4860 if (reset_devices)
4861 sg_tablesize = min_t(unsigned short, sg_tablesize,
4862 MPT_KDUMP_MIN_PHYS_SEGMENTS);
4863
Suganath Prabu Subramani0448f012018-02-07 02:51:46 -08004864 if (ioc->is_mcpu_endpoint)
4865 ioc->shost->sg_tablesize = MPT_MIN_PHYS_SEGMENTS;
4866 else {
4867 if (sg_tablesize < MPT_MIN_PHYS_SEGMENTS)
4868 sg_tablesize = MPT_MIN_PHYS_SEGMENTS;
4869 else if (sg_tablesize > MPT_MAX_PHYS_SEGMENTS) {
4870 sg_tablesize = min_t(unsigned short, sg_tablesize,
4871 SG_MAX_SEGMENTS);
Joe Perches919d8a32018-09-17 08:01:09 -07004872 ioc_warn(ioc, "sg_tablesize(%u) is bigger than kernel defined SG_CHUNK_SIZE(%u)\n",
4873 sg_tablesize, MPT_MAX_PHYS_SEGMENTS);
Suganath Prabu Subramani0448f012018-02-07 02:51:46 -08004874 }
4875 ioc->shost->sg_tablesize = sg_tablesize;
Sreekanth Reddyad666a02015-01-12 11:39:00 +05304876 }
Sreekanth Reddyf92363d2012-11-30 07:44:21 +05304877
Suganath prabu Subramanifd0331b2016-01-28 12:07:02 +05304878 ioc->internal_depth = min_t(int, (facts->HighPriorityCredit + (5)),
4879 (facts->RequestCredit / 4));
4880 if (ioc->internal_depth < INTERNAL_CMDS_COUNT) {
4881 if (facts->RequestCredit <= (INTERNAL_CMDS_COUNT +
4882 INTERNAL_SCSIIO_CMDS_COUNT)) {
Joe Perches919d8a32018-09-17 08:01:09 -07004883 ioc_err(ioc, "IOC doesn't have enough Request Credits, it has just %d number of credits\n",
4884 facts->RequestCredit);
Suganath prabu Subramanifd0331b2016-01-28 12:07:02 +05304885 return -ENOMEM;
4886 }
4887 ioc->internal_depth = 10;
4888 }
4889
4890 ioc->hi_priority_depth = ioc->internal_depth - (5);
Sreekanth Reddyf92363d2012-11-30 07:44:21 +05304891 /* command line tunables for max controller queue depth */
4892 if (max_queue_depth != -1 && max_queue_depth != 0) {
4893 max_request_credit = min_t(u16, max_queue_depth +
Suganath prabu Subramanifd0331b2016-01-28 12:07:02 +05304894 ioc->internal_depth, facts->RequestCredit);
Sreekanth Reddyf92363d2012-11-30 07:44:21 +05304895 if (max_request_credit > MAX_HBA_QUEUE_DEPTH)
4896 max_request_credit = MAX_HBA_QUEUE_DEPTH;
Sreekanth Reddy06f5f972017-10-10 18:41:16 +05304897 } else if (reset_devices)
4898 max_request_credit = min_t(u16, facts->RequestCredit,
4899 (MPT3SAS_KDUMP_SCSI_IO_DEPTH + ioc->internal_depth));
4900 else
Sreekanth Reddyf92363d2012-11-30 07:44:21 +05304901 max_request_credit = min_t(u16, facts->RequestCredit,
4902 MAX_HBA_QUEUE_DEPTH);
4903
Suganath prabu Subramanifd0331b2016-01-28 12:07:02 +05304904 /* Firmware maintains additional facts->HighPriorityCredit number of
4905 * credits for HiPriprity Request messages, so hba queue depth will be
4906 * sum of max_request_credit and high priority queue depth.
4907 */
4908 ioc->hba_queue_depth = max_request_credit + ioc->hi_priority_depth;
Sreekanth Reddyf92363d2012-11-30 07:44:21 +05304909
4910 /* request frame size */
4911 ioc->request_sz = facts->IOCRequestFrameSize * 4;
4912
4913 /* reply frame size */
4914 ioc->reply_sz = facts->ReplyFrameSize * 4;
4915
Suganath prabu Subramaniebb30242016-01-28 12:07:04 +05304916 /* chain segment size */
4917 if (ioc->hba_mpi_version_belonged != MPI2_VERSION) {
4918 if (facts->IOCMaxChainSegmentSize)
4919 ioc->chain_segment_sz =
4920 facts->IOCMaxChainSegmentSize *
4921 MAX_CHAIN_ELEMT_SZ;
4922 else
4923 /* set to 128 bytes size if IOCMaxChainSegmentSize is zero */
4924 ioc->chain_segment_sz = DEFAULT_NUM_FWCHAIN_ELEMTS *
4925 MAX_CHAIN_ELEMT_SZ;
4926 } else
4927 ioc->chain_segment_sz = ioc->request_sz;
4928
Sreekanth Reddyf92363d2012-11-30 07:44:21 +05304929 /* calculate the max scatter element size */
4930 sge_size = max_t(u16, ioc->sge_size, ioc->sge_size_ieee);
4931
4932 retry_allocation:
4933 total_sz = 0;
4934 /* calculate number of sg elements left over in the 1st frame */
4935 max_sge_elements = ioc->request_sz - ((sizeof(Mpi2SCSIIORequest_t) -
4936 sizeof(Mpi2SGEIOUnion_t)) + sge_size);
4937 ioc->max_sges_in_main_message = max_sge_elements/sge_size;
4938
4939 /* now do the same for a chain buffer */
Suganath prabu Subramaniebb30242016-01-28 12:07:04 +05304940 max_sge_elements = ioc->chain_segment_sz - sge_size;
Sreekanth Reddyf92363d2012-11-30 07:44:21 +05304941 ioc->max_sges_in_chain_message = max_sge_elements/sge_size;
4942
4943 /*
4944 * MPT3SAS_SG_DEPTH = CONFIG_FUSION_MAX_SGE
4945 */
4946 chains_needed_per_io = ((ioc->shost->sg_tablesize -
4947 ioc->max_sges_in_main_message)/ioc->max_sges_in_chain_message)
4948 + 1;
4949 if (chains_needed_per_io > facts->MaxChainDepth) {
4950 chains_needed_per_io = facts->MaxChainDepth;
4951 ioc->shost->sg_tablesize = min_t(u16,
4952 ioc->max_sges_in_main_message + (ioc->max_sges_in_chain_message
4953 * chains_needed_per_io), ioc->shost->sg_tablesize);
4954 }
4955 ioc->chains_needed_per_io = chains_needed_per_io;
4956
4957 /* reply free queue sizing - taking into account for 64 FW events */
4958 ioc->reply_free_queue_depth = ioc->hba_queue_depth + 64;
4959
Suganath Prabu Subramani0448f012018-02-07 02:51:46 -08004960 /* mCPU manage single counters for simplicity */
4961 if (ioc->is_mcpu_endpoint)
4962 ioc->reply_post_queue_depth = ioc->reply_free_queue_depth;
4963 else {
4964 /* calculate reply descriptor post queue depth */
4965 ioc->reply_post_queue_depth = ioc->hba_queue_depth +
4966 ioc->reply_free_queue_depth + 1;
4967 /* align the reply post queue on the next 16 count boundary */
4968 if (ioc->reply_post_queue_depth % 16)
4969 ioc->reply_post_queue_depth += 16 -
4970 (ioc->reply_post_queue_depth % 16);
4971 }
Sreekanth Reddyf92363d2012-11-30 07:44:21 +05304972
Sreekanth Reddyf92363d2012-11-30 07:44:21 +05304973 if (ioc->reply_post_queue_depth >
4974 facts->MaxReplyDescriptorPostQueueDepth) {
4975 ioc->reply_post_queue_depth =
4976 facts->MaxReplyDescriptorPostQueueDepth -
4977 (facts->MaxReplyDescriptorPostQueueDepth % 16);
4978 ioc->hba_queue_depth =
4979 ((ioc->reply_post_queue_depth - 64) / 2) - 1;
4980 ioc->reply_free_queue_depth = ioc->hba_queue_depth + 64;
4981 }
4982
Joe Perches919d8a32018-09-17 08:01:09 -07004983 dinitprintk(ioc,
4984 ioc_info(ioc, "scatter gather: sge_in_main_msg(%d), sge_per_chain(%d), sge_per_io(%d), chains_per_io(%d)\n",
4985 ioc->max_sges_in_main_message,
4986 ioc->max_sges_in_chain_message,
4987 ioc->shost->sg_tablesize,
4988 ioc->chains_needed_per_io));
Sreekanth Reddyf92363d2012-11-30 07:44:21 +05304989
Sreekanth Reddy9b05c912014-09-12 15:35:31 +05304990 /* reply post queue, 16 byte align */
4991 reply_post_free_sz = ioc->reply_post_queue_depth *
4992 sizeof(Mpi2DefaultReplyDescriptor_t);
4993
4994 sz = reply_post_free_sz;
4995 if (_base_is_controller_msix_enabled(ioc) && !ioc->rdpq_array_enable)
4996 sz *= ioc->reply_queue_count;
4997
4998 ioc->reply_post = kcalloc((ioc->rdpq_array_enable) ?
4999 (ioc->reply_queue_count):1,
5000 sizeof(struct reply_post_struct), GFP_KERNEL);
5001
5002 if (!ioc->reply_post) {
Joe Perches919d8a32018-09-17 08:01:09 -07005003 ioc_err(ioc, "reply_post_free pool: kcalloc failed\n");
Sreekanth Reddy9b05c912014-09-12 15:35:31 +05305004 goto out;
5005 }
Romain Periere9d98412017-07-06 10:13:07 +02005006 ioc->reply_post_free_dma_pool = dma_pool_create("reply_post_free pool",
5007 &ioc->pdev->dev, sz, 16, 0);
Sreekanth Reddy9b05c912014-09-12 15:35:31 +05305008 if (!ioc->reply_post_free_dma_pool) {
Joe Perches919d8a32018-09-17 08:01:09 -07005009 ioc_err(ioc, "reply_post_free pool: dma_pool_create failed\n");
Sreekanth Reddy9b05c912014-09-12 15:35:31 +05305010 goto out;
5011 }
5012 i = 0;
5013 do {
5014 ioc->reply_post[i].reply_post_free =
Souptick Joarderc39a4d72018-10-02 11:18:04 +05305015 dma_pool_zalloc(ioc->reply_post_free_dma_pool,
Sreekanth Reddy9b05c912014-09-12 15:35:31 +05305016 GFP_KERNEL,
5017 &ioc->reply_post[i].reply_post_free_dma);
5018 if (!ioc->reply_post[i].reply_post_free) {
Joe Perches919d8a32018-09-17 08:01:09 -07005019 ioc_err(ioc, "reply_post_free pool: dma_pool_alloc failed\n");
Sreekanth Reddy9b05c912014-09-12 15:35:31 +05305020 goto out;
5021 }
Joe Perches919d8a32018-09-17 08:01:09 -07005022 dinitprintk(ioc,
5023 ioc_info(ioc, "reply post free pool (0x%p): depth(%d), element_size(%d), pool_size(%d kB)\n",
5024 ioc->reply_post[i].reply_post_free,
5025 ioc->reply_post_queue_depth,
5026 8, sz / 1024));
5027 dinitprintk(ioc,
5028 ioc_info(ioc, "reply_post_free_dma = (0x%llx)\n",
5029 (u64)ioc->reply_post[i].reply_post_free_dma));
Sreekanth Reddy9b05c912014-09-12 15:35:31 +05305030 total_sz += sz;
5031 } while (ioc->rdpq_array_enable && (++i < ioc->reply_queue_count));
5032
Suganath Prabudf9a6062019-07-30 03:43:57 -04005033 if (ioc->dma_mask > 32) {
Sreekanth Reddy9b05c912014-09-12 15:35:31 +05305034 if (_base_change_consistent_dma_mask(ioc, ioc->pdev) != 0) {
Joe Perches919d8a32018-09-17 08:01:09 -07005035 ioc_warn(ioc, "no suitable consistent DMA mask for %s\n",
5036 pci_name(ioc->pdev));
Sreekanth Reddy9b05c912014-09-12 15:35:31 +05305037 goto out;
5038 }
5039 }
5040
Sreekanth Reddyf92363d2012-11-30 07:44:21 +05305041 ioc->scsiio_depth = ioc->hba_queue_depth -
5042 ioc->hi_priority_depth - ioc->internal_depth;
5043
5044 /* set the scsi host can_queue depth
5045 * with some internal commands that could be outstanding
5046 */
Suganath prabu Subramanifd0331b2016-01-28 12:07:02 +05305047 ioc->shost->can_queue = ioc->scsiio_depth - INTERNAL_SCSIIO_CMDS_COUNT;
Joe Perches919d8a32018-09-17 08:01:09 -07005048 dinitprintk(ioc,
5049 ioc_info(ioc, "scsi host: can_queue depth (%d)\n",
5050 ioc->shost->can_queue));
Sreekanth Reddyf92363d2012-11-30 07:44:21 +05305051
5052
5053 /* contiguous pool for request and chains, 16 byte align, one extra "
5054 * "frame for smid=0
5055 */
5056 ioc->chain_depth = ioc->chains_needed_per_io * ioc->scsiio_depth;
5057 sz = ((ioc->scsiio_depth + 1) * ioc->request_sz);
5058
5059 /* hi-priority queue */
5060 sz += (ioc->hi_priority_depth * ioc->request_sz);
5061
5062 /* internal queue */
5063 sz += (ioc->internal_depth * ioc->request_sz);
5064
5065 ioc->request_dma_sz = sz;
Christoph Hellwig1c2048b2018-10-11 09:35:25 +02005066 ioc->request = dma_alloc_coherent(&ioc->pdev->dev, sz,
5067 &ioc->request_dma, GFP_KERNEL);
Sreekanth Reddyf92363d2012-11-30 07:44:21 +05305068 if (!ioc->request) {
Christoph Hellwig1c2048b2018-10-11 09:35:25 +02005069 ioc_err(ioc, "request pool: dma_alloc_coherent failed: hba_depth(%d), chains_per_io(%d), frame_sz(%d), total(%d kB)\n",
Joe Perches919d8a32018-09-17 08:01:09 -07005070 ioc->hba_queue_depth, ioc->chains_needed_per_io,
5071 ioc->request_sz, sz / 1024);
Sreekanth Reddyf92363d2012-11-30 07:44:21 +05305072 if (ioc->scsiio_depth < MPT3SAS_SAS_QUEUE_DEPTH)
5073 goto out;
Suganath prabu Subramanifd0331b2016-01-28 12:07:02 +05305074 retry_sz = 64;
5075 ioc->hba_queue_depth -= retry_sz;
Suganath prabu Subramani8ff045c2016-02-18 14:09:45 +05305076 _base_release_memory_pools(ioc);
Sreekanth Reddyf92363d2012-11-30 07:44:21 +05305077 goto retry_allocation;
5078 }
Suganath Prabue224e032019-08-03 09:59:47 -04005079 memset(ioc->request, 0, sz);
Sreekanth Reddyf92363d2012-11-30 07:44:21 +05305080
5081 if (retry_sz)
Christoph Hellwig1c2048b2018-10-11 09:35:25 +02005082 ioc_err(ioc, "request pool: dma_alloc_coherent succeed: hba_depth(%d), chains_per_io(%d), frame_sz(%d), total(%d kb)\n",
Joe Perches919d8a32018-09-17 08:01:09 -07005083 ioc->hba_queue_depth, ioc->chains_needed_per_io,
5084 ioc->request_sz, sz / 1024);
Sreekanth Reddyf92363d2012-11-30 07:44:21 +05305085
5086 /* hi-priority queue */
5087 ioc->hi_priority = ioc->request + ((ioc->scsiio_depth + 1) *
5088 ioc->request_sz);
5089 ioc->hi_priority_dma = ioc->request_dma + ((ioc->scsiio_depth + 1) *
5090 ioc->request_sz);
5091
5092 /* internal queue */
5093 ioc->internal = ioc->hi_priority + (ioc->hi_priority_depth *
5094 ioc->request_sz);
5095 ioc->internal_dma = ioc->hi_priority_dma + (ioc->hi_priority_depth *
5096 ioc->request_sz);
5097
Joe Perches919d8a32018-09-17 08:01:09 -07005098 dinitprintk(ioc,
5099 ioc_info(ioc, "request pool(0x%p): depth(%d), frame_size(%d), pool_size(%d kB)\n",
5100 ioc->request, ioc->hba_queue_depth,
5101 ioc->request_sz,
5102 (ioc->hba_queue_depth * ioc->request_sz) / 1024));
Sreekanth Reddyf92363d2012-11-30 07:44:21 +05305103
Joe Perches919d8a32018-09-17 08:01:09 -07005104 dinitprintk(ioc,
5105 ioc_info(ioc, "request pool: dma(0x%llx)\n",
5106 (unsigned long long)ioc->request_dma));
Sreekanth Reddyf92363d2012-11-30 07:44:21 +05305107 total_sz += sz;
5108
Joe Perches919d8a32018-09-17 08:01:09 -07005109 dinitprintk(ioc,
5110 ioc_info(ioc, "scsiio(0x%p): depth(%d)\n",
5111 ioc->request, ioc->scsiio_depth));
Sreekanth Reddyf92363d2012-11-30 07:44:21 +05305112
5113 ioc->chain_depth = min_t(u32, ioc->chain_depth, MAX_CHAIN_DEPTH);
Chaitra P B93204b72018-04-24 05:28:32 -04005114 sz = ioc->scsiio_depth * sizeof(struct chain_lookup);
5115 ioc->chain_lookup = kzalloc(sz, GFP_KERNEL);
Sreekanth Reddyf92363d2012-11-30 07:44:21 +05305116 if (!ioc->chain_lookup) {
Joe Perches919d8a32018-09-17 08:01:09 -07005117 ioc_err(ioc, "chain_lookup: __get_free_pages failed\n");
Sreekanth Reddyf92363d2012-11-30 07:44:21 +05305118 goto out;
5119 }
Chaitra P B93204b72018-04-24 05:28:32 -04005120
5121 sz = ioc->chains_needed_per_io * sizeof(struct chain_tracker);
5122 for (i = 0; i < ioc->scsiio_depth; i++) {
5123 ioc->chain_lookup[i].chains_per_smid = kzalloc(sz, GFP_KERNEL);
5124 if (!ioc->chain_lookup[i].chains_per_smid) {
Joe Perches919d8a32018-09-17 08:01:09 -07005125 ioc_err(ioc, "chain_lookup: kzalloc failed\n");
Chaitra P B93204b72018-04-24 05:28:32 -04005126 goto out;
5127 }
5128 }
5129
Sreekanth Reddyf92363d2012-11-30 07:44:21 +05305130 /* initialize hi-priority queue smid's */
5131 ioc->hpr_lookup = kcalloc(ioc->hi_priority_depth,
5132 sizeof(struct request_tracker), GFP_KERNEL);
5133 if (!ioc->hpr_lookup) {
Joe Perches919d8a32018-09-17 08:01:09 -07005134 ioc_err(ioc, "hpr_lookup: kcalloc failed\n");
Sreekanth Reddyf92363d2012-11-30 07:44:21 +05305135 goto out;
5136 }
5137 ioc->hi_priority_smid = ioc->scsiio_depth + 1;
Joe Perches919d8a32018-09-17 08:01:09 -07005138 dinitprintk(ioc,
5139 ioc_info(ioc, "hi_priority(0x%p): depth(%d), start smid(%d)\n",
5140 ioc->hi_priority,
5141 ioc->hi_priority_depth, ioc->hi_priority_smid));
Sreekanth Reddyf92363d2012-11-30 07:44:21 +05305142
5143 /* initialize internal queue smid's */
5144 ioc->internal_lookup = kcalloc(ioc->internal_depth,
5145 sizeof(struct request_tracker), GFP_KERNEL);
5146 if (!ioc->internal_lookup) {
Joe Perches919d8a32018-09-17 08:01:09 -07005147 ioc_err(ioc, "internal_lookup: kcalloc failed\n");
Sreekanth Reddyf92363d2012-11-30 07:44:21 +05305148 goto out;
5149 }
5150 ioc->internal_smid = ioc->hi_priority_smid + ioc->hi_priority_depth;
Joe Perches919d8a32018-09-17 08:01:09 -07005151 dinitprintk(ioc,
5152 ioc_info(ioc, "internal(0x%p): depth(%d), start smid(%d)\n",
5153 ioc->internal,
5154 ioc->internal_depth, ioc->internal_smid));
Suganath Prabu Subramani016d5c32017-10-31 18:02:28 +05305155 /*
5156 * The number of NVMe page sized blocks needed is:
5157 * (((sg_tablesize * 8) - 1) / (page_size - 8)) + 1
5158 * ((sg_tablesize * 8) - 1) is the max PRP's minus the first PRP entry
5159 * that is placed in the main message frame. 8 is the size of each PRP
5160 * entry or PRP list pointer entry. 8 is subtracted from page_size
5161 * because of the PRP list pointer entry at the end of a page, so this
5162 * is not counted as a PRP entry. The 1 added page is a round up.
5163 *
5164 * To avoid allocation failures due to the amount of memory that could
5165 * be required for NVMe PRP's, only each set of NVMe blocks will be
5166 * contiguous, so a new set is allocated for each possible I/O.
5167 */
Chaitra P B74522a92018-04-24 05:28:33 -04005168 ioc->chains_per_prp_buffer = 0;
Suganath Prabu Subramani016d5c32017-10-31 18:02:28 +05305169 if (ioc->facts.ProtocolFlags & MPI2_IOCFACTS_PROTOCOL_NVME_DEVICES) {
5170 nvme_blocks_needed =
5171 (ioc->shost->sg_tablesize * NVME_PRP_SIZE) - 1;
5172 nvme_blocks_needed /= (ioc->page_size - NVME_PRP_SIZE);
5173 nvme_blocks_needed++;
Sreekanth Reddyf92363d2012-11-30 07:44:21 +05305174
Suganath Prabu Subramanidbec4c902018-01-04 04:57:11 -08005175 sz = sizeof(struct pcie_sg_list) * ioc->scsiio_depth;
5176 ioc->pcie_sg_lookup = kzalloc(sz, GFP_KERNEL);
5177 if (!ioc->pcie_sg_lookup) {
Joe Perches919d8a32018-09-17 08:01:09 -07005178 ioc_info(ioc, "PCIe SGL lookup: kzalloc failed\n");
Suganath Prabu Subramanidbec4c902018-01-04 04:57:11 -08005179 goto out;
5180 }
Suganath Prabu Subramani016d5c32017-10-31 18:02:28 +05305181 sz = nvme_blocks_needed * ioc->page_size;
5182 ioc->pcie_sgl_dma_pool =
Romain Perier13a06402017-11-20 20:32:46 +01005183 dma_pool_create("PCIe SGL pool", &ioc->pdev->dev, sz, 16, 0);
Suganath Prabu Subramani016d5c32017-10-31 18:02:28 +05305184 if (!ioc->pcie_sgl_dma_pool) {
Joe Perches919d8a32018-09-17 08:01:09 -07005185 ioc_info(ioc, "PCIe SGL pool: dma_pool_create failed\n");
Suganath Prabu Subramani016d5c32017-10-31 18:02:28 +05305186 goto out;
5187 }
Chaitra P B74522a92018-04-24 05:28:33 -04005188
5189 ioc->chains_per_prp_buffer = sz/ioc->chain_segment_sz;
5190 ioc->chains_per_prp_buffer = min(ioc->chains_per_prp_buffer,
5191 ioc->chains_needed_per_io);
5192
Suganath Prabu Subramani016d5c32017-10-31 18:02:28 +05305193 for (i = 0; i < ioc->scsiio_depth; i++) {
Suganath Prabu Subramanidbec4c902018-01-04 04:57:11 -08005194 ioc->pcie_sg_lookup[i].pcie_sgl = dma_pool_alloc(
5195 ioc->pcie_sgl_dma_pool, GFP_KERNEL,
5196 &ioc->pcie_sg_lookup[i].pcie_sgl_dma);
5197 if (!ioc->pcie_sg_lookup[i].pcie_sgl) {
Joe Perches919d8a32018-09-17 08:01:09 -07005198 ioc_info(ioc, "PCIe SGL pool: dma_pool_alloc failed\n");
Suganath Prabu Subramani016d5c32017-10-31 18:02:28 +05305199 goto out;
5200 }
Chaitra P B74522a92018-04-24 05:28:33 -04005201 for (j = 0; j < ioc->chains_per_prp_buffer; j++) {
5202 ct = &ioc->chain_lookup[i].chains_per_smid[j];
5203 ct->chain_buffer =
5204 ioc->pcie_sg_lookup[i].pcie_sgl +
5205 (j * ioc->chain_segment_sz);
5206 ct->chain_buffer_dma =
5207 ioc->pcie_sg_lookup[i].pcie_sgl_dma +
5208 (j * ioc->chain_segment_sz);
5209 }
Suganath Prabu Subramani016d5c32017-10-31 18:02:28 +05305210 }
5211
Joe Perches919d8a32018-09-17 08:01:09 -07005212 dinitprintk(ioc,
5213 ioc_info(ioc, "PCIe sgl pool depth(%d), element_size(%d), pool_size(%d kB)\n",
5214 ioc->scsiio_depth, sz,
5215 (sz * ioc->scsiio_depth) / 1024));
5216 dinitprintk(ioc,
5217 ioc_info(ioc, "Number of chains can fit in a PRP page(%d)\n",
5218 ioc->chains_per_prp_buffer));
Suganath Prabu Subramani016d5c32017-10-31 18:02:28 +05305219 total_sz += sz * ioc->scsiio_depth;
5220 }
Chaitra P B74522a92018-04-24 05:28:33 -04005221
5222 ioc->chain_dma_pool = dma_pool_create("chain pool", &ioc->pdev->dev,
5223 ioc->chain_segment_sz, 16, 0);
5224 if (!ioc->chain_dma_pool) {
Joe Perches919d8a32018-09-17 08:01:09 -07005225 ioc_err(ioc, "chain_dma_pool: dma_pool_create failed\n");
Chaitra P B74522a92018-04-24 05:28:33 -04005226 goto out;
5227 }
5228 for (i = 0; i < ioc->scsiio_depth; i++) {
5229 for (j = ioc->chains_per_prp_buffer;
5230 j < ioc->chains_needed_per_io; j++) {
5231 ct = &ioc->chain_lookup[i].chains_per_smid[j];
5232 ct->chain_buffer = dma_pool_alloc(
5233 ioc->chain_dma_pool, GFP_KERNEL,
5234 &ct->chain_buffer_dma);
5235 if (!ct->chain_buffer) {
Joe Perches919d8a32018-09-17 08:01:09 -07005236 ioc_err(ioc, "chain_lookup: pci_pool_alloc failed\n");
Chaitra P B74522a92018-04-24 05:28:33 -04005237 _base_release_memory_pools(ioc);
5238 goto out;
5239 }
5240 }
5241 total_sz += ioc->chain_segment_sz;
5242 }
5243
Joe Perches919d8a32018-09-17 08:01:09 -07005244 dinitprintk(ioc,
5245 ioc_info(ioc, "chain pool depth(%d), frame_size(%d), pool_size(%d kB)\n",
5246 ioc->chain_depth, ioc->chain_segment_sz,
5247 (ioc->chain_depth * ioc->chain_segment_sz) / 1024));
Chaitra P B74522a92018-04-24 05:28:33 -04005248
Sreekanth Reddyf92363d2012-11-30 07:44:21 +05305249 /* sense buffers, 4 byte align */
5250 sz = ioc->scsiio_depth * SCSI_SENSE_BUFFERSIZE;
Romain Periere9d98412017-07-06 10:13:07 +02005251 ioc->sense_dma_pool = dma_pool_create("sense pool", &ioc->pdev->dev, sz,
5252 4, 0);
Sreekanth Reddyf92363d2012-11-30 07:44:21 +05305253 if (!ioc->sense_dma_pool) {
Joe Perches919d8a32018-09-17 08:01:09 -07005254 ioc_err(ioc, "sense pool: dma_pool_create failed\n");
Sreekanth Reddyf92363d2012-11-30 07:44:21 +05305255 goto out;
5256 }
Romain Periere9d98412017-07-06 10:13:07 +02005257 ioc->sense = dma_pool_alloc(ioc->sense_dma_pool, GFP_KERNEL,
Sreekanth Reddyf92363d2012-11-30 07:44:21 +05305258 &ioc->sense_dma);
5259 if (!ioc->sense) {
Joe Perches919d8a32018-09-17 08:01:09 -07005260 ioc_err(ioc, "sense pool: dma_pool_alloc failed\n");
Sreekanth Reddyf92363d2012-11-30 07:44:21 +05305261 goto out;
5262 }
Chaitra P Be21fef62018-04-24 05:28:34 -04005263 /* sense buffer requires to be in same 4 gb region.
5264 * Below function will check the same.
5265 * In case of failure, new pci pool will be created with updated
5266 * alignment. Older allocation and pool will be destroyed.
5267 * Alignment will be used such a way that next allocation if
5268 * success, will always meet same 4gb region requirement.
5269 * Actual requirement is not alignment, but we need start and end of
5270 * DMA address must have same upper 32 bit address.
5271 */
5272 if (!is_MSB_are_same((long)ioc->sense, sz)) {
5273 //Release Sense pool & Reallocate
5274 dma_pool_free(ioc->sense_dma_pool, ioc->sense, ioc->sense_dma);
5275 dma_pool_destroy(ioc->sense_dma_pool);
5276 ioc->sense = NULL;
5277
5278 ioc->sense_dma_pool =
5279 dma_pool_create("sense pool", &ioc->pdev->dev, sz,
5280 roundup_pow_of_two(sz), 0);
5281 if (!ioc->sense_dma_pool) {
Joe Perches919d8a32018-09-17 08:01:09 -07005282 ioc_err(ioc, "sense pool: pci_pool_create failed\n");
Chaitra P Be21fef62018-04-24 05:28:34 -04005283 goto out;
5284 }
5285 ioc->sense = dma_pool_alloc(ioc->sense_dma_pool, GFP_KERNEL,
5286 &ioc->sense_dma);
5287 if (!ioc->sense) {
Joe Perches919d8a32018-09-17 08:01:09 -07005288 ioc_err(ioc, "sense pool: pci_pool_alloc failed\n");
Chaitra P Be21fef62018-04-24 05:28:34 -04005289 goto out;
5290 }
5291 }
Joe Perches919d8a32018-09-17 08:01:09 -07005292 dinitprintk(ioc,
5293 ioc_info(ioc, "sense pool(0x%p): depth(%d), element_size(%d), pool_size(%d kB)\n",
5294 ioc->sense, ioc->scsiio_depth,
5295 SCSI_SENSE_BUFFERSIZE, sz / 1024));
5296 dinitprintk(ioc,
5297 ioc_info(ioc, "sense_dma(0x%llx)\n",
5298 (unsigned long long)ioc->sense_dma));
Sreekanth Reddyf92363d2012-11-30 07:44:21 +05305299 total_sz += sz;
5300
5301 /* reply pool, 4 byte align */
5302 sz = ioc->reply_free_queue_depth * ioc->reply_sz;
Romain Periere9d98412017-07-06 10:13:07 +02005303 ioc->reply_dma_pool = dma_pool_create("reply pool", &ioc->pdev->dev, sz,
5304 4, 0);
Sreekanth Reddyf92363d2012-11-30 07:44:21 +05305305 if (!ioc->reply_dma_pool) {
Joe Perches919d8a32018-09-17 08:01:09 -07005306 ioc_err(ioc, "reply pool: dma_pool_create failed\n");
Sreekanth Reddyf92363d2012-11-30 07:44:21 +05305307 goto out;
5308 }
Romain Periere9d98412017-07-06 10:13:07 +02005309 ioc->reply = dma_pool_alloc(ioc->reply_dma_pool, GFP_KERNEL,
Sreekanth Reddyf92363d2012-11-30 07:44:21 +05305310 &ioc->reply_dma);
5311 if (!ioc->reply) {
Joe Perches919d8a32018-09-17 08:01:09 -07005312 ioc_err(ioc, "reply pool: dma_pool_alloc failed\n");
Sreekanth Reddyf92363d2012-11-30 07:44:21 +05305313 goto out;
5314 }
5315 ioc->reply_dma_min_address = (u32)(ioc->reply_dma);
5316 ioc->reply_dma_max_address = (u32)(ioc->reply_dma) + sz;
Joe Perches919d8a32018-09-17 08:01:09 -07005317 dinitprintk(ioc,
5318 ioc_info(ioc, "reply pool(0x%p): depth(%d), frame_size(%d), pool_size(%d kB)\n",
5319 ioc->reply, ioc->reply_free_queue_depth,
5320 ioc->reply_sz, sz / 1024));
5321 dinitprintk(ioc,
5322 ioc_info(ioc, "reply_dma(0x%llx)\n",
5323 (unsigned long long)ioc->reply_dma));
Sreekanth Reddyf92363d2012-11-30 07:44:21 +05305324 total_sz += sz;
5325
5326 /* reply free queue, 16 byte align */
5327 sz = ioc->reply_free_queue_depth * 4;
Romain Periere9d98412017-07-06 10:13:07 +02005328 ioc->reply_free_dma_pool = dma_pool_create("reply_free pool",
5329 &ioc->pdev->dev, sz, 16, 0);
Sreekanth Reddyf92363d2012-11-30 07:44:21 +05305330 if (!ioc->reply_free_dma_pool) {
Joe Perches919d8a32018-09-17 08:01:09 -07005331 ioc_err(ioc, "reply_free pool: dma_pool_create failed\n");
Sreekanth Reddyf92363d2012-11-30 07:44:21 +05305332 goto out;
5333 }
Souptick Joarderc39a4d72018-10-02 11:18:04 +05305334 ioc->reply_free = dma_pool_zalloc(ioc->reply_free_dma_pool, GFP_KERNEL,
Sreekanth Reddyf92363d2012-11-30 07:44:21 +05305335 &ioc->reply_free_dma);
5336 if (!ioc->reply_free) {
Joe Perches919d8a32018-09-17 08:01:09 -07005337 ioc_err(ioc, "reply_free pool: dma_pool_alloc failed\n");
Sreekanth Reddyf92363d2012-11-30 07:44:21 +05305338 goto out;
5339 }
Joe Perches919d8a32018-09-17 08:01:09 -07005340 dinitprintk(ioc,
5341 ioc_info(ioc, "reply_free pool(0x%p): depth(%d), element_size(%d), pool_size(%d kB)\n",
5342 ioc->reply_free, ioc->reply_free_queue_depth,
5343 4, sz / 1024));
5344 dinitprintk(ioc,
5345 ioc_info(ioc, "reply_free_dma (0x%llx)\n",
5346 (unsigned long long)ioc->reply_free_dma));
Sreekanth Reddyf92363d2012-11-30 07:44:21 +05305347 total_sz += sz;
5348
Chaitra P Bcd332232018-04-24 05:28:31 -04005349 if (ioc->rdpq_array_enable) {
5350 reply_post_free_array_sz = ioc->reply_queue_count *
5351 sizeof(Mpi2IOCInitRDPQArrayEntry);
5352 ioc->reply_post_free_array_dma_pool =
5353 dma_pool_create("reply_post_free_array pool",
5354 &ioc->pdev->dev, reply_post_free_array_sz, 16, 0);
5355 if (!ioc->reply_post_free_array_dma_pool) {
5356 dinitprintk(ioc,
Joe Perches919d8a32018-09-17 08:01:09 -07005357 ioc_info(ioc, "reply_post_free_array pool: dma_pool_create failed\n"));
Chaitra P Bcd332232018-04-24 05:28:31 -04005358 goto out;
5359 }
5360 ioc->reply_post_free_array =
5361 dma_pool_alloc(ioc->reply_post_free_array_dma_pool,
5362 GFP_KERNEL, &ioc->reply_post_free_array_dma);
5363 if (!ioc->reply_post_free_array) {
5364 dinitprintk(ioc,
Joe Perches919d8a32018-09-17 08:01:09 -07005365 ioc_info(ioc, "reply_post_free_array pool: dma_pool_alloc failed\n"));
Chaitra P Bcd332232018-04-24 05:28:31 -04005366 goto out;
5367 }
5368 }
Sreekanth Reddyf92363d2012-11-30 07:44:21 +05305369 ioc->config_page_sz = 512;
Christoph Hellwig1c2048b2018-10-11 09:35:25 +02005370 ioc->config_page = dma_alloc_coherent(&ioc->pdev->dev,
5371 ioc->config_page_sz, &ioc->config_page_dma, GFP_KERNEL);
Sreekanth Reddyf92363d2012-11-30 07:44:21 +05305372 if (!ioc->config_page) {
Joe Perches919d8a32018-09-17 08:01:09 -07005373 ioc_err(ioc, "config page: dma_pool_alloc failed\n");
Sreekanth Reddyf92363d2012-11-30 07:44:21 +05305374 goto out;
5375 }
Joe Perches919d8a32018-09-17 08:01:09 -07005376 dinitprintk(ioc,
5377 ioc_info(ioc, "config page(0x%p): size(%d)\n",
5378 ioc->config_page, ioc->config_page_sz));
5379 dinitprintk(ioc,
5380 ioc_info(ioc, "config_page_dma(0x%llx)\n",
5381 (unsigned long long)ioc->config_page_dma));
Sreekanth Reddyf92363d2012-11-30 07:44:21 +05305382 total_sz += ioc->config_page_sz;
5383
Joe Perches919d8a32018-09-17 08:01:09 -07005384 ioc_info(ioc, "Allocated physical memory: size(%d kB)\n",
5385 total_sz / 1024);
5386 ioc_info(ioc, "Current Controller Queue Depth(%d),Max Controller Queue Depth(%d)\n",
5387 ioc->shost->can_queue, facts->RequestCredit);
5388 ioc_info(ioc, "Scatter Gather Elements per IO(%d)\n",
5389 ioc->shost->sg_tablesize);
Sreekanth Reddyf92363d2012-11-30 07:44:21 +05305390 return 0;
5391
5392 out:
5393 return -ENOMEM;
5394}
5395
5396/**
5397 * mpt3sas_base_get_iocstate - Get the current state of a MPT adapter.
5398 * @ioc: Pointer to MPT_ADAPTER structure
5399 * @cooked: Request raw or cooked IOC state
5400 *
Bart Van Assche4beb4862018-06-15 14:42:01 -07005401 * Return: all IOC Doorbell register bits if cooked==0, else just the
Sreekanth Reddyf92363d2012-11-30 07:44:21 +05305402 * Doorbell bits in MPI_IOC_STATE_MASK.
5403 */
5404u32
5405mpt3sas_base_get_iocstate(struct MPT3SAS_ADAPTER *ioc, int cooked)
5406{
5407 u32 s, sc;
5408
Suganath Prabu306eaf22018-12-07 12:58:34 +05305409 s = ioc->base_readl(&ioc->chip->Doorbell);
Sreekanth Reddyf92363d2012-11-30 07:44:21 +05305410 sc = s & MPI2_IOC_STATE_MASK;
5411 return cooked ? sc : s;
5412}
5413
5414/**
5415 * _base_wait_on_iocstate - waiting on a particular ioc state
Bart Van Assche4beb4862018-06-15 14:42:01 -07005416 * @ioc: ?
Sreekanth Reddyf92363d2012-11-30 07:44:21 +05305417 * @ioc_state: controller state { READY, OPERATIONAL, or RESET }
5418 * @timeout: timeout in second
Sreekanth Reddyf92363d2012-11-30 07:44:21 +05305419 *
Bart Van Assche4beb4862018-06-15 14:42:01 -07005420 * Return: 0 for success, non-zero for failure.
Sreekanth Reddyf92363d2012-11-30 07:44:21 +05305421 */
5422static int
Calvin Owens98c56ad2016-07-28 21:38:21 -07005423_base_wait_on_iocstate(struct MPT3SAS_ADAPTER *ioc, u32 ioc_state, int timeout)
Sreekanth Reddyf92363d2012-11-30 07:44:21 +05305424{
5425 u32 count, cntdn;
5426 u32 current_state;
5427
5428 count = 0;
Calvin Owens98c56ad2016-07-28 21:38:21 -07005429 cntdn = 1000 * timeout;
Sreekanth Reddyf92363d2012-11-30 07:44:21 +05305430 do {
5431 current_state = mpt3sas_base_get_iocstate(ioc, 1);
5432 if (current_state == ioc_state)
5433 return 0;
5434 if (count && current_state == MPI2_IOC_STATE_FAULT)
5435 break;
Calvin Owens98c56ad2016-07-28 21:38:21 -07005436
5437 usleep_range(1000, 1500);
Sreekanth Reddyf92363d2012-11-30 07:44:21 +05305438 count++;
5439 } while (--cntdn);
5440
5441 return current_state;
5442}
5443
5444/**
5445 * _base_wait_for_doorbell_int - waiting for controller interrupt(generated by
5446 * a write to the doorbell)
5447 * @ioc: per adapter object
Sreekanth Reddyf92363d2012-11-30 07:44:21 +05305448 *
Bart Van Assche4beb4862018-06-15 14:42:01 -07005449 * Return: 0 for success, non-zero for failure.
Sreekanth Reddyf92363d2012-11-30 07:44:21 +05305450 *
5451 * Notes: MPI2_HIS_IOC2SYS_DB_STATUS - set to one when IOC writes to doorbell.
5452 */
Sreekanth Reddy4dc8c802015-06-30 12:24:48 +05305453
5454static int
Calvin Owens98c56ad2016-07-28 21:38:21 -07005455_base_wait_for_doorbell_int(struct MPT3SAS_ADAPTER *ioc, int timeout)
Sreekanth Reddyf92363d2012-11-30 07:44:21 +05305456{
5457 u32 cntdn, count;
5458 u32 int_status;
5459
5460 count = 0;
Calvin Owens98c56ad2016-07-28 21:38:21 -07005461 cntdn = 1000 * timeout;
Sreekanth Reddyf92363d2012-11-30 07:44:21 +05305462 do {
Suganath Prabu306eaf22018-12-07 12:58:34 +05305463 int_status = ioc->base_readl(&ioc->chip->HostInterruptStatus);
Sreekanth Reddyf92363d2012-11-30 07:44:21 +05305464 if (int_status & MPI2_HIS_IOC2SYS_DB_STATUS) {
Joe Perches919d8a32018-09-17 08:01:09 -07005465 dhsprintk(ioc,
5466 ioc_info(ioc, "%s: successful count(%d), timeout(%d)\n",
5467 __func__, count, timeout));
Sreekanth Reddyf92363d2012-11-30 07:44:21 +05305468 return 0;
5469 }
Calvin Owens98c56ad2016-07-28 21:38:21 -07005470
5471 usleep_range(1000, 1500);
Sreekanth Reddyf92363d2012-11-30 07:44:21 +05305472 count++;
5473 } while (--cntdn);
5474
Joe Perches919d8a32018-09-17 08:01:09 -07005475 ioc_err(ioc, "%s: failed due to timeout count(%d), int_status(%x)!\n",
5476 __func__, count, int_status);
Sreekanth Reddyf92363d2012-11-30 07:44:21 +05305477 return -EFAULT;
5478}
5479
Calvin Owens98c56ad2016-07-28 21:38:21 -07005480static int
5481_base_spin_on_doorbell_int(struct MPT3SAS_ADAPTER *ioc, int timeout)
5482{
5483 u32 cntdn, count;
5484 u32 int_status;
5485
5486 count = 0;
5487 cntdn = 2000 * timeout;
5488 do {
Suganath Prabu306eaf22018-12-07 12:58:34 +05305489 int_status = ioc->base_readl(&ioc->chip->HostInterruptStatus);
Calvin Owens98c56ad2016-07-28 21:38:21 -07005490 if (int_status & MPI2_HIS_IOC2SYS_DB_STATUS) {
Joe Perches919d8a32018-09-17 08:01:09 -07005491 dhsprintk(ioc,
5492 ioc_info(ioc, "%s: successful count(%d), timeout(%d)\n",
5493 __func__, count, timeout));
Calvin Owens98c56ad2016-07-28 21:38:21 -07005494 return 0;
5495 }
5496
5497 udelay(500);
5498 count++;
5499 } while (--cntdn);
5500
Joe Perches919d8a32018-09-17 08:01:09 -07005501 ioc_err(ioc, "%s: failed due to timeout count(%d), int_status(%x)!\n",
5502 __func__, count, int_status);
Calvin Owens98c56ad2016-07-28 21:38:21 -07005503 return -EFAULT;
5504
5505}
5506
Sreekanth Reddyf92363d2012-11-30 07:44:21 +05305507/**
5508 * _base_wait_for_doorbell_ack - waiting for controller to read the doorbell.
5509 * @ioc: per adapter object
5510 * @timeout: timeout in second
Sreekanth Reddyf92363d2012-11-30 07:44:21 +05305511 *
Bart Van Assche4beb4862018-06-15 14:42:01 -07005512 * Return: 0 for success, non-zero for failure.
Sreekanth Reddyf92363d2012-11-30 07:44:21 +05305513 *
5514 * Notes: MPI2_HIS_SYS2IOC_DB_STATUS - set to one when host writes to
5515 * doorbell.
5516 */
5517static int
Calvin Owens98c56ad2016-07-28 21:38:21 -07005518_base_wait_for_doorbell_ack(struct MPT3SAS_ADAPTER *ioc, int timeout)
Sreekanth Reddyf92363d2012-11-30 07:44:21 +05305519{
5520 u32 cntdn, count;
5521 u32 int_status;
5522 u32 doorbell;
5523
5524 count = 0;
Calvin Owens98c56ad2016-07-28 21:38:21 -07005525 cntdn = 1000 * timeout;
Sreekanth Reddyf92363d2012-11-30 07:44:21 +05305526 do {
Suganath Prabu306eaf22018-12-07 12:58:34 +05305527 int_status = ioc->base_readl(&ioc->chip->HostInterruptStatus);
Sreekanth Reddyf92363d2012-11-30 07:44:21 +05305528 if (!(int_status & MPI2_HIS_SYS2IOC_DB_STATUS)) {
Joe Perches919d8a32018-09-17 08:01:09 -07005529 dhsprintk(ioc,
5530 ioc_info(ioc, "%s: successful count(%d), timeout(%d)\n",
5531 __func__, count, timeout));
Sreekanth Reddyf92363d2012-11-30 07:44:21 +05305532 return 0;
5533 } else if (int_status & MPI2_HIS_IOC2SYS_DB_STATUS) {
Suganath Prabu306eaf22018-12-07 12:58:34 +05305534 doorbell = ioc->base_readl(&ioc->chip->Doorbell);
Sreekanth Reddyf92363d2012-11-30 07:44:21 +05305535 if ((doorbell & MPI2_IOC_STATE_MASK) ==
5536 MPI2_IOC_STATE_FAULT) {
5537 mpt3sas_base_fault_info(ioc , doorbell);
5538 return -EFAULT;
5539 }
5540 } else if (int_status == 0xFFFFFFFF)
5541 goto out;
5542
Calvin Owens98c56ad2016-07-28 21:38:21 -07005543 usleep_range(1000, 1500);
Sreekanth Reddyf92363d2012-11-30 07:44:21 +05305544 count++;
5545 } while (--cntdn);
5546
5547 out:
Joe Perches919d8a32018-09-17 08:01:09 -07005548 ioc_err(ioc, "%s: failed due to timeout count(%d), int_status(%x)!\n",
5549 __func__, count, int_status);
Sreekanth Reddyf92363d2012-11-30 07:44:21 +05305550 return -EFAULT;
5551}
5552
5553/**
5554 * _base_wait_for_doorbell_not_used - waiting for doorbell to not be in use
5555 * @ioc: per adapter object
5556 * @timeout: timeout in second
Sreekanth Reddyf92363d2012-11-30 07:44:21 +05305557 *
Bart Van Assche4beb4862018-06-15 14:42:01 -07005558 * Return: 0 for success, non-zero for failure.
Sreekanth Reddyf92363d2012-11-30 07:44:21 +05305559 */
5560static int
Calvin Owens98c56ad2016-07-28 21:38:21 -07005561_base_wait_for_doorbell_not_used(struct MPT3SAS_ADAPTER *ioc, int timeout)
Sreekanth Reddyf92363d2012-11-30 07:44:21 +05305562{
5563 u32 cntdn, count;
5564 u32 doorbell_reg;
5565
5566 count = 0;
Calvin Owens98c56ad2016-07-28 21:38:21 -07005567 cntdn = 1000 * timeout;
Sreekanth Reddyf92363d2012-11-30 07:44:21 +05305568 do {
Suganath Prabu306eaf22018-12-07 12:58:34 +05305569 doorbell_reg = ioc->base_readl(&ioc->chip->Doorbell);
Sreekanth Reddyf92363d2012-11-30 07:44:21 +05305570 if (!(doorbell_reg & MPI2_DOORBELL_USED)) {
Joe Perches919d8a32018-09-17 08:01:09 -07005571 dhsprintk(ioc,
5572 ioc_info(ioc, "%s: successful count(%d), timeout(%d)\n",
5573 __func__, count, timeout));
Sreekanth Reddyf92363d2012-11-30 07:44:21 +05305574 return 0;
5575 }
Calvin Owens98c56ad2016-07-28 21:38:21 -07005576
5577 usleep_range(1000, 1500);
Sreekanth Reddyf92363d2012-11-30 07:44:21 +05305578 count++;
5579 } while (--cntdn);
5580
Joe Perches919d8a32018-09-17 08:01:09 -07005581 ioc_err(ioc, "%s: failed due to timeout count(%d), doorbell_reg(%x)!\n",
5582 __func__, count, doorbell_reg);
Sreekanth Reddyf92363d2012-11-30 07:44:21 +05305583 return -EFAULT;
5584}
5585
5586/**
5587 * _base_send_ioc_reset - send doorbell reset
5588 * @ioc: per adapter object
5589 * @reset_type: currently only supports: MPI2_FUNCTION_IOC_MESSAGE_UNIT_RESET
5590 * @timeout: timeout in second
Sreekanth Reddyf92363d2012-11-30 07:44:21 +05305591 *
Bart Van Assche4beb4862018-06-15 14:42:01 -07005592 * Return: 0 for success, non-zero for failure.
Sreekanth Reddyf92363d2012-11-30 07:44:21 +05305593 */
5594static int
Calvin Owens98c56ad2016-07-28 21:38:21 -07005595_base_send_ioc_reset(struct MPT3SAS_ADAPTER *ioc, u8 reset_type, int timeout)
Sreekanth Reddyf92363d2012-11-30 07:44:21 +05305596{
5597 u32 ioc_state;
5598 int r = 0;
5599
5600 if (reset_type != MPI2_FUNCTION_IOC_MESSAGE_UNIT_RESET) {
Joe Perches919d8a32018-09-17 08:01:09 -07005601 ioc_err(ioc, "%s: unknown reset_type\n", __func__);
Sreekanth Reddyf92363d2012-11-30 07:44:21 +05305602 return -EFAULT;
5603 }
5604
5605 if (!(ioc->facts.IOCCapabilities &
5606 MPI2_IOCFACTS_CAPABILITY_EVENT_REPLAY))
5607 return -EFAULT;
5608
Joe Perches919d8a32018-09-17 08:01:09 -07005609 ioc_info(ioc, "sending message unit reset !!\n");
Sreekanth Reddyf92363d2012-11-30 07:44:21 +05305610
5611 writel(reset_type << MPI2_DOORBELL_FUNCTION_SHIFT,
5612 &ioc->chip->Doorbell);
Calvin Owens98c56ad2016-07-28 21:38:21 -07005613 if ((_base_wait_for_doorbell_ack(ioc, 15))) {
Sreekanth Reddyf92363d2012-11-30 07:44:21 +05305614 r = -EFAULT;
5615 goto out;
5616 }
Calvin Owens98c56ad2016-07-28 21:38:21 -07005617 ioc_state = _base_wait_on_iocstate(ioc, MPI2_IOC_STATE_READY, timeout);
Sreekanth Reddyf92363d2012-11-30 07:44:21 +05305618 if (ioc_state) {
Joe Perches919d8a32018-09-17 08:01:09 -07005619 ioc_err(ioc, "%s: failed going to ready state (ioc_state=0x%x)\n",
5620 __func__, ioc_state);
Sreekanth Reddyf92363d2012-11-30 07:44:21 +05305621 r = -EFAULT;
5622 goto out;
5623 }
5624 out:
Joe Perches919d8a32018-09-17 08:01:09 -07005625 ioc_info(ioc, "message unit reset: %s\n",
5626 r == 0 ? "SUCCESS" : "FAILED");
Sreekanth Reddyf92363d2012-11-30 07:44:21 +05305627 return r;
5628}
5629
5630/**
Suganath Prabuf4305742018-10-31 18:53:33 +05305631 * mpt3sas_wait_for_ioc - IOC's operational state is checked here.
5632 * @ioc: per adapter object
5633 * @wait_count: timeout in seconds
5634 *
5635 * Return: Waits up to timeout seconds for the IOC to
5636 * become operational. Returns 0 if IOC is present
5637 * and operational; otherwise returns -EFAULT.
5638 */
5639
5640int
5641mpt3sas_wait_for_ioc(struct MPT3SAS_ADAPTER *ioc, int timeout)
5642{
5643 int wait_state_count = 0;
5644 u32 ioc_state;
5645
Suganath Prabua064a642018-10-31 18:53:34 +05305646 do {
Suganath Prabuf4305742018-10-31 18:53:33 +05305647 ioc_state = mpt3sas_base_get_iocstate(ioc, 1);
Suganath Prabua064a642018-10-31 18:53:34 +05305648 if (ioc_state == MPI2_IOC_STATE_OPERATIONAL)
5649 break;
5650 ssleep(1);
Suganath Prabuf4305742018-10-31 18:53:33 +05305651 ioc_info(ioc, "%s: waiting for operational state(count=%d)\n",
Suganath Prabua064a642018-10-31 18:53:34 +05305652 __func__, ++wait_state_count);
5653 } while (--timeout);
5654 if (!timeout) {
5655 ioc_err(ioc, "%s: failed due to ioc not operational\n", __func__);
5656 return -EFAULT;
Suganath Prabuf4305742018-10-31 18:53:33 +05305657 }
5658 if (wait_state_count)
5659 ioc_info(ioc, "ioc is operational\n");
Suganath Prabuf4305742018-10-31 18:53:33 +05305660 return 0;
5661}
5662
5663/**
Sreekanth Reddyf92363d2012-11-30 07:44:21 +05305664 * _base_handshake_req_reply_wait - send request thru doorbell interface
5665 * @ioc: per adapter object
5666 * @request_bytes: request length
5667 * @request: pointer having request payload
5668 * @reply_bytes: reply length
5669 * @reply: pointer to reply payload
5670 * @timeout: timeout in second
Sreekanth Reddyf92363d2012-11-30 07:44:21 +05305671 *
Bart Van Assche4beb4862018-06-15 14:42:01 -07005672 * Return: 0 for success, non-zero for failure.
Sreekanth Reddyf92363d2012-11-30 07:44:21 +05305673 */
5674static int
5675_base_handshake_req_reply_wait(struct MPT3SAS_ADAPTER *ioc, int request_bytes,
Calvin Owens98c56ad2016-07-28 21:38:21 -07005676 u32 *request, int reply_bytes, u16 *reply, int timeout)
Sreekanth Reddyf92363d2012-11-30 07:44:21 +05305677{
5678 MPI2DefaultReply_t *default_reply = (MPI2DefaultReply_t *)reply;
5679 int i;
5680 u8 failed;
Sreekanth Reddyf92363d2012-11-30 07:44:21 +05305681 __le32 *mfp;
5682
5683 /* make sure doorbell is not in use */
Suganath Prabu306eaf22018-12-07 12:58:34 +05305684 if ((ioc->base_readl(&ioc->chip->Doorbell) & MPI2_DOORBELL_USED)) {
Joe Perches919d8a32018-09-17 08:01:09 -07005685 ioc_err(ioc, "doorbell is in use (line=%d)\n", __LINE__);
Sreekanth Reddyf92363d2012-11-30 07:44:21 +05305686 return -EFAULT;
5687 }
5688
5689 /* clear pending doorbell interrupts from previous state changes */
Suganath Prabu306eaf22018-12-07 12:58:34 +05305690 if (ioc->base_readl(&ioc->chip->HostInterruptStatus) &
Sreekanth Reddyf92363d2012-11-30 07:44:21 +05305691 MPI2_HIS_IOC2SYS_DB_STATUS)
5692 writel(0, &ioc->chip->HostInterruptStatus);
5693
5694 /* send message to ioc */
5695 writel(((MPI2_FUNCTION_HANDSHAKE<<MPI2_DOORBELL_FUNCTION_SHIFT) |
5696 ((request_bytes/4)<<MPI2_DOORBELL_ADD_DWORDS_SHIFT)),
5697 &ioc->chip->Doorbell);
5698
Calvin Owens98c56ad2016-07-28 21:38:21 -07005699 if ((_base_spin_on_doorbell_int(ioc, 5))) {
Joe Perches919d8a32018-09-17 08:01:09 -07005700 ioc_err(ioc, "doorbell handshake int failed (line=%d)\n",
5701 __LINE__);
Sreekanth Reddyf92363d2012-11-30 07:44:21 +05305702 return -EFAULT;
5703 }
5704 writel(0, &ioc->chip->HostInterruptStatus);
5705
Calvin Owens98c56ad2016-07-28 21:38:21 -07005706 if ((_base_wait_for_doorbell_ack(ioc, 5))) {
Joe Perches919d8a32018-09-17 08:01:09 -07005707 ioc_err(ioc, "doorbell handshake ack failed (line=%d)\n",
5708 __LINE__);
Sreekanth Reddyf92363d2012-11-30 07:44:21 +05305709 return -EFAULT;
5710 }
5711
5712 /* send message 32-bits at a time */
5713 for (i = 0, failed = 0; i < request_bytes/4 && !failed; i++) {
Sreekanth Reddy09c2f952018-07-31 01:36:36 -04005714 writel(cpu_to_le32(request[i]), &ioc->chip->Doorbell);
Calvin Owens98c56ad2016-07-28 21:38:21 -07005715 if ((_base_wait_for_doorbell_ack(ioc, 5)))
Sreekanth Reddyf92363d2012-11-30 07:44:21 +05305716 failed = 1;
5717 }
5718
5719 if (failed) {
Joe Perches919d8a32018-09-17 08:01:09 -07005720 ioc_err(ioc, "doorbell handshake sending request failed (line=%d)\n",
5721 __LINE__);
Sreekanth Reddyf92363d2012-11-30 07:44:21 +05305722 return -EFAULT;
5723 }
5724
5725 /* now wait for the reply */
Calvin Owens98c56ad2016-07-28 21:38:21 -07005726 if ((_base_wait_for_doorbell_int(ioc, timeout))) {
Joe Perches919d8a32018-09-17 08:01:09 -07005727 ioc_err(ioc, "doorbell handshake int failed (line=%d)\n",
5728 __LINE__);
Sreekanth Reddyf92363d2012-11-30 07:44:21 +05305729 return -EFAULT;
5730 }
5731
5732 /* read the first two 16-bits, it gives the total length of the reply */
Suganath Prabu306eaf22018-12-07 12:58:34 +05305733 reply[0] = le16_to_cpu(ioc->base_readl(&ioc->chip->Doorbell)
Sreekanth Reddyf92363d2012-11-30 07:44:21 +05305734 & MPI2_DOORBELL_DATA_MASK);
5735 writel(0, &ioc->chip->HostInterruptStatus);
Calvin Owens98c56ad2016-07-28 21:38:21 -07005736 if ((_base_wait_for_doorbell_int(ioc, 5))) {
Joe Perches919d8a32018-09-17 08:01:09 -07005737 ioc_err(ioc, "doorbell handshake int failed (line=%d)\n",
5738 __LINE__);
Sreekanth Reddyf92363d2012-11-30 07:44:21 +05305739 return -EFAULT;
5740 }
Suganath Prabu306eaf22018-12-07 12:58:34 +05305741 reply[1] = le16_to_cpu(ioc->base_readl(&ioc->chip->Doorbell)
Sreekanth Reddyf92363d2012-11-30 07:44:21 +05305742 & MPI2_DOORBELL_DATA_MASK);
5743 writel(0, &ioc->chip->HostInterruptStatus);
5744
5745 for (i = 2; i < default_reply->MsgLength * 2; i++) {
Calvin Owens98c56ad2016-07-28 21:38:21 -07005746 if ((_base_wait_for_doorbell_int(ioc, 5))) {
Joe Perches919d8a32018-09-17 08:01:09 -07005747 ioc_err(ioc, "doorbell handshake int failed (line=%d)\n",
5748 __LINE__);
Sreekanth Reddyf92363d2012-11-30 07:44:21 +05305749 return -EFAULT;
5750 }
5751 if (i >= reply_bytes/2) /* overflow case */
Suganath Prabu306eaf22018-12-07 12:58:34 +05305752 ioc->base_readl(&ioc->chip->Doorbell);
Sreekanth Reddyf92363d2012-11-30 07:44:21 +05305753 else
Suganath Prabu306eaf22018-12-07 12:58:34 +05305754 reply[i] = le16_to_cpu(
5755 ioc->base_readl(&ioc->chip->Doorbell)
Sreekanth Reddyf92363d2012-11-30 07:44:21 +05305756 & MPI2_DOORBELL_DATA_MASK);
5757 writel(0, &ioc->chip->HostInterruptStatus);
5758 }
5759
Calvin Owens98c56ad2016-07-28 21:38:21 -07005760 _base_wait_for_doorbell_int(ioc, 5);
5761 if (_base_wait_for_doorbell_not_used(ioc, 5) != 0) {
Joe Perches919d8a32018-09-17 08:01:09 -07005762 dhsprintk(ioc,
5763 ioc_info(ioc, "doorbell is in use (line=%d)\n",
5764 __LINE__));
Sreekanth Reddyf92363d2012-11-30 07:44:21 +05305765 }
5766 writel(0, &ioc->chip->HostInterruptStatus);
5767
5768 if (ioc->logging_level & MPT_DEBUG_INIT) {
5769 mfp = (__le32 *)reply;
5770 pr_info("\toffset:data\n");
5771 for (i = 0; i < reply_bytes/4; i++)
5772 pr_info("\t[0x%02x]:%08x\n", i*4,
5773 le32_to_cpu(mfp[i]));
5774 }
5775 return 0;
5776}
5777
5778/**
5779 * mpt3sas_base_sas_iounit_control - send sas iounit control to FW
5780 * @ioc: per adapter object
5781 * @mpi_reply: the reply payload from FW
5782 * @mpi_request: the request payload sent to FW
5783 *
5784 * The SAS IO Unit Control Request message allows the host to perform low-level
5785 * operations, such as resets on the PHYs of the IO Unit, also allows the host
5786 * to obtain the IOC assigned device handles for a device if it has other
5787 * identifying information about the device, in addition allows the host to
5788 * remove IOC resources associated with the device.
5789 *
Bart Van Assche4beb4862018-06-15 14:42:01 -07005790 * Return: 0 for success, non-zero for failure.
Sreekanth Reddyf92363d2012-11-30 07:44:21 +05305791 */
5792int
5793mpt3sas_base_sas_iounit_control(struct MPT3SAS_ADAPTER *ioc,
5794 Mpi2SasIoUnitControlReply_t *mpi_reply,
5795 Mpi2SasIoUnitControlRequest_t *mpi_request)
5796{
5797 u16 smid;
Chaitra P Bd37306c2018-05-31 06:34:50 -04005798 u8 issue_reset = 0;
Sreekanth Reddyf92363d2012-11-30 07:44:21 +05305799 int rc;
5800 void *request;
Sreekanth Reddyf92363d2012-11-30 07:44:21 +05305801
Joe Perches919d8a32018-09-17 08:01:09 -07005802 dinitprintk(ioc, ioc_info(ioc, "%s\n", __func__));
Sreekanth Reddyf92363d2012-11-30 07:44:21 +05305803
5804 mutex_lock(&ioc->base_cmds.mutex);
5805
5806 if (ioc->base_cmds.status != MPT3_CMD_NOT_USED) {
Joe Perches919d8a32018-09-17 08:01:09 -07005807 ioc_err(ioc, "%s: base_cmd in use\n", __func__);
Sreekanth Reddyf92363d2012-11-30 07:44:21 +05305808 rc = -EAGAIN;
5809 goto out;
5810 }
5811
Suganath Prabuf4305742018-10-31 18:53:33 +05305812 rc = mpt3sas_wait_for_ioc(ioc, IOC_OPERATIONAL_WAIT_COUNT);
5813 if (rc)
5814 goto out;
Sreekanth Reddyf92363d2012-11-30 07:44:21 +05305815
5816 smid = mpt3sas_base_get_smid(ioc, ioc->base_cb_idx);
5817 if (!smid) {
Joe Perches919d8a32018-09-17 08:01:09 -07005818 ioc_err(ioc, "%s: failed obtaining a smid\n", __func__);
Sreekanth Reddyf92363d2012-11-30 07:44:21 +05305819 rc = -EAGAIN;
5820 goto out;
5821 }
5822
5823 rc = 0;
5824 ioc->base_cmds.status = MPT3_CMD_PENDING;
5825 request = mpt3sas_base_get_msg_frame(ioc, smid);
5826 ioc->base_cmds.smid = smid;
5827 memcpy(request, mpi_request, sizeof(Mpi2SasIoUnitControlRequest_t));
5828 if (mpi_request->Operation == MPI2_SAS_OP_PHY_HARD_RESET ||
5829 mpi_request->Operation == MPI2_SAS_OP_PHY_LINK_RESET)
5830 ioc->ioc_link_reset_in_progress = 1;
5831 init_completion(&ioc->base_cmds.done);
Suganath Prabu S078a4cc2019-05-31 08:14:34 -04005832 ioc->put_smid_default(ioc, smid);
Calvin Owens8bbb1cf2016-07-28 21:38:22 -07005833 wait_for_completion_timeout(&ioc->base_cmds.done,
Sreekanth Reddyf92363d2012-11-30 07:44:21 +05305834 msecs_to_jiffies(10000));
5835 if ((mpi_request->Operation == MPI2_SAS_OP_PHY_HARD_RESET ||
5836 mpi_request->Operation == MPI2_SAS_OP_PHY_LINK_RESET) &&
5837 ioc->ioc_link_reset_in_progress)
5838 ioc->ioc_link_reset_in_progress = 0;
5839 if (!(ioc->base_cmds.status & MPT3_CMD_COMPLETE)) {
Chaitra P Bd37306c2018-05-31 06:34:50 -04005840 issue_reset =
5841 mpt3sas_base_check_cmd_timeout(ioc,
5842 ioc->base_cmds.status, mpi_request,
5843 sizeof(Mpi2SasIoUnitControlRequest_t)/4);
Sreekanth Reddyf92363d2012-11-30 07:44:21 +05305844 goto issue_host_reset;
5845 }
5846 if (ioc->base_cmds.status & MPT3_CMD_REPLY_VALID)
5847 memcpy(mpi_reply, ioc->base_cmds.reply,
5848 sizeof(Mpi2SasIoUnitControlReply_t));
5849 else
5850 memset(mpi_reply, 0, sizeof(Mpi2SasIoUnitControlReply_t));
5851 ioc->base_cmds.status = MPT3_CMD_NOT_USED;
5852 goto out;
5853
5854 issue_host_reset:
5855 if (issue_reset)
Calvin Owens98c56ad2016-07-28 21:38:21 -07005856 mpt3sas_base_hard_reset_handler(ioc, FORCE_BIG_HAMMER);
Sreekanth Reddyf92363d2012-11-30 07:44:21 +05305857 ioc->base_cmds.status = MPT3_CMD_NOT_USED;
5858 rc = -EFAULT;
5859 out:
5860 mutex_unlock(&ioc->base_cmds.mutex);
5861 return rc;
5862}
5863
5864/**
5865 * mpt3sas_base_scsi_enclosure_processor - sending request to sep device
5866 * @ioc: per adapter object
5867 * @mpi_reply: the reply payload from FW
5868 * @mpi_request: the request payload sent to FW
5869 *
5870 * The SCSI Enclosure Processor request message causes the IOC to
5871 * communicate with SES devices to control LED status signals.
5872 *
Bart Van Assche4beb4862018-06-15 14:42:01 -07005873 * Return: 0 for success, non-zero for failure.
Sreekanth Reddyf92363d2012-11-30 07:44:21 +05305874 */
5875int
5876mpt3sas_base_scsi_enclosure_processor(struct MPT3SAS_ADAPTER *ioc,
5877 Mpi2SepReply_t *mpi_reply, Mpi2SepRequest_t *mpi_request)
5878{
5879 u16 smid;
Chaitra P Bd37306c2018-05-31 06:34:50 -04005880 u8 issue_reset = 0;
Sreekanth Reddyf92363d2012-11-30 07:44:21 +05305881 int rc;
5882 void *request;
Sreekanth Reddyf92363d2012-11-30 07:44:21 +05305883
Joe Perches919d8a32018-09-17 08:01:09 -07005884 dinitprintk(ioc, ioc_info(ioc, "%s\n", __func__));
Sreekanth Reddyf92363d2012-11-30 07:44:21 +05305885
5886 mutex_lock(&ioc->base_cmds.mutex);
5887
5888 if (ioc->base_cmds.status != MPT3_CMD_NOT_USED) {
Joe Perches919d8a32018-09-17 08:01:09 -07005889 ioc_err(ioc, "%s: base_cmd in use\n", __func__);
Sreekanth Reddyf92363d2012-11-30 07:44:21 +05305890 rc = -EAGAIN;
5891 goto out;
5892 }
5893
Suganath Prabuf4305742018-10-31 18:53:33 +05305894 rc = mpt3sas_wait_for_ioc(ioc, IOC_OPERATIONAL_WAIT_COUNT);
5895 if (rc)
5896 goto out;
Sreekanth Reddyf92363d2012-11-30 07:44:21 +05305897
5898 smid = mpt3sas_base_get_smid(ioc, ioc->base_cb_idx);
5899 if (!smid) {
Joe Perches919d8a32018-09-17 08:01:09 -07005900 ioc_err(ioc, "%s: failed obtaining a smid\n", __func__);
Sreekanth Reddyf92363d2012-11-30 07:44:21 +05305901 rc = -EAGAIN;
5902 goto out;
5903 }
5904
5905 rc = 0;
5906 ioc->base_cmds.status = MPT3_CMD_PENDING;
5907 request = mpt3sas_base_get_msg_frame(ioc, smid);
5908 ioc->base_cmds.smid = smid;
Suganath Prabue224e032019-08-03 09:59:47 -04005909 memset(request, 0, ioc->request_sz);
Sreekanth Reddyf92363d2012-11-30 07:44:21 +05305910 memcpy(request, mpi_request, sizeof(Mpi2SepReply_t));
5911 init_completion(&ioc->base_cmds.done);
Suganath Prabu S078a4cc2019-05-31 08:14:34 -04005912 ioc->put_smid_default(ioc, smid);
Calvin Owens8bbb1cf2016-07-28 21:38:22 -07005913 wait_for_completion_timeout(&ioc->base_cmds.done,
Sreekanth Reddyf92363d2012-11-30 07:44:21 +05305914 msecs_to_jiffies(10000));
5915 if (!(ioc->base_cmds.status & MPT3_CMD_COMPLETE)) {
Chaitra P Bd37306c2018-05-31 06:34:50 -04005916 issue_reset =
5917 mpt3sas_base_check_cmd_timeout(ioc,
5918 ioc->base_cmds.status, mpi_request,
5919 sizeof(Mpi2SepRequest_t)/4);
Sreekanth Reddyf92363d2012-11-30 07:44:21 +05305920 goto issue_host_reset;
5921 }
5922 if (ioc->base_cmds.status & MPT3_CMD_REPLY_VALID)
5923 memcpy(mpi_reply, ioc->base_cmds.reply,
5924 sizeof(Mpi2SepReply_t));
5925 else
5926 memset(mpi_reply, 0, sizeof(Mpi2SepReply_t));
5927 ioc->base_cmds.status = MPT3_CMD_NOT_USED;
5928 goto out;
5929
5930 issue_host_reset:
5931 if (issue_reset)
Calvin Owens98c56ad2016-07-28 21:38:21 -07005932 mpt3sas_base_hard_reset_handler(ioc, FORCE_BIG_HAMMER);
Sreekanth Reddyf92363d2012-11-30 07:44:21 +05305933 ioc->base_cmds.status = MPT3_CMD_NOT_USED;
5934 rc = -EFAULT;
5935 out:
5936 mutex_unlock(&ioc->base_cmds.mutex);
5937 return rc;
5938}
5939
5940/**
5941 * _base_get_port_facts - obtain port facts reply and save in ioc
5942 * @ioc: per adapter object
Bart Van Assche4beb4862018-06-15 14:42:01 -07005943 * @port: ?
Sreekanth Reddyf92363d2012-11-30 07:44:21 +05305944 *
Bart Van Assche4beb4862018-06-15 14:42:01 -07005945 * Return: 0 for success, non-zero for failure.
Sreekanth Reddyf92363d2012-11-30 07:44:21 +05305946 */
5947static int
Calvin Owens98c56ad2016-07-28 21:38:21 -07005948_base_get_port_facts(struct MPT3SAS_ADAPTER *ioc, int port)
Sreekanth Reddyf92363d2012-11-30 07:44:21 +05305949{
5950 Mpi2PortFactsRequest_t mpi_request;
5951 Mpi2PortFactsReply_t mpi_reply;
5952 struct mpt3sas_port_facts *pfacts;
5953 int mpi_reply_sz, mpi_request_sz, r;
5954
Joe Perches919d8a32018-09-17 08:01:09 -07005955 dinitprintk(ioc, ioc_info(ioc, "%s\n", __func__));
Sreekanth Reddyf92363d2012-11-30 07:44:21 +05305956
5957 mpi_reply_sz = sizeof(Mpi2PortFactsReply_t);
5958 mpi_request_sz = sizeof(Mpi2PortFactsRequest_t);
5959 memset(&mpi_request, 0, mpi_request_sz);
5960 mpi_request.Function = MPI2_FUNCTION_PORT_FACTS;
5961 mpi_request.PortNumber = port;
5962 r = _base_handshake_req_reply_wait(ioc, mpi_request_sz,
Calvin Owens98c56ad2016-07-28 21:38:21 -07005963 (u32 *)&mpi_request, mpi_reply_sz, (u16 *)&mpi_reply, 5);
Sreekanth Reddyf92363d2012-11-30 07:44:21 +05305964
5965 if (r != 0) {
Joe Perches919d8a32018-09-17 08:01:09 -07005966 ioc_err(ioc, "%s: handshake failed (r=%d)\n", __func__, r);
Sreekanth Reddyf92363d2012-11-30 07:44:21 +05305967 return r;
5968 }
5969
5970 pfacts = &ioc->pfacts[port];
5971 memset(pfacts, 0, sizeof(struct mpt3sas_port_facts));
5972 pfacts->PortNumber = mpi_reply.PortNumber;
5973 pfacts->VP_ID = mpi_reply.VP_ID;
5974 pfacts->VF_ID = mpi_reply.VF_ID;
5975 pfacts->MaxPostedCmdBuffers =
5976 le16_to_cpu(mpi_reply.MaxPostedCmdBuffers);
5977
5978 return 0;
5979}
5980
5981/**
Sreekanth Reddy4dc8c802015-06-30 12:24:48 +05305982 * _base_wait_for_iocstate - Wait until the card is in READY or OPERATIONAL
5983 * @ioc: per adapter object
5984 * @timeout:
Sreekanth Reddy4dc8c802015-06-30 12:24:48 +05305985 *
Bart Van Assche4beb4862018-06-15 14:42:01 -07005986 * Return: 0 for success, non-zero for failure.
Sreekanth Reddy4dc8c802015-06-30 12:24:48 +05305987 */
5988static int
Calvin Owens98c56ad2016-07-28 21:38:21 -07005989_base_wait_for_iocstate(struct MPT3SAS_ADAPTER *ioc, int timeout)
Sreekanth Reddy4dc8c802015-06-30 12:24:48 +05305990{
5991 u32 ioc_state;
5992 int rc;
5993
Joe Perches1f95a472018-09-17 08:01:13 -07005994 dinitprintk(ioc, ioc_info(ioc, "%s\n", __func__));
Sreekanth Reddy4dc8c802015-06-30 12:24:48 +05305995
5996 if (ioc->pci_error_recovery) {
Joe Perches1f95a472018-09-17 08:01:13 -07005997 dfailprintk(ioc,
5998 ioc_info(ioc, "%s: host in pci error recovery\n",
5999 __func__));
Sreekanth Reddy4dc8c802015-06-30 12:24:48 +05306000 return -EFAULT;
6001 }
6002
6003 ioc_state = mpt3sas_base_get_iocstate(ioc, 0);
Joe Perches1f95a472018-09-17 08:01:13 -07006004 dhsprintk(ioc,
6005 ioc_info(ioc, "%s: ioc_state(0x%08x)\n",
6006 __func__, ioc_state));
Sreekanth Reddy4dc8c802015-06-30 12:24:48 +05306007
6008 if (((ioc_state & MPI2_IOC_STATE_MASK) == MPI2_IOC_STATE_READY) ||
6009 (ioc_state & MPI2_IOC_STATE_MASK) == MPI2_IOC_STATE_OPERATIONAL)
6010 return 0;
6011
6012 if (ioc_state & MPI2_DOORBELL_USED) {
Joe Perches1f95a472018-09-17 08:01:13 -07006013 dhsprintk(ioc, ioc_info(ioc, "unexpected doorbell active!\n"));
Sreekanth Reddy4dc8c802015-06-30 12:24:48 +05306014 goto issue_diag_reset;
6015 }
6016
6017 if ((ioc_state & MPI2_IOC_STATE_MASK) == MPI2_IOC_STATE_FAULT) {
6018 mpt3sas_base_fault_info(ioc, ioc_state &
6019 MPI2_DOORBELL_DATA_MASK);
6020 goto issue_diag_reset;
6021 }
6022
Calvin Owens98c56ad2016-07-28 21:38:21 -07006023 ioc_state = _base_wait_on_iocstate(ioc, MPI2_IOC_STATE_READY, timeout);
Sreekanth Reddy4dc8c802015-06-30 12:24:48 +05306024 if (ioc_state) {
Joe Perches1f95a472018-09-17 08:01:13 -07006025 dfailprintk(ioc,
6026 ioc_info(ioc, "%s: failed going to ready state (ioc_state=0x%x)\n",
6027 __func__, ioc_state));
Sreekanth Reddy4dc8c802015-06-30 12:24:48 +05306028 return -EFAULT;
6029 }
6030
6031 issue_diag_reset:
Calvin Owens98c56ad2016-07-28 21:38:21 -07006032 rc = _base_diag_reset(ioc);
Sreekanth Reddy4dc8c802015-06-30 12:24:48 +05306033 return rc;
6034}
6035
6036/**
Sreekanth Reddyf92363d2012-11-30 07:44:21 +05306037 * _base_get_ioc_facts - obtain ioc facts reply and save in ioc
6038 * @ioc: per adapter object
Sreekanth Reddyf92363d2012-11-30 07:44:21 +05306039 *
Bart Van Assche4beb4862018-06-15 14:42:01 -07006040 * Return: 0 for success, non-zero for failure.
Sreekanth Reddyf92363d2012-11-30 07:44:21 +05306041 */
6042static int
Calvin Owens98c56ad2016-07-28 21:38:21 -07006043_base_get_ioc_facts(struct MPT3SAS_ADAPTER *ioc)
Sreekanth Reddyf92363d2012-11-30 07:44:21 +05306044{
6045 Mpi2IOCFactsRequest_t mpi_request;
6046 Mpi2IOCFactsReply_t mpi_reply;
6047 struct mpt3sas_facts *facts;
6048 int mpi_reply_sz, mpi_request_sz, r;
6049
Joe Perches919d8a32018-09-17 08:01:09 -07006050 dinitprintk(ioc, ioc_info(ioc, "%s\n", __func__));
Sreekanth Reddyf92363d2012-11-30 07:44:21 +05306051
Calvin Owens98c56ad2016-07-28 21:38:21 -07006052 r = _base_wait_for_iocstate(ioc, 10);
Sreekanth Reddy4dc8c802015-06-30 12:24:48 +05306053 if (r) {
Joe Perches1f95a472018-09-17 08:01:13 -07006054 dfailprintk(ioc,
6055 ioc_info(ioc, "%s: failed getting to correct state\n",
6056 __func__));
Sreekanth Reddy4dc8c802015-06-30 12:24:48 +05306057 return r;
6058 }
Sreekanth Reddyf92363d2012-11-30 07:44:21 +05306059 mpi_reply_sz = sizeof(Mpi2IOCFactsReply_t);
6060 mpi_request_sz = sizeof(Mpi2IOCFactsRequest_t);
6061 memset(&mpi_request, 0, mpi_request_sz);
6062 mpi_request.Function = MPI2_FUNCTION_IOC_FACTS;
6063 r = _base_handshake_req_reply_wait(ioc, mpi_request_sz,
Calvin Owens98c56ad2016-07-28 21:38:21 -07006064 (u32 *)&mpi_request, mpi_reply_sz, (u16 *)&mpi_reply, 5);
Sreekanth Reddyf92363d2012-11-30 07:44:21 +05306065
6066 if (r != 0) {
Joe Perches919d8a32018-09-17 08:01:09 -07006067 ioc_err(ioc, "%s: handshake failed (r=%d)\n", __func__, r);
Sreekanth Reddyf92363d2012-11-30 07:44:21 +05306068 return r;
6069 }
6070
6071 facts = &ioc->facts;
6072 memset(facts, 0, sizeof(struct mpt3sas_facts));
6073 facts->MsgVersion = le16_to_cpu(mpi_reply.MsgVersion);
6074 facts->HeaderVersion = le16_to_cpu(mpi_reply.HeaderVersion);
6075 facts->VP_ID = mpi_reply.VP_ID;
6076 facts->VF_ID = mpi_reply.VF_ID;
6077 facts->IOCExceptions = le16_to_cpu(mpi_reply.IOCExceptions);
6078 facts->MaxChainDepth = mpi_reply.MaxChainDepth;
6079 facts->WhoInit = mpi_reply.WhoInit;
6080 facts->NumberOfPorts = mpi_reply.NumberOfPorts;
6081 facts->MaxMSIxVectors = mpi_reply.MaxMSIxVectors;
Chaitra P B2b48be62018-05-31 06:34:51 -04006082 if (ioc->msix_enable && (facts->MaxMSIxVectors <=
6083 MAX_COMBINED_MSIX_VECTORS(ioc->is_gen35_ioc)))
6084 ioc->combined_reply_queue = 0;
Sreekanth Reddyf92363d2012-11-30 07:44:21 +05306085 facts->RequestCredit = le16_to_cpu(mpi_reply.RequestCredit);
6086 facts->MaxReplyDescriptorPostQueueDepth =
6087 le16_to_cpu(mpi_reply.MaxReplyDescriptorPostQueueDepth);
6088 facts->ProductID = le16_to_cpu(mpi_reply.ProductID);
6089 facts->IOCCapabilities = le32_to_cpu(mpi_reply.IOCCapabilities);
6090 if ((facts->IOCCapabilities & MPI2_IOCFACTS_CAPABILITY_INTEGRATED_RAID))
6091 ioc->ir_firmware = 1;
Sreekanth Reddy9b05c912014-09-12 15:35:31 +05306092 if ((facts->IOCCapabilities &
Sreekanth Reddy06f5f972017-10-10 18:41:16 +05306093 MPI2_IOCFACTS_CAPABILITY_RDPQ_ARRAY_CAPABLE) && (!reset_devices))
Sreekanth Reddy9b05c912014-09-12 15:35:31 +05306094 ioc->rdpq_array_capable = 1;
Suganath Prabu S79c74d02019-05-31 08:14:35 -04006095 if ((facts->IOCCapabilities & MPI26_IOCFACTS_CAPABILITY_ATOMIC_REQ)
6096 && ioc->is_aero_ioc)
6097 ioc->atomic_desc_capable = 1;
Sreekanth Reddyf92363d2012-11-30 07:44:21 +05306098 facts->FWVersion.Word = le32_to_cpu(mpi_reply.FWVersion.Word);
6099 facts->IOCRequestFrameSize =
6100 le16_to_cpu(mpi_reply.IOCRequestFrameSize);
Suganath prabu Subramaniebb30242016-01-28 12:07:04 +05306101 if (ioc->hba_mpi_version_belonged != MPI2_VERSION) {
6102 facts->IOCMaxChainSegmentSize =
6103 le16_to_cpu(mpi_reply.IOCMaxChainSegmentSize);
6104 }
Sreekanth Reddyf92363d2012-11-30 07:44:21 +05306105 facts->MaxInitiators = le16_to_cpu(mpi_reply.MaxInitiators);
6106 facts->MaxTargets = le16_to_cpu(mpi_reply.MaxTargets);
6107 ioc->shost->max_id = -1;
6108 facts->MaxSasExpanders = le16_to_cpu(mpi_reply.MaxSasExpanders);
6109 facts->MaxEnclosures = le16_to_cpu(mpi_reply.MaxEnclosures);
6110 facts->ProtocolFlags = le16_to_cpu(mpi_reply.ProtocolFlags);
6111 facts->HighPriorityCredit =
6112 le16_to_cpu(mpi_reply.HighPriorityCredit);
6113 facts->ReplyFrameSize = mpi_reply.ReplyFrameSize;
6114 facts->MaxDevHandle = le16_to_cpu(mpi_reply.MaxDevHandle);
Suganath Prabu Subramani016d5c32017-10-31 18:02:28 +05306115 facts->CurrentHostPageSize = mpi_reply.CurrentHostPageSize;
6116
6117 /*
6118 * Get the Page Size from IOC Facts. If it's 0, default to 4k.
6119 */
6120 ioc->page_size = 1 << facts->CurrentHostPageSize;
6121 if (ioc->page_size == 1) {
Joe Perches919d8a32018-09-17 08:01:09 -07006122 ioc_info(ioc, "CurrentHostPageSize is 0: Setting default host page size to 4k\n");
Suganath Prabu Subramani016d5c32017-10-31 18:02:28 +05306123 ioc->page_size = 1 << MPT3SAS_HOST_PAGE_SIZE_4K;
6124 }
Joe Perches919d8a32018-09-17 08:01:09 -07006125 dinitprintk(ioc,
6126 ioc_info(ioc, "CurrentHostPageSize(%d)\n",
6127 facts->CurrentHostPageSize));
Sreekanth Reddyf92363d2012-11-30 07:44:21 +05306128
Joe Perches919d8a32018-09-17 08:01:09 -07006129 dinitprintk(ioc,
6130 ioc_info(ioc, "hba queue depth(%d), max chains per io(%d)\n",
6131 facts->RequestCredit, facts->MaxChainDepth));
6132 dinitprintk(ioc,
6133 ioc_info(ioc, "request frame size(%d), reply frame size(%d)\n",
6134 facts->IOCRequestFrameSize * 4,
6135 facts->ReplyFrameSize * 4));
Sreekanth Reddyf92363d2012-11-30 07:44:21 +05306136 return 0;
6137}
6138
6139/**
6140 * _base_send_ioc_init - send ioc_init to firmware
6141 * @ioc: per adapter object
Sreekanth Reddyf92363d2012-11-30 07:44:21 +05306142 *
Bart Van Assche4beb4862018-06-15 14:42:01 -07006143 * Return: 0 for success, non-zero for failure.
Sreekanth Reddyf92363d2012-11-30 07:44:21 +05306144 */
6145static int
Calvin Owens98c56ad2016-07-28 21:38:21 -07006146_base_send_ioc_init(struct MPT3SAS_ADAPTER *ioc)
Sreekanth Reddyf92363d2012-11-30 07:44:21 +05306147{
6148 Mpi2IOCInitRequest_t mpi_request;
6149 Mpi2IOCInitReply_t mpi_reply;
Sreekanth Reddy9b05c912014-09-12 15:35:31 +05306150 int i, r = 0;
Tina Ruchandani23409bd2016-04-13 00:01:40 -07006151 ktime_t current_time;
Sreekanth Reddyf92363d2012-11-30 07:44:21 +05306152 u16 ioc_status;
Sreekanth Reddy9b05c912014-09-12 15:35:31 +05306153 u32 reply_post_free_array_sz = 0;
Sreekanth Reddyf92363d2012-11-30 07:44:21 +05306154
Joe Perches919d8a32018-09-17 08:01:09 -07006155 dinitprintk(ioc, ioc_info(ioc, "%s\n", __func__));
Sreekanth Reddyf92363d2012-11-30 07:44:21 +05306156
6157 memset(&mpi_request, 0, sizeof(Mpi2IOCInitRequest_t));
6158 mpi_request.Function = MPI2_FUNCTION_IOC_INIT;
6159 mpi_request.WhoInit = MPI2_WHOINIT_HOST_DRIVER;
6160 mpi_request.VF_ID = 0; /* TODO */
6161 mpi_request.VP_ID = 0;
Sreekanth Reddyd357e842015-11-11 17:30:22 +05306162 mpi_request.MsgVersion = cpu_to_le16(ioc->hba_mpi_version_belonged);
Sreekanth Reddyf92363d2012-11-30 07:44:21 +05306163 mpi_request.HeaderVersion = cpu_to_le16(MPI2_HEADER_VERSION);
Suganath Prabu Subramani016d5c32017-10-31 18:02:28 +05306164 mpi_request.HostPageSize = MPT3SAS_HOST_PAGE_SIZE_4K;
Sreekanth Reddyf92363d2012-11-30 07:44:21 +05306165
6166 if (_base_is_controller_msix_enabled(ioc))
6167 mpi_request.HostMSIxVectors = ioc->reply_queue_count;
6168 mpi_request.SystemRequestFrameSize = cpu_to_le16(ioc->request_sz/4);
6169 mpi_request.ReplyDescriptorPostQueueDepth =
6170 cpu_to_le16(ioc->reply_post_queue_depth);
6171 mpi_request.ReplyFreeQueueDepth =
6172 cpu_to_le16(ioc->reply_free_queue_depth);
6173
6174 mpi_request.SenseBufferAddressHigh =
6175 cpu_to_le32((u64)ioc->sense_dma >> 32);
6176 mpi_request.SystemReplyAddressHigh =
6177 cpu_to_le32((u64)ioc->reply_dma >> 32);
6178 mpi_request.SystemRequestFrameBaseAddress =
6179 cpu_to_le64((u64)ioc->request_dma);
6180 mpi_request.ReplyFreeQueueAddress =
6181 cpu_to_le64((u64)ioc->reply_free_dma);
Sreekanth Reddyf92363d2012-11-30 07:44:21 +05306182
Sreekanth Reddy9b05c912014-09-12 15:35:31 +05306183 if (ioc->rdpq_array_enable) {
6184 reply_post_free_array_sz = ioc->reply_queue_count *
6185 sizeof(Mpi2IOCInitRDPQArrayEntry);
Chaitra P Bcd332232018-04-24 05:28:31 -04006186 memset(ioc->reply_post_free_array, 0, reply_post_free_array_sz);
Sreekanth Reddy9b05c912014-09-12 15:35:31 +05306187 for (i = 0; i < ioc->reply_queue_count; i++)
Chaitra P Bcd332232018-04-24 05:28:31 -04006188 ioc->reply_post_free_array[i].RDPQBaseAddress =
Sreekanth Reddy9b05c912014-09-12 15:35:31 +05306189 cpu_to_le64(
6190 (u64)ioc->reply_post[i].reply_post_free_dma);
6191 mpi_request.MsgFlags = MPI2_IOCINIT_MSGFLAG_RDPQ_ARRAY_MODE;
6192 mpi_request.ReplyDescriptorPostQueueAddress =
Chaitra P Bcd332232018-04-24 05:28:31 -04006193 cpu_to_le64((u64)ioc->reply_post_free_array_dma);
Sreekanth Reddy9b05c912014-09-12 15:35:31 +05306194 } else {
6195 mpi_request.ReplyDescriptorPostQueueAddress =
6196 cpu_to_le64((u64)ioc->reply_post[0].reply_post_free_dma);
6197 }
Sreekanth Reddyf92363d2012-11-30 07:44:21 +05306198
6199 /* This time stamp specifies number of milliseconds
6200 * since epoch ~ midnight January 1, 1970.
6201 */
Tina Ruchandani23409bd2016-04-13 00:01:40 -07006202 current_time = ktime_get_real();
6203 mpi_request.TimeStamp = cpu_to_le64(ktime_to_ms(current_time));
Sreekanth Reddyf92363d2012-11-30 07:44:21 +05306204
6205 if (ioc->logging_level & MPT_DEBUG_INIT) {
6206 __le32 *mfp;
6207 int i;
6208
6209 mfp = (__le32 *)&mpi_request;
6210 pr_info("\toffset:data\n");
6211 for (i = 0; i < sizeof(Mpi2IOCInitRequest_t)/4; i++)
6212 pr_info("\t[0x%02x]:%08x\n", i*4,
6213 le32_to_cpu(mfp[i]));
6214 }
6215
6216 r = _base_handshake_req_reply_wait(ioc,
6217 sizeof(Mpi2IOCInitRequest_t), (u32 *)&mpi_request,
Calvin Owens98c56ad2016-07-28 21:38:21 -07006218 sizeof(Mpi2IOCInitReply_t), (u16 *)&mpi_reply, 10);
Sreekanth Reddyf92363d2012-11-30 07:44:21 +05306219
6220 if (r != 0) {
Joe Perches919d8a32018-09-17 08:01:09 -07006221 ioc_err(ioc, "%s: handshake failed (r=%d)\n", __func__, r);
Chaitra P Bcd332232018-04-24 05:28:31 -04006222 return r;
Sreekanth Reddyf92363d2012-11-30 07:44:21 +05306223 }
6224
6225 ioc_status = le16_to_cpu(mpi_reply.IOCStatus) & MPI2_IOCSTATUS_MASK;
6226 if (ioc_status != MPI2_IOCSTATUS_SUCCESS ||
6227 mpi_reply.IOCLogInfo) {
Joe Perches919d8a32018-09-17 08:01:09 -07006228 ioc_err(ioc, "%s: failed\n", __func__);
Sreekanth Reddyf92363d2012-11-30 07:44:21 +05306229 r = -EIO;
6230 }
6231
Sreekanth Reddy9b05c912014-09-12 15:35:31 +05306232 return r;
Sreekanth Reddyf92363d2012-11-30 07:44:21 +05306233}
6234
6235/**
6236 * mpt3sas_port_enable_done - command completion routine for port enable
6237 * @ioc: per adapter object
6238 * @smid: system request message index
6239 * @msix_index: MSIX table index supplied by the OS
6240 * @reply: reply message frame(lower 32bit addr)
6241 *
Bart Van Assche4beb4862018-06-15 14:42:01 -07006242 * Return: 1 meaning mf should be freed from _base_interrupt
6243 * 0 means the mf is freed from this function.
Sreekanth Reddyf92363d2012-11-30 07:44:21 +05306244 */
6245u8
6246mpt3sas_port_enable_done(struct MPT3SAS_ADAPTER *ioc, u16 smid, u8 msix_index,
6247 u32 reply)
6248{
6249 MPI2DefaultReply_t *mpi_reply;
6250 u16 ioc_status;
6251
6252 if (ioc->port_enable_cmds.status == MPT3_CMD_NOT_USED)
6253 return 1;
6254
6255 mpi_reply = mpt3sas_base_get_reply_virt_addr(ioc, reply);
6256 if (!mpi_reply)
6257 return 1;
6258
6259 if (mpi_reply->Function != MPI2_FUNCTION_PORT_ENABLE)
6260 return 1;
6261
6262 ioc->port_enable_cmds.status &= ~MPT3_CMD_PENDING;
6263 ioc->port_enable_cmds.status |= MPT3_CMD_COMPLETE;
6264 ioc->port_enable_cmds.status |= MPT3_CMD_REPLY_VALID;
6265 memcpy(ioc->port_enable_cmds.reply, mpi_reply, mpi_reply->MsgLength*4);
6266 ioc_status = le16_to_cpu(mpi_reply->IOCStatus) & MPI2_IOCSTATUS_MASK;
6267 if (ioc_status != MPI2_IOCSTATUS_SUCCESS)
6268 ioc->port_enable_failed = 1;
6269
6270 if (ioc->is_driver_loading) {
6271 if (ioc_status == MPI2_IOCSTATUS_SUCCESS) {
6272 mpt3sas_port_enable_complete(ioc);
6273 return 1;
6274 } else {
6275 ioc->start_scan_failed = ioc_status;
6276 ioc->start_scan = 0;
6277 return 1;
6278 }
6279 }
6280 complete(&ioc->port_enable_cmds.done);
6281 return 1;
6282}
6283
6284/**
6285 * _base_send_port_enable - send port_enable(discovery stuff) to firmware
6286 * @ioc: per adapter object
Sreekanth Reddyf92363d2012-11-30 07:44:21 +05306287 *
Bart Van Assche4beb4862018-06-15 14:42:01 -07006288 * Return: 0 for success, non-zero for failure.
Sreekanth Reddyf92363d2012-11-30 07:44:21 +05306289 */
6290static int
Calvin Owens98c56ad2016-07-28 21:38:21 -07006291_base_send_port_enable(struct MPT3SAS_ADAPTER *ioc)
Sreekanth Reddyf92363d2012-11-30 07:44:21 +05306292{
6293 Mpi2PortEnableRequest_t *mpi_request;
6294 Mpi2PortEnableReply_t *mpi_reply;
Sreekanth Reddyf92363d2012-11-30 07:44:21 +05306295 int r = 0;
6296 u16 smid;
6297 u16 ioc_status;
6298
Joe Perches919d8a32018-09-17 08:01:09 -07006299 ioc_info(ioc, "sending port enable !!\n");
Sreekanth Reddyf92363d2012-11-30 07:44:21 +05306300
6301 if (ioc->port_enable_cmds.status & MPT3_CMD_PENDING) {
Joe Perches919d8a32018-09-17 08:01:09 -07006302 ioc_err(ioc, "%s: internal command already in use\n", __func__);
Sreekanth Reddyf92363d2012-11-30 07:44:21 +05306303 return -EAGAIN;
6304 }
6305
6306 smid = mpt3sas_base_get_smid(ioc, ioc->port_enable_cb_idx);
6307 if (!smid) {
Joe Perches919d8a32018-09-17 08:01:09 -07006308 ioc_err(ioc, "%s: failed obtaining a smid\n", __func__);
Sreekanth Reddyf92363d2012-11-30 07:44:21 +05306309 return -EAGAIN;
6310 }
6311
6312 ioc->port_enable_cmds.status = MPT3_CMD_PENDING;
6313 mpi_request = mpt3sas_base_get_msg_frame(ioc, smid);
6314 ioc->port_enable_cmds.smid = smid;
6315 memset(mpi_request, 0, sizeof(Mpi2PortEnableRequest_t));
6316 mpi_request->Function = MPI2_FUNCTION_PORT_ENABLE;
6317
6318 init_completion(&ioc->port_enable_cmds.done);
Suganath Prabu S078a4cc2019-05-31 08:14:34 -04006319 ioc->put_smid_default(ioc, smid);
Calvin Owens8bbb1cf2016-07-28 21:38:22 -07006320 wait_for_completion_timeout(&ioc->port_enable_cmds.done, 300*HZ);
Sreekanth Reddyf92363d2012-11-30 07:44:21 +05306321 if (!(ioc->port_enable_cmds.status & MPT3_CMD_COMPLETE)) {
Joe Perches919d8a32018-09-17 08:01:09 -07006322 ioc_err(ioc, "%s: timeout\n", __func__);
Sreekanth Reddyf92363d2012-11-30 07:44:21 +05306323 _debug_dump_mf(mpi_request,
6324 sizeof(Mpi2PortEnableRequest_t)/4);
6325 if (ioc->port_enable_cmds.status & MPT3_CMD_RESET)
6326 r = -EFAULT;
6327 else
6328 r = -ETIME;
6329 goto out;
6330 }
6331
6332 mpi_reply = ioc->port_enable_cmds.reply;
6333 ioc_status = le16_to_cpu(mpi_reply->IOCStatus) & MPI2_IOCSTATUS_MASK;
6334 if (ioc_status != MPI2_IOCSTATUS_SUCCESS) {
Joe Perches919d8a32018-09-17 08:01:09 -07006335 ioc_err(ioc, "%s: failed with (ioc_status=0x%08x)\n",
6336 __func__, ioc_status);
Sreekanth Reddyf92363d2012-11-30 07:44:21 +05306337 r = -EFAULT;
6338 goto out;
6339 }
6340
6341 out:
6342 ioc->port_enable_cmds.status = MPT3_CMD_NOT_USED;
Joe Perches919d8a32018-09-17 08:01:09 -07006343 ioc_info(ioc, "port enable: %s\n", r == 0 ? "SUCCESS" : "FAILED");
Sreekanth Reddyf92363d2012-11-30 07:44:21 +05306344 return r;
6345}
6346
6347/**
6348 * mpt3sas_port_enable - initiate firmware discovery (don't wait for reply)
6349 * @ioc: per adapter object
6350 *
Bart Van Assche4beb4862018-06-15 14:42:01 -07006351 * Return: 0 for success, non-zero for failure.
Sreekanth Reddyf92363d2012-11-30 07:44:21 +05306352 */
6353int
6354mpt3sas_port_enable(struct MPT3SAS_ADAPTER *ioc)
6355{
6356 Mpi2PortEnableRequest_t *mpi_request;
6357 u16 smid;
6358
Joe Perches919d8a32018-09-17 08:01:09 -07006359 ioc_info(ioc, "sending port enable !!\n");
Sreekanth Reddyf92363d2012-11-30 07:44:21 +05306360
6361 if (ioc->port_enable_cmds.status & MPT3_CMD_PENDING) {
Joe Perches919d8a32018-09-17 08:01:09 -07006362 ioc_err(ioc, "%s: internal command already in use\n", __func__);
Sreekanth Reddyf92363d2012-11-30 07:44:21 +05306363 return -EAGAIN;
6364 }
6365
6366 smid = mpt3sas_base_get_smid(ioc, ioc->port_enable_cb_idx);
6367 if (!smid) {
Joe Perches919d8a32018-09-17 08:01:09 -07006368 ioc_err(ioc, "%s: failed obtaining a smid\n", __func__);
Sreekanth Reddyf92363d2012-11-30 07:44:21 +05306369 return -EAGAIN;
6370 }
6371
6372 ioc->port_enable_cmds.status = MPT3_CMD_PENDING;
6373 mpi_request = mpt3sas_base_get_msg_frame(ioc, smid);
6374 ioc->port_enable_cmds.smid = smid;
6375 memset(mpi_request, 0, sizeof(Mpi2PortEnableRequest_t));
6376 mpi_request->Function = MPI2_FUNCTION_PORT_ENABLE;
6377
Suganath Prabu S078a4cc2019-05-31 08:14:34 -04006378 ioc->put_smid_default(ioc, smid);
Sreekanth Reddyf92363d2012-11-30 07:44:21 +05306379 return 0;
6380}
6381
6382/**
6383 * _base_determine_wait_on_discovery - desposition
6384 * @ioc: per adapter object
6385 *
6386 * Decide whether to wait on discovery to complete. Used to either
6387 * locate boot device, or report volumes ahead of physical devices.
6388 *
Bart Van Assche4beb4862018-06-15 14:42:01 -07006389 * Return: 1 for wait, 0 for don't wait.
Sreekanth Reddyf92363d2012-11-30 07:44:21 +05306390 */
6391static int
6392_base_determine_wait_on_discovery(struct MPT3SAS_ADAPTER *ioc)
6393{
6394 /* We wait for discovery to complete if IR firmware is loaded.
6395 * The sas topology events arrive before PD events, so we need time to
6396 * turn on the bit in ioc->pd_handles to indicate PD
6397 * Also, it maybe required to report Volumes ahead of physical
6398 * devices when MPI2_IOCPAGE8_IRFLAGS_LOW_VOLUME_MAPPING is set.
6399 */
6400 if (ioc->ir_firmware)
6401 return 1;
6402
6403 /* if no Bios, then we don't need to wait */
6404 if (!ioc->bios_pg3.BiosVersion)
6405 return 0;
6406
6407 /* Bios is present, then we drop down here.
6408 *
6409 * If there any entries in the Bios Page 2, then we wait
6410 * for discovery to complete.
6411 */
6412
6413 /* Current Boot Device */
6414 if ((ioc->bios_pg2.CurrentBootDeviceForm &
6415 MPI2_BIOSPAGE2_FORM_MASK) ==
6416 MPI2_BIOSPAGE2_FORM_NO_DEVICE_SPECIFIED &&
6417 /* Request Boot Device */
6418 (ioc->bios_pg2.ReqBootDeviceForm &
6419 MPI2_BIOSPAGE2_FORM_MASK) ==
6420 MPI2_BIOSPAGE2_FORM_NO_DEVICE_SPECIFIED &&
6421 /* Alternate Request Boot Device */
6422 (ioc->bios_pg2.ReqAltBootDeviceForm &
6423 MPI2_BIOSPAGE2_FORM_MASK) ==
6424 MPI2_BIOSPAGE2_FORM_NO_DEVICE_SPECIFIED)
6425 return 0;
6426
6427 return 1;
6428}
6429
6430/**
6431 * _base_unmask_events - turn on notification for this event
6432 * @ioc: per adapter object
6433 * @event: firmware event
6434 *
6435 * The mask is stored in ioc->event_masks.
6436 */
6437static void
6438_base_unmask_events(struct MPT3SAS_ADAPTER *ioc, u16 event)
6439{
6440 u32 desired_event;
6441
6442 if (event >= 128)
6443 return;
6444
6445 desired_event = (1 << (event % 32));
6446
6447 if (event < 32)
6448 ioc->event_masks[0] &= ~desired_event;
6449 else if (event < 64)
6450 ioc->event_masks[1] &= ~desired_event;
6451 else if (event < 96)
6452 ioc->event_masks[2] &= ~desired_event;
6453 else if (event < 128)
6454 ioc->event_masks[3] &= ~desired_event;
6455}
6456
6457/**
6458 * _base_event_notification - send event notification
6459 * @ioc: per adapter object
Sreekanth Reddyf92363d2012-11-30 07:44:21 +05306460 *
Bart Van Assche4beb4862018-06-15 14:42:01 -07006461 * Return: 0 for success, non-zero for failure.
Sreekanth Reddyf92363d2012-11-30 07:44:21 +05306462 */
6463static int
Calvin Owens98c56ad2016-07-28 21:38:21 -07006464_base_event_notification(struct MPT3SAS_ADAPTER *ioc)
Sreekanth Reddyf92363d2012-11-30 07:44:21 +05306465{
6466 Mpi2EventNotificationRequest_t *mpi_request;
Sreekanth Reddyf92363d2012-11-30 07:44:21 +05306467 u16 smid;
6468 int r = 0;
6469 int i;
6470
Joe Perches919d8a32018-09-17 08:01:09 -07006471 dinitprintk(ioc, ioc_info(ioc, "%s\n", __func__));
Sreekanth Reddyf92363d2012-11-30 07:44:21 +05306472
6473 if (ioc->base_cmds.status & MPT3_CMD_PENDING) {
Joe Perches919d8a32018-09-17 08:01:09 -07006474 ioc_err(ioc, "%s: internal command already in use\n", __func__);
Sreekanth Reddyf92363d2012-11-30 07:44:21 +05306475 return -EAGAIN;
6476 }
6477
6478 smid = mpt3sas_base_get_smid(ioc, ioc->base_cb_idx);
6479 if (!smid) {
Joe Perches919d8a32018-09-17 08:01:09 -07006480 ioc_err(ioc, "%s: failed obtaining a smid\n", __func__);
Sreekanth Reddyf92363d2012-11-30 07:44:21 +05306481 return -EAGAIN;
6482 }
6483 ioc->base_cmds.status = MPT3_CMD_PENDING;
6484 mpi_request = mpt3sas_base_get_msg_frame(ioc, smid);
6485 ioc->base_cmds.smid = smid;
6486 memset(mpi_request, 0, sizeof(Mpi2EventNotificationRequest_t));
6487 mpi_request->Function = MPI2_FUNCTION_EVENT_NOTIFICATION;
6488 mpi_request->VF_ID = 0; /* TODO */
6489 mpi_request->VP_ID = 0;
6490 for (i = 0; i < MPI2_EVENT_NOTIFY_EVENTMASK_WORDS; i++)
6491 mpi_request->EventMasks[i] =
6492 cpu_to_le32(ioc->event_masks[i]);
6493 init_completion(&ioc->base_cmds.done);
Suganath Prabu S078a4cc2019-05-31 08:14:34 -04006494 ioc->put_smid_default(ioc, smid);
Calvin Owens8bbb1cf2016-07-28 21:38:22 -07006495 wait_for_completion_timeout(&ioc->base_cmds.done, 30*HZ);
Sreekanth Reddyf92363d2012-11-30 07:44:21 +05306496 if (!(ioc->base_cmds.status & MPT3_CMD_COMPLETE)) {
Joe Perches919d8a32018-09-17 08:01:09 -07006497 ioc_err(ioc, "%s: timeout\n", __func__);
Sreekanth Reddyf92363d2012-11-30 07:44:21 +05306498 _debug_dump_mf(mpi_request,
6499 sizeof(Mpi2EventNotificationRequest_t)/4);
6500 if (ioc->base_cmds.status & MPT3_CMD_RESET)
6501 r = -EFAULT;
6502 else
6503 r = -ETIME;
6504 } else
Joe Perches919d8a32018-09-17 08:01:09 -07006505 dinitprintk(ioc, ioc_info(ioc, "%s: complete\n", __func__));
Sreekanth Reddyf92363d2012-11-30 07:44:21 +05306506 ioc->base_cmds.status = MPT3_CMD_NOT_USED;
6507 return r;
6508}
6509
6510/**
6511 * mpt3sas_base_validate_event_type - validating event types
6512 * @ioc: per adapter object
Bart Van Assche4beb4862018-06-15 14:42:01 -07006513 * @event_type: firmware event
Sreekanth Reddyf92363d2012-11-30 07:44:21 +05306514 *
6515 * This will turn on firmware event notification when application
6516 * ask for that event. We don't mask events that are already enabled.
6517 */
6518void
6519mpt3sas_base_validate_event_type(struct MPT3SAS_ADAPTER *ioc, u32 *event_type)
6520{
6521 int i, j;
6522 u32 event_mask, desired_event;
6523 u8 send_update_to_fw;
6524
6525 for (i = 0, send_update_to_fw = 0; i <
6526 MPI2_EVENT_NOTIFY_EVENTMASK_WORDS; i++) {
6527 event_mask = ~event_type[i];
6528 desired_event = 1;
6529 for (j = 0; j < 32; j++) {
6530 if (!(event_mask & desired_event) &&
6531 (ioc->event_masks[i] & desired_event)) {
6532 ioc->event_masks[i] &= ~desired_event;
6533 send_update_to_fw = 1;
6534 }
6535 desired_event = (desired_event << 1);
6536 }
6537 }
6538
6539 if (!send_update_to_fw)
6540 return;
6541
6542 mutex_lock(&ioc->base_cmds.mutex);
Calvin Owens98c56ad2016-07-28 21:38:21 -07006543 _base_event_notification(ioc);
Sreekanth Reddyf92363d2012-11-30 07:44:21 +05306544 mutex_unlock(&ioc->base_cmds.mutex);
6545}
6546
6547/**
6548 * _base_diag_reset - the "big hammer" start of day reset
6549 * @ioc: per adapter object
Sreekanth Reddyf92363d2012-11-30 07:44:21 +05306550 *
Bart Van Assche4beb4862018-06-15 14:42:01 -07006551 * Return: 0 for success, non-zero for failure.
Sreekanth Reddyf92363d2012-11-30 07:44:21 +05306552 */
6553static int
Calvin Owens98c56ad2016-07-28 21:38:21 -07006554_base_diag_reset(struct MPT3SAS_ADAPTER *ioc)
Sreekanth Reddyf92363d2012-11-30 07:44:21 +05306555{
6556 u32 host_diagnostic;
6557 u32 ioc_state;
6558 u32 count;
6559 u32 hcb_size;
6560
Joe Perches919d8a32018-09-17 08:01:09 -07006561 ioc_info(ioc, "sending diag reset !!\n");
Sreekanth Reddyf92363d2012-11-30 07:44:21 +05306562
Joe Perches919d8a32018-09-17 08:01:09 -07006563 drsprintk(ioc, ioc_info(ioc, "clear interrupts\n"));
Sreekanth Reddyf92363d2012-11-30 07:44:21 +05306564
6565 count = 0;
6566 do {
6567 /* Write magic sequence to WriteSequence register
6568 * Loop until in diagnostic mode
6569 */
Joe Perches919d8a32018-09-17 08:01:09 -07006570 drsprintk(ioc, ioc_info(ioc, "write magic sequence\n"));
Sreekanth Reddyf92363d2012-11-30 07:44:21 +05306571 writel(MPI2_WRSEQ_FLUSH_KEY_VALUE, &ioc->chip->WriteSequence);
6572 writel(MPI2_WRSEQ_1ST_KEY_VALUE, &ioc->chip->WriteSequence);
6573 writel(MPI2_WRSEQ_2ND_KEY_VALUE, &ioc->chip->WriteSequence);
6574 writel(MPI2_WRSEQ_3RD_KEY_VALUE, &ioc->chip->WriteSequence);
6575 writel(MPI2_WRSEQ_4TH_KEY_VALUE, &ioc->chip->WriteSequence);
6576 writel(MPI2_WRSEQ_5TH_KEY_VALUE, &ioc->chip->WriteSequence);
6577 writel(MPI2_WRSEQ_6TH_KEY_VALUE, &ioc->chip->WriteSequence);
6578
6579 /* wait 100 msec */
Calvin Owens98c56ad2016-07-28 21:38:21 -07006580 msleep(100);
Sreekanth Reddyf92363d2012-11-30 07:44:21 +05306581
6582 if (count++ > 20)
6583 goto out;
6584
Suganath Prabu306eaf22018-12-07 12:58:34 +05306585 host_diagnostic = ioc->base_readl(&ioc->chip->HostDiagnostic);
Joe Perches919d8a32018-09-17 08:01:09 -07006586 drsprintk(ioc,
6587 ioc_info(ioc, "wrote magic sequence: count(%d), host_diagnostic(0x%08x)\n",
6588 count, host_diagnostic));
Sreekanth Reddyf92363d2012-11-30 07:44:21 +05306589
6590 } while ((host_diagnostic & MPI2_DIAG_DIAG_WRITE_ENABLE) == 0);
6591
Suganath Prabu306eaf22018-12-07 12:58:34 +05306592 hcb_size = ioc->base_readl(&ioc->chip->HCBSize);
Sreekanth Reddyf92363d2012-11-30 07:44:21 +05306593
Joe Perches919d8a32018-09-17 08:01:09 -07006594 drsprintk(ioc, ioc_info(ioc, "diag reset: issued\n"));
Sreekanth Reddyf92363d2012-11-30 07:44:21 +05306595 writel(host_diagnostic | MPI2_DIAG_RESET_ADAPTER,
6596 &ioc->chip->HostDiagnostic);
6597
Sreekanth Reddyb453ff82013-06-29 03:51:19 +05306598 /*This delay allows the chip PCIe hardware time to finish reset tasks*/
Calvin Owens98c56ad2016-07-28 21:38:21 -07006599 msleep(MPI2_HARD_RESET_PCIE_FIRST_READ_DELAY_MICRO_SEC/1000);
Sreekanth Reddyf92363d2012-11-30 07:44:21 +05306600
Sreekanth Reddyb453ff82013-06-29 03:51:19 +05306601 /* Approximately 300 second max wait */
6602 for (count = 0; count < (300000000 /
6603 MPI2_HARD_RESET_PCIE_SECOND_READ_DELAY_MICRO_SEC); count++) {
Sreekanth Reddyf92363d2012-11-30 07:44:21 +05306604
Suganath Prabu306eaf22018-12-07 12:58:34 +05306605 host_diagnostic = ioc->base_readl(&ioc->chip->HostDiagnostic);
Sreekanth Reddyf92363d2012-11-30 07:44:21 +05306606
6607 if (host_diagnostic == 0xFFFFFFFF)
6608 goto out;
6609 if (!(host_diagnostic & MPI2_DIAG_RESET_ADAPTER))
6610 break;
6611
Calvin Owens98c56ad2016-07-28 21:38:21 -07006612 msleep(MPI2_HARD_RESET_PCIE_SECOND_READ_DELAY_MICRO_SEC / 1000);
Sreekanth Reddyf92363d2012-11-30 07:44:21 +05306613 }
6614
6615 if (host_diagnostic & MPI2_DIAG_HCB_MODE) {
6616
Joe Perches919d8a32018-09-17 08:01:09 -07006617 drsprintk(ioc,
6618 ioc_info(ioc, "restart the adapter assuming the HCB Address points to good F/W\n"));
Sreekanth Reddyf92363d2012-11-30 07:44:21 +05306619 host_diagnostic &= ~MPI2_DIAG_BOOT_DEVICE_SELECT_MASK;
6620 host_diagnostic |= MPI2_DIAG_BOOT_DEVICE_SELECT_HCDW;
6621 writel(host_diagnostic, &ioc->chip->HostDiagnostic);
6622
Joe Perches919d8a32018-09-17 08:01:09 -07006623 drsprintk(ioc, ioc_info(ioc, "re-enable the HCDW\n"));
Sreekanth Reddyf92363d2012-11-30 07:44:21 +05306624 writel(hcb_size | MPI2_HCB_SIZE_HCB_ENABLE,
6625 &ioc->chip->HCBSize);
6626 }
6627
Joe Perches919d8a32018-09-17 08:01:09 -07006628 drsprintk(ioc, ioc_info(ioc, "restart the adapter\n"));
Sreekanth Reddyf92363d2012-11-30 07:44:21 +05306629 writel(host_diagnostic & ~MPI2_DIAG_HOLD_IOC_RESET,
6630 &ioc->chip->HostDiagnostic);
6631
Joe Perches919d8a32018-09-17 08:01:09 -07006632 drsprintk(ioc,
6633 ioc_info(ioc, "disable writes to the diagnostic register\n"));
Sreekanth Reddyf92363d2012-11-30 07:44:21 +05306634 writel(MPI2_WRSEQ_FLUSH_KEY_VALUE, &ioc->chip->WriteSequence);
6635
Joe Perches919d8a32018-09-17 08:01:09 -07006636 drsprintk(ioc, ioc_info(ioc, "Wait for FW to go to the READY state\n"));
Calvin Owens98c56ad2016-07-28 21:38:21 -07006637 ioc_state = _base_wait_on_iocstate(ioc, MPI2_IOC_STATE_READY, 20);
Sreekanth Reddyf92363d2012-11-30 07:44:21 +05306638 if (ioc_state) {
Joe Perches919d8a32018-09-17 08:01:09 -07006639 ioc_err(ioc, "%s: failed going to ready state (ioc_state=0x%x)\n",
6640 __func__, ioc_state);
Sreekanth Reddyf92363d2012-11-30 07:44:21 +05306641 goto out;
6642 }
6643
Joe Perches919d8a32018-09-17 08:01:09 -07006644 ioc_info(ioc, "diag reset: SUCCESS\n");
Sreekanth Reddyf92363d2012-11-30 07:44:21 +05306645 return 0;
6646
6647 out:
Joe Perches919d8a32018-09-17 08:01:09 -07006648 ioc_err(ioc, "diag reset: FAILED\n");
Sreekanth Reddyf92363d2012-11-30 07:44:21 +05306649 return -EFAULT;
6650}
6651
6652/**
6653 * _base_make_ioc_ready - put controller in READY state
6654 * @ioc: per adapter object
Sreekanth Reddyf92363d2012-11-30 07:44:21 +05306655 * @type: FORCE_BIG_HAMMER or SOFT_RESET
6656 *
Bart Van Assche4beb4862018-06-15 14:42:01 -07006657 * Return: 0 for success, non-zero for failure.
Sreekanth Reddyf92363d2012-11-30 07:44:21 +05306658 */
6659static int
Calvin Owens98c56ad2016-07-28 21:38:21 -07006660_base_make_ioc_ready(struct MPT3SAS_ADAPTER *ioc, enum reset_type type)
Sreekanth Reddyf92363d2012-11-30 07:44:21 +05306661{
6662 u32 ioc_state;
6663 int rc;
6664 int count;
6665
Joe Perches919d8a32018-09-17 08:01:09 -07006666 dinitprintk(ioc, ioc_info(ioc, "%s\n", __func__));
Sreekanth Reddyf92363d2012-11-30 07:44:21 +05306667
6668 if (ioc->pci_error_recovery)
6669 return 0;
6670
6671 ioc_state = mpt3sas_base_get_iocstate(ioc, 0);
Joe Perches919d8a32018-09-17 08:01:09 -07006672 dhsprintk(ioc,
6673 ioc_info(ioc, "%s: ioc_state(0x%08x)\n",
6674 __func__, ioc_state));
Sreekanth Reddyf92363d2012-11-30 07:44:21 +05306675
6676 /* if in RESET state, it should move to READY state shortly */
6677 count = 0;
6678 if ((ioc_state & MPI2_IOC_STATE_MASK) == MPI2_IOC_STATE_RESET) {
6679 while ((ioc_state & MPI2_IOC_STATE_MASK) !=
6680 MPI2_IOC_STATE_READY) {
6681 if (count++ == 10) {
Joe Perches919d8a32018-09-17 08:01:09 -07006682 ioc_err(ioc, "%s: failed going to ready state (ioc_state=0x%x)\n",
6683 __func__, ioc_state);
Sreekanth Reddyf92363d2012-11-30 07:44:21 +05306684 return -EFAULT;
6685 }
Calvin Owens98c56ad2016-07-28 21:38:21 -07006686 ssleep(1);
Sreekanth Reddyf92363d2012-11-30 07:44:21 +05306687 ioc_state = mpt3sas_base_get_iocstate(ioc, 0);
6688 }
6689 }
6690
6691 if ((ioc_state & MPI2_IOC_STATE_MASK) == MPI2_IOC_STATE_READY)
6692 return 0;
6693
6694 if (ioc_state & MPI2_DOORBELL_USED) {
Joe Perches919d8a32018-09-17 08:01:09 -07006695 dhsprintk(ioc, ioc_info(ioc, "unexpected doorbell active!\n"));
Sreekanth Reddyf92363d2012-11-30 07:44:21 +05306696 goto issue_diag_reset;
6697 }
6698
6699 if ((ioc_state & MPI2_IOC_STATE_MASK) == MPI2_IOC_STATE_FAULT) {
6700 mpt3sas_base_fault_info(ioc, ioc_state &
6701 MPI2_DOORBELL_DATA_MASK);
6702 goto issue_diag_reset;
6703 }
6704
6705 if (type == FORCE_BIG_HAMMER)
6706 goto issue_diag_reset;
6707
6708 if ((ioc_state & MPI2_IOC_STATE_MASK) == MPI2_IOC_STATE_OPERATIONAL)
6709 if (!(_base_send_ioc_reset(ioc,
Calvin Owens98c56ad2016-07-28 21:38:21 -07006710 MPI2_FUNCTION_IOC_MESSAGE_UNIT_RESET, 15))) {
Sreekanth Reddyf92363d2012-11-30 07:44:21 +05306711 return 0;
6712 }
6713
6714 issue_diag_reset:
Calvin Owens98c56ad2016-07-28 21:38:21 -07006715 rc = _base_diag_reset(ioc);
Sreekanth Reddyf92363d2012-11-30 07:44:21 +05306716 return rc;
6717}
6718
6719/**
6720 * _base_make_ioc_operational - put controller in OPERATIONAL state
6721 * @ioc: per adapter object
Sreekanth Reddyf92363d2012-11-30 07:44:21 +05306722 *
Bart Van Assche4beb4862018-06-15 14:42:01 -07006723 * Return: 0 for success, non-zero for failure.
Sreekanth Reddyf92363d2012-11-30 07:44:21 +05306724 */
6725static int
Calvin Owens98c56ad2016-07-28 21:38:21 -07006726_base_make_ioc_operational(struct MPT3SAS_ADAPTER *ioc)
Sreekanth Reddyf92363d2012-11-30 07:44:21 +05306727{
Suganath Prabu9c067c02019-08-03 09:59:54 -04006728 int r, i, index, rc;
Sreekanth Reddyf92363d2012-11-30 07:44:21 +05306729 unsigned long flags;
6730 u32 reply_address;
6731 u16 smid;
6732 struct _tr_list *delayed_tr, *delayed_tr_next;
Suganath prabu Subramanifd0331b2016-01-28 12:07:02 +05306733 struct _sc_list *delayed_sc, *delayed_sc_next;
6734 struct _event_ack_list *delayed_event_ack, *delayed_event_ack_next;
Sreekanth Reddy7786ab62015-11-11 17:30:28 +05306735 u8 hide_flag;
Sreekanth Reddyf92363d2012-11-30 07:44:21 +05306736 struct adapter_reply_queue *reply_q;
Calvin Owens5ec8a172016-03-18 12:45:42 -07006737 Mpi2ReplyDescriptorsUnion_t *reply_post_free_contig;
Sreekanth Reddyf92363d2012-11-30 07:44:21 +05306738
Joe Perches919d8a32018-09-17 08:01:09 -07006739 dinitprintk(ioc, ioc_info(ioc, "%s\n", __func__));
Sreekanth Reddyf92363d2012-11-30 07:44:21 +05306740
6741 /* clean the delayed target reset list */
6742 list_for_each_entry_safe(delayed_tr, delayed_tr_next,
6743 &ioc->delayed_tr_list, list) {
6744 list_del(&delayed_tr->list);
6745 kfree(delayed_tr);
6746 }
6747
6748
6749 list_for_each_entry_safe(delayed_tr, delayed_tr_next,
6750 &ioc->delayed_tr_volume_list, list) {
6751 list_del(&delayed_tr->list);
6752 kfree(delayed_tr);
6753 }
6754
Suganath prabu Subramanifd0331b2016-01-28 12:07:02 +05306755 list_for_each_entry_safe(delayed_sc, delayed_sc_next,
6756 &ioc->delayed_sc_list, list) {
6757 list_del(&delayed_sc->list);
6758 kfree(delayed_sc);
6759 }
6760
6761 list_for_each_entry_safe(delayed_event_ack, delayed_event_ack_next,
6762 &ioc->delayed_event_ack_list, list) {
6763 list_del(&delayed_event_ack->list);
6764 kfree(delayed_event_ack);
6765 }
6766
Sreekanth Reddyf92363d2012-11-30 07:44:21 +05306767 spin_lock_irqsave(&ioc->scsi_lookup_lock, flags);
Sreekanth Reddyf92363d2012-11-30 07:44:21 +05306768
6769 /* hi-priority queue */
6770 INIT_LIST_HEAD(&ioc->hpr_free_list);
6771 smid = ioc->hi_priority_smid;
6772 for (i = 0; i < ioc->hi_priority_depth; i++, smid++) {
6773 ioc->hpr_lookup[i].cb_idx = 0xFF;
6774 ioc->hpr_lookup[i].smid = smid;
6775 list_add_tail(&ioc->hpr_lookup[i].tracker_list,
6776 &ioc->hpr_free_list);
6777 }
6778
6779 /* internal queue */
6780 INIT_LIST_HEAD(&ioc->internal_free_list);
6781 smid = ioc->internal_smid;
6782 for (i = 0; i < ioc->internal_depth; i++, smid++) {
6783 ioc->internal_lookup[i].cb_idx = 0xFF;
6784 ioc->internal_lookup[i].smid = smid;
6785 list_add_tail(&ioc->internal_lookup[i].tracker_list,
6786 &ioc->internal_free_list);
6787 }
6788
Sreekanth Reddyf92363d2012-11-30 07:44:21 +05306789 spin_unlock_irqrestore(&ioc->scsi_lookup_lock, flags);
6790
6791 /* initialize Reply Free Queue */
6792 for (i = 0, reply_address = (u32)ioc->reply_dma ;
6793 i < ioc->reply_free_queue_depth ; i++, reply_address +=
Suganath Prabu Subramanib4472d72018-02-07 02:51:50 -08006794 ioc->reply_sz) {
Sreekanth Reddyf92363d2012-11-30 07:44:21 +05306795 ioc->reply_free[i] = cpu_to_le32(reply_address);
Suganath Prabu Subramanib4472d72018-02-07 02:51:50 -08006796 if (ioc->is_mcpu_endpoint)
6797 _base_clone_reply_to_sys_mem(ioc,
Chaitra P Bcf6bf972018-04-24 05:28:30 -04006798 reply_address, i);
Suganath Prabu Subramanib4472d72018-02-07 02:51:50 -08006799 }
Sreekanth Reddyf92363d2012-11-30 07:44:21 +05306800
6801 /* initialize reply queues */
6802 if (ioc->is_driver_loading)
6803 _base_assign_reply_queues(ioc);
6804
6805 /* initialize Reply Post Free Queue */
Calvin Owens5ec8a172016-03-18 12:45:42 -07006806 index = 0;
6807 reply_post_free_contig = ioc->reply_post[0].reply_post_free;
Sreekanth Reddyf92363d2012-11-30 07:44:21 +05306808 list_for_each_entry(reply_q, &ioc->reply_queue_list, list) {
Calvin Owens5ec8a172016-03-18 12:45:42 -07006809 /*
6810 * If RDPQ is enabled, switch to the next allocation.
6811 * Otherwise advance within the contiguous region.
6812 */
6813 if (ioc->rdpq_array_enable) {
6814 reply_q->reply_post_free =
6815 ioc->reply_post[index++].reply_post_free;
6816 } else {
6817 reply_q->reply_post_free = reply_post_free_contig;
6818 reply_post_free_contig += ioc->reply_post_queue_depth;
6819 }
6820
Sreekanth Reddyf92363d2012-11-30 07:44:21 +05306821 reply_q->reply_post_host_index = 0;
Sreekanth Reddyf92363d2012-11-30 07:44:21 +05306822 for (i = 0; i < ioc->reply_post_queue_depth; i++)
6823 reply_q->reply_post_free[i].Words =
6824 cpu_to_le64(ULLONG_MAX);
6825 if (!_base_is_controller_msix_enabled(ioc))
6826 goto skip_init_reply_post_free_queue;
Sreekanth Reddyf92363d2012-11-30 07:44:21 +05306827 }
6828 skip_init_reply_post_free_queue:
6829
Calvin Owens98c56ad2016-07-28 21:38:21 -07006830 r = _base_send_ioc_init(ioc);
Suganath Prabu9c067c02019-08-03 09:59:54 -04006831 if (r) {
6832 /*
6833 * No need to check IOC state for fault state & issue
6834 * diag reset during host reset. This check is need
6835 * only during driver load time.
6836 */
6837 if (!ioc->is_driver_loading)
6838 return r;
6839
6840 rc = _base_check_for_fault_and_issue_reset(ioc);
6841 if (rc || (_base_send_ioc_init(ioc)))
6842 return r;
6843 }
Sreekanth Reddyf92363d2012-11-30 07:44:21 +05306844
6845 /* initialize reply free host index */
6846 ioc->reply_free_host_index = ioc->reply_free_queue_depth - 1;
6847 writel(ioc->reply_free_host_index, &ioc->chip->ReplyFreeHostIndex);
6848
6849 /* initialize reply post host index */
6850 list_for_each_entry(reply_q, &ioc->reply_queue_list, list) {
Suganath Prabu Subramani0bb337c2016-10-26 13:34:38 +05306851 if (ioc->combined_reply_queue)
Sreekanth Reddyfb77bb52015-06-30 12:24:47 +05306852 writel((reply_q->msix_index & 7)<<
6853 MPI2_RPHI_MSIX_INDEX_SHIFT,
6854 ioc->replyPostRegisterIndex[reply_q->msix_index/8]);
6855 else
6856 writel(reply_q->msix_index <<
6857 MPI2_RPHI_MSIX_INDEX_SHIFT,
6858 &ioc->chip->ReplyPostHostIndex);
6859
Sreekanth Reddyf92363d2012-11-30 07:44:21 +05306860 if (!_base_is_controller_msix_enabled(ioc))
6861 goto skip_init_reply_post_host_index;
6862 }
6863
6864 skip_init_reply_post_host_index:
6865
6866 _base_unmask_interrupts(ioc);
Chaitra P B3d29ed82018-04-24 05:28:39 -04006867
6868 if (ioc->hba_mpi_version_belonged != MPI2_VERSION) {
6869 r = _base_display_fwpkg_version(ioc);
6870 if (r)
6871 return r;
6872 }
6873
6874 _base_static_config_pages(ioc);
Calvin Owens98c56ad2016-07-28 21:38:21 -07006875 r = _base_event_notification(ioc);
Sreekanth Reddyf92363d2012-11-30 07:44:21 +05306876 if (r)
6877 return r;
6878
Sreekanth Reddyf92363d2012-11-30 07:44:21 +05306879 if (ioc->is_driver_loading) {
Sreekanth Reddy7786ab62015-11-11 17:30:28 +05306880
6881 if (ioc->is_warpdrive && ioc->manu_pg10.OEMIdentifier
6882 == 0x80) {
6883 hide_flag = (u8) (
6884 le32_to_cpu(ioc->manu_pg10.OEMSpecificFlags0) &
6885 MFG_PAGE10_HIDE_SSDS_MASK);
6886 if (hide_flag != MFG_PAGE10_HIDE_SSDS_MASK)
6887 ioc->mfg_pg10_hide_flag = hide_flag;
6888 }
6889
Sreekanth Reddyf92363d2012-11-30 07:44:21 +05306890 ioc->wait_for_discovery_to_complete =
6891 _base_determine_wait_on_discovery(ioc);
6892
6893 return r; /* scan_start and scan_finished support */
6894 }
6895
Calvin Owens98c56ad2016-07-28 21:38:21 -07006896 r = _base_send_port_enable(ioc);
Sreekanth Reddyf92363d2012-11-30 07:44:21 +05306897 if (r)
6898 return r;
6899
6900 return r;
6901}
6902
6903/**
6904 * mpt3sas_base_free_resources - free resources controller resources
6905 * @ioc: per adapter object
Sreekanth Reddyf92363d2012-11-30 07:44:21 +05306906 */
6907void
6908mpt3sas_base_free_resources(struct MPT3SAS_ADAPTER *ioc)
6909{
Joe Perches919d8a32018-09-17 08:01:09 -07006910 dexitprintk(ioc, ioc_info(ioc, "%s\n", __func__));
Sreekanth Reddyf92363d2012-11-30 07:44:21 +05306911
Sreekanth Reddy08c4d552015-11-11 17:30:33 +05306912 /* synchronizing freeing resource with pci_access_mutex lock */
6913 mutex_lock(&ioc->pci_access_mutex);
Joe Lawrencecf9bd21a2013-08-08 16:45:39 -04006914 if (ioc->chip_phys && ioc->chip) {
6915 _base_mask_interrupts(ioc);
6916 ioc->shost_recovery = 1;
Calvin Owens98c56ad2016-07-28 21:38:21 -07006917 _base_make_ioc_ready(ioc, SOFT_RESET);
Joe Lawrencecf9bd21a2013-08-08 16:45:39 -04006918 ioc->shost_recovery = 0;
6919 }
6920
Sreekanth Reddy580d4e32015-06-30 12:24:50 +05306921 mpt3sas_base_unmap_resources(ioc);
Sreekanth Reddy08c4d552015-11-11 17:30:33 +05306922 mutex_unlock(&ioc->pci_access_mutex);
Sreekanth Reddyf92363d2012-11-30 07:44:21 +05306923 return;
6924}
6925
6926/**
6927 * mpt3sas_base_attach - attach controller instance
6928 * @ioc: per adapter object
6929 *
Bart Van Assche4beb4862018-06-15 14:42:01 -07006930 * Return: 0 for success, non-zero for failure.
Sreekanth Reddyf92363d2012-11-30 07:44:21 +05306931 */
6932int
6933mpt3sas_base_attach(struct MPT3SAS_ADAPTER *ioc)
6934{
Suganath Prabu9c067c02019-08-03 09:59:54 -04006935 int r, i, rc;
Sreekanth Reddyf92363d2012-11-30 07:44:21 +05306936 int cpu_id, last_cpu_id = 0;
6937
Joe Perches919d8a32018-09-17 08:01:09 -07006938 dinitprintk(ioc, ioc_info(ioc, "%s\n", __func__));
Sreekanth Reddyf92363d2012-11-30 07:44:21 +05306939
6940 /* setup cpu_msix_table */
6941 ioc->cpu_count = num_online_cpus();
6942 for_each_online_cpu(cpu_id)
6943 last_cpu_id = cpu_id;
6944 ioc->cpu_msix_table_sz = last_cpu_id + 1;
6945 ioc->cpu_msix_table = kzalloc(ioc->cpu_msix_table_sz, GFP_KERNEL);
6946 ioc->reply_queue_count = 1;
6947 if (!ioc->cpu_msix_table) {
Joe Perches919d8a32018-09-17 08:01:09 -07006948 dfailprintk(ioc,
6949 ioc_info(ioc, "allocation for cpu_msix_table failed!!!\n"));
Sreekanth Reddyf92363d2012-11-30 07:44:21 +05306950 r = -ENOMEM;
6951 goto out_free_resources;
6952 }
6953
Sreekanth Reddy7786ab62015-11-11 17:30:28 +05306954 if (ioc->is_warpdrive) {
6955 ioc->reply_post_host_index = kcalloc(ioc->cpu_msix_table_sz,
6956 sizeof(resource_size_t *), GFP_KERNEL);
6957 if (!ioc->reply_post_host_index) {
Joe Perches919d8a32018-09-17 08:01:09 -07006958 dfailprintk(ioc,
6959 ioc_info(ioc, "allocation for reply_post_host_index failed!!!\n"));
Sreekanth Reddy7786ab62015-11-11 17:30:28 +05306960 r = -ENOMEM;
6961 goto out_free_resources;
6962 }
6963 }
6964
Sreekanth Reddy610ef1e2019-06-24 10:42:55 -04006965 ioc->smp_affinity_enable = smp_affinity_enable;
6966
Sreekanth Reddy9b05c912014-09-12 15:35:31 +05306967 ioc->rdpq_array_enable_assigned = 0;
6968 ioc->dma_mask = 0;
Suganath Prabub8992022018-12-07 12:58:33 +05306969 if (ioc->is_aero_ioc)
6970 ioc->base_readl = &_base_readl_aero;
6971 else
6972 ioc->base_readl = &_base_readl;
Sreekanth Reddyf92363d2012-11-30 07:44:21 +05306973 r = mpt3sas_base_map_resources(ioc);
6974 if (r)
6975 goto out_free_resources;
6976
Sreekanth Reddyf92363d2012-11-30 07:44:21 +05306977 pci_set_drvdata(ioc->pdev, ioc->shost);
Calvin Owens98c56ad2016-07-28 21:38:21 -07006978 r = _base_get_ioc_facts(ioc);
Suganath Prabu9c067c02019-08-03 09:59:54 -04006979 if (r) {
6980 rc = _base_check_for_fault_and_issue_reset(ioc);
6981 if (rc || (_base_get_ioc_facts(ioc)))
6982 goto out_free_resources;
6983 }
Sreekanth Reddyf92363d2012-11-30 07:44:21 +05306984
Sreekanth Reddy471ef9d2015-11-11 17:30:24 +05306985 switch (ioc->hba_mpi_version_belonged) {
6986 case MPI2_VERSION:
6987 ioc->build_sg_scmd = &_base_build_sg_scmd;
6988 ioc->build_sg = &_base_build_sg;
6989 ioc->build_zero_len_sge = &_base_build_zero_len_sge;
Suganath Prabu S5dd48a52019-05-31 08:14:38 -04006990 ioc->get_msix_index_for_smlio = &_base_get_msix_index;
Sreekanth Reddy471ef9d2015-11-11 17:30:24 +05306991 break;
6992 case MPI25_VERSION:
Suganath prabu Subramanib130b0d2016-01-28 12:06:58 +05306993 case MPI26_VERSION:
Sreekanth Reddy471ef9d2015-11-11 17:30:24 +05306994 /*
6995 * In SAS3.0,
6996 * SCSI_IO, SMP_PASSTHRU, SATA_PASSTHRU, Target Assist, and
6997 * Target Status - all require the IEEE formated scatter gather
6998 * elements.
6999 */
7000 ioc->build_sg_scmd = &_base_build_sg_scmd_ieee;
7001 ioc->build_sg = &_base_build_sg_ieee;
Suganath Prabu Subramaniaff39e62017-10-31 18:02:29 +05307002 ioc->build_nvme_prp = &_base_build_nvme_prp;
Sreekanth Reddy471ef9d2015-11-11 17:30:24 +05307003 ioc->build_zero_len_sge = &_base_build_zero_len_sge_ieee;
7004 ioc->sge_size_ieee = sizeof(Mpi2IeeeSgeSimple64_t);
Suganath Prabu S5dd48a52019-05-31 08:14:38 -04007005 if (ioc->high_iops_queues)
7006 ioc->get_msix_index_for_smlio =
7007 &_base_get_high_iops_msix_index;
7008 else
7009 ioc->get_msix_index_for_smlio = &_base_get_msix_index;
Sreekanth Reddy471ef9d2015-11-11 17:30:24 +05307010 break;
7011 }
Suganath Prabu S79c74d02019-05-31 08:14:35 -04007012 if (ioc->atomic_desc_capable) {
7013 ioc->put_smid_default = &_base_put_smid_default_atomic;
7014 ioc->put_smid_scsi_io = &_base_put_smid_scsi_io_atomic;
7015 ioc->put_smid_fast_path =
7016 &_base_put_smid_fast_path_atomic;
7017 ioc->put_smid_hi_priority =
7018 &_base_put_smid_hi_priority_atomic;
7019 } else {
7020 ioc->put_smid_default = &_base_put_smid_default;
7021 ioc->put_smid_fast_path = &_base_put_smid_fast_path;
7022 ioc->put_smid_hi_priority = &_base_put_smid_hi_priority;
7023 if (ioc->is_mcpu_endpoint)
7024 ioc->put_smid_scsi_io =
7025 &_base_put_smid_mpi_ep_scsi_io;
7026 else
7027 ioc->put_smid_scsi_io = &_base_put_smid_scsi_io;
7028 }
Sreekanth Reddyf92363d2012-11-30 07:44:21 +05307029 /*
7030 * These function pointers for other requests that don't
7031 * the require IEEE scatter gather elements.
7032 *
7033 * For example Configuration Pages and SAS IOUNIT Control don't.
7034 */
7035 ioc->build_sg_mpi = &_base_build_sg;
7036 ioc->build_zero_len_sge_mpi = &_base_build_zero_len_sge;
7037
Calvin Owens98c56ad2016-07-28 21:38:21 -07007038 r = _base_make_ioc_ready(ioc, SOFT_RESET);
Sreekanth Reddyf92363d2012-11-30 07:44:21 +05307039 if (r)
7040 goto out_free_resources;
7041
7042 ioc->pfacts = kcalloc(ioc->facts.NumberOfPorts,
7043 sizeof(struct mpt3sas_port_facts), GFP_KERNEL);
7044 if (!ioc->pfacts) {
7045 r = -ENOMEM;
7046 goto out_free_resources;
7047 }
7048
7049 for (i = 0 ; i < ioc->facts.NumberOfPorts; i++) {
Calvin Owens98c56ad2016-07-28 21:38:21 -07007050 r = _base_get_port_facts(ioc, i);
Suganath Prabu9c067c02019-08-03 09:59:54 -04007051 if (r) {
7052 rc = _base_check_for_fault_and_issue_reset(ioc);
7053 if (rc || (_base_get_port_facts(ioc, i)))
7054 goto out_free_resources;
7055 }
Sreekanth Reddyf92363d2012-11-30 07:44:21 +05307056 }
7057
Calvin Owens98c56ad2016-07-28 21:38:21 -07007058 r = _base_allocate_memory_pools(ioc);
Sreekanth Reddyf92363d2012-11-30 07:44:21 +05307059 if (r)
7060 goto out_free_resources;
7061
Suganath Prabu288addd2019-02-15 02:40:29 -05007062 if (irqpoll_weight > 0)
7063 ioc->thresh_hold = irqpoll_weight;
7064 else
7065 ioc->thresh_hold = ioc->hba_queue_depth/4;
7066
Suganath Prabu320e77a2019-02-15 02:40:27 -05007067 _base_init_irqpolls(ioc);
Sreekanth Reddyf92363d2012-11-30 07:44:21 +05307068 init_waitqueue_head(&ioc->reset_wq);
7069
7070 /* allocate memory pd handle bitmask list */
7071 ioc->pd_handles_sz = (ioc->facts.MaxDevHandle / 8);
7072 if (ioc->facts.MaxDevHandle % 8)
7073 ioc->pd_handles_sz++;
7074 ioc->pd_handles = kzalloc(ioc->pd_handles_sz,
7075 GFP_KERNEL);
7076 if (!ioc->pd_handles) {
7077 r = -ENOMEM;
7078 goto out_free_resources;
7079 }
7080 ioc->blocking_handles = kzalloc(ioc->pd_handles_sz,
7081 GFP_KERNEL);
7082 if (!ioc->blocking_handles) {
7083 r = -ENOMEM;
7084 goto out_free_resources;
7085 }
7086
Suganath Prabu Subramanic696f7b2016-10-26 13:34:34 +05307087 /* allocate memory for pending OS device add list */
7088 ioc->pend_os_device_add_sz = (ioc->facts.MaxDevHandle / 8);
7089 if (ioc->facts.MaxDevHandle % 8)
7090 ioc->pend_os_device_add_sz++;
7091 ioc->pend_os_device_add = kzalloc(ioc->pend_os_device_add_sz,
7092 GFP_KERNEL);
7093 if (!ioc->pend_os_device_add)
7094 goto out_free_resources;
7095
7096 ioc->device_remove_in_progress_sz = ioc->pend_os_device_add_sz;
7097 ioc->device_remove_in_progress =
7098 kzalloc(ioc->device_remove_in_progress_sz, GFP_KERNEL);
7099 if (!ioc->device_remove_in_progress)
7100 goto out_free_resources;
7101
Sreekanth Reddyf92363d2012-11-30 07:44:21 +05307102 ioc->fwfault_debug = mpt3sas_fwfault_debug;
7103
7104 /* base internal command bits */
7105 mutex_init(&ioc->base_cmds.mutex);
7106 ioc->base_cmds.reply = kzalloc(ioc->reply_sz, GFP_KERNEL);
7107 ioc->base_cmds.status = MPT3_CMD_NOT_USED;
7108
7109 /* port_enable command bits */
7110 ioc->port_enable_cmds.reply = kzalloc(ioc->reply_sz, GFP_KERNEL);
7111 ioc->port_enable_cmds.status = MPT3_CMD_NOT_USED;
7112
7113 /* transport internal command bits */
7114 ioc->transport_cmds.reply = kzalloc(ioc->reply_sz, GFP_KERNEL);
7115 ioc->transport_cmds.status = MPT3_CMD_NOT_USED;
7116 mutex_init(&ioc->transport_cmds.mutex);
7117
7118 /* scsih internal command bits */
7119 ioc->scsih_cmds.reply = kzalloc(ioc->reply_sz, GFP_KERNEL);
7120 ioc->scsih_cmds.status = MPT3_CMD_NOT_USED;
7121 mutex_init(&ioc->scsih_cmds.mutex);
7122
7123 /* task management internal command bits */
7124 ioc->tm_cmds.reply = kzalloc(ioc->reply_sz, GFP_KERNEL);
7125 ioc->tm_cmds.status = MPT3_CMD_NOT_USED;
7126 mutex_init(&ioc->tm_cmds.mutex);
7127
7128 /* config page internal command bits */
7129 ioc->config_cmds.reply = kzalloc(ioc->reply_sz, GFP_KERNEL);
7130 ioc->config_cmds.status = MPT3_CMD_NOT_USED;
7131 mutex_init(&ioc->config_cmds.mutex);
7132
7133 /* ctl module internal command bits */
7134 ioc->ctl_cmds.reply = kzalloc(ioc->reply_sz, GFP_KERNEL);
7135 ioc->ctl_cmds.sense = kzalloc(SCSI_SENSE_BUFFERSIZE, GFP_KERNEL);
7136 ioc->ctl_cmds.status = MPT3_CMD_NOT_USED;
7137 mutex_init(&ioc->ctl_cmds.mutex);
7138
Christophe JAILLETa5dd7ef2017-08-07 00:51:29 +02007139 if (!ioc->base_cmds.reply || !ioc->port_enable_cmds.reply ||
7140 !ioc->transport_cmds.reply || !ioc->scsih_cmds.reply ||
7141 !ioc->tm_cmds.reply || !ioc->config_cmds.reply ||
7142 !ioc->ctl_cmds.reply || !ioc->ctl_cmds.sense) {
Sreekanth Reddyf92363d2012-11-30 07:44:21 +05307143 r = -ENOMEM;
7144 goto out_free_resources;
7145 }
7146
7147 for (i = 0; i < MPI2_EVENT_NOTIFY_EVENTMASK_WORDS; i++)
7148 ioc->event_masks[i] = -1;
7149
7150 /* here we enable the events we care about */
7151 _base_unmask_events(ioc, MPI2_EVENT_SAS_DISCOVERY);
7152 _base_unmask_events(ioc, MPI2_EVENT_SAS_BROADCAST_PRIMITIVE);
7153 _base_unmask_events(ioc, MPI2_EVENT_SAS_TOPOLOGY_CHANGE_LIST);
7154 _base_unmask_events(ioc, MPI2_EVENT_SAS_DEVICE_STATUS_CHANGE);
7155 _base_unmask_events(ioc, MPI2_EVENT_SAS_ENCL_DEVICE_STATUS_CHANGE);
7156 _base_unmask_events(ioc, MPI2_EVENT_IR_CONFIGURATION_CHANGE_LIST);
7157 _base_unmask_events(ioc, MPI2_EVENT_IR_VOLUME);
7158 _base_unmask_events(ioc, MPI2_EVENT_IR_PHYSICAL_DISK);
7159 _base_unmask_events(ioc, MPI2_EVENT_IR_OPERATION_STATUS);
7160 _base_unmask_events(ioc, MPI2_EVENT_LOG_ENTRY_ADDED);
Sreekanth Reddy2d8ce8c2015-01-12 11:38:56 +05307161 _base_unmask_events(ioc, MPI2_EVENT_TEMP_THRESHOLD);
Sreekanth Reddyb99b1992017-10-10 18:41:14 +05307162 _base_unmask_events(ioc, MPI2_EVENT_ACTIVE_CABLE_EXCEPTION);
Chaitra P B95540b82018-04-24 05:28:35 -04007163 _base_unmask_events(ioc, MPI2_EVENT_SAS_DEVICE_DISCOVERY_ERROR);
Suganath Prabu Subramani4318c732017-10-31 18:02:32 +05307164 if (ioc->hba_mpi_version_belonged == MPI26_VERSION) {
7165 if (ioc->is_gen35_ioc) {
7166 _base_unmask_events(ioc,
7167 MPI2_EVENT_PCIE_DEVICE_STATUS_CHANGE);
7168 _base_unmask_events(ioc, MPI2_EVENT_PCIE_ENUMERATION);
7169 _base_unmask_events(ioc,
7170 MPI2_EVENT_PCIE_TOPOLOGY_CHANGE_LIST);
7171 }
7172 }
Calvin Owens98c56ad2016-07-28 21:38:21 -07007173 r = _base_make_ioc_operational(ioc);
Sreekanth Reddyf92363d2012-11-30 07:44:21 +05307174 if (r)
7175 goto out_free_resources;
7176
Suganath Prabuffedeae2019-08-03 09:59:48 -04007177 /*
7178 * Copy current copy of IOCFacts in prev_fw_facts
7179 * and it will be used during online firmware upgrade.
7180 */
7181 memcpy(&ioc->prev_fw_facts, &ioc->facts,
7182 sizeof(struct mpt3sas_facts));
7183
Sreekanth Reddy16e179b2015-11-11 17:30:27 +05307184 ioc->non_operational_loop = 0;
Chaitra P B459325c2017-01-23 15:26:08 +05307185 ioc->got_task_abort_from_ioctl = 0;
Sreekanth Reddyf92363d2012-11-30 07:44:21 +05307186 return 0;
7187
7188 out_free_resources:
7189
7190 ioc->remove_host = 1;
7191
7192 mpt3sas_base_free_resources(ioc);
7193 _base_release_memory_pools(ioc);
7194 pci_set_drvdata(ioc->pdev, NULL);
7195 kfree(ioc->cpu_msix_table);
Sreekanth Reddy7786ab62015-11-11 17:30:28 +05307196 if (ioc->is_warpdrive)
7197 kfree(ioc->reply_post_host_index);
Sreekanth Reddyf92363d2012-11-30 07:44:21 +05307198 kfree(ioc->pd_handles);
7199 kfree(ioc->blocking_handles);
Suganath Prabu Subramanic696f7b2016-10-26 13:34:34 +05307200 kfree(ioc->device_remove_in_progress);
7201 kfree(ioc->pend_os_device_add);
Sreekanth Reddyf92363d2012-11-30 07:44:21 +05307202 kfree(ioc->tm_cmds.reply);
7203 kfree(ioc->transport_cmds.reply);
7204 kfree(ioc->scsih_cmds.reply);
7205 kfree(ioc->config_cmds.reply);
7206 kfree(ioc->base_cmds.reply);
7207 kfree(ioc->port_enable_cmds.reply);
7208 kfree(ioc->ctl_cmds.reply);
7209 kfree(ioc->ctl_cmds.sense);
7210 kfree(ioc->pfacts);
7211 ioc->ctl_cmds.reply = NULL;
7212 ioc->base_cmds.reply = NULL;
7213 ioc->tm_cmds.reply = NULL;
7214 ioc->scsih_cmds.reply = NULL;
7215 ioc->transport_cmds.reply = NULL;
7216 ioc->config_cmds.reply = NULL;
7217 ioc->pfacts = NULL;
7218 return r;
7219}
7220
7221
7222/**
7223 * mpt3sas_base_detach - remove controller instance
7224 * @ioc: per adapter object
Sreekanth Reddyf92363d2012-11-30 07:44:21 +05307225 */
7226void
7227mpt3sas_base_detach(struct MPT3SAS_ADAPTER *ioc)
7228{
Joe Perches919d8a32018-09-17 08:01:09 -07007229 dexitprintk(ioc, ioc_info(ioc, "%s\n", __func__));
Sreekanth Reddyf92363d2012-11-30 07:44:21 +05307230
7231 mpt3sas_base_stop_watchdog(ioc);
7232 mpt3sas_base_free_resources(ioc);
7233 _base_release_memory_pools(ioc);
Chaitra P B22a923c2018-04-24 05:28:38 -04007234 mpt3sas_free_enclosure_list(ioc);
Sreekanth Reddyf92363d2012-11-30 07:44:21 +05307235 pci_set_drvdata(ioc->pdev, NULL);
7236 kfree(ioc->cpu_msix_table);
Sreekanth Reddy7786ab62015-11-11 17:30:28 +05307237 if (ioc->is_warpdrive)
7238 kfree(ioc->reply_post_host_index);
Sreekanth Reddyf92363d2012-11-30 07:44:21 +05307239 kfree(ioc->pd_handles);
7240 kfree(ioc->blocking_handles);
Suganath Prabu Subramanic696f7b2016-10-26 13:34:34 +05307241 kfree(ioc->device_remove_in_progress);
7242 kfree(ioc->pend_os_device_add);
Sreekanth Reddyf92363d2012-11-30 07:44:21 +05307243 kfree(ioc->pfacts);
7244 kfree(ioc->ctl_cmds.reply);
7245 kfree(ioc->ctl_cmds.sense);
7246 kfree(ioc->base_cmds.reply);
7247 kfree(ioc->port_enable_cmds.reply);
7248 kfree(ioc->tm_cmds.reply);
7249 kfree(ioc->transport_cmds.reply);
7250 kfree(ioc->scsih_cmds.reply);
7251 kfree(ioc->config_cmds.reply);
7252}
7253
7254/**
Bart Van Asschec7a35702018-06-15 14:42:00 -07007255 * _base_pre_reset_handler - pre reset handler
Sreekanth Reddyf92363d2012-11-30 07:44:21 +05307256 * @ioc: per adapter object
Sreekanth Reddyf92363d2012-11-30 07:44:21 +05307257 */
Bart Van Asschec7a35702018-06-15 14:42:00 -07007258static void _base_pre_reset_handler(struct MPT3SAS_ADAPTER *ioc)
Sreekanth Reddyf92363d2012-11-30 07:44:21 +05307259{
Bart Van Asschec7a35702018-06-15 14:42:00 -07007260 mpt3sas_scsih_pre_reset_handler(ioc);
7261 mpt3sas_ctl_pre_reset_handler(ioc);
Joe Perches919d8a32018-09-17 08:01:09 -07007262 dtmprintk(ioc, ioc_info(ioc, "%s: MPT3_IOC_PRE_RESET\n", __func__));
Bart Van Asschec7a35702018-06-15 14:42:00 -07007263}
7264
7265/**
7266 * _base_after_reset_handler - after reset handler
7267 * @ioc: per adapter object
7268 */
7269static void _base_after_reset_handler(struct MPT3SAS_ADAPTER *ioc)
7270{
7271 mpt3sas_scsih_after_reset_handler(ioc);
7272 mpt3sas_ctl_after_reset_handler(ioc);
Joe Perches919d8a32018-09-17 08:01:09 -07007273 dtmprintk(ioc, ioc_info(ioc, "%s: MPT3_IOC_AFTER_RESET\n", __func__));
Bart Van Asschec7a35702018-06-15 14:42:00 -07007274 if (ioc->transport_cmds.status & MPT3_CMD_PENDING) {
7275 ioc->transport_cmds.status |= MPT3_CMD_RESET;
7276 mpt3sas_base_free_smid(ioc, ioc->transport_cmds.smid);
7277 complete(&ioc->transport_cmds.done);
Sreekanth Reddyf92363d2012-11-30 07:44:21 +05307278 }
Bart Van Asschec7a35702018-06-15 14:42:00 -07007279 if (ioc->base_cmds.status & MPT3_CMD_PENDING) {
7280 ioc->base_cmds.status |= MPT3_CMD_RESET;
7281 mpt3sas_base_free_smid(ioc, ioc->base_cmds.smid);
7282 complete(&ioc->base_cmds.done);
7283 }
7284 if (ioc->port_enable_cmds.status & MPT3_CMD_PENDING) {
7285 ioc->port_enable_failed = 1;
7286 ioc->port_enable_cmds.status |= MPT3_CMD_RESET;
7287 mpt3sas_base_free_smid(ioc, ioc->port_enable_cmds.smid);
7288 if (ioc->is_driver_loading) {
7289 ioc->start_scan_failed =
7290 MPI2_IOCSTATUS_INTERNAL_ERROR;
7291 ioc->start_scan = 0;
7292 ioc->port_enable_cmds.status =
7293 MPT3_CMD_NOT_USED;
7294 } else {
7295 complete(&ioc->port_enable_cmds.done);
7296 }
7297 }
7298 if (ioc->config_cmds.status & MPT3_CMD_PENDING) {
7299 ioc->config_cmds.status |= MPT3_CMD_RESET;
7300 mpt3sas_base_free_smid(ioc, ioc->config_cmds.smid);
7301 ioc->config_cmds.smid = USHRT_MAX;
7302 complete(&ioc->config_cmds.done);
7303 }
7304}
7305
7306/**
7307 * _base_reset_done_handler - reset done handler
7308 * @ioc: per adapter object
7309 */
7310static void _base_reset_done_handler(struct MPT3SAS_ADAPTER *ioc)
7311{
7312 mpt3sas_scsih_reset_done_handler(ioc);
7313 mpt3sas_ctl_reset_done_handler(ioc);
Joe Perches919d8a32018-09-17 08:01:09 -07007314 dtmprintk(ioc, ioc_info(ioc, "%s: MPT3_IOC_DONE_RESET\n", __func__));
Sreekanth Reddyf92363d2012-11-30 07:44:21 +05307315}
7316
7317/**
Sreekanth Reddyc666d3b2018-02-16 20:39:58 -02007318 * mpt3sas_wait_for_commands_to_complete - reset controller
Sreekanth Reddyf92363d2012-11-30 07:44:21 +05307319 * @ioc: Pointer to MPT_ADAPTER structure
Sreekanth Reddyf92363d2012-11-30 07:44:21 +05307320 *
Hannes Reinecke272e2532018-01-04 04:57:10 -08007321 * This function is waiting 10s for all pending commands to complete
Sreekanth Reddyf92363d2012-11-30 07:44:21 +05307322 * prior to putting controller in reset.
7323 */
Sreekanth Reddyc666d3b2018-02-16 20:39:58 -02007324void
7325mpt3sas_wait_for_commands_to_complete(struct MPT3SAS_ADAPTER *ioc)
Sreekanth Reddyf92363d2012-11-30 07:44:21 +05307326{
7327 u32 ioc_state;
Sreekanth Reddyf92363d2012-11-30 07:44:21 +05307328
7329 ioc->pending_io_count = 0;
Sreekanth Reddyf92363d2012-11-30 07:44:21 +05307330
7331 ioc_state = mpt3sas_base_get_iocstate(ioc, 0);
7332 if ((ioc_state & MPI2_IOC_STATE_MASK) != MPI2_IOC_STATE_OPERATIONAL)
7333 return;
7334
7335 /* pending command count */
Ming Leic84b0232018-06-24 22:03:26 +08007336 ioc->pending_io_count = scsi_host_busy(ioc->shost);
Sreekanth Reddyf92363d2012-11-30 07:44:21 +05307337
7338 if (!ioc->pending_io_count)
7339 return;
7340
7341 /* wait for pending commands to complete */
7342 wait_event_timeout(ioc->reset_wq, ioc->pending_io_count == 0, 10 * HZ);
7343}
7344
7345/**
Suganath Prabuffedeae2019-08-03 09:59:48 -04007346 * _base_check_ioc_facts_changes - Look for increase/decrease of IOCFacts
7347 * attributes during online firmware upgrade and update the corresponding
7348 * IOC variables accordingly.
7349 *
7350 * @ioc: Pointer to MPT_ADAPTER structure
7351 */
7352static int
7353_base_check_ioc_facts_changes(struct MPT3SAS_ADAPTER *ioc)
7354{
7355 u16 pd_handles_sz;
7356 void *pd_handles = NULL, *blocking_handles = NULL;
7357 void *pend_os_device_add = NULL, *device_remove_in_progress = NULL;
7358 struct mpt3sas_facts *old_facts = &ioc->prev_fw_facts;
7359
7360 if (ioc->facts.MaxDevHandle > old_facts->MaxDevHandle) {
7361 pd_handles_sz = (ioc->facts.MaxDevHandle / 8);
7362 if (ioc->facts.MaxDevHandle % 8)
7363 pd_handles_sz++;
7364
7365 pd_handles = krealloc(ioc->pd_handles, pd_handles_sz,
7366 GFP_KERNEL);
7367 if (!pd_handles) {
7368 ioc_info(ioc,
7369 "Unable to allocate the memory for pd_handles of sz: %d\n",
7370 pd_handles_sz);
7371 return -ENOMEM;
7372 }
7373 memset(pd_handles + ioc->pd_handles_sz, 0,
7374 (pd_handles_sz - ioc->pd_handles_sz));
7375 ioc->pd_handles = pd_handles;
7376
7377 blocking_handles = krealloc(ioc->blocking_handles,
7378 pd_handles_sz, GFP_KERNEL);
7379 if (!blocking_handles) {
7380 ioc_info(ioc,
7381 "Unable to allocate the memory for "
7382 "blocking_handles of sz: %d\n",
7383 pd_handles_sz);
7384 return -ENOMEM;
7385 }
7386 memset(blocking_handles + ioc->pd_handles_sz, 0,
7387 (pd_handles_sz - ioc->pd_handles_sz));
7388 ioc->blocking_handles = blocking_handles;
7389 ioc->pd_handles_sz = pd_handles_sz;
7390
7391 pend_os_device_add = krealloc(ioc->pend_os_device_add,
7392 pd_handles_sz, GFP_KERNEL);
7393 if (!pend_os_device_add) {
7394 ioc_info(ioc,
7395 "Unable to allocate the memory for pend_os_device_add of sz: %d\n",
7396 pd_handles_sz);
7397 return -ENOMEM;
7398 }
7399 memset(pend_os_device_add + ioc->pend_os_device_add_sz, 0,
7400 (pd_handles_sz - ioc->pend_os_device_add_sz));
7401 ioc->pend_os_device_add = pend_os_device_add;
7402 ioc->pend_os_device_add_sz = pd_handles_sz;
7403
7404 device_remove_in_progress = krealloc(
7405 ioc->device_remove_in_progress, pd_handles_sz, GFP_KERNEL);
7406 if (!device_remove_in_progress) {
7407 ioc_info(ioc,
7408 "Unable to allocate the memory for "
7409 "device_remove_in_progress of sz: %d\n "
7410 , pd_handles_sz);
7411 return -ENOMEM;
7412 }
7413 memset(device_remove_in_progress +
7414 ioc->device_remove_in_progress_sz, 0,
7415 (pd_handles_sz - ioc->device_remove_in_progress_sz));
7416 ioc->device_remove_in_progress = device_remove_in_progress;
7417 ioc->device_remove_in_progress_sz = pd_handles_sz;
7418 }
7419
7420 memcpy(&ioc->prev_fw_facts, &ioc->facts, sizeof(struct mpt3sas_facts));
7421 return 0;
7422}
7423
7424/**
Sreekanth Reddyf92363d2012-11-30 07:44:21 +05307425 * mpt3sas_base_hard_reset_handler - reset controller
7426 * @ioc: Pointer to MPT_ADAPTER structure
Sreekanth Reddyf92363d2012-11-30 07:44:21 +05307427 * @type: FORCE_BIG_HAMMER or SOFT_RESET
7428 *
Bart Van Assche4beb4862018-06-15 14:42:01 -07007429 * Return: 0 for success, non-zero for failure.
Sreekanth Reddyf92363d2012-11-30 07:44:21 +05307430 */
7431int
Calvin Owens98c56ad2016-07-28 21:38:21 -07007432mpt3sas_base_hard_reset_handler(struct MPT3SAS_ADAPTER *ioc,
Sreekanth Reddyf92363d2012-11-30 07:44:21 +05307433 enum reset_type type)
7434{
7435 int r;
7436 unsigned long flags;
7437 u32 ioc_state;
7438 u8 is_fault = 0, is_trigger = 0;
7439
Joe Perches919d8a32018-09-17 08:01:09 -07007440 dtmprintk(ioc, ioc_info(ioc, "%s: enter\n", __func__));
Sreekanth Reddyf92363d2012-11-30 07:44:21 +05307441
7442 if (ioc->pci_error_recovery) {
Joe Perches919d8a32018-09-17 08:01:09 -07007443 ioc_err(ioc, "%s: pci error recovery reset\n", __func__);
Sreekanth Reddyf92363d2012-11-30 07:44:21 +05307444 r = 0;
7445 goto out_unlocked;
7446 }
7447
7448 if (mpt3sas_fwfault_debug)
7449 mpt3sas_halt_firmware(ioc);
7450
Sreekanth Reddyf92363d2012-11-30 07:44:21 +05307451 /* wait for an active reset in progress to complete */
Bart Van Assche982ea6f2018-06-15 14:41:59 -07007452 mutex_lock(&ioc->reset_in_progress_mutex);
Sreekanth Reddyf92363d2012-11-30 07:44:21 +05307453
7454 spin_lock_irqsave(&ioc->ioc_reset_in_progress_lock, flags);
7455 ioc->shost_recovery = 1;
7456 spin_unlock_irqrestore(&ioc->ioc_reset_in_progress_lock, flags);
7457
7458 if ((ioc->diag_buffer_status[MPI2_DIAG_BUF_TYPE_TRACE] &
7459 MPT3_DIAG_BUFFER_IS_REGISTERED) &&
7460 (!(ioc->diag_buffer_status[MPI2_DIAG_BUF_TYPE_TRACE] &
7461 MPT3_DIAG_BUFFER_IS_RELEASED))) {
7462 is_trigger = 1;
7463 ioc_state = mpt3sas_base_get_iocstate(ioc, 0);
7464 if ((ioc_state & MPI2_IOC_STATE_MASK) == MPI2_IOC_STATE_FAULT)
7465 is_fault = 1;
7466 }
Bart Van Asschec7a35702018-06-15 14:42:00 -07007467 _base_pre_reset_handler(ioc);
Sreekanth Reddyc666d3b2018-02-16 20:39:58 -02007468 mpt3sas_wait_for_commands_to_complete(ioc);
Sreekanth Reddyf92363d2012-11-30 07:44:21 +05307469 _base_mask_interrupts(ioc);
Calvin Owens98c56ad2016-07-28 21:38:21 -07007470 r = _base_make_ioc_ready(ioc, type);
Sreekanth Reddyf92363d2012-11-30 07:44:21 +05307471 if (r)
7472 goto out;
Bart Van Asschec7a35702018-06-15 14:42:00 -07007473 _base_after_reset_handler(ioc);
Sreekanth Reddyf92363d2012-11-30 07:44:21 +05307474
7475 /* If this hard reset is called while port enable is active, then
7476 * there is no reason to call make_ioc_operational
7477 */
7478 if (ioc->is_driver_loading && ioc->port_enable_failed) {
7479 ioc->remove_host = 1;
7480 r = -EFAULT;
7481 goto out;
7482 }
Calvin Owens98c56ad2016-07-28 21:38:21 -07007483 r = _base_get_ioc_facts(ioc);
Sreekanth Reddyf92363d2012-11-30 07:44:21 +05307484 if (r)
7485 goto out;
Sreekanth Reddy9b05c912014-09-12 15:35:31 +05307486
Suganath Prabuffedeae2019-08-03 09:59:48 -04007487 r = _base_check_ioc_facts_changes(ioc);
7488 if (r) {
7489 ioc_info(ioc,
7490 "Some of the parameters got changed in this new firmware"
7491 " image and it requires system reboot\n");
7492 goto out;
7493 }
Sreekanth Reddy9b05c912014-09-12 15:35:31 +05307494 if (ioc->rdpq_array_enable && !ioc->rdpq_array_capable)
7495 panic("%s: Issue occurred with flashing controller firmware."
7496 "Please reboot the system and ensure that the correct"
7497 " firmware version is running\n", ioc->name);
7498
Calvin Owens98c56ad2016-07-28 21:38:21 -07007499 r = _base_make_ioc_operational(ioc);
Sreekanth Reddyf92363d2012-11-30 07:44:21 +05307500 if (!r)
Bart Van Asschec7a35702018-06-15 14:42:00 -07007501 _base_reset_done_handler(ioc);
Sreekanth Reddyf92363d2012-11-30 07:44:21 +05307502
7503 out:
Joe Perches919d8a32018-09-17 08:01:09 -07007504 dtmprintk(ioc,
7505 ioc_info(ioc, "%s: %s\n",
7506 __func__, r == 0 ? "SUCCESS" : "FAILED"));
Sreekanth Reddyf92363d2012-11-30 07:44:21 +05307507
7508 spin_lock_irqsave(&ioc->ioc_reset_in_progress_lock, flags);
Sreekanth Reddyf92363d2012-11-30 07:44:21 +05307509 ioc->shost_recovery = 0;
7510 spin_unlock_irqrestore(&ioc->ioc_reset_in_progress_lock, flags);
7511 ioc->ioc_reset_count++;
7512 mutex_unlock(&ioc->reset_in_progress_mutex);
7513
7514 out_unlocked:
7515 if ((r == 0) && is_trigger) {
7516 if (is_fault)
7517 mpt3sas_trigger_master(ioc, MASTER_TRIGGER_FW_FAULT);
7518 else
7519 mpt3sas_trigger_master(ioc,
7520 MASTER_TRIGGER_ADAPTER_RESET);
7521 }
Joe Perches919d8a32018-09-17 08:01:09 -07007522 dtmprintk(ioc, ioc_info(ioc, "%s: exit\n", __func__));
Sreekanth Reddyf92363d2012-11-30 07:44:21 +05307523 return r;
7524}