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