vt: vt_ioctl: fix VT_DISALLOCATE freeing in-use virtual console
[linux/fpc-iii.git] / drivers / scsi / mpt3sas / mpt3sas_base.c
blob2c556c7fcf0dc69e338b156389c690fdbce63929
1 /*
2 * This is the Fusion MPT base driver providing common API layer interface
3 * for access to MPT (Message Passing Technology) firmware.
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)
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.
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.
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.
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
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.
46 #include <linux/kernel.h>
47 #include <linux/module.h>
48 #include <linux/errno.h>
49 #include <linux/init.h>
50 #include <linux/slab.h>
51 #include <linux/types.h>
52 #include <linux/pci.h>
53 #include <linux/kdev_t.h>
54 #include <linux/blkdev.h>
55 #include <linux/delay.h>
56 #include <linux/interrupt.h>
57 #include <linux/dma-mapping.h>
58 #include <linux/io.h>
59 #include <linux/time.h>
60 #include <linux/ktime.h>
61 #include <linux/kthread.h>
62 #include <asm/page.h> /* To get host page size per arch */
63 #include <linux/aer.h>
66 #include "mpt3sas_base.h"
68 static MPT_CALLBACK mpt_callbacks[MPT_MAX_CALLBACKS];
71 #define FAULT_POLLING_INTERVAL 1000 /* in milliseconds */
73 /* maximum controller queue depth */
74 #define MAX_HBA_QUEUE_DEPTH 30000
75 #define MAX_CHAIN_DEPTH 100000
76 static int max_queue_depth = -1;
77 module_param(max_queue_depth, int, 0);
78 MODULE_PARM_DESC(max_queue_depth, " max controller queue depth ");
80 static int max_sgl_entries = -1;
81 module_param(max_sgl_entries, int, 0);
82 MODULE_PARM_DESC(max_sgl_entries, " max sg entries ");
84 static int msix_disable = -1;
85 module_param(msix_disable, int, 0);
86 MODULE_PARM_DESC(msix_disable, " disable msix routed interrupts (default=0)");
88 static int smp_affinity_enable = 1;
89 module_param(smp_affinity_enable, int, S_IRUGO);
90 MODULE_PARM_DESC(smp_affinity_enable, "SMP affinity feature enable/disable Default: enable(1)");
92 static int max_msix_vectors = -1;
93 module_param(max_msix_vectors, int, 0);
94 MODULE_PARM_DESC(max_msix_vectors,
95 " max msix vectors");
97 static int mpt3sas_fwfault_debug;
98 MODULE_PARM_DESC(mpt3sas_fwfault_debug,
99 " enable detection of firmware fault and halt firmware - (default=0)");
101 static int
102 _base_get_ioc_facts(struct MPT3SAS_ADAPTER *ioc);
105 * mpt3sas_base_check_cmd_timeout - Function
106 * to check timeout and command termination due
107 * to Host reset.
109 * @ioc: per adapter object.
110 * @status: Status of issued command.
111 * @mpi_request:mf request pointer.
112 * @sz: size of buffer.
114 * @Returns - 1/0 Reset to be done or Not
117 mpt3sas_base_check_cmd_timeout(struct MPT3SAS_ADAPTER *ioc,
118 u8 status, void *mpi_request, int sz)
120 u8 issue_reset = 0;
122 if (!(status & MPT3_CMD_RESET))
123 issue_reset = 1;
125 pr_err(MPT3SAS_FMT "Command %s\n", ioc->name,
126 ((issue_reset == 0) ? "terminated due to Host Reset" : "Timeout"));
127 _debug_dump_mf(mpi_request, sz);
129 return issue_reset;
133 * _scsih_set_fwfault_debug - global setting of ioc->fwfault_debug.
134 * @val: ?
135 * @kp: ?
137 * Return: ?
139 static int
140 _scsih_set_fwfault_debug(const char *val, const struct kernel_param *kp)
142 int ret = param_set_int(val, kp);
143 struct MPT3SAS_ADAPTER *ioc;
145 if (ret)
146 return ret;
148 /* global ioc spinlock to protect controller list on list operations */
149 pr_info("setting fwfault_debug(%d)\n", mpt3sas_fwfault_debug);
150 spin_lock(&gioc_lock);
151 list_for_each_entry(ioc, &mpt3sas_ioc_list, list)
152 ioc->fwfault_debug = mpt3sas_fwfault_debug;
153 spin_unlock(&gioc_lock);
154 return 0;
156 module_param_call(mpt3sas_fwfault_debug, _scsih_set_fwfault_debug,
157 param_get_int, &mpt3sas_fwfault_debug, 0644);
160 * _base_clone_reply_to_sys_mem - copies reply to reply free iomem
161 * in BAR0 space.
163 * @ioc: per adapter object
164 * @reply: reply message frame(lower 32bit addr)
165 * @index: System request message index.
167 static void
168 _base_clone_reply_to_sys_mem(struct MPT3SAS_ADAPTER *ioc, u32 reply,
169 u32 index)
172 * 256 is offset within sys register.
173 * 256 offset MPI frame starts. Max MPI frame supported is 32.
174 * 32 * 128 = 4K. From here, Clone of reply free for mcpu starts
176 u16 cmd_credit = ioc->facts.RequestCredit + 1;
177 void __iomem *reply_free_iomem = (void __iomem *)ioc->chip +
178 MPI_FRAME_START_OFFSET +
179 (cmd_credit * ioc->request_sz) + (index * sizeof(u32));
181 writel(reply, reply_free_iomem);
185 * _base_clone_mpi_to_sys_mem - Writes/copies MPI frames
186 * to system/BAR0 region.
188 * @dst_iomem: Pointer to the destination location in BAR0 space.
189 * @src: Pointer to the Source data.
190 * @size: Size of data to be copied.
192 static void
193 _base_clone_mpi_to_sys_mem(void *dst_iomem, void *src, u32 size)
195 int i;
196 u32 *src_virt_mem = (u32 *)src;
198 for (i = 0; i < size/4; i++)
199 writel((u32)src_virt_mem[i],
200 (void __iomem *)dst_iomem + (i * 4));
204 * _base_clone_to_sys_mem - Writes/copies data to system/BAR0 region
206 * @dst_iomem: Pointer to the destination location in BAR0 space.
207 * @src: Pointer to the Source data.
208 * @size: Size of data to be copied.
210 static void
211 _base_clone_to_sys_mem(void __iomem *dst_iomem, void *src, u32 size)
213 int i;
214 u32 *src_virt_mem = (u32 *)(src);
216 for (i = 0; i < size/4; i++)
217 writel((u32)src_virt_mem[i],
218 (void __iomem *)dst_iomem + (i * 4));
222 * _base_get_chain - Calculates and Returns virtual chain address
223 * for the provided smid in BAR0 space.
225 * @ioc: per adapter object
226 * @smid: system request message index
227 * @sge_chain_count: Scatter gather chain count.
229 * Return: the chain address.
231 static inline void __iomem*
232 _base_get_chain(struct MPT3SAS_ADAPTER *ioc, u16 smid,
233 u8 sge_chain_count)
235 void __iomem *base_chain, *chain_virt;
236 u16 cmd_credit = ioc->facts.RequestCredit + 1;
238 base_chain = (void __iomem *)ioc->chip + MPI_FRAME_START_OFFSET +
239 (cmd_credit * ioc->request_sz) +
240 REPLY_FREE_POOL_SIZE;
241 chain_virt = base_chain + (smid * ioc->facts.MaxChainDepth *
242 ioc->request_sz) + (sge_chain_count * ioc->request_sz);
243 return chain_virt;
247 * _base_get_chain_phys - Calculates and Returns physical address
248 * in BAR0 for scatter gather chains, for
249 * the provided smid.
251 * @ioc: per adapter object
252 * @smid: system request message index
253 * @sge_chain_count: Scatter gather chain count.
255 * Return: Physical chain address.
257 static inline phys_addr_t
258 _base_get_chain_phys(struct MPT3SAS_ADAPTER *ioc, u16 smid,
259 u8 sge_chain_count)
261 phys_addr_t base_chain_phys, chain_phys;
262 u16 cmd_credit = ioc->facts.RequestCredit + 1;
264 base_chain_phys = ioc->chip_phys + MPI_FRAME_START_OFFSET +
265 (cmd_credit * ioc->request_sz) +
266 REPLY_FREE_POOL_SIZE;
267 chain_phys = base_chain_phys + (smid * ioc->facts.MaxChainDepth *
268 ioc->request_sz) + (sge_chain_count * ioc->request_sz);
269 return chain_phys;
273 * _base_get_buffer_bar0 - Calculates and Returns BAR0 mapped Host
274 * buffer address for the provided smid.
275 * (Each smid can have 64K starts from 17024)
277 * @ioc: per adapter object
278 * @smid: system request message index
280 * Return: Pointer to buffer location in BAR0.
283 static void __iomem *
284 _base_get_buffer_bar0(struct MPT3SAS_ADAPTER *ioc, u16 smid)
286 u16 cmd_credit = ioc->facts.RequestCredit + 1;
287 // Added extra 1 to reach end of chain.
288 void __iomem *chain_end = _base_get_chain(ioc,
289 cmd_credit + 1,
290 ioc->facts.MaxChainDepth);
291 return chain_end + (smid * 64 * 1024);
295 * _base_get_buffer_phys_bar0 - Calculates and Returns BAR0 mapped
296 * Host buffer Physical address for the provided smid.
297 * (Each smid can have 64K starts from 17024)
299 * @ioc: per adapter object
300 * @smid: system request message index
302 * Return: Pointer to buffer location in BAR0.
304 static phys_addr_t
305 _base_get_buffer_phys_bar0(struct MPT3SAS_ADAPTER *ioc, u16 smid)
307 u16 cmd_credit = ioc->facts.RequestCredit + 1;
308 phys_addr_t chain_end_phys = _base_get_chain_phys(ioc,
309 cmd_credit + 1,
310 ioc->facts.MaxChainDepth);
311 return chain_end_phys + (smid * 64 * 1024);
315 * _base_get_chain_buffer_dma_to_chain_buffer - Iterates chain
316 * lookup list and Provides chain_buffer
317 * address for the matching dma address.
318 * (Each smid can have 64K starts from 17024)
320 * @ioc: per adapter object
321 * @chain_buffer_dma: Chain buffer dma address.
323 * Return: Pointer to chain buffer. Or Null on Failure.
325 static void *
326 _base_get_chain_buffer_dma_to_chain_buffer(struct MPT3SAS_ADAPTER *ioc,
327 dma_addr_t chain_buffer_dma)
329 u16 index, j;
330 struct chain_tracker *ct;
332 for (index = 0; index < ioc->scsiio_depth; index++) {
333 for (j = 0; j < ioc->chains_needed_per_io; j++) {
334 ct = &ioc->chain_lookup[index].chains_per_smid[j];
335 if (ct && ct->chain_buffer_dma == chain_buffer_dma)
336 return ct->chain_buffer;
339 pr_info(MPT3SAS_FMT
340 "Provided chain_buffer_dma address is not in the lookup list\n",
341 ioc->name);
342 return NULL;
346 * _clone_sg_entries - MPI EP's scsiio and config requests
347 * are handled here. Base function for
348 * double buffering, before submitting
349 * the requests.
351 * @ioc: per adapter object.
352 * @mpi_request: mf request pointer.
353 * @smid: system request message index.
355 static void _clone_sg_entries(struct MPT3SAS_ADAPTER *ioc,
356 void *mpi_request, u16 smid)
358 Mpi2SGESimple32_t *sgel, *sgel_next;
359 u32 sgl_flags, sge_chain_count = 0;
360 bool is_write = 0;
361 u16 i = 0;
362 void __iomem *buffer_iomem;
363 phys_addr_t buffer_iomem_phys;
364 void __iomem *buff_ptr;
365 phys_addr_t buff_ptr_phys;
366 void __iomem *dst_chain_addr[MCPU_MAX_CHAINS_PER_IO];
367 void *src_chain_addr[MCPU_MAX_CHAINS_PER_IO];
368 phys_addr_t dst_addr_phys;
369 MPI2RequestHeader_t *request_hdr;
370 struct scsi_cmnd *scmd;
371 struct scatterlist *sg_scmd = NULL;
372 int is_scsiio_req = 0;
374 request_hdr = (MPI2RequestHeader_t *) mpi_request;
376 if (request_hdr->Function == MPI2_FUNCTION_SCSI_IO_REQUEST) {
377 Mpi25SCSIIORequest_t *scsiio_request =
378 (Mpi25SCSIIORequest_t *)mpi_request;
379 sgel = (Mpi2SGESimple32_t *) &scsiio_request->SGL;
380 is_scsiio_req = 1;
381 } else if (request_hdr->Function == MPI2_FUNCTION_CONFIG) {
382 Mpi2ConfigRequest_t *config_req =
383 (Mpi2ConfigRequest_t *)mpi_request;
384 sgel = (Mpi2SGESimple32_t *) &config_req->PageBufferSGE;
385 } else
386 return;
388 /* From smid we can get scsi_cmd, once we have sg_scmd,
389 * we just need to get sg_virt and sg_next to get virual
390 * address associated with sgel->Address.
393 if (is_scsiio_req) {
394 /* Get scsi_cmd using smid */
395 scmd = mpt3sas_scsih_scsi_lookup_get(ioc, smid);
396 if (scmd == NULL) {
397 pr_err(MPT3SAS_FMT "scmd is NULL\n", ioc->name);
398 return;
401 /* Get sg_scmd from scmd provided */
402 sg_scmd = scsi_sglist(scmd);
406 * 0 - 255 System register
407 * 256 - 4352 MPI Frame. (This is based on maxCredit 32)
408 * 4352 - 4864 Reply_free pool (512 byte is reserved
409 * considering maxCredit 32. Reply need extra
410 * room, for mCPU case kept four times of
411 * maxCredit).
412 * 4864 - 17152 SGE chain element. (32cmd * 3 chain of
413 * 128 byte size = 12288)
414 * 17152 - x Host buffer mapped with smid.
415 * (Each smid can have 64K Max IO.)
416 * BAR0+Last 1K MSIX Addr and Data
417 * Total size in use 2113664 bytes of 4MB BAR0
420 buffer_iomem = _base_get_buffer_bar0(ioc, smid);
421 buffer_iomem_phys = _base_get_buffer_phys_bar0(ioc, smid);
423 buff_ptr = buffer_iomem;
424 buff_ptr_phys = buffer_iomem_phys;
425 WARN_ON(buff_ptr_phys > U32_MAX);
427 if (le32_to_cpu(sgel->FlagsLength) &
428 (MPI2_SGE_FLAGS_HOST_TO_IOC << MPI2_SGE_FLAGS_SHIFT))
429 is_write = 1;
431 for (i = 0; i < MPT_MIN_PHYS_SEGMENTS + ioc->facts.MaxChainDepth; i++) {
433 sgl_flags =
434 (le32_to_cpu(sgel->FlagsLength) >> MPI2_SGE_FLAGS_SHIFT);
436 switch (sgl_flags & MPI2_SGE_FLAGS_ELEMENT_MASK) {
437 case MPI2_SGE_FLAGS_CHAIN_ELEMENT:
439 * Helper function which on passing
440 * chain_buffer_dma returns chain_buffer. Get
441 * the virtual address for sgel->Address
443 sgel_next =
444 _base_get_chain_buffer_dma_to_chain_buffer(ioc,
445 le32_to_cpu(sgel->Address));
446 if (sgel_next == NULL)
447 return;
449 * This is coping 128 byte chain
450 * frame (not a host buffer)
452 dst_chain_addr[sge_chain_count] =
453 _base_get_chain(ioc,
454 smid, sge_chain_count);
455 src_chain_addr[sge_chain_count] =
456 (void *) sgel_next;
457 dst_addr_phys = _base_get_chain_phys(ioc,
458 smid, sge_chain_count);
459 WARN_ON(dst_addr_phys > U32_MAX);
460 sgel->Address =
461 cpu_to_le32(lower_32_bits(dst_addr_phys));
462 sgel = sgel_next;
463 sge_chain_count++;
464 break;
465 case MPI2_SGE_FLAGS_SIMPLE_ELEMENT:
466 if (is_write) {
467 if (is_scsiio_req) {
468 _base_clone_to_sys_mem(buff_ptr,
469 sg_virt(sg_scmd),
470 (le32_to_cpu(sgel->FlagsLength) &
471 0x00ffffff));
473 * FIXME: this relies on a a zero
474 * PCI mem_offset.
476 sgel->Address =
477 cpu_to_le32((u32)buff_ptr_phys);
478 } else {
479 _base_clone_to_sys_mem(buff_ptr,
480 ioc->config_vaddr,
481 (le32_to_cpu(sgel->FlagsLength) &
482 0x00ffffff));
483 sgel->Address =
484 cpu_to_le32((u32)buff_ptr_phys);
487 buff_ptr += (le32_to_cpu(sgel->FlagsLength) &
488 0x00ffffff);
489 buff_ptr_phys += (le32_to_cpu(sgel->FlagsLength) &
490 0x00ffffff);
491 if ((le32_to_cpu(sgel->FlagsLength) &
492 (MPI2_SGE_FLAGS_END_OF_BUFFER
493 << MPI2_SGE_FLAGS_SHIFT)))
494 goto eob_clone_chain;
495 else {
497 * Every single element in MPT will have
498 * associated sg_next. Better to sanity that
499 * sg_next is not NULL, but it will be a bug
500 * if it is null.
502 if (is_scsiio_req) {
503 sg_scmd = sg_next(sg_scmd);
504 if (sg_scmd)
505 sgel++;
506 else
507 goto eob_clone_chain;
510 break;
514 eob_clone_chain:
515 for (i = 0; i < sge_chain_count; i++) {
516 if (is_scsiio_req)
517 _base_clone_to_sys_mem(dst_chain_addr[i],
518 src_chain_addr[i], ioc->request_sz);
523 * mpt3sas_remove_dead_ioc_func - kthread context to remove dead ioc
524 * @arg: input argument, used to derive ioc
526 * Return:
527 * 0 if controller is removed from pci subsystem.
528 * -1 for other case.
530 static int mpt3sas_remove_dead_ioc_func(void *arg)
532 struct MPT3SAS_ADAPTER *ioc = (struct MPT3SAS_ADAPTER *)arg;
533 struct pci_dev *pdev;
535 if ((ioc == NULL))
536 return -1;
538 pdev = ioc->pdev;
539 if ((pdev == NULL))
540 return -1;
541 pci_stop_and_remove_bus_device_locked(pdev);
542 return 0;
546 * _base_fault_reset_work - workq handling ioc fault conditions
547 * @work: input argument, used to derive ioc
549 * Context: sleep.
551 static void
552 _base_fault_reset_work(struct work_struct *work)
554 struct MPT3SAS_ADAPTER *ioc =
555 container_of(work, struct MPT3SAS_ADAPTER, fault_reset_work.work);
556 unsigned long flags;
557 u32 doorbell;
558 int rc;
559 struct task_struct *p;
562 spin_lock_irqsave(&ioc->ioc_reset_in_progress_lock, flags);
563 if (ioc->shost_recovery || ioc->pci_error_recovery)
564 goto rearm_timer;
565 spin_unlock_irqrestore(&ioc->ioc_reset_in_progress_lock, flags);
567 doorbell = mpt3sas_base_get_iocstate(ioc, 0);
568 if ((doorbell & MPI2_IOC_STATE_MASK) == MPI2_IOC_STATE_MASK) {
569 pr_err(MPT3SAS_FMT "SAS host is non-operational !!!!\n",
570 ioc->name);
572 /* It may be possible that EEH recovery can resolve some of
573 * pci bus failure issues rather removing the dead ioc function
574 * by considering controller is in a non-operational state. So
575 * here priority is given to the EEH recovery. If it doesn't
576 * not resolve this issue, mpt3sas driver will consider this
577 * controller to non-operational state and remove the dead ioc
578 * function.
580 if (ioc->non_operational_loop++ < 5) {
581 spin_lock_irqsave(&ioc->ioc_reset_in_progress_lock,
582 flags);
583 goto rearm_timer;
587 * Call _scsih_flush_pending_cmds callback so that we flush all
588 * pending commands back to OS. This call is required to aovid
589 * deadlock at block layer. Dead IOC will fail to do diag reset,
590 * and this call is safe since dead ioc will never return any
591 * command back from HW.
593 ioc->schedule_dead_ioc_flush_running_cmds(ioc);
595 * Set remove_host flag early since kernel thread will
596 * take some time to execute.
598 ioc->remove_host = 1;
599 /*Remove the Dead Host */
600 p = kthread_run(mpt3sas_remove_dead_ioc_func, ioc,
601 "%s_dead_ioc_%d", ioc->driver_name, ioc->id);
602 if (IS_ERR(p))
603 pr_err(MPT3SAS_FMT
604 "%s: Running mpt3sas_dead_ioc thread failed !!!!\n",
605 ioc->name, __func__);
606 else
607 pr_err(MPT3SAS_FMT
608 "%s: Running mpt3sas_dead_ioc thread success !!!!\n",
609 ioc->name, __func__);
610 return; /* don't rearm timer */
613 ioc->non_operational_loop = 0;
615 if ((doorbell & MPI2_IOC_STATE_MASK) != MPI2_IOC_STATE_OPERATIONAL) {
616 rc = mpt3sas_base_hard_reset_handler(ioc, FORCE_BIG_HAMMER);
617 pr_warn(MPT3SAS_FMT "%s: hard reset: %s\n", ioc->name,
618 __func__, (rc == 0) ? "success" : "failed");
619 doorbell = mpt3sas_base_get_iocstate(ioc, 0);
620 if ((doorbell & MPI2_IOC_STATE_MASK) == MPI2_IOC_STATE_FAULT)
621 mpt3sas_base_fault_info(ioc, doorbell &
622 MPI2_DOORBELL_DATA_MASK);
623 if (rc && (doorbell & MPI2_IOC_STATE_MASK) !=
624 MPI2_IOC_STATE_OPERATIONAL)
625 return; /* don't rearm timer */
628 spin_lock_irqsave(&ioc->ioc_reset_in_progress_lock, flags);
629 rearm_timer:
630 if (ioc->fault_reset_work_q)
631 queue_delayed_work(ioc->fault_reset_work_q,
632 &ioc->fault_reset_work,
633 msecs_to_jiffies(FAULT_POLLING_INTERVAL));
634 spin_unlock_irqrestore(&ioc->ioc_reset_in_progress_lock, flags);
638 * mpt3sas_base_start_watchdog - start the fault_reset_work_q
639 * @ioc: per adapter object
641 * Context: sleep.
643 void
644 mpt3sas_base_start_watchdog(struct MPT3SAS_ADAPTER *ioc)
646 unsigned long flags;
648 if (ioc->fault_reset_work_q)
649 return;
651 /* initialize fault polling */
653 INIT_DELAYED_WORK(&ioc->fault_reset_work, _base_fault_reset_work);
654 snprintf(ioc->fault_reset_work_q_name,
655 sizeof(ioc->fault_reset_work_q_name), "poll_%s%d_status",
656 ioc->driver_name, ioc->id);
657 ioc->fault_reset_work_q =
658 create_singlethread_workqueue(ioc->fault_reset_work_q_name);
659 if (!ioc->fault_reset_work_q) {
660 pr_err(MPT3SAS_FMT "%s: failed (line=%d)\n",
661 ioc->name, __func__, __LINE__);
662 return;
664 spin_lock_irqsave(&ioc->ioc_reset_in_progress_lock, flags);
665 if (ioc->fault_reset_work_q)
666 queue_delayed_work(ioc->fault_reset_work_q,
667 &ioc->fault_reset_work,
668 msecs_to_jiffies(FAULT_POLLING_INTERVAL));
669 spin_unlock_irqrestore(&ioc->ioc_reset_in_progress_lock, flags);
673 * mpt3sas_base_stop_watchdog - stop the fault_reset_work_q
674 * @ioc: per adapter object
676 * Context: sleep.
678 void
679 mpt3sas_base_stop_watchdog(struct MPT3SAS_ADAPTER *ioc)
681 unsigned long flags;
682 struct workqueue_struct *wq;
684 spin_lock_irqsave(&ioc->ioc_reset_in_progress_lock, flags);
685 wq = ioc->fault_reset_work_q;
686 ioc->fault_reset_work_q = NULL;
687 spin_unlock_irqrestore(&ioc->ioc_reset_in_progress_lock, flags);
688 if (wq) {
689 if (!cancel_delayed_work_sync(&ioc->fault_reset_work))
690 flush_workqueue(wq);
691 destroy_workqueue(wq);
696 * mpt3sas_base_fault_info - verbose translation of firmware FAULT code
697 * @ioc: per adapter object
698 * @fault_code: fault code
700 void
701 mpt3sas_base_fault_info(struct MPT3SAS_ADAPTER *ioc , u16 fault_code)
703 pr_err(MPT3SAS_FMT "fault_state(0x%04x)!\n",
704 ioc->name, fault_code);
708 * mpt3sas_halt_firmware - halt's mpt controller firmware
709 * @ioc: per adapter object
711 * For debugging timeout related issues. Writing 0xCOFFEE00
712 * to the doorbell register will halt controller firmware. With
713 * the purpose to stop both driver and firmware, the enduser can
714 * obtain a ring buffer from controller UART.
716 void
717 mpt3sas_halt_firmware(struct MPT3SAS_ADAPTER *ioc)
719 u32 doorbell;
721 if (!ioc->fwfault_debug)
722 return;
724 dump_stack();
726 doorbell = readl(&ioc->chip->Doorbell);
727 if ((doorbell & MPI2_IOC_STATE_MASK) == MPI2_IOC_STATE_FAULT)
728 mpt3sas_base_fault_info(ioc , doorbell);
729 else {
730 writel(0xC0FFEE00, &ioc->chip->Doorbell);
731 pr_err(MPT3SAS_FMT "Firmware is halted due to command timeout\n",
732 ioc->name);
735 if (ioc->fwfault_debug == 2)
736 for (;;)
738 else
739 panic("panic in %s\n", __func__);
743 * _base_sas_ioc_info - verbose translation of the ioc status
744 * @ioc: per adapter object
745 * @mpi_reply: reply mf payload returned from firmware
746 * @request_hdr: request mf
748 static void
749 _base_sas_ioc_info(struct MPT3SAS_ADAPTER *ioc, MPI2DefaultReply_t *mpi_reply,
750 MPI2RequestHeader_t *request_hdr)
752 u16 ioc_status = le16_to_cpu(mpi_reply->IOCStatus) &
753 MPI2_IOCSTATUS_MASK;
754 char *desc = NULL;
755 u16 frame_sz;
756 char *func_str = NULL;
758 /* SCSI_IO, RAID_PASS are handled from _scsih_scsi_ioc_info */
759 if (request_hdr->Function == MPI2_FUNCTION_SCSI_IO_REQUEST ||
760 request_hdr->Function == MPI2_FUNCTION_RAID_SCSI_IO_PASSTHROUGH ||
761 request_hdr->Function == MPI2_FUNCTION_EVENT_NOTIFICATION)
762 return;
764 if (ioc_status == MPI2_IOCSTATUS_CONFIG_INVALID_PAGE)
765 return;
767 switch (ioc_status) {
769 /****************************************************************************
770 * Common IOCStatus values for all replies
771 ****************************************************************************/
773 case MPI2_IOCSTATUS_INVALID_FUNCTION:
774 desc = "invalid function";
775 break;
776 case MPI2_IOCSTATUS_BUSY:
777 desc = "busy";
778 break;
779 case MPI2_IOCSTATUS_INVALID_SGL:
780 desc = "invalid sgl";
781 break;
782 case MPI2_IOCSTATUS_INTERNAL_ERROR:
783 desc = "internal error";
784 break;
785 case MPI2_IOCSTATUS_INVALID_VPID:
786 desc = "invalid vpid";
787 break;
788 case MPI2_IOCSTATUS_INSUFFICIENT_RESOURCES:
789 desc = "insufficient resources";
790 break;
791 case MPI2_IOCSTATUS_INSUFFICIENT_POWER:
792 desc = "insufficient power";
793 break;
794 case MPI2_IOCSTATUS_INVALID_FIELD:
795 desc = "invalid field";
796 break;
797 case MPI2_IOCSTATUS_INVALID_STATE:
798 desc = "invalid state";
799 break;
800 case MPI2_IOCSTATUS_OP_STATE_NOT_SUPPORTED:
801 desc = "op state not supported";
802 break;
804 /****************************************************************************
805 * Config IOCStatus values
806 ****************************************************************************/
808 case MPI2_IOCSTATUS_CONFIG_INVALID_ACTION:
809 desc = "config invalid action";
810 break;
811 case MPI2_IOCSTATUS_CONFIG_INVALID_TYPE:
812 desc = "config invalid type";
813 break;
814 case MPI2_IOCSTATUS_CONFIG_INVALID_PAGE:
815 desc = "config invalid page";
816 break;
817 case MPI2_IOCSTATUS_CONFIG_INVALID_DATA:
818 desc = "config invalid data";
819 break;
820 case MPI2_IOCSTATUS_CONFIG_NO_DEFAULTS:
821 desc = "config no defaults";
822 break;
823 case MPI2_IOCSTATUS_CONFIG_CANT_COMMIT:
824 desc = "config cant commit";
825 break;
827 /****************************************************************************
828 * SCSI IO Reply
829 ****************************************************************************/
831 case MPI2_IOCSTATUS_SCSI_RECOVERED_ERROR:
832 case MPI2_IOCSTATUS_SCSI_INVALID_DEVHANDLE:
833 case MPI2_IOCSTATUS_SCSI_DEVICE_NOT_THERE:
834 case MPI2_IOCSTATUS_SCSI_DATA_OVERRUN:
835 case MPI2_IOCSTATUS_SCSI_DATA_UNDERRUN:
836 case MPI2_IOCSTATUS_SCSI_IO_DATA_ERROR:
837 case MPI2_IOCSTATUS_SCSI_PROTOCOL_ERROR:
838 case MPI2_IOCSTATUS_SCSI_TASK_TERMINATED:
839 case MPI2_IOCSTATUS_SCSI_RESIDUAL_MISMATCH:
840 case MPI2_IOCSTATUS_SCSI_TASK_MGMT_FAILED:
841 case MPI2_IOCSTATUS_SCSI_IOC_TERMINATED:
842 case MPI2_IOCSTATUS_SCSI_EXT_TERMINATED:
843 break;
845 /****************************************************************************
846 * For use by SCSI Initiator and SCSI Target end-to-end data protection
847 ****************************************************************************/
849 case MPI2_IOCSTATUS_EEDP_GUARD_ERROR:
850 desc = "eedp guard error";
851 break;
852 case MPI2_IOCSTATUS_EEDP_REF_TAG_ERROR:
853 desc = "eedp ref tag error";
854 break;
855 case MPI2_IOCSTATUS_EEDP_APP_TAG_ERROR:
856 desc = "eedp app tag error";
857 break;
859 /****************************************************************************
860 * SCSI Target values
861 ****************************************************************************/
863 case MPI2_IOCSTATUS_TARGET_INVALID_IO_INDEX:
864 desc = "target invalid io index";
865 break;
866 case MPI2_IOCSTATUS_TARGET_ABORTED:
867 desc = "target aborted";
868 break;
869 case MPI2_IOCSTATUS_TARGET_NO_CONN_RETRYABLE:
870 desc = "target no conn retryable";
871 break;
872 case MPI2_IOCSTATUS_TARGET_NO_CONNECTION:
873 desc = "target no connection";
874 break;
875 case MPI2_IOCSTATUS_TARGET_XFER_COUNT_MISMATCH:
876 desc = "target xfer count mismatch";
877 break;
878 case MPI2_IOCSTATUS_TARGET_DATA_OFFSET_ERROR:
879 desc = "target data offset error";
880 break;
881 case MPI2_IOCSTATUS_TARGET_TOO_MUCH_WRITE_DATA:
882 desc = "target too much write data";
883 break;
884 case MPI2_IOCSTATUS_TARGET_IU_TOO_SHORT:
885 desc = "target iu too short";
886 break;
887 case MPI2_IOCSTATUS_TARGET_ACK_NAK_TIMEOUT:
888 desc = "target ack nak timeout";
889 break;
890 case MPI2_IOCSTATUS_TARGET_NAK_RECEIVED:
891 desc = "target nak received";
892 break;
894 /****************************************************************************
895 * Serial Attached SCSI values
896 ****************************************************************************/
898 case MPI2_IOCSTATUS_SAS_SMP_REQUEST_FAILED:
899 desc = "smp request failed";
900 break;
901 case MPI2_IOCSTATUS_SAS_SMP_DATA_OVERRUN:
902 desc = "smp data overrun";
903 break;
905 /****************************************************************************
906 * Diagnostic Buffer Post / Diagnostic Release values
907 ****************************************************************************/
909 case MPI2_IOCSTATUS_DIAGNOSTIC_RELEASED:
910 desc = "diagnostic released";
911 break;
912 default:
913 break;
916 if (!desc)
917 return;
919 switch (request_hdr->Function) {
920 case MPI2_FUNCTION_CONFIG:
921 frame_sz = sizeof(Mpi2ConfigRequest_t) + ioc->sge_size;
922 func_str = "config_page";
923 break;
924 case MPI2_FUNCTION_SCSI_TASK_MGMT:
925 frame_sz = sizeof(Mpi2SCSITaskManagementRequest_t);
926 func_str = "task_mgmt";
927 break;
928 case MPI2_FUNCTION_SAS_IO_UNIT_CONTROL:
929 frame_sz = sizeof(Mpi2SasIoUnitControlRequest_t);
930 func_str = "sas_iounit_ctl";
931 break;
932 case MPI2_FUNCTION_SCSI_ENCLOSURE_PROCESSOR:
933 frame_sz = sizeof(Mpi2SepRequest_t);
934 func_str = "enclosure";
935 break;
936 case MPI2_FUNCTION_IOC_INIT:
937 frame_sz = sizeof(Mpi2IOCInitRequest_t);
938 func_str = "ioc_init";
939 break;
940 case MPI2_FUNCTION_PORT_ENABLE:
941 frame_sz = sizeof(Mpi2PortEnableRequest_t);
942 func_str = "port_enable";
943 break;
944 case MPI2_FUNCTION_SMP_PASSTHROUGH:
945 frame_sz = sizeof(Mpi2SmpPassthroughRequest_t) + ioc->sge_size;
946 func_str = "smp_passthru";
947 break;
948 case MPI2_FUNCTION_NVME_ENCAPSULATED:
949 frame_sz = sizeof(Mpi26NVMeEncapsulatedRequest_t) +
950 ioc->sge_size;
951 func_str = "nvme_encapsulated";
952 break;
953 default:
954 frame_sz = 32;
955 func_str = "unknown";
956 break;
959 pr_warn(MPT3SAS_FMT "ioc_status: %s(0x%04x), request(0x%p),(%s)\n",
960 ioc->name, desc, ioc_status, request_hdr, func_str);
962 _debug_dump_mf(request_hdr, frame_sz/4);
966 * _base_display_event_data - verbose translation of firmware asyn events
967 * @ioc: per adapter object
968 * @mpi_reply: reply mf payload returned from firmware
970 static void
971 _base_display_event_data(struct MPT3SAS_ADAPTER *ioc,
972 Mpi2EventNotificationReply_t *mpi_reply)
974 char *desc = NULL;
975 u16 event;
977 if (!(ioc->logging_level & MPT_DEBUG_EVENTS))
978 return;
980 event = le16_to_cpu(mpi_reply->Event);
982 switch (event) {
983 case MPI2_EVENT_LOG_DATA:
984 desc = "Log Data";
985 break;
986 case MPI2_EVENT_STATE_CHANGE:
987 desc = "Status Change";
988 break;
989 case MPI2_EVENT_HARD_RESET_RECEIVED:
990 desc = "Hard Reset Received";
991 break;
992 case MPI2_EVENT_EVENT_CHANGE:
993 desc = "Event Change";
994 break;
995 case MPI2_EVENT_SAS_DEVICE_STATUS_CHANGE:
996 desc = "Device Status Change";
997 break;
998 case MPI2_EVENT_IR_OPERATION_STATUS:
999 if (!ioc->hide_ir_msg)
1000 desc = "IR Operation Status";
1001 break;
1002 case MPI2_EVENT_SAS_DISCOVERY:
1004 Mpi2EventDataSasDiscovery_t *event_data =
1005 (Mpi2EventDataSasDiscovery_t *)mpi_reply->EventData;
1006 pr_info(MPT3SAS_FMT "Discovery: (%s)", ioc->name,
1007 (event_data->ReasonCode == MPI2_EVENT_SAS_DISC_RC_STARTED) ?
1008 "start" : "stop");
1009 if (event_data->DiscoveryStatus)
1010 pr_cont(" discovery_status(0x%08x)",
1011 le32_to_cpu(event_data->DiscoveryStatus));
1012 pr_cont("\n");
1013 return;
1015 case MPI2_EVENT_SAS_BROADCAST_PRIMITIVE:
1016 desc = "SAS Broadcast Primitive";
1017 break;
1018 case MPI2_EVENT_SAS_INIT_DEVICE_STATUS_CHANGE:
1019 desc = "SAS Init Device Status Change";
1020 break;
1021 case MPI2_EVENT_SAS_INIT_TABLE_OVERFLOW:
1022 desc = "SAS Init Table Overflow";
1023 break;
1024 case MPI2_EVENT_SAS_TOPOLOGY_CHANGE_LIST:
1025 desc = "SAS Topology Change List";
1026 break;
1027 case MPI2_EVENT_SAS_ENCL_DEVICE_STATUS_CHANGE:
1028 desc = "SAS Enclosure Device Status Change";
1029 break;
1030 case MPI2_EVENT_IR_VOLUME:
1031 if (!ioc->hide_ir_msg)
1032 desc = "IR Volume";
1033 break;
1034 case MPI2_EVENT_IR_PHYSICAL_DISK:
1035 if (!ioc->hide_ir_msg)
1036 desc = "IR Physical Disk";
1037 break;
1038 case MPI2_EVENT_IR_CONFIGURATION_CHANGE_LIST:
1039 if (!ioc->hide_ir_msg)
1040 desc = "IR Configuration Change List";
1041 break;
1042 case MPI2_EVENT_LOG_ENTRY_ADDED:
1043 if (!ioc->hide_ir_msg)
1044 desc = "Log Entry Added";
1045 break;
1046 case MPI2_EVENT_TEMP_THRESHOLD:
1047 desc = "Temperature Threshold";
1048 break;
1049 case MPI2_EVENT_ACTIVE_CABLE_EXCEPTION:
1050 desc = "Cable Event";
1051 break;
1052 case MPI2_EVENT_SAS_DEVICE_DISCOVERY_ERROR:
1053 desc = "SAS Device Discovery Error";
1054 break;
1055 case MPI2_EVENT_PCIE_DEVICE_STATUS_CHANGE:
1056 desc = "PCIE Device Status Change";
1057 break;
1058 case MPI2_EVENT_PCIE_ENUMERATION:
1060 Mpi26EventDataPCIeEnumeration_t *event_data =
1061 (Mpi26EventDataPCIeEnumeration_t *)mpi_reply->EventData;
1062 pr_info(MPT3SAS_FMT "PCIE Enumeration: (%s)", ioc->name,
1063 (event_data->ReasonCode ==
1064 MPI26_EVENT_PCIE_ENUM_RC_STARTED) ?
1065 "start" : "stop");
1066 if (event_data->EnumerationStatus)
1067 pr_info("enumeration_status(0x%08x)",
1068 le32_to_cpu(event_data->EnumerationStatus));
1069 pr_info("\n");
1070 return;
1072 case MPI2_EVENT_PCIE_TOPOLOGY_CHANGE_LIST:
1073 desc = "PCIE Topology Change List";
1074 break;
1077 if (!desc)
1078 return;
1080 pr_info(MPT3SAS_FMT "%s\n", ioc->name, desc);
1084 * _base_sas_log_info - verbose translation of firmware log info
1085 * @ioc: per adapter object
1086 * @log_info: log info
1088 static void
1089 _base_sas_log_info(struct MPT3SAS_ADAPTER *ioc , u32 log_info)
1091 union loginfo_type {
1092 u32 loginfo;
1093 struct {
1094 u32 subcode:16;
1095 u32 code:8;
1096 u32 originator:4;
1097 u32 bus_type:4;
1098 } dw;
1100 union loginfo_type sas_loginfo;
1101 char *originator_str = NULL;
1103 sas_loginfo.loginfo = log_info;
1104 if (sas_loginfo.dw.bus_type != 3 /*SAS*/)
1105 return;
1107 /* each nexus loss loginfo */
1108 if (log_info == 0x31170000)
1109 return;
1111 /* eat the loginfos associated with task aborts */
1112 if (ioc->ignore_loginfos && (log_info == 0x30050000 || log_info ==
1113 0x31140000 || log_info == 0x31130000))
1114 return;
1116 switch (sas_loginfo.dw.originator) {
1117 case 0:
1118 originator_str = "IOP";
1119 break;
1120 case 1:
1121 originator_str = "PL";
1122 break;
1123 case 2:
1124 if (!ioc->hide_ir_msg)
1125 originator_str = "IR";
1126 else
1127 originator_str = "WarpDrive";
1128 break;
1131 pr_warn(MPT3SAS_FMT
1132 "log_info(0x%08x): originator(%s), code(0x%02x), sub_code(0x%04x)\n",
1133 ioc->name, log_info,
1134 originator_str, sas_loginfo.dw.code,
1135 sas_loginfo.dw.subcode);
1139 * _base_display_reply_info -
1140 * @ioc: per adapter object
1141 * @smid: system request message index
1142 * @msix_index: MSIX table index supplied by the OS
1143 * @reply: reply message frame(lower 32bit addr)
1145 static void
1146 _base_display_reply_info(struct MPT3SAS_ADAPTER *ioc, u16 smid, u8 msix_index,
1147 u32 reply)
1149 MPI2DefaultReply_t *mpi_reply;
1150 u16 ioc_status;
1151 u32 loginfo = 0;
1153 mpi_reply = mpt3sas_base_get_reply_virt_addr(ioc, reply);
1154 if (unlikely(!mpi_reply)) {
1155 pr_err(MPT3SAS_FMT "mpi_reply not valid at %s:%d/%s()!\n",
1156 ioc->name, __FILE__, __LINE__, __func__);
1157 return;
1159 ioc_status = le16_to_cpu(mpi_reply->IOCStatus);
1161 if ((ioc_status & MPI2_IOCSTATUS_MASK) &&
1162 (ioc->logging_level & MPT_DEBUG_REPLY)) {
1163 _base_sas_ioc_info(ioc , mpi_reply,
1164 mpt3sas_base_get_msg_frame(ioc, smid));
1167 if (ioc_status & MPI2_IOCSTATUS_FLAG_LOG_INFO_AVAILABLE) {
1168 loginfo = le32_to_cpu(mpi_reply->IOCLogInfo);
1169 _base_sas_log_info(ioc, loginfo);
1172 if (ioc_status || loginfo) {
1173 ioc_status &= MPI2_IOCSTATUS_MASK;
1174 mpt3sas_trigger_mpi(ioc, ioc_status, loginfo);
1179 * mpt3sas_base_done - base internal command completion routine
1180 * @ioc: per adapter object
1181 * @smid: system request message index
1182 * @msix_index: MSIX table index supplied by the OS
1183 * @reply: reply message frame(lower 32bit addr)
1185 * Return:
1186 * 1 meaning mf should be freed from _base_interrupt
1187 * 0 means the mf is freed from this function.
1190 mpt3sas_base_done(struct MPT3SAS_ADAPTER *ioc, u16 smid, u8 msix_index,
1191 u32 reply)
1193 MPI2DefaultReply_t *mpi_reply;
1195 mpi_reply = mpt3sas_base_get_reply_virt_addr(ioc, reply);
1196 if (mpi_reply && mpi_reply->Function == MPI2_FUNCTION_EVENT_ACK)
1197 return mpt3sas_check_for_pending_internal_cmds(ioc, smid);
1199 if (ioc->base_cmds.status == MPT3_CMD_NOT_USED)
1200 return 1;
1202 ioc->base_cmds.status |= MPT3_CMD_COMPLETE;
1203 if (mpi_reply) {
1204 ioc->base_cmds.status |= MPT3_CMD_REPLY_VALID;
1205 memcpy(ioc->base_cmds.reply, mpi_reply, mpi_reply->MsgLength*4);
1207 ioc->base_cmds.status &= ~MPT3_CMD_PENDING;
1209 complete(&ioc->base_cmds.done);
1210 return 1;
1214 * _base_async_event - main callback handler for firmware asyn events
1215 * @ioc: per adapter object
1216 * @msix_index: MSIX table index supplied by the OS
1217 * @reply: reply message frame(lower 32bit addr)
1219 * Return:
1220 * 1 meaning mf should be freed from _base_interrupt
1221 * 0 means the mf is freed from this function.
1223 static u8
1224 _base_async_event(struct MPT3SAS_ADAPTER *ioc, u8 msix_index, u32 reply)
1226 Mpi2EventNotificationReply_t *mpi_reply;
1227 Mpi2EventAckRequest_t *ack_request;
1228 u16 smid;
1229 struct _event_ack_list *delayed_event_ack;
1231 mpi_reply = mpt3sas_base_get_reply_virt_addr(ioc, reply);
1232 if (!mpi_reply)
1233 return 1;
1234 if (mpi_reply->Function != MPI2_FUNCTION_EVENT_NOTIFICATION)
1235 return 1;
1237 _base_display_event_data(ioc, mpi_reply);
1239 if (!(mpi_reply->AckRequired & MPI2_EVENT_NOTIFICATION_ACK_REQUIRED))
1240 goto out;
1241 smid = mpt3sas_base_get_smid(ioc, ioc->base_cb_idx);
1242 if (!smid) {
1243 delayed_event_ack = kzalloc(sizeof(*delayed_event_ack),
1244 GFP_ATOMIC);
1245 if (!delayed_event_ack)
1246 goto out;
1247 INIT_LIST_HEAD(&delayed_event_ack->list);
1248 delayed_event_ack->Event = mpi_reply->Event;
1249 delayed_event_ack->EventContext = mpi_reply->EventContext;
1250 list_add_tail(&delayed_event_ack->list,
1251 &ioc->delayed_event_ack_list);
1252 dewtprintk(ioc, pr_info(MPT3SAS_FMT
1253 "DELAYED: EVENT ACK: event (0x%04x)\n",
1254 ioc->name, le16_to_cpu(mpi_reply->Event)));
1255 goto out;
1258 ack_request = mpt3sas_base_get_msg_frame(ioc, smid);
1259 memset(ack_request, 0, sizeof(Mpi2EventAckRequest_t));
1260 ack_request->Function = MPI2_FUNCTION_EVENT_ACK;
1261 ack_request->Event = mpi_reply->Event;
1262 ack_request->EventContext = mpi_reply->EventContext;
1263 ack_request->VF_ID = 0; /* TODO */
1264 ack_request->VP_ID = 0;
1265 mpt3sas_base_put_smid_default(ioc, smid);
1267 out:
1269 /* scsih callback handler */
1270 mpt3sas_scsih_event_callback(ioc, msix_index, reply);
1272 /* ctl callback handler */
1273 mpt3sas_ctl_event_callback(ioc, msix_index, reply);
1275 return 1;
1278 static struct scsiio_tracker *
1279 _get_st_from_smid(struct MPT3SAS_ADAPTER *ioc, u16 smid)
1281 struct scsi_cmnd *cmd;
1283 if (WARN_ON(!smid) ||
1284 WARN_ON(smid >= ioc->hi_priority_smid))
1285 return NULL;
1287 cmd = mpt3sas_scsih_scsi_lookup_get(ioc, smid);
1288 if (cmd)
1289 return scsi_cmd_priv(cmd);
1291 return NULL;
1295 * _base_get_cb_idx - obtain the callback index
1296 * @ioc: per adapter object
1297 * @smid: system request message index
1299 * Return: callback index.
1301 static u8
1302 _base_get_cb_idx(struct MPT3SAS_ADAPTER *ioc, u16 smid)
1304 int i;
1305 u16 ctl_smid = ioc->scsiio_depth - INTERNAL_SCSIIO_CMDS_COUNT + 1;
1306 u8 cb_idx = 0xFF;
1308 if (smid < ioc->hi_priority_smid) {
1309 struct scsiio_tracker *st;
1311 if (smid < ctl_smid) {
1312 st = _get_st_from_smid(ioc, smid);
1313 if (st)
1314 cb_idx = st->cb_idx;
1315 } else if (smid == ctl_smid)
1316 cb_idx = ioc->ctl_cb_idx;
1317 } else if (smid < ioc->internal_smid) {
1318 i = smid - ioc->hi_priority_smid;
1319 cb_idx = ioc->hpr_lookup[i].cb_idx;
1320 } else if (smid <= ioc->hba_queue_depth) {
1321 i = smid - ioc->internal_smid;
1322 cb_idx = ioc->internal_lookup[i].cb_idx;
1324 return cb_idx;
1328 * _base_mask_interrupts - disable interrupts
1329 * @ioc: per adapter object
1331 * Disabling ResetIRQ, Reply and Doorbell Interrupts
1333 static void
1334 _base_mask_interrupts(struct MPT3SAS_ADAPTER *ioc)
1336 u32 him_register;
1338 ioc->mask_interrupts = 1;
1339 him_register = readl(&ioc->chip->HostInterruptMask);
1340 him_register |= MPI2_HIM_DIM + MPI2_HIM_RIM + MPI2_HIM_RESET_IRQ_MASK;
1341 writel(him_register, &ioc->chip->HostInterruptMask);
1342 readl(&ioc->chip->HostInterruptMask);
1346 * _base_unmask_interrupts - enable interrupts
1347 * @ioc: per adapter object
1349 * Enabling only Reply Interrupts
1351 static void
1352 _base_unmask_interrupts(struct MPT3SAS_ADAPTER *ioc)
1354 u32 him_register;
1356 him_register = readl(&ioc->chip->HostInterruptMask);
1357 him_register &= ~MPI2_HIM_RIM;
1358 writel(him_register, &ioc->chip->HostInterruptMask);
1359 ioc->mask_interrupts = 0;
1362 union reply_descriptor {
1363 u64 word;
1364 struct {
1365 u32 low;
1366 u32 high;
1367 } u;
1371 * _base_interrupt - MPT adapter (IOC) specific interrupt handler.
1372 * @irq: irq number (not used)
1373 * @bus_id: bus identifier cookie == pointer to MPT_ADAPTER structure
1375 * Return: IRQ_HANDLED if processed, else IRQ_NONE.
1377 static irqreturn_t
1378 _base_interrupt(int irq, void *bus_id)
1380 struct adapter_reply_queue *reply_q = bus_id;
1381 union reply_descriptor rd;
1382 u32 completed_cmds;
1383 u8 request_desript_type;
1384 u16 smid;
1385 u8 cb_idx;
1386 u32 reply;
1387 u8 msix_index = reply_q->msix_index;
1388 struct MPT3SAS_ADAPTER *ioc = reply_q->ioc;
1389 Mpi2ReplyDescriptorsUnion_t *rpf;
1390 u8 rc;
1392 if (ioc->mask_interrupts)
1393 return IRQ_NONE;
1395 if (!atomic_add_unless(&reply_q->busy, 1, 1))
1396 return IRQ_NONE;
1398 rpf = &reply_q->reply_post_free[reply_q->reply_post_host_index];
1399 request_desript_type = rpf->Default.ReplyFlags
1400 & MPI2_RPY_DESCRIPT_FLAGS_TYPE_MASK;
1401 if (request_desript_type == MPI2_RPY_DESCRIPT_FLAGS_UNUSED) {
1402 atomic_dec(&reply_q->busy);
1403 return IRQ_NONE;
1406 completed_cmds = 0;
1407 cb_idx = 0xFF;
1408 do {
1409 rd.word = le64_to_cpu(rpf->Words);
1410 if (rd.u.low == UINT_MAX || rd.u.high == UINT_MAX)
1411 goto out;
1412 reply = 0;
1413 smid = le16_to_cpu(rpf->Default.DescriptorTypeDependent1);
1414 if (request_desript_type ==
1415 MPI25_RPY_DESCRIPT_FLAGS_FAST_PATH_SCSI_IO_SUCCESS ||
1416 request_desript_type ==
1417 MPI2_RPY_DESCRIPT_FLAGS_SCSI_IO_SUCCESS ||
1418 request_desript_type ==
1419 MPI26_RPY_DESCRIPT_FLAGS_PCIE_ENCAPSULATED_SUCCESS) {
1420 cb_idx = _base_get_cb_idx(ioc, smid);
1421 if ((likely(cb_idx < MPT_MAX_CALLBACKS)) &&
1422 (likely(mpt_callbacks[cb_idx] != NULL))) {
1423 rc = mpt_callbacks[cb_idx](ioc, smid,
1424 msix_index, 0);
1425 if (rc)
1426 mpt3sas_base_free_smid(ioc, smid);
1428 } else if (request_desript_type ==
1429 MPI2_RPY_DESCRIPT_FLAGS_ADDRESS_REPLY) {
1430 reply = le32_to_cpu(
1431 rpf->AddressReply.ReplyFrameAddress);
1432 if (reply > ioc->reply_dma_max_address ||
1433 reply < ioc->reply_dma_min_address)
1434 reply = 0;
1435 if (smid) {
1436 cb_idx = _base_get_cb_idx(ioc, smid);
1437 if ((likely(cb_idx < MPT_MAX_CALLBACKS)) &&
1438 (likely(mpt_callbacks[cb_idx] != NULL))) {
1439 rc = mpt_callbacks[cb_idx](ioc, smid,
1440 msix_index, reply);
1441 if (reply)
1442 _base_display_reply_info(ioc,
1443 smid, msix_index, reply);
1444 if (rc)
1445 mpt3sas_base_free_smid(ioc,
1446 smid);
1448 } else {
1449 _base_async_event(ioc, msix_index, reply);
1452 /* reply free queue handling */
1453 if (reply) {
1454 ioc->reply_free_host_index =
1455 (ioc->reply_free_host_index ==
1456 (ioc->reply_free_queue_depth - 1)) ?
1457 0 : ioc->reply_free_host_index + 1;
1458 ioc->reply_free[ioc->reply_free_host_index] =
1459 cpu_to_le32(reply);
1460 if (ioc->is_mcpu_endpoint)
1461 _base_clone_reply_to_sys_mem(ioc,
1462 reply,
1463 ioc->reply_free_host_index);
1464 writel(ioc->reply_free_host_index,
1465 &ioc->chip->ReplyFreeHostIndex);
1469 rpf->Words = cpu_to_le64(ULLONG_MAX);
1470 reply_q->reply_post_host_index =
1471 (reply_q->reply_post_host_index ==
1472 (ioc->reply_post_queue_depth - 1)) ? 0 :
1473 reply_q->reply_post_host_index + 1;
1474 request_desript_type =
1475 reply_q->reply_post_free[reply_q->reply_post_host_index].
1476 Default.ReplyFlags & MPI2_RPY_DESCRIPT_FLAGS_TYPE_MASK;
1477 completed_cmds++;
1478 /* Update the reply post host index after continuously
1479 * processing the threshold number of Reply Descriptors.
1480 * So that FW can find enough entries to post the Reply
1481 * Descriptors in the reply descriptor post queue.
1483 if (completed_cmds > ioc->hba_queue_depth/3) {
1484 if (ioc->combined_reply_queue) {
1485 writel(reply_q->reply_post_host_index |
1486 ((msix_index & 7) <<
1487 MPI2_RPHI_MSIX_INDEX_SHIFT),
1488 ioc->replyPostRegisterIndex[msix_index/8]);
1489 } else {
1490 writel(reply_q->reply_post_host_index |
1491 (msix_index <<
1492 MPI2_RPHI_MSIX_INDEX_SHIFT),
1493 &ioc->chip->ReplyPostHostIndex);
1495 completed_cmds = 1;
1497 if (request_desript_type == MPI2_RPY_DESCRIPT_FLAGS_UNUSED)
1498 goto out;
1499 if (!reply_q->reply_post_host_index)
1500 rpf = reply_q->reply_post_free;
1501 else
1502 rpf++;
1503 } while (1);
1505 out:
1507 if (!completed_cmds) {
1508 atomic_dec(&reply_q->busy);
1509 return IRQ_NONE;
1512 if (ioc->is_warpdrive) {
1513 writel(reply_q->reply_post_host_index,
1514 ioc->reply_post_host_index[msix_index]);
1515 atomic_dec(&reply_q->busy);
1516 return IRQ_HANDLED;
1519 /* Update Reply Post Host Index.
1520 * For those HBA's which support combined reply queue feature
1521 * 1. Get the correct Supplemental Reply Post Host Index Register.
1522 * i.e. (msix_index / 8)th entry from Supplemental Reply Post Host
1523 * Index Register address bank i.e replyPostRegisterIndex[],
1524 * 2. Then update this register with new reply host index value
1525 * in ReplyPostIndex field and the MSIxIndex field with
1526 * msix_index value reduced to a value between 0 and 7,
1527 * using a modulo 8 operation. Since each Supplemental Reply Post
1528 * Host Index Register supports 8 MSI-X vectors.
1530 * For other HBA's just update the Reply Post Host Index register with
1531 * new reply host index value in ReplyPostIndex Field and msix_index
1532 * value in MSIxIndex field.
1534 if (ioc->combined_reply_queue)
1535 writel(reply_q->reply_post_host_index | ((msix_index & 7) <<
1536 MPI2_RPHI_MSIX_INDEX_SHIFT),
1537 ioc->replyPostRegisterIndex[msix_index/8]);
1538 else
1539 writel(reply_q->reply_post_host_index | (msix_index <<
1540 MPI2_RPHI_MSIX_INDEX_SHIFT),
1541 &ioc->chip->ReplyPostHostIndex);
1542 atomic_dec(&reply_q->busy);
1543 return IRQ_HANDLED;
1547 * _base_is_controller_msix_enabled - is controller support muli-reply queues
1548 * @ioc: per adapter object
1550 * Return: Whether or not MSI/X is enabled.
1552 static inline int
1553 _base_is_controller_msix_enabled(struct MPT3SAS_ADAPTER *ioc)
1555 return (ioc->facts.IOCCapabilities &
1556 MPI2_IOCFACTS_CAPABILITY_MSI_X_INDEX) && ioc->msix_enable;
1560 * mpt3sas_base_sync_reply_irqs - flush pending MSIX interrupts
1561 * @ioc: per adapter object
1562 * Context: non ISR conext
1564 * Called when a Task Management request has completed.
1566 void
1567 mpt3sas_base_sync_reply_irqs(struct MPT3SAS_ADAPTER *ioc)
1569 struct adapter_reply_queue *reply_q;
1571 /* If MSIX capability is turned off
1572 * then multi-queues are not enabled
1574 if (!_base_is_controller_msix_enabled(ioc))
1575 return;
1577 list_for_each_entry(reply_q, &ioc->reply_queue_list, list) {
1578 if (ioc->shost_recovery || ioc->remove_host ||
1579 ioc->pci_error_recovery)
1580 return;
1581 /* TMs are on msix_index == 0 */
1582 if (reply_q->msix_index == 0)
1583 continue;
1584 synchronize_irq(pci_irq_vector(ioc->pdev, reply_q->msix_index));
1589 * mpt3sas_base_release_callback_handler - clear interrupt callback handler
1590 * @cb_idx: callback index
1592 void
1593 mpt3sas_base_release_callback_handler(u8 cb_idx)
1595 mpt_callbacks[cb_idx] = NULL;
1599 * mpt3sas_base_register_callback_handler - obtain index for the interrupt callback handler
1600 * @cb_func: callback function
1602 * Return: Index of @cb_func.
1605 mpt3sas_base_register_callback_handler(MPT_CALLBACK cb_func)
1607 u8 cb_idx;
1609 for (cb_idx = MPT_MAX_CALLBACKS-1; cb_idx; cb_idx--)
1610 if (mpt_callbacks[cb_idx] == NULL)
1611 break;
1613 mpt_callbacks[cb_idx] = cb_func;
1614 return cb_idx;
1618 * mpt3sas_base_initialize_callback_handler - initialize the interrupt callback handler
1620 void
1621 mpt3sas_base_initialize_callback_handler(void)
1623 u8 cb_idx;
1625 for (cb_idx = 0; cb_idx < MPT_MAX_CALLBACKS; cb_idx++)
1626 mpt3sas_base_release_callback_handler(cb_idx);
1631 * _base_build_zero_len_sge - build zero length sg entry
1632 * @ioc: per adapter object
1633 * @paddr: virtual address for SGE
1635 * Create a zero length scatter gather entry to insure the IOCs hardware has
1636 * something to use if the target device goes brain dead and tries
1637 * to send data even when none is asked for.
1639 static void
1640 _base_build_zero_len_sge(struct MPT3SAS_ADAPTER *ioc, void *paddr)
1642 u32 flags_length = (u32)((MPI2_SGE_FLAGS_LAST_ELEMENT |
1643 MPI2_SGE_FLAGS_END_OF_BUFFER | MPI2_SGE_FLAGS_END_OF_LIST |
1644 MPI2_SGE_FLAGS_SIMPLE_ELEMENT) <<
1645 MPI2_SGE_FLAGS_SHIFT);
1646 ioc->base_add_sg_single(paddr, flags_length, -1);
1650 * _base_add_sg_single_32 - Place a simple 32 bit SGE at address pAddr.
1651 * @paddr: virtual address for SGE
1652 * @flags_length: SGE flags and data transfer length
1653 * @dma_addr: Physical address
1655 static void
1656 _base_add_sg_single_32(void *paddr, u32 flags_length, dma_addr_t dma_addr)
1658 Mpi2SGESimple32_t *sgel = paddr;
1660 flags_length |= (MPI2_SGE_FLAGS_32_BIT_ADDRESSING |
1661 MPI2_SGE_FLAGS_SYSTEM_ADDRESS) << MPI2_SGE_FLAGS_SHIFT;
1662 sgel->FlagsLength = cpu_to_le32(flags_length);
1663 sgel->Address = cpu_to_le32(dma_addr);
1668 * _base_add_sg_single_64 - Place a simple 64 bit SGE at address pAddr.
1669 * @paddr: virtual address for SGE
1670 * @flags_length: SGE flags and data transfer length
1671 * @dma_addr: Physical address
1673 static void
1674 _base_add_sg_single_64(void *paddr, u32 flags_length, dma_addr_t dma_addr)
1676 Mpi2SGESimple64_t *sgel = paddr;
1678 flags_length |= (MPI2_SGE_FLAGS_64_BIT_ADDRESSING |
1679 MPI2_SGE_FLAGS_SYSTEM_ADDRESS) << MPI2_SGE_FLAGS_SHIFT;
1680 sgel->FlagsLength = cpu_to_le32(flags_length);
1681 sgel->Address = cpu_to_le64(dma_addr);
1685 * _base_get_chain_buffer_tracker - obtain chain tracker
1686 * @ioc: per adapter object
1687 * @scmd: SCSI commands of the IO request
1689 * Return: chain tracker from chain_lookup table using key as
1690 * smid and smid's chain_offset.
1692 static struct chain_tracker *
1693 _base_get_chain_buffer_tracker(struct MPT3SAS_ADAPTER *ioc,
1694 struct scsi_cmnd *scmd)
1696 struct chain_tracker *chain_req;
1697 struct scsiio_tracker *st = scsi_cmd_priv(scmd);
1698 u16 smid = st->smid;
1699 u8 chain_offset =
1700 atomic_read(&ioc->chain_lookup[smid - 1].chain_offset);
1702 if (chain_offset == ioc->chains_needed_per_io)
1703 return NULL;
1705 chain_req = &ioc->chain_lookup[smid - 1].chains_per_smid[chain_offset];
1706 atomic_inc(&ioc->chain_lookup[smid - 1].chain_offset);
1707 return chain_req;
1712 * _base_build_sg - build generic sg
1713 * @ioc: per adapter object
1714 * @psge: virtual address for SGE
1715 * @data_out_dma: physical address for WRITES
1716 * @data_out_sz: data xfer size for WRITES
1717 * @data_in_dma: physical address for READS
1718 * @data_in_sz: data xfer size for READS
1720 static void
1721 _base_build_sg(struct MPT3SAS_ADAPTER *ioc, void *psge,
1722 dma_addr_t data_out_dma, size_t data_out_sz, dma_addr_t data_in_dma,
1723 size_t data_in_sz)
1725 u32 sgl_flags;
1727 if (!data_out_sz && !data_in_sz) {
1728 _base_build_zero_len_sge(ioc, psge);
1729 return;
1732 if (data_out_sz && data_in_sz) {
1733 /* WRITE sgel first */
1734 sgl_flags = (MPI2_SGE_FLAGS_SIMPLE_ELEMENT |
1735 MPI2_SGE_FLAGS_END_OF_BUFFER | MPI2_SGE_FLAGS_HOST_TO_IOC);
1736 sgl_flags = sgl_flags << MPI2_SGE_FLAGS_SHIFT;
1737 ioc->base_add_sg_single(psge, sgl_flags |
1738 data_out_sz, data_out_dma);
1740 /* incr sgel */
1741 psge += ioc->sge_size;
1743 /* READ sgel last */
1744 sgl_flags = (MPI2_SGE_FLAGS_SIMPLE_ELEMENT |
1745 MPI2_SGE_FLAGS_LAST_ELEMENT | MPI2_SGE_FLAGS_END_OF_BUFFER |
1746 MPI2_SGE_FLAGS_END_OF_LIST);
1747 sgl_flags = sgl_flags << MPI2_SGE_FLAGS_SHIFT;
1748 ioc->base_add_sg_single(psge, sgl_flags |
1749 data_in_sz, data_in_dma);
1750 } else if (data_out_sz) /* WRITE */ {
1751 sgl_flags = (MPI2_SGE_FLAGS_SIMPLE_ELEMENT |
1752 MPI2_SGE_FLAGS_LAST_ELEMENT | MPI2_SGE_FLAGS_END_OF_BUFFER |
1753 MPI2_SGE_FLAGS_END_OF_LIST | MPI2_SGE_FLAGS_HOST_TO_IOC);
1754 sgl_flags = sgl_flags << MPI2_SGE_FLAGS_SHIFT;
1755 ioc->base_add_sg_single(psge, sgl_flags |
1756 data_out_sz, data_out_dma);
1757 } else if (data_in_sz) /* READ */ {
1758 sgl_flags = (MPI2_SGE_FLAGS_SIMPLE_ELEMENT |
1759 MPI2_SGE_FLAGS_LAST_ELEMENT | MPI2_SGE_FLAGS_END_OF_BUFFER |
1760 MPI2_SGE_FLAGS_END_OF_LIST);
1761 sgl_flags = sgl_flags << MPI2_SGE_FLAGS_SHIFT;
1762 ioc->base_add_sg_single(psge, sgl_flags |
1763 data_in_sz, data_in_dma);
1767 /* IEEE format sgls */
1770 * _base_build_nvme_prp - This function is called for NVMe end devices to build
1771 * a native SGL (NVMe PRP). The native SGL is built starting in the first PRP
1772 * entry of the NVMe message (PRP1). If the data buffer is small enough to be
1773 * described entirely using PRP1, then PRP2 is not used. If needed, PRP2 is
1774 * used to describe a larger data buffer. If the data buffer is too large to
1775 * describe using the two PRP entriess inside the NVMe message, then PRP1
1776 * describes the first data memory segment, and PRP2 contains a pointer to a PRP
1777 * list located elsewhere in memory to describe the remaining data memory
1778 * segments. The PRP list will be contiguous.
1780 * The native SGL for NVMe devices is a Physical Region Page (PRP). A PRP
1781 * consists of a list of PRP entries to describe a number of noncontigous
1782 * physical memory segments as a single memory buffer, just as a SGL does. Note
1783 * however, that this function is only used by the IOCTL call, so the memory
1784 * given will be guaranteed to be contiguous. There is no need to translate
1785 * non-contiguous SGL into a PRP in this case. All PRPs will describe
1786 * contiguous space that is one page size each.
1788 * Each NVMe message contains two PRP entries. The first (PRP1) either contains
1789 * a PRP list pointer or a PRP element, depending upon the command. PRP2
1790 * contains the second PRP element if the memory being described fits within 2
1791 * PRP entries, or a PRP list pointer if the PRP spans more than two entries.
1793 * A PRP list pointer contains the address of a PRP list, structured as a linear
1794 * array of PRP entries. Each PRP entry in this list describes a segment of
1795 * physical memory.
1797 * Each 64-bit PRP entry comprises an address and an offset field. The address
1798 * always points at the beginning of a 4KB physical memory page, and the offset
1799 * describes where within that 4KB page the memory segment begins. Only the
1800 * first element in a PRP list may contain a non-zero offest, implying that all
1801 * memory segments following the first begin at the start of a 4KB page.
1803 * Each PRP element normally describes 4KB of physical memory, with exceptions
1804 * for the first and last elements in the list. If the memory being described
1805 * by the list begins at a non-zero offset within the first 4KB page, then the
1806 * first PRP element will contain a non-zero offset indicating where the region
1807 * begins within the 4KB page. The last memory segment may end before the end
1808 * of the 4KB segment, depending upon the overall size of the memory being
1809 * described by the PRP list.
1811 * Since PRP entries lack any indication of size, the overall data buffer length
1812 * is used to determine where the end of the data memory buffer is located, and
1813 * how many PRP entries are required to describe it.
1815 * @ioc: per adapter object
1816 * @smid: system request message index for getting asscociated SGL
1817 * @nvme_encap_request: the NVMe request msg frame pointer
1818 * @data_out_dma: physical address for WRITES
1819 * @data_out_sz: data xfer size for WRITES
1820 * @data_in_dma: physical address for READS
1821 * @data_in_sz: data xfer size for READS
1823 static void
1824 _base_build_nvme_prp(struct MPT3SAS_ADAPTER *ioc, u16 smid,
1825 Mpi26NVMeEncapsulatedRequest_t *nvme_encap_request,
1826 dma_addr_t data_out_dma, size_t data_out_sz, dma_addr_t data_in_dma,
1827 size_t data_in_sz)
1829 int prp_size = NVME_PRP_SIZE;
1830 __le64 *prp_entry, *prp1_entry, *prp2_entry;
1831 __le64 *prp_page;
1832 dma_addr_t prp_entry_dma, prp_page_dma, dma_addr;
1833 u32 offset, entry_len;
1834 u32 page_mask_result, page_mask;
1835 size_t length;
1836 struct mpt3sas_nvme_cmd *nvme_cmd =
1837 (void *)nvme_encap_request->NVMe_Command;
1840 * Not all commands require a data transfer. If no data, just return
1841 * without constructing any PRP.
1843 if (!data_in_sz && !data_out_sz)
1844 return;
1845 prp1_entry = &nvme_cmd->prp1;
1846 prp2_entry = &nvme_cmd->prp2;
1847 prp_entry = prp1_entry;
1849 * For the PRP entries, use the specially allocated buffer of
1850 * contiguous memory.
1852 prp_page = (__le64 *)mpt3sas_base_get_pcie_sgl(ioc, smid);
1853 prp_page_dma = mpt3sas_base_get_pcie_sgl_dma(ioc, smid);
1856 * Check if we are within 1 entry of a page boundary we don't
1857 * want our first entry to be a PRP List entry.
1859 page_mask = ioc->page_size - 1;
1860 page_mask_result = (uintptr_t)((u8 *)prp_page + prp_size) & page_mask;
1861 if (!page_mask_result) {
1862 /* Bump up to next page boundary. */
1863 prp_page = (__le64 *)((u8 *)prp_page + prp_size);
1864 prp_page_dma = prp_page_dma + prp_size;
1868 * Set PRP physical pointer, which initially points to the current PRP
1869 * DMA memory page.
1871 prp_entry_dma = prp_page_dma;
1873 /* Get physical address and length of the data buffer. */
1874 if (data_in_sz) {
1875 dma_addr = data_in_dma;
1876 length = data_in_sz;
1877 } else {
1878 dma_addr = data_out_dma;
1879 length = data_out_sz;
1882 /* Loop while the length is not zero. */
1883 while (length) {
1885 * Check if we need to put a list pointer here if we are at
1886 * page boundary - prp_size (8 bytes).
1888 page_mask_result = (prp_entry_dma + prp_size) & page_mask;
1889 if (!page_mask_result) {
1891 * This is the last entry in a PRP List, so we need to
1892 * put a PRP list pointer here. What this does is:
1893 * - bump the current memory pointer to the next
1894 * address, which will be the next full page.
1895 * - set the PRP Entry to point to that page. This
1896 * is now the PRP List pointer.
1897 * - bump the PRP Entry pointer the start of the
1898 * next page. Since all of this PRP memory is
1899 * contiguous, no need to get a new page - it's
1900 * just the next address.
1902 prp_entry_dma++;
1903 *prp_entry = cpu_to_le64(prp_entry_dma);
1904 prp_entry++;
1907 /* Need to handle if entry will be part of a page. */
1908 offset = dma_addr & page_mask;
1909 entry_len = ioc->page_size - offset;
1911 if (prp_entry == prp1_entry) {
1913 * Must fill in the first PRP pointer (PRP1) before
1914 * moving on.
1916 *prp1_entry = cpu_to_le64(dma_addr);
1919 * Now point to the second PRP entry within the
1920 * command (PRP2).
1922 prp_entry = prp2_entry;
1923 } else if (prp_entry == prp2_entry) {
1925 * Should the PRP2 entry be a PRP List pointer or just
1926 * a regular PRP pointer? If there is more than one
1927 * more page of data, must use a PRP List pointer.
1929 if (length > ioc->page_size) {
1931 * PRP2 will contain a PRP List pointer because
1932 * more PRP's are needed with this command. The
1933 * list will start at the beginning of the
1934 * contiguous buffer.
1936 *prp2_entry = cpu_to_le64(prp_entry_dma);
1939 * The next PRP Entry will be the start of the
1940 * first PRP List.
1942 prp_entry = prp_page;
1943 } else {
1945 * After this, the PRP Entries are complete.
1946 * This command uses 2 PRP's and no PRP list.
1948 *prp2_entry = cpu_to_le64(dma_addr);
1950 } else {
1952 * Put entry in list and bump the addresses.
1954 * After PRP1 and PRP2 are filled in, this will fill in
1955 * all remaining PRP entries in a PRP List, one per
1956 * each time through the loop.
1958 *prp_entry = cpu_to_le64(dma_addr);
1959 prp_entry++;
1960 prp_entry_dma++;
1964 * Bump the phys address of the command's data buffer by the
1965 * entry_len.
1967 dma_addr += entry_len;
1969 /* Decrement length accounting for last partial page. */
1970 if (entry_len > length)
1971 length = 0;
1972 else
1973 length -= entry_len;
1978 * base_make_prp_nvme -
1979 * Prepare PRPs(Physical Region Page)- SGLs specific to NVMe drives only
1981 * @ioc: per adapter object
1982 * @scmd: SCSI command from the mid-layer
1983 * @mpi_request: mpi request
1984 * @smid: msg Index
1985 * @sge_count: scatter gather element count.
1987 * Return: true: PRPs are built
1988 * false: IEEE SGLs needs to be built
1990 static void
1991 base_make_prp_nvme(struct MPT3SAS_ADAPTER *ioc,
1992 struct scsi_cmnd *scmd,
1993 Mpi25SCSIIORequest_t *mpi_request,
1994 u16 smid, int sge_count)
1996 int sge_len, num_prp_in_chain = 0;
1997 Mpi25IeeeSgeChain64_t *main_chain_element, *ptr_first_sgl;
1998 __le64 *curr_buff;
1999 dma_addr_t msg_dma, sge_addr, offset;
2000 u32 page_mask, page_mask_result;
2001 struct scatterlist *sg_scmd;
2002 u32 first_prp_len;
2003 int data_len = scsi_bufflen(scmd);
2004 u32 nvme_pg_size;
2006 nvme_pg_size = max_t(u32, ioc->page_size, NVME_PRP_PAGE_SIZE);
2008 * Nvme has a very convoluted prp format. One prp is required
2009 * for each page or partial page. Driver need to split up OS sg_list
2010 * entries if it is longer than one page or cross a page
2011 * boundary. Driver also have to insert a PRP list pointer entry as
2012 * the last entry in each physical page of the PRP list.
2014 * NOTE: The first PRP "entry" is actually placed in the first
2015 * SGL entry in the main message as IEEE 64 format. The 2nd
2016 * entry in the main message is the chain element, and the rest
2017 * of the PRP entries are built in the contiguous pcie buffer.
2019 page_mask = nvme_pg_size - 1;
2022 * Native SGL is needed.
2023 * Put a chain element in main message frame that points to the first
2024 * chain buffer.
2026 * NOTE: The ChainOffset field must be 0 when using a chain pointer to
2027 * a native SGL.
2030 /* Set main message chain element pointer */
2031 main_chain_element = (pMpi25IeeeSgeChain64_t)&mpi_request->SGL;
2033 * For NVMe the chain element needs to be the 2nd SG entry in the main
2034 * message.
2036 main_chain_element = (Mpi25IeeeSgeChain64_t *)
2037 ((u8 *)main_chain_element + sizeof(MPI25_IEEE_SGE_CHAIN64));
2040 * For the PRP entries, use the specially allocated buffer of
2041 * contiguous memory. Normal chain buffers can't be used
2042 * because each chain buffer would need to be the size of an OS
2043 * page (4k).
2045 curr_buff = mpt3sas_base_get_pcie_sgl(ioc, smid);
2046 msg_dma = mpt3sas_base_get_pcie_sgl_dma(ioc, smid);
2048 main_chain_element->Address = cpu_to_le64(msg_dma);
2049 main_chain_element->NextChainOffset = 0;
2050 main_chain_element->Flags = MPI2_IEEE_SGE_FLAGS_CHAIN_ELEMENT |
2051 MPI2_IEEE_SGE_FLAGS_SYSTEM_ADDR |
2052 MPI26_IEEE_SGE_FLAGS_NSF_NVME_PRP;
2054 /* Build first prp, sge need not to be page aligned*/
2055 ptr_first_sgl = (pMpi25IeeeSgeChain64_t)&mpi_request->SGL;
2056 sg_scmd = scsi_sglist(scmd);
2057 sge_addr = sg_dma_address(sg_scmd);
2058 sge_len = sg_dma_len(sg_scmd);
2060 offset = sge_addr & page_mask;
2061 first_prp_len = nvme_pg_size - offset;
2063 ptr_first_sgl->Address = cpu_to_le64(sge_addr);
2064 ptr_first_sgl->Length = cpu_to_le32(first_prp_len);
2066 data_len -= first_prp_len;
2068 if (sge_len > first_prp_len) {
2069 sge_addr += first_prp_len;
2070 sge_len -= first_prp_len;
2071 } else if (data_len && (sge_len == first_prp_len)) {
2072 sg_scmd = sg_next(sg_scmd);
2073 sge_addr = sg_dma_address(sg_scmd);
2074 sge_len = sg_dma_len(sg_scmd);
2077 for (;;) {
2078 offset = sge_addr & page_mask;
2080 /* Put PRP pointer due to page boundary*/
2081 page_mask_result = (uintptr_t)(curr_buff + 1) & page_mask;
2082 if (unlikely(!page_mask_result)) {
2083 scmd_printk(KERN_NOTICE,
2084 scmd, "page boundary curr_buff: 0x%p\n",
2085 curr_buff);
2086 msg_dma += 8;
2087 *curr_buff = cpu_to_le64(msg_dma);
2088 curr_buff++;
2089 num_prp_in_chain++;
2092 *curr_buff = cpu_to_le64(sge_addr);
2093 curr_buff++;
2094 msg_dma += 8;
2095 num_prp_in_chain++;
2097 sge_addr += nvme_pg_size;
2098 sge_len -= nvme_pg_size;
2099 data_len -= nvme_pg_size;
2101 if (data_len <= 0)
2102 break;
2104 if (sge_len > 0)
2105 continue;
2107 sg_scmd = sg_next(sg_scmd);
2108 sge_addr = sg_dma_address(sg_scmd);
2109 sge_len = sg_dma_len(sg_scmd);
2112 main_chain_element->Length =
2113 cpu_to_le32(num_prp_in_chain * sizeof(u64));
2114 return;
2117 static bool
2118 base_is_prp_possible(struct MPT3SAS_ADAPTER *ioc,
2119 struct _pcie_device *pcie_device, struct scsi_cmnd *scmd, int sge_count)
2121 u32 data_length = 0;
2122 bool build_prp = true;
2124 data_length = scsi_bufflen(scmd);
2126 /* If Datalenth is <= 16K and number of SGE’s entries are <= 2
2127 * we built IEEE SGL
2129 if ((data_length <= NVME_PRP_PAGE_SIZE*4) && (sge_count <= 2))
2130 build_prp = false;
2132 return build_prp;
2136 * _base_check_pcie_native_sgl - This function is called for PCIe end devices to
2137 * determine if the driver needs to build a native SGL. If so, that native
2138 * SGL is built in the special contiguous buffers allocated especially for
2139 * PCIe SGL creation. If the driver will not build a native SGL, return
2140 * TRUE and a normal IEEE SGL will be built. Currently this routine
2141 * supports NVMe.
2142 * @ioc: per adapter object
2143 * @mpi_request: mf request pointer
2144 * @smid: system request message index
2145 * @scmd: scsi command
2146 * @pcie_device: points to the PCIe device's info
2148 * Return: 0 if native SGL was built, 1 if no SGL was built
2150 static int
2151 _base_check_pcie_native_sgl(struct MPT3SAS_ADAPTER *ioc,
2152 Mpi25SCSIIORequest_t *mpi_request, u16 smid, struct scsi_cmnd *scmd,
2153 struct _pcie_device *pcie_device)
2155 int sges_left;
2157 /* Get the SG list pointer and info. */
2158 sges_left = scsi_dma_map(scmd);
2159 if (sges_left < 0) {
2160 sdev_printk(KERN_ERR, scmd->device,
2161 "scsi_dma_map failed: request for %d bytes!\n",
2162 scsi_bufflen(scmd));
2163 return 1;
2166 /* Check if we need to build a native SG list. */
2167 if (base_is_prp_possible(ioc, pcie_device,
2168 scmd, sges_left) == 0) {
2169 /* We built a native SG list, just return. */
2170 goto out;
2174 * Build native NVMe PRP.
2176 base_make_prp_nvme(ioc, scmd, mpi_request,
2177 smid, sges_left);
2179 return 0;
2180 out:
2181 scsi_dma_unmap(scmd);
2182 return 1;
2186 * _base_add_sg_single_ieee - add sg element for IEEE format
2187 * @paddr: virtual address for SGE
2188 * @flags: SGE flags
2189 * @chain_offset: number of 128 byte elements from start of segment
2190 * @length: data transfer length
2191 * @dma_addr: Physical address
2193 static void
2194 _base_add_sg_single_ieee(void *paddr, u8 flags, u8 chain_offset, u32 length,
2195 dma_addr_t dma_addr)
2197 Mpi25IeeeSgeChain64_t *sgel = paddr;
2199 sgel->Flags = flags;
2200 sgel->NextChainOffset = chain_offset;
2201 sgel->Length = cpu_to_le32(length);
2202 sgel->Address = cpu_to_le64(dma_addr);
2206 * _base_build_zero_len_sge_ieee - build zero length sg entry for IEEE format
2207 * @ioc: per adapter object
2208 * @paddr: virtual address for SGE
2210 * Create a zero length scatter gather entry to insure the IOCs hardware has
2211 * something to use if the target device goes brain dead and tries
2212 * to send data even when none is asked for.
2214 static void
2215 _base_build_zero_len_sge_ieee(struct MPT3SAS_ADAPTER *ioc, void *paddr)
2217 u8 sgl_flags = (MPI2_IEEE_SGE_FLAGS_SIMPLE_ELEMENT |
2218 MPI2_IEEE_SGE_FLAGS_SYSTEM_ADDR |
2219 MPI25_IEEE_SGE_FLAGS_END_OF_LIST);
2221 _base_add_sg_single_ieee(paddr, sgl_flags, 0, 0, -1);
2225 * _base_build_sg_scmd - main sg creation routine
2226 * pcie_device is unused here!
2227 * @ioc: per adapter object
2228 * @scmd: scsi command
2229 * @smid: system request message index
2230 * @unused: unused pcie_device pointer
2231 * Context: none.
2233 * The main routine that builds scatter gather table from a given
2234 * scsi request sent via the .queuecommand main handler.
2236 * Return: 0 success, anything else error
2238 static int
2239 _base_build_sg_scmd(struct MPT3SAS_ADAPTER *ioc,
2240 struct scsi_cmnd *scmd, u16 smid, struct _pcie_device *unused)
2242 Mpi2SCSIIORequest_t *mpi_request;
2243 dma_addr_t chain_dma;
2244 struct scatterlist *sg_scmd;
2245 void *sg_local, *chain;
2246 u32 chain_offset;
2247 u32 chain_length;
2248 u32 chain_flags;
2249 int sges_left;
2250 u32 sges_in_segment;
2251 u32 sgl_flags;
2252 u32 sgl_flags_last_element;
2253 u32 sgl_flags_end_buffer;
2254 struct chain_tracker *chain_req;
2256 mpi_request = mpt3sas_base_get_msg_frame(ioc, smid);
2258 /* init scatter gather flags */
2259 sgl_flags = MPI2_SGE_FLAGS_SIMPLE_ELEMENT;
2260 if (scmd->sc_data_direction == DMA_TO_DEVICE)
2261 sgl_flags |= MPI2_SGE_FLAGS_HOST_TO_IOC;
2262 sgl_flags_last_element = (sgl_flags | MPI2_SGE_FLAGS_LAST_ELEMENT)
2263 << MPI2_SGE_FLAGS_SHIFT;
2264 sgl_flags_end_buffer = (sgl_flags | MPI2_SGE_FLAGS_LAST_ELEMENT |
2265 MPI2_SGE_FLAGS_END_OF_BUFFER | MPI2_SGE_FLAGS_END_OF_LIST)
2266 << MPI2_SGE_FLAGS_SHIFT;
2267 sgl_flags = sgl_flags << MPI2_SGE_FLAGS_SHIFT;
2269 sg_scmd = scsi_sglist(scmd);
2270 sges_left = scsi_dma_map(scmd);
2271 if (sges_left < 0) {
2272 sdev_printk(KERN_ERR, scmd->device,
2273 "pci_map_sg failed: request for %d bytes!\n",
2274 scsi_bufflen(scmd));
2275 return -ENOMEM;
2278 sg_local = &mpi_request->SGL;
2279 sges_in_segment = ioc->max_sges_in_main_message;
2280 if (sges_left <= sges_in_segment)
2281 goto fill_in_last_segment;
2283 mpi_request->ChainOffset = (offsetof(Mpi2SCSIIORequest_t, SGL) +
2284 (sges_in_segment * ioc->sge_size))/4;
2286 /* fill in main message segment when there is a chain following */
2287 while (sges_in_segment) {
2288 if (sges_in_segment == 1)
2289 ioc->base_add_sg_single(sg_local,
2290 sgl_flags_last_element | sg_dma_len(sg_scmd),
2291 sg_dma_address(sg_scmd));
2292 else
2293 ioc->base_add_sg_single(sg_local, sgl_flags |
2294 sg_dma_len(sg_scmd), sg_dma_address(sg_scmd));
2295 sg_scmd = sg_next(sg_scmd);
2296 sg_local += ioc->sge_size;
2297 sges_left--;
2298 sges_in_segment--;
2301 /* initializing the chain flags and pointers */
2302 chain_flags = MPI2_SGE_FLAGS_CHAIN_ELEMENT << MPI2_SGE_FLAGS_SHIFT;
2303 chain_req = _base_get_chain_buffer_tracker(ioc, scmd);
2304 if (!chain_req)
2305 return -1;
2306 chain = chain_req->chain_buffer;
2307 chain_dma = chain_req->chain_buffer_dma;
2308 do {
2309 sges_in_segment = (sges_left <=
2310 ioc->max_sges_in_chain_message) ? sges_left :
2311 ioc->max_sges_in_chain_message;
2312 chain_offset = (sges_left == sges_in_segment) ?
2313 0 : (sges_in_segment * ioc->sge_size)/4;
2314 chain_length = sges_in_segment * ioc->sge_size;
2315 if (chain_offset) {
2316 chain_offset = chain_offset <<
2317 MPI2_SGE_CHAIN_OFFSET_SHIFT;
2318 chain_length += ioc->sge_size;
2320 ioc->base_add_sg_single(sg_local, chain_flags | chain_offset |
2321 chain_length, chain_dma);
2322 sg_local = chain;
2323 if (!chain_offset)
2324 goto fill_in_last_segment;
2326 /* fill in chain segments */
2327 while (sges_in_segment) {
2328 if (sges_in_segment == 1)
2329 ioc->base_add_sg_single(sg_local,
2330 sgl_flags_last_element |
2331 sg_dma_len(sg_scmd),
2332 sg_dma_address(sg_scmd));
2333 else
2334 ioc->base_add_sg_single(sg_local, sgl_flags |
2335 sg_dma_len(sg_scmd),
2336 sg_dma_address(sg_scmd));
2337 sg_scmd = sg_next(sg_scmd);
2338 sg_local += ioc->sge_size;
2339 sges_left--;
2340 sges_in_segment--;
2343 chain_req = _base_get_chain_buffer_tracker(ioc, scmd);
2344 if (!chain_req)
2345 return -1;
2346 chain = chain_req->chain_buffer;
2347 chain_dma = chain_req->chain_buffer_dma;
2348 } while (1);
2351 fill_in_last_segment:
2353 /* fill the last segment */
2354 while (sges_left) {
2355 if (sges_left == 1)
2356 ioc->base_add_sg_single(sg_local, sgl_flags_end_buffer |
2357 sg_dma_len(sg_scmd), sg_dma_address(sg_scmd));
2358 else
2359 ioc->base_add_sg_single(sg_local, sgl_flags |
2360 sg_dma_len(sg_scmd), sg_dma_address(sg_scmd));
2361 sg_scmd = sg_next(sg_scmd);
2362 sg_local += ioc->sge_size;
2363 sges_left--;
2366 return 0;
2370 * _base_build_sg_scmd_ieee - main sg creation routine for IEEE format
2371 * @ioc: per adapter object
2372 * @scmd: scsi command
2373 * @smid: system request message index
2374 * @pcie_device: Pointer to pcie_device. If set, the pcie native sgl will be
2375 * constructed on need.
2376 * Context: none.
2378 * The main routine that builds scatter gather table from a given
2379 * scsi request sent via the .queuecommand main handler.
2381 * Return: 0 success, anything else error
2383 static int
2384 _base_build_sg_scmd_ieee(struct MPT3SAS_ADAPTER *ioc,
2385 struct scsi_cmnd *scmd, u16 smid, struct _pcie_device *pcie_device)
2387 Mpi25SCSIIORequest_t *mpi_request;
2388 dma_addr_t chain_dma;
2389 struct scatterlist *sg_scmd;
2390 void *sg_local, *chain;
2391 u32 chain_offset;
2392 u32 chain_length;
2393 int sges_left;
2394 u32 sges_in_segment;
2395 u8 simple_sgl_flags;
2396 u8 simple_sgl_flags_last;
2397 u8 chain_sgl_flags;
2398 struct chain_tracker *chain_req;
2400 mpi_request = mpt3sas_base_get_msg_frame(ioc, smid);
2402 /* init scatter gather flags */
2403 simple_sgl_flags = MPI2_IEEE_SGE_FLAGS_SIMPLE_ELEMENT |
2404 MPI2_IEEE_SGE_FLAGS_SYSTEM_ADDR;
2405 simple_sgl_flags_last = simple_sgl_flags |
2406 MPI25_IEEE_SGE_FLAGS_END_OF_LIST;
2407 chain_sgl_flags = MPI2_IEEE_SGE_FLAGS_CHAIN_ELEMENT |
2408 MPI2_IEEE_SGE_FLAGS_SYSTEM_ADDR;
2410 /* Check if we need to build a native SG list. */
2411 if ((pcie_device) && (_base_check_pcie_native_sgl(ioc, mpi_request,
2412 smid, scmd, pcie_device) == 0)) {
2413 /* We built a native SG list, just return. */
2414 return 0;
2417 sg_scmd = scsi_sglist(scmd);
2418 sges_left = scsi_dma_map(scmd);
2419 if (sges_left < 0) {
2420 sdev_printk(KERN_ERR, scmd->device,
2421 "pci_map_sg failed: request for %d bytes!\n",
2422 scsi_bufflen(scmd));
2423 return -ENOMEM;
2426 sg_local = &mpi_request->SGL;
2427 sges_in_segment = (ioc->request_sz -
2428 offsetof(Mpi25SCSIIORequest_t, SGL))/ioc->sge_size_ieee;
2429 if (sges_left <= sges_in_segment)
2430 goto fill_in_last_segment;
2432 mpi_request->ChainOffset = (sges_in_segment - 1 /* chain element */) +
2433 (offsetof(Mpi25SCSIIORequest_t, SGL)/ioc->sge_size_ieee);
2435 /* fill in main message segment when there is a chain following */
2436 while (sges_in_segment > 1) {
2437 _base_add_sg_single_ieee(sg_local, simple_sgl_flags, 0,
2438 sg_dma_len(sg_scmd), sg_dma_address(sg_scmd));
2439 sg_scmd = sg_next(sg_scmd);
2440 sg_local += ioc->sge_size_ieee;
2441 sges_left--;
2442 sges_in_segment--;
2445 /* initializing the pointers */
2446 chain_req = _base_get_chain_buffer_tracker(ioc, scmd);
2447 if (!chain_req)
2448 return -1;
2449 chain = chain_req->chain_buffer;
2450 chain_dma = chain_req->chain_buffer_dma;
2451 do {
2452 sges_in_segment = (sges_left <=
2453 ioc->max_sges_in_chain_message) ? sges_left :
2454 ioc->max_sges_in_chain_message;
2455 chain_offset = (sges_left == sges_in_segment) ?
2456 0 : sges_in_segment;
2457 chain_length = sges_in_segment * ioc->sge_size_ieee;
2458 if (chain_offset)
2459 chain_length += ioc->sge_size_ieee;
2460 _base_add_sg_single_ieee(sg_local, chain_sgl_flags,
2461 chain_offset, chain_length, chain_dma);
2463 sg_local = chain;
2464 if (!chain_offset)
2465 goto fill_in_last_segment;
2467 /* fill in chain segments */
2468 while (sges_in_segment) {
2469 _base_add_sg_single_ieee(sg_local, simple_sgl_flags, 0,
2470 sg_dma_len(sg_scmd), sg_dma_address(sg_scmd));
2471 sg_scmd = sg_next(sg_scmd);
2472 sg_local += ioc->sge_size_ieee;
2473 sges_left--;
2474 sges_in_segment--;
2477 chain_req = _base_get_chain_buffer_tracker(ioc, scmd);
2478 if (!chain_req)
2479 return -1;
2480 chain = chain_req->chain_buffer;
2481 chain_dma = chain_req->chain_buffer_dma;
2482 } while (1);
2485 fill_in_last_segment:
2487 /* fill the last segment */
2488 while (sges_left > 0) {
2489 if (sges_left == 1)
2490 _base_add_sg_single_ieee(sg_local,
2491 simple_sgl_flags_last, 0, sg_dma_len(sg_scmd),
2492 sg_dma_address(sg_scmd));
2493 else
2494 _base_add_sg_single_ieee(sg_local, simple_sgl_flags, 0,
2495 sg_dma_len(sg_scmd), sg_dma_address(sg_scmd));
2496 sg_scmd = sg_next(sg_scmd);
2497 sg_local += ioc->sge_size_ieee;
2498 sges_left--;
2501 return 0;
2505 * _base_build_sg_ieee - build generic sg for IEEE format
2506 * @ioc: per adapter object
2507 * @psge: virtual address for SGE
2508 * @data_out_dma: physical address for WRITES
2509 * @data_out_sz: data xfer size for WRITES
2510 * @data_in_dma: physical address for READS
2511 * @data_in_sz: data xfer size for READS
2513 static void
2514 _base_build_sg_ieee(struct MPT3SAS_ADAPTER *ioc, void *psge,
2515 dma_addr_t data_out_dma, size_t data_out_sz, dma_addr_t data_in_dma,
2516 size_t data_in_sz)
2518 u8 sgl_flags;
2520 if (!data_out_sz && !data_in_sz) {
2521 _base_build_zero_len_sge_ieee(ioc, psge);
2522 return;
2525 if (data_out_sz && data_in_sz) {
2526 /* WRITE sgel first */
2527 sgl_flags = MPI2_IEEE_SGE_FLAGS_SIMPLE_ELEMENT |
2528 MPI2_IEEE_SGE_FLAGS_SYSTEM_ADDR;
2529 _base_add_sg_single_ieee(psge, sgl_flags, 0, data_out_sz,
2530 data_out_dma);
2532 /* incr sgel */
2533 psge += ioc->sge_size_ieee;
2535 /* READ sgel last */
2536 sgl_flags |= MPI25_IEEE_SGE_FLAGS_END_OF_LIST;
2537 _base_add_sg_single_ieee(psge, sgl_flags, 0, data_in_sz,
2538 data_in_dma);
2539 } else if (data_out_sz) /* WRITE */ {
2540 sgl_flags = MPI2_IEEE_SGE_FLAGS_SIMPLE_ELEMENT |
2541 MPI25_IEEE_SGE_FLAGS_END_OF_LIST |
2542 MPI2_IEEE_SGE_FLAGS_SYSTEM_ADDR;
2543 _base_add_sg_single_ieee(psge, sgl_flags, 0, data_out_sz,
2544 data_out_dma);
2545 } else if (data_in_sz) /* READ */ {
2546 sgl_flags = MPI2_IEEE_SGE_FLAGS_SIMPLE_ELEMENT |
2547 MPI25_IEEE_SGE_FLAGS_END_OF_LIST |
2548 MPI2_IEEE_SGE_FLAGS_SYSTEM_ADDR;
2549 _base_add_sg_single_ieee(psge, sgl_flags, 0, data_in_sz,
2550 data_in_dma);
2554 #define convert_to_kb(x) ((x) << (PAGE_SHIFT - 10))
2557 * _base_config_dma_addressing - set dma addressing
2558 * @ioc: per adapter object
2559 * @pdev: PCI device struct
2561 * Return: 0 for success, non-zero for failure.
2563 static int
2564 _base_config_dma_addressing(struct MPT3SAS_ADAPTER *ioc, struct pci_dev *pdev)
2566 struct sysinfo s;
2567 u64 consistent_dma_mask;
2568 /* Set 63 bit DMA mask for all SAS3 and SAS35 controllers */
2569 int dma_mask = (ioc->hba_mpi_version_belonged > MPI2_VERSION) ? 63 : 64;
2571 if (ioc->is_mcpu_endpoint)
2572 goto try_32bit;
2574 if (ioc->dma_mask)
2575 consistent_dma_mask = DMA_BIT_MASK(dma_mask);
2576 else
2577 consistent_dma_mask = DMA_BIT_MASK(32);
2579 if (sizeof(dma_addr_t) > 4) {
2580 const uint64_t required_mask =
2581 dma_get_required_mask(&pdev->dev);
2582 if ((required_mask > DMA_BIT_MASK(32)) &&
2583 !pci_set_dma_mask(pdev, DMA_BIT_MASK(dma_mask)) &&
2584 !pci_set_consistent_dma_mask(pdev, consistent_dma_mask)) {
2585 ioc->base_add_sg_single = &_base_add_sg_single_64;
2586 ioc->sge_size = sizeof(Mpi2SGESimple64_t);
2587 ioc->dma_mask = dma_mask;
2588 goto out;
2592 try_32bit:
2593 if (!pci_set_dma_mask(pdev, DMA_BIT_MASK(32))
2594 && !pci_set_consistent_dma_mask(pdev, DMA_BIT_MASK(32))) {
2595 ioc->base_add_sg_single = &_base_add_sg_single_32;
2596 ioc->sge_size = sizeof(Mpi2SGESimple32_t);
2597 ioc->dma_mask = 32;
2598 } else
2599 return -ENODEV;
2601 out:
2602 si_meminfo(&s);
2603 pr_info(MPT3SAS_FMT
2604 "%d BIT PCI BUS DMA ADDRESSING SUPPORTED, total mem (%ld kB)\n",
2605 ioc->name, ioc->dma_mask, convert_to_kb(s.totalram));
2607 return 0;
2610 static int
2611 _base_change_consistent_dma_mask(struct MPT3SAS_ADAPTER *ioc,
2612 struct pci_dev *pdev)
2614 if (pci_set_consistent_dma_mask(pdev, DMA_BIT_MASK(ioc->dma_mask))) {
2615 if (pci_set_consistent_dma_mask(pdev, DMA_BIT_MASK(32)))
2616 return -ENODEV;
2618 return 0;
2622 * _base_check_enable_msix - checks MSIX capabable.
2623 * @ioc: per adapter object
2625 * Check to see if card is capable of MSIX, and set number
2626 * of available msix vectors
2628 static int
2629 _base_check_enable_msix(struct MPT3SAS_ADAPTER *ioc)
2631 int base;
2632 u16 message_control;
2634 /* Check whether controller SAS2008 B0 controller,
2635 * if it is SAS2008 B0 controller use IO-APIC instead of MSIX
2637 if (ioc->pdev->device == MPI2_MFGPAGE_DEVID_SAS2008 &&
2638 ioc->pdev->revision == SAS2_PCI_DEVICE_B0_REVISION) {
2639 return -EINVAL;
2642 base = pci_find_capability(ioc->pdev, PCI_CAP_ID_MSIX);
2643 if (!base) {
2644 dfailprintk(ioc, pr_info(MPT3SAS_FMT "msix not supported\n",
2645 ioc->name));
2646 return -EINVAL;
2649 /* get msix vector count */
2650 /* NUMA_IO not supported for older controllers */
2651 if (ioc->pdev->device == MPI2_MFGPAGE_DEVID_SAS2004 ||
2652 ioc->pdev->device == MPI2_MFGPAGE_DEVID_SAS2008 ||
2653 ioc->pdev->device == MPI2_MFGPAGE_DEVID_SAS2108_1 ||
2654 ioc->pdev->device == MPI2_MFGPAGE_DEVID_SAS2108_2 ||
2655 ioc->pdev->device == MPI2_MFGPAGE_DEVID_SAS2108_3 ||
2656 ioc->pdev->device == MPI2_MFGPAGE_DEVID_SAS2116_1 ||
2657 ioc->pdev->device == MPI2_MFGPAGE_DEVID_SAS2116_2)
2658 ioc->msix_vector_count = 1;
2659 else {
2660 pci_read_config_word(ioc->pdev, base + 2, &message_control);
2661 ioc->msix_vector_count = (message_control & 0x3FF) + 1;
2663 dinitprintk(ioc, pr_info(MPT3SAS_FMT
2664 "msix is supported, vector_count(%d)\n",
2665 ioc->name, ioc->msix_vector_count));
2666 return 0;
2670 * _base_free_irq - free irq
2671 * @ioc: per adapter object
2673 * Freeing respective reply_queue from the list.
2675 static void
2676 _base_free_irq(struct MPT3SAS_ADAPTER *ioc)
2678 struct adapter_reply_queue *reply_q, *next;
2680 if (list_empty(&ioc->reply_queue_list))
2681 return;
2683 list_for_each_entry_safe(reply_q, next, &ioc->reply_queue_list, list) {
2684 list_del(&reply_q->list);
2685 free_irq(pci_irq_vector(ioc->pdev, reply_q->msix_index),
2686 reply_q);
2687 kfree(reply_q);
2692 * _base_request_irq - request irq
2693 * @ioc: per adapter object
2694 * @index: msix index into vector table
2696 * Inserting respective reply_queue into the list.
2698 static int
2699 _base_request_irq(struct MPT3SAS_ADAPTER *ioc, u8 index)
2701 struct pci_dev *pdev = ioc->pdev;
2702 struct adapter_reply_queue *reply_q;
2703 int r;
2705 reply_q = kzalloc(sizeof(struct adapter_reply_queue), GFP_KERNEL);
2706 if (!reply_q) {
2707 pr_err(MPT3SAS_FMT "unable to allocate memory %d!\n",
2708 ioc->name, (int)sizeof(struct adapter_reply_queue));
2709 return -ENOMEM;
2711 reply_q->ioc = ioc;
2712 reply_q->msix_index = index;
2714 atomic_set(&reply_q->busy, 0);
2715 if (ioc->msix_enable)
2716 snprintf(reply_q->name, MPT_NAME_LENGTH, "%s%d-msix%d",
2717 ioc->driver_name, ioc->id, index);
2718 else
2719 snprintf(reply_q->name, MPT_NAME_LENGTH, "%s%d",
2720 ioc->driver_name, ioc->id);
2721 r = request_irq(pci_irq_vector(pdev, index), _base_interrupt,
2722 IRQF_SHARED, reply_q->name, reply_q);
2723 if (r) {
2724 pr_err(MPT3SAS_FMT "unable to allocate interrupt %d!\n",
2725 reply_q->name, pci_irq_vector(pdev, index));
2726 kfree(reply_q);
2727 return -EBUSY;
2730 INIT_LIST_HEAD(&reply_q->list);
2731 list_add_tail(&reply_q->list, &ioc->reply_queue_list);
2732 return 0;
2736 * _base_assign_reply_queues - assigning msix index for each cpu
2737 * @ioc: per adapter object
2739 * The enduser would need to set the affinity via /proc/irq/#/smp_affinity
2741 * It would nice if we could call irq_set_affinity, however it is not
2742 * an exported symbol
2744 static void
2745 _base_assign_reply_queues(struct MPT3SAS_ADAPTER *ioc)
2747 unsigned int cpu, nr_cpus, nr_msix, index = 0;
2748 struct adapter_reply_queue *reply_q;
2750 if (!_base_is_controller_msix_enabled(ioc))
2751 return;
2753 memset(ioc->cpu_msix_table, 0, ioc->cpu_msix_table_sz);
2755 nr_cpus = num_online_cpus();
2756 nr_msix = ioc->reply_queue_count = min(ioc->reply_queue_count,
2757 ioc->facts.MaxMSIxVectors);
2758 if (!nr_msix)
2759 return;
2761 if (smp_affinity_enable) {
2762 list_for_each_entry(reply_q, &ioc->reply_queue_list, list) {
2763 const cpumask_t *mask = pci_irq_get_affinity(ioc->pdev,
2764 reply_q->msix_index);
2765 if (!mask) {
2766 pr_warn(MPT3SAS_FMT "no affinity for msi %x\n",
2767 ioc->name, reply_q->msix_index);
2768 continue;
2771 for_each_cpu_and(cpu, mask, cpu_online_mask) {
2772 if (cpu >= ioc->cpu_msix_table_sz)
2773 break;
2774 ioc->cpu_msix_table[cpu] = reply_q->msix_index;
2777 return;
2779 cpu = cpumask_first(cpu_online_mask);
2781 list_for_each_entry(reply_q, &ioc->reply_queue_list, list) {
2783 unsigned int i, group = nr_cpus / nr_msix;
2785 if (cpu >= nr_cpus)
2786 break;
2788 if (index < nr_cpus % nr_msix)
2789 group++;
2791 for (i = 0 ; i < group ; i++) {
2792 ioc->cpu_msix_table[cpu] = reply_q->msix_index;
2793 cpu = cpumask_next(cpu, cpu_online_mask);
2795 index++;
2800 * _base_disable_msix - disables msix
2801 * @ioc: per adapter object
2804 static void
2805 _base_disable_msix(struct MPT3SAS_ADAPTER *ioc)
2807 if (!ioc->msix_enable)
2808 return;
2809 pci_disable_msix(ioc->pdev);
2810 ioc->msix_enable = 0;
2814 * _base_enable_msix - enables msix, failback to io_apic
2815 * @ioc: per adapter object
2818 static int
2819 _base_enable_msix(struct MPT3SAS_ADAPTER *ioc)
2821 int r;
2822 int i, local_max_msix_vectors;
2823 u8 try_msix = 0;
2824 unsigned int irq_flags = PCI_IRQ_MSIX;
2826 if (msix_disable == -1 || msix_disable == 0)
2827 try_msix = 1;
2829 if (!try_msix)
2830 goto try_ioapic;
2832 if (_base_check_enable_msix(ioc) != 0)
2833 goto try_ioapic;
2835 ioc->reply_queue_count = min_t(int, ioc->cpu_count,
2836 ioc->msix_vector_count);
2838 printk(MPT3SAS_FMT "MSI-X vectors supported: %d, no of cores"
2839 ": %d, max_msix_vectors: %d\n", ioc->name, ioc->msix_vector_count,
2840 ioc->cpu_count, max_msix_vectors);
2842 if (!ioc->rdpq_array_enable && max_msix_vectors == -1)
2843 local_max_msix_vectors = (reset_devices) ? 1 : 8;
2844 else
2845 local_max_msix_vectors = max_msix_vectors;
2847 if (local_max_msix_vectors > 0)
2848 ioc->reply_queue_count = min_t(int, local_max_msix_vectors,
2849 ioc->reply_queue_count);
2850 else if (local_max_msix_vectors == 0)
2851 goto try_ioapic;
2853 if (ioc->msix_vector_count < ioc->cpu_count)
2854 smp_affinity_enable = 0;
2856 if (smp_affinity_enable)
2857 irq_flags |= PCI_IRQ_AFFINITY;
2859 r = pci_alloc_irq_vectors(ioc->pdev, 1, ioc->reply_queue_count,
2860 irq_flags);
2861 if (r < 0) {
2862 dfailprintk(ioc, pr_info(MPT3SAS_FMT
2863 "pci_alloc_irq_vectors failed (r=%d) !!!\n",
2864 ioc->name, r));
2865 goto try_ioapic;
2868 ioc->msix_enable = 1;
2869 ioc->reply_queue_count = r;
2870 for (i = 0; i < ioc->reply_queue_count; i++) {
2871 r = _base_request_irq(ioc, i);
2872 if (r) {
2873 _base_free_irq(ioc);
2874 _base_disable_msix(ioc);
2875 goto try_ioapic;
2879 return 0;
2881 /* failback to io_apic interrupt routing */
2882 try_ioapic:
2884 ioc->reply_queue_count = 1;
2885 r = pci_alloc_irq_vectors(ioc->pdev, 1, 1, PCI_IRQ_LEGACY);
2886 if (r < 0) {
2887 dfailprintk(ioc, pr_info(MPT3SAS_FMT
2888 "pci_alloc_irq_vector(legacy) failed (r=%d) !!!\n",
2889 ioc->name, r));
2890 } else
2891 r = _base_request_irq(ioc, 0);
2893 return r;
2897 * mpt3sas_base_unmap_resources - free controller resources
2898 * @ioc: per adapter object
2900 static void
2901 mpt3sas_base_unmap_resources(struct MPT3SAS_ADAPTER *ioc)
2903 struct pci_dev *pdev = ioc->pdev;
2905 dexitprintk(ioc, printk(MPT3SAS_FMT "%s\n",
2906 ioc->name, __func__));
2908 _base_free_irq(ioc);
2909 _base_disable_msix(ioc);
2911 kfree(ioc->replyPostRegisterIndex);
2912 ioc->replyPostRegisterIndex = NULL;
2915 if (ioc->chip_phys) {
2916 iounmap(ioc->chip);
2917 ioc->chip_phys = 0;
2920 if (pci_is_enabled(pdev)) {
2921 pci_release_selected_regions(ioc->pdev, ioc->bars);
2922 pci_disable_pcie_error_reporting(pdev);
2923 pci_disable_device(pdev);
2928 * mpt3sas_base_map_resources - map in controller resources (io/irq/memap)
2929 * @ioc: per adapter object
2931 * Return: 0 for success, non-zero for failure.
2934 mpt3sas_base_map_resources(struct MPT3SAS_ADAPTER *ioc)
2936 struct pci_dev *pdev = ioc->pdev;
2937 u32 memap_sz;
2938 u32 pio_sz;
2939 int i, r = 0;
2940 u64 pio_chip = 0;
2941 phys_addr_t chip_phys = 0;
2942 struct adapter_reply_queue *reply_q;
2944 dinitprintk(ioc, pr_info(MPT3SAS_FMT "%s\n",
2945 ioc->name, __func__));
2947 ioc->bars = pci_select_bars(pdev, IORESOURCE_MEM);
2948 if (pci_enable_device_mem(pdev)) {
2949 pr_warn(MPT3SAS_FMT "pci_enable_device_mem: failed\n",
2950 ioc->name);
2951 ioc->bars = 0;
2952 return -ENODEV;
2956 if (pci_request_selected_regions(pdev, ioc->bars,
2957 ioc->driver_name)) {
2958 pr_warn(MPT3SAS_FMT "pci_request_selected_regions: failed\n",
2959 ioc->name);
2960 ioc->bars = 0;
2961 r = -ENODEV;
2962 goto out_fail;
2965 /* AER (Advanced Error Reporting) hooks */
2966 pci_enable_pcie_error_reporting(pdev);
2968 pci_set_master(pdev);
2971 if (_base_config_dma_addressing(ioc, pdev) != 0) {
2972 pr_warn(MPT3SAS_FMT "no suitable DMA mask for %s\n",
2973 ioc->name, pci_name(pdev));
2974 r = -ENODEV;
2975 goto out_fail;
2978 for (i = 0, memap_sz = 0, pio_sz = 0; (i < DEVICE_COUNT_RESOURCE) &&
2979 (!memap_sz || !pio_sz); i++) {
2980 if (pci_resource_flags(pdev, i) & IORESOURCE_IO) {
2981 if (pio_sz)
2982 continue;
2983 pio_chip = (u64)pci_resource_start(pdev, i);
2984 pio_sz = pci_resource_len(pdev, i);
2985 } else if (pci_resource_flags(pdev, i) & IORESOURCE_MEM) {
2986 if (memap_sz)
2987 continue;
2988 ioc->chip_phys = pci_resource_start(pdev, i);
2989 chip_phys = ioc->chip_phys;
2990 memap_sz = pci_resource_len(pdev, i);
2991 ioc->chip = ioremap(ioc->chip_phys, memap_sz);
2995 if (ioc->chip == NULL) {
2996 pr_err(MPT3SAS_FMT "unable to map adapter memory! "
2997 " or resource not found\n", ioc->name);
2998 r = -EINVAL;
2999 goto out_fail;
3002 _base_mask_interrupts(ioc);
3004 r = _base_get_ioc_facts(ioc);
3005 if (r)
3006 goto out_fail;
3008 if (!ioc->rdpq_array_enable_assigned) {
3009 ioc->rdpq_array_enable = ioc->rdpq_array_capable;
3010 ioc->rdpq_array_enable_assigned = 1;
3013 r = _base_enable_msix(ioc);
3014 if (r)
3015 goto out_fail;
3017 /* Use the Combined reply queue feature only for SAS3 C0 & higher
3018 * revision HBAs and also only when reply queue count is greater than 8
3020 if (ioc->combined_reply_queue) {
3021 /* Determine the Supplemental Reply Post Host Index Registers
3022 * Addresse. Supplemental Reply Post Host Index Registers
3023 * starts at offset MPI25_SUP_REPLY_POST_HOST_INDEX_OFFSET and
3024 * each register is at offset bytes of
3025 * MPT3_SUP_REPLY_POST_HOST_INDEX_REG_OFFSET from previous one.
3027 ioc->replyPostRegisterIndex = kcalloc(
3028 ioc->combined_reply_index_count,
3029 sizeof(resource_size_t *), GFP_KERNEL);
3030 if (!ioc->replyPostRegisterIndex) {
3031 dfailprintk(ioc, printk(MPT3SAS_FMT
3032 "allocation for reply Post Register Index failed!!!\n",
3033 ioc->name));
3034 r = -ENOMEM;
3035 goto out_fail;
3038 for (i = 0; i < ioc->combined_reply_index_count; i++) {
3039 ioc->replyPostRegisterIndex[i] = (resource_size_t *)
3040 ((u8 __force *)&ioc->chip->Doorbell +
3041 MPI25_SUP_REPLY_POST_HOST_INDEX_OFFSET +
3042 (i * MPT3_SUP_REPLY_POST_HOST_INDEX_REG_OFFSET));
3046 if (ioc->is_warpdrive) {
3047 ioc->reply_post_host_index[0] = (resource_size_t __iomem *)
3048 &ioc->chip->ReplyPostHostIndex;
3050 for (i = 1; i < ioc->cpu_msix_table_sz; i++)
3051 ioc->reply_post_host_index[i] =
3052 (resource_size_t __iomem *)
3053 ((u8 __iomem *)&ioc->chip->Doorbell + (0x4000 + ((i - 1)
3054 * 4)));
3057 list_for_each_entry(reply_q, &ioc->reply_queue_list, list)
3058 pr_info(MPT3SAS_FMT "%s: IRQ %d\n",
3059 reply_q->name, ((ioc->msix_enable) ? "PCI-MSI-X enabled" :
3060 "IO-APIC enabled"),
3061 pci_irq_vector(ioc->pdev, reply_q->msix_index));
3063 pr_info(MPT3SAS_FMT "iomem(%pap), mapped(0x%p), size(%d)\n",
3064 ioc->name, &chip_phys, ioc->chip, memap_sz);
3065 pr_info(MPT3SAS_FMT "ioport(0x%016llx), size(%d)\n",
3066 ioc->name, (unsigned long long)pio_chip, pio_sz);
3068 /* Save PCI configuration state for recovery from PCI AER/EEH errors */
3069 pci_save_state(pdev);
3070 return 0;
3072 out_fail:
3073 mpt3sas_base_unmap_resources(ioc);
3074 return r;
3078 * mpt3sas_base_get_msg_frame - obtain request mf pointer
3079 * @ioc: per adapter object
3080 * @smid: system request message index(smid zero is invalid)
3082 * Return: virt pointer to message frame.
3084 void *
3085 mpt3sas_base_get_msg_frame(struct MPT3SAS_ADAPTER *ioc, u16 smid)
3087 return (void *)(ioc->request + (smid * ioc->request_sz));
3091 * mpt3sas_base_get_sense_buffer - obtain a sense buffer virt addr
3092 * @ioc: per adapter object
3093 * @smid: system request message index
3095 * Return: virt pointer to sense buffer.
3097 void *
3098 mpt3sas_base_get_sense_buffer(struct MPT3SAS_ADAPTER *ioc, u16 smid)
3100 return (void *)(ioc->sense + ((smid - 1) * SCSI_SENSE_BUFFERSIZE));
3104 * mpt3sas_base_get_sense_buffer_dma - obtain a sense buffer dma addr
3105 * @ioc: per adapter object
3106 * @smid: system request message index
3108 * Return: phys pointer to the low 32bit address of the sense buffer.
3110 __le32
3111 mpt3sas_base_get_sense_buffer_dma(struct MPT3SAS_ADAPTER *ioc, u16 smid)
3113 return cpu_to_le32(ioc->sense_dma + ((smid - 1) *
3114 SCSI_SENSE_BUFFERSIZE));
3118 * mpt3sas_base_get_pcie_sgl - obtain a PCIe SGL virt addr
3119 * @ioc: per adapter object
3120 * @smid: system request message index
3122 * Return: virt pointer to a PCIe SGL.
3124 void *
3125 mpt3sas_base_get_pcie_sgl(struct MPT3SAS_ADAPTER *ioc, u16 smid)
3127 return (void *)(ioc->pcie_sg_lookup[smid - 1].pcie_sgl);
3131 * mpt3sas_base_get_pcie_sgl_dma - obtain a PCIe SGL dma addr
3132 * @ioc: per adapter object
3133 * @smid: system request message index
3135 * Return: phys pointer to the address of the PCIe buffer.
3137 dma_addr_t
3138 mpt3sas_base_get_pcie_sgl_dma(struct MPT3SAS_ADAPTER *ioc, u16 smid)
3140 return ioc->pcie_sg_lookup[smid - 1].pcie_sgl_dma;
3144 * mpt3sas_base_get_reply_virt_addr - obtain reply frames virt address
3145 * @ioc: per adapter object
3146 * @phys_addr: lower 32 physical addr of the reply
3148 * Converts 32bit lower physical addr into a virt address.
3150 void *
3151 mpt3sas_base_get_reply_virt_addr(struct MPT3SAS_ADAPTER *ioc, u32 phys_addr)
3153 if (!phys_addr)
3154 return NULL;
3155 return ioc->reply + (phys_addr - (u32)ioc->reply_dma);
3158 static inline u8
3159 _base_get_msix_index(struct MPT3SAS_ADAPTER *ioc)
3161 return ioc->cpu_msix_table[raw_smp_processor_id()];
3165 * mpt3sas_base_get_smid - obtain a free smid from internal queue
3166 * @ioc: per adapter object
3167 * @cb_idx: callback index
3169 * Return: smid (zero is invalid)
3172 mpt3sas_base_get_smid(struct MPT3SAS_ADAPTER *ioc, u8 cb_idx)
3174 unsigned long flags;
3175 struct request_tracker *request;
3176 u16 smid;
3178 spin_lock_irqsave(&ioc->scsi_lookup_lock, flags);
3179 if (list_empty(&ioc->internal_free_list)) {
3180 spin_unlock_irqrestore(&ioc->scsi_lookup_lock, flags);
3181 pr_err(MPT3SAS_FMT "%s: smid not available\n",
3182 ioc->name, __func__);
3183 return 0;
3186 request = list_entry(ioc->internal_free_list.next,
3187 struct request_tracker, tracker_list);
3188 request->cb_idx = cb_idx;
3189 smid = request->smid;
3190 list_del(&request->tracker_list);
3191 spin_unlock_irqrestore(&ioc->scsi_lookup_lock, flags);
3192 return smid;
3196 * mpt3sas_base_get_smid_scsiio - obtain a free smid from scsiio queue
3197 * @ioc: per adapter object
3198 * @cb_idx: callback index
3199 * @scmd: pointer to scsi command object
3201 * Return: smid (zero is invalid)
3204 mpt3sas_base_get_smid_scsiio(struct MPT3SAS_ADAPTER *ioc, u8 cb_idx,
3205 struct scsi_cmnd *scmd)
3207 struct scsiio_tracker *request = scsi_cmd_priv(scmd);
3208 unsigned int tag = scmd->request->tag;
3209 u16 smid;
3211 smid = tag + 1;
3212 request->cb_idx = cb_idx;
3213 request->msix_io = _base_get_msix_index(ioc);
3214 request->smid = smid;
3215 INIT_LIST_HEAD(&request->chain_list);
3216 return smid;
3220 * mpt3sas_base_get_smid_hpr - obtain a free smid from hi-priority queue
3221 * @ioc: per adapter object
3222 * @cb_idx: callback index
3224 * Return: smid (zero is invalid)
3227 mpt3sas_base_get_smid_hpr(struct MPT3SAS_ADAPTER *ioc, u8 cb_idx)
3229 unsigned long flags;
3230 struct request_tracker *request;
3231 u16 smid;
3233 spin_lock_irqsave(&ioc->scsi_lookup_lock, flags);
3234 if (list_empty(&ioc->hpr_free_list)) {
3235 spin_unlock_irqrestore(&ioc->scsi_lookup_lock, flags);
3236 return 0;
3239 request = list_entry(ioc->hpr_free_list.next,
3240 struct request_tracker, tracker_list);
3241 request->cb_idx = cb_idx;
3242 smid = request->smid;
3243 list_del(&request->tracker_list);
3244 spin_unlock_irqrestore(&ioc->scsi_lookup_lock, flags);
3245 return smid;
3248 static void
3249 _base_recovery_check(struct MPT3SAS_ADAPTER *ioc)
3252 * See _wait_for_commands_to_complete() call with regards to this code.
3254 if (ioc->shost_recovery && ioc->pending_io_count) {
3255 ioc->pending_io_count = scsi_host_busy(ioc->shost);
3256 if (ioc->pending_io_count == 0)
3257 wake_up(&ioc->reset_wq);
3261 void mpt3sas_base_clear_st(struct MPT3SAS_ADAPTER *ioc,
3262 struct scsiio_tracker *st)
3264 if (WARN_ON(st->smid == 0))
3265 return;
3266 st->cb_idx = 0xFF;
3267 st->direct_io = 0;
3268 atomic_set(&ioc->chain_lookup[st->smid - 1].chain_offset, 0);
3269 st->smid = 0;
3273 * mpt3sas_base_free_smid - put smid back on free_list
3274 * @ioc: per adapter object
3275 * @smid: system request message index
3277 void
3278 mpt3sas_base_free_smid(struct MPT3SAS_ADAPTER *ioc, u16 smid)
3280 unsigned long flags;
3281 int i;
3283 if (smid < ioc->hi_priority_smid) {
3284 struct scsiio_tracker *st;
3285 void *request;
3287 st = _get_st_from_smid(ioc, smid);
3288 if (!st) {
3289 _base_recovery_check(ioc);
3290 return;
3293 /* Clear MPI request frame */
3294 request = mpt3sas_base_get_msg_frame(ioc, smid);
3295 memset(request, 0, ioc->request_sz);
3297 mpt3sas_base_clear_st(ioc, st);
3298 _base_recovery_check(ioc);
3299 return;
3302 spin_lock_irqsave(&ioc->scsi_lookup_lock, flags);
3303 if (smid < ioc->internal_smid) {
3304 /* hi-priority */
3305 i = smid - ioc->hi_priority_smid;
3306 ioc->hpr_lookup[i].cb_idx = 0xFF;
3307 list_add(&ioc->hpr_lookup[i].tracker_list, &ioc->hpr_free_list);
3308 } else if (smid <= ioc->hba_queue_depth) {
3309 /* internal queue */
3310 i = smid - ioc->internal_smid;
3311 ioc->internal_lookup[i].cb_idx = 0xFF;
3312 list_add(&ioc->internal_lookup[i].tracker_list,
3313 &ioc->internal_free_list);
3315 spin_unlock_irqrestore(&ioc->scsi_lookup_lock, flags);
3319 * _base_mpi_ep_writeq - 32 bit write to MMIO
3320 * @b: data payload
3321 * @addr: address in MMIO space
3322 * @writeq_lock: spin lock
3324 * This special handling for MPI EP to take care of 32 bit
3325 * environment where its not quarenteed to send the entire word
3326 * in one transfer.
3328 static inline void
3329 _base_mpi_ep_writeq(__u64 b, volatile void __iomem *addr,
3330 spinlock_t *writeq_lock)
3332 unsigned long flags;
3334 spin_lock_irqsave(writeq_lock, flags);
3335 __raw_writel((u32)(b), addr);
3336 __raw_writel((u32)(b >> 32), (addr + 4));
3337 mmiowb();
3338 spin_unlock_irqrestore(writeq_lock, flags);
3342 * _base_writeq - 64 bit write to MMIO
3343 * @b: data payload
3344 * @addr: address in MMIO space
3345 * @writeq_lock: spin lock
3347 * Glue for handling an atomic 64 bit word to MMIO. This special handling takes
3348 * care of 32 bit environment where its not quarenteed to send the entire word
3349 * in one transfer.
3351 #if defined(writeq) && defined(CONFIG_64BIT)
3352 static inline void
3353 _base_writeq(__u64 b, volatile void __iomem *addr, spinlock_t *writeq_lock)
3355 wmb();
3356 __raw_writeq(b, addr);
3357 barrier();
3359 #else
3360 static inline void
3361 _base_writeq(__u64 b, volatile void __iomem *addr, spinlock_t *writeq_lock)
3363 _base_mpi_ep_writeq(b, addr, writeq_lock);
3365 #endif
3368 * _base_put_smid_mpi_ep_scsi_io - send SCSI_IO request to firmware
3369 * @ioc: per adapter object
3370 * @smid: system request message index
3371 * @handle: device handle
3373 static void
3374 _base_put_smid_mpi_ep_scsi_io(struct MPT3SAS_ADAPTER *ioc, u16 smid, u16 handle)
3376 Mpi2RequestDescriptorUnion_t descriptor;
3377 u64 *request = (u64 *)&descriptor;
3378 void *mpi_req_iomem;
3379 __le32 *mfp = (__le32 *)mpt3sas_base_get_msg_frame(ioc, smid);
3381 _clone_sg_entries(ioc, (void *) mfp, smid);
3382 mpi_req_iomem = (void __force *)ioc->chip +
3383 MPI_FRAME_START_OFFSET + (smid * ioc->request_sz);
3384 _base_clone_mpi_to_sys_mem(mpi_req_iomem, (void *)mfp,
3385 ioc->request_sz);
3386 descriptor.SCSIIO.RequestFlags = MPI2_REQ_DESCRIPT_FLAGS_SCSI_IO;
3387 descriptor.SCSIIO.MSIxIndex = _base_get_msix_index(ioc);
3388 descriptor.SCSIIO.SMID = cpu_to_le16(smid);
3389 descriptor.SCSIIO.DevHandle = cpu_to_le16(handle);
3390 descriptor.SCSIIO.LMID = 0;
3391 _base_mpi_ep_writeq(*request, &ioc->chip->RequestDescriptorPostLow,
3392 &ioc->scsi_lookup_lock);
3396 * _base_put_smid_scsi_io - send SCSI_IO request to firmware
3397 * @ioc: per adapter object
3398 * @smid: system request message index
3399 * @handle: device handle
3401 static void
3402 _base_put_smid_scsi_io(struct MPT3SAS_ADAPTER *ioc, u16 smid, u16 handle)
3404 Mpi2RequestDescriptorUnion_t descriptor;
3405 u64 *request = (u64 *)&descriptor;
3408 descriptor.SCSIIO.RequestFlags = MPI2_REQ_DESCRIPT_FLAGS_SCSI_IO;
3409 descriptor.SCSIIO.MSIxIndex = _base_get_msix_index(ioc);
3410 descriptor.SCSIIO.SMID = cpu_to_le16(smid);
3411 descriptor.SCSIIO.DevHandle = cpu_to_le16(handle);
3412 descriptor.SCSIIO.LMID = 0;
3413 _base_writeq(*request, &ioc->chip->RequestDescriptorPostLow,
3414 &ioc->scsi_lookup_lock);
3418 * mpt3sas_base_put_smid_fast_path - send fast path request to firmware
3419 * @ioc: per adapter object
3420 * @smid: system request message index
3421 * @handle: device handle
3423 void
3424 mpt3sas_base_put_smid_fast_path(struct MPT3SAS_ADAPTER *ioc, u16 smid,
3425 u16 handle)
3427 Mpi2RequestDescriptorUnion_t descriptor;
3428 u64 *request = (u64 *)&descriptor;
3430 descriptor.SCSIIO.RequestFlags =
3431 MPI25_REQ_DESCRIPT_FLAGS_FAST_PATH_SCSI_IO;
3432 descriptor.SCSIIO.MSIxIndex = _base_get_msix_index(ioc);
3433 descriptor.SCSIIO.SMID = cpu_to_le16(smid);
3434 descriptor.SCSIIO.DevHandle = cpu_to_le16(handle);
3435 descriptor.SCSIIO.LMID = 0;
3436 _base_writeq(*request, &ioc->chip->RequestDescriptorPostLow,
3437 &ioc->scsi_lookup_lock);
3441 * mpt3sas_base_put_smid_hi_priority - send Task Management request to firmware
3442 * @ioc: per adapter object
3443 * @smid: system request message index
3444 * @msix_task: msix_task will be same as msix of IO incase of task abort else 0.
3446 void
3447 mpt3sas_base_put_smid_hi_priority(struct MPT3SAS_ADAPTER *ioc, u16 smid,
3448 u16 msix_task)
3450 Mpi2RequestDescriptorUnion_t descriptor;
3451 void *mpi_req_iomem;
3452 u64 *request;
3454 if (ioc->is_mcpu_endpoint) {
3455 __le32 *mfp = (__le32 *)mpt3sas_base_get_msg_frame(ioc, smid);
3457 /* TBD 256 is offset within sys register. */
3458 mpi_req_iomem = (void __force *)ioc->chip
3459 + MPI_FRAME_START_OFFSET
3460 + (smid * ioc->request_sz);
3461 _base_clone_mpi_to_sys_mem(mpi_req_iomem, (void *)mfp,
3462 ioc->request_sz);
3465 request = (u64 *)&descriptor;
3467 descriptor.HighPriority.RequestFlags =
3468 MPI2_REQ_DESCRIPT_FLAGS_HIGH_PRIORITY;
3469 descriptor.HighPriority.MSIxIndex = msix_task;
3470 descriptor.HighPriority.SMID = cpu_to_le16(smid);
3471 descriptor.HighPriority.LMID = 0;
3472 descriptor.HighPriority.Reserved1 = 0;
3473 if (ioc->is_mcpu_endpoint)
3474 _base_mpi_ep_writeq(*request,
3475 &ioc->chip->RequestDescriptorPostLow,
3476 &ioc->scsi_lookup_lock);
3477 else
3478 _base_writeq(*request, &ioc->chip->RequestDescriptorPostLow,
3479 &ioc->scsi_lookup_lock);
3483 * mpt3sas_base_put_smid_nvme_encap - send NVMe encapsulated request to
3484 * firmware
3485 * @ioc: per adapter object
3486 * @smid: system request message index
3488 void
3489 mpt3sas_base_put_smid_nvme_encap(struct MPT3SAS_ADAPTER *ioc, u16 smid)
3491 Mpi2RequestDescriptorUnion_t descriptor;
3492 u64 *request = (u64 *)&descriptor;
3494 descriptor.Default.RequestFlags =
3495 MPI26_REQ_DESCRIPT_FLAGS_PCIE_ENCAPSULATED;
3496 descriptor.Default.MSIxIndex = _base_get_msix_index(ioc);
3497 descriptor.Default.SMID = cpu_to_le16(smid);
3498 descriptor.Default.LMID = 0;
3499 descriptor.Default.DescriptorTypeDependent = 0;
3500 _base_writeq(*request, &ioc->chip->RequestDescriptorPostLow,
3501 &ioc->scsi_lookup_lock);
3505 * mpt3sas_base_put_smid_default - Default, primarily used for config pages
3506 * @ioc: per adapter object
3507 * @smid: system request message index
3509 void
3510 mpt3sas_base_put_smid_default(struct MPT3SAS_ADAPTER *ioc, u16 smid)
3512 Mpi2RequestDescriptorUnion_t descriptor;
3513 void *mpi_req_iomem;
3514 u64 *request;
3516 if (ioc->is_mcpu_endpoint) {
3517 __le32 *mfp = (__le32 *)mpt3sas_base_get_msg_frame(ioc, smid);
3519 _clone_sg_entries(ioc, (void *) mfp, smid);
3520 /* TBD 256 is offset within sys register */
3521 mpi_req_iomem = (void __force *)ioc->chip +
3522 MPI_FRAME_START_OFFSET + (smid * ioc->request_sz);
3523 _base_clone_mpi_to_sys_mem(mpi_req_iomem, (void *)mfp,
3524 ioc->request_sz);
3526 request = (u64 *)&descriptor;
3527 descriptor.Default.RequestFlags = MPI2_REQ_DESCRIPT_FLAGS_DEFAULT_TYPE;
3528 descriptor.Default.MSIxIndex = _base_get_msix_index(ioc);
3529 descriptor.Default.SMID = cpu_to_le16(smid);
3530 descriptor.Default.LMID = 0;
3531 descriptor.Default.DescriptorTypeDependent = 0;
3532 if (ioc->is_mcpu_endpoint)
3533 _base_mpi_ep_writeq(*request,
3534 &ioc->chip->RequestDescriptorPostLow,
3535 &ioc->scsi_lookup_lock);
3536 else
3537 _base_writeq(*request, &ioc->chip->RequestDescriptorPostLow,
3538 &ioc->scsi_lookup_lock);
3542 * _base_display_OEMs_branding - Display branding string
3543 * @ioc: per adapter object
3545 static void
3546 _base_display_OEMs_branding(struct MPT3SAS_ADAPTER *ioc)
3548 if (ioc->pdev->subsystem_vendor != PCI_VENDOR_ID_INTEL)
3549 return;
3551 switch (ioc->pdev->subsystem_vendor) {
3552 case PCI_VENDOR_ID_INTEL:
3553 switch (ioc->pdev->device) {
3554 case MPI2_MFGPAGE_DEVID_SAS2008:
3555 switch (ioc->pdev->subsystem_device) {
3556 case MPT2SAS_INTEL_RMS2LL080_SSDID:
3557 pr_info(MPT3SAS_FMT "%s\n", ioc->name,
3558 MPT2SAS_INTEL_RMS2LL080_BRANDING);
3559 break;
3560 case MPT2SAS_INTEL_RMS2LL040_SSDID:
3561 pr_info(MPT3SAS_FMT "%s\n", ioc->name,
3562 MPT2SAS_INTEL_RMS2LL040_BRANDING);
3563 break;
3564 case MPT2SAS_INTEL_SSD910_SSDID:
3565 pr_info(MPT3SAS_FMT "%s\n", ioc->name,
3566 MPT2SAS_INTEL_SSD910_BRANDING);
3567 break;
3568 default:
3569 pr_info(MPT3SAS_FMT
3570 "Intel(R) Controller: Subsystem ID: 0x%X\n",
3571 ioc->name, ioc->pdev->subsystem_device);
3572 break;
3574 case MPI2_MFGPAGE_DEVID_SAS2308_2:
3575 switch (ioc->pdev->subsystem_device) {
3576 case MPT2SAS_INTEL_RS25GB008_SSDID:
3577 pr_info(MPT3SAS_FMT "%s\n", ioc->name,
3578 MPT2SAS_INTEL_RS25GB008_BRANDING);
3579 break;
3580 case MPT2SAS_INTEL_RMS25JB080_SSDID:
3581 pr_info(MPT3SAS_FMT "%s\n", ioc->name,
3582 MPT2SAS_INTEL_RMS25JB080_BRANDING);
3583 break;
3584 case MPT2SAS_INTEL_RMS25JB040_SSDID:
3585 pr_info(MPT3SAS_FMT "%s\n", ioc->name,
3586 MPT2SAS_INTEL_RMS25JB040_BRANDING);
3587 break;
3588 case MPT2SAS_INTEL_RMS25KB080_SSDID:
3589 pr_info(MPT3SAS_FMT "%s\n", ioc->name,
3590 MPT2SAS_INTEL_RMS25KB080_BRANDING);
3591 break;
3592 case MPT2SAS_INTEL_RMS25KB040_SSDID:
3593 pr_info(MPT3SAS_FMT "%s\n", ioc->name,
3594 MPT2SAS_INTEL_RMS25KB040_BRANDING);
3595 break;
3596 case MPT2SAS_INTEL_RMS25LB040_SSDID:
3597 pr_info(MPT3SAS_FMT "%s\n", ioc->name,
3598 MPT2SAS_INTEL_RMS25LB040_BRANDING);
3599 break;
3600 case MPT2SAS_INTEL_RMS25LB080_SSDID:
3601 pr_info(MPT3SAS_FMT "%s\n", ioc->name,
3602 MPT2SAS_INTEL_RMS25LB080_BRANDING);
3603 break;
3604 default:
3605 pr_info(MPT3SAS_FMT
3606 "Intel(R) Controller: Subsystem ID: 0x%X\n",
3607 ioc->name, ioc->pdev->subsystem_device);
3608 break;
3610 case MPI25_MFGPAGE_DEVID_SAS3008:
3611 switch (ioc->pdev->subsystem_device) {
3612 case MPT3SAS_INTEL_RMS3JC080_SSDID:
3613 pr_info(MPT3SAS_FMT "%s\n", ioc->name,
3614 MPT3SAS_INTEL_RMS3JC080_BRANDING);
3615 break;
3617 case MPT3SAS_INTEL_RS3GC008_SSDID:
3618 pr_info(MPT3SAS_FMT "%s\n", ioc->name,
3619 MPT3SAS_INTEL_RS3GC008_BRANDING);
3620 break;
3621 case MPT3SAS_INTEL_RS3FC044_SSDID:
3622 pr_info(MPT3SAS_FMT "%s\n", ioc->name,
3623 MPT3SAS_INTEL_RS3FC044_BRANDING);
3624 break;
3625 case MPT3SAS_INTEL_RS3UC080_SSDID:
3626 pr_info(MPT3SAS_FMT "%s\n", ioc->name,
3627 MPT3SAS_INTEL_RS3UC080_BRANDING);
3628 break;
3629 default:
3630 pr_info(MPT3SAS_FMT
3631 "Intel(R) Controller: Subsystem ID: 0x%X\n",
3632 ioc->name, ioc->pdev->subsystem_device);
3633 break;
3635 break;
3636 default:
3637 pr_info(MPT3SAS_FMT
3638 "Intel(R) Controller: Subsystem ID: 0x%X\n",
3639 ioc->name, ioc->pdev->subsystem_device);
3640 break;
3642 break;
3643 case PCI_VENDOR_ID_DELL:
3644 switch (ioc->pdev->device) {
3645 case MPI2_MFGPAGE_DEVID_SAS2008:
3646 switch (ioc->pdev->subsystem_device) {
3647 case MPT2SAS_DELL_6GBPS_SAS_HBA_SSDID:
3648 pr_info(MPT3SAS_FMT "%s\n", ioc->name,
3649 MPT2SAS_DELL_6GBPS_SAS_HBA_BRANDING);
3650 break;
3651 case MPT2SAS_DELL_PERC_H200_ADAPTER_SSDID:
3652 pr_info(MPT3SAS_FMT "%s\n", ioc->name,
3653 MPT2SAS_DELL_PERC_H200_ADAPTER_BRANDING);
3654 break;
3655 case MPT2SAS_DELL_PERC_H200_INTEGRATED_SSDID:
3656 pr_info(MPT3SAS_FMT "%s\n", ioc->name,
3657 MPT2SAS_DELL_PERC_H200_INTEGRATED_BRANDING);
3658 break;
3659 case MPT2SAS_DELL_PERC_H200_MODULAR_SSDID:
3660 pr_info(MPT3SAS_FMT "%s\n", ioc->name,
3661 MPT2SAS_DELL_PERC_H200_MODULAR_BRANDING);
3662 break;
3663 case MPT2SAS_DELL_PERC_H200_EMBEDDED_SSDID:
3664 pr_info(MPT3SAS_FMT "%s\n", ioc->name,
3665 MPT2SAS_DELL_PERC_H200_EMBEDDED_BRANDING);
3666 break;
3667 case MPT2SAS_DELL_PERC_H200_SSDID:
3668 pr_info(MPT3SAS_FMT "%s\n", ioc->name,
3669 MPT2SAS_DELL_PERC_H200_BRANDING);
3670 break;
3671 case MPT2SAS_DELL_6GBPS_SAS_SSDID:
3672 pr_info(MPT3SAS_FMT "%s\n", ioc->name,
3673 MPT2SAS_DELL_6GBPS_SAS_BRANDING);
3674 break;
3675 default:
3676 pr_info(MPT3SAS_FMT
3677 "Dell 6Gbps HBA: Subsystem ID: 0x%X\n",
3678 ioc->name, ioc->pdev->subsystem_device);
3679 break;
3681 break;
3682 case MPI25_MFGPAGE_DEVID_SAS3008:
3683 switch (ioc->pdev->subsystem_device) {
3684 case MPT3SAS_DELL_12G_HBA_SSDID:
3685 pr_info(MPT3SAS_FMT "%s\n", ioc->name,
3686 MPT3SAS_DELL_12G_HBA_BRANDING);
3687 break;
3688 default:
3689 pr_info(MPT3SAS_FMT
3690 "Dell 12Gbps HBA: Subsystem ID: 0x%X\n",
3691 ioc->name, ioc->pdev->subsystem_device);
3692 break;
3694 break;
3695 default:
3696 pr_info(MPT3SAS_FMT
3697 "Dell HBA: Subsystem ID: 0x%X\n", ioc->name,
3698 ioc->pdev->subsystem_device);
3699 break;
3701 break;
3702 case PCI_VENDOR_ID_CISCO:
3703 switch (ioc->pdev->device) {
3704 case MPI25_MFGPAGE_DEVID_SAS3008:
3705 switch (ioc->pdev->subsystem_device) {
3706 case MPT3SAS_CISCO_12G_8E_HBA_SSDID:
3707 pr_info(MPT3SAS_FMT "%s\n", ioc->name,
3708 MPT3SAS_CISCO_12G_8E_HBA_BRANDING);
3709 break;
3710 case MPT3SAS_CISCO_12G_8I_HBA_SSDID:
3711 pr_info(MPT3SAS_FMT "%s\n", ioc->name,
3712 MPT3SAS_CISCO_12G_8I_HBA_BRANDING);
3713 break;
3714 case MPT3SAS_CISCO_12G_AVILA_HBA_SSDID:
3715 pr_info(MPT3SAS_FMT "%s\n", ioc->name,
3716 MPT3SAS_CISCO_12G_AVILA_HBA_BRANDING);
3717 break;
3718 default:
3719 pr_info(MPT3SAS_FMT
3720 "Cisco 12Gbps SAS HBA: Subsystem ID: 0x%X\n",
3721 ioc->name, ioc->pdev->subsystem_device);
3722 break;
3724 break;
3725 case MPI25_MFGPAGE_DEVID_SAS3108_1:
3726 switch (ioc->pdev->subsystem_device) {
3727 case MPT3SAS_CISCO_12G_AVILA_HBA_SSDID:
3728 pr_info(MPT3SAS_FMT "%s\n", ioc->name,
3729 MPT3SAS_CISCO_12G_AVILA_HBA_BRANDING);
3730 break;
3731 case MPT3SAS_CISCO_12G_COLUSA_MEZZANINE_HBA_SSDID:
3732 pr_info(MPT3SAS_FMT "%s\n", ioc->name,
3733 MPT3SAS_CISCO_12G_COLUSA_MEZZANINE_HBA_BRANDING
3735 break;
3736 default:
3737 pr_info(MPT3SAS_FMT
3738 "Cisco 12Gbps SAS HBA: Subsystem ID: 0x%X\n",
3739 ioc->name, ioc->pdev->subsystem_device);
3740 break;
3742 break;
3743 default:
3744 pr_info(MPT3SAS_FMT
3745 "Cisco SAS HBA: Subsystem ID: 0x%X\n",
3746 ioc->name, ioc->pdev->subsystem_device);
3747 break;
3749 break;
3750 case MPT2SAS_HP_3PAR_SSVID:
3751 switch (ioc->pdev->device) {
3752 case MPI2_MFGPAGE_DEVID_SAS2004:
3753 switch (ioc->pdev->subsystem_device) {
3754 case MPT2SAS_HP_DAUGHTER_2_4_INTERNAL_SSDID:
3755 pr_info(MPT3SAS_FMT "%s\n", ioc->name,
3756 MPT2SAS_HP_DAUGHTER_2_4_INTERNAL_BRANDING);
3757 break;
3758 default:
3759 pr_info(MPT3SAS_FMT
3760 "HP 6Gbps SAS HBA: Subsystem ID: 0x%X\n",
3761 ioc->name, ioc->pdev->subsystem_device);
3762 break;
3764 case MPI2_MFGPAGE_DEVID_SAS2308_2:
3765 switch (ioc->pdev->subsystem_device) {
3766 case MPT2SAS_HP_2_4_INTERNAL_SSDID:
3767 pr_info(MPT3SAS_FMT "%s\n", ioc->name,
3768 MPT2SAS_HP_2_4_INTERNAL_BRANDING);
3769 break;
3770 case MPT2SAS_HP_2_4_EXTERNAL_SSDID:
3771 pr_info(MPT3SAS_FMT "%s\n", ioc->name,
3772 MPT2SAS_HP_2_4_EXTERNAL_BRANDING);
3773 break;
3774 case MPT2SAS_HP_1_4_INTERNAL_1_4_EXTERNAL_SSDID:
3775 pr_info(MPT3SAS_FMT "%s\n", ioc->name,
3776 MPT2SAS_HP_1_4_INTERNAL_1_4_EXTERNAL_BRANDING);
3777 break;
3778 case MPT2SAS_HP_EMBEDDED_2_4_INTERNAL_SSDID:
3779 pr_info(MPT3SAS_FMT "%s\n", ioc->name,
3780 MPT2SAS_HP_EMBEDDED_2_4_INTERNAL_BRANDING);
3781 break;
3782 default:
3783 pr_info(MPT3SAS_FMT
3784 "HP 6Gbps SAS HBA: Subsystem ID: 0x%X\n",
3785 ioc->name, ioc->pdev->subsystem_device);
3786 break;
3788 default:
3789 pr_info(MPT3SAS_FMT
3790 "HP SAS HBA: Subsystem ID: 0x%X\n",
3791 ioc->name, ioc->pdev->subsystem_device);
3792 break;
3794 default:
3795 break;
3800 * _base_display_fwpkg_version - sends FWUpload request to pull FWPkg
3801 * version from FW Image Header.
3802 * @ioc: per adapter object
3804 * Return: 0 for success, non-zero for failure.
3806 static int
3807 _base_display_fwpkg_version(struct MPT3SAS_ADAPTER *ioc)
3809 Mpi2FWImageHeader_t *FWImgHdr;
3810 Mpi25FWUploadRequest_t *mpi_request;
3811 Mpi2FWUploadReply_t mpi_reply;
3812 int r = 0;
3813 void *fwpkg_data = NULL;
3814 dma_addr_t fwpkg_data_dma;
3815 u16 smid, ioc_status;
3816 size_t data_length;
3818 dinitprintk(ioc, pr_info(MPT3SAS_FMT "%s\n", ioc->name,
3819 __func__));
3821 if (ioc->base_cmds.status & MPT3_CMD_PENDING) {
3822 pr_err(MPT3SAS_FMT "%s: internal command already in use\n",
3823 ioc->name, __func__);
3824 return -EAGAIN;
3827 data_length = sizeof(Mpi2FWImageHeader_t);
3828 fwpkg_data = pci_alloc_consistent(ioc->pdev, data_length,
3829 &fwpkg_data_dma);
3830 if (!fwpkg_data) {
3831 pr_err(MPT3SAS_FMT "failure at %s:%d/%s()!\n",
3832 ioc->name, __FILE__, __LINE__, __func__);
3833 return -ENOMEM;
3836 smid = mpt3sas_base_get_smid(ioc, ioc->base_cb_idx);
3837 if (!smid) {
3838 pr_err(MPT3SAS_FMT "%s: failed obtaining a smid\n",
3839 ioc->name, __func__);
3840 r = -EAGAIN;
3841 goto out;
3844 ioc->base_cmds.status = MPT3_CMD_PENDING;
3845 mpi_request = mpt3sas_base_get_msg_frame(ioc, smid);
3846 ioc->base_cmds.smid = smid;
3847 memset(mpi_request, 0, sizeof(Mpi25FWUploadRequest_t));
3848 mpi_request->Function = MPI2_FUNCTION_FW_UPLOAD;
3849 mpi_request->ImageType = MPI2_FW_UPLOAD_ITYPE_FW_FLASH;
3850 mpi_request->ImageSize = cpu_to_le32(data_length);
3851 ioc->build_sg(ioc, &mpi_request->SGL, 0, 0, fwpkg_data_dma,
3852 data_length);
3853 init_completion(&ioc->base_cmds.done);
3854 mpt3sas_base_put_smid_default(ioc, smid);
3855 /* Wait for 15 seconds */
3856 wait_for_completion_timeout(&ioc->base_cmds.done,
3857 FW_IMG_HDR_READ_TIMEOUT*HZ);
3858 pr_info(MPT3SAS_FMT "%s: complete\n",
3859 ioc->name, __func__);
3860 if (!(ioc->base_cmds.status & MPT3_CMD_COMPLETE)) {
3861 pr_err(MPT3SAS_FMT "%s: timeout\n",
3862 ioc->name, __func__);
3863 _debug_dump_mf(mpi_request,
3864 sizeof(Mpi25FWUploadRequest_t)/4);
3865 r = -ETIME;
3866 } else {
3867 memset(&mpi_reply, 0, sizeof(Mpi2FWUploadReply_t));
3868 if (ioc->base_cmds.status & MPT3_CMD_REPLY_VALID) {
3869 memcpy(&mpi_reply, ioc->base_cmds.reply,
3870 sizeof(Mpi2FWUploadReply_t));
3871 ioc_status = le16_to_cpu(mpi_reply.IOCStatus) &
3872 MPI2_IOCSTATUS_MASK;
3873 if (ioc_status == MPI2_IOCSTATUS_SUCCESS) {
3874 FWImgHdr = (Mpi2FWImageHeader_t *)fwpkg_data;
3875 if (FWImgHdr->PackageVersion.Word) {
3876 pr_info(MPT3SAS_FMT "FW Package Version"
3877 "(%02d.%02d.%02d.%02d)\n",
3878 ioc->name,
3879 FWImgHdr->PackageVersion.Struct.Major,
3880 FWImgHdr->PackageVersion.Struct.Minor,
3881 FWImgHdr->PackageVersion.Struct.Unit,
3882 FWImgHdr->PackageVersion.Struct.Dev);
3884 } else {
3885 _debug_dump_mf(&mpi_reply,
3886 sizeof(Mpi2FWUploadReply_t)/4);
3890 ioc->base_cmds.status = MPT3_CMD_NOT_USED;
3891 out:
3892 if (fwpkg_data)
3893 pci_free_consistent(ioc->pdev, data_length, fwpkg_data,
3894 fwpkg_data_dma);
3895 return r;
3899 * _base_display_ioc_capabilities - Disply IOC's capabilities.
3900 * @ioc: per adapter object
3902 static void
3903 _base_display_ioc_capabilities(struct MPT3SAS_ADAPTER *ioc)
3905 int i = 0;
3906 char desc[16];
3907 u32 iounit_pg1_flags;
3908 u32 bios_version;
3910 bios_version = le32_to_cpu(ioc->bios_pg3.BiosVersion);
3911 strncpy(desc, ioc->manu_pg0.ChipName, 16);
3912 pr_info(MPT3SAS_FMT "%s: FWVersion(%02d.%02d.%02d.%02d), "\
3913 "ChipRevision(0x%02x), BiosVersion(%02d.%02d.%02d.%02d)\n",
3914 ioc->name, desc,
3915 (ioc->facts.FWVersion.Word & 0xFF000000) >> 24,
3916 (ioc->facts.FWVersion.Word & 0x00FF0000) >> 16,
3917 (ioc->facts.FWVersion.Word & 0x0000FF00) >> 8,
3918 ioc->facts.FWVersion.Word & 0x000000FF,
3919 ioc->pdev->revision,
3920 (bios_version & 0xFF000000) >> 24,
3921 (bios_version & 0x00FF0000) >> 16,
3922 (bios_version & 0x0000FF00) >> 8,
3923 bios_version & 0x000000FF);
3925 _base_display_OEMs_branding(ioc);
3927 if (ioc->facts.ProtocolFlags & MPI2_IOCFACTS_PROTOCOL_NVME_DEVICES) {
3928 pr_info("%sNVMe", i ? "," : "");
3929 i++;
3932 pr_info(MPT3SAS_FMT "Protocol=(", ioc->name);
3934 if (ioc->facts.ProtocolFlags & MPI2_IOCFACTS_PROTOCOL_SCSI_INITIATOR) {
3935 pr_info("Initiator");
3936 i++;
3939 if (ioc->facts.ProtocolFlags & MPI2_IOCFACTS_PROTOCOL_SCSI_TARGET) {
3940 pr_info("%sTarget", i ? "," : "");
3941 i++;
3944 i = 0;
3945 pr_info("), ");
3946 pr_info("Capabilities=(");
3948 if (!ioc->hide_ir_msg) {
3949 if (ioc->facts.IOCCapabilities &
3950 MPI2_IOCFACTS_CAPABILITY_INTEGRATED_RAID) {
3951 pr_info("Raid");
3952 i++;
3956 if (ioc->facts.IOCCapabilities & MPI2_IOCFACTS_CAPABILITY_TLR) {
3957 pr_info("%sTLR", i ? "," : "");
3958 i++;
3961 if (ioc->facts.IOCCapabilities & MPI2_IOCFACTS_CAPABILITY_MULTICAST) {
3962 pr_info("%sMulticast", i ? "," : "");
3963 i++;
3966 if (ioc->facts.IOCCapabilities &
3967 MPI2_IOCFACTS_CAPABILITY_BIDIRECTIONAL_TARGET) {
3968 pr_info("%sBIDI Target", i ? "," : "");
3969 i++;
3972 if (ioc->facts.IOCCapabilities & MPI2_IOCFACTS_CAPABILITY_EEDP) {
3973 pr_info("%sEEDP", i ? "," : "");
3974 i++;
3977 if (ioc->facts.IOCCapabilities &
3978 MPI2_IOCFACTS_CAPABILITY_SNAPSHOT_BUFFER) {
3979 pr_info("%sSnapshot Buffer", i ? "," : "");
3980 i++;
3983 if (ioc->facts.IOCCapabilities &
3984 MPI2_IOCFACTS_CAPABILITY_DIAG_TRACE_BUFFER) {
3985 pr_info("%sDiag Trace Buffer", i ? "," : "");
3986 i++;
3989 if (ioc->facts.IOCCapabilities &
3990 MPI2_IOCFACTS_CAPABILITY_EXTENDED_BUFFER) {
3991 pr_info("%sDiag Extended Buffer", i ? "," : "");
3992 i++;
3995 if (ioc->facts.IOCCapabilities &
3996 MPI2_IOCFACTS_CAPABILITY_TASK_SET_FULL_HANDLING) {
3997 pr_info("%sTask Set Full", i ? "," : "");
3998 i++;
4001 iounit_pg1_flags = le32_to_cpu(ioc->iounit_pg1.Flags);
4002 if (!(iounit_pg1_flags & MPI2_IOUNITPAGE1_NATIVE_COMMAND_Q_DISABLE)) {
4003 pr_info("%sNCQ", i ? "," : "");
4004 i++;
4007 pr_info(")\n");
4011 * mpt3sas_base_update_missing_delay - change the missing delay timers
4012 * @ioc: per adapter object
4013 * @device_missing_delay: amount of time till device is reported missing
4014 * @io_missing_delay: interval IO is returned when there is a missing device
4016 * Passed on the command line, this function will modify the device missing
4017 * delay, as well as the io missing delay. This should be called at driver
4018 * load time.
4020 void
4021 mpt3sas_base_update_missing_delay(struct MPT3SAS_ADAPTER *ioc,
4022 u16 device_missing_delay, u8 io_missing_delay)
4024 u16 dmd, dmd_new, dmd_orignal;
4025 u8 io_missing_delay_original;
4026 u16 sz;
4027 Mpi2SasIOUnitPage1_t *sas_iounit_pg1 = NULL;
4028 Mpi2ConfigReply_t mpi_reply;
4029 u8 num_phys = 0;
4030 u16 ioc_status;
4032 mpt3sas_config_get_number_hba_phys(ioc, &num_phys);
4033 if (!num_phys)
4034 return;
4036 sz = offsetof(Mpi2SasIOUnitPage1_t, PhyData) + (num_phys *
4037 sizeof(Mpi2SasIOUnit1PhyData_t));
4038 sas_iounit_pg1 = kzalloc(sz, GFP_KERNEL);
4039 if (!sas_iounit_pg1) {
4040 pr_err(MPT3SAS_FMT "failure at %s:%d/%s()!\n",
4041 ioc->name, __FILE__, __LINE__, __func__);
4042 goto out;
4044 if ((mpt3sas_config_get_sas_iounit_pg1(ioc, &mpi_reply,
4045 sas_iounit_pg1, sz))) {
4046 pr_err(MPT3SAS_FMT "failure at %s:%d/%s()!\n",
4047 ioc->name, __FILE__, __LINE__, __func__);
4048 goto out;
4050 ioc_status = le16_to_cpu(mpi_reply.IOCStatus) &
4051 MPI2_IOCSTATUS_MASK;
4052 if (ioc_status != MPI2_IOCSTATUS_SUCCESS) {
4053 pr_err(MPT3SAS_FMT "failure at %s:%d/%s()!\n",
4054 ioc->name, __FILE__, __LINE__, __func__);
4055 goto out;
4058 /* device missing delay */
4059 dmd = sas_iounit_pg1->ReportDeviceMissingDelay;
4060 if (dmd & MPI2_SASIOUNIT1_REPORT_MISSING_UNIT_16)
4061 dmd = (dmd & MPI2_SASIOUNIT1_REPORT_MISSING_TIMEOUT_MASK) * 16;
4062 else
4063 dmd = dmd & MPI2_SASIOUNIT1_REPORT_MISSING_TIMEOUT_MASK;
4064 dmd_orignal = dmd;
4065 if (device_missing_delay > 0x7F) {
4066 dmd = (device_missing_delay > 0x7F0) ? 0x7F0 :
4067 device_missing_delay;
4068 dmd = dmd / 16;
4069 dmd |= MPI2_SASIOUNIT1_REPORT_MISSING_UNIT_16;
4070 } else
4071 dmd = device_missing_delay;
4072 sas_iounit_pg1->ReportDeviceMissingDelay = dmd;
4074 /* io missing delay */
4075 io_missing_delay_original = sas_iounit_pg1->IODeviceMissingDelay;
4076 sas_iounit_pg1->IODeviceMissingDelay = io_missing_delay;
4078 if (!mpt3sas_config_set_sas_iounit_pg1(ioc, &mpi_reply, sas_iounit_pg1,
4079 sz)) {
4080 if (dmd & MPI2_SASIOUNIT1_REPORT_MISSING_UNIT_16)
4081 dmd_new = (dmd &
4082 MPI2_SASIOUNIT1_REPORT_MISSING_TIMEOUT_MASK) * 16;
4083 else
4084 dmd_new =
4085 dmd & MPI2_SASIOUNIT1_REPORT_MISSING_TIMEOUT_MASK;
4086 pr_info(MPT3SAS_FMT "device_missing_delay: old(%d), new(%d)\n",
4087 ioc->name, dmd_orignal, dmd_new);
4088 pr_info(MPT3SAS_FMT "ioc_missing_delay: old(%d), new(%d)\n",
4089 ioc->name, io_missing_delay_original,
4090 io_missing_delay);
4091 ioc->device_missing_delay = dmd_new;
4092 ioc->io_missing_delay = io_missing_delay;
4095 out:
4096 kfree(sas_iounit_pg1);
4100 * _base_static_config_pages - static start of day config pages
4101 * @ioc: per adapter object
4103 static void
4104 _base_static_config_pages(struct MPT3SAS_ADAPTER *ioc)
4106 Mpi2ConfigReply_t mpi_reply;
4107 u32 iounit_pg1_flags;
4109 ioc->nvme_abort_timeout = 30;
4110 mpt3sas_config_get_manufacturing_pg0(ioc, &mpi_reply, &ioc->manu_pg0);
4111 if (ioc->ir_firmware)
4112 mpt3sas_config_get_manufacturing_pg10(ioc, &mpi_reply,
4113 &ioc->manu_pg10);
4116 * Ensure correct T10 PI operation if vendor left EEDPTagMode
4117 * flag unset in NVDATA.
4119 mpt3sas_config_get_manufacturing_pg11(ioc, &mpi_reply, &ioc->manu_pg11);
4120 if (!ioc->is_gen35_ioc && ioc->manu_pg11.EEDPTagMode == 0) {
4121 pr_err("%s: overriding NVDATA EEDPTagMode setting\n",
4122 ioc->name);
4123 ioc->manu_pg11.EEDPTagMode &= ~0x3;
4124 ioc->manu_pg11.EEDPTagMode |= 0x1;
4125 mpt3sas_config_set_manufacturing_pg11(ioc, &mpi_reply,
4126 &ioc->manu_pg11);
4128 if (ioc->manu_pg11.AddlFlags2 & NVME_TASK_MNGT_CUSTOM_MASK)
4129 ioc->tm_custom_handling = 1;
4130 else {
4131 ioc->tm_custom_handling = 0;
4132 if (ioc->manu_pg11.NVMeAbortTO < NVME_TASK_ABORT_MIN_TIMEOUT)
4133 ioc->nvme_abort_timeout = NVME_TASK_ABORT_MIN_TIMEOUT;
4134 else if (ioc->manu_pg11.NVMeAbortTO >
4135 NVME_TASK_ABORT_MAX_TIMEOUT)
4136 ioc->nvme_abort_timeout = NVME_TASK_ABORT_MAX_TIMEOUT;
4137 else
4138 ioc->nvme_abort_timeout = ioc->manu_pg11.NVMeAbortTO;
4141 mpt3sas_config_get_bios_pg2(ioc, &mpi_reply, &ioc->bios_pg2);
4142 mpt3sas_config_get_bios_pg3(ioc, &mpi_reply, &ioc->bios_pg3);
4143 mpt3sas_config_get_ioc_pg8(ioc, &mpi_reply, &ioc->ioc_pg8);
4144 mpt3sas_config_get_iounit_pg0(ioc, &mpi_reply, &ioc->iounit_pg0);
4145 mpt3sas_config_get_iounit_pg1(ioc, &mpi_reply, &ioc->iounit_pg1);
4146 mpt3sas_config_get_iounit_pg8(ioc, &mpi_reply, &ioc->iounit_pg8);
4147 _base_display_ioc_capabilities(ioc);
4150 * Enable task_set_full handling in iounit_pg1 when the
4151 * facts capabilities indicate that its supported.
4153 iounit_pg1_flags = le32_to_cpu(ioc->iounit_pg1.Flags);
4154 if ((ioc->facts.IOCCapabilities &
4155 MPI2_IOCFACTS_CAPABILITY_TASK_SET_FULL_HANDLING))
4156 iounit_pg1_flags &=
4157 ~MPI2_IOUNITPAGE1_DISABLE_TASK_SET_FULL_HANDLING;
4158 else
4159 iounit_pg1_flags |=
4160 MPI2_IOUNITPAGE1_DISABLE_TASK_SET_FULL_HANDLING;
4161 ioc->iounit_pg1.Flags = cpu_to_le32(iounit_pg1_flags);
4162 mpt3sas_config_set_iounit_pg1(ioc, &mpi_reply, &ioc->iounit_pg1);
4164 if (ioc->iounit_pg8.NumSensors)
4165 ioc->temp_sensors_count = ioc->iounit_pg8.NumSensors;
4169 * mpt3sas_free_enclosure_list - release memory
4170 * @ioc: per adapter object
4172 * Free memory allocated during encloure add.
4174 void
4175 mpt3sas_free_enclosure_list(struct MPT3SAS_ADAPTER *ioc)
4177 struct _enclosure_node *enclosure_dev, *enclosure_dev_next;
4179 /* Free enclosure list */
4180 list_for_each_entry_safe(enclosure_dev,
4181 enclosure_dev_next, &ioc->enclosure_list, list) {
4182 list_del(&enclosure_dev->list);
4183 kfree(enclosure_dev);
4188 * _base_release_memory_pools - release memory
4189 * @ioc: per adapter object
4191 * Free memory allocated from _base_allocate_memory_pools.
4193 static void
4194 _base_release_memory_pools(struct MPT3SAS_ADAPTER *ioc)
4196 int i = 0;
4197 int j = 0;
4198 struct chain_tracker *ct;
4199 struct reply_post_struct *rps;
4201 dexitprintk(ioc, pr_info(MPT3SAS_FMT "%s\n", ioc->name,
4202 __func__));
4204 if (ioc->request) {
4205 pci_free_consistent(ioc->pdev, ioc->request_dma_sz,
4206 ioc->request, ioc->request_dma);
4207 dexitprintk(ioc, pr_info(MPT3SAS_FMT
4208 "request_pool(0x%p): free\n",
4209 ioc->name, ioc->request));
4210 ioc->request = NULL;
4213 if (ioc->sense) {
4214 dma_pool_free(ioc->sense_dma_pool, ioc->sense, ioc->sense_dma);
4215 dma_pool_destroy(ioc->sense_dma_pool);
4216 dexitprintk(ioc, pr_info(MPT3SAS_FMT
4217 "sense_pool(0x%p): free\n",
4218 ioc->name, ioc->sense));
4219 ioc->sense = NULL;
4222 if (ioc->reply) {
4223 dma_pool_free(ioc->reply_dma_pool, ioc->reply, ioc->reply_dma);
4224 dma_pool_destroy(ioc->reply_dma_pool);
4225 dexitprintk(ioc, pr_info(MPT3SAS_FMT
4226 "reply_pool(0x%p): free\n",
4227 ioc->name, ioc->reply));
4228 ioc->reply = NULL;
4231 if (ioc->reply_free) {
4232 dma_pool_free(ioc->reply_free_dma_pool, ioc->reply_free,
4233 ioc->reply_free_dma);
4234 dma_pool_destroy(ioc->reply_free_dma_pool);
4235 dexitprintk(ioc, pr_info(MPT3SAS_FMT
4236 "reply_free_pool(0x%p): free\n",
4237 ioc->name, ioc->reply_free));
4238 ioc->reply_free = NULL;
4241 if (ioc->reply_post) {
4242 do {
4243 rps = &ioc->reply_post[i];
4244 if (rps->reply_post_free) {
4245 dma_pool_free(
4246 ioc->reply_post_free_dma_pool,
4247 rps->reply_post_free,
4248 rps->reply_post_free_dma);
4249 dexitprintk(ioc, pr_info(MPT3SAS_FMT
4250 "reply_post_free_pool(0x%p): free\n",
4251 ioc->name, rps->reply_post_free));
4252 rps->reply_post_free = NULL;
4254 } while (ioc->rdpq_array_enable &&
4255 (++i < ioc->reply_queue_count));
4256 if (ioc->reply_post_free_array &&
4257 ioc->rdpq_array_enable) {
4258 dma_pool_free(ioc->reply_post_free_array_dma_pool,
4259 ioc->reply_post_free_array,
4260 ioc->reply_post_free_array_dma);
4261 ioc->reply_post_free_array = NULL;
4263 dma_pool_destroy(ioc->reply_post_free_array_dma_pool);
4264 dma_pool_destroy(ioc->reply_post_free_dma_pool);
4265 kfree(ioc->reply_post);
4268 if (ioc->pcie_sgl_dma_pool) {
4269 for (i = 0; i < ioc->scsiio_depth; i++) {
4270 dma_pool_free(ioc->pcie_sgl_dma_pool,
4271 ioc->pcie_sg_lookup[i].pcie_sgl,
4272 ioc->pcie_sg_lookup[i].pcie_sgl_dma);
4274 if (ioc->pcie_sgl_dma_pool)
4275 dma_pool_destroy(ioc->pcie_sgl_dma_pool);
4278 if (ioc->config_page) {
4279 dexitprintk(ioc, pr_info(MPT3SAS_FMT
4280 "config_page(0x%p): free\n", ioc->name,
4281 ioc->config_page));
4282 pci_free_consistent(ioc->pdev, ioc->config_page_sz,
4283 ioc->config_page, ioc->config_page_dma);
4286 kfree(ioc->hpr_lookup);
4287 kfree(ioc->internal_lookup);
4288 if (ioc->chain_lookup) {
4289 for (i = 0; i < ioc->scsiio_depth; i++) {
4290 for (j = ioc->chains_per_prp_buffer;
4291 j < ioc->chains_needed_per_io; j++) {
4292 ct = &ioc->chain_lookup[i].chains_per_smid[j];
4293 if (ct && ct->chain_buffer)
4294 dma_pool_free(ioc->chain_dma_pool,
4295 ct->chain_buffer,
4296 ct->chain_buffer_dma);
4298 kfree(ioc->chain_lookup[i].chains_per_smid);
4300 dma_pool_destroy(ioc->chain_dma_pool);
4301 kfree(ioc->chain_lookup);
4302 ioc->chain_lookup = NULL;
4307 * is_MSB_are_same - checks whether all reply queues in a set are
4308 * having same upper 32bits in their base memory address.
4309 * @reply_pool_start_address: Base address of a reply queue set
4310 * @pool_sz: Size of single Reply Descriptor Post Queues pool size
4312 * Return: 1 if reply queues in a set have a same upper 32bits in their base
4313 * memory address, else 0.
4316 static int
4317 is_MSB_are_same(long reply_pool_start_address, u32 pool_sz)
4319 long reply_pool_end_address;
4321 reply_pool_end_address = reply_pool_start_address + pool_sz;
4323 if (upper_32_bits(reply_pool_start_address) ==
4324 upper_32_bits(reply_pool_end_address))
4325 return 1;
4326 else
4327 return 0;
4331 * _base_allocate_memory_pools - allocate start of day memory pools
4332 * @ioc: per adapter object
4334 * Return: 0 success, anything else error.
4336 static int
4337 _base_allocate_memory_pools(struct MPT3SAS_ADAPTER *ioc)
4339 struct mpt3sas_facts *facts;
4340 u16 max_sge_elements;
4341 u16 chains_needed_per_io;
4342 u32 sz, total_sz, reply_post_free_sz, reply_post_free_array_sz;
4343 u32 retry_sz;
4344 u16 max_request_credit, nvme_blocks_needed;
4345 unsigned short sg_tablesize;
4346 u16 sge_size;
4347 int i, j;
4348 struct chain_tracker *ct;
4350 dinitprintk(ioc, pr_info(MPT3SAS_FMT "%s\n", ioc->name,
4351 __func__));
4354 retry_sz = 0;
4355 facts = &ioc->facts;
4357 /* command line tunables for max sgl entries */
4358 if (max_sgl_entries != -1)
4359 sg_tablesize = max_sgl_entries;
4360 else {
4361 if (ioc->hba_mpi_version_belonged == MPI2_VERSION)
4362 sg_tablesize = MPT2SAS_SG_DEPTH;
4363 else
4364 sg_tablesize = MPT3SAS_SG_DEPTH;
4367 /* max sgl entries <= MPT_KDUMP_MIN_PHYS_SEGMENTS in KDUMP mode */
4368 if (reset_devices)
4369 sg_tablesize = min_t(unsigned short, sg_tablesize,
4370 MPT_KDUMP_MIN_PHYS_SEGMENTS);
4372 if (ioc->is_mcpu_endpoint)
4373 ioc->shost->sg_tablesize = MPT_MIN_PHYS_SEGMENTS;
4374 else {
4375 if (sg_tablesize < MPT_MIN_PHYS_SEGMENTS)
4376 sg_tablesize = MPT_MIN_PHYS_SEGMENTS;
4377 else if (sg_tablesize > MPT_MAX_PHYS_SEGMENTS) {
4378 sg_tablesize = min_t(unsigned short, sg_tablesize,
4379 SG_MAX_SEGMENTS);
4380 pr_warn(MPT3SAS_FMT
4381 "sg_tablesize(%u) is bigger than kernel "
4382 "defined SG_CHUNK_SIZE(%u)\n", ioc->name,
4383 sg_tablesize, MPT_MAX_PHYS_SEGMENTS);
4385 ioc->shost->sg_tablesize = sg_tablesize;
4388 ioc->internal_depth = min_t(int, (facts->HighPriorityCredit + (5)),
4389 (facts->RequestCredit / 4));
4390 if (ioc->internal_depth < INTERNAL_CMDS_COUNT) {
4391 if (facts->RequestCredit <= (INTERNAL_CMDS_COUNT +
4392 INTERNAL_SCSIIO_CMDS_COUNT)) {
4393 pr_err(MPT3SAS_FMT "IOC doesn't have enough Request \
4394 Credits, it has just %d number of credits\n",
4395 ioc->name, facts->RequestCredit);
4396 return -ENOMEM;
4398 ioc->internal_depth = 10;
4401 ioc->hi_priority_depth = ioc->internal_depth - (5);
4402 /* command line tunables for max controller queue depth */
4403 if (max_queue_depth != -1 && max_queue_depth != 0) {
4404 max_request_credit = min_t(u16, max_queue_depth +
4405 ioc->internal_depth, facts->RequestCredit);
4406 if (max_request_credit > MAX_HBA_QUEUE_DEPTH)
4407 max_request_credit = MAX_HBA_QUEUE_DEPTH;
4408 } else if (reset_devices)
4409 max_request_credit = min_t(u16, facts->RequestCredit,
4410 (MPT3SAS_KDUMP_SCSI_IO_DEPTH + ioc->internal_depth));
4411 else
4412 max_request_credit = min_t(u16, facts->RequestCredit,
4413 MAX_HBA_QUEUE_DEPTH);
4415 /* Firmware maintains additional facts->HighPriorityCredit number of
4416 * credits for HiPriprity Request messages, so hba queue depth will be
4417 * sum of max_request_credit and high priority queue depth.
4419 ioc->hba_queue_depth = max_request_credit + ioc->hi_priority_depth;
4421 /* request frame size */
4422 ioc->request_sz = facts->IOCRequestFrameSize * 4;
4424 /* reply frame size */
4425 ioc->reply_sz = facts->ReplyFrameSize * 4;
4427 /* chain segment size */
4428 if (ioc->hba_mpi_version_belonged != MPI2_VERSION) {
4429 if (facts->IOCMaxChainSegmentSize)
4430 ioc->chain_segment_sz =
4431 facts->IOCMaxChainSegmentSize *
4432 MAX_CHAIN_ELEMT_SZ;
4433 else
4434 /* set to 128 bytes size if IOCMaxChainSegmentSize is zero */
4435 ioc->chain_segment_sz = DEFAULT_NUM_FWCHAIN_ELEMTS *
4436 MAX_CHAIN_ELEMT_SZ;
4437 } else
4438 ioc->chain_segment_sz = ioc->request_sz;
4440 /* calculate the max scatter element size */
4441 sge_size = max_t(u16, ioc->sge_size, ioc->sge_size_ieee);
4443 retry_allocation:
4444 total_sz = 0;
4445 /* calculate number of sg elements left over in the 1st frame */
4446 max_sge_elements = ioc->request_sz - ((sizeof(Mpi2SCSIIORequest_t) -
4447 sizeof(Mpi2SGEIOUnion_t)) + sge_size);
4448 ioc->max_sges_in_main_message = max_sge_elements/sge_size;
4450 /* now do the same for a chain buffer */
4451 max_sge_elements = ioc->chain_segment_sz - sge_size;
4452 ioc->max_sges_in_chain_message = max_sge_elements/sge_size;
4455 * MPT3SAS_SG_DEPTH = CONFIG_FUSION_MAX_SGE
4457 chains_needed_per_io = ((ioc->shost->sg_tablesize -
4458 ioc->max_sges_in_main_message)/ioc->max_sges_in_chain_message)
4459 + 1;
4460 if (chains_needed_per_io > facts->MaxChainDepth) {
4461 chains_needed_per_io = facts->MaxChainDepth;
4462 ioc->shost->sg_tablesize = min_t(u16,
4463 ioc->max_sges_in_main_message + (ioc->max_sges_in_chain_message
4464 * chains_needed_per_io), ioc->shost->sg_tablesize);
4466 ioc->chains_needed_per_io = chains_needed_per_io;
4468 /* reply free queue sizing - taking into account for 64 FW events */
4469 ioc->reply_free_queue_depth = ioc->hba_queue_depth + 64;
4471 /* mCPU manage single counters for simplicity */
4472 if (ioc->is_mcpu_endpoint)
4473 ioc->reply_post_queue_depth = ioc->reply_free_queue_depth;
4474 else {
4475 /* calculate reply descriptor post queue depth */
4476 ioc->reply_post_queue_depth = ioc->hba_queue_depth +
4477 ioc->reply_free_queue_depth + 1;
4478 /* align the reply post queue on the next 16 count boundary */
4479 if (ioc->reply_post_queue_depth % 16)
4480 ioc->reply_post_queue_depth += 16 -
4481 (ioc->reply_post_queue_depth % 16);
4484 if (ioc->reply_post_queue_depth >
4485 facts->MaxReplyDescriptorPostQueueDepth) {
4486 ioc->reply_post_queue_depth =
4487 facts->MaxReplyDescriptorPostQueueDepth -
4488 (facts->MaxReplyDescriptorPostQueueDepth % 16);
4489 ioc->hba_queue_depth =
4490 ((ioc->reply_post_queue_depth - 64) / 2) - 1;
4491 ioc->reply_free_queue_depth = ioc->hba_queue_depth + 64;
4494 dinitprintk(ioc, pr_info(MPT3SAS_FMT "scatter gather: " \
4495 "sge_in_main_msg(%d), sge_per_chain(%d), sge_per_io(%d), "
4496 "chains_per_io(%d)\n", ioc->name, ioc->max_sges_in_main_message,
4497 ioc->max_sges_in_chain_message, ioc->shost->sg_tablesize,
4498 ioc->chains_needed_per_io));
4500 /* reply post queue, 16 byte align */
4501 reply_post_free_sz = ioc->reply_post_queue_depth *
4502 sizeof(Mpi2DefaultReplyDescriptor_t);
4504 sz = reply_post_free_sz;
4505 if (_base_is_controller_msix_enabled(ioc) && !ioc->rdpq_array_enable)
4506 sz *= ioc->reply_queue_count;
4508 ioc->reply_post = kcalloc((ioc->rdpq_array_enable) ?
4509 (ioc->reply_queue_count):1,
4510 sizeof(struct reply_post_struct), GFP_KERNEL);
4512 if (!ioc->reply_post) {
4513 pr_err(MPT3SAS_FMT "reply_post_free pool: kcalloc failed\n",
4514 ioc->name);
4515 goto out;
4517 ioc->reply_post_free_dma_pool = dma_pool_create("reply_post_free pool",
4518 &ioc->pdev->dev, sz, 16, 0);
4519 if (!ioc->reply_post_free_dma_pool) {
4520 pr_err(MPT3SAS_FMT
4521 "reply_post_free pool: dma_pool_create failed\n",
4522 ioc->name);
4523 goto out;
4525 i = 0;
4526 do {
4527 ioc->reply_post[i].reply_post_free =
4528 dma_pool_alloc(ioc->reply_post_free_dma_pool,
4529 GFP_KERNEL,
4530 &ioc->reply_post[i].reply_post_free_dma);
4531 if (!ioc->reply_post[i].reply_post_free) {
4532 pr_err(MPT3SAS_FMT
4533 "reply_post_free pool: dma_pool_alloc failed\n",
4534 ioc->name);
4535 goto out;
4537 memset(ioc->reply_post[i].reply_post_free, 0, sz);
4538 dinitprintk(ioc, pr_info(MPT3SAS_FMT
4539 "reply post free pool (0x%p): depth(%d),"
4540 "element_size(%d), pool_size(%d kB)\n", ioc->name,
4541 ioc->reply_post[i].reply_post_free,
4542 ioc->reply_post_queue_depth, 8, sz/1024));
4543 dinitprintk(ioc, pr_info(MPT3SAS_FMT
4544 "reply_post_free_dma = (0x%llx)\n", ioc->name,
4545 (unsigned long long)
4546 ioc->reply_post[i].reply_post_free_dma));
4547 total_sz += sz;
4548 } while (ioc->rdpq_array_enable && (++i < ioc->reply_queue_count));
4550 if (ioc->dma_mask > 32) {
4551 if (_base_change_consistent_dma_mask(ioc, ioc->pdev) != 0) {
4552 pr_warn(MPT3SAS_FMT
4553 "no suitable consistent DMA mask for %s\n",
4554 ioc->name, pci_name(ioc->pdev));
4555 goto out;
4559 ioc->scsiio_depth = ioc->hba_queue_depth -
4560 ioc->hi_priority_depth - ioc->internal_depth;
4562 /* set the scsi host can_queue depth
4563 * with some internal commands that could be outstanding
4565 ioc->shost->can_queue = ioc->scsiio_depth - INTERNAL_SCSIIO_CMDS_COUNT;
4566 dinitprintk(ioc, pr_info(MPT3SAS_FMT
4567 "scsi host: can_queue depth (%d)\n",
4568 ioc->name, ioc->shost->can_queue));
4571 /* contiguous pool for request and chains, 16 byte align, one extra "
4572 * "frame for smid=0
4574 ioc->chain_depth = ioc->chains_needed_per_io * ioc->scsiio_depth;
4575 sz = ((ioc->scsiio_depth + 1) * ioc->request_sz);
4577 /* hi-priority queue */
4578 sz += (ioc->hi_priority_depth * ioc->request_sz);
4580 /* internal queue */
4581 sz += (ioc->internal_depth * ioc->request_sz);
4583 ioc->request_dma_sz = sz;
4584 ioc->request = pci_alloc_consistent(ioc->pdev, sz, &ioc->request_dma);
4585 if (!ioc->request) {
4586 pr_err(MPT3SAS_FMT "request pool: pci_alloc_consistent " \
4587 "failed: hba_depth(%d), chains_per_io(%d), frame_sz(%d), "
4588 "total(%d kB)\n", ioc->name, ioc->hba_queue_depth,
4589 ioc->chains_needed_per_io, ioc->request_sz, sz/1024);
4590 if (ioc->scsiio_depth < MPT3SAS_SAS_QUEUE_DEPTH)
4591 goto out;
4592 retry_sz = 64;
4593 ioc->hba_queue_depth -= retry_sz;
4594 _base_release_memory_pools(ioc);
4595 goto retry_allocation;
4598 if (retry_sz)
4599 pr_err(MPT3SAS_FMT "request pool: pci_alloc_consistent " \
4600 "succeed: hba_depth(%d), chains_per_io(%d), frame_sz(%d), "
4601 "total(%d kb)\n", ioc->name, ioc->hba_queue_depth,
4602 ioc->chains_needed_per_io, ioc->request_sz, sz/1024);
4604 /* hi-priority queue */
4605 ioc->hi_priority = ioc->request + ((ioc->scsiio_depth + 1) *
4606 ioc->request_sz);
4607 ioc->hi_priority_dma = ioc->request_dma + ((ioc->scsiio_depth + 1) *
4608 ioc->request_sz);
4610 /* internal queue */
4611 ioc->internal = ioc->hi_priority + (ioc->hi_priority_depth *
4612 ioc->request_sz);
4613 ioc->internal_dma = ioc->hi_priority_dma + (ioc->hi_priority_depth *
4614 ioc->request_sz);
4616 dinitprintk(ioc, pr_info(MPT3SAS_FMT
4617 "request pool(0x%p): depth(%d), frame_size(%d), pool_size(%d kB)\n",
4618 ioc->name, ioc->request, ioc->hba_queue_depth, ioc->request_sz,
4619 (ioc->hba_queue_depth * ioc->request_sz)/1024));
4621 dinitprintk(ioc, pr_info(MPT3SAS_FMT "request pool: dma(0x%llx)\n",
4622 ioc->name, (unsigned long long) ioc->request_dma));
4623 total_sz += sz;
4625 dinitprintk(ioc, pr_info(MPT3SAS_FMT "scsiio(0x%p): depth(%d)\n",
4626 ioc->name, ioc->request, ioc->scsiio_depth));
4628 ioc->chain_depth = min_t(u32, ioc->chain_depth, MAX_CHAIN_DEPTH);
4629 sz = ioc->scsiio_depth * sizeof(struct chain_lookup);
4630 ioc->chain_lookup = kzalloc(sz, GFP_KERNEL);
4631 if (!ioc->chain_lookup) {
4632 pr_err(MPT3SAS_FMT "chain_lookup: __get_free_pages "
4633 "failed\n", ioc->name);
4634 goto out;
4637 sz = ioc->chains_needed_per_io * sizeof(struct chain_tracker);
4638 for (i = 0; i < ioc->scsiio_depth; i++) {
4639 ioc->chain_lookup[i].chains_per_smid = kzalloc(sz, GFP_KERNEL);
4640 if (!ioc->chain_lookup[i].chains_per_smid) {
4641 pr_err(MPT3SAS_FMT "chain_lookup: "
4642 " kzalloc failed\n", ioc->name);
4643 goto out;
4647 /* initialize hi-priority queue smid's */
4648 ioc->hpr_lookup = kcalloc(ioc->hi_priority_depth,
4649 sizeof(struct request_tracker), GFP_KERNEL);
4650 if (!ioc->hpr_lookup) {
4651 pr_err(MPT3SAS_FMT "hpr_lookup: kcalloc failed\n",
4652 ioc->name);
4653 goto out;
4655 ioc->hi_priority_smid = ioc->scsiio_depth + 1;
4656 dinitprintk(ioc, pr_info(MPT3SAS_FMT
4657 "hi_priority(0x%p): depth(%d), start smid(%d)\n",
4658 ioc->name, ioc->hi_priority,
4659 ioc->hi_priority_depth, ioc->hi_priority_smid));
4661 /* initialize internal queue smid's */
4662 ioc->internal_lookup = kcalloc(ioc->internal_depth,
4663 sizeof(struct request_tracker), GFP_KERNEL);
4664 if (!ioc->internal_lookup) {
4665 pr_err(MPT3SAS_FMT "internal_lookup: kcalloc failed\n",
4666 ioc->name);
4667 goto out;
4669 ioc->internal_smid = ioc->hi_priority_smid + ioc->hi_priority_depth;
4670 dinitprintk(ioc, pr_info(MPT3SAS_FMT
4671 "internal(0x%p): depth(%d), start smid(%d)\n",
4672 ioc->name, ioc->internal,
4673 ioc->internal_depth, ioc->internal_smid));
4675 * The number of NVMe page sized blocks needed is:
4676 * (((sg_tablesize * 8) - 1) / (page_size - 8)) + 1
4677 * ((sg_tablesize * 8) - 1) is the max PRP's minus the first PRP entry
4678 * that is placed in the main message frame. 8 is the size of each PRP
4679 * entry or PRP list pointer entry. 8 is subtracted from page_size
4680 * because of the PRP list pointer entry at the end of a page, so this
4681 * is not counted as a PRP entry. The 1 added page is a round up.
4683 * To avoid allocation failures due to the amount of memory that could
4684 * be required for NVMe PRP's, only each set of NVMe blocks will be
4685 * contiguous, so a new set is allocated for each possible I/O.
4687 ioc->chains_per_prp_buffer = 0;
4688 if (ioc->facts.ProtocolFlags & MPI2_IOCFACTS_PROTOCOL_NVME_DEVICES) {
4689 nvme_blocks_needed =
4690 (ioc->shost->sg_tablesize * NVME_PRP_SIZE) - 1;
4691 nvme_blocks_needed /= (ioc->page_size - NVME_PRP_SIZE);
4692 nvme_blocks_needed++;
4694 sz = sizeof(struct pcie_sg_list) * ioc->scsiio_depth;
4695 ioc->pcie_sg_lookup = kzalloc(sz, GFP_KERNEL);
4696 if (!ioc->pcie_sg_lookup) {
4697 pr_info(MPT3SAS_FMT
4698 "PCIe SGL lookup: kzalloc failed\n", ioc->name);
4699 goto out;
4701 sz = nvme_blocks_needed * ioc->page_size;
4702 ioc->pcie_sgl_dma_pool =
4703 dma_pool_create("PCIe SGL pool", &ioc->pdev->dev, sz, 16, 0);
4704 if (!ioc->pcie_sgl_dma_pool) {
4705 pr_info(MPT3SAS_FMT
4706 "PCIe SGL pool: dma_pool_create failed\n",
4707 ioc->name);
4708 goto out;
4711 ioc->chains_per_prp_buffer = sz/ioc->chain_segment_sz;
4712 ioc->chains_per_prp_buffer = min(ioc->chains_per_prp_buffer,
4713 ioc->chains_needed_per_io);
4715 for (i = 0; i < ioc->scsiio_depth; i++) {
4716 ioc->pcie_sg_lookup[i].pcie_sgl = dma_pool_alloc(
4717 ioc->pcie_sgl_dma_pool, GFP_KERNEL,
4718 &ioc->pcie_sg_lookup[i].pcie_sgl_dma);
4719 if (!ioc->pcie_sg_lookup[i].pcie_sgl) {
4720 pr_info(MPT3SAS_FMT
4721 "PCIe SGL pool: dma_pool_alloc failed\n",
4722 ioc->name);
4723 goto out;
4725 for (j = 0; j < ioc->chains_per_prp_buffer; j++) {
4726 ct = &ioc->chain_lookup[i].chains_per_smid[j];
4727 ct->chain_buffer =
4728 ioc->pcie_sg_lookup[i].pcie_sgl +
4729 (j * ioc->chain_segment_sz);
4730 ct->chain_buffer_dma =
4731 ioc->pcie_sg_lookup[i].pcie_sgl_dma +
4732 (j * ioc->chain_segment_sz);
4736 dinitprintk(ioc, pr_info(MPT3SAS_FMT "PCIe sgl pool depth(%d), "
4737 "element_size(%d), pool_size(%d kB)\n", ioc->name,
4738 ioc->scsiio_depth, sz, (sz * ioc->scsiio_depth)/1024));
4739 dinitprintk(ioc, pr_info(MPT3SAS_FMT "Number of chains can "
4740 "fit in a PRP page(%d)\n", ioc->name,
4741 ioc->chains_per_prp_buffer));
4742 total_sz += sz * ioc->scsiio_depth;
4745 ioc->chain_dma_pool = dma_pool_create("chain pool", &ioc->pdev->dev,
4746 ioc->chain_segment_sz, 16, 0);
4747 if (!ioc->chain_dma_pool) {
4748 pr_err(MPT3SAS_FMT "chain_dma_pool: dma_pool_create failed\n",
4749 ioc->name);
4750 goto out;
4752 for (i = 0; i < ioc->scsiio_depth; i++) {
4753 for (j = ioc->chains_per_prp_buffer;
4754 j < ioc->chains_needed_per_io; j++) {
4755 ct = &ioc->chain_lookup[i].chains_per_smid[j];
4756 ct->chain_buffer = dma_pool_alloc(
4757 ioc->chain_dma_pool, GFP_KERNEL,
4758 &ct->chain_buffer_dma);
4759 if (!ct->chain_buffer) {
4760 pr_err(MPT3SAS_FMT "chain_lookup: "
4761 " pci_pool_alloc failed\n", ioc->name);
4762 _base_release_memory_pools(ioc);
4763 goto out;
4766 total_sz += ioc->chain_segment_sz;
4769 dinitprintk(ioc, pr_info(MPT3SAS_FMT
4770 "chain pool depth(%d), frame_size(%d), pool_size(%d kB)\n",
4771 ioc->name, ioc->chain_depth, ioc->chain_segment_sz,
4772 ((ioc->chain_depth * ioc->chain_segment_sz))/1024));
4774 /* sense buffers, 4 byte align */
4775 sz = ioc->scsiio_depth * SCSI_SENSE_BUFFERSIZE;
4776 ioc->sense_dma_pool = dma_pool_create("sense pool", &ioc->pdev->dev, sz,
4777 4, 0);
4778 if (!ioc->sense_dma_pool) {
4779 pr_err(MPT3SAS_FMT "sense pool: dma_pool_create failed\n",
4780 ioc->name);
4781 goto out;
4783 ioc->sense = dma_pool_alloc(ioc->sense_dma_pool, GFP_KERNEL,
4784 &ioc->sense_dma);
4785 if (!ioc->sense) {
4786 pr_err(MPT3SAS_FMT "sense pool: dma_pool_alloc failed\n",
4787 ioc->name);
4788 goto out;
4790 /* sense buffer requires to be in same 4 gb region.
4791 * Below function will check the same.
4792 * In case of failure, new pci pool will be created with updated
4793 * alignment. Older allocation and pool will be destroyed.
4794 * Alignment will be used such a way that next allocation if
4795 * success, will always meet same 4gb region requirement.
4796 * Actual requirement is not alignment, but we need start and end of
4797 * DMA address must have same upper 32 bit address.
4799 if (!is_MSB_are_same((long)ioc->sense, sz)) {
4800 //Release Sense pool & Reallocate
4801 dma_pool_free(ioc->sense_dma_pool, ioc->sense, ioc->sense_dma);
4802 dma_pool_destroy(ioc->sense_dma_pool);
4803 ioc->sense = NULL;
4805 ioc->sense_dma_pool =
4806 dma_pool_create("sense pool", &ioc->pdev->dev, sz,
4807 roundup_pow_of_two(sz), 0);
4808 if (!ioc->sense_dma_pool) {
4809 pr_err(MPT3SAS_FMT "sense pool: pci_pool_create failed\n",
4810 ioc->name);
4811 goto out;
4813 ioc->sense = dma_pool_alloc(ioc->sense_dma_pool, GFP_KERNEL,
4814 &ioc->sense_dma);
4815 if (!ioc->sense) {
4816 pr_err(MPT3SAS_FMT "sense pool: pci_pool_alloc failed\n",
4817 ioc->name);
4818 goto out;
4821 dinitprintk(ioc, pr_info(MPT3SAS_FMT
4822 "sense pool(0x%p): depth(%d), element_size(%d), pool_size"
4823 "(%d kB)\n", ioc->name, ioc->sense, ioc->scsiio_depth,
4824 SCSI_SENSE_BUFFERSIZE, sz/1024));
4825 dinitprintk(ioc, pr_info(MPT3SAS_FMT "sense_dma(0x%llx)\n",
4826 ioc->name, (unsigned long long)ioc->sense_dma));
4827 total_sz += sz;
4829 /* reply pool, 4 byte align */
4830 sz = ioc->reply_free_queue_depth * ioc->reply_sz;
4831 ioc->reply_dma_pool = dma_pool_create("reply pool", &ioc->pdev->dev, sz,
4832 4, 0);
4833 if (!ioc->reply_dma_pool) {
4834 pr_err(MPT3SAS_FMT "reply pool: dma_pool_create failed\n",
4835 ioc->name);
4836 goto out;
4838 ioc->reply = dma_pool_alloc(ioc->reply_dma_pool, GFP_KERNEL,
4839 &ioc->reply_dma);
4840 if (!ioc->reply) {
4841 pr_err(MPT3SAS_FMT "reply pool: dma_pool_alloc failed\n",
4842 ioc->name);
4843 goto out;
4845 ioc->reply_dma_min_address = (u32)(ioc->reply_dma);
4846 ioc->reply_dma_max_address = (u32)(ioc->reply_dma) + sz;
4847 dinitprintk(ioc, pr_info(MPT3SAS_FMT
4848 "reply pool(0x%p): depth(%d), frame_size(%d), pool_size(%d kB)\n",
4849 ioc->name, ioc->reply,
4850 ioc->reply_free_queue_depth, ioc->reply_sz, sz/1024));
4851 dinitprintk(ioc, pr_info(MPT3SAS_FMT "reply_dma(0x%llx)\n",
4852 ioc->name, (unsigned long long)ioc->reply_dma));
4853 total_sz += sz;
4855 /* reply free queue, 16 byte align */
4856 sz = ioc->reply_free_queue_depth * 4;
4857 ioc->reply_free_dma_pool = dma_pool_create("reply_free pool",
4858 &ioc->pdev->dev, sz, 16, 0);
4859 if (!ioc->reply_free_dma_pool) {
4860 pr_err(MPT3SAS_FMT "reply_free pool: dma_pool_create failed\n",
4861 ioc->name);
4862 goto out;
4864 ioc->reply_free = dma_pool_alloc(ioc->reply_free_dma_pool, GFP_KERNEL,
4865 &ioc->reply_free_dma);
4866 if (!ioc->reply_free) {
4867 pr_err(MPT3SAS_FMT "reply_free pool: dma_pool_alloc failed\n",
4868 ioc->name);
4869 goto out;
4871 memset(ioc->reply_free, 0, sz);
4872 dinitprintk(ioc, pr_info(MPT3SAS_FMT "reply_free pool(0x%p): " \
4873 "depth(%d), element_size(%d), pool_size(%d kB)\n", ioc->name,
4874 ioc->reply_free, ioc->reply_free_queue_depth, 4, sz/1024));
4875 dinitprintk(ioc, pr_info(MPT3SAS_FMT
4876 "reply_free_dma (0x%llx)\n",
4877 ioc->name, (unsigned long long)ioc->reply_free_dma));
4878 total_sz += sz;
4880 if (ioc->rdpq_array_enable) {
4881 reply_post_free_array_sz = ioc->reply_queue_count *
4882 sizeof(Mpi2IOCInitRDPQArrayEntry);
4883 ioc->reply_post_free_array_dma_pool =
4884 dma_pool_create("reply_post_free_array pool",
4885 &ioc->pdev->dev, reply_post_free_array_sz, 16, 0);
4886 if (!ioc->reply_post_free_array_dma_pool) {
4887 dinitprintk(ioc,
4888 pr_info(MPT3SAS_FMT "reply_post_free_array pool: "
4889 "dma_pool_create failed\n", ioc->name));
4890 goto out;
4892 ioc->reply_post_free_array =
4893 dma_pool_alloc(ioc->reply_post_free_array_dma_pool,
4894 GFP_KERNEL, &ioc->reply_post_free_array_dma);
4895 if (!ioc->reply_post_free_array) {
4896 dinitprintk(ioc,
4897 pr_info(MPT3SAS_FMT "reply_post_free_array pool: "
4898 "dma_pool_alloc failed\n", ioc->name));
4899 goto out;
4902 ioc->config_page_sz = 512;
4903 ioc->config_page = pci_alloc_consistent(ioc->pdev,
4904 ioc->config_page_sz, &ioc->config_page_dma);
4905 if (!ioc->config_page) {
4906 pr_err(MPT3SAS_FMT
4907 "config page: dma_pool_alloc failed\n",
4908 ioc->name);
4909 goto out;
4911 dinitprintk(ioc, pr_info(MPT3SAS_FMT
4912 "config page(0x%p): size(%d)\n",
4913 ioc->name, ioc->config_page, ioc->config_page_sz));
4914 dinitprintk(ioc, pr_info(MPT3SAS_FMT "config_page_dma(0x%llx)\n",
4915 ioc->name, (unsigned long long)ioc->config_page_dma));
4916 total_sz += ioc->config_page_sz;
4918 pr_info(MPT3SAS_FMT "Allocated physical memory: size(%d kB)\n",
4919 ioc->name, total_sz/1024);
4920 pr_info(MPT3SAS_FMT
4921 "Current Controller Queue Depth(%d),Max Controller Queue Depth(%d)\n",
4922 ioc->name, ioc->shost->can_queue, facts->RequestCredit);
4923 pr_info(MPT3SAS_FMT "Scatter Gather Elements per IO(%d)\n",
4924 ioc->name, ioc->shost->sg_tablesize);
4925 return 0;
4927 out:
4928 return -ENOMEM;
4932 * mpt3sas_base_get_iocstate - Get the current state of a MPT adapter.
4933 * @ioc: Pointer to MPT_ADAPTER structure
4934 * @cooked: Request raw or cooked IOC state
4936 * Return: all IOC Doorbell register bits if cooked==0, else just the
4937 * Doorbell bits in MPI_IOC_STATE_MASK.
4940 mpt3sas_base_get_iocstate(struct MPT3SAS_ADAPTER *ioc, int cooked)
4942 u32 s, sc;
4944 s = readl(&ioc->chip->Doorbell);
4945 sc = s & MPI2_IOC_STATE_MASK;
4946 return cooked ? sc : s;
4950 * _base_wait_on_iocstate - waiting on a particular ioc state
4951 * @ioc: ?
4952 * @ioc_state: controller state { READY, OPERATIONAL, or RESET }
4953 * @timeout: timeout in second
4955 * Return: 0 for success, non-zero for failure.
4957 static int
4958 _base_wait_on_iocstate(struct MPT3SAS_ADAPTER *ioc, u32 ioc_state, int timeout)
4960 u32 count, cntdn;
4961 u32 current_state;
4963 count = 0;
4964 cntdn = 1000 * timeout;
4965 do {
4966 current_state = mpt3sas_base_get_iocstate(ioc, 1);
4967 if (current_state == ioc_state)
4968 return 0;
4969 if (count && current_state == MPI2_IOC_STATE_FAULT)
4970 break;
4972 usleep_range(1000, 1500);
4973 count++;
4974 } while (--cntdn);
4976 return current_state;
4980 * _base_wait_for_doorbell_int - waiting for controller interrupt(generated by
4981 * a write to the doorbell)
4982 * @ioc: per adapter object
4984 * Return: 0 for success, non-zero for failure.
4986 * Notes: MPI2_HIS_IOC2SYS_DB_STATUS - set to one when IOC writes to doorbell.
4988 static int
4989 _base_diag_reset(struct MPT3SAS_ADAPTER *ioc);
4991 static int
4992 _base_wait_for_doorbell_int(struct MPT3SAS_ADAPTER *ioc, int timeout)
4994 u32 cntdn, count;
4995 u32 int_status;
4997 count = 0;
4998 cntdn = 1000 * timeout;
4999 do {
5000 int_status = readl(&ioc->chip->HostInterruptStatus);
5001 if (int_status & MPI2_HIS_IOC2SYS_DB_STATUS) {
5002 dhsprintk(ioc, pr_info(MPT3SAS_FMT
5003 "%s: successful count(%d), timeout(%d)\n",
5004 ioc->name, __func__, count, timeout));
5005 return 0;
5008 usleep_range(1000, 1500);
5009 count++;
5010 } while (--cntdn);
5012 pr_err(MPT3SAS_FMT
5013 "%s: failed due to timeout count(%d), int_status(%x)!\n",
5014 ioc->name, __func__, count, int_status);
5015 return -EFAULT;
5018 static int
5019 _base_spin_on_doorbell_int(struct MPT3SAS_ADAPTER *ioc, int timeout)
5021 u32 cntdn, count;
5022 u32 int_status;
5024 count = 0;
5025 cntdn = 2000 * timeout;
5026 do {
5027 int_status = readl(&ioc->chip->HostInterruptStatus);
5028 if (int_status & MPI2_HIS_IOC2SYS_DB_STATUS) {
5029 dhsprintk(ioc, pr_info(MPT3SAS_FMT
5030 "%s: successful count(%d), timeout(%d)\n",
5031 ioc->name, __func__, count, timeout));
5032 return 0;
5035 udelay(500);
5036 count++;
5037 } while (--cntdn);
5039 pr_err(MPT3SAS_FMT
5040 "%s: failed due to timeout count(%d), int_status(%x)!\n",
5041 ioc->name, __func__, count, int_status);
5042 return -EFAULT;
5047 * _base_wait_for_doorbell_ack - waiting for controller to read the doorbell.
5048 * @ioc: per adapter object
5049 * @timeout: timeout in second
5051 * Return: 0 for success, non-zero for failure.
5053 * Notes: MPI2_HIS_SYS2IOC_DB_STATUS - set to one when host writes to
5054 * doorbell.
5056 static int
5057 _base_wait_for_doorbell_ack(struct MPT3SAS_ADAPTER *ioc, int timeout)
5059 u32 cntdn, count;
5060 u32 int_status;
5061 u32 doorbell;
5063 count = 0;
5064 cntdn = 1000 * timeout;
5065 do {
5066 int_status = readl(&ioc->chip->HostInterruptStatus);
5067 if (!(int_status & MPI2_HIS_SYS2IOC_DB_STATUS)) {
5068 dhsprintk(ioc, pr_info(MPT3SAS_FMT
5069 "%s: successful count(%d), timeout(%d)\n",
5070 ioc->name, __func__, count, timeout));
5071 return 0;
5072 } else if (int_status & MPI2_HIS_IOC2SYS_DB_STATUS) {
5073 doorbell = readl(&ioc->chip->Doorbell);
5074 if ((doorbell & MPI2_IOC_STATE_MASK) ==
5075 MPI2_IOC_STATE_FAULT) {
5076 mpt3sas_base_fault_info(ioc , doorbell);
5077 return -EFAULT;
5079 } else if (int_status == 0xFFFFFFFF)
5080 goto out;
5082 usleep_range(1000, 1500);
5083 count++;
5084 } while (--cntdn);
5086 out:
5087 pr_err(MPT3SAS_FMT
5088 "%s: failed due to timeout count(%d), int_status(%x)!\n",
5089 ioc->name, __func__, count, int_status);
5090 return -EFAULT;
5094 * _base_wait_for_doorbell_not_used - waiting for doorbell to not be in use
5095 * @ioc: per adapter object
5096 * @timeout: timeout in second
5098 * Return: 0 for success, non-zero for failure.
5100 static int
5101 _base_wait_for_doorbell_not_used(struct MPT3SAS_ADAPTER *ioc, int timeout)
5103 u32 cntdn, count;
5104 u32 doorbell_reg;
5106 count = 0;
5107 cntdn = 1000 * timeout;
5108 do {
5109 doorbell_reg = readl(&ioc->chip->Doorbell);
5110 if (!(doorbell_reg & MPI2_DOORBELL_USED)) {
5111 dhsprintk(ioc, pr_info(MPT3SAS_FMT
5112 "%s: successful count(%d), timeout(%d)\n",
5113 ioc->name, __func__, count, timeout));
5114 return 0;
5117 usleep_range(1000, 1500);
5118 count++;
5119 } while (--cntdn);
5121 pr_err(MPT3SAS_FMT
5122 "%s: failed due to timeout count(%d), doorbell_reg(%x)!\n",
5123 ioc->name, __func__, count, doorbell_reg);
5124 return -EFAULT;
5128 * _base_send_ioc_reset - send doorbell reset
5129 * @ioc: per adapter object
5130 * @reset_type: currently only supports: MPI2_FUNCTION_IOC_MESSAGE_UNIT_RESET
5131 * @timeout: timeout in second
5133 * Return: 0 for success, non-zero for failure.
5135 static int
5136 _base_send_ioc_reset(struct MPT3SAS_ADAPTER *ioc, u8 reset_type, int timeout)
5138 u32 ioc_state;
5139 int r = 0;
5141 if (reset_type != MPI2_FUNCTION_IOC_MESSAGE_UNIT_RESET) {
5142 pr_err(MPT3SAS_FMT "%s: unknown reset_type\n",
5143 ioc->name, __func__);
5144 return -EFAULT;
5147 if (!(ioc->facts.IOCCapabilities &
5148 MPI2_IOCFACTS_CAPABILITY_EVENT_REPLAY))
5149 return -EFAULT;
5151 pr_info(MPT3SAS_FMT "sending message unit reset !!\n", ioc->name);
5153 writel(reset_type << MPI2_DOORBELL_FUNCTION_SHIFT,
5154 &ioc->chip->Doorbell);
5155 if ((_base_wait_for_doorbell_ack(ioc, 15))) {
5156 r = -EFAULT;
5157 goto out;
5159 ioc_state = _base_wait_on_iocstate(ioc, MPI2_IOC_STATE_READY, timeout);
5160 if (ioc_state) {
5161 pr_err(MPT3SAS_FMT
5162 "%s: failed going to ready state (ioc_state=0x%x)\n",
5163 ioc->name, __func__, ioc_state);
5164 r = -EFAULT;
5165 goto out;
5167 out:
5168 pr_info(MPT3SAS_FMT "message unit reset: %s\n",
5169 ioc->name, ((r == 0) ? "SUCCESS" : "FAILED"));
5170 return r;
5174 * _base_handshake_req_reply_wait - send request thru doorbell interface
5175 * @ioc: per adapter object
5176 * @request_bytes: request length
5177 * @request: pointer having request payload
5178 * @reply_bytes: reply length
5179 * @reply: pointer to reply payload
5180 * @timeout: timeout in second
5182 * Return: 0 for success, non-zero for failure.
5184 static int
5185 _base_handshake_req_reply_wait(struct MPT3SAS_ADAPTER *ioc, int request_bytes,
5186 u32 *request, int reply_bytes, u16 *reply, int timeout)
5188 MPI2DefaultReply_t *default_reply = (MPI2DefaultReply_t *)reply;
5189 int i;
5190 u8 failed;
5191 __le32 *mfp;
5193 /* make sure doorbell is not in use */
5194 if ((readl(&ioc->chip->Doorbell) & MPI2_DOORBELL_USED)) {
5195 pr_err(MPT3SAS_FMT
5196 "doorbell is in use (line=%d)\n",
5197 ioc->name, __LINE__);
5198 return -EFAULT;
5201 /* clear pending doorbell interrupts from previous state changes */
5202 if (readl(&ioc->chip->HostInterruptStatus) &
5203 MPI2_HIS_IOC2SYS_DB_STATUS)
5204 writel(0, &ioc->chip->HostInterruptStatus);
5206 /* send message to ioc */
5207 writel(((MPI2_FUNCTION_HANDSHAKE<<MPI2_DOORBELL_FUNCTION_SHIFT) |
5208 ((request_bytes/4)<<MPI2_DOORBELL_ADD_DWORDS_SHIFT)),
5209 &ioc->chip->Doorbell);
5211 if ((_base_spin_on_doorbell_int(ioc, 5))) {
5212 pr_err(MPT3SAS_FMT
5213 "doorbell handshake int failed (line=%d)\n",
5214 ioc->name, __LINE__);
5215 return -EFAULT;
5217 writel(0, &ioc->chip->HostInterruptStatus);
5219 if ((_base_wait_for_doorbell_ack(ioc, 5))) {
5220 pr_err(MPT3SAS_FMT
5221 "doorbell handshake ack failed (line=%d)\n",
5222 ioc->name, __LINE__);
5223 return -EFAULT;
5226 /* send message 32-bits at a time */
5227 for (i = 0, failed = 0; i < request_bytes/4 && !failed; i++) {
5228 writel(cpu_to_le32(request[i]), &ioc->chip->Doorbell);
5229 if ((_base_wait_for_doorbell_ack(ioc, 5)))
5230 failed = 1;
5233 if (failed) {
5234 pr_err(MPT3SAS_FMT
5235 "doorbell handshake sending request failed (line=%d)\n",
5236 ioc->name, __LINE__);
5237 return -EFAULT;
5240 /* now wait for the reply */
5241 if ((_base_wait_for_doorbell_int(ioc, timeout))) {
5242 pr_err(MPT3SAS_FMT
5243 "doorbell handshake int failed (line=%d)\n",
5244 ioc->name, __LINE__);
5245 return -EFAULT;
5248 /* read the first two 16-bits, it gives the total length of the reply */
5249 reply[0] = le16_to_cpu(readl(&ioc->chip->Doorbell)
5250 & MPI2_DOORBELL_DATA_MASK);
5251 writel(0, &ioc->chip->HostInterruptStatus);
5252 if ((_base_wait_for_doorbell_int(ioc, 5))) {
5253 pr_err(MPT3SAS_FMT
5254 "doorbell handshake int failed (line=%d)\n",
5255 ioc->name, __LINE__);
5256 return -EFAULT;
5258 reply[1] = le16_to_cpu(readl(&ioc->chip->Doorbell)
5259 & MPI2_DOORBELL_DATA_MASK);
5260 writel(0, &ioc->chip->HostInterruptStatus);
5262 for (i = 2; i < default_reply->MsgLength * 2; i++) {
5263 if ((_base_wait_for_doorbell_int(ioc, 5))) {
5264 pr_err(MPT3SAS_FMT
5265 "doorbell handshake int failed (line=%d)\n",
5266 ioc->name, __LINE__);
5267 return -EFAULT;
5269 if (i >= reply_bytes/2) /* overflow case */
5270 readl(&ioc->chip->Doorbell);
5271 else
5272 reply[i] = le16_to_cpu(readl(&ioc->chip->Doorbell)
5273 & MPI2_DOORBELL_DATA_MASK);
5274 writel(0, &ioc->chip->HostInterruptStatus);
5277 _base_wait_for_doorbell_int(ioc, 5);
5278 if (_base_wait_for_doorbell_not_used(ioc, 5) != 0) {
5279 dhsprintk(ioc, pr_info(MPT3SAS_FMT
5280 "doorbell is in use (line=%d)\n", ioc->name, __LINE__));
5282 writel(0, &ioc->chip->HostInterruptStatus);
5284 if (ioc->logging_level & MPT_DEBUG_INIT) {
5285 mfp = (__le32 *)reply;
5286 pr_info("\toffset:data\n");
5287 for (i = 0; i < reply_bytes/4; i++)
5288 pr_info("\t[0x%02x]:%08x\n", i*4,
5289 le32_to_cpu(mfp[i]));
5291 return 0;
5295 * mpt3sas_base_sas_iounit_control - send sas iounit control to FW
5296 * @ioc: per adapter object
5297 * @mpi_reply: the reply payload from FW
5298 * @mpi_request: the request payload sent to FW
5300 * The SAS IO Unit Control Request message allows the host to perform low-level
5301 * operations, such as resets on the PHYs of the IO Unit, also allows the host
5302 * to obtain the IOC assigned device handles for a device if it has other
5303 * identifying information about the device, in addition allows the host to
5304 * remove IOC resources associated with the device.
5306 * Return: 0 for success, non-zero for failure.
5309 mpt3sas_base_sas_iounit_control(struct MPT3SAS_ADAPTER *ioc,
5310 Mpi2SasIoUnitControlReply_t *mpi_reply,
5311 Mpi2SasIoUnitControlRequest_t *mpi_request)
5313 u16 smid;
5314 u32 ioc_state;
5315 u8 issue_reset = 0;
5316 int rc;
5317 void *request;
5318 u16 wait_state_count;
5320 dinitprintk(ioc, pr_info(MPT3SAS_FMT "%s\n", ioc->name,
5321 __func__));
5323 mutex_lock(&ioc->base_cmds.mutex);
5325 if (ioc->base_cmds.status != MPT3_CMD_NOT_USED) {
5326 pr_err(MPT3SAS_FMT "%s: base_cmd in use\n",
5327 ioc->name, __func__);
5328 rc = -EAGAIN;
5329 goto out;
5332 wait_state_count = 0;
5333 ioc_state = mpt3sas_base_get_iocstate(ioc, 1);
5334 while (ioc_state != MPI2_IOC_STATE_OPERATIONAL) {
5335 if (wait_state_count++ == 10) {
5336 pr_err(MPT3SAS_FMT
5337 "%s: failed due to ioc not operational\n",
5338 ioc->name, __func__);
5339 rc = -EFAULT;
5340 goto out;
5342 ssleep(1);
5343 ioc_state = mpt3sas_base_get_iocstate(ioc, 1);
5344 pr_info(MPT3SAS_FMT
5345 "%s: waiting for operational state(count=%d)\n",
5346 ioc->name, __func__, wait_state_count);
5349 smid = mpt3sas_base_get_smid(ioc, ioc->base_cb_idx);
5350 if (!smid) {
5351 pr_err(MPT3SAS_FMT "%s: failed obtaining a smid\n",
5352 ioc->name, __func__);
5353 rc = -EAGAIN;
5354 goto out;
5357 rc = 0;
5358 ioc->base_cmds.status = MPT3_CMD_PENDING;
5359 request = mpt3sas_base_get_msg_frame(ioc, smid);
5360 ioc->base_cmds.smid = smid;
5361 memcpy(request, mpi_request, sizeof(Mpi2SasIoUnitControlRequest_t));
5362 if (mpi_request->Operation == MPI2_SAS_OP_PHY_HARD_RESET ||
5363 mpi_request->Operation == MPI2_SAS_OP_PHY_LINK_RESET)
5364 ioc->ioc_link_reset_in_progress = 1;
5365 init_completion(&ioc->base_cmds.done);
5366 mpt3sas_base_put_smid_default(ioc, smid);
5367 wait_for_completion_timeout(&ioc->base_cmds.done,
5368 msecs_to_jiffies(10000));
5369 if ((mpi_request->Operation == MPI2_SAS_OP_PHY_HARD_RESET ||
5370 mpi_request->Operation == MPI2_SAS_OP_PHY_LINK_RESET) &&
5371 ioc->ioc_link_reset_in_progress)
5372 ioc->ioc_link_reset_in_progress = 0;
5373 if (!(ioc->base_cmds.status & MPT3_CMD_COMPLETE)) {
5374 issue_reset =
5375 mpt3sas_base_check_cmd_timeout(ioc,
5376 ioc->base_cmds.status, mpi_request,
5377 sizeof(Mpi2SasIoUnitControlRequest_t)/4);
5378 goto issue_host_reset;
5380 if (ioc->base_cmds.status & MPT3_CMD_REPLY_VALID)
5381 memcpy(mpi_reply, ioc->base_cmds.reply,
5382 sizeof(Mpi2SasIoUnitControlReply_t));
5383 else
5384 memset(mpi_reply, 0, sizeof(Mpi2SasIoUnitControlReply_t));
5385 ioc->base_cmds.status = MPT3_CMD_NOT_USED;
5386 goto out;
5388 issue_host_reset:
5389 if (issue_reset)
5390 mpt3sas_base_hard_reset_handler(ioc, FORCE_BIG_HAMMER);
5391 ioc->base_cmds.status = MPT3_CMD_NOT_USED;
5392 rc = -EFAULT;
5393 out:
5394 mutex_unlock(&ioc->base_cmds.mutex);
5395 return rc;
5399 * mpt3sas_base_scsi_enclosure_processor - sending request to sep device
5400 * @ioc: per adapter object
5401 * @mpi_reply: the reply payload from FW
5402 * @mpi_request: the request payload sent to FW
5404 * The SCSI Enclosure Processor request message causes the IOC to
5405 * communicate with SES devices to control LED status signals.
5407 * Return: 0 for success, non-zero for failure.
5410 mpt3sas_base_scsi_enclosure_processor(struct MPT3SAS_ADAPTER *ioc,
5411 Mpi2SepReply_t *mpi_reply, Mpi2SepRequest_t *mpi_request)
5413 u16 smid;
5414 u32 ioc_state;
5415 u8 issue_reset = 0;
5416 int rc;
5417 void *request;
5418 u16 wait_state_count;
5420 dinitprintk(ioc, pr_info(MPT3SAS_FMT "%s\n", ioc->name,
5421 __func__));
5423 mutex_lock(&ioc->base_cmds.mutex);
5425 if (ioc->base_cmds.status != MPT3_CMD_NOT_USED) {
5426 pr_err(MPT3SAS_FMT "%s: base_cmd in use\n",
5427 ioc->name, __func__);
5428 rc = -EAGAIN;
5429 goto out;
5432 wait_state_count = 0;
5433 ioc_state = mpt3sas_base_get_iocstate(ioc, 1);
5434 while (ioc_state != MPI2_IOC_STATE_OPERATIONAL) {
5435 if (wait_state_count++ == 10) {
5436 pr_err(MPT3SAS_FMT
5437 "%s: failed due to ioc not operational\n",
5438 ioc->name, __func__);
5439 rc = -EFAULT;
5440 goto out;
5442 ssleep(1);
5443 ioc_state = mpt3sas_base_get_iocstate(ioc, 1);
5444 pr_info(MPT3SAS_FMT
5445 "%s: waiting for operational state(count=%d)\n",
5446 ioc->name,
5447 __func__, wait_state_count);
5450 smid = mpt3sas_base_get_smid(ioc, ioc->base_cb_idx);
5451 if (!smid) {
5452 pr_err(MPT3SAS_FMT "%s: failed obtaining a smid\n",
5453 ioc->name, __func__);
5454 rc = -EAGAIN;
5455 goto out;
5458 rc = 0;
5459 ioc->base_cmds.status = MPT3_CMD_PENDING;
5460 request = mpt3sas_base_get_msg_frame(ioc, smid);
5461 ioc->base_cmds.smid = smid;
5462 memcpy(request, mpi_request, sizeof(Mpi2SepReply_t));
5463 init_completion(&ioc->base_cmds.done);
5464 mpt3sas_base_put_smid_default(ioc, smid);
5465 wait_for_completion_timeout(&ioc->base_cmds.done,
5466 msecs_to_jiffies(10000));
5467 if (!(ioc->base_cmds.status & MPT3_CMD_COMPLETE)) {
5468 issue_reset =
5469 mpt3sas_base_check_cmd_timeout(ioc,
5470 ioc->base_cmds.status, mpi_request,
5471 sizeof(Mpi2SepRequest_t)/4);
5472 goto issue_host_reset;
5474 if (ioc->base_cmds.status & MPT3_CMD_REPLY_VALID)
5475 memcpy(mpi_reply, ioc->base_cmds.reply,
5476 sizeof(Mpi2SepReply_t));
5477 else
5478 memset(mpi_reply, 0, sizeof(Mpi2SepReply_t));
5479 ioc->base_cmds.status = MPT3_CMD_NOT_USED;
5480 goto out;
5482 issue_host_reset:
5483 if (issue_reset)
5484 mpt3sas_base_hard_reset_handler(ioc, FORCE_BIG_HAMMER);
5485 ioc->base_cmds.status = MPT3_CMD_NOT_USED;
5486 rc = -EFAULT;
5487 out:
5488 mutex_unlock(&ioc->base_cmds.mutex);
5489 return rc;
5493 * _base_get_port_facts - obtain port facts reply and save in ioc
5494 * @ioc: per adapter object
5495 * @port: ?
5497 * Return: 0 for success, non-zero for failure.
5499 static int
5500 _base_get_port_facts(struct MPT3SAS_ADAPTER *ioc, int port)
5502 Mpi2PortFactsRequest_t mpi_request;
5503 Mpi2PortFactsReply_t mpi_reply;
5504 struct mpt3sas_port_facts *pfacts;
5505 int mpi_reply_sz, mpi_request_sz, r;
5507 dinitprintk(ioc, pr_info(MPT3SAS_FMT "%s\n", ioc->name,
5508 __func__));
5510 mpi_reply_sz = sizeof(Mpi2PortFactsReply_t);
5511 mpi_request_sz = sizeof(Mpi2PortFactsRequest_t);
5512 memset(&mpi_request, 0, mpi_request_sz);
5513 mpi_request.Function = MPI2_FUNCTION_PORT_FACTS;
5514 mpi_request.PortNumber = port;
5515 r = _base_handshake_req_reply_wait(ioc, mpi_request_sz,
5516 (u32 *)&mpi_request, mpi_reply_sz, (u16 *)&mpi_reply, 5);
5518 if (r != 0) {
5519 pr_err(MPT3SAS_FMT "%s: handshake failed (r=%d)\n",
5520 ioc->name, __func__, r);
5521 return r;
5524 pfacts = &ioc->pfacts[port];
5525 memset(pfacts, 0, sizeof(struct mpt3sas_port_facts));
5526 pfacts->PortNumber = mpi_reply.PortNumber;
5527 pfacts->VP_ID = mpi_reply.VP_ID;
5528 pfacts->VF_ID = mpi_reply.VF_ID;
5529 pfacts->MaxPostedCmdBuffers =
5530 le16_to_cpu(mpi_reply.MaxPostedCmdBuffers);
5532 return 0;
5536 * _base_wait_for_iocstate - Wait until the card is in READY or OPERATIONAL
5537 * @ioc: per adapter object
5538 * @timeout:
5540 * Return: 0 for success, non-zero for failure.
5542 static int
5543 _base_wait_for_iocstate(struct MPT3SAS_ADAPTER *ioc, int timeout)
5545 u32 ioc_state;
5546 int rc;
5548 dinitprintk(ioc, printk(MPT3SAS_FMT "%s\n", ioc->name,
5549 __func__));
5551 if (ioc->pci_error_recovery) {
5552 dfailprintk(ioc, printk(MPT3SAS_FMT
5553 "%s: host in pci error recovery\n", ioc->name, __func__));
5554 return -EFAULT;
5557 ioc_state = mpt3sas_base_get_iocstate(ioc, 0);
5558 dhsprintk(ioc, printk(MPT3SAS_FMT "%s: ioc_state(0x%08x)\n",
5559 ioc->name, __func__, ioc_state));
5561 if (((ioc_state & MPI2_IOC_STATE_MASK) == MPI2_IOC_STATE_READY) ||
5562 (ioc_state & MPI2_IOC_STATE_MASK) == MPI2_IOC_STATE_OPERATIONAL)
5563 return 0;
5565 if (ioc_state & MPI2_DOORBELL_USED) {
5566 dhsprintk(ioc, printk(MPT3SAS_FMT
5567 "unexpected doorbell active!\n", ioc->name));
5568 goto issue_diag_reset;
5571 if ((ioc_state & MPI2_IOC_STATE_MASK) == MPI2_IOC_STATE_FAULT) {
5572 mpt3sas_base_fault_info(ioc, ioc_state &
5573 MPI2_DOORBELL_DATA_MASK);
5574 goto issue_diag_reset;
5577 ioc_state = _base_wait_on_iocstate(ioc, MPI2_IOC_STATE_READY, timeout);
5578 if (ioc_state) {
5579 dfailprintk(ioc, printk(MPT3SAS_FMT
5580 "%s: failed going to ready state (ioc_state=0x%x)\n",
5581 ioc->name, __func__, ioc_state));
5582 return -EFAULT;
5585 issue_diag_reset:
5586 rc = _base_diag_reset(ioc);
5587 return rc;
5591 * _base_get_ioc_facts - obtain ioc facts reply and save in ioc
5592 * @ioc: per adapter object
5594 * Return: 0 for success, non-zero for failure.
5596 static int
5597 _base_get_ioc_facts(struct MPT3SAS_ADAPTER *ioc)
5599 Mpi2IOCFactsRequest_t mpi_request;
5600 Mpi2IOCFactsReply_t mpi_reply;
5601 struct mpt3sas_facts *facts;
5602 int mpi_reply_sz, mpi_request_sz, r;
5604 dinitprintk(ioc, pr_info(MPT3SAS_FMT "%s\n", ioc->name,
5605 __func__));
5607 r = _base_wait_for_iocstate(ioc, 10);
5608 if (r) {
5609 dfailprintk(ioc, printk(MPT3SAS_FMT
5610 "%s: failed getting to correct state\n",
5611 ioc->name, __func__));
5612 return r;
5614 mpi_reply_sz = sizeof(Mpi2IOCFactsReply_t);
5615 mpi_request_sz = sizeof(Mpi2IOCFactsRequest_t);
5616 memset(&mpi_request, 0, mpi_request_sz);
5617 mpi_request.Function = MPI2_FUNCTION_IOC_FACTS;
5618 r = _base_handshake_req_reply_wait(ioc, mpi_request_sz,
5619 (u32 *)&mpi_request, mpi_reply_sz, (u16 *)&mpi_reply, 5);
5621 if (r != 0) {
5622 pr_err(MPT3SAS_FMT "%s: handshake failed (r=%d)\n",
5623 ioc->name, __func__, r);
5624 return r;
5627 facts = &ioc->facts;
5628 memset(facts, 0, sizeof(struct mpt3sas_facts));
5629 facts->MsgVersion = le16_to_cpu(mpi_reply.MsgVersion);
5630 facts->HeaderVersion = le16_to_cpu(mpi_reply.HeaderVersion);
5631 facts->VP_ID = mpi_reply.VP_ID;
5632 facts->VF_ID = mpi_reply.VF_ID;
5633 facts->IOCExceptions = le16_to_cpu(mpi_reply.IOCExceptions);
5634 facts->MaxChainDepth = mpi_reply.MaxChainDepth;
5635 facts->WhoInit = mpi_reply.WhoInit;
5636 facts->NumberOfPorts = mpi_reply.NumberOfPorts;
5637 facts->MaxMSIxVectors = mpi_reply.MaxMSIxVectors;
5638 if (ioc->msix_enable && (facts->MaxMSIxVectors <=
5639 MAX_COMBINED_MSIX_VECTORS(ioc->is_gen35_ioc)))
5640 ioc->combined_reply_queue = 0;
5641 facts->RequestCredit = le16_to_cpu(mpi_reply.RequestCredit);
5642 facts->MaxReplyDescriptorPostQueueDepth =
5643 le16_to_cpu(mpi_reply.MaxReplyDescriptorPostQueueDepth);
5644 facts->ProductID = le16_to_cpu(mpi_reply.ProductID);
5645 facts->IOCCapabilities = le32_to_cpu(mpi_reply.IOCCapabilities);
5646 if ((facts->IOCCapabilities & MPI2_IOCFACTS_CAPABILITY_INTEGRATED_RAID))
5647 ioc->ir_firmware = 1;
5648 if ((facts->IOCCapabilities &
5649 MPI2_IOCFACTS_CAPABILITY_RDPQ_ARRAY_CAPABLE) && (!reset_devices))
5650 ioc->rdpq_array_capable = 1;
5651 facts->FWVersion.Word = le32_to_cpu(mpi_reply.FWVersion.Word);
5652 facts->IOCRequestFrameSize =
5653 le16_to_cpu(mpi_reply.IOCRequestFrameSize);
5654 if (ioc->hba_mpi_version_belonged != MPI2_VERSION) {
5655 facts->IOCMaxChainSegmentSize =
5656 le16_to_cpu(mpi_reply.IOCMaxChainSegmentSize);
5658 facts->MaxInitiators = le16_to_cpu(mpi_reply.MaxInitiators);
5659 facts->MaxTargets = le16_to_cpu(mpi_reply.MaxTargets);
5660 ioc->shost->max_id = -1;
5661 facts->MaxSasExpanders = le16_to_cpu(mpi_reply.MaxSasExpanders);
5662 facts->MaxEnclosures = le16_to_cpu(mpi_reply.MaxEnclosures);
5663 facts->ProtocolFlags = le16_to_cpu(mpi_reply.ProtocolFlags);
5664 facts->HighPriorityCredit =
5665 le16_to_cpu(mpi_reply.HighPriorityCredit);
5666 facts->ReplyFrameSize = mpi_reply.ReplyFrameSize;
5667 facts->MaxDevHandle = le16_to_cpu(mpi_reply.MaxDevHandle);
5668 facts->CurrentHostPageSize = mpi_reply.CurrentHostPageSize;
5671 * Get the Page Size from IOC Facts. If it's 0, default to 4k.
5673 ioc->page_size = 1 << facts->CurrentHostPageSize;
5674 if (ioc->page_size == 1) {
5675 pr_info(MPT3SAS_FMT "CurrentHostPageSize is 0: Setting "
5676 "default host page size to 4k\n", ioc->name);
5677 ioc->page_size = 1 << MPT3SAS_HOST_PAGE_SIZE_4K;
5679 dinitprintk(ioc, pr_info(MPT3SAS_FMT "CurrentHostPageSize(%d)\n",
5680 ioc->name, facts->CurrentHostPageSize));
5682 dinitprintk(ioc, pr_info(MPT3SAS_FMT
5683 "hba queue depth(%d), max chains per io(%d)\n",
5684 ioc->name, facts->RequestCredit,
5685 facts->MaxChainDepth));
5686 dinitprintk(ioc, pr_info(MPT3SAS_FMT
5687 "request frame size(%d), reply frame size(%d)\n", ioc->name,
5688 facts->IOCRequestFrameSize * 4, facts->ReplyFrameSize * 4));
5689 return 0;
5693 * _base_send_ioc_init - send ioc_init to firmware
5694 * @ioc: per adapter object
5696 * Return: 0 for success, non-zero for failure.
5698 static int
5699 _base_send_ioc_init(struct MPT3SAS_ADAPTER *ioc)
5701 Mpi2IOCInitRequest_t mpi_request;
5702 Mpi2IOCInitReply_t mpi_reply;
5703 int i, r = 0;
5704 ktime_t current_time;
5705 u16 ioc_status;
5706 u32 reply_post_free_array_sz = 0;
5708 dinitprintk(ioc, pr_info(MPT3SAS_FMT "%s\n", ioc->name,
5709 __func__));
5711 memset(&mpi_request, 0, sizeof(Mpi2IOCInitRequest_t));
5712 mpi_request.Function = MPI2_FUNCTION_IOC_INIT;
5713 mpi_request.WhoInit = MPI2_WHOINIT_HOST_DRIVER;
5714 mpi_request.VF_ID = 0; /* TODO */
5715 mpi_request.VP_ID = 0;
5716 mpi_request.MsgVersion = cpu_to_le16(ioc->hba_mpi_version_belonged);
5717 mpi_request.HeaderVersion = cpu_to_le16(MPI2_HEADER_VERSION);
5718 mpi_request.HostPageSize = MPT3SAS_HOST_PAGE_SIZE_4K;
5720 if (_base_is_controller_msix_enabled(ioc))
5721 mpi_request.HostMSIxVectors = ioc->reply_queue_count;
5722 mpi_request.SystemRequestFrameSize = cpu_to_le16(ioc->request_sz/4);
5723 mpi_request.ReplyDescriptorPostQueueDepth =
5724 cpu_to_le16(ioc->reply_post_queue_depth);
5725 mpi_request.ReplyFreeQueueDepth =
5726 cpu_to_le16(ioc->reply_free_queue_depth);
5728 mpi_request.SenseBufferAddressHigh =
5729 cpu_to_le32((u64)ioc->sense_dma >> 32);
5730 mpi_request.SystemReplyAddressHigh =
5731 cpu_to_le32((u64)ioc->reply_dma >> 32);
5732 mpi_request.SystemRequestFrameBaseAddress =
5733 cpu_to_le64((u64)ioc->request_dma);
5734 mpi_request.ReplyFreeQueueAddress =
5735 cpu_to_le64((u64)ioc->reply_free_dma);
5737 if (ioc->rdpq_array_enable) {
5738 reply_post_free_array_sz = ioc->reply_queue_count *
5739 sizeof(Mpi2IOCInitRDPQArrayEntry);
5740 memset(ioc->reply_post_free_array, 0, reply_post_free_array_sz);
5741 for (i = 0; i < ioc->reply_queue_count; i++)
5742 ioc->reply_post_free_array[i].RDPQBaseAddress =
5743 cpu_to_le64(
5744 (u64)ioc->reply_post[i].reply_post_free_dma);
5745 mpi_request.MsgFlags = MPI2_IOCINIT_MSGFLAG_RDPQ_ARRAY_MODE;
5746 mpi_request.ReplyDescriptorPostQueueAddress =
5747 cpu_to_le64((u64)ioc->reply_post_free_array_dma);
5748 } else {
5749 mpi_request.ReplyDescriptorPostQueueAddress =
5750 cpu_to_le64((u64)ioc->reply_post[0].reply_post_free_dma);
5753 /* This time stamp specifies number of milliseconds
5754 * since epoch ~ midnight January 1, 1970.
5756 current_time = ktime_get_real();
5757 mpi_request.TimeStamp = cpu_to_le64(ktime_to_ms(current_time));
5759 if (ioc->logging_level & MPT_DEBUG_INIT) {
5760 __le32 *mfp;
5761 int i;
5763 mfp = (__le32 *)&mpi_request;
5764 pr_info("\toffset:data\n");
5765 for (i = 0; i < sizeof(Mpi2IOCInitRequest_t)/4; i++)
5766 pr_info("\t[0x%02x]:%08x\n", i*4,
5767 le32_to_cpu(mfp[i]));
5770 r = _base_handshake_req_reply_wait(ioc,
5771 sizeof(Mpi2IOCInitRequest_t), (u32 *)&mpi_request,
5772 sizeof(Mpi2IOCInitReply_t), (u16 *)&mpi_reply, 10);
5774 if (r != 0) {
5775 pr_err(MPT3SAS_FMT "%s: handshake failed (r=%d)\n",
5776 ioc->name, __func__, r);
5777 return r;
5780 ioc_status = le16_to_cpu(mpi_reply.IOCStatus) & MPI2_IOCSTATUS_MASK;
5781 if (ioc_status != MPI2_IOCSTATUS_SUCCESS ||
5782 mpi_reply.IOCLogInfo) {
5783 pr_err(MPT3SAS_FMT "%s: failed\n", ioc->name, __func__);
5784 r = -EIO;
5787 return r;
5791 * mpt3sas_port_enable_done - command completion routine for port enable
5792 * @ioc: per adapter object
5793 * @smid: system request message index
5794 * @msix_index: MSIX table index supplied by the OS
5795 * @reply: reply message frame(lower 32bit addr)
5797 * Return: 1 meaning mf should be freed from _base_interrupt
5798 * 0 means the mf is freed from this function.
5801 mpt3sas_port_enable_done(struct MPT3SAS_ADAPTER *ioc, u16 smid, u8 msix_index,
5802 u32 reply)
5804 MPI2DefaultReply_t *mpi_reply;
5805 u16 ioc_status;
5807 if (ioc->port_enable_cmds.status == MPT3_CMD_NOT_USED)
5808 return 1;
5810 mpi_reply = mpt3sas_base_get_reply_virt_addr(ioc, reply);
5811 if (!mpi_reply)
5812 return 1;
5814 if (mpi_reply->Function != MPI2_FUNCTION_PORT_ENABLE)
5815 return 1;
5817 ioc->port_enable_cmds.status &= ~MPT3_CMD_PENDING;
5818 ioc->port_enable_cmds.status |= MPT3_CMD_COMPLETE;
5819 ioc->port_enable_cmds.status |= MPT3_CMD_REPLY_VALID;
5820 memcpy(ioc->port_enable_cmds.reply, mpi_reply, mpi_reply->MsgLength*4);
5821 ioc_status = le16_to_cpu(mpi_reply->IOCStatus) & MPI2_IOCSTATUS_MASK;
5822 if (ioc_status != MPI2_IOCSTATUS_SUCCESS)
5823 ioc->port_enable_failed = 1;
5825 if (ioc->is_driver_loading) {
5826 if (ioc_status == MPI2_IOCSTATUS_SUCCESS) {
5827 mpt3sas_port_enable_complete(ioc);
5828 return 1;
5829 } else {
5830 ioc->start_scan_failed = ioc_status;
5831 ioc->start_scan = 0;
5832 return 1;
5835 complete(&ioc->port_enable_cmds.done);
5836 return 1;
5840 * _base_send_port_enable - send port_enable(discovery stuff) to firmware
5841 * @ioc: per adapter object
5843 * Return: 0 for success, non-zero for failure.
5845 static int
5846 _base_send_port_enable(struct MPT3SAS_ADAPTER *ioc)
5848 Mpi2PortEnableRequest_t *mpi_request;
5849 Mpi2PortEnableReply_t *mpi_reply;
5850 int r = 0;
5851 u16 smid;
5852 u16 ioc_status;
5854 pr_info(MPT3SAS_FMT "sending port enable !!\n", ioc->name);
5856 if (ioc->port_enable_cmds.status & MPT3_CMD_PENDING) {
5857 pr_err(MPT3SAS_FMT "%s: internal command already in use\n",
5858 ioc->name, __func__);
5859 return -EAGAIN;
5862 smid = mpt3sas_base_get_smid(ioc, ioc->port_enable_cb_idx);
5863 if (!smid) {
5864 pr_err(MPT3SAS_FMT "%s: failed obtaining a smid\n",
5865 ioc->name, __func__);
5866 return -EAGAIN;
5869 ioc->port_enable_cmds.status = MPT3_CMD_PENDING;
5870 mpi_request = mpt3sas_base_get_msg_frame(ioc, smid);
5871 ioc->port_enable_cmds.smid = smid;
5872 memset(mpi_request, 0, sizeof(Mpi2PortEnableRequest_t));
5873 mpi_request->Function = MPI2_FUNCTION_PORT_ENABLE;
5875 init_completion(&ioc->port_enable_cmds.done);
5876 mpt3sas_base_put_smid_default(ioc, smid);
5877 wait_for_completion_timeout(&ioc->port_enable_cmds.done, 300*HZ);
5878 if (!(ioc->port_enable_cmds.status & MPT3_CMD_COMPLETE)) {
5879 pr_err(MPT3SAS_FMT "%s: timeout\n",
5880 ioc->name, __func__);
5881 _debug_dump_mf(mpi_request,
5882 sizeof(Mpi2PortEnableRequest_t)/4);
5883 if (ioc->port_enable_cmds.status & MPT3_CMD_RESET)
5884 r = -EFAULT;
5885 else
5886 r = -ETIME;
5887 goto out;
5890 mpi_reply = ioc->port_enable_cmds.reply;
5891 ioc_status = le16_to_cpu(mpi_reply->IOCStatus) & MPI2_IOCSTATUS_MASK;
5892 if (ioc_status != MPI2_IOCSTATUS_SUCCESS) {
5893 pr_err(MPT3SAS_FMT "%s: failed with (ioc_status=0x%08x)\n",
5894 ioc->name, __func__, ioc_status);
5895 r = -EFAULT;
5896 goto out;
5899 out:
5900 ioc->port_enable_cmds.status = MPT3_CMD_NOT_USED;
5901 pr_info(MPT3SAS_FMT "port enable: %s\n", ioc->name, ((r == 0) ?
5902 "SUCCESS" : "FAILED"));
5903 return r;
5907 * mpt3sas_port_enable - initiate firmware discovery (don't wait for reply)
5908 * @ioc: per adapter object
5910 * Return: 0 for success, non-zero for failure.
5913 mpt3sas_port_enable(struct MPT3SAS_ADAPTER *ioc)
5915 Mpi2PortEnableRequest_t *mpi_request;
5916 u16 smid;
5918 pr_info(MPT3SAS_FMT "sending port enable !!\n", ioc->name);
5920 if (ioc->port_enable_cmds.status & MPT3_CMD_PENDING) {
5921 pr_err(MPT3SAS_FMT "%s: internal command already in use\n",
5922 ioc->name, __func__);
5923 return -EAGAIN;
5926 smid = mpt3sas_base_get_smid(ioc, ioc->port_enable_cb_idx);
5927 if (!smid) {
5928 pr_err(MPT3SAS_FMT "%s: failed obtaining a smid\n",
5929 ioc->name, __func__);
5930 return -EAGAIN;
5933 ioc->port_enable_cmds.status = MPT3_CMD_PENDING;
5934 mpi_request = mpt3sas_base_get_msg_frame(ioc, smid);
5935 ioc->port_enable_cmds.smid = smid;
5936 memset(mpi_request, 0, sizeof(Mpi2PortEnableRequest_t));
5937 mpi_request->Function = MPI2_FUNCTION_PORT_ENABLE;
5939 mpt3sas_base_put_smid_default(ioc, smid);
5940 return 0;
5944 * _base_determine_wait_on_discovery - desposition
5945 * @ioc: per adapter object
5947 * Decide whether to wait on discovery to complete. Used to either
5948 * locate boot device, or report volumes ahead of physical devices.
5950 * Return: 1 for wait, 0 for don't wait.
5952 static int
5953 _base_determine_wait_on_discovery(struct MPT3SAS_ADAPTER *ioc)
5955 /* We wait for discovery to complete if IR firmware is loaded.
5956 * The sas topology events arrive before PD events, so we need time to
5957 * turn on the bit in ioc->pd_handles to indicate PD
5958 * Also, it maybe required to report Volumes ahead of physical
5959 * devices when MPI2_IOCPAGE8_IRFLAGS_LOW_VOLUME_MAPPING is set.
5961 if (ioc->ir_firmware)
5962 return 1;
5964 /* if no Bios, then we don't need to wait */
5965 if (!ioc->bios_pg3.BiosVersion)
5966 return 0;
5968 /* Bios is present, then we drop down here.
5970 * If there any entries in the Bios Page 2, then we wait
5971 * for discovery to complete.
5974 /* Current Boot Device */
5975 if ((ioc->bios_pg2.CurrentBootDeviceForm &
5976 MPI2_BIOSPAGE2_FORM_MASK) ==
5977 MPI2_BIOSPAGE2_FORM_NO_DEVICE_SPECIFIED &&
5978 /* Request Boot Device */
5979 (ioc->bios_pg2.ReqBootDeviceForm &
5980 MPI2_BIOSPAGE2_FORM_MASK) ==
5981 MPI2_BIOSPAGE2_FORM_NO_DEVICE_SPECIFIED &&
5982 /* Alternate Request Boot Device */
5983 (ioc->bios_pg2.ReqAltBootDeviceForm &
5984 MPI2_BIOSPAGE2_FORM_MASK) ==
5985 MPI2_BIOSPAGE2_FORM_NO_DEVICE_SPECIFIED)
5986 return 0;
5988 return 1;
5992 * _base_unmask_events - turn on notification for this event
5993 * @ioc: per adapter object
5994 * @event: firmware event
5996 * The mask is stored in ioc->event_masks.
5998 static void
5999 _base_unmask_events(struct MPT3SAS_ADAPTER *ioc, u16 event)
6001 u32 desired_event;
6003 if (event >= 128)
6004 return;
6006 desired_event = (1 << (event % 32));
6008 if (event < 32)
6009 ioc->event_masks[0] &= ~desired_event;
6010 else if (event < 64)
6011 ioc->event_masks[1] &= ~desired_event;
6012 else if (event < 96)
6013 ioc->event_masks[2] &= ~desired_event;
6014 else if (event < 128)
6015 ioc->event_masks[3] &= ~desired_event;
6019 * _base_event_notification - send event notification
6020 * @ioc: per adapter object
6022 * Return: 0 for success, non-zero for failure.
6024 static int
6025 _base_event_notification(struct MPT3SAS_ADAPTER *ioc)
6027 Mpi2EventNotificationRequest_t *mpi_request;
6028 u16 smid;
6029 int r = 0;
6030 int i;
6032 dinitprintk(ioc, pr_info(MPT3SAS_FMT "%s\n", ioc->name,
6033 __func__));
6035 if (ioc->base_cmds.status & MPT3_CMD_PENDING) {
6036 pr_err(MPT3SAS_FMT "%s: internal command already in use\n",
6037 ioc->name, __func__);
6038 return -EAGAIN;
6041 smid = mpt3sas_base_get_smid(ioc, ioc->base_cb_idx);
6042 if (!smid) {
6043 pr_err(MPT3SAS_FMT "%s: failed obtaining a smid\n",
6044 ioc->name, __func__);
6045 return -EAGAIN;
6047 ioc->base_cmds.status = MPT3_CMD_PENDING;
6048 mpi_request = mpt3sas_base_get_msg_frame(ioc, smid);
6049 ioc->base_cmds.smid = smid;
6050 memset(mpi_request, 0, sizeof(Mpi2EventNotificationRequest_t));
6051 mpi_request->Function = MPI2_FUNCTION_EVENT_NOTIFICATION;
6052 mpi_request->VF_ID = 0; /* TODO */
6053 mpi_request->VP_ID = 0;
6054 for (i = 0; i < MPI2_EVENT_NOTIFY_EVENTMASK_WORDS; i++)
6055 mpi_request->EventMasks[i] =
6056 cpu_to_le32(ioc->event_masks[i]);
6057 init_completion(&ioc->base_cmds.done);
6058 mpt3sas_base_put_smid_default(ioc, smid);
6059 wait_for_completion_timeout(&ioc->base_cmds.done, 30*HZ);
6060 if (!(ioc->base_cmds.status & MPT3_CMD_COMPLETE)) {
6061 pr_err(MPT3SAS_FMT "%s: timeout\n",
6062 ioc->name, __func__);
6063 _debug_dump_mf(mpi_request,
6064 sizeof(Mpi2EventNotificationRequest_t)/4);
6065 if (ioc->base_cmds.status & MPT3_CMD_RESET)
6066 r = -EFAULT;
6067 else
6068 r = -ETIME;
6069 } else
6070 dinitprintk(ioc, pr_info(MPT3SAS_FMT "%s: complete\n",
6071 ioc->name, __func__));
6072 ioc->base_cmds.status = MPT3_CMD_NOT_USED;
6073 return r;
6077 * mpt3sas_base_validate_event_type - validating event types
6078 * @ioc: per adapter object
6079 * @event_type: firmware event
6081 * This will turn on firmware event notification when application
6082 * ask for that event. We don't mask events that are already enabled.
6084 void
6085 mpt3sas_base_validate_event_type(struct MPT3SAS_ADAPTER *ioc, u32 *event_type)
6087 int i, j;
6088 u32 event_mask, desired_event;
6089 u8 send_update_to_fw;
6091 for (i = 0, send_update_to_fw = 0; i <
6092 MPI2_EVENT_NOTIFY_EVENTMASK_WORDS; i++) {
6093 event_mask = ~event_type[i];
6094 desired_event = 1;
6095 for (j = 0; j < 32; j++) {
6096 if (!(event_mask & desired_event) &&
6097 (ioc->event_masks[i] & desired_event)) {
6098 ioc->event_masks[i] &= ~desired_event;
6099 send_update_to_fw = 1;
6101 desired_event = (desired_event << 1);
6105 if (!send_update_to_fw)
6106 return;
6108 mutex_lock(&ioc->base_cmds.mutex);
6109 _base_event_notification(ioc);
6110 mutex_unlock(&ioc->base_cmds.mutex);
6114 * _base_diag_reset - the "big hammer" start of day reset
6115 * @ioc: per adapter object
6117 * Return: 0 for success, non-zero for failure.
6119 static int
6120 _base_diag_reset(struct MPT3SAS_ADAPTER *ioc)
6122 u32 host_diagnostic;
6123 u32 ioc_state;
6124 u32 count;
6125 u32 hcb_size;
6127 pr_info(MPT3SAS_FMT "sending diag reset !!\n", ioc->name);
6129 drsprintk(ioc, pr_info(MPT3SAS_FMT "clear interrupts\n",
6130 ioc->name));
6132 count = 0;
6133 do {
6134 /* Write magic sequence to WriteSequence register
6135 * Loop until in diagnostic mode
6137 drsprintk(ioc, pr_info(MPT3SAS_FMT
6138 "write magic sequence\n", ioc->name));
6139 writel(MPI2_WRSEQ_FLUSH_KEY_VALUE, &ioc->chip->WriteSequence);
6140 writel(MPI2_WRSEQ_1ST_KEY_VALUE, &ioc->chip->WriteSequence);
6141 writel(MPI2_WRSEQ_2ND_KEY_VALUE, &ioc->chip->WriteSequence);
6142 writel(MPI2_WRSEQ_3RD_KEY_VALUE, &ioc->chip->WriteSequence);
6143 writel(MPI2_WRSEQ_4TH_KEY_VALUE, &ioc->chip->WriteSequence);
6144 writel(MPI2_WRSEQ_5TH_KEY_VALUE, &ioc->chip->WriteSequence);
6145 writel(MPI2_WRSEQ_6TH_KEY_VALUE, &ioc->chip->WriteSequence);
6147 /* wait 100 msec */
6148 msleep(100);
6150 if (count++ > 20)
6151 goto out;
6153 host_diagnostic = readl(&ioc->chip->HostDiagnostic);
6154 drsprintk(ioc, pr_info(MPT3SAS_FMT
6155 "wrote magic sequence: count(%d), host_diagnostic(0x%08x)\n",
6156 ioc->name, count, host_diagnostic));
6158 } while ((host_diagnostic & MPI2_DIAG_DIAG_WRITE_ENABLE) == 0);
6160 hcb_size = readl(&ioc->chip->HCBSize);
6162 drsprintk(ioc, pr_info(MPT3SAS_FMT "diag reset: issued\n",
6163 ioc->name));
6164 writel(host_diagnostic | MPI2_DIAG_RESET_ADAPTER,
6165 &ioc->chip->HostDiagnostic);
6167 /*This delay allows the chip PCIe hardware time to finish reset tasks*/
6168 msleep(MPI2_HARD_RESET_PCIE_FIRST_READ_DELAY_MICRO_SEC/1000);
6170 /* Approximately 300 second max wait */
6171 for (count = 0; count < (300000000 /
6172 MPI2_HARD_RESET_PCIE_SECOND_READ_DELAY_MICRO_SEC); count++) {
6174 host_diagnostic = readl(&ioc->chip->HostDiagnostic);
6176 if (host_diagnostic == 0xFFFFFFFF)
6177 goto out;
6178 if (!(host_diagnostic & MPI2_DIAG_RESET_ADAPTER))
6179 break;
6181 msleep(MPI2_HARD_RESET_PCIE_SECOND_READ_DELAY_MICRO_SEC / 1000);
6184 if (host_diagnostic & MPI2_DIAG_HCB_MODE) {
6186 drsprintk(ioc, pr_info(MPT3SAS_FMT
6187 "restart the adapter assuming the HCB Address points to good F/W\n",
6188 ioc->name));
6189 host_diagnostic &= ~MPI2_DIAG_BOOT_DEVICE_SELECT_MASK;
6190 host_diagnostic |= MPI2_DIAG_BOOT_DEVICE_SELECT_HCDW;
6191 writel(host_diagnostic, &ioc->chip->HostDiagnostic);
6193 drsprintk(ioc, pr_info(MPT3SAS_FMT
6194 "re-enable the HCDW\n", ioc->name));
6195 writel(hcb_size | MPI2_HCB_SIZE_HCB_ENABLE,
6196 &ioc->chip->HCBSize);
6199 drsprintk(ioc, pr_info(MPT3SAS_FMT "restart the adapter\n",
6200 ioc->name));
6201 writel(host_diagnostic & ~MPI2_DIAG_HOLD_IOC_RESET,
6202 &ioc->chip->HostDiagnostic);
6204 drsprintk(ioc, pr_info(MPT3SAS_FMT
6205 "disable writes to the diagnostic register\n", ioc->name));
6206 writel(MPI2_WRSEQ_FLUSH_KEY_VALUE, &ioc->chip->WriteSequence);
6208 drsprintk(ioc, pr_info(MPT3SAS_FMT
6209 "Wait for FW to go to the READY state\n", ioc->name));
6210 ioc_state = _base_wait_on_iocstate(ioc, MPI2_IOC_STATE_READY, 20);
6211 if (ioc_state) {
6212 pr_err(MPT3SAS_FMT
6213 "%s: failed going to ready state (ioc_state=0x%x)\n",
6214 ioc->name, __func__, ioc_state);
6215 goto out;
6218 pr_info(MPT3SAS_FMT "diag reset: SUCCESS\n", ioc->name);
6219 return 0;
6221 out:
6222 pr_err(MPT3SAS_FMT "diag reset: FAILED\n", ioc->name);
6223 return -EFAULT;
6227 * _base_make_ioc_ready - put controller in READY state
6228 * @ioc: per adapter object
6229 * @type: FORCE_BIG_HAMMER or SOFT_RESET
6231 * Return: 0 for success, non-zero for failure.
6233 static int
6234 _base_make_ioc_ready(struct MPT3SAS_ADAPTER *ioc, enum reset_type type)
6236 u32 ioc_state;
6237 int rc;
6238 int count;
6240 dinitprintk(ioc, pr_info(MPT3SAS_FMT "%s\n", ioc->name,
6241 __func__));
6243 if (ioc->pci_error_recovery)
6244 return 0;
6246 ioc_state = mpt3sas_base_get_iocstate(ioc, 0);
6247 dhsprintk(ioc, pr_info(MPT3SAS_FMT "%s: ioc_state(0x%08x)\n",
6248 ioc->name, __func__, ioc_state));
6250 /* if in RESET state, it should move to READY state shortly */
6251 count = 0;
6252 if ((ioc_state & MPI2_IOC_STATE_MASK) == MPI2_IOC_STATE_RESET) {
6253 while ((ioc_state & MPI2_IOC_STATE_MASK) !=
6254 MPI2_IOC_STATE_READY) {
6255 if (count++ == 10) {
6256 pr_err(MPT3SAS_FMT
6257 "%s: failed going to ready state (ioc_state=0x%x)\n",
6258 ioc->name, __func__, ioc_state);
6259 return -EFAULT;
6261 ssleep(1);
6262 ioc_state = mpt3sas_base_get_iocstate(ioc, 0);
6266 if ((ioc_state & MPI2_IOC_STATE_MASK) == MPI2_IOC_STATE_READY)
6267 return 0;
6269 if (ioc_state & MPI2_DOORBELL_USED) {
6270 dhsprintk(ioc, pr_info(MPT3SAS_FMT
6271 "unexpected doorbell active!\n",
6272 ioc->name));
6273 goto issue_diag_reset;
6276 if ((ioc_state & MPI2_IOC_STATE_MASK) == MPI2_IOC_STATE_FAULT) {
6277 mpt3sas_base_fault_info(ioc, ioc_state &
6278 MPI2_DOORBELL_DATA_MASK);
6279 goto issue_diag_reset;
6282 if (type == FORCE_BIG_HAMMER)
6283 goto issue_diag_reset;
6285 if ((ioc_state & MPI2_IOC_STATE_MASK) == MPI2_IOC_STATE_OPERATIONAL)
6286 if (!(_base_send_ioc_reset(ioc,
6287 MPI2_FUNCTION_IOC_MESSAGE_UNIT_RESET, 15))) {
6288 return 0;
6291 issue_diag_reset:
6292 rc = _base_diag_reset(ioc);
6293 return rc;
6297 * _base_make_ioc_operational - put controller in OPERATIONAL state
6298 * @ioc: per adapter object
6300 * Return: 0 for success, non-zero for failure.
6302 static int
6303 _base_make_ioc_operational(struct MPT3SAS_ADAPTER *ioc)
6305 int r, i, index;
6306 unsigned long flags;
6307 u32 reply_address;
6308 u16 smid;
6309 struct _tr_list *delayed_tr, *delayed_tr_next;
6310 struct _sc_list *delayed_sc, *delayed_sc_next;
6311 struct _event_ack_list *delayed_event_ack, *delayed_event_ack_next;
6312 u8 hide_flag;
6313 struct adapter_reply_queue *reply_q;
6314 Mpi2ReplyDescriptorsUnion_t *reply_post_free_contig;
6316 dinitprintk(ioc, pr_info(MPT3SAS_FMT "%s\n", ioc->name,
6317 __func__));
6319 /* clean the delayed target reset list */
6320 list_for_each_entry_safe(delayed_tr, delayed_tr_next,
6321 &ioc->delayed_tr_list, list) {
6322 list_del(&delayed_tr->list);
6323 kfree(delayed_tr);
6327 list_for_each_entry_safe(delayed_tr, delayed_tr_next,
6328 &ioc->delayed_tr_volume_list, list) {
6329 list_del(&delayed_tr->list);
6330 kfree(delayed_tr);
6333 list_for_each_entry_safe(delayed_sc, delayed_sc_next,
6334 &ioc->delayed_sc_list, list) {
6335 list_del(&delayed_sc->list);
6336 kfree(delayed_sc);
6339 list_for_each_entry_safe(delayed_event_ack, delayed_event_ack_next,
6340 &ioc->delayed_event_ack_list, list) {
6341 list_del(&delayed_event_ack->list);
6342 kfree(delayed_event_ack);
6345 spin_lock_irqsave(&ioc->scsi_lookup_lock, flags);
6347 /* hi-priority queue */
6348 INIT_LIST_HEAD(&ioc->hpr_free_list);
6349 smid = ioc->hi_priority_smid;
6350 for (i = 0; i < ioc->hi_priority_depth; i++, smid++) {
6351 ioc->hpr_lookup[i].cb_idx = 0xFF;
6352 ioc->hpr_lookup[i].smid = smid;
6353 list_add_tail(&ioc->hpr_lookup[i].tracker_list,
6354 &ioc->hpr_free_list);
6357 /* internal queue */
6358 INIT_LIST_HEAD(&ioc->internal_free_list);
6359 smid = ioc->internal_smid;
6360 for (i = 0; i < ioc->internal_depth; i++, smid++) {
6361 ioc->internal_lookup[i].cb_idx = 0xFF;
6362 ioc->internal_lookup[i].smid = smid;
6363 list_add_tail(&ioc->internal_lookup[i].tracker_list,
6364 &ioc->internal_free_list);
6367 spin_unlock_irqrestore(&ioc->scsi_lookup_lock, flags);
6369 /* initialize Reply Free Queue */
6370 for (i = 0, reply_address = (u32)ioc->reply_dma ;
6371 i < ioc->reply_free_queue_depth ; i++, reply_address +=
6372 ioc->reply_sz) {
6373 ioc->reply_free[i] = cpu_to_le32(reply_address);
6374 if (ioc->is_mcpu_endpoint)
6375 _base_clone_reply_to_sys_mem(ioc,
6376 reply_address, i);
6379 /* initialize reply queues */
6380 if (ioc->is_driver_loading)
6381 _base_assign_reply_queues(ioc);
6383 /* initialize Reply Post Free Queue */
6384 index = 0;
6385 reply_post_free_contig = ioc->reply_post[0].reply_post_free;
6386 list_for_each_entry(reply_q, &ioc->reply_queue_list, list) {
6388 * If RDPQ is enabled, switch to the next allocation.
6389 * Otherwise advance within the contiguous region.
6391 if (ioc->rdpq_array_enable) {
6392 reply_q->reply_post_free =
6393 ioc->reply_post[index++].reply_post_free;
6394 } else {
6395 reply_q->reply_post_free = reply_post_free_contig;
6396 reply_post_free_contig += ioc->reply_post_queue_depth;
6399 reply_q->reply_post_host_index = 0;
6400 for (i = 0; i < ioc->reply_post_queue_depth; i++)
6401 reply_q->reply_post_free[i].Words =
6402 cpu_to_le64(ULLONG_MAX);
6403 if (!_base_is_controller_msix_enabled(ioc))
6404 goto skip_init_reply_post_free_queue;
6406 skip_init_reply_post_free_queue:
6408 r = _base_send_ioc_init(ioc);
6409 if (r)
6410 return r;
6412 /* initialize reply free host index */
6413 ioc->reply_free_host_index = ioc->reply_free_queue_depth - 1;
6414 writel(ioc->reply_free_host_index, &ioc->chip->ReplyFreeHostIndex);
6416 /* initialize reply post host index */
6417 list_for_each_entry(reply_q, &ioc->reply_queue_list, list) {
6418 if (ioc->combined_reply_queue)
6419 writel((reply_q->msix_index & 7)<<
6420 MPI2_RPHI_MSIX_INDEX_SHIFT,
6421 ioc->replyPostRegisterIndex[reply_q->msix_index/8]);
6422 else
6423 writel(reply_q->msix_index <<
6424 MPI2_RPHI_MSIX_INDEX_SHIFT,
6425 &ioc->chip->ReplyPostHostIndex);
6427 if (!_base_is_controller_msix_enabled(ioc))
6428 goto skip_init_reply_post_host_index;
6431 skip_init_reply_post_host_index:
6433 _base_unmask_interrupts(ioc);
6435 if (ioc->hba_mpi_version_belonged != MPI2_VERSION) {
6436 r = _base_display_fwpkg_version(ioc);
6437 if (r)
6438 return r;
6441 _base_static_config_pages(ioc);
6442 r = _base_event_notification(ioc);
6443 if (r)
6444 return r;
6446 if (ioc->is_driver_loading) {
6448 if (ioc->is_warpdrive && ioc->manu_pg10.OEMIdentifier
6449 == 0x80) {
6450 hide_flag = (u8) (
6451 le32_to_cpu(ioc->manu_pg10.OEMSpecificFlags0) &
6452 MFG_PAGE10_HIDE_SSDS_MASK);
6453 if (hide_flag != MFG_PAGE10_HIDE_SSDS_MASK)
6454 ioc->mfg_pg10_hide_flag = hide_flag;
6457 ioc->wait_for_discovery_to_complete =
6458 _base_determine_wait_on_discovery(ioc);
6460 return r; /* scan_start and scan_finished support */
6463 r = _base_send_port_enable(ioc);
6464 if (r)
6465 return r;
6467 return r;
6471 * mpt3sas_base_free_resources - free resources controller resources
6472 * @ioc: per adapter object
6474 void
6475 mpt3sas_base_free_resources(struct MPT3SAS_ADAPTER *ioc)
6477 dexitprintk(ioc, pr_info(MPT3SAS_FMT "%s\n", ioc->name,
6478 __func__));
6480 /* synchronizing freeing resource with pci_access_mutex lock */
6481 mutex_lock(&ioc->pci_access_mutex);
6482 if (ioc->chip_phys && ioc->chip) {
6483 _base_mask_interrupts(ioc);
6484 ioc->shost_recovery = 1;
6485 _base_make_ioc_ready(ioc, SOFT_RESET);
6486 ioc->shost_recovery = 0;
6489 mpt3sas_base_unmap_resources(ioc);
6490 mutex_unlock(&ioc->pci_access_mutex);
6491 return;
6495 * mpt3sas_base_attach - attach controller instance
6496 * @ioc: per adapter object
6498 * Return: 0 for success, non-zero for failure.
6501 mpt3sas_base_attach(struct MPT3SAS_ADAPTER *ioc)
6503 int r, i;
6504 int cpu_id, last_cpu_id = 0;
6506 dinitprintk(ioc, pr_info(MPT3SAS_FMT "%s\n", ioc->name,
6507 __func__));
6509 /* setup cpu_msix_table */
6510 ioc->cpu_count = num_online_cpus();
6511 for_each_online_cpu(cpu_id)
6512 last_cpu_id = cpu_id;
6513 ioc->cpu_msix_table_sz = last_cpu_id + 1;
6514 ioc->cpu_msix_table = kzalloc(ioc->cpu_msix_table_sz, GFP_KERNEL);
6515 ioc->reply_queue_count = 1;
6516 if (!ioc->cpu_msix_table) {
6517 dfailprintk(ioc, pr_info(MPT3SAS_FMT
6518 "allocation for cpu_msix_table failed!!!\n",
6519 ioc->name));
6520 r = -ENOMEM;
6521 goto out_free_resources;
6524 if (ioc->is_warpdrive) {
6525 ioc->reply_post_host_index = kcalloc(ioc->cpu_msix_table_sz,
6526 sizeof(resource_size_t *), GFP_KERNEL);
6527 if (!ioc->reply_post_host_index) {
6528 dfailprintk(ioc, pr_info(MPT3SAS_FMT "allocation "
6529 "for reply_post_host_index failed!!!\n",
6530 ioc->name));
6531 r = -ENOMEM;
6532 goto out_free_resources;
6536 ioc->rdpq_array_enable_assigned = 0;
6537 ioc->dma_mask = 0;
6538 r = mpt3sas_base_map_resources(ioc);
6539 if (r)
6540 goto out_free_resources;
6542 pci_set_drvdata(ioc->pdev, ioc->shost);
6543 r = _base_get_ioc_facts(ioc);
6544 if (r)
6545 goto out_free_resources;
6547 switch (ioc->hba_mpi_version_belonged) {
6548 case MPI2_VERSION:
6549 ioc->build_sg_scmd = &_base_build_sg_scmd;
6550 ioc->build_sg = &_base_build_sg;
6551 ioc->build_zero_len_sge = &_base_build_zero_len_sge;
6552 break;
6553 case MPI25_VERSION:
6554 case MPI26_VERSION:
6556 * In SAS3.0,
6557 * SCSI_IO, SMP_PASSTHRU, SATA_PASSTHRU, Target Assist, and
6558 * Target Status - all require the IEEE formated scatter gather
6559 * elements.
6561 ioc->build_sg_scmd = &_base_build_sg_scmd_ieee;
6562 ioc->build_sg = &_base_build_sg_ieee;
6563 ioc->build_nvme_prp = &_base_build_nvme_prp;
6564 ioc->build_zero_len_sge = &_base_build_zero_len_sge_ieee;
6565 ioc->sge_size_ieee = sizeof(Mpi2IeeeSgeSimple64_t);
6567 break;
6570 if (ioc->is_mcpu_endpoint)
6571 ioc->put_smid_scsi_io = &_base_put_smid_mpi_ep_scsi_io;
6572 else
6573 ioc->put_smid_scsi_io = &_base_put_smid_scsi_io;
6576 * These function pointers for other requests that don't
6577 * the require IEEE scatter gather elements.
6579 * For example Configuration Pages and SAS IOUNIT Control don't.
6581 ioc->build_sg_mpi = &_base_build_sg;
6582 ioc->build_zero_len_sge_mpi = &_base_build_zero_len_sge;
6584 r = _base_make_ioc_ready(ioc, SOFT_RESET);
6585 if (r)
6586 goto out_free_resources;
6588 ioc->pfacts = kcalloc(ioc->facts.NumberOfPorts,
6589 sizeof(struct mpt3sas_port_facts), GFP_KERNEL);
6590 if (!ioc->pfacts) {
6591 r = -ENOMEM;
6592 goto out_free_resources;
6595 for (i = 0 ; i < ioc->facts.NumberOfPorts; i++) {
6596 r = _base_get_port_facts(ioc, i);
6597 if (r)
6598 goto out_free_resources;
6601 r = _base_allocate_memory_pools(ioc);
6602 if (r)
6603 goto out_free_resources;
6605 init_waitqueue_head(&ioc->reset_wq);
6607 /* allocate memory pd handle bitmask list */
6608 ioc->pd_handles_sz = (ioc->facts.MaxDevHandle / 8);
6609 if (ioc->facts.MaxDevHandle % 8)
6610 ioc->pd_handles_sz++;
6611 ioc->pd_handles = kzalloc(ioc->pd_handles_sz,
6612 GFP_KERNEL);
6613 if (!ioc->pd_handles) {
6614 r = -ENOMEM;
6615 goto out_free_resources;
6617 ioc->blocking_handles = kzalloc(ioc->pd_handles_sz,
6618 GFP_KERNEL);
6619 if (!ioc->blocking_handles) {
6620 r = -ENOMEM;
6621 goto out_free_resources;
6624 /* allocate memory for pending OS device add list */
6625 ioc->pend_os_device_add_sz = (ioc->facts.MaxDevHandle / 8);
6626 if (ioc->facts.MaxDevHandle % 8)
6627 ioc->pend_os_device_add_sz++;
6628 ioc->pend_os_device_add = kzalloc(ioc->pend_os_device_add_sz,
6629 GFP_KERNEL);
6630 if (!ioc->pend_os_device_add)
6631 goto out_free_resources;
6633 ioc->device_remove_in_progress_sz = ioc->pend_os_device_add_sz;
6634 ioc->device_remove_in_progress =
6635 kzalloc(ioc->device_remove_in_progress_sz, GFP_KERNEL);
6636 if (!ioc->device_remove_in_progress)
6637 goto out_free_resources;
6639 ioc->fwfault_debug = mpt3sas_fwfault_debug;
6641 /* base internal command bits */
6642 mutex_init(&ioc->base_cmds.mutex);
6643 ioc->base_cmds.reply = kzalloc(ioc->reply_sz, GFP_KERNEL);
6644 ioc->base_cmds.status = MPT3_CMD_NOT_USED;
6646 /* port_enable command bits */
6647 ioc->port_enable_cmds.reply = kzalloc(ioc->reply_sz, GFP_KERNEL);
6648 ioc->port_enable_cmds.status = MPT3_CMD_NOT_USED;
6650 /* transport internal command bits */
6651 ioc->transport_cmds.reply = kzalloc(ioc->reply_sz, GFP_KERNEL);
6652 ioc->transport_cmds.status = MPT3_CMD_NOT_USED;
6653 mutex_init(&ioc->transport_cmds.mutex);
6655 /* scsih internal command bits */
6656 ioc->scsih_cmds.reply = kzalloc(ioc->reply_sz, GFP_KERNEL);
6657 ioc->scsih_cmds.status = MPT3_CMD_NOT_USED;
6658 mutex_init(&ioc->scsih_cmds.mutex);
6660 /* task management internal command bits */
6661 ioc->tm_cmds.reply = kzalloc(ioc->reply_sz, GFP_KERNEL);
6662 ioc->tm_cmds.status = MPT3_CMD_NOT_USED;
6663 mutex_init(&ioc->tm_cmds.mutex);
6665 /* config page internal command bits */
6666 ioc->config_cmds.reply = kzalloc(ioc->reply_sz, GFP_KERNEL);
6667 ioc->config_cmds.status = MPT3_CMD_NOT_USED;
6668 mutex_init(&ioc->config_cmds.mutex);
6670 /* ctl module internal command bits */
6671 ioc->ctl_cmds.reply = kzalloc(ioc->reply_sz, GFP_KERNEL);
6672 ioc->ctl_cmds.sense = kzalloc(SCSI_SENSE_BUFFERSIZE, GFP_KERNEL);
6673 ioc->ctl_cmds.status = MPT3_CMD_NOT_USED;
6674 mutex_init(&ioc->ctl_cmds.mutex);
6676 if (!ioc->base_cmds.reply || !ioc->port_enable_cmds.reply ||
6677 !ioc->transport_cmds.reply || !ioc->scsih_cmds.reply ||
6678 !ioc->tm_cmds.reply || !ioc->config_cmds.reply ||
6679 !ioc->ctl_cmds.reply || !ioc->ctl_cmds.sense) {
6680 r = -ENOMEM;
6681 goto out_free_resources;
6684 for (i = 0; i < MPI2_EVENT_NOTIFY_EVENTMASK_WORDS; i++)
6685 ioc->event_masks[i] = -1;
6687 /* here we enable the events we care about */
6688 _base_unmask_events(ioc, MPI2_EVENT_SAS_DISCOVERY);
6689 _base_unmask_events(ioc, MPI2_EVENT_SAS_BROADCAST_PRIMITIVE);
6690 _base_unmask_events(ioc, MPI2_EVENT_SAS_TOPOLOGY_CHANGE_LIST);
6691 _base_unmask_events(ioc, MPI2_EVENT_SAS_DEVICE_STATUS_CHANGE);
6692 _base_unmask_events(ioc, MPI2_EVENT_SAS_ENCL_DEVICE_STATUS_CHANGE);
6693 _base_unmask_events(ioc, MPI2_EVENT_IR_CONFIGURATION_CHANGE_LIST);
6694 _base_unmask_events(ioc, MPI2_EVENT_IR_VOLUME);
6695 _base_unmask_events(ioc, MPI2_EVENT_IR_PHYSICAL_DISK);
6696 _base_unmask_events(ioc, MPI2_EVENT_IR_OPERATION_STATUS);
6697 _base_unmask_events(ioc, MPI2_EVENT_LOG_ENTRY_ADDED);
6698 _base_unmask_events(ioc, MPI2_EVENT_TEMP_THRESHOLD);
6699 _base_unmask_events(ioc, MPI2_EVENT_ACTIVE_CABLE_EXCEPTION);
6700 _base_unmask_events(ioc, MPI2_EVENT_SAS_DEVICE_DISCOVERY_ERROR);
6701 if (ioc->hba_mpi_version_belonged == MPI26_VERSION) {
6702 if (ioc->is_gen35_ioc) {
6703 _base_unmask_events(ioc,
6704 MPI2_EVENT_PCIE_DEVICE_STATUS_CHANGE);
6705 _base_unmask_events(ioc, MPI2_EVENT_PCIE_ENUMERATION);
6706 _base_unmask_events(ioc,
6707 MPI2_EVENT_PCIE_TOPOLOGY_CHANGE_LIST);
6710 r = _base_make_ioc_operational(ioc);
6711 if (r)
6712 goto out_free_resources;
6714 ioc->non_operational_loop = 0;
6715 ioc->got_task_abort_from_ioctl = 0;
6716 return 0;
6718 out_free_resources:
6720 ioc->remove_host = 1;
6722 mpt3sas_base_free_resources(ioc);
6723 _base_release_memory_pools(ioc);
6724 pci_set_drvdata(ioc->pdev, NULL);
6725 kfree(ioc->cpu_msix_table);
6726 if (ioc->is_warpdrive)
6727 kfree(ioc->reply_post_host_index);
6728 kfree(ioc->pd_handles);
6729 kfree(ioc->blocking_handles);
6730 kfree(ioc->device_remove_in_progress);
6731 kfree(ioc->pend_os_device_add);
6732 kfree(ioc->tm_cmds.reply);
6733 kfree(ioc->transport_cmds.reply);
6734 kfree(ioc->scsih_cmds.reply);
6735 kfree(ioc->config_cmds.reply);
6736 kfree(ioc->base_cmds.reply);
6737 kfree(ioc->port_enable_cmds.reply);
6738 kfree(ioc->ctl_cmds.reply);
6739 kfree(ioc->ctl_cmds.sense);
6740 kfree(ioc->pfacts);
6741 ioc->ctl_cmds.reply = NULL;
6742 ioc->base_cmds.reply = NULL;
6743 ioc->tm_cmds.reply = NULL;
6744 ioc->scsih_cmds.reply = NULL;
6745 ioc->transport_cmds.reply = NULL;
6746 ioc->config_cmds.reply = NULL;
6747 ioc->pfacts = NULL;
6748 return r;
6753 * mpt3sas_base_detach - remove controller instance
6754 * @ioc: per adapter object
6756 void
6757 mpt3sas_base_detach(struct MPT3SAS_ADAPTER *ioc)
6759 dexitprintk(ioc, pr_info(MPT3SAS_FMT "%s\n", ioc->name,
6760 __func__));
6762 mpt3sas_base_stop_watchdog(ioc);
6763 mpt3sas_base_free_resources(ioc);
6764 _base_release_memory_pools(ioc);
6765 mpt3sas_free_enclosure_list(ioc);
6766 pci_set_drvdata(ioc->pdev, NULL);
6767 kfree(ioc->cpu_msix_table);
6768 if (ioc->is_warpdrive)
6769 kfree(ioc->reply_post_host_index);
6770 kfree(ioc->pd_handles);
6771 kfree(ioc->blocking_handles);
6772 kfree(ioc->device_remove_in_progress);
6773 kfree(ioc->pend_os_device_add);
6774 kfree(ioc->pfacts);
6775 kfree(ioc->ctl_cmds.reply);
6776 kfree(ioc->ctl_cmds.sense);
6777 kfree(ioc->base_cmds.reply);
6778 kfree(ioc->port_enable_cmds.reply);
6779 kfree(ioc->tm_cmds.reply);
6780 kfree(ioc->transport_cmds.reply);
6781 kfree(ioc->scsih_cmds.reply);
6782 kfree(ioc->config_cmds.reply);
6786 * _base_pre_reset_handler - pre reset handler
6787 * @ioc: per adapter object
6789 static void _base_pre_reset_handler(struct MPT3SAS_ADAPTER *ioc)
6791 mpt3sas_scsih_pre_reset_handler(ioc);
6792 mpt3sas_ctl_pre_reset_handler(ioc);
6793 dtmprintk(ioc, pr_info(MPT3SAS_FMT
6794 "%s: MPT3_IOC_PRE_RESET\n", ioc->name, __func__));
6798 * _base_after_reset_handler - after reset handler
6799 * @ioc: per adapter object
6801 static void _base_after_reset_handler(struct MPT3SAS_ADAPTER *ioc)
6803 mpt3sas_scsih_after_reset_handler(ioc);
6804 mpt3sas_ctl_after_reset_handler(ioc);
6805 dtmprintk(ioc, pr_info(MPT3SAS_FMT
6806 "%s: MPT3_IOC_AFTER_RESET\n", ioc->name, __func__));
6807 if (ioc->transport_cmds.status & MPT3_CMD_PENDING) {
6808 ioc->transport_cmds.status |= MPT3_CMD_RESET;
6809 mpt3sas_base_free_smid(ioc, ioc->transport_cmds.smid);
6810 complete(&ioc->transport_cmds.done);
6812 if (ioc->base_cmds.status & MPT3_CMD_PENDING) {
6813 ioc->base_cmds.status |= MPT3_CMD_RESET;
6814 mpt3sas_base_free_smid(ioc, ioc->base_cmds.smid);
6815 complete(&ioc->base_cmds.done);
6817 if (ioc->port_enable_cmds.status & MPT3_CMD_PENDING) {
6818 ioc->port_enable_failed = 1;
6819 ioc->port_enable_cmds.status |= MPT3_CMD_RESET;
6820 mpt3sas_base_free_smid(ioc, ioc->port_enable_cmds.smid);
6821 if (ioc->is_driver_loading) {
6822 ioc->start_scan_failed =
6823 MPI2_IOCSTATUS_INTERNAL_ERROR;
6824 ioc->start_scan = 0;
6825 ioc->port_enable_cmds.status =
6826 MPT3_CMD_NOT_USED;
6827 } else {
6828 complete(&ioc->port_enable_cmds.done);
6831 if (ioc->config_cmds.status & MPT3_CMD_PENDING) {
6832 ioc->config_cmds.status |= MPT3_CMD_RESET;
6833 mpt3sas_base_free_smid(ioc, ioc->config_cmds.smid);
6834 ioc->config_cmds.smid = USHRT_MAX;
6835 complete(&ioc->config_cmds.done);
6840 * _base_reset_done_handler - reset done handler
6841 * @ioc: per adapter object
6843 static void _base_reset_done_handler(struct MPT3SAS_ADAPTER *ioc)
6845 mpt3sas_scsih_reset_done_handler(ioc);
6846 mpt3sas_ctl_reset_done_handler(ioc);
6847 dtmprintk(ioc, pr_info(MPT3SAS_FMT
6848 "%s: MPT3_IOC_DONE_RESET\n", ioc->name, __func__));
6852 * mpt3sas_wait_for_commands_to_complete - reset controller
6853 * @ioc: Pointer to MPT_ADAPTER structure
6855 * This function is waiting 10s for all pending commands to complete
6856 * prior to putting controller in reset.
6858 void
6859 mpt3sas_wait_for_commands_to_complete(struct MPT3SAS_ADAPTER *ioc)
6861 u32 ioc_state;
6863 ioc->pending_io_count = 0;
6865 ioc_state = mpt3sas_base_get_iocstate(ioc, 0);
6866 if ((ioc_state & MPI2_IOC_STATE_MASK) != MPI2_IOC_STATE_OPERATIONAL)
6867 return;
6869 /* pending command count */
6870 ioc->pending_io_count = scsi_host_busy(ioc->shost);
6872 if (!ioc->pending_io_count)
6873 return;
6875 /* wait for pending commands to complete */
6876 wait_event_timeout(ioc->reset_wq, ioc->pending_io_count == 0, 10 * HZ);
6880 * mpt3sas_base_hard_reset_handler - reset controller
6881 * @ioc: Pointer to MPT_ADAPTER structure
6882 * @type: FORCE_BIG_HAMMER or SOFT_RESET
6884 * Return: 0 for success, non-zero for failure.
6887 mpt3sas_base_hard_reset_handler(struct MPT3SAS_ADAPTER *ioc,
6888 enum reset_type type)
6890 int r;
6891 unsigned long flags;
6892 u32 ioc_state;
6893 u8 is_fault = 0, is_trigger = 0;
6895 dtmprintk(ioc, pr_info(MPT3SAS_FMT "%s: enter\n", ioc->name,
6896 __func__));
6898 if (ioc->pci_error_recovery) {
6899 pr_err(MPT3SAS_FMT "%s: pci error recovery reset\n",
6900 ioc->name, __func__);
6901 r = 0;
6902 goto out_unlocked;
6905 if (mpt3sas_fwfault_debug)
6906 mpt3sas_halt_firmware(ioc);
6908 /* wait for an active reset in progress to complete */
6909 mutex_lock(&ioc->reset_in_progress_mutex);
6911 spin_lock_irqsave(&ioc->ioc_reset_in_progress_lock, flags);
6912 ioc->shost_recovery = 1;
6913 spin_unlock_irqrestore(&ioc->ioc_reset_in_progress_lock, flags);
6915 if ((ioc->diag_buffer_status[MPI2_DIAG_BUF_TYPE_TRACE] &
6916 MPT3_DIAG_BUFFER_IS_REGISTERED) &&
6917 (!(ioc->diag_buffer_status[MPI2_DIAG_BUF_TYPE_TRACE] &
6918 MPT3_DIAG_BUFFER_IS_RELEASED))) {
6919 is_trigger = 1;
6920 ioc_state = mpt3sas_base_get_iocstate(ioc, 0);
6921 if ((ioc_state & MPI2_IOC_STATE_MASK) == MPI2_IOC_STATE_FAULT)
6922 is_fault = 1;
6924 _base_pre_reset_handler(ioc);
6925 mpt3sas_wait_for_commands_to_complete(ioc);
6926 _base_mask_interrupts(ioc);
6927 r = _base_make_ioc_ready(ioc, type);
6928 if (r)
6929 goto out;
6930 _base_after_reset_handler(ioc);
6932 /* If this hard reset is called while port enable is active, then
6933 * there is no reason to call make_ioc_operational
6935 if (ioc->is_driver_loading && ioc->port_enable_failed) {
6936 ioc->remove_host = 1;
6937 r = -EFAULT;
6938 goto out;
6940 r = _base_get_ioc_facts(ioc);
6941 if (r)
6942 goto out;
6944 if (ioc->rdpq_array_enable && !ioc->rdpq_array_capable)
6945 panic("%s: Issue occurred with flashing controller firmware."
6946 "Please reboot the system and ensure that the correct"
6947 " firmware version is running\n", ioc->name);
6949 r = _base_make_ioc_operational(ioc);
6950 if (!r)
6951 _base_reset_done_handler(ioc);
6953 out:
6954 dtmprintk(ioc, pr_info(MPT3SAS_FMT "%s: %s\n",
6955 ioc->name, __func__, ((r == 0) ? "SUCCESS" : "FAILED")));
6957 spin_lock_irqsave(&ioc->ioc_reset_in_progress_lock, flags);
6958 ioc->shost_recovery = 0;
6959 spin_unlock_irqrestore(&ioc->ioc_reset_in_progress_lock, flags);
6960 ioc->ioc_reset_count++;
6961 mutex_unlock(&ioc->reset_in_progress_mutex);
6963 out_unlocked:
6964 if ((r == 0) && is_trigger) {
6965 if (is_fault)
6966 mpt3sas_trigger_master(ioc, MASTER_TRIGGER_FW_FAULT);
6967 else
6968 mpt3sas_trigger_master(ioc,
6969 MASTER_TRIGGER_ADAPTER_RESET);
6971 dtmprintk(ioc, pr_info(MPT3SAS_FMT "%s: exit\n", ioc->name,
6972 __func__));
6973 return r;