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