blob: d4b32af48782457092c7835d59c320d1b4870db7 [file] [log] [blame]
Sreekanth Reddyf92363d2012-11-30 07:44:21 +05301/*
2 * This is the Fusion MPT base driver providing common API layer interface
3 * for access to MPT (Message Passing Technology) firmware.
4 *
5 * This code is based on drivers/scsi/mpt3sas/mpt3sas_base.c
Sreekanth Reddya4ffce02014-09-12 15:35:29 +05306 * Copyright (C) 2012-2014 LSI Corporation
Sreekanth Reddya03bd152015-01-12 11:39:02 +05307 * Copyright (C) 2013-2014 Avago Technologies
8 * (mailto: MPT-FusionLinux.pdl@avagotech.com)
Sreekanth Reddyf92363d2012-11-30 07:44:21 +05309 *
10 * This program is free software; you can redistribute it and/or
11 * modify it under the terms of the GNU General Public License
12 * as published by the Free Software Foundation; either version 2
13 * of the License, or (at your option) any later version.
14 *
15 * This program is distributed in the hope that it will be useful,
16 * but WITHOUT ANY WARRANTY; without even the implied warranty of
17 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
18 * GNU General Public License for more details.
19 *
20 * NO WARRANTY
21 * THE PROGRAM IS PROVIDED ON AN "AS IS" BASIS, WITHOUT WARRANTIES OR
22 * CONDITIONS OF ANY KIND, EITHER EXPRESS OR IMPLIED INCLUDING, WITHOUT
23 * LIMITATION, ANY WARRANTIES OR CONDITIONS OF TITLE, NON-INFRINGEMENT,
24 * MERCHANTABILITY OR FITNESS FOR A PARTICULAR PURPOSE. Each Recipient is
25 * solely responsible for determining the appropriateness of using and
26 * distributing the Program and assumes all risks associated with its
27 * exercise of rights under this Agreement, including but not limited to
28 * the risks and costs of program errors, damage to or loss of data,
29 * programs or equipment, and unavailability or interruption of operations.
30
31 * DISCLAIMER OF LIABILITY
32 * NEITHER RECIPIENT NOR ANY CONTRIBUTORS SHALL HAVE ANY LIABILITY FOR ANY
33 * DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
34 * DAMAGES (INCLUDING WITHOUT LIMITATION LOST PROFITS), HOWEVER CAUSED AND
35 * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR
36 * TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE
37 * USE OR DISTRIBUTION OF THE PROGRAM OR THE EXERCISE OF ANY RIGHTS GRANTED
38 * HEREUNDER, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGES
39
40 * You should have received a copy of the GNU General Public License
41 * along with this program; if not, write to the Free Software
42 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301,
43 * USA.
44 */
45
Sreekanth Reddyf92363d2012-11-30 07:44:21 +053046#include <linux/kernel.h>
47#include <linux/module.h>
48#include <linux/errno.h>
49#include <linux/init.h>
50#include <linux/slab.h>
51#include <linux/types.h>
52#include <linux/pci.h>
53#include <linux/kdev_t.h>
54#include <linux/blkdev.h>
55#include <linux/delay.h>
56#include <linux/interrupt.h>
57#include <linux/dma-mapping.h>
58#include <linux/io.h>
59#include <linux/time.h>
Tina Ruchandani23409bd2016-04-13 00:01:40 -070060#include <linux/ktime.h>
Sreekanth Reddyf92363d2012-11-30 07:44:21 +053061#include <linux/kthread.h>
Suganath Prabu Subramani016d5c32017-10-31 18:02:28 +053062#include <asm/page.h> /* To get host page size per arch */
Sreekanth Reddyf92363d2012-11-30 07:44:21 +053063#include <linux/aer.h>
64
65
66#include "mpt3sas_base.h"
67
68static MPT_CALLBACK mpt_callbacks[MPT_MAX_CALLBACKS];
69
70
71#define FAULT_POLLING_INTERVAL 1000 /* in milliseconds */
72
73 /* maximum controller queue depth */
74#define MAX_HBA_QUEUE_DEPTH 30000
75#define MAX_CHAIN_DEPTH 100000
76static int max_queue_depth = -1;
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
Suganath Prabu Subramani64038302016-02-08 22:13:39 +053088static 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
Sreekanth Reddyfb77bb52015-06-30 12:24:47 +053092static int max_msix_vectors = -1;
Sreekanth Reddy9c500062013-08-14 18:23:20 +053093module_param(max_msix_vectors, int, 0);
94MODULE_PARM_DESC(max_msix_vectors,
Sreekanth Reddyfb77bb52015-06-30 12:24:47 +053095 " max msix vectors");
Sreekanth Reddyf92363d2012-11-30 07:44:21 +053096
97static int mpt3sas_fwfault_debug;
98MODULE_PARM_DESC(mpt3sas_fwfault_debug,
99 " enable detection of firmware fault and halt firmware - (default=0)");
100
Sreekanth Reddy9b05c912014-09-12 15:35:31 +0530101static int
Calvin Owens98c56ad2016-07-28 21:38:21 -0700102_base_get_ioc_facts(struct MPT3SAS_ADAPTER *ioc);
Sreekanth Reddyf92363d2012-11-30 07:44:21 +0530103
104/**
105 * _scsih_set_fwfault_debug - global setting of ioc->fwfault_debug.
106 *
107 */
108static int
Kees Cooke4dca7b2017-10-17 19:04:42 -0700109_scsih_set_fwfault_debug(const char *val, const struct kernel_param *kp)
Sreekanth Reddyf92363d2012-11-30 07:44:21 +0530110{
111 int ret = param_set_int(val, kp);
112 struct MPT3SAS_ADAPTER *ioc;
113
114 if (ret)
115 return ret;
116
Sreekanth Reddy08c4d552015-11-11 17:30:33 +0530117 /* global ioc spinlock to protect controller list on list operations */
Sreekanth Reddyf92363d2012-11-30 07:44:21 +0530118 pr_info("setting fwfault_debug(%d)\n", mpt3sas_fwfault_debug);
Sreekanth Reddy08c4d552015-11-11 17:30:33 +0530119 spin_lock(&gioc_lock);
Sreekanth Reddyf92363d2012-11-30 07:44:21 +0530120 list_for_each_entry(ioc, &mpt3sas_ioc_list, list)
121 ioc->fwfault_debug = mpt3sas_fwfault_debug;
Sreekanth Reddy08c4d552015-11-11 17:30:33 +0530122 spin_unlock(&gioc_lock);
Sreekanth Reddyf92363d2012-11-30 07:44:21 +0530123 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;
Rafael J. Wysocki64cdb412014-01-10 15:27:56 +0100146 pci_stop_and_remove_bus_device_locked(pdev);
Sreekanth Reddyf92363d2012-11-30 07:44:21 +0530147 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);
Sreekanth Reddy16e179b2015-11-11 17:30:27 +0530169 if (ioc->shost_recovery || ioc->pci_error_recovery)
Sreekanth Reddyf92363d2012-11-30 07:44:21 +0530170 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
Sreekanth Reddy16e179b2015-11-11 17:30:27 +0530178 /* 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
Sreekanth Reddyf92363d2012-11-30 07:44:21 +0530192 /*
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,
Sreekanth Reddyc84b06a2015-11-11 17:30:35 +0530207 "%s_dead_ioc_%d", ioc->driver_name, ioc->id);
Sreekanth Reddyf92363d2012-11-30 07:44:21 +0530208 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
Sreekanth Reddy16e179b2015-11-11 17:30:27 +0530219 ioc->non_operational_loop = 0;
220
Sreekanth Reddyf92363d2012-11-30 07:44:21 +0530221 if ((doorbell & MPI2_IOC_STATE_MASK) != MPI2_IOC_STATE_OPERATIONAL) {
Calvin Owens98c56ad2016-07-28 21:38:21 -0700222 rc = mpt3sas_base_hard_reset_handler(ioc, FORCE_BIG_HAMMER);
Sreekanth Reddyf92363d2012-11-30 07:44:21 +0530223 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,
Sreekanth Reddyc84b06a2015-11-11 17:30:35 +0530262 sizeof(ioc->fault_reset_work_q_name), "poll_%s%d_status",
263 ioc->driver_name, ioc->id);
Sreekanth Reddyf92363d2012-11-30 07:44:21 +0530264 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) {
Reddy, Sreekanth4dc06fd2014-07-14 12:01:35 +0530297 if (!cancel_delayed_work_sync(&ioc->fault_reset_work))
Sreekanth Reddyf92363d2012-11-30 07:44:21 +0530298 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
Sreekanth Reddyf92363d2012-11-30 07:44:21 +0530352/**
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;
Suganath prabu Subramanib130b0d2016-01-28 12:06:58 +0530403 case MPI2_IOCSTATUS_INSUFFICIENT_POWER:
404 desc = "insufficient power";
405 break;
Sreekanth Reddyf92363d2012-11-30 07:44:21 +0530406 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;
Suganath Prabu Subramaniaff39e62017-10-31 18:02:29 +0530560 case MPI2_FUNCTION_NVME_ENCAPSULATED:
561 frame_sz = sizeof(Mpi26NVMeEncapsulatedRequest_t) +
562 ioc->sge_size;
563 func_str = "nvme_encapsulated";
564 break;
Sreekanth Reddyf92363d2012-11-30 07:44:21 +0530565 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:
Sreekanth Reddy7786ab62015-11-11 17:30:28 +0530613 if (!ioc->hide_ir_msg)
614 desc = "IR Operation Status";
Sreekanth Reddyf92363d2012-11-30 07:44:21 +0530615 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)
Colin Ian Kingbbaf61e2017-08-15 14:51:45 +0100624 pr_cont(" discovery_status(0x%08x)",
Sreekanth Reddyf92363d2012-11-30 07:44:21 +0530625 le32_to_cpu(event_data->DiscoveryStatus));
Colin Ian Kingbbaf61e2017-08-15 14:51:45 +0100626 pr_cont("\n");
Sreekanth Reddyf92363d2012-11-30 07:44:21 +0530627 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:
Sreekanth Reddy7786ab62015-11-11 17:30:28 +0530645 if (!ioc->hide_ir_msg)
646 desc = "IR Volume";
Sreekanth Reddyf92363d2012-11-30 07:44:21 +0530647 break;
648 case MPI2_EVENT_IR_PHYSICAL_DISK:
Sreekanth Reddy7786ab62015-11-11 17:30:28 +0530649 if (!ioc->hide_ir_msg)
650 desc = "IR Physical Disk";
Sreekanth Reddyf92363d2012-11-30 07:44:21 +0530651 break;
652 case MPI2_EVENT_IR_CONFIGURATION_CHANGE_LIST:
Sreekanth Reddy7786ab62015-11-11 17:30:28 +0530653 if (!ioc->hide_ir_msg)
654 desc = "IR Configuration Change List";
Sreekanth Reddyf92363d2012-11-30 07:44:21 +0530655 break;
656 case MPI2_EVENT_LOG_ENTRY_ADDED:
Sreekanth Reddy7786ab62015-11-11 17:30:28 +0530657 if (!ioc->hide_ir_msg)
658 desc = "Log Entry Added";
Sreekanth Reddyf92363d2012-11-30 07:44:21 +0530659 break;
Sreekanth Reddy2d8ce8c2015-01-12 11:38:56 +0530660 case MPI2_EVENT_TEMP_THRESHOLD:
661 desc = "Temperature Threshold";
662 break;
Chaitra P Ba470a512016-05-06 14:29:27 +0530663 case MPI2_EVENT_ACTIVE_CABLE_EXCEPTION:
Sreekanth Reddyb99b1992017-10-10 18:41:14 +0530664 desc = "Cable Event";
Chaitra P Ba470a512016-05-06 14:29:27 +0530665 break;
Suganath Prabu Subramani4318c732017-10-31 18:02:32 +0530666 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;
Sreekanth Reddyf92363d2012-11-30 07:44:21 +0530686 }
687
688 if (!desc)
689 return;
690
691 pr_info(MPT3SAS_FMT "%s\n", ioc->name, desc);
692}
Sreekanth Reddyf92363d2012-11-30 07:44:21 +0530693
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:
Sreekanth Reddy7786ab62015-11-11 17:30:28 +0530737 if (!ioc->hide_ir_msg)
738 originator_str = "IR";
739 else
740 originator_str = "WarpDrive";
Sreekanth Reddyf92363d2012-11-30 07:44:21 +0530741 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);
Sreekanth Reddyaf009412015-11-11 17:30:23 +0530775
Sreekanth Reddyf92363d2012-11-30 07:44:21 +0530776 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 }
Sreekanth Reddyaf009412015-11-11 17:30:23 +0530781
Sreekanth Reddyf92363d2012-11-30 07:44:21 +0530782 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)
Suganath prabu Subramanifd0331b2016-01-28 12:07:02 +0530811 return mpt3sas_check_for_pending_internal_cmds(ioc, smid);
Sreekanth Reddyf92363d2012-11-30 07:44:21 +0530812
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;
Suganath prabu Subramanifd0331b2016-01-28 12:07:02 +0530842 struct _event_ack_list *delayed_event_ack;
Sreekanth Reddyf92363d2012-11-30 07:44:21 +0530843
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;
Sreekanth Reddyaf009412015-11-11 17:30:23 +0530849
Sreekanth Reddyf92363d2012-11-30 07:44:21 +0530850 _base_display_event_data(ioc, mpi_reply);
Sreekanth Reddyaf009412015-11-11 17:30:23 +0530851
Sreekanth Reddyf92363d2012-11-30 07:44:21 +0530852 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) {
Suganath prabu Subramanifd0331b2016-01-28 12:07:02 +0530856 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)));
Sreekanth Reddyf92363d2012-11-30 07:44:21 +0530868 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;
Suganath Prabu Subramani81c16f82016-10-26 13:34:40 +0530878 ioc->put_smid_default(ioc, smid);
Sreekanth Reddyf92363d2012-11-30 07:44:21 +0530879
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
Hannes Reinecke12e7c672018-01-04 04:57:05 -0800891struct 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
Sreekanth Reddyf92363d2012-11-30 07:44:21 +0530900/**
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;
Hannes Reineckeb0cd285e2018-01-04 04:57:07 -0800911 u16 ctl_smid = ioc->scsiio_depth - INTERNAL_SCSIIO_CMDS_COUNT + 1;
Hannes Reineckeba4494d2018-01-04 04:57:01 -0800912 u8 cb_idx = 0xFF;
Sreekanth Reddyf92363d2012-11-30 07:44:21 +0530913
914 if (smid < ioc->hi_priority_smid) {
Hannes Reinecke12e7c672018-01-04 04:57:05 -0800915 struct scsiio_tracker *st;
916
Hannes Reineckeb0cd285e2018-01-04 04:57:07 -0800917 if (smid < ctl_smid) {
918 st = mpt3sas_get_st_from_smid(ioc, smid);
919 if (st)
920 cb_idx = st->cb_idx;
921 } else if (smid == ctl_smid)
922 cb_idx = ioc->ctl_cb_idx;
Sreekanth Reddyf92363d2012-11-30 07:44:21 +0530923 } else if (smid < ioc->internal_smid) {
924 i = smid - ioc->hi_priority_smid;
925 cb_idx = ioc->hpr_lookup[i].cb_idx;
926 } else if (smid <= ioc->hba_queue_depth) {
927 i = smid - ioc->internal_smid;
928 cb_idx = ioc->internal_lookup[i].cb_idx;
Hannes Reineckeba4494d2018-01-04 04:57:01 -0800929 }
Sreekanth Reddyf92363d2012-11-30 07:44:21 +0530930 return cb_idx;
931}
932
933/**
934 * _base_mask_interrupts - disable interrupts
935 * @ioc: per adapter object
936 *
937 * Disabling ResetIRQ, Reply and Doorbell Interrupts
938 *
939 * Return nothing.
940 */
941static void
942_base_mask_interrupts(struct MPT3SAS_ADAPTER *ioc)
943{
944 u32 him_register;
945
946 ioc->mask_interrupts = 1;
947 him_register = readl(&ioc->chip->HostInterruptMask);
948 him_register |= MPI2_HIM_DIM + MPI2_HIM_RIM + MPI2_HIM_RESET_IRQ_MASK;
949 writel(him_register, &ioc->chip->HostInterruptMask);
950 readl(&ioc->chip->HostInterruptMask);
951}
952
953/**
954 * _base_unmask_interrupts - enable interrupts
955 * @ioc: per adapter object
956 *
957 * Enabling only Reply Interrupts
958 *
959 * Return nothing.
960 */
961static void
962_base_unmask_interrupts(struct MPT3SAS_ADAPTER *ioc)
963{
964 u32 him_register;
965
966 him_register = readl(&ioc->chip->HostInterruptMask);
967 him_register &= ~MPI2_HIM_RIM;
968 writel(him_register, &ioc->chip->HostInterruptMask);
969 ioc->mask_interrupts = 0;
970}
971
972union reply_descriptor {
973 u64 word;
974 struct {
975 u32 low;
976 u32 high;
977 } u;
978};
979
980/**
981 * _base_interrupt - MPT adapter (IOC) specific interrupt handler.
982 * @irq: irq number (not used)
983 * @bus_id: bus identifier cookie == pointer to MPT_ADAPTER structure
984 * @r: pt_regs pointer (not used)
985 *
986 * Return IRQ_HANDLE if processed, else IRQ_NONE.
987 */
988static irqreturn_t
989_base_interrupt(int irq, void *bus_id)
990{
991 struct adapter_reply_queue *reply_q = bus_id;
992 union reply_descriptor rd;
993 u32 completed_cmds;
994 u8 request_desript_type;
995 u16 smid;
996 u8 cb_idx;
997 u32 reply;
998 u8 msix_index = reply_q->msix_index;
999 struct MPT3SAS_ADAPTER *ioc = reply_q->ioc;
1000 Mpi2ReplyDescriptorsUnion_t *rpf;
1001 u8 rc;
1002
1003 if (ioc->mask_interrupts)
1004 return IRQ_NONE;
1005
1006 if (!atomic_add_unless(&reply_q->busy, 1, 1))
1007 return IRQ_NONE;
1008
1009 rpf = &reply_q->reply_post_free[reply_q->reply_post_host_index];
1010 request_desript_type = rpf->Default.ReplyFlags
1011 & MPI2_RPY_DESCRIPT_FLAGS_TYPE_MASK;
1012 if (request_desript_type == MPI2_RPY_DESCRIPT_FLAGS_UNUSED) {
1013 atomic_dec(&reply_q->busy);
1014 return IRQ_NONE;
1015 }
1016
1017 completed_cmds = 0;
1018 cb_idx = 0xFF;
1019 do {
1020 rd.word = le64_to_cpu(rpf->Words);
1021 if (rd.u.low == UINT_MAX || rd.u.high == UINT_MAX)
1022 goto out;
1023 reply = 0;
1024 smid = le16_to_cpu(rpf->Default.DescriptorTypeDependent1);
1025 if (request_desript_type ==
1026 MPI25_RPY_DESCRIPT_FLAGS_FAST_PATH_SCSI_IO_SUCCESS ||
1027 request_desript_type ==
Suganath Prabu Subramaniaff39e62017-10-31 18:02:29 +05301028 MPI2_RPY_DESCRIPT_FLAGS_SCSI_IO_SUCCESS ||
1029 request_desript_type ==
1030 MPI26_RPY_DESCRIPT_FLAGS_PCIE_ENCAPSULATED_SUCCESS) {
Sreekanth Reddyf92363d2012-11-30 07:44:21 +05301031 cb_idx = _base_get_cb_idx(ioc, smid);
1032 if ((likely(cb_idx < MPT_MAX_CALLBACKS)) &&
1033 (likely(mpt_callbacks[cb_idx] != NULL))) {
1034 rc = mpt_callbacks[cb_idx](ioc, smid,
1035 msix_index, 0);
1036 if (rc)
1037 mpt3sas_base_free_smid(ioc, smid);
1038 }
1039 } else if (request_desript_type ==
1040 MPI2_RPY_DESCRIPT_FLAGS_ADDRESS_REPLY) {
1041 reply = le32_to_cpu(
1042 rpf->AddressReply.ReplyFrameAddress);
1043 if (reply > ioc->reply_dma_max_address ||
1044 reply < ioc->reply_dma_min_address)
1045 reply = 0;
1046 if (smid) {
1047 cb_idx = _base_get_cb_idx(ioc, smid);
1048 if ((likely(cb_idx < MPT_MAX_CALLBACKS)) &&
1049 (likely(mpt_callbacks[cb_idx] != NULL))) {
1050 rc = mpt_callbacks[cb_idx](ioc, smid,
1051 msix_index, reply);
1052 if (reply)
1053 _base_display_reply_info(ioc,
1054 smid, msix_index, reply);
1055 if (rc)
1056 mpt3sas_base_free_smid(ioc,
1057 smid);
1058 }
1059 } else {
1060 _base_async_event(ioc, msix_index, reply);
1061 }
1062
1063 /* reply free queue handling */
1064 if (reply) {
1065 ioc->reply_free_host_index =
1066 (ioc->reply_free_host_index ==
1067 (ioc->reply_free_queue_depth - 1)) ?
1068 0 : ioc->reply_free_host_index + 1;
1069 ioc->reply_free[ioc->reply_free_host_index] =
1070 cpu_to_le32(reply);
Sreekanth Reddyf92363d2012-11-30 07:44:21 +05301071 writel(ioc->reply_free_host_index,
1072 &ioc->chip->ReplyFreeHostIndex);
1073 }
1074 }
1075
1076 rpf->Words = cpu_to_le64(ULLONG_MAX);
1077 reply_q->reply_post_host_index =
1078 (reply_q->reply_post_host_index ==
1079 (ioc->reply_post_queue_depth - 1)) ? 0 :
1080 reply_q->reply_post_host_index + 1;
1081 request_desript_type =
1082 reply_q->reply_post_free[reply_q->reply_post_host_index].
1083 Default.ReplyFlags & MPI2_RPY_DESCRIPT_FLAGS_TYPE_MASK;
1084 completed_cmds++;
Chaitra P B6b4c3352017-01-23 15:26:09 +05301085 /* Update the reply post host index after continuously
1086 * processing the threshold number of Reply Descriptors.
1087 * So that FW can find enough entries to post the Reply
1088 * Descriptors in the reply descriptor post queue.
1089 */
1090 if (completed_cmds > ioc->hba_queue_depth/3) {
1091 if (ioc->combined_reply_queue) {
1092 writel(reply_q->reply_post_host_index |
1093 ((msix_index & 7) <<
1094 MPI2_RPHI_MSIX_INDEX_SHIFT),
1095 ioc->replyPostRegisterIndex[msix_index/8]);
1096 } else {
1097 writel(reply_q->reply_post_host_index |
1098 (msix_index <<
1099 MPI2_RPHI_MSIX_INDEX_SHIFT),
1100 &ioc->chip->ReplyPostHostIndex);
1101 }
1102 completed_cmds = 1;
1103 }
Sreekanth Reddyf92363d2012-11-30 07:44:21 +05301104 if (request_desript_type == MPI2_RPY_DESCRIPT_FLAGS_UNUSED)
1105 goto out;
1106 if (!reply_q->reply_post_host_index)
1107 rpf = reply_q->reply_post_free;
1108 else
1109 rpf++;
1110 } while (1);
1111
1112 out:
1113
1114 if (!completed_cmds) {
1115 atomic_dec(&reply_q->busy);
1116 return IRQ_NONE;
1117 }
1118
Sreekanth Reddy7786ab62015-11-11 17:30:28 +05301119 if (ioc->is_warpdrive) {
1120 writel(reply_q->reply_post_host_index,
1121 ioc->reply_post_host_index[msix_index]);
1122 atomic_dec(&reply_q->busy);
1123 return IRQ_HANDLED;
1124 }
Sreekanth Reddyfb77bb52015-06-30 12:24:47 +05301125
1126 /* Update Reply Post Host Index.
1127 * For those HBA's which support combined reply queue feature
1128 * 1. Get the correct Supplemental Reply Post Host Index Register.
1129 * i.e. (msix_index / 8)th entry from Supplemental Reply Post Host
1130 * Index Register address bank i.e replyPostRegisterIndex[],
1131 * 2. Then update this register with new reply host index value
1132 * in ReplyPostIndex field and the MSIxIndex field with
1133 * msix_index value reduced to a value between 0 and 7,
1134 * using a modulo 8 operation. Since each Supplemental Reply Post
1135 * Host Index Register supports 8 MSI-X vectors.
1136 *
1137 * For other HBA's just update the Reply Post Host Index register with
1138 * new reply host index value in ReplyPostIndex Field and msix_index
1139 * value in MSIxIndex field.
1140 */
Suganath Prabu Subramani0bb337c2016-10-26 13:34:38 +05301141 if (ioc->combined_reply_queue)
Sreekanth Reddyfb77bb52015-06-30 12:24:47 +05301142 writel(reply_q->reply_post_host_index | ((msix_index & 7) <<
1143 MPI2_RPHI_MSIX_INDEX_SHIFT),
1144 ioc->replyPostRegisterIndex[msix_index/8]);
1145 else
1146 writel(reply_q->reply_post_host_index | (msix_index <<
1147 MPI2_RPHI_MSIX_INDEX_SHIFT),
1148 &ioc->chip->ReplyPostHostIndex);
Sreekanth Reddyf92363d2012-11-30 07:44:21 +05301149 atomic_dec(&reply_q->busy);
1150 return IRQ_HANDLED;
1151}
1152
1153/**
1154 * _base_is_controller_msix_enabled - is controller support muli-reply queues
1155 * @ioc: per adapter object
1156 *
1157 */
1158static inline int
1159_base_is_controller_msix_enabled(struct MPT3SAS_ADAPTER *ioc)
1160{
1161 return (ioc->facts.IOCCapabilities &
1162 MPI2_IOCFACTS_CAPABILITY_MSI_X_INDEX) && ioc->msix_enable;
1163}
1164
1165/**
Chaitra P B5f0dfb72016-05-06 14:29:31 +05301166 * mpt3sas_base_sync_reply_irqs - flush pending MSIX interrupts
Sreekanth Reddyf92363d2012-11-30 07:44:21 +05301167 * @ioc: per adapter object
Chaitra P B5f0dfb72016-05-06 14:29:31 +05301168 * Context: non ISR conext
Sreekanth Reddyf92363d2012-11-30 07:44:21 +05301169 *
Chaitra P B5f0dfb72016-05-06 14:29:31 +05301170 * Called when a Task Management request has completed.
Sreekanth Reddyf92363d2012-11-30 07:44:21 +05301171 *
1172 * Return nothing.
1173 */
1174void
Chaitra P B5f0dfb72016-05-06 14:29:31 +05301175mpt3sas_base_sync_reply_irqs(struct MPT3SAS_ADAPTER *ioc)
Sreekanth Reddyf92363d2012-11-30 07:44:21 +05301176{
1177 struct adapter_reply_queue *reply_q;
1178
1179 /* If MSIX capability is turned off
1180 * then multi-queues are not enabled
1181 */
1182 if (!_base_is_controller_msix_enabled(ioc))
1183 return;
1184
1185 list_for_each_entry(reply_q, &ioc->reply_queue_list, list) {
Chaitra P B5f0dfb72016-05-06 14:29:31 +05301186 if (ioc->shost_recovery || ioc->remove_host ||
1187 ioc->pci_error_recovery)
Sreekanth Reddyf92363d2012-11-30 07:44:21 +05301188 return;
1189 /* TMs are on msix_index == 0 */
1190 if (reply_q->msix_index == 0)
1191 continue;
Hannes Reinecke1d55abc2017-02-22 11:31:41 +01001192 synchronize_irq(pci_irq_vector(ioc->pdev, reply_q->msix_index));
Sreekanth Reddyf92363d2012-11-30 07:44:21 +05301193 }
1194}
1195
1196/**
1197 * mpt3sas_base_release_callback_handler - clear interrupt callback handler
1198 * @cb_idx: callback index
1199 *
1200 * Return nothing.
1201 */
1202void
1203mpt3sas_base_release_callback_handler(u8 cb_idx)
1204{
1205 mpt_callbacks[cb_idx] = NULL;
1206}
1207
1208/**
1209 * mpt3sas_base_register_callback_handler - obtain index for the interrupt callback handler
1210 * @cb_func: callback function
1211 *
1212 * Returns cb_func.
1213 */
1214u8
1215mpt3sas_base_register_callback_handler(MPT_CALLBACK cb_func)
1216{
1217 u8 cb_idx;
1218
1219 for (cb_idx = MPT_MAX_CALLBACKS-1; cb_idx; cb_idx--)
1220 if (mpt_callbacks[cb_idx] == NULL)
1221 break;
1222
1223 mpt_callbacks[cb_idx] = cb_func;
1224 return cb_idx;
1225}
1226
1227/**
1228 * mpt3sas_base_initialize_callback_handler - initialize the interrupt callback handler
1229 *
1230 * Return nothing.
1231 */
1232void
1233mpt3sas_base_initialize_callback_handler(void)
1234{
1235 u8 cb_idx;
1236
1237 for (cb_idx = 0; cb_idx < MPT_MAX_CALLBACKS; cb_idx++)
1238 mpt3sas_base_release_callback_handler(cb_idx);
1239}
1240
1241
1242/**
1243 * _base_build_zero_len_sge - build zero length sg entry
1244 * @ioc: per adapter object
1245 * @paddr: virtual address for SGE
1246 *
1247 * Create a zero length scatter gather entry to insure the IOCs hardware has
1248 * something to use if the target device goes brain dead and tries
1249 * to send data even when none is asked for.
1250 *
1251 * Return nothing.
1252 */
1253static void
1254_base_build_zero_len_sge(struct MPT3SAS_ADAPTER *ioc, void *paddr)
1255{
1256 u32 flags_length = (u32)((MPI2_SGE_FLAGS_LAST_ELEMENT |
1257 MPI2_SGE_FLAGS_END_OF_BUFFER | MPI2_SGE_FLAGS_END_OF_LIST |
1258 MPI2_SGE_FLAGS_SIMPLE_ELEMENT) <<
1259 MPI2_SGE_FLAGS_SHIFT);
1260 ioc->base_add_sg_single(paddr, flags_length, -1);
1261}
1262
1263/**
1264 * _base_add_sg_single_32 - Place a simple 32 bit SGE at address pAddr.
1265 * @paddr: virtual address for SGE
1266 * @flags_length: SGE flags and data transfer length
1267 * @dma_addr: Physical address
1268 *
1269 * Return nothing.
1270 */
1271static void
1272_base_add_sg_single_32(void *paddr, u32 flags_length, dma_addr_t dma_addr)
1273{
1274 Mpi2SGESimple32_t *sgel = paddr;
1275
1276 flags_length |= (MPI2_SGE_FLAGS_32_BIT_ADDRESSING |
1277 MPI2_SGE_FLAGS_SYSTEM_ADDRESS) << MPI2_SGE_FLAGS_SHIFT;
1278 sgel->FlagsLength = cpu_to_le32(flags_length);
1279 sgel->Address = cpu_to_le32(dma_addr);
1280}
1281
1282
1283/**
1284 * _base_add_sg_single_64 - Place a simple 64 bit SGE at address pAddr.
1285 * @paddr: virtual address for SGE
1286 * @flags_length: SGE flags and data transfer length
1287 * @dma_addr: Physical address
1288 *
1289 * Return nothing.
1290 */
1291static void
1292_base_add_sg_single_64(void *paddr, u32 flags_length, dma_addr_t dma_addr)
1293{
1294 Mpi2SGESimple64_t *sgel = paddr;
1295
1296 flags_length |= (MPI2_SGE_FLAGS_64_BIT_ADDRESSING |
1297 MPI2_SGE_FLAGS_SYSTEM_ADDRESS) << MPI2_SGE_FLAGS_SHIFT;
1298 sgel->FlagsLength = cpu_to_le32(flags_length);
1299 sgel->Address = cpu_to_le64(dma_addr);
1300}
1301
1302/**
1303 * _base_get_chain_buffer_tracker - obtain chain tracker
1304 * @ioc: per adapter object
1305 * @smid: smid associated to an IO request
1306 *
1307 * Returns chain tracker(from ioc->free_chain_list)
1308 */
1309static struct chain_tracker *
1310_base_get_chain_buffer_tracker(struct MPT3SAS_ADAPTER *ioc, u16 smid)
1311{
1312 struct chain_tracker *chain_req;
Hannes Reinecke12e7c672018-01-04 04:57:05 -08001313 struct scsiio_tracker *st;
Sreekanth Reddyf92363d2012-11-30 07:44:21 +05301314 unsigned long flags;
1315
1316 spin_lock_irqsave(&ioc->scsi_lookup_lock, flags);
1317 if (list_empty(&ioc->free_chain_list)) {
1318 spin_unlock_irqrestore(&ioc->scsi_lookup_lock, flags);
1319 dfailprintk(ioc, pr_warn(MPT3SAS_FMT
1320 "chain buffers not available\n", ioc->name));
1321 return NULL;
1322 }
1323 chain_req = list_entry(ioc->free_chain_list.next,
1324 struct chain_tracker, tracker_list);
1325 list_del_init(&chain_req->tracker_list);
Hannes Reinecke12e7c672018-01-04 04:57:05 -08001326 st = mpt3sas_get_st_from_smid(ioc, smid);
1327 if (st)
1328 list_add_tail(&chain_req->tracker_list, &st->chain_list);
Sreekanth Reddyf92363d2012-11-30 07:44:21 +05301329 spin_unlock_irqrestore(&ioc->scsi_lookup_lock, flags);
1330 return chain_req;
1331}
1332
1333
1334/**
1335 * _base_build_sg - build generic sg
1336 * @ioc: per adapter object
1337 * @psge: virtual address for SGE
1338 * @data_out_dma: physical address for WRITES
1339 * @data_out_sz: data xfer size for WRITES
1340 * @data_in_dma: physical address for READS
1341 * @data_in_sz: data xfer size for READS
1342 *
1343 * Return nothing.
1344 */
1345static void
1346_base_build_sg(struct MPT3SAS_ADAPTER *ioc, void *psge,
1347 dma_addr_t data_out_dma, size_t data_out_sz, dma_addr_t data_in_dma,
1348 size_t data_in_sz)
1349{
1350 u32 sgl_flags;
1351
1352 if (!data_out_sz && !data_in_sz) {
1353 _base_build_zero_len_sge(ioc, psge);
1354 return;
1355 }
1356
1357 if (data_out_sz && data_in_sz) {
1358 /* WRITE sgel first */
1359 sgl_flags = (MPI2_SGE_FLAGS_SIMPLE_ELEMENT |
1360 MPI2_SGE_FLAGS_END_OF_BUFFER | MPI2_SGE_FLAGS_HOST_TO_IOC);
1361 sgl_flags = sgl_flags << MPI2_SGE_FLAGS_SHIFT;
1362 ioc->base_add_sg_single(psge, sgl_flags |
1363 data_out_sz, data_out_dma);
1364
1365 /* incr sgel */
1366 psge += ioc->sge_size;
1367
1368 /* READ sgel last */
1369 sgl_flags = (MPI2_SGE_FLAGS_SIMPLE_ELEMENT |
1370 MPI2_SGE_FLAGS_LAST_ELEMENT | MPI2_SGE_FLAGS_END_OF_BUFFER |
1371 MPI2_SGE_FLAGS_END_OF_LIST);
1372 sgl_flags = sgl_flags << MPI2_SGE_FLAGS_SHIFT;
1373 ioc->base_add_sg_single(psge, sgl_flags |
1374 data_in_sz, data_in_dma);
1375 } else if (data_out_sz) /* WRITE */ {
1376 sgl_flags = (MPI2_SGE_FLAGS_SIMPLE_ELEMENT |
1377 MPI2_SGE_FLAGS_LAST_ELEMENT | MPI2_SGE_FLAGS_END_OF_BUFFER |
1378 MPI2_SGE_FLAGS_END_OF_LIST | MPI2_SGE_FLAGS_HOST_TO_IOC);
1379 sgl_flags = sgl_flags << MPI2_SGE_FLAGS_SHIFT;
1380 ioc->base_add_sg_single(psge, sgl_flags |
1381 data_out_sz, data_out_dma);
1382 } else if (data_in_sz) /* READ */ {
1383 sgl_flags = (MPI2_SGE_FLAGS_SIMPLE_ELEMENT |
1384 MPI2_SGE_FLAGS_LAST_ELEMENT | MPI2_SGE_FLAGS_END_OF_BUFFER |
1385 MPI2_SGE_FLAGS_END_OF_LIST);
1386 sgl_flags = sgl_flags << MPI2_SGE_FLAGS_SHIFT;
1387 ioc->base_add_sg_single(psge, sgl_flags |
1388 data_in_sz, data_in_dma);
1389 }
1390}
1391
Suganath Prabu Subramaniaff39e62017-10-31 18:02:29 +05301392/* IEEE format sgls */
1393
1394/**
1395 * _base_build_nvme_prp - This function is called for NVMe end devices to build
1396 * a native SGL (NVMe PRP). The native SGL is built starting in the first PRP
1397 * entry of the NVMe message (PRP1). If the data buffer is small enough to be
1398 * described entirely using PRP1, then PRP2 is not used. If needed, PRP2 is
1399 * used to describe a larger data buffer. If the data buffer is too large to
1400 * describe using the two PRP entriess inside the NVMe message, then PRP1
1401 * describes the first data memory segment, and PRP2 contains a pointer to a PRP
1402 * list located elsewhere in memory to describe the remaining data memory
1403 * segments. The PRP list will be contiguous.
1404
1405 * The native SGL for NVMe devices is a Physical Region Page (PRP). A PRP
1406 * consists of a list of PRP entries to describe a number of noncontigous
1407 * physical memory segments as a single memory buffer, just as a SGL does. Note
1408 * however, that this function is only used by the IOCTL call, so the memory
1409 * given will be guaranteed to be contiguous. There is no need to translate
1410 * non-contiguous SGL into a PRP in this case. All PRPs will describe
1411 * contiguous space that is one page size each.
1412 *
1413 * Each NVMe message contains two PRP entries. The first (PRP1) either contains
1414 * a PRP list pointer or a PRP element, depending upon the command. PRP2
1415 * contains the second PRP element if the memory being described fits within 2
1416 * PRP entries, or a PRP list pointer if the PRP spans more than two entries.
1417 *
1418 * A PRP list pointer contains the address of a PRP list, structured as a linear
1419 * array of PRP entries. Each PRP entry in this list describes a segment of
1420 * physical memory.
1421 *
1422 * Each 64-bit PRP entry comprises an address and an offset field. The address
1423 * always points at the beginning of a 4KB physical memory page, and the offset
1424 * describes where within that 4KB page the memory segment begins. Only the
1425 * first element in a PRP list may contain a non-zero offest, implying that all
1426 * memory segments following the first begin at the start of a 4KB page.
1427 *
1428 * Each PRP element normally describes 4KB of physical memory, with exceptions
1429 * for the first and last elements in the list. If the memory being described
1430 * by the list begins at a non-zero offset within the first 4KB page, then the
1431 * first PRP element will contain a non-zero offset indicating where the region
1432 * begins within the 4KB page. The last memory segment may end before the end
1433 * of the 4KB segment, depending upon the overall size of the memory being
1434 * described by the PRP list.
1435 *
1436 * Since PRP entries lack any indication of size, the overall data buffer length
1437 * is used to determine where the end of the data memory buffer is located, and
1438 * how many PRP entries are required to describe it.
1439 *
1440 * @ioc: per adapter object
1441 * @smid: system request message index for getting asscociated SGL
1442 * @nvme_encap_request: the NVMe request msg frame pointer
1443 * @data_out_dma: physical address for WRITES
1444 * @data_out_sz: data xfer size for WRITES
1445 * @data_in_dma: physical address for READS
1446 * @data_in_sz: data xfer size for READS
1447 *
1448 * Returns nothing.
1449 */
1450static void
1451_base_build_nvme_prp(struct MPT3SAS_ADAPTER *ioc, u16 smid,
1452 Mpi26NVMeEncapsulatedRequest_t *nvme_encap_request,
1453 dma_addr_t data_out_dma, size_t data_out_sz, dma_addr_t data_in_dma,
1454 size_t data_in_sz)
1455{
1456 int prp_size = NVME_PRP_SIZE;
Arnd Bergmannd8335ae2017-11-06 14:35:16 +01001457 __le64 *prp_entry, *prp1_entry, *prp2_entry;
1458 __le64 *prp_page;
1459 dma_addr_t prp_entry_dma, prp_page_dma, dma_addr;
Suganath Prabu Subramaniaff39e62017-10-31 18:02:29 +05301460 u32 offset, entry_len;
1461 u32 page_mask_result, page_mask;
Suganath Prabu Subramaniaff39e62017-10-31 18:02:29 +05301462 size_t length;
1463
1464 /*
1465 * Not all commands require a data transfer. If no data, just return
1466 * without constructing any PRP.
1467 */
1468 if (!data_in_sz && !data_out_sz)
1469 return;
1470 /*
1471 * Set pointers to PRP1 and PRP2, which are in the NVMe command.
1472 * PRP1 is located at a 24 byte offset from the start of the NVMe
1473 * command. Then set the current PRP entry pointer to PRP1.
1474 */
Suganath Prabu Subramani494f4012017-10-31 18:02:38 +05301475 prp1_entry = (__le64 *)(nvme_encap_request->NVMe_Command +
Suganath Prabu Subramaniaff39e62017-10-31 18:02:29 +05301476 NVME_CMD_PRP1_OFFSET);
Suganath Prabu Subramani494f4012017-10-31 18:02:38 +05301477 prp2_entry = (__le64 *)(nvme_encap_request->NVMe_Command +
Suganath Prabu Subramaniaff39e62017-10-31 18:02:29 +05301478 NVME_CMD_PRP2_OFFSET);
1479 prp_entry = prp1_entry;
1480 /*
1481 * For the PRP entries, use the specially allocated buffer of
1482 * contiguous memory.
1483 */
Suganath Prabu Subramani494f4012017-10-31 18:02:38 +05301484 prp_page = (__le64 *)mpt3sas_base_get_pcie_sgl(ioc, smid);
Arnd Bergmannd8335ae2017-11-06 14:35:16 +01001485 prp_page_dma = mpt3sas_base_get_pcie_sgl_dma(ioc, smid);
Suganath Prabu Subramaniaff39e62017-10-31 18:02:29 +05301486
1487 /*
1488 * Check if we are within 1 entry of a page boundary we don't
1489 * want our first entry to be a PRP List entry.
1490 */
1491 page_mask = ioc->page_size - 1;
1492 page_mask_result = (uintptr_t)((u8 *)prp_page + prp_size) & page_mask;
1493 if (!page_mask_result) {
1494 /* Bump up to next page boundary. */
Suganath Prabu Subramani494f4012017-10-31 18:02:38 +05301495 prp_page = (__le64 *)((u8 *)prp_page + prp_size);
Arnd Bergmannd8335ae2017-11-06 14:35:16 +01001496 prp_page_dma = prp_page_dma + prp_size;
Suganath Prabu Subramaniaff39e62017-10-31 18:02:29 +05301497 }
1498
1499 /*
1500 * Set PRP physical pointer, which initially points to the current PRP
1501 * DMA memory page.
1502 */
Arnd Bergmannd8335ae2017-11-06 14:35:16 +01001503 prp_entry_dma = prp_page_dma;
Suganath Prabu Subramaniaff39e62017-10-31 18:02:29 +05301504
1505 /* Get physical address and length of the data buffer. */
1506 if (data_in_sz) {
Arnd Bergmannd8335ae2017-11-06 14:35:16 +01001507 dma_addr = data_in_dma;
Suganath Prabu Subramaniaff39e62017-10-31 18:02:29 +05301508 length = data_in_sz;
1509 } else {
Arnd Bergmannd8335ae2017-11-06 14:35:16 +01001510 dma_addr = data_out_dma;
Suganath Prabu Subramaniaff39e62017-10-31 18:02:29 +05301511 length = data_out_sz;
1512 }
1513
1514 /* Loop while the length is not zero. */
1515 while (length) {
1516 /*
1517 * Check if we need to put a list pointer here if we are at
1518 * page boundary - prp_size (8 bytes).
1519 */
Arnd Bergmannd8335ae2017-11-06 14:35:16 +01001520 page_mask_result = (prp_entry_dma + prp_size) & page_mask;
Suganath Prabu Subramaniaff39e62017-10-31 18:02:29 +05301521 if (!page_mask_result) {
1522 /*
1523 * This is the last entry in a PRP List, so we need to
1524 * put a PRP list pointer here. What this does is:
1525 * - bump the current memory pointer to the next
1526 * address, which will be the next full page.
1527 * - set the PRP Entry to point to that page. This
1528 * is now the PRP List pointer.
1529 * - bump the PRP Entry pointer the start of the
1530 * next page. Since all of this PRP memory is
1531 * contiguous, no need to get a new page - it's
1532 * just the next address.
1533 */
Arnd Bergmannd8335ae2017-11-06 14:35:16 +01001534 prp_entry_dma++;
1535 *prp_entry = cpu_to_le64(prp_entry_dma);
Suganath Prabu Subramaniaff39e62017-10-31 18:02:29 +05301536 prp_entry++;
1537 }
1538
1539 /* Need to handle if entry will be part of a page. */
Arnd Bergmannd8335ae2017-11-06 14:35:16 +01001540 offset = dma_addr & page_mask;
Suganath Prabu Subramaniaff39e62017-10-31 18:02:29 +05301541 entry_len = ioc->page_size - offset;
1542
1543 if (prp_entry == prp1_entry) {
1544 /*
1545 * Must fill in the first PRP pointer (PRP1) before
1546 * moving on.
1547 */
Arnd Bergmannd8335ae2017-11-06 14:35:16 +01001548 *prp1_entry = cpu_to_le64(dma_addr);
Suganath Prabu Subramaniaff39e62017-10-31 18:02:29 +05301549
1550 /*
1551 * Now point to the second PRP entry within the
1552 * command (PRP2).
1553 */
1554 prp_entry = prp2_entry;
1555 } else if (prp_entry == prp2_entry) {
1556 /*
1557 * Should the PRP2 entry be a PRP List pointer or just
1558 * a regular PRP pointer? If there is more than one
1559 * more page of data, must use a PRP List pointer.
1560 */
1561 if (length > ioc->page_size) {
1562 /*
1563 * PRP2 will contain a PRP List pointer because
1564 * more PRP's are needed with this command. The
1565 * list will start at the beginning of the
1566 * contiguous buffer.
1567 */
Arnd Bergmannd8335ae2017-11-06 14:35:16 +01001568 *prp2_entry = cpu_to_le64(prp_entry_dma);
Suganath Prabu Subramaniaff39e62017-10-31 18:02:29 +05301569
1570 /*
1571 * The next PRP Entry will be the start of the
1572 * first PRP List.
1573 */
1574 prp_entry = prp_page;
1575 } else {
1576 /*
1577 * After this, the PRP Entries are complete.
1578 * This command uses 2 PRP's and no PRP list.
1579 */
Arnd Bergmannd8335ae2017-11-06 14:35:16 +01001580 *prp2_entry = cpu_to_le64(dma_addr);
Suganath Prabu Subramaniaff39e62017-10-31 18:02:29 +05301581 }
1582 } else {
1583 /*
1584 * Put entry in list and bump the addresses.
1585 *
1586 * After PRP1 and PRP2 are filled in, this will fill in
1587 * all remaining PRP entries in a PRP List, one per
1588 * each time through the loop.
1589 */
Arnd Bergmannd8335ae2017-11-06 14:35:16 +01001590 *prp_entry = cpu_to_le64(dma_addr);
Suganath Prabu Subramaniaff39e62017-10-31 18:02:29 +05301591 prp_entry++;
Arnd Bergmannd8335ae2017-11-06 14:35:16 +01001592 prp_entry_dma++;
Suganath Prabu Subramaniaff39e62017-10-31 18:02:29 +05301593 }
1594
1595 /*
1596 * Bump the phys address of the command's data buffer by the
1597 * entry_len.
1598 */
Arnd Bergmannd8335ae2017-11-06 14:35:16 +01001599 dma_addr += entry_len;
Suganath Prabu Subramaniaff39e62017-10-31 18:02:29 +05301600
1601 /* Decrement length accounting for last partial page. */
1602 if (entry_len > length)
1603 length = 0;
1604 else
1605 length -= entry_len;
1606 }
1607}
1608
Suganath Prabu Subramani016d5c32017-10-31 18:02:28 +05301609/**
1610 * base_make_prp_nvme -
1611 * Prepare PRPs(Physical Region Page)- SGLs specific to NVMe drives only
1612 *
1613 * @ioc: per adapter object
1614 * @scmd: SCSI command from the mid-layer
1615 * @mpi_request: mpi request
1616 * @smid: msg Index
1617 * @sge_count: scatter gather element count.
1618 *
1619 * Returns: true: PRPs are built
1620 * false: IEEE SGLs needs to be built
1621 */
Suganath Prabu Subramani494f4012017-10-31 18:02:38 +05301622static void
Suganath Prabu Subramani016d5c32017-10-31 18:02:28 +05301623base_make_prp_nvme(struct MPT3SAS_ADAPTER *ioc,
1624 struct scsi_cmnd *scmd,
1625 Mpi25SCSIIORequest_t *mpi_request,
1626 u16 smid, int sge_count)
1627{
Arnd Bergmannd8335ae2017-11-06 14:35:16 +01001628 int sge_len, num_prp_in_chain = 0;
Suganath Prabu Subramani016d5c32017-10-31 18:02:28 +05301629 Mpi25IeeeSgeChain64_t *main_chain_element, *ptr_first_sgl;
Suganath Prabu Subramani494f4012017-10-31 18:02:38 +05301630 __le64 *curr_buff;
Arnd Bergmannd8335ae2017-11-06 14:35:16 +01001631 dma_addr_t msg_dma, sge_addr, offset;
Suganath Prabu Subramani016d5c32017-10-31 18:02:28 +05301632 u32 page_mask, page_mask_result;
1633 struct scatterlist *sg_scmd;
1634 u32 first_prp_len;
1635 int data_len = scsi_bufflen(scmd);
1636 u32 nvme_pg_size;
1637
1638 nvme_pg_size = max_t(u32, ioc->page_size, NVME_PRP_PAGE_SIZE);
1639 /*
1640 * Nvme has a very convoluted prp format. One prp is required
1641 * for each page or partial page. Driver need to split up OS sg_list
1642 * entries if it is longer than one page or cross a page
1643 * boundary. Driver also have to insert a PRP list pointer entry as
1644 * the last entry in each physical page of the PRP list.
1645 *
1646 * NOTE: The first PRP "entry" is actually placed in the first
1647 * SGL entry in the main message as IEEE 64 format. The 2nd
1648 * entry in the main message is the chain element, and the rest
1649 * of the PRP entries are built in the contiguous pcie buffer.
1650 */
1651 page_mask = nvme_pg_size - 1;
1652
1653 /*
1654 * Native SGL is needed.
1655 * Put a chain element in main message frame that points to the first
1656 * chain buffer.
1657 *
1658 * NOTE: The ChainOffset field must be 0 when using a chain pointer to
1659 * a native SGL.
1660 */
1661
1662 /* Set main message chain element pointer */
1663 main_chain_element = (pMpi25IeeeSgeChain64_t)&mpi_request->SGL;
1664 /*
1665 * For NVMe the chain element needs to be the 2nd SG entry in the main
1666 * message.
1667 */
1668 main_chain_element = (Mpi25IeeeSgeChain64_t *)
1669 ((u8 *)main_chain_element + sizeof(MPI25_IEEE_SGE_CHAIN64));
1670
1671 /*
1672 * For the PRP entries, use the specially allocated buffer of
1673 * contiguous memory. Normal chain buffers can't be used
1674 * because each chain buffer would need to be the size of an OS
1675 * page (4k).
1676 */
1677 curr_buff = mpt3sas_base_get_pcie_sgl(ioc, smid);
Arnd Bergmannd8335ae2017-11-06 14:35:16 +01001678 msg_dma = mpt3sas_base_get_pcie_sgl_dma(ioc, smid);
Suganath Prabu Subramani016d5c32017-10-31 18:02:28 +05301679
Arnd Bergmannd8335ae2017-11-06 14:35:16 +01001680 main_chain_element->Address = cpu_to_le64(msg_dma);
Suganath Prabu Subramani016d5c32017-10-31 18:02:28 +05301681 main_chain_element->NextChainOffset = 0;
1682 main_chain_element->Flags = MPI2_IEEE_SGE_FLAGS_CHAIN_ELEMENT |
1683 MPI2_IEEE_SGE_FLAGS_SYSTEM_ADDR |
1684 MPI26_IEEE_SGE_FLAGS_NSF_NVME_PRP;
1685
1686 /* Build first prp, sge need not to be page aligned*/
1687 ptr_first_sgl = (pMpi25IeeeSgeChain64_t)&mpi_request->SGL;
1688 sg_scmd = scsi_sglist(scmd);
1689 sge_addr = sg_dma_address(sg_scmd);
1690 sge_len = sg_dma_len(sg_scmd);
1691
Arnd Bergmannd8335ae2017-11-06 14:35:16 +01001692 offset = sge_addr & page_mask;
Suganath Prabu Subramani016d5c32017-10-31 18:02:28 +05301693 first_prp_len = nvme_pg_size - offset;
1694
1695 ptr_first_sgl->Address = cpu_to_le64(sge_addr);
1696 ptr_first_sgl->Length = cpu_to_le32(first_prp_len);
1697
1698 data_len -= first_prp_len;
1699
1700 if (sge_len > first_prp_len) {
1701 sge_addr += first_prp_len;
1702 sge_len -= first_prp_len;
1703 } else if (data_len && (sge_len == first_prp_len)) {
1704 sg_scmd = sg_next(sg_scmd);
1705 sge_addr = sg_dma_address(sg_scmd);
1706 sge_len = sg_dma_len(sg_scmd);
1707 }
1708
1709 for (;;) {
Arnd Bergmannd8335ae2017-11-06 14:35:16 +01001710 offset = sge_addr & page_mask;
Suganath Prabu Subramani016d5c32017-10-31 18:02:28 +05301711
1712 /* Put PRP pointer due to page boundary*/
1713 page_mask_result = (uintptr_t)(curr_buff + 1) & page_mask;
1714 if (unlikely(!page_mask_result)) {
1715 scmd_printk(KERN_NOTICE,
1716 scmd, "page boundary curr_buff: 0x%p\n",
1717 curr_buff);
Arnd Bergmannd8335ae2017-11-06 14:35:16 +01001718 msg_dma += 8;
1719 *curr_buff = cpu_to_le64(msg_dma);
Suganath Prabu Subramani016d5c32017-10-31 18:02:28 +05301720 curr_buff++;
1721 num_prp_in_chain++;
1722 }
1723
1724 *curr_buff = cpu_to_le64(sge_addr);
1725 curr_buff++;
Arnd Bergmannd8335ae2017-11-06 14:35:16 +01001726 msg_dma += 8;
Suganath Prabu Subramani016d5c32017-10-31 18:02:28 +05301727 num_prp_in_chain++;
1728
1729 sge_addr += nvme_pg_size;
1730 sge_len -= nvme_pg_size;
1731 data_len -= nvme_pg_size;
1732
1733 if (data_len <= 0)
1734 break;
1735
1736 if (sge_len > 0)
1737 continue;
1738
1739 sg_scmd = sg_next(sg_scmd);
1740 sge_addr = sg_dma_address(sg_scmd);
1741 sge_len = sg_dma_len(sg_scmd);
1742 }
1743
1744 main_chain_element->Length =
1745 cpu_to_le32(num_prp_in_chain * sizeof(u64));
1746 return;
1747}
1748
1749static bool
1750base_is_prp_possible(struct MPT3SAS_ADAPTER *ioc,
1751 struct _pcie_device *pcie_device, struct scsi_cmnd *scmd, int sge_count)
1752{
1753 u32 data_length = 0;
1754 struct scatterlist *sg_scmd;
1755 bool build_prp = true;
1756
Suganath Prabu Subramani494f4012017-10-31 18:02:38 +05301757 data_length = scsi_bufflen(scmd);
Suganath Prabu Subramani016d5c32017-10-31 18:02:28 +05301758 sg_scmd = scsi_sglist(scmd);
1759
1760 /* If Datalenth is <= 16K and number of SGE’s entries are <= 2
1761 * we built IEEE SGL
1762 */
1763 if ((data_length <= NVME_PRP_PAGE_SIZE*4) && (sge_count <= 2))
1764 build_prp = false;
1765
1766 return build_prp;
1767}
1768
1769/**
1770 * _base_check_pcie_native_sgl - This function is called for PCIe end devices to
1771 * determine if the driver needs to build a native SGL. If so, that native
1772 * SGL is built in the special contiguous buffers allocated especially for
1773 * PCIe SGL creation. If the driver will not build a native SGL, return
1774 * TRUE and a normal IEEE SGL will be built. Currently this routine
1775 * supports NVMe.
1776 * @ioc: per adapter object
1777 * @mpi_request: mf request pointer
1778 * @smid: system request message index
1779 * @scmd: scsi command
1780 * @pcie_device: points to the PCIe device's info
1781 *
1782 * Returns 0 if native SGL was built, 1 if no SGL was built
1783 */
1784static int
1785_base_check_pcie_native_sgl(struct MPT3SAS_ADAPTER *ioc,
1786 Mpi25SCSIIORequest_t *mpi_request, u16 smid, struct scsi_cmnd *scmd,
1787 struct _pcie_device *pcie_device)
1788{
1789 struct scatterlist *sg_scmd;
1790 int sges_left;
1791
1792 /* Get the SG list pointer and info. */
1793 sg_scmd = scsi_sglist(scmd);
1794 sges_left = scsi_dma_map(scmd);
1795 if (sges_left < 0) {
1796 sdev_printk(KERN_ERR, scmd->device,
1797 "scsi_dma_map failed: request for %d bytes!\n",
1798 scsi_bufflen(scmd));
1799 return 1;
1800 }
1801
1802 /* Check if we need to build a native SG list. */
1803 if (base_is_prp_possible(ioc, pcie_device,
1804 scmd, sges_left) == 0) {
1805 /* We built a native SG list, just return. */
1806 goto out;
1807 }
1808
1809 /*
1810 * Build native NVMe PRP.
1811 */
1812 base_make_prp_nvme(ioc, scmd, mpi_request,
1813 smid, sges_left);
1814
1815 return 0;
1816out:
1817 scsi_dma_unmap(scmd);
1818 return 1;
1819}
Sreekanth Reddyf92363d2012-11-30 07:44:21 +05301820
1821/**
1822 * _base_add_sg_single_ieee - add sg element for IEEE format
1823 * @paddr: virtual address for SGE
1824 * @flags: SGE flags
1825 * @chain_offset: number of 128 byte elements from start of segment
1826 * @length: data transfer length
1827 * @dma_addr: Physical address
1828 *
1829 * Return nothing.
1830 */
1831static void
1832_base_add_sg_single_ieee(void *paddr, u8 flags, u8 chain_offset, u32 length,
1833 dma_addr_t dma_addr)
1834{
1835 Mpi25IeeeSgeChain64_t *sgel = paddr;
1836
1837 sgel->Flags = flags;
1838 sgel->NextChainOffset = chain_offset;
1839 sgel->Length = cpu_to_le32(length);
1840 sgel->Address = cpu_to_le64(dma_addr);
1841}
1842
1843/**
1844 * _base_build_zero_len_sge_ieee - build zero length sg entry for IEEE format
1845 * @ioc: per adapter object
1846 * @paddr: virtual address for SGE
1847 *
1848 * Create a zero length scatter gather entry to insure the IOCs hardware has
1849 * something to use if the target device goes brain dead and tries
1850 * to send data even when none is asked for.
1851 *
1852 * Return nothing.
1853 */
1854static void
1855_base_build_zero_len_sge_ieee(struct MPT3SAS_ADAPTER *ioc, void *paddr)
1856{
1857 u8 sgl_flags = (MPI2_IEEE_SGE_FLAGS_SIMPLE_ELEMENT |
1858 MPI2_IEEE_SGE_FLAGS_SYSTEM_ADDR |
1859 MPI25_IEEE_SGE_FLAGS_END_OF_LIST);
Suganath prabu Subramanib130b0d2016-01-28 12:06:58 +05301860
Sreekanth Reddyf92363d2012-11-30 07:44:21 +05301861 _base_add_sg_single_ieee(paddr, sgl_flags, 0, 0, -1);
1862}
1863
1864/**
Sreekanth Reddy471ef9d2015-11-11 17:30:24 +05301865 * _base_build_sg_scmd - main sg creation routine
Suganath Prabu Subramani016d5c32017-10-31 18:02:28 +05301866 * pcie_device is unused here!
Sreekanth Reddy471ef9d2015-11-11 17:30:24 +05301867 * @ioc: per adapter object
1868 * @scmd: scsi command
1869 * @smid: system request message index
Suganath Prabu Subramani016d5c32017-10-31 18:02:28 +05301870 * @unused: unused pcie_device pointer
Sreekanth Reddy471ef9d2015-11-11 17:30:24 +05301871 * Context: none.
1872 *
1873 * The main routine that builds scatter gather table from a given
1874 * scsi request sent via the .queuecommand main handler.
1875 *
1876 * Returns 0 success, anything else error
1877 */
1878static int
1879_base_build_sg_scmd(struct MPT3SAS_ADAPTER *ioc,
Suganath Prabu Subramani016d5c32017-10-31 18:02:28 +05301880 struct scsi_cmnd *scmd, u16 smid, struct _pcie_device *unused)
Sreekanth Reddy471ef9d2015-11-11 17:30:24 +05301881{
1882 Mpi2SCSIIORequest_t *mpi_request;
1883 dma_addr_t chain_dma;
1884 struct scatterlist *sg_scmd;
1885 void *sg_local, *chain;
1886 u32 chain_offset;
1887 u32 chain_length;
1888 u32 chain_flags;
1889 int sges_left;
1890 u32 sges_in_segment;
1891 u32 sgl_flags;
1892 u32 sgl_flags_last_element;
1893 u32 sgl_flags_end_buffer;
1894 struct chain_tracker *chain_req;
1895
1896 mpi_request = mpt3sas_base_get_msg_frame(ioc, smid);
1897
1898 /* init scatter gather flags */
1899 sgl_flags = MPI2_SGE_FLAGS_SIMPLE_ELEMENT;
1900 if (scmd->sc_data_direction == DMA_TO_DEVICE)
1901 sgl_flags |= MPI2_SGE_FLAGS_HOST_TO_IOC;
1902 sgl_flags_last_element = (sgl_flags | MPI2_SGE_FLAGS_LAST_ELEMENT)
1903 << MPI2_SGE_FLAGS_SHIFT;
1904 sgl_flags_end_buffer = (sgl_flags | MPI2_SGE_FLAGS_LAST_ELEMENT |
1905 MPI2_SGE_FLAGS_END_OF_BUFFER | MPI2_SGE_FLAGS_END_OF_LIST)
1906 << MPI2_SGE_FLAGS_SHIFT;
1907 sgl_flags = sgl_flags << MPI2_SGE_FLAGS_SHIFT;
1908
1909 sg_scmd = scsi_sglist(scmd);
1910 sges_left = scsi_dma_map(scmd);
1911 if (sges_left < 0) {
1912 sdev_printk(KERN_ERR, scmd->device,
1913 "pci_map_sg failed: request for %d bytes!\n",
1914 scsi_bufflen(scmd));
1915 return -ENOMEM;
1916 }
1917
1918 sg_local = &mpi_request->SGL;
1919 sges_in_segment = ioc->max_sges_in_main_message;
1920 if (sges_left <= sges_in_segment)
1921 goto fill_in_last_segment;
1922
1923 mpi_request->ChainOffset = (offsetof(Mpi2SCSIIORequest_t, SGL) +
1924 (sges_in_segment * ioc->sge_size))/4;
1925
1926 /* fill in main message segment when there is a chain following */
1927 while (sges_in_segment) {
1928 if (sges_in_segment == 1)
1929 ioc->base_add_sg_single(sg_local,
1930 sgl_flags_last_element | sg_dma_len(sg_scmd),
1931 sg_dma_address(sg_scmd));
1932 else
1933 ioc->base_add_sg_single(sg_local, sgl_flags |
1934 sg_dma_len(sg_scmd), sg_dma_address(sg_scmd));
1935 sg_scmd = sg_next(sg_scmd);
1936 sg_local += ioc->sge_size;
1937 sges_left--;
1938 sges_in_segment--;
1939 }
1940
1941 /* initializing the chain flags and pointers */
1942 chain_flags = MPI2_SGE_FLAGS_CHAIN_ELEMENT << MPI2_SGE_FLAGS_SHIFT;
1943 chain_req = _base_get_chain_buffer_tracker(ioc, smid);
1944 if (!chain_req)
1945 return -1;
1946 chain = chain_req->chain_buffer;
1947 chain_dma = chain_req->chain_buffer_dma;
1948 do {
1949 sges_in_segment = (sges_left <=
1950 ioc->max_sges_in_chain_message) ? sges_left :
1951 ioc->max_sges_in_chain_message;
1952 chain_offset = (sges_left == sges_in_segment) ?
1953 0 : (sges_in_segment * ioc->sge_size)/4;
1954 chain_length = sges_in_segment * ioc->sge_size;
1955 if (chain_offset) {
1956 chain_offset = chain_offset <<
1957 MPI2_SGE_CHAIN_OFFSET_SHIFT;
1958 chain_length += ioc->sge_size;
1959 }
1960 ioc->base_add_sg_single(sg_local, chain_flags | chain_offset |
1961 chain_length, chain_dma);
1962 sg_local = chain;
1963 if (!chain_offset)
1964 goto fill_in_last_segment;
1965
1966 /* fill in chain segments */
1967 while (sges_in_segment) {
1968 if (sges_in_segment == 1)
1969 ioc->base_add_sg_single(sg_local,
1970 sgl_flags_last_element |
1971 sg_dma_len(sg_scmd),
1972 sg_dma_address(sg_scmd));
1973 else
1974 ioc->base_add_sg_single(sg_local, sgl_flags |
1975 sg_dma_len(sg_scmd),
1976 sg_dma_address(sg_scmd));
1977 sg_scmd = sg_next(sg_scmd);
1978 sg_local += ioc->sge_size;
1979 sges_left--;
1980 sges_in_segment--;
1981 }
1982
1983 chain_req = _base_get_chain_buffer_tracker(ioc, smid);
1984 if (!chain_req)
1985 return -1;
1986 chain = chain_req->chain_buffer;
1987 chain_dma = chain_req->chain_buffer_dma;
1988 } while (1);
1989
1990
1991 fill_in_last_segment:
1992
1993 /* fill the last segment */
1994 while (sges_left) {
1995 if (sges_left == 1)
1996 ioc->base_add_sg_single(sg_local, sgl_flags_end_buffer |
1997 sg_dma_len(sg_scmd), sg_dma_address(sg_scmd));
1998 else
1999 ioc->base_add_sg_single(sg_local, sgl_flags |
2000 sg_dma_len(sg_scmd), sg_dma_address(sg_scmd));
2001 sg_scmd = sg_next(sg_scmd);
2002 sg_local += ioc->sge_size;
2003 sges_left--;
2004 }
2005
2006 return 0;
2007}
2008
2009/**
Sreekanth Reddyf92363d2012-11-30 07:44:21 +05302010 * _base_build_sg_scmd_ieee - main sg creation routine for IEEE format
2011 * @ioc: per adapter object
2012 * @scmd: scsi command
2013 * @smid: system request message index
Suganath Prabu Subramani016d5c32017-10-31 18:02:28 +05302014 * @pcie_device: Pointer to pcie_device. If set, the pcie native sgl will be
2015 * constructed on need.
Sreekanth Reddyf92363d2012-11-30 07:44:21 +05302016 * Context: none.
2017 *
2018 * The main routine that builds scatter gather table from a given
2019 * scsi request sent via the .queuecommand main handler.
2020 *
2021 * Returns 0 success, anything else error
2022 */
2023static int
2024_base_build_sg_scmd_ieee(struct MPT3SAS_ADAPTER *ioc,
Suganath Prabu Subramani016d5c32017-10-31 18:02:28 +05302025 struct scsi_cmnd *scmd, u16 smid, struct _pcie_device *pcie_device)
Sreekanth Reddyf92363d2012-11-30 07:44:21 +05302026{
Suganath Prabu Subramani016d5c32017-10-31 18:02:28 +05302027 Mpi25SCSIIORequest_t *mpi_request;
Sreekanth Reddyf92363d2012-11-30 07:44:21 +05302028 dma_addr_t chain_dma;
2029 struct scatterlist *sg_scmd;
2030 void *sg_local, *chain;
2031 u32 chain_offset;
2032 u32 chain_length;
Sreekanth Reddyf92363d2012-11-30 07:44:21 +05302033 int sges_left;
2034 u32 sges_in_segment;
2035 u8 simple_sgl_flags;
2036 u8 simple_sgl_flags_last;
2037 u8 chain_sgl_flags;
2038 struct chain_tracker *chain_req;
2039
2040 mpi_request = mpt3sas_base_get_msg_frame(ioc, smid);
2041
2042 /* init scatter gather flags */
2043 simple_sgl_flags = MPI2_IEEE_SGE_FLAGS_SIMPLE_ELEMENT |
2044 MPI2_IEEE_SGE_FLAGS_SYSTEM_ADDR;
2045 simple_sgl_flags_last = simple_sgl_flags |
2046 MPI25_IEEE_SGE_FLAGS_END_OF_LIST;
2047 chain_sgl_flags = MPI2_IEEE_SGE_FLAGS_CHAIN_ELEMENT |
2048 MPI2_IEEE_SGE_FLAGS_SYSTEM_ADDR;
2049
Suganath Prabu Subramani016d5c32017-10-31 18:02:28 +05302050 /* Check if we need to build a native SG list. */
2051 if ((pcie_device) && (_base_check_pcie_native_sgl(ioc, mpi_request,
2052 smid, scmd, pcie_device) == 0)) {
2053 /* We built a native SG list, just return. */
2054 return 0;
2055 }
2056
Sreekanth Reddyf92363d2012-11-30 07:44:21 +05302057 sg_scmd = scsi_sglist(scmd);
2058 sges_left = scsi_dma_map(scmd);
Sreekanth Reddy62f5c742015-06-30 12:25:01 +05302059 if (sges_left < 0) {
Sreekanth Reddyf92363d2012-11-30 07:44:21 +05302060 sdev_printk(KERN_ERR, scmd->device,
2061 "pci_map_sg failed: request for %d bytes!\n",
2062 scsi_bufflen(scmd));
2063 return -ENOMEM;
2064 }
2065
2066 sg_local = &mpi_request->SGL;
2067 sges_in_segment = (ioc->request_sz -
Suganath Prabu Subramani016d5c32017-10-31 18:02:28 +05302068 offsetof(Mpi25SCSIIORequest_t, SGL))/ioc->sge_size_ieee;
Sreekanth Reddyf92363d2012-11-30 07:44:21 +05302069 if (sges_left <= sges_in_segment)
2070 goto fill_in_last_segment;
2071
2072 mpi_request->ChainOffset = (sges_in_segment - 1 /* chain element */) +
Suganath Prabu Subramani016d5c32017-10-31 18:02:28 +05302073 (offsetof(Mpi25SCSIIORequest_t, SGL)/ioc->sge_size_ieee);
Sreekanth Reddyf92363d2012-11-30 07:44:21 +05302074
2075 /* fill in main message segment when there is a chain following */
2076 while (sges_in_segment > 1) {
2077 _base_add_sg_single_ieee(sg_local, simple_sgl_flags, 0,
2078 sg_dma_len(sg_scmd), sg_dma_address(sg_scmd));
2079 sg_scmd = sg_next(sg_scmd);
2080 sg_local += ioc->sge_size_ieee;
2081 sges_left--;
2082 sges_in_segment--;
2083 }
2084
Wei Yongjun25ef16d2012-12-12 02:26:51 +05302085 /* initializing the pointers */
Sreekanth Reddyf92363d2012-11-30 07:44:21 +05302086 chain_req = _base_get_chain_buffer_tracker(ioc, smid);
2087 if (!chain_req)
2088 return -1;
2089 chain = chain_req->chain_buffer;
2090 chain_dma = chain_req->chain_buffer_dma;
2091 do {
2092 sges_in_segment = (sges_left <=
2093 ioc->max_sges_in_chain_message) ? sges_left :
2094 ioc->max_sges_in_chain_message;
2095 chain_offset = (sges_left == sges_in_segment) ?
2096 0 : sges_in_segment;
2097 chain_length = sges_in_segment * ioc->sge_size_ieee;
2098 if (chain_offset)
2099 chain_length += ioc->sge_size_ieee;
2100 _base_add_sg_single_ieee(sg_local, chain_sgl_flags,
2101 chain_offset, chain_length, chain_dma);
2102
2103 sg_local = chain;
2104 if (!chain_offset)
2105 goto fill_in_last_segment;
2106
2107 /* fill in chain segments */
2108 while (sges_in_segment) {
2109 _base_add_sg_single_ieee(sg_local, simple_sgl_flags, 0,
2110 sg_dma_len(sg_scmd), sg_dma_address(sg_scmd));
2111 sg_scmd = sg_next(sg_scmd);
2112 sg_local += ioc->sge_size_ieee;
2113 sges_left--;
2114 sges_in_segment--;
2115 }
2116
2117 chain_req = _base_get_chain_buffer_tracker(ioc, smid);
2118 if (!chain_req)
2119 return -1;
2120 chain = chain_req->chain_buffer;
2121 chain_dma = chain_req->chain_buffer_dma;
2122 } while (1);
2123
2124
2125 fill_in_last_segment:
2126
2127 /* fill the last segment */
Sreekanth Reddy62f5c742015-06-30 12:25:01 +05302128 while (sges_left > 0) {
Sreekanth Reddyf92363d2012-11-30 07:44:21 +05302129 if (sges_left == 1)
2130 _base_add_sg_single_ieee(sg_local,
2131 simple_sgl_flags_last, 0, sg_dma_len(sg_scmd),
2132 sg_dma_address(sg_scmd));
2133 else
2134 _base_add_sg_single_ieee(sg_local, simple_sgl_flags, 0,
2135 sg_dma_len(sg_scmd), sg_dma_address(sg_scmd));
2136 sg_scmd = sg_next(sg_scmd);
2137 sg_local += ioc->sge_size_ieee;
2138 sges_left--;
2139 }
2140
2141 return 0;
2142}
2143
2144/**
2145 * _base_build_sg_ieee - build generic sg for IEEE format
2146 * @ioc: per adapter object
2147 * @psge: virtual address for SGE
2148 * @data_out_dma: physical address for WRITES
2149 * @data_out_sz: data xfer size for WRITES
2150 * @data_in_dma: physical address for READS
2151 * @data_in_sz: data xfer size for READS
2152 *
2153 * Return nothing.
2154 */
2155static void
2156_base_build_sg_ieee(struct MPT3SAS_ADAPTER *ioc, void *psge,
2157 dma_addr_t data_out_dma, size_t data_out_sz, dma_addr_t data_in_dma,
2158 size_t data_in_sz)
2159{
2160 u8 sgl_flags;
2161
2162 if (!data_out_sz && !data_in_sz) {
2163 _base_build_zero_len_sge_ieee(ioc, psge);
2164 return;
2165 }
2166
2167 if (data_out_sz && data_in_sz) {
2168 /* WRITE sgel first */
2169 sgl_flags = MPI2_IEEE_SGE_FLAGS_SIMPLE_ELEMENT |
2170 MPI2_IEEE_SGE_FLAGS_SYSTEM_ADDR;
2171 _base_add_sg_single_ieee(psge, sgl_flags, 0, data_out_sz,
2172 data_out_dma);
2173
2174 /* incr sgel */
2175 psge += ioc->sge_size_ieee;
2176
2177 /* READ sgel last */
2178 sgl_flags |= MPI25_IEEE_SGE_FLAGS_END_OF_LIST;
2179 _base_add_sg_single_ieee(psge, sgl_flags, 0, data_in_sz,
2180 data_in_dma);
2181 } else if (data_out_sz) /* WRITE */ {
2182 sgl_flags = MPI2_IEEE_SGE_FLAGS_SIMPLE_ELEMENT |
2183 MPI25_IEEE_SGE_FLAGS_END_OF_LIST |
2184 MPI2_IEEE_SGE_FLAGS_SYSTEM_ADDR;
2185 _base_add_sg_single_ieee(psge, sgl_flags, 0, data_out_sz,
2186 data_out_dma);
2187 } else if (data_in_sz) /* READ */ {
2188 sgl_flags = MPI2_IEEE_SGE_FLAGS_SIMPLE_ELEMENT |
2189 MPI25_IEEE_SGE_FLAGS_END_OF_LIST |
2190 MPI2_IEEE_SGE_FLAGS_SYSTEM_ADDR;
2191 _base_add_sg_single_ieee(psge, sgl_flags, 0, data_in_sz,
2192 data_in_dma);
2193 }
2194}
2195
2196#define convert_to_kb(x) ((x) << (PAGE_SHIFT - 10))
2197
2198/**
2199 * _base_config_dma_addressing - set dma addressing
2200 * @ioc: per adapter object
2201 * @pdev: PCI device struct
2202 *
2203 * Returns 0 for success, non-zero for failure.
2204 */
2205static int
2206_base_config_dma_addressing(struct MPT3SAS_ADAPTER *ioc, struct pci_dev *pdev)
2207{
2208 struct sysinfo s;
Sreekanth Reddy9b05c912014-09-12 15:35:31 +05302209 u64 consistent_dma_mask;
2210
2211 if (ioc->dma_mask)
2212 consistent_dma_mask = DMA_BIT_MASK(64);
2213 else
2214 consistent_dma_mask = DMA_BIT_MASK(32);
Sreekanth Reddyf92363d2012-11-30 07:44:21 +05302215
2216 if (sizeof(dma_addr_t) > 4) {
2217 const uint64_t required_mask =
2218 dma_get_required_mask(&pdev->dev);
2219 if ((required_mask > DMA_BIT_MASK(32)) &&
2220 !pci_set_dma_mask(pdev, DMA_BIT_MASK(64)) &&
Sreekanth Reddy9b05c912014-09-12 15:35:31 +05302221 !pci_set_consistent_dma_mask(pdev, consistent_dma_mask)) {
Sreekanth Reddyf92363d2012-11-30 07:44:21 +05302222 ioc->base_add_sg_single = &_base_add_sg_single_64;
2223 ioc->sge_size = sizeof(Mpi2SGESimple64_t);
Sreekanth Reddy9b05c912014-09-12 15:35:31 +05302224 ioc->dma_mask = 64;
Sreekanth Reddyf92363d2012-11-30 07:44:21 +05302225 goto out;
2226 }
2227 }
2228
2229 if (!pci_set_dma_mask(pdev, DMA_BIT_MASK(32))
2230 && !pci_set_consistent_dma_mask(pdev, DMA_BIT_MASK(32))) {
2231 ioc->base_add_sg_single = &_base_add_sg_single_32;
2232 ioc->sge_size = sizeof(Mpi2SGESimple32_t);
Sreekanth Reddy9b05c912014-09-12 15:35:31 +05302233 ioc->dma_mask = 32;
Sreekanth Reddyf92363d2012-11-30 07:44:21 +05302234 } else
2235 return -ENODEV;
2236
2237 out:
2238 si_meminfo(&s);
2239 pr_info(MPT3SAS_FMT
Sreekanth Reddy9b05c912014-09-12 15:35:31 +05302240 "%d BIT PCI BUS DMA ADDRESSING SUPPORTED, total mem (%ld kB)\n",
2241 ioc->name, ioc->dma_mask, convert_to_kb(s.totalram));
Sreekanth Reddyf92363d2012-11-30 07:44:21 +05302242
2243 return 0;
2244}
2245
Sreekanth Reddy9b05c912014-09-12 15:35:31 +05302246static int
2247_base_change_consistent_dma_mask(struct MPT3SAS_ADAPTER *ioc,
2248 struct pci_dev *pdev)
2249{
2250 if (pci_set_consistent_dma_mask(pdev, DMA_BIT_MASK(64))) {
2251 if (pci_set_consistent_dma_mask(pdev, DMA_BIT_MASK(32)))
2252 return -ENODEV;
2253 }
2254 return 0;
2255}
2256
Sreekanth Reddyf92363d2012-11-30 07:44:21 +05302257/**
2258 * _base_check_enable_msix - checks MSIX capabable.
2259 * @ioc: per adapter object
2260 *
2261 * Check to see if card is capable of MSIX, and set number
2262 * of available msix vectors
2263 */
2264static int
2265_base_check_enable_msix(struct MPT3SAS_ADAPTER *ioc)
2266{
2267 int base;
2268 u16 message_control;
2269
Sreekanth Reddy42081172015-11-11 17:30:26 +05302270 /* Check whether controller SAS2008 B0 controller,
2271 * if it is SAS2008 B0 controller use IO-APIC instead of MSIX
2272 */
2273 if (ioc->pdev->device == MPI2_MFGPAGE_DEVID_SAS2008 &&
2274 ioc->pdev->revision == SAS2_PCI_DEVICE_B0_REVISION) {
2275 return -EINVAL;
2276 }
2277
Sreekanth Reddyf92363d2012-11-30 07:44:21 +05302278 base = pci_find_capability(ioc->pdev, PCI_CAP_ID_MSIX);
2279 if (!base) {
2280 dfailprintk(ioc, pr_info(MPT3SAS_FMT "msix not supported\n",
2281 ioc->name));
2282 return -EINVAL;
2283 }
2284
2285 /* get msix vector count */
Sreekanth Reddy42081172015-11-11 17:30:26 +05302286 /* NUMA_IO not supported for older controllers */
2287 if (ioc->pdev->device == MPI2_MFGPAGE_DEVID_SAS2004 ||
2288 ioc->pdev->device == MPI2_MFGPAGE_DEVID_SAS2008 ||
2289 ioc->pdev->device == MPI2_MFGPAGE_DEVID_SAS2108_1 ||
2290 ioc->pdev->device == MPI2_MFGPAGE_DEVID_SAS2108_2 ||
2291 ioc->pdev->device == MPI2_MFGPAGE_DEVID_SAS2108_3 ||
2292 ioc->pdev->device == MPI2_MFGPAGE_DEVID_SAS2116_1 ||
2293 ioc->pdev->device == MPI2_MFGPAGE_DEVID_SAS2116_2)
2294 ioc->msix_vector_count = 1;
2295 else {
2296 pci_read_config_word(ioc->pdev, base + 2, &message_control);
2297 ioc->msix_vector_count = (message_control & 0x3FF) + 1;
2298 }
Sreekanth Reddyf92363d2012-11-30 07:44:21 +05302299 dinitprintk(ioc, pr_info(MPT3SAS_FMT
2300 "msix is supported, vector_count(%d)\n",
2301 ioc->name, ioc->msix_vector_count));
2302 return 0;
2303}
2304
2305/**
2306 * _base_free_irq - free irq
2307 * @ioc: per adapter object
2308 *
2309 * Freeing respective reply_queue from the list.
2310 */
2311static void
2312_base_free_irq(struct MPT3SAS_ADAPTER *ioc)
2313{
2314 struct adapter_reply_queue *reply_q, *next;
2315
2316 if (list_empty(&ioc->reply_queue_list))
2317 return;
2318
2319 list_for_each_entry_safe(reply_q, next, &ioc->reply_queue_list, list) {
2320 list_del(&reply_q->list);
Hannes Reinecke1d55abc2017-02-22 11:31:41 +01002321 free_irq(pci_irq_vector(ioc->pdev, reply_q->msix_index),
2322 reply_q);
Sreekanth Reddyf92363d2012-11-30 07:44:21 +05302323 kfree(reply_q);
2324 }
2325}
2326
2327/**
2328 * _base_request_irq - request irq
2329 * @ioc: per adapter object
2330 * @index: msix index into vector table
Sreekanth Reddyf92363d2012-11-30 07:44:21 +05302331 *
2332 * Inserting respective reply_queue into the list.
2333 */
2334static int
Hannes Reinecke1d55abc2017-02-22 11:31:41 +01002335_base_request_irq(struct MPT3SAS_ADAPTER *ioc, u8 index)
Sreekanth Reddyf92363d2012-11-30 07:44:21 +05302336{
Hannes Reinecke1d55abc2017-02-22 11:31:41 +01002337 struct pci_dev *pdev = ioc->pdev;
Sreekanth Reddyf92363d2012-11-30 07:44:21 +05302338 struct adapter_reply_queue *reply_q;
2339 int r;
2340
2341 reply_q = kzalloc(sizeof(struct adapter_reply_queue), GFP_KERNEL);
2342 if (!reply_q) {
2343 pr_err(MPT3SAS_FMT "unable to allocate memory %d!\n",
2344 ioc->name, (int)sizeof(struct adapter_reply_queue));
2345 return -ENOMEM;
2346 }
2347 reply_q->ioc = ioc;
2348 reply_q->msix_index = index;
Sreekanth Reddy14b31142015-01-12 11:39:03 +05302349
Sreekanth Reddyf92363d2012-11-30 07:44:21 +05302350 atomic_set(&reply_q->busy, 0);
2351 if (ioc->msix_enable)
2352 snprintf(reply_q->name, MPT_NAME_LENGTH, "%s%d-msix%d",
Sreekanth Reddyc84b06a2015-11-11 17:30:35 +05302353 ioc->driver_name, ioc->id, index);
Sreekanth Reddyf92363d2012-11-30 07:44:21 +05302354 else
2355 snprintf(reply_q->name, MPT_NAME_LENGTH, "%s%d",
Sreekanth Reddyc84b06a2015-11-11 17:30:35 +05302356 ioc->driver_name, ioc->id);
Hannes Reinecke1d55abc2017-02-22 11:31:41 +01002357 r = request_irq(pci_irq_vector(pdev, index), _base_interrupt,
2358 IRQF_SHARED, reply_q->name, reply_q);
Sreekanth Reddyf92363d2012-11-30 07:44:21 +05302359 if (r) {
2360 pr_err(MPT3SAS_FMT "unable to allocate interrupt %d!\n",
Hannes Reinecke1d55abc2017-02-22 11:31:41 +01002361 reply_q->name, pci_irq_vector(pdev, index));
Suganath prabu Subramanida3cec22016-02-11 15:02:55 +05302362 kfree(reply_q);
Sreekanth Reddyf92363d2012-11-30 07:44:21 +05302363 return -EBUSY;
2364 }
2365
2366 INIT_LIST_HEAD(&reply_q->list);
2367 list_add_tail(&reply_q->list, &ioc->reply_queue_list);
2368 return 0;
2369}
2370
2371/**
2372 * _base_assign_reply_queues - assigning msix index for each cpu
2373 * @ioc: per adapter object
2374 *
2375 * The enduser would need to set the affinity via /proc/irq/#/smp_affinity
2376 *
2377 * It would nice if we could call irq_set_affinity, however it is not
2378 * an exported symbol
2379 */
2380static void
2381_base_assign_reply_queues(struct MPT3SAS_ADAPTER *ioc)
2382{
Martin K. Petersen91b265b2014-01-03 19:16:56 -05002383 unsigned int cpu, nr_cpus, nr_msix, index = 0;
Sreekanth Reddy14b31142015-01-12 11:39:03 +05302384 struct adapter_reply_queue *reply_q;
Sreekanth Reddyf92363d2012-11-30 07:44:21 +05302385
2386 if (!_base_is_controller_msix_enabled(ioc))
2387 return;
2388
2389 memset(ioc->cpu_msix_table, 0, ioc->cpu_msix_table_sz);
2390
Martin K. Petersen91b265b2014-01-03 19:16:56 -05002391 nr_cpus = num_online_cpus();
2392 nr_msix = ioc->reply_queue_count = min(ioc->reply_queue_count,
2393 ioc->facts.MaxMSIxVectors);
2394 if (!nr_msix)
2395 return;
Sreekanth Reddyf92363d2012-11-30 07:44:21 +05302396
Hannes Reinecke1d55abc2017-02-22 11:31:41 +01002397 if (smp_affinity_enable) {
2398 list_for_each_entry(reply_q, &ioc->reply_queue_list, list) {
2399 const cpumask_t *mask = pci_irq_get_affinity(ioc->pdev,
2400 reply_q->msix_index);
2401 if (!mask) {
2402 pr_warn(MPT3SAS_FMT "no affinity for msi %x\n",
2403 ioc->name, reply_q->msix_index);
2404 continue;
2405 }
2406
2407 for_each_cpu(cpu, mask)
2408 ioc->cpu_msix_table[cpu] = reply_q->msix_index;
2409 }
2410 return;
2411 }
Martin K. Petersen91b265b2014-01-03 19:16:56 -05002412 cpu = cpumask_first(cpu_online_mask);
Sreekanth Reddyf92363d2012-11-30 07:44:21 +05302413
Sreekanth Reddy14b31142015-01-12 11:39:03 +05302414 list_for_each_entry(reply_q, &ioc->reply_queue_list, list) {
2415
Martin K. Petersen91b265b2014-01-03 19:16:56 -05002416 unsigned int i, group = nr_cpus / nr_msix;
2417
Sreekanth Reddy14b31142015-01-12 11:39:03 +05302418 if (cpu >= nr_cpus)
2419 break;
2420
Martin K. Petersen91b265b2014-01-03 19:16:56 -05002421 if (index < nr_cpus % nr_msix)
2422 group++;
2423
2424 for (i = 0 ; i < group ; i++) {
Hannes Reinecke1d55abc2017-02-22 11:31:41 +01002425 ioc->cpu_msix_table[cpu] = reply_q->msix_index;
Martin K. Petersen91b265b2014-01-03 19:16:56 -05002426 cpu = cpumask_next(cpu, cpu_online_mask);
Sreekanth Reddyf92363d2012-11-30 07:44:21 +05302427 }
Martin K. Petersen91b265b2014-01-03 19:16:56 -05002428 index++;
Sreekanth Reddy14b31142015-01-12 11:39:03 +05302429 }
Sreekanth Reddyf92363d2012-11-30 07:44:21 +05302430}
2431
2432/**
2433 * _base_disable_msix - disables msix
2434 * @ioc: per adapter object
2435 *
2436 */
2437static void
2438_base_disable_msix(struct MPT3SAS_ADAPTER *ioc)
2439{
2440 if (!ioc->msix_enable)
2441 return;
2442 pci_disable_msix(ioc->pdev);
2443 ioc->msix_enable = 0;
2444}
2445
2446/**
2447 * _base_enable_msix - enables msix, failback to io_apic
2448 * @ioc: per adapter object
2449 *
2450 */
2451static int
2452_base_enable_msix(struct MPT3SAS_ADAPTER *ioc)
2453{
Sreekanth Reddyf92363d2012-11-30 07:44:21 +05302454 int r;
Suganath Prabu Subramanibb350662016-10-26 13:34:33 +05302455 int i, local_max_msix_vectors;
Sreekanth Reddyf92363d2012-11-30 07:44:21 +05302456 u8 try_msix = 0;
Hannes Reinecke1d55abc2017-02-22 11:31:41 +01002457 unsigned int irq_flags = PCI_IRQ_MSIX;
Sreekanth Reddyf92363d2012-11-30 07:44:21 +05302458
Sreekanth Reddyf92363d2012-11-30 07:44:21 +05302459 if (msix_disable == -1 || msix_disable == 0)
2460 try_msix = 1;
2461
2462 if (!try_msix)
2463 goto try_ioapic;
2464
2465 if (_base_check_enable_msix(ioc) != 0)
2466 goto try_ioapic;
2467
2468 ioc->reply_queue_count = min_t(int, ioc->cpu_count,
Hannes Reinecke1d55abc2017-02-22 11:31:41 +01002469 ioc->msix_vector_count);
Sreekanth Reddyf92363d2012-11-30 07:44:21 +05302470
Sreekanth Reddy9c500062013-08-14 18:23:20 +05302471 printk(MPT3SAS_FMT "MSI-X vectors supported: %d, no of cores"
2472 ": %d, max_msix_vectors: %d\n", ioc->name, ioc->msix_vector_count,
2473 ioc->cpu_count, max_msix_vectors);
2474
Sreekanth Reddy9b05c912014-09-12 15:35:31 +05302475 if (!ioc->rdpq_array_enable && max_msix_vectors == -1)
Sreekanth Reddy06f5f972017-10-10 18:41:16 +05302476 local_max_msix_vectors = (reset_devices) ? 1 : 8;
Suganath Prabu Subramanibb350662016-10-26 13:34:33 +05302477 else
2478 local_max_msix_vectors = max_msix_vectors;
Sreekanth Reddy9b05c912014-09-12 15:35:31 +05302479
Hannes Reinecke1d55abc2017-02-22 11:31:41 +01002480 if (local_max_msix_vectors > 0)
Suganath Prabu Subramanibb350662016-10-26 13:34:33 +05302481 ioc->reply_queue_count = min_t(int, local_max_msix_vectors,
Sreekanth Reddy9c500062013-08-14 18:23:20 +05302482 ioc->reply_queue_count);
Hannes Reinecke1d55abc2017-02-22 11:31:41 +01002483 else if (local_max_msix_vectors == 0)
Sreekanth Reddy9b05c912014-09-12 15:35:31 +05302484 goto try_ioapic;
Sreekanth Reddy9c500062013-08-14 18:23:20 +05302485
Suganath Prabu Subramani64038302016-02-08 22:13:39 +05302486 if (ioc->msix_vector_count < ioc->cpu_count)
2487 smp_affinity_enable = 0;
2488
Hannes Reinecke1d55abc2017-02-22 11:31:41 +01002489 if (smp_affinity_enable)
2490 irq_flags |= PCI_IRQ_AFFINITY;
Sreekanth Reddyf92363d2012-11-30 07:44:21 +05302491
Hannes Reinecke1d55abc2017-02-22 11:31:41 +01002492 r = pci_alloc_irq_vectors(ioc->pdev, 1, ioc->reply_queue_count,
2493 irq_flags);
2494 if (r < 0) {
Sreekanth Reddyf92363d2012-11-30 07:44:21 +05302495 dfailprintk(ioc, pr_info(MPT3SAS_FMT
Hannes Reinecke1d55abc2017-02-22 11:31:41 +01002496 "pci_alloc_irq_vectors failed (r=%d) !!!\n",
Sreekanth Reddyf92363d2012-11-30 07:44:21 +05302497 ioc->name, r));
Sreekanth Reddyf92363d2012-11-30 07:44:21 +05302498 goto try_ioapic;
2499 }
2500
2501 ioc->msix_enable = 1;
Hannes Reinecke1d55abc2017-02-22 11:31:41 +01002502 ioc->reply_queue_count = r;
2503 for (i = 0; i < ioc->reply_queue_count; i++) {
2504 r = _base_request_irq(ioc, i);
Sreekanth Reddyf92363d2012-11-30 07:44:21 +05302505 if (r) {
2506 _base_free_irq(ioc);
2507 _base_disable_msix(ioc);
Sreekanth Reddyf92363d2012-11-30 07:44:21 +05302508 goto try_ioapic;
2509 }
2510 }
2511
Sreekanth Reddyf92363d2012-11-30 07:44:21 +05302512 return 0;
2513
2514/* failback to io_apic interrupt routing */
2515 try_ioapic:
2516
Sreekanth Reddy9b05c912014-09-12 15:35:31 +05302517 ioc->reply_queue_count = 1;
Hannes Reinecke1d55abc2017-02-22 11:31:41 +01002518 r = pci_alloc_irq_vectors(ioc->pdev, 1, 1, PCI_IRQ_LEGACY);
2519 if (r < 0) {
2520 dfailprintk(ioc, pr_info(MPT3SAS_FMT
2521 "pci_alloc_irq_vector(legacy) failed (r=%d) !!!\n",
2522 ioc->name, r));
2523 } else
2524 r = _base_request_irq(ioc, 0);
Sreekanth Reddyf92363d2012-11-30 07:44:21 +05302525
2526 return r;
2527}
2528
2529/**
Sreekanth Reddy580d4e32015-06-30 12:24:50 +05302530 * mpt3sas_base_unmap_resources - free controller resources
2531 * @ioc: per adapter object
2532 */
Calvin Owens8bbb1cf2016-07-28 21:38:22 -07002533static void
Sreekanth Reddy580d4e32015-06-30 12:24:50 +05302534mpt3sas_base_unmap_resources(struct MPT3SAS_ADAPTER *ioc)
2535{
2536 struct pci_dev *pdev = ioc->pdev;
2537
2538 dexitprintk(ioc, printk(MPT3SAS_FMT "%s\n",
2539 ioc->name, __func__));
2540
2541 _base_free_irq(ioc);
2542 _base_disable_msix(ioc);
2543
Suganath Prabu Subramani0bb337c2016-10-26 13:34:38 +05302544 if (ioc->combined_reply_queue) {
Sreekanth Reddy580d4e32015-06-30 12:24:50 +05302545 kfree(ioc->replyPostRegisterIndex);
Tomas Henzl5f985d82015-12-23 14:21:47 +01002546 ioc->replyPostRegisterIndex = NULL;
2547 }
Sreekanth Reddy580d4e32015-06-30 12:24:50 +05302548
2549 if (ioc->chip_phys) {
2550 iounmap(ioc->chip);
2551 ioc->chip_phys = 0;
2552 }
2553
2554 if (pci_is_enabled(pdev)) {
2555 pci_release_selected_regions(ioc->pdev, ioc->bars);
2556 pci_disable_pcie_error_reporting(pdev);
2557 pci_disable_device(pdev);
2558 }
2559}
2560
2561/**
Sreekanth Reddyf92363d2012-11-30 07:44:21 +05302562 * mpt3sas_base_map_resources - map in controller resources (io/irq/memap)
2563 * @ioc: per adapter object
2564 *
2565 * Returns 0 for success, non-zero for failure.
2566 */
2567int
2568mpt3sas_base_map_resources(struct MPT3SAS_ADAPTER *ioc)
2569{
2570 struct pci_dev *pdev = ioc->pdev;
2571 u32 memap_sz;
2572 u32 pio_sz;
2573 int i, r = 0;
2574 u64 pio_chip = 0;
2575 u64 chip_phys = 0;
2576 struct adapter_reply_queue *reply_q;
2577
2578 dinitprintk(ioc, pr_info(MPT3SAS_FMT "%s\n",
2579 ioc->name, __func__));
2580
2581 ioc->bars = pci_select_bars(pdev, IORESOURCE_MEM);
2582 if (pci_enable_device_mem(pdev)) {
2583 pr_warn(MPT3SAS_FMT "pci_enable_device_mem: failed\n",
2584 ioc->name);
Joe Lawrencecf9bd21a2013-08-08 16:45:39 -04002585 ioc->bars = 0;
Sreekanth Reddyf92363d2012-11-30 07:44:21 +05302586 return -ENODEV;
2587 }
2588
2589
2590 if (pci_request_selected_regions(pdev, ioc->bars,
Sreekanth Reddyc84b06a2015-11-11 17:30:35 +05302591 ioc->driver_name)) {
Sreekanth Reddyf92363d2012-11-30 07:44:21 +05302592 pr_warn(MPT3SAS_FMT "pci_request_selected_regions: failed\n",
2593 ioc->name);
Joe Lawrencecf9bd21a2013-08-08 16:45:39 -04002594 ioc->bars = 0;
Sreekanth Reddyf92363d2012-11-30 07:44:21 +05302595 r = -ENODEV;
2596 goto out_fail;
2597 }
2598
2599/* AER (Advanced Error Reporting) hooks */
2600 pci_enable_pcie_error_reporting(pdev);
2601
2602 pci_set_master(pdev);
2603
2604
2605 if (_base_config_dma_addressing(ioc, pdev) != 0) {
2606 pr_warn(MPT3SAS_FMT "no suitable DMA mask for %s\n",
2607 ioc->name, pci_name(pdev));
2608 r = -ENODEV;
2609 goto out_fail;
2610 }
2611
Sreekanth Reddy5aeeb782015-07-15 10:19:56 +05302612 for (i = 0, memap_sz = 0, pio_sz = 0; (i < DEVICE_COUNT_RESOURCE) &&
2613 (!memap_sz || !pio_sz); i++) {
Sreekanth Reddyf92363d2012-11-30 07:44:21 +05302614 if (pci_resource_flags(pdev, i) & IORESOURCE_IO) {
2615 if (pio_sz)
2616 continue;
2617 pio_chip = (u64)pci_resource_start(pdev, i);
2618 pio_sz = pci_resource_len(pdev, i);
2619 } else if (pci_resource_flags(pdev, i) & IORESOURCE_MEM) {
2620 if (memap_sz)
2621 continue;
2622 ioc->chip_phys = pci_resource_start(pdev, i);
2623 chip_phys = (u64)ioc->chip_phys;
2624 memap_sz = pci_resource_len(pdev, i);
2625 ioc->chip = ioremap(ioc->chip_phys, memap_sz);
Sreekanth Reddyf92363d2012-11-30 07:44:21 +05302626 }
2627 }
2628
Sreekanth Reddy5aeeb782015-07-15 10:19:56 +05302629 if (ioc->chip == NULL) {
2630 pr_err(MPT3SAS_FMT "unable to map adapter memory! "
2631 " or resource not found\n", ioc->name);
2632 r = -EINVAL;
2633 goto out_fail;
2634 }
2635
Sreekanth Reddyf92363d2012-11-30 07:44:21 +05302636 _base_mask_interrupts(ioc);
Sreekanth Reddy9b05c912014-09-12 15:35:31 +05302637
Calvin Owens98c56ad2016-07-28 21:38:21 -07002638 r = _base_get_ioc_facts(ioc);
Sreekanth Reddy9b05c912014-09-12 15:35:31 +05302639 if (r)
2640 goto out_fail;
2641
2642 if (!ioc->rdpq_array_enable_assigned) {
2643 ioc->rdpq_array_enable = ioc->rdpq_array_capable;
2644 ioc->rdpq_array_enable_assigned = 1;
2645 }
2646
Sreekanth Reddyf92363d2012-11-30 07:44:21 +05302647 r = _base_enable_msix(ioc);
2648 if (r)
2649 goto out_fail;
2650
Sreekanth Reddyfb77bb52015-06-30 12:24:47 +05302651 /* Use the Combined reply queue feature only for SAS3 C0 & higher
2652 * revision HBAs and also only when reply queue count is greater than 8
2653 */
Suganath Prabu Subramani0bb337c2016-10-26 13:34:38 +05302654 if (ioc->combined_reply_queue && ioc->reply_queue_count > 8) {
Sreekanth Reddyfb77bb52015-06-30 12:24:47 +05302655 /* Determine the Supplemental Reply Post Host Index Registers
2656 * Addresse. Supplemental Reply Post Host Index Registers
2657 * starts at offset MPI25_SUP_REPLY_POST_HOST_INDEX_OFFSET and
2658 * each register is at offset bytes of
2659 * MPT3_SUP_REPLY_POST_HOST_INDEX_REG_OFFSET from previous one.
2660 */
2661 ioc->replyPostRegisterIndex = kcalloc(
Suganath Prabu Subramani0bb337c2016-10-26 13:34:38 +05302662 ioc->combined_reply_index_count,
Sreekanth Reddyfb77bb52015-06-30 12:24:47 +05302663 sizeof(resource_size_t *), GFP_KERNEL);
2664 if (!ioc->replyPostRegisterIndex) {
2665 dfailprintk(ioc, printk(MPT3SAS_FMT
2666 "allocation for reply Post Register Index failed!!!\n",
2667 ioc->name));
2668 r = -ENOMEM;
2669 goto out_fail;
2670 }
2671
Suganath Prabu Subramani0bb337c2016-10-26 13:34:38 +05302672 for (i = 0; i < ioc->combined_reply_index_count; i++) {
Sreekanth Reddyfb77bb52015-06-30 12:24:47 +05302673 ioc->replyPostRegisterIndex[i] = (resource_size_t *)
2674 ((u8 *)&ioc->chip->Doorbell +
2675 MPI25_SUP_REPLY_POST_HOST_INDEX_OFFSET +
2676 (i * MPT3_SUP_REPLY_POST_HOST_INDEX_REG_OFFSET));
2677 }
2678 } else
Suganath Prabu Subramani0bb337c2016-10-26 13:34:38 +05302679 ioc->combined_reply_queue = 0;
Sreekanth Reddyfb77bb52015-06-30 12:24:47 +05302680
Greg Edwardsce7c6c92016-07-30 10:06:26 -06002681 if (ioc->is_warpdrive) {
2682 ioc->reply_post_host_index[0] = (resource_size_t __iomem *)
2683 &ioc->chip->ReplyPostHostIndex;
2684
2685 for (i = 1; i < ioc->cpu_msix_table_sz; i++)
2686 ioc->reply_post_host_index[i] =
2687 (resource_size_t __iomem *)
2688 ((u8 __iomem *)&ioc->chip->Doorbell + (0x4000 + ((i - 1)
2689 * 4)));
2690 }
2691
Sreekanth Reddyf92363d2012-11-30 07:44:21 +05302692 list_for_each_entry(reply_q, &ioc->reply_queue_list, list)
2693 pr_info(MPT3SAS_FMT "%s: IRQ %d\n",
2694 reply_q->name, ((ioc->msix_enable) ? "PCI-MSI-X enabled" :
Hannes Reinecke1d55abc2017-02-22 11:31:41 +01002695 "IO-APIC enabled"),
2696 pci_irq_vector(ioc->pdev, reply_q->msix_index));
Sreekanth Reddyf92363d2012-11-30 07:44:21 +05302697
2698 pr_info(MPT3SAS_FMT "iomem(0x%016llx), mapped(0x%p), size(%d)\n",
2699 ioc->name, (unsigned long long)chip_phys, ioc->chip, memap_sz);
2700 pr_info(MPT3SAS_FMT "ioport(0x%016llx), size(%d)\n",
2701 ioc->name, (unsigned long long)pio_chip, pio_sz);
2702
2703 /* Save PCI configuration state for recovery from PCI AER/EEH errors */
2704 pci_save_state(pdev);
2705 return 0;
2706
2707 out_fail:
Sreekanth Reddy580d4e32015-06-30 12:24:50 +05302708 mpt3sas_base_unmap_resources(ioc);
Sreekanth Reddyf92363d2012-11-30 07:44:21 +05302709 return r;
2710}
2711
2712/**
2713 * mpt3sas_base_get_msg_frame - obtain request mf pointer
2714 * @ioc: per adapter object
2715 * @smid: system request message index(smid zero is invalid)
2716 *
2717 * Returns virt pointer to message frame.
2718 */
2719void *
2720mpt3sas_base_get_msg_frame(struct MPT3SAS_ADAPTER *ioc, u16 smid)
2721{
2722 return (void *)(ioc->request + (smid * ioc->request_sz));
2723}
2724
2725/**
2726 * mpt3sas_base_get_sense_buffer - obtain a sense buffer virt addr
2727 * @ioc: per adapter object
2728 * @smid: system request message index
2729 *
2730 * Returns virt pointer to sense buffer.
2731 */
2732void *
2733mpt3sas_base_get_sense_buffer(struct MPT3SAS_ADAPTER *ioc, u16 smid)
2734{
2735 return (void *)(ioc->sense + ((smid - 1) * SCSI_SENSE_BUFFERSIZE));
2736}
2737
2738/**
2739 * mpt3sas_base_get_sense_buffer_dma - obtain a sense buffer dma addr
2740 * @ioc: per adapter object
2741 * @smid: system request message index
2742 *
2743 * Returns phys pointer to the low 32bit address of the sense buffer.
2744 */
2745__le32
2746mpt3sas_base_get_sense_buffer_dma(struct MPT3SAS_ADAPTER *ioc, u16 smid)
2747{
2748 return cpu_to_le32(ioc->sense_dma + ((smid - 1) *
2749 SCSI_SENSE_BUFFERSIZE));
2750}
2751
2752/**
Suganath Prabu Subramani016d5c32017-10-31 18:02:28 +05302753 * mpt3sas_base_get_pcie_sgl - obtain a PCIe SGL virt addr
2754 * @ioc: per adapter object
2755 * @smid: system request message index
2756 *
2757 * Returns virt pointer to a PCIe SGL.
2758 */
2759void *
2760mpt3sas_base_get_pcie_sgl(struct MPT3SAS_ADAPTER *ioc, u16 smid)
2761{
2762 return (void *)(ioc->scsi_lookup[smid - 1].pcie_sg_list.pcie_sgl);
2763}
2764
2765/**
2766 * mpt3sas_base_get_pcie_sgl_dma - obtain a PCIe SGL dma addr
2767 * @ioc: per adapter object
2768 * @smid: system request message index
2769 *
2770 * Returns phys pointer to the address of the PCIe buffer.
2771 */
Arnd Bergmannd8335ae2017-11-06 14:35:16 +01002772dma_addr_t
Suganath Prabu Subramani016d5c32017-10-31 18:02:28 +05302773mpt3sas_base_get_pcie_sgl_dma(struct MPT3SAS_ADAPTER *ioc, u16 smid)
2774{
Arnd Bergmannd8335ae2017-11-06 14:35:16 +01002775 return ioc->scsi_lookup[smid - 1].pcie_sg_list.pcie_sgl_dma;
Suganath Prabu Subramani016d5c32017-10-31 18:02:28 +05302776}
2777
2778/**
Sreekanth Reddyf92363d2012-11-30 07:44:21 +05302779 * mpt3sas_base_get_reply_virt_addr - obtain reply frames virt address
2780 * @ioc: per adapter object
2781 * @phys_addr: lower 32 physical addr of the reply
2782 *
2783 * Converts 32bit lower physical addr into a virt address.
2784 */
2785void *
2786mpt3sas_base_get_reply_virt_addr(struct MPT3SAS_ADAPTER *ioc, u32 phys_addr)
2787{
2788 if (!phys_addr)
2789 return NULL;
2790 return ioc->reply + (phys_addr - (u32)ioc->reply_dma);
2791}
2792
Suganath prabu Subramani03d1fb32016-01-28 12:07:06 +05302793static inline u8
2794_base_get_msix_index(struct MPT3SAS_ADAPTER *ioc)
2795{
2796 return ioc->cpu_msix_table[raw_smp_processor_id()];
2797}
2798
Sreekanth Reddyf92363d2012-11-30 07:44:21 +05302799/**
2800 * mpt3sas_base_get_smid - obtain a free smid from internal queue
2801 * @ioc: per adapter object
2802 * @cb_idx: callback index
2803 *
2804 * Returns smid (zero is invalid)
2805 */
2806u16
2807mpt3sas_base_get_smid(struct MPT3SAS_ADAPTER *ioc, u8 cb_idx)
2808{
2809 unsigned long flags;
2810 struct request_tracker *request;
2811 u16 smid;
2812
2813 spin_lock_irqsave(&ioc->scsi_lookup_lock, flags);
2814 if (list_empty(&ioc->internal_free_list)) {
2815 spin_unlock_irqrestore(&ioc->scsi_lookup_lock, flags);
2816 pr_err(MPT3SAS_FMT "%s: smid not available\n",
2817 ioc->name, __func__);
2818 return 0;
2819 }
2820
2821 request = list_entry(ioc->internal_free_list.next,
2822 struct request_tracker, tracker_list);
2823 request->cb_idx = cb_idx;
2824 smid = request->smid;
2825 list_del(&request->tracker_list);
2826 spin_unlock_irqrestore(&ioc->scsi_lookup_lock, flags);
2827 return smid;
2828}
2829
2830/**
2831 * mpt3sas_base_get_smid_scsiio - obtain a free smid from scsiio queue
2832 * @ioc: per adapter object
2833 * @cb_idx: callback index
2834 * @scmd: pointer to scsi command object
2835 *
2836 * Returns smid (zero is invalid)
2837 */
2838u16
2839mpt3sas_base_get_smid_scsiio(struct MPT3SAS_ADAPTER *ioc, u8 cb_idx,
2840 struct scsi_cmnd *scmd)
2841{
2842 unsigned long flags;
2843 struct scsiio_tracker *request;
2844 u16 smid;
2845
2846 spin_lock_irqsave(&ioc->scsi_lookup_lock, flags);
2847 if (list_empty(&ioc->free_list)) {
2848 spin_unlock_irqrestore(&ioc->scsi_lookup_lock, flags);
2849 pr_err(MPT3SAS_FMT "%s: smid not available\n",
2850 ioc->name, __func__);
2851 return 0;
2852 }
2853
2854 request = list_entry(ioc->free_list.next,
2855 struct scsiio_tracker, tracker_list);
2856 request->scmd = scmd;
2857 request->cb_idx = cb_idx;
2858 smid = request->smid;
Suganath prabu Subramani03d1fb32016-01-28 12:07:06 +05302859 request->msix_io = _base_get_msix_index(ioc);
Sreekanth Reddyf92363d2012-11-30 07:44:21 +05302860 list_del(&request->tracker_list);
2861 spin_unlock_irqrestore(&ioc->scsi_lookup_lock, flags);
2862 return smid;
2863}
2864
2865/**
2866 * mpt3sas_base_get_smid_hpr - obtain a free smid from hi-priority queue
2867 * @ioc: per adapter object
2868 * @cb_idx: callback index
2869 *
2870 * Returns smid (zero is invalid)
2871 */
2872u16
2873mpt3sas_base_get_smid_hpr(struct MPT3SAS_ADAPTER *ioc, u8 cb_idx)
2874{
2875 unsigned long flags;
2876 struct request_tracker *request;
2877 u16 smid;
2878
2879 spin_lock_irqsave(&ioc->scsi_lookup_lock, flags);
2880 if (list_empty(&ioc->hpr_free_list)) {
2881 spin_unlock_irqrestore(&ioc->scsi_lookup_lock, flags);
2882 return 0;
2883 }
2884
2885 request = list_entry(ioc->hpr_free_list.next,
2886 struct request_tracker, tracker_list);
2887 request->cb_idx = cb_idx;
2888 smid = request->smid;
2889 list_del(&request->tracker_list);
2890 spin_unlock_irqrestore(&ioc->scsi_lookup_lock, flags);
2891 return smid;
2892}
2893
Hannes Reinecke6a2d4612018-01-04 04:57:03 -08002894static void
2895_base_recovery_check(struct MPT3SAS_ADAPTER *ioc)
2896{
2897 /*
2898 * See _wait_for_commands_to_complete() call with regards to this code.
2899 */
2900 if (ioc->shost_recovery && ioc->pending_io_count) {
2901 if (ioc->pending_io_count == 1)
2902 wake_up(&ioc->reset_wq);
2903 ioc->pending_io_count--;
2904 }
2905}
2906
Sreekanth Reddyf92363d2012-11-30 07:44:21 +05302907/**
2908 * mpt3sas_base_free_smid - put smid back on free_list
2909 * @ioc: per adapter object
2910 * @smid: system request message index
2911 *
2912 * Return nothing.
2913 */
2914void
2915mpt3sas_base_free_smid(struct MPT3SAS_ADAPTER *ioc, u16 smid)
2916{
2917 unsigned long flags;
2918 int i;
Sreekanth Reddyf92363d2012-11-30 07:44:21 +05302919
2920 spin_lock_irqsave(&ioc->scsi_lookup_lock, flags);
2921 if (smid < ioc->hi_priority_smid) {
2922 /* scsiio queue */
2923 i = smid - 1;
Hannes Reinecke05303df2018-01-04 04:57:02 -08002924 list_splice_init(&ioc->scsi_lookup[i].chain_list,
2925 &ioc->free_chain_list);
Sreekanth Reddyf92363d2012-11-30 07:44:21 +05302926 ioc->scsi_lookup[i].cb_idx = 0xFF;
2927 ioc->scsi_lookup[i].scmd = NULL;
Sreekanth Reddy7786ab62015-11-11 17:30:28 +05302928 ioc->scsi_lookup[i].direct_io = 0;
Hannes Reineckeb0cd285e2018-01-04 04:57:07 -08002929 if (i < ioc->scsiio_depth - INTERNAL_SCSIIO_CMDS_COUNT)
2930 list_add(&ioc->scsi_lookup[i].tracker_list,
2931 &ioc->free_list);
Sreekanth Reddyf92363d2012-11-30 07:44:21 +05302932 spin_unlock_irqrestore(&ioc->scsi_lookup_lock, flags);
2933
Hannes Reinecke6a2d4612018-01-04 04:57:03 -08002934 _base_recovery_check(ioc);
Sreekanth Reddyf92363d2012-11-30 07:44:21 +05302935 return;
2936 } else if (smid < ioc->internal_smid) {
2937 /* hi-priority */
2938 i = smid - ioc->hi_priority_smid;
2939 ioc->hpr_lookup[i].cb_idx = 0xFF;
2940 list_add(&ioc->hpr_lookup[i].tracker_list, &ioc->hpr_free_list);
2941 } else if (smid <= ioc->hba_queue_depth) {
2942 /* internal queue */
2943 i = smid - ioc->internal_smid;
2944 ioc->internal_lookup[i].cb_idx = 0xFF;
2945 list_add(&ioc->internal_lookup[i].tracker_list,
2946 &ioc->internal_free_list);
2947 }
2948 spin_unlock_irqrestore(&ioc->scsi_lookup_lock, flags);
2949}
2950
2951/**
2952 * _base_writeq - 64 bit write to MMIO
2953 * @ioc: per adapter object
2954 * @b: data payload
2955 * @addr: address in MMIO space
2956 * @writeq_lock: spin lock
2957 *
2958 * Glue for handling an atomic 64 bit word to MMIO. This special handling takes
2959 * care of 32 bit environment where its not quarenteed to send the entire word
2960 * in one transfer.
2961 */
2962#if defined(writeq) && defined(CONFIG_64BIT)
2963static inline void
2964_base_writeq(__u64 b, volatile void __iomem *addr, spinlock_t *writeq_lock)
2965{
2966 writeq(cpu_to_le64(b), addr);
2967}
2968#else
2969static inline void
2970_base_writeq(__u64 b, volatile void __iomem *addr, spinlock_t *writeq_lock)
2971{
2972 unsigned long flags;
2973 __u64 data_out = cpu_to_le64(b);
2974
2975 spin_lock_irqsave(writeq_lock, flags);
2976 writel((u32)(data_out), addr);
2977 writel((u32)(data_out >> 32), (addr + 4));
2978 spin_unlock_irqrestore(writeq_lock, flags);
2979}
2980#endif
2981
Sreekanth Reddyf92363d2012-11-30 07:44:21 +05302982/**
Suganath Prabu Subramani81c16f82016-10-26 13:34:40 +05302983 * _base_put_smid_scsi_io - send SCSI_IO request to firmware
Sreekanth Reddyf92363d2012-11-30 07:44:21 +05302984 * @ioc: per adapter object
2985 * @smid: system request message index
2986 * @handle: device handle
2987 *
2988 * Return nothing.
2989 */
Suganath Prabu Subramani81c16f82016-10-26 13:34:40 +05302990static void
2991_base_put_smid_scsi_io(struct MPT3SAS_ADAPTER *ioc, u16 smid, u16 handle)
Sreekanth Reddyf92363d2012-11-30 07:44:21 +05302992{
2993 Mpi2RequestDescriptorUnion_t descriptor;
2994 u64 *request = (u64 *)&descriptor;
2995
2996
2997 descriptor.SCSIIO.RequestFlags = MPI2_REQ_DESCRIPT_FLAGS_SCSI_IO;
2998 descriptor.SCSIIO.MSIxIndex = _base_get_msix_index(ioc);
2999 descriptor.SCSIIO.SMID = cpu_to_le16(smid);
3000 descriptor.SCSIIO.DevHandle = cpu_to_le16(handle);
3001 descriptor.SCSIIO.LMID = 0;
3002 _base_writeq(*request, &ioc->chip->RequestDescriptorPostLow,
3003 &ioc->scsi_lookup_lock);
3004}
3005
3006/**
Suganath Prabu Subramani81c16f82016-10-26 13:34:40 +05303007 * _base_put_smid_fast_path - send fast path request to firmware
Sreekanth Reddyf92363d2012-11-30 07:44:21 +05303008 * @ioc: per adapter object
3009 * @smid: system request message index
3010 * @handle: device handle
3011 *
3012 * Return nothing.
3013 */
Suganath Prabu Subramani81c16f82016-10-26 13:34:40 +05303014static void
3015_base_put_smid_fast_path(struct MPT3SAS_ADAPTER *ioc, u16 smid,
Sreekanth Reddyf92363d2012-11-30 07:44:21 +05303016 u16 handle)
3017{
3018 Mpi2RequestDescriptorUnion_t descriptor;
3019 u64 *request = (u64 *)&descriptor;
3020
3021 descriptor.SCSIIO.RequestFlags =
3022 MPI25_REQ_DESCRIPT_FLAGS_FAST_PATH_SCSI_IO;
3023 descriptor.SCSIIO.MSIxIndex = _base_get_msix_index(ioc);
3024 descriptor.SCSIIO.SMID = cpu_to_le16(smid);
3025 descriptor.SCSIIO.DevHandle = cpu_to_le16(handle);
3026 descriptor.SCSIIO.LMID = 0;
3027 _base_writeq(*request, &ioc->chip->RequestDescriptorPostLow,
3028 &ioc->scsi_lookup_lock);
3029}
3030
3031/**
Suganath Prabu Subramani81c16f82016-10-26 13:34:40 +05303032 * _base_put_smid_hi_priority - send Task Management request to firmware
Sreekanth Reddyf92363d2012-11-30 07:44:21 +05303033 * @ioc: per adapter object
3034 * @smid: system request message index
Suganath prabu Subramani03d1fb32016-01-28 12:07:06 +05303035 * @msix_task: msix_task will be same as msix of IO incase of task abort else 0.
Sreekanth Reddyf92363d2012-11-30 07:44:21 +05303036 * Return nothing.
3037 */
Suganath Prabu Subramani81c16f82016-10-26 13:34:40 +05303038static void
3039_base_put_smid_hi_priority(struct MPT3SAS_ADAPTER *ioc, u16 smid,
Suganath prabu Subramani03d1fb32016-01-28 12:07:06 +05303040 u16 msix_task)
Sreekanth Reddyf92363d2012-11-30 07:44:21 +05303041{
3042 Mpi2RequestDescriptorUnion_t descriptor;
3043 u64 *request = (u64 *)&descriptor;
3044
3045 descriptor.HighPriority.RequestFlags =
3046 MPI2_REQ_DESCRIPT_FLAGS_HIGH_PRIORITY;
Suganath prabu Subramani03d1fb32016-01-28 12:07:06 +05303047 descriptor.HighPriority.MSIxIndex = msix_task;
Sreekanth Reddyf92363d2012-11-30 07:44:21 +05303048 descriptor.HighPriority.SMID = cpu_to_le16(smid);
3049 descriptor.HighPriority.LMID = 0;
3050 descriptor.HighPriority.Reserved1 = 0;
3051 _base_writeq(*request, &ioc->chip->RequestDescriptorPostLow,
3052 &ioc->scsi_lookup_lock);
3053}
3054
3055/**
Suganath Prabu Subramaniaff39e62017-10-31 18:02:29 +05303056 * _base_put_smid_nvme_encap - send NVMe encapsulated request to
3057 * firmware
3058 * @ioc: per adapter object
3059 * @smid: system request message index
3060 *
3061 * Return nothing.
3062 */
3063static void
3064_base_put_smid_nvme_encap(struct MPT3SAS_ADAPTER *ioc, u16 smid)
3065{
3066 Mpi2RequestDescriptorUnion_t descriptor;
3067 u64 *request = (u64 *)&descriptor;
3068
3069 descriptor.Default.RequestFlags =
3070 MPI26_REQ_DESCRIPT_FLAGS_PCIE_ENCAPSULATED;
3071 descriptor.Default.MSIxIndex = _base_get_msix_index(ioc);
3072 descriptor.Default.SMID = cpu_to_le16(smid);
3073 descriptor.Default.LMID = 0;
3074 descriptor.Default.DescriptorTypeDependent = 0;
3075 _base_writeq(*request, &ioc->chip->RequestDescriptorPostLow,
3076 &ioc->scsi_lookup_lock);
3077}
3078
3079/**
Suganath Prabu Subramani81c16f82016-10-26 13:34:40 +05303080 * _base_put_smid_default - Default, primarily used for config pages
Sreekanth Reddyf92363d2012-11-30 07:44:21 +05303081 * @ioc: per adapter object
3082 * @smid: system request message index
3083 *
3084 * Return nothing.
3085 */
Suganath Prabu Subramani81c16f82016-10-26 13:34:40 +05303086static void
3087_base_put_smid_default(struct MPT3SAS_ADAPTER *ioc, u16 smid)
Sreekanth Reddyf92363d2012-11-30 07:44:21 +05303088{
3089 Mpi2RequestDescriptorUnion_t descriptor;
3090 u64 *request = (u64 *)&descriptor;
3091
3092 descriptor.Default.RequestFlags = MPI2_REQ_DESCRIPT_FLAGS_DEFAULT_TYPE;
3093 descriptor.Default.MSIxIndex = _base_get_msix_index(ioc);
3094 descriptor.Default.SMID = cpu_to_le16(smid);
3095 descriptor.Default.LMID = 0;
3096 descriptor.Default.DescriptorTypeDependent = 0;
3097 _base_writeq(*request, &ioc->chip->RequestDescriptorPostLow,
3098 &ioc->scsi_lookup_lock);
3099}
3100
Sreekanth Reddy1117b312014-09-12 15:35:30 +05303101/**
Suganath Prabu Subramani81c16f82016-10-26 13:34:40 +05303102* _base_put_smid_scsi_io_atomic - send SCSI_IO request to firmware using
3103* Atomic Request Descriptor
3104* @ioc: per adapter object
3105* @smid: system request message index
3106* @handle: device handle, unused in this function, for function type match
3107*
3108* Return nothing.
3109*/
3110static void
3111_base_put_smid_scsi_io_atomic(struct MPT3SAS_ADAPTER *ioc, u16 smid,
3112 u16 handle)
3113{
3114 Mpi26AtomicRequestDescriptor_t descriptor;
3115 u32 *request = (u32 *)&descriptor;
3116
3117 descriptor.RequestFlags = MPI2_REQ_DESCRIPT_FLAGS_SCSI_IO;
3118 descriptor.MSIxIndex = _base_get_msix_index(ioc);
3119 descriptor.SMID = cpu_to_le16(smid);
3120
3121 writel(cpu_to_le32(*request), &ioc->chip->AtomicRequestDescriptorPost);
3122}
3123
3124/**
3125 * _base_put_smid_fast_path_atomic - send fast path request to firmware
3126 * using Atomic Request Descriptor
3127 * @ioc: per adapter object
3128 * @smid: system request message index
3129 * @handle: device handle, unused in this function, for function type match
3130 * Return nothing
3131 */
3132static void
3133_base_put_smid_fast_path_atomic(struct MPT3SAS_ADAPTER *ioc, u16 smid,
3134 u16 handle)
3135{
3136 Mpi26AtomicRequestDescriptor_t descriptor;
3137 u32 *request = (u32 *)&descriptor;
3138
3139 descriptor.RequestFlags = MPI25_REQ_DESCRIPT_FLAGS_FAST_PATH_SCSI_IO;
3140 descriptor.MSIxIndex = _base_get_msix_index(ioc);
3141 descriptor.SMID = cpu_to_le16(smid);
3142
3143 writel(cpu_to_le32(*request), &ioc->chip->AtomicRequestDescriptorPost);
3144}
3145
3146/**
3147 * _base_put_smid_hi_priority_atomic - send Task Management request to
3148 * firmware using Atomic Request Descriptor
3149 * @ioc: per adapter object
3150 * @smid: system request message index
3151 * @msix_task: msix_task will be same as msix of IO incase of task abort else 0
3152 *
3153 * Return nothing.
3154 */
3155static void
3156_base_put_smid_hi_priority_atomic(struct MPT3SAS_ADAPTER *ioc, u16 smid,
3157 u16 msix_task)
3158{
3159 Mpi26AtomicRequestDescriptor_t descriptor;
3160 u32 *request = (u32 *)&descriptor;
3161
3162 descriptor.RequestFlags = MPI2_REQ_DESCRIPT_FLAGS_HIGH_PRIORITY;
3163 descriptor.MSIxIndex = msix_task;
3164 descriptor.SMID = cpu_to_le16(smid);
3165
3166 writel(cpu_to_le32(*request), &ioc->chip->AtomicRequestDescriptorPost);
3167}
3168
3169/**
Suganath Prabu Subramaniaff39e62017-10-31 18:02:29 +05303170 * _base_put_smid_nvme_encap_atomic - send NVMe encapsulated request to
3171 * firmware using Atomic Request Descriptor
3172 * @ioc: per adapter object
3173 * @smid: system request message index
3174 *
3175 * Return nothing.
3176 */
3177static void
3178_base_put_smid_nvme_encap_atomic(struct MPT3SAS_ADAPTER *ioc, u16 smid)
3179{
3180 Mpi26AtomicRequestDescriptor_t descriptor;
3181 u32 *request = (u32 *)&descriptor;
3182
3183 descriptor.RequestFlags = MPI26_REQ_DESCRIPT_FLAGS_PCIE_ENCAPSULATED;
3184 descriptor.MSIxIndex = _base_get_msix_index(ioc);
3185 descriptor.SMID = cpu_to_le16(smid);
3186
3187 writel(cpu_to_le32(*request), &ioc->chip->AtomicRequestDescriptorPost);
3188}
3189
3190/**
Suganath Prabu Subramani81c16f82016-10-26 13:34:40 +05303191 * _base_put_smid_default - Default, primarily used for config pages
3192 * use Atomic Request Descriptor
3193 * @ioc: per adapter object
3194 * @smid: system request message index
3195 *
3196 * Return nothing.
3197 */
3198static void
3199_base_put_smid_default_atomic(struct MPT3SAS_ADAPTER *ioc, u16 smid)
3200{
3201 Mpi26AtomicRequestDescriptor_t descriptor;
3202 u32 *request = (u32 *)&descriptor;
3203
3204 descriptor.RequestFlags = MPI2_REQ_DESCRIPT_FLAGS_DEFAULT_TYPE;
3205 descriptor.MSIxIndex = _base_get_msix_index(ioc);
3206 descriptor.SMID = cpu_to_le16(smid);
3207
3208 writel(cpu_to_le32(*request), &ioc->chip->AtomicRequestDescriptorPost);
3209}
3210
3211/**
Sreekanth Reddy989e43c2015-11-11 17:30:32 +05303212 * _base_display_OEMs_branding - Display branding string
Sreekanth Reddy1117b312014-09-12 15:35:30 +05303213 * @ioc: per adapter object
3214 *
3215 * Return nothing.
3216 */
3217static void
Sreekanth Reddy989e43c2015-11-11 17:30:32 +05303218_base_display_OEMs_branding(struct MPT3SAS_ADAPTER *ioc)
Sreekanth Reddy1117b312014-09-12 15:35:30 +05303219{
3220 if (ioc->pdev->subsystem_vendor != PCI_VENDOR_ID_INTEL)
3221 return;
3222
Sreekanth Reddy989e43c2015-11-11 17:30:32 +05303223 switch (ioc->pdev->subsystem_vendor) {
3224 case PCI_VENDOR_ID_INTEL:
3225 switch (ioc->pdev->device) {
3226 case MPI2_MFGPAGE_DEVID_SAS2008:
3227 switch (ioc->pdev->subsystem_device) {
3228 case MPT2SAS_INTEL_RMS2LL080_SSDID:
3229 pr_info(MPT3SAS_FMT "%s\n", ioc->name,
3230 MPT2SAS_INTEL_RMS2LL080_BRANDING);
3231 break;
3232 case MPT2SAS_INTEL_RMS2LL040_SSDID:
3233 pr_info(MPT3SAS_FMT "%s\n", ioc->name,
3234 MPT2SAS_INTEL_RMS2LL040_BRANDING);
3235 break;
3236 case MPT2SAS_INTEL_SSD910_SSDID:
3237 pr_info(MPT3SAS_FMT "%s\n", ioc->name,
3238 MPT2SAS_INTEL_SSD910_BRANDING);
3239 break;
3240 default:
3241 pr_info(MPT3SAS_FMT
3242 "Intel(R) Controller: Subsystem ID: 0x%X\n",
3243 ioc->name, ioc->pdev->subsystem_device);
3244 break;
3245 }
3246 case MPI2_MFGPAGE_DEVID_SAS2308_2:
3247 switch (ioc->pdev->subsystem_device) {
3248 case MPT2SAS_INTEL_RS25GB008_SSDID:
3249 pr_info(MPT3SAS_FMT "%s\n", ioc->name,
3250 MPT2SAS_INTEL_RS25GB008_BRANDING);
3251 break;
3252 case MPT2SAS_INTEL_RMS25JB080_SSDID:
3253 pr_info(MPT3SAS_FMT "%s\n", ioc->name,
3254 MPT2SAS_INTEL_RMS25JB080_BRANDING);
3255 break;
3256 case MPT2SAS_INTEL_RMS25JB040_SSDID:
3257 pr_info(MPT3SAS_FMT "%s\n", ioc->name,
3258 MPT2SAS_INTEL_RMS25JB040_BRANDING);
3259 break;
3260 case MPT2SAS_INTEL_RMS25KB080_SSDID:
3261 pr_info(MPT3SAS_FMT "%s\n", ioc->name,
3262 MPT2SAS_INTEL_RMS25KB080_BRANDING);
3263 break;
3264 case MPT2SAS_INTEL_RMS25KB040_SSDID:
3265 pr_info(MPT3SAS_FMT "%s\n", ioc->name,
3266 MPT2SAS_INTEL_RMS25KB040_BRANDING);
3267 break;
3268 case MPT2SAS_INTEL_RMS25LB040_SSDID:
3269 pr_info(MPT3SAS_FMT "%s\n", ioc->name,
3270 MPT2SAS_INTEL_RMS25LB040_BRANDING);
3271 break;
3272 case MPT2SAS_INTEL_RMS25LB080_SSDID:
3273 pr_info(MPT3SAS_FMT "%s\n", ioc->name,
3274 MPT2SAS_INTEL_RMS25LB080_BRANDING);
3275 break;
3276 default:
3277 pr_info(MPT3SAS_FMT
3278 "Intel(R) Controller: Subsystem ID: 0x%X\n",
3279 ioc->name, ioc->pdev->subsystem_device);
3280 break;
3281 }
3282 case MPI25_MFGPAGE_DEVID_SAS3008:
3283 switch (ioc->pdev->subsystem_device) {
3284 case MPT3SAS_INTEL_RMS3JC080_SSDID:
3285 pr_info(MPT3SAS_FMT "%s\n", ioc->name,
3286 MPT3SAS_INTEL_RMS3JC080_BRANDING);
3287 break;
Sreekanth Reddy1117b312014-09-12 15:35:30 +05303288
Sreekanth Reddy989e43c2015-11-11 17:30:32 +05303289 case MPT3SAS_INTEL_RS3GC008_SSDID:
3290 pr_info(MPT3SAS_FMT "%s\n", ioc->name,
3291 MPT3SAS_INTEL_RS3GC008_BRANDING);
3292 break;
3293 case MPT3SAS_INTEL_RS3FC044_SSDID:
3294 pr_info(MPT3SAS_FMT "%s\n", ioc->name,
3295 MPT3SAS_INTEL_RS3FC044_BRANDING);
3296 break;
3297 case MPT3SAS_INTEL_RS3UC080_SSDID:
3298 pr_info(MPT3SAS_FMT "%s\n", ioc->name,
3299 MPT3SAS_INTEL_RS3UC080_BRANDING);
3300 break;
3301 default:
3302 pr_info(MPT3SAS_FMT
3303 "Intel(R) Controller: Subsystem ID: 0x%X\n",
3304 ioc->name, ioc->pdev->subsystem_device);
3305 break;
3306 }
Sreekanth Reddy1117b312014-09-12 15:35:30 +05303307 break;
3308 default:
3309 pr_info(MPT3SAS_FMT
Sreekanth Reddy989e43c2015-11-11 17:30:32 +05303310 "Intel(R) Controller: Subsystem ID: 0x%X\n",
Sreekanth Reddyd8eb4a42015-06-30 12:25:02 +05303311 ioc->name, ioc->pdev->subsystem_device);
3312 break;
3313 }
3314 break;
Sreekanth Reddy989e43c2015-11-11 17:30:32 +05303315 case PCI_VENDOR_ID_DELL:
3316 switch (ioc->pdev->device) {
3317 case MPI2_MFGPAGE_DEVID_SAS2008:
3318 switch (ioc->pdev->subsystem_device) {
3319 case MPT2SAS_DELL_6GBPS_SAS_HBA_SSDID:
3320 pr_info(MPT3SAS_FMT "%s\n", ioc->name,
3321 MPT2SAS_DELL_6GBPS_SAS_HBA_BRANDING);
3322 break;
3323 case MPT2SAS_DELL_PERC_H200_ADAPTER_SSDID:
3324 pr_info(MPT3SAS_FMT "%s\n", ioc->name,
3325 MPT2SAS_DELL_PERC_H200_ADAPTER_BRANDING);
3326 break;
3327 case MPT2SAS_DELL_PERC_H200_INTEGRATED_SSDID:
3328 pr_info(MPT3SAS_FMT "%s\n", ioc->name,
3329 MPT2SAS_DELL_PERC_H200_INTEGRATED_BRANDING);
3330 break;
3331 case MPT2SAS_DELL_PERC_H200_MODULAR_SSDID:
3332 pr_info(MPT3SAS_FMT "%s\n", ioc->name,
3333 MPT2SAS_DELL_PERC_H200_MODULAR_BRANDING);
3334 break;
3335 case MPT2SAS_DELL_PERC_H200_EMBEDDED_SSDID:
3336 pr_info(MPT3SAS_FMT "%s\n", ioc->name,
3337 MPT2SAS_DELL_PERC_H200_EMBEDDED_BRANDING);
3338 break;
3339 case MPT2SAS_DELL_PERC_H200_SSDID:
3340 pr_info(MPT3SAS_FMT "%s\n", ioc->name,
3341 MPT2SAS_DELL_PERC_H200_BRANDING);
3342 break;
3343 case MPT2SAS_DELL_6GBPS_SAS_SSDID:
3344 pr_info(MPT3SAS_FMT "%s\n", ioc->name,
3345 MPT2SAS_DELL_6GBPS_SAS_BRANDING);
3346 break;
3347 default:
3348 pr_info(MPT3SAS_FMT
3349 "Dell 6Gbps HBA: Subsystem ID: 0x%X\n",
3350 ioc->name, ioc->pdev->subsystem_device);
3351 break;
3352 }
3353 break;
3354 case MPI25_MFGPAGE_DEVID_SAS3008:
3355 switch (ioc->pdev->subsystem_device) {
3356 case MPT3SAS_DELL_12G_HBA_SSDID:
3357 pr_info(MPT3SAS_FMT "%s\n", ioc->name,
3358 MPT3SAS_DELL_12G_HBA_BRANDING);
3359 break;
3360 default:
3361 pr_info(MPT3SAS_FMT
3362 "Dell 12Gbps HBA: Subsystem ID: 0x%X\n",
3363 ioc->name, ioc->pdev->subsystem_device);
3364 break;
3365 }
3366 break;
3367 default:
3368 pr_info(MPT3SAS_FMT
3369 "Dell HBA: Subsystem ID: 0x%X\n", ioc->name,
3370 ioc->pdev->subsystem_device);
3371 break;
3372 }
3373 break;
3374 case PCI_VENDOR_ID_CISCO:
3375 switch (ioc->pdev->device) {
3376 case MPI25_MFGPAGE_DEVID_SAS3008:
3377 switch (ioc->pdev->subsystem_device) {
3378 case MPT3SAS_CISCO_12G_8E_HBA_SSDID:
3379 pr_info(MPT3SAS_FMT "%s\n", ioc->name,
3380 MPT3SAS_CISCO_12G_8E_HBA_BRANDING);
3381 break;
3382 case MPT3SAS_CISCO_12G_8I_HBA_SSDID:
3383 pr_info(MPT3SAS_FMT "%s\n", ioc->name,
3384 MPT3SAS_CISCO_12G_8I_HBA_BRANDING);
3385 break;
3386 case MPT3SAS_CISCO_12G_AVILA_HBA_SSDID:
3387 pr_info(MPT3SAS_FMT "%s\n", ioc->name,
3388 MPT3SAS_CISCO_12G_AVILA_HBA_BRANDING);
3389 break;
3390 default:
3391 pr_info(MPT3SAS_FMT
3392 "Cisco 12Gbps SAS HBA: Subsystem ID: 0x%X\n",
3393 ioc->name, ioc->pdev->subsystem_device);
3394 break;
3395 }
3396 break;
3397 case MPI25_MFGPAGE_DEVID_SAS3108_1:
3398 switch (ioc->pdev->subsystem_device) {
3399 case MPT3SAS_CISCO_12G_AVILA_HBA_SSDID:
3400 pr_info(MPT3SAS_FMT "%s\n", ioc->name,
3401 MPT3SAS_CISCO_12G_AVILA_HBA_BRANDING);
3402 break;
3403 case MPT3SAS_CISCO_12G_COLUSA_MEZZANINE_HBA_SSDID:
3404 pr_info(MPT3SAS_FMT "%s\n", ioc->name,
3405 MPT3SAS_CISCO_12G_COLUSA_MEZZANINE_HBA_BRANDING
3406 );
3407 break;
3408 default:
3409 pr_info(MPT3SAS_FMT
3410 "Cisco 12Gbps SAS HBA: Subsystem ID: 0x%X\n",
3411 ioc->name, ioc->pdev->subsystem_device);
3412 break;
3413 }
3414 break;
3415 default:
3416 pr_info(MPT3SAS_FMT
3417 "Cisco SAS HBA: Subsystem ID: 0x%X\n",
3418 ioc->name, ioc->pdev->subsystem_device);
3419 break;
3420 }
3421 break;
3422 case MPT2SAS_HP_3PAR_SSVID:
3423 switch (ioc->pdev->device) {
3424 case MPI2_MFGPAGE_DEVID_SAS2004:
3425 switch (ioc->pdev->subsystem_device) {
3426 case MPT2SAS_HP_DAUGHTER_2_4_INTERNAL_SSDID:
3427 pr_info(MPT3SAS_FMT "%s\n", ioc->name,
3428 MPT2SAS_HP_DAUGHTER_2_4_INTERNAL_BRANDING);
3429 break;
3430 default:
3431 pr_info(MPT3SAS_FMT
3432 "HP 6Gbps SAS HBA: Subsystem ID: 0x%X\n",
3433 ioc->name, ioc->pdev->subsystem_device);
3434 break;
3435 }
3436 case MPI2_MFGPAGE_DEVID_SAS2308_2:
3437 switch (ioc->pdev->subsystem_device) {
3438 case MPT2SAS_HP_2_4_INTERNAL_SSDID:
3439 pr_info(MPT3SAS_FMT "%s\n", ioc->name,
3440 MPT2SAS_HP_2_4_INTERNAL_BRANDING);
3441 break;
3442 case MPT2SAS_HP_2_4_EXTERNAL_SSDID:
3443 pr_info(MPT3SAS_FMT "%s\n", ioc->name,
3444 MPT2SAS_HP_2_4_EXTERNAL_BRANDING);
3445 break;
3446 case MPT2SAS_HP_1_4_INTERNAL_1_4_EXTERNAL_SSDID:
3447 pr_info(MPT3SAS_FMT "%s\n", ioc->name,
3448 MPT2SAS_HP_1_4_INTERNAL_1_4_EXTERNAL_BRANDING);
3449 break;
3450 case MPT2SAS_HP_EMBEDDED_2_4_INTERNAL_SSDID:
3451 pr_info(MPT3SAS_FMT "%s\n", ioc->name,
3452 MPT2SAS_HP_EMBEDDED_2_4_INTERNAL_BRANDING);
3453 break;
3454 default:
3455 pr_info(MPT3SAS_FMT
3456 "HP 6Gbps SAS HBA: Subsystem ID: 0x%X\n",
3457 ioc->name, ioc->pdev->subsystem_device);
3458 break;
3459 }
3460 default:
3461 pr_info(MPT3SAS_FMT
3462 "HP SAS HBA: Subsystem ID: 0x%X\n",
3463 ioc->name, ioc->pdev->subsystem_device);
3464 break;
3465 }
Sreekanth Reddy38e41412015-06-30 12:24:57 +05303466 default:
Sreekanth Reddy38e41412015-06-30 12:24:57 +05303467 break;
3468 }
3469}
Sreekanth Reddyfb84dfc2015-06-30 12:24:56 +05303470
3471/**
Sreekanth Reddyf92363d2012-11-30 07:44:21 +05303472 * _base_display_ioc_capabilities - Disply IOC's capabilities.
3473 * @ioc: per adapter object
3474 *
3475 * Return nothing.
3476 */
3477static void
3478_base_display_ioc_capabilities(struct MPT3SAS_ADAPTER *ioc)
3479{
3480 int i = 0;
3481 char desc[16];
3482 u32 iounit_pg1_flags;
3483 u32 bios_version;
3484
3485 bios_version = le32_to_cpu(ioc->bios_pg3.BiosVersion);
3486 strncpy(desc, ioc->manu_pg0.ChipName, 16);
3487 pr_info(MPT3SAS_FMT "%s: FWVersion(%02d.%02d.%02d.%02d), "\
3488 "ChipRevision(0x%02x), BiosVersion(%02d.%02d.%02d.%02d)\n",
3489 ioc->name, desc,
3490 (ioc->facts.FWVersion.Word & 0xFF000000) >> 24,
3491 (ioc->facts.FWVersion.Word & 0x00FF0000) >> 16,
3492 (ioc->facts.FWVersion.Word & 0x0000FF00) >> 8,
3493 ioc->facts.FWVersion.Word & 0x000000FF,
3494 ioc->pdev->revision,
3495 (bios_version & 0xFF000000) >> 24,
3496 (bios_version & 0x00FF0000) >> 16,
3497 (bios_version & 0x0000FF00) >> 8,
3498 bios_version & 0x000000FF);
3499
Sreekanth Reddy989e43c2015-11-11 17:30:32 +05303500 _base_display_OEMs_branding(ioc);
Sreekanth Reddy1117b312014-09-12 15:35:30 +05303501
Suganath Prabu Subramani016d5c32017-10-31 18:02:28 +05303502 if (ioc->facts.ProtocolFlags & MPI2_IOCFACTS_PROTOCOL_NVME_DEVICES) {
3503 pr_info("%sNVMe", i ? "," : "");
3504 i++;
3505 }
3506
Sreekanth Reddyf92363d2012-11-30 07:44:21 +05303507 pr_info(MPT3SAS_FMT "Protocol=(", ioc->name);
3508
3509 if (ioc->facts.ProtocolFlags & MPI2_IOCFACTS_PROTOCOL_SCSI_INITIATOR) {
3510 pr_info("Initiator");
3511 i++;
3512 }
3513
3514 if (ioc->facts.ProtocolFlags & MPI2_IOCFACTS_PROTOCOL_SCSI_TARGET) {
3515 pr_info("%sTarget", i ? "," : "");
3516 i++;
3517 }
3518
3519 i = 0;
3520 pr_info("), ");
3521 pr_info("Capabilities=(");
3522
Sreekanth Reddy7786ab62015-11-11 17:30:28 +05303523 if (!ioc->hide_ir_msg) {
3524 if (ioc->facts.IOCCapabilities &
Sreekanth Reddyf92363d2012-11-30 07:44:21 +05303525 MPI2_IOCFACTS_CAPABILITY_INTEGRATED_RAID) {
3526 pr_info("Raid");
3527 i++;
Sreekanth Reddy7786ab62015-11-11 17:30:28 +05303528 }
Sreekanth Reddyf92363d2012-11-30 07:44:21 +05303529 }
3530
3531 if (ioc->facts.IOCCapabilities & MPI2_IOCFACTS_CAPABILITY_TLR) {
3532 pr_info("%sTLR", i ? "," : "");
3533 i++;
3534 }
3535
3536 if (ioc->facts.IOCCapabilities & MPI2_IOCFACTS_CAPABILITY_MULTICAST) {
3537 pr_info("%sMulticast", i ? "," : "");
3538 i++;
3539 }
3540
3541 if (ioc->facts.IOCCapabilities &
3542 MPI2_IOCFACTS_CAPABILITY_BIDIRECTIONAL_TARGET) {
3543 pr_info("%sBIDI Target", i ? "," : "");
3544 i++;
3545 }
3546
3547 if (ioc->facts.IOCCapabilities & MPI2_IOCFACTS_CAPABILITY_EEDP) {
3548 pr_info("%sEEDP", i ? "," : "");
3549 i++;
3550 }
3551
3552 if (ioc->facts.IOCCapabilities &
3553 MPI2_IOCFACTS_CAPABILITY_SNAPSHOT_BUFFER) {
3554 pr_info("%sSnapshot Buffer", i ? "," : "");
3555 i++;
3556 }
3557
3558 if (ioc->facts.IOCCapabilities &
3559 MPI2_IOCFACTS_CAPABILITY_DIAG_TRACE_BUFFER) {
3560 pr_info("%sDiag Trace Buffer", i ? "," : "");
3561 i++;
3562 }
3563
3564 if (ioc->facts.IOCCapabilities &
3565 MPI2_IOCFACTS_CAPABILITY_EXTENDED_BUFFER) {
3566 pr_info("%sDiag Extended Buffer", i ? "," : "");
3567 i++;
3568 }
3569
3570 if (ioc->facts.IOCCapabilities &
3571 MPI2_IOCFACTS_CAPABILITY_TASK_SET_FULL_HANDLING) {
3572 pr_info("%sTask Set Full", i ? "," : "");
3573 i++;
3574 }
3575
3576 iounit_pg1_flags = le32_to_cpu(ioc->iounit_pg1.Flags);
3577 if (!(iounit_pg1_flags & MPI2_IOUNITPAGE1_NATIVE_COMMAND_Q_DISABLE)) {
3578 pr_info("%sNCQ", i ? "," : "");
3579 i++;
3580 }
3581
3582 pr_info(")\n");
3583}
3584
3585/**
3586 * mpt3sas_base_update_missing_delay - change the missing delay timers
3587 * @ioc: per adapter object
3588 * @device_missing_delay: amount of time till device is reported missing
3589 * @io_missing_delay: interval IO is returned when there is a missing device
3590 *
3591 * Return nothing.
3592 *
3593 * Passed on the command line, this function will modify the device missing
3594 * delay, as well as the io missing delay. This should be called at driver
3595 * load time.
3596 */
3597void
3598mpt3sas_base_update_missing_delay(struct MPT3SAS_ADAPTER *ioc,
3599 u16 device_missing_delay, u8 io_missing_delay)
3600{
3601 u16 dmd, dmd_new, dmd_orignal;
3602 u8 io_missing_delay_original;
3603 u16 sz;
3604 Mpi2SasIOUnitPage1_t *sas_iounit_pg1 = NULL;
3605 Mpi2ConfigReply_t mpi_reply;
3606 u8 num_phys = 0;
3607 u16 ioc_status;
3608
3609 mpt3sas_config_get_number_hba_phys(ioc, &num_phys);
3610 if (!num_phys)
3611 return;
3612
3613 sz = offsetof(Mpi2SasIOUnitPage1_t, PhyData) + (num_phys *
3614 sizeof(Mpi2SasIOUnit1PhyData_t));
3615 sas_iounit_pg1 = kzalloc(sz, GFP_KERNEL);
3616 if (!sas_iounit_pg1) {
3617 pr_err(MPT3SAS_FMT "failure at %s:%d/%s()!\n",
3618 ioc->name, __FILE__, __LINE__, __func__);
3619 goto out;
3620 }
3621 if ((mpt3sas_config_get_sas_iounit_pg1(ioc, &mpi_reply,
3622 sas_iounit_pg1, sz))) {
3623 pr_err(MPT3SAS_FMT "failure at %s:%d/%s()!\n",
3624 ioc->name, __FILE__, __LINE__, __func__);
3625 goto out;
3626 }
3627 ioc_status = le16_to_cpu(mpi_reply.IOCStatus) &
3628 MPI2_IOCSTATUS_MASK;
3629 if (ioc_status != MPI2_IOCSTATUS_SUCCESS) {
3630 pr_err(MPT3SAS_FMT "failure at %s:%d/%s()!\n",
3631 ioc->name, __FILE__, __LINE__, __func__);
3632 goto out;
3633 }
3634
3635 /* device missing delay */
3636 dmd = sas_iounit_pg1->ReportDeviceMissingDelay;
3637 if (dmd & MPI2_SASIOUNIT1_REPORT_MISSING_UNIT_16)
3638 dmd = (dmd & MPI2_SASIOUNIT1_REPORT_MISSING_TIMEOUT_MASK) * 16;
3639 else
3640 dmd = dmd & MPI2_SASIOUNIT1_REPORT_MISSING_TIMEOUT_MASK;
3641 dmd_orignal = dmd;
3642 if (device_missing_delay > 0x7F) {
3643 dmd = (device_missing_delay > 0x7F0) ? 0x7F0 :
3644 device_missing_delay;
3645 dmd = dmd / 16;
3646 dmd |= MPI2_SASIOUNIT1_REPORT_MISSING_UNIT_16;
3647 } else
3648 dmd = device_missing_delay;
3649 sas_iounit_pg1->ReportDeviceMissingDelay = dmd;
3650
3651 /* io missing delay */
3652 io_missing_delay_original = sas_iounit_pg1->IODeviceMissingDelay;
3653 sas_iounit_pg1->IODeviceMissingDelay = io_missing_delay;
3654
3655 if (!mpt3sas_config_set_sas_iounit_pg1(ioc, &mpi_reply, sas_iounit_pg1,
3656 sz)) {
3657 if (dmd & MPI2_SASIOUNIT1_REPORT_MISSING_UNIT_16)
3658 dmd_new = (dmd &
3659 MPI2_SASIOUNIT1_REPORT_MISSING_TIMEOUT_MASK) * 16;
3660 else
3661 dmd_new =
3662 dmd & MPI2_SASIOUNIT1_REPORT_MISSING_TIMEOUT_MASK;
3663 pr_info(MPT3SAS_FMT "device_missing_delay: old(%d), new(%d)\n",
3664 ioc->name, dmd_orignal, dmd_new);
3665 pr_info(MPT3SAS_FMT "ioc_missing_delay: old(%d), new(%d)\n",
3666 ioc->name, io_missing_delay_original,
3667 io_missing_delay);
3668 ioc->device_missing_delay = dmd_new;
3669 ioc->io_missing_delay = io_missing_delay;
3670 }
3671
3672out:
3673 kfree(sas_iounit_pg1);
3674}
3675/**
3676 * _base_static_config_pages - static start of day config pages
3677 * @ioc: per adapter object
3678 *
3679 * Return nothing.
3680 */
3681static void
3682_base_static_config_pages(struct MPT3SAS_ADAPTER *ioc)
3683{
3684 Mpi2ConfigReply_t mpi_reply;
3685 u32 iounit_pg1_flags;
3686
3687 mpt3sas_config_get_manufacturing_pg0(ioc, &mpi_reply, &ioc->manu_pg0);
3688 if (ioc->ir_firmware)
3689 mpt3sas_config_get_manufacturing_pg10(ioc, &mpi_reply,
3690 &ioc->manu_pg10);
3691
3692 /*
3693 * Ensure correct T10 PI operation if vendor left EEDPTagMode
3694 * flag unset in NVDATA.
3695 */
3696 mpt3sas_config_get_manufacturing_pg11(ioc, &mpi_reply, &ioc->manu_pg11);
3697 if (ioc->manu_pg11.EEDPTagMode == 0) {
3698 pr_err("%s: overriding NVDATA EEDPTagMode setting\n",
3699 ioc->name);
3700 ioc->manu_pg11.EEDPTagMode &= ~0x3;
3701 ioc->manu_pg11.EEDPTagMode |= 0x1;
3702 mpt3sas_config_set_manufacturing_pg11(ioc, &mpi_reply,
3703 &ioc->manu_pg11);
3704 }
3705
3706 mpt3sas_config_get_bios_pg2(ioc, &mpi_reply, &ioc->bios_pg2);
3707 mpt3sas_config_get_bios_pg3(ioc, &mpi_reply, &ioc->bios_pg3);
3708 mpt3sas_config_get_ioc_pg8(ioc, &mpi_reply, &ioc->ioc_pg8);
3709 mpt3sas_config_get_iounit_pg0(ioc, &mpi_reply, &ioc->iounit_pg0);
3710 mpt3sas_config_get_iounit_pg1(ioc, &mpi_reply, &ioc->iounit_pg1);
Sreekanth Reddy2d8ce8c2015-01-12 11:38:56 +05303711 mpt3sas_config_get_iounit_pg8(ioc, &mpi_reply, &ioc->iounit_pg8);
Sreekanth Reddyf92363d2012-11-30 07:44:21 +05303712 _base_display_ioc_capabilities(ioc);
3713
3714 /*
3715 * Enable task_set_full handling in iounit_pg1 when the
3716 * facts capabilities indicate that its supported.
3717 */
3718 iounit_pg1_flags = le32_to_cpu(ioc->iounit_pg1.Flags);
3719 if ((ioc->facts.IOCCapabilities &
3720 MPI2_IOCFACTS_CAPABILITY_TASK_SET_FULL_HANDLING))
3721 iounit_pg1_flags &=
3722 ~MPI2_IOUNITPAGE1_DISABLE_TASK_SET_FULL_HANDLING;
3723 else
3724 iounit_pg1_flags |=
3725 MPI2_IOUNITPAGE1_DISABLE_TASK_SET_FULL_HANDLING;
3726 ioc->iounit_pg1.Flags = cpu_to_le32(iounit_pg1_flags);
3727 mpt3sas_config_set_iounit_pg1(ioc, &mpi_reply, &ioc->iounit_pg1);
Sreekanth Reddy2d8ce8c2015-01-12 11:38:56 +05303728
3729 if (ioc->iounit_pg8.NumSensors)
3730 ioc->temp_sensors_count = ioc->iounit_pg8.NumSensors;
Sreekanth Reddyf92363d2012-11-30 07:44:21 +05303731}
3732
3733/**
3734 * _base_release_memory_pools - release memory
3735 * @ioc: per adapter object
3736 *
3737 * Free memory allocated from _base_allocate_memory_pools.
3738 *
3739 * Return nothing.
3740 */
3741static void
3742_base_release_memory_pools(struct MPT3SAS_ADAPTER *ioc)
3743{
Sreekanth Reddy9b05c912014-09-12 15:35:31 +05303744 int i = 0;
3745 struct reply_post_struct *rps;
Sreekanth Reddyf92363d2012-11-30 07:44:21 +05303746
3747 dexitprintk(ioc, pr_info(MPT3SAS_FMT "%s\n", ioc->name,
3748 __func__));
3749
3750 if (ioc->request) {
3751 pci_free_consistent(ioc->pdev, ioc->request_dma_sz,
3752 ioc->request, ioc->request_dma);
3753 dexitprintk(ioc, pr_info(MPT3SAS_FMT
3754 "request_pool(0x%p): free\n",
3755 ioc->name, ioc->request));
3756 ioc->request = NULL;
3757 }
3758
3759 if (ioc->sense) {
Romain Periere9d98412017-07-06 10:13:07 +02003760 dma_pool_free(ioc->sense_dma_pool, ioc->sense, ioc->sense_dma);
3761 dma_pool_destroy(ioc->sense_dma_pool);
Sreekanth Reddyf92363d2012-11-30 07:44:21 +05303762 dexitprintk(ioc, pr_info(MPT3SAS_FMT
3763 "sense_pool(0x%p): free\n",
3764 ioc->name, ioc->sense));
3765 ioc->sense = NULL;
3766 }
3767
3768 if (ioc->reply) {
Romain Periere9d98412017-07-06 10:13:07 +02003769 dma_pool_free(ioc->reply_dma_pool, ioc->reply, ioc->reply_dma);
3770 dma_pool_destroy(ioc->reply_dma_pool);
Sreekanth Reddyf92363d2012-11-30 07:44:21 +05303771 dexitprintk(ioc, pr_info(MPT3SAS_FMT
3772 "reply_pool(0x%p): free\n",
3773 ioc->name, ioc->reply));
3774 ioc->reply = NULL;
3775 }
3776
3777 if (ioc->reply_free) {
Romain Periere9d98412017-07-06 10:13:07 +02003778 dma_pool_free(ioc->reply_free_dma_pool, ioc->reply_free,
Sreekanth Reddyf92363d2012-11-30 07:44:21 +05303779 ioc->reply_free_dma);
Romain Periere9d98412017-07-06 10:13:07 +02003780 dma_pool_destroy(ioc->reply_free_dma_pool);
Sreekanth Reddyf92363d2012-11-30 07:44:21 +05303781 dexitprintk(ioc, pr_info(MPT3SAS_FMT
3782 "reply_free_pool(0x%p): free\n",
3783 ioc->name, ioc->reply_free));
3784 ioc->reply_free = NULL;
3785 }
3786
Sreekanth Reddy9b05c912014-09-12 15:35:31 +05303787 if (ioc->reply_post) {
3788 do {
3789 rps = &ioc->reply_post[i];
3790 if (rps->reply_post_free) {
Romain Periere9d98412017-07-06 10:13:07 +02003791 dma_pool_free(
Sreekanth Reddy9b05c912014-09-12 15:35:31 +05303792 ioc->reply_post_free_dma_pool,
3793 rps->reply_post_free,
3794 rps->reply_post_free_dma);
3795 dexitprintk(ioc, pr_info(MPT3SAS_FMT
3796 "reply_post_free_pool(0x%p): free\n",
3797 ioc->name, rps->reply_post_free));
3798 rps->reply_post_free = NULL;
3799 }
3800 } while (ioc->rdpq_array_enable &&
3801 (++i < ioc->reply_queue_count));
3802
Romain Periere9d98412017-07-06 10:13:07 +02003803 dma_pool_destroy(ioc->reply_post_free_dma_pool);
Sreekanth Reddy9b05c912014-09-12 15:35:31 +05303804 kfree(ioc->reply_post);
Sreekanth Reddyf92363d2012-11-30 07:44:21 +05303805 }
3806
Suganath Prabu Subramani016d5c32017-10-31 18:02:28 +05303807 if (ioc->pcie_sgl_dma_pool) {
3808 for (i = 0; i < ioc->scsiio_depth; i++) {
3809 if (ioc->scsi_lookup[i].pcie_sg_list.pcie_sgl)
Romain Perier13a06402017-11-20 20:32:46 +01003810 dma_pool_free(ioc->pcie_sgl_dma_pool,
Suganath Prabu Subramani016d5c32017-10-31 18:02:28 +05303811 ioc->scsi_lookup[i].pcie_sg_list.pcie_sgl,
3812 ioc->scsi_lookup[i].pcie_sg_list.pcie_sgl_dma);
3813 }
3814 if (ioc->pcie_sgl_dma_pool)
Romain Perier13a06402017-11-20 20:32:46 +01003815 dma_pool_destroy(ioc->pcie_sgl_dma_pool);
Suganath Prabu Subramani016d5c32017-10-31 18:02:28 +05303816 }
3817
Sreekanth Reddyf92363d2012-11-30 07:44:21 +05303818 if (ioc->config_page) {
3819 dexitprintk(ioc, pr_info(MPT3SAS_FMT
3820 "config_page(0x%p): free\n", ioc->name,
3821 ioc->config_page));
3822 pci_free_consistent(ioc->pdev, ioc->config_page_sz,
3823 ioc->config_page, ioc->config_page_dma);
3824 }
3825
3826 if (ioc->scsi_lookup) {
3827 free_pages((ulong)ioc->scsi_lookup, ioc->scsi_lookup_pages);
3828 ioc->scsi_lookup = NULL;
3829 }
3830 kfree(ioc->hpr_lookup);
3831 kfree(ioc->internal_lookup);
3832 if (ioc->chain_lookup) {
3833 for (i = 0; i < ioc->chain_depth; i++) {
3834 if (ioc->chain_lookup[i].chain_buffer)
Romain Periere9d98412017-07-06 10:13:07 +02003835 dma_pool_free(ioc->chain_dma_pool,
Sreekanth Reddyf92363d2012-11-30 07:44:21 +05303836 ioc->chain_lookup[i].chain_buffer,
3837 ioc->chain_lookup[i].chain_buffer_dma);
3838 }
Romain Periere9d98412017-07-06 10:13:07 +02003839 dma_pool_destroy(ioc->chain_dma_pool);
Sreekanth Reddyf92363d2012-11-30 07:44:21 +05303840 free_pages((ulong)ioc->chain_lookup, ioc->chain_pages);
3841 ioc->chain_lookup = NULL;
3842 }
3843}
3844
3845/**
3846 * _base_allocate_memory_pools - allocate start of day memory pools
3847 * @ioc: per adapter object
Sreekanth Reddyf92363d2012-11-30 07:44:21 +05303848 *
3849 * Returns 0 success, anything else error
3850 */
3851static int
Calvin Owens98c56ad2016-07-28 21:38:21 -07003852_base_allocate_memory_pools(struct MPT3SAS_ADAPTER *ioc)
Sreekanth Reddyf92363d2012-11-30 07:44:21 +05303853{
3854 struct mpt3sas_facts *facts;
3855 u16 max_sge_elements;
3856 u16 chains_needed_per_io;
3857 u32 sz, total_sz, reply_post_free_sz;
3858 u32 retry_sz;
Suganath Prabu Subramani016d5c32017-10-31 18:02:28 +05303859 u16 max_request_credit, nvme_blocks_needed;
Sreekanth Reddyf92363d2012-11-30 07:44:21 +05303860 unsigned short sg_tablesize;
3861 u16 sge_size;
3862 int i;
3863
3864 dinitprintk(ioc, pr_info(MPT3SAS_FMT "%s\n", ioc->name,
3865 __func__));
3866
3867
3868 retry_sz = 0;
3869 facts = &ioc->facts;
3870
3871 /* command line tunables for max sgl entries */
3872 if (max_sgl_entries != -1)
3873 sg_tablesize = max_sgl_entries;
Sreekanth Reddy471ef9d2015-11-11 17:30:24 +05303874 else {
3875 if (ioc->hba_mpi_version_belonged == MPI2_VERSION)
3876 sg_tablesize = MPT2SAS_SG_DEPTH;
3877 else
3878 sg_tablesize = MPT3SAS_SG_DEPTH;
3879 }
Sreekanth Reddyf92363d2012-11-30 07:44:21 +05303880
Sreekanth Reddy06f5f972017-10-10 18:41:16 +05303881 /* max sgl entries <= MPT_KDUMP_MIN_PHYS_SEGMENTS in KDUMP mode */
3882 if (reset_devices)
3883 sg_tablesize = min_t(unsigned short, sg_tablesize,
3884 MPT_KDUMP_MIN_PHYS_SEGMENTS);
3885
Sreekanth Reddy8a7e4c22015-11-11 17:30:18 +05303886 if (sg_tablesize < MPT_MIN_PHYS_SEGMENTS)
3887 sg_tablesize = MPT_MIN_PHYS_SEGMENTS;
3888 else if (sg_tablesize > MPT_MAX_PHYS_SEGMENTS) {
Sreekanth Reddyad666a02015-01-12 11:39:00 +05303889 sg_tablesize = min_t(unsigned short, sg_tablesize,
Ming Lin65e86172016-04-04 14:48:10 -07003890 SG_MAX_SEGMENTS);
Sreekanth Reddyad666a02015-01-12 11:39:00 +05303891 pr_warn(MPT3SAS_FMT
3892 "sg_tablesize(%u) is bigger than kernel"
Ming Lin65e86172016-04-04 14:48:10 -07003893 " defined SG_CHUNK_SIZE(%u)\n", ioc->name,
Sreekanth Reddy8a7e4c22015-11-11 17:30:18 +05303894 sg_tablesize, MPT_MAX_PHYS_SEGMENTS);
Sreekanth Reddyad666a02015-01-12 11:39:00 +05303895 }
Sreekanth Reddyf92363d2012-11-30 07:44:21 +05303896 ioc->shost->sg_tablesize = sg_tablesize;
3897
Suganath prabu Subramanifd0331b2016-01-28 12:07:02 +05303898 ioc->internal_depth = min_t(int, (facts->HighPriorityCredit + (5)),
3899 (facts->RequestCredit / 4));
3900 if (ioc->internal_depth < INTERNAL_CMDS_COUNT) {
3901 if (facts->RequestCredit <= (INTERNAL_CMDS_COUNT +
3902 INTERNAL_SCSIIO_CMDS_COUNT)) {
3903 pr_err(MPT3SAS_FMT "IOC doesn't have enough Request \
3904 Credits, it has just %d number of credits\n",
3905 ioc->name, facts->RequestCredit);
3906 return -ENOMEM;
3907 }
3908 ioc->internal_depth = 10;
3909 }
3910
3911 ioc->hi_priority_depth = ioc->internal_depth - (5);
Sreekanth Reddyf92363d2012-11-30 07:44:21 +05303912 /* command line tunables for max controller queue depth */
3913 if (max_queue_depth != -1 && max_queue_depth != 0) {
3914 max_request_credit = min_t(u16, max_queue_depth +
Suganath prabu Subramanifd0331b2016-01-28 12:07:02 +05303915 ioc->internal_depth, facts->RequestCredit);
Sreekanth Reddyf92363d2012-11-30 07:44:21 +05303916 if (max_request_credit > MAX_HBA_QUEUE_DEPTH)
3917 max_request_credit = MAX_HBA_QUEUE_DEPTH;
Sreekanth Reddy06f5f972017-10-10 18:41:16 +05303918 } else if (reset_devices)
3919 max_request_credit = min_t(u16, facts->RequestCredit,
3920 (MPT3SAS_KDUMP_SCSI_IO_DEPTH + ioc->internal_depth));
3921 else
Sreekanth Reddyf92363d2012-11-30 07:44:21 +05303922 max_request_credit = min_t(u16, facts->RequestCredit,
3923 MAX_HBA_QUEUE_DEPTH);
3924
Suganath prabu Subramanifd0331b2016-01-28 12:07:02 +05303925 /* Firmware maintains additional facts->HighPriorityCredit number of
3926 * credits for HiPriprity Request messages, so hba queue depth will be
3927 * sum of max_request_credit and high priority queue depth.
3928 */
3929 ioc->hba_queue_depth = max_request_credit + ioc->hi_priority_depth;
Sreekanth Reddyf92363d2012-11-30 07:44:21 +05303930
3931 /* request frame size */
3932 ioc->request_sz = facts->IOCRequestFrameSize * 4;
3933
3934 /* reply frame size */
3935 ioc->reply_sz = facts->ReplyFrameSize * 4;
3936
Suganath prabu Subramaniebb30242016-01-28 12:07:04 +05303937 /* chain segment size */
3938 if (ioc->hba_mpi_version_belonged != MPI2_VERSION) {
3939 if (facts->IOCMaxChainSegmentSize)
3940 ioc->chain_segment_sz =
3941 facts->IOCMaxChainSegmentSize *
3942 MAX_CHAIN_ELEMT_SZ;
3943 else
3944 /* set to 128 bytes size if IOCMaxChainSegmentSize is zero */
3945 ioc->chain_segment_sz = DEFAULT_NUM_FWCHAIN_ELEMTS *
3946 MAX_CHAIN_ELEMT_SZ;
3947 } else
3948 ioc->chain_segment_sz = ioc->request_sz;
3949
Sreekanth Reddyf92363d2012-11-30 07:44:21 +05303950 /* calculate the max scatter element size */
3951 sge_size = max_t(u16, ioc->sge_size, ioc->sge_size_ieee);
3952
3953 retry_allocation:
3954 total_sz = 0;
3955 /* calculate number of sg elements left over in the 1st frame */
3956 max_sge_elements = ioc->request_sz - ((sizeof(Mpi2SCSIIORequest_t) -
3957 sizeof(Mpi2SGEIOUnion_t)) + sge_size);
3958 ioc->max_sges_in_main_message = max_sge_elements/sge_size;
3959
3960 /* now do the same for a chain buffer */
Suganath prabu Subramaniebb30242016-01-28 12:07:04 +05303961 max_sge_elements = ioc->chain_segment_sz - sge_size;
Sreekanth Reddyf92363d2012-11-30 07:44:21 +05303962 ioc->max_sges_in_chain_message = max_sge_elements/sge_size;
3963
3964 /*
3965 * MPT3SAS_SG_DEPTH = CONFIG_FUSION_MAX_SGE
3966 */
3967 chains_needed_per_io = ((ioc->shost->sg_tablesize -
3968 ioc->max_sges_in_main_message)/ioc->max_sges_in_chain_message)
3969 + 1;
3970 if (chains_needed_per_io > facts->MaxChainDepth) {
3971 chains_needed_per_io = facts->MaxChainDepth;
3972 ioc->shost->sg_tablesize = min_t(u16,
3973 ioc->max_sges_in_main_message + (ioc->max_sges_in_chain_message
3974 * chains_needed_per_io), ioc->shost->sg_tablesize);
3975 }
3976 ioc->chains_needed_per_io = chains_needed_per_io;
3977
3978 /* reply free queue sizing - taking into account for 64 FW events */
3979 ioc->reply_free_queue_depth = ioc->hba_queue_depth + 64;
3980
3981 /* calculate reply descriptor post queue depth */
3982 ioc->reply_post_queue_depth = ioc->hba_queue_depth +
3983 ioc->reply_free_queue_depth + 1 ;
3984 /* align the reply post queue on the next 16 count boundary */
3985 if (ioc->reply_post_queue_depth % 16)
3986 ioc->reply_post_queue_depth += 16 -
3987 (ioc->reply_post_queue_depth % 16);
3988
Sreekanth Reddyf92363d2012-11-30 07:44:21 +05303989 if (ioc->reply_post_queue_depth >
3990 facts->MaxReplyDescriptorPostQueueDepth) {
3991 ioc->reply_post_queue_depth =
3992 facts->MaxReplyDescriptorPostQueueDepth -
3993 (facts->MaxReplyDescriptorPostQueueDepth % 16);
3994 ioc->hba_queue_depth =
3995 ((ioc->reply_post_queue_depth - 64) / 2) - 1;
3996 ioc->reply_free_queue_depth = ioc->hba_queue_depth + 64;
3997 }
3998
3999 dinitprintk(ioc, pr_info(MPT3SAS_FMT "scatter gather: " \
4000 "sge_in_main_msg(%d), sge_per_chain(%d), sge_per_io(%d), "
4001 "chains_per_io(%d)\n", ioc->name, ioc->max_sges_in_main_message,
4002 ioc->max_sges_in_chain_message, ioc->shost->sg_tablesize,
4003 ioc->chains_needed_per_io));
4004
Sreekanth Reddy9b05c912014-09-12 15:35:31 +05304005 /* reply post queue, 16 byte align */
4006 reply_post_free_sz = ioc->reply_post_queue_depth *
4007 sizeof(Mpi2DefaultReplyDescriptor_t);
4008
4009 sz = reply_post_free_sz;
4010 if (_base_is_controller_msix_enabled(ioc) && !ioc->rdpq_array_enable)
4011 sz *= ioc->reply_queue_count;
4012
4013 ioc->reply_post = kcalloc((ioc->rdpq_array_enable) ?
4014 (ioc->reply_queue_count):1,
4015 sizeof(struct reply_post_struct), GFP_KERNEL);
4016
4017 if (!ioc->reply_post) {
4018 pr_err(MPT3SAS_FMT "reply_post_free pool: kcalloc failed\n",
4019 ioc->name);
4020 goto out;
4021 }
Romain Periere9d98412017-07-06 10:13:07 +02004022 ioc->reply_post_free_dma_pool = dma_pool_create("reply_post_free pool",
4023 &ioc->pdev->dev, sz, 16, 0);
Sreekanth Reddy9b05c912014-09-12 15:35:31 +05304024 if (!ioc->reply_post_free_dma_pool) {
4025 pr_err(MPT3SAS_FMT
Romain Periere9d98412017-07-06 10:13:07 +02004026 "reply_post_free pool: dma_pool_create failed\n",
Sreekanth Reddy9b05c912014-09-12 15:35:31 +05304027 ioc->name);
4028 goto out;
4029 }
4030 i = 0;
4031 do {
4032 ioc->reply_post[i].reply_post_free =
Romain Periere9d98412017-07-06 10:13:07 +02004033 dma_pool_alloc(ioc->reply_post_free_dma_pool,
Sreekanth Reddy9b05c912014-09-12 15:35:31 +05304034 GFP_KERNEL,
4035 &ioc->reply_post[i].reply_post_free_dma);
4036 if (!ioc->reply_post[i].reply_post_free) {
4037 pr_err(MPT3SAS_FMT
Romain Periere9d98412017-07-06 10:13:07 +02004038 "reply_post_free pool: dma_pool_alloc failed\n",
Sreekanth Reddy9b05c912014-09-12 15:35:31 +05304039 ioc->name);
4040 goto out;
4041 }
4042 memset(ioc->reply_post[i].reply_post_free, 0, sz);
4043 dinitprintk(ioc, pr_info(MPT3SAS_FMT
4044 "reply post free pool (0x%p): depth(%d),"
4045 "element_size(%d), pool_size(%d kB)\n", ioc->name,
4046 ioc->reply_post[i].reply_post_free,
4047 ioc->reply_post_queue_depth, 8, sz/1024));
4048 dinitprintk(ioc, pr_info(MPT3SAS_FMT
4049 "reply_post_free_dma = (0x%llx)\n", ioc->name,
4050 (unsigned long long)
4051 ioc->reply_post[i].reply_post_free_dma));
4052 total_sz += sz;
4053 } while (ioc->rdpq_array_enable && (++i < ioc->reply_queue_count));
4054
4055 if (ioc->dma_mask == 64) {
4056 if (_base_change_consistent_dma_mask(ioc, ioc->pdev) != 0) {
4057 pr_warn(MPT3SAS_FMT
4058 "no suitable consistent DMA mask for %s\n",
4059 ioc->name, pci_name(ioc->pdev));
4060 goto out;
4061 }
4062 }
4063
Sreekanth Reddyf92363d2012-11-30 07:44:21 +05304064 ioc->scsiio_depth = ioc->hba_queue_depth -
4065 ioc->hi_priority_depth - ioc->internal_depth;
4066
4067 /* set the scsi host can_queue depth
4068 * with some internal commands that could be outstanding
4069 */
Suganath prabu Subramanifd0331b2016-01-28 12:07:02 +05304070 ioc->shost->can_queue = ioc->scsiio_depth - INTERNAL_SCSIIO_CMDS_COUNT;
Sreekanth Reddyf92363d2012-11-30 07:44:21 +05304071 dinitprintk(ioc, pr_info(MPT3SAS_FMT
4072 "scsi host: can_queue depth (%d)\n",
4073 ioc->name, ioc->shost->can_queue));
4074
4075
4076 /* contiguous pool for request and chains, 16 byte align, one extra "
4077 * "frame for smid=0
4078 */
4079 ioc->chain_depth = ioc->chains_needed_per_io * ioc->scsiio_depth;
4080 sz = ((ioc->scsiio_depth + 1) * ioc->request_sz);
4081
4082 /* hi-priority queue */
4083 sz += (ioc->hi_priority_depth * ioc->request_sz);
4084
4085 /* internal queue */
4086 sz += (ioc->internal_depth * ioc->request_sz);
4087
4088 ioc->request_dma_sz = sz;
4089 ioc->request = pci_alloc_consistent(ioc->pdev, sz, &ioc->request_dma);
4090 if (!ioc->request) {
4091 pr_err(MPT3SAS_FMT "request pool: pci_alloc_consistent " \
4092 "failed: hba_depth(%d), chains_per_io(%d), frame_sz(%d), "
4093 "total(%d kB)\n", ioc->name, ioc->hba_queue_depth,
4094 ioc->chains_needed_per_io, ioc->request_sz, sz/1024);
4095 if (ioc->scsiio_depth < MPT3SAS_SAS_QUEUE_DEPTH)
4096 goto out;
Suganath prabu Subramanifd0331b2016-01-28 12:07:02 +05304097 retry_sz = 64;
4098 ioc->hba_queue_depth -= retry_sz;
Suganath prabu Subramani8ff045c2016-02-18 14:09:45 +05304099 _base_release_memory_pools(ioc);
Sreekanth Reddyf92363d2012-11-30 07:44:21 +05304100 goto retry_allocation;
4101 }
4102
4103 if (retry_sz)
4104 pr_err(MPT3SAS_FMT "request pool: pci_alloc_consistent " \
4105 "succeed: hba_depth(%d), chains_per_io(%d), frame_sz(%d), "
4106 "total(%d kb)\n", ioc->name, ioc->hba_queue_depth,
4107 ioc->chains_needed_per_io, ioc->request_sz, sz/1024);
4108
4109 /* hi-priority queue */
4110 ioc->hi_priority = ioc->request + ((ioc->scsiio_depth + 1) *
4111 ioc->request_sz);
4112 ioc->hi_priority_dma = ioc->request_dma + ((ioc->scsiio_depth + 1) *
4113 ioc->request_sz);
4114
4115 /* internal queue */
4116 ioc->internal = ioc->hi_priority + (ioc->hi_priority_depth *
4117 ioc->request_sz);
4118 ioc->internal_dma = ioc->hi_priority_dma + (ioc->hi_priority_depth *
4119 ioc->request_sz);
4120
4121 dinitprintk(ioc, pr_info(MPT3SAS_FMT
4122 "request pool(0x%p): depth(%d), frame_size(%d), pool_size(%d kB)\n",
4123 ioc->name, ioc->request, ioc->hba_queue_depth, ioc->request_sz,
4124 (ioc->hba_queue_depth * ioc->request_sz)/1024));
4125
4126 dinitprintk(ioc, pr_info(MPT3SAS_FMT "request pool: dma(0x%llx)\n",
4127 ioc->name, (unsigned long long) ioc->request_dma));
4128 total_sz += sz;
4129
4130 sz = ioc->scsiio_depth * sizeof(struct scsiio_tracker);
4131 ioc->scsi_lookup_pages = get_order(sz);
4132 ioc->scsi_lookup = (struct scsiio_tracker *)__get_free_pages(
4133 GFP_KERNEL, ioc->scsi_lookup_pages);
4134 if (!ioc->scsi_lookup) {
4135 pr_err(MPT3SAS_FMT "scsi_lookup: get_free_pages failed, sz(%d)\n",
4136 ioc->name, (int)sz);
4137 goto out;
4138 }
4139
4140 dinitprintk(ioc, pr_info(MPT3SAS_FMT "scsiio(0x%p): depth(%d)\n",
4141 ioc->name, ioc->request, ioc->scsiio_depth));
4142
4143 ioc->chain_depth = min_t(u32, ioc->chain_depth, MAX_CHAIN_DEPTH);
4144 sz = ioc->chain_depth * sizeof(struct chain_tracker);
4145 ioc->chain_pages = get_order(sz);
4146 ioc->chain_lookup = (struct chain_tracker *)__get_free_pages(
4147 GFP_KERNEL, ioc->chain_pages);
4148 if (!ioc->chain_lookup) {
4149 pr_err(MPT3SAS_FMT "chain_lookup: __get_free_pages failed\n",
4150 ioc->name);
4151 goto out;
4152 }
Romain Periere9d98412017-07-06 10:13:07 +02004153 ioc->chain_dma_pool = dma_pool_create("chain pool", &ioc->pdev->dev,
Suganath prabu Subramaniebb30242016-01-28 12:07:04 +05304154 ioc->chain_segment_sz, 16, 0);
Sreekanth Reddyf92363d2012-11-30 07:44:21 +05304155 if (!ioc->chain_dma_pool) {
Romain Periere9d98412017-07-06 10:13:07 +02004156 pr_err(MPT3SAS_FMT "chain_dma_pool: dma_pool_create failed\n",
Sreekanth Reddyf92363d2012-11-30 07:44:21 +05304157 ioc->name);
4158 goto out;
4159 }
4160 for (i = 0; i < ioc->chain_depth; i++) {
Romain Periere9d98412017-07-06 10:13:07 +02004161 ioc->chain_lookup[i].chain_buffer = dma_pool_alloc(
Sreekanth Reddyf92363d2012-11-30 07:44:21 +05304162 ioc->chain_dma_pool , GFP_KERNEL,
4163 &ioc->chain_lookup[i].chain_buffer_dma);
4164 if (!ioc->chain_lookup[i].chain_buffer) {
4165 ioc->chain_depth = i;
4166 goto chain_done;
4167 }
Suganath prabu Subramaniebb30242016-01-28 12:07:04 +05304168 total_sz += ioc->chain_segment_sz;
Sreekanth Reddyf92363d2012-11-30 07:44:21 +05304169 }
4170 chain_done:
4171 dinitprintk(ioc, pr_info(MPT3SAS_FMT
4172 "chain pool depth(%d), frame_size(%d), pool_size(%d kB)\n",
Suganath prabu Subramaniebb30242016-01-28 12:07:04 +05304173 ioc->name, ioc->chain_depth, ioc->chain_segment_sz,
4174 ((ioc->chain_depth * ioc->chain_segment_sz))/1024));
Sreekanth Reddyf92363d2012-11-30 07:44:21 +05304175
4176 /* initialize hi-priority queue smid's */
4177 ioc->hpr_lookup = kcalloc(ioc->hi_priority_depth,
4178 sizeof(struct request_tracker), GFP_KERNEL);
4179 if (!ioc->hpr_lookup) {
4180 pr_err(MPT3SAS_FMT "hpr_lookup: kcalloc failed\n",
4181 ioc->name);
4182 goto out;
4183 }
4184 ioc->hi_priority_smid = ioc->scsiio_depth + 1;
4185 dinitprintk(ioc, pr_info(MPT3SAS_FMT
4186 "hi_priority(0x%p): depth(%d), start smid(%d)\n",
4187 ioc->name, ioc->hi_priority,
4188 ioc->hi_priority_depth, ioc->hi_priority_smid));
4189
4190 /* initialize internal queue smid's */
4191 ioc->internal_lookup = kcalloc(ioc->internal_depth,
4192 sizeof(struct request_tracker), GFP_KERNEL);
4193 if (!ioc->internal_lookup) {
4194 pr_err(MPT3SAS_FMT "internal_lookup: kcalloc failed\n",
4195 ioc->name);
4196 goto out;
4197 }
4198 ioc->internal_smid = ioc->hi_priority_smid + ioc->hi_priority_depth;
4199 dinitprintk(ioc, pr_info(MPT3SAS_FMT
4200 "internal(0x%p): depth(%d), start smid(%d)\n",
4201 ioc->name, ioc->internal,
4202 ioc->internal_depth, ioc->internal_smid));
Suganath Prabu Subramani016d5c32017-10-31 18:02:28 +05304203 /*
4204 * The number of NVMe page sized blocks needed is:
4205 * (((sg_tablesize * 8) - 1) / (page_size - 8)) + 1
4206 * ((sg_tablesize * 8) - 1) is the max PRP's minus the first PRP entry
4207 * that is placed in the main message frame. 8 is the size of each PRP
4208 * entry or PRP list pointer entry. 8 is subtracted from page_size
4209 * because of the PRP list pointer entry at the end of a page, so this
4210 * is not counted as a PRP entry. The 1 added page is a round up.
4211 *
4212 * To avoid allocation failures due to the amount of memory that could
4213 * be required for NVMe PRP's, only each set of NVMe blocks will be
4214 * contiguous, so a new set is allocated for each possible I/O.
4215 */
4216 if (ioc->facts.ProtocolFlags & MPI2_IOCFACTS_PROTOCOL_NVME_DEVICES) {
4217 nvme_blocks_needed =
4218 (ioc->shost->sg_tablesize * NVME_PRP_SIZE) - 1;
4219 nvme_blocks_needed /= (ioc->page_size - NVME_PRP_SIZE);
4220 nvme_blocks_needed++;
Sreekanth Reddyf92363d2012-11-30 07:44:21 +05304221
Suganath Prabu Subramani016d5c32017-10-31 18:02:28 +05304222 sz = nvme_blocks_needed * ioc->page_size;
4223 ioc->pcie_sgl_dma_pool =
Romain Perier13a06402017-11-20 20:32:46 +01004224 dma_pool_create("PCIe SGL pool", &ioc->pdev->dev, sz, 16, 0);
Suganath Prabu Subramani016d5c32017-10-31 18:02:28 +05304225 if (!ioc->pcie_sgl_dma_pool) {
4226 pr_info(MPT3SAS_FMT
Romain Perier13a06402017-11-20 20:32:46 +01004227 "PCIe SGL pool: dma_pool_create failed\n",
Suganath Prabu Subramani016d5c32017-10-31 18:02:28 +05304228 ioc->name);
4229 goto out;
4230 }
4231 for (i = 0; i < ioc->scsiio_depth; i++) {
4232 ioc->scsi_lookup[i].pcie_sg_list.pcie_sgl =
Romain Perier13a06402017-11-20 20:32:46 +01004233 dma_pool_alloc(ioc->pcie_sgl_dma_pool,
Suganath Prabu Subramani016d5c32017-10-31 18:02:28 +05304234 GFP_KERNEL,
4235 &ioc->scsi_lookup[i].pcie_sg_list.pcie_sgl_dma);
4236 if (!ioc->scsi_lookup[i].pcie_sg_list.pcie_sgl) {
4237 pr_info(MPT3SAS_FMT
Romain Perier13a06402017-11-20 20:32:46 +01004238 "PCIe SGL pool: dma_pool_alloc failed\n",
Suganath Prabu Subramani016d5c32017-10-31 18:02:28 +05304239 ioc->name);
4240 goto out;
4241 }
4242 }
4243
4244 dinitprintk(ioc, pr_info(MPT3SAS_FMT "PCIe sgl pool depth(%d), "
4245 "element_size(%d), pool_size(%d kB)\n", ioc->name,
4246 ioc->scsiio_depth, sz, (sz * ioc->scsiio_depth)/1024));
4247 total_sz += sz * ioc->scsiio_depth;
4248 }
Sreekanth Reddyf92363d2012-11-30 07:44:21 +05304249 /* sense buffers, 4 byte align */
4250 sz = ioc->scsiio_depth * SCSI_SENSE_BUFFERSIZE;
Romain Periere9d98412017-07-06 10:13:07 +02004251 ioc->sense_dma_pool = dma_pool_create("sense pool", &ioc->pdev->dev, sz,
4252 4, 0);
Sreekanth Reddyf92363d2012-11-30 07:44:21 +05304253 if (!ioc->sense_dma_pool) {
Romain Periere9d98412017-07-06 10:13:07 +02004254 pr_err(MPT3SAS_FMT "sense pool: dma_pool_create failed\n",
Sreekanth Reddyf92363d2012-11-30 07:44:21 +05304255 ioc->name);
4256 goto out;
4257 }
Romain Periere9d98412017-07-06 10:13:07 +02004258 ioc->sense = dma_pool_alloc(ioc->sense_dma_pool, GFP_KERNEL,
Sreekanth Reddyf92363d2012-11-30 07:44:21 +05304259 &ioc->sense_dma);
4260 if (!ioc->sense) {
Romain Periere9d98412017-07-06 10:13:07 +02004261 pr_err(MPT3SAS_FMT "sense pool: dma_pool_alloc failed\n",
Sreekanth Reddyf92363d2012-11-30 07:44:21 +05304262 ioc->name);
4263 goto out;
4264 }
4265 dinitprintk(ioc, pr_info(MPT3SAS_FMT
4266 "sense pool(0x%p): depth(%d), element_size(%d), pool_size"
4267 "(%d kB)\n", ioc->name, ioc->sense, ioc->scsiio_depth,
4268 SCSI_SENSE_BUFFERSIZE, sz/1024));
4269 dinitprintk(ioc, pr_info(MPT3SAS_FMT "sense_dma(0x%llx)\n",
4270 ioc->name, (unsigned long long)ioc->sense_dma));
4271 total_sz += sz;
4272
4273 /* reply pool, 4 byte align */
4274 sz = ioc->reply_free_queue_depth * ioc->reply_sz;
Romain Periere9d98412017-07-06 10:13:07 +02004275 ioc->reply_dma_pool = dma_pool_create("reply pool", &ioc->pdev->dev, sz,
4276 4, 0);
Sreekanth Reddyf92363d2012-11-30 07:44:21 +05304277 if (!ioc->reply_dma_pool) {
Romain Periere9d98412017-07-06 10:13:07 +02004278 pr_err(MPT3SAS_FMT "reply pool: dma_pool_create failed\n",
Sreekanth Reddyf92363d2012-11-30 07:44:21 +05304279 ioc->name);
4280 goto out;
4281 }
Romain Periere9d98412017-07-06 10:13:07 +02004282 ioc->reply = dma_pool_alloc(ioc->reply_dma_pool, GFP_KERNEL,
Sreekanth Reddyf92363d2012-11-30 07:44:21 +05304283 &ioc->reply_dma);
4284 if (!ioc->reply) {
Romain Periere9d98412017-07-06 10:13:07 +02004285 pr_err(MPT3SAS_FMT "reply pool: dma_pool_alloc failed\n",
Sreekanth Reddyf92363d2012-11-30 07:44:21 +05304286 ioc->name);
4287 goto out;
4288 }
4289 ioc->reply_dma_min_address = (u32)(ioc->reply_dma);
4290 ioc->reply_dma_max_address = (u32)(ioc->reply_dma) + sz;
4291 dinitprintk(ioc, pr_info(MPT3SAS_FMT
4292 "reply pool(0x%p): depth(%d), frame_size(%d), pool_size(%d kB)\n",
4293 ioc->name, ioc->reply,
4294 ioc->reply_free_queue_depth, ioc->reply_sz, sz/1024));
4295 dinitprintk(ioc, pr_info(MPT3SAS_FMT "reply_dma(0x%llx)\n",
4296 ioc->name, (unsigned long long)ioc->reply_dma));
4297 total_sz += sz;
4298
4299 /* reply free queue, 16 byte align */
4300 sz = ioc->reply_free_queue_depth * 4;
Romain Periere9d98412017-07-06 10:13:07 +02004301 ioc->reply_free_dma_pool = dma_pool_create("reply_free pool",
4302 &ioc->pdev->dev, sz, 16, 0);
Sreekanth Reddyf92363d2012-11-30 07:44:21 +05304303 if (!ioc->reply_free_dma_pool) {
Romain Periere9d98412017-07-06 10:13:07 +02004304 pr_err(MPT3SAS_FMT "reply_free pool: dma_pool_create failed\n",
Sreekanth Reddyf92363d2012-11-30 07:44:21 +05304305 ioc->name);
4306 goto out;
4307 }
Romain Periere9d98412017-07-06 10:13:07 +02004308 ioc->reply_free = dma_pool_alloc(ioc->reply_free_dma_pool, GFP_KERNEL,
Sreekanth Reddyf92363d2012-11-30 07:44:21 +05304309 &ioc->reply_free_dma);
4310 if (!ioc->reply_free) {
Romain Periere9d98412017-07-06 10:13:07 +02004311 pr_err(MPT3SAS_FMT "reply_free pool: dma_pool_alloc failed\n",
Sreekanth Reddyf92363d2012-11-30 07:44:21 +05304312 ioc->name);
4313 goto out;
4314 }
4315 memset(ioc->reply_free, 0, sz);
4316 dinitprintk(ioc, pr_info(MPT3SAS_FMT "reply_free pool(0x%p): " \
4317 "depth(%d), element_size(%d), pool_size(%d kB)\n", ioc->name,
4318 ioc->reply_free, ioc->reply_free_queue_depth, 4, sz/1024));
4319 dinitprintk(ioc, pr_info(MPT3SAS_FMT
4320 "reply_free_dma (0x%llx)\n",
4321 ioc->name, (unsigned long long)ioc->reply_free_dma));
4322 total_sz += sz;
4323
Sreekanth Reddyf92363d2012-11-30 07:44:21 +05304324 ioc->config_page_sz = 512;
4325 ioc->config_page = pci_alloc_consistent(ioc->pdev,
4326 ioc->config_page_sz, &ioc->config_page_dma);
4327 if (!ioc->config_page) {
4328 pr_err(MPT3SAS_FMT
Romain Periere9d98412017-07-06 10:13:07 +02004329 "config page: dma_pool_alloc failed\n",
Sreekanth Reddyf92363d2012-11-30 07:44:21 +05304330 ioc->name);
4331 goto out;
4332 }
4333 dinitprintk(ioc, pr_info(MPT3SAS_FMT
4334 "config page(0x%p): size(%d)\n",
4335 ioc->name, ioc->config_page, ioc->config_page_sz));
4336 dinitprintk(ioc, pr_info(MPT3SAS_FMT "config_page_dma(0x%llx)\n",
4337 ioc->name, (unsigned long long)ioc->config_page_dma));
4338 total_sz += ioc->config_page_sz;
4339
4340 pr_info(MPT3SAS_FMT "Allocated physical memory: size(%d kB)\n",
4341 ioc->name, total_sz/1024);
4342 pr_info(MPT3SAS_FMT
4343 "Current Controller Queue Depth(%d),Max Controller Queue Depth(%d)\n",
4344 ioc->name, ioc->shost->can_queue, facts->RequestCredit);
4345 pr_info(MPT3SAS_FMT "Scatter Gather Elements per IO(%d)\n",
4346 ioc->name, ioc->shost->sg_tablesize);
4347 return 0;
4348
4349 out:
4350 return -ENOMEM;
4351}
4352
4353/**
4354 * mpt3sas_base_get_iocstate - Get the current state of a MPT adapter.
4355 * @ioc: Pointer to MPT_ADAPTER structure
4356 * @cooked: Request raw or cooked IOC state
4357 *
4358 * Returns all IOC Doorbell register bits if cooked==0, else just the
4359 * Doorbell bits in MPI_IOC_STATE_MASK.
4360 */
4361u32
4362mpt3sas_base_get_iocstate(struct MPT3SAS_ADAPTER *ioc, int cooked)
4363{
4364 u32 s, sc;
4365
4366 s = readl(&ioc->chip->Doorbell);
4367 sc = s & MPI2_IOC_STATE_MASK;
4368 return cooked ? sc : s;
4369}
4370
4371/**
4372 * _base_wait_on_iocstate - waiting on a particular ioc state
4373 * @ioc_state: controller state { READY, OPERATIONAL, or RESET }
4374 * @timeout: timeout in second
Sreekanth Reddyf92363d2012-11-30 07:44:21 +05304375 *
4376 * Returns 0 for success, non-zero for failure.
4377 */
4378static int
Calvin Owens98c56ad2016-07-28 21:38:21 -07004379_base_wait_on_iocstate(struct MPT3SAS_ADAPTER *ioc, u32 ioc_state, int timeout)
Sreekanth Reddyf92363d2012-11-30 07:44:21 +05304380{
4381 u32 count, cntdn;
4382 u32 current_state;
4383
4384 count = 0;
Calvin Owens98c56ad2016-07-28 21:38:21 -07004385 cntdn = 1000 * timeout;
Sreekanth Reddyf92363d2012-11-30 07:44:21 +05304386 do {
4387 current_state = mpt3sas_base_get_iocstate(ioc, 1);
4388 if (current_state == ioc_state)
4389 return 0;
4390 if (count && current_state == MPI2_IOC_STATE_FAULT)
4391 break;
Calvin Owens98c56ad2016-07-28 21:38:21 -07004392
4393 usleep_range(1000, 1500);
Sreekanth Reddyf92363d2012-11-30 07:44:21 +05304394 count++;
4395 } while (--cntdn);
4396
4397 return current_state;
4398}
4399
4400/**
4401 * _base_wait_for_doorbell_int - waiting for controller interrupt(generated by
4402 * a write to the doorbell)
4403 * @ioc: per adapter object
4404 * @timeout: timeout in second
Sreekanth Reddyf92363d2012-11-30 07:44:21 +05304405 *
4406 * Returns 0 for success, non-zero for failure.
4407 *
4408 * Notes: MPI2_HIS_IOC2SYS_DB_STATUS - set to one when IOC writes to doorbell.
4409 */
4410static int
Calvin Owens98c56ad2016-07-28 21:38:21 -07004411_base_diag_reset(struct MPT3SAS_ADAPTER *ioc);
Sreekanth Reddy4dc8c802015-06-30 12:24:48 +05304412
4413static int
Calvin Owens98c56ad2016-07-28 21:38:21 -07004414_base_wait_for_doorbell_int(struct MPT3SAS_ADAPTER *ioc, int timeout)
Sreekanth Reddyf92363d2012-11-30 07:44:21 +05304415{
4416 u32 cntdn, count;
4417 u32 int_status;
4418
4419 count = 0;
Calvin Owens98c56ad2016-07-28 21:38:21 -07004420 cntdn = 1000 * timeout;
Sreekanth Reddyf92363d2012-11-30 07:44:21 +05304421 do {
4422 int_status = readl(&ioc->chip->HostInterruptStatus);
4423 if (int_status & MPI2_HIS_IOC2SYS_DB_STATUS) {
4424 dhsprintk(ioc, pr_info(MPT3SAS_FMT
4425 "%s: successful count(%d), timeout(%d)\n",
4426 ioc->name, __func__, count, timeout));
4427 return 0;
4428 }
Calvin Owens98c56ad2016-07-28 21:38:21 -07004429
4430 usleep_range(1000, 1500);
Sreekanth Reddyf92363d2012-11-30 07:44:21 +05304431 count++;
4432 } while (--cntdn);
4433
4434 pr_err(MPT3SAS_FMT
4435 "%s: failed due to timeout count(%d), int_status(%x)!\n",
4436 ioc->name, __func__, count, int_status);
4437 return -EFAULT;
4438}
4439
Calvin Owens98c56ad2016-07-28 21:38:21 -07004440static int
4441_base_spin_on_doorbell_int(struct MPT3SAS_ADAPTER *ioc, int timeout)
4442{
4443 u32 cntdn, count;
4444 u32 int_status;
4445
4446 count = 0;
4447 cntdn = 2000 * timeout;
4448 do {
4449 int_status = readl(&ioc->chip->HostInterruptStatus);
4450 if (int_status & MPI2_HIS_IOC2SYS_DB_STATUS) {
4451 dhsprintk(ioc, pr_info(MPT3SAS_FMT
4452 "%s: successful count(%d), timeout(%d)\n",
4453 ioc->name, __func__, count, timeout));
4454 return 0;
4455 }
4456
4457 udelay(500);
4458 count++;
4459 } while (--cntdn);
4460
4461 pr_err(MPT3SAS_FMT
4462 "%s: failed due to timeout count(%d), int_status(%x)!\n",
4463 ioc->name, __func__, count, int_status);
4464 return -EFAULT;
4465
4466}
4467
Sreekanth Reddyf92363d2012-11-30 07:44:21 +05304468/**
4469 * _base_wait_for_doorbell_ack - waiting for controller to read the doorbell.
4470 * @ioc: per adapter object
4471 * @timeout: timeout in second
Sreekanth Reddyf92363d2012-11-30 07:44:21 +05304472 *
4473 * Returns 0 for success, non-zero for failure.
4474 *
4475 * Notes: MPI2_HIS_SYS2IOC_DB_STATUS - set to one when host writes to
4476 * doorbell.
4477 */
4478static int
Calvin Owens98c56ad2016-07-28 21:38:21 -07004479_base_wait_for_doorbell_ack(struct MPT3SAS_ADAPTER *ioc, int timeout)
Sreekanth Reddyf92363d2012-11-30 07:44:21 +05304480{
4481 u32 cntdn, count;
4482 u32 int_status;
4483 u32 doorbell;
4484
4485 count = 0;
Calvin Owens98c56ad2016-07-28 21:38:21 -07004486 cntdn = 1000 * timeout;
Sreekanth Reddyf92363d2012-11-30 07:44:21 +05304487 do {
4488 int_status = readl(&ioc->chip->HostInterruptStatus);
4489 if (!(int_status & MPI2_HIS_SYS2IOC_DB_STATUS)) {
4490 dhsprintk(ioc, pr_info(MPT3SAS_FMT
4491 "%s: successful count(%d), timeout(%d)\n",
4492 ioc->name, __func__, count, timeout));
4493 return 0;
4494 } else if (int_status & MPI2_HIS_IOC2SYS_DB_STATUS) {
4495 doorbell = readl(&ioc->chip->Doorbell);
4496 if ((doorbell & MPI2_IOC_STATE_MASK) ==
4497 MPI2_IOC_STATE_FAULT) {
4498 mpt3sas_base_fault_info(ioc , doorbell);
4499 return -EFAULT;
4500 }
4501 } else if (int_status == 0xFFFFFFFF)
4502 goto out;
4503
Calvin Owens98c56ad2016-07-28 21:38:21 -07004504 usleep_range(1000, 1500);
Sreekanth Reddyf92363d2012-11-30 07:44:21 +05304505 count++;
4506 } while (--cntdn);
4507
4508 out:
4509 pr_err(MPT3SAS_FMT
4510 "%s: failed due to timeout count(%d), int_status(%x)!\n",
4511 ioc->name, __func__, count, int_status);
4512 return -EFAULT;
4513}
4514
4515/**
4516 * _base_wait_for_doorbell_not_used - waiting for doorbell to not be in use
4517 * @ioc: per adapter object
4518 * @timeout: timeout in second
Sreekanth Reddyf92363d2012-11-30 07:44:21 +05304519 *
4520 * Returns 0 for success, non-zero for failure.
4521 *
4522 */
4523static int
Calvin Owens98c56ad2016-07-28 21:38:21 -07004524_base_wait_for_doorbell_not_used(struct MPT3SAS_ADAPTER *ioc, int timeout)
Sreekanth Reddyf92363d2012-11-30 07:44:21 +05304525{
4526 u32 cntdn, count;
4527 u32 doorbell_reg;
4528
4529 count = 0;
Calvin Owens98c56ad2016-07-28 21:38:21 -07004530 cntdn = 1000 * timeout;
Sreekanth Reddyf92363d2012-11-30 07:44:21 +05304531 do {
4532 doorbell_reg = readl(&ioc->chip->Doorbell);
4533 if (!(doorbell_reg & MPI2_DOORBELL_USED)) {
4534 dhsprintk(ioc, pr_info(MPT3SAS_FMT
4535 "%s: successful count(%d), timeout(%d)\n",
4536 ioc->name, __func__, count, timeout));
4537 return 0;
4538 }
Calvin Owens98c56ad2016-07-28 21:38:21 -07004539
4540 usleep_range(1000, 1500);
Sreekanth Reddyf92363d2012-11-30 07:44:21 +05304541 count++;
4542 } while (--cntdn);
4543
4544 pr_err(MPT3SAS_FMT
4545 "%s: failed due to timeout count(%d), doorbell_reg(%x)!\n",
4546 ioc->name, __func__, count, doorbell_reg);
4547 return -EFAULT;
4548}
4549
4550/**
4551 * _base_send_ioc_reset - send doorbell reset
4552 * @ioc: per adapter object
4553 * @reset_type: currently only supports: MPI2_FUNCTION_IOC_MESSAGE_UNIT_RESET
4554 * @timeout: timeout in second
Sreekanth Reddyf92363d2012-11-30 07:44:21 +05304555 *
4556 * Returns 0 for success, non-zero for failure.
4557 */
4558static int
Calvin Owens98c56ad2016-07-28 21:38:21 -07004559_base_send_ioc_reset(struct MPT3SAS_ADAPTER *ioc, u8 reset_type, int timeout)
Sreekanth Reddyf92363d2012-11-30 07:44:21 +05304560{
4561 u32 ioc_state;
4562 int r = 0;
4563
4564 if (reset_type != MPI2_FUNCTION_IOC_MESSAGE_UNIT_RESET) {
4565 pr_err(MPT3SAS_FMT "%s: unknown reset_type\n",
4566 ioc->name, __func__);
4567 return -EFAULT;
4568 }
4569
4570 if (!(ioc->facts.IOCCapabilities &
4571 MPI2_IOCFACTS_CAPABILITY_EVENT_REPLAY))
4572 return -EFAULT;
4573
4574 pr_info(MPT3SAS_FMT "sending message unit reset !!\n", ioc->name);
4575
4576 writel(reset_type << MPI2_DOORBELL_FUNCTION_SHIFT,
4577 &ioc->chip->Doorbell);
Calvin Owens98c56ad2016-07-28 21:38:21 -07004578 if ((_base_wait_for_doorbell_ack(ioc, 15))) {
Sreekanth Reddyf92363d2012-11-30 07:44:21 +05304579 r = -EFAULT;
4580 goto out;
4581 }
Calvin Owens98c56ad2016-07-28 21:38:21 -07004582 ioc_state = _base_wait_on_iocstate(ioc, MPI2_IOC_STATE_READY, timeout);
Sreekanth Reddyf92363d2012-11-30 07:44:21 +05304583 if (ioc_state) {
4584 pr_err(MPT3SAS_FMT
4585 "%s: failed going to ready state (ioc_state=0x%x)\n",
4586 ioc->name, __func__, ioc_state);
4587 r = -EFAULT;
4588 goto out;
4589 }
4590 out:
4591 pr_info(MPT3SAS_FMT "message unit reset: %s\n",
4592 ioc->name, ((r == 0) ? "SUCCESS" : "FAILED"));
4593 return r;
4594}
4595
4596/**
4597 * _base_handshake_req_reply_wait - send request thru doorbell interface
4598 * @ioc: per adapter object
4599 * @request_bytes: request length
4600 * @request: pointer having request payload
4601 * @reply_bytes: reply length
4602 * @reply: pointer to reply payload
4603 * @timeout: timeout in second
Sreekanth Reddyf92363d2012-11-30 07:44:21 +05304604 *
4605 * Returns 0 for success, non-zero for failure.
4606 */
4607static int
4608_base_handshake_req_reply_wait(struct MPT3SAS_ADAPTER *ioc, int request_bytes,
Calvin Owens98c56ad2016-07-28 21:38:21 -07004609 u32 *request, int reply_bytes, u16 *reply, int timeout)
Sreekanth Reddyf92363d2012-11-30 07:44:21 +05304610{
4611 MPI2DefaultReply_t *default_reply = (MPI2DefaultReply_t *)reply;
4612 int i;
4613 u8 failed;
Sreekanth Reddyf92363d2012-11-30 07:44:21 +05304614 __le32 *mfp;
4615
4616 /* make sure doorbell is not in use */
4617 if ((readl(&ioc->chip->Doorbell) & MPI2_DOORBELL_USED)) {
4618 pr_err(MPT3SAS_FMT
4619 "doorbell is in use (line=%d)\n",
4620 ioc->name, __LINE__);
4621 return -EFAULT;
4622 }
4623
4624 /* clear pending doorbell interrupts from previous state changes */
4625 if (readl(&ioc->chip->HostInterruptStatus) &
4626 MPI2_HIS_IOC2SYS_DB_STATUS)
4627 writel(0, &ioc->chip->HostInterruptStatus);
4628
4629 /* send message to ioc */
4630 writel(((MPI2_FUNCTION_HANDSHAKE<<MPI2_DOORBELL_FUNCTION_SHIFT) |
4631 ((request_bytes/4)<<MPI2_DOORBELL_ADD_DWORDS_SHIFT)),
4632 &ioc->chip->Doorbell);
4633
Calvin Owens98c56ad2016-07-28 21:38:21 -07004634 if ((_base_spin_on_doorbell_int(ioc, 5))) {
Sreekanth Reddyf92363d2012-11-30 07:44:21 +05304635 pr_err(MPT3SAS_FMT
4636 "doorbell handshake int failed (line=%d)\n",
4637 ioc->name, __LINE__);
4638 return -EFAULT;
4639 }
4640 writel(0, &ioc->chip->HostInterruptStatus);
4641
Calvin Owens98c56ad2016-07-28 21:38:21 -07004642 if ((_base_wait_for_doorbell_ack(ioc, 5))) {
Sreekanth Reddyf92363d2012-11-30 07:44:21 +05304643 pr_err(MPT3SAS_FMT
4644 "doorbell handshake ack failed (line=%d)\n",
4645 ioc->name, __LINE__);
4646 return -EFAULT;
4647 }
4648
4649 /* send message 32-bits at a time */
4650 for (i = 0, failed = 0; i < request_bytes/4 && !failed; i++) {
4651 writel(cpu_to_le32(request[i]), &ioc->chip->Doorbell);
Calvin Owens98c56ad2016-07-28 21:38:21 -07004652 if ((_base_wait_for_doorbell_ack(ioc, 5)))
Sreekanth Reddyf92363d2012-11-30 07:44:21 +05304653 failed = 1;
4654 }
4655
4656 if (failed) {
4657 pr_err(MPT3SAS_FMT
4658 "doorbell handshake sending request failed (line=%d)\n",
4659 ioc->name, __LINE__);
4660 return -EFAULT;
4661 }
4662
4663 /* now wait for the reply */
Calvin Owens98c56ad2016-07-28 21:38:21 -07004664 if ((_base_wait_for_doorbell_int(ioc, timeout))) {
Sreekanth Reddyf92363d2012-11-30 07:44:21 +05304665 pr_err(MPT3SAS_FMT
4666 "doorbell handshake int failed (line=%d)\n",
4667 ioc->name, __LINE__);
4668 return -EFAULT;
4669 }
4670
4671 /* read the first two 16-bits, it gives the total length of the reply */
4672 reply[0] = le16_to_cpu(readl(&ioc->chip->Doorbell)
4673 & MPI2_DOORBELL_DATA_MASK);
4674 writel(0, &ioc->chip->HostInterruptStatus);
Calvin Owens98c56ad2016-07-28 21:38:21 -07004675 if ((_base_wait_for_doorbell_int(ioc, 5))) {
Sreekanth Reddyf92363d2012-11-30 07:44:21 +05304676 pr_err(MPT3SAS_FMT
4677 "doorbell handshake int failed (line=%d)\n",
4678 ioc->name, __LINE__);
4679 return -EFAULT;
4680 }
4681 reply[1] = le16_to_cpu(readl(&ioc->chip->Doorbell)
4682 & MPI2_DOORBELL_DATA_MASK);
4683 writel(0, &ioc->chip->HostInterruptStatus);
4684
4685 for (i = 2; i < default_reply->MsgLength * 2; i++) {
Calvin Owens98c56ad2016-07-28 21:38:21 -07004686 if ((_base_wait_for_doorbell_int(ioc, 5))) {
Sreekanth Reddyf92363d2012-11-30 07:44:21 +05304687 pr_err(MPT3SAS_FMT
4688 "doorbell handshake int failed (line=%d)\n",
4689 ioc->name, __LINE__);
4690 return -EFAULT;
4691 }
4692 if (i >= reply_bytes/2) /* overflow case */
Calvin Owens8bbb1cf2016-07-28 21:38:22 -07004693 readl(&ioc->chip->Doorbell);
Sreekanth Reddyf92363d2012-11-30 07:44:21 +05304694 else
4695 reply[i] = le16_to_cpu(readl(&ioc->chip->Doorbell)
4696 & MPI2_DOORBELL_DATA_MASK);
4697 writel(0, &ioc->chip->HostInterruptStatus);
4698 }
4699
Calvin Owens98c56ad2016-07-28 21:38:21 -07004700 _base_wait_for_doorbell_int(ioc, 5);
4701 if (_base_wait_for_doorbell_not_used(ioc, 5) != 0) {
Sreekanth Reddyf92363d2012-11-30 07:44:21 +05304702 dhsprintk(ioc, pr_info(MPT3SAS_FMT
4703 "doorbell is in use (line=%d)\n", ioc->name, __LINE__));
4704 }
4705 writel(0, &ioc->chip->HostInterruptStatus);
4706
4707 if (ioc->logging_level & MPT_DEBUG_INIT) {
4708 mfp = (__le32 *)reply;
4709 pr_info("\toffset:data\n");
4710 for (i = 0; i < reply_bytes/4; i++)
4711 pr_info("\t[0x%02x]:%08x\n", i*4,
4712 le32_to_cpu(mfp[i]));
4713 }
4714 return 0;
4715}
4716
4717/**
4718 * mpt3sas_base_sas_iounit_control - send sas iounit control to FW
4719 * @ioc: per adapter object
4720 * @mpi_reply: the reply payload from FW
4721 * @mpi_request: the request payload sent to FW
4722 *
4723 * The SAS IO Unit Control Request message allows the host to perform low-level
4724 * operations, such as resets on the PHYs of the IO Unit, also allows the host
4725 * to obtain the IOC assigned device handles for a device if it has other
4726 * identifying information about the device, in addition allows the host to
4727 * remove IOC resources associated with the device.
4728 *
4729 * Returns 0 for success, non-zero for failure.
4730 */
4731int
4732mpt3sas_base_sas_iounit_control(struct MPT3SAS_ADAPTER *ioc,
4733 Mpi2SasIoUnitControlReply_t *mpi_reply,
4734 Mpi2SasIoUnitControlRequest_t *mpi_request)
4735{
4736 u16 smid;
4737 u32 ioc_state;
Dan Carpentereb445522014-12-04 13:57:05 +03004738 bool issue_reset = false;
Sreekanth Reddyf92363d2012-11-30 07:44:21 +05304739 int rc;
4740 void *request;
4741 u16 wait_state_count;
4742
4743 dinitprintk(ioc, pr_info(MPT3SAS_FMT "%s\n", ioc->name,
4744 __func__));
4745
4746 mutex_lock(&ioc->base_cmds.mutex);
4747
4748 if (ioc->base_cmds.status != MPT3_CMD_NOT_USED) {
4749 pr_err(MPT3SAS_FMT "%s: base_cmd in use\n",
4750 ioc->name, __func__);
4751 rc = -EAGAIN;
4752 goto out;
4753 }
4754
4755 wait_state_count = 0;
4756 ioc_state = mpt3sas_base_get_iocstate(ioc, 1);
4757 while (ioc_state != MPI2_IOC_STATE_OPERATIONAL) {
4758 if (wait_state_count++ == 10) {
4759 pr_err(MPT3SAS_FMT
4760 "%s: failed due to ioc not operational\n",
4761 ioc->name, __func__);
4762 rc = -EFAULT;
4763 goto out;
4764 }
4765 ssleep(1);
4766 ioc_state = mpt3sas_base_get_iocstate(ioc, 1);
4767 pr_info(MPT3SAS_FMT
4768 "%s: waiting for operational state(count=%d)\n",
4769 ioc->name, __func__, wait_state_count);
4770 }
4771
4772 smid = mpt3sas_base_get_smid(ioc, ioc->base_cb_idx);
4773 if (!smid) {
4774 pr_err(MPT3SAS_FMT "%s: failed obtaining a smid\n",
4775 ioc->name, __func__);
4776 rc = -EAGAIN;
4777 goto out;
4778 }
4779
4780 rc = 0;
4781 ioc->base_cmds.status = MPT3_CMD_PENDING;
4782 request = mpt3sas_base_get_msg_frame(ioc, smid);
4783 ioc->base_cmds.smid = smid;
4784 memcpy(request, mpi_request, sizeof(Mpi2SasIoUnitControlRequest_t));
4785 if (mpi_request->Operation == MPI2_SAS_OP_PHY_HARD_RESET ||
4786 mpi_request->Operation == MPI2_SAS_OP_PHY_LINK_RESET)
4787 ioc->ioc_link_reset_in_progress = 1;
4788 init_completion(&ioc->base_cmds.done);
Suganath Prabu Subramani81c16f82016-10-26 13:34:40 +05304789 ioc->put_smid_default(ioc, smid);
Calvin Owens8bbb1cf2016-07-28 21:38:22 -07004790 wait_for_completion_timeout(&ioc->base_cmds.done,
Sreekanth Reddyf92363d2012-11-30 07:44:21 +05304791 msecs_to_jiffies(10000));
4792 if ((mpi_request->Operation == MPI2_SAS_OP_PHY_HARD_RESET ||
4793 mpi_request->Operation == MPI2_SAS_OP_PHY_LINK_RESET) &&
4794 ioc->ioc_link_reset_in_progress)
4795 ioc->ioc_link_reset_in_progress = 0;
4796 if (!(ioc->base_cmds.status & MPT3_CMD_COMPLETE)) {
4797 pr_err(MPT3SAS_FMT "%s: timeout\n",
4798 ioc->name, __func__);
4799 _debug_dump_mf(mpi_request,
4800 sizeof(Mpi2SasIoUnitControlRequest_t)/4);
4801 if (!(ioc->base_cmds.status & MPT3_CMD_RESET))
Dan Carpentereb445522014-12-04 13:57:05 +03004802 issue_reset = true;
Sreekanth Reddyf92363d2012-11-30 07:44:21 +05304803 goto issue_host_reset;
4804 }
4805 if (ioc->base_cmds.status & MPT3_CMD_REPLY_VALID)
4806 memcpy(mpi_reply, ioc->base_cmds.reply,
4807 sizeof(Mpi2SasIoUnitControlReply_t));
4808 else
4809 memset(mpi_reply, 0, sizeof(Mpi2SasIoUnitControlReply_t));
4810 ioc->base_cmds.status = MPT3_CMD_NOT_USED;
4811 goto out;
4812
4813 issue_host_reset:
4814 if (issue_reset)
Calvin Owens98c56ad2016-07-28 21:38:21 -07004815 mpt3sas_base_hard_reset_handler(ioc, FORCE_BIG_HAMMER);
Sreekanth Reddyf92363d2012-11-30 07:44:21 +05304816 ioc->base_cmds.status = MPT3_CMD_NOT_USED;
4817 rc = -EFAULT;
4818 out:
4819 mutex_unlock(&ioc->base_cmds.mutex);
4820 return rc;
4821}
4822
4823/**
4824 * mpt3sas_base_scsi_enclosure_processor - sending request to sep device
4825 * @ioc: per adapter object
4826 * @mpi_reply: the reply payload from FW
4827 * @mpi_request: the request payload sent to FW
4828 *
4829 * The SCSI Enclosure Processor request message causes the IOC to
4830 * communicate with SES devices to control LED status signals.
4831 *
4832 * Returns 0 for success, non-zero for failure.
4833 */
4834int
4835mpt3sas_base_scsi_enclosure_processor(struct MPT3SAS_ADAPTER *ioc,
4836 Mpi2SepReply_t *mpi_reply, Mpi2SepRequest_t *mpi_request)
4837{
4838 u16 smid;
4839 u32 ioc_state;
Dan Carpentereb445522014-12-04 13:57:05 +03004840 bool issue_reset = false;
Sreekanth Reddyf92363d2012-11-30 07:44:21 +05304841 int rc;
4842 void *request;
4843 u16 wait_state_count;
4844
4845 dinitprintk(ioc, pr_info(MPT3SAS_FMT "%s\n", ioc->name,
4846 __func__));
4847
4848 mutex_lock(&ioc->base_cmds.mutex);
4849
4850 if (ioc->base_cmds.status != MPT3_CMD_NOT_USED) {
4851 pr_err(MPT3SAS_FMT "%s: base_cmd in use\n",
4852 ioc->name, __func__);
4853 rc = -EAGAIN;
4854 goto out;
4855 }
4856
4857 wait_state_count = 0;
4858 ioc_state = mpt3sas_base_get_iocstate(ioc, 1);
4859 while (ioc_state != MPI2_IOC_STATE_OPERATIONAL) {
4860 if (wait_state_count++ == 10) {
4861 pr_err(MPT3SAS_FMT
4862 "%s: failed due to ioc not operational\n",
4863 ioc->name, __func__);
4864 rc = -EFAULT;
4865 goto out;
4866 }
4867 ssleep(1);
4868 ioc_state = mpt3sas_base_get_iocstate(ioc, 1);
4869 pr_info(MPT3SAS_FMT
4870 "%s: waiting for operational state(count=%d)\n",
4871 ioc->name,
4872 __func__, wait_state_count);
4873 }
4874
4875 smid = mpt3sas_base_get_smid(ioc, ioc->base_cb_idx);
4876 if (!smid) {
4877 pr_err(MPT3SAS_FMT "%s: failed obtaining a smid\n",
4878 ioc->name, __func__);
4879 rc = -EAGAIN;
4880 goto out;
4881 }
4882
4883 rc = 0;
4884 ioc->base_cmds.status = MPT3_CMD_PENDING;
4885 request = mpt3sas_base_get_msg_frame(ioc, smid);
4886 ioc->base_cmds.smid = smid;
4887 memcpy(request, mpi_request, sizeof(Mpi2SepReply_t));
4888 init_completion(&ioc->base_cmds.done);
Suganath Prabu Subramani81c16f82016-10-26 13:34:40 +05304889 ioc->put_smid_default(ioc, smid);
Calvin Owens8bbb1cf2016-07-28 21:38:22 -07004890 wait_for_completion_timeout(&ioc->base_cmds.done,
Sreekanth Reddyf92363d2012-11-30 07:44:21 +05304891 msecs_to_jiffies(10000));
4892 if (!(ioc->base_cmds.status & MPT3_CMD_COMPLETE)) {
4893 pr_err(MPT3SAS_FMT "%s: timeout\n",
4894 ioc->name, __func__);
4895 _debug_dump_mf(mpi_request,
4896 sizeof(Mpi2SepRequest_t)/4);
4897 if (!(ioc->base_cmds.status & MPT3_CMD_RESET))
Dan Carpentereb445522014-12-04 13:57:05 +03004898 issue_reset = false;
Sreekanth Reddyf92363d2012-11-30 07:44:21 +05304899 goto issue_host_reset;
4900 }
4901 if (ioc->base_cmds.status & MPT3_CMD_REPLY_VALID)
4902 memcpy(mpi_reply, ioc->base_cmds.reply,
4903 sizeof(Mpi2SepReply_t));
4904 else
4905 memset(mpi_reply, 0, sizeof(Mpi2SepReply_t));
4906 ioc->base_cmds.status = MPT3_CMD_NOT_USED;
4907 goto out;
4908
4909 issue_host_reset:
4910 if (issue_reset)
Calvin Owens98c56ad2016-07-28 21:38:21 -07004911 mpt3sas_base_hard_reset_handler(ioc, FORCE_BIG_HAMMER);
Sreekanth Reddyf92363d2012-11-30 07:44:21 +05304912 ioc->base_cmds.status = MPT3_CMD_NOT_USED;
4913 rc = -EFAULT;
4914 out:
4915 mutex_unlock(&ioc->base_cmds.mutex);
4916 return rc;
4917}
4918
4919/**
4920 * _base_get_port_facts - obtain port facts reply and save in ioc
4921 * @ioc: per adapter object
Sreekanth Reddyf92363d2012-11-30 07:44:21 +05304922 *
4923 * Returns 0 for success, non-zero for failure.
4924 */
4925static int
Calvin Owens98c56ad2016-07-28 21:38:21 -07004926_base_get_port_facts(struct MPT3SAS_ADAPTER *ioc, int port)
Sreekanth Reddyf92363d2012-11-30 07:44:21 +05304927{
4928 Mpi2PortFactsRequest_t mpi_request;
4929 Mpi2PortFactsReply_t mpi_reply;
4930 struct mpt3sas_port_facts *pfacts;
4931 int mpi_reply_sz, mpi_request_sz, r;
4932
4933 dinitprintk(ioc, pr_info(MPT3SAS_FMT "%s\n", ioc->name,
4934 __func__));
4935
4936 mpi_reply_sz = sizeof(Mpi2PortFactsReply_t);
4937 mpi_request_sz = sizeof(Mpi2PortFactsRequest_t);
4938 memset(&mpi_request, 0, mpi_request_sz);
4939 mpi_request.Function = MPI2_FUNCTION_PORT_FACTS;
4940 mpi_request.PortNumber = port;
4941 r = _base_handshake_req_reply_wait(ioc, mpi_request_sz,
Calvin Owens98c56ad2016-07-28 21:38:21 -07004942 (u32 *)&mpi_request, mpi_reply_sz, (u16 *)&mpi_reply, 5);
Sreekanth Reddyf92363d2012-11-30 07:44:21 +05304943
4944 if (r != 0) {
4945 pr_err(MPT3SAS_FMT "%s: handshake failed (r=%d)\n",
4946 ioc->name, __func__, r);
4947 return r;
4948 }
4949
4950 pfacts = &ioc->pfacts[port];
4951 memset(pfacts, 0, sizeof(struct mpt3sas_port_facts));
4952 pfacts->PortNumber = mpi_reply.PortNumber;
4953 pfacts->VP_ID = mpi_reply.VP_ID;
4954 pfacts->VF_ID = mpi_reply.VF_ID;
4955 pfacts->MaxPostedCmdBuffers =
4956 le16_to_cpu(mpi_reply.MaxPostedCmdBuffers);
4957
4958 return 0;
4959}
4960
4961/**
Sreekanth Reddy4dc8c802015-06-30 12:24:48 +05304962 * _base_wait_for_iocstate - Wait until the card is in READY or OPERATIONAL
4963 * @ioc: per adapter object
4964 * @timeout:
Sreekanth Reddy4dc8c802015-06-30 12:24:48 +05304965 *
4966 * Returns 0 for success, non-zero for failure.
4967 */
4968static int
Calvin Owens98c56ad2016-07-28 21:38:21 -07004969_base_wait_for_iocstate(struct MPT3SAS_ADAPTER *ioc, int timeout)
Sreekanth Reddy4dc8c802015-06-30 12:24:48 +05304970{
4971 u32 ioc_state;
4972 int rc;
4973
4974 dinitprintk(ioc, printk(MPT3SAS_FMT "%s\n", ioc->name,
4975 __func__));
4976
4977 if (ioc->pci_error_recovery) {
4978 dfailprintk(ioc, printk(MPT3SAS_FMT
4979 "%s: host in pci error recovery\n", ioc->name, __func__));
4980 return -EFAULT;
4981 }
4982
4983 ioc_state = mpt3sas_base_get_iocstate(ioc, 0);
4984 dhsprintk(ioc, printk(MPT3SAS_FMT "%s: ioc_state(0x%08x)\n",
4985 ioc->name, __func__, ioc_state));
4986
4987 if (((ioc_state & MPI2_IOC_STATE_MASK) == MPI2_IOC_STATE_READY) ||
4988 (ioc_state & MPI2_IOC_STATE_MASK) == MPI2_IOC_STATE_OPERATIONAL)
4989 return 0;
4990
4991 if (ioc_state & MPI2_DOORBELL_USED) {
4992 dhsprintk(ioc, printk(MPT3SAS_FMT
4993 "unexpected doorbell active!\n", ioc->name));
4994 goto issue_diag_reset;
4995 }
4996
4997 if ((ioc_state & MPI2_IOC_STATE_MASK) == MPI2_IOC_STATE_FAULT) {
4998 mpt3sas_base_fault_info(ioc, ioc_state &
4999 MPI2_DOORBELL_DATA_MASK);
5000 goto issue_diag_reset;
5001 }
5002
Calvin Owens98c56ad2016-07-28 21:38:21 -07005003 ioc_state = _base_wait_on_iocstate(ioc, MPI2_IOC_STATE_READY, timeout);
Sreekanth Reddy4dc8c802015-06-30 12:24:48 +05305004 if (ioc_state) {
5005 dfailprintk(ioc, printk(MPT3SAS_FMT
5006 "%s: failed going to ready state (ioc_state=0x%x)\n",
5007 ioc->name, __func__, ioc_state));
5008 return -EFAULT;
5009 }
5010
5011 issue_diag_reset:
Calvin Owens98c56ad2016-07-28 21:38:21 -07005012 rc = _base_diag_reset(ioc);
Sreekanth Reddy4dc8c802015-06-30 12:24:48 +05305013 return rc;
5014}
5015
5016/**
Sreekanth Reddyf92363d2012-11-30 07:44:21 +05305017 * _base_get_ioc_facts - obtain ioc facts reply and save in ioc
5018 * @ioc: per adapter object
Sreekanth Reddyf92363d2012-11-30 07:44:21 +05305019 *
5020 * Returns 0 for success, non-zero for failure.
5021 */
5022static int
Calvin Owens98c56ad2016-07-28 21:38:21 -07005023_base_get_ioc_facts(struct MPT3SAS_ADAPTER *ioc)
Sreekanth Reddyf92363d2012-11-30 07:44:21 +05305024{
5025 Mpi2IOCFactsRequest_t mpi_request;
5026 Mpi2IOCFactsReply_t mpi_reply;
5027 struct mpt3sas_facts *facts;
5028 int mpi_reply_sz, mpi_request_sz, r;
5029
5030 dinitprintk(ioc, pr_info(MPT3SAS_FMT "%s\n", ioc->name,
5031 __func__));
5032
Calvin Owens98c56ad2016-07-28 21:38:21 -07005033 r = _base_wait_for_iocstate(ioc, 10);
Sreekanth Reddy4dc8c802015-06-30 12:24:48 +05305034 if (r) {
5035 dfailprintk(ioc, printk(MPT3SAS_FMT
5036 "%s: failed getting to correct state\n",
5037 ioc->name, __func__));
5038 return r;
5039 }
Sreekanth Reddyf92363d2012-11-30 07:44:21 +05305040 mpi_reply_sz = sizeof(Mpi2IOCFactsReply_t);
5041 mpi_request_sz = sizeof(Mpi2IOCFactsRequest_t);
5042 memset(&mpi_request, 0, mpi_request_sz);
5043 mpi_request.Function = MPI2_FUNCTION_IOC_FACTS;
5044 r = _base_handshake_req_reply_wait(ioc, mpi_request_sz,
Calvin Owens98c56ad2016-07-28 21:38:21 -07005045 (u32 *)&mpi_request, mpi_reply_sz, (u16 *)&mpi_reply, 5);
Sreekanth Reddyf92363d2012-11-30 07:44:21 +05305046
5047 if (r != 0) {
5048 pr_err(MPT3SAS_FMT "%s: handshake failed (r=%d)\n",
5049 ioc->name, __func__, r);
5050 return r;
5051 }
5052
5053 facts = &ioc->facts;
5054 memset(facts, 0, sizeof(struct mpt3sas_facts));
5055 facts->MsgVersion = le16_to_cpu(mpi_reply.MsgVersion);
5056 facts->HeaderVersion = le16_to_cpu(mpi_reply.HeaderVersion);
5057 facts->VP_ID = mpi_reply.VP_ID;
5058 facts->VF_ID = mpi_reply.VF_ID;
5059 facts->IOCExceptions = le16_to_cpu(mpi_reply.IOCExceptions);
5060 facts->MaxChainDepth = mpi_reply.MaxChainDepth;
5061 facts->WhoInit = mpi_reply.WhoInit;
5062 facts->NumberOfPorts = mpi_reply.NumberOfPorts;
5063 facts->MaxMSIxVectors = mpi_reply.MaxMSIxVectors;
5064 facts->RequestCredit = le16_to_cpu(mpi_reply.RequestCredit);
5065 facts->MaxReplyDescriptorPostQueueDepth =
5066 le16_to_cpu(mpi_reply.MaxReplyDescriptorPostQueueDepth);
5067 facts->ProductID = le16_to_cpu(mpi_reply.ProductID);
5068 facts->IOCCapabilities = le32_to_cpu(mpi_reply.IOCCapabilities);
5069 if ((facts->IOCCapabilities & MPI2_IOCFACTS_CAPABILITY_INTEGRATED_RAID))
5070 ioc->ir_firmware = 1;
Sreekanth Reddy9b05c912014-09-12 15:35:31 +05305071 if ((facts->IOCCapabilities &
Sreekanth Reddy06f5f972017-10-10 18:41:16 +05305072 MPI2_IOCFACTS_CAPABILITY_RDPQ_ARRAY_CAPABLE) && (!reset_devices))
Sreekanth Reddy9b05c912014-09-12 15:35:31 +05305073 ioc->rdpq_array_capable = 1;
Suganath Prabu Subramani81c16f82016-10-26 13:34:40 +05305074 if (facts->IOCCapabilities & MPI26_IOCFACTS_CAPABILITY_ATOMIC_REQ)
5075 ioc->atomic_desc_capable = 1;
Sreekanth Reddyf92363d2012-11-30 07:44:21 +05305076 facts->FWVersion.Word = le32_to_cpu(mpi_reply.FWVersion.Word);
5077 facts->IOCRequestFrameSize =
5078 le16_to_cpu(mpi_reply.IOCRequestFrameSize);
Suganath prabu Subramaniebb30242016-01-28 12:07:04 +05305079 if (ioc->hba_mpi_version_belonged != MPI2_VERSION) {
5080 facts->IOCMaxChainSegmentSize =
5081 le16_to_cpu(mpi_reply.IOCMaxChainSegmentSize);
5082 }
Sreekanth Reddyf92363d2012-11-30 07:44:21 +05305083 facts->MaxInitiators = le16_to_cpu(mpi_reply.MaxInitiators);
5084 facts->MaxTargets = le16_to_cpu(mpi_reply.MaxTargets);
5085 ioc->shost->max_id = -1;
5086 facts->MaxSasExpanders = le16_to_cpu(mpi_reply.MaxSasExpanders);
5087 facts->MaxEnclosures = le16_to_cpu(mpi_reply.MaxEnclosures);
5088 facts->ProtocolFlags = le16_to_cpu(mpi_reply.ProtocolFlags);
5089 facts->HighPriorityCredit =
5090 le16_to_cpu(mpi_reply.HighPriorityCredit);
5091 facts->ReplyFrameSize = mpi_reply.ReplyFrameSize;
5092 facts->MaxDevHandle = le16_to_cpu(mpi_reply.MaxDevHandle);
Suganath Prabu Subramani016d5c32017-10-31 18:02:28 +05305093 facts->CurrentHostPageSize = mpi_reply.CurrentHostPageSize;
5094
5095 /*
5096 * Get the Page Size from IOC Facts. If it's 0, default to 4k.
5097 */
5098 ioc->page_size = 1 << facts->CurrentHostPageSize;
5099 if (ioc->page_size == 1) {
5100 pr_info(MPT3SAS_FMT "CurrentHostPageSize is 0: Setting "
5101 "default host page size to 4k\n", ioc->name);
5102 ioc->page_size = 1 << MPT3SAS_HOST_PAGE_SIZE_4K;
5103 }
5104 dinitprintk(ioc, pr_info(MPT3SAS_FMT "CurrentHostPageSize(%d)\n",
5105 ioc->name, facts->CurrentHostPageSize));
Sreekanth Reddyf92363d2012-11-30 07:44:21 +05305106
5107 dinitprintk(ioc, pr_info(MPT3SAS_FMT
5108 "hba queue depth(%d), max chains per io(%d)\n",
5109 ioc->name, facts->RequestCredit,
5110 facts->MaxChainDepth));
5111 dinitprintk(ioc, pr_info(MPT3SAS_FMT
5112 "request frame size(%d), reply frame size(%d)\n", ioc->name,
5113 facts->IOCRequestFrameSize * 4, facts->ReplyFrameSize * 4));
5114 return 0;
5115}
5116
5117/**
5118 * _base_send_ioc_init - send ioc_init to firmware
5119 * @ioc: per adapter object
Sreekanth Reddyf92363d2012-11-30 07:44:21 +05305120 *
5121 * Returns 0 for success, non-zero for failure.
5122 */
5123static int
Calvin Owens98c56ad2016-07-28 21:38:21 -07005124_base_send_ioc_init(struct MPT3SAS_ADAPTER *ioc)
Sreekanth Reddyf92363d2012-11-30 07:44:21 +05305125{
5126 Mpi2IOCInitRequest_t mpi_request;
5127 Mpi2IOCInitReply_t mpi_reply;
Sreekanth Reddy9b05c912014-09-12 15:35:31 +05305128 int i, r = 0;
Tina Ruchandani23409bd2016-04-13 00:01:40 -07005129 ktime_t current_time;
Sreekanth Reddyf92363d2012-11-30 07:44:21 +05305130 u16 ioc_status;
Sreekanth Reddy9b05c912014-09-12 15:35:31 +05305131 u32 reply_post_free_array_sz = 0;
5132 Mpi2IOCInitRDPQArrayEntry *reply_post_free_array = NULL;
5133 dma_addr_t reply_post_free_array_dma;
Sreekanth Reddyf92363d2012-11-30 07:44:21 +05305134
5135 dinitprintk(ioc, pr_info(MPT3SAS_FMT "%s\n", ioc->name,
5136 __func__));
5137
5138 memset(&mpi_request, 0, sizeof(Mpi2IOCInitRequest_t));
5139 mpi_request.Function = MPI2_FUNCTION_IOC_INIT;
5140 mpi_request.WhoInit = MPI2_WHOINIT_HOST_DRIVER;
5141 mpi_request.VF_ID = 0; /* TODO */
5142 mpi_request.VP_ID = 0;
Sreekanth Reddyd357e842015-11-11 17:30:22 +05305143 mpi_request.MsgVersion = cpu_to_le16(ioc->hba_mpi_version_belonged);
Sreekanth Reddyf92363d2012-11-30 07:44:21 +05305144 mpi_request.HeaderVersion = cpu_to_le16(MPI2_HEADER_VERSION);
Suganath Prabu Subramani016d5c32017-10-31 18:02:28 +05305145 mpi_request.HostPageSize = MPT3SAS_HOST_PAGE_SIZE_4K;
Sreekanth Reddyf92363d2012-11-30 07:44:21 +05305146
5147 if (_base_is_controller_msix_enabled(ioc))
5148 mpi_request.HostMSIxVectors = ioc->reply_queue_count;
5149 mpi_request.SystemRequestFrameSize = cpu_to_le16(ioc->request_sz/4);
5150 mpi_request.ReplyDescriptorPostQueueDepth =
5151 cpu_to_le16(ioc->reply_post_queue_depth);
5152 mpi_request.ReplyFreeQueueDepth =
5153 cpu_to_le16(ioc->reply_free_queue_depth);
5154
5155 mpi_request.SenseBufferAddressHigh =
5156 cpu_to_le32((u64)ioc->sense_dma >> 32);
5157 mpi_request.SystemReplyAddressHigh =
5158 cpu_to_le32((u64)ioc->reply_dma >> 32);
5159 mpi_request.SystemRequestFrameBaseAddress =
5160 cpu_to_le64((u64)ioc->request_dma);
5161 mpi_request.ReplyFreeQueueAddress =
5162 cpu_to_le64((u64)ioc->reply_free_dma);
Sreekanth Reddyf92363d2012-11-30 07:44:21 +05305163
Sreekanth Reddy9b05c912014-09-12 15:35:31 +05305164 if (ioc->rdpq_array_enable) {
5165 reply_post_free_array_sz = ioc->reply_queue_count *
5166 sizeof(Mpi2IOCInitRDPQArrayEntry);
5167 reply_post_free_array = pci_alloc_consistent(ioc->pdev,
5168 reply_post_free_array_sz, &reply_post_free_array_dma);
5169 if (!reply_post_free_array) {
5170 pr_err(MPT3SAS_FMT
5171 "reply_post_free_array: pci_alloc_consistent failed\n",
5172 ioc->name);
5173 r = -ENOMEM;
5174 goto out;
5175 }
5176 memset(reply_post_free_array, 0, reply_post_free_array_sz);
5177 for (i = 0; i < ioc->reply_queue_count; i++)
5178 reply_post_free_array[i].RDPQBaseAddress =
5179 cpu_to_le64(
5180 (u64)ioc->reply_post[i].reply_post_free_dma);
5181 mpi_request.MsgFlags = MPI2_IOCINIT_MSGFLAG_RDPQ_ARRAY_MODE;
5182 mpi_request.ReplyDescriptorPostQueueAddress =
5183 cpu_to_le64((u64)reply_post_free_array_dma);
5184 } else {
5185 mpi_request.ReplyDescriptorPostQueueAddress =
5186 cpu_to_le64((u64)ioc->reply_post[0].reply_post_free_dma);
5187 }
Sreekanth Reddyf92363d2012-11-30 07:44:21 +05305188
5189 /* This time stamp specifies number of milliseconds
5190 * since epoch ~ midnight January 1, 1970.
5191 */
Tina Ruchandani23409bd2016-04-13 00:01:40 -07005192 current_time = ktime_get_real();
5193 mpi_request.TimeStamp = cpu_to_le64(ktime_to_ms(current_time));
Sreekanth Reddyf92363d2012-11-30 07:44:21 +05305194
5195 if (ioc->logging_level & MPT_DEBUG_INIT) {
5196 __le32 *mfp;
5197 int i;
5198
5199 mfp = (__le32 *)&mpi_request;
5200 pr_info("\toffset:data\n");
5201 for (i = 0; i < sizeof(Mpi2IOCInitRequest_t)/4; i++)
5202 pr_info("\t[0x%02x]:%08x\n", i*4,
5203 le32_to_cpu(mfp[i]));
5204 }
5205
5206 r = _base_handshake_req_reply_wait(ioc,
5207 sizeof(Mpi2IOCInitRequest_t), (u32 *)&mpi_request,
Calvin Owens98c56ad2016-07-28 21:38:21 -07005208 sizeof(Mpi2IOCInitReply_t), (u16 *)&mpi_reply, 10);
Sreekanth Reddyf92363d2012-11-30 07:44:21 +05305209
5210 if (r != 0) {
5211 pr_err(MPT3SAS_FMT "%s: handshake failed (r=%d)\n",
5212 ioc->name, __func__, r);
Sreekanth Reddy9b05c912014-09-12 15:35:31 +05305213 goto out;
Sreekanth Reddyf92363d2012-11-30 07:44:21 +05305214 }
5215
5216 ioc_status = le16_to_cpu(mpi_reply.IOCStatus) & MPI2_IOCSTATUS_MASK;
5217 if (ioc_status != MPI2_IOCSTATUS_SUCCESS ||
5218 mpi_reply.IOCLogInfo) {
5219 pr_err(MPT3SAS_FMT "%s: failed\n", ioc->name, __func__);
5220 r = -EIO;
5221 }
5222
Sreekanth Reddy9b05c912014-09-12 15:35:31 +05305223out:
5224 if (reply_post_free_array)
5225 pci_free_consistent(ioc->pdev, reply_post_free_array_sz,
5226 reply_post_free_array,
5227 reply_post_free_array_dma);
5228 return r;
Sreekanth Reddyf92363d2012-11-30 07:44:21 +05305229}
5230
5231/**
5232 * mpt3sas_port_enable_done - command completion routine for port enable
5233 * @ioc: per adapter object
5234 * @smid: system request message index
5235 * @msix_index: MSIX table index supplied by the OS
5236 * @reply: reply message frame(lower 32bit addr)
5237 *
5238 * Return 1 meaning mf should be freed from _base_interrupt
5239 * 0 means the mf is freed from this function.
5240 */
5241u8
5242mpt3sas_port_enable_done(struct MPT3SAS_ADAPTER *ioc, u16 smid, u8 msix_index,
5243 u32 reply)
5244{
5245 MPI2DefaultReply_t *mpi_reply;
5246 u16 ioc_status;
5247
5248 if (ioc->port_enable_cmds.status == MPT3_CMD_NOT_USED)
5249 return 1;
5250
5251 mpi_reply = mpt3sas_base_get_reply_virt_addr(ioc, reply);
5252 if (!mpi_reply)
5253 return 1;
5254
5255 if (mpi_reply->Function != MPI2_FUNCTION_PORT_ENABLE)
5256 return 1;
5257
5258 ioc->port_enable_cmds.status &= ~MPT3_CMD_PENDING;
5259 ioc->port_enable_cmds.status |= MPT3_CMD_COMPLETE;
5260 ioc->port_enable_cmds.status |= MPT3_CMD_REPLY_VALID;
5261 memcpy(ioc->port_enable_cmds.reply, mpi_reply, mpi_reply->MsgLength*4);
5262 ioc_status = le16_to_cpu(mpi_reply->IOCStatus) & MPI2_IOCSTATUS_MASK;
5263 if (ioc_status != MPI2_IOCSTATUS_SUCCESS)
5264 ioc->port_enable_failed = 1;
5265
5266 if (ioc->is_driver_loading) {
5267 if (ioc_status == MPI2_IOCSTATUS_SUCCESS) {
5268 mpt3sas_port_enable_complete(ioc);
5269 return 1;
5270 } else {
5271 ioc->start_scan_failed = ioc_status;
5272 ioc->start_scan = 0;
5273 return 1;
5274 }
5275 }
5276 complete(&ioc->port_enable_cmds.done);
5277 return 1;
5278}
5279
5280/**
5281 * _base_send_port_enable - send port_enable(discovery stuff) to firmware
5282 * @ioc: per adapter object
Sreekanth Reddyf92363d2012-11-30 07:44:21 +05305283 *
5284 * Returns 0 for success, non-zero for failure.
5285 */
5286static int
Calvin Owens98c56ad2016-07-28 21:38:21 -07005287_base_send_port_enable(struct MPT3SAS_ADAPTER *ioc)
Sreekanth Reddyf92363d2012-11-30 07:44:21 +05305288{
5289 Mpi2PortEnableRequest_t *mpi_request;
5290 Mpi2PortEnableReply_t *mpi_reply;
Sreekanth Reddyf92363d2012-11-30 07:44:21 +05305291 int r = 0;
5292 u16 smid;
5293 u16 ioc_status;
5294
5295 pr_info(MPT3SAS_FMT "sending port enable !!\n", ioc->name);
5296
5297 if (ioc->port_enable_cmds.status & MPT3_CMD_PENDING) {
5298 pr_err(MPT3SAS_FMT "%s: internal command already in use\n",
5299 ioc->name, __func__);
5300 return -EAGAIN;
5301 }
5302
5303 smid = mpt3sas_base_get_smid(ioc, ioc->port_enable_cb_idx);
5304 if (!smid) {
5305 pr_err(MPT3SAS_FMT "%s: failed obtaining a smid\n",
5306 ioc->name, __func__);
5307 return -EAGAIN;
5308 }
5309
5310 ioc->port_enable_cmds.status = MPT3_CMD_PENDING;
5311 mpi_request = mpt3sas_base_get_msg_frame(ioc, smid);
5312 ioc->port_enable_cmds.smid = smid;
5313 memset(mpi_request, 0, sizeof(Mpi2PortEnableRequest_t));
5314 mpi_request->Function = MPI2_FUNCTION_PORT_ENABLE;
5315
5316 init_completion(&ioc->port_enable_cmds.done);
Suganath Prabu Subramani81c16f82016-10-26 13:34:40 +05305317 ioc->put_smid_default(ioc, smid);
Calvin Owens8bbb1cf2016-07-28 21:38:22 -07005318 wait_for_completion_timeout(&ioc->port_enable_cmds.done, 300*HZ);
Sreekanth Reddyf92363d2012-11-30 07:44:21 +05305319 if (!(ioc->port_enable_cmds.status & MPT3_CMD_COMPLETE)) {
5320 pr_err(MPT3SAS_FMT "%s: timeout\n",
5321 ioc->name, __func__);
5322 _debug_dump_mf(mpi_request,
5323 sizeof(Mpi2PortEnableRequest_t)/4);
5324 if (ioc->port_enable_cmds.status & MPT3_CMD_RESET)
5325 r = -EFAULT;
5326 else
5327 r = -ETIME;
5328 goto out;
5329 }
5330
5331 mpi_reply = ioc->port_enable_cmds.reply;
5332 ioc_status = le16_to_cpu(mpi_reply->IOCStatus) & MPI2_IOCSTATUS_MASK;
5333 if (ioc_status != MPI2_IOCSTATUS_SUCCESS) {
5334 pr_err(MPT3SAS_FMT "%s: failed with (ioc_status=0x%08x)\n",
5335 ioc->name, __func__, ioc_status);
5336 r = -EFAULT;
5337 goto out;
5338 }
5339
5340 out:
5341 ioc->port_enable_cmds.status = MPT3_CMD_NOT_USED;
5342 pr_info(MPT3SAS_FMT "port enable: %s\n", ioc->name, ((r == 0) ?
5343 "SUCCESS" : "FAILED"));
5344 return r;
5345}
5346
5347/**
5348 * mpt3sas_port_enable - initiate firmware discovery (don't wait for reply)
5349 * @ioc: per adapter object
5350 *
5351 * Returns 0 for success, non-zero for failure.
5352 */
5353int
5354mpt3sas_port_enable(struct MPT3SAS_ADAPTER *ioc)
5355{
5356 Mpi2PortEnableRequest_t *mpi_request;
5357 u16 smid;
5358
5359 pr_info(MPT3SAS_FMT "sending port enable !!\n", ioc->name);
5360
5361 if (ioc->port_enable_cmds.status & MPT3_CMD_PENDING) {
5362 pr_err(MPT3SAS_FMT "%s: internal command already in use\n",
5363 ioc->name, __func__);
5364 return -EAGAIN;
5365 }
5366
5367 smid = mpt3sas_base_get_smid(ioc, ioc->port_enable_cb_idx);
5368 if (!smid) {
5369 pr_err(MPT3SAS_FMT "%s: failed obtaining a smid\n",
5370 ioc->name, __func__);
5371 return -EAGAIN;
5372 }
5373
5374 ioc->port_enable_cmds.status = MPT3_CMD_PENDING;
5375 mpi_request = mpt3sas_base_get_msg_frame(ioc, smid);
5376 ioc->port_enable_cmds.smid = smid;
5377 memset(mpi_request, 0, sizeof(Mpi2PortEnableRequest_t));
5378 mpi_request->Function = MPI2_FUNCTION_PORT_ENABLE;
5379
Suganath Prabu Subramani81c16f82016-10-26 13:34:40 +05305380 ioc->put_smid_default(ioc, smid);
Sreekanth Reddyf92363d2012-11-30 07:44:21 +05305381 return 0;
5382}
5383
5384/**
5385 * _base_determine_wait_on_discovery - desposition
5386 * @ioc: per adapter object
5387 *
5388 * Decide whether to wait on discovery to complete. Used to either
5389 * locate boot device, or report volumes ahead of physical devices.
5390 *
5391 * Returns 1 for wait, 0 for don't wait
5392 */
5393static int
5394_base_determine_wait_on_discovery(struct MPT3SAS_ADAPTER *ioc)
5395{
5396 /* We wait for discovery to complete if IR firmware is loaded.
5397 * The sas topology events arrive before PD events, so we need time to
5398 * turn on the bit in ioc->pd_handles to indicate PD
5399 * Also, it maybe required to report Volumes ahead of physical
5400 * devices when MPI2_IOCPAGE8_IRFLAGS_LOW_VOLUME_MAPPING is set.
5401 */
5402 if (ioc->ir_firmware)
5403 return 1;
5404
5405 /* if no Bios, then we don't need to wait */
5406 if (!ioc->bios_pg3.BiosVersion)
5407 return 0;
5408
5409 /* Bios is present, then we drop down here.
5410 *
5411 * If there any entries in the Bios Page 2, then we wait
5412 * for discovery to complete.
5413 */
5414
5415 /* Current Boot Device */
5416 if ((ioc->bios_pg2.CurrentBootDeviceForm &
5417 MPI2_BIOSPAGE2_FORM_MASK) ==
5418 MPI2_BIOSPAGE2_FORM_NO_DEVICE_SPECIFIED &&
5419 /* Request Boot Device */
5420 (ioc->bios_pg2.ReqBootDeviceForm &
5421 MPI2_BIOSPAGE2_FORM_MASK) ==
5422 MPI2_BIOSPAGE2_FORM_NO_DEVICE_SPECIFIED &&
5423 /* Alternate Request Boot Device */
5424 (ioc->bios_pg2.ReqAltBootDeviceForm &
5425 MPI2_BIOSPAGE2_FORM_MASK) ==
5426 MPI2_BIOSPAGE2_FORM_NO_DEVICE_SPECIFIED)
5427 return 0;
5428
5429 return 1;
5430}
5431
5432/**
5433 * _base_unmask_events - turn on notification for this event
5434 * @ioc: per adapter object
5435 * @event: firmware event
5436 *
5437 * The mask is stored in ioc->event_masks.
5438 */
5439static void
5440_base_unmask_events(struct MPT3SAS_ADAPTER *ioc, u16 event)
5441{
5442 u32 desired_event;
5443
5444 if (event >= 128)
5445 return;
5446
5447 desired_event = (1 << (event % 32));
5448
5449 if (event < 32)
5450 ioc->event_masks[0] &= ~desired_event;
5451 else if (event < 64)
5452 ioc->event_masks[1] &= ~desired_event;
5453 else if (event < 96)
5454 ioc->event_masks[2] &= ~desired_event;
5455 else if (event < 128)
5456 ioc->event_masks[3] &= ~desired_event;
5457}
5458
5459/**
5460 * _base_event_notification - send event notification
5461 * @ioc: per adapter object
Sreekanth Reddyf92363d2012-11-30 07:44:21 +05305462 *
5463 * Returns 0 for success, non-zero for failure.
5464 */
5465static int
Calvin Owens98c56ad2016-07-28 21:38:21 -07005466_base_event_notification(struct MPT3SAS_ADAPTER *ioc)
Sreekanth Reddyf92363d2012-11-30 07:44:21 +05305467{
5468 Mpi2EventNotificationRequest_t *mpi_request;
Sreekanth Reddyf92363d2012-11-30 07:44:21 +05305469 u16 smid;
5470 int r = 0;
5471 int i;
5472
5473 dinitprintk(ioc, pr_info(MPT3SAS_FMT "%s\n", ioc->name,
5474 __func__));
5475
5476 if (ioc->base_cmds.status & MPT3_CMD_PENDING) {
5477 pr_err(MPT3SAS_FMT "%s: internal command already in use\n",
5478 ioc->name, __func__);
5479 return -EAGAIN;
5480 }
5481
5482 smid = mpt3sas_base_get_smid(ioc, ioc->base_cb_idx);
5483 if (!smid) {
5484 pr_err(MPT3SAS_FMT "%s: failed obtaining a smid\n",
5485 ioc->name, __func__);
5486 return -EAGAIN;
5487 }
5488 ioc->base_cmds.status = MPT3_CMD_PENDING;
5489 mpi_request = mpt3sas_base_get_msg_frame(ioc, smid);
5490 ioc->base_cmds.smid = smid;
5491 memset(mpi_request, 0, sizeof(Mpi2EventNotificationRequest_t));
5492 mpi_request->Function = MPI2_FUNCTION_EVENT_NOTIFICATION;
5493 mpi_request->VF_ID = 0; /* TODO */
5494 mpi_request->VP_ID = 0;
5495 for (i = 0; i < MPI2_EVENT_NOTIFY_EVENTMASK_WORDS; i++)
5496 mpi_request->EventMasks[i] =
5497 cpu_to_le32(ioc->event_masks[i]);
5498 init_completion(&ioc->base_cmds.done);
Suganath Prabu Subramani81c16f82016-10-26 13:34:40 +05305499 ioc->put_smid_default(ioc, smid);
Calvin Owens8bbb1cf2016-07-28 21:38:22 -07005500 wait_for_completion_timeout(&ioc->base_cmds.done, 30*HZ);
Sreekanth Reddyf92363d2012-11-30 07:44:21 +05305501 if (!(ioc->base_cmds.status & MPT3_CMD_COMPLETE)) {
5502 pr_err(MPT3SAS_FMT "%s: timeout\n",
5503 ioc->name, __func__);
5504 _debug_dump_mf(mpi_request,
5505 sizeof(Mpi2EventNotificationRequest_t)/4);
5506 if (ioc->base_cmds.status & MPT3_CMD_RESET)
5507 r = -EFAULT;
5508 else
5509 r = -ETIME;
5510 } else
5511 dinitprintk(ioc, pr_info(MPT3SAS_FMT "%s: complete\n",
5512 ioc->name, __func__));
5513 ioc->base_cmds.status = MPT3_CMD_NOT_USED;
5514 return r;
5515}
5516
5517/**
5518 * mpt3sas_base_validate_event_type - validating event types
5519 * @ioc: per adapter object
5520 * @event: firmware event
5521 *
5522 * This will turn on firmware event notification when application
5523 * ask for that event. We don't mask events that are already enabled.
5524 */
5525void
5526mpt3sas_base_validate_event_type(struct MPT3SAS_ADAPTER *ioc, u32 *event_type)
5527{
5528 int i, j;
5529 u32 event_mask, desired_event;
5530 u8 send_update_to_fw;
5531
5532 for (i = 0, send_update_to_fw = 0; i <
5533 MPI2_EVENT_NOTIFY_EVENTMASK_WORDS; i++) {
5534 event_mask = ~event_type[i];
5535 desired_event = 1;
5536 for (j = 0; j < 32; j++) {
5537 if (!(event_mask & desired_event) &&
5538 (ioc->event_masks[i] & desired_event)) {
5539 ioc->event_masks[i] &= ~desired_event;
5540 send_update_to_fw = 1;
5541 }
5542 desired_event = (desired_event << 1);
5543 }
5544 }
5545
5546 if (!send_update_to_fw)
5547 return;
5548
5549 mutex_lock(&ioc->base_cmds.mutex);
Calvin Owens98c56ad2016-07-28 21:38:21 -07005550 _base_event_notification(ioc);
Sreekanth Reddyf92363d2012-11-30 07:44:21 +05305551 mutex_unlock(&ioc->base_cmds.mutex);
5552}
5553
5554/**
5555 * _base_diag_reset - the "big hammer" start of day reset
5556 * @ioc: per adapter object
Sreekanth Reddyf92363d2012-11-30 07:44:21 +05305557 *
5558 * Returns 0 for success, non-zero for failure.
5559 */
5560static int
Calvin Owens98c56ad2016-07-28 21:38:21 -07005561_base_diag_reset(struct MPT3SAS_ADAPTER *ioc)
Sreekanth Reddyf92363d2012-11-30 07:44:21 +05305562{
5563 u32 host_diagnostic;
5564 u32 ioc_state;
5565 u32 count;
5566 u32 hcb_size;
5567
5568 pr_info(MPT3SAS_FMT "sending diag reset !!\n", ioc->name);
5569
5570 drsprintk(ioc, pr_info(MPT3SAS_FMT "clear interrupts\n",
5571 ioc->name));
5572
5573 count = 0;
5574 do {
5575 /* Write magic sequence to WriteSequence register
5576 * Loop until in diagnostic mode
5577 */
5578 drsprintk(ioc, pr_info(MPT3SAS_FMT
5579 "write magic sequence\n", ioc->name));
5580 writel(MPI2_WRSEQ_FLUSH_KEY_VALUE, &ioc->chip->WriteSequence);
5581 writel(MPI2_WRSEQ_1ST_KEY_VALUE, &ioc->chip->WriteSequence);
5582 writel(MPI2_WRSEQ_2ND_KEY_VALUE, &ioc->chip->WriteSequence);
5583 writel(MPI2_WRSEQ_3RD_KEY_VALUE, &ioc->chip->WriteSequence);
5584 writel(MPI2_WRSEQ_4TH_KEY_VALUE, &ioc->chip->WriteSequence);
5585 writel(MPI2_WRSEQ_5TH_KEY_VALUE, &ioc->chip->WriteSequence);
5586 writel(MPI2_WRSEQ_6TH_KEY_VALUE, &ioc->chip->WriteSequence);
5587
5588 /* wait 100 msec */
Calvin Owens98c56ad2016-07-28 21:38:21 -07005589 msleep(100);
Sreekanth Reddyf92363d2012-11-30 07:44:21 +05305590
5591 if (count++ > 20)
5592 goto out;
5593
5594 host_diagnostic = readl(&ioc->chip->HostDiagnostic);
5595 drsprintk(ioc, pr_info(MPT3SAS_FMT
5596 "wrote magic sequence: count(%d), host_diagnostic(0x%08x)\n",
5597 ioc->name, count, host_diagnostic));
5598
5599 } while ((host_diagnostic & MPI2_DIAG_DIAG_WRITE_ENABLE) == 0);
5600
5601 hcb_size = readl(&ioc->chip->HCBSize);
5602
5603 drsprintk(ioc, pr_info(MPT3SAS_FMT "diag reset: issued\n",
5604 ioc->name));
5605 writel(host_diagnostic | MPI2_DIAG_RESET_ADAPTER,
5606 &ioc->chip->HostDiagnostic);
5607
Sreekanth Reddyb453ff82013-06-29 03:51:19 +05305608 /*This delay allows the chip PCIe hardware time to finish reset tasks*/
Calvin Owens98c56ad2016-07-28 21:38:21 -07005609 msleep(MPI2_HARD_RESET_PCIE_FIRST_READ_DELAY_MICRO_SEC/1000);
Sreekanth Reddyf92363d2012-11-30 07:44:21 +05305610
Sreekanth Reddyb453ff82013-06-29 03:51:19 +05305611 /* Approximately 300 second max wait */
5612 for (count = 0; count < (300000000 /
5613 MPI2_HARD_RESET_PCIE_SECOND_READ_DELAY_MICRO_SEC); count++) {
Sreekanth Reddyf92363d2012-11-30 07:44:21 +05305614
5615 host_diagnostic = readl(&ioc->chip->HostDiagnostic);
5616
5617 if (host_diagnostic == 0xFFFFFFFF)
5618 goto out;
5619 if (!(host_diagnostic & MPI2_DIAG_RESET_ADAPTER))
5620 break;
5621
Calvin Owens98c56ad2016-07-28 21:38:21 -07005622 msleep(MPI2_HARD_RESET_PCIE_SECOND_READ_DELAY_MICRO_SEC / 1000);
Sreekanth Reddyf92363d2012-11-30 07:44:21 +05305623 }
5624
5625 if (host_diagnostic & MPI2_DIAG_HCB_MODE) {
5626
5627 drsprintk(ioc, pr_info(MPT3SAS_FMT
5628 "restart the adapter assuming the HCB Address points to good F/W\n",
5629 ioc->name));
5630 host_diagnostic &= ~MPI2_DIAG_BOOT_DEVICE_SELECT_MASK;
5631 host_diagnostic |= MPI2_DIAG_BOOT_DEVICE_SELECT_HCDW;
5632 writel(host_diagnostic, &ioc->chip->HostDiagnostic);
5633
5634 drsprintk(ioc, pr_info(MPT3SAS_FMT
5635 "re-enable the HCDW\n", ioc->name));
5636 writel(hcb_size | MPI2_HCB_SIZE_HCB_ENABLE,
5637 &ioc->chip->HCBSize);
5638 }
5639
5640 drsprintk(ioc, pr_info(MPT3SAS_FMT "restart the adapter\n",
5641 ioc->name));
5642 writel(host_diagnostic & ~MPI2_DIAG_HOLD_IOC_RESET,
5643 &ioc->chip->HostDiagnostic);
5644
5645 drsprintk(ioc, pr_info(MPT3SAS_FMT
5646 "disable writes to the diagnostic register\n", ioc->name));
5647 writel(MPI2_WRSEQ_FLUSH_KEY_VALUE, &ioc->chip->WriteSequence);
5648
5649 drsprintk(ioc, pr_info(MPT3SAS_FMT
5650 "Wait for FW to go to the READY state\n", ioc->name));
Calvin Owens98c56ad2016-07-28 21:38:21 -07005651 ioc_state = _base_wait_on_iocstate(ioc, MPI2_IOC_STATE_READY, 20);
Sreekanth Reddyf92363d2012-11-30 07:44:21 +05305652 if (ioc_state) {
5653 pr_err(MPT3SAS_FMT
5654 "%s: failed going to ready state (ioc_state=0x%x)\n",
5655 ioc->name, __func__, ioc_state);
5656 goto out;
5657 }
5658
5659 pr_info(MPT3SAS_FMT "diag reset: SUCCESS\n", ioc->name);
5660 return 0;
5661
5662 out:
5663 pr_err(MPT3SAS_FMT "diag reset: FAILED\n", ioc->name);
5664 return -EFAULT;
5665}
5666
5667/**
5668 * _base_make_ioc_ready - put controller in READY state
5669 * @ioc: per adapter object
Sreekanth Reddyf92363d2012-11-30 07:44:21 +05305670 * @type: FORCE_BIG_HAMMER or SOFT_RESET
5671 *
5672 * Returns 0 for success, non-zero for failure.
5673 */
5674static int
Calvin Owens98c56ad2016-07-28 21:38:21 -07005675_base_make_ioc_ready(struct MPT3SAS_ADAPTER *ioc, enum reset_type type)
Sreekanth Reddyf92363d2012-11-30 07:44:21 +05305676{
5677 u32 ioc_state;
5678 int rc;
5679 int count;
5680
5681 dinitprintk(ioc, pr_info(MPT3SAS_FMT "%s\n", ioc->name,
5682 __func__));
5683
5684 if (ioc->pci_error_recovery)
5685 return 0;
5686
5687 ioc_state = mpt3sas_base_get_iocstate(ioc, 0);
5688 dhsprintk(ioc, pr_info(MPT3SAS_FMT "%s: ioc_state(0x%08x)\n",
5689 ioc->name, __func__, ioc_state));
5690
5691 /* if in RESET state, it should move to READY state shortly */
5692 count = 0;
5693 if ((ioc_state & MPI2_IOC_STATE_MASK) == MPI2_IOC_STATE_RESET) {
5694 while ((ioc_state & MPI2_IOC_STATE_MASK) !=
5695 MPI2_IOC_STATE_READY) {
5696 if (count++ == 10) {
5697 pr_err(MPT3SAS_FMT
5698 "%s: failed going to ready state (ioc_state=0x%x)\n",
5699 ioc->name, __func__, ioc_state);
5700 return -EFAULT;
5701 }
Calvin Owens98c56ad2016-07-28 21:38:21 -07005702 ssleep(1);
Sreekanth Reddyf92363d2012-11-30 07:44:21 +05305703 ioc_state = mpt3sas_base_get_iocstate(ioc, 0);
5704 }
5705 }
5706
5707 if ((ioc_state & MPI2_IOC_STATE_MASK) == MPI2_IOC_STATE_READY)
5708 return 0;
5709
5710 if (ioc_state & MPI2_DOORBELL_USED) {
5711 dhsprintk(ioc, pr_info(MPT3SAS_FMT
5712 "unexpected doorbell active!\n",
5713 ioc->name));
5714 goto issue_diag_reset;
5715 }
5716
5717 if ((ioc_state & MPI2_IOC_STATE_MASK) == MPI2_IOC_STATE_FAULT) {
5718 mpt3sas_base_fault_info(ioc, ioc_state &
5719 MPI2_DOORBELL_DATA_MASK);
5720 goto issue_diag_reset;
5721 }
5722
5723 if (type == FORCE_BIG_HAMMER)
5724 goto issue_diag_reset;
5725
5726 if ((ioc_state & MPI2_IOC_STATE_MASK) == MPI2_IOC_STATE_OPERATIONAL)
5727 if (!(_base_send_ioc_reset(ioc,
Calvin Owens98c56ad2016-07-28 21:38:21 -07005728 MPI2_FUNCTION_IOC_MESSAGE_UNIT_RESET, 15))) {
Sreekanth Reddyf92363d2012-11-30 07:44:21 +05305729 return 0;
5730 }
5731
5732 issue_diag_reset:
Calvin Owens98c56ad2016-07-28 21:38:21 -07005733 rc = _base_diag_reset(ioc);
Sreekanth Reddyf92363d2012-11-30 07:44:21 +05305734 return rc;
5735}
5736
5737/**
5738 * _base_make_ioc_operational - put controller in OPERATIONAL state
5739 * @ioc: per adapter object
Sreekanth Reddyf92363d2012-11-30 07:44:21 +05305740 *
5741 * Returns 0 for success, non-zero for failure.
5742 */
5743static int
Calvin Owens98c56ad2016-07-28 21:38:21 -07005744_base_make_ioc_operational(struct MPT3SAS_ADAPTER *ioc)
Sreekanth Reddyf92363d2012-11-30 07:44:21 +05305745{
Calvin Owens5ec8a172016-03-18 12:45:42 -07005746 int r, i, index;
Sreekanth Reddyf92363d2012-11-30 07:44:21 +05305747 unsigned long flags;
5748 u32 reply_address;
5749 u16 smid;
5750 struct _tr_list *delayed_tr, *delayed_tr_next;
Suganath prabu Subramanifd0331b2016-01-28 12:07:02 +05305751 struct _sc_list *delayed_sc, *delayed_sc_next;
5752 struct _event_ack_list *delayed_event_ack, *delayed_event_ack_next;
Sreekanth Reddy7786ab62015-11-11 17:30:28 +05305753 u8 hide_flag;
Sreekanth Reddyf92363d2012-11-30 07:44:21 +05305754 struct adapter_reply_queue *reply_q;
Calvin Owens5ec8a172016-03-18 12:45:42 -07005755 Mpi2ReplyDescriptorsUnion_t *reply_post_free_contig;
Sreekanth Reddyf92363d2012-11-30 07:44:21 +05305756
5757 dinitprintk(ioc, pr_info(MPT3SAS_FMT "%s\n", ioc->name,
5758 __func__));
5759
5760 /* clean the delayed target reset list */
5761 list_for_each_entry_safe(delayed_tr, delayed_tr_next,
5762 &ioc->delayed_tr_list, list) {
5763 list_del(&delayed_tr->list);
5764 kfree(delayed_tr);
5765 }
5766
5767
5768 list_for_each_entry_safe(delayed_tr, delayed_tr_next,
5769 &ioc->delayed_tr_volume_list, list) {
5770 list_del(&delayed_tr->list);
5771 kfree(delayed_tr);
5772 }
5773
Suganath prabu Subramanifd0331b2016-01-28 12:07:02 +05305774 list_for_each_entry_safe(delayed_sc, delayed_sc_next,
5775 &ioc->delayed_sc_list, list) {
5776 list_del(&delayed_sc->list);
5777 kfree(delayed_sc);
5778 }
5779
5780 list_for_each_entry_safe(delayed_event_ack, delayed_event_ack_next,
5781 &ioc->delayed_event_ack_list, list) {
5782 list_del(&delayed_event_ack->list);
5783 kfree(delayed_event_ack);
5784 }
5785
Sreekanth Reddyf92363d2012-11-30 07:44:21 +05305786 /* initialize the scsi lookup free list */
5787 spin_lock_irqsave(&ioc->scsi_lookup_lock, flags);
5788 INIT_LIST_HEAD(&ioc->free_list);
5789 smid = 1;
5790 for (i = 0; i < ioc->scsiio_depth; i++, smid++) {
5791 INIT_LIST_HEAD(&ioc->scsi_lookup[i].chain_list);
5792 ioc->scsi_lookup[i].cb_idx = 0xFF;
5793 ioc->scsi_lookup[i].smid = smid;
5794 ioc->scsi_lookup[i].scmd = NULL;
Sreekanth Reddy7786ab62015-11-11 17:30:28 +05305795 ioc->scsi_lookup[i].direct_io = 0;
Hannes Reineckeb0cd285e2018-01-04 04:57:07 -08005796 if (i < ioc->scsiio_depth - INTERNAL_SCSIIO_CMDS_COUNT)
5797 list_add_tail(&ioc->scsi_lookup[i].tracker_list,
5798 &ioc->free_list);
Sreekanth Reddyf92363d2012-11-30 07:44:21 +05305799 }
5800
5801 /* hi-priority queue */
5802 INIT_LIST_HEAD(&ioc->hpr_free_list);
5803 smid = ioc->hi_priority_smid;
5804 for (i = 0; i < ioc->hi_priority_depth; i++, smid++) {
5805 ioc->hpr_lookup[i].cb_idx = 0xFF;
5806 ioc->hpr_lookup[i].smid = smid;
5807 list_add_tail(&ioc->hpr_lookup[i].tracker_list,
5808 &ioc->hpr_free_list);
5809 }
5810
5811 /* internal queue */
5812 INIT_LIST_HEAD(&ioc->internal_free_list);
5813 smid = ioc->internal_smid;
5814 for (i = 0; i < ioc->internal_depth; i++, smid++) {
5815 ioc->internal_lookup[i].cb_idx = 0xFF;
5816 ioc->internal_lookup[i].smid = smid;
5817 list_add_tail(&ioc->internal_lookup[i].tracker_list,
5818 &ioc->internal_free_list);
5819 }
5820
5821 /* chain pool */
5822 INIT_LIST_HEAD(&ioc->free_chain_list);
5823 for (i = 0; i < ioc->chain_depth; i++)
5824 list_add_tail(&ioc->chain_lookup[i].tracker_list,
5825 &ioc->free_chain_list);
5826
5827 spin_unlock_irqrestore(&ioc->scsi_lookup_lock, flags);
5828
5829 /* initialize Reply Free Queue */
5830 for (i = 0, reply_address = (u32)ioc->reply_dma ;
5831 i < ioc->reply_free_queue_depth ; i++, reply_address +=
5832 ioc->reply_sz)
5833 ioc->reply_free[i] = cpu_to_le32(reply_address);
5834
5835 /* initialize reply queues */
5836 if (ioc->is_driver_loading)
5837 _base_assign_reply_queues(ioc);
5838
5839 /* initialize Reply Post Free Queue */
Calvin Owens5ec8a172016-03-18 12:45:42 -07005840 index = 0;
5841 reply_post_free_contig = ioc->reply_post[0].reply_post_free;
Sreekanth Reddyf92363d2012-11-30 07:44:21 +05305842 list_for_each_entry(reply_q, &ioc->reply_queue_list, list) {
Calvin Owens5ec8a172016-03-18 12:45:42 -07005843 /*
5844 * If RDPQ is enabled, switch to the next allocation.
5845 * Otherwise advance within the contiguous region.
5846 */
5847 if (ioc->rdpq_array_enable) {
5848 reply_q->reply_post_free =
5849 ioc->reply_post[index++].reply_post_free;
5850 } else {
5851 reply_q->reply_post_free = reply_post_free_contig;
5852 reply_post_free_contig += ioc->reply_post_queue_depth;
5853 }
5854
Sreekanth Reddyf92363d2012-11-30 07:44:21 +05305855 reply_q->reply_post_host_index = 0;
Sreekanth Reddyf92363d2012-11-30 07:44:21 +05305856 for (i = 0; i < ioc->reply_post_queue_depth; i++)
5857 reply_q->reply_post_free[i].Words =
5858 cpu_to_le64(ULLONG_MAX);
5859 if (!_base_is_controller_msix_enabled(ioc))
5860 goto skip_init_reply_post_free_queue;
Sreekanth Reddyf92363d2012-11-30 07:44:21 +05305861 }
5862 skip_init_reply_post_free_queue:
5863
Calvin Owens98c56ad2016-07-28 21:38:21 -07005864 r = _base_send_ioc_init(ioc);
Sreekanth Reddyf92363d2012-11-30 07:44:21 +05305865 if (r)
5866 return r;
5867
5868 /* initialize reply free host index */
5869 ioc->reply_free_host_index = ioc->reply_free_queue_depth - 1;
5870 writel(ioc->reply_free_host_index, &ioc->chip->ReplyFreeHostIndex);
5871
5872 /* initialize reply post host index */
5873 list_for_each_entry(reply_q, &ioc->reply_queue_list, list) {
Suganath Prabu Subramani0bb337c2016-10-26 13:34:38 +05305874 if (ioc->combined_reply_queue)
Sreekanth Reddyfb77bb52015-06-30 12:24:47 +05305875 writel((reply_q->msix_index & 7)<<
5876 MPI2_RPHI_MSIX_INDEX_SHIFT,
5877 ioc->replyPostRegisterIndex[reply_q->msix_index/8]);
5878 else
5879 writel(reply_q->msix_index <<
5880 MPI2_RPHI_MSIX_INDEX_SHIFT,
5881 &ioc->chip->ReplyPostHostIndex);
5882
Sreekanth Reddyf92363d2012-11-30 07:44:21 +05305883 if (!_base_is_controller_msix_enabled(ioc))
5884 goto skip_init_reply_post_host_index;
5885 }
5886
5887 skip_init_reply_post_host_index:
5888
5889 _base_unmask_interrupts(ioc);
Calvin Owens98c56ad2016-07-28 21:38:21 -07005890 r = _base_event_notification(ioc);
Sreekanth Reddyf92363d2012-11-30 07:44:21 +05305891 if (r)
5892 return r;
5893
Calvin Owens98c56ad2016-07-28 21:38:21 -07005894 _base_static_config_pages(ioc);
Sreekanth Reddyf92363d2012-11-30 07:44:21 +05305895
5896 if (ioc->is_driver_loading) {
Sreekanth Reddy7786ab62015-11-11 17:30:28 +05305897
5898 if (ioc->is_warpdrive && ioc->manu_pg10.OEMIdentifier
5899 == 0x80) {
5900 hide_flag = (u8) (
5901 le32_to_cpu(ioc->manu_pg10.OEMSpecificFlags0) &
5902 MFG_PAGE10_HIDE_SSDS_MASK);
5903 if (hide_flag != MFG_PAGE10_HIDE_SSDS_MASK)
5904 ioc->mfg_pg10_hide_flag = hide_flag;
5905 }
5906
Sreekanth Reddyf92363d2012-11-30 07:44:21 +05305907 ioc->wait_for_discovery_to_complete =
5908 _base_determine_wait_on_discovery(ioc);
5909
5910 return r; /* scan_start and scan_finished support */
5911 }
5912
Calvin Owens98c56ad2016-07-28 21:38:21 -07005913 r = _base_send_port_enable(ioc);
Sreekanth Reddyf92363d2012-11-30 07:44:21 +05305914 if (r)
5915 return r;
5916
5917 return r;
5918}
5919
5920/**
5921 * mpt3sas_base_free_resources - free resources controller resources
5922 * @ioc: per adapter object
5923 *
5924 * Return nothing.
5925 */
5926void
5927mpt3sas_base_free_resources(struct MPT3SAS_ADAPTER *ioc)
5928{
Sreekanth Reddyf92363d2012-11-30 07:44:21 +05305929 dexitprintk(ioc, pr_info(MPT3SAS_FMT "%s\n", ioc->name,
5930 __func__));
5931
Sreekanth Reddy08c4d552015-11-11 17:30:33 +05305932 /* synchronizing freeing resource with pci_access_mutex lock */
5933 mutex_lock(&ioc->pci_access_mutex);
Joe Lawrencecf9bd21a2013-08-08 16:45:39 -04005934 if (ioc->chip_phys && ioc->chip) {
5935 _base_mask_interrupts(ioc);
5936 ioc->shost_recovery = 1;
Calvin Owens98c56ad2016-07-28 21:38:21 -07005937 _base_make_ioc_ready(ioc, SOFT_RESET);
Joe Lawrencecf9bd21a2013-08-08 16:45:39 -04005938 ioc->shost_recovery = 0;
5939 }
5940
Sreekanth Reddy580d4e32015-06-30 12:24:50 +05305941 mpt3sas_base_unmap_resources(ioc);
Sreekanth Reddy08c4d552015-11-11 17:30:33 +05305942 mutex_unlock(&ioc->pci_access_mutex);
Sreekanth Reddyf92363d2012-11-30 07:44:21 +05305943 return;
5944}
5945
5946/**
5947 * mpt3sas_base_attach - attach controller instance
5948 * @ioc: per adapter object
5949 *
5950 * Returns 0 for success, non-zero for failure.
5951 */
5952int
5953mpt3sas_base_attach(struct MPT3SAS_ADAPTER *ioc)
5954{
5955 int r, i;
5956 int cpu_id, last_cpu_id = 0;
5957
5958 dinitprintk(ioc, pr_info(MPT3SAS_FMT "%s\n", ioc->name,
5959 __func__));
5960
5961 /* setup cpu_msix_table */
5962 ioc->cpu_count = num_online_cpus();
5963 for_each_online_cpu(cpu_id)
5964 last_cpu_id = cpu_id;
5965 ioc->cpu_msix_table_sz = last_cpu_id + 1;
5966 ioc->cpu_msix_table = kzalloc(ioc->cpu_msix_table_sz, GFP_KERNEL);
5967 ioc->reply_queue_count = 1;
5968 if (!ioc->cpu_msix_table) {
5969 dfailprintk(ioc, pr_info(MPT3SAS_FMT
5970 "allocation for cpu_msix_table failed!!!\n",
5971 ioc->name));
5972 r = -ENOMEM;
5973 goto out_free_resources;
5974 }
5975
Sreekanth Reddy7786ab62015-11-11 17:30:28 +05305976 if (ioc->is_warpdrive) {
5977 ioc->reply_post_host_index = kcalloc(ioc->cpu_msix_table_sz,
5978 sizeof(resource_size_t *), GFP_KERNEL);
5979 if (!ioc->reply_post_host_index) {
5980 dfailprintk(ioc, pr_info(MPT3SAS_FMT "allocation "
Hannes Reinecke1d55abc2017-02-22 11:31:41 +01005981 "for reply_post_host_index failed!!!\n",
5982 ioc->name));
Sreekanth Reddy7786ab62015-11-11 17:30:28 +05305983 r = -ENOMEM;
5984 goto out_free_resources;
5985 }
5986 }
5987
Sreekanth Reddy9b05c912014-09-12 15:35:31 +05305988 ioc->rdpq_array_enable_assigned = 0;
5989 ioc->dma_mask = 0;
Sreekanth Reddyf92363d2012-11-30 07:44:21 +05305990 r = mpt3sas_base_map_resources(ioc);
5991 if (r)
5992 goto out_free_resources;
5993
Sreekanth Reddyf92363d2012-11-30 07:44:21 +05305994 pci_set_drvdata(ioc->pdev, ioc->shost);
Calvin Owens98c56ad2016-07-28 21:38:21 -07005995 r = _base_get_ioc_facts(ioc);
Sreekanth Reddyf92363d2012-11-30 07:44:21 +05305996 if (r)
5997 goto out_free_resources;
5998
Sreekanth Reddy471ef9d2015-11-11 17:30:24 +05305999 switch (ioc->hba_mpi_version_belonged) {
6000 case MPI2_VERSION:
6001 ioc->build_sg_scmd = &_base_build_sg_scmd;
6002 ioc->build_sg = &_base_build_sg;
6003 ioc->build_zero_len_sge = &_base_build_zero_len_sge;
6004 break;
6005 case MPI25_VERSION:
Suganath prabu Subramanib130b0d2016-01-28 12:06:58 +05306006 case MPI26_VERSION:
Sreekanth Reddy471ef9d2015-11-11 17:30:24 +05306007 /*
6008 * In SAS3.0,
6009 * SCSI_IO, SMP_PASSTHRU, SATA_PASSTHRU, Target Assist, and
6010 * Target Status - all require the IEEE formated scatter gather
6011 * elements.
6012 */
6013 ioc->build_sg_scmd = &_base_build_sg_scmd_ieee;
6014 ioc->build_sg = &_base_build_sg_ieee;
Suganath Prabu Subramaniaff39e62017-10-31 18:02:29 +05306015 ioc->build_nvme_prp = &_base_build_nvme_prp;
Sreekanth Reddy471ef9d2015-11-11 17:30:24 +05306016 ioc->build_zero_len_sge = &_base_build_zero_len_sge_ieee;
6017 ioc->sge_size_ieee = sizeof(Mpi2IeeeSgeSimple64_t);
Suganath Prabu Subramani81c16f82016-10-26 13:34:40 +05306018
Sreekanth Reddy471ef9d2015-11-11 17:30:24 +05306019 break;
6020 }
Sreekanth Reddyf92363d2012-11-30 07:44:21 +05306021
Suganath Prabu Subramani81c16f82016-10-26 13:34:40 +05306022 if (ioc->atomic_desc_capable) {
6023 ioc->put_smid_default = &_base_put_smid_default_atomic;
6024 ioc->put_smid_scsi_io = &_base_put_smid_scsi_io_atomic;
6025 ioc->put_smid_fast_path = &_base_put_smid_fast_path_atomic;
6026 ioc->put_smid_hi_priority = &_base_put_smid_hi_priority_atomic;
Suganath Prabu Subramaniaff39e62017-10-31 18:02:29 +05306027 ioc->put_smid_nvme_encap = &_base_put_smid_nvme_encap_atomic;
Suganath Prabu Subramani81c16f82016-10-26 13:34:40 +05306028 } else {
6029 ioc->put_smid_default = &_base_put_smid_default;
6030 ioc->put_smid_scsi_io = &_base_put_smid_scsi_io;
6031 ioc->put_smid_fast_path = &_base_put_smid_fast_path;
6032 ioc->put_smid_hi_priority = &_base_put_smid_hi_priority;
Suganath Prabu Subramaniaff39e62017-10-31 18:02:29 +05306033 ioc->put_smid_nvme_encap = &_base_put_smid_nvme_encap;
Suganath Prabu Subramani81c16f82016-10-26 13:34:40 +05306034 }
6035
6036
Sreekanth Reddyf92363d2012-11-30 07:44:21 +05306037 /*
6038 * These function pointers for other requests that don't
6039 * the require IEEE scatter gather elements.
6040 *
6041 * For example Configuration Pages and SAS IOUNIT Control don't.
6042 */
6043 ioc->build_sg_mpi = &_base_build_sg;
6044 ioc->build_zero_len_sge_mpi = &_base_build_zero_len_sge;
6045
Calvin Owens98c56ad2016-07-28 21:38:21 -07006046 r = _base_make_ioc_ready(ioc, SOFT_RESET);
Sreekanth Reddyf92363d2012-11-30 07:44:21 +05306047 if (r)
6048 goto out_free_resources;
6049
6050 ioc->pfacts = kcalloc(ioc->facts.NumberOfPorts,
6051 sizeof(struct mpt3sas_port_facts), GFP_KERNEL);
6052 if (!ioc->pfacts) {
6053 r = -ENOMEM;
6054 goto out_free_resources;
6055 }
6056
6057 for (i = 0 ; i < ioc->facts.NumberOfPorts; i++) {
Calvin Owens98c56ad2016-07-28 21:38:21 -07006058 r = _base_get_port_facts(ioc, i);
Sreekanth Reddyf92363d2012-11-30 07:44:21 +05306059 if (r)
6060 goto out_free_resources;
6061 }
6062
Calvin Owens98c56ad2016-07-28 21:38:21 -07006063 r = _base_allocate_memory_pools(ioc);
Sreekanth Reddyf92363d2012-11-30 07:44:21 +05306064 if (r)
6065 goto out_free_resources;
6066
6067 init_waitqueue_head(&ioc->reset_wq);
6068
6069 /* allocate memory pd handle bitmask list */
6070 ioc->pd_handles_sz = (ioc->facts.MaxDevHandle / 8);
6071 if (ioc->facts.MaxDevHandle % 8)
6072 ioc->pd_handles_sz++;
6073 ioc->pd_handles = kzalloc(ioc->pd_handles_sz,
6074 GFP_KERNEL);
6075 if (!ioc->pd_handles) {
6076 r = -ENOMEM;
6077 goto out_free_resources;
6078 }
6079 ioc->blocking_handles = kzalloc(ioc->pd_handles_sz,
6080 GFP_KERNEL);
6081 if (!ioc->blocking_handles) {
6082 r = -ENOMEM;
6083 goto out_free_resources;
6084 }
6085
Suganath Prabu Subramanic696f7b2016-10-26 13:34:34 +05306086 /* allocate memory for pending OS device add list */
6087 ioc->pend_os_device_add_sz = (ioc->facts.MaxDevHandle / 8);
6088 if (ioc->facts.MaxDevHandle % 8)
6089 ioc->pend_os_device_add_sz++;
6090 ioc->pend_os_device_add = kzalloc(ioc->pend_os_device_add_sz,
6091 GFP_KERNEL);
6092 if (!ioc->pend_os_device_add)
6093 goto out_free_resources;
6094
6095 ioc->device_remove_in_progress_sz = ioc->pend_os_device_add_sz;
6096 ioc->device_remove_in_progress =
6097 kzalloc(ioc->device_remove_in_progress_sz, GFP_KERNEL);
6098 if (!ioc->device_remove_in_progress)
6099 goto out_free_resources;
6100
Sreekanth Reddyf92363d2012-11-30 07:44:21 +05306101 ioc->fwfault_debug = mpt3sas_fwfault_debug;
6102
6103 /* base internal command bits */
6104 mutex_init(&ioc->base_cmds.mutex);
6105 ioc->base_cmds.reply = kzalloc(ioc->reply_sz, GFP_KERNEL);
6106 ioc->base_cmds.status = MPT3_CMD_NOT_USED;
6107
6108 /* port_enable command bits */
6109 ioc->port_enable_cmds.reply = kzalloc(ioc->reply_sz, GFP_KERNEL);
6110 ioc->port_enable_cmds.status = MPT3_CMD_NOT_USED;
6111
6112 /* transport internal command bits */
6113 ioc->transport_cmds.reply = kzalloc(ioc->reply_sz, GFP_KERNEL);
6114 ioc->transport_cmds.status = MPT3_CMD_NOT_USED;
6115 mutex_init(&ioc->transport_cmds.mutex);
6116
6117 /* scsih internal command bits */
6118 ioc->scsih_cmds.reply = kzalloc(ioc->reply_sz, GFP_KERNEL);
6119 ioc->scsih_cmds.status = MPT3_CMD_NOT_USED;
6120 mutex_init(&ioc->scsih_cmds.mutex);
6121
6122 /* task management internal command bits */
6123 ioc->tm_cmds.reply = kzalloc(ioc->reply_sz, GFP_KERNEL);
6124 ioc->tm_cmds.status = MPT3_CMD_NOT_USED;
6125 mutex_init(&ioc->tm_cmds.mutex);
6126
6127 /* config page internal command bits */
6128 ioc->config_cmds.reply = kzalloc(ioc->reply_sz, GFP_KERNEL);
6129 ioc->config_cmds.status = MPT3_CMD_NOT_USED;
6130 mutex_init(&ioc->config_cmds.mutex);
6131
6132 /* ctl module internal command bits */
6133 ioc->ctl_cmds.reply = kzalloc(ioc->reply_sz, GFP_KERNEL);
6134 ioc->ctl_cmds.sense = kzalloc(SCSI_SENSE_BUFFERSIZE, GFP_KERNEL);
6135 ioc->ctl_cmds.status = MPT3_CMD_NOT_USED;
6136 mutex_init(&ioc->ctl_cmds.mutex);
6137
Christophe JAILLETa5dd7ef2017-08-07 00:51:29 +02006138 if (!ioc->base_cmds.reply || !ioc->port_enable_cmds.reply ||
6139 !ioc->transport_cmds.reply || !ioc->scsih_cmds.reply ||
6140 !ioc->tm_cmds.reply || !ioc->config_cmds.reply ||
6141 !ioc->ctl_cmds.reply || !ioc->ctl_cmds.sense) {
Sreekanth Reddyf92363d2012-11-30 07:44:21 +05306142 r = -ENOMEM;
6143 goto out_free_resources;
6144 }
6145
6146 for (i = 0; i < MPI2_EVENT_NOTIFY_EVENTMASK_WORDS; i++)
6147 ioc->event_masks[i] = -1;
6148
6149 /* here we enable the events we care about */
6150 _base_unmask_events(ioc, MPI2_EVENT_SAS_DISCOVERY);
6151 _base_unmask_events(ioc, MPI2_EVENT_SAS_BROADCAST_PRIMITIVE);
6152 _base_unmask_events(ioc, MPI2_EVENT_SAS_TOPOLOGY_CHANGE_LIST);
6153 _base_unmask_events(ioc, MPI2_EVENT_SAS_DEVICE_STATUS_CHANGE);
6154 _base_unmask_events(ioc, MPI2_EVENT_SAS_ENCL_DEVICE_STATUS_CHANGE);
6155 _base_unmask_events(ioc, MPI2_EVENT_IR_CONFIGURATION_CHANGE_LIST);
6156 _base_unmask_events(ioc, MPI2_EVENT_IR_VOLUME);
6157 _base_unmask_events(ioc, MPI2_EVENT_IR_PHYSICAL_DISK);
6158 _base_unmask_events(ioc, MPI2_EVENT_IR_OPERATION_STATUS);
6159 _base_unmask_events(ioc, MPI2_EVENT_LOG_ENTRY_ADDED);
Sreekanth Reddy2d8ce8c2015-01-12 11:38:56 +05306160 _base_unmask_events(ioc, MPI2_EVENT_TEMP_THRESHOLD);
Sreekanth Reddyb99b1992017-10-10 18:41:14 +05306161 _base_unmask_events(ioc, MPI2_EVENT_ACTIVE_CABLE_EXCEPTION);
Suganath Prabu Subramani4318c732017-10-31 18:02:32 +05306162 if (ioc->hba_mpi_version_belonged == MPI26_VERSION) {
6163 if (ioc->is_gen35_ioc) {
6164 _base_unmask_events(ioc,
6165 MPI2_EVENT_PCIE_DEVICE_STATUS_CHANGE);
6166 _base_unmask_events(ioc, MPI2_EVENT_PCIE_ENUMERATION);
6167 _base_unmask_events(ioc,
6168 MPI2_EVENT_PCIE_TOPOLOGY_CHANGE_LIST);
6169 }
6170 }
Calvin Owens98c56ad2016-07-28 21:38:21 -07006171 r = _base_make_ioc_operational(ioc);
Sreekanth Reddyf92363d2012-11-30 07:44:21 +05306172 if (r)
6173 goto out_free_resources;
6174
Sreekanth Reddy16e179b2015-11-11 17:30:27 +05306175 ioc->non_operational_loop = 0;
Chaitra P B459325c2017-01-23 15:26:08 +05306176 ioc->got_task_abort_from_ioctl = 0;
Sreekanth Reddyf92363d2012-11-30 07:44:21 +05306177 return 0;
6178
6179 out_free_resources:
6180
6181 ioc->remove_host = 1;
6182
6183 mpt3sas_base_free_resources(ioc);
6184 _base_release_memory_pools(ioc);
6185 pci_set_drvdata(ioc->pdev, NULL);
6186 kfree(ioc->cpu_msix_table);
Sreekanth Reddy7786ab62015-11-11 17:30:28 +05306187 if (ioc->is_warpdrive)
6188 kfree(ioc->reply_post_host_index);
Sreekanth Reddyf92363d2012-11-30 07:44:21 +05306189 kfree(ioc->pd_handles);
6190 kfree(ioc->blocking_handles);
Suganath Prabu Subramanic696f7b2016-10-26 13:34:34 +05306191 kfree(ioc->device_remove_in_progress);
6192 kfree(ioc->pend_os_device_add);
Sreekanth Reddyf92363d2012-11-30 07:44:21 +05306193 kfree(ioc->tm_cmds.reply);
6194 kfree(ioc->transport_cmds.reply);
6195 kfree(ioc->scsih_cmds.reply);
6196 kfree(ioc->config_cmds.reply);
6197 kfree(ioc->base_cmds.reply);
6198 kfree(ioc->port_enable_cmds.reply);
6199 kfree(ioc->ctl_cmds.reply);
6200 kfree(ioc->ctl_cmds.sense);
6201 kfree(ioc->pfacts);
6202 ioc->ctl_cmds.reply = NULL;
6203 ioc->base_cmds.reply = NULL;
6204 ioc->tm_cmds.reply = NULL;
6205 ioc->scsih_cmds.reply = NULL;
6206 ioc->transport_cmds.reply = NULL;
6207 ioc->config_cmds.reply = NULL;
6208 ioc->pfacts = NULL;
6209 return r;
6210}
6211
6212
6213/**
6214 * mpt3sas_base_detach - remove controller instance
6215 * @ioc: per adapter object
6216 *
6217 * Return nothing.
6218 */
6219void
6220mpt3sas_base_detach(struct MPT3SAS_ADAPTER *ioc)
6221{
6222 dexitprintk(ioc, pr_info(MPT3SAS_FMT "%s\n", ioc->name,
6223 __func__));
6224
6225 mpt3sas_base_stop_watchdog(ioc);
6226 mpt3sas_base_free_resources(ioc);
6227 _base_release_memory_pools(ioc);
6228 pci_set_drvdata(ioc->pdev, NULL);
6229 kfree(ioc->cpu_msix_table);
Sreekanth Reddy7786ab62015-11-11 17:30:28 +05306230 if (ioc->is_warpdrive)
6231 kfree(ioc->reply_post_host_index);
Sreekanth Reddyf92363d2012-11-30 07:44:21 +05306232 kfree(ioc->pd_handles);
6233 kfree(ioc->blocking_handles);
Suganath Prabu Subramanic696f7b2016-10-26 13:34:34 +05306234 kfree(ioc->device_remove_in_progress);
6235 kfree(ioc->pend_os_device_add);
Sreekanth Reddyf92363d2012-11-30 07:44:21 +05306236 kfree(ioc->pfacts);
6237 kfree(ioc->ctl_cmds.reply);
6238 kfree(ioc->ctl_cmds.sense);
6239 kfree(ioc->base_cmds.reply);
6240 kfree(ioc->port_enable_cmds.reply);
6241 kfree(ioc->tm_cmds.reply);
6242 kfree(ioc->transport_cmds.reply);
6243 kfree(ioc->scsih_cmds.reply);
6244 kfree(ioc->config_cmds.reply);
6245}
6246
6247/**
6248 * _base_reset_handler - reset callback handler (for base)
6249 * @ioc: per adapter object
6250 * @reset_phase: phase
6251 *
6252 * The handler for doing any required cleanup or initialization.
6253 *
6254 * The reset phase can be MPT3_IOC_PRE_RESET, MPT3_IOC_AFTER_RESET,
6255 * MPT3_IOC_DONE_RESET
6256 *
6257 * Return nothing.
6258 */
6259static void
6260_base_reset_handler(struct MPT3SAS_ADAPTER *ioc, int reset_phase)
6261{
6262 mpt3sas_scsih_reset_handler(ioc, reset_phase);
6263 mpt3sas_ctl_reset_handler(ioc, reset_phase);
6264 switch (reset_phase) {
6265 case MPT3_IOC_PRE_RESET:
6266 dtmprintk(ioc, pr_info(MPT3SAS_FMT
6267 "%s: MPT3_IOC_PRE_RESET\n", ioc->name, __func__));
6268 break;
6269 case MPT3_IOC_AFTER_RESET:
6270 dtmprintk(ioc, pr_info(MPT3SAS_FMT
6271 "%s: MPT3_IOC_AFTER_RESET\n", ioc->name, __func__));
6272 if (ioc->transport_cmds.status & MPT3_CMD_PENDING) {
6273 ioc->transport_cmds.status |= MPT3_CMD_RESET;
6274 mpt3sas_base_free_smid(ioc, ioc->transport_cmds.smid);
6275 complete(&ioc->transport_cmds.done);
6276 }
6277 if (ioc->base_cmds.status & MPT3_CMD_PENDING) {
6278 ioc->base_cmds.status |= MPT3_CMD_RESET;
6279 mpt3sas_base_free_smid(ioc, ioc->base_cmds.smid);
6280 complete(&ioc->base_cmds.done);
6281 }
6282 if (ioc->port_enable_cmds.status & MPT3_CMD_PENDING) {
6283 ioc->port_enable_failed = 1;
6284 ioc->port_enable_cmds.status |= MPT3_CMD_RESET;
6285 mpt3sas_base_free_smid(ioc, ioc->port_enable_cmds.smid);
6286 if (ioc->is_driver_loading) {
6287 ioc->start_scan_failed =
6288 MPI2_IOCSTATUS_INTERNAL_ERROR;
6289 ioc->start_scan = 0;
6290 ioc->port_enable_cmds.status =
6291 MPT3_CMD_NOT_USED;
6292 } else
6293 complete(&ioc->port_enable_cmds.done);
6294 }
6295 if (ioc->config_cmds.status & MPT3_CMD_PENDING) {
6296 ioc->config_cmds.status |= MPT3_CMD_RESET;
6297 mpt3sas_base_free_smid(ioc, ioc->config_cmds.smid);
6298 ioc->config_cmds.smid = USHRT_MAX;
6299 complete(&ioc->config_cmds.done);
6300 }
6301 break;
6302 case MPT3_IOC_DONE_RESET:
6303 dtmprintk(ioc, pr_info(MPT3SAS_FMT
6304 "%s: MPT3_IOC_DONE_RESET\n", ioc->name, __func__));
6305 break;
6306 }
6307}
6308
6309/**
6310 * _wait_for_commands_to_complete - reset controller
6311 * @ioc: Pointer to MPT_ADAPTER structure
Sreekanth Reddyf92363d2012-11-30 07:44:21 +05306312 *
6313 * This function waiting(3s) for all pending commands to complete
6314 * prior to putting controller in reset.
6315 */
6316static void
Calvin Owens98c56ad2016-07-28 21:38:21 -07006317_wait_for_commands_to_complete(struct MPT3SAS_ADAPTER *ioc)
Sreekanth Reddyf92363d2012-11-30 07:44:21 +05306318{
6319 u32 ioc_state;
6320 unsigned long flags;
6321 u16 i;
6322
6323 ioc->pending_io_count = 0;
Sreekanth Reddyf92363d2012-11-30 07:44:21 +05306324
6325 ioc_state = mpt3sas_base_get_iocstate(ioc, 0);
6326 if ((ioc_state & MPI2_IOC_STATE_MASK) != MPI2_IOC_STATE_OPERATIONAL)
6327 return;
6328
6329 /* pending command count */
6330 spin_lock_irqsave(&ioc->scsi_lookup_lock, flags);
6331 for (i = 0; i < ioc->scsiio_depth; i++)
6332 if (ioc->scsi_lookup[i].cb_idx != 0xFF)
6333 ioc->pending_io_count++;
6334 spin_unlock_irqrestore(&ioc->scsi_lookup_lock, flags);
6335
6336 if (!ioc->pending_io_count)
6337 return;
6338
6339 /* wait for pending commands to complete */
6340 wait_event_timeout(ioc->reset_wq, ioc->pending_io_count == 0, 10 * HZ);
6341}
6342
6343/**
6344 * mpt3sas_base_hard_reset_handler - reset controller
6345 * @ioc: Pointer to MPT_ADAPTER structure
Sreekanth Reddyf92363d2012-11-30 07:44:21 +05306346 * @type: FORCE_BIG_HAMMER or SOFT_RESET
6347 *
6348 * Returns 0 for success, non-zero for failure.
6349 */
6350int
Calvin Owens98c56ad2016-07-28 21:38:21 -07006351mpt3sas_base_hard_reset_handler(struct MPT3SAS_ADAPTER *ioc,
Sreekanth Reddyf92363d2012-11-30 07:44:21 +05306352 enum reset_type type)
6353{
6354 int r;
6355 unsigned long flags;
6356 u32 ioc_state;
6357 u8 is_fault = 0, is_trigger = 0;
6358
6359 dtmprintk(ioc, pr_info(MPT3SAS_FMT "%s: enter\n", ioc->name,
6360 __func__));
6361
6362 if (ioc->pci_error_recovery) {
6363 pr_err(MPT3SAS_FMT "%s: pci error recovery reset\n",
6364 ioc->name, __func__);
6365 r = 0;
6366 goto out_unlocked;
6367 }
6368
6369 if (mpt3sas_fwfault_debug)
6370 mpt3sas_halt_firmware(ioc);
6371
Sreekanth Reddyf92363d2012-11-30 07:44:21 +05306372 /* wait for an active reset in progress to complete */
6373 if (!mutex_trylock(&ioc->reset_in_progress_mutex)) {
6374 do {
6375 ssleep(1);
6376 } while (ioc->shost_recovery == 1);
6377 dtmprintk(ioc, pr_info(MPT3SAS_FMT "%s: exit\n", ioc->name,
6378 __func__));
6379 return ioc->ioc_reset_in_progress_status;
6380 }
6381
6382 spin_lock_irqsave(&ioc->ioc_reset_in_progress_lock, flags);
6383 ioc->shost_recovery = 1;
6384 spin_unlock_irqrestore(&ioc->ioc_reset_in_progress_lock, flags);
6385
6386 if ((ioc->diag_buffer_status[MPI2_DIAG_BUF_TYPE_TRACE] &
6387 MPT3_DIAG_BUFFER_IS_REGISTERED) &&
6388 (!(ioc->diag_buffer_status[MPI2_DIAG_BUF_TYPE_TRACE] &
6389 MPT3_DIAG_BUFFER_IS_RELEASED))) {
6390 is_trigger = 1;
6391 ioc_state = mpt3sas_base_get_iocstate(ioc, 0);
6392 if ((ioc_state & MPI2_IOC_STATE_MASK) == MPI2_IOC_STATE_FAULT)
6393 is_fault = 1;
6394 }
6395 _base_reset_handler(ioc, MPT3_IOC_PRE_RESET);
Calvin Owens98c56ad2016-07-28 21:38:21 -07006396 _wait_for_commands_to_complete(ioc);
Sreekanth Reddyf92363d2012-11-30 07:44:21 +05306397 _base_mask_interrupts(ioc);
Calvin Owens98c56ad2016-07-28 21:38:21 -07006398 r = _base_make_ioc_ready(ioc, type);
Sreekanth Reddyf92363d2012-11-30 07:44:21 +05306399 if (r)
6400 goto out;
6401 _base_reset_handler(ioc, MPT3_IOC_AFTER_RESET);
6402
6403 /* If this hard reset is called while port enable is active, then
6404 * there is no reason to call make_ioc_operational
6405 */
6406 if (ioc->is_driver_loading && ioc->port_enable_failed) {
6407 ioc->remove_host = 1;
6408 r = -EFAULT;
6409 goto out;
6410 }
Calvin Owens98c56ad2016-07-28 21:38:21 -07006411 r = _base_get_ioc_facts(ioc);
Sreekanth Reddyf92363d2012-11-30 07:44:21 +05306412 if (r)
6413 goto out;
Sreekanth Reddy9b05c912014-09-12 15:35:31 +05306414
6415 if (ioc->rdpq_array_enable && !ioc->rdpq_array_capable)
6416 panic("%s: Issue occurred with flashing controller firmware."
6417 "Please reboot the system and ensure that the correct"
6418 " firmware version is running\n", ioc->name);
6419
Calvin Owens98c56ad2016-07-28 21:38:21 -07006420 r = _base_make_ioc_operational(ioc);
Sreekanth Reddyf92363d2012-11-30 07:44:21 +05306421 if (!r)
6422 _base_reset_handler(ioc, MPT3_IOC_DONE_RESET);
6423
6424 out:
6425 dtmprintk(ioc, pr_info(MPT3SAS_FMT "%s: %s\n",
6426 ioc->name, __func__, ((r == 0) ? "SUCCESS" : "FAILED")));
6427
6428 spin_lock_irqsave(&ioc->ioc_reset_in_progress_lock, flags);
6429 ioc->ioc_reset_in_progress_status = r;
6430 ioc->shost_recovery = 0;
6431 spin_unlock_irqrestore(&ioc->ioc_reset_in_progress_lock, flags);
6432 ioc->ioc_reset_count++;
6433 mutex_unlock(&ioc->reset_in_progress_mutex);
6434
6435 out_unlocked:
6436 if ((r == 0) && is_trigger) {
6437 if (is_fault)
6438 mpt3sas_trigger_master(ioc, MASTER_TRIGGER_FW_FAULT);
6439 else
6440 mpt3sas_trigger_master(ioc,
6441 MASTER_TRIGGER_ADAPTER_RESET);
6442 }
6443 dtmprintk(ioc, pr_info(MPT3SAS_FMT "%s: exit\n", ioc->name,
6444 __func__));
6445 return r;
6446}