mpt2sas: Removed redundant calling of _scsih_probe_devices() from _scsih_probe
[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         base = pci_find_capability(ioc->pdev, PCI_CAP_ID_MSIX);
1160         if (!base) {
1161                 dfailprintk(ioc, printk(MPT2SAS_INFO_FMT "msix not "
1162                     "supported\n", ioc->name));
1163                 return -EINVAL;
1164         }
1165
1166         /* get msix vector count */
1167         /* NUMA_IO not supported for older controllers */
1168         if (ioc->pdev->device == MPI2_MFGPAGE_DEVID_SAS2004 ||
1169             ioc->pdev->device == MPI2_MFGPAGE_DEVID_SAS2008 ||
1170             ioc->pdev->device == MPI2_MFGPAGE_DEVID_SAS2108_1 ||
1171             ioc->pdev->device == MPI2_MFGPAGE_DEVID_SAS2108_2 ||
1172             ioc->pdev->device == MPI2_MFGPAGE_DEVID_SAS2108_3 ||
1173             ioc->pdev->device == MPI2_MFGPAGE_DEVID_SAS2116_1 ||
1174             ioc->pdev->device == MPI2_MFGPAGE_DEVID_SAS2116_2)
1175                 ioc->msix_vector_count = 1;
1176         else {
1177                 pci_read_config_word(ioc->pdev, base + 2, &message_control);
1178                 ioc->msix_vector_count = (message_control & 0x3FF) + 1;
1179         }
1180         dinitprintk(ioc, printk(MPT2SAS_INFO_FMT "msix is supported, "
1181             "vector_count(%d)\n", ioc->name, ioc->msix_vector_count));
1182
1183         return 0;
1184 }
1185
1186 /**
1187  * _base_free_irq - free irq
1188  * @ioc: per adapter object
1189  *
1190  * Freeing respective reply_queue from the list.
1191  */
1192 static void
1193 _base_free_irq(struct MPT2SAS_ADAPTER *ioc)
1194 {
1195         struct adapter_reply_queue *reply_q, *next;
1196
1197         if (list_empty(&ioc->reply_queue_list))
1198                 return;
1199
1200         list_for_each_entry_safe(reply_q, next, &ioc->reply_queue_list, list) {
1201                 list_del(&reply_q->list);
1202                 synchronize_irq(reply_q->vector);
1203                 free_irq(reply_q->vector, reply_q);
1204                 kfree(reply_q);
1205         }
1206 }
1207
1208 /**
1209  * _base_request_irq - request irq
1210  * @ioc: per adapter object
1211  * @index: msix index into vector table
1212  * @vector: irq vector
1213  *
1214  * Inserting respective reply_queue into the list.
1215  */
1216 static int
1217 _base_request_irq(struct MPT2SAS_ADAPTER *ioc, u8 index, u32 vector)
1218 {
1219         struct adapter_reply_queue *reply_q;
1220         int r;
1221
1222         reply_q =  kzalloc(sizeof(struct adapter_reply_queue), GFP_KERNEL);
1223         if (!reply_q) {
1224                 printk(MPT2SAS_ERR_FMT "unable to allocate memory %d!\n",
1225                     ioc->name, (int)sizeof(struct adapter_reply_queue));
1226                 return -ENOMEM;
1227         }
1228         reply_q->ioc = ioc;
1229         reply_q->msix_index = index;
1230         reply_q->vector = vector;
1231         atomic_set(&reply_q->busy, 0);
1232         if (ioc->msix_enable)
1233                 snprintf(reply_q->name, MPT_NAME_LENGTH, "%s%d-msix%d",
1234                     MPT2SAS_DRIVER_NAME, ioc->id, index);
1235         else
1236                 snprintf(reply_q->name, MPT_NAME_LENGTH, "%s%d",
1237                     MPT2SAS_DRIVER_NAME, ioc->id);
1238         r = request_irq(vector, _base_interrupt, IRQF_SHARED, reply_q->name,
1239             reply_q);
1240         if (r) {
1241                 printk(MPT2SAS_ERR_FMT "unable to allocate interrupt %d!\n",
1242                     reply_q->name, vector);
1243                 kfree(reply_q);
1244                 return -EBUSY;
1245         }
1246
1247         INIT_LIST_HEAD(&reply_q->list);
1248         list_add_tail(&reply_q->list, &ioc->reply_queue_list);
1249         return 0;
1250 }
1251
1252 /**
1253  * _base_assign_reply_queues - assigning msix index for each cpu
1254  * @ioc: per adapter object
1255  *
1256  * The enduser would need to set the affinity via /proc/irq/#/smp_affinity
1257  *
1258  * It would nice if we could call irq_set_affinity, however it is not
1259  * an exported symbol
1260  */
1261 static void
1262 _base_assign_reply_queues(struct MPT2SAS_ADAPTER *ioc)
1263 {
1264         struct adapter_reply_queue *reply_q;
1265         int cpu_id;
1266         int cpu_grouping, loop, grouping, grouping_mod;
1267
1268         if (!_base_is_controller_msix_enabled(ioc))
1269                 return;
1270
1271         memset(ioc->cpu_msix_table, 0, ioc->cpu_msix_table_sz);
1272         /* when there are more cpus than available msix vectors,
1273          * then group cpus togeather on same irq
1274          */
1275         if (ioc->cpu_count > ioc->msix_vector_count) {
1276                 grouping = ioc->cpu_count / ioc->msix_vector_count;
1277                 grouping_mod = ioc->cpu_count % ioc->msix_vector_count;
1278                 if (grouping < 2 || (grouping == 2 && !grouping_mod))
1279                         cpu_grouping = 2;
1280                 else if (grouping < 4 || (grouping == 4 && !grouping_mod))
1281                         cpu_grouping = 4;
1282                 else if (grouping < 8 || (grouping == 8 && !grouping_mod))
1283                         cpu_grouping = 8;
1284                 else
1285                         cpu_grouping = 16;
1286         } else
1287                 cpu_grouping = 0;
1288
1289         loop = 0;
1290         reply_q = list_entry(ioc->reply_queue_list.next,
1291              struct adapter_reply_queue, list);
1292         for_each_online_cpu(cpu_id) {
1293                 if (!cpu_grouping) {
1294                         ioc->cpu_msix_table[cpu_id] = reply_q->msix_index;
1295                         reply_q = list_entry(reply_q->list.next,
1296                             struct adapter_reply_queue, list);
1297                 } else {
1298                         if (loop < cpu_grouping) {
1299                                 ioc->cpu_msix_table[cpu_id] =
1300                                         reply_q->msix_index;
1301                                 loop++;
1302                         } else {
1303                                 reply_q = list_entry(reply_q->list.next,
1304                                     struct adapter_reply_queue, list);
1305                                 ioc->cpu_msix_table[cpu_id] =
1306                                         reply_q->msix_index;
1307                                 loop = 1;
1308                         }
1309                 }
1310         }
1311 }
1312
1313 /**
1314  * _base_disable_msix - disables msix
1315  * @ioc: per adapter object
1316  *
1317  */
1318 static void
1319 _base_disable_msix(struct MPT2SAS_ADAPTER *ioc)
1320 {
1321         if (ioc->msix_enable) {
1322                 pci_disable_msix(ioc->pdev);
1323                 ioc->msix_enable = 0;
1324         }
1325 }
1326
1327 /**
1328  * _base_enable_msix - enables msix, failback to io_apic
1329  * @ioc: per adapter object
1330  *
1331  */
1332 static int
1333 _base_enable_msix(struct MPT2SAS_ADAPTER *ioc)
1334 {
1335         struct msix_entry *entries, *a;
1336         int r;
1337         int i;
1338         u8 try_msix = 0;
1339
1340         INIT_LIST_HEAD(&ioc->reply_queue_list);
1341
1342         if (msix_disable == -1 || msix_disable == 0)
1343                 try_msix = 1;
1344
1345         if (!try_msix)
1346                 goto try_ioapic;
1347
1348         if (_base_check_enable_msix(ioc) != 0)
1349                 goto try_ioapic;
1350
1351         ioc->reply_queue_count = min_t(u8, ioc->cpu_count,
1352             ioc->msix_vector_count);
1353
1354         entries = kcalloc(ioc->reply_queue_count, sizeof(struct msix_entry),
1355             GFP_KERNEL);
1356         if (!entries) {
1357                 dfailprintk(ioc, printk(MPT2SAS_INFO_FMT "kcalloc "
1358                     "failed @ at %s:%d/%s() !!!\n", ioc->name, __FILE__,
1359                     __LINE__, __func__));
1360                 goto try_ioapic;
1361         }
1362
1363         for (i = 0, a = entries; i < ioc->reply_queue_count; i++, a++)
1364                 a->entry = i;
1365
1366         r = pci_enable_msix(ioc->pdev, entries, ioc->reply_queue_count);
1367         if (r) {
1368                 dfailprintk(ioc, printk(MPT2SAS_INFO_FMT "pci_enable_msix "
1369                     "failed (r=%d) !!!\n", ioc->name, r));
1370                 kfree(entries);
1371                 goto try_ioapic;
1372         }
1373
1374         ioc->msix_enable = 1;
1375         for (i = 0, a = entries; i < ioc->reply_queue_count; i++, a++) {
1376                 r = _base_request_irq(ioc, i, a->vector);
1377                 if (r) {
1378                         _base_free_irq(ioc);
1379                         _base_disable_msix(ioc);
1380                         kfree(entries);
1381                         goto try_ioapic;
1382                 }
1383         }
1384
1385         kfree(entries);
1386         return 0;
1387
1388 /* failback to io_apic interrupt routing */
1389  try_ioapic:
1390
1391         r = _base_request_irq(ioc, 0, ioc->pdev->irq);
1392
1393         return r;
1394 }
1395
1396 /**
1397  * mpt2sas_base_map_resources - map in controller resources (io/irq/memap)
1398  * @ioc: per adapter object
1399  *
1400  * Returns 0 for success, non-zero for failure.
1401  */
1402 int
1403 mpt2sas_base_map_resources(struct MPT2SAS_ADAPTER *ioc)
1404 {
1405         struct pci_dev *pdev = ioc->pdev;
1406         u32 memap_sz;
1407         u32 pio_sz;
1408         int i, r = 0;
1409         u64 pio_chip = 0;
1410         u64 chip_phys = 0;
1411         struct adapter_reply_queue *reply_q;
1412
1413         dinitprintk(ioc, printk(MPT2SAS_INFO_FMT "%s\n",
1414             ioc->name, __func__));
1415
1416         ioc->bars = pci_select_bars(pdev, IORESOURCE_MEM);
1417         if (pci_enable_device_mem(pdev)) {
1418                 printk(MPT2SAS_WARN_FMT "pci_enable_device_mem: "
1419                     "failed\n", ioc->name);
1420                 return -ENODEV;
1421         }
1422
1423
1424         if (pci_request_selected_regions(pdev, ioc->bars,
1425             MPT2SAS_DRIVER_NAME)) {
1426                 printk(MPT2SAS_WARN_FMT "pci_request_selected_regions: "
1427                     "failed\n", ioc->name);
1428                 r = -ENODEV;
1429                 goto out_fail;
1430         }
1431
1432         /* AER (Advanced Error Reporting) hooks */
1433         pci_enable_pcie_error_reporting(pdev);
1434
1435         pci_set_master(pdev);
1436
1437         if (_base_config_dma_addressing(ioc, pdev) != 0) {
1438                 printk(MPT2SAS_WARN_FMT "no suitable DMA mask for %s\n",
1439                     ioc->name, pci_name(pdev));
1440                 r = -ENODEV;
1441                 goto out_fail;
1442         }
1443
1444         for (i = 0, memap_sz = 0, pio_sz = 0 ; i < DEVICE_COUNT_RESOURCE; i++) {
1445                 if (pci_resource_flags(pdev, i) & IORESOURCE_IO) {
1446                         if (pio_sz)
1447                                 continue;
1448                         pio_chip = (u64)pci_resource_start(pdev, i);
1449                         pio_sz = pci_resource_len(pdev, i);
1450                 } else {
1451                         if (memap_sz)
1452                                 continue;
1453                         /* verify memory resource is valid before using */
1454                         if (pci_resource_flags(pdev, i) & IORESOURCE_MEM) {
1455                                 ioc->chip_phys = pci_resource_start(pdev, i);
1456                                 chip_phys = (u64)ioc->chip_phys;
1457                                 memap_sz = pci_resource_len(pdev, i);
1458                                 ioc->chip = ioremap(ioc->chip_phys, memap_sz);
1459                                 if (ioc->chip == NULL) {
1460                                         printk(MPT2SAS_ERR_FMT "unable to map "
1461                                             "adapter memory!\n", ioc->name);
1462                                         r = -EINVAL;
1463                                         goto out_fail;
1464                                 }
1465                         }
1466                 }
1467         }
1468
1469         _base_mask_interrupts(ioc);
1470         r = _base_enable_msix(ioc);
1471         if (r)
1472                 goto out_fail;
1473
1474         list_for_each_entry(reply_q, &ioc->reply_queue_list, list)
1475                 printk(MPT2SAS_INFO_FMT "%s: IRQ %d\n",
1476                     reply_q->name,  ((ioc->msix_enable) ? "PCI-MSI-X enabled" :
1477                     "IO-APIC enabled"), reply_q->vector);
1478
1479         printk(MPT2SAS_INFO_FMT "iomem(0x%016llx), mapped(0x%p), size(%d)\n",
1480             ioc->name, (unsigned long long)chip_phys, ioc->chip, memap_sz);
1481         printk(MPT2SAS_INFO_FMT "ioport(0x%016llx), size(%d)\n",
1482             ioc->name, (unsigned long long)pio_chip, pio_sz);
1483
1484         /* Save PCI configuration state for recovery from PCI AER/EEH errors */
1485         pci_save_state(pdev);
1486
1487         return 0;
1488
1489  out_fail:
1490         if (ioc->chip_phys)
1491                 iounmap(ioc->chip);
1492         ioc->chip_phys = 0;
1493         pci_release_selected_regions(ioc->pdev, ioc->bars);
1494         pci_disable_pcie_error_reporting(pdev);
1495         pci_disable_device(pdev);
1496         return r;
1497 }
1498
1499 /**
1500  * mpt2sas_base_get_msg_frame - obtain request mf pointer
1501  * @ioc: per adapter object
1502  * @smid: system request message index(smid zero is invalid)
1503  *
1504  * Returns virt pointer to message frame.
1505  */
1506 void *
1507 mpt2sas_base_get_msg_frame(struct MPT2SAS_ADAPTER *ioc, u16 smid)
1508 {
1509         return (void *)(ioc->request + (smid * ioc->request_sz));
1510 }
1511
1512 /**
1513  * mpt2sas_base_get_sense_buffer - obtain a sense buffer assigned to a mf request
1514  * @ioc: per adapter object
1515  * @smid: system request message index
1516  *
1517  * Returns virt pointer to sense buffer.
1518  */
1519 void *
1520 mpt2sas_base_get_sense_buffer(struct MPT2SAS_ADAPTER *ioc, u16 smid)
1521 {
1522         return (void *)(ioc->sense + ((smid - 1) * SCSI_SENSE_BUFFERSIZE));
1523 }
1524
1525 /**
1526  * mpt2sas_base_get_sense_buffer_dma - obtain a sense buffer assigned to a mf request
1527  * @ioc: per adapter object
1528  * @smid: system request message index
1529  *
1530  * Returns phys pointer to the low 32bit address of the sense buffer.
1531  */
1532 __le32
1533 mpt2sas_base_get_sense_buffer_dma(struct MPT2SAS_ADAPTER *ioc, u16 smid)
1534 {
1535         return cpu_to_le32(ioc->sense_dma +
1536                         ((smid - 1) * SCSI_SENSE_BUFFERSIZE));
1537 }
1538
1539 /**
1540  * mpt2sas_base_get_reply_virt_addr - obtain reply frames virt address
1541  * @ioc: per adapter object
1542  * @phys_addr: lower 32 physical addr of the reply
1543  *
1544  * Converts 32bit lower physical addr into a virt address.
1545  */
1546 void *
1547 mpt2sas_base_get_reply_virt_addr(struct MPT2SAS_ADAPTER *ioc, u32 phys_addr)
1548 {
1549         if (!phys_addr)
1550                 return NULL;
1551         return ioc->reply + (phys_addr - (u32)ioc->reply_dma);
1552 }
1553
1554 /**
1555  * mpt2sas_base_get_smid - obtain a free smid from internal queue
1556  * @ioc: per adapter object
1557  * @cb_idx: callback index
1558  *
1559  * Returns smid (zero is invalid)
1560  */
1561 u16
1562 mpt2sas_base_get_smid(struct MPT2SAS_ADAPTER *ioc, u8 cb_idx)
1563 {
1564         unsigned long flags;
1565         struct request_tracker *request;
1566         u16 smid;
1567
1568         spin_lock_irqsave(&ioc->scsi_lookup_lock, flags);
1569         if (list_empty(&ioc->internal_free_list)) {
1570                 spin_unlock_irqrestore(&ioc->scsi_lookup_lock, flags);
1571                 printk(MPT2SAS_ERR_FMT "%s: smid not available\n",
1572                     ioc->name, __func__);
1573                 return 0;
1574         }
1575
1576         request = list_entry(ioc->internal_free_list.next,
1577             struct request_tracker, tracker_list);
1578         request->cb_idx = cb_idx;
1579         smid = request->smid;
1580         list_del(&request->tracker_list);
1581         spin_unlock_irqrestore(&ioc->scsi_lookup_lock, flags);
1582         return smid;
1583 }
1584
1585 /**
1586  * mpt2sas_base_get_smid_scsiio - obtain a free smid from scsiio queue
1587  * @ioc: per adapter object
1588  * @cb_idx: callback index
1589  * @scmd: pointer to scsi command object
1590  *
1591  * Returns smid (zero is invalid)
1592  */
1593 u16
1594 mpt2sas_base_get_smid_scsiio(struct MPT2SAS_ADAPTER *ioc, u8 cb_idx,
1595     struct scsi_cmnd *scmd)
1596 {
1597         unsigned long flags;
1598         struct scsiio_tracker *request;
1599         u16 smid;
1600
1601         spin_lock_irqsave(&ioc->scsi_lookup_lock, flags);
1602         if (list_empty(&ioc->free_list)) {
1603                 spin_unlock_irqrestore(&ioc->scsi_lookup_lock, flags);
1604                 printk(MPT2SAS_ERR_FMT "%s: smid not available\n",
1605                     ioc->name, __func__);
1606                 return 0;
1607         }
1608
1609         request = list_entry(ioc->free_list.next,
1610             struct scsiio_tracker, tracker_list);
1611         request->scmd = scmd;
1612         request->cb_idx = cb_idx;
1613         smid = request->smid;
1614         list_del(&request->tracker_list);
1615         spin_unlock_irqrestore(&ioc->scsi_lookup_lock, flags);
1616         return smid;
1617 }
1618
1619 /**
1620  * mpt2sas_base_get_smid_hpr - obtain a free smid from hi-priority queue
1621  * @ioc: per adapter object
1622  * @cb_idx: callback index
1623  *
1624  * Returns smid (zero is invalid)
1625  */
1626 u16
1627 mpt2sas_base_get_smid_hpr(struct MPT2SAS_ADAPTER *ioc, u8 cb_idx)
1628 {
1629         unsigned long flags;
1630         struct request_tracker *request;
1631         u16 smid;
1632
1633         spin_lock_irqsave(&ioc->scsi_lookup_lock, flags);
1634         if (list_empty(&ioc->hpr_free_list)) {
1635                 spin_unlock_irqrestore(&ioc->scsi_lookup_lock, flags);
1636                 return 0;
1637         }
1638
1639         request = list_entry(ioc->hpr_free_list.next,
1640             struct request_tracker, tracker_list);
1641         request->cb_idx = cb_idx;
1642         smid = request->smid;
1643         list_del(&request->tracker_list);
1644         spin_unlock_irqrestore(&ioc->scsi_lookup_lock, flags);
1645         return smid;
1646 }
1647
1648
1649 /**
1650  * mpt2sas_base_free_smid - put smid back on free_list
1651  * @ioc: per adapter object
1652  * @smid: system request message index
1653  *
1654  * Return nothing.
1655  */
1656 void
1657 mpt2sas_base_free_smid(struct MPT2SAS_ADAPTER *ioc, u16 smid)
1658 {
1659         unsigned long flags;
1660         int i;
1661         struct chain_tracker *chain_req, *next;
1662
1663         spin_lock_irqsave(&ioc->scsi_lookup_lock, flags);
1664         if (smid < ioc->hi_priority_smid) {
1665                 /* scsiio queue */
1666                 i = smid - 1;
1667                 if (!list_empty(&ioc->scsi_lookup[i].chain_list)) {
1668                         list_for_each_entry_safe(chain_req, next,
1669                             &ioc->scsi_lookup[i].chain_list, tracker_list) {
1670                                 list_del_init(&chain_req->tracker_list);
1671                                 list_add_tail(&chain_req->tracker_list,
1672                                     &ioc->free_chain_list);
1673                         }
1674                 }
1675                 ioc->scsi_lookup[i].cb_idx = 0xFF;
1676                 ioc->scsi_lookup[i].scmd = NULL;
1677                 ioc->scsi_lookup[i].direct_io = 0;
1678                 list_add_tail(&ioc->scsi_lookup[i].tracker_list,
1679                     &ioc->free_list);
1680                 spin_unlock_irqrestore(&ioc->scsi_lookup_lock, flags);
1681
1682                 /*
1683                  * See _wait_for_commands_to_complete() call with regards
1684                  * to this code.
1685                  */
1686                 if (ioc->shost_recovery && ioc->pending_io_count) {
1687                         if (ioc->pending_io_count == 1)
1688                                 wake_up(&ioc->reset_wq);
1689                         ioc->pending_io_count--;
1690                 }
1691                 return;
1692         } else if (smid < ioc->internal_smid) {
1693                 /* hi-priority */
1694                 i = smid - ioc->hi_priority_smid;
1695                 ioc->hpr_lookup[i].cb_idx = 0xFF;
1696                 list_add_tail(&ioc->hpr_lookup[i].tracker_list,
1697                     &ioc->hpr_free_list);
1698         } else if (smid <= ioc->hba_queue_depth) {
1699                 /* internal queue */
1700                 i = smid - ioc->internal_smid;
1701                 ioc->internal_lookup[i].cb_idx = 0xFF;
1702                 list_add_tail(&ioc->internal_lookup[i].tracker_list,
1703                     &ioc->internal_free_list);
1704         }
1705         spin_unlock_irqrestore(&ioc->scsi_lookup_lock, flags);
1706 }
1707
1708 /**
1709  * _base_writeq - 64 bit write to MMIO
1710  * @ioc: per adapter object
1711  * @b: data payload
1712  * @addr: address in MMIO space
1713  * @writeq_lock: spin lock
1714  *
1715  * Glue for handling an atomic 64 bit word to MMIO. This special handling takes
1716  * care of 32 bit environment where its not quarenteed to send the entire word
1717  * in one transfer.
1718  */
1719 #ifndef writeq
1720 static inline void _base_writeq(__u64 b, volatile void __iomem *addr,
1721     spinlock_t *writeq_lock)
1722 {
1723         unsigned long flags;
1724         __u64 data_out = cpu_to_le64(b);
1725
1726         spin_lock_irqsave(writeq_lock, flags);
1727         writel((u32)(data_out), addr);
1728         writel((u32)(data_out >> 32), (addr + 4));
1729         spin_unlock_irqrestore(writeq_lock, flags);
1730 }
1731 #else
1732 static inline void _base_writeq(__u64 b, volatile void __iomem *addr,
1733     spinlock_t *writeq_lock)
1734 {
1735         writeq(cpu_to_le64(b), addr);
1736 }
1737 #endif
1738
1739 static inline u8
1740 _base_get_msix_index(struct MPT2SAS_ADAPTER *ioc)
1741 {
1742         return ioc->cpu_msix_table[smp_processor_id()];
1743 }
1744
1745 /**
1746  * mpt2sas_base_put_smid_scsi_io - send SCSI_IO request to firmware
1747  * @ioc: per adapter object
1748  * @smid: system request message index
1749  * @handle: device handle
1750  *
1751  * Return nothing.
1752  */
1753 void
1754 mpt2sas_base_put_smid_scsi_io(struct MPT2SAS_ADAPTER *ioc, u16 smid, u16 handle)
1755 {
1756         Mpi2RequestDescriptorUnion_t descriptor;
1757         u64 *request = (u64 *)&descriptor;
1758
1759
1760         descriptor.SCSIIO.RequestFlags = MPI2_REQ_DESCRIPT_FLAGS_SCSI_IO;
1761         descriptor.SCSIIO.MSIxIndex =  _base_get_msix_index(ioc);
1762         descriptor.SCSIIO.SMID = cpu_to_le16(smid);
1763         descriptor.SCSIIO.DevHandle = cpu_to_le16(handle);
1764         descriptor.SCSIIO.LMID = 0;
1765         _base_writeq(*request, &ioc->chip->RequestDescriptorPostLow,
1766             &ioc->scsi_lookup_lock);
1767 }
1768
1769
1770 /**
1771  * mpt2sas_base_put_smid_hi_priority - send Task Management request to firmware
1772  * @ioc: per adapter object
1773  * @smid: system request message index
1774  *
1775  * Return nothing.
1776  */
1777 void
1778 mpt2sas_base_put_smid_hi_priority(struct MPT2SAS_ADAPTER *ioc, u16 smid)
1779 {
1780         Mpi2RequestDescriptorUnion_t descriptor;
1781         u64 *request = (u64 *)&descriptor;
1782
1783         descriptor.HighPriority.RequestFlags =
1784             MPI2_REQ_DESCRIPT_FLAGS_HIGH_PRIORITY;
1785         descriptor.HighPriority.MSIxIndex =  0;
1786         descriptor.HighPriority.SMID = cpu_to_le16(smid);
1787         descriptor.HighPriority.LMID = 0;
1788         descriptor.HighPriority.Reserved1 = 0;
1789         _base_writeq(*request, &ioc->chip->RequestDescriptorPostLow,
1790             &ioc->scsi_lookup_lock);
1791 }
1792
1793 /**
1794  * mpt2sas_base_put_smid_default - Default, primarily used for config pages
1795  * @ioc: per adapter object
1796  * @smid: system request message index
1797  *
1798  * Return nothing.
1799  */
1800 void
1801 mpt2sas_base_put_smid_default(struct MPT2SAS_ADAPTER *ioc, u16 smid)
1802 {
1803         Mpi2RequestDescriptorUnion_t descriptor;
1804         u64 *request = (u64 *)&descriptor;
1805
1806         descriptor.Default.RequestFlags = MPI2_REQ_DESCRIPT_FLAGS_DEFAULT_TYPE;
1807         descriptor.Default.MSIxIndex =  _base_get_msix_index(ioc);
1808         descriptor.Default.SMID = cpu_to_le16(smid);
1809         descriptor.Default.LMID = 0;
1810         descriptor.Default.DescriptorTypeDependent = 0;
1811         _base_writeq(*request, &ioc->chip->RequestDescriptorPostLow,
1812             &ioc->scsi_lookup_lock);
1813 }
1814
1815 /**
1816  * mpt2sas_base_put_smid_target_assist - send Target Assist/Status to firmware
1817  * @ioc: per adapter object
1818  * @smid: system request message index
1819  * @io_index: value used to track the IO
1820  *
1821  * Return nothing.
1822  */
1823 void
1824 mpt2sas_base_put_smid_target_assist(struct MPT2SAS_ADAPTER *ioc, u16 smid,
1825     u16 io_index)
1826 {
1827         Mpi2RequestDescriptorUnion_t descriptor;
1828         u64 *request = (u64 *)&descriptor;
1829
1830         descriptor.SCSITarget.RequestFlags =
1831             MPI2_REQ_DESCRIPT_FLAGS_SCSI_TARGET;
1832         descriptor.SCSITarget.MSIxIndex =  _base_get_msix_index(ioc);
1833         descriptor.SCSITarget.SMID = cpu_to_le16(smid);
1834         descriptor.SCSITarget.LMID = 0;
1835         descriptor.SCSITarget.IoIndex = cpu_to_le16(io_index);
1836         _base_writeq(*request, &ioc->chip->RequestDescriptorPostLow,
1837             &ioc->scsi_lookup_lock);
1838 }
1839
1840 /**
1841  * _base_display_dell_branding - Disply branding string
1842  * @ioc: per adapter object
1843  *
1844  * Return nothing.
1845  */
1846 static void
1847 _base_display_dell_branding(struct MPT2SAS_ADAPTER *ioc)
1848 {
1849         char dell_branding[MPT2SAS_DELL_BRANDING_SIZE];
1850
1851         if (ioc->pdev->subsystem_vendor != PCI_VENDOR_ID_DELL)
1852                 return;
1853
1854         memset(dell_branding, 0, MPT2SAS_DELL_BRANDING_SIZE);
1855         switch (ioc->pdev->subsystem_device) {
1856         case MPT2SAS_DELL_6GBPS_SAS_HBA_SSDID:
1857                 strncpy(dell_branding, MPT2SAS_DELL_6GBPS_SAS_HBA_BRANDING,
1858                     MPT2SAS_DELL_BRANDING_SIZE - 1);
1859                 break;
1860         case MPT2SAS_DELL_PERC_H200_ADAPTER_SSDID:
1861                 strncpy(dell_branding, MPT2SAS_DELL_PERC_H200_ADAPTER_BRANDING,
1862                     MPT2SAS_DELL_BRANDING_SIZE - 1);
1863                 break;
1864         case MPT2SAS_DELL_PERC_H200_INTEGRATED_SSDID:
1865                 strncpy(dell_branding,
1866                     MPT2SAS_DELL_PERC_H200_INTEGRATED_BRANDING,
1867                     MPT2SAS_DELL_BRANDING_SIZE - 1);
1868                 break;
1869         case MPT2SAS_DELL_PERC_H200_MODULAR_SSDID:
1870                 strncpy(dell_branding,
1871                     MPT2SAS_DELL_PERC_H200_MODULAR_BRANDING,
1872                     MPT2SAS_DELL_BRANDING_SIZE - 1);
1873                 break;
1874         case MPT2SAS_DELL_PERC_H200_EMBEDDED_SSDID:
1875                 strncpy(dell_branding,
1876                     MPT2SAS_DELL_PERC_H200_EMBEDDED_BRANDING,
1877                     MPT2SAS_DELL_BRANDING_SIZE - 1);
1878                 break;
1879         case MPT2SAS_DELL_PERC_H200_SSDID:
1880                 strncpy(dell_branding, MPT2SAS_DELL_PERC_H200_BRANDING,
1881                     MPT2SAS_DELL_BRANDING_SIZE - 1);
1882                 break;
1883         case MPT2SAS_DELL_6GBPS_SAS_SSDID:
1884                 strncpy(dell_branding, MPT2SAS_DELL_6GBPS_SAS_BRANDING,
1885                     MPT2SAS_DELL_BRANDING_SIZE - 1);
1886                 break;
1887         default:
1888                 sprintf(dell_branding, "0x%4X", ioc->pdev->subsystem_device);
1889                 break;
1890         }
1891
1892         printk(MPT2SAS_INFO_FMT "%s: Vendor(0x%04X), Device(0x%04X),"
1893             " SSVID(0x%04X), SSDID(0x%04X)\n", ioc->name, dell_branding,
1894             ioc->pdev->vendor, ioc->pdev->device, ioc->pdev->subsystem_vendor,
1895             ioc->pdev->subsystem_device);
1896 }
1897
1898 /**
1899  * _base_display_intel_branding - Display branding string
1900  * @ioc: per adapter object
1901  *
1902  * Return nothing.
1903  */
1904 static void
1905 _base_display_intel_branding(struct MPT2SAS_ADAPTER *ioc)
1906 {
1907         if (ioc->pdev->subsystem_vendor != PCI_VENDOR_ID_INTEL)
1908                 return;
1909
1910         switch (ioc->pdev->device) {
1911         case MPI2_MFGPAGE_DEVID_SAS2008:
1912                 switch (ioc->pdev->subsystem_device) {
1913                 case MPT2SAS_INTEL_RMS2LL080_SSDID:
1914                         printk(MPT2SAS_INFO_FMT "%s\n", ioc->name,
1915                             MPT2SAS_INTEL_RMS2LL080_BRANDING);
1916                         break;
1917                 case MPT2SAS_INTEL_RMS2LL040_SSDID:
1918                         printk(MPT2SAS_INFO_FMT "%s\n", ioc->name,
1919                             MPT2SAS_INTEL_RMS2LL040_BRANDING);
1920                         break;
1921                 default:
1922                         break;
1923                 }
1924         case MPI2_MFGPAGE_DEVID_SAS2308_2:
1925                 switch (ioc->pdev->subsystem_device) {
1926                 case MPT2SAS_INTEL_RS25GB008_SSDID:
1927                         printk(MPT2SAS_INFO_FMT "%s\n", ioc->name,
1928                             MPT2SAS_INTEL_RS25GB008_BRANDING);
1929                         break;
1930                 default:
1931                         break;
1932                 }
1933         default:
1934                 break;
1935         }
1936 }
1937
1938 /**
1939  * _base_display_hp_branding - Display branding string
1940  * @ioc: per adapter object
1941  *
1942  * Return nothing.
1943  */
1944 static void
1945 _base_display_hp_branding(struct MPT2SAS_ADAPTER *ioc)
1946 {
1947         if (ioc->pdev->subsystem_vendor != MPT2SAS_HP_3PAR_SSVID)
1948                 return;
1949
1950         switch (ioc->pdev->device) {
1951         case MPI2_MFGPAGE_DEVID_SAS2004:
1952                 switch (ioc->pdev->subsystem_device) {
1953                 case MPT2SAS_HP_DAUGHTER_2_4_INTERNAL_SSDID:
1954                         printk(MPT2SAS_INFO_FMT "%s\n", ioc->name,
1955                             MPT2SAS_HP_DAUGHTER_2_4_INTERNAL_BRANDING);
1956                         break;
1957                 default:
1958                         break;
1959                 }
1960         case MPI2_MFGPAGE_DEVID_SAS2308_2:
1961                 switch (ioc->pdev->subsystem_device) {
1962                 case MPT2SAS_HP_2_4_INTERNAL_SSDID:
1963                         printk(MPT2SAS_INFO_FMT "%s\n", ioc->name,
1964                             MPT2SAS_HP_2_4_INTERNAL_BRANDING);
1965                         break;
1966                 case MPT2SAS_HP_2_4_EXTERNAL_SSDID:
1967                         printk(MPT2SAS_INFO_FMT "%s\n", ioc->name,
1968                             MPT2SAS_HP_2_4_EXTERNAL_BRANDING);
1969                         break;
1970                 case MPT2SAS_HP_1_4_INTERNAL_1_4_EXTERNAL_SSDID:
1971                         printk(MPT2SAS_INFO_FMT "%s\n", ioc->name,
1972                             MPT2SAS_HP_1_4_INTERNAL_1_4_EXTERNAL_BRANDING);
1973                         break;
1974                 case MPT2SAS_HP_EMBEDDED_2_4_INTERNAL_SSDID:
1975                         printk(MPT2SAS_INFO_FMT "%s\n", ioc->name,
1976                             MPT2SAS_HP_EMBEDDED_2_4_INTERNAL_BRANDING);
1977                         break;
1978                 default:
1979                         break;
1980                 }
1981         default:
1982                 break;
1983         }
1984 }
1985
1986 /**
1987  * _base_display_ioc_capabilities - Disply IOC's capabilities.
1988  * @ioc: per adapter object
1989  *
1990  * Return nothing.
1991  */
1992 static void
1993 _base_display_ioc_capabilities(struct MPT2SAS_ADAPTER *ioc)
1994 {
1995         int i = 0;
1996         char desc[16];
1997         u8 revision;
1998         u32 iounit_pg1_flags;
1999         u32 bios_version;
2000
2001         bios_version = le32_to_cpu(ioc->bios_pg3.BiosVersion);
2002         pci_read_config_byte(ioc->pdev, PCI_CLASS_REVISION, &revision);
2003         strncpy(desc, ioc->manu_pg0.ChipName, 16);
2004         printk(MPT2SAS_INFO_FMT "%s: FWVersion(%02d.%02d.%02d.%02d), "
2005            "ChipRevision(0x%02x), BiosVersion(%02d.%02d.%02d.%02d)\n",
2006             ioc->name, desc,
2007            (ioc->facts.FWVersion.Word & 0xFF000000) >> 24,
2008            (ioc->facts.FWVersion.Word & 0x00FF0000) >> 16,
2009            (ioc->facts.FWVersion.Word & 0x0000FF00) >> 8,
2010            ioc->facts.FWVersion.Word & 0x000000FF,
2011            revision,
2012            (bios_version & 0xFF000000) >> 24,
2013            (bios_version & 0x00FF0000) >> 16,
2014            (bios_version & 0x0000FF00) >> 8,
2015             bios_version & 0x000000FF);
2016
2017         _base_display_dell_branding(ioc);
2018         _base_display_intel_branding(ioc);
2019         _base_display_hp_branding(ioc);
2020
2021         printk(MPT2SAS_INFO_FMT "Protocol=(", ioc->name);
2022
2023         if (ioc->facts.ProtocolFlags & MPI2_IOCFACTS_PROTOCOL_SCSI_INITIATOR) {
2024                 printk("Initiator");
2025                 i++;
2026         }
2027
2028         if (ioc->facts.ProtocolFlags & MPI2_IOCFACTS_PROTOCOL_SCSI_TARGET) {
2029                 printk("%sTarget", i ? "," : "");
2030                 i++;
2031         }
2032
2033         i = 0;
2034         printk("), ");
2035         printk("Capabilities=(");
2036
2037         if (!ioc->hide_ir_msg) {
2038                 if (ioc->facts.IOCCapabilities &
2039                     MPI2_IOCFACTS_CAPABILITY_INTEGRATED_RAID) {
2040                         printk("Raid");
2041                         i++;
2042                 }
2043         }
2044
2045         if (ioc->facts.IOCCapabilities & MPI2_IOCFACTS_CAPABILITY_TLR) {
2046                 printk("%sTLR", i ? "," : "");
2047                 i++;
2048         }
2049
2050         if (ioc->facts.IOCCapabilities & MPI2_IOCFACTS_CAPABILITY_MULTICAST) {
2051                 printk("%sMulticast", i ? "," : "");
2052                 i++;
2053         }
2054
2055         if (ioc->facts.IOCCapabilities &
2056             MPI2_IOCFACTS_CAPABILITY_BIDIRECTIONAL_TARGET) {
2057                 printk("%sBIDI Target", i ? "," : "");
2058                 i++;
2059         }
2060
2061         if (ioc->facts.IOCCapabilities & MPI2_IOCFACTS_CAPABILITY_EEDP) {
2062                 printk("%sEEDP", i ? "," : "");
2063                 i++;
2064         }
2065
2066         if (ioc->facts.IOCCapabilities &
2067             MPI2_IOCFACTS_CAPABILITY_SNAPSHOT_BUFFER) {
2068                 printk("%sSnapshot Buffer", i ? "," : "");
2069                 i++;
2070         }
2071
2072         if (ioc->facts.IOCCapabilities &
2073             MPI2_IOCFACTS_CAPABILITY_DIAG_TRACE_BUFFER) {
2074                 printk("%sDiag Trace Buffer", i ? "," : "");
2075                 i++;
2076         }
2077
2078         if (ioc->facts.IOCCapabilities &
2079             MPI2_IOCFACTS_CAPABILITY_EXTENDED_BUFFER) {
2080                 printk(KERN_INFO "%sDiag Extended Buffer", i ? "," : "");
2081                 i++;
2082         }
2083
2084         if (ioc->facts.IOCCapabilities &
2085             MPI2_IOCFACTS_CAPABILITY_TASK_SET_FULL_HANDLING) {
2086                 printk("%sTask Set Full", i ? "," : "");
2087                 i++;
2088         }
2089
2090         iounit_pg1_flags = le32_to_cpu(ioc->iounit_pg1.Flags);
2091         if (!(iounit_pg1_flags & MPI2_IOUNITPAGE1_NATIVE_COMMAND_Q_DISABLE)) {
2092                 printk("%sNCQ", i ? "," : "");
2093                 i++;
2094         }
2095
2096         printk(")\n");
2097 }
2098
2099 /**
2100  * _base_update_missing_delay - change the missing delay timers
2101  * @ioc: per adapter object
2102  * @device_missing_delay: amount of time till device is reported missing
2103  * @io_missing_delay: interval IO is returned when there is a missing device
2104  *
2105  * Return nothing.
2106  *
2107  * Passed on the command line, this function will modify the device missing
2108  * delay, as well as the io missing delay. This should be called at driver
2109  * load time.
2110  */
2111 static void
2112 _base_update_missing_delay(struct MPT2SAS_ADAPTER *ioc,
2113         u16 device_missing_delay, u8 io_missing_delay)
2114 {
2115         u16 dmd, dmd_new, dmd_orignal;
2116         u8 io_missing_delay_original;
2117         u16 sz;
2118         Mpi2SasIOUnitPage1_t *sas_iounit_pg1 = NULL;
2119         Mpi2ConfigReply_t mpi_reply;
2120         u8 num_phys = 0;
2121         u16 ioc_status;
2122
2123         mpt2sas_config_get_number_hba_phys(ioc, &num_phys);
2124         if (!num_phys)
2125                 return;
2126
2127         sz = offsetof(Mpi2SasIOUnitPage1_t, PhyData) + (num_phys *
2128             sizeof(Mpi2SasIOUnit1PhyData_t));
2129         sas_iounit_pg1 = kzalloc(sz, GFP_KERNEL);
2130         if (!sas_iounit_pg1) {
2131                 printk(MPT2SAS_ERR_FMT "failure at %s:%d/%s()!\n",
2132                     ioc->name, __FILE__, __LINE__, __func__);
2133                 goto out;
2134         }
2135         if ((mpt2sas_config_get_sas_iounit_pg1(ioc, &mpi_reply,
2136             sas_iounit_pg1, sz))) {
2137                 printk(MPT2SAS_ERR_FMT "failure at %s:%d/%s()!\n",
2138                     ioc->name, __FILE__, __LINE__, __func__);
2139                 goto out;
2140         }
2141         ioc_status = le16_to_cpu(mpi_reply.IOCStatus) &
2142             MPI2_IOCSTATUS_MASK;
2143         if (ioc_status != MPI2_IOCSTATUS_SUCCESS) {
2144                 printk(MPT2SAS_ERR_FMT "failure at %s:%d/%s()!\n",
2145                     ioc->name, __FILE__, __LINE__, __func__);
2146                 goto out;
2147         }
2148
2149         /* device missing delay */
2150         dmd = sas_iounit_pg1->ReportDeviceMissingDelay;
2151         if (dmd & MPI2_SASIOUNIT1_REPORT_MISSING_UNIT_16)
2152                 dmd = (dmd & MPI2_SASIOUNIT1_REPORT_MISSING_TIMEOUT_MASK) * 16;
2153         else
2154                 dmd = dmd & MPI2_SASIOUNIT1_REPORT_MISSING_TIMEOUT_MASK;
2155         dmd_orignal = dmd;
2156         if (device_missing_delay > 0x7F) {
2157                 dmd = (device_missing_delay > 0x7F0) ? 0x7F0 :
2158                     device_missing_delay;
2159                 dmd = dmd / 16;
2160                 dmd |= MPI2_SASIOUNIT1_REPORT_MISSING_UNIT_16;
2161         } else
2162                 dmd = device_missing_delay;
2163         sas_iounit_pg1->ReportDeviceMissingDelay = dmd;
2164
2165         /* io missing delay */
2166         io_missing_delay_original = sas_iounit_pg1->IODeviceMissingDelay;
2167         sas_iounit_pg1->IODeviceMissingDelay = io_missing_delay;
2168
2169         if (!mpt2sas_config_set_sas_iounit_pg1(ioc, &mpi_reply, sas_iounit_pg1,
2170             sz)) {
2171                 if (dmd & MPI2_SASIOUNIT1_REPORT_MISSING_UNIT_16)
2172                         dmd_new = (dmd &
2173                             MPI2_SASIOUNIT1_REPORT_MISSING_TIMEOUT_MASK) * 16;
2174                 else
2175                         dmd_new =
2176                     dmd & MPI2_SASIOUNIT1_REPORT_MISSING_TIMEOUT_MASK;
2177                 printk(MPT2SAS_INFO_FMT "device_missing_delay: old(%d), "
2178                     "new(%d)\n", ioc->name, dmd_orignal, dmd_new);
2179                 printk(MPT2SAS_INFO_FMT "ioc_missing_delay: old(%d), "
2180                     "new(%d)\n", ioc->name, io_missing_delay_original,
2181                     io_missing_delay);
2182                 ioc->device_missing_delay = dmd_new;
2183                 ioc->io_missing_delay = io_missing_delay;
2184         }
2185
2186 out:
2187         kfree(sas_iounit_pg1);
2188 }
2189
2190 /**
2191  * _base_static_config_pages - static start of day config pages
2192  * @ioc: per adapter object
2193  *
2194  * Return nothing.
2195  */
2196 static void
2197 _base_static_config_pages(struct MPT2SAS_ADAPTER *ioc)
2198 {
2199         Mpi2ConfigReply_t mpi_reply;
2200         u32 iounit_pg1_flags;
2201
2202         mpt2sas_config_get_manufacturing_pg0(ioc, &mpi_reply, &ioc->manu_pg0);
2203         if (ioc->ir_firmware)
2204                 mpt2sas_config_get_manufacturing_pg10(ioc, &mpi_reply,
2205                     &ioc->manu_pg10);
2206         mpt2sas_config_get_bios_pg2(ioc, &mpi_reply, &ioc->bios_pg2);
2207         mpt2sas_config_get_bios_pg3(ioc, &mpi_reply, &ioc->bios_pg3);
2208         mpt2sas_config_get_ioc_pg8(ioc, &mpi_reply, &ioc->ioc_pg8);
2209         mpt2sas_config_get_iounit_pg0(ioc, &mpi_reply, &ioc->iounit_pg0);
2210         mpt2sas_config_get_iounit_pg1(ioc, &mpi_reply, &ioc->iounit_pg1);
2211         _base_display_ioc_capabilities(ioc);
2212
2213         /*
2214          * Enable task_set_full handling in iounit_pg1 when the
2215          * facts capabilities indicate that its supported.
2216          */
2217         iounit_pg1_flags = le32_to_cpu(ioc->iounit_pg1.Flags);
2218         if ((ioc->facts.IOCCapabilities &
2219             MPI2_IOCFACTS_CAPABILITY_TASK_SET_FULL_HANDLING))
2220                 iounit_pg1_flags &=
2221                     ~MPI2_IOUNITPAGE1_DISABLE_TASK_SET_FULL_HANDLING;
2222         else
2223                 iounit_pg1_flags |=
2224                     MPI2_IOUNITPAGE1_DISABLE_TASK_SET_FULL_HANDLING;
2225         ioc->iounit_pg1.Flags = cpu_to_le32(iounit_pg1_flags);
2226         mpt2sas_config_set_iounit_pg1(ioc, &mpi_reply, &ioc->iounit_pg1);
2227
2228 }
2229
2230 /**
2231  * _base_release_memory_pools - release memory
2232  * @ioc: per adapter object
2233  *
2234  * Free memory allocated from _base_allocate_memory_pools.
2235  *
2236  * Return nothing.
2237  */
2238 static void
2239 _base_release_memory_pools(struct MPT2SAS_ADAPTER *ioc)
2240 {
2241         int i;
2242
2243         dexitprintk(ioc, printk(MPT2SAS_INFO_FMT "%s\n", ioc->name,
2244             __func__));
2245
2246         if (ioc->request) {
2247                 pci_free_consistent(ioc->pdev, ioc->request_dma_sz,
2248                     ioc->request,  ioc->request_dma);
2249                 dexitprintk(ioc, printk(MPT2SAS_INFO_FMT "request_pool(0x%p)"
2250                     ": free\n", ioc->name, ioc->request));
2251                 ioc->request = NULL;
2252         }
2253
2254         if (ioc->sense) {
2255                 pci_pool_free(ioc->sense_dma_pool, ioc->sense, ioc->sense_dma);
2256                 if (ioc->sense_dma_pool)
2257                         pci_pool_destroy(ioc->sense_dma_pool);
2258                 dexitprintk(ioc, printk(MPT2SAS_INFO_FMT "sense_pool(0x%p)"
2259                     ": free\n", ioc->name, ioc->sense));
2260                 ioc->sense = NULL;
2261         }
2262
2263         if (ioc->reply) {
2264                 pci_pool_free(ioc->reply_dma_pool, ioc->reply, ioc->reply_dma);
2265                 if (ioc->reply_dma_pool)
2266                         pci_pool_destroy(ioc->reply_dma_pool);
2267                 dexitprintk(ioc, printk(MPT2SAS_INFO_FMT "reply_pool(0x%p)"
2268                      ": free\n", ioc->name, ioc->reply));
2269                 ioc->reply = NULL;
2270         }
2271
2272         if (ioc->reply_free) {
2273                 pci_pool_free(ioc->reply_free_dma_pool, ioc->reply_free,
2274                     ioc->reply_free_dma);
2275                 if (ioc->reply_free_dma_pool)
2276                         pci_pool_destroy(ioc->reply_free_dma_pool);
2277                 dexitprintk(ioc, printk(MPT2SAS_INFO_FMT "reply_free_pool"
2278                     "(0x%p): free\n", ioc->name, ioc->reply_free));
2279                 ioc->reply_free = NULL;
2280         }
2281
2282         if (ioc->reply_post_free) {
2283                 pci_pool_free(ioc->reply_post_free_dma_pool,
2284                     ioc->reply_post_free, ioc->reply_post_free_dma);
2285                 if (ioc->reply_post_free_dma_pool)
2286                         pci_pool_destroy(ioc->reply_post_free_dma_pool);
2287                 dexitprintk(ioc, printk(MPT2SAS_INFO_FMT
2288                     "reply_post_free_pool(0x%p): free\n", ioc->name,
2289                     ioc->reply_post_free));
2290                 ioc->reply_post_free = NULL;
2291         }
2292
2293         if (ioc->config_page) {
2294                 dexitprintk(ioc, printk(MPT2SAS_INFO_FMT
2295                     "config_page(0x%p): free\n", ioc->name,
2296                     ioc->config_page));
2297                 pci_free_consistent(ioc->pdev, ioc->config_page_sz,
2298                     ioc->config_page, ioc->config_page_dma);
2299         }
2300
2301         if (ioc->scsi_lookup) {
2302                 free_pages((ulong)ioc->scsi_lookup, ioc->scsi_lookup_pages);
2303                 ioc->scsi_lookup = NULL;
2304         }
2305         kfree(ioc->hpr_lookup);
2306         kfree(ioc->internal_lookup);
2307         if (ioc->chain_lookup) {
2308                 for (i = 0; i < ioc->chain_depth; i++) {
2309                         if (ioc->chain_lookup[i].chain_buffer)
2310                                 pci_pool_free(ioc->chain_dma_pool,
2311                                     ioc->chain_lookup[i].chain_buffer,
2312                                     ioc->chain_lookup[i].chain_buffer_dma);
2313                 }
2314                 if (ioc->chain_dma_pool)
2315                         pci_pool_destroy(ioc->chain_dma_pool);
2316                 free_pages((ulong)ioc->chain_lookup, ioc->chain_pages);
2317                 ioc->chain_lookup = NULL;
2318         }
2319 }
2320
2321
2322 /**
2323  * _base_allocate_memory_pools - allocate start of day memory pools
2324  * @ioc: per adapter object
2325  * @sleep_flag: CAN_SLEEP or NO_SLEEP
2326  *
2327  * Returns 0 success, anything else error
2328  */
2329 static int
2330 _base_allocate_memory_pools(struct MPT2SAS_ADAPTER *ioc,  int sleep_flag)
2331 {
2332         struct mpt2sas_facts *facts;
2333         u16 max_sge_elements;
2334         u16 chains_needed_per_io;
2335         u32 sz, total_sz, reply_post_free_sz;
2336         u32 retry_sz;
2337         u16 max_request_credit;
2338         int i;
2339
2340         dinitprintk(ioc, printk(MPT2SAS_INFO_FMT "%s\n", ioc->name,
2341             __func__));
2342
2343         retry_sz = 0;
2344         facts = &ioc->facts;
2345
2346         /* command line tunables  for max sgl entries */
2347         if (max_sgl_entries != -1) {
2348                 ioc->shost->sg_tablesize = (max_sgl_entries <
2349                     MPT2SAS_SG_DEPTH) ? max_sgl_entries :
2350                     MPT2SAS_SG_DEPTH;
2351         } else {
2352                 ioc->shost->sg_tablesize = MPT2SAS_SG_DEPTH;
2353         }
2354
2355         /* command line tunables  for max controller queue depth */
2356         if (max_queue_depth != -1)
2357                 max_request_credit = (max_queue_depth < facts->RequestCredit)
2358                     ? max_queue_depth : facts->RequestCredit;
2359         else
2360                 max_request_credit = min_t(u16, facts->RequestCredit,
2361                     MAX_HBA_QUEUE_DEPTH);
2362
2363         ioc->hba_queue_depth = max_request_credit;
2364         ioc->hi_priority_depth = facts->HighPriorityCredit;
2365         ioc->internal_depth = ioc->hi_priority_depth + 5;
2366
2367         /* request frame size */
2368         ioc->request_sz = facts->IOCRequestFrameSize * 4;
2369
2370         /* reply frame size */
2371         ioc->reply_sz = facts->ReplyFrameSize * 4;
2372
2373  retry_allocation:
2374         total_sz = 0;
2375         /* calculate number of sg elements left over in the 1st frame */
2376         max_sge_elements = ioc->request_sz - ((sizeof(Mpi2SCSIIORequest_t) -
2377             sizeof(Mpi2SGEIOUnion_t)) + ioc->sge_size);
2378         ioc->max_sges_in_main_message = max_sge_elements/ioc->sge_size;
2379
2380         /* now do the same for a chain buffer */
2381         max_sge_elements = ioc->request_sz - ioc->sge_size;
2382         ioc->max_sges_in_chain_message = max_sge_elements/ioc->sge_size;
2383
2384         ioc->chain_offset_value_for_main_message =
2385             ((sizeof(Mpi2SCSIIORequest_t) - sizeof(Mpi2SGEIOUnion_t)) +
2386              (ioc->max_sges_in_chain_message * ioc->sge_size)) / 4;
2387
2388         /*
2389          *  MPT2SAS_SG_DEPTH = CONFIG_FUSION_MAX_SGE
2390          */
2391         chains_needed_per_io = ((ioc->shost->sg_tablesize -
2392            ioc->max_sges_in_main_message)/ioc->max_sges_in_chain_message)
2393             + 1;
2394         if (chains_needed_per_io > facts->MaxChainDepth) {
2395                 chains_needed_per_io = facts->MaxChainDepth;
2396                 ioc->shost->sg_tablesize = min_t(u16,
2397                 ioc->max_sges_in_main_message + (ioc->max_sges_in_chain_message
2398                 * chains_needed_per_io), ioc->shost->sg_tablesize);
2399         }
2400         ioc->chains_needed_per_io = chains_needed_per_io;
2401
2402         /* reply free queue sizing - taking into account for 64 FW events */
2403         ioc->reply_free_queue_depth = ioc->hba_queue_depth + 64;
2404
2405         /* align the reply post queue on the next 16 count boundary */
2406         if (!ioc->reply_free_queue_depth % 16)
2407                 ioc->reply_post_queue_depth = ioc->reply_free_queue_depth + 16;
2408         else
2409                 ioc->reply_post_queue_depth = ioc->reply_free_queue_depth +
2410                                 32 - (ioc->reply_free_queue_depth % 16);
2411         if (ioc->reply_post_queue_depth >
2412             facts->MaxReplyDescriptorPostQueueDepth) {
2413                 ioc->reply_post_queue_depth = min_t(u16,
2414                     (facts->MaxReplyDescriptorPostQueueDepth -
2415                     (facts->MaxReplyDescriptorPostQueueDepth % 16)),
2416                     (ioc->hba_queue_depth - (ioc->hba_queue_depth % 16)));
2417                 ioc->reply_free_queue_depth = ioc->reply_post_queue_depth - 16;
2418                 ioc->hba_queue_depth = ioc->reply_free_queue_depth - 64;
2419         }
2420
2421
2422         dinitprintk(ioc, printk(MPT2SAS_INFO_FMT "scatter gather: "
2423             "sge_in_main_msg(%d), sge_per_chain(%d), sge_per_io(%d), "
2424             "chains_per_io(%d)\n", ioc->name, ioc->max_sges_in_main_message,
2425             ioc->max_sges_in_chain_message, ioc->shost->sg_tablesize,
2426             ioc->chains_needed_per_io));
2427
2428         ioc->scsiio_depth = ioc->hba_queue_depth -
2429             ioc->hi_priority_depth - ioc->internal_depth;
2430
2431         /* set the scsi host can_queue depth
2432          * with some internal commands that could be outstanding
2433          */
2434         ioc->shost->can_queue = ioc->scsiio_depth - (2);
2435         dinitprintk(ioc, printk(MPT2SAS_INFO_FMT "scsi host: "
2436             "can_queue depth (%d)\n", ioc->name, ioc->shost->can_queue));
2437
2438         /* contiguous pool for request and chains, 16 byte align, one extra "
2439          * "frame for smid=0
2440          */
2441         ioc->chain_depth = ioc->chains_needed_per_io * ioc->scsiio_depth;
2442         sz = ((ioc->scsiio_depth + 1) * ioc->request_sz);
2443
2444         /* hi-priority queue */
2445         sz += (ioc->hi_priority_depth * ioc->request_sz);
2446
2447         /* internal queue */
2448         sz += (ioc->internal_depth * ioc->request_sz);
2449
2450         ioc->request_dma_sz = sz;
2451         ioc->request = pci_alloc_consistent(ioc->pdev, sz, &ioc->request_dma);
2452         if (!ioc->request) {
2453                 printk(MPT2SAS_ERR_FMT "request pool: pci_alloc_consistent "
2454                     "failed: hba_depth(%d), chains_per_io(%d), frame_sz(%d), "
2455                     "total(%d kB)\n", ioc->name, ioc->hba_queue_depth,
2456                     ioc->chains_needed_per_io, ioc->request_sz, sz/1024);
2457                 if (ioc->scsiio_depth < MPT2SAS_SAS_QUEUE_DEPTH)
2458                         goto out;
2459                 retry_sz += 64;
2460                 ioc->hba_queue_depth = max_request_credit - retry_sz;
2461                 goto retry_allocation;
2462         }
2463
2464         if (retry_sz)
2465                 printk(MPT2SAS_ERR_FMT "request pool: pci_alloc_consistent "
2466                     "succeed: hba_depth(%d), chains_per_io(%d), frame_sz(%d), "
2467                     "total(%d kb)\n", ioc->name, ioc->hba_queue_depth,
2468                     ioc->chains_needed_per_io, ioc->request_sz, sz/1024);
2469
2470
2471         /* hi-priority queue */
2472         ioc->hi_priority = ioc->request + ((ioc->scsiio_depth + 1) *
2473             ioc->request_sz);
2474         ioc->hi_priority_dma = ioc->request_dma + ((ioc->scsiio_depth + 1) *
2475             ioc->request_sz);
2476
2477         /* internal queue */
2478         ioc->internal = ioc->hi_priority + (ioc->hi_priority_depth *
2479             ioc->request_sz);
2480         ioc->internal_dma = ioc->hi_priority_dma + (ioc->hi_priority_depth *
2481             ioc->request_sz);
2482
2483
2484         dinitprintk(ioc, printk(MPT2SAS_INFO_FMT "request pool(0x%p): "
2485             "depth(%d), frame_size(%d), pool_size(%d kB)\n", ioc->name,
2486             ioc->request, ioc->hba_queue_depth, ioc->request_sz,
2487             (ioc->hba_queue_depth * ioc->request_sz)/1024));
2488         dinitprintk(ioc, printk(MPT2SAS_INFO_FMT "request pool: dma(0x%llx)\n",
2489             ioc->name, (unsigned long long) ioc->request_dma));
2490         total_sz += sz;
2491
2492         sz = ioc->scsiio_depth * sizeof(struct scsiio_tracker);
2493         ioc->scsi_lookup_pages = get_order(sz);
2494         ioc->scsi_lookup = (struct scsiio_tracker *)__get_free_pages(
2495             GFP_KERNEL, ioc->scsi_lookup_pages);
2496         if (!ioc->scsi_lookup) {
2497                 printk(MPT2SAS_ERR_FMT "scsi_lookup: get_free_pages failed, "
2498                     "sz(%d)\n", ioc->name, (int)sz);
2499                 goto out;
2500         }
2501
2502         dinitprintk(ioc, printk(MPT2SAS_INFO_FMT "scsiio(0x%p): "
2503             "depth(%d)\n", ioc->name, ioc->request,
2504             ioc->scsiio_depth));
2505
2506         ioc->chain_depth = min_t(u32, ioc->chain_depth, MAX_CHAIN_DEPTH);
2507         sz = ioc->chain_depth * sizeof(struct chain_tracker);
2508         ioc->chain_pages = get_order(sz);
2509
2510         ioc->chain_lookup = (struct chain_tracker *)__get_free_pages(
2511             GFP_KERNEL, ioc->chain_pages);
2512         ioc->chain_dma_pool = pci_pool_create("chain pool", ioc->pdev,
2513             ioc->request_sz, 16, 0);
2514         if (!ioc->chain_dma_pool) {
2515                 printk(MPT2SAS_ERR_FMT "chain_dma_pool: pci_pool_create "
2516                     "failed\n", ioc->name);
2517                 goto out;
2518         }
2519         for (i = 0; i < ioc->chain_depth; i++) {
2520                 ioc->chain_lookup[i].chain_buffer = pci_pool_alloc(
2521                     ioc->chain_dma_pool , GFP_KERNEL,
2522                     &ioc->chain_lookup[i].chain_buffer_dma);
2523                 if (!ioc->chain_lookup[i].chain_buffer) {
2524                         ioc->chain_depth = i;
2525                         goto chain_done;
2526                 }
2527                 total_sz += ioc->request_sz;
2528         }
2529 chain_done:
2530         dinitprintk(ioc, printk(MPT2SAS_INFO_FMT "chain pool depth"
2531             "(%d), frame_size(%d), pool_size(%d kB)\n", ioc->name,
2532             ioc->chain_depth, ioc->request_sz, ((ioc->chain_depth *
2533             ioc->request_sz))/1024));
2534
2535         /* initialize hi-priority queue smid's */
2536         ioc->hpr_lookup = kcalloc(ioc->hi_priority_depth,
2537             sizeof(struct request_tracker), GFP_KERNEL);
2538         if (!ioc->hpr_lookup) {
2539                 printk(MPT2SAS_ERR_FMT "hpr_lookup: kcalloc failed\n",
2540                     ioc->name);
2541                 goto out;
2542         }
2543         ioc->hi_priority_smid = ioc->scsiio_depth + 1;
2544         dinitprintk(ioc, printk(MPT2SAS_INFO_FMT "hi_priority(0x%p): "
2545             "depth(%d), start smid(%d)\n", ioc->name, ioc->hi_priority,
2546             ioc->hi_priority_depth, ioc->hi_priority_smid));
2547
2548         /* initialize internal queue smid's */
2549         ioc->internal_lookup = kcalloc(ioc->internal_depth,
2550             sizeof(struct request_tracker), GFP_KERNEL);
2551         if (!ioc->internal_lookup) {
2552                 printk(MPT2SAS_ERR_FMT "internal_lookup: kcalloc failed\n",
2553                     ioc->name);
2554                 goto out;
2555         }
2556         ioc->internal_smid = ioc->hi_priority_smid + ioc->hi_priority_depth;
2557         dinitprintk(ioc, printk(MPT2SAS_INFO_FMT "internal(0x%p): "
2558             "depth(%d), start smid(%d)\n", ioc->name, ioc->internal,
2559              ioc->internal_depth, ioc->internal_smid));
2560
2561         /* sense buffers, 4 byte align */
2562         sz = ioc->scsiio_depth * SCSI_SENSE_BUFFERSIZE;
2563         ioc->sense_dma_pool = pci_pool_create("sense pool", ioc->pdev, sz, 4,
2564             0);
2565         if (!ioc->sense_dma_pool) {
2566                 printk(MPT2SAS_ERR_FMT "sense pool: pci_pool_create failed\n",
2567                     ioc->name);
2568                 goto out;
2569         }
2570         ioc->sense = pci_pool_alloc(ioc->sense_dma_pool , GFP_KERNEL,
2571             &ioc->sense_dma);
2572         if (!ioc->sense) {
2573                 printk(MPT2SAS_ERR_FMT "sense pool: pci_pool_alloc failed\n",
2574                     ioc->name);
2575                 goto out;
2576         }
2577         dinitprintk(ioc, printk(MPT2SAS_INFO_FMT
2578             "sense pool(0x%p): depth(%d), element_size(%d), pool_size"
2579             "(%d kB)\n", ioc->name, ioc->sense, ioc->scsiio_depth,
2580             SCSI_SENSE_BUFFERSIZE, sz/1024));
2581         dinitprintk(ioc, printk(MPT2SAS_INFO_FMT "sense_dma(0x%llx)\n",
2582             ioc->name, (unsigned long long)ioc->sense_dma));
2583         total_sz += sz;
2584
2585         /* reply pool, 4 byte align */
2586         sz = ioc->reply_free_queue_depth * ioc->reply_sz;
2587         ioc->reply_dma_pool = pci_pool_create("reply pool", ioc->pdev, sz, 4,
2588             0);
2589         if (!ioc->reply_dma_pool) {
2590                 printk(MPT2SAS_ERR_FMT "reply pool: pci_pool_create failed\n",
2591                     ioc->name);
2592                 goto out;
2593         }
2594         ioc->reply = pci_pool_alloc(ioc->reply_dma_pool , GFP_KERNEL,
2595             &ioc->reply_dma);
2596         if (!ioc->reply) {
2597                 printk(MPT2SAS_ERR_FMT "reply pool: pci_pool_alloc failed\n",
2598                     ioc->name);
2599                 goto out;
2600         }
2601         ioc->reply_dma_min_address = (u32)(ioc->reply_dma);
2602         ioc->reply_dma_max_address = (u32)(ioc->reply_dma) + sz;
2603         dinitprintk(ioc, printk(MPT2SAS_INFO_FMT "reply pool(0x%p): depth"
2604             "(%d), frame_size(%d), pool_size(%d kB)\n", ioc->name, ioc->reply,
2605             ioc->reply_free_queue_depth, ioc->reply_sz, sz/1024));
2606         dinitprintk(ioc, printk(MPT2SAS_INFO_FMT "reply_dma(0x%llx)\n",
2607             ioc->name, (unsigned long long)ioc->reply_dma));
2608         total_sz += sz;
2609
2610         /* reply free queue, 16 byte align */
2611         sz = ioc->reply_free_queue_depth * 4;
2612         ioc->reply_free_dma_pool = pci_pool_create("reply_free pool",
2613             ioc->pdev, sz, 16, 0);
2614         if (!ioc->reply_free_dma_pool) {
2615                 printk(MPT2SAS_ERR_FMT "reply_free pool: pci_pool_create "
2616                     "failed\n", ioc->name);
2617                 goto out;
2618         }
2619         ioc->reply_free = pci_pool_alloc(ioc->reply_free_dma_pool , GFP_KERNEL,
2620             &ioc->reply_free_dma);
2621         if (!ioc->reply_free) {
2622                 printk(MPT2SAS_ERR_FMT "reply_free pool: pci_pool_alloc "
2623                     "failed\n", ioc->name);
2624                 goto out;
2625         }
2626         memset(ioc->reply_free, 0, sz);
2627         dinitprintk(ioc, printk(MPT2SAS_INFO_FMT "reply_free pool(0x%p): "
2628             "depth(%d), element_size(%d), pool_size(%d kB)\n", ioc->name,
2629             ioc->reply_free, ioc->reply_free_queue_depth, 4, sz/1024));
2630         dinitprintk(ioc, printk(MPT2SAS_INFO_FMT "reply_free_dma"
2631             "(0x%llx)\n", ioc->name, (unsigned long long)ioc->reply_free_dma));
2632         total_sz += sz;
2633
2634         /* reply post queue, 16 byte align */
2635         reply_post_free_sz = ioc->reply_post_queue_depth *
2636             sizeof(Mpi2DefaultReplyDescriptor_t);
2637         if (_base_is_controller_msix_enabled(ioc))
2638                 sz = reply_post_free_sz * ioc->reply_queue_count;
2639         else
2640                 sz = reply_post_free_sz;
2641         ioc->reply_post_free_dma_pool = pci_pool_create("reply_post_free pool",
2642             ioc->pdev, sz, 16, 0);
2643         if (!ioc->reply_post_free_dma_pool) {
2644                 printk(MPT2SAS_ERR_FMT "reply_post_free pool: pci_pool_create "
2645                     "failed\n", ioc->name);
2646                 goto out;
2647         }
2648         ioc->reply_post_free = pci_pool_alloc(ioc->reply_post_free_dma_pool ,
2649             GFP_KERNEL, &ioc->reply_post_free_dma);
2650         if (!ioc->reply_post_free) {
2651                 printk(MPT2SAS_ERR_FMT "reply_post_free pool: pci_pool_alloc "
2652                     "failed\n", ioc->name);
2653                 goto out;
2654         }
2655         memset(ioc->reply_post_free, 0, sz);
2656         dinitprintk(ioc, printk(MPT2SAS_INFO_FMT "reply post free pool"
2657             "(0x%p): depth(%d), element_size(%d), pool_size(%d kB)\n",
2658             ioc->name, ioc->reply_post_free, ioc->reply_post_queue_depth, 8,
2659             sz/1024));
2660         dinitprintk(ioc, printk(MPT2SAS_INFO_FMT "reply_post_free_dma = "
2661             "(0x%llx)\n", ioc->name, (unsigned long long)
2662             ioc->reply_post_free_dma));
2663         total_sz += sz;
2664
2665         ioc->config_page_sz = 512;
2666         ioc->config_page = pci_alloc_consistent(ioc->pdev,
2667             ioc->config_page_sz, &ioc->config_page_dma);
2668         if (!ioc->config_page) {
2669                 printk(MPT2SAS_ERR_FMT "config page: pci_pool_alloc "
2670                     "failed\n", ioc->name);
2671                 goto out;
2672         }
2673         dinitprintk(ioc, printk(MPT2SAS_INFO_FMT "config page(0x%p): size"
2674             "(%d)\n", ioc->name, ioc->config_page, ioc->config_page_sz));
2675         dinitprintk(ioc, printk(MPT2SAS_INFO_FMT "config_page_dma"
2676             "(0x%llx)\n", ioc->name, (unsigned long long)ioc->config_page_dma));
2677         total_sz += ioc->config_page_sz;
2678
2679         printk(MPT2SAS_INFO_FMT "Allocated physical memory: size(%d kB)\n",
2680             ioc->name, total_sz/1024);
2681         printk(MPT2SAS_INFO_FMT "Current Controller Queue Depth(%d), "
2682             "Max Controller Queue Depth(%d)\n",
2683             ioc->name, ioc->shost->can_queue, facts->RequestCredit);
2684         printk(MPT2SAS_INFO_FMT "Scatter Gather Elements per IO(%d)\n",
2685             ioc->name, ioc->shost->sg_tablesize);
2686         return 0;
2687
2688  out:
2689         return -ENOMEM;
2690 }
2691
2692
2693 /**
2694  * mpt2sas_base_get_iocstate - Get the current state of a MPT adapter.
2695  * @ioc: Pointer to MPT_ADAPTER structure
2696  * @cooked: Request raw or cooked IOC state
2697  *
2698  * Returns all IOC Doorbell register bits if cooked==0, else just the
2699  * Doorbell bits in MPI_IOC_STATE_MASK.
2700  */
2701 u32
2702 mpt2sas_base_get_iocstate(struct MPT2SAS_ADAPTER *ioc, int cooked)
2703 {
2704         u32 s, sc;
2705
2706         s = readl(&ioc->chip->Doorbell);
2707         sc = s & MPI2_IOC_STATE_MASK;
2708         return cooked ? sc : s;
2709 }
2710
2711 /**
2712  * _base_wait_on_iocstate - waiting on a particular ioc state
2713  * @ioc_state: controller state { READY, OPERATIONAL, or RESET }
2714  * @timeout: timeout in second
2715  * @sleep_flag: CAN_SLEEP or NO_SLEEP
2716  *
2717  * Returns 0 for success, non-zero for failure.
2718  */
2719 static int
2720 _base_wait_on_iocstate(struct MPT2SAS_ADAPTER *ioc, u32 ioc_state, int timeout,
2721     int sleep_flag)
2722 {
2723         u32 count, cntdn;
2724         u32 current_state;
2725
2726         count = 0;
2727         cntdn = (sleep_flag == CAN_SLEEP) ? 1000*timeout : 2000*timeout;
2728         do {
2729                 current_state = mpt2sas_base_get_iocstate(ioc, 1);
2730                 if (current_state == ioc_state)
2731                         return 0;
2732                 if (count && current_state == MPI2_IOC_STATE_FAULT)
2733                         break;
2734                 if (sleep_flag == CAN_SLEEP)
2735                         msleep(1);
2736                 else
2737                         udelay(500);
2738                 count++;
2739         } while (--cntdn);
2740
2741         return current_state;
2742 }
2743
2744 /**
2745  * _base_wait_for_doorbell_int - waiting for controller interrupt(generated by
2746  * a write to the doorbell)
2747  * @ioc: per adapter object
2748  * @timeout: timeout in second
2749  * @sleep_flag: CAN_SLEEP or NO_SLEEP
2750  *
2751  * Returns 0 for success, non-zero for failure.
2752  *
2753  * Notes: MPI2_HIS_IOC2SYS_DB_STATUS - set to one when IOC writes to doorbell.
2754  */
2755 static int
2756 _base_wait_for_doorbell_int(struct MPT2SAS_ADAPTER *ioc, int timeout,
2757     int sleep_flag)
2758 {
2759         u32 cntdn, count;
2760         u32 int_status;
2761
2762         count = 0;
2763         cntdn = (sleep_flag == CAN_SLEEP) ? 1000*timeout : 2000*timeout;
2764         do {
2765                 int_status = readl(&ioc->chip->HostInterruptStatus);
2766                 if (int_status & MPI2_HIS_IOC2SYS_DB_STATUS) {
2767                         dhsprintk(ioc, printk(MPT2SAS_INFO_FMT "%s: "
2768                             "successful count(%d), timeout(%d)\n", ioc->name,
2769                             __func__, count, timeout));
2770                         return 0;
2771                 }
2772                 if (sleep_flag == CAN_SLEEP)
2773                         msleep(1);
2774                 else
2775                         udelay(500);
2776                 count++;
2777         } while (--cntdn);
2778
2779         printk(MPT2SAS_ERR_FMT "%s: failed due to timeout count(%d), "
2780             "int_status(%x)!\n", ioc->name, __func__, count, int_status);
2781         return -EFAULT;
2782 }
2783
2784 /**
2785  * _base_wait_for_doorbell_ack - waiting for controller to read the doorbell.
2786  * @ioc: per adapter object
2787  * @timeout: timeout in second
2788  * @sleep_flag: CAN_SLEEP or NO_SLEEP
2789  *
2790  * Returns 0 for success, non-zero for failure.
2791  *
2792  * Notes: MPI2_HIS_SYS2IOC_DB_STATUS - set to one when host writes to
2793  * doorbell.
2794  */
2795 static int
2796 _base_wait_for_doorbell_ack(struct MPT2SAS_ADAPTER *ioc, int timeout,
2797     int sleep_flag)
2798 {
2799         u32 cntdn, count;
2800         u32 int_status;
2801         u32 doorbell;
2802
2803         count = 0;
2804         cntdn = (sleep_flag == CAN_SLEEP) ? 1000*timeout : 2000*timeout;
2805         do {
2806                 int_status = readl(&ioc->chip->HostInterruptStatus);
2807                 if (!(int_status & MPI2_HIS_SYS2IOC_DB_STATUS)) {
2808                         dhsprintk(ioc, printk(MPT2SAS_INFO_FMT "%s: "
2809                             "successful count(%d), timeout(%d)\n", ioc->name,
2810                             __func__, count, timeout));
2811                         return 0;
2812                 } else if (int_status & MPI2_HIS_IOC2SYS_DB_STATUS) {
2813                         doorbell = readl(&ioc->chip->Doorbell);
2814                         if ((doorbell & MPI2_IOC_STATE_MASK) ==
2815                             MPI2_IOC_STATE_FAULT) {
2816                                 mpt2sas_base_fault_info(ioc , doorbell);
2817                                 return -EFAULT;
2818                         }
2819                 } else if (int_status == 0xFFFFFFFF)
2820                         goto out;
2821
2822                 if (sleep_flag == CAN_SLEEP)
2823                         msleep(1);
2824                 else
2825                         udelay(500);
2826                 count++;
2827         } while (--cntdn);
2828
2829  out:
2830         printk(MPT2SAS_ERR_FMT "%s: failed due to timeout count(%d), "
2831             "int_status(%x)!\n", ioc->name, __func__, count, int_status);
2832         return -EFAULT;
2833 }
2834
2835 /**
2836  * _base_wait_for_doorbell_not_used - waiting for doorbell to not be in use
2837  * @ioc: per adapter object
2838  * @timeout: timeout in second
2839  * @sleep_flag: CAN_SLEEP or NO_SLEEP
2840  *
2841  * Returns 0 for success, non-zero for failure.
2842  *
2843  */
2844 static int
2845 _base_wait_for_doorbell_not_used(struct MPT2SAS_ADAPTER *ioc, int timeout,
2846     int sleep_flag)
2847 {
2848         u32 cntdn, count;
2849         u32 doorbell_reg;
2850
2851         count = 0;
2852         cntdn = (sleep_flag == CAN_SLEEP) ? 1000*timeout : 2000*timeout;
2853         do {
2854                 doorbell_reg = readl(&ioc->chip->Doorbell);
2855                 if (!(doorbell_reg & MPI2_DOORBELL_USED)) {
2856                         dhsprintk(ioc, printk(MPT2SAS_INFO_FMT "%s: "
2857                             "successful count(%d), timeout(%d)\n", ioc->name,
2858                             __func__, count, timeout));
2859                         return 0;
2860                 }
2861                 if (sleep_flag == CAN_SLEEP)
2862                         msleep(1);
2863                 else
2864                         udelay(500);
2865                 count++;
2866         } while (--cntdn);
2867
2868         printk(MPT2SAS_ERR_FMT "%s: failed due to timeout count(%d), "
2869             "doorbell_reg(%x)!\n", ioc->name, __func__, count, doorbell_reg);
2870         return -EFAULT;
2871 }
2872
2873 /**
2874  * _base_send_ioc_reset - send doorbell reset
2875  * @ioc: per adapter object
2876  * @reset_type: currently only supports: MPI2_FUNCTION_IOC_MESSAGE_UNIT_RESET
2877  * @timeout: timeout in second
2878  * @sleep_flag: CAN_SLEEP or NO_SLEEP
2879  *
2880  * Returns 0 for success, non-zero for failure.
2881  */
2882 static int
2883 _base_send_ioc_reset(struct MPT2SAS_ADAPTER *ioc, u8 reset_type, int timeout,
2884     int sleep_flag)
2885 {
2886         u32 ioc_state;
2887         int r = 0;
2888
2889         if (reset_type != MPI2_FUNCTION_IOC_MESSAGE_UNIT_RESET) {
2890                 printk(MPT2SAS_ERR_FMT "%s: unknown reset_type\n",
2891                     ioc->name, __func__);
2892                 return -EFAULT;
2893         }
2894
2895         if (!(ioc->facts.IOCCapabilities &
2896            MPI2_IOCFACTS_CAPABILITY_EVENT_REPLAY))
2897                 return -EFAULT;
2898
2899         printk(MPT2SAS_INFO_FMT "sending message unit reset !!\n", ioc->name);
2900
2901         writel(reset_type << MPI2_DOORBELL_FUNCTION_SHIFT,
2902             &ioc->chip->Doorbell);
2903         if ((_base_wait_for_doorbell_ack(ioc, 15, sleep_flag))) {
2904                 r = -EFAULT;
2905                 goto out;
2906         }
2907         ioc_state = _base_wait_on_iocstate(ioc, MPI2_IOC_STATE_READY,
2908             timeout, sleep_flag);
2909         if (ioc_state) {
2910                 printk(MPT2SAS_ERR_FMT "%s: failed going to ready state "
2911                     " (ioc_state=0x%x)\n", ioc->name, __func__, ioc_state);
2912                 r = -EFAULT;
2913                 goto out;
2914         }
2915  out:
2916         printk(MPT2SAS_INFO_FMT "message unit reset: %s\n",
2917             ioc->name, ((r == 0) ? "SUCCESS" : "FAILED"));
2918         return r;
2919 }
2920
2921 /**
2922  * _base_handshake_req_reply_wait - send request thru doorbell interface
2923  * @ioc: per adapter object
2924  * @request_bytes: request length
2925  * @request: pointer having request payload
2926  * @reply_bytes: reply length
2927  * @reply: pointer to reply payload
2928  * @timeout: timeout in second
2929  * @sleep_flag: CAN_SLEEP or NO_SLEEP
2930  *
2931  * Returns 0 for success, non-zero for failure.
2932  */
2933 static int
2934 _base_handshake_req_reply_wait(struct MPT2SAS_ADAPTER *ioc, int request_bytes,
2935     u32 *request, int reply_bytes, u16 *reply, int timeout, int sleep_flag)
2936 {
2937         MPI2DefaultReply_t *default_reply = (MPI2DefaultReply_t *)reply;
2938         int i;
2939         u8 failed;
2940         u16 dummy;
2941         __le32 *mfp;
2942
2943         /* make sure doorbell is not in use */
2944         if ((readl(&ioc->chip->Doorbell) & MPI2_DOORBELL_USED)) {
2945                 printk(MPT2SAS_ERR_FMT "doorbell is in use "
2946                     " (line=%d)\n", ioc->name, __LINE__);
2947                 return -EFAULT;
2948         }
2949
2950         /* clear pending doorbell interrupts from previous state changes */
2951         if (readl(&ioc->chip->HostInterruptStatus) &
2952             MPI2_HIS_IOC2SYS_DB_STATUS)
2953                 writel(0, &ioc->chip->HostInterruptStatus);
2954
2955         /* send message to ioc */
2956         writel(((MPI2_FUNCTION_HANDSHAKE<<MPI2_DOORBELL_FUNCTION_SHIFT) |
2957             ((request_bytes/4)<<MPI2_DOORBELL_ADD_DWORDS_SHIFT)),
2958             &ioc->chip->Doorbell);
2959
2960         if ((_base_wait_for_doorbell_int(ioc, 5, NO_SLEEP))) {
2961                 printk(MPT2SAS_ERR_FMT "doorbell handshake "
2962                    "int failed (line=%d)\n", ioc->name, __LINE__);
2963                 return -EFAULT;
2964         }
2965         writel(0, &ioc->chip->HostInterruptStatus);
2966
2967         if ((_base_wait_for_doorbell_ack(ioc, 5, sleep_flag))) {
2968                 printk(MPT2SAS_ERR_FMT "doorbell handshake "
2969                     "ack failed (line=%d)\n", ioc->name, __LINE__);
2970                 return -EFAULT;
2971         }
2972
2973         /* send message 32-bits at a time */
2974         for (i = 0, failed = 0; i < request_bytes/4 && !failed; i++) {
2975                 writel(cpu_to_le32(request[i]), &ioc->chip->Doorbell);
2976                 if ((_base_wait_for_doorbell_ack(ioc, 5, sleep_flag)))
2977                         failed = 1;
2978         }
2979
2980         if (failed) {
2981                 printk(MPT2SAS_ERR_FMT "doorbell handshake "
2982                     "sending request failed (line=%d)\n", ioc->name, __LINE__);
2983                 return -EFAULT;
2984         }
2985
2986         /* now wait for the reply */
2987         if ((_base_wait_for_doorbell_int(ioc, timeout, sleep_flag))) {
2988                 printk(MPT2SAS_ERR_FMT "doorbell handshake "
2989                    "int failed (line=%d)\n", ioc->name, __LINE__);
2990                 return -EFAULT;
2991         }
2992
2993         /* read the first two 16-bits, it gives the total length of the reply */
2994         reply[0] = le16_to_cpu(readl(&ioc->chip->Doorbell)
2995             & MPI2_DOORBELL_DATA_MASK);
2996         writel(0, &ioc->chip->HostInterruptStatus);
2997         if ((_base_wait_for_doorbell_int(ioc, 5, sleep_flag))) {
2998                 printk(MPT2SAS_ERR_FMT "doorbell handshake "
2999                    "int failed (line=%d)\n", ioc->name, __LINE__);
3000                 return -EFAULT;
3001         }
3002         reply[1] = le16_to_cpu(readl(&ioc->chip->Doorbell)
3003             & MPI2_DOORBELL_DATA_MASK);
3004         writel(0, &ioc->chip->HostInterruptStatus);
3005
3006         for (i = 2; i < default_reply->MsgLength * 2; i++)  {
3007                 if ((_base_wait_for_doorbell_int(ioc, 5, sleep_flag))) {
3008                         printk(MPT2SAS_ERR_FMT "doorbell "
3009                             "handshake int failed (line=%d)\n", ioc->name,
3010                             __LINE__);
3011                         return -EFAULT;
3012                 }
3013                 if (i >=  reply_bytes/2) /* overflow case */
3014                         dummy = readl(&ioc->chip->Doorbell);
3015                 else
3016                         reply[i] = le16_to_cpu(readl(&ioc->chip->Doorbell)
3017                             & MPI2_DOORBELL_DATA_MASK);
3018                 writel(0, &ioc->chip->HostInterruptStatus);
3019         }
3020
3021         _base_wait_for_doorbell_int(ioc, 5, sleep_flag);
3022         if (_base_wait_for_doorbell_not_used(ioc, 5, sleep_flag) != 0) {
3023                 dhsprintk(ioc, printk(MPT2SAS_INFO_FMT "doorbell is in use "
3024                     " (line=%d)\n", ioc->name, __LINE__));
3025         }
3026         writel(0, &ioc->chip->HostInterruptStatus);
3027
3028         if (ioc->logging_level & MPT_DEBUG_INIT) {
3029                 mfp = (__le32 *)reply;
3030                 printk(KERN_INFO "\toffset:data\n");
3031                 for (i = 0; i < reply_bytes/4; i++)
3032                         printk(KERN_INFO "\t[0x%02x]:%08x\n", i*4,
3033                             le32_to_cpu(mfp[i]));
3034         }
3035         return 0;
3036 }
3037
3038 /**
3039  * mpt2sas_base_sas_iounit_control - send sas iounit control to FW
3040  * @ioc: per adapter object
3041  * @mpi_reply: the reply payload from FW
3042  * @mpi_request: the request payload sent to FW
3043  *
3044  * The SAS IO Unit Control Request message allows the host to perform low-level
3045  * operations, such as resets on the PHYs of the IO Unit, also allows the host
3046  * to obtain the IOC assigned device handles for a device if it has other
3047  * identifying information about the device, in addition allows the host to
3048  * remove IOC resources associated with the device.
3049  *
3050  * Returns 0 for success, non-zero for failure.
3051  */
3052 int
3053 mpt2sas_base_sas_iounit_control(struct MPT2SAS_ADAPTER *ioc,
3054     Mpi2SasIoUnitControlReply_t *mpi_reply,
3055     Mpi2SasIoUnitControlRequest_t *mpi_request)
3056 {
3057         u16 smid;
3058         u32 ioc_state;
3059         unsigned long timeleft;
3060         u8 issue_reset;
3061         int rc;
3062         void *request;
3063         u16 wait_state_count;
3064
3065         dinitprintk(ioc, printk(MPT2SAS_INFO_FMT "%s\n", ioc->name,
3066             __func__));
3067
3068         mutex_lock(&ioc->base_cmds.mutex);
3069
3070         if (ioc->base_cmds.status != MPT2_CMD_NOT_USED) {
3071                 printk(MPT2SAS_ERR_FMT "%s: base_cmd in use\n",
3072                     ioc->name, __func__);
3073                 rc = -EAGAIN;
3074                 goto out;
3075         }
3076
3077         wait_state_count = 0;
3078         ioc_state = mpt2sas_base_get_iocstate(ioc, 1);
3079         while (ioc_state != MPI2_IOC_STATE_OPERATIONAL) {
3080                 if (wait_state_count++ == 10) {
3081                         printk(MPT2SAS_ERR_FMT
3082                             "%s: failed due to ioc not operational\n",
3083                             ioc->name, __func__);
3084                         rc = -EFAULT;
3085                         goto out;
3086                 }
3087                 ssleep(1);
3088                 ioc_state = mpt2sas_base_get_iocstate(ioc, 1);
3089                 printk(MPT2SAS_INFO_FMT "%s: waiting for "
3090                     "operational state(count=%d)\n", ioc->name,
3091                     __func__, wait_state_count);
3092         }
3093
3094         smid = mpt2sas_base_get_smid(ioc, ioc->base_cb_idx);
3095         if (!smid) {
3096                 printk(MPT2SAS_ERR_FMT "%s: failed obtaining a smid\n",
3097                     ioc->name, __func__);
3098                 rc = -EAGAIN;
3099                 goto out;
3100         }
3101
3102         rc = 0;
3103         ioc->base_cmds.status = MPT2_CMD_PENDING;
3104         request = mpt2sas_base_get_msg_frame(ioc, smid);
3105         ioc->base_cmds.smid = smid;
3106         memcpy(request, mpi_request, sizeof(Mpi2SasIoUnitControlRequest_t));
3107         if (mpi_request->Operation == MPI2_SAS_OP_PHY_HARD_RESET ||
3108             mpi_request->Operation == MPI2_SAS_OP_PHY_LINK_RESET)
3109                 ioc->ioc_link_reset_in_progress = 1;
3110         mpt2sas_base_put_smid_default(ioc, smid);
3111         init_completion(&ioc->base_cmds.done);
3112         timeleft = wait_for_completion_timeout(&ioc->base_cmds.done,
3113             msecs_to_jiffies(10000));
3114         if ((mpi_request->Operation == MPI2_SAS_OP_PHY_HARD_RESET ||
3115             mpi_request->Operation == MPI2_SAS_OP_PHY_LINK_RESET) &&
3116             ioc->ioc_link_reset_in_progress)
3117                 ioc->ioc_link_reset_in_progress = 0;
3118         if (!(ioc->base_cmds.status & MPT2_CMD_COMPLETE)) {
3119                 printk(MPT2SAS_ERR_FMT "%s: timeout\n",
3120                     ioc->name, __func__);
3121                 _debug_dump_mf(mpi_request,
3122                     sizeof(Mpi2SasIoUnitControlRequest_t)/4);
3123                 if (!(ioc->base_cmds.status & MPT2_CMD_RESET))
3124                         issue_reset = 1;
3125                 goto issue_host_reset;
3126         }
3127         if (ioc->base_cmds.status & MPT2_CMD_REPLY_VALID)
3128                 memcpy(mpi_reply, ioc->base_cmds.reply,
3129                     sizeof(Mpi2SasIoUnitControlReply_t));
3130         else
3131                 memset(mpi_reply, 0, sizeof(Mpi2SasIoUnitControlReply_t));
3132         ioc->base_cmds.status = MPT2_CMD_NOT_USED;
3133         goto out;
3134
3135  issue_host_reset:
3136         if (issue_reset)
3137                 mpt2sas_base_hard_reset_handler(ioc, CAN_SLEEP,
3138                     FORCE_BIG_HAMMER);
3139         ioc->base_cmds.status = MPT2_CMD_NOT_USED;
3140         rc = -EFAULT;
3141  out:
3142         mutex_unlock(&ioc->base_cmds.mutex);
3143         return rc;
3144 }
3145
3146
3147 /**
3148  * mpt2sas_base_scsi_enclosure_processor - sending request to sep device
3149  * @ioc: per adapter object
3150  * @mpi_reply: the reply payload from FW
3151  * @mpi_request: the request payload sent to FW
3152  *
3153  * The SCSI Enclosure Processor request message causes the IOC to
3154  * communicate with SES devices to control LED status signals.
3155  *
3156  * Returns 0 for success, non-zero for failure.
3157  */
3158 int
3159 mpt2sas_base_scsi_enclosure_processor(struct MPT2SAS_ADAPTER *ioc,
3160     Mpi2SepReply_t *mpi_reply, Mpi2SepRequest_t *mpi_request)
3161 {
3162         u16 smid;
3163         u32 ioc_state;
3164         unsigned long timeleft;
3165         u8 issue_reset;
3166         int rc;
3167         void *request;
3168         u16 wait_state_count;
3169
3170         dinitprintk(ioc, printk(MPT2SAS_INFO_FMT "%s\n", ioc->name,
3171             __func__));
3172
3173         mutex_lock(&ioc->base_cmds.mutex);
3174
3175         if (ioc->base_cmds.status != MPT2_CMD_NOT_USED) {
3176                 printk(MPT2SAS_ERR_FMT "%s: base_cmd in use\n",
3177                     ioc->name, __func__);
3178                 rc = -EAGAIN;
3179                 goto out;
3180         }
3181
3182         wait_state_count = 0;
3183         ioc_state = mpt2sas_base_get_iocstate(ioc, 1);
3184         while (ioc_state != MPI2_IOC_STATE_OPERATIONAL) {
3185                 if (wait_state_count++ == 10) {
3186                         printk(MPT2SAS_ERR_FMT
3187                             "%s: failed due to ioc not operational\n",
3188                             ioc->name, __func__);
3189                         rc = -EFAULT;
3190                         goto out;
3191                 }
3192                 ssleep(1);
3193                 ioc_state = mpt2sas_base_get_iocstate(ioc, 1);
3194                 printk(MPT2SAS_INFO_FMT "%s: waiting for "
3195                     "operational state(count=%d)\n", ioc->name,
3196                     __func__, wait_state_count);
3197         }
3198
3199         smid = mpt2sas_base_get_smid(ioc, ioc->base_cb_idx);
3200         if (!smid) {
3201                 printk(MPT2SAS_ERR_FMT "%s: failed obtaining a smid\n",
3202                     ioc->name, __func__);
3203                 rc = -EAGAIN;
3204                 goto out;
3205         }
3206
3207         rc = 0;
3208         ioc->base_cmds.status = MPT2_CMD_PENDING;
3209         request = mpt2sas_base_get_msg_frame(ioc, smid);
3210         ioc->base_cmds.smid = smid;
3211         memcpy(request, mpi_request, sizeof(Mpi2SepReply_t));
3212         mpt2sas_base_put_smid_default(ioc, smid);
3213         init_completion(&ioc->base_cmds.done);
3214         timeleft = wait_for_completion_timeout(&ioc->base_cmds.done,
3215             msecs_to_jiffies(10000));
3216         if (!(ioc->base_cmds.status & MPT2_CMD_COMPLETE)) {
3217                 printk(MPT2SAS_ERR_FMT "%s: timeout\n",
3218                     ioc->name, __func__);
3219                 _debug_dump_mf(mpi_request,
3220                     sizeof(Mpi2SepRequest_t)/4);
3221                 if (!(ioc->base_cmds.status & MPT2_CMD_RESET))
3222                         issue_reset = 1;
3223                 goto issue_host_reset;
3224         }
3225         if (ioc->base_cmds.status & MPT2_CMD_REPLY_VALID)
3226                 memcpy(mpi_reply, ioc->base_cmds.reply,
3227                     sizeof(Mpi2SepReply_t));
3228         else
3229                 memset(mpi_reply, 0, sizeof(Mpi2SepReply_t));
3230         ioc->base_cmds.status = MPT2_CMD_NOT_USED;
3231         goto out;
3232
3233  issue_host_reset:
3234         if (issue_reset)
3235                 mpt2sas_base_hard_reset_handler(ioc, CAN_SLEEP,
3236                     FORCE_BIG_HAMMER);
3237         ioc->base_cmds.status = MPT2_CMD_NOT_USED;
3238         rc = -EFAULT;
3239  out:
3240         mutex_unlock(&ioc->base_cmds.mutex);
3241         return rc;
3242 }
3243
3244 /**
3245  * _base_get_port_facts - obtain port facts reply and save in ioc
3246  * @ioc: per adapter object
3247  * @sleep_flag: CAN_SLEEP or NO_SLEEP
3248  *
3249  * Returns 0 for success, non-zero for failure.
3250  */
3251 static int
3252 _base_get_port_facts(struct MPT2SAS_ADAPTER *ioc, int port, int sleep_flag)
3253 {
3254         Mpi2PortFactsRequest_t mpi_request;
3255         Mpi2PortFactsReply_t mpi_reply;
3256         struct mpt2sas_port_facts *pfacts;
3257         int mpi_reply_sz, mpi_request_sz, r;
3258
3259         dinitprintk(ioc, printk(MPT2SAS_INFO_FMT "%s\n", ioc->name,
3260             __func__));
3261
3262         mpi_reply_sz = sizeof(Mpi2PortFactsReply_t);
3263         mpi_request_sz = sizeof(Mpi2PortFactsRequest_t);
3264         memset(&mpi_request, 0, mpi_request_sz);
3265         mpi_request.Function = MPI2_FUNCTION_PORT_FACTS;
3266         mpi_request.PortNumber = port;
3267         r = _base_handshake_req_reply_wait(ioc, mpi_request_sz,
3268             (u32 *)&mpi_request, mpi_reply_sz, (u16 *)&mpi_reply, 5, CAN_SLEEP);
3269
3270         if (r != 0) {
3271                 printk(MPT2SAS_ERR_FMT "%s: handshake failed (r=%d)\n",
3272                     ioc->name, __func__, r);
3273                 return r;
3274         }
3275
3276         pfacts = &ioc->pfacts[port];
3277         memset(pfacts, 0, sizeof(Mpi2PortFactsReply_t));
3278         pfacts->PortNumber = mpi_reply.PortNumber;
3279         pfacts->VP_ID = mpi_reply.VP_ID;
3280         pfacts->VF_ID = mpi_reply.VF_ID;
3281         pfacts->MaxPostedCmdBuffers =
3282             le16_to_cpu(mpi_reply.MaxPostedCmdBuffers);
3283
3284         return 0;
3285 }
3286
3287 /**
3288  * _base_get_ioc_facts - obtain ioc facts reply and save in ioc
3289  * @ioc: per adapter object
3290  * @sleep_flag: CAN_SLEEP or NO_SLEEP
3291  *
3292  * Returns 0 for success, non-zero for failure.
3293  */
3294 static int
3295 _base_get_ioc_facts(struct MPT2SAS_ADAPTER *ioc, int sleep_flag)
3296 {
3297         Mpi2IOCFactsRequest_t mpi_request;
3298         Mpi2IOCFactsReply_t mpi_reply;
3299         struct mpt2sas_facts *facts;
3300         int mpi_reply_sz, mpi_request_sz, r;
3301
3302         dinitprintk(ioc, printk(MPT2SAS_INFO_FMT "%s\n", ioc->name,
3303             __func__));
3304
3305         mpi_reply_sz = sizeof(Mpi2IOCFactsReply_t);
3306         mpi_request_sz = sizeof(Mpi2IOCFactsRequest_t);
3307         memset(&mpi_request, 0, mpi_request_sz);
3308         mpi_request.Function = MPI2_FUNCTION_IOC_FACTS;
3309         r = _base_handshake_req_reply_wait(ioc, mpi_request_sz,
3310             (u32 *)&mpi_request, mpi_reply_sz, (u16 *)&mpi_reply, 5, CAN_SLEEP);
3311
3312         if (r != 0) {
3313                 printk(MPT2SAS_ERR_FMT "%s: handshake failed (r=%d)\n",
3314                     ioc->name, __func__, r);
3315                 return r;
3316         }
3317
3318         facts = &ioc->facts;
3319         memset(facts, 0, sizeof(Mpi2IOCFactsReply_t));
3320         facts->MsgVersion = le16_to_cpu(mpi_reply.MsgVersion);
3321         facts->HeaderVersion = le16_to_cpu(mpi_reply.HeaderVersion);
3322         facts->VP_ID = mpi_reply.VP_ID;
3323         facts->VF_ID = mpi_reply.VF_ID;
3324         facts->IOCExceptions = le16_to_cpu(mpi_reply.IOCExceptions);
3325         facts->MaxChainDepth = mpi_reply.MaxChainDepth;
3326         facts->WhoInit = mpi_reply.WhoInit;
3327         facts->NumberOfPorts = mpi_reply.NumberOfPorts;
3328         facts->MaxMSIxVectors = mpi_reply.MaxMSIxVectors;
3329         facts->RequestCredit = le16_to_cpu(mpi_reply.RequestCredit);
3330         facts->MaxReplyDescriptorPostQueueDepth =
3331             le16_to_cpu(mpi_reply.MaxReplyDescriptorPostQueueDepth);
3332         facts->ProductID = le16_to_cpu(mpi_reply.ProductID);
3333         facts->IOCCapabilities = le32_to_cpu(mpi_reply.IOCCapabilities);
3334         if ((facts->IOCCapabilities & MPI2_IOCFACTS_CAPABILITY_INTEGRATED_RAID))
3335                 ioc->ir_firmware = 1;
3336         facts->FWVersion.Word = le32_to_cpu(mpi_reply.FWVersion.Word);
3337         facts->IOCRequestFrameSize =
3338             le16_to_cpu(mpi_reply.IOCRequestFrameSize);
3339         facts->MaxInitiators = le16_to_cpu(mpi_reply.MaxInitiators);
3340         facts->MaxTargets = le16_to_cpu(mpi_reply.MaxTargets);
3341         ioc->shost->max_id = -1;
3342         facts->MaxSasExpanders = le16_to_cpu(mpi_reply.MaxSasExpanders);
3343         facts->MaxEnclosures = le16_to_cpu(mpi_reply.MaxEnclosures);
3344         facts->ProtocolFlags = le16_to_cpu(mpi_reply.ProtocolFlags);
3345         facts->HighPriorityCredit =
3346             le16_to_cpu(mpi_reply.HighPriorityCredit);
3347         facts->ReplyFrameSize = mpi_reply.ReplyFrameSize;
3348         facts->MaxDevHandle = le16_to_cpu(mpi_reply.MaxDevHandle);
3349
3350         dinitprintk(ioc, printk(MPT2SAS_INFO_FMT "hba queue depth(%d), "
3351             "max chains per io(%d)\n", ioc->name, facts->RequestCredit,
3352             facts->MaxChainDepth));
3353         dinitprintk(ioc, printk(MPT2SAS_INFO_FMT "request frame size(%d), "
3354             "reply frame size(%d)\n", ioc->name,
3355             facts->IOCRequestFrameSize * 4, facts->ReplyFrameSize * 4));
3356         return 0;
3357 }
3358
3359 /**
3360  * _base_send_ioc_init - send ioc_init to firmware
3361  * @ioc: per adapter object
3362  * @sleep_flag: CAN_SLEEP or NO_SLEEP
3363  *
3364  * Returns 0 for success, non-zero for failure.
3365  */
3366 static int
3367 _base_send_ioc_init(struct MPT2SAS_ADAPTER *ioc, int sleep_flag)
3368 {
3369         Mpi2IOCInitRequest_t mpi_request;
3370         Mpi2IOCInitReply_t mpi_reply;
3371         int r;
3372         struct timeval current_time;
3373         u16 ioc_status;
3374
3375         dinitprintk(ioc, printk(MPT2SAS_INFO_FMT "%s\n", ioc->name,
3376             __func__));
3377
3378         memset(&mpi_request, 0, sizeof(Mpi2IOCInitRequest_t));
3379         mpi_request.Function = MPI2_FUNCTION_IOC_INIT;
3380         mpi_request.WhoInit = MPI2_WHOINIT_HOST_DRIVER;
3381         mpi_request.VF_ID = 0; /* TODO */
3382         mpi_request.VP_ID = 0;
3383         mpi_request.MsgVersion = cpu_to_le16(MPI2_VERSION);
3384         mpi_request.HeaderVersion = cpu_to_le16(MPI2_HEADER_VERSION);
3385
3386         if (_base_is_controller_msix_enabled(ioc))
3387                 mpi_request.HostMSIxVectors = ioc->reply_queue_count;
3388         mpi_request.SystemRequestFrameSize = cpu_to_le16(ioc->request_sz/4);
3389         mpi_request.ReplyDescriptorPostQueueDepth =
3390             cpu_to_le16(ioc->reply_post_queue_depth);
3391         mpi_request.ReplyFreeQueueDepth =
3392             cpu_to_le16(ioc->reply_free_queue_depth);
3393
3394         mpi_request.SenseBufferAddressHigh =
3395             cpu_to_le32((u64)ioc->sense_dma >> 32);
3396         mpi_request.SystemReplyAddressHigh =
3397             cpu_to_le32((u64)ioc->reply_dma >> 32);
3398         mpi_request.SystemRequestFrameBaseAddress =
3399             cpu_to_le64((u64)ioc->request_dma);
3400         mpi_request.ReplyFreeQueueAddress =
3401             cpu_to_le64((u64)ioc->reply_free_dma);
3402         mpi_request.ReplyDescriptorPostQueueAddress =
3403             cpu_to_le64((u64)ioc->reply_post_free_dma);
3404
3405
3406         /* This time stamp specifies number of milliseconds
3407          * since epoch ~ midnight January 1, 1970.
3408          */
3409         do_gettimeofday(&current_time);
3410         mpi_request.TimeStamp = cpu_to_le64((u64)current_time.tv_sec * 1000 +
3411             (current_time.tv_usec / 1000));
3412
3413         if (ioc->logging_level & MPT_DEBUG_INIT) {
3414                 __le32 *mfp;
3415                 int i;
3416
3417                 mfp = (__le32 *)&mpi_request;
3418                 printk(KERN_INFO "\toffset:data\n");
3419                 for (i = 0; i < sizeof(Mpi2IOCInitRequest_t)/4; i++)
3420                         printk(KERN_INFO "\t[0x%02x]:%08x\n", i*4,
3421                             le32_to_cpu(mfp[i]));
3422         }
3423
3424         r = _base_handshake_req_reply_wait(ioc,
3425             sizeof(Mpi2IOCInitRequest_t), (u32 *)&mpi_request,
3426             sizeof(Mpi2IOCInitReply_t), (u16 *)&mpi_reply, 10,
3427             sleep_flag);
3428
3429         if (r != 0) {
3430                 printk(MPT2SAS_ERR_FMT "%s: handshake failed (r=%d)\n",
3431                     ioc->name, __func__, r);
3432                 return r;
3433         }
3434
3435         ioc_status = le16_to_cpu(mpi_reply.IOCStatus) & MPI2_IOCSTATUS_MASK;
3436         if (ioc_status != MPI2_IOCSTATUS_SUCCESS ||
3437             mpi_reply.IOCLogInfo) {
3438                 printk(MPT2SAS_ERR_FMT "%s: failed\n", ioc->name, __func__);
3439                 r = -EIO;
3440         }
3441
3442         return 0;
3443 }
3444
3445 /**
3446  * mpt2sas_port_enable_done - command completion routine for port enable
3447  * @ioc: per adapter object
3448  * @smid: system request message index
3449  * @msix_index: MSIX table index supplied by the OS
3450  * @reply: reply message frame(lower 32bit addr)
3451  *
3452  * Return 1 meaning mf should be freed from _base_interrupt
3453  *        0 means the mf is freed from this function.
3454  */
3455 u8
3456 mpt2sas_port_enable_done(struct MPT2SAS_ADAPTER *ioc, u16 smid, u8 msix_index,
3457         u32 reply)
3458 {
3459         MPI2DefaultReply_t *mpi_reply;
3460         u16 ioc_status;
3461
3462         mpi_reply = mpt2sas_base_get_reply_virt_addr(ioc, reply);
3463         if (mpi_reply && mpi_reply->Function == MPI2_FUNCTION_EVENT_ACK)
3464                 return 1;
3465
3466         if (ioc->port_enable_cmds.status == MPT2_CMD_NOT_USED)
3467                 return 1;
3468
3469         ioc->port_enable_cmds.status |= MPT2_CMD_COMPLETE;
3470         if (mpi_reply) {
3471                 ioc->port_enable_cmds.status |= MPT2_CMD_REPLY_VALID;
3472                 memcpy(ioc->port_enable_cmds.reply, mpi_reply,
3473                     mpi_reply->MsgLength*4);
3474         }
3475         ioc->port_enable_cmds.status &= ~MPT2_CMD_PENDING;
3476
3477         ioc_status = le16_to_cpu(mpi_reply->IOCStatus) & MPI2_IOCSTATUS_MASK;
3478
3479         if (ioc_status != MPI2_IOCSTATUS_SUCCESS)
3480                 ioc->port_enable_failed = 1;
3481
3482         if (ioc->is_driver_loading) {
3483                 if (ioc_status == MPI2_IOCSTATUS_SUCCESS) {
3484                         mpt2sas_port_enable_complete(ioc);
3485                         return 1;
3486                 } else {
3487                         ioc->start_scan_failed = ioc_status;
3488                         ioc->start_scan = 0;
3489                         return 1;
3490                 }
3491         }
3492         complete(&ioc->port_enable_cmds.done);
3493         return 1;
3494 }
3495
3496
3497 /**
3498  * _base_send_port_enable - send port_enable(discovery stuff) to firmware
3499  * @ioc: per adapter object
3500  * @sleep_flag: CAN_SLEEP or NO_SLEEP
3501  *
3502  * Returns 0 for success, non-zero for failure.
3503  */
3504 static int
3505 _base_send_port_enable(struct MPT2SAS_ADAPTER *ioc, int sleep_flag)
3506 {
3507         Mpi2PortEnableRequest_t *mpi_request;
3508         Mpi2PortEnableReply_t *mpi_reply;
3509         unsigned long timeleft;
3510         int r = 0;
3511         u16 smid;
3512         u16 ioc_status;
3513
3514         printk(MPT2SAS_INFO_FMT "sending port enable !!\n", ioc->name);
3515
3516         if (ioc->port_enable_cmds.status & MPT2_CMD_PENDING) {
3517                 printk(MPT2SAS_ERR_FMT "%s: internal command already in use\n",
3518                     ioc->name, __func__);
3519                 return -EAGAIN;
3520         }
3521
3522         smid = mpt2sas_base_get_smid(ioc, ioc->port_enable_cb_idx);
3523         if (!smid) {
3524                 printk(MPT2SAS_ERR_FMT "%s: failed obtaining a smid\n",
3525                     ioc->name, __func__);
3526                 return -EAGAIN;
3527         }
3528
3529         ioc->port_enable_cmds.status = MPT2_CMD_PENDING;
3530         mpi_request = mpt2sas_base_get_msg_frame(ioc, smid);
3531         ioc->port_enable_cmds.smid = smid;
3532         memset(mpi_request, 0, sizeof(Mpi2PortEnableRequest_t));
3533         mpi_request->Function = MPI2_FUNCTION_PORT_ENABLE;
3534
3535         init_completion(&ioc->port_enable_cmds.done);
3536         mpt2sas_base_put_smid_default(ioc, smid);
3537         timeleft = wait_for_completion_timeout(&ioc->port_enable_cmds.done,
3538             300*HZ);
3539         if (!(ioc->port_enable_cmds.status & MPT2_CMD_COMPLETE)) {
3540                 printk(MPT2SAS_ERR_FMT "%s: timeout\n",
3541                     ioc->name, __func__);
3542                 _debug_dump_mf(mpi_request,
3543                     sizeof(Mpi2PortEnableRequest_t)/4);
3544                 if (ioc->port_enable_cmds.status & MPT2_CMD_RESET)
3545                         r = -EFAULT;
3546                 else
3547                         r = -ETIME;
3548                 goto out;
3549         }
3550         mpi_reply = ioc->port_enable_cmds.reply;
3551
3552         ioc_status = le16_to_cpu(mpi_reply->IOCStatus) & MPI2_IOCSTATUS_MASK;
3553         if (ioc_status != MPI2_IOCSTATUS_SUCCESS) {
3554                 printk(MPT2SAS_ERR_FMT "%s: failed with (ioc_status=0x%08x)\n",
3555                     ioc->name, __func__, ioc_status);
3556                 r = -EFAULT;
3557                 goto out;
3558         }
3559  out:
3560         ioc->port_enable_cmds.status = MPT2_CMD_NOT_USED;
3561         printk(MPT2SAS_INFO_FMT "port enable: %s\n", ioc->name, ((r == 0) ?
3562             "SUCCESS" : "FAILED"));
3563         return r;
3564 }
3565
3566 /**
3567  * mpt2sas_port_enable - initiate firmware discovery (don't wait for reply)
3568  * @ioc: per adapter object
3569  *
3570  * Returns 0 for success, non-zero for failure.
3571  */
3572 int
3573 mpt2sas_port_enable(struct MPT2SAS_ADAPTER *ioc)
3574 {
3575         Mpi2PortEnableRequest_t *mpi_request;
3576         u16 smid;
3577
3578         printk(MPT2SAS_INFO_FMT "sending port enable !!\n", ioc->name);
3579
3580         if (ioc->port_enable_cmds.status & MPT2_CMD_PENDING) {
3581                 printk(MPT2SAS_ERR_FMT "%s: internal command already in use\n",
3582                     ioc->name, __func__);
3583                 return -EAGAIN;
3584         }
3585
3586         smid = mpt2sas_base_get_smid(ioc, ioc->port_enable_cb_idx);
3587         if (!smid) {
3588                 printk(MPT2SAS_ERR_FMT "%s: failed obtaining a smid\n",
3589                     ioc->name, __func__);
3590                 return -EAGAIN;
3591         }
3592
3593         ioc->port_enable_cmds.status = MPT2_CMD_PENDING;
3594         mpi_request = mpt2sas_base_get_msg_frame(ioc, smid);
3595         ioc->port_enable_cmds.smid = smid;
3596         memset(mpi_request, 0, sizeof(Mpi2PortEnableRequest_t));
3597         mpi_request->Function = MPI2_FUNCTION_PORT_ENABLE;
3598
3599         mpt2sas_base_put_smid_default(ioc, smid);
3600         return 0;
3601 }
3602
3603 /**
3604  * _base_determine_wait_on_discovery - desposition
3605  * @ioc: per adapter object
3606  *
3607  * Decide whether to wait on discovery to complete. Used to either
3608  * locate boot device, or report volumes ahead of physical devices.
3609  *
3610  * Returns 1 for wait, 0 for don't wait
3611  */
3612 static int
3613 _base_determine_wait_on_discovery(struct MPT2SAS_ADAPTER *ioc)
3614 {
3615         /* We wait for discovery to complete if IR firmware is loaded.
3616          * The sas topology events arrive before PD events, so we need time to
3617          * turn on the bit in ioc->pd_handles to indicate PD
3618          * Also, it maybe required to report Volumes ahead of physical
3619          * devices when MPI2_IOCPAGE8_IRFLAGS_LOW_VOLUME_MAPPING is set.
3620          */
3621         if (ioc->ir_firmware)
3622                 return 1;
3623
3624         /* if no Bios, then we don't need to wait */
3625         if (!ioc->bios_pg3.BiosVersion)
3626                 return 0;
3627
3628         /* Bios is present, then we drop down here.
3629          *
3630          * If there any entries in the Bios Page 2, then we wait
3631          * for discovery to complete.
3632          */
3633
3634         /* Current Boot Device */
3635         if ((ioc->bios_pg2.CurrentBootDeviceForm &
3636             MPI2_BIOSPAGE2_FORM_MASK) ==
3637             MPI2_BIOSPAGE2_FORM_NO_DEVICE_SPECIFIED &&
3638         /* Request Boot Device */
3639            (ioc->bios_pg2.ReqBootDeviceForm &
3640             MPI2_BIOSPAGE2_FORM_MASK) ==
3641             MPI2_BIOSPAGE2_FORM_NO_DEVICE_SPECIFIED &&
3642         /* Alternate Request Boot Device */
3643            (ioc->bios_pg2.ReqAltBootDeviceForm &
3644             MPI2_BIOSPAGE2_FORM_MASK) ==
3645             MPI2_BIOSPAGE2_FORM_NO_DEVICE_SPECIFIED)
3646                 return 0;
3647
3648         return 1;
3649 }
3650
3651
3652 /**
3653  * _base_unmask_events - turn on notification for this event
3654  * @ioc: per adapter object
3655  * @event: firmware event
3656  *
3657  * The mask is stored in ioc->event_masks.
3658  */
3659 static void
3660 _base_unmask_events(struct MPT2SAS_ADAPTER *ioc, u16 event)
3661 {
3662         u32 desired_event;
3663
3664         if (event >= 128)
3665                 return;
3666
3667         desired_event = (1 << (event % 32));
3668
3669         if (event < 32)
3670                 ioc->event_masks[0] &= ~desired_event;
3671         else if (event < 64)
3672                 ioc->event_masks[1] &= ~desired_event;
3673         else if (event < 96)
3674                 ioc->event_masks[2] &= ~desired_event;
3675         else if (event < 128)
3676                 ioc->event_masks[3] &= ~desired_event;
3677 }
3678
3679 /**
3680  * _base_event_notification - send event notification
3681  * @ioc: per adapter object
3682  * @sleep_flag: CAN_SLEEP or NO_SLEEP
3683  *
3684  * Returns 0 for success, non-zero for failure.
3685  */
3686 static int
3687 _base_event_notification(struct MPT2SAS_ADAPTER *ioc, int sleep_flag)
3688 {
3689         Mpi2EventNotificationRequest_t *mpi_request;
3690         unsigned long timeleft;
3691         u16 smid;
3692         int r = 0;
3693         int i;
3694
3695         dinitprintk(ioc, printk(MPT2SAS_INFO_FMT "%s\n", ioc->name,
3696             __func__));
3697
3698         if (ioc->base_cmds.status & MPT2_CMD_PENDING) {
3699                 printk(MPT2SAS_ERR_FMT "%s: internal command already in use\n",
3700                     ioc->name, __func__);
3701                 return -EAGAIN;
3702         }
3703
3704         smid = mpt2sas_base_get_smid(ioc, ioc->base_cb_idx);
3705         if (!smid) {
3706                 printk(MPT2SAS_ERR_FMT "%s: failed obtaining a smid\n",
3707                     ioc->name, __func__);
3708                 return -EAGAIN;
3709         }
3710         ioc->base_cmds.status = MPT2_CMD_PENDING;
3711         mpi_request = mpt2sas_base_get_msg_frame(ioc, smid);
3712         ioc->base_cmds.smid = smid;
3713         memset(mpi_request, 0, sizeof(Mpi2EventNotificationRequest_t));
3714         mpi_request->Function = MPI2_FUNCTION_EVENT_NOTIFICATION;
3715         mpi_request->VF_ID = 0; /* TODO */
3716         mpi_request->VP_ID = 0;
3717         for (i = 0; i < MPI2_EVENT_NOTIFY_EVENTMASK_WORDS; i++)
3718                 mpi_request->EventMasks[i] =
3719                     cpu_to_le32(ioc->event_masks[i]);
3720         mpt2sas_base_put_smid_default(ioc, smid);
3721         init_completion(&ioc->base_cmds.done);
3722         timeleft = wait_for_completion_timeout(&ioc->base_cmds.done, 30*HZ);
3723         if (!(ioc->base_cmds.status & MPT2_CMD_COMPLETE)) {
3724                 printk(MPT2SAS_ERR_FMT "%s: timeout\n",
3725                     ioc->name, __func__);
3726                 _debug_dump_mf(mpi_request,
3727                     sizeof(Mpi2EventNotificationRequest_t)/4);
3728                 if (ioc->base_cmds.status & MPT2_CMD_RESET)
3729                         r = -EFAULT;
3730                 else
3731                         r = -ETIME;
3732         } else
3733                 dinitprintk(ioc, printk(MPT2SAS_INFO_FMT "%s: complete\n",
3734                     ioc->name, __func__));
3735         ioc->base_cmds.status = MPT2_CMD_NOT_USED;
3736         return r;
3737 }
3738
3739 /**
3740  * mpt2sas_base_validate_event_type - validating event types
3741  * @ioc: per adapter object
3742  * @event: firmware event
3743  *
3744  * This will turn on firmware event notification when application
3745  * ask for that event. We don't mask events that are already enabled.
3746  */
3747 void
3748 mpt2sas_base_validate_event_type(struct MPT2SAS_ADAPTER *ioc, u32 *event_type)
3749 {
3750         int i, j;
3751         u32 event_mask, desired_event;
3752         u8 send_update_to_fw;
3753
3754         for (i = 0, send_update_to_fw = 0; i <
3755             MPI2_EVENT_NOTIFY_EVENTMASK_WORDS; i++) {
3756                 event_mask = ~event_type[i];
3757                 desired_event = 1;
3758                 for (j = 0; j < 32; j++) {
3759                         if (!(event_mask & desired_event) &&
3760                             (ioc->event_masks[i] & desired_event)) {
3761                                 ioc->event_masks[i] &= ~desired_event;
3762                                 send_update_to_fw = 1;
3763                         }
3764                         desired_event = (desired_event << 1);
3765                 }
3766         }
3767
3768         if (!send_update_to_fw)
3769                 return;
3770
3771         mutex_lock(&ioc->base_cmds.mutex);
3772         _base_event_notification(ioc, CAN_SLEEP);
3773         mutex_unlock(&ioc->base_cmds.mutex);
3774 }
3775
3776 /**
3777  * _base_diag_reset - the "big hammer" start of day reset
3778  * @ioc: per adapter object
3779  * @sleep_flag: CAN_SLEEP or NO_SLEEP
3780  *
3781  * Returns 0 for success, non-zero for failure.
3782  */
3783 static int
3784 _base_diag_reset(struct MPT2SAS_ADAPTER *ioc, int sleep_flag)
3785 {
3786         u32 host_diagnostic;
3787         u32 ioc_state;
3788         u32 count;
3789         u32 hcb_size;
3790
3791         printk(MPT2SAS_INFO_FMT "sending diag reset !!\n", ioc->name);
3792         drsprintk(ioc, printk(MPT2SAS_INFO_FMT "clear interrupts\n",
3793             ioc->name));
3794
3795         count = 0;
3796         do {
3797                 /* Write magic sequence to WriteSequence register
3798                  * Loop until in diagnostic mode
3799                  */
3800                 drsprintk(ioc, printk(MPT2SAS_INFO_FMT "write magic "
3801                     "sequence\n", ioc->name));
3802                 writel(MPI2_WRSEQ_FLUSH_KEY_VALUE, &ioc->chip->WriteSequence);
3803                 writel(MPI2_WRSEQ_1ST_KEY_VALUE, &ioc->chip->WriteSequence);
3804                 writel(MPI2_WRSEQ_2ND_KEY_VALUE, &ioc->chip->WriteSequence);
3805                 writel(MPI2_WRSEQ_3RD_KEY_VALUE, &ioc->chip->WriteSequence);
3806                 writel(MPI2_WRSEQ_4TH_KEY_VALUE, &ioc->chip->WriteSequence);
3807                 writel(MPI2_WRSEQ_5TH_KEY_VALUE, &ioc->chip->WriteSequence);
3808                 writel(MPI2_WRSEQ_6TH_KEY_VALUE, &ioc->chip->WriteSequence);
3809
3810                 /* wait 100 msec */
3811                 if (sleep_flag == CAN_SLEEP)
3812                         msleep(100);
3813                 else
3814                         mdelay(100);
3815
3816                 if (count++ > 20)
3817                         goto out;
3818
3819                 host_diagnostic = readl(&ioc->chip->HostDiagnostic);
3820                 drsprintk(ioc, printk(MPT2SAS_INFO_FMT "wrote magic "
3821                     "sequence: count(%d), host_diagnostic(0x%08x)\n",
3822                     ioc->name, count, host_diagnostic));
3823
3824         } while ((host_diagnostic & MPI2_DIAG_DIAG_WRITE_ENABLE) == 0);
3825
3826         hcb_size = readl(&ioc->chip->HCBSize);
3827
3828         drsprintk(ioc, printk(MPT2SAS_INFO_FMT "diag reset: issued\n",
3829             ioc->name));
3830         writel(host_diagnostic | MPI2_DIAG_RESET_ADAPTER,
3831              &ioc->chip->HostDiagnostic);
3832
3833         /* don't access any registers for 50 milliseconds */
3834         msleep(50);
3835
3836         /* 300 second max wait */
3837         for (count = 0; count < 3000000 ; count++) {
3838
3839                 host_diagnostic = readl(&ioc->chip->HostDiagnostic);
3840
3841                 if (host_diagnostic == 0xFFFFFFFF)
3842                         goto out;
3843                 if (!(host_diagnostic & MPI2_DIAG_RESET_ADAPTER))
3844                         break;
3845
3846                 /* wait 100 msec */
3847                 if (sleep_flag == CAN_SLEEP)
3848                         msleep(1);
3849                 else
3850                         mdelay(1);
3851         }
3852
3853         if (host_diagnostic & MPI2_DIAG_HCB_MODE) {
3854
3855                 drsprintk(ioc, printk(MPT2SAS_INFO_FMT "restart the adapter "
3856                     "assuming the HCB Address points to good F/W\n",
3857                     ioc->name));
3858                 host_diagnostic &= ~MPI2_DIAG_BOOT_DEVICE_SELECT_MASK;
3859                 host_diagnostic |= MPI2_DIAG_BOOT_DEVICE_SELECT_HCDW;
3860                 writel(host_diagnostic, &ioc->chip->HostDiagnostic);
3861
3862                 drsprintk(ioc, printk(MPT2SAS_INFO_FMT
3863                     "re-enable the HCDW\n", ioc->name));
3864                 writel(hcb_size | MPI2_HCB_SIZE_HCB_ENABLE,
3865                     &ioc->chip->HCBSize);
3866         }
3867
3868         drsprintk(ioc, printk(MPT2SAS_INFO_FMT "restart the adapter\n",
3869             ioc->name));
3870         writel(host_diagnostic & ~MPI2_DIAG_HOLD_IOC_RESET,
3871             &ioc->chip->HostDiagnostic);
3872
3873         drsprintk(ioc, printk(MPT2SAS_INFO_FMT "disable writes to the "
3874             "diagnostic register\n", ioc->name));
3875         writel(MPI2_WRSEQ_FLUSH_KEY_VALUE, &ioc->chip->WriteSequence);
3876
3877         drsprintk(ioc, printk(MPT2SAS_INFO_FMT "Wait for FW to go to the "
3878             "READY state\n", ioc->name));
3879         ioc_state = _base_wait_on_iocstate(ioc, MPI2_IOC_STATE_READY, 20,
3880             sleep_flag);
3881         if (ioc_state) {
3882                 printk(MPT2SAS_ERR_FMT "%s: failed going to ready state "
3883                     " (ioc_state=0x%x)\n", ioc->name, __func__, ioc_state);
3884                 goto out;
3885         }
3886
3887         printk(MPT2SAS_INFO_FMT "diag reset: SUCCESS\n", ioc->name);
3888         return 0;
3889
3890  out:
3891         printk(MPT2SAS_ERR_FMT "diag reset: FAILED\n", ioc->name);
3892         return -EFAULT;
3893 }
3894
3895 /**
3896  * _base_make_ioc_ready - put controller in READY state
3897  * @ioc: per adapter object
3898  * @sleep_flag: CAN_SLEEP or NO_SLEEP
3899  * @type: FORCE_BIG_HAMMER or SOFT_RESET
3900  *
3901  * Returns 0 for success, non-zero for failure.
3902  */
3903 static int
3904 _base_make_ioc_ready(struct MPT2SAS_ADAPTER *ioc, int sleep_flag,
3905     enum reset_type type)
3906 {
3907         u32 ioc_state;
3908         int rc;
3909
3910         dinitprintk(ioc, printk(MPT2SAS_INFO_FMT "%s\n", ioc->name,
3911             __func__));
3912
3913         if (ioc->pci_error_recovery)
3914                 return 0;
3915
3916         ioc_state = mpt2sas_base_get_iocstate(ioc, 0);
3917         dhsprintk(ioc, printk(MPT2SAS_INFO_FMT "%s: ioc_state(0x%08x)\n",
3918             ioc->name, __func__, ioc_state));
3919
3920         if ((ioc_state & MPI2_IOC_STATE_MASK) == MPI2_IOC_STATE_READY)
3921                 return 0;
3922
3923         if (ioc_state & MPI2_DOORBELL_USED) {
3924                 dhsprintk(ioc, printk(MPT2SAS_INFO_FMT "unexpected doorbell "
3925                     "active!\n", ioc->name));
3926                 goto issue_diag_reset;
3927         }
3928
3929         if ((ioc_state & MPI2_IOC_STATE_MASK) == MPI2_IOC_STATE_FAULT) {
3930                 mpt2sas_base_fault_info(ioc, ioc_state &
3931                     MPI2_DOORBELL_DATA_MASK);
3932                 goto issue_diag_reset;
3933         }
3934
3935         if (type == FORCE_BIG_HAMMER)
3936                 goto issue_diag_reset;
3937
3938         if ((ioc_state & MPI2_IOC_STATE_MASK) == MPI2_IOC_STATE_OPERATIONAL)
3939                 if (!(_base_send_ioc_reset(ioc,
3940                     MPI2_FUNCTION_IOC_MESSAGE_UNIT_RESET, 15, CAN_SLEEP))) {
3941                         ioc->ioc_reset_count++;
3942                         return 0;
3943         }
3944
3945  issue_diag_reset:
3946         rc = _base_diag_reset(ioc, CAN_SLEEP);
3947         ioc->ioc_reset_count++;
3948         return rc;
3949 }
3950
3951 /**
3952  * _base_make_ioc_operational - put controller in OPERATIONAL state
3953  * @ioc: per adapter object
3954  * @sleep_flag: CAN_SLEEP or NO_SLEEP
3955  *
3956  * Returns 0 for success, non-zero for failure.
3957  */
3958 static int
3959 _base_make_ioc_operational(struct MPT2SAS_ADAPTER *ioc, int sleep_flag)
3960 {
3961         int r, i;
3962         unsigned long   flags;
3963         u32 reply_address;
3964         u16 smid;
3965         struct _tr_list *delayed_tr, *delayed_tr_next;
3966         u8 hide_flag;
3967         struct adapter_reply_queue *reply_q;
3968         long reply_post_free;
3969         u32 reply_post_free_sz;
3970
3971         dinitprintk(ioc, printk(MPT2SAS_INFO_FMT "%s\n", ioc->name,
3972             __func__));
3973
3974         /* clean the delayed target reset list */
3975         list_for_each_entry_safe(delayed_tr, delayed_tr_next,
3976             &ioc->delayed_tr_list, list) {
3977                 list_del(&delayed_tr->list);
3978                 kfree(delayed_tr);
3979         }
3980
3981         list_for_each_entry_safe(delayed_tr, delayed_tr_next,
3982             &ioc->delayed_tr_volume_list, list) {
3983                 list_del(&delayed_tr->list);
3984                 kfree(delayed_tr);
3985         }
3986
3987         /* initialize the scsi lookup free list */
3988         spin_lock_irqsave(&ioc->scsi_lookup_lock, flags);
3989         INIT_LIST_HEAD(&ioc->free_list);
3990         smid = 1;
3991         for (i = 0; i < ioc->scsiio_depth; i++, smid++) {
3992                 INIT_LIST_HEAD(&ioc->scsi_lookup[i].chain_list);
3993                 ioc->scsi_lookup[i].cb_idx = 0xFF;
3994                 ioc->scsi_lookup[i].smid = smid;
3995                 ioc->scsi_lookup[i].scmd = NULL;
3996                 ioc->scsi_lookup[i].direct_io = 0;
3997                 list_add_tail(&ioc->scsi_lookup[i].tracker_list,
3998                     &ioc->free_list);
3999         }
4000
4001         /* hi-priority queue */
4002         INIT_LIST_HEAD(&ioc->hpr_free_list);
4003         smid = ioc->hi_priority_smid;
4004         for (i = 0; i < ioc->hi_priority_depth; i++, smid++) {
4005                 ioc->hpr_lookup[i].cb_idx = 0xFF;
4006                 ioc->hpr_lookup[i].smid = smid;
4007                 list_add_tail(&ioc->hpr_lookup[i].tracker_list,
4008                     &ioc->hpr_free_list);
4009         }
4010
4011         /* internal queue */
4012         INIT_LIST_HEAD(&ioc->internal_free_list);
4013         smid = ioc->internal_smid;
4014         for (i = 0; i < ioc->internal_depth; i++, smid++) {
4015                 ioc->internal_lookup[i].cb_idx = 0xFF;
4016                 ioc->internal_lookup[i].smid = smid;
4017                 list_add_tail(&ioc->internal_lookup[i].tracker_list,
4018                     &ioc->internal_free_list);
4019         }
4020
4021         /* chain pool */
4022         INIT_LIST_HEAD(&ioc->free_chain_list);
4023         for (i = 0; i < ioc->chain_depth; i++)
4024                 list_add_tail(&ioc->chain_lookup[i].tracker_list,
4025                     &ioc->free_chain_list);
4026
4027         spin_unlock_irqrestore(&ioc->scsi_lookup_lock, flags);
4028
4029         /* initialize Reply Free Queue */
4030         for (i = 0, reply_address = (u32)ioc->reply_dma ;
4031             i < ioc->reply_free_queue_depth ; i++, reply_address +=
4032             ioc->reply_sz)
4033                 ioc->reply_free[i] = cpu_to_le32(reply_address);
4034
4035         /* initialize reply queues */
4036         if (ioc->is_driver_loading)
4037                 _base_assign_reply_queues(ioc);
4038
4039         /* initialize Reply Post Free Queue */
4040         reply_post_free = (long)ioc->reply_post_free;
4041         reply_post_free_sz = ioc->reply_post_queue_depth *
4042             sizeof(Mpi2DefaultReplyDescriptor_t);
4043         list_for_each_entry(reply_q, &ioc->reply_queue_list, list) {
4044                 reply_q->reply_post_host_index = 0;
4045                 reply_q->reply_post_free = (Mpi2ReplyDescriptorsUnion_t *)
4046                     reply_post_free;
4047                 for (i = 0; i < ioc->reply_post_queue_depth; i++)
4048                         reply_q->reply_post_free[i].Words =
4049                                                         cpu_to_le64(ULLONG_MAX);
4050                 if (!_base_is_controller_msix_enabled(ioc))
4051                         goto skip_init_reply_post_free_queue;
4052                 reply_post_free += reply_post_free_sz;
4053         }
4054  skip_init_reply_post_free_queue:
4055
4056         r = _base_send_ioc_init(ioc, sleep_flag);
4057         if (r)
4058                 return r;
4059
4060         /* initialize reply free host index */
4061         ioc->reply_free_host_index = ioc->reply_free_queue_depth - 1;
4062         writel(ioc->reply_free_host_index, &ioc->chip->ReplyFreeHostIndex);
4063
4064         /* initialize reply post host index */
4065         list_for_each_entry(reply_q, &ioc->reply_queue_list, list) {
4066                 writel(reply_q->msix_index << MPI2_RPHI_MSIX_INDEX_SHIFT,
4067                     &ioc->chip->ReplyPostHostIndex);
4068                 if (!_base_is_controller_msix_enabled(ioc))
4069                         goto skip_init_reply_post_host_index;
4070         }
4071
4072  skip_init_reply_post_host_index:
4073
4074         _base_unmask_interrupts(ioc);
4075
4076         r = _base_event_notification(ioc, sleep_flag);
4077         if (r)
4078                 return r;
4079
4080         if (sleep_flag == CAN_SLEEP)
4081                 _base_static_config_pages(ioc);
4082
4083
4084         if (ioc->is_driver_loading) {
4085                 if (ioc->is_warpdrive && ioc->manu_pg10.OEMIdentifier
4086                     == 0x80) {
4087                         hide_flag = (u8) (ioc->manu_pg10.OEMSpecificFlags0 &
4088                             MFG_PAGE10_HIDE_SSDS_MASK);
4089                         if (hide_flag != MFG_PAGE10_HIDE_SSDS_MASK)
4090                                 ioc->mfg_pg10_hide_flag = hide_flag;
4091                 }
4092                 ioc->wait_for_discovery_to_complete =
4093                     _base_determine_wait_on_discovery(ioc);
4094                 return r; /* scan_start and scan_finished support */
4095         }
4096         r = _base_send_port_enable(ioc, sleep_flag);
4097         if (r)
4098                 return r;
4099
4100         return r;
4101 }
4102
4103 /**
4104  * mpt2sas_base_free_resources - free resources controller resources (io/irq/memap)
4105  * @ioc: per adapter object
4106  *
4107  * Return nothing.
4108  */
4109 void
4110 mpt2sas_base_free_resources(struct MPT2SAS_ADAPTER *ioc)
4111 {
4112         struct pci_dev *pdev = ioc->pdev;
4113
4114         dexitprintk(ioc, printk(MPT2SAS_INFO_FMT "%s\n", ioc->name,
4115             __func__));
4116
4117         _base_mask_interrupts(ioc);
4118         ioc->shost_recovery = 1;
4119         _base_make_ioc_ready(ioc, CAN_SLEEP, SOFT_RESET);
4120         ioc->shost_recovery = 0;
4121         _base_free_irq(ioc);
4122         _base_disable_msix(ioc);
4123         if (ioc->chip_phys)
4124                 iounmap(ioc->chip);
4125         ioc->chip_phys = 0;
4126         pci_release_selected_regions(ioc->pdev, ioc->bars);
4127         pci_disable_pcie_error_reporting(pdev);
4128         pci_disable_device(pdev);
4129         return;
4130 }
4131
4132 /**
4133  * mpt2sas_base_attach - attach controller instance
4134  * @ioc: per adapter object
4135  *
4136  * Returns 0 for success, non-zero for failure.
4137  */
4138 int
4139 mpt2sas_base_attach(struct MPT2SAS_ADAPTER *ioc)
4140 {
4141         int r, i;
4142         int cpu_id, last_cpu_id = 0;
4143
4144         dinitprintk(ioc, printk(MPT2SAS_INFO_FMT "%s\n", ioc->name,
4145             __func__));
4146
4147         /* setup cpu_msix_table */
4148         ioc->cpu_count = num_online_cpus();
4149         for_each_online_cpu(cpu_id)
4150                 last_cpu_id = cpu_id;
4151         ioc->cpu_msix_table_sz = last_cpu_id + 1;
4152         ioc->cpu_msix_table = kzalloc(ioc->cpu_msix_table_sz, GFP_KERNEL);
4153         ioc->reply_queue_count = 1;
4154         if (!ioc->cpu_msix_table) {
4155                 dfailprintk(ioc, printk(MPT2SAS_INFO_FMT "allocation for "
4156                     "cpu_msix_table failed!!!\n", ioc->name));
4157                 r = -ENOMEM;
4158                 goto out_free_resources;
4159         }
4160
4161         if (ioc->is_warpdrive) {
4162                 ioc->reply_post_host_index = kcalloc(ioc->cpu_msix_table_sz,
4163                     sizeof(resource_size_t *), GFP_KERNEL);
4164                 if (!ioc->reply_post_host_index) {
4165                         dfailprintk(ioc, printk(MPT2SAS_INFO_FMT "allocation "
4166                                 "for cpu_msix_table failed!!!\n", ioc->name));
4167                         r = -ENOMEM;
4168                         goto out_free_resources;
4169                 }
4170         }
4171
4172         r = mpt2sas_base_map_resources(ioc);
4173         if (r)
4174                 return r;
4175
4176         if (ioc->is_warpdrive) {
4177                 ioc->reply_post_host_index[0] =
4178                     (resource_size_t *)&ioc->chip->ReplyPostHostIndex;
4179
4180                 for (i = 1; i < ioc->cpu_msix_table_sz; i++)
4181                         ioc->reply_post_host_index[i] = (resource_size_t *)
4182                         ((u8 *)&ioc->chip->Doorbell + (0x4000 + ((i - 1)
4183                         * 4)));
4184         }
4185
4186         pci_set_drvdata(ioc->pdev, ioc->shost);
4187         r = _base_get_ioc_facts(ioc, CAN_SLEEP);
4188         if (r)
4189                 goto out_free_resources;
4190
4191         r = _base_make_ioc_ready(ioc, CAN_SLEEP, SOFT_RESET);
4192         if (r)
4193                 goto out_free_resources;
4194
4195         ioc->pfacts = kcalloc(ioc->facts.NumberOfPorts,
4196             sizeof(Mpi2PortFactsReply_t), GFP_KERNEL);
4197         if (!ioc->pfacts) {
4198                 r = -ENOMEM;
4199                 goto out_free_resources;
4200         }
4201
4202         for (i = 0 ; i < ioc->facts.NumberOfPorts; i++) {
4203                 r = _base_get_port_facts(ioc, i, CAN_SLEEP);
4204                 if (r)
4205                         goto out_free_resources;
4206         }
4207
4208         r = _base_allocate_memory_pools(ioc, CAN_SLEEP);
4209         if (r)
4210                 goto out_free_resources;
4211
4212         init_waitqueue_head(&ioc->reset_wq);
4213
4214         /* allocate memory pd handle bitmask list */
4215         ioc->pd_handles_sz = (ioc->facts.MaxDevHandle / 8);
4216         if (ioc->facts.MaxDevHandle % 8)
4217                 ioc->pd_handles_sz++;
4218         ioc->pd_handles = kzalloc(ioc->pd_handles_sz,
4219             GFP_KERNEL);
4220         if (!ioc->pd_handles) {
4221                 r = -ENOMEM;
4222                 goto out_free_resources;
4223         }
4224
4225         ioc->fwfault_debug = mpt2sas_fwfault_debug;
4226
4227         /* base internal command bits */
4228         mutex_init(&ioc->base_cmds.mutex);
4229         ioc->base_cmds.reply = kzalloc(ioc->reply_sz, GFP_KERNEL);
4230         ioc->base_cmds.status = MPT2_CMD_NOT_USED;
4231
4232         /* port_enable command bits */
4233         ioc->port_enable_cmds.reply = kzalloc(ioc->reply_sz, GFP_KERNEL);
4234         ioc->port_enable_cmds.status = MPT2_CMD_NOT_USED;
4235
4236         /* transport internal command bits */
4237         ioc->transport_cmds.reply = kzalloc(ioc->reply_sz, GFP_KERNEL);
4238         ioc->transport_cmds.status = MPT2_CMD_NOT_USED;
4239         mutex_init(&ioc->transport_cmds.mutex);
4240
4241         /* scsih internal command bits */
4242         ioc->scsih_cmds.reply = kzalloc(ioc->reply_sz, GFP_KERNEL);
4243         ioc->scsih_cmds.status = MPT2_CMD_NOT_USED;
4244         mutex_init(&ioc->scsih_cmds.mutex);
4245
4246         /* task management internal command bits */
4247         ioc->tm_cmds.reply = kzalloc(ioc->reply_sz, GFP_KERNEL);
4248         ioc->tm_cmds.status = MPT2_CMD_NOT_USED;
4249         mutex_init(&ioc->tm_cmds.mutex);
4250
4251         /* config page internal command bits */
4252         ioc->config_cmds.reply = kzalloc(ioc->reply_sz, GFP_KERNEL);
4253         ioc->config_cmds.status = MPT2_CMD_NOT_USED;
4254         mutex_init(&ioc->config_cmds.mutex);
4255
4256         /* ctl module internal command bits */
4257         ioc->ctl_cmds.reply = kzalloc(ioc->reply_sz, GFP_KERNEL);
4258         ioc->ctl_cmds.sense = kzalloc(SCSI_SENSE_BUFFERSIZE, GFP_KERNEL);
4259         ioc->ctl_cmds.status = MPT2_CMD_NOT_USED;
4260         mutex_init(&ioc->ctl_cmds.mutex);
4261
4262         if (!ioc->base_cmds.reply || !ioc->transport_cmds.reply ||
4263             !ioc->scsih_cmds.reply || !ioc->tm_cmds.reply ||
4264             !ioc->config_cmds.reply || !ioc->ctl_cmds.reply ||
4265             !ioc->ctl_cmds.sense) {
4266                 r = -ENOMEM;
4267                 goto out_free_resources;
4268         }
4269
4270         if (!ioc->base_cmds.reply || !ioc->transport_cmds.reply ||
4271             !ioc->scsih_cmds.reply || !ioc->tm_cmds.reply ||
4272             !ioc->config_cmds.reply || !ioc->ctl_cmds.reply) {
4273                 r = -ENOMEM;
4274                 goto out_free_resources;
4275         }
4276
4277         for (i = 0; i < MPI2_EVENT_NOTIFY_EVENTMASK_WORDS; i++)
4278                 ioc->event_masks[i] = -1;
4279
4280         /* here we enable the events we care about */
4281         _base_unmask_events(ioc, MPI2_EVENT_SAS_DISCOVERY);
4282         _base_unmask_events(ioc, MPI2_EVENT_SAS_BROADCAST_PRIMITIVE);
4283         _base_unmask_events(ioc, MPI2_EVENT_SAS_TOPOLOGY_CHANGE_LIST);
4284         _base_unmask_events(ioc, MPI2_EVENT_SAS_DEVICE_STATUS_CHANGE);
4285         _base_unmask_events(ioc, MPI2_EVENT_SAS_ENCL_DEVICE_STATUS_CHANGE);
4286         _base_unmask_events(ioc, MPI2_EVENT_IR_CONFIGURATION_CHANGE_LIST);
4287         _base_unmask_events(ioc, MPI2_EVENT_IR_VOLUME);
4288         _base_unmask_events(ioc, MPI2_EVENT_IR_PHYSICAL_DISK);
4289         _base_unmask_events(ioc, MPI2_EVENT_IR_OPERATION_STATUS);
4290         _base_unmask_events(ioc, MPI2_EVENT_LOG_ENTRY_ADDED);
4291         r = _base_make_ioc_operational(ioc, CAN_SLEEP);
4292         if (r)
4293                 goto out_free_resources;
4294
4295         if (missing_delay[0] != -1 && missing_delay[1] != -1)
4296                 _base_update_missing_delay(ioc, missing_delay[0],
4297                     missing_delay[1]);
4298
4299         return 0;
4300
4301  out_free_resources:
4302
4303         ioc->remove_host = 1;
4304         mpt2sas_base_free_resources(ioc);
4305         _base_release_memory_pools(ioc);
4306         pci_set_drvdata(ioc->pdev, NULL);
4307         kfree(ioc->cpu_msix_table);
4308         if (ioc->is_warpdrive)
4309                 kfree(ioc->reply_post_host_index);
4310         kfree(ioc->pd_handles);
4311         kfree(ioc->tm_cmds.reply);
4312         kfree(ioc->transport_cmds.reply);
4313         kfree(ioc->scsih_cmds.reply);
4314         kfree(ioc->config_cmds.reply);
4315         kfree(ioc->base_cmds.reply);
4316         kfree(ioc->port_enable_cmds.reply);
4317         kfree(ioc->ctl_cmds.reply);
4318         kfree(ioc->ctl_cmds.sense);
4319         kfree(ioc->pfacts);
4320         ioc->ctl_cmds.reply = NULL;
4321         ioc->base_cmds.reply = NULL;
4322         ioc->tm_cmds.reply = NULL;
4323         ioc->scsih_cmds.reply = NULL;
4324         ioc->transport_cmds.reply = NULL;
4325         ioc->config_cmds.reply = NULL;
4326         ioc->pfacts = NULL;
4327         return r;
4328 }
4329
4330
4331 /**
4332  * mpt2sas_base_detach - remove controller instance
4333  * @ioc: per adapter object
4334  *
4335  * Return nothing.
4336  */
4337 void
4338 mpt2sas_base_detach(struct MPT2SAS_ADAPTER *ioc)
4339 {
4340
4341         dexitprintk(ioc, printk(MPT2SAS_INFO_FMT "%s\n", ioc->name,
4342             __func__));
4343
4344         mpt2sas_base_stop_watchdog(ioc);
4345         mpt2sas_base_free_resources(ioc);
4346         _base_release_memory_pools(ioc);
4347         pci_set_drvdata(ioc->pdev, NULL);
4348         kfree(ioc->cpu_msix_table);
4349         if (ioc->is_warpdrive)
4350                 kfree(ioc->reply_post_host_index);
4351         kfree(ioc->pd_handles);
4352         kfree(ioc->pfacts);
4353         kfree(ioc->ctl_cmds.reply);
4354         kfree(ioc->ctl_cmds.sense);
4355         kfree(ioc->base_cmds.reply);
4356         kfree(ioc->port_enable_cmds.reply);
4357         kfree(ioc->tm_cmds.reply);
4358         kfree(ioc->transport_cmds.reply);
4359         kfree(ioc->scsih_cmds.reply);
4360         kfree(ioc->config_cmds.reply);
4361 }
4362
4363 /**
4364  * _base_reset_handler - reset callback handler (for base)
4365  * @ioc: per adapter object
4366  * @reset_phase: phase
4367  *
4368  * The handler for doing any required cleanup or initialization.
4369  *
4370  * The reset phase can be MPT2_IOC_PRE_RESET, MPT2_IOC_AFTER_RESET,
4371  * MPT2_IOC_DONE_RESET
4372  *
4373  * Return nothing.
4374  */
4375 static void
4376 _base_reset_handler(struct MPT2SAS_ADAPTER *ioc, int reset_phase)
4377 {
4378         mpt2sas_scsih_reset_handler(ioc, reset_phase);
4379         mpt2sas_ctl_reset_handler(ioc, reset_phase);
4380         switch (reset_phase) {
4381         case MPT2_IOC_PRE_RESET:
4382                 dtmprintk(ioc, printk(MPT2SAS_INFO_FMT "%s: "
4383                     "MPT2_IOC_PRE_RESET\n", ioc->name, __func__));
4384                 break;
4385         case MPT2_IOC_AFTER_RESET:
4386                 dtmprintk(ioc, printk(MPT2SAS_INFO_FMT "%s: "
4387                     "MPT2_IOC_AFTER_RESET\n", ioc->name, __func__));
4388                 if (ioc->transport_cmds.status & MPT2_CMD_PENDING) {
4389                         ioc->transport_cmds.status |= MPT2_CMD_RESET;
4390                         mpt2sas_base_free_smid(ioc, ioc->transport_cmds.smid);
4391                         complete(&ioc->transport_cmds.done);
4392                 }
4393                 if (ioc->base_cmds.status & MPT2_CMD_PENDING) {
4394                         ioc->base_cmds.status |= MPT2_CMD_RESET;
4395                         mpt2sas_base_free_smid(ioc, ioc->base_cmds.smid);
4396                         complete(&ioc->base_cmds.done);
4397                 }
4398                 if (ioc->port_enable_cmds.status & MPT2_CMD_PENDING) {
4399                         ioc->port_enable_failed = 1;
4400                         ioc->port_enable_cmds.status |= MPT2_CMD_RESET;
4401                         mpt2sas_base_free_smid(ioc, ioc->port_enable_cmds.smid);
4402                         if (ioc->is_driver_loading) {
4403                                 ioc->start_scan_failed =
4404                                     MPI2_IOCSTATUS_INTERNAL_ERROR;
4405                                 ioc->start_scan = 0;
4406                                 ioc->port_enable_cmds.status =
4407                                                 MPT2_CMD_NOT_USED;
4408                         } else
4409                                 complete(&ioc->port_enable_cmds.done);
4410
4411                 }
4412                 if (ioc->config_cmds.status & MPT2_CMD_PENDING) {
4413                         ioc->config_cmds.status |= MPT2_CMD_RESET;
4414                         mpt2sas_base_free_smid(ioc, ioc->config_cmds.smid);
4415                         ioc->config_cmds.smid = USHRT_MAX;
4416                         complete(&ioc->config_cmds.done);
4417                 }
4418                 break;
4419         case MPT2_IOC_DONE_RESET:
4420                 dtmprintk(ioc, printk(MPT2SAS_INFO_FMT "%s: "
4421                     "MPT2_IOC_DONE_RESET\n", ioc->name, __func__));
4422                 break;
4423         }
4424 }
4425
4426 /**
4427  * _wait_for_commands_to_complete - reset controller
4428  * @ioc: Pointer to MPT_ADAPTER structure
4429  * @sleep_flag: CAN_SLEEP or NO_SLEEP
4430  *
4431  * This function waiting(3s) for all pending commands to complete
4432  * prior to putting controller in reset.
4433  */
4434 static void
4435 _wait_for_commands_to_complete(struct MPT2SAS_ADAPTER *ioc, int sleep_flag)
4436 {
4437         u32 ioc_state;
4438         unsigned long flags;
4439         u16 i;
4440
4441         ioc->pending_io_count = 0;
4442         if (sleep_flag != CAN_SLEEP)
4443                 return;
4444
4445         ioc_state = mpt2sas_base_get_iocstate(ioc, 0);
4446         if ((ioc_state & MPI2_IOC_STATE_MASK) != MPI2_IOC_STATE_OPERATIONAL)
4447                 return;
4448
4449         /* pending command count */
4450         spin_lock_irqsave(&ioc->scsi_lookup_lock, flags);
4451         for (i = 0; i < ioc->scsiio_depth; i++)
4452                 if (ioc->scsi_lookup[i].cb_idx != 0xFF)
4453                         ioc->pending_io_count++;
4454         spin_unlock_irqrestore(&ioc->scsi_lookup_lock, flags);
4455
4456         if (!ioc->pending_io_count)
4457                 return;
4458
4459         /* wait for pending commands to complete */
4460         wait_event_timeout(ioc->reset_wq, ioc->pending_io_count == 0, 10 * HZ);
4461 }
4462
4463 /**
4464  * mpt2sas_base_hard_reset_handler - reset controller
4465  * @ioc: Pointer to MPT_ADAPTER structure
4466  * @sleep_flag: CAN_SLEEP or NO_SLEEP
4467  * @type: FORCE_BIG_HAMMER or SOFT_RESET
4468  *
4469  * Returns 0 for success, non-zero for failure.
4470  */
4471 int
4472 mpt2sas_base_hard_reset_handler(struct MPT2SAS_ADAPTER *ioc, int sleep_flag,
4473     enum reset_type type)
4474 {
4475         int r;
4476         unsigned long flags;
4477
4478         dtmprintk(ioc, printk(MPT2SAS_INFO_FMT "%s: enter\n", ioc->name,
4479             __func__));
4480
4481         if (ioc->pci_error_recovery) {
4482                 printk(MPT2SAS_ERR_FMT "%s: pci error recovery reset\n",
4483                     ioc->name, __func__);
4484                 r = 0;
4485                 goto out;
4486         }
4487
4488         if (mpt2sas_fwfault_debug)
4489                 mpt2sas_halt_firmware(ioc);
4490
4491         /* TODO - What we really should be doing is pulling
4492          * out all the code associated with NO_SLEEP; its never used.
4493          * That is legacy code from mpt fusion driver, ported over.
4494          * I will leave this BUG_ON here for now till its been resolved.
4495          */
4496         BUG_ON(sleep_flag == NO_SLEEP);
4497
4498         /* wait for an active reset in progress to complete */
4499         if (!mutex_trylock(&ioc->reset_in_progress_mutex)) {
4500                 do {
4501                         ssleep(1);
4502                 } while (ioc->shost_recovery == 1);
4503                 dtmprintk(ioc, printk(MPT2SAS_INFO_FMT "%s: exit\n", ioc->name,
4504                     __func__));
4505                 return ioc->ioc_reset_in_progress_status;
4506         }
4507
4508         spin_lock_irqsave(&ioc->ioc_reset_in_progress_lock, flags);
4509         ioc->shost_recovery = 1;
4510         spin_unlock_irqrestore(&ioc->ioc_reset_in_progress_lock, flags);
4511
4512         _base_reset_handler(ioc, MPT2_IOC_PRE_RESET);
4513         _wait_for_commands_to_complete(ioc, sleep_flag);
4514         _base_mask_interrupts(ioc);
4515         r = _base_make_ioc_ready(ioc, sleep_flag, type);
4516         if (r)
4517                 goto out;
4518         _base_reset_handler(ioc, MPT2_IOC_AFTER_RESET);
4519
4520         /* If this hard reset is called while port enable is active, then
4521          * there is no reason to call make_ioc_operational
4522          */
4523         if (ioc->is_driver_loading && ioc->port_enable_failed) {
4524                 ioc->remove_host = 1;
4525                 r = -EFAULT;
4526                 goto out;
4527         }
4528         r = _base_make_ioc_operational(ioc, sleep_flag);
4529         if (!r)
4530                 _base_reset_handler(ioc, MPT2_IOC_DONE_RESET);
4531  out:
4532         dtmprintk(ioc, printk(MPT2SAS_INFO_FMT "%s: %s\n",
4533             ioc->name, __func__, ((r == 0) ? "SUCCESS" : "FAILED")));
4534
4535         spin_lock_irqsave(&ioc->ioc_reset_in_progress_lock, flags);
4536         ioc->ioc_reset_in_progress_status = r;
4537         ioc->shost_recovery = 0;
4538         spin_unlock_irqrestore(&ioc->ioc_reset_in_progress_lock, flags);
4539         mutex_unlock(&ioc->reset_in_progress_mutex);
4540
4541         dtmprintk(ioc, printk(MPT2SAS_INFO_FMT "%s: exit\n", ioc->name,
4542             __func__));
4543         return r;
4544 }