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