scsi: mpt3sas: check command status before attempting abort
[linux-2.6-block.git] / drivers / scsi / mpt3sas / mpt3sas_base.c
CommitLineData
f92363d1
SR
1/*
2 * This is the Fusion MPT base driver providing common API layer interface
3 * for access to MPT (Message Passing Technology) firmware.
4 *
5 * This code is based on drivers/scsi/mpt3sas/mpt3sas_base.c
a4ffce0d 6 * Copyright (C) 2012-2014 LSI Corporation
a03bd153
SR
7 * Copyright (C) 2013-2014 Avago Technologies
8 * (mailto: MPT-FusionLinux.pdl@avagotech.com)
f92363d1
SR
9 *
10 * This program is free software; you can redistribute it and/or
11 * modify it under the terms of the GNU General Public License
12 * as published by the Free Software Foundation; either version 2
13 * of the License, or (at your option) any later version.
14 *
15 * This program is distributed in the hope that it will be useful,
16 * but WITHOUT ANY WARRANTY; without even the implied warranty of
17 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
18 * GNU General Public License for more details.
19 *
20 * NO WARRANTY
21 * THE PROGRAM IS PROVIDED ON AN "AS IS" BASIS, WITHOUT WARRANTIES OR
22 * CONDITIONS OF ANY KIND, EITHER EXPRESS OR IMPLIED INCLUDING, WITHOUT
23 * LIMITATION, ANY WARRANTIES OR CONDITIONS OF TITLE, NON-INFRINGEMENT,
24 * MERCHANTABILITY OR FITNESS FOR A PARTICULAR PURPOSE. Each Recipient is
25 * solely responsible for determining the appropriateness of using and
26 * distributing the Program and assumes all risks associated with its
27 * exercise of rights under this Agreement, including but not limited to
28 * the risks and costs of program errors, damage to or loss of data,
29 * programs or equipment, and unavailability or interruption of operations.
30
31 * DISCLAIMER OF LIABILITY
32 * NEITHER RECIPIENT NOR ANY CONTRIBUTORS SHALL HAVE ANY LIABILITY FOR ANY
33 * DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
34 * DAMAGES (INCLUDING WITHOUT LIMITATION LOST PROFITS), HOWEVER CAUSED AND
35 * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR
36 * TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE
37 * USE OR DISTRIBUTION OF THE PROGRAM OR THE EXERCISE OF ANY RIGHTS GRANTED
38 * HEREUNDER, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGES
39
40 * You should have received a copy of the GNU General Public License
41 * along with this program; if not, write to the Free Software
42 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301,
43 * USA.
44 */
45
f92363d1
SR
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>
23409bd4 60#include <linux/ktime.h>
f92363d1 61#include <linux/kthread.h>
016d5c35 62#include <asm/page.h> /* To get host page size per arch */
f92363d1
SR
63#include <linux/aer.h>
64
65
66#include "mpt3sas_base.h"
67
68static MPT_CALLBACK mpt_callbacks[MPT_MAX_CALLBACKS];
69
70
71#define FAULT_POLLING_INTERVAL 1000 /* in milliseconds */
72
73 /* maximum controller queue depth */
74#define MAX_HBA_QUEUE_DEPTH 30000
75#define MAX_CHAIN_DEPTH 100000
76static int max_queue_depth = -1;
77module_param(max_queue_depth, int, 0);
78MODULE_PARM_DESC(max_queue_depth, " max controller queue depth ");
79
80static int max_sgl_entries = -1;
81module_param(max_sgl_entries, int, 0);
82MODULE_PARM_DESC(max_sgl_entries, " max sg entries ");
83
84static int msix_disable = -1;
85module_param(msix_disable, int, 0);
86MODULE_PARM_DESC(msix_disable, " disable msix routed interrupts (default=0)");
87
64038301
SPS
88static int smp_affinity_enable = 1;
89module_param(smp_affinity_enable, int, S_IRUGO);
90MODULE_PARM_DESC(smp_affinity_enable, "SMP affinity feature enable/disbale Default: enable(1)");
91
fb77bb53 92static int max_msix_vectors = -1;
9c500060
SR
93module_param(max_msix_vectors, int, 0);
94MODULE_PARM_DESC(max_msix_vectors,
fb77bb53 95 " max msix vectors");
f92363d1
SR
96
97static int mpt3sas_fwfault_debug;
98MODULE_PARM_DESC(mpt3sas_fwfault_debug,
99 " enable detection of firmware fault and halt firmware - (default=0)");
100
9b05c91a 101static int
98c56ad3 102_base_get_ioc_facts(struct MPT3SAS_ADAPTER *ioc);
f92363d1
SR
103
104/**
105 * _scsih_set_fwfault_debug - global setting of ioc->fwfault_debug.
106 *
107 */
108static int
e4dca7b7 109_scsih_set_fwfault_debug(const char *val, const struct kernel_param *kp)
f92363d1
SR
110{
111 int ret = param_set_int(val, kp);
112 struct MPT3SAS_ADAPTER *ioc;
113
114 if (ret)
115 return ret;
116
08c4d550 117 /* global ioc spinlock to protect controller list on list operations */
f92363d1 118 pr_info("setting fwfault_debug(%d)\n", mpt3sas_fwfault_debug);
08c4d550 119 spin_lock(&gioc_lock);
f92363d1
SR
120 list_for_each_entry(ioc, &mpt3sas_ioc_list, list)
121 ioc->fwfault_debug = mpt3sas_fwfault_debug;
08c4d550 122 spin_unlock(&gioc_lock);
f92363d1
SR
123 return 0;
124}
125module_param_call(mpt3sas_fwfault_debug, _scsih_set_fwfault_debug,
126 param_get_int, &mpt3sas_fwfault_debug, 0644);
127
128/**
129 * mpt3sas_remove_dead_ioc_func - kthread context to remove dead ioc
130 * @arg: input argument, used to derive ioc
131 *
132 * Return 0 if controller is removed from pci subsystem.
133 * Return -1 for other case.
134 */
135static int mpt3sas_remove_dead_ioc_func(void *arg)
136{
137 struct MPT3SAS_ADAPTER *ioc = (struct MPT3SAS_ADAPTER *)arg;
138 struct pci_dev *pdev;
139
140 if ((ioc == NULL))
141 return -1;
142
143 pdev = ioc->pdev;
144 if ((pdev == NULL))
145 return -1;
64cdb418 146 pci_stop_and_remove_bus_device_locked(pdev);
f92363d1
SR
147 return 0;
148}
149
150/**
151 * _base_fault_reset_work - workq handling ioc fault conditions
152 * @work: input argument, used to derive ioc
153 * Context: sleep.
154 *
155 * Return nothing.
156 */
157static void
158_base_fault_reset_work(struct work_struct *work)
159{
160 struct MPT3SAS_ADAPTER *ioc =
161 container_of(work, struct MPT3SAS_ADAPTER, fault_reset_work.work);
162 unsigned long flags;
163 u32 doorbell;
164 int rc;
165 struct task_struct *p;
166
167
168 spin_lock_irqsave(&ioc->ioc_reset_in_progress_lock, flags);
16e179bd 169 if (ioc->shost_recovery || ioc->pci_error_recovery)
f92363d1
SR
170 goto rearm_timer;
171 spin_unlock_irqrestore(&ioc->ioc_reset_in_progress_lock, flags);
172
173 doorbell = mpt3sas_base_get_iocstate(ioc, 0);
174 if ((doorbell & MPI2_IOC_STATE_MASK) == MPI2_IOC_STATE_MASK) {
175 pr_err(MPT3SAS_FMT "SAS host is non-operational !!!!\n",
176 ioc->name);
177
16e179bd
SR
178 /* It may be possible that EEH recovery can resolve some of
179 * pci bus failure issues rather removing the dead ioc function
180 * by considering controller is in a non-operational state. So
181 * here priority is given to the EEH recovery. If it doesn't
182 * not resolve this issue, mpt3sas driver will consider this
183 * controller to non-operational state and remove the dead ioc
184 * function.
185 */
186 if (ioc->non_operational_loop++ < 5) {
187 spin_lock_irqsave(&ioc->ioc_reset_in_progress_lock,
188 flags);
189 goto rearm_timer;
190 }
191
f92363d1
SR
192 /*
193 * Call _scsih_flush_pending_cmds callback so that we flush all
194 * pending commands back to OS. This call is required to aovid
195 * deadlock at block layer. Dead IOC will fail to do diag reset,
196 * and this call is safe since dead ioc will never return any
197 * command back from HW.
198 */
199 ioc->schedule_dead_ioc_flush_running_cmds(ioc);
200 /*
201 * Set remove_host flag early since kernel thread will
202 * take some time to execute.
203 */
204 ioc->remove_host = 1;
205 /*Remove the Dead Host */
206 p = kthread_run(mpt3sas_remove_dead_ioc_func, ioc,
c84b06a4 207 "%s_dead_ioc_%d", ioc->driver_name, ioc->id);
f92363d1
SR
208 if (IS_ERR(p))
209 pr_err(MPT3SAS_FMT
210 "%s: Running mpt3sas_dead_ioc thread failed !!!!\n",
211 ioc->name, __func__);
212 else
213 pr_err(MPT3SAS_FMT
214 "%s: Running mpt3sas_dead_ioc thread success !!!!\n",
215 ioc->name, __func__);
216 return; /* don't rearm timer */
217 }
218
16e179bd
SR
219 ioc->non_operational_loop = 0;
220
f92363d1 221 if ((doorbell & MPI2_IOC_STATE_MASK) != MPI2_IOC_STATE_OPERATIONAL) {
98c56ad3 222 rc = mpt3sas_base_hard_reset_handler(ioc, FORCE_BIG_HAMMER);
f92363d1
SR
223 pr_warn(MPT3SAS_FMT "%s: hard reset: %s\n", ioc->name,
224 __func__, (rc == 0) ? "success" : "failed");
225 doorbell = mpt3sas_base_get_iocstate(ioc, 0);
226 if ((doorbell & MPI2_IOC_STATE_MASK) == MPI2_IOC_STATE_FAULT)
227 mpt3sas_base_fault_info(ioc, doorbell &
228 MPI2_DOORBELL_DATA_MASK);
229 if (rc && (doorbell & MPI2_IOC_STATE_MASK) !=
230 MPI2_IOC_STATE_OPERATIONAL)
231 return; /* don't rearm timer */
232 }
233
234 spin_lock_irqsave(&ioc->ioc_reset_in_progress_lock, flags);
235 rearm_timer:
236 if (ioc->fault_reset_work_q)
237 queue_delayed_work(ioc->fault_reset_work_q,
238 &ioc->fault_reset_work,
239 msecs_to_jiffies(FAULT_POLLING_INTERVAL));
240 spin_unlock_irqrestore(&ioc->ioc_reset_in_progress_lock, flags);
241}
242
243/**
244 * mpt3sas_base_start_watchdog - start the fault_reset_work_q
245 * @ioc: per adapter object
246 * Context: sleep.
247 *
248 * Return nothing.
249 */
250void
251mpt3sas_base_start_watchdog(struct MPT3SAS_ADAPTER *ioc)
252{
253 unsigned long flags;
254
255 if (ioc->fault_reset_work_q)
256 return;
257
258 /* initialize fault polling */
259
260 INIT_DELAYED_WORK(&ioc->fault_reset_work, _base_fault_reset_work);
261 snprintf(ioc->fault_reset_work_q_name,
c84b06a4
SR
262 sizeof(ioc->fault_reset_work_q_name), "poll_%s%d_status",
263 ioc->driver_name, ioc->id);
f92363d1
SR
264 ioc->fault_reset_work_q =
265 create_singlethread_workqueue(ioc->fault_reset_work_q_name);
266 if (!ioc->fault_reset_work_q) {
267 pr_err(MPT3SAS_FMT "%s: failed (line=%d)\n",
268 ioc->name, __func__, __LINE__);
269 return;
270 }
271 spin_lock_irqsave(&ioc->ioc_reset_in_progress_lock, flags);
272 if (ioc->fault_reset_work_q)
273 queue_delayed_work(ioc->fault_reset_work_q,
274 &ioc->fault_reset_work,
275 msecs_to_jiffies(FAULT_POLLING_INTERVAL));
276 spin_unlock_irqrestore(&ioc->ioc_reset_in_progress_lock, flags);
277}
278
279/**
280 * mpt3sas_base_stop_watchdog - stop the fault_reset_work_q
281 * @ioc: per adapter object
282 * Context: sleep.
283 *
284 * Return nothing.
285 */
286void
287mpt3sas_base_stop_watchdog(struct MPT3SAS_ADAPTER *ioc)
288{
289 unsigned long flags;
290 struct workqueue_struct *wq;
291
292 spin_lock_irqsave(&ioc->ioc_reset_in_progress_lock, flags);
293 wq = ioc->fault_reset_work_q;
294 ioc->fault_reset_work_q = NULL;
295 spin_unlock_irqrestore(&ioc->ioc_reset_in_progress_lock, flags);
296 if (wq) {
4dc06fd8 297 if (!cancel_delayed_work_sync(&ioc->fault_reset_work))
f92363d1
SR
298 flush_workqueue(wq);
299 destroy_workqueue(wq);
300 }
301}
302
303/**
304 * mpt3sas_base_fault_info - verbose translation of firmware FAULT code
305 * @ioc: per adapter object
306 * @fault_code: fault code
307 *
308 * Return nothing.
309 */
310void
311mpt3sas_base_fault_info(struct MPT3SAS_ADAPTER *ioc , u16 fault_code)
312{
313 pr_err(MPT3SAS_FMT "fault_state(0x%04x)!\n",
314 ioc->name, fault_code);
315}
316
317/**
318 * mpt3sas_halt_firmware - halt's mpt controller firmware
319 * @ioc: per adapter object
320 *
321 * For debugging timeout related issues. Writing 0xCOFFEE00
322 * to the doorbell register will halt controller firmware. With
323 * the purpose to stop both driver and firmware, the enduser can
324 * obtain a ring buffer from controller UART.
325 */
326void
327mpt3sas_halt_firmware(struct MPT3SAS_ADAPTER *ioc)
328{
329 u32 doorbell;
330
331 if (!ioc->fwfault_debug)
332 return;
333
334 dump_stack();
335
336 doorbell = readl(&ioc->chip->Doorbell);
337 if ((doorbell & MPI2_IOC_STATE_MASK) == MPI2_IOC_STATE_FAULT)
338 mpt3sas_base_fault_info(ioc , doorbell);
339 else {
340 writel(0xC0FFEE00, &ioc->chip->Doorbell);
341 pr_err(MPT3SAS_FMT "Firmware is halted due to command timeout\n",
342 ioc->name);
343 }
344
345 if (ioc->fwfault_debug == 2)
346 for (;;)
347 ;
348 else
349 panic("panic in %s\n", __func__);
350}
351
f92363d1
SR
352/**
353 * _base_sas_ioc_info - verbose translation of the ioc status
354 * @ioc: per adapter object
355 * @mpi_reply: reply mf payload returned from firmware
356 * @request_hdr: request mf
357 *
358 * Return nothing.
359 */
360static void
361_base_sas_ioc_info(struct MPT3SAS_ADAPTER *ioc, MPI2DefaultReply_t *mpi_reply,
362 MPI2RequestHeader_t *request_hdr)
363{
364 u16 ioc_status = le16_to_cpu(mpi_reply->IOCStatus) &
365 MPI2_IOCSTATUS_MASK;
366 char *desc = NULL;
367 u16 frame_sz;
368 char *func_str = NULL;
369
370 /* SCSI_IO, RAID_PASS are handled from _scsih_scsi_ioc_info */
371 if (request_hdr->Function == MPI2_FUNCTION_SCSI_IO_REQUEST ||
372 request_hdr->Function == MPI2_FUNCTION_RAID_SCSI_IO_PASSTHROUGH ||
373 request_hdr->Function == MPI2_FUNCTION_EVENT_NOTIFICATION)
374 return;
375
376 if (ioc_status == MPI2_IOCSTATUS_CONFIG_INVALID_PAGE)
377 return;
378
379 switch (ioc_status) {
380
381/****************************************************************************
382* Common IOCStatus values for all replies
383****************************************************************************/
384
385 case MPI2_IOCSTATUS_INVALID_FUNCTION:
386 desc = "invalid function";
387 break;
388 case MPI2_IOCSTATUS_BUSY:
389 desc = "busy";
390 break;
391 case MPI2_IOCSTATUS_INVALID_SGL:
392 desc = "invalid sgl";
393 break;
394 case MPI2_IOCSTATUS_INTERNAL_ERROR:
395 desc = "internal error";
396 break;
397 case MPI2_IOCSTATUS_INVALID_VPID:
398 desc = "invalid vpid";
399 break;
400 case MPI2_IOCSTATUS_INSUFFICIENT_RESOURCES:
401 desc = "insufficient resources";
402 break;
b130b0d5
SS
403 case MPI2_IOCSTATUS_INSUFFICIENT_POWER:
404 desc = "insufficient power";
405 break;
f92363d1
SR
406 case MPI2_IOCSTATUS_INVALID_FIELD:
407 desc = "invalid field";
408 break;
409 case MPI2_IOCSTATUS_INVALID_STATE:
410 desc = "invalid state";
411 break;
412 case MPI2_IOCSTATUS_OP_STATE_NOT_SUPPORTED:
413 desc = "op state not supported";
414 break;
415
416/****************************************************************************
417* Config IOCStatus values
418****************************************************************************/
419
420 case MPI2_IOCSTATUS_CONFIG_INVALID_ACTION:
421 desc = "config invalid action";
422 break;
423 case MPI2_IOCSTATUS_CONFIG_INVALID_TYPE:
424 desc = "config invalid type";
425 break;
426 case MPI2_IOCSTATUS_CONFIG_INVALID_PAGE:
427 desc = "config invalid page";
428 break;
429 case MPI2_IOCSTATUS_CONFIG_INVALID_DATA:
430 desc = "config invalid data";
431 break;
432 case MPI2_IOCSTATUS_CONFIG_NO_DEFAULTS:
433 desc = "config no defaults";
434 break;
435 case MPI2_IOCSTATUS_CONFIG_CANT_COMMIT:
436 desc = "config cant commit";
437 break;
438
439/****************************************************************************
440* SCSI IO Reply
441****************************************************************************/
442
443 case MPI2_IOCSTATUS_SCSI_RECOVERED_ERROR:
444 case MPI2_IOCSTATUS_SCSI_INVALID_DEVHANDLE:
445 case MPI2_IOCSTATUS_SCSI_DEVICE_NOT_THERE:
446 case MPI2_IOCSTATUS_SCSI_DATA_OVERRUN:
447 case MPI2_IOCSTATUS_SCSI_DATA_UNDERRUN:
448 case MPI2_IOCSTATUS_SCSI_IO_DATA_ERROR:
449 case MPI2_IOCSTATUS_SCSI_PROTOCOL_ERROR:
450 case MPI2_IOCSTATUS_SCSI_TASK_TERMINATED:
451 case MPI2_IOCSTATUS_SCSI_RESIDUAL_MISMATCH:
452 case MPI2_IOCSTATUS_SCSI_TASK_MGMT_FAILED:
453 case MPI2_IOCSTATUS_SCSI_IOC_TERMINATED:
454 case MPI2_IOCSTATUS_SCSI_EXT_TERMINATED:
455 break;
456
457/****************************************************************************
458* For use by SCSI Initiator and SCSI Target end-to-end data protection
459****************************************************************************/
460
461 case MPI2_IOCSTATUS_EEDP_GUARD_ERROR:
462 desc = "eedp guard error";
463 break;
464 case MPI2_IOCSTATUS_EEDP_REF_TAG_ERROR:
465 desc = "eedp ref tag error";
466 break;
467 case MPI2_IOCSTATUS_EEDP_APP_TAG_ERROR:
468 desc = "eedp app tag error";
469 break;
470
471/****************************************************************************
472* SCSI Target values
473****************************************************************************/
474
475 case MPI2_IOCSTATUS_TARGET_INVALID_IO_INDEX:
476 desc = "target invalid io index";
477 break;
478 case MPI2_IOCSTATUS_TARGET_ABORTED:
479 desc = "target aborted";
480 break;
481 case MPI2_IOCSTATUS_TARGET_NO_CONN_RETRYABLE:
482 desc = "target no conn retryable";
483 break;
484 case MPI2_IOCSTATUS_TARGET_NO_CONNECTION:
485 desc = "target no connection";
486 break;
487 case MPI2_IOCSTATUS_TARGET_XFER_COUNT_MISMATCH:
488 desc = "target xfer count mismatch";
489 break;
490 case MPI2_IOCSTATUS_TARGET_DATA_OFFSET_ERROR:
491 desc = "target data offset error";
492 break;
493 case MPI2_IOCSTATUS_TARGET_TOO_MUCH_WRITE_DATA:
494 desc = "target too much write data";
495 break;
496 case MPI2_IOCSTATUS_TARGET_IU_TOO_SHORT:
497 desc = "target iu too short";
498 break;
499 case MPI2_IOCSTATUS_TARGET_ACK_NAK_TIMEOUT:
500 desc = "target ack nak timeout";
501 break;
502 case MPI2_IOCSTATUS_TARGET_NAK_RECEIVED:
503 desc = "target nak received";
504 break;
505
506/****************************************************************************
507* Serial Attached SCSI values
508****************************************************************************/
509
510 case MPI2_IOCSTATUS_SAS_SMP_REQUEST_FAILED:
511 desc = "smp request failed";
512 break;
513 case MPI2_IOCSTATUS_SAS_SMP_DATA_OVERRUN:
514 desc = "smp data overrun";
515 break;
516
517/****************************************************************************
518* Diagnostic Buffer Post / Diagnostic Release values
519****************************************************************************/
520
521 case MPI2_IOCSTATUS_DIAGNOSTIC_RELEASED:
522 desc = "diagnostic released";
523 break;
524 default:
525 break;
526 }
527
528 if (!desc)
529 return;
530
531 switch (request_hdr->Function) {
532 case MPI2_FUNCTION_CONFIG:
533 frame_sz = sizeof(Mpi2ConfigRequest_t) + ioc->sge_size;
534 func_str = "config_page";
535 break;
536 case MPI2_FUNCTION_SCSI_TASK_MGMT:
537 frame_sz = sizeof(Mpi2SCSITaskManagementRequest_t);
538 func_str = "task_mgmt";
539 break;
540 case MPI2_FUNCTION_SAS_IO_UNIT_CONTROL:
541 frame_sz = sizeof(Mpi2SasIoUnitControlRequest_t);
542 func_str = "sas_iounit_ctl";
543 break;
544 case MPI2_FUNCTION_SCSI_ENCLOSURE_PROCESSOR:
545 frame_sz = sizeof(Mpi2SepRequest_t);
546 func_str = "enclosure";
547 break;
548 case MPI2_FUNCTION_IOC_INIT:
549 frame_sz = sizeof(Mpi2IOCInitRequest_t);
550 func_str = "ioc_init";
551 break;
552 case MPI2_FUNCTION_PORT_ENABLE:
553 frame_sz = sizeof(Mpi2PortEnableRequest_t);
554 func_str = "port_enable";
555 break;
556 case MPI2_FUNCTION_SMP_PASSTHROUGH:
557 frame_sz = sizeof(Mpi2SmpPassthroughRequest_t) + ioc->sge_size;
558 func_str = "smp_passthru";
559 break;
aff39e61
SPS
560 case MPI2_FUNCTION_NVME_ENCAPSULATED:
561 frame_sz = sizeof(Mpi26NVMeEncapsulatedRequest_t) +
562 ioc->sge_size;
563 func_str = "nvme_encapsulated";
564 break;
f92363d1
SR
565 default:
566 frame_sz = 32;
567 func_str = "unknown";
568 break;
569 }
570
571 pr_warn(MPT3SAS_FMT "ioc_status: %s(0x%04x), request(0x%p),(%s)\n",
572 ioc->name, desc, ioc_status, request_hdr, func_str);
573
574 _debug_dump_mf(request_hdr, frame_sz/4);
575}
576
577/**
578 * _base_display_event_data - verbose translation of firmware asyn events
579 * @ioc: per adapter object
580 * @mpi_reply: reply mf payload returned from firmware
581 *
582 * Return nothing.
583 */
584static void
585_base_display_event_data(struct MPT3SAS_ADAPTER *ioc,
586 Mpi2EventNotificationReply_t *mpi_reply)
587{
588 char *desc = NULL;
589 u16 event;
590
591 if (!(ioc->logging_level & MPT_DEBUG_EVENTS))
592 return;
593
594 event = le16_to_cpu(mpi_reply->Event);
595
596 switch (event) {
597 case MPI2_EVENT_LOG_DATA:
598 desc = "Log Data";
599 break;
600 case MPI2_EVENT_STATE_CHANGE:
601 desc = "Status Change";
602 break;
603 case MPI2_EVENT_HARD_RESET_RECEIVED:
604 desc = "Hard Reset Received";
605 break;
606 case MPI2_EVENT_EVENT_CHANGE:
607 desc = "Event Change";
608 break;
609 case MPI2_EVENT_SAS_DEVICE_STATUS_CHANGE:
610 desc = "Device Status Change";
611 break;
612 case MPI2_EVENT_IR_OPERATION_STATUS:
7786ab6a
SR
613 if (!ioc->hide_ir_msg)
614 desc = "IR Operation Status";
f92363d1
SR
615 break;
616 case MPI2_EVENT_SAS_DISCOVERY:
617 {
618 Mpi2EventDataSasDiscovery_t *event_data =
619 (Mpi2EventDataSasDiscovery_t *)mpi_reply->EventData;
620 pr_info(MPT3SAS_FMT "Discovery: (%s)", ioc->name,
621 (event_data->ReasonCode == MPI2_EVENT_SAS_DISC_RC_STARTED) ?
622 "start" : "stop");
623 if (event_data->DiscoveryStatus)
bbaf61e2 624 pr_cont(" discovery_status(0x%08x)",
f92363d1 625 le32_to_cpu(event_data->DiscoveryStatus));
bbaf61e2 626 pr_cont("\n");
f92363d1
SR
627 return;
628 }
629 case MPI2_EVENT_SAS_BROADCAST_PRIMITIVE:
630 desc = "SAS Broadcast Primitive";
631 break;
632 case MPI2_EVENT_SAS_INIT_DEVICE_STATUS_CHANGE:
633 desc = "SAS Init Device Status Change";
634 break;
635 case MPI2_EVENT_SAS_INIT_TABLE_OVERFLOW:
636 desc = "SAS Init Table Overflow";
637 break;
638 case MPI2_EVENT_SAS_TOPOLOGY_CHANGE_LIST:
639 desc = "SAS Topology Change List";
640 break;
641 case MPI2_EVENT_SAS_ENCL_DEVICE_STATUS_CHANGE:
642 desc = "SAS Enclosure Device Status Change";
643 break;
644 case MPI2_EVENT_IR_VOLUME:
7786ab6a
SR
645 if (!ioc->hide_ir_msg)
646 desc = "IR Volume";
f92363d1
SR
647 break;
648 case MPI2_EVENT_IR_PHYSICAL_DISK:
7786ab6a
SR
649 if (!ioc->hide_ir_msg)
650 desc = "IR Physical Disk";
f92363d1
SR
651 break;
652 case MPI2_EVENT_IR_CONFIGURATION_CHANGE_LIST:
7786ab6a
SR
653 if (!ioc->hide_ir_msg)
654 desc = "IR Configuration Change List";
f92363d1
SR
655 break;
656 case MPI2_EVENT_LOG_ENTRY_ADDED:
7786ab6a
SR
657 if (!ioc->hide_ir_msg)
658 desc = "Log Entry Added";
f92363d1 659 break;
2d8ce8c9
SR
660 case MPI2_EVENT_TEMP_THRESHOLD:
661 desc = "Temperature Threshold";
662 break;
a470a51c 663 case MPI2_EVENT_ACTIVE_CABLE_EXCEPTION:
b99b1993 664 desc = "Cable Event";
a470a51c 665 break;
4318c734
SPS
666 case MPI2_EVENT_PCIE_DEVICE_STATUS_CHANGE:
667 desc = "PCIE Device Status Change";
668 break;
669 case MPI2_EVENT_PCIE_ENUMERATION:
670 {
671 Mpi26EventDataPCIeEnumeration_t *event_data =
672 (Mpi26EventDataPCIeEnumeration_t *)mpi_reply->EventData;
673 pr_info(MPT3SAS_FMT "PCIE Enumeration: (%s)", ioc->name,
674 (event_data->ReasonCode ==
675 MPI26_EVENT_PCIE_ENUM_RC_STARTED) ?
676 "start" : "stop");
677 if (event_data->EnumerationStatus)
678 pr_info("enumeration_status(0x%08x)",
679 le32_to_cpu(event_data->EnumerationStatus));
680 pr_info("\n");
681 return;
682 }
683 case MPI2_EVENT_PCIE_TOPOLOGY_CHANGE_LIST:
684 desc = "PCIE Topology Change List";
685 break;
f92363d1
SR
686 }
687
688 if (!desc)
689 return;
690
691 pr_info(MPT3SAS_FMT "%s\n", ioc->name, desc);
692}
f92363d1
SR
693
694/**
695 * _base_sas_log_info - verbose translation of firmware log info
696 * @ioc: per adapter object
697 * @log_info: log info
698 *
699 * Return nothing.
700 */
701static void
702_base_sas_log_info(struct MPT3SAS_ADAPTER *ioc , u32 log_info)
703{
704 union loginfo_type {
705 u32 loginfo;
706 struct {
707 u32 subcode:16;
708 u32 code:8;
709 u32 originator:4;
710 u32 bus_type:4;
711 } dw;
712 };
713 union loginfo_type sas_loginfo;
714 char *originator_str = NULL;
715
716 sas_loginfo.loginfo = log_info;
717 if (sas_loginfo.dw.bus_type != 3 /*SAS*/)
718 return;
719
720 /* each nexus loss loginfo */
721 if (log_info == 0x31170000)
722 return;
723
724 /* eat the loginfos associated with task aborts */
725 if (ioc->ignore_loginfos && (log_info == 0x30050000 || log_info ==
726 0x31140000 || log_info == 0x31130000))
727 return;
728
729 switch (sas_loginfo.dw.originator) {
730 case 0:
731 originator_str = "IOP";
732 break;
733 case 1:
734 originator_str = "PL";
735 break;
736 case 2:
7786ab6a
SR
737 if (!ioc->hide_ir_msg)
738 originator_str = "IR";
739 else
740 originator_str = "WarpDrive";
f92363d1
SR
741 break;
742 }
743
744 pr_warn(MPT3SAS_FMT
745 "log_info(0x%08x): originator(%s), code(0x%02x), sub_code(0x%04x)\n",
746 ioc->name, log_info,
747 originator_str, sas_loginfo.dw.code,
748 sas_loginfo.dw.subcode);
749}
750
751/**
752 * _base_display_reply_info -
753 * @ioc: per adapter object
754 * @smid: system request message index
755 * @msix_index: MSIX table index supplied by the OS
756 * @reply: reply message frame(lower 32bit addr)
757 *
758 * Return nothing.
759 */
760static void
761_base_display_reply_info(struct MPT3SAS_ADAPTER *ioc, u16 smid, u8 msix_index,
762 u32 reply)
763{
764 MPI2DefaultReply_t *mpi_reply;
765 u16 ioc_status;
766 u32 loginfo = 0;
767
768 mpi_reply = mpt3sas_base_get_reply_virt_addr(ioc, reply);
769 if (unlikely(!mpi_reply)) {
770 pr_err(MPT3SAS_FMT "mpi_reply not valid at %s:%d/%s()!\n",
771 ioc->name, __FILE__, __LINE__, __func__);
772 return;
773 }
774 ioc_status = le16_to_cpu(mpi_reply->IOCStatus);
af009411 775
f92363d1
SR
776 if ((ioc_status & MPI2_IOCSTATUS_MASK) &&
777 (ioc->logging_level & MPT_DEBUG_REPLY)) {
778 _base_sas_ioc_info(ioc , mpi_reply,
779 mpt3sas_base_get_msg_frame(ioc, smid));
780 }
af009411 781
f92363d1
SR
782 if (ioc_status & MPI2_IOCSTATUS_FLAG_LOG_INFO_AVAILABLE) {
783 loginfo = le32_to_cpu(mpi_reply->IOCLogInfo);
784 _base_sas_log_info(ioc, loginfo);
785 }
786
787 if (ioc_status || loginfo) {
788 ioc_status &= MPI2_IOCSTATUS_MASK;
789 mpt3sas_trigger_mpi(ioc, ioc_status, loginfo);
790 }
791}
792
793/**
794 * mpt3sas_base_done - base internal command completion routine
795 * @ioc: per adapter object
796 * @smid: system request message index
797 * @msix_index: MSIX table index supplied by the OS
798 * @reply: reply message frame(lower 32bit addr)
799 *
800 * Return 1 meaning mf should be freed from _base_interrupt
801 * 0 means the mf is freed from this function.
802 */
803u8
804mpt3sas_base_done(struct MPT3SAS_ADAPTER *ioc, u16 smid, u8 msix_index,
805 u32 reply)
806{
807 MPI2DefaultReply_t *mpi_reply;
808
809 mpi_reply = mpt3sas_base_get_reply_virt_addr(ioc, reply);
810 if (mpi_reply && mpi_reply->Function == MPI2_FUNCTION_EVENT_ACK)
fd0331b3 811 return mpt3sas_check_for_pending_internal_cmds(ioc, smid);
f92363d1
SR
812
813 if (ioc->base_cmds.status == MPT3_CMD_NOT_USED)
814 return 1;
815
816 ioc->base_cmds.status |= MPT3_CMD_COMPLETE;
817 if (mpi_reply) {
818 ioc->base_cmds.status |= MPT3_CMD_REPLY_VALID;
819 memcpy(ioc->base_cmds.reply, mpi_reply, mpi_reply->MsgLength*4);
820 }
821 ioc->base_cmds.status &= ~MPT3_CMD_PENDING;
822
823 complete(&ioc->base_cmds.done);
824 return 1;
825}
826
827/**
828 * _base_async_event - main callback handler for firmware asyn events
829 * @ioc: per adapter object
830 * @msix_index: MSIX table index supplied by the OS
831 * @reply: reply message frame(lower 32bit addr)
832 *
833 * Return 1 meaning mf should be freed from _base_interrupt
834 * 0 means the mf is freed from this function.
835 */
836static u8
837_base_async_event(struct MPT3SAS_ADAPTER *ioc, u8 msix_index, u32 reply)
838{
839 Mpi2EventNotificationReply_t *mpi_reply;
840 Mpi2EventAckRequest_t *ack_request;
841 u16 smid;
fd0331b3 842 struct _event_ack_list *delayed_event_ack;
f92363d1
SR
843
844 mpi_reply = mpt3sas_base_get_reply_virt_addr(ioc, reply);
845 if (!mpi_reply)
846 return 1;
847 if (mpi_reply->Function != MPI2_FUNCTION_EVENT_NOTIFICATION)
848 return 1;
af009411 849
f92363d1 850 _base_display_event_data(ioc, mpi_reply);
af009411 851
f92363d1
SR
852 if (!(mpi_reply->AckRequired & MPI2_EVENT_NOTIFICATION_ACK_REQUIRED))
853 goto out;
854 smid = mpt3sas_base_get_smid(ioc, ioc->base_cb_idx);
855 if (!smid) {
fd0331b3
SS
856 delayed_event_ack = kzalloc(sizeof(*delayed_event_ack),
857 GFP_ATOMIC);
858 if (!delayed_event_ack)
859 goto out;
860 INIT_LIST_HEAD(&delayed_event_ack->list);
861 delayed_event_ack->Event = mpi_reply->Event;
862 delayed_event_ack->EventContext = mpi_reply->EventContext;
863 list_add_tail(&delayed_event_ack->list,
864 &ioc->delayed_event_ack_list);
865 dewtprintk(ioc, pr_info(MPT3SAS_FMT
866 "DELAYED: EVENT ACK: event (0x%04x)\n",
867 ioc->name, le16_to_cpu(mpi_reply->Event)));
f92363d1
SR
868 goto out;
869 }
870
871 ack_request = mpt3sas_base_get_msg_frame(ioc, smid);
872 memset(ack_request, 0, sizeof(Mpi2EventAckRequest_t));
873 ack_request->Function = MPI2_FUNCTION_EVENT_ACK;
874 ack_request->Event = mpi_reply->Event;
875 ack_request->EventContext = mpi_reply->EventContext;
876 ack_request->VF_ID = 0; /* TODO */
877 ack_request->VP_ID = 0;
81c16f83 878 ioc->put_smid_default(ioc, smid);
f92363d1
SR
879
880 out:
881
882 /* scsih callback handler */
883 mpt3sas_scsih_event_callback(ioc, msix_index, reply);
884
885 /* ctl callback handler */
886 mpt3sas_ctl_event_callback(ioc, msix_index, reply);
887
888 return 1;
889}
890
12e7c678
HR
891struct scsiio_tracker *
892mpt3sas_get_st_from_smid(struct MPT3SAS_ADAPTER *ioc, u16 smid)
893{
894 if (WARN_ON(!smid) ||
895 WARN_ON(smid >= ioc->hi_priority_smid))
896 return NULL;
897 return &ioc->scsi_lookup[smid - 1];
898}
899
f92363d1
SR
900/**
901 * _base_get_cb_idx - obtain the callback index
902 * @ioc: per adapter object
903 * @smid: system request message index
904 *
905 * Return callback index.
906 */
907static u8
908_base_get_cb_idx(struct MPT3SAS_ADAPTER *ioc, u16 smid)
909{
910 int i;
ba4494d4 911 u8 cb_idx = 0xFF;
f92363d1
SR
912
913 if (smid < ioc->hi_priority_smid) {
12e7c678
HR
914 struct scsiio_tracker *st;
915
916 st = mpt3sas_get_st_from_smid(ioc, smid);
917 if (st)
918 cb_idx = st->cb_idx;
f92363d1
SR
919 } else if (smid < ioc->internal_smid) {
920 i = smid - ioc->hi_priority_smid;
921 cb_idx = ioc->hpr_lookup[i].cb_idx;
922 } else if (smid <= ioc->hba_queue_depth) {
923 i = smid - ioc->internal_smid;
924 cb_idx = ioc->internal_lookup[i].cb_idx;
ba4494d4 925 }
f92363d1
SR
926 return cb_idx;
927}
928
929/**
930 * _base_mask_interrupts - disable interrupts
931 * @ioc: per adapter object
932 *
933 * Disabling ResetIRQ, Reply and Doorbell Interrupts
934 *
935 * Return nothing.
936 */
937static void
938_base_mask_interrupts(struct MPT3SAS_ADAPTER *ioc)
939{
940 u32 him_register;
941
942 ioc->mask_interrupts = 1;
943 him_register = readl(&ioc->chip->HostInterruptMask);
944 him_register |= MPI2_HIM_DIM + MPI2_HIM_RIM + MPI2_HIM_RESET_IRQ_MASK;
945 writel(him_register, &ioc->chip->HostInterruptMask);
946 readl(&ioc->chip->HostInterruptMask);
947}
948
949/**
950 * _base_unmask_interrupts - enable interrupts
951 * @ioc: per adapter object
952 *
953 * Enabling only Reply Interrupts
954 *
955 * Return nothing.
956 */
957static void
958_base_unmask_interrupts(struct MPT3SAS_ADAPTER *ioc)
959{
960 u32 him_register;
961
962 him_register = readl(&ioc->chip->HostInterruptMask);
963 him_register &= ~MPI2_HIM_RIM;
964 writel(him_register, &ioc->chip->HostInterruptMask);
965 ioc->mask_interrupts = 0;
966}
967
968union reply_descriptor {
969 u64 word;
970 struct {
971 u32 low;
972 u32 high;
973 } u;
974};
975
976/**
977 * _base_interrupt - MPT adapter (IOC) specific interrupt handler.
978 * @irq: irq number (not used)
979 * @bus_id: bus identifier cookie == pointer to MPT_ADAPTER structure
980 * @r: pt_regs pointer (not used)
981 *
982 * Return IRQ_HANDLE if processed, else IRQ_NONE.
983 */
984static irqreturn_t
985_base_interrupt(int irq, void *bus_id)
986{
987 struct adapter_reply_queue *reply_q = bus_id;
988 union reply_descriptor rd;
989 u32 completed_cmds;
990 u8 request_desript_type;
991 u16 smid;
992 u8 cb_idx;
993 u32 reply;
994 u8 msix_index = reply_q->msix_index;
995 struct MPT3SAS_ADAPTER *ioc = reply_q->ioc;
996 Mpi2ReplyDescriptorsUnion_t *rpf;
997 u8 rc;
998
999 if (ioc->mask_interrupts)
1000 return IRQ_NONE;
1001
1002 if (!atomic_add_unless(&reply_q->busy, 1, 1))
1003 return IRQ_NONE;
1004
1005 rpf = &reply_q->reply_post_free[reply_q->reply_post_host_index];
1006 request_desript_type = rpf->Default.ReplyFlags
1007 & MPI2_RPY_DESCRIPT_FLAGS_TYPE_MASK;
1008 if (request_desript_type == MPI2_RPY_DESCRIPT_FLAGS_UNUSED) {
1009 atomic_dec(&reply_q->busy);
1010 return IRQ_NONE;
1011 }
1012
1013 completed_cmds = 0;
1014 cb_idx = 0xFF;
1015 do {
1016 rd.word = le64_to_cpu(rpf->Words);
1017 if (rd.u.low == UINT_MAX || rd.u.high == UINT_MAX)
1018 goto out;
1019 reply = 0;
1020 smid = le16_to_cpu(rpf->Default.DescriptorTypeDependent1);
1021 if (request_desript_type ==
1022 MPI25_RPY_DESCRIPT_FLAGS_FAST_PATH_SCSI_IO_SUCCESS ||
1023 request_desript_type ==
aff39e61
SPS
1024 MPI2_RPY_DESCRIPT_FLAGS_SCSI_IO_SUCCESS ||
1025 request_desript_type ==
1026 MPI26_RPY_DESCRIPT_FLAGS_PCIE_ENCAPSULATED_SUCCESS) {
f92363d1
SR
1027 cb_idx = _base_get_cb_idx(ioc, smid);
1028 if ((likely(cb_idx < MPT_MAX_CALLBACKS)) &&
1029 (likely(mpt_callbacks[cb_idx] != NULL))) {
1030 rc = mpt_callbacks[cb_idx](ioc, smid,
1031 msix_index, 0);
1032 if (rc)
1033 mpt3sas_base_free_smid(ioc, smid);
1034 }
1035 } else if (request_desript_type ==
1036 MPI2_RPY_DESCRIPT_FLAGS_ADDRESS_REPLY) {
1037 reply = le32_to_cpu(
1038 rpf->AddressReply.ReplyFrameAddress);
1039 if (reply > ioc->reply_dma_max_address ||
1040 reply < ioc->reply_dma_min_address)
1041 reply = 0;
1042 if (smid) {
1043 cb_idx = _base_get_cb_idx(ioc, smid);
1044 if ((likely(cb_idx < MPT_MAX_CALLBACKS)) &&
1045 (likely(mpt_callbacks[cb_idx] != NULL))) {
1046 rc = mpt_callbacks[cb_idx](ioc, smid,
1047 msix_index, reply);
1048 if (reply)
1049 _base_display_reply_info(ioc,
1050 smid, msix_index, reply);
1051 if (rc)
1052 mpt3sas_base_free_smid(ioc,
1053 smid);
1054 }
1055 } else {
1056 _base_async_event(ioc, msix_index, reply);
1057 }
1058
1059 /* reply free queue handling */
1060 if (reply) {
1061 ioc->reply_free_host_index =
1062 (ioc->reply_free_host_index ==
1063 (ioc->reply_free_queue_depth - 1)) ?
1064 0 : ioc->reply_free_host_index + 1;
1065 ioc->reply_free[ioc->reply_free_host_index] =
1066 cpu_to_le32(reply);
f92363d1
SR
1067 writel(ioc->reply_free_host_index,
1068 &ioc->chip->ReplyFreeHostIndex);
1069 }
1070 }
1071
1072 rpf->Words = cpu_to_le64(ULLONG_MAX);
1073 reply_q->reply_post_host_index =
1074 (reply_q->reply_post_host_index ==
1075 (ioc->reply_post_queue_depth - 1)) ? 0 :
1076 reply_q->reply_post_host_index + 1;
1077 request_desript_type =
1078 reply_q->reply_post_free[reply_q->reply_post_host_index].
1079 Default.ReplyFlags & MPI2_RPY_DESCRIPT_FLAGS_TYPE_MASK;
1080 completed_cmds++;
6b4c335a
C
1081 /* Update the reply post host index after continuously
1082 * processing the threshold number of Reply Descriptors.
1083 * So that FW can find enough entries to post the Reply
1084 * Descriptors in the reply descriptor post queue.
1085 */
1086 if (completed_cmds > ioc->hba_queue_depth/3) {
1087 if (ioc->combined_reply_queue) {
1088 writel(reply_q->reply_post_host_index |
1089 ((msix_index & 7) <<
1090 MPI2_RPHI_MSIX_INDEX_SHIFT),
1091 ioc->replyPostRegisterIndex[msix_index/8]);
1092 } else {
1093 writel(reply_q->reply_post_host_index |
1094 (msix_index <<
1095 MPI2_RPHI_MSIX_INDEX_SHIFT),
1096 &ioc->chip->ReplyPostHostIndex);
1097 }
1098 completed_cmds = 1;
1099 }
f92363d1
SR
1100 if (request_desript_type == MPI2_RPY_DESCRIPT_FLAGS_UNUSED)
1101 goto out;
1102 if (!reply_q->reply_post_host_index)
1103 rpf = reply_q->reply_post_free;
1104 else
1105 rpf++;
1106 } while (1);
1107
1108 out:
1109
1110 if (!completed_cmds) {
1111 atomic_dec(&reply_q->busy);
1112 return IRQ_NONE;
1113 }
1114
7786ab6a
SR
1115 if (ioc->is_warpdrive) {
1116 writel(reply_q->reply_post_host_index,
1117 ioc->reply_post_host_index[msix_index]);
1118 atomic_dec(&reply_q->busy);
1119 return IRQ_HANDLED;
1120 }
fb77bb53
SR
1121
1122 /* Update Reply Post Host Index.
1123 * For those HBA's which support combined reply queue feature
1124 * 1. Get the correct Supplemental Reply Post Host Index Register.
1125 * i.e. (msix_index / 8)th entry from Supplemental Reply Post Host
1126 * Index Register address bank i.e replyPostRegisterIndex[],
1127 * 2. Then update this register with new reply host index value
1128 * in ReplyPostIndex field and the MSIxIndex field with
1129 * msix_index value reduced to a value between 0 and 7,
1130 * using a modulo 8 operation. Since each Supplemental Reply Post
1131 * Host Index Register supports 8 MSI-X vectors.
1132 *
1133 * For other HBA's just update the Reply Post Host Index register with
1134 * new reply host index value in ReplyPostIndex Field and msix_index
1135 * value in MSIxIndex field.
1136 */
0bb337c9 1137 if (ioc->combined_reply_queue)
fb77bb53
SR
1138 writel(reply_q->reply_post_host_index | ((msix_index & 7) <<
1139 MPI2_RPHI_MSIX_INDEX_SHIFT),
1140 ioc->replyPostRegisterIndex[msix_index/8]);
1141 else
1142 writel(reply_q->reply_post_host_index | (msix_index <<
1143 MPI2_RPHI_MSIX_INDEX_SHIFT),
1144 &ioc->chip->ReplyPostHostIndex);
f92363d1
SR
1145 atomic_dec(&reply_q->busy);
1146 return IRQ_HANDLED;
1147}
1148
1149/**
1150 * _base_is_controller_msix_enabled - is controller support muli-reply queues
1151 * @ioc: per adapter object
1152 *
1153 */
1154static inline int
1155_base_is_controller_msix_enabled(struct MPT3SAS_ADAPTER *ioc)
1156{
1157 return (ioc->facts.IOCCapabilities &
1158 MPI2_IOCFACTS_CAPABILITY_MSI_X_INDEX) && ioc->msix_enable;
1159}
1160
1161/**
5f0dfb7a 1162 * mpt3sas_base_sync_reply_irqs - flush pending MSIX interrupts
f92363d1 1163 * @ioc: per adapter object
5f0dfb7a 1164 * Context: non ISR conext
f92363d1 1165 *
5f0dfb7a 1166 * Called when a Task Management request has completed.
f92363d1
SR
1167 *
1168 * Return nothing.
1169 */
1170void
5f0dfb7a 1171mpt3sas_base_sync_reply_irqs(struct MPT3SAS_ADAPTER *ioc)
f92363d1
SR
1172{
1173 struct adapter_reply_queue *reply_q;
1174
1175 /* If MSIX capability is turned off
1176 * then multi-queues are not enabled
1177 */
1178 if (!_base_is_controller_msix_enabled(ioc))
1179 return;
1180
1181 list_for_each_entry(reply_q, &ioc->reply_queue_list, list) {
5f0dfb7a
C
1182 if (ioc->shost_recovery || ioc->remove_host ||
1183 ioc->pci_error_recovery)
f92363d1
SR
1184 return;
1185 /* TMs are on msix_index == 0 */
1186 if (reply_q->msix_index == 0)
1187 continue;
1d55abc0 1188 synchronize_irq(pci_irq_vector(ioc->pdev, reply_q->msix_index));
f92363d1
SR
1189 }
1190}
1191
1192/**
1193 * mpt3sas_base_release_callback_handler - clear interrupt callback handler
1194 * @cb_idx: callback index
1195 *
1196 * Return nothing.
1197 */
1198void
1199mpt3sas_base_release_callback_handler(u8 cb_idx)
1200{
1201 mpt_callbacks[cb_idx] = NULL;
1202}
1203
1204/**
1205 * mpt3sas_base_register_callback_handler - obtain index for the interrupt callback handler
1206 * @cb_func: callback function
1207 *
1208 * Returns cb_func.
1209 */
1210u8
1211mpt3sas_base_register_callback_handler(MPT_CALLBACK cb_func)
1212{
1213 u8 cb_idx;
1214
1215 for (cb_idx = MPT_MAX_CALLBACKS-1; cb_idx; cb_idx--)
1216 if (mpt_callbacks[cb_idx] == NULL)
1217 break;
1218
1219 mpt_callbacks[cb_idx] = cb_func;
1220 return cb_idx;
1221}
1222
1223/**
1224 * mpt3sas_base_initialize_callback_handler - initialize the interrupt callback handler
1225 *
1226 * Return nothing.
1227 */
1228void
1229mpt3sas_base_initialize_callback_handler(void)
1230{
1231 u8 cb_idx;
1232
1233 for (cb_idx = 0; cb_idx < MPT_MAX_CALLBACKS; cb_idx++)
1234 mpt3sas_base_release_callback_handler(cb_idx);
1235}
1236
1237
1238/**
1239 * _base_build_zero_len_sge - build zero length sg entry
1240 * @ioc: per adapter object
1241 * @paddr: virtual address for SGE
1242 *
1243 * Create a zero length scatter gather entry to insure the IOCs hardware has
1244 * something to use if the target device goes brain dead and tries
1245 * to send data even when none is asked for.
1246 *
1247 * Return nothing.
1248 */
1249static void
1250_base_build_zero_len_sge(struct MPT3SAS_ADAPTER *ioc, void *paddr)
1251{
1252 u32 flags_length = (u32)((MPI2_SGE_FLAGS_LAST_ELEMENT |
1253 MPI2_SGE_FLAGS_END_OF_BUFFER | MPI2_SGE_FLAGS_END_OF_LIST |
1254 MPI2_SGE_FLAGS_SIMPLE_ELEMENT) <<
1255 MPI2_SGE_FLAGS_SHIFT);
1256 ioc->base_add_sg_single(paddr, flags_length, -1);
1257}
1258
1259/**
1260 * _base_add_sg_single_32 - Place a simple 32 bit SGE at address pAddr.
1261 * @paddr: virtual address for SGE
1262 * @flags_length: SGE flags and data transfer length
1263 * @dma_addr: Physical address
1264 *
1265 * Return nothing.
1266 */
1267static void
1268_base_add_sg_single_32(void *paddr, u32 flags_length, dma_addr_t dma_addr)
1269{
1270 Mpi2SGESimple32_t *sgel = paddr;
1271
1272 flags_length |= (MPI2_SGE_FLAGS_32_BIT_ADDRESSING |
1273 MPI2_SGE_FLAGS_SYSTEM_ADDRESS) << MPI2_SGE_FLAGS_SHIFT;
1274 sgel->FlagsLength = cpu_to_le32(flags_length);
1275 sgel->Address = cpu_to_le32(dma_addr);
1276}
1277
1278
1279/**
1280 * _base_add_sg_single_64 - Place a simple 64 bit SGE at address pAddr.
1281 * @paddr: virtual address for SGE
1282 * @flags_length: SGE flags and data transfer length
1283 * @dma_addr: Physical address
1284 *
1285 * Return nothing.
1286 */
1287static void
1288_base_add_sg_single_64(void *paddr, u32 flags_length, dma_addr_t dma_addr)
1289{
1290 Mpi2SGESimple64_t *sgel = paddr;
1291
1292 flags_length |= (MPI2_SGE_FLAGS_64_BIT_ADDRESSING |
1293 MPI2_SGE_FLAGS_SYSTEM_ADDRESS) << MPI2_SGE_FLAGS_SHIFT;
1294 sgel->FlagsLength = cpu_to_le32(flags_length);
1295 sgel->Address = cpu_to_le64(dma_addr);
1296}
1297
1298/**
1299 * _base_get_chain_buffer_tracker - obtain chain tracker
1300 * @ioc: per adapter object
1301 * @smid: smid associated to an IO request
1302 *
1303 * Returns chain tracker(from ioc->free_chain_list)
1304 */
1305static struct chain_tracker *
1306_base_get_chain_buffer_tracker(struct MPT3SAS_ADAPTER *ioc, u16 smid)
1307{
1308 struct chain_tracker *chain_req;
12e7c678 1309 struct scsiio_tracker *st;
f92363d1
SR
1310 unsigned long flags;
1311
1312 spin_lock_irqsave(&ioc->scsi_lookup_lock, flags);
1313 if (list_empty(&ioc->free_chain_list)) {
1314 spin_unlock_irqrestore(&ioc->scsi_lookup_lock, flags);
1315 dfailprintk(ioc, pr_warn(MPT3SAS_FMT
1316 "chain buffers not available\n", ioc->name));
1317 return NULL;
1318 }
1319 chain_req = list_entry(ioc->free_chain_list.next,
1320 struct chain_tracker, tracker_list);
1321 list_del_init(&chain_req->tracker_list);
12e7c678
HR
1322 st = mpt3sas_get_st_from_smid(ioc, smid);
1323 if (st)
1324 list_add_tail(&chain_req->tracker_list, &st->chain_list);
f92363d1
SR
1325 spin_unlock_irqrestore(&ioc->scsi_lookup_lock, flags);
1326 return chain_req;
1327}
1328
1329
1330/**
1331 * _base_build_sg - build generic sg
1332 * @ioc: per adapter object
1333 * @psge: virtual address for SGE
1334 * @data_out_dma: physical address for WRITES
1335 * @data_out_sz: data xfer size for WRITES
1336 * @data_in_dma: physical address for READS
1337 * @data_in_sz: data xfer size for READS
1338 *
1339 * Return nothing.
1340 */
1341static void
1342_base_build_sg(struct MPT3SAS_ADAPTER *ioc, void *psge,
1343 dma_addr_t data_out_dma, size_t data_out_sz, dma_addr_t data_in_dma,
1344 size_t data_in_sz)
1345{
1346 u32 sgl_flags;
1347
1348 if (!data_out_sz && !data_in_sz) {
1349 _base_build_zero_len_sge(ioc, psge);
1350 return;
1351 }
1352
1353 if (data_out_sz && data_in_sz) {
1354 /* WRITE sgel first */
1355 sgl_flags = (MPI2_SGE_FLAGS_SIMPLE_ELEMENT |
1356 MPI2_SGE_FLAGS_END_OF_BUFFER | MPI2_SGE_FLAGS_HOST_TO_IOC);
1357 sgl_flags = sgl_flags << MPI2_SGE_FLAGS_SHIFT;
1358 ioc->base_add_sg_single(psge, sgl_flags |
1359 data_out_sz, data_out_dma);
1360
1361 /* incr sgel */
1362 psge += ioc->sge_size;
1363
1364 /* READ sgel last */
1365 sgl_flags = (MPI2_SGE_FLAGS_SIMPLE_ELEMENT |
1366 MPI2_SGE_FLAGS_LAST_ELEMENT | MPI2_SGE_FLAGS_END_OF_BUFFER |
1367 MPI2_SGE_FLAGS_END_OF_LIST);
1368 sgl_flags = sgl_flags << MPI2_SGE_FLAGS_SHIFT;
1369 ioc->base_add_sg_single(psge, sgl_flags |
1370 data_in_sz, data_in_dma);
1371 } else if (data_out_sz) /* WRITE */ {
1372 sgl_flags = (MPI2_SGE_FLAGS_SIMPLE_ELEMENT |
1373 MPI2_SGE_FLAGS_LAST_ELEMENT | MPI2_SGE_FLAGS_END_OF_BUFFER |
1374 MPI2_SGE_FLAGS_END_OF_LIST | MPI2_SGE_FLAGS_HOST_TO_IOC);
1375 sgl_flags = sgl_flags << MPI2_SGE_FLAGS_SHIFT;
1376 ioc->base_add_sg_single(psge, sgl_flags |
1377 data_out_sz, data_out_dma);
1378 } else if (data_in_sz) /* READ */ {
1379 sgl_flags = (MPI2_SGE_FLAGS_SIMPLE_ELEMENT |
1380 MPI2_SGE_FLAGS_LAST_ELEMENT | MPI2_SGE_FLAGS_END_OF_BUFFER |
1381 MPI2_SGE_FLAGS_END_OF_LIST);
1382 sgl_flags = sgl_flags << MPI2_SGE_FLAGS_SHIFT;
1383 ioc->base_add_sg_single(psge, sgl_flags |
1384 data_in_sz, data_in_dma);
1385 }
1386}
1387
aff39e61
SPS
1388/* IEEE format sgls */
1389
1390/**
1391 * _base_build_nvme_prp - This function is called for NVMe end devices to build
1392 * a native SGL (NVMe PRP). The native SGL is built starting in the first PRP
1393 * entry of the NVMe message (PRP1). If the data buffer is small enough to be
1394 * described entirely using PRP1, then PRP2 is not used. If needed, PRP2 is
1395 * used to describe a larger data buffer. If the data buffer is too large to
1396 * describe using the two PRP entriess inside the NVMe message, then PRP1
1397 * describes the first data memory segment, and PRP2 contains a pointer to a PRP
1398 * list located elsewhere in memory to describe the remaining data memory
1399 * segments. The PRP list will be contiguous.
1400
1401 * The native SGL for NVMe devices is a Physical Region Page (PRP). A PRP
1402 * consists of a list of PRP entries to describe a number of noncontigous
1403 * physical memory segments as a single memory buffer, just as a SGL does. Note
1404 * however, that this function is only used by the IOCTL call, so the memory
1405 * given will be guaranteed to be contiguous. There is no need to translate
1406 * non-contiguous SGL into a PRP in this case. All PRPs will describe
1407 * contiguous space that is one page size each.
1408 *
1409 * Each NVMe message contains two PRP entries. The first (PRP1) either contains
1410 * a PRP list pointer or a PRP element, depending upon the command. PRP2
1411 * contains the second PRP element if the memory being described fits within 2
1412 * PRP entries, or a PRP list pointer if the PRP spans more than two entries.
1413 *
1414 * A PRP list pointer contains the address of a PRP list, structured as a linear
1415 * array of PRP entries. Each PRP entry in this list describes a segment of
1416 * physical memory.
1417 *
1418 * Each 64-bit PRP entry comprises an address and an offset field. The address
1419 * always points at the beginning of a 4KB physical memory page, and the offset
1420 * describes where within that 4KB page the memory segment begins. Only the
1421 * first element in a PRP list may contain a non-zero offest, implying that all
1422 * memory segments following the first begin at the start of a 4KB page.
1423 *
1424 * Each PRP element normally describes 4KB of physical memory, with exceptions
1425 * for the first and last elements in the list. If the memory being described
1426 * by the list begins at a non-zero offset within the first 4KB page, then the
1427 * first PRP element will contain a non-zero offset indicating where the region
1428 * begins within the 4KB page. The last memory segment may end before the end
1429 * of the 4KB segment, depending upon the overall size of the memory being
1430 * described by the PRP list.
1431 *
1432 * Since PRP entries lack any indication of size, the overall data buffer length
1433 * is used to determine where the end of the data memory buffer is located, and
1434 * how many PRP entries are required to describe it.
1435 *
1436 * @ioc: per adapter object
1437 * @smid: system request message index for getting asscociated SGL
1438 * @nvme_encap_request: the NVMe request msg frame pointer
1439 * @data_out_dma: physical address for WRITES
1440 * @data_out_sz: data xfer size for WRITES
1441 * @data_in_dma: physical address for READS
1442 * @data_in_sz: data xfer size for READS
1443 *
1444 * Returns nothing.
1445 */
1446static void
1447_base_build_nvme_prp(struct MPT3SAS_ADAPTER *ioc, u16 smid,
1448 Mpi26NVMeEncapsulatedRequest_t *nvme_encap_request,
1449 dma_addr_t data_out_dma, size_t data_out_sz, dma_addr_t data_in_dma,
1450 size_t data_in_sz)
1451{
1452 int prp_size = NVME_PRP_SIZE;
d8335ae2
AB
1453 __le64 *prp_entry, *prp1_entry, *prp2_entry;
1454 __le64 *prp_page;
1455 dma_addr_t prp_entry_dma, prp_page_dma, dma_addr;
aff39e61
SPS
1456 u32 offset, entry_len;
1457 u32 page_mask_result, page_mask;
aff39e61
SPS
1458 size_t length;
1459
1460 /*
1461 * Not all commands require a data transfer. If no data, just return
1462 * without constructing any PRP.
1463 */
1464 if (!data_in_sz && !data_out_sz)
1465 return;
1466 /*
1467 * Set pointers to PRP1 and PRP2, which are in the NVMe command.
1468 * PRP1 is located at a 24 byte offset from the start of the NVMe
1469 * command. Then set the current PRP entry pointer to PRP1.
1470 */
494f401b 1471 prp1_entry = (__le64 *)(nvme_encap_request->NVMe_Command +
aff39e61 1472 NVME_CMD_PRP1_OFFSET);
494f401b 1473 prp2_entry = (__le64 *)(nvme_encap_request->NVMe_Command +
aff39e61
SPS
1474 NVME_CMD_PRP2_OFFSET);
1475 prp_entry = prp1_entry;
1476 /*
1477 * For the PRP entries, use the specially allocated buffer of
1478 * contiguous memory.
1479 */
494f401b 1480 prp_page = (__le64 *)mpt3sas_base_get_pcie_sgl(ioc, smid);
d8335ae2 1481 prp_page_dma = mpt3sas_base_get_pcie_sgl_dma(ioc, smid);
aff39e61
SPS
1482
1483 /*
1484 * Check if we are within 1 entry of a page boundary we don't
1485 * want our first entry to be a PRP List entry.
1486 */
1487 page_mask = ioc->page_size - 1;
1488 page_mask_result = (uintptr_t)((u8 *)prp_page + prp_size) & page_mask;
1489 if (!page_mask_result) {
1490 /* Bump up to next page boundary. */
494f401b 1491 prp_page = (__le64 *)((u8 *)prp_page + prp_size);
d8335ae2 1492 prp_page_dma = prp_page_dma + prp_size;
aff39e61
SPS
1493 }
1494
1495 /*
1496 * Set PRP physical pointer, which initially points to the current PRP
1497 * DMA memory page.
1498 */
d8335ae2 1499 prp_entry_dma = prp_page_dma;
aff39e61
SPS
1500
1501 /* Get physical address and length of the data buffer. */
1502 if (data_in_sz) {
d8335ae2 1503 dma_addr = data_in_dma;
aff39e61
SPS
1504 length = data_in_sz;
1505 } else {
d8335ae2 1506 dma_addr = data_out_dma;
aff39e61
SPS
1507 length = data_out_sz;
1508 }
1509
1510 /* Loop while the length is not zero. */
1511 while (length) {
1512 /*
1513 * Check if we need to put a list pointer here if we are at
1514 * page boundary - prp_size (8 bytes).
1515 */
d8335ae2 1516 page_mask_result = (prp_entry_dma + prp_size) & page_mask;
aff39e61
SPS
1517 if (!page_mask_result) {
1518 /*
1519 * This is the last entry in a PRP List, so we need to
1520 * put a PRP list pointer here. What this does is:
1521 * - bump the current memory pointer to the next
1522 * address, which will be the next full page.
1523 * - set the PRP Entry to point to that page. This
1524 * is now the PRP List pointer.
1525 * - bump the PRP Entry pointer the start of the
1526 * next page. Since all of this PRP memory is
1527 * contiguous, no need to get a new page - it's
1528 * just the next address.
1529 */
d8335ae2
AB
1530 prp_entry_dma++;
1531 *prp_entry = cpu_to_le64(prp_entry_dma);
aff39e61
SPS
1532 prp_entry++;
1533 }
1534
1535 /* Need to handle if entry will be part of a page. */
d8335ae2 1536 offset = dma_addr & page_mask;
aff39e61
SPS
1537 entry_len = ioc->page_size - offset;
1538
1539 if (prp_entry == prp1_entry) {
1540 /*
1541 * Must fill in the first PRP pointer (PRP1) before
1542 * moving on.
1543 */
d8335ae2 1544 *prp1_entry = cpu_to_le64(dma_addr);
aff39e61
SPS
1545
1546 /*
1547 * Now point to the second PRP entry within the
1548 * command (PRP2).
1549 */
1550 prp_entry = prp2_entry;
1551 } else if (prp_entry == prp2_entry) {
1552 /*
1553 * Should the PRP2 entry be a PRP List pointer or just
1554 * a regular PRP pointer? If there is more than one
1555 * more page of data, must use a PRP List pointer.
1556 */
1557 if (length > ioc->page_size) {
1558 /*
1559 * PRP2 will contain a PRP List pointer because
1560 * more PRP's are needed with this command. The
1561 * list will start at the beginning of the
1562 * contiguous buffer.
1563 */
d8335ae2 1564 *prp2_entry = cpu_to_le64(prp_entry_dma);
aff39e61
SPS
1565
1566 /*
1567 * The next PRP Entry will be the start of the
1568 * first PRP List.
1569 */
1570 prp_entry = prp_page;
1571 } else {
1572 /*
1573 * After this, the PRP Entries are complete.
1574 * This command uses 2 PRP's and no PRP list.
1575 */
d8335ae2 1576 *prp2_entry = cpu_to_le64(dma_addr);
aff39e61
SPS
1577 }
1578 } else {
1579 /*
1580 * Put entry in list and bump the addresses.
1581 *
1582 * After PRP1 and PRP2 are filled in, this will fill in
1583 * all remaining PRP entries in a PRP List, one per
1584 * each time through the loop.
1585 */
d8335ae2 1586 *prp_entry = cpu_to_le64(dma_addr);
aff39e61 1587 prp_entry++;
d8335ae2 1588 prp_entry_dma++;
aff39e61
SPS
1589 }
1590
1591 /*
1592 * Bump the phys address of the command's data buffer by the
1593 * entry_len.
1594 */
d8335ae2 1595 dma_addr += entry_len;
aff39e61
SPS
1596
1597 /* Decrement length accounting for last partial page. */
1598 if (entry_len > length)
1599 length = 0;
1600 else
1601 length -= entry_len;
1602 }
1603}
1604
016d5c35
SPS
1605/**
1606 * base_make_prp_nvme -
1607 * Prepare PRPs(Physical Region Page)- SGLs specific to NVMe drives only
1608 *
1609 * @ioc: per adapter object
1610 * @scmd: SCSI command from the mid-layer
1611 * @mpi_request: mpi request
1612 * @smid: msg Index
1613 * @sge_count: scatter gather element count.
1614 *
1615 * Returns: true: PRPs are built
1616 * false: IEEE SGLs needs to be built
1617 */
494f401b 1618static void
016d5c35
SPS
1619base_make_prp_nvme(struct MPT3SAS_ADAPTER *ioc,
1620 struct scsi_cmnd *scmd,
1621 Mpi25SCSIIORequest_t *mpi_request,
1622 u16 smid, int sge_count)
1623{
d8335ae2 1624 int sge_len, num_prp_in_chain = 0;
016d5c35 1625 Mpi25IeeeSgeChain64_t *main_chain_element, *ptr_first_sgl;
494f401b 1626 __le64 *curr_buff;
d8335ae2 1627 dma_addr_t msg_dma, sge_addr, offset;
016d5c35
SPS
1628 u32 page_mask, page_mask_result;
1629 struct scatterlist *sg_scmd;
1630 u32 first_prp_len;
1631 int data_len = scsi_bufflen(scmd);
1632 u32 nvme_pg_size;
1633
1634 nvme_pg_size = max_t(u32, ioc->page_size, NVME_PRP_PAGE_SIZE);
1635 /*
1636 * Nvme has a very convoluted prp format. One prp is required
1637 * for each page or partial page. Driver need to split up OS sg_list
1638 * entries if it is longer than one page or cross a page
1639 * boundary. Driver also have to insert a PRP list pointer entry as
1640 * the last entry in each physical page of the PRP list.
1641 *
1642 * NOTE: The first PRP "entry" is actually placed in the first
1643 * SGL entry in the main message as IEEE 64 format. The 2nd
1644 * entry in the main message is the chain element, and the rest
1645 * of the PRP entries are built in the contiguous pcie buffer.
1646 */
1647 page_mask = nvme_pg_size - 1;
1648
1649 /*
1650 * Native SGL is needed.
1651 * Put a chain element in main message frame that points to the first
1652 * chain buffer.
1653 *
1654 * NOTE: The ChainOffset field must be 0 when using a chain pointer to
1655 * a native SGL.
1656 */
1657
1658 /* Set main message chain element pointer */
1659 main_chain_element = (pMpi25IeeeSgeChain64_t)&mpi_request->SGL;
1660 /*
1661 * For NVMe the chain element needs to be the 2nd SG entry in the main
1662 * message.
1663 */
1664 main_chain_element = (Mpi25IeeeSgeChain64_t *)
1665 ((u8 *)main_chain_element + sizeof(MPI25_IEEE_SGE_CHAIN64));
1666
1667 /*
1668 * For the PRP entries, use the specially allocated buffer of
1669 * contiguous memory. Normal chain buffers can't be used
1670 * because each chain buffer would need to be the size of an OS
1671 * page (4k).
1672 */
1673 curr_buff = mpt3sas_base_get_pcie_sgl(ioc, smid);
d8335ae2 1674 msg_dma = mpt3sas_base_get_pcie_sgl_dma(ioc, smid);
016d5c35 1675
d8335ae2 1676 main_chain_element->Address = cpu_to_le64(msg_dma);
016d5c35
SPS
1677 main_chain_element->NextChainOffset = 0;
1678 main_chain_element->Flags = MPI2_IEEE_SGE_FLAGS_CHAIN_ELEMENT |
1679 MPI2_IEEE_SGE_FLAGS_SYSTEM_ADDR |
1680 MPI26_IEEE_SGE_FLAGS_NSF_NVME_PRP;
1681
1682 /* Build first prp, sge need not to be page aligned*/
1683 ptr_first_sgl = (pMpi25IeeeSgeChain64_t)&mpi_request->SGL;
1684 sg_scmd = scsi_sglist(scmd);
1685 sge_addr = sg_dma_address(sg_scmd);
1686 sge_len = sg_dma_len(sg_scmd);
1687
d8335ae2 1688 offset = sge_addr & page_mask;
016d5c35
SPS
1689 first_prp_len = nvme_pg_size - offset;
1690
1691 ptr_first_sgl->Address = cpu_to_le64(sge_addr);
1692 ptr_first_sgl->Length = cpu_to_le32(first_prp_len);
1693
1694 data_len -= first_prp_len;
1695
1696 if (sge_len > first_prp_len) {
1697 sge_addr += first_prp_len;
1698 sge_len -= first_prp_len;
1699 } else if (data_len && (sge_len == first_prp_len)) {
1700 sg_scmd = sg_next(sg_scmd);
1701 sge_addr = sg_dma_address(sg_scmd);
1702 sge_len = sg_dma_len(sg_scmd);
1703 }
1704
1705 for (;;) {
d8335ae2 1706 offset = sge_addr & page_mask;
016d5c35
SPS
1707
1708 /* Put PRP pointer due to page boundary*/
1709 page_mask_result = (uintptr_t)(curr_buff + 1) & page_mask;
1710 if (unlikely(!page_mask_result)) {
1711 scmd_printk(KERN_NOTICE,
1712 scmd, "page boundary curr_buff: 0x%p\n",
1713 curr_buff);
d8335ae2
AB
1714 msg_dma += 8;
1715 *curr_buff = cpu_to_le64(msg_dma);
016d5c35
SPS
1716 curr_buff++;
1717 num_prp_in_chain++;
1718 }
1719
1720 *curr_buff = cpu_to_le64(sge_addr);
1721 curr_buff++;
d8335ae2 1722 msg_dma += 8;
016d5c35
SPS
1723 num_prp_in_chain++;
1724
1725 sge_addr += nvme_pg_size;
1726 sge_len -= nvme_pg_size;
1727 data_len -= nvme_pg_size;
1728
1729 if (data_len <= 0)
1730 break;
1731
1732 if (sge_len > 0)
1733 continue;
1734
1735 sg_scmd = sg_next(sg_scmd);
1736 sge_addr = sg_dma_address(sg_scmd);
1737 sge_len = sg_dma_len(sg_scmd);
1738 }
1739
1740 main_chain_element->Length =
1741 cpu_to_le32(num_prp_in_chain * sizeof(u64));
1742 return;
1743}
1744
1745static bool
1746base_is_prp_possible(struct MPT3SAS_ADAPTER *ioc,
1747 struct _pcie_device *pcie_device, struct scsi_cmnd *scmd, int sge_count)
1748{
1749 u32 data_length = 0;
1750 struct scatterlist *sg_scmd;
1751 bool build_prp = true;
1752
494f401b 1753 data_length = scsi_bufflen(scmd);
016d5c35
SPS
1754 sg_scmd = scsi_sglist(scmd);
1755
1756 /* If Datalenth is <= 16K and number of SGE’s entries are <= 2
1757 * we built IEEE SGL
1758 */
1759 if ((data_length <= NVME_PRP_PAGE_SIZE*4) && (sge_count <= 2))
1760 build_prp = false;
1761
1762 return build_prp;
1763}
1764
1765/**
1766 * _base_check_pcie_native_sgl - This function is called for PCIe end devices to
1767 * determine if the driver needs to build a native SGL. If so, that native
1768 * SGL is built in the special contiguous buffers allocated especially for
1769 * PCIe SGL creation. If the driver will not build a native SGL, return
1770 * TRUE and a normal IEEE SGL will be built. Currently this routine
1771 * supports NVMe.
1772 * @ioc: per adapter object
1773 * @mpi_request: mf request pointer
1774 * @smid: system request message index
1775 * @scmd: scsi command
1776 * @pcie_device: points to the PCIe device's info
1777 *
1778 * Returns 0 if native SGL was built, 1 if no SGL was built
1779 */
1780static int
1781_base_check_pcie_native_sgl(struct MPT3SAS_ADAPTER *ioc,
1782 Mpi25SCSIIORequest_t *mpi_request, u16 smid, struct scsi_cmnd *scmd,
1783 struct _pcie_device *pcie_device)
1784{
1785 struct scatterlist *sg_scmd;
1786 int sges_left;
1787
1788 /* Get the SG list pointer and info. */
1789 sg_scmd = scsi_sglist(scmd);
1790 sges_left = scsi_dma_map(scmd);
1791 if (sges_left < 0) {
1792 sdev_printk(KERN_ERR, scmd->device,
1793 "scsi_dma_map failed: request for %d bytes!\n",
1794 scsi_bufflen(scmd));
1795 return 1;
1796 }
1797
1798 /* Check if we need to build a native SG list. */
1799 if (base_is_prp_possible(ioc, pcie_device,
1800 scmd, sges_left) == 0) {
1801 /* We built a native SG list, just return. */
1802 goto out;
1803 }
1804
1805 /*
1806 * Build native NVMe PRP.
1807 */
1808 base_make_prp_nvme(ioc, scmd, mpi_request,
1809 smid, sges_left);
1810
1811 return 0;
1812out:
1813 scsi_dma_unmap(scmd);
1814 return 1;
1815}
f92363d1
SR
1816
1817/**
1818 * _base_add_sg_single_ieee - add sg element for IEEE format
1819 * @paddr: virtual address for SGE
1820 * @flags: SGE flags
1821 * @chain_offset: number of 128 byte elements from start of segment
1822 * @length: data transfer length
1823 * @dma_addr: Physical address
1824 *
1825 * Return nothing.
1826 */
1827static void
1828_base_add_sg_single_ieee(void *paddr, u8 flags, u8 chain_offset, u32 length,
1829 dma_addr_t dma_addr)
1830{
1831 Mpi25IeeeSgeChain64_t *sgel = paddr;
1832
1833 sgel->Flags = flags;
1834 sgel->NextChainOffset = chain_offset;
1835 sgel->Length = cpu_to_le32(length);
1836 sgel->Address = cpu_to_le64(dma_addr);
1837}
1838
1839/**
1840 * _base_build_zero_len_sge_ieee - build zero length sg entry for IEEE format
1841 * @ioc: per adapter object
1842 * @paddr: virtual address for SGE
1843 *
1844 * Create a zero length scatter gather entry to insure the IOCs hardware has
1845 * something to use if the target device goes brain dead and tries
1846 * to send data even when none is asked for.
1847 *
1848 * Return nothing.
1849 */
1850static void
1851_base_build_zero_len_sge_ieee(struct MPT3SAS_ADAPTER *ioc, void *paddr)
1852{
1853 u8 sgl_flags = (MPI2_IEEE_SGE_FLAGS_SIMPLE_ELEMENT |
1854 MPI2_IEEE_SGE_FLAGS_SYSTEM_ADDR |
1855 MPI25_IEEE_SGE_FLAGS_END_OF_LIST);
b130b0d5 1856
f92363d1
SR
1857 _base_add_sg_single_ieee(paddr, sgl_flags, 0, 0, -1);
1858}
1859
471ef9d4
SR
1860/**
1861 * _base_build_sg_scmd - main sg creation routine
016d5c35 1862 * pcie_device is unused here!
471ef9d4
SR
1863 * @ioc: per adapter object
1864 * @scmd: scsi command
1865 * @smid: system request message index
016d5c35 1866 * @unused: unused pcie_device pointer
471ef9d4
SR
1867 * Context: none.
1868 *
1869 * The main routine that builds scatter gather table from a given
1870 * scsi request sent via the .queuecommand main handler.
1871 *
1872 * Returns 0 success, anything else error
1873 */
1874static int
1875_base_build_sg_scmd(struct MPT3SAS_ADAPTER *ioc,
016d5c35 1876 struct scsi_cmnd *scmd, u16 smid, struct _pcie_device *unused)
471ef9d4
SR
1877{
1878 Mpi2SCSIIORequest_t *mpi_request;
1879 dma_addr_t chain_dma;
1880 struct scatterlist *sg_scmd;
1881 void *sg_local, *chain;
1882 u32 chain_offset;
1883 u32 chain_length;
1884 u32 chain_flags;
1885 int sges_left;
1886 u32 sges_in_segment;
1887 u32 sgl_flags;
1888 u32 sgl_flags_last_element;
1889 u32 sgl_flags_end_buffer;
1890 struct chain_tracker *chain_req;
1891
1892 mpi_request = mpt3sas_base_get_msg_frame(ioc, smid);
1893
1894 /* init scatter gather flags */
1895 sgl_flags = MPI2_SGE_FLAGS_SIMPLE_ELEMENT;
1896 if (scmd->sc_data_direction == DMA_TO_DEVICE)
1897 sgl_flags |= MPI2_SGE_FLAGS_HOST_TO_IOC;
1898 sgl_flags_last_element = (sgl_flags | MPI2_SGE_FLAGS_LAST_ELEMENT)
1899 << MPI2_SGE_FLAGS_SHIFT;
1900 sgl_flags_end_buffer = (sgl_flags | MPI2_SGE_FLAGS_LAST_ELEMENT |
1901 MPI2_SGE_FLAGS_END_OF_BUFFER | MPI2_SGE_FLAGS_END_OF_LIST)
1902 << MPI2_SGE_FLAGS_SHIFT;
1903 sgl_flags = sgl_flags << MPI2_SGE_FLAGS_SHIFT;
1904
1905 sg_scmd = scsi_sglist(scmd);
1906 sges_left = scsi_dma_map(scmd);
1907 if (sges_left < 0) {
1908 sdev_printk(KERN_ERR, scmd->device,
1909 "pci_map_sg failed: request for %d bytes!\n",
1910 scsi_bufflen(scmd));
1911 return -ENOMEM;
1912 }
1913
1914 sg_local = &mpi_request->SGL;
1915 sges_in_segment = ioc->max_sges_in_main_message;
1916 if (sges_left <= sges_in_segment)
1917 goto fill_in_last_segment;
1918
1919 mpi_request->ChainOffset = (offsetof(Mpi2SCSIIORequest_t, SGL) +
1920 (sges_in_segment * ioc->sge_size))/4;
1921
1922 /* fill in main message segment when there is a chain following */
1923 while (sges_in_segment) {
1924 if (sges_in_segment == 1)
1925 ioc->base_add_sg_single(sg_local,
1926 sgl_flags_last_element | sg_dma_len(sg_scmd),
1927 sg_dma_address(sg_scmd));
1928 else
1929 ioc->base_add_sg_single(sg_local, sgl_flags |
1930 sg_dma_len(sg_scmd), sg_dma_address(sg_scmd));
1931 sg_scmd = sg_next(sg_scmd);
1932 sg_local += ioc->sge_size;
1933 sges_left--;
1934 sges_in_segment--;
1935 }
1936
1937 /* initializing the chain flags and pointers */
1938 chain_flags = MPI2_SGE_FLAGS_CHAIN_ELEMENT << MPI2_SGE_FLAGS_SHIFT;
1939 chain_req = _base_get_chain_buffer_tracker(ioc, smid);
1940 if (!chain_req)
1941 return -1;
1942 chain = chain_req->chain_buffer;
1943 chain_dma = chain_req->chain_buffer_dma;
1944 do {
1945 sges_in_segment = (sges_left <=
1946 ioc->max_sges_in_chain_message) ? sges_left :
1947 ioc->max_sges_in_chain_message;
1948 chain_offset = (sges_left == sges_in_segment) ?
1949 0 : (sges_in_segment * ioc->sge_size)/4;
1950 chain_length = sges_in_segment * ioc->sge_size;
1951 if (chain_offset) {
1952 chain_offset = chain_offset <<
1953 MPI2_SGE_CHAIN_OFFSET_SHIFT;
1954 chain_length += ioc->sge_size;
1955 }
1956 ioc->base_add_sg_single(sg_local, chain_flags | chain_offset |
1957 chain_length, chain_dma);
1958 sg_local = chain;
1959 if (!chain_offset)
1960 goto fill_in_last_segment;
1961
1962 /* fill in chain segments */
1963 while (sges_in_segment) {
1964 if (sges_in_segment == 1)
1965 ioc->base_add_sg_single(sg_local,
1966 sgl_flags_last_element |
1967 sg_dma_len(sg_scmd),
1968 sg_dma_address(sg_scmd));
1969 else
1970 ioc->base_add_sg_single(sg_local, sgl_flags |
1971 sg_dma_len(sg_scmd),
1972 sg_dma_address(sg_scmd));
1973 sg_scmd = sg_next(sg_scmd);
1974 sg_local += ioc->sge_size;
1975 sges_left--;
1976 sges_in_segment--;
1977 }
1978
1979 chain_req = _base_get_chain_buffer_tracker(ioc, smid);
1980 if (!chain_req)
1981 return -1;
1982 chain = chain_req->chain_buffer;
1983 chain_dma = chain_req->chain_buffer_dma;
1984 } while (1);
1985
1986
1987 fill_in_last_segment:
1988
1989 /* fill the last segment */
1990 while (sges_left) {
1991 if (sges_left == 1)
1992 ioc->base_add_sg_single(sg_local, sgl_flags_end_buffer |
1993 sg_dma_len(sg_scmd), sg_dma_address(sg_scmd));
1994 else
1995 ioc->base_add_sg_single(sg_local, sgl_flags |
1996 sg_dma_len(sg_scmd), sg_dma_address(sg_scmd));
1997 sg_scmd = sg_next(sg_scmd);
1998 sg_local += ioc->sge_size;
1999 sges_left--;
2000 }
2001
2002 return 0;
2003}
2004
f92363d1
SR
2005/**
2006 * _base_build_sg_scmd_ieee - main sg creation routine for IEEE format
2007 * @ioc: per adapter object
2008 * @scmd: scsi command
2009 * @smid: system request message index
016d5c35
SPS
2010 * @pcie_device: Pointer to pcie_device. If set, the pcie native sgl will be
2011 * constructed on need.
f92363d1
SR
2012 * Context: none.
2013 *
2014 * The main routine that builds scatter gather table from a given
2015 * scsi request sent via the .queuecommand main handler.
2016 *
2017 * Returns 0 success, anything else error
2018 */
2019static int
2020_base_build_sg_scmd_ieee(struct MPT3SAS_ADAPTER *ioc,
016d5c35 2021 struct scsi_cmnd *scmd, u16 smid, struct _pcie_device *pcie_device)
f92363d1 2022{
016d5c35 2023 Mpi25SCSIIORequest_t *mpi_request;
f92363d1
SR
2024 dma_addr_t chain_dma;
2025 struct scatterlist *sg_scmd;
2026 void *sg_local, *chain;
2027 u32 chain_offset;
2028 u32 chain_length;
f92363d1
SR
2029 int sges_left;
2030 u32 sges_in_segment;
2031 u8 simple_sgl_flags;
2032 u8 simple_sgl_flags_last;
2033 u8 chain_sgl_flags;
2034 struct chain_tracker *chain_req;
2035
2036 mpi_request = mpt3sas_base_get_msg_frame(ioc, smid);
2037
2038 /* init scatter gather flags */
2039 simple_sgl_flags = MPI2_IEEE_SGE_FLAGS_SIMPLE_ELEMENT |
2040 MPI2_IEEE_SGE_FLAGS_SYSTEM_ADDR;
2041 simple_sgl_flags_last = simple_sgl_flags |
2042 MPI25_IEEE_SGE_FLAGS_END_OF_LIST;
2043 chain_sgl_flags = MPI2_IEEE_SGE_FLAGS_CHAIN_ELEMENT |
2044 MPI2_IEEE_SGE_FLAGS_SYSTEM_ADDR;
2045
016d5c35
SPS
2046 /* Check if we need to build a native SG list. */
2047 if ((pcie_device) && (_base_check_pcie_native_sgl(ioc, mpi_request,
2048 smid, scmd, pcie_device) == 0)) {
2049 /* We built a native SG list, just return. */
2050 return 0;
2051 }
2052
f92363d1
SR
2053 sg_scmd = scsi_sglist(scmd);
2054 sges_left = scsi_dma_map(scmd);
62f5c74c 2055 if (sges_left < 0) {
f92363d1
SR
2056 sdev_printk(KERN_ERR, scmd->device,
2057 "pci_map_sg failed: request for %d bytes!\n",
2058 scsi_bufflen(scmd));
2059 return -ENOMEM;
2060 }
2061
2062 sg_local = &mpi_request->SGL;
2063 sges_in_segment = (ioc->request_sz -
016d5c35 2064 offsetof(Mpi25SCSIIORequest_t, SGL))/ioc->sge_size_ieee;
f92363d1
SR
2065 if (sges_left <= sges_in_segment)
2066 goto fill_in_last_segment;
2067
2068 mpi_request->ChainOffset = (sges_in_segment - 1 /* chain element */) +
016d5c35 2069 (offsetof(Mpi25SCSIIORequest_t, SGL)/ioc->sge_size_ieee);
f92363d1
SR
2070
2071 /* fill in main message segment when there is a chain following */
2072 while (sges_in_segment > 1) {
2073 _base_add_sg_single_ieee(sg_local, simple_sgl_flags, 0,
2074 sg_dma_len(sg_scmd), sg_dma_address(sg_scmd));
2075 sg_scmd = sg_next(sg_scmd);
2076 sg_local += ioc->sge_size_ieee;
2077 sges_left--;
2078 sges_in_segment--;
2079 }
2080
25ef16d0 2081 /* initializing the pointers */
f92363d1
SR
2082 chain_req = _base_get_chain_buffer_tracker(ioc, smid);
2083 if (!chain_req)
2084 return -1;
2085 chain = chain_req->chain_buffer;
2086 chain_dma = chain_req->chain_buffer_dma;
2087 do {
2088 sges_in_segment = (sges_left <=
2089 ioc->max_sges_in_chain_message) ? sges_left :
2090 ioc->max_sges_in_chain_message;
2091 chain_offset = (sges_left == sges_in_segment) ?
2092 0 : sges_in_segment;
2093 chain_length = sges_in_segment * ioc->sge_size_ieee;
2094 if (chain_offset)
2095 chain_length += ioc->sge_size_ieee;
2096 _base_add_sg_single_ieee(sg_local, chain_sgl_flags,
2097 chain_offset, chain_length, chain_dma);
2098
2099 sg_local = chain;
2100 if (!chain_offset)
2101 goto fill_in_last_segment;
2102
2103 /* fill in chain segments */
2104 while (sges_in_segment) {
2105 _base_add_sg_single_ieee(sg_local, simple_sgl_flags, 0,
2106 sg_dma_len(sg_scmd), sg_dma_address(sg_scmd));
2107 sg_scmd = sg_next(sg_scmd);
2108 sg_local += ioc->sge_size_ieee;
2109 sges_left--;
2110 sges_in_segment--;
2111 }
2112
2113 chain_req = _base_get_chain_buffer_tracker(ioc, smid);
2114 if (!chain_req)
2115 return -1;
2116 chain = chain_req->chain_buffer;
2117 chain_dma = chain_req->chain_buffer_dma;
2118 } while (1);
2119
2120
2121 fill_in_last_segment:
2122
2123 /* fill the last segment */
62f5c74c 2124 while (sges_left > 0) {
f92363d1
SR
2125 if (sges_left == 1)
2126 _base_add_sg_single_ieee(sg_local,
2127 simple_sgl_flags_last, 0, sg_dma_len(sg_scmd),
2128 sg_dma_address(sg_scmd));
2129 else
2130 _base_add_sg_single_ieee(sg_local, simple_sgl_flags, 0,
2131 sg_dma_len(sg_scmd), sg_dma_address(sg_scmd));
2132 sg_scmd = sg_next(sg_scmd);
2133 sg_local += ioc->sge_size_ieee;
2134 sges_left--;
2135 }
2136
2137 return 0;
2138}
2139
2140/**
2141 * _base_build_sg_ieee - build generic sg for IEEE format
2142 * @ioc: per adapter object
2143 * @psge: virtual address for SGE
2144 * @data_out_dma: physical address for WRITES
2145 * @data_out_sz: data xfer size for WRITES
2146 * @data_in_dma: physical address for READS
2147 * @data_in_sz: data xfer size for READS
2148 *
2149 * Return nothing.
2150 */
2151static void
2152_base_build_sg_ieee(struct MPT3SAS_ADAPTER *ioc, void *psge,
2153 dma_addr_t data_out_dma, size_t data_out_sz, dma_addr_t data_in_dma,
2154 size_t data_in_sz)
2155{
2156 u8 sgl_flags;
2157
2158 if (!data_out_sz && !data_in_sz) {
2159 _base_build_zero_len_sge_ieee(ioc, psge);
2160 return;
2161 }
2162
2163 if (data_out_sz && data_in_sz) {
2164 /* WRITE sgel first */
2165 sgl_flags = MPI2_IEEE_SGE_FLAGS_SIMPLE_ELEMENT |
2166 MPI2_IEEE_SGE_FLAGS_SYSTEM_ADDR;
2167 _base_add_sg_single_ieee(psge, sgl_flags, 0, data_out_sz,
2168 data_out_dma);
2169
2170 /* incr sgel */
2171 psge += ioc->sge_size_ieee;
2172
2173 /* READ sgel last */
2174 sgl_flags |= MPI25_IEEE_SGE_FLAGS_END_OF_LIST;
2175 _base_add_sg_single_ieee(psge, sgl_flags, 0, data_in_sz,
2176 data_in_dma);
2177 } else if (data_out_sz) /* WRITE */ {
2178 sgl_flags = MPI2_IEEE_SGE_FLAGS_SIMPLE_ELEMENT |
2179 MPI25_IEEE_SGE_FLAGS_END_OF_LIST |
2180 MPI2_IEEE_SGE_FLAGS_SYSTEM_ADDR;
2181 _base_add_sg_single_ieee(psge, sgl_flags, 0, data_out_sz,
2182 data_out_dma);
2183 } else if (data_in_sz) /* READ */ {
2184 sgl_flags = MPI2_IEEE_SGE_FLAGS_SIMPLE_ELEMENT |
2185 MPI25_IEEE_SGE_FLAGS_END_OF_LIST |
2186 MPI2_IEEE_SGE_FLAGS_SYSTEM_ADDR;
2187 _base_add_sg_single_ieee(psge, sgl_flags, 0, data_in_sz,
2188 data_in_dma);
2189 }
2190}
2191
2192#define convert_to_kb(x) ((x) << (PAGE_SHIFT - 10))
2193
2194/**
2195 * _base_config_dma_addressing - set dma addressing
2196 * @ioc: per adapter object
2197 * @pdev: PCI device struct
2198 *
2199 * Returns 0 for success, non-zero for failure.
2200 */
2201static int
2202_base_config_dma_addressing(struct MPT3SAS_ADAPTER *ioc, struct pci_dev *pdev)
2203{
2204 struct sysinfo s;
9b05c91a
SR
2205 u64 consistent_dma_mask;
2206
2207 if (ioc->dma_mask)
2208 consistent_dma_mask = DMA_BIT_MASK(64);
2209 else
2210 consistent_dma_mask = DMA_BIT_MASK(32);
f92363d1
SR
2211
2212 if (sizeof(dma_addr_t) > 4) {
2213 const uint64_t required_mask =
2214 dma_get_required_mask(&pdev->dev);
2215 if ((required_mask > DMA_BIT_MASK(32)) &&
2216 !pci_set_dma_mask(pdev, DMA_BIT_MASK(64)) &&
9b05c91a 2217 !pci_set_consistent_dma_mask(pdev, consistent_dma_mask)) {
f92363d1
SR
2218 ioc->base_add_sg_single = &_base_add_sg_single_64;
2219 ioc->sge_size = sizeof(Mpi2SGESimple64_t);
9b05c91a 2220 ioc->dma_mask = 64;
f92363d1
SR
2221 goto out;
2222 }
2223 }
2224
2225 if (!pci_set_dma_mask(pdev, DMA_BIT_MASK(32))
2226 && !pci_set_consistent_dma_mask(pdev, DMA_BIT_MASK(32))) {
2227 ioc->base_add_sg_single = &_base_add_sg_single_32;
2228 ioc->sge_size = sizeof(Mpi2SGESimple32_t);
9b05c91a 2229 ioc->dma_mask = 32;
f92363d1
SR
2230 } else
2231 return -ENODEV;
2232
2233 out:
2234 si_meminfo(&s);
2235 pr_info(MPT3SAS_FMT
9b05c91a
SR
2236 "%d BIT PCI BUS DMA ADDRESSING SUPPORTED, total mem (%ld kB)\n",
2237 ioc->name, ioc->dma_mask, convert_to_kb(s.totalram));
2238
2239 return 0;
2240}
f92363d1 2241
9b05c91a
SR
2242static int
2243_base_change_consistent_dma_mask(struct MPT3SAS_ADAPTER *ioc,
2244 struct pci_dev *pdev)
2245{
2246 if (pci_set_consistent_dma_mask(pdev, DMA_BIT_MASK(64))) {
2247 if (pci_set_consistent_dma_mask(pdev, DMA_BIT_MASK(32)))
2248 return -ENODEV;
2249 }
f92363d1
SR
2250 return 0;
2251}
2252
2253/**
2254 * _base_check_enable_msix - checks MSIX capabable.
2255 * @ioc: per adapter object
2256 *
2257 * Check to see if card is capable of MSIX, and set number
2258 * of available msix vectors
2259 */
2260static int
2261_base_check_enable_msix(struct MPT3SAS_ADAPTER *ioc)
2262{
2263 int base;
2264 u16 message_control;
2265
42081173
SR
2266 /* Check whether controller SAS2008 B0 controller,
2267 * if it is SAS2008 B0 controller use IO-APIC instead of MSIX
2268 */
2269 if (ioc->pdev->device == MPI2_MFGPAGE_DEVID_SAS2008 &&
2270 ioc->pdev->revision == SAS2_PCI_DEVICE_B0_REVISION) {
2271 return -EINVAL;
2272 }
2273
f92363d1
SR
2274 base = pci_find_capability(ioc->pdev, PCI_CAP_ID_MSIX);
2275 if (!base) {
2276 dfailprintk(ioc, pr_info(MPT3SAS_FMT "msix not supported\n",
2277 ioc->name));
2278 return -EINVAL;
2279 }
2280
2281 /* get msix vector count */
42081173
SR
2282 /* NUMA_IO not supported for older controllers */
2283 if (ioc->pdev->device == MPI2_MFGPAGE_DEVID_SAS2004 ||
2284 ioc->pdev->device == MPI2_MFGPAGE_DEVID_SAS2008 ||
2285 ioc->pdev->device == MPI2_MFGPAGE_DEVID_SAS2108_1 ||
2286 ioc->pdev->device == MPI2_MFGPAGE_DEVID_SAS2108_2 ||
2287 ioc->pdev->device == MPI2_MFGPAGE_DEVID_SAS2108_3 ||
2288 ioc->pdev->device == MPI2_MFGPAGE_DEVID_SAS2116_1 ||
2289 ioc->pdev->device == MPI2_MFGPAGE_DEVID_SAS2116_2)
2290 ioc->msix_vector_count = 1;
2291 else {
2292 pci_read_config_word(ioc->pdev, base + 2, &message_control);
2293 ioc->msix_vector_count = (message_control & 0x3FF) + 1;
2294 }
f92363d1
SR
2295 dinitprintk(ioc, pr_info(MPT3SAS_FMT
2296 "msix is supported, vector_count(%d)\n",
2297 ioc->name, ioc->msix_vector_count));
2298 return 0;
2299}
2300
2301/**
2302 * _base_free_irq - free irq
2303 * @ioc: per adapter object
2304 *
2305 * Freeing respective reply_queue from the list.
2306 */
2307static void
2308_base_free_irq(struct MPT3SAS_ADAPTER *ioc)
2309{
2310 struct adapter_reply_queue *reply_q, *next;
2311
2312 if (list_empty(&ioc->reply_queue_list))
2313 return;
2314
2315 list_for_each_entry_safe(reply_q, next, &ioc->reply_queue_list, list) {
2316 list_del(&reply_q->list);
1d55abc0
HR
2317 free_irq(pci_irq_vector(ioc->pdev, reply_q->msix_index),
2318 reply_q);
f92363d1
SR
2319 kfree(reply_q);
2320 }
2321}
2322
2323/**
2324 * _base_request_irq - request irq
2325 * @ioc: per adapter object
2326 * @index: msix index into vector table
f92363d1
SR
2327 *
2328 * Inserting respective reply_queue into the list.
2329 */
2330static int
1d55abc0 2331_base_request_irq(struct MPT3SAS_ADAPTER *ioc, u8 index)
f92363d1 2332{
1d55abc0 2333 struct pci_dev *pdev = ioc->pdev;
f92363d1
SR
2334 struct adapter_reply_queue *reply_q;
2335 int r;
2336
2337 reply_q = kzalloc(sizeof(struct adapter_reply_queue), GFP_KERNEL);
2338 if (!reply_q) {
2339 pr_err(MPT3SAS_FMT "unable to allocate memory %d!\n",
2340 ioc->name, (int)sizeof(struct adapter_reply_queue));
2341 return -ENOMEM;
2342 }
2343 reply_q->ioc = ioc;
2344 reply_q->msix_index = index;
14b3114d 2345
f92363d1
SR
2346 atomic_set(&reply_q->busy, 0);
2347 if (ioc->msix_enable)
2348 snprintf(reply_q->name, MPT_NAME_LENGTH, "%s%d-msix%d",
c84b06a4 2349 ioc->driver_name, ioc->id, index);
f92363d1
SR
2350 else
2351 snprintf(reply_q->name, MPT_NAME_LENGTH, "%s%d",
c84b06a4 2352 ioc->driver_name, ioc->id);
1d55abc0
HR
2353 r = request_irq(pci_irq_vector(pdev, index), _base_interrupt,
2354 IRQF_SHARED, reply_q->name, reply_q);
f92363d1
SR
2355 if (r) {
2356 pr_err(MPT3SAS_FMT "unable to allocate interrupt %d!\n",
1d55abc0 2357 reply_q->name, pci_irq_vector(pdev, index));
da3cec25 2358 kfree(reply_q);
f92363d1
SR
2359 return -EBUSY;
2360 }
2361
2362 INIT_LIST_HEAD(&reply_q->list);
2363 list_add_tail(&reply_q->list, &ioc->reply_queue_list);
2364 return 0;
2365}
2366
2367/**
2368 * _base_assign_reply_queues - assigning msix index for each cpu
2369 * @ioc: per adapter object
2370 *
2371 * The enduser would need to set the affinity via /proc/irq/#/smp_affinity
2372 *
2373 * It would nice if we could call irq_set_affinity, however it is not
2374 * an exported symbol
2375 */
2376static void
2377_base_assign_reply_queues(struct MPT3SAS_ADAPTER *ioc)
2378{
91b265bf 2379 unsigned int cpu, nr_cpus, nr_msix, index = 0;
14b3114d 2380 struct adapter_reply_queue *reply_q;
f92363d1
SR
2381
2382 if (!_base_is_controller_msix_enabled(ioc))
2383 return;
2384
2385 memset(ioc->cpu_msix_table, 0, ioc->cpu_msix_table_sz);
2386
91b265bf
MP
2387 nr_cpus = num_online_cpus();
2388 nr_msix = ioc->reply_queue_count = min(ioc->reply_queue_count,
2389 ioc->facts.MaxMSIxVectors);
2390 if (!nr_msix)
2391 return;
f92363d1 2392
1d55abc0
HR
2393 if (smp_affinity_enable) {
2394 list_for_each_entry(reply_q, &ioc->reply_queue_list, list) {
2395 const cpumask_t *mask = pci_irq_get_affinity(ioc->pdev,
2396 reply_q->msix_index);
2397 if (!mask) {
2398 pr_warn(MPT3SAS_FMT "no affinity for msi %x\n",
2399 ioc->name, reply_q->msix_index);
2400 continue;
2401 }
2402
2403 for_each_cpu(cpu, mask)
2404 ioc->cpu_msix_table[cpu] = reply_q->msix_index;
2405 }
2406 return;
2407 }
91b265bf
MP
2408 cpu = cpumask_first(cpu_online_mask);
2409
14b3114d
SR
2410 list_for_each_entry(reply_q, &ioc->reply_queue_list, list) {
2411
91b265bf
MP
2412 unsigned int i, group = nr_cpus / nr_msix;
2413
14b3114d
SR
2414 if (cpu >= nr_cpus)
2415 break;
2416
91b265bf
MP
2417 if (index < nr_cpus % nr_msix)
2418 group++;
2419
2420 for (i = 0 ; i < group ; i++) {
1d55abc0 2421 ioc->cpu_msix_table[cpu] = reply_q->msix_index;
91b265bf 2422 cpu = cpumask_next(cpu, cpu_online_mask);
f92363d1 2423 }
91b265bf 2424 index++;
14b3114d 2425 }
f92363d1
SR
2426}
2427
2428/**
2429 * _base_disable_msix - disables msix
2430 * @ioc: per adapter object
2431 *
2432 */
2433static void
2434_base_disable_msix(struct MPT3SAS_ADAPTER *ioc)
2435{
2436 if (!ioc->msix_enable)
2437 return;
2438 pci_disable_msix(ioc->pdev);
2439 ioc->msix_enable = 0;
2440}
2441
2442/**
2443 * _base_enable_msix - enables msix, failback to io_apic
2444 * @ioc: per adapter object
2445 *
2446 */
2447static int
2448_base_enable_msix(struct MPT3SAS_ADAPTER *ioc)
2449{
f92363d1 2450 int r;
bb350661 2451 int i, local_max_msix_vectors;
f92363d1 2452 u8 try_msix = 0;
1d55abc0 2453 unsigned int irq_flags = PCI_IRQ_MSIX;
f92363d1 2454
f92363d1
SR
2455 if (msix_disable == -1 || msix_disable == 0)
2456 try_msix = 1;
2457
2458 if (!try_msix)
2459 goto try_ioapic;
2460
2461 if (_base_check_enable_msix(ioc) != 0)
2462 goto try_ioapic;
2463
2464 ioc->reply_queue_count = min_t(int, ioc->cpu_count,
1d55abc0 2465 ioc->msix_vector_count);
f92363d1 2466
9c500060
SR
2467 printk(MPT3SAS_FMT "MSI-X vectors supported: %d, no of cores"
2468 ": %d, max_msix_vectors: %d\n", ioc->name, ioc->msix_vector_count,
2469 ioc->cpu_count, max_msix_vectors);
2470
9b05c91a 2471 if (!ioc->rdpq_array_enable && max_msix_vectors == -1)
06f5f976 2472 local_max_msix_vectors = (reset_devices) ? 1 : 8;
bb350661
SPS
2473 else
2474 local_max_msix_vectors = max_msix_vectors;
9b05c91a 2475
1d55abc0 2476 if (local_max_msix_vectors > 0)
bb350661 2477 ioc->reply_queue_count = min_t(int, local_max_msix_vectors,
9c500060 2478 ioc->reply_queue_count);
1d55abc0 2479 else if (local_max_msix_vectors == 0)
9b05c91a 2480 goto try_ioapic;
9c500060 2481
64038301
SPS
2482 if (ioc->msix_vector_count < ioc->cpu_count)
2483 smp_affinity_enable = 0;
2484
1d55abc0
HR
2485 if (smp_affinity_enable)
2486 irq_flags |= PCI_IRQ_AFFINITY;
f92363d1 2487
1d55abc0
HR
2488 r = pci_alloc_irq_vectors(ioc->pdev, 1, ioc->reply_queue_count,
2489 irq_flags);
2490 if (r < 0) {
f92363d1 2491 dfailprintk(ioc, pr_info(MPT3SAS_FMT
1d55abc0 2492 "pci_alloc_irq_vectors failed (r=%d) !!!\n",
f92363d1 2493 ioc->name, r));
f92363d1
SR
2494 goto try_ioapic;
2495 }
2496
2497 ioc->msix_enable = 1;
1d55abc0
HR
2498 ioc->reply_queue_count = r;
2499 for (i = 0; i < ioc->reply_queue_count; i++) {
2500 r = _base_request_irq(ioc, i);
f92363d1
SR
2501 if (r) {
2502 _base_free_irq(ioc);
2503 _base_disable_msix(ioc);
f92363d1
SR
2504 goto try_ioapic;
2505 }
2506 }
2507
f92363d1
SR
2508 return 0;
2509
2510/* failback to io_apic interrupt routing */
2511 try_ioapic:
2512
9b05c91a 2513 ioc->reply_queue_count = 1;
1d55abc0
HR
2514 r = pci_alloc_irq_vectors(ioc->pdev, 1, 1, PCI_IRQ_LEGACY);
2515 if (r < 0) {
2516 dfailprintk(ioc, pr_info(MPT3SAS_FMT
2517 "pci_alloc_irq_vector(legacy) failed (r=%d) !!!\n",
2518 ioc->name, r));
2519 } else
2520 r = _base_request_irq(ioc, 0);
f92363d1
SR
2521
2522 return r;
2523}
2524
580d4e31
SR
2525/**
2526 * mpt3sas_base_unmap_resources - free controller resources
2527 * @ioc: per adapter object
2528 */
8bbb1cf6 2529static void
580d4e31
SR
2530mpt3sas_base_unmap_resources(struct MPT3SAS_ADAPTER *ioc)
2531{
2532 struct pci_dev *pdev = ioc->pdev;
2533
2534 dexitprintk(ioc, printk(MPT3SAS_FMT "%s\n",
2535 ioc->name, __func__));
2536
2537 _base_free_irq(ioc);
2538 _base_disable_msix(ioc);
2539
0bb337c9 2540 if (ioc->combined_reply_queue) {
580d4e31 2541 kfree(ioc->replyPostRegisterIndex);
5f985d88
TH
2542 ioc->replyPostRegisterIndex = NULL;
2543 }
580d4e31
SR
2544
2545 if (ioc->chip_phys) {
2546 iounmap(ioc->chip);
2547 ioc->chip_phys = 0;
2548 }
2549
2550 if (pci_is_enabled(pdev)) {
2551 pci_release_selected_regions(ioc->pdev, ioc->bars);
2552 pci_disable_pcie_error_reporting(pdev);
2553 pci_disable_device(pdev);
2554 }
2555}
2556
f92363d1
SR
2557/**
2558 * mpt3sas_base_map_resources - map in controller resources (io/irq/memap)
2559 * @ioc: per adapter object
2560 *
2561 * Returns 0 for success, non-zero for failure.
2562 */
2563int
2564mpt3sas_base_map_resources(struct MPT3SAS_ADAPTER *ioc)
2565{
2566 struct pci_dev *pdev = ioc->pdev;
2567 u32 memap_sz;
2568 u32 pio_sz;
2569 int i, r = 0;
2570 u64 pio_chip = 0;
2571 u64 chip_phys = 0;
2572 struct adapter_reply_queue *reply_q;
2573
2574 dinitprintk(ioc, pr_info(MPT3SAS_FMT "%s\n",
2575 ioc->name, __func__));
2576
2577 ioc->bars = pci_select_bars(pdev, IORESOURCE_MEM);
2578 if (pci_enable_device_mem(pdev)) {
2579 pr_warn(MPT3SAS_FMT "pci_enable_device_mem: failed\n",
2580 ioc->name);
cf9bd21a 2581 ioc->bars = 0;
f92363d1
SR
2582 return -ENODEV;
2583 }
2584
2585
2586 if (pci_request_selected_regions(pdev, ioc->bars,
c84b06a4 2587 ioc->driver_name)) {
f92363d1
SR
2588 pr_warn(MPT3SAS_FMT "pci_request_selected_regions: failed\n",
2589 ioc->name);
cf9bd21a 2590 ioc->bars = 0;
f92363d1
SR
2591 r = -ENODEV;
2592 goto out_fail;
2593 }
2594
2595/* AER (Advanced Error Reporting) hooks */
2596 pci_enable_pcie_error_reporting(pdev);
2597
2598 pci_set_master(pdev);
2599
2600
2601 if (_base_config_dma_addressing(ioc, pdev) != 0) {
2602 pr_warn(MPT3SAS_FMT "no suitable DMA mask for %s\n",
2603 ioc->name, pci_name(pdev));
2604 r = -ENODEV;
2605 goto out_fail;
2606 }
2607
5aeeb78a
SR
2608 for (i = 0, memap_sz = 0, pio_sz = 0; (i < DEVICE_COUNT_RESOURCE) &&
2609 (!memap_sz || !pio_sz); i++) {
f92363d1
SR
2610 if (pci_resource_flags(pdev, i) & IORESOURCE_IO) {
2611 if (pio_sz)
2612 continue;
2613 pio_chip = (u64)pci_resource_start(pdev, i);
2614 pio_sz = pci_resource_len(pdev, i);
2615 } else if (pci_resource_flags(pdev, i) & IORESOURCE_MEM) {
2616 if (memap_sz)
2617 continue;
2618 ioc->chip_phys = pci_resource_start(pdev, i);
2619 chip_phys = (u64)ioc->chip_phys;
2620 memap_sz = pci_resource_len(pdev, i);
2621 ioc->chip = ioremap(ioc->chip_phys, memap_sz);
f92363d1
SR
2622 }
2623 }
2624
5aeeb78a
SR
2625 if (ioc->chip == NULL) {
2626 pr_err(MPT3SAS_FMT "unable to map adapter memory! "
2627 " or resource not found\n", ioc->name);
2628 r = -EINVAL;
2629 goto out_fail;
2630 }
2631
f92363d1 2632 _base_mask_interrupts(ioc);
9b05c91a 2633
98c56ad3 2634 r = _base_get_ioc_facts(ioc);
9b05c91a
SR
2635 if (r)
2636 goto out_fail;
2637
2638 if (!ioc->rdpq_array_enable_assigned) {
2639 ioc->rdpq_array_enable = ioc->rdpq_array_capable;
2640 ioc->rdpq_array_enable_assigned = 1;
2641 }
2642
f92363d1
SR
2643 r = _base_enable_msix(ioc);
2644 if (r)
2645 goto out_fail;
2646
fb77bb53
SR
2647 /* Use the Combined reply queue feature only for SAS3 C0 & higher
2648 * revision HBAs and also only when reply queue count is greater than 8
2649 */
0bb337c9 2650 if (ioc->combined_reply_queue && ioc->reply_queue_count > 8) {
fb77bb53
SR
2651 /* Determine the Supplemental Reply Post Host Index Registers
2652 * Addresse. Supplemental Reply Post Host Index Registers
2653 * starts at offset MPI25_SUP_REPLY_POST_HOST_INDEX_OFFSET and
2654 * each register is at offset bytes of
2655 * MPT3_SUP_REPLY_POST_HOST_INDEX_REG_OFFSET from previous one.
2656 */
2657 ioc->replyPostRegisterIndex = kcalloc(
0bb337c9 2658 ioc->combined_reply_index_count,
fb77bb53
SR
2659 sizeof(resource_size_t *), GFP_KERNEL);
2660 if (!ioc->replyPostRegisterIndex) {
2661 dfailprintk(ioc, printk(MPT3SAS_FMT
2662 "allocation for reply Post Register Index failed!!!\n",
2663 ioc->name));
2664 r = -ENOMEM;
2665 goto out_fail;
2666 }
2667
0bb337c9 2668 for (i = 0; i < ioc->combined_reply_index_count; i++) {
fb77bb53
SR
2669 ioc->replyPostRegisterIndex[i] = (resource_size_t *)
2670 ((u8 *)&ioc->chip->Doorbell +
2671 MPI25_SUP_REPLY_POST_HOST_INDEX_OFFSET +
2672 (i * MPT3_SUP_REPLY_POST_HOST_INDEX_REG_OFFSET));
2673 }
2674 } else
0bb337c9 2675 ioc->combined_reply_queue = 0;
fb77bb53 2676
ce7c6c9e
GE
2677 if (ioc->is_warpdrive) {
2678 ioc->reply_post_host_index[0] = (resource_size_t __iomem *)
2679 &ioc->chip->ReplyPostHostIndex;
2680
2681 for (i = 1; i < ioc->cpu_msix_table_sz; i++)
2682 ioc->reply_post_host_index[i] =
2683 (resource_size_t __iomem *)
2684 ((u8 __iomem *)&ioc->chip->Doorbell + (0x4000 + ((i - 1)
2685 * 4)));
2686 }
2687
f92363d1
SR
2688 list_for_each_entry(reply_q, &ioc->reply_queue_list, list)
2689 pr_info(MPT3SAS_FMT "%s: IRQ %d\n",
2690 reply_q->name, ((ioc->msix_enable) ? "PCI-MSI-X enabled" :
1d55abc0
HR
2691 "IO-APIC enabled"),
2692 pci_irq_vector(ioc->pdev, reply_q->msix_index));
f92363d1
SR
2693
2694 pr_info(MPT3SAS_FMT "iomem(0x%016llx), mapped(0x%p), size(%d)\n",
2695 ioc->name, (unsigned long long)chip_phys, ioc->chip, memap_sz);
2696 pr_info(MPT3SAS_FMT "ioport(0x%016llx), size(%d)\n",
2697 ioc->name, (unsigned long long)pio_chip, pio_sz);
2698
2699 /* Save PCI configuration state for recovery from PCI AER/EEH errors */
2700 pci_save_state(pdev);
2701 return 0;
2702
2703 out_fail:
580d4e31 2704 mpt3sas_base_unmap_resources(ioc);
f92363d1
SR
2705 return r;
2706}
2707
2708/**
2709 * mpt3sas_base_get_msg_frame - obtain request mf pointer
2710 * @ioc: per adapter object
2711 * @smid: system request message index(smid zero is invalid)
2712 *
2713 * Returns virt pointer to message frame.
2714 */
2715void *
2716mpt3sas_base_get_msg_frame(struct MPT3SAS_ADAPTER *ioc, u16 smid)
2717{
2718 return (void *)(ioc->request + (smid * ioc->request_sz));
2719}
2720
2721/**
2722 * mpt3sas_base_get_sense_buffer - obtain a sense buffer virt addr
2723 * @ioc: per adapter object
2724 * @smid: system request message index
2725 *
2726 * Returns virt pointer to sense buffer.
2727 */
2728void *
2729mpt3sas_base_get_sense_buffer(struct MPT3SAS_ADAPTER *ioc, u16 smid)
2730{
2731 return (void *)(ioc->sense + ((smid - 1) * SCSI_SENSE_BUFFERSIZE));
2732}
2733
2734/**
2735 * mpt3sas_base_get_sense_buffer_dma - obtain a sense buffer dma addr
2736 * @ioc: per adapter object
2737 * @smid: system request message index
2738 *
2739 * Returns phys pointer to the low 32bit address of the sense buffer.
2740 */
2741__le32
2742mpt3sas_base_get_sense_buffer_dma(struct MPT3SAS_ADAPTER *ioc, u16 smid)
2743{
2744 return cpu_to_le32(ioc->sense_dma + ((smid - 1) *
2745 SCSI_SENSE_BUFFERSIZE));
2746}
2747
016d5c35
SPS
2748/**
2749 * mpt3sas_base_get_pcie_sgl - obtain a PCIe SGL virt addr
2750 * @ioc: per adapter object
2751 * @smid: system request message index
2752 *
2753 * Returns virt pointer to a PCIe SGL.
2754 */
2755void *
2756mpt3sas_base_get_pcie_sgl(struct MPT3SAS_ADAPTER *ioc, u16 smid)
2757{
2758 return (void *)(ioc->scsi_lookup[smid - 1].pcie_sg_list.pcie_sgl);
2759}
2760
2761/**
2762 * mpt3sas_base_get_pcie_sgl_dma - obtain a PCIe SGL dma addr
2763 * @ioc: per adapter object
2764 * @smid: system request message index
2765 *
2766 * Returns phys pointer to the address of the PCIe buffer.
2767 */
d8335ae2 2768dma_addr_t
016d5c35
SPS
2769mpt3sas_base_get_pcie_sgl_dma(struct MPT3SAS_ADAPTER *ioc, u16 smid)
2770{
d8335ae2 2771 return ioc->scsi_lookup[smid - 1].pcie_sg_list.pcie_sgl_dma;
016d5c35
SPS
2772}
2773
f92363d1
SR
2774/**
2775 * mpt3sas_base_get_reply_virt_addr - obtain reply frames virt address
2776 * @ioc: per adapter object
2777 * @phys_addr: lower 32 physical addr of the reply
2778 *
2779 * Converts 32bit lower physical addr into a virt address.
2780 */
2781void *
2782mpt3sas_base_get_reply_virt_addr(struct MPT3SAS_ADAPTER *ioc, u32 phys_addr)
2783{
2784 if (!phys_addr)
2785 return NULL;
2786 return ioc->reply + (phys_addr - (u32)ioc->reply_dma);
2787}
2788
03d1fb3a
SS
2789static inline u8
2790_base_get_msix_index(struct MPT3SAS_ADAPTER *ioc)
2791{
2792 return ioc->cpu_msix_table[raw_smp_processor_id()];
2793}
2794
f92363d1
SR
2795/**
2796 * mpt3sas_base_get_smid - obtain a free smid from internal queue
2797 * @ioc: per adapter object
2798 * @cb_idx: callback index
2799 *
2800 * Returns smid (zero is invalid)
2801 */
2802u16
2803mpt3sas_base_get_smid(struct MPT3SAS_ADAPTER *ioc, u8 cb_idx)
2804{
2805 unsigned long flags;
2806 struct request_tracker *request;
2807 u16 smid;
2808
2809 spin_lock_irqsave(&ioc->scsi_lookup_lock, flags);
2810 if (list_empty(&ioc->internal_free_list)) {
2811 spin_unlock_irqrestore(&ioc->scsi_lookup_lock, flags);
2812 pr_err(MPT3SAS_FMT "%s: smid not available\n",
2813 ioc->name, __func__);
2814 return 0;
2815 }
2816
2817 request = list_entry(ioc->internal_free_list.next,
2818 struct request_tracker, tracker_list);
2819 request->cb_idx = cb_idx;
2820 smid = request->smid;
2821 list_del(&request->tracker_list);
2822 spin_unlock_irqrestore(&ioc->scsi_lookup_lock, flags);
2823 return smid;
2824}
2825
2826/**
2827 * mpt3sas_base_get_smid_scsiio - obtain a free smid from scsiio queue
2828 * @ioc: per adapter object
2829 * @cb_idx: callback index
2830 * @scmd: pointer to scsi command object
2831 *
2832 * Returns smid (zero is invalid)
2833 */
2834u16
2835mpt3sas_base_get_smid_scsiio(struct MPT3SAS_ADAPTER *ioc, u8 cb_idx,
2836 struct scsi_cmnd *scmd)
2837{
2838 unsigned long flags;
2839 struct scsiio_tracker *request;
2840 u16 smid;
2841
2842 spin_lock_irqsave(&ioc->scsi_lookup_lock, flags);
2843 if (list_empty(&ioc->free_list)) {
2844 spin_unlock_irqrestore(&ioc->scsi_lookup_lock, flags);
2845 pr_err(MPT3SAS_FMT "%s: smid not available\n",
2846 ioc->name, __func__);
2847 return 0;
2848 }
2849
2850 request = list_entry(ioc->free_list.next,
2851 struct scsiio_tracker, tracker_list);
2852 request->scmd = scmd;
2853 request->cb_idx = cb_idx;
2854 smid = request->smid;
03d1fb3a 2855 request->msix_io = _base_get_msix_index(ioc);
f92363d1
SR
2856 list_del(&request->tracker_list);
2857 spin_unlock_irqrestore(&ioc->scsi_lookup_lock, flags);
2858 return smid;
2859}
2860
2861/**
2862 * mpt3sas_base_get_smid_hpr - obtain a free smid from hi-priority queue
2863 * @ioc: per adapter object
2864 * @cb_idx: callback index
2865 *
2866 * Returns smid (zero is invalid)
2867 */
2868u16
2869mpt3sas_base_get_smid_hpr(struct MPT3SAS_ADAPTER *ioc, u8 cb_idx)
2870{
2871 unsigned long flags;
2872 struct request_tracker *request;
2873 u16 smid;
2874
2875 spin_lock_irqsave(&ioc->scsi_lookup_lock, flags);
2876 if (list_empty(&ioc->hpr_free_list)) {
2877 spin_unlock_irqrestore(&ioc->scsi_lookup_lock, flags);
2878 return 0;
2879 }
2880
2881 request = list_entry(ioc->hpr_free_list.next,
2882 struct request_tracker, tracker_list);
2883 request->cb_idx = cb_idx;
2884 smid = request->smid;
2885 list_del(&request->tracker_list);
2886 spin_unlock_irqrestore(&ioc->scsi_lookup_lock, flags);
2887 return smid;
2888}
2889
6a2d4618
HR
2890static void
2891_base_recovery_check(struct MPT3SAS_ADAPTER *ioc)
2892{
2893 /*
2894 * See _wait_for_commands_to_complete() call with regards to this code.
2895 */
2896 if (ioc->shost_recovery && ioc->pending_io_count) {
2897 if (ioc->pending_io_count == 1)
2898 wake_up(&ioc->reset_wq);
2899 ioc->pending_io_count--;
2900 }
2901}
2902
f92363d1
SR
2903/**
2904 * mpt3sas_base_free_smid - put smid back on free_list
2905 * @ioc: per adapter object
2906 * @smid: system request message index
2907 *
2908 * Return nothing.
2909 */
2910void
2911mpt3sas_base_free_smid(struct MPT3SAS_ADAPTER *ioc, u16 smid)
2912{
2913 unsigned long flags;
2914 int i;
f92363d1
SR
2915
2916 spin_lock_irqsave(&ioc->scsi_lookup_lock, flags);
2917 if (smid < ioc->hi_priority_smid) {
2918 /* scsiio queue */
2919 i = smid - 1;
05303dfb
HR
2920 list_splice_init(&ioc->scsi_lookup[i].chain_list,
2921 &ioc->free_chain_list);
f92363d1
SR
2922 ioc->scsi_lookup[i].cb_idx = 0xFF;
2923 ioc->scsi_lookup[i].scmd = NULL;
7786ab6a 2924 ioc->scsi_lookup[i].direct_io = 0;
f92363d1
SR
2925 list_add(&ioc->scsi_lookup[i].tracker_list, &ioc->free_list);
2926 spin_unlock_irqrestore(&ioc->scsi_lookup_lock, flags);
2927
6a2d4618 2928 _base_recovery_check(ioc);
f92363d1
SR
2929 return;
2930 } else if (smid < ioc->internal_smid) {
2931 /* hi-priority */
2932 i = smid - ioc->hi_priority_smid;
2933 ioc->hpr_lookup[i].cb_idx = 0xFF;
2934 list_add(&ioc->hpr_lookup[i].tracker_list, &ioc->hpr_free_list);
2935 } else if (smid <= ioc->hba_queue_depth) {
2936 /* internal queue */
2937 i = smid - ioc->internal_smid;
2938 ioc->internal_lookup[i].cb_idx = 0xFF;
2939 list_add(&ioc->internal_lookup[i].tracker_list,
2940 &ioc->internal_free_list);
2941 }
2942 spin_unlock_irqrestore(&ioc->scsi_lookup_lock, flags);
2943}
2944
2945/**
2946 * _base_writeq - 64 bit write to MMIO
2947 * @ioc: per adapter object
2948 * @b: data payload
2949 * @addr: address in MMIO space
2950 * @writeq_lock: spin lock
2951 *
2952 * Glue for handling an atomic 64 bit word to MMIO. This special handling takes
2953 * care of 32 bit environment where its not quarenteed to send the entire word
2954 * in one transfer.
2955 */
2956#if defined(writeq) && defined(CONFIG_64BIT)
2957static inline void
2958_base_writeq(__u64 b, volatile void __iomem *addr, spinlock_t *writeq_lock)
2959{
2960 writeq(cpu_to_le64(b), addr);
2961}
2962#else
2963static inline void
2964_base_writeq(__u64 b, volatile void __iomem *addr, spinlock_t *writeq_lock)
2965{
2966 unsigned long flags;
2967 __u64 data_out = cpu_to_le64(b);
2968
2969 spin_lock_irqsave(writeq_lock, flags);
2970 writel((u32)(data_out), addr);
2971 writel((u32)(data_out >> 32), (addr + 4));
2972 spin_unlock_irqrestore(writeq_lock, flags);
2973}
2974#endif
2975
f92363d1 2976/**
81c16f83 2977 * _base_put_smid_scsi_io - send SCSI_IO request to firmware
f92363d1
SR
2978 * @ioc: per adapter object
2979 * @smid: system request message index
2980 * @handle: device handle
2981 *
2982 * Return nothing.
2983 */
81c16f83
SPS
2984static void
2985_base_put_smid_scsi_io(struct MPT3SAS_ADAPTER *ioc, u16 smid, u16 handle)
f92363d1
SR
2986{
2987 Mpi2RequestDescriptorUnion_t descriptor;
2988 u64 *request = (u64 *)&descriptor;
2989
2990
2991 descriptor.SCSIIO.RequestFlags = MPI2_REQ_DESCRIPT_FLAGS_SCSI_IO;
2992 descriptor.SCSIIO.MSIxIndex = _base_get_msix_index(ioc);
2993 descriptor.SCSIIO.SMID = cpu_to_le16(smid);
2994 descriptor.SCSIIO.DevHandle = cpu_to_le16(handle);
2995 descriptor.SCSIIO.LMID = 0;
2996 _base_writeq(*request, &ioc->chip->RequestDescriptorPostLow,
2997 &ioc->scsi_lookup_lock);
2998}
2999
3000/**
81c16f83 3001 * _base_put_smid_fast_path - send fast path request to firmware
f92363d1
SR
3002 * @ioc: per adapter object
3003 * @smid: system request message index
3004 * @handle: device handle
3005 *
3006 * Return nothing.
3007 */
81c16f83
SPS
3008static void
3009_base_put_smid_fast_path(struct MPT3SAS_ADAPTER *ioc, u16 smid,
f92363d1
SR
3010 u16 handle)
3011{
3012 Mpi2RequestDescriptorUnion_t descriptor;
3013 u64 *request = (u64 *)&descriptor;
3014
3015 descriptor.SCSIIO.RequestFlags =
3016 MPI25_REQ_DESCRIPT_FLAGS_FAST_PATH_SCSI_IO;
3017 descriptor.SCSIIO.MSIxIndex = _base_get_msix_index(ioc);
3018 descriptor.SCSIIO.SMID = cpu_to_le16(smid);
3019 descriptor.SCSIIO.DevHandle = cpu_to_le16(handle);
3020 descriptor.SCSIIO.LMID = 0;
3021 _base_writeq(*request, &ioc->chip->RequestDescriptorPostLow,
3022 &ioc->scsi_lookup_lock);
3023}
3024
3025/**
81c16f83 3026 * _base_put_smid_hi_priority - send Task Management request to firmware
f92363d1
SR
3027 * @ioc: per adapter object
3028 * @smid: system request message index
03d1fb3a 3029 * @msix_task: msix_task will be same as msix of IO incase of task abort else 0.
f92363d1
SR
3030 * Return nothing.
3031 */
81c16f83
SPS
3032static void
3033_base_put_smid_hi_priority(struct MPT3SAS_ADAPTER *ioc, u16 smid,
03d1fb3a 3034 u16 msix_task)
f92363d1
SR
3035{
3036 Mpi2RequestDescriptorUnion_t descriptor;
3037 u64 *request = (u64 *)&descriptor;
3038
3039 descriptor.HighPriority.RequestFlags =
3040 MPI2_REQ_DESCRIPT_FLAGS_HIGH_PRIORITY;
03d1fb3a 3041 descriptor.HighPriority.MSIxIndex = msix_task;
f92363d1
SR
3042 descriptor.HighPriority.SMID = cpu_to_le16(smid);
3043 descriptor.HighPriority.LMID = 0;
3044 descriptor.HighPriority.Reserved1 = 0;
3045 _base_writeq(*request, &ioc->chip->RequestDescriptorPostLow,
3046 &ioc->scsi_lookup_lock);
3047}
3048
aff39e61
SPS
3049/**
3050 * _base_put_smid_nvme_encap - send NVMe encapsulated request to
3051 * firmware
3052 * @ioc: per adapter object
3053 * @smid: system request message index
3054 *
3055 * Return nothing.
3056 */
3057static void
3058_base_put_smid_nvme_encap(struct MPT3SAS_ADAPTER *ioc, u16 smid)
3059{
3060 Mpi2RequestDescriptorUnion_t descriptor;
3061 u64 *request = (u64 *)&descriptor;
3062
3063 descriptor.Default.RequestFlags =
3064 MPI26_REQ_DESCRIPT_FLAGS_PCIE_ENCAPSULATED;
3065 descriptor.Default.MSIxIndex = _base_get_msix_index(ioc);
3066 descriptor.Default.SMID = cpu_to_le16(smid);
3067 descriptor.Default.LMID = 0;
3068 descriptor.Default.DescriptorTypeDependent = 0;
3069 _base_writeq(*request, &ioc->chip->RequestDescriptorPostLow,
3070 &ioc->scsi_lookup_lock);
3071}
3072
f92363d1 3073/**
81c16f83 3074 * _base_put_smid_default - Default, primarily used for config pages
f92363d1
SR
3075 * @ioc: per adapter object
3076 * @smid: system request message index
3077 *
3078 * Return nothing.
3079 */
81c16f83
SPS
3080static void
3081_base_put_smid_default(struct MPT3SAS_ADAPTER *ioc, u16 smid)
f92363d1
SR
3082{
3083 Mpi2RequestDescriptorUnion_t descriptor;
3084 u64 *request = (u64 *)&descriptor;
3085
3086 descriptor.Default.RequestFlags = MPI2_REQ_DESCRIPT_FLAGS_DEFAULT_TYPE;
3087 descriptor.Default.MSIxIndex = _base_get_msix_index(ioc);
3088 descriptor.Default.SMID = cpu_to_le16(smid);
3089 descriptor.Default.LMID = 0;
3090 descriptor.Default.DescriptorTypeDependent = 0;
3091 _base_writeq(*request, &ioc->chip->RequestDescriptorPostLow,
3092 &ioc->scsi_lookup_lock);
3093}
3094
81c16f83
SPS
3095/**
3096* _base_put_smid_scsi_io_atomic - send SCSI_IO request to firmware using
3097* Atomic Request Descriptor
3098* @ioc: per adapter object
3099* @smid: system request message index
3100* @handle: device handle, unused in this function, for function type match
3101*
3102* Return nothing.
3103*/
3104static void
3105_base_put_smid_scsi_io_atomic(struct MPT3SAS_ADAPTER *ioc, u16 smid,
3106 u16 handle)
3107{
3108 Mpi26AtomicRequestDescriptor_t descriptor;
3109 u32 *request = (u32 *)&descriptor;
3110
3111 descriptor.RequestFlags = MPI2_REQ_DESCRIPT_FLAGS_SCSI_IO;
3112 descriptor.MSIxIndex = _base_get_msix_index(ioc);
3113 descriptor.SMID = cpu_to_le16(smid);
3114
3115 writel(cpu_to_le32(*request), &ioc->chip->AtomicRequestDescriptorPost);
3116}
3117
3118/**
3119 * _base_put_smid_fast_path_atomic - send fast path request to firmware
3120 * using Atomic Request Descriptor
3121 * @ioc: per adapter object
3122 * @smid: system request message index
3123 * @handle: device handle, unused in this function, for function type match
3124 * Return nothing
3125 */
3126static void
3127_base_put_smid_fast_path_atomic(struct MPT3SAS_ADAPTER *ioc, u16 smid,
3128 u16 handle)
3129{
3130 Mpi26AtomicRequestDescriptor_t descriptor;
3131 u32 *request = (u32 *)&descriptor;
3132
3133 descriptor.RequestFlags = MPI25_REQ_DESCRIPT_FLAGS_FAST_PATH_SCSI_IO;
3134 descriptor.MSIxIndex = _base_get_msix_index(ioc);
3135 descriptor.SMID = cpu_to_le16(smid);
3136
3137 writel(cpu_to_le32(*request), &ioc->chip->AtomicRequestDescriptorPost);
3138}
3139
3140/**
3141 * _base_put_smid_hi_priority_atomic - send Task Management request to
3142 * firmware using Atomic Request Descriptor
3143 * @ioc: per adapter object
3144 * @smid: system request message index
3145 * @msix_task: msix_task will be same as msix of IO incase of task abort else 0
3146 *
3147 * Return nothing.
3148 */
3149static void
3150_base_put_smid_hi_priority_atomic(struct MPT3SAS_ADAPTER *ioc, u16 smid,
3151 u16 msix_task)
3152{
3153 Mpi26AtomicRequestDescriptor_t descriptor;
3154 u32 *request = (u32 *)&descriptor;
3155
3156 descriptor.RequestFlags = MPI2_REQ_DESCRIPT_FLAGS_HIGH_PRIORITY;
3157 descriptor.MSIxIndex = msix_task;
3158 descriptor.SMID = cpu_to_le16(smid);
3159
3160 writel(cpu_to_le32(*request), &ioc->chip->AtomicRequestDescriptorPost);
3161}
3162
aff39e61
SPS
3163/**
3164 * _base_put_smid_nvme_encap_atomic - send NVMe encapsulated request to
3165 * firmware using Atomic Request Descriptor
3166 * @ioc: per adapter object
3167 * @smid: system request message index
3168 *
3169 * Return nothing.
3170 */
3171static void
3172_base_put_smid_nvme_encap_atomic(struct MPT3SAS_ADAPTER *ioc, u16 smid)
3173{
3174 Mpi26AtomicRequestDescriptor_t descriptor;
3175 u32 *request = (u32 *)&descriptor;
3176
3177 descriptor.RequestFlags = MPI26_REQ_DESCRIPT_FLAGS_PCIE_ENCAPSULATED;
3178 descriptor.MSIxIndex = _base_get_msix_index(ioc);
3179 descriptor.SMID = cpu_to_le16(smid);
3180
3181 writel(cpu_to_le32(*request), &ioc->chip->AtomicRequestDescriptorPost);
3182}
3183
81c16f83
SPS
3184/**
3185 * _base_put_smid_default - Default, primarily used for config pages
3186 * use Atomic Request Descriptor
3187 * @ioc: per adapter object
3188 * @smid: system request message index
3189 *
3190 * Return nothing.
3191 */
3192static void
3193_base_put_smid_default_atomic(struct MPT3SAS_ADAPTER *ioc, u16 smid)
3194{
3195 Mpi26AtomicRequestDescriptor_t descriptor;
3196 u32 *request = (u32 *)&descriptor;
3197
3198 descriptor.RequestFlags = MPI2_REQ_DESCRIPT_FLAGS_DEFAULT_TYPE;
3199 descriptor.MSIxIndex = _base_get_msix_index(ioc);
3200 descriptor.SMID = cpu_to_le16(smid);
3201
3202 writel(cpu_to_le32(*request), &ioc->chip->AtomicRequestDescriptorPost);
3203}
3204
1117b31a 3205/**
989e43c7 3206 * _base_display_OEMs_branding - Display branding string
1117b31a
SR
3207 * @ioc: per adapter object
3208 *
3209 * Return nothing.
3210 */
3211static void
989e43c7 3212_base_display_OEMs_branding(struct MPT3SAS_ADAPTER *ioc)
1117b31a
SR
3213{
3214 if (ioc->pdev->subsystem_vendor != PCI_VENDOR_ID_INTEL)
3215 return;
3216
989e43c7
SR
3217 switch (ioc->pdev->subsystem_vendor) {
3218 case PCI_VENDOR_ID_INTEL:
3219 switch (ioc->pdev->device) {
3220 case MPI2_MFGPAGE_DEVID_SAS2008:
3221 switch (ioc->pdev->subsystem_device) {
3222 case MPT2SAS_INTEL_RMS2LL080_SSDID:
3223 pr_info(MPT3SAS_FMT "%s\n", ioc->name,
3224 MPT2SAS_INTEL_RMS2LL080_BRANDING);
3225 break;
3226 case MPT2SAS_INTEL_RMS2LL040_SSDID:
3227 pr_info(MPT3SAS_FMT "%s\n", ioc->name,
3228 MPT2SAS_INTEL_RMS2LL040_BRANDING);
3229 break;
3230 case MPT2SAS_INTEL_SSD910_SSDID:
3231 pr_info(MPT3SAS_FMT "%s\n", ioc->name,
3232 MPT2SAS_INTEL_SSD910_BRANDING);
3233 break;
3234 default:
3235 pr_info(MPT3SAS_FMT
3236 "Intel(R) Controller: Subsystem ID: 0x%X\n",
3237 ioc->name, ioc->pdev->subsystem_device);
3238 break;
3239 }
3240 case MPI2_MFGPAGE_DEVID_SAS2308_2:
3241 switch (ioc->pdev->subsystem_device) {
3242 case MPT2SAS_INTEL_RS25GB008_SSDID:
3243 pr_info(MPT3SAS_FMT "%s\n", ioc->name,
3244 MPT2SAS_INTEL_RS25GB008_BRANDING);
3245 break;
3246 case MPT2SAS_INTEL_RMS25JB080_SSDID:
3247 pr_info(MPT3SAS_FMT "%s\n", ioc->name,
3248 MPT2SAS_INTEL_RMS25JB080_BRANDING);
3249 break;
3250 case MPT2SAS_INTEL_RMS25JB040_SSDID:
3251 pr_info(MPT3SAS_FMT "%s\n", ioc->name,
3252 MPT2SAS_INTEL_RMS25JB040_BRANDING);
3253 break;
3254 case MPT2SAS_INTEL_RMS25KB080_SSDID:
3255 pr_info(MPT3SAS_FMT "%s\n", ioc->name,
3256 MPT2SAS_INTEL_RMS25KB080_BRANDING);
3257 break;
3258 case MPT2SAS_INTEL_RMS25KB040_SSDID:
3259 pr_info(MPT3SAS_FMT "%s\n", ioc->name,
3260 MPT2SAS_INTEL_RMS25KB040_BRANDING);
3261 break;
3262 case MPT2SAS_INTEL_RMS25LB040_SSDID:
3263 pr_info(MPT3SAS_FMT "%s\n", ioc->name,
3264 MPT2SAS_INTEL_RMS25LB040_BRANDING);
3265 break;
3266 case MPT2SAS_INTEL_RMS25LB080_SSDID:
3267 pr_info(MPT3SAS_FMT "%s\n", ioc->name,
3268 MPT2SAS_INTEL_RMS25LB080_BRANDING);
3269 break;
3270 default:
3271 pr_info(MPT3SAS_FMT
3272 "Intel(R) Controller: Subsystem ID: 0x%X\n",
3273 ioc->name, ioc->pdev->subsystem_device);
3274 break;
3275 }
3276 case MPI25_MFGPAGE_DEVID_SAS3008:
3277 switch (ioc->pdev->subsystem_device) {
3278 case MPT3SAS_INTEL_RMS3JC080_SSDID:
3279 pr_info(MPT3SAS_FMT "%s\n", ioc->name,
3280 MPT3SAS_INTEL_RMS3JC080_BRANDING);
3281 break;
3282
3283 case MPT3SAS_INTEL_RS3GC008_SSDID:
3284 pr_info(MPT3SAS_FMT "%s\n", ioc->name,
3285 MPT3SAS_INTEL_RS3GC008_BRANDING);
3286 break;
3287 case MPT3SAS_INTEL_RS3FC044_SSDID:
3288 pr_info(MPT3SAS_FMT "%s\n", ioc->name,
3289 MPT3SAS_INTEL_RS3FC044_BRANDING);
3290 break;
3291 case MPT3SAS_INTEL_RS3UC080_SSDID:
3292 pr_info(MPT3SAS_FMT "%s\n", ioc->name,
3293 MPT3SAS_INTEL_RS3UC080_BRANDING);
3294 break;
3295 default:
3296 pr_info(MPT3SAS_FMT
3297 "Intel(R) Controller: Subsystem ID: 0x%X\n",
3298 ioc->name, ioc->pdev->subsystem_device);
3299 break;
3300 }
1117b31a
SR
3301 break;
3302 default:
3303 pr_info(MPT3SAS_FMT
989e43c7
SR
3304 "Intel(R) Controller: Subsystem ID: 0x%X\n",
3305 ioc->name, ioc->pdev->subsystem_device);
1117b31a
SR
3306 break;
3307 }
3308 break;
989e43c7
SR
3309 case PCI_VENDOR_ID_DELL:
3310 switch (ioc->pdev->device) {
3311 case MPI2_MFGPAGE_DEVID_SAS2008:
3312 switch (ioc->pdev->subsystem_device) {
3313 case MPT2SAS_DELL_6GBPS_SAS_HBA_SSDID:
3314 pr_info(MPT3SAS_FMT "%s\n", ioc->name,
3315 MPT2SAS_DELL_6GBPS_SAS_HBA_BRANDING);
3316 break;
3317 case MPT2SAS_DELL_PERC_H200_ADAPTER_SSDID:
3318 pr_info(MPT3SAS_FMT "%s\n", ioc->name,
3319 MPT2SAS_DELL_PERC_H200_ADAPTER_BRANDING);
3320 break;
3321 case MPT2SAS_DELL_PERC_H200_INTEGRATED_SSDID:
3322 pr_info(MPT3SAS_FMT "%s\n", ioc->name,
3323 MPT2SAS_DELL_PERC_H200_INTEGRATED_BRANDING);
3324 break;
3325 case MPT2SAS_DELL_PERC_H200_MODULAR_SSDID:
3326 pr_info(MPT3SAS_FMT "%s\n", ioc->name,
3327 MPT2SAS_DELL_PERC_H200_MODULAR_BRANDING);
3328 break;
3329 case MPT2SAS_DELL_PERC_H200_EMBEDDED_SSDID:
3330 pr_info(MPT3SAS_FMT "%s\n", ioc->name,
3331 MPT2SAS_DELL_PERC_H200_EMBEDDED_BRANDING);
3332 break;
3333 case MPT2SAS_DELL_PERC_H200_SSDID:
3334 pr_info(MPT3SAS_FMT "%s\n", ioc->name,
3335 MPT2SAS_DELL_PERC_H200_BRANDING);
3336 break;
3337 case MPT2SAS_DELL_6GBPS_SAS_SSDID:
3338 pr_info(MPT3SAS_FMT "%s\n", ioc->name,
3339 MPT2SAS_DELL_6GBPS_SAS_BRANDING);
3340 break;
3341 default:
3342 pr_info(MPT3SAS_FMT
3343 "Dell 6Gbps HBA: Subsystem ID: 0x%X\n",
3344 ioc->name, ioc->pdev->subsystem_device);
3345 break;
3346 }
3347 break;
3348 case MPI25_MFGPAGE_DEVID_SAS3008:
3349 switch (ioc->pdev->subsystem_device) {
3350 case MPT3SAS_DELL_12G_HBA_SSDID:
3351 pr_info(MPT3SAS_FMT "%s\n", ioc->name,
3352 MPT3SAS_DELL_12G_HBA_BRANDING);
3353 break;
3354 default:
3355 pr_info(MPT3SAS_FMT
3356 "Dell 12Gbps HBA: Subsystem ID: 0x%X\n",
3357 ioc->name, ioc->pdev->subsystem_device);
3358 break;
3359 }
fb84dfc4
SR
3360 break;
3361 default:
3362 pr_info(MPT3SAS_FMT
989e43c7 3363 "Dell HBA: Subsystem ID: 0x%X\n", ioc->name,
fb84dfc4
SR
3364 ioc->pdev->subsystem_device);
3365 break;
3366 }
3367 break;
989e43c7
SR
3368 case PCI_VENDOR_ID_CISCO:
3369 switch (ioc->pdev->device) {
3370 case MPI25_MFGPAGE_DEVID_SAS3008:
3371 switch (ioc->pdev->subsystem_device) {
3372 case MPT3SAS_CISCO_12G_8E_HBA_SSDID:
3373 pr_info(MPT3SAS_FMT "%s\n", ioc->name,
3374 MPT3SAS_CISCO_12G_8E_HBA_BRANDING);
3375 break;
3376 case MPT3SAS_CISCO_12G_8I_HBA_SSDID:
3377 pr_info(MPT3SAS_FMT "%s\n", ioc->name,
3378 MPT3SAS_CISCO_12G_8I_HBA_BRANDING);
3379 break;
3380 case MPT3SAS_CISCO_12G_AVILA_HBA_SSDID:
3381 pr_info(MPT3SAS_FMT "%s\n", ioc->name,
3382 MPT3SAS_CISCO_12G_AVILA_HBA_BRANDING);
3383 break;
3384 default:
3385 pr_info(MPT3SAS_FMT
3386 "Cisco 12Gbps SAS HBA: Subsystem ID: 0x%X\n",
3387 ioc->name, ioc->pdev->subsystem_device);
3388 break;
3389 }
d8eb4a47 3390 break;
989e43c7
SR
3391 case MPI25_MFGPAGE_DEVID_SAS3108_1:
3392 switch (ioc->pdev->subsystem_device) {
3393 case MPT3SAS_CISCO_12G_AVILA_HBA_SSDID:
3394 pr_info(MPT3SAS_FMT "%s\n", ioc->name,
d8eb4a47 3395 MPT3SAS_CISCO_12G_AVILA_HBA_BRANDING);
989e43c7
SR
3396 break;
3397 case MPT3SAS_CISCO_12G_COLUSA_MEZZANINE_HBA_SSDID:
3398 pr_info(MPT3SAS_FMT "%s\n", ioc->name,
3399 MPT3SAS_CISCO_12G_COLUSA_MEZZANINE_HBA_BRANDING
3400 );
3401 break;
3402 default:
3403 pr_info(MPT3SAS_FMT
3404 "Cisco 12Gbps SAS HBA: Subsystem ID: 0x%X\n",
3405 ioc->name, ioc->pdev->subsystem_device);
3406 break;
3407 }
38e4141e
SR
3408 break;
3409 default:
3410 pr_info(MPT3SAS_FMT
989e43c7
SR
3411 "Cisco SAS HBA: Subsystem ID: 0x%X\n",
3412 ioc->name, ioc->pdev->subsystem_device);
38e4141e
SR
3413 break;
3414 }
3415 break;
989e43c7
SR
3416 case MPT2SAS_HP_3PAR_SSVID:
3417 switch (ioc->pdev->device) {
3418 case MPI2_MFGPAGE_DEVID_SAS2004:
3419 switch (ioc->pdev->subsystem_device) {
3420 case MPT2SAS_HP_DAUGHTER_2_4_INTERNAL_SSDID:
3421 pr_info(MPT3SAS_FMT "%s\n", ioc->name,
3422 MPT2SAS_HP_DAUGHTER_2_4_INTERNAL_BRANDING);
3423 break;
3424 default:
3425 pr_info(MPT3SAS_FMT
3426 "HP 6Gbps SAS HBA: Subsystem ID: 0x%X\n",
3427 ioc->name, ioc->pdev->subsystem_device);
3428 break;
3429 }
3430 case MPI2_MFGPAGE_DEVID_SAS2308_2:
3431 switch (ioc->pdev->subsystem_device) {
3432 case MPT2SAS_HP_2_4_INTERNAL_SSDID:
3433 pr_info(MPT3SAS_FMT "%s\n", ioc->name,
3434 MPT2SAS_HP_2_4_INTERNAL_BRANDING);
3435 break;
3436 case MPT2SAS_HP_2_4_EXTERNAL_SSDID:
3437 pr_info(MPT3SAS_FMT "%s\n", ioc->name,
3438 MPT2SAS_HP_2_4_EXTERNAL_BRANDING);
3439 break;
3440 case MPT2SAS_HP_1_4_INTERNAL_1_4_EXTERNAL_SSDID:
3441 pr_info(MPT3SAS_FMT "%s\n", ioc->name,
3442 MPT2SAS_HP_1_4_INTERNAL_1_4_EXTERNAL_BRANDING);
3443 break;
3444 case MPT2SAS_HP_EMBEDDED_2_4_INTERNAL_SSDID:
3445 pr_info(MPT3SAS_FMT "%s\n", ioc->name,
3446 MPT2SAS_HP_EMBEDDED_2_4_INTERNAL_BRANDING);
3447 break;
3448 default:
3449 pr_info(MPT3SAS_FMT
3450 "HP 6Gbps SAS HBA: Subsystem ID: 0x%X\n",
3451 ioc->name, ioc->pdev->subsystem_device);
3452 break;
3453 }
d8eb4a47
SR
3454 default:
3455 pr_info(MPT3SAS_FMT
989e43c7
SR
3456 "HP SAS HBA: Subsystem ID: 0x%X\n",
3457 ioc->name, ioc->pdev->subsystem_device);
d8eb4a47
SR
3458 break;
3459 }
38e4141e 3460 default:
38e4141e
SR
3461 break;
3462 }
3463}
fb84dfc4 3464
f92363d1
SR
3465/**
3466 * _base_display_ioc_capabilities - Disply IOC's capabilities.
3467 * @ioc: per adapter object
3468 *
3469 * Return nothing.
3470 */
3471static void
3472_base_display_ioc_capabilities(struct MPT3SAS_ADAPTER *ioc)
3473{
3474 int i = 0;
3475 char desc[16];
3476 u32 iounit_pg1_flags;
3477 u32 bios_version;
3478
3479 bios_version = le32_to_cpu(ioc->bios_pg3.BiosVersion);
3480 strncpy(desc, ioc->manu_pg0.ChipName, 16);
3481 pr_info(MPT3SAS_FMT "%s: FWVersion(%02d.%02d.%02d.%02d), "\
3482 "ChipRevision(0x%02x), BiosVersion(%02d.%02d.%02d.%02d)\n",
3483 ioc->name, desc,
3484 (ioc->facts.FWVersion.Word & 0xFF000000) >> 24,
3485 (ioc->facts.FWVersion.Word & 0x00FF0000) >> 16,
3486 (ioc->facts.FWVersion.Word & 0x0000FF00) >> 8,
3487 ioc->facts.FWVersion.Word & 0x000000FF,
3488 ioc->pdev->revision,
3489 (bios_version & 0xFF000000) >> 24,
3490 (bios_version & 0x00FF0000) >> 16,
3491 (bios_version & 0x0000FF00) >> 8,
3492 bios_version & 0x000000FF);
3493
989e43c7 3494 _base_display_OEMs_branding(ioc);
1117b31a 3495
016d5c35
SPS
3496 if (ioc->facts.ProtocolFlags & MPI2_IOCFACTS_PROTOCOL_NVME_DEVICES) {
3497 pr_info("%sNVMe", i ? "," : "");
3498 i++;
3499 }
3500
f92363d1
SR
3501 pr_info(MPT3SAS_FMT "Protocol=(", ioc->name);
3502
3503 if (ioc->facts.ProtocolFlags & MPI2_IOCFACTS_PROTOCOL_SCSI_INITIATOR) {
3504 pr_info("Initiator");
3505 i++;
3506 }
3507
3508 if (ioc->facts.ProtocolFlags & MPI2_IOCFACTS_PROTOCOL_SCSI_TARGET) {
3509 pr_info("%sTarget", i ? "," : "");
3510 i++;
3511 }
3512
3513 i = 0;
3514 pr_info("), ");
3515 pr_info("Capabilities=(");
3516
7786ab6a
SR
3517 if (!ioc->hide_ir_msg) {
3518 if (ioc->facts.IOCCapabilities &
f92363d1
SR
3519 MPI2_IOCFACTS_CAPABILITY_INTEGRATED_RAID) {
3520 pr_info("Raid");
3521 i++;
7786ab6a 3522 }
f92363d1
SR
3523 }
3524
3525 if (ioc->facts.IOCCapabilities & MPI2_IOCFACTS_CAPABILITY_TLR) {
3526 pr_info("%sTLR", i ? "," : "");
3527 i++;
3528 }
3529
3530 if (ioc->facts.IOCCapabilities & MPI2_IOCFACTS_CAPABILITY_MULTICAST) {
3531 pr_info("%sMulticast", i ? "," : "");
3532 i++;
3533 }
3534
3535 if (ioc->facts.IOCCapabilities &
3536 MPI2_IOCFACTS_CAPABILITY_BIDIRECTIONAL_TARGET) {
3537 pr_info("%sBIDI Target", i ? "," : "");
3538 i++;
3539 }
3540
3541 if (ioc->facts.IOCCapabilities & MPI2_IOCFACTS_CAPABILITY_EEDP) {
3542 pr_info("%sEEDP", i ? "," : "");
3543 i++;
3544 }
3545
3546 if (ioc->facts.IOCCapabilities &
3547 MPI2_IOCFACTS_CAPABILITY_SNAPSHOT_BUFFER) {
3548 pr_info("%sSnapshot Buffer", i ? "," : "");
3549 i++;
3550 }
3551
3552 if (ioc->facts.IOCCapabilities &
3553 MPI2_IOCFACTS_CAPABILITY_DIAG_TRACE_BUFFER) {
3554 pr_info("%sDiag Trace Buffer", i ? "," : "");
3555 i++;
3556 }
3557
3558 if (ioc->facts.IOCCapabilities &
3559 MPI2_IOCFACTS_CAPABILITY_EXTENDED_BUFFER) {
3560 pr_info("%sDiag Extended Buffer", i ? "," : "");
3561 i++;
3562 }
3563
3564 if (ioc->facts.IOCCapabilities &
3565 MPI2_IOCFACTS_CAPABILITY_TASK_SET_FULL_HANDLING) {
3566 pr_info("%sTask Set Full", i ? "," : "");
3567 i++;
3568 }
3569
3570 iounit_pg1_flags = le32_to_cpu(ioc->iounit_pg1.Flags);
3571 if (!(iounit_pg1_flags & MPI2_IOUNITPAGE1_NATIVE_COMMAND_Q_DISABLE)) {
3572 pr_info("%sNCQ", i ? "," : "");
3573 i++;
3574 }
3575
3576 pr_info(")\n");
3577}
3578
3579/**
3580 * mpt3sas_base_update_missing_delay - change the missing delay timers
3581 * @ioc: per adapter object
3582 * @device_missing_delay: amount of time till device is reported missing
3583 * @io_missing_delay: interval IO is returned when there is a missing device
3584 *
3585 * Return nothing.
3586 *
3587 * Passed on the command line, this function will modify the device missing
3588 * delay, as well as the io missing delay. This should be called at driver
3589 * load time.
3590 */
3591void
3592mpt3sas_base_update_missing_delay(struct MPT3SAS_ADAPTER *ioc,
3593 u16 device_missing_delay, u8 io_missing_delay)
3594{
3595 u16 dmd, dmd_new, dmd_orignal;
3596 u8 io_missing_delay_original;
3597 u16 sz;
3598 Mpi2SasIOUnitPage1_t *sas_iounit_pg1 = NULL;
3599 Mpi2ConfigReply_t mpi_reply;
3600 u8 num_phys = 0;
3601 u16 ioc_status;
3602
3603 mpt3sas_config_get_number_hba_phys(ioc, &num_phys);
3604 if (!num_phys)
3605 return;
3606
3607 sz = offsetof(Mpi2SasIOUnitPage1_t, PhyData) + (num_phys *
3608 sizeof(Mpi2SasIOUnit1PhyData_t));
3609 sas_iounit_pg1 = kzalloc(sz, GFP_KERNEL);
3610 if (!sas_iounit_pg1) {
3611 pr_err(MPT3SAS_FMT "failure at %s:%d/%s()!\n",
3612 ioc->name, __FILE__, __LINE__, __func__);
3613 goto out;
3614 }
3615 if ((mpt3sas_config_get_sas_iounit_pg1(ioc, &mpi_reply,
3616 sas_iounit_pg1, sz))) {
3617 pr_err(MPT3SAS_FMT "failure at %s:%d/%s()!\n",
3618 ioc->name, __FILE__, __LINE__, __func__);
3619 goto out;
3620 }
3621 ioc_status = le16_to_cpu(mpi_reply.IOCStatus) &
3622 MPI2_IOCSTATUS_MASK;
3623 if (ioc_status != MPI2_IOCSTATUS_SUCCESS) {
3624 pr_err(MPT3SAS_FMT "failure at %s:%d/%s()!\n",
3625 ioc->name, __FILE__, __LINE__, __func__);
3626 goto out;
3627 }
3628
3629 /* device missing delay */
3630 dmd = sas_iounit_pg1->ReportDeviceMissingDelay;
3631 if (dmd & MPI2_SASIOUNIT1_REPORT_MISSING_UNIT_16)
3632 dmd = (dmd & MPI2_SASIOUNIT1_REPORT_MISSING_TIMEOUT_MASK) * 16;
3633 else
3634 dmd = dmd & MPI2_SASIOUNIT1_REPORT_MISSING_TIMEOUT_MASK;
3635 dmd_orignal = dmd;
3636 if (device_missing_delay > 0x7F) {
3637 dmd = (device_missing_delay > 0x7F0) ? 0x7F0 :
3638 device_missing_delay;
3639 dmd = dmd / 16;
3640 dmd |= MPI2_SASIOUNIT1_REPORT_MISSING_UNIT_16;
3641 } else
3642 dmd = device_missing_delay;
3643 sas_iounit_pg1->ReportDeviceMissingDelay = dmd;
3644
3645 /* io missing delay */
3646 io_missing_delay_original = sas_iounit_pg1->IODeviceMissingDelay;
3647 sas_iounit_pg1->IODeviceMissingDelay = io_missing_delay;
3648
3649 if (!mpt3sas_config_set_sas_iounit_pg1(ioc, &mpi_reply, sas_iounit_pg1,
3650 sz)) {
3651 if (dmd & MPI2_SASIOUNIT1_REPORT_MISSING_UNIT_16)
3652 dmd_new = (dmd &
3653 MPI2_SASIOUNIT1_REPORT_MISSING_TIMEOUT_MASK) * 16;
3654 else
3655 dmd_new =
3656 dmd & MPI2_SASIOUNIT1_REPORT_MISSING_TIMEOUT_MASK;
3657 pr_info(MPT3SAS_FMT "device_missing_delay: old(%d), new(%d)\n",
3658 ioc->name, dmd_orignal, dmd_new);
3659 pr_info(MPT3SAS_FMT "ioc_missing_delay: old(%d), new(%d)\n",
3660 ioc->name, io_missing_delay_original,
3661 io_missing_delay);
3662 ioc->device_missing_delay = dmd_new;
3663 ioc->io_missing_delay = io_missing_delay;
3664 }
3665
3666out:
3667 kfree(sas_iounit_pg1);
3668}
3669/**
3670 * _base_static_config_pages - static start of day config pages
3671 * @ioc: per adapter object
3672 *
3673 * Return nothing.
3674 */
3675static void
3676_base_static_config_pages(struct MPT3SAS_ADAPTER *ioc)
3677{
3678 Mpi2ConfigReply_t mpi_reply;
3679 u32 iounit_pg1_flags;
3680
3681 mpt3sas_config_get_manufacturing_pg0(ioc, &mpi_reply, &ioc->manu_pg0);
3682 if (ioc->ir_firmware)
3683 mpt3sas_config_get_manufacturing_pg10(ioc, &mpi_reply,
3684 &ioc->manu_pg10);
3685
3686 /*
3687 * Ensure correct T10 PI operation if vendor left EEDPTagMode
3688 * flag unset in NVDATA.
3689 */
3690 mpt3sas_config_get_manufacturing_pg11(ioc, &mpi_reply, &ioc->manu_pg11);
3691 if (ioc->manu_pg11.EEDPTagMode == 0) {
3692 pr_err("%s: overriding NVDATA EEDPTagMode setting\n",
3693 ioc->name);
3694 ioc->manu_pg11.EEDPTagMode &= ~0x3;
3695 ioc->manu_pg11.EEDPTagMode |= 0x1;
3696 mpt3sas_config_set_manufacturing_pg11(ioc, &mpi_reply,
3697 &ioc->manu_pg11);
3698 }
3699
3700 mpt3sas_config_get_bios_pg2(ioc, &mpi_reply, &ioc->bios_pg2);
3701 mpt3sas_config_get_bios_pg3(ioc, &mpi_reply, &ioc->bios_pg3);
3702 mpt3sas_config_get_ioc_pg8(ioc, &mpi_reply, &ioc->ioc_pg8);
3703 mpt3sas_config_get_iounit_pg0(ioc, &mpi_reply, &ioc->iounit_pg0);
3704 mpt3sas_config_get_iounit_pg1(ioc, &mpi_reply, &ioc->iounit_pg1);
2d8ce8c9 3705 mpt3sas_config_get_iounit_pg8(ioc, &mpi_reply, &ioc->iounit_pg8);
f92363d1
SR
3706 _base_display_ioc_capabilities(ioc);
3707
3708 /*
3709 * Enable task_set_full handling in iounit_pg1 when the
3710 * facts capabilities indicate that its supported.
3711 */
3712 iounit_pg1_flags = le32_to_cpu(ioc->iounit_pg1.Flags);
3713 if ((ioc->facts.IOCCapabilities &
3714 MPI2_IOCFACTS_CAPABILITY_TASK_SET_FULL_HANDLING))
3715 iounit_pg1_flags &=
3716 ~MPI2_IOUNITPAGE1_DISABLE_TASK_SET_FULL_HANDLING;
3717 else
3718 iounit_pg1_flags |=
3719 MPI2_IOUNITPAGE1_DISABLE_TASK_SET_FULL_HANDLING;
3720 ioc->iounit_pg1.Flags = cpu_to_le32(iounit_pg1_flags);
3721 mpt3sas_config_set_iounit_pg1(ioc, &mpi_reply, &ioc->iounit_pg1);
2d8ce8c9
SR
3722
3723 if (ioc->iounit_pg8.NumSensors)
3724 ioc->temp_sensors_count = ioc->iounit_pg8.NumSensors;
f92363d1
SR
3725}
3726
3727/**
3728 * _base_release_memory_pools - release memory
3729 * @ioc: per adapter object
3730 *
3731 * Free memory allocated from _base_allocate_memory_pools.
3732 *
3733 * Return nothing.
3734 */
3735static void
3736_base_release_memory_pools(struct MPT3SAS_ADAPTER *ioc)
3737{
9b05c91a
SR
3738 int i = 0;
3739 struct reply_post_struct *rps;
f92363d1
SR
3740
3741 dexitprintk(ioc, pr_info(MPT3SAS_FMT "%s\n", ioc->name,
3742 __func__));
3743
3744 if (ioc->request) {
3745 pci_free_consistent(ioc->pdev, ioc->request_dma_sz,
3746 ioc->request, ioc->request_dma);
3747 dexitprintk(ioc, pr_info(MPT3SAS_FMT
3748 "request_pool(0x%p): free\n",
3749 ioc->name, ioc->request));
3750 ioc->request = NULL;
3751 }
3752
3753 if (ioc->sense) {
e9d98418
RP
3754 dma_pool_free(ioc->sense_dma_pool, ioc->sense, ioc->sense_dma);
3755 dma_pool_destroy(ioc->sense_dma_pool);
f92363d1
SR
3756 dexitprintk(ioc, pr_info(MPT3SAS_FMT
3757 "sense_pool(0x%p): free\n",
3758 ioc->name, ioc->sense));
3759 ioc->sense = NULL;
3760 }
3761
3762 if (ioc->reply) {
e9d98418
RP
3763 dma_pool_free(ioc->reply_dma_pool, ioc->reply, ioc->reply_dma);
3764 dma_pool_destroy(ioc->reply_dma_pool);
f92363d1
SR
3765 dexitprintk(ioc, pr_info(MPT3SAS_FMT
3766 "reply_pool(0x%p): free\n",
3767 ioc->name, ioc->reply));
3768 ioc->reply = NULL;
3769 }
3770
3771 if (ioc->reply_free) {
e9d98418 3772 dma_pool_free(ioc->reply_free_dma_pool, ioc->reply_free,
f92363d1 3773 ioc->reply_free_dma);
e9d98418 3774 dma_pool_destroy(ioc->reply_free_dma_pool);
f92363d1
SR
3775 dexitprintk(ioc, pr_info(MPT3SAS_FMT
3776 "reply_free_pool(0x%p): free\n",
3777 ioc->name, ioc->reply_free));
3778 ioc->reply_free = NULL;
3779 }
3780
9b05c91a
SR
3781 if (ioc->reply_post) {
3782 do {
3783 rps = &ioc->reply_post[i];
3784 if (rps->reply_post_free) {
e9d98418 3785 dma_pool_free(
9b05c91a
SR
3786 ioc->reply_post_free_dma_pool,
3787 rps->reply_post_free,
3788 rps->reply_post_free_dma);
3789 dexitprintk(ioc, pr_info(MPT3SAS_FMT
3790 "reply_post_free_pool(0x%p): free\n",
3791 ioc->name, rps->reply_post_free));
3792 rps->reply_post_free = NULL;
3793 }
3794 } while (ioc->rdpq_array_enable &&
3795 (++i < ioc->reply_queue_count));
3796
e9d98418 3797 dma_pool_destroy(ioc->reply_post_free_dma_pool);
9b05c91a 3798 kfree(ioc->reply_post);
f92363d1
SR
3799 }
3800
016d5c35
SPS
3801 if (ioc->pcie_sgl_dma_pool) {
3802 for (i = 0; i < ioc->scsiio_depth; i++) {
3803 if (ioc->scsi_lookup[i].pcie_sg_list.pcie_sgl)
13a06405 3804 dma_pool_free(ioc->pcie_sgl_dma_pool,
016d5c35
SPS
3805 ioc->scsi_lookup[i].pcie_sg_list.pcie_sgl,
3806 ioc->scsi_lookup[i].pcie_sg_list.pcie_sgl_dma);
3807 }
3808 if (ioc->pcie_sgl_dma_pool)
13a06405 3809 dma_pool_destroy(ioc->pcie_sgl_dma_pool);
016d5c35
SPS
3810 }
3811
f92363d1
SR
3812 if (ioc->config_page) {
3813 dexitprintk(ioc, pr_info(MPT3SAS_FMT
3814 "config_page(0x%p): free\n", ioc->name,
3815 ioc->config_page));
3816 pci_free_consistent(ioc->pdev, ioc->config_page_sz,
3817 ioc->config_page, ioc->config_page_dma);
3818 }
3819
3820 if (ioc->scsi_lookup) {
3821 free_pages((ulong)ioc->scsi_lookup, ioc->scsi_lookup_pages);
3822 ioc->scsi_lookup = NULL;
3823 }
3824 kfree(ioc->hpr_lookup);
3825 kfree(ioc->internal_lookup);
3826 if (ioc->chain_lookup) {
3827 for (i = 0; i < ioc->chain_depth; i++) {
3828 if (ioc->chain_lookup[i].chain_buffer)
e9d98418 3829 dma_pool_free(ioc->chain_dma_pool,
f92363d1
SR
3830 ioc->chain_lookup[i].chain_buffer,
3831 ioc->chain_lookup[i].chain_buffer_dma);
3832 }
e9d98418 3833 dma_pool_destroy(ioc->chain_dma_pool);
f92363d1
SR
3834 free_pages((ulong)ioc->chain_lookup, ioc->chain_pages);
3835 ioc->chain_lookup = NULL;
3836 }
3837}
3838
3839/**
3840 * _base_allocate_memory_pools - allocate start of day memory pools
3841 * @ioc: per adapter object
f92363d1
SR
3842 *
3843 * Returns 0 success, anything else error
3844 */
3845static int
98c56ad3 3846_base_allocate_memory_pools(struct MPT3SAS_ADAPTER *ioc)
f92363d1
SR
3847{
3848 struct mpt3sas_facts *facts;
3849 u16 max_sge_elements;
3850 u16 chains_needed_per_io;
3851 u32 sz, total_sz, reply_post_free_sz;
3852 u32 retry_sz;
016d5c35 3853 u16 max_request_credit, nvme_blocks_needed;
f92363d1
SR
3854 unsigned short sg_tablesize;
3855 u16 sge_size;
3856 int i;
3857
3858 dinitprintk(ioc, pr_info(MPT3SAS_FMT "%s\n", ioc->name,
3859 __func__));
3860
3861
3862 retry_sz = 0;
3863 facts = &ioc->facts;
3864
3865 /* command line tunables for max sgl entries */
3866 if (max_sgl_entries != -1)
3867 sg_tablesize = max_sgl_entries;
471ef9d4
SR
3868 else {
3869 if (ioc->hba_mpi_version_belonged == MPI2_VERSION)
3870 sg_tablesize = MPT2SAS_SG_DEPTH;
3871 else
3872 sg_tablesize = MPT3SAS_SG_DEPTH;
3873 }
f92363d1 3874
06f5f976
SR
3875 /* max sgl entries <= MPT_KDUMP_MIN_PHYS_SEGMENTS in KDUMP mode */
3876 if (reset_devices)
3877 sg_tablesize = min_t(unsigned short, sg_tablesize,
3878 MPT_KDUMP_MIN_PHYS_SEGMENTS);
3879
8a7e4c24
SR
3880 if (sg_tablesize < MPT_MIN_PHYS_SEGMENTS)
3881 sg_tablesize = MPT_MIN_PHYS_SEGMENTS;
3882 else if (sg_tablesize > MPT_MAX_PHYS_SEGMENTS) {
ad666a0f 3883 sg_tablesize = min_t(unsigned short, sg_tablesize,
65e8617f 3884 SG_MAX_SEGMENTS);
ad666a0f
SR
3885 pr_warn(MPT3SAS_FMT
3886 "sg_tablesize(%u) is bigger than kernel"
65e8617f 3887 " defined SG_CHUNK_SIZE(%u)\n", ioc->name,
8a7e4c24 3888 sg_tablesize, MPT_MAX_PHYS_SEGMENTS);
ad666a0f 3889 }
f92363d1
SR
3890 ioc->shost->sg_tablesize = sg_tablesize;
3891
fd0331b3
SS
3892 ioc->internal_depth = min_t(int, (facts->HighPriorityCredit + (5)),
3893 (facts->RequestCredit / 4));
3894 if (ioc->internal_depth < INTERNAL_CMDS_COUNT) {
3895 if (facts->RequestCredit <= (INTERNAL_CMDS_COUNT +
3896 INTERNAL_SCSIIO_CMDS_COUNT)) {
3897 pr_err(MPT3SAS_FMT "IOC doesn't have enough Request \
3898 Credits, it has just %d number of credits\n",
3899 ioc->name, facts->RequestCredit);
3900 return -ENOMEM;
3901 }
3902 ioc->internal_depth = 10;
3903 }
3904
3905 ioc->hi_priority_depth = ioc->internal_depth - (5);
f92363d1
SR
3906 /* command line tunables for max controller queue depth */
3907 if (max_queue_depth != -1 && max_queue_depth != 0) {
3908 max_request_credit = min_t(u16, max_queue_depth +
fd0331b3 3909 ioc->internal_depth, facts->RequestCredit);
f92363d1
SR
3910 if (max_request_credit > MAX_HBA_QUEUE_DEPTH)
3911 max_request_credit = MAX_HBA_QUEUE_DEPTH;
06f5f976
SR
3912 } else if (reset_devices)
3913 max_request_credit = min_t(u16, facts->RequestCredit,
3914 (MPT3SAS_KDUMP_SCSI_IO_DEPTH + ioc->internal_depth));
3915 else
f92363d1
SR
3916 max_request_credit = min_t(u16, facts->RequestCredit,
3917 MAX_HBA_QUEUE_DEPTH);
3918
fd0331b3
SS
3919 /* Firmware maintains additional facts->HighPriorityCredit number of
3920 * credits for HiPriprity Request messages, so hba queue depth will be
3921 * sum of max_request_credit and high priority queue depth.
3922 */
3923 ioc->hba_queue_depth = max_request_credit + ioc->hi_priority_depth;
f92363d1
SR
3924
3925 /* request frame size */
3926 ioc->request_sz = facts->IOCRequestFrameSize * 4;
3927
3928 /* reply frame size */
3929 ioc->reply_sz = facts->ReplyFrameSize * 4;
3930
ebb3024e
SS
3931 /* chain segment size */
3932 if (ioc->hba_mpi_version_belonged != MPI2_VERSION) {
3933 if (facts->IOCMaxChainSegmentSize)
3934 ioc->chain_segment_sz =
3935 facts->IOCMaxChainSegmentSize *
3936 MAX_CHAIN_ELEMT_SZ;
3937 else
3938 /* set to 128 bytes size if IOCMaxChainSegmentSize is zero */
3939 ioc->chain_segment_sz = DEFAULT_NUM_FWCHAIN_ELEMTS *
3940 MAX_CHAIN_ELEMT_SZ;
3941 } else
3942 ioc->chain_segment_sz = ioc->request_sz;
3943
f92363d1
SR
3944 /* calculate the max scatter element size */
3945 sge_size = max_t(u16, ioc->sge_size, ioc->sge_size_ieee);
3946
3947 retry_allocation:
3948 total_sz = 0;
3949 /* calculate number of sg elements left over in the 1st frame */
3950 max_sge_elements = ioc->request_sz - ((sizeof(Mpi2SCSIIORequest_t) -
3951 sizeof(Mpi2SGEIOUnion_t)) + sge_size);
3952 ioc->max_sges_in_main_message = max_sge_elements/sge_size;
3953
3954 /* now do the same for a chain buffer */
ebb3024e 3955 max_sge_elements = ioc->chain_segment_sz - sge_size;
f92363d1
SR
3956 ioc->max_sges_in_chain_message = max_sge_elements/sge_size;
3957
3958 /*
3959 * MPT3SAS_SG_DEPTH = CONFIG_FUSION_MAX_SGE
3960 */
3961 chains_needed_per_io = ((ioc->shost->sg_tablesize -
3962 ioc->max_sges_in_main_message)/ioc->max_sges_in_chain_message)
3963 + 1;
3964 if (chains_needed_per_io > facts->MaxChainDepth) {
3965 chains_needed_per_io = facts->MaxChainDepth;
3966 ioc->shost->sg_tablesize = min_t(u16,
3967 ioc->max_sges_in_main_message + (ioc->max_sges_in_chain_message
3968 * chains_needed_per_io), ioc->shost->sg_tablesize);
3969 }
3970 ioc->chains_needed_per_io = chains_needed_per_io;
3971
3972 /* reply free queue sizing - taking into account for 64 FW events */
3973 ioc->reply_free_queue_depth = ioc->hba_queue_depth + 64;
3974
3975 /* calculate reply descriptor post queue depth */
3976 ioc->reply_post_queue_depth = ioc->hba_queue_depth +
3977 ioc->reply_free_queue_depth + 1 ;
3978 /* align the reply post queue on the next 16 count boundary */
3979 if (ioc->reply_post_queue_depth % 16)
3980 ioc->reply_post_queue_depth += 16 -
3981 (ioc->reply_post_queue_depth % 16);
3982
f92363d1
SR
3983 if (ioc->reply_post_queue_depth >
3984 facts->MaxReplyDescriptorPostQueueDepth) {
3985 ioc->reply_post_queue_depth =
3986 facts->MaxReplyDescriptorPostQueueDepth -
3987 (facts->MaxReplyDescriptorPostQueueDepth % 16);
3988 ioc->hba_queue_depth =
3989 ((ioc->reply_post_queue_depth - 64) / 2) - 1;
3990 ioc->reply_free_queue_depth = ioc->hba_queue_depth + 64;
3991 }
3992
3993 dinitprintk(ioc, pr_info(MPT3SAS_FMT "scatter gather: " \
3994 "sge_in_main_msg(%d), sge_per_chain(%d), sge_per_io(%d), "
3995 "chains_per_io(%d)\n", ioc->name, ioc->max_sges_in_main_message,
3996 ioc->max_sges_in_chain_message, ioc->shost->sg_tablesize,
3997 ioc->chains_needed_per_io));
3998
9b05c91a
SR
3999 /* reply post queue, 16 byte align */
4000 reply_post_free_sz = ioc->reply_post_queue_depth *
4001 sizeof(Mpi2DefaultReplyDescriptor_t);
4002
4003 sz = reply_post_free_sz;
4004 if (_base_is_controller_msix_enabled(ioc) && !ioc->rdpq_array_enable)
4005 sz *= ioc->reply_queue_count;
4006
4007 ioc->reply_post = kcalloc((ioc->rdpq_array_enable) ?
4008 (ioc->reply_queue_count):1,
4009 sizeof(struct reply_post_struct), GFP_KERNEL);
4010
4011 if (!ioc->reply_post) {
4012 pr_err(MPT3SAS_FMT "reply_post_free pool: kcalloc failed\n",
4013 ioc->name);
4014 goto out;
4015 }
e9d98418
RP
4016 ioc->reply_post_free_dma_pool = dma_pool_create("reply_post_free pool",
4017 &ioc->pdev->dev, sz, 16, 0);
9b05c91a
SR
4018 if (!ioc->reply_post_free_dma_pool) {
4019 pr_err(MPT3SAS_FMT
e9d98418 4020 "reply_post_free pool: dma_pool_create failed\n",
9b05c91a
SR
4021 ioc->name);
4022 goto out;
4023 }
4024 i = 0;
4025 do {
4026 ioc->reply_post[i].reply_post_free =
e9d98418 4027 dma_pool_alloc(ioc->reply_post_free_dma_pool,
9b05c91a
SR
4028 GFP_KERNEL,
4029 &ioc->reply_post[i].reply_post_free_dma);
4030 if (!ioc->reply_post[i].reply_post_free) {
4031 pr_err(MPT3SAS_FMT
e9d98418 4032 "reply_post_free pool: dma_pool_alloc failed\n",
9b05c91a
SR
4033 ioc->name);
4034 goto out;
4035 }
4036 memset(ioc->reply_post[i].reply_post_free, 0, sz);
4037 dinitprintk(ioc, pr_info(MPT3SAS_FMT
4038 "reply post free pool (0x%p): depth(%d),"
4039 "element_size(%d), pool_size(%d kB)\n", ioc->name,
4040 ioc->reply_post[i].reply_post_free,
4041 ioc->reply_post_queue_depth, 8, sz/1024));
4042 dinitprintk(ioc, pr_info(MPT3SAS_FMT
4043 "reply_post_free_dma = (0x%llx)\n", ioc->name,
4044 (unsigned long long)
4045 ioc->reply_post[i].reply_post_free_dma));
4046 total_sz += sz;
4047 } while (ioc->rdpq_array_enable && (++i < ioc->reply_queue_count));
4048
4049 if (ioc->dma_mask == 64) {
4050 if (_base_change_consistent_dma_mask(ioc, ioc->pdev) != 0) {
4051 pr_warn(MPT3SAS_FMT
4052 "no suitable consistent DMA mask for %s\n",
4053 ioc->name, pci_name(ioc->pdev));
4054 goto out;
4055 }
4056 }
4057
f92363d1
SR
4058 ioc->scsiio_depth = ioc->hba_queue_depth -
4059 ioc->hi_priority_depth - ioc->internal_depth;
4060
4061 /* set the scsi host can_queue depth
4062 * with some internal commands that could be outstanding
4063 */
fd0331b3 4064 ioc->shost->can_queue = ioc->scsiio_depth - INTERNAL_SCSIIO_CMDS_COUNT;
f92363d1
SR
4065 dinitprintk(ioc, pr_info(MPT3SAS_FMT
4066 "scsi host: can_queue depth (%d)\n",
4067 ioc->name, ioc->shost->can_queue));
4068
4069
4070 /* contiguous pool for request and chains, 16 byte align, one extra "
4071 * "frame for smid=0
4072 */
4073 ioc->chain_depth = ioc->chains_needed_per_io * ioc->scsiio_depth;
4074 sz = ((ioc->scsiio_depth + 1) * ioc->request_sz);
4075
4076 /* hi-priority queue */
4077 sz += (ioc->hi_priority_depth * ioc->request_sz);
4078
4079 /* internal queue */
4080 sz += (ioc->internal_depth * ioc->request_sz);
4081
4082 ioc->request_dma_sz = sz;
4083 ioc->request = pci_alloc_consistent(ioc->pdev, sz, &ioc->request_dma);
4084 if (!ioc->request) {
4085 pr_err(MPT3SAS_FMT "request pool: pci_alloc_consistent " \
4086 "failed: hba_depth(%d), chains_per_io(%d), frame_sz(%d), "
4087 "total(%d kB)\n", ioc->name, ioc->hba_queue_depth,
4088 ioc->chains_needed_per_io, ioc->request_sz, sz/1024);
4089 if (ioc->scsiio_depth < MPT3SAS_SAS_QUEUE_DEPTH)
4090 goto out;
fd0331b3
SS
4091 retry_sz = 64;
4092 ioc->hba_queue_depth -= retry_sz;
8ff045c9 4093 _base_release_memory_pools(ioc);
f92363d1
SR
4094 goto retry_allocation;
4095 }
4096
4097 if (retry_sz)
4098 pr_err(MPT3SAS_FMT "request pool: pci_alloc_consistent " \
4099 "succeed: hba_depth(%d), chains_per_io(%d), frame_sz(%d), "
4100 "total(%d kb)\n", ioc->name, ioc->hba_queue_depth,
4101 ioc->chains_needed_per_io, ioc->request_sz, sz/1024);
4102
4103 /* hi-priority queue */
4104 ioc->hi_priority = ioc->request + ((ioc->scsiio_depth + 1) *
4105 ioc->request_sz);
4106 ioc->hi_priority_dma = ioc->request_dma + ((ioc->scsiio_depth + 1) *
4107 ioc->request_sz);
4108
4109 /* internal queue */
4110 ioc->internal = ioc->hi_priority + (ioc->hi_priority_depth *
4111 ioc->request_sz);
4112 ioc->internal_dma = ioc->hi_priority_dma + (ioc->hi_priority_depth *
4113 ioc->request_sz);
4114
4115 dinitprintk(ioc, pr_info(MPT3SAS_FMT
4116 "request pool(0x%p): depth(%d), frame_size(%d), pool_size(%d kB)\n",
4117 ioc->name, ioc->request, ioc->hba_queue_depth, ioc->request_sz,
4118 (ioc->hba_queue_depth * ioc->request_sz)/1024));
4119
4120 dinitprintk(ioc, pr_info(MPT3SAS_FMT "request pool: dma(0x%llx)\n",
4121 ioc->name, (unsigned long long) ioc->request_dma));
4122 total_sz += sz;
4123
4124 sz = ioc->scsiio_depth * sizeof(struct scsiio_tracker);
4125 ioc->scsi_lookup_pages = get_order(sz);
4126 ioc->scsi_lookup = (struct scsiio_tracker *)__get_free_pages(
4127 GFP_KERNEL, ioc->scsi_lookup_pages);
4128 if (!ioc->scsi_lookup) {
4129 pr_err(MPT3SAS_FMT "scsi_lookup: get_free_pages failed, sz(%d)\n",
4130 ioc->name, (int)sz);
4131 goto out;
4132 }
4133
4134 dinitprintk(ioc, pr_info(MPT3SAS_FMT "scsiio(0x%p): depth(%d)\n",
4135 ioc->name, ioc->request, ioc->scsiio_depth));
4136
4137 ioc->chain_depth = min_t(u32, ioc->chain_depth, MAX_CHAIN_DEPTH);
4138 sz = ioc->chain_depth * sizeof(struct chain_tracker);
4139 ioc->chain_pages = get_order(sz);
4140 ioc->chain_lookup = (struct chain_tracker *)__get_free_pages(
4141 GFP_KERNEL, ioc->chain_pages);
4142 if (!ioc->chain_lookup) {
4143 pr_err(MPT3SAS_FMT "chain_lookup: __get_free_pages failed\n",
4144 ioc->name);
4145 goto out;
4146 }
e9d98418 4147 ioc->chain_dma_pool = dma_pool_create("chain pool", &ioc->pdev->dev,
ebb3024e 4148 ioc->chain_segment_sz, 16, 0);
f92363d1 4149 if (!ioc->chain_dma_pool) {
e9d98418 4150 pr_err(MPT3SAS_FMT "chain_dma_pool: dma_pool_create failed\n",
f92363d1
SR
4151 ioc->name);
4152 goto out;
4153 }
4154 for (i = 0; i < ioc->chain_depth; i++) {
e9d98418 4155 ioc->chain_lookup[i].chain_buffer = dma_pool_alloc(
f92363d1
SR
4156 ioc->chain_dma_pool , GFP_KERNEL,
4157 &ioc->chain_lookup[i].chain_buffer_dma);
4158 if (!ioc->chain_lookup[i].chain_buffer) {
4159 ioc->chain_depth = i;
4160 goto chain_done;
4161 }
ebb3024e 4162 total_sz += ioc->chain_segment_sz;
f92363d1
SR
4163 }
4164 chain_done:
4165 dinitprintk(ioc, pr_info(MPT3SAS_FMT
4166 "chain pool depth(%d), frame_size(%d), pool_size(%d kB)\n",
ebb3024e
SS
4167 ioc->name, ioc->chain_depth, ioc->chain_segment_sz,
4168 ((ioc->chain_depth * ioc->chain_segment_sz))/1024));
f92363d1
SR
4169
4170 /* initialize hi-priority queue smid's */
4171 ioc->hpr_lookup = kcalloc(ioc->hi_priority_depth,
4172 sizeof(struct request_tracker), GFP_KERNEL);
4173 if (!ioc->hpr_lookup) {
4174 pr_err(MPT3SAS_FMT "hpr_lookup: kcalloc failed\n",
4175 ioc->name);
4176 goto out;
4177 }
4178 ioc->hi_priority_smid = ioc->scsiio_depth + 1;
4179 dinitprintk(ioc, pr_info(MPT3SAS_FMT
4180 "hi_priority(0x%p): depth(%d), start smid(%d)\n",
4181 ioc->name, ioc->hi_priority,
4182 ioc->hi_priority_depth, ioc->hi_priority_smid));
4183
4184 /* initialize internal queue smid's */
4185 ioc->internal_lookup = kcalloc(ioc->internal_depth,
4186 sizeof(struct request_tracker), GFP_KERNEL);
4187 if (!ioc->internal_lookup) {
4188 pr_err(MPT3SAS_FMT "internal_lookup: kcalloc failed\n",
4189 ioc->name);
4190 goto out;
4191 }
4192 ioc->internal_smid = ioc->hi_priority_smid + ioc->hi_priority_depth;
4193 dinitprintk(ioc, pr_info(MPT3SAS_FMT
4194 "internal(0x%p): depth(%d), start smid(%d)\n",
4195 ioc->name, ioc->internal,
4196 ioc->internal_depth, ioc->internal_smid));
016d5c35
SPS
4197 /*
4198 * The number of NVMe page sized blocks needed is:
4199 * (((sg_tablesize * 8) - 1) / (page_size - 8)) + 1
4200 * ((sg_tablesize * 8) - 1) is the max PRP's minus the first PRP entry
4201 * that is placed in the main message frame. 8 is the size of each PRP
4202 * entry or PRP list pointer entry. 8 is subtracted from page_size
4203 * because of the PRP list pointer entry at the end of a page, so this
4204 * is not counted as a PRP entry. The 1 added page is a round up.
4205 *
4206 * To avoid allocation failures due to the amount of memory that could
4207 * be required for NVMe PRP's, only each set of NVMe blocks will be
4208 * contiguous, so a new set is allocated for each possible I/O.
4209 */
4210 if (ioc->facts.ProtocolFlags & MPI2_IOCFACTS_PROTOCOL_NVME_DEVICES) {
4211 nvme_blocks_needed =
4212 (ioc->shost->sg_tablesize * NVME_PRP_SIZE) - 1;
4213 nvme_blocks_needed /= (ioc->page_size - NVME_PRP_SIZE);
4214 nvme_blocks_needed++;
4215
4216 sz = nvme_blocks_needed * ioc->page_size;
4217 ioc->pcie_sgl_dma_pool =
13a06405 4218 dma_pool_create("PCIe SGL pool", &ioc->pdev->dev, sz, 16, 0);
016d5c35
SPS
4219 if (!ioc->pcie_sgl_dma_pool) {
4220 pr_info(MPT3SAS_FMT
13a06405 4221 "PCIe SGL pool: dma_pool_create failed\n",
016d5c35
SPS
4222 ioc->name);
4223 goto out;
4224 }
4225 for (i = 0; i < ioc->scsiio_depth; i++) {
4226 ioc->scsi_lookup[i].pcie_sg_list.pcie_sgl =
13a06405 4227 dma_pool_alloc(ioc->pcie_sgl_dma_pool,
016d5c35
SPS
4228 GFP_KERNEL,
4229 &ioc->scsi_lookup[i].pcie_sg_list.pcie_sgl_dma);
4230 if (!ioc->scsi_lookup[i].pcie_sg_list.pcie_sgl) {
4231 pr_info(MPT3SAS_FMT
13a06405 4232 "PCIe SGL pool: dma_pool_alloc failed\n",
016d5c35
SPS
4233 ioc->name);
4234 goto out;
4235 }
4236 }
f92363d1 4237
016d5c35
SPS
4238 dinitprintk(ioc, pr_info(MPT3SAS_FMT "PCIe sgl pool depth(%d), "
4239 "element_size(%d), pool_size(%d kB)\n", ioc->name,
4240 ioc->scsiio_depth, sz, (sz * ioc->scsiio_depth)/1024));
4241 total_sz += sz * ioc->scsiio_depth;
4242 }
f92363d1
SR
4243 /* sense buffers, 4 byte align */
4244 sz = ioc->scsiio_depth * SCSI_SENSE_BUFFERSIZE;
e9d98418
RP
4245 ioc->sense_dma_pool = dma_pool_create("sense pool", &ioc->pdev->dev, sz,
4246 4, 0);
f92363d1 4247 if (!ioc->sense_dma_pool) {
e9d98418 4248 pr_err(MPT3SAS_FMT "sense pool: dma_pool_create failed\n",
f92363d1
SR
4249 ioc->name);
4250 goto out;
4251 }
e9d98418 4252 ioc->sense = dma_pool_alloc(ioc->sense_dma_pool, GFP_KERNEL,
f92363d1
SR
4253 &ioc->sense_dma);
4254 if (!ioc->sense) {
e9d98418 4255 pr_err(MPT3SAS_FMT "sense pool: dma_pool_alloc failed\n",
f92363d1
SR
4256 ioc->name);
4257 goto out;
4258 }
4259 dinitprintk(ioc, pr_info(MPT3SAS_FMT
4260 "sense pool(0x%p): depth(%d), element_size(%d), pool_size"
4261 "(%d kB)\n", ioc->name, ioc->sense, ioc->scsiio_depth,
4262 SCSI_SENSE_BUFFERSIZE, sz/1024));
4263 dinitprintk(ioc, pr_info(MPT3SAS_FMT "sense_dma(0x%llx)\n",
4264 ioc->name, (unsigned long long)ioc->sense_dma));
4265 total_sz += sz;
4266
4267 /* reply pool, 4 byte align */
4268 sz = ioc->reply_free_queue_depth * ioc->reply_sz;
e9d98418
RP
4269 ioc->reply_dma_pool = dma_pool_create("reply pool", &ioc->pdev->dev, sz,
4270 4, 0);
f92363d1 4271 if (!ioc->reply_dma_pool) {
e9d98418 4272 pr_err(MPT3SAS_FMT "reply pool: dma_pool_create failed\n",
f92363d1
SR
4273 ioc->name);
4274 goto out;
4275 }
e9d98418 4276 ioc->reply = dma_pool_alloc(ioc->reply_dma_pool, GFP_KERNEL,
f92363d1
SR
4277 &ioc->reply_dma);
4278 if (!ioc->reply) {
e9d98418 4279 pr_err(MPT3SAS_FMT "reply pool: dma_pool_alloc failed\n",
f92363d1
SR
4280 ioc->name);
4281 goto out;
4282 }
4283 ioc->reply_dma_min_address = (u32)(ioc->reply_dma);
4284 ioc->reply_dma_max_address = (u32)(ioc->reply_dma) + sz;
4285 dinitprintk(ioc, pr_info(MPT3SAS_FMT
4286 "reply pool(0x%p): depth(%d), frame_size(%d), pool_size(%d kB)\n",
4287 ioc->name, ioc->reply,
4288 ioc->reply_free_queue_depth, ioc->reply_sz, sz/1024));
4289 dinitprintk(ioc, pr_info(MPT3SAS_FMT "reply_dma(0x%llx)\n",
4290 ioc->name, (unsigned long long)ioc->reply_dma));
4291 total_sz += sz;
4292
4293 /* reply free queue, 16 byte align */
4294 sz = ioc->reply_free_queue_depth * 4;
e9d98418
RP
4295 ioc->reply_free_dma_pool = dma_pool_create("reply_free pool",
4296 &ioc->pdev->dev, sz, 16, 0);
f92363d1 4297 if (!ioc->reply_free_dma_pool) {
e9d98418 4298 pr_err(MPT3SAS_FMT "reply_free pool: dma_pool_create failed\n",
f92363d1
SR
4299 ioc->name);
4300 goto out;
4301 }
e9d98418 4302 ioc->reply_free = dma_pool_alloc(ioc->reply_free_dma_pool, GFP_KERNEL,
f92363d1
SR
4303 &ioc->reply_free_dma);
4304 if (!ioc->reply_free) {
e9d98418 4305 pr_err(MPT3SAS_FMT "reply_free pool: dma_pool_alloc failed\n",
f92363d1
SR
4306 ioc->name);
4307 goto out;
4308 }
4309 memset(ioc->reply_free, 0, sz);
4310 dinitprintk(ioc, pr_info(MPT3SAS_FMT "reply_free pool(0x%p): " \
4311 "depth(%d), element_size(%d), pool_size(%d kB)\n", ioc->name,
4312 ioc->reply_free, ioc->reply_free_queue_depth, 4, sz/1024));
4313 dinitprintk(ioc, pr_info(MPT3SAS_FMT
4314 "reply_free_dma (0x%llx)\n",
4315 ioc->name, (unsigned long long)ioc->reply_free_dma));
4316 total_sz += sz;
4317
f92363d1
SR
4318 ioc->config_page_sz = 512;
4319 ioc->config_page = pci_alloc_consistent(ioc->pdev,
4320 ioc->config_page_sz, &ioc->config_page_dma);
4321 if (!ioc->config_page) {
4322 pr_err(MPT3SAS_FMT
e9d98418 4323 "config page: dma_pool_alloc failed\n",
f92363d1
SR
4324 ioc->name);
4325 goto out;
4326 }
4327 dinitprintk(ioc, pr_info(MPT3SAS_FMT
4328 "config page(0x%p): size(%d)\n",
4329 ioc->name, ioc->config_page, ioc->config_page_sz));
4330 dinitprintk(ioc, pr_info(MPT3SAS_FMT "config_page_dma(0x%llx)\n",
4331 ioc->name, (unsigned long long)ioc->config_page_dma));
4332 total_sz += ioc->config_page_sz;
4333
4334 pr_info(MPT3SAS_FMT "Allocated physical memory: size(%d kB)\n",
4335 ioc->name, total_sz/1024);
4336 pr_info(MPT3SAS_FMT
4337 "Current Controller Queue Depth(%d),Max Controller Queue Depth(%d)\n",
4338 ioc->name, ioc->shost->can_queue, facts->RequestCredit);
4339 pr_info(MPT3SAS_FMT "Scatter Gather Elements per IO(%d)\n",
4340 ioc->name, ioc->shost->sg_tablesize);
4341 return 0;
4342
4343 out:
4344 return -ENOMEM;
4345}
4346
4347/**
4348 * mpt3sas_base_get_iocstate - Get the current state of a MPT adapter.
4349 * @ioc: Pointer to MPT_ADAPTER structure
4350 * @cooked: Request raw or cooked IOC state
4351 *
4352 * Returns all IOC Doorbell register bits if cooked==0, else just the
4353 * Doorbell bits in MPI_IOC_STATE_MASK.
4354 */
4355u32
4356mpt3sas_base_get_iocstate(struct MPT3SAS_ADAPTER *ioc, int cooked)
4357{
4358 u32 s, sc;
4359
4360 s = readl(&ioc->chip->Doorbell);
4361 sc = s & MPI2_IOC_STATE_MASK;
4362 return cooked ? sc : s;
4363}
4364
4365/**
4366 * _base_wait_on_iocstate - waiting on a particular ioc state
4367 * @ioc_state: controller state { READY, OPERATIONAL, or RESET }
4368 * @timeout: timeout in second
f92363d1
SR
4369 *
4370 * Returns 0 for success, non-zero for failure.
4371 */
4372static int
98c56ad3 4373_base_wait_on_iocstate(struct MPT3SAS_ADAPTER *ioc, u32 ioc_state, int timeout)
f92363d1
SR
4374{
4375 u32 count, cntdn;
4376 u32 current_state;
4377
4378 count = 0;
98c56ad3 4379 cntdn = 1000 * timeout;
f92363d1
SR
4380 do {
4381 current_state = mpt3sas_base_get_iocstate(ioc, 1);
4382 if (current_state == ioc_state)
4383 return 0;
4384 if (count && current_state == MPI2_IOC_STATE_FAULT)
4385 break;
98c56ad3
CO
4386
4387 usleep_range(1000, 1500);
f92363d1
SR
4388 count++;
4389 } while (--cntdn);
4390
4391 return current_state;
4392}
4393
4394/**
4395 * _base_wait_for_doorbell_int - waiting for controller interrupt(generated by
4396 * a write to the doorbell)
4397 * @ioc: per adapter object
4398 * @timeout: timeout in second
f92363d1
SR
4399 *
4400 * Returns 0 for success, non-zero for failure.
4401 *
4402 * Notes: MPI2_HIS_IOC2SYS_DB_STATUS - set to one when IOC writes to doorbell.
4403 */
4dc8c808 4404static int
98c56ad3 4405_base_diag_reset(struct MPT3SAS_ADAPTER *ioc);
4dc8c808 4406
f92363d1 4407static int
98c56ad3 4408_base_wait_for_doorbell_int(struct MPT3SAS_ADAPTER *ioc, int timeout)
f92363d1
SR
4409{
4410 u32 cntdn, count;
4411 u32 int_status;
4412
4413 count = 0;
98c56ad3 4414 cntdn = 1000 * timeout;
f92363d1
SR
4415 do {
4416 int_status = readl(&ioc->chip->HostInterruptStatus);
4417 if (int_status & MPI2_HIS_IOC2SYS_DB_STATUS) {
4418 dhsprintk(ioc, pr_info(MPT3SAS_FMT
4419 "%s: successful count(%d), timeout(%d)\n",
4420 ioc->name, __func__, count, timeout));
4421 return 0;
4422 }
98c56ad3
CO
4423
4424 usleep_range(1000, 1500);
4425 count++;
4426 } while (--cntdn);
4427
4428 pr_err(MPT3SAS_FMT
4429 "%s: failed due to timeout count(%d), int_status(%x)!\n",
4430 ioc->name, __func__, count, int_status);
4431 return -EFAULT;
4432}
4433
4434static int
4435_base_spin_on_doorbell_int(struct MPT3SAS_ADAPTER *ioc, int timeout)
4436{
4437 u32 cntdn, count;
4438 u32 int_status;
4439
4440 count = 0;
4441 cntdn = 2000 * timeout;
4442 do {
4443 int_status = readl(&ioc->chip->HostInterruptStatus);
4444 if (int_status & MPI2_HIS_IOC2SYS_DB_STATUS) {
4445 dhsprintk(ioc, pr_info(MPT3SAS_FMT
4446 "%s: successful count(%d), timeout(%d)\n",
4447 ioc->name, __func__, count, timeout));
4448 return 0;
4449 }
4450
4451 udelay(500);
f92363d1
SR
4452 count++;
4453 } while (--cntdn);
4454
4455 pr_err(MPT3SAS_FMT
4456 "%s: failed due to timeout count(%d), int_status(%x)!\n",
4457 ioc->name, __func__, count, int_status);
4458 return -EFAULT;
98c56ad3 4459
f92363d1
SR
4460}
4461
4462/**
4463 * _base_wait_for_doorbell_ack - waiting for controller to read the doorbell.
4464 * @ioc: per adapter object
4465 * @timeout: timeout in second
f92363d1
SR
4466 *
4467 * Returns 0 for success, non-zero for failure.
4468 *
4469 * Notes: MPI2_HIS_SYS2IOC_DB_STATUS - set to one when host writes to
4470 * doorbell.
4471 */
4472static int
98c56ad3 4473_base_wait_for_doorbell_ack(struct MPT3SAS_ADAPTER *ioc, int timeout)
f92363d1
SR
4474{
4475 u32 cntdn, count;
4476 u32 int_status;
4477 u32 doorbell;
4478
4479 count = 0;
98c56ad3 4480 cntdn = 1000 * timeout;
f92363d1
SR
4481 do {
4482 int_status = readl(&ioc->chip->HostInterruptStatus);
4483 if (!(int_status & MPI2_HIS_SYS2IOC_DB_STATUS)) {
4484 dhsprintk(ioc, pr_info(MPT3SAS_FMT
4485 "%s: successful count(%d), timeout(%d)\n",
4486 ioc->name, __func__, count, timeout));
4487 return 0;
4488 } else if (int_status & MPI2_HIS_IOC2SYS_DB_STATUS) {
4489 doorbell = readl(&ioc->chip->Doorbell);
4490 if ((doorbell & MPI2_IOC_STATE_MASK) ==
4491 MPI2_IOC_STATE_FAULT) {
4492 mpt3sas_base_fault_info(ioc , doorbell);
4493 return -EFAULT;
4494 }
4495 } else if (int_status == 0xFFFFFFFF)
4496 goto out;
4497
98c56ad3 4498 usleep_range(1000, 1500);
f92363d1
SR
4499 count++;
4500 } while (--cntdn);
4501
4502 out:
4503 pr_err(MPT3SAS_FMT
4504 "%s: failed due to timeout count(%d), int_status(%x)!\n",
4505 ioc->name, __func__, count, int_status);
4506 return -EFAULT;
4507}
4508
4509/**
4510 * _base_wait_for_doorbell_not_used - waiting for doorbell to not be in use
4511 * @ioc: per adapter object
4512 * @timeout: timeout in second
f92363d1
SR
4513 *
4514 * Returns 0 for success, non-zero for failure.
4515 *
4516 */
4517static int
98c56ad3 4518_base_wait_for_doorbell_not_used(struct MPT3SAS_ADAPTER *ioc, int timeout)
f92363d1
SR
4519{
4520 u32 cntdn, count;
4521 u32 doorbell_reg;
4522
4523 count = 0;
98c56ad3 4524 cntdn = 1000 * timeout;
f92363d1
SR
4525 do {
4526 doorbell_reg = readl(&ioc->chip->Doorbell);
4527 if (!(doorbell_reg & MPI2_DOORBELL_USED)) {
4528 dhsprintk(ioc, pr_info(MPT3SAS_FMT
4529 "%s: successful count(%d), timeout(%d)\n",
4530 ioc->name, __func__, count, timeout));
4531 return 0;
4532 }
98c56ad3
CO
4533
4534 usleep_range(1000, 1500);
f92363d1
SR
4535 count++;
4536 } while (--cntdn);
4537
4538 pr_err(MPT3SAS_FMT
4539 "%s: failed due to timeout count(%d), doorbell_reg(%x)!\n",
4540 ioc->name, __func__, count, doorbell_reg);
4541 return -EFAULT;
4542}
4543
4544/**
4545 * _base_send_ioc_reset - send doorbell reset
4546 * @ioc: per adapter object
4547 * @reset_type: currently only supports: MPI2_FUNCTION_IOC_MESSAGE_UNIT_RESET
4548 * @timeout: timeout in second
f92363d1
SR
4549 *
4550 * Returns 0 for success, non-zero for failure.
4551 */
4552static int
98c56ad3 4553_base_send_ioc_reset(struct MPT3SAS_ADAPTER *ioc, u8 reset_type, int timeout)
f92363d1
SR
4554{
4555 u32 ioc_state;
4556 int r = 0;
4557
4558 if (reset_type != MPI2_FUNCTION_IOC_MESSAGE_UNIT_RESET) {
4559 pr_err(MPT3SAS_FMT "%s: unknown reset_type\n",
4560 ioc->name, __func__);
4561 return -EFAULT;
4562 }
4563
4564 if (!(ioc->facts.IOCCapabilities &
4565 MPI2_IOCFACTS_CAPABILITY_EVENT_REPLAY))
4566 return -EFAULT;
4567
4568 pr_info(MPT3SAS_FMT "sending message unit reset !!\n", ioc->name);
4569
4570 writel(reset_type << MPI2_DOORBELL_FUNCTION_SHIFT,
4571 &ioc->chip->Doorbell);
98c56ad3 4572 if ((_base_wait_for_doorbell_ack(ioc, 15))) {
f92363d1
SR
4573 r = -EFAULT;
4574 goto out;
4575 }
98c56ad3 4576 ioc_state = _base_wait_on_iocstate(ioc, MPI2_IOC_STATE_READY, timeout);
f92363d1
SR
4577 if (ioc_state) {
4578 pr_err(MPT3SAS_FMT
4579 "%s: failed going to ready state (ioc_state=0x%x)\n",
4580 ioc->name, __func__, ioc_state);
4581 r = -EFAULT;
4582 goto out;
4583 }
4584 out:
4585 pr_info(MPT3SAS_FMT "message unit reset: %s\n",
4586 ioc->name, ((r == 0) ? "SUCCESS" : "FAILED"));
4587 return r;
4588}
4589
4590/**
4591 * _base_handshake_req_reply_wait - send request thru doorbell interface
4592 * @ioc: per adapter object
4593 * @request_bytes: request length
4594 * @request: pointer having request payload
4595 * @reply_bytes: reply length
4596 * @reply: pointer to reply payload
4597 * @timeout: timeout in second
f92363d1
SR
4598 *
4599 * Returns 0 for success, non-zero for failure.
4600 */
4601static int
4602_base_handshake_req_reply_wait(struct MPT3SAS_ADAPTER *ioc, int request_bytes,
98c56ad3 4603 u32 *request, int reply_bytes, u16 *reply, int timeout)
f92363d1
SR
4604{
4605 MPI2DefaultReply_t *default_reply = (MPI2DefaultReply_t *)reply;
4606 int i;
4607 u8 failed;
f92363d1
SR
4608 __le32 *mfp;
4609
4610 /* make sure doorbell is not in use */
4611 if ((readl(&ioc->chip->Doorbell) & MPI2_DOORBELL_USED)) {
4612 pr_err(MPT3SAS_FMT
4613 "doorbell is in use (line=%d)\n",
4614 ioc->name, __LINE__);
4615 return -EFAULT;
4616 }
4617
4618 /* clear pending doorbell interrupts from previous state changes */
4619 if (readl(&ioc->chip->HostInterruptStatus) &
4620 MPI2_HIS_IOC2SYS_DB_STATUS)
4621 writel(0, &ioc->chip->HostInterruptStatus);
4622
4623 /* send message to ioc */
4624 writel(((MPI2_FUNCTION_HANDSHAKE<<MPI2_DOORBELL_FUNCTION_SHIFT) |
4625 ((request_bytes/4)<<MPI2_DOORBELL_ADD_DWORDS_SHIFT)),
4626 &ioc->chip->Doorbell);
4627
98c56ad3 4628 if ((_base_spin_on_doorbell_int(ioc, 5))) {
f92363d1
SR
4629 pr_err(MPT3SAS_FMT
4630 "doorbell handshake int failed (line=%d)\n",
4631 ioc->name, __LINE__);
4632 return -EFAULT;
4633 }
4634 writel(0, &ioc->chip->HostInterruptStatus);
4635
98c56ad3 4636 if ((_base_wait_for_doorbell_ack(ioc, 5))) {
f92363d1
SR
4637 pr_err(MPT3SAS_FMT
4638 "doorbell handshake ack failed (line=%d)\n",
4639 ioc->name, __LINE__);
4640 return -EFAULT;
4641 }
4642
4643 /* send message 32-bits at a time */
4644 for (i = 0, failed = 0; i < request_bytes/4 && !failed; i++) {
4645 writel(cpu_to_le32(request[i]), &ioc->chip->Doorbell);
98c56ad3 4646 if ((_base_wait_for_doorbell_ack(ioc, 5)))
f92363d1
SR
4647 failed = 1;
4648 }
4649
4650 if (failed) {
4651 pr_err(MPT3SAS_FMT
4652 "doorbell handshake sending request failed (line=%d)\n",
4653 ioc->name, __LINE__);
4654 return -EFAULT;
4655 }
4656
4657 /* now wait for the reply */
98c56ad3 4658 if ((_base_wait_for_doorbell_int(ioc, timeout))) {
f92363d1
SR
4659 pr_err(MPT3SAS_FMT
4660 "doorbell handshake int failed (line=%d)\n",
4661 ioc->name, __LINE__);
4662 return -EFAULT;
4663 }
4664
4665 /* read the first two 16-bits, it gives the total length of the reply */
4666 reply[0] = le16_to_cpu(readl(&ioc->chip->Doorbell)
4667 & MPI2_DOORBELL_DATA_MASK);
4668 writel(0, &ioc->chip->HostInterruptStatus);
98c56ad3 4669 if ((_base_wait_for_doorbell_int(ioc, 5))) {
f92363d1
SR
4670 pr_err(MPT3SAS_FMT
4671 "doorbell handshake int failed (line=%d)\n",
4672 ioc->name, __LINE__);
4673 return -EFAULT;
4674 }
4675 reply[1] = le16_to_cpu(readl(&ioc->chip->Doorbell)
4676 & MPI2_DOORBELL_DATA_MASK);
4677 writel(0, &ioc->chip->HostInterruptStatus);
4678
4679 for (i = 2; i < default_reply->MsgLength * 2; i++) {
98c56ad3 4680 if ((_base_wait_for_doorbell_int(ioc, 5))) {
f92363d1
SR
4681 pr_err(MPT3SAS_FMT
4682 "doorbell handshake int failed (line=%d)\n",
4683 ioc->name, __LINE__);
4684 return -EFAULT;
4685 }
4686 if (i >= reply_bytes/2) /* overflow case */
8bbb1cf6 4687 readl(&ioc->chip->Doorbell);
f92363d1
SR
4688 else
4689 reply[i] = le16_to_cpu(readl(&ioc->chip->Doorbell)
4690 & MPI2_DOORBELL_DATA_MASK);
4691 writel(0, &ioc->chip->HostInterruptStatus);
4692 }
4693
98c56ad3
CO
4694 _base_wait_for_doorbell_int(ioc, 5);
4695 if (_base_wait_for_doorbell_not_used(ioc, 5) != 0) {
f92363d1
SR
4696 dhsprintk(ioc, pr_info(MPT3SAS_FMT
4697 "doorbell is in use (line=%d)\n", ioc->name, __LINE__));
4698 }
4699 writel(0, &ioc->chip->HostInterruptStatus);
4700
4701 if (ioc->logging_level & MPT_DEBUG_INIT) {
4702 mfp = (__le32 *)reply;
4703 pr_info("\toffset:data\n");
4704 for (i = 0; i < reply_bytes/4; i++)
4705 pr_info("\t[0x%02x]:%08x\n", i*4,
4706 le32_to_cpu(mfp[i]));
4707 }
4708 return 0;
4709}
4710
4711/**
4712 * mpt3sas_base_sas_iounit_control - send sas iounit control to FW
4713 * @ioc: per adapter object
4714 * @mpi_reply: the reply payload from FW
4715 * @mpi_request: the request payload sent to FW
4716 *
4717 * The SAS IO Unit Control Request message allows the host to perform low-level
4718 * operations, such as resets on the PHYs of the IO Unit, also allows the host
4719 * to obtain the IOC assigned device handles for a device if it has other
4720 * identifying information about the device, in addition allows the host to
4721 * remove IOC resources associated with the device.
4722 *
4723 * Returns 0 for success, non-zero for failure.
4724 */
4725int
4726mpt3sas_base_sas_iounit_control(struct MPT3SAS_ADAPTER *ioc,
4727 Mpi2SasIoUnitControlReply_t *mpi_reply,
4728 Mpi2SasIoUnitControlRequest_t *mpi_request)
4729{
4730 u16 smid;
4731 u32 ioc_state;
eb44552b 4732 bool issue_reset = false;
f92363d1
SR
4733 int rc;
4734 void *request;
4735 u16 wait_state_count;
4736
4737 dinitprintk(ioc, pr_info(MPT3SAS_FMT "%s\n", ioc->name,
4738 __func__));
4739
4740 mutex_lock(&ioc->base_cmds.mutex);
4741
4742 if (ioc->base_cmds.status != MPT3_CMD_NOT_USED) {
4743 pr_err(MPT3SAS_FMT "%s: base_cmd in use\n",
4744 ioc->name, __func__);
4745 rc = -EAGAIN;
4746 goto out;
4747 }
4748
4749 wait_state_count = 0;
4750 ioc_state = mpt3sas_base_get_iocstate(ioc, 1);
4751 while (ioc_state != MPI2_IOC_STATE_OPERATIONAL) {
4752 if (wait_state_count++ == 10) {
4753 pr_err(MPT3SAS_FMT
4754 "%s: failed due to ioc not operational\n",
4755 ioc->name, __func__);
4756 rc = -EFAULT;
4757 goto out;
4758 }
4759 ssleep(1);
4760 ioc_state = mpt3sas_base_get_iocstate(ioc, 1);
4761 pr_info(MPT3SAS_FMT
4762 "%s: waiting for operational state(count=%d)\n",
4763 ioc->name, __func__, wait_state_count);
4764 }
4765
4766 smid = mpt3sas_base_get_smid(ioc, ioc->base_cb_idx);
4767 if (!smid) {
4768 pr_err(MPT3SAS_FMT "%s: failed obtaining a smid\n",
4769 ioc->name, __func__);
4770 rc = -EAGAIN;
4771 goto out;
4772 }
4773
4774 rc = 0;
4775 ioc->base_cmds.status = MPT3_CMD_PENDING;
4776 request = mpt3sas_base_get_msg_frame(ioc, smid);
4777 ioc->base_cmds.smid = smid;
4778 memcpy(request, mpi_request, sizeof(Mpi2SasIoUnitControlRequest_t));
4779 if (mpi_request->Operation == MPI2_SAS_OP_PHY_HARD_RESET ||
4780 mpi_request->Operation == MPI2_SAS_OP_PHY_LINK_RESET)
4781 ioc->ioc_link_reset_in_progress = 1;
4782 init_completion(&ioc->base_cmds.done);
81c16f83 4783 ioc->put_smid_default(ioc, smid);
8bbb1cf6 4784 wait_for_completion_timeout(&ioc->base_cmds.done,
f92363d1
SR
4785 msecs_to_jiffies(10000));
4786 if ((mpi_request->Operation == MPI2_SAS_OP_PHY_HARD_RESET ||
4787 mpi_request->Operation == MPI2_SAS_OP_PHY_LINK_RESET) &&
4788 ioc->ioc_link_reset_in_progress)
4789 ioc->ioc_link_reset_in_progress = 0;
4790 if (!(ioc->base_cmds.status & MPT3_CMD_COMPLETE)) {
4791 pr_err(MPT3SAS_FMT "%s: timeout\n",
4792 ioc->name, __func__);
4793 _debug_dump_mf(mpi_request,
4794 sizeof(Mpi2SasIoUnitControlRequest_t)/4);
4795 if (!(ioc->base_cmds.status & MPT3_CMD_RESET))
eb44552b 4796 issue_reset = true;
f92363d1
SR
4797 goto issue_host_reset;
4798 }
4799 if (ioc->base_cmds.status & MPT3_CMD_REPLY_VALID)
4800 memcpy(mpi_reply, ioc->base_cmds.reply,
4801 sizeof(Mpi2SasIoUnitControlReply_t));
4802 else
4803 memset(mpi_reply, 0, sizeof(Mpi2SasIoUnitControlReply_t));
4804 ioc->base_cmds.status = MPT3_CMD_NOT_USED;
4805 goto out;
4806
4807 issue_host_reset:
4808 if (issue_reset)
98c56ad3 4809 mpt3sas_base_hard_reset_handler(ioc, FORCE_BIG_HAMMER);
f92363d1
SR
4810 ioc->base_cmds.status = MPT3_CMD_NOT_USED;
4811 rc = -EFAULT;
4812 out:
4813 mutex_unlock(&ioc->base_cmds.mutex);
4814 return rc;
4815}
4816
4817/**
4818 * mpt3sas_base_scsi_enclosure_processor - sending request to sep device
4819 * @ioc: per adapter object
4820 * @mpi_reply: the reply payload from FW
4821 * @mpi_request: the request payload sent to FW
4822 *
4823 * The SCSI Enclosure Processor request message causes the IOC to
4824 * communicate with SES devices to control LED status signals.
4825 *
4826 * Returns 0 for success, non-zero for failure.
4827 */
4828int
4829mpt3sas_base_scsi_enclosure_processor(struct MPT3SAS_ADAPTER *ioc,
4830 Mpi2SepReply_t *mpi_reply, Mpi2SepRequest_t *mpi_request)
4831{
4832 u16 smid;
4833 u32 ioc_state;
eb44552b 4834 bool issue_reset = false;
f92363d1
SR
4835 int rc;
4836 void *request;
4837 u16 wait_state_count;
4838
4839 dinitprintk(ioc, pr_info(MPT3SAS_FMT "%s\n", ioc->name,
4840 __func__));
4841
4842 mutex_lock(&ioc->base_cmds.mutex);
4843
4844 if (ioc->base_cmds.status != MPT3_CMD_NOT_USED) {
4845 pr_err(MPT3SAS_FMT "%s: base_cmd in use\n",
4846 ioc->name, __func__);
4847 rc = -EAGAIN;
4848 goto out;
4849 }
4850
4851 wait_state_count = 0;
4852 ioc_state = mpt3sas_base_get_iocstate(ioc, 1);
4853 while (ioc_state != MPI2_IOC_STATE_OPERATIONAL) {
4854 if (wait_state_count++ == 10) {
4855 pr_err(MPT3SAS_FMT
4856 "%s: failed due to ioc not operational\n",
4857 ioc->name, __func__);
4858 rc = -EFAULT;
4859 goto out;
4860 }
4861 ssleep(1);
4862 ioc_state = mpt3sas_base_get_iocstate(ioc, 1);
4863 pr_info(MPT3SAS_FMT
4864 "%s: waiting for operational state(count=%d)\n",
4865 ioc->name,
4866 __func__, wait_state_count);
4867 }
4868
4869 smid = mpt3sas_base_get_smid(ioc, ioc->base_cb_idx);
4870 if (!smid) {
4871 pr_err(MPT3SAS_FMT "%s: failed obtaining a smid\n",
4872 ioc->name, __func__);
4873 rc = -EAGAIN;
4874 goto out;
4875 }
4876
4877 rc = 0;
4878 ioc->base_cmds.status = MPT3_CMD_PENDING;
4879 request = mpt3sas_base_get_msg_frame(ioc, smid);
4880 ioc->base_cmds.smid = smid;
4881 memcpy(request, mpi_request, sizeof(Mpi2SepReply_t));
4882 init_completion(&ioc->base_cmds.done);
81c16f83 4883 ioc->put_smid_default(ioc, smid);
8bbb1cf6 4884 wait_for_completion_timeout(&ioc->base_cmds.done,
f92363d1
SR
4885 msecs_to_jiffies(10000));
4886 if (!(ioc->base_cmds.status & MPT3_CMD_COMPLETE)) {
4887 pr_err(MPT3SAS_FMT "%s: timeout\n",
4888 ioc->name, __func__);
4889 _debug_dump_mf(mpi_request,
4890 sizeof(Mpi2SepRequest_t)/4);
4891 if (!(ioc->base_cmds.status & MPT3_CMD_RESET))
eb44552b 4892 issue_reset = false;
f92363d1
SR
4893 goto issue_host_reset;
4894 }
4895 if (ioc->base_cmds.status & MPT3_CMD_REPLY_VALID)
4896 memcpy(mpi_reply, ioc->base_cmds.reply,
4897 sizeof(Mpi2SepReply_t));
4898 else
4899 memset(mpi_reply, 0, sizeof(Mpi2SepReply_t));
4900 ioc->base_cmds.status = MPT3_CMD_NOT_USED;
4901 goto out;
4902
4903 issue_host_reset:
4904 if (issue_reset)
98c56ad3 4905 mpt3sas_base_hard_reset_handler(ioc, FORCE_BIG_HAMMER);
f92363d1
SR
4906 ioc->base_cmds.status = MPT3_CMD_NOT_USED;
4907 rc = -EFAULT;
4908 out:
4909 mutex_unlock(&ioc->base_cmds.mutex);
4910 return rc;
4911}
4912
4913/**
4914 * _base_get_port_facts - obtain port facts reply and save in ioc
4915 * @ioc: per adapter object
f92363d1
SR
4916 *
4917 * Returns 0 for success, non-zero for failure.
4918 */
4919static int
98c56ad3 4920_base_get_port_facts(struct MPT3SAS_ADAPTER *ioc, int port)
f92363d1
SR
4921{
4922 Mpi2PortFactsRequest_t mpi_request;
4923 Mpi2PortFactsReply_t mpi_reply;
4924 struct mpt3sas_port_facts *pfacts;
4925 int mpi_reply_sz, mpi_request_sz, r;
4926
4927 dinitprintk(ioc, pr_info(MPT3SAS_FMT "%s\n", ioc->name,
4928 __func__));
4929
4930 mpi_reply_sz = sizeof(Mpi2PortFactsReply_t);
4931 mpi_request_sz = sizeof(Mpi2PortFactsRequest_t);
4932 memset(&mpi_request, 0, mpi_request_sz);
4933 mpi_request.Function = MPI2_FUNCTION_PORT_FACTS;
4934 mpi_request.PortNumber = port;
4935 r = _base_handshake_req_reply_wait(ioc, mpi_request_sz,
98c56ad3 4936 (u32 *)&mpi_request, mpi_reply_sz, (u16 *)&mpi_reply, 5);
f92363d1
SR
4937
4938 if (r != 0) {
4939 pr_err(MPT3SAS_FMT "%s: handshake failed (r=%d)\n",
4940 ioc->name, __func__, r);
4941 return r;
4942 }
4943
4944 pfacts = &ioc->pfacts[port];
4945 memset(pfacts, 0, sizeof(struct mpt3sas_port_facts));
4946 pfacts->PortNumber = mpi_reply.PortNumber;
4947 pfacts->VP_ID = mpi_reply.VP_ID;
4948 pfacts->VF_ID = mpi_reply.VF_ID;
4949 pfacts->MaxPostedCmdBuffers =
4950 le16_to_cpu(mpi_reply.MaxPostedCmdBuffers);
4951
4952 return 0;
4953}
4954
4dc8c808
SR
4955/**
4956 * _base_wait_for_iocstate - Wait until the card is in READY or OPERATIONAL
4957 * @ioc: per adapter object
4958 * @timeout:
4dc8c808
SR
4959 *
4960 * Returns 0 for success, non-zero for failure.
4961 */
4962static int
98c56ad3 4963_base_wait_for_iocstate(struct MPT3SAS_ADAPTER *ioc, int timeout)
4dc8c808
SR
4964{
4965 u32 ioc_state;
4966 int rc;
4967
4968 dinitprintk(ioc, printk(MPT3SAS_FMT "%s\n", ioc->name,
4969 __func__));
4970
4971 if (ioc->pci_error_recovery) {
4972 dfailprintk(ioc, printk(MPT3SAS_FMT
4973 "%s: host in pci error recovery\n", ioc->name, __func__));
4974 return -EFAULT;
4975 }
4976
4977 ioc_state = mpt3sas_base_get_iocstate(ioc, 0);
4978 dhsprintk(ioc, printk(MPT3SAS_FMT "%s: ioc_state(0x%08x)\n",
4979 ioc->name, __func__, ioc_state));
4980
4981 if (((ioc_state & MPI2_IOC_STATE_MASK) == MPI2_IOC_STATE_READY) ||
4982 (ioc_state & MPI2_IOC_STATE_MASK) == MPI2_IOC_STATE_OPERATIONAL)
4983 return 0;
4984
4985 if (ioc_state & MPI2_DOORBELL_USED) {
4986 dhsprintk(ioc, printk(MPT3SAS_FMT
4987 "unexpected doorbell active!\n", ioc->name));
4988 goto issue_diag_reset;
4989 }
4990
4991 if ((ioc_state & MPI2_IOC_STATE_MASK) == MPI2_IOC_STATE_FAULT) {
4992 mpt3sas_base_fault_info(ioc, ioc_state &
4993 MPI2_DOORBELL_DATA_MASK);
4994 goto issue_diag_reset;
4995 }
4996
98c56ad3 4997 ioc_state = _base_wait_on_iocstate(ioc, MPI2_IOC_STATE_READY, timeout);
4dc8c808
SR
4998 if (ioc_state) {
4999 dfailprintk(ioc, printk(MPT3SAS_FMT
5000 "%s: failed going to ready state (ioc_state=0x%x)\n",
5001 ioc->name, __func__, ioc_state));
5002 return -EFAULT;
5003 }
5004
5005 issue_diag_reset:
98c56ad3 5006 rc = _base_diag_reset(ioc);
4dc8c808
SR
5007 return rc;
5008}
5009
f92363d1
SR
5010/**
5011 * _base_get_ioc_facts - obtain ioc facts reply and save in ioc
5012 * @ioc: per adapter object
f92363d1
SR
5013 *
5014 * Returns 0 for success, non-zero for failure.
5015 */
5016static int
98c56ad3 5017_base_get_ioc_facts(struct MPT3SAS_ADAPTER *ioc)
f92363d1
SR
5018{
5019 Mpi2IOCFactsRequest_t mpi_request;
5020 Mpi2IOCFactsReply_t mpi_reply;
5021 struct mpt3sas_facts *facts;
5022 int mpi_reply_sz, mpi_request_sz, r;
5023
5024 dinitprintk(ioc, pr_info(MPT3SAS_FMT "%s\n", ioc->name,
5025 __func__));
5026
98c56ad3 5027 r = _base_wait_for_iocstate(ioc, 10);
4dc8c808
SR
5028 if (r) {
5029 dfailprintk(ioc, printk(MPT3SAS_FMT
5030 "%s: failed getting to correct state\n",
5031 ioc->name, __func__));
5032 return r;
5033 }
f92363d1
SR
5034 mpi_reply_sz = sizeof(Mpi2IOCFactsReply_t);
5035 mpi_request_sz = sizeof(Mpi2IOCFactsRequest_t);
5036 memset(&mpi_request, 0, mpi_request_sz);
5037 mpi_request.Function = MPI2_FUNCTION_IOC_FACTS;
5038 r = _base_handshake_req_reply_wait(ioc, mpi_request_sz,
98c56ad3 5039 (u32 *)&mpi_request, mpi_reply_sz, (u16 *)&mpi_reply, 5);
f92363d1
SR
5040
5041 if (r != 0) {
5042 pr_err(MPT3SAS_FMT "%s: handshake failed (r=%d)\n",
5043 ioc->name, __func__, r);
5044 return r;
5045 }
5046
5047 facts = &ioc->facts;
5048 memset(facts, 0, sizeof(struct mpt3sas_facts));
5049 facts->MsgVersion = le16_to_cpu(mpi_reply.MsgVersion);
5050 facts->HeaderVersion = le16_to_cpu(mpi_reply.HeaderVersion);
5051 facts->VP_ID = mpi_reply.VP_ID;
5052 facts->VF_ID = mpi_reply.VF_ID;
5053 facts->IOCExceptions = le16_to_cpu(mpi_reply.IOCExceptions);
5054 facts->MaxChainDepth = mpi_reply.MaxChainDepth;
5055 facts->WhoInit = mpi_reply.WhoInit;
5056 facts->NumberOfPorts = mpi_reply.NumberOfPorts;
5057 facts->MaxMSIxVectors = mpi_reply.MaxMSIxVectors;
5058 facts->RequestCredit = le16_to_cpu(mpi_reply.RequestCredit);
5059 facts->MaxReplyDescriptorPostQueueDepth =
5060 le16_to_cpu(mpi_reply.MaxReplyDescriptorPostQueueDepth);
5061 facts->ProductID = le16_to_cpu(mpi_reply.ProductID);
5062 facts->IOCCapabilities = le32_to_cpu(mpi_reply.IOCCapabilities);
5063 if ((facts->IOCCapabilities & MPI2_IOCFACTS_CAPABILITY_INTEGRATED_RAID))
5064 ioc->ir_firmware = 1;
9b05c91a 5065 if ((facts->IOCCapabilities &
06f5f976 5066 MPI2_IOCFACTS_CAPABILITY_RDPQ_ARRAY_CAPABLE) && (!reset_devices))
9b05c91a 5067 ioc->rdpq_array_capable = 1;
81c16f83
SPS
5068 if (facts->IOCCapabilities & MPI26_IOCFACTS_CAPABILITY_ATOMIC_REQ)
5069 ioc->atomic_desc_capable = 1;
f92363d1
SR
5070 facts->FWVersion.Word = le32_to_cpu(mpi_reply.FWVersion.Word);
5071 facts->IOCRequestFrameSize =
5072 le16_to_cpu(mpi_reply.IOCRequestFrameSize);
ebb3024e
SS
5073 if (ioc->hba_mpi_version_belonged != MPI2_VERSION) {
5074 facts->IOCMaxChainSegmentSize =
5075 le16_to_cpu(mpi_reply.IOCMaxChainSegmentSize);
5076 }
f92363d1
SR
5077 facts->MaxInitiators = le16_to_cpu(mpi_reply.MaxInitiators);
5078 facts->MaxTargets = le16_to_cpu(mpi_reply.MaxTargets);
5079 ioc->shost->max_id = -1;
5080 facts->MaxSasExpanders = le16_to_cpu(mpi_reply.MaxSasExpanders);
5081 facts->MaxEnclosures = le16_to_cpu(mpi_reply.MaxEnclosures);
5082 facts->ProtocolFlags = le16_to_cpu(mpi_reply.ProtocolFlags);
5083 facts->HighPriorityCredit =
5084 le16_to_cpu(mpi_reply.HighPriorityCredit);
5085 facts->ReplyFrameSize = mpi_reply.ReplyFrameSize;
5086 facts->MaxDevHandle = le16_to_cpu(mpi_reply.MaxDevHandle);
016d5c35
SPS
5087 facts->CurrentHostPageSize = mpi_reply.CurrentHostPageSize;
5088
5089 /*
5090 * Get the Page Size from IOC Facts. If it's 0, default to 4k.
5091 */
5092 ioc->page_size = 1 << facts->CurrentHostPageSize;
5093 if (ioc->page_size == 1) {
5094 pr_info(MPT3SAS_FMT "CurrentHostPageSize is 0: Setting "
5095 "default host page size to 4k\n", ioc->name);
5096 ioc->page_size = 1 << MPT3SAS_HOST_PAGE_SIZE_4K;
5097 }
5098 dinitprintk(ioc, pr_info(MPT3SAS_FMT "CurrentHostPageSize(%d)\n",
5099 ioc->name, facts->CurrentHostPageSize));
f92363d1
SR
5100
5101 dinitprintk(ioc, pr_info(MPT3SAS_FMT
5102 "hba queue depth(%d), max chains per io(%d)\n",
5103 ioc->name, facts->RequestCredit,
5104 facts->MaxChainDepth));
5105 dinitprintk(ioc, pr_info(MPT3SAS_FMT
5106 "request frame size(%d), reply frame size(%d)\n", ioc->name,
5107 facts->IOCRequestFrameSize * 4, facts->ReplyFrameSize * 4));
5108 return 0;
5109}
5110
5111/**
5112 * _base_send_ioc_init - send ioc_init to firmware
5113 * @ioc: per adapter object
f92363d1
SR
5114 *
5115 * Returns 0 for success, non-zero for failure.
5116 */
5117static int
98c56ad3 5118_base_send_ioc_init(struct MPT3SAS_ADAPTER *ioc)
f92363d1
SR
5119{
5120 Mpi2IOCInitRequest_t mpi_request;
5121 Mpi2IOCInitReply_t mpi_reply;
9b05c91a 5122 int i, r = 0;
23409bd4 5123 ktime_t current_time;
f92363d1 5124 u16 ioc_status;
9b05c91a
SR
5125 u32 reply_post_free_array_sz = 0;
5126 Mpi2IOCInitRDPQArrayEntry *reply_post_free_array = NULL;
5127 dma_addr_t reply_post_free_array_dma;
f92363d1
SR
5128
5129 dinitprintk(ioc, pr_info(MPT3SAS_FMT "%s\n", ioc->name,
5130 __func__));
5131
5132 memset(&mpi_request, 0, sizeof(Mpi2IOCInitRequest_t));
5133 mpi_request.Function = MPI2_FUNCTION_IOC_INIT;
5134 mpi_request.WhoInit = MPI2_WHOINIT_HOST_DRIVER;
5135 mpi_request.VF_ID = 0; /* TODO */
5136 mpi_request.VP_ID = 0;
d357e84d 5137 mpi_request.MsgVersion = cpu_to_le16(ioc->hba_mpi_version_belonged);
f92363d1 5138 mpi_request.HeaderVersion = cpu_to_le16(MPI2_HEADER_VERSION);
016d5c35 5139 mpi_request.HostPageSize = MPT3SAS_HOST_PAGE_SIZE_4K;
f92363d1
SR
5140
5141 if (_base_is_controller_msix_enabled(ioc))
5142 mpi_request.HostMSIxVectors = ioc->reply_queue_count;
5143 mpi_request.SystemRequestFrameSize = cpu_to_le16(ioc->request_sz/4);
5144 mpi_request.ReplyDescriptorPostQueueDepth =
5145 cpu_to_le16(ioc->reply_post_queue_depth);
5146 mpi_request.ReplyFreeQueueDepth =
5147 cpu_to_le16(ioc->reply_free_queue_depth);
5148
5149 mpi_request.SenseBufferAddressHigh =
5150 cpu_to_le32((u64)ioc->sense_dma >> 32);
5151 mpi_request.SystemReplyAddressHigh =
5152 cpu_to_le32((u64)ioc->reply_dma >> 32);
5153 mpi_request.SystemRequestFrameBaseAddress =
5154 cpu_to_le64((u64)ioc->request_dma);
5155 mpi_request.ReplyFreeQueueAddress =
5156 cpu_to_le64((u64)ioc->reply_free_dma);
f92363d1 5157
9b05c91a
SR
5158 if (ioc->rdpq_array_enable) {
5159 reply_post_free_array_sz = ioc->reply_queue_count *
5160 sizeof(Mpi2IOCInitRDPQArrayEntry);
5161 reply_post_free_array = pci_alloc_consistent(ioc->pdev,
5162 reply_post_free_array_sz, &reply_post_free_array_dma);
5163 if (!reply_post_free_array) {
5164 pr_err(MPT3SAS_FMT
5165 "reply_post_free_array: pci_alloc_consistent failed\n",
5166 ioc->name);
5167 r = -ENOMEM;
5168 goto out;
5169 }
5170 memset(reply_post_free_array, 0, reply_post_free_array_sz);
5171 for (i = 0; i < ioc->reply_queue_count; i++)
5172 reply_post_free_array[i].RDPQBaseAddress =
5173 cpu_to_le64(
5174 (u64)ioc->reply_post[i].reply_post_free_dma);
5175 mpi_request.MsgFlags = MPI2_IOCINIT_MSGFLAG_RDPQ_ARRAY_MODE;
5176 mpi_request.ReplyDescriptorPostQueueAddress =
5177 cpu_to_le64((u64)reply_post_free_array_dma);
5178 } else {
5179 mpi_request.ReplyDescriptorPostQueueAddress =
5180 cpu_to_le64((u64)ioc->reply_post[0].reply_post_free_dma);
5181 }
f92363d1
SR
5182
5183 /* This time stamp specifies number of milliseconds
5184 * since epoch ~ midnight January 1, 1970.
5185 */
23409bd4
TR
5186 current_time = ktime_get_real();
5187 mpi_request.TimeStamp = cpu_to_le64(ktime_to_ms(current_time));
f92363d1
SR
5188
5189 if (ioc->logging_level & MPT_DEBUG_INIT) {
5190 __le32 *mfp;
5191 int i;
5192
5193 mfp = (__le32 *)&mpi_request;
5194 pr_info("\toffset:data\n");
5195 for (i = 0; i < sizeof(Mpi2IOCInitRequest_t)/4; i++)
5196 pr_info("\t[0x%02x]:%08x\n", i*4,
5197 le32_to_cpu(mfp[i]));
5198 }
5199
5200 r = _base_handshake_req_reply_wait(ioc,
5201 sizeof(Mpi2IOCInitRequest_t), (u32 *)&mpi_request,
98c56ad3 5202 sizeof(Mpi2IOCInitReply_t), (u16 *)&mpi_reply, 10);
f92363d1
SR
5203
5204 if (r != 0) {
5205 pr_err(MPT3SAS_FMT "%s: handshake failed (r=%d)\n",
5206 ioc->name, __func__, r);
9b05c91a 5207 goto out;
f92363d1
SR
5208 }
5209
5210 ioc_status = le16_to_cpu(mpi_reply.IOCStatus) & MPI2_IOCSTATUS_MASK;
5211 if (ioc_status != MPI2_IOCSTATUS_SUCCESS ||
5212 mpi_reply.IOCLogInfo) {
5213 pr_err(MPT3SAS_FMT "%s: failed\n", ioc->name, __func__);
5214 r = -EIO;
5215 }
5216
9b05c91a
SR
5217out:
5218 if (reply_post_free_array)
5219 pci_free_consistent(ioc->pdev, reply_post_free_array_sz,
5220 reply_post_free_array,
5221 reply_post_free_array_dma);
5222 return r;
f92363d1
SR
5223}
5224
5225/**
5226 * mpt3sas_port_enable_done - command completion routine for port enable
5227 * @ioc: per adapter object
5228 * @smid: system request message index
5229 * @msix_index: MSIX table index supplied by the OS
5230 * @reply: reply message frame(lower 32bit addr)
5231 *
5232 * Return 1 meaning mf should be freed from _base_interrupt
5233 * 0 means the mf is freed from this function.
5234 */
5235u8
5236mpt3sas_port_enable_done(struct MPT3SAS_ADAPTER *ioc, u16 smid, u8 msix_index,
5237 u32 reply)
5238{
5239 MPI2DefaultReply_t *mpi_reply;
5240 u16 ioc_status;
5241
5242 if (ioc->port_enable_cmds.status == MPT3_CMD_NOT_USED)
5243 return 1;
5244
5245 mpi_reply = mpt3sas_base_get_reply_virt_addr(ioc, reply);
5246 if (!mpi_reply)
5247 return 1;
5248
5249 if (mpi_reply->Function != MPI2_FUNCTION_PORT_ENABLE)
5250 return 1;
5251
5252 ioc->port_enable_cmds.status &= ~MPT3_CMD_PENDING;
5253 ioc->port_enable_cmds.status |= MPT3_CMD_COMPLETE;
5254 ioc->port_enable_cmds.status |= MPT3_CMD_REPLY_VALID;
5255 memcpy(ioc->port_enable_cmds.reply, mpi_reply, mpi_reply->MsgLength*4);
5256 ioc_status = le16_to_cpu(mpi_reply->IOCStatus) & MPI2_IOCSTATUS_MASK;
5257 if (ioc_status != MPI2_IOCSTATUS_SUCCESS)
5258 ioc->port_enable_failed = 1;
5259
5260 if (ioc->is_driver_loading) {
5261 if (ioc_status == MPI2_IOCSTATUS_SUCCESS) {
5262 mpt3sas_port_enable_complete(ioc);
5263 return 1;
5264 } else {
5265 ioc->start_scan_failed = ioc_status;
5266 ioc->start_scan = 0;
5267 return 1;
5268 }
5269 }
5270 complete(&ioc->port_enable_cmds.done);
5271 return 1;
5272}
5273
5274/**
5275 * _base_send_port_enable - send port_enable(discovery stuff) to firmware
5276 * @ioc: per adapter object
f92363d1
SR
5277 *
5278 * Returns 0 for success, non-zero for failure.
5279 */
5280static int
98c56ad3 5281_base_send_port_enable(struct MPT3SAS_ADAPTER *ioc)
f92363d1
SR
5282{
5283 Mpi2PortEnableRequest_t *mpi_request;
5284 Mpi2PortEnableReply_t *mpi_reply;
f92363d1
SR
5285 int r = 0;
5286 u16 smid;
5287 u16 ioc_status;
5288
5289 pr_info(MPT3SAS_FMT "sending port enable !!\n", ioc->name);
5290
5291 if (ioc->port_enable_cmds.status & MPT3_CMD_PENDING) {
5292 pr_err(MPT3SAS_FMT "%s: internal command already in use\n",
5293 ioc->name, __func__);
5294 return -EAGAIN;
5295 }
5296
5297 smid = mpt3sas_base_get_smid(ioc, ioc->port_enable_cb_idx);
5298 if (!smid) {
5299 pr_err(MPT3SAS_FMT "%s: failed obtaining a smid\n",
5300 ioc->name, __func__);
5301 return -EAGAIN;
5302 }
5303
5304 ioc->port_enable_cmds.status = MPT3_CMD_PENDING;
5305 mpi_request = mpt3sas_base_get_msg_frame(ioc, smid);
5306 ioc->port_enable_cmds.smid = smid;
5307 memset(mpi_request, 0, sizeof(Mpi2PortEnableRequest_t));
5308 mpi_request->Function = MPI2_FUNCTION_PORT_ENABLE;
5309
5310 init_completion(&ioc->port_enable_cmds.done);
81c16f83 5311 ioc->put_smid_default(ioc, smid);
8bbb1cf6 5312 wait_for_completion_timeout(&ioc->port_enable_cmds.done, 300*HZ);
f92363d1
SR
5313 if (!(ioc->port_enable_cmds.status & MPT3_CMD_COMPLETE)) {
5314 pr_err(MPT3SAS_FMT "%s: timeout\n",
5315 ioc->name, __func__);
5316 _debug_dump_mf(mpi_request,
5317 sizeof(Mpi2PortEnableRequest_t)/4);
5318 if (ioc->port_enable_cmds.status & MPT3_CMD_RESET)
5319 r = -EFAULT;
5320 else
5321 r = -ETIME;
5322 goto out;
5323 }
5324
5325 mpi_reply = ioc->port_enable_cmds.reply;
5326 ioc_status = le16_to_cpu(mpi_reply->IOCStatus) & MPI2_IOCSTATUS_MASK;
5327 if (ioc_status != MPI2_IOCSTATUS_SUCCESS) {
5328 pr_err(MPT3SAS_FMT "%s: failed with (ioc_status=0x%08x)\n",
5329 ioc->name, __func__, ioc_status);
5330 r = -EFAULT;
5331 goto out;
5332 }
5333
5334 out:
5335 ioc->port_enable_cmds.status = MPT3_CMD_NOT_USED;
5336 pr_info(MPT3SAS_FMT "port enable: %s\n", ioc->name, ((r == 0) ?
5337 "SUCCESS" : "FAILED"));
5338 return r;
5339}
5340
5341/**
5342 * mpt3sas_port_enable - initiate firmware discovery (don't wait for reply)
5343 * @ioc: per adapter object
5344 *
5345 * Returns 0 for success, non-zero for failure.
5346 */
5347int
5348mpt3sas_port_enable(struct MPT3SAS_ADAPTER *ioc)
5349{
5350 Mpi2PortEnableRequest_t *mpi_request;
5351 u16 smid;
5352
5353 pr_info(MPT3SAS_FMT "sending port enable !!\n", ioc->name);
5354
5355 if (ioc->port_enable_cmds.status & MPT3_CMD_PENDING) {
5356 pr_err(MPT3SAS_FMT "%s: internal command already in use\n",
5357 ioc->name, __func__);
5358 return -EAGAIN;
5359 }
5360
5361 smid = mpt3sas_base_get_smid(ioc, ioc->port_enable_cb_idx);
5362 if (!smid) {
5363 pr_err(MPT3SAS_FMT "%s: failed obtaining a smid\n",
5364 ioc->name, __func__);
5365 return -EAGAIN;
5366 }
5367
5368 ioc->port_enable_cmds.status = MPT3_CMD_PENDING;
5369 mpi_request = mpt3sas_base_get_msg_frame(ioc, smid);
5370 ioc->port_enable_cmds.smid = smid;
5371 memset(mpi_request, 0, sizeof(Mpi2PortEnableRequest_t));
5372 mpi_request->Function = MPI2_FUNCTION_PORT_ENABLE;
5373
81c16f83 5374 ioc->put_smid_default(ioc, smid);
f92363d1
SR
5375 return 0;
5376}
5377
5378/**
5379 * _base_determine_wait_on_discovery - desposition
5380 * @ioc: per adapter object
5381 *
5382 * Decide whether to wait on discovery to complete. Used to either
5383 * locate boot device, or report volumes ahead of physical devices.
5384 *
5385 * Returns 1 for wait, 0 for don't wait
5386 */
5387static int
5388_base_determine_wait_on_discovery(struct MPT3SAS_ADAPTER *ioc)
5389{
5390 /* We wait for discovery to complete if IR firmware is loaded.
5391 * The sas topology events arrive before PD events, so we need time to
5392 * turn on the bit in ioc->pd_handles to indicate PD
5393 * Also, it maybe required to report Volumes ahead of physical
5394 * devices when MPI2_IOCPAGE8_IRFLAGS_LOW_VOLUME_MAPPING is set.
5395 */
5396 if (ioc->ir_firmware)
5397 return 1;
5398
5399 /* if no Bios, then we don't need to wait */
5400 if (!ioc->bios_pg3.BiosVersion)
5401 return 0;
5402
5403 /* Bios is present, then we drop down here.
5404 *
5405 * If there any entries in the Bios Page 2, then we wait
5406 * for discovery to complete.
5407 */
5408
5409 /* Current Boot Device */
5410 if ((ioc->bios_pg2.CurrentBootDeviceForm &
5411 MPI2_BIOSPAGE2_FORM_MASK) ==
5412 MPI2_BIOSPAGE2_FORM_NO_DEVICE_SPECIFIED &&
5413 /* Request Boot Device */
5414 (ioc->bios_pg2.ReqBootDeviceForm &
5415 MPI2_BIOSPAGE2_FORM_MASK) ==
5416 MPI2_BIOSPAGE2_FORM_NO_DEVICE_SPECIFIED &&
5417 /* Alternate Request Boot Device */
5418 (ioc->bios_pg2.ReqAltBootDeviceForm &
5419 MPI2_BIOSPAGE2_FORM_MASK) ==
5420 MPI2_BIOSPAGE2_FORM_NO_DEVICE_SPECIFIED)
5421 return 0;
5422
5423 return 1;
5424}
5425
5426/**
5427 * _base_unmask_events - turn on notification for this event
5428 * @ioc: per adapter object
5429 * @event: firmware event
5430 *
5431 * The mask is stored in ioc->event_masks.
5432 */
5433static void
5434_base_unmask_events(struct MPT3SAS_ADAPTER *ioc, u16 event)
5435{
5436 u32 desired_event;
5437
5438 if (event >= 128)
5439 return;
5440
5441 desired_event = (1 << (event % 32));
5442
5443 if (event < 32)
5444 ioc->event_masks[0] &= ~desired_event;
5445 else if (event < 64)
5446 ioc->event_masks[1] &= ~desired_event;
5447 else if (event < 96)
5448 ioc->event_masks[2] &= ~desired_event;
5449 else if (event < 128)
5450 ioc->event_masks[3] &= ~desired_event;
5451}
5452
5453/**
5454 * _base_event_notification - send event notification
5455 * @ioc: per adapter object
f92363d1
SR
5456 *
5457 * Returns 0 for success, non-zero for failure.
5458 */
5459static int
98c56ad3 5460_base_event_notification(struct MPT3SAS_ADAPTER *ioc)
f92363d1
SR
5461{
5462 Mpi2EventNotificationRequest_t *mpi_request;
f92363d1
SR
5463 u16 smid;
5464 int r = 0;
5465 int i;
5466
5467 dinitprintk(ioc, pr_info(MPT3SAS_FMT "%s\n", ioc->name,
5468 __func__));
5469
5470 if (ioc->base_cmds.status & MPT3_CMD_PENDING) {
5471 pr_err(MPT3SAS_FMT "%s: internal command already in use\n",
5472 ioc->name, __func__);
5473 return -EAGAIN;
5474 }
5475
5476 smid = mpt3sas_base_get_smid(ioc, ioc->base_cb_idx);
5477 if (!smid) {
5478 pr_err(MPT3SAS_FMT "%s: failed obtaining a smid\n",
5479 ioc->name, __func__);
5480 return -EAGAIN;
5481 }
5482 ioc->base_cmds.status = MPT3_CMD_PENDING;
5483 mpi_request = mpt3sas_base_get_msg_frame(ioc, smid);
5484 ioc->base_cmds.smid = smid;
5485 memset(mpi_request, 0, sizeof(Mpi2EventNotificationRequest_t));
5486 mpi_request->Function = MPI2_FUNCTION_EVENT_NOTIFICATION;
5487 mpi_request->VF_ID = 0; /* TODO */
5488 mpi_request->VP_ID = 0;
5489 for (i = 0; i < MPI2_EVENT_NOTIFY_EVENTMASK_WORDS; i++)
5490 mpi_request->EventMasks[i] =
5491 cpu_to_le32(ioc->event_masks[i]);
5492 init_completion(&ioc->base_cmds.done);
81c16f83 5493 ioc->put_smid_default(ioc, smid);
8bbb1cf6 5494 wait_for_completion_timeout(&ioc->base_cmds.done, 30*HZ);
f92363d1
SR
5495 if (!(ioc->base_cmds.status & MPT3_CMD_COMPLETE)) {
5496 pr_err(MPT3SAS_FMT "%s: timeout\n",
5497 ioc->name, __func__);
5498 _debug_dump_mf(mpi_request,
5499 sizeof(Mpi2EventNotificationRequest_t)/4);
5500 if (ioc->base_cmds.status & MPT3_CMD_RESET)
5501 r = -EFAULT;
5502 else
5503 r = -ETIME;
5504 } else
5505 dinitprintk(ioc, pr_info(MPT3SAS_FMT "%s: complete\n",
5506 ioc->name, __func__));
5507 ioc->base_cmds.status = MPT3_CMD_NOT_USED;
5508 return r;
5509}
5510
5511/**
5512 * mpt3sas_base_validate_event_type - validating event types
5513 * @ioc: per adapter object
5514 * @event: firmware event
5515 *
5516 * This will turn on firmware event notification when application
5517 * ask for that event. We don't mask events that are already enabled.
5518 */
5519void
5520mpt3sas_base_validate_event_type(struct MPT3SAS_ADAPTER *ioc, u32 *event_type)
5521{
5522 int i, j;
5523 u32 event_mask, desired_event;
5524 u8 send_update_to_fw;
5525
5526 for (i = 0, send_update_to_fw = 0; i <
5527 MPI2_EVENT_NOTIFY_EVENTMASK_WORDS; i++) {
5528 event_mask = ~event_type[i];
5529 desired_event = 1;
5530 for (j = 0; j < 32; j++) {
5531 if (!(event_mask & desired_event) &&
5532 (ioc->event_masks[i] & desired_event)) {
5533 ioc->event_masks[i] &= ~desired_event;
5534 send_update_to_fw = 1;
5535 }
5536 desired_event = (desired_event << 1);
5537 }
5538 }
5539
5540 if (!send_update_to_fw)
5541 return;
5542
5543 mutex_lock(&ioc->base_cmds.mutex);
98c56ad3 5544 _base_event_notification(ioc);
f92363d1
SR
5545 mutex_unlock(&ioc->base_cmds.mutex);
5546}
5547
5548/**
5549 * _base_diag_reset - the "big hammer" start of day reset
5550 * @ioc: per adapter object
f92363d1
SR
5551 *
5552 * Returns 0 for success, non-zero for failure.
5553 */
5554static int
98c56ad3 5555_base_diag_reset(struct MPT3SAS_ADAPTER *ioc)
f92363d1
SR
5556{
5557 u32 host_diagnostic;
5558 u32 ioc_state;
5559 u32 count;
5560 u32 hcb_size;
5561
5562 pr_info(MPT3SAS_FMT "sending diag reset !!\n", ioc->name);
5563
5564 drsprintk(ioc, pr_info(MPT3SAS_FMT "clear interrupts\n",
5565 ioc->name));
5566
5567 count = 0;
5568 do {
5569 /* Write magic sequence to WriteSequence register
5570 * Loop until in diagnostic mode
5571 */
5572 drsprintk(ioc, pr_info(MPT3SAS_FMT
5573 "write magic sequence\n", ioc->name));
5574 writel(MPI2_WRSEQ_FLUSH_KEY_VALUE, &ioc->chip->WriteSequence);
5575 writel(MPI2_WRSEQ_1ST_KEY_VALUE, &ioc->chip->WriteSequence);
5576 writel(MPI2_WRSEQ_2ND_KEY_VALUE, &ioc->chip->WriteSequence);
5577 writel(MPI2_WRSEQ_3RD_KEY_VALUE, &ioc->chip->WriteSequence);
5578 writel(MPI2_WRSEQ_4TH_KEY_VALUE, &ioc->chip->WriteSequence);
5579 writel(MPI2_WRSEQ_5TH_KEY_VALUE, &ioc->chip->WriteSequence);
5580 writel(MPI2_WRSEQ_6TH_KEY_VALUE, &ioc->chip->WriteSequence);
5581
5582 /* wait 100 msec */
98c56ad3 5583 msleep(100);
f92363d1
SR
5584
5585 if (count++ > 20)
5586 goto out;
5587
5588 host_diagnostic = readl(&ioc->chip->HostDiagnostic);
5589 drsprintk(ioc, pr_info(MPT3SAS_FMT
5590 "wrote magic sequence: count(%d), host_diagnostic(0x%08x)\n",
5591 ioc->name, count, host_diagnostic));
5592
5593 } while ((host_diagnostic & MPI2_DIAG_DIAG_WRITE_ENABLE) == 0);
5594
5595 hcb_size = readl(&ioc->chip->HCBSize);
5596
5597 drsprintk(ioc, pr_info(MPT3SAS_FMT "diag reset: issued\n",
5598 ioc->name));
5599 writel(host_diagnostic | MPI2_DIAG_RESET_ADAPTER,
5600 &ioc->chip->HostDiagnostic);
5601
b453ff84 5602 /*This delay allows the chip PCIe hardware time to finish reset tasks*/
98c56ad3 5603 msleep(MPI2_HARD_RESET_PCIE_FIRST_READ_DELAY_MICRO_SEC/1000);
f92363d1 5604
b453ff84
SR
5605 /* Approximately 300 second max wait */
5606 for (count = 0; count < (300000000 /
5607 MPI2_HARD_RESET_PCIE_SECOND_READ_DELAY_MICRO_SEC); count++) {
f92363d1
SR
5608
5609 host_diagnostic = readl(&ioc->chip->HostDiagnostic);
5610
5611 if (host_diagnostic == 0xFFFFFFFF)
5612 goto out;
5613 if (!(host_diagnostic & MPI2_DIAG_RESET_ADAPTER))
5614 break;
5615
98c56ad3 5616 msleep(MPI2_HARD_RESET_PCIE_SECOND_READ_DELAY_MICRO_SEC / 1000);
f92363d1
SR
5617 }
5618
5619 if (host_diagnostic & MPI2_DIAG_HCB_MODE) {
5620
5621 drsprintk(ioc, pr_info(MPT3SAS_FMT
5622 "restart the adapter assuming the HCB Address points to good F/W\n",
5623 ioc->name));
5624 host_diagnostic &= ~MPI2_DIAG_BOOT_DEVICE_SELECT_MASK;
5625 host_diagnostic |= MPI2_DIAG_BOOT_DEVICE_SELECT_HCDW;
5626 writel(host_diagnostic, &ioc->chip->HostDiagnostic);
5627
5628 drsprintk(ioc, pr_info(MPT3SAS_FMT
5629 "re-enable the HCDW\n", ioc->name));
5630 writel(hcb_size | MPI2_HCB_SIZE_HCB_ENABLE,
5631 &ioc->chip->HCBSize);
5632 }
5633
5634 drsprintk(ioc, pr_info(MPT3SAS_FMT "restart the adapter\n",
5635 ioc->name));
5636 writel(host_diagnostic & ~MPI2_DIAG_HOLD_IOC_RESET,
5637 &ioc->chip->HostDiagnostic);
5638
5639 drsprintk(ioc, pr_info(MPT3SAS_FMT
5640 "disable writes to the diagnostic register\n", ioc->name));
5641 writel(MPI2_WRSEQ_FLUSH_KEY_VALUE, &ioc->chip->WriteSequence);
5642
5643 drsprintk(ioc, pr_info(MPT3SAS_FMT
5644 "Wait for FW to go to the READY state\n", ioc->name));
98c56ad3 5645 ioc_state = _base_wait_on_iocstate(ioc, MPI2_IOC_STATE_READY, 20);
f92363d1
SR
5646 if (ioc_state) {
5647 pr_err(MPT3SAS_FMT
5648 "%s: failed going to ready state (ioc_state=0x%x)\n",
5649 ioc->name, __func__, ioc_state);
5650 goto out;
5651 }
5652
5653 pr_info(MPT3SAS_FMT "diag reset: SUCCESS\n", ioc->name);
5654 return 0;
5655
5656 out:
5657 pr_err(MPT3SAS_FMT "diag reset: FAILED\n", ioc->name);
5658 return -EFAULT;
5659}
5660
5661/**
5662 * _base_make_ioc_ready - put controller in READY state
5663 * @ioc: per adapter object
f92363d1
SR
5664 * @type: FORCE_BIG_HAMMER or SOFT_RESET
5665 *
5666 * Returns 0 for success, non-zero for failure.
5667 */
5668static int
98c56ad3 5669_base_make_ioc_ready(struct MPT3SAS_ADAPTER *ioc, enum reset_type type)
f92363d1
SR
5670{
5671 u32 ioc_state;
5672 int rc;
5673 int count;
5674
5675 dinitprintk(ioc, pr_info(MPT3SAS_FMT "%s\n", ioc->name,
5676 __func__));
5677
5678 if (ioc->pci_error_recovery)
5679 return 0;
5680
5681 ioc_state = mpt3sas_base_get_iocstate(ioc, 0);
5682 dhsprintk(ioc, pr_info(MPT3SAS_FMT "%s: ioc_state(0x%08x)\n",
5683 ioc->name, __func__, ioc_state));
5684
5685 /* if in RESET state, it should move to READY state shortly */
5686 count = 0;
5687 if ((ioc_state & MPI2_IOC_STATE_MASK) == MPI2_IOC_STATE_RESET) {
5688 while ((ioc_state & MPI2_IOC_STATE_MASK) !=
5689 MPI2_IOC_STATE_READY) {
5690 if (count++ == 10) {
5691 pr_err(MPT3SAS_FMT
5692 "%s: failed going to ready state (ioc_state=0x%x)\n",
5693 ioc->name, __func__, ioc_state);
5694 return -EFAULT;
5695 }
98c56ad3 5696 ssleep(1);
f92363d1
SR
5697 ioc_state = mpt3sas_base_get_iocstate(ioc, 0);
5698 }
5699 }
5700
5701 if ((ioc_state & MPI2_IOC_STATE_MASK) == MPI2_IOC_STATE_READY)
5702 return 0;
5703
5704 if (ioc_state & MPI2_DOORBELL_USED) {
5705 dhsprintk(ioc, pr_info(MPT3SAS_FMT
5706 "unexpected doorbell active!\n",
5707 ioc->name));
5708 goto issue_diag_reset;
5709 }
5710
5711 if ((ioc_state & MPI2_IOC_STATE_MASK) == MPI2_IOC_STATE_FAULT) {
5712 mpt3sas_base_fault_info(ioc, ioc_state &
5713 MPI2_DOORBELL_DATA_MASK);
5714 goto issue_diag_reset;
5715 }
5716
5717 if (type == FORCE_BIG_HAMMER)
5718 goto issue_diag_reset;
5719
5720 if ((ioc_state & MPI2_IOC_STATE_MASK) == MPI2_IOC_STATE_OPERATIONAL)
5721 if (!(_base_send_ioc_reset(ioc,
98c56ad3 5722 MPI2_FUNCTION_IOC_MESSAGE_UNIT_RESET, 15))) {
f92363d1
SR
5723 return 0;
5724 }
5725
5726 issue_diag_reset:
98c56ad3 5727 rc = _base_diag_reset(ioc);
f92363d1
SR
5728 return rc;
5729}
5730
5731/**
5732 * _base_make_ioc_operational - put controller in OPERATIONAL state
5733 * @ioc: per adapter object
f92363d1
SR
5734 *
5735 * Returns 0 for success, non-zero for failure.
5736 */
5737static int
98c56ad3 5738_base_make_ioc_operational(struct MPT3SAS_ADAPTER *ioc)
f92363d1 5739{
5ec8a175 5740 int r, i, index;
f92363d1
SR
5741 unsigned long flags;
5742 u32 reply_address;
5743 u16 smid;
5744 struct _tr_list *delayed_tr, *delayed_tr_next;
fd0331b3
SS
5745 struct _sc_list *delayed_sc, *delayed_sc_next;
5746 struct _event_ack_list *delayed_event_ack, *delayed_event_ack_next;
7786ab6a 5747 u8 hide_flag;
f92363d1 5748 struct adapter_reply_queue *reply_q;
5ec8a175 5749 Mpi2ReplyDescriptorsUnion_t *reply_post_free_contig;
f92363d1
SR
5750
5751 dinitprintk(ioc, pr_info(MPT3SAS_FMT "%s\n", ioc->name,
5752 __func__));
5753
5754 /* clean the delayed target reset list */
5755 list_for_each_entry_safe(delayed_tr, delayed_tr_next,
5756 &ioc->delayed_tr_list, list) {
5757 list_del(&delayed_tr->list);
5758 kfree(delayed_tr);
5759 }
5760
5761
5762 list_for_each_entry_safe(delayed_tr, delayed_tr_next,
5763 &ioc->delayed_tr_volume_list, list) {
5764 list_del(&delayed_tr->list);
5765 kfree(delayed_tr);
5766 }
5767
fd0331b3
SS
5768 list_for_each_entry_safe(delayed_sc, delayed_sc_next,
5769 &ioc->delayed_sc_list, list) {
5770 list_del(&delayed_sc->list);
5771 kfree(delayed_sc);
5772 }
5773
5774 list_for_each_entry_safe(delayed_event_ack, delayed_event_ack_next,
5775 &ioc->delayed_event_ack_list, list) {
5776 list_del(&delayed_event_ack->list);
5777 kfree(delayed_event_ack);
5778 }
5779
f92363d1
SR
5780 /* initialize the scsi lookup free list */
5781 spin_lock_irqsave(&ioc->scsi_lookup_lock, flags);
5782 INIT_LIST_HEAD(&ioc->free_list);
5783 smid = 1;
5784 for (i = 0; i < ioc->scsiio_depth; i++, smid++) {
5785 INIT_LIST_HEAD(&ioc->scsi_lookup[i].chain_list);
5786 ioc->scsi_lookup[i].cb_idx = 0xFF;
5787 ioc->scsi_lookup[i].smid = smid;
5788 ioc->scsi_lookup[i].scmd = NULL;
7786ab6a 5789 ioc->scsi_lookup[i].direct_io = 0;
f92363d1
SR
5790 list_add_tail(&ioc->scsi_lookup[i].tracker_list,
5791 &ioc->free_list);
5792 }
5793
5794 /* hi-priority queue */
5795 INIT_LIST_HEAD(&ioc->hpr_free_list);
5796 smid = ioc->hi_priority_smid;
5797 for (i = 0; i < ioc->hi_priority_depth; i++, smid++) {
5798 ioc->hpr_lookup[i].cb_idx = 0xFF;
5799 ioc->hpr_lookup[i].smid = smid;
5800 list_add_tail(&ioc->hpr_lookup[i].tracker_list,
5801 &ioc->hpr_free_list);
5802 }
5803
5804 /* internal queue */
5805 INIT_LIST_HEAD(&ioc->internal_free_list);
5806 smid = ioc->internal_smid;
5807 for (i = 0; i < ioc->internal_depth; i++, smid++) {
5808 ioc->internal_lookup[i].cb_idx = 0xFF;
5809 ioc->internal_lookup[i].smid = smid;
5810 list_add_tail(&ioc->internal_lookup[i].tracker_list,
5811 &ioc->internal_free_list);
5812 }
5813
5814 /* chain pool */
5815 INIT_LIST_HEAD(&ioc->free_chain_list);
5816 for (i = 0; i < ioc->chain_depth; i++)
5817 list_add_tail(&ioc->chain_lookup[i].tracker_list,
5818 &ioc->free_chain_list);
5819
5820 spin_unlock_irqrestore(&ioc->scsi_lookup_lock, flags);
5821
5822 /* initialize Reply Free Queue */
5823 for (i = 0, reply_address = (u32)ioc->reply_dma ;
5824 i < ioc->reply_free_queue_depth ; i++, reply_address +=
5825 ioc->reply_sz)
5826 ioc->reply_free[i] = cpu_to_le32(reply_address);
5827
5828 /* initialize reply queues */
5829 if (ioc->is_driver_loading)
5830 _base_assign_reply_queues(ioc);
5831
5832 /* initialize Reply Post Free Queue */
5ec8a175
CO
5833 index = 0;
5834 reply_post_free_contig = ioc->reply_post[0].reply_post_free;
f92363d1 5835 list_for_each_entry(reply_q, &ioc->reply_queue_list, list) {
5ec8a175
CO
5836 /*
5837 * If RDPQ is enabled, switch to the next allocation.
5838 * Otherwise advance within the contiguous region.
5839 */
5840 if (ioc->rdpq_array_enable) {
5841 reply_q->reply_post_free =
5842 ioc->reply_post[index++].reply_post_free;
5843 } else {
5844 reply_q->reply_post_free = reply_post_free_contig;
5845 reply_post_free_contig += ioc->reply_post_queue_depth;
5846 }
5847
f92363d1 5848 reply_q->reply_post_host_index = 0;
f92363d1
SR
5849 for (i = 0; i < ioc->reply_post_queue_depth; i++)
5850 reply_q->reply_post_free[i].Words =
5851 cpu_to_le64(ULLONG_MAX);
5852 if (!_base_is_controller_msix_enabled(ioc))
5853 goto skip_init_reply_post_free_queue;
f92363d1
SR
5854 }
5855 skip_init_reply_post_free_queue:
5856
98c56ad3 5857 r = _base_send_ioc_init(ioc);
f92363d1
SR
5858 if (r)
5859 return r;
5860
5861 /* initialize reply free host index */
5862 ioc->reply_free_host_index = ioc->reply_free_queue_depth - 1;
5863 writel(ioc->reply_free_host_index, &ioc->chip->ReplyFreeHostIndex);
5864
5865 /* initialize reply post host index */
5866 list_for_each_entry(reply_q, &ioc->reply_queue_list, list) {
0bb337c9 5867 if (ioc->combined_reply_queue)
fb77bb53
SR
5868 writel((reply_q->msix_index & 7)<<
5869 MPI2_RPHI_MSIX_INDEX_SHIFT,
5870 ioc->replyPostRegisterIndex[reply_q->msix_index/8]);
5871 else
5872 writel(reply_q->msix_index <<
5873 MPI2_RPHI_MSIX_INDEX_SHIFT,
5874 &ioc->chip->ReplyPostHostIndex);
5875
f92363d1
SR
5876 if (!_base_is_controller_msix_enabled(ioc))
5877 goto skip_init_reply_post_host_index;
5878 }
5879
5880 skip_init_reply_post_host_index:
5881
5882 _base_unmask_interrupts(ioc);
98c56ad3 5883 r = _base_event_notification(ioc);
f92363d1
SR
5884 if (r)
5885 return r;
5886
98c56ad3 5887 _base_static_config_pages(ioc);
f92363d1
SR
5888
5889 if (ioc->is_driver_loading) {
7786ab6a
SR
5890
5891 if (ioc->is_warpdrive && ioc->manu_pg10.OEMIdentifier
5892 == 0x80) {
5893 hide_flag = (u8) (
5894 le32_to_cpu(ioc->manu_pg10.OEMSpecificFlags0) &
5895 MFG_PAGE10_HIDE_SSDS_MASK);
5896 if (hide_flag != MFG_PAGE10_HIDE_SSDS_MASK)
5897 ioc->mfg_pg10_hide_flag = hide_flag;
5898 }
5899
f92363d1
SR
5900 ioc->wait_for_discovery_to_complete =
5901 _base_determine_wait_on_discovery(ioc);
5902
5903 return r; /* scan_start and scan_finished support */
5904 }
5905
98c56ad3 5906 r = _base_send_port_enable(ioc);
f92363d1
SR
5907 if (r)
5908 return r;
5909
5910 return r;
5911}
5912
5913/**
5914 * mpt3sas_base_free_resources - free resources controller resources
5915 * @ioc: per adapter object
5916 *
5917 * Return nothing.
5918 */
5919void
5920mpt3sas_base_free_resources(struct MPT3SAS_ADAPTER *ioc)
5921{
f92363d1
SR
5922 dexitprintk(ioc, pr_info(MPT3SAS_FMT "%s\n", ioc->name,
5923 __func__));
5924
08c4d550
SR
5925 /* synchronizing freeing resource with pci_access_mutex lock */
5926 mutex_lock(&ioc->pci_access_mutex);
cf9bd21a
JL
5927 if (ioc->chip_phys && ioc->chip) {
5928 _base_mask_interrupts(ioc);
5929 ioc->shost_recovery = 1;
98c56ad3 5930 _base_make_ioc_ready(ioc, SOFT_RESET);
cf9bd21a
JL
5931 ioc->shost_recovery = 0;
5932 }
5933
580d4e31 5934 mpt3sas_base_unmap_resources(ioc);
08c4d550 5935 mutex_unlock(&ioc->pci_access_mutex);
f92363d1
SR
5936 return;
5937}
5938
5939/**
5940 * mpt3sas_base_attach - attach controller instance
5941 * @ioc: per adapter object
5942 *
5943 * Returns 0 for success, non-zero for failure.
5944 */
5945int
5946mpt3sas_base_attach(struct MPT3SAS_ADAPTER *ioc)
5947{
5948 int r, i;
5949 int cpu_id, last_cpu_id = 0;
5950
5951 dinitprintk(ioc, pr_info(MPT3SAS_FMT "%s\n", ioc->name,
5952 __func__));
5953
5954 /* setup cpu_msix_table */
5955 ioc->cpu_count = num_online_cpus();
5956 for_each_online_cpu(cpu_id)
5957 last_cpu_id = cpu_id;
5958 ioc->cpu_msix_table_sz = last_cpu_id + 1;
5959 ioc->cpu_msix_table = kzalloc(ioc->cpu_msix_table_sz, GFP_KERNEL);
5960 ioc->reply_queue_count = 1;
5961 if (!ioc->cpu_msix_table) {
5962 dfailprintk(ioc, pr_info(MPT3SAS_FMT
5963 "allocation for cpu_msix_table failed!!!\n",
5964 ioc->name));
5965 r = -ENOMEM;
5966 goto out_free_resources;
5967 }
5968
7786ab6a
SR
5969 if (ioc->is_warpdrive) {
5970 ioc->reply_post_host_index = kcalloc(ioc->cpu_msix_table_sz,
5971 sizeof(resource_size_t *), GFP_KERNEL);
5972 if (!ioc->reply_post_host_index) {
5973 dfailprintk(ioc, pr_info(MPT3SAS_FMT "allocation "
1d55abc0
HR
5974 "for reply_post_host_index failed!!!\n",
5975 ioc->name));
7786ab6a
SR
5976 r = -ENOMEM;
5977 goto out_free_resources;
5978 }
5979 }
5980
9b05c91a
SR
5981 ioc->rdpq_array_enable_assigned = 0;
5982 ioc->dma_mask = 0;
f92363d1
SR
5983 r = mpt3sas_base_map_resources(ioc);
5984 if (r)
5985 goto out_free_resources;
5986
f92363d1 5987 pci_set_drvdata(ioc->pdev, ioc->shost);
98c56ad3 5988 r = _base_get_ioc_facts(ioc);
f92363d1
SR
5989 if (r)
5990 goto out_free_resources;
5991
471ef9d4
SR
5992 switch (ioc->hba_mpi_version_belonged) {
5993 case MPI2_VERSION:
5994 ioc->build_sg_scmd = &_base_build_sg_scmd;
5995 ioc->build_sg = &_base_build_sg;
5996 ioc->build_zero_len_sge = &_base_build_zero_len_sge;
5997 break;
5998 case MPI25_VERSION:
b130b0d5 5999 case MPI26_VERSION:
471ef9d4
SR
6000 /*
6001 * In SAS3.0,
6002 * SCSI_IO, SMP_PASSTHRU, SATA_PASSTHRU, Target Assist, and
6003 * Target Status - all require the IEEE formated scatter gather
6004 * elements.
6005 */
6006 ioc->build_sg_scmd = &_base_build_sg_scmd_ieee;
6007 ioc->build_sg = &_base_build_sg_ieee;
aff39e61 6008 ioc->build_nvme_prp = &_base_build_nvme_prp;
471ef9d4
SR
6009 ioc->build_zero_len_sge = &_base_build_zero_len_sge_ieee;
6010 ioc->sge_size_ieee = sizeof(Mpi2IeeeSgeSimple64_t);
81c16f83 6011
471ef9d4
SR
6012 break;
6013 }
f92363d1 6014
81c16f83
SPS
6015 if (ioc->atomic_desc_capable) {
6016 ioc->put_smid_default = &_base_put_smid_default_atomic;
6017 ioc->put_smid_scsi_io = &_base_put_smid_scsi_io_atomic;
6018 ioc->put_smid_fast_path = &_base_put_smid_fast_path_atomic;
6019 ioc->put_smid_hi_priority = &_base_put_smid_hi_priority_atomic;
aff39e61 6020 ioc->put_smid_nvme_encap = &_base_put_smid_nvme_encap_atomic;
81c16f83
SPS
6021 } else {
6022 ioc->put_smid_default = &_base_put_smid_default;
6023 ioc->put_smid_scsi_io = &_base_put_smid_scsi_io;
6024 ioc->put_smid_fast_path = &_base_put_smid_fast_path;
6025 ioc->put_smid_hi_priority = &_base_put_smid_hi_priority;
aff39e61 6026 ioc->put_smid_nvme_encap = &_base_put_smid_nvme_encap;
81c16f83
SPS
6027 }
6028
6029
f92363d1
SR
6030 /*
6031 * These function pointers for other requests that don't
6032 * the require IEEE scatter gather elements.
6033 *
6034 * For example Configuration Pages and SAS IOUNIT Control don't.
6035 */
6036 ioc->build_sg_mpi = &_base_build_sg;
6037 ioc->build_zero_len_sge_mpi = &_base_build_zero_len_sge;
6038
98c56ad3 6039 r = _base_make_ioc_ready(ioc, SOFT_RESET);
f92363d1
SR
6040 if (r)
6041 goto out_free_resources;
6042
6043 ioc->pfacts = kcalloc(ioc->facts.NumberOfPorts,
6044 sizeof(struct mpt3sas_port_facts), GFP_KERNEL);
6045 if (!ioc->pfacts) {
6046 r = -ENOMEM;
6047 goto out_free_resources;
6048 }
6049
6050 for (i = 0 ; i < ioc->facts.NumberOfPorts; i++) {
98c56ad3 6051 r = _base_get_port_facts(ioc, i);
f92363d1
SR
6052 if (r)
6053 goto out_free_resources;
6054 }
6055
98c56ad3 6056 r = _base_allocate_memory_pools(ioc);
f92363d1
SR
6057 if (r)
6058 goto out_free_resources;
6059
6060 init_waitqueue_head(&ioc->reset_wq);
6061
6062 /* allocate memory pd handle bitmask list */
6063 ioc->pd_handles_sz = (ioc->facts.MaxDevHandle / 8);
6064 if (ioc->facts.MaxDevHandle % 8)
6065 ioc->pd_handles_sz++;
6066 ioc->pd_handles = kzalloc(ioc->pd_handles_sz,
6067 GFP_KERNEL);
6068 if (!ioc->pd_handles) {
6069 r = -ENOMEM;
6070 goto out_free_resources;
6071 }
6072 ioc->blocking_handles = kzalloc(ioc->pd_handles_sz,
6073 GFP_KERNEL);
6074 if (!ioc->blocking_handles) {
6075 r = -ENOMEM;
6076 goto out_free_resources;
6077 }
6078
c696f7b8
SPS
6079 /* allocate memory for pending OS device add list */
6080 ioc->pend_os_device_add_sz = (ioc->facts.MaxDevHandle / 8);
6081 if (ioc->facts.MaxDevHandle % 8)
6082 ioc->pend_os_device_add_sz++;
6083 ioc->pend_os_device_add = kzalloc(ioc->pend_os_device_add_sz,
6084 GFP_KERNEL);
6085 if (!ioc->pend_os_device_add)
6086 goto out_free_resources;
6087
6088 ioc->device_remove_in_progress_sz = ioc->pend_os_device_add_sz;
6089 ioc->device_remove_in_progress =
6090 kzalloc(ioc->device_remove_in_progress_sz, GFP_KERNEL);
6091 if (!ioc->device_remove_in_progress)
6092 goto out_free_resources;
6093
f92363d1
SR
6094 ioc->fwfault_debug = mpt3sas_fwfault_debug;
6095
6096 /* base internal command bits */
6097 mutex_init(&ioc->base_cmds.mutex);
6098 ioc->base_cmds.reply = kzalloc(ioc->reply_sz, GFP_KERNEL);
6099 ioc->base_cmds.status = MPT3_CMD_NOT_USED;
6100
6101 /* port_enable command bits */
6102 ioc->port_enable_cmds.reply = kzalloc(ioc->reply_sz, GFP_KERNEL);
6103 ioc->port_enable_cmds.status = MPT3_CMD_NOT_USED;
6104
6105 /* transport internal command bits */
6106 ioc->transport_cmds.reply = kzalloc(ioc->reply_sz, GFP_KERNEL);
6107 ioc->transport_cmds.status = MPT3_CMD_NOT_USED;
6108 mutex_init(&ioc->transport_cmds.mutex);
6109
6110 /* scsih internal command bits */
6111 ioc->scsih_cmds.reply = kzalloc(ioc->reply_sz, GFP_KERNEL);
6112 ioc->scsih_cmds.status = MPT3_CMD_NOT_USED;
6113 mutex_init(&ioc->scsih_cmds.mutex);
6114
6115 /* task management internal command bits */
6116 ioc->tm_cmds.reply = kzalloc(ioc->reply_sz, GFP_KERNEL);
6117 ioc->tm_cmds.status = MPT3_CMD_NOT_USED;
6118 mutex_init(&ioc->tm_cmds.mutex);
6119
6120 /* config page internal command bits */
6121 ioc->config_cmds.reply = kzalloc(ioc->reply_sz, GFP_KERNEL);
6122 ioc->config_cmds.status = MPT3_CMD_NOT_USED;
6123 mutex_init(&ioc->config_cmds.mutex);
6124
6125 /* ctl module internal command bits */
6126 ioc->ctl_cmds.reply = kzalloc(ioc->reply_sz, GFP_KERNEL);
6127 ioc->ctl_cmds.sense = kzalloc(SCSI_SENSE_BUFFERSIZE, GFP_KERNEL);
6128 ioc->ctl_cmds.status = MPT3_CMD_NOT_USED;
6129 mutex_init(&ioc->ctl_cmds.mutex);
6130
a5dd7efd
CJ
6131 if (!ioc->base_cmds.reply || !ioc->port_enable_cmds.reply ||
6132 !ioc->transport_cmds.reply || !ioc->scsih_cmds.reply ||
6133 !ioc->tm_cmds.reply || !ioc->config_cmds.reply ||
6134 !ioc->ctl_cmds.reply || !ioc->ctl_cmds.sense) {
f92363d1
SR
6135 r = -ENOMEM;
6136 goto out_free_resources;
6137 }
6138
6139 for (i = 0; i < MPI2_EVENT_NOTIFY_EVENTMASK_WORDS; i++)
6140 ioc->event_masks[i] = -1;
6141
6142 /* here we enable the events we care about */
6143 _base_unmask_events(ioc, MPI2_EVENT_SAS_DISCOVERY);
6144 _base_unmask_events(ioc, MPI2_EVENT_SAS_BROADCAST_PRIMITIVE);
6145 _base_unmask_events(ioc, MPI2_EVENT_SAS_TOPOLOGY_CHANGE_LIST);
6146 _base_unmask_events(ioc, MPI2_EVENT_SAS_DEVICE_STATUS_CHANGE);
6147 _base_unmask_events(ioc, MPI2_EVENT_SAS_ENCL_DEVICE_STATUS_CHANGE);
6148 _base_unmask_events(ioc, MPI2_EVENT_IR_CONFIGURATION_CHANGE_LIST);
6149 _base_unmask_events(ioc, MPI2_EVENT_IR_VOLUME);
6150 _base_unmask_events(ioc, MPI2_EVENT_IR_PHYSICAL_DISK);
6151 _base_unmask_events(ioc, MPI2_EVENT_IR_OPERATION_STATUS);
6152 _base_unmask_events(ioc, MPI2_EVENT_LOG_ENTRY_ADDED);
2d8ce8c9 6153 _base_unmask_events(ioc, MPI2_EVENT_TEMP_THRESHOLD);
b99b1993 6154 _base_unmask_events(ioc, MPI2_EVENT_ACTIVE_CABLE_EXCEPTION);
4318c734
SPS
6155 if (ioc->hba_mpi_version_belonged == MPI26_VERSION) {
6156 if (ioc->is_gen35_ioc) {
6157 _base_unmask_events(ioc,
6158 MPI2_EVENT_PCIE_DEVICE_STATUS_CHANGE);
6159 _base_unmask_events(ioc, MPI2_EVENT_PCIE_ENUMERATION);
6160 _base_unmask_events(ioc,
6161 MPI2_EVENT_PCIE_TOPOLOGY_CHANGE_LIST);
6162 }
6163 }
98c56ad3 6164 r = _base_make_ioc_operational(ioc);
f92363d1
SR
6165 if (r)
6166 goto out_free_resources;
6167
16e179bd 6168 ioc->non_operational_loop = 0;
459325c4 6169 ioc->got_task_abort_from_ioctl = 0;
f92363d1
SR
6170 return 0;
6171
6172 out_free_resources:
6173
6174 ioc->remove_host = 1;
6175
6176 mpt3sas_base_free_resources(ioc);
6177 _base_release_memory_pools(ioc);
6178 pci_set_drvdata(ioc->pdev, NULL);
6179 kfree(ioc->cpu_msix_table);
7786ab6a
SR
6180 if (ioc->is_warpdrive)
6181 kfree(ioc->reply_post_host_index);
f92363d1
SR
6182 kfree(ioc->pd_handles);
6183 kfree(ioc->blocking_handles);
c696f7b8
SPS
6184 kfree(ioc->device_remove_in_progress);
6185 kfree(ioc->pend_os_device_add);
f92363d1
SR
6186 kfree(ioc->tm_cmds.reply);
6187 kfree(ioc->transport_cmds.reply);
6188 kfree(ioc->scsih_cmds.reply);
6189 kfree(ioc->config_cmds.reply);
6190 kfree(ioc->base_cmds.reply);
6191 kfree(ioc->port_enable_cmds.reply);
6192 kfree(ioc->ctl_cmds.reply);
6193 kfree(ioc->ctl_cmds.sense);
6194 kfree(ioc->pfacts);
6195 ioc->ctl_cmds.reply = NULL;
6196 ioc->base_cmds.reply = NULL;
6197 ioc->tm_cmds.reply = NULL;
6198 ioc->scsih_cmds.reply = NULL;
6199 ioc->transport_cmds.reply = NULL;
6200 ioc->config_cmds.reply = NULL;
6201 ioc->pfacts = NULL;
6202 return r;
6203}
6204
6205
6206/**
6207 * mpt3sas_base_detach - remove controller instance
6208 * @ioc: per adapter object
6209 *
6210 * Return nothing.
6211 */
6212void
6213mpt3sas_base_detach(struct MPT3SAS_ADAPTER *ioc)
6214{
6215 dexitprintk(ioc, pr_info(MPT3SAS_FMT "%s\n", ioc->name,
6216 __func__));
6217
6218 mpt3sas_base_stop_watchdog(ioc);
6219 mpt3sas_base_free_resources(ioc);
6220 _base_release_memory_pools(ioc);
6221 pci_set_drvdata(ioc->pdev, NULL);
6222 kfree(ioc->cpu_msix_table);
7786ab6a
SR
6223 if (ioc->is_warpdrive)
6224 kfree(ioc->reply_post_host_index);
f92363d1
SR
6225 kfree(ioc->pd_handles);
6226 kfree(ioc->blocking_handles);
c696f7b8
SPS
6227 kfree(ioc->device_remove_in_progress);
6228 kfree(ioc->pend_os_device_add);
f92363d1
SR
6229 kfree(ioc->pfacts);
6230 kfree(ioc->ctl_cmds.reply);
6231 kfree(ioc->ctl_cmds.sense);
6232 kfree(ioc->base_cmds.reply);
6233 kfree(ioc->port_enable_cmds.reply);
6234 kfree(ioc->tm_cmds.reply);
6235 kfree(ioc->transport_cmds.reply);
6236 kfree(ioc->scsih_cmds.reply);
6237 kfree(ioc->config_cmds.reply);
6238}
6239
6240/**
6241 * _base_reset_handler - reset callback handler (for base)
6242 * @ioc: per adapter object
6243 * @reset_phase: phase
6244 *
6245 * The handler for doing any required cleanup or initialization.
6246 *
6247 * The reset phase can be MPT3_IOC_PRE_RESET, MPT3_IOC_AFTER_RESET,
6248 * MPT3_IOC_DONE_RESET
6249 *
6250 * Return nothing.
6251 */
6252static void
6253_base_reset_handler(struct MPT3SAS_ADAPTER *ioc, int reset_phase)
6254{
6255 mpt3sas_scsih_reset_handler(ioc, reset_phase);
6256 mpt3sas_ctl_reset_handler(ioc, reset_phase);
6257 switch (reset_phase) {
6258 case MPT3_IOC_PRE_RESET:
6259 dtmprintk(ioc, pr_info(MPT3SAS_FMT
6260 "%s: MPT3_IOC_PRE_RESET\n", ioc->name, __func__));
6261 break;
6262 case MPT3_IOC_AFTER_RESET:
6263 dtmprintk(ioc, pr_info(MPT3SAS_FMT
6264 "%s: MPT3_IOC_AFTER_RESET\n", ioc->name, __func__));
6265 if (ioc->transport_cmds.status & MPT3_CMD_PENDING) {
6266 ioc->transport_cmds.status |= MPT3_CMD_RESET;
6267 mpt3sas_base_free_smid(ioc, ioc->transport_cmds.smid);
6268 complete(&ioc->transport_cmds.done);
6269 }
6270 if (ioc->base_cmds.status & MPT3_CMD_PENDING) {
6271 ioc->base_cmds.status |= MPT3_CMD_RESET;
6272 mpt3sas_base_free_smid(ioc, ioc->base_cmds.smid);
6273 complete(&ioc->base_cmds.done);
6274 }
6275 if (ioc->port_enable_cmds.status & MPT3_CMD_PENDING) {
6276 ioc->port_enable_failed = 1;
6277 ioc->port_enable_cmds.status |= MPT3_CMD_RESET;
6278 mpt3sas_base_free_smid(ioc, ioc->port_enable_cmds.smid);
6279 if (ioc->is_driver_loading) {
6280 ioc->start_scan_failed =
6281 MPI2_IOCSTATUS_INTERNAL_ERROR;
6282 ioc->start_scan = 0;
6283 ioc->port_enable_cmds.status =
6284 MPT3_CMD_NOT_USED;
6285 } else
6286 complete(&ioc->port_enable_cmds.done);
6287 }
6288 if (ioc->config_cmds.status & MPT3_CMD_PENDING) {
6289 ioc->config_cmds.status |= MPT3_CMD_RESET;
6290 mpt3sas_base_free_smid(ioc, ioc->config_cmds.smid);
6291 ioc->config_cmds.smid = USHRT_MAX;
6292 complete(&ioc->config_cmds.done);
6293 }
6294 break;
6295 case MPT3_IOC_DONE_RESET:
6296 dtmprintk(ioc, pr_info(MPT3SAS_FMT
6297 "%s: MPT3_IOC_DONE_RESET\n", ioc->name, __func__));
6298 break;
6299 }
6300}
6301
6302/**
6303 * _wait_for_commands_to_complete - reset controller
6304 * @ioc: Pointer to MPT_ADAPTER structure
f92363d1
SR
6305 *
6306 * This function waiting(3s) for all pending commands to complete
6307 * prior to putting controller in reset.
6308 */
6309static void
98c56ad3 6310_wait_for_commands_to_complete(struct MPT3SAS_ADAPTER *ioc)
f92363d1
SR
6311{
6312 u32 ioc_state;
6313 unsigned long flags;
6314 u16 i;
6315
6316 ioc->pending_io_count = 0;
f92363d1
SR
6317
6318 ioc_state = mpt3sas_base_get_iocstate(ioc, 0);
6319 if ((ioc_state & MPI2_IOC_STATE_MASK) != MPI2_IOC_STATE_OPERATIONAL)
6320 return;
6321
6322 /* pending command count */
6323 spin_lock_irqsave(&ioc->scsi_lookup_lock, flags);
6324 for (i = 0; i < ioc->scsiio_depth; i++)
6325 if (ioc->scsi_lookup[i].cb_idx != 0xFF)
6326 ioc->pending_io_count++;
6327 spin_unlock_irqrestore(&ioc->scsi_lookup_lock, flags);
6328
6329 if (!ioc->pending_io_count)
6330 return;
6331
6332 /* wait for pending commands to complete */
6333 wait_event_timeout(ioc->reset_wq, ioc->pending_io_count == 0, 10 * HZ);
6334}
6335
6336/**
6337 * mpt3sas_base_hard_reset_handler - reset controller
6338 * @ioc: Pointer to MPT_ADAPTER structure
f92363d1
SR
6339 * @type: FORCE_BIG_HAMMER or SOFT_RESET
6340 *
6341 * Returns 0 for success, non-zero for failure.
6342 */
6343int
98c56ad3 6344mpt3sas_base_hard_reset_handler(struct MPT3SAS_ADAPTER *ioc,
f92363d1
SR
6345 enum reset_type type)
6346{
6347 int r;
6348 unsigned long flags;
6349 u32 ioc_state;
6350 u8 is_fault = 0, is_trigger = 0;
6351
6352 dtmprintk(ioc, pr_info(MPT3SAS_FMT "%s: enter\n", ioc->name,
6353 __func__));
6354
6355 if (ioc->pci_error_recovery) {
6356 pr_err(MPT3SAS_FMT "%s: pci error recovery reset\n",
6357 ioc->name, __func__);
6358 r = 0;
6359 goto out_unlocked;
6360 }
6361
6362 if (mpt3sas_fwfault_debug)
6363 mpt3sas_halt_firmware(ioc);
6364
f92363d1
SR
6365 /* wait for an active reset in progress to complete */
6366 if (!mutex_trylock(&ioc->reset_in_progress_mutex)) {
6367 do {
6368 ssleep(1);
6369 } while (ioc->shost_recovery == 1);
6370 dtmprintk(ioc, pr_info(MPT3SAS_FMT "%s: exit\n", ioc->name,
6371 __func__));
6372 return ioc->ioc_reset_in_progress_status;
6373 }
6374
6375 spin_lock_irqsave(&ioc->ioc_reset_in_progress_lock, flags);
6376 ioc->shost_recovery = 1;
6377 spin_unlock_irqrestore(&ioc->ioc_reset_in_progress_lock, flags);
6378
6379 if ((ioc->diag_buffer_status[MPI2_DIAG_BUF_TYPE_TRACE] &
6380 MPT3_DIAG_BUFFER_IS_REGISTERED) &&
6381 (!(ioc->diag_buffer_status[MPI2_DIAG_BUF_TYPE_TRACE] &
6382 MPT3_DIAG_BUFFER_IS_RELEASED))) {
6383 is_trigger = 1;
6384 ioc_state = mpt3sas_base_get_iocstate(ioc, 0);
6385 if ((ioc_state & MPI2_IOC_STATE_MASK) == MPI2_IOC_STATE_FAULT)
6386 is_fault = 1;
6387 }
6388 _base_reset_handler(ioc, MPT3_IOC_PRE_RESET);
98c56ad3 6389 _wait_for_commands_to_complete(ioc);
f92363d1 6390 _base_mask_interrupts(ioc);
98c56ad3 6391 r = _base_make_ioc_ready(ioc, type);
f92363d1
SR
6392 if (r)
6393 goto out;
6394 _base_reset_handler(ioc, MPT3_IOC_AFTER_RESET);
6395
6396 /* If this hard reset is called while port enable is active, then
6397 * there is no reason to call make_ioc_operational
6398 */
6399 if (ioc->is_driver_loading && ioc->port_enable_failed) {
6400 ioc->remove_host = 1;
6401 r = -EFAULT;
6402 goto out;
6403 }
98c56ad3 6404 r = _base_get_ioc_facts(ioc);
f92363d1
SR
6405 if (r)
6406 goto out;
9b05c91a
SR
6407
6408 if (ioc->rdpq_array_enable && !ioc->rdpq_array_capable)
6409 panic("%s: Issue occurred with flashing controller firmware."
6410 "Please reboot the system and ensure that the correct"
6411 " firmware version is running\n", ioc->name);
6412
98c56ad3 6413 r = _base_make_ioc_operational(ioc);
f92363d1
SR
6414 if (!r)
6415 _base_reset_handler(ioc, MPT3_IOC_DONE_RESET);
6416
6417 out:
6418 dtmprintk(ioc, pr_info(MPT3SAS_FMT "%s: %s\n",
6419 ioc->name, __func__, ((r == 0) ? "SUCCESS" : "FAILED")));
6420
6421 spin_lock_irqsave(&ioc->ioc_reset_in_progress_lock, flags);
6422 ioc->ioc_reset_in_progress_status = r;
6423 ioc->shost_recovery = 0;
6424 spin_unlock_irqrestore(&ioc->ioc_reset_in_progress_lock, flags);
6425 ioc->ioc_reset_count++;
6426 mutex_unlock(&ioc->reset_in_progress_mutex);
6427
6428 out_unlocked:
6429 if ((r == 0) && is_trigger) {
6430 if (is_fault)
6431 mpt3sas_trigger_master(ioc, MASTER_TRIGGER_FW_FAULT);
6432 else
6433 mpt3sas_trigger_master(ioc,
6434 MASTER_TRIGGER_ADAPTER_RESET);
6435 }
6436 dtmprintk(ioc, pr_info(MPT3SAS_FMT "%s: exit\n", ioc->name,
6437 __func__));
6438 return r;
6439}