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