[SCSI] mptbase: fix use-after-free's
[sfrench/cifs-2.6.git] / drivers / message / fusion / mptbase.c
1 /*
2  *  linux/drivers/message/fusion/mptbase.c
3  *      This is the Fusion MPT base driver which supports multiple
4  *      (SCSI + LAN) specialized protocol drivers.
5  *      For use with LSI PCI chip/adapter(s)
6  *      running LSI Fusion MPT (Message Passing Technology) firmware.
7  *
8  *  Copyright (c) 1999-2007 LSI Corporation
9  *  (mailto:DL-MPTFusionLinux@lsi.com)
10  *
11  */
12 /*=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=*/
13 /*
14     This program is free software; you can redistribute it and/or modify
15     it under the terms of the GNU General Public License as published by
16     the Free Software Foundation; version 2 of the License.
17
18     This program is distributed in the hope that it will be useful,
19     but WITHOUT ANY WARRANTY; without even the implied warranty of
20     MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
21     GNU General Public License for more details.
22
23     NO WARRANTY
24     THE PROGRAM IS PROVIDED ON AN "AS IS" BASIS, WITHOUT WARRANTIES OR
25     CONDITIONS OF ANY KIND, EITHER EXPRESS OR IMPLIED INCLUDING, WITHOUT
26     LIMITATION, ANY WARRANTIES OR CONDITIONS OF TITLE, NON-INFRINGEMENT,
27     MERCHANTABILITY OR FITNESS FOR A PARTICULAR PURPOSE. Each Recipient is
28     solely responsible for determining the appropriateness of using and
29     distributing the Program and assumes all risks associated with its
30     exercise of rights under this Agreement, including but not limited to
31     the risks and costs of program errors, damage to or loss of data,
32     programs or equipment, and unavailability or interruption of operations.
33
34     DISCLAIMER OF LIABILITY
35     NEITHER RECIPIENT NOR ANY CONTRIBUTORS SHALL HAVE ANY LIABILITY FOR ANY
36     DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
37     DAMAGES (INCLUDING WITHOUT LIMITATION LOST PROFITS), HOWEVER CAUSED AND
38     ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR
39     TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE
40     USE OR DISTRIBUTION OF THE PROGRAM OR THE EXERCISE OF ANY RIGHTS GRANTED
41     HEREUNDER, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGES
42
43     You should have received a copy of the GNU General Public License
44     along with this program; if not, write to the Free Software
45     Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
46 */
47 /*=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=*/
48
49 #include <linux/kernel.h>
50 #include <linux/module.h>
51 #include <linux/errno.h>
52 #include <linux/init.h>
53 #include <linux/slab.h>
54 #include <linux/types.h>
55 #include <linux/pci.h>
56 #include <linux/kdev_t.h>
57 #include <linux/blkdev.h>
58 #include <linux/delay.h>
59 #include <linux/interrupt.h>            /* needed for in_interrupt() proto */
60 #include <linux/dma-mapping.h>
61 #include <asm/io.h>
62 #ifdef CONFIG_MTRR
63 #include <asm/mtrr.h>
64 #endif
65
66 #include "mptbase.h"
67 #include "lsi/mpi_log_fc.h"
68
69 /*=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=*/
70 #define my_NAME         "Fusion MPT base driver"
71 #define my_VERSION      MPT_LINUX_VERSION_COMMON
72 #define MYNAM           "mptbase"
73
74 MODULE_AUTHOR(MODULEAUTHOR);
75 MODULE_DESCRIPTION(my_NAME);
76 MODULE_LICENSE("GPL");
77 MODULE_VERSION(my_VERSION);
78
79 /*
80  *  cmd line parameters
81  */
82 static int mpt_msi_enable;
83 module_param(mpt_msi_enable, int, 0);
84 MODULE_PARM_DESC(mpt_msi_enable, " MSI Support Enable (default=0)");
85
86 static int mpt_channel_mapping;
87 module_param(mpt_channel_mapping, int, 0);
88 MODULE_PARM_DESC(mpt_channel_mapping, " Mapping id's to channels (default=0)");
89
90 static int mpt_debug_level;
91 static int mpt_set_debug_level(const char *val, struct kernel_param *kp);
92 module_param_call(mpt_debug_level, mpt_set_debug_level, param_get_int,
93                   &mpt_debug_level, 0600);
94 MODULE_PARM_DESC(mpt_debug_level, " debug level - refer to mptdebug.h - (default=0)");
95
96 #ifdef MFCNT
97 static int mfcounter = 0;
98 #define PRINT_MF_COUNT 20000
99 #endif
100
101 /*=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=*/
102 /*
103  *  Public data...
104  */
105
106 struct proc_dir_entry *mpt_proc_root_dir;
107
108 #define WHOINIT_UNKNOWN         0xAA
109
110 /*=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=*/
111 /*
112  *  Private data...
113  */
114                                         /* Adapter link list */
115 LIST_HEAD(ioc_list);
116                                         /* Callback lookup table */
117 static MPT_CALLBACK              MptCallbacks[MPT_MAX_PROTOCOL_DRIVERS];
118                                         /* Protocol driver class lookup table */
119 static int                       MptDriverClass[MPT_MAX_PROTOCOL_DRIVERS];
120                                         /* Event handler lookup table */
121 static MPT_EVHANDLER             MptEvHandlers[MPT_MAX_PROTOCOL_DRIVERS];
122                                         /* Reset handler lookup table */
123 static MPT_RESETHANDLER          MptResetHandlers[MPT_MAX_PROTOCOL_DRIVERS];
124 static struct mpt_pci_driver    *MptDeviceDriverHandlers[MPT_MAX_PROTOCOL_DRIVERS];
125
126 static DECLARE_WAIT_QUEUE_HEAD(mpt_waitq);
127
128 /*
129  *  Driver Callback Index's
130  */
131 static u8 mpt_base_index = MPT_MAX_PROTOCOL_DRIVERS;
132 static u8 last_drv_idx;
133
134 /*=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=*/
135 /*
136  *  Forward protos...
137  */
138 static irqreturn_t mpt_interrupt(int irq, void *bus_id);
139 static int      mpt_base_reply(MPT_ADAPTER *ioc, MPT_FRAME_HDR *req, MPT_FRAME_HDR *reply);
140 static int      mpt_handshake_req_reply_wait(MPT_ADAPTER *ioc, int reqBytes,
141                         u32 *req, int replyBytes, u16 *u16reply, int maxwait,
142                         int sleepFlag);
143 static int      mpt_do_ioc_recovery(MPT_ADAPTER *ioc, u32 reason, int sleepFlag);
144 static void     mpt_detect_bound_ports(MPT_ADAPTER *ioc, struct pci_dev *pdev);
145 static void     mpt_adapter_disable(MPT_ADAPTER *ioc);
146 static void     mpt_adapter_dispose(MPT_ADAPTER *ioc);
147
148 static void     MptDisplayIocCapabilities(MPT_ADAPTER *ioc);
149 static int      MakeIocReady(MPT_ADAPTER *ioc, int force, int sleepFlag);
150 static int      GetIocFacts(MPT_ADAPTER *ioc, int sleepFlag, int reason);
151 static int      GetPortFacts(MPT_ADAPTER *ioc, int portnum, int sleepFlag);
152 static int      SendIocInit(MPT_ADAPTER *ioc, int sleepFlag);
153 static int      SendPortEnable(MPT_ADAPTER *ioc, int portnum, int sleepFlag);
154 static int      mpt_do_upload(MPT_ADAPTER *ioc, int sleepFlag);
155 static int      mpt_downloadboot(MPT_ADAPTER *ioc, MpiFwHeader_t *pFwHeader, int sleepFlag);
156 static int      mpt_diag_reset(MPT_ADAPTER *ioc, int ignore, int sleepFlag);
157 static int      KickStart(MPT_ADAPTER *ioc, int ignore, int sleepFlag);
158 static int      SendIocReset(MPT_ADAPTER *ioc, u8 reset_type, int sleepFlag);
159 static int      PrimeIocFifos(MPT_ADAPTER *ioc);
160 static int      WaitForDoorbellAck(MPT_ADAPTER *ioc, int howlong, int sleepFlag);
161 static int      WaitForDoorbellInt(MPT_ADAPTER *ioc, int howlong, int sleepFlag);
162 static int      WaitForDoorbellReply(MPT_ADAPTER *ioc, int howlong, int sleepFlag);
163 static int      GetLanConfigPages(MPT_ADAPTER *ioc);
164 static int      GetIoUnitPage2(MPT_ADAPTER *ioc);
165 int             mptbase_sas_persist_operation(MPT_ADAPTER *ioc, u8 persist_opcode);
166 static int      mpt_GetScsiPortSettings(MPT_ADAPTER *ioc, int portnum);
167 static int      mpt_readScsiDevicePageHeaders(MPT_ADAPTER *ioc, int portnum);
168 static void     mpt_read_ioc_pg_1(MPT_ADAPTER *ioc);
169 static void     mpt_read_ioc_pg_4(MPT_ADAPTER *ioc);
170 static void     mpt_timer_expired(unsigned long data);
171 static void     mpt_get_manufacturing_pg_0(MPT_ADAPTER *ioc);
172 static int      SendEventNotification(MPT_ADAPTER *ioc, u8 EvSwitch);
173 static int      SendEventAck(MPT_ADAPTER *ioc, EventNotificationReply_t *evnp);
174 static int      mpt_host_page_access_control(MPT_ADAPTER *ioc, u8 access_control_value, int sleepFlag);
175 static int      mpt_host_page_alloc(MPT_ADAPTER *ioc, pIOCInit_t ioc_init);
176
177 #ifdef CONFIG_PROC_FS
178 static int      procmpt_summary_read(char *buf, char **start, off_t offset,
179                                 int request, int *eof, void *data);
180 static int      procmpt_version_read(char *buf, char **start, off_t offset,
181                                 int request, int *eof, void *data);
182 static int      procmpt_iocinfo_read(char *buf, char **start, off_t offset,
183                                 int request, int *eof, void *data);
184 #endif
185 static void     mpt_get_fw_exp_ver(char *buf, MPT_ADAPTER *ioc);
186
187 //int           mpt_HardResetHandler(MPT_ADAPTER *ioc, int sleepFlag);
188 static int      ProcessEventNotification(MPT_ADAPTER *ioc, EventNotificationReply_t *evReply, int *evHandlers);
189 static void     mpt_iocstatus_info(MPT_ADAPTER *ioc, u32 ioc_status, MPT_FRAME_HDR *mf);
190 static void     mpt_fc_log_info(MPT_ADAPTER *ioc, u32 log_info);
191 static void     mpt_spi_log_info(MPT_ADAPTER *ioc, u32 log_info);
192 static void     mpt_sas_log_info(MPT_ADAPTER *ioc, u32 log_info);
193 static int      mpt_read_ioc_pg_3(MPT_ADAPTER *ioc);
194 static void     mpt_inactive_raid_list_free(MPT_ADAPTER *ioc);
195
196 /* module entry point */
197 static int  __init    fusion_init  (void);
198 static void __exit    fusion_exit  (void);
199
200 #define CHIPREG_READ32(addr)            readl_relaxed(addr)
201 #define CHIPREG_READ32_dmasync(addr)    readl(addr)
202 #define CHIPREG_WRITE32(addr,val)       writel(val, addr)
203 #define CHIPREG_PIO_WRITE32(addr,val)   outl(val, (unsigned long)addr)
204 #define CHIPREG_PIO_READ32(addr)        inl((unsigned long)addr)
205
206 static void
207 pci_disable_io_access(struct pci_dev *pdev)
208 {
209         u16 command_reg;
210
211         pci_read_config_word(pdev, PCI_COMMAND, &command_reg);
212         command_reg &= ~1;
213         pci_write_config_word(pdev, PCI_COMMAND, command_reg);
214 }
215
216 static void
217 pci_enable_io_access(struct pci_dev *pdev)
218 {
219         u16 command_reg;
220
221         pci_read_config_word(pdev, PCI_COMMAND, &command_reg);
222         command_reg |= 1;
223         pci_write_config_word(pdev, PCI_COMMAND, command_reg);
224 }
225
226 static int mpt_set_debug_level(const char *val, struct kernel_param *kp)
227 {
228         int ret = param_set_int(val, kp);
229         MPT_ADAPTER *ioc;
230
231         if (ret)
232                 return ret;
233
234         list_for_each_entry(ioc, &ioc_list, list)
235                 ioc->debug_level = mpt_debug_level;
236         return 0;
237 }
238
239 /**
240  *      mpt_get_cb_idx - obtain cb_idx for registered driver
241  *      @dclass: class driver enum
242  *
243  *      Returns cb_idx, or zero means it wasn't found
244  **/
245 static u8
246 mpt_get_cb_idx(MPT_DRIVER_CLASS dclass)
247 {
248         u8 cb_idx;
249
250         for (cb_idx = MPT_MAX_PROTOCOL_DRIVERS-1; cb_idx; cb_idx--)
251                 if (MptDriverClass[cb_idx] == dclass)
252                         return cb_idx;
253         return 0;
254 }
255
256 /*
257  *  Process turbo (context) reply...
258  */
259 static void
260 mpt_turbo_reply(MPT_ADAPTER *ioc, u32 pa)
261 {
262         MPT_FRAME_HDR *mf = NULL;
263         MPT_FRAME_HDR *mr = NULL;
264         u16 req_idx = 0;
265         u8 cb_idx;
266
267         dmfprintk(ioc, printk(MYIOC_s_DEBUG_FMT "Got TURBO reply req_idx=%08x\n",
268                                 ioc->name, pa));
269
270         switch (pa >> MPI_CONTEXT_REPLY_TYPE_SHIFT) {
271         case MPI_CONTEXT_REPLY_TYPE_SCSI_INIT:
272                 req_idx = pa & 0x0000FFFF;
273                 cb_idx = (pa & 0x00FF0000) >> 16;
274                 mf = MPT_INDEX_2_MFPTR(ioc, req_idx);
275                 break;
276         case MPI_CONTEXT_REPLY_TYPE_LAN:
277                 cb_idx = mpt_get_cb_idx(MPTLAN_DRIVER);
278                 /*
279                  *  Blind set of mf to NULL here was fatal
280                  *  after lan_reply says "freeme"
281                  *  Fix sort of combined with an optimization here;
282                  *  added explicit check for case where lan_reply
283                  *  was just returning 1 and doing nothing else.
284                  *  For this case skip the callback, but set up
285                  *  proper mf value first here:-)
286                  */
287                 if ((pa & 0x58000000) == 0x58000000) {
288                         req_idx = pa & 0x0000FFFF;
289                         mf = MPT_INDEX_2_MFPTR(ioc, req_idx);
290                         mpt_free_msg_frame(ioc, mf);
291                         mb();
292                         return;
293                         break;
294                 }
295                 mr = (MPT_FRAME_HDR *) CAST_U32_TO_PTR(pa);
296                 break;
297         case MPI_CONTEXT_REPLY_TYPE_SCSI_TARGET:
298                 cb_idx = mpt_get_cb_idx(MPTSTM_DRIVER);
299                 mr = (MPT_FRAME_HDR *) CAST_U32_TO_PTR(pa);
300                 break;
301         default:
302                 cb_idx = 0;
303                 BUG();
304         }
305
306         /*  Check for (valid) IO callback!  */
307         if (!cb_idx || cb_idx >= MPT_MAX_PROTOCOL_DRIVERS ||
308                 MptCallbacks[cb_idx] == NULL) {
309                 printk(MYIOC_s_WARN_FMT "%s: Invalid cb_idx (%d)!\n",
310                                 __FUNCTION__, ioc->name, cb_idx);
311                 goto out;
312         }
313
314         if (MptCallbacks[cb_idx](ioc, mf, mr))
315                 mpt_free_msg_frame(ioc, mf);
316  out:
317         mb();
318 }
319
320 static void
321 mpt_reply(MPT_ADAPTER *ioc, u32 pa)
322 {
323         MPT_FRAME_HDR   *mf;
324         MPT_FRAME_HDR   *mr;
325         u16              req_idx;
326         u8               cb_idx;
327         int              freeme;
328
329         u32 reply_dma_low;
330         u16 ioc_stat;
331
332         /* non-TURBO reply!  Hmmm, something may be up...
333          *  Newest turbo reply mechanism; get address
334          *  via left shift 1 (get rid of MPI_ADDRESS_REPLY_A_BIT)!
335          */
336
337         /* Map DMA address of reply header to cpu address.
338          * pa is 32 bits - but the dma address may be 32 or 64 bits
339          * get offset based only only the low addresses
340          */
341
342         reply_dma_low = (pa <<= 1);
343         mr = (MPT_FRAME_HDR *)((u8 *)ioc->reply_frames +
344                          (reply_dma_low - ioc->reply_frames_low_dma));
345
346         req_idx = le16_to_cpu(mr->u.frame.hwhdr.msgctxu.fld.req_idx);
347         cb_idx = mr->u.frame.hwhdr.msgctxu.fld.cb_idx;
348         mf = MPT_INDEX_2_MFPTR(ioc, req_idx);
349
350         dmfprintk(ioc, printk(MYIOC_s_DEBUG_FMT "Got non-TURBO reply=%p req_idx=%x cb_idx=%x Function=%x\n",
351                         ioc->name, mr, req_idx, cb_idx, mr->u.hdr.Function));
352         DBG_DUMP_REPLY_FRAME(ioc, (u32 *)mr);
353
354          /*  Check/log IOC log info
355          */
356         ioc_stat = le16_to_cpu(mr->u.reply.IOCStatus);
357         if (ioc_stat & MPI_IOCSTATUS_FLAG_LOG_INFO_AVAILABLE) {
358                 u32      log_info = le32_to_cpu(mr->u.reply.IOCLogInfo);
359                 if (ioc->bus_type == FC)
360                         mpt_fc_log_info(ioc, log_info);
361                 else if (ioc->bus_type == SPI)
362                         mpt_spi_log_info(ioc, log_info);
363                 else if (ioc->bus_type == SAS)
364                         mpt_sas_log_info(ioc, log_info);
365         }
366
367         if (ioc_stat & MPI_IOCSTATUS_MASK)
368                 mpt_iocstatus_info(ioc, (u32)ioc_stat, mf);
369
370         /*  Check for (valid) IO callback!  */
371         if (!cb_idx || cb_idx >= MPT_MAX_PROTOCOL_DRIVERS ||
372                 MptCallbacks[cb_idx] == NULL) {
373                 printk(MYIOC_s_WARN_FMT "%s: Invalid cb_idx (%d)!\n",
374                                 __FUNCTION__, ioc->name, cb_idx);
375                 freeme = 0;
376                 goto out;
377         }
378
379         freeme = MptCallbacks[cb_idx](ioc, mf, mr);
380
381  out:
382         /*  Flush (non-TURBO) reply with a WRITE!  */
383         CHIPREG_WRITE32(&ioc->chip->ReplyFifo, pa);
384
385         if (freeme)
386                 mpt_free_msg_frame(ioc, mf);
387         mb();
388 }
389
390 /*=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=*/
391 /**
392  *      mpt_interrupt - MPT adapter (IOC) specific interrupt handler.
393  *      @irq: irq number (not used)
394  *      @bus_id: bus identifier cookie == pointer to MPT_ADAPTER structure
395  *
396  *      This routine is registered via the request_irq() kernel API call,
397  *      and handles all interrupts generated from a specific MPT adapter
398  *      (also referred to as a IO Controller or IOC).
399  *      This routine must clear the interrupt from the adapter and does
400  *      so by reading the reply FIFO.  Multiple replies may be processed
401  *      per single call to this routine.
402  *
403  *      This routine handles register-level access of the adapter but
404  *      dispatches (calls) a protocol-specific callback routine to handle
405  *      the protocol-specific details of the MPT request completion.
406  */
407 static irqreturn_t
408 mpt_interrupt(int irq, void *bus_id)
409 {
410         MPT_ADAPTER *ioc = bus_id;
411         u32 pa = CHIPREG_READ32_dmasync(&ioc->chip->ReplyFifo);
412
413         if (pa == 0xFFFFFFFF)
414                 return IRQ_NONE;
415
416         /*
417          *  Drain the reply FIFO!
418          */
419         do {
420                 if (pa & MPI_ADDRESS_REPLY_A_BIT)
421                         mpt_reply(ioc, pa);
422                 else
423                         mpt_turbo_reply(ioc, pa);
424                 pa = CHIPREG_READ32_dmasync(&ioc->chip->ReplyFifo);
425         } while (pa != 0xFFFFFFFF);
426
427         return IRQ_HANDLED;
428 }
429
430 /*=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=*/
431 /**
432  *      mpt_base_reply - MPT base driver's callback routine
433  *      @ioc: Pointer to MPT_ADAPTER structure
434  *      @mf: Pointer to original MPT request frame
435  *      @reply: Pointer to MPT reply frame (NULL if TurboReply)
436  *
437  *      MPT base driver's callback routine; all base driver
438  *      "internal" request/reply processing is routed here.
439  *      Currently used for EventNotification and EventAck handling.
440  *
441  *      Returns 1 indicating original alloc'd request frame ptr
442  *      should be freed, or 0 if it shouldn't.
443  */
444 static int
445 mpt_base_reply(MPT_ADAPTER *ioc, MPT_FRAME_HDR *mf, MPT_FRAME_HDR *reply)
446 {
447         int freereq = 1;
448         u8 func;
449
450         dmfprintk(ioc, printk(MYIOC_s_DEBUG_FMT "mpt_base_reply() called\n", ioc->name));
451 #ifdef CONFIG_FUSION_LOGGING
452         if ((ioc->debug_level & MPT_DEBUG_MSG_FRAME) &&
453                         !(reply->u.hdr.MsgFlags & MPI_MSGFLAGS_CONTINUATION_REPLY)) {
454                 dmfprintk(ioc, printk(MYIOC_s_INFO_FMT ": Original request frame (@%p) header\n",
455                     ioc->name, mf));
456                 DBG_DUMP_REQUEST_FRAME_HDR(ioc, (u32 *)mf);
457         }
458 #endif
459
460         func = reply->u.hdr.Function;
461         dmfprintk(ioc, printk(MYIOC_s_DEBUG_FMT "mpt_base_reply, Function=%02Xh\n",
462                         ioc->name, func));
463
464         if (func == MPI_FUNCTION_EVENT_NOTIFICATION) {
465                 EventNotificationReply_t *pEvReply = (EventNotificationReply_t *) reply;
466                 int evHandlers = 0;
467                 int results;
468
469                 results = ProcessEventNotification(ioc, pEvReply, &evHandlers);
470                 if (results != evHandlers) {
471                         /* CHECKME! Any special handling needed here? */
472                         devtverboseprintk(ioc, printk(MYIOC_s_WARN_FMT "Called %d event handlers, sum results = %d\n",
473                                         ioc->name, evHandlers, results));
474                 }
475
476                 /*
477                  *      Hmmm...  It seems that EventNotificationReply is an exception
478                  *      to the rule of one reply per request.
479                  */
480                 if (pEvReply->MsgFlags & MPI_MSGFLAGS_CONTINUATION_REPLY) {
481                         freereq = 0;
482                 } else {
483                         devtverboseprintk(ioc, printk(MYIOC_s_WARN_FMT "EVENT_NOTIFICATION reply %p returns Request frame\n",
484                                 ioc->name, pEvReply));
485                 }
486
487 #ifdef CONFIG_PROC_FS
488 //              LogEvent(ioc, pEvReply);
489 #endif
490
491         } else if (func == MPI_FUNCTION_EVENT_ACK) {
492                 dprintk(ioc, printk(MYIOC_s_DEBUG_FMT "mpt_base_reply, EventAck reply received\n",
493                                 ioc->name));
494         } else if (func == MPI_FUNCTION_CONFIG) {
495                 CONFIGPARMS *pCfg;
496                 unsigned long flags;
497
498                 dcprintk(ioc, printk(MYIOC_s_DEBUG_FMT "config_complete (mf=%p,mr=%p)\n",
499                                 ioc->name, mf, reply));
500
501                 pCfg = * ((CONFIGPARMS **)((u8 *) mf + ioc->req_sz - sizeof(void *)));
502
503                 if (pCfg) {
504                         /* disable timer and remove from linked list */
505                         del_timer(&pCfg->timer);
506
507                         spin_lock_irqsave(&ioc->FreeQlock, flags);
508                         list_del(&pCfg->linkage);
509                         spin_unlock_irqrestore(&ioc->FreeQlock, flags);
510
511                         /*
512                          *      If IOC Status is SUCCESS, save the header
513                          *      and set the status code to GOOD.
514                          */
515                         pCfg->status = MPT_CONFIG_ERROR;
516                         if (reply) {
517                                 ConfigReply_t   *pReply = (ConfigReply_t *)reply;
518                                 u16              status;
519
520                                 status = le16_to_cpu(pReply->IOCStatus) & MPI_IOCSTATUS_MASK;
521                                 dcprintk(ioc, printk(MYIOC_s_NOTE_FMT "  IOCStatus=%04xh, IOCLogInfo=%08xh\n",
522                                      ioc->name, status, le32_to_cpu(pReply->IOCLogInfo)));
523
524                                 pCfg->status = status;
525                                 if (status == MPI_IOCSTATUS_SUCCESS) {
526                                         if ((pReply->Header.PageType &
527                                             MPI_CONFIG_PAGETYPE_MASK) ==
528                                             MPI_CONFIG_PAGETYPE_EXTENDED) {
529                                                 pCfg->cfghdr.ehdr->ExtPageLength =
530                                                     le16_to_cpu(pReply->ExtPageLength);
531                                                 pCfg->cfghdr.ehdr->ExtPageType =
532                                                     pReply->ExtPageType;
533                                         }
534                                         pCfg->cfghdr.hdr->PageVersion = pReply->Header.PageVersion;
535
536                                         /* If this is a regular header, save PageLength. */
537                                         /* LMP Do this better so not using a reserved field! */
538                                         pCfg->cfghdr.hdr->PageLength = pReply->Header.PageLength;
539                                         pCfg->cfghdr.hdr->PageNumber = pReply->Header.PageNumber;
540                                         pCfg->cfghdr.hdr->PageType = pReply->Header.PageType;
541                                 }
542                         }
543
544                         /*
545                          *      Wake up the original calling thread
546                          */
547                         pCfg->wait_done = 1;
548                         wake_up(&mpt_waitq);
549                 }
550         } else if (func == MPI_FUNCTION_SAS_IO_UNIT_CONTROL) {
551                 /* we should be always getting a reply frame */
552                 memcpy(ioc->persist_reply_frame, reply,
553                     min(MPT_DEFAULT_FRAME_SIZE,
554                     4*reply->u.reply.MsgLength));
555                 del_timer(&ioc->persist_timer);
556                 ioc->persist_wait_done = 1;
557                 wake_up(&mpt_waitq);
558         } else {
559                 printk(MYIOC_s_ERR_FMT "Unexpected msg function (=%02Xh) reply received!\n",
560                                 ioc->name, func);
561         }
562
563         /*
564          *      Conditionally tell caller to free the original
565          *      EventNotification/EventAck/unexpected request frame!
566          */
567         return freereq;
568 }
569
570 /*=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=*/
571 /**
572  *      mpt_register - Register protocol-specific main callback handler.
573  *      @cbfunc: callback function pointer
574  *      @dclass: Protocol driver's class (%MPT_DRIVER_CLASS enum value)
575  *
576  *      This routine is called by a protocol-specific driver (SCSI host,
577  *      LAN, SCSI target) to register its reply callback routine.  Each
578  *      protocol-specific driver must do this before it will be able to
579  *      use any IOC resources, such as obtaining request frames.
580  *
581  *      NOTES: The SCSI protocol driver currently calls this routine thrice
582  *      in order to register separate callbacks; one for "normal" SCSI IO;
583  *      one for MptScsiTaskMgmt requests; one for Scan/DV requests.
584  *
585  *      Returns u8 valued "handle" in the range (and S.O.D. order)
586  *      {N,...,7,6,5,...,1} if successful.
587  *      A return value of MPT_MAX_PROTOCOL_DRIVERS (including zero!) should be
588  *      considered an error by the caller.
589  */
590 u8
591 mpt_register(MPT_CALLBACK cbfunc, MPT_DRIVER_CLASS dclass)
592 {
593         u8 cb_idx;
594         last_drv_idx = MPT_MAX_PROTOCOL_DRIVERS;
595
596         /*
597          *  Search for empty callback slot in this order: {N,...,7,6,5,...,1}
598          *  (slot/handle 0 is reserved!)
599          */
600         for (cb_idx = MPT_MAX_PROTOCOL_DRIVERS-1; cb_idx; cb_idx--) {
601                 if (MptCallbacks[cb_idx] == NULL) {
602                         MptCallbacks[cb_idx] = cbfunc;
603                         MptDriverClass[cb_idx] = dclass;
604                         MptEvHandlers[cb_idx] = NULL;
605                         last_drv_idx = cb_idx;
606                         break;
607                 }
608         }
609
610         return last_drv_idx;
611 }
612
613 /*=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=*/
614 /**
615  *      mpt_deregister - Deregister a protocol drivers resources.
616  *      @cb_idx: previously registered callback handle
617  *
618  *      Each protocol-specific driver should call this routine when its
619  *      module is unloaded.
620  */
621 void
622 mpt_deregister(u8 cb_idx)
623 {
624         if (cb_idx && (cb_idx < MPT_MAX_PROTOCOL_DRIVERS)) {
625                 MptCallbacks[cb_idx] = NULL;
626                 MptDriverClass[cb_idx] = MPTUNKNOWN_DRIVER;
627                 MptEvHandlers[cb_idx] = NULL;
628
629                 last_drv_idx++;
630         }
631 }
632
633 /*=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=*/
634 /**
635  *      mpt_event_register - Register protocol-specific event callback
636  *      handler.
637  *      @cb_idx: previously registered (via mpt_register) callback handle
638  *      @ev_cbfunc: callback function
639  *
640  *      This routine can be called by one or more protocol-specific drivers
641  *      if/when they choose to be notified of MPT events.
642  *
643  *      Returns 0 for success.
644  */
645 int
646 mpt_event_register(u8 cb_idx, MPT_EVHANDLER ev_cbfunc)
647 {
648         if (!cb_idx || cb_idx >= MPT_MAX_PROTOCOL_DRIVERS)
649                 return -1;
650
651         MptEvHandlers[cb_idx] = ev_cbfunc;
652         return 0;
653 }
654
655 /*=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=*/
656 /**
657  *      mpt_event_deregister - Deregister protocol-specific event callback
658  *      handler.
659  *      @cb_idx: previously registered callback handle
660  *
661  *      Each protocol-specific driver should call this routine
662  *      when it does not (or can no longer) handle events,
663  *      or when its module is unloaded.
664  */
665 void
666 mpt_event_deregister(u8 cb_idx)
667 {
668         if (!cb_idx || cb_idx >= MPT_MAX_PROTOCOL_DRIVERS)
669                 return;
670
671         MptEvHandlers[cb_idx] = NULL;
672 }
673
674 /*=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=*/
675 /**
676  *      mpt_reset_register - Register protocol-specific IOC reset handler.
677  *      @cb_idx: previously registered (via mpt_register) callback handle
678  *      @reset_func: reset function
679  *
680  *      This routine can be called by one or more protocol-specific drivers
681  *      if/when they choose to be notified of IOC resets.
682  *
683  *      Returns 0 for success.
684  */
685 int
686 mpt_reset_register(u8 cb_idx, MPT_RESETHANDLER reset_func)
687 {
688         if (!cb_idx || cb_idx >= MPT_MAX_PROTOCOL_DRIVERS)
689                 return -1;
690
691         MptResetHandlers[cb_idx] = reset_func;
692         return 0;
693 }
694
695 /*=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=*/
696 /**
697  *      mpt_reset_deregister - Deregister protocol-specific IOC reset handler.
698  *      @cb_idx: previously registered callback handle
699  *
700  *      Each protocol-specific driver should call this routine
701  *      when it does not (or can no longer) handle IOC reset handling,
702  *      or when its module is unloaded.
703  */
704 void
705 mpt_reset_deregister(u8 cb_idx)
706 {
707         if (!cb_idx || cb_idx >= MPT_MAX_PROTOCOL_DRIVERS)
708                 return;
709
710         MptResetHandlers[cb_idx] = NULL;
711 }
712
713 /*=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=*/
714 /**
715  *      mpt_device_driver_register - Register device driver hooks
716  *      @dd_cbfunc: driver callbacks struct
717  *      @cb_idx: MPT protocol driver index
718  */
719 int
720 mpt_device_driver_register(struct mpt_pci_driver * dd_cbfunc, u8 cb_idx)
721 {
722         MPT_ADAPTER     *ioc;
723         const struct pci_device_id *id;
724
725         if (!cb_idx || cb_idx >= MPT_MAX_PROTOCOL_DRIVERS)
726                 return -EINVAL;
727
728         MptDeviceDriverHandlers[cb_idx] = dd_cbfunc;
729
730         /* call per pci device probe entry point */
731         list_for_each_entry(ioc, &ioc_list, list) {
732                 id = ioc->pcidev->driver ?
733                     ioc->pcidev->driver->id_table : NULL;
734                 if (dd_cbfunc->probe)
735                         dd_cbfunc->probe(ioc->pcidev, id);
736          }
737
738         return 0;
739 }
740
741 /*=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=*/
742 /**
743  *      mpt_device_driver_deregister - DeRegister device driver hooks
744  *      @cb_idx: MPT protocol driver index
745  */
746 void
747 mpt_device_driver_deregister(u8 cb_idx)
748 {
749         struct mpt_pci_driver *dd_cbfunc;
750         MPT_ADAPTER     *ioc;
751
752         if (!cb_idx || cb_idx >= MPT_MAX_PROTOCOL_DRIVERS)
753                 return;
754
755         dd_cbfunc = MptDeviceDriverHandlers[cb_idx];
756
757         list_for_each_entry(ioc, &ioc_list, list) {
758                 if (dd_cbfunc->remove)
759                         dd_cbfunc->remove(ioc->pcidev);
760         }
761
762         MptDeviceDriverHandlers[cb_idx] = NULL;
763 }
764
765
766 /*=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=*/
767 /**
768  *      mpt_get_msg_frame - Obtain a MPT request frame from the pool (of 1024)
769  *      allocated per MPT adapter.
770  *      @cb_idx: Handle of registered MPT protocol driver
771  *      @ioc: Pointer to MPT adapter structure
772  *
773  *      Returns pointer to a MPT request frame or %NULL if none are available
774  *      or IOC is not active.
775  */
776 MPT_FRAME_HDR*
777 mpt_get_msg_frame(u8 cb_idx, MPT_ADAPTER *ioc)
778 {
779         MPT_FRAME_HDR *mf;
780         unsigned long flags;
781         u16      req_idx;       /* Request index */
782
783         /* validate handle and ioc identifier */
784
785 #ifdef MFCNT
786         if (!ioc->active)
787                 printk(MYIOC_s_WARN_FMT "IOC Not Active! mpt_get_msg_frame "
788                     "returning NULL!\n", ioc->name);
789 #endif
790
791         /* If interrupts are not attached, do not return a request frame */
792         if (!ioc->active)
793                 return NULL;
794
795         spin_lock_irqsave(&ioc->FreeQlock, flags);
796         if (!list_empty(&ioc->FreeQ)) {
797                 int req_offset;
798
799                 mf = list_entry(ioc->FreeQ.next, MPT_FRAME_HDR,
800                                 u.frame.linkage.list);
801                 list_del(&mf->u.frame.linkage.list);
802                 mf->u.frame.linkage.arg1 = 0;
803                 mf->u.frame.hwhdr.msgctxu.fld.cb_idx = cb_idx;  /* byte */
804                 req_offset = (u8 *)mf - (u8 *)ioc->req_frames;
805                                                                 /* u16! */
806                 req_idx = req_offset / ioc->req_sz;
807                 mf->u.frame.hwhdr.msgctxu.fld.req_idx = cpu_to_le16(req_idx);
808                 mf->u.frame.hwhdr.msgctxu.fld.rsvd = 0;
809                 /* Default, will be changed if necessary in SG generation */
810                 ioc->RequestNB[req_idx] = ioc->NB_for_64_byte_frame;
811 #ifdef MFCNT
812                 ioc->mfcnt++;
813 #endif
814         }
815         else
816                 mf = NULL;
817         spin_unlock_irqrestore(&ioc->FreeQlock, flags);
818
819 #ifdef MFCNT
820         if (mf == NULL)
821                 printk(MYIOC_s_WARN_FMT "IOC Active. No free Msg Frames! "
822                     "Count 0x%x Max 0x%x\n", ioc->name, ioc->mfcnt,
823                     ioc->req_depth);
824         mfcounter++;
825         if (mfcounter == PRINT_MF_COUNT)
826                 printk(MYIOC_s_INFO_FMT "MF Count 0x%x Max 0x%x \n", ioc->name,
827                     ioc->mfcnt, ioc->req_depth);
828 #endif
829
830         dmfprintk(ioc, printk(MYIOC_s_DEBUG_FMT "mpt_get_msg_frame(%d,%d), got mf=%p\n",
831             ioc->name, cb_idx, ioc->id, mf));
832         return mf;
833 }
834
835 /*=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=*/
836 /**
837  *      mpt_put_msg_frame - Send a protocol specific MPT request frame
838  *      to a IOC.
839  *      @cb_idx: Handle of registered MPT protocol driver
840  *      @ioc: Pointer to MPT adapter structure
841  *      @mf: Pointer to MPT request frame
842  *
843  *      This routine posts a MPT request frame to the request post FIFO of a
844  *      specific MPT adapter.
845  */
846 void
847 mpt_put_msg_frame(u8 cb_idx, MPT_ADAPTER *ioc, MPT_FRAME_HDR *mf)
848 {
849         u32 mf_dma_addr;
850         int req_offset;
851         u16      req_idx;       /* Request index */
852
853         /* ensure values are reset properly! */
854         mf->u.frame.hwhdr.msgctxu.fld.cb_idx = cb_idx;          /* byte */
855         req_offset = (u8 *)mf - (u8 *)ioc->req_frames;
856                                                                 /* u16! */
857         req_idx = req_offset / ioc->req_sz;
858         mf->u.frame.hwhdr.msgctxu.fld.req_idx = cpu_to_le16(req_idx);
859         mf->u.frame.hwhdr.msgctxu.fld.rsvd = 0;
860
861         DBG_DUMP_PUT_MSG_FRAME(ioc, (u32 *)mf);
862
863         mf_dma_addr = (ioc->req_frames_low_dma + req_offset) | ioc->RequestNB[req_idx];
864         dsgprintk(ioc, printk(MYIOC_s_DEBUG_FMT "mf_dma_addr=%x req_idx=%d "
865             "RequestNB=%x\n", ioc->name, mf_dma_addr, req_idx,
866             ioc->RequestNB[req_idx]));
867         CHIPREG_WRITE32(&ioc->chip->RequestFifo, mf_dma_addr);
868 }
869
870 /**
871  *      mpt_put_msg_frame_hi_pri - Send a protocol specific MPT request frame
872  *      to a IOC using hi priority request queue.
873  *      @cb_idx: Handle of registered MPT protocol driver
874  *      @ioc: Pointer to MPT adapter structure
875  *      @mf: Pointer to MPT request frame
876  *
877  *      This routine posts a MPT request frame to the request post FIFO of a
878  *      specific MPT adapter.
879  **/
880 void
881 mpt_put_msg_frame_hi_pri(u8 cb_idx, MPT_ADAPTER *ioc, MPT_FRAME_HDR *mf)
882 {
883         u32 mf_dma_addr;
884         int req_offset;
885         u16      req_idx;       /* Request index */
886
887         /* ensure values are reset properly! */
888         mf->u.frame.hwhdr.msgctxu.fld.cb_idx = cb_idx;
889         req_offset = (u8 *)mf - (u8 *)ioc->req_frames;
890         req_idx = req_offset / ioc->req_sz;
891         mf->u.frame.hwhdr.msgctxu.fld.req_idx = cpu_to_le16(req_idx);
892         mf->u.frame.hwhdr.msgctxu.fld.rsvd = 0;
893
894         DBG_DUMP_PUT_MSG_FRAME(ioc, (u32 *)mf);
895
896         mf_dma_addr = (ioc->req_frames_low_dma + req_offset);
897         dsgprintk(ioc, printk(MYIOC_s_DEBUG_FMT "mf_dma_addr=%x req_idx=%d\n",
898                 ioc->name, mf_dma_addr, req_idx));
899         CHIPREG_WRITE32(&ioc->chip->RequestHiPriFifo, mf_dma_addr);
900 }
901
902 /*=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=*/
903 /**
904  *      mpt_free_msg_frame - Place MPT request frame back on FreeQ.
905  *      @handle: Handle of registered MPT protocol driver
906  *      @ioc: Pointer to MPT adapter structure
907  *      @mf: Pointer to MPT request frame
908  *
909  *      This routine places a MPT request frame back on the MPT adapter's
910  *      FreeQ.
911  */
912 void
913 mpt_free_msg_frame(MPT_ADAPTER *ioc, MPT_FRAME_HDR *mf)
914 {
915         unsigned long flags;
916
917         /*  Put Request back on FreeQ!  */
918         spin_lock_irqsave(&ioc->FreeQlock, flags);
919         mf->u.frame.linkage.arg1 = 0xdeadbeaf; /* signature to know if this mf is freed */
920         list_add_tail(&mf->u.frame.linkage.list, &ioc->FreeQ);
921 #ifdef MFCNT
922         ioc->mfcnt--;
923 #endif
924         spin_unlock_irqrestore(&ioc->FreeQlock, flags);
925 }
926
927 /*=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=*/
928 /**
929  *      mpt_add_sge - Place a simple SGE at address pAddr.
930  *      @pAddr: virtual address for SGE
931  *      @flagslength: SGE flags and data transfer length
932  *      @dma_addr: Physical address
933  *
934  *      This routine places a MPT request frame back on the MPT adapter's
935  *      FreeQ.
936  */
937 void
938 mpt_add_sge(char *pAddr, u32 flagslength, dma_addr_t dma_addr)
939 {
940         if (sizeof(dma_addr_t) == sizeof(u64)) {
941                 SGESimple64_t *pSge = (SGESimple64_t *) pAddr;
942                 u32 tmp = dma_addr & 0xFFFFFFFF;
943
944                 pSge->FlagsLength = cpu_to_le32(flagslength);
945                 pSge->Address.Low = cpu_to_le32(tmp);
946                 tmp = (u32) ((u64)dma_addr >> 32);
947                 pSge->Address.High = cpu_to_le32(tmp);
948
949         } else {
950                 SGESimple32_t *pSge = (SGESimple32_t *) pAddr;
951                 pSge->FlagsLength = cpu_to_le32(flagslength);
952                 pSge->Address = cpu_to_le32(dma_addr);
953         }
954 }
955
956 /*=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=*/
957 /**
958  *      mpt_send_handshake_request - Send MPT request via doorbell handshake method.
959  *      @cb_idx: Handle of registered MPT protocol driver
960  *      @ioc: Pointer to MPT adapter structure
961  *      @reqBytes: Size of the request in bytes
962  *      @req: Pointer to MPT request frame
963  *      @sleepFlag: Use schedule if CAN_SLEEP else use udelay.
964  *
965  *      This routine is used exclusively to send MptScsiTaskMgmt
966  *      requests since they are required to be sent via doorbell handshake.
967  *
968  *      NOTE: It is the callers responsibility to byte-swap fields in the
969  *      request which are greater than 1 byte in size.
970  *
971  *      Returns 0 for success, non-zero for failure.
972  */
973 int
974 mpt_send_handshake_request(u8 cb_idx, MPT_ADAPTER *ioc, int reqBytes, u32 *req, int sleepFlag)
975 {
976         int     r = 0;
977         u8      *req_as_bytes;
978         int      ii;
979
980         /* State is known to be good upon entering
981          * this function so issue the bus reset
982          * request.
983          */
984
985         /*
986          * Emulate what mpt_put_msg_frame() does /wrt to sanity
987          * setting cb_idx/req_idx.  But ONLY if this request
988          * is in proper (pre-alloc'd) request buffer range...
989          */
990         ii = MFPTR_2_MPT_INDEX(ioc,(MPT_FRAME_HDR*)req);
991         if (reqBytes >= 12 && ii >= 0 && ii < ioc->req_depth) {
992                 MPT_FRAME_HDR *mf = (MPT_FRAME_HDR*)req;
993                 mf->u.frame.hwhdr.msgctxu.fld.req_idx = cpu_to_le16(ii);
994                 mf->u.frame.hwhdr.msgctxu.fld.cb_idx = cb_idx;
995         }
996
997         /* Make sure there are no doorbells */
998         CHIPREG_WRITE32(&ioc->chip->IntStatus, 0);
999
1000         CHIPREG_WRITE32(&ioc->chip->Doorbell,
1001                         ((MPI_FUNCTION_HANDSHAKE<<MPI_DOORBELL_FUNCTION_SHIFT) |
1002                          ((reqBytes/4)<<MPI_DOORBELL_ADD_DWORDS_SHIFT)));
1003
1004         /* Wait for IOC doorbell int */
1005         if ((ii = WaitForDoorbellInt(ioc, 5, sleepFlag)) < 0) {
1006                 return ii;
1007         }
1008
1009         /* Read doorbell and check for active bit */
1010         if (!(CHIPREG_READ32(&ioc->chip->Doorbell) & MPI_DOORBELL_ACTIVE))
1011                 return -5;
1012
1013         dhsprintk(ioc, printk(MYIOC_s_DEBUG_FMT "mpt_send_handshake_request start, WaitCnt=%d\n",
1014                 ioc->name, ii));
1015
1016         CHIPREG_WRITE32(&ioc->chip->IntStatus, 0);
1017
1018         if ((r = WaitForDoorbellAck(ioc, 5, sleepFlag)) < 0) {
1019                 return -2;
1020         }
1021
1022         /* Send request via doorbell handshake */
1023         req_as_bytes = (u8 *) req;
1024         for (ii = 0; ii < reqBytes/4; ii++) {
1025                 u32 word;
1026
1027                 word = ((req_as_bytes[(ii*4) + 0] <<  0) |
1028                         (req_as_bytes[(ii*4) + 1] <<  8) |
1029                         (req_as_bytes[(ii*4) + 2] << 16) |
1030                         (req_as_bytes[(ii*4) + 3] << 24));
1031                 CHIPREG_WRITE32(&ioc->chip->Doorbell, word);
1032                 if ((r = WaitForDoorbellAck(ioc, 5, sleepFlag)) < 0) {
1033                         r = -3;
1034                         break;
1035                 }
1036         }
1037
1038         if (r >= 0 && WaitForDoorbellInt(ioc, 10, sleepFlag) >= 0)
1039                 r = 0;
1040         else
1041                 r = -4;
1042
1043         /* Make sure there are no doorbells */
1044         CHIPREG_WRITE32(&ioc->chip->IntStatus, 0);
1045
1046         return r;
1047 }
1048
1049 /*=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=*/
1050 /**
1051  * mpt_host_page_access_control - control the IOC's Host Page Buffer access
1052  * @ioc: Pointer to MPT adapter structure
1053  * @access_control_value: define bits below
1054  * @sleepFlag: Specifies whether the process can sleep
1055  *
1056  * Provides mechanism for the host driver to control the IOC's
1057  * Host Page Buffer access.
1058  *
1059  * Access Control Value - bits[15:12]
1060  * 0h Reserved
1061  * 1h Enable Access { MPI_DB_HPBAC_ENABLE_ACCESS }
1062  * 2h Disable Access { MPI_DB_HPBAC_DISABLE_ACCESS }
1063  * 3h Free Buffer { MPI_DB_HPBAC_FREE_BUFFER }
1064  *
1065  * Returns 0 for success, non-zero for failure.
1066  */
1067
1068 static int
1069 mpt_host_page_access_control(MPT_ADAPTER *ioc, u8 access_control_value, int sleepFlag)
1070 {
1071         int      r = 0;
1072
1073         /* return if in use */
1074         if (CHIPREG_READ32(&ioc->chip->Doorbell)
1075             & MPI_DOORBELL_ACTIVE)
1076             return -1;
1077
1078         CHIPREG_WRITE32(&ioc->chip->IntStatus, 0);
1079
1080         CHIPREG_WRITE32(&ioc->chip->Doorbell,
1081                 ((MPI_FUNCTION_HOST_PAGEBUF_ACCESS_CONTROL
1082                  <<MPI_DOORBELL_FUNCTION_SHIFT) |
1083                  (access_control_value<<12)));
1084
1085         /* Wait for IOC to clear Doorbell Status bit */
1086         if ((r = WaitForDoorbellAck(ioc, 5, sleepFlag)) < 0) {
1087                 return -2;
1088         }else
1089                 return 0;
1090 }
1091
1092 /*=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=*/
1093 /**
1094  *      mpt_host_page_alloc - allocate system memory for the fw
1095  *      @ioc: Pointer to pointer to IOC adapter
1096  *      @ioc_init: Pointer to ioc init config page
1097  *
1098  *      If we already allocated memory in past, then resend the same pointer.
1099  *      Returns 0 for success, non-zero for failure.
1100  */
1101 static int
1102 mpt_host_page_alloc(MPT_ADAPTER *ioc, pIOCInit_t ioc_init)
1103 {
1104         char    *psge;
1105         int     flags_length;
1106         u32     host_page_buffer_sz=0;
1107
1108         if(!ioc->HostPageBuffer) {
1109
1110                 host_page_buffer_sz =
1111                     le32_to_cpu(ioc->facts.HostPageBufferSGE.FlagsLength) & 0xFFFFFF;
1112
1113                 if(!host_page_buffer_sz)
1114                         return 0; /* fw doesn't need any host buffers */
1115
1116                 /* spin till we get enough memory */
1117                 while(host_page_buffer_sz > 0) {
1118
1119                         if((ioc->HostPageBuffer = pci_alloc_consistent(
1120                             ioc->pcidev,
1121                             host_page_buffer_sz,
1122                             &ioc->HostPageBuffer_dma)) != NULL) {
1123
1124                                 dinitprintk(ioc, printk(MYIOC_s_DEBUG_FMT
1125                                     "host_page_buffer @ %p, dma @ %x, sz=%d bytes\n",
1126                                     ioc->name, ioc->HostPageBuffer,
1127                                     (u32)ioc->HostPageBuffer_dma,
1128                                     host_page_buffer_sz));
1129                                 ioc->alloc_total += host_page_buffer_sz;
1130                                 ioc->HostPageBuffer_sz = host_page_buffer_sz;
1131                                 break;
1132                         }
1133
1134                         host_page_buffer_sz -= (4*1024);
1135                 }
1136         }
1137
1138         if(!ioc->HostPageBuffer) {
1139                 printk(MYIOC_s_ERR_FMT
1140                     "Failed to alloc memory for host_page_buffer!\n",
1141                     ioc->name);
1142                 return -999;
1143         }
1144
1145         psge = (char *)&ioc_init->HostPageBufferSGE;
1146         flags_length = MPI_SGE_FLAGS_SIMPLE_ELEMENT |
1147             MPI_SGE_FLAGS_SYSTEM_ADDRESS |
1148             MPI_SGE_FLAGS_32_BIT_ADDRESSING |
1149             MPI_SGE_FLAGS_HOST_TO_IOC |
1150             MPI_SGE_FLAGS_END_OF_BUFFER;
1151         if (sizeof(dma_addr_t) == sizeof(u64)) {
1152             flags_length |= MPI_SGE_FLAGS_64_BIT_ADDRESSING;
1153         }
1154         flags_length = flags_length << MPI_SGE_FLAGS_SHIFT;
1155         flags_length |= ioc->HostPageBuffer_sz;
1156         mpt_add_sge(psge, flags_length, ioc->HostPageBuffer_dma);
1157         ioc->facts.HostPageBufferSGE = ioc_init->HostPageBufferSGE;
1158
1159 return 0;
1160 }
1161
1162 /*=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=*/
1163 /**
1164  *      mpt_verify_adapter - Given IOC identifier, set pointer to its adapter structure.
1165  *      @iocid: IOC unique identifier (integer)
1166  *      @iocpp: Pointer to pointer to IOC adapter
1167  *
1168  *      Given a unique IOC identifier, set pointer to the associated MPT
1169  *      adapter structure.
1170  *
1171  *      Returns iocid and sets iocpp if iocid is found.
1172  *      Returns -1 if iocid is not found.
1173  */
1174 int
1175 mpt_verify_adapter(int iocid, MPT_ADAPTER **iocpp)
1176 {
1177         MPT_ADAPTER *ioc;
1178
1179         list_for_each_entry(ioc,&ioc_list,list) {
1180                 if (ioc->id == iocid) {
1181                         *iocpp =ioc;
1182                         return iocid;
1183                 }
1184         }
1185
1186         *iocpp = NULL;
1187         return -1;
1188 }
1189
1190 /**
1191  *      mpt_get_product_name - returns product string
1192  *      @vendor: pci vendor id
1193  *      @device: pci device id
1194  *      @revision: pci revision id
1195  *      @prod_name: string returned
1196  *
1197  *      Returns product string displayed when driver loads,
1198  *      in /proc/mpt/summary and /sysfs/class/scsi_host/host<X>/version_product
1199  *
1200  **/
1201 static void
1202 mpt_get_product_name(u16 vendor, u16 device, u8 revision, char *prod_name)
1203 {
1204         char *product_str = NULL;
1205
1206         if (vendor == PCI_VENDOR_ID_BROCADE) {
1207                 switch (device)
1208                 {
1209                 case MPI_MANUFACTPAGE_DEVICEID_FC949E:
1210                         switch (revision)
1211                         {
1212                         case 0x00:
1213                                 product_str = "BRE040 A0";
1214                                 break;
1215                         case 0x01:
1216                                 product_str = "BRE040 A1";
1217                                 break;
1218                         default:
1219                                 product_str = "BRE040";
1220                                 break;
1221                         }
1222                         break;
1223                 }
1224                 goto out;
1225         }
1226
1227         switch (device)
1228         {
1229         case MPI_MANUFACTPAGE_DEVICEID_FC909:
1230                 product_str = "LSIFC909 B1";
1231                 break;
1232         case MPI_MANUFACTPAGE_DEVICEID_FC919:
1233                 product_str = "LSIFC919 B0";
1234                 break;
1235         case MPI_MANUFACTPAGE_DEVICEID_FC929:
1236                 product_str = "LSIFC929 B0";
1237                 break;
1238         case MPI_MANUFACTPAGE_DEVICEID_FC919X:
1239                 if (revision < 0x80)
1240                         product_str = "LSIFC919X A0";
1241                 else
1242                         product_str = "LSIFC919XL A1";
1243                 break;
1244         case MPI_MANUFACTPAGE_DEVICEID_FC929X:
1245                 if (revision < 0x80)
1246                         product_str = "LSIFC929X A0";
1247                 else
1248                         product_str = "LSIFC929XL A1";
1249                 break;
1250         case MPI_MANUFACTPAGE_DEVICEID_FC939X:
1251                 product_str = "LSIFC939X A1";
1252                 break;
1253         case MPI_MANUFACTPAGE_DEVICEID_FC949X:
1254                 product_str = "LSIFC949X A1";
1255                 break;
1256         case MPI_MANUFACTPAGE_DEVICEID_FC949E:
1257                 switch (revision)
1258                 {
1259                 case 0x00:
1260                         product_str = "LSIFC949E A0";
1261                         break;
1262                 case 0x01:
1263                         product_str = "LSIFC949E A1";
1264                         break;
1265                 default:
1266                         product_str = "LSIFC949E";
1267                         break;
1268                 }
1269                 break;
1270         case MPI_MANUFACTPAGE_DEVID_53C1030:
1271                 switch (revision)
1272                 {
1273                 case 0x00:
1274                         product_str = "LSI53C1030 A0";
1275                         break;
1276                 case 0x01:
1277                         product_str = "LSI53C1030 B0";
1278                         break;
1279                 case 0x03:
1280                         product_str = "LSI53C1030 B1";
1281                         break;
1282                 case 0x07:
1283                         product_str = "LSI53C1030 B2";
1284                         break;
1285                 case 0x08:
1286                         product_str = "LSI53C1030 C0";
1287                         break;
1288                 case 0x80:
1289                         product_str = "LSI53C1030T A0";
1290                         break;
1291                 case 0x83:
1292                         product_str = "LSI53C1030T A2";
1293                         break;
1294                 case 0x87:
1295                         product_str = "LSI53C1030T A3";
1296                         break;
1297                 case 0xc1:
1298                         product_str = "LSI53C1020A A1";
1299                         break;
1300                 default:
1301                         product_str = "LSI53C1030";
1302                         break;
1303                 }
1304                 break;
1305         case MPI_MANUFACTPAGE_DEVID_1030_53C1035:
1306                 switch (revision)
1307                 {
1308                 case 0x03:
1309                         product_str = "LSI53C1035 A2";
1310                         break;
1311                 case 0x04:
1312                         product_str = "LSI53C1035 B0";
1313                         break;
1314                 default:
1315                         product_str = "LSI53C1035";
1316                         break;
1317                 }
1318                 break;
1319         case MPI_MANUFACTPAGE_DEVID_SAS1064:
1320                 switch (revision)
1321                 {
1322                 case 0x00:
1323                         product_str = "LSISAS1064 A1";
1324                         break;
1325                 case 0x01:
1326                         product_str = "LSISAS1064 A2";
1327                         break;
1328                 case 0x02:
1329                         product_str = "LSISAS1064 A3";
1330                         break;
1331                 case 0x03:
1332                         product_str = "LSISAS1064 A4";
1333                         break;
1334                 default:
1335                         product_str = "LSISAS1064";
1336                         break;
1337                 }
1338                 break;
1339         case MPI_MANUFACTPAGE_DEVID_SAS1064E:
1340                 switch (revision)
1341                 {
1342                 case 0x00:
1343                         product_str = "LSISAS1064E A0";
1344                         break;
1345                 case 0x01:
1346                         product_str = "LSISAS1064E B0";
1347                         break;
1348                 case 0x02:
1349                         product_str = "LSISAS1064E B1";
1350                         break;
1351                 case 0x04:
1352                         product_str = "LSISAS1064E B2";
1353                         break;
1354                 case 0x08:
1355                         product_str = "LSISAS1064E B3";
1356                         break;
1357                 default:
1358                         product_str = "LSISAS1064E";
1359                         break;
1360                 }
1361                 break;
1362         case MPI_MANUFACTPAGE_DEVID_SAS1068:
1363                 switch (revision)
1364                 {
1365                 case 0x00:
1366                         product_str = "LSISAS1068 A0";
1367                         break;
1368                 case 0x01:
1369                         product_str = "LSISAS1068 B0";
1370                         break;
1371                 case 0x02:
1372                         product_str = "LSISAS1068 B1";
1373                         break;
1374                 default:
1375                         product_str = "LSISAS1068";
1376                         break;
1377                 }
1378                 break;
1379         case MPI_MANUFACTPAGE_DEVID_SAS1068E:
1380                 switch (revision)
1381                 {
1382                 case 0x00:
1383                         product_str = "LSISAS1068E A0";
1384                         break;
1385                 case 0x01:
1386                         product_str = "LSISAS1068E B0";
1387                         break;
1388                 case 0x02:
1389                         product_str = "LSISAS1068E B1";
1390                         break;
1391                 case 0x04:
1392                         product_str = "LSISAS1068E B2";
1393                         break;
1394                 case 0x08:
1395                         product_str = "LSISAS1068E B3";
1396                         break;
1397                 default:
1398                         product_str = "LSISAS1068E";
1399                         break;
1400                 }
1401                 break;
1402         case MPI_MANUFACTPAGE_DEVID_SAS1078:
1403                 switch (revision)
1404                 {
1405                 case 0x00:
1406                         product_str = "LSISAS1078 A0";
1407                         break;
1408                 case 0x01:
1409                         product_str = "LSISAS1078 B0";
1410                         break;
1411                 case 0x02:
1412                         product_str = "LSISAS1078 C0";
1413                         break;
1414                 case 0x03:
1415                         product_str = "LSISAS1078 C1";
1416                         break;
1417                 case 0x04:
1418                         product_str = "LSISAS1078 C2";
1419                         break;
1420                 default:
1421                         product_str = "LSISAS1078";
1422                         break;
1423                 }
1424                 break;
1425         }
1426
1427  out:
1428         if (product_str)
1429                 sprintf(prod_name, "%s", product_str);
1430 }
1431
1432 /*=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=*/
1433 /**
1434  *      mpt_attach - Install a PCI intelligent MPT adapter.
1435  *      @pdev: Pointer to pci_dev structure
1436  *      @id: PCI device ID information
1437  *
1438  *      This routine performs all the steps necessary to bring the IOC of
1439  *      a MPT adapter to a OPERATIONAL state.  This includes registering
1440  *      memory regions, registering the interrupt, and allocating request
1441  *      and reply memory pools.
1442  *
1443  *      This routine also pre-fetches the LAN MAC address of a Fibre Channel
1444  *      MPT adapter.
1445  *
1446  *      Returns 0 for success, non-zero for failure.
1447  *
1448  *      TODO: Add support for polled controllers
1449  */
1450 int
1451 mpt_attach(struct pci_dev *pdev, const struct pci_device_id *id)
1452 {
1453         MPT_ADAPTER     *ioc;
1454         u8              __iomem *mem;
1455         u8              __iomem *pmem;
1456         unsigned long    mem_phys;
1457         unsigned long    port;
1458         u32              msize;
1459         u32              psize;
1460         int              ii;
1461         u8               cb_idx;
1462         int              r = -ENODEV;
1463         u8               revision;
1464         u8               pcixcmd;
1465         static int       mpt_ids = 0;
1466 #ifdef CONFIG_PROC_FS
1467         struct proc_dir_entry *dent, *ent;
1468 #endif
1469
1470         if (mpt_debug_level)
1471                 printk(KERN_INFO MYNAM ": mpt_debug_level=%xh\n", mpt_debug_level);
1472
1473         ioc = kzalloc(sizeof(MPT_ADAPTER), GFP_ATOMIC);
1474         if (ioc == NULL) {
1475                 printk(KERN_ERR MYNAM ": ERROR - Insufficient memory to add adapter!\n");
1476                 return -ENOMEM;
1477         }
1478         ioc->debug_level = mpt_debug_level;
1479         ioc->id = mpt_ids++;
1480         sprintf(ioc->name, "ioc%d", ioc->id);
1481
1482         ioc->bars = pci_select_bars(pdev, IORESOURCE_MEM);
1483         if (pci_enable_device_mem(pdev)) {
1484                 printk(MYIOC_s_ERR_FMT "pci_enable_device_mem() "
1485                        "failed\n", ioc->name);
1486                 kfree(ioc);
1487                 return r;
1488         }
1489         if (pci_request_selected_regions(pdev, ioc->bars, "mpt")) {
1490                 printk(MYIOC_s_ERR_FMT "pci_request_selected_regions() with "
1491                        "MEM failed\n", ioc->name);
1492                 kfree(ioc);
1493                 return r;
1494         }
1495
1496         dinitprintk(ioc, printk(MYIOC_s_INFO_FMT ": mpt_adapter_install\n", ioc->name));
1497
1498         if (!pci_set_dma_mask(pdev, DMA_64BIT_MASK)) {
1499                 dprintk(ioc, printk(MYIOC_s_INFO_FMT
1500                         ": 64 BIT PCI BUS DMA ADDRESSING SUPPORTED\n", ioc->name));
1501         } else if (pci_set_dma_mask(pdev, DMA_32BIT_MASK)) {
1502                 printk(MYIOC_s_WARN_FMT ": 32 BIT PCI BUS DMA ADDRESSING NOT SUPPORTED\n",
1503                     ioc->name);
1504                 kfree(ioc);
1505                 return r;
1506         }
1507
1508         if (!pci_set_consistent_dma_mask(pdev, DMA_64BIT_MASK)) {
1509                 dprintk(ioc, printk(MYIOC_s_INFO_FMT
1510                         ": Using 64 bit consistent mask\n", ioc->name));
1511         } else {
1512                 dprintk(ioc, printk(MYIOC_s_INFO_FMT
1513                         ": Not using 64 bit consistent mask\n", ioc->name));
1514         }
1515
1516         ioc->alloc_total = sizeof(MPT_ADAPTER);
1517         ioc->req_sz = MPT_DEFAULT_FRAME_SIZE;           /* avoid div by zero! */
1518         ioc->reply_sz = MPT_REPLY_FRAME_SIZE;
1519
1520         ioc->pcidev = pdev;
1521         ioc->diagPending = 0;
1522         spin_lock_init(&ioc->diagLock);
1523         spin_lock_init(&ioc->initializing_hba_lock);
1524
1525         /* Initialize the event logging.
1526          */
1527         ioc->eventTypes = 0;    /* None */
1528         ioc->eventContext = 0;
1529         ioc->eventLogSize = 0;
1530         ioc->events = NULL;
1531
1532 #ifdef MFCNT
1533         ioc->mfcnt = 0;
1534 #endif
1535
1536         ioc->cached_fw = NULL;
1537
1538         /* Initilize SCSI Config Data structure
1539          */
1540         memset(&ioc->spi_data, 0, sizeof(SpiCfgData));
1541
1542         /* Initialize the running configQ head.
1543          */
1544         INIT_LIST_HEAD(&ioc->configQ);
1545
1546         /* Initialize the fc rport list head.
1547          */
1548         INIT_LIST_HEAD(&ioc->fc_rports);
1549
1550         /* Find lookup slot. */
1551         INIT_LIST_HEAD(&ioc->list);
1552
1553         mem_phys = msize = 0;
1554         port = psize = 0;
1555         for (ii=0; ii < DEVICE_COUNT_RESOURCE; ii++) {
1556                 if (pci_resource_flags(pdev, ii) & PCI_BASE_ADDRESS_SPACE_IO) {
1557                         if (psize)
1558                                 continue;
1559                         /* Get I/O space! */
1560                         port = pci_resource_start(pdev, ii);
1561                         psize = pci_resource_len(pdev,ii);
1562                 } else {
1563                         if (msize)
1564                                 continue;
1565                         /* Get memmap */
1566                         mem_phys = pci_resource_start(pdev, ii);
1567                         msize = pci_resource_len(pdev,ii);
1568                 }
1569         }
1570         ioc->mem_size = msize;
1571
1572         mem = NULL;
1573         /* Get logical ptr for PciMem0 space */
1574         /*mem = ioremap(mem_phys, msize);*/
1575         mem = ioremap(mem_phys, msize);
1576         if (mem == NULL) {
1577                 printk(MYIOC_s_ERR_FMT "Unable to map adapter memory!\n", ioc->name);
1578                 kfree(ioc);
1579                 return -EINVAL;
1580         }
1581         ioc->memmap = mem;
1582         dinitprintk(ioc, printk(MYIOC_s_INFO_FMT "mem = %p, mem_phys = %lx\n", ioc->name, mem, mem_phys));
1583
1584         dinitprintk(ioc, printk(MYIOC_s_INFO_FMT "facts @ %p, pfacts[0] @ %p\n",
1585             ioc->name, &ioc->facts, &ioc->pfacts[0]));
1586
1587         ioc->mem_phys = mem_phys;
1588         ioc->chip = (SYSIF_REGS __iomem *)mem;
1589
1590         /* Save Port IO values in case we need to do downloadboot */
1591         ioc->pio_mem_phys = port;
1592         pmem = (u8 __iomem *)port;
1593         ioc->pio_chip = (SYSIF_REGS __iomem *)pmem;
1594
1595         pci_read_config_byte(pdev, PCI_CLASS_REVISION, &revision);
1596         mpt_get_product_name(pdev->vendor, pdev->device, revision, ioc->prod_name);
1597
1598         switch (pdev->device)
1599         {
1600         case MPI_MANUFACTPAGE_DEVICEID_FC939X:
1601         case MPI_MANUFACTPAGE_DEVICEID_FC949X:
1602                 ioc->errata_flag_1064 = 1;
1603         case MPI_MANUFACTPAGE_DEVICEID_FC909:
1604         case MPI_MANUFACTPAGE_DEVICEID_FC929:
1605         case MPI_MANUFACTPAGE_DEVICEID_FC919:
1606         case MPI_MANUFACTPAGE_DEVICEID_FC949E:
1607                 ioc->bus_type = FC;
1608                 break;
1609
1610         case MPI_MANUFACTPAGE_DEVICEID_FC929X:
1611                 if (revision < XL_929) {
1612                         /* 929X Chip Fix. Set Split transactions level
1613                         * for PCIX. Set MOST bits to zero.
1614                         */
1615                         pci_read_config_byte(pdev, 0x6a, &pcixcmd);
1616                         pcixcmd &= 0x8F;
1617                         pci_write_config_byte(pdev, 0x6a, pcixcmd);
1618                 } else {
1619                         /* 929XL Chip Fix. Set MMRBC to 0x08.
1620                         */
1621                         pci_read_config_byte(pdev, 0x6a, &pcixcmd);
1622                         pcixcmd |= 0x08;
1623                         pci_write_config_byte(pdev, 0x6a, pcixcmd);
1624                 }
1625                 ioc->bus_type = FC;
1626                 break;
1627
1628         case MPI_MANUFACTPAGE_DEVICEID_FC919X:
1629                 /* 919X Chip Fix. Set Split transactions level
1630                  * for PCIX. Set MOST bits to zero.
1631                  */
1632                 pci_read_config_byte(pdev, 0x6a, &pcixcmd);
1633                 pcixcmd &= 0x8F;
1634                 pci_write_config_byte(pdev, 0x6a, pcixcmd);
1635                 ioc->bus_type = FC;
1636                 break;
1637
1638         case MPI_MANUFACTPAGE_DEVID_53C1030:
1639                 /* 1030 Chip Fix. Disable Split transactions
1640                  * for PCIX. Set MOST bits to zero if Rev < C0( = 8).
1641                  */
1642                 if (revision < C0_1030) {
1643                         pci_read_config_byte(pdev, 0x6a, &pcixcmd);
1644                         pcixcmd &= 0x8F;
1645                         pci_write_config_byte(pdev, 0x6a, pcixcmd);
1646                 }
1647
1648         case MPI_MANUFACTPAGE_DEVID_1030_53C1035:
1649                 ioc->bus_type = SPI;
1650                 break;
1651
1652         case MPI_MANUFACTPAGE_DEVID_SAS1064:
1653         case MPI_MANUFACTPAGE_DEVID_SAS1068:
1654                 ioc->errata_flag_1064 = 1;
1655
1656         case MPI_MANUFACTPAGE_DEVID_SAS1064E:
1657         case MPI_MANUFACTPAGE_DEVID_SAS1068E:
1658         case MPI_MANUFACTPAGE_DEVID_SAS1078:
1659                 ioc->bus_type = SAS;
1660         }
1661
1662         if (ioc->errata_flag_1064)
1663                 pci_disable_io_access(pdev);
1664
1665         spin_lock_init(&ioc->FreeQlock);
1666
1667         /* Disable all! */
1668         CHIPREG_WRITE32(&ioc->chip->IntMask, 0xFFFFFFFF);
1669         ioc->active = 0;
1670         CHIPREG_WRITE32(&ioc->chip->IntStatus, 0);
1671
1672         /* Set IOC ptr in the pcidev's driver data. */
1673         pci_set_drvdata(ioc->pcidev, ioc);
1674
1675         /* Set lookup ptr. */
1676         list_add_tail(&ioc->list, &ioc_list);
1677
1678         /* Check for "bound ports" (929, 929X, 1030, 1035) to reduce redundant resets.
1679          */
1680         mpt_detect_bound_ports(ioc, pdev);
1681
1682         if ((r = mpt_do_ioc_recovery(ioc, MPT_HOSTEVENT_IOC_BRINGUP,
1683             CAN_SLEEP)) != 0){
1684                 printk(MYIOC_s_ERR_FMT "didn't initialize properly! (%d)\n",
1685                     ioc->name, r);
1686
1687                 list_del(&ioc->list);
1688                 if (ioc->alt_ioc)
1689                         ioc->alt_ioc->alt_ioc = NULL;
1690                 iounmap(mem);
1691                 kfree(ioc);
1692                 pci_set_drvdata(pdev, NULL);
1693                 return r;
1694         }
1695
1696         /* call per device driver probe entry point */
1697         for(cb_idx = 0; cb_idx < MPT_MAX_PROTOCOL_DRIVERS; cb_idx++) {
1698                 if(MptDeviceDriverHandlers[cb_idx] &&
1699                   MptDeviceDriverHandlers[cb_idx]->probe) {
1700                         MptDeviceDriverHandlers[cb_idx]->probe(pdev,id);
1701                 }
1702         }
1703
1704 #ifdef CONFIG_PROC_FS
1705         /*
1706          *  Create "/proc/mpt/iocN" subdirectory entry for each MPT adapter.
1707          */
1708         dent = proc_mkdir(ioc->name, mpt_proc_root_dir);
1709         if (dent) {
1710                 ent = create_proc_entry("info", S_IFREG|S_IRUGO, dent);
1711                 if (ent) {
1712                         ent->read_proc = procmpt_iocinfo_read;
1713                         ent->data = ioc;
1714                 }
1715                 ent = create_proc_entry("summary", S_IFREG|S_IRUGO, dent);
1716                 if (ent) {
1717                         ent->read_proc = procmpt_summary_read;
1718                         ent->data = ioc;
1719                 }
1720         }
1721 #endif
1722
1723         return 0;
1724 }
1725
1726 /*=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=*/
1727 /**
1728  *      mpt_detach - Remove a PCI intelligent MPT adapter.
1729  *      @pdev: Pointer to pci_dev structure
1730  */
1731
1732 void
1733 mpt_detach(struct pci_dev *pdev)
1734 {
1735         MPT_ADAPTER     *ioc = pci_get_drvdata(pdev);
1736         char pname[32];
1737         u8 cb_idx;
1738
1739         sprintf(pname, MPT_PROCFS_MPTBASEDIR "/%s/summary", ioc->name);
1740         remove_proc_entry(pname, NULL);
1741         sprintf(pname, MPT_PROCFS_MPTBASEDIR "/%s/info", ioc->name);
1742         remove_proc_entry(pname, NULL);
1743         sprintf(pname, MPT_PROCFS_MPTBASEDIR "/%s", ioc->name);
1744         remove_proc_entry(pname, NULL);
1745
1746         /* call per device driver remove entry point */
1747         for(cb_idx = 0; cb_idx < MPT_MAX_PROTOCOL_DRIVERS; cb_idx++) {
1748                 if(MptDeviceDriverHandlers[cb_idx] &&
1749                   MptDeviceDriverHandlers[cb_idx]->remove) {
1750                         MptDeviceDriverHandlers[cb_idx]->remove(pdev);
1751                 }
1752         }
1753
1754         /* Disable interrupts! */
1755         CHIPREG_WRITE32(&ioc->chip->IntMask, 0xFFFFFFFF);
1756
1757         ioc->active = 0;
1758         synchronize_irq(pdev->irq);
1759
1760         /* Clear any lingering interrupt */
1761         CHIPREG_WRITE32(&ioc->chip->IntStatus, 0);
1762
1763         CHIPREG_READ32(&ioc->chip->IntStatus);
1764
1765         mpt_adapter_dispose(ioc);
1766
1767         pci_set_drvdata(pdev, NULL);
1768 }
1769
1770 /**************************************************************************
1771  * Power Management
1772  */
1773 #ifdef CONFIG_PM
1774 /*=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=*/
1775 /**
1776  *      mpt_suspend - Fusion MPT base driver suspend routine.
1777  *      @pdev: Pointer to pci_dev structure
1778  *      @state: new state to enter
1779  */
1780 int
1781 mpt_suspend(struct pci_dev *pdev, pm_message_t state)
1782 {
1783         u32 device_state;
1784         MPT_ADAPTER *ioc = pci_get_drvdata(pdev);
1785
1786         device_state=pci_choose_state(pdev, state);
1787
1788         printk(MYIOC_s_INFO_FMT
1789         "pci-suspend: pdev=0x%p, slot=%s, Entering operating state [D%d]\n",
1790                 ioc->name, pdev, pci_name(pdev), device_state);
1791
1792         pci_save_state(pdev);
1793
1794         /* put ioc into READY_STATE */
1795         if(SendIocReset(ioc, MPI_FUNCTION_IOC_MESSAGE_UNIT_RESET, CAN_SLEEP)) {
1796                 printk(MYIOC_s_ERR_FMT
1797                 "pci-suspend:  IOC msg unit reset failed!\n", ioc->name);
1798         }
1799
1800         /* disable interrupts */
1801         CHIPREG_WRITE32(&ioc->chip->IntMask, 0xFFFFFFFF);
1802         ioc->active = 0;
1803
1804         /* Clear any lingering interrupt */
1805         CHIPREG_WRITE32(&ioc->chip->IntStatus, 0);
1806
1807         pci_disable_device(pdev);
1808         pci_release_selected_regions(pdev, ioc->bars);
1809         pci_set_power_state(pdev, device_state);
1810
1811         return 0;
1812 }
1813
1814 /*=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=*/
1815 /**
1816  *      mpt_resume - Fusion MPT base driver resume routine.
1817  *      @pdev: Pointer to pci_dev structure
1818  */
1819 int
1820 mpt_resume(struct pci_dev *pdev)
1821 {
1822         MPT_ADAPTER *ioc = pci_get_drvdata(pdev);
1823         u32 device_state = pdev->current_state;
1824         int recovery_state;
1825
1826         printk(MYIOC_s_INFO_FMT
1827         "pci-resume: pdev=0x%p, slot=%s, Previous operating state [D%d]\n",
1828                 ioc->name, pdev, pci_name(pdev), device_state);
1829
1830         pci_set_power_state(pdev, 0);
1831         pci_restore_state(pdev);
1832         if (ioc->facts.Flags & MPI_IOCFACTS_FLAGS_FW_DOWNLOAD_BOOT) {
1833                 ioc->bars = pci_select_bars(ioc->pcidev, IORESOURCE_MEM |
1834                         IORESOURCE_IO);
1835                 if (pci_enable_device(pdev))
1836                         return 0;
1837         } else {
1838                 ioc->bars = pci_select_bars(pdev, IORESOURCE_MEM);
1839                 if (pci_enable_device_mem(pdev))
1840                         return 0;
1841         }
1842         if (pci_request_selected_regions(pdev, ioc->bars, "mpt"))
1843                 return 0;
1844
1845         /* enable interrupts */
1846         CHIPREG_WRITE32(&ioc->chip->IntMask, MPI_HIM_DIM);
1847         ioc->active = 1;
1848
1849         printk(MYIOC_s_INFO_FMT
1850                 "pci-resume: ioc-state=0x%x,doorbell=0x%x\n",
1851                 ioc->name,
1852                 (mpt_GetIocState(ioc, 1) >> MPI_IOC_STATE_SHIFT),
1853                 CHIPREG_READ32(&ioc->chip->Doorbell));
1854
1855         /* bring ioc to operational state */
1856         if ((recovery_state = mpt_do_ioc_recovery(ioc,
1857             MPT_HOSTEVENT_IOC_RECOVER, CAN_SLEEP)) != 0) {
1858                 printk(MYIOC_s_INFO_FMT
1859                         "pci-resume: Cannot recover, error:[%x]\n",
1860                         ioc->name, recovery_state);
1861         } else {
1862                 printk(MYIOC_s_INFO_FMT
1863                         "pci-resume: success\n", ioc->name);
1864         }
1865
1866         return 0;
1867 }
1868 #endif
1869
1870 static int
1871 mpt_signal_reset(u8 index, MPT_ADAPTER *ioc, int reset_phase)
1872 {
1873         if ((MptDriverClass[index] == MPTSPI_DRIVER &&
1874              ioc->bus_type != SPI) ||
1875             (MptDriverClass[index] == MPTFC_DRIVER &&
1876              ioc->bus_type != FC) ||
1877             (MptDriverClass[index] == MPTSAS_DRIVER &&
1878              ioc->bus_type != SAS))
1879                 /* make sure we only call the relevant reset handler
1880                  * for the bus */
1881                 return 0;
1882         return (MptResetHandlers[index])(ioc, reset_phase);
1883 }
1884
1885 /*=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=*/
1886 /**
1887  *      mpt_do_ioc_recovery - Initialize or recover MPT adapter.
1888  *      @ioc: Pointer to MPT adapter structure
1889  *      @reason: Event word / reason
1890  *      @sleepFlag: Use schedule if CAN_SLEEP else use udelay.
1891  *
1892  *      This routine performs all the steps necessary to bring the IOC
1893  *      to a OPERATIONAL state.
1894  *
1895  *      This routine also pre-fetches the LAN MAC address of a Fibre Channel
1896  *      MPT adapter.
1897  *
1898  *      Returns:
1899  *               0 for success
1900  *              -1 if failed to get board READY
1901  *              -2 if READY but IOCFacts Failed
1902  *              -3 if READY but PrimeIOCFifos Failed
1903  *              -4 if READY but IOCInit Failed
1904  *              -5 if failed to enable_device and/or request_selected_regions
1905  */
1906 static int
1907 mpt_do_ioc_recovery(MPT_ADAPTER *ioc, u32 reason, int sleepFlag)
1908 {
1909         int      hard_reset_done = 0;
1910         int      alt_ioc_ready = 0;
1911         int      hard;
1912         int      rc=0;
1913         int      ii;
1914         u8       cb_idx;
1915         int      handlers;
1916         int      ret = 0;
1917         int      reset_alt_ioc_active = 0;
1918         int      irq_allocated = 0;
1919         u8      *a;
1920
1921         printk(MYIOC_s_INFO_FMT "Initiating %s\n", ioc->name,
1922             reason == MPT_HOSTEVENT_IOC_BRINGUP ? "bringup" : "recovery");
1923
1924         /* Disable reply interrupts (also blocks FreeQ) */
1925         CHIPREG_WRITE32(&ioc->chip->IntMask, 0xFFFFFFFF);
1926         ioc->active = 0;
1927
1928         if (ioc->alt_ioc) {
1929                 if (ioc->alt_ioc->active)
1930                         reset_alt_ioc_active = 1;
1931
1932                 /* Disable alt-IOC's reply interrupts (and FreeQ) for a bit ... */
1933                 CHIPREG_WRITE32(&ioc->alt_ioc->chip->IntMask, 0xFFFFFFFF);
1934                 ioc->alt_ioc->active = 0;
1935         }
1936
1937         hard = 1;
1938         if (reason == MPT_HOSTEVENT_IOC_BRINGUP)
1939                 hard = 0;
1940
1941         if ((hard_reset_done = MakeIocReady(ioc, hard, sleepFlag)) < 0) {
1942                 if (hard_reset_done == -4) {
1943                         printk(MYIOC_s_WARN_FMT "Owned by PEER..skipping!\n",
1944                             ioc->name);
1945
1946                         if (reset_alt_ioc_active && ioc->alt_ioc) {
1947                                 /* (re)Enable alt-IOC! (reply interrupt, FreeQ) */
1948                                 dprintk(ioc, printk(MYIOC_s_INFO_FMT
1949                                     "alt_ioc reply irq re-enabled\n", ioc->alt_ioc->name));
1950                                 CHIPREG_WRITE32(&ioc->alt_ioc->chip->IntMask, MPI_HIM_DIM);
1951                                 ioc->alt_ioc->active = 1;
1952                         }
1953
1954                 } else {
1955                         printk(MYIOC_s_WARN_FMT "NOT READY!\n", ioc->name);
1956                 }
1957                 return -1;
1958         }
1959
1960         /* hard_reset_done = 0 if a soft reset was performed
1961          * and 1 if a hard reset was performed.
1962          */
1963         if (hard_reset_done && reset_alt_ioc_active && ioc->alt_ioc) {
1964                 if ((rc = MakeIocReady(ioc->alt_ioc, 0, sleepFlag)) == 0)
1965                         alt_ioc_ready = 1;
1966                 else
1967                         printk(MYIOC_s_WARN_FMT "alt_ioc not ready!\n", ioc->alt_ioc->name);
1968         }
1969
1970         for (ii=0; ii<5; ii++) {
1971                 /* Get IOC facts! Allow 5 retries */
1972                 if ((rc = GetIocFacts(ioc, sleepFlag, reason)) == 0)
1973                         break;
1974         }
1975
1976
1977         if (ii == 5) {
1978                 dinitprintk(ioc, printk(MYIOC_s_DEBUG_FMT
1979                     "Retry IocFacts failed rc=%x\n", ioc->name, rc));
1980                 ret = -2;
1981         } else if (reason == MPT_HOSTEVENT_IOC_BRINGUP) {
1982                 MptDisplayIocCapabilities(ioc);
1983         }
1984
1985         if (alt_ioc_ready) {
1986                 if ((rc = GetIocFacts(ioc->alt_ioc, sleepFlag, reason)) != 0) {
1987                         dinitprintk(ioc, printk(MYIOC_s_DEBUG_FMT
1988                             "Initial Alt IocFacts failed rc=%x\n", ioc->name, rc));
1989                         /* Retry - alt IOC was initialized once
1990                          */
1991                         rc = GetIocFacts(ioc->alt_ioc, sleepFlag, reason);
1992                 }
1993                 if (rc) {
1994                         dinitprintk(ioc, printk(MYIOC_s_DEBUG_FMT
1995                             "Retry Alt IocFacts failed rc=%x\n", ioc->name, rc));
1996                         alt_ioc_ready = 0;
1997                         reset_alt_ioc_active = 0;
1998                 } else if (reason == MPT_HOSTEVENT_IOC_BRINGUP) {
1999                         MptDisplayIocCapabilities(ioc->alt_ioc);
2000                 }
2001         }
2002
2003         if ((ret == 0) && (reason == MPT_HOSTEVENT_IOC_BRINGUP) &&
2004             (ioc->facts.Flags & MPI_IOCFACTS_FLAGS_FW_DOWNLOAD_BOOT)) {
2005                 pci_release_selected_regions(ioc->pcidev, ioc->bars);
2006                 ioc->bars = pci_select_bars(ioc->pcidev, IORESOURCE_MEM |
2007                     IORESOURCE_IO);
2008                 if (pci_enable_device(ioc->pcidev))
2009                         return -5;
2010                 if (pci_request_selected_regions(ioc->pcidev, ioc->bars,
2011                         "mpt"))
2012                         return -5;
2013         }
2014
2015         /*
2016          * Device is reset now. It must have de-asserted the interrupt line
2017          * (if it was asserted) and it should be safe to register for the
2018          * interrupt now.
2019          */
2020         if ((ret == 0) && (reason == MPT_HOSTEVENT_IOC_BRINGUP)) {
2021                 ioc->pci_irq = -1;
2022                 if (ioc->pcidev->irq) {
2023                         if (mpt_msi_enable && !pci_enable_msi(ioc->pcidev))
2024                                 printk(MYIOC_s_INFO_FMT "PCI-MSI enabled\n",
2025                                     ioc->name);
2026                         rc = request_irq(ioc->pcidev->irq, mpt_interrupt,
2027                             IRQF_SHARED, ioc->name, ioc);
2028                         if (rc < 0) {
2029                                 printk(MYIOC_s_ERR_FMT "Unable to allocate "
2030                                     "interrupt %d!\n", ioc->name, ioc->pcidev->irq);
2031                                 if (mpt_msi_enable)
2032                                         pci_disable_msi(ioc->pcidev);
2033                                 return -EBUSY;
2034                         }
2035                         irq_allocated = 1;
2036                         ioc->pci_irq = ioc->pcidev->irq;
2037                         pci_set_master(ioc->pcidev);            /* ?? */
2038                         dprintk(ioc, printk(MYIOC_s_INFO_FMT "installed at interrupt "
2039                             "%d\n", ioc->name, ioc->pcidev->irq));
2040                 }
2041         }
2042
2043         /* Prime reply & request queues!
2044          * (mucho alloc's) Must be done prior to
2045          * init as upper addresses are needed for init.
2046          * If fails, continue with alt-ioc processing
2047          */
2048         if ((ret == 0) && ((rc = PrimeIocFifos(ioc)) != 0))
2049                 ret = -3;
2050
2051         /* May need to check/upload firmware & data here!
2052          * If fails, continue with alt-ioc processing
2053          */
2054         if ((ret == 0) && ((rc = SendIocInit(ioc, sleepFlag)) != 0))
2055                 ret = -4;
2056 // NEW!
2057         if (alt_ioc_ready && ((rc = PrimeIocFifos(ioc->alt_ioc)) != 0)) {
2058                 printk(MYIOC_s_WARN_FMT ": alt_ioc (%d) FIFO mgmt alloc!\n",
2059                     ioc->alt_ioc->name, rc);
2060                 alt_ioc_ready = 0;
2061                 reset_alt_ioc_active = 0;
2062         }
2063
2064         if (alt_ioc_ready) {
2065                 if ((rc = SendIocInit(ioc->alt_ioc, sleepFlag)) != 0) {
2066                         alt_ioc_ready = 0;
2067                         reset_alt_ioc_active = 0;
2068                         printk(MYIOC_s_WARN_FMT "alt_ioc (%d) init failure!\n",
2069                             ioc->alt_ioc->name, rc);
2070                 }
2071         }
2072
2073         if (reason == MPT_HOSTEVENT_IOC_BRINGUP){
2074                 if (ioc->upload_fw) {
2075                         ddlprintk(ioc, printk(MYIOC_s_DEBUG_FMT
2076                             "firmware upload required!\n", ioc->name));
2077
2078                         /* Controller is not operational, cannot do upload
2079                          */
2080                         if (ret == 0) {
2081                                 rc = mpt_do_upload(ioc, sleepFlag);
2082                                 if (rc == 0) {
2083                                         if (ioc->alt_ioc && ioc->alt_ioc->cached_fw) {
2084                                                 /*
2085                                                  * Maintain only one pointer to FW memory
2086                                                  * so there will not be two attempt to
2087                                                  * downloadboot onboard dual function
2088                                                  * chips (mpt_adapter_disable,
2089                                                  * mpt_diag_reset)
2090                                                  */
2091                                                 ddlprintk(ioc, printk(MYIOC_s_DEBUG_FMT
2092                                                     "mpt_upload:  alt_%s has cached_fw=%p \n",
2093                                                     ioc->name, ioc->alt_ioc->name, ioc->alt_ioc->cached_fw));
2094                                                 ioc->cached_fw = NULL;
2095                                         }
2096                                 } else {
2097                                         printk(MYIOC_s_WARN_FMT
2098                                             "firmware upload failure!\n", ioc->name);
2099                                         ret = -5;
2100                                 }
2101                         }
2102                 }
2103         }
2104
2105         if (ret == 0) {
2106                 /* Enable! (reply interrupt) */
2107                 CHIPREG_WRITE32(&ioc->chip->IntMask, MPI_HIM_DIM);
2108                 ioc->active = 1;
2109         }
2110
2111         if (reset_alt_ioc_active && ioc->alt_ioc) {
2112                 /* (re)Enable alt-IOC! (reply interrupt) */
2113                 dinitprintk(ioc, printk(MYIOC_s_INFO_FMT "alt_ioc reply irq re-enabled\n",
2114                     ioc->alt_ioc->name));
2115                 CHIPREG_WRITE32(&ioc->alt_ioc->chip->IntMask, MPI_HIM_DIM);
2116                 ioc->alt_ioc->active = 1;
2117         }
2118
2119         /*  Enable MPT base driver management of EventNotification
2120          *  and EventAck handling.
2121          */
2122         if ((ret == 0) && (!ioc->facts.EventState))
2123                 (void) SendEventNotification(ioc, 1);   /* 1=Enable EventNotification */
2124
2125         if (ioc->alt_ioc && alt_ioc_ready && !ioc->alt_ioc->facts.EventState)
2126                 (void) SendEventNotification(ioc->alt_ioc, 1);  /* 1=Enable EventNotification */
2127
2128         /*      Add additional "reason" check before call to GetLanConfigPages
2129          *      (combined with GetIoUnitPage2 call).  This prevents a somewhat
2130          *      recursive scenario; GetLanConfigPages times out, timer expired
2131          *      routine calls HardResetHandler, which calls into here again,
2132          *      and we try GetLanConfigPages again...
2133          */
2134         if ((ret == 0) && (reason == MPT_HOSTEVENT_IOC_BRINGUP)) {
2135
2136                 /*
2137                  * Initalize link list for inactive raid volumes.
2138                  */
2139                 init_MUTEX(&ioc->raid_data.inactive_list_mutex);
2140                 INIT_LIST_HEAD(&ioc->raid_data.inactive_list);
2141
2142                 if (ioc->bus_type == SAS) {
2143
2144                         /* clear persistency table */
2145                         if(ioc->facts.IOCExceptions &
2146                             MPI_IOCFACTS_EXCEPT_PERSISTENT_TABLE_FULL) {
2147                                 ret = mptbase_sas_persist_operation(ioc,
2148                                     MPI_SAS_OP_CLEAR_NOT_PRESENT);
2149                                 if(ret != 0)
2150                                         goto out;
2151                         }
2152
2153                         /* Find IM volumes
2154                          */
2155                         mpt_findImVolumes(ioc);
2156
2157                 } else if (ioc->bus_type == FC) {
2158                         if ((ioc->pfacts[0].ProtocolFlags & MPI_PORTFACTS_PROTOCOL_LAN) &&
2159                             (ioc->lan_cnfg_page0.Header.PageLength == 0)) {
2160                                 /*
2161                                  *  Pre-fetch the ports LAN MAC address!
2162                                  *  (LANPage1_t stuff)
2163                                  */
2164                                 (void) GetLanConfigPages(ioc);
2165                                 a = (u8*)&ioc->lan_cnfg_page1.HardwareAddressLow;
2166                                 dprintk(ioc, printk(MYIOC_s_DEBUG_FMT
2167                                     "LanAddr = %02X:%02X:%02X:%02X:%02X:%02X\n",
2168                                     ioc->name, a[5], a[4], a[3], a[2], a[1], a[0]));
2169
2170                         }
2171                 } else {
2172                         /* Get NVRAM and adapter maximums from SPP 0 and 2
2173                          */
2174                         mpt_GetScsiPortSettings(ioc, 0);
2175
2176                         /* Get version and length of SDP 1
2177                          */
2178                         mpt_readScsiDevicePageHeaders(ioc, 0);
2179
2180                         /* Find IM volumes
2181                          */
2182                         if (ioc->facts.MsgVersion >= MPI_VERSION_01_02)
2183                                 mpt_findImVolumes(ioc);
2184
2185                         /* Check, and possibly reset, the coalescing value
2186                          */
2187                         mpt_read_ioc_pg_1(ioc);
2188
2189                         mpt_read_ioc_pg_4(ioc);
2190                 }
2191
2192                 GetIoUnitPage2(ioc);
2193                 mpt_get_manufacturing_pg_0(ioc);
2194         }
2195
2196         /*
2197          * Call each currently registered protocol IOC reset handler
2198          * with post-reset indication.
2199          * NOTE: If we're doing _IOC_BRINGUP, there can be no
2200          * MptResetHandlers[] registered yet.
2201          */
2202         if (hard_reset_done) {
2203                 rc = handlers = 0;
2204                 for (cb_idx = MPT_MAX_PROTOCOL_DRIVERS-1; cb_idx; cb_idx--) {
2205                         if ((ret == 0) && MptResetHandlers[cb_idx]) {
2206                                 dprintk(ioc, printk(MYIOC_s_DEBUG_FMT
2207                                     "Calling IOC post_reset handler #%d\n",
2208                                     ioc->name, cb_idx));
2209                                 rc += mpt_signal_reset(cb_idx, ioc, MPT_IOC_POST_RESET);
2210                                 handlers++;
2211                         }
2212
2213                         if (alt_ioc_ready && MptResetHandlers[cb_idx]) {
2214                                 drsprintk(ioc, printk(MYIOC_s_DEBUG_FMT
2215                                     "Calling IOC post_reset handler #%d\n",
2216                                     ioc->alt_ioc->name, cb_idx));
2217                                 rc += mpt_signal_reset(cb_idx, ioc->alt_ioc, MPT_IOC_POST_RESET);
2218                                 handlers++;
2219                         }
2220                 }
2221                 /* FIXME?  Examine results here? */
2222         }
2223
2224  out:
2225         if ((ret != 0) && irq_allocated) {
2226                 free_irq(ioc->pci_irq, ioc);
2227                 if (mpt_msi_enable)
2228                         pci_disable_msi(ioc->pcidev);
2229         }
2230         return ret;
2231 }
2232
2233 /*=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=*/
2234 /**
2235  *      mpt_detect_bound_ports - Search for matching PCI bus/dev_function
2236  *      @ioc: Pointer to MPT adapter structure
2237  *      @pdev: Pointer to (struct pci_dev) structure
2238  *
2239  *      Search for PCI bus/dev_function which matches
2240  *      PCI bus/dev_function (+/-1) for newly discovered 929,
2241  *      929X, 1030 or 1035.
2242  *
2243  *      If match on PCI dev_function +/-1 is found, bind the two MPT adapters
2244  *      using alt_ioc pointer fields in their %MPT_ADAPTER structures.
2245  */
2246 static void
2247 mpt_detect_bound_ports(MPT_ADAPTER *ioc, struct pci_dev *pdev)
2248 {
2249         struct pci_dev *peer=NULL;
2250         unsigned int slot = PCI_SLOT(pdev->devfn);
2251         unsigned int func = PCI_FUNC(pdev->devfn);
2252         MPT_ADAPTER *ioc_srch;
2253
2254         dprintk(ioc, printk(MYIOC_s_DEBUG_FMT "PCI device %s devfn=%x/%x,"
2255             " searching for devfn match on %x or %x\n",
2256             ioc->name, pci_name(pdev), pdev->bus->number,
2257             pdev->devfn, func-1, func+1));
2258
2259         peer = pci_get_slot(pdev->bus, PCI_DEVFN(slot,func-1));
2260         if (!peer) {
2261                 peer = pci_get_slot(pdev->bus, PCI_DEVFN(slot,func+1));
2262                 if (!peer)
2263                         return;
2264         }
2265
2266         list_for_each_entry(ioc_srch, &ioc_list, list) {
2267                 struct pci_dev *_pcidev = ioc_srch->pcidev;
2268                 if (_pcidev == peer) {
2269                         /* Paranoia checks */
2270                         if (ioc->alt_ioc != NULL) {
2271                                 printk(MYIOC_s_WARN_FMT "Oops, already bound to %s!\n",
2272                                         ioc->name, ioc->alt_ioc->name);
2273                                 break;
2274                         } else if (ioc_srch->alt_ioc != NULL) {
2275                                 printk(MYIOC_s_WARN_FMT "Oops, already bound to %s!\n",
2276                                         ioc_srch->name, ioc_srch->alt_ioc->name);
2277                                 break;
2278                         }
2279                         dprintk(ioc, printk(MYIOC_s_INFO_FMT "FOUND! binding to %s\n",
2280                                 ioc->name, ioc_srch->name));
2281                         ioc_srch->alt_ioc = ioc;
2282                         ioc->alt_ioc = ioc_srch;
2283                 }
2284         }
2285         pci_dev_put(peer);
2286 }
2287
2288 /*=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=*/
2289 /**
2290  *      mpt_adapter_disable - Disable misbehaving MPT adapter.
2291  *      @ioc: Pointer to MPT adapter structure
2292  */
2293 static void
2294 mpt_adapter_disable(MPT_ADAPTER *ioc)
2295 {
2296         int sz;
2297         int ret;
2298
2299         if (ioc->cached_fw != NULL) {
2300                 ddlprintk(ioc, printk(MYIOC_s_DEBUG_FMT "%s: Pushing FW onto "
2301                     "adapter\n", __FUNCTION__, ioc->name));
2302                 if ((ret = mpt_downloadboot(ioc, (MpiFwHeader_t *)
2303                     ioc->cached_fw, CAN_SLEEP)) < 0) {
2304                         printk(MYIOC_s_WARN_FMT
2305                             ": firmware downloadboot failure (%d)!\n",
2306                             ioc->name, ret);
2307                 }
2308         }
2309
2310         /* Disable adapter interrupts! */
2311         CHIPREG_WRITE32(&ioc->chip->IntMask, 0xFFFFFFFF);
2312         ioc->active = 0;
2313         /* Clear any lingering interrupt */
2314         CHIPREG_WRITE32(&ioc->chip->IntStatus, 0);
2315
2316         if (ioc->alloc != NULL) {
2317                 sz = ioc->alloc_sz;
2318                 dexitprintk(ioc, printk(MYIOC_s_INFO_FMT "free  @ %p, sz=%d bytes\n",
2319                     ioc->name, ioc->alloc, ioc->alloc_sz));
2320                 pci_free_consistent(ioc->pcidev, sz,
2321                                 ioc->alloc, ioc->alloc_dma);
2322                 ioc->reply_frames = NULL;
2323                 ioc->req_frames = NULL;
2324                 ioc->alloc = NULL;
2325                 ioc->alloc_total -= sz;
2326         }
2327
2328         if (ioc->sense_buf_pool != NULL) {
2329                 sz = (ioc->req_depth * MPT_SENSE_BUFFER_ALLOC);
2330                 pci_free_consistent(ioc->pcidev, sz,
2331                                 ioc->sense_buf_pool, ioc->sense_buf_pool_dma);
2332                 ioc->sense_buf_pool = NULL;
2333                 ioc->alloc_total -= sz;
2334         }
2335
2336         if (ioc->events != NULL){
2337                 sz = MPTCTL_EVENT_LOG_SIZE * sizeof(MPT_IOCTL_EVENTS);
2338                 kfree(ioc->events);
2339                 ioc->events = NULL;
2340                 ioc->alloc_total -= sz;
2341         }
2342
2343         mpt_free_fw_memory(ioc);
2344
2345         kfree(ioc->spi_data.nvram);
2346         mpt_inactive_raid_list_free(ioc);
2347         kfree(ioc->raid_data.pIocPg2);
2348         kfree(ioc->raid_data.pIocPg3);
2349         ioc->spi_data.nvram = NULL;
2350         ioc->raid_data.pIocPg3 = NULL;
2351
2352         if (ioc->spi_data.pIocPg4 != NULL) {
2353                 sz = ioc->spi_data.IocPg4Sz;
2354                 pci_free_consistent(ioc->pcidev, sz,
2355                         ioc->spi_data.pIocPg4,
2356                         ioc->spi_data.IocPg4_dma);
2357                 ioc->spi_data.pIocPg4 = NULL;
2358                 ioc->alloc_total -= sz;
2359         }
2360
2361         if (ioc->ReqToChain != NULL) {
2362                 kfree(ioc->ReqToChain);
2363                 kfree(ioc->RequestNB);
2364                 ioc->ReqToChain = NULL;
2365         }
2366
2367         kfree(ioc->ChainToChain);
2368         ioc->ChainToChain = NULL;
2369
2370         if (ioc->HostPageBuffer != NULL) {
2371                 if((ret = mpt_host_page_access_control(ioc,
2372                     MPI_DB_HPBAC_FREE_BUFFER, NO_SLEEP)) != 0) {
2373                         printk(MYIOC_s_ERR_FMT
2374                            "host page buffers free failed (%d)!\n",
2375                             ioc->name, ret);
2376                 }
2377                 dexitprintk(ioc, printk(MYIOC_s_INFO_FMT "HostPageBuffer free  @ %p, sz=%d bytes\n",
2378                         ioc->name, ioc->HostPageBuffer, ioc->HostPageBuffer_sz));
2379                 pci_free_consistent(ioc->pcidev, ioc->HostPageBuffer_sz,
2380                     ioc->HostPageBuffer, ioc->HostPageBuffer_dma);
2381                 ioc->HostPageBuffer = NULL;
2382                 ioc->HostPageBuffer_sz = 0;
2383                 ioc->alloc_total -= ioc->HostPageBuffer_sz;
2384         }
2385 }
2386
2387 /*=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=*/
2388 /**
2389  *      mpt_adapter_dispose - Free all resources associated with an MPT adapter
2390  *      @ioc: Pointer to MPT adapter structure
2391  *
2392  *      This routine unregisters h/w resources and frees all alloc'd memory
2393  *      associated with a MPT adapter structure.
2394  */
2395 static void
2396 mpt_adapter_dispose(MPT_ADAPTER *ioc)
2397 {
2398         int sz_first, sz_last;
2399
2400         if (ioc == NULL)
2401                 return;
2402
2403         sz_first = ioc->alloc_total;
2404
2405         mpt_adapter_disable(ioc);
2406
2407         if (ioc->pci_irq != -1) {
2408                 free_irq(ioc->pci_irq, ioc);
2409                 if (mpt_msi_enable)
2410                         pci_disable_msi(ioc->pcidev);
2411                 ioc->pci_irq = -1;
2412         }
2413
2414         if (ioc->memmap != NULL) {
2415                 iounmap(ioc->memmap);
2416                 ioc->memmap = NULL;
2417         }
2418
2419         pci_disable_device(ioc->pcidev);
2420         pci_release_selected_regions(ioc->pcidev, ioc->bars);
2421
2422 #if defined(CONFIG_MTRR) && 0
2423         if (ioc->mtrr_reg > 0) {
2424                 mtrr_del(ioc->mtrr_reg, 0, 0);
2425                 dprintk(ioc, printk(MYIOC_s_INFO_FMT "MTRR region de-registered\n", ioc->name));
2426         }
2427 #endif
2428
2429         /*  Zap the adapter lookup ptr!  */
2430         list_del(&ioc->list);
2431
2432         sz_last = ioc->alloc_total;
2433         dprintk(ioc, printk(MYIOC_s_INFO_FMT "free'd %d of %d bytes\n",
2434             ioc->name, sz_first-sz_last+(int)sizeof(*ioc), sz_first));
2435
2436         if (ioc->alt_ioc)
2437                 ioc->alt_ioc->alt_ioc = NULL;
2438
2439         kfree(ioc);
2440 }
2441
2442 /*=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=*/
2443 /**
2444  *      MptDisplayIocCapabilities - Disply IOC's capabilities.
2445  *      @ioc: Pointer to MPT adapter structure
2446  */
2447 static void
2448 MptDisplayIocCapabilities(MPT_ADAPTER *ioc)
2449 {
2450         int i = 0;
2451
2452         printk(KERN_INFO "%s: ", ioc->name);
2453         if (ioc->prod_name)
2454                 printk("%s: ", ioc->prod_name);
2455         printk("Capabilities={");
2456
2457         if (ioc->pfacts[0].ProtocolFlags & MPI_PORTFACTS_PROTOCOL_INITIATOR) {
2458                 printk("Initiator");
2459                 i++;
2460         }
2461
2462         if (ioc->pfacts[0].ProtocolFlags & MPI_PORTFACTS_PROTOCOL_TARGET) {
2463                 printk("%sTarget", i ? "," : "");
2464                 i++;
2465         }
2466
2467         if (ioc->pfacts[0].ProtocolFlags & MPI_PORTFACTS_PROTOCOL_LAN) {
2468                 printk("%sLAN", i ? "," : "");
2469                 i++;
2470         }
2471
2472 #if 0
2473         /*
2474          *  This would probably evoke more questions than it's worth
2475          */
2476         if (ioc->pfacts[0].ProtocolFlags & MPI_PORTFACTS_PROTOCOL_TARGET) {
2477                 printk("%sLogBusAddr", i ? "," : "");
2478                 i++;
2479         }
2480 #endif
2481
2482         printk("}\n");
2483 }
2484
2485 /*=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=*/
2486 /**
2487  *      MakeIocReady - Get IOC to a READY state, using KickStart if needed.
2488  *      @ioc: Pointer to MPT_ADAPTER structure
2489  *      @force: Force hard KickStart of IOC
2490  *      @sleepFlag: Specifies whether the process can sleep
2491  *
2492  *      Returns:
2493  *               1 - DIAG reset and READY
2494  *               0 - READY initially OR soft reset and READY
2495  *              -1 - Any failure on KickStart
2496  *              -2 - Msg Unit Reset Failed
2497  *              -3 - IO Unit Reset Failed
2498  *              -4 - IOC owned by a PEER
2499  */
2500 static int
2501 MakeIocReady(MPT_ADAPTER *ioc, int force, int sleepFlag)
2502 {
2503         u32      ioc_state;
2504         int      statefault = 0;
2505         int      cntdn;
2506         int      hard_reset_done = 0;
2507         int      r;
2508         int      ii;
2509         int      whoinit;
2510
2511         /* Get current [raw] IOC state  */
2512         ioc_state = mpt_GetIocState(ioc, 0);
2513         dhsprintk(ioc, printk(MYIOC_s_INFO_FMT "MakeIocReady [raw] state=%08x\n", ioc->name, ioc_state));
2514
2515         /*
2516          *      Check to see if IOC got left/stuck in doorbell handshake
2517          *      grip of death.  If so, hard reset the IOC.
2518          */
2519         if (ioc_state & MPI_DOORBELL_ACTIVE) {
2520                 statefault = 1;
2521                 printk(MYIOC_s_WARN_FMT "Unexpected doorbell active!\n",
2522                                 ioc->name);
2523         }
2524
2525         /* Is it already READY? */
2526         if (!statefault && (ioc_state & MPI_IOC_STATE_MASK) == MPI_IOC_STATE_READY)
2527                 return 0;
2528
2529         /*
2530          *      Check to see if IOC is in FAULT state.
2531          */
2532         if ((ioc_state & MPI_IOC_STATE_MASK) == MPI_IOC_STATE_FAULT) {
2533                 statefault = 2;
2534                 printk(MYIOC_s_WARN_FMT "IOC is in FAULT state!!!\n",
2535                     ioc->name);
2536                 printk(MYIOC_s_WARN_FMT "           FAULT code = %04xh\n",
2537                     ioc->name, ioc_state & MPI_DOORBELL_DATA_MASK);
2538         }
2539
2540         /*
2541          *      Hmmm...  Did it get left operational?
2542          */
2543         if ((ioc_state & MPI_IOC_STATE_MASK) == MPI_IOC_STATE_OPERATIONAL) {
2544                 dinitprintk(ioc, printk(MYIOC_s_DEBUG_FMT "IOC operational unexpected\n",
2545                                 ioc->name));
2546
2547                 /* Check WhoInit.
2548                  * If PCI Peer, exit.
2549                  * Else, if no fault conditions are present, issue a MessageUnitReset
2550                  * Else, fall through to KickStart case
2551                  */
2552                 whoinit = (ioc_state & MPI_DOORBELL_WHO_INIT_MASK) >> MPI_DOORBELL_WHO_INIT_SHIFT;
2553                 dinitprintk(ioc, printk(MYIOC_s_INFO_FMT
2554                         "whoinit 0x%x statefault %d force %d\n",
2555                         ioc->name, whoinit, statefault, force));
2556                 if (whoinit == MPI_WHOINIT_PCI_PEER)
2557                         return -4;
2558                 else {
2559                         if ((statefault == 0 ) && (force == 0)) {
2560                                 if ((r = SendIocReset(ioc, MPI_FUNCTION_IOC_MESSAGE_UNIT_RESET, sleepFlag)) == 0)
2561                                         return 0;
2562                         }
2563                         statefault = 3;
2564                 }
2565         }
2566
2567         hard_reset_done = KickStart(ioc, statefault||force, sleepFlag);
2568         if (hard_reset_done < 0)
2569                 return -1;
2570
2571         /*
2572          *  Loop here waiting for IOC to come READY.
2573          */
2574         ii = 0;
2575         cntdn = ((sleepFlag == CAN_SLEEP) ? HZ : 1000) * 5;     /* 5 seconds */
2576
2577         while ((ioc_state = mpt_GetIocState(ioc, 1)) != MPI_IOC_STATE_READY) {
2578                 if (ioc_state == MPI_IOC_STATE_OPERATIONAL) {
2579                         /*
2580                          *  BIOS or previous driver load left IOC in OP state.
2581                          *  Reset messaging FIFOs.
2582                          */
2583                         if ((r = SendIocReset(ioc, MPI_FUNCTION_IOC_MESSAGE_UNIT_RESET, sleepFlag)) != 0) {
2584                                 printk(MYIOC_s_ERR_FMT "IOC msg unit reset failed!\n", ioc->name);
2585                                 return -2;
2586                         }
2587                 } else if (ioc_state == MPI_IOC_STATE_RESET) {
2588                         /*
2589                          *  Something is wrong.  Try to get IOC back
2590                          *  to a known state.
2591                          */
2592                         if ((r = SendIocReset(ioc, MPI_FUNCTION_IO_UNIT_RESET, sleepFlag)) != 0) {
2593                                 printk(MYIOC_s_ERR_FMT "IO unit reset failed!\n", ioc->name);
2594                                 return -3;
2595                         }
2596                 }
2597
2598                 ii++; cntdn--;
2599                 if (!cntdn) {
2600                         printk(MYIOC_s_ERR_FMT "Wait IOC_READY state timeout(%d)!\n",
2601                                         ioc->name, (int)((ii+5)/HZ));
2602                         return -ETIME;
2603                 }
2604
2605                 if (sleepFlag == CAN_SLEEP) {
2606                         msleep(1);
2607                 } else {
2608                         mdelay (1);     /* 1 msec delay */
2609                 }
2610
2611         }
2612
2613         if (statefault < 3) {
2614                 printk(MYIOC_s_INFO_FMT "Recovered from %s\n",
2615                                 ioc->name,
2616                                 statefault==1 ? "stuck handshake" : "IOC FAULT");
2617         }
2618
2619         return hard_reset_done;
2620 }
2621
2622 /*=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=*/
2623 /**
2624  *      mpt_GetIocState - Get the current state of a MPT adapter.
2625  *      @ioc: Pointer to MPT_ADAPTER structure
2626  *      @cooked: Request raw or cooked IOC state
2627  *
2628  *      Returns all IOC Doorbell register bits if cooked==0, else just the
2629  *      Doorbell bits in MPI_IOC_STATE_MASK.
2630  */
2631 u32
2632 mpt_GetIocState(MPT_ADAPTER *ioc, int cooked)
2633 {
2634         u32 s, sc;
2635
2636         /*  Get!  */
2637         s = CHIPREG_READ32(&ioc->chip->Doorbell);
2638         sc = s & MPI_IOC_STATE_MASK;
2639
2640         /*  Save!  */
2641         ioc->last_state = sc;
2642
2643         return cooked ? sc : s;
2644 }
2645
2646 /*=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=*/
2647 /**
2648  *      GetIocFacts - Send IOCFacts request to MPT adapter.
2649  *      @ioc: Pointer to MPT_ADAPTER structure
2650  *      @sleepFlag: Specifies whether the process can sleep
2651  *      @reason: If recovery, only update facts.
2652  *
2653  *      Returns 0 for success, non-zero for failure.
2654  */
2655 static int
2656 GetIocFacts(MPT_ADAPTER *ioc, int sleepFlag, int reason)
2657 {
2658         IOCFacts_t               get_facts;
2659         IOCFactsReply_t         *facts;
2660         int                      r;
2661         int                      req_sz;
2662         int                      reply_sz;
2663         int                      sz;
2664         u32                      status, vv;
2665         u8                       shiftFactor=1;
2666
2667         /* IOC *must* NOT be in RESET state! */
2668         if (ioc->last_state == MPI_IOC_STATE_RESET) {
2669                 printk(MYIOC_s_ERR_FMT "Can't get IOCFacts NOT READY! (%08x)\n",
2670                     ioc->name, ioc->last_state );
2671                 return -44;
2672         }
2673
2674         facts = &ioc->facts;
2675
2676         /* Destination (reply area)... */
2677         reply_sz = sizeof(*facts);
2678         memset(facts, 0, reply_sz);
2679
2680         /* Request area (get_facts on the stack right now!) */
2681         req_sz = sizeof(get_facts);
2682         memset(&get_facts, 0, req_sz);
2683
2684         get_facts.Function = MPI_FUNCTION_IOC_FACTS;
2685         /* Assert: All other get_facts fields are zero! */
2686
2687         dinitprintk(ioc, printk(MYIOC_s_DEBUG_FMT
2688             "Sending get IocFacts request req_sz=%d reply_sz=%d\n",
2689             ioc->name, req_sz, reply_sz));
2690
2691         /* No non-zero fields in the get_facts request are greater than
2692          * 1 byte in size, so we can just fire it off as is.
2693          */
2694         r = mpt_handshake_req_reply_wait(ioc, req_sz, (u32*)&get_facts,
2695                         reply_sz, (u16*)facts, 5 /*seconds*/, sleepFlag);
2696         if (r != 0)
2697                 return r;
2698
2699         /*
2700          * Now byte swap (GRRR) the necessary fields before any further
2701          * inspection of reply contents.
2702          *
2703          * But need to do some sanity checks on MsgLength (byte) field
2704          * to make sure we don't zero IOC's req_sz!
2705          */
2706         /* Did we get a valid reply? */
2707         if (facts->MsgLength > offsetof(IOCFactsReply_t, RequestFrameSize)/sizeof(u32)) {
2708                 if (reason == MPT_HOSTEVENT_IOC_BRINGUP) {
2709                         /*
2710                          * If not been here, done that, save off first WhoInit value
2711                          */
2712                         if (ioc->FirstWhoInit == WHOINIT_UNKNOWN)
2713                                 ioc->FirstWhoInit = facts->WhoInit;
2714                 }
2715
2716                 facts->MsgVersion = le16_to_cpu(facts->MsgVersion);
2717                 facts->MsgContext = le32_to_cpu(facts->MsgContext);
2718                 facts->IOCExceptions = le16_to_cpu(facts->IOCExceptions);
2719                 facts->IOCStatus = le16_to_cpu(facts->IOCStatus);
2720                 facts->IOCLogInfo = le32_to_cpu(facts->IOCLogInfo);
2721                 status = le16_to_cpu(facts->IOCStatus) & MPI_IOCSTATUS_MASK;
2722                 /* CHECKME! IOCStatus, IOCLogInfo */
2723
2724                 facts->ReplyQueueDepth = le16_to_cpu(facts->ReplyQueueDepth);
2725                 facts->RequestFrameSize = le16_to_cpu(facts->RequestFrameSize);
2726
2727                 /*
2728                  * FC f/w version changed between 1.1 and 1.2
2729                  *      Old: u16{Major(4),Minor(4),SubMinor(8)}
2730                  *      New: u32{Major(8),Minor(8),Unit(8),Dev(8)}
2731                  */
2732                 if (facts->MsgVersion < 0x0102) {
2733                         /*
2734                          *      Handle old FC f/w style, convert to new...
2735                          */
2736                         u16      oldv = le16_to_cpu(facts->Reserved_0101_FWVersion);
2737                         facts->FWVersion.Word =
2738                                         ((oldv<<12) & 0xFF000000) |
2739                                         ((oldv<<8)  & 0x000FFF00);
2740                 } else
2741                         facts->FWVersion.Word = le32_to_cpu(facts->FWVersion.Word);
2742
2743                 facts->ProductID = le16_to_cpu(facts->ProductID);
2744                 if ((ioc->facts.ProductID & MPI_FW_HEADER_PID_PROD_MASK)
2745                     > MPI_FW_HEADER_PID_PROD_TARGET_SCSI)
2746                         ioc->ir_firmware = 1;
2747                 facts->CurrentHostMfaHighAddr =
2748                                 le32_to_cpu(facts->CurrentHostMfaHighAddr);
2749                 facts->GlobalCredits = le16_to_cpu(facts->GlobalCredits);
2750                 facts->CurrentSenseBufferHighAddr =
2751                                 le32_to_cpu(facts->CurrentSenseBufferHighAddr);
2752                 facts->CurReplyFrameSize =
2753                                 le16_to_cpu(facts->CurReplyFrameSize);
2754                 facts->IOCCapabilities = le32_to_cpu(facts->IOCCapabilities);
2755
2756                 /*
2757                  * Handle NEW (!) IOCFactsReply fields in MPI-1.01.xx
2758                  * Older MPI-1.00.xx struct had 13 dwords, and enlarged
2759                  * to 14 in MPI-1.01.0x.
2760                  */
2761                 if (facts->MsgLength >= (offsetof(IOCFactsReply_t,FWImageSize) + 7)/4 &&
2762                     facts->MsgVersion > 0x0100) {
2763                         facts->FWImageSize = le32_to_cpu(facts->FWImageSize);
2764                 }
2765
2766                 sz = facts->FWImageSize;
2767                 if ( sz & 0x01 )
2768                         sz += 1;
2769                 if ( sz & 0x02 )
2770                         sz += 2;
2771                 facts->FWImageSize = sz;
2772
2773                 if (!facts->RequestFrameSize) {
2774                         /*  Something is wrong!  */
2775                         printk(MYIOC_s_ERR_FMT "IOC reported invalid 0 request size!\n",
2776                                         ioc->name);
2777                         return -55;
2778                 }
2779
2780                 r = sz = facts->BlockSize;
2781                 vv = ((63 / (sz * 4)) + 1) & 0x03;
2782                 ioc->NB_for_64_byte_frame = vv;
2783                 while ( sz )
2784                 {
2785                         shiftFactor++;
2786                         sz = sz >> 1;
2787                 }
2788                 ioc->NBShiftFactor  = shiftFactor;
2789                 dinitprintk(ioc, printk(MYIOC_s_DEBUG_FMT
2790                     "NB_for_64_byte_frame=%x NBShiftFactor=%x BlockSize=%x\n",
2791                     ioc->name, vv, shiftFactor, r));
2792
2793                 if (reason == MPT_HOSTEVENT_IOC_BRINGUP) {
2794                         /*
2795                          * Set values for this IOC's request & reply frame sizes,
2796                          * and request & reply queue depths...
2797                          */
2798                         ioc->req_sz = min(MPT_DEFAULT_FRAME_SIZE, facts->RequestFrameSize * 4);
2799                         ioc->req_depth = min_t(int, MPT_MAX_REQ_DEPTH, facts->GlobalCredits);
2800                         ioc->reply_sz = MPT_REPLY_FRAME_SIZE;
2801                         ioc->reply_depth = min_t(int, MPT_DEFAULT_REPLY_DEPTH, facts->ReplyQueueDepth);
2802
2803                         dinitprintk(ioc, printk(MYIOC_s_DEBUG_FMT "reply_sz=%3d, reply_depth=%4d\n",
2804                                 ioc->name, ioc->reply_sz, ioc->reply_depth));
2805                         dinitprintk(ioc, printk(MYIOC_s_DEBUG_FMT "req_sz  =%3d, req_depth  =%4d\n",
2806                                 ioc->name, ioc->req_sz, ioc->req_depth));
2807
2808                         /* Get port facts! */
2809                         if ( (r = GetPortFacts(ioc, 0, sleepFlag)) != 0 )
2810                                 return r;
2811                 }
2812         } else {
2813                 printk(MYIOC_s_ERR_FMT
2814                      "Invalid IOC facts reply, msgLength=%d offsetof=%zd!\n",
2815                      ioc->name, facts->MsgLength, (offsetof(IOCFactsReply_t,
2816                      RequestFrameSize)/sizeof(u32)));
2817                 return -66;
2818         }
2819
2820         return 0;
2821 }
2822
2823 /*=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=*/
2824 /**
2825  *      GetPortFacts - Send PortFacts request to MPT adapter.
2826  *      @ioc: Pointer to MPT_ADAPTER structure
2827  *      @portnum: Port number
2828  *      @sleepFlag: Specifies whether the process can sleep
2829  *
2830  *      Returns 0 for success, non-zero for failure.
2831  */
2832 static int
2833 GetPortFacts(MPT_ADAPTER *ioc, int portnum, int sleepFlag)
2834 {
2835         PortFacts_t              get_pfacts;
2836         PortFactsReply_t        *pfacts;
2837         int                      ii;
2838         int                      req_sz;
2839         int                      reply_sz;
2840         int                      max_id;
2841
2842         /* IOC *must* NOT be in RESET state! */
2843         if (ioc->last_state == MPI_IOC_STATE_RESET) {
2844                 printk(MYIOC_s_ERR_FMT "Can't get PortFacts NOT READY! (%08x)\n",
2845                     ioc->name, ioc->last_state );
2846                 return -4;
2847         }
2848
2849         pfacts = &ioc->pfacts[portnum];
2850
2851         /* Destination (reply area)...  */
2852         reply_sz = sizeof(*pfacts);
2853         memset(pfacts, 0, reply_sz);
2854
2855         /* Request area (get_pfacts on the stack right now!) */
2856         req_sz = sizeof(get_pfacts);
2857         memset(&get_pfacts, 0, req_sz);
2858
2859         get_pfacts.Function = MPI_FUNCTION_PORT_FACTS;
2860         get_pfacts.PortNumber = portnum;
2861         /* Assert: All other get_pfacts fields are zero! */
2862
2863         dinitprintk(ioc, printk(MYIOC_s_DEBUG_FMT "Sending get PortFacts(%d) request\n",
2864                         ioc->name, portnum));
2865
2866         /* No non-zero fields in the get_pfacts request are greater than
2867          * 1 byte in size, so we can just fire it off as is.
2868          */
2869         ii = mpt_handshake_req_reply_wait(ioc, req_sz, (u32*)&get_pfacts,
2870                                 reply_sz, (u16*)pfacts, 5 /*seconds*/, sleepFlag);
2871         if (ii != 0)
2872                 return ii;
2873
2874         /* Did we get a valid reply? */
2875
2876         /* Now byte swap the necessary fields in the response. */
2877         pfacts->MsgContext = le32_to_cpu(pfacts->MsgContext);
2878         pfacts->IOCStatus = le16_to_cpu(pfacts->IOCStatus);
2879         pfacts->IOCLogInfo = le32_to_cpu(pfacts->IOCLogInfo);
2880         pfacts->MaxDevices = le16_to_cpu(pfacts->MaxDevices);
2881         pfacts->PortSCSIID = le16_to_cpu(pfacts->PortSCSIID);
2882         pfacts->ProtocolFlags = le16_to_cpu(pfacts->ProtocolFlags);
2883         pfacts->MaxPostedCmdBuffers = le16_to_cpu(pfacts->MaxPostedCmdBuffers);
2884         pfacts->MaxPersistentIDs = le16_to_cpu(pfacts->MaxPersistentIDs);
2885         pfacts->MaxLanBuckets = le16_to_cpu(pfacts->MaxLanBuckets);
2886
2887         max_id = (ioc->bus_type == SAS) ? pfacts->PortSCSIID :
2888             pfacts->MaxDevices;
2889         ioc->devices_per_bus = (max_id > 255) ? 256 : max_id;
2890         ioc->number_of_buses = (ioc->devices_per_bus < 256) ? 1 : max_id/256;
2891
2892         /*
2893          * Place all the devices on channels
2894          *
2895          * (for debuging)
2896          */
2897         if (mpt_channel_mapping) {
2898                 ioc->devices_per_bus = 1;
2899                 ioc->number_of_buses = (max_id > 255) ? 255 : max_id;
2900         }
2901
2902         return 0;
2903 }
2904
2905 /*=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=*/
2906 /**
2907  *      SendIocInit - Send IOCInit request to MPT adapter.
2908  *      @ioc: Pointer to MPT_ADAPTER structure
2909  *      @sleepFlag: Specifies whether the process can sleep
2910  *
2911  *      Send IOCInit followed by PortEnable to bring IOC to OPERATIONAL state.
2912  *
2913  *      Returns 0 for success, non-zero for failure.
2914  */
2915 static int
2916 SendIocInit(MPT_ADAPTER *ioc, int sleepFlag)
2917 {
2918         IOCInit_t                ioc_init;
2919         MPIDefaultReply_t        init_reply;
2920         u32                      state;
2921         int                      r;
2922         int                      count;
2923         int                      cntdn;
2924
2925         memset(&ioc_init, 0, sizeof(ioc_init));
2926         memset(&init_reply, 0, sizeof(init_reply));
2927
2928         ioc_init.WhoInit = MPI_WHOINIT_HOST_DRIVER;
2929         ioc_init.Function = MPI_FUNCTION_IOC_INIT;
2930
2931         /* If we are in a recovery mode and we uploaded the FW image,
2932          * then this pointer is not NULL. Skip the upload a second time.
2933          * Set this flag if cached_fw set for either IOC.
2934          */
2935         if (ioc->facts.Flags & MPI_IOCFACTS_FLAGS_FW_DOWNLOAD_BOOT)
2936                 ioc->upload_fw = 1;
2937         else
2938                 ioc->upload_fw = 0;
2939         ddlprintk(ioc, printk(MYIOC_s_DEBUG_FMT "upload_fw %d facts.Flags=%x\n",
2940                    ioc->name, ioc->upload_fw, ioc->facts.Flags));
2941
2942         ioc_init.MaxDevices = (U8)ioc->devices_per_bus;
2943         ioc_init.MaxBuses = (U8)ioc->number_of_buses;
2944         dinitprintk(ioc, printk(MYIOC_s_DEBUG_FMT "facts.MsgVersion=%x\n",
2945                    ioc->name, ioc->facts.MsgVersion));
2946         if (ioc->facts.MsgVersion >= MPI_VERSION_01_05) {
2947                 // set MsgVersion and HeaderVersion host driver was built with
2948                 ioc_init.MsgVersion = cpu_to_le16(MPI_VERSION);
2949                 ioc_init.HeaderVersion = cpu_to_le16(MPI_HEADER_VERSION);
2950
2951                 if (ioc->facts.Flags & MPI_IOCFACTS_FLAGS_HOST_PAGE_BUFFER_PERSISTENT) {
2952                         ioc_init.HostPageBufferSGE = ioc->facts.HostPageBufferSGE;
2953                 } else if(mpt_host_page_alloc(ioc, &ioc_init))
2954                         return -99;
2955         }
2956         ioc_init.ReplyFrameSize = cpu_to_le16(ioc->reply_sz);   /* in BYTES */
2957
2958         if (sizeof(dma_addr_t) == sizeof(u64)) {
2959                 /* Save the upper 32-bits of the request
2960                  * (reply) and sense buffers.
2961                  */
2962                 ioc_init.HostMfaHighAddr = cpu_to_le32((u32)((u64)ioc->alloc_dma >> 32));
2963                 ioc_init.SenseBufferHighAddr = cpu_to_le32((u32)((u64)ioc->sense_buf_pool_dma >> 32));
2964         } else {
2965                 /* Force 32-bit addressing */
2966                 ioc_init.HostMfaHighAddr = cpu_to_le32(0);
2967                 ioc_init.SenseBufferHighAddr = cpu_to_le32(0);
2968         }
2969
2970         ioc->facts.CurrentHostMfaHighAddr = ioc_init.HostMfaHighAddr;
2971         ioc->facts.CurrentSenseBufferHighAddr = ioc_init.SenseBufferHighAddr;
2972         ioc->facts.MaxDevices = ioc_init.MaxDevices;
2973         ioc->facts.MaxBuses = ioc_init.MaxBuses;
2974
2975         dhsprintk(ioc, printk(MYIOC_s_DEBUG_FMT "Sending IOCInit (req @ %p)\n",
2976                         ioc->name, &ioc_init));
2977
2978         r = mpt_handshake_req_reply_wait(ioc, sizeof(IOCInit_t), (u32*)&ioc_init,
2979                                 sizeof(MPIDefaultReply_t), (u16*)&init_reply, 10 /*seconds*/, sleepFlag);
2980         if (r != 0) {
2981                 printk(MYIOC_s_ERR_FMT "Sending IOCInit failed(%d)!\n",ioc->name, r);
2982                 return r;
2983         }
2984
2985         /* No need to byte swap the multibyte fields in the reply
2986          * since we don't even look at its contents.
2987          */
2988
2989         dhsprintk(ioc, printk(MYIOC_s_DEBUG_FMT "Sending PortEnable (req @ %p)\n",
2990                         ioc->name, &ioc_init));
2991
2992         if ((r = SendPortEnable(ioc, 0, sleepFlag)) != 0) {
2993                 printk(MYIOC_s_ERR_FMT "Sending PortEnable failed(%d)!\n",ioc->name, r);
2994                 return r;
2995         }
2996
2997         /* YIKES!  SUPER IMPORTANT!!!
2998          *  Poll IocState until _OPERATIONAL while IOC is doing
2999          *  LoopInit and TargetDiscovery!
3000          */
3001         count = 0;
3002         cntdn = ((sleepFlag == CAN_SLEEP) ? HZ : 1000) * 60;    /* 60 seconds */
3003         state = mpt_GetIocState(ioc, 1);
3004         while (state != MPI_IOC_STATE_OPERATIONAL && --cntdn) {
3005                 if (sleepFlag == CAN_SLEEP) {
3006                         msleep(1);
3007                 } else {
3008                         mdelay(1);
3009                 }
3010
3011                 if (!cntdn) {
3012                         printk(MYIOC_s_ERR_FMT "Wait IOC_OP state timeout(%d)!\n",
3013                                         ioc->name, (int)((count+5)/HZ));
3014                         return -9;
3015                 }
3016
3017                 state = mpt_GetIocState(ioc, 1);
3018                 count++;
3019         }
3020         dinitprintk(ioc, printk(MYIOC_s_DEBUG_FMT "Wait IOC_OPERATIONAL state (cnt=%d)\n",
3021                         ioc->name, count));
3022
3023         ioc->aen_event_read_flag=0;
3024         return r;
3025 }
3026
3027 /*=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=*/
3028 /**
3029  *      SendPortEnable - Send PortEnable request to MPT adapter port.
3030  *      @ioc: Pointer to MPT_ADAPTER structure
3031  *      @portnum: Port number to enable
3032  *      @sleepFlag: Specifies whether the process can sleep
3033  *
3034  *      Send PortEnable to bring IOC to OPERATIONAL state.
3035  *
3036  *      Returns 0 for success, non-zero for failure.
3037  */
3038 static int