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