• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
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
6  * Copyright (C) 2012-2014  LSI Corporation
7  * Copyright (C) 2013-2014 Avago Technologies
8  *  (mailto: MPT-FusionLinux.pdl@avagotech.com)
9  *
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 
46 #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>
60 #include <linux/ktime.h>
61 #include <linux/kthread.h>
62 #include <asm/page.h>        /* To get host page size per arch */
63 #include <linux/aer.h>
64 
65 
66 #include "mpt3sas_base.h"
67 
68 static 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
76 static int max_queue_depth = -1;
77 module_param(max_queue_depth, int, 0444);
78 MODULE_PARM_DESC(max_queue_depth, " max controller queue depth ");
79 
80 static int max_sgl_entries = -1;
81 module_param(max_sgl_entries, int, 0444);
82 MODULE_PARM_DESC(max_sgl_entries, " max sg entries ");
83 
84 static int msix_disable = -1;
85 module_param(msix_disable, int, 0444);
86 MODULE_PARM_DESC(msix_disable, " disable msix routed interrupts (default=0)");
87 
88 static int smp_affinity_enable = 1;
89 module_param(smp_affinity_enable, int, 0444);
90 MODULE_PARM_DESC(smp_affinity_enable, "SMP affinity feature enable/disable Default: enable(1)");
91 
92 static int max_msix_vectors = -1;
93 module_param(max_msix_vectors, int, 0444);
94 MODULE_PARM_DESC(max_msix_vectors,
95 	" max msix vectors");
96 
97 static int irqpoll_weight = -1;
98 module_param(irqpoll_weight, int, 0444);
99 MODULE_PARM_DESC(irqpoll_weight,
100 	"irq poll weight (default= one fourth of HBA queue depth)");
101 
102 static int mpt3sas_fwfault_debug;
103 MODULE_PARM_DESC(mpt3sas_fwfault_debug,
104 	" enable detection of firmware fault and halt firmware - (default=0)");
105 
106 static int perf_mode = -1;
107 module_param(perf_mode, int, 0444);
108 MODULE_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"
116 	"\t\tdefault - default perf_mode is 'balanced'"
117 	);
118 
119 static int poll_queues;
120 module_param(poll_queues, int, 0444);
121 MODULE_PARM_DESC(poll_queues, "Number of queues to be use for io_uring poll mode.\n\t\t"
122 	"This parameter is effective only if host_tagset_enable=1. &\n\t\t"
123 	"when poll_queues are enabled then &\n\t\t"
124 	"perf_mode is set to latency mode. &\n\t\t"
125 	);
126 
127 enum mpt3sas_perf_mode {
128 	MPT_PERF_MODE_DEFAULT	= -1,
129 	MPT_PERF_MODE_BALANCED	= 0,
130 	MPT_PERF_MODE_IOPS	= 1,
131 	MPT_PERF_MODE_LATENCY	= 2,
132 };
133 
134 static int
135 _base_wait_on_iocstate(struct MPT3SAS_ADAPTER *ioc,
136 		u32 ioc_state, int timeout);
137 static int
138 _base_get_ioc_facts(struct MPT3SAS_ADAPTER *ioc);
139 static void
140 _base_clear_outstanding_commands(struct MPT3SAS_ADAPTER *ioc);
141 
142 static u32
143 _base_readl_ext_retry(const volatile void __iomem *addr);
144 
145 /**
146  * mpt3sas_base_check_cmd_timeout - Function
147  *		to check timeout and command termination due
148  *		to Host reset.
149  *
150  * @ioc:	per adapter object.
151  * @status:	Status of issued command.
152  * @mpi_request:mf request pointer.
153  * @sz:		size of buffer.
154  *
155  * Return: 1/0 Reset to be done or Not
156  */
157 u8
mpt3sas_base_check_cmd_timeout(struct MPT3SAS_ADAPTER * ioc,u8 status,void * mpi_request,int sz)158 mpt3sas_base_check_cmd_timeout(struct MPT3SAS_ADAPTER *ioc,
159 		u8 status, void *mpi_request, int sz)
160 {
161 	u8 issue_reset = 0;
162 
163 	if (!(status & MPT3_CMD_RESET))
164 		issue_reset = 1;
165 
166 	ioc_err(ioc, "Command %s\n",
167 		issue_reset == 0 ? "terminated due to Host Reset" : "Timeout");
168 	_debug_dump_mf(mpi_request, sz);
169 
170 	return issue_reset;
171 }
172 
173 /**
174  * _scsih_set_fwfault_debug - global setting of ioc->fwfault_debug.
175  * @val: ?
176  * @kp: ?
177  *
178  * Return: ?
179  */
180 static int
_scsih_set_fwfault_debug(const char * val,const struct kernel_param * kp)181 _scsih_set_fwfault_debug(const char *val, const struct kernel_param *kp)
182 {
183 	int ret = param_set_int(val, kp);
184 	struct MPT3SAS_ADAPTER *ioc;
185 
186 	if (ret)
187 		return ret;
188 
189 	/* global ioc spinlock to protect controller list on list operations */
190 	pr_info("setting fwfault_debug(%d)\n", mpt3sas_fwfault_debug);
191 	spin_lock(&gioc_lock);
192 	list_for_each_entry(ioc, &mpt3sas_ioc_list, list)
193 		ioc->fwfault_debug = mpt3sas_fwfault_debug;
194 	spin_unlock(&gioc_lock);
195 	return 0;
196 }
197 module_param_call(mpt3sas_fwfault_debug, _scsih_set_fwfault_debug,
198 	param_get_int, &mpt3sas_fwfault_debug, 0644);
199 
200 /**
201  * _base_readl_aero - retry readl for max three times.
202  * @addr: MPT Fusion system interface register address
203  *
204  * Retry the readl() for max three times if it gets zero value
205  * while reading the system interface register.
206  */
207 static inline u32
_base_readl_aero(const volatile void __iomem * addr)208 _base_readl_aero(const volatile void __iomem *addr)
209 {
210 	u32 i = 0, ret_val;
211 
212 	do {
213 		ret_val = readl(addr);
214 		i++;
215 	} while (ret_val == 0 && i < 3);
216 
217 	return ret_val;
218 }
219 
220 static u32
_base_readl_ext_retry(const volatile void __iomem * addr)221 _base_readl_ext_retry(const volatile void __iomem *addr)
222 {
223 	u32 i, ret_val;
224 
225 	for (i = 0 ; i < 30 ; i++) {
226 		ret_val = readl(addr);
227 		if (ret_val != 0)
228 			break;
229 	}
230 
231 	return ret_val;
232 }
233 
234 static inline u32
_base_readl(const volatile void __iomem * addr)235 _base_readl(const volatile void __iomem *addr)
236 {
237 	return readl(addr);
238 }
239 
240 /**
241  * _base_clone_reply_to_sys_mem - copies reply to reply free iomem
242  *				  in BAR0 space.
243  *
244  * @ioc: per adapter object
245  * @reply: reply message frame(lower 32bit addr)
246  * @index: System request message index.
247  */
248 static void
_base_clone_reply_to_sys_mem(struct MPT3SAS_ADAPTER * ioc,u32 reply,u32 index)249 _base_clone_reply_to_sys_mem(struct MPT3SAS_ADAPTER *ioc, u32 reply,
250 		u32 index)
251 {
252 	/*
253 	 * 256 is offset within sys register.
254 	 * 256 offset MPI frame starts. Max MPI frame supported is 32.
255 	 * 32 * 128 = 4K. From here, Clone of reply free for mcpu starts
256 	 */
257 	u16 cmd_credit = ioc->facts.RequestCredit + 1;
258 	void __iomem *reply_free_iomem = (void __iomem *)ioc->chip +
259 			MPI_FRAME_START_OFFSET +
260 			(cmd_credit * ioc->request_sz) + (index * sizeof(u32));
261 
262 	writel(reply, reply_free_iomem);
263 }
264 
265 /**
266  * _base_clone_mpi_to_sys_mem - Writes/copies MPI frames
267  *				to system/BAR0 region.
268  *
269  * @dst_iomem: Pointer to the destination location in BAR0 space.
270  * @src: Pointer to the Source data.
271  * @size: Size of data to be copied.
272  */
273 static void
_base_clone_mpi_to_sys_mem(void * dst_iomem,void * src,u32 size)274 _base_clone_mpi_to_sys_mem(void *dst_iomem, void *src, u32 size)
275 {
276 	int i;
277 	u32 *src_virt_mem = (u32 *)src;
278 
279 	for (i = 0; i < size/4; i++)
280 		writel((u32)src_virt_mem[i],
281 				(void __iomem *)dst_iomem + (i * 4));
282 }
283 
284 /**
285  * _base_clone_to_sys_mem - Writes/copies data to system/BAR0 region
286  *
287  * @dst_iomem: Pointer to the destination location in BAR0 space.
288  * @src: Pointer to the Source data.
289  * @size: Size of data to be copied.
290  */
291 static void
_base_clone_to_sys_mem(void __iomem * dst_iomem,void * src,u32 size)292 _base_clone_to_sys_mem(void __iomem *dst_iomem, void *src, u32 size)
293 {
294 	int i;
295 	u32 *src_virt_mem = (u32 *)(src);
296 
297 	for (i = 0; i < size/4; i++)
298 		writel((u32)src_virt_mem[i],
299 			(void __iomem *)dst_iomem + (i * 4));
300 }
301 
302 /**
303  * _base_get_chain - Calculates and Returns virtual chain address
304  *			 for the provided smid in BAR0 space.
305  *
306  * @ioc: per adapter object
307  * @smid: system request message index
308  * @sge_chain_count: Scatter gather chain count.
309  *
310  * Return: the chain address.
311  */
312 static inline void __iomem*
_base_get_chain(struct MPT3SAS_ADAPTER * ioc,u16 smid,u8 sge_chain_count)313 _base_get_chain(struct MPT3SAS_ADAPTER *ioc, u16 smid,
314 		u8 sge_chain_count)
315 {
316 	void __iomem *base_chain, *chain_virt;
317 	u16 cmd_credit = ioc->facts.RequestCredit + 1;
318 
319 	base_chain  = (void __iomem *)ioc->chip + MPI_FRAME_START_OFFSET +
320 		(cmd_credit * ioc->request_sz) +
321 		REPLY_FREE_POOL_SIZE;
322 	chain_virt = base_chain + (smid * ioc->facts.MaxChainDepth *
323 			ioc->request_sz) + (sge_chain_count * ioc->request_sz);
324 	return chain_virt;
325 }
326 
327 /**
328  * _base_get_chain_phys - Calculates and Returns physical address
329  *			in BAR0 for scatter gather chains, for
330  *			the provided smid.
331  *
332  * @ioc: per adapter object
333  * @smid: system request message index
334  * @sge_chain_count: Scatter gather chain count.
335  *
336  * Return: Physical chain address.
337  */
338 static inline phys_addr_t
_base_get_chain_phys(struct MPT3SAS_ADAPTER * ioc,u16 smid,u8 sge_chain_count)339 _base_get_chain_phys(struct MPT3SAS_ADAPTER *ioc, u16 smid,
340 		u8 sge_chain_count)
341 {
342 	phys_addr_t base_chain_phys, chain_phys;
343 	u16 cmd_credit = ioc->facts.RequestCredit + 1;
344 
345 	base_chain_phys  = ioc->chip_phys + MPI_FRAME_START_OFFSET +
346 		(cmd_credit * ioc->request_sz) +
347 		REPLY_FREE_POOL_SIZE;
348 	chain_phys = base_chain_phys + (smid * ioc->facts.MaxChainDepth *
349 			ioc->request_sz) + (sge_chain_count * ioc->request_sz);
350 	return chain_phys;
351 }
352 
353 /**
354  * _base_get_buffer_bar0 - Calculates and Returns BAR0 mapped Host
355  *			buffer address for the provided smid.
356  *			(Each smid can have 64K starts from 17024)
357  *
358  * @ioc: per adapter object
359  * @smid: system request message index
360  *
361  * Return: Pointer to buffer location in BAR0.
362  */
363 
364 static void __iomem *
_base_get_buffer_bar0(struct MPT3SAS_ADAPTER * ioc,u16 smid)365 _base_get_buffer_bar0(struct MPT3SAS_ADAPTER *ioc, u16 smid)
366 {
367 	u16 cmd_credit = ioc->facts.RequestCredit + 1;
368 	// Added extra 1 to reach end of chain.
369 	void __iomem *chain_end = _base_get_chain(ioc,
370 			cmd_credit + 1,
371 			ioc->facts.MaxChainDepth);
372 	return chain_end + (smid * 64 * 1024);
373 }
374 
375 /**
376  * _base_get_buffer_phys_bar0 - Calculates and Returns BAR0 mapped
377  *		Host buffer Physical address for the provided smid.
378  *		(Each smid can have 64K starts from 17024)
379  *
380  * @ioc: per adapter object
381  * @smid: system request message index
382  *
383  * Return: Pointer to buffer location in BAR0.
384  */
385 static phys_addr_t
_base_get_buffer_phys_bar0(struct MPT3SAS_ADAPTER * ioc,u16 smid)386 _base_get_buffer_phys_bar0(struct MPT3SAS_ADAPTER *ioc, u16 smid)
387 {
388 	u16 cmd_credit = ioc->facts.RequestCredit + 1;
389 	phys_addr_t chain_end_phys = _base_get_chain_phys(ioc,
390 			cmd_credit + 1,
391 			ioc->facts.MaxChainDepth);
392 	return chain_end_phys + (smid * 64 * 1024);
393 }
394 
395 /**
396  * _base_get_chain_buffer_dma_to_chain_buffer - Iterates chain
397  *			lookup list and Provides chain_buffer
398  *			address for the matching dma address.
399  *			(Each smid can have 64K starts from 17024)
400  *
401  * @ioc: per adapter object
402  * @chain_buffer_dma: Chain buffer dma address.
403  *
404  * Return: Pointer to chain buffer. Or Null on Failure.
405  */
406 static void *
_base_get_chain_buffer_dma_to_chain_buffer(struct MPT3SAS_ADAPTER * ioc,dma_addr_t chain_buffer_dma)407 _base_get_chain_buffer_dma_to_chain_buffer(struct MPT3SAS_ADAPTER *ioc,
408 		dma_addr_t chain_buffer_dma)
409 {
410 	u16 index, j;
411 	struct chain_tracker *ct;
412 
413 	for (index = 0; index < ioc->scsiio_depth; index++) {
414 		for (j = 0; j < ioc->chains_needed_per_io; j++) {
415 			ct = &ioc->chain_lookup[index].chains_per_smid[j];
416 			if (ct && ct->chain_buffer_dma == chain_buffer_dma)
417 				return ct->chain_buffer;
418 		}
419 	}
420 	ioc_info(ioc, "Provided chain_buffer_dma address is not in the lookup list\n");
421 	return NULL;
422 }
423 
424 /**
425  * _clone_sg_entries -	MPI EP's scsiio and config requests
426  *			are handled here. Base function for
427  *			double buffering, before submitting
428  *			the requests.
429  *
430  * @ioc: per adapter object.
431  * @mpi_request: mf request pointer.
432  * @smid: system request message index.
433  */
_clone_sg_entries(struct MPT3SAS_ADAPTER * ioc,void * mpi_request,u16 smid)434 static void _clone_sg_entries(struct MPT3SAS_ADAPTER *ioc,
435 		void *mpi_request, u16 smid)
436 {
437 	Mpi2SGESimple32_t *sgel, *sgel_next;
438 	u32  sgl_flags, sge_chain_count = 0;
439 	bool is_write = false;
440 	u16 i = 0;
441 	void __iomem *buffer_iomem;
442 	phys_addr_t buffer_iomem_phys;
443 	void __iomem *buff_ptr;
444 	phys_addr_t buff_ptr_phys;
445 	void __iomem *dst_chain_addr[MCPU_MAX_CHAINS_PER_IO];
446 	void *src_chain_addr[MCPU_MAX_CHAINS_PER_IO];
447 	phys_addr_t dst_addr_phys;
448 	MPI2RequestHeader_t *request_hdr;
449 	struct scsi_cmnd *scmd;
450 	struct scatterlist *sg_scmd = NULL;
451 	int is_scsiio_req = 0;
452 
453 	request_hdr = (MPI2RequestHeader_t *) mpi_request;
454 
455 	if (request_hdr->Function == MPI2_FUNCTION_SCSI_IO_REQUEST) {
456 		Mpi25SCSIIORequest_t *scsiio_request =
457 			(Mpi25SCSIIORequest_t *)mpi_request;
458 		sgel = (Mpi2SGESimple32_t *) &scsiio_request->SGL;
459 		is_scsiio_req = 1;
460 	} else if (request_hdr->Function == MPI2_FUNCTION_CONFIG) {
461 		Mpi2ConfigRequest_t  *config_req =
462 			(Mpi2ConfigRequest_t *)mpi_request;
463 		sgel = (Mpi2SGESimple32_t *) &config_req->PageBufferSGE;
464 	} else
465 		return;
466 
467 	/* From smid we can get scsi_cmd, once we have sg_scmd,
468 	 * we just need to get sg_virt and sg_next to get virtual
469 	 * address associated with sgel->Address.
470 	 */
471 
472 	if (is_scsiio_req) {
473 		/* Get scsi_cmd using smid */
474 		scmd = mpt3sas_scsih_scsi_lookup_get(ioc, smid);
475 		if (scmd == NULL) {
476 			ioc_err(ioc, "scmd is NULL\n");
477 			return;
478 		}
479 
480 		/* Get sg_scmd from scmd provided */
481 		sg_scmd = scsi_sglist(scmd);
482 	}
483 
484 	/*
485 	 * 0 - 255	System register
486 	 * 256 - 4352	MPI Frame. (This is based on maxCredit 32)
487 	 * 4352 - 4864	Reply_free pool (512 byte is reserved
488 	 *		considering maxCredit 32. Reply need extra
489 	 *		room, for mCPU case kept four times of
490 	 *		maxCredit).
491 	 * 4864 - 17152	SGE chain element. (32cmd * 3 chain of
492 	 *		128 byte size = 12288)
493 	 * 17152 - x	Host buffer mapped with smid.
494 	 *		(Each smid can have 64K Max IO.)
495 	 * BAR0+Last 1K MSIX Addr and Data
496 	 * Total size in use 2113664 bytes of 4MB BAR0
497 	 */
498 
499 	buffer_iomem = _base_get_buffer_bar0(ioc, smid);
500 	buffer_iomem_phys = _base_get_buffer_phys_bar0(ioc, smid);
501 
502 	buff_ptr = buffer_iomem;
503 	buff_ptr_phys = buffer_iomem_phys;
504 	WARN_ON(buff_ptr_phys > U32_MAX);
505 
506 	if (le32_to_cpu(sgel->FlagsLength) &
507 			(MPI2_SGE_FLAGS_HOST_TO_IOC << MPI2_SGE_FLAGS_SHIFT))
508 		is_write = true;
509 
510 	for (i = 0; i < MPT_MIN_PHYS_SEGMENTS + ioc->facts.MaxChainDepth; i++) {
511 
512 		sgl_flags =
513 		    (le32_to_cpu(sgel->FlagsLength) >> MPI2_SGE_FLAGS_SHIFT);
514 
515 		switch (sgl_flags & MPI2_SGE_FLAGS_ELEMENT_MASK) {
516 		case MPI2_SGE_FLAGS_CHAIN_ELEMENT:
517 			/*
518 			 * Helper function which on passing
519 			 * chain_buffer_dma returns chain_buffer. Get
520 			 * the virtual address for sgel->Address
521 			 */
522 			sgel_next =
523 				_base_get_chain_buffer_dma_to_chain_buffer(ioc,
524 						le32_to_cpu(sgel->Address));
525 			if (sgel_next == NULL)
526 				return;
527 			/*
528 			 * This is coping 128 byte chain
529 			 * frame (not a host buffer)
530 			 */
531 			dst_chain_addr[sge_chain_count] =
532 				_base_get_chain(ioc,
533 					smid, sge_chain_count);
534 			src_chain_addr[sge_chain_count] =
535 						(void *) sgel_next;
536 			dst_addr_phys = _base_get_chain_phys(ioc,
537 						smid, sge_chain_count);
538 			WARN_ON(dst_addr_phys > U32_MAX);
539 			sgel->Address =
540 				cpu_to_le32(lower_32_bits(dst_addr_phys));
541 			sgel = sgel_next;
542 			sge_chain_count++;
543 			break;
544 		case MPI2_SGE_FLAGS_SIMPLE_ELEMENT:
545 			if (is_write) {
546 				if (is_scsiio_req) {
547 					_base_clone_to_sys_mem(buff_ptr,
548 					    sg_virt(sg_scmd),
549 					    (le32_to_cpu(sgel->FlagsLength) &
550 					    0x00ffffff));
551 					/*
552 					 * FIXME: this relies on a a zero
553 					 * PCI mem_offset.
554 					 */
555 					sgel->Address =
556 					    cpu_to_le32((u32)buff_ptr_phys);
557 				} else {
558 					_base_clone_to_sys_mem(buff_ptr,
559 					    ioc->config_vaddr,
560 					    (le32_to_cpu(sgel->FlagsLength) &
561 					    0x00ffffff));
562 					sgel->Address =
563 					    cpu_to_le32((u32)buff_ptr_phys);
564 				}
565 			}
566 			buff_ptr += (le32_to_cpu(sgel->FlagsLength) &
567 			    0x00ffffff);
568 			buff_ptr_phys += (le32_to_cpu(sgel->FlagsLength) &
569 			    0x00ffffff);
570 			if ((le32_to_cpu(sgel->FlagsLength) &
571 			    (MPI2_SGE_FLAGS_END_OF_BUFFER
572 					<< MPI2_SGE_FLAGS_SHIFT)))
573 				goto eob_clone_chain;
574 			else {
575 				/*
576 				 * Every single element in MPT will have
577 				 * associated sg_next. Better to sanity that
578 				 * sg_next is not NULL, but it will be a bug
579 				 * if it is null.
580 				 */
581 				if (is_scsiio_req) {
582 					sg_scmd = sg_next(sg_scmd);
583 					if (sg_scmd)
584 						sgel++;
585 					else
586 						goto eob_clone_chain;
587 				}
588 			}
589 			break;
590 		}
591 	}
592 
593 eob_clone_chain:
594 	for (i = 0; i < sge_chain_count; i++) {
595 		if (is_scsiio_req)
596 			_base_clone_to_sys_mem(dst_chain_addr[i],
597 				src_chain_addr[i], ioc->request_sz);
598 	}
599 }
600 
601 /**
602  *  mpt3sas_remove_dead_ioc_func - kthread context to remove dead ioc
603  * @arg: input argument, used to derive ioc
604  *
605  * Return:
606  * 0 if controller is removed from pci subsystem.
607  * -1 for other case.
608  */
mpt3sas_remove_dead_ioc_func(void * arg)609 static int mpt3sas_remove_dead_ioc_func(void *arg)
610 {
611 	struct MPT3SAS_ADAPTER *ioc = (struct MPT3SAS_ADAPTER *)arg;
612 	struct pci_dev *pdev;
613 
614 	if (!ioc)
615 		return -1;
616 
617 	pdev = ioc->pdev;
618 	if (!pdev)
619 		return -1;
620 	pci_stop_and_remove_bus_device_locked(pdev);
621 	return 0;
622 }
623 
624 /**
625  * _base_sync_drv_fw_timestamp - Sync Drive-Fw TimeStamp.
626  * @ioc: Per Adapter Object
627  *
628  * Return: nothing.
629  */
_base_sync_drv_fw_timestamp(struct MPT3SAS_ADAPTER * ioc)630 static void _base_sync_drv_fw_timestamp(struct MPT3SAS_ADAPTER *ioc)
631 {
632 	Mpi26IoUnitControlRequest_t *mpi_request;
633 	Mpi26IoUnitControlReply_t *mpi_reply;
634 	u16 smid;
635 	ktime_t current_time;
636 	u64 TimeStamp = 0;
637 	u8 issue_reset = 0;
638 
639 	mutex_lock(&ioc->scsih_cmds.mutex);
640 	if (ioc->scsih_cmds.status != MPT3_CMD_NOT_USED) {
641 		ioc_err(ioc, "scsih_cmd in use %s\n", __func__);
642 		goto out;
643 	}
644 	ioc->scsih_cmds.status = MPT3_CMD_PENDING;
645 	smid = mpt3sas_base_get_smid(ioc, ioc->scsih_cb_idx);
646 	if (!smid) {
647 		ioc_err(ioc, "Failed obtaining a smid %s\n", __func__);
648 		ioc->scsih_cmds.status = MPT3_CMD_NOT_USED;
649 		goto out;
650 	}
651 	mpi_request = mpt3sas_base_get_msg_frame(ioc, smid);
652 	ioc->scsih_cmds.smid = smid;
653 	memset(mpi_request, 0, sizeof(Mpi26IoUnitControlRequest_t));
654 	mpi_request->Function = MPI2_FUNCTION_IO_UNIT_CONTROL;
655 	mpi_request->Operation = MPI26_CTRL_OP_SET_IOC_PARAMETER;
656 	mpi_request->IOCParameter = MPI26_SET_IOC_PARAMETER_SYNC_TIMESTAMP;
657 	current_time = ktime_get_real();
658 	TimeStamp = ktime_to_ms(current_time);
659 	mpi_request->Reserved7 = cpu_to_le32(TimeStamp >> 32);
660 	mpi_request->IOCParameterValue = cpu_to_le32(TimeStamp & 0xFFFFFFFF);
661 	init_completion(&ioc->scsih_cmds.done);
662 	ioc->put_smid_default(ioc, smid);
663 	dinitprintk(ioc, ioc_info(ioc,
664 	    "Io Unit Control Sync TimeStamp (sending), @time %lld ms\n",
665 	    TimeStamp));
666 	wait_for_completion_timeout(&ioc->scsih_cmds.done,
667 		MPT3SAS_TIMESYNC_TIMEOUT_SECONDS*HZ);
668 	if (!(ioc->scsih_cmds.status & MPT3_CMD_COMPLETE)) {
669 		mpt3sas_check_cmd_timeout(ioc,
670 		    ioc->scsih_cmds.status, mpi_request,
671 		    sizeof(Mpi2SasIoUnitControlRequest_t)/4, issue_reset);
672 		goto issue_host_reset;
673 	}
674 	if (ioc->scsih_cmds.status & MPT3_CMD_REPLY_VALID) {
675 		mpi_reply = ioc->scsih_cmds.reply;
676 		dinitprintk(ioc, ioc_info(ioc,
677 		    "Io Unit Control sync timestamp (complete): ioc_status(0x%04x), loginfo(0x%08x)\n",
678 		    le16_to_cpu(mpi_reply->IOCStatus),
679 		    le32_to_cpu(mpi_reply->IOCLogInfo)));
680 	}
681 issue_host_reset:
682 	if (issue_reset)
683 		mpt3sas_base_hard_reset_handler(ioc, FORCE_BIG_HAMMER);
684 	ioc->scsih_cmds.status = MPT3_CMD_NOT_USED;
685 out:
686 	mutex_unlock(&ioc->scsih_cmds.mutex);
687 }
688 
689 /**
690  * _base_fault_reset_work - workq handling ioc fault conditions
691  * @work: input argument, used to derive ioc
692  *
693  * Context: sleep.
694  */
695 static void
_base_fault_reset_work(struct work_struct * work)696 _base_fault_reset_work(struct work_struct *work)
697 {
698 	struct MPT3SAS_ADAPTER *ioc =
699 	    container_of(work, struct MPT3SAS_ADAPTER, fault_reset_work.work);
700 	unsigned long	 flags;
701 	u32 doorbell;
702 	int rc;
703 	struct task_struct *p;
704 
705 
706 	spin_lock_irqsave(&ioc->ioc_reset_in_progress_lock, flags);
707 	if ((ioc->shost_recovery && (ioc->ioc_coredump_loop == 0)) ||
708 			ioc->pci_error_recovery)
709 		goto rearm_timer;
710 	spin_unlock_irqrestore(&ioc->ioc_reset_in_progress_lock, flags);
711 
712 	doorbell = mpt3sas_base_get_iocstate(ioc, 0);
713 	if ((doorbell & MPI2_IOC_STATE_MASK) == MPI2_IOC_STATE_MASK) {
714 		ioc_err(ioc, "SAS host is non-operational !!!!\n");
715 
716 		/* It may be possible that EEH recovery can resolve some of
717 		 * pci bus failure issues rather removing the dead ioc function
718 		 * by considering controller is in a non-operational state. So
719 		 * here priority is given to the EEH recovery. If it doesn't
720 		 * not resolve this issue, mpt3sas driver will consider this
721 		 * controller to non-operational state and remove the dead ioc
722 		 * function.
723 		 */
724 		if (ioc->non_operational_loop++ < 5) {
725 			spin_lock_irqsave(&ioc->ioc_reset_in_progress_lock,
726 							 flags);
727 			goto rearm_timer;
728 		}
729 
730 		/*
731 		 * Call _scsih_flush_pending_cmds callback so that we flush all
732 		 * pending commands back to OS. This call is required to avoid
733 		 * deadlock at block layer. Dead IOC will fail to do diag reset,
734 		 * and this call is safe since dead ioc will never return any
735 		 * command back from HW.
736 		 */
737 		mpt3sas_base_pause_mq_polling(ioc);
738 		ioc->schedule_dead_ioc_flush_running_cmds(ioc);
739 		/*
740 		 * Set remove_host flag early since kernel thread will
741 		 * take some time to execute.
742 		 */
743 		ioc->remove_host = 1;
744 		/*Remove the Dead Host */
745 		p = kthread_run(mpt3sas_remove_dead_ioc_func, ioc,
746 		    "%s_dead_ioc_%d", ioc->driver_name, ioc->id);
747 		if (IS_ERR(p))
748 			ioc_err(ioc, "%s: Running mpt3sas_dead_ioc thread failed !!!!\n",
749 				__func__);
750 		else
751 			ioc_err(ioc, "%s: Running mpt3sas_dead_ioc thread success !!!!\n",
752 				__func__);
753 		return; /* don't rearm timer */
754 	}
755 
756 	if ((doorbell & MPI2_IOC_STATE_MASK) == MPI2_IOC_STATE_COREDUMP) {
757 		u8 timeout = (ioc->manu_pg11.CoreDumpTOSec) ?
758 		    ioc->manu_pg11.CoreDumpTOSec :
759 		    MPT3SAS_DEFAULT_COREDUMP_TIMEOUT_SECONDS;
760 
761 		timeout /= (FAULT_POLLING_INTERVAL/1000);
762 
763 		if (ioc->ioc_coredump_loop == 0) {
764 			mpt3sas_print_coredump_info(ioc,
765 			    doorbell & MPI2_DOORBELL_DATA_MASK);
766 			/* do not accept any IOs and disable the interrupts */
767 			spin_lock_irqsave(
768 			    &ioc->ioc_reset_in_progress_lock, flags);
769 			ioc->shost_recovery = 1;
770 			spin_unlock_irqrestore(
771 			    &ioc->ioc_reset_in_progress_lock, flags);
772 			mpt3sas_base_mask_interrupts(ioc);
773 			mpt3sas_base_pause_mq_polling(ioc);
774 			_base_clear_outstanding_commands(ioc);
775 		}
776 
777 		ioc_info(ioc, "%s: CoreDump loop %d.",
778 		    __func__, ioc->ioc_coredump_loop);
779 
780 		/* Wait until CoreDump completes or times out */
781 		if (ioc->ioc_coredump_loop++ < timeout) {
782 			spin_lock_irqsave(
783 			    &ioc->ioc_reset_in_progress_lock, flags);
784 			goto rearm_timer;
785 		}
786 	}
787 
788 	if (ioc->ioc_coredump_loop) {
789 		if ((doorbell & MPI2_IOC_STATE_MASK) != MPI2_IOC_STATE_COREDUMP)
790 			ioc_err(ioc, "%s: CoreDump completed. LoopCount: %d",
791 			    __func__, ioc->ioc_coredump_loop);
792 		else
793 			ioc_err(ioc, "%s: CoreDump Timed out. LoopCount: %d",
794 			    __func__, ioc->ioc_coredump_loop);
795 		ioc->ioc_coredump_loop = MPT3SAS_COREDUMP_LOOP_DONE;
796 	}
797 	ioc->non_operational_loop = 0;
798 	if ((doorbell & MPI2_IOC_STATE_MASK) != MPI2_IOC_STATE_OPERATIONAL) {
799 		rc = mpt3sas_base_hard_reset_handler(ioc, FORCE_BIG_HAMMER);
800 		ioc_warn(ioc, "%s: hard reset: %s\n",
801 			 __func__, rc == 0 ? "success" : "failed");
802 		doorbell = mpt3sas_base_get_iocstate(ioc, 0);
803 		if ((doorbell & MPI2_IOC_STATE_MASK) == MPI2_IOC_STATE_FAULT) {
804 			mpt3sas_print_fault_code(ioc, doorbell &
805 			    MPI2_DOORBELL_DATA_MASK);
806 		} else if ((doorbell & MPI2_IOC_STATE_MASK) ==
807 		    MPI2_IOC_STATE_COREDUMP)
808 			mpt3sas_print_coredump_info(ioc, doorbell &
809 			    MPI2_DOORBELL_DATA_MASK);
810 		if (rc && (doorbell & MPI2_IOC_STATE_MASK) !=
811 		    MPI2_IOC_STATE_OPERATIONAL)
812 			return; /* don't rearm timer */
813 	}
814 	ioc->ioc_coredump_loop = 0;
815 	if (ioc->time_sync_interval &&
816 	    ++ioc->timestamp_update_count >= ioc->time_sync_interval) {
817 		ioc->timestamp_update_count = 0;
818 		_base_sync_drv_fw_timestamp(ioc);
819 	}
820 	spin_lock_irqsave(&ioc->ioc_reset_in_progress_lock, flags);
821  rearm_timer:
822 	if (ioc->fault_reset_work_q)
823 		queue_delayed_work(ioc->fault_reset_work_q,
824 		    &ioc->fault_reset_work,
825 		    msecs_to_jiffies(FAULT_POLLING_INTERVAL));
826 	spin_unlock_irqrestore(&ioc->ioc_reset_in_progress_lock, flags);
827 }
828 
829 /**
830  * mpt3sas_base_start_watchdog - start the fault_reset_work_q
831  * @ioc: per adapter object
832  *
833  * Context: sleep.
834  */
835 void
mpt3sas_base_start_watchdog(struct MPT3SAS_ADAPTER * ioc)836 mpt3sas_base_start_watchdog(struct MPT3SAS_ADAPTER *ioc)
837 {
838 	unsigned long	 flags;
839 
840 	if (ioc->fault_reset_work_q)
841 		return;
842 
843 	ioc->timestamp_update_count = 0;
844 	/* initialize fault polling */
845 
846 	INIT_DELAYED_WORK(&ioc->fault_reset_work, _base_fault_reset_work);
847 	snprintf(ioc->fault_reset_work_q_name,
848 	    sizeof(ioc->fault_reset_work_q_name), "poll_%s%d_status",
849 	    ioc->driver_name, ioc->id);
850 	ioc->fault_reset_work_q =
851 		create_singlethread_workqueue(ioc->fault_reset_work_q_name);
852 	if (!ioc->fault_reset_work_q) {
853 		ioc_err(ioc, "%s: failed (line=%d)\n", __func__, __LINE__);
854 		return;
855 	}
856 	spin_lock_irqsave(&ioc->ioc_reset_in_progress_lock, flags);
857 	if (ioc->fault_reset_work_q)
858 		queue_delayed_work(ioc->fault_reset_work_q,
859 		    &ioc->fault_reset_work,
860 		    msecs_to_jiffies(FAULT_POLLING_INTERVAL));
861 	spin_unlock_irqrestore(&ioc->ioc_reset_in_progress_lock, flags);
862 }
863 
864 /**
865  * mpt3sas_base_stop_watchdog - stop the fault_reset_work_q
866  * @ioc: per adapter object
867  *
868  * Context: sleep.
869  */
870 void
mpt3sas_base_stop_watchdog(struct MPT3SAS_ADAPTER * ioc)871 mpt3sas_base_stop_watchdog(struct MPT3SAS_ADAPTER *ioc)
872 {
873 	unsigned long flags;
874 	struct workqueue_struct *wq;
875 
876 	spin_lock_irqsave(&ioc->ioc_reset_in_progress_lock, flags);
877 	wq = ioc->fault_reset_work_q;
878 	ioc->fault_reset_work_q = NULL;
879 	spin_unlock_irqrestore(&ioc->ioc_reset_in_progress_lock, flags);
880 	if (wq) {
881 		if (!cancel_delayed_work_sync(&ioc->fault_reset_work))
882 			flush_workqueue(wq);
883 		destroy_workqueue(wq);
884 	}
885 }
886 
887 /**
888  * mpt3sas_base_fault_info - verbose translation of firmware FAULT code
889  * @ioc: per adapter object
890  * @fault_code: fault code
891  */
892 void
mpt3sas_base_fault_info(struct MPT3SAS_ADAPTER * ioc,u16 fault_code)893 mpt3sas_base_fault_info(struct MPT3SAS_ADAPTER *ioc , u16 fault_code)
894 {
895 	ioc_err(ioc, "fault_state(0x%04x)!\n", fault_code);
896 }
897 
898 /**
899  * mpt3sas_base_coredump_info - verbose translation of firmware CoreDump state
900  * @ioc: per adapter object
901  * @fault_code: fault code
902  *
903  * Return: nothing.
904  */
905 void
mpt3sas_base_coredump_info(struct MPT3SAS_ADAPTER * ioc,u16 fault_code)906 mpt3sas_base_coredump_info(struct MPT3SAS_ADAPTER *ioc, u16 fault_code)
907 {
908 	ioc_err(ioc, "coredump_state(0x%04x)!\n", fault_code);
909 }
910 
911 /**
912  * mpt3sas_base_wait_for_coredump_completion - Wait until coredump
913  * completes or times out
914  * @ioc: per adapter object
915  * @caller: caller function name
916  *
917  * Return: 0 for success, non-zero for failure.
918  */
919 int
mpt3sas_base_wait_for_coredump_completion(struct MPT3SAS_ADAPTER * ioc,const char * caller)920 mpt3sas_base_wait_for_coredump_completion(struct MPT3SAS_ADAPTER *ioc,
921 		const char *caller)
922 {
923 	u8 timeout = (ioc->manu_pg11.CoreDumpTOSec) ?
924 			ioc->manu_pg11.CoreDumpTOSec :
925 			MPT3SAS_DEFAULT_COREDUMP_TIMEOUT_SECONDS;
926 
927 	int ioc_state = _base_wait_on_iocstate(ioc, MPI2_IOC_STATE_FAULT,
928 					timeout);
929 
930 	if (ioc_state)
931 		ioc_err(ioc,
932 		    "%s: CoreDump timed out. (ioc_state=0x%x)\n",
933 		    caller, ioc_state);
934 	else
935 		ioc_info(ioc,
936 		    "%s: CoreDump completed. (ioc_state=0x%x)\n",
937 		    caller, ioc_state);
938 
939 	return ioc_state;
940 }
941 
942 /**
943  * mpt3sas_halt_firmware - halt's mpt controller firmware
944  * @ioc: per adapter object
945  *
946  * For debugging timeout related issues.  Writing 0xCOFFEE00
947  * to the doorbell register will halt controller firmware. With
948  * the purpose to stop both driver and firmware, the enduser can
949  * obtain a ring buffer from controller UART.
950  */
951 void
mpt3sas_halt_firmware(struct MPT3SAS_ADAPTER * ioc)952 mpt3sas_halt_firmware(struct MPT3SAS_ADAPTER *ioc)
953 {
954 	u32 doorbell;
955 
956 	if (!ioc->fwfault_debug)
957 		return;
958 
959 	dump_stack();
960 
961 	doorbell = ioc->base_readl_ext_retry(&ioc->chip->Doorbell);
962 	if ((doorbell & MPI2_IOC_STATE_MASK) == MPI2_IOC_STATE_FAULT) {
963 		mpt3sas_print_fault_code(ioc, doorbell &
964 		    MPI2_DOORBELL_DATA_MASK);
965 	} else if ((doorbell & MPI2_IOC_STATE_MASK) ==
966 	    MPI2_IOC_STATE_COREDUMP) {
967 		mpt3sas_print_coredump_info(ioc, doorbell &
968 		    MPI2_DOORBELL_DATA_MASK);
969 	} else {
970 		writel(0xC0FFEE00, &ioc->chip->Doorbell);
971 		ioc_err(ioc, "Firmware is halted due to command timeout\n");
972 	}
973 
974 	if (ioc->fwfault_debug == 2)
975 		for (;;)
976 			;
977 	else
978 		panic("panic in %s\n", __func__);
979 }
980 
981 /**
982  * _base_sas_ioc_info - verbose translation of the ioc status
983  * @ioc: per adapter object
984  * @mpi_reply: reply mf payload returned from firmware
985  * @request_hdr: request mf
986  */
987 static void
_base_sas_ioc_info(struct MPT3SAS_ADAPTER * ioc,MPI2DefaultReply_t * mpi_reply,MPI2RequestHeader_t * request_hdr)988 _base_sas_ioc_info(struct MPT3SAS_ADAPTER *ioc, MPI2DefaultReply_t *mpi_reply,
989 	MPI2RequestHeader_t *request_hdr)
990 {
991 	u16 ioc_status = le16_to_cpu(mpi_reply->IOCStatus) &
992 	    MPI2_IOCSTATUS_MASK;
993 	char *desc = NULL;
994 	u16 frame_sz;
995 	char *func_str = NULL;
996 
997 	/* SCSI_IO, RAID_PASS are handled from _scsih_scsi_ioc_info */
998 	if (request_hdr->Function == MPI2_FUNCTION_SCSI_IO_REQUEST ||
999 	    request_hdr->Function == MPI2_FUNCTION_RAID_SCSI_IO_PASSTHROUGH ||
1000 	    request_hdr->Function == MPI2_FUNCTION_EVENT_NOTIFICATION)
1001 		return;
1002 
1003 	if (ioc_status == MPI2_IOCSTATUS_CONFIG_INVALID_PAGE)
1004 		return;
1005 	/*
1006 	 * Older Firmware version doesn't support driver trigger pages.
1007 	 * So, skip displaying 'config invalid type' type
1008 	 * of error message.
1009 	 */
1010 	if (request_hdr->Function == MPI2_FUNCTION_CONFIG) {
1011 		Mpi2ConfigRequest_t *rqst = (Mpi2ConfigRequest_t *)request_hdr;
1012 
1013 		if ((rqst->ExtPageType ==
1014 		    MPI2_CONFIG_EXTPAGETYPE_DRIVER_PERSISTENT_TRIGGER) &&
1015 		    !(ioc->logging_level & MPT_DEBUG_CONFIG)) {
1016 			return;
1017 		}
1018 	}
1019 
1020 	switch (ioc_status) {
1021 
1022 /****************************************************************************
1023 *  Common IOCStatus values for all replies
1024 ****************************************************************************/
1025 
1026 	case MPI2_IOCSTATUS_INVALID_FUNCTION:
1027 		desc = "invalid function";
1028 		break;
1029 	case MPI2_IOCSTATUS_BUSY:
1030 		desc = "busy";
1031 		break;
1032 	case MPI2_IOCSTATUS_INVALID_SGL:
1033 		desc = "invalid sgl";
1034 		break;
1035 	case MPI2_IOCSTATUS_INTERNAL_ERROR:
1036 		desc = "internal error";
1037 		break;
1038 	case MPI2_IOCSTATUS_INVALID_VPID:
1039 		desc = "invalid vpid";
1040 		break;
1041 	case MPI2_IOCSTATUS_INSUFFICIENT_RESOURCES:
1042 		desc = "insufficient resources";
1043 		break;
1044 	case MPI2_IOCSTATUS_INSUFFICIENT_POWER:
1045 		desc = "insufficient power";
1046 		break;
1047 	case MPI2_IOCSTATUS_INVALID_FIELD:
1048 		desc = "invalid field";
1049 		break;
1050 	case MPI2_IOCSTATUS_INVALID_STATE:
1051 		desc = "invalid state";
1052 		break;
1053 	case MPI2_IOCSTATUS_OP_STATE_NOT_SUPPORTED:
1054 		desc = "op state not supported";
1055 		break;
1056 
1057 /****************************************************************************
1058 *  Config IOCStatus values
1059 ****************************************************************************/
1060 
1061 	case MPI2_IOCSTATUS_CONFIG_INVALID_ACTION:
1062 		desc = "config invalid action";
1063 		break;
1064 	case MPI2_IOCSTATUS_CONFIG_INVALID_TYPE:
1065 		desc = "config invalid type";
1066 		break;
1067 	case MPI2_IOCSTATUS_CONFIG_INVALID_PAGE:
1068 		desc = "config invalid page";
1069 		break;
1070 	case MPI2_IOCSTATUS_CONFIG_INVALID_DATA:
1071 		desc = "config invalid data";
1072 		break;
1073 	case MPI2_IOCSTATUS_CONFIG_NO_DEFAULTS:
1074 		desc = "config no defaults";
1075 		break;
1076 	case MPI2_IOCSTATUS_CONFIG_CANT_COMMIT:
1077 		desc = "config cant commit";
1078 		break;
1079 
1080 /****************************************************************************
1081 *  SCSI IO Reply
1082 ****************************************************************************/
1083 
1084 	case MPI2_IOCSTATUS_SCSI_RECOVERED_ERROR:
1085 	case MPI2_IOCSTATUS_SCSI_INVALID_DEVHANDLE:
1086 	case MPI2_IOCSTATUS_SCSI_DEVICE_NOT_THERE:
1087 	case MPI2_IOCSTATUS_SCSI_DATA_OVERRUN:
1088 	case MPI2_IOCSTATUS_SCSI_DATA_UNDERRUN:
1089 	case MPI2_IOCSTATUS_SCSI_IO_DATA_ERROR:
1090 	case MPI2_IOCSTATUS_SCSI_PROTOCOL_ERROR:
1091 	case MPI2_IOCSTATUS_SCSI_TASK_TERMINATED:
1092 	case MPI2_IOCSTATUS_SCSI_RESIDUAL_MISMATCH:
1093 	case MPI2_IOCSTATUS_SCSI_TASK_MGMT_FAILED:
1094 	case MPI2_IOCSTATUS_SCSI_IOC_TERMINATED:
1095 	case MPI2_IOCSTATUS_SCSI_EXT_TERMINATED:
1096 		break;
1097 
1098 /****************************************************************************
1099 *  For use by SCSI Initiator and SCSI Target end-to-end data protection
1100 ****************************************************************************/
1101 
1102 	case MPI2_IOCSTATUS_EEDP_GUARD_ERROR:
1103 		desc = "eedp guard error";
1104 		break;
1105 	case MPI2_IOCSTATUS_EEDP_REF_TAG_ERROR:
1106 		desc = "eedp ref tag error";
1107 		break;
1108 	case MPI2_IOCSTATUS_EEDP_APP_TAG_ERROR:
1109 		desc = "eedp app tag error";
1110 		break;
1111 
1112 /****************************************************************************
1113 *  SCSI Target values
1114 ****************************************************************************/
1115 
1116 	case MPI2_IOCSTATUS_TARGET_INVALID_IO_INDEX:
1117 		desc = "target invalid io index";
1118 		break;
1119 	case MPI2_IOCSTATUS_TARGET_ABORTED:
1120 		desc = "target aborted";
1121 		break;
1122 	case MPI2_IOCSTATUS_TARGET_NO_CONN_RETRYABLE:
1123 		desc = "target no conn retryable";
1124 		break;
1125 	case MPI2_IOCSTATUS_TARGET_NO_CONNECTION:
1126 		desc = "target no connection";
1127 		break;
1128 	case MPI2_IOCSTATUS_TARGET_XFER_COUNT_MISMATCH:
1129 		desc = "target xfer count mismatch";
1130 		break;
1131 	case MPI2_IOCSTATUS_TARGET_DATA_OFFSET_ERROR:
1132 		desc = "target data offset error";
1133 		break;
1134 	case MPI2_IOCSTATUS_TARGET_TOO_MUCH_WRITE_DATA:
1135 		desc = "target too much write data";
1136 		break;
1137 	case MPI2_IOCSTATUS_TARGET_IU_TOO_SHORT:
1138 		desc = "target iu too short";
1139 		break;
1140 	case MPI2_IOCSTATUS_TARGET_ACK_NAK_TIMEOUT:
1141 		desc = "target ack nak timeout";
1142 		break;
1143 	case MPI2_IOCSTATUS_TARGET_NAK_RECEIVED:
1144 		desc = "target nak received";
1145 		break;
1146 
1147 /****************************************************************************
1148 *  Serial Attached SCSI values
1149 ****************************************************************************/
1150 
1151 	case MPI2_IOCSTATUS_SAS_SMP_REQUEST_FAILED:
1152 		desc = "smp request failed";
1153 		break;
1154 	case MPI2_IOCSTATUS_SAS_SMP_DATA_OVERRUN:
1155 		desc = "smp data overrun";
1156 		break;
1157 
1158 /****************************************************************************
1159 *  Diagnostic Buffer Post / Diagnostic Release values
1160 ****************************************************************************/
1161 
1162 	case MPI2_IOCSTATUS_DIAGNOSTIC_RELEASED:
1163 		desc = "diagnostic released";
1164 		break;
1165 	default:
1166 		break;
1167 	}
1168 
1169 	if (!desc)
1170 		return;
1171 
1172 	switch (request_hdr->Function) {
1173 	case MPI2_FUNCTION_CONFIG:
1174 		frame_sz = sizeof(Mpi2ConfigRequest_t) + ioc->sge_size;
1175 		func_str = "config_page";
1176 		break;
1177 	case MPI2_FUNCTION_SCSI_TASK_MGMT:
1178 		frame_sz = sizeof(Mpi2SCSITaskManagementRequest_t);
1179 		func_str = "task_mgmt";
1180 		break;
1181 	case MPI2_FUNCTION_SAS_IO_UNIT_CONTROL:
1182 		frame_sz = sizeof(Mpi2SasIoUnitControlRequest_t);
1183 		func_str = "sas_iounit_ctl";
1184 		break;
1185 	case MPI2_FUNCTION_SCSI_ENCLOSURE_PROCESSOR:
1186 		frame_sz = sizeof(Mpi2SepRequest_t);
1187 		func_str = "enclosure";
1188 		break;
1189 	case MPI2_FUNCTION_IOC_INIT:
1190 		frame_sz = sizeof(Mpi2IOCInitRequest_t);
1191 		func_str = "ioc_init";
1192 		break;
1193 	case MPI2_FUNCTION_PORT_ENABLE:
1194 		frame_sz = sizeof(Mpi2PortEnableRequest_t);
1195 		func_str = "port_enable";
1196 		break;
1197 	case MPI2_FUNCTION_SMP_PASSTHROUGH:
1198 		frame_sz = sizeof(Mpi2SmpPassthroughRequest_t) + ioc->sge_size;
1199 		func_str = "smp_passthru";
1200 		break;
1201 	case MPI2_FUNCTION_NVME_ENCAPSULATED:
1202 		frame_sz = sizeof(Mpi26NVMeEncapsulatedRequest_t) +
1203 		    ioc->sge_size;
1204 		func_str = "nvme_encapsulated";
1205 		break;
1206 	default:
1207 		frame_sz = 32;
1208 		func_str = "unknown";
1209 		break;
1210 	}
1211 
1212 	ioc_warn(ioc, "ioc_status: %s(0x%04x), request(0x%p),(%s)\n",
1213 		 desc, ioc_status, request_hdr, func_str);
1214 
1215 	_debug_dump_mf(request_hdr, frame_sz/4);
1216 }
1217 
1218 /**
1219  * _base_display_event_data - verbose translation of firmware asyn events
1220  * @ioc: per adapter object
1221  * @mpi_reply: reply mf payload returned from firmware
1222  */
1223 static void
_base_display_event_data(struct MPT3SAS_ADAPTER * ioc,Mpi2EventNotificationReply_t * mpi_reply)1224 _base_display_event_data(struct MPT3SAS_ADAPTER *ioc,
1225 	Mpi2EventNotificationReply_t *mpi_reply)
1226 {
1227 	char *desc = NULL;
1228 	u16 event;
1229 
1230 	if (!(ioc->logging_level & MPT_DEBUG_EVENTS))
1231 		return;
1232 
1233 	event = le16_to_cpu(mpi_reply->Event);
1234 
1235 	switch (event) {
1236 	case MPI2_EVENT_LOG_DATA:
1237 		desc = "Log Data";
1238 		break;
1239 	case MPI2_EVENT_STATE_CHANGE:
1240 		desc = "Status Change";
1241 		break;
1242 	case MPI2_EVENT_HARD_RESET_RECEIVED:
1243 		desc = "Hard Reset Received";
1244 		break;
1245 	case MPI2_EVENT_EVENT_CHANGE:
1246 		desc = "Event Change";
1247 		break;
1248 	case MPI2_EVENT_SAS_DEVICE_STATUS_CHANGE:
1249 		desc = "Device Status Change";
1250 		break;
1251 	case MPI2_EVENT_IR_OPERATION_STATUS:
1252 		if (!ioc->hide_ir_msg)
1253 			desc = "IR Operation Status";
1254 		break;
1255 	case MPI2_EVENT_SAS_DISCOVERY:
1256 	{
1257 		Mpi2EventDataSasDiscovery_t *event_data =
1258 		    (Mpi2EventDataSasDiscovery_t *)mpi_reply->EventData;
1259 		ioc_info(ioc, "Discovery: (%s)",
1260 			 event_data->ReasonCode == MPI2_EVENT_SAS_DISC_RC_STARTED ?
1261 			 "start" : "stop");
1262 		if (event_data->DiscoveryStatus)
1263 			pr_cont(" discovery_status(0x%08x)",
1264 			    le32_to_cpu(event_data->DiscoveryStatus));
1265 		pr_cont("\n");
1266 		return;
1267 	}
1268 	case MPI2_EVENT_SAS_BROADCAST_PRIMITIVE:
1269 		desc = "SAS Broadcast Primitive";
1270 		break;
1271 	case MPI2_EVENT_SAS_INIT_DEVICE_STATUS_CHANGE:
1272 		desc = "SAS Init Device Status Change";
1273 		break;
1274 	case MPI2_EVENT_SAS_INIT_TABLE_OVERFLOW:
1275 		desc = "SAS Init Table Overflow";
1276 		break;
1277 	case MPI2_EVENT_SAS_TOPOLOGY_CHANGE_LIST:
1278 		desc = "SAS Topology Change List";
1279 		break;
1280 	case MPI2_EVENT_SAS_ENCL_DEVICE_STATUS_CHANGE:
1281 		desc = "SAS Enclosure Device Status Change";
1282 		break;
1283 	case MPI2_EVENT_IR_VOLUME:
1284 		if (!ioc->hide_ir_msg)
1285 			desc = "IR Volume";
1286 		break;
1287 	case MPI2_EVENT_IR_PHYSICAL_DISK:
1288 		if (!ioc->hide_ir_msg)
1289 			desc = "IR Physical Disk";
1290 		break;
1291 	case MPI2_EVENT_IR_CONFIGURATION_CHANGE_LIST:
1292 		if (!ioc->hide_ir_msg)
1293 			desc = "IR Configuration Change List";
1294 		break;
1295 	case MPI2_EVENT_LOG_ENTRY_ADDED:
1296 		if (!ioc->hide_ir_msg)
1297 			desc = "Log Entry Added";
1298 		break;
1299 	case MPI2_EVENT_TEMP_THRESHOLD:
1300 		desc = "Temperature Threshold";
1301 		break;
1302 	case MPI2_EVENT_ACTIVE_CABLE_EXCEPTION:
1303 		desc = "Cable Event";
1304 		break;
1305 	case MPI2_EVENT_SAS_DEVICE_DISCOVERY_ERROR:
1306 		desc = "SAS Device Discovery Error";
1307 		break;
1308 	case MPI2_EVENT_PCIE_DEVICE_STATUS_CHANGE:
1309 		desc = "PCIE Device Status Change";
1310 		break;
1311 	case MPI2_EVENT_PCIE_ENUMERATION:
1312 	{
1313 		Mpi26EventDataPCIeEnumeration_t *event_data =
1314 			(Mpi26EventDataPCIeEnumeration_t *)mpi_reply->EventData;
1315 		ioc_info(ioc, "PCIE Enumeration: (%s)",
1316 			 event_data->ReasonCode == MPI26_EVENT_PCIE_ENUM_RC_STARTED ?
1317 			 "start" : "stop");
1318 		if (event_data->EnumerationStatus)
1319 			pr_cont("enumeration_status(0x%08x)",
1320 				le32_to_cpu(event_data->EnumerationStatus));
1321 		pr_cont("\n");
1322 		return;
1323 	}
1324 	case MPI2_EVENT_PCIE_TOPOLOGY_CHANGE_LIST:
1325 		desc = "PCIE Topology Change List";
1326 		break;
1327 	}
1328 
1329 	if (!desc)
1330 		return;
1331 
1332 	ioc_info(ioc, "%s\n", desc);
1333 }
1334 
1335 /**
1336  * _base_sas_log_info - verbose translation of firmware log info
1337  * @ioc: per adapter object
1338  * @log_info: log info
1339  */
1340 static void
_base_sas_log_info(struct MPT3SAS_ADAPTER * ioc,u32 log_info)1341 _base_sas_log_info(struct MPT3SAS_ADAPTER *ioc , u32 log_info)
1342 {
1343 	union loginfo_type {
1344 		u32	loginfo;
1345 		struct {
1346 			u32	subcode:16;
1347 			u32	code:8;
1348 			u32	originator:4;
1349 			u32	bus_type:4;
1350 		} dw;
1351 	};
1352 	union loginfo_type sas_loginfo;
1353 	char *originator_str = NULL;
1354 
1355 	sas_loginfo.loginfo = log_info;
1356 	if (sas_loginfo.dw.bus_type != 3 /*SAS*/)
1357 		return;
1358 
1359 	/* each nexus loss loginfo */
1360 	if (log_info == 0x31170000)
1361 		return;
1362 
1363 	/* eat the loginfos associated with task aborts */
1364 	if (ioc->ignore_loginfos && (log_info == 0x30050000 || log_info ==
1365 	    0x31140000 || log_info == 0x31130000))
1366 		return;
1367 
1368 	switch (sas_loginfo.dw.originator) {
1369 	case 0:
1370 		originator_str = "IOP";
1371 		break;
1372 	case 1:
1373 		originator_str = "PL";
1374 		break;
1375 	case 2:
1376 		if (!ioc->hide_ir_msg)
1377 			originator_str = "IR";
1378 		else
1379 			originator_str = "WarpDrive";
1380 		break;
1381 	}
1382 
1383 	ioc_warn(ioc, "log_info(0x%08x): originator(%s), code(0x%02x), sub_code(0x%04x)\n",
1384 		 log_info,
1385 		 originator_str, sas_loginfo.dw.code, sas_loginfo.dw.subcode);
1386 }
1387 
1388 /**
1389  * _base_display_reply_info - handle reply descriptors depending on IOC Status
1390  * @ioc: per adapter object
1391  * @smid: system request message index
1392  * @msix_index: MSIX table index supplied by the OS
1393  * @reply: reply message frame (lower 32bit addr)
1394  */
1395 static void
_base_display_reply_info(struct MPT3SAS_ADAPTER * ioc,u16 smid,u8 msix_index,u32 reply)1396 _base_display_reply_info(struct MPT3SAS_ADAPTER *ioc, u16 smid, u8 msix_index,
1397 	u32 reply)
1398 {
1399 	MPI2DefaultReply_t *mpi_reply;
1400 	u16 ioc_status;
1401 	u32 loginfo = 0;
1402 
1403 	mpi_reply = mpt3sas_base_get_reply_virt_addr(ioc, reply);
1404 	if (unlikely(!mpi_reply)) {
1405 		ioc_err(ioc, "mpi_reply not valid at %s:%d/%s()!\n",
1406 			__FILE__, __LINE__, __func__);
1407 		return;
1408 	}
1409 	ioc_status = le16_to_cpu(mpi_reply->IOCStatus);
1410 
1411 	if ((ioc_status & MPI2_IOCSTATUS_MASK) &&
1412 	    (ioc->logging_level & MPT_DEBUG_REPLY)) {
1413 		_base_sas_ioc_info(ioc , mpi_reply,
1414 		   mpt3sas_base_get_msg_frame(ioc, smid));
1415 	}
1416 
1417 	if (ioc_status & MPI2_IOCSTATUS_FLAG_LOG_INFO_AVAILABLE) {
1418 		loginfo = le32_to_cpu(mpi_reply->IOCLogInfo);
1419 		_base_sas_log_info(ioc, loginfo);
1420 	}
1421 
1422 	if (ioc_status || loginfo) {
1423 		ioc_status &= MPI2_IOCSTATUS_MASK;
1424 		mpt3sas_trigger_mpi(ioc, ioc_status, loginfo);
1425 	}
1426 }
1427 
1428 /**
1429  * mpt3sas_base_done - base internal command completion routine
1430  * @ioc: per adapter object
1431  * @smid: system request message index
1432  * @msix_index: MSIX table index supplied by the OS
1433  * @reply: reply message frame(lower 32bit addr)
1434  *
1435  * Return:
1436  * 1 meaning mf should be freed from _base_interrupt
1437  * 0 means the mf is freed from this function.
1438  */
1439 u8
mpt3sas_base_done(struct MPT3SAS_ADAPTER * ioc,u16 smid,u8 msix_index,u32 reply)1440 mpt3sas_base_done(struct MPT3SAS_ADAPTER *ioc, u16 smid, u8 msix_index,
1441 	u32 reply)
1442 {
1443 	MPI2DefaultReply_t *mpi_reply;
1444 
1445 	mpi_reply = mpt3sas_base_get_reply_virt_addr(ioc, reply);
1446 	if (mpi_reply && mpi_reply->Function == MPI2_FUNCTION_EVENT_ACK)
1447 		return mpt3sas_check_for_pending_internal_cmds(ioc, smid);
1448 
1449 	if (ioc->base_cmds.status == MPT3_CMD_NOT_USED)
1450 		return 1;
1451 
1452 	ioc->base_cmds.status |= MPT3_CMD_COMPLETE;
1453 	if (mpi_reply) {
1454 		ioc->base_cmds.status |= MPT3_CMD_REPLY_VALID;
1455 		memcpy(ioc->base_cmds.reply, mpi_reply, mpi_reply->MsgLength*4);
1456 	}
1457 	ioc->base_cmds.status &= ~MPT3_CMD_PENDING;
1458 
1459 	complete(&ioc->base_cmds.done);
1460 	return 1;
1461 }
1462 
1463 /**
1464  * _base_async_event - main callback handler for firmware asyn events
1465  * @ioc: per adapter object
1466  * @msix_index: MSIX table index supplied by the OS
1467  * @reply: reply message frame(lower 32bit addr)
1468  *
1469  * Return:
1470  * 1 meaning mf should be freed from _base_interrupt
1471  * 0 means the mf is freed from this function.
1472  */
1473 static u8
_base_async_event(struct MPT3SAS_ADAPTER * ioc,u8 msix_index,u32 reply)1474 _base_async_event(struct MPT3SAS_ADAPTER *ioc, u8 msix_index, u32 reply)
1475 {
1476 	Mpi2EventNotificationReply_t *mpi_reply;
1477 	Mpi2EventAckRequest_t *ack_request;
1478 	u16 smid;
1479 	struct _event_ack_list *delayed_event_ack;
1480 
1481 	mpi_reply = mpt3sas_base_get_reply_virt_addr(ioc, reply);
1482 	if (!mpi_reply)
1483 		return 1;
1484 	if (mpi_reply->Function != MPI2_FUNCTION_EVENT_NOTIFICATION)
1485 		return 1;
1486 
1487 	_base_display_event_data(ioc, mpi_reply);
1488 
1489 	if (!(mpi_reply->AckRequired & MPI2_EVENT_NOTIFICATION_ACK_REQUIRED))
1490 		goto out;
1491 	smid = mpt3sas_base_get_smid(ioc, ioc->base_cb_idx);
1492 	if (!smid) {
1493 		delayed_event_ack = kzalloc(sizeof(*delayed_event_ack),
1494 					GFP_ATOMIC);
1495 		if (!delayed_event_ack)
1496 			goto out;
1497 		INIT_LIST_HEAD(&delayed_event_ack->list);
1498 		delayed_event_ack->Event = mpi_reply->Event;
1499 		delayed_event_ack->EventContext = mpi_reply->EventContext;
1500 		list_add_tail(&delayed_event_ack->list,
1501 				&ioc->delayed_event_ack_list);
1502 		dewtprintk(ioc,
1503 			   ioc_info(ioc, "DELAYED: EVENT ACK: event (0x%04x)\n",
1504 				    le16_to_cpu(mpi_reply->Event)));
1505 		goto out;
1506 	}
1507 
1508 	ack_request = mpt3sas_base_get_msg_frame(ioc, smid);
1509 	memset(ack_request, 0, sizeof(Mpi2EventAckRequest_t));
1510 	ack_request->Function = MPI2_FUNCTION_EVENT_ACK;
1511 	ack_request->Event = mpi_reply->Event;
1512 	ack_request->EventContext = mpi_reply->EventContext;
1513 	ack_request->VF_ID = 0;  /* TODO */
1514 	ack_request->VP_ID = 0;
1515 	ioc->put_smid_default(ioc, smid);
1516 
1517  out:
1518 
1519 	/* scsih callback handler */
1520 	mpt3sas_scsih_event_callback(ioc, msix_index, reply);
1521 
1522 	/* ctl callback handler */
1523 	mpt3sas_ctl_event_callback(ioc, msix_index, reply);
1524 
1525 	return 1;
1526 }
1527 
1528 static struct scsiio_tracker *
_get_st_from_smid(struct MPT3SAS_ADAPTER * ioc,u16 smid)1529 _get_st_from_smid(struct MPT3SAS_ADAPTER *ioc, u16 smid)
1530 {
1531 	struct scsi_cmnd *cmd;
1532 
1533 	if (WARN_ON(!smid) ||
1534 	    WARN_ON(smid >= ioc->hi_priority_smid))
1535 		return NULL;
1536 
1537 	cmd = mpt3sas_scsih_scsi_lookup_get(ioc, smid);
1538 	if (cmd)
1539 		return scsi_cmd_priv(cmd);
1540 
1541 	return NULL;
1542 }
1543 
1544 /**
1545  * _base_get_cb_idx - obtain the callback index
1546  * @ioc: per adapter object
1547  * @smid: system request message index
1548  *
1549  * Return: callback index.
1550  */
1551 static u8
_base_get_cb_idx(struct MPT3SAS_ADAPTER * ioc,u16 smid)1552 _base_get_cb_idx(struct MPT3SAS_ADAPTER *ioc, u16 smid)
1553 {
1554 	int i;
1555 	u16 ctl_smid = ioc->scsiio_depth - INTERNAL_SCSIIO_CMDS_COUNT + 1;
1556 	u8 cb_idx = 0xFF;
1557 
1558 	if (smid < ioc->hi_priority_smid) {
1559 		struct scsiio_tracker *st;
1560 
1561 		if (smid < ctl_smid) {
1562 			st = _get_st_from_smid(ioc, smid);
1563 			if (st)
1564 				cb_idx = st->cb_idx;
1565 		} else if (smid == ctl_smid)
1566 			cb_idx = ioc->ctl_cb_idx;
1567 	} else if (smid < ioc->internal_smid) {
1568 		i = smid - ioc->hi_priority_smid;
1569 		cb_idx = ioc->hpr_lookup[i].cb_idx;
1570 	} else if (smid <= ioc->hba_queue_depth) {
1571 		i = smid - ioc->internal_smid;
1572 		cb_idx = ioc->internal_lookup[i].cb_idx;
1573 	}
1574 	return cb_idx;
1575 }
1576 
1577 /**
1578  * mpt3sas_base_pause_mq_polling - pause polling on the mq poll queues
1579  *				when driver is flushing out the IOs.
1580  * @ioc: per adapter object
1581  *
1582  * Pause polling on the mq poll (io uring) queues when driver is flushing
1583  * out the IOs. Otherwise we may see the race condition of completing the same
1584  * IO from two paths.
1585  *
1586  * Returns nothing.
1587  */
1588 void
mpt3sas_base_pause_mq_polling(struct MPT3SAS_ADAPTER * ioc)1589 mpt3sas_base_pause_mq_polling(struct MPT3SAS_ADAPTER *ioc)
1590 {
1591 	int iopoll_q_count =
1592 	    ioc->reply_queue_count - ioc->iopoll_q_start_index;
1593 	int qid;
1594 
1595 	for (qid = 0; qid < iopoll_q_count; qid++)
1596 		atomic_set(&ioc->io_uring_poll_queues[qid].pause, 1);
1597 
1598 	/*
1599 	 * wait for current poll to complete.
1600 	 */
1601 	for (qid = 0; qid < iopoll_q_count; qid++) {
1602 		while (atomic_read(&ioc->io_uring_poll_queues[qid].busy)) {
1603 			cpu_relax();
1604 			udelay(500);
1605 		}
1606 	}
1607 }
1608 
1609 /**
1610  * mpt3sas_base_resume_mq_polling - Resume polling on mq poll queues.
1611  * @ioc: per adapter object
1612  *
1613  * Returns nothing.
1614  */
1615 void
mpt3sas_base_resume_mq_polling(struct MPT3SAS_ADAPTER * ioc)1616 mpt3sas_base_resume_mq_polling(struct MPT3SAS_ADAPTER *ioc)
1617 {
1618 	int iopoll_q_count =
1619 	    ioc->reply_queue_count - ioc->iopoll_q_start_index;
1620 	int qid;
1621 
1622 	for (qid = 0; qid < iopoll_q_count; qid++)
1623 		atomic_set(&ioc->io_uring_poll_queues[qid].pause, 0);
1624 }
1625 
1626 /**
1627  * mpt3sas_base_mask_interrupts - disable interrupts
1628  * @ioc: per adapter object
1629  *
1630  * Disabling ResetIRQ, Reply and Doorbell Interrupts
1631  */
1632 void
mpt3sas_base_mask_interrupts(struct MPT3SAS_ADAPTER * ioc)1633 mpt3sas_base_mask_interrupts(struct MPT3SAS_ADAPTER *ioc)
1634 {
1635 	u32 him_register;
1636 
1637 	ioc->mask_interrupts = 1;
1638 	him_register = ioc->base_readl(&ioc->chip->HostInterruptMask);
1639 	him_register |= MPI2_HIM_DIM + MPI2_HIM_RIM + MPI2_HIM_RESET_IRQ_MASK;
1640 	writel(him_register, &ioc->chip->HostInterruptMask);
1641 	ioc->base_readl(&ioc->chip->HostInterruptMask);
1642 }
1643 
1644 /**
1645  * mpt3sas_base_unmask_interrupts - enable interrupts
1646  * @ioc: per adapter object
1647  *
1648  * Enabling only Reply Interrupts
1649  */
1650 void
mpt3sas_base_unmask_interrupts(struct MPT3SAS_ADAPTER * ioc)1651 mpt3sas_base_unmask_interrupts(struct MPT3SAS_ADAPTER *ioc)
1652 {
1653 	u32 him_register;
1654 
1655 	him_register = ioc->base_readl(&ioc->chip->HostInterruptMask);
1656 	him_register &= ~MPI2_HIM_RIM;
1657 	writel(him_register, &ioc->chip->HostInterruptMask);
1658 	ioc->mask_interrupts = 0;
1659 }
1660 
1661 union reply_descriptor {
1662 	u64 word;
1663 	struct {
1664 		u32 low;
1665 		u32 high;
1666 	} u;
1667 };
1668 
base_mod64(u64 dividend,u32 divisor)1669 static u32 base_mod64(u64 dividend, u32 divisor)
1670 {
1671 	u32 remainder;
1672 
1673 	if (!divisor)
1674 		pr_err("mpt3sas: DIVISOR is zero, in div fn\n");
1675 	remainder = do_div(dividend, divisor);
1676 	return remainder;
1677 }
1678 
1679 /**
1680  * _base_process_reply_queue - Process reply descriptors from reply
1681  *		descriptor post queue.
1682  * @reply_q: per IRQ's reply queue object.
1683  *
1684  * Return: number of reply descriptors processed from reply
1685  *		descriptor queue.
1686  */
1687 static int
_base_process_reply_queue(struct adapter_reply_queue * reply_q)1688 _base_process_reply_queue(struct adapter_reply_queue *reply_q)
1689 {
1690 	union reply_descriptor rd;
1691 	u64 completed_cmds;
1692 	u8 request_descript_type;
1693 	u16 smid;
1694 	u8 cb_idx;
1695 	u32 reply;
1696 	u8 msix_index = reply_q->msix_index;
1697 	struct MPT3SAS_ADAPTER *ioc = reply_q->ioc;
1698 	Mpi2ReplyDescriptorsUnion_t *rpf;
1699 	u8 rc;
1700 
1701 	completed_cmds = 0;
1702 	if (!atomic_add_unless(&reply_q->busy, 1, 1))
1703 		return completed_cmds;
1704 
1705 	rpf = &reply_q->reply_post_free[reply_q->reply_post_host_index];
1706 	request_descript_type = rpf->Default.ReplyFlags
1707 	     & MPI2_RPY_DESCRIPT_FLAGS_TYPE_MASK;
1708 	if (request_descript_type == MPI2_RPY_DESCRIPT_FLAGS_UNUSED) {
1709 		atomic_dec(&reply_q->busy);
1710 		return completed_cmds;
1711 	}
1712 
1713 	cb_idx = 0xFF;
1714 	do {
1715 		rd.word = le64_to_cpu(rpf->Words);
1716 		if (rd.u.low == UINT_MAX || rd.u.high == UINT_MAX)
1717 			goto out;
1718 		reply = 0;
1719 		smid = le16_to_cpu(rpf->Default.DescriptorTypeDependent1);
1720 		if (request_descript_type ==
1721 		    MPI25_RPY_DESCRIPT_FLAGS_FAST_PATH_SCSI_IO_SUCCESS ||
1722 		    request_descript_type ==
1723 		    MPI2_RPY_DESCRIPT_FLAGS_SCSI_IO_SUCCESS ||
1724 		    request_descript_type ==
1725 		    MPI26_RPY_DESCRIPT_FLAGS_PCIE_ENCAPSULATED_SUCCESS) {
1726 			cb_idx = _base_get_cb_idx(ioc, smid);
1727 			if ((likely(cb_idx < MPT_MAX_CALLBACKS)) &&
1728 			    (likely(mpt_callbacks[cb_idx] != NULL))) {
1729 				rc = mpt_callbacks[cb_idx](ioc, smid,
1730 				    msix_index, 0);
1731 				if (rc)
1732 					mpt3sas_base_free_smid(ioc, smid);
1733 			}
1734 		} else if (request_descript_type ==
1735 		    MPI2_RPY_DESCRIPT_FLAGS_ADDRESS_REPLY) {
1736 			reply = le32_to_cpu(
1737 			    rpf->AddressReply.ReplyFrameAddress);
1738 			if (reply > ioc->reply_dma_max_address ||
1739 			    reply < ioc->reply_dma_min_address)
1740 				reply = 0;
1741 			if (smid) {
1742 				cb_idx = _base_get_cb_idx(ioc, smid);
1743 				if ((likely(cb_idx < MPT_MAX_CALLBACKS)) &&
1744 				    (likely(mpt_callbacks[cb_idx] != NULL))) {
1745 					rc = mpt_callbacks[cb_idx](ioc, smid,
1746 					    msix_index, reply);
1747 					if (reply)
1748 						_base_display_reply_info(ioc,
1749 						    smid, msix_index, reply);
1750 					if (rc)
1751 						mpt3sas_base_free_smid(ioc,
1752 						    smid);
1753 				}
1754 			} else {
1755 				_base_async_event(ioc, msix_index, reply);
1756 			}
1757 
1758 			/* reply free queue handling */
1759 			if (reply) {
1760 				ioc->reply_free_host_index =
1761 				    (ioc->reply_free_host_index ==
1762 				    (ioc->reply_free_queue_depth - 1)) ?
1763 				    0 : ioc->reply_free_host_index + 1;
1764 				ioc->reply_free[ioc->reply_free_host_index] =
1765 				    cpu_to_le32(reply);
1766 				if (ioc->is_mcpu_endpoint)
1767 					_base_clone_reply_to_sys_mem(ioc,
1768 						reply,
1769 						ioc->reply_free_host_index);
1770 				writel(ioc->reply_free_host_index,
1771 				    &ioc->chip->ReplyFreeHostIndex);
1772 			}
1773 		}
1774 
1775 		rpf->Words = cpu_to_le64(ULLONG_MAX);
1776 		reply_q->reply_post_host_index =
1777 		    (reply_q->reply_post_host_index ==
1778 		    (ioc->reply_post_queue_depth - 1)) ? 0 :
1779 		    reply_q->reply_post_host_index + 1;
1780 		request_descript_type =
1781 		    reply_q->reply_post_free[reply_q->reply_post_host_index].
1782 		    Default.ReplyFlags & MPI2_RPY_DESCRIPT_FLAGS_TYPE_MASK;
1783 		completed_cmds++;
1784 		/* Update the reply post host index after continuously
1785 		 * processing the threshold number of Reply Descriptors.
1786 		 * So that FW can find enough entries to post the Reply
1787 		 * Descriptors in the reply descriptor post queue.
1788 		 */
1789 		if (completed_cmds >= ioc->thresh_hold) {
1790 			if (ioc->combined_reply_queue) {
1791 				writel(reply_q->reply_post_host_index |
1792 						((msix_index  & 7) <<
1793 						 MPI2_RPHI_MSIX_INDEX_SHIFT),
1794 				    ioc->replyPostRegisterIndex[msix_index/8]);
1795 			} else {
1796 				writel(reply_q->reply_post_host_index |
1797 						(msix_index <<
1798 						 MPI2_RPHI_MSIX_INDEX_SHIFT),
1799 						&ioc->chip->ReplyPostHostIndex);
1800 			}
1801 			if (!reply_q->is_iouring_poll_q &&
1802 			    !reply_q->irq_poll_scheduled) {
1803 				reply_q->irq_poll_scheduled = true;
1804 				irq_poll_sched(&reply_q->irqpoll);
1805 			}
1806 			atomic_dec(&reply_q->busy);
1807 			return completed_cmds;
1808 		}
1809 		if (request_descript_type == MPI2_RPY_DESCRIPT_FLAGS_UNUSED)
1810 			goto out;
1811 		if (!reply_q->reply_post_host_index)
1812 			rpf = reply_q->reply_post_free;
1813 		else
1814 			rpf++;
1815 	} while (1);
1816 
1817  out:
1818 
1819 	if (!completed_cmds) {
1820 		atomic_dec(&reply_q->busy);
1821 		return completed_cmds;
1822 	}
1823 
1824 	if (ioc->is_warpdrive) {
1825 		writel(reply_q->reply_post_host_index,
1826 		ioc->reply_post_host_index[msix_index]);
1827 		atomic_dec(&reply_q->busy);
1828 		return completed_cmds;
1829 	}
1830 
1831 	/* Update Reply Post Host Index.
1832 	 * For those HBA's which support combined reply queue feature
1833 	 * 1. Get the correct Supplemental Reply Post Host Index Register.
1834 	 *    i.e. (msix_index / 8)th entry from Supplemental Reply Post Host
1835 	 *    Index Register address bank i.e replyPostRegisterIndex[],
1836 	 * 2. Then update this register with new reply host index value
1837 	 *    in ReplyPostIndex field and the MSIxIndex field with
1838 	 *    msix_index value reduced to a value between 0 and 7,
1839 	 *    using a modulo 8 operation. Since each Supplemental Reply Post
1840 	 *    Host Index Register supports 8 MSI-X vectors.
1841 	 *
1842 	 * For other HBA's just update the Reply Post Host Index register with
1843 	 * new reply host index value in ReplyPostIndex Field and msix_index
1844 	 * value in MSIxIndex field.
1845 	 */
1846 	if (ioc->combined_reply_queue)
1847 		writel(reply_q->reply_post_host_index | ((msix_index  & 7) <<
1848 			MPI2_RPHI_MSIX_INDEX_SHIFT),
1849 			ioc->replyPostRegisterIndex[msix_index/8]);
1850 	else
1851 		writel(reply_q->reply_post_host_index | (msix_index <<
1852 			MPI2_RPHI_MSIX_INDEX_SHIFT),
1853 			&ioc->chip->ReplyPostHostIndex);
1854 	atomic_dec(&reply_q->busy);
1855 	return completed_cmds;
1856 }
1857 
1858 /**
1859  * mpt3sas_blk_mq_poll - poll the blk mq poll queue
1860  * @shost: Scsi_Host object
1861  * @queue_num: hw ctx queue number
1862  *
1863  * Return number of entries that has been processed from poll queue.
1864  */
mpt3sas_blk_mq_poll(struct Scsi_Host * shost,unsigned int queue_num)1865 int mpt3sas_blk_mq_poll(struct Scsi_Host *shost, unsigned int queue_num)
1866 {
1867 	struct MPT3SAS_ADAPTER *ioc =
1868 	    (struct MPT3SAS_ADAPTER *)shost->hostdata;
1869 	struct adapter_reply_queue *reply_q;
1870 	int num_entries = 0;
1871 	int qid = queue_num - ioc->iopoll_q_start_index;
1872 
1873 	if (atomic_read(&ioc->io_uring_poll_queues[qid].pause) ||
1874 	    !atomic_add_unless(&ioc->io_uring_poll_queues[qid].busy, 1, 1))
1875 		return 0;
1876 
1877 	reply_q = ioc->io_uring_poll_queues[qid].reply_q;
1878 
1879 	num_entries = _base_process_reply_queue(reply_q);
1880 	atomic_dec(&ioc->io_uring_poll_queues[qid].busy);
1881 
1882 	return num_entries;
1883 }
1884 
1885 /**
1886  * _base_interrupt - MPT adapter (IOC) specific interrupt handler.
1887  * @irq: irq number (not used)
1888  * @bus_id: bus identifier cookie == pointer to MPT_ADAPTER structure
1889  *
1890  * Return: IRQ_HANDLED if processed, else IRQ_NONE.
1891  */
1892 static irqreturn_t
_base_interrupt(int irq,void * bus_id)1893 _base_interrupt(int irq, void *bus_id)
1894 {
1895 	struct adapter_reply_queue *reply_q = bus_id;
1896 	struct MPT3SAS_ADAPTER *ioc = reply_q->ioc;
1897 
1898 	if (ioc->mask_interrupts)
1899 		return IRQ_NONE;
1900 	if (reply_q->irq_poll_scheduled)
1901 		return IRQ_HANDLED;
1902 	return ((_base_process_reply_queue(reply_q) > 0) ?
1903 			IRQ_HANDLED : IRQ_NONE);
1904 }
1905 
1906 /**
1907  * _base_irqpoll - IRQ poll callback handler
1908  * @irqpoll: irq_poll object
1909  * @budget: irq poll weight
1910  *
1911  * Return: number of reply descriptors processed
1912  */
1913 static int
_base_irqpoll(struct irq_poll * irqpoll,int budget)1914 _base_irqpoll(struct irq_poll *irqpoll, int budget)
1915 {
1916 	struct adapter_reply_queue *reply_q;
1917 	int num_entries = 0;
1918 
1919 	reply_q = container_of(irqpoll, struct adapter_reply_queue,
1920 			irqpoll);
1921 	if (reply_q->irq_line_enable) {
1922 		disable_irq_nosync(reply_q->os_irq);
1923 		reply_q->irq_line_enable = false;
1924 	}
1925 	num_entries = _base_process_reply_queue(reply_q);
1926 	if (num_entries < budget) {
1927 		irq_poll_complete(irqpoll);
1928 		reply_q->irq_poll_scheduled = false;
1929 		reply_q->irq_line_enable = true;
1930 		enable_irq(reply_q->os_irq);
1931 		/*
1932 		 * Go for one more round of processing the
1933 		 * reply descriptor post queue in case the HBA
1934 		 * Firmware has posted some reply descriptors
1935 		 * while reenabling the IRQ.
1936 		 */
1937 		_base_process_reply_queue(reply_q);
1938 	}
1939 
1940 	return num_entries;
1941 }
1942 
1943 /**
1944  * _base_init_irqpolls - initliaze IRQ polls
1945  * @ioc: per adapter object
1946  *
1947  * Return: nothing
1948  */
1949 static void
_base_init_irqpolls(struct MPT3SAS_ADAPTER * ioc)1950 _base_init_irqpolls(struct MPT3SAS_ADAPTER *ioc)
1951 {
1952 	struct adapter_reply_queue *reply_q, *next;
1953 
1954 	if (list_empty(&ioc->reply_queue_list))
1955 		return;
1956 
1957 	list_for_each_entry_safe(reply_q, next, &ioc->reply_queue_list, list) {
1958 		if (reply_q->is_iouring_poll_q)
1959 			continue;
1960 		irq_poll_init(&reply_q->irqpoll,
1961 			ioc->hba_queue_depth/4, _base_irqpoll);
1962 		reply_q->irq_poll_scheduled = false;
1963 		reply_q->irq_line_enable = true;
1964 		reply_q->os_irq = pci_irq_vector(ioc->pdev,
1965 		    reply_q->msix_index);
1966 	}
1967 }
1968 
1969 /**
1970  * _base_is_controller_msix_enabled - is controller support muli-reply queues
1971  * @ioc: per adapter object
1972  *
1973  * Return: Whether or not MSI/X is enabled.
1974  */
1975 static inline int
_base_is_controller_msix_enabled(struct MPT3SAS_ADAPTER * ioc)1976 _base_is_controller_msix_enabled(struct MPT3SAS_ADAPTER *ioc)
1977 {
1978 	return (ioc->facts.IOCCapabilities &
1979 	    MPI2_IOCFACTS_CAPABILITY_MSI_X_INDEX) && ioc->msix_enable;
1980 }
1981 
1982 /**
1983  * mpt3sas_base_sync_reply_irqs - flush pending MSIX interrupts
1984  * @ioc: per adapter object
1985  * @poll: poll over reply descriptor pools incase interrupt for
1986  *		timed-out SCSI command got delayed
1987  * Context: non-ISR context
1988  *
1989  * Called when a Task Management request has completed.
1990  */
1991 void
mpt3sas_base_sync_reply_irqs(struct MPT3SAS_ADAPTER * ioc,u8 poll)1992 mpt3sas_base_sync_reply_irqs(struct MPT3SAS_ADAPTER *ioc, u8 poll)
1993 {
1994 	struct adapter_reply_queue *reply_q;
1995 
1996 	/* If MSIX capability is turned off
1997 	 * then multi-queues are not enabled
1998 	 */
1999 	if (!_base_is_controller_msix_enabled(ioc))
2000 		return;
2001 
2002 	list_for_each_entry(reply_q, &ioc->reply_queue_list, list) {
2003 		if (ioc->shost_recovery || ioc->remove_host ||
2004 				ioc->pci_error_recovery)
2005 			return;
2006 		/* TMs are on msix_index == 0 */
2007 		if (reply_q->msix_index == 0)
2008 			continue;
2009 
2010 		if (reply_q->is_iouring_poll_q) {
2011 			_base_process_reply_queue(reply_q);
2012 			continue;
2013 		}
2014 
2015 		synchronize_irq(pci_irq_vector(ioc->pdev, reply_q->msix_index));
2016 		if (reply_q->irq_poll_scheduled) {
2017 			/* Calling irq_poll_disable will wait for any pending
2018 			 * callbacks to have completed.
2019 			 */
2020 			irq_poll_disable(&reply_q->irqpoll);
2021 			irq_poll_enable(&reply_q->irqpoll);
2022 			/* check how the scheduled poll has ended,
2023 			 * clean up only if necessary
2024 			 */
2025 			if (reply_q->irq_poll_scheduled) {
2026 				reply_q->irq_poll_scheduled = false;
2027 				reply_q->irq_line_enable = true;
2028 				enable_irq(reply_q->os_irq);
2029 			}
2030 		}
2031 
2032 		if (poll)
2033 			_base_process_reply_queue(reply_q);
2034 	}
2035 }
2036 
2037 /**
2038  * mpt3sas_base_release_callback_handler - clear interrupt callback handler
2039  * @cb_idx: callback index
2040  */
2041 void
mpt3sas_base_release_callback_handler(u8 cb_idx)2042 mpt3sas_base_release_callback_handler(u8 cb_idx)
2043 {
2044 	mpt_callbacks[cb_idx] = NULL;
2045 }
2046 
2047 /**
2048  * mpt3sas_base_register_callback_handler - obtain index for the interrupt callback handler
2049  * @cb_func: callback function
2050  *
2051  * Return: Index of @cb_func.
2052  */
2053 u8
mpt3sas_base_register_callback_handler(MPT_CALLBACK cb_func)2054 mpt3sas_base_register_callback_handler(MPT_CALLBACK cb_func)
2055 {
2056 	u8 cb_idx;
2057 
2058 	for (cb_idx = MPT_MAX_CALLBACKS-1; cb_idx; cb_idx--)
2059 		if (mpt_callbacks[cb_idx] == NULL)
2060 			break;
2061 
2062 	mpt_callbacks[cb_idx] = cb_func;
2063 	return cb_idx;
2064 }
2065 
2066 /**
2067  * mpt3sas_base_initialize_callback_handler - initialize the interrupt callback handler
2068  */
2069 void
mpt3sas_base_initialize_callback_handler(void)2070 mpt3sas_base_initialize_callback_handler(void)
2071 {
2072 	u8 cb_idx;
2073 
2074 	for (cb_idx = 0; cb_idx < MPT_MAX_CALLBACKS; cb_idx++)
2075 		mpt3sas_base_release_callback_handler(cb_idx);
2076 }
2077 
2078 
2079 /**
2080  * _base_build_zero_len_sge - build zero length sg entry
2081  * @ioc: per adapter object
2082  * @paddr: virtual address for SGE
2083  *
2084  * Create a zero length scatter gather entry to insure the IOCs hardware has
2085  * something to use if the target device goes brain dead and tries
2086  * to send data even when none is asked for.
2087  */
2088 static void
_base_build_zero_len_sge(struct MPT3SAS_ADAPTER * ioc,void * paddr)2089 _base_build_zero_len_sge(struct MPT3SAS_ADAPTER *ioc, void *paddr)
2090 {
2091 	u32 flags_length = (u32)((MPI2_SGE_FLAGS_LAST_ELEMENT |
2092 	    MPI2_SGE_FLAGS_END_OF_BUFFER | MPI2_SGE_FLAGS_END_OF_LIST |
2093 	    MPI2_SGE_FLAGS_SIMPLE_ELEMENT) <<
2094 	    MPI2_SGE_FLAGS_SHIFT);
2095 	ioc->base_add_sg_single(paddr, flags_length, -1);
2096 }
2097 
2098 /**
2099  * _base_add_sg_single_32 - Place a simple 32 bit SGE at address pAddr.
2100  * @paddr: virtual address for SGE
2101  * @flags_length: SGE flags and data transfer length
2102  * @dma_addr: Physical address
2103  */
2104 static void
_base_add_sg_single_32(void * paddr,u32 flags_length,dma_addr_t dma_addr)2105 _base_add_sg_single_32(void *paddr, u32 flags_length, dma_addr_t dma_addr)
2106 {
2107 	Mpi2SGESimple32_t *sgel = paddr;
2108 
2109 	flags_length |= (MPI2_SGE_FLAGS_32_BIT_ADDRESSING |
2110 	    MPI2_SGE_FLAGS_SYSTEM_ADDRESS) << MPI2_SGE_FLAGS_SHIFT;
2111 	sgel->FlagsLength = cpu_to_le32(flags_length);
2112 	sgel->Address = cpu_to_le32(dma_addr);
2113 }
2114 
2115 
2116 /**
2117  * _base_add_sg_single_64 - Place a simple 64 bit SGE at address pAddr.
2118  * @paddr: virtual address for SGE
2119  * @flags_length: SGE flags and data transfer length
2120  * @dma_addr: Physical address
2121  */
2122 static void
_base_add_sg_single_64(void * paddr,u32 flags_length,dma_addr_t dma_addr)2123 _base_add_sg_single_64(void *paddr, u32 flags_length, dma_addr_t dma_addr)
2124 {
2125 	Mpi2SGESimple64_t *sgel = paddr;
2126 
2127 	flags_length |= (MPI2_SGE_FLAGS_64_BIT_ADDRESSING |
2128 	    MPI2_SGE_FLAGS_SYSTEM_ADDRESS) << MPI2_SGE_FLAGS_SHIFT;
2129 	sgel->FlagsLength = cpu_to_le32(flags_length);
2130 	sgel->Address = cpu_to_le64(dma_addr);
2131 }
2132 
2133 /**
2134  * _base_get_chain_buffer_tracker - obtain chain tracker
2135  * @ioc: per adapter object
2136  * @scmd: SCSI commands of the IO request
2137  *
2138  * Return: chain tracker from chain_lookup table using key as
2139  * smid and smid's chain_offset.
2140  */
2141 static struct chain_tracker *
_base_get_chain_buffer_tracker(struct MPT3SAS_ADAPTER * ioc,struct scsi_cmnd * scmd)2142 _base_get_chain_buffer_tracker(struct MPT3SAS_ADAPTER *ioc,
2143 			       struct scsi_cmnd *scmd)
2144 {
2145 	struct chain_tracker *chain_req;
2146 	struct scsiio_tracker *st = scsi_cmd_priv(scmd);
2147 	u16 smid = st->smid;
2148 	u8 chain_offset =
2149 	   atomic_read(&ioc->chain_lookup[smid - 1].chain_offset);
2150 
2151 	if (chain_offset == ioc->chains_needed_per_io)
2152 		return NULL;
2153 
2154 	chain_req = &ioc->chain_lookup[smid - 1].chains_per_smid[chain_offset];
2155 	atomic_inc(&ioc->chain_lookup[smid - 1].chain_offset);
2156 	return chain_req;
2157 }
2158 
2159 
2160 /**
2161  * _base_build_sg - build generic sg
2162  * @ioc: per adapter object
2163  * @psge: virtual address for SGE
2164  * @data_out_dma: physical address for WRITES
2165  * @data_out_sz: data xfer size for WRITES
2166  * @data_in_dma: physical address for READS
2167  * @data_in_sz: data xfer size for READS
2168  */
2169 static void
_base_build_sg(struct MPT3SAS_ADAPTER * ioc,void * psge,dma_addr_t data_out_dma,size_t data_out_sz,dma_addr_t data_in_dma,size_t data_in_sz)2170 _base_build_sg(struct MPT3SAS_ADAPTER *ioc, void *psge,
2171 	dma_addr_t data_out_dma, size_t data_out_sz, dma_addr_t data_in_dma,
2172 	size_t data_in_sz)
2173 {
2174 	u32 sgl_flags;
2175 
2176 	if (!data_out_sz && !data_in_sz) {
2177 		_base_build_zero_len_sge(ioc, psge);
2178 		return;
2179 	}
2180 
2181 	if (data_out_sz && data_in_sz) {
2182 		/* WRITE sgel first */
2183 		sgl_flags = (MPI2_SGE_FLAGS_SIMPLE_ELEMENT |
2184 		    MPI2_SGE_FLAGS_END_OF_BUFFER | MPI2_SGE_FLAGS_HOST_TO_IOC);
2185 		sgl_flags = sgl_flags << MPI2_SGE_FLAGS_SHIFT;
2186 		ioc->base_add_sg_single(psge, sgl_flags |
2187 		    data_out_sz, data_out_dma);
2188 
2189 		/* incr sgel */
2190 		psge += ioc->sge_size;
2191 
2192 		/* READ sgel last */
2193 		sgl_flags = (MPI2_SGE_FLAGS_SIMPLE_ELEMENT |
2194 		    MPI2_SGE_FLAGS_LAST_ELEMENT | MPI2_SGE_FLAGS_END_OF_BUFFER |
2195 		    MPI2_SGE_FLAGS_END_OF_LIST);
2196 		sgl_flags = sgl_flags << MPI2_SGE_FLAGS_SHIFT;
2197 		ioc->base_add_sg_single(psge, sgl_flags |
2198 		    data_in_sz, data_in_dma);
2199 	} else if (data_out_sz) /* WRITE */ {
2200 		sgl_flags = (MPI2_SGE_FLAGS_SIMPLE_ELEMENT |
2201 		    MPI2_SGE_FLAGS_LAST_ELEMENT | MPI2_SGE_FLAGS_END_OF_BUFFER |
2202 		    MPI2_SGE_FLAGS_END_OF_LIST | MPI2_SGE_FLAGS_HOST_TO_IOC);
2203 		sgl_flags = sgl_flags << MPI2_SGE_FLAGS_SHIFT;
2204 		ioc->base_add_sg_single(psge, sgl_flags |
2205 		    data_out_sz, data_out_dma);
2206 	} else if (data_in_sz) /* READ */ {
2207 		sgl_flags = (MPI2_SGE_FLAGS_SIMPLE_ELEMENT |
2208 		    MPI2_SGE_FLAGS_LAST_ELEMENT | MPI2_SGE_FLAGS_END_OF_BUFFER |
2209 		    MPI2_SGE_FLAGS_END_OF_LIST);
2210 		sgl_flags = sgl_flags << MPI2_SGE_FLAGS_SHIFT;
2211 		ioc->base_add_sg_single(psge, sgl_flags |
2212 		    data_in_sz, data_in_dma);
2213 	}
2214 }
2215 
2216 /* IEEE format sgls */
2217 
2218 /**
2219  * _base_build_nvme_prp - This function is called for NVMe end devices to build
2220  *                        a native SGL (NVMe PRP).
2221  * @ioc: per adapter object
2222  * @smid: system request message index for getting asscociated SGL
2223  * @nvme_encap_request: the NVMe request msg frame pointer
2224  * @data_out_dma: physical address for WRITES
2225  * @data_out_sz: data xfer size for WRITES
2226  * @data_in_dma: physical address for READS
2227  * @data_in_sz: data xfer size for READS
2228  *
2229  * The native SGL is built starting in the first PRP
2230  * entry of the NVMe message (PRP1).  If the data buffer is small enough to be
2231  * described entirely using PRP1, then PRP2 is not used.  If needed, PRP2 is
2232  * used to describe a larger data buffer.  If the data buffer is too large to
2233  * describe using the two PRP entriess inside the NVMe message, then PRP1
2234  * describes the first data memory segment, and PRP2 contains a pointer to a PRP
2235  * list located elsewhere in memory to describe the remaining data memory
2236  * segments.  The PRP list will be contiguous.
2237  *
2238  * The native SGL for NVMe devices is a Physical Region Page (PRP).  A PRP
2239  * consists of a list of PRP entries to describe a number of noncontigous
2240  * physical memory segments as a single memory buffer, just as a SGL does.  Note
2241  * however, that this function is only used by the IOCTL call, so the memory
2242  * given will be guaranteed to be contiguous.  There is no need to translate
2243  * non-contiguous SGL into a PRP in this case.  All PRPs will describe
2244  * contiguous space that is one page size each.
2245  *
2246  * Each NVMe message contains two PRP entries.  The first (PRP1) either contains
2247  * a PRP list pointer or a PRP element, depending upon the command.  PRP2
2248  * contains the second PRP element if the memory being described fits within 2
2249  * PRP entries, or a PRP list pointer if the PRP spans more than two entries.
2250  *
2251  * A PRP list pointer contains the address of a PRP list, structured as a linear
2252  * array of PRP entries.  Each PRP entry in this list describes a segment of
2253  * physical memory.
2254  *
2255  * Each 64-bit PRP entry comprises an address and an offset field.  The address
2256  * always points at the beginning of a 4KB physical memory page, and the offset
2257  * describes where within that 4KB page the memory segment begins.  Only the
2258  * first element in a PRP list may contain a non-zero offset, implying that all
2259  * memory segments following the first begin at the start of a 4KB page.
2260  *
2261  * Each PRP element normally describes 4KB of physical memory, with exceptions
2262  * for the first and last elements in the list.  If the memory being described
2263  * by the list begins at a non-zero offset within the first 4KB page, then the
2264  * first PRP element will contain a non-zero offset indicating where the region
2265  * begins within the 4KB page.  The last memory segment may end before the end
2266  * of the 4KB segment, depending upon the overall size of the memory being
2267  * described by the PRP list.
2268  *
2269  * Since PRP entries lack any indication of size, the overall data buffer length
2270  * is used to determine where the end of the data memory buffer is located, and
2271  * how many PRP entries are required to describe it.
2272  */
2273 static void
_base_build_nvme_prp(struct MPT3SAS_ADAPTER * ioc,u16 smid,Mpi26NVMeEncapsulatedRequest_t * nvme_encap_request,dma_addr_t data_out_dma,size_t data_out_sz,dma_addr_t data_in_dma,size_t data_in_sz)2274 _base_build_nvme_prp(struct MPT3SAS_ADAPTER *ioc, u16 smid,
2275 	Mpi26NVMeEncapsulatedRequest_t *nvme_encap_request,
2276 	dma_addr_t data_out_dma, size_t data_out_sz, dma_addr_t data_in_dma,
2277 	size_t data_in_sz)
2278 {
2279 	int		prp_size = NVME_PRP_SIZE;
2280 	__le64		*prp_entry, *prp1_entry, *prp2_entry;
2281 	__le64		*prp_page;
2282 	dma_addr_t	prp_entry_dma, prp_page_dma, dma_addr;
2283 	u32		offset, entry_len;
2284 	u32		page_mask_result, page_mask;
2285 	size_t		length;
2286 	struct mpt3sas_nvme_cmd *nvme_cmd =
2287 		(void *)nvme_encap_request->NVMe_Command;
2288 
2289 	/*
2290 	 * Not all commands require a data transfer. If no data, just return
2291 	 * without constructing any PRP.
2292 	 */
2293 	if (!data_in_sz && !data_out_sz)
2294 		return;
2295 	prp1_entry = &nvme_cmd->prp1;
2296 	prp2_entry = &nvme_cmd->prp2;
2297 	prp_entry = prp1_entry;
2298 	/*
2299 	 * For the PRP entries, use the specially allocated buffer of
2300 	 * contiguous memory.
2301 	 */
2302 	prp_page = (__le64 *)mpt3sas_base_get_pcie_sgl(ioc, smid);
2303 	prp_page_dma = mpt3sas_base_get_pcie_sgl_dma(ioc, smid);
2304 
2305 	/*
2306 	 * Check if we are within 1 entry of a page boundary we don't
2307 	 * want our first entry to be a PRP List entry.
2308 	 */
2309 	page_mask = ioc->page_size - 1;
2310 	page_mask_result = (uintptr_t)((u8 *)prp_page + prp_size) & page_mask;
2311 	if (!page_mask_result) {
2312 		/* Bump up to next page boundary. */
2313 		prp_page = (__le64 *)((u8 *)prp_page + prp_size);
2314 		prp_page_dma = prp_page_dma + prp_size;
2315 	}
2316 
2317 	/*
2318 	 * Set PRP physical pointer, which initially points to the current PRP
2319 	 * DMA memory page.
2320 	 */
2321 	prp_entry_dma = prp_page_dma;
2322 
2323 	/* Get physical address and length of the data buffer. */
2324 	if (data_in_sz) {
2325 		dma_addr = data_in_dma;
2326 		length = data_in_sz;
2327 	} else {
2328 		dma_addr = data_out_dma;
2329 		length = data_out_sz;
2330 	}
2331 
2332 	/* Loop while the length is not zero. */
2333 	while (length) {
2334 		/*
2335 		 * Check if we need to put a list pointer here if we are at
2336 		 * page boundary - prp_size (8 bytes).
2337 		 */
2338 		page_mask_result = (prp_entry_dma + prp_size) & page_mask;
2339 		if (!page_mask_result) {
2340 			/*
2341 			 * This is the last entry in a PRP List, so we need to
2342 			 * put a PRP list pointer here.  What this does is:
2343 			 *   - bump the current memory pointer to the next
2344 			 *     address, which will be the next full page.
2345 			 *   - set the PRP Entry to point to that page.  This
2346 			 *     is now the PRP List pointer.
2347 			 *   - bump the PRP Entry pointer the start of the
2348 			 *     next page.  Since all of this PRP memory is
2349 			 *     contiguous, no need to get a new page - it's
2350 			 *     just the next address.
2351 			 */
2352 			prp_entry_dma++;
2353 			*prp_entry = cpu_to_le64(prp_entry_dma);
2354 			prp_entry++;
2355 		}
2356 
2357 		/* Need to handle if entry will be part of a page. */
2358 		offset = dma_addr & page_mask;
2359 		entry_len = ioc->page_size - offset;
2360 
2361 		if (prp_entry == prp1_entry) {
2362 			/*
2363 			 * Must fill in the first PRP pointer (PRP1) before
2364 			 * moving on.
2365 			 */
2366 			*prp1_entry = cpu_to_le64(dma_addr);
2367 
2368 			/*
2369 			 * Now point to the second PRP entry within the
2370 			 * command (PRP2).
2371 			 */
2372 			prp_entry = prp2_entry;
2373 		} else if (prp_entry == prp2_entry) {
2374 			/*
2375 			 * Should the PRP2 entry be a PRP List pointer or just
2376 			 * a regular PRP pointer?  If there is more than one
2377 			 * more page of data, must use a PRP List pointer.
2378 			 */
2379 			if (length > ioc->page_size) {
2380 				/*
2381 				 * PRP2 will contain a PRP List pointer because
2382 				 * more PRP's are needed with this command. The
2383 				 * list will start at the beginning of the
2384 				 * contiguous buffer.
2385 				 */
2386 				*prp2_entry = cpu_to_le64(prp_entry_dma);
2387 
2388 				/*
2389 				 * The next PRP Entry will be the start of the
2390 				 * first PRP List.
2391 				 */
2392 				prp_entry = prp_page;
2393 			} else {
2394 				/*
2395 				 * After this, the PRP Entries are complete.
2396 				 * This command uses 2 PRP's and no PRP list.
2397 				 */
2398 				*prp2_entry = cpu_to_le64(dma_addr);
2399 			}
2400 		} else {
2401 			/*
2402 			 * Put entry in list and bump the addresses.
2403 			 *
2404 			 * After PRP1 and PRP2 are filled in, this will fill in
2405 			 * all remaining PRP entries in a PRP List, one per
2406 			 * each time through the loop.
2407 			 */
2408 			*prp_entry = cpu_to_le64(dma_addr);
2409 			prp_entry++;
2410 			prp_entry_dma++;
2411 		}
2412 
2413 		/*
2414 		 * Bump the phys address of the command's data buffer by the
2415 		 * entry_len.
2416 		 */
2417 		dma_addr += entry_len;
2418 
2419 		/* Decrement length accounting for last partial page. */
2420 		if (entry_len > length)
2421 			length = 0;
2422 		else
2423 			length -= entry_len;
2424 	}
2425 }
2426 
2427 /**
2428  * base_make_prp_nvme - Prepare PRPs (Physical Region Page) -
2429  *			SGLs specific to NVMe drives only
2430  *
2431  * @ioc:		per adapter object
2432  * @scmd:		SCSI command from the mid-layer
2433  * @mpi_request:	mpi request
2434  * @smid:		msg Index
2435  * @sge_count:		scatter gather element count.
2436  *
2437  * Return:		true: PRPs are built
2438  *			false: IEEE SGLs needs to be built
2439  */
2440 static void
base_make_prp_nvme(struct MPT3SAS_ADAPTER * ioc,struct scsi_cmnd * scmd,Mpi25SCSIIORequest_t * mpi_request,u16 smid,int sge_count)2441 base_make_prp_nvme(struct MPT3SAS_ADAPTER *ioc,
2442 		struct scsi_cmnd *scmd,
2443 		Mpi25SCSIIORequest_t *mpi_request,
2444 		u16 smid, int sge_count)
2445 {
2446 	int sge_len, num_prp_in_chain = 0;
2447 	Mpi25IeeeSgeChain64_t *main_chain_element, *ptr_first_sgl;
2448 	__le64 *curr_buff;
2449 	dma_addr_t msg_dma, sge_addr, offset;
2450 	u32 page_mask, page_mask_result;
2451 	struct scatterlist *sg_scmd;
2452 	u32 first_prp_len;
2453 	int data_len = scsi_bufflen(scmd);
2454 	u32 nvme_pg_size;
2455 
2456 	nvme_pg_size = max_t(u32, ioc->page_size, NVME_PRP_PAGE_SIZE);
2457 	/*
2458 	 * Nvme has a very convoluted prp format.  One prp is required
2459 	 * for each page or partial page. Driver need to split up OS sg_list
2460 	 * entries if it is longer than one page or cross a page
2461 	 * boundary.  Driver also have to insert a PRP list pointer entry as
2462 	 * the last entry in each physical page of the PRP list.
2463 	 *
2464 	 * NOTE: The first PRP "entry" is actually placed in the first
2465 	 * SGL entry in the main message as IEEE 64 format.  The 2nd
2466 	 * entry in the main message is the chain element, and the rest
2467 	 * of the PRP entries are built in the contiguous pcie buffer.
2468 	 */
2469 	page_mask = nvme_pg_size - 1;
2470 
2471 	/*
2472 	 * Native SGL is needed.
2473 	 * Put a chain element in main message frame that points to the first
2474 	 * chain buffer.
2475 	 *
2476 	 * NOTE:  The ChainOffset field must be 0 when using a chain pointer to
2477 	 *        a native SGL.
2478 	 */
2479 
2480 	/* Set main message chain element pointer */
2481 	main_chain_element = (pMpi25IeeeSgeChain64_t)&mpi_request->SGL;
2482 	/*
2483 	 * For NVMe the chain element needs to be the 2nd SG entry in the main
2484 	 * message.
2485 	 */
2486 	main_chain_element = (Mpi25IeeeSgeChain64_t *)
2487 		((u8 *)main_chain_element + sizeof(MPI25_IEEE_SGE_CHAIN64));
2488 
2489 	/*
2490 	 * For the PRP entries, use the specially allocated buffer of
2491 	 * contiguous memory.  Normal chain buffers can't be used
2492 	 * because each chain buffer would need to be the size of an OS
2493 	 * page (4k).
2494 	 */
2495 	curr_buff = mpt3sas_base_get_pcie_sgl(ioc, smid);
2496 	msg_dma = mpt3sas_base_get_pcie_sgl_dma(ioc, smid);
2497 
2498 	main_chain_element->Address = cpu_to_le64(msg_dma);
2499 	main_chain_element->NextChainOffset = 0;
2500 	main_chain_element->Flags = MPI2_IEEE_SGE_FLAGS_CHAIN_ELEMENT |
2501 			MPI2_IEEE_SGE_FLAGS_SYSTEM_ADDR |
2502 			MPI26_IEEE_SGE_FLAGS_NSF_NVME_PRP;
2503 
2504 	/* Build first prp, sge need not to be page aligned*/
2505 	ptr_first_sgl = (pMpi25IeeeSgeChain64_t)&mpi_request->SGL;
2506 	sg_scmd = scsi_sglist(scmd);
2507 	sge_addr = sg_dma_address(sg_scmd);
2508 	sge_len = sg_dma_len(sg_scmd);
2509 
2510 	offset = sge_addr & page_mask;
2511 	first_prp_len = nvme_pg_size - offset;
2512 
2513 	ptr_first_sgl->Address = cpu_to_le64(sge_addr);
2514 	ptr_first_sgl->Length = cpu_to_le32(first_prp_len);
2515 
2516 	data_len -= first_prp_len;
2517 
2518 	if (sge_len > first_prp_len) {
2519 		sge_addr += first_prp_len;
2520 		sge_len -= first_prp_len;
2521 	} else if (data_len && (sge_len == first_prp_len)) {
2522 		sg_scmd = sg_next(sg_scmd);
2523 		sge_addr = sg_dma_address(sg_scmd);
2524 		sge_len = sg_dma_len(sg_scmd);
2525 	}
2526 
2527 	for (;;) {
2528 		offset = sge_addr & page_mask;
2529 
2530 		/* Put PRP pointer due to page boundary*/
2531 		page_mask_result = (uintptr_t)(curr_buff + 1) & page_mask;
2532 		if (unlikely(!page_mask_result)) {
2533 			scmd_printk(KERN_NOTICE,
2534 				scmd, "page boundary curr_buff: 0x%p\n",
2535 				curr_buff);
2536 			msg_dma += 8;
2537 			*curr_buff = cpu_to_le64(msg_dma);
2538 			curr_buff++;
2539 			num_prp_in_chain++;
2540 		}
2541 
2542 		*curr_buff = cpu_to_le64(sge_addr);
2543 		curr_buff++;
2544 		msg_dma += 8;
2545 		num_prp_in_chain++;
2546 
2547 		sge_addr += nvme_pg_size;
2548 		sge_len -= nvme_pg_size;
2549 		data_len -= nvme_pg_size;
2550 
2551 		if (data_len <= 0)
2552 			break;
2553 
2554 		if (sge_len > 0)
2555 			continue;
2556 
2557 		sg_scmd = sg_next(sg_scmd);
2558 		sge_addr = sg_dma_address(sg_scmd);
2559 		sge_len = sg_dma_len(sg_scmd);
2560 	}
2561 
2562 	main_chain_element->Length =
2563 		cpu_to_le32(num_prp_in_chain * sizeof(u64));
2564 	return;
2565 }
2566 
2567 static bool
base_is_prp_possible(struct MPT3SAS_ADAPTER * ioc,struct _pcie_device * pcie_device,struct scsi_cmnd * scmd,int sge_count)2568 base_is_prp_possible(struct MPT3SAS_ADAPTER *ioc,
2569 	struct _pcie_device *pcie_device, struct scsi_cmnd *scmd, int sge_count)
2570 {
2571 	u32 data_length = 0;
2572 	bool build_prp = true;
2573 
2574 	data_length = scsi_bufflen(scmd);
2575 	if (pcie_device &&
2576 	    (mpt3sas_scsih_is_pcie_scsi_device(pcie_device->device_info))) {
2577 		build_prp = false;
2578 		return build_prp;
2579 	}
2580 
2581 	/* If Datalenth is <= 16K and number of SGE’s entries are <= 2
2582 	 * we built IEEE SGL
2583 	 */
2584 	if ((data_length <= NVME_PRP_PAGE_SIZE*4) && (sge_count <= 2))
2585 		build_prp = false;
2586 
2587 	return build_prp;
2588 }
2589 
2590 /**
2591  * _base_check_pcie_native_sgl - This function is called for PCIe end devices to
2592  * determine if the driver needs to build a native SGL.  If so, that native
2593  * SGL is built in the special contiguous buffers allocated especially for
2594  * PCIe SGL creation.  If the driver will not build a native SGL, return
2595  * TRUE and a normal IEEE SGL will be built.  Currently this routine
2596  * supports NVMe.
2597  * @ioc: per adapter object
2598  * @mpi_request: mf request pointer
2599  * @smid: system request message index
2600  * @scmd: scsi command
2601  * @pcie_device: points to the PCIe device's info
2602  *
2603  * Return: 0 if native SGL was built, 1 if no SGL was built
2604  */
2605 static int
_base_check_pcie_native_sgl(struct MPT3SAS_ADAPTER * ioc,Mpi25SCSIIORequest_t * mpi_request,u16 smid,struct scsi_cmnd * scmd,struct _pcie_device * pcie_device)2606 _base_check_pcie_native_sgl(struct MPT3SAS_ADAPTER *ioc,
2607 	Mpi25SCSIIORequest_t *mpi_request, u16 smid, struct scsi_cmnd *scmd,
2608 	struct _pcie_device *pcie_device)
2609 {
2610 	int sges_left;
2611 
2612 	/* Get the SG list pointer and info. */
2613 	sges_left = scsi_dma_map(scmd);
2614 	if (sges_left < 0)
2615 		return 1;
2616 
2617 	/* Check if we need to build a native SG list. */
2618 	if (!base_is_prp_possible(ioc, pcie_device,
2619 				scmd, sges_left)) {
2620 		/* We built a native SG list, just return. */
2621 		goto out;
2622 	}
2623 
2624 	/*
2625 	 * Build native NVMe PRP.
2626 	 */
2627 	base_make_prp_nvme(ioc, scmd, mpi_request,
2628 			smid, sges_left);
2629 
2630 	return 0;
2631 out:
2632 	scsi_dma_unmap(scmd);
2633 	return 1;
2634 }
2635 
2636 /**
2637  * _base_add_sg_single_ieee - add sg element for IEEE format
2638  * @paddr: virtual address for SGE
2639  * @flags: SGE flags
2640  * @chain_offset: number of 128 byte elements from start of segment
2641  * @length: data transfer length
2642  * @dma_addr: Physical address
2643  */
2644 static void
_base_add_sg_single_ieee(void * paddr,u8 flags,u8 chain_offset,u32 length,dma_addr_t dma_addr)2645 _base_add_sg_single_ieee(void *paddr, u8 flags, u8 chain_offset, u32 length,
2646 	dma_addr_t dma_addr)
2647 {
2648 	Mpi25IeeeSgeChain64_t *sgel = paddr;
2649 
2650 	sgel->Flags = flags;
2651 	sgel->NextChainOffset = chain_offset;
2652 	sgel->Length = cpu_to_le32(length);
2653 	sgel->Address = cpu_to_le64(dma_addr);
2654 }
2655 
2656 /**
2657  * _base_build_zero_len_sge_ieee - build zero length sg entry for IEEE format
2658  * @ioc: per adapter object
2659  * @paddr: virtual address for SGE
2660  *
2661  * Create a zero length scatter gather entry to insure the IOCs hardware has
2662  * something to use if the target device goes brain dead and tries
2663  * to send data even when none is asked for.
2664  */
2665 static void
_base_build_zero_len_sge_ieee(struct MPT3SAS_ADAPTER * ioc,void * paddr)2666 _base_build_zero_len_sge_ieee(struct MPT3SAS_ADAPTER *ioc, void *paddr)
2667 {
2668 	u8 sgl_flags = (MPI2_IEEE_SGE_FLAGS_SIMPLE_ELEMENT |
2669 		MPI2_IEEE_SGE_FLAGS_SYSTEM_ADDR |
2670 		MPI25_IEEE_SGE_FLAGS_END_OF_LIST);
2671 
2672 	_base_add_sg_single_ieee(paddr, sgl_flags, 0, 0, -1);
2673 }
2674 
2675 /**
2676  * _base_build_sg_scmd - main sg creation routine
2677  *		pcie_device is unused here!
2678  * @ioc: per adapter object
2679  * @scmd: scsi command
2680  * @smid: system request message index
2681  * @unused: unused pcie_device pointer
2682  * Context: none.
2683  *
2684  * The main routine that builds scatter gather table from a given
2685  * scsi request sent via the .queuecommand main handler.
2686  *
2687  * Return: 0 success, anything else error
2688  */
2689 static int
_base_build_sg_scmd(struct MPT3SAS_ADAPTER * ioc,struct scsi_cmnd * scmd,u16 smid,struct _pcie_device * unused)2690 _base_build_sg_scmd(struct MPT3SAS_ADAPTER *ioc,
2691 	struct scsi_cmnd *scmd, u16 smid, struct _pcie_device *unused)
2692 {
2693 	Mpi2SCSIIORequest_t *mpi_request;
2694 	dma_addr_t chain_dma;
2695 	struct scatterlist *sg_scmd;
2696 	void *sg_local, *chain;
2697 	u32 chain_offset;
2698 	u32 chain_length;
2699 	u32 chain_flags;
2700 	int sges_left;
2701 	u32 sges_in_segment;
2702 	u32 sgl_flags;
2703 	u32 sgl_flags_last_element;
2704 	u32 sgl_flags_end_buffer;
2705 	struct chain_tracker *chain_req;
2706 
2707 	mpi_request = mpt3sas_base_get_msg_frame(ioc, smid);
2708 
2709 	/* init scatter gather flags */
2710 	sgl_flags = MPI2_SGE_FLAGS_SIMPLE_ELEMENT;
2711 	if (scmd->sc_data_direction == DMA_TO_DEVICE)
2712 		sgl_flags |= MPI2_SGE_FLAGS_HOST_TO_IOC;
2713 	sgl_flags_last_element = (sgl_flags | MPI2_SGE_FLAGS_LAST_ELEMENT)
2714 	    << MPI2_SGE_FLAGS_SHIFT;
2715 	sgl_flags_end_buffer = (sgl_flags | MPI2_SGE_FLAGS_LAST_ELEMENT |
2716 	    MPI2_SGE_FLAGS_END_OF_BUFFER | MPI2_SGE_FLAGS_END_OF_LIST)
2717 	    << MPI2_SGE_FLAGS_SHIFT;
2718 	sgl_flags = sgl_flags << MPI2_SGE_FLAGS_SHIFT;
2719 
2720 	sg_scmd = scsi_sglist(scmd);
2721 	sges_left = scsi_dma_map(scmd);
2722 	if (sges_left < 0)
2723 		return -ENOMEM;
2724 
2725 	sg_local = &mpi_request->SGL;
2726 	sges_in_segment = ioc->max_sges_in_main_message;
2727 	if (sges_left <= sges_in_segment)
2728 		goto fill_in_last_segment;
2729 
2730 	mpi_request->ChainOffset = (offsetof(Mpi2SCSIIORequest_t, SGL) +
2731 	    (sges_in_segment * ioc->sge_size))/4;
2732 
2733 	/* fill in main message segment when there is a chain following */
2734 	while (sges_in_segment) {
2735 		if (sges_in_segment == 1)
2736 			ioc->base_add_sg_single(sg_local,
2737 			    sgl_flags_last_element | sg_dma_len(sg_scmd),
2738 			    sg_dma_address(sg_scmd));
2739 		else
2740 			ioc->base_add_sg_single(sg_local, sgl_flags |
2741 			    sg_dma_len(sg_scmd), sg_dma_address(sg_scmd));
2742 		sg_scmd = sg_next(sg_scmd);
2743 		sg_local += ioc->sge_size;
2744 		sges_left--;
2745 		sges_in_segment--;
2746 	}
2747 
2748 	/* initializing the chain flags and pointers */
2749 	chain_flags = MPI2_SGE_FLAGS_CHAIN_ELEMENT << MPI2_SGE_FLAGS_SHIFT;
2750 	chain_req = _base_get_chain_buffer_tracker(ioc, scmd);
2751 	if (!chain_req)
2752 		return -1;
2753 	chain = chain_req->chain_buffer;
2754 	chain_dma = chain_req->chain_buffer_dma;
2755 	do {
2756 		sges_in_segment = (sges_left <=
2757 		    ioc->max_sges_in_chain_message) ? sges_left :
2758 		    ioc->max_sges_in_chain_message;
2759 		chain_offset = (sges_left == sges_in_segment) ?
2760 		    0 : (sges_in_segment * ioc->sge_size)/4;
2761 		chain_length = sges_in_segment * ioc->sge_size;
2762 		if (chain_offset) {
2763 			chain_offset = chain_offset <<
2764 			    MPI2_SGE_CHAIN_OFFSET_SHIFT;
2765 			chain_length += ioc->sge_size;
2766 		}
2767 		ioc->base_add_sg_single(sg_local, chain_flags | chain_offset |
2768 		    chain_length, chain_dma);
2769 		sg_local = chain;
2770 		if (!chain_offset)
2771 			goto fill_in_last_segment;
2772 
2773 		/* fill in chain segments */
2774 		while (sges_in_segment) {
2775 			if (sges_in_segment == 1)
2776 				ioc->base_add_sg_single(sg_local,
2777 				    sgl_flags_last_element |
2778 				    sg_dma_len(sg_scmd),
2779 				    sg_dma_address(sg_scmd));
2780 			else
2781 				ioc->base_add_sg_single(sg_local, sgl_flags |
2782 				    sg_dma_len(sg_scmd),
2783 				    sg_dma_address(sg_scmd));
2784 			sg_scmd = sg_next(sg_scmd);
2785 			sg_local += ioc->sge_size;
2786 			sges_left--;
2787 			sges_in_segment--;
2788 		}
2789 
2790 		chain_req = _base_get_chain_buffer_tracker(ioc, scmd);
2791 		if (!chain_req)
2792 			return -1;
2793 		chain = chain_req->chain_buffer;
2794 		chain_dma = chain_req->chain_buffer_dma;
2795 	} while (1);
2796 
2797 
2798  fill_in_last_segment:
2799 
2800 	/* fill the last segment */
2801 	while (sges_left) {
2802 		if (sges_left == 1)
2803 			ioc->base_add_sg_single(sg_local, sgl_flags_end_buffer |
2804 			    sg_dma_len(sg_scmd), sg_dma_address(sg_scmd));
2805 		else
2806 			ioc->base_add_sg_single(sg_local, sgl_flags |
2807 			    sg_dma_len(sg_scmd), sg_dma_address(sg_scmd));
2808 		sg_scmd = sg_next(sg_scmd);
2809 		sg_local += ioc->sge_size;
2810 		sges_left--;
2811 	}
2812 
2813 	return 0;
2814 }
2815 
2816 /**
2817  * _base_build_sg_scmd_ieee - main sg creation routine for IEEE format
2818  * @ioc: per adapter object
2819  * @scmd: scsi command
2820  * @smid: system request message index
2821  * @pcie_device: Pointer to pcie_device. If set, the pcie native sgl will be
2822  * constructed on need.
2823  * Context: none.
2824  *
2825  * The main routine that builds scatter gather table from a given
2826  * scsi request sent via the .queuecommand main handler.
2827  *
2828  * Return: 0 success, anything else error
2829  */
2830 static int
_base_build_sg_scmd_ieee(struct MPT3SAS_ADAPTER * ioc,struct scsi_cmnd * scmd,u16 smid,struct _pcie_device * pcie_device)2831 _base_build_sg_scmd_ieee(struct MPT3SAS_ADAPTER *ioc,
2832 	struct scsi_cmnd *scmd, u16 smid, struct _pcie_device *pcie_device)
2833 {
2834 	Mpi25SCSIIORequest_t *mpi_request;
2835 	dma_addr_t chain_dma;
2836 	struct scatterlist *sg_scmd;
2837 	void *sg_local, *chain;
2838 	u32 chain_offset;
2839 	u32 chain_length;
2840 	int sges_left;
2841 	u32 sges_in_segment;
2842 	u8 simple_sgl_flags;
2843 	u8 simple_sgl_flags_last;
2844 	u8 chain_sgl_flags;
2845 	struct chain_tracker *chain_req;
2846 
2847 	mpi_request = mpt3sas_base_get_msg_frame(ioc, smid);
2848 
2849 	/* init scatter gather flags */
2850 	simple_sgl_flags = MPI2_IEEE_SGE_FLAGS_SIMPLE_ELEMENT |
2851 	    MPI2_IEEE_SGE_FLAGS_SYSTEM_ADDR;
2852 	simple_sgl_flags_last = simple_sgl_flags |
2853 	    MPI25_IEEE_SGE_FLAGS_END_OF_LIST;
2854 	chain_sgl_flags = MPI2_IEEE_SGE_FLAGS_CHAIN_ELEMENT |
2855 	    MPI2_IEEE_SGE_FLAGS_SYSTEM_ADDR;
2856 
2857 	/* Check if we need to build a native SG list. */
2858 	if ((pcie_device) && (_base_check_pcie_native_sgl(ioc, mpi_request,
2859 			smid, scmd, pcie_device) == 0)) {
2860 		/* We built a native SG list, just return. */
2861 		return 0;
2862 	}
2863 
2864 	sg_scmd = scsi_sglist(scmd);
2865 	sges_left = scsi_dma_map(scmd);
2866 	if (sges_left < 0)
2867 		return -ENOMEM;
2868 
2869 	sg_local = &mpi_request->SGL;
2870 	sges_in_segment = (ioc->request_sz -
2871 		   offsetof(Mpi25SCSIIORequest_t, SGL))/ioc->sge_size_ieee;
2872 	if (sges_left <= sges_in_segment)
2873 		goto fill_in_last_segment;
2874 
2875 	mpi_request->ChainOffset = (sges_in_segment - 1 /* chain element */) +
2876 	    (offsetof(Mpi25SCSIIORequest_t, SGL)/ioc->sge_size_ieee);
2877 
2878 	/* fill in main message segment when there is a chain following */
2879 	while (sges_in_segment > 1) {
2880 		_base_add_sg_single_ieee(sg_local, simple_sgl_flags, 0,
2881 		    sg_dma_len(sg_scmd), sg_dma_address(sg_scmd));
2882 		sg_scmd = sg_next(sg_scmd);
2883 		sg_local += ioc->sge_size_ieee;
2884 		sges_left--;
2885 		sges_in_segment--;
2886 	}
2887 
2888 	/* initializing the pointers */
2889 	chain_req = _base_get_chain_buffer_tracker(ioc, scmd);
2890 	if (!chain_req)
2891 		return -1;
2892 	chain = chain_req->chain_buffer;
2893 	chain_dma = chain_req->chain_buffer_dma;
2894 	do {
2895 		sges_in_segment = (sges_left <=
2896 		    ioc->max_sges_in_chain_message) ? sges_left :
2897 		    ioc->max_sges_in_chain_message;
2898 		chain_offset = (sges_left == sges_in_segment) ?
2899 		    0 : sges_in_segment;
2900 		chain_length = sges_in_segment * ioc->sge_size_ieee;
2901 		if (chain_offset)
2902 			chain_length += ioc->sge_size_ieee;
2903 		_base_add_sg_single_ieee(sg_local, chain_sgl_flags,
2904 		    chain_offset, chain_length, chain_dma);
2905 
2906 		sg_local = chain;
2907 		if (!chain_offset)
2908 			goto fill_in_last_segment;
2909 
2910 		/* fill in chain segments */
2911 		while (sges_in_segment) {
2912 			_base_add_sg_single_ieee(sg_local, simple_sgl_flags, 0,
2913 			    sg_dma_len(sg_scmd), sg_dma_address(sg_scmd));
2914 			sg_scmd = sg_next(sg_scmd);
2915 			sg_local += ioc->sge_size_ieee;
2916 			sges_left--;
2917 			sges_in_segment--;
2918 		}
2919 
2920 		chain_req = _base_get_chain_buffer_tracker(ioc, scmd);
2921 		if (!chain_req)
2922 			return -1;
2923 		chain = chain_req->chain_buffer;
2924 		chain_dma = chain_req->chain_buffer_dma;
2925 	} while (1);
2926 
2927 
2928  fill_in_last_segment:
2929 
2930 	/* fill the last segment */
2931 	while (sges_left > 0) {
2932 		if (sges_left == 1)
2933 			_base_add_sg_single_ieee(sg_local,
2934 			    simple_sgl_flags_last, 0, sg_dma_len(sg_scmd),
2935 			    sg_dma_address(sg_scmd));
2936 		else
2937 			_base_add_sg_single_ieee(sg_local, simple_sgl_flags, 0,
2938 			    sg_dma_len(sg_scmd), sg_dma_address(sg_scmd));
2939 		sg_scmd = sg_next(sg_scmd);
2940 		sg_local += ioc->sge_size_ieee;
2941 		sges_left--;
2942 	}
2943 
2944 	return 0;
2945 }
2946 
2947 /**
2948  * _base_build_sg_ieee - build generic sg for IEEE format
2949  * @ioc: per adapter object
2950  * @psge: virtual address for SGE
2951  * @data_out_dma: physical address for WRITES
2952  * @data_out_sz: data xfer size for WRITES
2953  * @data_in_dma: physical address for READS
2954  * @data_in_sz: data xfer size for READS
2955  */
2956 static void
_base_build_sg_ieee(struct MPT3SAS_ADAPTER * ioc,void * psge,dma_addr_t data_out_dma,size_t data_out_sz,dma_addr_t data_in_dma,size_t data_in_sz)2957 _base_build_sg_ieee(struct MPT3SAS_ADAPTER *ioc, void *psge,
2958 	dma_addr_t data_out_dma, size_t data_out_sz, dma_addr_t data_in_dma,
2959 	size_t data_in_sz)
2960 {
2961 	u8 sgl_flags;
2962 
2963 	if (!data_out_sz && !data_in_sz) {
2964 		_base_build_zero_len_sge_ieee(ioc, psge);
2965 		return;
2966 	}
2967 
2968 	if (data_out_sz && data_in_sz) {
2969 		/* WRITE sgel first */
2970 		sgl_flags = MPI2_IEEE_SGE_FLAGS_SIMPLE_ELEMENT |
2971 		    MPI2_IEEE_SGE_FLAGS_SYSTEM_ADDR;
2972 		_base_add_sg_single_ieee(psge, sgl_flags, 0, data_out_sz,
2973 		    data_out_dma);
2974 
2975 		/* incr sgel */
2976 		psge += ioc->sge_size_ieee;
2977 
2978 		/* READ sgel last */
2979 		sgl_flags |= MPI25_IEEE_SGE_FLAGS_END_OF_LIST;
2980 		_base_add_sg_single_ieee(psge, sgl_flags, 0, data_in_sz,
2981 		    data_in_dma);
2982 	} else if (data_out_sz) /* WRITE */ {
2983 		sgl_flags = MPI2_IEEE_SGE_FLAGS_SIMPLE_ELEMENT |
2984 		    MPI25_IEEE_SGE_FLAGS_END_OF_LIST |
2985 		    MPI2_IEEE_SGE_FLAGS_SYSTEM_ADDR;
2986 		_base_add_sg_single_ieee(psge, sgl_flags, 0, data_out_sz,
2987 		    data_out_dma);
2988 	} else if (data_in_sz) /* READ */ {
2989 		sgl_flags = MPI2_IEEE_SGE_FLAGS_SIMPLE_ELEMENT |
2990 		    MPI25_IEEE_SGE_FLAGS_END_OF_LIST |
2991 		    MPI2_IEEE_SGE_FLAGS_SYSTEM_ADDR;
2992 		_base_add_sg_single_ieee(psge, sgl_flags, 0, data_in_sz,
2993 		    data_in_dma);
2994 	}
2995 }
2996 
2997 #define convert_to_kb(x) ((x) << (PAGE_SHIFT - 10))
2998 
2999 /**
3000  * _base_config_dma_addressing - set dma addressing
3001  * @ioc: per adapter object
3002  * @pdev: PCI device struct
3003  *
3004  * Return: 0 for success, non-zero for failure.
3005  */
3006 static int
_base_config_dma_addressing(struct MPT3SAS_ADAPTER * ioc,struct pci_dev * pdev)3007 _base_config_dma_addressing(struct MPT3SAS_ADAPTER *ioc, struct pci_dev *pdev)
3008 {
3009 	struct sysinfo s;
3010 	u64 coherent_dma_mask, dma_mask;
3011 
3012 	if (ioc->is_mcpu_endpoint || sizeof(dma_addr_t) == 4) {
3013 		ioc->dma_mask = 32;
3014 		coherent_dma_mask = dma_mask = DMA_BIT_MASK(32);
3015 	/* Set 63 bit DMA mask for all SAS3 and SAS35 controllers */
3016 	} else if (ioc->hba_mpi_version_belonged > MPI2_VERSION) {
3017 		ioc->dma_mask = 63;
3018 		coherent_dma_mask = dma_mask = DMA_BIT_MASK(63);
3019 	} else {
3020 		ioc->dma_mask = 64;
3021 		coherent_dma_mask = dma_mask = DMA_BIT_MASK(64);
3022 	}
3023 
3024 	if (ioc->use_32bit_dma)
3025 		coherent_dma_mask = DMA_BIT_MASK(32);
3026 
3027 	if (dma_set_mask(&pdev->dev, dma_mask) ||
3028 	    dma_set_coherent_mask(&pdev->dev, coherent_dma_mask))
3029 		return -ENODEV;
3030 
3031 	if (ioc->dma_mask > 32) {
3032 		ioc->base_add_sg_single = &_base_add_sg_single_64;
3033 		ioc->sge_size = sizeof(Mpi2SGESimple64_t);
3034 	} else {
3035 		ioc->base_add_sg_single = &_base_add_sg_single_32;
3036 		ioc->sge_size = sizeof(Mpi2SGESimple32_t);
3037 	}
3038 
3039 	si_meminfo(&s);
3040 	ioc_info(ioc, "%d BIT PCI BUS DMA ADDRESSING SUPPORTED, total mem (%ld kB)\n",
3041 		ioc->dma_mask, convert_to_kb(s.totalram));
3042 
3043 	return 0;
3044 }
3045 
3046 /**
3047  * _base_check_enable_msix - checks MSIX capabable.
3048  * @ioc: per adapter object
3049  *
3050  * Check to see if card is capable of MSIX, and set number
3051  * of available msix vectors
3052  */
3053 static int
_base_check_enable_msix(struct MPT3SAS_ADAPTER * ioc)3054 _base_check_enable_msix(struct MPT3SAS_ADAPTER *ioc)
3055 {
3056 	int base;
3057 	u16 message_control;
3058 
3059 	/* Check whether controller SAS2008 B0 controller,
3060 	 * if it is SAS2008 B0 controller use IO-APIC instead of MSIX
3061 	 */
3062 	if (ioc->pdev->device == MPI2_MFGPAGE_DEVID_SAS2008 &&
3063 	    ioc->pdev->revision == SAS2_PCI_DEVICE_B0_REVISION) {
3064 		return -EINVAL;
3065 	}
3066 
3067 	base = pci_find_capability(ioc->pdev, PCI_CAP_ID_MSIX);
3068 	if (!base) {
3069 		dfailprintk(ioc, ioc_info(ioc, "msix not supported\n"));
3070 		return -EINVAL;
3071 	}
3072 
3073 	/* get msix vector count */
3074 	/* NUMA_IO not supported for older controllers */
3075 	if (ioc->pdev->device == MPI2_MFGPAGE_DEVID_SAS2004 ||
3076 	    ioc->pdev->device == MPI2_MFGPAGE_DEVID_SAS2008 ||
3077 	    ioc->pdev->device == MPI2_MFGPAGE_DEVID_SAS2108_1 ||
3078 	    ioc->pdev->device == MPI2_MFGPAGE_DEVID_SAS2108_2 ||
3079 	    ioc->pdev->device == MPI2_MFGPAGE_DEVID_SAS2108_3 ||
3080 	    ioc->pdev->device == MPI2_MFGPAGE_DEVID_SAS2116_1 ||
3081 	    ioc->pdev->device == MPI2_MFGPAGE_DEVID_SAS2116_2)
3082 		ioc->msix_vector_count = 1;
3083 	else {
3084 		pci_read_config_word(ioc->pdev, base + 2, &message_control);
3085 		ioc->msix_vector_count = (message_control & 0x3FF) + 1;
3086 	}
3087 	dinitprintk(ioc, ioc_info(ioc, "msix is supported, vector_count(%d)\n",
3088 				  ioc->msix_vector_count));
3089 	return 0;
3090 }
3091 
3092 /**
3093  * mpt3sas_base_free_irq - free irq
3094  * @ioc: per adapter object
3095  *
3096  * Freeing respective reply_queue from the list.
3097  */
3098 void
mpt3sas_base_free_irq(struct MPT3SAS_ADAPTER * ioc)3099 mpt3sas_base_free_irq(struct MPT3SAS_ADAPTER *ioc)
3100 {
3101 	struct adapter_reply_queue *reply_q, *next;
3102 
3103 	if (list_empty(&ioc->reply_queue_list))
3104 		return;
3105 
3106 	list_for_each_entry_safe(reply_q, next, &ioc->reply_queue_list, list) {
3107 		list_del(&reply_q->list);
3108 		if (reply_q->is_iouring_poll_q) {
3109 			kfree(reply_q);
3110 			continue;
3111 		}
3112 
3113 		if (ioc->smp_affinity_enable)
3114 			irq_set_affinity_hint(pci_irq_vector(ioc->pdev,
3115 			    reply_q->msix_index), NULL);
3116 		free_irq(pci_irq_vector(ioc->pdev, reply_q->msix_index),
3117 			 reply_q);
3118 		kfree(reply_q);
3119 	}
3120 }
3121 
3122 /**
3123  * _base_request_irq - request irq
3124  * @ioc: per adapter object
3125  * @index: msix index into vector table
3126  *
3127  * Inserting respective reply_queue into the list.
3128  */
3129 static int
_base_request_irq(struct MPT3SAS_ADAPTER * ioc,u8 index)3130 _base_request_irq(struct MPT3SAS_ADAPTER *ioc, u8 index)
3131 {
3132 	struct pci_dev *pdev = ioc->pdev;
3133 	struct adapter_reply_queue *reply_q;
3134 	int r, qid;
3135 
3136 	reply_q =  kzalloc(sizeof(struct adapter_reply_queue), GFP_KERNEL);
3137 	if (!reply_q) {
3138 		ioc_err(ioc, "unable to allocate memory %zu!\n",
3139 			sizeof(struct adapter_reply_queue));
3140 		return -ENOMEM;
3141 	}
3142 	reply_q->ioc = ioc;
3143 	reply_q->msix_index = index;
3144 
3145 	atomic_set(&reply_q->busy, 0);
3146 
3147 	if (index >= ioc->iopoll_q_start_index) {
3148 		qid = index - ioc->iopoll_q_start_index;
3149 		snprintf(reply_q->name, MPT_NAME_LENGTH, "%s%d-mq-poll%d",
3150 		    ioc->driver_name, ioc->id, qid);
3151 		reply_q->is_iouring_poll_q = 1;
3152 		ioc->io_uring_poll_queues[qid].reply_q = reply_q;
3153 		goto out;
3154 	}
3155 
3156 
3157 	if (ioc->msix_enable)
3158 		snprintf(reply_q->name, MPT_NAME_LENGTH, "%s%d-msix%d",
3159 		    ioc->driver_name, ioc->id, index);
3160 	else
3161 		snprintf(reply_q->name, MPT_NAME_LENGTH, "%s%d",
3162 		    ioc->driver_name, ioc->id);
3163 	r = request_irq(pci_irq_vector(pdev, index), _base_interrupt,
3164 			IRQF_SHARED, reply_q->name, reply_q);
3165 	if (r) {
3166 		pr_err("%s: unable to allocate interrupt %d!\n",
3167 		       reply_q->name, pci_irq_vector(pdev, index));
3168 		kfree(reply_q);
3169 		return -EBUSY;
3170 	}
3171 out:
3172 	INIT_LIST_HEAD(&reply_q->list);
3173 	list_add_tail(&reply_q->list, &ioc->reply_queue_list);
3174 	return 0;
3175 }
3176 
3177 /**
3178  * _base_assign_reply_queues - assigning msix index for each cpu
3179  * @ioc: per adapter object
3180  *
3181  * The enduser would need to set the affinity via /proc/irq/#/smp_affinity
3182  *
3183  * It would nice if we could call irq_set_affinity, however it is not
3184  * an exported symbol
3185  */
3186 static void
_base_assign_reply_queues(struct MPT3SAS_ADAPTER * ioc)3187 _base_assign_reply_queues(struct MPT3SAS_ADAPTER *ioc)
3188 {
3189 	unsigned int cpu, nr_cpus, nr_msix, index = 0;
3190 	struct adapter_reply_queue *reply_q;
3191 	int local_numa_node;
3192 	int iopoll_q_count = ioc->reply_queue_count -
3193 	    ioc->iopoll_q_start_index;
3194 
3195 	if (!_base_is_controller_msix_enabled(ioc))
3196 		return;
3197 
3198 	if (ioc->msix_load_balance)
3199 		return;
3200 
3201 	memset(ioc->cpu_msix_table, 0, ioc->cpu_msix_table_sz);
3202 
3203 	nr_cpus = num_online_cpus();
3204 	nr_msix = ioc->reply_queue_count = min(ioc->reply_queue_count,
3205 					       ioc->facts.MaxMSIxVectors);
3206 	if (!nr_msix)
3207 		return;
3208 
3209 	if (ioc->smp_affinity_enable) {
3210 
3211 		/*
3212 		 * set irq affinity to local numa node for those irqs
3213 		 * corresponding to high iops queues.
3214 		 */
3215 		if (ioc->high_iops_queues) {
3216 			local_numa_node = dev_to_node(&ioc->pdev->dev);
3217 			for (index = 0; index < ioc->high_iops_queues;
3218 			    index++) {
3219 				irq_set_affinity_hint(pci_irq_vector(ioc->pdev,
3220 				    index), cpumask_of_node(local_numa_node));
3221 			}
3222 		}
3223 
3224 		list_for_each_entry(reply_q, &ioc->reply_queue_list, list) {
3225 			const cpumask_t *mask;
3226 
3227 			if (reply_q->msix_index < ioc->high_iops_queues ||
3228 			    reply_q->msix_index >= ioc->iopoll_q_start_index)
3229 				continue;
3230 
3231 			mask = pci_irq_get_affinity(ioc->pdev,
3232 			    reply_q->msix_index);
3233 			if (!mask) {
3234 				ioc_warn(ioc, "no affinity for msi %x\n",
3235 					 reply_q->msix_index);
3236 				goto fall_back;
3237 			}
3238 
3239 			for_each_cpu_and(cpu, mask, cpu_online_mask) {
3240 				if (cpu >= ioc->cpu_msix_table_sz)
3241 					break;
3242 				ioc->cpu_msix_table[cpu] = reply_q->msix_index;
3243 			}
3244 		}
3245 		return;
3246 	}
3247 
3248 fall_back:
3249 	cpu = cpumask_first(cpu_online_mask);
3250 	nr_msix -= (ioc->high_iops_queues - iopoll_q_count);
3251 	index = 0;
3252 
3253 	list_for_each_entry(reply_q, &ioc->reply_queue_list, list) {
3254 		unsigned int i, group = nr_cpus / nr_msix;
3255 
3256 		if (reply_q->msix_index < ioc->high_iops_queues ||
3257 		    reply_q->msix_index >= ioc->iopoll_q_start_index)
3258 			continue;
3259 
3260 		if (cpu >= nr_cpus)
3261 			break;
3262 
3263 		if (index < nr_cpus % nr_msix)
3264 			group++;
3265 
3266 		for (i = 0 ; i < group ; i++) {
3267 			ioc->cpu_msix_table[cpu] = reply_q->msix_index;
3268 			cpu = cpumask_next(cpu, cpu_online_mask);
3269 		}
3270 		index++;
3271 	}
3272 }
3273 
3274 /**
3275  * _base_check_and_enable_high_iops_queues - enable high iops mode
3276  * @ioc: per adapter object
3277  * @hba_msix_vector_count: msix vectors supported by HBA
3278  *
3279  * Enable high iops queues only if
3280  *  - HBA is a SEA/AERO controller and
3281  *  - MSI-Xs vector supported by the HBA is 128 and
3282  *  - total CPU count in the system >=16 and
3283  *  - loaded driver with default max_msix_vectors module parameter and
3284  *  - system booted in non kdump mode
3285  *
3286  * Return: nothing.
3287  */
3288 static void
_base_check_and_enable_high_iops_queues(struct MPT3SAS_ADAPTER * ioc,int hba_msix_vector_count)3289 _base_check_and_enable_high_iops_queues(struct MPT3SAS_ADAPTER *ioc,
3290 		int hba_msix_vector_count)
3291 {
3292 	u16 lnksta, speed;
3293 
3294 	/*
3295 	 * Disable high iops queues if io uring poll queues are enabled.
3296 	 */
3297 	if (perf_mode == MPT_PERF_MODE_IOPS ||
3298 	    perf_mode == MPT_PERF_MODE_LATENCY ||
3299 	    ioc->io_uring_poll_queues) {
3300 		ioc->high_iops_queues = 0;
3301 		return;
3302 	}
3303 
3304 	if (perf_mode == MPT_PERF_MODE_DEFAULT) {
3305 
3306 		pcie_capability_read_word(ioc->pdev, PCI_EXP_LNKSTA, &lnksta);
3307 		speed = lnksta & PCI_EXP_LNKSTA_CLS;
3308 
3309 		if (speed < 0x4) {
3310 			ioc->high_iops_queues = 0;
3311 			return;
3312 		}
3313 	}
3314 
3315 	if (!reset_devices && ioc->is_aero_ioc &&
3316 	    hba_msix_vector_count == MPT3SAS_GEN35_MAX_MSIX_QUEUES &&
3317 	    num_online_cpus() >= MPT3SAS_HIGH_IOPS_REPLY_QUEUES &&
3318 	    max_msix_vectors == -1)
3319 		ioc->high_iops_queues = MPT3SAS_HIGH_IOPS_REPLY_QUEUES;
3320 	else
3321 		ioc->high_iops_queues = 0;
3322 }
3323 
3324 /**
3325  * mpt3sas_base_disable_msix - disables msix
3326  * @ioc: per adapter object
3327  *
3328  */
3329 void
mpt3sas_base_disable_msix(struct MPT3SAS_ADAPTER * ioc)3330 mpt3sas_base_disable_msix(struct MPT3SAS_ADAPTER *ioc)
3331 {
3332 	if (!ioc->msix_enable)
3333 		return;
3334 	pci_free_irq_vectors(ioc->pdev);
3335 	ioc->msix_enable = 0;
3336 	kfree(ioc->io_uring_poll_queues);
3337 }
3338 
3339 /**
3340  * _base_alloc_irq_vectors - allocate msix vectors
3341  * @ioc: per adapter object
3342  *
3343  */
3344 static int
_base_alloc_irq_vectors(struct MPT3SAS_ADAPTER * ioc)3345 _base_alloc_irq_vectors(struct MPT3SAS_ADAPTER *ioc)
3346 {
3347 	int i, irq_flags = PCI_IRQ_MSIX;
3348 	struct irq_affinity desc = { .pre_vectors = ioc->high_iops_queues };
3349 	struct irq_affinity *descp = &desc;
3350 	/*
3351 	 * Don't allocate msix vectors for poll_queues.
3352 	 * msix_vectors is always within a range of FW supported reply queue.
3353 	 */
3354 	int nr_msix_vectors = ioc->iopoll_q_start_index;
3355 
3356 
3357 	if (ioc->smp_affinity_enable)
3358 		irq_flags |= PCI_IRQ_AFFINITY | PCI_IRQ_ALL_TYPES;
3359 	else
3360 		descp = NULL;
3361 
3362 	ioc_info(ioc, " %d %d %d\n", ioc->high_iops_queues,
3363 	    ioc->reply_queue_count, nr_msix_vectors);
3364 
3365 	i = pci_alloc_irq_vectors_affinity(ioc->pdev,
3366 	    ioc->high_iops_queues,
3367 	    nr_msix_vectors, irq_flags, descp);
3368 
3369 	return i;
3370 }
3371 
3372 /**
3373  * _base_enable_msix - enables msix, failback to io_apic
3374  * @ioc: per adapter object
3375  *
3376  */
3377 static int
_base_enable_msix(struct MPT3SAS_ADAPTER * ioc)3378 _base_enable_msix(struct MPT3SAS_ADAPTER *ioc)
3379 {
3380 	int r;
3381 	int i, local_max_msix_vectors;
3382 	u8 try_msix = 0;
3383 	int iopoll_q_count = 0;
3384 
3385 	ioc->msix_load_balance = false;
3386 
3387 	if (msix_disable == -1 || msix_disable == 0)
3388 		try_msix = 1;
3389 
3390 	if (!try_msix)
3391 		goto try_ioapic;
3392 
3393 	if (_base_check_enable_msix(ioc) != 0)
3394 		goto try_ioapic;
3395 
3396 	ioc_info(ioc, "MSI-X vectors supported: %d\n", ioc->msix_vector_count);
3397 	pr_info("\t no of cores: %d, max_msix_vectors: %d\n",
3398 		ioc->cpu_count, max_msix_vectors);
3399 
3400 	ioc->reply_queue_count =
3401 		min_t(int, ioc->cpu_count, ioc->msix_vector_count);
3402 
3403 	if (!ioc->rdpq_array_enable && max_msix_vectors == -1)
3404 		local_max_msix_vectors = (reset_devices) ? 1 : 8;
3405 	else
3406 		local_max_msix_vectors = max_msix_vectors;
3407 
3408 	if (local_max_msix_vectors == 0)
3409 		goto try_ioapic;
3410 
3411 	/*
3412 	 * Enable msix_load_balance only if combined reply queue mode is
3413 	 * disabled on SAS3 & above generation HBA devices.
3414 	 */
3415 	if (!ioc->combined_reply_queue &&
3416 	    ioc->hba_mpi_version_belonged != MPI2_VERSION) {
3417 		ioc_info(ioc,
3418 		    "combined ReplyQueue is off, Enabling msix load balance\n");
3419 		ioc->msix_load_balance = true;
3420 	}
3421 
3422 	/*
3423 	 * smp affinity setting is not need when msix load balance
3424 	 * is enabled.
3425 	 */
3426 	if (ioc->msix_load_balance)
3427 		ioc->smp_affinity_enable = 0;
3428 
3429 	if (!ioc->smp_affinity_enable || ioc->reply_queue_count <= 1)
3430 		ioc->shost->host_tagset = 0;
3431 
3432 	/*
3433 	 * Enable io uring poll queues only if host_tagset is enabled.
3434 	 */
3435 	if (ioc->shost->host_tagset)
3436 		iopoll_q_count = poll_queues;
3437 
3438 	if (iopoll_q_count) {
3439 		ioc->io_uring_poll_queues = kcalloc(iopoll_q_count,
3440 		    sizeof(struct io_uring_poll_queue), GFP_KERNEL);
3441 		if (!ioc->io_uring_poll_queues)
3442 			iopoll_q_count = 0;
3443 	}
3444 
3445 	if (ioc->is_aero_ioc)
3446 		_base_check_and_enable_high_iops_queues(ioc,
3447 		    ioc->msix_vector_count);
3448 
3449 	/*
3450 	 * Add high iops queues count to reply queue count if high iops queues
3451 	 * are enabled.
3452 	 */
3453 	ioc->reply_queue_count = min_t(int,
3454 	    ioc->reply_queue_count + ioc->high_iops_queues,
3455 	    ioc->msix_vector_count);
3456 
3457 	/*
3458 	 * Adjust the reply queue count incase reply queue count
3459 	 * exceeds the user provided MSIx vectors count.
3460 	 */
3461 	if (local_max_msix_vectors > 0)
3462 		ioc->reply_queue_count = min_t(int, local_max_msix_vectors,
3463 		    ioc->reply_queue_count);
3464 	/*
3465 	 * Add io uring poll queues count to reply queues count
3466 	 * if io uring is enabled in driver.
3467 	 */
3468 	if (iopoll_q_count) {
3469 		if (ioc->reply_queue_count < (iopoll_q_count + MPT3_MIN_IRQS))
3470 			iopoll_q_count = 0;
3471 		ioc->reply_queue_count = min_t(int,
3472 		    ioc->reply_queue_count + iopoll_q_count,
3473 		    ioc->msix_vector_count);
3474 	}
3475 
3476 	/*
3477 	 * Starting index of io uring poll queues in reply queue list.
3478 	 */
3479 	ioc->iopoll_q_start_index =
3480 	    ioc->reply_queue_count - iopoll_q_count;
3481 
3482 	r = _base_alloc_irq_vectors(ioc);
3483 	if (r < 0) {
3484 		ioc_info(ioc, "pci_alloc_irq_vectors failed (r=%d) !!!\n", r);
3485 		goto try_ioapic;
3486 	}
3487 
3488 	/*
3489 	 * Adjust the reply queue count if the allocated
3490 	 * MSIx vectors is less then the requested number
3491 	 * of MSIx vectors.
3492 	 */
3493 	if (r < ioc->iopoll_q_start_index) {
3494 		ioc->reply_queue_count = r + iopoll_q_count;
3495 		ioc->iopoll_q_start_index =
3496 		    ioc->reply_queue_count - iopoll_q_count;
3497 	}
3498 
3499 	ioc->msix_enable = 1;
3500 	for (i = 0; i < ioc->reply_queue_count; i++) {
3501 		r = _base_request_irq(ioc, i);
3502 		if (r) {
3503 			mpt3sas_base_free_irq(ioc);
3504 			mpt3sas_base_disable_msix(ioc);
3505 			goto try_ioapic;
3506 		}
3507 	}
3508 
3509 	ioc_info(ioc, "High IOPs queues : %s\n",
3510 			ioc->high_iops_queues ? "enabled" : "disabled");
3511 
3512 	return 0;
3513 
3514 /* failback to io_apic interrupt routing */
3515  try_ioapic:
3516 	ioc->high_iops_queues = 0;
3517 	ioc_info(ioc, "High IOPs queues : disabled\n");
3518 	ioc->reply_queue_count = 1;
3519 	ioc->iopoll_q_start_index = ioc->reply_queue_count - 0;
3520 	r = pci_alloc_irq_vectors(ioc->pdev, 1, 1, PCI_IRQ_LEGACY);
3521 	if (r < 0) {
3522 		dfailprintk(ioc,
3523 			    ioc_info(ioc, "pci_alloc_irq_vector(legacy) failed (r=%d) !!!\n",
3524 				     r));
3525 	} else
3526 		r = _base_request_irq(ioc, 0);
3527 
3528 	return r;
3529 }
3530 
3531 /**
3532  * mpt3sas_base_unmap_resources - free controller resources
3533  * @ioc: per adapter object
3534  */
3535 static void
mpt3sas_base_unmap_resources(struct MPT3SAS_ADAPTER * ioc)3536 mpt3sas_base_unmap_resources(struct MPT3SAS_ADAPTER *ioc)
3537 {
3538 	struct pci_dev *pdev = ioc->pdev;
3539 
3540 	dexitprintk(ioc, ioc_info(ioc, "%s\n", __func__));
3541 
3542 	mpt3sas_base_free_irq(ioc);
3543 	mpt3sas_base_disable_msix(ioc);
3544 
3545 	kfree(ioc->replyPostRegisterIndex);
3546 	ioc->replyPostRegisterIndex = NULL;
3547 
3548 
3549 	if (ioc->chip_phys) {
3550 		iounmap(ioc->chip);
3551 		ioc->chip_phys = 0;
3552 	}
3553 
3554 	if (pci_is_enabled(pdev)) {
3555 		pci_release_selected_regions(ioc->pdev, ioc->bars);
3556 		pci_disable_pcie_error_reporting(pdev);
3557 		pci_disable_device(pdev);
3558 	}
3559 }
3560 
3561 static int
3562 _base_diag_reset(struct MPT3SAS_ADAPTER *ioc);
3563 
3564 /**
3565  * mpt3sas_base_check_for_fault_and_issue_reset - check if IOC is in fault state
3566  *     and if it is in fault state then issue diag reset.
3567  * @ioc: per adapter object
3568  *
3569  * Return: 0 for success, non-zero for failure.
3570  */
3571 int
mpt3sas_base_check_for_fault_and_issue_reset(struct MPT3SAS_ADAPTER * ioc)3572 mpt3sas_base_check_for_fault_and_issue_reset(struct MPT3SAS_ADAPTER *ioc)
3573 {
3574 	u32 ioc_state;
3575 	int rc = -EFAULT;
3576 
3577 	dinitprintk(ioc, pr_info("%s\n", __func__));
3578 	if (ioc->pci_error_recovery)
3579 		return 0;
3580 	ioc_state = mpt3sas_base_get_iocstate(ioc, 0);
3581 	dhsprintk(ioc, pr_info("%s: ioc_state(0x%08x)\n", __func__, ioc_state));
3582 
3583 	if ((ioc_state & MPI2_IOC_STATE_MASK) == MPI2_IOC_STATE_FAULT) {
3584 		mpt3sas_print_fault_code(ioc, ioc_state &
3585 		    MPI2_DOORBELL_DATA_MASK);
3586 		mpt3sas_base_mask_interrupts(ioc);
3587 		rc = _base_diag_reset(ioc);
3588 	} else if ((ioc_state & MPI2_IOC_STATE_MASK) ==
3589 	    MPI2_IOC_STATE_COREDUMP) {
3590 		mpt3sas_print_coredump_info(ioc, ioc_state &
3591 		     MPI2_DOORBELL_DATA_MASK);
3592 		mpt3sas_base_wait_for_coredump_completion(ioc, __func__);
3593 		mpt3sas_base_mask_interrupts(ioc);
3594 		rc = _base_diag_reset(ioc);
3595 	}
3596 
3597 	return rc;
3598 }
3599 
3600 /**
3601  * mpt3sas_base_map_resources - map in controller resources (io/irq/memap)
3602  * @ioc: per adapter object
3603  *
3604  * Return: 0 for success, non-zero for failure.
3605  */
3606 int
mpt3sas_base_map_resources(struct MPT3SAS_ADAPTER * ioc)3607 mpt3sas_base_map_resources(struct MPT3SAS_ADAPTER *ioc)
3608 {
3609 	struct pci_dev *pdev = ioc->pdev;
3610 	u32 memap_sz;
3611 	u32 pio_sz;
3612 	int i, r = 0, rc;
3613 	u64 pio_chip = 0;
3614 	phys_addr_t chip_phys = 0;
3615 	struct adapter_reply_queue *reply_q;
3616 	int iopoll_q_count = 0;
3617 
3618 	dinitprintk(ioc, ioc_info(ioc, "%s\n", __func__));
3619 
3620 	ioc->bars = pci_select_bars(pdev, IORESOURCE_MEM);
3621 	if (pci_enable_device_mem(pdev)) {
3622 		ioc_warn(ioc, "pci_enable_device_mem: failed\n");
3623 		ioc->bars = 0;
3624 		return -ENODEV;
3625 	}
3626 
3627 
3628 	if (pci_request_selected_regions(pdev, ioc->bars,
3629 	    ioc->driver_name)) {
3630 		ioc_warn(ioc, "pci_request_selected_regions: failed\n");
3631 		ioc->bars = 0;
3632 		r = -ENODEV;
3633 		goto out_fail;
3634 	}
3635 
3636 /* AER (Advanced Error Reporting) hooks */
3637 	pci_enable_pcie_error_reporting(pdev);
3638 
3639 	pci_set_master(pdev);
3640 
3641 
3642 	if (_base_config_dma_addressing(ioc, pdev) != 0) {
3643 		ioc_warn(ioc, "no suitable DMA mask for %s\n", pci_name(pdev));
3644 		r = -ENODEV;
3645 		goto out_fail;
3646 	}
3647 
3648 	for (i = 0, memap_sz = 0, pio_sz = 0; (i < DEVICE_COUNT_RESOURCE) &&
3649 	     (!memap_sz || !pio_sz); i++) {
3650 		if (pci_resource_flags(pdev, i) & IORESOURCE_IO) {
3651 			if (pio_sz)
3652 				continue;
3653 			pio_chip = (u64)pci_resource_start(pdev, i);
3654 			pio_sz = pci_resource_len(pdev, i);
3655 		} else if (pci_resource_flags(pdev, i) & IORESOURCE_MEM) {
3656 			if (memap_sz)
3657 				continue;
3658 			ioc->chip_phys = pci_resource_start(pdev, i);
3659 			chip_phys = ioc->chip_phys;
3660 			memap_sz = pci_resource_len(pdev, i);
3661 			ioc->chip = ioremap(ioc->chip_phys, memap_sz);
3662 		}
3663 	}
3664 
3665 	if (ioc->chip == NULL) {
3666 		ioc_err(ioc,
3667 		    "unable to map adapter memory! or resource not found\n");
3668 		r = -EINVAL;
3669 		goto out_fail;
3670 	}
3671 
3672 	mpt3sas_base_mask_interrupts(ioc);
3673 
3674 	r = _base_get_ioc_facts(ioc);
3675 	if (r) {
3676 		rc = mpt3sas_base_check_for_fault_and_issue_reset(ioc);
3677 		if (rc || (_base_get_ioc_facts(ioc)))
3678 			goto out_fail;
3679 	}
3680 
3681 	if (!ioc->rdpq_array_enable_assigned) {
3682 		ioc->rdpq_array_enable = ioc->rdpq_array_capable;
3683 		ioc->rdpq_array_enable_assigned = 1;
3684 	}
3685 
3686 	r = _base_enable_msix(ioc);
3687 	if (r)
3688 		goto out_fail;
3689 
3690 	iopoll_q_count = ioc->reply_queue_count - ioc->iopoll_q_start_index;
3691 	for (i = 0; i < iopoll_q_count; i++) {
3692 		atomic_set(&ioc->io_uring_poll_queues[i].busy, 0);
3693 		atomic_set(&ioc->io_uring_poll_queues[i].pause, 0);
3694 	}
3695 
3696 	if (!ioc->is_driver_loading)
3697 		_base_init_irqpolls(ioc);
3698 	/* Use the Combined reply queue feature only for SAS3 C0 & higher
3699 	 * revision HBAs and also only when reply queue count is greater than 8
3700 	 */
3701 	if (ioc->combined_reply_queue) {
3702 		/* Determine the Supplemental Reply Post Host Index Registers
3703 		 * Addresse. Supplemental Reply Post Host Index Registers
3704 		 * starts at offset MPI25_SUP_REPLY_POST_HOST_INDEX_OFFSET and
3705 		 * each register is at offset bytes of
3706 		 * MPT3_SUP_REPLY_POST_HOST_INDEX_REG_OFFSET from previous one.
3707 		 */
3708 		ioc->replyPostRegisterIndex = kcalloc(
3709 		     ioc->combined_reply_index_count,
3710 		     sizeof(resource_size_t *), GFP_KERNEL);
3711 		if (!ioc->replyPostRegisterIndex) {
3712 			ioc_err(ioc,
3713 			    "allocation for replyPostRegisterIndex failed!\n");
3714 			r = -ENOMEM;
3715 			goto out_fail;
3716 		}
3717 
3718 		for (i = 0; i < ioc->combined_reply_index_count; i++) {
3719 			ioc->replyPostRegisterIndex[i] = (resource_size_t *)
3720 			     ((u8 __force *)&ioc->chip->Doorbell +
3721 			     MPI25_SUP_REPLY_POST_HOST_INDEX_OFFSET +
3722 			     (i * MPT3_SUP_REPLY_POST_HOST_INDEX_REG_OFFSET));
3723 		}
3724 	}
3725 
3726 	if (ioc->is_warpdrive) {
3727 		ioc->reply_post_host_index[0] = (resource_size_t __iomem *)
3728 		    &ioc->chip->ReplyPostHostIndex;
3729 
3730 		for (i = 1; i < ioc->cpu_msix_table_sz; i++)
3731 			ioc->reply_post_host_index[i] =
3732 			(resource_size_t __iomem *)
3733 			((u8 __iomem *)&ioc->chip->Doorbell + (0x4000 + ((i - 1)
3734 			* 4)));
3735 	}
3736 
3737 	list_for_each_entry(reply_q, &ioc->reply_queue_list, list) {
3738 		if (reply_q->msix_index >= ioc->iopoll_q_start_index) {
3739 			pr_info("%s: enabled: index: %d\n",
3740 			    reply_q->name, reply_q->msix_index);
3741 			continue;
3742 		}
3743 
3744 		pr_info("%s: %s enabled: IRQ %d\n",
3745 			reply_q->name,
3746 			ioc->msix_enable ? "PCI-MSI-X" : "IO-APIC",
3747 			pci_irq_vector(ioc->pdev, reply_q->msix_index));
3748 	}
3749 
3750 	ioc_info(ioc, "iomem(%pap), mapped(0x%p), size(%d)\n",
3751 		 &chip_phys, ioc->chip, memap_sz);
3752 	ioc_info(ioc, "ioport(0x%016llx), size(%d)\n",
3753 		 (unsigned long long)pio_chip, pio_sz);
3754 
3755 	/* Save PCI configuration state for recovery from PCI AER/EEH errors */
3756 	pci_save_state(pdev);
3757 	return 0;
3758 
3759  out_fail:
3760 	mpt3sas_base_unmap_resources(ioc);
3761 	return r;
3762 }
3763 
3764 /**
3765  * mpt3sas_base_get_msg_frame - obtain request mf pointer
3766  * @ioc: per adapter object
3767  * @smid: system request message index(smid zero is invalid)
3768  *
3769  * Return: virt pointer to message frame.
3770  */
3771 void *
mpt3sas_base_get_msg_frame(struct MPT3SAS_ADAPTER * ioc,u16 smid)3772 mpt3sas_base_get_msg_frame(struct MPT3SAS_ADAPTER *ioc, u16 smid)
3773 {
3774 	return (void *)(ioc->request + (smid * ioc->request_sz));
3775 }
3776 
3777 /**
3778  * mpt3sas_base_get_sense_buffer - obtain a sense buffer virt addr
3779  * @ioc: per adapter object
3780  * @smid: system request message index
3781  *
3782  * Return: virt pointer to sense buffer.
3783  */
3784 void *
mpt3sas_base_get_sense_buffer(struct MPT3SAS_ADAPTER * ioc,u16 smid)3785 mpt3sas_base_get_sense_buffer(struct MPT3SAS_ADAPTER *ioc, u16 smid)
3786 {
3787 	return (void *)(ioc->sense + ((smid - 1) * SCSI_SENSE_BUFFERSIZE));
3788 }
3789 
3790 /**
3791  * mpt3sas_base_get_sense_buffer_dma - obtain a sense buffer dma addr
3792  * @ioc: per adapter object
3793  * @smid: system request message index
3794  *
3795  * Return: phys pointer to the low 32bit address of the sense buffer.
3796  */
3797 __le32
mpt3sas_base_get_sense_buffer_dma(struct MPT3SAS_ADAPTER * ioc,u16 smid)3798 mpt3sas_base_get_sense_buffer_dma(struct MPT3SAS_ADAPTER *ioc, u16 smid)
3799 {
3800 	return cpu_to_le32(ioc->sense_dma + ((smid - 1) *
3801 	    SCSI_SENSE_BUFFERSIZE));
3802 }
3803 
3804 /**
3805  * mpt3sas_base_get_pcie_sgl - obtain a PCIe SGL virt addr
3806  * @ioc: per adapter object
3807  * @smid: system request message index
3808  *
3809  * Return: virt pointer to a PCIe SGL.
3810  */
3811 void *
mpt3sas_base_get_pcie_sgl(struct MPT3SAS_ADAPTER * ioc,u16 smid)3812 mpt3sas_base_get_pcie_sgl(struct MPT3SAS_ADAPTER *ioc, u16 smid)
3813 {
3814 	return (void *)(ioc->pcie_sg_lookup[smid - 1].pcie_sgl);
3815 }
3816 
3817 /**
3818  * mpt3sas_base_get_pcie_sgl_dma - obtain a PCIe SGL dma addr
3819  * @ioc: per adapter object
3820  * @smid: system request message index
3821  *
3822  * Return: phys pointer to the address of the PCIe buffer.
3823  */
3824 dma_addr_t
mpt3sas_base_get_pcie_sgl_dma(struct MPT3SAS_ADAPTER * ioc,u16 smid)3825 mpt3sas_base_get_pcie_sgl_dma(struct MPT3SAS_ADAPTER *ioc, u16 smid)
3826 {
3827 	return ioc->pcie_sg_lookup[smid - 1].pcie_sgl_dma;
3828 }
3829 
3830 /**
3831  * mpt3sas_base_get_reply_virt_addr - obtain reply frames virt address
3832  * @ioc: per adapter object
3833  * @phys_addr: lower 32 physical addr of the reply
3834  *
3835  * Converts 32bit lower physical addr into a virt address.
3836  */
3837 void *
mpt3sas_base_get_reply_virt_addr(struct MPT3SAS_ADAPTER * ioc,u32 phys_addr)3838 mpt3sas_base_get_reply_virt_addr(struct MPT3SAS_ADAPTER *ioc, u32 phys_addr)
3839 {
3840 	if (!phys_addr)
3841 		return NULL;
3842 	return ioc->reply + (phys_addr - (u32)ioc->reply_dma);
3843 }
3844 
3845 /**
3846  * _base_get_msix_index - get the msix index
3847  * @ioc: per adapter object
3848  * @scmd: scsi_cmnd object
3849  *
3850  * Return: msix index of general reply queues,
3851  * i.e. reply queue on which IO request's reply
3852  * should be posted by the HBA firmware.
3853  */
3854 static inline u8
_base_get_msix_index(struct MPT3SAS_ADAPTER * ioc,struct scsi_cmnd * scmd)3855 _base_get_msix_index(struct MPT3SAS_ADAPTER *ioc,
3856 	struct scsi_cmnd *scmd)
3857 {
3858 	/* Enables reply_queue load balancing */
3859 	if (ioc->msix_load_balance)
3860 		return ioc->reply_queue_count ?
3861 		    base_mod64(atomic64_add_return(1,
3862 		    &ioc->total_io_cnt), ioc->reply_queue_count) : 0;
3863 
3864 	if (scmd && ioc->shost->nr_hw_queues > 1) {
3865 		u32 tag = blk_mq_unique_tag(scsi_cmd_to_rq(scmd));
3866 
3867 		return blk_mq_unique_tag_to_hwq(tag) +
3868 			ioc->high_iops_queues;
3869 	}
3870 
3871 	return ioc->cpu_msix_table[raw_smp_processor_id()];
3872 }
3873 
3874 /**
3875  * _base_get_high_iops_msix_index - get the msix index of
3876  *				high iops queues
3877  * @ioc: per adapter object
3878  * @scmd: scsi_cmnd object
3879  *
3880  * Return: msix index of high iops reply queues.
3881  * i.e. high iops reply queue on which IO request's
3882  * reply should be posted by the HBA firmware.
3883  */
3884 static inline u8
_base_get_high_iops_msix_index(struct MPT3SAS_ADAPTER * ioc,struct scsi_cmnd * scmd)3885 _base_get_high_iops_msix_index(struct MPT3SAS_ADAPTER *ioc,
3886 	struct scsi_cmnd *scmd)
3887 {
3888 	/**
3889 	 * Round robin the IO interrupts among the high iops
3890 	 * reply queues in terms of batch count 16 when outstanding
3891 	 * IOs on the target device is >=8.
3892 	 */
3893 
3894 	if (scsi_device_busy(scmd->device) > MPT3SAS_DEVICE_HIGH_IOPS_DEPTH)
3895 		return base_mod64((
3896 		    atomic64_add_return(1, &ioc->high_iops_outstanding) /
3897 		    MPT3SAS_HIGH_IOPS_BATCH_COUNT),
3898 		    MPT3SAS_HIGH_IOPS_REPLY_QUEUES);
3899 
3900 	return _base_get_msix_index(ioc, scmd);
3901 }
3902 
3903 /**
3904  * mpt3sas_base_get_smid - obtain a free smid from internal queue
3905  * @ioc: per adapter object
3906  * @cb_idx: callback index
3907  *
3908  * Return: smid (zero is invalid)
3909  */
3910 u16
mpt3sas_base_get_smid(struct MPT3SAS_ADAPTER * ioc,u8 cb_idx)3911 mpt3sas_base_get_smid(struct MPT3SAS_ADAPTER *ioc, u8 cb_idx)
3912 {
3913 	unsigned long flags;
3914 	struct request_tracker *request;
3915 	u16 smid;
3916 
3917 	spin_lock_irqsave(&ioc->scsi_lookup_lock, flags);
3918 	if (list_empty(&ioc->internal_free_list)) {
3919 		spin_unlock_irqrestore(&ioc->scsi_lookup_lock, flags);
3920 		ioc_err(ioc, "%s: smid not available\n", __func__);
3921 		return 0;
3922 	}
3923 
3924 	request = list_entry(ioc->internal_free_list.next,
3925 	    struct request_tracker, tracker_list);
3926 	request->cb_idx = cb_idx;
3927 	smid = request->smid;
3928 	list_del(&request->tracker_list);
3929 	spin_unlock_irqrestore(&ioc->scsi_lookup_lock, flags);
3930 	return smid;
3931 }
3932 
3933 /**
3934  * mpt3sas_base_get_smid_scsiio - obtain a free smid from scsiio queue
3935  * @ioc: per adapter object
3936  * @cb_idx: callback index
3937  * @scmd: pointer to scsi command object
3938  *
3939  * Return: smid (zero is invalid)
3940  */
3941 u16
mpt3sas_base_get_smid_scsiio(struct MPT3SAS_ADAPTER * ioc,u8 cb_idx,struct scsi_cmnd * scmd)3942 mpt3sas_base_get_smid_scsiio(struct MPT3SAS_ADAPTER *ioc, u8 cb_idx,
3943 	struct scsi_cmnd *scmd)
3944 {
3945 	struct scsiio_tracker *request = scsi_cmd_priv(scmd);
3946 	u16 smid;
3947 	u32 tag, unique_tag;
3948 
3949 	unique_tag = blk_mq_unique_tag(scsi_cmd_to_rq(scmd));
3950 	tag = blk_mq_unique_tag_to_tag(unique_tag);
3951 
3952 	/*
3953 	 * Store hw queue number corresponding to the tag.
3954 	 * This hw queue number is used later to determine
3955 	 * the unique_tag using the logic below. This unique_tag
3956 	 * is used to retrieve the scmd pointer corresponding
3957 	 * to tag using scsi_host_find_tag() API.
3958 	 *
3959 	 * tag = smid - 1;
3960 	 * unique_tag = ioc->io_queue_num[tag] << BLK_MQ_UNIQUE_TAG_BITS | tag;
3961 	 */
3962 	ioc->io_queue_num[tag] = blk_mq_unique_tag_to_hwq(unique_tag);
3963 
3964 	smid = tag + 1;
3965 	request->cb_idx = cb_idx;
3966 	request->smid = smid;
3967 	request->scmd = scmd;
3968 	INIT_LIST_HEAD(&request->chain_list);
3969 	return smid;
3970 }
3971 
3972 /**
3973  * mpt3sas_base_get_smid_hpr - obtain a free smid from hi-priority queue
3974  * @ioc: per adapter object
3975  * @cb_idx: callback index
3976  *
3977  * Return: smid (zero is invalid)
3978  */
3979 u16
mpt3sas_base_get_smid_hpr(struct MPT3SAS_ADAPTER * ioc,u8 cb_idx)3980 mpt3sas_base_get_smid_hpr(struct MPT3SAS_ADAPTER *ioc, u8 cb_idx)
3981 {
3982 	unsigned long flags;
3983 	struct request_tracker *request;
3984 	u16 smid;
3985 
3986 	spin_lock_irqsave(&ioc->scsi_lookup_lock, flags);
3987 	if (list_empty(&ioc->hpr_free_list)) {
3988 		spin_unlock_irqrestore(&ioc->scsi_lookup_lock, flags);
3989 		return 0;
3990 	}
3991 
3992 	request = list_entry(ioc->hpr_free_list.next,
3993 	    struct request_tracker, tracker_list);
3994 	request->cb_idx = cb_idx;
3995 	smid = request->smid;
3996 	list_del(&request->tracker_list);
3997 	spin_unlock_irqrestore(&ioc->scsi_lookup_lock, flags);
3998 	return smid;
3999 }
4000 
4001 static void
_base_recovery_check(struct MPT3SAS_ADAPTER * ioc)4002 _base_recovery_check(struct MPT3SAS_ADAPTER *ioc)
4003 {
4004 	/*
4005 	 * See _wait_for_commands_to_complete() call with regards to this code.
4006 	 */
4007 	if (ioc->shost_recovery && ioc->pending_io_count) {
4008 		ioc->pending_io_count = scsi_host_busy(ioc->shost);
4009 		if (ioc->pending_io_count == 0)
4010 			wake_up(&ioc->reset_wq);
4011 	}
4012 }
4013 
mpt3sas_base_clear_st(struct MPT3SAS_ADAPTER * ioc,struct scsiio_tracker * st)4014 void mpt3sas_base_clear_st(struct MPT3SAS_ADAPTER *ioc,
4015 			   struct scsiio_tracker *st)
4016 {
4017 	if (WARN_ON(st->smid == 0))
4018 		return;
4019 	st->cb_idx = 0xFF;
4020 	st->direct_io = 0;
4021 	st->scmd = NULL;
4022 	atomic_set(&ioc->chain_lookup[st->smid - 1].chain_offset, 0);
4023 	st->smid = 0;
4024 }
4025 
4026 /**
4027  * mpt3sas_base_free_smid - put smid back on free_list
4028  * @ioc: per adapter object
4029  * @smid: system request message index
4030  */
4031 void
mpt3sas_base_free_smid(struct MPT3SAS_ADAPTER * ioc,u16 smid)4032 mpt3sas_base_free_smid(struct MPT3SAS_ADAPTER *ioc, u16 smid)
4033 {
4034 	unsigned long flags;
4035 	int i;
4036 
4037 	if (smid < ioc->hi_priority_smid) {
4038 		struct scsiio_tracker *st;
4039 		void *request;
4040 
4041 		st = _get_st_from_smid(ioc, smid);
4042 		if (!st) {
4043 			_base_recovery_check(ioc);
4044 			return;
4045 		}
4046 
4047 		/* Clear MPI request frame */
4048 		request = mpt3sas_base_get_msg_frame(ioc, smid);
4049 		memset(request, 0, ioc->request_sz);
4050 
4051 		mpt3sas_base_clear_st(ioc, st);
4052 		_base_recovery_check(ioc);
4053 		ioc->io_queue_num[smid - 1] = 0;
4054 		return;
4055 	}
4056 
4057 	spin_lock_irqsave(&ioc->scsi_lookup_lock, flags);
4058 	if (smid < ioc->internal_smid) {
4059 		/* hi-priority */
4060 		i = smid - ioc->hi_priority_smid;
4061 		ioc->hpr_lookup[i].cb_idx = 0xFF;
4062 		list_add(&ioc->hpr_lookup[i].tracker_list, &ioc->hpr_free_list);
4063 	} else if (smid <= ioc->hba_queue_depth) {
4064 		/* internal queue */
4065 		i = smid - ioc->internal_smid;
4066 		ioc->internal_lookup[i].cb_idx = 0xFF;
4067 		list_add(&ioc->internal_lookup[i].tracker_list,
4068 		    &ioc->internal_free_list);
4069 	}
4070 	spin_unlock_irqrestore(&ioc->scsi_lookup_lock, flags);
4071 }
4072 
4073 /**
4074  * _base_mpi_ep_writeq - 32 bit write to MMIO
4075  * @b: data payload
4076  * @addr: address in MMIO space
4077  * @writeq_lock: spin lock
4078  *
4079  * This special handling for MPI EP to take care of 32 bit
4080  * environment where its not quarenteed to send the entire word
4081  * in one transfer.
4082  */
4083 static inline void
_base_mpi_ep_writeq(__u64 b,volatile void __iomem * addr,spinlock_t * writeq_lock)4084 _base_mpi_ep_writeq(__u64 b, volatile void __iomem *addr,
4085 					spinlock_t *writeq_lock)
4086 {
4087 	unsigned long flags;
4088 
4089 	spin_lock_irqsave(writeq_lock, flags);
4090 	__raw_writel((u32)(b), addr);
4091 	__raw_writel((u32)(b >> 32), (addr + 4));
4092 	spin_unlock_irqrestore(writeq_lock, flags);
4093 }
4094 
4095 /**
4096  * _base_writeq - 64 bit write to MMIO
4097  * @b: data payload
4098  * @addr: address in MMIO space
4099  * @writeq_lock: spin lock
4100  *
4101  * Glue for handling an atomic 64 bit word to MMIO. This special handling takes
4102  * care of 32 bit environment where its not quarenteed to send the entire word
4103  * in one transfer.
4104  */
4105 #if defined(writeq) && defined(CONFIG_64BIT)
4106 static inline void
_base_writeq(__u64 b,volatile void __iomem * addr,spinlock_t * writeq_lock)4107 _base_writeq(__u64 b, volatile void __iomem *addr, spinlock_t *writeq_lock)
4108 {
4109 	wmb();
4110 	__raw_writeq(b, addr);
4111 	barrier();
4112 }
4113 #else
4114 static inline void
_base_writeq(__u64 b,volatile void __iomem * addr,spinlock_t * writeq_lock)4115 _base_writeq(__u64 b, volatile void __iomem *addr, spinlock_t *writeq_lock)
4116 {
4117 	_base_mpi_ep_writeq(b, addr, writeq_lock);
4118 }
4119 #endif
4120 
4121 /**
4122  * _base_set_and_get_msix_index - get the msix index and assign to msix_io
4123  *                                variable of scsi tracker
4124  * @ioc: per adapter object
4125  * @smid: system request message index
4126  *
4127  * Return: msix index.
4128  */
4129 static u8
_base_set_and_get_msix_index(struct MPT3SAS_ADAPTER * ioc,u16 smid)4130 _base_set_and_get_msix_index(struct MPT3SAS_ADAPTER *ioc, u16 smid)
4131 {
4132 	struct scsiio_tracker *st = NULL;
4133 
4134 	if (smid < ioc->hi_priority_smid)
4135 		st = _get_st_from_smid(ioc, smid);
4136 
4137 	if (st == NULL)
4138 		return  _base_get_msix_index(ioc, NULL);
4139 
4140 	st->msix_io = ioc->get_msix_index_for_smlio(ioc, st->scmd);
4141 	return st->msix_io;
4142 }
4143 
4144 /**
4145  * _base_put_smid_mpi_ep_scsi_io - send SCSI_IO request to firmware
4146  * @ioc: per adapter object
4147  * @smid: system request message index
4148  * @handle: device handle
4149  */
4150 static void
_base_put_smid_mpi_ep_scsi_io(struct MPT3SAS_ADAPTER * ioc,u16 smid,u16 handle)4151 _base_put_smid_mpi_ep_scsi_io(struct MPT3SAS_ADAPTER *ioc,
4152 	u16 smid, u16 handle)
4153 {
4154 	Mpi2RequestDescriptorUnion_t descriptor;
4155 	u64 *request = (u64 *)&descriptor;
4156 	void *mpi_req_iomem;
4157 	__le32 *mfp = (__le32 *)mpt3sas_base_get_msg_frame(ioc, smid);
4158 
4159 	_clone_sg_entries(ioc, (void *) mfp, smid);
4160 	mpi_req_iomem = (void __force *)ioc->chip +
4161 			MPI_FRAME_START_OFFSET + (smid * ioc->request_sz);
4162 	_base_clone_mpi_to_sys_mem(mpi_req_iomem, (void *)mfp,
4163 					ioc->request_sz);
4164 	descriptor.SCSIIO.RequestFlags = MPI2_REQ_DESCRIPT_FLAGS_SCSI_IO;
4165 	descriptor.SCSIIO.MSIxIndex = _base_set_and_get_msix_index(ioc, smid);
4166 	descriptor.SCSIIO.SMID = cpu_to_le16(smid);
4167 	descriptor.SCSIIO.DevHandle = cpu_to_le16(handle);
4168 	descriptor.SCSIIO.LMID = 0;
4169 	_base_mpi_ep_writeq(*request, &ioc->chip->RequestDescriptorPostLow,
4170 	    &ioc->scsi_lookup_lock);
4171 }
4172 
4173 /**
4174  * _base_put_smid_scsi_io - send SCSI_IO request to firmware
4175  * @ioc: per adapter object
4176  * @smid: system request message index
4177  * @handle: device handle
4178  */
4179 static void
_base_put_smid_scsi_io(struct MPT3SAS_ADAPTER * ioc,u16 smid,u16 handle)4180 _base_put_smid_scsi_io(struct MPT3SAS_ADAPTER *ioc, u16 smid, u16 handle)
4181 {
4182 	Mpi2RequestDescriptorUnion_t descriptor;
4183 	u64 *request = (u64 *)&descriptor;
4184 
4185 
4186 	descriptor.SCSIIO.RequestFlags = MPI2_REQ_DESCRIPT_FLAGS_SCSI_IO;
4187 	descriptor.SCSIIO.MSIxIndex = _base_set_and_get_msix_index(ioc, smid);
4188 	descriptor.SCSIIO.SMID = cpu_to_le16(smid);
4189 	descriptor.SCSIIO.DevHandle = cpu_to_le16(handle);
4190 	descriptor.SCSIIO.LMID = 0;
4191 	_base_writeq(*request, &ioc->chip->RequestDescriptorPostLow,
4192 	    &ioc->scsi_lookup_lock);
4193 }
4194 
4195 /**
4196  * _base_put_smid_fast_path - send fast path request to firmware
4197  * @ioc: per adapter object
4198  * @smid: system request message index
4199  * @handle: device handle
4200  */
4201 static void
_base_put_smid_fast_path(struct MPT3SAS_ADAPTER * ioc,u16 smid,u16 handle)4202 _base_put_smid_fast_path(struct MPT3SAS_ADAPTER *ioc, u16 smid,
4203 	u16 handle)
4204 {
4205 	Mpi2RequestDescriptorUnion_t descriptor;
4206 	u64 *request = (u64 *)&descriptor;
4207 
4208 	descriptor.SCSIIO.RequestFlags =
4209 	    MPI25_REQ_DESCRIPT_FLAGS_FAST_PATH_SCSI_IO;
4210 	descriptor.SCSIIO.MSIxIndex = _base_set_and_get_msix_index(ioc, smid);
4211 	descriptor.SCSIIO.SMID = cpu_to_le16(smid);
4212 	descriptor.SCSIIO.DevHandle = cpu_to_le16(handle);
4213 	descriptor.SCSIIO.LMID = 0;
4214 	_base_writeq(*request, &ioc->chip->RequestDescriptorPostLow,
4215 	    &ioc->scsi_lookup_lock);
4216 }
4217 
4218 /**
4219  * _base_put_smid_hi_priority - send Task Management request to firmware
4220  * @ioc: per adapter object
4221  * @smid: system request message index
4222  * @msix_task: msix_task will be same as msix of IO in case of task abort else 0
4223  */
4224 static void
_base_put_smid_hi_priority(struct MPT3SAS_ADAPTER * ioc,u16 smid,u16 msix_task)4225 _base_put_smid_hi_priority(struct MPT3SAS_ADAPTER *ioc, u16 smid,
4226 	u16 msix_task)
4227 {
4228 	Mpi2RequestDescriptorUnion_t descriptor;
4229 	void *mpi_req_iomem;
4230 	u64 *request;
4231 
4232 	if (ioc->is_mcpu_endpoint) {
4233 		__le32 *mfp = (__le32 *)mpt3sas_base_get_msg_frame(ioc, smid);
4234 
4235 		/* TBD 256 is offset within sys register. */
4236 		mpi_req_iomem = (void __force *)ioc->chip
4237 					+ MPI_FRAME_START_OFFSET
4238 					+ (smid * ioc->request_sz);
4239 		_base_clone_mpi_to_sys_mem(mpi_req_iomem, (void *)mfp,
4240 							ioc->request_sz);
4241 	}
4242 
4243 	request = (u64 *)&descriptor;
4244 
4245 	descriptor.HighPriority.RequestFlags =
4246 	    MPI2_REQ_DESCRIPT_FLAGS_HIGH_PRIORITY;
4247 	descriptor.HighPriority.MSIxIndex =  msix_task;
4248 	descriptor.HighPriority.SMID = cpu_to_le16(smid);
4249 	descriptor.HighPriority.LMID = 0;
4250 	descriptor.HighPriority.Reserved1 = 0;
4251 	if (ioc->is_mcpu_endpoint)
4252 		_base_mpi_ep_writeq(*request,
4253 				&ioc->chip->RequestDescriptorPostLow,
4254 				&ioc->scsi_lookup_lock);
4255 	else
4256 		_base_writeq(*request, &ioc->chip->RequestDescriptorPostLow,
4257 		    &ioc->scsi_lookup_lock);
4258 }
4259 
4260 /**
4261  * mpt3sas_base_put_smid_nvme_encap - send NVMe encapsulated request to
4262  *  firmware
4263  * @ioc: per adapter object
4264  * @smid: system request message index
4265  */
4266 void
mpt3sas_base_put_smid_nvme_encap(struct MPT3SAS_ADAPTER * ioc,u16 smid)4267 mpt3sas_base_put_smid_nvme_encap(struct MPT3SAS_ADAPTER *ioc, u16 smid)
4268 {
4269 	Mpi2RequestDescriptorUnion_t descriptor;
4270 	u64 *request = (u64 *)&descriptor;
4271 
4272 	descriptor.Default.RequestFlags =
4273 		MPI26_REQ_DESCRIPT_FLAGS_PCIE_ENCAPSULATED;
4274 	descriptor.Default.MSIxIndex =  _base_set_and_get_msix_index(ioc, smid);
4275 	descriptor.Default.SMID = cpu_to_le16(smid);
4276 	descriptor.Default.LMID = 0;
4277 	descriptor.Default.DescriptorTypeDependent = 0;
4278 	_base_writeq(*request, &ioc->chip->RequestDescriptorPostLow,
4279 	    &ioc->scsi_lookup_lock);
4280 }
4281 
4282 /**
4283  * _base_put_smid_default - Default, primarily used for config pages
4284  * @ioc: per adapter object
4285  * @smid: system request message index
4286  */
4287 static void
_base_put_smid_default(struct MPT3SAS_ADAPTER * ioc,u16 smid)4288 _base_put_smid_default(struct MPT3SAS_ADAPTER *ioc, u16 smid)
4289 {
4290 	Mpi2RequestDescriptorUnion_t descriptor;
4291 	void *mpi_req_iomem;
4292 	u64 *request;
4293 
4294 	if (ioc->is_mcpu_endpoint) {
4295 		__le32 *mfp = (__le32 *)mpt3sas_base_get_msg_frame(ioc, smid);
4296 
4297 		_clone_sg_entries(ioc, (void *) mfp, smid);
4298 		/* TBD 256 is offset within sys register */
4299 		mpi_req_iomem = (void __force *)ioc->chip +
4300 			MPI_FRAME_START_OFFSET + (smid * ioc->request_sz);
4301 		_base_clone_mpi_to_sys_mem(mpi_req_iomem, (void *)mfp,
4302 							ioc->request_sz);
4303 	}
4304 	request = (u64 *)&descriptor;
4305 	descriptor.Default.RequestFlags = MPI2_REQ_DESCRIPT_FLAGS_DEFAULT_TYPE;
4306 	descriptor.Default.MSIxIndex = _base_set_and_get_msix_index(ioc, smid);
4307 	descriptor.Default.SMID = cpu_to_le16(smid);
4308 	descriptor.Default.LMID = 0;
4309 	descriptor.Default.DescriptorTypeDependent = 0;
4310 	if (ioc->is_mcpu_endpoint)
4311 		_base_mpi_ep_writeq(*request,
4312 				&ioc->chip->RequestDescriptorPostLow,
4313 				&ioc->scsi_lookup_lock);
4314 	else
4315 		_base_writeq(*request, &ioc->chip->RequestDescriptorPostLow,
4316 				&ioc->scsi_lookup_lock);
4317 }
4318 
4319 /**
4320  * _base_put_smid_scsi_io_atomic - send SCSI_IO request to firmware using
4321  *   Atomic Request Descriptor
4322  * @ioc: per adapter object
4323  * @smid: system request message index
4324  * @handle: device handle, unused in this function, for function type match
4325  *
4326  * Return: nothing.
4327  */
4328 static void
_base_put_smid_scsi_io_atomic(struct MPT3SAS_ADAPTER * ioc,u16 smid,u16 handle)4329 _base_put_smid_scsi_io_atomic(struct MPT3SAS_ADAPTER *ioc, u16 smid,
4330 	u16 handle)
4331 {
4332 	Mpi26AtomicRequestDescriptor_t descriptor;
4333 	u32 *request = (u32 *)&descriptor;
4334 
4335 	descriptor.RequestFlags = MPI2_REQ_DESCRIPT_FLAGS_SCSI_IO;
4336 	descriptor.MSIxIndex = _base_set_and_get_msix_index(ioc, smid);
4337 	descriptor.SMID = cpu_to_le16(smid);
4338 
4339 	writel(cpu_to_le32(*request), &ioc->chip->AtomicRequestDescriptorPost);
4340 }
4341 
4342 /**
4343  * _base_put_smid_fast_path_atomic - send fast path request to firmware
4344  * using Atomic Request Descriptor
4345  * @ioc: per adapter object
4346  * @smid: system request message index
4347  * @handle: device handle, unused in this function, for function type match
4348  * Return: nothing
4349  */
4350 static void
_base_put_smid_fast_path_atomic(struct MPT3SAS_ADAPTER * ioc,u16 smid,u16 handle)4351 _base_put_smid_fast_path_atomic(struct MPT3SAS_ADAPTER *ioc, u16 smid,
4352 	u16 handle)
4353 {
4354 	Mpi26AtomicRequestDescriptor_t descriptor;
4355 	u32 *request = (u32 *)&descriptor;
4356 
4357 	descriptor.RequestFlags = MPI25_REQ_DESCRIPT_FLAGS_FAST_PATH_SCSI_IO;
4358 	descriptor.MSIxIndex = _base_set_and_get_msix_index(ioc, smid);
4359 	descriptor.SMID = cpu_to_le16(smid);
4360 
4361 	writel(cpu_to_le32(*request), &ioc->chip->AtomicRequestDescriptorPost);
4362 }
4363 
4364 /**
4365  * _base_put_smid_hi_priority_atomic - send Task Management request to
4366  * firmware using Atomic Request Descriptor
4367  * @ioc: per adapter object
4368  * @smid: system request message index
4369  * @msix_task: msix_task will be same as msix of IO in case of task abort else 0
4370  *
4371  * Return: nothing.
4372  */
4373 static void
_base_put_smid_hi_priority_atomic(struct MPT3SAS_ADAPTER * ioc,u16 smid,u16 msix_task)4374 _base_put_smid_hi_priority_atomic(struct MPT3SAS_ADAPTER *ioc, u16 smid,
4375 	u16 msix_task)
4376 {
4377 	Mpi26AtomicRequestDescriptor_t descriptor;
4378 	u32 *request = (u32 *)&descriptor;
4379 
4380 	descriptor.RequestFlags = MPI2_REQ_DESCRIPT_FLAGS_HIGH_PRIORITY;
4381 	descriptor.MSIxIndex = msix_task;
4382 	descriptor.SMID = cpu_to_le16(smid);
4383 
4384 	writel(cpu_to_le32(*request), &ioc->chip->AtomicRequestDescriptorPost);
4385 }
4386 
4387 /**
4388  * _base_put_smid_default_atomic - Default, primarily used for config pages
4389  * use Atomic Request Descriptor
4390  * @ioc: per adapter object
4391  * @smid: system request message index
4392  *
4393  * Return: nothing.
4394  */
4395 static void
_base_put_smid_default_atomic(struct MPT3SAS_ADAPTER * ioc,u16 smid)4396 _base_put_smid_default_atomic(struct MPT3SAS_ADAPTER *ioc, u16 smid)
4397 {
4398 	Mpi26AtomicRequestDescriptor_t descriptor;
4399 	u32 *request = (u32 *)&descriptor;
4400 
4401 	descriptor.RequestFlags = MPI2_REQ_DESCRIPT_FLAGS_DEFAULT_TYPE;
4402 	descriptor.MSIxIndex = _base_set_and_get_msix_index(ioc, smid);
4403 	descriptor.SMID = cpu_to_le16(smid);
4404 
4405 	writel(cpu_to_le32(*request), &ioc->chip->AtomicRequestDescriptorPost);
4406 }
4407 
4408 /**
4409  * _base_display_OEMs_branding - Display branding string
4410  * @ioc: per adapter object
4411  */
4412 static void
_base_display_OEMs_branding(struct MPT3SAS_ADAPTER * ioc)4413 _base_display_OEMs_branding(struct MPT3SAS_ADAPTER *ioc)
4414 {
4415 	if (ioc->pdev->subsystem_vendor != PCI_VENDOR_ID_INTEL)
4416 		return;
4417 
4418 	switch (ioc->pdev->subsystem_vendor) {
4419 	case PCI_VENDOR_ID_INTEL:
4420 		switch (ioc->pdev->device) {
4421 		case MPI2_MFGPAGE_DEVID_SAS2008:
4422 			switch (ioc->pdev->subsystem_device) {
4423 			case MPT2SAS_INTEL_RMS2LL080_SSDID:
4424 				ioc_info(ioc, "%s\n",
4425 					 MPT2SAS_INTEL_RMS2LL080_BRANDING);
4426 				break;
4427 			case MPT2SAS_INTEL_RMS2LL040_SSDID:
4428 				ioc_info(ioc, "%s\n",
4429 					 MPT2SAS_INTEL_RMS2LL040_BRANDING);
4430 				break;
4431 			case MPT2SAS_INTEL_SSD910_SSDID:
4432 				ioc_info(ioc, "%s\n",
4433 					 MPT2SAS_INTEL_SSD910_BRANDING);
4434 				break;
4435 			default:
4436 				ioc_info(ioc, "Intel(R) Controller: Subsystem ID: 0x%X\n",
4437 					 ioc->pdev->subsystem_device);
4438 				break;
4439 			}
4440 			break;
4441 		case MPI2_MFGPAGE_DEVID_SAS2308_2:
4442 			switch (ioc->pdev->subsystem_device) {
4443 			case MPT2SAS_INTEL_RS25GB008_SSDID:
4444 				ioc_info(ioc, "%s\n",
4445 					 MPT2SAS_INTEL_RS25GB008_BRANDING);
4446 				break;
4447 			case MPT2SAS_INTEL_RMS25JB080_SSDID:
4448 				ioc_info(ioc, "%s\n",
4449 					 MPT2SAS_INTEL_RMS25JB080_BRANDING);
4450 				break;
4451 			case MPT2SAS_INTEL_RMS25JB040_SSDID:
4452 				ioc_info(ioc, "%s\n",
4453 					 MPT2SAS_INTEL_RMS25JB040_BRANDING);
4454 				break;
4455 			case MPT2SAS_INTEL_RMS25KB080_SSDID:
4456 				ioc_info(ioc, "%s\n",
4457 					 MPT2SAS_INTEL_RMS25KB080_BRANDING);
4458 				break;
4459 			case MPT2SAS_INTEL_RMS25KB040_SSDID:
4460 				ioc_info(ioc, "%s\n",
4461 					 MPT2SAS_INTEL_RMS25KB040_BRANDING);
4462 				break;
4463 			case MPT2SAS_INTEL_RMS25LB040_SSDID:
4464 				ioc_info(ioc, "%s\n",
4465 					 MPT2SAS_INTEL_RMS25LB040_BRANDING);
4466 				break;
4467 			case MPT2SAS_INTEL_RMS25LB080_SSDID:
4468 				ioc_info(ioc, "%s\n",
4469 					 MPT2SAS_INTEL_RMS25LB080_BRANDING);
4470 				break;
4471 			default:
4472 				ioc_info(ioc, "Intel(R) Controller: Subsystem ID: 0x%X\n",
4473 					 ioc->pdev->subsystem_device);
4474 				break;
4475 			}
4476 			break;
4477 		case MPI25_MFGPAGE_DEVID_SAS3008:
4478 			switch (ioc->pdev->subsystem_device) {
4479 			case MPT3SAS_INTEL_RMS3JC080_SSDID:
4480 				ioc_info(ioc, "%s\n",
4481 					 MPT3SAS_INTEL_RMS3JC080_BRANDING);
4482 				break;
4483 
4484 			case MPT3SAS_INTEL_RS3GC008_SSDID:
4485 				ioc_info(ioc, "%s\n",
4486 					 MPT3SAS_INTEL_RS3GC008_BRANDING);
4487 				break;
4488 			case MPT3SAS_INTEL_RS3FC044_SSDID:
4489 				ioc_info(ioc, "%s\n",
4490 					 MPT3SAS_INTEL_RS3FC044_BRANDING);
4491 				break;
4492 			case MPT3SAS_INTEL_RS3UC080_SSDID:
4493 				ioc_info(ioc, "%s\n",
4494 					 MPT3SAS_INTEL_RS3UC080_BRANDING);
4495 				break;
4496 			default:
4497 				ioc_info(ioc, "Intel(R) Controller: Subsystem ID: 0x%X\n",
4498 					 ioc->pdev->subsystem_device);
4499 				break;
4500 			}
4501 			break;
4502 		default:
4503 			ioc_info(ioc, "Intel(R) Controller: Subsystem ID: 0x%X\n",
4504 				 ioc->pdev->subsystem_device);
4505 			break;
4506 		}
4507 		break;
4508 	case PCI_VENDOR_ID_DELL:
4509 		switch (ioc->pdev->device) {
4510 		case MPI2_MFGPAGE_DEVID_SAS2008:
4511 			switch (ioc->pdev->subsystem_device) {
4512 			case MPT2SAS_DELL_6GBPS_SAS_HBA_SSDID:
4513 				ioc_info(ioc, "%s\n",
4514 					 MPT2SAS_DELL_6GBPS_SAS_HBA_BRANDING);
4515 				break;
4516 			case MPT2SAS_DELL_PERC_H200_ADAPTER_SSDID:
4517 				ioc_info(ioc, "%s\n",
4518 					 MPT2SAS_DELL_PERC_H200_ADAPTER_BRANDING);
4519 				break;
4520 			case MPT2SAS_DELL_PERC_H200_INTEGRATED_SSDID:
4521 				ioc_info(ioc, "%s\n",
4522 					 MPT2SAS_DELL_PERC_H200_INTEGRATED_BRANDING);
4523 				break;
4524 			case MPT2SAS_DELL_PERC_H200_MODULAR_SSDID:
4525 				ioc_info(ioc, "%s\n",
4526 					 MPT2SAS_DELL_PERC_H200_MODULAR_BRANDING);
4527 				break;
4528 			case MPT2SAS_DELL_PERC_H200_EMBEDDED_SSDID:
4529 				ioc_info(ioc, "%s\n",
4530 					 MPT2SAS_DELL_PERC_H200_EMBEDDED_BRANDING);
4531 				break;
4532 			case MPT2SAS_DELL_PERC_H200_SSDID:
4533 				ioc_info(ioc, "%s\n",
4534 					 MPT2SAS_DELL_PERC_H200_BRANDING);
4535 				break;
4536 			case MPT2SAS_DELL_6GBPS_SAS_SSDID:
4537 				ioc_info(ioc, "%s\n",
4538 					 MPT2SAS_DELL_6GBPS_SAS_BRANDING);
4539 				break;
4540 			default:
4541 				ioc_info(ioc, "Dell 6Gbps HBA: Subsystem ID: 0x%X\n",
4542 					 ioc->pdev->subsystem_device);
4543 				break;
4544 			}
4545 			break;
4546 		case MPI25_MFGPAGE_DEVID_SAS3008:
4547 			switch (ioc->pdev->subsystem_device) {
4548 			case MPT3SAS_DELL_12G_HBA_SSDID:
4549 				ioc_info(ioc, "%s\n",
4550 					 MPT3SAS_DELL_12G_HBA_BRANDING);
4551 				break;
4552 			default:
4553 				ioc_info(ioc, "Dell 12Gbps HBA: Subsystem ID: 0x%X\n",
4554 					 ioc->pdev->subsystem_device);
4555 				break;
4556 			}
4557 			break;
4558 		default:
4559 			ioc_info(ioc, "Dell HBA: Subsystem ID: 0x%X\n",
4560 				 ioc->pdev->subsystem_device);
4561 			break;
4562 		}
4563 		break;
4564 	case PCI_VENDOR_ID_CISCO:
4565 		switch (ioc->pdev->device) {
4566 		case MPI25_MFGPAGE_DEVID_SAS3008:
4567 			switch (ioc->pdev->subsystem_device) {
4568 			case MPT3SAS_CISCO_12G_8E_HBA_SSDID:
4569 				ioc_info(ioc, "%s\n",
4570 					 MPT3SAS_CISCO_12G_8E_HBA_BRANDING);
4571 				break;
4572 			case MPT3SAS_CISCO_12G_8I_HBA_SSDID:
4573 				ioc_info(ioc, "%s\n",
4574 					 MPT3SAS_CISCO_12G_8I_HBA_BRANDING);
4575 				break;
4576 			case MPT3SAS_CISCO_12G_AVILA_HBA_SSDID:
4577 				ioc_info(ioc, "%s\n",
4578 					 MPT3SAS_CISCO_12G_AVILA_HBA_BRANDING);
4579 				break;
4580 			default:
4581 				ioc_info(ioc, "Cisco 12Gbps SAS HBA: Subsystem ID: 0x%X\n",
4582 					 ioc->pdev->subsystem_device);
4583 				break;
4584 			}
4585 			break;
4586 		case MPI25_MFGPAGE_DEVID_SAS3108_1:
4587 			switch (ioc->pdev->subsystem_device) {
4588 			case MPT3SAS_CISCO_12G_AVILA_HBA_SSDID:
4589 				ioc_info(ioc, "%s\n",
4590 					 MPT3SAS_CISCO_12G_AVILA_HBA_BRANDING);
4591 				break;
4592 			case MPT3SAS_CISCO_12G_COLUSA_MEZZANINE_HBA_SSDID:
4593 				ioc_info(ioc, "%s\n",
4594 					 MPT3SAS_CISCO_12G_COLUSA_MEZZANINE_HBA_BRANDING);
4595 				break;
4596 			default:
4597 				ioc_info(ioc, "Cisco 12Gbps SAS HBA: Subsystem ID: 0x%X\n",
4598 					 ioc->pdev->subsystem_device);
4599 				break;
4600 			}
4601 			break;
4602 		default:
4603 			ioc_info(ioc, "Cisco SAS HBA: Subsystem ID: 0x%X\n",
4604 				 ioc->pdev->subsystem_device);
4605 			break;
4606 		}
4607 		break;
4608 	case MPT2SAS_HP_3PAR_SSVID:
4609 		switch (ioc->pdev->device) {
4610 		case MPI2_MFGPAGE_DEVID_SAS2004:
4611 			switch (ioc->pdev->subsystem_device) {
4612 			case MPT2SAS_HP_DAUGHTER_2_4_INTERNAL_SSDID:
4613 				ioc_info(ioc, "%s\n",
4614 					 MPT2SAS_HP_DAUGHTER_2_4_INTERNAL_BRANDING);
4615 				break;
4616 			default:
4617 				ioc_info(ioc, "HP 6Gbps SAS HBA: Subsystem ID: 0x%X\n",
4618 					 ioc->pdev->subsystem_device);
4619 				break;
4620 			}
4621 			break;
4622 		case MPI2_MFGPAGE_DEVID_SAS2308_2:
4623 			switch (ioc->pdev->subsystem_device) {
4624 			case MPT2SAS_HP_2_4_INTERNAL_SSDID:
4625 				ioc_info(ioc, "%s\n",
4626 					 MPT2SAS_HP_2_4_INTERNAL_BRANDING);
4627 				break;
4628 			case MPT2SAS_HP_2_4_EXTERNAL_SSDID:
4629 				ioc_info(ioc, "%s\n",
4630 					 MPT2SAS_HP_2_4_EXTERNAL_BRANDING);
4631 				break;
4632 			case MPT2SAS_HP_1_4_INTERNAL_1_4_EXTERNAL_SSDID:
4633 				ioc_info(ioc, "%s\n",
4634 					 MPT2SAS_HP_1_4_INTERNAL_1_4_EXTERNAL_BRANDING);
4635 				break;
4636 			case MPT2SAS_HP_EMBEDDED_2_4_INTERNAL_SSDID:
4637 				ioc_info(ioc, "%s\n",
4638 					 MPT2SAS_HP_EMBEDDED_2_4_INTERNAL_BRANDING);
4639 				break;
4640 			default:
4641 				ioc_info(ioc, "HP 6Gbps SAS HBA: Subsystem ID: 0x%X\n",
4642 					 ioc->pdev->subsystem_device);
4643 				break;
4644 			}
4645 			break;
4646 		default:
4647 			ioc_info(ioc, "HP SAS HBA: Subsystem ID: 0x%X\n",
4648 				 ioc->pdev->subsystem_device);
4649 			break;
4650 		}
4651 		break;
4652 	default:
4653 		break;
4654 	}
4655 }
4656 
4657 /**
4658  * _base_display_fwpkg_version - sends FWUpload request to pull FWPkg
4659  *				version from FW Image Header.
4660  * @ioc: per adapter object
4661  *
4662  * Return: 0 for success, non-zero for failure.
4663  */
4664 	static int
_base_display_fwpkg_version(struct MPT3SAS_ADAPTER * ioc)4665 _base_display_fwpkg_version(struct MPT3SAS_ADAPTER *ioc)
4666 {
4667 	Mpi2FWImageHeader_t *fw_img_hdr;
4668 	Mpi26ComponentImageHeader_t *cmp_img_hdr;
4669 	Mpi25FWUploadRequest_t *mpi_request;
4670 	Mpi2FWUploadReply_t mpi_reply;
4671 	int r = 0, issue_diag_reset = 0;
4672 	u32  package_version = 0;
4673 	void *fwpkg_data = NULL;
4674 	dma_addr_t fwpkg_data_dma;
4675 	u16 smid, ioc_status;
4676 	size_t data_length;
4677 
4678 	dinitprintk(ioc, ioc_info(ioc, "%s\n", __func__));
4679 
4680 	if (ioc->base_cmds.status & MPT3_CMD_PENDING) {
4681 		ioc_err(ioc, "%s: internal command already in use\n", __func__);
4682 		return -EAGAIN;
4683 	}
4684 
4685 	data_length = sizeof(Mpi2FWImageHeader_t);
4686 	fwpkg_data = dma_alloc_coherent(&ioc->pdev->dev, data_length,
4687 			&fwpkg_data_dma, GFP_KERNEL);
4688 	if (!fwpkg_data) {
4689 		ioc_err(ioc,
4690 		    "Memory allocation for fwpkg data failed at %s:%d/%s()!\n",
4691 			__FILE__, __LINE__, __func__);
4692 		return -ENOMEM;
4693 	}
4694 
4695 	smid = mpt3sas_base_get_smid(ioc, ioc->base_cb_idx);
4696 	if (!smid) {
4697 		ioc_err(ioc, "%s: failed obtaining a smid\n", __func__);
4698 		r = -EAGAIN;
4699 		goto out;
4700 	}
4701 
4702 	ioc->base_cmds.status = MPT3_CMD_PENDING;
4703 	mpi_request = mpt3sas_base_get_msg_frame(ioc, smid);
4704 	ioc->base_cmds.smid = smid;
4705 	memset(mpi_request, 0, sizeof(Mpi25FWUploadRequest_t));
4706 	mpi_request->Function = MPI2_FUNCTION_FW_UPLOAD;
4707 	mpi_request->ImageType = MPI2_FW_UPLOAD_ITYPE_FW_FLASH;
4708 	mpi_request->ImageSize = cpu_to_le32(data_length);
4709 	ioc->build_sg(ioc, &mpi_request->SGL, 0, 0, fwpkg_data_dma,
4710 			data_length);
4711 	init_completion(&ioc->base_cmds.done);
4712 	ioc->put_smid_default(ioc, smid);
4713 	/* Wait for 15 seconds */
4714 	wait_for_completion_timeout(&ioc->base_cmds.done,
4715 			FW_IMG_HDR_READ_TIMEOUT*HZ);
4716 	ioc_info(ioc, "%s: complete\n", __func__);
4717 	if (!(ioc->base_cmds.status & MPT3_CMD_COMPLETE)) {
4718 		ioc_err(ioc, "%s: timeout\n", __func__);
4719 		_debug_dump_mf(mpi_request,
4720 				sizeof(Mpi25FWUploadRequest_t)/4);
4721 		issue_diag_reset = 1;
4722 	} else {
4723 		memset(&mpi_reply, 0, sizeof(Mpi2FWUploadReply_t));
4724 		if (ioc->base_cmds.status & MPT3_CMD_REPLY_VALID) {
4725 			memcpy(&mpi_reply, ioc->base_cmds.reply,
4726 					sizeof(Mpi2FWUploadReply_t));
4727 			ioc_status = le16_to_cpu(mpi_reply.IOCStatus) &
4728 						MPI2_IOCSTATUS_MASK;
4729 			if (ioc_status == MPI2_IOCSTATUS_SUCCESS) {
4730 				fw_img_hdr = (Mpi2FWImageHeader_t *)fwpkg_data;
4731 				if (le32_to_cpu(fw_img_hdr->Signature) ==
4732 				    MPI26_IMAGE_HEADER_SIGNATURE0_MPI26) {
4733 					cmp_img_hdr =
4734 					    (Mpi26ComponentImageHeader_t *)
4735 					    (fwpkg_data);
4736 					package_version =
4737 					    le32_to_cpu(
4738 					    cmp_img_hdr->ApplicationSpecific);
4739 				} else
4740 					package_version =
4741 					    le32_to_cpu(
4742 					    fw_img_hdr->PackageVersion.Word);
4743 				if (package_version)
4744 					ioc_info(ioc,
4745 					"FW Package Ver(%02d.%02d.%02d.%02d)\n",
4746 					((package_version) & 0xFF000000) >> 24,
4747 					((package_version) & 0x00FF0000) >> 16,
4748 					((package_version) & 0x0000FF00) >> 8,
4749 					(package_version) & 0x000000FF);
4750 			} else {
4751 				_debug_dump_mf(&mpi_reply,
4752 						sizeof(Mpi2FWUploadReply_t)/4);
4753 			}
4754 		}
4755 	}
4756 	ioc->base_cmds.status = MPT3_CMD_NOT_USED;
4757 out:
4758 	if (fwpkg_data)
4759 		dma_free_coherent(&ioc->pdev->dev, data_length, fwpkg_data,
4760 				fwpkg_data_dma);
4761 	if (issue_diag_reset) {
4762 		if (ioc->drv_internal_flags & MPT_DRV_INTERNAL_FIRST_PE_ISSUED)
4763 			return -EFAULT;
4764 		if (mpt3sas_base_check_for_fault_and_issue_reset(ioc))
4765 			return -EFAULT;
4766 		r = -EAGAIN;
4767 	}
4768 	return r;
4769 }
4770 
4771 /**
4772  * _base_display_ioc_capabilities - Display IOC's capabilities.
4773  * @ioc: per adapter object
4774  */
4775 static void
_base_display_ioc_capabilities(struct MPT3SAS_ADAPTER * ioc)4776 _base_display_ioc_capabilities(struct MPT3SAS_ADAPTER *ioc)
4777 {
4778 	int i = 0;
4779 	char desc[16];
4780 	u32 iounit_pg1_flags;
4781 	u32 bios_version;
4782 
4783 	bios_version = le32_to_cpu(ioc->bios_pg3.BiosVersion);
4784 	strncpy(desc, ioc->manu_pg0.ChipName, 16);
4785 	ioc_info(ioc, "%s: FWVersion(%02d.%02d.%02d.%02d), ChipRevision(0x%02x), BiosVersion(%02d.%02d.%02d.%02d)\n",
4786 		 desc,
4787 		 (ioc->facts.FWVersion.Word & 0xFF000000) >> 24,
4788 		 (ioc->facts.FWVersion.Word & 0x00FF0000) >> 16,
4789 		 (ioc->facts.FWVersion.Word & 0x0000FF00) >> 8,
4790 		 ioc->facts.FWVersion.Word & 0x000000FF,
4791 		 ioc->pdev->revision,
4792 		 (bios_version & 0xFF000000) >> 24,
4793 		 (bios_version & 0x00FF0000) >> 16,
4794 		 (bios_version & 0x0000FF00) >> 8,
4795 		 bios_version & 0x000000FF);
4796 
4797 	_base_display_OEMs_branding(ioc);
4798 
4799 	if (ioc->facts.ProtocolFlags & MPI2_IOCFACTS_PROTOCOL_NVME_DEVICES) {
4800 		pr_info("%sNVMe", i ? "," : "");
4801 		i++;
4802 	}
4803 
4804 	ioc_info(ioc, "Protocol=(");
4805 
4806 	if (ioc->facts.ProtocolFlags & MPI2_IOCFACTS_PROTOCOL_SCSI_INITIATOR) {
4807 		pr_cont("Initiator");
4808 		i++;
4809 	}
4810 
4811 	if (ioc->facts.ProtocolFlags & MPI2_IOCFACTS_PROTOCOL_SCSI_TARGET) {
4812 		pr_cont("%sTarget", i ? "," : "");
4813 		i++;
4814 	}
4815 
4816 	i = 0;
4817 	pr_cont("), Capabilities=(");
4818 
4819 	if (!ioc->hide_ir_msg) {
4820 		if (ioc->facts.IOCCapabilities &
4821 		    MPI2_IOCFACTS_CAPABILITY_INTEGRATED_RAID) {
4822 			pr_cont("Raid");
4823 			i++;
4824 		}
4825 	}
4826 
4827 	if (ioc->facts.IOCCapabilities & MPI2_IOCFACTS_CAPABILITY_TLR) {
4828 		pr_cont("%sTLR", i ? "," : "");
4829 		i++;
4830 	}
4831 
4832 	if (ioc->facts.IOCCapabilities & MPI2_IOCFACTS_CAPABILITY_MULTICAST) {
4833 		pr_cont("%sMulticast", i ? "," : "");
4834 		i++;
4835 	}
4836 
4837 	if (ioc->facts.IOCCapabilities &
4838 	    MPI2_IOCFACTS_CAPABILITY_BIDIRECTIONAL_TARGET) {
4839 		pr_cont("%sBIDI Target", i ? "," : "");
4840 		i++;
4841 	}
4842 
4843 	if (ioc->facts.IOCCapabilities & MPI2_IOCFACTS_CAPABILITY_EEDP) {
4844 		pr_cont("%sEEDP", i ? "," : "");
4845 		i++;
4846 	}
4847 
4848 	if (ioc->facts.IOCCapabilities &
4849 	    MPI2_IOCFACTS_CAPABILITY_SNAPSHOT_BUFFER) {
4850 		pr_cont("%sSnapshot Buffer", i ? "," : "");
4851 		i++;
4852 	}
4853 
4854 	if (ioc->facts.IOCCapabilities &
4855 	    MPI2_IOCFACTS_CAPABILITY_DIAG_TRACE_BUFFER) {
4856 		pr_cont("%sDiag Trace Buffer", i ? "," : "");
4857 		i++;
4858 	}
4859 
4860 	if (ioc->facts.IOCCapabilities &
4861 	    MPI2_IOCFACTS_CAPABILITY_EXTENDED_BUFFER) {
4862 		pr_cont("%sDiag Extended Buffer", i ? "," : "");
4863 		i++;
4864 	}
4865 
4866 	if (ioc->facts.IOCCapabilities &
4867 	    MPI2_IOCFACTS_CAPABILITY_TASK_SET_FULL_HANDLING) {
4868 		pr_cont("%sTask Set Full", i ? "," : "");
4869 		i++;
4870 	}
4871 
4872 	iounit_pg1_flags = le32_to_cpu(ioc->iounit_pg1.Flags);
4873 	if (!(iounit_pg1_flags & MPI2_IOUNITPAGE1_NATIVE_COMMAND_Q_DISABLE)) {
4874 		pr_cont("%sNCQ", i ? "," : "");
4875 		i++;
4876 	}
4877 
4878 	pr_cont(")\n");
4879 }
4880 
4881 /**
4882  * mpt3sas_base_update_missing_delay - change the missing delay timers
4883  * @ioc: per adapter object
4884  * @device_missing_delay: amount of time till device is reported missing
4885  * @io_missing_delay: interval IO is returned when there is a missing device
4886  *
4887  * Passed on the command line, this function will modify the device missing
4888  * delay, as well as the io missing delay. This should be called at driver
4889  * load time.
4890  */
4891 void
mpt3sas_base_update_missing_delay(struct MPT3SAS_ADAPTER * ioc,u16 device_missing_delay,u8 io_missing_delay)4892 mpt3sas_base_update_missing_delay(struct MPT3SAS_ADAPTER *ioc,
4893 	u16 device_missing_delay, u8 io_missing_delay)
4894 {
4895 	u16 dmd, dmd_new, dmd_orignal;
4896 	u8 io_missing_delay_original;
4897 	u16 sz;
4898 	Mpi2SasIOUnitPage1_t *sas_iounit_pg1 = NULL;
4899 	Mpi2ConfigReply_t mpi_reply;
4900 	u8 num_phys = 0;
4901 	u16 ioc_status;
4902 
4903 	mpt3sas_config_get_number_hba_phys(ioc, &num_phys);
4904 	if (!num_phys)
4905 		return;
4906 
4907 	sz = offsetof(Mpi2SasIOUnitPage1_t, PhyData) + (num_phys *
4908 	    sizeof(Mpi2SasIOUnit1PhyData_t));
4909 	sas_iounit_pg1 = kzalloc(sz, GFP_KERNEL);
4910 	if (!sas_iounit_pg1) {
4911 		ioc_err(ioc, "failure at %s:%d/%s()!\n",
4912 			__FILE__, __LINE__, __func__);
4913 		goto out;
4914 	}
4915 	if ((mpt3sas_config_get_sas_iounit_pg1(ioc, &mpi_reply,
4916 	    sas_iounit_pg1, sz))) {
4917 		ioc_err(ioc, "failure at %s:%d/%s()!\n",
4918 			__FILE__, __LINE__, __func__);
4919 		goto out;
4920 	}
4921 	ioc_status = le16_to_cpu(mpi_reply.IOCStatus) &
4922 	    MPI2_IOCSTATUS_MASK;
4923 	if (ioc_status != MPI2_IOCSTATUS_SUCCESS) {
4924 		ioc_err(ioc, "failure at %s:%d/%s()!\n",
4925 			__FILE__, __LINE__, __func__);
4926 		goto out;
4927 	}
4928 
4929 	/* device missing delay */
4930 	dmd = sas_iounit_pg1->ReportDeviceMissingDelay;
4931 	if (dmd & MPI2_SASIOUNIT1_REPORT_MISSING_UNIT_16)
4932 		dmd = (dmd & MPI2_SASIOUNIT1_REPORT_MISSING_TIMEOUT_MASK) * 16;
4933 	else
4934 		dmd = dmd & MPI2_SASIOUNIT1_REPORT_MISSING_TIMEOUT_MASK;
4935 	dmd_orignal = dmd;
4936 	if (device_missing_delay > 0x7F) {
4937 		dmd = (device_missing_delay > 0x7F0) ? 0x7F0 :
4938 		    device_missing_delay;
4939 		dmd = dmd / 16;
4940 		dmd |= MPI2_SASIOUNIT1_REPORT_MISSING_UNIT_16;
4941 	} else
4942 		dmd = device_missing_delay;
4943 	sas_iounit_pg1->ReportDeviceMissingDelay = dmd;
4944 
4945 	/* io missing delay */
4946 	io_missing_delay_original = sas_iounit_pg1->IODeviceMissingDelay;
4947 	sas_iounit_pg1->IODeviceMissingDelay = io_missing_delay;
4948 
4949 	if (!mpt3sas_config_set_sas_iounit_pg1(ioc, &mpi_reply, sas_iounit_pg1,
4950 	    sz)) {
4951 		if (dmd & MPI2_SASIOUNIT1_REPORT_MISSING_UNIT_16)
4952 			dmd_new = (dmd &
4953 			    MPI2_SASIOUNIT1_REPORT_MISSING_TIMEOUT_MASK) * 16;
4954 		else
4955 			dmd_new =
4956 		    dmd & MPI2_SASIOUNIT1_REPORT_MISSING_TIMEOUT_MASK;
4957 		ioc_info(ioc, "device_missing_delay: old(%d), new(%d)\n",
4958 			 dmd_orignal, dmd_new);
4959 		ioc_info(ioc, "ioc_missing_delay: old(%d), new(%d)\n",
4960 			 io_missing_delay_original,
4961 			 io_missing_delay);
4962 		ioc->device_missing_delay = dmd_new;
4963 		ioc->io_missing_delay = io_missing_delay;
4964 	}
4965 
4966 out:
4967 	kfree(sas_iounit_pg1);
4968 }
4969 
4970 /**
4971  * _base_update_ioc_page1_inlinewith_perf_mode - Update IOC Page1 fields
4972  *    according to performance mode.
4973  * @ioc : per adapter object
4974  *
4975  * Return: zero on success; otherwise return EAGAIN error code asking the
4976  * caller to retry.
4977  */
4978 static int
_base_update_ioc_page1_inlinewith_perf_mode(struct MPT3SAS_ADAPTER * ioc)4979 _base_update_ioc_page1_inlinewith_perf_mode(struct MPT3SAS_ADAPTER *ioc)
4980 {
4981 	Mpi2IOCPage1_t ioc_pg1;
4982 	Mpi2ConfigReply_t mpi_reply;
4983 	int rc;
4984 
4985 	rc = mpt3sas_config_get_ioc_pg1(ioc, &mpi_reply, &ioc->ioc_pg1_copy);
4986 	if (rc)
4987 		return rc;
4988 	memcpy(&ioc_pg1, &ioc->ioc_pg1_copy, sizeof(Mpi2IOCPage1_t));
4989 
4990 	switch (perf_mode) {
4991 	case MPT_PERF_MODE_DEFAULT:
4992 	case MPT_PERF_MODE_BALANCED:
4993 		if (ioc->high_iops_queues) {
4994 			ioc_info(ioc,
4995 				"Enable interrupt coalescing only for first\t"
4996 				"%d reply queues\n",
4997 				MPT3SAS_HIGH_IOPS_REPLY_QUEUES);
4998 			/*
4999 			 * If 31st bit is zero then interrupt coalescing is
5000 			 * enabled for all reply descriptor post queues.
5001 			 * If 31st bit is set to one then user can
5002 			 * enable/disable interrupt coalescing on per reply
5003 			 * descriptor post queue group(8) basis. So to enable
5004 			 * interrupt coalescing only on first reply descriptor
5005 			 * post queue group 31st bit and zero th bit is enabled.
5006 			 */
5007 			ioc_pg1.ProductSpecific = cpu_to_le32(0x80000000 |
5008 			    ((1 << MPT3SAS_HIGH_IOPS_REPLY_QUEUES/8) - 1));
5009 			rc = mpt3sas_config_set_ioc_pg1(ioc, &mpi_reply, &ioc_pg1);
5010 			if (rc)
5011 				return rc;
5012 			ioc_info(ioc, "performance mode: balanced\n");
5013 			return 0;
5014 		}
5015 		fallthrough;
5016 	case MPT_PERF_MODE_LATENCY:
5017 		/*
5018 		 * Enable interrupt coalescing on all reply queues
5019 		 * with timeout value 0xA
5020 		 */
5021 		ioc_pg1.CoalescingTimeout = cpu_to_le32(0xa);
5022 		ioc_pg1.Flags |= cpu_to_le32(MPI2_IOCPAGE1_REPLY_COALESCING);
5023 		ioc_pg1.ProductSpecific = 0;
5024 		rc = mpt3sas_config_set_ioc_pg1(ioc, &mpi_reply, &ioc_pg1);
5025 		if (rc)
5026 			return rc;
5027 		ioc_info(ioc, "performance mode: latency\n");
5028 		break;
5029 	case MPT_PERF_MODE_IOPS:
5030 		/*
5031 		 * Enable interrupt coalescing on all reply queues.
5032 		 */
5033 		ioc_info(ioc,
5034 		    "performance mode: iops with coalescing timeout: 0x%x\n",
5035 		    le32_to_cpu(ioc_pg1.CoalescingTimeout));
5036 		ioc_pg1.Flags |= cpu_to_le32(MPI2_IOCPAGE1_REPLY_COALESCING);
5037 		ioc_pg1.ProductSpecific = 0;
5038 		rc = mpt3sas_config_set_ioc_pg1(ioc, &mpi_reply, &ioc_pg1);
5039 		if (rc)
5040 			return rc;
5041 		break;
5042 	}
5043 	return 0;
5044 }
5045 
5046 /**
5047  * _base_get_event_diag_triggers - get event diag trigger values from
5048  *				persistent pages
5049  * @ioc : per adapter object
5050  *
5051  * Return: nothing.
5052  */
5053 static int
_base_get_event_diag_triggers(struct MPT3SAS_ADAPTER * ioc)5054 _base_get_event_diag_triggers(struct MPT3SAS_ADAPTER *ioc)
5055 {
5056 	Mpi26DriverTriggerPage2_t trigger_pg2;
5057 	struct SL_WH_EVENT_TRIGGER_T *event_tg;
5058 	MPI26_DRIVER_MPI_EVENT_TIGGER_ENTRY *mpi_event_tg;
5059 	Mpi2ConfigReply_t mpi_reply;
5060 	int r = 0, i = 0;
5061 	u16 count = 0;
5062 	u16 ioc_status;
5063 
5064 	r = mpt3sas_config_get_driver_trigger_pg2(ioc, &mpi_reply,
5065 	    &trigger_pg2);
5066 	if (r)
5067 		return r;
5068 
5069 	ioc_status = le16_to_cpu(mpi_reply.IOCStatus) &
5070 	    MPI2_IOCSTATUS_MASK;
5071 	if (ioc_status != MPI2_IOCSTATUS_SUCCESS) {
5072 		dinitprintk(ioc,
5073 		    ioc_err(ioc,
5074 		    "%s: Failed to get trigger pg2, ioc_status(0x%04x)\n",
5075 		   __func__, ioc_status));
5076 		return 0;
5077 	}
5078 
5079 	if (le16_to_cpu(trigger_pg2.NumMPIEventTrigger)) {
5080 		count = le16_to_cpu(trigger_pg2.NumMPIEventTrigger);
5081 		count = min_t(u16, NUM_VALID_ENTRIES, count);
5082 		ioc->diag_trigger_event.ValidEntries = count;
5083 
5084 		event_tg = &ioc->diag_trigger_event.EventTriggerEntry[0];
5085 		mpi_event_tg = &trigger_pg2.MPIEventTriggers[0];
5086 		for (i = 0; i < count; i++) {
5087 			event_tg->EventValue = le16_to_cpu(
5088 			    mpi_event_tg->MPIEventCode);
5089 			event_tg->LogEntryQualifier = le16_to_cpu(
5090 			    mpi_event_tg->MPIEventCodeSpecific);
5091 			event_tg++;
5092 			mpi_event_tg++;
5093 		}
5094 	}
5095 	return 0;
5096 }
5097 
5098 /**
5099  * _base_get_scsi_diag_triggers - get scsi diag trigger values from
5100  *				persistent pages
5101  * @ioc : per adapter object
5102  *
5103  * Return: 0 on success; otherwise return failure status.
5104  */
5105 static int
_base_get_scsi_diag_triggers(struct MPT3SAS_ADAPTER * ioc)5106 _base_get_scsi_diag_triggers(struct MPT3SAS_ADAPTER *ioc)
5107 {
5108 	Mpi26DriverTriggerPage3_t trigger_pg3;
5109 	struct SL_WH_SCSI_TRIGGER_T *scsi_tg;
5110 	MPI26_DRIVER_SCSI_SENSE_TIGGER_ENTRY *mpi_scsi_tg;
5111 	Mpi2ConfigReply_t mpi_reply;
5112 	int r = 0, i = 0;
5113 	u16 count = 0;
5114 	u16 ioc_status;
5115 
5116 	r = mpt3sas_config_get_driver_trigger_pg3(ioc, &mpi_reply,
5117 	    &trigger_pg3);
5118 	if (r)
5119 		return r;
5120 
5121 	ioc_status = le16_to_cpu(mpi_reply.IOCStatus) &
5122 	    MPI2_IOCSTATUS_MASK;
5123 	if (ioc_status != MPI2_IOCSTATUS_SUCCESS) {
5124 		dinitprintk(ioc,
5125 		    ioc_err(ioc,
5126 		    "%s: Failed to get trigger pg3, ioc_status(0x%04x)\n",
5127 		    __func__, ioc_status));
5128 		return 0;
5129 	}
5130 
5131 	if (le16_to_cpu(trigger_pg3.NumSCSISenseTrigger)) {
5132 		count = le16_to_cpu(trigger_pg3.NumSCSISenseTrigger);
5133 		count = min_t(u16, NUM_VALID_ENTRIES, count);
5134 		ioc->diag_trigger_scsi.ValidEntries = count;
5135 
5136 		scsi_tg = &ioc->diag_trigger_scsi.SCSITriggerEntry[0];
5137 		mpi_scsi_tg = &trigger_pg3.SCSISenseTriggers[0];
5138 		for (i = 0; i < count; i++) {
5139 			scsi_tg->ASCQ = mpi_scsi_tg->ASCQ;
5140 			scsi_tg->ASC = mpi_scsi_tg->ASC;
5141 			scsi_tg->SenseKey = mpi_scsi_tg->SenseKey;
5142 
5143 			scsi_tg++;
5144 			mpi_scsi_tg++;
5145 		}
5146 	}
5147 	return 0;
5148 }
5149 
5150 /**
5151  * _base_get_mpi_diag_triggers - get mpi diag trigger values from
5152  *				persistent pages
5153  * @ioc : per adapter object
5154  *
5155  * Return: 0 on success; otherwise return failure status.
5156  */
5157 static int
_base_get_mpi_diag_triggers(struct MPT3SAS_ADAPTER * ioc)5158 _base_get_mpi_diag_triggers(struct MPT3SAS_ADAPTER *ioc)
5159 {
5160 	Mpi26DriverTriggerPage4_t trigger_pg4;
5161 	struct SL_WH_MPI_TRIGGER_T *status_tg;
5162 	MPI26_DRIVER_IOCSTATUS_LOGINFO_TIGGER_ENTRY *mpi_status_tg;
5163 	Mpi2ConfigReply_t mpi_reply;
5164 	int r = 0, i = 0;
5165 	u16 count = 0;
5166 	u16 ioc_status;
5167 
5168 	r = mpt3sas_config_get_driver_trigger_pg4(ioc, &mpi_reply,
5169 	    &trigger_pg4);
5170 	if (r)
5171 		return r;
5172 
5173 	ioc_status = le16_to_cpu(mpi_reply.IOCStatus) &
5174 	    MPI2_IOCSTATUS_MASK;
5175 	if (ioc_status != MPI2_IOCSTATUS_SUCCESS) {
5176 		dinitprintk(ioc,
5177 		    ioc_err(ioc,
5178 		    "%s: Failed to get trigger pg4, ioc_status(0x%04x)\n",
5179 		    __func__, ioc_status));
5180 		return 0;
5181 	}
5182 
5183 	if (le16_to_cpu(trigger_pg4.NumIOCStatusLogInfoTrigger)) {
5184 		count = le16_to_cpu(trigger_pg4.NumIOCStatusLogInfoTrigger);
5185 		count = min_t(u16, NUM_VALID_ENTRIES, count);
5186 		ioc->diag_trigger_mpi.ValidEntries = count;
5187 
5188 		status_tg = &ioc->diag_trigger_mpi.MPITriggerEntry[0];
5189 		mpi_status_tg = &trigger_pg4.IOCStatusLoginfoTriggers[0];
5190 
5191 		for (i = 0; i < count; i++) {
5192 			status_tg->IOCStatus = le16_to_cpu(
5193 			    mpi_status_tg->IOCStatus);
5194 			status_tg->IocLogInfo = le32_to_cpu(
5195 			    mpi_status_tg->LogInfo);
5196 
5197 			status_tg++;
5198 			mpi_status_tg++;
5199 		}
5200 	}
5201 	return 0;
5202 }
5203 
5204 /**
5205  * _base_get_master_diag_triggers - get master diag trigger values from
5206  *				persistent pages
5207  * @ioc : per adapter object
5208  *
5209  * Return: nothing.
5210  */
5211 static int
_base_get_master_diag_triggers(struct MPT3SAS_ADAPTER * ioc)5212 _base_get_master_diag_triggers(struct MPT3SAS_ADAPTER *ioc)
5213 {
5214 	Mpi26DriverTriggerPage1_t trigger_pg1;
5215 	Mpi2ConfigReply_t mpi_reply;
5216 	int r;
5217 	u16 ioc_status;
5218 
5219 	r = mpt3sas_config_get_driver_trigger_pg1(ioc, &mpi_reply,
5220 	    &trigger_pg1);
5221 	if (r)
5222 		return r;
5223 
5224 	ioc_status = le16_to_cpu(mpi_reply.IOCStatus) &
5225 	    MPI2_IOCSTATUS_MASK;
5226 	if (ioc_status != MPI2_IOCSTATUS_SUCCESS) {
5227 		dinitprintk(ioc,
5228 		    ioc_err(ioc,
5229 		    "%s: Failed to get trigger pg1, ioc_status(0x%04x)\n",
5230 		   __func__, ioc_status));
5231 		return 0;
5232 	}
5233 
5234 	if (le16_to_cpu(trigger_pg1.NumMasterTrigger))
5235 		ioc->diag_trigger_master.MasterData |=
5236 		    le32_to_cpu(
5237 		    trigger_pg1.MasterTriggers[0].MasterTriggerFlags);
5238 	return 0;
5239 }
5240 
5241 /**
5242  * _base_check_for_trigger_pages_support - checks whether HBA FW supports
5243  *					driver trigger pages or not
5244  * @ioc : per adapter object
5245  * @trigger_flags : address where trigger page0's TriggerFlags value is copied
5246  *
5247  * Return: trigger flags mask if HBA FW supports driver trigger pages;
5248  * otherwise returns %-EFAULT if driver trigger pages are not supported by FW or
5249  * return EAGAIN if diag reset occurred due to FW fault and asking the
5250  * caller to retry the command.
5251  *
5252  */
5253 static int
_base_check_for_trigger_pages_support(struct MPT3SAS_ADAPTER * ioc,u32 * trigger_flags)5254 _base_check_for_trigger_pages_support(struct MPT3SAS_ADAPTER *ioc, u32 *trigger_flags)
5255 {
5256 	Mpi26DriverTriggerPage0_t trigger_pg0;
5257 	int r = 0;
5258 	Mpi2ConfigReply_t mpi_reply;
5259 	u16 ioc_status;
5260 
5261 	r = mpt3sas_config_get_driver_trigger_pg0(ioc, &mpi_reply,
5262 	    &trigger_pg0);
5263 	if (r)
5264 		return r;
5265 
5266 	ioc_status = le16_to_cpu(mpi_reply.IOCStatus) &
5267 	    MPI2_IOCSTATUS_MASK;
5268 	if (ioc_status != MPI2_IOCSTATUS_SUCCESS)
5269 		return -EFAULT;
5270 
5271 	*trigger_flags = le16_to_cpu(trigger_pg0.TriggerFlags);
5272 	return 0;
5273 }
5274 
5275 /**
5276  * _base_get_diag_triggers - Retrieve diag trigger values from
5277  *				persistent pages.
5278  * @ioc : per adapter object
5279  *
5280  * Return: zero on success; otherwise return EAGAIN error codes
5281  * asking the caller to retry.
5282  */
5283 static int
_base_get_diag_triggers(struct MPT3SAS_ADAPTER * ioc)5284 _base_get_diag_triggers(struct MPT3SAS_ADAPTER *ioc)
5285 {
5286 	int trigger_flags;
5287 	int r;
5288 
5289 	/*
5290 	 * Default setting of master trigger.
5291 	 */
5292 	ioc->diag_trigger_master.MasterData =
5293 	    (MASTER_TRIGGER_FW_FAULT + MASTER_TRIGGER_ADAPTER_RESET);
5294 
5295 	r = _base_check_for_trigger_pages_support(ioc, &trigger_flags);
5296 	if (r) {
5297 		if (r == -EAGAIN)
5298 			return r;
5299 		/*
5300 		 * Don't go for error handling when FW doesn't support
5301 		 * driver trigger pages.
5302 		 */
5303 		return 0;
5304 	}
5305 
5306 	ioc->supports_trigger_pages = 1;
5307 
5308 	/*
5309 	 * Retrieve master diag trigger values from driver trigger pg1
5310 	 * if master trigger bit enabled in TriggerFlags.
5311 	 */
5312 	if ((u16)trigger_flags &
5313 	    MPI26_DRIVER_TRIGGER0_FLAG_MASTER_TRIGGER_VALID) {
5314 		r = _base_get_master_diag_triggers(ioc);
5315 		if (r)
5316 			return r;
5317 	}
5318 
5319 	/*
5320 	 * Retrieve event diag trigger values from driver trigger pg2
5321 	 * if event trigger bit enabled in TriggerFlags.
5322 	 */
5323 	if ((u16)trigger_flags &
5324 	    MPI26_DRIVER_TRIGGER0_FLAG_MPI_EVENT_TRIGGER_VALID) {
5325 		r = _base_get_event_diag_triggers(ioc);
5326 		if (r)
5327 			return r;
5328 	}
5329 
5330 	/*
5331 	 * Retrieve scsi diag trigger values from driver trigger pg3
5332 	 * if scsi trigger bit enabled in TriggerFlags.
5333 	 */
5334 	if ((u16)trigger_flags &
5335 	    MPI26_DRIVER_TRIGGER0_FLAG_SCSI_SENSE_TRIGGER_VALID) {
5336 		r = _base_get_scsi_diag_triggers(ioc);
5337 		if (r)
5338 			return r;
5339 	}
5340 	/*
5341 	 * Retrieve mpi error diag trigger values from driver trigger pg4
5342 	 * if loginfo trigger bit enabled in TriggerFlags.
5343 	 */
5344 	if ((u16)trigger_flags &
5345 	    MPI26_DRIVER_TRIGGER0_FLAG_LOGINFO_TRIGGER_VALID) {
5346 		r = _base_get_mpi_diag_triggers(ioc);
5347 		if (r)
5348 			return r;
5349 	}
5350 	return 0;
5351 }
5352 
5353 /**
5354  * _base_update_diag_trigger_pages - Update the driver trigger pages after
5355  *			online FW update, in case updated FW supports driver
5356  *			trigger pages.
5357  * @ioc : per adapter object
5358  *
5359  * Return: nothing.
5360  */
5361 static void
_base_update_diag_trigger_pages(struct MPT3SAS_ADAPTER * ioc)5362 _base_update_diag_trigger_pages(struct MPT3SAS_ADAPTER *ioc)
5363 {
5364 
5365 	if (ioc->diag_trigger_master.MasterData)
5366 		mpt3sas_config_update_driver_trigger_pg1(ioc,
5367 		    &ioc->diag_trigger_master, 1);
5368 
5369 	if (ioc->diag_trigger_event.ValidEntries)
5370 		mpt3sas_config_update_driver_trigger_pg2(ioc,
5371 		    &ioc->diag_trigger_event, 1);
5372 
5373 	if (ioc->diag_trigger_scsi.ValidEntries)
5374 		mpt3sas_config_update_driver_trigger_pg3(ioc,
5375 		    &ioc->diag_trigger_scsi, 1);
5376 
5377 	if (ioc->diag_trigger_mpi.ValidEntries)
5378 		mpt3sas_config_update_driver_trigger_pg4(ioc,
5379 		    &ioc->diag_trigger_mpi, 1);
5380 }
5381 
5382 /**
5383  * _base_assign_fw_reported_qd	- Get FW reported QD for SAS/SATA devices.
5384  *				- On failure set default QD values.
5385  * @ioc : per adapter object
5386  *
5387  * Returns 0 for success, non-zero for failure.
5388  *
5389  */
_base_assign_fw_reported_qd(struct MPT3SAS_ADAPTER * ioc)5390 static int _base_assign_fw_reported_qd(struct MPT3SAS_ADAPTER *ioc)
5391 {
5392 	Mpi2ConfigReply_t mpi_reply;
5393 	Mpi2SasIOUnitPage1_t *sas_iounit_pg1 = NULL;
5394 	Mpi26PCIeIOUnitPage1_t pcie_iounit_pg1;
5395 	u16 depth;
5396 	int sz;
5397 	int rc = 0;
5398 
5399 	ioc->max_wideport_qd = MPT3SAS_SAS_QUEUE_DEPTH;
5400 	ioc->max_narrowport_qd = MPT3SAS_SAS_QUEUE_DEPTH;
5401 	ioc->max_sata_qd = MPT3SAS_SATA_QUEUE_DEPTH;
5402 	ioc->max_nvme_qd = MPT3SAS_NVME_QUEUE_DEPTH;
5403 	if (!ioc->is_gen35_ioc)
5404 		goto out;
5405 	/* sas iounit page 1 */
5406 	sz = offsetof(Mpi2SasIOUnitPage1_t, PhyData);
5407 	sas_iounit_pg1 = kzalloc(sizeof(Mpi2SasIOUnitPage1_t), GFP_KERNEL);
5408 	if (!sas_iounit_pg1) {
5409 		pr_err("%s: failure at %s:%d/%s()!\n",
5410 		    ioc->name, __FILE__, __LINE__, __func__);
5411 		return rc;
5412 	}
5413 	rc = mpt3sas_config_get_sas_iounit_pg1(ioc, &mpi_reply,
5414 	    sas_iounit_pg1, sz);
5415 	if (rc) {
5416 		pr_err("%s: failure at %s:%d/%s()!\n",
5417 		    ioc->name, __FILE__, __LINE__, __func__);
5418 		goto out;
5419 	}
5420 
5421 	depth = le16_to_cpu(sas_iounit_pg1->SASWideMaxQueueDepth);
5422 	ioc->max_wideport_qd = (depth ? depth : MPT3SAS_SAS_QUEUE_DEPTH);
5423 
5424 	depth = le16_to_cpu(sas_iounit_pg1->SASNarrowMaxQueueDepth);
5425 	ioc->max_narrowport_qd = (depth ? depth : MPT3SAS_SAS_QUEUE_DEPTH);
5426 
5427 	depth = sas_iounit_pg1->SATAMaxQDepth;
5428 	ioc->max_sata_qd = (depth ? depth : MPT3SAS_SATA_QUEUE_DEPTH);
5429 
5430 	/* pcie iounit page 1 */
5431 	rc = mpt3sas_config_get_pcie_iounit_pg1(ioc, &mpi_reply,
5432 	    &pcie_iounit_pg1, sizeof(Mpi26PCIeIOUnitPage1_t));
5433 	if (rc) {
5434 		pr_err("%s: failure at %s:%d/%s()!\n",
5435 		    ioc->name, __FILE__, __LINE__, __func__);
5436 		goto out;
5437 	}
5438 	ioc->max_nvme_qd = (le16_to_cpu(pcie_iounit_pg1.NVMeMaxQueueDepth)) ?
5439 	    (le16_to_cpu(pcie_iounit_pg1.NVMeMaxQueueDepth)) :
5440 	    MPT3SAS_NVME_QUEUE_DEPTH;
5441 out:
5442 	dinitprintk(ioc, pr_err(
5443 	    "MaxWidePortQD: 0x%x MaxNarrowPortQD: 0x%x MaxSataQD: 0x%x MaxNvmeQD: 0x%x\n",
5444 	    ioc->max_wideport_qd, ioc->max_narrowport_qd,
5445 	    ioc->max_sata_qd, ioc->max_nvme_qd));
5446 	kfree(sas_iounit_pg1);
5447 	return rc;
5448 }
5449 
5450 /**
5451  * _base_static_config_pages - static start of day config pages
5452  * @ioc: per adapter object
5453  */
5454 static int
_base_static_config_pages(struct MPT3SAS_ADAPTER * ioc)5455 _base_static_config_pages(struct MPT3SAS_ADAPTER *ioc)
5456 {
5457 	Mpi2ConfigReply_t mpi_reply;
5458 	u32 iounit_pg1_flags;
5459 	int tg_flags = 0;
5460 	int rc;
5461 	ioc->nvme_abort_timeout = 30;
5462 
5463 	rc = mpt3sas_config_get_manufacturing_pg0(ioc, &mpi_reply,
5464 	    &ioc->manu_pg0);
5465 	if (rc)
5466 		return rc;
5467 	if (ioc->ir_firmware) {
5468 		rc = mpt3sas_config_get_manufacturing_pg10(ioc, &mpi_reply,
5469 		    &ioc->manu_pg10);
5470 		if (rc)
5471 			return rc;
5472 	}
5473 	/*
5474 	 * Ensure correct T10 PI operation if vendor left EEDPTagMode
5475 	 * flag unset in NVDATA.
5476 	 */
5477 	rc = mpt3sas_config_get_manufacturing_pg11(ioc, &mpi_reply,
5478 	    &ioc->manu_pg11);
5479 	if (rc)
5480 		return rc;
5481 	if (!ioc->is_gen35_ioc && ioc->manu_pg11.EEDPTagMode == 0) {
5482 		pr_err("%s: overriding NVDATA EEDPTagMode setting\n",
5483 		    ioc->name);
5484 		ioc->manu_pg11.EEDPTagMode &= ~0x3;
5485 		ioc->manu_pg11.EEDPTagMode |= 0x1;
5486 		mpt3sas_config_set_manufacturing_pg11(ioc, &mpi_reply,
5487 		    &ioc->manu_pg11);
5488 	}
5489 	if (ioc->manu_pg11.AddlFlags2 & NVME_TASK_MNGT_CUSTOM_MASK)
5490 		ioc->tm_custom_handling = 1;
5491 	else {
5492 		ioc->tm_custom_handling = 0;
5493 		if (ioc->manu_pg11.NVMeAbortTO < NVME_TASK_ABORT_MIN_TIMEOUT)
5494 			ioc->nvme_abort_timeout = NVME_TASK_ABORT_MIN_TIMEOUT;
5495 		else if (ioc->manu_pg11.NVMeAbortTO >
5496 					NVME_TASK_ABORT_MAX_TIMEOUT)
5497 			ioc->nvme_abort_timeout = NVME_TASK_ABORT_MAX_TIMEOUT;
5498 		else
5499 			ioc->nvme_abort_timeout = ioc->manu_pg11.NVMeAbortTO;
5500 	}
5501 	ioc->time_sync_interval =
5502 	    ioc->manu_pg11.TimeSyncInterval & MPT3SAS_TIMESYNC_MASK;
5503 	if (ioc->time_sync_interval) {
5504 		if (ioc->manu_pg11.TimeSyncInterval & MPT3SAS_TIMESYNC_UNIT_MASK)
5505 			ioc->time_sync_interval =
5506 			    ioc->time_sync_interval * SECONDS_PER_HOUR;
5507 		else
5508 			ioc->time_sync_interval =
5509 			    ioc->time_sync_interval * SECONDS_PER_MIN;
5510 		dinitprintk(ioc, ioc_info(ioc,
5511 		    "Driver-FW TimeSync interval is %d seconds. ManuPg11 TimeSync Unit is in %s\n",
5512 		    ioc->time_sync_interval, (ioc->manu_pg11.TimeSyncInterval &
5513 		    MPT3SAS_TIMESYNC_UNIT_MASK) ? "Hour" : "Minute"));
5514 	} else {
5515 		if (ioc->is_gen35_ioc)
5516 			ioc_warn(ioc,
5517 			    "TimeSync Interval in Manuf page-11 is not enabled. Periodic Time-Sync will be disabled\n");
5518 	}
5519 	rc = _base_assign_fw_reported_qd(ioc);
5520 	if (rc)
5521 		return rc;
5522 	rc = mpt3sas_config_get_bios_pg2(ioc, &mpi_reply, &ioc->bios_pg2);
5523 	if (rc)
5524 		return rc;
5525 	rc = mpt3sas_config_get_bios_pg3(ioc, &mpi_reply, &ioc->bios_pg3);
5526 	if (rc)
5527 		return rc;
5528 	rc = mpt3sas_config_get_ioc_pg8(ioc, &mpi_reply, &ioc->ioc_pg8);
5529 	if (rc)
5530 		return rc;
5531 	rc = mpt3sas_config_get_iounit_pg0(ioc, &mpi_reply, &ioc->iounit_pg0);
5532 	if (rc)
5533 		return rc;
5534 	rc = mpt3sas_config_get_iounit_pg1(ioc, &mpi_reply, &ioc->iounit_pg1);
5535 	if (rc)
5536 		return rc;
5537 	rc = mpt3sas_config_get_iounit_pg8(ioc, &mpi_reply, &ioc->iounit_pg8);
5538 	if (rc)
5539 		return rc;
5540 	_base_display_ioc_capabilities(ioc);
5541 
5542 	/*
5543 	 * Enable task_set_full handling in iounit_pg1 when the
5544 	 * facts capabilities indicate that its supported.
5545 	 */
5546 	iounit_pg1_flags = le32_to_cpu(ioc->iounit_pg1.Flags);
5547 	if ((ioc->facts.IOCCapabilities &
5548 	    MPI2_IOCFACTS_CAPABILITY_TASK_SET_FULL_HANDLING))
5549 		iounit_pg1_flags &=
5550 		    ~MPI2_IOUNITPAGE1_DISABLE_TASK_SET_FULL_HANDLING;
5551 	else
5552 		iounit_pg1_flags |=
5553 		    MPI2_IOUNITPAGE1_DISABLE_TASK_SET_FULL_HANDLING;
5554 	ioc->iounit_pg1.Flags = cpu_to_le32(iounit_pg1_flags);
5555 	rc = mpt3sas_config_set_iounit_pg1(ioc, &mpi_reply, &ioc->iounit_pg1);
5556 	if (rc)
5557 		return rc;
5558 
5559 	if (ioc->iounit_pg8.NumSensors)
5560 		ioc->temp_sensors_count = ioc->iounit_pg8.NumSensors;
5561 	if (ioc->is_aero_ioc) {
5562 		rc = _base_update_ioc_page1_inlinewith_perf_mode(ioc);
5563 		if (rc)
5564 			return rc;
5565 	}
5566 	if (ioc->is_gen35_ioc) {
5567 		if (ioc->is_driver_loading) {
5568 			rc = _base_get_diag_triggers(ioc);
5569 			if (rc)
5570 				return rc;
5571 		} else {
5572 			/*
5573 			 * In case of online HBA FW update operation,
5574 			 * check whether updated FW supports the driver trigger
5575 			 * pages or not.
5576 			 * - If previous FW has not supported driver trigger
5577 			 *   pages and newer FW supports them then update these
5578 			 *   pages with current diag trigger values.
5579 			 * - If previous FW has supported driver trigger pages
5580 			 *   and new FW doesn't support them then disable
5581 			 *   support_trigger_pages flag.
5582 			 */
5583 			_base_check_for_trigger_pages_support(ioc, &tg_flags);
5584 			if (!ioc->supports_trigger_pages && tg_flags != -EFAULT)
5585 				_base_update_diag_trigger_pages(ioc);
5586 			else if (ioc->supports_trigger_pages &&
5587 			    tg_flags == -EFAULT)
5588 				ioc->supports_trigger_pages = 0;
5589 		}
5590 	}
5591 	return 0;
5592 }
5593 
5594 /**
5595  * mpt3sas_free_enclosure_list - release memory
5596  * @ioc: per adapter object
5597  *
5598  * Free memory allocated during enclosure add.
5599  */
5600 void
mpt3sas_free_enclosure_list(struct MPT3SAS_ADAPTER * ioc)5601 mpt3sas_free_enclosure_list(struct MPT3SAS_ADAPTER *ioc)
5602 {
5603 	struct _enclosure_node *enclosure_dev, *enclosure_dev_next;
5604 
5605 	/* Free enclosure list */
5606 	list_for_each_entry_safe(enclosure_dev,
5607 			enclosure_dev_next, &ioc->enclosure_list, list) {
5608 		list_del(&enclosure_dev->list);
5609 		kfree(enclosure_dev);
5610 	}
5611 }
5612 
5613 /**
5614  * _base_release_memory_pools - release memory
5615  * @ioc: per adapter object
5616  *
5617  * Free memory allocated from _base_allocate_memory_pools.
5618  */
5619 static void
_base_release_memory_pools(struct MPT3SAS_ADAPTER * ioc)5620 _base_release_memory_pools(struct MPT3SAS_ADAPTER *ioc)
5621 {
5622 	int i = 0;
5623 	int j = 0;
5624 	int dma_alloc_count = 0;
5625 	struct chain_tracker *ct;
5626 	int count = ioc->rdpq_array_enable ? ioc->reply_queue_count : 1;
5627 
5628 	dexitprintk(ioc, ioc_info(ioc, "%s\n", __func__));
5629 
5630 	if (ioc->request) {
5631 		dma_free_coherent(&ioc->pdev->dev, ioc->request_dma_sz,
5632 		    ioc->request,  ioc->request_dma);
5633 		dexitprintk(ioc,
5634 			    ioc_info(ioc, "request_pool(0x%p): free\n",
5635 				     ioc->request));
5636 		ioc->request = NULL;
5637 	}
5638 
5639 	if (ioc->sense) {
5640 		dma_pool_free(ioc->sense_dma_pool, ioc->sense, ioc->sense_dma);
5641 		dma_pool_destroy(ioc->sense_dma_pool);
5642 		dexitprintk(ioc,
5643 			    ioc_info(ioc, "sense_pool(0x%p): free\n",
5644 				     ioc->sense));
5645 		ioc->sense = NULL;
5646 	}
5647 
5648 	if (ioc->reply) {
5649 		dma_pool_free(ioc->reply_dma_pool, ioc->reply, ioc->reply_dma);
5650 		dma_pool_destroy(ioc->reply_dma_pool);
5651 		dexitprintk(ioc,
5652 			    ioc_info(ioc, "reply_pool(0x%p): free\n",
5653 				     ioc->reply));
5654 		ioc->reply = NULL;
5655 	}
5656 
5657 	if (ioc->reply_free) {
5658 		dma_pool_free(ioc->reply_free_dma_pool, ioc->reply_free,
5659 		    ioc->reply_free_dma);
5660 		dma_pool_destroy(ioc->reply_free_dma_pool);
5661 		dexitprintk(ioc,
5662 			    ioc_info(ioc, "reply_free_pool(0x%p): free\n",
5663 				     ioc->reply_free));
5664 		ioc->reply_free = NULL;
5665 	}
5666 
5667 	if (ioc->reply_post) {
5668 		dma_alloc_count = DIV_ROUND_UP(count,
5669 				RDPQ_MAX_INDEX_IN_ONE_CHUNK);
5670 		for (i = 0; i < count; i++) {
5671 			if (i % RDPQ_MAX_INDEX_IN_ONE_CHUNK == 0
5672 			    && dma_alloc_count) {
5673 				if (ioc->reply_post[i].reply_post_free) {
5674 					dma_pool_free(
5675 					    ioc->reply_post_free_dma_pool,
5676 					    ioc->reply_post[i].reply_post_free,
5677 					ioc->reply_post[i].reply_post_free_dma);
5678 					dexitprintk(ioc, ioc_info(ioc,
5679 					   "reply_post_free_pool(0x%p): free\n",
5680 					   ioc->reply_post[i].reply_post_free));
5681 					ioc->reply_post[i].reply_post_free =
5682 									NULL;
5683 				}
5684 				--dma_alloc_count;
5685 			}
5686 		}
5687 		dma_pool_destroy(ioc->reply_post_free_dma_pool);
5688 		if (ioc->reply_post_free_array &&
5689 			ioc->rdpq_array_enable) {
5690 			dma_pool_free(ioc->reply_post_free_array_dma_pool,
5691 			    ioc->reply_post_free_array,
5692 			    ioc->reply_post_free_array_dma);
5693 			ioc->reply_post_free_array = NULL;
5694 		}
5695 		dma_pool_destroy(ioc->reply_post_free_array_dma_pool);
5696 		kfree(ioc->reply_post);
5697 	}
5698 
5699 	if (ioc->pcie_sgl_dma_pool) {
5700 		for (i = 0; i < ioc->scsiio_depth; i++) {
5701 			dma_pool_free(ioc->pcie_sgl_dma_pool,
5702 					ioc->pcie_sg_lookup[i].pcie_sgl,
5703 					ioc->pcie_sg_lookup[i].pcie_sgl_dma);
5704 			ioc->pcie_sg_lookup[i].pcie_sgl = NULL;
5705 		}
5706 		dma_pool_destroy(ioc->pcie_sgl_dma_pool);
5707 	}
5708 	kfree(ioc->pcie_sg_lookup);
5709 	ioc->pcie_sg_lookup = NULL;
5710 
5711 	if (ioc->config_page) {
5712 		dexitprintk(ioc,
5713 			    ioc_info(ioc, "config_page(0x%p): free\n",
5714 				     ioc->config_page));
5715 		dma_free_coherent(&ioc->pdev->dev, ioc->config_page_sz,
5716 		    ioc->config_page, ioc->config_page_dma);
5717 	}
5718 
5719 	kfree(ioc->hpr_lookup);
5720 	ioc->hpr_lookup = NULL;
5721 	kfree(ioc->internal_lookup);
5722 	ioc->internal_lookup = NULL;
5723 	if (ioc->chain_lookup) {
5724 		for (i = 0; i < ioc->scsiio_depth; i++) {
5725 			for (j = ioc->chains_per_prp_buffer;
5726 			    j < ioc->chains_needed_per_io; j++) {
5727 				ct = &ioc->chain_lookup[i].chains_per_smid[j];
5728 				if (ct && ct->chain_buffer)
5729 					dma_pool_free(ioc->chain_dma_pool,
5730 						ct->chain_buffer,
5731 						ct->chain_buffer_dma);
5732 			}
5733 			kfree(ioc->chain_lookup[i].chains_per_smid);
5734 		}
5735 		dma_pool_destroy(ioc->chain_dma_pool);
5736 		kfree(ioc->chain_lookup);
5737 		ioc->chain_lookup = NULL;
5738 	}
5739 
5740 	kfree(ioc->io_queue_num);
5741 	ioc->io_queue_num = NULL;
5742 }
5743 
5744 /**
5745  * mpt3sas_check_same_4gb_region - checks whether all reply queues in a set are
5746  *	having same upper 32bits in their base memory address.
5747  * @reply_pool_start_address: Base address of a reply queue set
5748  * @pool_sz: Size of single Reply Descriptor Post Queues pool size
5749  *
5750  * Return: 1 if reply queues in a set have a same upper 32bits in their base
5751  * memory address, else 0.
5752  */
5753 
5754 static int
mpt3sas_check_same_4gb_region(dma_addr_t start_address,u32 pool_sz)5755 mpt3sas_check_same_4gb_region(dma_addr_t start_address, u32 pool_sz)
5756 {
5757 	dma_addr_t end_address;
5758 
5759 	end_address = start_address + pool_sz - 1;
5760 
5761 	if (upper_32_bits(start_address) == upper_32_bits(end_address))
5762 		return 1;
5763 	else
5764 		return 0;
5765 }
5766 
5767 /**
5768  * _base_reduce_hba_queue_depth- Retry with reduced queue depth
5769  * @ioc: Adapter object
5770  *
5771  * Return: 0 for success, non-zero for failure.
5772  **/
5773 static inline int
_base_reduce_hba_queue_depth(struct MPT3SAS_ADAPTER * ioc)5774 _base_reduce_hba_queue_depth(struct MPT3SAS_ADAPTER *ioc)
5775 {
5776 	int reduce_sz = 64;
5777 
5778 	if ((ioc->hba_queue_depth - reduce_sz) >
5779 	    (ioc->internal_depth + INTERNAL_SCSIIO_CMDS_COUNT)) {
5780 		ioc->hba_queue_depth -= reduce_sz;
5781 		return 0;
5782 	} else
5783 		return -ENOMEM;
5784 }
5785 
5786 /**
5787  * _base_allocate_pcie_sgl_pool - Allocating DMA'able memory
5788  *			for pcie sgl pools.
5789  * @ioc: Adapter object
5790  * @sz: DMA Pool size
5791  *
5792  * Return: 0 for success, non-zero for failure.
5793  */
5794 
5795 static int
_base_allocate_pcie_sgl_pool(struct MPT3SAS_ADAPTER * ioc,u32 sz)5796 _base_allocate_pcie_sgl_pool(struct MPT3SAS_ADAPTER *ioc, u32 sz)
5797 {
5798 	int i = 0, j = 0;
5799 	struct chain_tracker *ct;
5800 
5801 	ioc->pcie_sgl_dma_pool =
5802 	    dma_pool_create("PCIe SGL pool", &ioc->pdev->dev, sz,
5803 	    ioc->page_size, 0);
5804 	if (!ioc->pcie_sgl_dma_pool) {
5805 		ioc_err(ioc, "PCIe SGL pool: dma_pool_create failed\n");
5806 		return -ENOMEM;
5807 	}
5808 
5809 	ioc->chains_per_prp_buffer = sz/ioc->chain_segment_sz;
5810 	ioc->chains_per_prp_buffer =
5811 	    min(ioc->chains_per_prp_buffer, ioc->chains_needed_per_io);
5812 	for (i = 0; i < ioc->scsiio_depth; i++) {
5813 		ioc->pcie_sg_lookup[i].pcie_sgl =
5814 		    dma_pool_alloc(ioc->pcie_sgl_dma_pool, GFP_KERNEL,
5815 		    &ioc->pcie_sg_lookup[i].pcie_sgl_dma);
5816 		if (!ioc->pcie_sg_lookup[i].pcie_sgl) {
5817 			ioc_err(ioc, "PCIe SGL pool: dma_pool_alloc failed\n");
5818 			return -EAGAIN;
5819 		}
5820 
5821 		if (!mpt3sas_check_same_4gb_region(
5822 		    ioc->pcie_sg_lookup[i].pcie_sgl_dma, sz)) {
5823 			ioc_err(ioc, "PCIE SGLs are not in same 4G !! pcie sgl (0x%p) dma = (0x%llx)\n",
5824 			    ioc->pcie_sg_lookup[i].pcie_sgl,
5825 			    (unsigned long long)
5826 			    ioc->pcie_sg_lookup[i].pcie_sgl_dma);
5827 			ioc->use_32bit_dma = true;
5828 			return -EAGAIN;
5829 		}
5830 
5831 		for (j = 0; j < ioc->chains_per_prp_buffer; j++) {
5832 			ct = &ioc->chain_lookup[i].chains_per_smid[j];
5833 			ct->chain_buffer =
5834 			    ioc->pcie_sg_lookup[i].pcie_sgl +
5835 			    (j * ioc->chain_segment_sz);
5836 			ct->chain_buffer_dma =
5837 			    ioc->pcie_sg_lookup[i].pcie_sgl_dma +
5838 			    (j * ioc->chain_segment_sz);
5839 		}
5840 	}
5841 	dinitprintk(ioc, ioc_info(ioc,
5842 	    "PCIe sgl pool depth(%d), element_size(%d), pool_size(%d kB)\n",
5843 	    ioc->scsiio_depth, sz, (sz * ioc->scsiio_depth)/1024));
5844 	dinitprintk(ioc, ioc_info(ioc,
5845 	    "Number of chains can fit in a PRP page(%d)\n",
5846 	    ioc->chains_per_prp_buffer));
5847 	return 0;
5848 }
5849 
5850 /**
5851  * _base_allocate_chain_dma_pool - Allocating DMA'able memory
5852  *			for chain dma pool.
5853  * @ioc: Adapter object
5854  * @sz: DMA Pool size
5855  *
5856  * Return: 0 for success, non-zero for failure.
5857  */
5858 static int
_base_allocate_chain_dma_pool(struct MPT3SAS_ADAPTER * ioc,u32 sz)5859 _base_allocate_chain_dma_pool(struct MPT3SAS_ADAPTER *ioc, u32 sz)
5860 {
5861 	int i = 0, j = 0;
5862 	struct chain_tracker *ctr;
5863 
5864 	ioc->chain_dma_pool = dma_pool_create("chain pool", &ioc->pdev->dev,
5865 	    ioc->chain_segment_sz, 16, 0);
5866 	if (!ioc->chain_dma_pool)
5867 		return -ENOMEM;
5868 
5869 	for (i = 0; i < ioc->scsiio_depth; i++) {
5870 		for (j = ioc->chains_per_prp_buffer;
5871 		    j < ioc->chains_needed_per_io; j++) {
5872 			ctr = &ioc->chain_lookup[i].chains_per_smid[j];
5873 			ctr->chain_buffer = dma_pool_alloc(ioc->chain_dma_pool,
5874 			    GFP_KERNEL, &ctr->chain_buffer_dma);
5875 			if (!ctr->chain_buffer)
5876 				return -EAGAIN;
5877 			if (!mpt3sas_check_same_4gb_region(
5878 			    ctr->chain_buffer_dma, ioc->chain_segment_sz)) {
5879 				ioc_err(ioc,
5880 				    "Chain buffers are not in same 4G !!! Chain buff (0x%p) dma = (0x%llx)\n",
5881 				    ctr->chain_buffer,
5882 				    (unsigned long long)ctr->chain_buffer_dma);
5883 				ioc->use_32bit_dma = true;
5884 				return -EAGAIN;
5885 			}
5886 		}
5887 	}
5888 	dinitprintk(ioc, ioc_info(ioc,
5889 	    "chain_lookup depth (%d), frame_size(%d), pool_size(%d kB)\n",
5890 	    ioc->scsiio_depth, ioc->chain_segment_sz, ((ioc->scsiio_depth *
5891 	    (ioc->chains_needed_per_io - ioc->chains_per_prp_buffer) *
5892 	    ioc->chain_segment_sz))/1024));
5893 	return 0;
5894 }
5895 
5896 /**
5897  * _base_allocate_sense_dma_pool - Allocating DMA'able memory
5898  *			for sense dma pool.
5899  * @ioc: Adapter object
5900  * @sz: DMA Pool size
5901  * Return: 0 for success, non-zero for failure.
5902  */
5903 static int
_base_allocate_sense_dma_pool(struct MPT3SAS_ADAPTER * ioc,u32 sz)5904 _base_allocate_sense_dma_pool(struct MPT3SAS_ADAPTER *ioc, u32 sz)
5905 {
5906 	ioc->sense_dma_pool =
5907 	    dma_pool_create("sense pool", &ioc->pdev->dev, sz, 4, 0);
5908 	if (!ioc->sense_dma_pool)
5909 		return -ENOMEM;
5910 	ioc->sense = dma_pool_alloc(ioc->sense_dma_pool,
5911 	    GFP_KERNEL, &ioc->sense_dma);
5912 	if (!ioc->sense)
5913 		return -EAGAIN;
5914 	if (!mpt3sas_check_same_4gb_region(ioc->sense_dma, sz)) {
5915 		dinitprintk(ioc, pr_err(
5916 		    "Bad Sense Pool! sense (0x%p) sense_dma = (0x%llx)\n",
5917 		    ioc->sense, (unsigned long long) ioc->sense_dma));
5918 		ioc->use_32bit_dma = true;
5919 		return -EAGAIN;
5920 	}
5921 	ioc_info(ioc,
5922 	    "sense pool(0x%p) - dma(0x%llx): depth(%d), element_size(%d), pool_size (%d kB)\n",
5923 	    ioc->sense, (unsigned long long)ioc->sense_dma,
5924 	    ioc->scsiio_depth, SCSI_SENSE_BUFFERSIZE, sz/1024);
5925 	return 0;
5926 }
5927 
5928 /**
5929  * _base_allocate_reply_pool - Allocating DMA'able memory
5930  *			for reply pool.
5931  * @ioc: Adapter object
5932  * @sz: DMA Pool size
5933  * Return: 0 for success, non-zero for failure.
5934  */
5935 static int
_base_allocate_reply_pool(struct MPT3SAS_ADAPTER * ioc,u32 sz)5936 _base_allocate_reply_pool(struct MPT3SAS_ADAPTER *ioc, u32 sz)
5937 {
5938 	/* reply pool, 4 byte align */
5939 	ioc->reply_dma_pool = dma_pool_create("reply pool",
5940 	    &ioc->pdev->dev, sz, 4, 0);
5941 	if (!ioc->reply_dma_pool)
5942 		return -ENOMEM;
5943 	ioc->reply = dma_pool_alloc(ioc->reply_dma_pool, GFP_KERNEL,
5944 	    &ioc->reply_dma);
5945 	if (!ioc->reply)
5946 		return -EAGAIN;
5947 	if (!mpt3sas_check_same_4gb_region(ioc->reply_dma, sz)) {
5948 		dinitprintk(ioc, pr_err(
5949 		    "Bad Reply Pool! Reply (0x%p) Reply dma = (0x%llx)\n",
5950 		    ioc->reply, (unsigned long long) ioc->reply_dma));
5951 		ioc->use_32bit_dma = true;
5952 		return -EAGAIN;
5953 	}
5954 	ioc->reply_dma_min_address = (u32)(ioc->reply_dma);
5955 	ioc->reply_dma_max_address = (u32)(ioc->reply_dma) + sz;
5956 	ioc_info(ioc,
5957 	    "reply pool(0x%p) - dma(0x%llx): depth(%d), frame_size(%d), pool_size(%d kB)\n",
5958 	    ioc->reply, (unsigned long long)ioc->reply_dma,
5959 	    ioc->reply_free_queue_depth, ioc->reply_sz, sz/1024);
5960 	return 0;
5961 }
5962 
5963 /**
5964  * _base_allocate_reply_free_dma_pool - Allocating DMA'able memory
5965  *			for reply free dma pool.
5966  * @ioc: Adapter object
5967  * @sz: DMA Pool size
5968  * Return: 0 for success, non-zero for failure.
5969  */
5970 static int
_base_allocate_reply_free_dma_pool(struct MPT3SAS_ADAPTER * ioc,u32 sz)5971 _base_allocate_reply_free_dma_pool(struct MPT3SAS_ADAPTER *ioc, u32 sz)
5972 {
5973 	/* reply free queue, 16 byte align */
5974 	ioc->reply_free_dma_pool = dma_pool_create(
5975 	    "reply_free pool", &ioc->pdev->dev, sz, 16, 0);
5976 	if (!ioc->reply_free_dma_pool)
5977 		return -ENOMEM;
5978 	ioc->reply_free = dma_pool_alloc(ioc->reply_free_dma_pool,
5979 	    GFP_KERNEL, &ioc->reply_free_dma);
5980 	if (!ioc->reply_free)
5981 		return -EAGAIN;
5982 	if (!mpt3sas_check_same_4gb_region(ioc->reply_free_dma, sz)) {
5983 		dinitprintk(ioc,
5984 		    pr_err("Bad Reply Free Pool! Reply Free (0x%p) Reply Free dma = (0x%llx)\n",
5985 		    ioc->reply_free, (unsigned long long) ioc->reply_free_dma));
5986 		ioc->use_32bit_dma = true;
5987 		return -EAGAIN;
5988 	}
5989 	memset(ioc->reply_free, 0, sz);
5990 	dinitprintk(ioc, ioc_info(ioc,
5991 	    "reply_free pool(0x%p): depth(%d), element_size(%d), pool_size(%d kB)\n",
5992 	    ioc->reply_free, ioc->reply_free_queue_depth, 4, sz/1024));
5993 	dinitprintk(ioc, ioc_info(ioc,
5994 	    "reply_free_dma (0x%llx)\n",
5995 	    (unsigned long long)ioc->reply_free_dma));
5996 	return 0;
5997 }
5998 
5999 /**
6000  * _base_allocate_reply_post_free_array - Allocating DMA'able memory
6001  *			for reply post free array.
6002  * @ioc: Adapter object
6003  * @reply_post_free_array_sz: DMA Pool size
6004  * Return: 0 for success, non-zero for failure.
6005  */
6006 
6007 static int
_base_allocate_reply_post_free_array(struct MPT3SAS_ADAPTER * ioc,u32 reply_post_free_array_sz)6008 _base_allocate_reply_post_free_array(struct MPT3SAS_ADAPTER *ioc,
6009 	u32 reply_post_free_array_sz)
6010 {
6011 	ioc->reply_post_free_array_dma_pool =
6012 	    dma_pool_create("reply_post_free_array pool",
6013 	    &ioc->pdev->dev, reply_post_free_array_sz, 16, 0);
6014 	if (!ioc->reply_post_free_array_dma_pool)
6015 		return -ENOMEM;
6016 	ioc->reply_post_free_array =
6017 	    dma_pool_alloc(ioc->reply_post_free_array_dma_pool,
6018 	    GFP_KERNEL, &ioc->reply_post_free_array_dma);
6019 	if (!ioc->reply_post_free_array)
6020 		return -EAGAIN;
6021 	if (!mpt3sas_check_same_4gb_region(ioc->reply_post_free_array_dma,
6022 	    reply_post_free_array_sz)) {
6023 		dinitprintk(ioc, pr_err(
6024 		    "Bad Reply Free Pool! Reply Free (0x%p) Reply Free dma = (0x%llx)\n",
6025 		    ioc->reply_free,
6026 		    (unsigned long long) ioc->reply_free_dma));
6027 		ioc->use_32bit_dma = true;
6028 		return -EAGAIN;
6029 	}
6030 	return 0;
6031 }
6032 /**
6033  * base_alloc_rdpq_dma_pool - Allocating DMA'able memory
6034  *                     for reply queues.
6035  * @ioc: per adapter object
6036  * @sz: DMA Pool size
6037  * Return: 0 for success, non-zero for failure.
6038  */
6039 static int
base_alloc_rdpq_dma_pool(struct MPT3SAS_ADAPTER * ioc,int sz)6040 base_alloc_rdpq_dma_pool(struct MPT3SAS_ADAPTER *ioc, int sz)
6041 {
6042 	int i = 0;
6043 	u32 dma_alloc_count = 0;
6044 	int reply_post_free_sz = ioc->reply_post_queue_depth *
6045 		sizeof(Mpi2DefaultReplyDescriptor_t);
6046 	int count = ioc->rdpq_array_enable ? ioc->reply_queue_count : 1;
6047 
6048 	ioc->reply_post = kcalloc(count, sizeof(struct reply_post_struct),
6049 			GFP_KERNEL);
6050 	if (!ioc->reply_post)
6051 		return -ENOMEM;
6052 	/*
6053 	 *  For INVADER_SERIES each set of 8 reply queues(0-7, 8-15, ..) and
6054 	 *  VENTURA_SERIES each set of 16 reply queues(0-15, 16-31, ..) should
6055 	 *  be within 4GB boundary i.e reply queues in a set must have same
6056 	 *  upper 32-bits in their memory address. so here driver is allocating
6057 	 *  the DMA'able memory for reply queues according.
6058 	 *  Driver uses limitation of
6059 	 *  VENTURA_SERIES to manage INVADER_SERIES as well.
6060 	 */
6061 	dma_alloc_count = DIV_ROUND_UP(count,
6062 				RDPQ_MAX_INDEX_IN_ONE_CHUNK);
6063 	ioc->reply_post_free_dma_pool =
6064 		dma_pool_create("reply_post_free pool",
6065 		    &ioc->pdev->dev, sz, 16, 0);
6066 	if (!ioc->reply_post_free_dma_pool)
6067 		return -ENOMEM;
6068 	for (i = 0; i < count; i++) {
6069 		if ((i % RDPQ_MAX_INDEX_IN_ONE_CHUNK == 0) && dma_alloc_count) {
6070 			ioc->reply_post[i].reply_post_free =
6071 			    dma_pool_zalloc(ioc->reply_post_free_dma_pool,
6072 				GFP_KERNEL,
6073 				&ioc->reply_post[i].reply_post_free_dma);
6074 			if (!ioc->reply_post[i].reply_post_free)
6075 				return -ENOMEM;
6076 			/*
6077 			 * Each set of RDPQ pool must satisfy 4gb boundary
6078 			 * restriction.
6079 			 * 1) Check if allocated resources for RDPQ pool are in
6080 			 *	the same 4GB range.
6081 			 * 2) If #1 is true, continue with 64 bit DMA.
6082 			 * 3) If #1 is false, return 1. which means free all the
6083 			 * resources and set DMA mask to 32 and allocate.
6084 			 */
6085 			if (!mpt3sas_check_same_4gb_region(
6086 				ioc->reply_post[i].reply_post_free_dma, sz)) {
6087 				dinitprintk(ioc,
6088 				    ioc_err(ioc, "bad Replypost free pool(0x%p)"
6089 				    "reply_post_free_dma = (0x%llx)\n",
6090 				    ioc->reply_post[i].reply_post_free,
6091 				    (unsigned long long)
6092 				    ioc->reply_post[i].reply_post_free_dma));
6093 				return -EAGAIN;
6094 			}
6095 			dma_alloc_count--;
6096 
6097 		} else {
6098 			ioc->reply_post[i].reply_post_free =
6099 			    (Mpi2ReplyDescriptorsUnion_t *)
6100 			    ((long)ioc->reply_post[i-1].reply_post_free
6101 			    + reply_post_free_sz);
6102 			ioc->reply_post[i].reply_post_free_dma =
6103 			    (dma_addr_t)
6104 			    (ioc->reply_post[i-1].reply_post_free_dma +
6105 			    reply_post_free_sz);
6106 		}
6107 	}
6108 	return 0;
6109 }
6110 
6111 /**
6112  * _base_allocate_memory_pools - allocate start of day memory pools
6113  * @ioc: per adapter object
6114  *
6115  * Return: 0 success, anything else error.
6116  */
6117 static int
_base_allocate_memory_pools(struct MPT3SAS_ADAPTER * ioc)6118 _base_allocate_memory_pools(struct MPT3SAS_ADAPTER *ioc)
6119 {
6120 	struct mpt3sas_facts *facts;
6121 	u16 max_sge_elements;
6122 	u16 chains_needed_per_io;
6123 	u32 sz, total_sz, reply_post_free_sz, reply_post_free_array_sz;
6124 	u32 retry_sz;
6125 	u32 rdpq_sz = 0, sense_sz = 0;
6126 	u16 max_request_credit, nvme_blocks_needed;
6127 	unsigned short sg_tablesize;
6128 	u16 sge_size;
6129 	int i;
6130 	int ret = 0, rc = 0;
6131 
6132 	dinitprintk(ioc, ioc_info(ioc, "%s\n", __func__));
6133 
6134 
6135 	retry_sz = 0;
6136 	facts = &ioc->facts;
6137 
6138 	/* command line tunables for max sgl entries */
6139 	if (max_sgl_entries != -1)
6140 		sg_tablesize = max_sgl_entries;
6141 	else {
6142 		if (ioc->hba_mpi_version_belonged == MPI2_VERSION)
6143 			sg_tablesize = MPT2SAS_SG_DEPTH;
6144 		else
6145 			sg_tablesize = MPT3SAS_SG_DEPTH;
6146 	}
6147 
6148 	/* max sgl entries <= MPT_KDUMP_MIN_PHYS_SEGMENTS in KDUMP mode */
6149 	if (reset_devices)
6150 		sg_tablesize = min_t(unsigned short, sg_tablesize,
6151 		   MPT_KDUMP_MIN_PHYS_SEGMENTS);
6152 
6153 	if (ioc->is_mcpu_endpoint)
6154 		ioc->shost->sg_tablesize = MPT_MIN_PHYS_SEGMENTS;
6155 	else {
6156 		if (sg_tablesize < MPT_MIN_PHYS_SEGMENTS)
6157 			sg_tablesize = MPT_MIN_PHYS_SEGMENTS;
6158 		else if (sg_tablesize > MPT_MAX_PHYS_SEGMENTS) {
6159 			sg_tablesize = min_t(unsigned short, sg_tablesize,
6160 					SG_MAX_SEGMENTS);
6161 			ioc_warn(ioc, "sg_tablesize(%u) is bigger than kernel defined SG_CHUNK_SIZE(%u)\n",
6162 				 sg_tablesize, MPT_MAX_PHYS_SEGMENTS);
6163 		}
6164 		ioc->shost->sg_tablesize = sg_tablesize;
6165 	}
6166 
6167 	ioc->internal_depth = min_t(int, (facts->HighPriorityCredit + (5)),
6168 		(facts->RequestCredit / 4));
6169 	if (ioc->internal_depth < INTERNAL_CMDS_COUNT) {
6170 		if (facts->RequestCredit <= (INTERNAL_CMDS_COUNT +
6171 				INTERNAL_SCSIIO_CMDS_COUNT)) {
6172 			ioc_err(ioc, "IOC doesn't have enough Request Credits, it has just %d number of credits\n",
6173 				facts->RequestCredit);
6174 			return -ENOMEM;
6175 		}
6176 		ioc->internal_depth = 10;
6177 	}
6178 
6179 	ioc->hi_priority_depth = ioc->internal_depth - (5);
6180 	/* command line tunables  for max controller queue depth */
6181 	if (max_queue_depth != -1 && max_queue_depth != 0) {
6182 		max_request_credit = min_t(u16, max_queue_depth +
6183 			ioc->internal_depth, facts->RequestCredit);
6184 		if (max_request_credit > MAX_HBA_QUEUE_DEPTH)
6185 			max_request_credit =  MAX_HBA_QUEUE_DEPTH;
6186 	} else if (reset_devices)
6187 		max_request_credit = min_t(u16, facts->RequestCredit,
6188 		    (MPT3SAS_KDUMP_SCSI_IO_DEPTH + ioc->internal_depth));
6189 	else
6190 		max_request_credit = min_t(u16, facts->RequestCredit,
6191 		    MAX_HBA_QUEUE_DEPTH);
6192 
6193 	/* Firmware maintains additional facts->HighPriorityCredit number of
6194 	 * credits for HiPriprity Request messages, so hba queue depth will be
6195 	 * sum of max_request_credit and high priority queue depth.
6196 	 */
6197 	ioc->hba_queue_depth = max_request_credit + ioc->hi_priority_depth;
6198 
6199 	/* request frame size */
6200 	ioc->request_sz = facts->IOCRequestFrameSize * 4;
6201 
6202 	/* reply frame size */
6203 	ioc->reply_sz = facts->ReplyFrameSize * 4;
6204 
6205 	/* chain segment size */
6206 	if (ioc->hba_mpi_version_belonged != MPI2_VERSION) {
6207 		if (facts->IOCMaxChainSegmentSize)
6208 			ioc->chain_segment_sz =
6209 					facts->IOCMaxChainSegmentSize *
6210 					MAX_CHAIN_ELEMT_SZ;
6211 		else
6212 		/* set to 128 bytes size if IOCMaxChainSegmentSize is zero */
6213 			ioc->chain_segment_sz = DEFAULT_NUM_FWCHAIN_ELEMTS *
6214 						    MAX_CHAIN_ELEMT_SZ;
6215 	} else
6216 		ioc->chain_segment_sz = ioc->request_sz;
6217 
6218 	/* calculate the max scatter element size */
6219 	sge_size = max_t(u16, ioc->sge_size, ioc->sge_size_ieee);
6220 
6221  retry_allocation:
6222 	total_sz = 0;
6223 	/* calculate number of sg elements left over in the 1st frame */
6224 	max_sge_elements = ioc->request_sz - ((sizeof(Mpi2SCSIIORequest_t) -
6225 	    sizeof(Mpi2SGEIOUnion_t)) + sge_size);
6226 	ioc->max_sges_in_main_message = max_sge_elements/sge_size;
6227 
6228 	/* now do the same for a chain buffer */
6229 	max_sge_elements = ioc->chain_segment_sz - sge_size;
6230 	ioc->max_sges_in_chain_message = max_sge_elements/sge_size;
6231 
6232 	/*
6233 	 *  MPT3SAS_SG_DEPTH = CONFIG_FUSION_MAX_SGE
6234 	 */
6235 	chains_needed_per_io = ((ioc->shost->sg_tablesize -
6236 	   ioc->max_sges_in_main_message)/ioc->max_sges_in_chain_message)
6237 	    + 1;
6238 	if (chains_needed_per_io > facts->MaxChainDepth) {
6239 		chains_needed_per_io = facts->MaxChainDepth;
6240 		ioc->shost->sg_tablesize = min_t(u16,
6241 		ioc->max_sges_in_main_message + (ioc->max_sges_in_chain_message
6242 		* chains_needed_per_io), ioc->shost->sg_tablesize);
6243 	}
6244 	ioc->chains_needed_per_io = chains_needed_per_io;
6245 
6246 	/* reply free queue sizing - taking into account for 64 FW events */
6247 	ioc->reply_free_queue_depth = ioc->hba_queue_depth + 64;
6248 
6249 	/* mCPU manage single counters for simplicity */
6250 	if (ioc->is_mcpu_endpoint)
6251 		ioc->reply_post_queue_depth = ioc->reply_free_queue_depth;
6252 	else {
6253 		/* calculate reply descriptor post queue depth */
6254 		ioc->reply_post_queue_depth = ioc->hba_queue_depth +
6255 			ioc->reply_free_queue_depth +  1;
6256 		/* align the reply post queue on the next 16 count boundary */
6257 		if (ioc->reply_post_queue_depth % 16)
6258 			ioc->reply_post_queue_depth += 16 -
6259 				(ioc->reply_post_queue_depth % 16);
6260 	}
6261 
6262 	if (ioc->reply_post_queue_depth >
6263 	    facts->MaxReplyDescriptorPostQueueDepth) {
6264 		ioc->reply_post_queue_depth =
6265 				facts->MaxReplyDescriptorPostQueueDepth -
6266 		    (facts->MaxReplyDescriptorPostQueueDepth % 16);
6267 		ioc->hba_queue_depth =
6268 				((ioc->reply_post_queue_depth - 64) / 2) - 1;
6269 		ioc->reply_free_queue_depth = ioc->hba_queue_depth + 64;
6270 	}
6271 
6272 	ioc_info(ioc,
6273 	    "scatter gather: sge_in_main_msg(%d), sge_per_chain(%d), "
6274 	    "sge_per_io(%d), chains_per_io(%d)\n",
6275 	    ioc->max_sges_in_main_message,
6276 	    ioc->max_sges_in_chain_message,
6277 	    ioc->shost->sg_tablesize,
6278 	    ioc->chains_needed_per_io);
6279 
6280 	/* reply post queue, 16 byte align */
6281 	reply_post_free_sz = ioc->reply_post_queue_depth *
6282 	    sizeof(Mpi2DefaultReplyDescriptor_t);
6283 	rdpq_sz = reply_post_free_sz * RDPQ_MAX_INDEX_IN_ONE_CHUNK;
6284 	if ((_base_is_controller_msix_enabled(ioc) && !ioc->rdpq_array_enable)
6285 	    || (ioc->reply_queue_count < RDPQ_MAX_INDEX_IN_ONE_CHUNK))
6286 		rdpq_sz = reply_post_free_sz * ioc->reply_queue_count;
6287 	ret = base_alloc_rdpq_dma_pool(ioc, rdpq_sz);
6288 	if (ret == -EAGAIN) {
6289 		/*
6290 		 * Free allocated bad RDPQ memory pools.
6291 		 * Change dma coherent mask to 32 bit and reallocate RDPQ
6292 		 */
6293 		_base_release_memory_pools(ioc);
6294 		ioc->use_32bit_dma = true;
6295 		if (_base_config_dma_addressing(ioc, ioc->pdev) != 0) {
6296 			ioc_err(ioc,
6297 			    "32 DMA mask failed %s\n", pci_name(ioc->pdev));
6298 			return -ENODEV;
6299 		}
6300 		if (base_alloc_rdpq_dma_pool(ioc, rdpq_sz))
6301 			return -ENOMEM;
6302 	} else if (ret == -ENOMEM)
6303 		return -ENOMEM;
6304 	total_sz = rdpq_sz * (!ioc->rdpq_array_enable ? 1 :
6305 	    DIV_ROUND_UP(ioc->reply_queue_count, RDPQ_MAX_INDEX_IN_ONE_CHUNK));
6306 	ioc->scsiio_depth = ioc->hba_queue_depth -
6307 	    ioc->hi_priority_depth - ioc->internal_depth;
6308 
6309 	/* set the scsi host can_queue depth
6310 	 * with some internal commands that could be outstanding
6311 	 */
6312 	ioc->shost->can_queue = ioc->scsiio_depth - INTERNAL_SCSIIO_CMDS_COUNT;
6313 	dinitprintk(ioc,
6314 		    ioc_info(ioc, "scsi host: can_queue depth (%d)\n",
6315 			     ioc->shost->can_queue));
6316 
6317 	/* contiguous pool for request and chains, 16 byte align, one extra "
6318 	 * "frame for smid=0
6319 	 */
6320 	ioc->chain_depth = ioc->chains_needed_per_io * ioc->scsiio_depth;
6321 	sz = ((ioc->scsiio_depth + 1) * ioc->request_sz);
6322 
6323 	/* hi-priority queue */
6324 	sz += (ioc->hi_priority_depth * ioc->request_sz);
6325 
6326 	/* internal queue */
6327 	sz += (ioc->internal_depth * ioc->request_sz);
6328 
6329 	ioc->request_dma_sz = sz;
6330 	ioc->request = dma_alloc_coherent(&ioc->pdev->dev, sz,
6331 			&ioc->request_dma, GFP_KERNEL);
6332 	if (!ioc->request) {
6333 		ioc_err(ioc, "request pool: dma_alloc_coherent failed: hba_depth(%d), chains_per_io(%d), frame_sz(%d), total(%d kB)\n",
6334 			ioc->hba_queue_depth, ioc->chains_needed_per_io,
6335 			ioc->request_sz, sz / 1024);
6336 		if (ioc->scsiio_depth < MPT3SAS_SAS_QUEUE_DEPTH)
6337 			goto out;
6338 		retry_sz = 64;
6339 		ioc->hba_queue_depth -= retry_sz;
6340 		_base_release_memory_pools(ioc);
6341 		goto retry_allocation;
6342 	}
6343 
6344 	if (retry_sz)
6345 		ioc_err(ioc, "request pool: dma_alloc_coherent succeed: hba_depth(%d), chains_per_io(%d), frame_sz(%d), total(%d kb)\n",
6346 			ioc->hba_queue_depth, ioc->chains_needed_per_io,
6347 			ioc->request_sz, sz / 1024);
6348 
6349 	/* hi-priority queue */
6350 	ioc->hi_priority = ioc->request + ((ioc->scsiio_depth + 1) *
6351 	    ioc->request_sz);
6352 	ioc->hi_priority_dma = ioc->request_dma + ((ioc->scsiio_depth + 1) *
6353 	    ioc->request_sz);
6354 
6355 	/* internal queue */
6356 	ioc->internal = ioc->hi_priority + (ioc->hi_priority_depth *
6357 	    ioc->request_sz);
6358 	ioc->internal_dma = ioc->hi_priority_dma + (ioc->hi_priority_depth *
6359 	    ioc->request_sz);
6360 
6361 	ioc_info(ioc,
6362 	    "request pool(0x%p) - dma(0x%llx): "
6363 	    "depth(%d), frame_size(%d), pool_size(%d kB)\n",
6364 	    ioc->request, (unsigned long long) ioc->request_dma,
6365 	    ioc->hba_queue_depth, ioc->request_sz,
6366 	    (ioc->hba_queue_depth * ioc->request_sz) / 1024);
6367 
6368 	total_sz += sz;
6369 
6370 	dinitprintk(ioc,
6371 		    ioc_info(ioc, "scsiio(0x%p): depth(%d)\n",
6372 			     ioc->request, ioc->scsiio_depth));
6373 
6374 	ioc->chain_depth = min_t(u32, ioc->chain_depth, MAX_CHAIN_DEPTH);
6375 	sz = ioc->scsiio_depth * sizeof(struct chain_lookup);
6376 	ioc->chain_lookup = kzalloc(sz, GFP_KERNEL);
6377 	if (!ioc->chain_lookup) {
6378 		ioc_err(ioc, "chain_lookup: __get_free_pages failed\n");
6379 		goto out;
6380 	}
6381 
6382 	sz = ioc->chains_needed_per_io * sizeof(struct chain_tracker);
6383 	for (i = 0; i < ioc->scsiio_depth; i++) {
6384 		ioc->chain_lookup[i].chains_per_smid = kzalloc(sz, GFP_KERNEL);
6385 		if (!ioc->chain_lookup[i].chains_per_smid) {
6386 			ioc_err(ioc, "chain_lookup: kzalloc failed\n");
6387 			goto out;
6388 		}
6389 	}
6390 
6391 	/* initialize hi-priority queue smid's */
6392 	ioc->hpr_lookup = kcalloc(ioc->hi_priority_depth,
6393 	    sizeof(struct request_tracker), GFP_KERNEL);
6394 	if (!ioc->hpr_lookup) {
6395 		ioc_err(ioc, "hpr_lookup: kcalloc failed\n");
6396 		goto out;
6397 	}
6398 	ioc->hi_priority_smid = ioc->scsiio_depth + 1;
6399 	dinitprintk(ioc,
6400 		    ioc_info(ioc, "hi_priority(0x%p): depth(%d), start smid(%d)\n",
6401 			     ioc->hi_priority,
6402 			     ioc->hi_priority_depth, ioc->hi_priority_smid));
6403 
6404 	/* initialize internal queue smid's */
6405 	ioc->internal_lookup = kcalloc(ioc->internal_depth,
6406 	    sizeof(struct request_tracker), GFP_KERNEL);
6407 	if (!ioc->internal_lookup) {
6408 		ioc_err(ioc, "internal_lookup: kcalloc failed\n");
6409 		goto out;
6410 	}
6411 	ioc->internal_smid = ioc->hi_priority_smid + ioc->hi_priority_depth;
6412 	dinitprintk(ioc,
6413 		    ioc_info(ioc, "internal(0x%p): depth(%d), start smid(%d)\n",
6414 			     ioc->internal,
6415 			     ioc->internal_depth, ioc->internal_smid));
6416 
6417 	ioc->io_queue_num = kcalloc(ioc->scsiio_depth,
6418 	    sizeof(u16), GFP_KERNEL);
6419 	if (!ioc->io_queue_num)
6420 		goto out;
6421 	/*
6422 	 * The number of NVMe page sized blocks needed is:
6423 	 *     (((sg_tablesize * 8) - 1) / (page_size - 8)) + 1
6424 	 * ((sg_tablesize * 8) - 1) is the max PRP's minus the first PRP entry
6425 	 * that is placed in the main message frame.  8 is the size of each PRP
6426 	 * entry or PRP list pointer entry.  8 is subtracted from page_size
6427 	 * because of the PRP list pointer entry at the end of a page, so this
6428 	 * is not counted as a PRP entry.  The 1 added page is a round up.
6429 	 *
6430 	 * To avoid allocation failures due to the amount of memory that could
6431 	 * be required for NVMe PRP's, only each set of NVMe blocks will be
6432 	 * contiguous, so a new set is allocated for each possible I/O.
6433 	 */
6434 
6435 	ioc->chains_per_prp_buffer = 0;
6436 	if (ioc->facts.ProtocolFlags & MPI2_IOCFACTS_PROTOCOL_NVME_DEVICES) {
6437 		nvme_blocks_needed =
6438 			(ioc->shost->sg_tablesize * NVME_PRP_SIZE) - 1;
6439 		nvme_blocks_needed /= (ioc->page_size - NVME_PRP_SIZE);
6440 		nvme_blocks_needed++;
6441 
6442 		sz = sizeof(struct pcie_sg_list) * ioc->scsiio_depth;
6443 		ioc->pcie_sg_lookup = kzalloc(sz, GFP_KERNEL);
6444 		if (!ioc->pcie_sg_lookup) {
6445 			ioc_info(ioc, "PCIe SGL lookup: kzalloc failed\n");
6446 			goto out;
6447 		}
6448 		sz = nvme_blocks_needed * ioc->page_size;
6449 		rc = _base_allocate_pcie_sgl_pool(ioc, sz);
6450 		if (rc == -ENOMEM)
6451 			return -ENOMEM;
6452 		else if (rc == -EAGAIN)
6453 			goto try_32bit_dma;
6454 		total_sz += sz * ioc->scsiio_depth;
6455 	}
6456 
6457 	rc = _base_allocate_chain_dma_pool(ioc, ioc->chain_segment_sz);
6458 	if (rc == -ENOMEM)
6459 		return -ENOMEM;
6460 	else if (rc == -EAGAIN)
6461 		goto try_32bit_dma;
6462 	total_sz += ioc->chain_segment_sz * ((ioc->chains_needed_per_io -
6463 		ioc->chains_per_prp_buffer) * ioc->scsiio_depth);
6464 	dinitprintk(ioc,
6465 	    ioc_info(ioc, "chain pool depth(%d), frame_size(%d), pool_size(%d kB)\n",
6466 	    ioc->chain_depth, ioc->chain_segment_sz,
6467 	    (ioc->chain_depth * ioc->chain_segment_sz) / 1024));
6468 	/* sense buffers, 4 byte align */
6469 	sense_sz = ioc->scsiio_depth * SCSI_SENSE_BUFFERSIZE;
6470 	rc = _base_allocate_sense_dma_pool(ioc, sense_sz);
6471 	if (rc  == -ENOMEM)
6472 		return -ENOMEM;
6473 	else if (rc == -EAGAIN)
6474 		goto try_32bit_dma;
6475 	total_sz += sense_sz;
6476 	/* reply pool, 4 byte align */
6477 	sz = ioc->reply_free_queue_depth * ioc->reply_sz;
6478 	rc = _base_allocate_reply_pool(ioc, sz);
6479 	if (rc == -ENOMEM)
6480 		return -ENOMEM;
6481 	else if (rc == -EAGAIN)
6482 		goto try_32bit_dma;
6483 	total_sz += sz;
6484 
6485 	/* reply free queue, 16 byte align */
6486 	sz = ioc->reply_free_queue_depth * 4;
6487 	rc = _base_allocate_reply_free_dma_pool(ioc, sz);
6488 	if (rc  == -ENOMEM)
6489 		return -ENOMEM;
6490 	else if (rc == -EAGAIN)
6491 		goto try_32bit_dma;
6492 	dinitprintk(ioc,
6493 		    ioc_info(ioc, "reply_free_dma (0x%llx)\n",
6494 			     (unsigned long long)ioc->reply_free_dma));
6495 	total_sz += sz;
6496 	if (ioc->rdpq_array_enable) {
6497 		reply_post_free_array_sz = ioc->reply_queue_count *
6498 		    sizeof(Mpi2IOCInitRDPQArrayEntry);
6499 		rc = _base_allocate_reply_post_free_array(ioc,
6500 		    reply_post_free_array_sz);
6501 		if (rc == -ENOMEM)
6502 			return -ENOMEM;
6503 		else if (rc == -EAGAIN)
6504 			goto try_32bit_dma;
6505 	}
6506 	ioc->config_page_sz = 512;
6507 	ioc->config_page = dma_alloc_coherent(&ioc->pdev->dev,
6508 			ioc->config_page_sz, &ioc->config_page_dma, GFP_KERNEL);
6509 	if (!ioc->config_page) {
6510 		ioc_err(ioc, "config page: dma_pool_alloc failed\n");
6511 		goto out;
6512 	}
6513 
6514 	ioc_info(ioc, "config page(0x%p) - dma(0x%llx): size(%d)\n",
6515 	    ioc->config_page, (unsigned long long)ioc->config_page_dma,
6516 	    ioc->config_page_sz);
6517 	total_sz += ioc->config_page_sz;
6518 
6519 	ioc_info(ioc, "Allocated physical memory: size(%d kB)\n",
6520 		 total_sz / 1024);
6521 	ioc_info(ioc, "Current Controller Queue Depth(%d),Max Controller Queue Depth(%d)\n",
6522 		 ioc->shost->can_queue, facts->RequestCredit);
6523 	ioc_info(ioc, "Scatter Gather Elements per IO(%d)\n",
6524 		 ioc->shost->sg_tablesize);
6525 	return 0;
6526 
6527 try_32bit_dma:
6528 	_base_release_memory_pools(ioc);
6529 	if (ioc->use_32bit_dma && (ioc->dma_mask > 32)) {
6530 		/* Change dma coherent mask to 32 bit and reallocate */
6531 		if (_base_config_dma_addressing(ioc, ioc->pdev) != 0) {
6532 			pr_err("Setting 32 bit coherent DMA mask Failed %s\n",
6533 			    pci_name(ioc->pdev));
6534 			return -ENODEV;
6535 		}
6536 	} else if (_base_reduce_hba_queue_depth(ioc) != 0)
6537 		return -ENOMEM;
6538 	goto retry_allocation;
6539 
6540  out:
6541 	return -ENOMEM;
6542 }
6543 
6544 /**
6545  * mpt3sas_base_get_iocstate - Get the current state of a MPT adapter.
6546  * @ioc: Pointer to MPT_ADAPTER structure
6547  * @cooked: Request raw or cooked IOC state
6548  *
6549  * Return: all IOC Doorbell register bits if cooked==0, else just the
6550  * Doorbell bits in MPI_IOC_STATE_MASK.
6551  */
6552 u32
mpt3sas_base_get_iocstate(struct MPT3SAS_ADAPTER * ioc,int cooked)6553 mpt3sas_base_get_iocstate(struct MPT3SAS_ADAPTER *ioc, int cooked)
6554 {
6555 	u32 s, sc;
6556 
6557 	s = ioc->base_readl_ext_retry(&ioc->chip->Doorbell);
6558 	sc = s & MPI2_IOC_STATE_MASK;
6559 	return cooked ? sc : s;
6560 }
6561 
6562 /**
6563  * _base_wait_on_iocstate - waiting on a particular ioc state
6564  * @ioc: ?
6565  * @ioc_state: controller state { READY, OPERATIONAL, or RESET }
6566  * @timeout: timeout in second
6567  *
6568  * Return: 0 for success, non-zero for failure.
6569  */
6570 static int
_base_wait_on_iocstate(struct MPT3SAS_ADAPTER * ioc,u32 ioc_state,int timeout)6571 _base_wait_on_iocstate(struct MPT3SAS_ADAPTER *ioc, u32 ioc_state, int timeout)
6572 {
6573 	u32 count, cntdn;
6574 	u32 current_state;
6575 
6576 	count = 0;
6577 	cntdn = 1000 * timeout;
6578 	do {
6579 		current_state = mpt3sas_base_get_iocstate(ioc, 1);
6580 		if (current_state == ioc_state)
6581 			return 0;
6582 		if (count && current_state == MPI2_IOC_STATE_FAULT)
6583 			break;
6584 		if (count && current_state == MPI2_IOC_STATE_COREDUMP)
6585 			break;
6586 
6587 		usleep_range(1000, 1500);
6588 		count++;
6589 	} while (--cntdn);
6590 
6591 	return current_state;
6592 }
6593 
6594 /**
6595  * _base_dump_reg_set -	This function will print hexdump of register set.
6596  * @ioc: per adapter object
6597  *
6598  * Return: nothing.
6599  */
6600 static inline void
_base_dump_reg_set(struct MPT3SAS_ADAPTER * ioc)6601 _base_dump_reg_set(struct MPT3SAS_ADAPTER *ioc)
6602 {
6603 	unsigned int i, sz = 256;
6604 	u32 __iomem *reg = (u32 __iomem *)ioc->chip;
6605 
6606 	ioc_info(ioc, "System Register set:\n");
6607 	for (i = 0; i < (sz / sizeof(u32)); i++)
6608 		pr_info("%08x: %08x\n", (i * 4), readl(&reg[i]));
6609 }
6610 
6611 /**
6612  * _base_wait_for_doorbell_int - waiting for controller interrupt(generated by
6613  * a write to the doorbell)
6614  * @ioc: per adapter object
6615  * @timeout: timeout in seconds
6616  *
6617  * Return: 0 for success, non-zero for failure.
6618  *
6619  * Notes: MPI2_HIS_IOC2SYS_DB_STATUS - set to one when IOC writes to doorbell.
6620  */
6621 
6622 static int
_base_wait_for_doorbell_int(struct MPT3SAS_ADAPTER * ioc,int timeout)6623 _base_wait_for_doorbell_int(struct MPT3SAS_ADAPTER *ioc, int timeout)
6624 {
6625 	u32 cntdn, count;
6626 	u32 int_status;
6627 
6628 	count = 0;
6629 	cntdn = 1000 * timeout;
6630 	do {
6631 		int_status = ioc->base_readl(&ioc->chip->HostInterruptStatus);
6632 		if (int_status & MPI2_HIS_IOC2SYS_DB_STATUS) {
6633 			dhsprintk(ioc,
6634 				  ioc_info(ioc, "%s: successful count(%d), timeout(%d)\n",
6635 					   __func__, count, timeout));
6636 			return 0;
6637 		}
6638 
6639 		usleep_range(1000, 1500);
6640 		count++;
6641 	} while (--cntdn);
6642 
6643 	ioc_err(ioc, "%s: failed due to timeout count(%d), int_status(%x)!\n",
6644 		__func__, count, int_status);
6645 	return -EFAULT;
6646 }
6647 
6648 static int
_base_spin_on_doorbell_int(struct MPT3SAS_ADAPTER * ioc,int timeout)6649 _base_spin_on_doorbell_int(struct MPT3SAS_ADAPTER *ioc, int timeout)
6650 {
6651 	u32 cntdn, count;
6652 	u32 int_status;
6653 
6654 	count = 0;
6655 	cntdn = 2000 * timeout;
6656 	do {
6657 		int_status = ioc->base_readl(&ioc->chip->HostInterruptStatus);
6658 		if (int_status & MPI2_HIS_IOC2SYS_DB_STATUS) {
6659 			dhsprintk(ioc,
6660 				  ioc_info(ioc, "%s: successful count(%d), timeout(%d)\n",
6661 					   __func__, count, timeout));
6662 			return 0;
6663 		}
6664 
6665 		udelay(500);
6666 		count++;
6667 	} while (--cntdn);
6668 
6669 	ioc_err(ioc, "%s: failed due to timeout count(%d), int_status(%x)!\n",
6670 		__func__, count, int_status);
6671 	return -EFAULT;
6672 
6673 }
6674 
6675 /**
6676  * _base_wait_for_doorbell_ack - waiting for controller to read the doorbell.
6677  * @ioc: per adapter object
6678  * @timeout: timeout in second
6679  *
6680  * Return: 0 for success, non-zero for failure.
6681  *
6682  * Notes: MPI2_HIS_SYS2IOC_DB_STATUS - set to one when host writes to
6683  * doorbell.
6684  */
6685 static int
_base_wait_for_doorbell_ack(struct MPT3SAS_ADAPTER * ioc,int timeout)6686 _base_wait_for_doorbell_ack(struct MPT3SAS_ADAPTER *ioc, int timeout)
6687 {
6688 	u32 cntdn, count;
6689 	u32 int_status;
6690 	u32 doorbell;
6691 
6692 	count = 0;
6693 	cntdn = 1000 * timeout;
6694 	do {
6695 		int_status = ioc->base_readl(&ioc->chip->HostInterruptStatus);
6696 		if (!(int_status & MPI2_HIS_SYS2IOC_DB_STATUS)) {
6697 			dhsprintk(ioc,
6698 				  ioc_info(ioc, "%s: successful count(%d), timeout(%d)\n",
6699 					   __func__, count, timeout));
6700 			return 0;
6701 		} else if (int_status & MPI2_HIS_IOC2SYS_DB_STATUS) {
6702 			doorbell = ioc->base_readl_ext_retry(&ioc->chip->Doorbell);
6703 			if ((doorbell & MPI2_IOC_STATE_MASK) ==
6704 			    MPI2_IOC_STATE_FAULT) {
6705 				mpt3sas_print_fault_code(ioc, doorbell);
6706 				return -EFAULT;
6707 			}
6708 			if ((doorbell & MPI2_IOC_STATE_MASK) ==
6709 			    MPI2_IOC_STATE_COREDUMP) {
6710 				mpt3sas_print_coredump_info(ioc, doorbell);
6711 				return -EFAULT;
6712 			}
6713 		} else if (int_status == 0xFFFFFFFF)
6714 			goto out;
6715 
6716 		usleep_range(1000, 1500);
6717 		count++;
6718 	} while (--cntdn);
6719 
6720  out:
6721 	ioc_err(ioc, "%s: failed due to timeout count(%d), int_status(%x)!\n",
6722 		__func__, count, int_status);
6723 	return -EFAULT;
6724 }
6725 
6726 /**
6727  * _base_wait_for_doorbell_not_used - waiting for doorbell to not be in use
6728  * @ioc: per adapter object
6729  * @timeout: timeout in second
6730  *
6731  * Return: 0 for success, non-zero for failure.
6732  */
6733 static int
_base_wait_for_doorbell_not_used(struct MPT3SAS_ADAPTER * ioc,int timeout)6734 _base_wait_for_doorbell_not_used(struct MPT3SAS_ADAPTER *ioc, int timeout)
6735 {
6736 	u32 cntdn, count;
6737 	u32 doorbell_reg;
6738 
6739 	count = 0;
6740 	cntdn = 1000 * timeout;
6741 	do {
6742 		doorbell_reg = ioc->base_readl_ext_retry(&ioc->chip->Doorbell);
6743 		if (!(doorbell_reg & MPI2_DOORBELL_USED)) {
6744 			dhsprintk(ioc,
6745 				  ioc_info(ioc, "%s: successful count(%d), timeout(%d)\n",
6746 					   __func__, count, timeout));
6747 			return 0;
6748 		}
6749 
6750 		usleep_range(1000, 1500);
6751 		count++;
6752 	} while (--cntdn);
6753 
6754 	ioc_err(ioc, "%s: failed due to timeout count(%d), doorbell_reg(%x)!\n",
6755 		__func__, count, doorbell_reg);
6756 	return -EFAULT;
6757 }
6758 
6759 /**
6760  * _base_send_ioc_reset - send doorbell reset
6761  * @ioc: per adapter object
6762  * @reset_type: currently only supports: MPI2_FUNCTION_IOC_MESSAGE_UNIT_RESET
6763  * @timeout: timeout in second
6764  *
6765  * Return: 0 for success, non-zero for failure.
6766  */
6767 static int
_base_send_ioc_reset(struct MPT3SAS_ADAPTER * ioc,u8 reset_type,int timeout)6768 _base_send_ioc_reset(struct MPT3SAS_ADAPTER *ioc, u8 reset_type, int timeout)
6769 {
6770 	u32 ioc_state;
6771 	int r = 0;
6772 	unsigned long flags;
6773 
6774 	if (reset_type != MPI2_FUNCTION_IOC_MESSAGE_UNIT_RESET) {
6775 		ioc_err(ioc, "%s: unknown reset_type\n", __func__);
6776 		return -EFAULT;
6777 	}
6778 
6779 	if (!(ioc->facts.IOCCapabilities &
6780 	   MPI2_IOCFACTS_CAPABILITY_EVENT_REPLAY))
6781 		return -EFAULT;
6782 
6783 	ioc_info(ioc, "sending message unit reset !!\n");
6784 
6785 	writel(reset_type << MPI2_DOORBELL_FUNCTION_SHIFT,
6786 	    &ioc->chip->Doorbell);
6787 	if ((_base_wait_for_doorbell_ack(ioc, 15))) {
6788 		r = -EFAULT;
6789 		goto out;
6790 	}
6791 
6792 	ioc_state = _base_wait_on_iocstate(ioc, MPI2_IOC_STATE_READY, timeout);
6793 	if (ioc_state) {
6794 		ioc_err(ioc, "%s: failed going to ready state (ioc_state=0x%x)\n",
6795 			__func__, ioc_state);
6796 		r = -EFAULT;
6797 		goto out;
6798 	}
6799  out:
6800 	if (r != 0) {
6801 		ioc_state = mpt3sas_base_get_iocstate(ioc, 0);
6802 		spin_lock_irqsave(&ioc->ioc_reset_in_progress_lock, flags);
6803 		/*
6804 		 * Wait for IOC state CoreDump to clear only during
6805 		 * HBA initialization & release time.
6806 		 */
6807 		if ((ioc_state & MPI2_IOC_STATE_MASK) ==
6808 		    MPI2_IOC_STATE_COREDUMP && (ioc->is_driver_loading == 1 ||
6809 		    ioc->fault_reset_work_q == NULL)) {
6810 			spin_unlock_irqrestore(
6811 			    &ioc->ioc_reset_in_progress_lock, flags);
6812 			mpt3sas_print_coredump_info(ioc, ioc_state);
6813 			mpt3sas_base_wait_for_coredump_completion(ioc,
6814 			    __func__);
6815 			spin_lock_irqsave(
6816 			    &ioc->ioc_reset_in_progress_lock, flags);
6817 		}
6818 		spin_unlock_irqrestore(&ioc->ioc_reset_in_progress_lock, flags);
6819 	}
6820 	ioc_info(ioc, "message unit reset: %s\n",
6821 		 r == 0 ? "SUCCESS" : "FAILED");
6822 	return r;
6823 }
6824 
6825 /**
6826  * mpt3sas_wait_for_ioc - IOC's operational state is checked here.
6827  * @ioc: per adapter object
6828  * @timeout: timeout in seconds
6829  *
6830  * Return: Waits up to timeout seconds for the IOC to
6831  * become operational. Returns 0 if IOC is present
6832  * and operational; otherwise returns %-EFAULT.
6833  */
6834 
6835 int
mpt3sas_wait_for_ioc(struct MPT3SAS_ADAPTER * ioc,int timeout)6836 mpt3sas_wait_for_ioc(struct MPT3SAS_ADAPTER *ioc, int timeout)
6837 {
6838 	int wait_state_count = 0;
6839 	u32 ioc_state;
6840 
6841 	do {
6842 		ioc_state = mpt3sas_base_get_iocstate(ioc, 1);
6843 		if (ioc_state == MPI2_IOC_STATE_OPERATIONAL)
6844 			break;
6845 
6846 		/*
6847 		 * Watchdog thread will be started after IOC Initialization, so
6848 		 * no need to wait here for IOC state to become operational
6849 		 * when IOC Initialization is on. Instead the driver will
6850 		 * return ETIME status, so that calling function can issue
6851 		 * diag reset operation and retry the command.
6852 		 */
6853 		if (ioc->is_driver_loading)
6854 			return -ETIME;
6855 
6856 		ssleep(1);
6857 		ioc_info(ioc, "%s: waiting for operational state(count=%d)\n",
6858 				__func__, ++wait_state_count);
6859 	} while (--timeout);
6860 	if (!timeout) {
6861 		ioc_err(ioc, "%s: failed due to ioc not operational\n", __func__);
6862 		return -EFAULT;
6863 	}
6864 	if (wait_state_count)
6865 		ioc_info(ioc, "ioc is operational\n");
6866 	return 0;
6867 }
6868 
6869 /**
6870  * _base_handshake_req_reply_wait - send request thru doorbell interface
6871  * @ioc: per adapter object
6872  * @request_bytes: request length
6873  * @request: pointer having request payload
6874  * @reply_bytes: reply length
6875  * @reply: pointer to reply payload
6876  * @timeout: timeout in second
6877  *
6878  * Return: 0 for success, non-zero for failure.
6879  */
6880 static int
_base_handshake_req_reply_wait(struct MPT3SAS_ADAPTER * ioc,int request_bytes,u32 * request,int reply_bytes,u16 * reply,int timeout)6881 _base_handshake_req_reply_wait(struct MPT3SAS_ADAPTER *ioc, int request_bytes,
6882 	u32 *request, int reply_bytes, u16 *reply, int timeout)
6883 {
6884 	MPI2DefaultReply_t *default_reply = (MPI2DefaultReply_t *)reply;
6885 	int i;
6886 	u8 failed;
6887 	__le32 *mfp;
6888 
6889 	/* make sure doorbell is not in use */
6890 	if ((ioc->base_readl_ext_retry(&ioc->chip->Doorbell) & MPI2_DOORBELL_USED)) {
6891 		ioc_err(ioc, "doorbell is in use (line=%d)\n", __LINE__);
6892 		return -EFAULT;
6893 	}
6894 
6895 	/* clear pending doorbell interrupts from previous state changes */
6896 	if (ioc->base_readl(&ioc->chip->HostInterruptStatus) &
6897 	    MPI2_HIS_IOC2SYS_DB_STATUS)
6898 		writel(0, &ioc->chip->HostInterruptStatus);
6899 
6900 	/* send message to ioc */
6901 	writel(((MPI2_FUNCTION_HANDSHAKE<<MPI2_DOORBELL_FUNCTION_SHIFT) |
6902 	    ((request_bytes/4)<<MPI2_DOORBELL_ADD_DWORDS_SHIFT)),
6903 	    &ioc->chip->Doorbell);
6904 
6905 	if ((_base_spin_on_doorbell_int(ioc, 5))) {
6906 		ioc_err(ioc, "doorbell handshake int failed (line=%d)\n",
6907 			__LINE__);
6908 		return -EFAULT;
6909 	}
6910 	writel(0, &ioc->chip->HostInterruptStatus);
6911 
6912 	if ((_base_wait_for_doorbell_ack(ioc, 5))) {
6913 		ioc_err(ioc, "doorbell handshake ack failed (line=%d)\n",
6914 			__LINE__);
6915 		return -EFAULT;
6916 	}
6917 
6918 	/* send message 32-bits at a time */
6919 	for (i = 0, failed = 0; i < request_bytes/4 && !failed; i++) {
6920 		writel(cpu_to_le32(request[i]), &ioc->chip->Doorbell);
6921 		if ((_base_wait_for_doorbell_ack(ioc, 5)))
6922 			failed = 1;
6923 	}
6924 
6925 	if (failed) {
6926 		ioc_err(ioc, "doorbell handshake sending request failed (line=%d)\n",
6927 			__LINE__);
6928 		return -EFAULT;
6929 	}
6930 
6931 	/* now wait for the reply */
6932 	if ((_base_wait_for_doorbell_int(ioc, timeout))) {
6933 		ioc_err(ioc, "doorbell handshake int failed (line=%d)\n",
6934 			__LINE__);
6935 		return -EFAULT;
6936 	}
6937 
6938 	/* read the first two 16-bits, it gives the total length of the reply */
6939 	reply[0] = le16_to_cpu(ioc->base_readl_ext_retry(&ioc->chip->Doorbell)
6940 	    & MPI2_DOORBELL_DATA_MASK);
6941 	writel(0, &ioc->chip->HostInterruptStatus);
6942 	if ((_base_wait_for_doorbell_int(ioc, 5))) {
6943 		ioc_err(ioc, "doorbell handshake int failed (line=%d)\n",
6944 			__LINE__);
6945 		return -EFAULT;
6946 	}
6947 	reply[1] = le16_to_cpu(ioc->base_readl_ext_retry(&ioc->chip->Doorbell)
6948 	    & MPI2_DOORBELL_DATA_MASK);
6949 	writel(0, &ioc->chip->HostInterruptStatus);
6950 
6951 	for (i = 2; i < default_reply->MsgLength * 2; i++)  {
6952 		if ((_base_wait_for_doorbell_int(ioc, 5))) {
6953 			ioc_err(ioc, "doorbell handshake int failed (line=%d)\n",
6954 				__LINE__);
6955 			return -EFAULT;
6956 		}
6957 		if (i >=  reply_bytes/2) /* overflow case */
6958 			ioc->base_readl_ext_retry(&ioc->chip->Doorbell);
6959 		else
6960 			reply[i] = le16_to_cpu(
6961 			    ioc->base_readl_ext_retry(&ioc->chip->Doorbell)
6962 			    & MPI2_DOORBELL_DATA_MASK);
6963 		writel(0, &ioc->chip->HostInterruptStatus);
6964 	}
6965 
6966 	_base_wait_for_doorbell_int(ioc, 5);
6967 	if (_base_wait_for_doorbell_not_used(ioc, 5) != 0) {
6968 		dhsprintk(ioc,
6969 			  ioc_info(ioc, "doorbell is in use (line=%d)\n",
6970 				   __LINE__));
6971 	}
6972 	writel(0, &ioc->chip->HostInterruptStatus);
6973 
6974 	if (ioc->logging_level & MPT_DEBUG_INIT) {
6975 		mfp = (__le32 *)reply;
6976 		pr_info("\toffset:data\n");
6977 		for (i = 0; i < reply_bytes/4; i++)
6978 			ioc_info(ioc, "\t[0x%02x]:%08x\n", i*4,
6979 			    le32_to_cpu(mfp[i]));
6980 	}
6981 	return 0;
6982 }
6983 
6984 /**
6985  * mpt3sas_base_sas_iounit_control - send sas iounit control to FW
6986  * @ioc: per adapter object
6987  * @mpi_reply: the reply payload from FW
6988  * @mpi_request: the request payload sent to FW
6989  *
6990  * The SAS IO Unit Control Request message allows the host to perform low-level
6991  * operations, such as resets on the PHYs of the IO Unit, also allows the host
6992  * to obtain the IOC assigned device handles for a device if it has other
6993  * identifying information about the device, in addition allows the host to
6994  * remove IOC resources associated with the device.
6995  *
6996  * Return: 0 for success, non-zero for failure.
6997  */
6998 int
mpt3sas_base_sas_iounit_control(struct MPT3SAS_ADAPTER * ioc,Mpi2SasIoUnitControlReply_t * mpi_reply,Mpi2SasIoUnitControlRequest_t * mpi_request)6999 mpt3sas_base_sas_iounit_control(struct MPT3SAS_ADAPTER *ioc,
7000 	Mpi2SasIoUnitControlReply_t *mpi_reply,
7001 	Mpi2SasIoUnitControlRequest_t *mpi_request)
7002 {
7003 	u16 smid;
7004 	u8 issue_reset = 0;
7005 	int rc;
7006 	void *request;
7007 
7008 	dinitprintk(ioc, ioc_info(ioc, "%s\n", __func__));
7009 
7010 	mutex_lock(&ioc->base_cmds.mutex);
7011 
7012 	if (ioc->base_cmds.status != MPT3_CMD_NOT_USED) {
7013 		ioc_err(ioc, "%s: base_cmd in use\n", __func__);
7014 		rc = -EAGAIN;
7015 		goto out;
7016 	}
7017 
7018 	rc = mpt3sas_wait_for_ioc(ioc, IOC_OPERATIONAL_WAIT_COUNT);
7019 	if (rc)
7020 		goto out;
7021 
7022 	smid = mpt3sas_base_get_smid(ioc, ioc->base_cb_idx);
7023 	if (!smid) {
7024 		ioc_err(ioc, "%s: failed obtaining a smid\n", __func__);
7025 		rc = -EAGAIN;
7026 		goto out;
7027 	}
7028 
7029 	rc = 0;
7030 	ioc->base_cmds.status = MPT3_CMD_PENDING;
7031 	request = mpt3sas_base_get_msg_frame(ioc, smid);
7032 	ioc->base_cmds.smid = smid;
7033 	memcpy(request, mpi_request, sizeof(Mpi2SasIoUnitControlRequest_t));
7034 	if (mpi_request->Operation == MPI2_SAS_OP_PHY_HARD_RESET ||
7035 	    mpi_request->Operation == MPI2_SAS_OP_PHY_LINK_RESET)
7036 		ioc->ioc_link_reset_in_progress = 1;
7037 	init_completion(&ioc->base_cmds.done);
7038 	ioc->put_smid_default(ioc, smid);
7039 	wait_for_completion_timeout(&ioc->base_cmds.done,
7040 	    msecs_to_jiffies(10000));
7041 	if ((mpi_request->Operation == MPI2_SAS_OP_PHY_HARD_RESET ||
7042 	    mpi_request->Operation == MPI2_SAS_OP_PHY_LINK_RESET) &&
7043 	    ioc->ioc_link_reset_in_progress)
7044 		ioc->ioc_link_reset_in_progress = 0;
7045 	if (!(ioc->base_cmds.status & MPT3_CMD_COMPLETE)) {
7046 		mpt3sas_check_cmd_timeout(ioc, ioc->base_cmds.status,
7047 		    mpi_request, sizeof(Mpi2SasIoUnitControlRequest_t)/4,
7048 		    issue_reset);
7049 		goto issue_host_reset;
7050 	}
7051 	if (ioc->base_cmds.status & MPT3_CMD_REPLY_VALID)
7052 		memcpy(mpi_reply, ioc->base_cmds.reply,
7053 		    sizeof(Mpi2SasIoUnitControlReply_t));
7054 	else
7055 		memset(mpi_reply, 0, sizeof(Mpi2SasIoUnitControlReply_t));
7056 	ioc->base_cmds.status = MPT3_CMD_NOT_USED;
7057 	goto out;
7058 
7059  issue_host_reset:
7060 	if (issue_reset)
7061 		mpt3sas_base_hard_reset_handler(ioc, FORCE_BIG_HAMMER);
7062 	ioc->base_cmds.status = MPT3_CMD_NOT_USED;
7063 	rc = -EFAULT;
7064  out:
7065 	mutex_unlock(&ioc->base_cmds.mutex);
7066 	return rc;
7067 }
7068 
7069 /**
7070  * mpt3sas_base_scsi_enclosure_processor - sending request to sep device
7071  * @ioc: per adapter object
7072  * @mpi_reply: the reply payload from FW
7073  * @mpi_request: the request payload sent to FW
7074  *
7075  * The SCSI Enclosure Processor request message causes the IOC to
7076  * communicate with SES devices to control LED status signals.
7077  *
7078  * Return: 0 for success, non-zero for failure.
7079  */
7080 int
mpt3sas_base_scsi_enclosure_processor(struct MPT3SAS_ADAPTER * ioc,Mpi2SepReply_t * mpi_reply,Mpi2SepRequest_t * mpi_request)7081 mpt3sas_base_scsi_enclosure_processor(struct MPT3SAS_ADAPTER *ioc,
7082 	Mpi2SepReply_t *mpi_reply, Mpi2SepRequest_t *mpi_request)
7083 {
7084 	u16 smid;
7085 	u8 issue_reset = 0;
7086 	int rc;
7087 	void *request;
7088 
7089 	dinitprintk(ioc, ioc_info(ioc, "%s\n", __func__));
7090 
7091 	mutex_lock(&ioc->base_cmds.mutex);
7092 
7093 	if (ioc->base_cmds.status != MPT3_CMD_NOT_USED) {
7094 		ioc_err(ioc, "%s: base_cmd in use\n", __func__);
7095 		rc = -EAGAIN;
7096 		goto out;
7097 	}
7098 
7099 	rc = mpt3sas_wait_for_ioc(ioc, IOC_OPERATIONAL_WAIT_COUNT);
7100 	if (rc)
7101 		goto out;
7102 
7103 	smid = mpt3sas_base_get_smid(ioc, ioc->base_cb_idx);
7104 	if (!smid) {
7105 		ioc_err(ioc, "%s: failed obtaining a smid\n", __func__);
7106 		rc = -EAGAIN;
7107 		goto out;
7108 	}
7109 
7110 	rc = 0;
7111 	ioc->base_cmds.status = MPT3_CMD_PENDING;
7112 	request = mpt3sas_base_get_msg_frame(ioc, smid);
7113 	ioc->base_cmds.smid = smid;
7114 	memset(request, 0, ioc->request_sz);
7115 	memcpy(request, mpi_request, sizeof(Mpi2SepReply_t));
7116 	init_completion(&ioc->base_cmds.done);
7117 	ioc->put_smid_default(ioc, smid);
7118 	wait_for_completion_timeout(&ioc->base_cmds.done,
7119 	    msecs_to_jiffies(10000));
7120 	if (!(ioc->base_cmds.status & MPT3_CMD_COMPLETE)) {
7121 		mpt3sas_check_cmd_timeout(ioc,
7122 		    ioc->base_cmds.status, mpi_request,
7123 		    sizeof(Mpi2SepRequest_t)/4, issue_reset);
7124 		goto issue_host_reset;
7125 	}
7126 	if (ioc->base_cmds.status & MPT3_CMD_REPLY_VALID)
7127 		memcpy(mpi_reply, ioc->base_cmds.reply,
7128 		    sizeof(Mpi2SepReply_t));
7129 	else
7130 		memset(mpi_reply, 0, sizeof(Mpi2SepReply_t));
7131 	ioc->base_cmds.status = MPT3_CMD_NOT_USED;
7132 	goto out;
7133 
7134  issue_host_reset:
7135 	if (issue_reset)
7136 		mpt3sas_base_hard_reset_handler(ioc, FORCE_BIG_HAMMER);
7137 	ioc->base_cmds.status = MPT3_CMD_NOT_USED;
7138 	rc = -EFAULT;
7139  out:
7140 	mutex_unlock(&ioc->base_cmds.mutex);
7141 	return rc;
7142 }
7143 
7144 /**
7145  * _base_get_port_facts - obtain port facts reply and save in ioc
7146  * @ioc: per adapter object
7147  * @port: ?
7148  *
7149  * Return: 0 for success, non-zero for failure.
7150  */
7151 static int
_base_get_port_facts(struct MPT3SAS_ADAPTER * ioc,int port)7152 _base_get_port_facts(struct MPT3SAS_ADAPTER *ioc, int port)
7153 {
7154 	Mpi2PortFactsRequest_t mpi_request;
7155 	Mpi2PortFactsReply_t mpi_reply;
7156 	struct mpt3sas_port_facts *pfacts;
7157 	int mpi_reply_sz, mpi_request_sz, r;
7158 
7159 	dinitprintk(ioc, ioc_info(ioc, "%s\n", __func__));
7160 
7161 	mpi_reply_sz = sizeof(Mpi2PortFactsReply_t);
7162 	mpi_request_sz = sizeof(Mpi2PortFactsRequest_t);
7163 	memset(&mpi_request, 0, mpi_request_sz);
7164 	mpi_request.Function = MPI2_FUNCTION_PORT_FACTS;
7165 	mpi_request.PortNumber = port;
7166 	r = _base_handshake_req_reply_wait(ioc, mpi_request_sz,
7167 	    (u32 *)&mpi_request, mpi_reply_sz, (u16 *)&mpi_reply, 5);
7168 
7169 	if (r != 0) {
7170 		ioc_err(ioc, "%s: handshake failed (r=%d)\n", __func__, r);
7171 		return r;
7172 	}
7173 
7174 	pfacts = &ioc->pfacts[port];
7175 	memset(pfacts, 0, sizeof(struct mpt3sas_port_facts));
7176 	pfacts->PortNumber = mpi_reply.PortNumber;
7177 	pfacts->VP_ID = mpi_reply.VP_ID;
7178 	pfacts->VF_ID = mpi_reply.VF_ID;
7179 	pfacts->MaxPostedCmdBuffers =
7180 	    le16_to_cpu(mpi_reply.MaxPostedCmdBuffers);
7181 
7182 	return 0;
7183 }
7184 
7185 /**
7186  * _base_wait_for_iocstate - Wait until the card is in READY or OPERATIONAL
7187  * @ioc: per adapter object
7188  * @timeout:
7189  *
7190  * Return: 0 for success, non-zero for failure.
7191  */
7192 static int
_base_wait_for_iocstate(struct MPT3SAS_ADAPTER * ioc,int timeout)7193 _base_wait_for_iocstate(struct MPT3SAS_ADAPTER *ioc, int timeout)
7194 {
7195 	u32 ioc_state;
7196 	int rc;
7197 
7198 	dinitprintk(ioc, ioc_info(ioc, "%s\n", __func__));
7199 
7200 	if (ioc->pci_error_recovery) {
7201 		dfailprintk(ioc,
7202 			    ioc_info(ioc, "%s: host in pci error recovery\n",
7203 				     __func__));
7204 		return -EFAULT;
7205 	}
7206 
7207 	ioc_state = mpt3sas_base_get_iocstate(ioc, 0);
7208 	dhsprintk(ioc,
7209 		  ioc_info(ioc, "%s: ioc_state(0x%08x)\n",
7210 			   __func__, ioc_state));
7211 
7212 	if (((ioc_state & MPI2_IOC_STATE_MASK) == MPI2_IOC_STATE_READY) ||
7213 	    (ioc_state & MPI2_IOC_STATE_MASK) == MPI2_IOC_STATE_OPERATIONAL)
7214 		return 0;
7215 
7216 	if (ioc_state & MPI2_DOORBELL_USED) {
7217 		dhsprintk(ioc, ioc_info(ioc, "unexpected doorbell active!\n"));
7218 		goto issue_diag_reset;
7219 	}
7220 
7221 	if ((ioc_state & MPI2_IOC_STATE_MASK) == MPI2_IOC_STATE_FAULT) {
7222 		mpt3sas_print_fault_code(ioc, ioc_state &
7223 		    MPI2_DOORBELL_DATA_MASK);
7224 		goto issue_diag_reset;
7225 	} else if ((ioc_state & MPI2_IOC_STATE_MASK) ==
7226 	    MPI2_IOC_STATE_COREDUMP) {
7227 		ioc_info(ioc,
7228 		    "%s: Skipping the diag reset here. (ioc_state=0x%x)\n",
7229 		    __func__, ioc_state);
7230 		return -EFAULT;
7231 	}
7232 
7233 	ioc_state = _base_wait_on_iocstate(ioc, MPI2_IOC_STATE_READY, timeout);
7234 	if (ioc_state) {
7235 		dfailprintk(ioc,
7236 			    ioc_info(ioc, "%s: failed going to ready state (ioc_state=0x%x)\n",
7237 				     __func__, ioc_state));
7238 		return -EFAULT;
7239 	}
7240 
7241  issue_diag_reset:
7242 	rc = _base_diag_reset(ioc);
7243 	return rc;
7244 }
7245 
7246 /**
7247  * _base_get_ioc_facts - obtain ioc facts reply and save in ioc
7248  * @ioc: per adapter object
7249  *
7250  * Return: 0 for success, non-zero for failure.
7251  */
7252 static int
_base_get_ioc_facts(struct MPT3SAS_ADAPTER * ioc)7253 _base_get_ioc_facts(struct MPT3SAS_ADAPTER *ioc)
7254 {
7255 	Mpi2IOCFactsRequest_t mpi_request;
7256 	Mpi2IOCFactsReply_t mpi_reply;
7257 	struct mpt3sas_facts *facts;
7258 	int mpi_reply_sz, mpi_request_sz, r;
7259 
7260 	dinitprintk(ioc, ioc_info(ioc, "%s\n", __func__));
7261 
7262 	r = _base_wait_for_iocstate(ioc, 10);
7263 	if (r) {
7264 		dfailprintk(ioc,
7265 			    ioc_info(ioc, "%s: failed getting to correct state\n",
7266 				     __func__));
7267 		return r;
7268 	}
7269 	mpi_reply_sz = sizeof(Mpi2IOCFactsReply_t);
7270 	mpi_request_sz = sizeof(Mpi2IOCFactsRequest_t);
7271 	memset(&mpi_request, 0, mpi_request_sz);
7272 	mpi_request.Function = MPI2_FUNCTION_IOC_FACTS;
7273 	r = _base_handshake_req_reply_wait(ioc, mpi_request_sz,
7274 	    (u32 *)&mpi_request, mpi_reply_sz, (u16 *)&mpi_reply, 5);
7275 
7276 	if (r != 0) {
7277 		ioc_err(ioc, "%s: handshake failed (r=%d)\n", __func__, r);
7278 		return r;
7279 	}
7280 
7281 	facts = &ioc->facts;
7282 	memset(facts, 0, sizeof(struct mpt3sas_facts));
7283 	facts->MsgVersion = le16_to_cpu(mpi_reply.MsgVersion);
7284 	facts->HeaderVersion = le16_to_cpu(mpi_reply.HeaderVersion);
7285 	facts->VP_ID = mpi_reply.VP_ID;
7286 	facts->VF_ID = mpi_reply.VF_ID;
7287 	facts->IOCExceptions = le16_to_cpu(mpi_reply.IOCExceptions);
7288 	facts->MaxChainDepth = mpi_reply.MaxChainDepth;
7289 	facts->WhoInit = mpi_reply.WhoInit;
7290 	facts->NumberOfPorts = mpi_reply.NumberOfPorts;
7291 	facts->MaxMSIxVectors = mpi_reply.MaxMSIxVectors;
7292 	if (ioc->msix_enable && (facts->MaxMSIxVectors <=
7293 	    MAX_COMBINED_MSIX_VECTORS(ioc->is_gen35_ioc)))
7294 		ioc->combined_reply_queue = 0;
7295 	facts->RequestCredit = le16_to_cpu(mpi_reply.RequestCredit);
7296 	facts->MaxReplyDescriptorPostQueueDepth =
7297 	    le16_to_cpu(mpi_reply.MaxReplyDescriptorPostQueueDepth);
7298 	facts->ProductID = le16_to_cpu(mpi_reply.ProductID);
7299 	facts->IOCCapabilities = le32_to_cpu(mpi_reply.IOCCapabilities);
7300 	if ((facts->IOCCapabilities & MPI2_IOCFACTS_CAPABILITY_INTEGRATED_RAID))
7301 		ioc->ir_firmware = 1;
7302 	if ((facts->IOCCapabilities &
7303 	      MPI2_IOCFACTS_CAPABILITY_RDPQ_ARRAY_CAPABLE) && (!reset_devices))
7304 		ioc->rdpq_array_capable = 1;
7305 	if ((facts->IOCCapabilities & MPI26_IOCFACTS_CAPABILITY_ATOMIC_REQ)
7306 	    && ioc->is_aero_ioc)
7307 		ioc->atomic_desc_capable = 1;
7308 	facts->FWVersion.Word = le32_to_cpu(mpi_reply.FWVersion.Word);
7309 	facts->IOCRequestFrameSize =
7310 	    le16_to_cpu(mpi_reply.IOCRequestFrameSize);
7311 	if (ioc->hba_mpi_version_belonged != MPI2_VERSION) {
7312 		facts->IOCMaxChainSegmentSize =
7313 			le16_to_cpu(mpi_reply.IOCMaxChainSegmentSize);
7314 	}
7315 	facts->MaxInitiators = le16_to_cpu(mpi_reply.MaxInitiators);
7316 	facts->MaxTargets = le16_to_cpu(mpi_reply.MaxTargets);
7317 	ioc->shost->max_id = -1;
7318 	facts->MaxSasExpanders = le16_to_cpu(mpi_reply.MaxSasExpanders);
7319 	facts->MaxEnclosures = le16_to_cpu(mpi_reply.MaxEnclosures);
7320 	facts->ProtocolFlags = le16_to_cpu(mpi_reply.ProtocolFlags);
7321 	facts->HighPriorityCredit =
7322 	    le16_to_cpu(mpi_reply.HighPriorityCredit);
7323 	facts->ReplyFrameSize = mpi_reply.ReplyFrameSize;
7324 	facts->MaxDevHandle = le16_to_cpu(mpi_reply.MaxDevHandle);
7325 	facts->CurrentHostPageSize = mpi_reply.CurrentHostPageSize;
7326 
7327 	/*
7328 	 * Get the Page Size from IOC Facts. If it's 0, default to 4k.
7329 	 */
7330 	ioc->page_size = 1 << facts->CurrentHostPageSize;
7331 	if (ioc->page_size == 1) {
7332 		ioc_info(ioc, "CurrentHostPageSize is 0: Setting default host page size to 4k\n");
7333 		ioc->page_size = 1 << MPT3SAS_HOST_PAGE_SIZE_4K;
7334 	}
7335 	dinitprintk(ioc,
7336 		    ioc_info(ioc, "CurrentHostPageSize(%d)\n",
7337 			     facts->CurrentHostPageSize));
7338 
7339 	dinitprintk(ioc,
7340 		    ioc_info(ioc, "hba queue depth(%d), max chains per io(%d)\n",
7341 			     facts->RequestCredit, facts->MaxChainDepth));
7342 	dinitprintk(ioc,
7343 		    ioc_info(ioc, "request frame size(%d), reply frame size(%d)\n",
7344 			     facts->IOCRequestFrameSize * 4,
7345 			     facts->ReplyFrameSize * 4));
7346 	return 0;
7347 }
7348 
7349 /**
7350  * _base_send_ioc_init - send ioc_init to firmware
7351  * @ioc: per adapter object
7352  *
7353  * Return: 0 for success, non-zero for failure.
7354  */
7355 static int
_base_send_ioc_init(struct MPT3SAS_ADAPTER * ioc)7356 _base_send_ioc_init(struct MPT3SAS_ADAPTER *ioc)
7357 {
7358 	Mpi2IOCInitRequest_t mpi_request;
7359 	Mpi2IOCInitReply_t mpi_reply;
7360 	int i, r = 0;
7361 	ktime_t current_time;
7362 	u16 ioc_status;
7363 	u32 reply_post_free_array_sz = 0;
7364 
7365 	dinitprintk(ioc, ioc_info(ioc, "%s\n", __func__));
7366 
7367 	memset(&mpi_request, 0, sizeof(Mpi2IOCInitRequest_t));
7368 	mpi_request.Function = MPI2_FUNCTION_IOC_INIT;
7369 	mpi_request.WhoInit = MPI2_WHOINIT_HOST_DRIVER;
7370 	mpi_request.VF_ID = 0; /* TODO */
7371 	mpi_request.VP_ID = 0;
7372 	mpi_request.MsgVersion = cpu_to_le16(ioc->hba_mpi_version_belonged);
7373 	mpi_request.HeaderVersion = cpu_to_le16(MPI2_HEADER_VERSION);
7374 	mpi_request.HostPageSize = MPT3SAS_HOST_PAGE_SIZE_4K;
7375 
7376 	if (_base_is_controller_msix_enabled(ioc))
7377 		mpi_request.HostMSIxVectors = ioc->reply_queue_count;
7378 	mpi_request.SystemRequestFrameSize = cpu_to_le16(ioc->request_sz/4);
7379 	mpi_request.ReplyDescriptorPostQueueDepth =
7380 	    cpu_to_le16(ioc->reply_post_queue_depth);
7381 	mpi_request.ReplyFreeQueueDepth =
7382 	    cpu_to_le16(ioc->reply_free_queue_depth);
7383 
7384 	mpi_request.SenseBufferAddressHigh =
7385 	    cpu_to_le32((u64)ioc->sense_dma >> 32);
7386 	mpi_request.SystemReplyAddressHigh =
7387 	    cpu_to_le32((u64)ioc->reply_dma >> 32);
7388 	mpi_request.SystemRequestFrameBaseAddress =
7389 	    cpu_to_le64((u64)ioc->request_dma);
7390 	mpi_request.ReplyFreeQueueAddress =
7391 	    cpu_to_le64((u64)ioc->reply_free_dma);
7392 
7393 	if (ioc->rdpq_array_enable) {
7394 		reply_post_free_array_sz = ioc->reply_queue_count *
7395 		    sizeof(Mpi2IOCInitRDPQArrayEntry);
7396 		memset(ioc->reply_post_free_array, 0, reply_post_free_array_sz);
7397 		for (i = 0; i < ioc->reply_queue_count; i++)
7398 			ioc->reply_post_free_array[i].RDPQBaseAddress =
7399 			    cpu_to_le64(
7400 				(u64)ioc->reply_post[i].reply_post_free_dma);
7401 		mpi_request.MsgFlags = MPI2_IOCINIT_MSGFLAG_RDPQ_ARRAY_MODE;
7402 		mpi_request.ReplyDescriptorPostQueueAddress =
7403 		    cpu_to_le64((u64)ioc->reply_post_free_array_dma);
7404 	} else {
7405 		mpi_request.ReplyDescriptorPostQueueAddress =
7406 		    cpu_to_le64((u64)ioc->reply_post[0].reply_post_free_dma);
7407 	}
7408 
7409 	/*
7410 	 * Set the flag to enable CoreDump state feature in IOC firmware.
7411 	 */
7412 	mpi_request.ConfigurationFlags |=
7413 	    cpu_to_le16(MPI26_IOCINIT_CFGFLAGS_COREDUMP_ENABLE);
7414 
7415 	/* This time stamp specifies number of milliseconds
7416 	 * since epoch ~ midnight January 1, 1970.
7417 	 */
7418 	current_time = ktime_get_real();
7419 	mpi_request.TimeStamp = cpu_to_le64(ktime_to_ms(current_time));
7420 
7421 	if (ioc->logging_level & MPT_DEBUG_INIT) {
7422 		__le32 *mfp;
7423 		int i;
7424 
7425 		mfp = (__le32 *)&mpi_request;
7426 		ioc_info(ioc, "\toffset:data\n");
7427 		for (i = 0; i < sizeof(Mpi2IOCInitRequest_t)/4; i++)
7428 			ioc_info(ioc, "\t[0x%02x]:%08x\n", i*4,
7429 			    le32_to_cpu(mfp[i]));
7430 	}
7431 
7432 	r = _base_handshake_req_reply_wait(ioc,
7433 	    sizeof(Mpi2IOCInitRequest_t), (u32 *)&mpi_request,
7434 	    sizeof(Mpi2IOCInitReply_t), (u16 *)&mpi_reply, 30);
7435 
7436 	if (r != 0) {
7437 		ioc_err(ioc, "%s: handshake failed (r=%d)\n", __func__, r);
7438 		return r;
7439 	}
7440 
7441 	ioc_status = le16_to_cpu(mpi_reply.IOCStatus) & MPI2_IOCSTATUS_MASK;
7442 	if (ioc_status != MPI2_IOCSTATUS_SUCCESS ||
7443 	    mpi_reply.IOCLogInfo) {
7444 		ioc_err(ioc, "%s: failed\n", __func__);
7445 		r = -EIO;
7446 	}
7447 
7448 	/* Reset TimeSync Counter*/
7449 	ioc->timestamp_update_count = 0;
7450 	return r;
7451 }
7452 
7453 /**
7454  * mpt3sas_port_enable_done - command completion routine for port enable
7455  * @ioc: per adapter object
7456  * @smid: system request message index
7457  * @msix_index: MSIX table index supplied by the OS
7458  * @reply: reply message frame(lower 32bit addr)
7459  *
7460  * Return: 1 meaning mf should be freed from _base_interrupt
7461  *          0 means the mf is freed from this function.
7462  */
7463 u8
mpt3sas_port_enable_done(struct MPT3SAS_ADAPTER * ioc,u16 smid,u8 msix_index,u32 reply)7464 mpt3sas_port_enable_done(struct MPT3SAS_ADAPTER *ioc, u16 smid, u8 msix_index,
7465 	u32 reply)
7466 {
7467 	MPI2DefaultReply_t *mpi_reply;
7468 	u16 ioc_status;
7469 
7470 	if (ioc->port_enable_cmds.status == MPT3_CMD_NOT_USED)
7471 		return 1;
7472 
7473 	mpi_reply = mpt3sas_base_get_reply_virt_addr(ioc, reply);
7474 	if (!mpi_reply)
7475 		return 1;
7476 
7477 	if (mpi_reply->Function != MPI2_FUNCTION_PORT_ENABLE)
7478 		return 1;
7479 
7480 	ioc->port_enable_cmds.status &= ~MPT3_CMD_PENDING;
7481 	ioc->port_enable_cmds.status |= MPT3_CMD_COMPLETE;
7482 	ioc->port_enable_cmds.status |= MPT3_CMD_REPLY_VALID;
7483 	memcpy(ioc->port_enable_cmds.reply, mpi_reply, mpi_reply->MsgLength*4);
7484 	ioc_status = le16_to_cpu(mpi_reply->IOCStatus) & MPI2_IOCSTATUS_MASK;
7485 	if (ioc_status != MPI2_IOCSTATUS_SUCCESS)
7486 		ioc->port_enable_failed = 1;
7487 
7488 	if (ioc->port_enable_cmds.status & MPT3_CMD_COMPLETE_ASYNC) {
7489 		ioc->port_enable_cmds.status &= ~MPT3_CMD_COMPLETE_ASYNC;
7490 		if (ioc_status == MPI2_IOCSTATUS_SUCCESS) {
7491 			mpt3sas_port_enable_complete(ioc);
7492 			return 1;
7493 		} else {
7494 			ioc->start_scan_failed = ioc_status;
7495 			ioc->start_scan = 0;
7496 			return 1;
7497 		}
7498 	}
7499 	complete(&ioc->port_enable_cmds.done);
7500 	return 1;
7501 }
7502 
7503 /**
7504  * _base_send_port_enable - send port_enable(discovery stuff) to firmware
7505  * @ioc: per adapter object
7506  *
7507  * Return: 0 for success, non-zero for failure.
7508  */
7509 static int
_base_send_port_enable(struct MPT3SAS_ADAPTER * ioc)7510 _base_send_port_enable(struct MPT3SAS_ADAPTER *ioc)
7511 {
7512 	Mpi2PortEnableRequest_t *mpi_request;
7513 	Mpi2PortEnableReply_t *mpi_reply;
7514 	int r = 0;
7515 	u16 smid;
7516 	u16 ioc_status;
7517 
7518 	ioc_info(ioc, "sending port enable !!\n");
7519 
7520 	if (ioc->port_enable_cmds.status & MPT3_CMD_PENDING) {
7521 		ioc_err(ioc, "%s: internal command already in use\n", __func__);
7522 		return -EAGAIN;
7523 	}
7524 
7525 	smid = mpt3sas_base_get_smid(ioc, ioc->port_enable_cb_idx);
7526 	if (!smid) {
7527 		ioc_err(ioc, "%s: failed obtaining a smid\n", __func__);
7528 		return -EAGAIN;
7529 	}
7530 
7531 	ioc->port_enable_cmds.status = MPT3_CMD_PENDING;
7532 	mpi_request = mpt3sas_base_get_msg_frame(ioc, smid);
7533 	ioc->port_enable_cmds.smid = smid;
7534 	memset(mpi_request, 0, sizeof(Mpi2PortEnableRequest_t));
7535 	mpi_request->Function = MPI2_FUNCTION_PORT_ENABLE;
7536 
7537 	init_completion(&ioc->port_enable_cmds.done);
7538 	ioc->put_smid_default(ioc, smid);
7539 	wait_for_completion_timeout(&ioc->port_enable_cmds.done, 300*HZ);
7540 	if (!(ioc->port_enable_cmds.status & MPT3_CMD_COMPLETE)) {
7541 		ioc_err(ioc, "%s: timeout\n", __func__);
7542 		_debug_dump_mf(mpi_request,
7543 		    sizeof(Mpi2PortEnableRequest_t)/4);
7544 		if (ioc->port_enable_cmds.status & MPT3_CMD_RESET)
7545 			r = -EFAULT;
7546 		else
7547 			r = -ETIME;
7548 		goto out;
7549 	}
7550 
7551 	mpi_reply = ioc->port_enable_cmds.reply;
7552 	ioc_status = le16_to_cpu(mpi_reply->IOCStatus) & MPI2_IOCSTATUS_MASK;
7553 	if (ioc_status != MPI2_IOCSTATUS_SUCCESS) {
7554 		ioc_err(ioc, "%s: failed with (ioc_status=0x%08x)\n",
7555 			__func__, ioc_status);
7556 		r = -EFAULT;
7557 		goto out;
7558 	}
7559 
7560  out:
7561 	ioc->port_enable_cmds.status = MPT3_CMD_NOT_USED;
7562 	ioc_info(ioc, "port enable: %s\n", r == 0 ? "SUCCESS" : "FAILED");
7563 	return r;
7564 }
7565 
7566 /**
7567  * mpt3sas_port_enable - initiate firmware discovery (don't wait for reply)
7568  * @ioc: per adapter object
7569  *
7570  * Return: 0 for success, non-zero for failure.
7571  */
7572 int
mpt3sas_port_enable(struct MPT3SAS_ADAPTER * ioc)7573 mpt3sas_port_enable(struct MPT3SAS_ADAPTER *ioc)
7574 {
7575 	Mpi2PortEnableRequest_t *mpi_request;
7576 	u16 smid;
7577 
7578 	ioc_info(ioc, "sending port enable !!\n");
7579 
7580 	if (ioc->port_enable_cmds.status & MPT3_CMD_PENDING) {
7581 		ioc_err(ioc, "%s: internal command already in use\n", __func__);
7582 		return -EAGAIN;
7583 	}
7584 
7585 	smid = mpt3sas_base_get_smid(ioc, ioc->port_enable_cb_idx);
7586 	if (!smid) {
7587 		ioc_err(ioc, "%s: failed obtaining a smid\n", __func__);
7588 		return -EAGAIN;
7589 	}
7590 	ioc->drv_internal_flags |= MPT_DRV_INTERNAL_FIRST_PE_ISSUED;
7591 	ioc->port_enable_cmds.status = MPT3_CMD_PENDING;
7592 	ioc->port_enable_cmds.status |= MPT3_CMD_COMPLETE_ASYNC;
7593 	mpi_request = mpt3sas_base_get_msg_frame(ioc, smid);
7594 	ioc->port_enable_cmds.smid = smid;
7595 	memset(mpi_request, 0, sizeof(Mpi2PortEnableRequest_t));
7596 	mpi_request->Function = MPI2_FUNCTION_PORT_ENABLE;
7597 
7598 	ioc->put_smid_default(ioc, smid);
7599 	return 0;
7600 }
7601 
7602 /**
7603  * _base_determine_wait_on_discovery - desposition
7604  * @ioc: per adapter object
7605  *
7606  * Decide whether to wait on discovery to complete. Used to either
7607  * locate boot device, or report volumes ahead of physical devices.
7608  *
7609  * Return: 1 for wait, 0 for don't wait.
7610  */
7611 static int
_base_determine_wait_on_discovery(struct MPT3SAS_ADAPTER * ioc)7612 _base_determine_wait_on_discovery(struct MPT3SAS_ADAPTER *ioc)
7613 {
7614 	/* We wait for discovery to complete if IR firmware is loaded.
7615 	 * The sas topology events arrive before PD events, so we need time to
7616 	 * turn on the bit in ioc->pd_handles to indicate PD
7617 	 * Also, it maybe required to report Volumes ahead of physical
7618 	 * devices when MPI2_IOCPAGE8_IRFLAGS_LOW_VOLUME_MAPPING is set.
7619 	 */
7620 	if (ioc->ir_firmware)
7621 		return 1;
7622 
7623 	/* if no Bios, then we don't need to wait */
7624 	if (!ioc->bios_pg3.BiosVersion)
7625 		return 0;
7626 
7627 	/* Bios is present, then we drop down here.
7628 	 *
7629 	 * If there any entries in the Bios Page 2, then we wait
7630 	 * for discovery to complete.
7631 	 */
7632 
7633 	/* Current Boot Device */
7634 	if ((ioc->bios_pg2.CurrentBootDeviceForm &
7635 	    MPI2_BIOSPAGE2_FORM_MASK) ==
7636 	    MPI2_BIOSPAGE2_FORM_NO_DEVICE_SPECIFIED &&
7637 	/* Request Boot Device */
7638 	   (ioc->bios_pg2.ReqBootDeviceForm &
7639 	    MPI2_BIOSPAGE2_FORM_MASK) ==
7640 	    MPI2_BIOSPAGE2_FORM_NO_DEVICE_SPECIFIED &&
7641 	/* Alternate Request Boot Device */
7642 	   (ioc->bios_pg2.ReqAltBootDeviceForm &
7643 	    MPI2_BIOSPAGE2_FORM_MASK) ==
7644 	    MPI2_BIOSPAGE2_FORM_NO_DEVICE_SPECIFIED)
7645 		return 0;
7646 
7647 	return 1;
7648 }
7649 
7650 /**
7651  * _base_unmask_events - turn on notification for this event
7652  * @ioc: per adapter object
7653  * @event: firmware event
7654  *
7655  * The mask is stored in ioc->event_masks.
7656  */
7657 static void
_base_unmask_events(struct MPT3SAS_ADAPTER * ioc,u16 event)7658 _base_unmask_events(struct MPT3SAS_ADAPTER *ioc, u16 event)
7659 {
7660 	u32 desired_event;
7661 
7662 	if (event >= 128)
7663 		return;
7664 
7665 	desired_event = (1 << (event % 32));
7666 
7667 	if (event < 32)
7668 		ioc->event_masks[0] &= ~desired_event;
7669 	else if (event < 64)
7670 		ioc->event_masks[1] &= ~desired_event;
7671 	else if (event < 96)
7672 		ioc->event_masks[2] &= ~desired_event;
7673 	else if (event < 128)
7674 		ioc->event_masks[3] &= ~desired_event;
7675 }
7676 
7677 /**
7678  * _base_event_notification - send event notification
7679  * @ioc: per adapter object
7680  *
7681  * Return: 0 for success, non-zero for failure.
7682  */
7683 static int
_base_event_notification(struct MPT3SAS_ADAPTER * ioc)7684 _base_event_notification(struct MPT3SAS_ADAPTER *ioc)
7685 {
7686 	Mpi2EventNotificationRequest_t *mpi_request;
7687 	u16 smid;
7688 	int r = 0;
7689 	int i, issue_diag_reset = 0;
7690 
7691 	dinitprintk(ioc, ioc_info(ioc, "%s\n", __func__));
7692 
7693 	if (ioc->base_cmds.status & MPT3_CMD_PENDING) {
7694 		ioc_err(ioc, "%s: internal command already in use\n", __func__);
7695 		return -EAGAIN;
7696 	}
7697 
7698 	smid = mpt3sas_base_get_smid(ioc, ioc->base_cb_idx);
7699 	if (!smid) {
7700 		ioc_err(ioc, "%s: failed obtaining a smid\n", __func__);
7701 		return -EAGAIN;
7702 	}
7703 	ioc->base_cmds.status = MPT3_CMD_PENDING;
7704 	mpi_request = mpt3sas_base_get_msg_frame(ioc, smid);
7705 	ioc->base_cmds.smid = smid;
7706 	memset(mpi_request, 0, sizeof(Mpi2EventNotificationRequest_t));
7707 	mpi_request->Function = MPI2_FUNCTION_EVENT_NOTIFICATION;
7708 	mpi_request->VF_ID = 0; /* TODO */
7709 	mpi_request->VP_ID = 0;
7710 	for (i = 0; i < MPI2_EVENT_NOTIFY_EVENTMASK_WORDS; i++)
7711 		mpi_request->EventMasks[i] =
7712 		    cpu_to_le32(ioc->event_masks[i]);
7713 	init_completion(&ioc->base_cmds.done);
7714 	ioc->put_smid_default(ioc, smid);
7715 	wait_for_completion_timeout(&ioc->base_cmds.done, 30*HZ);
7716 	if (!(ioc->base_cmds.status & MPT3_CMD_COMPLETE)) {
7717 		ioc_err(ioc, "%s: timeout\n", __func__);
7718 		_debug_dump_mf(mpi_request,
7719 		    sizeof(Mpi2EventNotificationRequest_t)/4);
7720 		if (ioc->base_cmds.status & MPT3_CMD_RESET)
7721 			r = -EFAULT;
7722 		else
7723 			issue_diag_reset = 1;
7724 
7725 	} else
7726 		dinitprintk(ioc, ioc_info(ioc, "%s: complete\n", __func__));
7727 	ioc->base_cmds.status = MPT3_CMD_NOT_USED;
7728 
7729 	if (issue_diag_reset) {
7730 		if (ioc->drv_internal_flags & MPT_DRV_INTERNAL_FIRST_PE_ISSUED)
7731 			return -EFAULT;
7732 		if (mpt3sas_base_check_for_fault_and_issue_reset(ioc))
7733 			return -EFAULT;
7734 		r = -EAGAIN;
7735 	}
7736 	return r;
7737 }
7738 
7739 /**
7740  * mpt3sas_base_validate_event_type - validating event types
7741  * @ioc: per adapter object
7742  * @event_type: firmware event
7743  *
7744  * This will turn on firmware event notification when application
7745  * ask for that event. We don't mask events that are already enabled.
7746  */
7747 void
mpt3sas_base_validate_event_type(struct MPT3SAS_ADAPTER * ioc,u32 * event_type)7748 mpt3sas_base_validate_event_type(struct MPT3SAS_ADAPTER *ioc, u32 *event_type)
7749 {
7750 	int i, j;
7751 	u32 event_mask, desired_event;
7752 	u8 send_update_to_fw;
7753 
7754 	for (i = 0, send_update_to_fw = 0; i <
7755 	    MPI2_EVENT_NOTIFY_EVENTMASK_WORDS; i++) {
7756 		event_mask = ~event_type[i];
7757 		desired_event = 1;
7758 		for (j = 0; j < 32; j++) {
7759 			if (!(event_mask & desired_event) &&
7760 			    (ioc->event_masks[i] & desired_event)) {
7761 				ioc->event_masks[i] &= ~desired_event;
7762 				send_update_to_fw = 1;
7763 			}
7764 			desired_event = (desired_event << 1);
7765 		}
7766 	}
7767 
7768 	if (!send_update_to_fw)
7769 		return;
7770 
7771 	mutex_lock(&ioc->base_cmds.mutex);
7772 	_base_event_notification(ioc);
7773 	mutex_unlock(&ioc->base_cmds.mutex);
7774 }
7775 
7776 /**
7777  * _base_diag_reset - the "big hammer" start of day reset
7778  * @ioc: per adapter object
7779  *
7780  * Return: 0 for success, non-zero for failure.
7781  */
7782 static int
_base_diag_reset(struct MPT3SAS_ADAPTER * ioc)7783 _base_diag_reset(struct MPT3SAS_ADAPTER *ioc)
7784 {
7785 	u32 host_diagnostic;
7786 	u32 ioc_state;
7787 	u32 count;
7788 	u32 hcb_size;
7789 
7790 	ioc_info(ioc, "sending diag reset !!\n");
7791 
7792 	pci_cfg_access_lock(ioc->pdev);
7793 
7794 	drsprintk(ioc, ioc_info(ioc, "clear interrupts\n"));
7795 
7796 	count = 0;
7797 	do {
7798 		/* Write magic sequence to WriteSequence register
7799 		 * Loop until in diagnostic mode
7800 		 */
7801 		drsprintk(ioc, ioc_info(ioc, "write magic sequence\n"));
7802 		writel(MPI2_WRSEQ_FLUSH_KEY_VALUE, &ioc->chip->WriteSequence);
7803 		writel(MPI2_WRSEQ_1ST_KEY_VALUE, &ioc->chip->WriteSequence);
7804 		writel(MPI2_WRSEQ_2ND_KEY_VALUE, &ioc->chip->WriteSequence);
7805 		writel(MPI2_WRSEQ_3RD_KEY_VALUE, &ioc->chip->WriteSequence);
7806 		writel(MPI2_WRSEQ_4TH_KEY_VALUE, &ioc->chip->WriteSequence);
7807 		writel(MPI2_WRSEQ_5TH_KEY_VALUE, &ioc->chip->WriteSequence);
7808 		writel(MPI2_WRSEQ_6TH_KEY_VALUE, &ioc->chip->WriteSequence);
7809 
7810 		/* wait 100 msec */
7811 		msleep(100);
7812 
7813 		if (count++ > 20) {
7814 			ioc_info(ioc,
7815 			    "Stop writing magic sequence after 20 retries\n");
7816 			_base_dump_reg_set(ioc);
7817 			goto out;
7818 		}
7819 
7820 		host_diagnostic = ioc->base_readl_ext_retry(&ioc->chip->HostDiagnostic);
7821 		drsprintk(ioc,
7822 			  ioc_info(ioc, "wrote magic sequence: count(%d), host_diagnostic(0x%08x)\n",
7823 				   count, host_diagnostic));
7824 
7825 	} while ((host_diagnostic & MPI2_DIAG_DIAG_WRITE_ENABLE) == 0);
7826 
7827 	hcb_size = ioc->base_readl(&ioc->chip->HCBSize);
7828 
7829 	drsprintk(ioc, ioc_info(ioc, "diag reset: issued\n"));
7830 	writel(host_diagnostic | MPI2_DIAG_RESET_ADAPTER,
7831 	     &ioc->chip->HostDiagnostic);
7832 
7833 	/*This delay allows the chip PCIe hardware time to finish reset tasks*/
7834 	msleep(MPI2_HARD_RESET_PCIE_FIRST_READ_DELAY_MICRO_SEC/1000);
7835 
7836 	/* Approximately 300 second max wait */
7837 	for (count = 0; count < (300000000 /
7838 		MPI2_HARD_RESET_PCIE_SECOND_READ_DELAY_MICRO_SEC); count++) {
7839 
7840 		host_diagnostic = ioc->base_readl_ext_retry(&ioc->chip->HostDiagnostic);
7841 
7842 		if (host_diagnostic == 0xFFFFFFFF) {
7843 			ioc_info(ioc,
7844 			    "Invalid host diagnostic register value\n");
7845 			_base_dump_reg_set(ioc);
7846 			goto out;
7847 		}
7848 		if (!(host_diagnostic & MPI2_DIAG_RESET_ADAPTER))
7849 			break;
7850 
7851 		msleep(MPI2_HARD_RESET_PCIE_SECOND_READ_DELAY_MICRO_SEC / 1000);
7852 	}
7853 
7854 	if (host_diagnostic & MPI2_DIAG_HCB_MODE) {
7855 
7856 		drsprintk(ioc,
7857 			  ioc_info(ioc, "restart the adapter assuming the HCB Address points to good F/W\n"));
7858 		host_diagnostic &= ~MPI2_DIAG_BOOT_DEVICE_SELECT_MASK;
7859 		host_diagnostic |= MPI2_DIAG_BOOT_DEVICE_SELECT_HCDW;
7860 		writel(host_diagnostic, &ioc->chip->HostDiagnostic);
7861 
7862 		drsprintk(ioc, ioc_info(ioc, "re-enable the HCDW\n"));
7863 		writel(hcb_size | MPI2_HCB_SIZE_HCB_ENABLE,
7864 		    &ioc->chip->HCBSize);
7865 	}
7866 
7867 	drsprintk(ioc, ioc_info(ioc, "restart the adapter\n"));
7868 	writel(host_diagnostic & ~MPI2_DIAG_HOLD_IOC_RESET,
7869 	    &ioc->chip->HostDiagnostic);
7870 
7871 	drsprintk(ioc,
7872 		  ioc_info(ioc, "disable writes to the diagnostic register\n"));
7873 	writel(MPI2_WRSEQ_FLUSH_KEY_VALUE, &ioc->chip->WriteSequence);
7874 
7875 	drsprintk(ioc, ioc_info(ioc, "Wait for FW to go to the READY state\n"));
7876 	ioc_state = _base_wait_on_iocstate(ioc, MPI2_IOC_STATE_READY, 20);
7877 	if (ioc_state) {
7878 		ioc_err(ioc, "%s: failed going to ready state (ioc_state=0x%x)\n",
7879 			__func__, ioc_state);
7880 		_base_dump_reg_set(ioc);
7881 		goto out;
7882 	}
7883 
7884 	pci_cfg_access_unlock(ioc->pdev);
7885 	ioc_info(ioc, "diag reset: SUCCESS\n");
7886 	return 0;
7887 
7888  out:
7889 	pci_cfg_access_unlock(ioc->pdev);
7890 	ioc_err(ioc, "diag reset: FAILED\n");
7891 	return -EFAULT;
7892 }
7893 
7894 /**
7895  * mpt3sas_base_make_ioc_ready - put controller in READY state
7896  * @ioc: per adapter object
7897  * @type: FORCE_BIG_HAMMER or SOFT_RESET
7898  *
7899  * Return: 0 for success, non-zero for failure.
7900  */
7901 int
mpt3sas_base_make_ioc_ready(struct MPT3SAS_ADAPTER * ioc,enum reset_type type)7902 mpt3sas_base_make_ioc_ready(struct MPT3SAS_ADAPTER *ioc, enum reset_type type)
7903 {
7904 	u32 ioc_state;
7905 	int rc;
7906 	int count;
7907 
7908 	dinitprintk(ioc, ioc_info(ioc, "%s\n", __func__));
7909 
7910 	if (ioc->pci_error_recovery)
7911 		return 0;
7912 
7913 	ioc_state = mpt3sas_base_get_iocstate(ioc, 0);
7914 	dhsprintk(ioc,
7915 		  ioc_info(ioc, "%s: ioc_state(0x%08x)\n",
7916 			   __func__, ioc_state));
7917 
7918 	/* if in RESET state, it should move to READY state shortly */
7919 	count = 0;
7920 	if ((ioc_state & MPI2_IOC_STATE_MASK) == MPI2_IOC_STATE_RESET) {
7921 		while ((ioc_state & MPI2_IOC_STATE_MASK) !=
7922 		    MPI2_IOC_STATE_READY) {
7923 			if (count++ == 10) {
7924 				ioc_err(ioc, "%s: failed going to ready state (ioc_state=0x%x)\n",
7925 					__func__, ioc_state);
7926 				return -EFAULT;
7927 			}
7928 			ssleep(1);
7929 			ioc_state = mpt3sas_base_get_iocstate(ioc, 0);
7930 		}
7931 	}
7932 
7933 	if ((ioc_state & MPI2_IOC_STATE_MASK) == MPI2_IOC_STATE_READY)
7934 		return 0;
7935 
7936 	if (ioc_state & MPI2_DOORBELL_USED) {
7937 		ioc_info(ioc, "unexpected doorbell active!\n");
7938 		goto issue_diag_reset;
7939 	}
7940 
7941 	if ((ioc_state & MPI2_IOC_STATE_MASK) == MPI2_IOC_STATE_FAULT) {
7942 		mpt3sas_print_fault_code(ioc, ioc_state &
7943 		    MPI2_DOORBELL_DATA_MASK);
7944 		goto issue_diag_reset;
7945 	}
7946 
7947 	if ((ioc_state & MPI2_IOC_STATE_MASK) == MPI2_IOC_STATE_COREDUMP) {
7948 		/*
7949 		 * if host reset is invoked while watch dog thread is waiting
7950 		 * for IOC state to be changed to Fault state then driver has
7951 		 * to wait here for CoreDump state to clear otherwise reset
7952 		 * will be issued to the FW and FW move the IOC state to
7953 		 * reset state without copying the FW logs to coredump region.
7954 		 */
7955 		if (ioc->ioc_coredump_loop != MPT3SAS_COREDUMP_LOOP_DONE) {
7956 			mpt3sas_print_coredump_info(ioc, ioc_state &
7957 			    MPI2_DOORBELL_DATA_MASK);
7958 			mpt3sas_base_wait_for_coredump_completion(ioc,
7959 			    __func__);
7960 		}
7961 		goto issue_diag_reset;
7962 	}
7963 
7964 	if (type == FORCE_BIG_HAMMER)
7965 		goto issue_diag_reset;
7966 
7967 	if ((ioc_state & MPI2_IOC_STATE_MASK) == MPI2_IOC_STATE_OPERATIONAL)
7968 		if (!(_base_send_ioc_reset(ioc,
7969 		    MPI2_FUNCTION_IOC_MESSAGE_UNIT_RESET, 15))) {
7970 			return 0;
7971 	}
7972 
7973  issue_diag_reset:
7974 	rc = _base_diag_reset(ioc);
7975 	return rc;
7976 }
7977 
7978 /**
7979  * _base_make_ioc_operational - put controller in OPERATIONAL state
7980  * @ioc: per adapter object
7981  *
7982  * Return: 0 for success, non-zero for failure.
7983  */
7984 static int
_base_make_ioc_operational(struct MPT3SAS_ADAPTER * ioc)7985 _base_make_ioc_operational(struct MPT3SAS_ADAPTER *ioc)
7986 {
7987 	int r, i, index, rc;
7988 	unsigned long	flags;
7989 	u32 reply_address;
7990 	u16 smid;
7991 	struct _tr_list *delayed_tr, *delayed_tr_next;
7992 	struct _sc_list *delayed_sc, *delayed_sc_next;
7993 	struct _event_ack_list *delayed_event_ack, *delayed_event_ack_next;
7994 	u8 hide_flag;
7995 	struct adapter_reply_queue *reply_q;
7996 	Mpi2ReplyDescriptorsUnion_t *reply_post_free_contig;
7997 
7998 	dinitprintk(ioc, ioc_info(ioc, "%s\n", __func__));
7999 
8000 	/* clean the delayed target reset list */
8001 	list_for_each_entry_safe(delayed_tr, delayed_tr_next,
8002 	    &ioc->delayed_tr_list, list) {
8003 		list_del(&delayed_tr->list);
8004 		kfree(delayed_tr);
8005 	}
8006 
8007 
8008 	list_for_each_entry_safe(delayed_tr, delayed_tr_next,
8009 	    &ioc->delayed_tr_volume_list, list) {
8010 		list_del(&delayed_tr->list);
8011 		kfree(delayed_tr);
8012 	}
8013 
8014 	list_for_each_entry_safe(delayed_sc, delayed_sc_next,
8015 	    &ioc->delayed_sc_list, list) {
8016 		list_del(&delayed_sc->list);
8017 		kfree(delayed_sc);
8018 	}
8019 
8020 	list_for_each_entry_safe(delayed_event_ack, delayed_event_ack_next,
8021 	    &ioc->delayed_event_ack_list, list) {
8022 		list_del(&delayed_event_ack->list);
8023 		kfree(delayed_event_ack);
8024 	}
8025 
8026 	spin_lock_irqsave(&ioc->scsi_lookup_lock, flags);
8027 
8028 	/* hi-priority queue */
8029 	INIT_LIST_HEAD(&ioc->hpr_free_list);
8030 	smid = ioc->hi_priority_smid;
8031 	for (i = 0; i < ioc->hi_priority_depth; i++, smid++) {
8032 		ioc->hpr_lookup[i].cb_idx = 0xFF;
8033 		ioc->hpr_lookup[i].smid = smid;
8034 		list_add_tail(&ioc->hpr_lookup[i].tracker_list,
8035 		    &ioc->hpr_free_list);
8036 	}
8037 
8038 	/* internal queue */
8039 	INIT_LIST_HEAD(&ioc->internal_free_list);
8040 	smid = ioc->internal_smid;
8041 	for (i = 0; i < ioc->internal_depth; i++, smid++) {
8042 		ioc->internal_lookup[i].cb_idx = 0xFF;
8043 		ioc->internal_lookup[i].smid = smid;
8044 		list_add_tail(&ioc->internal_lookup[i].tracker_list,
8045 		    &ioc->internal_free_list);
8046 	}
8047 
8048 	spin_unlock_irqrestore(&ioc->scsi_lookup_lock, flags);
8049 
8050 	/* initialize Reply Free Queue */
8051 	for (i = 0, reply_address = (u32)ioc->reply_dma ;
8052 	    i < ioc->reply_free_queue_depth ; i++, reply_address +=
8053 	    ioc->reply_sz) {
8054 		ioc->reply_free[i] = cpu_to_le32(reply_address);
8055 		if (ioc->is_mcpu_endpoint)
8056 			_base_clone_reply_to_sys_mem(ioc,
8057 					reply_address, i);
8058 	}
8059 
8060 	/* initialize reply queues */
8061 	if (ioc->is_driver_loading)
8062 		_base_assign_reply_queues(ioc);
8063 
8064 	/* initialize Reply Post Free Queue */
8065 	index = 0;
8066 	reply_post_free_contig = ioc->reply_post[0].reply_post_free;
8067 	list_for_each_entry(reply_q, &ioc->reply_queue_list, list) {
8068 		/*
8069 		 * If RDPQ is enabled, switch to the next allocation.
8070 		 * Otherwise advance within the contiguous region.
8071 		 */
8072 		if (ioc->rdpq_array_enable) {
8073 			reply_q->reply_post_free =
8074 				ioc->reply_post[index++].reply_post_free;
8075 		} else {
8076 			reply_q->reply_post_free = reply_post_free_contig;
8077 			reply_post_free_contig += ioc->reply_post_queue_depth;
8078 		}
8079 
8080 		reply_q->reply_post_host_index = 0;
8081 		for (i = 0; i < ioc->reply_post_queue_depth; i++)
8082 			reply_q->reply_post_free[i].Words =
8083 			    cpu_to_le64(ULLONG_MAX);
8084 		if (!_base_is_controller_msix_enabled(ioc))
8085 			goto skip_init_reply_post_free_queue;
8086 	}
8087  skip_init_reply_post_free_queue:
8088 
8089 	r = _base_send_ioc_init(ioc);
8090 	if (r) {
8091 		/*
8092 		 * No need to check IOC state for fault state & issue
8093 		 * diag reset during host reset. This check is need
8094 		 * only during driver load time.
8095 		 */
8096 		if (!ioc->is_driver_loading)
8097 			return r;
8098 
8099 		rc = mpt3sas_base_check_for_fault_and_issue_reset(ioc);
8100 		if (rc || (_base_send_ioc_init(ioc)))
8101 			return r;
8102 	}
8103 
8104 	/* initialize reply free host index */
8105 	ioc->reply_free_host_index = ioc->reply_free_queue_depth - 1;
8106 	writel(ioc->reply_free_host_index, &ioc->chip->ReplyFreeHostIndex);
8107 
8108 	/* initialize reply post host index */
8109 	list_for_each_entry(reply_q, &ioc->reply_queue_list, list) {
8110 		if (ioc->combined_reply_queue)
8111 			writel((reply_q->msix_index & 7)<<
8112 			   MPI2_RPHI_MSIX_INDEX_SHIFT,
8113 			   ioc->replyPostRegisterIndex[reply_q->msix_index/8]);
8114 		else
8115 			writel(reply_q->msix_index <<
8116 				MPI2_RPHI_MSIX_INDEX_SHIFT,
8117 				&ioc->chip->ReplyPostHostIndex);
8118 
8119 		if (!_base_is_controller_msix_enabled(ioc))
8120 			goto skip_init_reply_post_host_index;
8121 	}
8122 
8123  skip_init_reply_post_host_index:
8124 
8125 	mpt3sas_base_unmask_interrupts(ioc);
8126 
8127 	if (ioc->hba_mpi_version_belonged != MPI2_VERSION) {
8128 		r = _base_display_fwpkg_version(ioc);
8129 		if (r)
8130 			return r;
8131 	}
8132 
8133 	r = _base_static_config_pages(ioc);
8134 	if (r)
8135 		return r;
8136 
8137 	r = _base_event_notification(ioc);
8138 	if (r)
8139 		return r;
8140 
8141 	if (!ioc->shost_recovery) {
8142 
8143 		if (ioc->is_warpdrive && ioc->manu_pg10.OEMIdentifier
8144 		    == 0x80) {
8145 			hide_flag = (u8) (
8146 			    le32_to_cpu(ioc->manu_pg10.OEMSpecificFlags0) &
8147 			    MFG_PAGE10_HIDE_SSDS_MASK);
8148 			if (hide_flag != MFG_PAGE10_HIDE_SSDS_MASK)
8149 				ioc->mfg_pg10_hide_flag = hide_flag;
8150 		}
8151 
8152 		ioc->wait_for_discovery_to_complete =
8153 		    _base_determine_wait_on_discovery(ioc);
8154 
8155 		return r; /* scan_start and scan_finished support */
8156 	}
8157 
8158 	r = _base_send_port_enable(ioc);
8159 	if (r)
8160 		return r;
8161 
8162 	return r;
8163 }
8164 
8165 /**
8166  * mpt3sas_base_free_resources - free resources controller resources
8167  * @ioc: per adapter object
8168  */
8169 void
mpt3sas_base_free_resources(struct MPT3SAS_ADAPTER * ioc)8170 mpt3sas_base_free_resources(struct MPT3SAS_ADAPTER *ioc)
8171 {
8172 	dexitprintk(ioc, ioc_info(ioc, "%s\n", __func__));
8173 
8174 	/* synchronizing freeing resource with pci_access_mutex lock */
8175 	mutex_lock(&ioc->pci_access_mutex);
8176 	if (ioc->chip_phys && ioc->chip) {
8177 		mpt3sas_base_mask_interrupts(ioc);
8178 		ioc->shost_recovery = 1;
8179 		mpt3sas_base_make_ioc_ready(ioc, SOFT_RESET);
8180 		ioc->shost_recovery = 0;
8181 	}
8182 
8183 	mpt3sas_base_unmap_resources(ioc);
8184 	mutex_unlock(&ioc->pci_access_mutex);
8185 	return;
8186 }
8187 
8188 /**
8189  * mpt3sas_base_attach - attach controller instance
8190  * @ioc: per adapter object
8191  *
8192  * Return: 0 for success, non-zero for failure.
8193  */
8194 int
mpt3sas_base_attach(struct MPT3SAS_ADAPTER * ioc)8195 mpt3sas_base_attach(struct MPT3SAS_ADAPTER *ioc)
8196 {
8197 	int r, i, rc;
8198 	int cpu_id, last_cpu_id = 0;
8199 
8200 	dinitprintk(ioc, ioc_info(ioc, "%s\n", __func__));
8201 
8202 	/* setup cpu_msix_table */
8203 	ioc->cpu_count = num_online_cpus();
8204 	for_each_online_cpu(cpu_id)
8205 		last_cpu_id = cpu_id;
8206 	ioc->cpu_msix_table_sz = last_cpu_id + 1;
8207 	ioc->cpu_msix_table = kzalloc(ioc->cpu_msix_table_sz, GFP_KERNEL);
8208 	ioc->reply_queue_count = 1;
8209 	if (!ioc->cpu_msix_table) {
8210 		ioc_info(ioc, "Allocation for cpu_msix_table failed!!!\n");
8211 		r = -ENOMEM;
8212 		goto out_free_resources;
8213 	}
8214 
8215 	if (ioc->is_warpdrive) {
8216 		ioc->reply_post_host_index = kcalloc(ioc->cpu_msix_table_sz,
8217 		    sizeof(resource_size_t *), GFP_KERNEL);
8218 		if (!ioc->reply_post_host_index) {
8219 			ioc_info(ioc, "Allocation for reply_post_host_index failed!!!\n");
8220 			r = -ENOMEM;
8221 			goto out_free_resources;
8222 		}
8223 	}
8224 
8225 	ioc->smp_affinity_enable = smp_affinity_enable;
8226 
8227 	ioc->rdpq_array_enable_assigned = 0;
8228 	ioc->use_32bit_dma = false;
8229 	ioc->dma_mask = 64;
8230 	if (ioc->is_aero_ioc) {
8231 		ioc->base_readl = &_base_readl_aero;
8232 		ioc->base_readl_ext_retry = &_base_readl_ext_retry;
8233 	} else {
8234 		ioc->base_readl = &_base_readl;
8235 		ioc->base_readl_ext_retry = &_base_readl;
8236 	}
8237 	r = mpt3sas_base_map_resources(ioc);
8238 	if (r)
8239 		goto out_free_resources;
8240 
8241 	pci_set_drvdata(ioc->pdev, ioc->shost);
8242 	r = _base_get_ioc_facts(ioc);
8243 	if (r) {
8244 		rc = mpt3sas_base_check_for_fault_and_issue_reset(ioc);
8245 		if (rc || (_base_get_ioc_facts(ioc)))
8246 			goto out_free_resources;
8247 	}
8248 
8249 	switch (ioc->hba_mpi_version_belonged) {
8250 	case MPI2_VERSION:
8251 		ioc->build_sg_scmd = &_base_build_sg_scmd;
8252 		ioc->build_sg = &_base_build_sg;
8253 		ioc->build_zero_len_sge = &_base_build_zero_len_sge;
8254 		ioc->get_msix_index_for_smlio = &_base_get_msix_index;
8255 		break;
8256 	case MPI25_VERSION:
8257 	case MPI26_VERSION:
8258 		/*
8259 		 * In SAS3.0,
8260 		 * SCSI_IO, SMP_PASSTHRU, SATA_PASSTHRU, Target Assist, and
8261 		 * Target Status - all require the IEEE formatted scatter gather
8262 		 * elements.
8263 		 */
8264 		ioc->build_sg_scmd = &_base_build_sg_scmd_ieee;
8265 		ioc->build_sg = &_base_build_sg_ieee;
8266 		ioc->build_nvme_prp = &_base_build_nvme_prp;
8267 		ioc->build_zero_len_sge = &_base_build_zero_len_sge_ieee;
8268 		ioc->sge_size_ieee = sizeof(Mpi2IeeeSgeSimple64_t);
8269 		if (ioc->high_iops_queues)
8270 			ioc->get_msix_index_for_smlio =
8271 					&_base_get_high_iops_msix_index;
8272 		else
8273 			ioc->get_msix_index_for_smlio = &_base_get_msix_index;
8274 		break;
8275 	}
8276 	if (ioc->atomic_desc_capable) {
8277 		ioc->put_smid_default = &_base_put_smid_default_atomic;
8278 		ioc->put_smid_scsi_io = &_base_put_smid_scsi_io_atomic;
8279 		ioc->put_smid_fast_path =
8280 				&_base_put_smid_fast_path_atomic;
8281 		ioc->put_smid_hi_priority =
8282 				&_base_put_smid_hi_priority_atomic;
8283 	} else {
8284 		ioc->put_smid_default = &_base_put_smid_default;
8285 		ioc->put_smid_fast_path = &_base_put_smid_fast_path;
8286 		ioc->put_smid_hi_priority = &_base_put_smid_hi_priority;
8287 		if (ioc->is_mcpu_endpoint)
8288 			ioc->put_smid_scsi_io =
8289 				&_base_put_smid_mpi_ep_scsi_io;
8290 		else
8291 			ioc->put_smid_scsi_io = &_base_put_smid_scsi_io;
8292 	}
8293 	/*
8294 	 * These function pointers for other requests that don't
8295 	 * the require IEEE scatter gather elements.
8296 	 *
8297 	 * For example Configuration Pages and SAS IOUNIT Control don't.
8298 	 */
8299 	ioc->build_sg_mpi = &_base_build_sg;
8300 	ioc->build_zero_len_sge_mpi = &_base_build_zero_len_sge;
8301 
8302 	r = mpt3sas_base_make_ioc_ready(ioc, SOFT_RESET);
8303 	if (r)
8304 		goto out_free_resources;
8305 
8306 	ioc->pfacts = kcalloc(ioc->facts.NumberOfPorts,
8307 	    sizeof(struct mpt3sas_port_facts), GFP_KERNEL);
8308 	if (!ioc->pfacts) {
8309 		r = -ENOMEM;
8310 		goto out_free_resources;
8311 	}
8312 
8313 	for (i = 0 ; i < ioc->facts.NumberOfPorts; i++) {
8314 		r = _base_get_port_facts(ioc, i);
8315 		if (r) {
8316 			rc = mpt3sas_base_check_for_fault_and_issue_reset(ioc);
8317 			if (rc || (_base_get_port_facts(ioc, i)))
8318 				goto out_free_resources;
8319 		}
8320 	}
8321 
8322 	r = _base_allocate_memory_pools(ioc);
8323 	if (r)
8324 		goto out_free_resources;
8325 
8326 	if (irqpoll_weight > 0)
8327 		ioc->thresh_hold = irqpoll_weight;
8328 	else
8329 		ioc->thresh_hold = ioc->hba_queue_depth/4;
8330 
8331 	_base_init_irqpolls(ioc);
8332 	init_waitqueue_head(&ioc->reset_wq);
8333 
8334 	/* allocate memory pd handle bitmask list */
8335 	ioc->pd_handles_sz = (ioc->facts.MaxDevHandle / 8);
8336 	if (ioc->facts.MaxDevHandle % 8)
8337 		ioc->pd_handles_sz++;
8338 	ioc->pd_handles = kzalloc(ioc->pd_handles_sz,
8339 	    GFP_KERNEL);
8340 	if (!ioc->pd_handles) {
8341 		r = -ENOMEM;
8342 		goto out_free_resources;
8343 	}
8344 	ioc->blocking_handles = kzalloc(ioc->pd_handles_sz,
8345 	    GFP_KERNEL);
8346 	if (!ioc->blocking_handles) {
8347 		r = -ENOMEM;
8348 		goto out_free_resources;
8349 	}
8350 
8351 	/* allocate memory for pending OS device add list */
8352 	ioc->pend_os_device_add_sz = (ioc->facts.MaxDevHandle / 8);
8353 	if (ioc->facts.MaxDevHandle % 8)
8354 		ioc->pend_os_device_add_sz++;
8355 	ioc->pend_os_device_add = kzalloc(ioc->pend_os_device_add_sz,
8356 	    GFP_KERNEL);
8357 	if (!ioc->pend_os_device_add) {
8358 		r = -ENOMEM;
8359 		goto out_free_resources;
8360 	}
8361 
8362 	ioc->device_remove_in_progress_sz = ioc->pend_os_device_add_sz;
8363 	ioc->device_remove_in_progress =
8364 		kzalloc(ioc->device_remove_in_progress_sz, GFP_KERNEL);
8365 	if (!ioc->device_remove_in_progress) {
8366 		r = -ENOMEM;
8367 		goto out_free_resources;
8368 	}
8369 
8370 	ioc->fwfault_debug = mpt3sas_fwfault_debug;
8371 
8372 	/* base internal command bits */
8373 	mutex_init(&ioc->base_cmds.mutex);
8374 	ioc->base_cmds.reply = kzalloc(ioc->reply_sz, GFP_KERNEL);
8375 	ioc->base_cmds.status = MPT3_CMD_NOT_USED;
8376 
8377 	/* port_enable command bits */
8378 	ioc->port_enable_cmds.reply = kzalloc(ioc->reply_sz, GFP_KERNEL);
8379 	ioc->port_enable_cmds.status = MPT3_CMD_NOT_USED;
8380 
8381 	/* transport internal command bits */
8382 	ioc->transport_cmds.reply = kzalloc(ioc->reply_sz, GFP_KERNEL);
8383 	ioc->transport_cmds.status = MPT3_CMD_NOT_USED;
8384 	mutex_init(&ioc->transport_cmds.mutex);
8385 
8386 	/* scsih internal command bits */
8387 	ioc->scsih_cmds.reply = kzalloc(ioc->reply_sz, GFP_KERNEL);
8388 	ioc->scsih_cmds.status = MPT3_CMD_NOT_USED;
8389 	mutex_init(&ioc->scsih_cmds.mutex);
8390 
8391 	/* task management internal command bits */
8392 	ioc->tm_cmds.reply = kzalloc(ioc->reply_sz, GFP_KERNEL);
8393 	ioc->tm_cmds.status = MPT3_CMD_NOT_USED;
8394 	mutex_init(&ioc->tm_cmds.mutex);
8395 
8396 	/* config page internal command bits */
8397 	ioc->config_cmds.reply = kzalloc(ioc->reply_sz, GFP_KERNEL);
8398 	ioc->config_cmds.status = MPT3_CMD_NOT_USED;
8399 	mutex_init(&ioc->config_cmds.mutex);
8400 
8401 	/* ctl module internal command bits */
8402 	ioc->ctl_cmds.reply = kzalloc(ioc->reply_sz, GFP_KERNEL);
8403 	ioc->ctl_cmds.sense = kzalloc(SCSI_SENSE_BUFFERSIZE, GFP_KERNEL);
8404 	ioc->ctl_cmds.status = MPT3_CMD_NOT_USED;
8405 	mutex_init(&ioc->ctl_cmds.mutex);
8406 
8407 	if (!ioc->base_cmds.reply || !ioc->port_enable_cmds.reply ||
8408 	    !ioc->transport_cmds.reply || !ioc->scsih_cmds.reply ||
8409 	    !ioc->tm_cmds.reply || !ioc->config_cmds.reply ||
8410 	    !ioc->ctl_cmds.reply || !ioc->ctl_cmds.sense) {
8411 		r = -ENOMEM;
8412 		goto out_free_resources;
8413 	}
8414 
8415 	for (i = 0; i < MPI2_EVENT_NOTIFY_EVENTMASK_WORDS; i++)
8416 		ioc->event_masks[i] = -1;
8417 
8418 	/* here we enable the events we care about */
8419 	_base_unmask_events(ioc, MPI2_EVENT_SAS_DISCOVERY);
8420 	_base_unmask_events(ioc, MPI2_EVENT_SAS_BROADCAST_PRIMITIVE);
8421 	_base_unmask_events(ioc, MPI2_EVENT_SAS_TOPOLOGY_CHANGE_LIST);
8422 	_base_unmask_events(ioc, MPI2_EVENT_SAS_DEVICE_STATUS_CHANGE);
8423 	_base_unmask_events(ioc, MPI2_EVENT_SAS_ENCL_DEVICE_STATUS_CHANGE);
8424 	_base_unmask_events(ioc, MPI2_EVENT_IR_CONFIGURATION_CHANGE_LIST);
8425 	_base_unmask_events(ioc, MPI2_EVENT_IR_VOLUME);
8426 	_base_unmask_events(ioc, MPI2_EVENT_IR_PHYSICAL_DISK);
8427 	_base_unmask_events(ioc, MPI2_EVENT_IR_OPERATION_STATUS);
8428 	_base_unmask_events(ioc, MPI2_EVENT_LOG_ENTRY_ADDED);
8429 	_base_unmask_events(ioc, MPI2_EVENT_TEMP_THRESHOLD);
8430 	_base_unmask_events(ioc, MPI2_EVENT_ACTIVE_CABLE_EXCEPTION);
8431 	_base_unmask_events(ioc, MPI2_EVENT_SAS_DEVICE_DISCOVERY_ERROR);
8432 	if (ioc->hba_mpi_version_belonged == MPI26_VERSION) {
8433 		if (ioc->is_gen35_ioc) {
8434 			_base_unmask_events(ioc,
8435 				MPI2_EVENT_PCIE_DEVICE_STATUS_CHANGE);
8436 			_base_unmask_events(ioc, MPI2_EVENT_PCIE_ENUMERATION);
8437 			_base_unmask_events(ioc,
8438 				MPI2_EVENT_PCIE_TOPOLOGY_CHANGE_LIST);
8439 		}
8440 	}
8441 	r = _base_make_ioc_operational(ioc);
8442 	if (r == -EAGAIN) {
8443 		r = _base_make_ioc_operational(ioc);
8444 		if (r)
8445 			goto out_free_resources;
8446 	}
8447 
8448 	/*
8449 	 * Copy current copy of IOCFacts in prev_fw_facts
8450 	 * and it will be used during online firmware upgrade.
8451 	 */
8452 	memcpy(&ioc->prev_fw_facts, &ioc->facts,
8453 	    sizeof(struct mpt3sas_facts));
8454 
8455 	ioc->non_operational_loop = 0;
8456 	ioc->ioc_coredump_loop = 0;
8457 	ioc->got_task_abort_from_ioctl = 0;
8458 	return 0;
8459 
8460  out_free_resources:
8461 
8462 	ioc->remove_host = 1;
8463 
8464 	mpt3sas_base_free_resources(ioc);
8465 	_base_release_memory_pools(ioc);
8466 	pci_set_drvdata(ioc->pdev, NULL);
8467 	kfree(ioc->cpu_msix_table);
8468 	if (ioc->is_warpdrive)
8469 		kfree(ioc->reply_post_host_index);
8470 	kfree(ioc->pd_handles);
8471 	kfree(ioc->blocking_handles);
8472 	kfree(ioc->device_remove_in_progress);
8473 	kfree(ioc->pend_os_device_add);
8474 	kfree(ioc->tm_cmds.reply);
8475 	kfree(ioc->transport_cmds.reply);
8476 	kfree(ioc->scsih_cmds.reply);
8477 	kfree(ioc->config_cmds.reply);
8478 	kfree(ioc->base_cmds.reply);
8479 	kfree(ioc->port_enable_cmds.reply);
8480 	kfree(ioc->ctl_cmds.reply);
8481 	kfree(ioc->ctl_cmds.sense);
8482 	kfree(ioc->pfacts);
8483 	ioc->ctl_cmds.reply = NULL;
8484 	ioc->base_cmds.reply = NULL;
8485 	ioc->tm_cmds.reply = NULL;
8486 	ioc->scsih_cmds.reply = NULL;
8487 	ioc->transport_cmds.reply = NULL;
8488 	ioc->config_cmds.reply = NULL;
8489 	ioc->pfacts = NULL;
8490 	return r;
8491 }
8492 
8493 
8494 /**
8495  * mpt3sas_base_detach - remove controller instance
8496  * @ioc: per adapter object
8497  */
8498 void
mpt3sas_base_detach(struct MPT3SAS_ADAPTER * ioc)8499 mpt3sas_base_detach(struct MPT3SAS_ADAPTER *ioc)
8500 {
8501 	dexitprintk(ioc, ioc_info(ioc, "%s\n", __func__));
8502 
8503 	mpt3sas_base_stop_watchdog(ioc);
8504 	mpt3sas_base_free_resources(ioc);
8505 	_base_release_memory_pools(ioc);
8506 	mpt3sas_free_enclosure_list(ioc);
8507 	pci_set_drvdata(ioc->pdev, NULL);
8508 	kfree(ioc->cpu_msix_table);
8509 	if (ioc->is_warpdrive)
8510 		kfree(ioc->reply_post_host_index);
8511 	kfree(ioc->pd_handles);
8512 	kfree(ioc->blocking_handles);
8513 	kfree(ioc->device_remove_in_progress);
8514 	kfree(ioc->pend_os_device_add);
8515 	kfree(ioc->pfacts);
8516 	kfree(ioc->ctl_cmds.reply);
8517 	kfree(ioc->ctl_cmds.sense);
8518 	kfree(ioc->base_cmds.reply);
8519 	kfree(ioc->port_enable_cmds.reply);
8520 	kfree(ioc->tm_cmds.reply);
8521 	kfree(ioc->transport_cmds.reply);
8522 	kfree(ioc->scsih_cmds.reply);
8523 	kfree(ioc->config_cmds.reply);
8524 }
8525 
8526 /**
8527  * _base_pre_reset_handler - pre reset handler
8528  * @ioc: per adapter object
8529  */
_base_pre_reset_handler(struct MPT3SAS_ADAPTER * ioc)8530 static void _base_pre_reset_handler(struct MPT3SAS_ADAPTER *ioc)
8531 {
8532 	mpt3sas_scsih_pre_reset_handler(ioc);
8533 	mpt3sas_ctl_pre_reset_handler(ioc);
8534 	dtmprintk(ioc, ioc_info(ioc, "%s: MPT3_IOC_PRE_RESET\n", __func__));
8535 }
8536 
8537 /**
8538  * _base_clear_outstanding_mpt_commands - clears outstanding mpt commands
8539  * @ioc: per adapter object
8540  */
8541 static void
_base_clear_outstanding_mpt_commands(struct MPT3SAS_ADAPTER * ioc)8542 _base_clear_outstanding_mpt_commands(struct MPT3SAS_ADAPTER *ioc)
8543 {
8544 	dtmprintk(ioc,
8545 	    ioc_info(ioc, "%s: clear outstanding mpt cmds\n", __func__));
8546 	if (ioc->transport_cmds.status & MPT3_CMD_PENDING) {
8547 		ioc->transport_cmds.status |= MPT3_CMD_RESET;
8548 		mpt3sas_base_free_smid(ioc, ioc->transport_cmds.smid);
8549 		complete(&ioc->transport_cmds.done);
8550 	}
8551 	if (ioc->base_cmds.status & MPT3_CMD_PENDING) {
8552 		ioc->base_cmds.status |= MPT3_CMD_RESET;
8553 		mpt3sas_base_free_smid(ioc, ioc->base_cmds.smid);
8554 		complete(&ioc->base_cmds.done);
8555 	}
8556 	if (ioc->port_enable_cmds.status & MPT3_CMD_PENDING) {
8557 		ioc->port_enable_failed = 1;
8558 		ioc->port_enable_cmds.status |= MPT3_CMD_RESET;
8559 		mpt3sas_base_free_smid(ioc, ioc->port_enable_cmds.smid);
8560 		if (ioc->is_driver_loading) {
8561 			ioc->start_scan_failed =
8562 				MPI2_IOCSTATUS_INTERNAL_ERROR;
8563 			ioc->start_scan = 0;
8564 		} else {
8565 			complete(&ioc->port_enable_cmds.done);
8566 		}
8567 	}
8568 	if (ioc->config_cmds.status & MPT3_CMD_PENDING) {
8569 		ioc->config_cmds.status |= MPT3_CMD_RESET;
8570 		mpt3sas_base_free_smid(ioc, ioc->config_cmds.smid);
8571 		ioc->config_cmds.smid = USHRT_MAX;
8572 		complete(&ioc->config_cmds.done);
8573 	}
8574 }
8575 
8576 /**
8577  * _base_clear_outstanding_commands - clear all outstanding commands
8578  * @ioc: per adapter object
8579  */
_base_clear_outstanding_commands(struct MPT3SAS_ADAPTER * ioc)8580 static void _base_clear_outstanding_commands(struct MPT3SAS_ADAPTER *ioc)
8581 {
8582 	mpt3sas_scsih_clear_outstanding_scsi_tm_commands(ioc);
8583 	mpt3sas_ctl_clear_outstanding_ioctls(ioc);
8584 	_base_clear_outstanding_mpt_commands(ioc);
8585 }
8586 
8587 /**
8588  * _base_reset_done_handler - reset done handler
8589  * @ioc: per adapter object
8590  */
_base_reset_done_handler(struct MPT3SAS_ADAPTER * ioc)8591 static void _base_reset_done_handler(struct MPT3SAS_ADAPTER *ioc)
8592 {
8593 	mpt3sas_scsih_reset_done_handler(ioc);
8594 	mpt3sas_ctl_reset_done_handler(ioc);
8595 	dtmprintk(ioc, ioc_info(ioc, "%s: MPT3_IOC_DONE_RESET\n", __func__));
8596 }
8597 
8598 /**
8599  * mpt3sas_wait_for_commands_to_complete - reset controller
8600  * @ioc: Pointer to MPT_ADAPTER structure
8601  *
8602  * This function is waiting 10s for all pending commands to complete
8603  * prior to putting controller in reset.
8604  */
8605 void
mpt3sas_wait_for_commands_to_complete(struct MPT3SAS_ADAPTER * ioc)8606 mpt3sas_wait_for_commands_to_complete(struct MPT3SAS_ADAPTER *ioc)
8607 {
8608 	u32 ioc_state;
8609 
8610 	ioc->pending_io_count = 0;
8611 
8612 	ioc_state = mpt3sas_base_get_iocstate(ioc, 0);
8613 	if ((ioc_state & MPI2_IOC_STATE_MASK) != MPI2_IOC_STATE_OPERATIONAL)
8614 		return;
8615 
8616 	/* pending command count */
8617 	ioc->pending_io_count = scsi_host_busy(ioc->shost);
8618 
8619 	if (!ioc->pending_io_count)
8620 		return;
8621 
8622 	/* wait for pending commands to complete */
8623 	wait_event_timeout(ioc->reset_wq, ioc->pending_io_count == 0, 10 * HZ);
8624 }
8625 
8626 /**
8627  * _base_check_ioc_facts_changes - Look for increase/decrease of IOCFacts
8628  *     attributes during online firmware upgrade and update the corresponding
8629  *     IOC variables accordingly.
8630  *
8631  * @ioc: Pointer to MPT_ADAPTER structure
8632  */
8633 static int
_base_check_ioc_facts_changes(struct MPT3SAS_ADAPTER * ioc)8634 _base_check_ioc_facts_changes(struct MPT3SAS_ADAPTER *ioc)
8635 {
8636 	u16 pd_handles_sz;
8637 	void *pd_handles = NULL, *blocking_handles = NULL;
8638 	void *pend_os_device_add = NULL, *device_remove_in_progress = NULL;
8639 	struct mpt3sas_facts *old_facts = &ioc->prev_fw_facts;
8640 
8641 	if (ioc->facts.MaxDevHandle > old_facts->MaxDevHandle) {
8642 		pd_handles_sz = (ioc->facts.MaxDevHandle / 8);
8643 		if (ioc->facts.MaxDevHandle % 8)
8644 			pd_handles_sz++;
8645 
8646 		pd_handles = krealloc(ioc->pd_handles, pd_handles_sz,
8647 		    GFP_KERNEL);
8648 		if (!pd_handles) {
8649 			ioc_info(ioc,
8650 			    "Unable to allocate the memory for pd_handles of sz: %d\n",
8651 			    pd_handles_sz);
8652 			return -ENOMEM;
8653 		}
8654 		memset(pd_handles + ioc->pd_handles_sz, 0,
8655 		    (pd_handles_sz - ioc->pd_handles_sz));
8656 		ioc->pd_handles = pd_handles;
8657 
8658 		blocking_handles = krealloc(ioc->blocking_handles,
8659 		    pd_handles_sz, GFP_KERNEL);
8660 		if (!blocking_handles) {
8661 			ioc_info(ioc,
8662 			    "Unable to allocate the memory for "
8663 			    "blocking_handles of sz: %d\n",
8664 			    pd_handles_sz);
8665 			return -ENOMEM;
8666 		}
8667 		memset(blocking_handles + ioc->pd_handles_sz, 0,
8668 		    (pd_handles_sz - ioc->pd_handles_sz));
8669 		ioc->blocking_handles = blocking_handles;
8670 		ioc->pd_handles_sz = pd_handles_sz;
8671 
8672 		pend_os_device_add = krealloc(ioc->pend_os_device_add,
8673 		    pd_handles_sz, GFP_KERNEL);
8674 		if (!pend_os_device_add) {
8675 			ioc_info(ioc,
8676 			    "Unable to allocate the memory for pend_os_device_add of sz: %d\n",
8677 			    pd_handles_sz);
8678 			return -ENOMEM;
8679 		}
8680 		memset(pend_os_device_add + ioc->pend_os_device_add_sz, 0,
8681 		    (pd_handles_sz - ioc->pend_os_device_add_sz));
8682 		ioc->pend_os_device_add = pend_os_device_add;
8683 		ioc->pend_os_device_add_sz = pd_handles_sz;
8684 
8685 		device_remove_in_progress = krealloc(
8686 		    ioc->device_remove_in_progress, pd_handles_sz, GFP_KERNEL);
8687 		if (!device_remove_in_progress) {
8688 			ioc_info(ioc,
8689 			    "Unable to allocate the memory for "
8690 			    "device_remove_in_progress of sz: %d\n "
8691 			    , pd_handles_sz);
8692 			return -ENOMEM;
8693 		}
8694 		memset(device_remove_in_progress +
8695 		    ioc->device_remove_in_progress_sz, 0,
8696 		    (pd_handles_sz - ioc->device_remove_in_progress_sz));
8697 		ioc->device_remove_in_progress = device_remove_in_progress;
8698 		ioc->device_remove_in_progress_sz = pd_handles_sz;
8699 	}
8700 
8701 	memcpy(&ioc->prev_fw_facts, &ioc->facts, sizeof(struct mpt3sas_facts));
8702 	return 0;
8703 }
8704 
8705 /**
8706  * mpt3sas_base_hard_reset_handler - reset controller
8707  * @ioc: Pointer to MPT_ADAPTER structure
8708  * @type: FORCE_BIG_HAMMER or SOFT_RESET
8709  *
8710  * Return: 0 for success, non-zero for failure.
8711  */
8712 int
mpt3sas_base_hard_reset_handler(struct MPT3SAS_ADAPTER * ioc,enum reset_type type)8713 mpt3sas_base_hard_reset_handler(struct MPT3SAS_ADAPTER *ioc,
8714 	enum reset_type type)
8715 {
8716 	int r;
8717 	unsigned long flags;
8718 	u32 ioc_state;
8719 	u8 is_fault = 0, is_trigger = 0;
8720 
8721 	dtmprintk(ioc, ioc_info(ioc, "%s: enter\n", __func__));
8722 
8723 	if (ioc->pci_error_recovery) {
8724 		ioc_err(ioc, "%s: pci error recovery reset\n", __func__);
8725 		r = 0;
8726 		goto out_unlocked;
8727 	}
8728 
8729 	if (mpt3sas_fwfault_debug)
8730 		mpt3sas_halt_firmware(ioc);
8731 
8732 	/* wait for an active reset in progress to complete */
8733 	mutex_lock(&ioc->reset_in_progress_mutex);
8734 
8735 	spin_lock_irqsave(&ioc->ioc_reset_in_progress_lock, flags);
8736 	ioc->shost_recovery = 1;
8737 	spin_unlock_irqrestore(&ioc->ioc_reset_in_progress_lock, flags);
8738 
8739 	if ((ioc->diag_buffer_status[MPI2_DIAG_BUF_TYPE_TRACE] &
8740 	    MPT3_DIAG_BUFFER_IS_REGISTERED) &&
8741 	    (!(ioc->diag_buffer_status[MPI2_DIAG_BUF_TYPE_TRACE] &
8742 	    MPT3_DIAG_BUFFER_IS_RELEASED))) {
8743 		is_trigger = 1;
8744 		ioc_state = mpt3sas_base_get_iocstate(ioc, 0);
8745 		if ((ioc_state & MPI2_IOC_STATE_MASK) == MPI2_IOC_STATE_FAULT ||
8746 		    (ioc_state & MPI2_IOC_STATE_MASK) ==
8747 		    MPI2_IOC_STATE_COREDUMP) {
8748 			is_fault = 1;
8749 			ioc->htb_rel.trigger_info_dwords[1] =
8750 			    (ioc_state & MPI2_DOORBELL_DATA_MASK);
8751 		}
8752 	}
8753 	_base_pre_reset_handler(ioc);
8754 	mpt3sas_wait_for_commands_to_complete(ioc);
8755 	mpt3sas_base_mask_interrupts(ioc);
8756 	mpt3sas_base_pause_mq_polling(ioc);
8757 	r = mpt3sas_base_make_ioc_ready(ioc, type);
8758 	if (r)
8759 		goto out;
8760 	_base_clear_outstanding_commands(ioc);
8761 
8762 	/* If this hard reset is called while port enable is active, then
8763 	 * there is no reason to call make_ioc_operational
8764 	 */
8765 	if (ioc->is_driver_loading && ioc->port_enable_failed) {
8766 		ioc->remove_host = 1;
8767 		r = -EFAULT;
8768 		goto out;
8769 	}
8770 	r = _base_get_ioc_facts(ioc);
8771 	if (r)
8772 		goto out;
8773 
8774 	r = _base_check_ioc_facts_changes(ioc);
8775 	if (r) {
8776 		ioc_info(ioc,
8777 		    "Some of the parameters got changed in this new firmware"
8778 		    " image and it requires system reboot\n");
8779 		goto out;
8780 	}
8781 	if (ioc->rdpq_array_enable && !ioc->rdpq_array_capable)
8782 		panic("%s: Issue occurred with flashing controller firmware."
8783 		      "Please reboot the system and ensure that the correct"
8784 		      " firmware version is running\n", ioc->name);
8785 
8786 	r = _base_make_ioc_operational(ioc);
8787 	if (!r)
8788 		_base_reset_done_handler(ioc);
8789 
8790  out:
8791 	ioc_info(ioc, "%s: %s\n", __func__, r == 0 ? "SUCCESS" : "FAILED");
8792 
8793 	spin_lock_irqsave(&ioc->ioc_reset_in_progress_lock, flags);
8794 	ioc->shost_recovery = 0;
8795 	spin_unlock_irqrestore(&ioc->ioc_reset_in_progress_lock, flags);
8796 	ioc->ioc_reset_count++;
8797 	mutex_unlock(&ioc->reset_in_progress_mutex);
8798 	mpt3sas_base_resume_mq_polling(ioc);
8799 
8800  out_unlocked:
8801 	if ((r == 0) && is_trigger) {
8802 		if (is_fault)
8803 			mpt3sas_trigger_master(ioc, MASTER_TRIGGER_FW_FAULT);
8804 		else
8805 			mpt3sas_trigger_master(ioc,
8806 			    MASTER_TRIGGER_ADAPTER_RESET);
8807 	}
8808 	dtmprintk(ioc, ioc_info(ioc, "%s: exit\n", __func__));
8809 	return r;
8810 }
8811