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