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