blob: 549b50031d7ebca01bbbac5931f37613a13448b5 [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
Sreekanth Reddy9c500062013-08-14 18:23:20 +053086static int max_msix_vectors = 8;
87module_param(max_msix_vectors, int, 0);
88MODULE_PARM_DESC(max_msix_vectors,
89 " max msix vectors - (default=8)");
Sreekanth Reddyf92363d2012-11-30 07:44:21 +053090
91static int mpt3sas_fwfault_debug;
92MODULE_PARM_DESC(mpt3sas_fwfault_debug,
93 " enable detection of firmware fault and halt firmware - (default=0)");
94
Sreekanth Reddy9b05c912014-09-12 15:35:31 +053095static int
96_base_get_ioc_facts(struct MPT3SAS_ADAPTER *ioc, int sleep_flag);
Sreekanth Reddyf92363d2012-11-30 07:44:21 +053097
98/**
99 * _scsih_set_fwfault_debug - global setting of ioc->fwfault_debug.
100 *
101 */
102static int
103_scsih_set_fwfault_debug(const char *val, struct kernel_param *kp)
104{
105 int ret = param_set_int(val, kp);
106 struct MPT3SAS_ADAPTER *ioc;
107
108 if (ret)
109 return ret;
110
111 pr_info("setting fwfault_debug(%d)\n", mpt3sas_fwfault_debug);
112 list_for_each_entry(ioc, &mpt3sas_ioc_list, list)
113 ioc->fwfault_debug = mpt3sas_fwfault_debug;
114 return 0;
115}
116module_param_call(mpt3sas_fwfault_debug, _scsih_set_fwfault_debug,
117 param_get_int, &mpt3sas_fwfault_debug, 0644);
118
119/**
120 * mpt3sas_remove_dead_ioc_func - kthread context to remove dead ioc
121 * @arg: input argument, used to derive ioc
122 *
123 * Return 0 if controller is removed from pci subsystem.
124 * Return -1 for other case.
125 */
126static int mpt3sas_remove_dead_ioc_func(void *arg)
127{
128 struct MPT3SAS_ADAPTER *ioc = (struct MPT3SAS_ADAPTER *)arg;
129 struct pci_dev *pdev;
130
131 if ((ioc == NULL))
132 return -1;
133
134 pdev = ioc->pdev;
135 if ((pdev == NULL))
136 return -1;
Rafael J. Wysocki64cdb412014-01-10 15:27:56 +0100137 pci_stop_and_remove_bus_device_locked(pdev);
Sreekanth Reddyf92363d2012-11-30 07:44:21 +0530138 return 0;
139}
140
141/**
142 * _base_fault_reset_work - workq handling ioc fault conditions
143 * @work: input argument, used to derive ioc
144 * Context: sleep.
145 *
146 * Return nothing.
147 */
148static void
149_base_fault_reset_work(struct work_struct *work)
150{
151 struct MPT3SAS_ADAPTER *ioc =
152 container_of(work, struct MPT3SAS_ADAPTER, fault_reset_work.work);
153 unsigned long flags;
154 u32 doorbell;
155 int rc;
156 struct task_struct *p;
157
158
159 spin_lock_irqsave(&ioc->ioc_reset_in_progress_lock, flags);
160 if (ioc->shost_recovery)
161 goto rearm_timer;
162 spin_unlock_irqrestore(&ioc->ioc_reset_in_progress_lock, flags);
163
164 doorbell = mpt3sas_base_get_iocstate(ioc, 0);
165 if ((doorbell & MPI2_IOC_STATE_MASK) == MPI2_IOC_STATE_MASK) {
166 pr_err(MPT3SAS_FMT "SAS host is non-operational !!!!\n",
167 ioc->name);
168
169 /*
170 * Call _scsih_flush_pending_cmds callback so that we flush all
171 * pending commands back to OS. This call is required to aovid
172 * deadlock at block layer. Dead IOC will fail to do diag reset,
173 * and this call is safe since dead ioc will never return any
174 * command back from HW.
175 */
176 ioc->schedule_dead_ioc_flush_running_cmds(ioc);
177 /*
178 * Set remove_host flag early since kernel thread will
179 * take some time to execute.
180 */
181 ioc->remove_host = 1;
182 /*Remove the Dead Host */
183 p = kthread_run(mpt3sas_remove_dead_ioc_func, ioc,
184 "mpt3sas_dead_ioc_%d", ioc->id);
185 if (IS_ERR(p))
186 pr_err(MPT3SAS_FMT
187 "%s: Running mpt3sas_dead_ioc thread failed !!!!\n",
188 ioc->name, __func__);
189 else
190 pr_err(MPT3SAS_FMT
191 "%s: Running mpt3sas_dead_ioc thread success !!!!\n",
192 ioc->name, __func__);
193 return; /* don't rearm timer */
194 }
195
196 if ((doorbell & MPI2_IOC_STATE_MASK) != MPI2_IOC_STATE_OPERATIONAL) {
197 rc = mpt3sas_base_hard_reset_handler(ioc, CAN_SLEEP,
198 FORCE_BIG_HAMMER);
199 pr_warn(MPT3SAS_FMT "%s: hard reset: %s\n", ioc->name,
200 __func__, (rc == 0) ? "success" : "failed");
201 doorbell = mpt3sas_base_get_iocstate(ioc, 0);
202 if ((doorbell & MPI2_IOC_STATE_MASK) == MPI2_IOC_STATE_FAULT)
203 mpt3sas_base_fault_info(ioc, doorbell &
204 MPI2_DOORBELL_DATA_MASK);
205 if (rc && (doorbell & MPI2_IOC_STATE_MASK) !=
206 MPI2_IOC_STATE_OPERATIONAL)
207 return; /* don't rearm timer */
208 }
209
210 spin_lock_irqsave(&ioc->ioc_reset_in_progress_lock, flags);
211 rearm_timer:
212 if (ioc->fault_reset_work_q)
213 queue_delayed_work(ioc->fault_reset_work_q,
214 &ioc->fault_reset_work,
215 msecs_to_jiffies(FAULT_POLLING_INTERVAL));
216 spin_unlock_irqrestore(&ioc->ioc_reset_in_progress_lock, flags);
217}
218
219/**
220 * mpt3sas_base_start_watchdog - start the fault_reset_work_q
221 * @ioc: per adapter object
222 * Context: sleep.
223 *
224 * Return nothing.
225 */
226void
227mpt3sas_base_start_watchdog(struct MPT3SAS_ADAPTER *ioc)
228{
229 unsigned long flags;
230
231 if (ioc->fault_reset_work_q)
232 return;
233
234 /* initialize fault polling */
235
236 INIT_DELAYED_WORK(&ioc->fault_reset_work, _base_fault_reset_work);
237 snprintf(ioc->fault_reset_work_q_name,
238 sizeof(ioc->fault_reset_work_q_name), "poll_%d_status", ioc->id);
239 ioc->fault_reset_work_q =
240 create_singlethread_workqueue(ioc->fault_reset_work_q_name);
241 if (!ioc->fault_reset_work_q) {
242 pr_err(MPT3SAS_FMT "%s: failed (line=%d)\n",
243 ioc->name, __func__, __LINE__);
244 return;
245 }
246 spin_lock_irqsave(&ioc->ioc_reset_in_progress_lock, flags);
247 if (ioc->fault_reset_work_q)
248 queue_delayed_work(ioc->fault_reset_work_q,
249 &ioc->fault_reset_work,
250 msecs_to_jiffies(FAULT_POLLING_INTERVAL));
251 spin_unlock_irqrestore(&ioc->ioc_reset_in_progress_lock, flags);
252}
253
254/**
255 * mpt3sas_base_stop_watchdog - stop the fault_reset_work_q
256 * @ioc: per adapter object
257 * Context: sleep.
258 *
259 * Return nothing.
260 */
261void
262mpt3sas_base_stop_watchdog(struct MPT3SAS_ADAPTER *ioc)
263{
264 unsigned long flags;
265 struct workqueue_struct *wq;
266
267 spin_lock_irqsave(&ioc->ioc_reset_in_progress_lock, flags);
268 wq = ioc->fault_reset_work_q;
269 ioc->fault_reset_work_q = NULL;
270 spin_unlock_irqrestore(&ioc->ioc_reset_in_progress_lock, flags);
271 if (wq) {
Reddy, Sreekanth4dc06fd2014-07-14 12:01:35 +0530272 if (!cancel_delayed_work_sync(&ioc->fault_reset_work))
Sreekanth Reddyf92363d2012-11-30 07:44:21 +0530273 flush_workqueue(wq);
274 destroy_workqueue(wq);
275 }
276}
277
278/**
279 * mpt3sas_base_fault_info - verbose translation of firmware FAULT code
280 * @ioc: per adapter object
281 * @fault_code: fault code
282 *
283 * Return nothing.
284 */
285void
286mpt3sas_base_fault_info(struct MPT3SAS_ADAPTER *ioc , u16 fault_code)
287{
288 pr_err(MPT3SAS_FMT "fault_state(0x%04x)!\n",
289 ioc->name, fault_code);
290}
291
292/**
293 * mpt3sas_halt_firmware - halt's mpt controller firmware
294 * @ioc: per adapter object
295 *
296 * For debugging timeout related issues. Writing 0xCOFFEE00
297 * to the doorbell register will halt controller firmware. With
298 * the purpose to stop both driver and firmware, the enduser can
299 * obtain a ring buffer from controller UART.
300 */
301void
302mpt3sas_halt_firmware(struct MPT3SAS_ADAPTER *ioc)
303{
304 u32 doorbell;
305
306 if (!ioc->fwfault_debug)
307 return;
308
309 dump_stack();
310
311 doorbell = readl(&ioc->chip->Doorbell);
312 if ((doorbell & MPI2_IOC_STATE_MASK) == MPI2_IOC_STATE_FAULT)
313 mpt3sas_base_fault_info(ioc , doorbell);
314 else {
315 writel(0xC0FFEE00, &ioc->chip->Doorbell);
316 pr_err(MPT3SAS_FMT "Firmware is halted due to command timeout\n",
317 ioc->name);
318 }
319
320 if (ioc->fwfault_debug == 2)
321 for (;;)
322 ;
323 else
324 panic("panic in %s\n", __func__);
325}
326
327#ifdef CONFIG_SCSI_MPT3SAS_LOGGING
328/**
329 * _base_sas_ioc_info - verbose translation of the ioc status
330 * @ioc: per adapter object
331 * @mpi_reply: reply mf payload returned from firmware
332 * @request_hdr: request mf
333 *
334 * Return nothing.
335 */
336static void
337_base_sas_ioc_info(struct MPT3SAS_ADAPTER *ioc, MPI2DefaultReply_t *mpi_reply,
338 MPI2RequestHeader_t *request_hdr)
339{
340 u16 ioc_status = le16_to_cpu(mpi_reply->IOCStatus) &
341 MPI2_IOCSTATUS_MASK;
342 char *desc = NULL;
343 u16 frame_sz;
344 char *func_str = NULL;
345
346 /* SCSI_IO, RAID_PASS are handled from _scsih_scsi_ioc_info */
347 if (request_hdr->Function == MPI2_FUNCTION_SCSI_IO_REQUEST ||
348 request_hdr->Function == MPI2_FUNCTION_RAID_SCSI_IO_PASSTHROUGH ||
349 request_hdr->Function == MPI2_FUNCTION_EVENT_NOTIFICATION)
350 return;
351
352 if (ioc_status == MPI2_IOCSTATUS_CONFIG_INVALID_PAGE)
353 return;
354
355 switch (ioc_status) {
356
357/****************************************************************************
358* Common IOCStatus values for all replies
359****************************************************************************/
360
361 case MPI2_IOCSTATUS_INVALID_FUNCTION:
362 desc = "invalid function";
363 break;
364 case MPI2_IOCSTATUS_BUSY:
365 desc = "busy";
366 break;
367 case MPI2_IOCSTATUS_INVALID_SGL:
368 desc = "invalid sgl";
369 break;
370 case MPI2_IOCSTATUS_INTERNAL_ERROR:
371 desc = "internal error";
372 break;
373 case MPI2_IOCSTATUS_INVALID_VPID:
374 desc = "invalid vpid";
375 break;
376 case MPI2_IOCSTATUS_INSUFFICIENT_RESOURCES:
377 desc = "insufficient resources";
378 break;
379 case MPI2_IOCSTATUS_INVALID_FIELD:
380 desc = "invalid field";
381 break;
382 case MPI2_IOCSTATUS_INVALID_STATE:
383 desc = "invalid state";
384 break;
385 case MPI2_IOCSTATUS_OP_STATE_NOT_SUPPORTED:
386 desc = "op state not supported";
387 break;
388
389/****************************************************************************
390* Config IOCStatus values
391****************************************************************************/
392
393 case MPI2_IOCSTATUS_CONFIG_INVALID_ACTION:
394 desc = "config invalid action";
395 break;
396 case MPI2_IOCSTATUS_CONFIG_INVALID_TYPE:
397 desc = "config invalid type";
398 break;
399 case MPI2_IOCSTATUS_CONFIG_INVALID_PAGE:
400 desc = "config invalid page";
401 break;
402 case MPI2_IOCSTATUS_CONFIG_INVALID_DATA:
403 desc = "config invalid data";
404 break;
405 case MPI2_IOCSTATUS_CONFIG_NO_DEFAULTS:
406 desc = "config no defaults";
407 break;
408 case MPI2_IOCSTATUS_CONFIG_CANT_COMMIT:
409 desc = "config cant commit";
410 break;
411
412/****************************************************************************
413* SCSI IO Reply
414****************************************************************************/
415
416 case MPI2_IOCSTATUS_SCSI_RECOVERED_ERROR:
417 case MPI2_IOCSTATUS_SCSI_INVALID_DEVHANDLE:
418 case MPI2_IOCSTATUS_SCSI_DEVICE_NOT_THERE:
419 case MPI2_IOCSTATUS_SCSI_DATA_OVERRUN:
420 case MPI2_IOCSTATUS_SCSI_DATA_UNDERRUN:
421 case MPI2_IOCSTATUS_SCSI_IO_DATA_ERROR:
422 case MPI2_IOCSTATUS_SCSI_PROTOCOL_ERROR:
423 case MPI2_IOCSTATUS_SCSI_TASK_TERMINATED:
424 case MPI2_IOCSTATUS_SCSI_RESIDUAL_MISMATCH:
425 case MPI2_IOCSTATUS_SCSI_TASK_MGMT_FAILED:
426 case MPI2_IOCSTATUS_SCSI_IOC_TERMINATED:
427 case MPI2_IOCSTATUS_SCSI_EXT_TERMINATED:
428 break;
429
430/****************************************************************************
431* For use by SCSI Initiator and SCSI Target end-to-end data protection
432****************************************************************************/
433
434 case MPI2_IOCSTATUS_EEDP_GUARD_ERROR:
435 desc = "eedp guard error";
436 break;
437 case MPI2_IOCSTATUS_EEDP_REF_TAG_ERROR:
438 desc = "eedp ref tag error";
439 break;
440 case MPI2_IOCSTATUS_EEDP_APP_TAG_ERROR:
441 desc = "eedp app tag error";
442 break;
443
444/****************************************************************************
445* SCSI Target values
446****************************************************************************/
447
448 case MPI2_IOCSTATUS_TARGET_INVALID_IO_INDEX:
449 desc = "target invalid io index";
450 break;
451 case MPI2_IOCSTATUS_TARGET_ABORTED:
452 desc = "target aborted";
453 break;
454 case MPI2_IOCSTATUS_TARGET_NO_CONN_RETRYABLE:
455 desc = "target no conn retryable";
456 break;
457 case MPI2_IOCSTATUS_TARGET_NO_CONNECTION:
458 desc = "target no connection";
459 break;
460 case MPI2_IOCSTATUS_TARGET_XFER_COUNT_MISMATCH:
461 desc = "target xfer count mismatch";
462 break;
463 case MPI2_IOCSTATUS_TARGET_DATA_OFFSET_ERROR:
464 desc = "target data offset error";
465 break;
466 case MPI2_IOCSTATUS_TARGET_TOO_MUCH_WRITE_DATA:
467 desc = "target too much write data";
468 break;
469 case MPI2_IOCSTATUS_TARGET_IU_TOO_SHORT:
470 desc = "target iu too short";
471 break;
472 case MPI2_IOCSTATUS_TARGET_ACK_NAK_TIMEOUT:
473 desc = "target ack nak timeout";
474 break;
475 case MPI2_IOCSTATUS_TARGET_NAK_RECEIVED:
476 desc = "target nak received";
477 break;
478
479/****************************************************************************
480* Serial Attached SCSI values
481****************************************************************************/
482
483 case MPI2_IOCSTATUS_SAS_SMP_REQUEST_FAILED:
484 desc = "smp request failed";
485 break;
486 case MPI2_IOCSTATUS_SAS_SMP_DATA_OVERRUN:
487 desc = "smp data overrun";
488 break;
489
490/****************************************************************************
491* Diagnostic Buffer Post / Diagnostic Release values
492****************************************************************************/
493
494 case MPI2_IOCSTATUS_DIAGNOSTIC_RELEASED:
495 desc = "diagnostic released";
496 break;
497 default:
498 break;
499 }
500
501 if (!desc)
502 return;
503
504 switch (request_hdr->Function) {
505 case MPI2_FUNCTION_CONFIG:
506 frame_sz = sizeof(Mpi2ConfigRequest_t) + ioc->sge_size;
507 func_str = "config_page";
508 break;
509 case MPI2_FUNCTION_SCSI_TASK_MGMT:
510 frame_sz = sizeof(Mpi2SCSITaskManagementRequest_t);
511 func_str = "task_mgmt";
512 break;
513 case MPI2_FUNCTION_SAS_IO_UNIT_CONTROL:
514 frame_sz = sizeof(Mpi2SasIoUnitControlRequest_t);
515 func_str = "sas_iounit_ctl";
516 break;
517 case MPI2_FUNCTION_SCSI_ENCLOSURE_PROCESSOR:
518 frame_sz = sizeof(Mpi2SepRequest_t);
519 func_str = "enclosure";
520 break;
521 case MPI2_FUNCTION_IOC_INIT:
522 frame_sz = sizeof(Mpi2IOCInitRequest_t);
523 func_str = "ioc_init";
524 break;
525 case MPI2_FUNCTION_PORT_ENABLE:
526 frame_sz = sizeof(Mpi2PortEnableRequest_t);
527 func_str = "port_enable";
528 break;
529 case MPI2_FUNCTION_SMP_PASSTHROUGH:
530 frame_sz = sizeof(Mpi2SmpPassthroughRequest_t) + ioc->sge_size;
531 func_str = "smp_passthru";
532 break;
533 default:
534 frame_sz = 32;
535 func_str = "unknown";
536 break;
537 }
538
539 pr_warn(MPT3SAS_FMT "ioc_status: %s(0x%04x), request(0x%p),(%s)\n",
540 ioc->name, desc, ioc_status, request_hdr, func_str);
541
542 _debug_dump_mf(request_hdr, frame_sz/4);
543}
544
545/**
546 * _base_display_event_data - verbose translation of firmware asyn events
547 * @ioc: per adapter object
548 * @mpi_reply: reply mf payload returned from firmware
549 *
550 * Return nothing.
551 */
552static void
553_base_display_event_data(struct MPT3SAS_ADAPTER *ioc,
554 Mpi2EventNotificationReply_t *mpi_reply)
555{
556 char *desc = NULL;
557 u16 event;
558
559 if (!(ioc->logging_level & MPT_DEBUG_EVENTS))
560 return;
561
562 event = le16_to_cpu(mpi_reply->Event);
563
564 switch (event) {
565 case MPI2_EVENT_LOG_DATA:
566 desc = "Log Data";
567 break;
568 case MPI2_EVENT_STATE_CHANGE:
569 desc = "Status Change";
570 break;
571 case MPI2_EVENT_HARD_RESET_RECEIVED:
572 desc = "Hard Reset Received";
573 break;
574 case MPI2_EVENT_EVENT_CHANGE:
575 desc = "Event Change";
576 break;
577 case MPI2_EVENT_SAS_DEVICE_STATUS_CHANGE:
578 desc = "Device Status Change";
579 break;
580 case MPI2_EVENT_IR_OPERATION_STATUS:
581 desc = "IR Operation Status";
582 break;
583 case MPI2_EVENT_SAS_DISCOVERY:
584 {
585 Mpi2EventDataSasDiscovery_t *event_data =
586 (Mpi2EventDataSasDiscovery_t *)mpi_reply->EventData;
587 pr_info(MPT3SAS_FMT "Discovery: (%s)", ioc->name,
588 (event_data->ReasonCode == MPI2_EVENT_SAS_DISC_RC_STARTED) ?
589 "start" : "stop");
590 if (event_data->DiscoveryStatus)
591 pr_info("discovery_status(0x%08x)",
592 le32_to_cpu(event_data->DiscoveryStatus));
593 pr_info("\n");
594 return;
595 }
596 case MPI2_EVENT_SAS_BROADCAST_PRIMITIVE:
597 desc = "SAS Broadcast Primitive";
598 break;
599 case MPI2_EVENT_SAS_INIT_DEVICE_STATUS_CHANGE:
600 desc = "SAS Init Device Status Change";
601 break;
602 case MPI2_EVENT_SAS_INIT_TABLE_OVERFLOW:
603 desc = "SAS Init Table Overflow";
604 break;
605 case MPI2_EVENT_SAS_TOPOLOGY_CHANGE_LIST:
606 desc = "SAS Topology Change List";
607 break;
608 case MPI2_EVENT_SAS_ENCL_DEVICE_STATUS_CHANGE:
609 desc = "SAS Enclosure Device Status Change";
610 break;
611 case MPI2_EVENT_IR_VOLUME:
612 desc = "IR Volume";
613 break;
614 case MPI2_EVENT_IR_PHYSICAL_DISK:
615 desc = "IR Physical Disk";
616 break;
617 case MPI2_EVENT_IR_CONFIGURATION_CHANGE_LIST:
618 desc = "IR Configuration Change List";
619 break;
620 case MPI2_EVENT_LOG_ENTRY_ADDED:
621 desc = "Log Entry Added";
622 break;
Sreekanth Reddy2d8ce8c2015-01-12 11:38:56 +0530623 case MPI2_EVENT_TEMP_THRESHOLD:
624 desc = "Temperature Threshold";
625 break;
Sreekanth Reddyf92363d2012-11-30 07:44:21 +0530626 }
627
628 if (!desc)
629 return;
630
631 pr_info(MPT3SAS_FMT "%s\n", ioc->name, desc);
632}
633#endif
634
635/**
636 * _base_sas_log_info - verbose translation of firmware log info
637 * @ioc: per adapter object
638 * @log_info: log info
639 *
640 * Return nothing.
641 */
642static void
643_base_sas_log_info(struct MPT3SAS_ADAPTER *ioc , u32 log_info)
644{
645 union loginfo_type {
646 u32 loginfo;
647 struct {
648 u32 subcode:16;
649 u32 code:8;
650 u32 originator:4;
651 u32 bus_type:4;
652 } dw;
653 };
654 union loginfo_type sas_loginfo;
655 char *originator_str = NULL;
656
657 sas_loginfo.loginfo = log_info;
658 if (sas_loginfo.dw.bus_type != 3 /*SAS*/)
659 return;
660
661 /* each nexus loss loginfo */
662 if (log_info == 0x31170000)
663 return;
664
665 /* eat the loginfos associated with task aborts */
666 if (ioc->ignore_loginfos && (log_info == 0x30050000 || log_info ==
667 0x31140000 || log_info == 0x31130000))
668 return;
669
670 switch (sas_loginfo.dw.originator) {
671 case 0:
672 originator_str = "IOP";
673 break;
674 case 1:
675 originator_str = "PL";
676 break;
677 case 2:
678 originator_str = "IR";
679 break;
680 }
681
682 pr_warn(MPT3SAS_FMT
683 "log_info(0x%08x): originator(%s), code(0x%02x), sub_code(0x%04x)\n",
684 ioc->name, log_info,
685 originator_str, sas_loginfo.dw.code,
686 sas_loginfo.dw.subcode);
687}
688
689/**
690 * _base_display_reply_info -
691 * @ioc: per adapter object
692 * @smid: system request message index
693 * @msix_index: MSIX table index supplied by the OS
694 * @reply: reply message frame(lower 32bit addr)
695 *
696 * Return nothing.
697 */
698static void
699_base_display_reply_info(struct MPT3SAS_ADAPTER *ioc, u16 smid, u8 msix_index,
700 u32 reply)
701{
702 MPI2DefaultReply_t *mpi_reply;
703 u16 ioc_status;
704 u32 loginfo = 0;
705
706 mpi_reply = mpt3sas_base_get_reply_virt_addr(ioc, reply);
707 if (unlikely(!mpi_reply)) {
708 pr_err(MPT3SAS_FMT "mpi_reply not valid at %s:%d/%s()!\n",
709 ioc->name, __FILE__, __LINE__, __func__);
710 return;
711 }
712 ioc_status = le16_to_cpu(mpi_reply->IOCStatus);
713#ifdef CONFIG_SCSI_MPT3SAS_LOGGING
714 if ((ioc_status & MPI2_IOCSTATUS_MASK) &&
715 (ioc->logging_level & MPT_DEBUG_REPLY)) {
716 _base_sas_ioc_info(ioc , mpi_reply,
717 mpt3sas_base_get_msg_frame(ioc, smid));
718 }
719#endif
720 if (ioc_status & MPI2_IOCSTATUS_FLAG_LOG_INFO_AVAILABLE) {
721 loginfo = le32_to_cpu(mpi_reply->IOCLogInfo);
722 _base_sas_log_info(ioc, loginfo);
723 }
724
725 if (ioc_status || loginfo) {
726 ioc_status &= MPI2_IOCSTATUS_MASK;
727 mpt3sas_trigger_mpi(ioc, ioc_status, loginfo);
728 }
729}
730
731/**
732 * mpt3sas_base_done - base internal command completion routine
733 * @ioc: per adapter object
734 * @smid: system request message index
735 * @msix_index: MSIX table index supplied by the OS
736 * @reply: reply message frame(lower 32bit addr)
737 *
738 * Return 1 meaning mf should be freed from _base_interrupt
739 * 0 means the mf is freed from this function.
740 */
741u8
742mpt3sas_base_done(struct MPT3SAS_ADAPTER *ioc, u16 smid, u8 msix_index,
743 u32 reply)
744{
745 MPI2DefaultReply_t *mpi_reply;
746
747 mpi_reply = mpt3sas_base_get_reply_virt_addr(ioc, reply);
748 if (mpi_reply && mpi_reply->Function == MPI2_FUNCTION_EVENT_ACK)
749 return 1;
750
751 if (ioc->base_cmds.status == MPT3_CMD_NOT_USED)
752 return 1;
753
754 ioc->base_cmds.status |= MPT3_CMD_COMPLETE;
755 if (mpi_reply) {
756 ioc->base_cmds.status |= MPT3_CMD_REPLY_VALID;
757 memcpy(ioc->base_cmds.reply, mpi_reply, mpi_reply->MsgLength*4);
758 }
759 ioc->base_cmds.status &= ~MPT3_CMD_PENDING;
760
761 complete(&ioc->base_cmds.done);
762 return 1;
763}
764
765/**
766 * _base_async_event - main callback handler for firmware asyn events
767 * @ioc: per adapter object
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 */
774static u8
775_base_async_event(struct MPT3SAS_ADAPTER *ioc, u8 msix_index, u32 reply)
776{
777 Mpi2EventNotificationReply_t *mpi_reply;
778 Mpi2EventAckRequest_t *ack_request;
779 u16 smid;
780
781 mpi_reply = mpt3sas_base_get_reply_virt_addr(ioc, reply);
782 if (!mpi_reply)
783 return 1;
784 if (mpi_reply->Function != MPI2_FUNCTION_EVENT_NOTIFICATION)
785 return 1;
786#ifdef CONFIG_SCSI_MPT3SAS_LOGGING
787 _base_display_event_data(ioc, mpi_reply);
788#endif
789 if (!(mpi_reply->AckRequired & MPI2_EVENT_NOTIFICATION_ACK_REQUIRED))
790 goto out;
791 smid = mpt3sas_base_get_smid(ioc, ioc->base_cb_idx);
792 if (!smid) {
793 pr_err(MPT3SAS_FMT "%s: failed obtaining a smid\n",
794 ioc->name, __func__);
795 goto out;
796 }
797
798 ack_request = mpt3sas_base_get_msg_frame(ioc, smid);
799 memset(ack_request, 0, sizeof(Mpi2EventAckRequest_t));
800 ack_request->Function = MPI2_FUNCTION_EVENT_ACK;
801 ack_request->Event = mpi_reply->Event;
802 ack_request->EventContext = mpi_reply->EventContext;
803 ack_request->VF_ID = 0; /* TODO */
804 ack_request->VP_ID = 0;
805 mpt3sas_base_put_smid_default(ioc, smid);
806
807 out:
808
809 /* scsih callback handler */
810 mpt3sas_scsih_event_callback(ioc, msix_index, reply);
811
812 /* ctl callback handler */
813 mpt3sas_ctl_event_callback(ioc, msix_index, reply);
814
815 return 1;
816}
817
818/**
819 * _base_get_cb_idx - obtain the callback index
820 * @ioc: per adapter object
821 * @smid: system request message index
822 *
823 * Return callback index.
824 */
825static u8
826_base_get_cb_idx(struct MPT3SAS_ADAPTER *ioc, u16 smid)
827{
828 int i;
829 u8 cb_idx;
830
831 if (smid < ioc->hi_priority_smid) {
832 i = smid - 1;
833 cb_idx = ioc->scsi_lookup[i].cb_idx;
834 } else if (smid < ioc->internal_smid) {
835 i = smid - ioc->hi_priority_smid;
836 cb_idx = ioc->hpr_lookup[i].cb_idx;
837 } else if (smid <= ioc->hba_queue_depth) {
838 i = smid - ioc->internal_smid;
839 cb_idx = ioc->internal_lookup[i].cb_idx;
840 } else
841 cb_idx = 0xFF;
842 return cb_idx;
843}
844
845/**
846 * _base_mask_interrupts - disable interrupts
847 * @ioc: per adapter object
848 *
849 * Disabling ResetIRQ, Reply and Doorbell Interrupts
850 *
851 * Return nothing.
852 */
853static void
854_base_mask_interrupts(struct MPT3SAS_ADAPTER *ioc)
855{
856 u32 him_register;
857
858 ioc->mask_interrupts = 1;
859 him_register = readl(&ioc->chip->HostInterruptMask);
860 him_register |= MPI2_HIM_DIM + MPI2_HIM_RIM + MPI2_HIM_RESET_IRQ_MASK;
861 writel(him_register, &ioc->chip->HostInterruptMask);
862 readl(&ioc->chip->HostInterruptMask);
863}
864
865/**
866 * _base_unmask_interrupts - enable interrupts
867 * @ioc: per adapter object
868 *
869 * Enabling only Reply Interrupts
870 *
871 * Return nothing.
872 */
873static void
874_base_unmask_interrupts(struct MPT3SAS_ADAPTER *ioc)
875{
876 u32 him_register;
877
878 him_register = readl(&ioc->chip->HostInterruptMask);
879 him_register &= ~MPI2_HIM_RIM;
880 writel(him_register, &ioc->chip->HostInterruptMask);
881 ioc->mask_interrupts = 0;
882}
883
884union reply_descriptor {
885 u64 word;
886 struct {
887 u32 low;
888 u32 high;
889 } u;
890};
891
892/**
893 * _base_interrupt - MPT adapter (IOC) specific interrupt handler.
894 * @irq: irq number (not used)
895 * @bus_id: bus identifier cookie == pointer to MPT_ADAPTER structure
896 * @r: pt_regs pointer (not used)
897 *
898 * Return IRQ_HANDLE if processed, else IRQ_NONE.
899 */
900static irqreturn_t
901_base_interrupt(int irq, void *bus_id)
902{
903 struct adapter_reply_queue *reply_q = bus_id;
904 union reply_descriptor rd;
905 u32 completed_cmds;
906 u8 request_desript_type;
907 u16 smid;
908 u8 cb_idx;
909 u32 reply;
910 u8 msix_index = reply_q->msix_index;
911 struct MPT3SAS_ADAPTER *ioc = reply_q->ioc;
912 Mpi2ReplyDescriptorsUnion_t *rpf;
913 u8 rc;
914
915 if (ioc->mask_interrupts)
916 return IRQ_NONE;
917
918 if (!atomic_add_unless(&reply_q->busy, 1, 1))
919 return IRQ_NONE;
920
921 rpf = &reply_q->reply_post_free[reply_q->reply_post_host_index];
922 request_desript_type = rpf->Default.ReplyFlags
923 & MPI2_RPY_DESCRIPT_FLAGS_TYPE_MASK;
924 if (request_desript_type == MPI2_RPY_DESCRIPT_FLAGS_UNUSED) {
925 atomic_dec(&reply_q->busy);
926 return IRQ_NONE;
927 }
928
929 completed_cmds = 0;
930 cb_idx = 0xFF;
931 do {
932 rd.word = le64_to_cpu(rpf->Words);
933 if (rd.u.low == UINT_MAX || rd.u.high == UINT_MAX)
934 goto out;
935 reply = 0;
936 smid = le16_to_cpu(rpf->Default.DescriptorTypeDependent1);
937 if (request_desript_type ==
938 MPI25_RPY_DESCRIPT_FLAGS_FAST_PATH_SCSI_IO_SUCCESS ||
939 request_desript_type ==
940 MPI2_RPY_DESCRIPT_FLAGS_SCSI_IO_SUCCESS) {
941 cb_idx = _base_get_cb_idx(ioc, smid);
942 if ((likely(cb_idx < MPT_MAX_CALLBACKS)) &&
943 (likely(mpt_callbacks[cb_idx] != NULL))) {
944 rc = mpt_callbacks[cb_idx](ioc, smid,
945 msix_index, 0);
946 if (rc)
947 mpt3sas_base_free_smid(ioc, smid);
948 }
949 } else if (request_desript_type ==
950 MPI2_RPY_DESCRIPT_FLAGS_ADDRESS_REPLY) {
951 reply = le32_to_cpu(
952 rpf->AddressReply.ReplyFrameAddress);
953 if (reply > ioc->reply_dma_max_address ||
954 reply < ioc->reply_dma_min_address)
955 reply = 0;
956 if (smid) {
957 cb_idx = _base_get_cb_idx(ioc, smid);
958 if ((likely(cb_idx < MPT_MAX_CALLBACKS)) &&
959 (likely(mpt_callbacks[cb_idx] != NULL))) {
960 rc = mpt_callbacks[cb_idx](ioc, smid,
961 msix_index, reply);
962 if (reply)
963 _base_display_reply_info(ioc,
964 smid, msix_index, reply);
965 if (rc)
966 mpt3sas_base_free_smid(ioc,
967 smid);
968 }
969 } else {
970 _base_async_event(ioc, msix_index, reply);
971 }
972
973 /* reply free queue handling */
974 if (reply) {
975 ioc->reply_free_host_index =
976 (ioc->reply_free_host_index ==
977 (ioc->reply_free_queue_depth - 1)) ?
978 0 : ioc->reply_free_host_index + 1;
979 ioc->reply_free[ioc->reply_free_host_index] =
980 cpu_to_le32(reply);
981 wmb();
982 writel(ioc->reply_free_host_index,
983 &ioc->chip->ReplyFreeHostIndex);
984 }
985 }
986
987 rpf->Words = cpu_to_le64(ULLONG_MAX);
988 reply_q->reply_post_host_index =
989 (reply_q->reply_post_host_index ==
990 (ioc->reply_post_queue_depth - 1)) ? 0 :
991 reply_q->reply_post_host_index + 1;
992 request_desript_type =
993 reply_q->reply_post_free[reply_q->reply_post_host_index].
994 Default.ReplyFlags & MPI2_RPY_DESCRIPT_FLAGS_TYPE_MASK;
995 completed_cmds++;
996 if (request_desript_type == MPI2_RPY_DESCRIPT_FLAGS_UNUSED)
997 goto out;
998 if (!reply_q->reply_post_host_index)
999 rpf = reply_q->reply_post_free;
1000 else
1001 rpf++;
1002 } while (1);
1003
1004 out:
1005
1006 if (!completed_cmds) {
1007 atomic_dec(&reply_q->busy);
1008 return IRQ_NONE;
1009 }
1010
1011 wmb();
1012 writel(reply_q->reply_post_host_index | (msix_index <<
1013 MPI2_RPHI_MSIX_INDEX_SHIFT), &ioc->chip->ReplyPostHostIndex);
1014 atomic_dec(&reply_q->busy);
1015 return IRQ_HANDLED;
1016}
1017
1018/**
1019 * _base_is_controller_msix_enabled - is controller support muli-reply queues
1020 * @ioc: per adapter object
1021 *
1022 */
1023static inline int
1024_base_is_controller_msix_enabled(struct MPT3SAS_ADAPTER *ioc)
1025{
1026 return (ioc->facts.IOCCapabilities &
1027 MPI2_IOCFACTS_CAPABILITY_MSI_X_INDEX) && ioc->msix_enable;
1028}
1029
1030/**
1031 * mpt3sas_base_flush_reply_queues - flushing the MSIX reply queues
1032 * @ioc: per adapter object
1033 * Context: ISR conext
1034 *
1035 * Called when a Task Management request has completed. We want
1036 * to flush the other reply queues so all the outstanding IO has been
1037 * completed back to OS before we process the TM completetion.
1038 *
1039 * Return nothing.
1040 */
1041void
1042mpt3sas_base_flush_reply_queues(struct MPT3SAS_ADAPTER *ioc)
1043{
1044 struct adapter_reply_queue *reply_q;
1045
1046 /* If MSIX capability is turned off
1047 * then multi-queues are not enabled
1048 */
1049 if (!_base_is_controller_msix_enabled(ioc))
1050 return;
1051
1052 list_for_each_entry(reply_q, &ioc->reply_queue_list, list) {
1053 if (ioc->shost_recovery)
1054 return;
1055 /* TMs are on msix_index == 0 */
1056 if (reply_q->msix_index == 0)
1057 continue;
1058 _base_interrupt(reply_q->vector, (void *)reply_q);
1059 }
1060}
1061
1062/**
1063 * mpt3sas_base_release_callback_handler - clear interrupt callback handler
1064 * @cb_idx: callback index
1065 *
1066 * Return nothing.
1067 */
1068void
1069mpt3sas_base_release_callback_handler(u8 cb_idx)
1070{
1071 mpt_callbacks[cb_idx] = NULL;
1072}
1073
1074/**
1075 * mpt3sas_base_register_callback_handler - obtain index for the interrupt callback handler
1076 * @cb_func: callback function
1077 *
1078 * Returns cb_func.
1079 */
1080u8
1081mpt3sas_base_register_callback_handler(MPT_CALLBACK cb_func)
1082{
1083 u8 cb_idx;
1084
1085 for (cb_idx = MPT_MAX_CALLBACKS-1; cb_idx; cb_idx--)
1086 if (mpt_callbacks[cb_idx] == NULL)
1087 break;
1088
1089 mpt_callbacks[cb_idx] = cb_func;
1090 return cb_idx;
1091}
1092
1093/**
1094 * mpt3sas_base_initialize_callback_handler - initialize the interrupt callback handler
1095 *
1096 * Return nothing.
1097 */
1098void
1099mpt3sas_base_initialize_callback_handler(void)
1100{
1101 u8 cb_idx;
1102
1103 for (cb_idx = 0; cb_idx < MPT_MAX_CALLBACKS; cb_idx++)
1104 mpt3sas_base_release_callback_handler(cb_idx);
1105}
1106
1107
1108/**
1109 * _base_build_zero_len_sge - build zero length sg entry
1110 * @ioc: per adapter object
1111 * @paddr: virtual address for SGE
1112 *
1113 * Create a zero length scatter gather entry to insure the IOCs hardware has
1114 * something to use if the target device goes brain dead and tries
1115 * to send data even when none is asked for.
1116 *
1117 * Return nothing.
1118 */
1119static void
1120_base_build_zero_len_sge(struct MPT3SAS_ADAPTER *ioc, void *paddr)
1121{
1122 u32 flags_length = (u32)((MPI2_SGE_FLAGS_LAST_ELEMENT |
1123 MPI2_SGE_FLAGS_END_OF_BUFFER | MPI2_SGE_FLAGS_END_OF_LIST |
1124 MPI2_SGE_FLAGS_SIMPLE_ELEMENT) <<
1125 MPI2_SGE_FLAGS_SHIFT);
1126 ioc->base_add_sg_single(paddr, flags_length, -1);
1127}
1128
1129/**
1130 * _base_add_sg_single_32 - Place a simple 32 bit SGE at address pAddr.
1131 * @paddr: virtual address for SGE
1132 * @flags_length: SGE flags and data transfer length
1133 * @dma_addr: Physical address
1134 *
1135 * Return nothing.
1136 */
1137static void
1138_base_add_sg_single_32(void *paddr, u32 flags_length, dma_addr_t dma_addr)
1139{
1140 Mpi2SGESimple32_t *sgel = paddr;
1141
1142 flags_length |= (MPI2_SGE_FLAGS_32_BIT_ADDRESSING |
1143 MPI2_SGE_FLAGS_SYSTEM_ADDRESS) << MPI2_SGE_FLAGS_SHIFT;
1144 sgel->FlagsLength = cpu_to_le32(flags_length);
1145 sgel->Address = cpu_to_le32(dma_addr);
1146}
1147
1148
1149/**
1150 * _base_add_sg_single_64 - Place a simple 64 bit SGE at address pAddr.
1151 * @paddr: virtual address for SGE
1152 * @flags_length: SGE flags and data transfer length
1153 * @dma_addr: Physical address
1154 *
1155 * Return nothing.
1156 */
1157static void
1158_base_add_sg_single_64(void *paddr, u32 flags_length, dma_addr_t dma_addr)
1159{
1160 Mpi2SGESimple64_t *sgel = paddr;
1161
1162 flags_length |= (MPI2_SGE_FLAGS_64_BIT_ADDRESSING |
1163 MPI2_SGE_FLAGS_SYSTEM_ADDRESS) << MPI2_SGE_FLAGS_SHIFT;
1164 sgel->FlagsLength = cpu_to_le32(flags_length);
1165 sgel->Address = cpu_to_le64(dma_addr);
1166}
1167
1168/**
1169 * _base_get_chain_buffer_tracker - obtain chain tracker
1170 * @ioc: per adapter object
1171 * @smid: smid associated to an IO request
1172 *
1173 * Returns chain tracker(from ioc->free_chain_list)
1174 */
1175static struct chain_tracker *
1176_base_get_chain_buffer_tracker(struct MPT3SAS_ADAPTER *ioc, u16 smid)
1177{
1178 struct chain_tracker *chain_req;
1179 unsigned long flags;
1180
1181 spin_lock_irqsave(&ioc->scsi_lookup_lock, flags);
1182 if (list_empty(&ioc->free_chain_list)) {
1183 spin_unlock_irqrestore(&ioc->scsi_lookup_lock, flags);
1184 dfailprintk(ioc, pr_warn(MPT3SAS_FMT
1185 "chain buffers not available\n", ioc->name));
1186 return NULL;
1187 }
1188 chain_req = list_entry(ioc->free_chain_list.next,
1189 struct chain_tracker, tracker_list);
1190 list_del_init(&chain_req->tracker_list);
1191 list_add_tail(&chain_req->tracker_list,
1192 &ioc->scsi_lookup[smid - 1].chain_list);
1193 spin_unlock_irqrestore(&ioc->scsi_lookup_lock, flags);
1194 return chain_req;
1195}
1196
1197
1198/**
1199 * _base_build_sg - build generic sg
1200 * @ioc: per adapter object
1201 * @psge: virtual address for SGE
1202 * @data_out_dma: physical address for WRITES
1203 * @data_out_sz: data xfer size for WRITES
1204 * @data_in_dma: physical address for READS
1205 * @data_in_sz: data xfer size for READS
1206 *
1207 * Return nothing.
1208 */
1209static void
1210_base_build_sg(struct MPT3SAS_ADAPTER *ioc, void *psge,
1211 dma_addr_t data_out_dma, size_t data_out_sz, dma_addr_t data_in_dma,
1212 size_t data_in_sz)
1213{
1214 u32 sgl_flags;
1215
1216 if (!data_out_sz && !data_in_sz) {
1217 _base_build_zero_len_sge(ioc, psge);
1218 return;
1219 }
1220
1221 if (data_out_sz && data_in_sz) {
1222 /* WRITE sgel first */
1223 sgl_flags = (MPI2_SGE_FLAGS_SIMPLE_ELEMENT |
1224 MPI2_SGE_FLAGS_END_OF_BUFFER | MPI2_SGE_FLAGS_HOST_TO_IOC);
1225 sgl_flags = sgl_flags << MPI2_SGE_FLAGS_SHIFT;
1226 ioc->base_add_sg_single(psge, sgl_flags |
1227 data_out_sz, data_out_dma);
1228
1229 /* incr sgel */
1230 psge += ioc->sge_size;
1231
1232 /* READ sgel last */
1233 sgl_flags = (MPI2_SGE_FLAGS_SIMPLE_ELEMENT |
1234 MPI2_SGE_FLAGS_LAST_ELEMENT | MPI2_SGE_FLAGS_END_OF_BUFFER |
1235 MPI2_SGE_FLAGS_END_OF_LIST);
1236 sgl_flags = sgl_flags << MPI2_SGE_FLAGS_SHIFT;
1237 ioc->base_add_sg_single(psge, sgl_flags |
1238 data_in_sz, data_in_dma);
1239 } else if (data_out_sz) /* WRITE */ {
1240 sgl_flags = (MPI2_SGE_FLAGS_SIMPLE_ELEMENT |
1241 MPI2_SGE_FLAGS_LAST_ELEMENT | MPI2_SGE_FLAGS_END_OF_BUFFER |
1242 MPI2_SGE_FLAGS_END_OF_LIST | MPI2_SGE_FLAGS_HOST_TO_IOC);
1243 sgl_flags = sgl_flags << MPI2_SGE_FLAGS_SHIFT;
1244 ioc->base_add_sg_single(psge, sgl_flags |
1245 data_out_sz, data_out_dma);
1246 } else if (data_in_sz) /* READ */ {
1247 sgl_flags = (MPI2_SGE_FLAGS_SIMPLE_ELEMENT |
1248 MPI2_SGE_FLAGS_LAST_ELEMENT | MPI2_SGE_FLAGS_END_OF_BUFFER |
1249 MPI2_SGE_FLAGS_END_OF_LIST);
1250 sgl_flags = sgl_flags << MPI2_SGE_FLAGS_SHIFT;
1251 ioc->base_add_sg_single(psge, sgl_flags |
1252 data_in_sz, data_in_dma);
1253 }
1254}
1255
1256/* IEEE format sgls */
1257
1258/**
1259 * _base_add_sg_single_ieee - add sg element for IEEE format
1260 * @paddr: virtual address for SGE
1261 * @flags: SGE flags
1262 * @chain_offset: number of 128 byte elements from start of segment
1263 * @length: data transfer length
1264 * @dma_addr: Physical address
1265 *
1266 * Return nothing.
1267 */
1268static void
1269_base_add_sg_single_ieee(void *paddr, u8 flags, u8 chain_offset, u32 length,
1270 dma_addr_t dma_addr)
1271{
1272 Mpi25IeeeSgeChain64_t *sgel = paddr;
1273
1274 sgel->Flags = flags;
1275 sgel->NextChainOffset = chain_offset;
1276 sgel->Length = cpu_to_le32(length);
1277 sgel->Address = cpu_to_le64(dma_addr);
1278}
1279
1280/**
1281 * _base_build_zero_len_sge_ieee - build zero length sg entry for IEEE format
1282 * @ioc: per adapter object
1283 * @paddr: virtual address for SGE
1284 *
1285 * Create a zero length scatter gather entry to insure the IOCs hardware has
1286 * something to use if the target device goes brain dead and tries
1287 * to send data even when none is asked for.
1288 *
1289 * Return nothing.
1290 */
1291static void
1292_base_build_zero_len_sge_ieee(struct MPT3SAS_ADAPTER *ioc, void *paddr)
1293{
1294 u8 sgl_flags = (MPI2_IEEE_SGE_FLAGS_SIMPLE_ELEMENT |
1295 MPI2_IEEE_SGE_FLAGS_SYSTEM_ADDR |
1296 MPI25_IEEE_SGE_FLAGS_END_OF_LIST);
1297 _base_add_sg_single_ieee(paddr, sgl_flags, 0, 0, -1);
1298}
1299
1300/**
1301 * _base_build_sg_scmd_ieee - main sg creation routine for IEEE format
1302 * @ioc: per adapter object
1303 * @scmd: scsi command
1304 * @smid: system request message index
1305 * Context: none.
1306 *
1307 * The main routine that builds scatter gather table from a given
1308 * scsi request sent via the .queuecommand main handler.
1309 *
1310 * Returns 0 success, anything else error
1311 */
1312static int
1313_base_build_sg_scmd_ieee(struct MPT3SAS_ADAPTER *ioc,
1314 struct scsi_cmnd *scmd, u16 smid)
1315{
1316 Mpi2SCSIIORequest_t *mpi_request;
1317 dma_addr_t chain_dma;
1318 struct scatterlist *sg_scmd;
1319 void *sg_local, *chain;
1320 u32 chain_offset;
1321 u32 chain_length;
Sreekanth Reddyf92363d2012-11-30 07:44:21 +05301322 int sges_left;
1323 u32 sges_in_segment;
1324 u8 simple_sgl_flags;
1325 u8 simple_sgl_flags_last;
1326 u8 chain_sgl_flags;
1327 struct chain_tracker *chain_req;
1328
1329 mpi_request = mpt3sas_base_get_msg_frame(ioc, smid);
1330
1331 /* init scatter gather flags */
1332 simple_sgl_flags = MPI2_IEEE_SGE_FLAGS_SIMPLE_ELEMENT |
1333 MPI2_IEEE_SGE_FLAGS_SYSTEM_ADDR;
1334 simple_sgl_flags_last = simple_sgl_flags |
1335 MPI25_IEEE_SGE_FLAGS_END_OF_LIST;
1336 chain_sgl_flags = MPI2_IEEE_SGE_FLAGS_CHAIN_ELEMENT |
1337 MPI2_IEEE_SGE_FLAGS_SYSTEM_ADDR;
1338
1339 sg_scmd = scsi_sglist(scmd);
1340 sges_left = scsi_dma_map(scmd);
1341 if (!sges_left) {
1342 sdev_printk(KERN_ERR, scmd->device,
1343 "pci_map_sg failed: request for %d bytes!\n",
1344 scsi_bufflen(scmd));
1345 return -ENOMEM;
1346 }
1347
1348 sg_local = &mpi_request->SGL;
1349 sges_in_segment = (ioc->request_sz -
1350 offsetof(Mpi2SCSIIORequest_t, SGL))/ioc->sge_size_ieee;
1351 if (sges_left <= sges_in_segment)
1352 goto fill_in_last_segment;
1353
1354 mpi_request->ChainOffset = (sges_in_segment - 1 /* chain element */) +
1355 (offsetof(Mpi2SCSIIORequest_t, SGL)/ioc->sge_size_ieee);
1356
1357 /* fill in main message segment when there is a chain following */
1358 while (sges_in_segment > 1) {
1359 _base_add_sg_single_ieee(sg_local, simple_sgl_flags, 0,
1360 sg_dma_len(sg_scmd), sg_dma_address(sg_scmd));
1361 sg_scmd = sg_next(sg_scmd);
1362 sg_local += ioc->sge_size_ieee;
1363 sges_left--;
1364 sges_in_segment--;
1365 }
1366
Wei Yongjun25ef16d2012-12-12 02:26:51 +05301367 /* initializing the pointers */
Sreekanth Reddyf92363d2012-11-30 07:44:21 +05301368 chain_req = _base_get_chain_buffer_tracker(ioc, smid);
1369 if (!chain_req)
1370 return -1;
1371 chain = chain_req->chain_buffer;
1372 chain_dma = chain_req->chain_buffer_dma;
1373 do {
1374 sges_in_segment = (sges_left <=
1375 ioc->max_sges_in_chain_message) ? sges_left :
1376 ioc->max_sges_in_chain_message;
1377 chain_offset = (sges_left == sges_in_segment) ?
1378 0 : sges_in_segment;
1379 chain_length = sges_in_segment * ioc->sge_size_ieee;
1380 if (chain_offset)
1381 chain_length += ioc->sge_size_ieee;
1382 _base_add_sg_single_ieee(sg_local, chain_sgl_flags,
1383 chain_offset, chain_length, chain_dma);
1384
1385 sg_local = chain;
1386 if (!chain_offset)
1387 goto fill_in_last_segment;
1388
1389 /* fill in chain segments */
1390 while (sges_in_segment) {
1391 _base_add_sg_single_ieee(sg_local, simple_sgl_flags, 0,
1392 sg_dma_len(sg_scmd), sg_dma_address(sg_scmd));
1393 sg_scmd = sg_next(sg_scmd);
1394 sg_local += ioc->sge_size_ieee;
1395 sges_left--;
1396 sges_in_segment--;
1397 }
1398
1399 chain_req = _base_get_chain_buffer_tracker(ioc, smid);
1400 if (!chain_req)
1401 return -1;
1402 chain = chain_req->chain_buffer;
1403 chain_dma = chain_req->chain_buffer_dma;
1404 } while (1);
1405
1406
1407 fill_in_last_segment:
1408
1409 /* fill the last segment */
1410 while (sges_left) {
1411 if (sges_left == 1)
1412 _base_add_sg_single_ieee(sg_local,
1413 simple_sgl_flags_last, 0, sg_dma_len(sg_scmd),
1414 sg_dma_address(sg_scmd));
1415 else
1416 _base_add_sg_single_ieee(sg_local, simple_sgl_flags, 0,
1417 sg_dma_len(sg_scmd), sg_dma_address(sg_scmd));
1418 sg_scmd = sg_next(sg_scmd);
1419 sg_local += ioc->sge_size_ieee;
1420 sges_left--;
1421 }
1422
1423 return 0;
1424}
1425
1426/**
1427 * _base_build_sg_ieee - build generic sg for IEEE format
1428 * @ioc: per adapter object
1429 * @psge: virtual address for SGE
1430 * @data_out_dma: physical address for WRITES
1431 * @data_out_sz: data xfer size for WRITES
1432 * @data_in_dma: physical address for READS
1433 * @data_in_sz: data xfer size for READS
1434 *
1435 * Return nothing.
1436 */
1437static void
1438_base_build_sg_ieee(struct MPT3SAS_ADAPTER *ioc, void *psge,
1439 dma_addr_t data_out_dma, size_t data_out_sz, dma_addr_t data_in_dma,
1440 size_t data_in_sz)
1441{
1442 u8 sgl_flags;
1443
1444 if (!data_out_sz && !data_in_sz) {
1445 _base_build_zero_len_sge_ieee(ioc, psge);
1446 return;
1447 }
1448
1449 if (data_out_sz && data_in_sz) {
1450 /* WRITE sgel first */
1451 sgl_flags = MPI2_IEEE_SGE_FLAGS_SIMPLE_ELEMENT |
1452 MPI2_IEEE_SGE_FLAGS_SYSTEM_ADDR;
1453 _base_add_sg_single_ieee(psge, sgl_flags, 0, data_out_sz,
1454 data_out_dma);
1455
1456 /* incr sgel */
1457 psge += ioc->sge_size_ieee;
1458
1459 /* READ sgel last */
1460 sgl_flags |= MPI25_IEEE_SGE_FLAGS_END_OF_LIST;
1461 _base_add_sg_single_ieee(psge, sgl_flags, 0, data_in_sz,
1462 data_in_dma);
1463 } else if (data_out_sz) /* WRITE */ {
1464 sgl_flags = MPI2_IEEE_SGE_FLAGS_SIMPLE_ELEMENT |
1465 MPI25_IEEE_SGE_FLAGS_END_OF_LIST |
1466 MPI2_IEEE_SGE_FLAGS_SYSTEM_ADDR;
1467 _base_add_sg_single_ieee(psge, sgl_flags, 0, data_out_sz,
1468 data_out_dma);
1469 } else if (data_in_sz) /* READ */ {
1470 sgl_flags = MPI2_IEEE_SGE_FLAGS_SIMPLE_ELEMENT |
1471 MPI25_IEEE_SGE_FLAGS_END_OF_LIST |
1472 MPI2_IEEE_SGE_FLAGS_SYSTEM_ADDR;
1473 _base_add_sg_single_ieee(psge, sgl_flags, 0, data_in_sz,
1474 data_in_dma);
1475 }
1476}
1477
1478#define convert_to_kb(x) ((x) << (PAGE_SHIFT - 10))
1479
1480/**
1481 * _base_config_dma_addressing - set dma addressing
1482 * @ioc: per adapter object
1483 * @pdev: PCI device struct
1484 *
1485 * Returns 0 for success, non-zero for failure.
1486 */
1487static int
1488_base_config_dma_addressing(struct MPT3SAS_ADAPTER *ioc, struct pci_dev *pdev)
1489{
1490 struct sysinfo s;
Sreekanth Reddy9b05c912014-09-12 15:35:31 +05301491 u64 consistent_dma_mask;
1492
1493 if (ioc->dma_mask)
1494 consistent_dma_mask = DMA_BIT_MASK(64);
1495 else
1496 consistent_dma_mask = DMA_BIT_MASK(32);
Sreekanth Reddyf92363d2012-11-30 07:44:21 +05301497
1498 if (sizeof(dma_addr_t) > 4) {
1499 const uint64_t required_mask =
1500 dma_get_required_mask(&pdev->dev);
1501 if ((required_mask > DMA_BIT_MASK(32)) &&
1502 !pci_set_dma_mask(pdev, DMA_BIT_MASK(64)) &&
Sreekanth Reddy9b05c912014-09-12 15:35:31 +05301503 !pci_set_consistent_dma_mask(pdev, consistent_dma_mask)) {
Sreekanth Reddyf92363d2012-11-30 07:44:21 +05301504 ioc->base_add_sg_single = &_base_add_sg_single_64;
1505 ioc->sge_size = sizeof(Mpi2SGESimple64_t);
Sreekanth Reddy9b05c912014-09-12 15:35:31 +05301506 ioc->dma_mask = 64;
Sreekanth Reddyf92363d2012-11-30 07:44:21 +05301507 goto out;
1508 }
1509 }
1510
1511 if (!pci_set_dma_mask(pdev, DMA_BIT_MASK(32))
1512 && !pci_set_consistent_dma_mask(pdev, DMA_BIT_MASK(32))) {
1513 ioc->base_add_sg_single = &_base_add_sg_single_32;
1514 ioc->sge_size = sizeof(Mpi2SGESimple32_t);
Sreekanth Reddy9b05c912014-09-12 15:35:31 +05301515 ioc->dma_mask = 32;
Sreekanth Reddyf92363d2012-11-30 07:44:21 +05301516 } else
1517 return -ENODEV;
1518
1519 out:
1520 si_meminfo(&s);
1521 pr_info(MPT3SAS_FMT
Sreekanth Reddy9b05c912014-09-12 15:35:31 +05301522 "%d BIT PCI BUS DMA ADDRESSING SUPPORTED, total mem (%ld kB)\n",
1523 ioc->name, ioc->dma_mask, convert_to_kb(s.totalram));
Sreekanth Reddyf92363d2012-11-30 07:44:21 +05301524
1525 return 0;
1526}
1527
Sreekanth Reddy9b05c912014-09-12 15:35:31 +05301528static int
1529_base_change_consistent_dma_mask(struct MPT3SAS_ADAPTER *ioc,
1530 struct pci_dev *pdev)
1531{
1532 if (pci_set_consistent_dma_mask(pdev, DMA_BIT_MASK(64))) {
1533 if (pci_set_consistent_dma_mask(pdev, DMA_BIT_MASK(32)))
1534 return -ENODEV;
1535 }
1536 return 0;
1537}
1538
Sreekanth Reddyf92363d2012-11-30 07:44:21 +05301539/**
1540 * _base_check_enable_msix - checks MSIX capabable.
1541 * @ioc: per adapter object
1542 *
1543 * Check to see if card is capable of MSIX, and set number
1544 * of available msix vectors
1545 */
1546static int
1547_base_check_enable_msix(struct MPT3SAS_ADAPTER *ioc)
1548{
1549 int base;
1550 u16 message_control;
1551
1552 base = pci_find_capability(ioc->pdev, PCI_CAP_ID_MSIX);
1553 if (!base) {
1554 dfailprintk(ioc, pr_info(MPT3SAS_FMT "msix not supported\n",
1555 ioc->name));
1556 return -EINVAL;
1557 }
1558
1559 /* get msix vector count */
1560
1561 pci_read_config_word(ioc->pdev, base + 2, &message_control);
1562 ioc->msix_vector_count = (message_control & 0x3FF) + 1;
1563 if (ioc->msix_vector_count > 8)
1564 ioc->msix_vector_count = 8;
1565 dinitprintk(ioc, pr_info(MPT3SAS_FMT
1566 "msix is supported, vector_count(%d)\n",
1567 ioc->name, ioc->msix_vector_count));
1568 return 0;
1569}
1570
1571/**
1572 * _base_free_irq - free irq
1573 * @ioc: per adapter object
1574 *
1575 * Freeing respective reply_queue from the list.
1576 */
1577static void
1578_base_free_irq(struct MPT3SAS_ADAPTER *ioc)
1579{
1580 struct adapter_reply_queue *reply_q, *next;
1581
1582 if (list_empty(&ioc->reply_queue_list))
1583 return;
1584
1585 list_for_each_entry_safe(reply_q, next, &ioc->reply_queue_list, list) {
1586 list_del(&reply_q->list);
1587 synchronize_irq(reply_q->vector);
1588 free_irq(reply_q->vector, reply_q);
1589 kfree(reply_q);
1590 }
1591}
1592
1593/**
1594 * _base_request_irq - request irq
1595 * @ioc: per adapter object
1596 * @index: msix index into vector table
1597 * @vector: irq vector
1598 *
1599 * Inserting respective reply_queue into the list.
1600 */
1601static int
1602_base_request_irq(struct MPT3SAS_ADAPTER *ioc, u8 index, u32 vector)
1603{
1604 struct adapter_reply_queue *reply_q;
1605 int r;
1606
1607 reply_q = kzalloc(sizeof(struct adapter_reply_queue), GFP_KERNEL);
1608 if (!reply_q) {
1609 pr_err(MPT3SAS_FMT "unable to allocate memory %d!\n",
1610 ioc->name, (int)sizeof(struct adapter_reply_queue));
1611 return -ENOMEM;
1612 }
1613 reply_q->ioc = ioc;
1614 reply_q->msix_index = index;
1615 reply_q->vector = vector;
1616 atomic_set(&reply_q->busy, 0);
1617 if (ioc->msix_enable)
1618 snprintf(reply_q->name, MPT_NAME_LENGTH, "%s%d-msix%d",
1619 MPT3SAS_DRIVER_NAME, ioc->id, index);
1620 else
1621 snprintf(reply_q->name, MPT_NAME_LENGTH, "%s%d",
1622 MPT3SAS_DRIVER_NAME, ioc->id);
1623 r = request_irq(vector, _base_interrupt, IRQF_SHARED, reply_q->name,
1624 reply_q);
1625 if (r) {
1626 pr_err(MPT3SAS_FMT "unable to allocate interrupt %d!\n",
1627 reply_q->name, vector);
1628 kfree(reply_q);
1629 return -EBUSY;
1630 }
1631
1632 INIT_LIST_HEAD(&reply_q->list);
1633 list_add_tail(&reply_q->list, &ioc->reply_queue_list);
1634 return 0;
1635}
1636
1637/**
1638 * _base_assign_reply_queues - assigning msix index for each cpu
1639 * @ioc: per adapter object
1640 *
1641 * The enduser would need to set the affinity via /proc/irq/#/smp_affinity
1642 *
1643 * It would nice if we could call irq_set_affinity, however it is not
1644 * an exported symbol
1645 */
1646static void
1647_base_assign_reply_queues(struct MPT3SAS_ADAPTER *ioc)
1648{
Martin K. Petersen91b265b2014-01-03 19:16:56 -05001649 unsigned int cpu, nr_cpus, nr_msix, index = 0;
Sreekanth Reddyf92363d2012-11-30 07:44:21 +05301650
1651 if (!_base_is_controller_msix_enabled(ioc))
1652 return;
1653
1654 memset(ioc->cpu_msix_table, 0, ioc->cpu_msix_table_sz);
1655
Martin K. Petersen91b265b2014-01-03 19:16:56 -05001656 nr_cpus = num_online_cpus();
1657 nr_msix = ioc->reply_queue_count = min(ioc->reply_queue_count,
1658 ioc->facts.MaxMSIxVectors);
1659 if (!nr_msix)
1660 return;
Sreekanth Reddyf92363d2012-11-30 07:44:21 +05301661
Martin K. Petersen91b265b2014-01-03 19:16:56 -05001662 cpu = cpumask_first(cpu_online_mask);
Sreekanth Reddyf92363d2012-11-30 07:44:21 +05301663
Martin K. Petersen91b265b2014-01-03 19:16:56 -05001664 do {
1665 unsigned int i, group = nr_cpus / nr_msix;
1666
1667 if (index < nr_cpus % nr_msix)
1668 group++;
1669
1670 for (i = 0 ; i < group ; i++) {
1671 ioc->cpu_msix_table[cpu] = index;
1672 cpu = cpumask_next(cpu, cpu_online_mask);
Sreekanth Reddyf92363d2012-11-30 07:44:21 +05301673 }
Martin K. Petersen91b265b2014-01-03 19:16:56 -05001674
1675 index++;
1676
1677 } while (cpu < nr_cpus);
Sreekanth Reddyf92363d2012-11-30 07:44:21 +05301678}
1679
1680/**
1681 * _base_disable_msix - disables msix
1682 * @ioc: per adapter object
1683 *
1684 */
1685static void
1686_base_disable_msix(struct MPT3SAS_ADAPTER *ioc)
1687{
1688 if (!ioc->msix_enable)
1689 return;
1690 pci_disable_msix(ioc->pdev);
1691 ioc->msix_enable = 0;
1692}
1693
1694/**
1695 * _base_enable_msix - enables msix, failback to io_apic
1696 * @ioc: per adapter object
1697 *
1698 */
1699static int
1700_base_enable_msix(struct MPT3SAS_ADAPTER *ioc)
1701{
1702 struct msix_entry *entries, *a;
1703 int r;
1704 int i;
1705 u8 try_msix = 0;
1706
Sreekanth Reddyf92363d2012-11-30 07:44:21 +05301707 if (msix_disable == -1 || msix_disable == 0)
1708 try_msix = 1;
1709
1710 if (!try_msix)
1711 goto try_ioapic;
1712
1713 if (_base_check_enable_msix(ioc) != 0)
1714 goto try_ioapic;
1715
1716 ioc->reply_queue_count = min_t(int, ioc->cpu_count,
1717 ioc->msix_vector_count);
1718
Sreekanth Reddy9c500062013-08-14 18:23:20 +05301719 printk(MPT3SAS_FMT "MSI-X vectors supported: %d, no of cores"
1720 ": %d, max_msix_vectors: %d\n", ioc->name, ioc->msix_vector_count,
1721 ioc->cpu_count, max_msix_vectors);
1722
Sreekanth Reddy9b05c912014-09-12 15:35:31 +05301723 if (!ioc->rdpq_array_enable && max_msix_vectors == -1)
1724 max_msix_vectors = 8;
1725
Sreekanth Reddy9c500062013-08-14 18:23:20 +05301726 if (max_msix_vectors > 0) {
1727 ioc->reply_queue_count = min_t(int, max_msix_vectors,
1728 ioc->reply_queue_count);
1729 ioc->msix_vector_count = ioc->reply_queue_count;
Sreekanth Reddy9b05c912014-09-12 15:35:31 +05301730 } else if (max_msix_vectors == 0)
1731 goto try_ioapic;
Sreekanth Reddy9c500062013-08-14 18:23:20 +05301732
Sreekanth Reddyf92363d2012-11-30 07:44:21 +05301733 entries = kcalloc(ioc->reply_queue_count, sizeof(struct msix_entry),
1734 GFP_KERNEL);
1735 if (!entries) {
1736 dfailprintk(ioc, pr_info(MPT3SAS_FMT
1737 "kcalloc failed @ at %s:%d/%s() !!!\n",
1738 ioc->name, __FILE__, __LINE__, __func__));
1739 goto try_ioapic;
1740 }
1741
1742 for (i = 0, a = entries; i < ioc->reply_queue_count; i++, a++)
1743 a->entry = i;
1744
Alexander Gordeev6bfa6902014-08-18 08:01:46 +02001745 r = pci_enable_msix_exact(ioc->pdev, entries, ioc->reply_queue_count);
Sreekanth Reddyf92363d2012-11-30 07:44:21 +05301746 if (r) {
1747 dfailprintk(ioc, pr_info(MPT3SAS_FMT
Alexander Gordeev6bfa6902014-08-18 08:01:46 +02001748 "pci_enable_msix_exact failed (r=%d) !!!\n",
Sreekanth Reddyf92363d2012-11-30 07:44:21 +05301749 ioc->name, r));
1750 kfree(entries);
1751 goto try_ioapic;
1752 }
1753
1754 ioc->msix_enable = 1;
1755 for (i = 0, a = entries; i < ioc->reply_queue_count; i++, a++) {
1756 r = _base_request_irq(ioc, i, a->vector);
1757 if (r) {
1758 _base_free_irq(ioc);
1759 _base_disable_msix(ioc);
1760 kfree(entries);
1761 goto try_ioapic;
1762 }
1763 }
1764
1765 kfree(entries);
1766 return 0;
1767
1768/* failback to io_apic interrupt routing */
1769 try_ioapic:
1770
Sreekanth Reddy9b05c912014-09-12 15:35:31 +05301771 ioc->reply_queue_count = 1;
Sreekanth Reddyf92363d2012-11-30 07:44:21 +05301772 r = _base_request_irq(ioc, 0, ioc->pdev->irq);
1773
1774 return r;
1775}
1776
1777/**
1778 * mpt3sas_base_map_resources - map in controller resources (io/irq/memap)
1779 * @ioc: per adapter object
1780 *
1781 * Returns 0 for success, non-zero for failure.
1782 */
1783int
1784mpt3sas_base_map_resources(struct MPT3SAS_ADAPTER *ioc)
1785{
1786 struct pci_dev *pdev = ioc->pdev;
1787 u32 memap_sz;
1788 u32 pio_sz;
1789 int i, r = 0;
1790 u64 pio_chip = 0;
1791 u64 chip_phys = 0;
1792 struct adapter_reply_queue *reply_q;
1793
1794 dinitprintk(ioc, pr_info(MPT3SAS_FMT "%s\n",
1795 ioc->name, __func__));
1796
1797 ioc->bars = pci_select_bars(pdev, IORESOURCE_MEM);
1798 if (pci_enable_device_mem(pdev)) {
1799 pr_warn(MPT3SAS_FMT "pci_enable_device_mem: failed\n",
1800 ioc->name);
Joe Lawrencecf9bd21a2013-08-08 16:45:39 -04001801 ioc->bars = 0;
Sreekanth Reddyf92363d2012-11-30 07:44:21 +05301802 return -ENODEV;
1803 }
1804
1805
1806 if (pci_request_selected_regions(pdev, ioc->bars,
1807 MPT3SAS_DRIVER_NAME)) {
1808 pr_warn(MPT3SAS_FMT "pci_request_selected_regions: failed\n",
1809 ioc->name);
Joe Lawrencecf9bd21a2013-08-08 16:45:39 -04001810 ioc->bars = 0;
Sreekanth Reddyf92363d2012-11-30 07:44:21 +05301811 r = -ENODEV;
1812 goto out_fail;
1813 }
1814
1815/* AER (Advanced Error Reporting) hooks */
1816 pci_enable_pcie_error_reporting(pdev);
1817
1818 pci_set_master(pdev);
1819
1820
1821 if (_base_config_dma_addressing(ioc, pdev) != 0) {
1822 pr_warn(MPT3SAS_FMT "no suitable DMA mask for %s\n",
1823 ioc->name, pci_name(pdev));
1824 r = -ENODEV;
1825 goto out_fail;
1826 }
1827
1828 for (i = 0, memap_sz = 0, pio_sz = 0 ; i < DEVICE_COUNT_RESOURCE; i++) {
1829 if (pci_resource_flags(pdev, i) & IORESOURCE_IO) {
1830 if (pio_sz)
1831 continue;
1832 pio_chip = (u64)pci_resource_start(pdev, i);
1833 pio_sz = pci_resource_len(pdev, i);
1834 } else if (pci_resource_flags(pdev, i) & IORESOURCE_MEM) {
1835 if (memap_sz)
1836 continue;
1837 ioc->chip_phys = pci_resource_start(pdev, i);
1838 chip_phys = (u64)ioc->chip_phys;
1839 memap_sz = pci_resource_len(pdev, i);
1840 ioc->chip = ioremap(ioc->chip_phys, memap_sz);
1841 if (ioc->chip == NULL) {
1842 pr_err(MPT3SAS_FMT "unable to map adapter memory!\n",
1843 ioc->name);
1844 r = -EINVAL;
1845 goto out_fail;
1846 }
1847 }
1848 }
1849
1850 _base_mask_interrupts(ioc);
Sreekanth Reddy9b05c912014-09-12 15:35:31 +05301851
1852 r = _base_get_ioc_facts(ioc, CAN_SLEEP);
1853 if (r)
1854 goto out_fail;
1855
1856 if (!ioc->rdpq_array_enable_assigned) {
1857 ioc->rdpq_array_enable = ioc->rdpq_array_capable;
1858 ioc->rdpq_array_enable_assigned = 1;
1859 }
1860
Sreekanth Reddyf92363d2012-11-30 07:44:21 +05301861 r = _base_enable_msix(ioc);
1862 if (r)
1863 goto out_fail;
1864
1865 list_for_each_entry(reply_q, &ioc->reply_queue_list, list)
1866 pr_info(MPT3SAS_FMT "%s: IRQ %d\n",
1867 reply_q->name, ((ioc->msix_enable) ? "PCI-MSI-X enabled" :
1868 "IO-APIC enabled"), reply_q->vector);
1869
1870 pr_info(MPT3SAS_FMT "iomem(0x%016llx), mapped(0x%p), size(%d)\n",
1871 ioc->name, (unsigned long long)chip_phys, ioc->chip, memap_sz);
1872 pr_info(MPT3SAS_FMT "ioport(0x%016llx), size(%d)\n",
1873 ioc->name, (unsigned long long)pio_chip, pio_sz);
1874
1875 /* Save PCI configuration state for recovery from PCI AER/EEH errors */
1876 pci_save_state(pdev);
1877 return 0;
1878
1879 out_fail:
1880 if (ioc->chip_phys)
1881 iounmap(ioc->chip);
1882 ioc->chip_phys = 0;
1883 pci_release_selected_regions(ioc->pdev, ioc->bars);
1884 pci_disable_pcie_error_reporting(pdev);
1885 pci_disable_device(pdev);
1886 return r;
1887}
1888
1889/**
1890 * mpt3sas_base_get_msg_frame - obtain request mf pointer
1891 * @ioc: per adapter object
1892 * @smid: system request message index(smid zero is invalid)
1893 *
1894 * Returns virt pointer to message frame.
1895 */
1896void *
1897mpt3sas_base_get_msg_frame(struct MPT3SAS_ADAPTER *ioc, u16 smid)
1898{
1899 return (void *)(ioc->request + (smid * ioc->request_sz));
1900}
1901
1902/**
1903 * mpt3sas_base_get_sense_buffer - obtain a sense buffer virt addr
1904 * @ioc: per adapter object
1905 * @smid: system request message index
1906 *
1907 * Returns virt pointer to sense buffer.
1908 */
1909void *
1910mpt3sas_base_get_sense_buffer(struct MPT3SAS_ADAPTER *ioc, u16 smid)
1911{
1912 return (void *)(ioc->sense + ((smid - 1) * SCSI_SENSE_BUFFERSIZE));
1913}
1914
1915/**
1916 * mpt3sas_base_get_sense_buffer_dma - obtain a sense buffer dma addr
1917 * @ioc: per adapter object
1918 * @smid: system request message index
1919 *
1920 * Returns phys pointer to the low 32bit address of the sense buffer.
1921 */
1922__le32
1923mpt3sas_base_get_sense_buffer_dma(struct MPT3SAS_ADAPTER *ioc, u16 smid)
1924{
1925 return cpu_to_le32(ioc->sense_dma + ((smid - 1) *
1926 SCSI_SENSE_BUFFERSIZE));
1927}
1928
1929/**
1930 * mpt3sas_base_get_reply_virt_addr - obtain reply frames virt address
1931 * @ioc: per adapter object
1932 * @phys_addr: lower 32 physical addr of the reply
1933 *
1934 * Converts 32bit lower physical addr into a virt address.
1935 */
1936void *
1937mpt3sas_base_get_reply_virt_addr(struct MPT3SAS_ADAPTER *ioc, u32 phys_addr)
1938{
1939 if (!phys_addr)
1940 return NULL;
1941 return ioc->reply + (phys_addr - (u32)ioc->reply_dma);
1942}
1943
1944/**
1945 * mpt3sas_base_get_smid - obtain a free smid from internal queue
1946 * @ioc: per adapter object
1947 * @cb_idx: callback index
1948 *
1949 * Returns smid (zero is invalid)
1950 */
1951u16
1952mpt3sas_base_get_smid(struct MPT3SAS_ADAPTER *ioc, u8 cb_idx)
1953{
1954 unsigned long flags;
1955 struct request_tracker *request;
1956 u16 smid;
1957
1958 spin_lock_irqsave(&ioc->scsi_lookup_lock, flags);
1959 if (list_empty(&ioc->internal_free_list)) {
1960 spin_unlock_irqrestore(&ioc->scsi_lookup_lock, flags);
1961 pr_err(MPT3SAS_FMT "%s: smid not available\n",
1962 ioc->name, __func__);
1963 return 0;
1964 }
1965
1966 request = list_entry(ioc->internal_free_list.next,
1967 struct request_tracker, tracker_list);
1968 request->cb_idx = cb_idx;
1969 smid = request->smid;
1970 list_del(&request->tracker_list);
1971 spin_unlock_irqrestore(&ioc->scsi_lookup_lock, flags);
1972 return smid;
1973}
1974
1975/**
1976 * mpt3sas_base_get_smid_scsiio - obtain a free smid from scsiio queue
1977 * @ioc: per adapter object
1978 * @cb_idx: callback index
1979 * @scmd: pointer to scsi command object
1980 *
1981 * Returns smid (zero is invalid)
1982 */
1983u16
1984mpt3sas_base_get_smid_scsiio(struct MPT3SAS_ADAPTER *ioc, u8 cb_idx,
1985 struct scsi_cmnd *scmd)
1986{
1987 unsigned long flags;
1988 struct scsiio_tracker *request;
1989 u16 smid;
1990
1991 spin_lock_irqsave(&ioc->scsi_lookup_lock, flags);
1992 if (list_empty(&ioc->free_list)) {
1993 spin_unlock_irqrestore(&ioc->scsi_lookup_lock, flags);
1994 pr_err(MPT3SAS_FMT "%s: smid not available\n",
1995 ioc->name, __func__);
1996 return 0;
1997 }
1998
1999 request = list_entry(ioc->free_list.next,
2000 struct scsiio_tracker, tracker_list);
2001 request->scmd = scmd;
2002 request->cb_idx = cb_idx;
2003 smid = request->smid;
2004 list_del(&request->tracker_list);
2005 spin_unlock_irqrestore(&ioc->scsi_lookup_lock, flags);
2006 return smid;
2007}
2008
2009/**
2010 * mpt3sas_base_get_smid_hpr - obtain a free smid from hi-priority queue
2011 * @ioc: per adapter object
2012 * @cb_idx: callback index
2013 *
2014 * Returns smid (zero is invalid)
2015 */
2016u16
2017mpt3sas_base_get_smid_hpr(struct MPT3SAS_ADAPTER *ioc, u8 cb_idx)
2018{
2019 unsigned long flags;
2020 struct request_tracker *request;
2021 u16 smid;
2022
2023 spin_lock_irqsave(&ioc->scsi_lookup_lock, flags);
2024 if (list_empty(&ioc->hpr_free_list)) {
2025 spin_unlock_irqrestore(&ioc->scsi_lookup_lock, flags);
2026 return 0;
2027 }
2028
2029 request = list_entry(ioc->hpr_free_list.next,
2030 struct request_tracker, tracker_list);
2031 request->cb_idx = cb_idx;
2032 smid = request->smid;
2033 list_del(&request->tracker_list);
2034 spin_unlock_irqrestore(&ioc->scsi_lookup_lock, flags);
2035 return smid;
2036}
2037
2038/**
2039 * mpt3sas_base_free_smid - put smid back on free_list
2040 * @ioc: per adapter object
2041 * @smid: system request message index
2042 *
2043 * Return nothing.
2044 */
2045void
2046mpt3sas_base_free_smid(struct MPT3SAS_ADAPTER *ioc, u16 smid)
2047{
2048 unsigned long flags;
2049 int i;
2050 struct chain_tracker *chain_req, *next;
2051
2052 spin_lock_irqsave(&ioc->scsi_lookup_lock, flags);
2053 if (smid < ioc->hi_priority_smid) {
2054 /* scsiio queue */
2055 i = smid - 1;
2056 if (!list_empty(&ioc->scsi_lookup[i].chain_list)) {
2057 list_for_each_entry_safe(chain_req, next,
2058 &ioc->scsi_lookup[i].chain_list, tracker_list) {
2059 list_del_init(&chain_req->tracker_list);
2060 list_add(&chain_req->tracker_list,
2061 &ioc->free_chain_list);
2062 }
2063 }
2064 ioc->scsi_lookup[i].cb_idx = 0xFF;
2065 ioc->scsi_lookup[i].scmd = NULL;
2066 list_add(&ioc->scsi_lookup[i].tracker_list, &ioc->free_list);
2067 spin_unlock_irqrestore(&ioc->scsi_lookup_lock, flags);
2068
2069 /*
2070 * See _wait_for_commands_to_complete() call with regards
2071 * to this code.
2072 */
2073 if (ioc->shost_recovery && ioc->pending_io_count) {
2074 if (ioc->pending_io_count == 1)
2075 wake_up(&ioc->reset_wq);
2076 ioc->pending_io_count--;
2077 }
2078 return;
2079 } else if (smid < ioc->internal_smid) {
2080 /* hi-priority */
2081 i = smid - ioc->hi_priority_smid;
2082 ioc->hpr_lookup[i].cb_idx = 0xFF;
2083 list_add(&ioc->hpr_lookup[i].tracker_list, &ioc->hpr_free_list);
2084 } else if (smid <= ioc->hba_queue_depth) {
2085 /* internal queue */
2086 i = smid - ioc->internal_smid;
2087 ioc->internal_lookup[i].cb_idx = 0xFF;
2088 list_add(&ioc->internal_lookup[i].tracker_list,
2089 &ioc->internal_free_list);
2090 }
2091 spin_unlock_irqrestore(&ioc->scsi_lookup_lock, flags);
2092}
2093
2094/**
2095 * _base_writeq - 64 bit write to MMIO
2096 * @ioc: per adapter object
2097 * @b: data payload
2098 * @addr: address in MMIO space
2099 * @writeq_lock: spin lock
2100 *
2101 * Glue for handling an atomic 64 bit word to MMIO. This special handling takes
2102 * care of 32 bit environment where its not quarenteed to send the entire word
2103 * in one transfer.
2104 */
2105#if defined(writeq) && defined(CONFIG_64BIT)
2106static inline void
2107_base_writeq(__u64 b, volatile void __iomem *addr, spinlock_t *writeq_lock)
2108{
2109 writeq(cpu_to_le64(b), addr);
2110}
2111#else
2112static inline void
2113_base_writeq(__u64 b, volatile void __iomem *addr, spinlock_t *writeq_lock)
2114{
2115 unsigned long flags;
2116 __u64 data_out = cpu_to_le64(b);
2117
2118 spin_lock_irqsave(writeq_lock, flags);
2119 writel((u32)(data_out), addr);
2120 writel((u32)(data_out >> 32), (addr + 4));
2121 spin_unlock_irqrestore(writeq_lock, flags);
2122}
2123#endif
2124
2125static inline u8
2126_base_get_msix_index(struct MPT3SAS_ADAPTER *ioc)
2127{
2128 return ioc->cpu_msix_table[raw_smp_processor_id()];
2129}
2130
2131/**
2132 * mpt3sas_base_put_smid_scsi_io - send SCSI_IO request to firmware
2133 * @ioc: per adapter object
2134 * @smid: system request message index
2135 * @handle: device handle
2136 *
2137 * Return nothing.
2138 */
2139void
2140mpt3sas_base_put_smid_scsi_io(struct MPT3SAS_ADAPTER *ioc, u16 smid, u16 handle)
2141{
2142 Mpi2RequestDescriptorUnion_t descriptor;
2143 u64 *request = (u64 *)&descriptor;
2144
2145
2146 descriptor.SCSIIO.RequestFlags = MPI2_REQ_DESCRIPT_FLAGS_SCSI_IO;
2147 descriptor.SCSIIO.MSIxIndex = _base_get_msix_index(ioc);
2148 descriptor.SCSIIO.SMID = cpu_to_le16(smid);
2149 descriptor.SCSIIO.DevHandle = cpu_to_le16(handle);
2150 descriptor.SCSIIO.LMID = 0;
2151 _base_writeq(*request, &ioc->chip->RequestDescriptorPostLow,
2152 &ioc->scsi_lookup_lock);
2153}
2154
2155/**
2156 * mpt3sas_base_put_smid_fast_path - send fast path request to firmware
2157 * @ioc: per adapter object
2158 * @smid: system request message index
2159 * @handle: device handle
2160 *
2161 * Return nothing.
2162 */
2163void
2164mpt3sas_base_put_smid_fast_path(struct MPT3SAS_ADAPTER *ioc, u16 smid,
2165 u16 handle)
2166{
2167 Mpi2RequestDescriptorUnion_t descriptor;
2168 u64 *request = (u64 *)&descriptor;
2169
2170 descriptor.SCSIIO.RequestFlags =
2171 MPI25_REQ_DESCRIPT_FLAGS_FAST_PATH_SCSI_IO;
2172 descriptor.SCSIIO.MSIxIndex = _base_get_msix_index(ioc);
2173 descriptor.SCSIIO.SMID = cpu_to_le16(smid);
2174 descriptor.SCSIIO.DevHandle = cpu_to_le16(handle);
2175 descriptor.SCSIIO.LMID = 0;
2176 _base_writeq(*request, &ioc->chip->RequestDescriptorPostLow,
2177 &ioc->scsi_lookup_lock);
2178}
2179
2180/**
2181 * mpt3sas_base_put_smid_hi_priority - send Task Managment request to firmware
2182 * @ioc: per adapter object
2183 * @smid: system request message index
2184 *
2185 * Return nothing.
2186 */
2187void
2188mpt3sas_base_put_smid_hi_priority(struct MPT3SAS_ADAPTER *ioc, u16 smid)
2189{
2190 Mpi2RequestDescriptorUnion_t descriptor;
2191 u64 *request = (u64 *)&descriptor;
2192
2193 descriptor.HighPriority.RequestFlags =
2194 MPI2_REQ_DESCRIPT_FLAGS_HIGH_PRIORITY;
2195 descriptor.HighPriority.MSIxIndex = 0;
2196 descriptor.HighPriority.SMID = cpu_to_le16(smid);
2197 descriptor.HighPriority.LMID = 0;
2198 descriptor.HighPriority.Reserved1 = 0;
2199 _base_writeq(*request, &ioc->chip->RequestDescriptorPostLow,
2200 &ioc->scsi_lookup_lock);
2201}
2202
2203/**
2204 * mpt3sas_base_put_smid_default - Default, primarily used for config pages
2205 * @ioc: per adapter object
2206 * @smid: system request message index
2207 *
2208 * Return nothing.
2209 */
2210void
2211mpt3sas_base_put_smid_default(struct MPT3SAS_ADAPTER *ioc, u16 smid)
2212{
2213 Mpi2RequestDescriptorUnion_t descriptor;
2214 u64 *request = (u64 *)&descriptor;
2215
2216 descriptor.Default.RequestFlags = MPI2_REQ_DESCRIPT_FLAGS_DEFAULT_TYPE;
2217 descriptor.Default.MSIxIndex = _base_get_msix_index(ioc);
2218 descriptor.Default.SMID = cpu_to_le16(smid);
2219 descriptor.Default.LMID = 0;
2220 descriptor.Default.DescriptorTypeDependent = 0;
2221 _base_writeq(*request, &ioc->chip->RequestDescriptorPostLow,
2222 &ioc->scsi_lookup_lock);
2223}
2224
Sreekanth Reddy1117b312014-09-12 15:35:30 +05302225/**
2226 * _base_display_intel_branding - Display branding string
2227 * @ioc: per adapter object
2228 *
2229 * Return nothing.
2230 */
2231static void
2232_base_display_intel_branding(struct MPT3SAS_ADAPTER *ioc)
2233{
2234 if (ioc->pdev->subsystem_vendor != PCI_VENDOR_ID_INTEL)
2235 return;
2236
2237 switch (ioc->pdev->device) {
2238 case MPI25_MFGPAGE_DEVID_SAS3008:
2239 switch (ioc->pdev->subsystem_device) {
2240 case MPT3SAS_INTEL_RMS3JC080_SSDID:
2241 pr_info(MPT3SAS_FMT "%s\n", ioc->name,
2242 MPT3SAS_INTEL_RMS3JC080_BRANDING);
2243 break;
2244
2245 case MPT3SAS_INTEL_RS3GC008_SSDID:
2246 pr_info(MPT3SAS_FMT "%s\n", ioc->name,
2247 MPT3SAS_INTEL_RS3GC008_BRANDING);
2248 break;
2249 case MPT3SAS_INTEL_RS3FC044_SSDID:
2250 pr_info(MPT3SAS_FMT "%s\n", ioc->name,
2251 MPT3SAS_INTEL_RS3FC044_BRANDING);
2252 break;
2253 case MPT3SAS_INTEL_RS3UC080_SSDID:
2254 pr_info(MPT3SAS_FMT "%s\n", ioc->name,
2255 MPT3SAS_INTEL_RS3UC080_BRANDING);
2256 break;
2257 default:
2258 pr_info(MPT3SAS_FMT
2259 "Intel(R) Controller: Subsystem ID: 0x%X\n",
2260 ioc->name, ioc->pdev->subsystem_device);
2261 break;
2262 }
2263 break;
2264 default:
2265 pr_info(MPT3SAS_FMT
2266 "Intel(R) Controller: Subsystem ID: 0x%X\n",
2267 ioc->name, ioc->pdev->subsystem_device);
2268 break;
2269 }
2270}
2271
Sreekanth Reddyf92363d2012-11-30 07:44:21 +05302272
2273
2274/**
2275 * _base_display_ioc_capabilities - Disply IOC's capabilities.
2276 * @ioc: per adapter object
2277 *
2278 * Return nothing.
2279 */
2280static void
2281_base_display_ioc_capabilities(struct MPT3SAS_ADAPTER *ioc)
2282{
2283 int i = 0;
2284 char desc[16];
2285 u32 iounit_pg1_flags;
2286 u32 bios_version;
2287
2288 bios_version = le32_to_cpu(ioc->bios_pg3.BiosVersion);
2289 strncpy(desc, ioc->manu_pg0.ChipName, 16);
2290 pr_info(MPT3SAS_FMT "%s: FWVersion(%02d.%02d.%02d.%02d), "\
2291 "ChipRevision(0x%02x), BiosVersion(%02d.%02d.%02d.%02d)\n",
2292 ioc->name, desc,
2293 (ioc->facts.FWVersion.Word & 0xFF000000) >> 24,
2294 (ioc->facts.FWVersion.Word & 0x00FF0000) >> 16,
2295 (ioc->facts.FWVersion.Word & 0x0000FF00) >> 8,
2296 ioc->facts.FWVersion.Word & 0x000000FF,
2297 ioc->pdev->revision,
2298 (bios_version & 0xFF000000) >> 24,
2299 (bios_version & 0x00FF0000) >> 16,
2300 (bios_version & 0x0000FF00) >> 8,
2301 bios_version & 0x000000FF);
2302
Sreekanth Reddy1117b312014-09-12 15:35:30 +05302303 _base_display_intel_branding(ioc);
2304
Sreekanth Reddyf92363d2012-11-30 07:44:21 +05302305 pr_info(MPT3SAS_FMT "Protocol=(", ioc->name);
2306
2307 if (ioc->facts.ProtocolFlags & MPI2_IOCFACTS_PROTOCOL_SCSI_INITIATOR) {
2308 pr_info("Initiator");
2309 i++;
2310 }
2311
2312 if (ioc->facts.ProtocolFlags & MPI2_IOCFACTS_PROTOCOL_SCSI_TARGET) {
2313 pr_info("%sTarget", i ? "," : "");
2314 i++;
2315 }
2316
2317 i = 0;
2318 pr_info("), ");
2319 pr_info("Capabilities=(");
2320
2321 if (ioc->facts.IOCCapabilities &
2322 MPI2_IOCFACTS_CAPABILITY_INTEGRATED_RAID) {
2323 pr_info("Raid");
2324 i++;
2325 }
2326
2327 if (ioc->facts.IOCCapabilities & MPI2_IOCFACTS_CAPABILITY_TLR) {
2328 pr_info("%sTLR", i ? "," : "");
2329 i++;
2330 }
2331
2332 if (ioc->facts.IOCCapabilities & MPI2_IOCFACTS_CAPABILITY_MULTICAST) {
2333 pr_info("%sMulticast", i ? "," : "");
2334 i++;
2335 }
2336
2337 if (ioc->facts.IOCCapabilities &
2338 MPI2_IOCFACTS_CAPABILITY_BIDIRECTIONAL_TARGET) {
2339 pr_info("%sBIDI Target", i ? "," : "");
2340 i++;
2341 }
2342
2343 if (ioc->facts.IOCCapabilities & MPI2_IOCFACTS_CAPABILITY_EEDP) {
2344 pr_info("%sEEDP", i ? "," : "");
2345 i++;
2346 }
2347
2348 if (ioc->facts.IOCCapabilities &
2349 MPI2_IOCFACTS_CAPABILITY_SNAPSHOT_BUFFER) {
2350 pr_info("%sSnapshot Buffer", i ? "," : "");
2351 i++;
2352 }
2353
2354 if (ioc->facts.IOCCapabilities &
2355 MPI2_IOCFACTS_CAPABILITY_DIAG_TRACE_BUFFER) {
2356 pr_info("%sDiag Trace Buffer", i ? "," : "");
2357 i++;
2358 }
2359
2360 if (ioc->facts.IOCCapabilities &
2361 MPI2_IOCFACTS_CAPABILITY_EXTENDED_BUFFER) {
2362 pr_info("%sDiag Extended Buffer", i ? "," : "");
2363 i++;
2364 }
2365
2366 if (ioc->facts.IOCCapabilities &
2367 MPI2_IOCFACTS_CAPABILITY_TASK_SET_FULL_HANDLING) {
2368 pr_info("%sTask Set Full", i ? "," : "");
2369 i++;
2370 }
2371
2372 iounit_pg1_flags = le32_to_cpu(ioc->iounit_pg1.Flags);
2373 if (!(iounit_pg1_flags & MPI2_IOUNITPAGE1_NATIVE_COMMAND_Q_DISABLE)) {
2374 pr_info("%sNCQ", i ? "," : "");
2375 i++;
2376 }
2377
2378 pr_info(")\n");
2379}
2380
2381/**
2382 * mpt3sas_base_update_missing_delay - change the missing delay timers
2383 * @ioc: per adapter object
2384 * @device_missing_delay: amount of time till device is reported missing
2385 * @io_missing_delay: interval IO is returned when there is a missing device
2386 *
2387 * Return nothing.
2388 *
2389 * Passed on the command line, this function will modify the device missing
2390 * delay, as well as the io missing delay. This should be called at driver
2391 * load time.
2392 */
2393void
2394mpt3sas_base_update_missing_delay(struct MPT3SAS_ADAPTER *ioc,
2395 u16 device_missing_delay, u8 io_missing_delay)
2396{
2397 u16 dmd, dmd_new, dmd_orignal;
2398 u8 io_missing_delay_original;
2399 u16 sz;
2400 Mpi2SasIOUnitPage1_t *sas_iounit_pg1 = NULL;
2401 Mpi2ConfigReply_t mpi_reply;
2402 u8 num_phys = 0;
2403 u16 ioc_status;
2404
2405 mpt3sas_config_get_number_hba_phys(ioc, &num_phys);
2406 if (!num_phys)
2407 return;
2408
2409 sz = offsetof(Mpi2SasIOUnitPage1_t, PhyData) + (num_phys *
2410 sizeof(Mpi2SasIOUnit1PhyData_t));
2411 sas_iounit_pg1 = kzalloc(sz, GFP_KERNEL);
2412 if (!sas_iounit_pg1) {
2413 pr_err(MPT3SAS_FMT "failure at %s:%d/%s()!\n",
2414 ioc->name, __FILE__, __LINE__, __func__);
2415 goto out;
2416 }
2417 if ((mpt3sas_config_get_sas_iounit_pg1(ioc, &mpi_reply,
2418 sas_iounit_pg1, sz))) {
2419 pr_err(MPT3SAS_FMT "failure at %s:%d/%s()!\n",
2420 ioc->name, __FILE__, __LINE__, __func__);
2421 goto out;
2422 }
2423 ioc_status = le16_to_cpu(mpi_reply.IOCStatus) &
2424 MPI2_IOCSTATUS_MASK;
2425 if (ioc_status != MPI2_IOCSTATUS_SUCCESS) {
2426 pr_err(MPT3SAS_FMT "failure at %s:%d/%s()!\n",
2427 ioc->name, __FILE__, __LINE__, __func__);
2428 goto out;
2429 }
2430
2431 /* device missing delay */
2432 dmd = sas_iounit_pg1->ReportDeviceMissingDelay;
2433 if (dmd & MPI2_SASIOUNIT1_REPORT_MISSING_UNIT_16)
2434 dmd = (dmd & MPI2_SASIOUNIT1_REPORT_MISSING_TIMEOUT_MASK) * 16;
2435 else
2436 dmd = dmd & MPI2_SASIOUNIT1_REPORT_MISSING_TIMEOUT_MASK;
2437 dmd_orignal = dmd;
2438 if (device_missing_delay > 0x7F) {
2439 dmd = (device_missing_delay > 0x7F0) ? 0x7F0 :
2440 device_missing_delay;
2441 dmd = dmd / 16;
2442 dmd |= MPI2_SASIOUNIT1_REPORT_MISSING_UNIT_16;
2443 } else
2444 dmd = device_missing_delay;
2445 sas_iounit_pg1->ReportDeviceMissingDelay = dmd;
2446
2447 /* io missing delay */
2448 io_missing_delay_original = sas_iounit_pg1->IODeviceMissingDelay;
2449 sas_iounit_pg1->IODeviceMissingDelay = io_missing_delay;
2450
2451 if (!mpt3sas_config_set_sas_iounit_pg1(ioc, &mpi_reply, sas_iounit_pg1,
2452 sz)) {
2453 if (dmd & MPI2_SASIOUNIT1_REPORT_MISSING_UNIT_16)
2454 dmd_new = (dmd &
2455 MPI2_SASIOUNIT1_REPORT_MISSING_TIMEOUT_MASK) * 16;
2456 else
2457 dmd_new =
2458 dmd & MPI2_SASIOUNIT1_REPORT_MISSING_TIMEOUT_MASK;
2459 pr_info(MPT3SAS_FMT "device_missing_delay: old(%d), new(%d)\n",
2460 ioc->name, dmd_orignal, dmd_new);
2461 pr_info(MPT3SAS_FMT "ioc_missing_delay: old(%d), new(%d)\n",
2462 ioc->name, io_missing_delay_original,
2463 io_missing_delay);
2464 ioc->device_missing_delay = dmd_new;
2465 ioc->io_missing_delay = io_missing_delay;
2466 }
2467
2468out:
2469 kfree(sas_iounit_pg1);
2470}
2471/**
2472 * _base_static_config_pages - static start of day config pages
2473 * @ioc: per adapter object
2474 *
2475 * Return nothing.
2476 */
2477static void
2478_base_static_config_pages(struct MPT3SAS_ADAPTER *ioc)
2479{
2480 Mpi2ConfigReply_t mpi_reply;
2481 u32 iounit_pg1_flags;
2482
2483 mpt3sas_config_get_manufacturing_pg0(ioc, &mpi_reply, &ioc->manu_pg0);
2484 if (ioc->ir_firmware)
2485 mpt3sas_config_get_manufacturing_pg10(ioc, &mpi_reply,
2486 &ioc->manu_pg10);
2487
2488 /*
2489 * Ensure correct T10 PI operation if vendor left EEDPTagMode
2490 * flag unset in NVDATA.
2491 */
2492 mpt3sas_config_get_manufacturing_pg11(ioc, &mpi_reply, &ioc->manu_pg11);
2493 if (ioc->manu_pg11.EEDPTagMode == 0) {
2494 pr_err("%s: overriding NVDATA EEDPTagMode setting\n",
2495 ioc->name);
2496 ioc->manu_pg11.EEDPTagMode &= ~0x3;
2497 ioc->manu_pg11.EEDPTagMode |= 0x1;
2498 mpt3sas_config_set_manufacturing_pg11(ioc, &mpi_reply,
2499 &ioc->manu_pg11);
2500 }
2501
2502 mpt3sas_config_get_bios_pg2(ioc, &mpi_reply, &ioc->bios_pg2);
2503 mpt3sas_config_get_bios_pg3(ioc, &mpi_reply, &ioc->bios_pg3);
2504 mpt3sas_config_get_ioc_pg8(ioc, &mpi_reply, &ioc->ioc_pg8);
2505 mpt3sas_config_get_iounit_pg0(ioc, &mpi_reply, &ioc->iounit_pg0);
2506 mpt3sas_config_get_iounit_pg1(ioc, &mpi_reply, &ioc->iounit_pg1);
Sreekanth Reddy2d8ce8c2015-01-12 11:38:56 +05302507 mpt3sas_config_get_iounit_pg8(ioc, &mpi_reply, &ioc->iounit_pg8);
Sreekanth Reddyf92363d2012-11-30 07:44:21 +05302508 _base_display_ioc_capabilities(ioc);
2509
2510 /*
2511 * Enable task_set_full handling in iounit_pg1 when the
2512 * facts capabilities indicate that its supported.
2513 */
2514 iounit_pg1_flags = le32_to_cpu(ioc->iounit_pg1.Flags);
2515 if ((ioc->facts.IOCCapabilities &
2516 MPI2_IOCFACTS_CAPABILITY_TASK_SET_FULL_HANDLING))
2517 iounit_pg1_flags &=
2518 ~MPI2_IOUNITPAGE1_DISABLE_TASK_SET_FULL_HANDLING;
2519 else
2520 iounit_pg1_flags |=
2521 MPI2_IOUNITPAGE1_DISABLE_TASK_SET_FULL_HANDLING;
2522 ioc->iounit_pg1.Flags = cpu_to_le32(iounit_pg1_flags);
2523 mpt3sas_config_set_iounit_pg1(ioc, &mpi_reply, &ioc->iounit_pg1);
Sreekanth Reddy2d8ce8c2015-01-12 11:38:56 +05302524
2525 if (ioc->iounit_pg8.NumSensors)
2526 ioc->temp_sensors_count = ioc->iounit_pg8.NumSensors;
Sreekanth Reddyf92363d2012-11-30 07:44:21 +05302527}
2528
2529/**
2530 * _base_release_memory_pools - release memory
2531 * @ioc: per adapter object
2532 *
2533 * Free memory allocated from _base_allocate_memory_pools.
2534 *
2535 * Return nothing.
2536 */
2537static void
2538_base_release_memory_pools(struct MPT3SAS_ADAPTER *ioc)
2539{
Sreekanth Reddy9b05c912014-09-12 15:35:31 +05302540 int i = 0;
2541 struct reply_post_struct *rps;
Sreekanth Reddyf92363d2012-11-30 07:44:21 +05302542
2543 dexitprintk(ioc, pr_info(MPT3SAS_FMT "%s\n", ioc->name,
2544 __func__));
2545
2546 if (ioc->request) {
2547 pci_free_consistent(ioc->pdev, ioc->request_dma_sz,
2548 ioc->request, ioc->request_dma);
2549 dexitprintk(ioc, pr_info(MPT3SAS_FMT
2550 "request_pool(0x%p): free\n",
2551 ioc->name, ioc->request));
2552 ioc->request = NULL;
2553 }
2554
2555 if (ioc->sense) {
2556 pci_pool_free(ioc->sense_dma_pool, ioc->sense, ioc->sense_dma);
2557 if (ioc->sense_dma_pool)
2558 pci_pool_destroy(ioc->sense_dma_pool);
2559 dexitprintk(ioc, pr_info(MPT3SAS_FMT
2560 "sense_pool(0x%p): free\n",
2561 ioc->name, ioc->sense));
2562 ioc->sense = NULL;
2563 }
2564
2565 if (ioc->reply) {
2566 pci_pool_free(ioc->reply_dma_pool, ioc->reply, ioc->reply_dma);
2567 if (ioc->reply_dma_pool)
2568 pci_pool_destroy(ioc->reply_dma_pool);
2569 dexitprintk(ioc, pr_info(MPT3SAS_FMT
2570 "reply_pool(0x%p): free\n",
2571 ioc->name, ioc->reply));
2572 ioc->reply = NULL;
2573 }
2574
2575 if (ioc->reply_free) {
2576 pci_pool_free(ioc->reply_free_dma_pool, ioc->reply_free,
2577 ioc->reply_free_dma);
2578 if (ioc->reply_free_dma_pool)
2579 pci_pool_destroy(ioc->reply_free_dma_pool);
2580 dexitprintk(ioc, pr_info(MPT3SAS_FMT
2581 "reply_free_pool(0x%p): free\n",
2582 ioc->name, ioc->reply_free));
2583 ioc->reply_free = NULL;
2584 }
2585
Sreekanth Reddy9b05c912014-09-12 15:35:31 +05302586 if (ioc->reply_post) {
2587 do {
2588 rps = &ioc->reply_post[i];
2589 if (rps->reply_post_free) {
2590 pci_pool_free(
2591 ioc->reply_post_free_dma_pool,
2592 rps->reply_post_free,
2593 rps->reply_post_free_dma);
2594 dexitprintk(ioc, pr_info(MPT3SAS_FMT
2595 "reply_post_free_pool(0x%p): free\n",
2596 ioc->name, rps->reply_post_free));
2597 rps->reply_post_free = NULL;
2598 }
2599 } while (ioc->rdpq_array_enable &&
2600 (++i < ioc->reply_queue_count));
2601
Sreekanth Reddyf92363d2012-11-30 07:44:21 +05302602 if (ioc->reply_post_free_dma_pool)
2603 pci_pool_destroy(ioc->reply_post_free_dma_pool);
Sreekanth Reddy9b05c912014-09-12 15:35:31 +05302604 kfree(ioc->reply_post);
Sreekanth Reddyf92363d2012-11-30 07:44:21 +05302605 }
2606
2607 if (ioc->config_page) {
2608 dexitprintk(ioc, pr_info(MPT3SAS_FMT
2609 "config_page(0x%p): free\n", ioc->name,
2610 ioc->config_page));
2611 pci_free_consistent(ioc->pdev, ioc->config_page_sz,
2612 ioc->config_page, ioc->config_page_dma);
2613 }
2614
2615 if (ioc->scsi_lookup) {
2616 free_pages((ulong)ioc->scsi_lookup, ioc->scsi_lookup_pages);
2617 ioc->scsi_lookup = NULL;
2618 }
2619 kfree(ioc->hpr_lookup);
2620 kfree(ioc->internal_lookup);
2621 if (ioc->chain_lookup) {
2622 for (i = 0; i < ioc->chain_depth; i++) {
2623 if (ioc->chain_lookup[i].chain_buffer)
2624 pci_pool_free(ioc->chain_dma_pool,
2625 ioc->chain_lookup[i].chain_buffer,
2626 ioc->chain_lookup[i].chain_buffer_dma);
2627 }
2628 if (ioc->chain_dma_pool)
2629 pci_pool_destroy(ioc->chain_dma_pool);
2630 free_pages((ulong)ioc->chain_lookup, ioc->chain_pages);
2631 ioc->chain_lookup = NULL;
2632 }
2633}
2634
2635/**
2636 * _base_allocate_memory_pools - allocate start of day memory pools
2637 * @ioc: per adapter object
2638 * @sleep_flag: CAN_SLEEP or NO_SLEEP
2639 *
2640 * Returns 0 success, anything else error
2641 */
2642static int
2643_base_allocate_memory_pools(struct MPT3SAS_ADAPTER *ioc, int sleep_flag)
2644{
2645 struct mpt3sas_facts *facts;
2646 u16 max_sge_elements;
2647 u16 chains_needed_per_io;
2648 u32 sz, total_sz, reply_post_free_sz;
2649 u32 retry_sz;
2650 u16 max_request_credit;
2651 unsigned short sg_tablesize;
2652 u16 sge_size;
2653 int i;
2654
2655 dinitprintk(ioc, pr_info(MPT3SAS_FMT "%s\n", ioc->name,
2656 __func__));
2657
2658
2659 retry_sz = 0;
2660 facts = &ioc->facts;
2661
2662 /* command line tunables for max sgl entries */
2663 if (max_sgl_entries != -1)
2664 sg_tablesize = max_sgl_entries;
2665 else
2666 sg_tablesize = MPT3SAS_SG_DEPTH;
2667
2668 if (sg_tablesize < MPT3SAS_MIN_PHYS_SEGMENTS)
2669 sg_tablesize = MPT3SAS_MIN_PHYS_SEGMENTS;
Sreekanth Reddyad666a02015-01-12 11:39:00 +05302670 else if (sg_tablesize > MPT3SAS_MAX_PHYS_SEGMENTS) {
2671 sg_tablesize = min_t(unsigned short, sg_tablesize,
2672 SCSI_MAX_SG_CHAIN_SEGMENTS);
2673 pr_warn(MPT3SAS_FMT
2674 "sg_tablesize(%u) is bigger than kernel"
2675 " defined SCSI_MAX_SG_SEGMENTS(%u)\n", ioc->name,
2676 sg_tablesize, MPT3SAS_MAX_PHYS_SEGMENTS);
2677 }
Sreekanth Reddyf92363d2012-11-30 07:44:21 +05302678 ioc->shost->sg_tablesize = sg_tablesize;
2679
2680 ioc->hi_priority_depth = facts->HighPriorityCredit;
2681 ioc->internal_depth = ioc->hi_priority_depth + (5);
2682 /* command line tunables for max controller queue depth */
2683 if (max_queue_depth != -1 && max_queue_depth != 0) {
2684 max_request_credit = min_t(u16, max_queue_depth +
2685 ioc->hi_priority_depth + ioc->internal_depth,
2686 facts->RequestCredit);
2687 if (max_request_credit > MAX_HBA_QUEUE_DEPTH)
2688 max_request_credit = MAX_HBA_QUEUE_DEPTH;
2689 } else
2690 max_request_credit = min_t(u16, facts->RequestCredit,
2691 MAX_HBA_QUEUE_DEPTH);
2692
2693 ioc->hba_queue_depth = max_request_credit;
2694
2695 /* request frame size */
2696 ioc->request_sz = facts->IOCRequestFrameSize * 4;
2697
2698 /* reply frame size */
2699 ioc->reply_sz = facts->ReplyFrameSize * 4;
2700
2701 /* calculate the max scatter element size */
2702 sge_size = max_t(u16, ioc->sge_size, ioc->sge_size_ieee);
2703
2704 retry_allocation:
2705 total_sz = 0;
2706 /* calculate number of sg elements left over in the 1st frame */
2707 max_sge_elements = ioc->request_sz - ((sizeof(Mpi2SCSIIORequest_t) -
2708 sizeof(Mpi2SGEIOUnion_t)) + sge_size);
2709 ioc->max_sges_in_main_message = max_sge_elements/sge_size;
2710
2711 /* now do the same for a chain buffer */
2712 max_sge_elements = ioc->request_sz - sge_size;
2713 ioc->max_sges_in_chain_message = max_sge_elements/sge_size;
2714
2715 /*
2716 * MPT3SAS_SG_DEPTH = CONFIG_FUSION_MAX_SGE
2717 */
2718 chains_needed_per_io = ((ioc->shost->sg_tablesize -
2719 ioc->max_sges_in_main_message)/ioc->max_sges_in_chain_message)
2720 + 1;
2721 if (chains_needed_per_io > facts->MaxChainDepth) {
2722 chains_needed_per_io = facts->MaxChainDepth;
2723 ioc->shost->sg_tablesize = min_t(u16,
2724 ioc->max_sges_in_main_message + (ioc->max_sges_in_chain_message
2725 * chains_needed_per_io), ioc->shost->sg_tablesize);
2726 }
2727 ioc->chains_needed_per_io = chains_needed_per_io;
2728
2729 /* reply free queue sizing - taking into account for 64 FW events */
2730 ioc->reply_free_queue_depth = ioc->hba_queue_depth + 64;
2731
2732 /* calculate reply descriptor post queue depth */
2733 ioc->reply_post_queue_depth = ioc->hba_queue_depth +
2734 ioc->reply_free_queue_depth + 1 ;
2735 /* align the reply post queue on the next 16 count boundary */
2736 if (ioc->reply_post_queue_depth % 16)
2737 ioc->reply_post_queue_depth += 16 -
2738 (ioc->reply_post_queue_depth % 16);
2739
2740
2741 if (ioc->reply_post_queue_depth >
2742 facts->MaxReplyDescriptorPostQueueDepth) {
2743 ioc->reply_post_queue_depth =
2744 facts->MaxReplyDescriptorPostQueueDepth -
2745 (facts->MaxReplyDescriptorPostQueueDepth % 16);
2746 ioc->hba_queue_depth =
2747 ((ioc->reply_post_queue_depth - 64) / 2) - 1;
2748 ioc->reply_free_queue_depth = ioc->hba_queue_depth + 64;
2749 }
2750
2751 dinitprintk(ioc, pr_info(MPT3SAS_FMT "scatter gather: " \
2752 "sge_in_main_msg(%d), sge_per_chain(%d), sge_per_io(%d), "
2753 "chains_per_io(%d)\n", ioc->name, ioc->max_sges_in_main_message,
2754 ioc->max_sges_in_chain_message, ioc->shost->sg_tablesize,
2755 ioc->chains_needed_per_io));
2756
Sreekanth Reddy9b05c912014-09-12 15:35:31 +05302757 /* reply post queue, 16 byte align */
2758 reply_post_free_sz = ioc->reply_post_queue_depth *
2759 sizeof(Mpi2DefaultReplyDescriptor_t);
2760
2761 sz = reply_post_free_sz;
2762 if (_base_is_controller_msix_enabled(ioc) && !ioc->rdpq_array_enable)
2763 sz *= ioc->reply_queue_count;
2764
2765 ioc->reply_post = kcalloc((ioc->rdpq_array_enable) ?
2766 (ioc->reply_queue_count):1,
2767 sizeof(struct reply_post_struct), GFP_KERNEL);
2768
2769 if (!ioc->reply_post) {
2770 pr_err(MPT3SAS_FMT "reply_post_free pool: kcalloc failed\n",
2771 ioc->name);
2772 goto out;
2773 }
2774 ioc->reply_post_free_dma_pool = pci_pool_create("reply_post_free pool",
2775 ioc->pdev, sz, 16, 0);
2776 if (!ioc->reply_post_free_dma_pool) {
2777 pr_err(MPT3SAS_FMT
2778 "reply_post_free pool: pci_pool_create failed\n",
2779 ioc->name);
2780 goto out;
2781 }
2782 i = 0;
2783 do {
2784 ioc->reply_post[i].reply_post_free =
2785 pci_pool_alloc(ioc->reply_post_free_dma_pool,
2786 GFP_KERNEL,
2787 &ioc->reply_post[i].reply_post_free_dma);
2788 if (!ioc->reply_post[i].reply_post_free) {
2789 pr_err(MPT3SAS_FMT
2790 "reply_post_free pool: pci_pool_alloc failed\n",
2791 ioc->name);
2792 goto out;
2793 }
2794 memset(ioc->reply_post[i].reply_post_free, 0, sz);
2795 dinitprintk(ioc, pr_info(MPT3SAS_FMT
2796 "reply post free pool (0x%p): depth(%d),"
2797 "element_size(%d), pool_size(%d kB)\n", ioc->name,
2798 ioc->reply_post[i].reply_post_free,
2799 ioc->reply_post_queue_depth, 8, sz/1024));
2800 dinitprintk(ioc, pr_info(MPT3SAS_FMT
2801 "reply_post_free_dma = (0x%llx)\n", ioc->name,
2802 (unsigned long long)
2803 ioc->reply_post[i].reply_post_free_dma));
2804 total_sz += sz;
2805 } while (ioc->rdpq_array_enable && (++i < ioc->reply_queue_count));
2806
2807 if (ioc->dma_mask == 64) {
2808 if (_base_change_consistent_dma_mask(ioc, ioc->pdev) != 0) {
2809 pr_warn(MPT3SAS_FMT
2810 "no suitable consistent DMA mask for %s\n",
2811 ioc->name, pci_name(ioc->pdev));
2812 goto out;
2813 }
2814 }
2815
Sreekanth Reddyf92363d2012-11-30 07:44:21 +05302816 ioc->scsiio_depth = ioc->hba_queue_depth -
2817 ioc->hi_priority_depth - ioc->internal_depth;
2818
2819 /* set the scsi host can_queue depth
2820 * with some internal commands that could be outstanding
2821 */
2822 ioc->shost->can_queue = ioc->scsiio_depth;
2823 dinitprintk(ioc, pr_info(MPT3SAS_FMT
2824 "scsi host: can_queue depth (%d)\n",
2825 ioc->name, ioc->shost->can_queue));
2826
2827
2828 /* contiguous pool for request and chains, 16 byte align, one extra "
2829 * "frame for smid=0
2830 */
2831 ioc->chain_depth = ioc->chains_needed_per_io * ioc->scsiio_depth;
2832 sz = ((ioc->scsiio_depth + 1) * ioc->request_sz);
2833
2834 /* hi-priority queue */
2835 sz += (ioc->hi_priority_depth * ioc->request_sz);
2836
2837 /* internal queue */
2838 sz += (ioc->internal_depth * ioc->request_sz);
2839
2840 ioc->request_dma_sz = sz;
2841 ioc->request = pci_alloc_consistent(ioc->pdev, sz, &ioc->request_dma);
2842 if (!ioc->request) {
2843 pr_err(MPT3SAS_FMT "request pool: pci_alloc_consistent " \
2844 "failed: hba_depth(%d), chains_per_io(%d), frame_sz(%d), "
2845 "total(%d kB)\n", ioc->name, ioc->hba_queue_depth,
2846 ioc->chains_needed_per_io, ioc->request_sz, sz/1024);
2847 if (ioc->scsiio_depth < MPT3SAS_SAS_QUEUE_DEPTH)
2848 goto out;
2849 retry_sz += 64;
2850 ioc->hba_queue_depth = max_request_credit - retry_sz;
2851 goto retry_allocation;
2852 }
2853
2854 if (retry_sz)
2855 pr_err(MPT3SAS_FMT "request pool: pci_alloc_consistent " \
2856 "succeed: hba_depth(%d), chains_per_io(%d), frame_sz(%d), "
2857 "total(%d kb)\n", ioc->name, ioc->hba_queue_depth,
2858 ioc->chains_needed_per_io, ioc->request_sz, sz/1024);
2859
2860 /* hi-priority queue */
2861 ioc->hi_priority = ioc->request + ((ioc->scsiio_depth + 1) *
2862 ioc->request_sz);
2863 ioc->hi_priority_dma = ioc->request_dma + ((ioc->scsiio_depth + 1) *
2864 ioc->request_sz);
2865
2866 /* internal queue */
2867 ioc->internal = ioc->hi_priority + (ioc->hi_priority_depth *
2868 ioc->request_sz);
2869 ioc->internal_dma = ioc->hi_priority_dma + (ioc->hi_priority_depth *
2870 ioc->request_sz);
2871
2872 dinitprintk(ioc, pr_info(MPT3SAS_FMT
2873 "request pool(0x%p): depth(%d), frame_size(%d), pool_size(%d kB)\n",
2874 ioc->name, ioc->request, ioc->hba_queue_depth, ioc->request_sz,
2875 (ioc->hba_queue_depth * ioc->request_sz)/1024));
2876
2877 dinitprintk(ioc, pr_info(MPT3SAS_FMT "request pool: dma(0x%llx)\n",
2878 ioc->name, (unsigned long long) ioc->request_dma));
2879 total_sz += sz;
2880
2881 sz = ioc->scsiio_depth * sizeof(struct scsiio_tracker);
2882 ioc->scsi_lookup_pages = get_order(sz);
2883 ioc->scsi_lookup = (struct scsiio_tracker *)__get_free_pages(
2884 GFP_KERNEL, ioc->scsi_lookup_pages);
2885 if (!ioc->scsi_lookup) {
2886 pr_err(MPT3SAS_FMT "scsi_lookup: get_free_pages failed, sz(%d)\n",
2887 ioc->name, (int)sz);
2888 goto out;
2889 }
2890
2891 dinitprintk(ioc, pr_info(MPT3SAS_FMT "scsiio(0x%p): depth(%d)\n",
2892 ioc->name, ioc->request, ioc->scsiio_depth));
2893
2894 ioc->chain_depth = min_t(u32, ioc->chain_depth, MAX_CHAIN_DEPTH);
2895 sz = ioc->chain_depth * sizeof(struct chain_tracker);
2896 ioc->chain_pages = get_order(sz);
2897 ioc->chain_lookup = (struct chain_tracker *)__get_free_pages(
2898 GFP_KERNEL, ioc->chain_pages);
2899 if (!ioc->chain_lookup) {
2900 pr_err(MPT3SAS_FMT "chain_lookup: __get_free_pages failed\n",
2901 ioc->name);
2902 goto out;
2903 }
2904 ioc->chain_dma_pool = pci_pool_create("chain pool", ioc->pdev,
2905 ioc->request_sz, 16, 0);
2906 if (!ioc->chain_dma_pool) {
2907 pr_err(MPT3SAS_FMT "chain_dma_pool: pci_pool_create failed\n",
2908 ioc->name);
2909 goto out;
2910 }
2911 for (i = 0; i < ioc->chain_depth; i++) {
2912 ioc->chain_lookup[i].chain_buffer = pci_pool_alloc(
2913 ioc->chain_dma_pool , GFP_KERNEL,
2914 &ioc->chain_lookup[i].chain_buffer_dma);
2915 if (!ioc->chain_lookup[i].chain_buffer) {
2916 ioc->chain_depth = i;
2917 goto chain_done;
2918 }
2919 total_sz += ioc->request_sz;
2920 }
2921 chain_done:
2922 dinitprintk(ioc, pr_info(MPT3SAS_FMT
2923 "chain pool depth(%d), frame_size(%d), pool_size(%d kB)\n",
2924 ioc->name, ioc->chain_depth, ioc->request_sz,
2925 ((ioc->chain_depth * ioc->request_sz))/1024));
2926
2927 /* initialize hi-priority queue smid's */
2928 ioc->hpr_lookup = kcalloc(ioc->hi_priority_depth,
2929 sizeof(struct request_tracker), GFP_KERNEL);
2930 if (!ioc->hpr_lookup) {
2931 pr_err(MPT3SAS_FMT "hpr_lookup: kcalloc failed\n",
2932 ioc->name);
2933 goto out;
2934 }
2935 ioc->hi_priority_smid = ioc->scsiio_depth + 1;
2936 dinitprintk(ioc, pr_info(MPT3SAS_FMT
2937 "hi_priority(0x%p): depth(%d), start smid(%d)\n",
2938 ioc->name, ioc->hi_priority,
2939 ioc->hi_priority_depth, ioc->hi_priority_smid));
2940
2941 /* initialize internal queue smid's */
2942 ioc->internal_lookup = kcalloc(ioc->internal_depth,
2943 sizeof(struct request_tracker), GFP_KERNEL);
2944 if (!ioc->internal_lookup) {
2945 pr_err(MPT3SAS_FMT "internal_lookup: kcalloc failed\n",
2946 ioc->name);
2947 goto out;
2948 }
2949 ioc->internal_smid = ioc->hi_priority_smid + ioc->hi_priority_depth;
2950 dinitprintk(ioc, pr_info(MPT3SAS_FMT
2951 "internal(0x%p): depth(%d), start smid(%d)\n",
2952 ioc->name, ioc->internal,
2953 ioc->internal_depth, ioc->internal_smid));
2954
2955 /* sense buffers, 4 byte align */
2956 sz = ioc->scsiio_depth * SCSI_SENSE_BUFFERSIZE;
2957 ioc->sense_dma_pool = pci_pool_create("sense pool", ioc->pdev, sz, 4,
2958 0);
2959 if (!ioc->sense_dma_pool) {
2960 pr_err(MPT3SAS_FMT "sense pool: pci_pool_create failed\n",
2961 ioc->name);
2962 goto out;
2963 }
2964 ioc->sense = pci_pool_alloc(ioc->sense_dma_pool , GFP_KERNEL,
2965 &ioc->sense_dma);
2966 if (!ioc->sense) {
2967 pr_err(MPT3SAS_FMT "sense pool: pci_pool_alloc failed\n",
2968 ioc->name);
2969 goto out;
2970 }
2971 dinitprintk(ioc, pr_info(MPT3SAS_FMT
2972 "sense pool(0x%p): depth(%d), element_size(%d), pool_size"
2973 "(%d kB)\n", ioc->name, ioc->sense, ioc->scsiio_depth,
2974 SCSI_SENSE_BUFFERSIZE, sz/1024));
2975 dinitprintk(ioc, pr_info(MPT3SAS_FMT "sense_dma(0x%llx)\n",
2976 ioc->name, (unsigned long long)ioc->sense_dma));
2977 total_sz += sz;
2978
2979 /* reply pool, 4 byte align */
2980 sz = ioc->reply_free_queue_depth * ioc->reply_sz;
2981 ioc->reply_dma_pool = pci_pool_create("reply pool", ioc->pdev, sz, 4,
2982 0);
2983 if (!ioc->reply_dma_pool) {
2984 pr_err(MPT3SAS_FMT "reply pool: pci_pool_create failed\n",
2985 ioc->name);
2986 goto out;
2987 }
2988 ioc->reply = pci_pool_alloc(ioc->reply_dma_pool , GFP_KERNEL,
2989 &ioc->reply_dma);
2990 if (!ioc->reply) {
2991 pr_err(MPT3SAS_FMT "reply pool: pci_pool_alloc failed\n",
2992 ioc->name);
2993 goto out;
2994 }
2995 ioc->reply_dma_min_address = (u32)(ioc->reply_dma);
2996 ioc->reply_dma_max_address = (u32)(ioc->reply_dma) + sz;
2997 dinitprintk(ioc, pr_info(MPT3SAS_FMT
2998 "reply pool(0x%p): depth(%d), frame_size(%d), pool_size(%d kB)\n",
2999 ioc->name, ioc->reply,
3000 ioc->reply_free_queue_depth, ioc->reply_sz, sz/1024));
3001 dinitprintk(ioc, pr_info(MPT3SAS_FMT "reply_dma(0x%llx)\n",
3002 ioc->name, (unsigned long long)ioc->reply_dma));
3003 total_sz += sz;
3004
3005 /* reply free queue, 16 byte align */
3006 sz = ioc->reply_free_queue_depth * 4;
3007 ioc->reply_free_dma_pool = pci_pool_create("reply_free pool",
3008 ioc->pdev, sz, 16, 0);
3009 if (!ioc->reply_free_dma_pool) {
3010 pr_err(MPT3SAS_FMT "reply_free pool: pci_pool_create failed\n",
3011 ioc->name);
3012 goto out;
3013 }
3014 ioc->reply_free = pci_pool_alloc(ioc->reply_free_dma_pool , GFP_KERNEL,
3015 &ioc->reply_free_dma);
3016 if (!ioc->reply_free) {
3017 pr_err(MPT3SAS_FMT "reply_free pool: pci_pool_alloc failed\n",
3018 ioc->name);
3019 goto out;
3020 }
3021 memset(ioc->reply_free, 0, sz);
3022 dinitprintk(ioc, pr_info(MPT3SAS_FMT "reply_free pool(0x%p): " \
3023 "depth(%d), element_size(%d), pool_size(%d kB)\n", ioc->name,
3024 ioc->reply_free, ioc->reply_free_queue_depth, 4, sz/1024));
3025 dinitprintk(ioc, pr_info(MPT3SAS_FMT
3026 "reply_free_dma (0x%llx)\n",
3027 ioc->name, (unsigned long long)ioc->reply_free_dma));
3028 total_sz += sz;
3029
Sreekanth Reddyf92363d2012-11-30 07:44:21 +05303030 ioc->config_page_sz = 512;
3031 ioc->config_page = pci_alloc_consistent(ioc->pdev,
3032 ioc->config_page_sz, &ioc->config_page_dma);
3033 if (!ioc->config_page) {
3034 pr_err(MPT3SAS_FMT
3035 "config page: pci_pool_alloc failed\n",
3036 ioc->name);
3037 goto out;
3038 }
3039 dinitprintk(ioc, pr_info(MPT3SAS_FMT
3040 "config page(0x%p): size(%d)\n",
3041 ioc->name, ioc->config_page, ioc->config_page_sz));
3042 dinitprintk(ioc, pr_info(MPT3SAS_FMT "config_page_dma(0x%llx)\n",
3043 ioc->name, (unsigned long long)ioc->config_page_dma));
3044 total_sz += ioc->config_page_sz;
3045
3046 pr_info(MPT3SAS_FMT "Allocated physical memory: size(%d kB)\n",
3047 ioc->name, total_sz/1024);
3048 pr_info(MPT3SAS_FMT
3049 "Current Controller Queue Depth(%d),Max Controller Queue Depth(%d)\n",
3050 ioc->name, ioc->shost->can_queue, facts->RequestCredit);
3051 pr_info(MPT3SAS_FMT "Scatter Gather Elements per IO(%d)\n",
3052 ioc->name, ioc->shost->sg_tablesize);
3053 return 0;
3054
3055 out:
3056 return -ENOMEM;
3057}
3058
3059/**
3060 * mpt3sas_base_get_iocstate - Get the current state of a MPT adapter.
3061 * @ioc: Pointer to MPT_ADAPTER structure
3062 * @cooked: Request raw or cooked IOC state
3063 *
3064 * Returns all IOC Doorbell register bits if cooked==0, else just the
3065 * Doorbell bits in MPI_IOC_STATE_MASK.
3066 */
3067u32
3068mpt3sas_base_get_iocstate(struct MPT3SAS_ADAPTER *ioc, int cooked)
3069{
3070 u32 s, sc;
3071
3072 s = readl(&ioc->chip->Doorbell);
3073 sc = s & MPI2_IOC_STATE_MASK;
3074 return cooked ? sc : s;
3075}
3076
3077/**
3078 * _base_wait_on_iocstate - waiting on a particular ioc state
3079 * @ioc_state: controller state { READY, OPERATIONAL, or RESET }
3080 * @timeout: timeout in second
3081 * @sleep_flag: CAN_SLEEP or NO_SLEEP
3082 *
3083 * Returns 0 for success, non-zero for failure.
3084 */
3085static int
3086_base_wait_on_iocstate(struct MPT3SAS_ADAPTER *ioc, u32 ioc_state, int timeout,
3087 int sleep_flag)
3088{
3089 u32 count, cntdn;
3090 u32 current_state;
3091
3092 count = 0;
3093 cntdn = (sleep_flag == CAN_SLEEP) ? 1000*timeout : 2000*timeout;
3094 do {
3095 current_state = mpt3sas_base_get_iocstate(ioc, 1);
3096 if (current_state == ioc_state)
3097 return 0;
3098 if (count && current_state == MPI2_IOC_STATE_FAULT)
3099 break;
3100 if (sleep_flag == CAN_SLEEP)
3101 usleep_range(1000, 1500);
3102 else
3103 udelay(500);
3104 count++;
3105 } while (--cntdn);
3106
3107 return current_state;
3108}
3109
3110/**
3111 * _base_wait_for_doorbell_int - waiting for controller interrupt(generated by
3112 * a write to the doorbell)
3113 * @ioc: per adapter object
3114 * @timeout: timeout in second
3115 * @sleep_flag: CAN_SLEEP or NO_SLEEP
3116 *
3117 * Returns 0 for success, non-zero for failure.
3118 *
3119 * Notes: MPI2_HIS_IOC2SYS_DB_STATUS - set to one when IOC writes to doorbell.
3120 */
3121static int
3122_base_wait_for_doorbell_int(struct MPT3SAS_ADAPTER *ioc, int timeout,
3123 int sleep_flag)
3124{
3125 u32 cntdn, count;
3126 u32 int_status;
3127
3128 count = 0;
3129 cntdn = (sleep_flag == CAN_SLEEP) ? 1000*timeout : 2000*timeout;
3130 do {
3131 int_status = readl(&ioc->chip->HostInterruptStatus);
3132 if (int_status & MPI2_HIS_IOC2SYS_DB_STATUS) {
3133 dhsprintk(ioc, pr_info(MPT3SAS_FMT
3134 "%s: successful count(%d), timeout(%d)\n",
3135 ioc->name, __func__, count, timeout));
3136 return 0;
3137 }
3138 if (sleep_flag == CAN_SLEEP)
3139 usleep_range(1000, 1500);
3140 else
3141 udelay(500);
3142 count++;
3143 } while (--cntdn);
3144
3145 pr_err(MPT3SAS_FMT
3146 "%s: failed due to timeout count(%d), int_status(%x)!\n",
3147 ioc->name, __func__, count, int_status);
3148 return -EFAULT;
3149}
3150
3151/**
3152 * _base_wait_for_doorbell_ack - waiting for controller to read the doorbell.
3153 * @ioc: per adapter object
3154 * @timeout: timeout in second
3155 * @sleep_flag: CAN_SLEEP or NO_SLEEP
3156 *
3157 * Returns 0 for success, non-zero for failure.
3158 *
3159 * Notes: MPI2_HIS_SYS2IOC_DB_STATUS - set to one when host writes to
3160 * doorbell.
3161 */
3162static int
3163_base_wait_for_doorbell_ack(struct MPT3SAS_ADAPTER *ioc, int timeout,
3164 int sleep_flag)
3165{
3166 u32 cntdn, count;
3167 u32 int_status;
3168 u32 doorbell;
3169
3170 count = 0;
3171 cntdn = (sleep_flag == CAN_SLEEP) ? 1000*timeout : 2000*timeout;
3172 do {
3173 int_status = readl(&ioc->chip->HostInterruptStatus);
3174 if (!(int_status & MPI2_HIS_SYS2IOC_DB_STATUS)) {
3175 dhsprintk(ioc, pr_info(MPT3SAS_FMT
3176 "%s: successful count(%d), timeout(%d)\n",
3177 ioc->name, __func__, count, timeout));
3178 return 0;
3179 } else if (int_status & MPI2_HIS_IOC2SYS_DB_STATUS) {
3180 doorbell = readl(&ioc->chip->Doorbell);
3181 if ((doorbell & MPI2_IOC_STATE_MASK) ==
3182 MPI2_IOC_STATE_FAULT) {
3183 mpt3sas_base_fault_info(ioc , doorbell);
3184 return -EFAULT;
3185 }
3186 } else if (int_status == 0xFFFFFFFF)
3187 goto out;
3188
3189 if (sleep_flag == CAN_SLEEP)
3190 usleep_range(1000, 1500);
3191 else
3192 udelay(500);
3193 count++;
3194 } while (--cntdn);
3195
3196 out:
3197 pr_err(MPT3SAS_FMT
3198 "%s: failed due to timeout count(%d), int_status(%x)!\n",
3199 ioc->name, __func__, count, int_status);
3200 return -EFAULT;
3201}
3202
3203/**
3204 * _base_wait_for_doorbell_not_used - waiting for doorbell to not be in use
3205 * @ioc: per adapter object
3206 * @timeout: timeout in second
3207 * @sleep_flag: CAN_SLEEP or NO_SLEEP
3208 *
3209 * Returns 0 for success, non-zero for failure.
3210 *
3211 */
3212static int
3213_base_wait_for_doorbell_not_used(struct MPT3SAS_ADAPTER *ioc, int timeout,
3214 int sleep_flag)
3215{
3216 u32 cntdn, count;
3217 u32 doorbell_reg;
3218
3219 count = 0;
3220 cntdn = (sleep_flag == CAN_SLEEP) ? 1000*timeout : 2000*timeout;
3221 do {
3222 doorbell_reg = readl(&ioc->chip->Doorbell);
3223 if (!(doorbell_reg & MPI2_DOORBELL_USED)) {
3224 dhsprintk(ioc, pr_info(MPT3SAS_FMT
3225 "%s: successful count(%d), timeout(%d)\n",
3226 ioc->name, __func__, count, timeout));
3227 return 0;
3228 }
3229 if (sleep_flag == CAN_SLEEP)
3230 usleep_range(1000, 1500);
3231 else
3232 udelay(500);
3233 count++;
3234 } while (--cntdn);
3235
3236 pr_err(MPT3SAS_FMT
3237 "%s: failed due to timeout count(%d), doorbell_reg(%x)!\n",
3238 ioc->name, __func__, count, doorbell_reg);
3239 return -EFAULT;
3240}
3241
3242/**
3243 * _base_send_ioc_reset - send doorbell reset
3244 * @ioc: per adapter object
3245 * @reset_type: currently only supports: MPI2_FUNCTION_IOC_MESSAGE_UNIT_RESET
3246 * @timeout: timeout in second
3247 * @sleep_flag: CAN_SLEEP or NO_SLEEP
3248 *
3249 * Returns 0 for success, non-zero for failure.
3250 */
3251static int
3252_base_send_ioc_reset(struct MPT3SAS_ADAPTER *ioc, u8 reset_type, int timeout,
3253 int sleep_flag)
3254{
3255 u32 ioc_state;
3256 int r = 0;
3257
3258 if (reset_type != MPI2_FUNCTION_IOC_MESSAGE_UNIT_RESET) {
3259 pr_err(MPT3SAS_FMT "%s: unknown reset_type\n",
3260 ioc->name, __func__);
3261 return -EFAULT;
3262 }
3263
3264 if (!(ioc->facts.IOCCapabilities &
3265 MPI2_IOCFACTS_CAPABILITY_EVENT_REPLAY))
3266 return -EFAULT;
3267
3268 pr_info(MPT3SAS_FMT "sending message unit reset !!\n", ioc->name);
3269
3270 writel(reset_type << MPI2_DOORBELL_FUNCTION_SHIFT,
3271 &ioc->chip->Doorbell);
3272 if ((_base_wait_for_doorbell_ack(ioc, 15, sleep_flag))) {
3273 r = -EFAULT;
3274 goto out;
3275 }
3276 ioc_state = _base_wait_on_iocstate(ioc, MPI2_IOC_STATE_READY,
3277 timeout, sleep_flag);
3278 if (ioc_state) {
3279 pr_err(MPT3SAS_FMT
3280 "%s: failed going to ready state (ioc_state=0x%x)\n",
3281 ioc->name, __func__, ioc_state);
3282 r = -EFAULT;
3283 goto out;
3284 }
3285 out:
3286 pr_info(MPT3SAS_FMT "message unit reset: %s\n",
3287 ioc->name, ((r == 0) ? "SUCCESS" : "FAILED"));
3288 return r;
3289}
3290
3291/**
3292 * _base_handshake_req_reply_wait - send request thru doorbell interface
3293 * @ioc: per adapter object
3294 * @request_bytes: request length
3295 * @request: pointer having request payload
3296 * @reply_bytes: reply length
3297 * @reply: pointer to reply payload
3298 * @timeout: timeout in second
3299 * @sleep_flag: CAN_SLEEP or NO_SLEEP
3300 *
3301 * Returns 0 for success, non-zero for failure.
3302 */
3303static int
3304_base_handshake_req_reply_wait(struct MPT3SAS_ADAPTER *ioc, int request_bytes,
3305 u32 *request, int reply_bytes, u16 *reply, int timeout, int sleep_flag)
3306{
3307 MPI2DefaultReply_t *default_reply = (MPI2DefaultReply_t *)reply;
3308 int i;
3309 u8 failed;
3310 u16 dummy;
3311 __le32 *mfp;
3312
3313 /* make sure doorbell is not in use */
3314 if ((readl(&ioc->chip->Doorbell) & MPI2_DOORBELL_USED)) {
3315 pr_err(MPT3SAS_FMT
3316 "doorbell is in use (line=%d)\n",
3317 ioc->name, __LINE__);
3318 return -EFAULT;
3319 }
3320
3321 /* clear pending doorbell interrupts from previous state changes */
3322 if (readl(&ioc->chip->HostInterruptStatus) &
3323 MPI2_HIS_IOC2SYS_DB_STATUS)
3324 writel(0, &ioc->chip->HostInterruptStatus);
3325
3326 /* send message to ioc */
3327 writel(((MPI2_FUNCTION_HANDSHAKE<<MPI2_DOORBELL_FUNCTION_SHIFT) |
3328 ((request_bytes/4)<<MPI2_DOORBELL_ADD_DWORDS_SHIFT)),
3329 &ioc->chip->Doorbell);
3330
3331 if ((_base_wait_for_doorbell_int(ioc, 5, NO_SLEEP))) {
3332 pr_err(MPT3SAS_FMT
3333 "doorbell handshake int failed (line=%d)\n",
3334 ioc->name, __LINE__);
3335 return -EFAULT;
3336 }
3337 writel(0, &ioc->chip->HostInterruptStatus);
3338
3339 if ((_base_wait_for_doorbell_ack(ioc, 5, sleep_flag))) {
3340 pr_err(MPT3SAS_FMT
3341 "doorbell handshake ack failed (line=%d)\n",
3342 ioc->name, __LINE__);
3343 return -EFAULT;
3344 }
3345
3346 /* send message 32-bits at a time */
3347 for (i = 0, failed = 0; i < request_bytes/4 && !failed; i++) {
3348 writel(cpu_to_le32(request[i]), &ioc->chip->Doorbell);
3349 if ((_base_wait_for_doorbell_ack(ioc, 5, sleep_flag)))
3350 failed = 1;
3351 }
3352
3353 if (failed) {
3354 pr_err(MPT3SAS_FMT
3355 "doorbell handshake sending request failed (line=%d)\n",
3356 ioc->name, __LINE__);
3357 return -EFAULT;
3358 }
3359
3360 /* now wait for the reply */
3361 if ((_base_wait_for_doorbell_int(ioc, timeout, sleep_flag))) {
3362 pr_err(MPT3SAS_FMT
3363 "doorbell handshake int failed (line=%d)\n",
3364 ioc->name, __LINE__);
3365 return -EFAULT;
3366 }
3367
3368 /* read the first two 16-bits, it gives the total length of the reply */
3369 reply[0] = le16_to_cpu(readl(&ioc->chip->Doorbell)
3370 & MPI2_DOORBELL_DATA_MASK);
3371 writel(0, &ioc->chip->HostInterruptStatus);
3372 if ((_base_wait_for_doorbell_int(ioc, 5, sleep_flag))) {
3373 pr_err(MPT3SAS_FMT
3374 "doorbell handshake int failed (line=%d)\n",
3375 ioc->name, __LINE__);
3376 return -EFAULT;
3377 }
3378 reply[1] = le16_to_cpu(readl(&ioc->chip->Doorbell)
3379 & MPI2_DOORBELL_DATA_MASK);
3380 writel(0, &ioc->chip->HostInterruptStatus);
3381
3382 for (i = 2; i < default_reply->MsgLength * 2; i++) {
3383 if ((_base_wait_for_doorbell_int(ioc, 5, sleep_flag))) {
3384 pr_err(MPT3SAS_FMT
3385 "doorbell handshake int failed (line=%d)\n",
3386 ioc->name, __LINE__);
3387 return -EFAULT;
3388 }
3389 if (i >= reply_bytes/2) /* overflow case */
3390 dummy = readl(&ioc->chip->Doorbell);
3391 else
3392 reply[i] = le16_to_cpu(readl(&ioc->chip->Doorbell)
3393 & MPI2_DOORBELL_DATA_MASK);
3394 writel(0, &ioc->chip->HostInterruptStatus);
3395 }
3396
3397 _base_wait_for_doorbell_int(ioc, 5, sleep_flag);
3398 if (_base_wait_for_doorbell_not_used(ioc, 5, sleep_flag) != 0) {
3399 dhsprintk(ioc, pr_info(MPT3SAS_FMT
3400 "doorbell is in use (line=%d)\n", ioc->name, __LINE__));
3401 }
3402 writel(0, &ioc->chip->HostInterruptStatus);
3403
3404 if (ioc->logging_level & MPT_DEBUG_INIT) {
3405 mfp = (__le32 *)reply;
3406 pr_info("\toffset:data\n");
3407 for (i = 0; i < reply_bytes/4; i++)
3408 pr_info("\t[0x%02x]:%08x\n", i*4,
3409 le32_to_cpu(mfp[i]));
3410 }
3411 return 0;
3412}
3413
3414/**
3415 * mpt3sas_base_sas_iounit_control - send sas iounit control to FW
3416 * @ioc: per adapter object
3417 * @mpi_reply: the reply payload from FW
3418 * @mpi_request: the request payload sent to FW
3419 *
3420 * The SAS IO Unit Control Request message allows the host to perform low-level
3421 * operations, such as resets on the PHYs of the IO Unit, also allows the host
3422 * to obtain the IOC assigned device handles for a device if it has other
3423 * identifying information about the device, in addition allows the host to
3424 * remove IOC resources associated with the device.
3425 *
3426 * Returns 0 for success, non-zero for failure.
3427 */
3428int
3429mpt3sas_base_sas_iounit_control(struct MPT3SAS_ADAPTER *ioc,
3430 Mpi2SasIoUnitControlReply_t *mpi_reply,
3431 Mpi2SasIoUnitControlRequest_t *mpi_request)
3432{
3433 u16 smid;
3434 u32 ioc_state;
3435 unsigned long timeleft;
Dan Carpentereb445522014-12-04 13:57:05 +03003436 bool issue_reset = false;
Sreekanth Reddyf92363d2012-11-30 07:44:21 +05303437 int rc;
3438 void *request;
3439 u16 wait_state_count;
3440
3441 dinitprintk(ioc, pr_info(MPT3SAS_FMT "%s\n", ioc->name,
3442 __func__));
3443
3444 mutex_lock(&ioc->base_cmds.mutex);
3445
3446 if (ioc->base_cmds.status != MPT3_CMD_NOT_USED) {
3447 pr_err(MPT3SAS_FMT "%s: base_cmd in use\n",
3448 ioc->name, __func__);
3449 rc = -EAGAIN;
3450 goto out;
3451 }
3452
3453 wait_state_count = 0;
3454 ioc_state = mpt3sas_base_get_iocstate(ioc, 1);
3455 while (ioc_state != MPI2_IOC_STATE_OPERATIONAL) {
3456 if (wait_state_count++ == 10) {
3457 pr_err(MPT3SAS_FMT
3458 "%s: failed due to ioc not operational\n",
3459 ioc->name, __func__);
3460 rc = -EFAULT;
3461 goto out;
3462 }
3463 ssleep(1);
3464 ioc_state = mpt3sas_base_get_iocstate(ioc, 1);
3465 pr_info(MPT3SAS_FMT
3466 "%s: waiting for operational state(count=%d)\n",
3467 ioc->name, __func__, wait_state_count);
3468 }
3469
3470 smid = mpt3sas_base_get_smid(ioc, ioc->base_cb_idx);
3471 if (!smid) {
3472 pr_err(MPT3SAS_FMT "%s: failed obtaining a smid\n",
3473 ioc->name, __func__);
3474 rc = -EAGAIN;
3475 goto out;
3476 }
3477
3478 rc = 0;
3479 ioc->base_cmds.status = MPT3_CMD_PENDING;
3480 request = mpt3sas_base_get_msg_frame(ioc, smid);
3481 ioc->base_cmds.smid = smid;
3482 memcpy(request, mpi_request, sizeof(Mpi2SasIoUnitControlRequest_t));
3483 if (mpi_request->Operation == MPI2_SAS_OP_PHY_HARD_RESET ||
3484 mpi_request->Operation == MPI2_SAS_OP_PHY_LINK_RESET)
3485 ioc->ioc_link_reset_in_progress = 1;
3486 init_completion(&ioc->base_cmds.done);
3487 mpt3sas_base_put_smid_default(ioc, smid);
3488 timeleft = wait_for_completion_timeout(&ioc->base_cmds.done,
3489 msecs_to_jiffies(10000));
3490 if ((mpi_request->Operation == MPI2_SAS_OP_PHY_HARD_RESET ||
3491 mpi_request->Operation == MPI2_SAS_OP_PHY_LINK_RESET) &&
3492 ioc->ioc_link_reset_in_progress)
3493 ioc->ioc_link_reset_in_progress = 0;
3494 if (!(ioc->base_cmds.status & MPT3_CMD_COMPLETE)) {
3495 pr_err(MPT3SAS_FMT "%s: timeout\n",
3496 ioc->name, __func__);
3497 _debug_dump_mf(mpi_request,
3498 sizeof(Mpi2SasIoUnitControlRequest_t)/4);
3499 if (!(ioc->base_cmds.status & MPT3_CMD_RESET))
Dan Carpentereb445522014-12-04 13:57:05 +03003500 issue_reset = true;
Sreekanth Reddyf92363d2012-11-30 07:44:21 +05303501 goto issue_host_reset;
3502 }
3503 if (ioc->base_cmds.status & MPT3_CMD_REPLY_VALID)
3504 memcpy(mpi_reply, ioc->base_cmds.reply,
3505 sizeof(Mpi2SasIoUnitControlReply_t));
3506 else
3507 memset(mpi_reply, 0, sizeof(Mpi2SasIoUnitControlReply_t));
3508 ioc->base_cmds.status = MPT3_CMD_NOT_USED;
3509 goto out;
3510
3511 issue_host_reset:
3512 if (issue_reset)
3513 mpt3sas_base_hard_reset_handler(ioc, CAN_SLEEP,
3514 FORCE_BIG_HAMMER);
3515 ioc->base_cmds.status = MPT3_CMD_NOT_USED;
3516 rc = -EFAULT;
3517 out:
3518 mutex_unlock(&ioc->base_cmds.mutex);
3519 return rc;
3520}
3521
3522/**
3523 * mpt3sas_base_scsi_enclosure_processor - sending request to sep device
3524 * @ioc: per adapter object
3525 * @mpi_reply: the reply payload from FW
3526 * @mpi_request: the request payload sent to FW
3527 *
3528 * The SCSI Enclosure Processor request message causes the IOC to
3529 * communicate with SES devices to control LED status signals.
3530 *
3531 * Returns 0 for success, non-zero for failure.
3532 */
3533int
3534mpt3sas_base_scsi_enclosure_processor(struct MPT3SAS_ADAPTER *ioc,
3535 Mpi2SepReply_t *mpi_reply, Mpi2SepRequest_t *mpi_request)
3536{
3537 u16 smid;
3538 u32 ioc_state;
3539 unsigned long timeleft;
Dan Carpentereb445522014-12-04 13:57:05 +03003540 bool issue_reset = false;
Sreekanth Reddyf92363d2012-11-30 07:44:21 +05303541 int rc;
3542 void *request;
3543 u16 wait_state_count;
3544
3545 dinitprintk(ioc, pr_info(MPT3SAS_FMT "%s\n", ioc->name,
3546 __func__));
3547
3548 mutex_lock(&ioc->base_cmds.mutex);
3549
3550 if (ioc->base_cmds.status != MPT3_CMD_NOT_USED) {
3551 pr_err(MPT3SAS_FMT "%s: base_cmd in use\n",
3552 ioc->name, __func__);
3553 rc = -EAGAIN;
3554 goto out;
3555 }
3556
3557 wait_state_count = 0;
3558 ioc_state = mpt3sas_base_get_iocstate(ioc, 1);
3559 while (ioc_state != MPI2_IOC_STATE_OPERATIONAL) {
3560 if (wait_state_count++ == 10) {
3561 pr_err(MPT3SAS_FMT
3562 "%s: failed due to ioc not operational\n",
3563 ioc->name, __func__);
3564 rc = -EFAULT;
3565 goto out;
3566 }
3567 ssleep(1);
3568 ioc_state = mpt3sas_base_get_iocstate(ioc, 1);
3569 pr_info(MPT3SAS_FMT
3570 "%s: waiting for operational state(count=%d)\n",
3571 ioc->name,
3572 __func__, wait_state_count);
3573 }
3574
3575 smid = mpt3sas_base_get_smid(ioc, ioc->base_cb_idx);
3576 if (!smid) {
3577 pr_err(MPT3SAS_FMT "%s: failed obtaining a smid\n",
3578 ioc->name, __func__);
3579 rc = -EAGAIN;
3580 goto out;
3581 }
3582
3583 rc = 0;
3584 ioc->base_cmds.status = MPT3_CMD_PENDING;
3585 request = mpt3sas_base_get_msg_frame(ioc, smid);
3586 ioc->base_cmds.smid = smid;
3587 memcpy(request, mpi_request, sizeof(Mpi2SepReply_t));
3588 init_completion(&ioc->base_cmds.done);
3589 mpt3sas_base_put_smid_default(ioc, smid);
3590 timeleft = wait_for_completion_timeout(&ioc->base_cmds.done,
3591 msecs_to_jiffies(10000));
3592 if (!(ioc->base_cmds.status & MPT3_CMD_COMPLETE)) {
3593 pr_err(MPT3SAS_FMT "%s: timeout\n",
3594 ioc->name, __func__);
3595 _debug_dump_mf(mpi_request,
3596 sizeof(Mpi2SepRequest_t)/4);
3597 if (!(ioc->base_cmds.status & MPT3_CMD_RESET))
Dan Carpentereb445522014-12-04 13:57:05 +03003598 issue_reset = false;
Sreekanth Reddyf92363d2012-11-30 07:44:21 +05303599 goto issue_host_reset;
3600 }
3601 if (ioc->base_cmds.status & MPT3_CMD_REPLY_VALID)
3602 memcpy(mpi_reply, ioc->base_cmds.reply,
3603 sizeof(Mpi2SepReply_t));
3604 else
3605 memset(mpi_reply, 0, sizeof(Mpi2SepReply_t));
3606 ioc->base_cmds.status = MPT3_CMD_NOT_USED;
3607 goto out;
3608
3609 issue_host_reset:
3610 if (issue_reset)
3611 mpt3sas_base_hard_reset_handler(ioc, CAN_SLEEP,
3612 FORCE_BIG_HAMMER);
3613 ioc->base_cmds.status = MPT3_CMD_NOT_USED;
3614 rc = -EFAULT;
3615 out:
3616 mutex_unlock(&ioc->base_cmds.mutex);
3617 return rc;
3618}
3619
3620/**
3621 * _base_get_port_facts - obtain port facts reply and save in ioc
3622 * @ioc: per adapter object
3623 * @sleep_flag: CAN_SLEEP or NO_SLEEP
3624 *
3625 * Returns 0 for success, non-zero for failure.
3626 */
3627static int
3628_base_get_port_facts(struct MPT3SAS_ADAPTER *ioc, int port, int sleep_flag)
3629{
3630 Mpi2PortFactsRequest_t mpi_request;
3631 Mpi2PortFactsReply_t mpi_reply;
3632 struct mpt3sas_port_facts *pfacts;
3633 int mpi_reply_sz, mpi_request_sz, r;
3634
3635 dinitprintk(ioc, pr_info(MPT3SAS_FMT "%s\n", ioc->name,
3636 __func__));
3637
3638 mpi_reply_sz = sizeof(Mpi2PortFactsReply_t);
3639 mpi_request_sz = sizeof(Mpi2PortFactsRequest_t);
3640 memset(&mpi_request, 0, mpi_request_sz);
3641 mpi_request.Function = MPI2_FUNCTION_PORT_FACTS;
3642 mpi_request.PortNumber = port;
3643 r = _base_handshake_req_reply_wait(ioc, mpi_request_sz,
3644 (u32 *)&mpi_request, mpi_reply_sz, (u16 *)&mpi_reply, 5, CAN_SLEEP);
3645
3646 if (r != 0) {
3647 pr_err(MPT3SAS_FMT "%s: handshake failed (r=%d)\n",
3648 ioc->name, __func__, r);
3649 return r;
3650 }
3651
3652 pfacts = &ioc->pfacts[port];
3653 memset(pfacts, 0, sizeof(struct mpt3sas_port_facts));
3654 pfacts->PortNumber = mpi_reply.PortNumber;
3655 pfacts->VP_ID = mpi_reply.VP_ID;
3656 pfacts->VF_ID = mpi_reply.VF_ID;
3657 pfacts->MaxPostedCmdBuffers =
3658 le16_to_cpu(mpi_reply.MaxPostedCmdBuffers);
3659
3660 return 0;
3661}
3662
3663/**
3664 * _base_get_ioc_facts - obtain ioc facts reply and save in ioc
3665 * @ioc: per adapter object
3666 * @sleep_flag: CAN_SLEEP or NO_SLEEP
3667 *
3668 * Returns 0 for success, non-zero for failure.
3669 */
3670static int
3671_base_get_ioc_facts(struct MPT3SAS_ADAPTER *ioc, int sleep_flag)
3672{
3673 Mpi2IOCFactsRequest_t mpi_request;
3674 Mpi2IOCFactsReply_t mpi_reply;
3675 struct mpt3sas_facts *facts;
3676 int mpi_reply_sz, mpi_request_sz, r;
3677
3678 dinitprintk(ioc, pr_info(MPT3SAS_FMT "%s\n", ioc->name,
3679 __func__));
3680
3681 mpi_reply_sz = sizeof(Mpi2IOCFactsReply_t);
3682 mpi_request_sz = sizeof(Mpi2IOCFactsRequest_t);
3683 memset(&mpi_request, 0, mpi_request_sz);
3684 mpi_request.Function = MPI2_FUNCTION_IOC_FACTS;
3685 r = _base_handshake_req_reply_wait(ioc, mpi_request_sz,
3686 (u32 *)&mpi_request, mpi_reply_sz, (u16 *)&mpi_reply, 5, CAN_SLEEP);
3687
3688 if (r != 0) {
3689 pr_err(MPT3SAS_FMT "%s: handshake failed (r=%d)\n",
3690 ioc->name, __func__, r);
3691 return r;
3692 }
3693
3694 facts = &ioc->facts;
3695 memset(facts, 0, sizeof(struct mpt3sas_facts));
3696 facts->MsgVersion = le16_to_cpu(mpi_reply.MsgVersion);
3697 facts->HeaderVersion = le16_to_cpu(mpi_reply.HeaderVersion);
3698 facts->VP_ID = mpi_reply.VP_ID;
3699 facts->VF_ID = mpi_reply.VF_ID;
3700 facts->IOCExceptions = le16_to_cpu(mpi_reply.IOCExceptions);
3701 facts->MaxChainDepth = mpi_reply.MaxChainDepth;
3702 facts->WhoInit = mpi_reply.WhoInit;
3703 facts->NumberOfPorts = mpi_reply.NumberOfPorts;
3704 facts->MaxMSIxVectors = mpi_reply.MaxMSIxVectors;
3705 facts->RequestCredit = le16_to_cpu(mpi_reply.RequestCredit);
3706 facts->MaxReplyDescriptorPostQueueDepth =
3707 le16_to_cpu(mpi_reply.MaxReplyDescriptorPostQueueDepth);
3708 facts->ProductID = le16_to_cpu(mpi_reply.ProductID);
3709 facts->IOCCapabilities = le32_to_cpu(mpi_reply.IOCCapabilities);
3710 if ((facts->IOCCapabilities & MPI2_IOCFACTS_CAPABILITY_INTEGRATED_RAID))
3711 ioc->ir_firmware = 1;
Sreekanth Reddy9b05c912014-09-12 15:35:31 +05303712 if ((facts->IOCCapabilities &
3713 MPI2_IOCFACTS_CAPABILITY_RDPQ_ARRAY_CAPABLE))
3714 ioc->rdpq_array_capable = 1;
Sreekanth Reddyf92363d2012-11-30 07:44:21 +05303715 facts->FWVersion.Word = le32_to_cpu(mpi_reply.FWVersion.Word);
3716 facts->IOCRequestFrameSize =
3717 le16_to_cpu(mpi_reply.IOCRequestFrameSize);
3718 facts->MaxInitiators = le16_to_cpu(mpi_reply.MaxInitiators);
3719 facts->MaxTargets = le16_to_cpu(mpi_reply.MaxTargets);
3720 ioc->shost->max_id = -1;
3721 facts->MaxSasExpanders = le16_to_cpu(mpi_reply.MaxSasExpanders);
3722 facts->MaxEnclosures = le16_to_cpu(mpi_reply.MaxEnclosures);
3723 facts->ProtocolFlags = le16_to_cpu(mpi_reply.ProtocolFlags);
3724 facts->HighPriorityCredit =
3725 le16_to_cpu(mpi_reply.HighPriorityCredit);
3726 facts->ReplyFrameSize = mpi_reply.ReplyFrameSize;
3727 facts->MaxDevHandle = le16_to_cpu(mpi_reply.MaxDevHandle);
3728
3729 dinitprintk(ioc, pr_info(MPT3SAS_FMT
3730 "hba queue depth(%d), max chains per io(%d)\n",
3731 ioc->name, facts->RequestCredit,
3732 facts->MaxChainDepth));
3733 dinitprintk(ioc, pr_info(MPT3SAS_FMT
3734 "request frame size(%d), reply frame size(%d)\n", ioc->name,
3735 facts->IOCRequestFrameSize * 4, facts->ReplyFrameSize * 4));
3736 return 0;
3737}
3738
3739/**
3740 * _base_send_ioc_init - send ioc_init to firmware
3741 * @ioc: per adapter object
3742 * @sleep_flag: CAN_SLEEP or NO_SLEEP
3743 *
3744 * Returns 0 for success, non-zero for failure.
3745 */
3746static int
3747_base_send_ioc_init(struct MPT3SAS_ADAPTER *ioc, int sleep_flag)
3748{
3749 Mpi2IOCInitRequest_t mpi_request;
3750 Mpi2IOCInitReply_t mpi_reply;
Sreekanth Reddy9b05c912014-09-12 15:35:31 +05303751 int i, r = 0;
Sreekanth Reddyf92363d2012-11-30 07:44:21 +05303752 struct timeval current_time;
3753 u16 ioc_status;
Sreekanth Reddy9b05c912014-09-12 15:35:31 +05303754 u32 reply_post_free_array_sz = 0;
3755 Mpi2IOCInitRDPQArrayEntry *reply_post_free_array = NULL;
3756 dma_addr_t reply_post_free_array_dma;
Sreekanth Reddyf92363d2012-11-30 07:44:21 +05303757
3758 dinitprintk(ioc, pr_info(MPT3SAS_FMT "%s\n", ioc->name,
3759 __func__));
3760
3761 memset(&mpi_request, 0, sizeof(Mpi2IOCInitRequest_t));
3762 mpi_request.Function = MPI2_FUNCTION_IOC_INIT;
3763 mpi_request.WhoInit = MPI2_WHOINIT_HOST_DRIVER;
3764 mpi_request.VF_ID = 0; /* TODO */
3765 mpi_request.VP_ID = 0;
3766 mpi_request.MsgVersion = cpu_to_le16(MPI2_VERSION);
3767 mpi_request.HeaderVersion = cpu_to_le16(MPI2_HEADER_VERSION);
3768
3769 if (_base_is_controller_msix_enabled(ioc))
3770 mpi_request.HostMSIxVectors = ioc->reply_queue_count;
3771 mpi_request.SystemRequestFrameSize = cpu_to_le16(ioc->request_sz/4);
3772 mpi_request.ReplyDescriptorPostQueueDepth =
3773 cpu_to_le16(ioc->reply_post_queue_depth);
3774 mpi_request.ReplyFreeQueueDepth =
3775 cpu_to_le16(ioc->reply_free_queue_depth);
3776
3777 mpi_request.SenseBufferAddressHigh =
3778 cpu_to_le32((u64)ioc->sense_dma >> 32);
3779 mpi_request.SystemReplyAddressHigh =
3780 cpu_to_le32((u64)ioc->reply_dma >> 32);
3781 mpi_request.SystemRequestFrameBaseAddress =
3782 cpu_to_le64((u64)ioc->request_dma);
3783 mpi_request.ReplyFreeQueueAddress =
3784 cpu_to_le64((u64)ioc->reply_free_dma);
Sreekanth Reddyf92363d2012-11-30 07:44:21 +05303785
Sreekanth Reddy9b05c912014-09-12 15:35:31 +05303786 if (ioc->rdpq_array_enable) {
3787 reply_post_free_array_sz = ioc->reply_queue_count *
3788 sizeof(Mpi2IOCInitRDPQArrayEntry);
3789 reply_post_free_array = pci_alloc_consistent(ioc->pdev,
3790 reply_post_free_array_sz, &reply_post_free_array_dma);
3791 if (!reply_post_free_array) {
3792 pr_err(MPT3SAS_FMT
3793 "reply_post_free_array: pci_alloc_consistent failed\n",
3794 ioc->name);
3795 r = -ENOMEM;
3796 goto out;
3797 }
3798 memset(reply_post_free_array, 0, reply_post_free_array_sz);
3799 for (i = 0; i < ioc->reply_queue_count; i++)
3800 reply_post_free_array[i].RDPQBaseAddress =
3801 cpu_to_le64(
3802 (u64)ioc->reply_post[i].reply_post_free_dma);
3803 mpi_request.MsgFlags = MPI2_IOCINIT_MSGFLAG_RDPQ_ARRAY_MODE;
3804 mpi_request.ReplyDescriptorPostQueueAddress =
3805 cpu_to_le64((u64)reply_post_free_array_dma);
3806 } else {
3807 mpi_request.ReplyDescriptorPostQueueAddress =
3808 cpu_to_le64((u64)ioc->reply_post[0].reply_post_free_dma);
3809 }
Sreekanth Reddyf92363d2012-11-30 07:44:21 +05303810
3811 /* This time stamp specifies number of milliseconds
3812 * since epoch ~ midnight January 1, 1970.
3813 */
3814 do_gettimeofday(&current_time);
3815 mpi_request.TimeStamp = cpu_to_le64((u64)current_time.tv_sec * 1000 +
3816 (current_time.tv_usec / 1000));
3817
3818 if (ioc->logging_level & MPT_DEBUG_INIT) {
3819 __le32 *mfp;
3820 int i;
3821
3822 mfp = (__le32 *)&mpi_request;
3823 pr_info("\toffset:data\n");
3824 for (i = 0; i < sizeof(Mpi2IOCInitRequest_t)/4; i++)
3825 pr_info("\t[0x%02x]:%08x\n", i*4,
3826 le32_to_cpu(mfp[i]));
3827 }
3828
3829 r = _base_handshake_req_reply_wait(ioc,
3830 sizeof(Mpi2IOCInitRequest_t), (u32 *)&mpi_request,
3831 sizeof(Mpi2IOCInitReply_t), (u16 *)&mpi_reply, 10,
3832 sleep_flag);
3833
3834 if (r != 0) {
3835 pr_err(MPT3SAS_FMT "%s: handshake failed (r=%d)\n",
3836 ioc->name, __func__, r);
Sreekanth Reddy9b05c912014-09-12 15:35:31 +05303837 goto out;
Sreekanth Reddyf92363d2012-11-30 07:44:21 +05303838 }
3839
3840 ioc_status = le16_to_cpu(mpi_reply.IOCStatus) & MPI2_IOCSTATUS_MASK;
3841 if (ioc_status != MPI2_IOCSTATUS_SUCCESS ||
3842 mpi_reply.IOCLogInfo) {
3843 pr_err(MPT3SAS_FMT "%s: failed\n", ioc->name, __func__);
3844 r = -EIO;
3845 }
3846
Sreekanth Reddy9b05c912014-09-12 15:35:31 +05303847out:
3848 if (reply_post_free_array)
3849 pci_free_consistent(ioc->pdev, reply_post_free_array_sz,
3850 reply_post_free_array,
3851 reply_post_free_array_dma);
3852 return r;
Sreekanth Reddyf92363d2012-11-30 07:44:21 +05303853}
3854
3855/**
3856 * mpt3sas_port_enable_done - command completion routine for port enable
3857 * @ioc: per adapter object
3858 * @smid: system request message index
3859 * @msix_index: MSIX table index supplied by the OS
3860 * @reply: reply message frame(lower 32bit addr)
3861 *
3862 * Return 1 meaning mf should be freed from _base_interrupt
3863 * 0 means the mf is freed from this function.
3864 */
3865u8
3866mpt3sas_port_enable_done(struct MPT3SAS_ADAPTER *ioc, u16 smid, u8 msix_index,
3867 u32 reply)
3868{
3869 MPI2DefaultReply_t *mpi_reply;
3870 u16 ioc_status;
3871
3872 if (ioc->port_enable_cmds.status == MPT3_CMD_NOT_USED)
3873 return 1;
3874
3875 mpi_reply = mpt3sas_base_get_reply_virt_addr(ioc, reply);
3876 if (!mpi_reply)
3877 return 1;
3878
3879 if (mpi_reply->Function != MPI2_FUNCTION_PORT_ENABLE)
3880 return 1;
3881
3882 ioc->port_enable_cmds.status &= ~MPT3_CMD_PENDING;
3883 ioc->port_enable_cmds.status |= MPT3_CMD_COMPLETE;
3884 ioc->port_enable_cmds.status |= MPT3_CMD_REPLY_VALID;
3885 memcpy(ioc->port_enable_cmds.reply, mpi_reply, mpi_reply->MsgLength*4);
3886 ioc_status = le16_to_cpu(mpi_reply->IOCStatus) & MPI2_IOCSTATUS_MASK;
3887 if (ioc_status != MPI2_IOCSTATUS_SUCCESS)
3888 ioc->port_enable_failed = 1;
3889
3890 if (ioc->is_driver_loading) {
3891 if (ioc_status == MPI2_IOCSTATUS_SUCCESS) {
3892 mpt3sas_port_enable_complete(ioc);
3893 return 1;
3894 } else {
3895 ioc->start_scan_failed = ioc_status;
3896 ioc->start_scan = 0;
3897 return 1;
3898 }
3899 }
3900 complete(&ioc->port_enable_cmds.done);
3901 return 1;
3902}
3903
3904/**
3905 * _base_send_port_enable - send port_enable(discovery stuff) to firmware
3906 * @ioc: per adapter object
3907 * @sleep_flag: CAN_SLEEP or NO_SLEEP
3908 *
3909 * Returns 0 for success, non-zero for failure.
3910 */
3911static int
3912_base_send_port_enable(struct MPT3SAS_ADAPTER *ioc, int sleep_flag)
3913{
3914 Mpi2PortEnableRequest_t *mpi_request;
3915 Mpi2PortEnableReply_t *mpi_reply;
3916 unsigned long timeleft;
3917 int r = 0;
3918 u16 smid;
3919 u16 ioc_status;
3920
3921 pr_info(MPT3SAS_FMT "sending port enable !!\n", ioc->name);
3922
3923 if (ioc->port_enable_cmds.status & MPT3_CMD_PENDING) {
3924 pr_err(MPT3SAS_FMT "%s: internal command already in use\n",
3925 ioc->name, __func__);
3926 return -EAGAIN;
3927 }
3928
3929 smid = mpt3sas_base_get_smid(ioc, ioc->port_enable_cb_idx);
3930 if (!smid) {
3931 pr_err(MPT3SAS_FMT "%s: failed obtaining a smid\n",
3932 ioc->name, __func__);
3933 return -EAGAIN;
3934 }
3935
3936 ioc->port_enable_cmds.status = MPT3_CMD_PENDING;
3937 mpi_request = mpt3sas_base_get_msg_frame(ioc, smid);
3938 ioc->port_enable_cmds.smid = smid;
3939 memset(mpi_request, 0, sizeof(Mpi2PortEnableRequest_t));
3940 mpi_request->Function = MPI2_FUNCTION_PORT_ENABLE;
3941
3942 init_completion(&ioc->port_enable_cmds.done);
3943 mpt3sas_base_put_smid_default(ioc, smid);
3944 timeleft = wait_for_completion_timeout(&ioc->port_enable_cmds.done,
3945 300*HZ);
3946 if (!(ioc->port_enable_cmds.status & MPT3_CMD_COMPLETE)) {
3947 pr_err(MPT3SAS_FMT "%s: timeout\n",
3948 ioc->name, __func__);
3949 _debug_dump_mf(mpi_request,
3950 sizeof(Mpi2PortEnableRequest_t)/4);
3951 if (ioc->port_enable_cmds.status & MPT3_CMD_RESET)
3952 r = -EFAULT;
3953 else
3954 r = -ETIME;
3955 goto out;
3956 }
3957
3958 mpi_reply = ioc->port_enable_cmds.reply;
3959 ioc_status = le16_to_cpu(mpi_reply->IOCStatus) & MPI2_IOCSTATUS_MASK;
3960 if (ioc_status != MPI2_IOCSTATUS_SUCCESS) {
3961 pr_err(MPT3SAS_FMT "%s: failed with (ioc_status=0x%08x)\n",
3962 ioc->name, __func__, ioc_status);
3963 r = -EFAULT;
3964 goto out;
3965 }
3966
3967 out:
3968 ioc->port_enable_cmds.status = MPT3_CMD_NOT_USED;
3969 pr_info(MPT3SAS_FMT "port enable: %s\n", ioc->name, ((r == 0) ?
3970 "SUCCESS" : "FAILED"));
3971 return r;
3972}
3973
3974/**
3975 * mpt3sas_port_enable - initiate firmware discovery (don't wait for reply)
3976 * @ioc: per adapter object
3977 *
3978 * Returns 0 for success, non-zero for failure.
3979 */
3980int
3981mpt3sas_port_enable(struct MPT3SAS_ADAPTER *ioc)
3982{
3983 Mpi2PortEnableRequest_t *mpi_request;
3984 u16 smid;
3985
3986 pr_info(MPT3SAS_FMT "sending port enable !!\n", ioc->name);
3987
3988 if (ioc->port_enable_cmds.status & MPT3_CMD_PENDING) {
3989 pr_err(MPT3SAS_FMT "%s: internal command already in use\n",
3990 ioc->name, __func__);
3991 return -EAGAIN;
3992 }
3993
3994 smid = mpt3sas_base_get_smid(ioc, ioc->port_enable_cb_idx);
3995 if (!smid) {
3996 pr_err(MPT3SAS_FMT "%s: failed obtaining a smid\n",
3997 ioc->name, __func__);
3998 return -EAGAIN;
3999 }
4000
4001 ioc->port_enable_cmds.status = MPT3_CMD_PENDING;
4002 mpi_request = mpt3sas_base_get_msg_frame(ioc, smid);
4003 ioc->port_enable_cmds.smid = smid;
4004 memset(mpi_request, 0, sizeof(Mpi2PortEnableRequest_t));
4005 mpi_request->Function = MPI2_FUNCTION_PORT_ENABLE;
4006
4007 mpt3sas_base_put_smid_default(ioc, smid);
4008 return 0;
4009}
4010
4011/**
4012 * _base_determine_wait_on_discovery - desposition
4013 * @ioc: per adapter object
4014 *
4015 * Decide whether to wait on discovery to complete. Used to either
4016 * locate boot device, or report volumes ahead of physical devices.
4017 *
4018 * Returns 1 for wait, 0 for don't wait
4019 */
4020static int
4021_base_determine_wait_on_discovery(struct MPT3SAS_ADAPTER *ioc)
4022{
4023 /* We wait for discovery to complete if IR firmware is loaded.
4024 * The sas topology events arrive before PD events, so we need time to
4025 * turn on the bit in ioc->pd_handles to indicate PD
4026 * Also, it maybe required to report Volumes ahead of physical
4027 * devices when MPI2_IOCPAGE8_IRFLAGS_LOW_VOLUME_MAPPING is set.
4028 */
4029 if (ioc->ir_firmware)
4030 return 1;
4031
4032 /* if no Bios, then we don't need to wait */
4033 if (!ioc->bios_pg3.BiosVersion)
4034 return 0;
4035
4036 /* Bios is present, then we drop down here.
4037 *
4038 * If there any entries in the Bios Page 2, then we wait
4039 * for discovery to complete.
4040 */
4041
4042 /* Current Boot Device */
4043 if ((ioc->bios_pg2.CurrentBootDeviceForm &
4044 MPI2_BIOSPAGE2_FORM_MASK) ==
4045 MPI2_BIOSPAGE2_FORM_NO_DEVICE_SPECIFIED &&
4046 /* Request Boot Device */
4047 (ioc->bios_pg2.ReqBootDeviceForm &
4048 MPI2_BIOSPAGE2_FORM_MASK) ==
4049 MPI2_BIOSPAGE2_FORM_NO_DEVICE_SPECIFIED &&
4050 /* Alternate Request Boot Device */
4051 (ioc->bios_pg2.ReqAltBootDeviceForm &
4052 MPI2_BIOSPAGE2_FORM_MASK) ==
4053 MPI2_BIOSPAGE2_FORM_NO_DEVICE_SPECIFIED)
4054 return 0;
4055
4056 return 1;
4057}
4058
4059/**
4060 * _base_unmask_events - turn on notification for this event
4061 * @ioc: per adapter object
4062 * @event: firmware event
4063 *
4064 * The mask is stored in ioc->event_masks.
4065 */
4066static void
4067_base_unmask_events(struct MPT3SAS_ADAPTER *ioc, u16 event)
4068{
4069 u32 desired_event;
4070
4071 if (event >= 128)
4072 return;
4073
4074 desired_event = (1 << (event % 32));
4075
4076 if (event < 32)
4077 ioc->event_masks[0] &= ~desired_event;
4078 else if (event < 64)
4079 ioc->event_masks[1] &= ~desired_event;
4080 else if (event < 96)
4081 ioc->event_masks[2] &= ~desired_event;
4082 else if (event < 128)
4083 ioc->event_masks[3] &= ~desired_event;
4084}
4085
4086/**
4087 * _base_event_notification - send event notification
4088 * @ioc: per adapter object
4089 * @sleep_flag: CAN_SLEEP or NO_SLEEP
4090 *
4091 * Returns 0 for success, non-zero for failure.
4092 */
4093static int
4094_base_event_notification(struct MPT3SAS_ADAPTER *ioc, int sleep_flag)
4095{
4096 Mpi2EventNotificationRequest_t *mpi_request;
4097 unsigned long timeleft;
4098 u16 smid;
4099 int r = 0;
4100 int i;
4101
4102 dinitprintk(ioc, pr_info(MPT3SAS_FMT "%s\n", ioc->name,
4103 __func__));
4104
4105 if (ioc->base_cmds.status & MPT3_CMD_PENDING) {
4106 pr_err(MPT3SAS_FMT "%s: internal command already in use\n",
4107 ioc->name, __func__);
4108 return -EAGAIN;
4109 }
4110
4111 smid = mpt3sas_base_get_smid(ioc, ioc->base_cb_idx);
4112 if (!smid) {
4113 pr_err(MPT3SAS_FMT "%s: failed obtaining a smid\n",
4114 ioc->name, __func__);
4115 return -EAGAIN;
4116 }
4117 ioc->base_cmds.status = MPT3_CMD_PENDING;
4118 mpi_request = mpt3sas_base_get_msg_frame(ioc, smid);
4119 ioc->base_cmds.smid = smid;
4120 memset(mpi_request, 0, sizeof(Mpi2EventNotificationRequest_t));
4121 mpi_request->Function = MPI2_FUNCTION_EVENT_NOTIFICATION;
4122 mpi_request->VF_ID = 0; /* TODO */
4123 mpi_request->VP_ID = 0;
4124 for (i = 0; i < MPI2_EVENT_NOTIFY_EVENTMASK_WORDS; i++)
4125 mpi_request->EventMasks[i] =
4126 cpu_to_le32(ioc->event_masks[i]);
4127 init_completion(&ioc->base_cmds.done);
4128 mpt3sas_base_put_smid_default(ioc, smid);
4129 timeleft = wait_for_completion_timeout(&ioc->base_cmds.done, 30*HZ);
4130 if (!(ioc->base_cmds.status & MPT3_CMD_COMPLETE)) {
4131 pr_err(MPT3SAS_FMT "%s: timeout\n",
4132 ioc->name, __func__);
4133 _debug_dump_mf(mpi_request,
4134 sizeof(Mpi2EventNotificationRequest_t)/4);
4135 if (ioc->base_cmds.status & MPT3_CMD_RESET)
4136 r = -EFAULT;
4137 else
4138 r = -ETIME;
4139 } else
4140 dinitprintk(ioc, pr_info(MPT3SAS_FMT "%s: complete\n",
4141 ioc->name, __func__));
4142 ioc->base_cmds.status = MPT3_CMD_NOT_USED;
4143 return r;
4144}
4145
4146/**
4147 * mpt3sas_base_validate_event_type - validating event types
4148 * @ioc: per adapter object
4149 * @event: firmware event
4150 *
4151 * This will turn on firmware event notification when application
4152 * ask for that event. We don't mask events that are already enabled.
4153 */
4154void
4155mpt3sas_base_validate_event_type(struct MPT3SAS_ADAPTER *ioc, u32 *event_type)
4156{
4157 int i, j;
4158 u32 event_mask, desired_event;
4159 u8 send_update_to_fw;
4160
4161 for (i = 0, send_update_to_fw = 0; i <
4162 MPI2_EVENT_NOTIFY_EVENTMASK_WORDS; i++) {
4163 event_mask = ~event_type[i];
4164 desired_event = 1;
4165 for (j = 0; j < 32; j++) {
4166 if (!(event_mask & desired_event) &&
4167 (ioc->event_masks[i] & desired_event)) {
4168 ioc->event_masks[i] &= ~desired_event;
4169 send_update_to_fw = 1;
4170 }
4171 desired_event = (desired_event << 1);
4172 }
4173 }
4174
4175 if (!send_update_to_fw)
4176 return;
4177
4178 mutex_lock(&ioc->base_cmds.mutex);
4179 _base_event_notification(ioc, CAN_SLEEP);
4180 mutex_unlock(&ioc->base_cmds.mutex);
4181}
4182
4183/**
4184 * _base_diag_reset - the "big hammer" start of day reset
4185 * @ioc: per adapter object
4186 * @sleep_flag: CAN_SLEEP or NO_SLEEP
4187 *
4188 * Returns 0 for success, non-zero for failure.
4189 */
4190static int
4191_base_diag_reset(struct MPT3SAS_ADAPTER *ioc, int sleep_flag)
4192{
4193 u32 host_diagnostic;
4194 u32 ioc_state;
4195 u32 count;
4196 u32 hcb_size;
4197
4198 pr_info(MPT3SAS_FMT "sending diag reset !!\n", ioc->name);
4199
4200 drsprintk(ioc, pr_info(MPT3SAS_FMT "clear interrupts\n",
4201 ioc->name));
4202
4203 count = 0;
4204 do {
4205 /* Write magic sequence to WriteSequence register
4206 * Loop until in diagnostic mode
4207 */
4208 drsprintk(ioc, pr_info(MPT3SAS_FMT
4209 "write magic sequence\n", ioc->name));
4210 writel(MPI2_WRSEQ_FLUSH_KEY_VALUE, &ioc->chip->WriteSequence);
4211 writel(MPI2_WRSEQ_1ST_KEY_VALUE, &ioc->chip->WriteSequence);
4212 writel(MPI2_WRSEQ_2ND_KEY_VALUE, &ioc->chip->WriteSequence);
4213 writel(MPI2_WRSEQ_3RD_KEY_VALUE, &ioc->chip->WriteSequence);
4214 writel(MPI2_WRSEQ_4TH_KEY_VALUE, &ioc->chip->WriteSequence);
4215 writel(MPI2_WRSEQ_5TH_KEY_VALUE, &ioc->chip->WriteSequence);
4216 writel(MPI2_WRSEQ_6TH_KEY_VALUE, &ioc->chip->WriteSequence);
4217
4218 /* wait 100 msec */
4219 if (sleep_flag == CAN_SLEEP)
4220 msleep(100);
4221 else
4222 mdelay(100);
4223
4224 if (count++ > 20)
4225 goto out;
4226
4227 host_diagnostic = readl(&ioc->chip->HostDiagnostic);
4228 drsprintk(ioc, pr_info(MPT3SAS_FMT
4229 "wrote magic sequence: count(%d), host_diagnostic(0x%08x)\n",
4230 ioc->name, count, host_diagnostic));
4231
4232 } while ((host_diagnostic & MPI2_DIAG_DIAG_WRITE_ENABLE) == 0);
4233
4234 hcb_size = readl(&ioc->chip->HCBSize);
4235
4236 drsprintk(ioc, pr_info(MPT3SAS_FMT "diag reset: issued\n",
4237 ioc->name));
4238 writel(host_diagnostic | MPI2_DIAG_RESET_ADAPTER,
4239 &ioc->chip->HostDiagnostic);
4240
Sreekanth Reddyb453ff82013-06-29 03:51:19 +05304241 /*This delay allows the chip PCIe hardware time to finish reset tasks*/
4242 if (sleep_flag == CAN_SLEEP)
4243 msleep(MPI2_HARD_RESET_PCIE_FIRST_READ_DELAY_MICRO_SEC/1000);
4244 else
4245 mdelay(MPI2_HARD_RESET_PCIE_FIRST_READ_DELAY_MICRO_SEC/1000);
Sreekanth Reddyf92363d2012-11-30 07:44:21 +05304246
Sreekanth Reddyb453ff82013-06-29 03:51:19 +05304247 /* Approximately 300 second max wait */
4248 for (count = 0; count < (300000000 /
4249 MPI2_HARD_RESET_PCIE_SECOND_READ_DELAY_MICRO_SEC); count++) {
Sreekanth Reddyf92363d2012-11-30 07:44:21 +05304250
4251 host_diagnostic = readl(&ioc->chip->HostDiagnostic);
4252
4253 if (host_diagnostic == 0xFFFFFFFF)
4254 goto out;
4255 if (!(host_diagnostic & MPI2_DIAG_RESET_ADAPTER))
4256 break;
4257
Sreekanth Reddyb453ff82013-06-29 03:51:19 +05304258 /* Wait to pass the second read delay window */
Sreekanth Reddyf92363d2012-11-30 07:44:21 +05304259 if (sleep_flag == CAN_SLEEP)
Sreekanth Reddyb453ff82013-06-29 03:51:19 +05304260 msleep(MPI2_HARD_RESET_PCIE_SECOND_READ_DELAY_MICRO_SEC
4261 / 1000);
Sreekanth Reddyf92363d2012-11-30 07:44:21 +05304262 else
Sreekanth Reddyb453ff82013-06-29 03:51:19 +05304263 mdelay(MPI2_HARD_RESET_PCIE_SECOND_READ_DELAY_MICRO_SEC
4264 / 1000);
Sreekanth Reddyf92363d2012-11-30 07:44:21 +05304265 }
4266
4267 if (host_diagnostic & MPI2_DIAG_HCB_MODE) {
4268
4269 drsprintk(ioc, pr_info(MPT3SAS_FMT
4270 "restart the adapter assuming the HCB Address points to good F/W\n",
4271 ioc->name));
4272 host_diagnostic &= ~MPI2_DIAG_BOOT_DEVICE_SELECT_MASK;
4273 host_diagnostic |= MPI2_DIAG_BOOT_DEVICE_SELECT_HCDW;
4274 writel(host_diagnostic, &ioc->chip->HostDiagnostic);
4275
4276 drsprintk(ioc, pr_info(MPT3SAS_FMT
4277 "re-enable the HCDW\n", ioc->name));
4278 writel(hcb_size | MPI2_HCB_SIZE_HCB_ENABLE,
4279 &ioc->chip->HCBSize);
4280 }
4281
4282 drsprintk(ioc, pr_info(MPT3SAS_FMT "restart the adapter\n",
4283 ioc->name));
4284 writel(host_diagnostic & ~MPI2_DIAG_HOLD_IOC_RESET,
4285 &ioc->chip->HostDiagnostic);
4286
4287 drsprintk(ioc, pr_info(MPT3SAS_FMT
4288 "disable writes to the diagnostic register\n", ioc->name));
4289 writel(MPI2_WRSEQ_FLUSH_KEY_VALUE, &ioc->chip->WriteSequence);
4290
4291 drsprintk(ioc, pr_info(MPT3SAS_FMT
4292 "Wait for FW to go to the READY state\n", ioc->name));
4293 ioc_state = _base_wait_on_iocstate(ioc, MPI2_IOC_STATE_READY, 20,
4294 sleep_flag);
4295 if (ioc_state) {
4296 pr_err(MPT3SAS_FMT
4297 "%s: failed going to ready state (ioc_state=0x%x)\n",
4298 ioc->name, __func__, ioc_state);
4299 goto out;
4300 }
4301
4302 pr_info(MPT3SAS_FMT "diag reset: SUCCESS\n", ioc->name);
4303 return 0;
4304
4305 out:
4306 pr_err(MPT3SAS_FMT "diag reset: FAILED\n", ioc->name);
4307 return -EFAULT;
4308}
4309
4310/**
4311 * _base_make_ioc_ready - put controller in READY state
4312 * @ioc: per adapter object
4313 * @sleep_flag: CAN_SLEEP or NO_SLEEP
4314 * @type: FORCE_BIG_HAMMER or SOFT_RESET
4315 *
4316 * Returns 0 for success, non-zero for failure.
4317 */
4318static int
4319_base_make_ioc_ready(struct MPT3SAS_ADAPTER *ioc, int sleep_flag,
4320 enum reset_type type)
4321{
4322 u32 ioc_state;
4323 int rc;
4324 int count;
4325
4326 dinitprintk(ioc, pr_info(MPT3SAS_FMT "%s\n", ioc->name,
4327 __func__));
4328
4329 if (ioc->pci_error_recovery)
4330 return 0;
4331
4332 ioc_state = mpt3sas_base_get_iocstate(ioc, 0);
4333 dhsprintk(ioc, pr_info(MPT3SAS_FMT "%s: ioc_state(0x%08x)\n",
4334 ioc->name, __func__, ioc_state));
4335
4336 /* if in RESET state, it should move to READY state shortly */
4337 count = 0;
4338 if ((ioc_state & MPI2_IOC_STATE_MASK) == MPI2_IOC_STATE_RESET) {
4339 while ((ioc_state & MPI2_IOC_STATE_MASK) !=
4340 MPI2_IOC_STATE_READY) {
4341 if (count++ == 10) {
4342 pr_err(MPT3SAS_FMT
4343 "%s: failed going to ready state (ioc_state=0x%x)\n",
4344 ioc->name, __func__, ioc_state);
4345 return -EFAULT;
4346 }
4347 if (sleep_flag == CAN_SLEEP)
4348 ssleep(1);
4349 else
4350 mdelay(1000);
4351 ioc_state = mpt3sas_base_get_iocstate(ioc, 0);
4352 }
4353 }
4354
4355 if ((ioc_state & MPI2_IOC_STATE_MASK) == MPI2_IOC_STATE_READY)
4356 return 0;
4357
4358 if (ioc_state & MPI2_DOORBELL_USED) {
4359 dhsprintk(ioc, pr_info(MPT3SAS_FMT
4360 "unexpected doorbell active!\n",
4361 ioc->name));
4362 goto issue_diag_reset;
4363 }
4364
4365 if ((ioc_state & MPI2_IOC_STATE_MASK) == MPI2_IOC_STATE_FAULT) {
4366 mpt3sas_base_fault_info(ioc, ioc_state &
4367 MPI2_DOORBELL_DATA_MASK);
4368 goto issue_diag_reset;
4369 }
4370
4371 if (type == FORCE_BIG_HAMMER)
4372 goto issue_diag_reset;
4373
4374 if ((ioc_state & MPI2_IOC_STATE_MASK) == MPI2_IOC_STATE_OPERATIONAL)
4375 if (!(_base_send_ioc_reset(ioc,
4376 MPI2_FUNCTION_IOC_MESSAGE_UNIT_RESET, 15, CAN_SLEEP))) {
4377 return 0;
4378 }
4379
4380 issue_diag_reset:
4381 rc = _base_diag_reset(ioc, CAN_SLEEP);
4382 return rc;
4383}
4384
4385/**
4386 * _base_make_ioc_operational - put controller in OPERATIONAL state
4387 * @ioc: per adapter object
4388 * @sleep_flag: CAN_SLEEP or NO_SLEEP
4389 *
4390 * Returns 0 for success, non-zero for failure.
4391 */
4392static int
4393_base_make_ioc_operational(struct MPT3SAS_ADAPTER *ioc, int sleep_flag)
4394{
4395 int r, i;
4396 unsigned long flags;
4397 u32 reply_address;
4398 u16 smid;
4399 struct _tr_list *delayed_tr, *delayed_tr_next;
4400 struct adapter_reply_queue *reply_q;
4401 long reply_post_free;
Sreekanth Reddy9b05c912014-09-12 15:35:31 +05304402 u32 reply_post_free_sz, index = 0;
Sreekanth Reddyf92363d2012-11-30 07:44:21 +05304403
4404 dinitprintk(ioc, pr_info(MPT3SAS_FMT "%s\n", ioc->name,
4405 __func__));
4406
4407 /* clean the delayed target reset list */
4408 list_for_each_entry_safe(delayed_tr, delayed_tr_next,
4409 &ioc->delayed_tr_list, list) {
4410 list_del(&delayed_tr->list);
4411 kfree(delayed_tr);
4412 }
4413
4414
4415 list_for_each_entry_safe(delayed_tr, delayed_tr_next,
4416 &ioc->delayed_tr_volume_list, list) {
4417 list_del(&delayed_tr->list);
4418 kfree(delayed_tr);
4419 }
4420
4421 /* initialize the scsi lookup free list */
4422 spin_lock_irqsave(&ioc->scsi_lookup_lock, flags);
4423 INIT_LIST_HEAD(&ioc->free_list);
4424 smid = 1;
4425 for (i = 0; i < ioc->scsiio_depth; i++, smid++) {
4426 INIT_LIST_HEAD(&ioc->scsi_lookup[i].chain_list);
4427 ioc->scsi_lookup[i].cb_idx = 0xFF;
4428 ioc->scsi_lookup[i].smid = smid;
4429 ioc->scsi_lookup[i].scmd = NULL;
4430 list_add_tail(&ioc->scsi_lookup[i].tracker_list,
4431 &ioc->free_list);
4432 }
4433
4434 /* hi-priority queue */
4435 INIT_LIST_HEAD(&ioc->hpr_free_list);
4436 smid = ioc->hi_priority_smid;
4437 for (i = 0; i < ioc->hi_priority_depth; i++, smid++) {
4438 ioc->hpr_lookup[i].cb_idx = 0xFF;
4439 ioc->hpr_lookup[i].smid = smid;
4440 list_add_tail(&ioc->hpr_lookup[i].tracker_list,
4441 &ioc->hpr_free_list);
4442 }
4443
4444 /* internal queue */
4445 INIT_LIST_HEAD(&ioc->internal_free_list);
4446 smid = ioc->internal_smid;
4447 for (i = 0; i < ioc->internal_depth; i++, smid++) {
4448 ioc->internal_lookup[i].cb_idx = 0xFF;
4449 ioc->internal_lookup[i].smid = smid;
4450 list_add_tail(&ioc->internal_lookup[i].tracker_list,
4451 &ioc->internal_free_list);
4452 }
4453
4454 /* chain pool */
4455 INIT_LIST_HEAD(&ioc->free_chain_list);
4456 for (i = 0; i < ioc->chain_depth; i++)
4457 list_add_tail(&ioc->chain_lookup[i].tracker_list,
4458 &ioc->free_chain_list);
4459
4460 spin_unlock_irqrestore(&ioc->scsi_lookup_lock, flags);
4461
4462 /* initialize Reply Free Queue */
4463 for (i = 0, reply_address = (u32)ioc->reply_dma ;
4464 i < ioc->reply_free_queue_depth ; i++, reply_address +=
4465 ioc->reply_sz)
4466 ioc->reply_free[i] = cpu_to_le32(reply_address);
4467
4468 /* initialize reply queues */
4469 if (ioc->is_driver_loading)
4470 _base_assign_reply_queues(ioc);
4471
4472 /* initialize Reply Post Free Queue */
Sreekanth Reddyf92363d2012-11-30 07:44:21 +05304473 reply_post_free_sz = ioc->reply_post_queue_depth *
4474 sizeof(Mpi2DefaultReplyDescriptor_t);
Sreekanth Reddy9b05c912014-09-12 15:35:31 +05304475 reply_post_free = (long)ioc->reply_post[index].reply_post_free;
Sreekanth Reddyf92363d2012-11-30 07:44:21 +05304476 list_for_each_entry(reply_q, &ioc->reply_queue_list, list) {
4477 reply_q->reply_post_host_index = 0;
4478 reply_q->reply_post_free = (Mpi2ReplyDescriptorsUnion_t *)
4479 reply_post_free;
4480 for (i = 0; i < ioc->reply_post_queue_depth; i++)
4481 reply_q->reply_post_free[i].Words =
4482 cpu_to_le64(ULLONG_MAX);
4483 if (!_base_is_controller_msix_enabled(ioc))
4484 goto skip_init_reply_post_free_queue;
Sreekanth Reddy9b05c912014-09-12 15:35:31 +05304485 /*
4486 * If RDPQ is enabled, switch to the next allocation.
4487 * Otherwise advance within the contiguous region.
4488 */
4489 if (ioc->rdpq_array_enable)
4490 reply_post_free = (long)
4491 ioc->reply_post[++index].reply_post_free;
4492 else
4493 reply_post_free += reply_post_free_sz;
Sreekanth Reddyf92363d2012-11-30 07:44:21 +05304494 }
4495 skip_init_reply_post_free_queue:
4496
4497 r = _base_send_ioc_init(ioc, sleep_flag);
4498 if (r)
4499 return r;
4500
4501 /* initialize reply free host index */
4502 ioc->reply_free_host_index = ioc->reply_free_queue_depth - 1;
4503 writel(ioc->reply_free_host_index, &ioc->chip->ReplyFreeHostIndex);
4504
4505 /* initialize reply post host index */
4506 list_for_each_entry(reply_q, &ioc->reply_queue_list, list) {
4507 writel(reply_q->msix_index << MPI2_RPHI_MSIX_INDEX_SHIFT,
4508 &ioc->chip->ReplyPostHostIndex);
4509 if (!_base_is_controller_msix_enabled(ioc))
4510 goto skip_init_reply_post_host_index;
4511 }
4512
4513 skip_init_reply_post_host_index:
4514
4515 _base_unmask_interrupts(ioc);
4516 r = _base_event_notification(ioc, sleep_flag);
4517 if (r)
4518 return r;
4519
4520 if (sleep_flag == CAN_SLEEP)
4521 _base_static_config_pages(ioc);
4522
4523
4524 if (ioc->is_driver_loading) {
4525 ioc->wait_for_discovery_to_complete =
4526 _base_determine_wait_on_discovery(ioc);
4527
4528 return r; /* scan_start and scan_finished support */
4529 }
4530
4531 r = _base_send_port_enable(ioc, sleep_flag);
4532 if (r)
4533 return r;
4534
4535 return r;
4536}
4537
4538/**
4539 * mpt3sas_base_free_resources - free resources controller resources
4540 * @ioc: per adapter object
4541 *
4542 * Return nothing.
4543 */
4544void
4545mpt3sas_base_free_resources(struct MPT3SAS_ADAPTER *ioc)
4546{
4547 struct pci_dev *pdev = ioc->pdev;
4548
4549 dexitprintk(ioc, pr_info(MPT3SAS_FMT "%s\n", ioc->name,
4550 __func__));
4551
Joe Lawrencecf9bd21a2013-08-08 16:45:39 -04004552 if (ioc->chip_phys && ioc->chip) {
4553 _base_mask_interrupts(ioc);
4554 ioc->shost_recovery = 1;
4555 _base_make_ioc_ready(ioc, CAN_SLEEP, SOFT_RESET);
4556 ioc->shost_recovery = 0;
4557 }
4558
Sreekanth Reddyf92363d2012-11-30 07:44:21 +05304559 _base_free_irq(ioc);
4560 _base_disable_msix(ioc);
Joe Lawrencecf9bd21a2013-08-08 16:45:39 -04004561
4562 if (ioc->chip_phys && ioc->chip)
Sreekanth Reddyf92363d2012-11-30 07:44:21 +05304563 iounmap(ioc->chip);
4564 ioc->chip_phys = 0;
Joe Lawrencecf9bd21a2013-08-08 16:45:39 -04004565
4566 if (pci_is_enabled(pdev)) {
4567 pci_release_selected_regions(ioc->pdev, ioc->bars);
4568 pci_disable_pcie_error_reporting(pdev);
4569 pci_disable_device(pdev);
4570 }
Sreekanth Reddyf92363d2012-11-30 07:44:21 +05304571 return;
4572}
4573
4574/**
4575 * mpt3sas_base_attach - attach controller instance
4576 * @ioc: per adapter object
4577 *
4578 * Returns 0 for success, non-zero for failure.
4579 */
4580int
4581mpt3sas_base_attach(struct MPT3SAS_ADAPTER *ioc)
4582{
4583 int r, i;
4584 int cpu_id, last_cpu_id = 0;
4585
4586 dinitprintk(ioc, pr_info(MPT3SAS_FMT "%s\n", ioc->name,
4587 __func__));
4588
4589 /* setup cpu_msix_table */
4590 ioc->cpu_count = num_online_cpus();
4591 for_each_online_cpu(cpu_id)
4592 last_cpu_id = cpu_id;
4593 ioc->cpu_msix_table_sz = last_cpu_id + 1;
4594 ioc->cpu_msix_table = kzalloc(ioc->cpu_msix_table_sz, GFP_KERNEL);
4595 ioc->reply_queue_count = 1;
4596 if (!ioc->cpu_msix_table) {
4597 dfailprintk(ioc, pr_info(MPT3SAS_FMT
4598 "allocation for cpu_msix_table failed!!!\n",
4599 ioc->name));
4600 r = -ENOMEM;
4601 goto out_free_resources;
4602 }
4603
Sreekanth Reddy9b05c912014-09-12 15:35:31 +05304604 ioc->rdpq_array_enable_assigned = 0;
4605 ioc->dma_mask = 0;
Sreekanth Reddyf92363d2012-11-30 07:44:21 +05304606 r = mpt3sas_base_map_resources(ioc);
4607 if (r)
4608 goto out_free_resources;
4609
4610
4611 pci_set_drvdata(ioc->pdev, ioc->shost);
4612 r = _base_get_ioc_facts(ioc, CAN_SLEEP);
4613 if (r)
4614 goto out_free_resources;
4615
4616 /*
4617 * In SAS3.0,
4618 * SCSI_IO, SMP_PASSTHRU, SATA_PASSTHRU, Target Assist, and
4619 * Target Status - all require the IEEE formated scatter gather
4620 * elements.
4621 */
4622
4623 ioc->build_sg_scmd = &_base_build_sg_scmd_ieee;
4624 ioc->build_sg = &_base_build_sg_ieee;
4625 ioc->build_zero_len_sge = &_base_build_zero_len_sge_ieee;
4626 ioc->mpi25 = 1;
4627 ioc->sge_size_ieee = sizeof(Mpi2IeeeSgeSimple64_t);
4628
4629 /*
4630 * These function pointers for other requests that don't
4631 * the require IEEE scatter gather elements.
4632 *
4633 * For example Configuration Pages and SAS IOUNIT Control don't.
4634 */
4635 ioc->build_sg_mpi = &_base_build_sg;
4636 ioc->build_zero_len_sge_mpi = &_base_build_zero_len_sge;
4637
4638 r = _base_make_ioc_ready(ioc, CAN_SLEEP, SOFT_RESET);
4639 if (r)
4640 goto out_free_resources;
4641
4642 ioc->pfacts = kcalloc(ioc->facts.NumberOfPorts,
4643 sizeof(struct mpt3sas_port_facts), GFP_KERNEL);
4644 if (!ioc->pfacts) {
4645 r = -ENOMEM;
4646 goto out_free_resources;
4647 }
4648
4649 for (i = 0 ; i < ioc->facts.NumberOfPorts; i++) {
4650 r = _base_get_port_facts(ioc, i, CAN_SLEEP);
4651 if (r)
4652 goto out_free_resources;
4653 }
4654
4655 r = _base_allocate_memory_pools(ioc, CAN_SLEEP);
4656 if (r)
4657 goto out_free_resources;
4658
4659 init_waitqueue_head(&ioc->reset_wq);
4660
4661 /* allocate memory pd handle bitmask list */
4662 ioc->pd_handles_sz = (ioc->facts.MaxDevHandle / 8);
4663 if (ioc->facts.MaxDevHandle % 8)
4664 ioc->pd_handles_sz++;
4665 ioc->pd_handles = kzalloc(ioc->pd_handles_sz,
4666 GFP_KERNEL);
4667 if (!ioc->pd_handles) {
4668 r = -ENOMEM;
4669 goto out_free_resources;
4670 }
4671 ioc->blocking_handles = kzalloc(ioc->pd_handles_sz,
4672 GFP_KERNEL);
4673 if (!ioc->blocking_handles) {
4674 r = -ENOMEM;
4675 goto out_free_resources;
4676 }
4677
4678 ioc->fwfault_debug = mpt3sas_fwfault_debug;
4679
4680 /* base internal command bits */
4681 mutex_init(&ioc->base_cmds.mutex);
4682 ioc->base_cmds.reply = kzalloc(ioc->reply_sz, GFP_KERNEL);
4683 ioc->base_cmds.status = MPT3_CMD_NOT_USED;
4684
4685 /* port_enable command bits */
4686 ioc->port_enable_cmds.reply = kzalloc(ioc->reply_sz, GFP_KERNEL);
4687 ioc->port_enable_cmds.status = MPT3_CMD_NOT_USED;
4688
4689 /* transport internal command bits */
4690 ioc->transport_cmds.reply = kzalloc(ioc->reply_sz, GFP_KERNEL);
4691 ioc->transport_cmds.status = MPT3_CMD_NOT_USED;
4692 mutex_init(&ioc->transport_cmds.mutex);
4693
4694 /* scsih internal command bits */
4695 ioc->scsih_cmds.reply = kzalloc(ioc->reply_sz, GFP_KERNEL);
4696 ioc->scsih_cmds.status = MPT3_CMD_NOT_USED;
4697 mutex_init(&ioc->scsih_cmds.mutex);
4698
4699 /* task management internal command bits */
4700 ioc->tm_cmds.reply = kzalloc(ioc->reply_sz, GFP_KERNEL);
4701 ioc->tm_cmds.status = MPT3_CMD_NOT_USED;
4702 mutex_init(&ioc->tm_cmds.mutex);
4703
4704 /* config page internal command bits */
4705 ioc->config_cmds.reply = kzalloc(ioc->reply_sz, GFP_KERNEL);
4706 ioc->config_cmds.status = MPT3_CMD_NOT_USED;
4707 mutex_init(&ioc->config_cmds.mutex);
4708
4709 /* ctl module internal command bits */
4710 ioc->ctl_cmds.reply = kzalloc(ioc->reply_sz, GFP_KERNEL);
4711 ioc->ctl_cmds.sense = kzalloc(SCSI_SENSE_BUFFERSIZE, GFP_KERNEL);
4712 ioc->ctl_cmds.status = MPT3_CMD_NOT_USED;
4713 mutex_init(&ioc->ctl_cmds.mutex);
4714
4715 if (!ioc->base_cmds.reply || !ioc->transport_cmds.reply ||
4716 !ioc->scsih_cmds.reply || !ioc->tm_cmds.reply ||
4717 !ioc->config_cmds.reply || !ioc->ctl_cmds.reply ||
4718 !ioc->ctl_cmds.sense) {
4719 r = -ENOMEM;
4720 goto out_free_resources;
4721 }
4722
4723 for (i = 0; i < MPI2_EVENT_NOTIFY_EVENTMASK_WORDS; i++)
4724 ioc->event_masks[i] = -1;
4725
4726 /* here we enable the events we care about */
4727 _base_unmask_events(ioc, MPI2_EVENT_SAS_DISCOVERY);
4728 _base_unmask_events(ioc, MPI2_EVENT_SAS_BROADCAST_PRIMITIVE);
4729 _base_unmask_events(ioc, MPI2_EVENT_SAS_TOPOLOGY_CHANGE_LIST);
4730 _base_unmask_events(ioc, MPI2_EVENT_SAS_DEVICE_STATUS_CHANGE);
4731 _base_unmask_events(ioc, MPI2_EVENT_SAS_ENCL_DEVICE_STATUS_CHANGE);
4732 _base_unmask_events(ioc, MPI2_EVENT_IR_CONFIGURATION_CHANGE_LIST);
4733 _base_unmask_events(ioc, MPI2_EVENT_IR_VOLUME);
4734 _base_unmask_events(ioc, MPI2_EVENT_IR_PHYSICAL_DISK);
4735 _base_unmask_events(ioc, MPI2_EVENT_IR_OPERATION_STATUS);
4736 _base_unmask_events(ioc, MPI2_EVENT_LOG_ENTRY_ADDED);
Sreekanth Reddy2d8ce8c2015-01-12 11:38:56 +05304737 _base_unmask_events(ioc, MPI2_EVENT_TEMP_THRESHOLD);
Sreekanth Reddyf92363d2012-11-30 07:44:21 +05304738
4739 r = _base_make_ioc_operational(ioc, CAN_SLEEP);
4740 if (r)
4741 goto out_free_resources;
4742
4743 return 0;
4744
4745 out_free_resources:
4746
4747 ioc->remove_host = 1;
4748
4749 mpt3sas_base_free_resources(ioc);
4750 _base_release_memory_pools(ioc);
4751 pci_set_drvdata(ioc->pdev, NULL);
4752 kfree(ioc->cpu_msix_table);
4753 kfree(ioc->pd_handles);
4754 kfree(ioc->blocking_handles);
4755 kfree(ioc->tm_cmds.reply);
4756 kfree(ioc->transport_cmds.reply);
4757 kfree(ioc->scsih_cmds.reply);
4758 kfree(ioc->config_cmds.reply);
4759 kfree(ioc->base_cmds.reply);
4760 kfree(ioc->port_enable_cmds.reply);
4761 kfree(ioc->ctl_cmds.reply);
4762 kfree(ioc->ctl_cmds.sense);
4763 kfree(ioc->pfacts);
4764 ioc->ctl_cmds.reply = NULL;
4765 ioc->base_cmds.reply = NULL;
4766 ioc->tm_cmds.reply = NULL;
4767 ioc->scsih_cmds.reply = NULL;
4768 ioc->transport_cmds.reply = NULL;
4769 ioc->config_cmds.reply = NULL;
4770 ioc->pfacts = NULL;
4771 return r;
4772}
4773
4774
4775/**
4776 * mpt3sas_base_detach - remove controller instance
4777 * @ioc: per adapter object
4778 *
4779 * Return nothing.
4780 */
4781void
4782mpt3sas_base_detach(struct MPT3SAS_ADAPTER *ioc)
4783{
4784 dexitprintk(ioc, pr_info(MPT3SAS_FMT "%s\n", ioc->name,
4785 __func__));
4786
4787 mpt3sas_base_stop_watchdog(ioc);
4788 mpt3sas_base_free_resources(ioc);
4789 _base_release_memory_pools(ioc);
4790 pci_set_drvdata(ioc->pdev, NULL);
4791 kfree(ioc->cpu_msix_table);
4792 kfree(ioc->pd_handles);
4793 kfree(ioc->blocking_handles);
4794 kfree(ioc->pfacts);
4795 kfree(ioc->ctl_cmds.reply);
4796 kfree(ioc->ctl_cmds.sense);
4797 kfree(ioc->base_cmds.reply);
4798 kfree(ioc->port_enable_cmds.reply);
4799 kfree(ioc->tm_cmds.reply);
4800 kfree(ioc->transport_cmds.reply);
4801 kfree(ioc->scsih_cmds.reply);
4802 kfree(ioc->config_cmds.reply);
4803}
4804
4805/**
4806 * _base_reset_handler - reset callback handler (for base)
4807 * @ioc: per adapter object
4808 * @reset_phase: phase
4809 *
4810 * The handler for doing any required cleanup or initialization.
4811 *
4812 * The reset phase can be MPT3_IOC_PRE_RESET, MPT3_IOC_AFTER_RESET,
4813 * MPT3_IOC_DONE_RESET
4814 *
4815 * Return nothing.
4816 */
4817static void
4818_base_reset_handler(struct MPT3SAS_ADAPTER *ioc, int reset_phase)
4819{
4820 mpt3sas_scsih_reset_handler(ioc, reset_phase);
4821 mpt3sas_ctl_reset_handler(ioc, reset_phase);
4822 switch (reset_phase) {
4823 case MPT3_IOC_PRE_RESET:
4824 dtmprintk(ioc, pr_info(MPT3SAS_FMT
4825 "%s: MPT3_IOC_PRE_RESET\n", ioc->name, __func__));
4826 break;
4827 case MPT3_IOC_AFTER_RESET:
4828 dtmprintk(ioc, pr_info(MPT3SAS_FMT
4829 "%s: MPT3_IOC_AFTER_RESET\n", ioc->name, __func__));
4830 if (ioc->transport_cmds.status & MPT3_CMD_PENDING) {
4831 ioc->transport_cmds.status |= MPT3_CMD_RESET;
4832 mpt3sas_base_free_smid(ioc, ioc->transport_cmds.smid);
4833 complete(&ioc->transport_cmds.done);
4834 }
4835 if (ioc->base_cmds.status & MPT3_CMD_PENDING) {
4836 ioc->base_cmds.status |= MPT3_CMD_RESET;
4837 mpt3sas_base_free_smid(ioc, ioc->base_cmds.smid);
4838 complete(&ioc->base_cmds.done);
4839 }
4840 if (ioc->port_enable_cmds.status & MPT3_CMD_PENDING) {
4841 ioc->port_enable_failed = 1;
4842 ioc->port_enable_cmds.status |= MPT3_CMD_RESET;
4843 mpt3sas_base_free_smid(ioc, ioc->port_enable_cmds.smid);
4844 if (ioc->is_driver_loading) {
4845 ioc->start_scan_failed =
4846 MPI2_IOCSTATUS_INTERNAL_ERROR;
4847 ioc->start_scan = 0;
4848 ioc->port_enable_cmds.status =
4849 MPT3_CMD_NOT_USED;
4850 } else
4851 complete(&ioc->port_enable_cmds.done);
4852 }
4853 if (ioc->config_cmds.status & MPT3_CMD_PENDING) {
4854 ioc->config_cmds.status |= MPT3_CMD_RESET;
4855 mpt3sas_base_free_smid(ioc, ioc->config_cmds.smid);
4856 ioc->config_cmds.smid = USHRT_MAX;
4857 complete(&ioc->config_cmds.done);
4858 }
4859 break;
4860 case MPT3_IOC_DONE_RESET:
4861 dtmprintk(ioc, pr_info(MPT3SAS_FMT
4862 "%s: MPT3_IOC_DONE_RESET\n", ioc->name, __func__));
4863 break;
4864 }
4865}
4866
4867/**
4868 * _wait_for_commands_to_complete - reset controller
4869 * @ioc: Pointer to MPT_ADAPTER structure
4870 * @sleep_flag: CAN_SLEEP or NO_SLEEP
4871 *
4872 * This function waiting(3s) for all pending commands to complete
4873 * prior to putting controller in reset.
4874 */
4875static void
4876_wait_for_commands_to_complete(struct MPT3SAS_ADAPTER *ioc, int sleep_flag)
4877{
4878 u32 ioc_state;
4879 unsigned long flags;
4880 u16 i;
4881
4882 ioc->pending_io_count = 0;
4883 if (sleep_flag != CAN_SLEEP)
4884 return;
4885
4886 ioc_state = mpt3sas_base_get_iocstate(ioc, 0);
4887 if ((ioc_state & MPI2_IOC_STATE_MASK) != MPI2_IOC_STATE_OPERATIONAL)
4888 return;
4889
4890 /* pending command count */
4891 spin_lock_irqsave(&ioc->scsi_lookup_lock, flags);
4892 for (i = 0; i < ioc->scsiio_depth; i++)
4893 if (ioc->scsi_lookup[i].cb_idx != 0xFF)
4894 ioc->pending_io_count++;
4895 spin_unlock_irqrestore(&ioc->scsi_lookup_lock, flags);
4896
4897 if (!ioc->pending_io_count)
4898 return;
4899
4900 /* wait for pending commands to complete */
4901 wait_event_timeout(ioc->reset_wq, ioc->pending_io_count == 0, 10 * HZ);
4902}
4903
4904/**
4905 * mpt3sas_base_hard_reset_handler - reset controller
4906 * @ioc: Pointer to MPT_ADAPTER structure
4907 * @sleep_flag: CAN_SLEEP or NO_SLEEP
4908 * @type: FORCE_BIG_HAMMER or SOFT_RESET
4909 *
4910 * Returns 0 for success, non-zero for failure.
4911 */
4912int
4913mpt3sas_base_hard_reset_handler(struct MPT3SAS_ADAPTER *ioc, int sleep_flag,
4914 enum reset_type type)
4915{
4916 int r;
4917 unsigned long flags;
4918 u32 ioc_state;
4919 u8 is_fault = 0, is_trigger = 0;
4920
4921 dtmprintk(ioc, pr_info(MPT3SAS_FMT "%s: enter\n", ioc->name,
4922 __func__));
4923
4924 if (ioc->pci_error_recovery) {
4925 pr_err(MPT3SAS_FMT "%s: pci error recovery reset\n",
4926 ioc->name, __func__);
4927 r = 0;
4928 goto out_unlocked;
4929 }
4930
4931 if (mpt3sas_fwfault_debug)
4932 mpt3sas_halt_firmware(ioc);
4933
4934 /* TODO - What we really should be doing is pulling
4935 * out all the code associated with NO_SLEEP; its never used.
4936 * That is legacy code from mpt fusion driver, ported over.
4937 * I will leave this BUG_ON here for now till its been resolved.
4938 */
4939 BUG_ON(sleep_flag == NO_SLEEP);
4940
4941 /* wait for an active reset in progress to complete */
4942 if (!mutex_trylock(&ioc->reset_in_progress_mutex)) {
4943 do {
4944 ssleep(1);
4945 } while (ioc->shost_recovery == 1);
4946 dtmprintk(ioc, pr_info(MPT3SAS_FMT "%s: exit\n", ioc->name,
4947 __func__));
4948 return ioc->ioc_reset_in_progress_status;
4949 }
4950
4951 spin_lock_irqsave(&ioc->ioc_reset_in_progress_lock, flags);
4952 ioc->shost_recovery = 1;
4953 spin_unlock_irqrestore(&ioc->ioc_reset_in_progress_lock, flags);
4954
4955 if ((ioc->diag_buffer_status[MPI2_DIAG_BUF_TYPE_TRACE] &
4956 MPT3_DIAG_BUFFER_IS_REGISTERED) &&
4957 (!(ioc->diag_buffer_status[MPI2_DIAG_BUF_TYPE_TRACE] &
4958 MPT3_DIAG_BUFFER_IS_RELEASED))) {
4959 is_trigger = 1;
4960 ioc_state = mpt3sas_base_get_iocstate(ioc, 0);
4961 if ((ioc_state & MPI2_IOC_STATE_MASK) == MPI2_IOC_STATE_FAULT)
4962 is_fault = 1;
4963 }
4964 _base_reset_handler(ioc, MPT3_IOC_PRE_RESET);
4965 _wait_for_commands_to_complete(ioc, sleep_flag);
4966 _base_mask_interrupts(ioc);
4967 r = _base_make_ioc_ready(ioc, sleep_flag, type);
4968 if (r)
4969 goto out;
4970 _base_reset_handler(ioc, MPT3_IOC_AFTER_RESET);
4971
4972 /* If this hard reset is called while port enable is active, then
4973 * there is no reason to call make_ioc_operational
4974 */
4975 if (ioc->is_driver_loading && ioc->port_enable_failed) {
4976 ioc->remove_host = 1;
4977 r = -EFAULT;
4978 goto out;
4979 }
4980 r = _base_get_ioc_facts(ioc, CAN_SLEEP);
4981 if (r)
4982 goto out;
Sreekanth Reddy9b05c912014-09-12 15:35:31 +05304983
4984 if (ioc->rdpq_array_enable && !ioc->rdpq_array_capable)
4985 panic("%s: Issue occurred with flashing controller firmware."
4986 "Please reboot the system and ensure that the correct"
4987 " firmware version is running\n", ioc->name);
4988
Sreekanth Reddyf92363d2012-11-30 07:44:21 +05304989 r = _base_make_ioc_operational(ioc, sleep_flag);
4990 if (!r)
4991 _base_reset_handler(ioc, MPT3_IOC_DONE_RESET);
4992
4993 out:
4994 dtmprintk(ioc, pr_info(MPT3SAS_FMT "%s: %s\n",
4995 ioc->name, __func__, ((r == 0) ? "SUCCESS" : "FAILED")));
4996
4997 spin_lock_irqsave(&ioc->ioc_reset_in_progress_lock, flags);
4998 ioc->ioc_reset_in_progress_status = r;
4999 ioc->shost_recovery = 0;
5000 spin_unlock_irqrestore(&ioc->ioc_reset_in_progress_lock, flags);
5001 ioc->ioc_reset_count++;
5002 mutex_unlock(&ioc->reset_in_progress_mutex);
5003
5004 out_unlocked:
5005 if ((r == 0) && is_trigger) {
5006 if (is_fault)
5007 mpt3sas_trigger_master(ioc, MASTER_TRIGGER_FW_FAULT);
5008 else
5009 mpt3sas_trigger_master(ioc,
5010 MASTER_TRIGGER_ADAPTER_RESET);
5011 }
5012 dtmprintk(ioc, pr_info(MPT3SAS_FMT "%s: exit\n", ioc->name,
5013 __func__));
5014 return r;
5015}