]> Pileus Git - ~andy/linux/blob - drivers/scsi/mpt2sas/mpt2sas_base.c
Merge branch 'for-3.9' of git://linux-nfs.org/~bfields/linux
[~andy/linux] / 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                 default:
2027                         break;
2028                 }
2029         default:
2030                 break;
2031         }
2032 }
2033
2034 /**
2035  * _base_display_hp_branding - Display branding string
2036  * @ioc: per adapter object
2037  *
2038  * Return nothing.
2039  */
2040 static void
2041 _base_display_hp_branding(struct MPT2SAS_ADAPTER *ioc)
2042 {
2043         if (ioc->pdev->subsystem_vendor != MPT2SAS_HP_3PAR_SSVID)
2044                 return;
2045
2046         switch (ioc->pdev->device) {
2047         case MPI2_MFGPAGE_DEVID_SAS2004:
2048                 switch (ioc->pdev->subsystem_device) {
2049                 case MPT2SAS_HP_DAUGHTER_2_4_INTERNAL_SSDID:
2050                         printk(MPT2SAS_INFO_FMT "%s\n", ioc->name,
2051                             MPT2SAS_HP_DAUGHTER_2_4_INTERNAL_BRANDING);
2052                         break;
2053                 default:
2054                         break;
2055                 }
2056         case MPI2_MFGPAGE_DEVID_SAS2308_2:
2057                 switch (ioc->pdev->subsystem_device) {
2058                 case MPT2SAS_HP_2_4_INTERNAL_SSDID:
2059                         printk(MPT2SAS_INFO_FMT "%s\n", ioc->name,
2060                             MPT2SAS_HP_2_4_INTERNAL_BRANDING);
2061                         break;
2062                 case MPT2SAS_HP_2_4_EXTERNAL_SSDID:
2063                         printk(MPT2SAS_INFO_FMT "%s\n", ioc->name,
2064                             MPT2SAS_HP_2_4_EXTERNAL_BRANDING);
2065                         break;
2066                 case MPT2SAS_HP_1_4_INTERNAL_1_4_EXTERNAL_SSDID:
2067                         printk(MPT2SAS_INFO_FMT "%s\n", ioc->name,
2068                             MPT2SAS_HP_1_4_INTERNAL_1_4_EXTERNAL_BRANDING);
2069                         break;
2070                 case MPT2SAS_HP_EMBEDDED_2_4_INTERNAL_SSDID:
2071                         printk(MPT2SAS_INFO_FMT "%s\n", ioc->name,
2072                             MPT2SAS_HP_EMBEDDED_2_4_INTERNAL_BRANDING);
2073                         break;
2074                 default:
2075                         break;
2076                 }
2077         default:
2078                 break;
2079         }
2080 }
2081
2082 /**
2083  * _base_display_ioc_capabilities - Disply IOC's capabilities.
2084  * @ioc: per adapter object
2085  *
2086  * Return nothing.
2087  */
2088 static void
2089 _base_display_ioc_capabilities(struct MPT2SAS_ADAPTER *ioc)
2090 {
2091         int i = 0;
2092         char desc[16];
2093         u32 iounit_pg1_flags;
2094         u32 bios_version;
2095
2096         bios_version = le32_to_cpu(ioc->bios_pg3.BiosVersion);
2097         strncpy(desc, ioc->manu_pg0.ChipName, 16);
2098         printk(MPT2SAS_INFO_FMT "%s: FWVersion(%02d.%02d.%02d.%02d), "
2099            "ChipRevision(0x%02x), BiosVersion(%02d.%02d.%02d.%02d)\n",
2100             ioc->name, desc,
2101            (ioc->facts.FWVersion.Word & 0xFF000000) >> 24,
2102            (ioc->facts.FWVersion.Word & 0x00FF0000) >> 16,
2103            (ioc->facts.FWVersion.Word & 0x0000FF00) >> 8,
2104            ioc->facts.FWVersion.Word & 0x000000FF,
2105            ioc->pdev->revision,
2106            (bios_version & 0xFF000000) >> 24,
2107            (bios_version & 0x00FF0000) >> 16,
2108            (bios_version & 0x0000FF00) >> 8,
2109             bios_version & 0x000000FF);
2110
2111         _base_display_dell_branding(ioc);
2112         _base_display_intel_branding(ioc);
2113         _base_display_hp_branding(ioc);
2114
2115         printk(MPT2SAS_INFO_FMT "Protocol=(", ioc->name);
2116
2117         if (ioc->facts.ProtocolFlags & MPI2_IOCFACTS_PROTOCOL_SCSI_INITIATOR) {
2118                 printk("Initiator");
2119                 i++;
2120         }
2121
2122         if (ioc->facts.ProtocolFlags & MPI2_IOCFACTS_PROTOCOL_SCSI_TARGET) {
2123                 printk("%sTarget", i ? "," : "");
2124                 i++;
2125         }
2126
2127         i = 0;
2128         printk("), ");
2129         printk("Capabilities=(");
2130
2131         if (!ioc->hide_ir_msg) {
2132                 if (ioc->facts.IOCCapabilities &
2133                     MPI2_IOCFACTS_CAPABILITY_INTEGRATED_RAID) {
2134                         printk("Raid");
2135                         i++;
2136                 }
2137         }
2138
2139         if (ioc->facts.IOCCapabilities & MPI2_IOCFACTS_CAPABILITY_TLR) {
2140                 printk("%sTLR", i ? "," : "");
2141                 i++;
2142         }
2143
2144         if (ioc->facts.IOCCapabilities & MPI2_IOCFACTS_CAPABILITY_MULTICAST) {
2145                 printk("%sMulticast", i ? "," : "");
2146                 i++;
2147         }
2148
2149         if (ioc->facts.IOCCapabilities &
2150             MPI2_IOCFACTS_CAPABILITY_BIDIRECTIONAL_TARGET) {
2151                 printk("%sBIDI Target", i ? "," : "");
2152                 i++;
2153         }
2154
2155         if (ioc->facts.IOCCapabilities & MPI2_IOCFACTS_CAPABILITY_EEDP) {
2156                 printk("%sEEDP", i ? "," : "");
2157                 i++;
2158         }
2159
2160         if (ioc->facts.IOCCapabilities &
2161             MPI2_IOCFACTS_CAPABILITY_SNAPSHOT_BUFFER) {
2162                 printk("%sSnapshot Buffer", i ? "," : "");
2163                 i++;
2164         }
2165
2166         if (ioc->facts.IOCCapabilities &
2167             MPI2_IOCFACTS_CAPABILITY_DIAG_TRACE_BUFFER) {
2168                 printk("%sDiag Trace Buffer", i ? "," : "");
2169                 i++;
2170         }
2171
2172         if (ioc->facts.IOCCapabilities &
2173             MPI2_IOCFACTS_CAPABILITY_EXTENDED_BUFFER) {
2174                 printk(KERN_INFO "%sDiag Extended Buffer", i ? "," : "");
2175                 i++;
2176         }
2177
2178         if (ioc->facts.IOCCapabilities &
2179             MPI2_IOCFACTS_CAPABILITY_TASK_SET_FULL_HANDLING) {
2180                 printk("%sTask Set Full", i ? "," : "");
2181                 i++;
2182         }
2183
2184         iounit_pg1_flags = le32_to_cpu(ioc->iounit_pg1.Flags);
2185         if (!(iounit_pg1_flags & MPI2_IOUNITPAGE1_NATIVE_COMMAND_Q_DISABLE)) {
2186                 printk("%sNCQ", i ? "," : "");
2187                 i++;
2188         }
2189
2190         printk(")\n");
2191 }
2192
2193 /**
2194  * _base_update_missing_delay - change the missing delay timers
2195  * @ioc: per adapter object
2196  * @device_missing_delay: amount of time till device is reported missing
2197  * @io_missing_delay: interval IO is returned when there is a missing device
2198  *
2199  * Return nothing.
2200  *
2201  * Passed on the command line, this function will modify the device missing
2202  * delay, as well as the io missing delay. This should be called at driver
2203  * load time.
2204  */
2205 static void
2206 _base_update_missing_delay(struct MPT2SAS_ADAPTER *ioc,
2207         u16 device_missing_delay, u8 io_missing_delay)
2208 {
2209         u16 dmd, dmd_new, dmd_orignal;
2210         u8 io_missing_delay_original;
2211         u16 sz;
2212         Mpi2SasIOUnitPage1_t *sas_iounit_pg1 = NULL;
2213         Mpi2ConfigReply_t mpi_reply;
2214         u8 num_phys = 0;
2215         u16 ioc_status;
2216
2217         mpt2sas_config_get_number_hba_phys(ioc, &num_phys);
2218         if (!num_phys)
2219                 return;
2220
2221         sz = offsetof(Mpi2SasIOUnitPage1_t, PhyData) + (num_phys *
2222             sizeof(Mpi2SasIOUnit1PhyData_t));
2223         sas_iounit_pg1 = kzalloc(sz, GFP_KERNEL);
2224         if (!sas_iounit_pg1) {
2225                 printk(MPT2SAS_ERR_FMT "failure at %s:%d/%s()!\n",
2226                     ioc->name, __FILE__, __LINE__, __func__);
2227                 goto out;
2228         }
2229         if ((mpt2sas_config_get_sas_iounit_pg1(ioc, &mpi_reply,
2230             sas_iounit_pg1, sz))) {
2231                 printk(MPT2SAS_ERR_FMT "failure at %s:%d/%s()!\n",
2232                     ioc->name, __FILE__, __LINE__, __func__);
2233                 goto out;
2234         }
2235         ioc_status = le16_to_cpu(mpi_reply.IOCStatus) &
2236             MPI2_IOCSTATUS_MASK;
2237         if (ioc_status != MPI2_IOCSTATUS_SUCCESS) {
2238                 printk(MPT2SAS_ERR_FMT "failure at %s:%d/%s()!\n",
2239                     ioc->name, __FILE__, __LINE__, __func__);
2240                 goto out;
2241         }
2242
2243         /* device missing delay */
2244         dmd = sas_iounit_pg1->ReportDeviceMissingDelay;
2245         if (dmd & MPI2_SASIOUNIT1_REPORT_MISSING_UNIT_16)
2246                 dmd = (dmd & MPI2_SASIOUNIT1_REPORT_MISSING_TIMEOUT_MASK) * 16;
2247         else
2248                 dmd = dmd & MPI2_SASIOUNIT1_REPORT_MISSING_TIMEOUT_MASK;
2249         dmd_orignal = dmd;
2250         if (device_missing_delay > 0x7F) {
2251                 dmd = (device_missing_delay > 0x7F0) ? 0x7F0 :
2252                     device_missing_delay;
2253                 dmd = dmd / 16;
2254                 dmd |= MPI2_SASIOUNIT1_REPORT_MISSING_UNIT_16;
2255         } else
2256                 dmd = device_missing_delay;
2257         sas_iounit_pg1->ReportDeviceMissingDelay = dmd;
2258
2259         /* io missing delay */
2260         io_missing_delay_original = sas_iounit_pg1->IODeviceMissingDelay;
2261         sas_iounit_pg1->IODeviceMissingDelay = io_missing_delay;
2262
2263         if (!mpt2sas_config_set_sas_iounit_pg1(ioc, &mpi_reply, sas_iounit_pg1,
2264             sz)) {
2265                 if (dmd & MPI2_SASIOUNIT1_REPORT_MISSING_UNIT_16)
2266                         dmd_new = (dmd &
2267                             MPI2_SASIOUNIT1_REPORT_MISSING_TIMEOUT_MASK) * 16;
2268                 else
2269                         dmd_new =
2270                     dmd & MPI2_SASIOUNIT1_REPORT_MISSING_TIMEOUT_MASK;
2271                 printk(MPT2SAS_INFO_FMT "device_missing_delay: old(%d), "
2272                     "new(%d)\n", ioc->name, dmd_orignal, dmd_new);
2273                 printk(MPT2SAS_INFO_FMT "ioc_missing_delay: old(%d), "
2274                     "new(%d)\n", ioc->name, io_missing_delay_original,
2275                     io_missing_delay);
2276                 ioc->device_missing_delay = dmd_new;
2277                 ioc->io_missing_delay = io_missing_delay;
2278         }
2279
2280 out:
2281         kfree(sas_iounit_pg1);
2282 }
2283
2284 /**
2285  * _base_static_config_pages - static start of day config pages
2286  * @ioc: per adapter object
2287  *
2288  * Return nothing.
2289  */
2290 static void
2291 _base_static_config_pages(struct MPT2SAS_ADAPTER *ioc)
2292 {
2293         Mpi2ConfigReply_t mpi_reply;
2294         u32 iounit_pg1_flags;
2295
2296         mpt2sas_config_get_manufacturing_pg0(ioc, &mpi_reply, &ioc->manu_pg0);
2297         if (ioc->ir_firmware)
2298                 mpt2sas_config_get_manufacturing_pg10(ioc, &mpi_reply,
2299                     &ioc->manu_pg10);
2300         mpt2sas_config_get_bios_pg2(ioc, &mpi_reply, &ioc->bios_pg2);
2301         mpt2sas_config_get_bios_pg3(ioc, &mpi_reply, &ioc->bios_pg3);
2302         mpt2sas_config_get_ioc_pg8(ioc, &mpi_reply, &ioc->ioc_pg8);
2303         mpt2sas_config_get_iounit_pg0(ioc, &mpi_reply, &ioc->iounit_pg0);
2304         mpt2sas_config_get_iounit_pg1(ioc, &mpi_reply, &ioc->iounit_pg1);
2305         _base_display_ioc_capabilities(ioc);
2306
2307         /*
2308          * Enable task_set_full handling in iounit_pg1 when the
2309          * facts capabilities indicate that its supported.
2310          */
2311         iounit_pg1_flags = le32_to_cpu(ioc->iounit_pg1.Flags);
2312         if ((ioc->facts.IOCCapabilities &
2313             MPI2_IOCFACTS_CAPABILITY_TASK_SET_FULL_HANDLING))
2314                 iounit_pg1_flags &=
2315                     ~MPI2_IOUNITPAGE1_DISABLE_TASK_SET_FULL_HANDLING;
2316         else
2317                 iounit_pg1_flags |=
2318                     MPI2_IOUNITPAGE1_DISABLE_TASK_SET_FULL_HANDLING;
2319         ioc->iounit_pg1.Flags = cpu_to_le32(iounit_pg1_flags);
2320         mpt2sas_config_set_iounit_pg1(ioc, &mpi_reply, &ioc->iounit_pg1);
2321
2322 }
2323
2324 /**
2325  * _base_release_memory_pools - release memory
2326  * @ioc: per adapter object
2327  *
2328  * Free memory allocated from _base_allocate_memory_pools.
2329  *
2330  * Return nothing.
2331  */
2332 static void
2333 _base_release_memory_pools(struct MPT2SAS_ADAPTER *ioc)
2334 {
2335         int i;
2336
2337         dexitprintk(ioc, printk(MPT2SAS_INFO_FMT "%s\n", ioc->name,
2338             __func__));
2339
2340         if (ioc->request) {
2341                 pci_free_consistent(ioc->pdev, ioc->request_dma_sz,
2342                     ioc->request,  ioc->request_dma);
2343                 dexitprintk(ioc, printk(MPT2SAS_INFO_FMT "request_pool(0x%p)"
2344                     ": free\n", ioc->name, ioc->request));
2345                 ioc->request = NULL;
2346         }
2347
2348         if (ioc->sense) {
2349                 pci_pool_free(ioc->sense_dma_pool, ioc->sense, ioc->sense_dma);
2350                 if (ioc->sense_dma_pool)
2351                         pci_pool_destroy(ioc->sense_dma_pool);
2352                 dexitprintk(ioc, printk(MPT2SAS_INFO_FMT "sense_pool(0x%p)"
2353                     ": free\n", ioc->name, ioc->sense));
2354                 ioc->sense = NULL;
2355         }
2356
2357         if (ioc->reply) {
2358                 pci_pool_free(ioc->reply_dma_pool, ioc->reply, ioc->reply_dma);
2359                 if (ioc->reply_dma_pool)
2360                         pci_pool_destroy(ioc->reply_dma_pool);
2361                 dexitprintk(ioc, printk(MPT2SAS_INFO_FMT "reply_pool(0x%p)"
2362                      ": free\n", ioc->name, ioc->reply));
2363                 ioc->reply = NULL;
2364         }
2365
2366         if (ioc->reply_free) {
2367                 pci_pool_free(ioc->reply_free_dma_pool, ioc->reply_free,
2368                     ioc->reply_free_dma);
2369                 if (ioc->reply_free_dma_pool)
2370                         pci_pool_destroy(ioc->reply_free_dma_pool);
2371                 dexitprintk(ioc, printk(MPT2SAS_INFO_FMT "reply_free_pool"
2372                     "(0x%p): free\n", ioc->name, ioc->reply_free));
2373                 ioc->reply_free = NULL;
2374         }
2375
2376         if (ioc->reply_post_free) {
2377                 pci_pool_free(ioc->reply_post_free_dma_pool,
2378                     ioc->reply_post_free, ioc->reply_post_free_dma);
2379                 if (ioc->reply_post_free_dma_pool)
2380                         pci_pool_destroy(ioc->reply_post_free_dma_pool);
2381                 dexitprintk(ioc, printk(MPT2SAS_INFO_FMT
2382                     "reply_post_free_pool(0x%p): free\n", ioc->name,
2383                     ioc->reply_post_free));
2384                 ioc->reply_post_free = NULL;
2385         }
2386
2387         if (ioc->config_page) {
2388                 dexitprintk(ioc, printk(MPT2SAS_INFO_FMT
2389                     "config_page(0x%p): free\n", ioc->name,
2390                     ioc->config_page));
2391                 pci_free_consistent(ioc->pdev, ioc->config_page_sz,
2392                     ioc->config_page, ioc->config_page_dma);
2393         }
2394
2395         if (ioc->scsi_lookup) {
2396                 free_pages((ulong)ioc->scsi_lookup, ioc->scsi_lookup_pages);
2397                 ioc->scsi_lookup = NULL;
2398         }
2399         kfree(ioc->hpr_lookup);
2400         kfree(ioc->internal_lookup);
2401         if (ioc->chain_lookup) {
2402                 for (i = 0; i < ioc->chain_depth; i++) {
2403                         if (ioc->chain_lookup[i].chain_buffer)
2404                                 pci_pool_free(ioc->chain_dma_pool,
2405                                     ioc->chain_lookup[i].chain_buffer,
2406                                     ioc->chain_lookup[i].chain_buffer_dma);
2407                 }
2408                 if (ioc->chain_dma_pool)
2409                         pci_pool_destroy(ioc->chain_dma_pool);
2410                 free_pages((ulong)ioc->chain_lookup, ioc->chain_pages);
2411                 ioc->chain_lookup = NULL;
2412         }
2413 }
2414
2415
2416 /**
2417  * _base_allocate_memory_pools - allocate start of day memory pools
2418  * @ioc: per adapter object
2419  * @sleep_flag: CAN_SLEEP or NO_SLEEP
2420  *
2421  * Returns 0 success, anything else error
2422  */
2423 static int
2424 _base_allocate_memory_pools(struct MPT2SAS_ADAPTER *ioc,  int sleep_flag)
2425 {
2426         struct mpt2sas_facts *facts;
2427         u16 max_sge_elements;
2428         u16 chains_needed_per_io;
2429         u32 sz, total_sz, reply_post_free_sz;
2430         u32 retry_sz;
2431         u16 max_request_credit;
2432         int i;
2433
2434         dinitprintk(ioc, printk(MPT2SAS_INFO_FMT "%s\n", ioc->name,
2435             __func__));
2436
2437         retry_sz = 0;
2438         facts = &ioc->facts;
2439
2440         /* command line tunables  for max sgl entries */
2441         if (max_sgl_entries != -1) {
2442                 ioc->shost->sg_tablesize = (max_sgl_entries <
2443                     MPT2SAS_SG_DEPTH) ? max_sgl_entries :
2444                     MPT2SAS_SG_DEPTH;
2445         } else {
2446                 ioc->shost->sg_tablesize = MPT2SAS_SG_DEPTH;
2447         }
2448
2449         /* command line tunables  for max controller queue depth */
2450         if (max_queue_depth != -1 && max_queue_depth != 0) {
2451                 max_request_credit = min_t(u16, max_queue_depth +
2452                         ioc->hi_priority_depth + ioc->internal_depth,
2453                         facts->RequestCredit);
2454                 if (max_request_credit > MAX_HBA_QUEUE_DEPTH)
2455                         max_request_credit =  MAX_HBA_QUEUE_DEPTH;
2456         } else
2457                 max_request_credit = min_t(u16, facts->RequestCredit,
2458                     MAX_HBA_QUEUE_DEPTH);
2459
2460         ioc->hba_queue_depth = max_request_credit;
2461         ioc->hi_priority_depth = facts->HighPriorityCredit;
2462         ioc->internal_depth = ioc->hi_priority_depth + 5;
2463
2464         /* request frame size */
2465         ioc->request_sz = facts->IOCRequestFrameSize * 4;
2466
2467         /* reply frame size */
2468         ioc->reply_sz = facts->ReplyFrameSize * 4;
2469
2470  retry_allocation:
2471         total_sz = 0;
2472         /* calculate number of sg elements left over in the 1st frame */
2473         max_sge_elements = ioc->request_sz - ((sizeof(Mpi2SCSIIORequest_t) -
2474             sizeof(Mpi2SGEIOUnion_t)) + ioc->sge_size);
2475         ioc->max_sges_in_main_message = max_sge_elements/ioc->sge_size;
2476
2477         /* now do the same for a chain buffer */
2478         max_sge_elements = ioc->request_sz - ioc->sge_size;
2479         ioc->max_sges_in_chain_message = max_sge_elements/ioc->sge_size;
2480
2481         ioc->chain_offset_value_for_main_message =
2482             ((sizeof(Mpi2SCSIIORequest_t) - sizeof(Mpi2SGEIOUnion_t)) +
2483              (ioc->max_sges_in_chain_message * ioc->sge_size)) / 4;
2484
2485         /*
2486          *  MPT2SAS_SG_DEPTH = CONFIG_FUSION_MAX_SGE
2487          */
2488         chains_needed_per_io = ((ioc->shost->sg_tablesize -
2489            ioc->max_sges_in_main_message)/ioc->max_sges_in_chain_message)
2490             + 1;
2491         if (chains_needed_per_io > facts->MaxChainDepth) {
2492                 chains_needed_per_io = facts->MaxChainDepth;
2493                 ioc->shost->sg_tablesize = min_t(u16,
2494                 ioc->max_sges_in_main_message + (ioc->max_sges_in_chain_message
2495                 * chains_needed_per_io), ioc->shost->sg_tablesize);
2496         }
2497         ioc->chains_needed_per_io = chains_needed_per_io;
2498
2499         /* reply free queue sizing - taking into account for 64 FW events */
2500         ioc->reply_free_queue_depth = ioc->hba_queue_depth + 64;
2501
2502         /* align the reply post queue on the next 16 count boundary */
2503         if (!ioc->reply_free_queue_depth % 16)
2504                 ioc->reply_post_queue_depth = ioc->reply_free_queue_depth + 16;
2505         else
2506                 ioc->reply_post_queue_depth = ioc->reply_free_queue_depth +
2507                                 32 - (ioc->reply_free_queue_depth % 16);
2508         if (ioc->reply_post_queue_depth >
2509             facts->MaxReplyDescriptorPostQueueDepth) {
2510                 ioc->reply_post_queue_depth = min_t(u16,
2511                     (facts->MaxReplyDescriptorPostQueueDepth -
2512                     (facts->MaxReplyDescriptorPostQueueDepth % 16)),
2513                     (ioc->hba_queue_depth - (ioc->hba_queue_depth % 16)));
2514                 ioc->reply_free_queue_depth = ioc->reply_post_queue_depth - 16;
2515                 ioc->hba_queue_depth = ioc->reply_free_queue_depth - 64;
2516         }
2517
2518
2519         dinitprintk(ioc, printk(MPT2SAS_INFO_FMT "scatter gather: "
2520             "sge_in_main_msg(%d), sge_per_chain(%d), sge_per_io(%d), "
2521             "chains_per_io(%d)\n", ioc->name, ioc->max_sges_in_main_message,
2522             ioc->max_sges_in_chain_message, ioc->shost->sg_tablesize,
2523             ioc->chains_needed_per_io));
2524
2525         ioc->scsiio_depth = ioc->hba_queue_depth -
2526             ioc->hi_priority_depth - ioc->internal_depth;
2527
2528         /* set the scsi host can_queue depth
2529          * with some internal commands that could be outstanding
2530          */
2531         ioc->shost->can_queue = ioc->scsiio_depth;
2532         dinitprintk(ioc, printk(MPT2SAS_INFO_FMT "scsi host: "
2533             "can_queue depth (%d)\n", ioc->name, ioc->shost->can_queue));
2534
2535         /* contiguous pool for request and chains, 16 byte align, one extra "
2536          * "frame for smid=0
2537          */
2538         ioc->chain_depth = ioc->chains_needed_per_io * ioc->scsiio_depth;
2539         sz = ((ioc->scsiio_depth + 1) * ioc->request_sz);
2540
2541         /* hi-priority queue */
2542         sz += (ioc->hi_priority_depth * ioc->request_sz);
2543
2544         /* internal queue */
2545         sz += (ioc->internal_depth * ioc->request_sz);
2546
2547         ioc->request_dma_sz = sz;
2548         ioc->request = pci_alloc_consistent(ioc->pdev, sz, &ioc->request_dma);
2549         if (!ioc->request) {
2550                 printk(MPT2SAS_ERR_FMT "request pool: pci_alloc_consistent "
2551                     "failed: hba_depth(%d), chains_per_io(%d), frame_sz(%d), "
2552                     "total(%d kB)\n", ioc->name, ioc->hba_queue_depth,
2553                     ioc->chains_needed_per_io, ioc->request_sz, sz/1024);
2554                 if (ioc->scsiio_depth < MPT2SAS_SAS_QUEUE_DEPTH)
2555                         goto out;
2556                 retry_sz += 64;
2557                 ioc->hba_queue_depth = max_request_credit - retry_sz;
2558                 goto retry_allocation;
2559         }
2560
2561         if (retry_sz)
2562                 printk(MPT2SAS_ERR_FMT "request pool: pci_alloc_consistent "
2563                     "succeed: hba_depth(%d), chains_per_io(%d), frame_sz(%d), "
2564                     "total(%d kb)\n", ioc->name, ioc->hba_queue_depth,
2565                     ioc->chains_needed_per_io, ioc->request_sz, sz/1024);
2566
2567
2568         /* hi-priority queue */
2569         ioc->hi_priority = ioc->request + ((ioc->scsiio_depth + 1) *
2570             ioc->request_sz);
2571         ioc->hi_priority_dma = ioc->request_dma + ((ioc->scsiio_depth + 1) *
2572             ioc->request_sz);
2573
2574         /* internal queue */
2575         ioc->internal = ioc->hi_priority + (ioc->hi_priority_depth *
2576             ioc->request_sz);
2577         ioc->internal_dma = ioc->hi_priority_dma + (ioc->hi_priority_depth *
2578             ioc->request_sz);
2579
2580
2581         dinitprintk(ioc, printk(MPT2SAS_INFO_FMT "request pool(0x%p): "
2582             "depth(%d), frame_size(%d), pool_size(%d kB)\n", ioc->name,
2583             ioc->request, ioc->hba_queue_depth, ioc->request_sz,
2584             (ioc->hba_queue_depth * ioc->request_sz)/1024));
2585         dinitprintk(ioc, printk(MPT2SAS_INFO_FMT "request pool: dma(0x%llx)\n",
2586             ioc->name, (unsigned long long) ioc->request_dma));
2587         total_sz += sz;
2588
2589         sz = ioc->scsiio_depth * sizeof(struct scsiio_tracker);
2590         ioc->scsi_lookup_pages = get_order(sz);
2591         ioc->scsi_lookup = (struct scsiio_tracker *)__get_free_pages(
2592             GFP_KERNEL, ioc->scsi_lookup_pages);
2593         if (!ioc->scsi_lookup) {
2594                 printk(MPT2SAS_ERR_FMT "scsi_lookup: get_free_pages failed, "
2595                     "sz(%d)\n", ioc->name, (int)sz);
2596                 goto out;
2597         }
2598
2599         dinitprintk(ioc, printk(MPT2SAS_INFO_FMT "scsiio(0x%p): "
2600             "depth(%d)\n", ioc->name, ioc->request,
2601             ioc->scsiio_depth));
2602
2603         ioc->chain_depth = min_t(u32, ioc->chain_depth, MAX_CHAIN_DEPTH);
2604         sz = ioc->chain_depth * sizeof(struct chain_tracker);
2605         ioc->chain_pages = get_order(sz);
2606
2607         ioc->chain_lookup = (struct chain_tracker *)__get_free_pages(
2608             GFP_KERNEL, ioc->chain_pages);
2609         if (!ioc->chain_lookup) {
2610                 printk(MPT2SAS_ERR_FMT "chain_lookup: get_free_pages failed, "
2611                     "sz(%d)\n", ioc->name, (int)sz);
2612                 goto out;
2613         }
2614         ioc->chain_dma_pool = pci_pool_create("chain pool", ioc->pdev,
2615             ioc->request_sz, 16, 0);
2616         if (!ioc->chain_dma_pool) {
2617                 printk(MPT2SAS_ERR_FMT "chain_dma_pool: pci_pool_create "
2618                     "failed\n", ioc->name);
2619                 goto out;
2620         }
2621         for (i = 0; i < ioc->chain_depth; i++) {
2622                 ioc->chain_lookup[i].chain_buffer = pci_pool_alloc(
2623                     ioc->chain_dma_pool , GFP_KERNEL,
2624                     &ioc->chain_lookup[i].chain_buffer_dma);
2625                 if (!ioc->chain_lookup[i].chain_buffer) {
2626                         ioc->chain_depth = i;
2627                         goto chain_done;
2628                 }
2629                 total_sz += ioc->request_sz;
2630         }
2631 chain_done:
2632         dinitprintk(ioc, printk(MPT2SAS_INFO_FMT "chain pool depth"
2633             "(%d), frame_size(%d), pool_size(%d kB)\n", ioc->name,
2634             ioc->chain_depth, ioc->request_sz, ((ioc->chain_depth *
2635             ioc->request_sz))/1024));
2636
2637         /* initialize hi-priority queue smid's */
2638         ioc->hpr_lookup = kcalloc(ioc->hi_priority_depth,
2639             sizeof(struct request_tracker), GFP_KERNEL);
2640         if (!ioc->hpr_lookup) {
2641                 printk(MPT2SAS_ERR_FMT "hpr_lookup: kcalloc failed\n",
2642                     ioc->name);
2643                 goto out;
2644         }
2645         ioc->hi_priority_smid = ioc->scsiio_depth + 1;
2646         dinitprintk(ioc, printk(MPT2SAS_INFO_FMT "hi_priority(0x%p): "
2647             "depth(%d), start smid(%d)\n", ioc->name, ioc->hi_priority,
2648             ioc->hi_priority_depth, ioc->hi_priority_smid));
2649
2650         /* initialize internal queue smid's */
2651         ioc->internal_lookup = kcalloc(ioc->internal_depth,
2652             sizeof(struct request_tracker), GFP_KERNEL);
2653         if (!ioc->internal_lookup) {
2654                 printk(MPT2SAS_ERR_FMT "internal_lookup: kcalloc failed\n",
2655                     ioc->name);
2656                 goto out;
2657         }
2658         ioc->internal_smid = ioc->hi_priority_smid + ioc->hi_priority_depth;
2659         dinitprintk(ioc, printk(MPT2SAS_INFO_FMT "internal(0x%p): "
2660             "depth(%d), start smid(%d)\n", ioc->name, ioc->internal,
2661              ioc->internal_depth, ioc->internal_smid));
2662
2663         /* sense buffers, 4 byte align */
2664         sz = ioc->scsiio_depth * SCSI_SENSE_BUFFERSIZE;
2665         ioc->sense_dma_pool = pci_pool_create("sense pool", ioc->pdev, sz, 4,
2666             0);
2667         if (!ioc->sense_dma_pool) {
2668                 printk(MPT2SAS_ERR_FMT "sense pool: pci_pool_create failed\n",
2669                     ioc->name);
2670                 goto out;
2671         }
2672         ioc->sense = pci_pool_alloc(ioc->sense_dma_pool , GFP_KERNEL,
2673             &ioc->sense_dma);
2674         if (!ioc->sense) {
2675                 printk(MPT2SAS_ERR_FMT "sense pool: pci_pool_alloc failed\n",
2676                     ioc->name);
2677                 goto out;
2678         }
2679         dinitprintk(ioc, printk(MPT2SAS_INFO_FMT
2680             "sense pool(0x%p): depth(%d), element_size(%d), pool_size"
2681             "(%d kB)\n", ioc->name, ioc->sense, ioc->scsiio_depth,
2682             SCSI_SENSE_BUFFERSIZE, sz/1024));
2683         dinitprintk(ioc, printk(MPT2SAS_INFO_FMT "sense_dma(0x%llx)\n",
2684             ioc->name, (unsigned long long)ioc->sense_dma));
2685         total_sz += sz;
2686
2687         /* reply pool, 4 byte align */
2688         sz = ioc->reply_free_queue_depth * ioc->reply_sz;
2689         ioc->reply_dma_pool = pci_pool_create("reply pool", ioc->pdev, sz, 4,
2690             0);
2691         if (!ioc->reply_dma_pool) {
2692                 printk(MPT2SAS_ERR_FMT "reply pool: pci_pool_create failed\n",
2693                     ioc->name);
2694                 goto out;
2695         }
2696         ioc->reply = pci_pool_alloc(ioc->reply_dma_pool , GFP_KERNEL,
2697             &ioc->reply_dma);
2698         if (!ioc->reply) {
2699                 printk(MPT2SAS_ERR_FMT "reply pool: pci_pool_alloc failed\n",
2700                     ioc->name);
2701                 goto out;
2702         }
2703         ioc->reply_dma_min_address = (u32)(ioc->reply_dma);
2704         ioc->reply_dma_max_address = (u32)(ioc->reply_dma) + sz;
2705         dinitprintk(ioc, printk(MPT2SAS_INFO_FMT "reply pool(0x%p): depth"
2706             "(%d), frame_size(%d), pool_size(%d kB)\n", ioc->name, ioc->reply,
2707             ioc->reply_free_queue_depth, ioc->reply_sz, sz/1024));
2708         dinitprintk(ioc, printk(MPT2SAS_INFO_FMT "reply_dma(0x%llx)\n",
2709             ioc->name, (unsigned long long)ioc->reply_dma));
2710         total_sz += sz;
2711
2712         /* reply free queue, 16 byte align */
2713         sz = ioc->reply_free_queue_depth * 4;
2714         ioc->reply_free_dma_pool = pci_pool_create("reply_free pool",
2715             ioc->pdev, sz, 16, 0);
2716         if (!ioc->reply_free_dma_pool) {
2717                 printk(MPT2SAS_ERR_FMT "reply_free pool: pci_pool_create "
2718                     "failed\n", ioc->name);
2719                 goto out;
2720         }
2721         ioc->reply_free = pci_pool_alloc(ioc->reply_free_dma_pool , GFP_KERNEL,
2722             &ioc->reply_free_dma);
2723         if (!ioc->reply_free) {
2724                 printk(MPT2SAS_ERR_FMT "reply_free pool: pci_pool_alloc "
2725                     "failed\n", ioc->name);
2726                 goto out;
2727         }
2728         memset(ioc->reply_free, 0, sz);
2729         dinitprintk(ioc, printk(MPT2SAS_INFO_FMT "reply_free pool(0x%p): "
2730             "depth(%d), element_size(%d), pool_size(%d kB)\n", ioc->name,
2731             ioc->reply_free, ioc->reply_free_queue_depth, 4, sz/1024));
2732         dinitprintk(ioc, printk(MPT2SAS_INFO_FMT "reply_free_dma"
2733             "(0x%llx)\n", ioc->name, (unsigned long long)ioc->reply_free_dma));
2734         total_sz += sz;
2735
2736         /* reply post queue, 16 byte align */
2737         reply_post_free_sz = ioc->reply_post_queue_depth *
2738             sizeof(Mpi2DefaultReplyDescriptor_t);
2739         if (_base_is_controller_msix_enabled(ioc))
2740                 sz = reply_post_free_sz * ioc->reply_queue_count;
2741         else
2742                 sz = reply_post_free_sz;
2743         ioc->reply_post_free_dma_pool = pci_pool_create("reply_post_free pool",
2744             ioc->pdev, sz, 16, 0);
2745         if (!ioc->reply_post_free_dma_pool) {
2746                 printk(MPT2SAS_ERR_FMT "reply_post_free pool: pci_pool_create "
2747                     "failed\n", ioc->name);
2748                 goto out;
2749         }
2750         ioc->reply_post_free = pci_pool_alloc(ioc->reply_post_free_dma_pool ,
2751             GFP_KERNEL, &ioc->reply_post_free_dma);
2752         if (!ioc->reply_post_free) {
2753                 printk(MPT2SAS_ERR_FMT "reply_post_free pool: pci_pool_alloc "
2754                     "failed\n", ioc->name);
2755                 goto out;
2756         }
2757         memset(ioc->reply_post_free, 0, sz);
2758         dinitprintk(ioc, printk(MPT2SAS_INFO_FMT "reply post free pool"
2759             "(0x%p): depth(%d), element_size(%d), pool_size(%d kB)\n",
2760             ioc->name, ioc->reply_post_free, ioc->reply_post_queue_depth, 8,
2761             sz/1024));
2762         dinitprintk(ioc, printk(MPT2SAS_INFO_FMT "reply_post_free_dma = "
2763             "(0x%llx)\n", ioc->name, (unsigned long long)
2764             ioc->reply_post_free_dma));
2765         total_sz += sz;
2766
2767         ioc->config_page_sz = 512;
2768         ioc->config_page = pci_alloc_consistent(ioc->pdev,
2769             ioc->config_page_sz, &ioc->config_page_dma);
2770         if (!ioc->config_page) {
2771                 printk(MPT2SAS_ERR_FMT "config page: pci_pool_alloc "
2772                     "failed\n", ioc->name);
2773                 goto out;
2774         }
2775         dinitprintk(ioc, printk(MPT2SAS_INFO_FMT "config page(0x%p): size"
2776             "(%d)\n", ioc->name, ioc->config_page, ioc->config_page_sz));
2777         dinitprintk(ioc, printk(MPT2SAS_INFO_FMT "config_page_dma"
2778             "(0x%llx)\n", ioc->name, (unsigned long long)ioc->config_page_dma));
2779         total_sz += ioc->config_page_sz;
2780
2781         printk(MPT2SAS_INFO_FMT "Allocated physical memory: size(%d kB)\n",
2782             ioc->name, total_sz/1024);
2783         printk(MPT2SAS_INFO_FMT "Current Controller Queue Depth(%d), "
2784             "Max Controller Queue Depth(%d)\n",
2785             ioc->name, ioc->shost->can_queue, facts->RequestCredit);
2786         printk(MPT2SAS_INFO_FMT "Scatter Gather Elements per IO(%d)\n",
2787             ioc->name, ioc->shost->sg_tablesize);
2788         return 0;
2789
2790  out:
2791         return -ENOMEM;
2792 }
2793
2794
2795 /**
2796  * mpt2sas_base_get_iocstate - Get the current state of a MPT adapter.
2797  * @ioc: Pointer to MPT_ADAPTER structure
2798  * @cooked: Request raw or cooked IOC state
2799  *
2800  * Returns all IOC Doorbell register bits if cooked==0, else just the
2801  * Doorbell bits in MPI_IOC_STATE_MASK.
2802  */
2803 u32
2804 mpt2sas_base_get_iocstate(struct MPT2SAS_ADAPTER *ioc, int cooked)
2805 {
2806         u32 s, sc;
2807
2808         s = readl(&ioc->chip->Doorbell);
2809         sc = s & MPI2_IOC_STATE_MASK;
2810         return cooked ? sc : s;
2811 }
2812
2813 /**
2814  * _base_wait_on_iocstate - waiting on a particular ioc state
2815  * @ioc_state: controller state { READY, OPERATIONAL, or RESET }
2816  * @timeout: timeout in second
2817  * @sleep_flag: CAN_SLEEP or NO_SLEEP
2818  *
2819  * Returns 0 for success, non-zero for failure.
2820  */
2821 static int
2822 _base_wait_on_iocstate(struct MPT2SAS_ADAPTER *ioc, u32 ioc_state, int timeout,
2823     int sleep_flag)
2824 {
2825         u32 count, cntdn;
2826         u32 current_state;
2827
2828         count = 0;
2829         cntdn = (sleep_flag == CAN_SLEEP) ? 1000*timeout : 2000*timeout;
2830         do {
2831                 current_state = mpt2sas_base_get_iocstate(ioc, 1);
2832                 if (current_state == ioc_state)
2833                         return 0;
2834                 if (count && current_state == MPI2_IOC_STATE_FAULT)
2835                         break;
2836                 if (sleep_flag == CAN_SLEEP)
2837                         msleep(1);
2838                 else
2839                         udelay(500);
2840                 count++;
2841         } while (--cntdn);
2842
2843         return current_state;
2844 }
2845
2846 /**
2847  * _base_wait_for_doorbell_int - waiting for controller interrupt(generated by
2848  * a write to the doorbell)
2849  * @ioc: per adapter object
2850  * @timeout: timeout in second
2851  * @sleep_flag: CAN_SLEEP or NO_SLEEP
2852  *
2853  * Returns 0 for success, non-zero for failure.
2854  *
2855  * Notes: MPI2_HIS_IOC2SYS_DB_STATUS - set to one when IOC writes to doorbell.
2856  */
2857 static int
2858 _base_wait_for_doorbell_int(struct MPT2SAS_ADAPTER *ioc, int timeout,
2859     int sleep_flag)
2860 {
2861         u32 cntdn, count;
2862         u32 int_status;
2863
2864         count = 0;
2865         cntdn = (sleep_flag == CAN_SLEEP) ? 1000*timeout : 2000*timeout;
2866         do {
2867                 int_status = readl(&ioc->chip->HostInterruptStatus);
2868                 if (int_status & MPI2_HIS_IOC2SYS_DB_STATUS) {
2869                         dhsprintk(ioc, printk(MPT2SAS_INFO_FMT "%s: "
2870                             "successful count(%d), timeout(%d)\n", ioc->name,
2871                             __func__, count, timeout));
2872                         return 0;
2873                 }
2874                 if (sleep_flag == CAN_SLEEP)
2875                         msleep(1);
2876                 else
2877                         udelay(500);
2878                 count++;
2879         } while (--cntdn);
2880
2881         printk(MPT2SAS_ERR_FMT "%s: failed due to timeout count(%d), "
2882             "int_status(%x)!\n", ioc->name, __func__, count, int_status);
2883         return -EFAULT;
2884 }
2885
2886 /**
2887  * _base_wait_for_doorbell_ack - waiting for controller to read the doorbell.
2888  * @ioc: per adapter object
2889  * @timeout: timeout in second
2890  * @sleep_flag: CAN_SLEEP or NO_SLEEP
2891  *
2892  * Returns 0 for success, non-zero for failure.
2893  *
2894  * Notes: MPI2_HIS_SYS2IOC_DB_STATUS - set to one when host writes to
2895  * doorbell.
2896  */
2897 static int
2898 _base_wait_for_doorbell_ack(struct MPT2SAS_ADAPTER *ioc, int timeout,
2899     int sleep_flag)
2900 {
2901         u32 cntdn, count;
2902         u32 int_status;
2903         u32 doorbell;
2904
2905         count = 0;
2906         cntdn = (sleep_flag == CAN_SLEEP) ? 1000*timeout : 2000*timeout;
2907         do {
2908                 int_status = readl(&ioc->chip->HostInterruptStatus);
2909                 if (!(int_status & MPI2_HIS_SYS2IOC_DB_STATUS)) {
2910                         dhsprintk(ioc, printk(MPT2SAS_INFO_FMT "%s: "
2911                             "successful count(%d), timeout(%d)\n", ioc->name,
2912                             __func__, count, timeout));
2913                         return 0;
2914                 } else if (int_status & MPI2_HIS_IOC2SYS_DB_STATUS) {
2915                         doorbell = readl(&ioc->chip->Doorbell);
2916                         if ((doorbell & MPI2_IOC_STATE_MASK) ==
2917                             MPI2_IOC_STATE_FAULT) {
2918                                 mpt2sas_base_fault_info(ioc , doorbell);
2919                                 return -EFAULT;
2920                         }
2921                 } else if (int_status == 0xFFFFFFFF)
2922                         goto out;
2923
2924                 if (sleep_flag == CAN_SLEEP)
2925                         msleep(1);
2926                 else
2927                         udelay(500);
2928                 count++;
2929         } while (--cntdn);
2930
2931  out:
2932         printk(MPT2SAS_ERR_FMT "%s: failed due to timeout count(%d), "
2933             "int_status(%x)!\n", ioc->name, __func__, count, int_status);
2934         return -EFAULT;
2935 }
2936
2937 /**
2938  * _base_wait_for_doorbell_not_used - waiting for doorbell to not be in use
2939  * @ioc: per adapter object
2940  * @timeout: timeout in second
2941  * @sleep_flag: CAN_SLEEP or NO_SLEEP
2942  *
2943  * Returns 0 for success, non-zero for failure.
2944  *
2945  */
2946 static int
2947 _base_wait_for_doorbell_not_used(struct MPT2SAS_ADAPTER *ioc, int timeout,
2948     int sleep_flag)
2949 {
2950         u32 cntdn, count;
2951         u32 doorbell_reg;
2952
2953         count = 0;
2954         cntdn = (sleep_flag == CAN_SLEEP) ? 1000*timeout : 2000*timeout;
2955         do {
2956                 doorbell_reg = readl(&ioc->chip->Doorbell);
2957                 if (!(doorbell_reg & MPI2_DOORBELL_USED)) {
2958                         dhsprintk(ioc, printk(MPT2SAS_INFO_FMT "%s: "
2959                             "successful count(%d), timeout(%d)\n", ioc->name,
2960                             __func__, count, timeout));
2961                         return 0;
2962                 }
2963                 if (sleep_flag == CAN_SLEEP)
2964                         msleep(1);
2965                 else
2966                         udelay(500);
2967                 count++;
2968         } while (--cntdn);
2969
2970         printk(MPT2SAS_ERR_FMT "%s: failed due to timeout count(%d), "
2971             "doorbell_reg(%x)!\n", ioc->name, __func__, count, doorbell_reg);
2972         return -EFAULT;
2973 }
2974
2975 /**
2976  * _base_send_ioc_reset - send doorbell reset
2977  * @ioc: per adapter object
2978  * @reset_type: currently only supports: MPI2_FUNCTION_IOC_MESSAGE_UNIT_RESET
2979  * @timeout: timeout in second
2980  * @sleep_flag: CAN_SLEEP or NO_SLEEP
2981  *
2982  * Returns 0 for success, non-zero for failure.
2983  */
2984 static int
2985 _base_send_ioc_reset(struct MPT2SAS_ADAPTER *ioc, u8 reset_type, int timeout,
2986     int sleep_flag)
2987 {
2988         u32 ioc_state;
2989         int r = 0;
2990
2991         if (reset_type != MPI2_FUNCTION_IOC_MESSAGE_UNIT_RESET) {
2992                 printk(MPT2SAS_ERR_FMT "%s: unknown reset_type\n",
2993                     ioc->name, __func__);
2994                 return -EFAULT;
2995         }
2996
2997         if (!(ioc->facts.IOCCapabilities &
2998            MPI2_IOCFACTS_CAPABILITY_EVENT_REPLAY))
2999                 return -EFAULT;
3000
3001         printk(MPT2SAS_INFO_FMT "sending message unit reset !!\n", ioc->name);
3002
3003         writel(reset_type << MPI2_DOORBELL_FUNCTION_SHIFT,
3004             &ioc->chip->Doorbell);
3005         if ((_base_wait_for_doorbell_ack(ioc, 15, sleep_flag))) {
3006                 r = -EFAULT;
3007                 goto out;
3008         }
3009         ioc_state = _base_wait_on_iocstate(ioc, MPI2_IOC_STATE_READY,
3010             timeout, sleep_flag);
3011         if (ioc_state) {
3012                 printk(MPT2SAS_ERR_FMT "%s: failed going to ready state "
3013                     " (ioc_state=0x%x)\n", ioc->name, __func__, ioc_state);
3014                 r = -EFAULT;
3015                 goto out;
3016         }
3017  out:
3018         printk(MPT2SAS_INFO_FMT "message unit reset: %s\n",
3019             ioc->name, ((r == 0) ? "SUCCESS" : "FAILED"));
3020         return r;
3021 }
3022
3023 /**
3024  * _base_handshake_req_reply_wait - send request thru doorbell interface
3025  * @ioc: per adapter object
3026  * @request_bytes: request length
3027  * @request: pointer having request payload
3028  * @reply_bytes: reply length
3029  * @reply: pointer to reply payload
3030  * @timeout: timeout in second
3031  * @sleep_flag: CAN_SLEEP or NO_SLEEP
3032  *
3033  * Returns 0 for success, non-zero for failure.
3034  */
3035 static int
3036 _base_handshake_req_reply_wait(struct MPT2SAS_ADAPTER *ioc, int request_bytes,
3037     u32 *request, int reply_bytes, u16 *reply, int timeout, int sleep_flag)
3038 {
3039         MPI2DefaultReply_t *default_reply = (MPI2DefaultReply_t *)reply;
3040         int i;
3041         u8 failed;
3042         u16 dummy;
3043         __le32 *mfp;
3044
3045         /* make sure doorbell is not in use */
3046         if ((readl(&ioc->chip->Doorbell) & MPI2_DOORBELL_USED)) {
3047                 printk(MPT2SAS_ERR_FMT "doorbell is in use "
3048                     " (line=%d)\n", ioc->name, __LINE__);
3049                 return -EFAULT;
3050         }
3051
3052         /* clear pending doorbell interrupts from previous state changes */
3053         if (readl(&ioc->chip->HostInterruptStatus) &
3054             MPI2_HIS_IOC2SYS_DB_STATUS)
3055                 writel(0, &ioc->chip->HostInterruptStatus);
3056
3057         /* send message to ioc */
3058         writel(((MPI2_FUNCTION_HANDSHAKE<<MPI2_DOORBELL_FUNCTION_SHIFT) |
3059             ((request_bytes/4)<<MPI2_DOORBELL_ADD_DWORDS_SHIFT)),
3060             &ioc->chip->Doorbell);
3061
3062         if ((_base_wait_for_doorbell_int(ioc, 5, NO_SLEEP))) {
3063                 printk(MPT2SAS_ERR_FMT "doorbell handshake "
3064                    "int failed (line=%d)\n", ioc->name, __LINE__);
3065                 return -EFAULT;
3066         }
3067         writel(0, &ioc->chip->HostInterruptStatus);
3068
3069         if ((_base_wait_for_doorbell_ack(ioc, 5, sleep_flag))) {
3070                 printk(MPT2SAS_ERR_FMT "doorbell handshake "
3071                     "ack failed (line=%d)\n", ioc->name, __LINE__);
3072                 return -EFAULT;
3073         }
3074
3075         /* send message 32-bits at a time */
3076         for (i = 0, failed = 0; i < request_bytes/4 && !failed; i++) {
3077                 writel(cpu_to_le32(request[i]), &ioc->chip->Doorbell);
3078                 if ((_base_wait_for_doorbell_ack(ioc, 5, sleep_flag)))
3079                         failed = 1;
3080         }
3081
3082         if (failed) {
3083                 printk(MPT2SAS_ERR_FMT "doorbell handshake "
3084                     "sending request failed (line=%d)\n", ioc->name, __LINE__);
3085                 return -EFAULT;
3086         }
3087
3088         /* now wait for the reply */
3089         if ((_base_wait_for_doorbell_int(ioc, timeout, sleep_flag))) {
3090                 printk(MPT2SAS_ERR_FMT "doorbell handshake "
3091                    "int failed (line=%d)\n", ioc->name, __LINE__);
3092                 return -EFAULT;
3093         }
3094
3095         /* read the first two 16-bits, it gives the total length of the reply */
3096         reply[0] = le16_to_cpu(readl(&ioc->chip->Doorbell)
3097             & MPI2_DOORBELL_DATA_MASK);
3098         writel(0, &ioc->chip->HostInterruptStatus);
3099         if ((_base_wait_for_doorbell_int(ioc, 5, sleep_flag))) {
3100                 printk(MPT2SAS_ERR_FMT "doorbell handshake "
3101                    "int failed (line=%d)\n", ioc->name, __LINE__);
3102                 return -EFAULT;
3103         }
3104         reply[1] = le16_to_cpu(readl(&ioc->chip->Doorbell)
3105             & MPI2_DOORBELL_DATA_MASK);
3106         writel(0, &ioc->chip->HostInterruptStatus);
3107
3108         for (i = 2; i < default_reply->MsgLength * 2; i++)  {
3109                 if ((_base_wait_for_doorbell_int(ioc, 5, sleep_flag))) {
3110                         printk(MPT2SAS_ERR_FMT "doorbell "
3111                             "handshake int failed (line=%d)\n", ioc->name,
3112                             __LINE__);
3113                         return -EFAULT;
3114                 }
3115                 if (i >=  reply_bytes/2) /* overflow case */
3116                         dummy = readl(&ioc->chip->Doorbell);
3117                 else
3118                         reply[i] = le16_to_cpu(readl(&ioc->chip->Doorbell)
3119                             & MPI2_DOORBELL_DATA_MASK);
3120                 writel(0, &ioc->chip->HostInterruptStatus);
3121         }
3122
3123         _base_wait_for_doorbell_int(ioc, 5, sleep_flag);
3124         if (_base_wait_for_doorbell_not_used(ioc, 5, sleep_flag) != 0) {
3125                 dhsprintk(ioc, printk(MPT2SAS_INFO_FMT "doorbell is in use "
3126                     " (line=%d)\n", ioc->name, __LINE__));
3127         }
3128         writel(0, &ioc->chip->HostInterruptStatus);
3129
3130         if (ioc->logging_level & MPT_DEBUG_INIT) {
3131                 mfp = (__le32 *)reply;
3132                 printk(KERN_INFO "\toffset:data\n");
3133                 for (i = 0; i < reply_bytes/4; i++)
3134                         printk(KERN_INFO "\t[0x%02x]:%08x\n", i*4,
3135                             le32_to_cpu(mfp[i]));
3136         }
3137         return 0;
3138 }
3139
3140 /**
3141  * mpt2sas_base_sas_iounit_control - send sas iounit control to FW
3142  * @ioc: per adapter object
3143  * @mpi_reply: the reply payload from FW
3144  * @mpi_request: the request payload sent to FW
3145  *
3146  * The SAS IO Unit Control Request message allows the host to perform low-level
3147  * operations, such as resets on the PHYs of the IO Unit, also allows the host
3148  * to obtain the IOC assigned device handles for a device if it has other
3149  * identifying information about the device, in addition allows the host to
3150  * remove IOC resources associated with the device.
3151  *
3152  * Returns 0 for success, non-zero for failure.
3153  */
3154 int
3155 mpt2sas_base_sas_iounit_control(struct MPT2SAS_ADAPTER *ioc,
3156     Mpi2SasIoUnitControlReply_t *mpi_reply,
3157     Mpi2SasIoUnitControlRequest_t *mpi_request)
3158 {
3159         u16 smid;
3160         u32 ioc_state;
3161         unsigned long timeleft;
3162         u8 issue_reset;
3163         int rc;
3164         void *request;
3165         u16 wait_state_count;
3166
3167         dinitprintk(ioc, printk(MPT2SAS_INFO_FMT "%s\n", ioc->name,
3168             __func__));
3169
3170         mutex_lock(&ioc->base_cmds.mutex);
3171
3172         if (ioc->base_cmds.status != MPT2_CMD_NOT_USED) {
3173                 printk(MPT2SAS_ERR_FMT "%s: base_cmd in use\n",
3174                     ioc->name, __func__);
3175                 rc = -EAGAIN;
3176                 goto out;
3177         }
3178
3179         wait_state_count = 0;
3180         ioc_state = mpt2sas_base_get_iocstate(ioc, 1);
3181         while (ioc_state != MPI2_IOC_STATE_OPERATIONAL) {
3182                 if (wait_state_count++ == 10) {
3183                         printk(MPT2SAS_ERR_FMT
3184                             "%s: failed due to ioc not operational\n",
3185                             ioc->name, __func__);
3186                         rc = -EFAULT;
3187                         goto out;
3188                 }
3189                 ssleep(1);
3190                 ioc_state = mpt2sas_base_get_iocstate(ioc, 1);
3191                 printk(MPT2SAS_INFO_FMT "%s: waiting for "
3192                     "operational state(count=%d)\n", ioc->name,
3193                     __func__, wait_state_count);
3194         }
3195
3196         smid = mpt2sas_base_get_smid(ioc, ioc->base_cb_idx);
3197         if (!smid) {
3198                 printk(MPT2SAS_ERR_FMT "%s: failed obtaining a smid\n",
3199                     ioc->name, __func__);
3200                 rc = -EAGAIN;
3201                 goto out;
3202         }
3203
3204         rc = 0;
3205         ioc->base_cmds.status = MPT2_CMD_PENDING;
3206         request = mpt2sas_base_get_msg_frame(ioc, smid);
3207         ioc->base_cmds.smid = smid;
3208         memcpy(request, mpi_request, sizeof(Mpi2SasIoUnitControlRequest_t));
3209         if (mpi_request->Operation == MPI2_SAS_OP_PHY_HARD_RESET ||
3210             mpi_request->Operation == MPI2_SAS_OP_PHY_LINK_RESET)
3211                 ioc->ioc_link_reset_in_progress = 1;
3212         init_completion(&ioc->base_cmds.done);
3213         mpt2sas_base_put_smid_default(ioc, smid);
3214         timeleft = wait_for_completion_timeout(&ioc->base_cmds.done,
3215             msecs_to_jiffies(10000));
3216         if ((mpi_request->Operation == MPI2_SAS_OP_PHY_HARD_RESET ||
3217             mpi_request->Operation == MPI2_SAS_OP_PHY_LINK_RESET) &&
3218             ioc->ioc_link_reset_in_progress)
3219                 ioc->ioc_link_reset_in_progress = 0;
3220         if (!(ioc->base_cmds.status & MPT2_CMD_COMPLETE)) {
3221                 printk(MPT2SAS_ERR_FMT "%s: timeout\n",
3222                     ioc->name, __func__);
3223                 _debug_dump_mf(mpi_request,
3224                     sizeof(Mpi2SasIoUnitControlRequest_t)/4);
3225                 if (!(ioc->base_cmds.status & MPT2_CMD_RESET))
3226                         issue_reset = 1;
3227                 goto issue_host_reset;
3228         }
3229         if (ioc->base_cmds.status & MPT2_CMD_REPLY_VALID)
3230                 memcpy(mpi_reply, ioc->base_cmds.reply,
3231                     sizeof(Mpi2SasIoUnitControlReply_t));
3232         else
3233                 memset(mpi_reply, 0, sizeof(Mpi2SasIoUnitControlReply_t));
3234         ioc->base_cmds.status = MPT2_CMD_NOT_USED;
3235         goto out;
3236
3237  issue_host_reset:
3238         if (issue_reset)
3239                 mpt2sas_base_hard_reset_handler(ioc, CAN_SLEEP,
3240                     FORCE_BIG_HAMMER);
3241         ioc->base_cmds.status = MPT2_CMD_NOT_USED;
3242         rc = -EFAULT;
3243  out:
3244         mutex_unlock(&ioc->base_cmds.mutex);
3245         return rc;
3246 }
3247
3248
3249 /**
3250  * mpt2sas_base_scsi_enclosure_processor - sending request to sep device
3251  * @ioc: per adapter object
3252  * @mpi_reply: the reply payload from FW
3253  * @mpi_request: the request payload sent to FW
3254  *
3255  * The SCSI Enclosure Processor request message causes the IOC to
3256  * communicate with SES devices to control LED status signals.
3257  *
3258  * Returns 0 for success, non-zero for failure.
3259  */
3260 int
3261 mpt2sas_base_scsi_enclosure_processor(struct MPT2SAS_ADAPTER *ioc,
3262     Mpi2SepReply_t *mpi_reply, Mpi2SepRequest_t *mpi_request)
3263 {
3264         u16 smid;
3265         u32 ioc_state;
3266         unsigned long timeleft;
3267         u8 issue_reset;
3268         int rc;
3269         void *request;
3270         u16 wait_state_count;
3271
3272         dinitprintk(ioc, printk(MPT2SAS_INFO_FMT "%s\n", ioc->name,
3273             __func__));
3274
3275         mutex_lock(&ioc->base_cmds.mutex);
3276
3277         if (ioc->base_cmds.status != MPT2_CMD_NOT_USED) {
3278                 printk(MPT2SAS_ERR_FMT "%s: base_cmd in use\n",
3279                     ioc->name, __func__);
3280                 rc = -EAGAIN;
3281                 goto out;
3282         }
3283
3284         wait_state_count = 0;
3285         ioc_state = mpt2sas_base_get_iocstate(ioc, 1);
3286         while (ioc_state != MPI2_IOC_STATE_OPERATIONAL) {
3287                 if (wait_state_count++ == 10) {
3288                         printk(MPT2SAS_ERR_FMT
3289                             "%s: failed due to ioc not operational\n",
3290                             ioc->name, __func__);
3291                         rc = -EFAULT;
3292                         goto out;
3293                 }
3294                 ssleep(1);
3295                 ioc_state = mpt2sas_base_get_iocstate(ioc, 1);
3296                 printk(MPT2SAS_INFO_FMT "%s: waiting for "
3297                     "operational state(count=%d)\n", ioc->name,
3298                     __func__, wait_state_count);
3299         }
3300
3301         smid = mpt2sas_base_get_smid(ioc, ioc->base_cb_idx);
3302         if (!smid) {
3303                 printk(MPT2SAS_ERR_FMT "%s: failed obtaining a smid\n",
3304                     ioc->name, __func__);
3305                 rc = -EAGAIN;
3306                 goto out;
3307         }
3308
3309         rc = 0;
3310         ioc->base_cmds.status = MPT2_CMD_PENDING;
3311         request = mpt2sas_base_get_msg_frame(ioc, smid);
3312         ioc->base_cmds.smid = smid;
3313         memcpy(request, mpi_request, sizeof(Mpi2SepReply_t));
3314         init_completion(&ioc->base_cmds.done);
3315         mpt2sas_base_put_smid_default(ioc, smid);
3316         timeleft = wait_for_completion_timeout(&ioc->base_cmds.done,
3317             msecs_to_jiffies(10000));
3318         if (!(ioc->base_cmds.status & MPT2_CMD_COMPLETE)) {
3319                 printk(MPT2SAS_ERR_FMT "%s: timeout\n",
3320                     ioc->name, __func__);
3321                 _debug_dump_mf(mpi_request,
3322                     sizeof(Mpi2SepRequest_t)/4);
3323                 if (!(ioc->base_cmds.status & MPT2_CMD_RESET))
3324                         issue_reset = 1;
3325                 goto issue_host_reset;
3326         }
3327         if (ioc->base_cmds.status & MPT2_CMD_REPLY_VALID)
3328                 memcpy(mpi_reply, ioc->base_cmds.reply,
3329                     sizeof(Mpi2SepReply_t));
3330         else
3331                 memset(mpi_reply, 0, sizeof(Mpi2SepReply_t));
3332         ioc->base_cmds.status = MPT2_CMD_NOT_USED;
3333         goto out;
3334
3335  issue_host_reset:
3336         if (issue_reset)
3337                 mpt2sas_base_hard_reset_handler(ioc, CAN_SLEEP,
3338                     FORCE_BIG_HAMMER);
3339         ioc->base_cmds.status = MPT2_CMD_NOT_USED;
3340         rc = -EFAULT;
3341  out:
3342         mutex_unlock(&ioc->base_cmds.mutex);
3343         return rc;
3344 }
3345
3346 /**
3347  * _base_get_port_facts - obtain port facts reply and save in ioc
3348  * @ioc: per adapter object
3349  * @sleep_flag: CAN_SLEEP or NO_SLEEP
3350  *
3351  * Returns 0 for success, non-zero for failure.
3352  */
3353 static int
3354 _base_get_port_facts(struct MPT2SAS_ADAPTER *ioc, int port, int sleep_flag)
3355 {
3356         Mpi2PortFactsRequest_t mpi_request;
3357         Mpi2PortFactsReply_t mpi_reply;
3358         struct mpt2sas_port_facts *pfacts;
3359         int mpi_reply_sz, mpi_request_sz, r;
3360
3361         dinitprintk(ioc, printk(MPT2SAS_INFO_FMT "%s\n", ioc->name,
3362             __func__));
3363
3364         mpi_reply_sz = sizeof(Mpi2PortFactsReply_t);
3365         mpi_request_sz = sizeof(Mpi2PortFactsRequest_t);
3366         memset(&mpi_request, 0, mpi_request_sz);
3367         mpi_request.Function = MPI2_FUNCTION_PORT_FACTS;
3368         mpi_request.PortNumber = port;
3369         r = _base_handshake_req_reply_wait(ioc, mpi_request_sz,
3370             (u32 *)&mpi_request, mpi_reply_sz, (u16 *)&mpi_reply, 5, CAN_SLEEP);
3371
3372         if (r != 0) {
3373                 printk(MPT2SAS_ERR_FMT "%s: handshake failed (r=%d)\n",
3374                     ioc->name, __func__, r);
3375                 return r;
3376         }
3377
3378         pfacts = &ioc->pfacts[port];
3379         memset(pfacts, 0, sizeof(struct mpt2sas_port_facts));
3380         pfacts->PortNumber = mpi_reply.PortNumber;
3381         pfacts->VP_ID = mpi_reply.VP_ID;
3382         pfacts->VF_ID = mpi_reply.VF_ID;
3383         pfacts->MaxPostedCmdBuffers =
3384             le16_to_cpu(mpi_reply.MaxPostedCmdBuffers);
3385
3386         return 0;
3387 }
3388
3389 /**
3390  * _base_get_ioc_facts - obtain ioc facts reply and save in ioc
3391  * @ioc: per adapter object
3392  * @sleep_flag: CAN_SLEEP or NO_SLEEP
3393  *
3394  * Returns 0 for success, non-zero for failure.
3395  */
3396 static int
3397 _base_get_ioc_facts(struct MPT2SAS_ADAPTER *ioc, int sleep_flag)
3398 {
3399         Mpi2IOCFactsRequest_t mpi_request;
3400         Mpi2IOCFactsReply_t mpi_reply;
3401         struct mpt2sas_facts *facts;
3402         int mpi_reply_sz, mpi_request_sz, r;
3403
3404         dinitprintk(ioc, printk(MPT2SAS_INFO_FMT "%s\n", ioc->name,
3405             __func__));
3406
3407         mpi_reply_sz = sizeof(Mpi2IOCFactsReply_t);
3408         mpi_request_sz = sizeof(Mpi2IOCFactsRequest_t);
3409         memset(&mpi_request, 0, mpi_request_sz);
3410         mpi_request.Function = MPI2_FUNCTION_IOC_FACTS;
3411         r = _base_handshake_req_reply_wait(ioc, mpi_request_sz,
3412             (u32 *)&mpi_request, mpi_reply_sz, (u16 *)&mpi_reply, 5, CAN_SLEEP);
3413
3414         if (r != 0) {
3415                 printk(MPT2SAS_ERR_FMT "%s: handshake failed (r=%d)\n",
3416                     ioc->name, __func__, r);
3417                 return r;
3418         }
3419
3420         facts = &ioc->facts;
3421         memset(facts, 0, sizeof(struct mpt2sas_facts));
3422         facts->MsgVersion = le16_to_cpu(mpi_reply.MsgVersion);
3423         facts->HeaderVersion = le16_to_cpu(mpi_reply.HeaderVersion);
3424         facts->VP_ID = mpi_reply.VP_ID;
3425         facts->VF_ID = mpi_reply.VF_ID;
3426         facts->IOCExceptions = le16_to_cpu(mpi_reply.IOCExceptions);
3427         facts->MaxChainDepth = mpi_reply.MaxChainDepth;
3428         facts->WhoInit = mpi_reply.WhoInit;
3429         facts->NumberOfPorts = mpi_reply.NumberOfPorts;
3430         facts->MaxMSIxVectors = mpi_reply.MaxMSIxVectors;
3431         facts->RequestCredit = le16_to_cpu(mpi_reply.RequestCredit);
3432         facts->MaxReplyDescriptorPostQueueDepth =
3433             le16_to_cpu(mpi_reply.MaxReplyDescriptorPostQueueDepth);
3434         facts->ProductID = le16_to_cpu(mpi_reply.ProductID);
3435         facts->IOCCapabilities = le32_to_cpu(mpi_reply.IOCCapabilities);
3436         if ((facts->IOCCapabilities & MPI2_IOCFACTS_CAPABILITY_INTEGRATED_RAID))
3437                 ioc->ir_firmware = 1;
3438         facts->FWVersion.Word = le32_to_cpu(mpi_reply.FWVersion.Word);
3439         facts->IOCRequestFrameSize =
3440             le16_to_cpu(mpi_reply.IOCRequestFrameSize);
3441         facts->MaxInitiators = le16_to_cpu(mpi_reply.MaxInitiators);
3442         facts->MaxTargets = le16_to_cpu(mpi_reply.MaxTargets);
3443         ioc->shost->max_id = -1;
3444         facts->MaxSasExpanders = le16_to_cpu(mpi_reply.MaxSasExpanders);
3445         facts->MaxEnclosures = le16_to_cpu(mpi_reply.MaxEnclosures);
3446         facts->ProtocolFlags = le16_to_cpu(mpi_reply.ProtocolFlags);
3447         facts->HighPriorityCredit =
3448             le16_to_cpu(mpi_reply.HighPriorityCredit);
3449         facts->ReplyFrameSize = mpi_reply.ReplyFrameSize;
3450         facts->MaxDevHandle = le16_to_cpu(mpi_reply.MaxDevHandle);
3451
3452         dinitprintk(ioc, printk(MPT2SAS_INFO_FMT "hba queue depth(%d), "
3453             "max chains per io(%d)\n", ioc->name, facts->RequestCredit,
3454             facts->MaxChainDepth));
3455         dinitprintk(ioc, printk(MPT2SAS_INFO_FMT "request frame size(%d), "
3456             "reply frame size(%d)\n", ioc->name,
3457             facts->IOCRequestFrameSize * 4, facts->ReplyFrameSize * 4));
3458         return 0;
3459 }
3460
3461 /**
3462  * _base_send_ioc_init - send ioc_init to firmware
3463  * @ioc: per adapter object
3464  * @sleep_flag: CAN_SLEEP or NO_SLEEP
3465  *
3466  * Returns 0 for success, non-zero for failure.
3467  */
3468 static int
3469 _base_send_ioc_init(struct MPT2SAS_ADAPTER *ioc, int sleep_flag)
3470 {
3471         Mpi2IOCInitRequest_t mpi_request;
3472         Mpi2IOCInitReply_t mpi_reply;
3473         int r;
3474         struct timeval current_time;
3475         u16 ioc_status;
3476
3477         dinitprintk(ioc, printk(MPT2SAS_INFO_FMT "%s\n", ioc->name,
3478             __func__));
3479
3480         memset(&mpi_request, 0, sizeof(Mpi2IOCInitRequest_t));
3481         mpi_request.Function = MPI2_FUNCTION_IOC_INIT;
3482         mpi_request.WhoInit = MPI2_WHOINIT_HOST_DRIVER;
3483         mpi_request.VF_ID = 0; /* TODO */
3484         mpi_request.VP_ID = 0;
3485         mpi_request.MsgVersion = cpu_to_le16(MPI2_VERSION);
3486         mpi_request.HeaderVersion = cpu_to_le16(MPI2_HEADER_VERSION);
3487
3488         if (_base_is_controller_msix_enabled(ioc))
3489                 mpi_request.HostMSIxVectors = ioc->reply_queue_count;
3490         mpi_request.SystemRequestFrameSize = cpu_to_le16(ioc->request_sz/4);
3491         mpi_request.ReplyDescriptorPostQueueDepth =
3492             cpu_to_le16(ioc->reply_post_queue_depth);
3493         mpi_request.ReplyFreeQueueDepth =
3494             cpu_to_le16(ioc->reply_free_queue_depth);
3495
3496         mpi_request.SenseBufferAddressHigh =
3497             cpu_to_le32((u64)ioc->sense_dma >> 32);
3498         mpi_request.SystemReplyAddressHigh =
3499             cpu_to_le32((u64)ioc->reply_dma >> 32);
3500         mpi_request.SystemRequestFrameBaseAddress =
3501             cpu_to_le64((u64)ioc->request_dma);
3502         mpi_request.ReplyFreeQueueAddress =
3503             cpu_to_le64((u64)ioc->reply_free_dma);
3504         mpi_request.ReplyDescriptorPostQueueAddress =
3505             cpu_to_le64((u64)ioc->reply_post_free_dma);
3506
3507
3508         /* This time stamp specifies number of milliseconds
3509          * since epoch ~ midnight January 1, 1970.
3510          */
3511         do_gettimeofday(&current_time);
3512         mpi_request.TimeStamp = cpu_to_le64((u64)current_time.tv_sec * 1000 +
3513             (current_time.tv_usec / 1000));
3514
3515         if (ioc->logging_level & MPT_DEBUG_INIT) {
3516                 __le32 *mfp;
3517                 int i;
3518
3519                 mfp = (__le32 *)&mpi_request;
3520                 printk(KERN_INFO "\toffset:data\n");
3521                 for (i = 0; i < sizeof(Mpi2IOCInitRequest_t)/4; i++)
3522                         printk(KERN_INFO "\t[0x%02x]:%08x\n", i*4,
3523                             le32_to_cpu(mfp[i]));
3524         }
3525
3526         r = _base_handshake_req_reply_wait(ioc,
3527             sizeof(Mpi2IOCInitRequest_t), (u32 *)&mpi_request,
3528             sizeof(Mpi2IOCInitReply_t), (u16 *)&mpi_reply, 10,
3529             sleep_flag);
3530
3531         if (r != 0) {
3532                 printk(MPT2SAS_ERR_FMT "%s: handshake failed (r=%d)\n",
3533                     ioc->name, __func__, r);
3534                 return r;
3535         }
3536
3537         ioc_status = le16_to_cpu(mpi_reply.IOCStatus) & MPI2_IOCSTATUS_MASK;
3538         if (ioc_status != MPI2_IOCSTATUS_SUCCESS ||
3539             mpi_reply.IOCLogInfo) {
3540                 printk(MPT2SAS_ERR_FMT "%s: failed\n", ioc->name, __func__);
3541                 r = -EIO;
3542         }
3543
3544         return 0;
3545 }
3546
3547 /**
3548  * mpt2sas_port_enable_done - command completion routine for port enable
3549  * @ioc: per adapter object
3550  * @smid: system request message index
3551  * @msix_index: MSIX table index supplied by the OS
3552  * @reply: reply message frame(lower 32bit addr)
3553  *
3554  * Return 1 meaning mf should be freed from _base_interrupt
3555  *        0 means the mf is freed from this function.
3556  */
3557 u8
3558 mpt2sas_port_enable_done(struct MPT2SAS_ADAPTER *ioc, u16 smid, u8 msix_index,
3559         u32 reply)
3560 {
3561         MPI2DefaultReply_t *mpi_reply;
3562         u16 ioc_status;
3563
3564         mpi_reply = mpt2sas_base_get_reply_virt_addr(ioc, reply);
3565         if (mpi_reply && mpi_reply->Function == MPI2_FUNCTION_EVENT_ACK)
3566                 return 1;
3567
3568         if (ioc->port_enable_cmds.status == MPT2_CMD_NOT_USED)
3569                 return 1;
3570
3571         ioc->port_enable_cmds.status |= MPT2_CMD_COMPLETE;
3572         if (mpi_reply) {
3573                 ioc->port_enable_cmds.status |= MPT2_CMD_REPLY_VALID;
3574                 memcpy(ioc->port_enable_cmds.reply, mpi_reply,
3575                     mpi_reply->MsgLength*4);
3576         }
3577         ioc->port_enable_cmds.status &= ~MPT2_CMD_PENDING;
3578
3579         ioc_status = le16_to_cpu(mpi_reply->IOCStatus) & MPI2_IOCSTATUS_MASK;
3580
3581         if (ioc_status != MPI2_IOCSTATUS_SUCCESS)
3582                 ioc->port_enable_failed = 1;
3583
3584         if (ioc->is_driver_loading) {
3585                 if (ioc_status == MPI2_IOCSTATUS_SUCCESS) {
3586                         mpt2sas_port_enable_complete(ioc);
3587                         return 1;
3588                 } else {
3589                         ioc->start_scan_failed = ioc_status;
3590                         ioc->start_scan = 0;
3591                         return 1;
3592                 }
3593         }
3594         complete(&ioc->port_enable_cmds.done);
3595         return 1;
3596 }
3597
3598
3599 /**
3600  * _base_send_port_enable - send port_enable(discovery stuff) to firmware
3601  * @ioc: per adapter object
3602  * @sleep_flag: CAN_SLEEP or NO_SLEEP
3603  *
3604  * Returns 0 for success, non-zero for failure.
3605  */
3606 static int
3607 _base_send_port_enable(struct MPT2SAS_ADAPTER *ioc, int sleep_flag)
3608 {
3609         Mpi2PortEnableRequest_t *mpi_request;
3610         Mpi2PortEnableReply_t *mpi_reply;
3611         unsigned long timeleft;
3612         int r = 0;
3613         u16 smid;
3614         u16 ioc_status;
3615
3616         printk(MPT2SAS_INFO_FMT "sending port enable !!\n", ioc->name);
3617
3618         if (ioc->port_enable_cmds.status & MPT2_CMD_PENDING) {
3619                 printk(MPT2SAS_ERR_FMT "%s: internal command already in use\n",
3620                     ioc->name, __func__);
3621                 return -EAGAIN;
3622         }
3623
3624         smid = mpt2sas_base_get_smid(ioc, ioc->port_enable_cb_idx);
3625         if (!smid) {
3626                 printk(MPT2SAS_ERR_FMT "%s: failed obtaining a smid\n",
3627                     ioc->name, __func__);
3628                 return -EAGAIN;
3629         }
3630
3631         ioc->port_enable_cmds.status = MPT2_CMD_PENDING;
3632         mpi_request = mpt2sas_base_get_msg_frame(ioc, smid);
3633         ioc->port_enable_cmds.smid = smid;
3634         memset(mpi_request, 0, sizeof(Mpi2PortEnableRequest_t));
3635         mpi_request->Function = MPI2_FUNCTION_PORT_ENABLE;
3636
3637         init_completion(&ioc->port_enable_cmds.done);
3638         mpt2sas_base_put_smid_default(ioc, smid);
3639         timeleft = wait_for_completion_timeout(&ioc->port_enable_cmds.done,
3640             300*HZ);
3641         if (!(ioc->port_enable_cmds.status & MPT2_CMD_COMPLETE)) {
3642                 printk(MPT2SAS_ERR_FMT "%s: timeout\n",
3643                     ioc->name, __func__);
3644                 _debug_dump_mf(mpi_request,
3645                     sizeof(Mpi2PortEnableRequest_t)/4);
3646                 if (ioc->port_enable_cmds.status & MPT2_CMD_RESET)
3647                         r = -EFAULT;
3648                 else
3649                         r = -ETIME;
3650                 goto out;
3651         }
3652         mpi_reply = ioc->port_enable_cmds.reply;
3653
3654         ioc_status = le16_to_cpu(mpi_reply->IOCStatus) & MPI2_IOCSTATUS_MASK;
3655         if (ioc_status != MPI2_IOCSTATUS_SUCCESS) {
3656                 printk(MPT2SAS_ERR_FMT "%s: failed with (ioc_status=0x%08x)\n",
3657                     ioc->name, __func__, ioc_status);
3658                 r = -EFAULT;
3659                 goto out;
3660         }
3661  out:
3662         ioc->port_enable_cmds.status = MPT2_CMD_NOT_USED;
3663         printk(MPT2SAS_INFO_FMT "port enable: %s\n", ioc->name, ((r == 0) ?
3664             "SUCCESS" : "FAILED"));
3665         return r;
3666 }
3667
3668 /**
3669  * mpt2sas_port_enable - initiate firmware discovery (don't wait for reply)
3670  * @ioc: per adapter object
3671  *
3672  * Returns 0 for success, non-zero for failure.
3673  */
3674 int
3675 mpt2sas_port_enable(struct MPT2SAS_ADAPTER *ioc)
3676 {
3677         Mpi2PortEnableRequest_t *mpi_request;
3678         u16 smid;
3679
3680         printk(MPT2SAS_INFO_FMT "sending port enable !!\n", ioc->name);
3681
3682         if (ioc->port_enable_cmds.status & MPT2_CMD_PENDING) {
3683                 printk(MPT2SAS_ERR_FMT "%s: internal command already in use\n",
3684                     ioc->name, __func__);
3685                 return -EAGAIN;
3686         }
3687
3688         smid = mpt2sas_base_get_smid(ioc, ioc->port_enable_cb_idx);
3689         if (!smid) {
3690                 printk(MPT2SAS_ERR_FMT "%s: failed obtaining a smid\n",
3691                     ioc->name, __func__);
3692                 return -EAGAIN;
3693         }
3694
3695         ioc->port_enable_cmds.status = MPT2_CMD_PENDING;
3696         mpi_request = mpt2sas_base_get_msg_frame(ioc, smid);
3697         ioc->port_enable_cmds.smid = smid;
3698         memset(mpi_request, 0, sizeof(Mpi2PortEnableRequest_t));
3699         mpi_request->Function = MPI2_FUNCTION_PORT_ENABLE;
3700
3701         mpt2sas_base_put_smid_default(ioc, smid);
3702         return 0;
3703 }
3704
3705 /**
3706  * _base_determine_wait_on_discovery - desposition
3707  * @ioc: per adapter object
3708  *
3709  * Decide whether to wait on discovery to complete. Used to either
3710  * locate boot device, or report volumes ahead of physical devices.
3711  *
3712  * Returns 1 for wait, 0 for don't wait
3713  */
3714 static int
3715 _base_determine_wait_on_discovery(struct MPT2SAS_ADAPTER *ioc)
3716 {
3717         /* We wait for discovery to complete if IR firmware is loaded.
3718          * The sas topology events arrive before PD events, so we need time to
3719          * turn on the bit in ioc->pd_handles to indicate PD
3720          * Also, it maybe required to report Volumes ahead of physical
3721          * devices when MPI2_IOCPAGE8_IRFLAGS_LOW_VOLUME_MAPPING is set.
3722          */
3723         if (ioc->ir_firmware)
3724                 return 1;
3725
3726         /* if no Bios, then we don't need to wait */
3727         if (!ioc->bios_pg3.BiosVersion)
3728                 return 0;
3729
3730         /* Bios is present, then we drop down here.
3731          *
3732          * If there any entries in the Bios Page 2, then we wait
3733          * for discovery to complete.
3734          */
3735
3736         /* Current Boot Device */
3737         if ((ioc->bios_pg2.CurrentBootDeviceForm &
3738             MPI2_BIOSPAGE2_FORM_MASK) ==
3739             MPI2_BIOSPAGE2_FORM_NO_DEVICE_SPECIFIED &&
3740         /* Request Boot Device */
3741            (ioc->bios_pg2.ReqBootDeviceForm &
3742             MPI2_BIOSPAGE2_FORM_MASK) ==
3743             MPI2_BIOSPAGE2_FORM_NO_DEVICE_SPECIFIED &&
3744         /* Alternate Request Boot Device */
3745            (ioc->bios_pg2.ReqAltBootDeviceForm &
3746             MPI2_BIOSPAGE2_FORM_MASK) ==
3747             MPI2_BIOSPAGE2_FORM_NO_DEVICE_SPECIFIED)
3748                 return 0;
3749
3750         return 1;
3751 }
3752
3753
3754 /**
3755  * _base_unmask_events - turn on notification for this event
3756  * @ioc: per adapter object
3757  * @event: firmware event
3758  *
3759  * The mask is stored in ioc->event_masks.
3760  */
3761 static void
3762 _base_unmask_events(struct MPT2SAS_ADAPTER *ioc, u16 event)
3763 {
3764         u32 desired_event;
3765
3766         if (event >= 128)
3767                 return;
3768
3769         desired_event = (1 << (event % 32));
3770
3771         if (event < 32)
3772                 ioc->event_masks[0] &= ~desired_event;
3773         else if (event < 64)
3774                 ioc->event_masks[1] &= ~desired_event;
3775         else if (event < 96)
3776                 ioc->event_masks[2] &= ~desired_event;
3777         else if (event < 128)
3778                 ioc->event_masks[3] &= ~desired_event;
3779 }
3780
3781 /**
3782  * _base_event_notification - send event notification
3783  * @ioc: per adapter object
3784  * @sleep_flag: CAN_SLEEP or NO_SLEEP
3785  *
3786  * Returns 0 for success, non-zero for failure.
3787  */
3788 static int
3789 _base_event_notification(struct MPT2SAS_ADAPTER *ioc, int sleep_flag)
3790 {
3791         Mpi2EventNotificationRequest_t *mpi_request;
3792         unsigned long timeleft;
3793         u16 smid;
3794         int r = 0;
3795         int i;
3796
3797         dinitprintk(ioc, printk(MPT2SAS_INFO_FMT "%s\n", ioc->name,
3798             __func__));
3799
3800         if (ioc->base_cmds.status & MPT2_CMD_PENDING) {
3801                 printk(MPT2SAS_ERR_FMT "%s: internal command already in use\n",
3802                     ioc->name, __func__);
3803                 return -EAGAIN;
3804         }
3805
3806         smid = mpt2sas_base_get_smid(ioc, ioc->base_cb_idx);
3807         if (!smid) {
3808                 printk(MPT2SAS_ERR_FMT "%s: failed obtaining a smid\n",
3809                     ioc->name, __func__);
3810                 return -EAGAIN;
3811         }
3812         ioc->base_cmds.status = MPT2_CMD_PENDING;
3813         mpi_request = mpt2sas_base_get_msg_frame(ioc, smid);
3814         ioc->base_cmds.smid = smid;
3815         memset(mpi_request, 0, sizeof(Mpi2EventNotificationRequest_t));
3816         mpi_request->Function = MPI2_FUNCTION_EVENT_NOTIFICATION;
3817         mpi_request->VF_ID = 0; /* TODO */
3818         mpi_request->VP_ID = 0;
3819         for (i = 0; i < MPI2_EVENT_NOTIFY_EVENTMASK_WORDS; i++)
3820                 mpi_request->EventMasks[i] =
3821                     cpu_to_le32(ioc->event_masks[i]);
3822         init_completion(&ioc->base_cmds.done);
3823         mpt2sas_base_put_smid_default(ioc, smid);
3824         timeleft = wait_for_completion_timeout(&ioc->base_cmds.done, 30*HZ);
3825         if (!(ioc->base_cmds.status & MPT2_CMD_COMPLETE)) {
3826                 printk(MPT2SAS_ERR_FMT "%s: timeout\n",
3827                     ioc->name, __func__);
3828                 _debug_dump_mf(mpi_request,
3829                     sizeof(Mpi2EventNotificationRequest_t)/4);
3830                 if (ioc->base_cmds.status & MPT2_CMD_RESET)
3831                         r = -EFAULT;
3832                 else
3833                         r = -ETIME;
3834         } else
3835                 dinitprintk(ioc, printk(MPT2SAS_INFO_FMT "%s: complete\n",
3836                     ioc->name, __func__));
3837         ioc->base_cmds.status = MPT2_CMD_NOT_USED;
3838         return r;
3839 }
3840
3841 /**
3842  * mpt2sas_base_validate_event_type - validating event types
3843  * @ioc: per adapter object
3844  * @event: firmware event
3845  *
3846  * This will turn on firmware event notification when application
3847  * ask for that event. We don't mask events that are already enabled.
3848  */
3849 void
3850 mpt2sas_base_validate_event_type(struct MPT2SAS_ADAPTER *ioc, u32 *event_type)
3851 {
3852         int i, j;
3853         u32 event_mask, desired_event;
3854         u8 send_update_to_fw;
3855
3856         for (i = 0, send_update_to_fw = 0; i <
3857             MPI2_EVENT_NOTIFY_EVENTMASK_WORDS; i++) {
3858                 event_mask = ~event_type[i];
3859                 desired_event = 1;
3860                 for (j = 0; j < 32; j++) {
3861                         if (!(event_mask & desired_event) &&
3862                             (ioc->event_masks[i] & desired_event)) {
3863                                 ioc->event_masks[i] &= ~desired_event;
3864                                 send_update_to_fw = 1;
3865                         }
3866                         desired_event = (desired_event << 1);
3867                 }
3868         }
3869
3870         if (!send_update_to_fw)
3871                 return;
3872
3873         mutex_lock(&ioc->base_cmds.mutex);
3874         _base_event_notification(ioc, CAN_SLEEP);
3875         mutex_unlock(&ioc->base_cmds.mutex);
3876 }
3877
3878 /**
3879  * _base_diag_reset - the "big hammer" start of day reset
3880  * @ioc: per adapter object
3881  * @sleep_flag: CAN_SLEEP or NO_SLEEP
3882  *
3883  * Returns 0 for success, non-zero for failure.
3884  */
3885 static int
3886 _base_diag_reset(struct MPT2SAS_ADAPTER *ioc, int sleep_flag)
3887 {
3888         u32 host_diagnostic;
3889         u32 ioc_state;
3890         u32 count;
3891         u32 hcb_size;
3892
3893         printk(MPT2SAS_INFO_FMT "sending diag reset !!\n", ioc->name);
3894         drsprintk(ioc, printk(MPT2SAS_INFO_FMT "clear interrupts\n",
3895             ioc->name));
3896
3897         count = 0;
3898         do {
3899                 /* Write magic sequence to WriteSequence register
3900                  * Loop until in diagnostic mode
3901                  */
3902                 drsprintk(ioc, printk(MPT2SAS_INFO_FMT "write magic "
3903                     "sequence\n", ioc->name));
3904                 writel(MPI2_WRSEQ_FLUSH_KEY_VALUE, &ioc->chip->WriteSequence);
3905                 writel(MPI2_WRSEQ_1ST_KEY_VALUE, &ioc->chip->WriteSequence);
3906                 writel(MPI2_WRSEQ_2ND_KEY_VALUE, &ioc->chip->WriteSequence);
3907                 writel(MPI2_WRSEQ_3RD_KEY_VALUE, &ioc->chip->WriteSequence);
3908                 writel(MPI2_WRSEQ_4TH_KEY_VALUE, &ioc->chip->WriteSequence);
3909                 writel(MPI2_WRSEQ_5TH_KEY_VALUE, &ioc->chip->WriteSequence);
3910                 writel(MPI2_WRSEQ_6TH_KEY_VALUE, &ioc->chip->WriteSequence);
3911
3912                 /* wait 100 msec */
3913                 if (sleep_flag == CAN_SLEEP)
3914                         msleep(100);
3915                 else
3916                         mdelay(100);
3917
3918                 if (count++ > 20)
3919                         goto out;
3920
3921                 host_diagnostic = readl(&ioc->chip->HostDiagnostic);
3922                 drsprintk(ioc, printk(MPT2SAS_INFO_FMT "wrote magic "
3923                     "sequence: count(%d), host_diagnostic(0x%08x)\n",
3924                     ioc->name, count, host_diagnostic));
3925
3926         } while ((host_diagnostic & MPI2_DIAG_DIAG_WRITE_ENABLE) == 0);
3927
3928         hcb_size = readl(&ioc->chip->HCBSize);
3929
3930         drsprintk(ioc, printk(MPT2SAS_INFO_FMT "diag reset: issued\n",
3931             ioc->name));
3932         writel(host_diagnostic | MPI2_DIAG_RESET_ADAPTER,
3933              &ioc->chip->HostDiagnostic);
3934
3935         /* don't access any registers for 50 milliseconds */
3936         msleep(50);
3937
3938         /* 300 second max wait */
3939         for (count = 0; count < 3000000 ; count++) {
3940
3941                 host_diagnostic = readl(&ioc->chip->HostDiagnostic);
3942
3943                 if (host_diagnostic == 0xFFFFFFFF)
3944                         goto out;
3945                 if (!(host_diagnostic & MPI2_DIAG_RESET_ADAPTER))
3946                         break;
3947
3948                 /* wait 100 msec */
3949                 if (sleep_flag == CAN_SLEEP)
3950                         msleep(1);
3951                 else
3952                         mdelay(1);
3953         }
3954
3955         if (host_diagnostic & MPI2_DIAG_HCB_MODE) {
3956
3957                 drsprintk(ioc, printk(MPT2SAS_INFO_FMT "restart the adapter "
3958                     "assuming the HCB Address points to good F/W\n",
3959                     ioc->name));
3960                 host_diagnostic &= ~MPI2_DIAG_BOOT_DEVICE_SELECT_MASK;
3961                 host_diagnostic |= MPI2_DIAG_BOOT_DEVICE_SELECT_HCDW;
3962                 writel(host_diagnostic, &ioc->chip->HostDiagnostic);
3963
3964                 drsprintk(ioc, printk(MPT2SAS_INFO_FMT
3965                     "re-enable the HCDW\n", ioc->name));
3966                 writel(hcb_size | MPI2_HCB_SIZE_HCB_ENABLE,
3967                     &ioc->chip->HCBSize);
3968         }
3969
3970         drsprintk(ioc, printk(MPT2SAS_INFO_FMT "restart the adapter\n",
3971             ioc->name));
3972         writel(host_diagnostic & ~MPI2_DIAG_HOLD_IOC_RESET,
3973             &ioc->chip->HostDiagnostic);
3974
3975         drsprintk(ioc, printk(MPT2SAS_INFO_FMT "disable writes to the "
3976             "diagnostic register\n", ioc->name));
3977         writel(MPI2_WRSEQ_FLUSH_KEY_VALUE, &ioc->chip->WriteSequence);
3978
3979         drsprintk(ioc, printk(MPT2SAS_INFO_FMT "Wait for FW to go to the "
3980             "READY state\n", ioc->name));
3981         ioc_state = _base_wait_on_iocstate(ioc, MPI2_IOC_STATE_READY, 20,
3982             sleep_flag);
3983         if (ioc_state) {
3984                 printk(MPT2SAS_ERR_FMT "%s: failed going to ready state "
3985                     " (ioc_state=0x%x)\n", ioc->name, __func__, ioc_state);
3986                 goto out;
3987         }
3988
3989         printk(MPT2SAS_INFO_FMT "diag reset: SUCCESS\n", ioc->name);
3990         return 0;
3991
3992  out:
3993         printk(MPT2SAS_ERR_FMT "diag reset: FAILED\n", ioc->name);
3994         return -EFAULT;
3995 }
3996
3997 /**
3998  * _base_make_ioc_ready - put controller in READY state
3999  * @ioc: per adapter object
4000  * @sleep_flag: CAN_SLEEP or NO_SLEEP
4001  * @type: FORCE_BIG_HAMMER or SOFT_RESET
4002  *
4003  * Returns 0 for success, non-zero for failure.
4004  */
4005 static int
4006 _base_make_ioc_ready(struct MPT2SAS_ADAPTER *ioc, int sleep_flag,
4007     enum reset_type type)
4008 {
4009         u32 ioc_state;
4010         int rc;
4011
4012         dinitprintk(ioc, printk(MPT2SAS_INFO_FMT "%s\n", ioc->name,
4013             __func__));
4014
4015         if (ioc->pci_error_recovery)
4016                 return 0;
4017
4018         ioc_state = mpt2sas_base_get_iocstate(ioc, 0);
4019         dhsprintk(ioc, printk(MPT2SAS_INFO_FMT "%s: ioc_state(0x%08x)\n",
4020             ioc->name, __func__, ioc_state));
4021
4022         if ((ioc_state & MPI2_IOC_STATE_MASK) == MPI2_IOC_STATE_READY)
4023                 return 0;
4024
4025         if (ioc_state & MPI2_DOORBELL_USED) {
4026                 dhsprintk(ioc, printk(MPT2SAS_INFO_FMT "unexpected doorbell "
4027                     "active!\n", ioc->name));
4028                 goto issue_diag_reset;
4029         }
4030
4031         if ((ioc_state & MPI2_IOC_STATE_MASK) == MPI2_IOC_STATE_FAULT) {
4032                 mpt2sas_base_fault_info(ioc, ioc_state &
4033                     MPI2_DOORBELL_DATA_MASK);
4034                 goto issue_diag_reset;
4035         }
4036
4037         if (type == FORCE_BIG_HAMMER)
4038                 goto issue_diag_reset;
4039
4040         if ((ioc_state & MPI2_IOC_STATE_MASK) == MPI2_IOC_STATE_OPERATIONAL)
4041                 if (!(_base_send_ioc_reset(ioc,
4042                     MPI2_FUNCTION_IOC_MESSAGE_UNIT_RESET, 15, CAN_SLEEP))) {
4043                         ioc->ioc_reset_count++;
4044                         return 0;
4045         }
4046
4047  issue_diag_reset:
4048         rc = _base_diag_reset(ioc, CAN_SLEEP);
4049         ioc->ioc_reset_count++;
4050         return rc;
4051 }
4052
4053 /**
4054  * _base_make_ioc_operational - put controller in OPERATIONAL state
4055  * @ioc: per adapter object
4056  * @sleep_flag: CAN_SLEEP or NO_SLEEP
4057  *
4058  * Returns 0 for success, non-zero for failure.
4059  */
4060 static int
4061 _base_make_ioc_operational(struct MPT2SAS_ADAPTER *ioc, int sleep_flag)
4062 {
4063         int r, i;
4064         unsigned long   flags;
4065         u32 reply_address;
4066         u16 smid;
4067         struct _tr_list *delayed_tr, *delayed_tr_next;
4068         u8 hide_flag;
4069         struct adapter_reply_queue *reply_q;
4070         long reply_post_free;
4071         u32 reply_post_free_sz;
4072
4073         dinitprintk(ioc, printk(MPT2SAS_INFO_FMT "%s\n", ioc->name,
4074             __func__));
4075
4076         /* clean the delayed target reset list */
4077         list_for_each_entry_safe(delayed_tr, delayed_tr_next,
4078             &ioc->delayed_tr_list, list) {
4079                 list_del(&delayed_tr->list);
4080                 kfree(delayed_tr);
4081         }
4082
4083         list_for_each_entry_safe(delayed_tr, delayed_tr_next,
4084             &ioc->delayed_tr_volume_list, list) {
4085                 list_del(&delayed_tr->list);
4086                 kfree(delayed_tr);
4087         }
4088
4089         /* initialize the scsi lookup free list */
4090         spin_lock_irqsave(&ioc->scsi_lookup_lock, flags);
4091         INIT_LIST_HEAD(&ioc->free_list);
4092         smid = 1;
4093         for (i = 0; i < ioc->scsiio_depth; i++, smid++) {
4094                 INIT_LIST_HEAD(&ioc->scsi_lookup[i].chain_list);
4095                 ioc->scsi_lookup[i].cb_idx = 0xFF;
4096                 ioc->scsi_lookup[i].smid = smid;
4097                 ioc->scsi_lookup[i].scmd = NULL;
4098                 ioc->scsi_lookup[i].direct_io = 0;
4099                 list_add_tail(&ioc->scsi_lookup[i].tracker_list,
4100                     &ioc->free_list);
4101         }
4102
4103         /* hi-priority queue */
4104         INIT_LIST_HEAD(&ioc->hpr_free_list);
4105         smid = ioc->hi_priority_smid;
4106         for (i = 0; i < ioc->hi_priority_depth; i++, smid++) {
4107                 ioc->hpr_lookup[i].cb_idx = 0xFF;
4108                 ioc->hpr_lookup[i].smid = smid;
4109                 list_add_tail(&ioc->hpr_lookup[i].tracker_list,
4110                     &ioc->hpr_free_list);
4111         }
4112
4113         /* internal queue */
4114         INIT_LIST_HEAD(&ioc->internal_free_list);
4115         smid = ioc->internal_smid;
4116         for (i = 0; i < ioc->internal_depth; i++, smid++) {
4117                 ioc->internal_lookup[i].cb_idx = 0xFF;
4118                 ioc->internal_lookup[i].smid = smid;
4119                 list_add_tail(&ioc->internal_lookup[i].tracker_list,
4120                     &ioc->internal_free_list);
4121         }
4122
4123         /* chain pool */
4124         INIT_LIST_HEAD(&ioc->free_chain_list);
4125         for (i = 0; i < ioc->chain_depth; i++)
4126                 list_add_tail(&ioc->chain_lookup[i].tracker_list,
4127                     &ioc->free_chain_list);
4128
4129         spin_unlock_irqrestore(&ioc->scsi_lookup_lock, flags);
4130
4131         /* initialize Reply Free Queue */
4132         for (i = 0, reply_address = (u32)ioc->reply_dma ;
4133             i < ioc->reply_free_queue_depth ; i++, reply_address +=
4134             ioc->reply_sz)
4135                 ioc->reply_free[i] = cpu_to_le32(reply_address);
4136
4137         /* initialize reply queues */
4138         if (ioc->is_driver_loading)
4139                 _base_assign_reply_queues(ioc);
4140
4141         /* initialize Reply Post Free Queue */
4142         reply_post_free = (long)ioc->reply_post_free;
4143         reply_post_free_sz = ioc->reply_post_queue_depth *
4144             sizeof(Mpi2DefaultReplyDescriptor_t);
4145         list_for_each_entry(reply_q, &ioc->reply_queue_list, list) {
4146                 reply_q->reply_post_host_index = 0;
4147                 reply_q->reply_post_free = (Mpi2ReplyDescriptorsUnion_t *)
4148                     reply_post_free;
4149                 for (i = 0; i < ioc->reply_post_queue_depth; i++)
4150                         reply_q->reply_post_free[i].Words =
4151                                                         cpu_to_le64(ULLONG_MAX);
4152                 if (!_base_is_controller_msix_enabled(ioc))
4153                         goto skip_init_reply_post_free_queue;
4154                 reply_post_free += reply_post_free_sz;
4155         }
4156  skip_init_reply_post_free_queue:
4157
4158         r = _base_send_ioc_init(ioc, sleep_flag);
4159         if (r)
4160                 return r;
4161
4162         /* initialize reply free host index */
4163         ioc->reply_free_host_index = ioc->reply_free_queue_depth - 1;
4164         writel(ioc->reply_free_host_index, &ioc->chip->ReplyFreeHostIndex);
4165
4166         /* initialize reply post host index */
4167         list_for_each_entry(reply_q, &ioc->reply_queue_list, list) {
4168                 writel(reply_q->msix_index << MPI2_RPHI_MSIX_INDEX_SHIFT,
4169                     &ioc->chip->ReplyPostHostIndex);
4170                 if (!_base_is_controller_msix_enabled(ioc))
4171                         goto skip_init_reply_post_host_index;
4172         }
4173
4174  skip_init_reply_post_host_index:
4175
4176         _base_unmask_interrupts(ioc);
4177
4178         r = _base_event_notification(ioc, sleep_flag);
4179         if (r)
4180                 return r;
4181
4182         if (sleep_flag == CAN_SLEEP)
4183                 _base_static_config_pages(ioc);
4184
4185
4186         if (ioc->is_driver_loading) {
4187                 if (ioc->is_warpdrive && ioc->manu_pg10.OEMIdentifier
4188                     == 0x80) {
4189                         hide_flag = (u8) (
4190                             le32_to_cpu(ioc->manu_pg10.OEMSpecificFlags0) &
4191                             MFG_PAGE10_HIDE_SSDS_MASK);
4192                         if (hide_flag != MFG_PAGE10_HIDE_SSDS_MASK)
4193                                 ioc->mfg_pg10_hide_flag = hide_flag;
4194                 }
4195                 ioc->wait_for_discovery_to_complete =
4196                     _base_determine_wait_on_discovery(ioc);
4197                 return r; /* scan_start and scan_finished support */
4198         }
4199         r = _base_send_port_enable(ioc, sleep_flag);
4200         if (r)
4201                 return r;
4202
4203         return r;
4204 }
4205
4206 /**
4207  * mpt2sas_base_free_resources - free resources controller resources (io/irq/memap)
4208  * @ioc: per adapter object
4209  *
4210  * Return nothing.
4211  */
4212 void
4213 mpt2sas_base_free_resources(struct MPT2SAS_ADAPTER *ioc)
4214 {
4215         struct pci_dev *pdev = ioc->pdev;
4216
4217         dexitprintk(ioc, printk(MPT2SAS_INFO_FMT "%s\n", ioc->name,
4218             __func__));
4219
4220         _base_mask_interrupts(ioc);
4221         ioc->shost_recovery = 1;
4222         _base_make_ioc_ready(ioc, CAN_SLEEP, SOFT_RESET);
4223         ioc->shost_recovery = 0;
4224         _base_free_irq(ioc);
4225         _base_disable_msix(ioc);
4226         if (ioc->chip_phys)
4227                 iounmap(ioc->chip);
4228         ioc->chip_phys = 0;
4229         pci_release_selected_regions(ioc->pdev, ioc->bars);
4230         pci_disable_pcie_error_reporting(pdev);
4231         pci_disable_device(pdev);
4232         return;
4233 }
4234
4235 /**
4236  * mpt2sas_base_attach - attach controller instance
4237  * @ioc: per adapter object
4238  *
4239  * Returns 0 for success, non-zero for failure.
4240  */
4241 int
4242 mpt2sas_base_attach(struct MPT2SAS_ADAPTER *ioc)
4243 {
4244         int r, i;
4245         int cpu_id, last_cpu_id = 0;
4246
4247         dinitprintk(ioc, printk(MPT2SAS_INFO_FMT "%s\n", ioc->name,
4248             __func__));
4249
4250         /* setup cpu_msix_table */
4251         ioc->cpu_count = num_online_cpus();
4252         for_each_online_cpu(cpu_id)
4253                 last_cpu_id = cpu_id;
4254         ioc->cpu_msix_table_sz = last_cpu_id + 1;
4255         ioc->cpu_msix_table = kzalloc(ioc->cpu_msix_table_sz, GFP_KERNEL);
4256         ioc->reply_queue_count = 1;
4257         if (!ioc->cpu_msix_table) {
4258                 dfailprintk(ioc, printk(MPT2SAS_INFO_FMT "allocation for "
4259                     "cpu_msix_table failed!!!\n", ioc->name));
4260                 r = -ENOMEM;
4261                 goto out_free_resources;
4262         }
4263
4264         if (ioc->is_warpdrive) {
4265                 ioc->reply_post_host_index = kcalloc(ioc->cpu_msix_table_sz,
4266                     sizeof(resource_size_t *), GFP_KERNEL);
4267                 if (!ioc->reply_post_host_index) {
4268                         dfailprintk(ioc, printk(MPT2SAS_INFO_FMT "allocation "
4269                                 "for cpu_msix_table failed!!!\n", ioc->name));
4270                         r = -ENOMEM;
4271                         goto out_free_resources;
4272                 }
4273         }
4274
4275         r = mpt2sas_base_map_resources(ioc);
4276         if (r)
4277                 goto out_free_resources;
4278
4279         if (ioc->is_warpdrive) {
4280                 ioc->reply_post_host_index[0] =
4281                     (resource_size_t *)&ioc->chip->ReplyPostHostIndex;
4282
4283                 for (i = 1; i < ioc->cpu_msix_table_sz; i++)
4284                         ioc->reply_post_host_index[i] = (resource_size_t *)
4285                         ((u8 *)&ioc->chip->Doorbell + (0x4000 + ((i - 1)
4286                         * 4)));
4287         }
4288
4289         pci_set_drvdata(ioc->pdev, ioc->shost);
4290         r = _base_get_ioc_facts(ioc, CAN_SLEEP);
4291         if (r)
4292                 goto out_free_resources;
4293
4294         r = _base_make_ioc_ready(ioc, CAN_SLEEP, SOFT_RESET);
4295         if (r)
4296                 goto out_free_resources;
4297
4298         ioc->pfacts = kcalloc(ioc->facts.NumberOfPorts,
4299             sizeof(struct mpt2sas_port_facts), GFP_KERNEL);
4300         if (!ioc->pfacts) {
4301                 r = -ENOMEM;
4302                 goto out_free_resources;
4303         }
4304
4305         for (i = 0 ; i < ioc->facts.NumberOfPorts; i++) {
4306                 r = _base_get_port_facts(ioc, i, CAN_SLEEP);
4307                 if (r)
4308                         goto out_free_resources;
4309         }
4310
4311         r = _base_allocate_memory_pools(ioc, CAN_SLEEP);
4312         if (r)
4313                 goto out_free_resources;
4314
4315         init_waitqueue_head(&ioc->reset_wq);
4316         /* allocate memory pd handle bitmask list */
4317         ioc->pd_handles_sz = (ioc->facts.MaxDevHandle / 8);
4318         if (ioc->facts.MaxDevHandle % 8)
4319                 ioc->pd_handles_sz++;
4320         ioc->pd_handles = kzalloc(ioc->pd_handles_sz,
4321             GFP_KERNEL);
4322         if (!ioc->pd_handles) {
4323                 r = -ENOMEM;
4324                 goto out_free_resources;
4325         }
4326         ioc->blocking_handles = kzalloc(ioc->pd_handles_sz,
4327             GFP_KERNEL);
4328         if (!ioc->blocking_handles) {
4329                 r = -ENOMEM;
4330                 goto out_free_resources;
4331         }
4332         ioc->fwfault_debug = mpt2sas_fwfault_debug;
4333
4334         /* base internal command bits */
4335         mutex_init(&ioc->base_cmds.mutex);
4336         ioc->base_cmds.reply = kzalloc(ioc->reply_sz, GFP_KERNEL);
4337         ioc->base_cmds.status = MPT2_CMD_NOT_USED;
4338
4339         /* port_enable command bits */
4340         ioc->port_enable_cmds.reply = kzalloc(ioc->reply_sz, GFP_KERNEL);
4341         ioc->port_enable_cmds.status = MPT2_CMD_NOT_USED;
4342
4343         /* transport internal command bits */
4344         ioc->transport_cmds.reply = kzalloc(ioc->reply_sz, GFP_KERNEL);
4345         ioc->transport_cmds.status = MPT2_CMD_NOT_USED;
4346         mutex_init(&ioc->transport_cmds.mutex);
4347
4348         /* scsih internal command bits */
4349         ioc->scsih_cmds.reply = kzalloc(ioc->reply_sz, GFP_KERNEL);
4350         ioc->scsih_cmds.status = MPT2_CMD_NOT_USED;
4351         mutex_init(&ioc->scsih_cmds.mutex);
4352
4353         /* task management internal command bits */
4354         ioc->tm_cmds.reply = kzalloc(ioc->reply_sz, GFP_KERNEL);
4355         ioc->tm_cmds.status = MPT2_CMD_NOT_USED;
4356         mutex_init(&ioc->tm_cmds.mutex);
4357
4358         /* config page internal command bits */
4359         ioc->config_cmds.reply = kzalloc(ioc->reply_sz, GFP_KERNEL);
4360         ioc->config_cmds.status = MPT2_CMD_NOT_USED;
4361         mutex_init(&ioc->config_cmds.mutex);
4362
4363         /* ctl module internal command bits */
4364         ioc->ctl_cmds.reply = kzalloc(ioc->reply_sz, GFP_KERNEL);
4365         ioc->ctl_cmds.sense = kzalloc(SCSI_SENSE_BUFFERSIZE, GFP_KERNEL);
4366         ioc->ctl_cmds.status = MPT2_CMD_NOT_USED;
4367         mutex_init(&ioc->ctl_cmds.mutex);
4368
4369         if (!ioc->base_cmds.reply || !ioc->transport_cmds.reply ||
4370             !ioc->scsih_cmds.reply || !ioc->tm_cmds.reply ||
4371             !ioc->config_cmds.reply || !ioc->ctl_cmds.reply ||
4372             !ioc->ctl_cmds.sense) {
4373                 r = -ENOMEM;
4374                 goto out_free_resources;
4375         }
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                 r = -ENOMEM;
4381                 goto out_free_resources;
4382         }
4383
4384         for (i = 0; i < MPI2_EVENT_NOTIFY_EVENTMASK_WORDS; i++)
4385                 ioc->event_masks[i] = -1;
4386
4387         /* here we enable the events we care about */
4388         _base_unmask_events(ioc, MPI2_EVENT_SAS_DISCOVERY);
4389         _base_unmask_events(ioc, MPI2_EVENT_SAS_BROADCAST_PRIMITIVE);
4390         _base_unmask_events(ioc, MPI2_EVENT_SAS_TOPOLOGY_CHANGE_LIST);
4391         _base_unmask_events(ioc, MPI2_EVENT_SAS_DEVICE_STATUS_CHANGE);
4392         _base_unmask_events(ioc, MPI2_EVENT_SAS_ENCL_DEVICE_STATUS_CHANGE);
4393         _base_unmask_events(ioc, MPI2_EVENT_IR_CONFIGURATION_CHANGE_LIST);
4394         _base_unmask_events(ioc, MPI2_EVENT_IR_VOLUME);
4395         _base_unmask_events(ioc, MPI2_EVENT_IR_PHYSICAL_DISK);
4396         _base_unmask_events(ioc, MPI2_EVENT_IR_OPERATION_STATUS);
4397         _base_unmask_events(ioc, MPI2_EVENT_LOG_ENTRY_ADDED);
4398         r = _base_make_ioc_operational(ioc, CAN_SLEEP);
4399         if (r)
4400                 goto out_free_resources;
4401
4402         if (missing_delay[0] != -1 && missing_delay[1] != -1)
4403                 _base_update_missing_delay(ioc, missing_delay[0],
4404                     missing_delay[1]);
4405         ioc->non_operational_loop = 0;
4406
4407         return 0;
4408
4409  out_free_resources:
4410
4411         ioc->remove_host = 1;
4412         mpt2sas_base_free_resources(ioc);
4413         _base_release_memory_pools(ioc);
4414         pci_set_drvdata(ioc->pdev, NULL);
4415         kfree(ioc->cpu_msix_table);
4416         if (ioc->is_warpdrive)
4417                 kfree(ioc->reply_post_host_index);
4418         kfree(ioc->pd_handles);
4419         kfree(ioc->blocking_handles);
4420         kfree(ioc->tm_cmds.reply);
4421         kfree(ioc->transport_cmds.reply);
4422         kfree(ioc->scsih_cmds.reply);
4423         kfree(ioc->config_cmds.reply);
4424         kfree(ioc->base_cmds.reply);
4425         kfree(ioc->port_enable_cmds.reply);
4426         kfree(ioc->ctl_cmds.reply);
4427         kfree(ioc->ctl_cmds.sense);
4428         kfree(ioc->pfacts);
4429         ioc->ctl_cmds.reply = NULL;
4430         ioc->base_cmds.reply = NULL;
4431         ioc->tm_cmds.reply = NULL;
4432         ioc->scsih_cmds.reply = NULL;
4433         ioc->transport_cmds.reply = NULL;
4434         ioc->config_cmds.reply = NULL;
4435         ioc->pfacts = NULL;
4436         return r;
4437 }
4438
4439
4440 /**
4441  * mpt2sas_base_detach - remove controller instance
4442  * @ioc: per adapter object
4443  *
4444  * Return nothing.
4445  */
4446 void
4447 mpt2sas_base_detach(struct MPT2SAS_ADAPTER *ioc)
4448 {
4449
4450         dexitprintk(ioc, printk(MPT2SAS_INFO_FMT "%s\n", ioc->name,
4451             __func__));
4452
4453         mpt2sas_base_stop_watchdog(ioc);
4454         mpt2sas_base_free_resources(ioc);
4455         _base_release_memory_pools(ioc);
4456         pci_set_drvdata(ioc->pdev, NULL);
4457         kfree(ioc->cpu_msix_table);
4458         if (ioc->is_warpdrive)
4459                 kfree(ioc->reply_post_host_index);
4460         kfree(ioc->pd_handles);
4461         kfree(ioc->blocking_handles);
4462         kfree(ioc->pfacts);
4463         kfree(ioc->ctl_cmds.reply);
4464         kfree(ioc->ctl_cmds.sense);
4465         kfree(ioc->base_cmds.reply);
4466         kfree(ioc->port_enable_cmds.reply);
4467         kfree(ioc->tm_cmds.reply);
4468         kfree(ioc->transport_cmds.reply);
4469         kfree(ioc->scsih_cmds.reply);
4470         kfree(ioc->config_cmds.reply);
4471 }
4472
4473 /**
4474  * _base_reset_handler - reset callback handler (for base)
4475  * @ioc: per adapter object
4476  * @reset_phase: phase
4477  *
4478  * The handler for doing any required cleanup or initialization.
4479  *
4480  * The reset phase can be MPT2_IOC_PRE_RESET, MPT2_IOC_AFTER_RESET,
4481  * MPT2_IOC_DONE_RESET
4482  *
4483  * Return nothing.
4484  */
4485 static void
4486 _base_reset_handler(struct MPT2SAS_ADAPTER *ioc, int reset_phase)
4487 {
4488         mpt2sas_scsih_reset_handler(ioc, reset_phase);
4489         mpt2sas_ctl_reset_handler(ioc, reset_phase);
4490         switch (reset_phase) {
4491         case MPT2_IOC_PRE_RESET:
4492                 dtmprintk(ioc, printk(MPT2SAS_INFO_FMT "%s: "
4493                     "MPT2_IOC_PRE_RESET\n", ioc->name, __func__));
4494                 break;
4495         case MPT2_IOC_AFTER_RESET:
4496                 dtmprintk(ioc, printk(MPT2SAS_INFO_FMT "%s: "
4497                     "MPT2_IOC_AFTER_RESET\n", ioc->name, __func__));
4498                 if (ioc->transport_cmds.status & MPT2_CMD_PENDING) {
4499                         ioc->transport_cmds.status |= MPT2_CMD_RESET;
4500                         mpt2sas_base_free_smid(ioc, ioc->transport_cmds.smid);
4501                         complete(&ioc->transport_cmds.done);
4502                 }
4503                 if (ioc->base_cmds.status & MPT2_CMD_PENDING) {
4504                         ioc->base_cmds.status |= MPT2_CMD_RESET;
4505                         mpt2sas_base_free_smid(ioc, ioc->base_cmds.smid);
4506                         complete(&ioc->base_cmds.done);
4507                 }
4508                 if (ioc->port_enable_cmds.status & MPT2_CMD_PENDING) {
4509                         ioc->port_enable_failed = 1;
4510                         ioc->port_enable_cmds.status |= MPT2_CMD_RESET;
4511                         mpt2sas_base_free_smid(ioc, ioc->port_enable_cmds.smid);
4512                         if (ioc->is_driver_loading) {
4513                                 ioc->start_scan_failed =
4514                                     MPI2_IOCSTATUS_INTERNAL_ERROR;
4515                                 ioc->start_scan = 0;
4516                                 ioc->port_enable_cmds.status =
4517                                                 MPT2_CMD_NOT_USED;
4518                         } else
4519                                 complete(&ioc->port_enable_cmds.done);
4520
4521                 }
4522                 if (ioc->config_cmds.status & MPT2_CMD_PENDING) {
4523                         ioc->config_cmds.status |= MPT2_CMD_RESET;
4524                         mpt2sas_base_free_smid(ioc, ioc->config_cmds.smid);
4525                         ioc->config_cmds.smid = USHRT_MAX;
4526                         complete(&ioc->config_cmds.done);
4527                 }
4528                 break;
4529         case MPT2_IOC_DONE_RESET:
4530                 dtmprintk(ioc, printk(MPT2SAS_INFO_FMT "%s: "
4531                     "MPT2_IOC_DONE_RESET\n", ioc->name, __func__));
4532                 break;
4533         }
4534 }
4535
4536 /**
4537  * _wait_for_commands_to_complete - reset controller
4538  * @ioc: Pointer to MPT_ADAPTER structure
4539  * @sleep_flag: CAN_SLEEP or NO_SLEEP
4540  *
4541  * This function waiting(3s) for all pending commands to complete
4542  * prior to putting controller in reset.
4543  */
4544 static void
4545 _wait_for_commands_to_complete(struct MPT2SAS_ADAPTER *ioc, int sleep_flag)
4546 {
4547         u32 ioc_state;
4548         unsigned long flags;
4549         u16 i;
4550
4551         ioc->pending_io_count = 0;
4552         if (sleep_flag != CAN_SLEEP)
4553                 return;
4554
4555         ioc_state = mpt2sas_base_get_iocstate(ioc, 0);
4556         if ((ioc_state & MPI2_IOC_STATE_MASK) != MPI2_IOC_STATE_OPERATIONAL)
4557                 return;
4558
4559         /* pending command count */
4560         spin_lock_irqsave(&ioc->scsi_lookup_lock, flags);
4561         for (i = 0; i < ioc->scsiio_depth; i++)
4562                 if (ioc->scsi_lookup[i].cb_idx != 0xFF)
4563                         ioc->pending_io_count++;
4564         spin_unlock_irqrestore(&ioc->scsi_lookup_lock, flags);
4565
4566         if (!ioc->pending_io_count)
4567                 return;
4568
4569         /* wait for pending commands to complete */
4570         wait_event_timeout(ioc->reset_wq, ioc->pending_io_count == 0, 10 * HZ);
4571 }
4572
4573 /**
4574  * mpt2sas_base_hard_reset_handler - reset controller
4575  * @ioc: Pointer to MPT_ADAPTER structure
4576  * @sleep_flag: CAN_SLEEP or NO_SLEEP
4577  * @type: FORCE_BIG_HAMMER or SOFT_RESET
4578  *
4579  * Returns 0 for success, non-zero for failure.
4580  */
4581 int
4582 mpt2sas_base_hard_reset_handler(struct MPT2SAS_ADAPTER *ioc, int sleep_flag,
4583     enum reset_type type)
4584 {
4585         int r;
4586         unsigned long flags;
4587
4588         dtmprintk(ioc, printk(MPT2SAS_INFO_FMT "%s: enter\n", ioc->name,
4589             __func__));
4590
4591         if (ioc->pci_error_recovery) {
4592                 printk(MPT2SAS_ERR_FMT "%s: pci error recovery reset\n",
4593                     ioc->name, __func__);
4594                 r = 0;
4595                 goto out_unlocked;
4596         }
4597
4598         if (mpt2sas_fwfault_debug)
4599                 mpt2sas_halt_firmware(ioc);
4600
4601         /* TODO - What we really should be doing is pulling
4602          * out all the code associated with NO_SLEEP; its never used.
4603          * That is legacy code from mpt fusion driver, ported over.
4604          * I will leave this BUG_ON here for now till its been resolved.
4605          */
4606         BUG_ON(sleep_flag == NO_SLEEP);
4607
4608         /* wait for an active reset in progress to complete */
4609         if (!mutex_trylock(&ioc->reset_in_progress_mutex)) {
4610                 do {
4611                         ssleep(1);
4612                 } while (ioc->shost_recovery == 1);
4613                 dtmprintk(ioc, printk(MPT2SAS_INFO_FMT "%s: exit\n", ioc->name,
4614                     __func__));
4615                 return ioc->ioc_reset_in_progress_status;
4616         }
4617
4618         spin_lock_irqsave(&ioc->ioc_reset_in_progress_lock, flags);
4619         ioc->shost_recovery = 1;
4620         spin_unlock_irqrestore(&ioc->ioc_reset_in_progress_lock, flags);
4621
4622         _base_reset_handler(ioc, MPT2_IOC_PRE_RESET);
4623         _wait_for_commands_to_complete(ioc, sleep_flag);
4624         _base_mask_interrupts(ioc);
4625         r = _base_make_ioc_ready(ioc, sleep_flag, type);
4626         if (r)
4627                 goto out;
4628         _base_reset_handler(ioc, MPT2_IOC_AFTER_RESET);
4629
4630         /* If this hard reset is called while port enable is active, then
4631          * there is no reason to call make_ioc_operational
4632          */
4633         if (ioc->is_driver_loading && ioc->port_enable_failed) {
4634                 ioc->remove_host = 1;
4635                 r = -EFAULT;
4636                 goto out;
4637         }
4638         r = _base_make_ioc_operational(ioc, sleep_flag);
4639         if (!r)
4640                 _base_reset_handler(ioc, MPT2_IOC_DONE_RESET);
4641  out:
4642         dtmprintk(ioc, printk(MPT2SAS_INFO_FMT "%s: %s\n",
4643             ioc->name, __func__, ((r == 0) ? "SUCCESS" : "FAILED")));
4644
4645         spin_lock_irqsave(&ioc->ioc_reset_in_progress_lock, flags);
4646         ioc->ioc_reset_in_progress_status = r;
4647         ioc->shost_recovery = 0;
4648         spin_unlock_irqrestore(&ioc->ioc_reset_in_progress_lock, flags);
4649         mutex_unlock(&ioc->reset_in_progress_mutex);
4650
4651  out_unlocked:
4652         dtmprintk(ioc, printk(MPT2SAS_INFO_FMT "%s: exit\n", ioc->name,
4653             __func__));
4654         return r;
4655 }