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