blob: c0a9d97b343fc2a98543b1cbb733e6150fb13b0f [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>
60#include <linux/kthread.h>
61#include <linux/aer.h>
62
63
64#include "mpt3sas_base.h"
65
66static MPT_CALLBACK mpt_callbacks[MPT_MAX_CALLBACKS];
67
68
69#define FAULT_POLLING_INTERVAL 1000 /* in milliseconds */
70
71 /* maximum controller queue depth */
72#define MAX_HBA_QUEUE_DEPTH 30000
73#define MAX_CHAIN_DEPTH 100000
74static int max_queue_depth = -1;
75module_param(max_queue_depth, int, 0);
76MODULE_PARM_DESC(max_queue_depth, " max controller queue depth ");
77
78static int max_sgl_entries = -1;
79module_param(max_sgl_entries, int, 0);
80MODULE_PARM_DESC(max_sgl_entries, " max sg entries ");
81
82static int msix_disable = -1;
83module_param(msix_disable, int, 0);
84MODULE_PARM_DESC(msix_disable, " disable msix routed interrupts (default=0)");
85
Suganath Prabu Subramani64038302016-02-08 22:13:39 +053086static int smp_affinity_enable = 1;
87module_param(smp_affinity_enable, int, S_IRUGO);
88MODULE_PARM_DESC(smp_affinity_enable, "SMP affinity feature enable/disbale Default: enable(1)");
89
Sreekanth Reddyfb77bb52015-06-30 12:24:47 +053090static int max_msix_vectors = -1;
Sreekanth Reddy9c500062013-08-14 18:23:20 +053091module_param(max_msix_vectors, int, 0);
92MODULE_PARM_DESC(max_msix_vectors,
Sreekanth Reddyfb77bb52015-06-30 12:24:47 +053093 " max msix vectors");
Sreekanth Reddyf92363d2012-11-30 07:44:21 +053094
95static int mpt3sas_fwfault_debug;
96MODULE_PARM_DESC(mpt3sas_fwfault_debug,
97 " enable detection of firmware fault and halt firmware - (default=0)");
98
Sreekanth Reddy9b05c912014-09-12 15:35:31 +053099static int
100_base_get_ioc_facts(struct MPT3SAS_ADAPTER *ioc, int sleep_flag);
Sreekanth Reddyf92363d2012-11-30 07:44:21 +0530101
102/**
103 * _scsih_set_fwfault_debug - global setting of ioc->fwfault_debug.
104 *
105 */
106static int
107_scsih_set_fwfault_debug(const char *val, struct kernel_param *kp)
108{
109 int ret = param_set_int(val, kp);
110 struct MPT3SAS_ADAPTER *ioc;
111
112 if (ret)
113 return ret;
114
Sreekanth Reddy08c4d552015-11-11 17:30:33 +0530115 /* global ioc spinlock to protect controller list on list operations */
Sreekanth Reddyf92363d2012-11-30 07:44:21 +0530116 pr_info("setting fwfault_debug(%d)\n", mpt3sas_fwfault_debug);
Sreekanth Reddy08c4d552015-11-11 17:30:33 +0530117 spin_lock(&gioc_lock);
Sreekanth Reddyf92363d2012-11-30 07:44:21 +0530118 list_for_each_entry(ioc, &mpt3sas_ioc_list, list)
119 ioc->fwfault_debug = mpt3sas_fwfault_debug;
Sreekanth Reddy08c4d552015-11-11 17:30:33 +0530120 spin_unlock(&gioc_lock);
Sreekanth Reddyf92363d2012-11-30 07:44:21 +0530121 return 0;
122}
123module_param_call(mpt3sas_fwfault_debug, _scsih_set_fwfault_debug,
124 param_get_int, &mpt3sas_fwfault_debug, 0644);
125
126/**
127 * mpt3sas_remove_dead_ioc_func - kthread context to remove dead ioc
128 * @arg: input argument, used to derive ioc
129 *
130 * Return 0 if controller is removed from pci subsystem.
131 * Return -1 for other case.
132 */
133static int mpt3sas_remove_dead_ioc_func(void *arg)
134{
135 struct MPT3SAS_ADAPTER *ioc = (struct MPT3SAS_ADAPTER *)arg;
136 struct pci_dev *pdev;
137
138 if ((ioc == NULL))
139 return -1;
140
141 pdev = ioc->pdev;
142 if ((pdev == NULL))
143 return -1;
Rafael J. Wysocki64cdb412014-01-10 15:27:56 +0100144 pci_stop_and_remove_bus_device_locked(pdev);
Sreekanth Reddyf92363d2012-11-30 07:44:21 +0530145 return 0;
146}
147
148/**
149 * _base_fault_reset_work - workq handling ioc fault conditions
150 * @work: input argument, used to derive ioc
151 * Context: sleep.
152 *
153 * Return nothing.
154 */
155static void
156_base_fault_reset_work(struct work_struct *work)
157{
158 struct MPT3SAS_ADAPTER *ioc =
159 container_of(work, struct MPT3SAS_ADAPTER, fault_reset_work.work);
160 unsigned long flags;
161 u32 doorbell;
162 int rc;
163 struct task_struct *p;
164
165
166 spin_lock_irqsave(&ioc->ioc_reset_in_progress_lock, flags);
Sreekanth Reddy16e179b2015-11-11 17:30:27 +0530167 if (ioc->shost_recovery || ioc->pci_error_recovery)
Sreekanth Reddyf92363d2012-11-30 07:44:21 +0530168 goto rearm_timer;
169 spin_unlock_irqrestore(&ioc->ioc_reset_in_progress_lock, flags);
170
171 doorbell = mpt3sas_base_get_iocstate(ioc, 0);
172 if ((doorbell & MPI2_IOC_STATE_MASK) == MPI2_IOC_STATE_MASK) {
173 pr_err(MPT3SAS_FMT "SAS host is non-operational !!!!\n",
174 ioc->name);
175
Sreekanth Reddy16e179b2015-11-11 17:30:27 +0530176 /* It may be possible that EEH recovery can resolve some of
177 * pci bus failure issues rather removing the dead ioc function
178 * by considering controller is in a non-operational state. So
179 * here priority is given to the EEH recovery. If it doesn't
180 * not resolve this issue, mpt3sas driver will consider this
181 * controller to non-operational state and remove the dead ioc
182 * function.
183 */
184 if (ioc->non_operational_loop++ < 5) {
185 spin_lock_irqsave(&ioc->ioc_reset_in_progress_lock,
186 flags);
187 goto rearm_timer;
188 }
189
Sreekanth Reddyf92363d2012-11-30 07:44:21 +0530190 /*
191 * Call _scsih_flush_pending_cmds callback so that we flush all
192 * pending commands back to OS. This call is required to aovid
193 * deadlock at block layer. Dead IOC will fail to do diag reset,
194 * and this call is safe since dead ioc will never return any
195 * command back from HW.
196 */
197 ioc->schedule_dead_ioc_flush_running_cmds(ioc);
198 /*
199 * Set remove_host flag early since kernel thread will
200 * take some time to execute.
201 */
202 ioc->remove_host = 1;
203 /*Remove the Dead Host */
204 p = kthread_run(mpt3sas_remove_dead_ioc_func, ioc,
Sreekanth Reddyc84b06a2015-11-11 17:30:35 +0530205 "%s_dead_ioc_%d", ioc->driver_name, ioc->id);
Sreekanth Reddyf92363d2012-11-30 07:44:21 +0530206 if (IS_ERR(p))
207 pr_err(MPT3SAS_FMT
208 "%s: Running mpt3sas_dead_ioc thread failed !!!!\n",
209 ioc->name, __func__);
210 else
211 pr_err(MPT3SAS_FMT
212 "%s: Running mpt3sas_dead_ioc thread success !!!!\n",
213 ioc->name, __func__);
214 return; /* don't rearm timer */
215 }
216
Sreekanth Reddy16e179b2015-11-11 17:30:27 +0530217 ioc->non_operational_loop = 0;
218
Sreekanth Reddyf92363d2012-11-30 07:44:21 +0530219 if ((doorbell & MPI2_IOC_STATE_MASK) != MPI2_IOC_STATE_OPERATIONAL) {
220 rc = mpt3sas_base_hard_reset_handler(ioc, CAN_SLEEP,
221 FORCE_BIG_HAMMER);
222 pr_warn(MPT3SAS_FMT "%s: hard reset: %s\n", ioc->name,
223 __func__, (rc == 0) ? "success" : "failed");
224 doorbell = mpt3sas_base_get_iocstate(ioc, 0);
225 if ((doorbell & MPI2_IOC_STATE_MASK) == MPI2_IOC_STATE_FAULT)
226 mpt3sas_base_fault_info(ioc, doorbell &
227 MPI2_DOORBELL_DATA_MASK);
228 if (rc && (doorbell & MPI2_IOC_STATE_MASK) !=
229 MPI2_IOC_STATE_OPERATIONAL)
230 return; /* don't rearm timer */
231 }
232
233 spin_lock_irqsave(&ioc->ioc_reset_in_progress_lock, flags);
234 rearm_timer:
235 if (ioc->fault_reset_work_q)
236 queue_delayed_work(ioc->fault_reset_work_q,
237 &ioc->fault_reset_work,
238 msecs_to_jiffies(FAULT_POLLING_INTERVAL));
239 spin_unlock_irqrestore(&ioc->ioc_reset_in_progress_lock, flags);
240}
241
242/**
243 * mpt3sas_base_start_watchdog - start the fault_reset_work_q
244 * @ioc: per adapter object
245 * Context: sleep.
246 *
247 * Return nothing.
248 */
249void
250mpt3sas_base_start_watchdog(struct MPT3SAS_ADAPTER *ioc)
251{
252 unsigned long flags;
253
254 if (ioc->fault_reset_work_q)
255 return;
256
257 /* initialize fault polling */
258
259 INIT_DELAYED_WORK(&ioc->fault_reset_work, _base_fault_reset_work);
260 snprintf(ioc->fault_reset_work_q_name,
Sreekanth Reddyc84b06a2015-11-11 17:30:35 +0530261 sizeof(ioc->fault_reset_work_q_name), "poll_%s%d_status",
262 ioc->driver_name, ioc->id);
Sreekanth Reddyf92363d2012-11-30 07:44:21 +0530263 ioc->fault_reset_work_q =
264 create_singlethread_workqueue(ioc->fault_reset_work_q_name);
265 if (!ioc->fault_reset_work_q) {
266 pr_err(MPT3SAS_FMT "%s: failed (line=%d)\n",
267 ioc->name, __func__, __LINE__);
268 return;
269 }
270 spin_lock_irqsave(&ioc->ioc_reset_in_progress_lock, flags);
271 if (ioc->fault_reset_work_q)
272 queue_delayed_work(ioc->fault_reset_work_q,
273 &ioc->fault_reset_work,
274 msecs_to_jiffies(FAULT_POLLING_INTERVAL));
275 spin_unlock_irqrestore(&ioc->ioc_reset_in_progress_lock, flags);
276}
277
278/**
279 * mpt3sas_base_stop_watchdog - stop the fault_reset_work_q
280 * @ioc: per adapter object
281 * Context: sleep.
282 *
283 * Return nothing.
284 */
285void
286mpt3sas_base_stop_watchdog(struct MPT3SAS_ADAPTER *ioc)
287{
288 unsigned long flags;
289 struct workqueue_struct *wq;
290
291 spin_lock_irqsave(&ioc->ioc_reset_in_progress_lock, flags);
292 wq = ioc->fault_reset_work_q;
293 ioc->fault_reset_work_q = NULL;
294 spin_unlock_irqrestore(&ioc->ioc_reset_in_progress_lock, flags);
295 if (wq) {
Reddy, Sreekanth4dc06fd2014-07-14 12:01:35 +0530296 if (!cancel_delayed_work_sync(&ioc->fault_reset_work))
Sreekanth Reddyf92363d2012-11-30 07:44:21 +0530297 flush_workqueue(wq);
298 destroy_workqueue(wq);
299 }
300}
301
302/**
303 * mpt3sas_base_fault_info - verbose translation of firmware FAULT code
304 * @ioc: per adapter object
305 * @fault_code: fault code
306 *
307 * Return nothing.
308 */
309void
310mpt3sas_base_fault_info(struct MPT3SAS_ADAPTER *ioc , u16 fault_code)
311{
312 pr_err(MPT3SAS_FMT "fault_state(0x%04x)!\n",
313 ioc->name, fault_code);
314}
315
316/**
317 * mpt3sas_halt_firmware - halt's mpt controller firmware
318 * @ioc: per adapter object
319 *
320 * For debugging timeout related issues. Writing 0xCOFFEE00
321 * to the doorbell register will halt controller firmware. With
322 * the purpose to stop both driver and firmware, the enduser can
323 * obtain a ring buffer from controller UART.
324 */
325void
326mpt3sas_halt_firmware(struct MPT3SAS_ADAPTER *ioc)
327{
328 u32 doorbell;
329
330 if (!ioc->fwfault_debug)
331 return;
332
333 dump_stack();
334
335 doorbell = readl(&ioc->chip->Doorbell);
336 if ((doorbell & MPI2_IOC_STATE_MASK) == MPI2_IOC_STATE_FAULT)
337 mpt3sas_base_fault_info(ioc , doorbell);
338 else {
339 writel(0xC0FFEE00, &ioc->chip->Doorbell);
340 pr_err(MPT3SAS_FMT "Firmware is halted due to command timeout\n",
341 ioc->name);
342 }
343
344 if (ioc->fwfault_debug == 2)
345 for (;;)
346 ;
347 else
348 panic("panic in %s\n", __func__);
349}
350
Sreekanth Reddyf92363d2012-11-30 07:44:21 +0530351/**
352 * _base_sas_ioc_info - verbose translation of the ioc status
353 * @ioc: per adapter object
354 * @mpi_reply: reply mf payload returned from firmware
355 * @request_hdr: request mf
356 *
357 * Return nothing.
358 */
359static void
360_base_sas_ioc_info(struct MPT3SAS_ADAPTER *ioc, MPI2DefaultReply_t *mpi_reply,
361 MPI2RequestHeader_t *request_hdr)
362{
363 u16 ioc_status = le16_to_cpu(mpi_reply->IOCStatus) &
364 MPI2_IOCSTATUS_MASK;
365 char *desc = NULL;
366 u16 frame_sz;
367 char *func_str = NULL;
368
369 /* SCSI_IO, RAID_PASS are handled from _scsih_scsi_ioc_info */
370 if (request_hdr->Function == MPI2_FUNCTION_SCSI_IO_REQUEST ||
371 request_hdr->Function == MPI2_FUNCTION_RAID_SCSI_IO_PASSTHROUGH ||
372 request_hdr->Function == MPI2_FUNCTION_EVENT_NOTIFICATION)
373 return;
374
375 if (ioc_status == MPI2_IOCSTATUS_CONFIG_INVALID_PAGE)
376 return;
377
378 switch (ioc_status) {
379
380/****************************************************************************
381* Common IOCStatus values for all replies
382****************************************************************************/
383
384 case MPI2_IOCSTATUS_INVALID_FUNCTION:
385 desc = "invalid function";
386 break;
387 case MPI2_IOCSTATUS_BUSY:
388 desc = "busy";
389 break;
390 case MPI2_IOCSTATUS_INVALID_SGL:
391 desc = "invalid sgl";
392 break;
393 case MPI2_IOCSTATUS_INTERNAL_ERROR:
394 desc = "internal error";
395 break;
396 case MPI2_IOCSTATUS_INVALID_VPID:
397 desc = "invalid vpid";
398 break;
399 case MPI2_IOCSTATUS_INSUFFICIENT_RESOURCES:
400 desc = "insufficient resources";
401 break;
Suganath prabu Subramanib130b0d2016-01-28 12:06:58 +0530402 case MPI2_IOCSTATUS_INSUFFICIENT_POWER:
403 desc = "insufficient power";
404 break;
Sreekanth Reddyf92363d2012-11-30 07:44:21 +0530405 case MPI2_IOCSTATUS_INVALID_FIELD:
406 desc = "invalid field";
407 break;
408 case MPI2_IOCSTATUS_INVALID_STATE:
409 desc = "invalid state";
410 break;
411 case MPI2_IOCSTATUS_OP_STATE_NOT_SUPPORTED:
412 desc = "op state not supported";
413 break;
414
415/****************************************************************************
416* Config IOCStatus values
417****************************************************************************/
418
419 case MPI2_IOCSTATUS_CONFIG_INVALID_ACTION:
420 desc = "config invalid action";
421 break;
422 case MPI2_IOCSTATUS_CONFIG_INVALID_TYPE:
423 desc = "config invalid type";
424 break;
425 case MPI2_IOCSTATUS_CONFIG_INVALID_PAGE:
426 desc = "config invalid page";
427 break;
428 case MPI2_IOCSTATUS_CONFIG_INVALID_DATA:
429 desc = "config invalid data";
430 break;
431 case MPI2_IOCSTATUS_CONFIG_NO_DEFAULTS:
432 desc = "config no defaults";
433 break;
434 case MPI2_IOCSTATUS_CONFIG_CANT_COMMIT:
435 desc = "config cant commit";
436 break;
437
438/****************************************************************************
439* SCSI IO Reply
440****************************************************************************/
441
442 case MPI2_IOCSTATUS_SCSI_RECOVERED_ERROR:
443 case MPI2_IOCSTATUS_SCSI_INVALID_DEVHANDLE:
444 case MPI2_IOCSTATUS_SCSI_DEVICE_NOT_THERE:
445 case MPI2_IOCSTATUS_SCSI_DATA_OVERRUN:
446 case MPI2_IOCSTATUS_SCSI_DATA_UNDERRUN:
447 case MPI2_IOCSTATUS_SCSI_IO_DATA_ERROR:
448 case MPI2_IOCSTATUS_SCSI_PROTOCOL_ERROR:
449 case MPI2_IOCSTATUS_SCSI_TASK_TERMINATED:
450 case MPI2_IOCSTATUS_SCSI_RESIDUAL_MISMATCH:
451 case MPI2_IOCSTATUS_SCSI_TASK_MGMT_FAILED:
452 case MPI2_IOCSTATUS_SCSI_IOC_TERMINATED:
453 case MPI2_IOCSTATUS_SCSI_EXT_TERMINATED:
454 break;
455
456/****************************************************************************
457* For use by SCSI Initiator and SCSI Target end-to-end data protection
458****************************************************************************/
459
460 case MPI2_IOCSTATUS_EEDP_GUARD_ERROR:
461 desc = "eedp guard error";
462 break;
463 case MPI2_IOCSTATUS_EEDP_REF_TAG_ERROR:
464 desc = "eedp ref tag error";
465 break;
466 case MPI2_IOCSTATUS_EEDP_APP_TAG_ERROR:
467 desc = "eedp app tag error";
468 break;
469
470/****************************************************************************
471* SCSI Target values
472****************************************************************************/
473
474 case MPI2_IOCSTATUS_TARGET_INVALID_IO_INDEX:
475 desc = "target invalid io index";
476 break;
477 case MPI2_IOCSTATUS_TARGET_ABORTED:
478 desc = "target aborted";
479 break;
480 case MPI2_IOCSTATUS_TARGET_NO_CONN_RETRYABLE:
481 desc = "target no conn retryable";
482 break;
483 case MPI2_IOCSTATUS_TARGET_NO_CONNECTION:
484 desc = "target no connection";
485 break;
486 case MPI2_IOCSTATUS_TARGET_XFER_COUNT_MISMATCH:
487 desc = "target xfer count mismatch";
488 break;
489 case MPI2_IOCSTATUS_TARGET_DATA_OFFSET_ERROR:
490 desc = "target data offset error";
491 break;
492 case MPI2_IOCSTATUS_TARGET_TOO_MUCH_WRITE_DATA:
493 desc = "target too much write data";
494 break;
495 case MPI2_IOCSTATUS_TARGET_IU_TOO_SHORT:
496 desc = "target iu too short";
497 break;
498 case MPI2_IOCSTATUS_TARGET_ACK_NAK_TIMEOUT:
499 desc = "target ack nak timeout";
500 break;
501 case MPI2_IOCSTATUS_TARGET_NAK_RECEIVED:
502 desc = "target nak received";
503 break;
504
505/****************************************************************************
506* Serial Attached SCSI values
507****************************************************************************/
508
509 case MPI2_IOCSTATUS_SAS_SMP_REQUEST_FAILED:
510 desc = "smp request failed";
511 break;
512 case MPI2_IOCSTATUS_SAS_SMP_DATA_OVERRUN:
513 desc = "smp data overrun";
514 break;
515
516/****************************************************************************
517* Diagnostic Buffer Post / Diagnostic Release values
518****************************************************************************/
519
520 case MPI2_IOCSTATUS_DIAGNOSTIC_RELEASED:
521 desc = "diagnostic released";
522 break;
523 default:
524 break;
525 }
526
527 if (!desc)
528 return;
529
530 switch (request_hdr->Function) {
531 case MPI2_FUNCTION_CONFIG:
532 frame_sz = sizeof(Mpi2ConfigRequest_t) + ioc->sge_size;
533 func_str = "config_page";
534 break;
535 case MPI2_FUNCTION_SCSI_TASK_MGMT:
536 frame_sz = sizeof(Mpi2SCSITaskManagementRequest_t);
537 func_str = "task_mgmt";
538 break;
539 case MPI2_FUNCTION_SAS_IO_UNIT_CONTROL:
540 frame_sz = sizeof(Mpi2SasIoUnitControlRequest_t);
541 func_str = "sas_iounit_ctl";
542 break;
543 case MPI2_FUNCTION_SCSI_ENCLOSURE_PROCESSOR:
544 frame_sz = sizeof(Mpi2SepRequest_t);
545 func_str = "enclosure";
546 break;
547 case MPI2_FUNCTION_IOC_INIT:
548 frame_sz = sizeof(Mpi2IOCInitRequest_t);
549 func_str = "ioc_init";
550 break;
551 case MPI2_FUNCTION_PORT_ENABLE:
552 frame_sz = sizeof(Mpi2PortEnableRequest_t);
553 func_str = "port_enable";
554 break;
555 case MPI2_FUNCTION_SMP_PASSTHROUGH:
556 frame_sz = sizeof(Mpi2SmpPassthroughRequest_t) + ioc->sge_size;
557 func_str = "smp_passthru";
558 break;
559 default:
560 frame_sz = 32;
561 func_str = "unknown";
562 break;
563 }
564
565 pr_warn(MPT3SAS_FMT "ioc_status: %s(0x%04x), request(0x%p),(%s)\n",
566 ioc->name, desc, ioc_status, request_hdr, func_str);
567
568 _debug_dump_mf(request_hdr, frame_sz/4);
569}
570
571/**
572 * _base_display_event_data - verbose translation of firmware asyn events
573 * @ioc: per adapter object
574 * @mpi_reply: reply mf payload returned from firmware
575 *
576 * Return nothing.
577 */
578static void
579_base_display_event_data(struct MPT3SAS_ADAPTER *ioc,
580 Mpi2EventNotificationReply_t *mpi_reply)
581{
582 char *desc = NULL;
583 u16 event;
584
585 if (!(ioc->logging_level & MPT_DEBUG_EVENTS))
586 return;
587
588 event = le16_to_cpu(mpi_reply->Event);
589
590 switch (event) {
591 case MPI2_EVENT_LOG_DATA:
592 desc = "Log Data";
593 break;
594 case MPI2_EVENT_STATE_CHANGE:
595 desc = "Status Change";
596 break;
597 case MPI2_EVENT_HARD_RESET_RECEIVED:
598 desc = "Hard Reset Received";
599 break;
600 case MPI2_EVENT_EVENT_CHANGE:
601 desc = "Event Change";
602 break;
603 case MPI2_EVENT_SAS_DEVICE_STATUS_CHANGE:
604 desc = "Device Status Change";
605 break;
606 case MPI2_EVENT_IR_OPERATION_STATUS:
Sreekanth Reddy7786ab62015-11-11 17:30:28 +0530607 if (!ioc->hide_ir_msg)
608 desc = "IR Operation Status";
Sreekanth Reddyf92363d2012-11-30 07:44:21 +0530609 break;
610 case MPI2_EVENT_SAS_DISCOVERY:
611 {
612 Mpi2EventDataSasDiscovery_t *event_data =
613 (Mpi2EventDataSasDiscovery_t *)mpi_reply->EventData;
614 pr_info(MPT3SAS_FMT "Discovery: (%s)", ioc->name,
615 (event_data->ReasonCode == MPI2_EVENT_SAS_DISC_RC_STARTED) ?
616 "start" : "stop");
617 if (event_data->DiscoveryStatus)
618 pr_info("discovery_status(0x%08x)",
619 le32_to_cpu(event_data->DiscoveryStatus));
620 pr_info("\n");
621 return;
622 }
623 case MPI2_EVENT_SAS_BROADCAST_PRIMITIVE:
624 desc = "SAS Broadcast Primitive";
625 break;
626 case MPI2_EVENT_SAS_INIT_DEVICE_STATUS_CHANGE:
627 desc = "SAS Init Device Status Change";
628 break;
629 case MPI2_EVENT_SAS_INIT_TABLE_OVERFLOW:
630 desc = "SAS Init Table Overflow";
631 break;
632 case MPI2_EVENT_SAS_TOPOLOGY_CHANGE_LIST:
633 desc = "SAS Topology Change List";
634 break;
635 case MPI2_EVENT_SAS_ENCL_DEVICE_STATUS_CHANGE:
636 desc = "SAS Enclosure Device Status Change";
637 break;
638 case MPI2_EVENT_IR_VOLUME:
Sreekanth Reddy7786ab62015-11-11 17:30:28 +0530639 if (!ioc->hide_ir_msg)
640 desc = "IR Volume";
Sreekanth Reddyf92363d2012-11-30 07:44:21 +0530641 break;
642 case MPI2_EVENT_IR_PHYSICAL_DISK:
Sreekanth Reddy7786ab62015-11-11 17:30:28 +0530643 if (!ioc->hide_ir_msg)
644 desc = "IR Physical Disk";
Sreekanth Reddyf92363d2012-11-30 07:44:21 +0530645 break;
646 case MPI2_EVENT_IR_CONFIGURATION_CHANGE_LIST:
Sreekanth Reddy7786ab62015-11-11 17:30:28 +0530647 if (!ioc->hide_ir_msg)
648 desc = "IR Configuration Change List";
Sreekanth Reddyf92363d2012-11-30 07:44:21 +0530649 break;
650 case MPI2_EVENT_LOG_ENTRY_ADDED:
Sreekanth Reddy7786ab62015-11-11 17:30:28 +0530651 if (!ioc->hide_ir_msg)
652 desc = "Log Entry Added";
Sreekanth Reddyf92363d2012-11-30 07:44:21 +0530653 break;
Sreekanth Reddy2d8ce8c2015-01-12 11:38:56 +0530654 case MPI2_EVENT_TEMP_THRESHOLD:
655 desc = "Temperature Threshold";
656 break;
Sreekanth Reddyf92363d2012-11-30 07:44:21 +0530657 }
658
659 if (!desc)
660 return;
661
662 pr_info(MPT3SAS_FMT "%s\n", ioc->name, desc);
663}
Sreekanth Reddyf92363d2012-11-30 07:44:21 +0530664
665/**
666 * _base_sas_log_info - verbose translation of firmware log info
667 * @ioc: per adapter object
668 * @log_info: log info
669 *
670 * Return nothing.
671 */
672static void
673_base_sas_log_info(struct MPT3SAS_ADAPTER *ioc , u32 log_info)
674{
675 union loginfo_type {
676 u32 loginfo;
677 struct {
678 u32 subcode:16;
679 u32 code:8;
680 u32 originator:4;
681 u32 bus_type:4;
682 } dw;
683 };
684 union loginfo_type sas_loginfo;
685 char *originator_str = NULL;
686
687 sas_loginfo.loginfo = log_info;
688 if (sas_loginfo.dw.bus_type != 3 /*SAS*/)
689 return;
690
691 /* each nexus loss loginfo */
692 if (log_info == 0x31170000)
693 return;
694
695 /* eat the loginfos associated with task aborts */
696 if (ioc->ignore_loginfos && (log_info == 0x30050000 || log_info ==
697 0x31140000 || log_info == 0x31130000))
698 return;
699
700 switch (sas_loginfo.dw.originator) {
701 case 0:
702 originator_str = "IOP";
703 break;
704 case 1:
705 originator_str = "PL";
706 break;
707 case 2:
Sreekanth Reddy7786ab62015-11-11 17:30:28 +0530708 if (!ioc->hide_ir_msg)
709 originator_str = "IR";
710 else
711 originator_str = "WarpDrive";
Sreekanth Reddyf92363d2012-11-30 07:44:21 +0530712 break;
713 }
714
715 pr_warn(MPT3SAS_FMT
716 "log_info(0x%08x): originator(%s), code(0x%02x), sub_code(0x%04x)\n",
717 ioc->name, log_info,
718 originator_str, sas_loginfo.dw.code,
719 sas_loginfo.dw.subcode);
720}
721
722/**
723 * _base_display_reply_info -
724 * @ioc: per adapter object
725 * @smid: system request message index
726 * @msix_index: MSIX table index supplied by the OS
727 * @reply: reply message frame(lower 32bit addr)
728 *
729 * Return nothing.
730 */
731static void
732_base_display_reply_info(struct MPT3SAS_ADAPTER *ioc, u16 smid, u8 msix_index,
733 u32 reply)
734{
735 MPI2DefaultReply_t *mpi_reply;
736 u16 ioc_status;
737 u32 loginfo = 0;
738
739 mpi_reply = mpt3sas_base_get_reply_virt_addr(ioc, reply);
740 if (unlikely(!mpi_reply)) {
741 pr_err(MPT3SAS_FMT "mpi_reply not valid at %s:%d/%s()!\n",
742 ioc->name, __FILE__, __LINE__, __func__);
743 return;
744 }
745 ioc_status = le16_to_cpu(mpi_reply->IOCStatus);
Sreekanth Reddyaf009412015-11-11 17:30:23 +0530746
Sreekanth Reddyf92363d2012-11-30 07:44:21 +0530747 if ((ioc_status & MPI2_IOCSTATUS_MASK) &&
748 (ioc->logging_level & MPT_DEBUG_REPLY)) {
749 _base_sas_ioc_info(ioc , mpi_reply,
750 mpt3sas_base_get_msg_frame(ioc, smid));
751 }
Sreekanth Reddyaf009412015-11-11 17:30:23 +0530752
Sreekanth Reddyf92363d2012-11-30 07:44:21 +0530753 if (ioc_status & MPI2_IOCSTATUS_FLAG_LOG_INFO_AVAILABLE) {
754 loginfo = le32_to_cpu(mpi_reply->IOCLogInfo);
755 _base_sas_log_info(ioc, loginfo);
756 }
757
758 if (ioc_status || loginfo) {
759 ioc_status &= MPI2_IOCSTATUS_MASK;
760 mpt3sas_trigger_mpi(ioc, ioc_status, loginfo);
761 }
762}
763
764/**
765 * mpt3sas_base_done - base internal command completion routine
766 * @ioc: per adapter object
767 * @smid: system request message index
768 * @msix_index: MSIX table index supplied by the OS
769 * @reply: reply message frame(lower 32bit addr)
770 *
771 * Return 1 meaning mf should be freed from _base_interrupt
772 * 0 means the mf is freed from this function.
773 */
774u8
775mpt3sas_base_done(struct MPT3SAS_ADAPTER *ioc, u16 smid, u8 msix_index,
776 u32 reply)
777{
778 MPI2DefaultReply_t *mpi_reply;
779
780 mpi_reply = mpt3sas_base_get_reply_virt_addr(ioc, reply);
781 if (mpi_reply && mpi_reply->Function == MPI2_FUNCTION_EVENT_ACK)
Suganath prabu Subramanifd0331b2016-01-28 12:07:02 +0530782 return mpt3sas_check_for_pending_internal_cmds(ioc, smid);
Sreekanth Reddyf92363d2012-11-30 07:44:21 +0530783
784 if (ioc->base_cmds.status == MPT3_CMD_NOT_USED)
785 return 1;
786
787 ioc->base_cmds.status |= MPT3_CMD_COMPLETE;
788 if (mpi_reply) {
789 ioc->base_cmds.status |= MPT3_CMD_REPLY_VALID;
790 memcpy(ioc->base_cmds.reply, mpi_reply, mpi_reply->MsgLength*4);
791 }
792 ioc->base_cmds.status &= ~MPT3_CMD_PENDING;
793
794 complete(&ioc->base_cmds.done);
795 return 1;
796}
797
798/**
799 * _base_async_event - main callback handler for firmware asyn events
800 * @ioc: per adapter object
801 * @msix_index: MSIX table index supplied by the OS
802 * @reply: reply message frame(lower 32bit addr)
803 *
804 * Return 1 meaning mf should be freed from _base_interrupt
805 * 0 means the mf is freed from this function.
806 */
807static u8
808_base_async_event(struct MPT3SAS_ADAPTER *ioc, u8 msix_index, u32 reply)
809{
810 Mpi2EventNotificationReply_t *mpi_reply;
811 Mpi2EventAckRequest_t *ack_request;
812 u16 smid;
Suganath prabu Subramanifd0331b2016-01-28 12:07:02 +0530813 struct _event_ack_list *delayed_event_ack;
Sreekanth Reddyf92363d2012-11-30 07:44:21 +0530814
815 mpi_reply = mpt3sas_base_get_reply_virt_addr(ioc, reply);
816 if (!mpi_reply)
817 return 1;
818 if (mpi_reply->Function != MPI2_FUNCTION_EVENT_NOTIFICATION)
819 return 1;
Sreekanth Reddyaf009412015-11-11 17:30:23 +0530820
Sreekanth Reddyf92363d2012-11-30 07:44:21 +0530821 _base_display_event_data(ioc, mpi_reply);
Sreekanth Reddyaf009412015-11-11 17:30:23 +0530822
Sreekanth Reddyf92363d2012-11-30 07:44:21 +0530823 if (!(mpi_reply->AckRequired & MPI2_EVENT_NOTIFICATION_ACK_REQUIRED))
824 goto out;
825 smid = mpt3sas_base_get_smid(ioc, ioc->base_cb_idx);
826 if (!smid) {
Suganath prabu Subramanifd0331b2016-01-28 12:07:02 +0530827 delayed_event_ack = kzalloc(sizeof(*delayed_event_ack),
828 GFP_ATOMIC);
829 if (!delayed_event_ack)
830 goto out;
831 INIT_LIST_HEAD(&delayed_event_ack->list);
832 delayed_event_ack->Event = mpi_reply->Event;
833 delayed_event_ack->EventContext = mpi_reply->EventContext;
834 list_add_tail(&delayed_event_ack->list,
835 &ioc->delayed_event_ack_list);
836 dewtprintk(ioc, pr_info(MPT3SAS_FMT
837 "DELAYED: EVENT ACK: event (0x%04x)\n",
838 ioc->name, le16_to_cpu(mpi_reply->Event)));
Sreekanth Reddyf92363d2012-11-30 07:44:21 +0530839 goto out;
840 }
841
842 ack_request = mpt3sas_base_get_msg_frame(ioc, smid);
843 memset(ack_request, 0, sizeof(Mpi2EventAckRequest_t));
844 ack_request->Function = MPI2_FUNCTION_EVENT_ACK;
845 ack_request->Event = mpi_reply->Event;
846 ack_request->EventContext = mpi_reply->EventContext;
847 ack_request->VF_ID = 0; /* TODO */
848 ack_request->VP_ID = 0;
849 mpt3sas_base_put_smid_default(ioc, smid);
850
851 out:
852
853 /* scsih callback handler */
854 mpt3sas_scsih_event_callback(ioc, msix_index, reply);
855
856 /* ctl callback handler */
857 mpt3sas_ctl_event_callback(ioc, msix_index, reply);
858
859 return 1;
860}
861
862/**
863 * _base_get_cb_idx - obtain the callback index
864 * @ioc: per adapter object
865 * @smid: system request message index
866 *
867 * Return callback index.
868 */
869static u8
870_base_get_cb_idx(struct MPT3SAS_ADAPTER *ioc, u16 smid)
871{
872 int i;
873 u8 cb_idx;
874
875 if (smid < ioc->hi_priority_smid) {
876 i = smid - 1;
877 cb_idx = ioc->scsi_lookup[i].cb_idx;
878 } else if (smid < ioc->internal_smid) {
879 i = smid - ioc->hi_priority_smid;
880 cb_idx = ioc->hpr_lookup[i].cb_idx;
881 } else if (smid <= ioc->hba_queue_depth) {
882 i = smid - ioc->internal_smid;
883 cb_idx = ioc->internal_lookup[i].cb_idx;
884 } else
885 cb_idx = 0xFF;
886 return cb_idx;
887}
888
889/**
890 * _base_mask_interrupts - disable interrupts
891 * @ioc: per adapter object
892 *
893 * Disabling ResetIRQ, Reply and Doorbell Interrupts
894 *
895 * Return nothing.
896 */
897static void
898_base_mask_interrupts(struct MPT3SAS_ADAPTER *ioc)
899{
900 u32 him_register;
901
902 ioc->mask_interrupts = 1;
903 him_register = readl(&ioc->chip->HostInterruptMask);
904 him_register |= MPI2_HIM_DIM + MPI2_HIM_RIM + MPI2_HIM_RESET_IRQ_MASK;
905 writel(him_register, &ioc->chip->HostInterruptMask);
906 readl(&ioc->chip->HostInterruptMask);
907}
908
909/**
910 * _base_unmask_interrupts - enable interrupts
911 * @ioc: per adapter object
912 *
913 * Enabling only Reply Interrupts
914 *
915 * Return nothing.
916 */
917static void
918_base_unmask_interrupts(struct MPT3SAS_ADAPTER *ioc)
919{
920 u32 him_register;
921
922 him_register = readl(&ioc->chip->HostInterruptMask);
923 him_register &= ~MPI2_HIM_RIM;
924 writel(him_register, &ioc->chip->HostInterruptMask);
925 ioc->mask_interrupts = 0;
926}
927
928union reply_descriptor {
929 u64 word;
930 struct {
931 u32 low;
932 u32 high;
933 } u;
934};
935
936/**
937 * _base_interrupt - MPT adapter (IOC) specific interrupt handler.
938 * @irq: irq number (not used)
939 * @bus_id: bus identifier cookie == pointer to MPT_ADAPTER structure
940 * @r: pt_regs pointer (not used)
941 *
942 * Return IRQ_HANDLE if processed, else IRQ_NONE.
943 */
944static irqreturn_t
945_base_interrupt(int irq, void *bus_id)
946{
947 struct adapter_reply_queue *reply_q = bus_id;
948 union reply_descriptor rd;
949 u32 completed_cmds;
950 u8 request_desript_type;
951 u16 smid;
952 u8 cb_idx;
953 u32 reply;
954 u8 msix_index = reply_q->msix_index;
955 struct MPT3SAS_ADAPTER *ioc = reply_q->ioc;
956 Mpi2ReplyDescriptorsUnion_t *rpf;
957 u8 rc;
958
959 if (ioc->mask_interrupts)
960 return IRQ_NONE;
961
962 if (!atomic_add_unless(&reply_q->busy, 1, 1))
963 return IRQ_NONE;
964
965 rpf = &reply_q->reply_post_free[reply_q->reply_post_host_index];
966 request_desript_type = rpf->Default.ReplyFlags
967 & MPI2_RPY_DESCRIPT_FLAGS_TYPE_MASK;
968 if (request_desript_type == MPI2_RPY_DESCRIPT_FLAGS_UNUSED) {
969 atomic_dec(&reply_q->busy);
970 return IRQ_NONE;
971 }
972
973 completed_cmds = 0;
974 cb_idx = 0xFF;
975 do {
976 rd.word = le64_to_cpu(rpf->Words);
977 if (rd.u.low == UINT_MAX || rd.u.high == UINT_MAX)
978 goto out;
979 reply = 0;
980 smid = le16_to_cpu(rpf->Default.DescriptorTypeDependent1);
981 if (request_desript_type ==
982 MPI25_RPY_DESCRIPT_FLAGS_FAST_PATH_SCSI_IO_SUCCESS ||
983 request_desript_type ==
984 MPI2_RPY_DESCRIPT_FLAGS_SCSI_IO_SUCCESS) {
985 cb_idx = _base_get_cb_idx(ioc, smid);
986 if ((likely(cb_idx < MPT_MAX_CALLBACKS)) &&
987 (likely(mpt_callbacks[cb_idx] != NULL))) {
988 rc = mpt_callbacks[cb_idx](ioc, smid,
989 msix_index, 0);
990 if (rc)
991 mpt3sas_base_free_smid(ioc, smid);
992 }
993 } else if (request_desript_type ==
994 MPI2_RPY_DESCRIPT_FLAGS_ADDRESS_REPLY) {
995 reply = le32_to_cpu(
996 rpf->AddressReply.ReplyFrameAddress);
997 if (reply > ioc->reply_dma_max_address ||
998 reply < ioc->reply_dma_min_address)
999 reply = 0;
1000 if (smid) {
1001 cb_idx = _base_get_cb_idx(ioc, smid);
1002 if ((likely(cb_idx < MPT_MAX_CALLBACKS)) &&
1003 (likely(mpt_callbacks[cb_idx] != NULL))) {
1004 rc = mpt_callbacks[cb_idx](ioc, smid,
1005 msix_index, reply);
1006 if (reply)
1007 _base_display_reply_info(ioc,
1008 smid, msix_index, reply);
1009 if (rc)
1010 mpt3sas_base_free_smid(ioc,
1011 smid);
1012 }
1013 } else {
1014 _base_async_event(ioc, msix_index, reply);
1015 }
1016
1017 /* reply free queue handling */
1018 if (reply) {
1019 ioc->reply_free_host_index =
1020 (ioc->reply_free_host_index ==
1021 (ioc->reply_free_queue_depth - 1)) ?
1022 0 : ioc->reply_free_host_index + 1;
1023 ioc->reply_free[ioc->reply_free_host_index] =
1024 cpu_to_le32(reply);
1025 wmb();
1026 writel(ioc->reply_free_host_index,
1027 &ioc->chip->ReplyFreeHostIndex);
1028 }
1029 }
1030
1031 rpf->Words = cpu_to_le64(ULLONG_MAX);
1032 reply_q->reply_post_host_index =
1033 (reply_q->reply_post_host_index ==
1034 (ioc->reply_post_queue_depth - 1)) ? 0 :
1035 reply_q->reply_post_host_index + 1;
1036 request_desript_type =
1037 reply_q->reply_post_free[reply_q->reply_post_host_index].
1038 Default.ReplyFlags & MPI2_RPY_DESCRIPT_FLAGS_TYPE_MASK;
1039 completed_cmds++;
1040 if (request_desript_type == MPI2_RPY_DESCRIPT_FLAGS_UNUSED)
1041 goto out;
1042 if (!reply_q->reply_post_host_index)
1043 rpf = reply_q->reply_post_free;
1044 else
1045 rpf++;
1046 } while (1);
1047
1048 out:
1049
1050 if (!completed_cmds) {
1051 atomic_dec(&reply_q->busy);
1052 return IRQ_NONE;
1053 }
1054
1055 wmb();
Sreekanth Reddy7786ab62015-11-11 17:30:28 +05301056 if (ioc->is_warpdrive) {
1057 writel(reply_q->reply_post_host_index,
1058 ioc->reply_post_host_index[msix_index]);
1059 atomic_dec(&reply_q->busy);
1060 return IRQ_HANDLED;
1061 }
Sreekanth Reddyfb77bb52015-06-30 12:24:47 +05301062
1063 /* Update Reply Post Host Index.
1064 * For those HBA's which support combined reply queue feature
1065 * 1. Get the correct Supplemental Reply Post Host Index Register.
1066 * i.e. (msix_index / 8)th entry from Supplemental Reply Post Host
1067 * Index Register address bank i.e replyPostRegisterIndex[],
1068 * 2. Then update this register with new reply host index value
1069 * in ReplyPostIndex field and the MSIxIndex field with
1070 * msix_index value reduced to a value between 0 and 7,
1071 * using a modulo 8 operation. Since each Supplemental Reply Post
1072 * Host Index Register supports 8 MSI-X vectors.
1073 *
1074 * For other HBA's just update the Reply Post Host Index register with
1075 * new reply host index value in ReplyPostIndex Field and msix_index
1076 * value in MSIxIndex field.
1077 */
1078 if (ioc->msix96_vector)
1079 writel(reply_q->reply_post_host_index | ((msix_index & 7) <<
1080 MPI2_RPHI_MSIX_INDEX_SHIFT),
1081 ioc->replyPostRegisterIndex[msix_index/8]);
1082 else
1083 writel(reply_q->reply_post_host_index | (msix_index <<
1084 MPI2_RPHI_MSIX_INDEX_SHIFT),
1085 &ioc->chip->ReplyPostHostIndex);
Sreekanth Reddyf92363d2012-11-30 07:44:21 +05301086 atomic_dec(&reply_q->busy);
1087 return IRQ_HANDLED;
1088}
1089
1090/**
1091 * _base_is_controller_msix_enabled - is controller support muli-reply queues
1092 * @ioc: per adapter object
1093 *
1094 */
1095static inline int
1096_base_is_controller_msix_enabled(struct MPT3SAS_ADAPTER *ioc)
1097{
1098 return (ioc->facts.IOCCapabilities &
1099 MPI2_IOCFACTS_CAPABILITY_MSI_X_INDEX) && ioc->msix_enable;
1100}
1101
1102/**
1103 * mpt3sas_base_flush_reply_queues - flushing the MSIX reply queues
1104 * @ioc: per adapter object
1105 * Context: ISR conext
1106 *
1107 * Called when a Task Management request has completed. We want
1108 * to flush the other reply queues so all the outstanding IO has been
1109 * completed back to OS before we process the TM completetion.
1110 *
1111 * Return nothing.
1112 */
1113void
1114mpt3sas_base_flush_reply_queues(struct MPT3SAS_ADAPTER *ioc)
1115{
1116 struct adapter_reply_queue *reply_q;
1117
1118 /* If MSIX capability is turned off
1119 * then multi-queues are not enabled
1120 */
1121 if (!_base_is_controller_msix_enabled(ioc))
1122 return;
1123
1124 list_for_each_entry(reply_q, &ioc->reply_queue_list, list) {
1125 if (ioc->shost_recovery)
1126 return;
1127 /* TMs are on msix_index == 0 */
1128 if (reply_q->msix_index == 0)
1129 continue;
1130 _base_interrupt(reply_q->vector, (void *)reply_q);
1131 }
1132}
1133
1134/**
1135 * mpt3sas_base_release_callback_handler - clear interrupt callback handler
1136 * @cb_idx: callback index
1137 *
1138 * Return nothing.
1139 */
1140void
1141mpt3sas_base_release_callback_handler(u8 cb_idx)
1142{
1143 mpt_callbacks[cb_idx] = NULL;
1144}
1145
1146/**
1147 * mpt3sas_base_register_callback_handler - obtain index for the interrupt callback handler
1148 * @cb_func: callback function
1149 *
1150 * Returns cb_func.
1151 */
1152u8
1153mpt3sas_base_register_callback_handler(MPT_CALLBACK cb_func)
1154{
1155 u8 cb_idx;
1156
1157 for (cb_idx = MPT_MAX_CALLBACKS-1; cb_idx; cb_idx--)
1158 if (mpt_callbacks[cb_idx] == NULL)
1159 break;
1160
1161 mpt_callbacks[cb_idx] = cb_func;
1162 return cb_idx;
1163}
1164
1165/**
1166 * mpt3sas_base_initialize_callback_handler - initialize the interrupt callback handler
1167 *
1168 * Return nothing.
1169 */
1170void
1171mpt3sas_base_initialize_callback_handler(void)
1172{
1173 u8 cb_idx;
1174
1175 for (cb_idx = 0; cb_idx < MPT_MAX_CALLBACKS; cb_idx++)
1176 mpt3sas_base_release_callback_handler(cb_idx);
1177}
1178
1179
1180/**
1181 * _base_build_zero_len_sge - build zero length sg entry
1182 * @ioc: per adapter object
1183 * @paddr: virtual address for SGE
1184 *
1185 * Create a zero length scatter gather entry to insure the IOCs hardware has
1186 * something to use if the target device goes brain dead and tries
1187 * to send data even when none is asked for.
1188 *
1189 * Return nothing.
1190 */
1191static void
1192_base_build_zero_len_sge(struct MPT3SAS_ADAPTER *ioc, void *paddr)
1193{
1194 u32 flags_length = (u32)((MPI2_SGE_FLAGS_LAST_ELEMENT |
1195 MPI2_SGE_FLAGS_END_OF_BUFFER | MPI2_SGE_FLAGS_END_OF_LIST |
1196 MPI2_SGE_FLAGS_SIMPLE_ELEMENT) <<
1197 MPI2_SGE_FLAGS_SHIFT);
1198 ioc->base_add_sg_single(paddr, flags_length, -1);
1199}
1200
1201/**
1202 * _base_add_sg_single_32 - Place a simple 32 bit SGE at address pAddr.
1203 * @paddr: virtual address for SGE
1204 * @flags_length: SGE flags and data transfer length
1205 * @dma_addr: Physical address
1206 *
1207 * Return nothing.
1208 */
1209static void
1210_base_add_sg_single_32(void *paddr, u32 flags_length, dma_addr_t dma_addr)
1211{
1212 Mpi2SGESimple32_t *sgel = paddr;
1213
1214 flags_length |= (MPI2_SGE_FLAGS_32_BIT_ADDRESSING |
1215 MPI2_SGE_FLAGS_SYSTEM_ADDRESS) << MPI2_SGE_FLAGS_SHIFT;
1216 sgel->FlagsLength = cpu_to_le32(flags_length);
1217 sgel->Address = cpu_to_le32(dma_addr);
1218}
1219
1220
1221/**
1222 * _base_add_sg_single_64 - Place a simple 64 bit SGE at address pAddr.
1223 * @paddr: virtual address for SGE
1224 * @flags_length: SGE flags and data transfer length
1225 * @dma_addr: Physical address
1226 *
1227 * Return nothing.
1228 */
1229static void
1230_base_add_sg_single_64(void *paddr, u32 flags_length, dma_addr_t dma_addr)
1231{
1232 Mpi2SGESimple64_t *sgel = paddr;
1233
1234 flags_length |= (MPI2_SGE_FLAGS_64_BIT_ADDRESSING |
1235 MPI2_SGE_FLAGS_SYSTEM_ADDRESS) << MPI2_SGE_FLAGS_SHIFT;
1236 sgel->FlagsLength = cpu_to_le32(flags_length);
1237 sgel->Address = cpu_to_le64(dma_addr);
1238}
1239
1240/**
1241 * _base_get_chain_buffer_tracker - obtain chain tracker
1242 * @ioc: per adapter object
1243 * @smid: smid associated to an IO request
1244 *
1245 * Returns chain tracker(from ioc->free_chain_list)
1246 */
1247static struct chain_tracker *
1248_base_get_chain_buffer_tracker(struct MPT3SAS_ADAPTER *ioc, u16 smid)
1249{
1250 struct chain_tracker *chain_req;
1251 unsigned long flags;
1252
1253 spin_lock_irqsave(&ioc->scsi_lookup_lock, flags);
1254 if (list_empty(&ioc->free_chain_list)) {
1255 spin_unlock_irqrestore(&ioc->scsi_lookup_lock, flags);
1256 dfailprintk(ioc, pr_warn(MPT3SAS_FMT
1257 "chain buffers not available\n", ioc->name));
1258 return NULL;
1259 }
1260 chain_req = list_entry(ioc->free_chain_list.next,
1261 struct chain_tracker, tracker_list);
1262 list_del_init(&chain_req->tracker_list);
1263 list_add_tail(&chain_req->tracker_list,
1264 &ioc->scsi_lookup[smid - 1].chain_list);
1265 spin_unlock_irqrestore(&ioc->scsi_lookup_lock, flags);
1266 return chain_req;
1267}
1268
1269
1270/**
1271 * _base_build_sg - build generic sg
1272 * @ioc: per adapter object
1273 * @psge: virtual address for SGE
1274 * @data_out_dma: physical address for WRITES
1275 * @data_out_sz: data xfer size for WRITES
1276 * @data_in_dma: physical address for READS
1277 * @data_in_sz: data xfer size for READS
1278 *
1279 * Return nothing.
1280 */
1281static void
1282_base_build_sg(struct MPT3SAS_ADAPTER *ioc, void *psge,
1283 dma_addr_t data_out_dma, size_t data_out_sz, dma_addr_t data_in_dma,
1284 size_t data_in_sz)
1285{
1286 u32 sgl_flags;
1287
1288 if (!data_out_sz && !data_in_sz) {
1289 _base_build_zero_len_sge(ioc, psge);
1290 return;
1291 }
1292
1293 if (data_out_sz && data_in_sz) {
1294 /* WRITE sgel first */
1295 sgl_flags = (MPI2_SGE_FLAGS_SIMPLE_ELEMENT |
1296 MPI2_SGE_FLAGS_END_OF_BUFFER | MPI2_SGE_FLAGS_HOST_TO_IOC);
1297 sgl_flags = sgl_flags << MPI2_SGE_FLAGS_SHIFT;
1298 ioc->base_add_sg_single(psge, sgl_flags |
1299 data_out_sz, data_out_dma);
1300
1301 /* incr sgel */
1302 psge += ioc->sge_size;
1303
1304 /* READ sgel last */
1305 sgl_flags = (MPI2_SGE_FLAGS_SIMPLE_ELEMENT |
1306 MPI2_SGE_FLAGS_LAST_ELEMENT | MPI2_SGE_FLAGS_END_OF_BUFFER |
1307 MPI2_SGE_FLAGS_END_OF_LIST);
1308 sgl_flags = sgl_flags << MPI2_SGE_FLAGS_SHIFT;
1309 ioc->base_add_sg_single(psge, sgl_flags |
1310 data_in_sz, data_in_dma);
1311 } else if (data_out_sz) /* WRITE */ {
1312 sgl_flags = (MPI2_SGE_FLAGS_SIMPLE_ELEMENT |
1313 MPI2_SGE_FLAGS_LAST_ELEMENT | MPI2_SGE_FLAGS_END_OF_BUFFER |
1314 MPI2_SGE_FLAGS_END_OF_LIST | MPI2_SGE_FLAGS_HOST_TO_IOC);
1315 sgl_flags = sgl_flags << MPI2_SGE_FLAGS_SHIFT;
1316 ioc->base_add_sg_single(psge, sgl_flags |
1317 data_out_sz, data_out_dma);
1318 } else if (data_in_sz) /* READ */ {
1319 sgl_flags = (MPI2_SGE_FLAGS_SIMPLE_ELEMENT |
1320 MPI2_SGE_FLAGS_LAST_ELEMENT | MPI2_SGE_FLAGS_END_OF_BUFFER |
1321 MPI2_SGE_FLAGS_END_OF_LIST);
1322 sgl_flags = sgl_flags << MPI2_SGE_FLAGS_SHIFT;
1323 ioc->base_add_sg_single(psge, sgl_flags |
1324 data_in_sz, data_in_dma);
1325 }
1326}
1327
1328/* IEEE format sgls */
1329
1330/**
1331 * _base_add_sg_single_ieee - add sg element for IEEE format
1332 * @paddr: virtual address for SGE
1333 * @flags: SGE flags
1334 * @chain_offset: number of 128 byte elements from start of segment
1335 * @length: data transfer length
1336 * @dma_addr: Physical address
1337 *
1338 * Return nothing.
1339 */
1340static void
1341_base_add_sg_single_ieee(void *paddr, u8 flags, u8 chain_offset, u32 length,
1342 dma_addr_t dma_addr)
1343{
1344 Mpi25IeeeSgeChain64_t *sgel = paddr;
1345
1346 sgel->Flags = flags;
1347 sgel->NextChainOffset = chain_offset;
1348 sgel->Length = cpu_to_le32(length);
1349 sgel->Address = cpu_to_le64(dma_addr);
1350}
1351
1352/**
1353 * _base_build_zero_len_sge_ieee - build zero length sg entry for IEEE format
1354 * @ioc: per adapter object
1355 * @paddr: virtual address for SGE
1356 *
1357 * Create a zero length scatter gather entry to insure the IOCs hardware has
1358 * something to use if the target device goes brain dead and tries
1359 * to send data even when none is asked for.
1360 *
1361 * Return nothing.
1362 */
1363static void
1364_base_build_zero_len_sge_ieee(struct MPT3SAS_ADAPTER *ioc, void *paddr)
1365{
1366 u8 sgl_flags = (MPI2_IEEE_SGE_FLAGS_SIMPLE_ELEMENT |
1367 MPI2_IEEE_SGE_FLAGS_SYSTEM_ADDR |
1368 MPI25_IEEE_SGE_FLAGS_END_OF_LIST);
Suganath prabu Subramanib130b0d2016-01-28 12:06:58 +05301369
Sreekanth Reddyf92363d2012-11-30 07:44:21 +05301370 _base_add_sg_single_ieee(paddr, sgl_flags, 0, 0, -1);
1371}
1372
1373/**
Sreekanth Reddy471ef9d2015-11-11 17:30:24 +05301374 * _base_build_sg_scmd - main sg creation routine
1375 * @ioc: per adapter object
1376 * @scmd: scsi command
1377 * @smid: system request message index
1378 * Context: none.
1379 *
1380 * The main routine that builds scatter gather table from a given
1381 * scsi request sent via the .queuecommand main handler.
1382 *
1383 * Returns 0 success, anything else error
1384 */
1385static int
1386_base_build_sg_scmd(struct MPT3SAS_ADAPTER *ioc,
1387 struct scsi_cmnd *scmd, u16 smid)
1388{
1389 Mpi2SCSIIORequest_t *mpi_request;
1390 dma_addr_t chain_dma;
1391 struct scatterlist *sg_scmd;
1392 void *sg_local, *chain;
1393 u32 chain_offset;
1394 u32 chain_length;
1395 u32 chain_flags;
1396 int sges_left;
1397 u32 sges_in_segment;
1398 u32 sgl_flags;
1399 u32 sgl_flags_last_element;
1400 u32 sgl_flags_end_buffer;
1401 struct chain_tracker *chain_req;
1402
1403 mpi_request = mpt3sas_base_get_msg_frame(ioc, smid);
1404
1405 /* init scatter gather flags */
1406 sgl_flags = MPI2_SGE_FLAGS_SIMPLE_ELEMENT;
1407 if (scmd->sc_data_direction == DMA_TO_DEVICE)
1408 sgl_flags |= MPI2_SGE_FLAGS_HOST_TO_IOC;
1409 sgl_flags_last_element = (sgl_flags | MPI2_SGE_FLAGS_LAST_ELEMENT)
1410 << MPI2_SGE_FLAGS_SHIFT;
1411 sgl_flags_end_buffer = (sgl_flags | MPI2_SGE_FLAGS_LAST_ELEMENT |
1412 MPI2_SGE_FLAGS_END_OF_BUFFER | MPI2_SGE_FLAGS_END_OF_LIST)
1413 << MPI2_SGE_FLAGS_SHIFT;
1414 sgl_flags = sgl_flags << MPI2_SGE_FLAGS_SHIFT;
1415
1416 sg_scmd = scsi_sglist(scmd);
1417 sges_left = scsi_dma_map(scmd);
1418 if (sges_left < 0) {
1419 sdev_printk(KERN_ERR, scmd->device,
1420 "pci_map_sg failed: request for %d bytes!\n",
1421 scsi_bufflen(scmd));
1422 return -ENOMEM;
1423 }
1424
1425 sg_local = &mpi_request->SGL;
1426 sges_in_segment = ioc->max_sges_in_main_message;
1427 if (sges_left <= sges_in_segment)
1428 goto fill_in_last_segment;
1429
1430 mpi_request->ChainOffset = (offsetof(Mpi2SCSIIORequest_t, SGL) +
1431 (sges_in_segment * ioc->sge_size))/4;
1432
1433 /* fill in main message segment when there is a chain following */
1434 while (sges_in_segment) {
1435 if (sges_in_segment == 1)
1436 ioc->base_add_sg_single(sg_local,
1437 sgl_flags_last_element | sg_dma_len(sg_scmd),
1438 sg_dma_address(sg_scmd));
1439 else
1440 ioc->base_add_sg_single(sg_local, sgl_flags |
1441 sg_dma_len(sg_scmd), sg_dma_address(sg_scmd));
1442 sg_scmd = sg_next(sg_scmd);
1443 sg_local += ioc->sge_size;
1444 sges_left--;
1445 sges_in_segment--;
1446 }
1447
1448 /* initializing the chain flags and pointers */
1449 chain_flags = MPI2_SGE_FLAGS_CHAIN_ELEMENT << MPI2_SGE_FLAGS_SHIFT;
1450 chain_req = _base_get_chain_buffer_tracker(ioc, smid);
1451 if (!chain_req)
1452 return -1;
1453 chain = chain_req->chain_buffer;
1454 chain_dma = chain_req->chain_buffer_dma;
1455 do {
1456 sges_in_segment = (sges_left <=
1457 ioc->max_sges_in_chain_message) ? sges_left :
1458 ioc->max_sges_in_chain_message;
1459 chain_offset = (sges_left == sges_in_segment) ?
1460 0 : (sges_in_segment * ioc->sge_size)/4;
1461 chain_length = sges_in_segment * ioc->sge_size;
1462 if (chain_offset) {
1463 chain_offset = chain_offset <<
1464 MPI2_SGE_CHAIN_OFFSET_SHIFT;
1465 chain_length += ioc->sge_size;
1466 }
1467 ioc->base_add_sg_single(sg_local, chain_flags | chain_offset |
1468 chain_length, chain_dma);
1469 sg_local = chain;
1470 if (!chain_offset)
1471 goto fill_in_last_segment;
1472
1473 /* fill in chain segments */
1474 while (sges_in_segment) {
1475 if (sges_in_segment == 1)
1476 ioc->base_add_sg_single(sg_local,
1477 sgl_flags_last_element |
1478 sg_dma_len(sg_scmd),
1479 sg_dma_address(sg_scmd));
1480 else
1481 ioc->base_add_sg_single(sg_local, sgl_flags |
1482 sg_dma_len(sg_scmd),
1483 sg_dma_address(sg_scmd));
1484 sg_scmd = sg_next(sg_scmd);
1485 sg_local += ioc->sge_size;
1486 sges_left--;
1487 sges_in_segment--;
1488 }
1489
1490 chain_req = _base_get_chain_buffer_tracker(ioc, smid);
1491 if (!chain_req)
1492 return -1;
1493 chain = chain_req->chain_buffer;
1494 chain_dma = chain_req->chain_buffer_dma;
1495 } while (1);
1496
1497
1498 fill_in_last_segment:
1499
1500 /* fill the last segment */
1501 while (sges_left) {
1502 if (sges_left == 1)
1503 ioc->base_add_sg_single(sg_local, sgl_flags_end_buffer |
1504 sg_dma_len(sg_scmd), sg_dma_address(sg_scmd));
1505 else
1506 ioc->base_add_sg_single(sg_local, sgl_flags |
1507 sg_dma_len(sg_scmd), sg_dma_address(sg_scmd));
1508 sg_scmd = sg_next(sg_scmd);
1509 sg_local += ioc->sge_size;
1510 sges_left--;
1511 }
1512
1513 return 0;
1514}
1515
1516/**
Sreekanth Reddyf92363d2012-11-30 07:44:21 +05301517 * _base_build_sg_scmd_ieee - main sg creation routine for IEEE format
1518 * @ioc: per adapter object
1519 * @scmd: scsi command
1520 * @smid: system request message index
1521 * Context: none.
1522 *
1523 * The main routine that builds scatter gather table from a given
1524 * scsi request sent via the .queuecommand main handler.
1525 *
1526 * Returns 0 success, anything else error
1527 */
1528static int
1529_base_build_sg_scmd_ieee(struct MPT3SAS_ADAPTER *ioc,
1530 struct scsi_cmnd *scmd, u16 smid)
1531{
1532 Mpi2SCSIIORequest_t *mpi_request;
1533 dma_addr_t chain_dma;
1534 struct scatterlist *sg_scmd;
1535 void *sg_local, *chain;
1536 u32 chain_offset;
1537 u32 chain_length;
Sreekanth Reddyf92363d2012-11-30 07:44:21 +05301538 int sges_left;
1539 u32 sges_in_segment;
1540 u8 simple_sgl_flags;
1541 u8 simple_sgl_flags_last;
1542 u8 chain_sgl_flags;
1543 struct chain_tracker *chain_req;
1544
1545 mpi_request = mpt3sas_base_get_msg_frame(ioc, smid);
1546
1547 /* init scatter gather flags */
1548 simple_sgl_flags = MPI2_IEEE_SGE_FLAGS_SIMPLE_ELEMENT |
1549 MPI2_IEEE_SGE_FLAGS_SYSTEM_ADDR;
1550 simple_sgl_flags_last = simple_sgl_flags |
1551 MPI25_IEEE_SGE_FLAGS_END_OF_LIST;
1552 chain_sgl_flags = MPI2_IEEE_SGE_FLAGS_CHAIN_ELEMENT |
1553 MPI2_IEEE_SGE_FLAGS_SYSTEM_ADDR;
1554
1555 sg_scmd = scsi_sglist(scmd);
1556 sges_left = scsi_dma_map(scmd);
Sreekanth Reddy62f5c742015-06-30 12:25:01 +05301557 if (sges_left < 0) {
Sreekanth Reddyf92363d2012-11-30 07:44:21 +05301558 sdev_printk(KERN_ERR, scmd->device,
1559 "pci_map_sg failed: request for %d bytes!\n",
1560 scsi_bufflen(scmd));
1561 return -ENOMEM;
1562 }
1563
1564 sg_local = &mpi_request->SGL;
1565 sges_in_segment = (ioc->request_sz -
1566 offsetof(Mpi2SCSIIORequest_t, SGL))/ioc->sge_size_ieee;
1567 if (sges_left <= sges_in_segment)
1568 goto fill_in_last_segment;
1569
1570 mpi_request->ChainOffset = (sges_in_segment - 1 /* chain element */) +
1571 (offsetof(Mpi2SCSIIORequest_t, SGL)/ioc->sge_size_ieee);
1572
1573 /* fill in main message segment when there is a chain following */
1574 while (sges_in_segment > 1) {
1575 _base_add_sg_single_ieee(sg_local, simple_sgl_flags, 0,
1576 sg_dma_len(sg_scmd), sg_dma_address(sg_scmd));
1577 sg_scmd = sg_next(sg_scmd);
1578 sg_local += ioc->sge_size_ieee;
1579 sges_left--;
1580 sges_in_segment--;
1581 }
1582
Wei Yongjun25ef16d2012-12-12 02:26:51 +05301583 /* initializing the pointers */
Sreekanth Reddyf92363d2012-11-30 07:44:21 +05301584 chain_req = _base_get_chain_buffer_tracker(ioc, smid);
1585 if (!chain_req)
1586 return -1;
1587 chain = chain_req->chain_buffer;
1588 chain_dma = chain_req->chain_buffer_dma;
1589 do {
1590 sges_in_segment = (sges_left <=
1591 ioc->max_sges_in_chain_message) ? sges_left :
1592 ioc->max_sges_in_chain_message;
1593 chain_offset = (sges_left == sges_in_segment) ?
1594 0 : sges_in_segment;
1595 chain_length = sges_in_segment * ioc->sge_size_ieee;
1596 if (chain_offset)
1597 chain_length += ioc->sge_size_ieee;
1598 _base_add_sg_single_ieee(sg_local, chain_sgl_flags,
1599 chain_offset, chain_length, chain_dma);
1600
1601 sg_local = chain;
1602 if (!chain_offset)
1603 goto fill_in_last_segment;
1604
1605 /* fill in chain segments */
1606 while (sges_in_segment) {
1607 _base_add_sg_single_ieee(sg_local, simple_sgl_flags, 0,
1608 sg_dma_len(sg_scmd), sg_dma_address(sg_scmd));
1609 sg_scmd = sg_next(sg_scmd);
1610 sg_local += ioc->sge_size_ieee;
1611 sges_left--;
1612 sges_in_segment--;
1613 }
1614
1615 chain_req = _base_get_chain_buffer_tracker(ioc, smid);
1616 if (!chain_req)
1617 return -1;
1618 chain = chain_req->chain_buffer;
1619 chain_dma = chain_req->chain_buffer_dma;
1620 } while (1);
1621
1622
1623 fill_in_last_segment:
1624
1625 /* fill the last segment */
Sreekanth Reddy62f5c742015-06-30 12:25:01 +05301626 while (sges_left > 0) {
Sreekanth Reddyf92363d2012-11-30 07:44:21 +05301627 if (sges_left == 1)
1628 _base_add_sg_single_ieee(sg_local,
1629 simple_sgl_flags_last, 0, sg_dma_len(sg_scmd),
1630 sg_dma_address(sg_scmd));
1631 else
1632 _base_add_sg_single_ieee(sg_local, simple_sgl_flags, 0,
1633 sg_dma_len(sg_scmd), sg_dma_address(sg_scmd));
1634 sg_scmd = sg_next(sg_scmd);
1635 sg_local += ioc->sge_size_ieee;
1636 sges_left--;
1637 }
1638
1639 return 0;
1640}
1641
1642/**
1643 * _base_build_sg_ieee - build generic sg for IEEE format
1644 * @ioc: per adapter object
1645 * @psge: virtual address for SGE
1646 * @data_out_dma: physical address for WRITES
1647 * @data_out_sz: data xfer size for WRITES
1648 * @data_in_dma: physical address for READS
1649 * @data_in_sz: data xfer size for READS
1650 *
1651 * Return nothing.
1652 */
1653static void
1654_base_build_sg_ieee(struct MPT3SAS_ADAPTER *ioc, void *psge,
1655 dma_addr_t data_out_dma, size_t data_out_sz, dma_addr_t data_in_dma,
1656 size_t data_in_sz)
1657{
1658 u8 sgl_flags;
1659
1660 if (!data_out_sz && !data_in_sz) {
1661 _base_build_zero_len_sge_ieee(ioc, psge);
1662 return;
1663 }
1664
1665 if (data_out_sz && data_in_sz) {
1666 /* WRITE sgel first */
1667 sgl_flags = MPI2_IEEE_SGE_FLAGS_SIMPLE_ELEMENT |
1668 MPI2_IEEE_SGE_FLAGS_SYSTEM_ADDR;
1669 _base_add_sg_single_ieee(psge, sgl_flags, 0, data_out_sz,
1670 data_out_dma);
1671
1672 /* incr sgel */
1673 psge += ioc->sge_size_ieee;
1674
1675 /* READ sgel last */
1676 sgl_flags |= MPI25_IEEE_SGE_FLAGS_END_OF_LIST;
1677 _base_add_sg_single_ieee(psge, sgl_flags, 0, data_in_sz,
1678 data_in_dma);
1679 } else if (data_out_sz) /* WRITE */ {
1680 sgl_flags = MPI2_IEEE_SGE_FLAGS_SIMPLE_ELEMENT |
1681 MPI25_IEEE_SGE_FLAGS_END_OF_LIST |
1682 MPI2_IEEE_SGE_FLAGS_SYSTEM_ADDR;
1683 _base_add_sg_single_ieee(psge, sgl_flags, 0, data_out_sz,
1684 data_out_dma);
1685 } else if (data_in_sz) /* READ */ {
1686 sgl_flags = MPI2_IEEE_SGE_FLAGS_SIMPLE_ELEMENT |
1687 MPI25_IEEE_SGE_FLAGS_END_OF_LIST |
1688 MPI2_IEEE_SGE_FLAGS_SYSTEM_ADDR;
1689 _base_add_sg_single_ieee(psge, sgl_flags, 0, data_in_sz,
1690 data_in_dma);
1691 }
1692}
1693
1694#define convert_to_kb(x) ((x) << (PAGE_SHIFT - 10))
1695
1696/**
1697 * _base_config_dma_addressing - set dma addressing
1698 * @ioc: per adapter object
1699 * @pdev: PCI device struct
1700 *
1701 * Returns 0 for success, non-zero for failure.
1702 */
1703static int
1704_base_config_dma_addressing(struct MPT3SAS_ADAPTER *ioc, struct pci_dev *pdev)
1705{
1706 struct sysinfo s;
Sreekanth Reddy9b05c912014-09-12 15:35:31 +05301707 u64 consistent_dma_mask;
1708
1709 if (ioc->dma_mask)
1710 consistent_dma_mask = DMA_BIT_MASK(64);
1711 else
1712 consistent_dma_mask = DMA_BIT_MASK(32);
Sreekanth Reddyf92363d2012-11-30 07:44:21 +05301713
1714 if (sizeof(dma_addr_t) > 4) {
1715 const uint64_t required_mask =
1716 dma_get_required_mask(&pdev->dev);
1717 if ((required_mask > DMA_BIT_MASK(32)) &&
1718 !pci_set_dma_mask(pdev, DMA_BIT_MASK(64)) &&
Sreekanth Reddy9b05c912014-09-12 15:35:31 +05301719 !pci_set_consistent_dma_mask(pdev, consistent_dma_mask)) {
Sreekanth Reddyf92363d2012-11-30 07:44:21 +05301720 ioc->base_add_sg_single = &_base_add_sg_single_64;
1721 ioc->sge_size = sizeof(Mpi2SGESimple64_t);
Sreekanth Reddy9b05c912014-09-12 15:35:31 +05301722 ioc->dma_mask = 64;
Sreekanth Reddyf92363d2012-11-30 07:44:21 +05301723 goto out;
1724 }
1725 }
1726
1727 if (!pci_set_dma_mask(pdev, DMA_BIT_MASK(32))
1728 && !pci_set_consistent_dma_mask(pdev, DMA_BIT_MASK(32))) {
1729 ioc->base_add_sg_single = &_base_add_sg_single_32;
1730 ioc->sge_size = sizeof(Mpi2SGESimple32_t);
Sreekanth Reddy9b05c912014-09-12 15:35:31 +05301731 ioc->dma_mask = 32;
Sreekanth Reddyf92363d2012-11-30 07:44:21 +05301732 } else
1733 return -ENODEV;
1734
1735 out:
1736 si_meminfo(&s);
1737 pr_info(MPT3SAS_FMT
Sreekanth Reddy9b05c912014-09-12 15:35:31 +05301738 "%d BIT PCI BUS DMA ADDRESSING SUPPORTED, total mem (%ld kB)\n",
1739 ioc->name, ioc->dma_mask, convert_to_kb(s.totalram));
Sreekanth Reddyf92363d2012-11-30 07:44:21 +05301740
1741 return 0;
1742}
1743
Sreekanth Reddy9b05c912014-09-12 15:35:31 +05301744static int
1745_base_change_consistent_dma_mask(struct MPT3SAS_ADAPTER *ioc,
1746 struct pci_dev *pdev)
1747{
1748 if (pci_set_consistent_dma_mask(pdev, DMA_BIT_MASK(64))) {
1749 if (pci_set_consistent_dma_mask(pdev, DMA_BIT_MASK(32)))
1750 return -ENODEV;
1751 }
1752 return 0;
1753}
1754
Sreekanth Reddyf92363d2012-11-30 07:44:21 +05301755/**
1756 * _base_check_enable_msix - checks MSIX capabable.
1757 * @ioc: per adapter object
1758 *
1759 * Check to see if card is capable of MSIX, and set number
1760 * of available msix vectors
1761 */
1762static int
1763_base_check_enable_msix(struct MPT3SAS_ADAPTER *ioc)
1764{
1765 int base;
1766 u16 message_control;
1767
Sreekanth Reddy42081172015-11-11 17:30:26 +05301768 /* Check whether controller SAS2008 B0 controller,
1769 * if it is SAS2008 B0 controller use IO-APIC instead of MSIX
1770 */
1771 if (ioc->pdev->device == MPI2_MFGPAGE_DEVID_SAS2008 &&
1772 ioc->pdev->revision == SAS2_PCI_DEVICE_B0_REVISION) {
1773 return -EINVAL;
1774 }
1775
Sreekanth Reddyf92363d2012-11-30 07:44:21 +05301776 base = pci_find_capability(ioc->pdev, PCI_CAP_ID_MSIX);
1777 if (!base) {
1778 dfailprintk(ioc, pr_info(MPT3SAS_FMT "msix not supported\n",
1779 ioc->name));
1780 return -EINVAL;
1781 }
1782
1783 /* get msix vector count */
Sreekanth Reddy42081172015-11-11 17:30:26 +05301784 /* NUMA_IO not supported for older controllers */
1785 if (ioc->pdev->device == MPI2_MFGPAGE_DEVID_SAS2004 ||
1786 ioc->pdev->device == MPI2_MFGPAGE_DEVID_SAS2008 ||
1787 ioc->pdev->device == MPI2_MFGPAGE_DEVID_SAS2108_1 ||
1788 ioc->pdev->device == MPI2_MFGPAGE_DEVID_SAS2108_2 ||
1789 ioc->pdev->device == MPI2_MFGPAGE_DEVID_SAS2108_3 ||
1790 ioc->pdev->device == MPI2_MFGPAGE_DEVID_SAS2116_1 ||
1791 ioc->pdev->device == MPI2_MFGPAGE_DEVID_SAS2116_2)
1792 ioc->msix_vector_count = 1;
1793 else {
1794 pci_read_config_word(ioc->pdev, base + 2, &message_control);
1795 ioc->msix_vector_count = (message_control & 0x3FF) + 1;
1796 }
Sreekanth Reddyf92363d2012-11-30 07:44:21 +05301797 dinitprintk(ioc, pr_info(MPT3SAS_FMT
1798 "msix is supported, vector_count(%d)\n",
1799 ioc->name, ioc->msix_vector_count));
1800 return 0;
1801}
1802
1803/**
1804 * _base_free_irq - free irq
1805 * @ioc: per adapter object
1806 *
1807 * Freeing respective reply_queue from the list.
1808 */
1809static void
1810_base_free_irq(struct MPT3SAS_ADAPTER *ioc)
1811{
1812 struct adapter_reply_queue *reply_q, *next;
1813
1814 if (list_empty(&ioc->reply_queue_list))
1815 return;
1816
1817 list_for_each_entry_safe(reply_q, next, &ioc->reply_queue_list, list) {
1818 list_del(&reply_q->list);
Suganath Prabu Subramani64038302016-02-08 22:13:39 +05301819 if (smp_affinity_enable) {
1820 irq_set_affinity_hint(reply_q->vector, NULL);
1821 free_cpumask_var(reply_q->affinity_hint);
1822 }
Sreekanth Reddyf92363d2012-11-30 07:44:21 +05301823 synchronize_irq(reply_q->vector);
1824 free_irq(reply_q->vector, reply_q);
1825 kfree(reply_q);
1826 }
1827}
1828
1829/**
1830 * _base_request_irq - request irq
1831 * @ioc: per adapter object
1832 * @index: msix index into vector table
1833 * @vector: irq vector
1834 *
1835 * Inserting respective reply_queue into the list.
1836 */
1837static int
1838_base_request_irq(struct MPT3SAS_ADAPTER *ioc, u8 index, u32 vector)
1839{
1840 struct adapter_reply_queue *reply_q;
1841 int r;
1842
1843 reply_q = kzalloc(sizeof(struct adapter_reply_queue), GFP_KERNEL);
1844 if (!reply_q) {
1845 pr_err(MPT3SAS_FMT "unable to allocate memory %d!\n",
1846 ioc->name, (int)sizeof(struct adapter_reply_queue));
1847 return -ENOMEM;
1848 }
1849 reply_q->ioc = ioc;
1850 reply_q->msix_index = index;
1851 reply_q->vector = vector;
Sreekanth Reddy14b31142015-01-12 11:39:03 +05301852
Suganath Prabu Subramani64038302016-02-08 22:13:39 +05301853 if (smp_affinity_enable) {
1854 if (!zalloc_cpumask_var(&reply_q->affinity_hint, GFP_KERNEL)) {
1855 kfree(reply_q);
1856 return -ENOMEM;
1857 }
1858 cpumask_clear(reply_q->affinity_hint);
1859 }
Sreekanth Reddy14b31142015-01-12 11:39:03 +05301860
Sreekanth Reddyf92363d2012-11-30 07:44:21 +05301861 atomic_set(&reply_q->busy, 0);
1862 if (ioc->msix_enable)
1863 snprintf(reply_q->name, MPT_NAME_LENGTH, "%s%d-msix%d",
Sreekanth Reddyc84b06a2015-11-11 17:30:35 +05301864 ioc->driver_name, ioc->id, index);
Sreekanth Reddyf92363d2012-11-30 07:44:21 +05301865 else
1866 snprintf(reply_q->name, MPT_NAME_LENGTH, "%s%d",
Sreekanth Reddyc84b06a2015-11-11 17:30:35 +05301867 ioc->driver_name, ioc->id);
Sreekanth Reddyf92363d2012-11-30 07:44:21 +05301868 r = request_irq(vector, _base_interrupt, IRQF_SHARED, reply_q->name,
1869 reply_q);
1870 if (r) {
1871 pr_err(MPT3SAS_FMT "unable to allocate interrupt %d!\n",
1872 reply_q->name, vector);
1873 kfree(reply_q);
Suganath Prabu Subramani64038302016-02-08 22:13:39 +05301874 free_cpumask_var(reply_q->affinity_hint);
Sreekanth Reddyf92363d2012-11-30 07:44:21 +05301875 return -EBUSY;
1876 }
1877
1878 INIT_LIST_HEAD(&reply_q->list);
1879 list_add_tail(&reply_q->list, &ioc->reply_queue_list);
1880 return 0;
1881}
1882
1883/**
1884 * _base_assign_reply_queues - assigning msix index for each cpu
1885 * @ioc: per adapter object
1886 *
1887 * The enduser would need to set the affinity via /proc/irq/#/smp_affinity
1888 *
1889 * It would nice if we could call irq_set_affinity, however it is not
1890 * an exported symbol
1891 */
1892static void
1893_base_assign_reply_queues(struct MPT3SAS_ADAPTER *ioc)
1894{
Martin K. Petersen91b265b2014-01-03 19:16:56 -05001895 unsigned int cpu, nr_cpus, nr_msix, index = 0;
Sreekanth Reddy14b31142015-01-12 11:39:03 +05301896 struct adapter_reply_queue *reply_q;
Sreekanth Reddyf92363d2012-11-30 07:44:21 +05301897
1898 if (!_base_is_controller_msix_enabled(ioc))
1899 return;
1900
1901 memset(ioc->cpu_msix_table, 0, ioc->cpu_msix_table_sz);
1902
Martin K. Petersen91b265b2014-01-03 19:16:56 -05001903 nr_cpus = num_online_cpus();
1904 nr_msix = ioc->reply_queue_count = min(ioc->reply_queue_count,
1905 ioc->facts.MaxMSIxVectors);
1906 if (!nr_msix)
1907 return;
Sreekanth Reddyf92363d2012-11-30 07:44:21 +05301908
Martin K. Petersen91b265b2014-01-03 19:16:56 -05001909 cpu = cpumask_first(cpu_online_mask);
Sreekanth Reddyf92363d2012-11-30 07:44:21 +05301910
Sreekanth Reddy14b31142015-01-12 11:39:03 +05301911 list_for_each_entry(reply_q, &ioc->reply_queue_list, list) {
1912
Martin K. Petersen91b265b2014-01-03 19:16:56 -05001913 unsigned int i, group = nr_cpus / nr_msix;
1914
Sreekanth Reddy14b31142015-01-12 11:39:03 +05301915 if (cpu >= nr_cpus)
1916 break;
1917
Martin K. Petersen91b265b2014-01-03 19:16:56 -05001918 if (index < nr_cpus % nr_msix)
1919 group++;
1920
1921 for (i = 0 ; i < group ; i++) {
1922 ioc->cpu_msix_table[cpu] = index;
Suganath Prabu Subramani64038302016-02-08 22:13:39 +05301923 if (smp_affinity_enable)
1924 cpumask_or(reply_q->affinity_hint,
Sreekanth Reddy14b31142015-01-12 11:39:03 +05301925 reply_q->affinity_hint, get_cpu_mask(cpu));
Martin K. Petersen91b265b2014-01-03 19:16:56 -05001926 cpu = cpumask_next(cpu, cpu_online_mask);
Sreekanth Reddyf92363d2012-11-30 07:44:21 +05301927 }
Suganath Prabu Subramani64038302016-02-08 22:13:39 +05301928 if (smp_affinity_enable)
1929 if (irq_set_affinity_hint(reply_q->vector,
Sreekanth Reddy14b31142015-01-12 11:39:03 +05301930 reply_q->affinity_hint))
Suganath Prabu Subramani64038302016-02-08 22:13:39 +05301931 dinitprintk(ioc, pr_info(MPT3SAS_FMT
1932 "Err setting affinity hint to irq vector %d\n",
1933 ioc->name, reply_q->vector));
Martin K. Petersen91b265b2014-01-03 19:16:56 -05001934 index++;
Sreekanth Reddy14b31142015-01-12 11:39:03 +05301935 }
Sreekanth Reddyf92363d2012-11-30 07:44:21 +05301936}
1937
1938/**
1939 * _base_disable_msix - disables msix
1940 * @ioc: per adapter object
1941 *
1942 */
1943static void
1944_base_disable_msix(struct MPT3SAS_ADAPTER *ioc)
1945{
1946 if (!ioc->msix_enable)
1947 return;
1948 pci_disable_msix(ioc->pdev);
1949 ioc->msix_enable = 0;
1950}
1951
1952/**
1953 * _base_enable_msix - enables msix, failback to io_apic
1954 * @ioc: per adapter object
1955 *
1956 */
1957static int
1958_base_enable_msix(struct MPT3SAS_ADAPTER *ioc)
1959{
1960 struct msix_entry *entries, *a;
1961 int r;
1962 int i;
1963 u8 try_msix = 0;
1964
Sreekanth Reddyf92363d2012-11-30 07:44:21 +05301965 if (msix_disable == -1 || msix_disable == 0)
1966 try_msix = 1;
1967
1968 if (!try_msix)
1969 goto try_ioapic;
1970
1971 if (_base_check_enable_msix(ioc) != 0)
1972 goto try_ioapic;
1973
1974 ioc->reply_queue_count = min_t(int, ioc->cpu_count,
1975 ioc->msix_vector_count);
1976
Sreekanth Reddy9c500062013-08-14 18:23:20 +05301977 printk(MPT3SAS_FMT "MSI-X vectors supported: %d, no of cores"
1978 ": %d, max_msix_vectors: %d\n", ioc->name, ioc->msix_vector_count,
1979 ioc->cpu_count, max_msix_vectors);
1980
Sreekanth Reddy9b05c912014-09-12 15:35:31 +05301981 if (!ioc->rdpq_array_enable && max_msix_vectors == -1)
1982 max_msix_vectors = 8;
1983
Sreekanth Reddy9c500062013-08-14 18:23:20 +05301984 if (max_msix_vectors > 0) {
1985 ioc->reply_queue_count = min_t(int, max_msix_vectors,
1986 ioc->reply_queue_count);
1987 ioc->msix_vector_count = ioc->reply_queue_count;
Sreekanth Reddy9b05c912014-09-12 15:35:31 +05301988 } else if (max_msix_vectors == 0)
1989 goto try_ioapic;
Sreekanth Reddy9c500062013-08-14 18:23:20 +05301990
Suganath Prabu Subramani64038302016-02-08 22:13:39 +05301991 if (ioc->msix_vector_count < ioc->cpu_count)
1992 smp_affinity_enable = 0;
1993
Sreekanth Reddyf92363d2012-11-30 07:44:21 +05301994 entries = kcalloc(ioc->reply_queue_count, sizeof(struct msix_entry),
1995 GFP_KERNEL);
1996 if (!entries) {
1997 dfailprintk(ioc, pr_info(MPT3SAS_FMT
1998 "kcalloc failed @ at %s:%d/%s() !!!\n",
1999 ioc->name, __FILE__, __LINE__, __func__));
2000 goto try_ioapic;
2001 }
2002
2003 for (i = 0, a = entries; i < ioc->reply_queue_count; i++, a++)
2004 a->entry = i;
2005
Alexander Gordeev6bfa6902014-08-18 08:01:46 +02002006 r = pci_enable_msix_exact(ioc->pdev, entries, ioc->reply_queue_count);
Sreekanth Reddyf92363d2012-11-30 07:44:21 +05302007 if (r) {
2008 dfailprintk(ioc, pr_info(MPT3SAS_FMT
Alexander Gordeev6bfa6902014-08-18 08:01:46 +02002009 "pci_enable_msix_exact failed (r=%d) !!!\n",
Sreekanth Reddyf92363d2012-11-30 07:44:21 +05302010 ioc->name, r));
2011 kfree(entries);
2012 goto try_ioapic;
2013 }
2014
2015 ioc->msix_enable = 1;
2016 for (i = 0, a = entries; i < ioc->reply_queue_count; i++, a++) {
2017 r = _base_request_irq(ioc, i, a->vector);
2018 if (r) {
2019 _base_free_irq(ioc);
2020 _base_disable_msix(ioc);
2021 kfree(entries);
2022 goto try_ioapic;
2023 }
2024 }
2025
2026 kfree(entries);
2027 return 0;
2028
2029/* failback to io_apic interrupt routing */
2030 try_ioapic:
2031
Sreekanth Reddy9b05c912014-09-12 15:35:31 +05302032 ioc->reply_queue_count = 1;
Sreekanth Reddyf92363d2012-11-30 07:44:21 +05302033 r = _base_request_irq(ioc, 0, ioc->pdev->irq);
2034
2035 return r;
2036}
2037
2038/**
Sreekanth Reddy580d4e32015-06-30 12:24:50 +05302039 * mpt3sas_base_unmap_resources - free controller resources
2040 * @ioc: per adapter object
2041 */
2042void
2043mpt3sas_base_unmap_resources(struct MPT3SAS_ADAPTER *ioc)
2044{
2045 struct pci_dev *pdev = ioc->pdev;
2046
2047 dexitprintk(ioc, printk(MPT3SAS_FMT "%s\n",
2048 ioc->name, __func__));
2049
2050 _base_free_irq(ioc);
2051 _base_disable_msix(ioc);
2052
Tomas Henzl5f985d82015-12-23 14:21:47 +01002053 if (ioc->msix96_vector) {
Sreekanth Reddy580d4e32015-06-30 12:24:50 +05302054 kfree(ioc->replyPostRegisterIndex);
Tomas Henzl5f985d82015-12-23 14:21:47 +01002055 ioc->replyPostRegisterIndex = NULL;
2056 }
Sreekanth Reddy580d4e32015-06-30 12:24:50 +05302057
2058 if (ioc->chip_phys) {
2059 iounmap(ioc->chip);
2060 ioc->chip_phys = 0;
2061 }
2062
2063 if (pci_is_enabled(pdev)) {
2064 pci_release_selected_regions(ioc->pdev, ioc->bars);
2065 pci_disable_pcie_error_reporting(pdev);
2066 pci_disable_device(pdev);
2067 }
2068}
2069
2070/**
Sreekanth Reddyf92363d2012-11-30 07:44:21 +05302071 * mpt3sas_base_map_resources - map in controller resources (io/irq/memap)
2072 * @ioc: per adapter object
2073 *
2074 * Returns 0 for success, non-zero for failure.
2075 */
2076int
2077mpt3sas_base_map_resources(struct MPT3SAS_ADAPTER *ioc)
2078{
2079 struct pci_dev *pdev = ioc->pdev;
2080 u32 memap_sz;
2081 u32 pio_sz;
2082 int i, r = 0;
2083 u64 pio_chip = 0;
2084 u64 chip_phys = 0;
2085 struct adapter_reply_queue *reply_q;
2086
2087 dinitprintk(ioc, pr_info(MPT3SAS_FMT "%s\n",
2088 ioc->name, __func__));
2089
2090 ioc->bars = pci_select_bars(pdev, IORESOURCE_MEM);
2091 if (pci_enable_device_mem(pdev)) {
2092 pr_warn(MPT3SAS_FMT "pci_enable_device_mem: failed\n",
2093 ioc->name);
Joe Lawrencecf9bd21a2013-08-08 16:45:39 -04002094 ioc->bars = 0;
Sreekanth Reddyf92363d2012-11-30 07:44:21 +05302095 return -ENODEV;
2096 }
2097
2098
2099 if (pci_request_selected_regions(pdev, ioc->bars,
Sreekanth Reddyc84b06a2015-11-11 17:30:35 +05302100 ioc->driver_name)) {
Sreekanth Reddyf92363d2012-11-30 07:44:21 +05302101 pr_warn(MPT3SAS_FMT "pci_request_selected_regions: failed\n",
2102 ioc->name);
Joe Lawrencecf9bd21a2013-08-08 16:45:39 -04002103 ioc->bars = 0;
Sreekanth Reddyf92363d2012-11-30 07:44:21 +05302104 r = -ENODEV;
2105 goto out_fail;
2106 }
2107
2108/* AER (Advanced Error Reporting) hooks */
2109 pci_enable_pcie_error_reporting(pdev);
2110
2111 pci_set_master(pdev);
2112
2113
2114 if (_base_config_dma_addressing(ioc, pdev) != 0) {
2115 pr_warn(MPT3SAS_FMT "no suitable DMA mask for %s\n",
2116 ioc->name, pci_name(pdev));
2117 r = -ENODEV;
2118 goto out_fail;
2119 }
2120
Sreekanth Reddy5aeeb782015-07-15 10:19:56 +05302121 for (i = 0, memap_sz = 0, pio_sz = 0; (i < DEVICE_COUNT_RESOURCE) &&
2122 (!memap_sz || !pio_sz); i++) {
Sreekanth Reddyf92363d2012-11-30 07:44:21 +05302123 if (pci_resource_flags(pdev, i) & IORESOURCE_IO) {
2124 if (pio_sz)
2125 continue;
2126 pio_chip = (u64)pci_resource_start(pdev, i);
2127 pio_sz = pci_resource_len(pdev, i);
2128 } else if (pci_resource_flags(pdev, i) & IORESOURCE_MEM) {
2129 if (memap_sz)
2130 continue;
2131 ioc->chip_phys = pci_resource_start(pdev, i);
2132 chip_phys = (u64)ioc->chip_phys;
2133 memap_sz = pci_resource_len(pdev, i);
2134 ioc->chip = ioremap(ioc->chip_phys, memap_sz);
Sreekanth Reddyf92363d2012-11-30 07:44:21 +05302135 }
2136 }
2137
Sreekanth Reddy5aeeb782015-07-15 10:19:56 +05302138 if (ioc->chip == NULL) {
2139 pr_err(MPT3SAS_FMT "unable to map adapter memory! "
2140 " or resource not found\n", ioc->name);
2141 r = -EINVAL;
2142 goto out_fail;
2143 }
2144
Sreekanth Reddyf92363d2012-11-30 07:44:21 +05302145 _base_mask_interrupts(ioc);
Sreekanth Reddy9b05c912014-09-12 15:35:31 +05302146
2147 r = _base_get_ioc_facts(ioc, CAN_SLEEP);
2148 if (r)
2149 goto out_fail;
2150
2151 if (!ioc->rdpq_array_enable_assigned) {
2152 ioc->rdpq_array_enable = ioc->rdpq_array_capable;
2153 ioc->rdpq_array_enable_assigned = 1;
2154 }
2155
Sreekanth Reddyf92363d2012-11-30 07:44:21 +05302156 r = _base_enable_msix(ioc);
2157 if (r)
2158 goto out_fail;
2159
Sreekanth Reddyfb77bb52015-06-30 12:24:47 +05302160 /* Use the Combined reply queue feature only for SAS3 C0 & higher
2161 * revision HBAs and also only when reply queue count is greater than 8
2162 */
2163 if (ioc->msix96_vector && ioc->reply_queue_count > 8) {
2164 /* Determine the Supplemental Reply Post Host Index Registers
2165 * Addresse. Supplemental Reply Post Host Index Registers
2166 * starts at offset MPI25_SUP_REPLY_POST_HOST_INDEX_OFFSET and
2167 * each register is at offset bytes of
2168 * MPT3_SUP_REPLY_POST_HOST_INDEX_REG_OFFSET from previous one.
2169 */
2170 ioc->replyPostRegisterIndex = kcalloc(
2171 MPT3_SUP_REPLY_POST_HOST_INDEX_REG_COUNT,
2172 sizeof(resource_size_t *), GFP_KERNEL);
2173 if (!ioc->replyPostRegisterIndex) {
2174 dfailprintk(ioc, printk(MPT3SAS_FMT
2175 "allocation for reply Post Register Index failed!!!\n",
2176 ioc->name));
2177 r = -ENOMEM;
2178 goto out_fail;
2179 }
2180
2181 for (i = 0; i < MPT3_SUP_REPLY_POST_HOST_INDEX_REG_COUNT; i++) {
2182 ioc->replyPostRegisterIndex[i] = (resource_size_t *)
2183 ((u8 *)&ioc->chip->Doorbell +
2184 MPI25_SUP_REPLY_POST_HOST_INDEX_OFFSET +
2185 (i * MPT3_SUP_REPLY_POST_HOST_INDEX_REG_OFFSET));
2186 }
2187 } else
2188 ioc->msix96_vector = 0;
2189
Sreekanth Reddyf92363d2012-11-30 07:44:21 +05302190 list_for_each_entry(reply_q, &ioc->reply_queue_list, list)
2191 pr_info(MPT3SAS_FMT "%s: IRQ %d\n",
2192 reply_q->name, ((ioc->msix_enable) ? "PCI-MSI-X enabled" :
2193 "IO-APIC enabled"), reply_q->vector);
2194
2195 pr_info(MPT3SAS_FMT "iomem(0x%016llx), mapped(0x%p), size(%d)\n",
2196 ioc->name, (unsigned long long)chip_phys, ioc->chip, memap_sz);
2197 pr_info(MPT3SAS_FMT "ioport(0x%016llx), size(%d)\n",
2198 ioc->name, (unsigned long long)pio_chip, pio_sz);
2199
2200 /* Save PCI configuration state for recovery from PCI AER/EEH errors */
2201 pci_save_state(pdev);
2202 return 0;
2203
2204 out_fail:
Sreekanth Reddy580d4e32015-06-30 12:24:50 +05302205 mpt3sas_base_unmap_resources(ioc);
Sreekanth Reddyf92363d2012-11-30 07:44:21 +05302206 return r;
2207}
2208
2209/**
2210 * mpt3sas_base_get_msg_frame - obtain request mf pointer
2211 * @ioc: per adapter object
2212 * @smid: system request message index(smid zero is invalid)
2213 *
2214 * Returns virt pointer to message frame.
2215 */
2216void *
2217mpt3sas_base_get_msg_frame(struct MPT3SAS_ADAPTER *ioc, u16 smid)
2218{
2219 return (void *)(ioc->request + (smid * ioc->request_sz));
2220}
2221
2222/**
2223 * mpt3sas_base_get_sense_buffer - obtain a sense buffer virt addr
2224 * @ioc: per adapter object
2225 * @smid: system request message index
2226 *
2227 * Returns virt pointer to sense buffer.
2228 */
2229void *
2230mpt3sas_base_get_sense_buffer(struct MPT3SAS_ADAPTER *ioc, u16 smid)
2231{
2232 return (void *)(ioc->sense + ((smid - 1) * SCSI_SENSE_BUFFERSIZE));
2233}
2234
2235/**
2236 * mpt3sas_base_get_sense_buffer_dma - obtain a sense buffer dma addr
2237 * @ioc: per adapter object
2238 * @smid: system request message index
2239 *
2240 * Returns phys pointer to the low 32bit address of the sense buffer.
2241 */
2242__le32
2243mpt3sas_base_get_sense_buffer_dma(struct MPT3SAS_ADAPTER *ioc, u16 smid)
2244{
2245 return cpu_to_le32(ioc->sense_dma + ((smid - 1) *
2246 SCSI_SENSE_BUFFERSIZE));
2247}
2248
2249/**
2250 * mpt3sas_base_get_reply_virt_addr - obtain reply frames virt address
2251 * @ioc: per adapter object
2252 * @phys_addr: lower 32 physical addr of the reply
2253 *
2254 * Converts 32bit lower physical addr into a virt address.
2255 */
2256void *
2257mpt3sas_base_get_reply_virt_addr(struct MPT3SAS_ADAPTER *ioc, u32 phys_addr)
2258{
2259 if (!phys_addr)
2260 return NULL;
2261 return ioc->reply + (phys_addr - (u32)ioc->reply_dma);
2262}
2263
Suganath prabu Subramani03d1fb32016-01-28 12:07:06 +05302264static inline u8
2265_base_get_msix_index(struct MPT3SAS_ADAPTER *ioc)
2266{
2267 return ioc->cpu_msix_table[raw_smp_processor_id()];
2268}
2269
Sreekanth Reddyf92363d2012-11-30 07:44:21 +05302270/**
2271 * mpt3sas_base_get_smid - obtain a free smid from internal queue
2272 * @ioc: per adapter object
2273 * @cb_idx: callback index
2274 *
2275 * Returns smid (zero is invalid)
2276 */
2277u16
2278mpt3sas_base_get_smid(struct MPT3SAS_ADAPTER *ioc, u8 cb_idx)
2279{
2280 unsigned long flags;
2281 struct request_tracker *request;
2282 u16 smid;
2283
2284 spin_lock_irqsave(&ioc->scsi_lookup_lock, flags);
2285 if (list_empty(&ioc->internal_free_list)) {
2286 spin_unlock_irqrestore(&ioc->scsi_lookup_lock, flags);
2287 pr_err(MPT3SAS_FMT "%s: smid not available\n",
2288 ioc->name, __func__);
2289 return 0;
2290 }
2291
2292 request = list_entry(ioc->internal_free_list.next,
2293 struct request_tracker, tracker_list);
2294 request->cb_idx = cb_idx;
2295 smid = request->smid;
2296 list_del(&request->tracker_list);
2297 spin_unlock_irqrestore(&ioc->scsi_lookup_lock, flags);
2298 return smid;
2299}
2300
2301/**
2302 * mpt3sas_base_get_smid_scsiio - obtain a free smid from scsiio queue
2303 * @ioc: per adapter object
2304 * @cb_idx: callback index
2305 * @scmd: pointer to scsi command object
2306 *
2307 * Returns smid (zero is invalid)
2308 */
2309u16
2310mpt3sas_base_get_smid_scsiio(struct MPT3SAS_ADAPTER *ioc, u8 cb_idx,
2311 struct scsi_cmnd *scmd)
2312{
2313 unsigned long flags;
2314 struct scsiio_tracker *request;
2315 u16 smid;
2316
2317 spin_lock_irqsave(&ioc->scsi_lookup_lock, flags);
2318 if (list_empty(&ioc->free_list)) {
2319 spin_unlock_irqrestore(&ioc->scsi_lookup_lock, flags);
2320 pr_err(MPT3SAS_FMT "%s: smid not available\n",
2321 ioc->name, __func__);
2322 return 0;
2323 }
2324
2325 request = list_entry(ioc->free_list.next,
2326 struct scsiio_tracker, tracker_list);
2327 request->scmd = scmd;
2328 request->cb_idx = cb_idx;
2329 smid = request->smid;
Suganath prabu Subramani03d1fb32016-01-28 12:07:06 +05302330 request->msix_io = _base_get_msix_index(ioc);
Sreekanth Reddyf92363d2012-11-30 07:44:21 +05302331 list_del(&request->tracker_list);
2332 spin_unlock_irqrestore(&ioc->scsi_lookup_lock, flags);
2333 return smid;
2334}
2335
2336/**
2337 * mpt3sas_base_get_smid_hpr - obtain a free smid from hi-priority queue
2338 * @ioc: per adapter object
2339 * @cb_idx: callback index
2340 *
2341 * Returns smid (zero is invalid)
2342 */
2343u16
2344mpt3sas_base_get_smid_hpr(struct MPT3SAS_ADAPTER *ioc, u8 cb_idx)
2345{
2346 unsigned long flags;
2347 struct request_tracker *request;
2348 u16 smid;
2349
2350 spin_lock_irqsave(&ioc->scsi_lookup_lock, flags);
2351 if (list_empty(&ioc->hpr_free_list)) {
2352 spin_unlock_irqrestore(&ioc->scsi_lookup_lock, flags);
2353 return 0;
2354 }
2355
2356 request = list_entry(ioc->hpr_free_list.next,
2357 struct request_tracker, tracker_list);
2358 request->cb_idx = cb_idx;
2359 smid = request->smid;
2360 list_del(&request->tracker_list);
2361 spin_unlock_irqrestore(&ioc->scsi_lookup_lock, flags);
2362 return smid;
2363}
2364
2365/**
2366 * mpt3sas_base_free_smid - put smid back on free_list
2367 * @ioc: per adapter object
2368 * @smid: system request message index
2369 *
2370 * Return nothing.
2371 */
2372void
2373mpt3sas_base_free_smid(struct MPT3SAS_ADAPTER *ioc, u16 smid)
2374{
2375 unsigned long flags;
2376 int i;
2377 struct chain_tracker *chain_req, *next;
2378
2379 spin_lock_irqsave(&ioc->scsi_lookup_lock, flags);
2380 if (smid < ioc->hi_priority_smid) {
2381 /* scsiio queue */
2382 i = smid - 1;
2383 if (!list_empty(&ioc->scsi_lookup[i].chain_list)) {
2384 list_for_each_entry_safe(chain_req, next,
2385 &ioc->scsi_lookup[i].chain_list, tracker_list) {
2386 list_del_init(&chain_req->tracker_list);
2387 list_add(&chain_req->tracker_list,
2388 &ioc->free_chain_list);
2389 }
2390 }
2391 ioc->scsi_lookup[i].cb_idx = 0xFF;
2392 ioc->scsi_lookup[i].scmd = NULL;
Sreekanth Reddy7786ab62015-11-11 17:30:28 +05302393 ioc->scsi_lookup[i].direct_io = 0;
Sreekanth Reddyf92363d2012-11-30 07:44:21 +05302394 list_add(&ioc->scsi_lookup[i].tracker_list, &ioc->free_list);
2395 spin_unlock_irqrestore(&ioc->scsi_lookup_lock, flags);
2396
2397 /*
2398 * See _wait_for_commands_to_complete() call with regards
2399 * to this code.
2400 */
2401 if (ioc->shost_recovery && ioc->pending_io_count) {
2402 if (ioc->pending_io_count == 1)
2403 wake_up(&ioc->reset_wq);
2404 ioc->pending_io_count--;
2405 }
2406 return;
2407 } else if (smid < ioc->internal_smid) {
2408 /* hi-priority */
2409 i = smid - ioc->hi_priority_smid;
2410 ioc->hpr_lookup[i].cb_idx = 0xFF;
2411 list_add(&ioc->hpr_lookup[i].tracker_list, &ioc->hpr_free_list);
2412 } else if (smid <= ioc->hba_queue_depth) {
2413 /* internal queue */
2414 i = smid - ioc->internal_smid;
2415 ioc->internal_lookup[i].cb_idx = 0xFF;
2416 list_add(&ioc->internal_lookup[i].tracker_list,
2417 &ioc->internal_free_list);
2418 }
2419 spin_unlock_irqrestore(&ioc->scsi_lookup_lock, flags);
2420}
2421
2422/**
2423 * _base_writeq - 64 bit write to MMIO
2424 * @ioc: per adapter object
2425 * @b: data payload
2426 * @addr: address in MMIO space
2427 * @writeq_lock: spin lock
2428 *
2429 * Glue for handling an atomic 64 bit word to MMIO. This special handling takes
2430 * care of 32 bit environment where its not quarenteed to send the entire word
2431 * in one transfer.
2432 */
2433#if defined(writeq) && defined(CONFIG_64BIT)
2434static inline void
2435_base_writeq(__u64 b, volatile void __iomem *addr, spinlock_t *writeq_lock)
2436{
2437 writeq(cpu_to_le64(b), addr);
2438}
2439#else
2440static inline void
2441_base_writeq(__u64 b, volatile void __iomem *addr, spinlock_t *writeq_lock)
2442{
2443 unsigned long flags;
2444 __u64 data_out = cpu_to_le64(b);
2445
2446 spin_lock_irqsave(writeq_lock, flags);
2447 writel((u32)(data_out), addr);
2448 writel((u32)(data_out >> 32), (addr + 4));
2449 spin_unlock_irqrestore(writeq_lock, flags);
2450}
2451#endif
2452
Sreekanth Reddyf92363d2012-11-30 07:44:21 +05302453/**
2454 * mpt3sas_base_put_smid_scsi_io - send SCSI_IO request to firmware
2455 * @ioc: per adapter object
2456 * @smid: system request message index
2457 * @handle: device handle
2458 *
2459 * Return nothing.
2460 */
2461void
2462mpt3sas_base_put_smid_scsi_io(struct MPT3SAS_ADAPTER *ioc, u16 smid, u16 handle)
2463{
2464 Mpi2RequestDescriptorUnion_t descriptor;
2465 u64 *request = (u64 *)&descriptor;
2466
2467
2468 descriptor.SCSIIO.RequestFlags = MPI2_REQ_DESCRIPT_FLAGS_SCSI_IO;
2469 descriptor.SCSIIO.MSIxIndex = _base_get_msix_index(ioc);
2470 descriptor.SCSIIO.SMID = cpu_to_le16(smid);
2471 descriptor.SCSIIO.DevHandle = cpu_to_le16(handle);
2472 descriptor.SCSIIO.LMID = 0;
2473 _base_writeq(*request, &ioc->chip->RequestDescriptorPostLow,
2474 &ioc->scsi_lookup_lock);
2475}
2476
2477/**
2478 * mpt3sas_base_put_smid_fast_path - send fast path request to firmware
2479 * @ioc: per adapter object
2480 * @smid: system request message index
2481 * @handle: device handle
2482 *
2483 * Return nothing.
2484 */
2485void
2486mpt3sas_base_put_smid_fast_path(struct MPT3SAS_ADAPTER *ioc, u16 smid,
2487 u16 handle)
2488{
2489 Mpi2RequestDescriptorUnion_t descriptor;
2490 u64 *request = (u64 *)&descriptor;
2491
2492 descriptor.SCSIIO.RequestFlags =
2493 MPI25_REQ_DESCRIPT_FLAGS_FAST_PATH_SCSI_IO;
2494 descriptor.SCSIIO.MSIxIndex = _base_get_msix_index(ioc);
2495 descriptor.SCSIIO.SMID = cpu_to_le16(smid);
2496 descriptor.SCSIIO.DevHandle = cpu_to_le16(handle);
2497 descriptor.SCSIIO.LMID = 0;
2498 _base_writeq(*request, &ioc->chip->RequestDescriptorPostLow,
2499 &ioc->scsi_lookup_lock);
2500}
2501
2502/**
2503 * mpt3sas_base_put_smid_hi_priority - send Task Managment request to firmware
2504 * @ioc: per adapter object
2505 * @smid: system request message index
Suganath prabu Subramani03d1fb32016-01-28 12:07:06 +05302506 * @msix_task: msix_task will be same as msix of IO incase of task abort else 0.
Sreekanth Reddyf92363d2012-11-30 07:44:21 +05302507 * Return nothing.
2508 */
2509void
Suganath prabu Subramani03d1fb32016-01-28 12:07:06 +05302510mpt3sas_base_put_smid_hi_priority(struct MPT3SAS_ADAPTER *ioc, u16 smid,
2511 u16 msix_task)
Sreekanth Reddyf92363d2012-11-30 07:44:21 +05302512{
2513 Mpi2RequestDescriptorUnion_t descriptor;
2514 u64 *request = (u64 *)&descriptor;
2515
2516 descriptor.HighPriority.RequestFlags =
2517 MPI2_REQ_DESCRIPT_FLAGS_HIGH_PRIORITY;
Suganath prabu Subramani03d1fb32016-01-28 12:07:06 +05302518 descriptor.HighPriority.MSIxIndex = msix_task;
Sreekanth Reddyf92363d2012-11-30 07:44:21 +05302519 descriptor.HighPriority.SMID = cpu_to_le16(smid);
2520 descriptor.HighPriority.LMID = 0;
2521 descriptor.HighPriority.Reserved1 = 0;
2522 _base_writeq(*request, &ioc->chip->RequestDescriptorPostLow,
2523 &ioc->scsi_lookup_lock);
2524}
2525
2526/**
2527 * mpt3sas_base_put_smid_default - Default, primarily used for config pages
2528 * @ioc: per adapter object
2529 * @smid: system request message index
2530 *
2531 * Return nothing.
2532 */
2533void
2534mpt3sas_base_put_smid_default(struct MPT3SAS_ADAPTER *ioc, u16 smid)
2535{
2536 Mpi2RequestDescriptorUnion_t descriptor;
2537 u64 *request = (u64 *)&descriptor;
2538
2539 descriptor.Default.RequestFlags = MPI2_REQ_DESCRIPT_FLAGS_DEFAULT_TYPE;
2540 descriptor.Default.MSIxIndex = _base_get_msix_index(ioc);
2541 descriptor.Default.SMID = cpu_to_le16(smid);
2542 descriptor.Default.LMID = 0;
2543 descriptor.Default.DescriptorTypeDependent = 0;
2544 _base_writeq(*request, &ioc->chip->RequestDescriptorPostLow,
2545 &ioc->scsi_lookup_lock);
2546}
2547
Sreekanth Reddy1117b312014-09-12 15:35:30 +05302548/**
Sreekanth Reddy989e43c2015-11-11 17:30:32 +05302549 * _base_display_OEMs_branding - Display branding string
Sreekanth Reddy1117b312014-09-12 15:35:30 +05302550 * @ioc: per adapter object
2551 *
2552 * Return nothing.
2553 */
2554static void
Sreekanth Reddy989e43c2015-11-11 17:30:32 +05302555_base_display_OEMs_branding(struct MPT3SAS_ADAPTER *ioc)
Sreekanth Reddy1117b312014-09-12 15:35:30 +05302556{
2557 if (ioc->pdev->subsystem_vendor != PCI_VENDOR_ID_INTEL)
2558 return;
2559
Sreekanth Reddy989e43c2015-11-11 17:30:32 +05302560 switch (ioc->pdev->subsystem_vendor) {
2561 case PCI_VENDOR_ID_INTEL:
2562 switch (ioc->pdev->device) {
2563 case MPI2_MFGPAGE_DEVID_SAS2008:
2564 switch (ioc->pdev->subsystem_device) {
2565 case MPT2SAS_INTEL_RMS2LL080_SSDID:
2566 pr_info(MPT3SAS_FMT "%s\n", ioc->name,
2567 MPT2SAS_INTEL_RMS2LL080_BRANDING);
2568 break;
2569 case MPT2SAS_INTEL_RMS2LL040_SSDID:
2570 pr_info(MPT3SAS_FMT "%s\n", ioc->name,
2571 MPT2SAS_INTEL_RMS2LL040_BRANDING);
2572 break;
2573 case MPT2SAS_INTEL_SSD910_SSDID:
2574 pr_info(MPT3SAS_FMT "%s\n", ioc->name,
2575 MPT2SAS_INTEL_SSD910_BRANDING);
2576 break;
2577 default:
2578 pr_info(MPT3SAS_FMT
2579 "Intel(R) Controller: Subsystem ID: 0x%X\n",
2580 ioc->name, ioc->pdev->subsystem_device);
2581 break;
2582 }
2583 case MPI2_MFGPAGE_DEVID_SAS2308_2:
2584 switch (ioc->pdev->subsystem_device) {
2585 case MPT2SAS_INTEL_RS25GB008_SSDID:
2586 pr_info(MPT3SAS_FMT "%s\n", ioc->name,
2587 MPT2SAS_INTEL_RS25GB008_BRANDING);
2588 break;
2589 case MPT2SAS_INTEL_RMS25JB080_SSDID:
2590 pr_info(MPT3SAS_FMT "%s\n", ioc->name,
2591 MPT2SAS_INTEL_RMS25JB080_BRANDING);
2592 break;
2593 case MPT2SAS_INTEL_RMS25JB040_SSDID:
2594 pr_info(MPT3SAS_FMT "%s\n", ioc->name,
2595 MPT2SAS_INTEL_RMS25JB040_BRANDING);
2596 break;
2597 case MPT2SAS_INTEL_RMS25KB080_SSDID:
2598 pr_info(MPT3SAS_FMT "%s\n", ioc->name,
2599 MPT2SAS_INTEL_RMS25KB080_BRANDING);
2600 break;
2601 case MPT2SAS_INTEL_RMS25KB040_SSDID:
2602 pr_info(MPT3SAS_FMT "%s\n", ioc->name,
2603 MPT2SAS_INTEL_RMS25KB040_BRANDING);
2604 break;
2605 case MPT2SAS_INTEL_RMS25LB040_SSDID:
2606 pr_info(MPT3SAS_FMT "%s\n", ioc->name,
2607 MPT2SAS_INTEL_RMS25LB040_BRANDING);
2608 break;
2609 case MPT2SAS_INTEL_RMS25LB080_SSDID:
2610 pr_info(MPT3SAS_FMT "%s\n", ioc->name,
2611 MPT2SAS_INTEL_RMS25LB080_BRANDING);
2612 break;
2613 default:
2614 pr_info(MPT3SAS_FMT
2615 "Intel(R) Controller: Subsystem ID: 0x%X\n",
2616 ioc->name, ioc->pdev->subsystem_device);
2617 break;
2618 }
2619 case MPI25_MFGPAGE_DEVID_SAS3008:
2620 switch (ioc->pdev->subsystem_device) {
2621 case MPT3SAS_INTEL_RMS3JC080_SSDID:
2622 pr_info(MPT3SAS_FMT "%s\n", ioc->name,
2623 MPT3SAS_INTEL_RMS3JC080_BRANDING);
2624 break;
Sreekanth Reddy1117b312014-09-12 15:35:30 +05302625
Sreekanth Reddy989e43c2015-11-11 17:30:32 +05302626 case MPT3SAS_INTEL_RS3GC008_SSDID:
2627 pr_info(MPT3SAS_FMT "%s\n", ioc->name,
2628 MPT3SAS_INTEL_RS3GC008_BRANDING);
2629 break;
2630 case MPT3SAS_INTEL_RS3FC044_SSDID:
2631 pr_info(MPT3SAS_FMT "%s\n", ioc->name,
2632 MPT3SAS_INTEL_RS3FC044_BRANDING);
2633 break;
2634 case MPT3SAS_INTEL_RS3UC080_SSDID:
2635 pr_info(MPT3SAS_FMT "%s\n", ioc->name,
2636 MPT3SAS_INTEL_RS3UC080_BRANDING);
2637 break;
2638 default:
2639 pr_info(MPT3SAS_FMT
2640 "Intel(R) Controller: Subsystem ID: 0x%X\n",
2641 ioc->name, ioc->pdev->subsystem_device);
2642 break;
2643 }
Sreekanth Reddy1117b312014-09-12 15:35:30 +05302644 break;
2645 default:
2646 pr_info(MPT3SAS_FMT
Sreekanth Reddy989e43c2015-11-11 17:30:32 +05302647 "Intel(R) Controller: Subsystem ID: 0x%X\n",
Sreekanth Reddyd8eb4a42015-06-30 12:25:02 +05302648 ioc->name, ioc->pdev->subsystem_device);
2649 break;
2650 }
2651 break;
Sreekanth Reddy989e43c2015-11-11 17:30:32 +05302652 case PCI_VENDOR_ID_DELL:
2653 switch (ioc->pdev->device) {
2654 case MPI2_MFGPAGE_DEVID_SAS2008:
2655 switch (ioc->pdev->subsystem_device) {
2656 case MPT2SAS_DELL_6GBPS_SAS_HBA_SSDID:
2657 pr_info(MPT3SAS_FMT "%s\n", ioc->name,
2658 MPT2SAS_DELL_6GBPS_SAS_HBA_BRANDING);
2659 break;
2660 case MPT2SAS_DELL_PERC_H200_ADAPTER_SSDID:
2661 pr_info(MPT3SAS_FMT "%s\n", ioc->name,
2662 MPT2SAS_DELL_PERC_H200_ADAPTER_BRANDING);
2663 break;
2664 case MPT2SAS_DELL_PERC_H200_INTEGRATED_SSDID:
2665 pr_info(MPT3SAS_FMT "%s\n", ioc->name,
2666 MPT2SAS_DELL_PERC_H200_INTEGRATED_BRANDING);
2667 break;
2668 case MPT2SAS_DELL_PERC_H200_MODULAR_SSDID:
2669 pr_info(MPT3SAS_FMT "%s\n", ioc->name,
2670 MPT2SAS_DELL_PERC_H200_MODULAR_BRANDING);
2671 break;
2672 case MPT2SAS_DELL_PERC_H200_EMBEDDED_SSDID:
2673 pr_info(MPT3SAS_FMT "%s\n", ioc->name,
2674 MPT2SAS_DELL_PERC_H200_EMBEDDED_BRANDING);
2675 break;
2676 case MPT2SAS_DELL_PERC_H200_SSDID:
2677 pr_info(MPT3SAS_FMT "%s\n", ioc->name,
2678 MPT2SAS_DELL_PERC_H200_BRANDING);
2679 break;
2680 case MPT2SAS_DELL_6GBPS_SAS_SSDID:
2681 pr_info(MPT3SAS_FMT "%s\n", ioc->name,
2682 MPT2SAS_DELL_6GBPS_SAS_BRANDING);
2683 break;
2684 default:
2685 pr_info(MPT3SAS_FMT
2686 "Dell 6Gbps HBA: Subsystem ID: 0x%X\n",
2687 ioc->name, ioc->pdev->subsystem_device);
2688 break;
2689 }
2690 break;
2691 case MPI25_MFGPAGE_DEVID_SAS3008:
2692 switch (ioc->pdev->subsystem_device) {
2693 case MPT3SAS_DELL_12G_HBA_SSDID:
2694 pr_info(MPT3SAS_FMT "%s\n", ioc->name,
2695 MPT3SAS_DELL_12G_HBA_BRANDING);
2696 break;
2697 default:
2698 pr_info(MPT3SAS_FMT
2699 "Dell 12Gbps HBA: Subsystem ID: 0x%X\n",
2700 ioc->name, ioc->pdev->subsystem_device);
2701 break;
2702 }
2703 break;
2704 default:
2705 pr_info(MPT3SAS_FMT
2706 "Dell HBA: Subsystem ID: 0x%X\n", ioc->name,
2707 ioc->pdev->subsystem_device);
2708 break;
2709 }
2710 break;
2711 case PCI_VENDOR_ID_CISCO:
2712 switch (ioc->pdev->device) {
2713 case MPI25_MFGPAGE_DEVID_SAS3008:
2714 switch (ioc->pdev->subsystem_device) {
2715 case MPT3SAS_CISCO_12G_8E_HBA_SSDID:
2716 pr_info(MPT3SAS_FMT "%s\n", ioc->name,
2717 MPT3SAS_CISCO_12G_8E_HBA_BRANDING);
2718 break;
2719 case MPT3SAS_CISCO_12G_8I_HBA_SSDID:
2720 pr_info(MPT3SAS_FMT "%s\n", ioc->name,
2721 MPT3SAS_CISCO_12G_8I_HBA_BRANDING);
2722 break;
2723 case MPT3SAS_CISCO_12G_AVILA_HBA_SSDID:
2724 pr_info(MPT3SAS_FMT "%s\n", ioc->name,
2725 MPT3SAS_CISCO_12G_AVILA_HBA_BRANDING);
2726 break;
2727 default:
2728 pr_info(MPT3SAS_FMT
2729 "Cisco 12Gbps SAS HBA: Subsystem ID: 0x%X\n",
2730 ioc->name, ioc->pdev->subsystem_device);
2731 break;
2732 }
2733 break;
2734 case MPI25_MFGPAGE_DEVID_SAS3108_1:
2735 switch (ioc->pdev->subsystem_device) {
2736 case MPT3SAS_CISCO_12G_AVILA_HBA_SSDID:
2737 pr_info(MPT3SAS_FMT "%s\n", ioc->name,
2738 MPT3SAS_CISCO_12G_AVILA_HBA_BRANDING);
2739 break;
2740 case MPT3SAS_CISCO_12G_COLUSA_MEZZANINE_HBA_SSDID:
2741 pr_info(MPT3SAS_FMT "%s\n", ioc->name,
2742 MPT3SAS_CISCO_12G_COLUSA_MEZZANINE_HBA_BRANDING
2743 );
2744 break;
2745 default:
2746 pr_info(MPT3SAS_FMT
2747 "Cisco 12Gbps SAS HBA: Subsystem ID: 0x%X\n",
2748 ioc->name, ioc->pdev->subsystem_device);
2749 break;
2750 }
2751 break;
2752 default:
2753 pr_info(MPT3SAS_FMT
2754 "Cisco SAS HBA: Subsystem ID: 0x%X\n",
2755 ioc->name, ioc->pdev->subsystem_device);
2756 break;
2757 }
2758 break;
2759 case MPT2SAS_HP_3PAR_SSVID:
2760 switch (ioc->pdev->device) {
2761 case MPI2_MFGPAGE_DEVID_SAS2004:
2762 switch (ioc->pdev->subsystem_device) {
2763 case MPT2SAS_HP_DAUGHTER_2_4_INTERNAL_SSDID:
2764 pr_info(MPT3SAS_FMT "%s\n", ioc->name,
2765 MPT2SAS_HP_DAUGHTER_2_4_INTERNAL_BRANDING);
2766 break;
2767 default:
2768 pr_info(MPT3SAS_FMT
2769 "HP 6Gbps SAS HBA: Subsystem ID: 0x%X\n",
2770 ioc->name, ioc->pdev->subsystem_device);
2771 break;
2772 }
2773 case MPI2_MFGPAGE_DEVID_SAS2308_2:
2774 switch (ioc->pdev->subsystem_device) {
2775 case MPT2SAS_HP_2_4_INTERNAL_SSDID:
2776 pr_info(MPT3SAS_FMT "%s\n", ioc->name,
2777 MPT2SAS_HP_2_4_INTERNAL_BRANDING);
2778 break;
2779 case MPT2SAS_HP_2_4_EXTERNAL_SSDID:
2780 pr_info(MPT3SAS_FMT "%s\n", ioc->name,
2781 MPT2SAS_HP_2_4_EXTERNAL_BRANDING);
2782 break;
2783 case MPT2SAS_HP_1_4_INTERNAL_1_4_EXTERNAL_SSDID:
2784 pr_info(MPT3SAS_FMT "%s\n", ioc->name,
2785 MPT2SAS_HP_1_4_INTERNAL_1_4_EXTERNAL_BRANDING);
2786 break;
2787 case MPT2SAS_HP_EMBEDDED_2_4_INTERNAL_SSDID:
2788 pr_info(MPT3SAS_FMT "%s\n", ioc->name,
2789 MPT2SAS_HP_EMBEDDED_2_4_INTERNAL_BRANDING);
2790 break;
2791 default:
2792 pr_info(MPT3SAS_FMT
2793 "HP 6Gbps SAS HBA: Subsystem ID: 0x%X\n",
2794 ioc->name, ioc->pdev->subsystem_device);
2795 break;
2796 }
2797 default:
2798 pr_info(MPT3SAS_FMT
2799 "HP SAS HBA: Subsystem ID: 0x%X\n",
2800 ioc->name, ioc->pdev->subsystem_device);
2801 break;
2802 }
Sreekanth Reddy38e41412015-06-30 12:24:57 +05302803 default:
Sreekanth Reddy38e41412015-06-30 12:24:57 +05302804 break;
2805 }
2806}
Sreekanth Reddyfb84dfc2015-06-30 12:24:56 +05302807
2808/**
Sreekanth Reddyf92363d2012-11-30 07:44:21 +05302809 * _base_display_ioc_capabilities - Disply IOC's capabilities.
2810 * @ioc: per adapter object
2811 *
2812 * Return nothing.
2813 */
2814static void
2815_base_display_ioc_capabilities(struct MPT3SAS_ADAPTER *ioc)
2816{
2817 int i = 0;
2818 char desc[16];
2819 u32 iounit_pg1_flags;
2820 u32 bios_version;
2821
2822 bios_version = le32_to_cpu(ioc->bios_pg3.BiosVersion);
2823 strncpy(desc, ioc->manu_pg0.ChipName, 16);
2824 pr_info(MPT3SAS_FMT "%s: FWVersion(%02d.%02d.%02d.%02d), "\
2825 "ChipRevision(0x%02x), BiosVersion(%02d.%02d.%02d.%02d)\n",
2826 ioc->name, desc,
2827 (ioc->facts.FWVersion.Word & 0xFF000000) >> 24,
2828 (ioc->facts.FWVersion.Word & 0x00FF0000) >> 16,
2829 (ioc->facts.FWVersion.Word & 0x0000FF00) >> 8,
2830 ioc->facts.FWVersion.Word & 0x000000FF,
2831 ioc->pdev->revision,
2832 (bios_version & 0xFF000000) >> 24,
2833 (bios_version & 0x00FF0000) >> 16,
2834 (bios_version & 0x0000FF00) >> 8,
2835 bios_version & 0x000000FF);
2836
Sreekanth Reddy989e43c2015-11-11 17:30:32 +05302837 _base_display_OEMs_branding(ioc);
Sreekanth Reddy1117b312014-09-12 15:35:30 +05302838
Sreekanth Reddyf92363d2012-11-30 07:44:21 +05302839 pr_info(MPT3SAS_FMT "Protocol=(", ioc->name);
2840
2841 if (ioc->facts.ProtocolFlags & MPI2_IOCFACTS_PROTOCOL_SCSI_INITIATOR) {
2842 pr_info("Initiator");
2843 i++;
2844 }
2845
2846 if (ioc->facts.ProtocolFlags & MPI2_IOCFACTS_PROTOCOL_SCSI_TARGET) {
2847 pr_info("%sTarget", i ? "," : "");
2848 i++;
2849 }
2850
2851 i = 0;
2852 pr_info("), ");
2853 pr_info("Capabilities=(");
2854
Sreekanth Reddy7786ab62015-11-11 17:30:28 +05302855 if (!ioc->hide_ir_msg) {
2856 if (ioc->facts.IOCCapabilities &
Sreekanth Reddyf92363d2012-11-30 07:44:21 +05302857 MPI2_IOCFACTS_CAPABILITY_INTEGRATED_RAID) {
2858 pr_info("Raid");
2859 i++;
Sreekanth Reddy7786ab62015-11-11 17:30:28 +05302860 }
Sreekanth Reddyf92363d2012-11-30 07:44:21 +05302861 }
2862
2863 if (ioc->facts.IOCCapabilities & MPI2_IOCFACTS_CAPABILITY_TLR) {
2864 pr_info("%sTLR", i ? "," : "");
2865 i++;
2866 }
2867
2868 if (ioc->facts.IOCCapabilities & MPI2_IOCFACTS_CAPABILITY_MULTICAST) {
2869 pr_info("%sMulticast", i ? "," : "");
2870 i++;
2871 }
2872
2873 if (ioc->facts.IOCCapabilities &
2874 MPI2_IOCFACTS_CAPABILITY_BIDIRECTIONAL_TARGET) {
2875 pr_info("%sBIDI Target", i ? "," : "");
2876 i++;
2877 }
2878
2879 if (ioc->facts.IOCCapabilities & MPI2_IOCFACTS_CAPABILITY_EEDP) {
2880 pr_info("%sEEDP", i ? "," : "");
2881 i++;
2882 }
2883
2884 if (ioc->facts.IOCCapabilities &
2885 MPI2_IOCFACTS_CAPABILITY_SNAPSHOT_BUFFER) {
2886 pr_info("%sSnapshot Buffer", i ? "," : "");
2887 i++;
2888 }
2889
2890 if (ioc->facts.IOCCapabilities &
2891 MPI2_IOCFACTS_CAPABILITY_DIAG_TRACE_BUFFER) {
2892 pr_info("%sDiag Trace Buffer", i ? "," : "");
2893 i++;
2894 }
2895
2896 if (ioc->facts.IOCCapabilities &
2897 MPI2_IOCFACTS_CAPABILITY_EXTENDED_BUFFER) {
2898 pr_info("%sDiag Extended Buffer", i ? "," : "");
2899 i++;
2900 }
2901
2902 if (ioc->facts.IOCCapabilities &
2903 MPI2_IOCFACTS_CAPABILITY_TASK_SET_FULL_HANDLING) {
2904 pr_info("%sTask Set Full", i ? "," : "");
2905 i++;
2906 }
2907
2908 iounit_pg1_flags = le32_to_cpu(ioc->iounit_pg1.Flags);
2909 if (!(iounit_pg1_flags & MPI2_IOUNITPAGE1_NATIVE_COMMAND_Q_DISABLE)) {
2910 pr_info("%sNCQ", i ? "," : "");
2911 i++;
2912 }
2913
2914 pr_info(")\n");
2915}
2916
2917/**
2918 * mpt3sas_base_update_missing_delay - change the missing delay timers
2919 * @ioc: per adapter object
2920 * @device_missing_delay: amount of time till device is reported missing
2921 * @io_missing_delay: interval IO is returned when there is a missing device
2922 *
2923 * Return nothing.
2924 *
2925 * Passed on the command line, this function will modify the device missing
2926 * delay, as well as the io missing delay. This should be called at driver
2927 * load time.
2928 */
2929void
2930mpt3sas_base_update_missing_delay(struct MPT3SAS_ADAPTER *ioc,
2931 u16 device_missing_delay, u8 io_missing_delay)
2932{
2933 u16 dmd, dmd_new, dmd_orignal;
2934 u8 io_missing_delay_original;
2935 u16 sz;
2936 Mpi2SasIOUnitPage1_t *sas_iounit_pg1 = NULL;
2937 Mpi2ConfigReply_t mpi_reply;
2938 u8 num_phys = 0;
2939 u16 ioc_status;
2940
2941 mpt3sas_config_get_number_hba_phys(ioc, &num_phys);
2942 if (!num_phys)
2943 return;
2944
2945 sz = offsetof(Mpi2SasIOUnitPage1_t, PhyData) + (num_phys *
2946 sizeof(Mpi2SasIOUnit1PhyData_t));
2947 sas_iounit_pg1 = kzalloc(sz, GFP_KERNEL);
2948 if (!sas_iounit_pg1) {
2949 pr_err(MPT3SAS_FMT "failure at %s:%d/%s()!\n",
2950 ioc->name, __FILE__, __LINE__, __func__);
2951 goto out;
2952 }
2953 if ((mpt3sas_config_get_sas_iounit_pg1(ioc, &mpi_reply,
2954 sas_iounit_pg1, sz))) {
2955 pr_err(MPT3SAS_FMT "failure at %s:%d/%s()!\n",
2956 ioc->name, __FILE__, __LINE__, __func__);
2957 goto out;
2958 }
2959 ioc_status = le16_to_cpu(mpi_reply.IOCStatus) &
2960 MPI2_IOCSTATUS_MASK;
2961 if (ioc_status != MPI2_IOCSTATUS_SUCCESS) {
2962 pr_err(MPT3SAS_FMT "failure at %s:%d/%s()!\n",
2963 ioc->name, __FILE__, __LINE__, __func__);
2964 goto out;
2965 }
2966
2967 /* device missing delay */
2968 dmd = sas_iounit_pg1->ReportDeviceMissingDelay;
2969 if (dmd & MPI2_SASIOUNIT1_REPORT_MISSING_UNIT_16)
2970 dmd = (dmd & MPI2_SASIOUNIT1_REPORT_MISSING_TIMEOUT_MASK) * 16;
2971 else
2972 dmd = dmd & MPI2_SASIOUNIT1_REPORT_MISSING_TIMEOUT_MASK;
2973 dmd_orignal = dmd;
2974 if (device_missing_delay > 0x7F) {
2975 dmd = (device_missing_delay > 0x7F0) ? 0x7F0 :
2976 device_missing_delay;
2977 dmd = dmd / 16;
2978 dmd |= MPI2_SASIOUNIT1_REPORT_MISSING_UNIT_16;
2979 } else
2980 dmd = device_missing_delay;
2981 sas_iounit_pg1->ReportDeviceMissingDelay = dmd;
2982
2983 /* io missing delay */
2984 io_missing_delay_original = sas_iounit_pg1->IODeviceMissingDelay;
2985 sas_iounit_pg1->IODeviceMissingDelay = io_missing_delay;
2986
2987 if (!mpt3sas_config_set_sas_iounit_pg1(ioc, &mpi_reply, sas_iounit_pg1,
2988 sz)) {
2989 if (dmd & MPI2_SASIOUNIT1_REPORT_MISSING_UNIT_16)
2990 dmd_new = (dmd &
2991 MPI2_SASIOUNIT1_REPORT_MISSING_TIMEOUT_MASK) * 16;
2992 else
2993 dmd_new =
2994 dmd & MPI2_SASIOUNIT1_REPORT_MISSING_TIMEOUT_MASK;
2995 pr_info(MPT3SAS_FMT "device_missing_delay: old(%d), new(%d)\n",
2996 ioc->name, dmd_orignal, dmd_new);
2997 pr_info(MPT3SAS_FMT "ioc_missing_delay: old(%d), new(%d)\n",
2998 ioc->name, io_missing_delay_original,
2999 io_missing_delay);
3000 ioc->device_missing_delay = dmd_new;
3001 ioc->io_missing_delay = io_missing_delay;
3002 }
3003
3004out:
3005 kfree(sas_iounit_pg1);
3006}
3007/**
3008 * _base_static_config_pages - static start of day config pages
3009 * @ioc: per adapter object
3010 *
3011 * Return nothing.
3012 */
3013static void
3014_base_static_config_pages(struct MPT3SAS_ADAPTER *ioc)
3015{
3016 Mpi2ConfigReply_t mpi_reply;
3017 u32 iounit_pg1_flags;
3018
3019 mpt3sas_config_get_manufacturing_pg0(ioc, &mpi_reply, &ioc->manu_pg0);
3020 if (ioc->ir_firmware)
3021 mpt3sas_config_get_manufacturing_pg10(ioc, &mpi_reply,
3022 &ioc->manu_pg10);
3023
3024 /*
3025 * Ensure correct T10 PI operation if vendor left EEDPTagMode
3026 * flag unset in NVDATA.
3027 */
3028 mpt3sas_config_get_manufacturing_pg11(ioc, &mpi_reply, &ioc->manu_pg11);
3029 if (ioc->manu_pg11.EEDPTagMode == 0) {
3030 pr_err("%s: overriding NVDATA EEDPTagMode setting\n",
3031 ioc->name);
3032 ioc->manu_pg11.EEDPTagMode &= ~0x3;
3033 ioc->manu_pg11.EEDPTagMode |= 0x1;
3034 mpt3sas_config_set_manufacturing_pg11(ioc, &mpi_reply,
3035 &ioc->manu_pg11);
3036 }
3037
3038 mpt3sas_config_get_bios_pg2(ioc, &mpi_reply, &ioc->bios_pg2);
3039 mpt3sas_config_get_bios_pg3(ioc, &mpi_reply, &ioc->bios_pg3);
3040 mpt3sas_config_get_ioc_pg8(ioc, &mpi_reply, &ioc->ioc_pg8);
3041 mpt3sas_config_get_iounit_pg0(ioc, &mpi_reply, &ioc->iounit_pg0);
3042 mpt3sas_config_get_iounit_pg1(ioc, &mpi_reply, &ioc->iounit_pg1);
Sreekanth Reddy2d8ce8c2015-01-12 11:38:56 +05303043 mpt3sas_config_get_iounit_pg8(ioc, &mpi_reply, &ioc->iounit_pg8);
Sreekanth Reddyf92363d2012-11-30 07:44:21 +05303044 _base_display_ioc_capabilities(ioc);
3045
3046 /*
3047 * Enable task_set_full handling in iounit_pg1 when the
3048 * facts capabilities indicate that its supported.
3049 */
3050 iounit_pg1_flags = le32_to_cpu(ioc->iounit_pg1.Flags);
3051 if ((ioc->facts.IOCCapabilities &
3052 MPI2_IOCFACTS_CAPABILITY_TASK_SET_FULL_HANDLING))
3053 iounit_pg1_flags &=
3054 ~MPI2_IOUNITPAGE1_DISABLE_TASK_SET_FULL_HANDLING;
3055 else
3056 iounit_pg1_flags |=
3057 MPI2_IOUNITPAGE1_DISABLE_TASK_SET_FULL_HANDLING;
3058 ioc->iounit_pg1.Flags = cpu_to_le32(iounit_pg1_flags);
3059 mpt3sas_config_set_iounit_pg1(ioc, &mpi_reply, &ioc->iounit_pg1);
Sreekanth Reddy2d8ce8c2015-01-12 11:38:56 +05303060
3061 if (ioc->iounit_pg8.NumSensors)
3062 ioc->temp_sensors_count = ioc->iounit_pg8.NumSensors;
Sreekanth Reddyf92363d2012-11-30 07:44:21 +05303063}
3064
3065/**
3066 * _base_release_memory_pools - release memory
3067 * @ioc: per adapter object
3068 *
3069 * Free memory allocated from _base_allocate_memory_pools.
3070 *
3071 * Return nothing.
3072 */
3073static void
3074_base_release_memory_pools(struct MPT3SAS_ADAPTER *ioc)
3075{
Sreekanth Reddy9b05c912014-09-12 15:35:31 +05303076 int i = 0;
3077 struct reply_post_struct *rps;
Sreekanth Reddyf92363d2012-11-30 07:44:21 +05303078
3079 dexitprintk(ioc, pr_info(MPT3SAS_FMT "%s\n", ioc->name,
3080 __func__));
3081
3082 if (ioc->request) {
3083 pci_free_consistent(ioc->pdev, ioc->request_dma_sz,
3084 ioc->request, ioc->request_dma);
3085 dexitprintk(ioc, pr_info(MPT3SAS_FMT
3086 "request_pool(0x%p): free\n",
3087 ioc->name, ioc->request));
3088 ioc->request = NULL;
3089 }
3090
3091 if (ioc->sense) {
3092 pci_pool_free(ioc->sense_dma_pool, ioc->sense, ioc->sense_dma);
3093 if (ioc->sense_dma_pool)
3094 pci_pool_destroy(ioc->sense_dma_pool);
3095 dexitprintk(ioc, pr_info(MPT3SAS_FMT
3096 "sense_pool(0x%p): free\n",
3097 ioc->name, ioc->sense));
3098 ioc->sense = NULL;
3099 }
3100
3101 if (ioc->reply) {
3102 pci_pool_free(ioc->reply_dma_pool, ioc->reply, ioc->reply_dma);
3103 if (ioc->reply_dma_pool)
3104 pci_pool_destroy(ioc->reply_dma_pool);
3105 dexitprintk(ioc, pr_info(MPT3SAS_FMT
3106 "reply_pool(0x%p): free\n",
3107 ioc->name, ioc->reply));
3108 ioc->reply = NULL;
3109 }
3110
3111 if (ioc->reply_free) {
3112 pci_pool_free(ioc->reply_free_dma_pool, ioc->reply_free,
3113 ioc->reply_free_dma);
3114 if (ioc->reply_free_dma_pool)
3115 pci_pool_destroy(ioc->reply_free_dma_pool);
3116 dexitprintk(ioc, pr_info(MPT3SAS_FMT
3117 "reply_free_pool(0x%p): free\n",
3118 ioc->name, ioc->reply_free));
3119 ioc->reply_free = NULL;
3120 }
3121
Sreekanth Reddy9b05c912014-09-12 15:35:31 +05303122 if (ioc->reply_post) {
3123 do {
3124 rps = &ioc->reply_post[i];
3125 if (rps->reply_post_free) {
3126 pci_pool_free(
3127 ioc->reply_post_free_dma_pool,
3128 rps->reply_post_free,
3129 rps->reply_post_free_dma);
3130 dexitprintk(ioc, pr_info(MPT3SAS_FMT
3131 "reply_post_free_pool(0x%p): free\n",
3132 ioc->name, rps->reply_post_free));
3133 rps->reply_post_free = NULL;
3134 }
3135 } while (ioc->rdpq_array_enable &&
3136 (++i < ioc->reply_queue_count));
3137
Sreekanth Reddyf92363d2012-11-30 07:44:21 +05303138 if (ioc->reply_post_free_dma_pool)
3139 pci_pool_destroy(ioc->reply_post_free_dma_pool);
Sreekanth Reddy9b05c912014-09-12 15:35:31 +05303140 kfree(ioc->reply_post);
Sreekanth Reddyf92363d2012-11-30 07:44:21 +05303141 }
3142
3143 if (ioc->config_page) {
3144 dexitprintk(ioc, pr_info(MPT3SAS_FMT
3145 "config_page(0x%p): free\n", ioc->name,
3146 ioc->config_page));
3147 pci_free_consistent(ioc->pdev, ioc->config_page_sz,
3148 ioc->config_page, ioc->config_page_dma);
3149 }
3150
3151 if (ioc->scsi_lookup) {
3152 free_pages((ulong)ioc->scsi_lookup, ioc->scsi_lookup_pages);
3153 ioc->scsi_lookup = NULL;
3154 }
3155 kfree(ioc->hpr_lookup);
3156 kfree(ioc->internal_lookup);
3157 if (ioc->chain_lookup) {
3158 for (i = 0; i < ioc->chain_depth; i++) {
3159 if (ioc->chain_lookup[i].chain_buffer)
3160 pci_pool_free(ioc->chain_dma_pool,
3161 ioc->chain_lookup[i].chain_buffer,
3162 ioc->chain_lookup[i].chain_buffer_dma);
3163 }
3164 if (ioc->chain_dma_pool)
3165 pci_pool_destroy(ioc->chain_dma_pool);
3166 free_pages((ulong)ioc->chain_lookup, ioc->chain_pages);
3167 ioc->chain_lookup = NULL;
3168 }
3169}
3170
3171/**
3172 * _base_allocate_memory_pools - allocate start of day memory pools
3173 * @ioc: per adapter object
3174 * @sleep_flag: CAN_SLEEP or NO_SLEEP
3175 *
3176 * Returns 0 success, anything else error
3177 */
3178static int
3179_base_allocate_memory_pools(struct MPT3SAS_ADAPTER *ioc, int sleep_flag)
3180{
3181 struct mpt3sas_facts *facts;
3182 u16 max_sge_elements;
3183 u16 chains_needed_per_io;
3184 u32 sz, total_sz, reply_post_free_sz;
3185 u32 retry_sz;
3186 u16 max_request_credit;
3187 unsigned short sg_tablesize;
3188 u16 sge_size;
3189 int i;
3190
3191 dinitprintk(ioc, pr_info(MPT3SAS_FMT "%s\n", ioc->name,
3192 __func__));
3193
3194
3195 retry_sz = 0;
3196 facts = &ioc->facts;
3197
3198 /* command line tunables for max sgl entries */
3199 if (max_sgl_entries != -1)
3200 sg_tablesize = max_sgl_entries;
Sreekanth Reddy471ef9d2015-11-11 17:30:24 +05303201 else {
3202 if (ioc->hba_mpi_version_belonged == MPI2_VERSION)
3203 sg_tablesize = MPT2SAS_SG_DEPTH;
3204 else
3205 sg_tablesize = MPT3SAS_SG_DEPTH;
3206 }
Sreekanth Reddyf92363d2012-11-30 07:44:21 +05303207
Sreekanth Reddy8a7e4c22015-11-11 17:30:18 +05303208 if (sg_tablesize < MPT_MIN_PHYS_SEGMENTS)
3209 sg_tablesize = MPT_MIN_PHYS_SEGMENTS;
3210 else if (sg_tablesize > MPT_MAX_PHYS_SEGMENTS) {
Sreekanth Reddyad666a02015-01-12 11:39:00 +05303211 sg_tablesize = min_t(unsigned short, sg_tablesize,
3212 SCSI_MAX_SG_CHAIN_SEGMENTS);
3213 pr_warn(MPT3SAS_FMT
3214 "sg_tablesize(%u) is bigger than kernel"
3215 " defined SCSI_MAX_SG_SEGMENTS(%u)\n", ioc->name,
Sreekanth Reddy8a7e4c22015-11-11 17:30:18 +05303216 sg_tablesize, MPT_MAX_PHYS_SEGMENTS);
Sreekanth Reddyad666a02015-01-12 11:39:00 +05303217 }
Sreekanth Reddyf92363d2012-11-30 07:44:21 +05303218 ioc->shost->sg_tablesize = sg_tablesize;
3219
Suganath prabu Subramanifd0331b2016-01-28 12:07:02 +05303220 ioc->internal_depth = min_t(int, (facts->HighPriorityCredit + (5)),
3221 (facts->RequestCredit / 4));
3222 if (ioc->internal_depth < INTERNAL_CMDS_COUNT) {
3223 if (facts->RequestCredit <= (INTERNAL_CMDS_COUNT +
3224 INTERNAL_SCSIIO_CMDS_COUNT)) {
3225 pr_err(MPT3SAS_FMT "IOC doesn't have enough Request \
3226 Credits, it has just %d number of credits\n",
3227 ioc->name, facts->RequestCredit);
3228 return -ENOMEM;
3229 }
3230 ioc->internal_depth = 10;
3231 }
3232
3233 ioc->hi_priority_depth = ioc->internal_depth - (5);
Sreekanth Reddyf92363d2012-11-30 07:44:21 +05303234 /* command line tunables for max controller queue depth */
3235 if (max_queue_depth != -1 && max_queue_depth != 0) {
3236 max_request_credit = min_t(u16, max_queue_depth +
Suganath prabu Subramanifd0331b2016-01-28 12:07:02 +05303237 ioc->internal_depth, facts->RequestCredit);
Sreekanth Reddyf92363d2012-11-30 07:44:21 +05303238 if (max_request_credit > MAX_HBA_QUEUE_DEPTH)
3239 max_request_credit = MAX_HBA_QUEUE_DEPTH;
3240 } else
3241 max_request_credit = min_t(u16, facts->RequestCredit,
3242 MAX_HBA_QUEUE_DEPTH);
3243
Suganath prabu Subramanifd0331b2016-01-28 12:07:02 +05303244 /* Firmware maintains additional facts->HighPriorityCredit number of
3245 * credits for HiPriprity Request messages, so hba queue depth will be
3246 * sum of max_request_credit and high priority queue depth.
3247 */
3248 ioc->hba_queue_depth = max_request_credit + ioc->hi_priority_depth;
Sreekanth Reddyf92363d2012-11-30 07:44:21 +05303249
3250 /* request frame size */
3251 ioc->request_sz = facts->IOCRequestFrameSize * 4;
3252
3253 /* reply frame size */
3254 ioc->reply_sz = facts->ReplyFrameSize * 4;
3255
Suganath prabu Subramaniebb30242016-01-28 12:07:04 +05303256 /* chain segment size */
3257 if (ioc->hba_mpi_version_belonged != MPI2_VERSION) {
3258 if (facts->IOCMaxChainSegmentSize)
3259 ioc->chain_segment_sz =
3260 facts->IOCMaxChainSegmentSize *
3261 MAX_CHAIN_ELEMT_SZ;
3262 else
3263 /* set to 128 bytes size if IOCMaxChainSegmentSize is zero */
3264 ioc->chain_segment_sz = DEFAULT_NUM_FWCHAIN_ELEMTS *
3265 MAX_CHAIN_ELEMT_SZ;
3266 } else
3267 ioc->chain_segment_sz = ioc->request_sz;
3268
Sreekanth Reddyf92363d2012-11-30 07:44:21 +05303269 /* calculate the max scatter element size */
3270 sge_size = max_t(u16, ioc->sge_size, ioc->sge_size_ieee);
3271
3272 retry_allocation:
3273 total_sz = 0;
3274 /* calculate number of sg elements left over in the 1st frame */
3275 max_sge_elements = ioc->request_sz - ((sizeof(Mpi2SCSIIORequest_t) -
3276 sizeof(Mpi2SGEIOUnion_t)) + sge_size);
3277 ioc->max_sges_in_main_message = max_sge_elements/sge_size;
3278
3279 /* now do the same for a chain buffer */
Suganath prabu Subramaniebb30242016-01-28 12:07:04 +05303280 max_sge_elements = ioc->chain_segment_sz - sge_size;
Sreekanth Reddyf92363d2012-11-30 07:44:21 +05303281 ioc->max_sges_in_chain_message = max_sge_elements/sge_size;
3282
3283 /*
3284 * MPT3SAS_SG_DEPTH = CONFIG_FUSION_MAX_SGE
3285 */
3286 chains_needed_per_io = ((ioc->shost->sg_tablesize -
3287 ioc->max_sges_in_main_message)/ioc->max_sges_in_chain_message)
3288 + 1;
3289 if (chains_needed_per_io > facts->MaxChainDepth) {
3290 chains_needed_per_io = facts->MaxChainDepth;
3291 ioc->shost->sg_tablesize = min_t(u16,
3292 ioc->max_sges_in_main_message + (ioc->max_sges_in_chain_message
3293 * chains_needed_per_io), ioc->shost->sg_tablesize);
3294 }
3295 ioc->chains_needed_per_io = chains_needed_per_io;
3296
3297 /* reply free queue sizing - taking into account for 64 FW events */
3298 ioc->reply_free_queue_depth = ioc->hba_queue_depth + 64;
3299
3300 /* calculate reply descriptor post queue depth */
3301 ioc->reply_post_queue_depth = ioc->hba_queue_depth +
3302 ioc->reply_free_queue_depth + 1 ;
3303 /* align the reply post queue on the next 16 count boundary */
3304 if (ioc->reply_post_queue_depth % 16)
3305 ioc->reply_post_queue_depth += 16 -
3306 (ioc->reply_post_queue_depth % 16);
3307
Sreekanth Reddyf92363d2012-11-30 07:44:21 +05303308 if (ioc->reply_post_queue_depth >
3309 facts->MaxReplyDescriptorPostQueueDepth) {
3310 ioc->reply_post_queue_depth =
3311 facts->MaxReplyDescriptorPostQueueDepth -
3312 (facts->MaxReplyDescriptorPostQueueDepth % 16);
3313 ioc->hba_queue_depth =
3314 ((ioc->reply_post_queue_depth - 64) / 2) - 1;
3315 ioc->reply_free_queue_depth = ioc->hba_queue_depth + 64;
3316 }
3317
3318 dinitprintk(ioc, pr_info(MPT3SAS_FMT "scatter gather: " \
3319 "sge_in_main_msg(%d), sge_per_chain(%d), sge_per_io(%d), "
3320 "chains_per_io(%d)\n", ioc->name, ioc->max_sges_in_main_message,
3321 ioc->max_sges_in_chain_message, ioc->shost->sg_tablesize,
3322 ioc->chains_needed_per_io));
3323
Sreekanth Reddy9b05c912014-09-12 15:35:31 +05303324 /* reply post queue, 16 byte align */
3325 reply_post_free_sz = ioc->reply_post_queue_depth *
3326 sizeof(Mpi2DefaultReplyDescriptor_t);
3327
3328 sz = reply_post_free_sz;
3329 if (_base_is_controller_msix_enabled(ioc) && !ioc->rdpq_array_enable)
3330 sz *= ioc->reply_queue_count;
3331
3332 ioc->reply_post = kcalloc((ioc->rdpq_array_enable) ?
3333 (ioc->reply_queue_count):1,
3334 sizeof(struct reply_post_struct), GFP_KERNEL);
3335
3336 if (!ioc->reply_post) {
3337 pr_err(MPT3SAS_FMT "reply_post_free pool: kcalloc failed\n",
3338 ioc->name);
3339 goto out;
3340 }
3341 ioc->reply_post_free_dma_pool = pci_pool_create("reply_post_free pool",
3342 ioc->pdev, sz, 16, 0);
3343 if (!ioc->reply_post_free_dma_pool) {
3344 pr_err(MPT3SAS_FMT
3345 "reply_post_free pool: pci_pool_create failed\n",
3346 ioc->name);
3347 goto out;
3348 }
3349 i = 0;
3350 do {
3351 ioc->reply_post[i].reply_post_free =
3352 pci_pool_alloc(ioc->reply_post_free_dma_pool,
3353 GFP_KERNEL,
3354 &ioc->reply_post[i].reply_post_free_dma);
3355 if (!ioc->reply_post[i].reply_post_free) {
3356 pr_err(MPT3SAS_FMT
3357 "reply_post_free pool: pci_pool_alloc failed\n",
3358 ioc->name);
3359 goto out;
3360 }
3361 memset(ioc->reply_post[i].reply_post_free, 0, sz);
3362 dinitprintk(ioc, pr_info(MPT3SAS_FMT
3363 "reply post free pool (0x%p): depth(%d),"
3364 "element_size(%d), pool_size(%d kB)\n", ioc->name,
3365 ioc->reply_post[i].reply_post_free,
3366 ioc->reply_post_queue_depth, 8, sz/1024));
3367 dinitprintk(ioc, pr_info(MPT3SAS_FMT
3368 "reply_post_free_dma = (0x%llx)\n", ioc->name,
3369 (unsigned long long)
3370 ioc->reply_post[i].reply_post_free_dma));
3371 total_sz += sz;
3372 } while (ioc->rdpq_array_enable && (++i < ioc->reply_queue_count));
3373
3374 if (ioc->dma_mask == 64) {
3375 if (_base_change_consistent_dma_mask(ioc, ioc->pdev) != 0) {
3376 pr_warn(MPT3SAS_FMT
3377 "no suitable consistent DMA mask for %s\n",
3378 ioc->name, pci_name(ioc->pdev));
3379 goto out;
3380 }
3381 }
3382
Sreekanth Reddyf92363d2012-11-30 07:44:21 +05303383 ioc->scsiio_depth = ioc->hba_queue_depth -
3384 ioc->hi_priority_depth - ioc->internal_depth;
3385
3386 /* set the scsi host can_queue depth
3387 * with some internal commands that could be outstanding
3388 */
Suganath prabu Subramanifd0331b2016-01-28 12:07:02 +05303389 ioc->shost->can_queue = ioc->scsiio_depth - INTERNAL_SCSIIO_CMDS_COUNT;
Sreekanth Reddyf92363d2012-11-30 07:44:21 +05303390 dinitprintk(ioc, pr_info(MPT3SAS_FMT
3391 "scsi host: can_queue depth (%d)\n",
3392 ioc->name, ioc->shost->can_queue));
3393
3394
3395 /* contiguous pool for request and chains, 16 byte align, one extra "
3396 * "frame for smid=0
3397 */
3398 ioc->chain_depth = ioc->chains_needed_per_io * ioc->scsiio_depth;
3399 sz = ((ioc->scsiio_depth + 1) * ioc->request_sz);
3400
3401 /* hi-priority queue */
3402 sz += (ioc->hi_priority_depth * ioc->request_sz);
3403
3404 /* internal queue */
3405 sz += (ioc->internal_depth * ioc->request_sz);
3406
3407 ioc->request_dma_sz = sz;
3408 ioc->request = pci_alloc_consistent(ioc->pdev, sz, &ioc->request_dma);
3409 if (!ioc->request) {
3410 pr_err(MPT3SAS_FMT "request pool: pci_alloc_consistent " \
3411 "failed: hba_depth(%d), chains_per_io(%d), frame_sz(%d), "
3412 "total(%d kB)\n", ioc->name, ioc->hba_queue_depth,
3413 ioc->chains_needed_per_io, ioc->request_sz, sz/1024);
3414 if (ioc->scsiio_depth < MPT3SAS_SAS_QUEUE_DEPTH)
3415 goto out;
Suganath prabu Subramanifd0331b2016-01-28 12:07:02 +05303416 retry_sz = 64;
3417 ioc->hba_queue_depth -= retry_sz;
Sreekanth Reddyf92363d2012-11-30 07:44:21 +05303418 goto retry_allocation;
3419 }
3420
3421 if (retry_sz)
3422 pr_err(MPT3SAS_FMT "request pool: pci_alloc_consistent " \
3423 "succeed: hba_depth(%d), chains_per_io(%d), frame_sz(%d), "
3424 "total(%d kb)\n", ioc->name, ioc->hba_queue_depth,
3425 ioc->chains_needed_per_io, ioc->request_sz, sz/1024);
3426
3427 /* hi-priority queue */
3428 ioc->hi_priority = ioc->request + ((ioc->scsiio_depth + 1) *
3429 ioc->request_sz);
3430 ioc->hi_priority_dma = ioc->request_dma + ((ioc->scsiio_depth + 1) *
3431 ioc->request_sz);
3432
3433 /* internal queue */
3434 ioc->internal = ioc->hi_priority + (ioc->hi_priority_depth *
3435 ioc->request_sz);
3436 ioc->internal_dma = ioc->hi_priority_dma + (ioc->hi_priority_depth *
3437 ioc->request_sz);
3438
3439 dinitprintk(ioc, pr_info(MPT3SAS_FMT
3440 "request pool(0x%p): depth(%d), frame_size(%d), pool_size(%d kB)\n",
3441 ioc->name, ioc->request, ioc->hba_queue_depth, ioc->request_sz,
3442 (ioc->hba_queue_depth * ioc->request_sz)/1024));
3443
3444 dinitprintk(ioc, pr_info(MPT3SAS_FMT "request pool: dma(0x%llx)\n",
3445 ioc->name, (unsigned long long) ioc->request_dma));
3446 total_sz += sz;
3447
3448 sz = ioc->scsiio_depth * sizeof(struct scsiio_tracker);
3449 ioc->scsi_lookup_pages = get_order(sz);
3450 ioc->scsi_lookup = (struct scsiio_tracker *)__get_free_pages(
3451 GFP_KERNEL, ioc->scsi_lookup_pages);
3452 if (!ioc->scsi_lookup) {
3453 pr_err(MPT3SAS_FMT "scsi_lookup: get_free_pages failed, sz(%d)\n",
3454 ioc->name, (int)sz);
3455 goto out;
3456 }
3457
3458 dinitprintk(ioc, pr_info(MPT3SAS_FMT "scsiio(0x%p): depth(%d)\n",
3459 ioc->name, ioc->request, ioc->scsiio_depth));
3460
3461 ioc->chain_depth = min_t(u32, ioc->chain_depth, MAX_CHAIN_DEPTH);
3462 sz = ioc->chain_depth * sizeof(struct chain_tracker);
3463 ioc->chain_pages = get_order(sz);
3464 ioc->chain_lookup = (struct chain_tracker *)__get_free_pages(
3465 GFP_KERNEL, ioc->chain_pages);
3466 if (!ioc->chain_lookup) {
3467 pr_err(MPT3SAS_FMT "chain_lookup: __get_free_pages failed\n",
3468 ioc->name);
3469 goto out;
3470 }
3471 ioc->chain_dma_pool = pci_pool_create("chain pool", ioc->pdev,
Suganath prabu Subramaniebb30242016-01-28 12:07:04 +05303472 ioc->chain_segment_sz, 16, 0);
Sreekanth Reddyf92363d2012-11-30 07:44:21 +05303473 if (!ioc->chain_dma_pool) {
3474 pr_err(MPT3SAS_FMT "chain_dma_pool: pci_pool_create failed\n",
3475 ioc->name);
3476 goto out;
3477 }
3478 for (i = 0; i < ioc->chain_depth; i++) {
3479 ioc->chain_lookup[i].chain_buffer = pci_pool_alloc(
3480 ioc->chain_dma_pool , GFP_KERNEL,
3481 &ioc->chain_lookup[i].chain_buffer_dma);
3482 if (!ioc->chain_lookup[i].chain_buffer) {
3483 ioc->chain_depth = i;
3484 goto chain_done;
3485 }
Suganath prabu Subramaniebb30242016-01-28 12:07:04 +05303486 total_sz += ioc->chain_segment_sz;
Sreekanth Reddyf92363d2012-11-30 07:44:21 +05303487 }
3488 chain_done:
3489 dinitprintk(ioc, pr_info(MPT3SAS_FMT
3490 "chain pool depth(%d), frame_size(%d), pool_size(%d kB)\n",
Suganath prabu Subramaniebb30242016-01-28 12:07:04 +05303491 ioc->name, ioc->chain_depth, ioc->chain_segment_sz,
3492 ((ioc->chain_depth * ioc->chain_segment_sz))/1024));
Sreekanth Reddyf92363d2012-11-30 07:44:21 +05303493
3494 /* initialize hi-priority queue smid's */
3495 ioc->hpr_lookup = kcalloc(ioc->hi_priority_depth,
3496 sizeof(struct request_tracker), GFP_KERNEL);
3497 if (!ioc->hpr_lookup) {
3498 pr_err(MPT3SAS_FMT "hpr_lookup: kcalloc failed\n",
3499 ioc->name);
3500 goto out;
3501 }
3502 ioc->hi_priority_smid = ioc->scsiio_depth + 1;
3503 dinitprintk(ioc, pr_info(MPT3SAS_FMT
3504 "hi_priority(0x%p): depth(%d), start smid(%d)\n",
3505 ioc->name, ioc->hi_priority,
3506 ioc->hi_priority_depth, ioc->hi_priority_smid));
3507
3508 /* initialize internal queue smid's */
3509 ioc->internal_lookup = kcalloc(ioc->internal_depth,
3510 sizeof(struct request_tracker), GFP_KERNEL);
3511 if (!ioc->internal_lookup) {
3512 pr_err(MPT3SAS_FMT "internal_lookup: kcalloc failed\n",
3513 ioc->name);
3514 goto out;
3515 }
3516 ioc->internal_smid = ioc->hi_priority_smid + ioc->hi_priority_depth;
3517 dinitprintk(ioc, pr_info(MPT3SAS_FMT
3518 "internal(0x%p): depth(%d), start smid(%d)\n",
3519 ioc->name, ioc->internal,
3520 ioc->internal_depth, ioc->internal_smid));
3521
3522 /* sense buffers, 4 byte align */
3523 sz = ioc->scsiio_depth * SCSI_SENSE_BUFFERSIZE;
3524 ioc->sense_dma_pool = pci_pool_create("sense pool", ioc->pdev, sz, 4,
3525 0);
3526 if (!ioc->sense_dma_pool) {
3527 pr_err(MPT3SAS_FMT "sense pool: pci_pool_create failed\n",
3528 ioc->name);
3529 goto out;
3530 }
3531 ioc->sense = pci_pool_alloc(ioc->sense_dma_pool , GFP_KERNEL,
3532 &ioc->sense_dma);
3533 if (!ioc->sense) {
3534 pr_err(MPT3SAS_FMT "sense pool: pci_pool_alloc failed\n",
3535 ioc->name);
3536 goto out;
3537 }
3538 dinitprintk(ioc, pr_info(MPT3SAS_FMT
3539 "sense pool(0x%p): depth(%d), element_size(%d), pool_size"
3540 "(%d kB)\n", ioc->name, ioc->sense, ioc->scsiio_depth,
3541 SCSI_SENSE_BUFFERSIZE, sz/1024));
3542 dinitprintk(ioc, pr_info(MPT3SAS_FMT "sense_dma(0x%llx)\n",
3543 ioc->name, (unsigned long long)ioc->sense_dma));
3544 total_sz += sz;
3545
3546 /* reply pool, 4 byte align */
3547 sz = ioc->reply_free_queue_depth * ioc->reply_sz;
3548 ioc->reply_dma_pool = pci_pool_create("reply pool", ioc->pdev, sz, 4,
3549 0);
3550 if (!ioc->reply_dma_pool) {
3551 pr_err(MPT3SAS_FMT "reply pool: pci_pool_create failed\n",
3552 ioc->name);
3553 goto out;
3554 }
3555 ioc->reply = pci_pool_alloc(ioc->reply_dma_pool , GFP_KERNEL,
3556 &ioc->reply_dma);
3557 if (!ioc->reply) {
3558 pr_err(MPT3SAS_FMT "reply pool: pci_pool_alloc failed\n",
3559 ioc->name);
3560 goto out;
3561 }
3562 ioc->reply_dma_min_address = (u32)(ioc->reply_dma);
3563 ioc->reply_dma_max_address = (u32)(ioc->reply_dma) + sz;
3564 dinitprintk(ioc, pr_info(MPT3SAS_FMT
3565 "reply pool(0x%p): depth(%d), frame_size(%d), pool_size(%d kB)\n",
3566 ioc->name, ioc->reply,
3567 ioc->reply_free_queue_depth, ioc->reply_sz, sz/1024));
3568 dinitprintk(ioc, pr_info(MPT3SAS_FMT "reply_dma(0x%llx)\n",
3569 ioc->name, (unsigned long long)ioc->reply_dma));
3570 total_sz += sz;
3571
3572 /* reply free queue, 16 byte align */
3573 sz = ioc->reply_free_queue_depth * 4;
3574 ioc->reply_free_dma_pool = pci_pool_create("reply_free pool",
3575 ioc->pdev, sz, 16, 0);
3576 if (!ioc->reply_free_dma_pool) {
3577 pr_err(MPT3SAS_FMT "reply_free pool: pci_pool_create failed\n",
3578 ioc->name);
3579 goto out;
3580 }
3581 ioc->reply_free = pci_pool_alloc(ioc->reply_free_dma_pool , GFP_KERNEL,
3582 &ioc->reply_free_dma);
3583 if (!ioc->reply_free) {
3584 pr_err(MPT3SAS_FMT "reply_free pool: pci_pool_alloc failed\n",
3585 ioc->name);
3586 goto out;
3587 }
3588 memset(ioc->reply_free, 0, sz);
3589 dinitprintk(ioc, pr_info(MPT3SAS_FMT "reply_free pool(0x%p): " \
3590 "depth(%d), element_size(%d), pool_size(%d kB)\n", ioc->name,
3591 ioc->reply_free, ioc->reply_free_queue_depth, 4, sz/1024));
3592 dinitprintk(ioc, pr_info(MPT3SAS_FMT
3593 "reply_free_dma (0x%llx)\n",
3594 ioc->name, (unsigned long long)ioc->reply_free_dma));
3595 total_sz += sz;
3596
Sreekanth Reddyf92363d2012-11-30 07:44:21 +05303597 ioc->config_page_sz = 512;
3598 ioc->config_page = pci_alloc_consistent(ioc->pdev,
3599 ioc->config_page_sz, &ioc->config_page_dma);
3600 if (!ioc->config_page) {
3601 pr_err(MPT3SAS_FMT
3602 "config page: pci_pool_alloc failed\n",
3603 ioc->name);
3604 goto out;
3605 }
3606 dinitprintk(ioc, pr_info(MPT3SAS_FMT
3607 "config page(0x%p): size(%d)\n",
3608 ioc->name, ioc->config_page, ioc->config_page_sz));
3609 dinitprintk(ioc, pr_info(MPT3SAS_FMT "config_page_dma(0x%llx)\n",
3610 ioc->name, (unsigned long long)ioc->config_page_dma));
3611 total_sz += ioc->config_page_sz;
3612
3613 pr_info(MPT3SAS_FMT "Allocated physical memory: size(%d kB)\n",
3614 ioc->name, total_sz/1024);
3615 pr_info(MPT3SAS_FMT
3616 "Current Controller Queue Depth(%d),Max Controller Queue Depth(%d)\n",
3617 ioc->name, ioc->shost->can_queue, facts->RequestCredit);
3618 pr_info(MPT3SAS_FMT "Scatter Gather Elements per IO(%d)\n",
3619 ioc->name, ioc->shost->sg_tablesize);
3620 return 0;
3621
3622 out:
3623 return -ENOMEM;
3624}
3625
3626/**
3627 * mpt3sas_base_get_iocstate - Get the current state of a MPT adapter.
3628 * @ioc: Pointer to MPT_ADAPTER structure
3629 * @cooked: Request raw or cooked IOC state
3630 *
3631 * Returns all IOC Doorbell register bits if cooked==0, else just the
3632 * Doorbell bits in MPI_IOC_STATE_MASK.
3633 */
3634u32
3635mpt3sas_base_get_iocstate(struct MPT3SAS_ADAPTER *ioc, int cooked)
3636{
3637 u32 s, sc;
3638
3639 s = readl(&ioc->chip->Doorbell);
3640 sc = s & MPI2_IOC_STATE_MASK;
3641 return cooked ? sc : s;
3642}
3643
3644/**
3645 * _base_wait_on_iocstate - waiting on a particular ioc state
3646 * @ioc_state: controller state { READY, OPERATIONAL, or RESET }
3647 * @timeout: timeout in second
3648 * @sleep_flag: CAN_SLEEP or NO_SLEEP
3649 *
3650 * Returns 0 for success, non-zero for failure.
3651 */
3652static int
3653_base_wait_on_iocstate(struct MPT3SAS_ADAPTER *ioc, u32 ioc_state, int timeout,
3654 int sleep_flag)
3655{
3656 u32 count, cntdn;
3657 u32 current_state;
3658
3659 count = 0;
3660 cntdn = (sleep_flag == CAN_SLEEP) ? 1000*timeout : 2000*timeout;
3661 do {
3662 current_state = mpt3sas_base_get_iocstate(ioc, 1);
3663 if (current_state == ioc_state)
3664 return 0;
3665 if (count && current_state == MPI2_IOC_STATE_FAULT)
3666 break;
3667 if (sleep_flag == CAN_SLEEP)
3668 usleep_range(1000, 1500);
3669 else
3670 udelay(500);
3671 count++;
3672 } while (--cntdn);
3673
3674 return current_state;
3675}
3676
3677/**
3678 * _base_wait_for_doorbell_int - waiting for controller interrupt(generated by
3679 * a write to the doorbell)
3680 * @ioc: per adapter object
3681 * @timeout: timeout in second
3682 * @sleep_flag: CAN_SLEEP or NO_SLEEP
3683 *
3684 * Returns 0 for success, non-zero for failure.
3685 *
3686 * Notes: MPI2_HIS_IOC2SYS_DB_STATUS - set to one when IOC writes to doorbell.
3687 */
3688static int
Sreekanth Reddy4dc8c802015-06-30 12:24:48 +05303689_base_diag_reset(struct MPT3SAS_ADAPTER *ioc, int sleep_flag);
3690
3691static int
Sreekanth Reddyf92363d2012-11-30 07:44:21 +05303692_base_wait_for_doorbell_int(struct MPT3SAS_ADAPTER *ioc, int timeout,
3693 int sleep_flag)
3694{
3695 u32 cntdn, count;
3696 u32 int_status;
3697
3698 count = 0;
3699 cntdn = (sleep_flag == CAN_SLEEP) ? 1000*timeout : 2000*timeout;
3700 do {
3701 int_status = readl(&ioc->chip->HostInterruptStatus);
3702 if (int_status & MPI2_HIS_IOC2SYS_DB_STATUS) {
3703 dhsprintk(ioc, pr_info(MPT3SAS_FMT
3704 "%s: successful count(%d), timeout(%d)\n",
3705 ioc->name, __func__, count, timeout));
3706 return 0;
3707 }
3708 if (sleep_flag == CAN_SLEEP)
3709 usleep_range(1000, 1500);
3710 else
3711 udelay(500);
3712 count++;
3713 } while (--cntdn);
3714
3715 pr_err(MPT3SAS_FMT
3716 "%s: failed due to timeout count(%d), int_status(%x)!\n",
3717 ioc->name, __func__, count, int_status);
3718 return -EFAULT;
3719}
3720
3721/**
3722 * _base_wait_for_doorbell_ack - waiting for controller to read the doorbell.
3723 * @ioc: per adapter object
3724 * @timeout: timeout in second
3725 * @sleep_flag: CAN_SLEEP or NO_SLEEP
3726 *
3727 * Returns 0 for success, non-zero for failure.
3728 *
3729 * Notes: MPI2_HIS_SYS2IOC_DB_STATUS - set to one when host writes to
3730 * doorbell.
3731 */
3732static int
3733_base_wait_for_doorbell_ack(struct MPT3SAS_ADAPTER *ioc, int timeout,
3734 int sleep_flag)
3735{
3736 u32 cntdn, count;
3737 u32 int_status;
3738 u32 doorbell;
3739
3740 count = 0;
3741 cntdn = (sleep_flag == CAN_SLEEP) ? 1000*timeout : 2000*timeout;
3742 do {
3743 int_status = readl(&ioc->chip->HostInterruptStatus);
3744 if (!(int_status & MPI2_HIS_SYS2IOC_DB_STATUS)) {
3745 dhsprintk(ioc, pr_info(MPT3SAS_FMT
3746 "%s: successful count(%d), timeout(%d)\n",
3747 ioc->name, __func__, count, timeout));
3748 return 0;
3749 } else if (int_status & MPI2_HIS_IOC2SYS_DB_STATUS) {
3750 doorbell = readl(&ioc->chip->Doorbell);
3751 if ((doorbell & MPI2_IOC_STATE_MASK) ==
3752 MPI2_IOC_STATE_FAULT) {
3753 mpt3sas_base_fault_info(ioc , doorbell);
3754 return -EFAULT;
3755 }
3756 } else if (int_status == 0xFFFFFFFF)
3757 goto out;
3758
3759 if (sleep_flag == CAN_SLEEP)
3760 usleep_range(1000, 1500);
3761 else
3762 udelay(500);
3763 count++;
3764 } while (--cntdn);
3765
3766 out:
3767 pr_err(MPT3SAS_FMT
3768 "%s: failed due to timeout count(%d), int_status(%x)!\n",
3769 ioc->name, __func__, count, int_status);
3770 return -EFAULT;
3771}
3772
3773/**
3774 * _base_wait_for_doorbell_not_used - waiting for doorbell to not be in use
3775 * @ioc: per adapter object
3776 * @timeout: timeout in second
3777 * @sleep_flag: CAN_SLEEP or NO_SLEEP
3778 *
3779 * Returns 0 for success, non-zero for failure.
3780 *
3781 */
3782static int
3783_base_wait_for_doorbell_not_used(struct MPT3SAS_ADAPTER *ioc, int timeout,
3784 int sleep_flag)
3785{
3786 u32 cntdn, count;
3787 u32 doorbell_reg;
3788
3789 count = 0;
3790 cntdn = (sleep_flag == CAN_SLEEP) ? 1000*timeout : 2000*timeout;
3791 do {
3792 doorbell_reg = readl(&ioc->chip->Doorbell);
3793 if (!(doorbell_reg & MPI2_DOORBELL_USED)) {
3794 dhsprintk(ioc, pr_info(MPT3SAS_FMT
3795 "%s: successful count(%d), timeout(%d)\n",
3796 ioc->name, __func__, count, timeout));
3797 return 0;
3798 }
3799 if (sleep_flag == CAN_SLEEP)
3800 usleep_range(1000, 1500);
3801 else
3802 udelay(500);
3803 count++;
3804 } while (--cntdn);
3805
3806 pr_err(MPT3SAS_FMT
3807 "%s: failed due to timeout count(%d), doorbell_reg(%x)!\n",
3808 ioc->name, __func__, count, doorbell_reg);
3809 return -EFAULT;
3810}
3811
3812/**
3813 * _base_send_ioc_reset - send doorbell reset
3814 * @ioc: per adapter object
3815 * @reset_type: currently only supports: MPI2_FUNCTION_IOC_MESSAGE_UNIT_RESET
3816 * @timeout: timeout in second
3817 * @sleep_flag: CAN_SLEEP or NO_SLEEP
3818 *
3819 * Returns 0 for success, non-zero for failure.
3820 */
3821static int
3822_base_send_ioc_reset(struct MPT3SAS_ADAPTER *ioc, u8 reset_type, int timeout,
3823 int sleep_flag)
3824{
3825 u32 ioc_state;
3826 int r = 0;
3827
3828 if (reset_type != MPI2_FUNCTION_IOC_MESSAGE_UNIT_RESET) {
3829 pr_err(MPT3SAS_FMT "%s: unknown reset_type\n",
3830 ioc->name, __func__);
3831 return -EFAULT;
3832 }
3833
3834 if (!(ioc->facts.IOCCapabilities &
3835 MPI2_IOCFACTS_CAPABILITY_EVENT_REPLAY))
3836 return -EFAULT;
3837
3838 pr_info(MPT3SAS_FMT "sending message unit reset !!\n", ioc->name);
3839
3840 writel(reset_type << MPI2_DOORBELL_FUNCTION_SHIFT,
3841 &ioc->chip->Doorbell);
3842 if ((_base_wait_for_doorbell_ack(ioc, 15, sleep_flag))) {
3843 r = -EFAULT;
3844 goto out;
3845 }
3846 ioc_state = _base_wait_on_iocstate(ioc, MPI2_IOC_STATE_READY,
3847 timeout, sleep_flag);
3848 if (ioc_state) {
3849 pr_err(MPT3SAS_FMT
3850 "%s: failed going to ready state (ioc_state=0x%x)\n",
3851 ioc->name, __func__, ioc_state);
3852 r = -EFAULT;
3853 goto out;
3854 }
3855 out:
3856 pr_info(MPT3SAS_FMT "message unit reset: %s\n",
3857 ioc->name, ((r == 0) ? "SUCCESS" : "FAILED"));
3858 return r;
3859}
3860
3861/**
3862 * _base_handshake_req_reply_wait - send request thru doorbell interface
3863 * @ioc: per adapter object
3864 * @request_bytes: request length
3865 * @request: pointer having request payload
3866 * @reply_bytes: reply length
3867 * @reply: pointer to reply payload
3868 * @timeout: timeout in second
3869 * @sleep_flag: CAN_SLEEP or NO_SLEEP
3870 *
3871 * Returns 0 for success, non-zero for failure.
3872 */
3873static int
3874_base_handshake_req_reply_wait(struct MPT3SAS_ADAPTER *ioc, int request_bytes,
3875 u32 *request, int reply_bytes, u16 *reply, int timeout, int sleep_flag)
3876{
3877 MPI2DefaultReply_t *default_reply = (MPI2DefaultReply_t *)reply;
3878 int i;
3879 u8 failed;
3880 u16 dummy;
3881 __le32 *mfp;
3882
3883 /* make sure doorbell is not in use */
3884 if ((readl(&ioc->chip->Doorbell) & MPI2_DOORBELL_USED)) {
3885 pr_err(MPT3SAS_FMT
3886 "doorbell is in use (line=%d)\n",
3887 ioc->name, __LINE__);
3888 return -EFAULT;
3889 }
3890
3891 /* clear pending doorbell interrupts from previous state changes */
3892 if (readl(&ioc->chip->HostInterruptStatus) &
3893 MPI2_HIS_IOC2SYS_DB_STATUS)
3894 writel(0, &ioc->chip->HostInterruptStatus);
3895
3896 /* send message to ioc */
3897 writel(((MPI2_FUNCTION_HANDSHAKE<<MPI2_DOORBELL_FUNCTION_SHIFT) |
3898 ((request_bytes/4)<<MPI2_DOORBELL_ADD_DWORDS_SHIFT)),
3899 &ioc->chip->Doorbell);
3900
3901 if ((_base_wait_for_doorbell_int(ioc, 5, NO_SLEEP))) {
3902 pr_err(MPT3SAS_FMT
3903 "doorbell handshake int failed (line=%d)\n",
3904 ioc->name, __LINE__);
3905 return -EFAULT;
3906 }
3907 writel(0, &ioc->chip->HostInterruptStatus);
3908
3909 if ((_base_wait_for_doorbell_ack(ioc, 5, sleep_flag))) {
3910 pr_err(MPT3SAS_FMT
3911 "doorbell handshake ack failed (line=%d)\n",
3912 ioc->name, __LINE__);
3913 return -EFAULT;
3914 }
3915
3916 /* send message 32-bits at a time */
3917 for (i = 0, failed = 0; i < request_bytes/4 && !failed; i++) {
3918 writel(cpu_to_le32(request[i]), &ioc->chip->Doorbell);
3919 if ((_base_wait_for_doorbell_ack(ioc, 5, sleep_flag)))
3920 failed = 1;
3921 }
3922
3923 if (failed) {
3924 pr_err(MPT3SAS_FMT
3925 "doorbell handshake sending request failed (line=%d)\n",
3926 ioc->name, __LINE__);
3927 return -EFAULT;
3928 }
3929
3930 /* now wait for the reply */
3931 if ((_base_wait_for_doorbell_int(ioc, timeout, sleep_flag))) {
3932 pr_err(MPT3SAS_FMT
3933 "doorbell handshake int failed (line=%d)\n",
3934 ioc->name, __LINE__);
3935 return -EFAULT;
3936 }
3937
3938 /* read the first two 16-bits, it gives the total length of the reply */
3939 reply[0] = le16_to_cpu(readl(&ioc->chip->Doorbell)
3940 & MPI2_DOORBELL_DATA_MASK);
3941 writel(0, &ioc->chip->HostInterruptStatus);
3942 if ((_base_wait_for_doorbell_int(ioc, 5, sleep_flag))) {
3943 pr_err(MPT3SAS_FMT
3944 "doorbell handshake int failed (line=%d)\n",
3945 ioc->name, __LINE__);
3946 return -EFAULT;
3947 }
3948 reply[1] = le16_to_cpu(readl(&ioc->chip->Doorbell)
3949 & MPI2_DOORBELL_DATA_MASK);
3950 writel(0, &ioc->chip->HostInterruptStatus);
3951
3952 for (i = 2; i < default_reply->MsgLength * 2; i++) {
3953 if ((_base_wait_for_doorbell_int(ioc, 5, sleep_flag))) {
3954 pr_err(MPT3SAS_FMT
3955 "doorbell handshake int failed (line=%d)\n",
3956 ioc->name, __LINE__);
3957 return -EFAULT;
3958 }
3959 if (i >= reply_bytes/2) /* overflow case */
3960 dummy = readl(&ioc->chip->Doorbell);
3961 else
3962 reply[i] = le16_to_cpu(readl(&ioc->chip->Doorbell)
3963 & MPI2_DOORBELL_DATA_MASK);
3964 writel(0, &ioc->chip->HostInterruptStatus);
3965 }
3966
3967 _base_wait_for_doorbell_int(ioc, 5, sleep_flag);
3968 if (_base_wait_for_doorbell_not_used(ioc, 5, sleep_flag) != 0) {
3969 dhsprintk(ioc, pr_info(MPT3SAS_FMT
3970 "doorbell is in use (line=%d)\n", ioc->name, __LINE__));
3971 }
3972 writel(0, &ioc->chip->HostInterruptStatus);
3973
3974 if (ioc->logging_level & MPT_DEBUG_INIT) {
3975 mfp = (__le32 *)reply;
3976 pr_info("\toffset:data\n");
3977 for (i = 0; i < reply_bytes/4; i++)
3978 pr_info("\t[0x%02x]:%08x\n", i*4,
3979 le32_to_cpu(mfp[i]));
3980 }
3981 return 0;
3982}
3983
3984/**
3985 * mpt3sas_base_sas_iounit_control - send sas iounit control to FW
3986 * @ioc: per adapter object
3987 * @mpi_reply: the reply payload from FW
3988 * @mpi_request: the request payload sent to FW
3989 *
3990 * The SAS IO Unit Control Request message allows the host to perform low-level
3991 * operations, such as resets on the PHYs of the IO Unit, also allows the host
3992 * to obtain the IOC assigned device handles for a device if it has other
3993 * identifying information about the device, in addition allows the host to
3994 * remove IOC resources associated with the device.
3995 *
3996 * Returns 0 for success, non-zero for failure.
3997 */
3998int
3999mpt3sas_base_sas_iounit_control(struct MPT3SAS_ADAPTER *ioc,
4000 Mpi2SasIoUnitControlReply_t *mpi_reply,
4001 Mpi2SasIoUnitControlRequest_t *mpi_request)
4002{
4003 u16 smid;
4004 u32 ioc_state;
4005 unsigned long timeleft;
Dan Carpentereb445522014-12-04 13:57:05 +03004006 bool issue_reset = false;
Sreekanth Reddyf92363d2012-11-30 07:44:21 +05304007 int rc;
4008 void *request;
4009 u16 wait_state_count;
4010
4011 dinitprintk(ioc, pr_info(MPT3SAS_FMT "%s\n", ioc->name,
4012 __func__));
4013
4014 mutex_lock(&ioc->base_cmds.mutex);
4015
4016 if (ioc->base_cmds.status != MPT3_CMD_NOT_USED) {
4017 pr_err(MPT3SAS_FMT "%s: base_cmd in use\n",
4018 ioc->name, __func__);
4019 rc = -EAGAIN;
4020 goto out;
4021 }
4022
4023 wait_state_count = 0;
4024 ioc_state = mpt3sas_base_get_iocstate(ioc, 1);
4025 while (ioc_state != MPI2_IOC_STATE_OPERATIONAL) {
4026 if (wait_state_count++ == 10) {
4027 pr_err(MPT3SAS_FMT
4028 "%s: failed due to ioc not operational\n",
4029 ioc->name, __func__);
4030 rc = -EFAULT;
4031 goto out;
4032 }
4033 ssleep(1);
4034 ioc_state = mpt3sas_base_get_iocstate(ioc, 1);
4035 pr_info(MPT3SAS_FMT
4036 "%s: waiting for operational state(count=%d)\n",
4037 ioc->name, __func__, wait_state_count);
4038 }
4039
4040 smid = mpt3sas_base_get_smid(ioc, ioc->base_cb_idx);
4041 if (!smid) {
4042 pr_err(MPT3SAS_FMT "%s: failed obtaining a smid\n",
4043 ioc->name, __func__);
4044 rc = -EAGAIN;
4045 goto out;
4046 }
4047
4048 rc = 0;
4049 ioc->base_cmds.status = MPT3_CMD_PENDING;
4050 request = mpt3sas_base_get_msg_frame(ioc, smid);
4051 ioc->base_cmds.smid = smid;
4052 memcpy(request, mpi_request, sizeof(Mpi2SasIoUnitControlRequest_t));
4053 if (mpi_request->Operation == MPI2_SAS_OP_PHY_HARD_RESET ||
4054 mpi_request->Operation == MPI2_SAS_OP_PHY_LINK_RESET)
4055 ioc->ioc_link_reset_in_progress = 1;
4056 init_completion(&ioc->base_cmds.done);
4057 mpt3sas_base_put_smid_default(ioc, smid);
4058 timeleft = wait_for_completion_timeout(&ioc->base_cmds.done,
4059 msecs_to_jiffies(10000));
4060 if ((mpi_request->Operation == MPI2_SAS_OP_PHY_HARD_RESET ||
4061 mpi_request->Operation == MPI2_SAS_OP_PHY_LINK_RESET) &&
4062 ioc->ioc_link_reset_in_progress)
4063 ioc->ioc_link_reset_in_progress = 0;
4064 if (!(ioc->base_cmds.status & MPT3_CMD_COMPLETE)) {
4065 pr_err(MPT3SAS_FMT "%s: timeout\n",
4066 ioc->name, __func__);
4067 _debug_dump_mf(mpi_request,
4068 sizeof(Mpi2SasIoUnitControlRequest_t)/4);
4069 if (!(ioc->base_cmds.status & MPT3_CMD_RESET))
Dan Carpentereb445522014-12-04 13:57:05 +03004070 issue_reset = true;
Sreekanth Reddyf92363d2012-11-30 07:44:21 +05304071 goto issue_host_reset;
4072 }
4073 if (ioc->base_cmds.status & MPT3_CMD_REPLY_VALID)
4074 memcpy(mpi_reply, ioc->base_cmds.reply,
4075 sizeof(Mpi2SasIoUnitControlReply_t));
4076 else
4077 memset(mpi_reply, 0, sizeof(Mpi2SasIoUnitControlReply_t));
4078 ioc->base_cmds.status = MPT3_CMD_NOT_USED;
4079 goto out;
4080
4081 issue_host_reset:
4082 if (issue_reset)
4083 mpt3sas_base_hard_reset_handler(ioc, CAN_SLEEP,
4084 FORCE_BIG_HAMMER);
4085 ioc->base_cmds.status = MPT3_CMD_NOT_USED;
4086 rc = -EFAULT;
4087 out:
4088 mutex_unlock(&ioc->base_cmds.mutex);
4089 return rc;
4090}
4091
4092/**
4093 * mpt3sas_base_scsi_enclosure_processor - sending request to sep device
4094 * @ioc: per adapter object
4095 * @mpi_reply: the reply payload from FW
4096 * @mpi_request: the request payload sent to FW
4097 *
4098 * The SCSI Enclosure Processor request message causes the IOC to
4099 * communicate with SES devices to control LED status signals.
4100 *
4101 * Returns 0 for success, non-zero for failure.
4102 */
4103int
4104mpt3sas_base_scsi_enclosure_processor(struct MPT3SAS_ADAPTER *ioc,
4105 Mpi2SepReply_t *mpi_reply, Mpi2SepRequest_t *mpi_request)
4106{
4107 u16 smid;
4108 u32 ioc_state;
4109 unsigned long timeleft;
Dan Carpentereb445522014-12-04 13:57:05 +03004110 bool issue_reset = false;
Sreekanth Reddyf92363d2012-11-30 07:44:21 +05304111 int rc;
4112 void *request;
4113 u16 wait_state_count;
4114
4115 dinitprintk(ioc, pr_info(MPT3SAS_FMT "%s\n", ioc->name,
4116 __func__));
4117
4118 mutex_lock(&ioc->base_cmds.mutex);
4119
4120 if (ioc->base_cmds.status != MPT3_CMD_NOT_USED) {
4121 pr_err(MPT3SAS_FMT "%s: base_cmd in use\n",
4122 ioc->name, __func__);
4123 rc = -EAGAIN;
4124 goto out;
4125 }
4126
4127 wait_state_count = 0;
4128 ioc_state = mpt3sas_base_get_iocstate(ioc, 1);
4129 while (ioc_state != MPI2_IOC_STATE_OPERATIONAL) {
4130 if (wait_state_count++ == 10) {
4131 pr_err(MPT3SAS_FMT
4132 "%s: failed due to ioc not operational\n",
4133 ioc->name, __func__);
4134 rc = -EFAULT;
4135 goto out;
4136 }
4137 ssleep(1);
4138 ioc_state = mpt3sas_base_get_iocstate(ioc, 1);
4139 pr_info(MPT3SAS_FMT
4140 "%s: waiting for operational state(count=%d)\n",
4141 ioc->name,
4142 __func__, wait_state_count);
4143 }
4144
4145 smid = mpt3sas_base_get_smid(ioc, ioc->base_cb_idx);
4146 if (!smid) {
4147 pr_err(MPT3SAS_FMT "%s: failed obtaining a smid\n",
4148 ioc->name, __func__);
4149 rc = -EAGAIN;
4150 goto out;
4151 }
4152
4153 rc = 0;
4154 ioc->base_cmds.status = MPT3_CMD_PENDING;
4155 request = mpt3sas_base_get_msg_frame(ioc, smid);
4156 ioc->base_cmds.smid = smid;
4157 memcpy(request, mpi_request, sizeof(Mpi2SepReply_t));
4158 init_completion(&ioc->base_cmds.done);
4159 mpt3sas_base_put_smid_default(ioc, smid);
4160 timeleft = wait_for_completion_timeout(&ioc->base_cmds.done,
4161 msecs_to_jiffies(10000));
4162 if (!(ioc->base_cmds.status & MPT3_CMD_COMPLETE)) {
4163 pr_err(MPT3SAS_FMT "%s: timeout\n",
4164 ioc->name, __func__);
4165 _debug_dump_mf(mpi_request,
4166 sizeof(Mpi2SepRequest_t)/4);
4167 if (!(ioc->base_cmds.status & MPT3_CMD_RESET))
Dan Carpentereb445522014-12-04 13:57:05 +03004168 issue_reset = false;
Sreekanth Reddyf92363d2012-11-30 07:44:21 +05304169 goto issue_host_reset;
4170 }
4171 if (ioc->base_cmds.status & MPT3_CMD_REPLY_VALID)
4172 memcpy(mpi_reply, ioc->base_cmds.reply,
4173 sizeof(Mpi2SepReply_t));
4174 else
4175 memset(mpi_reply, 0, sizeof(Mpi2SepReply_t));
4176 ioc->base_cmds.status = MPT3_CMD_NOT_USED;
4177 goto out;
4178
4179 issue_host_reset:
4180 if (issue_reset)
4181 mpt3sas_base_hard_reset_handler(ioc, CAN_SLEEP,
4182 FORCE_BIG_HAMMER);
4183 ioc->base_cmds.status = MPT3_CMD_NOT_USED;
4184 rc = -EFAULT;
4185 out:
4186 mutex_unlock(&ioc->base_cmds.mutex);
4187 return rc;
4188}
4189
4190/**
4191 * _base_get_port_facts - obtain port facts reply and save in ioc
4192 * @ioc: per adapter object
4193 * @sleep_flag: CAN_SLEEP or NO_SLEEP
4194 *
4195 * Returns 0 for success, non-zero for failure.
4196 */
4197static int
4198_base_get_port_facts(struct MPT3SAS_ADAPTER *ioc, int port, int sleep_flag)
4199{
4200 Mpi2PortFactsRequest_t mpi_request;
4201 Mpi2PortFactsReply_t mpi_reply;
4202 struct mpt3sas_port_facts *pfacts;
4203 int mpi_reply_sz, mpi_request_sz, r;
4204
4205 dinitprintk(ioc, pr_info(MPT3SAS_FMT "%s\n", ioc->name,
4206 __func__));
4207
4208 mpi_reply_sz = sizeof(Mpi2PortFactsReply_t);
4209 mpi_request_sz = sizeof(Mpi2PortFactsRequest_t);
4210 memset(&mpi_request, 0, mpi_request_sz);
4211 mpi_request.Function = MPI2_FUNCTION_PORT_FACTS;
4212 mpi_request.PortNumber = port;
4213 r = _base_handshake_req_reply_wait(ioc, mpi_request_sz,
4214 (u32 *)&mpi_request, mpi_reply_sz, (u16 *)&mpi_reply, 5, CAN_SLEEP);
4215
4216 if (r != 0) {
4217 pr_err(MPT3SAS_FMT "%s: handshake failed (r=%d)\n",
4218 ioc->name, __func__, r);
4219 return r;
4220 }
4221
4222 pfacts = &ioc->pfacts[port];
4223 memset(pfacts, 0, sizeof(struct mpt3sas_port_facts));
4224 pfacts->PortNumber = mpi_reply.PortNumber;
4225 pfacts->VP_ID = mpi_reply.VP_ID;
4226 pfacts->VF_ID = mpi_reply.VF_ID;
4227 pfacts->MaxPostedCmdBuffers =
4228 le16_to_cpu(mpi_reply.MaxPostedCmdBuffers);
4229
4230 return 0;
4231}
4232
4233/**
Sreekanth Reddy4dc8c802015-06-30 12:24:48 +05304234 * _base_wait_for_iocstate - Wait until the card is in READY or OPERATIONAL
4235 * @ioc: per adapter object
4236 * @timeout:
4237 * @sleep_flag: CAN_SLEEP or NO_SLEEP
4238 *
4239 * Returns 0 for success, non-zero for failure.
4240 */
4241static int
4242_base_wait_for_iocstate(struct MPT3SAS_ADAPTER *ioc, int timeout,
4243 int sleep_flag)
4244{
4245 u32 ioc_state;
4246 int rc;
4247
4248 dinitprintk(ioc, printk(MPT3SAS_FMT "%s\n", ioc->name,
4249 __func__));
4250
4251 if (ioc->pci_error_recovery) {
4252 dfailprintk(ioc, printk(MPT3SAS_FMT
4253 "%s: host in pci error recovery\n", ioc->name, __func__));
4254 return -EFAULT;
4255 }
4256
4257 ioc_state = mpt3sas_base_get_iocstate(ioc, 0);
4258 dhsprintk(ioc, printk(MPT3SAS_FMT "%s: ioc_state(0x%08x)\n",
4259 ioc->name, __func__, ioc_state));
4260
4261 if (((ioc_state & MPI2_IOC_STATE_MASK) == MPI2_IOC_STATE_READY) ||
4262 (ioc_state & MPI2_IOC_STATE_MASK) == MPI2_IOC_STATE_OPERATIONAL)
4263 return 0;
4264
4265 if (ioc_state & MPI2_DOORBELL_USED) {
4266 dhsprintk(ioc, printk(MPT3SAS_FMT
4267 "unexpected doorbell active!\n", ioc->name));
4268 goto issue_diag_reset;
4269 }
4270
4271 if ((ioc_state & MPI2_IOC_STATE_MASK) == MPI2_IOC_STATE_FAULT) {
4272 mpt3sas_base_fault_info(ioc, ioc_state &
4273 MPI2_DOORBELL_DATA_MASK);
4274 goto issue_diag_reset;
4275 }
4276
4277 ioc_state = _base_wait_on_iocstate(ioc, MPI2_IOC_STATE_READY,
4278 timeout, sleep_flag);
4279 if (ioc_state) {
4280 dfailprintk(ioc, printk(MPT3SAS_FMT
4281 "%s: failed going to ready state (ioc_state=0x%x)\n",
4282 ioc->name, __func__, ioc_state));
4283 return -EFAULT;
4284 }
4285
4286 issue_diag_reset:
4287 rc = _base_diag_reset(ioc, sleep_flag);
4288 return rc;
4289}
4290
4291/**
Sreekanth Reddyf92363d2012-11-30 07:44:21 +05304292 * _base_get_ioc_facts - obtain ioc facts reply and save in ioc
4293 * @ioc: per adapter object
4294 * @sleep_flag: CAN_SLEEP or NO_SLEEP
4295 *
4296 * Returns 0 for success, non-zero for failure.
4297 */
4298static int
4299_base_get_ioc_facts(struct MPT3SAS_ADAPTER *ioc, int sleep_flag)
4300{
4301 Mpi2IOCFactsRequest_t mpi_request;
4302 Mpi2IOCFactsReply_t mpi_reply;
4303 struct mpt3sas_facts *facts;
4304 int mpi_reply_sz, mpi_request_sz, r;
4305
4306 dinitprintk(ioc, pr_info(MPT3SAS_FMT "%s\n", ioc->name,
4307 __func__));
4308
Sreekanth Reddy4dc8c802015-06-30 12:24:48 +05304309 r = _base_wait_for_iocstate(ioc, 10, sleep_flag);
4310 if (r) {
4311 dfailprintk(ioc, printk(MPT3SAS_FMT
4312 "%s: failed getting to correct state\n",
4313 ioc->name, __func__));
4314 return r;
4315 }
Sreekanth Reddyf92363d2012-11-30 07:44:21 +05304316 mpi_reply_sz = sizeof(Mpi2IOCFactsReply_t);
4317 mpi_request_sz = sizeof(Mpi2IOCFactsRequest_t);
4318 memset(&mpi_request, 0, mpi_request_sz);
4319 mpi_request.Function = MPI2_FUNCTION_IOC_FACTS;
4320 r = _base_handshake_req_reply_wait(ioc, mpi_request_sz,
4321 (u32 *)&mpi_request, mpi_reply_sz, (u16 *)&mpi_reply, 5, CAN_SLEEP);
4322
4323 if (r != 0) {
4324 pr_err(MPT3SAS_FMT "%s: handshake failed (r=%d)\n",
4325 ioc->name, __func__, r);
4326 return r;
4327 }
4328
4329 facts = &ioc->facts;
4330 memset(facts, 0, sizeof(struct mpt3sas_facts));
4331 facts->MsgVersion = le16_to_cpu(mpi_reply.MsgVersion);
4332 facts->HeaderVersion = le16_to_cpu(mpi_reply.HeaderVersion);
4333 facts->VP_ID = mpi_reply.VP_ID;
4334 facts->VF_ID = mpi_reply.VF_ID;
4335 facts->IOCExceptions = le16_to_cpu(mpi_reply.IOCExceptions);
4336 facts->MaxChainDepth = mpi_reply.MaxChainDepth;
4337 facts->WhoInit = mpi_reply.WhoInit;
4338 facts->NumberOfPorts = mpi_reply.NumberOfPorts;
4339 facts->MaxMSIxVectors = mpi_reply.MaxMSIxVectors;
4340 facts->RequestCredit = le16_to_cpu(mpi_reply.RequestCredit);
4341 facts->MaxReplyDescriptorPostQueueDepth =
4342 le16_to_cpu(mpi_reply.MaxReplyDescriptorPostQueueDepth);
4343 facts->ProductID = le16_to_cpu(mpi_reply.ProductID);
4344 facts->IOCCapabilities = le32_to_cpu(mpi_reply.IOCCapabilities);
4345 if ((facts->IOCCapabilities & MPI2_IOCFACTS_CAPABILITY_INTEGRATED_RAID))
4346 ioc->ir_firmware = 1;
Sreekanth Reddy9b05c912014-09-12 15:35:31 +05304347 if ((facts->IOCCapabilities &
4348 MPI2_IOCFACTS_CAPABILITY_RDPQ_ARRAY_CAPABLE))
4349 ioc->rdpq_array_capable = 1;
Sreekanth Reddyf92363d2012-11-30 07:44:21 +05304350 facts->FWVersion.Word = le32_to_cpu(mpi_reply.FWVersion.Word);
4351 facts->IOCRequestFrameSize =
4352 le16_to_cpu(mpi_reply.IOCRequestFrameSize);
Suganath prabu Subramaniebb30242016-01-28 12:07:04 +05304353 if (ioc->hba_mpi_version_belonged != MPI2_VERSION) {
4354 facts->IOCMaxChainSegmentSize =
4355 le16_to_cpu(mpi_reply.IOCMaxChainSegmentSize);
4356 }
Sreekanth Reddyf92363d2012-11-30 07:44:21 +05304357 facts->MaxInitiators = le16_to_cpu(mpi_reply.MaxInitiators);
4358 facts->MaxTargets = le16_to_cpu(mpi_reply.MaxTargets);
4359 ioc->shost->max_id = -1;
4360 facts->MaxSasExpanders = le16_to_cpu(mpi_reply.MaxSasExpanders);
4361 facts->MaxEnclosures = le16_to_cpu(mpi_reply.MaxEnclosures);
4362 facts->ProtocolFlags = le16_to_cpu(mpi_reply.ProtocolFlags);
4363 facts->HighPriorityCredit =
4364 le16_to_cpu(mpi_reply.HighPriorityCredit);
4365 facts->ReplyFrameSize = mpi_reply.ReplyFrameSize;
4366 facts->MaxDevHandle = le16_to_cpu(mpi_reply.MaxDevHandle);
4367
4368 dinitprintk(ioc, pr_info(MPT3SAS_FMT
4369 "hba queue depth(%d), max chains per io(%d)\n",
4370 ioc->name, facts->RequestCredit,
4371 facts->MaxChainDepth));
4372 dinitprintk(ioc, pr_info(MPT3SAS_FMT
4373 "request frame size(%d), reply frame size(%d)\n", ioc->name,
4374 facts->IOCRequestFrameSize * 4, facts->ReplyFrameSize * 4));
4375 return 0;
4376}
4377
4378/**
4379 * _base_send_ioc_init - send ioc_init to firmware
4380 * @ioc: per adapter object
4381 * @sleep_flag: CAN_SLEEP or NO_SLEEP
4382 *
4383 * Returns 0 for success, non-zero for failure.
4384 */
4385static int
4386_base_send_ioc_init(struct MPT3SAS_ADAPTER *ioc, int sleep_flag)
4387{
4388 Mpi2IOCInitRequest_t mpi_request;
4389 Mpi2IOCInitReply_t mpi_reply;
Sreekanth Reddy9b05c912014-09-12 15:35:31 +05304390 int i, r = 0;
Sreekanth Reddyf92363d2012-11-30 07:44:21 +05304391 struct timeval current_time;
4392 u16 ioc_status;
Sreekanth Reddy9b05c912014-09-12 15:35:31 +05304393 u32 reply_post_free_array_sz = 0;
4394 Mpi2IOCInitRDPQArrayEntry *reply_post_free_array = NULL;
4395 dma_addr_t reply_post_free_array_dma;
Sreekanth Reddyf92363d2012-11-30 07:44:21 +05304396
4397 dinitprintk(ioc, pr_info(MPT3SAS_FMT "%s\n", ioc->name,
4398 __func__));
4399
4400 memset(&mpi_request, 0, sizeof(Mpi2IOCInitRequest_t));
4401 mpi_request.Function = MPI2_FUNCTION_IOC_INIT;
4402 mpi_request.WhoInit = MPI2_WHOINIT_HOST_DRIVER;
4403 mpi_request.VF_ID = 0; /* TODO */
4404 mpi_request.VP_ID = 0;
Sreekanth Reddyd357e842015-11-11 17:30:22 +05304405 mpi_request.MsgVersion = cpu_to_le16(ioc->hba_mpi_version_belonged);
Sreekanth Reddyf92363d2012-11-30 07:44:21 +05304406 mpi_request.HeaderVersion = cpu_to_le16(MPI2_HEADER_VERSION);
4407
4408 if (_base_is_controller_msix_enabled(ioc))
4409 mpi_request.HostMSIxVectors = ioc->reply_queue_count;
4410 mpi_request.SystemRequestFrameSize = cpu_to_le16(ioc->request_sz/4);
4411 mpi_request.ReplyDescriptorPostQueueDepth =
4412 cpu_to_le16(ioc->reply_post_queue_depth);
4413 mpi_request.ReplyFreeQueueDepth =
4414 cpu_to_le16(ioc->reply_free_queue_depth);
4415
4416 mpi_request.SenseBufferAddressHigh =
4417 cpu_to_le32((u64)ioc->sense_dma >> 32);
4418 mpi_request.SystemReplyAddressHigh =
4419 cpu_to_le32((u64)ioc->reply_dma >> 32);
4420 mpi_request.SystemRequestFrameBaseAddress =
4421 cpu_to_le64((u64)ioc->request_dma);
4422 mpi_request.ReplyFreeQueueAddress =
4423 cpu_to_le64((u64)ioc->reply_free_dma);
Sreekanth Reddyf92363d2012-11-30 07:44:21 +05304424
Sreekanth Reddy9b05c912014-09-12 15:35:31 +05304425 if (ioc->rdpq_array_enable) {
4426 reply_post_free_array_sz = ioc->reply_queue_count *
4427 sizeof(Mpi2IOCInitRDPQArrayEntry);
4428 reply_post_free_array = pci_alloc_consistent(ioc->pdev,
4429 reply_post_free_array_sz, &reply_post_free_array_dma);
4430 if (!reply_post_free_array) {
4431 pr_err(MPT3SAS_FMT
4432 "reply_post_free_array: pci_alloc_consistent failed\n",
4433 ioc->name);
4434 r = -ENOMEM;
4435 goto out;
4436 }
4437 memset(reply_post_free_array, 0, reply_post_free_array_sz);
4438 for (i = 0; i < ioc->reply_queue_count; i++)
4439 reply_post_free_array[i].RDPQBaseAddress =
4440 cpu_to_le64(
4441 (u64)ioc->reply_post[i].reply_post_free_dma);
4442 mpi_request.MsgFlags = MPI2_IOCINIT_MSGFLAG_RDPQ_ARRAY_MODE;
4443 mpi_request.ReplyDescriptorPostQueueAddress =
4444 cpu_to_le64((u64)reply_post_free_array_dma);
4445 } else {
4446 mpi_request.ReplyDescriptorPostQueueAddress =
4447 cpu_to_le64((u64)ioc->reply_post[0].reply_post_free_dma);
4448 }
Sreekanth Reddyf92363d2012-11-30 07:44:21 +05304449
4450 /* This time stamp specifies number of milliseconds
4451 * since epoch ~ midnight January 1, 1970.
4452 */
4453 do_gettimeofday(&current_time);
4454 mpi_request.TimeStamp = cpu_to_le64((u64)current_time.tv_sec * 1000 +
4455 (current_time.tv_usec / 1000));
4456
4457 if (ioc->logging_level & MPT_DEBUG_INIT) {
4458 __le32 *mfp;
4459 int i;
4460
4461 mfp = (__le32 *)&mpi_request;
4462 pr_info("\toffset:data\n");
4463 for (i = 0; i < sizeof(Mpi2IOCInitRequest_t)/4; i++)
4464 pr_info("\t[0x%02x]:%08x\n", i*4,
4465 le32_to_cpu(mfp[i]));
4466 }
4467
4468 r = _base_handshake_req_reply_wait(ioc,
4469 sizeof(Mpi2IOCInitRequest_t), (u32 *)&mpi_request,
4470 sizeof(Mpi2IOCInitReply_t), (u16 *)&mpi_reply, 10,
4471 sleep_flag);
4472
4473 if (r != 0) {
4474 pr_err(MPT3SAS_FMT "%s: handshake failed (r=%d)\n",
4475 ioc->name, __func__, r);
Sreekanth Reddy9b05c912014-09-12 15:35:31 +05304476 goto out;
Sreekanth Reddyf92363d2012-11-30 07:44:21 +05304477 }
4478
4479 ioc_status = le16_to_cpu(mpi_reply.IOCStatus) & MPI2_IOCSTATUS_MASK;
4480 if (ioc_status != MPI2_IOCSTATUS_SUCCESS ||
4481 mpi_reply.IOCLogInfo) {
4482 pr_err(MPT3SAS_FMT "%s: failed\n", ioc->name, __func__);
4483 r = -EIO;
4484 }
4485
Sreekanth Reddy9b05c912014-09-12 15:35:31 +05304486out:
4487 if (reply_post_free_array)
4488 pci_free_consistent(ioc->pdev, reply_post_free_array_sz,
4489 reply_post_free_array,
4490 reply_post_free_array_dma);
4491 return r;
Sreekanth Reddyf92363d2012-11-30 07:44:21 +05304492}
4493
4494/**
4495 * mpt3sas_port_enable_done - command completion routine for port enable
4496 * @ioc: per adapter object
4497 * @smid: system request message index
4498 * @msix_index: MSIX table index supplied by the OS
4499 * @reply: reply message frame(lower 32bit addr)
4500 *
4501 * Return 1 meaning mf should be freed from _base_interrupt
4502 * 0 means the mf is freed from this function.
4503 */
4504u8
4505mpt3sas_port_enable_done(struct MPT3SAS_ADAPTER *ioc, u16 smid, u8 msix_index,
4506 u32 reply)
4507{
4508 MPI2DefaultReply_t *mpi_reply;
4509 u16 ioc_status;
4510
4511 if (ioc->port_enable_cmds.status == MPT3_CMD_NOT_USED)
4512 return 1;
4513
4514 mpi_reply = mpt3sas_base_get_reply_virt_addr(ioc, reply);
4515 if (!mpi_reply)
4516 return 1;
4517
4518 if (mpi_reply->Function != MPI2_FUNCTION_PORT_ENABLE)
4519 return 1;
4520
4521 ioc->port_enable_cmds.status &= ~MPT3_CMD_PENDING;
4522 ioc->port_enable_cmds.status |= MPT3_CMD_COMPLETE;
4523 ioc->port_enable_cmds.status |= MPT3_CMD_REPLY_VALID;
4524 memcpy(ioc->port_enable_cmds.reply, mpi_reply, mpi_reply->MsgLength*4);
4525 ioc_status = le16_to_cpu(mpi_reply->IOCStatus) & MPI2_IOCSTATUS_MASK;
4526 if (ioc_status != MPI2_IOCSTATUS_SUCCESS)
4527 ioc->port_enable_failed = 1;
4528
4529 if (ioc->is_driver_loading) {
4530 if (ioc_status == MPI2_IOCSTATUS_SUCCESS) {
4531 mpt3sas_port_enable_complete(ioc);
4532 return 1;
4533 } else {
4534 ioc->start_scan_failed = ioc_status;
4535 ioc->start_scan = 0;
4536 return 1;
4537 }
4538 }
4539 complete(&ioc->port_enable_cmds.done);
4540 return 1;
4541}
4542
4543/**
4544 * _base_send_port_enable - send port_enable(discovery stuff) to firmware
4545 * @ioc: per adapter object
4546 * @sleep_flag: CAN_SLEEP or NO_SLEEP
4547 *
4548 * Returns 0 for success, non-zero for failure.
4549 */
4550static int
4551_base_send_port_enable(struct MPT3SAS_ADAPTER *ioc, int sleep_flag)
4552{
4553 Mpi2PortEnableRequest_t *mpi_request;
4554 Mpi2PortEnableReply_t *mpi_reply;
4555 unsigned long timeleft;
4556 int r = 0;
4557 u16 smid;
4558 u16 ioc_status;
4559
4560 pr_info(MPT3SAS_FMT "sending port enable !!\n", ioc->name);
4561
4562 if (ioc->port_enable_cmds.status & MPT3_CMD_PENDING) {
4563 pr_err(MPT3SAS_FMT "%s: internal command already in use\n",
4564 ioc->name, __func__);
4565 return -EAGAIN;
4566 }
4567
4568 smid = mpt3sas_base_get_smid(ioc, ioc->port_enable_cb_idx);
4569 if (!smid) {
4570 pr_err(MPT3SAS_FMT "%s: failed obtaining a smid\n",
4571 ioc->name, __func__);
4572 return -EAGAIN;
4573 }
4574
4575 ioc->port_enable_cmds.status = MPT3_CMD_PENDING;
4576 mpi_request = mpt3sas_base_get_msg_frame(ioc, smid);
4577 ioc->port_enable_cmds.smid = smid;
4578 memset(mpi_request, 0, sizeof(Mpi2PortEnableRequest_t));
4579 mpi_request->Function = MPI2_FUNCTION_PORT_ENABLE;
4580
4581 init_completion(&ioc->port_enable_cmds.done);
4582 mpt3sas_base_put_smid_default(ioc, smid);
4583 timeleft = wait_for_completion_timeout(&ioc->port_enable_cmds.done,
4584 300*HZ);
4585 if (!(ioc->port_enable_cmds.status & MPT3_CMD_COMPLETE)) {
4586 pr_err(MPT3SAS_FMT "%s: timeout\n",
4587 ioc->name, __func__);
4588 _debug_dump_mf(mpi_request,
4589 sizeof(Mpi2PortEnableRequest_t)/4);
4590 if (ioc->port_enable_cmds.status & MPT3_CMD_RESET)
4591 r = -EFAULT;
4592 else
4593 r = -ETIME;
4594 goto out;
4595 }
4596
4597 mpi_reply = ioc->port_enable_cmds.reply;
4598 ioc_status = le16_to_cpu(mpi_reply->IOCStatus) & MPI2_IOCSTATUS_MASK;
4599 if (ioc_status != MPI2_IOCSTATUS_SUCCESS) {
4600 pr_err(MPT3SAS_FMT "%s: failed with (ioc_status=0x%08x)\n",
4601 ioc->name, __func__, ioc_status);
4602 r = -EFAULT;
4603 goto out;
4604 }
4605
4606 out:
4607 ioc->port_enable_cmds.status = MPT3_CMD_NOT_USED;
4608 pr_info(MPT3SAS_FMT "port enable: %s\n", ioc->name, ((r == 0) ?
4609 "SUCCESS" : "FAILED"));
4610 return r;
4611}
4612
4613/**
4614 * mpt3sas_port_enable - initiate firmware discovery (don't wait for reply)
4615 * @ioc: per adapter object
4616 *
4617 * Returns 0 for success, non-zero for failure.
4618 */
4619int
4620mpt3sas_port_enable(struct MPT3SAS_ADAPTER *ioc)
4621{
4622 Mpi2PortEnableRequest_t *mpi_request;
4623 u16 smid;
4624
4625 pr_info(MPT3SAS_FMT "sending port enable !!\n", ioc->name);
4626
4627 if (ioc->port_enable_cmds.status & MPT3_CMD_PENDING) {
4628 pr_err(MPT3SAS_FMT "%s: internal command already in use\n",
4629 ioc->name, __func__);
4630 return -EAGAIN;
4631 }
4632
4633 smid = mpt3sas_base_get_smid(ioc, ioc->port_enable_cb_idx);
4634 if (!smid) {
4635 pr_err(MPT3SAS_FMT "%s: failed obtaining a smid\n",
4636 ioc->name, __func__);
4637 return -EAGAIN;
4638 }
4639
4640 ioc->port_enable_cmds.status = MPT3_CMD_PENDING;
4641 mpi_request = mpt3sas_base_get_msg_frame(ioc, smid);
4642 ioc->port_enable_cmds.smid = smid;
4643 memset(mpi_request, 0, sizeof(Mpi2PortEnableRequest_t));
4644 mpi_request->Function = MPI2_FUNCTION_PORT_ENABLE;
4645
4646 mpt3sas_base_put_smid_default(ioc, smid);
4647 return 0;
4648}
4649
4650/**
4651 * _base_determine_wait_on_discovery - desposition
4652 * @ioc: per adapter object
4653 *
4654 * Decide whether to wait on discovery to complete. Used to either
4655 * locate boot device, or report volumes ahead of physical devices.
4656 *
4657 * Returns 1 for wait, 0 for don't wait
4658 */
4659static int
4660_base_determine_wait_on_discovery(struct MPT3SAS_ADAPTER *ioc)
4661{
4662 /* We wait for discovery to complete if IR firmware is loaded.
4663 * The sas topology events arrive before PD events, so we need time to
4664 * turn on the bit in ioc->pd_handles to indicate PD
4665 * Also, it maybe required to report Volumes ahead of physical
4666 * devices when MPI2_IOCPAGE8_IRFLAGS_LOW_VOLUME_MAPPING is set.
4667 */
4668 if (ioc->ir_firmware)
4669 return 1;
4670
4671 /* if no Bios, then we don't need to wait */
4672 if (!ioc->bios_pg3.BiosVersion)
4673 return 0;
4674
4675 /* Bios is present, then we drop down here.
4676 *
4677 * If there any entries in the Bios Page 2, then we wait
4678 * for discovery to complete.
4679 */
4680
4681 /* Current Boot Device */
4682 if ((ioc->bios_pg2.CurrentBootDeviceForm &
4683 MPI2_BIOSPAGE2_FORM_MASK) ==
4684 MPI2_BIOSPAGE2_FORM_NO_DEVICE_SPECIFIED &&
4685 /* Request Boot Device */
4686 (ioc->bios_pg2.ReqBootDeviceForm &
4687 MPI2_BIOSPAGE2_FORM_MASK) ==
4688 MPI2_BIOSPAGE2_FORM_NO_DEVICE_SPECIFIED &&
4689 /* Alternate Request Boot Device */
4690 (ioc->bios_pg2.ReqAltBootDeviceForm &
4691 MPI2_BIOSPAGE2_FORM_MASK) ==
4692 MPI2_BIOSPAGE2_FORM_NO_DEVICE_SPECIFIED)
4693 return 0;
4694
4695 return 1;
4696}
4697
4698/**
4699 * _base_unmask_events - turn on notification for this event
4700 * @ioc: per adapter object
4701 * @event: firmware event
4702 *
4703 * The mask is stored in ioc->event_masks.
4704 */
4705static void
4706_base_unmask_events(struct MPT3SAS_ADAPTER *ioc, u16 event)
4707{
4708 u32 desired_event;
4709
4710 if (event >= 128)
4711 return;
4712
4713 desired_event = (1 << (event % 32));
4714
4715 if (event < 32)
4716 ioc->event_masks[0] &= ~desired_event;
4717 else if (event < 64)
4718 ioc->event_masks[1] &= ~desired_event;
4719 else if (event < 96)
4720 ioc->event_masks[2] &= ~desired_event;
4721 else if (event < 128)
4722 ioc->event_masks[3] &= ~desired_event;
4723}
4724
4725/**
4726 * _base_event_notification - send event notification
4727 * @ioc: per adapter object
4728 * @sleep_flag: CAN_SLEEP or NO_SLEEP
4729 *
4730 * Returns 0 for success, non-zero for failure.
4731 */
4732static int
4733_base_event_notification(struct MPT3SAS_ADAPTER *ioc, int sleep_flag)
4734{
4735 Mpi2EventNotificationRequest_t *mpi_request;
4736 unsigned long timeleft;
4737 u16 smid;
4738 int r = 0;
4739 int i;
4740
4741 dinitprintk(ioc, pr_info(MPT3SAS_FMT "%s\n", ioc->name,
4742 __func__));
4743
4744 if (ioc->base_cmds.status & MPT3_CMD_PENDING) {
4745 pr_err(MPT3SAS_FMT "%s: internal command already in use\n",
4746 ioc->name, __func__);
4747 return -EAGAIN;
4748 }
4749
4750 smid = mpt3sas_base_get_smid(ioc, ioc->base_cb_idx);
4751 if (!smid) {
4752 pr_err(MPT3SAS_FMT "%s: failed obtaining a smid\n",
4753 ioc->name, __func__);
4754 return -EAGAIN;
4755 }
4756 ioc->base_cmds.status = MPT3_CMD_PENDING;
4757 mpi_request = mpt3sas_base_get_msg_frame(ioc, smid);
4758 ioc->base_cmds.smid = smid;
4759 memset(mpi_request, 0, sizeof(Mpi2EventNotificationRequest_t));
4760 mpi_request->Function = MPI2_FUNCTION_EVENT_NOTIFICATION;
4761 mpi_request->VF_ID = 0; /* TODO */
4762 mpi_request->VP_ID = 0;
4763 for (i = 0; i < MPI2_EVENT_NOTIFY_EVENTMASK_WORDS; i++)
4764 mpi_request->EventMasks[i] =
4765 cpu_to_le32(ioc->event_masks[i]);
4766 init_completion(&ioc->base_cmds.done);
4767 mpt3sas_base_put_smid_default(ioc, smid);
4768 timeleft = wait_for_completion_timeout(&ioc->base_cmds.done, 30*HZ);
4769 if (!(ioc->base_cmds.status & MPT3_CMD_COMPLETE)) {
4770 pr_err(MPT3SAS_FMT "%s: timeout\n",
4771 ioc->name, __func__);
4772 _debug_dump_mf(mpi_request,
4773 sizeof(Mpi2EventNotificationRequest_t)/4);
4774 if (ioc->base_cmds.status & MPT3_CMD_RESET)
4775 r = -EFAULT;
4776 else
4777 r = -ETIME;
4778 } else
4779 dinitprintk(ioc, pr_info(MPT3SAS_FMT "%s: complete\n",
4780 ioc->name, __func__));
4781 ioc->base_cmds.status = MPT3_CMD_NOT_USED;
4782 return r;
4783}
4784
4785/**
4786 * mpt3sas_base_validate_event_type - validating event types
4787 * @ioc: per adapter object
4788 * @event: firmware event
4789 *
4790 * This will turn on firmware event notification when application
4791 * ask for that event. We don't mask events that are already enabled.
4792 */
4793void
4794mpt3sas_base_validate_event_type(struct MPT3SAS_ADAPTER *ioc, u32 *event_type)
4795{
4796 int i, j;
4797 u32 event_mask, desired_event;
4798 u8 send_update_to_fw;
4799
4800 for (i = 0, send_update_to_fw = 0; i <
4801 MPI2_EVENT_NOTIFY_EVENTMASK_WORDS; i++) {
4802 event_mask = ~event_type[i];
4803 desired_event = 1;
4804 for (j = 0; j < 32; j++) {
4805 if (!(event_mask & desired_event) &&
4806 (ioc->event_masks[i] & desired_event)) {
4807 ioc->event_masks[i] &= ~desired_event;
4808 send_update_to_fw = 1;
4809 }
4810 desired_event = (desired_event << 1);
4811 }
4812 }
4813
4814 if (!send_update_to_fw)
4815 return;
4816
4817 mutex_lock(&ioc->base_cmds.mutex);
4818 _base_event_notification(ioc, CAN_SLEEP);
4819 mutex_unlock(&ioc->base_cmds.mutex);
4820}
4821
4822/**
4823 * _base_diag_reset - the "big hammer" start of day reset
4824 * @ioc: per adapter object
4825 * @sleep_flag: CAN_SLEEP or NO_SLEEP
4826 *
4827 * Returns 0 for success, non-zero for failure.
4828 */
4829static int
4830_base_diag_reset(struct MPT3SAS_ADAPTER *ioc, int sleep_flag)
4831{
4832 u32 host_diagnostic;
4833 u32 ioc_state;
4834 u32 count;
4835 u32 hcb_size;
4836
4837 pr_info(MPT3SAS_FMT "sending diag reset !!\n", ioc->name);
4838
4839 drsprintk(ioc, pr_info(MPT3SAS_FMT "clear interrupts\n",
4840 ioc->name));
4841
4842 count = 0;
4843 do {
4844 /* Write magic sequence to WriteSequence register
4845 * Loop until in diagnostic mode
4846 */
4847 drsprintk(ioc, pr_info(MPT3SAS_FMT
4848 "write magic sequence\n", ioc->name));
4849 writel(MPI2_WRSEQ_FLUSH_KEY_VALUE, &ioc->chip->WriteSequence);
4850 writel(MPI2_WRSEQ_1ST_KEY_VALUE, &ioc->chip->WriteSequence);
4851 writel(MPI2_WRSEQ_2ND_KEY_VALUE, &ioc->chip->WriteSequence);
4852 writel(MPI2_WRSEQ_3RD_KEY_VALUE, &ioc->chip->WriteSequence);
4853 writel(MPI2_WRSEQ_4TH_KEY_VALUE, &ioc->chip->WriteSequence);
4854 writel(MPI2_WRSEQ_5TH_KEY_VALUE, &ioc->chip->WriteSequence);
4855 writel(MPI2_WRSEQ_6TH_KEY_VALUE, &ioc->chip->WriteSequence);
4856
4857 /* wait 100 msec */
4858 if (sleep_flag == CAN_SLEEP)
4859 msleep(100);
4860 else
4861 mdelay(100);
4862
4863 if (count++ > 20)
4864 goto out;
4865
4866 host_diagnostic = readl(&ioc->chip->HostDiagnostic);
4867 drsprintk(ioc, pr_info(MPT3SAS_FMT
4868 "wrote magic sequence: count(%d), host_diagnostic(0x%08x)\n",
4869 ioc->name, count, host_diagnostic));
4870
4871 } while ((host_diagnostic & MPI2_DIAG_DIAG_WRITE_ENABLE) == 0);
4872
4873 hcb_size = readl(&ioc->chip->HCBSize);
4874
4875 drsprintk(ioc, pr_info(MPT3SAS_FMT "diag reset: issued\n",
4876 ioc->name));
4877 writel(host_diagnostic | MPI2_DIAG_RESET_ADAPTER,
4878 &ioc->chip->HostDiagnostic);
4879
Sreekanth Reddyb453ff82013-06-29 03:51:19 +05304880 /*This delay allows the chip PCIe hardware time to finish reset tasks*/
4881 if (sleep_flag == CAN_SLEEP)
4882 msleep(MPI2_HARD_RESET_PCIE_FIRST_READ_DELAY_MICRO_SEC/1000);
4883 else
4884 mdelay(MPI2_HARD_RESET_PCIE_FIRST_READ_DELAY_MICRO_SEC/1000);
Sreekanth Reddyf92363d2012-11-30 07:44:21 +05304885
Sreekanth Reddyb453ff82013-06-29 03:51:19 +05304886 /* Approximately 300 second max wait */
4887 for (count = 0; count < (300000000 /
4888 MPI2_HARD_RESET_PCIE_SECOND_READ_DELAY_MICRO_SEC); count++) {
Sreekanth Reddyf92363d2012-11-30 07:44:21 +05304889
4890 host_diagnostic = readl(&ioc->chip->HostDiagnostic);
4891
4892 if (host_diagnostic == 0xFFFFFFFF)
4893 goto out;
4894 if (!(host_diagnostic & MPI2_DIAG_RESET_ADAPTER))
4895 break;
4896
Sreekanth Reddyb453ff82013-06-29 03:51:19 +05304897 /* Wait to pass the second read delay window */
Sreekanth Reddyf92363d2012-11-30 07:44:21 +05304898 if (sleep_flag == CAN_SLEEP)
Sreekanth Reddyb453ff82013-06-29 03:51:19 +05304899 msleep(MPI2_HARD_RESET_PCIE_SECOND_READ_DELAY_MICRO_SEC
4900 / 1000);
Sreekanth Reddyf92363d2012-11-30 07:44:21 +05304901 else
Sreekanth Reddyb453ff82013-06-29 03:51:19 +05304902 mdelay(MPI2_HARD_RESET_PCIE_SECOND_READ_DELAY_MICRO_SEC
4903 / 1000);
Sreekanth Reddyf92363d2012-11-30 07:44:21 +05304904 }
4905
4906 if (host_diagnostic & MPI2_DIAG_HCB_MODE) {
4907
4908 drsprintk(ioc, pr_info(MPT3SAS_FMT
4909 "restart the adapter assuming the HCB Address points to good F/W\n",
4910 ioc->name));
4911 host_diagnostic &= ~MPI2_DIAG_BOOT_DEVICE_SELECT_MASK;
4912 host_diagnostic |= MPI2_DIAG_BOOT_DEVICE_SELECT_HCDW;
4913 writel(host_diagnostic, &ioc->chip->HostDiagnostic);
4914
4915 drsprintk(ioc, pr_info(MPT3SAS_FMT
4916 "re-enable the HCDW\n", ioc->name));
4917 writel(hcb_size | MPI2_HCB_SIZE_HCB_ENABLE,
4918 &ioc->chip->HCBSize);
4919 }
4920
4921 drsprintk(ioc, pr_info(MPT3SAS_FMT "restart the adapter\n",
4922 ioc->name));
4923 writel(host_diagnostic & ~MPI2_DIAG_HOLD_IOC_RESET,
4924 &ioc->chip->HostDiagnostic);
4925
4926 drsprintk(ioc, pr_info(MPT3SAS_FMT
4927 "disable writes to the diagnostic register\n", ioc->name));
4928 writel(MPI2_WRSEQ_FLUSH_KEY_VALUE, &ioc->chip->WriteSequence);
4929
4930 drsprintk(ioc, pr_info(MPT3SAS_FMT
4931 "Wait for FW to go to the READY state\n", ioc->name));
4932 ioc_state = _base_wait_on_iocstate(ioc, MPI2_IOC_STATE_READY, 20,
4933 sleep_flag);
4934 if (ioc_state) {
4935 pr_err(MPT3SAS_FMT
4936 "%s: failed going to ready state (ioc_state=0x%x)\n",
4937 ioc->name, __func__, ioc_state);
4938 goto out;
4939 }
4940
4941 pr_info(MPT3SAS_FMT "diag reset: SUCCESS\n", ioc->name);
4942 return 0;
4943
4944 out:
4945 pr_err(MPT3SAS_FMT "diag reset: FAILED\n", ioc->name);
4946 return -EFAULT;
4947}
4948
4949/**
4950 * _base_make_ioc_ready - put controller in READY state
4951 * @ioc: per adapter object
4952 * @sleep_flag: CAN_SLEEP or NO_SLEEP
4953 * @type: FORCE_BIG_HAMMER or SOFT_RESET
4954 *
4955 * Returns 0 for success, non-zero for failure.
4956 */
4957static int
4958_base_make_ioc_ready(struct MPT3SAS_ADAPTER *ioc, int sleep_flag,
4959 enum reset_type type)
4960{
4961 u32 ioc_state;
4962 int rc;
4963 int count;
4964
4965 dinitprintk(ioc, pr_info(MPT3SAS_FMT "%s\n", ioc->name,
4966 __func__));
4967
4968 if (ioc->pci_error_recovery)
4969 return 0;
4970
4971 ioc_state = mpt3sas_base_get_iocstate(ioc, 0);
4972 dhsprintk(ioc, pr_info(MPT3SAS_FMT "%s: ioc_state(0x%08x)\n",
4973 ioc->name, __func__, ioc_state));
4974
4975 /* if in RESET state, it should move to READY state shortly */
4976 count = 0;
4977 if ((ioc_state & MPI2_IOC_STATE_MASK) == MPI2_IOC_STATE_RESET) {
4978 while ((ioc_state & MPI2_IOC_STATE_MASK) !=
4979 MPI2_IOC_STATE_READY) {
4980 if (count++ == 10) {
4981 pr_err(MPT3SAS_FMT
4982 "%s: failed going to ready state (ioc_state=0x%x)\n",
4983 ioc->name, __func__, ioc_state);
4984 return -EFAULT;
4985 }
4986 if (sleep_flag == CAN_SLEEP)
4987 ssleep(1);
4988 else
4989 mdelay(1000);
4990 ioc_state = mpt3sas_base_get_iocstate(ioc, 0);
4991 }
4992 }
4993
4994 if ((ioc_state & MPI2_IOC_STATE_MASK) == MPI2_IOC_STATE_READY)
4995 return 0;
4996
4997 if (ioc_state & MPI2_DOORBELL_USED) {
4998 dhsprintk(ioc, pr_info(MPT3SAS_FMT
4999 "unexpected doorbell active!\n",
5000 ioc->name));
5001 goto issue_diag_reset;
5002 }
5003
5004 if ((ioc_state & MPI2_IOC_STATE_MASK) == MPI2_IOC_STATE_FAULT) {
5005 mpt3sas_base_fault_info(ioc, ioc_state &
5006 MPI2_DOORBELL_DATA_MASK);
5007 goto issue_diag_reset;
5008 }
5009
5010 if (type == FORCE_BIG_HAMMER)
5011 goto issue_diag_reset;
5012
5013 if ((ioc_state & MPI2_IOC_STATE_MASK) == MPI2_IOC_STATE_OPERATIONAL)
5014 if (!(_base_send_ioc_reset(ioc,
5015 MPI2_FUNCTION_IOC_MESSAGE_UNIT_RESET, 15, CAN_SLEEP))) {
5016 return 0;
5017 }
5018
5019 issue_diag_reset:
5020 rc = _base_diag_reset(ioc, CAN_SLEEP);
5021 return rc;
5022}
5023
5024/**
5025 * _base_make_ioc_operational - put controller in OPERATIONAL state
5026 * @ioc: per adapter object
5027 * @sleep_flag: CAN_SLEEP or NO_SLEEP
5028 *
5029 * Returns 0 for success, non-zero for failure.
5030 */
5031static int
5032_base_make_ioc_operational(struct MPT3SAS_ADAPTER *ioc, int sleep_flag)
5033{
5034 int r, i;
5035 unsigned long flags;
5036 u32 reply_address;
5037 u16 smid;
5038 struct _tr_list *delayed_tr, *delayed_tr_next;
Suganath prabu Subramanifd0331b2016-01-28 12:07:02 +05305039 struct _sc_list *delayed_sc, *delayed_sc_next;
5040 struct _event_ack_list *delayed_event_ack, *delayed_event_ack_next;
Sreekanth Reddy7786ab62015-11-11 17:30:28 +05305041 u8 hide_flag;
Sreekanth Reddyf92363d2012-11-30 07:44:21 +05305042 struct adapter_reply_queue *reply_q;
5043 long reply_post_free;
Sreekanth Reddy9b05c912014-09-12 15:35:31 +05305044 u32 reply_post_free_sz, index = 0;
Sreekanth Reddyf92363d2012-11-30 07:44:21 +05305045
5046 dinitprintk(ioc, pr_info(MPT3SAS_FMT "%s\n", ioc->name,
5047 __func__));
5048
5049 /* clean the delayed target reset list */
5050 list_for_each_entry_safe(delayed_tr, delayed_tr_next,
5051 &ioc->delayed_tr_list, list) {
5052 list_del(&delayed_tr->list);
5053 kfree(delayed_tr);
5054 }
5055
5056
5057 list_for_each_entry_safe(delayed_tr, delayed_tr_next,
5058 &ioc->delayed_tr_volume_list, list) {
5059 list_del(&delayed_tr->list);
5060 kfree(delayed_tr);
5061 }
5062
Suganath prabu Subramanifd0331b2016-01-28 12:07:02 +05305063 list_for_each_entry_safe(delayed_sc, delayed_sc_next,
5064 &ioc->delayed_sc_list, list) {
5065 list_del(&delayed_sc->list);
5066 kfree(delayed_sc);
5067 }
5068
5069 list_for_each_entry_safe(delayed_event_ack, delayed_event_ack_next,
5070 &ioc->delayed_event_ack_list, list) {
5071 list_del(&delayed_event_ack->list);
5072 kfree(delayed_event_ack);
5073 }
5074
Sreekanth Reddyf92363d2012-11-30 07:44:21 +05305075 /* initialize the scsi lookup free list */
5076 spin_lock_irqsave(&ioc->scsi_lookup_lock, flags);
5077 INIT_LIST_HEAD(&ioc->free_list);
5078 smid = 1;
5079 for (i = 0; i < ioc->scsiio_depth; i++, smid++) {
5080 INIT_LIST_HEAD(&ioc->scsi_lookup[i].chain_list);
5081 ioc->scsi_lookup[i].cb_idx = 0xFF;
5082 ioc->scsi_lookup[i].smid = smid;
5083 ioc->scsi_lookup[i].scmd = NULL;
Sreekanth Reddy7786ab62015-11-11 17:30:28 +05305084 ioc->scsi_lookup[i].direct_io = 0;
Sreekanth Reddyf92363d2012-11-30 07:44:21 +05305085 list_add_tail(&ioc->scsi_lookup[i].tracker_list,
5086 &ioc->free_list);
5087 }
5088
5089 /* hi-priority queue */
5090 INIT_LIST_HEAD(&ioc->hpr_free_list);
5091 smid = ioc->hi_priority_smid;
5092 for (i = 0; i < ioc->hi_priority_depth; i++, smid++) {
5093 ioc->hpr_lookup[i].cb_idx = 0xFF;
5094 ioc->hpr_lookup[i].smid = smid;
5095 list_add_tail(&ioc->hpr_lookup[i].tracker_list,
5096 &ioc->hpr_free_list);
5097 }
5098
5099 /* internal queue */
5100 INIT_LIST_HEAD(&ioc->internal_free_list);
5101 smid = ioc->internal_smid;
5102 for (i = 0; i < ioc->internal_depth; i++, smid++) {
5103 ioc->internal_lookup[i].cb_idx = 0xFF;
5104 ioc->internal_lookup[i].smid = smid;
5105 list_add_tail(&ioc->internal_lookup[i].tracker_list,
5106 &ioc->internal_free_list);
5107 }
5108
5109 /* chain pool */
5110 INIT_LIST_HEAD(&ioc->free_chain_list);
5111 for (i = 0; i < ioc->chain_depth; i++)
5112 list_add_tail(&ioc->chain_lookup[i].tracker_list,
5113 &ioc->free_chain_list);
5114
5115 spin_unlock_irqrestore(&ioc->scsi_lookup_lock, flags);
5116
5117 /* initialize Reply Free Queue */
5118 for (i = 0, reply_address = (u32)ioc->reply_dma ;
5119 i < ioc->reply_free_queue_depth ; i++, reply_address +=
5120 ioc->reply_sz)
5121 ioc->reply_free[i] = cpu_to_le32(reply_address);
5122
5123 /* initialize reply queues */
5124 if (ioc->is_driver_loading)
5125 _base_assign_reply_queues(ioc);
5126
5127 /* initialize Reply Post Free Queue */
Sreekanth Reddyf92363d2012-11-30 07:44:21 +05305128 reply_post_free_sz = ioc->reply_post_queue_depth *
5129 sizeof(Mpi2DefaultReplyDescriptor_t);
Sreekanth Reddy9b05c912014-09-12 15:35:31 +05305130 reply_post_free = (long)ioc->reply_post[index].reply_post_free;
Sreekanth Reddyf92363d2012-11-30 07:44:21 +05305131 list_for_each_entry(reply_q, &ioc->reply_queue_list, list) {
5132 reply_q->reply_post_host_index = 0;
5133 reply_q->reply_post_free = (Mpi2ReplyDescriptorsUnion_t *)
5134 reply_post_free;
5135 for (i = 0; i < ioc->reply_post_queue_depth; i++)
5136 reply_q->reply_post_free[i].Words =
5137 cpu_to_le64(ULLONG_MAX);
5138 if (!_base_is_controller_msix_enabled(ioc))
5139 goto skip_init_reply_post_free_queue;
Sreekanth Reddy9b05c912014-09-12 15:35:31 +05305140 /*
5141 * If RDPQ is enabled, switch to the next allocation.
5142 * Otherwise advance within the contiguous region.
5143 */
5144 if (ioc->rdpq_array_enable)
5145 reply_post_free = (long)
5146 ioc->reply_post[++index].reply_post_free;
5147 else
5148 reply_post_free += reply_post_free_sz;
Sreekanth Reddyf92363d2012-11-30 07:44:21 +05305149 }
5150 skip_init_reply_post_free_queue:
5151
5152 r = _base_send_ioc_init(ioc, sleep_flag);
5153 if (r)
5154 return r;
5155
5156 /* initialize reply free host index */
5157 ioc->reply_free_host_index = ioc->reply_free_queue_depth - 1;
5158 writel(ioc->reply_free_host_index, &ioc->chip->ReplyFreeHostIndex);
5159
5160 /* initialize reply post host index */
5161 list_for_each_entry(reply_q, &ioc->reply_queue_list, list) {
Sreekanth Reddyfb77bb52015-06-30 12:24:47 +05305162 if (ioc->msix96_vector)
5163 writel((reply_q->msix_index & 7)<<
5164 MPI2_RPHI_MSIX_INDEX_SHIFT,
5165 ioc->replyPostRegisterIndex[reply_q->msix_index/8]);
5166 else
5167 writel(reply_q->msix_index <<
5168 MPI2_RPHI_MSIX_INDEX_SHIFT,
5169 &ioc->chip->ReplyPostHostIndex);
5170
Sreekanth Reddyf92363d2012-11-30 07:44:21 +05305171 if (!_base_is_controller_msix_enabled(ioc))
5172 goto skip_init_reply_post_host_index;
5173 }
5174
5175 skip_init_reply_post_host_index:
5176
5177 _base_unmask_interrupts(ioc);
5178 r = _base_event_notification(ioc, sleep_flag);
5179 if (r)
5180 return r;
5181
5182 if (sleep_flag == CAN_SLEEP)
5183 _base_static_config_pages(ioc);
5184
5185
5186 if (ioc->is_driver_loading) {
Sreekanth Reddy7786ab62015-11-11 17:30:28 +05305187
5188 if (ioc->is_warpdrive && ioc->manu_pg10.OEMIdentifier
5189 == 0x80) {
5190 hide_flag = (u8) (
5191 le32_to_cpu(ioc->manu_pg10.OEMSpecificFlags0) &
5192 MFG_PAGE10_HIDE_SSDS_MASK);
5193 if (hide_flag != MFG_PAGE10_HIDE_SSDS_MASK)
5194 ioc->mfg_pg10_hide_flag = hide_flag;
5195 }
5196
Sreekanth Reddyf92363d2012-11-30 07:44:21 +05305197 ioc->wait_for_discovery_to_complete =
5198 _base_determine_wait_on_discovery(ioc);
5199
5200 return r; /* scan_start and scan_finished support */
5201 }
5202
5203 r = _base_send_port_enable(ioc, sleep_flag);
5204 if (r)
5205 return r;
5206
5207 return r;
5208}
5209
5210/**
5211 * mpt3sas_base_free_resources - free resources controller resources
5212 * @ioc: per adapter object
5213 *
5214 * Return nothing.
5215 */
5216void
5217mpt3sas_base_free_resources(struct MPT3SAS_ADAPTER *ioc)
5218{
Sreekanth Reddyf92363d2012-11-30 07:44:21 +05305219 dexitprintk(ioc, pr_info(MPT3SAS_FMT "%s\n", ioc->name,
5220 __func__));
5221
Sreekanth Reddy08c4d552015-11-11 17:30:33 +05305222 /* synchronizing freeing resource with pci_access_mutex lock */
5223 mutex_lock(&ioc->pci_access_mutex);
Joe Lawrencecf9bd21a2013-08-08 16:45:39 -04005224 if (ioc->chip_phys && ioc->chip) {
5225 _base_mask_interrupts(ioc);
5226 ioc->shost_recovery = 1;
5227 _base_make_ioc_ready(ioc, CAN_SLEEP, SOFT_RESET);
5228 ioc->shost_recovery = 0;
5229 }
5230
Sreekanth Reddy580d4e32015-06-30 12:24:50 +05305231 mpt3sas_base_unmap_resources(ioc);
Sreekanth Reddy08c4d552015-11-11 17:30:33 +05305232 mutex_unlock(&ioc->pci_access_mutex);
Sreekanth Reddyf92363d2012-11-30 07:44:21 +05305233 return;
5234}
5235
5236/**
5237 * mpt3sas_base_attach - attach controller instance
5238 * @ioc: per adapter object
5239 *
5240 * Returns 0 for success, non-zero for failure.
5241 */
5242int
5243mpt3sas_base_attach(struct MPT3SAS_ADAPTER *ioc)
5244{
5245 int r, i;
5246 int cpu_id, last_cpu_id = 0;
5247
5248 dinitprintk(ioc, pr_info(MPT3SAS_FMT "%s\n", ioc->name,
5249 __func__));
5250
5251 /* setup cpu_msix_table */
5252 ioc->cpu_count = num_online_cpus();
5253 for_each_online_cpu(cpu_id)
5254 last_cpu_id = cpu_id;
5255 ioc->cpu_msix_table_sz = last_cpu_id + 1;
5256 ioc->cpu_msix_table = kzalloc(ioc->cpu_msix_table_sz, GFP_KERNEL);
5257 ioc->reply_queue_count = 1;
5258 if (!ioc->cpu_msix_table) {
5259 dfailprintk(ioc, pr_info(MPT3SAS_FMT
5260 "allocation for cpu_msix_table failed!!!\n",
5261 ioc->name));
5262 r = -ENOMEM;
5263 goto out_free_resources;
5264 }
5265
Sreekanth Reddy7786ab62015-11-11 17:30:28 +05305266 if (ioc->is_warpdrive) {
5267 ioc->reply_post_host_index = kcalloc(ioc->cpu_msix_table_sz,
5268 sizeof(resource_size_t *), GFP_KERNEL);
5269 if (!ioc->reply_post_host_index) {
5270 dfailprintk(ioc, pr_info(MPT3SAS_FMT "allocation "
5271 "for cpu_msix_table failed!!!\n", ioc->name));
5272 r = -ENOMEM;
5273 goto out_free_resources;
5274 }
5275 }
5276
Sreekanth Reddy9b05c912014-09-12 15:35:31 +05305277 ioc->rdpq_array_enable_assigned = 0;
5278 ioc->dma_mask = 0;
Sreekanth Reddyf92363d2012-11-30 07:44:21 +05305279 r = mpt3sas_base_map_resources(ioc);
5280 if (r)
5281 goto out_free_resources;
5282
Sreekanth Reddy7786ab62015-11-11 17:30:28 +05305283 if (ioc->is_warpdrive) {
5284 ioc->reply_post_host_index[0] = (resource_size_t __iomem *)
5285 &ioc->chip->ReplyPostHostIndex;
5286
5287 for (i = 1; i < ioc->cpu_msix_table_sz; i++)
5288 ioc->reply_post_host_index[i] =
5289 (resource_size_t __iomem *)
5290 ((u8 __iomem *)&ioc->chip->Doorbell + (0x4000 + ((i - 1)
5291 * 4)));
5292 }
Sreekanth Reddyf92363d2012-11-30 07:44:21 +05305293
5294 pci_set_drvdata(ioc->pdev, ioc->shost);
5295 r = _base_get_ioc_facts(ioc, CAN_SLEEP);
5296 if (r)
5297 goto out_free_resources;
5298
Sreekanth Reddy471ef9d2015-11-11 17:30:24 +05305299 switch (ioc->hba_mpi_version_belonged) {
5300 case MPI2_VERSION:
5301 ioc->build_sg_scmd = &_base_build_sg_scmd;
5302 ioc->build_sg = &_base_build_sg;
5303 ioc->build_zero_len_sge = &_base_build_zero_len_sge;
5304 break;
5305 case MPI25_VERSION:
Suganath prabu Subramanib130b0d2016-01-28 12:06:58 +05305306 case MPI26_VERSION:
Sreekanth Reddy471ef9d2015-11-11 17:30:24 +05305307 /*
5308 * In SAS3.0,
5309 * SCSI_IO, SMP_PASSTHRU, SATA_PASSTHRU, Target Assist, and
5310 * Target Status - all require the IEEE formated scatter gather
5311 * elements.
5312 */
5313 ioc->build_sg_scmd = &_base_build_sg_scmd_ieee;
5314 ioc->build_sg = &_base_build_sg_ieee;
5315 ioc->build_zero_len_sge = &_base_build_zero_len_sge_ieee;
5316 ioc->sge_size_ieee = sizeof(Mpi2IeeeSgeSimple64_t);
5317 break;
5318 }
Sreekanth Reddyf92363d2012-11-30 07:44:21 +05305319
5320 /*
5321 * These function pointers for other requests that don't
5322 * the require IEEE scatter gather elements.
5323 *
5324 * For example Configuration Pages and SAS IOUNIT Control don't.
5325 */
5326 ioc->build_sg_mpi = &_base_build_sg;
5327 ioc->build_zero_len_sge_mpi = &_base_build_zero_len_sge;
5328
5329 r = _base_make_ioc_ready(ioc, CAN_SLEEP, SOFT_RESET);
5330 if (r)
5331 goto out_free_resources;
5332
5333 ioc->pfacts = kcalloc(ioc->facts.NumberOfPorts,
5334 sizeof(struct mpt3sas_port_facts), GFP_KERNEL);
5335 if (!ioc->pfacts) {
5336 r = -ENOMEM;
5337 goto out_free_resources;
5338 }
5339
5340 for (i = 0 ; i < ioc->facts.NumberOfPorts; i++) {
5341 r = _base_get_port_facts(ioc, i, CAN_SLEEP);
5342 if (r)
5343 goto out_free_resources;
5344 }
5345
5346 r = _base_allocate_memory_pools(ioc, CAN_SLEEP);
5347 if (r)
5348 goto out_free_resources;
5349
5350 init_waitqueue_head(&ioc->reset_wq);
5351
5352 /* allocate memory pd handle bitmask list */
5353 ioc->pd_handles_sz = (ioc->facts.MaxDevHandle / 8);
5354 if (ioc->facts.MaxDevHandle % 8)
5355 ioc->pd_handles_sz++;
5356 ioc->pd_handles = kzalloc(ioc->pd_handles_sz,
5357 GFP_KERNEL);
5358 if (!ioc->pd_handles) {
5359 r = -ENOMEM;
5360 goto out_free_resources;
5361 }
5362 ioc->blocking_handles = kzalloc(ioc->pd_handles_sz,
5363 GFP_KERNEL);
5364 if (!ioc->blocking_handles) {
5365 r = -ENOMEM;
5366 goto out_free_resources;
5367 }
5368
5369 ioc->fwfault_debug = mpt3sas_fwfault_debug;
5370
5371 /* base internal command bits */
5372 mutex_init(&ioc->base_cmds.mutex);
5373 ioc->base_cmds.reply = kzalloc(ioc->reply_sz, GFP_KERNEL);
5374 ioc->base_cmds.status = MPT3_CMD_NOT_USED;
5375
5376 /* port_enable command bits */
5377 ioc->port_enable_cmds.reply = kzalloc(ioc->reply_sz, GFP_KERNEL);
5378 ioc->port_enable_cmds.status = MPT3_CMD_NOT_USED;
5379
5380 /* transport internal command bits */
5381 ioc->transport_cmds.reply = kzalloc(ioc->reply_sz, GFP_KERNEL);
5382 ioc->transport_cmds.status = MPT3_CMD_NOT_USED;
5383 mutex_init(&ioc->transport_cmds.mutex);
5384
5385 /* scsih internal command bits */
5386 ioc->scsih_cmds.reply = kzalloc(ioc->reply_sz, GFP_KERNEL);
5387 ioc->scsih_cmds.status = MPT3_CMD_NOT_USED;
5388 mutex_init(&ioc->scsih_cmds.mutex);
5389
5390 /* task management internal command bits */
5391 ioc->tm_cmds.reply = kzalloc(ioc->reply_sz, GFP_KERNEL);
5392 ioc->tm_cmds.status = MPT3_CMD_NOT_USED;
5393 mutex_init(&ioc->tm_cmds.mutex);
5394
5395 /* config page internal command bits */
5396 ioc->config_cmds.reply = kzalloc(ioc->reply_sz, GFP_KERNEL);
5397 ioc->config_cmds.status = MPT3_CMD_NOT_USED;
5398 mutex_init(&ioc->config_cmds.mutex);
5399
5400 /* ctl module internal command bits */
5401 ioc->ctl_cmds.reply = kzalloc(ioc->reply_sz, GFP_KERNEL);
5402 ioc->ctl_cmds.sense = kzalloc(SCSI_SENSE_BUFFERSIZE, GFP_KERNEL);
5403 ioc->ctl_cmds.status = MPT3_CMD_NOT_USED;
5404 mutex_init(&ioc->ctl_cmds.mutex);
5405
5406 if (!ioc->base_cmds.reply || !ioc->transport_cmds.reply ||
5407 !ioc->scsih_cmds.reply || !ioc->tm_cmds.reply ||
5408 !ioc->config_cmds.reply || !ioc->ctl_cmds.reply ||
5409 !ioc->ctl_cmds.sense) {
5410 r = -ENOMEM;
5411 goto out_free_resources;
5412 }
5413
5414 for (i = 0; i < MPI2_EVENT_NOTIFY_EVENTMASK_WORDS; i++)
5415 ioc->event_masks[i] = -1;
5416
5417 /* here we enable the events we care about */
5418 _base_unmask_events(ioc, MPI2_EVENT_SAS_DISCOVERY);
5419 _base_unmask_events(ioc, MPI2_EVENT_SAS_BROADCAST_PRIMITIVE);
5420 _base_unmask_events(ioc, MPI2_EVENT_SAS_TOPOLOGY_CHANGE_LIST);
5421 _base_unmask_events(ioc, MPI2_EVENT_SAS_DEVICE_STATUS_CHANGE);
5422 _base_unmask_events(ioc, MPI2_EVENT_SAS_ENCL_DEVICE_STATUS_CHANGE);
5423 _base_unmask_events(ioc, MPI2_EVENT_IR_CONFIGURATION_CHANGE_LIST);
5424 _base_unmask_events(ioc, MPI2_EVENT_IR_VOLUME);
5425 _base_unmask_events(ioc, MPI2_EVENT_IR_PHYSICAL_DISK);
5426 _base_unmask_events(ioc, MPI2_EVENT_IR_OPERATION_STATUS);
5427 _base_unmask_events(ioc, MPI2_EVENT_LOG_ENTRY_ADDED);
Sreekanth Reddy2d8ce8c2015-01-12 11:38:56 +05305428 _base_unmask_events(ioc, MPI2_EVENT_TEMP_THRESHOLD);
Sreekanth Reddyf92363d2012-11-30 07:44:21 +05305429
5430 r = _base_make_ioc_operational(ioc, CAN_SLEEP);
5431 if (r)
5432 goto out_free_resources;
5433
Sreekanth Reddy16e179b2015-11-11 17:30:27 +05305434 ioc->non_operational_loop = 0;
Sreekanth Reddyf92363d2012-11-30 07:44:21 +05305435 return 0;
5436
5437 out_free_resources:
5438
5439 ioc->remove_host = 1;
5440
5441 mpt3sas_base_free_resources(ioc);
5442 _base_release_memory_pools(ioc);
5443 pci_set_drvdata(ioc->pdev, NULL);
5444 kfree(ioc->cpu_msix_table);
Sreekanth Reddy7786ab62015-11-11 17:30:28 +05305445 if (ioc->is_warpdrive)
5446 kfree(ioc->reply_post_host_index);
Sreekanth Reddyf92363d2012-11-30 07:44:21 +05305447 kfree(ioc->pd_handles);
5448 kfree(ioc->blocking_handles);
5449 kfree(ioc->tm_cmds.reply);
5450 kfree(ioc->transport_cmds.reply);
5451 kfree(ioc->scsih_cmds.reply);
5452 kfree(ioc->config_cmds.reply);
5453 kfree(ioc->base_cmds.reply);
5454 kfree(ioc->port_enable_cmds.reply);
5455 kfree(ioc->ctl_cmds.reply);
5456 kfree(ioc->ctl_cmds.sense);
5457 kfree(ioc->pfacts);
5458 ioc->ctl_cmds.reply = NULL;
5459 ioc->base_cmds.reply = NULL;
5460 ioc->tm_cmds.reply = NULL;
5461 ioc->scsih_cmds.reply = NULL;
5462 ioc->transport_cmds.reply = NULL;
5463 ioc->config_cmds.reply = NULL;
5464 ioc->pfacts = NULL;
5465 return r;
5466}
5467
5468
5469/**
5470 * mpt3sas_base_detach - remove controller instance
5471 * @ioc: per adapter object
5472 *
5473 * Return nothing.
5474 */
5475void
5476mpt3sas_base_detach(struct MPT3SAS_ADAPTER *ioc)
5477{
5478 dexitprintk(ioc, pr_info(MPT3SAS_FMT "%s\n", ioc->name,
5479 __func__));
5480
5481 mpt3sas_base_stop_watchdog(ioc);
5482 mpt3sas_base_free_resources(ioc);
5483 _base_release_memory_pools(ioc);
5484 pci_set_drvdata(ioc->pdev, NULL);
5485 kfree(ioc->cpu_msix_table);
Sreekanth Reddy7786ab62015-11-11 17:30:28 +05305486 if (ioc->is_warpdrive)
5487 kfree(ioc->reply_post_host_index);
Sreekanth Reddyf92363d2012-11-30 07:44:21 +05305488 kfree(ioc->pd_handles);
5489 kfree(ioc->blocking_handles);
5490 kfree(ioc->pfacts);
5491 kfree(ioc->ctl_cmds.reply);
5492 kfree(ioc->ctl_cmds.sense);
5493 kfree(ioc->base_cmds.reply);
5494 kfree(ioc->port_enable_cmds.reply);
5495 kfree(ioc->tm_cmds.reply);
5496 kfree(ioc->transport_cmds.reply);
5497 kfree(ioc->scsih_cmds.reply);
5498 kfree(ioc->config_cmds.reply);
5499}
5500
5501/**
5502 * _base_reset_handler - reset callback handler (for base)
5503 * @ioc: per adapter object
5504 * @reset_phase: phase
5505 *
5506 * The handler for doing any required cleanup or initialization.
5507 *
5508 * The reset phase can be MPT3_IOC_PRE_RESET, MPT3_IOC_AFTER_RESET,
5509 * MPT3_IOC_DONE_RESET
5510 *
5511 * Return nothing.
5512 */
5513static void
5514_base_reset_handler(struct MPT3SAS_ADAPTER *ioc, int reset_phase)
5515{
5516 mpt3sas_scsih_reset_handler(ioc, reset_phase);
5517 mpt3sas_ctl_reset_handler(ioc, reset_phase);
5518 switch (reset_phase) {
5519 case MPT3_IOC_PRE_RESET:
5520 dtmprintk(ioc, pr_info(MPT3SAS_FMT
5521 "%s: MPT3_IOC_PRE_RESET\n", ioc->name, __func__));
5522 break;
5523 case MPT3_IOC_AFTER_RESET:
5524 dtmprintk(ioc, pr_info(MPT3SAS_FMT
5525 "%s: MPT3_IOC_AFTER_RESET\n", ioc->name, __func__));
5526 if (ioc->transport_cmds.status & MPT3_CMD_PENDING) {
5527 ioc->transport_cmds.status |= MPT3_CMD_RESET;
5528 mpt3sas_base_free_smid(ioc, ioc->transport_cmds.smid);
5529 complete(&ioc->transport_cmds.done);
5530 }
5531 if (ioc->base_cmds.status & MPT3_CMD_PENDING) {
5532 ioc->base_cmds.status |= MPT3_CMD_RESET;
5533 mpt3sas_base_free_smid(ioc, ioc->base_cmds.smid);
5534 complete(&ioc->base_cmds.done);
5535 }
5536 if (ioc->port_enable_cmds.status & MPT3_CMD_PENDING) {
5537 ioc->port_enable_failed = 1;
5538 ioc->port_enable_cmds.status |= MPT3_CMD_RESET;
5539 mpt3sas_base_free_smid(ioc, ioc->port_enable_cmds.smid);
5540 if (ioc->is_driver_loading) {
5541 ioc->start_scan_failed =
5542 MPI2_IOCSTATUS_INTERNAL_ERROR;
5543 ioc->start_scan = 0;
5544 ioc->port_enable_cmds.status =
5545 MPT3_CMD_NOT_USED;
5546 } else
5547 complete(&ioc->port_enable_cmds.done);
5548 }
5549 if (ioc->config_cmds.status & MPT3_CMD_PENDING) {
5550 ioc->config_cmds.status |= MPT3_CMD_RESET;
5551 mpt3sas_base_free_smid(ioc, ioc->config_cmds.smid);
5552 ioc->config_cmds.smid = USHRT_MAX;
5553 complete(&ioc->config_cmds.done);
5554 }
5555 break;
5556 case MPT3_IOC_DONE_RESET:
5557 dtmprintk(ioc, pr_info(MPT3SAS_FMT
5558 "%s: MPT3_IOC_DONE_RESET\n", ioc->name, __func__));
5559 break;
5560 }
5561}
5562
5563/**
5564 * _wait_for_commands_to_complete - reset controller
5565 * @ioc: Pointer to MPT_ADAPTER structure
5566 * @sleep_flag: CAN_SLEEP or NO_SLEEP
5567 *
5568 * This function waiting(3s) for all pending commands to complete
5569 * prior to putting controller in reset.
5570 */
5571static void
5572_wait_for_commands_to_complete(struct MPT3SAS_ADAPTER *ioc, int sleep_flag)
5573{
5574 u32 ioc_state;
5575 unsigned long flags;
5576 u16 i;
5577
5578 ioc->pending_io_count = 0;
5579 if (sleep_flag != CAN_SLEEP)
5580 return;
5581
5582 ioc_state = mpt3sas_base_get_iocstate(ioc, 0);
5583 if ((ioc_state & MPI2_IOC_STATE_MASK) != MPI2_IOC_STATE_OPERATIONAL)
5584 return;
5585
5586 /* pending command count */
5587 spin_lock_irqsave(&ioc->scsi_lookup_lock, flags);
5588 for (i = 0; i < ioc->scsiio_depth; i++)
5589 if (ioc->scsi_lookup[i].cb_idx != 0xFF)
5590 ioc->pending_io_count++;
5591 spin_unlock_irqrestore(&ioc->scsi_lookup_lock, flags);
5592
5593 if (!ioc->pending_io_count)
5594 return;
5595
5596 /* wait for pending commands to complete */
5597 wait_event_timeout(ioc->reset_wq, ioc->pending_io_count == 0, 10 * HZ);
5598}
5599
5600/**
5601 * mpt3sas_base_hard_reset_handler - reset controller
5602 * @ioc: Pointer to MPT_ADAPTER structure
5603 * @sleep_flag: CAN_SLEEP or NO_SLEEP
5604 * @type: FORCE_BIG_HAMMER or SOFT_RESET
5605 *
5606 * Returns 0 for success, non-zero for failure.
5607 */
5608int
5609mpt3sas_base_hard_reset_handler(struct MPT3SAS_ADAPTER *ioc, int sleep_flag,
5610 enum reset_type type)
5611{
5612 int r;
5613 unsigned long flags;
5614 u32 ioc_state;
5615 u8 is_fault = 0, is_trigger = 0;
5616
5617 dtmprintk(ioc, pr_info(MPT3SAS_FMT "%s: enter\n", ioc->name,
5618 __func__));
5619
5620 if (ioc->pci_error_recovery) {
5621 pr_err(MPT3SAS_FMT "%s: pci error recovery reset\n",
5622 ioc->name, __func__);
5623 r = 0;
5624 goto out_unlocked;
5625 }
5626
5627 if (mpt3sas_fwfault_debug)
5628 mpt3sas_halt_firmware(ioc);
5629
5630 /* TODO - What we really should be doing is pulling
5631 * out all the code associated with NO_SLEEP; its never used.
5632 * That is legacy code from mpt fusion driver, ported over.
5633 * I will leave this BUG_ON here for now till its been resolved.
5634 */
5635 BUG_ON(sleep_flag == NO_SLEEP);
5636
5637 /* wait for an active reset in progress to complete */
5638 if (!mutex_trylock(&ioc->reset_in_progress_mutex)) {
5639 do {
5640 ssleep(1);
5641 } while (ioc->shost_recovery == 1);
5642 dtmprintk(ioc, pr_info(MPT3SAS_FMT "%s: exit\n", ioc->name,
5643 __func__));
5644 return ioc->ioc_reset_in_progress_status;
5645 }
5646
5647 spin_lock_irqsave(&ioc->ioc_reset_in_progress_lock, flags);
5648 ioc->shost_recovery = 1;
5649 spin_unlock_irqrestore(&ioc->ioc_reset_in_progress_lock, flags);
5650
5651 if ((ioc->diag_buffer_status[MPI2_DIAG_BUF_TYPE_TRACE] &
5652 MPT3_DIAG_BUFFER_IS_REGISTERED) &&
5653 (!(ioc->diag_buffer_status[MPI2_DIAG_BUF_TYPE_TRACE] &
5654 MPT3_DIAG_BUFFER_IS_RELEASED))) {
5655 is_trigger = 1;
5656 ioc_state = mpt3sas_base_get_iocstate(ioc, 0);
5657 if ((ioc_state & MPI2_IOC_STATE_MASK) == MPI2_IOC_STATE_FAULT)
5658 is_fault = 1;
5659 }
5660 _base_reset_handler(ioc, MPT3_IOC_PRE_RESET);
5661 _wait_for_commands_to_complete(ioc, sleep_flag);
5662 _base_mask_interrupts(ioc);
5663 r = _base_make_ioc_ready(ioc, sleep_flag, type);
5664 if (r)
5665 goto out;
5666 _base_reset_handler(ioc, MPT3_IOC_AFTER_RESET);
5667
5668 /* If this hard reset is called while port enable is active, then
5669 * there is no reason to call make_ioc_operational
5670 */
5671 if (ioc->is_driver_loading && ioc->port_enable_failed) {
5672 ioc->remove_host = 1;
5673 r = -EFAULT;
5674 goto out;
5675 }
5676 r = _base_get_ioc_facts(ioc, CAN_SLEEP);
5677 if (r)
5678 goto out;
Sreekanth Reddy9b05c912014-09-12 15:35:31 +05305679
5680 if (ioc->rdpq_array_enable && !ioc->rdpq_array_capable)
5681 panic("%s: Issue occurred with flashing controller firmware."
5682 "Please reboot the system and ensure that the correct"
5683 " firmware version is running\n", ioc->name);
5684
Sreekanth Reddyf92363d2012-11-30 07:44:21 +05305685 r = _base_make_ioc_operational(ioc, sleep_flag);
5686 if (!r)
5687 _base_reset_handler(ioc, MPT3_IOC_DONE_RESET);
5688
5689 out:
5690 dtmprintk(ioc, pr_info(MPT3SAS_FMT "%s: %s\n",
5691 ioc->name, __func__, ((r == 0) ? "SUCCESS" : "FAILED")));
5692
5693 spin_lock_irqsave(&ioc->ioc_reset_in_progress_lock, flags);
5694 ioc->ioc_reset_in_progress_status = r;
5695 ioc->shost_recovery = 0;
5696 spin_unlock_irqrestore(&ioc->ioc_reset_in_progress_lock, flags);
5697 ioc->ioc_reset_count++;
5698 mutex_unlock(&ioc->reset_in_progress_mutex);
5699
5700 out_unlocked:
5701 if ((r == 0) && is_trigger) {
5702 if (is_fault)
5703 mpt3sas_trigger_master(ioc, MASTER_TRIGGER_FW_FAULT);
5704 else
5705 mpt3sas_trigger_master(ioc,
5706 MASTER_TRIGGER_ADAPTER_RESET);
5707 }
5708 dtmprintk(ioc, pr_info(MPT3SAS_FMT "%s: exit\n", ioc->name,
5709 __func__));
5710 return r;
5711}